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 struct switchdev_trans
{
23 static inline bool switchdev_trans_ph_prepare(struct switchdev_trans
*trans
)
25 return trans
&& trans
->ph_prepare
;
28 static inline bool switchdev_trans_ph_commit(struct switchdev_trans
*trans
)
30 return trans
&& !trans
->ph_prepare
;
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_VLAN_PROTOCOL
,
42 SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED
,
43 SWITCHDEV_ATTR_ID_BRIDGE_MROUTER
,
44 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
45 SWITCHDEV_ATTR_ID_MRP_PORT_STATE
,
46 SWITCHDEV_ATTR_ID_MRP_PORT_ROLE
,
50 struct switchdev_attr
{
51 struct net_device
*orig_dev
;
52 enum switchdev_attr_id id
;
55 void (*complete
)(struct net_device
*dev
, int err
, void *priv
);
57 u8 stp_state
; /* PORT_STP_STATE */
58 unsigned long brport_flags
; /* PORT_{PRE}_BRIDGE_FLAGS */
59 bool mrouter
; /* PORT_MROUTER */
60 clock_t ageing_time
; /* BRIDGE_AGEING_TIME */
61 bool vlan_filtering
; /* BRIDGE_VLAN_FILTERING */
62 u16 vlan_protocol
; /* BRIDGE_VLAN_PROTOCOL */
63 bool mc_disabled
; /* MC_DISABLED */
64 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
65 u8 mrp_port_state
; /* MRP_PORT_STATE */
66 u8 mrp_port_role
; /* MRP_PORT_ROLE */
71 enum switchdev_obj_id
{
72 SWITCHDEV_OBJ_ID_UNDEFINED
,
73 SWITCHDEV_OBJ_ID_PORT_VLAN
,
74 SWITCHDEV_OBJ_ID_PORT_MDB
,
75 SWITCHDEV_OBJ_ID_HOST_MDB
,
76 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
78 SWITCHDEV_OBJ_ID_RING_TEST_MRP
,
79 SWITCHDEV_OBJ_ID_RING_ROLE_MRP
,
80 SWITCHDEV_OBJ_ID_RING_STATE_MRP
,
81 SWITCHDEV_OBJ_ID_IN_TEST_MRP
,
82 SWITCHDEV_OBJ_ID_IN_ROLE_MRP
,
83 SWITCHDEV_OBJ_ID_IN_STATE_MRP
,
88 struct switchdev_obj
{
89 struct net_device
*orig_dev
;
90 enum switchdev_obj_id id
;
93 void (*complete
)(struct net_device
*dev
, int err
, void *priv
);
96 /* SWITCHDEV_OBJ_ID_PORT_VLAN */
97 struct switchdev_obj_port_vlan
{
98 struct switchdev_obj obj
;
104 #define SWITCHDEV_OBJ_PORT_VLAN(OBJ) \
105 container_of((OBJ), struct switchdev_obj_port_vlan, obj)
107 /* SWITCHDEV_OBJ_ID_PORT_MDB */
108 struct switchdev_obj_port_mdb
{
109 struct switchdev_obj obj
;
110 unsigned char addr
[ETH_ALEN
];
114 #define SWITCHDEV_OBJ_PORT_MDB(OBJ) \
115 container_of((OBJ), struct switchdev_obj_port_mdb, obj)
118 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
119 /* SWITCHDEV_OBJ_ID_MRP */
120 struct switchdev_obj_mrp
{
121 struct switchdev_obj obj
;
122 struct net_device
*p_port
;
123 struct net_device
*s_port
;
128 #define SWITCHDEV_OBJ_MRP(OBJ) \
129 container_of((OBJ), struct switchdev_obj_mrp, obj)
131 /* SWITCHDEV_OBJ_ID_RING_TEST_MRP */
132 struct switchdev_obj_ring_test_mrp
{
133 struct switchdev_obj obj
;
134 /* The value is in us and a value of 0 represents to stop */
142 #define SWITCHDEV_OBJ_RING_TEST_MRP(OBJ) \
143 container_of((OBJ), struct switchdev_obj_ring_test_mrp, obj)
145 /* SWICHDEV_OBJ_ID_RING_ROLE_MRP */
146 struct switchdev_obj_ring_role_mrp
{
147 struct switchdev_obj obj
;
152 #define SWITCHDEV_OBJ_RING_ROLE_MRP(OBJ) \
153 container_of((OBJ), struct switchdev_obj_ring_role_mrp, obj)
155 struct switchdev_obj_ring_state_mrp
{
156 struct switchdev_obj obj
;
161 #define SWITCHDEV_OBJ_RING_STATE_MRP(OBJ) \
162 container_of((OBJ), struct switchdev_obj_ring_state_mrp, obj)
164 /* SWITCHDEV_OBJ_ID_IN_TEST_MRP */
165 struct switchdev_obj_in_test_mrp
{
166 struct switchdev_obj obj
;
167 /* The value is in us and a value of 0 represents to stop */
174 #define SWITCHDEV_OBJ_IN_TEST_MRP(OBJ) \
175 container_of((OBJ), struct switchdev_obj_in_test_mrp, obj)
177 /* SWICHDEV_OBJ_ID_IN_ROLE_MRP */
178 struct switchdev_obj_in_role_mrp
{
179 struct switchdev_obj obj
;
180 struct net_device
*i_port
;
186 #define SWITCHDEV_OBJ_IN_ROLE_MRP(OBJ) \
187 container_of((OBJ), struct switchdev_obj_in_role_mrp, obj)
189 struct switchdev_obj_in_state_mrp
{
190 struct switchdev_obj obj
;
195 #define SWITCHDEV_OBJ_IN_STATE_MRP(OBJ) \
196 container_of((OBJ), struct switchdev_obj_in_state_mrp, obj)
200 typedef int switchdev_obj_dump_cb_t(struct switchdev_obj
*obj
);
202 enum switchdev_notifier_type
{
203 SWITCHDEV_FDB_ADD_TO_BRIDGE
= 1,
204 SWITCHDEV_FDB_DEL_TO_BRIDGE
,
205 SWITCHDEV_FDB_ADD_TO_DEVICE
,
206 SWITCHDEV_FDB_DEL_TO_DEVICE
,
207 SWITCHDEV_FDB_OFFLOADED
,
208 SWITCHDEV_FDB_FLUSH_TO_BRIDGE
,
210 SWITCHDEV_PORT_OBJ_ADD
, /* Blocking. */
211 SWITCHDEV_PORT_OBJ_DEL
, /* Blocking. */
212 SWITCHDEV_PORT_ATTR_SET
, /* May be blocking . */
214 SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE
,
215 SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE
,
216 SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
,
217 SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE
,
218 SWITCHDEV_VXLAN_FDB_OFFLOADED
,
221 struct switchdev_notifier_info
{
222 struct net_device
*dev
;
223 struct netlink_ext_ack
*extack
;
226 struct switchdev_notifier_fdb_info
{
227 struct switchdev_notifier_info info
; /* must be first */
228 const unsigned char *addr
;
234 struct switchdev_notifier_port_obj_info
{
235 struct switchdev_notifier_info info
; /* must be first */
236 const struct switchdev_obj
*obj
;
237 struct switchdev_trans
*trans
;
241 struct switchdev_notifier_port_attr_info
{
242 struct switchdev_notifier_info info
; /* must be first */
243 const struct switchdev_attr
*attr
;
244 struct switchdev_trans
*trans
;
248 static inline struct net_device
*
249 switchdev_notifier_info_to_dev(const struct switchdev_notifier_info
*info
)
254 static inline struct netlink_ext_ack
*
255 switchdev_notifier_info_to_extack(const struct switchdev_notifier_info
*info
)
260 #ifdef CONFIG_NET_SWITCHDEV
262 void switchdev_deferred_process(void);
263 int switchdev_port_attr_set(struct net_device
*dev
,
264 const struct switchdev_attr
*attr
);
265 int switchdev_port_obj_add(struct net_device
*dev
,
266 const struct switchdev_obj
*obj
,
267 struct netlink_ext_ack
*extack
);
268 int switchdev_port_obj_del(struct net_device
*dev
,
269 const struct switchdev_obj
*obj
);
271 int register_switchdev_notifier(struct notifier_block
*nb
);
272 int unregister_switchdev_notifier(struct notifier_block
*nb
);
273 int call_switchdev_notifiers(unsigned long val
, struct net_device
*dev
,
274 struct switchdev_notifier_info
*info
,
275 struct netlink_ext_ack
*extack
);
277 int register_switchdev_blocking_notifier(struct notifier_block
*nb
);
278 int unregister_switchdev_blocking_notifier(struct notifier_block
*nb
);
279 int call_switchdev_blocking_notifiers(unsigned long val
, struct net_device
*dev
,
280 struct switchdev_notifier_info
*info
,
281 struct netlink_ext_ack
*extack
);
283 void switchdev_port_fwd_mark_set(struct net_device
*dev
,
284 struct net_device
*group_dev
,
287 int switchdev_handle_port_obj_add(struct net_device
*dev
,
288 struct switchdev_notifier_port_obj_info
*port_obj_info
,
289 bool (*check_cb
)(const struct net_device
*dev
),
290 int (*add_cb
)(struct net_device
*dev
,
291 const struct switchdev_obj
*obj
,
292 struct switchdev_trans
*trans
,
293 struct netlink_ext_ack
*extack
));
294 int switchdev_handle_port_obj_del(struct net_device
*dev
,
295 struct switchdev_notifier_port_obj_info
*port_obj_info
,
296 bool (*check_cb
)(const struct net_device
*dev
),
297 int (*del_cb
)(struct net_device
*dev
,
298 const struct switchdev_obj
*obj
));
300 int switchdev_handle_port_attr_set(struct net_device
*dev
,
301 struct switchdev_notifier_port_attr_info
*port_attr_info
,
302 bool (*check_cb
)(const struct net_device
*dev
),
303 int (*set_cb
)(struct net_device
*dev
,
304 const struct switchdev_attr
*attr
,
305 struct switchdev_trans
*trans
));
308 static inline void switchdev_deferred_process(void)
312 static inline int switchdev_port_attr_set(struct net_device
*dev
,
313 const struct switchdev_attr
*attr
)
318 static inline int switchdev_port_obj_add(struct net_device
*dev
,
319 const struct switchdev_obj
*obj
,
320 struct netlink_ext_ack
*extack
)
325 static inline int switchdev_port_obj_del(struct net_device
*dev
,
326 const struct switchdev_obj
*obj
)
331 static inline int register_switchdev_notifier(struct notifier_block
*nb
)
336 static inline int unregister_switchdev_notifier(struct notifier_block
*nb
)
341 static inline int call_switchdev_notifiers(unsigned long val
,
342 struct net_device
*dev
,
343 struct switchdev_notifier_info
*info
,
344 struct netlink_ext_ack
*extack
)
350 register_switchdev_blocking_notifier(struct notifier_block
*nb
)
356 unregister_switchdev_blocking_notifier(struct notifier_block
*nb
)
362 call_switchdev_blocking_notifiers(unsigned long val
,
363 struct net_device
*dev
,
364 struct switchdev_notifier_info
*info
,
365 struct netlink_ext_ack
*extack
)
371 switchdev_handle_port_obj_add(struct net_device
*dev
,
372 struct switchdev_notifier_port_obj_info
*port_obj_info
,
373 bool (*check_cb
)(const struct net_device
*dev
),
374 int (*add_cb
)(struct net_device
*dev
,
375 const struct switchdev_obj
*obj
,
376 struct switchdev_trans
*trans
,
377 struct netlink_ext_ack
*extack
))
383 switchdev_handle_port_obj_del(struct net_device
*dev
,
384 struct switchdev_notifier_port_obj_info
*port_obj_info
,
385 bool (*check_cb
)(const struct net_device
*dev
),
386 int (*del_cb
)(struct net_device
*dev
,
387 const struct switchdev_obj
*obj
))
393 switchdev_handle_port_attr_set(struct net_device
*dev
,
394 struct switchdev_notifier_port_attr_info
*port_attr_info
,
395 bool (*check_cb
)(const struct net_device
*dev
),
396 int (*set_cb
)(struct net_device
*dev
,
397 const struct switchdev_attr
*attr
,
398 struct switchdev_trans
*trans
))
404 #endif /* _LINUX_SWITCHDEV_H_ */