blob: 8528015590e44fdab27d6200eef46fd0ff838e50 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-or-later */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 */
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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019struct switchdev_trans {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020 bool ph_prepare;
21};
22
23static inline bool switchdev_trans_ph_prepare(struct switchdev_trans *trans)
24{
25 return trans && trans->ph_prepare;
26}
27
28static inline bool switchdev_trans_ph_commit(struct switchdev_trans *trans)
29{
30 return trans && !trans->ph_prepare;
31}
32
33enum switchdev_attr_id {
34 SWITCHDEV_ATTR_ID_UNDEFINED,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035 SWITCHDEV_ATTR_ID_PORT_STP_STATE,
36 SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
David Brazdil0f672f62019-12-10 10:32:29 +000037 SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038 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,
Olivier Deprez157378f2022-04-04 15:47:50 +020043#if IS_ENABLED(CONFIG_BRIDGE_MRP)
44 SWITCHDEV_ATTR_ID_MRP_PORT_ROLE,
45#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046};
47
48struct 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 {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055 u8 stp_state; /* PORT_STP_STATE */
David Brazdil0f672f62019-12-10 10:32:29 +000056 unsigned long brport_flags; /* PORT_{PRE}_BRIDGE_FLAGS */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057 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 */
Olivier Deprez157378f2022-04-04 15:47:50 +020061#if IS_ENABLED(CONFIG_BRIDGE_MRP)
62 u8 mrp_port_role; /* MRP_PORT_ROLE */
63#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064 } u;
65};
66
67enum 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,
Olivier Deprez157378f2022-04-04 15:47:50 +020072#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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000082};
83
84struct 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 */
93struct switchdev_obj_port_vlan {
94 struct switchdev_obj obj;
95 u16 flags;
96 u16 vid_begin;
97 u16 vid_end;
98};
99
David Brazdil0f672f62019-12-10 10:32:29 +0000100#define SWITCHDEV_OBJ_PORT_VLAN(OBJ) \
101 container_of((OBJ), struct switchdev_obj_port_vlan, obj)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000102
103/* SWITCHDEV_OBJ_ID_PORT_MDB */
104struct switchdev_obj_port_mdb {
105 struct switchdev_obj obj;
106 unsigned char addr[ETH_ALEN];
107 u16 vid;
108};
109
David Brazdil0f672f62019-12-10 10:32:29 +0000110#define SWITCHDEV_OBJ_PORT_MDB(OBJ) \
111 container_of((OBJ), struct switchdev_obj_port_mdb, obj)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000112
Olivier Deprez157378f2022-04-04 15:47:50 +0200113
114#if IS_ENABLED(CONFIG_BRIDGE_MRP)
115/* SWITCHDEV_OBJ_ID_MRP */
116struct 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 */
128struct 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 */
142struct 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
151struct 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 */
161struct 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 */
174struct 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
185struct 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000196typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
197
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000198enum 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,
Olivier Deprez157378f2022-04-04 15:47:50 +0200204 SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
David Brazdil0f672f62019-12-10 10:32:29 +0000205
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,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000215};
216
217struct switchdev_notifier_info {
218 struct net_device *dev;
David Brazdil0f672f62019-12-10 10:32:29 +0000219 struct netlink_ext_ack *extack;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000220};
221
222struct switchdev_notifier_fdb_info {
223 struct switchdev_notifier_info info; /* must be first */
224 const unsigned char *addr;
225 u16 vid;
David Brazdil0f672f62019-12-10 10:32:29 +0000226 u8 added_by_user:1,
227 offloaded:1;
228};
229
230struct 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
237struct 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000242};
243
244static inline struct net_device *
245switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
246{
247 return info->dev;
248}
249
David Brazdil0f672f62019-12-10 10:32:29 +0000250static inline struct netlink_ext_ack *
251switchdev_notifier_info_to_extack(const struct switchdev_notifier_info *info)
252{
253 return info->extack;
254}
255
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000256#ifdef CONFIG_NET_SWITCHDEV
257
258void switchdev_deferred_process(void);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000259int switchdev_port_attr_set(struct net_device *dev,
260 const struct switchdev_attr *attr);
261int switchdev_port_obj_add(struct net_device *dev,
David Brazdil0f672f62019-12-10 10:32:29 +0000262 const struct switchdev_obj *obj,
263 struct netlink_ext_ack *extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000264int switchdev_port_obj_del(struct net_device *dev,
265 const struct switchdev_obj *obj);
David Brazdil0f672f62019-12-10 10:32:29 +0000266
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000267int register_switchdev_notifier(struct notifier_block *nb);
268int unregister_switchdev_notifier(struct notifier_block *nb);
269int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
David Brazdil0f672f62019-12-10 10:32:29 +0000270 struct switchdev_notifier_info *info,
271 struct netlink_ext_ack *extack);
272
273int register_switchdev_blocking_notifier(struct notifier_block *nb);
274int unregister_switchdev_blocking_notifier(struct notifier_block *nb);
275int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
276 struct switchdev_notifier_info *info,
277 struct netlink_ext_ack *extack);
278
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000279void switchdev_port_fwd_mark_set(struct net_device *dev,
280 struct net_device *group_dev,
281 bool joining);
282
David Brazdil0f672f62019-12-10 10:32:29 +0000283int 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));
290int 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));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000295
David Brazdil0f672f62019-12-10 10:32:29 +0000296int 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));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000302#else
303
304static inline void switchdev_deferred_process(void)
305{
306}
307
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000308static inline int switchdev_port_attr_set(struct net_device *dev,
309 const struct switchdev_attr *attr)
310{
311 return -EOPNOTSUPP;
312}
313
314static inline int switchdev_port_obj_add(struct net_device *dev,
David Brazdil0f672f62019-12-10 10:32:29 +0000315 const struct switchdev_obj *obj,
316 struct netlink_ext_ack *extack)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000317{
318 return -EOPNOTSUPP;
319}
320
321static inline int switchdev_port_obj_del(struct net_device *dev,
322 const struct switchdev_obj *obj)
323{
324 return -EOPNOTSUPP;
325}
326
327static inline int register_switchdev_notifier(struct notifier_block *nb)
328{
329 return 0;
330}
331
332static inline int unregister_switchdev_notifier(struct notifier_block *nb)
333{
334 return 0;
335}
336
337static inline int call_switchdev_notifiers(unsigned long val,
338 struct net_device *dev,
David Brazdil0f672f62019-12-10 10:32:29 +0000339 struct switchdev_notifier_info *info,
340 struct netlink_ext_ack *extack)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000341{
342 return NOTIFY_DONE;
343}
344
David Brazdil0f672f62019-12-10 10:32:29 +0000345static inline int
346register_switchdev_blocking_notifier(struct notifier_block *nb)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000347{
David Brazdil0f672f62019-12-10 10:32:29 +0000348 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349}
350
David Brazdil0f672f62019-12-10 10:32:29 +0000351static inline int
352unregister_switchdev_blocking_notifier(struct notifier_block *nb)
353{
354 return 0;
355}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000356
David Brazdil0f672f62019-12-10 10:32:29 +0000357static inline int
358call_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
366static inline int
367switchdev_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
378static inline int
379switchdev_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
388static inline int
389switchdev_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}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000398#endif
399
400#endif /* _LINUX_SWITCHDEV_H_ */