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