xref: /openbmc/linux/include/net/switchdev.h (revision b4e18b29)
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 #else
286 
287 static inline void switchdev_deferred_process(void)
288 {
289 }
290 
291 static inline int switchdev_port_attr_set(struct net_device *dev,
292 					  const struct switchdev_attr *attr)
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 {
377 	return 0;
378 }
379 #endif
380 
381 #endif /* _LINUX_SWITCHDEV_H_ */
382