Netdev List
 help / color / mirror / Atom feed
From: Nikolay Aleksandrov <razor@blackwall.org>
To: netdev@vger.kernel.org
Cc: Nikolay Aleksandrov <razor@blackwall.org>,
	Ido Schimmel <idosch@nvidia.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	bridge@lists.linux.dev
Subject: [PATCH net-next 2/3] net: bridge: mdb: index port groups by VLAN
Date: Mon, 23 Nov 2026 12:28:32 +0200	[thread overview]
Message-ID: <20261123102833.924427-3-razor@blackwall.org> (raw)
In-Reply-To: <20261123102833.924427-1-razor@blackwall.org>

Index each port's multicast groups by VID in a fixed 64-bucket hash
table. Maintain the index under multicast_lock alongside mglist,
including groups retained without port VLAN membership, and remove
entries on both port-group deletion paths.

Use the index when counting groups for a newly created port VLAN
context, avoiding a scan of all groups on the port for every added VLAN.
Keep mglist for its existing RCU readers and teardown walks. The index
also lets VLAN publication and unpublication update cached destinations
without repeatedly walking the full group list.

Operation  VLANs   Global ms before/after   Change %   Per-VLAN ms before/after   Change %
  ------------------------------------------------------------------------------------------
  add            1      0.0519 /   0.0900      +73.52       0.0481 /   0.0895      +86.16
  add           64      2.2500 /   0.7190      -68.04       2.2726 /   0.7441      -67.26
  add         1024     32.7464 /   5.7028      -82.58      32.9841 /   6.3116      -80.86
  add         4094    149.6750 /  34.3223      -77.07     151.5808 /  35.4586      -76.61

Note that the struct net_bridge_port_group increase pushes the allocations
above 256 bytes and into kmalloc-512, this can be kept compact by
introducing a kmalloc cache for these. It can be done separately.

Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
---
CC: Ido Schimmel <idosch@nvidia.com>
CC: David S. Miller <davem@davemloft.net>
CC: Eric Dumazet <edumazet@kernel.org>
CC: Jakub Kicinski <kuba@kernel.org>
CC: Paolo Abeni <pabeni@redhat.com>
CC: Simon Horman <horms@kernel.org>
CC: bridge@lists.linux.dev

 net/bridge/br_multicast.c | 7 ++++++-
 net/bridge/br_private.h   | 4 ++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/net/bridge/br_multicast.c b/net/bridge/br_multicast.c
index ae4fa050be97..7285229ca6d7 100644
--- a/net/bridge/br_multicast.c
+++ b/net/bridge/br_multicast.c
@@ -826,6 +826,7 @@ static void __br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
 	 * and deleting other groups of the same port must not truncate it.
 	 */
 	hlist_del_init_rcu(&pg->mglist);
+	hash_del(&pg->vlan_node);
 	br_multicast_eht_clean_sets(pg);
 	hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
 		br_multicast_del_group_src(ent, false);
@@ -1472,6 +1473,7 @@ struct net_bridge_port_group *br_multicast_new_port_group(
 
 	RCU_INIT_POINTER(p->next, next);
 	hlist_add_head_rcu(&p->mglist, &port->mglist);
+	hash_add(port->mdb_vlan_hash, &p->vlan_node, group->vid);
 
 	return p;
 
@@ -1488,6 +1490,7 @@ void br_multicast_del_port_group(struct net_bridge_port_group *p)
 	__u16 vid = p->key.addr.vid;
 
 	hlist_del_init_rcu(&p->mglist);
+	hash_del(&p->vlan_node);
 	if (!br_multicast_is_star_g(&p->key.addr))
 		rhashtable_remove_fast(&port->br->sg_port_tbl, &p->rhnode,
 				       br_sg_port_rht_params);
@@ -2105,7 +2108,8 @@ void br_multicast_port_ctx_init(struct net_bridge_port *port,
 		u32 n = 0;
 
 		spin_lock_bh(&port->br->multicast_lock);
-		hlist_for_each_entry(pg, &port->mglist, mglist)
+		hash_for_each_possible(port->mdb_vlan_hash, pg, vlan_node,
+				       vlan->vid)
 			if (pg->key.addr.vid == vlan->vid)
 				n++;
 		WRITE_ONCE(pmctx->mdb_n_entries, n);
@@ -2138,6 +2142,7 @@ int br_multicast_add_port(struct net_bridge_port *port)
 {
 	int err;
 
+	hash_init(port->mdb_vlan_hash);
 	port->multicast_eht_hosts_limit = BR_MCAST_DEFAULT_EHT_HOSTS_LIMIT;
 	br_multicast_port_ctx_init(port, NULL, &port->multicast_ctx);
 
diff --git a/net/bridge/br_private.h b/net/bridge/br_private.h
index 20e423e22132..04430dc32d02 100644
--- a/net/bridge/br_private.h
+++ b/net/bridge/br_private.h
@@ -18,6 +18,7 @@
 #include <net/pkt_cls.h>
 #include <linux/if_vlan.h>
 #include <linux/rhashtable.h>
+#include <linux/hashtable.h>
 #include <linux/refcount.h>
 
 #define BR_HASH_BITS 8
@@ -29,6 +30,7 @@
 #define BR_MAX_PORTS	(1<<BR_PORT_BITS)
 
 #define BR_MULTICAST_DEFAULT_HASH_MAX 4096
+#define BR_MDB_VLAN_HASH_BITS 6
 #define BR_MULTICAST_QUERY_INTVL_MIN msecs_to_jiffies(1000)
 #define BR_MULTICAST_STARTUP_QUERY_INTVL_MIN BR_MULTICAST_QUERY_INTVL_MIN
 #define BR_MULTICAST_QUERY_INTVL_MAX msecs_to_jiffies(86400000) /* 24 hours */
@@ -386,6 +388,7 @@ struct net_bridge_port_group {
 	struct timer_list		timer;
 	struct timer_list		rexmit_timer;
 	struct hlist_node		mglist;
+	struct hlist_node		vlan_node;
 	struct rb_root			eht_set_tree;
 	struct rb_root			eht_host_tree;
 
@@ -449,6 +452,7 @@ struct net_bridge_port {
 	u32				multicast_eht_hosts_limit;
 	u32				multicast_eht_hosts_cnt;
 	struct hlist_head		mglist;
+	DECLARE_HASHTABLE(mdb_vlan_hash, BR_MDB_VLAN_HASH_BITS);
 #endif
 
 #ifdef CONFIG_SYSFS
-- 
2.47.3


  parent reply	other threads:[~2026-11-23 10:28 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 10:28 [PATCH net-next 0/3] net: bridge: optimize vlan multicast forwarding Nikolay Aleksandrov
2026-11-23 10:28 ` [PATCH net-next 1/3] net: bridge: avoid VLAN lookups for multicast router forwarding Nikolay Aleksandrov
2026-11-23 10:28 ` Nikolay Aleksandrov [this message]
2026-11-23 10:28 ` [PATCH net-next 3/3] net: bridge: mdb: avoid VLAN lookups in listener forwarding Nikolay Aleksandrov

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=20261123102833.924427-3-razor@blackwall.org \
    --to=razor@blackwall.org \
    --cc=bridge@lists.linux.dev \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /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