Commit | Line | Data |
---|---|---|
2874c5fd | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
007f790c JP |
2 | /* |
3 | * include/net/switchdev.h - Switch device API | |
7ea6eb3f | 4 | * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us> |
f8f21471 | 5 | * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com> |
007f790c JP |
6 | */ |
7 | #ifndef _LINUX_SWITCHDEV_H_ | |
8 | #define _LINUX_SWITCHDEV_H_ | |
9 | ||
10 | #include <linux/netdevice.h> | |
03bf0c28 | 11 | #include <linux/notifier.h> |
7ea6eb3f | 12 | #include <linux/list.h> |
850d0cbc | 13 | #include <net/ip_fib.h> |
03bf0c28 | 14 | |
3094333d | 15 | #define SWITCHDEV_F_NO_RECURSE BIT(0) |
464314ea | 16 | #define SWITCHDEV_F_SKIP_EOPNOTSUPP BIT(1) |
0bc05d58 | 17 | #define SWITCHDEV_F_DEFER BIT(2) |
3094333d | 18 | |
7ea6eb3f | 19 | struct switchdev_trans { |
f623ab7f | 20 | bool ph_prepare; |
7ea6eb3f JP |
21 | }; |
22 | ||
8bdb4272 JP |
23 | static inline bool switchdev_trans_ph_prepare(struct switchdev_trans *trans) |
24 | { | |
f623ab7f | 25 | return trans && trans->ph_prepare; |
8bdb4272 JP |
26 | } |
27 | ||
28 | static inline bool switchdev_trans_ph_commit(struct switchdev_trans *trans) | |
29 | { | |
f623ab7f | 30 | return trans && !trans->ph_prepare; |
8bdb4272 JP |
31 | } |
32 | ||
3094333d | 33 | enum switchdev_attr_id { |
1f868398 | 34 | SWITCHDEV_ATTR_ID_UNDEFINED, |
1f868398 JP |
35 | SWITCHDEV_ATTR_ID_PORT_STP_STATE, |
36 | SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS, | |
746dc184 | 37 | SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS, |
6d549648 | 38 | SWITCHDEV_ATTR_ID_PORT_MROUTER, |
f55ac58a | 39 | SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME, |
81435c33 | 40 | SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING, |
147c1e9b | 41 | SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED, |
77041420 | 42 | SWITCHDEV_ATTR_ID_BRIDGE_MROUTER, |
c284b545 HV |
43 | #if IS_ENABLED(CONFIG_BRIDGE_MRP) |
44 | SWITCHDEV_ATTR_ID_MRP_PORT_STATE, | |
45 | SWITCHDEV_ATTR_ID_MRP_PORT_ROLE, | |
46 | #endif | |
3094333d SF |
47 | }; |
48 | ||
49 | struct switchdev_attr { | |
6ff64f6f | 50 | struct net_device *orig_dev; |
3094333d | 51 | enum switchdev_attr_id id; |
3094333d | 52 | u32 flags; |
7ceb2afb ER |
53 | void *complete_priv; |
54 | void (*complete)(struct net_device *dev, int err, void *priv); | |
f8e20a9f | 55 | union { |
35636062 | 56 | u8 stp_state; /* PORT_STP_STATE */ |
746dc184 | 57 | unsigned long brport_flags; /* PORT_{PRE}_BRIDGE_FLAGS */ |
6d549648 | 58 | bool mrouter; /* PORT_MROUTER */ |
eabfdda9 | 59 | clock_t ageing_time; /* BRIDGE_AGEING_TIME */ |
81435c33 | 60 | bool vlan_filtering; /* BRIDGE_VLAN_FILTERING */ |
147c1e9b | 61 | bool mc_disabled; /* MC_DISABLED */ |
c284b545 HV |
62 | #if IS_ENABLED(CONFIG_BRIDGE_MRP) |
63 | u8 mrp_port_state; /* MRP_PORT_STATE */ | |
64 | u8 mrp_port_role; /* MRP_PORT_ROLE */ | |
c284b545 | 65 | #endif |
42275bd8 | 66 | } u; |
3094333d SF |
67 | }; |
68 | ||
491d0f15 | 69 | enum switchdev_obj_id { |
57d80838 JP |
70 | SWITCHDEV_OBJ_ID_UNDEFINED, |
71 | SWITCHDEV_OBJ_ID_PORT_VLAN, | |
4d41e125 | 72 | SWITCHDEV_OBJ_ID_PORT_MDB, |
47d5b6db | 73 | SWITCHDEV_OBJ_ID_HOST_MDB, |
c284b545 HV |
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 | |
491d0f15 SF |
80 | }; |
81 | ||
648b4a99 | 82 | struct switchdev_obj { |
6ff64f6f | 83 | struct net_device *orig_dev; |
9e8f4a54 | 84 | enum switchdev_obj_id id; |
4d429c5d | 85 | u32 flags; |
7ceb2afb ER |
86 | void *complete_priv; |
87 | void (*complete)(struct net_device *dev, int err, void *priv); | |
648b4a99 JP |
88 | }; |
89 | ||
57d80838 | 90 | /* SWITCHDEV_OBJ_ID_PORT_VLAN */ |
8f24f309 | 91 | struct switchdev_obj_port_vlan { |
648b4a99 | 92 | struct switchdev_obj obj; |
44bbcf5c VD |
93 | u16 flags; |
94 | u16 vid_begin; | |
95 | u16 vid_end; | |
96 | }; | |
97 | ||
ec394af5 PM |
98 | #define SWITCHDEV_OBJ_PORT_VLAN(OBJ) \ |
99 | container_of((OBJ), struct switchdev_obj_port_vlan, obj) | |
648b4a99 | 100 | |
4d41e125 ER |
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 | ||
ec394af5 PM |
108 | #define SWITCHDEV_OBJ_PORT_MDB(OBJ) \ |
109 | container_of((OBJ), struct switchdev_obj_port_mdb, obj) | |
4d41e125 | 110 | |
c284b545 HV |
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; | |
4b3a61b0 | 119 | u16 prio; |
c284b545 HV |
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 | }; | |
134 | ||
135 | #define SWITCHDEV_OBJ_RING_TEST_MRP(OBJ) \ | |
136 | container_of((OBJ), struct switchdev_obj_ring_test_mrp, obj) | |
137 | ||
138 | /* SWICHDEV_OBJ_ID_RING_ROLE_MRP */ | |
139 | struct switchdev_obj_ring_role_mrp { | |
140 | struct switchdev_obj obj; | |
141 | u8 ring_role; | |
142 | u32 ring_id; | |
143 | }; | |
144 | ||
145 | #define SWITCHDEV_OBJ_RING_ROLE_MRP(OBJ) \ | |
146 | container_of((OBJ), struct switchdev_obj_ring_role_mrp, obj) | |
147 | ||
148 | struct switchdev_obj_ring_state_mrp { | |
149 | struct switchdev_obj obj; | |
150 | u8 ring_state; | |
151 | u32 ring_id; | |
152 | }; | |
153 | ||
154 | #define SWITCHDEV_OBJ_RING_STATE_MRP(OBJ) \ | |
155 | container_of((OBJ), struct switchdev_obj_ring_state_mrp, obj) | |
156 | ||
157 | #endif | |
158 | ||
648b4a99 JP |
159 | typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj); |
160 | ||
ebb9a03a | 161 | enum switchdev_notifier_type { |
6b26b51b AS |
162 | SWITCHDEV_FDB_ADD_TO_BRIDGE = 1, |
163 | SWITCHDEV_FDB_DEL_TO_BRIDGE, | |
164 | SWITCHDEV_FDB_ADD_TO_DEVICE, | |
165 | SWITCHDEV_FDB_DEL_TO_DEVICE, | |
9fe8bcec | 166 | SWITCHDEV_FDB_OFFLOADED, |
9a997353 | 167 | |
aa4efe21 PM |
168 | SWITCHDEV_PORT_OBJ_ADD, /* Blocking. */ |
169 | SWITCHDEV_PORT_OBJ_DEL, /* Blocking. */ | |
1cb33af1 | 170 | SWITCHDEV_PORT_ATTR_SET, /* May be blocking . */ |
aa4efe21 | 171 | |
5728ae0d PM |
172 | SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE, |
173 | SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE, | |
9a997353 PM |
174 | SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE, |
175 | SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE, | |
0efe1173 | 176 | SWITCHDEV_VXLAN_FDB_OFFLOADED, |
3aeb6617 JP |
177 | }; |
178 | ||
ebb9a03a | 179 | struct switchdev_notifier_info { |
03bf0c28 | 180 | struct net_device *dev; |
479c86dc | 181 | struct netlink_ext_ack *extack; |
03bf0c28 JP |
182 | }; |
183 | ||
ebb9a03a JP |
184 | struct switchdev_notifier_fdb_info { |
185 | struct switchdev_notifier_info info; /* must be first */ | |
3aeb6617 JP |
186 | const unsigned char *addr; |
187 | u16 vid; | |
e9ba0fbc IS |
188 | u8 added_by_user:1, |
189 | offloaded:1; | |
3aeb6617 JP |
190 | }; |
191 | ||
aa4efe21 PM |
192 | struct switchdev_notifier_port_obj_info { |
193 | struct switchdev_notifier_info info; /* must be first */ | |
194 | const struct switchdev_obj *obj; | |
195 | struct switchdev_trans *trans; | |
196 | bool handled; | |
197 | }; | |
198 | ||
1cb33af1 FF |
199 | struct switchdev_notifier_port_attr_info { |
200 | struct switchdev_notifier_info info; /* must be first */ | |
201 | const struct switchdev_attr *attr; | |
202 | struct switchdev_trans *trans; | |
203 | bool handled; | |
204 | }; | |
205 | ||
03bf0c28 | 206 | static inline struct net_device * |
ebb9a03a | 207 | switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info) |
03bf0c28 JP |
208 | { |
209 | return info->dev; | |
210 | } | |
007f790c | 211 | |
479c86dc PM |
212 | static inline struct netlink_ext_ack * |
213 | switchdev_notifier_info_to_extack(const struct switchdev_notifier_info *info) | |
214 | { | |
215 | return info->extack; | |
216 | } | |
217 | ||
007f790c JP |
218 | #ifdef CONFIG_NET_SWITCHDEV |
219 | ||
793f4014 | 220 | void switchdev_deferred_process(void); |
3094333d | 221 | int switchdev_port_attr_set(struct net_device *dev, |
f7fadf30 | 222 | const struct switchdev_attr *attr); |
9e8f4a54 | 223 | int switchdev_port_obj_add(struct net_device *dev, |
69b7320e PM |
224 | const struct switchdev_obj *obj, |
225 | struct netlink_ext_ack *extack); | |
9e8f4a54 | 226 | int switchdev_port_obj_del(struct net_device *dev, |
648b4a99 | 227 | const struct switchdev_obj *obj); |
a93e3b17 | 228 | |
ebb9a03a JP |
229 | int register_switchdev_notifier(struct notifier_block *nb); |
230 | int unregister_switchdev_notifier(struct notifier_block *nb); | |
231 | int call_switchdev_notifiers(unsigned long val, struct net_device *dev, | |
6685987c PM |
232 | struct switchdev_notifier_info *info, |
233 | struct netlink_ext_ack *extack); | |
a93e3b17 PM |
234 | |
235 | int register_switchdev_blocking_notifier(struct notifier_block *nb); | |
236 | int unregister_switchdev_blocking_notifier(struct notifier_block *nb); | |
237 | int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev, | |
479c86dc PM |
238 | struct switchdev_notifier_info *info, |
239 | struct netlink_ext_ack *extack); | |
a93e3b17 | 240 | |
1a3b2ec9 SF |
241 | void switchdev_port_fwd_mark_set(struct net_device *dev, |
242 | struct net_device *group_dev, | |
243 | bool joining); | |
5e8d9049 | 244 | |
f30f0601 PM |
245 | int switchdev_handle_port_obj_add(struct net_device *dev, |
246 | struct switchdev_notifier_port_obj_info *port_obj_info, | |
247 | bool (*check_cb)(const struct net_device *dev), | |
248 | int (*add_cb)(struct net_device *dev, | |
249 | const struct switchdev_obj *obj, | |
69213513 PM |
250 | struct switchdev_trans *trans, |
251 | struct netlink_ext_ack *extack)); | |
f30f0601 PM |
252 | int switchdev_handle_port_obj_del(struct net_device *dev, |
253 | struct switchdev_notifier_port_obj_info *port_obj_info, | |
254 | bool (*check_cb)(const struct net_device *dev), | |
255 | int (*del_cb)(struct net_device *dev, | |
256 | const struct switchdev_obj *obj)); | |
257 | ||
1cb33af1 FF |
258 | int switchdev_handle_port_attr_set(struct net_device *dev, |
259 | struct switchdev_notifier_port_attr_info *port_attr_info, | |
260 | bool (*check_cb)(const struct net_device *dev), | |
261 | int (*set_cb)(struct net_device *dev, | |
262 | const struct switchdev_attr *attr, | |
263 | struct switchdev_trans *trans)); | |
007f790c JP |
264 | #else |
265 | ||
793f4014 JP |
266 | static inline void switchdev_deferred_process(void) |
267 | { | |
268 | } | |
269 | ||
3094333d | 270 | static inline int switchdev_port_attr_set(struct net_device *dev, |
f7fadf30 | 271 | const struct switchdev_attr *attr) |
3094333d SF |
272 | { |
273 | return -EOPNOTSUPP; | |
274 | } | |
275 | ||
491d0f15 | 276 | static inline int switchdev_port_obj_add(struct net_device *dev, |
69b7320e PM |
277 | const struct switchdev_obj *obj, |
278 | struct netlink_ext_ack *extack) | |
491d0f15 SF |
279 | { |
280 | return -EOPNOTSUPP; | |
281 | } | |
282 | ||
283 | static inline int switchdev_port_obj_del(struct net_device *dev, | |
648b4a99 | 284 | const struct switchdev_obj *obj) |
491d0f15 SF |
285 | { |
286 | return -EOPNOTSUPP; | |
287 | } | |
288 | ||
ebb9a03a | 289 | static inline int register_switchdev_notifier(struct notifier_block *nb) |
03bf0c28 JP |
290 | { |
291 | return 0; | |
292 | } | |
293 | ||
ebb9a03a | 294 | static inline int unregister_switchdev_notifier(struct notifier_block *nb) |
03bf0c28 JP |
295 | { |
296 | return 0; | |
297 | } | |
298 | ||
ebb9a03a JP |
299 | static inline int call_switchdev_notifiers(unsigned long val, |
300 | struct net_device *dev, | |
6685987c PM |
301 | struct switchdev_notifier_info *info, |
302 | struct netlink_ext_ack *extack) | |
03bf0c28 JP |
303 | { |
304 | return NOTIFY_DONE; | |
305 | } | |
306 | ||
a93e3b17 PM |
307 | static inline int |
308 | register_switchdev_blocking_notifier(struct notifier_block *nb) | |
309 | { | |
310 | return 0; | |
311 | } | |
312 | ||
313 | static inline int | |
314 | unregister_switchdev_blocking_notifier(struct notifier_block *nb) | |
315 | { | |
316 | return 0; | |
317 | } | |
318 | ||
319 | static inline int | |
320 | call_switchdev_blocking_notifiers(unsigned long val, | |
321 | struct net_device *dev, | |
479c86dc PM |
322 | struct switchdev_notifier_info *info, |
323 | struct netlink_ext_ack *extack) | |
a93e3b17 PM |
324 | { |
325 | return NOTIFY_DONE; | |
326 | } | |
327 | ||
f30f0601 PM |
328 | static inline int |
329 | switchdev_handle_port_obj_add(struct net_device *dev, | |
330 | struct switchdev_notifier_port_obj_info *port_obj_info, | |
331 | bool (*check_cb)(const struct net_device *dev), | |
332 | int (*add_cb)(struct net_device *dev, | |
333 | const struct switchdev_obj *obj, | |
69213513 PM |
334 | struct switchdev_trans *trans, |
335 | struct netlink_ext_ack *extack)) | |
f30f0601 PM |
336 | { |
337 | return 0; | |
338 | } | |
339 | ||
340 | static inline int | |
341 | switchdev_handle_port_obj_del(struct net_device *dev, | |
342 | struct switchdev_notifier_port_obj_info *port_obj_info, | |
343 | bool (*check_cb)(const struct net_device *dev), | |
344 | int (*del_cb)(struct net_device *dev, | |
345 | const struct switchdev_obj *obj)) | |
346 | { | |
347 | return 0; | |
348 | } | |
349 | ||
1cb33af1 FF |
350 | static inline int |
351 | switchdev_handle_port_attr_set(struct net_device *dev, | |
352 | struct switchdev_notifier_port_attr_info *port_attr_info, | |
353 | bool (*check_cb)(const struct net_device *dev), | |
354 | int (*set_cb)(struct net_device *dev, | |
355 | const struct switchdev_attr *attr, | |
356 | struct switchdev_trans *trans)) | |
357 | { | |
358 | return 0; | |
359 | } | |
007f790c JP |
360 | #endif |
361 | ||
362 | #endif /* _LINUX_SWITCHDEV_H_ */ |