Skip to contentSkip to conversation

net: bridge: fwd optimization cleanups

PATCH Applied Nikolay Aleksandrov 3 participants 9 messages Oct 5 – Oct 7

Subsystems (3)
lore
Share & tools
lore

Nikolay Aleksandrov

author maintainer
#0 lore raw

cover net: bridge: fwd optimization cleanups

Hi,
This patch-set takes care of the review comments and also does some additional cleanups after the recent forwarding optimizations. The first patch and the fourth take care of Ido's comments[1]. I have folded his suggestion with inlining the pvid helpers which aren't necessary anymore. The second patch takes care of Sashiko's comment[2]. The rest are additional cleanups. No functional changes intended by any of these. For more information please check their commit messages.

I do have one more separate patch-set which removes more VLAN lookups, this one is only about following up on comments and minor cleanups.

[1] https://lore.kernel.org/netdev/20260921134458.GA2199421@shredder/
[2] https://lore.kernel.org/netdev/[email protected]/

Thanks,
Nik

Nikolay Aleksandrov (6):
net: bridge: rename dst to fwd in br_flood_vlan
net: bridge: fdb: update the comment for br_fdb_cleanup_by_dst
net: bridge: vlan: share VLAN validation in br_should_learn
net: bridge: vlan: inline pvid pointer updates
net: bridge: fdb: factor out configured entry destination updates
net: bridge: fdb: use the entry key in __fdb_update

 net/bridge/br_fdb.c     | 51 ++++++++++++++++++++++------------------
 net/bridge/br_forward.c | 14 +++++------
 net/bridge/br_vlan.c    | 52 +++++++++++++----------------------------
 3 files changed, 51 insertions(+), 66 deletions(-)
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH 1/6 net: bridge: rename dst to fwd in br_flood_vlan

Rename dst to fwd to be consistent and avoid confusion (dst is used for fdbs).

---
 net/bridge/br_forward.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
