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