Debian Codegraph @

net/bridge/br_switchdev.c

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