Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 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; |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 27 | struct list_head sb_list; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 28 | struct list_head dpipe_table_list; |
| 29 | struct devlink_dpipe_headers *dpipe_headers; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 30 | const struct devlink_ops *ops; |
| 31 | struct device *dev; |
| 32 | possible_net_t _net; |
| 33 | char priv[0] __aligned(NETDEV_ALIGN); |
| 34 | }; |
| 35 | |
| 36 | struct devlink_port { |
| 37 | struct list_head list; |
| 38 | struct devlink *devlink; |
| 39 | unsigned index; |
| 40 | bool registered; |
| 41 | enum devlink_port_type type; |
| 42 | enum devlink_port_type desired_type; |
| 43 | void *type_dev; |
| 44 | bool split; |
| 45 | u32 split_group; |
| 46 | }; |
| 47 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 48 | struct devlink_sb_pool_info { |
| 49 | enum devlink_sb_pool_type pool_type; |
| 50 | u32 size; |
| 51 | enum devlink_sb_threshold_type threshold_type; |
| 52 | }; |
| 53 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 54 | /** |
| 55 | * struct devlink_dpipe_field - dpipe field object |
| 56 | * @name: field name |
| 57 | * @id: index inside the headers field array |
| 58 | * @bitwidth: bitwidth |
| 59 | * @mapping_type: mapping type |
| 60 | */ |
| 61 | struct devlink_dpipe_field { |
| 62 | const char *name; |
| 63 | unsigned int id; |
| 64 | unsigned int bitwidth; |
| 65 | enum devlink_dpipe_field_mapping_type mapping_type; |
| 66 | }; |
| 67 | |
| 68 | /** |
| 69 | * struct devlink_dpipe_header - dpipe header object |
| 70 | * @name: header name |
| 71 | * @id: index, global/local detrmined by global bit |
| 72 | * @fields: fields |
| 73 | * @fields_count: number of fields |
| 74 | * @global: indicates if header is shared like most protocol header |
| 75 | * or driver specific |
| 76 | */ |
| 77 | struct devlink_dpipe_header { |
| 78 | const char *name; |
| 79 | unsigned int id; |
| 80 | struct devlink_dpipe_field *fields; |
| 81 | unsigned int fields_count; |
| 82 | bool global; |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * struct devlink_dpipe_match - represents match operation |
| 87 | * @type: type of match |
| 88 | * @header_index: header index (packets can have several headers of same |
| 89 | * type like in case of tunnels) |
| 90 | * @header: header |
| 91 | * @fieled_id: field index |
| 92 | */ |
| 93 | struct devlink_dpipe_match { |
| 94 | enum devlink_dpipe_match_type type; |
| 95 | unsigned int header_index; |
| 96 | struct devlink_dpipe_header *header; |
| 97 | unsigned int field_id; |
| 98 | }; |
| 99 | |
| 100 | /** |
| 101 | * struct devlink_dpipe_action - represents action operation |
| 102 | * @type: type of action |
| 103 | * @header_index: header index (packets can have several headers of same |
| 104 | * type like in case of tunnels) |
| 105 | * @header: header |
| 106 | * @fieled_id: field index |
| 107 | */ |
| 108 | struct devlink_dpipe_action { |
| 109 | enum devlink_dpipe_action_type type; |
| 110 | unsigned int header_index; |
| 111 | struct devlink_dpipe_header *header; |
| 112 | unsigned int field_id; |
| 113 | }; |
| 114 | |
| 115 | /** |
| 116 | * struct devlink_dpipe_value - represents value of match/action |
| 117 | * @action: action |
| 118 | * @match: match |
| 119 | * @mapping_value: in case the field has some mapping this value |
| 120 | * specified the mapping value |
| 121 | * @mapping_valid: specify if mapping value is valid |
| 122 | * @value_size: value size |
| 123 | * @value: value |
| 124 | * @mask: bit mask |
| 125 | */ |
| 126 | struct devlink_dpipe_value { |
| 127 | union { |
| 128 | struct devlink_dpipe_action *action; |
| 129 | struct devlink_dpipe_match *match; |
| 130 | }; |
| 131 | unsigned int mapping_value; |
| 132 | bool mapping_valid; |
| 133 | unsigned int value_size; |
| 134 | void *value; |
| 135 | void *mask; |
| 136 | }; |
| 137 | |
| 138 | /** |
| 139 | * struct devlink_dpipe_entry - table entry object |
| 140 | * @index: index of the entry in the table |
| 141 | * @match_values: match values |
| 142 | * @matche_values_count: count of matches tuples |
| 143 | * @action_values: actions values |
| 144 | * @action_values_count: count of actions values |
| 145 | * @counter: value of counter |
| 146 | * @counter_valid: Specify if value is valid from hardware |
| 147 | */ |
| 148 | struct devlink_dpipe_entry { |
| 149 | u64 index; |
| 150 | struct devlink_dpipe_value *match_values; |
| 151 | unsigned int match_values_count; |
| 152 | struct devlink_dpipe_value *action_values; |
| 153 | unsigned int action_values_count; |
| 154 | u64 counter; |
| 155 | bool counter_valid; |
| 156 | }; |
| 157 | |
| 158 | /** |
| 159 | * struct devlink_dpipe_dump_ctx - context provided to driver in order |
| 160 | * to dump |
| 161 | * @info: info |
| 162 | * @cmd: devlink command |
| 163 | * @skb: skb |
| 164 | * @nest: top attribute |
| 165 | * @hdr: hdr |
| 166 | */ |
| 167 | struct devlink_dpipe_dump_ctx { |
| 168 | struct genl_info *info; |
| 169 | enum devlink_command cmd; |
| 170 | struct sk_buff *skb; |
| 171 | struct nlattr *nest; |
| 172 | void *hdr; |
| 173 | }; |
| 174 | |
| 175 | struct devlink_dpipe_table_ops; |
| 176 | |
| 177 | /** |
| 178 | * struct devlink_dpipe_table - table object |
| 179 | * @priv: private |
| 180 | * @name: table name |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 181 | * @counters_enabled: indicates if counters are active |
| 182 | * @counter_control_extern: indicates if counter control is in dpipe or |
| 183 | * external tool |
| 184 | * @table_ops: table operations |
| 185 | * @rcu: rcu |
| 186 | */ |
| 187 | struct devlink_dpipe_table { |
| 188 | void *priv; |
| 189 | struct list_head list; |
| 190 | const char *name; |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 191 | bool counters_enabled; |
| 192 | bool counter_control_extern; |
| 193 | struct devlink_dpipe_table_ops *table_ops; |
| 194 | struct rcu_head rcu; |
| 195 | }; |
| 196 | |
| 197 | /** |
| 198 | * struct devlink_dpipe_table_ops - dpipe_table ops |
| 199 | * @actions_dump - dumps all tables actions |
| 200 | * @matches_dump - dumps all tables matches |
| 201 | * @entries_dump - dumps all active entries in the table |
| 202 | * @counters_set_update - when changing the counter status hardware sync |
| 203 | * maybe needed to allocate/free counter related |
| 204 | * resources |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame^] | 205 | * @size_get - get size |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 206 | */ |
| 207 | struct devlink_dpipe_table_ops { |
| 208 | int (*actions_dump)(void *priv, struct sk_buff *skb); |
| 209 | int (*matches_dump)(void *priv, struct sk_buff *skb); |
| 210 | int (*entries_dump)(void *priv, bool counters_enabled, |
| 211 | struct devlink_dpipe_dump_ctx *dump_ctx); |
| 212 | int (*counters_set_update)(void *priv, bool enable); |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame^] | 213 | u64 (*size_get)(void *priv); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | /** |
| 217 | * struct devlink_dpipe_headers - dpipe headers |
| 218 | * @headers - header array can be shared (global bit) or driver specific |
| 219 | * @headers_count - count of headers |
| 220 | */ |
| 221 | struct devlink_dpipe_headers { |
| 222 | struct devlink_dpipe_header **headers; |
| 223 | unsigned int headers_count; |
| 224 | }; |
| 225 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 226 | struct devlink_ops { |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 227 | int (*port_type_set)(struct devlink_port *devlink_port, |
| 228 | enum devlink_port_type port_type); |
| 229 | int (*port_split)(struct devlink *devlink, unsigned int port_index, |
| 230 | unsigned int count); |
| 231 | int (*port_unsplit)(struct devlink *devlink, unsigned int port_index); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 232 | int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index, |
| 233 | u16 pool_index, |
| 234 | struct devlink_sb_pool_info *pool_info); |
| 235 | int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index, |
| 236 | u16 pool_index, u32 size, |
| 237 | enum devlink_sb_threshold_type threshold_type); |
| 238 | int (*sb_port_pool_get)(struct devlink_port *devlink_port, |
| 239 | unsigned int sb_index, u16 pool_index, |
| 240 | u32 *p_threshold); |
| 241 | int (*sb_port_pool_set)(struct devlink_port *devlink_port, |
| 242 | unsigned int sb_index, u16 pool_index, |
| 243 | u32 threshold); |
| 244 | int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port, |
| 245 | unsigned int sb_index, |
| 246 | u16 tc_index, |
| 247 | enum devlink_sb_pool_type pool_type, |
| 248 | u16 *p_pool_index, u32 *p_threshold); |
| 249 | int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port, |
| 250 | unsigned int sb_index, |
| 251 | u16 tc_index, |
| 252 | enum devlink_sb_pool_type pool_type, |
| 253 | u16 pool_index, u32 threshold); |
Jiri Pirko | df38daf | 2016-04-14 18:19:14 +0200 | [diff] [blame] | 254 | int (*sb_occ_snapshot)(struct devlink *devlink, |
| 255 | unsigned int sb_index); |
| 256 | int (*sb_occ_max_clear)(struct devlink *devlink, |
| 257 | unsigned int sb_index); |
| 258 | int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port, |
| 259 | unsigned int sb_index, u16 pool_index, |
| 260 | u32 *p_cur, u32 *p_max); |
| 261 | int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port, |
| 262 | unsigned int sb_index, |
| 263 | u16 tc_index, |
| 264 | enum devlink_sb_pool_type pool_type, |
| 265 | u32 *p_cur, u32 *p_max); |
Or Gerlitz | 08f4b59 | 2016-07-01 14:51:01 +0300 | [diff] [blame] | 266 | |
| 267 | int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode); |
| 268 | int (*eswitch_mode_set)(struct devlink *devlink, u16 mode); |
Roi Dayan | 59bfde0 | 2016-11-22 23:09:57 +0200 | [diff] [blame] | 269 | int (*eswitch_inline_mode_get)(struct devlink *devlink, u8 *p_inline_mode); |
| 270 | int (*eswitch_inline_mode_set)(struct devlink *devlink, u8 inline_mode); |
Roi Dayan | f43e9b0 | 2016-09-25 13:52:44 +0300 | [diff] [blame] | 271 | int (*eswitch_encap_mode_get)(struct devlink *devlink, u8 *p_encap_mode); |
| 272 | int (*eswitch_encap_mode_set)(struct devlink *devlink, u8 encap_mode); |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | static inline void *devlink_priv(struct devlink *devlink) |
| 276 | { |
| 277 | BUG_ON(!devlink); |
| 278 | return &devlink->priv; |
| 279 | } |
| 280 | |
| 281 | static inline struct devlink *priv_to_devlink(void *priv) |
| 282 | { |
| 283 | BUG_ON(!priv); |
| 284 | return container_of(priv, struct devlink, priv); |
| 285 | } |
| 286 | |
| 287 | struct ib_device; |
| 288 | |
| 289 | #if IS_ENABLED(CONFIG_NET_DEVLINK) |
| 290 | |
| 291 | struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size); |
| 292 | int devlink_register(struct devlink *devlink, struct device *dev); |
| 293 | void devlink_unregister(struct devlink *devlink); |
| 294 | void devlink_free(struct devlink *devlink); |
| 295 | int devlink_port_register(struct devlink *devlink, |
| 296 | struct devlink_port *devlink_port, |
| 297 | unsigned int port_index); |
| 298 | void devlink_port_unregister(struct devlink_port *devlink_port); |
| 299 | void devlink_port_type_eth_set(struct devlink_port *devlink_port, |
| 300 | struct net_device *netdev); |
| 301 | void devlink_port_type_ib_set(struct devlink_port *devlink_port, |
| 302 | struct ib_device *ibdev); |
| 303 | void devlink_port_type_clear(struct devlink_port *devlink_port); |
| 304 | void devlink_port_split_set(struct devlink_port *devlink_port, |
| 305 | u32 split_group); |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 306 | int devlink_sb_register(struct devlink *devlink, unsigned int sb_index, |
| 307 | u32 size, u16 ingress_pools_count, |
| 308 | u16 egress_pools_count, u16 ingress_tc_count, |
| 309 | u16 egress_tc_count); |
| 310 | void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 311 | int devlink_dpipe_table_register(struct devlink *devlink, |
| 312 | const char *table_name, |
| 313 | struct devlink_dpipe_table_ops *table_ops, |
Arkadi Sharshevsky | ffd3cdc | 2017-08-24 08:40:02 +0200 | [diff] [blame^] | 314 | void *priv, bool counter_control_extern); |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 315 | void devlink_dpipe_table_unregister(struct devlink *devlink, |
| 316 | const char *table_name); |
| 317 | int devlink_dpipe_headers_register(struct devlink *devlink, |
| 318 | struct devlink_dpipe_headers *dpipe_headers); |
| 319 | void devlink_dpipe_headers_unregister(struct devlink *devlink); |
| 320 | bool devlink_dpipe_table_counter_enabled(struct devlink *devlink, |
| 321 | const char *table_name); |
| 322 | int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx); |
| 323 | int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx, |
| 324 | struct devlink_dpipe_entry *entry); |
| 325 | int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx); |
| 326 | int devlink_dpipe_action_put(struct sk_buff *skb, |
| 327 | struct devlink_dpipe_action *action); |
| 328 | int devlink_dpipe_match_put(struct sk_buff *skb, |
| 329 | struct devlink_dpipe_match *match); |
Arkadi Sharshevsky | 1177009 | 2017-08-24 08:39:59 +0200 | [diff] [blame] | 330 | extern struct devlink_dpipe_header devlink_dpipe_header_ethernet; |
Arkadi Sharshevsky | 3fb886e | 2017-08-24 08:40:00 +0200 | [diff] [blame] | 331 | extern struct devlink_dpipe_header devlink_dpipe_header_ipv4; |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 332 | |
| 333 | #else |
| 334 | |
| 335 | static inline struct devlink *devlink_alloc(const struct devlink_ops *ops, |
| 336 | size_t priv_size) |
| 337 | { |
| 338 | return kzalloc(sizeof(struct devlink) + priv_size, GFP_KERNEL); |
| 339 | } |
| 340 | |
| 341 | static inline int devlink_register(struct devlink *devlink, struct device *dev) |
| 342 | { |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static inline void devlink_unregister(struct devlink *devlink) |
| 347 | { |
| 348 | } |
| 349 | |
| 350 | static inline void devlink_free(struct devlink *devlink) |
| 351 | { |
| 352 | kfree(devlink); |
| 353 | } |
| 354 | |
| 355 | static inline int devlink_port_register(struct devlink *devlink, |
| 356 | struct devlink_port *devlink_port, |
| 357 | unsigned int port_index) |
| 358 | { |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | static inline void devlink_port_unregister(struct devlink_port *devlink_port) |
| 363 | { |
| 364 | } |
| 365 | |
| 366 | static inline void devlink_port_type_eth_set(struct devlink_port *devlink_port, |
| 367 | struct net_device *netdev) |
| 368 | { |
| 369 | } |
| 370 | |
| 371 | static inline void devlink_port_type_ib_set(struct devlink_port *devlink_port, |
| 372 | struct ib_device *ibdev) |
| 373 | { |
| 374 | } |
| 375 | |
| 376 | static inline void devlink_port_type_clear(struct devlink_port *devlink_port) |
| 377 | { |
| 378 | } |
| 379 | |
| 380 | static inline void devlink_port_split_set(struct devlink_port *devlink_port, |
| 381 | u32 split_group) |
| 382 | { |
| 383 | } |
| 384 | |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 385 | static inline int devlink_sb_register(struct devlink *devlink, |
| 386 | unsigned int sb_index, u32 size, |
| 387 | u16 ingress_pools_count, |
Jiri Pirko | de33efd | 2016-04-15 09:17:08 +0200 | [diff] [blame] | 388 | u16 egress_pools_count, |
| 389 | u16 ingress_tc_count, |
| 390 | u16 egress_tc_count) |
Jiri Pirko | bf79747 | 2016-04-14 18:19:13 +0200 | [diff] [blame] | 391 | { |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static inline void devlink_sb_unregister(struct devlink *devlink, |
| 396 | unsigned int sb_index) |
| 397 | { |
| 398 | } |
| 399 | |
Arkadi Sharshevsky | 1555d20 | 2017-03-28 17:24:10 +0200 | [diff] [blame] | 400 | static inline int |
| 401 | devlink_dpipe_table_register(struct devlink *devlink, |
| 402 | const char *table_name, |
| 403 | struct devlink_dpipe_table_ops *table_ops, |
| 404 | void *priv, u64 size, |
| 405 | bool counter_control_extern) |
| 406 | { |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static inline void devlink_dpipe_table_unregister(struct devlink *devlink, |
| 411 | const char *table_name) |
| 412 | { |
| 413 | } |
| 414 | |
| 415 | static inline int devlink_dpipe_headers_register(struct devlink *devlink, |
| 416 | struct devlink_dpipe_headers * |
| 417 | dpipe_headers) |
| 418 | { |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | static inline void devlink_dpipe_headers_unregister(struct devlink *devlink) |
| 423 | { |
| 424 | } |
| 425 | |
| 426 | static inline bool devlink_dpipe_table_counter_enabled(struct devlink *devlink, |
| 427 | const char *table_name) |
| 428 | { |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | static inline int |
| 433 | devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 434 | { |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static inline int |
| 439 | devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx, |
| 440 | struct devlink_dpipe_entry *entry) |
| 441 | { |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | static inline int |
| 446 | devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx) |
| 447 | { |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | static inline int |
| 452 | devlink_dpipe_action_put(struct sk_buff *skb, |
| 453 | struct devlink_dpipe_action *action) |
| 454 | { |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static inline int |
| 459 | devlink_dpipe_match_put(struct sk_buff *skb, |
| 460 | struct devlink_dpipe_match *match) |
| 461 | { |
| 462 | return 0; |
| 463 | } |
| 464 | |
Jiri Pirko | bfcd3a4 | 2016-02-26 17:32:23 +0100 | [diff] [blame] | 465 | #endif |
| 466 | |
| 467 | #endif /* _NET_DEVLINK_H_ */ |