Skip to contentSkip to conversation

Allow configuration of multipath hash seed

PATCH Superseded Petr Machata 8 participants 22 messages May 29, 2024 – Jun 3, 2024

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Petr Machata

author maintainer
#0 lore raw

cover Allow configuration of multipath hash seed

Let me just quote the commit message of patch #2 here to inform the motivation and some of the implementation:

When calculating hashes for the purpose of multipath forwarding, both IPv4 and IPv6 code currently fall back on flow_hash_from_keys(). That uses a randomly-generated seed. That's a fine choice by default, but unfortunately some deployments may need a tighter control over the seed used.

In this patchset, make the seed configurable by adding a new sysctl key, net.ipv4.fib_multipath_hash_seed to control the seed. This seed is used specifically for multipath forwarding and not for the other concerns that flow_hash_from_keys() is used for, such as queue selection. Expose the knob as sysctl because other such settings, such as headers to hash, are also handled that way.

Despite being placed in the net.ipv4 namespace, the multipath seed sysctl is used for both IPv4 and IPv6, similarly to e.g. a number of TCP variables. Like those, the multipath hash seed is a per-netns variable.

The new sysctl is added with permissions 0600 so that the hash is only readable and writable by root.

The seed used by flow_hash_from_keys() is a 128-bit quantity. However it seems that usually the seed is a much more modest value. 32 bits seem typical (Cisco, Cumulus), some systems go even lower. For that reason, and to decouple the user interface from implementation details, go with a 32-bit quantity, which is then quadruplicated to form the siphash key.

One example of use of this interface is avoiding hash polarization, where two ECMP routers, one behind the other, happen to make consistent hashing decisions, and as a result, part of the ECMP space of the latter router is never used. Another is a load balancer where several machines forward traffic to one of a number of leaves, and the forwarding decisions need to be made consistently. (This is a case of a desired hash polarization, mentioned e.g. in chapter 6.3 of [0].)

There has already been a proposal to include a hash seed control interface in the past[1]. This patchset uses broadly the same ideas, but limits the externally visible seed size to 32 bits.

- Patches #1-#2 contain the substance of the work
- Patch #3 is a mlxsw offload
- Patch #4 is a selftest

[0] https://www.usenix.org/system/files/conference/nsdi18/nsdi18-araujo.pdf
[1] https://lore.kernel.org/netdev/YIlVpYMCn%2F8WfE1P@rnd/

Petr Machata (4):
net: ipv4,ipv6: Pass multipath hash computation through a helper
net: ipv4: Add a sysctl to set multipath hash seed
mlxsw: spectrum_router: Apply user-defined multipath hash seed
selftests: forwarding: router_mpath_hash: Add a new selftest

 Documentation/networking/ip-sysctl.rst        |  10 +
 .../ethernet/mellanox/mlxsw/spectrum_router.c |  14 +-
 include/net/flow_dissector.h                  |   2 +
 include/net/ip_fib.h                          |  24 ++
 include/net/netns/ipv4.h                      |  10 +
 net/core/flow_dissector.c                     |   7 +
 net/ipv4/route.c                              |  12 +-
 net/ipv4/sysctl_net_ipv4.c                    |  82 +++++
 net/ipv6/route.c                              |  12 +-
 .../testing/selftests/net/forwarding/Makefile |   1 +
 .../net/forwarding/router_mpath_seed.sh       | 322 ++++++++++++++++++
 11 files changed, 482 insertions(+), 14 deletions(-)
 create mode 100755 tools/testing/selftests/net/forwarding/router_mpath_seed.sh
Signature · 1 line
2.45.0
author maintainer
#1 lore raw

PATCH 1/4 net: ipv4,ipv6: Pass multipath hash computation through a helper

The following patches will add a sysctl to control multipath hash seed. On a system where the seed is not set by hand, the algorithm will need to fall back to the default system-wide flow hash. In order to centralize this dispatch, add a helper, fib_multipath_hash_from_keys(), and have all IPv4 and IPv6 route.c invocations of flow_hash_from_keys() go through this helper instead.

---
 include/net/ip_fib.h |  7 +++++++
 net/ipv4/route.c     | 12 ++++++------
 net/ipv6/route.c     | 12 ++++++------
 3 files changed, 19 insertions(+), 12 deletions(-)
include/net/ip_fib.h +7 -0
index 9b2f69ba5e49..b8b3c07e8f7b 100644
@@ -521,6 +521,13 @@ void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig);
 int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
 		       const struct sk_buff *skb, struct flow_keys *flkeys);
 #endif
+
+static inline u32 fib_multipath_hash_from_keys(const struct net *net,
+					       struct flow_keys *keys)
+{
+	return flow_hash_from_keys(keys);
+}
+
 int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
 		 struct netlink_ext_ack *extack);
 void fib_select_multipath(struct fib_result *res, int hash);
net/ipv4/route.c +6 -6
index 5fd54103174f..daaccfb37802 100644
@@ -1930,7 +1930,7 @@ static u32 fib_multipath_custom_hash_outer(const struct net *net,
 		hash_keys.ports.dst = keys.ports.dst;
 
 	*p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
-	return flow_hash_from_keys(&hash_keys);
+	return fib_multipath_hash_from_keys(net, &hash_keys);
 }
 
 static u32 fib_multipath_custom_hash_inner(const struct net *net,
@@ -1979,7 +1979,7 @@ static u32 fib_multipath_custom_hash_inner(const struct net *net,
 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
 		hash_keys.ports.dst = keys.ports.dst;
 
-	return flow_hash_from_keys(&hash_keys);
+	return fib_multipath_hash_from_keys(net, &hash_keys);
 }
 
 static u32 fib_multipath_custom_hash_skb(const struct net *net,
@@ -2016,7 +2016,7 @@ static u32 fib_multipath_custom_hash_fl4(const struct net *net,
 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
 		hash_keys.ports.dst = fl4->fl4_dport;
 
-	return flow_hash_from_keys(&hash_keys);
+	return fib_multipath_hash_from_keys(net, &hash_keys);
 }
 
 /* if skb is set it will be used and fl4 can be NULL */
@@ -2037,7 +2037,7 @@ int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
 			hash_keys.addrs.v4addrs.src = fl4->saddr;
 			hash_keys.addrs.v4addrs.dst = fl4->daddr;
 		}
-		mhash = flow_hash_from_keys(&hash_keys);
+		mhash = fib_multipath_hash_from_keys(net, &hash_keys);
 		break;
 	case 1:
 		/* skb is currently provided only when forwarding */
@@ -2071,7 +2071,7 @@ int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
 			hash_keys.ports.dst = fl4->fl4_dport;
 			hash_keys.basic.ip_proto = fl4->flowi4_proto;
 		}
-		mhash = flow_hash_from_keys(&hash_keys);
+		mhash = fib_multipath_hash_from_keys(net, &hash_keys);
 		break;
 	case 2:
 		memset(&hash_keys, 0, sizeof(hash_keys));
