29
29
#include <linux/init.h>
30
30
#include <linux/slab.h>
31
31
#include <linux/netlink.h>
32
+ #include <linux/hash.h>
32
33
33
34
#include <net/arp.h>
34
35
#include <net/ip.h>
@@ -51,6 +52,7 @@ static DEFINE_SPINLOCK(fib_info_lock);
51
52
static struct hlist_head * fib_info_hash ;
52
53
static struct hlist_head * fib_info_laddrhash ;
53
54
static unsigned int fib_info_hash_size ;
55
+ static unsigned int fib_info_hash_bits ;
54
56
static unsigned int fib_info_cnt ;
55
57
56
58
#define DEVINDEX_HASHBITS 8
@@ -319,11 +321,15 @@ static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
319
321
320
322
static inline unsigned int fib_devindex_hashfn (unsigned int val )
321
323
{
322
- unsigned int mask = DEVINDEX_HASHSIZE - 1 ;
324
+ return hash_32 (val , DEVINDEX_HASHBITS );
325
+ }
326
+
327
+ static struct hlist_head *
328
+ fib_info_devhash_bucket (const struct net_device * dev )
329
+ {
330
+ u32 val = net_hash_mix (dev_net (dev )) ^ dev -> ifindex ;
323
331
324
- return (val ^
325
- (val >> DEVINDEX_HASHBITS ) ^
326
- (val >> (DEVINDEX_HASHBITS * 2 ))) & mask ;
332
+ return & fib_info_devhash [fib_devindex_hashfn (val )];
327
333
}
328
334
329
335
static unsigned int fib_info_hashfn_1 (int init_val , u8 protocol , u8 scope ,
@@ -433,12 +439,11 @@ int ip_fib_check_default(__be32 gw, struct net_device *dev)
433
439
{
434
440
struct hlist_head * head ;
435
441
struct fib_nh * nh ;
436
- unsigned int hash ;
437
442
438
443
spin_lock (& fib_info_lock );
439
444
440
- hash = fib_devindex_hashfn (dev -> ifindex );
441
- head = & fib_info_devhash [ hash ];
445
+ head = fib_info_devhash_bucket (dev );
446
+
442
447
hlist_for_each_entry (nh , head , nh_hash ) {
443
448
if (nh -> fib_nh_dev == dev &&
444
449
nh -> fib_nh_gw4 == gw &&
@@ -1243,13 +1248,13 @@ int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1243
1248
return err ;
1244
1249
}
1245
1250
1246
- static inline unsigned int fib_laddr_hashfn (__be32 val )
1251
+ static struct hlist_head *
1252
+ fib_info_laddrhash_bucket (const struct net * net , __be32 val )
1247
1253
{
1248
- unsigned int mask = (fib_info_hash_size - 1 );
1254
+ u32 slot = hash_32 (net_hash_mix (net ) ^ (__force u32 )val ,
1255
+ fib_info_hash_bits );
1249
1256
1250
- return ((__force u32 )val ^
1251
- ((__force u32 )val >> 7 ) ^
1252
- ((__force u32 )val >> 14 )) & mask ;
1257
+ return & fib_info_laddrhash [slot ];
1253
1258
}
1254
1259
1255
1260
static struct hlist_head * fib_info_hash_alloc (int bytes )
@@ -1285,6 +1290,7 @@ static void fib_info_hash_move(struct hlist_head *new_info_hash,
1285
1290
old_info_hash = fib_info_hash ;
1286
1291
old_laddrhash = fib_info_laddrhash ;
1287
1292
fib_info_hash_size = new_size ;
1293
+ fib_info_hash_bits = ilog2 (new_size );
1288
1294
1289
1295
for (i = 0 ; i < old_size ; i ++ ) {
1290
1296
struct hlist_head * head = & fib_info_hash [i ];
@@ -1302,21 +1308,20 @@ static void fib_info_hash_move(struct hlist_head *new_info_hash,
1302
1308
}
1303
1309
fib_info_hash = new_info_hash ;
1304
1310
1311
+ fib_info_laddrhash = new_laddrhash ;
1305
1312
for (i = 0 ; i < old_size ; i ++ ) {
1306
- struct hlist_head * lhead = & fib_info_laddrhash [i ];
1313
+ struct hlist_head * lhead = & old_laddrhash [i ];
1307
1314
struct hlist_node * n ;
1308
1315
struct fib_info * fi ;
1309
1316
1310
1317
hlist_for_each_entry_safe (fi , n , lhead , fib_lhash ) {
1311
1318
struct hlist_head * ldest ;
1312
- unsigned int new_hash ;
1313
1319
1314
- new_hash = fib_laddr_hashfn (fi -> fib_prefsrc );
1315
- ldest = & new_laddrhash [ new_hash ] ;
1320
+ ldest = fib_info_laddrhash_bucket (fi -> fib_net ,
1321
+ fi -> fib_prefsrc ) ;
1316
1322
hlist_add_head (& fi -> fib_lhash , ldest );
1317
1323
}
1318
1324
}
1319
- fib_info_laddrhash = new_laddrhash ;
1320
1325
1321
1326
spin_unlock_bh (& fib_info_lock );
1322
1327
@@ -1601,20 +1606,18 @@ struct fib_info *fib_create_info(struct fib_config *cfg,
1601
1606
if (fi -> fib_prefsrc ) {
1602
1607
struct hlist_head * head ;
1603
1608
1604
- head = & fib_info_laddrhash [ fib_laddr_hashfn ( fi -> fib_prefsrc )] ;
1609
+ head = fib_info_laddrhash_bucket ( net , fi -> fib_prefsrc );
1605
1610
hlist_add_head (& fi -> fib_lhash , head );
1606
1611
}
1607
1612
if (fi -> nh ) {
1608
1613
list_add (& fi -> nh_list , & nh -> fi_list );
1609
1614
} else {
1610
1615
change_nexthops (fi ) {
1611
1616
struct hlist_head * head ;
1612
- unsigned int hash ;
1613
1617
1614
1618
if (!nexthop_nh -> fib_nh_dev )
1615
1619
continue ;
1616
- hash = fib_devindex_hashfn (nexthop_nh -> fib_nh_dev -> ifindex );
1617
- head = & fib_info_devhash [hash ];
1620
+ head = fib_info_devhash_bucket (nexthop_nh -> fib_nh_dev );
1618
1621
hlist_add_head (& nexthop_nh -> nh_hash , head );
1619
1622
} endfor_nexthops (fi )
1620
1623
}
@@ -1875,16 +1878,16 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1875
1878
*/
1876
1879
int fib_sync_down_addr (struct net_device * dev , __be32 local )
1877
1880
{
1878
- int ret = 0 ;
1879
- unsigned int hash = fib_laddr_hashfn (local );
1880
- struct hlist_head * head = & fib_info_laddrhash [hash ];
1881
1881
int tb_id = l3mdev_fib_table (dev ) ? : RT_TABLE_MAIN ;
1882
1882
struct net * net = dev_net (dev );
1883
+ struct hlist_head * head ;
1883
1884
struct fib_info * fi ;
1885
+ int ret = 0 ;
1884
1886
1885
1887
if (!fib_info_laddrhash || local == 0 )
1886
1888
return 0 ;
1887
1889
1890
+ head = fib_info_laddrhash_bucket (net , local );
1888
1891
hlist_for_each_entry (fi , head , fib_lhash ) {
1889
1892
if (!net_eq (fi -> fib_net , net ) ||
1890
1893
fi -> fib_tb_id != tb_id )
@@ -1966,8 +1969,7 @@ void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
1966
1969
1967
1970
void fib_sync_mtu (struct net_device * dev , u32 orig_mtu )
1968
1971
{
1969
- unsigned int hash = fib_devindex_hashfn (dev -> ifindex );
1970
- struct hlist_head * head = & fib_info_devhash [hash ];
1972
+ struct hlist_head * head = fib_info_devhash_bucket (dev );
1971
1973
struct fib_nh * nh ;
1972
1974
1973
1975
hlist_for_each_entry (nh , head , nh_hash ) {
@@ -1986,12 +1988,11 @@ void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1986
1988
*/
1987
1989
int fib_sync_down_dev (struct net_device * dev , unsigned long event , bool force )
1988
1990
{
1989
- int ret = 0 ;
1990
- int scope = RT_SCOPE_NOWHERE ;
1991
+ struct hlist_head * head = fib_info_devhash_bucket (dev );
1991
1992
struct fib_info * prev_fi = NULL ;
1992
- unsigned int hash = fib_devindex_hashfn (dev -> ifindex );
1993
- struct hlist_head * head = & fib_info_devhash [hash ];
1993
+ int scope = RT_SCOPE_NOWHERE ;
1994
1994
struct fib_nh * nh ;
1995
+ int ret = 0 ;
1995
1996
1996
1997
if (force )
1997
1998
scope = -1 ;
@@ -2136,7 +2137,6 @@ static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
2136
2137
int fib_sync_up (struct net_device * dev , unsigned char nh_flags )
2137
2138
{
2138
2139
struct fib_info * prev_fi ;
2139
- unsigned int hash ;
2140
2140
struct hlist_head * head ;
2141
2141
struct fib_nh * nh ;
2142
2142
int ret ;
@@ -2152,8 +2152,7 @@ int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
2152
2152
}
2153
2153
2154
2154
prev_fi = NULL ;
2155
- hash = fib_devindex_hashfn (dev -> ifindex );
2156
- head = & fib_info_devhash [hash ];
2155
+ head = fib_info_devhash_bucket (dev );
2157
2156
ret = 0 ;
2158
2157
2159
2158
hlist_for_each_entry (nh , head , nh_hash ) {
0 commit comments