xref: /linux/include/net/devlink.h (revision 93a3545d812ae7cfe4426374e00a7d8f64ac02e0)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * include/net/devlink.h - Network physical device Netlink interface
4  * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
5  * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
6  */
7 #ifndef _NET_DEVLINK_H_
8 #define _NET_DEVLINK_H_
9 
10 #include <linux/device.h>
11 #include <linux/slab.h>
12 #include <linux/gfp.h>
13 #include <linux/list.h>
14 #include <linux/netdevice.h>
15 #include <linux/spinlock.h>
16 #include <linux/workqueue.h>
17 #include <linux/refcount.h>
18 #include <net/net_namespace.h>
19 #include <net/flow_offload.h>
20 #include <uapi/linux/devlink.h>
21 #include <linux/xarray.h>
22 
23 struct devlink_ops;
24 
25 struct devlink {
26 	struct list_head list;
27 	struct list_head port_list;
28 	struct list_head sb_list;
29 	struct list_head dpipe_table_list;
30 	struct list_head resource_list;
31 	struct list_head param_list;
32 	struct list_head region_list;
33 	struct list_head reporter_list;
34 	struct mutex reporters_lock; /* protects reporter_list */
35 	struct devlink_dpipe_headers *dpipe_headers;
36 	struct list_head trap_list;
37 	struct list_head trap_group_list;
38 	struct list_head trap_policer_list;
39 	const struct devlink_ops *ops;
40 	struct xarray snapshot_ids;
41 	struct device *dev;
42 	possible_net_t _net;
43 	struct mutex lock;
44 	u8 reload_failed:1,
45 	   reload_enabled:1,
46 	   registered:1;
47 	char priv[0] __aligned(NETDEV_ALIGN);
48 };
49 
50 struct devlink_port_phys_attrs {
51 	u32 port_number; /* Same value as "split group".
52 			  * A physical port which is visible to the user
53 			  * for a given port flavour.
54 			  */
55 	u32 split_subport_number; /* If the port is split, this is the number of subport. */
56 };
57 
58 struct devlink_port_pci_pf_attrs {
59 	u16 pf;	/* Associated PCI PF for this port. */
60 };
61 
62 struct devlink_port_pci_vf_attrs {
63 	u16 pf;	/* Associated PCI PF for this port. */
64 	u16 vf;	/* Associated PCI VF for of the PCI PF for this port. */
65 };
66 
67 /**
68  * struct devlink_port_attrs - devlink port object
69  * @flavour: flavour of the port
70  * @split: indicates if this is split port
71  * @splittable: indicates if the port can be split.
72  * @lanes: maximum number of lanes the port supports. 0 value is not passed to netlink.
73  * @switch_id: if the port is part of switch, this is buffer with ID, otherwise this is NULL
74  */
75 struct devlink_port_attrs {
76 	u8 split:1,
77 	   splittable:1;
78 	u32 lanes;
79 	enum devlink_port_flavour flavour;
80 	struct netdev_phys_item_id switch_id;
81 	union {
82 		struct devlink_port_phys_attrs phys;
83 		struct devlink_port_pci_pf_attrs pci_pf;
84 		struct devlink_port_pci_vf_attrs pci_vf;
85 	};
86 };
87 
88 struct devlink_port {
89 	struct list_head list;
90 	struct list_head param_list;
91 	struct devlink *devlink;
92 	unsigned int index;
93 	bool registered;
94 	spinlock_t type_lock; /* Protects type and type_dev
95 			       * pointer consistency.
96 			       */
97 	enum devlink_port_type type;
98 	enum devlink_port_type desired_type;
99 	void *type_dev;
100 	struct devlink_port_attrs attrs;
101 	u8 attrs_set:1,
102 	   switch_port:1;
103 	struct delayed_work type_warn_dw;
104 	struct list_head reporter_list;
105 	struct mutex reporters_lock; /* Protects reporter_list */
106 };
107 
108 struct devlink_sb_pool_info {
109 	enum devlink_sb_pool_type pool_type;
110 	u32 size;
111 	enum devlink_sb_threshold_type threshold_type;
112 	u32 cell_size;
113 };
114 
115 /**
116  * struct devlink_dpipe_field - dpipe field object
117  * @name: field name
118  * @id: index inside the headers field array
119  * @bitwidth: bitwidth
120  * @mapping_type: mapping type
121  */
122 struct devlink_dpipe_field {
123 	const char *name;
124 	unsigned int id;
125 	unsigned int bitwidth;
126 	enum devlink_dpipe_field_mapping_type mapping_type;
127 };
128 
129 /**
130  * struct devlink_dpipe_header - dpipe header object
131  * @name: header name
132  * @id: index, global/local detrmined by global bit
133  * @fields: fields
134  * @fields_count: number of fields
135  * @global: indicates if header is shared like most protocol header
136  *	    or driver specific
137  */
138 struct devlink_dpipe_header {
139 	const char *name;
140 	unsigned int id;
141 	struct devlink_dpipe_field *fields;
142 	unsigned int fields_count;
143 	bool global;
144 };
145 
146 /**
147  * struct devlink_dpipe_match - represents match operation
148  * @type: type of match
149  * @header_index: header index (packets can have several headers of same
150  *		  type like in case of tunnels)
151  * @header: header
152  * @fieled_id: field index
153  */
154 struct devlink_dpipe_match {
155 	enum devlink_dpipe_match_type type;
156 	unsigned int header_index;
157 	struct devlink_dpipe_header *header;
158 	unsigned int field_id;
159 };
160 
161 /**
162  * struct devlink_dpipe_action - represents action operation
163  * @type: type of action
164  * @header_index: header index (packets can have several headers of same
165  *		  type like in case of tunnels)
166  * @header: header
167  * @fieled_id: field index
168  */
169 struct devlink_dpipe_action {
170 	enum devlink_dpipe_action_type type;
171 	unsigned int header_index;
172 	struct devlink_dpipe_header *header;
173 	unsigned int field_id;
174 };
175 
176 /**
177  * struct devlink_dpipe_value - represents value of match/action
178  * @action: action
179  * @match: match
180  * @mapping_value: in case the field has some mapping this value
181  *                 specified the mapping value
182  * @mapping_valid: specify if mapping value is valid
183  * @value_size: value size
184  * @value: value
185  * @mask: bit mask
186  */
187 struct devlink_dpipe_value {
188 	union {
189 		struct devlink_dpipe_action *action;
190 		struct devlink_dpipe_match *match;
191 	};
192 	unsigned int mapping_value;
193 	bool mapping_valid;
194 	unsigned int value_size;
195 	void *value;
196 	void *mask;
197 };
198 
199 /**
200  * struct devlink_dpipe_entry - table entry object
201  * @index: index of the entry in the table
202  * @match_values: match values
203  * @matche_values_count: count of matches tuples
204  * @action_values: actions values
205  * @action_values_count: count of actions values
206  * @counter: value of counter
207  * @counter_valid: Specify if value is valid from hardware
208  */
209 struct devlink_dpipe_entry {
210 	u64 index;
211 	struct devlink_dpipe_value *match_values;
212 	unsigned int match_values_count;
213 	struct devlink_dpipe_value *action_values;
214 	unsigned int action_values_count;
215 	u64 counter;
216 	bool counter_valid;
217 };
218 
219 /**
220  * struct devlink_dpipe_dump_ctx - context provided to driver in order
221  *				   to dump
222  * @info: info
223  * @cmd: devlink command
224  * @skb: skb
225  * @nest: top attribute
226  * @hdr: hdr
227  */
228 struct devlink_dpipe_dump_ctx {
229 	struct genl_info *info;
230 	enum devlink_command cmd;
231 	struct sk_buff *skb;
232 	struct nlattr *nest;
233 	void *hdr;
234 };
235 
236 struct devlink_dpipe_table_ops;
237 
238 /**
239  * struct devlink_dpipe_table - table object
240  * @priv: private
241  * @name: table name
242  * @counters_enabled: indicates if counters are active
243  * @counter_control_extern: indicates if counter control is in dpipe or
244  *			    external tool
245  * @resource_valid: Indicate that the resource id is valid
246  * @resource_id: relative resource this table is related to
247  * @resource_units: number of resource's unit consumed per table's entry
248  * @table_ops: table operations
249  * @rcu: rcu
250  */
251 struct devlink_dpipe_table {
252 	void *priv;
253 	struct list_head list;
254 	const char *name;
255 	bool counters_enabled;
256 	bool counter_control_extern;
257 	bool resource_valid;
258 	u64 resource_id;
259 	u64 resource_units;
260 	struct devlink_dpipe_table_ops *table_ops;
261 	struct rcu_head rcu;
262 };
263 
264 /**
265  * struct devlink_dpipe_table_ops - dpipe_table ops
266  * @actions_dump - dumps all tables actions
267  * @matches_dump - dumps all tables matches
268  * @entries_dump - dumps all active entries in the table
269  * @counters_set_update - when changing the counter status hardware sync
270  *			  maybe needed to allocate/free counter related
271  *			  resources
272  * @size_get - get size
273  */
274 struct devlink_dpipe_table_ops {
275 	int (*actions_dump)(void *priv, struct sk_buff *skb);
276 	int (*matches_dump)(void *priv, struct sk_buff *skb);
277 	int (*entries_dump)(void *priv, bool counters_enabled,
278 			    struct devlink_dpipe_dump_ctx *dump_ctx);
279 	int (*counters_set_update)(void *priv, bool enable);
280 	u64 (*size_get)(void *priv);
281 };
282 
283 /**
284  * struct devlink_dpipe_headers - dpipe headers
285  * @headers - header array can be shared (global bit) or driver specific
286  * @headers_count - count of headers
287  */
288 struct devlink_dpipe_headers {
289 	struct devlink_dpipe_header **headers;
290 	unsigned int headers_count;
291 };
292 
293 /**
294  * struct devlink_resource_size_params - resource's size parameters
295  * @size_min: minimum size which can be set
296  * @size_max: maximum size which can be set
297  * @size_granularity: size granularity
298  * @size_unit: resource's basic unit
299  */
300 struct devlink_resource_size_params {
301 	u64 size_min;
302 	u64 size_max;
303 	u64 size_granularity;
304 	enum devlink_resource_unit unit;
305 };
306 
307 static inline void
308 devlink_resource_size_params_init(struct devlink_resource_size_params *size_params,
309 				  u64 size_min, u64 size_max,
310 				  u64 size_granularity,
311 				  enum devlink_resource_unit unit)
312 {
313 	size_params->size_min = size_min;
314 	size_params->size_max = size_max;
315 	size_params->size_granularity = size_granularity;
316 	size_params->unit = unit;
317 }
318 
319 typedef u64 devlink_resource_occ_get_t(void *priv);
320 
321 /**
322  * struct devlink_resource - devlink resource
323  * @name: name of the resource
324  * @id: id, per devlink instance
325  * @size: size of the resource
326  * @size_new: updated size of the resource, reload is needed
327  * @size_valid: valid in case the total size of the resource is valid
328  *              including its children
329  * @parent: parent resource
330  * @size_params: size parameters
331  * @list: parent list
332  * @resource_list: list of child resources
333  */
334 struct devlink_resource {
335 	const char *name;
336 	u64 id;
337 	u64 size;
338 	u64 size_new;
339 	bool size_valid;
340 	struct devlink_resource *parent;
341 	struct devlink_resource_size_params size_params;
342 	struct list_head list;
343 	struct list_head resource_list;
344 	devlink_resource_occ_get_t *occ_get;
345 	void *occ_get_priv;
346 };
347 
348 #define DEVLINK_RESOURCE_ID_PARENT_TOP 0
349 
350 #define __DEVLINK_PARAM_MAX_STRING_VALUE 32
351 enum devlink_param_type {
352 	DEVLINK_PARAM_TYPE_U8,
353 	DEVLINK_PARAM_TYPE_U16,
354 	DEVLINK_PARAM_TYPE_U32,
355 	DEVLINK_PARAM_TYPE_STRING,
356 	DEVLINK_PARAM_TYPE_BOOL,
357 };
358 
359 union devlink_param_value {
360 	u8 vu8;
361 	u16 vu16;
362 	u32 vu32;
363 	char vstr[__DEVLINK_PARAM_MAX_STRING_VALUE];
364 	bool vbool;
365 };
366 
367 struct devlink_param_gset_ctx {
368 	union devlink_param_value val;
369 	enum devlink_param_cmode cmode;
370 };
371 
372 /**
373  * struct devlink_param - devlink configuration parameter data
374  * @name: name of the parameter
375  * @generic: indicates if the parameter is generic or driver specific
376  * @type: parameter type
377  * @supported_cmodes: bitmap of supported configuration modes
378  * @get: get parameter value, used for runtime and permanent
379  *       configuration modes
380  * @set: set parameter value, used for runtime and permanent
381  *       configuration modes
382  * @validate: validate input value is applicable (within value range, etc.)
383  *
384  * This struct should be used by the driver to fill the data for
385  * a parameter it registers.
386  */
387 struct devlink_param {
388 	u32 id;
389 	const char *name;
390 	bool generic;
391 	enum devlink_param_type type;
392 	unsigned long supported_cmodes;
393 	int (*get)(struct devlink *devlink, u32 id,
394 		   struct devlink_param_gset_ctx *ctx);
395 	int (*set)(struct devlink *devlink, u32 id,
396 		   struct devlink_param_gset_ctx *ctx);
397 	int (*validate)(struct devlink *devlink, u32 id,
398 			union devlink_param_value val,
399 			struct netlink_ext_ack *extack);
400 };
401 
402 struct devlink_param_item {
403 	struct list_head list;
404 	const struct devlink_param *param;
405 	union devlink_param_value driverinit_value;
406 	bool driverinit_value_valid;
407 	bool published;
408 };
409 
410 enum devlink_param_generic_id {
411 	DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
412 	DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
413 	DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
414 	DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
415 	DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
416 	DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
417 	DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
418 	DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
419 	DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
420 	DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
421 
422 	/* add new param generic ids above here*/
423 	__DEVLINK_PARAM_GENERIC_ID_MAX,
424 	DEVLINK_PARAM_GENERIC_ID_MAX = __DEVLINK_PARAM_GENERIC_ID_MAX - 1,
425 };
426 
427 #define DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME "internal_error_reset"
428 #define DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE DEVLINK_PARAM_TYPE_BOOL
429 
430 #define DEVLINK_PARAM_GENERIC_MAX_MACS_NAME "max_macs"
431 #define DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE DEVLINK_PARAM_TYPE_U32
432 
433 #define DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME "enable_sriov"
434 #define DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE DEVLINK_PARAM_TYPE_BOOL
435 
436 #define DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME "region_snapshot_enable"
437 #define DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE DEVLINK_PARAM_TYPE_BOOL
438 
439 #define DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME "ignore_ari"
440 #define DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE DEVLINK_PARAM_TYPE_BOOL
441 
442 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME "msix_vec_per_pf_max"
443 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE DEVLINK_PARAM_TYPE_U32
444 
445 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME "msix_vec_per_pf_min"
446 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE DEVLINK_PARAM_TYPE_U32
447 
448 #define DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME "fw_load_policy"
449 #define DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE DEVLINK_PARAM_TYPE_U8
450 
451 #define DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME \
452 	"reset_dev_on_drv_probe"
453 #define DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE DEVLINK_PARAM_TYPE_U8
454 
455 #define DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME "enable_roce"
456 #define DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE DEVLINK_PARAM_TYPE_BOOL
457 
458 #define DEVLINK_PARAM_GENERIC(_id, _cmodes, _get, _set, _validate)	\
459 {									\
460 	.id = DEVLINK_PARAM_GENERIC_ID_##_id,				\
461 	.name = DEVLINK_PARAM_GENERIC_##_id##_NAME,			\
462 	.type = DEVLINK_PARAM_GENERIC_##_id##_TYPE,			\
463 	.generic = true,						\
464 	.supported_cmodes = _cmodes,					\
465 	.get = _get,							\
466 	.set = _set,							\
467 	.validate = _validate,						\
468 }
469 
470 #define DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, _get, _set, _validate)	\
471 {									\
472 	.id = _id,							\
473 	.name = _name,							\
474 	.type = _type,							\
475 	.supported_cmodes = _cmodes,					\
476 	.get = _get,							\
477 	.set = _set,							\
478 	.validate = _validate,						\
479 }
480 
481 /* Part number, identifier of board design */
482 #define DEVLINK_INFO_VERSION_GENERIC_BOARD_ID	"board.id"
483 /* Revision of board design */
484 #define DEVLINK_INFO_VERSION_GENERIC_BOARD_REV	"board.rev"
485 /* Maker of the board */
486 #define DEVLINK_INFO_VERSION_GENERIC_BOARD_MANUFACTURE	"board.manufacture"
487 
488 /* Part number, identifier of asic design */
489 #define DEVLINK_INFO_VERSION_GENERIC_ASIC_ID	"asic.id"
490 /* Revision of asic design */
491 #define DEVLINK_INFO_VERSION_GENERIC_ASIC_REV	"asic.rev"
492 
493 /* Overall FW version */
494 #define DEVLINK_INFO_VERSION_GENERIC_FW		"fw"
495 /* Control processor FW version */
496 #define DEVLINK_INFO_VERSION_GENERIC_FW_MGMT	"fw.mgmt"
497 /* FW interface specification version */
498 #define DEVLINK_INFO_VERSION_GENERIC_FW_MGMT_API	"fw.mgmt.api"
499 /* Data path microcode controlling high-speed packet processing */
500 #define DEVLINK_INFO_VERSION_GENERIC_FW_APP	"fw.app"
501 /* UNDI software version */
502 #define DEVLINK_INFO_VERSION_GENERIC_FW_UNDI	"fw.undi"
503 /* NCSI support/handler version */
504 #define DEVLINK_INFO_VERSION_GENERIC_FW_NCSI	"fw.ncsi"
505 /* FW parameter set id */
506 #define DEVLINK_INFO_VERSION_GENERIC_FW_PSID	"fw.psid"
507 /* RoCE FW version */
508 #define DEVLINK_INFO_VERSION_GENERIC_FW_ROCE	"fw.roce"
509 /* Firmware bundle identifier */
510 #define DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID	"fw.bundle_id"
511 
512 struct devlink_region;
513 struct devlink_info_req;
514 
515 /**
516  * struct devlink_region_ops - Region operations
517  * @name: region name
518  * @destructor: callback used to free snapshot memory when deleting
519  * @snapshot: callback to request an immediate snapshot. On success,
520  *            the data variable must be updated to point to the snapshot data.
521  *            The function will be called while the devlink instance lock is
522  *            held.
523  */
524 struct devlink_region_ops {
525 	const char *name;
526 	void (*destructor)(const void *data);
527 	int (*snapshot)(struct devlink *devlink, struct netlink_ext_ack *extack,
528 			u8 **data);
529 };
530 
531 struct devlink_fmsg;
532 struct devlink_health_reporter;
533 
534 enum devlink_health_reporter_state {
535 	DEVLINK_HEALTH_REPORTER_STATE_HEALTHY,
536 	DEVLINK_HEALTH_REPORTER_STATE_ERROR,
537 };
538 
539 /**
540  * struct devlink_health_reporter_ops - Reporter operations
541  * @name: reporter name
542  * @recover: callback to recover from reported error
543  *           if priv_ctx is NULL, run a full recover
544  * @dump: callback to dump an object
545  *        if priv_ctx is NULL, run a full dump
546  * @diagnose: callback to diagnose the current status
547  */
548 
549 struct devlink_health_reporter_ops {
550 	char *name;
551 	int (*recover)(struct devlink_health_reporter *reporter,
552 		       void *priv_ctx, struct netlink_ext_ack *extack);
553 	int (*dump)(struct devlink_health_reporter *reporter,
554 		    struct devlink_fmsg *fmsg, void *priv_ctx,
555 		    struct netlink_ext_ack *extack);
556 	int (*diagnose)(struct devlink_health_reporter *reporter,
557 			struct devlink_fmsg *fmsg,
558 			struct netlink_ext_ack *extack);
559 };
560 
561 /**
562  * struct devlink_trap_policer - Immutable packet trap policer attributes.
563  * @id: Policer identifier.
564  * @init_rate: Initial rate in packets / sec.
565  * @init_burst: Initial burst size in packets.
566  * @max_rate: Maximum rate.
567  * @min_rate: Minimum rate.
568  * @max_burst: Maximum burst size.
569  * @min_burst: Minimum burst size.
570  *
571  * Describes immutable attributes of packet trap policers that drivers register
572  * with devlink.
573  */
574 struct devlink_trap_policer {
575 	u32 id;
576 	u64 init_rate;
577 	u64 init_burst;
578 	u64 max_rate;
579 	u64 min_rate;
580 	u64 max_burst;
581 	u64 min_burst;
582 };
583 
584 /**
585  * struct devlink_trap_group - Immutable packet trap group attributes.
586  * @name: Trap group name.
587  * @id: Trap group identifier.
588  * @generic: Whether the trap group is generic or not.
589  * @init_policer_id: Initial policer identifier.
590  *
591  * Describes immutable attributes of packet trap groups that drivers register
592  * with devlink.
593  */
594 struct devlink_trap_group {
595 	const char *name;
596 	u16 id;
597 	bool generic;
598 	u32 init_policer_id;
599 };
600 
601 #define DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT	BIT(0)
602 #define DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE	BIT(1)
603 
604 /**
605  * struct devlink_trap - Immutable packet trap attributes.
606  * @type: Trap type.
607  * @init_action: Initial trap action.
608  * @generic: Whether the trap is generic or not.
609  * @id: Trap identifier.
610  * @name: Trap name.
611  * @init_group_id: Initial group identifier.
612  * @metadata_cap: Metadata types that can be provided by the trap.
613  *
614  * Describes immutable attributes of packet traps that drivers register with
615  * devlink.
616  */
617 struct devlink_trap {
618 	enum devlink_trap_type type;
619 	enum devlink_trap_action init_action;
620 	bool generic;
621 	u16 id;
622 	const char *name;
623 	u16 init_group_id;
624 	u32 metadata_cap;
625 };
626 
627 /* All traps must be documented in
628  * Documentation/networking/devlink/devlink-trap.rst
629  */
630 enum devlink_trap_generic_id {
631 	DEVLINK_TRAP_GENERIC_ID_SMAC_MC,
632 	DEVLINK_TRAP_GENERIC_ID_VLAN_TAG_MISMATCH,
633 	DEVLINK_TRAP_GENERIC_ID_INGRESS_VLAN_FILTER,
634 	DEVLINK_TRAP_GENERIC_ID_INGRESS_STP_FILTER,
635 	DEVLINK_TRAP_GENERIC_ID_EMPTY_TX_LIST,
636 	DEVLINK_TRAP_GENERIC_ID_PORT_LOOPBACK_FILTER,
637 	DEVLINK_TRAP_GENERIC_ID_BLACKHOLE_ROUTE,
638 	DEVLINK_TRAP_GENERIC_ID_TTL_ERROR,
639 	DEVLINK_TRAP_GENERIC_ID_TAIL_DROP,
640 	DEVLINK_TRAP_GENERIC_ID_NON_IP_PACKET,
641 	DEVLINK_TRAP_GENERIC_ID_UC_DIP_MC_DMAC,
642 	DEVLINK_TRAP_GENERIC_ID_DIP_LB,
643 	DEVLINK_TRAP_GENERIC_ID_SIP_MC,
644 	DEVLINK_TRAP_GENERIC_ID_SIP_LB,
645 	DEVLINK_TRAP_GENERIC_ID_CORRUPTED_IP_HDR,
646 	DEVLINK_TRAP_GENERIC_ID_IPV4_SIP_BC,
647 	DEVLINK_TRAP_GENERIC_ID_IPV6_MC_DIP_RESERVED_SCOPE,
648 	DEVLINK_TRAP_GENERIC_ID_IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE,
649 	DEVLINK_TRAP_GENERIC_ID_MTU_ERROR,
650 	DEVLINK_TRAP_GENERIC_ID_UNRESOLVED_NEIGH,
651 	DEVLINK_TRAP_GENERIC_ID_RPF,
652 	DEVLINK_TRAP_GENERIC_ID_REJECT_ROUTE,
653 	DEVLINK_TRAP_GENERIC_ID_IPV4_LPM_UNICAST_MISS,
654 	DEVLINK_TRAP_GENERIC_ID_IPV6_LPM_UNICAST_MISS,
655 	DEVLINK_TRAP_GENERIC_ID_NON_ROUTABLE,
656 	DEVLINK_TRAP_GENERIC_ID_DECAP_ERROR,
657 	DEVLINK_TRAP_GENERIC_ID_OVERLAY_SMAC_MC,
658 	DEVLINK_TRAP_GENERIC_ID_INGRESS_FLOW_ACTION_DROP,
659 	DEVLINK_TRAP_GENERIC_ID_EGRESS_FLOW_ACTION_DROP,
660 	DEVLINK_TRAP_GENERIC_ID_STP,
661 	DEVLINK_TRAP_GENERIC_ID_LACP,
662 	DEVLINK_TRAP_GENERIC_ID_LLDP,
663 	DEVLINK_TRAP_GENERIC_ID_IGMP_QUERY,
664 	DEVLINK_TRAP_GENERIC_ID_IGMP_V1_REPORT,
665 	DEVLINK_TRAP_GENERIC_ID_IGMP_V2_REPORT,
666 	DEVLINK_TRAP_GENERIC_ID_IGMP_V3_REPORT,
667 	DEVLINK_TRAP_GENERIC_ID_IGMP_V2_LEAVE,
668 	DEVLINK_TRAP_GENERIC_ID_MLD_QUERY,
669 	DEVLINK_TRAP_GENERIC_ID_MLD_V1_REPORT,
670 	DEVLINK_TRAP_GENERIC_ID_MLD_V2_REPORT,
671 	DEVLINK_TRAP_GENERIC_ID_MLD_V1_DONE,
672 	DEVLINK_TRAP_GENERIC_ID_IPV4_DHCP,
673 	DEVLINK_TRAP_GENERIC_ID_IPV6_DHCP,
674 	DEVLINK_TRAP_GENERIC_ID_ARP_REQUEST,
675 	DEVLINK_TRAP_GENERIC_ID_ARP_RESPONSE,
676 	DEVLINK_TRAP_GENERIC_ID_ARP_OVERLAY,
677 	DEVLINK_TRAP_GENERIC_ID_IPV6_NEIGH_SOLICIT,
678 	DEVLINK_TRAP_GENERIC_ID_IPV6_NEIGH_ADVERT,
679 	DEVLINK_TRAP_GENERIC_ID_IPV4_BFD,
680 	DEVLINK_TRAP_GENERIC_ID_IPV6_BFD,
681 	DEVLINK_TRAP_GENERIC_ID_IPV4_OSPF,
682 	DEVLINK_TRAP_GENERIC_ID_IPV6_OSPF,
683 	DEVLINK_TRAP_GENERIC_ID_IPV4_BGP,
684 	DEVLINK_TRAP_GENERIC_ID_IPV6_BGP,
685 	DEVLINK_TRAP_GENERIC_ID_IPV4_VRRP,
686 	DEVLINK_TRAP_GENERIC_ID_IPV6_VRRP,
687 	DEVLINK_TRAP_GENERIC_ID_IPV4_PIM,
688 	DEVLINK_TRAP_GENERIC_ID_IPV6_PIM,
689 	DEVLINK_TRAP_GENERIC_ID_UC_LB,
690 	DEVLINK_TRAP_GENERIC_ID_LOCAL_ROUTE,
691 	DEVLINK_TRAP_GENERIC_ID_EXTERNAL_ROUTE,
692 	DEVLINK_TRAP_GENERIC_ID_IPV6_UC_DIP_LINK_LOCAL_SCOPE,
693 	DEVLINK_TRAP_GENERIC_ID_IPV6_DIP_ALL_NODES,
694 	DEVLINK_TRAP_GENERIC_ID_IPV6_DIP_ALL_ROUTERS,
695 	DEVLINK_TRAP_GENERIC_ID_IPV6_ROUTER_SOLICIT,
696 	DEVLINK_TRAP_GENERIC_ID_IPV6_ROUTER_ADVERT,
697 	DEVLINK_TRAP_GENERIC_ID_IPV6_REDIRECT,
698 	DEVLINK_TRAP_GENERIC_ID_IPV4_ROUTER_ALERT,
699 	DEVLINK_TRAP_GENERIC_ID_IPV6_ROUTER_ALERT,
700 	DEVLINK_TRAP_GENERIC_ID_PTP_EVENT,
701 	DEVLINK_TRAP_GENERIC_ID_PTP_GENERAL,
702 	DEVLINK_TRAP_GENERIC_ID_FLOW_ACTION_SAMPLE,
703 	DEVLINK_TRAP_GENERIC_ID_FLOW_ACTION_TRAP,
704 
705 	/* Add new generic trap IDs above */
706 	__DEVLINK_TRAP_GENERIC_ID_MAX,
707 	DEVLINK_TRAP_GENERIC_ID_MAX = __DEVLINK_TRAP_GENERIC_ID_MAX - 1,
708 };
709 
710 /* All trap groups must be documented in
711  * Documentation/networking/devlink/devlink-trap.rst
712  */
713 enum devlink_trap_group_generic_id {
714 	DEVLINK_TRAP_GROUP_GENERIC_ID_L2_DROPS,
715 	DEVLINK_TRAP_GROUP_GENERIC_ID_L3_DROPS,
716 	DEVLINK_TRAP_GROUP_GENERIC_ID_L3_EXCEPTIONS,
717 	DEVLINK_TRAP_GROUP_GENERIC_ID_BUFFER_DROPS,
718 	DEVLINK_TRAP_GROUP_GENERIC_ID_TUNNEL_DROPS,
719 	DEVLINK_TRAP_GROUP_GENERIC_ID_ACL_DROPS,
720 	DEVLINK_TRAP_GROUP_GENERIC_ID_STP,
721 	DEVLINK_TRAP_GROUP_GENERIC_ID_LACP,
722 	DEVLINK_TRAP_GROUP_GENERIC_ID_LLDP,
723 	DEVLINK_TRAP_GROUP_GENERIC_ID_MC_SNOOPING,
724 	DEVLINK_TRAP_GROUP_GENERIC_ID_DHCP,
725 	DEVLINK_TRAP_GROUP_GENERIC_ID_NEIGH_DISCOVERY,
726 	DEVLINK_TRAP_GROUP_GENERIC_ID_BFD,
727 	DEVLINK_TRAP_GROUP_GENERIC_ID_OSPF,
728 	DEVLINK_TRAP_GROUP_GENERIC_ID_BGP,
729 	DEVLINK_TRAP_GROUP_GENERIC_ID_VRRP,
730 	DEVLINK_TRAP_GROUP_GENERIC_ID_PIM,
731 	DEVLINK_TRAP_GROUP_GENERIC_ID_UC_LB,
732 	DEVLINK_TRAP_GROUP_GENERIC_ID_LOCAL_DELIVERY,
733 	DEVLINK_TRAP_GROUP_GENERIC_ID_IPV6,
734 	DEVLINK_TRAP_GROUP_GENERIC_ID_PTP_EVENT,
735 	DEVLINK_TRAP_GROUP_GENERIC_ID_PTP_GENERAL,
736 	DEVLINK_TRAP_GROUP_GENERIC_ID_ACL_SAMPLE,
737 	DEVLINK_TRAP_GROUP_GENERIC_ID_ACL_TRAP,
738 
739 	/* Add new generic trap group IDs above */
740 	__DEVLINK_TRAP_GROUP_GENERIC_ID_MAX,
741 	DEVLINK_TRAP_GROUP_GENERIC_ID_MAX =
742 		__DEVLINK_TRAP_GROUP_GENERIC_ID_MAX - 1,
743 };
744 
745 #define DEVLINK_TRAP_GENERIC_NAME_SMAC_MC \
746 	"source_mac_is_multicast"
747 #define DEVLINK_TRAP_GENERIC_NAME_VLAN_TAG_MISMATCH \
748 	"vlan_tag_mismatch"
749 #define DEVLINK_TRAP_GENERIC_NAME_INGRESS_VLAN_FILTER \
750 	"ingress_vlan_filter"
751 #define DEVLINK_TRAP_GENERIC_NAME_INGRESS_STP_FILTER \
752 	"ingress_spanning_tree_filter"
753 #define DEVLINK_TRAP_GENERIC_NAME_EMPTY_TX_LIST \
754 	"port_list_is_empty"
755 #define DEVLINK_TRAP_GENERIC_NAME_PORT_LOOPBACK_FILTER \
756 	"port_loopback_filter"
757 #define DEVLINK_TRAP_GENERIC_NAME_BLACKHOLE_ROUTE \
758 	"blackhole_route"
759 #define DEVLINK_TRAP_GENERIC_NAME_TTL_ERROR \
760 	"ttl_value_is_too_small"
761 #define DEVLINK_TRAP_GENERIC_NAME_TAIL_DROP \
762 	"tail_drop"
763 #define DEVLINK_TRAP_GENERIC_NAME_NON_IP_PACKET \
764 	"non_ip"
765 #define DEVLINK_TRAP_GENERIC_NAME_UC_DIP_MC_DMAC \
766 	"uc_dip_over_mc_dmac"
767 #define DEVLINK_TRAP_GENERIC_NAME_DIP_LB \
768 	"dip_is_loopback_address"
769 #define DEVLINK_TRAP_GENERIC_NAME_SIP_MC \
770 	"sip_is_mc"
771 #define DEVLINK_TRAP_GENERIC_NAME_SIP_LB \
772 	"sip_is_loopback_address"
773 #define DEVLINK_TRAP_GENERIC_NAME_CORRUPTED_IP_HDR \
774 	"ip_header_corrupted"
775 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_SIP_BC \
776 	"ipv4_sip_is_limited_bc"
777 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_MC_DIP_RESERVED_SCOPE \
778 	"ipv6_mc_dip_reserved_scope"
779 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE \
780 	"ipv6_mc_dip_interface_local_scope"
781 #define DEVLINK_TRAP_GENERIC_NAME_MTU_ERROR \
782 	"mtu_value_is_too_small"
783 #define DEVLINK_TRAP_GENERIC_NAME_UNRESOLVED_NEIGH \
784 	"unresolved_neigh"
785 #define DEVLINK_TRAP_GENERIC_NAME_RPF \
786 	"mc_reverse_path_forwarding"
787 #define DEVLINK_TRAP_GENERIC_NAME_REJECT_ROUTE \
788 	"reject_route"
789 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_LPM_UNICAST_MISS \
790 	"ipv4_lpm_miss"
791 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_LPM_UNICAST_MISS \
792 	"ipv6_lpm_miss"
793 #define DEVLINK_TRAP_GENERIC_NAME_NON_ROUTABLE \
794 	"non_routable_packet"
795 #define DEVLINK_TRAP_GENERIC_NAME_DECAP_ERROR \
796 	"decap_error"
797 #define DEVLINK_TRAP_GENERIC_NAME_OVERLAY_SMAC_MC \
798 	"overlay_smac_is_mc"
799 #define DEVLINK_TRAP_GENERIC_NAME_INGRESS_FLOW_ACTION_DROP \
800 	"ingress_flow_action_drop"
801 #define DEVLINK_TRAP_GENERIC_NAME_EGRESS_FLOW_ACTION_DROP \
802 	"egress_flow_action_drop"
803 #define DEVLINK_TRAP_GENERIC_NAME_STP \
804 	"stp"
805 #define DEVLINK_TRAP_GENERIC_NAME_LACP \
806 	"lacp"
807 #define DEVLINK_TRAP_GENERIC_NAME_LLDP \
808 	"lldp"
809 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_QUERY \
810 	"igmp_query"
811 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_V1_REPORT \
812 	"igmp_v1_report"
813 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_V2_REPORT \
814 	"igmp_v2_report"
815 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_V3_REPORT \
816 	"igmp_v3_report"
817 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_V2_LEAVE \
818 	"igmp_v2_leave"
819 #define DEVLINK_TRAP_GENERIC_NAME_MLD_QUERY \
820 	"mld_query"
821 #define DEVLINK_TRAP_GENERIC_NAME_MLD_V1_REPORT \
822 	"mld_v1_report"
823 #define DEVLINK_TRAP_GENERIC_NAME_MLD_V2_REPORT \
824 	"mld_v2_report"
825 #define DEVLINK_TRAP_GENERIC_NAME_MLD_V1_DONE \
826 	"mld_v1_done"
827 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_DHCP \
828 	"ipv4_dhcp"
829 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_DHCP \
830 	"ipv6_dhcp"
831 #define DEVLINK_TRAP_GENERIC_NAME_ARP_REQUEST \
832 	"arp_request"
833 #define DEVLINK_TRAP_GENERIC_NAME_ARP_RESPONSE \
834 	"arp_response"
835 #define DEVLINK_TRAP_GENERIC_NAME_ARP_OVERLAY \
836 	"arp_overlay"
837 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_NEIGH_SOLICIT \
838 	"ipv6_neigh_solicit"
839 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_NEIGH_ADVERT \
840 	"ipv6_neigh_advert"
841 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_BFD \
842 	"ipv4_bfd"
843 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_BFD \
844 	"ipv6_bfd"
845 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_OSPF \
846 	"ipv4_ospf"
847 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_OSPF \
848 	"ipv6_ospf"
849 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_BGP \
850 	"ipv4_bgp"
851 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_BGP \
852 	"ipv6_bgp"
853 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_VRRP \
854 	"ipv4_vrrp"
855 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_VRRP \
856 	"ipv6_vrrp"
857 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_PIM \
858 	"ipv4_pim"
859 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_PIM \
860 	"ipv6_pim"
861 #define DEVLINK_TRAP_GENERIC_NAME_UC_LB \
862 	"uc_loopback"
863 #define DEVLINK_TRAP_GENERIC_NAME_LOCAL_ROUTE \
864 	"local_route"
865 #define DEVLINK_TRAP_GENERIC_NAME_EXTERNAL_ROUTE \
866 	"external_route"
867 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_UC_DIP_LINK_LOCAL_SCOPE \
868 	"ipv6_uc_dip_link_local_scope"
869 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_DIP_ALL_NODES \
870 	"ipv6_dip_all_nodes"
871 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_DIP_ALL_ROUTERS \
872 	"ipv6_dip_all_routers"
873 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_ROUTER_SOLICIT \
874 	"ipv6_router_solicit"
875 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_ROUTER_ADVERT \
876 	"ipv6_router_advert"
877 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_REDIRECT \
878 	"ipv6_redirect"
879 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_ROUTER_ALERT \
880 	"ipv4_router_alert"
881 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_ROUTER_ALERT \
882 	"ipv6_router_alert"
883 #define DEVLINK_TRAP_GENERIC_NAME_PTP_EVENT \
884 	"ptp_event"
885 #define DEVLINK_TRAP_GENERIC_NAME_PTP_GENERAL \
886 	"ptp_general"
887 #define DEVLINK_TRAP_GENERIC_NAME_FLOW_ACTION_SAMPLE \
888 	"flow_action_sample"
889 #define DEVLINK_TRAP_GENERIC_NAME_FLOW_ACTION_TRAP \
890 	"flow_action_trap"
891 
892 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_L2_DROPS \
893 	"l2_drops"
894 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_L3_DROPS \
895 	"l3_drops"
896 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_L3_EXCEPTIONS \
897 	"l3_exceptions"
898 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_BUFFER_DROPS \
899 	"buffer_drops"
900 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_TUNNEL_DROPS \
901 	"tunnel_drops"
902 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_ACL_DROPS \
903 	"acl_drops"
904 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_STP \
905 	"stp"
906 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_LACP \
907 	"lacp"
908 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_LLDP \
909 	"lldp"
910 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_MC_SNOOPING  \
911 	"mc_snooping"
912 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_DHCP \
913 	"dhcp"
914 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_NEIGH_DISCOVERY \
915 	"neigh_discovery"
916 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_BFD \
917 	"bfd"
918 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_OSPF \
919 	"ospf"
920 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_BGP \
921 	"bgp"
922 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_VRRP \
923 	"vrrp"
924 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_PIM \
925 	"pim"
926 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_UC_LB \
927 	"uc_loopback"
928 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_LOCAL_DELIVERY \
929 	"local_delivery"
930 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_IPV6 \
931 	"ipv6"
932 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_PTP_EVENT \
933 	"ptp_event"
934 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_PTP_GENERAL \
935 	"ptp_general"
936 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_ACL_SAMPLE \
937 	"acl_sample"
938 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_ACL_TRAP \
939 	"acl_trap"
940 
941 #define DEVLINK_TRAP_GENERIC(_type, _init_action, _id, _group_id,	      \
942 			     _metadata_cap)				      \
943 	{								      \
944 		.type = DEVLINK_TRAP_TYPE_##_type,			      \
945 		.init_action = DEVLINK_TRAP_ACTION_##_init_action,	      \
946 		.generic = true,					      \
947 		.id = DEVLINK_TRAP_GENERIC_ID_##_id,			      \
948 		.name = DEVLINK_TRAP_GENERIC_NAME_##_id,		      \
949 		.init_group_id = _group_id,				      \
950 		.metadata_cap = _metadata_cap,				      \
951 	}
952 
953 #define DEVLINK_TRAP_DRIVER(_type, _init_action, _id, _name, _group_id,	      \
954 			    _metadata_cap)				      \
955 	{								      \
956 		.type = DEVLINK_TRAP_TYPE_##_type,			      \
957 		.init_action = DEVLINK_TRAP_ACTION_##_init_action,	      \
958 		.generic = false,					      \
959 		.id = _id,						      \
960 		.name = _name,						      \
961 		.init_group_id = _group_id,				      \
962 		.metadata_cap = _metadata_cap,				      \
963 	}
964 
965 #define DEVLINK_TRAP_GROUP_GENERIC(_id, _policer_id)			      \
966 	{								      \
967 		.name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id,		      \
968 		.id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id,		      \
969 		.generic = true,					      \
970 		.init_policer_id = _policer_id,				      \
971 	}
972 
973 #define DEVLINK_TRAP_POLICER(_id, _rate, _burst, _max_rate, _min_rate,	      \
974 			     _max_burst, _min_burst)			      \
975 	{								      \
976 		.id = _id,						      \
977 		.init_rate = _rate,					      \
978 		.init_burst = _burst,					      \
979 		.max_rate = _max_rate,					      \
980 		.min_rate = _min_rate,					      \
981 		.max_burst = _max_burst,				      \
982 		.min_burst = _min_burst,				      \
983 	}
984 
985 struct devlink_ops {
986 	int (*reload_down)(struct devlink *devlink, bool netns_change,
987 			   struct netlink_ext_ack *extack);
988 	int (*reload_up)(struct devlink *devlink,
989 			 struct netlink_ext_ack *extack);
990 	int (*port_type_set)(struct devlink_port *devlink_port,
991 			     enum devlink_port_type port_type);
992 	int (*port_split)(struct devlink *devlink, unsigned int port_index,
993 			  unsigned int count, struct netlink_ext_ack *extack);
994 	int (*port_unsplit)(struct devlink *devlink, unsigned int port_index,
995 			    struct netlink_ext_ack *extack);
996 	int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index,
997 			   u16 pool_index,
998 			   struct devlink_sb_pool_info *pool_info);
999 	int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index,
1000 			   u16 pool_index, u32 size,
1001 			   enum devlink_sb_threshold_type threshold_type,
1002 			   struct netlink_ext_ack *extack);
1003 	int (*sb_port_pool_get)(struct devlink_port *devlink_port,
1004 				unsigned int sb_index, u16 pool_index,
1005 				u32 *p_threshold);
1006 	int (*sb_port_pool_set)(struct devlink_port *devlink_port,
1007 				unsigned int sb_index, u16 pool_index,
1008 				u32 threshold, struct netlink_ext_ack *extack);
1009 	int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port,
1010 				   unsigned int sb_index,
1011 				   u16 tc_index,
1012 				   enum devlink_sb_pool_type pool_type,
1013 				   u16 *p_pool_index, u32 *p_threshold);
1014 	int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port,
1015 				   unsigned int sb_index,
1016 				   u16 tc_index,
1017 				   enum devlink_sb_pool_type pool_type,
1018 				   u16 pool_index, u32 threshold,
1019 				   struct netlink_ext_ack *extack);
1020 	int (*sb_occ_snapshot)(struct devlink *devlink,
1021 			       unsigned int sb_index);
1022 	int (*sb_occ_max_clear)(struct devlink *devlink,
1023 				unsigned int sb_index);
1024 	int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port,
1025 				    unsigned int sb_index, u16 pool_index,
1026 				    u32 *p_cur, u32 *p_max);
1027 	int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port,
1028 				       unsigned int sb_index,
1029 				       u16 tc_index,
1030 				       enum devlink_sb_pool_type pool_type,
1031 				       u32 *p_cur, u32 *p_max);
1032 
1033 	int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode);
1034 	int (*eswitch_mode_set)(struct devlink *devlink, u16 mode,
1035 				struct netlink_ext_ack *extack);
1036 	int (*eswitch_inline_mode_get)(struct devlink *devlink, u8 *p_inline_mode);
1037 	int (*eswitch_inline_mode_set)(struct devlink *devlink, u8 inline_mode,
1038 				       struct netlink_ext_ack *extack);
1039 	int (*eswitch_encap_mode_get)(struct devlink *devlink,
1040 				      enum devlink_eswitch_encap_mode *p_encap_mode);
1041 	int (*eswitch_encap_mode_set)(struct devlink *devlink,
1042 				      enum devlink_eswitch_encap_mode encap_mode,
1043 				      struct netlink_ext_ack *extack);
1044 	int (*info_get)(struct devlink *devlink, struct devlink_info_req *req,
1045 			struct netlink_ext_ack *extack);
1046 	int (*flash_update)(struct devlink *devlink, const char *file_name,
1047 			    const char *component,
1048 			    struct netlink_ext_ack *extack);
1049 	/**
1050 	 * @trap_init: Trap initialization function.
1051 	 *
1052 	 * Should be used by device drivers to initialize the trap in the
1053 	 * underlying device. Drivers should also store the provided trap
1054 	 * context, so that they could efficiently pass it to
1055 	 * devlink_trap_report() when the trap is triggered.
1056 	 */
1057 	int (*trap_init)(struct devlink *devlink,
1058 			 const struct devlink_trap *trap, void *trap_ctx);
1059 	/**
1060 	 * @trap_fini: Trap de-initialization function.
1061 	 *
1062 	 * Should be used by device drivers to de-initialize the trap in the
1063 	 * underlying device.
1064 	 */
1065 	void (*trap_fini)(struct devlink *devlink,
1066 			  const struct devlink_trap *trap, void *trap_ctx);
1067 	/**
1068 	 * @trap_action_set: Trap action set function.
1069 	 */
1070 	int (*trap_action_set)(struct devlink *devlink,
1071 			       const struct devlink_trap *trap,
1072 			       enum devlink_trap_action action);
1073 	/**
1074 	 * @trap_group_init: Trap group initialization function.
1075 	 *
1076 	 * Should be used by device drivers to initialize the trap group in the
1077 	 * underlying device.
1078 	 */
1079 	int (*trap_group_init)(struct devlink *devlink,
1080 			       const struct devlink_trap_group *group);
1081 	/**
1082 	 * @trap_group_set: Trap group parameters set function.
1083 	 *
1084 	 * Note: @policer can be NULL when a policer is being unbound from
1085 	 * @group.
1086 	 */
1087 	int (*trap_group_set)(struct devlink *devlink,
1088 			      const struct devlink_trap_group *group,
1089 			      const struct devlink_trap_policer *policer);
1090 	/**
1091 	 * @trap_policer_init: Trap policer initialization function.
1092 	 *
1093 	 * Should be used by device drivers to initialize the trap policer in
1094 	 * the underlying device.
1095 	 */
1096 	int (*trap_policer_init)(struct devlink *devlink,
1097 				 const struct devlink_trap_policer *policer);
1098 	/**
1099 	 * @trap_policer_fini: Trap policer de-initialization function.
1100 	 *
1101 	 * Should be used by device drivers to de-initialize the trap policer
1102 	 * in the underlying device.
1103 	 */
1104 	void (*trap_policer_fini)(struct devlink *devlink,
1105 				  const struct devlink_trap_policer *policer);
1106 	/**
1107 	 * @trap_policer_set: Trap policer parameters set function.
1108 	 */
1109 	int (*trap_policer_set)(struct devlink *devlink,
1110 				const struct devlink_trap_policer *policer,
1111 				u64 rate, u64 burst,
1112 				struct netlink_ext_ack *extack);
1113 	/**
1114 	 * @trap_policer_counter_get: Trap policer counter get function.
1115 	 *
1116 	 * Should be used by device drivers to report number of packets dropped
1117 	 * by the policer.
1118 	 */
1119 	int (*trap_policer_counter_get)(struct devlink *devlink,
1120 					const struct devlink_trap_policer *policer,
1121 					u64 *p_drops);
1122 	/**
1123 	 * @port_function_hw_addr_get: Port function's hardware address get function.
1124 	 *
1125 	 * Should be used by device drivers to report the hardware address of a function managed
1126 	 * by the devlink port. Driver should return -EOPNOTSUPP if it doesn't support port
1127 	 * function handling for a particular port.
1128 	 *
1129 	 * Note: @extack can be NULL when port notifier queries the port function.
1130 	 */
1131 	int (*port_function_hw_addr_get)(struct devlink *devlink, struct devlink_port *port,
1132 					 u8 *hw_addr, int *hw_addr_len,
1133 					 struct netlink_ext_ack *extack);
1134 	/**
1135 	 * @port_function_hw_addr_set: Port function's hardware address set function.
1136 	 *
1137 	 * Should be used by device drivers to set the hardware address of a function managed
1138 	 * by the devlink port. Driver should return -EOPNOTSUPP if it doesn't support port
1139 	 * function handling for a particular port.
1140 	 */
1141 	int (*port_function_hw_addr_set)(struct devlink *devlink, struct devlink_port *port,
1142 					 const u8 *hw_addr, int hw_addr_len,
1143 					 struct netlink_ext_ack *extack);
1144 };
1145 
1146 static inline void *devlink_priv(struct devlink *devlink)
1147 {
1148 	BUG_ON(!devlink);
1149 	return &devlink->priv;
1150 }
1151 
1152 static inline struct devlink *priv_to_devlink(void *priv)
1153 {
1154 	BUG_ON(!priv);
1155 	return container_of(priv, struct devlink, priv);
1156 }
1157 
1158 static inline struct devlink_port *
1159 netdev_to_devlink_port(struct net_device *dev)
1160 {
1161 	if (dev->netdev_ops->ndo_get_devlink_port)
1162 		return dev->netdev_ops->ndo_get_devlink_port(dev);
1163 	return NULL;
1164 }
1165 
1166 static inline struct devlink *netdev_to_devlink(struct net_device *dev)
1167 {
1168 	struct devlink_port *devlink_port = netdev_to_devlink_port(dev);
1169 
1170 	if (devlink_port)
1171 		return devlink_port->devlink;
1172 	return NULL;
1173 }
1174 
1175 struct ib_device;
1176 
1177 struct net *devlink_net(const struct devlink *devlink);
1178 void devlink_net_set(struct devlink *devlink, struct net *net);
1179 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
1180 int devlink_register(struct devlink *devlink, struct device *dev);
1181 void devlink_unregister(struct devlink *devlink);
1182 void devlink_reload_enable(struct devlink *devlink);
1183 void devlink_reload_disable(struct devlink *devlink);
1184 void devlink_free(struct devlink *devlink);
1185 int devlink_port_register(struct devlink *devlink,
1186 			  struct devlink_port *devlink_port,
1187 			  unsigned int port_index);
1188 void devlink_port_unregister(struct devlink_port *devlink_port);
1189 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
1190 			       struct net_device *netdev);
1191 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
1192 			      struct ib_device *ibdev);
1193 void devlink_port_type_clear(struct devlink_port *devlink_port);
1194 void devlink_port_attrs_set(struct devlink_port *devlink_port,
1195 			    struct devlink_port_attrs *devlink_port_attrs);
1196 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u16 pf);
1197 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port,
1198 				   u16 pf, u16 vf);
1199 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
1200 			u32 size, u16 ingress_pools_count,
1201 			u16 egress_pools_count, u16 ingress_tc_count,
1202 			u16 egress_tc_count);
1203 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index);
1204 int devlink_dpipe_table_register(struct devlink *devlink,
1205 				 const char *table_name,
1206 				 struct devlink_dpipe_table_ops *table_ops,
1207 				 void *priv, bool counter_control_extern);
1208 void devlink_dpipe_table_unregister(struct devlink *devlink,
1209 				    const char *table_name);
1210 int devlink_dpipe_headers_register(struct devlink *devlink,
1211 				   struct devlink_dpipe_headers *dpipe_headers);
1212 void devlink_dpipe_headers_unregister(struct devlink *devlink);
1213 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
1214 					 const char *table_name);
1215 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx);
1216 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
1217 				   struct devlink_dpipe_entry *entry);
1218 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx);
1219 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry);
1220 int devlink_dpipe_action_put(struct sk_buff *skb,
1221 			     struct devlink_dpipe_action *action);
1222 int devlink_dpipe_match_put(struct sk_buff *skb,
1223 			    struct devlink_dpipe_match *match);
1224 extern struct devlink_dpipe_header devlink_dpipe_header_ethernet;
1225 extern struct devlink_dpipe_header devlink_dpipe_header_ipv4;
1226 extern struct devlink_dpipe_header devlink_dpipe_header_ipv6;
1227 
1228 int devlink_resource_register(struct devlink *devlink,
1229 			      const char *resource_name,
1230 			      u64 resource_size,
1231 			      u64 resource_id,
1232 			      u64 parent_resource_id,
1233 			      const struct devlink_resource_size_params *size_params);
1234 void devlink_resources_unregister(struct devlink *devlink,
1235 				  struct devlink_resource *resource);
1236 int devlink_resource_size_get(struct devlink *devlink,
1237 			      u64 resource_id,
1238 			      u64 *p_resource_size);
1239 int devlink_dpipe_table_resource_set(struct devlink *devlink,
1240 				     const char *table_name, u64 resource_id,
1241 				     u64 resource_units);
1242 void devlink_resource_occ_get_register(struct devlink *devlink,
1243 				       u64 resource_id,
1244 				       devlink_resource_occ_get_t *occ_get,
1245 				       void *occ_get_priv);
1246 void devlink_resource_occ_get_unregister(struct devlink *devlink,
1247 					 u64 resource_id);
1248 int devlink_params_register(struct devlink *devlink,
1249 			    const struct devlink_param *params,
1250 			    size_t params_count);
1251 void devlink_params_unregister(struct devlink *devlink,
1252 			       const struct devlink_param *params,
1253 			       size_t params_count);
1254 void devlink_params_publish(struct devlink *devlink);
1255 void devlink_params_unpublish(struct devlink *devlink);
1256 int devlink_port_params_register(struct devlink_port *devlink_port,
1257 				 const struct devlink_param *params,
1258 				 size_t params_count);
1259 void devlink_port_params_unregister(struct devlink_port *devlink_port,
1260 				    const struct devlink_param *params,
1261 				    size_t params_count);
1262 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
1263 				       union devlink_param_value *init_val);
1264 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
1265 				       union devlink_param_value init_val);
1266 int
1267 devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
1268 					u32 param_id,
1269 					union devlink_param_value *init_val);
1270 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
1271 					    u32 param_id,
1272 					    union devlink_param_value init_val);
1273 void devlink_param_value_changed(struct devlink *devlink, u32 param_id);
1274 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
1275 				      u32 param_id);
1276 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
1277 				  const char *src);
1278 struct devlink_region *
1279 devlink_region_create(struct devlink *devlink,
1280 		      const struct devlink_region_ops *ops,
1281 		      u32 region_max_snapshots, u64 region_size);
1282 void devlink_region_destroy(struct devlink_region *region);
1283 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id);
1284 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id);
1285 int devlink_region_snapshot_create(struct devlink_region *region,
1286 				   u8 *data, u32 snapshot_id);
1287 int devlink_info_serial_number_put(struct devlink_info_req *req,
1288 				   const char *sn);
1289 int devlink_info_driver_name_put(struct devlink_info_req *req,
1290 				 const char *name);
1291 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
1292 					 const char *bsn);
1293 int devlink_info_version_fixed_put(struct devlink_info_req *req,
1294 				   const char *version_name,
1295 				   const char *version_value);
1296 int devlink_info_version_stored_put(struct devlink_info_req *req,
1297 				    const char *version_name,
1298 				    const char *version_value);
1299 int devlink_info_version_running_put(struct devlink_info_req *req,
1300 				     const char *version_name,
1301 				     const char *version_value);
1302 
1303 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg);
1304 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg);
1305 
1306 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name);
1307 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg);
1308 
1309 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
1310 				     const char *name);
1311 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg);
1312 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
1313 					const char *name);
1314 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg);
1315 
1316 int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value);
1317 int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value);
1318 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value);
1319 int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value);
1320 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value);
1321 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
1322 			    u16 value_len);
1323 
1324 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
1325 			       bool value);
1326 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
1327 			     u8 value);
1328 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
1329 			      u32 value);
1330 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
1331 			      u64 value);
1332 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
1333 				 const char *value);
1334 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
1335 				 const void *value, u32 value_len);
1336 
1337 struct devlink_health_reporter *
1338 devlink_health_reporter_create(struct devlink *devlink,
1339 			       const struct devlink_health_reporter_ops *ops,
1340 			       u64 graceful_period, void *priv);
1341 
1342 struct devlink_health_reporter *
1343 devlink_port_health_reporter_create(struct devlink_port *port,
1344 				    const struct devlink_health_reporter_ops *ops,
1345 				    u64 graceful_period, void *priv);
1346 
1347 void
1348 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter);
1349 
1350 void
1351 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter);
1352 
1353 void *
1354 devlink_health_reporter_priv(struct devlink_health_reporter *reporter);
1355 int devlink_health_report(struct devlink_health_reporter *reporter,
1356 			  const char *msg, void *priv_ctx);
1357 void
1358 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
1359 				     enum devlink_health_reporter_state state);
1360 void
1361 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter);
1362 
1363 bool devlink_is_reload_failed(const struct devlink *devlink);
1364 
1365 void devlink_flash_update_begin_notify(struct devlink *devlink);
1366 void devlink_flash_update_end_notify(struct devlink *devlink);
1367 void devlink_flash_update_status_notify(struct devlink *devlink,
1368 					const char *status_msg,
1369 					const char *component,
1370 					unsigned long done,
1371 					unsigned long total);
1372 
1373 int devlink_traps_register(struct devlink *devlink,
1374 			   const struct devlink_trap *traps,
1375 			   size_t traps_count, void *priv);
1376 void devlink_traps_unregister(struct devlink *devlink,
1377 			      const struct devlink_trap *traps,
1378 			      size_t traps_count);
1379 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
1380 			 void *trap_ctx, struct devlink_port *in_devlink_port,
1381 			 const struct flow_action_cookie *fa_cookie);
1382 void *devlink_trap_ctx_priv(void *trap_ctx);
1383 int devlink_trap_groups_register(struct devlink *devlink,
1384 				 const struct devlink_trap_group *groups,
1385 				 size_t groups_count);
1386 void devlink_trap_groups_unregister(struct devlink *devlink,
1387 				    const struct devlink_trap_group *groups,
1388 				    size_t groups_count);
1389 int
1390 devlink_trap_policers_register(struct devlink *devlink,
1391 			       const struct devlink_trap_policer *policers,
1392 			       size_t policers_count);
1393 void
1394 devlink_trap_policers_unregister(struct devlink *devlink,
1395 				 const struct devlink_trap_policer *policers,
1396 				 size_t policers_count);
1397 
1398 #if IS_ENABLED(CONFIG_NET_DEVLINK)
1399 
1400 void devlink_compat_running_version(struct net_device *dev,
1401 				    char *buf, size_t len);
1402 int devlink_compat_flash_update(struct net_device *dev, const char *file_name);
1403 int devlink_compat_phys_port_name_get(struct net_device *dev,
1404 				      char *name, size_t len);
1405 int devlink_compat_switch_id_get(struct net_device *dev,
1406 				 struct netdev_phys_item_id *ppid);
1407 
1408 #else
1409 
1410 static inline void
1411 devlink_compat_running_version(struct net_device *dev, char *buf, size_t len)
1412 {
1413 }
1414 
1415 static inline int
1416 devlink_compat_flash_update(struct net_device *dev, const char *file_name)
1417 {
1418 	return -EOPNOTSUPP;
1419 }
1420 
1421 static inline int
1422 devlink_compat_phys_port_name_get(struct net_device *dev,
1423 				  char *name, size_t len)
1424 {
1425 	return -EOPNOTSUPP;
1426 }
1427 
1428 static inline int
1429 devlink_compat_switch_id_get(struct net_device *dev,
1430 			     struct netdev_phys_item_id *ppid)
1431 {
1432 	return -EOPNOTSUPP;
1433 }
1434 
1435 #endif
1436 
1437 #endif /* _NET_DEVLINK_H_ */
1438