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