Skip to content

Commit b58555f

Browse files
gouault6windklassert
authored andcommitted
xfrm: hash prefixed policies based on preflen thresholds
The idea is an extension of the current policy hashing. Today only non-prefixed policies are stored in a hash table. This patch relaxes the constraints, and hashes policies whose prefix lengths are greater or equal to a configurable threshold. Each hash table (one per direction) maintains its own set of IPv4 and IPv6 thresholds (dbits4, sbits4, dbits6, sbits6), by default (32, 32, 128, 128). Example, if the output hash table is configured with values (16, 24, 56, 64): ip xfrm policy add dir out src 10.22.0.0/20 dst 10.24.1.0/24 ... => hashed ip xfrm policy add dir out src 10.22.0.0/16 dst 10.24.1.1/32 ... => hashed ip xfrm policy add dir out src 10.22.0.0/16 dst 10.24.0.0/16 ... => unhashed ip xfrm policy add dir out \ src 3ffe:304:124:2200::/60 dst 3ffe:304:124:2401::/64 ... => hashed ip xfrm policy add dir out \ src 3ffe:304:124:2200::/56 dst 3ffe:304:124:2401::2/128 ... => hashed ip xfrm policy add dir out \ src 3ffe:304:124:2200::/56 dst 3ffe:304:124:2400::/56 ... => unhashed The high order bits of the addresses (up to the threshold) are used to compute the hash key. Signed-off-by: Christophe Gouault <christophe.gouault@6wind.com> Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
1 parent 0244790 commit b58555f

3 files changed

Lines changed: 117 additions & 16 deletions

File tree

‎include/net/netns/xfrm.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ struct ctl_table_header;
1313
struct xfrm_policy_hash {
1414
struct hlist_head *table;
1515
unsigned int hmask;
16+
u8 dbits4;
17+
u8 sbits4;
18+
u8 dbits6;
19+
u8 sbits6;
1620
};
1721

