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