@@ -2102,7 +2102,7 @@ int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
 			hash_keys.addrs.v4addrs.src = fl4->saddr;
 			hash_keys.addrs.v4addrs.dst = fl4->daddr;
 		}
-		mhash = flow_hash_from_keys(&hash_keys);
+		mhash = fib_multipath_hash_from_keys(net, &hash_keys);
 		break;
 	case 3:
 		if (skb)
net/ipv6/route.c +6 -6
index bbc2a0dd9314..9d561b9f0f75 100644
@@ -2372,7 +2372,7 @@ static u32 rt6_multipath_custom_hash_outer(const struct net *net,
 		hash_keys.ports.dst = keys.ports.dst;
 
 	*p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
-	return flow_hash_from_keys(&hash_keys);
+	return fib_multipath_hash_from_keys(net, &hash_keys);
 }
 
 static u32 rt6_multipath_custom_hash_inner(const struct net *net,
@@ -2421,7 +2421,7 @@ static u32 rt6_multipath_custom_hash_inner(const struct net *net,
 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
 		hash_keys.ports.dst = keys.ports.dst;
 
-	return flow_hash_from_keys(&hash_keys);
+	return fib_multipath_hash_from_keys(net, &hash_keys);
 }
 
 static u32 rt6_multipath_custom_hash_skb(const struct net *net,
@@ -2460,7 +2460,7 @@ static u32 rt6_multipath_custom_hash_fl6(const struct net *net,
 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
 		hash_keys.ports.dst = fl6->fl6_dport;
 
-	return flow_hash_from_keys(&hash_keys);
+	return fib_multipath_hash_from_keys(net, &hash_keys);
 }
 
 /* if skb is set it will be used and fl6 can be NULL */
@@ -2482,7 +2482,7 @@ u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
 		}
-		mhash = flow_hash_from_keys(&hash_keys);
+		mhash = fib_multipath_hash_from_keys(net, &hash_keys);
 		break;
 	case 1:
 		if (skb) {
@@ -2514,7 +2514,7 @@ u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
 			hash_keys.ports.dst = fl6->fl6_dport;
 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
 		}
-		mhash = flow_hash_from_keys(&hash_keys);
+		mhash = fib_multipath_hash_from_keys(net, &hash_keys);
 		break;
 	case 2:
 		memset(&hash_keys, 0, sizeof(hash_keys));
@@ -2551,7 +2551,7 @@ u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
 		}
-		mhash = flow_hash_from_keys(&hash_keys);
+		mhash = fib_multipath_hash_from_keys(net, &hash_keys);
 		break;
 	case 3:
 		if (skb)
Signature · 1 line
2.45.0
author maintainer
#2 lore raw

PATCH 2/4 net: ipv4: Add a sysctl to set multipath hash seed

In reply to Petr Machata

When calculating hashes for the purpose of multipath forwarding, both IPv4 and IPv6 code currently fall back on flow_hash_from_keys(). That uses a randomly-generated seed. That's a fine choice by default, but unfortunately some deployments may need a tighter control over the seed used.

In this patch, make the seed configurable by adding a new sysctl key, net.ipv4.fib_multipath_hash_seed to control the seed. This seed is used specifically for multipath forwarding and not for the other concerns that flow_hash_from_keys() is used for, such as queue selection. Expose the knob as sysctl because other such settings, such as headers to hash, are also handled that way. Like those, the multipath hash seed is a per-netns variable.

Despite being placed in the net.ipv4 namespace, the multipath seed sysctl is used for both IPv4 and IPv6, similarly to e.g. a number of TCP variables.

The seed used by flow_hash_from_keys() is a 128-bit quantity. However it seems that usually the seed is a much more modest value. 32 bits seem typical (Cisco, Cumulus), some systems go even lower. For that reason, and to decouple the user interface from implementation details, go with a 32-bit quantity, which is then quadruplicated to form the siphash key.

For locking, use RTNL instead of a custom lock. This based on feedback given to a patch from Pavel Balaev, which also aimed to introduce multipath hash seed control [0].

[0] https://lore.kernel.org/netdev/[email protected]/

---

Cc Jonathan Corbet <[email protected]>
Cc Simon Horman <[email protected]>
 Documentation/networking/ip-sysctl.rst | 10 ++++
 include/net/flow_dissector.h           |  2 +
 include/net/ip_fib.h                   | 19 +++++-
 include/net/netns/ipv4.h               | 10 ++++
 net/core/flow_dissector.c              |  7 +++
 net/ipv4/sysctl_net_ipv4.c             | 82 ++++++++++++++++++++++++++
 6 files changed, 129 insertions(+), 1 deletion(-)
Documentation/networking/ip-sysctl.rst +10 -0
index bd50df6a5a42..afcf3f323965 100644
@@ -131,6 +131,16 @@ fib_multipath_hash_fields - UNSIGNED INTEGER
 
 	Default: 0x0007 (source IP, destination IP and IP protocol)
 
+fib_multipath_hash_seed - UNSIGNED INTEGER
+	The seed value used when calculating hash for multipath routes. Applies
+	to both IPv4 and IPv6 datapath. Only valid for kernels built with
+	CONFIG_IP_ROUTE_MULTIPATH enabled.
+
+	When set to 0, the seed value used for multipath routing defaults to an
+	internal random-generated one.
+
+	Default: 0 (random)
+
 fib_sync_mem - UNSIGNED INTEGER
 	Amount of dirty memory from fib entries that can be backlogged before
 	synchronize_rcu is forced.
include/net/flow_dissector.h +2 -0
index 9ab376d1a677..a5423219dee1 100644
@@ -433,6 +433,8 @@ static inline bool flow_keys_have_l4(const struct flow_keys *keys)
 }
 
 u32 flow_hash_from_keys(struct flow_keys *keys);
+u32 flow_hash_from_keys_seed(struct flow_keys *keys,
+			     const siphash_key_t *keyval);
 void skb_flow_get_icmp_tci(const struct sk_buff *skb,
 			   struct flow_dissector_key_icmp *key_icmp,
 			   const void *data, int thoff, int hlen);
include/net/ip_fib.h +18 -1
index b8b3c07e8f7b..785c571e2cef 100644
@@ -520,13 +520,30 @@ void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig);
 #ifdef CONFIG_IP_ROUTE_MULTIPATH
 int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
 		       const struct sk_buff *skb, struct flow_keys *flkeys);
-#endif
 
