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