xref: /linux/include/net/switchdev.h (revision 6c363eafc4d637ac4bd83d4a7dd06dd3cfbe7c5f)
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 	u8 sw_backup;
131 };
132 
133 #define SWITCHDEV_OBJ_RING_ROLE_MRP(OBJ) \
134 	container_of((OBJ), struct switchdev_obj_ring_role_mrp, obj)
135 
136 struct switchdev_obj_ring_state_mrp {
137 	struct switchdev_obj obj;
138 	u8 ring_state;
139 	u32 ring_id;
140 };
141 
142 #define SWITCHDEV_OBJ_RING_STATE_MRP(OBJ) \
143 	container_of((OBJ), struct switchdev_obj_ring_state_mrp, obj)
144 
145 /* SWITCHDEV_OBJ_ID_IN_TEST_MRP */
146 struct switchdev_obj_in_test_mrp {
147 	struct switchdev_obj obj;
148 	/* The value is in us and a value of 0 represents to stop */
149 	u32 interval;
150 	u32 in_id;
151 	u32 period;
152 	u8 max_miss;
153 };
154 
155 #define SWITCHDEV_OBJ_IN_TEST_MRP(OBJ) \
156 	container_of((OBJ), struct switchdev_obj_in_test_mrp, obj)
157 
158 /* SWICHDEV_OBJ_ID_IN_ROLE_MRP */
159 struct switchdev_obj_in_role_mrp {
160 	struct switchdev_obj obj;
161 	struct net_device *i_port;
162 	u32 ring_id;
163 	u16 in_id;
164 	u8 in_role;
165 	u8 sw_backup;
166 };
167 
168 #define SWITCHDEV_OBJ_IN_ROLE_MRP(OBJ) \
169 	container_of((OBJ), struct switchdev_obj_in_role_mrp, obj)
170 
171 struct switchdev_obj_in_state_mrp {
172 	struct switchdev_obj obj;
173 	u32 in_id;
174 	u8 in_state;
175 };
176 
177 #define SWITCHDEV_OBJ_IN_STATE_MRP(OBJ) \
178 	container_of((OBJ), struct switchdev_obj_in_state_mrp, obj)
179 
180 typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
181 
182 enum switchdev_notifier_type {
183 	SWITCHDEV_FDB_ADD_TO_BRIDGE = 1,
184 	SWITCHDEV_FDB_DEL_TO_BRIDGE,
185 	SWITCHDEV_FDB_ADD_TO_DEVICE,
186 	SWITCHDEV_FDB_DEL_TO_DEVICE,
187 	SWITCHDEV_FDB_OFFLOADED,
188 	SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
189 
190 	SWITCHDEV_PORT_OBJ_ADD, /* Blocking. */
191 	SWITCHDEV_PORT_OBJ_DEL, /* Blocking. */
192 	SWITCHDEV_PORT_ATTR_SET, /* May be blocking . */
193 
194 	SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE,
195 	SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE,
196 	SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
197 	SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE,
198 	SWITCHDEV_VXLAN_FDB_OFFLOADED,
199 };
200 
201 struct switchdev_notifier_info {
202 	struct net_device *dev;
203 	struct netlink_ext_ack *extack;
204 };
205 
206 struct switchdev_notifier_fdb_info {
207 	struct switchdev_notifier_info info; /* must be first */
208 	const unsigned char *addr;
209 	u16 vid;
210 	u8 added_by_user:1,
211 	   offloaded:1;
212 };
213 
214 struct switchdev_notifier_port_obj_info {
215 	struct switchdev_notifier_info info; /* must be first */
216 	const struct switchdev_obj *obj;
217 	bool handled;
218 };
219 
220 struct switchdev_notifier_port_attr_info {
221 	struct switchdev_notifier_info info; /* must be first */
222 	const struct switchdev_attr *attr;
223 	bool handled;
224 };
225 
226 static inline struct net_device *
227 switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
228 {
229 	return info->dev;
230 }
231 
232 static inline struct netlink_ext_ack *
233 switchdev_notifier_info_to_extack(const struct switchdev_notifier_info *info)
234 {
235 	return info->extack;
236 }
237 
238 #ifdef CONFIG_NET_SWITCHDEV
239 
240 void switchdev_deferred_process(void);
241 int switchdev_port_attr_set(struct net_device *dev,
242 			    const struct switchdev_attr *attr,
243 			    struct netlink_ext_ack *extack);
244 int switchdev_port_obj_add(struct net_device *dev,
245 			   const struct switchdev_obj *obj,
246 			   struct netlink_ext_ack *extack);
247 int switchdev_port_obj_del(struct net_device *dev,
248 			   const struct switchdev_obj *obj);
249 
250 int register_switchdev_notifier(struct notifier_block *nb);
251 int unregister_switchdev_notifier(struct notifier_block *nb);
252 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
253 			     struct switchdev_notifier_info *info,
254 			     struct netlink_ext_ack *extack);
255 
256 int register_switchdev_blocking_notifier(struct notifier_block *nb);
257 int unregister_switchdev_blocking_notifier(struct notifier_block *nb);
258 int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
259 				      struct switchdev_notifier_info *info,
260 				      struct netlink_ext_ack *extack);
261 
262 void switchdev_port_fwd_mark_set(struct net_device *dev,
263 				 struct net_device *group_dev,
264 				 bool joining);
265 
266 int switchdev_handle_port_obj_add(struct net_device *dev,
267 			struct switchdev_notifier_port_obj_info *port_obj_info,
268 			bool (*check_cb)(const struct net_device *dev),
269 			int (*add_cb)(struct net_device *dev,
270 				      const struct switchdev_obj *obj,
271 				      struct netlink_ext_ack *extack));
272 int switchdev_handle_port_obj_del(struct net_device *dev,
273 			struct switchdev_notifier_port_obj_info *port_obj_info,
274 			bool (*check_cb)(const struct net_device *dev),
275 			int (*del_cb)(struct net_device *dev,
276 				      const struct switchdev_obj *obj));
277 
278 int switchdev_handle_port_attr_set(struct net_device *dev,
279 			struct switchdev_notifier_port_attr_info *port_attr_info,
280 			bool (*check_cb)(const struct net_device *dev),
281 			int (*set_cb)(struct net_device *dev,
282 				      const struct switchdev_attr *attr,
283 				      struct netlink_ext_ack *extack));
284 #else
285 
286 static inline void switchdev_deferred_process(void)
287 {
288 }
289 
290 static inline int switchdev_port_attr_set(struct net_device *dev,
291 					  const struct switchdev_attr *attr,
292 					  struct netlink_ext_ack *extack)
293 {
294 	return -EOPNOTSUPP;
295 }
296 
297 static inline int switchdev_port_obj_add(struct net_device *dev,
298 					 const struct switchdev_obj *obj,
299 					 struct netlink_ext_ack *extack)
300 {
301 	return -EOPNOTSUPP;
302 }
303 
304 static inline int switchdev_port_obj_del(struct net_device *dev,
305 					 const struct switchdev_obj *obj)
306 {
307 	return -EOPNOTSUPP;
308 }
309 
310 static inline int register_switchdev_notifier(struct notifier_block *nb)
311 {
312 	return 0;
313 }
314 
315 static inline int unregister_switchdev_notifier(struct notifier_block *nb)
316 {
317 	return 0;
318 }
319 
320 static inline int call_switchdev_notifiers(unsigned long val,
321 					   struct net_device *dev,
322 					   struct switchdev_notifier_info *info,
323 					   struct netlink_ext_ack *extack)
324 {
325 	return NOTIFY_DONE;
326 }
327 
328 static inline int
329 register_switchdev_blocking_notifier(struct notifier_block *nb)
330 {
331 	return 0;
332 }
333 
334 static inline int
335 unregister_switchdev_blocking_notifier(struct notifier_block *nb)
336 {
337 	return 0;
338 }
339 
340 static inline int
341 call_switchdev_blocking_notifiers(unsigned long val,
342 				  struct net_device *dev,
343 				  struct switchdev_notifier_info *info,
344 				  struct netlink_ext_ack *extack)
345 {
346 	return NOTIFY_DONE;
347 }
348 
349 static inline int
350 switchdev_handle_port_obj_add(struct net_device *dev,
351 			struct switchdev_notifier_port_obj_info *port_obj_info,
352 			bool (*check_cb)(const struct net_device *dev),
353 			int (*add_cb)(struct net_device *dev,
354 				      const struct switchdev_obj *obj,
355 				      struct netlink_ext_ack *extack))
356 {
357 	return 0;
358 }
359 
360 static inline int
361 switchdev_handle_port_obj_del(struct net_device *dev,
362 			struct switchdev_notifier_port_obj_info *port_obj_info,
363 			bool (*check_cb)(const struct net_device *dev),
364 			int (*del_cb)(struct net_device *dev,
365 				      const struct switchdev_obj *obj))
366 {
367 	return 0;
368 }
369 
370 static inline int
371 switchdev_handle_port_attr_set(struct net_device *dev,
372 			struct switchdev_notifier_port_attr_info *port_attr_info,
373 			bool (*check_cb)(const struct net_device *dev),
374 			int (*set_cb)(struct net_device *dev,
375 				      const struct switchdev_attr *attr,
376 				      struct netlink_ext_ack *extack))
377 {
378 	return 0;
379 }
380 #endif
381 
382 #endif /* _LINUX_SWITCHDEV_H_ */
383