Skip to contentSkip to conversation

net: bridge: convert multicast to generic rhashtable

PATCH In review Nikolay Aleksandrov 2 participants 6 messages Dec 5, 2018 – Dec 6, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover net: bridge: convert multicast to generic rhashtable

Hi,
The current bridge multicast code uses a custom rhashtable implementation which predates the generic rhashtable API. Patch 01 converts it to use the generic kernel rhashtable which simplifies the code a lot and removes duplicated functionality. The convert also makes hash_elasticity obsolete as the generic rhashtable already has such checks and has a fixed elasticity of RHT_ELASTICITY (16 currently) so we emit a warning whenever elasticity is set and return RHT_ELASTICITY when read (patch 03). Patch 02 converts the multicast code to use non-bh RCU flavor as it was mixing bh and non-bh. Since now we have the generic rhashtable which autoshrinks we can be more liberal with the default hash maximum so patch 04 increases it to 4096 and moves it to a define in br_private.h.

v3: add non-rcu br_mdb_get variant and use it where we have
multicast_lock, drop special hash_max handling and just set it where needed and use non-bh RCU consistently (patch 02, new)
v2: send the latest version of the set which handles when IGMP snooping
is not defined, changes are in patch 01

Thanks,
Nik

Nikolay Aleksandrov (4):
net: bridge: convert multicast to generic rhashtable
net: bridge: multicast: use non-bh rcu flavor
net: bridge: mark hash_elasticity as obsolete
net: bridge: increase multicast's default maximum number of entries

 net/bridge/br_device.c    |  10 +
 net/bridge/br_mdb.c       | 122 +++++------
 net/bridge/br_multicast.c | 439 +++++++-------------------------------
 net/bridge/br_netlink.c   |  20 +-
 net/bridge/br_private.h   |  42 ++--
 net/bridge/br_sysfs_br.c  |  14 +-
 6 files changed, 177 insertions(+), 470 deletions(-)
Signature · 1 line
2.17.2
#1 lore raw

PATCH v3 1/4 net: bridge: convert multicast to generic rhashtable

The bridge multicast code currently uses a custom resizable hashtable which predates the generic rhashtable interface. It has many shortcomings compared and duplicates functionality that is presently available via the generic rhashtable, so this patch removes the custom rhashtable implementation in favor of the kernel's generic rhashtable. The hash maximum is kept and the rhashtable's size is used to do a loose check if it's reached in which case we revert to the old behaviour and disable further bridge multicast processing. Also now we can support any hash maximum, doesn't need to be a power of 2.

v3: add non-rcu br_mdb_get variant and use it where multicast_lock is
held to avoid RCU splat, drop hash_max function and just set it directly

v2: handle when IGMP snooping is undefined, add br_mdb_init/uninit
placeholders

---
 net/bridge/br_device.c    |  10 +
 net/bridge/br_mdb.c       | 120 +++++------
 net/bridge/br_multicast.c | 414 +++++++-------------------------------
 net/bridge/br_netlink.c   |   9 +-
 net/bridge/br_private.h   |  38 ++--
 net/bridge/br_sysfs_br.c  |   8 +-
 6 files changed, 163 insertions(+), 436 deletions(-)
net/bridge/br_device.c +10 -0
index c6abf927f0c9..9f41a5d4da3f 100644
@@ -131,9 +131,17 @@ static int br_dev_init(struct net_device *dev)
 		return err;
 	}
 
+	err = br_mdb_hash_init(br);
+	if (err) {
+		free_percpu(br->stats);
+		br_fdb_hash_fini(br);
+		return err;
+	}
+
 	err = br_vlan_init(br);
 	if (err) {
 		free_percpu(br->stats);
+		br_mdb_hash_fini(br);
 		br_fdb_hash_fini(br);
 		return err;
 	}