1822
struct netns_xfrm {

‎net/xfrm/xfrm_hash.h‎

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <linux/xfrm.h>
55
#include <linux/socket.h>
6+
#include <linux/jhash.h>
67

78
static inline unsigned int __xfrm4_addr_hash(const xfrm_address_t *addr)
89
{
@@ -28,6 +29,58 @@ static inline unsigned int __xfrm6_daddr_saddr_hash(const xfrm_address_t *daddr,
2829
saddr->a6[2] ^ saddr->a6[3]);
2930
}
3031

32+
static inline u32 __bits2mask32(__u8 bits)
33+
{
34+
u32 mask32 = 0xffffffff;
35+
36+
if (bits == 0)
37+
mask32 = 0;
38+
else if (bits < 32)
39+
mask32 <<= (32 - bits);
40+
41+
return mask32;
42+
}
43+
44+
static inline unsigned int __xfrm4_dpref_spref_hash(const xfrm_address_t *daddr,
45+
const xfrm_address_t *saddr,
46+
__u8 dbits,
47+
__u8 sbits)
48+
{
49+
return jhash_2words(ntohl(daddr->a4) & __bits2mask32(dbits),
50+
ntohl(saddr->a4) & __bits2mask32(sbits),
51+
0);
52+
}
53+
54+
static inline unsigned int __xfrm6_pref_hash(const xfrm_address_t *addr,
55+
__u8 prefixlen)
56+
{
57+
int pdw;
58+
int pbi;
59+
u32 initval = 0;
60+
61+
pdw = prefixlen >> 5; /* num of whole u32 in prefix */
62+
pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */
63+
64+
if (pbi) {
65+
__be32 mask;
66+
67+
mask = htonl((0xffffffff) << (32 - pbi));
68+
69+
initval = (__force u32)(addr->a6[pdw] & mask);
70+
}
71+
72+
return jhash2((__force u32 *)addr->a6, pdw, initval);
73+
}
74+
75+
static inline unsigned int __xfrm6_dpref_spref_hash(const xfrm_address_t *daddr,
76+
const xfrm_address_t *saddr,
77+
__u8 dbits,
78+
__u8 sbits)
79+
{
80+
return __xfrm6_pref_hash(daddr, dbits) ^
81+
__xfrm6_pref_hash(saddr, sbits);
82+
}
83+
3184
static inline unsigned int __xfrm_dst_hash(const xfrm_address_t *daddr,
3285
const xfrm_address_t *saddr,
3386
u32 reqid, unsigned short family,
@@ -84,27 +137,28 @@ static inline unsigned int __idx_hash(u32 index, unsigned int hmask)
84137
}
85138

86139
static inline unsigned int __sel_hash(const struct xfrm_selector *sel,
87-
unsigned short family, unsigned int hmask)
140+
unsigned short family, unsigned int hmask,
141+
u8 dbits, u8 sbits)
88142
{
89143
const xfrm_address_t *daddr = &sel->daddr;
90144
const xfrm_address_t *saddr = &sel->saddr;
91145
unsigned int h = 0;
92146

93147
switch (family) {
94148
case AF_INET:
95-
if (sel->prefixlen_d != 32 ||
96-
sel->prefixlen_s != 32)
149+
if (sel->prefixlen_d < dbits ||
150+
sel->prefixlen_s < sbits)
97151
return hmask + 1;
98152

99-
h = __xfrm4_daddr_saddr_hash(daddr, saddr);
153+
h = __xfrm4_dpref_spref_hash(daddr, saddr, dbits, sbits);
100154
break;
101155

102156
case AF_INET6:
103-
if (sel->prefixlen_d != 128 ||
104-
sel->prefixlen_s != 128)
157+
if (sel->prefixlen_d < dbits ||
158+
sel->prefixlen_s < sbits)
105159
return hmask + 1;
106160

107-
h = __xfrm6_daddr_saddr_hash(daddr, saddr);
161+
h = __xfrm6_dpref_spref_hash(daddr, saddr, dbits, sbits);
108162
break;
109163
}
110164
h ^= (h >> 16);
@@ -113,17 +167,19 @@ static inline unsigned int __sel_hash(const struct xfrm_selector *sel,
113167

114168
static inline unsigned int __addr_hash(const xfrm_address_t *daddr,
115169
const xfrm_address_t *saddr,
116-
unsigned short family, unsigned int hmask)
170+
unsigned short family,
171+
unsigned int hmask,
172+
u8 dbits, u8 sbits)
117173
{
118174
unsigned int h = 0;
119175

120176
switch (family) {
121177
case AF_INET:
122-
h = __xfrm4_daddr_saddr_hash(daddr, saddr);
178+
h = __xfrm4_dpref_spref_hash(daddr, saddr, dbits, sbits);
123179
break;
124180

125181
case AF_INET6:
126-
h = __xfrm6_daddr_saddr_hash(daddr, saddr);
182+
h = __xfrm6_dpref_spref_hash(daddr, saddr, dbits, sbits);
127183
break;
128184
}
129185
h ^= (h >> 16);

‎net/xfrm/xfrm_policy.c‎

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,39 @@ static inline unsigned int idx_hash(struct net *net, u32 index)
344344
return __idx_hash(index, net->xfrm.policy_idx_hmask);
345345
}
346346

347+
/* calculate policy hash thresholds */
348+
static void __get_hash_thresh(struct net *net,
349+
unsigned short family, int dir,
350+
u8 *dbits, u8 *sbits)
351+
{
352+
switch (family) {
353+
case AF_INET:
354+
*dbits = net->xfrm.policy_bydst[dir].dbits4;
355+
*sbits = net->xfrm.policy_bydst[dir].sbits4;
356+
break;
357+
358+
case AF_INET6:
359+
*dbits = net->xfrm.policy_bydst[dir].dbits6;
360+
*sbits = net->xfrm.policy_bydst[dir].sbits6;
361+
break;
362+
363+
default:
364+
*dbits = 0;
365+
*sbits = 0;
366+
}
367+
}
368+
347369
static struct hlist_head *policy_hash_bysel(struct net *net,
348370
const struct xfrm_selector *sel,
349371
unsigned short family, int dir)
350372
{
351373
unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
352-
unsigned int hash = __sel_hash(sel, family, hmask);
374+
unsigned int hash;
375+
u8 dbits;
376+
u8 sbits;
377+
378+
__get_hash_thresh(net, family, dir, &dbits, &sbits);
379+
hash = __sel_hash(sel, family, hmask, dbits, sbits);
353380

354381
return (hash == hmask + 1 ?
355382
&net->xfrm.policy_inexact[dir] :
@@ -362,25 +389,35 @@ static struct hlist_head *policy_hash_direct(struct net *net,
362389
unsigned short family, int dir)
363390
{
364391
unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
365-
unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
392+
unsigned int hash;
393+
u8 dbits;
394+
u8 sbits;
395+
396+
__get_hash_thresh(net, family, dir, &dbits, &sbits);
397+
hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
366398

367399
return net->xfrm.policy_bydst[dir].table + hash;
368400
}
369401

370-
static void xfrm_dst_hash_transfer(struct hlist_head *list,
402+
static void xfrm_dst_hash_transfer(struct net *net,
403+
struct hlist_head *list,
371404
struct hlist_head *ndsttable,
372-
unsigned int nhashmask)
405+
unsigned int nhashmask,
406+
int dir)
373407
{
374408
struct hlist_node *tmp, *entry0 = NULL;
375409
struct xfrm_policy *pol;
376410
unsigned int h0 = 0;
411+
u8 dbits;
412+
u8 sbits;
377413

378414
redo:
379415
hlist_for_each_entry_safe(pol, tmp, list, bydst) {
380416
unsigned int h;
381417

418+
__get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
382419
h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
383-
pol->family, nhashmask);
420+
pol->family, nhashmask, dbits, sbits);
384421
if (!entry0) {
385422
hlist_del(&pol->bydst);
386423
hlist_add_head(&pol->bydst, ndsttable+h);
@@ -434,7 +471,7 @@ static void xfrm_bydst_resize(struct net *net, int dir)
434471
write_lock_bh(&net->xfrm.xfrm_policy_lock);
435472

436473
for (i = hmask; i >= 0; i--)
437-
xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
474+
xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
438475

439476
net->xfrm.policy_bydst[dir].table = ndst;
440477
net->xfrm.policy_bydst[dir].hmask = nhashmask;
@@ -2830,6 +2867,10 @@ static int __net_init xfrm_policy_init(struct net *net)
28302867
if (!htab->table)
28312868
goto out_bydst;
28322869
htab->hmask = hmask;
2870+
htab->dbits4 = 32;
2871+
htab->sbits4 = 32;
2872+
htab->dbits6 = 128;
2873+
htab->sbits6 = 128;
28332874
}
28342875

28352876
INIT_LIST_HEAD(&net->xfrm.policy_all);

0 commit comments

Comments
 (0)