Skip to content

Commit 957e223

Browse files
vladimirolteandavem330
authored andcommitted
net: make switchdev_bridge_port_{,unoffload} loosely coupled with the bridge
With the introduction of explicit offloading API in switchdev in commit 2f5dc00 ("net: bridge: switchdev: let drivers inform which bridge ports are offloaded"), we started having Ethernet switch drivers calling directly into a function exported by net/bridge/br_switchdev.c, which is a function exported by the bridge driver. This means that drivers that did not have an explicit dependency on the bridge before, like cpsw and am65-cpsw, now do - otherwise it is not possible to call a symbol exported by a driver that can be built as module unless you are a module too. There was an attempt to solve the dependency issue in the form of commit b0e8181 ("net: build all switchdev drivers as modules when the bridge is a module"). Grygorii Strashko, however, says about it: | In my opinion, the problem is a bit bigger here than just fixing the | build :( | | In case, of ^cpsw the switchdev mode is kinda optional and in many | cases (especially for testing purposes, NFS) the multi-mac mode is | still preferable mode. | | There were no such tight dependency between switchdev drivers and | bridge core before and switchdev serviced as independent, notification | based layer between them, so ^cpsw still can be "Y" and bridge can be | "M". Now for mostly every kernel build configuration the CONFIG_BRIDGE | will need to be set as "Y", or we will have to update drivers to | support build with BRIDGE=n and maintain separate builds for | networking vs non-networking testing. But is this enough? Wouldn't | it cause 'chain reaction' required to add more and more "Y" options | (like CONFIG_VLAN_8021Q)? | | PS. Just to be sure we on the same page - ARM builds will be forced | (with this patch) to have CONFIG_TI_CPSW_SWITCHDEV=m and so all our | automation testing will just fail with omap2plus_defconfig. In the light of this, it would be desirable for some configurations to avoid dependencies between switchdev drivers and the bridge, and have the switchdev mode as completely optional within the driver. Arnd Bergmann also tried to write a patch which better expressed the build time dependency for Ethernet switch drivers where the switchdev support is optional, like cpsw/am65-cpsw, and this made the drivers follow the bridge (compile as module if the bridge is a module) only if the optional switchdev support in the driver was enabled in the first place: https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/ but this still did not solve the fact that cpsw and am65-cpsw now must be built as modules when the bridge is a module - it just expressed correctly that optional dependency. But the new behavior is an apparent regression from Grygorii's perspective. So to support the use case where the Ethernet driver is built-in, NET_SWITCHDEV (a bool option) is enabled, and the bridge is a module, we need a framework that can handle the possible absence of the bridge from the running system, i.e. runtime bloatware as opposed to build-time bloatware. Luckily we already have this framework, since switchdev has been using it extensively. Events from the bridge side are transmitted to the driver side using notifier chains - this was originally done so that unrelated drivers could snoop for events emitted by the bridge towards ports that are implemented by other drivers (think of a switch driver with LAG offload that listens for switchdev events on a bonding/team interface that it offloads). There are also events which are transmitted from the driver side to the bridge side, which again are modeled using notifiers. SWITCHDEV_FDB_ADD_TO_BRIDGE is an example of this, and deals with notifying the bridge that a MAC address has been dynamically learned. So there is a precedent we can use for modeling the new framework. The difference compared to SWITCHDEV_FDB_ADD_TO_BRIDGE is that the work that the bridge needs to do when a port becomes offloaded is blocking in its nature: replay VLANs, MDBs etc. The calling context is indeed blocking (we are under rtnl_mutex), but the existing switchdev notification chain that the bridge is subscribed to is only the atomic one. So we need to subscribe the bridge to the blocking switchdev notification chain too. This patch: - keeps the driver-side perception of the switchdev_bridge_port_{,un}offload unchanged - moves the implementation of switchdev_bridge_port_{,un}offload from the bridge module into the switchdev module. - makes everybody that is subscribed to the switchdev blocking notifier chain "hear" offload & unoffload events - makes the bridge driver subscribe and handle those events - moves the bridge driver's handling of those events into 2 new functions called br_switchdev_port_{,un}offload. These functions contain in fact the core of the logic that was previously in switchdev_bridge_port_{,un}offload, just that now we go through an extra indirection layer to reach them. Unlike all the other switchdev notification structures, the structure used to carry the bridge port information, struct switchdev_notifier_brport_info, does not contain a "bool handled". This is because in the current usage pattern, we always know that a switchdev bridge port offloading event will be handled by the bridge, because the switchdev_bridge_port_offload() call was initiated by a NETDEV_CHANGEUPPER event in the first place, where info->upper_dev is a bridge. So if the bridge wasn't loaded, then the CHANGEUPPER event couldn't have happened. Signed-off-by: Vladimir Oltean <[email protected]> Tested-by: Grygorii Strashko <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 9c0532f commit 957e223

File tree

8 files changed

+184
-65
lines changed

8 files changed

+184
-65
lines changed

drivers/net/ethernet/ti/am65-cpsw-nuss.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <linux/clk.h>
99
#include <linux/etherdevice.h>
10-
#include <linux/if_bridge.h>
1110
#include <linux/if_vlan.h>
1211
#include <linux/interrupt.h>
1312
#include <linux/kernel.h>
@@ -28,6 +27,7 @@
2827
#include <linux/sys_soc.h>
2928
#include <linux/dma/ti-cppi5.h>
3029
#include <linux/dma/k3-udma-glue.h>
30+
#include <net/switchdev.h>
3131

3232
#include "cpsw_ale.h"
3333
#include "cpsw_sl.h"

drivers/net/ethernet/ti/cpsw_new.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <linux/module.h>
1212
#include <linux/irqreturn.h>
1313
#include <linux/interrupt.h>
14-
#include <linux/if_bridge.h>
1514
#include <linux/if_ether.h>
1615
#include <linux/etherdevice.h>
1716
#include <linux/net_tstamp.h>
@@ -29,6 +28,7 @@
2928
#include <linux/kmemleak.h>
3029
#include <linux/sys_soc.h>
3130

31+
#include <net/switchdev.h>
3232
#include <net/page_pool.h>
3333
#include <net/pkt_cls.h>
3434
#include <net/devlink.h>

include/linux/if_bridge.h

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -190,39 +190,4 @@ static inline clock_t br_get_ageing_time(const struct net_device *br_dev)
190190
}
191191
#endif
192192

