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/switchdev.h>
9 #include "br_private.h"
11 static struct static_key_false br_switchdev_tx_fwd_offload;
13 static bool nbp_switchdev_can_offload_tx_fwd(const struct net_bridge_port *p,
14 const struct sk_buff *skb)
16 if (!static_branch_unlikely(&br_switchdev_tx_fwd_offload))
19 return (p->flags & BR_TX_FWD_OFFLOAD) &&
20 (p->hwdom != BR_INPUT_SKB_CB(skb)->src_hwdom);
23 bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb)
25 if (!static_branch_unlikely(&br_switchdev_tx_fwd_offload))
28 return BR_INPUT_SKB_CB(skb)->tx_fwd_offload;
31 void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb)
33 skb->offload_fwd_mark = br_switchdev_frame_uses_tx_fwd_offload(skb);
36 /* Mark the frame for TX forwarding offload if this egress port supports it */
37 void nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
40 if (nbp_switchdev_can_offload_tx_fwd(p, skb))
41 BR_INPUT_SKB_CB(skb)->tx_fwd_offload = true;
44 /* Lazily adds the hwdom of the egress bridge port to the bit mask of hwdoms
45 * that the skb has been already forwarded to, to avoid further cloning to
46 * other ports in the same hwdom by making nbp_switchdev_allowed_egress()
49 void nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
52 if (nbp_switchdev_can_offload_tx_fwd(p, skb))
53 set_bit(p->hwdom, &BR_INPUT_SKB_CB(skb)->fwd_hwdoms);
56 void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
60 BR_INPUT_SKB_CB(skb)->src_hwdom = p->hwdom;
63 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
64 const struct sk_buff *skb)
66 struct br_input_skb_cb *cb = BR_INPUT_SKB_CB(skb);
68 return !test_bit(p->hwdom, &cb->fwd_hwdoms) &&
69 (!skb->offload_fwd_mark || cb->src_hwdom != p->hwdom);
72 /* Flags that can be offloaded to hardware */
73 #define BR_PORT_FLAGS_HW_OFFLOAD (BR_LEARNING | BR_FLOOD | \
74 BR_MCAST_FLOOD | BR_BCAST_FLOOD)
76 int br_switchdev_set_port_flag(struct net_bridge_port *p,
79 struct netlink_ext_ack *extack)
81 struct switchdev_attr attr = {
84 struct switchdev_notifier_port_attr_info info = {
89 mask &= BR_PORT_FLAGS_HW_OFFLOAD;
93 attr.id = SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS;
94 attr.u.brport_flags.val = flags;
95 attr.u.brport_flags.mask = mask;
97 /* We run from atomic context here */
98 err = call_switchdev_notifiers(SWITCHDEV_PORT_ATTR_SET, p->dev,
100 err = notifier_to_errno(err);
101 if (err == -EOPNOTSUPP)
105 if (extack && !extack->_msg)
106 NL_SET_ERR_MSG_MOD(extack,
107 "bridge flag offload is not supported");
111 attr.id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS;
112 attr.flags = SWITCHDEV_F_DEFER;
114 err = switchdev_port_attr_set(p->dev, &attr, extack);
116 if (extack && !extack->_msg)
117 NL_SET_ERR_MSG_MOD(extack,
118 "error setting offload flag on port");
126 br_switchdev_fdb_notify(struct net_bridge *br,
127 const struct net_bridge_fdb_entry *fdb, int type)
129 const struct net_bridge_port *dst = READ_ONCE(fdb->dst);
130 struct switchdev_notifier_fdb_info info = {
131 .addr = fdb->key.addr.addr,
132 .vid = fdb->key.vlan_id,
133 .added_by_user = test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags),
134 .is_local = test_bit(BR_FDB_LOCAL, &fdb->flags),
135 .offloaded = test_bit(BR_FDB_OFFLOADED, &fdb->flags),
137 struct net_device *dev = (!dst || info.is_local) ? br->dev : dst->dev;
141 call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_DEVICE,
142 dev, &info.info, NULL);
145 call_switchdev_notifiers(SWITCHDEV_FDB_ADD_TO_DEVICE,
146 dev, &info.info, NULL);
151 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
152 struct netlink_ext_ack *extack)
154 struct switchdev_obj_port_vlan v = {
156 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
161 return switchdev_port_obj_add(dev, &v.obj, extack);
164 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
166 struct switchdev_obj_port_vlan v = {
168 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
172 return switchdev_port_obj_del(dev, &v.obj);
175 static int nbp_switchdev_hwdom_set(struct net_bridge_port *joining)
177 struct net_bridge *br = joining->br;
178 struct net_bridge_port *p;
181 /* joining is yet to be added to the port list. */
182 list_for_each_entry(p, &br->port_list, list) {
183 if (netdev_phys_item_id_same(&joining->ppid, &p->ppid)) {
184 joining->hwdom = p->hwdom;
189 hwdom = find_next_zero_bit(&br->busy_hwdoms, BR_HWDOM_MAX, 1);
190 if (hwdom >= BR_HWDOM_MAX)
193 set_bit(hwdom, &br->busy_hwdoms);
194 joining->hwdom = hwdom;
198 static void nbp_switchdev_hwdom_put(struct net_bridge_port *leaving)
200 struct net_bridge *br = leaving->br;
201 struct net_bridge_port *p;
203 /* leaving is no longer in the port list. */
204 list_for_each_entry(p, &br->port_list, list) {
205 if (p->hwdom == leaving->hwdom)
209 clear_bit(leaving->hwdom, &br->busy_hwdoms);
212 static int nbp_switchdev_add(struct net_bridge_port *p,
213 struct netdev_phys_item_id ppid,
215 struct netlink_ext_ack *extack)
219 if (p->offload_count) {
220 /* Prevent unsupported configurations such as a bridge port
221 * which is a bonding interface, and the member ports are from
222 * different hardware switches.
224 if (!netdev_phys_item_id_same(&p->ppid, &ppid)) {
225 NL_SET_ERR_MSG_MOD(extack,
226 "Same bridge port cannot be offloaded by two physical switches");
230 /* Tolerate drivers that call switchdev_bridge_port_offload()
231 * more than once for the same bridge port, such as when the
232 * bridge port is an offloaded bonding/team interface.
240 p->offload_count = 1;
242 err = nbp_switchdev_hwdom_set(p);
246 if (tx_fwd_offload) {
247 p->flags |= BR_TX_FWD_OFFLOAD;
248 static_branch_inc(&br_switchdev_tx_fwd_offload);
254 static void nbp_switchdev_del(struct net_bridge_port *p)
256 if (WARN_ON(!p->offload_count))
261 if (p->offload_count)
265 nbp_switchdev_hwdom_put(p);
267 if (p->flags & BR_TX_FWD_OFFLOAD) {
268 p->flags &= ~BR_TX_FWD_OFFLOAD;
269 static_branch_dec(&br_switchdev_tx_fwd_offload);
273 static int nbp_switchdev_sync_objs(struct net_bridge_port *p, const void *ctx,
274 struct notifier_block *atomic_nb,
275 struct notifier_block *blocking_nb,
276 struct netlink_ext_ack *extack)
278 struct net_device *br_dev = p->br->dev;
279 struct net_device *dev = p->dev;
282 err = br_vlan_replay(br_dev, dev, ctx, true, blocking_nb, extack);
283 if (err && err != -EOPNOTSUPP)
286 err = br_mdb_replay(br_dev, dev, ctx, true, blocking_nb, extack);
287 if (err && err != -EOPNOTSUPP)
290 err = br_fdb_replay(br_dev, ctx, true, atomic_nb);
291 if (err && err != -EOPNOTSUPP)
297 static void nbp_switchdev_unsync_objs(struct net_bridge_port *p,
299 struct notifier_block *atomic_nb,
300 struct notifier_block *blocking_nb)
302 struct net_device *br_dev = p->br->dev;
303 struct net_device *dev = p->dev;
305 br_vlan_replay(br_dev, dev, ctx, false, blocking_nb, NULL);
307 br_mdb_replay(br_dev, dev, ctx, false, blocking_nb, NULL);
309 br_fdb_replay(br_dev, ctx, false, atomic_nb);
312 /* Let the bridge know that this port is offloaded, so that it can assign a
313 * switchdev hardware domain to it.
315 int br_switchdev_port_offload(struct net_bridge_port *p,
316 struct net_device *dev, const void *ctx,
317 struct notifier_block *atomic_nb,
318 struct notifier_block *blocking_nb,
320 struct netlink_ext_ack *extack)
322 struct netdev_phys_item_id ppid;
325 err = dev_get_port_parent_id(dev, &ppid, false);
329 err = nbp_switchdev_add(p, ppid, tx_fwd_offload, extack);
333 err = nbp_switchdev_sync_objs(p, ctx, atomic_nb, blocking_nb, extack);
335 goto out_switchdev_del;
340 nbp_switchdev_del(p);
345 void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
346 struct notifier_block *atomic_nb,
347 struct notifier_block *blocking_nb)
349 nbp_switchdev_unsync_objs(p, ctx, atomic_nb, blocking_nb);
351 nbp_switchdev_del(p);