xref: /linux-6.15/include/net/switchdev.h (revision 87c167bb)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * include/net/switchdev.h - Switch device API
4  * Copyright (c) 2014-2015 Jiri Pirko <[email protected]>
5  * Copyright (c) 2014-2015 Scott Feldman <[email protected]>
6  */
7 #ifndef _LINUX_SWITCHDEV_H_
8 #define _LINUX_SWITCHDEV_H_
9 
10 #include <linux/netdevice.h>
11 #include <linux/notifier.h>
12 #include <linux/list.h>
13 #include <net/ip_fib.h>
14 
15 #define SWITCHDEV_F_NO_RECURSE		BIT(0)
16 #define SWITCHDEV_F_SKIP_EOPNOTSUPP	BIT(1)
17 #define SWITCHDEV_F_DEFER		BIT(2)
18 
19 enum switchdev_attr_id {
20 	SWITCHDEV_ATTR_ID_UNDEFINED,
21 	SWITCHDEV_ATTR_ID_PORT_STP_STATE,
22 	SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
23 	SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS,
24 	SWITCHDEV_ATTR_ID_PORT_MROUTER,
25 	SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
26 	SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
27 	SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL,
28 	SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
29 	SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
30 	SWITCHDEV_ATTR_ID_BRIDGE_MST,
31 	SWITCHDEV_ATTR_ID_MRP_PORT_ROLE,
32 };
33 
34 struct switchdev_brport_flags {
35 	unsigned long val;
36 	unsigned long mask;
37 };
38 
39 struct switchdev_attr {
40 	struct net_device *orig_dev;
41 	enum switchdev_attr_id id;
42 	u32 flags;
43 	void *complete_priv;
44 	void (*complete)(struct net_device *dev, int err, void *priv);
45 	union {
46 		u8 stp_state;				/* PORT_STP_STATE */
47 		struct switchdev_brport_flags brport_flags; /* PORT_BRIDGE_FLAGS */
48 		bool mrouter;				/* PORT_MROUTER */
49 		clock_t ageing_time;			/* BRIDGE_AGEING_TIME */
50 		bool vlan_filtering;			/* BRIDGE_VLAN_FILTERING */
51 		u16 vlan_protocol;			/* BRIDGE_VLAN_PROTOCOL */
52 		bool mst;				/* BRIDGE_MST */
53 		bool mc_disabled;			/* MC_DISABLED */
54 		u8 mrp_port_role;			/* MRP_PORT_ROLE */
55 	} u;
56 };
57 
58 enum switchdev_obj_id {
59 	SWITCHDEV_OBJ_ID_UNDEFINED,
60 	SWITCHDEV_OBJ_ID_PORT_VLAN,
61 	SWITCHDEV_OBJ_ID_PORT_MDB,
62 	SWITCHDEV_OBJ_ID_HOST_MDB,
63 	SWITCHDEV_OBJ_ID_MRP,
64 	SWITCHDEV_OBJ_ID_RING_TEST_MRP,
65 	SWITCHDEV_OBJ_ID_RING_ROLE_MRP,
66 	SWITCHDEV_OBJ_ID_RING_STATE_MRP,
67 	SWITCHDEV_OBJ_ID_IN_TEST_MRP,
68 	SWITCHDEV_OBJ_ID_IN_ROLE_MRP,
69 	SWITCHDEV_OBJ_ID_IN_STATE_MRP,
70 };
71 
72 struct switchdev_obj {
73 	struct list_head list;
74 	struct net_device *orig_dev;
75 	enum switchdev_obj_id id;
76 	u32 flags;
77 	void *complete_priv;
78 	void (*complete)(struct net_device *dev, int err, void *priv);
79 };
80 
81 /* SWITCHDEV_OBJ_ID_PORT_VLAN */
82 struct switchdev_obj_port_vlan {
83 	struct switchdev_obj obj;
84 	u16 flags;
85 	u16 vid;
86 	/* If set, the notifier signifies a change of one of the following
87 	 * flags for a VLAN that already exists:
88 	 * - BRIDGE_VLAN_INFO_PVID
89 	 * - BRIDGE_VLAN_INFO_UNTAGGED
90 	 * Entries with BRIDGE_VLAN_INFO_BRENTRY unset are not notified at all.
91 	 */
92 	bool changed;
93 };
94 
95 #define SWITCHDEV_OBJ_PORT_VLAN(OBJ) \
96 	container_of((OBJ), struct switchdev_obj_port_vlan, obj)
97 
98 /* SWITCHDEV_OBJ_ID_PORT_MDB */
99 struct switchdev_obj_port_mdb {
100 	struct switchdev_obj obj;
101 	unsigned char addr[ETH_ALEN];
102 	u16 vid;
103 };
104 
105 #define SWITCHDEV_OBJ_PORT_MDB(OBJ) \
106 	container_of((OBJ), struct switchdev_obj_port_mdb, obj)
107 
108 
109 /* SWITCHDEV_OBJ_ID_MRP */
110 struct switchdev_obj_mrp {
111 	struct switchdev_obj obj;
112 	struct net_device *p_port;
113 	struct net_device *s_port;
114 	u32 ring_id;
115 	u16 prio;
116 };
117 
118 #define SWITCHDEV_OBJ_MRP(OBJ) \
119 	container_of((OBJ), struct switchdev_obj_mrp, obj)
120 
121 /* SWITCHDEV_OBJ_ID_RING_TEST_MRP */
122 struct switchdev_obj_ring_test_mrp {
123 	struct switchdev_obj obj;
124 	/* The value is in us and a value of 0 represents to stop */
125 	u32 interval;
126 	u8 max_miss;
127 	u32 ring_id;
128 	u32 period;
129 	bool monitor;
130 };
131 
132 #define SWITCHDEV_OBJ_RING_TEST_MRP(OBJ) \
133 	container_of((OBJ), struct switchdev_obj_ring_test_mrp, obj)
134 
135 /* SWICHDEV_OBJ_ID_RING_ROLE_MRP */
136 struct switchdev_obj_ring_role_mrp {
137 	struct switchdev_obj obj;
138 	u8 ring_role;
139 	u32 ring_id;
140 	u8 sw_backup;
141 };
142 
143 #define SWITCHDEV_OBJ_RING_ROLE_MRP(OBJ) \
144 	container_of((OBJ), struct switchdev_obj_ring_role_mrp, obj)
145 
146 struct switchdev_obj_ring_state_mrp {
147 	struct switchdev_obj obj;
148 	u8 ring_state;
149 	u32 ring_id;
150 };
151 
152 #define SWITCHDEV_OBJ_RING_STATE_MRP(OBJ) \
153 	container_of((OBJ), struct switchdev_obj_ring_state_mrp, obj)
154 
155 /* SWITCHDEV_OBJ_ID_IN_TEST_MRP */
156 struct switchdev_obj_in_test_mrp {
157 	struct switchdev_obj obj;
158 	/* The value is in us and a value of 0 represents to stop */
159 	u32 interval;
160 	u32 in_id;
161 	u32 period;
162 	u8 max_miss;
163 };
164 
165 #define SWITCHDEV_OBJ_IN_TEST_MRP(OBJ) \
166 	container_of((OBJ), struct switchdev_obj_in_test_mrp, obj)
167 
168 /* SWICHDEV_OBJ_ID_IN_ROLE_MRP */
169 struct switchdev_obj_in_role_mrp {
170 	struct switchdev_obj obj;
171 	struct net_device *i_port;
172 	u32 ring_id;
173 	u16 in_id;
174 	u8 in_role;
175 	u8 sw_backup;
176 };
177 
178 #define SWITCHDEV_OBJ_IN_ROLE_MRP(OBJ) \
179 	container_of((OBJ), struct switchdev_obj_in_role_mrp, obj)
180 
181 struct switchdev_obj_in_state_mrp {
182 	struct switchdev_obj obj;
183 	u32 in_id;
184 	u8 in_state;
185 };
186 
187 #define SWITCHDEV_OBJ_IN_STATE_MRP(OBJ) \
188 	container_of((OBJ), struct switchdev_obj_in_state_mrp, obj)
189 
190 typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
191 
192 struct switchdev_brport {
193 	struct net_device *dev;
194 	const void *ctx;
195 	struct notifier_block *atomic_nb;
196 	struct notifier_block *blocking_nb;
197 	bool tx_fwd_offload;
198 };
199 
200 enum switchdev_notifier_type {
201 	SWITCHDEV_FDB_ADD_TO_BRIDGE = 1,
202 	SWITCHDEV_FDB_DEL_TO_BRIDGE,
203 	SWITCHDEV_FDB_ADD_TO_DEVICE,
204 	SWITCHDEV_FDB_DEL_TO_DEVICE,
205 	SWITCHDEV_FDB_OFFLOADED,
206 	SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
207 
208 	SWITCHDEV_PORT_OBJ_ADD, /* Blocking. */
209 	SWITCHDEV_PORT_OBJ_DEL, /* Blocking. */
210 	SWITCHDEV_PORT_ATTR_SET, /* May be blocking . */
211 
212 	SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE,
213 	SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE,
214 	SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
215 	SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE,
216 	SWITCHDEV_VXLAN_FDB_OFFLOADED,
217 
218 	SWITCHDEV_BRPORT_OFFLOADED,
219 	SWITCHDEV_BRPORT_UNOFFLOADED,
220 };
221 
222 struct switchdev_notifier_info {
223 	struct net_device *dev;
224 	struct netlink_ext_ack *extack;
225 	const void *ctx;
226 };
227 
228 struct switchdev_notifier_fdb_info {
229 	struct switchdev_notifier_info info; /* must be first */
230 	const unsigned char *addr;
231 	u16 vid;
232 	u8 added_by_user:1,
233 	   is_local:1,
234 	   offloaded:1;
235 };
236 
237 struct switchdev_notifier_port_obj_info {
238 	struct switchdev_notifier_info info; /* must be first */
239 	const struct switchdev_obj *obj;
240 	bool handled;
241 };
242 
243 struct switchdev_notifier_port_attr_info {
244 	struct switchdev_notifier_info info; /* must be first */
245 	const struct switchdev_attr *attr;
246 	bool handled;
247 };
248 
249 struct switchdev_notifier_brport_info {
250 	struct switchdev_notifier_info info; /* must be first */
251 	const struct switchdev_brport brport;
252 };
253 
254 static inline struct net_device *
255 switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
256 {
257 	return info->dev;
258 }
259 
260 static inline struct netlink_ext_ack *
261 switchdev_notifier_info_to_extack(const struct switchdev_notifier_info *info)
262 {
263 	return info->extack;
264 }
265 
266 static inline bool
267 switchdev_fdb_is_dynamically_learned(const struct switchdev_notifier_fdb_info *fdb_info)
268 {
269 	return !fdb_info->added_by_user && !fdb_info->is_local;
270 }
271 
272 #ifdef CONFIG_NET_SWITCHDEV
273 
274 int switchdev_bridge_port_offload(struct net_device *brport_dev,
275 				  struct net_device *dev, const void *ctx,
276 				  struct notifier_block *atomic_nb,
277 				  struct notifier_block *blocking_nb,
278 				  bool tx_fwd_offload,
279 				  struct netlink_ext_ack *extack);
280 void switchdev_bridge_port_unoffload(struct net_device *brport_dev,
281 				     const void *ctx,
282 				     struct notifier_block *atomic_nb,
283 				     struct notifier_block *blocking_nb);
284 
285 void switchdev_deferred_process(void);
286 int switchdev_port_attr_set(struct net_device *dev,
287 			    const struct switchdev_attr *attr,
288 			    struct netlink_ext_ack *extack);
289 int switchdev_port_obj_add(struct net_device *dev,
290 			   const struct switchdev_obj *obj,
291 			   struct netlink_ext_ack *extack);
292 int switchdev_port_obj_del(struct net_device *dev,
293 			   const struct switchdev_obj *obj);
294 
295 int register_switchdev_notifier(struct notifier_block *nb);
296 int unregister_switchdev_notifier(struct notifier_block *nb);
297 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
298 			     struct switchdev_notifier_info *info,
299 			     struct netlink_ext_ack *extack);
300 
301 int register_switchdev_blocking_notifier(struct notifier_block *nb);
302 int unregister_switchdev_blocking_notifier(struct notifier_block *nb);
303 int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
304 				      struct switchdev_notifier_info *info,
305 				      struct netlink_ext_ack *extack);
306 
307 void switchdev_port_fwd_mark_set(struct net_device *dev,
308 				 struct net_device *group_dev,
309 				 bool joining);
310 
311 int switchdev_handle_fdb_event_to_device(struct net_device *dev, unsigned long event,
312 		const struct switchdev_notifier_fdb_info *fdb_info,
313 		bool (*check_cb)(const struct net_device *dev),
314 		bool (*foreign_dev_check_cb)(const struct net_device *dev,
315 					     const struct net_device *foreign_dev),
316 		int (*mod_cb)(struct net_device *dev, struct net_device *orig_dev,
317 			      unsigned long event, const void *ctx,
318 			      const struct switchdev_notifier_fdb_info *fdb_info));
319 
320 int switchdev_handle_port_obj_add(struct net_device *dev,
321 			struct switchdev_notifier_port_obj_info *port_obj_info,
322 			bool (*check_cb)(const struct net_device *dev),
323 			int (*add_cb)(struct net_device *dev, const void *ctx,
324 				      const struct switchdev_obj *obj,
325 				      struct netlink_ext_ack *extack));
326 int switchdev_handle_port_obj_add_foreign(struct net_device *dev,
327 			struct switchdev_notifier_port_obj_info *port_obj_info,
328 			bool (*check_cb)(const struct net_device *dev),
329 			bool (*foreign_dev_check_cb)(const struct net_device *dev,
330 						     const struct net_device *foreign_dev),
331 			int (*add_cb)(struct net_device *dev, const void *ctx,
332 				      const struct switchdev_obj *obj,
333 				      struct netlink_ext_ack *extack));
334 int switchdev_handle_port_obj_del(struct net_device *dev,
335 			struct switchdev_notifier_port_obj_info *port_obj_info,
336 			bool (*check_cb)(const struct net_device *dev),
337 			int (*del_cb)(struct net_device *dev, const void *ctx,
338 				      const struct switchdev_obj *obj));
339 int switchdev_handle_port_obj_del_foreign(struct net_device *dev,
340 			struct switchdev_notifier_port_obj_info *port_obj_info,
341 			bool (*check_cb)(const struct net_device *dev),
342 			bool (*foreign_dev_check_cb)(const struct net_device *dev,
343 						     const struct net_device *foreign_dev),
344 			int (*del_cb)(struct net_device *dev, const void *ctx,
345 				      const struct switchdev_obj *obj));
346 
347 int switchdev_handle_port_attr_set(struct net_device *dev,
348 			struct switchdev_notifier_port_attr_info *port_attr_info,
349 			bool (*check_cb)(const struct net_device *dev),
350 			int (*set_cb)(struct net_device *dev, const void *ctx,
351 				      const struct switchdev_attr *attr,
352 				      struct netlink_ext_ack *extack));
353 #else
354 
355 static inline int
356 switchdev_bridge_port_offload(struct net_device *brport_dev,
357 			      struct net_device *dev, const void *ctx,
358 			      struct notifier_block *atomic_nb,
359 			      struct notifier_block *blocking_nb,
360 			      bool tx_fwd_offload,
361 			      struct netlink_ext_ack *extack)
362 {
363 	return -EOPNOTSUPP;
364 }
365 
366 static inline void
367 switchdev_bridge_port_unoffload(struct net_device *brport_dev,
368 				const void *ctx,
369 				struct notifier_block *atomic_nb,
370 				struct notifier_block *blocking_nb)
371 {
372 }
373 
374 static inline void switchdev_deferred_process(void)
375 {
376 }
377 
378 static inline int switchdev_port_attr_set(struct net_device *dev,
379 					  const struct switchdev_attr *attr,
380 					  struct netlink_ext_ack *extack)
381 {
382 	return -EOPNOTSUPP;
383 }
384 
385 static inline int switchdev_port_obj_add(struct net_device *dev,
386 					 const struct switchdev_obj *obj,
387 					 struct netlink_ext_ack *extack)
388 {
389 	return -EOPNOTSUPP;
390 }
391 
392 static inline int switchdev_port_obj_del(struct net_device *dev,
393 					 const struct switchdev_obj *obj)
394 {
395 	return -EOPNOTSUPP;
396 }
397 
398 static inline int register_switchdev_notifier(struct notifier_block *nb)
399 {
400 	return 0;
401 }
402 
403 static inline int unregister_switchdev_notifier(struct notifier_block *nb)
404 {
405 	return 0;
406 }
407 
408 static inline int call_switchdev_notifiers(unsigned long val,
409 					   struct net_device *dev,
410 					   struct switchdev_notifier_info *info,
411 					   struct netlink_ext_ack *extack)
412 {
413 	return NOTIFY_DONE;
414 }
415 
416 static inline int
417 register_switchdev_blocking_notifier(struct notifier_block *nb)
418 {
419 	return 0;
420 }
421 
422 static inline int
423 unregister_switchdev_blocking_notifier(struct notifier_block *nb)
424 {
425 	return 0;
426 }
427 
428 static inline int
429 call_switchdev_blocking_notifiers(unsigned long val,
430 				  struct net_device *dev,
431 				  struct switchdev_notifier_info *info,
432 				  struct netlink_ext_ack *extack)
433 {
434 	return NOTIFY_DONE;
435 }
436 
437 static inline int
438 switchdev_handle_fdb_event_to_device(struct net_device *dev, unsigned long event,
439 		const struct switchdev_notifier_fdb_info *fdb_info,
440 		bool (*check_cb)(const struct net_device *dev),
441 		bool (*foreign_dev_check_cb)(const struct net_device *dev,
442 					     const struct net_device *foreign_dev),
443 		int (*mod_cb)(struct net_device *dev, struct net_device *orig_dev,
444 			      unsigned long event, const void *ctx,
445 			      const struct switchdev_notifier_fdb_info *fdb_info))
446 {
447 	return 0;
448 }
449 
450 static inline int
451 switchdev_handle_port_obj_add(struct net_device *dev,
452 			struct switchdev_notifier_port_obj_info *port_obj_info,
453 			bool (*check_cb)(const struct net_device *dev),
454 			int (*add_cb)(struct net_device *dev, const void *ctx,
455 				      const struct switchdev_obj *obj,
456 				      struct netlink_ext_ack *extack))
457 {
458 	return 0;
459 }
460 
461 static inline int switchdev_handle_port_obj_add_foreign(struct net_device *dev,
462 			struct switchdev_notifier_port_obj_info *port_obj_info,
463 			bool (*check_cb)(const struct net_device *dev),
464 			bool (*foreign_dev_check_cb)(const struct net_device *dev,
465 						     const struct net_device *foreign_dev),
466 			int (*add_cb)(struct net_device *dev, const void *ctx,
467 				      const struct switchdev_obj *obj,
468 				      struct netlink_ext_ack *extack))
469 {
470 	return 0;
471 }
472 
473 static inline int
474 switchdev_handle_port_obj_del(struct net_device *dev,
475 			struct switchdev_notifier_port_obj_info *port_obj_info,
476 			bool (*check_cb)(const struct net_device *dev),
477 			int (*del_cb)(struct net_device *dev, const void *ctx,
478 				      const struct switchdev_obj *obj))
479 {
480 	return 0;
481 }
482 
483 static inline int
484 switchdev_handle_port_obj_del_foreign(struct net_device *dev,
485 			struct switchdev_notifier_port_obj_info *port_obj_info,
486 			bool (*check_cb)(const struct net_device *dev),
487 			bool (*foreign_dev_check_cb)(const struct net_device *dev,
488 						     const struct net_device *foreign_dev),
489 			int (*del_cb)(struct net_device *dev, const void *ctx,
490 				      const struct switchdev_obj *obj))
491 {
492 	return 0;
493 }
494 
495 static inline int
496 switchdev_handle_port_attr_set(struct net_device *dev,
497 			struct switchdev_notifier_port_attr_info *port_attr_info,
498 			bool (*check_cb)(const struct net_device *dev),
499 			int (*set_cb)(struct net_device *dev, const void *ctx,
500 				      const struct switchdev_attr *attr,
501 				      struct netlink_ext_ack *extack))
502 {
503 	return 0;
504 }
505 #endif
506 
507 #endif /* _LINUX_SWITCHDEV_H_ */
508