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