@@ -142,6 +150,7 @@ static int br_dev_init(struct net_device *dev)
 	if (err) {
 		free_percpu(br->stats);
 		br_vlan_flush(br);
+		br_mdb_hash_fini(br);
 		br_fdb_hash_fini(br);
 	}
 	br_set_lockdep_class(dev);
@@ -156,6 +165,7 @@ static void br_dev_uninit(struct net_device *dev)
 	br_multicast_dev_del(br);
 	br_multicast_uninit_stats(br);
 	br_vlan_flush(br);
+	br_mdb_hash_fini(br);
 	br_fdb_hash_fini(br);
 	free_percpu(br->stats);
 }
net/bridge/br_mdb.c +51 -69

186 lines not on this page · raw message

net/bridge/br_multicast.c +73 -341

598 lines not on this page · raw message

net/bridge/br_netlink.c +2 -7
index 13cd50326af2..063da636cb82 100644
@@ -1195,13 +1195,8 @@ static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
 		br->hash_elasticity = val;
 	}
 
-	if (data[IFLA_BR_MCAST_HASH_MAX]) {
-		u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
-
-		err = br_multicast_set_hash_max(br, hash_max);
-		if (err)
-			return err;
-	}
+	if (data[IFLA_BR_MCAST_HASH_MAX])
+		br->hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
 
 	if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
 		u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
net/bridge/br_private.h +20 -18
index d29f837cd7a2..a0ce0822921c 100644
@@ -213,23 +213,14 @@ struct net_bridge_port_group {
 };
 
 struct net_bridge_mdb_entry {
-	struct hlist_node		hlist[2];
+	struct rhash_head		rhnode;
 	struct net_bridge		*br;
 	struct net_bridge_port_group __rcu *ports;
 	struct rcu_head			rcu;
 	struct timer_list		timer;
 	struct br_ip			addr;
 	bool				host_joined;
-};
-
-struct net_bridge_mdb_htable {
-	struct hlist_head		*mhash;
-	struct rcu_head			rcu;
-	struct net_bridge_mdb_htable	*old;
-	u32				size;
-	u32				max;
-	u32				secret;
-	u32				ver;
+	struct hlist_node		mdb_node;
 };
 
 struct net_bridge_port {
@@ -400,7 +391,9 @@ struct net_bridge {
 	unsigned long			multicast_query_response_interval;
 	unsigned long			multicast_startup_query_interval;
 
-	struct net_bridge_mdb_htable __rcu *mdb;
+	struct rhashtable		mdb_hash_tbl;
+
+	struct hlist_head		mdb_list;
 	struct hlist_head		router_list;
 
 	struct timer_list		multicast_router_timer;
@@ -659,7 +652,6 @@ int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd,
 
 /* br_multicast.c */
 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
-extern unsigned int br_mdb_rehash_seq;
 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
 		     struct sk_buff *skb, u16 vid);
 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
@@ -684,17 +676,16 @@ int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val);
 int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val);
 #endif
 struct net_bridge_mdb_entry *
-br_mdb_ip_get(struct net_bridge_mdb_htable *mdb, struct br_ip *dst);
+br_mdb_ip_get(struct net_bridge *br, struct br_ip *dst);
 struct net_bridge_mdb_entry *
-br_multicast_new_group(struct net_bridge *br, struct net_bridge_port *port,
-		       struct br_ip *group);
+br_multicast_new_group(struct net_bridge *br, struct br_ip *group);
 void br_multicast_free_pg(struct rcu_head *head);
 struct net_bridge_port_group *
 br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
 			    struct net_bridge_port_group __rcu *next,
 			    unsigned char flags, const unsigned char *src);
