1 /* 2 * include/net/devlink.h - Network physical device Netlink interface 3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved. 4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 */ 11 #ifndef _NET_DEVLINK_H_ 12 #define _NET_DEVLINK_H_ 13 14 #include <linux/device.h> 15 #include <linux/slab.h> 16 #include <linux/gfp.h> 17 #include <linux/list.h> 18 #include <linux/netdevice.h> 19 #include <net/net_namespace.h> 20 #include <uapi/linux/devlink.h> 21 22 struct devlink_ops; 23 24 struct devlink { 25 struct list_head list; 26 struct list_head port_list; 27 struct list_head sb_list; 28 struct list_head dpipe_table_list; 29 struct list_head resource_list; 30 struct devlink_dpipe_headers *dpipe_headers; 31 const struct devlink_ops *ops; 32 struct device *dev; 33 possible_net_t _net; 34 struct mutex lock; 35 char priv[0] __aligned(NETDEV_ALIGN); 36 }; 37 38 struct devlink_port_attrs { 39 bool set; 40 enum devlink_port_flavour flavour; 41 u32 port_number; /* same value as "split group" */ 42 bool split; 43 u32 split_subport_number; 44 }; 45 46 struct devlink_port { 47 struct list_head list; 48 struct devlink *devlink; 49 unsigned index; 50 bool registered; 51 enum devlink_port_type type; 52 enum devlink_port_type desired_type; 53 void *type_dev; 54 struct devlink_port_attrs attrs; 55 }; 56 57 struct devlink_sb_pool_info { 58 enum devlink_sb_pool_type pool_type; 59 u32 size; 60 enum devlink_sb_threshold_type threshold_type; 61 }; 62 63 /** 64 * struct devlink_dpipe_field - dpipe field object 65 * @name: field name 66 * @id: index inside the headers field array 67 * @bitwidth: bitwidth 68 * @mapping_type: mapping type 69 */ 70 struct devlink_dpipe_field { 71 const char *name; 72 unsigned int id; 73 unsigned int bitwidth; 74 enum devlink_dpipe_field_mapping_type mapping_type; 75 }; 76 77 /** 78 * struct devlink_dpipe_header - dpipe header object 79 * @name: header name 80 * @id: index, global/local detrmined by global bit 81 * @fields: fields 82 * @fields_count: number of fields 83 * @global: indicates if header is shared like most protocol header 84 * or driver specific 85 */ 86 struct devlink_dpipe_header { 87 const char *name; 88 unsigned int id; 89 struct devlink_dpipe_field *fields; 90 unsigned int fields_count; 91 bool global; 92 }; 93 94 /** 95 * struct devlink_dpipe_match - represents match operation 96 * @type: type of match 97 * @header_index: header index (packets can have several headers of same 98 * type like in case of tunnels) 99 * @header: header 100 * @fieled_id: field index 101 */ 102 struct devlink_dpipe_match { 103 enum devlink_dpipe_match_type type; 104 unsigned int header_index; 105 struct devlink_dpipe_header *header; 106 unsigned int field_id; 107 }; 108 109 /** 110 * struct devlink_dpipe_action - represents action operation 111 * @type: type of action 112 * @header_index: header index (packets can have several headers of same 113 * type like in case of tunnels) 114 * @header: header 115 * @fieled_id: field index 116 */ 117 struct devlink_dpipe_action { 118 enum devlink_dpipe_action_type type; 119 unsigned int header_index; 120 struct devlink_dpipe_header *header; 121 unsigned int field_id; 122 }; 123 124 /** 125 * struct devlink_dpipe_value - represents value of match/action 126 * @action: action 127 * @match: match 128 * @mapping_value: in case the field has some mapping this value 129 * specified the mapping value 130 * @mapping_valid: specify if mapping value is valid 131 * @value_size: value size 132 * @value: value 133 * @mask: bit mask 134 */ 135 struct devlink_dpipe_value { 136 union { 137 struct devlink_dpipe_action *action; 138 struct devlink_dpipe_match *match; 139 }; 140 unsigned int mapping_value; 141 bool mapping_valid; 142 unsigned int value_size; 143 void *value; 144 void *mask; 145 }; 146 147 /** 148 * struct devlink_dpipe_entry - table entry object 149 * @index: index of the entry in the table 150 * @match_values: match values 151 * @matche_values_count: count of matches tuples 152 * @action_values: actions values 153 * @action_values_count: count of actions values 154 * @counter: value of counter 155 * @counter_valid: Specify if value is valid from hardware 156 */ 157 struct devlink_dpipe_entry { 158 u64 index; 159 struct devlink_dpipe_value *match_values; 160 unsigned int match_values_count; 161 struct devlink_dpipe_value *action_values; 162 unsigned int action_values_count; 163 u64 counter; 164 bool counter_valid; 165 }; 166 167 /** 168 * struct devlink_dpipe_dump_ctx - context provided to driver in order 169 * to dump 170 * @info: info 171 * @cmd: devlink command 172 * @skb: skb 173 * @nest: top attribute 174 * @hdr: hdr 175 */ 176 struct devlink_dpipe_dump_ctx { 177 struct genl_info *info; 178 enum devlink_command cmd; 179 struct sk_buff *skb; 180 struct nlattr *nest; 181 void *hdr; 182 }; 183 184 struct devlink_dpipe_table_ops; 185 186 /** 187 * struct devlink_dpipe_table - table object 188 * @priv: private 189 * @name: table name 190 * @counters_enabled: indicates if counters are active 191 * @counter_control_extern: indicates if counter control is in dpipe or 192 * external tool 193 * @resource_valid: Indicate that the resource id is valid 194 * @resource_id: relative resource this table is related to 195 * @resource_units: number of resource's unit consumed per table's entry 196 * @table_ops: table operations 197 * @rcu: rcu 198 */ 199 struct devlink_dpipe_table { 200 void *priv; 201 struct list_head list; 202 const char *name; 203 bool counters_enabled; 204 bool counter_control_extern; 205 bool resource_valid; 206 u64 resource_id; 207 u64 resource_units; 208 struct devlink_dpipe_table_ops *table_ops; 209 struct rcu_head rcu; 210 }; 211 212 /** 213 * struct devlink_dpipe_table_ops - dpipe_table ops 214 * @actions_dump - dumps all tables actions 215 * @matches_dump - dumps all tables matches 216 * @entries_dump - dumps all active entries in the table 217 * @counters_set_update - when changing the counter status hardware sync 218 * maybe needed to allocate/free counter related 219 * resources 220 * @size_get - get size 221 */ 222 struct devlink_dpipe_table_ops { 223 int (*actions_dump)(void *priv, struct sk_buff *skb); 224 int (*matches_dump)(void *priv, struct sk_buff *skb); 225 int (*entries_dump)(void *priv, bool counters_enabled, 226 struct devlink_dpipe_dump_ctx *dump_ctx); 227 int (*counters_set_update)(void *priv, bool enable); 228 u64 (*size_get)(void *priv); 229 }; 230 231 /** 232 * struct devlink_dpipe_headers - dpipe headers 233 * @headers - header array can be shared (global bit) or driver specific 234 * @headers_count - count of headers 235 */ 236 struct devlink_dpipe_headers { 237 struct devlink_dpipe_header **headers; 238 unsigned int headers_count; 239 }; 240 241 /** 242 * struct devlink_resource_size_params - resource's size parameters 243 * @size_min: minimum size which can be set 244 * @size_max: maximum size which can be set 245 * @size_granularity: size granularity 246 * @size_unit: resource's basic unit 247 */ 248 struct devlink_resource_size_params { 249 u64 size_min; 250 u64 size_max; 251 u64 size_granularity; 252 enum devlink_resource_unit unit; 253 }; 254 255 static inline void 256 devlink_resource_size_params_init(struct devlink_resource_size_params *size_params, 257 u64 size_min, u64 size_max, 258 u64 size_granularity, 259 enum devlink_resource_unit unit) 260 { 261 size_params->size_min = size_min; 262 size_params->size_max = size_max; 263 size_params->size_granularity = size_granularity; 264 size_params->unit = unit; 265 } 266 267 typedef u64 devlink_resource_occ_get_t(void *priv); 268 269 /** 270 * struct devlink_resource - devlink resource 271 * @name: name of the resource 272 * @id: id, per devlink instance 273 * @size: size of the resource 274 * @size_new: updated size of the resource, reload is needed 275 * @size_valid: valid in case the total size of the resource is valid 276 * including its children 277 * @parent: parent resource 278 * @size_params: size parameters 279 * @list: parent list 280 * @resource_list: list of child resources 281 */ 282 struct devlink_resource { 283 const char *name; 284 u64 id; 285 u64 size; 286 u64 size_new; 287 bool size_valid; 288 struct devlink_resource *parent; 289 struct devlink_resource_size_params size_params; 290 struct list_head list; 291 struct list_head resource_list; 292 devlink_resource_occ_get_t *occ_get; 293 void *occ_get_priv; 294 }; 295 296 #define DEVLINK_RESOURCE_ID_PARENT_TOP 0 297 298 struct devlink_ops { 299 int (*reload)(struct devlink *devlink); 300 int (*port_type_set)(struct devlink_port *devlink_port, 301 enum devlink_port_type port_type); 302 int (*port_split)(struct devlink *devlink, unsigned int port_index, 303 unsigned int count); 304 int (*port_unsplit)(struct devlink *devlink, unsigned int port_index); 305 int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index, 306 u16 pool_index, 307 struct devlink_sb_pool_info *pool_info); 308 int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index, 309 u16 pool_index, u32 size, 310 enum devlink_sb_threshold_type threshold_type); 311 int (*sb_port_pool_get)(struct devlink_port *devlink_port, 312 unsigned int sb_index, u16 pool_index, 313 u32 *p_threshold); 314 int (*sb_port_pool_set)(struct devlink_port *devlink_port, 315 unsigned int sb_index, u16 pool_index, 316 u32 threshold); 317 int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port, 318 unsigned int sb_index, 319 u16 tc_index, 320 enum devlink_sb_pool_type pool_type, 321 u16 *p_pool_index, u32 *p_threshold); 322 int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port, 323 unsigned int sb_index, 324 u16 tc_index, 325 enum devlink_sb_pool_type pool_type, 326 u16 pool_index, u32 threshold); 327 int (*sb_occ_snapshot)(struct devlink *devlink, 328 unsigned int sb_index); 329 int (*sb_occ_max_clear)(struct devlink *devlink, 330 unsigned int sb_index); 331 int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port, 332 unsigned int sb_index, u16 pool_index, 333 u32 *p_cur, u32 *p_max); 334 int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port, 335 unsigned int sb_index, 336 u16 tc_index, 337 enum devlink_sb_pool_type pool_type, 338 u32 *p_cur, u32 *p_max); 339 340 int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode); 341 int (*eswitch_mode_set)(struct devlink *devlink, u16 mode); 342 int (*eswitch_inline_mode_get)(struct devlink *devlink, u8 *p_inline_mode); 343 int (*eswitch_inline_mode_set)(struct devlink *devlink, u8 inline_mode); 344 int (*eswitch_encap_mode_get)(struct devlink *devlink, u8 *p_encap_mode); 345 int (*eswitch_encap_mode_set)(struct devlink *devlink, u8 encap_mode); 346 }; 347 348 static inline void *devlink_priv(struct devlink *devlink) 349 { 350 BUG_ON(!devlink); 351 return &devlink->priv; 352 } 353 354 static inline struct devlink *priv_to_devlink(void *priv) 355 { 356 BUG_ON(!priv); 357 return container_of(priv, struct devlink, priv); 358 } 359 360 struct ib_device; 361 362 #if IS_ENABLED(CONFIG_NET_DEVLINK) 363 364 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size); 365 int devlink_register(struct devlink *devlink, struct device *dev); 366 void devlink_unregister(struct devlink *devlink); 367 void devlink_free(struct devlink *devlink); 368 int devlink_port_register(struct devlink *devlink, 369 struct devlink_port *devlink_port, 370 unsigned int port_index); 371 void devlink_port_unregister(struct devlink_port *devlink_port); 372 void devlink_port_type_eth_set(struct devlink_port *devlink_port, 373 struct net_device *netdev); 374 void devlink_port_type_ib_set(struct devlink_port *devlink_port, 375 struct ib_device *ibdev); 376 void devlink_port_type_clear(struct devlink_port *devlink_port); 377 void devlink_port_attrs_set(struct devlink_port *devlink_port, 378 enum devlink_port_flavour flavour, 379 u32 port_number, bool split, 380 u32 split_subport_number); 381 int devlink_port_get_phys_port_name(struct devlink_port *devlink_port, 382 char *name, size_t len); 383 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index, 384 u32 size, u16 ingress_pools_count, 385 u16 egress_pools_count, u16 ingress_tc_count, 386 u16 egress_tc_count); 387 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index); 388 int devlink_dpipe_table_register(struct devlink *devlink, 389 const char *table_name, 390 struct devlink_dpipe_table_ops *table_ops, 391 void *priv, bool counter_control_extern); 392 void devlink_dpipe_table_unregister(struct devlink *devlink, 393 const char *table_name); 394 int devlink_dpipe_headers_register(struct devlink *devlink, 395 struct devlink_dpipe_headers *dpipe_headers); 396 void devlink_dpipe_headers_unregister(struct devlink *devlink); 397 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink, 398 const char *table_name); 399 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx); 400 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx, 401 struct devlink_dpipe_entry *entry); 402 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx); 403 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry); 404 int devlink_dpipe_action_put(struct sk_buff *skb, 405 struct devlink_dpipe_action *action); 406 int devlink_dpipe_match_put(struct sk_buff *skb, 407 struct devlink_dpipe_match *match); 408 extern struct devlink_dpipe_header devlink_dpipe_header_ethernet; 409 extern struct devlink_dpipe_header devlink_dpipe_header_ipv4; 410 extern struct devlink_dpipe_header devlink_dpipe_header_ipv6; 411 412 int devlink_resource_register(struct devlink *devlink, 413 const char *resource_name, 414 u64 resource_size, 415 u64 resource_id, 416 u64 parent_resource_id, 417 const struct devlink_resource_size_params *size_params); 418 void devlink_resources_unregister(struct devlink *devlink, 419 struct devlink_resource *resource); 420 int devlink_resource_size_get(struct devlink *devlink, 421 u64 resource_id, 422 u64 *p_resource_size); 423 int devlink_dpipe_table_resource_set(struct devlink *devlink, 424 const char *table_name, u64 resource_id, 425 u64 resource_units); 426 void devlink_resource_occ_get_register(struct devlink *devlink, 427 u64 resource_id, 428 devlink_resource_occ_get_t *occ_get, 429 void *occ_get_priv); 430 void devlink_resource_occ_get_unregister(struct devlink *devlink, 431 u64 resource_id); 432 433 #else 434 435 static inline struct devlink *devlink_alloc(const struct devlink_ops *ops, 436 size_t priv_size) 437 { 438 return kzalloc(sizeof(struct devlink) + priv_size, GFP_KERNEL); 439 } 440 441 static inline int devlink_register(struct devlink *devlink, struct device *dev) 442 { 443 return 0; 444 } 445 446 static inline void devlink_unregister(struct devlink *devlink) 447 { 448 } 449 450 static inline void devlink_free(struct devlink *devlink) 451 { 452 kfree(devlink); 453 } 454 455 static inline int devlink_port_register(struct devlink *devlink, 456 struct devlink_port *devlink_port, 457 unsigned int port_index) 458 { 459 return 0; 460 } 461 462 static inline void devlink_port_unregister(struct devlink_port *devlink_port) 463 { 464 } 465 466 static inline void devlink_port_type_eth_set(struct devlink_port *devlink_port, 467 struct net_device *netdev) 468 { 469 } 470 471 static inline void devlink_port_type_ib_set(struct devlink_port *devlink_port, 472 struct ib_device *ibdev) 473 { 474 } 475 476 static inline void devlink_port_type_clear(struct devlink_port *devlink_port) 477 { 478 } 479 480 static inline void devlink_port_attrs_set(struct devlink_port *devlink_port, 481 enum devlink_port_flavour flavour, 482 u32 port_number, bool split, 483 u32 split_subport_number) 484 { 485 } 486 487 static inline int 488 devlink_port_get_phys_port_name(struct devlink_port *devlink_port, 489 char *name, size_t len) 490 { 491 return -EOPNOTSUPP; 492 } 493 494 static inline int devlink_sb_register(struct devlink *devlink, 495 unsigned int sb_index, u32 size, 496 u16 ingress_pools_count, 497 u16 egress_pools_count, 498 u16 ingress_tc_count, 499 u16 egress_tc_count) 500 { 501 return 0; 502 } 503 504 static inline void devlink_sb_unregister(struct devlink *devlink, 505 unsigned int sb_index) 506 { 507 } 508 509 static inline int 510 devlink_dpipe_table_register(struct devlink *devlink, 511 const char *table_name, 512 struct devlink_dpipe_table_ops *table_ops, 513 void *priv, bool counter_control_extern) 514 { 515 return 0; 516 } 517 518 static inline void devlink_dpipe_table_unregister(struct devlink *devlink, 519 const char *table_name) 520 { 521 } 522 523 static inline int devlink_dpipe_headers_register(struct devlink *devlink, 524 struct devlink_dpipe_headers * 525 dpipe_headers) 526 { 527 return 0; 528 } 529 530 static inline void devlink_dpipe_headers_unregister(struct devlink *devlink) 531 { 532 } 533 534 static inline bool devlink_dpipe_table_counter_enabled(struct devlink *devlink, 535 const char *table_name) 536 { 537 return false; 538 } 539 540 static inline int 541 devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx) 542 { 543 return 0; 544 } 545 546 static inline int 547 devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx, 548 struct devlink_dpipe_entry *entry) 549 { 550 return 0; 551 } 552 553 static inline int 554 devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx) 555 { 556 return 0; 557 } 558 559 static inline void 560 devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry) 561 { 562 } 563 564 static inline int 565 devlink_dpipe_action_put(struct sk_buff *skb, 566 struct devlink_dpipe_action *action) 567 { 568 return 0; 569 } 570 571 static inline int 572 devlink_dpipe_match_put(struct sk_buff *skb, 573 struct devlink_dpipe_match *match) 574 { 575 return 0; 576 } 577 578 static inline int 579 devlink_resource_register(struct devlink *devlink, 580 const char *resource_name, 581 u64 resource_size, 582 u64 resource_id, 583 u64 parent_resource_id, 584 const struct devlink_resource_size_params *size_params) 585 { 586 return 0; 587 } 588 589 static inline void 590 devlink_resources_unregister(struct devlink *devlink, 591 struct devlink_resource *resource) 592 { 593 } 594 595 static inline int 596 devlink_resource_size_get(struct devlink *devlink, u64 resource_id, 597 u64 *p_resource_size) 598 { 599 return -EOPNOTSUPP; 600 } 601 602 static inline int 603 devlink_dpipe_table_resource_set(struct devlink *devlink, 604 const char *table_name, u64 resource_id, 605 u64 resource_units) 606 { 607 return -EOPNOTSUPP; 608 } 609 610 static inline void 611 devlink_resource_occ_get_register(struct devlink *devlink, 612 u64 resource_id, 613 devlink_resource_occ_get_t *occ_get, 614 void *occ_get_priv) 615 { 616 } 617 618 static inline void 619 devlink_resource_occ_get_unregister(struct devlink *devlink, 620 u64 resource_id) 621 { 622 } 623 624 #endif 625 626 #endif /* _NET_DEVLINK_H_ */ 627