2 * include/net/devlink.h - Network physical device Netlink interface
3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
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.
11 #ifndef _NET_DEVLINK_H_
12 #define _NET_DEVLINK_H_
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>
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 list_head param_list;
31 struct list_head region_list;
33 struct devlink_dpipe_headers *dpipe_headers;
34 const struct devlink_ops *ops;
38 char priv[0] __aligned(NETDEV_ALIGN);
41 struct devlink_port_attrs {
43 enum devlink_port_flavour flavour;
44 u32 port_number; /* same value as "split group" */
46 u32 split_subport_number;
50 struct list_head list;
51 struct devlink *devlink;
54 enum devlink_port_type type;
55 enum devlink_port_type desired_type;
57 struct devlink_port_attrs attrs;
60 struct devlink_sb_pool_info {
61 enum devlink_sb_pool_type pool_type;
63 enum devlink_sb_threshold_type threshold_type;
67 * struct devlink_dpipe_field - dpipe field object
69 * @id: index inside the headers field array
71 * @mapping_type: mapping type
73 struct devlink_dpipe_field {
76 unsigned int bitwidth;
77 enum devlink_dpipe_field_mapping_type mapping_type;
81 * struct devlink_dpipe_header - dpipe header object
83 * @id: index, global/local detrmined by global bit
85 * @fields_count: number of fields
86 * @global: indicates if header is shared like most protocol header
89 struct devlink_dpipe_header {
92 struct devlink_dpipe_field *fields;
93 unsigned int fields_count;
98 * struct devlink_dpipe_match - represents match operation
99 * @type: type of match
100 * @header_index: header index (packets can have several headers of same
101 * type like in case of tunnels)
103 * @fieled_id: field index
105 struct devlink_dpipe_match {
106 enum devlink_dpipe_match_type type;
107 unsigned int header_index;
108 struct devlink_dpipe_header *header;
109 unsigned int field_id;
113 * struct devlink_dpipe_action - represents action operation
114 * @type: type of action
115 * @header_index: header index (packets can have several headers of same
116 * type like in case of tunnels)
118 * @fieled_id: field index
120 struct devlink_dpipe_action {
121 enum devlink_dpipe_action_type type;
122 unsigned int header_index;
123 struct devlink_dpipe_header *header;
124 unsigned int field_id;
128 * struct devlink_dpipe_value - represents value of match/action
131 * @mapping_value: in case the field has some mapping this value
132 * specified the mapping value
133 * @mapping_valid: specify if mapping value is valid
134 * @value_size: value size
138 struct devlink_dpipe_value {
140 struct devlink_dpipe_action *action;
141 struct devlink_dpipe_match *match;
143 unsigned int mapping_value;
145 unsigned int value_size;
151 * struct devlink_dpipe_entry - table entry object
152 * @index: index of the entry in the table
153 * @match_values: match values
154 * @matche_values_count: count of matches tuples
155 * @action_values: actions values
156 * @action_values_count: count of actions values
157 * @counter: value of counter
158 * @counter_valid: Specify if value is valid from hardware
160 struct devlink_dpipe_entry {
162 struct devlink_dpipe_value *match_values;
163 unsigned int match_values_count;
164 struct devlink_dpipe_value *action_values;
165 unsigned int action_values_count;
171 * struct devlink_dpipe_dump_ctx - context provided to driver in order
174 * @cmd: devlink command
176 * @nest: top attribute
179 struct devlink_dpipe_dump_ctx {
180 struct genl_info *info;
181 enum devlink_command cmd;
187 struct devlink_dpipe_table_ops;
190 * struct devlink_dpipe_table - table object
193 * @counters_enabled: indicates if counters are active
194 * @counter_control_extern: indicates if counter control is in dpipe or
196 * @resource_valid: Indicate that the resource id is valid
197 * @resource_id: relative resource this table is related to
198 * @resource_units: number of resource's unit consumed per table's entry
199 * @table_ops: table operations
202 struct devlink_dpipe_table {
204 struct list_head list;
206 bool counters_enabled;
207 bool counter_control_extern;
211 struct devlink_dpipe_table_ops *table_ops;
216 * struct devlink_dpipe_table_ops - dpipe_table ops
217 * @actions_dump - dumps all tables actions
218 * @matches_dump - dumps all tables matches
219 * @entries_dump - dumps all active entries in the table
220 * @counters_set_update - when changing the counter status hardware sync
221 * maybe needed to allocate/free counter related
223 * @size_get - get size
225 struct devlink_dpipe_table_ops {
226 int (*actions_dump)(void *priv, struct sk_buff *skb);
227 int (*matches_dump)(void *priv, struct sk_buff *skb);
228 int (*entries_dump)(void *priv, bool counters_enabled,
229 struct devlink_dpipe_dump_ctx *dump_ctx);
230 int (*counters_set_update)(void *priv, bool enable);
231 u64 (*size_get)(void *priv);
235 * struct devlink_dpipe_headers - dpipe headers
236 * @headers - header array can be shared (global bit) or driver specific
237 * @headers_count - count of headers
239 struct devlink_dpipe_headers {
240 struct devlink_dpipe_header **headers;
241 unsigned int headers_count;
245 * struct devlink_resource_size_params - resource's size parameters
246 * @size_min: minimum size which can be set
247 * @size_max: maximum size which can be set
248 * @size_granularity: size granularity
249 * @size_unit: resource's basic unit
251 struct devlink_resource_size_params {
254 u64 size_granularity;
255 enum devlink_resource_unit unit;
259 devlink_resource_size_params_init(struct devlink_resource_size_params *size_params,
260 u64 size_min, u64 size_max,
261 u64 size_granularity,
262 enum devlink_resource_unit unit)
264 size_params->size_min = size_min;
265 size_params->size_max = size_max;
266 size_params->size_granularity = size_granularity;
267 size_params->unit = unit;
270 typedef u64 devlink_resource_occ_get_t(void *priv);
273 * struct devlink_resource - devlink resource
274 * @name: name of the resource
275 * @id: id, per devlink instance
276 * @size: size of the resource
277 * @size_new: updated size of the resource, reload is needed
278 * @size_valid: valid in case the total size of the resource is valid
279 * including its children
280 * @parent: parent resource
281 * @size_params: size parameters
283 * @resource_list: list of child resources
285 struct devlink_resource {
291 struct devlink_resource *parent;
292 struct devlink_resource_size_params size_params;
293 struct list_head list;
294 struct list_head resource_list;
295 devlink_resource_occ_get_t *occ_get;
299 #define DEVLINK_RESOURCE_ID_PARENT_TOP 0
301 #define __DEVLINK_PARAM_MAX_STRING_VALUE 32
302 enum devlink_param_type {
303 DEVLINK_PARAM_TYPE_U8,
304 DEVLINK_PARAM_TYPE_U16,
305 DEVLINK_PARAM_TYPE_U32,
306 DEVLINK_PARAM_TYPE_STRING,
307 DEVLINK_PARAM_TYPE_BOOL,
310 union devlink_param_value {
314 char vstr[__DEVLINK_PARAM_MAX_STRING_VALUE];
318 struct devlink_param_gset_ctx {
319 union devlink_param_value val;
320 enum devlink_param_cmode cmode;
324 * struct devlink_param - devlink configuration parameter data
325 * @name: name of the parameter
326 * @generic: indicates if the parameter is generic or driver specific
327 * @type: parameter type
328 * @supported_cmodes: bitmap of supported configuration modes
329 * @get: get parameter value, used for runtime and permanent
330 * configuration modes
331 * @set: set parameter value, used for runtime and permanent
332 * configuration modes
333 * @validate: validate input value is applicable (within value range, etc.)
335 * This struct should be used by the driver to fill the data for
336 * a parameter it registers.
338 struct devlink_param {
342 enum devlink_param_type type;
343 unsigned long supported_cmodes;
344 int (*get)(struct devlink *devlink, u32 id,
345 struct devlink_param_gset_ctx *ctx);
346 int (*set)(struct devlink *devlink, u32 id,
347 struct devlink_param_gset_ctx *ctx);
348 int (*validate)(struct devlink *devlink, u32 id,
349 union devlink_param_value val,
350 struct netlink_ext_ack *extack);
353 struct devlink_param_item {
354 struct list_head list;
355 const struct devlink_param *param;
356 union devlink_param_value driverinit_value;
357 bool driverinit_value_valid;
360 enum devlink_param_generic_id {
361 DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
362 DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
363 DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
364 DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
365 DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
366 DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
367 DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
369 /* add new param generic ids above here*/
370 __DEVLINK_PARAM_GENERIC_ID_MAX,
371 DEVLINK_PARAM_GENERIC_ID_MAX = __DEVLINK_PARAM_GENERIC_ID_MAX - 1,
374 #define DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME "internal_error_reset"
375 #define DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE DEVLINK_PARAM_TYPE_BOOL
377 #define DEVLINK_PARAM_GENERIC_MAX_MACS_NAME "max_macs"
378 #define DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE DEVLINK_PARAM_TYPE_U32
380 #define DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME "enable_sriov"
381 #define DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE DEVLINK_PARAM_TYPE_BOOL
383 #define DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME "region_snapshot_enable"
384 #define DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE DEVLINK_PARAM_TYPE_BOOL
386 #define DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME "ignore_ari"
387 #define DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE DEVLINK_PARAM_TYPE_BOOL
389 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME "msix_vec_per_pf_max"
390 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE DEVLINK_PARAM_TYPE_U32
392 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME "msix_vec_per_pf_min"
393 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE DEVLINK_PARAM_TYPE_U32
395 #define DEVLINK_PARAM_GENERIC(_id, _cmodes, _get, _set, _validate) \
397 .id = DEVLINK_PARAM_GENERIC_ID_##_id, \
398 .name = DEVLINK_PARAM_GENERIC_##_id##_NAME, \
399 .type = DEVLINK_PARAM_GENERIC_##_id##_TYPE, \
401 .supported_cmodes = _cmodes, \
404 .validate = _validate, \
407 #define DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, _get, _set, _validate) \
412 .supported_cmodes = _cmodes, \
415 .validate = _validate, \
418 struct devlink_region;
420 typedef void devlink_snapshot_data_dest_t(const void *data);
423 int (*reload)(struct devlink *devlink, struct netlink_ext_ack *extack);
424 int (*port_type_set)(struct devlink_port *devlink_port,
425 enum devlink_port_type port_type);
426 int (*port_split)(struct devlink *devlink, unsigned int port_index,
427 unsigned int count, struct netlink_ext_ack *extack);
428 int (*port_unsplit)(struct devlink *devlink, unsigned int port_index,
429 struct netlink_ext_ack *extack);
430 int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index,
432 struct devlink_sb_pool_info *pool_info);
433 int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index,
434 u16 pool_index, u32 size,
435 enum devlink_sb_threshold_type threshold_type);
436 int (*sb_port_pool_get)(struct devlink_port *devlink_port,
437 unsigned int sb_index, u16 pool_index,
439 int (*sb_port_pool_set)(struct devlink_port *devlink_port,
440 unsigned int sb_index, u16 pool_index,
442 int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port,
443 unsigned int sb_index,
445 enum devlink_sb_pool_type pool_type,
446 u16 *p_pool_index, u32 *p_threshold);
447 int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port,
448 unsigned int sb_index,
450 enum devlink_sb_pool_type pool_type,
451 u16 pool_index, u32 threshold);
452 int (*sb_occ_snapshot)(struct devlink *devlink,
453 unsigned int sb_index);
454 int (*sb_occ_max_clear)(struct devlink *devlink,
455 unsigned int sb_index);
456 int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port,
457 unsigned int sb_index, u16 pool_index,
458 u32 *p_cur, u32 *p_max);
459 int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port,
460 unsigned int sb_index,
462 enum devlink_sb_pool_type pool_type,
463 u32 *p_cur, u32 *p_max);
465 int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode);
466 int (*eswitch_mode_set)(struct devlink *devlink, u16 mode,
467 struct netlink_ext_ack *extack);
468 int (*eswitch_inline_mode_get)(struct devlink *devlink, u8 *p_inline_mode);
469 int (*eswitch_inline_mode_set)(struct devlink *devlink, u8 inline_mode,
470 struct netlink_ext_ack *extack);
471 int (*eswitch_encap_mode_get)(struct devlink *devlink, u8 *p_encap_mode);
472 int (*eswitch_encap_mode_set)(struct devlink *devlink, u8 encap_mode,
473 struct netlink_ext_ack *extack);
476 static inline void *devlink_priv(struct devlink *devlink)
479 return &devlink->priv;
482 static inline struct devlink *priv_to_devlink(void *priv)
485 return container_of(priv, struct devlink, priv);
490 #if IS_ENABLED(CONFIG_NET_DEVLINK)
492 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
493 int devlink_register(struct devlink *devlink, struct device *dev);
494 void devlink_unregister(struct devlink *devlink);
495 void devlink_free(struct devlink *devlink);
496 int devlink_port_register(struct devlink *devlink,
497 struct devlink_port *devlink_port,
498 unsigned int port_index);
499 void devlink_port_unregister(struct devlink_port *devlink_port);
500 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
501 struct net_device *netdev);
502 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
503 struct ib_device *ibdev);
504 void devlink_port_type_clear(struct devlink_port *devlink_port);
505 void devlink_port_attrs_set(struct devlink_port *devlink_port,
506 enum devlink_port_flavour flavour,
507 u32 port_number, bool split,
508 u32 split_subport_number);
509 int devlink_port_get_phys_port_name(struct devlink_port *devlink_port,
510 char *name, size_t len);
511 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
512 u32 size, u16 ingress_pools_count,
513 u16 egress_pools_count, u16 ingress_tc_count,
514 u16 egress_tc_count);
515 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index);
516 int devlink_dpipe_table_register(struct devlink *devlink,
517 const char *table_name,
518 struct devlink_dpipe_table_ops *table_ops,
519 void *priv, bool counter_control_extern);
520 void devlink_dpipe_table_unregister(struct devlink *devlink,
521 const char *table_name);
522 int devlink_dpipe_headers_register(struct devlink *devlink,
523 struct devlink_dpipe_headers *dpipe_headers);
524 void devlink_dpipe_headers_unregister(struct devlink *devlink);
525 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
526 const char *table_name);
527 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx);
528 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
529 struct devlink_dpipe_entry *entry);
530 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx);
531 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry);
532 int devlink_dpipe_action_put(struct sk_buff *skb,
533 struct devlink_dpipe_action *action);
534 int devlink_dpipe_match_put(struct sk_buff *skb,
535 struct devlink_dpipe_match *match);
536 extern struct devlink_dpipe_header devlink_dpipe_header_ethernet;
537 extern struct devlink_dpipe_header devlink_dpipe_header_ipv4;
538 extern struct devlink_dpipe_header devlink_dpipe_header_ipv6;
540 int devlink_resource_register(struct devlink *devlink,
541 const char *resource_name,
544 u64 parent_resource_id,
545 const struct devlink_resource_size_params *size_params);
546 void devlink_resources_unregister(struct devlink *devlink,
547 struct devlink_resource *resource);
548 int devlink_resource_size_get(struct devlink *devlink,
550 u64 *p_resource_size);
551 int devlink_dpipe_table_resource_set(struct devlink *devlink,
552 const char *table_name, u64 resource_id,
554 void devlink_resource_occ_get_register(struct devlink *devlink,
556 devlink_resource_occ_get_t *occ_get,
558 void devlink_resource_occ_get_unregister(struct devlink *devlink,
560 int devlink_params_register(struct devlink *devlink,
561 const struct devlink_param *params,
562 size_t params_count);
563 void devlink_params_unregister(struct devlink *devlink,
564 const struct devlink_param *params,
565 size_t params_count);
566 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
567 union devlink_param_value *init_val);
568 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
569 union devlink_param_value init_val);
570 void devlink_param_value_changed(struct devlink *devlink, u32 param_id);
571 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
573 struct devlink_region *devlink_region_create(struct devlink *devlink,
574 const char *region_name,
575 u32 region_max_snapshots,
577 void devlink_region_destroy(struct devlink_region *region);
578 u32 devlink_region_shapshot_id_get(struct devlink *devlink);
579 int devlink_region_snapshot_create(struct devlink_region *region, u64 data_len,
580 u8 *data, u32 snapshot_id,
581 devlink_snapshot_data_dest_t *data_destructor);
585 static inline struct devlink *devlink_alloc(const struct devlink_ops *ops,
588 return kzalloc(sizeof(struct devlink) + priv_size, GFP_KERNEL);
591 static inline int devlink_register(struct devlink *devlink, struct device *dev)
596 static inline void devlink_unregister(struct devlink *devlink)
600 static inline void devlink_free(struct devlink *devlink)
605 static inline int devlink_port_register(struct devlink *devlink,
606 struct devlink_port *devlink_port,
607 unsigned int port_index)
612 static inline void devlink_port_unregister(struct devlink_port *devlink_port)
616 static inline void devlink_port_type_eth_set(struct devlink_port *devlink_port,
617 struct net_device *netdev)
621 static inline void devlink_port_type_ib_set(struct devlink_port *devlink_port,
622 struct ib_device *ibdev)
626 static inline void devlink_port_type_clear(struct devlink_port *devlink_port)
630 static inline void devlink_port_attrs_set(struct devlink_port *devlink_port,
631 enum devlink_port_flavour flavour,
632 u32 port_number, bool split,
633 u32 split_subport_number)
638 devlink_port_get_phys_port_name(struct devlink_port *devlink_port,
639 char *name, size_t len)
644 static inline int devlink_sb_register(struct devlink *devlink,
645 unsigned int sb_index, u32 size,
646 u16 ingress_pools_count,
647 u16 egress_pools_count,
648 u16 ingress_tc_count,
654 static inline void devlink_sb_unregister(struct devlink *devlink,
655 unsigned int sb_index)
660 devlink_dpipe_table_register(struct devlink *devlink,
661 const char *table_name,
662 struct devlink_dpipe_table_ops *table_ops,
663 void *priv, bool counter_control_extern)
668 static inline void devlink_dpipe_table_unregister(struct devlink *devlink,
669 const char *table_name)
673 static inline int devlink_dpipe_headers_register(struct devlink *devlink,
674 struct devlink_dpipe_headers *
680 static inline void devlink_dpipe_headers_unregister(struct devlink *devlink)
684 static inline bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
685 const char *table_name)
691 devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
697 devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
698 struct devlink_dpipe_entry *entry)
704 devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
710 devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
715 devlink_dpipe_action_put(struct sk_buff *skb,
716 struct devlink_dpipe_action *action)
722 devlink_dpipe_match_put(struct sk_buff *skb,
723 struct devlink_dpipe_match *match)
729 devlink_resource_register(struct devlink *devlink,
730 const char *resource_name,
733 u64 parent_resource_id,
734 const struct devlink_resource_size_params *size_params)
740 devlink_resources_unregister(struct devlink *devlink,
741 struct devlink_resource *resource)
746 devlink_resource_size_get(struct devlink *devlink, u64 resource_id,
747 u64 *p_resource_size)
753 devlink_dpipe_table_resource_set(struct devlink *devlink,
754 const char *table_name, u64 resource_id,
761 devlink_resource_occ_get_register(struct devlink *devlink,
763 devlink_resource_occ_get_t *occ_get,
769 devlink_resource_occ_get_unregister(struct devlink *devlink,
775 devlink_params_register(struct devlink *devlink,
776 const struct devlink_param *params,
783 devlink_params_unregister(struct devlink *devlink,
784 const struct devlink_param *params,
791 devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
792 union devlink_param_value *init_val)
798 devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
799 union devlink_param_value init_val)
805 devlink_param_value_changed(struct devlink *devlink, u32 param_id)
810 devlink_param_value_str_fill(union devlink_param_value *dst_val,
815 static inline struct devlink_region *
816 devlink_region_create(struct devlink *devlink,
817 const char *region_name,
818 u32 region_max_snapshots,
825 devlink_region_destroy(struct devlink_region *region)
830 devlink_region_shapshot_id_get(struct devlink *devlink)
836 devlink_region_snapshot_create(struct devlink_region *region, u64 data_len,
837 u8 *data, u32 snapshot_id,
838 devlink_snapshot_data_dest_t *data_destructor)
845 #endif /* _NET_DEVLINK_H_ */