mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Jens Emil Schulz Østergaard" <jensemil.schulzostergaard@microchip.com>
To: Horatiu Vultur <horatiu.vultur@microchip.com>,
	<UNGLinuxDriver@microchip.com>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	Daniel Machon <daniel.machon@microchip.com>,
	"Steen Hegelund" <Steen.Hegelund@microchip.com>,
	Kees Cook <kees@kernel.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	Robert Marko <robert.marko@sartura.hr>,
	Eric Dumazet <edumazet@kernel.org>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-hardening@vger.kernel.org,
	"Jens Emil Schulz Østergaard"
	<jensemil.schulzostergaard@microchip.com>
Subject: [PATCH net-next v3 3/3] net: sparx5: keep router legs in sync with the bridge
Date: Tue, 17 Nov 2026 14:17:39 +0100	[thread overview]
Message-ID: <20261117-sparx5_l3_routing-v3-3-e680ed79c7dc@microchip.com> (raw)
In-Reply-To: <20261117-sparx5_l3_routing-v3-0-e680ed79c7dc@microchip.com>

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 runs at once 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.

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 <jensemil.schulzostergaard@microchip.com>
---
 .../net/ethernet/microchip/sparx5/sparx5_main.h    |   4 +
 .../net/ethernet/microchip/sparx5/sparx5_router.c  | 142 ++++++++++++++++++++-
 .../ethernet/microchip/sparx5/sparx5_switchdev.c   |  25 +++-
 3 files changed, 164 insertions(+), 7 deletions(-)

diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_main.h b/drivers/net/ethernet/microchip/sparx5/sparx5_main.h
index 7ce7ad607c34..96e6a2857721 100644
--- a/drivers/net/ethernet/microchip/sparx5/sparx5_main.h
+++ b/drivers/net/ethernet/microchip/sparx5/sparx5_main.h
@@ -556,6 +556,10 @@ 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(struct sparx5 *sparx5);
+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 26aeee17733c..7412db7a6e7b 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,36 @@ 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);
+		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,19 +650,66 @@ static void sparx5_rr_router_legs_flush(struct sparx5 *sparx5)
 		sparx5_rr_router_leg_destroy(leg);
 }
 
-static void sparx5_rr_legs_reconcile(struct sparx5 *sparx5)
+/* 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);
+}
+
+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();
 
 	router = sparx5->router;
+	if (!router)
+		return;
 
 	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);
 }
@@ -681,6 +810,7 @@ int sparx5_rr_router_init(struct sparx5 *sparx5)
 
 	INIT_LIST_HEAD(&r->leg_list);
 
+	/* The exported helpers use leg_list once sparx5->router is set. */
 	sparx5->router = r;
 
 	r->sparx5_router_owq = alloc_ordered_workqueue("sparx5_router_owq", 0);
diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c b/drivers/net/ethernet/microchip/sparx5/sparx5_switchdev.c
index 46b3351f717f..baa46d21e7ba 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(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


      parent reply	other threads:[~2026-11-17 13:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 13:17 [PATCH net-next v3 0/3] net: sparx5: add router legs for L3 routing offload Jens Emil Schulz Østergaard
2026-11-17 13:17 ` [PATCH net-next v3 1/3] net: sparx5: add l3 routing registers Jens Emil Schulz Østergaard
2026-11-17 13:17 ` [PATCH net-next v3 2/3] net: sparx5: add L3 router legs Jens Emil Schulz Østergaard
2026-11-17 13:17 ` Jens Emil Schulz Østergaard [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117-sparx5_l3_routing-v3-3-e680ed79c7dc@microchip.com \
    --to=jensemil.schulzostergaard@microchip.com \
    --cc=Steen.Hegelund@microchip.com \
    --cc=UNGLinuxDriver@microchip.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=daniel.machon@microchip.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=gustavoars@kernel.org \
    --cc=horatiu.vultur@microchip.com \
    --cc=kees@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=robert.marko@sartura.hr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®