Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6bc506b4 IS |
2 | #include <linux/kernel.h> |
3 | #include <linux/list.h> | |
4 | #include <linux/netdevice.h> | |
5 | #include <linux/rtnetlink.h> | |
6 | #include <linux/skbuff.h> | |
9776457c | 7 | #include <net/ip.h> |
6bc506b4 IS |
8 | #include <net/switchdev.h> |
9 | ||
10 | #include "br_private.h" | |
11 | ||
47211192 TW |
12 | static struct static_key_false br_switchdev_tx_fwd_offload; |
13 | ||
14 | static bool nbp_switchdev_can_offload_tx_fwd(const struct net_bridge_port *p, | |
15 | const struct sk_buff *skb) | |
16 | { | |
17 | if (!static_branch_unlikely(&br_switchdev_tx_fwd_offload)) | |
18 | return false; | |
19 | ||
20 | return (p->flags & BR_TX_FWD_OFFLOAD) && | |
21 | (p->hwdom != BR_INPUT_SKB_CB(skb)->src_hwdom); | |
22 | } | |
23 | ||
24 | bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb) | |
25 | { | |
26 | if (!static_branch_unlikely(&br_switchdev_tx_fwd_offload)) | |
27 | return false; | |
28 | ||
29 | return BR_INPUT_SKB_CB(skb)->tx_fwd_offload; | |
30 | } | |
31 | ||
c5381154 VO |
32 | void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb) |
33 | { | |
34 | skb->offload_fwd_mark = br_switchdev_frame_uses_tx_fwd_offload(skb); | |
35 | } | |
36 | ||
47211192 TW |
37 | /* Mark the frame for TX forwarding offload if this egress port supports it */ |
38 | void nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p, | |
39 | struct sk_buff *skb) | |
40 | { | |
41 | if (nbp_switchdev_can_offload_tx_fwd(p, skb)) | |
42 | BR_INPUT_SKB_CB(skb)->tx_fwd_offload = true; | |
43 | } | |
44 | ||
45 | /* Lazily adds the hwdom of the egress bridge port to the bit mask of hwdoms | |
46 | * that the skb has been already forwarded to, to avoid further cloning to | |
47 | * other ports in the same hwdom by making nbp_switchdev_allowed_egress() | |
48 | * return false. | |
49 | */ | |
50 | void nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p, | |
51 | struct sk_buff *skb) | |
52 | { | |
53 | if (nbp_switchdev_can_offload_tx_fwd(p, skb)) | |
54 | set_bit(p->hwdom, &BR_INPUT_SKB_CB(skb)->fwd_hwdoms); | |
55 | } | |
56 | ||
6bc506b4 IS |
57 | void nbp_switchdev_frame_mark(const struct net_bridge_port *p, |
58 | struct sk_buff *skb) | |
59 | { | |
f7cf972f TW |
60 | if (p->hwdom) |
61 | BR_INPUT_SKB_CB(skb)->src_hwdom = p->hwdom; | |
6bc506b4 IS |
62 | } |
63 | ||
64 | bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p, | |
65 | const struct sk_buff *skb) | |
66 | { | |
47211192 TW |
67 | struct br_input_skb_cb *cb = BR_INPUT_SKB_CB(skb); |
68 | ||
69 | return !test_bit(p->hwdom, &cb->fwd_hwdoms) && | |
70 | (!skb->offload_fwd_mark || cb->src_hwdom != p->hwdom); | |
6bc506b4 | 71 | } |
3922285d AS |
72 | |
73 | /* Flags that can be offloaded to hardware */ | |
9c0ca02b | 74 | #define BR_PORT_FLAGS_HW_OFFLOAD (BR_LEARNING | BR_FLOOD | BR_PORT_MAB | \ |
c3976a3f AÜ |
75 | BR_MCAST_FLOOD | BR_BCAST_FLOOD | BR_PORT_LOCKED | \ |
76 | BR_HAIRPIN_MODE | BR_ISOLATED | BR_MULTICAST_TO_UNICAST) | |
3922285d AS |
77 | |
78 | int br_switchdev_set_port_flag(struct net_bridge_port *p, | |
79 | unsigned long flags, | |
078bbb85 VO |
80 | unsigned long mask, |
81 | struct netlink_ext_ack *extack) | |
3922285d AS |
82 | { |
83 | struct switchdev_attr attr = { | |
84 | .orig_dev = p->dev, | |
3922285d | 85 | }; |
d45224d6 FF |
86 | struct switchdev_notifier_port_attr_info info = { |
87 | .attr = &attr, | |
88 | }; | |
3922285d AS |
89 | int err; |
90 | ||
304ae3bf VO |
91 | mask &= BR_PORT_FLAGS_HW_OFFLOAD; |
92 | if (!mask) | |
3922285d AS |
93 | return 0; |
94 | ||
e18f4c18 VO |
95 | attr.id = SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS; |
96 | attr.u.brport_flags.val = flags; | |
97 | attr.u.brport_flags.mask = mask; | |
304ae3bf | 98 | |
d45224d6 FF |
99 | /* We run from atomic context here */ |
100 | err = call_switchdev_notifiers(SWITCHDEV_PORT_ATTR_SET, p->dev, | |
078bbb85 | 101 | &info.info, extack); |
d45224d6 | 102 | err = notifier_to_errno(err); |
3922285d AS |
103 | if (err == -EOPNOTSUPP) |
104 | return 0; | |
3922285d | 105 | |
1ef07644 | 106 | if (err) { |
028fb19c LR |
107 | NL_SET_ERR_MSG_WEAK_MOD(extack, |
108 | "bridge flag offload is not supported"); | |
3922285d AS |
109 | return -EOPNOTSUPP; |
110 | } | |
111 | ||
112 | attr.id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS; | |
113 | attr.flags = SWITCHDEV_F_DEFER; | |
1ef07644 | 114 | |
dcbdf135 | 115 | err = switchdev_port_attr_set(p->dev, &attr, extack); |
3922285d | 116 | if (err) { |
028fb19c LR |
117 | NL_SET_ERR_MSG_WEAK_MOD(extack, |
118 | "error setting offload flag on port"); | |
3922285d AS |
119 | return err; |
120 | } | |
121 | ||
122 | return 0; | |
123 | } | |
6b26b51b | 124 | |
fab9eca8 VO |
125 | static void br_switchdev_fdb_populate(struct net_bridge *br, |
126 | struct switchdev_notifier_fdb_info *item, | |
127 | const struct net_bridge_fdb_entry *fdb, | |
128 | const void *ctx) | |
129 | { | |
130 | const struct net_bridge_port *p = READ_ONCE(fdb->dst); | |
131 | ||
132 | item->addr = fdb->key.addr.addr; | |
133 | item->vid = fdb->key.vlan_id; | |
134 | item->added_by_user = test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags); | |
135 | item->offloaded = test_bit(BR_FDB_OFFLOADED, &fdb->flags); | |
136 | item->is_local = test_bit(BR_FDB_LOCAL, &fdb->flags); | |
27fabd02 | 137 | item->locked = false; |
fab9eca8 VO |
138 | item->info.dev = (!p || item->is_local) ? br->dev : p->dev; |
139 | item->info.ctx = ctx; | |
140 | } | |
141 | ||
6b26b51b | 142 | void |
6eb38bf8 TW |
143 | br_switchdev_fdb_notify(struct net_bridge *br, |
144 | const struct net_bridge_fdb_entry *fdb, int type) | |
6b26b51b | 145 | { |
fab9eca8 VO |
146 | struct switchdev_notifier_fdb_info item; |
147 | ||
27fabd02 HS |
148 | if (test_bit(BR_FDB_LOCKED, &fdb->flags)) |
149 | return; | |
150 | ||
927cdea5 VO |
151 | /* Entries with these flags were created using ndm_state == NUD_REACHABLE, |
152 | * ndm_flags == NTF_MASTER( | NTF_STICKY), ext_flags == 0 by something | |
153 | * equivalent to 'bridge fdb add ... master dynamic (sticky)'. | |
154 | * Drivers don't know how to deal with these, so don't notify them to | |
155 | * avoid confusing them. | |
156 | */ | |
157 | if (test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags) && | |
158 | !test_bit(BR_FDB_STATIC, &fdb->flags) && | |
159 | !test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) | |
160 | return; | |
161 | ||
fab9eca8 | 162 | br_switchdev_fdb_populate(br, &item, fdb, NULL); |
e5b4b898 | 163 | |
6b26b51b AS |
164 | switch (type) { |
165 | case RTM_DELNEIGH: | |
e5b4b898 | 166 | call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_DEVICE, |
fab9eca8 | 167 | item.info.dev, &item.info, NULL); |
6b26b51b AS |
168 | break; |
169 | case RTM_NEWNEIGH: | |
e5b4b898 | 170 | call_switchdev_notifiers(SWITCHDEV_FDB_ADD_TO_DEVICE, |
fab9eca8 | 171 | item.info.dev, &item.info, NULL); |
6b26b51b AS |
172 | break; |
173 | } | |
174 | } | |
d66e4348 | 175 | |
169327d5 | 176 | int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags, |
8d23a54f | 177 | bool changed, struct netlink_ext_ack *extack) |
d66e4348 PM |
178 | { |
179 | struct switchdev_obj_port_vlan v = { | |
180 | .obj.orig_dev = dev, | |
181 | .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, | |
182 | .flags = flags, | |
b7a9e0da | 183 | .vid = vid, |
8d23a54f | 184 | .changed = changed, |
d66e4348 PM |
185 | }; |
186 | ||
69b7320e | 187 | return switchdev_port_obj_add(dev, &v.obj, extack); |
d66e4348 PM |
188 | } |
189 | ||
190 | int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid) | |
191 | { | |
192 | struct switchdev_obj_port_vlan v = { | |
193 | .obj.orig_dev = dev, | |
194 | .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, | |
b7a9e0da | 195 | .vid = vid, |
d66e4348 PM |
196 | }; |
197 | ||
198 | return switchdev_port_obj_del(dev, &v.obj); | |
199 | } | |
85826610 TW |
200 | |
201 | static int nbp_switchdev_hwdom_set(struct net_bridge_port *joining) | |
202 | { | |
203 | struct net_bridge *br = joining->br; | |
204 | struct net_bridge_port *p; | |
205 | int hwdom; | |
206 | ||
207 | /* joining is yet to be added to the port list. */ | |
208 | list_for_each_entry(p, &br->port_list, list) { | |
2f5dc00f | 209 | if (netdev_phys_item_id_same(&joining->ppid, &p->ppid)) { |
85826610 TW |
210 | joining->hwdom = p->hwdom; |
211 | return 0; | |
212 | } | |
213 | } | |
214 | ||
215 | hwdom = find_next_zero_bit(&br->busy_hwdoms, BR_HWDOM_MAX, 1); | |
216 | if (hwdom >= BR_HWDOM_MAX) | |
217 | return -EBUSY; | |
218 | ||
219 | set_bit(hwdom, &br->busy_hwdoms); | |
220 | joining->hwdom = hwdom; | |
221 | return 0; | |
222 | } | |
223 | ||
224 | static void nbp_switchdev_hwdom_put(struct net_bridge_port *leaving) | |
225 | { | |
226 | struct net_bridge *br = leaving->br; | |
227 | struct net_bridge_port *p; | |
228 | ||
229 | /* leaving is no longer in the port list. */ | |
230 | list_for_each_entry(p, &br->port_list, list) { | |
231 | if (p->hwdom == leaving->hwdom) | |
232 | return; | |
233 | } | |
234 | ||
235 | clear_bit(leaving->hwdom, &br->busy_hwdoms); | |
236 | } | |
237 | ||
2f5dc00f VO |
238 | static int nbp_switchdev_add(struct net_bridge_port *p, |
239 | struct netdev_phys_item_id ppid, | |
47211192 | 240 | bool tx_fwd_offload, |
2f5dc00f | 241 | struct netlink_ext_ack *extack) |
85826610 | 242 | { |
47211192 TW |
243 | int err; |
244 | ||
2f5dc00f VO |
245 | if (p->offload_count) { |
246 | /* Prevent unsupported configurations such as a bridge port | |
247 | * which is a bonding interface, and the member ports are from | |
248 | * different hardware switches. | |
249 | */ | |
250 | if (!netdev_phys_item_id_same(&p->ppid, &ppid)) { | |
251 | NL_SET_ERR_MSG_MOD(extack, | |
252 | "Same bridge port cannot be offloaded by two physical switches"); | |
253 | return -EBUSY; | |
254 | } | |
85826610 | 255 | |
2f5dc00f VO |
256 | /* Tolerate drivers that call switchdev_bridge_port_offload() |
257 | * more than once for the same bridge port, such as when the | |
258 | * bridge port is an offloaded bonding/team interface. | |
259 | */ | |
260 | p->offload_count++; | |
85826610 | 261 | |
2f5dc00f | 262 | return 0; |
85826610 TW |
263 | } |
264 | ||
2f5dc00f VO |
265 | p->ppid = ppid; |
266 | p->offload_count = 1; | |
267 | ||
47211192 TW |
268 | err = nbp_switchdev_hwdom_set(p); |
269 | if (err) | |
270 | return err; | |
271 | ||
272 | if (tx_fwd_offload) { | |
273 | p->flags |= BR_TX_FWD_OFFLOAD; | |
274 | static_branch_inc(&br_switchdev_tx_fwd_offload); | |
275 | } | |
276 | ||
277 | return 0; | |
85826610 TW |
278 | } |
279 | ||
2f5dc00f | 280 | static void nbp_switchdev_del(struct net_bridge_port *p) |
85826610 | 281 | { |
2f5dc00f VO |
282 | if (WARN_ON(!p->offload_count)) |
283 | return; | |
284 | ||
285 | p->offload_count--; | |
286 | ||
287 | if (p->offload_count) | |
288 | return; | |
85826610 TW |
289 | |
290 | if (p->hwdom) | |
291 | nbp_switchdev_hwdom_put(p); | |
47211192 TW |
292 | |
293 | if (p->flags & BR_TX_FWD_OFFLOAD) { | |
294 | p->flags &= ~BR_TX_FWD_OFFLOAD; | |
295 | static_branch_dec(&br_switchdev_tx_fwd_offload); | |
296 | } | |
85826610 | 297 | } |
2f5dc00f | 298 | |
326b212e VO |
299 | static int |
300 | br_switchdev_fdb_replay_one(struct net_bridge *br, struct notifier_block *nb, | |
301 | const struct net_bridge_fdb_entry *fdb, | |
302 | unsigned long action, const void *ctx) | |
5cda5272 | 303 | { |
5cda5272 VO |
304 | struct switchdev_notifier_fdb_info item; |
305 | int err; | |
306 | ||
fab9eca8 | 307 | br_switchdev_fdb_populate(br, &item, fdb, ctx); |
5cda5272 VO |
308 | |
309 | err = nb->notifier_call(nb, action, &item); | |
310 | return notifier_to_errno(err); | |
311 | } | |
312 | ||
326b212e VO |
313 | static int |
314 | br_switchdev_fdb_replay(const struct net_device *br_dev, const void *ctx, | |
315 | bool adding, struct notifier_block *nb) | |
5cda5272 VO |
316 | { |
317 | struct net_bridge_fdb_entry *fdb; | |
318 | struct net_bridge *br; | |
319 | unsigned long action; | |
320 | int err = 0; | |
321 | ||
322 | if (!nb) | |
323 | return 0; | |
324 | ||
325 | if (!netif_is_bridge_master(br_dev)) | |
326 | return -EINVAL; | |
327 | ||
328 | br = netdev_priv(br_dev); | |
329 | ||
330 | if (adding) | |
331 | action = SWITCHDEV_FDB_ADD_TO_DEVICE; | |
332 | else | |
333 | action = SWITCHDEV_FDB_DEL_TO_DEVICE; | |
334 | ||
335 | rcu_read_lock(); | |
336 | ||
337 | hlist_for_each_entry_rcu(fdb, &br->fdb_list, fdb_node) { | |
326b212e | 338 | err = br_switchdev_fdb_replay_one(br, nb, fdb, action, ctx); |
5cda5272 VO |
339 | if (err) |
340 | break; | |
341 | } | |
342 | ||
343 | rcu_read_unlock(); | |
344 | ||
345 | return err; | |
346 | } | |
347 | ||
6284c723 TW |
348 | static int br_switchdev_vlan_attr_replay(struct net_device *br_dev, |
349 | const void *ctx, | |
350 | struct notifier_block *nb, | |
351 | struct netlink_ext_ack *extack) | |
352 | { | |
353 | struct switchdev_notifier_port_attr_info attr_info = { | |
354 | .info = { | |
355 | .dev = br_dev, | |
356 | .extack = extack, | |
357 | .ctx = ctx, | |
358 | }, | |
359 | }; | |
360 | struct net_bridge *br = netdev_priv(br_dev); | |
361 | struct net_bridge_vlan_group *vg; | |
362 | struct switchdev_attr attr; | |
363 | struct net_bridge_vlan *v; | |
364 | int err; | |
365 | ||
366 | attr_info.attr = &attr; | |
367 | attr.orig_dev = br_dev; | |
368 | ||
369 | vg = br_vlan_group(br); | |
7f40ea21 CL |
370 | if (!vg) |
371 | return 0; | |
6284c723 TW |
372 | |
373 | list_for_each_entry(v, &vg->vlan_list, vlist) { | |
374 | if (v->msti) { | |
375 | attr.id = SWITCHDEV_ATTR_ID_VLAN_MSTI; | |
376 | attr.u.vlan_msti.vid = v->vid; | |
377 | attr.u.vlan_msti.msti = v->msti; | |
378 | ||
379 | err = nb->notifier_call(nb, SWITCHDEV_PORT_ATTR_SET, | |
380 | &attr_info); | |
381 | err = notifier_to_errno(err); | |
382 | if (err) | |
383 | return err; | |
384 | } | |
385 | } | |
386 | ||
387 | return 0; | |
388 | } | |
389 | ||
326b212e VO |
390 | static int |
391 | br_switchdev_vlan_replay_one(struct notifier_block *nb, | |
392 | struct net_device *dev, | |
393 | struct switchdev_obj_port_vlan *vlan, | |
394 | const void *ctx, unsigned long action, | |
395 | struct netlink_ext_ack *extack) | |
4a6849e4 VO |
396 | { |
397 | struct switchdev_notifier_port_obj_info obj_info = { | |
398 | .info = { | |
399 | .dev = dev, | |
400 | .extack = extack, | |
401 | .ctx = ctx, | |
402 | }, | |
403 | .obj = &vlan->obj, | |
404 | }; | |
405 | int err; | |
406 | ||
407 | err = nb->notifier_call(nb, action, &obj_info); | |
408 | return notifier_to_errno(err); | |
409 | } | |
410 | ||
b28d580e VO |
411 | static int br_switchdev_vlan_replay_group(struct notifier_block *nb, |
412 | struct net_device *dev, | |
413 | struct net_bridge_vlan_group *vg, | |
414 | const void *ctx, unsigned long action, | |
415 | struct netlink_ext_ack *extack) | |
4a6849e4 | 416 | { |
4a6849e4 | 417 | struct net_bridge_vlan *v; |
4a6849e4 VO |
418 | int err = 0; |
419 | u16 pvid; | |
420 | ||
4a6849e4 VO |
421 | if (!vg) |
422 | return 0; | |
423 | ||
4a6849e4 VO |
424 | pvid = br_get_pvid(vg); |
425 | ||
426 | list_for_each_entry(v, &vg->vlan_list, vlist) { | |
427 | struct switchdev_obj_port_vlan vlan = { | |
428 | .obj.orig_dev = dev, | |
429 | .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, | |
430 | .flags = br_vlan_flags(v, pvid), | |
431 | .vid = v->vid, | |
432 | }; | |
433 | ||
434 | if (!br_vlan_should_use(v)) | |
435 | continue; | |
436 | ||
326b212e VO |
437 | err = br_switchdev_vlan_replay_one(nb, dev, &vlan, ctx, |
438 | action, extack); | |
4a6849e4 VO |
439 | if (err) |
440 | return err; | |
441 | } | |
442 | ||
b28d580e VO |
443 | return 0; |
444 | } | |
445 | ||
446 | static int br_switchdev_vlan_replay(struct net_device *br_dev, | |
447 | const void *ctx, bool adding, | |
448 | struct notifier_block *nb, | |
449 | struct netlink_ext_ack *extack) | |
450 | { | |
451 | struct net_bridge *br = netdev_priv(br_dev); | |
452 | struct net_bridge_port *p; | |
453 | unsigned long action; | |
454 | int err; | |
455 | ||
456 | ASSERT_RTNL(); | |
457 | ||
458 | if (!nb) | |
459 | return 0; | |
460 | ||
461 | if (!netif_is_bridge_master(br_dev)) | |
462 | return -EINVAL; | |
463 | ||
464 | if (adding) | |
465 | action = SWITCHDEV_PORT_OBJ_ADD; | |
466 | else | |
467 | action = SWITCHDEV_PORT_OBJ_DEL; | |
468 | ||
469 | err = br_switchdev_vlan_replay_group(nb, br_dev, br_vlan_group(br), | |
470 | ctx, action, extack); | |
471 | if (err) | |
472 | return err; | |
473 | ||
474 | list_for_each_entry(p, &br->port_list, list) { | |
475 | struct net_device *dev = p->dev; | |
476 | ||
477 | err = br_switchdev_vlan_replay_group(nb, dev, | |
478 | nbp_vlan_group(p), | |
479 | ctx, action, extack); | |
480 | if (err) | |
481 | return err; | |
482 | } | |
483 | ||
6284c723 TW |
484 | if (adding) { |
485 | err = br_switchdev_vlan_attr_replay(br_dev, ctx, nb, extack); | |
486 | if (err) | |
487 | return err; | |
488 | } | |
489 | ||
b28d580e | 490 | return 0; |
4a6849e4 VO |
491 | } |
492 | ||
9776457c | 493 | #ifdef CONFIG_BRIDGE_IGMP_SNOOPING |
326b212e | 494 | struct br_switchdev_mdb_complete_info { |
9776457c VO |
495 | struct net_bridge_port *port; |
496 | struct br_ip ip; | |
497 | }; | |
498 | ||
326b212e | 499 | static void br_switchdev_mdb_complete(struct net_device *dev, int err, void *priv) |
9776457c | 500 | { |
326b212e | 501 | struct br_switchdev_mdb_complete_info *data = priv; |
9776457c VO |
502 | struct net_bridge_port_group __rcu **pp; |
503 | struct net_bridge_port_group *p; | |
504 | struct net_bridge_mdb_entry *mp; | |
505 | struct net_bridge_port *port = data->port; | |
506 | struct net_bridge *br = port->br; | |
c428d43d | 507 | u8 old_flags; |
9776457c | 508 | |
e846fb5e JH |
509 | if (err == -EOPNOTSUPP) |
510 | goto out_free; | |
9776457c VO |
511 | |
512 | spin_lock_bh(&br->multicast_lock); | |
513 | mp = br_mdb_ip_get(br, &data->ip); | |
514 | if (!mp) | |
515 | goto out; | |
516 | for (pp = &mp->ports; (p = mlock_dereference(*pp, br)) != NULL; | |
517 | pp = &p->next) { | |
518 | if (p->key.port != port) | |
519 | continue; | |
e846fb5e | 520 | |
c428d43d | 521 | old_flags = p->flags; |
e846fb5e | 522 | br_multicast_set_pg_offload_flags(p, !err); |
c428d43d JH |
523 | if (br_mdb_should_notify(br, old_flags ^ p->flags)) |
524 | br_mdb_flag_change_notify(br->dev, mp, p); | |
9776457c VO |
525 | } |
526 | out: | |
527 | spin_unlock_bh(&br->multicast_lock); | |
e846fb5e | 528 | out_free: |
9776457c VO |
529 | kfree(priv); |
530 | } | |
531 | ||
532 | static void br_switchdev_mdb_populate(struct switchdev_obj_port_mdb *mdb, | |
533 | const struct net_bridge_mdb_entry *mp) | |
534 | { | |
535 | if (mp->addr.proto == htons(ETH_P_IP)) | |
536 | ip_eth_mc_map(mp->addr.dst.ip4, mdb->addr); | |
537 | #if IS_ENABLED(CONFIG_IPV6) | |
538 | else if (mp->addr.proto == htons(ETH_P_IPV6)) | |
539 | ipv6_eth_mc_map(&mp->addr.dst.ip6, mdb->addr); | |
540 | #endif | |
541 | else | |
542 | ether_addr_copy(mdb->addr, mp->addr.dst.mac_addr); | |
543 | ||
544 | mdb->vid = mp->addr.vid; | |
545 | } | |
546 | ||
326b212e VO |
547 | static void br_switchdev_host_mdb_one(struct net_device *dev, |
548 | struct net_device *lower_dev, | |
549 | struct net_bridge_mdb_entry *mp, | |
550 | int type) | |
9776457c VO |
551 | { |
552 | struct switchdev_obj_port_mdb mdb = { | |
553 | .obj = { | |
554 | .id = SWITCHDEV_OBJ_ID_HOST_MDB, | |
555 | .flags = SWITCHDEV_F_DEFER, | |
556 | .orig_dev = dev, | |
557 | }, | |
558 | }; | |
559 | ||
560 | br_switchdev_mdb_populate(&mdb, mp); | |
561 | ||
562 | switch (type) { | |
563 | case RTM_NEWMDB: | |
564 | switchdev_port_obj_add(lower_dev, &mdb.obj, NULL); | |
565 | break; | |
566 | case RTM_DELMDB: | |
567 | switchdev_port_obj_del(lower_dev, &mdb.obj); | |
568 | break; | |
569 | } | |
570 | } | |
571 | ||
326b212e | 572 | static void br_switchdev_host_mdb(struct net_device *dev, |
9776457c VO |
573 | struct net_bridge_mdb_entry *mp, int type) |
574 | { | |
575 | struct net_device *lower_dev; | |
576 | struct list_head *iter; | |
577 | ||
578 | netdev_for_each_lower_dev(dev, lower_dev, iter) | |
326b212e | 579 | br_switchdev_host_mdb_one(dev, lower_dev, mp, type); |
9776457c VO |
580 | } |
581 | ||
326b212e VO |
582 | static int |
583 | br_switchdev_mdb_replay_one(struct notifier_block *nb, struct net_device *dev, | |
584 | const struct switchdev_obj_port_mdb *mdb, | |
585 | unsigned long action, const void *ctx, | |
586 | struct netlink_ext_ack *extack) | |
9776457c VO |
587 | { |
588 | struct switchdev_notifier_port_obj_info obj_info = { | |
589 | .info = { | |
590 | .dev = dev, | |
591 | .extack = extack, | |
592 | .ctx = ctx, | |
593 | }, | |
594 | .obj = &mdb->obj, | |
595 | }; | |
596 | int err; | |
597 | ||
598 | err = nb->notifier_call(nb, action, &obj_info); | |
599 | return notifier_to_errno(err); | |
600 | } | |
601 | ||
326b212e | 602 | static int br_switchdev_mdb_queue_one(struct list_head *mdb_list, |
dc489f86 TW |
603 | struct net_device *dev, |
604 | unsigned long action, | |
326b212e VO |
605 | enum switchdev_obj_id id, |
606 | const struct net_bridge_mdb_entry *mp, | |
607 | struct net_device *orig_dev) | |
9776457c | 608 | { |
dc489f86 TW |
609 | struct switchdev_obj_port_mdb mdb = { |
610 | .obj = { | |
611 | .id = id, | |
612 | .orig_dev = orig_dev, | |
613 | }, | |
614 | }; | |
615 | struct switchdev_obj_port_mdb *pmdb; | |
9776457c | 616 | |
dc489f86 | 617 | br_switchdev_mdb_populate(&mdb, mp); |
9776457c | 618 | |
dc489f86 TW |
619 | if (action == SWITCHDEV_PORT_OBJ_ADD && |
620 | switchdev_port_obj_act_is_deferred(dev, action, &mdb.obj)) { | |
621 | /* This event is already in the deferred queue of | |
622 | * events, so this replay must be elided, lest the | |
623 | * driver receives duplicate events for it. This can | |
624 | * only happen when replaying additions, since | |
625 | * modifications are always immediately visible in | |
626 | * br->mdb_list, whereas actual event delivery may be | |
627 | * delayed. | |
628 | */ | |
629 | return 0; | |
630 | } | |
631 | ||
632 | pmdb = kmemdup(&mdb, sizeof(mdb), GFP_ATOMIC); | |
633 | if (!pmdb) | |
634 | return -ENOMEM; | |
9776457c | 635 | |
dc489f86 | 636 | list_add_tail(&pmdb->obj.list, mdb_list); |
9776457c VO |
637 | return 0; |
638 | } | |
639 | ||
640 | void br_switchdev_mdb_notify(struct net_device *dev, | |
641 | struct net_bridge_mdb_entry *mp, | |
642 | struct net_bridge_port_group *pg, | |
643 | int type) | |
644 | { | |
326b212e | 645 | struct br_switchdev_mdb_complete_info *complete_info; |
9776457c VO |
646 | struct switchdev_obj_port_mdb mdb = { |
647 | .obj = { | |
648 | .id = SWITCHDEV_OBJ_ID_PORT_MDB, | |
649 | .flags = SWITCHDEV_F_DEFER, | |
650 | }, | |
651 | }; | |
652 | ||
653 | if (!pg) | |
326b212e | 654 | return br_switchdev_host_mdb(dev, mp, type); |
9776457c VO |
655 | |
656 | br_switchdev_mdb_populate(&mdb, mp); | |
657 | ||
658 | mdb.obj.orig_dev = pg->key.port->dev; | |
659 | switch (type) { | |
660 | case RTM_NEWMDB: | |
661 | complete_info = kmalloc(sizeof(*complete_info), GFP_ATOMIC); | |
662 | if (!complete_info) | |
663 | break; | |
664 | complete_info->port = pg->key.port; | |
665 | complete_info->ip = mp->addr; | |
666 | mdb.obj.complete_priv = complete_info; | |
326b212e | 667 | mdb.obj.complete = br_switchdev_mdb_complete; |
9776457c VO |
668 | if (switchdev_port_obj_add(pg->key.port->dev, &mdb.obj, NULL)) |
669 | kfree(complete_info); | |
670 | break; | |
671 | case RTM_DELMDB: | |
672 | switchdev_port_obj_del(pg->key.port->dev, &mdb.obj); | |
673 | break; | |
674 | } | |
675 | } | |
676 | #endif | |
677 | ||
326b212e VO |
678 | static int |
679 | br_switchdev_mdb_replay(struct net_device *br_dev, struct net_device *dev, | |
680 | const void *ctx, bool adding, struct notifier_block *nb, | |
681 | struct netlink_ext_ack *extack) | |
9776457c VO |
682 | { |
683 | #ifdef CONFIG_BRIDGE_IGMP_SNOOPING | |
684 | const struct net_bridge_mdb_entry *mp; | |
685 | struct switchdev_obj *obj, *tmp; | |
686 | struct net_bridge *br; | |
687 | unsigned long action; | |
688 | LIST_HEAD(mdb_list); | |
689 | int err = 0; | |
690 | ||
691 | ASSERT_RTNL(); | |
692 | ||
693 | if (!nb) | |
694 | return 0; | |
695 | ||
696 | if (!netif_is_bridge_master(br_dev) || !netif_is_bridge_port(dev)) | |
697 | return -EINVAL; | |
698 | ||
699 | br = netdev_priv(br_dev); | |
700 | ||
701 | if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) | |
702 | return 0; | |
703 | ||
dc489f86 TW |
704 | if (adding) |
705 | action = SWITCHDEV_PORT_OBJ_ADD; | |
706 | else | |
707 | action = SWITCHDEV_PORT_OBJ_DEL; | |
708 | ||
709 | /* br_switchdev_mdb_queue_one() will take care to not queue a | |
710 | * replay of an event that is already pending in the switchdev | |
711 | * deferred queue. In order to safely determine that, there | |
712 | * must be no new deferred MDB notifications enqueued for the | |
713 | * duration of the MDB scan. Therefore, grab the write-side | |
714 | * lock to avoid racing with any concurrent IGMP/MLD snooping. | |
9776457c | 715 | */ |
dc489f86 | 716 | spin_lock_bh(&br->multicast_lock); |
9776457c | 717 | |
dc489f86 | 718 | hlist_for_each_entry(mp, &br->mdb_list, mdb_node) { |
9776457c VO |
719 | struct net_bridge_port_group __rcu * const *pp; |
720 | const struct net_bridge_port_group *p; | |
721 | ||
722 | if (mp->host_joined) { | |
dc489f86 | 723 | err = br_switchdev_mdb_queue_one(&mdb_list, dev, action, |
326b212e VO |
724 | SWITCHDEV_OBJ_ID_HOST_MDB, |
725 | mp, br_dev); | |
9776457c | 726 | if (err) { |
dc489f86 | 727 | spin_unlock_bh(&br->multicast_lock); |
9776457c VO |
728 | goto out_free_mdb; |
729 | } | |
730 | } | |
731 | ||
dc489f86 | 732 | for (pp = &mp->ports; (p = mlock_dereference(*pp, br)) != NULL; |
9776457c VO |
733 | pp = &p->next) { |
734 | if (p->key.port->dev != dev) | |
735 | continue; | |
736 | ||
dc489f86 | 737 | err = br_switchdev_mdb_queue_one(&mdb_list, dev, action, |
326b212e VO |
738 | SWITCHDEV_OBJ_ID_PORT_MDB, |
739 | mp, dev); | |
9776457c | 740 | if (err) { |
dc489f86 | 741 | spin_unlock_bh(&br->multicast_lock); |
9776457c VO |
742 | goto out_free_mdb; |
743 | } | |
744 | } | |
745 | } | |
746 | ||
dc489f86 | 747 | spin_unlock_bh(&br->multicast_lock); |
9776457c VO |
748 | |
749 | list_for_each_entry(obj, &mdb_list, list) { | |
326b212e VO |
750 | err = br_switchdev_mdb_replay_one(nb, dev, |
751 | SWITCHDEV_OBJ_PORT_MDB(obj), | |
752 | action, ctx, extack); | |
989280d6 PM |
753 | if (err == -EOPNOTSUPP) |
754 | err = 0; | |
9776457c VO |
755 | if (err) |
756 | goto out_free_mdb; | |
757 | } | |
758 | ||
759 | out_free_mdb: | |
760 | list_for_each_entry_safe(obj, tmp, &mdb_list, list) { | |
761 | list_del(&obj->list); | |
762 | kfree(SWITCHDEV_OBJ_PORT_MDB(obj)); | |
763 | } | |
764 | ||
765 | if (err) | |
766 | return err; | |
767 | #endif | |
768 | ||
769 | return 0; | |
770 | } | |
771 | ||
4e51bf44 VO |
772 | static int nbp_switchdev_sync_objs(struct net_bridge_port *p, const void *ctx, |
773 | struct notifier_block *atomic_nb, | |
774 | struct notifier_block *blocking_nb, | |
775 | struct netlink_ext_ack *extack) | |
776 | { | |
777 | struct net_device *br_dev = p->br->dev; | |
778 | struct net_device *dev = p->dev; | |
779 | int err; | |
780 | ||
b28d580e | 781 | err = br_switchdev_vlan_replay(br_dev, ctx, true, blocking_nb, extack); |
4e51bf44 VO |
782 | if (err && err != -EOPNOTSUPP) |
783 | return err; | |
784 | ||
326b212e VO |
785 | err = br_switchdev_mdb_replay(br_dev, dev, ctx, true, blocking_nb, |
786 | extack); | |
989280d6 PM |
787 | if (err) { |
788 | /* -EOPNOTSUPP not propagated from MDB replay. */ | |
4e51bf44 | 789 | return err; |
989280d6 | 790 | } |
4e51bf44 | 791 | |
326b212e | 792 | err = br_switchdev_fdb_replay(br_dev, ctx, true, atomic_nb); |
4e51bf44 VO |
793 | if (err && err != -EOPNOTSUPP) |
794 | return err; | |
795 | ||
796 | return 0; | |
797 | } | |
798 | ||
799 | static void nbp_switchdev_unsync_objs(struct net_bridge_port *p, | |
800 | const void *ctx, | |
801 | struct notifier_block *atomic_nb, | |
802 | struct notifier_block *blocking_nb) | |
803 | { | |
804 | struct net_device *br_dev = p->br->dev; | |
805 | struct net_device *dev = p->dev; | |
806 | ||
263029ae | 807 | br_switchdev_fdb_replay(br_dev, ctx, false, atomic_nb); |
4e51bf44 | 808 | |
326b212e | 809 | br_switchdev_mdb_replay(br_dev, dev, ctx, false, blocking_nb, NULL); |
4e51bf44 | 810 | |
b28d580e | 811 | br_switchdev_vlan_replay(br_dev, ctx, false, blocking_nb, NULL); |
f7a70d65 TW |
812 | |
813 | /* Make sure that the device leaving this bridge has seen all | |
814 | * relevant events before it is disassociated. In the normal | |
815 | * case, when the device is directly attached to the bridge, | |
816 | * this is covered by del_nbp(). If the association was indirect | |
817 | * however, e.g. via a team or bond, and the device is leaving | |
818 | * that intermediate device, then the bridge port remains in | |
819 | * place. | |
820 | */ | |
821 | switchdev_deferred_process(); | |
4e51bf44 VO |
822 | } |
823 | ||
2f5dc00f VO |
824 | /* Let the bridge know that this port is offloaded, so that it can assign a |
825 | * switchdev hardware domain to it. | |
826 | */ | |
957e2235 VO |
827 | int br_switchdev_port_offload(struct net_bridge_port *p, |
828 | struct net_device *dev, const void *ctx, | |
829 | struct notifier_block *atomic_nb, | |
830 | struct notifier_block *blocking_nb, | |
831 | bool tx_fwd_offload, | |
832 | struct netlink_ext_ack *extack) | |
2f5dc00f VO |
833 | { |
834 | struct netdev_phys_item_id ppid; | |
2f5dc00f VO |
835 | int err; |
836 | ||
2f5dc00f VO |
837 | err = dev_get_port_parent_id(dev, &ppid, false); |
838 | if (err) | |
839 | return err; | |
840 | ||
47211192 | 841 | err = nbp_switchdev_add(p, ppid, tx_fwd_offload, extack); |
4e51bf44 VO |
842 | if (err) |
843 | return err; | |
844 | ||
845 | err = nbp_switchdev_sync_objs(p, ctx, atomic_nb, blocking_nb, extack); | |
846 | if (err) | |
847 | goto out_switchdev_del; | |
848 | ||
849 | return 0; | |
850 | ||
851 | out_switchdev_del: | |
852 | nbp_switchdev_del(p); | |
853 | ||
854 | return err; | |
2f5dc00f | 855 | } |
2f5dc00f | 856 | |
957e2235 VO |
857 | void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx, |
858 | struct notifier_block *atomic_nb, | |
859 | struct notifier_block *blocking_nb) | |
2f5dc00f | 860 | { |
4e51bf44 VO |
861 | nbp_switchdev_unsync_objs(p, ctx, atomic_nb, blocking_nb); |
862 | ||
2f5dc00f VO |
863 | nbp_switchdev_del(p); |
864 | } | |
f2e2857b PM |
865 | |
866 | int br_switchdev_port_replay(struct net_bridge_port *p, | |
867 | struct net_device *dev, const void *ctx, | |
868 | struct notifier_block *atomic_nb, | |
869 | struct notifier_block *blocking_nb, | |
870 | struct netlink_ext_ack *extack) | |
871 | { | |
872 | return nbp_switchdev_sync_objs(p, ctx, atomic_nb, blocking_nb, extack); | |
873 | } |