blob: 6bf518d78f0292d796fcc4ca21703902c33b618a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ido Schimmel6bc506b2016-08-25 18:42:37 +02002#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>
8
9#include "br_private.h"
10
Tobias Waldekranz47211192021-07-22 18:55:38 +030011static struct static_key_false br_switchdev_tx_fwd_offload;
12
13static bool nbp_switchdev_can_offload_tx_fwd(const struct net_bridge_port *p,
14 const struct sk_buff *skb)
15{
16 if (!static_branch_unlikely(&br_switchdev_tx_fwd_offload))
17 return false;
18
19 return (p->flags & BR_TX_FWD_OFFLOAD) &&
20 (p->hwdom != BR_INPUT_SKB_CB(skb)->src_hwdom);
21}
22
23bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb)
24{
25 if (!static_branch_unlikely(&br_switchdev_tx_fwd_offload))
26 return false;
27
28 return BR_INPUT_SKB_CB(skb)->tx_fwd_offload;
29}
30
Vladimir Olteanc5381152021-07-23 23:49:11 +030031void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb)
32{
33 skb->offload_fwd_mark = br_switchdev_frame_uses_tx_fwd_offload(skb);
34}
35
Tobias Waldekranz47211192021-07-22 18:55:38 +030036/* Mark the frame for TX forwarding offload if this egress port supports it */
37void nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
38 struct sk_buff *skb)
39{
40 if (nbp_switchdev_can_offload_tx_fwd(p, skb))
41 BR_INPUT_SKB_CB(skb)->tx_fwd_offload = true;
42}
43
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()
47 * return false.
48 */
49void nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
50 struct sk_buff *skb)
51{
52 if (nbp_switchdev_can_offload_tx_fwd(p, skb))
53 set_bit(p->hwdom, &BR_INPUT_SKB_CB(skb)->fwd_hwdoms);
54}
55
Ido Schimmel6bc506b2016-08-25 18:42:37 +020056void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
57 struct sk_buff *skb)
58{
Tobias Waldekranzf7cf9722021-07-21 19:23:59 +030059 if (p->hwdom)
60 BR_INPUT_SKB_CB(skb)->src_hwdom = p->hwdom;
Ido Schimmel6bc506b2016-08-25 18:42:37 +020061}
62
63bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
64 const struct sk_buff *skb)
65{
Tobias Waldekranz47211192021-07-22 18:55:38 +030066 struct br_input_skb_cb *cb = BR_INPUT_SKB_CB(skb);
67
68 return !test_bit(p->hwdom, &cb->fwd_hwdoms) &&
69 (!skb->offload_fwd_mark || cb->src_hwdom != p->hwdom);
Ido Schimmel6bc506b2016-08-25 18:42:37 +020070}
Arkadi Sharshevsky39222852017-06-08 08:44:11 +020071
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)
75
76int br_switchdev_set_port_flag(struct net_bridge_port *p,
77 unsigned long flags,
Vladimir Oltean078bbb82021-02-12 17:15:53 +020078 unsigned long mask,
79 struct netlink_ext_ack *extack)
Arkadi Sharshevsky39222852017-06-08 08:44:11 +020080{
81 struct switchdev_attr attr = {
82 .orig_dev = p->dev,
Arkadi Sharshevsky39222852017-06-08 08:44:11 +020083 };
Florian Fainellid45224d2019-02-27 11:44:31 -080084 struct switchdev_notifier_port_attr_info info = {
85 .attr = &attr,
86 };
Arkadi Sharshevsky39222852017-06-08 08:44:11 +020087 int err;
88
Vladimir Oltean304ae3b2021-02-12 17:15:52 +020089 mask &= BR_PORT_FLAGS_HW_OFFLOAD;
90 if (!mask)
Arkadi Sharshevsky39222852017-06-08 08:44:11 +020091 return 0;
92
Vladimir Olteane18f4c12021-02-12 17:15:55 +020093 attr.id = SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS;
94 attr.u.brport_flags.val = flags;
95 attr.u.brport_flags.mask = mask;
Vladimir Oltean304ae3b2021-02-12 17:15:52 +020096
Florian Fainellid45224d2019-02-27 11:44:31 -080097 /* We run from atomic context here */
98 err = call_switchdev_notifiers(SWITCHDEV_PORT_ATTR_SET, p->dev,
Vladimir Oltean078bbb82021-02-12 17:15:53 +020099 &info.info, extack);
Florian Fainellid45224d2019-02-27 11:44:31 -0800100 err = notifier_to_errno(err);
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200101 if (err == -EOPNOTSUPP)
102 return 0;
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200103
Florian Fainelli1ef07642019-02-20 16:58:24 -0800104 if (err) {
Vladimir Oltean078bbb82021-02-12 17:15:53 +0200105 if (extack && !extack->_msg)
106 NL_SET_ERR_MSG_MOD(extack,
107 "bridge flag offload is not supported");
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200108 return -EOPNOTSUPP;
109 }
110
111 attr.id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS;
112 attr.flags = SWITCHDEV_F_DEFER;
Florian Fainelli1ef07642019-02-20 16:58:24 -0800113
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200114 err = switchdev_port_attr_set(p->dev, &attr, extack);
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200115 if (err) {
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200116 if (extack && !extack->_msg)
117 NL_SET_ERR_MSG_MOD(extack,
118 "error setting offload flag on port");
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200119 return err;
120 }
121
122 return 0;
123}
Arkadi Sharshevsky6b26b512017-06-08 08:44:14 +0200124
Arkadi Sharshevsky6b26b512017-06-08 08:44:14 +0200125void
Tobias Waldekranz6eb38bf2021-06-29 17:06:45 +0300126br_switchdev_fdb_notify(struct net_bridge *br,
127 const struct net_bridge_fdb_entry *fdb, int type)
Arkadi Sharshevsky6b26b512017-06-08 08:44:14 +0200128{
Vladimir Oltean3e19ae72021-06-29 17:06:44 +0300129 const struct net_bridge_port *dst = READ_ONCE(fdb->dst);
Tobias Waldekranze5b4b892021-04-14 19:52:55 +0300130 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),
Vladimir Oltean2c4eca32021-04-14 19:52:56 +0300134 .is_local = test_bit(BR_FDB_LOCAL, &fdb->flags),
Tobias Waldekranze5b4b892021-04-14 19:52:55 +0300135 .offloaded = test_bit(BR_FDB_OFFLOADED, &fdb->flags),
136 };
Vladimir Oltean2e19bb32021-08-02 14:36:33 +0300137 struct net_device *dev = (!dst || info.is_local) ? br->dev : dst->dev;
Tobias Waldekranze5b4b892021-04-14 19:52:55 +0300138
Arkadi Sharshevsky6b26b512017-06-08 08:44:14 +0200139 switch (type) {
140 case RTM_DELNEIGH:
Tobias Waldekranze5b4b892021-04-14 19:52:55 +0300141 call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_DEVICE,
Tobias Waldekranz6eb38bf2021-06-29 17:06:45 +0300142 dev, &info.info, NULL);
Arkadi Sharshevsky6b26b512017-06-08 08:44:14 +0200143 break;
144 case RTM_NEWNEIGH:
Tobias Waldekranze5b4b892021-04-14 19:52:55 +0300145 call_switchdev_notifiers(SWITCHDEV_FDB_ADD_TO_DEVICE,
Tobias Waldekranz6eb38bf2021-06-29 17:06:45 +0300146 dev, &info.info, NULL);
Arkadi Sharshevsky6b26b512017-06-08 08:44:14 +0200147 break;
148 }
149}
Petr Machatad66e4342018-05-30 02:56:03 +0200150
Petr Machata169327d2018-12-12 17:02:50 +0000151int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
152 struct netlink_ext_ack *extack)
Petr Machatad66e4342018-05-30 02:56:03 +0200153{
154 struct switchdev_obj_port_vlan v = {
155 .obj.orig_dev = dev,
156 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
157 .flags = flags,
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +0200158 .vid = vid,
Petr Machatad66e4342018-05-30 02:56:03 +0200159 };
160
Petr Machata69b73202018-12-12 17:02:52 +0000161 return switchdev_port_obj_add(dev, &v.obj, extack);
Petr Machatad66e4342018-05-30 02:56:03 +0200162}
163
164int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
165{
166 struct switchdev_obj_port_vlan v = {
167 .obj.orig_dev = dev,
168 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +0200169 .vid = vid,
Petr Machatad66e4342018-05-30 02:56:03 +0200170 };
171
172 return switchdev_port_obj_del(dev, &v.obj);
173}
Tobias Waldekranz85826612021-07-21 19:24:00 +0300174
175static int nbp_switchdev_hwdom_set(struct net_bridge_port *joining)
176{
177 struct net_bridge *br = joining->br;
178 struct net_bridge_port *p;
179 int hwdom;
180
181 /* joining is yet to be added to the port list. */
182 list_for_each_entry(p, &br->port_list, list) {
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300183 if (netdev_phys_item_id_same(&joining->ppid, &p->ppid)) {
Tobias Waldekranz85826612021-07-21 19:24:00 +0300184 joining->hwdom = p->hwdom;
185 return 0;
186 }
187 }
188
189 hwdom = find_next_zero_bit(&br->busy_hwdoms, BR_HWDOM_MAX, 1);
190 if (hwdom >= BR_HWDOM_MAX)
191 return -EBUSY;
192
193 set_bit(hwdom, &br->busy_hwdoms);
194 joining->hwdom = hwdom;
195 return 0;
196}
197
198static void nbp_switchdev_hwdom_put(struct net_bridge_port *leaving)
199{
200 struct net_bridge *br = leaving->br;
201 struct net_bridge_port *p;
202
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)
206 return;
207 }
208
209 clear_bit(leaving->hwdom, &br->busy_hwdoms);
210}
211
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300212static int nbp_switchdev_add(struct net_bridge_port *p,
213 struct netdev_phys_item_id ppid,
Tobias Waldekranz47211192021-07-22 18:55:38 +0300214 bool tx_fwd_offload,
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300215 struct netlink_ext_ack *extack)
Tobias Waldekranz85826612021-07-21 19:24:00 +0300216{
Tobias Waldekranz47211192021-07-22 18:55:38 +0300217 int err;
218
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300219 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.
223 */
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");
227 return -EBUSY;
228 }
Tobias Waldekranz85826612021-07-21 19:24:00 +0300229
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300230 /* 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.
233 */
234 p->offload_count++;
Tobias Waldekranz85826612021-07-21 19:24:00 +0300235
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300236 return 0;
Tobias Waldekranz85826612021-07-21 19:24:00 +0300237 }
238
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300239 p->ppid = ppid;
240 p->offload_count = 1;
241
Tobias Waldekranz47211192021-07-22 18:55:38 +0300242 err = nbp_switchdev_hwdom_set(p);
243 if (err)
244 return err;
245
246 if (tx_fwd_offload) {
247 p->flags |= BR_TX_FWD_OFFLOAD;
248 static_branch_inc(&br_switchdev_tx_fwd_offload);
249 }
250
251 return 0;
Tobias Waldekranz85826612021-07-21 19:24:00 +0300252}
253
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300254static void nbp_switchdev_del(struct net_bridge_port *p)
Tobias Waldekranz85826612021-07-21 19:24:00 +0300255{
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300256 if (WARN_ON(!p->offload_count))
257 return;
258
259 p->offload_count--;
260
261 if (p->offload_count)
262 return;
Tobias Waldekranz85826612021-07-21 19:24:00 +0300263
264 if (p->hwdom)
265 nbp_switchdev_hwdom_put(p);
Tobias Waldekranz47211192021-07-22 18:55:38 +0300266
267 if (p->flags & BR_TX_FWD_OFFLOAD) {
268 p->flags &= ~BR_TX_FWD_OFFLOAD;
269 static_branch_dec(&br_switchdev_tx_fwd_offload);
270 }
Tobias Waldekranz85826612021-07-21 19:24:00 +0300271}
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300272
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300273static 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)
277{
278 struct net_device *br_dev = p->br->dev;
279 struct net_device *dev = p->dev;
280 int err;
281
282 err = br_vlan_replay(br_dev, dev, ctx, true, blocking_nb, extack);
283 if (err && err != -EOPNOTSUPP)
284 return err;
285
286 err = br_mdb_replay(br_dev, dev, ctx, true, blocking_nb, extack);
287 if (err && err != -EOPNOTSUPP)
288 return err;
289
Vladimir Olteanb4454bc2021-07-28 21:27:47 +0300290 err = br_fdb_replay(br_dev, ctx, true, atomic_nb);
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300291 if (err && err != -EOPNOTSUPP)
292 return err;
293
294 return 0;
295}
296
297static void nbp_switchdev_unsync_objs(struct net_bridge_port *p,
298 const void *ctx,
299 struct notifier_block *atomic_nb,
300 struct notifier_block *blocking_nb)
301{
302 struct net_device *br_dev = p->br->dev;
303 struct net_device *dev = p->dev;
304
305 br_vlan_replay(br_dev, dev, ctx, false, blocking_nb, NULL);
306
307 br_mdb_replay(br_dev, dev, ctx, false, blocking_nb, NULL);
308
Vladimir Olteanb4454bc2021-07-28 21:27:47 +0300309 br_fdb_replay(br_dev, ctx, false, atomic_nb);
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300310}
311
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300312/* Let the bridge know that this port is offloaded, so that it can assign a
313 * switchdev hardware domain to it.
314 */
Vladimir Oltean957e2232021-08-03 23:34:08 +0300315int 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,
319 bool tx_fwd_offload,
320 struct netlink_ext_ack *extack)
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300321{
322 struct netdev_phys_item_id ppid;
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300323 int err;
324
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300325 err = dev_get_port_parent_id(dev, &ppid, false);
326 if (err)
327 return err;
328
Tobias Waldekranz47211192021-07-22 18:55:38 +0300329 err = nbp_switchdev_add(p, ppid, tx_fwd_offload, extack);
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300330 if (err)
331 return err;
332
333 err = nbp_switchdev_sync_objs(p, ctx, atomic_nb, blocking_nb, extack);
334 if (err)
335 goto out_switchdev_del;
336
337 return 0;
338
339out_switchdev_del:
340 nbp_switchdev_del(p);
341
342 return err;
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300343}
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300344
Vladimir Oltean957e2232021-08-03 23:34:08 +0300345void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
346 struct notifier_block *atomic_nb,
347 struct notifier_block *blocking_nb)
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300348{
Vladimir Oltean4e51bf42021-07-21 19:24:03 +0300349 nbp_switchdev_unsync_objs(p, ctx, atomic_nb, blocking_nb);
350
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300351 nbp_switchdev_del(p);
352}