193-
#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_NET_SWITCHDEV)
194-
195-
int switchdev_bridge_port_offload(struct net_device *brport_dev,
196-
struct net_device *dev, const void *ctx,
197-
struct notifier_block *atomic_nb,
198-
struct notifier_block *blocking_nb,
199-
bool tx_fwd_offload,
200-
struct netlink_ext_ack *extack);
201-
void switchdev_bridge_port_unoffload(struct net_device *brport_dev,
202-
const void *ctx,
203-
struct notifier_block *atomic_nb,
204-
struct notifier_block *blocking_nb);
205-
206-
#else
207-
208-
static inline int
209-
switchdev_bridge_port_offload(struct net_device *brport_dev,
210-
struct net_device *dev, const void *ctx,
211-
struct notifier_block *atomic_nb,
212-
struct notifier_block *blocking_nb,
213-
bool tx_fwd_offload,
214-
struct netlink_ext_ack *extack)
215-
{
216-
return -EINVAL;
217-
}
218-
219-
static inline void
220-
switchdev_bridge_port_unoffload(struct net_device *brport_dev,
221-
const void *ctx,
222-
struct notifier_block *atomic_nb,
223-
struct notifier_block *blocking_nb)
224-
{
225-
}
226-
#endif
227-
228193
#endif