net/bridge/br_forward.c +7 -7
index 82645db32efb..54365c6104b5 100644
@@ -291,7 +291,7 @@ static int br_flood_vlan(struct br_fwd_dst *prev,
 {
 	struct net_bridge_vlan_port_array *array;
 	struct net_bridge_vlan *masterv, *pv;
-	struct br_fwd_dst dst;
+	struct br_fwd_dst fwd;
 	int err;
 
 	masterv = br_vlan_is_master(v) ? v : v->brvlan;
@@ -301,18 +301,18 @@ static int br_flood_vlan(struct br_fwd_dst *prev,
 
 		for (i = 0; i < array->count; i++) {
 			pv = array->vlans[i];
-			dst.port = pv->port;
-			dst.vlan = pv;
-			err = br_flood_port(prev, &dst, skb, pkt_type,
+			fwd.port = pv->port;
+			fwd.vlan = pv;
+			err = br_flood_port(prev, &fwd, skb, pkt_type,
 					    local_orig);
 			if (err)
 				return err;
 		}
 	} else {
 		list_for_each_entry_rcu(pv, &masterv->port_vlist, port_vlist) {
-			dst.port = pv->port;
-			dst.vlan = pv;
-			err = br_flood_port(prev, &dst, skb, pkt_type,
+			fwd.port = pv->port;
+			fwd.vlan = pv;
+			err = br_flood_port(prev, &fwd, skb, pkt_type,
 					    local_orig);
 			if (err)
 				return err;
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH 2/6 net: bridge: fdb: update the comment for br_fdb_cleanup_by_dst

In reply to Nikolay Aleksandrov

Update the comment for br_fdb_cleanup_by_dst to better reflect what it is doing after the recent changes.

---
 net/bridge/br_fdb.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
net/bridge/br_fdb.c +5 -3
index 730b178cf6c4..ce09e3fbc58b 100644
@@ -868,9 +868,11 @@ int br_fdb_delete_bulk(struct nlmsghdr *nlh, struct net_device *dev,
 	return 0;
 }
 
-/* Clean up all entries referring to a specific destination.
- * if do_all is set also flush static entries
- * if vid is set delete all entries that match the vlan_id
+/* Clean up entries referring to cleanup_dst's port.
+ * If do_all is set ignore vid and entry flags. Otherwise vid selects
+ * the VLAN (0 means all), keeping static and non-offloaded ext learned entries.
+ * For a port-VLAN dst, vid must match its VLAN ID. Always clean up its local
+ * entry unless user-added and fall back to the port dst for retained entries.
  */
 void br_fdb_cleanup_by_dst(struct net_bridge *br,
 			   struct net_bridge_dst cleanup_dst, u16 vid,
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH 3/6 net: bridge: vlan: share VLAN validation in br_should_learn

In reply to Nikolay Aleksandrov

Both the pvid and tagged paths now resolve a VLAN entry. Select the entry first and share the state check and output assignment.

---
 net/bridge/br_vlan.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)
net/bridge/br_vlan.c +7 -12
index 0675f74cf1b9..dcf783e1a440 100644
@@ -742,21 +742,16 @@ bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb,
 	if (!br_vlan_get_tag(skb, &vid) && skb->vlan_proto != br->vlan_proto)
 		vid = 0;
 
-	if (!vid) {
+	if (vid)
+		v = br_vlan_find(vg, vid);
+	else
 		v = rcu_dereference(vg->pvid);
-		if (!v || !br_vlan_state_allowed(br_vlan_get_state(v), true))
-			return false;
-		*vlan = v;
-		return true;
-	}
 
-	v = br_vlan_find(vg, vid);
-	if (v && br_vlan_state_allowed(br_vlan_get_state(v), true)) {
-		*vlan = v;
-		return true;
-	}
+	if (!v || !br_vlan_state_allowed(br_vlan_get_state(v), true))
+		return false;
 
-	return false;
+	*vlan = v;
+	return true;
 }
 
 static int br_vlan_add_existing(struct net_bridge *br,
Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH 4/6 net: bridge: vlan: inline pvid pointer updates

In reply to Nikolay Aleksandrov

After caching the pvid VLAN entry directly, the add and delete helpers only check and update the pointer. So inline them both and clear pvid directly in __vlan_flush before walking the VLAN entries.

---
 net/bridge/br_vlan.c | 33 +++++++++------------------------
 1 file changed, 9 insertions(+), 24 deletions(-)
net/bridge/br_vlan.c +9 -24
index dcf783e1a440..c4e7e03e0969 100644
@@ -34,24 +34,6 @@ static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
 	return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
 }
 
-static void __vlan_add_pvid(struct net_bridge_vlan_group *vg,
-			    struct net_bridge_vlan *v)
-{
-	if (rcu_access_pointer(vg->pvid) == v)
-		return;
-
-	RCU_INIT_POINTER(vg->pvid, v);
-}
-
-static void __vlan_delete_pvid(struct net_bridge_vlan_group *vg,
-			       struct net_bridge_vlan *v)
-{
-	if (rcu_access_pointer(vg->pvid) != v)
-		return;
-
-	RCU_INIT_POINTER(vg->pvid, NULL);
-}
-
 /* Update the BRIDGE_VLAN_INFO_PVID and BRIDGE_VLAN_INFO_UNTAGGED flags of @v.
  * If @commit is false, return just whether the BRIDGE_VLAN_INFO_PVID and
  * BRIDGE_VLAN_INFO_UNTAGGED bits of @flags would produce any change onto @v.
@@ -78,10 +60,12 @@ static bool __vlan_flags_update(struct net_bridge_vlan *v, u16 flags,
 	if (!commit)
 		goto out;
 
-	if (flags & BRIDGE_VLAN_INFO_PVID)
-		__vlan_add_pvid(vg, v);
-	else
-		__vlan_delete_pvid(vg, v);
+	if (flags & BRIDGE_VLAN_INFO_PVID) {
+		if (pvid != v)
+			RCU_INIT_POINTER(vg->pvid, v);
+	} else if (pvid == v) {
+		RCU_INIT_POINTER(vg->pvid, NULL);
+	}
 
 	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
 		vlan_flags |= BRIDGE_VLAN_INFO_UNTAGGED;
@@ -432,7 +416,8 @@ static int __vlan_add(struct net_bridge_vlan *v, u16 flags,
 static void __vlan_unpublish(struct net_bridge_vlan_group *vg,
 			     struct net_bridge_vlan *v)
 {
-	__vlan_delete_pvid(vg, v);
+	if (rcu_access_pointer(vg->pvid) == v)
+		RCU_INIT_POINTER(vg->pvid, NULL);
 	if (!br_vlan_is_master(v)) {
 		struct net_bridge_vlan *masterv = v->brvlan;
 
@@ -503,7 +488,7 @@ static void __vlan_flush(const struct net_bridge *br,
 	struct net_bridge_vlan *vlan, *tmp;
 	u16 v_start = 0, v_end = 0;
 
-	__vlan_delete_pvid(vg, rtnl_dereference(vg->pvid));
+	RCU_INIT_POINTER(vg->pvid, NULL);
 	list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist) {
 		/* take care of disjoint ranges */
 		if (!v_start) {
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

PATCH 5/6 net: bridge: fdb: factor out configured entry destination updates

In reply to Nikolay Aleksandrov

Share the destination update logic used by user configured and ext learned entries. Report a modification only when the port changes.

---
 net/bridge/br_fdb.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
net/bridge/br_fdb.c +18 -16
index ce09e3fbc58b..927e5f23df5b 100644
@@ -1193,6 +1193,22 @@ static bool fdb_handle_notify(struct net_bridge_fdb_entry *fdb, u8 notify)
 	return modified;
 }
 
+static bool fdb_update_dst(struct net_bridge_fdb_entry *fdb,
+			   struct net_bridge_dst old_dst,
+			   struct net_bridge_dst dst)
+{
+	if (br_dst_equal(old_dst, dst))
+		return false;
+
+	if (br_dst_port(old_dst) == br_dst_port(dst)) {
+		br_fdb_dst_replace(fdb, old_dst, dst);
+		return false;
+	}
+
+	br_fdb_dst_write(fdb, dst);
+	return true;
+}
+
 /* Update (create or replace) forwarding database entry */
 static int fdb_add_entry(struct net_bridge *br, struct net_bridge_dst dst,
 			 const u8 *addr, struct ndmsg *ndm, u16 flags, u16 vid,
@@ -1246,14 +1262,7 @@ static int fdb_add_entry(struct net_bridge *br, struct net_bridge_dst dst,
 			return -EEXIST;
 
 		old_dst = br_fdb_dst_read(fdb);
-		if (!br_dst_equal(old_dst, dst)) {
-			if (br_dst_port(old_dst) != source) {
-				modified = true;
-				br_fdb_dst_write(fdb, dst);
-			} else {
-				br_fdb_dst_replace(fdb, old_dst, dst);
-			}
-		}
+		modified = fdb_update_dst(fdb, old_dst, dst);
 
 		set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
 		if (test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED, &fdb->flags))
@@ -1634,14 +1643,7 @@ int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
 
 		WRITE_ONCE(fdb->updated, jiffies);
 
-		if (!br_dst_equal(old_dst, dst)) {
-			if (br_dst_port(old_dst) != p) {
-				modified = true;
-				br_fdb_dst_write(fdb, dst);
-			} else {
-				br_fdb_dst_replace(fdb, old_dst, dst);
-			}
-		}
+		modified = fdb_update_dst(fdb, old_dst, dst);
 
 		if (test_and_set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) {
 			/* Refresh entry */
Signature · 1 line
2.47.3
author maintainer
#6 lore raw

PATCH 6/6 net: bridge: fdb: use the entry key in __fdb_update

In reply to Nikolay Aleksandrov

The address and VLAN ID passed to __fdb_update() match the key of the entry just found, so we can just use it.

---
 net/bridge/br_fdb.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
net/bridge/br_fdb.c +5 -4
index 927e5f23df5b..b601245ad923 100644
@@ -1000,7 +1000,6 @@ static void __fdb_update(struct net_bridge *br,
 			 struct net_bridge_fdb_entry *fdb,
 			 struct net_bridge_port *source,
 			 struct net_bridge_dst dst,
-			 const unsigned char *addr, u16 vid,
 			 unsigned long flags)
 {
 	struct net_bridge_port *old_port;
@@ -1012,7 +1011,8 @@ static void __fdb_update(struct net_bridge *br,
 	if (unlikely(test_bit(BR_FDB_LOCAL, &fdb->flags))) {
 		if (net_ratelimit())
 			br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
-				source->dev->name, addr, vid);
+				source->dev->name, fdb->key.addr.addr,
+				fdb->key.vlan_id);
 		return;
 	}
 
@@ -1055,7 +1055,8 @@ static void __fdb_update(struct net_bridge *br,
 	}
 
 	if (unlikely(fdb_modified)) {
-		trace_br_fdb_update(br, source, addr, vid, flags);
+		trace_br_fdb_update(br, source, fdb->key.addr.addr,
+				    fdb->key.vlan_id, flags);
 		fdb_notify(br, fdb, RTM_NEWNEIGH, true);
 	}
 }
@@ -1075,7 +1076,7 @@ void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
 
 	fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
 	if (likely(fdb)) {
-		__fdb_update(br, fdb, source, dst, addr, vid, flags);
+		__fdb_update(br, fdb, source, dst, flags);
 	} else {
 		spin_lock(&br->hash_lock);
 		fdb = fdb_create(br, dst, addr, vid, flags);
Signature · 1 line
2.47.3
maintainer Reviewed-by on the v1 series
#7 lore raw
In reply to Nikolay Aleksandrov
Quoted 8 lines · Nikolay Aleksandrov

Hi,
This patch-set takes care of the review comments and also does some additional cleanups after the recent forwarding optimizations. The first patch and the fourth take care of Ido's comments[1]. I have folded his suggestion with inlining the pvid helpers which aren't necessary anymore. The second patch takes care of Sashiko's comment[2]. The rest are additional cleanups. No functional changes intended by any of these. For more information please check their commit messages.

#8 lore raw
In reply to Nikolay Aleksandrov
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Mon, 5 Oct 2026 17:39:31 +0300 you wrote:

Hi,
This patch-set takes care of the review comments and also does some additional cleanups after the recent forwarding optimizations. The first patch and the fourth take care of Ido's comments[1]. I have folded his suggestion with inlining the pvid helpers which aren't necessary anymore. The second patch takes care of Sashiko's comment[2]. The rest are additional cleanups. No functional changes intended by any of these. For more information please check their commit messages.

[...]

Here is the summary with links:
- [net-next,1/6] net: bridge: rename dst to fwd in br_flood_vlan

    https://git.kernel.org/netdev/net-next/c/46d20a256be5
  - [net-next,2/6] net: bridge: fdb: update the comment for br_fdb_cleanup_by_dst
    https://git.kernel.org/netdev/net-next/c/6317ffc9bdd0
  - [net-next,3/6] net: bridge: vlan: share VLAN validation in br_should_learn
    https://git.kernel.org/netdev/net-next/c/33c73892eaab
  - [net-next,4/6] net: bridge: vlan: inline pvid pointer updates
    https://git.kernel.org/netdev/net-next/c/1fbe2d9d37ad
  - [net-next,5/6] net: bridge: fdb: factor out configured entry destination updates
    https://git.kernel.org/netdev/net-next/c/d65082c8bfb8
  - [net-next,6/6] net: bridge: fdb: use the entry key in __fdb_update
    https://git.kernel.org/netdev/net-next/c/8f8a20dc7bf0

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html