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