Skip to content

Commit 4ee2a8c

Browse files
pmachatakuba-moo
authored andcommitted
net: ipv4: Add a sysctl to set multipath hash seed
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. Signed-off-by: Petr Machata <petrm@nvidia.com> Reviewed-by: Ido Schimmel <idosch@nvidia.com> Reviewed-by: Nikolay Aleksandrov <razor@blackwall.org> Reviewed-by: David Ahern <dsahern@kernel.org> Link: https://lore.kernel.org/r/20240607151357.421181-3-petrm@nvidia.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 3e453ca commit 4ee2a8c

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed

Documentation/networking/ip-sysctl.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ fib_multipath_hash_fields - UNSIGNED INTEGER
131131

132132
Default: 0x0007 (source IP, destination IP and IP protocol)
133133

134+
fib_multipath_hash_seed - UNSIGNED INTEGER
135+
The seed value used when calculating hash for multipath routes. Applies
136+
to both IPv4 and IPv6 datapath. Only present for kernels built with
137+
CONFIG_IP_ROUTE_MULTIPATH enabled.
138+
139+
When set to 0, the seed value used for multipath routing defaults to an
140+
internal random-generated one.
141+
142+
The actual hashing algorithm is not specified -- there is no guarantee
143+
that a next hop distribution effected by a given seed will keep stable
144+
across kernel versions.
145+
146+
Default: 0 (random)
147+
134148
fib_sync_mem - UNSIGNED INTEGER
135149
Amount of dirty memory from fib entries that can be backlogged before
136150
synchronize_rcu is forced.

include/net/flow_dissector.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ static inline bool flow_keys_have_l4(const struct flow_keys *keys)
442442
}
443443

444444
u32 flow_hash_from_keys(struct flow_keys *keys);
445+
u32 flow_hash_from_keys_seed(struct flow_keys *keys,
446+
const siphash_key_t *keyval);
445447
void skb_flow_get_icmp_tci(const struct sk_buff *skb,
446448
struct flow_dissector_key_icmp *key_icmp,
447449
const void *data, int thoff, int hlen);

include/net/ip_fib.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,34 @@ void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig);
520520
#ifdef CONFIG_IP_ROUTE_MULTIPATH
521521
int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
522522
const struct sk_buff *skb, struct flow_keys *flkeys);
523-
#endif
523+
524+
static void
525+
fib_multipath_hash_construct_key(siphash_key_t *key, u32 mp_seed)
526+
{
527+
u64 mp_seed_64 = mp_seed;
528+
529+
key->key[0] = (mp_seed_64 << 32) | mp_seed_64;
530+
key->key[1] = key->key[0];
531+
}
524532

533+
static inline u32 fib_multipath_hash_from_keys(const struct net *net,
534+
struct flow_keys *keys)
535+
{
536+
siphash_aligned_key_t hash_key;
537+
u32 mp_seed;
538+
539+
mp_seed = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_seed).mp_seed;
540+
fib_multipath_hash_construct_key(&hash_key, mp_seed);
541+
542+
return flow_hash_from_keys_seed(keys, &hash_key);
543+
}
544+
#else
525545
static inline u32 fib_multipath_hash_from_keys(const struct net *net,
526546
struct flow_keys *keys)
527547
{
528548
return flow_hash_from_keys(keys);
529549
}
550+
#endif
530551

531552
int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
532553
struct netlink_ext_ack *extack);

