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