Skip to content

Commit d07418a

Browse files
edumazetkuba-moo
authored andcommitted
ipv4: avoid quadratic behavior in netns dismantle
net/ipv4/fib_semantics.c uses an hash table of 256 slots, keyed by device ifindexes: fib_info_devhash[DEVINDEX_HASHSIZE] Problem is that with network namespaces, devices tend to use the same ifindex. lo device for instance has a fixed ifindex of one, for all network namespaces. This means that hosts with thousands of netns spend a lot of time looking at some hash buckets with thousands of elements, notably at netns dismantle. Simply add a per netns perturbation (net_hash_mix()) to spread elements more uniformely. Also change fib_devindex_hashfn() to use more entropy. Fixes: aa79e66 ("net: Make ifindex generation per-net namespace") Signed-off-by: Eric Dumazet <[email protected]> Reviewed-by: David Ahern <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 8eb896a commit d07418a

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

net/ipv4/fib_semantics.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/init.h>
3030
#include <linux/slab.h>
3131
#include <linux/netlink.h>
32+
#include <linux/hash.h>
3233

3334
#include <net/arp.h>
3435
#include <net/ip.h>
@@ -319,11 +320,15 @@ static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
319320

320321
static inline unsigned int fib_devindex_hashfn(unsigned int val)
321322
{
322-
unsigned int mask = DEVINDEX_HASHSIZE - 1;
323+
return hash_32(val, DEVINDEX_HASHBITS);
324+
}
325+
326+
static struct hlist_head *
327+
fib_info_devhash_bucket(const struct net_device *dev)
328+
{
329+
u32 val = net_hash_mix(dev_net(dev)) ^ dev->ifindex;
323330

324-
return (val ^
325-
(val >> DEVINDEX_HASHBITS) ^
326-
(val >> (DEVINDEX_HASHBITS * 2))) & mask;
331+
return &fib_info_devhash[fib_devindex_hashfn(val)];
327332
}
328333

329334
static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
@@ -433,12 +438,11 @@ int ip_fib_check_default(__be32 gw, struct net_device *dev)
433438
{
434439
struct hlist_head *head;
435440
struct fib_nh *nh;
436-
unsigned int hash;
437441

438442
spin_lock(&fib_info_lock);
439443

440-
hash = fib_devindex_hashfn(dev->ifindex);
441-
head = &fib_info_devhash[hash];
444+
head = fib_info_devhash_bucket(dev);
445+
442446
hlist_for_each_entry(nh, head, nh_hash) {
443447
if (nh->fib_nh_dev == dev &&
444448
nh->fib_nh_gw4 == gw &&
@@ -1609,12 +1613,10 @@ struct fib_info *fib_create_info(struct fib_config *cfg,
16091613
} else {
16101614
change_nexthops(fi) {
16111615
struct hlist_head *head;
1612-
unsigned int hash;
16131616

16141617
if (!nexthop_nh->fib_nh_dev)
16151618
continue;
1616-
hash = fib_devindex_hashfn(nexthop_nh->fib_nh_dev->ifindex);
1617-
head = &fib_info_devhash[hash];
1619+
head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev);
16181620
hlist_add_head(&nexthop_nh->nh_hash, head);
16191621
} endfor_nexthops(fi)
16201622
}
@@ -1966,8 +1968,7 @@ void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
19661968

19671969
void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
19681970
{
1969-
unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1970-
struct hlist_head *head = &fib_info_devhash[hash];
1971+
struct hlist_head *head = fib_info_devhash_bucket(dev);
19711972
struct fib_nh *nh;
19721973

19731974
hlist_for_each_entry(nh, head, nh_hash) {
@@ -1986,12 +1987,11 @@ void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
19861987
*/
19871988
int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
19881989
{
1989-
int ret = 0;
1990-
int scope = RT_SCOPE_NOWHERE;
1990+
struct hlist_head *head = fib_info_devhash_bucket(dev);
19911991
struct fib_info *prev_fi = NULL;
1992-
unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1993-
struct hlist_head *head = &fib_info_devhash[hash];
1992+
int scope = RT_SCOPE_NOWHERE;
19941993
struct fib_nh *nh;
1994+
int ret = 0;
19951995

19961996
if (force)
19971997
scope = -1;
@@ -2136,7 +2136,6 @@ static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
21362136
int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
21372137
{
21382138
struct fib_info *prev_fi;
2139-
unsigned int hash;
21402139
struct hlist_head *head;
21412140
struct fib_nh *nh;
21422141
int ret;
@@ -2152,8 +2151,7 @@ int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
21522151
}
21532152

21542153
prev_fi = NULL;
2155-
hash = fib_devindex_hashfn(dev->ifindex);
2156-
head = &fib_info_devhash[hash];
2154+
head = fib_info_devhash_bucket(dev);
21572155
ret = 0;
21582156

21592157
hlist_for_each_entry(nh, head, nh_hash) {

0 commit comments

Comments
 (0)