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, struct netlink_ext_ack *extack); 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, struct netlink_ext_ack *extack); 304 int (*port_unsplit)(struct devlink *devlink, unsigned int port_index, 305 struct netlink_ext_ack *extack); 306 int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index, 307 u16 pool_index, 308 struct devlink_sb_pool_info *pool_info); 309 int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index, 310 u16 pool_index, u32 size, 311 enum devlink_sb_threshold_type threshold_type); 312 int (*sb_port_pool_get)(struct devlink_port *devlink_port, 313 unsigned int sb_index, u16 pool_index, 314 u32 *p_threshold); 315 int (*sb_port_pool_set)(struct devlink_port *devlink_port, 316 unsigned int sb_index, u16 pool_index, 317 u32 threshold); 318 int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port, 319 unsigned int sb_index, 320 u16 tc_index, 321 enum devlink_sb_pool_type pool_type, 322 u16 *p_pool_index, u32 *p_threshold); 323 int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port, 324 unsigned int sb_index, 325 u16 tc_index, 326 enum devlink_sb_pool_type pool_type, 327 u16 pool_index, u32 threshold); 328 int (*sb_occ_snapshot)(struct devlink *devlink, 329 unsigned int sb_index); 330 int (*sb_occ_max_clear)(struct devlink *devlink, 331 unsigned int sb_index); 332 int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port, 333 unsigned int sb_index, u16 pool_index, 334 u32 *p_cur, u32 *p_max); 335 int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port, 336 unsigned int sb_index, 337 u16 tc_index, 338 enum devlink_sb_pool_type pool_type, 339 u32 *p_cur, u32 *p_max); 340 341 int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode); 342 int (*eswitch_mode_set)(struct devlink *devlink, u16 mode); 343 int (*eswitch_inline_mode_get)(struct devlink *devlink, u8 *p_inline_mode); 344 int (*eswitch_inline_mode_set)(struct devlink *devlink, u8 inline_mode); 345 int (*eswitch_encap_mode_get)(struct devlink *devlink, u8 *p_encap_mode); 346 int (*eswitch_encap_mode_set)(struct devlink *devlink, u8 encap_mode); 347 }; 348 349 static inline void *devlink_priv(struct devlink *devlink) 350 { 351 BUG_ON(!devlink); 352 return &devlink->priv; 353 } 354 355 static inline struct devlink *priv_to_devlink(void *priv) 356 { 357 BUG_ON(!priv); 358 return container_of(priv, struct devlink, priv); 359 } 360 361 struct ib_device; 362 363 #if IS_ENABLED(CONFIG_NET_DEVLINK) 364 365 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size); 366 int devlink_register(struct devlink *devlink, struct device *dev); 367 void devlink_unregister(struct devlink *devlink); 368 void devlink_free(struct devlink *devlink); 369 int devlink_port_register(struct devlink *devlink, 370 struct devlink_port *devlink_port, 371 unsigned int port_index); 372 void devlink_port_unregister(struct devlink_port *devlink_port); 373 void devlink_port_type_eth_set(struct devlink_port *devlink_port, 374 struct net_device *netdev); 375 void devlink_port_type_ib_set(struct devlink_port *devlink_port, 376 struct ib_device *ibdev); 377 void devlink_port_type_clear(struct devlink_port *devlink_port); 378 void devlink_port_attrs_set(struct devlink_port *devlink_port, 379 enum devlink_port_flavour flavour, 380 u32 port_number, bool split, 381 u32 split_subport_number); 382 int devlink_port_get_phys_port_name(struct devlink_port *devlink_port, 383 char *name, size_t len); 384 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index, 385 u32 size, u16 ingress_pools_count, 386 u16 egress_pools_count, u16 ingress_tc_count, 387 u16 egress_tc_count); 388 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index); 389 int devlink_dpipe_table_register(struct devlink *devlink, 390 const char *table_name, 391 struct devlink_dpipe_table_ops *table_ops, 392 void *priv, bool counter_control_extern); 393 void devlink_dpipe_table_unregister(struct devlink *devlink, 394 const char *table_name); 395 int devlink_dpipe_headers_register(struct devlink *devlink, 396 struct devlink_dpipe_headers *dpipe_headers); 397 void devlink_dpipe_headers_unregister(struct devlink *devlink); 398 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink, 399 const char *table_name); 400 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx); 401 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx, 402 struct devlink_dpipe_entry *entry); 403 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx); 404 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry); 405 int devlink_dpipe_action_put(struct sk_buff *skb, 406 struct devlink_dpipe_action *action); 407 int devlink_dpipe_match_put(struct sk_buff *skb, 408 struct devlink_dpipe_match *match); 409 extern struct devlink_dpipe_header devlink_dpipe_header_ethernet; 410 extern struct devlink_dpipe_header devlink_dpipe_header_ipv4; 411 extern struct devlink_dpipe_header devlink_dpipe_header_ipv6; 412 413 int devlink_resource_register(struct devlink *devlink, 414 const char *resource_name, 415 u64 resource_size, 416 u64 resource_id, 417 u64 parent_resource_id, 418 const struct devlink_resource_size_params *size_params); 419 void devlink_resources_unregister(struct devlink *devlink, 420 struct devlink_resource *resource); 421 int devlink_resource_size_get(struct devlink *devlink, 422 u64 resource_id, 423 u64 *p_resource_size); 424 int devlink_dpipe_table_resource_set(struct devlink *devlink, 425 const char *table_name, u64 resource_id, 426 u64 resource_units); 427 void devlink_resource_occ_get_register(struct devlink *devlink, 428 u64 resource_id, 429 devlink_resource_occ_get_t *occ_get, 430 void *occ_get_priv); 431 void devlink_resource_occ_get_unregister(struct devlink *devlink, 432 u64 resource_id); 433 434 #else 435 436 static inline struct devlink *devlink_alloc(const struct devlink_ops *ops, 437 size_t priv_size) 438 { 439 return kzalloc(sizeof(struct devlink) + priv_size, GFP_KERNEL); 440 } 441 442 static inline int devlink_register(struct devlink *devlink, struct device *dev) 443 { 444 return 0; 445 } 446 447 static inline void devlink_unregister(struct devlink *devlink) 448 { 449 } 450 451 static inline void devlink_free(struct devlink *devlink) 452 { 453 kfree(devlink); 454 } 455 456 static inline int devlink_port_register(struct devlink *devlink, 457 struct devlink_port *devlink_port, 458 unsigned int port_index) 459 { 460 return 0; 461 } 462 463 static inline void devlink_port_unregister(struct devlink_port *devlink_port) 464 { 465 } 466 467 static inline void devlink_port_type_eth_set(struct devlink_port *devlink_port, 468 struct net_device *netdev) 469 { 470 } 471 472 static inline void devlink_port_type_ib_set(struct devlink_port *devlink_port, 473 struct ib_device *ibdev) 474 { 475 } 476 477 static inline void devlink_port_type_clear(struct devlink_port *devlink_port) 478 { 479 } 480 481 static inline void devlink_port_attrs_set(struct devlink_port *devlink_port, 482 enum devlink_port_flavour flavour, 483 u32 port_number, bool split, 484 u32 split_subport_number) 485 { 486 } 487 488 static inline int 489 devlink_port_get_phys_port_name(struct devlink_port *devlink_port, 490 char *name, size_t len) 491 { 492 return -EOPNOTSUPP; 493 } 494 495 static inline int devlink_sb_register(struct devlink *devlink, 496 unsigned int sb_index, u32 size, 497 u16 ingress_pools_count, 498 u16 egress_pools_count, 499 u16 ingress_tc_count, 500 u16 egress_tc_count) 501 { 502 return 0; 503 } 504 505 static inline void devlink_sb_unregister(struct devlink *devlink, 506 unsigned int sb_index) 507 { 508 } 509 510 static inline int 511 devlink_dpipe_table_register(struct devlink *devlink, 512 const char *table_name, 513 struct devlink_dpipe_table_ops *table_ops, 514 void *priv, bool counter_control_extern) 515 { 516 return 0; 517 } 518 519 static inline void devlink_dpipe_table_unregister(struct devlink *devlink, 520 const char *table_name) 521 { 522 } 523 524 static inline int devlink_dpipe_headers_register(struct devlink *devlink, 525 struct devlink_dpipe_headers * 526 dpipe_headers) 527 { 528 return 0; 529 } 530 531 static inline void devlink_dpipe_headers_unregister(struct devlink *devlink) 532 { 533 } 534 535 static inline bool devlink_dpipe_table_counter_enabled(struct devlink *devlink, 536 const char *table_name) 537 { 538 return false; 539 } 540 541 static inline int 542 devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx) 543 { 544 return 0; 545 } 546 547 static inline int 548 devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx, 549 struct devlink_dpipe_entry *entry) 550 { 551 return 0; 552 } 553 554 static inline int 555 devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx) 556 { 557 return 0; 558 } 559 560 static inline void 561 devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry) 562 { 563 } 564 565 static inline int 566 devlink_dpipe_action_put(struct sk_buff *skb, 567 struct devlink_dpipe_action *action) 568 { 569 return 0; 570 } 571 572 static inline int 573 devlink_dpipe_match_put(struct sk_buff *skb, 574 struct devlink_dpipe_match *match) 575 { 576 return 0; 577 } 578 579 static inline int 580 devlink_resource_register(struct devlink *devlink, 581 const char *resource_name, 582 u64 resource_size, 583 u64 resource_id, 584 u64 parent_resource_id, 585 const struct devlink_resource_size_params *size_params) 586 { 587 return 0; 588 } 589 590 static inline void 591 devlink_resources_unregister(struct devlink *devlink, 592 struct devlink_resource *resource) 593 { 594 } 595 596 static inline int 597 devlink_resource_size_get(struct devlink *devlink, u64 resource_id, 598 u64 *p_resource_size) 599 { 600 return -EOPNOTSUPP; 601 } 602 603 static inline int 604 devlink_dpipe_table_resource_set(struct devlink *devlink, 605 const char *table_name, u64 resource_id, 606 u64 resource_units) 607 { 608 return -EOPNOTSUPP; 609 } 610 611 static inline void 612 devlink_resource_occ_get_register(struct devlink *devlink, 613 u64 resource_id, 614 devlink_resource_occ_get_t *occ_get, 615 void *occ_get_priv) 616 { 617 } 618 619 static inline void 620 devlink_resource_occ_get_unregister(struct devlink *devlink, 621 u64 resource_id) 622 { 623 } 624 625 #endif 626 627 #endif /* _NET_DEVLINK_H_ */ 628