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