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>
8 #include <net/switchdev.h>
10 #include "br_private.h"
12 static struct static_key_false br_switchdev_tx_fwd_offload;
14 static bool nbp_switchdev_can_offload_tx_fwd(const struct net_bridge_port *p,
15 const struct sk_buff *skb)
17 if (!static_branch_unlikely(&br_switchdev_tx_fwd_offload))
20 return (p->flags & BR_TX_FWD_OFFLOAD) &&
21 (p->hwdom != BR_INPUT_SKB_CB(skb)->src_hwdom);
24 bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb)
26 if (!static_branch_unlikely(&br_switchdev_tx_fwd_offload))
29 return BR_INPUT_SKB_CB(skb)->tx_fwd_offload;
32 void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb)
34 skb->offload_fwd_mark = br_switchdev_frame_uses_tx_fwd_offload(skb);
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,
41 if (nbp_switchdev_can_offload_tx_fwd(p, skb))
42 BR_INPUT_SKB_CB(skb)->tx_fwd_offload = true;
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()
50 void nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
53 if (nbp_switchdev_can_offload_tx_fwd(p, skb))
54 set_bit(p->hwdom, &BR_INPUT_SKB_CB(skb)->fwd_hwdoms);
57 void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
61 BR_INPUT_SKB_CB(skb)->src_hwdom = p->hwdom;
64 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
65 const struct sk_buff *skb)
67 struct br_input_skb_cb *cb = BR_INPUT_SKB_CB(skb);
69 return !test_bit(p->hwdom, &cb->fwd_hwdoms) &&
70 (!skb->offload_fwd_mark || cb->src_hwdom != p->hwdom);
73 /* Flags that can be offloaded to hardware */
74 #define BR_PORT_FLAGS_HW_OFFLOAD (BR_LEARNING | BR_FLOOD | BR_PORT_MAB | \
75 BR_MCAST_FLOOD | BR_BCAST_FLOOD | BR_PORT_LOCKED | \
76 BR_HAIRPIN_MODE | BR_ISOLATED | BR_MULTICAST_TO_UNICAST)
78 int br_switchdev_set_port_flag(struct net_bridge_port *p,
81 struct netlink_ext_ack *extack)
83 struct switchdev_attr attr = {
86 struct switchdev_notifier_port_attr_info info = {
91 mask &= BR_PORT_FLAGS_HW_OFFLOAD;
95 attr.id = SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS;
96 attr.u.brport_flags.val = flags;
97 attr.u.brport_flags.mask = mask;
99 /* We run from atomic context here */
100 err = call_switchdev_notifiers(SWITCHDEV_PORT_ATTR_SET, p->dev,
102 err = notifier_to_errno(err);
103 if (err == -EOPNOTSUPP)
107 if (extack && !extack->_msg)
108 NL_SET_ERR_MSG_MOD(extack,
109 "bridge flag offload is not supported");
113 attr.id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS;
114 attr.flags = SWITCHDEV_F_DEFER;
116 err = switchdev_port_attr_set(p->dev, &attr, extack);
118 if (extack && !extack->_msg)
119 NL_SET_ERR_MSG_MOD(extack,
120 "error setting offload flag on port");
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,
132 const struct net_bridge_port *p = READ_ONCE(fdb->dst);
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;
145 br_switchdev_fdb_notify(struct net_bridge *br,
146 const struct net_bridge_fdb_entry *fdb, int type)
148 struct switchdev_notifier_fdb_info item;
150 if (test_bit(BR_FDB_LOCKED, &fdb->flags))
153 br_switchdev_fdb_populate(br, &item, fdb, NULL);
157 call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_DEVICE,
158 item.info.dev, &item.info, NULL);
161 call_switchdev_notifiers(SWITCHDEV_FDB_ADD_TO_DEVICE,
162 item.info.dev, &item.info, NULL);
167 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
168 bool changed, struct netlink_ext_ack *extack)
170 struct switchdev_obj_port_vlan v = {
172 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
178 return switchdev_port_obj_add(dev, &v.obj, extack);
181 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
183 struct switchdev_obj_port_vlan v = {
185 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
189 return switchdev_port_obj_del(dev, &v.obj);
192 static int nbp_switchdev_hwdom_set(struct net_bridge_port *joining)
194 struct net_bridge *br = joining->br;
195 struct net_bridge_port *p;
198 /* joining is yet to be added to the port list. */
199 list_for_each_entry(p, &br->port_list, list) {
200 if (netdev_phys_item_id_same(&joining->ppid, &p->ppid)) {
201 joining->hwdom = p->hwdom;
206 hwdom = find_next_zero_bit(&br->busy_hwdoms, BR_HWDOM_MAX, 1);
207 if (hwdom >= BR_HWDOM_MAX)
210 set_bit(hwdom, &br->busy_hwdoms);
211 joining->hwdom = hwdom;
215 static void nbp_switchdev_hwdom_put(struct net_bridge_port *leaving)
217 struct net_bridge *br = leaving->br;
218 struct net_bridge_port *p;
220 /* leaving is no longer in the port list. */
221 list_for_each_entry(p, &br->port_list, list) {
222 if (p->hwdom == leaving->hwdom)
226 clear_bit(leaving->hwdom, &br->busy_hwdoms);
229 static int nbp_switchdev_add(struct net_bridge_port *p,
230 struct netdev_phys_item_id ppid,
232 struct netlink_ext_ack *extack)
236 if (p->offload_count) {
237 /* Prevent unsupported configurations such as a bridge port
238 * which is a bonding interface, and the member ports are from
239 * different hardware switches.
241 if (!netdev_phys_item_id_same(&p->ppid, &ppid)) {
242 NL_SET_ERR_MSG_MOD(extack,
243 "Same bridge port cannot be offloaded by two physical switches");
247 /* Tolerate drivers that call switchdev_bridge_port_offload()
248 * more than once for the same bridge port, such as when the
249 * bridge port is an offloaded bonding/team interface.
257 p->offload_count = 1;
259 err = nbp_switchdev_hwdom_set(p);
263 if (tx_fwd_offload) {
264 p->flags |= BR_TX_FWD_OFFLOAD;
265 static_branch_inc(&br_switchdev_tx_fwd_offload);
271 static void nbp_switchdev_del(struct net_bridge_port *p)
273 if (WARN_ON(!p->offload_count))
278 if (p->offload_count)
282 nbp_switchdev_hwdom_put(p);
284 if (p->flags & BR_TX_FWD_OFFLOAD) {
285 p->flags &= ~BR_TX_FWD_OFFLOAD;
286 static_branch_dec(&br_switchdev_tx_fwd_offload);
291 br_switchdev_fdb_replay_one(struct net_bridge *br, struct notifier_block *nb,
292 const struct net_bridge_fdb_entry *fdb,
293 unsigned long action, const void *ctx)
295 struct switchdev_notifier_fdb_info item;
298 br_switchdev_fdb_populate(br, &item, fdb, ctx);
300 err = nb->notifier_call(nb, action, &item);
301 return notifier_to_errno(err);
305 br_switchdev_fdb_replay(const struct net_device *br_dev, const void *ctx,
306 bool adding, struct notifier_block *nb)
308 struct net_bridge_fdb_entry *fdb;
309 struct net_bridge *br;
310 unsigned long action;
316 if (!netif_is_bridge_master(br_dev))
319 br = netdev_priv(br_dev);
322 action = SWITCHDEV_FDB_ADD_TO_DEVICE;
324 action = SWITCHDEV_FDB_DEL_TO_DEVICE;
328 hlist_for_each_entry_rcu(fdb, &br->fdb_list, fdb_node) {
329 err = br_switchdev_fdb_replay_one(br, nb, fdb, action, ctx);
339 static int br_switchdev_vlan_attr_replay(struct net_device *br_dev,
341 struct notifier_block *nb,
342 struct netlink_ext_ack *extack)
344 struct switchdev_notifier_port_attr_info attr_info = {
351 struct net_bridge *br = netdev_priv(br_dev);
352 struct net_bridge_vlan_group *vg;
353 struct switchdev_attr attr;
354 struct net_bridge_vlan *v;
357 attr_info.attr = &attr;
358 attr.orig_dev = br_dev;
360 vg = br_vlan_group(br);
364 list_for_each_entry(v, &vg->vlan_list, vlist) {
366 attr.id = SWITCHDEV_ATTR_ID_VLAN_MSTI;
367 attr.u.vlan_msti.vid = v->vid;
368 attr.u.vlan_msti.msti = v->msti;
370 err = nb->notifier_call(nb, SWITCHDEV_PORT_ATTR_SET,
372 err = notifier_to_errno(err);
382 br_switchdev_vlan_replay_one(struct notifier_block *nb,
383 struct net_device *dev,
384 struct switchdev_obj_port_vlan *vlan,
385 const void *ctx, unsigned long action,
386 struct netlink_ext_ack *extack)
388 struct switchdev_notifier_port_obj_info obj_info = {
398 err = nb->notifier_call(nb, action, &obj_info);
399 return notifier_to_errno(err);
402 static int br_switchdev_vlan_replay_group(struct notifier_block *nb,
403 struct net_device *dev,
404 struct net_bridge_vlan_group *vg,
405 const void *ctx, unsigned long action,
406 struct netlink_ext_ack *extack)
408 struct net_bridge_vlan *v;
415 pvid = br_get_pvid(vg);
417 list_for_each_entry(v, &vg->vlan_list, vlist) {
418 struct switchdev_obj_port_vlan vlan = {
420 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
421 .flags = br_vlan_flags(v, pvid),
425 if (!br_vlan_should_use(v))
428 err = br_switchdev_vlan_replay_one(nb, dev, &vlan, ctx,
437 static int br_switchdev_vlan_replay(struct net_device *br_dev,
438 const void *ctx, bool adding,
439 struct notifier_block *nb,
440 struct netlink_ext_ack *extack)
442 struct net_bridge *br = netdev_priv(br_dev);
443 struct net_bridge_port *p;
444 unsigned long action;
452 if (!netif_is_bridge_master(br_dev))
456 action = SWITCHDEV_PORT_OBJ_ADD;
458 action = SWITCHDEV_PORT_OBJ_DEL;
460 err = br_switchdev_vlan_replay_group(nb, br_dev, br_vlan_group(br),
461 ctx, action, extack);
465 list_for_each_entry(p, &br->port_list, list) {
466 struct net_device *dev = p->dev;
468 err = br_switchdev_vlan_replay_group(nb, dev,
470 ctx, action, extack);
476 err = br_switchdev_vlan_attr_replay(br_dev, ctx, nb, extack);
484 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
485 struct br_switchdev_mdb_complete_info {
486 struct net_bridge_port *port;
490 static void br_switchdev_mdb_complete(struct net_device *dev, int err, void *priv)
492 struct br_switchdev_mdb_complete_info *data = priv;
493 struct net_bridge_port_group __rcu **pp;
494 struct net_bridge_port_group *p;
495 struct net_bridge_mdb_entry *mp;
496 struct net_bridge_port *port = data->port;
497 struct net_bridge *br = port->br;
502 spin_lock_bh(&br->multicast_lock);
503 mp = br_mdb_ip_get(br, &data->ip);
506 for (pp = &mp->ports; (p = mlock_dereference(*pp, br)) != NULL;
508 if (p->key.port != port)
510 p->flags |= MDB_PG_FLAGS_OFFLOAD;
513 spin_unlock_bh(&br->multicast_lock);
518 static void br_switchdev_mdb_populate(struct switchdev_obj_port_mdb *mdb,
519 const struct net_bridge_mdb_entry *mp)
521 if (mp->addr.proto == htons(ETH_P_IP))
522 ip_eth_mc_map(mp->addr.dst.ip4, mdb->addr);
523 #if IS_ENABLED(CONFIG_IPV6)
524 else if (mp->addr.proto == htons(ETH_P_IPV6))
525 ipv6_eth_mc_map(&mp->addr.dst.ip6, mdb->addr);
528 ether_addr_copy(mdb->addr, mp->addr.dst.mac_addr);
530 mdb->vid = mp->addr.vid;
533 static void br_switchdev_host_mdb_one(struct net_device *dev,
534 struct net_device *lower_dev,
535 struct net_bridge_mdb_entry *mp,
538 struct switchdev_obj_port_mdb mdb = {
540 .id = SWITCHDEV_OBJ_ID_HOST_MDB,
541 .flags = SWITCHDEV_F_DEFER,
546 br_switchdev_mdb_populate(&mdb, mp);
550 switchdev_port_obj_add(lower_dev, &mdb.obj, NULL);
553 switchdev_port_obj_del(lower_dev, &mdb.obj);
558 static void br_switchdev_host_mdb(struct net_device *dev,
559 struct net_bridge_mdb_entry *mp, int type)
561 struct net_device *lower_dev;
562 struct list_head *iter;
564 netdev_for_each_lower_dev(dev, lower_dev, iter)
565 br_switchdev_host_mdb_one(dev, lower_dev, mp, type);
569 br_switchdev_mdb_replay_one(struct notifier_block *nb, struct net_device *dev,
570 const struct switchdev_obj_port_mdb *mdb,
571 unsigned long action, const void *ctx,
572 struct netlink_ext_ack *extack)
574 struct switchdev_notifier_port_obj_info obj_info = {
584 err = nb->notifier_call(nb, action, &obj_info);
585 return notifier_to_errno(err);
588 static int br_switchdev_mdb_queue_one(struct list_head *mdb_list,
589 enum switchdev_obj_id id,
590 const struct net_bridge_mdb_entry *mp,
591 struct net_device *orig_dev)
593 struct switchdev_obj_port_mdb *mdb;
595 mdb = kzalloc(sizeof(*mdb), GFP_ATOMIC);
600 mdb->obj.orig_dev = orig_dev;
601 br_switchdev_mdb_populate(mdb, mp);
602 list_add_tail(&mdb->obj.list, mdb_list);
607 void br_switchdev_mdb_notify(struct net_device *dev,
608 struct net_bridge_mdb_entry *mp,
609 struct net_bridge_port_group *pg,
612 struct br_switchdev_mdb_complete_info *complete_info;
613 struct switchdev_obj_port_mdb mdb = {
615 .id = SWITCHDEV_OBJ_ID_PORT_MDB,
616 .flags = SWITCHDEV_F_DEFER,
621 return br_switchdev_host_mdb(dev, mp, type);
623 br_switchdev_mdb_populate(&mdb, mp);
625 mdb.obj.orig_dev = pg->key.port->dev;
628 complete_info = kmalloc(sizeof(*complete_info), GFP_ATOMIC);
631 complete_info->port = pg->key.port;
632 complete_info->ip = mp->addr;
633 mdb.obj.complete_priv = complete_info;
634 mdb.obj.complete = br_switchdev_mdb_complete;
635 if (switchdev_port_obj_add(pg->key.port->dev, &mdb.obj, NULL))
636 kfree(complete_info);
639 switchdev_port_obj_del(pg->key.port->dev, &mdb.obj);
646 br_switchdev_mdb_replay(struct net_device *br_dev, struct net_device *dev,
647 const void *ctx, bool adding, struct notifier_block *nb,
648 struct netlink_ext_ack *extack)
650 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
651 const struct net_bridge_mdb_entry *mp;
652 struct switchdev_obj *obj, *tmp;
653 struct net_bridge *br;
654 unsigned long action;
663 if (!netif_is_bridge_master(br_dev) || !netif_is_bridge_port(dev))
666 br = netdev_priv(br_dev);
668 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
671 /* We cannot walk over br->mdb_list protected just by the rtnl_mutex,
672 * because the write-side protection is br->multicast_lock. But we
673 * need to emulate the [ blocking ] calling context of a regular
674 * switchdev event, so since both br->multicast_lock and RCU read side
675 * critical sections are atomic, we have no choice but to pick the RCU
676 * read side lock, queue up all our events, leave the critical section
677 * and notify switchdev from blocking context.
681 hlist_for_each_entry_rcu(mp, &br->mdb_list, mdb_node) {
682 struct net_bridge_port_group __rcu * const *pp;
683 const struct net_bridge_port_group *p;
685 if (mp->host_joined) {
686 err = br_switchdev_mdb_queue_one(&mdb_list,
687 SWITCHDEV_OBJ_ID_HOST_MDB,
695 for (pp = &mp->ports; (p = rcu_dereference(*pp)) != NULL;
697 if (p->key.port->dev != dev)
700 err = br_switchdev_mdb_queue_one(&mdb_list,
701 SWITCHDEV_OBJ_ID_PORT_MDB,
713 action = SWITCHDEV_PORT_OBJ_ADD;
715 action = SWITCHDEV_PORT_OBJ_DEL;
717 list_for_each_entry(obj, &mdb_list, list) {
718 err = br_switchdev_mdb_replay_one(nb, dev,
719 SWITCHDEV_OBJ_PORT_MDB(obj),
720 action, ctx, extack);
726 list_for_each_entry_safe(obj, tmp, &mdb_list, list) {
727 list_del(&obj->list);
728 kfree(SWITCHDEV_OBJ_PORT_MDB(obj));
738 static int nbp_switchdev_sync_objs(struct net_bridge_port *p, const void *ctx,
739 struct notifier_block *atomic_nb,
740 struct notifier_block *blocking_nb,
741 struct netlink_ext_ack *extack)
743 struct net_device *br_dev = p->br->dev;
744 struct net_device *dev = p->dev;
747 err = br_switchdev_vlan_replay(br_dev, ctx, true, blocking_nb, extack);
748 if (err && err != -EOPNOTSUPP)
751 err = br_switchdev_mdb_replay(br_dev, dev, ctx, true, blocking_nb,
753 if (err && err != -EOPNOTSUPP)
756 err = br_switchdev_fdb_replay(br_dev, ctx, true, atomic_nb);
757 if (err && err != -EOPNOTSUPP)
763 static void nbp_switchdev_unsync_objs(struct net_bridge_port *p,
765 struct notifier_block *atomic_nb,
766 struct notifier_block *blocking_nb)
768 struct net_device *br_dev = p->br->dev;
769 struct net_device *dev = p->dev;
771 br_switchdev_fdb_replay(br_dev, ctx, false, atomic_nb);
773 br_switchdev_mdb_replay(br_dev, dev, ctx, false, blocking_nb, NULL);
775 br_switchdev_vlan_replay(br_dev, ctx, false, blocking_nb, NULL);
778 /* Let the bridge know that this port is offloaded, so that it can assign a
779 * switchdev hardware domain to it.
781 int br_switchdev_port_offload(struct net_bridge_port *p,
782 struct net_device *dev, const void *ctx,
783 struct notifier_block *atomic_nb,
784 struct notifier_block *blocking_nb,
786 struct netlink_ext_ack *extack)
788 struct netdev_phys_item_id ppid;
791 err = dev_get_port_parent_id(dev, &ppid, false);
795 err = nbp_switchdev_add(p, ppid, tx_fwd_offload, extack);
799 err = nbp_switchdev_sync_objs(p, ctx, atomic_nb, blocking_nb, extack);
801 goto out_switchdev_del;
806 nbp_switchdev_del(p);
811 void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
812 struct notifier_block *atomic_nb,
813 struct notifier_block *blocking_nb)
815 nbp_switchdev_unsync_objs(p, ctx, atomic_nb, blocking_nb);
817 nbp_switchdev_del(p);