include/net/netns/ipv4.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ struct inet_timewait_death_row {
4040

4141
struct tcp_fastopen_context;
4242

43+
#ifdef CONFIG_IP_ROUTE_MULTIPATH
44+
struct sysctl_fib_multipath_hash_seed {
45+
u32 user_seed;
46+
u32 mp_seed;
47+
};
48+
#endif
49+
4350
struct netns_ipv4 {
4451
/* Cacheline organization can be found documented in
4552
* Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst.
@@ -246,6 +253,7 @@ struct netns_ipv4 {
246253
#endif
247254
#endif
248255
#ifdef CONFIG_IP_ROUTE_MULTIPATH
256+
struct sysctl_fib_multipath_hash_seed sysctl_fib_multipath_hash_seed;
249257
u32 sysctl_fib_multipath_hash_fields;
250258
u8 sysctl_fib_multipath_use_neigh;
251259
u8 sysctl_fib_multipath_hash_policy;

net/core/flow_dissector.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,13 @@ u32 flow_hash_from_keys(struct flow_keys *keys)
18061806
}
18071807
EXPORT_SYMBOL(flow_hash_from_keys);
18081808

1809+
u32 flow_hash_from_keys_seed(struct flow_keys *keys,
1810+
const siphash_key_t *keyval)
1811+
{
1812+
return __flow_hash_from_keys(keys, keyval);
1813+
}
1814+
EXPORT_SYMBOL(flow_hash_from_keys_seed);
1815+
18091816
static inline u32 ___skb_get_hash(const struct sk_buff *skb,
18101817
struct flow_keys *keys,
18111818
const siphash_key_t *keyval)

net/ipv4/sysctl_net_ipv4.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,61 @@ static int proc_fib_multipath_hash_fields(struct ctl_table *table, int write,
464464

465465
return ret;
466466
}
467+
468+
static u32 proc_fib_multipath_hash_rand_seed __ro_after_init;
469+
470+
static void proc_fib_multipath_hash_init_rand_seed(void)
471+
{
472+
get_random_bytes(&proc_fib_multipath_hash_rand_seed,
473+
sizeof(proc_fib_multipath_hash_rand_seed));
474+
}
475+
476+
static void proc_fib_multipath_hash_set_seed(struct net *net, u32 user_seed)
477+
{
478+
struct sysctl_fib_multipath_hash_seed new = {
479+
.user_seed = user_seed,
480+
.mp_seed = (user_seed ? user_seed :
481+
proc_fib_multipath_hash_rand_seed),
482+
};
483+
484+
WRITE_ONCE(net->ipv4.sysctl_fib_multipath_hash_seed, new);
485+
}
486+
487+
static int proc_fib_multipath_hash_seed(struct ctl_table *table, int write,
488+
void *buffer, size_t *lenp,
489+
loff_t *ppos)
490+
{
491+
struct sysctl_fib_multipath_hash_seed *mphs;
492+
struct net *net = table->data;
493+
struct ctl_table tmp;
494+
u32 user_seed;
495+
int ret;
496+
497+
mphs = &net->ipv4.sysctl_fib_multipath_hash_seed;
498+
user_seed = mphs->user_seed;
499+
500+
tmp = *table;
501+
tmp.data = &user_seed;
502+
503+
ret = proc_douintvec_minmax(&tmp, write, buffer, lenp, ppos);
504+
505+
if (write && ret == 0) {
506+
proc_fib_multipath_hash_set_seed(net, user_seed);
507+
call_netevent_notifiers(NETEVENT_IPV4_MPATH_HASH_UPDATE, net);
508+
}
509+
510+
return ret;
511+
}
512+
#else
513+
514+
static void proc_fib_multipath_hash_init_rand_seed(void)
515+
{
516+
}
517+
518+
static void proc_fib_multipath_hash_set_seed(struct net *net, u32 user_seed)
519+
{
520+
}
521+
467522
#endif
468523

469524
static struct ctl_table ipv4_table[] = {
@@ -1072,6 +1127,13 @@ static struct ctl_table ipv4_net_table[] = {
10721127
.extra1 = SYSCTL_ONE,
10731128
.extra2 = &fib_multipath_hash_fields_all_mask,
10741129
},
1130+
{
1131+
.procname = "fib_multipath_hash_seed",
1132+
.data = &init_net,
1133+
.maxlen = sizeof(u32),
1134+
.mode = 0644,
1135+
.proc_handler = proc_fib_multipath_hash_seed,
1136+
},
10751137
#endif
10761138
{
10771139
.procname = "ip_unprivileged_port_start",
@@ -1550,6 +1612,8 @@ static __net_init int ipv4_sysctl_init_net(struct net *net)
15501612
if (!net->ipv4.sysctl_local_reserved_ports)
15511613
goto err_ports;
15521614

1615+
proc_fib_multipath_hash_set_seed(net, 0);
1616+
15531617
return 0;
15541618

15551619
err_ports:
@@ -1584,6 +1648,8 @@ static __init int sysctl_ipv4_init(void)
15841648
if (!hdr)
15851649
return -ENOMEM;
15861650

1651+
proc_fib_multipath_hash_init_rand_seed();
1652+
15871653
if (register_pernet_subsys(&ipv4_sysctl_ops)) {
15881654
unregister_net_sysctl_table(hdr);
15891655
return -ENOMEM;

0 commit comments

Comments
 (0)