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