xref: /linux-6.15/include/net/switchdev.h (revision bae33f2b)
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 struct switchdev_trans {
20 	bool ph_prepare;
21 };
22 
23 static inline bool switchdev_trans_ph_prepare(struct switchdev_trans *trans)
24 {
25 	return trans && trans->ph_prepare;
26 }
27 
28 static inline bool switchdev_trans_ph_commit(struct switchdev_trans *trans)
29 {
30 	return trans && !trans->ph_prepare;
31 }
32 
33 enum switchdev_attr_id {
34 	SWITCHDEV_ATTR_ID_UNDEFINED,
35 	SWITCHDEV_ATTR_ID_PORT_STP_STATE,
36 	SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
37 	SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS,
38 	SWITCHDEV_ATTR_ID_PORT_MROUTER,
39 	SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
40 	SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
41 	SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL,
42 	SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
43 	SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
44 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
45 	SWITCHDEV_ATTR_ID_MRP_PORT_STATE,
46 	SWITCHDEV_ATTR_ID_MRP_PORT_ROLE,
47 #endif
48 };
49 
50 struct switchdev_attr {
51 	struct net_device *orig_dev;
52 	enum switchdev_attr_id id;
53 	u32 flags;
54 	void *complete_priv;
55 	void (*complete)(struct net_device *dev, int err, void *priv);
56 	union {
57 		u8 stp_state;				/* PORT_STP_STATE */
58 		unsigned long brport_flags;		/* PORT_{PRE}_BRIDGE_FLAGS */
59 		bool mrouter;				/* PORT_MROUTER */
60 		clock_t ageing_time;			/* BRIDGE_AGEING_TIME */
61 		bool vlan_filtering;			/* BRIDGE_VLAN_FILTERING */
62 		u16 vlan_protocol;			/* BRIDGE_VLAN_PROTOCOL */
63 		bool mc_disabled;			/* MC_DISABLED */
64 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
65 		u8 mrp_port_state;			/* MRP_PORT_STATE */
66 		u8 mrp_port_role;			/* MRP_PORT_ROLE */
67 #endif
68 	} u;
69 };
70 
71 enum switchdev_obj_id {
72 	SWITCHDEV_OBJ_ID_UNDEFINED,
73 	SWITCHDEV_OBJ_ID_PORT_VLAN,
74 	SWITCHDEV_OBJ_ID_PORT_MDB,
75 	SWITCHDEV_OBJ_ID_HOST_MDB,
76 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
77 	SWITCHDEV_OBJ_ID_MRP,
78 	SWITCHDEV_OBJ_ID_RING_TEST_MRP,
79 	SWITCHDEV_OBJ_ID_RING_ROLE_MRP,
80 	SWITCHDEV_OBJ_ID_RING_STATE_MRP,
81 	SWITCHDEV_OBJ_ID_IN_TEST_MRP,
82 	SWITCHDEV_OBJ_ID_IN_ROLE_MRP,
83 	SWITCHDEV_OBJ_ID_IN_STATE_MRP,
84 
85 #endif
86 };
87 
88 struct switchdev_obj {
89 	struct net_device *orig_dev;
90 	enum switchdev_obj_id id;
91 	u32 flags;
92 	void *complete_priv;
93 	void (*complete)(struct net_device *dev, int err, void *priv);
94 };
95 
96 /* SWITCHDEV_OBJ_ID_PORT_VLAN */
97 struct switchdev_obj_port_vlan {
98 	struct switchdev_obj obj;
99 	u16 flags;
100 	u16 vid;
101 };
102 
103 #define SWITCHDEV_OBJ_PORT_VLAN(OBJ) \
104 	container_of((OBJ), struct switchdev_obj_port_vlan, obj)
105 
106 /* SWITCHDEV_OBJ_ID_PORT_MDB */
107 struct switchdev_obj_port_mdb {
108 	struct switchdev_obj obj;
109 	unsigned char addr[ETH_ALEN];
110 	u16 vid;
111 };
112 
113 #define SWITCHDEV_OBJ_PORT_MDB(OBJ) \
114 	container_of((OBJ), struct switchdev_obj_port_mdb, obj)
115 
116 
117 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
118 /* SWITCHDEV_OBJ_ID_MRP */
119 struct switchdev_obj_mrp {
120 	struct switchdev_obj obj;
121 	struct net_device *p_port;
122 	struct net_device *s_port;
123 	u32 ring_id;
124 	u16 prio;
125 };
126 
127 #define SWITCHDEV_OBJ_MRP(OBJ) \
128 	container_of((OBJ), struct switchdev_obj_mrp, obj)
129 
130 /* SWITCHDEV_OBJ_ID_RING_TEST_MRP */
131 struct switchdev_obj_ring_test_mrp {
132 	struct switchdev_obj obj;
133 	/* The value is in us and a value of 0 represents to stop */
134 	u32 interval;
135 	u8 max_miss;
136 	u32 ring_id;
137 	u32 period;
138 	bool monitor;
139 };
140 
141 #define SWITCHDEV_OBJ_RING_TEST_MRP(OBJ) \
142 	container_of((OBJ), struct switchdev_obj_ring_test_mrp, obj)
143 
144 /* SWICHDEV_OBJ_ID_RING_ROLE_MRP */
145 struct switchdev_obj_ring_role_mrp {
146 	struct switchdev_obj obj;
147 	u8 ring_role;
148 	u32 ring_id;
149 };
150 
151 #define SWITCHDEV_OBJ_RING_ROLE_MRP(OBJ) \
152 	container_of((OBJ), struct switchdev_obj_ring_role_mrp, obj)
153 
154 struct switchdev_obj_ring_state_mrp {
155 	struct switchdev_obj obj;
156 	u8 ring_state;
157 	u32 ring_id;
158 };
159 
160 #define SWITCHDEV_OBJ_RING_STATE_MRP(OBJ) \
161 	container_of((OBJ), struct switchdev_obj_ring_state_mrp, obj)
162 
163 /* SWITCHDEV_OBJ_ID_IN_TEST_MRP */
164 struct switchdev_obj_in_test_mrp {
165 	struct switchdev_obj obj;
166 	/* The value is in us and a value of 0 represents to stop */
167 	u32 interval;
168 	u32 in_id;
169 	u32 period;
170 	u8 max_miss;
171 };
172 
173 #define SWITCHDEV_OBJ_IN_TEST_MRP(OBJ) \
174 	container_of((OBJ), struct switchdev_obj_in_test_mrp, obj)
175 
176 /* SWICHDEV_OBJ_ID_IN_ROLE_MRP */
177 struct switchdev_obj_in_role_mrp {
178 	struct switchdev_obj obj;
179 	struct net_device *i_port;
180 	u32 ring_id;
181 	u16 in_id;
182 	u8 in_role;
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 #endif
198 
199 typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
200 
201 enum switchdev_notifier_type {
202 	SWITCHDEV_FDB_ADD_TO_BRIDGE = 1,
203 	SWITCHDEV_FDB_DEL_TO_BRIDGE,
204 	SWITCHDEV_FDB_ADD_TO_DEVICE,
205 	SWITCHDEV_FDB_DEL_TO_DEVICE,
206 	SWITCHDEV_FDB_OFFLOADED,
207 	SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
208 
209 	SWITCHDEV_PORT_OBJ_ADD, /* Blocking. */
210 	SWITCHDEV_PORT_OBJ_DEL, /* Blocking. */
211 	SWITCHDEV_PORT_ATTR_SET, /* May be blocking . */
212 
213 	SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE,
214 	SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE,
215 	SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
216 	SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE,
217 	SWITCHDEV_VXLAN_FDB_OFFLOADED,
218 };
219 
220 struct switchdev_notifier_info {
221 	struct net_device *dev;
222 	struct netlink_ext_ack *extack;
223 };
224 
225 struct switchdev_notifier_fdb_info {
226 	struct switchdev_notifier_info info; /* must be first */
227 	const unsigned char *addr;
228 	u16 vid;
229 	u8 added_by_user:1,
230 	   offloaded:1;
231 };
232 
233 struct switchdev_notifier_port_obj_info {
234 	struct switchdev_notifier_info info; /* must be first */
235 	const struct switchdev_obj *obj;
236 	bool handled;
237 };
238 
239 struct switchdev_notifier_port_attr_info {
240 	struct switchdev_notifier_info info; /* must be first */
241 	const struct switchdev_attr *attr;
242 	bool handled;
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 #ifdef CONFIG_NET_SWITCHDEV
258 
259 void switchdev_deferred_process(void);
260 int switchdev_port_attr_set(struct net_device *dev,
261 			    const struct switchdev_attr *attr);
262 int switchdev_port_obj_add(struct net_device *dev,
263 			   const struct switchdev_obj *obj,
264 			   struct netlink_ext_ack *extack);
265 int switchdev_port_obj_del(struct net_device *dev,
266 			   const struct switchdev_obj *obj);
267 
268 int register_switchdev_notifier(struct notifier_block *nb);
269 int unregister_switchdev_notifier(struct notifier_block *nb);
270 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
271 			     struct switchdev_notifier_info *info,
272 			     struct netlink_ext_ack *extack);
273 
274 int register_switchdev_blocking_notifier(struct notifier_block *nb);
275 int unregister_switchdev_blocking_notifier(struct notifier_block *nb);
276 int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
277 				      struct switchdev_notifier_info *info,
278 				      struct netlink_ext_ack *extack);
279 
280 void switchdev_port_fwd_mark_set(struct net_device *dev,
281 				 struct net_device *group_dev,
282 				 bool joining);
283 
284 int switchdev_handle_port_obj_add(struct net_device *dev,
285 			struct switchdev_notifier_port_obj_info *port_obj_info,
286 			bool (*check_cb)(const struct net_device *dev),
287 			int (*add_cb)(struct net_device *dev,
288 				      const struct switchdev_obj *obj,
289 				      struct netlink_ext_ack *extack));
290 int switchdev_handle_port_obj_del(struct net_device *dev,
291 			struct switchdev_notifier_port_obj_info *port_obj_info,
292 			bool (*check_cb)(const struct net_device *dev),
293 			int (*del_cb)(struct net_device *dev,
294 				      const struct switchdev_obj *obj));
295 
296 int switchdev_handle_port_attr_set(struct net_device *dev,
297 			struct switchdev_notifier_port_attr_info *port_attr_info,
298 			bool (*check_cb)(const struct net_device *dev),
299 			int (*set_cb)(struct net_device *dev,
300 				      const struct switchdev_attr *attr));
301 #else
302 
303 static inline void switchdev_deferred_process(void)
304 {
305 }
306 
307 static inline int switchdev_port_attr_set(struct net_device *dev,
308 					  const struct switchdev_attr *attr)
309 {
310 	return -EOPNOTSUPP;
311 }
312 
313 static inline int switchdev_port_obj_add(struct net_device *dev,
314 					 const struct switchdev_obj *obj,
315 					 struct netlink_ext_ack *extack)
316 {
317 	return -EOPNOTSUPP;
318 }
319 
320 static inline int switchdev_port_obj_del(struct net_device *dev,
321 					 const struct switchdev_obj *obj)
322 {
323 	return -EOPNOTSUPP;
324 }
325 
326 static inline int register_switchdev_notifier(struct notifier_block *nb)
327 {
328 	return 0;
329 }
330 
331 static inline int unregister_switchdev_notifier(struct notifier_block *nb)
332 {
333 	return 0;
334 }
335 
336 static inline int call_switchdev_notifiers(unsigned long val,
337 					   struct net_device *dev,
338 					   struct switchdev_notifier_info *info,
339 					   struct netlink_ext_ack *extack)
340 {
341 	return NOTIFY_DONE;
342 }
343 
344 static inline int
345 register_switchdev_blocking_notifier(struct notifier_block *nb)
346 {
347 	return 0;
348 }
349 
350 static inline int
351 unregister_switchdev_blocking_notifier(struct notifier_block *nb)
352 {
353 	return 0;
354 }
355 
356 static inline int
357 call_switchdev_blocking_notifiers(unsigned long val,
358 				  struct net_device *dev,
359 				  struct switchdev_notifier_info *info,
360 				  struct netlink_ext_ack *extack)
361 {
362 	return NOTIFY_DONE;
363 }
364 
365 static inline int
366 switchdev_handle_port_obj_add(struct net_device *dev,
367 			struct switchdev_notifier_port_obj_info *port_obj_info,
368 			bool (*check_cb)(const struct net_device *dev),
369 			int (*add_cb)(struct net_device *dev,
370 				      const struct switchdev_obj *obj,
371 				      struct netlink_ext_ack *extack))
372 {
373 	return 0;
374 }
375 
376 static inline int
377 switchdev_handle_port_obj_del(struct net_device *dev,
378 			struct switchdev_notifier_port_obj_info *port_obj_info,
379 			bool (*check_cb)(const struct net_device *dev),
380 			int (*del_cb)(struct net_device *dev,
381 				      const struct switchdev_obj *obj))
382 {
383 	return 0;
384 }
385 
386 static inline int
387 switchdev_handle_port_attr_set(struct net_device *dev,
388 			struct switchdev_notifier_port_attr_info *port_attr_info,
389 			bool (*check_cb)(const struct net_device *dev),
390 			int (*set_cb)(struct net_device *dev,
391 				      const struct switchdev_attr *attr))
392 {
393 	return 0;
394 }
395 #endif
396 
397 #endif /* _LINUX_SWITCHDEV_H_ */
398