-void br_mdb_init(void);
-void br_mdb_uninit(void);
+int br_mdb_hash_init(struct net_bridge *br);
+void br_mdb_hash_fini(struct net_bridge *br);
 void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
 		   struct br_ip *group, int type, u8 flags);
 void br_rtr_notify(struct net_device *dev, struct net_bridge_port *port,
@@ -706,6 +697,8 @@ void br_multicast_uninit_stats(struct net_bridge *br);
 void br_multicast_get_stats(const struct net_bridge *br,
 			    const struct net_bridge_port *p,
 			    struct br_mcast_stats *dest);
+void br_mdb_init(void);
+void br_mdb_uninit(void);
 
 #define mlock_dereference(X, br) \
 	rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
@@ -831,6 +824,15 @@ static inline void br_mdb_uninit(void)
 {
 }
 
+static inline int br_mdb_hash_init(struct net_bridge *br)
+{
+	return 0;
+}
+
+static inline void br_mdb_hash_fini(struct net_bridge *br)
+{
+}
+
 static inline void br_multicast_count(struct net_bridge *br,
 				      const struct net_bridge_port *p,
 				      const struct sk_buff *skb,
net/bridge/br_sysfs_br.c +7 -1
index 6a378a7e16ea..f164cda30fe2 100644
@@ -449,10 +449,16 @@ static ssize_t hash_max_show(struct device *d, struct device_attribute *attr,
 	return sprintf(buf, "%u\n", br->hash_max);
 }
 
+static int set_hash_max(struct net_bridge *br, unsigned long val)
+{
+	br->hash_max = val;
+	return 0;
+}
+
 static ssize_t hash_max_store(struct device *d, struct device_attribute *attr,
 			      const char *buf, size_t len)
 {
-	return store_bridge_parm(d, buf, len, br_multicast_set_hash_max);
+	return store_bridge_parm(d, buf, len, set_hash_max);
 }
 static DEVICE_ATTR_RW(hash_max);
 
Signature · 1 line
2.17.2
#2 lore raw

PATCH v3 2/4 net: bridge: multicast: use non-bh rcu flavor

In reply to Nikolay Aleksandrov

The bridge multicast code has been using a mix of RCU and RCU-bh flavors sometimes in questionable way. Since we've moved to rhashtable just use non-bh RCU everywhere. In addition this simplifies freeing of objects and allows us to remove some unnecessary callback functions.

v3: new patch

---
 net/bridge/br_mdb.c       |  2 +-
 net/bridge/br_multicast.c | 26 +++++---------------------
 net/bridge/br_private.h   |  1 -
 3 files changed, 6 insertions(+), 23 deletions(-)
net/bridge/br_mdb.c +1 -1
index ea8abdb56df3..79d4c9d253e0 100644
@@ -710,7 +710,7 @@ static int __br_mdb_del(struct net_bridge *br, struct br_mdb_entry *entry)
 		rcu_assign_pointer(*pp, p->next);
 		hlist_del_init(&p->mglist);
 		del_timer(&p->timer);
-		call_rcu_bh(&p->rcu, br_multicast_free_pg);
+		kfree_rcu(p, rcu);
 		err = 0;
 
 		if (!mp->ports && !mp->host_joined &&
net/bridge/br_multicast.c +5 -21
index 83a5931a7784..5b8cfef8bc4d 100644
@@ -160,22 +160,6 @@ struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
 	return br_mdb_ip_get_rcu(br, &ip);
 }
 
-void br_multicast_free_pg(struct rcu_head *head)
-{
-	struct net_bridge_port_group *p =
-		container_of(head, struct net_bridge_port_group, rcu);
-
-	kfree(p);
-}
-
-static void br_multicast_free_group(struct rcu_head *head)
-{
-	struct net_bridge_mdb_entry *mp =
-		container_of(head, struct net_bridge_mdb_entry, rcu);
-
-	kfree(mp);
-}
-
 static void br_multicast_group_expired(struct timer_list *t)
 {
 	struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer);
@@ -195,7 +179,7 @@ static void br_multicast_group_expired(struct timer_list *t)
 			       br_mdb_rht_params);
 	hlist_del_rcu(&mp->mdb_node);
 
-	call_rcu_bh(&mp->rcu, br_multicast_free_group);
+	kfree_rcu(mp, rcu);
 
 out:
 	spin_unlock(&br->multicast_lock);
@@ -223,7 +207,7 @@ static void br_multicast_del_pg(struct net_bridge *br,
 		del_timer(&p->timer);
 		br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
 			      p->flags);
-		call_rcu_bh(&p->rcu, br_multicast_free_pg);
+		kfree_rcu(p, rcu);
 
 		if (!mp->ports && !mp->host_joined &&
 		    netif_running(br->dev))
@@ -1425,7 +1409,7 @@ br_multicast_leave_group(struct net_bridge *br,
 			rcu_assign_pointer(*pp, p->next);
 			hlist_del_init(&p->mglist);
 			del_timer(&p->timer);
-			call_rcu_bh(&p->rcu, br_multicast_free_pg);
+			kfree_rcu(p, rcu);
 			br_mdb_notify(br->dev, port, group, RTM_DELMDB,
 				      p->flags);
 
@@ -1839,11 +1823,11 @@ void br_multicast_dev_del(struct net_bridge *br)
 		rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
 				       br_mdb_rht_params);
 		hlist_del_rcu(&mp->mdb_node);
-		call_rcu_bh(&mp->rcu, br_multicast_free_group);
+		kfree_rcu(mp, rcu);
 	}
 	spin_unlock_bh(&br->multicast_lock);
 