include/net/switchdev.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ struct switchdev_obj_in_state_mrp {
180180

181181
typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
182182

183+
struct switchdev_brport {
184+
struct net_device *dev;
185+
const void *ctx;
186+
struct notifier_block *atomic_nb;
187+
struct notifier_block *blocking_nb;
188+
bool tx_fwd_offload;
189+
};
190+
183191
enum switchdev_notifier_type {
184192
SWITCHDEV_FDB_ADD_TO_BRIDGE = 1,
185193
SWITCHDEV_FDB_DEL_TO_BRIDGE,
@@ -197,6 +205,9 @@ enum switchdev_notifier_type {
197205
SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
198206
SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE,
199207
SWITCHDEV_VXLAN_FDB_OFFLOADED,
208+
209+
SWITCHDEV_BRPORT_OFFLOADED,
210+
SWITCHDEV_BRPORT_UNOFFLOADED,
200211
};
201212

202213
struct switchdev_notifier_info {
@@ -226,6 +237,11 @@ struct switchdev_notifier_port_attr_info {
226237
bool handled;
227238
};
228239

240+
struct switchdev_notifier_brport_info {
241+
struct switchdev_notifier_info info; /* must be first */
242+
const struct switchdev_brport brport;
243+
};
244+
229245
static inline struct net_device *
230246
switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
231247
{
@@ -246,6 +262,17 @@ switchdev_fdb_is_dynamically_learned(const struct switchdev_notifier_fdb_info *f
246262

247263
#ifdef CONFIG_NET_SWITCHDEV
248264

265+
int switchdev_bridge_port_offload(struct net_device *brport_dev,
266+
struct net_device *dev, const void *ctx,
267+
struct notifier_block *atomic_nb,
268+
struct notifier_block *blocking_nb,
269+
bool tx_fwd_offload,
270+
struct netlink_ext_ack *extack);
271+
void switchdev_bridge_port_unoffload(struct net_device *brport_dev,
272+
const void *ctx,
273+
struct notifier_block *atomic_nb,
274+
struct notifier_block *blocking_nb);
275+
249276
void switchdev_deferred_process(void);
250277
int switchdev_port_attr_set(struct net_device *dev,
251278
const struct switchdev_attr *attr,
@@ -316,6 +343,25 @@ int switchdev_handle_port_attr_set(struct net_device *dev,
316343
struct netlink_ext_ack *extack));
317344
#else
318345

346+
static inline int
347+
switchdev_bridge_port_offload(struct net_device *brport_dev,
348+
struct net_device *dev, const void *ctx,
349+
struct notifier_block *atomic_nb,
350+
struct notifier_block *blocking_nb,
351+
bool tx_fwd_offload,
352+
struct netlink_ext_ack *extack)
353+
{
354+
return -EOPNOTSUPP;
355+
}
356+
357+
static inline void
358+
switchdev_bridge_port_unoffload(struct net_device *brport_dev,
359+
const void *ctx,
360+
struct notifier_block *atomic_nb,
361+
struct notifier_block *blocking_nb)
362+
{
363+
}
364+
319365
static inline void switchdev_deferred_process(void)
320366
{
321367
}

net/bridge/br.c

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,48 @@ static struct notifier_block br_switchdev_notifier = {
201201
.notifier_call = br_switchdev_event,
202202
};
203203

204+
/* called under rtnl_mutex */
205+
static int br_switchdev_blocking_event(struct notifier_block *nb,
206+
unsigned long event, void *ptr)
207+
{
208+
struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
209+
struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
210+
struct switchdev_notifier_brport_info *brport_info;
211+
const struct switchdev_brport *b;
212+
struct net_bridge_port *p;
213+
int err = NOTIFY_DONE;
214+
215+
p = br_port_get_rtnl(dev);
216+
if (!p)
217+
goto out;
218+
219+
switch (event) {
220+
case SWITCHDEV_BRPORT_OFFLOADED:
221+
brport_info = ptr;
222+
b = &brport_info->brport;
223+
224+
err = br_switchdev_port_offload(p, b->dev, b->ctx,
225+
b->atomic_nb, b->blocking_nb,
226+
b->tx_fwd_offload, extack);
227+
err = notifier_from_errno(err);
228+
break;
229+
case SWITCHDEV_BRPORT_UNOFFLOADED:
230+
brport_info = ptr;
231+
b = &brport_info->brport;
232+
233+
br_switchdev_port_unoffload(p, b->ctx, b->atomic_nb,
234+
b->blocking_nb);
235+
break;
236+
}
237+
238+
out:
239+
return err;
240+
}
241+
242+
static struct notifier_block br_switchdev_blocking_notifier = {
243+
.notifier_call = br_switchdev_blocking_event,
244+
};
245+
204246
/* br_boolopt_toggle - change user-controlled boolean option
205247
*
206248
* @br: bridge device
@@ -355,10 +397,14 @@ static int __init br_init(void)
355397
if (err)
356398
goto err_out4;
357399

358-
err = br_netlink_init();
400+
err = register_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
359401
if (err)
360402
goto err_out5;
361403

404+
err = br_netlink_init();
405+
if (err)
406+
goto err_out6;
407+
362408
brioctl_set(br_ioctl_stub);
363409

364410
#if IS_ENABLED(CONFIG_ATM_LANE)
@@ -373,6 +419,8 @@ static int __init br_init(void)
373419

374420
return 0;
375421

422+
err_out6:
423+
unregister_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
376424
err_out5:
377425
unregister_switchdev_notifier(&br_switchdev_notifier);
378426
err_out4:
@@ -392,6 +440,7 @@ static void __exit br_deinit(void)
392440
{
393441
stp_proto_unregister(&br_stp_proto);
394442
br_netlink_fini();
443+
unregister_switchdev_blocking_notifier(&br_switchdev_blocking_notifier);
395444
unregister_switchdev_notifier(&br_switchdev_notifier);
396445
unregister_netdevice_notifier(&br_device_notifier);
397446
brioctl_set(NULL);

net/bridge/br_private.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,17 @@ static inline void br_sysfs_delbr(struct net_device *dev) { return; }
18801880

18811881
/* br_switchdev.c */
18821882
#ifdef CONFIG_NET_SWITCHDEV
1883+
int br_switchdev_port_offload(struct net_bridge_port *p,
1884+
struct net_device *dev, const void *ctx,
1885+
struct notifier_block *atomic_nb,
1886+
struct notifier_block *blocking_nb,
1887+
bool tx_fwd_offload,
1888+
struct netlink_ext_ack *extack);
1889+
1890+
void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
1891+
struct notifier_block *atomic_nb,
1892+
struct notifier_block *blocking_nb);
1893+
18831894
bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb);
18841895

18851896
void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb);
@@ -1908,6 +1919,24 @@ static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
19081919
skb->offload_fwd_mark = 0;
19091920
}
19101921
#else
1922+
static inline int
1923+
br_switchdev_port_offload(struct net_bridge_port *p,
1924+
struct net_device *dev, const void *ctx,
1925+
struct notifier_block *atomic_nb,
1926+
struct notifier_block *blocking_nb,
1927+
bool tx_fwd_offload,
1928+
struct netlink_ext_ack *extack)
1929+
{
1930+
return -EOPNOTSUPP;
1931+
}
1932+
1933+
static inline void
1934+
br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
1935+
struct notifier_block *atomic_nb,
1936+
struct notifier_block *blocking_nb)
1937+
{
1938+
}
1939+
19111940
static inline bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb)
19121941
{
19131942
return false;

net/bridge/br_switchdev.c

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -312,23 +312,16 @@ static void nbp_switchdev_unsync_objs(struct net_bridge_port *p,
312312
/* Let the bridge know that this port is offloaded, so that it can assign a
313313
* switchdev hardware domain to it.
314314
*/
315-
int switchdev_bridge_port_offload(struct net_device *brport_dev,
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)
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,
319+
bool tx_fwd_offload,
320+
struct netlink_ext_ack *extack)
321321
{
322322
struct netdev_phys_item_id ppid;
323-
struct net_bridge_port *p;
324323
int err;
325324

326-
ASSERT_RTNL();
327-
328-
p = br_port_get_rtnl(brport_dev);
329-
if (!p)
330-
return -ENODEV;
331-
332325
err = dev_get_port_parent_id(dev, &ppid, false);
333326
if (err)
334327
return err;
@@ -348,23 +341,12 @@ int switchdev_bridge_port_offload(struct net_device *brport_dev,
348341

349342
return err;
350343
}
351-
EXPORT_SYMBOL_GPL(switchdev_bridge_port_offload);
352344

353-
void switchdev_bridge_port_unoffload(struct net_device *brport_dev,
354-
const void *ctx,
355-
struct notifier_block *atomic_nb,
356-
struct notifier_block *blocking_nb)
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)
357348
{
358-
struct net_bridge_port *p;
359-
360-
ASSERT_RTNL();
361-
362-
p = br_port_get_rtnl(brport_dev);
363-
if (!p)
364-
return;
365-
366349
nbp_switchdev_unsync_objs(p, ctx, atomic_nb, blocking_nb);
367350

368351
nbp_switchdev_del(p);
369352
}
370-
EXPORT_SYMBOL_GPL(switchdev_bridge_port_unoffload);

net/switchdev/switchdev.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,51 @@ int switchdev_handle_port_attr_set(struct net_device *dev,
809809
return err;
810810
}
811811
EXPORT_SYMBOL_GPL(switchdev_handle_port_attr_set);
812+
813+
int switchdev_bridge_port_offload(struct net_device *brport_dev,
814+
struct net_device *dev, const void *ctx,
815+
struct notifier_block *atomic_nb,
816+
struct notifier_block *blocking_nb,
817+
bool tx_fwd_offload,
818+
struct netlink_ext_ack *extack)
819+
{
820+
struct switchdev_notifier_brport_info brport_info = {
821+
.brport = {
822+
.dev = dev,
823+
.ctx = ctx,
824+
.atomic_nb = atomic_nb,
825+
.blocking_nb = blocking_nb,
826+
.tx_fwd_offload = tx_fwd_offload,
827+
},
828+
};
829+
int err;
830+
831+
ASSERT_RTNL();
832+
833+
err = call_switchdev_blocking_notifiers(SWITCHDEV_BRPORT_OFFLOADED,
834+
brport_dev, &brport_info.info,
835+
extack);
836+
return notifier_to_errno(err);
837+
}
838+
EXPORT_SYMBOL_GPL(switchdev_bridge_port_offload);
839+
840+
void switchdev_bridge_port_unoffload(struct net_device *brport_dev,
841+
const void *ctx,
842+
struct notifier_block *atomic_nb,
843+
struct notifier_block *blocking_nb)
844+
{
845+
struct switchdev_notifier_brport_info brport_info = {
846+
.brport = {
847+
.ctx = ctx,
848+
.atomic_nb = atomic_nb,
849+
.blocking_nb = blocking_nb,
850+
},
851+
};
852+
853+
ASSERT_RTNL();
854+
855+
call_switchdev_blocking_notifiers(SWITCHDEV_BRPORT_UNOFFLOADED,
856+
brport_dev, &brport_info.info,
857+
NULL);
858+
}
859+
EXPORT_SYMBOL_GPL(switchdev_bridge_port_unoffload);

0 commit comments

Comments
 (0)