The conditions for offloading a leg are evaluated when an address is added, but the bridge and the upper can change afterwards. A change that would leave the chip routing differently from the kernel destroys the leg before it takes effect: disabling VLAN filtering, changing the bridge to a VLAN protocol other than 802.1Q, linking the upper to a master, and changing the upper's MAC. The global leg base MAC follows later bridge MAC changes. When the first switch port joins a bridge again, the reconcile is queued and creates the legs for its uppers that kept their addresses while the bridge was not offloaded. While the upper is administratively down the leg is disabled as an ingress router leg. The same holds while the bridge itself does not carry the leg's vid, or carries it untagged, since the kernel then does not deliver that vid to the upper. A bridge vlan delete, or an add that makes the vid untagged, disables the leg at once. A leg with an IPv4 address is kept while the upper is down, so it is available again when the interface comes back up. Unless keep_addr_on_down is set, link down removes IPv6 addresses, so an IPv6 only leg goes with its last address. A down upper does not follow bridge MAC changes, so it can lose its leg while down. Bringing a vlan upper of the bridge up queues the reconcile, which creates the leg again. IPv6 addresses that the kernel adds without calling the validator, such as SLAAC and temporary addresses, get their leg from the reconcile, which the address notifier queues for them. Signed-off-by: Jens Emil Schulz Østergaard --- .../net/ethernet/microchip/sparx5/sparx5_main.h | 3 + .../net/ethernet/microchip/sparx5/sparx5_router.c | 140 ++++++++++++++++++++- .../ethernet/microchip/sparx5/sparx5_switchdev.c | 25 +++- 3 files changed, 162 insertions(+), 6 deletions(-) diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_main.h b/drivers/net/ethernet/microchip/sparx5/sparx5_main.h index b940f8199942..36df730e9bdb 100644 --- a/drivers/net/ethernet/microchip/sparx5/sparx5_main.h +++ b/drivers/net/ethernet/microchip/sparx5/sparx5_main.h @@ -563,6 +563,9 @@ int sparx5_rr_router_init(struct sparx5 *sparx5); void sparx5_rr_router_deinit(struct sparx5 *sparx5); void sparx5_rr_bridge_legs_flush(struct sparx5 *sparx5, const struct net_device *bridge); +void sparx5_rr_legs_reconcile_schedule(struct sparx5 *sparx5); +void sparx5_rr_bridge_vlan_clear(struct sparx5 *sparx5, + const struct net_device *bridge, u16 vid); struct sparx5_router { struct sparx5 *sparx5; diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_router.c b/drivers/net/ethernet/microchip/sparx5/sparx5_router.c index c04be943e1af..068bf8c71b78 100644 --- a/drivers/net/ethernet/microchip/sparx5/sparx5_router.c +++ b/drivers/net/ethernet/microchip/sparx5/sparx5_router.c @@ -32,6 +32,8 @@ struct sparx5_rr_router_leg { struct list_head leg_list_node; /* Router member */ u16 vmid; /* Internal id */ u16 vid; /* VLAN id */ + /* The bridge carries vid as a vlan of its own. */ + bool vlan_present; }; static void sparx5_rr_schedule_work(struct sparx5 *sparx5, @@ -40,9 +42,10 @@ static void sparx5_rr_schedule_work(struct sparx5 *sparx5, queue_work(sparx5->router->sparx5_router_owq, work); } -static void sparx5_rr_legs_reconcile_schedule(struct sparx5 *sparx5) +void sparx5_rr_legs_reconcile_schedule(struct sparx5 *sparx5) { - sparx5_rr_schedule_work(sparx5, &sparx5->router->legs_work); + if (sparx5->router) + sparx5_rr_schedule_work(sparx5, &sparx5->router->legs_work); } static void sparx5_rr_split_mac(const unsigned char *mac, u32 split, @@ -100,6 +103,19 @@ sparx5_rr_leg_find_by_vid(struct sparx5 *sparx5, u16 vid) return NULL; } +static bool sparx5_rr_bridge_has_legs(struct sparx5 *sparx5, + const struct net_device *bridge) +{ + struct sparx5_rr_router_leg *leg; + + list_for_each_entry(leg, &sparx5->router->leg_list, leg_list_node) { + if (vlan_dev_real_dev(leg->dev) == bridge) + return true; + } + + return false; +} + static void sparx5_rr_leg_base_mac_set(struct sparx5 *sparx5, const unsigned char *mac) { @@ -154,6 +170,16 @@ static void sparx5_rr_leg_hw_uc_set(struct sparx5 *sparx5, ANA_L3_RLEG_CTRL(leg->vmid)); } +/* Route ingress on the leg only while the kernel would receive on it: the + * upper is up and the bridge carries its vid. + */ +static void sparx5_rr_leg_hw_uc_update(struct sparx5 *sparx5, + struct sparx5_rr_router_leg *leg) +{ + sparx5_rr_leg_hw_uc_set(sparx5, leg, + netif_running(leg->dev) && leg->vlan_present); +} + static void sparx5_rr_leg_hw_init(struct sparx5 *sparx5, struct sparx5_rr_router_leg *leg) { @@ -172,7 +198,7 @@ static void sparx5_rr_leg_hw_init(struct sparx5 *sparx5, ANA_L3_RLEG_CTRL_RLEG_EVID, sparx5, ANA_L3_RLEG_CTRL(leg->vmid)); - sparx5_rr_leg_hw_uc_set(sparx5, leg, true); + sparx5_rr_leg_hw_uc_update(sparx5, leg); /* Configure egress VLAN in rewriter */ spx5_rmw(REW_RLEG_CTRL_RLEG_EVID_SET(leg->vid), REW_RLEG_CTRL_RLEG_EVID, @@ -260,6 +286,15 @@ static void sparx5_rr_router_leg_destroy(struct sparx5_rr_router_leg *leg) kfree(leg); } +static bool sparx5_rr_bridge_vlan_present(struct net_device *bridge, u16 vid) +{ + struct bridge_vlan_info info; + + return !br_vlan_get_info(bridge, vid, &info) && + info.flags & BRIDGE_VLAN_INFO_BRENTRY && + !(info.flags & BRIDGE_VLAN_INFO_UNTAGGED); +} + static struct sparx5_rr_router_leg * sparx5_rr_router_leg_create(struct sparx5 *sparx5, struct net_device *dev, u16 vid, struct netlink_ext_ack *extack) @@ -285,6 +320,9 @@ sparx5_rr_router_leg_create(struct sparx5 *sparx5, struct net_device *dev, /* Global router leg base MAC tracks the bridge MAC. */ sparx5_rr_leg_base_mac_set(sparx5, vlan_dev_real_dev(dev)->dev_addr); + leg->vlan_present = + sparx5_rr_bridge_vlan_present(vlan_dev_real_dev(dev), vid); + list_add(&leg->leg_list_node, &sparx5->router->leg_list); sparx5_rr_leg_hw_init(sparx5, leg); @@ -476,7 +514,7 @@ static int sparx5_rr_inet6addr_event(struct notifier_block *nb, struct net_device *dev = if6->idev->dev; struct sparx5_router *router; - if (event != NETDEV_DOWN) + if (event != NETDEV_UP && event != NETDEV_DOWN) return NOTIFY_DONE; if (!net_eq(dev_net(dev), &init_net)) @@ -529,6 +567,20 @@ static int sparx5_rr_inetaddr_valid_event(struct notifier_block *nb, ivi->extack); } +static void sparx5_rr_leg_withdraw(struct sparx5 *sparx5, + struct net_device *dev) +{ + struct sparx5_rr_router_leg *leg; + + mutex_lock(&sparx5->router->lock); + leg = sparx5_rr_leg_find_by_dev(sparx5, dev); + if (leg) { + sparx5_rr_router_leg_destroy(leg); + sparx5_rr_legs_reconcile_schedule(sparx5); + } + mutex_unlock(&sparx5->router->lock); +} + /* Called with RTNL. */ static int sparx5_rr_netdevice_event(struct notifier_block *nb, unsigned long event, void *ptr) @@ -547,6 +599,39 @@ static int sparx5_rr_netdevice_event(struct notifier_block *nb, sparx5 = router->sparx5; switch (event) { + case NETDEV_PRECHANGEUPPER: { + struct netdev_notifier_changeupper_info *upper_info = ptr; + + if (upper_info->linking && upper_info->master) + sparx5_rr_leg_withdraw(sparx5, dev); + break; + } + case NETDEV_CHANGEUPPER: + if (is_vlan_dev(dev)) + sparx5_rr_legs_reconcile_schedule(sparx5); + break; + case NETDEV_UP: + case NETDEV_DOWN: + mutex_lock(&router->lock); + leg = sparx5_rr_leg_find_by_dev(sparx5, dev); + if (leg) + sparx5_rr_leg_hw_uc_update(sparx5, leg); + mutex_unlock(&router->lock); + if (event == NETDEV_UP && is_vlan_dev(dev) && + vlan_dev_real_dev(dev) == sparx5->hw_bridge_dev) + sparx5_rr_legs_reconcile_schedule(sparx5); + break; + case NETDEV_PRE_CHANGEADDR: + sparx5_rr_leg_withdraw(sparx5, dev); + break; + case NETDEV_CHANGEADDR: + mutex_lock(&router->lock); + if (sparx5_rr_bridge_has_legs(sparx5, dev)) + sparx5_rr_leg_base_mac_set(sparx5, dev->dev_addr); + mutex_unlock(&router->lock); + if (is_vlan_dev(dev)) + sparx5_rr_legs_reconcile_schedule(sparx5); + break; case NETDEV_UNREGISTER: mutex_lock(&router->lock); leg = sparx5_rr_leg_find_by_dev(sparx5, dev); @@ -568,10 +653,34 @@ static void sparx5_rr_router_legs_flush(struct sparx5 *sparx5) sparx5_rr_router_leg_destroy(leg); } +/* Follow a vlan of the bridge itself, the kernel only receives on a vlan + * upper while the bridge carries its vid. + */ +void sparx5_rr_bridge_vlan_clear(struct sparx5 *sparx5, + const struct net_device *bridge, u16 vid) +{ + struct sparx5_rr_router_leg *leg; + + if (!sparx5->router) + return; + + mutex_lock(&sparx5->router->lock); + list_for_each_entry(leg, &sparx5->router->leg_list, leg_list_node) { + if (leg->vid != vid || vlan_dev_real_dev(leg->dev) != bridge) + continue; + leg->vlan_present = false; + sparx5_rr_leg_hw_uc_update(sparx5, leg); + } + sparx5_rr_legs_reconcile_schedule(sparx5); + mutex_unlock(&sparx5->router->lock); +} + static void sparx5_rr_legs_reconcile(struct sparx5 *sparx5) { struct sparx5_rr_router_leg *leg, *tmp; + struct net_device *bridge, *dev; struct sparx5_router *router; + struct list_head *iter; ASSERT_RTNL(); @@ -579,8 +688,29 @@ static void sparx5_rr_legs_reconcile(struct sparx5 *sparx5) mutex_lock(&router->lock); list_for_each_entry_safe(leg, tmp, &router->leg_list, leg_list_node) { - if (sparx5_rr_dev_addr_list_empty(leg->dev)) + if (sparx5_rr_dev_addr_list_empty(leg->dev) || + !sparx5_rr_dev_real_is_vlan_aware(sparx5, leg->dev)) { sparx5_rr_router_leg_destroy(leg); + continue; + } + + bridge = vlan_dev_real_dev(leg->dev); + leg->vlan_present = sparx5_rr_bridge_vlan_present(bridge, + leg->vid); + sparx5_rr_leg_hw_uc_update(sparx5, leg); + } + + bridge = sparx5->hw_bridge_dev; + if (!bitmap_empty(sparx5->bridge_mask, SPX5_PORTS) && bridge) { + netdev_for_each_upper_dev_rcu(bridge, dev, iter) { + if (!is_vlan_dev(dev) || + sparx5_rr_dev_addr_list_empty(dev)) + continue; + if (__sparx5_rr_inetaddr_event(sparx5, dev, NETDEV_UP, + NULL) && net_ratelimit()) + netdev_warn(dev, + "Failed to offload L3 router leg\n"); + } } mutex_unlock(&router->lock); } diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c b/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c index 46b3351f717f..322806fbfe2b 100644 --- a/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c +++ b/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c @@ -152,6 +152,11 @@ static int sparx5_port_attr_set(struct net_device *dev, const void *ctx, sparx5_port_attr_ageing_set(port, attr->u.ageing_time); break; case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: + /* L3 offload requires a VLAN aware bridge. */ + if (!attr->u.vlan_filtering) + sparx5_rr_bridge_legs_flush(port->sparx5, + attr->orig_dev); + sparx5_rr_legs_reconcile_schedule(port->sparx5); /* Used PVID 1 when default_pvid is 0, to avoid * collision with non-bridged ports. */ @@ -160,6 +165,13 @@ static int sparx5_port_attr_set(struct net_device *dev, const void *ctx, port->vlan_aware = attr->u.vlan_filtering; sparx5_vlan_port_apply(port->sparx5, port); break; + case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL: + /* A leg is classified by vid alone, whatever the protocol. */ + if (attr->u.vlan_protocol != ETH_P_8021Q) + sparx5_rr_bridge_legs_flush(port->sparx5, + attr->orig_dev); + sparx5_rr_legs_reconcile_schedule(port->sparx5); + return -EOPNOTSUPP; case SWITCHDEV_ATTR_ID_PORT_MROUTER: sparx5_port_attr_mrouter_set(port, attr->orig_dev, @@ -179,9 +191,11 @@ static int sparx5_port_bridge_join(struct sparx5_port *port, struct switchdev_brport_flags flags = {0}; struct sparx5 *sparx5 = port->sparx5; struct net_device *ndev = port->ndev; + bool first; int err; - if (bitmap_empty(sparx5->bridge_mask, SPX5_PORTS)) + first = bitmap_empty(sparx5->bridge_mask, SPX5_PORTS); + if (first) /* First bridged port */ sparx5->hw_bridge_dev = bridge; else @@ -211,6 +225,10 @@ static int sparx5_port_bridge_join(struct sparx5_port *port, flags.val = flags.mask; sparx5_port_attr_bridge_flags(port, flags); + /* The bridge's legs were flushed when its last port left. */ + if (first) + sparx5_rr_legs_reconcile_schedule(sparx5); + return 0; err_switchdev_offload: @@ -448,6 +466,10 @@ static int sparx5_handle_port_vlan_add(struct net_device *dev, /* Flood broadcast to CPU */ sparx5_mact_learn(sparx5, sparx5_get_pgid(sparx5, PGID_BCAST), dev->broadcast, v->vid); + if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED) + sparx5_rr_bridge_vlan_clear(sparx5, dev, v->vid); + else + sparx5_rr_legs_reconcile_schedule(sparx5); return 0; } @@ -683,6 +705,7 @@ static int sparx5_handle_port_vlan_del(struct net_device *dev, switchdev_blocking_nb); sparx5_mact_forget(sparx5, dev->broadcast, vid); + sparx5_rr_bridge_vlan_clear(sparx5, dev, vid); return 0; } -- 2.52.0