-	rcu_barrier_bh();
+	rcu_barrier();
 }
 
 int br_multicast_set_router(struct net_bridge *br, unsigned long val)
net/bridge/br_private.h +0 -1
index a0ce0822921c..cf1f7365deb3 100644
@@ -679,7 +679,6 @@ struct net_bridge_mdb_entry *
 br_mdb_ip_get(struct net_bridge *br, struct br_ip *dst);
 struct net_bridge_mdb_entry *
 br_multicast_new_group(struct net_bridge *br, struct br_ip *group);
-void br_multicast_free_pg(struct rcu_head *head);
 struct net_bridge_port_group *
 br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
 			    struct net_bridge_port_group __rcu *next,
Signature · 1 line
2.17.2
#3 lore raw

PATCH v3 3/4 net: bridge: mark hash_elasticity as obsolete

In reply to Nikolay Aleksandrov

Now that the bridge multicast uses the generic rhashtable interface we can drop the hash_elasticity option as that is already done for us and it's hardcoded to a maximum of RHT_ELASTICITY (16 currently). Add a warning about the obsolete option when the hash_elasticity is set.

---
v2, v3: no change

 net/bridge/br_multicast.c |  1 -
 net/bridge/br_netlink.c   | 11 ++++-------
 net/bridge/br_private.h   |  1 -
 net/bridge/br_sysfs_br.c  |  6 +++---
 4 files changed, 7 insertions(+), 12 deletions(-)
net/bridge/br_multicast.c +0 -1
index 5b8cfef8bc4d..804ee15c45c1 100644
@@ -1741,7 +1741,6 @@ static void br_ip6_multicast_query_expired(struct timer_list *t)
 
 void br_multicast_init(struct net_bridge *br)
 {
-	br->hash_elasticity = 4;
 	br->hash_max = 512;
 
 	br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
net/bridge/br_netlink.c +4 -7
index 063da636cb82..ff2c10d47529 100644
@@ -1189,11 +1189,9 @@ static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
 			return err;
 	}
 
-	if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
-		u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
-
-		br->hash_elasticity = val;
-	}
+	if (data[IFLA_BR_MCAST_HASH_ELASTICITY])
+		br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n",
+			RHT_ELASTICITY);
 
 	if (data[IFLA_BR_MCAST_HASH_MAX])
 		br->hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
@@ -1452,8 +1450,7 @@ static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
 		       br_opt_get(br, BROPT_MULTICAST_QUERIER)) ||
 	    nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
 		       br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) ||
