1 /* SPDX-License-Identifier: GPL-2.0-or-later */
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>
7 #ifndef _LINUX_SWITCHDEV_H_
8 #define _LINUX_SWITCHDEV_H_
10 #include <linux/netdevice.h>
11 #include <linux/notifier.h>
12 #include <linux/list.h>
13 #include <net/ip_fib.h>
15 #define SWITCHDEV_F_NO_RECURSE BIT(0)
16 #define SWITCHDEV_F_SKIP_EOPNOTSUPP BIT(1)
17 #define SWITCHDEV_F_DEFER BIT(2)
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,
35 struct switchdev_brport_flags {
40 struct switchdev_attr {
41 struct net_device *orig_dev;
42 enum switchdev_attr_id id;
45 void (*complete)(struct net_device *dev, int err, void *priv);
47 u8 stp_state; /* PORT_STP_STATE */
48 struct switchdev_brport_flags brport_flags; /* PORT_BRIDGE_FLAGS */
49 bool mrouter; /* PORT_MROUTER */
50 clock_t ageing_time; /* BRIDGE_AGEING_TIME */
51 bool vlan_filtering; /* BRIDGE_VLAN_FILTERING */
52 u16 vlan_protocol; /* BRIDGE_VLAN_PROTOCOL */
53 bool mc_disabled; /* MC_DISABLED */
54 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
55 u8 mrp_port_role; /* MRP_PORT_ROLE */
60 enum switchdev_obj_id {
61 SWITCHDEV_OBJ_ID_UNDEFINED,
62 SWITCHDEV_OBJ_ID_PORT_VLAN,
63 SWITCHDEV_OBJ_ID_PORT_MDB,
64 SWITCHDEV_OBJ_ID_HOST_MDB,
65 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
67 SWITCHDEV_OBJ_ID_RING_TEST_MRP,
68 SWITCHDEV_OBJ_ID_RING_ROLE_MRP,
69 SWITCHDEV_OBJ_ID_RING_STATE_MRP,
70 SWITCHDEV_OBJ_ID_IN_TEST_MRP,
71 SWITCHDEV_OBJ_ID_IN_ROLE_MRP,
72 SWITCHDEV_OBJ_ID_IN_STATE_MRP,
77 struct switchdev_obj {
78 struct net_device *orig_dev;
79 enum switchdev_obj_id id;
82 void (*complete)(struct net_device *dev, int err, void *priv);
85 /* SWITCHDEV_OBJ_ID_PORT_VLAN */
86 struct switchdev_obj_port_vlan {
87 struct switchdev_obj obj;
92 #define SWITCHDEV_OBJ_PORT_VLAN(OBJ) \
93 container_of((OBJ), struct switchdev_obj_port_vlan, obj)
95 /* SWITCHDEV_OBJ_ID_PORT_MDB */
96 struct switchdev_obj_port_mdb {
97 struct switchdev_obj obj;
98 unsigned char addr[ETH_ALEN];
102 #define SWITCHDEV_OBJ_PORT_MDB(OBJ) \
103 container_of((OBJ), struct switchdev_obj_port_mdb, obj)
106 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
107 /* SWITCHDEV_OBJ_ID_MRP */
108 struct switchdev_obj_mrp {
109 struct switchdev_obj obj;
110 struct net_device *p_port;
111 struct net_device *s_port;
116 #define SWITCHDEV_OBJ_MRP(OBJ) \
117 container_of((OBJ), struct switchdev_obj_mrp, obj)
119 /* SWITCHDEV_OBJ_ID_RING_TEST_MRP */
120 struct switchdev_obj_ring_test_mrp {
121 struct switchdev_obj obj;
122 /* The value is in us and a value of 0 represents to stop */
130 #define SWITCHDEV_OBJ_RING_TEST_MRP(OBJ) \
131 container_of((OBJ), struct switchdev_obj_ring_test_mrp, obj)
133 /* SWICHDEV_OBJ_ID_RING_ROLE_MRP */
134 struct switchdev_obj_ring_role_mrp {
135 struct switchdev_obj obj;
140 #define SWITCHDEV_OBJ_RING_ROLE_MRP(OBJ) \
141 container_of((OBJ), struct switchdev_obj_ring_role_mrp, obj)
143 struct switchdev_obj_ring_state_mrp {
144 struct switchdev_obj obj;
149 #define SWITCHDEV_OBJ_RING_STATE_MRP(OBJ) \
150 container_of((OBJ), struct switchdev_obj_ring_state_mrp, obj)
152 /* SWITCHDEV_OBJ_ID_IN_TEST_MRP */
153 struct switchdev_obj_in_test_mrp {
154 struct switchdev_obj obj;
155 /* The value is in us and a value of 0 represents to stop */
162 #define SWITCHDEV_OBJ_IN_TEST_MRP(OBJ) \
163 container_of((OBJ), struct switchdev_obj_in_test_mrp, obj)
165 /* SWICHDEV_OBJ_ID_IN_ROLE_MRP */
166 struct switchdev_obj_in_role_mrp {
167 struct switchdev_obj obj;
168 struct net_device *i_port;
174 #define SWITCHDEV_OBJ_IN_ROLE_MRP(OBJ) \
175 container_of((OBJ), struct switchdev_obj_in_role_mrp, obj)
177 struct switchdev_obj_in_state_mrp {
178 struct switchdev_obj obj;
183 #define SWITCHDEV_OBJ_IN_STATE_MRP(OBJ) \
184 container_of((OBJ), struct switchdev_obj_in_state_mrp, obj)
188 typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
190 enum switchdev_notifier_type {
191 SWITCHDEV_FDB_ADD_TO_BRIDGE = 1,
192 SWITCHDEV_FDB_DEL_TO_BRIDGE,
193 SWITCHDEV_FDB_ADD_TO_DEVICE,
194 SWITCHDEV_FDB_DEL_TO_DEVICE,
195 SWITCHDEV_FDB_OFFLOADED,
196 SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
198 SWITCHDEV_PORT_OBJ_ADD, /* Blocking. */
199 SWITCHDEV_PORT_OBJ_DEL, /* Blocking. */
200 SWITCHDEV_PORT_ATTR_SET, /* May be blocking . */
202 SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE,
203 SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE,
204 SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
205 SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE,
206 SWITCHDEV_VXLAN_FDB_OFFLOADED,
209 struct switchdev_notifier_info {
210 struct net_device *dev;
211 struct netlink_ext_ack *extack;
214 struct switchdev_notifier_fdb_info {
215 struct switchdev_notifier_info info; /* must be first */
216 const unsigned char *addr;
222 struct switchdev_notifier_port_obj_info {
223 struct switchdev_notifier_info info; /* must be first */
224 const struct switchdev_obj *obj;
228 struct switchdev_notifier_port_attr_info {
229 struct switchdev_notifier_info info; /* must be first */
230 const struct switchdev_attr *attr;
234 static inline struct net_device *
235 switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
240 static inline struct netlink_ext_ack *
241 switchdev_notifier_info_to_extack(const struct switchdev_notifier_info *info)
246 #ifdef CONFIG_NET_SWITCHDEV
248 void switchdev_deferred_process(void);
249 int switchdev_port_attr_set(struct net_device *dev,
250 const struct switchdev_attr *attr,
251 struct netlink_ext_ack *extack);
252 int switchdev_port_obj_add(struct net_device *dev,
253 const struct switchdev_obj *obj,
254 struct netlink_ext_ack *extack);
255 int switchdev_port_obj_del(struct net_device *dev,
256 const struct switchdev_obj *obj);
258 int register_switchdev_notifier(struct notifier_block *nb);
259 int unregister_switchdev_notifier(struct notifier_block *nb);
260 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
261 struct switchdev_notifier_info *info,
262 struct netlink_ext_ack *extack);
264 int register_switchdev_blocking_notifier(struct notifier_block *nb);
265 int unregister_switchdev_blocking_notifier(struct notifier_block *nb);
266 int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
267 struct switchdev_notifier_info *info,
268 struct netlink_ext_ack *extack);
270 void switchdev_port_fwd_mark_set(struct net_device *dev,
271 struct net_device *group_dev,
274 int switchdev_handle_port_obj_add(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 (*add_cb)(struct net_device *dev,
278 const struct switchdev_obj *obj,
279 struct netlink_ext_ack *extack));
280 int switchdev_handle_port_obj_del(struct net_device *dev,
281 struct switchdev_notifier_port_obj_info *port_obj_info,
282 bool (*check_cb)(const struct net_device *dev),
283 int (*del_cb)(struct net_device *dev,
284 const struct switchdev_obj *obj));
286 int switchdev_handle_port_attr_set(struct net_device *dev,
287 struct switchdev_notifier_port_attr_info *port_attr_info,
288 bool (*check_cb)(const struct net_device *dev),
289 int (*set_cb)(struct net_device *dev,
290 const struct switchdev_attr *attr,
291 struct netlink_ext_ack *extack));
294 static inline void switchdev_deferred_process(void)
298 static inline int switchdev_port_attr_set(struct net_device *dev,
299 const struct switchdev_attr *attr)
304 static inline int switchdev_port_obj_add(struct net_device *dev,
305 const struct switchdev_obj *obj,
306 struct netlink_ext_ack *extack)
311 static inline int switchdev_port_obj_del(struct net_device *dev,
312 const struct switchdev_obj *obj)
317 static inline int register_switchdev_notifier(struct notifier_block *nb)
322 static inline int unregister_switchdev_notifier(struct notifier_block *nb)
327 static inline int call_switchdev_notifiers(unsigned long val,
328 struct net_device *dev,
329 struct switchdev_notifier_info *info,
330 struct netlink_ext_ack *extack)
336 register_switchdev_blocking_notifier(struct notifier_block *nb)
342 unregister_switchdev_blocking_notifier(struct notifier_block *nb)
348 call_switchdev_blocking_notifiers(unsigned long val,
349 struct net_device *dev,
350 struct switchdev_notifier_info *info,
351 struct netlink_ext_ack *extack)
357 switchdev_handle_port_obj_add(struct net_device *dev,
358 struct switchdev_notifier_port_obj_info *port_obj_info,
359 bool (*check_cb)(const struct net_device *dev),
360 int (*add_cb)(struct net_device *dev,
361 const struct switchdev_obj *obj,
362 struct netlink_ext_ack *extack))
368 switchdev_handle_port_obj_del(struct net_device *dev,
369 struct switchdev_notifier_port_obj_info *port_obj_info,
370 bool (*check_cb)(const struct net_device *dev),
371 int (*del_cb)(struct net_device *dev,
372 const struct switchdev_obj *obj))
378 switchdev_handle_port_attr_set(struct net_device *dev,
379 struct switchdev_notifier_port_attr_info *port_attr_info,
380 bool (*check_cb)(const struct net_device *dev),
381 int (*set_cb)(struct net_device *dev,
382 const struct switchdev_attr *attr,
383 struct netlink_ext_ack *extack))
389 #endif /* _LINUX_SWITCHDEV_H_ */