xref: /linux/include/net/switchdev.h (revision dcbdf135)
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_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 			    struct netlink_ext_ack *extack);
252 int switchdev_port_obj_add(struct net_device *dev,
253 			   const struct switchdev_obj *obj,
254 			   struct netlink_ext_ack *extack);
255 int switchdev_port_obj_del(struct net_device *dev,
256 			   const struct switchdev_obj *obj);
257 
258 int register_switchdev_notifier(struct notifier_block *nb);
259 int unregister_switchdev_notifier(struct notifier_block *nb);
260 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
261 			     struct switchdev_notifier_info *info,
262 			     struct netlink_ext_ack *extack);
263 
264 int register_switchdev_blocking_notifier(struct notifier_block *nb);
265 int unregister_switchdev_blocking_notifier(struct notifier_block *nb);
266 int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
267 				      struct switchdev_notifier_info *info,
268 				      struct netlink_ext_ack *extack);
269 
270 void switchdev_port_fwd_mark_set(struct net_device *dev,
271 				 struct net_device *group_dev,
272 				 bool joining);
273 
274 int switchdev_handle_port_obj_add(struct net_device *dev,
275 			struct switchdev_notifier_port_obj_info *port_obj_info,
276 			bool (*check_cb)(const struct net_device *dev),
277 			int (*add_cb)(struct net_device *dev,
278 				      const struct switchdev_obj *obj,
279 				      struct netlink_ext_ack *extack));
280 int switchdev_handle_port_obj_del(struct net_device *dev,
281 			struct switchdev_notifier_port_obj_info *port_obj_info,
282 			bool (*check_cb)(const struct net_device *dev),
283 			int (*del_cb)(struct net_device *dev,
284 				      const struct switchdev_obj *obj));
285 
286 int switchdev_handle_port_attr_set(struct net_device *dev,
287 			struct switchdev_notifier_port_attr_info *port_attr_info,
288 			bool (*check_cb)(const struct net_device *dev),
289 			int (*set_cb)(struct net_device *dev,
290 				      const struct switchdev_attr *attr,
291 				      struct netlink_ext_ack *extack));
292 #else
293 
294 static inline void switchdev_deferred_process(void)
295 {
296 }
297 
298 static inline int switchdev_port_attr_set(struct net_device *dev,
299 					  const struct switchdev_attr *attr)
300 {
301 	return -EOPNOTSUPP;
302 }
303 
304 static inline int switchdev_port_obj_add(struct net_device *dev,
305 					 const struct switchdev_obj *obj,
306 					 struct netlink_ext_ack *extack)
307 {
308 	return -EOPNOTSUPP;
309 }
310 
311 static inline int switchdev_port_obj_del(struct net_device *dev,
312 					 const struct switchdev_obj *obj)
313 {
314 	return -EOPNOTSUPP;
315 }
316 
317 static inline int register_switchdev_notifier(struct notifier_block *nb)
318 {
319 	return 0;
320 }
321 
322 static inline int unregister_switchdev_notifier(struct notifier_block *nb)
323 {
324 	return 0;
325 }
326 
327 static inline int call_switchdev_notifiers(unsigned long val,
328 					   struct net_device *dev,
329 					   struct switchdev_notifier_info *info,
330 					   struct netlink_ext_ack *extack)
331 {
332 	return NOTIFY_DONE;
333 }
334 
335 static inline int
336 register_switchdev_blocking_notifier(struct notifier_block *nb)
337 {
338 	return 0;
339 }
340 
341 static inline int
342 unregister_switchdev_blocking_notifier(struct notifier_block *nb)
343 {
344 	return 0;
345 }
346 
347 static inline int
348 call_switchdev_blocking_notifiers(unsigned long val,
349 				  struct net_device *dev,
350 				  struct switchdev_notifier_info *info,
351 				  struct netlink_ext_ack *extack)
352 {
353 	return NOTIFY_DONE;
354 }
355 
356 static inline int
357 switchdev_handle_port_obj_add(struct net_device *dev,
358 			struct switchdev_notifier_port_obj_info *port_obj_info,
359 			bool (*check_cb)(const struct net_device *dev),
360 			int (*add_cb)(struct net_device *dev,
361 				      const struct switchdev_obj *obj,
362 				      struct netlink_ext_ack *extack))
363 {
364 	return 0;
365 }
366 
367 static inline int
368 switchdev_handle_port_obj_del(struct net_device *dev,
369 			struct switchdev_notifier_port_obj_info *port_obj_info,
370 			bool (*check_cb)(const struct net_device *dev),
371 			int (*del_cb)(struct net_device *dev,
372 				      const struct switchdev_obj *obj))
373 {
374 	return 0;
375 }
376 
377 static inline int
378 switchdev_handle_port_attr_set(struct net_device *dev,
379 			struct switchdev_notifier_port_attr_info *port_attr_info,
380 			bool (*check_cb)(const struct net_device *dev),
381 			int (*set_cb)(struct net_device *dev,
382 				      const struct switchdev_attr *attr,
383 				      struct netlink_ext_ack *extack))
384 {
385 	return 0;
386 }
387 #endif
388 
389 #endif /* _LINUX_SWITCHDEV_H_ */
390