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