-	    nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
-			br->hash_elasticity) ||
+	    nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY, RHT_ELASTICITY) ||
 	    nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
 	    nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
 			br->multicast_last_member_count) ||
net/bridge/br_private.h +0 -1
index cf1f7365deb3..ba50fa437a94 100644
@@ -372,7 +372,6 @@ struct net_bridge {
 
 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
 
-	u32				hash_elasticity;
 	u32				hash_max;
 
 	u32				multicast_last_member_count;
net/bridge/br_sysfs_br.c +3 -3
index f164cda30fe2..b05b94e9c595 100644
@@ -424,13 +424,13 @@ static DEVICE_ATTR_RW(multicast_querier);
 static ssize_t hash_elasticity_show(struct device *d,
 				    struct device_attribute *attr, char *buf)
 {
-	struct net_bridge *br = to_bridge(d);
-	return sprintf(buf, "%u\n", br->hash_elasticity);
+	return sprintf(buf, "%u\n", RHT_ELASTICITY);
 }
 
 static int set_elasticity(struct net_bridge *br, unsigned long val)
 {
-	br->hash_elasticity = val;
+	br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n",
+		RHT_ELASTICITY);
 	return 0;
 }
 
Signature · 1 line
2.17.2
#4 lore raw

PATCH v3 4/4 net: bridge: increase multicast's default maximum number of entries

In reply to Nikolay Aleksandrov

bridge's default hash_max was 512 which is rather conservative, now that we're using the generic rhashtable API which autoshrinks let's increase it to 4096 and move it to a define in br_private.h.

---
v2, v3: no change

 net/bridge/br_multicast.c | 2 +-
 net/bridge/br_private.h   | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
net/bridge/br_multicast.c +1 -1
index 804ee15c45c1..879cd2315769 100644
@@ -1741,7 +1741,7 @@ static void br_ip6_multicast_query_expired(struct timer_list *t)
 
 void br_multicast_init(struct net_bridge *br)
 {
-	br->hash_max = 512;
+	br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
 
 	br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
 	br->multicast_last_member_count = 2;
net/bridge/br_private.h +2 -0
index ba50fa437a94..5719b4d3e466 100644
@@ -31,6 +31,8 @@
 #define BR_PORT_BITS	10
 #define BR_MAX_PORTS	(1<<BR_PORT_BITS)
 
+#define BR_MULTICAST_DEFAULT_HASH_MAX 4096
+
 #define BR_VERSION	"2.3"
 
 /* Control of forwarding link local multicast */
Signature · 1 line
2.17.2
maintainer
#5 lore raw
In reply to Nikolay Aleksandrov

From: Nikolay Aleksandrov <[email protected]>
Date: Wed, 5 Dec 2018 15:14:23 +0200

Quoted 13 lines

The current bridge multicast code uses a custom rhashtable implementation which predates the generic rhashtable API. Patch 01 converts it to use the generic kernel rhashtable which simplifies the code a lot and removes duplicated functionality. The convert also makes hash_elasticity obsolete as the generic rhashtable already has such checks and has a fixed elasticity of RHT_ELASTICITY (16 currently) so we emit a warning whenever elasticity is set and return RHT_ELASTICITY when read (patch 03). Patch 02 converts the multicast code to use non-bh RCU flavor as it was mixing bh and non-bh. Since now we have the generic rhashtable which autoshrinks we can be more liberal with the default hash maximum so patch 04 increases it to 4096 and moves it to a define in br_private.h.

Quoted 5 lines

v3: add non-rcu br_mdb_get variant and use it where we have
multicast_lock, drop special hash_max handling and just set it where needed and use non-bh RCU consistently (patch 02, new)
v2: send the latest version of the set which handles when IGMP snooping
is not defined, changes are in patch 01

Series applied, thanks Nik.