+static inline u32 fib_multipath_hash_from_keys(const struct net *net,
+					       struct flow_keys *keys)
+{
+	struct sysctl_fib_multipath_hash_seed *mphs;
+	u32 ret;
+
+	rcu_read_lock();
+	mphs = rcu_dereference(net->ipv4.sysctl_fib_multipath_hash_seed);
+	if (likely(!mphs))
+		ret = flow_hash_from_keys(keys);
+	else
+		ret = flow_hash_from_keys_seed(keys, &mphs->seed);
+	rcu_read_unlock();
+
+	return ret;
+}
+#else
 static inline u32 fib_multipath_hash_from_keys(const struct net *net,
 					       struct flow_keys *keys)
 {
 	return flow_hash_from_keys(keys);
 }
+#endif
 
 int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
 		 struct netlink_ext_ack *extack);
include/net/netns/ipv4.h +10 -0
index c356c458b340..1f5043d32cb0 100644
@@ -40,6 +40,14 @@ struct inet_timewait_death_row {
 
 struct tcp_fastopen_context;
 
+#ifdef CONFIG_IP_ROUTE_MULTIPATH
+struct sysctl_fib_multipath_hash_seed {
+	siphash_aligned_key_t	seed;
+	u32			user_seed;
+	struct rcu_head		rcu;
+};
+#endif
+
 struct netns_ipv4 {
 	/* Cacheline organization can be found documented in
 	 * Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst.
@@ -245,6 +253,8 @@ struct netns_ipv4 {
 #endif
 #endif
 #ifdef CONFIG_IP_ROUTE_MULTIPATH
+	struct sysctl_fib_multipath_hash_seed
+					__rcu *sysctl_fib_multipath_hash_seed;
 	u32 sysctl_fib_multipath_hash_fields;
 	u8 sysctl_fib_multipath_use_neigh;
 	u8 sysctl_fib_multipath_hash_policy;
net/core/flow_dissector.c +7 -0
index f82e9a7d3b37..7b3283ad5b39 100644
@@ -1792,6 +1792,13 @@ u32 flow_hash_from_keys(struct flow_keys *keys)
 }
 EXPORT_SYMBOL(flow_hash_from_keys);
 
+u32 flow_hash_from_keys_seed(struct flow_keys *keys,
+			     const siphash_key_t *keyval)
+{
+	return __flow_hash_from_keys(keys, keyval);
+}
+EXPORT_SYMBOL(flow_hash_from_keys_seed);
+
 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
 				  struct flow_keys *keys,
 				  const siphash_key_t *keyval)
net/ipv4/sysctl_net_ipv4.c +82 -0

100 lines not on this page · raw message

Signature · 1 line
2.45.0
author maintainer
#14 lore raw

PATCH 3/4 mlxsw: spectrum_router: Apply user-defined multipath hash seed

In reply to Petr Machata

When Spectrum machines compute hash for the purposes of ECMP routing, they use a seed specified through RECR_v2 (Router ECMP Configuration Register). Up until now mlxsw computed the seed by hashing the machine's base MAC. Now that we can optionally have a user-provided seed, use that if possible.

---
 .../net/ethernet/mellanox/mlxsw/spectrum_router.c  | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c +12 -2
index 40ba314fbc72..e5b669f0822d 100644
@@ -11449,13 +11449,23 @@ static int mlxsw_sp_mp_hash_parsing_depth_adjust(struct mlxsw_sp *mlxsw_sp,
 static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp)
 {
 	bool old_inc_parsing_depth, new_inc_parsing_depth;
+	struct sysctl_fib_multipath_hash_seed *mphs;
 	struct mlxsw_sp_mp_hash_config config = {};
+	struct net *net = mlxsw_sp_net(mlxsw_sp);
 	char recr2_pl[MLXSW_REG_RECR2_LEN];
 	unsigned long bit;
-	u32 seed;
+	u32 seed = 0;
 	int err;
 
-	seed = jhash(mlxsw_sp->base_mac, sizeof(mlxsw_sp->base_mac), 0);
+	rcu_read_lock();
+	mphs = rcu_dereference(net->ipv4.sysctl_fib_multipath_hash_seed);
+	if (mphs)
+		seed = mphs->user_seed;
+	rcu_read_unlock();
+
+	if (!seed)
+		seed = jhash(mlxsw_sp->base_mac, sizeof(mlxsw_sp->base_mac), 0);
+
 	mlxsw_reg_recr2_pack(recr2_pl, seed);
 	mlxsw_sp_mp4_hash_init(mlxsw_sp, &config);
 	mlxsw_sp_mp6_hash_init(mlxsw_sp, &config);
Signature · 1 line
2.45.0
author maintainer
#15 lore raw

PATCH 4/4 selftests: forwarding: router_mpath_hash: Add a new selftest

In reply to Petr Machata

Add a selftest that exercises the sysctl added in the previous patches.

Test that set/get works as expected; that across seeds we eventually hit all NHs (test_mpath_seed_*); and that a given seed keeps hitting the same NHs even across seed changes (test_mpath_seed_stability_*).

---

 .../testing/selftests/net/forwarding/Makefile |   1 +
 .../net/forwarding/router_mpath_seed.sh       | 322 ++++++++++++++++++
 2 files changed, 323 insertions(+)
 create mode 100755 tools/testing/selftests/net/forwarding/router_mpath_seed.sh
tools/testing/selftests/net/forwarding/Makefile +1 -0
index fa7b59ff4029..99576d7ecbf6 100644
@@ -70,6 +70,7 @@ TEST_PROGS = bridge_fdb_learning_limit.sh \
 	router_broadcast.sh \
 	router_mpath_nh_res.sh \
 	router_mpath_nh.sh \
+	router_mpath_seed.sh \
 	router_multicast.sh \
 	router_multipath.sh \
 	router_nh.sh \
tools/testing/selftests/net/forwarding/router_mpath_seed.sh new file +322 -0

322 lines not on this page · raw message

Signature · 1 line
2.45.0
#16 lore raw
In reply to Petr Machata
Quoted 56 lines · Petr Machata

Let me just quote the commit message of patch #2 here to inform the motivation and some of the implementation:

When calculating hashes for the purpose of multipath forwarding, both IPv4 and IPv6 code currently fall back on flow_hash_from_keys(). That uses a randomly-generated seed. That's a fine choice by default, but unfortunately some deployments may need a tighter control over the seed used.

In this patchset, make the seed configurable by adding a new sysctl key, net.ipv4.fib_multipath_hash_seed to control the seed. This seed is used specifically for multipath forwarding and not for the other concerns that flow_hash_from_keys() is used for, such as queue selection. Expose the knob as sysctl because other such settings, such as headers to hash, are also handled that way.

Despite being placed in the net.ipv4 namespace, the multipath seed sysctl is used for both IPv4 and IPv6, similarly to e.g. a number of TCP variables. Like those, the multipath hash seed is a per-netns variable.

The new sysctl is added with permissions 0600 so that the hash is only readable and writable by root.

The seed used by flow_hash_from_keys() is a 128-bit quantity. However it seems that usually the seed is a much more modest value. 32 bits seem typical (Cisco, Cumulus), some systems go even lower. For that reason, and to decouple the user interface from implementation details, go with a 32-bit quantity, which is then quadruplicated to form the siphash key.

One example of use of this interface is avoiding hash polarization, where two ECMP routers, one behind the other, happen to make consistent hashing decisions, and as a result, part of the ECMP space of the latter router is never used. Another is a load balancer where several machines forward traffic to one of a number of leaves, and the forwarding decisions need to be made consistently. (This is a case of a desired hash polarization, mentioned e.g. in chapter 6.3 of [0].)

There has already been a proposal to include a hash seed control interface in the past[1]. This patchset uses broadly the same ideas, but limits the externally visible seed size to 32 bits.

- Patches #1-#2 contain the substance of the work
- Patch #3 is a mlxsw offload
- Patch #4 is a selftest

[0] https://www.usenix.org/system/files/conference/nsdi18/nsdi18-araujo.pdf
[1] https://lore.kernel.org/netdev/YIlVpYMCn%2F8WfE1P@rnd/

Petr Machata (4):
net: ipv4,ipv6: Pass multipath hash computation through a helper
net: ipv4: Add a sysctl to set multipath hash seed
mlxsw: spectrum_router: Apply user-defined multipath hash seed
selftests: forwarding: router_mpath_hash: Add a new selftest

Quoted 14 lines
 Documentation/networking/ip-sysctl.rst        |  10 +
 .../ethernet/mellanox/mlxsw/spectrum_router.c |  14 +-
 include/net/flow_dissector.h                  |   2 +
 include/net/ip_fib.h                          |  24 ++
 include/net/netns/ipv4.h                      |  10 +
 net/core/flow_dissector.c                     |   7 +
 net/ipv4/route.c                              |  12 +-
 net/ipv4/sysctl_net_ipv4.c                    |  82 +++++
 net/ipv6/route.c                              |  12 +-
 .../testing/selftests/net/forwarding/Makefile |   1 +
 .../net/forwarding/router_mpath_seed.sh       | 322 ++++++++++++++++++
 11 files changed, 482 insertions(+), 14 deletions(-)
 create mode 100755 tools/testing/selftests/net/forwarding/router_mpath_seed.sh

Hi,
I think that using memory management for such simple task is an overkill. Would it be simpler to define 2 x 4 byte seed variables in netns_ipv4 (e.g. user_seed, mp_seed). One is set only by the user through the sysctl, which would also set mp_seed. Then you can use mp_seed in the fast-path to construct that siphash key. If the user_seed is set to 0 then you reset to some static init hash value that is generated on init_net's creation. The idea is to avoid leaking that initial seed, to have the same seed for all netns (known behaviour), be able to recognize when a seed was set and if the user sets a seed then overwrite it for that ns, but to be able to reset it as well.
Since 32 bits are enough I don't see why we should be using the flow hash seed, note that init_net's initialization already uses get_random_bytes() for hashes. This seems like a simpler scheme that doesn't require memory management for a 32 bit seed. Also it has the benefit that it will remove the test when generating a hash because in the initial/non-user-set case we just have the initial seed in mp_seed which is used to generate the siphash key, i.e. we always use that internal seed for the hash, regardless if it was set by the user or it's the initial seed.

That's just one suggestion, if you decide to use more memory you can keep the whole key in netns_ipv4 instead, the point is I don't think we need memory management for this value.

Cheers,
Nik

author maintainer
#17 lore raw
Quoted 7 lines · Nikolay Aleksandrov

I think that using memory management for such simple task is an overkill. Would it be simpler to define 2 x 4 byte seed variables in netns_ipv4 (e.g. user_seed, mp_seed). One is set only by the user through the sysctl, which would also set mp_seed. Then you can use mp_seed in the fast-path to construct that siphash key. If the user_seed is set to 0 then you reset to some static init hash value that is generated on init_net's creation. The idea

Currently the flow dissector siphash key is initialized lazily so that the pool of random bytes is full when the initialization is done:

    https://lore.kernel.org/all/[email protected]
    https://lore.kernel.org/all/[email protected]

I'm not sure how important that is -- the mailing does not really discuss much in the way of rationale, and admits it's not critical. But initializing the seed during net init would undo that. At the same time, initializing it lazily would be a bit of a mess, as we would have to possibly retroactively update mp_hash as well, which would be racy vs. user_hash update unless locked. So dunno.

If you are OK with giving up on the siphash key "quality", I'm fine with this.

Alternatively I can keep the dispatch in like it currently is. I.e.:

	if (user_seed) {
		sip_hash = construct(user_seed)
		return flow_hash_from_keys_seed(sip_hash)
	} else {
		return flow_hash_from_keys()
	}

I wanted to have the flow dispatcher hash init early as well, as it made the code branch-free like you note below, but then Ido dug out that there are $reasons for how it's currently done.

Quoted 6 lines

is to avoid leaking that initial seed, to have the same seed for all netns (known behaviour), be able to recognize when a seed was set and if the user sets a seed then overwrite it for that ns, but to be able to reset it as well.
Since 32 bits are enough I don't see why we should be using the flow hash seed, note that init_net's initialization already

No deep reason in using the dissector hash as far as I'm concerned. I just didn't want to change things arbitrarily, so kept the current behavior except where I needed it to change.

Quoted 11 lines

uses get_random_bytes() for hashes. This seems like a simpler scheme that doesn't require memory management for a 32 bit seed. Also it has the benefit that it will remove the test when generating a hash because in the initial/non-user-set case we just have the initial seed in mp_seed which is used to generate the siphash key, i.e. we always use that internal seed for the hash, regardless if it was set by the user or it's the initial seed.

That's just one suggestion, if you decide to use more memory you can keep the whole key in netns_ipv4 instead, the point is I don't think we need memory management for this value.

I kept the RCU stuff in because it makes it easy to precompute the siphash key while allowing readers to access it lock-free. I could inline it and guard with a seqlock instead, but that's a bit messier code-wise. Or indeed construct in-situ, it's an atomic access plus like four instructions or something like that.

#18 lore raw
Quoted 17 lines · Petr Machata
Quoted 7 lines · Nikolay Aleksandrov

I think that using memory management for such simple task is an overkill. Would it be simpler to define 2 x 4 byte seed variables in netns_ipv4 (e.g. user_seed, mp_seed). One is set only by the user through the sysctl, which would also set mp_seed. Then you can use mp_seed in the fast-path to construct that siphash key. If the user_seed is set to 0 then you reset to some static init hash value that is generated on init_net's creation. The idea

Currently the flow dissector siphash key is initialized lazily so that the pool of random bytes is full when the initialization is done:

    https://lore.kernel.org/all/[email protected]
    https://lore.kernel.org/all/[email protected]
Quoted 6 lines

I'm not sure how important that is -- the mailing does not really discuss much in the way of rationale, and admits it's not critical. But initializing the seed during net init would undo that. At the same time, initializing it lazily would be a bit of a mess, as we would have to possibly retroactively update mp_hash as well, which would be racy vs. user_hash update unless locked. So dunno.

If you want to keep the late init, untested:
init_mp_seed_once() -> DO_ONCE(func (net_get_random_once(&init_mp_seed), memcpy(&init_net.mp_seed, &init_mp_seed))

Quoted 4 lines

If you are OK with giving up on the siphash key "quality", I'm fine with this.

IMO that's fine, early init of the seed wouldn't be a problem. net's hash_mix is already initialized early.

Quoted 11 lines

Alternatively I can keep the dispatch in like it currently is. I.e.:

	if (user_seed) {
		sip_hash = construct(user_seed)
		return flow_hash_from_keys_seed(sip_hash)
	} else {
		return flow_hash_from_keys()
	}

I wanted to have the flow dispatcher hash init early as well, as it made the code branch-free like you note below, but then Ido dug out that

+1

Quoted 24 lines

there are $reasons for how it's currently done.

Quoted 6 lines
Quoted 1 line

is to avoid leaking that initial seed, to have the same seed

for all netns (known behaviour), be able to recognize when a seed was set and if the user sets a seed then overwrite it for that ns, but to be able to reset it as well.
Since 32 bits are enough I don't see why we should be using the flow hash seed, note that init_net's initialization already

No deep reason in using the dissector hash as far as I'm concerned. I just didn't want to change things arbitrarily, so kept the current behavior except where I needed it to change.

Quoted 11 lines

uses get_random_bytes() for hashes. This seems like a simpler scheme that doesn't require memory management for a 32 bit seed. Also it has the benefit that it will remove the test when generating a hash because in the initial/non-user-set case we just have the initial seed in mp_seed which is used to generate the siphash key, i.e. we always use that internal seed for the hash, regardless if it was set by the user or it's the initial seed.

That's just one suggestion, if you decide to use more memory you can keep the whole key in netns_ipv4 instead, the point is I don't think we need memory management for this value.

Quoted 5 lines

I kept the RCU stuff in because it makes it easy to precompute the siphash key while allowing readers to access it lock-free. I could inline it and guard with a seqlock instead, but that's a bit messier code-wise. Or indeed construct in-situ, it's an atomic access plus like four instructions or something like that.

You can READ/WRITE_ONCE() the full 8 bytes every time so it's lock-free and consistent view of both values for observers. For fast-path it'll only be accessing one of the two values, so it's fine either way. You can use barriers to ensure latest value is seen by interested readers, but for most eventual consistency would be enough.

#19 lore raw
Quoted 25 lines · Nikolay Aleksandrov
Quoted 17 lines · Petr Machata
Quoted 7 lines · Nikolay Aleksandrov

I think that using memory management for such simple task is an overkill. Would it be simpler to define 2 x 4 byte seed variables in netns_ipv4 (e.g. user_seed, mp_seed). One is set only by the user through the sysctl, which would also set mp_seed. Then you can use mp_seed in the fast-path to construct that siphash key. If the user_seed is set to 0 then you reset to some static init hash value that is generated on init_net's creation. The idea

Currently the flow dissector siphash key is initialized lazily so that the pool of random bytes is full when the initialization is done:

    https://lore.kernel.org/all/[email protected]
    https://lore.kernel.org/all/[email protected]
Quoted 6 lines

I'm not sure how important that is -- the mailing does not really discuss much in the way of rationale, and admits it's not critical. But initializing the seed during net init would undo that. At the same time, initializing it lazily would be a bit of a mess, as we would have to possibly retroactively update mp_hash as well, which would be racy vs. user_hash update unless locked. So dunno.

Quoted 4 lines

If you want to keep the late init, untested:
init_mp_seed_once() -> DO_ONCE(func (net_get_random_once(&init_mp_seed), memcpy(&init_net.mp_seed, &init_mp_seed))

Blah, just get_random_bytes() instead of net_get*. :)
It'll be executed once anyway. But again I think we can do without all of this.

Quoted 8 lines
Quoted 4 lines

If you are OK with giving up on the siphash key "quality", I'm fine with this.

IMO that's fine, early init of the seed wouldn't be a problem. net's hash_mix is already initialized early.

[snip]

Quoted 7 lines
Quoted 6 lines

I kept the RCU stuff in because it makes it easy to precompute the siphash key while allowing readers to access it lock-free. I could inline it and guard with a seqlock instead, but that's a bit messier code-wise. Or indeed construct in-situ, it's an atomic access plus like four instructions or something like that.

Quoted 6 lines

You can READ/WRITE_ONCE() the full 8 bytes every time so it's lock-free and consistent view of both values for observers. For fast-path it'll only be accessing one of the two values, so it's fine either way. You can use barriers to ensure latest value is seen by interested readers, but for most eventual consistency would be enough.

Actually aren't we interested only in user_seed in the external reader case? We don't care what's in mp_seed, so this is much simpler.

#20 lore raw
Quoted 7 lines · Nikolay Aleksandrov
Quoted 5 lines · Nikolay Aleksandrov
Quoted 3 lines · Petr Machata

Nikolay Aleksandrov <[email protected]> writes:

[snip]

[snip snip]

Quoted 14 lines
Quoted 7 lines
Quoted 6 lines

I kept the RCU stuff in because it makes it easy to precompute the siphash key while allowing readers to access it lock-free. I could inline it and guard with a seqlock instead, but that's a bit messier code-wise. Or indeed construct in-situ, it's an atomic access plus like four instructions or something like that.

Quoted 6 lines

You can READ/WRITE_ONCE() the full 8 bytes every time so it's lock-free and consistent view of both values for observers. For fast-path it'll only be accessing one of the two values, so it's fine either way. You can use barriers to ensure latest value is seen by interested readers, but for most eventual consistency would be enough.

Quoted 3 lines

Actually aren't we interested only in user_seed in the external reader case? We don't care what's in mp_seed, so this is much simpler.

Oh, I misunderstood you, didn't I? :) Were you talking about constructing the siphash key in the fast-path above? If yes, then sure it's a few instructions but nothing conditional. I don't think we need anything atomic in that case.

maintainer
#3 lore raw

Re: [PATCH net-next 2/4] net: ipv4: Add a sysctl to set multipath hash seed

In reply to Petr Machata

Documentation/networking/ip-sysctl.rst fib_multipath_hash_fields - UNSIGNED INTEGER · line 135

Quoted 2 lines · On Wed, 29 May 2024 13:18:42 +0200 Petr Machata wrote:
Documentation/networking/ip-sysctl.rst +2 -0
+fib_multipath_hash_seed - UNSIGNED INTEGER
+	The seed value used when calculating hash for multipath routes. Applies

nits..

For RSS we call it key rather than seed, is calling it seed well established for ECMP?

Can we also call out that hashing implementation is not well defined?

Documentation/networking/ip-sysctl.rst fib_multipath_hash_fields - UNSIGNED INTEGER · line 136

Quoted 1 line
Documentation/networking/ip-sysctl.rst +1 -0
+	to both IPv4 and IPv6 datapath. Only valid for kernels built with

s/valid/present/ ?

Quoted 4 lines
Documentation/networking/ip-sysctl.rst +4 -0
+	CONFIG_IP_ROUTE_MULTIPATH enabled.
+
+	When set to 0, the seed value used for multipath routing defaults to an
+	internal random-generated one.
#8 lore raw

Re: [PATCH net-next 2/4] net: ipv4: Add a sysctl to set multipath hash seed

In reply to Petr Machata
Quoted 18 lines · Petr Machata

When calculating hashes for the purpose of multipath forwarding, both IPv4 and IPv6 code currently fall back on flow_hash_from_keys(). That uses a randomly-generated seed. That's a fine choice by default, but unfortunately some deployments may need a tighter control over the seed used.

In this patch, make the seed configurable by adding a new sysctl key, net.ipv4.fib_multipath_hash_seed to control the seed. This seed is used specifically for multipath forwarding and not for the other concerns that flow_hash_from_keys() is used for, such as queue selection. Expose the knob as sysctl because other such settings, such as headers to hash, are also handled that way. Like those, the multipath hash seed is a per-netns variable.

Despite being placed in the net.ipv4 namespace, the multipath seed sysctl is used for both IPv4 and IPv6, similarly to e.g. a number of TCP variables.

...

net/ipv4/sysctl_net_ipv4.c static int proc_fib_multipath_hash_fields(struct ctl_table *table, int write, · line 495

Quoted 5 lines
net/ipv4/sysctl_net_ipv4.c +5 -0
+       rtnl_lock();
+       old = rcu_replace_pointer_rtnl(net->ipv4.sysctl_fib_multipath_hash_seed,
+                                      mphs);
+       rtnl_unlock();
+

In case you keep RCU for the next version, please do not use rtnl_lock() here.

A simple xchg() will work just fine.

old = xchg((__force struct struct sysctl_fib_multipath_hash_seed **)&net->ipv4.sysctl_fib_multipath_hash_seed,

                 mphs);
maintainer
#4 lore raw

Re: [PATCH net-next 2/4] net: ipv4: Add a sysctl to set multipath hash seed

In reply to Jakub Kicinski
Quoted 8 lines · Jakub Kicinski

Documentation/networking/ip-sysctl.rst fib_multipath_hash_fields - UNSIGNED INTEGER · line 135

Quoted 2 lines · On Wed, 29 May 2024 13:18:42 +0200 Petr Machata wrote:
Documentation/networking/ip-sysctl.rst +2 -0
+fib_multipath_hash_seed - UNSIGNED INTEGER
+	The seed value used when calculating hash for multipath routes. Applies

nits..

For RSS we call it key rather than seed, is calling it seed well established for ECMP?

I have only seen documentation where it is called "seed". Examples:

Cumulus:
https://docs.nvidia.com/networking-ethernet-software/cumulus-linux-59/Layer-3/Routing/Equal-Cost-Multipath-Load-Sharing/#unique-hash-seed

Arista:
https://arista.my.site.com/AristaCommunity/s/article/hashing-for-l2-port-channels-and-l3-ecmp

Research from Fastly around load balancing (Section 6.3):
https://www.usenix.org/system/files/conference/nsdi18/nsdi18-araujo.pdf

#5 lore raw

Re: [PATCH net-next 2/4] net: ipv4: Add a sysctl to set multipath hash seed

Le 02/06/2024 à 13:15, Ido Schimmel a écrit :

Quoted 9 lines · Jakub Kicinski
Quoted 8 lines · On Wed, 29 May 2024 13:18:42 +0200 Petr Machata wrote:

Documentation/networking/ip-sysctl.rst fib_multipath_hash_fields - UNSIGNED INTEGER · line 135

Quoted 2 lines
Documentation/networking/ip-sysctl.rst +2 -0
+fib_multipath_hash_seed - UNSIGNED INTEGER
+	The seed value used when calculating hash for multipath routes. Applies

nits..

For RSS we call it key rather than seed, is calling it seed well established for ECMP?

It seems standard for me (we call it like this in our products).

Quoted 12 lines

I have only seen documentation where it is called "seed". Examples:

Cumulus:
https://docs.nvidia.com/networking-ethernet-software/cumulus-linux-59/Layer-3/Routing/Equal-Cost-Multipath-Load-Sharing/#unique-hash-seed

Arista:
https://arista.my.site.com/AristaCommunity/s/article/hashing-for-l2-port-channels-and-l3-ecmp

Research from Fastly around load balancing (Section 6.3):
https://www.usenix.org/system/files/conference/nsdi18/nsdi18-araujo.pdf

You can add some others:

https://www.juniper.net/documentation/us/en/software/junos/interfaces-ethernet-switches/topics/topic-map/switches-interface-resilient-hashing.html

https://www.cisco.com/c/en/us/td/docs/switches/datacenter/nexus9000/sw/6-x/unicast/configuration/guide/l3_cli_nxos/l3_manage-routes.html

#9 lore raw

Re: [PATCH net-next 2/4] net: ipv4: Add a sysctl to set multipath hash seed

In reply to Eric Dumazet
Quoted 27 lines · Eric Dumazet
Quoted 18 lines · Petr Machata

When calculating hashes for the purpose of multipath forwarding, both IPv4 and IPv6 code currently fall back on flow_hash_from_keys(). That uses a randomly-generated seed. That's a fine choice by default, but unfortunately some deployments may need a tighter control over the seed used.

In this patch, make the seed configurable by adding a new sysctl key, net.ipv4.fib_multipath_hash_seed to control the seed. This seed is used specifically for multipath forwarding and not for the other concerns that flow_hash_from_keys() is used for, such as queue selection. Expose the knob as sysctl because other such settings, such as headers to hash, are also handled that way. Like those, the multipath hash seed is a per-netns variable.

Despite being placed in the net.ipv4 namespace, the multipath seed sysctl is used for both IPv4 and IPv6, similarly to e.g. a number of TCP variables.

...

net/ipv4/sysctl_net_ipv4.c static int proc_fib_multipath_hash_fields(struct ctl_table *table, int write, · line 495

Quoted 5 lines
net/ipv4/sysctl_net_ipv4.c +5 -0
+       rtnl_lock();
+       old = rcu_replace_pointer_rtnl(net->ipv4.sysctl_fib_multipath_hash_seed,
+                                      mphs);
+       rtnl_unlock();
+
Quoted 7 lines

In case you keep RCU for the next version, please do not use rtnl_lock() here.

A simple xchg() will work just fine.

old = xchg((__force struct struct sysctl_fib_multipath_hash_seed **)&net->ipv4.sysctl_fib_multipath_hash_seed,

                 mphs);

We added a macro to do this kind of thing without triggering any of the RCU type linter warnings, in:

76c8eaafe4f0 ("rcu: Create an unrcu_pointer() to remove __rcu from a pointer")

So as an alternative to open-coding the cast, something like this could work - I guess it's mostly a matter of taste:

old = unrcu_pointer(xchg(&net->ipv4.sysctl_fib_multipath_hash_seed, RCU_INITIALIZER(mphs)));

-Toke

#10 lore raw

Re: [PATCH net-next 2/4] net: ipv4: Add a sysctl to set multipath hash seed

Quoted 41 lines · Toke Høiland-Jørgensen
Quoted 27 lines · Eric Dumazet
Quoted 18 lines · Petr Machata

When calculating hashes for the purpose of multipath forwarding, both IPv4 and IPv6 code currently fall back on flow_hash_from_keys(). That uses a randomly-generated seed. That's a fine choice by default, but unfortunately some deployments may need a tighter control over the seed used.

In this patch, make the seed configurable by adding a new sysctl key, net.ipv4.fib_multipath_hash_seed to control the seed. This seed is used specifically for multipath forwarding and not for the other concerns that flow_hash_from_keys() is used for, such as queue selection. Expose the knob as sysctl because other such settings, such as headers to hash, are also handled that way. Like those, the multipath hash seed is a per-netns variable.

Despite being placed in the net.ipv4 namespace, the multipath seed sysctl is used for both IPv4 and IPv6, similarly to e.g. a number of TCP variables.

...

net/ipv4/sysctl_net_ipv4.c static int proc_fib_multipath_hash_fields(struct ctl_table *table, int write, · line 495

Quoted 5 lines
net/ipv4/sysctl_net_ipv4.c +5 -0
+       rtnl_lock();
+       old = rcu_replace_pointer_rtnl(net->ipv4.sysctl_fib_multipath_hash_seed,
+                                      mphs);
+       rtnl_unlock();
+
Quoted 7 lines

In case you keep RCU for the next version, please do not use rtnl_lock() here.

A simple xchg() will work just fine.

old = xchg((__force struct struct sysctl_fib_multipath_hash_seed **)&net->ipv4.sysctl_fib_multipath_hash_seed,

                 mphs);

We added a macro to do this kind of thing without triggering any of the RCU type linter warnings, in:

Quoted 6 lines

76c8eaafe4f0 ("rcu: Create an unrcu_pointer() to remove __rcu from a pointer")

So as an alternative to open-coding the cast, something like this could work - I guess it's mostly a matter of taste:

old = unrcu_pointer(xchg(&net->ipv4.sysctl_fib_multipath_hash_seed, RCU_INITIALIZER(mphs)));

Good to know, thanks.

Not sure why __kernel qualifier has been put there.

#11 lore raw

Re: [PATCH net-next 2/4] net: ipv4: Add a sysctl to set multipath hash seed

Quoted 44 lines · Eric Dumazet
Quoted 43 lines · Toke Høiland-Jørgensen
Quoted 27 lines · Eric Dumazet
Quoted 18 lines · Petr Machata

When calculating hashes for the purpose of multipath forwarding, both IPv4 and IPv6 code currently fall back on flow_hash_from_keys(). That uses a randomly-generated seed. That's a fine choice by default, but unfortunately some deployments may need a tighter control over the seed used.

In this patch, make the seed configurable by adding a new sysctl key, net.ipv4.fib_multipath_hash_seed to control the seed. This seed is used specifically for multipath forwarding and not for the other concerns that flow_hash_from_keys() is used for, such as queue selection. Expose the knob as sysctl because other such settings, such as headers to hash, are also handled that way. Like those, the multipath hash seed is a per-netns variable.

Despite being placed in the net.ipv4 namespace, the multipath seed sysctl is used for both IPv4 and IPv6, similarly to e.g. a number of TCP variables.

...

net/ipv4/sysctl_net_ipv4.c static int proc_fib_multipath_hash_fields(struct ctl_table *table, int write, · line 495

Quoted 5 lines
net/ipv4/sysctl_net_ipv4.c +5 -0
+       rtnl_lock();
+       old = rcu_replace_pointer_rtnl(net->ipv4.sysctl_fib_multipath_hash_seed,
+                                      mphs);
+       rtnl_unlock();
+
Quoted 7 lines

In case you keep RCU for the next version, please do not use rtnl_lock() here.

A simple xchg() will work just fine.

old = xchg((__force struct struct sysctl_fib_multipath_hash_seed **)&net->ipv4.sysctl_fib_multipath_hash_seed,

                 mphs);

We added a macro to do this kind of thing without triggering any of the RCU type linter warnings, in:

76c8eaafe4f0 ("rcu: Create an unrcu_pointer() to remove __rcu from a pointer")

Quoted 8 lines
Quoted 4 lines

So as an alternative to open-coding the cast, something like this could work - I guess it's mostly a matter of taste:

old = unrcu_pointer(xchg(&net->ipv4.sysctl_fib_multipath_hash_seed, RCU_INITIALIZER(mphs)));

Good to know, thanks.

Not sure why __kernel qualifier has been put there.

Not sure either. Paul, care to enlighten us? :)

-Toke

author maintainer
#21 lore raw
In reply to Nikolay Aleksandrov
Quoted 16 lines · Nikolay Aleksandrov
Quoted 15 lines · Nikolay Aleksandrov
Quoted 13 lines · Nikolay Aleksandrov
Quoted 6 lines · Petr Machata

I kept the RCU stuff in because it makes it easy to precompute the siphash key while allowing readers to access it lock-free. I could inline it and guard with a seqlock instead, but that's a bit messier code-wise. Or indeed construct in-situ, it's an atomic access plus like four instructions or something like that.

You can READ/WRITE_ONCE() the full 8 bytes every time so it's lock-free and consistent view of both values for observers. For fast-path it'll only be accessing one of the two values, so it's fine either way. You can use barriers to ensure latest value is seen by interested readers, but for most eventual consistency would be enough.

Quoted 6 lines
Quoted 2 lines

Actually aren't we interested only in user_seed in the external reader case? We don't care what's in mp_seed, so this is much simpler.

Oh, I misunderstood you, didn't I? :) Were you talking about constructing the siphash key in the fast-path above? If yes, then sure it's a few instructions but nothing conditional.

That's what I meant. I tried to be concise and went overboard.

Quoted 1 line

I don't think we need anything atomic in that case.

Hmm, right, no competing increments of any sort, so WRITE_ONCE in the control path and READ_ONCE in fastpath should be enough.

Thanks for the feedback, I'll send v2 this week.

author maintainer
#13 lore raw

Re: [PATCH net-next 2/4] net: ipv4: Add a sysctl to set multipath hash seed

In reply to Eric Dumazet
Quoted 21 lines · Eric Dumazet
Quoted 18 lines · Petr Machata

When calculating hashes for the purpose of multipath forwarding, both IPv4 and IPv6 code currently fall back on flow_hash_from_keys(). That uses a randomly-generated seed. That's a fine choice by default, but unfortunately some deployments may need a tighter control over the seed used.

In this patch, make the seed configurable by adding a new sysctl key, net.ipv4.fib_multipath_hash_seed to control the seed. This seed is used specifically for multipath forwarding and not for the other concerns that flow_hash_from_keys() is used for, such as queue selection. Expose the knob as sysctl because other such settings, such as headers to hash, are also handled that way. Like those, the multipath hash seed is a per-netns variable.

Despite being placed in the net.ipv4 namespace, the multipath seed sysctl is used for both IPv4 and IPv6, similarly to e.g. a number of TCP variables.

...

Quoted 7 lines

net/ipv4/sysctl_net_ipv4.c static int proc_fib_multipath_hash_fields(struct ctl_table *table, int write, · line 495

Quoted 5 lines
net/ipv4/sysctl_net_ipv4.c +5 -0
+       rtnl_lock();
+       old = rcu_replace_pointer_rtnl(net->ipv4.sysctl_fib_multipath_hash_seed,
+                                      mphs);
+       rtnl_unlock();
+

In case you keep RCU for the next version, please do not use rtnl_lock() here.

Thanks. It looks like it's going to be inline and key constructed at the point of use, so no RCU.

author maintainer
#6 lore raw

Re: [PATCH net-next 2/4] net: ipv4: Add a sysctl to set multipath hash seed

In reply to Jakub Kicinski
Quoted 10 lines · Jakub Kicinski

Documentation/networking/ip-sysctl.rst fib_multipath_hash_fields - UNSIGNED INTEGER · line 135

Quoted 2 lines · On Wed, 29 May 2024 13:18:42 +0200 Petr Machata wrote:
Documentation/networking/ip-sysctl.rst +2 -0
+fib_multipath_hash_seed - UNSIGNED INTEGER
+	The seed value used when calculating hash for multipath routes. Applies

nits..

For RSS we call it key rather than seed, is calling it seed well established for ECMP?

Can we also call out that hashing implementation is not well defined?

As others note, this seems to be industry nomenclature, so I'll keep it.

Quoted 3 lines

Documentation/networking/ip-sysctl.rst fib_multipath_hash_fields - UNSIGNED INTEGER · line 136

Quoted 1 line
Documentation/networking/ip-sysctl.rst +1 -0
+	to both IPv4 and IPv6 datapath. Only valid for kernels built with

s/valid/present/ ?

Ack.

Quoted 4 lines
Quoted 4 lines
Documentation/networking/ip-sysctl.rst +4 -0
+	CONFIG_IP_ROUTE_MULTIPATH enabled.
+
+	When set to 0, the seed value used for multipath routing defaults to an
+	internal random-generated one.
author maintainer
#7 lore raw

Re: [PATCH net-next 2/4] net: ipv4: Add a sysctl to set multipath hash seed

Quoted 8 lines · Petr Machata
Quoted 6 lines · Jakub Kicinski

Documentation/networking/ip-sysctl.rst fib_multipath_hash_fields - UNSIGNED INTEGER · line 135

Quoted 2 lines · On Wed, 29 May 2024 13:18:42 +0200 Petr Machata wrote:
Documentation/networking/ip-sysctl.rst +2 -0
+fib_multipath_hash_seed - UNSIGNED INTEGER
+	The seed value used when calculating hash for multipath routes. Applies

nits..

Quoted 6 lines
Quoted 4 lines

For RSS we call it key rather than seed, is calling it seed well established for ECMP?

Can we also call out that hashing implementation is not well defined?

As others note, this seems to be industry nomenclature, so I'll keep it.

I meant the "seed" name, I'll mention the algorithm is undefined and doesn't constitute an ABI.

#12 lore raw

Re: [PATCH net-next 2/4] net: ipv4: Add a sysctl to set multipath hash seed

In reply to Toke Høiland-Jørgensen
Quoted 49 lines · Toke Høiland-Jørgensen
Quoted 47 lines · Eric Dumazet
Quoted 46 lines · Toke Høiland-Jørgensen
Quoted 27 lines · Eric Dumazet
Quoted 18 lines · Petr Machata

When calculating hashes for the purpose of multipath forwarding, both IPv4 and IPv6 code currently fall back on flow_hash_from_keys(). That uses a randomly-generated seed. That's a fine choice by default, but unfortunately some deployments may need a tighter control over the seed used.

In this patch, make the seed configurable by adding a new sysctl key, net.ipv4.fib_multipath_hash_seed to control the seed. This seed is used specifically for multipath forwarding and not for the other concerns that flow_hash_from_keys() is used for, such as queue selection. Expose the knob as sysctl because other such settings, such as headers to hash, are also handled that way. Like those, the multipath hash seed is a per-netns variable.

Despite being placed in the net.ipv4 namespace, the multipath seed sysctl is used for both IPv4 and IPv6, similarly to e.g. a number of TCP variables.

...

net/ipv4/sysctl_net_ipv4.c static int proc_fib_multipath_hash_fields(struct ctl_table *table, int write, · line 495

Quoted 5 lines
net/ipv4/sysctl_net_ipv4.c +5 -0
+       rtnl_lock();
+       old = rcu_replace_pointer_rtnl(net->ipv4.sysctl_fib_multipath_hash_seed,
+                                      mphs);
+       rtnl_unlock();
+
Quoted 7 lines

In case you keep RCU for the next version, please do not use rtnl_lock() here.

A simple xchg() will work just fine.

old = xchg((__force struct struct sysctl_fib_multipath_hash_seed **)&net->ipv4.sysctl_fib_multipath_hash_seed,

                 mphs);

We added a macro to do this kind of thing without triggering any of the RCU type linter warnings, in:

76c8eaafe4f0 ("rcu: Create an unrcu_pointer() to remove __rcu from a pointer")

So as an alternative to open-coding the cast, something like this could work - I guess it's mostly a matter of taste:

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

old = unrcu_pointer(xchg(&net->ipv4.sysctl_fib_multipath_hash_seed, RCU_INITIALIZER(mphs)));

Good to know, thanks.

Not sure why __kernel qualifier has been put there.

Not sure either. Paul, care to enlighten us? :)

Because __kernel says "just plain kernel access". Here are the options:

# define __kernel       __attribute__((address_space(0)))
# define __user         __attribute__((noderef, address_space(__user)))
# define __iomem        __attribute__((noderef, address_space(__iomem)))
# define __percpu       __attribute__((noderef, address_space(__percpu)))
# define __rcu          __attribute__((noderef, address_space(__rcu)))

So casting to __kernel removes the __rcu, thus avoiding the sparse complaint.

Thanx, Paul