Skip to content

Commit ccf0a4b

Browse files
Jozsef Kadlecsikummakynes
authored andcommitted
netfilter: ipset: Add bucketsize parameter to all hash types
The parameter defines the upper limit in any hash bucket at adding new entries from userspace - if the limit would be exceeded, ipset doubles the hash size and rehashes. It means the set may consume more memory but gives faster evaluation at matching in the set. Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent a304ea7 commit ccf0a4b

16 files changed

Lines changed: 79 additions & 40 deletions

‎include/linux/netfilter/ipset/ip_set.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ struct ip_set_region {
198198
u32 elements; /* Number of elements vs timeout */
199199
};
200200

201+
/* The max revision number supported by any set type + 1 */
202+
#define IPSET_REVISION_MAX 9
203+
201204
/* The core set type structure */
202205
struct ip_set_type {
203206
struct list_head list;
@@ -215,6 +218,8 @@ struct ip_set_type {
215218
u8 family;
216219
/* Type revisions */
217220
u8 revision_min, revision_max;
221+
/* Revision-specific supported (create) flags */
222+
u8 create_flags[IPSET_REVISION_MAX+1];
218223
/* Set features to control swapping */
219224
u16 features;
220225

‎include/uapi/linux/netfilter/ipset/ip_set.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ enum {
9696
IPSET_ATTR_HASHSIZE,
9797
IPSET_ATTR_MAXELEM,
9898
IPSET_ATTR_NETMASK,
99-
IPSET_ATTR_PROBES,
99+
IPSET_ATTR_BUCKETSIZE, /* was unused IPSET_ATTR_PROBES */
100100
IPSET_ATTR_RESIZE,
101101
IPSET_ATTR_SIZE,
102102
/* Kernel-only */
@@ -214,6 +214,8 @@ enum ipset_cadt_flags {
214214
enum ipset_create_flags {
215215
IPSET_CREATE_FLAG_BIT_FORCEADD = 0,
216216
IPSET_CREATE_FLAG_FORCEADD = (1 << IPSET_CREATE_FLAG_BIT_FORCEADD),
217+
IPSET_CREATE_FLAG_BIT_BUCKETSIZE = 1,
218+
IPSET_CREATE_FLAG_BUCKETSIZE = (1 << IPSET_CREATE_FLAG_BIT_BUCKETSIZE),
217219
IPSET_CREATE_FLAG_BIT_MAX = 7,
218220
};
219221

‎net/netfilter/ipset/ip_set_core.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,8 @@ static int ip_set_create(struct net *net, struct sock *ctnl,
11091109
ret = -IPSET_ERR_PROTOCOL;
11101110
goto put_out;
11111111
}
1112+
/* Set create flags depending on the type revision */
1113+
set->flags |= set->type->create_flags[revision];
11121114

11131115
ret = set->type->create(net, set, tb, flags);
11141116
if (ret != 0)

‎net/netfilter/ipset/ip_set_hash_gen.h‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@
3737
*/
3838

3939
/* Number of elements to store in an initial array block */
40-
#define AHASH_INIT_SIZE 4
40+
#define AHASH_INIT_SIZE 2
4141
/* Max number of elements to store in an array block */
42-
#define AHASH_MAX_SIZE (3 * AHASH_INIT_SIZE)
42+
#define AHASH_MAX_SIZE (6 * AHASH_INIT_SIZE)
4343
/* Max muber of elements in the array block when tuned */
4444
#define AHASH_MAX_TUNED 64
4545

46+
#define AHASH_MAX(h) ((h)->bucketsize)
47+
4648
/* Max number of elements can be tuned */
4749
#ifdef IP_SET_HASH_WITH_MULTI
48-
#define AHASH_MAX(h) ((h)->ahash_max)
49-
5050
static u8
51-
tune_ahash_max(u8 curr, u32 multi)
51+
tune_bucketsize(u8 curr, u32 multi)
5252
{
5353
u32 n;
5454

@@ -61,12 +61,10 @@ tune_ahash_max(u8 curr, u32 multi)
6161
*/
6262
return n > curr && n <= AHASH_MAX_TUNED ? n : curr;
6363
}
64-
65-
#define TUNE_AHASH_MAX(h, multi) \
66-
((h)->ahash_max = tune_ahash_max((h)->ahash_max, multi))
64+
#define TUNE_BUCKETSIZE(h, multi) \
65+
((h)->bucketsize = tune_bucketsize((h)->bucketsize, multi))
6766
#else
68-
#define AHASH_MAX(h) AHASH_MAX_SIZE
69-
#define TUNE_AHASH_MAX(h, multi)
67+
#define TUNE_BUCKETSIZE(h, multi)
7068
#endif
7169

7270
/* A hash bucket */
@@ -321,9 +319,7 @@ struct htype {
321319
#ifdef IP_SET_HASH_WITH_MARKMASK
322320
u32 markmask; /* markmask value for mark mask to store */
323321
#endif
324-
#ifdef IP_SET_HASH_WITH_MULTI
325-
u8 ahash_max; /* max elements in an array block */
326-
#endif
322+
u8 bucketsize; /* max elements in an array block */
327323
#ifdef IP_SET_HASH_WITH_NETMASK
328324
u8 netmask; /* netmask value for subnets to store */
329325
#endif
@@ -950,7 +946,7 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
950946
goto set_full;
951947
/* Create a new slot */
952948
if (n->pos >= n->size) {
953-
TUNE_AHASH_MAX(h, multi);
949+
TUNE_BUCKETSIZE(h, multi);
954950
if (n->size >= AHASH_MAX(h)) {
955951
/* Trigger rehashing */
956952
mtype_data_next(&h->next, d);
@@ -1305,6 +1301,9 @@ mtype_head(struct ip_set *set, struct sk_buff *skb)
13051301
if (nla_put_u32(skb, IPSET_ATTR_MARKMASK, h->markmask))
13061302
goto nla_put_failure;
13071303
#endif
1304+
if (set->flags & IPSET_CREATE_FLAG_BUCKETSIZE &&
1305+
nla_put_u8(skb, IPSET_ATTR_BUCKETSIZE, h->bucketsize))
1306+
goto nla_put_failure;
13081307
if (nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref)) ||
13091308
nla_put_net32(skb, IPSET_ATTR_MEMSIZE, htonl(memsize)) ||
13101309
nla_put_net32(skb, IPSET_ATTR_ELEMENTS, htonl(elements)))
@@ -1548,7 +1547,16 @@ IPSET_TOKEN(HTYPE, _create)(struct net *net, struct ip_set *set,
15481547
h->markmask = markmask;
15491548
#endif
15501549
get_random_bytes(&h->initval, sizeof(h->initval));
1551-
1550+
h->bucketsize = AHASH_MAX_SIZE;
1551+
if (tb[IPSET_ATTR_BUCKETSIZE]) {
1552+
h->bucketsize = nla_get_u8(tb[IPSET_ATTR_BUCKETSIZE]);
1553+
if (h->bucketsize < AHASH_INIT_SIZE)
1554+
h->bucketsize = AHASH_INIT_SIZE;
1555+
else if (h->bucketsize > AHASH_MAX_SIZE)
1556+
h->bucketsize = AHASH_MAX_SIZE;
1557+
else if (h->bucketsize % 2)
1558+
h->bucketsize += 1;
1559+
}
15521560
t->htable_bits = hbits;
15531561
t->maxelem = h->maxelem / ahash_numof_locks(hbits);
15541562
RCU_INIT_POINTER(h->table, t);

‎net/netfilter/ipset/ip_set_hash_ip.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
/* 1 Counters support */
2424
/* 2 Comments support */
2525
/* 3 Forceadd support */
26-
#define IPSET_TYPE_REV_MAX 4 /* skbinfo support */
26+
/* 4 skbinfo support */
27+
#define IPSET_TYPE_REV_MAX 5 /* bucketsize support */
2728

2829
MODULE_LICENSE("GPL");
2930
MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
@@ -277,11 +278,12 @@ static struct ip_set_type hash_ip_type __read_mostly = {
277278
.family = NFPROTO_UNSPEC,
278279
.revision_min = IPSET_TYPE_REV_MIN,
279280
.revision_max = IPSET_TYPE_REV_MAX,
281+
.create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
280282
.create = hash_ip_create,
281283
.create_policy = {
282284
[IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
283285
[IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
284-
[IPSET_ATTR_PROBES] = { .type = NLA_U8 },
286+
[IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
285287
[IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
286288
[IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
287289
[IPSET_ATTR_NETMASK] = { .type = NLA_U8 },

‎net/netfilter/ipset/ip_set_hash_ipmac.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <linux/netfilter/ipset/ip_set_hash.h>
2424

2525
#define IPSET_TYPE_REV_MIN 0
26-
#define IPSET_TYPE_REV_MAX 0
26+
#define IPSET_TYPE_REV_MAX 1 /* bucketsize support */
2727

2828
MODULE_LICENSE("GPL");
2929
MODULE_AUTHOR("Tomasz Chilinski <tomasz.chilinski@chilan.com>");
@@ -268,11 +268,12 @@ static struct ip_set_type hash_ipmac_type __read_mostly = {
268268
.family = NFPROTO_UNSPEC,
269269
.revision_min = IPSET_TYPE_REV_MIN,
270270
.revision_max = IPSET_TYPE_REV_MAX,
271+
.create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
271272
.create = hash_ipmac_create,
272273
.create_policy = {
273274
[IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
274275
[IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
275-
[IPSET_ATTR_PROBES] = { .type = NLA_U8 },
276+
[IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
276277
[IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
277278
[IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
278279
[IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },

‎net/netfilter/ipset/ip_set_hash_ipmark.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
#define IPSET_TYPE_REV_MIN 0
2323
/* 1 Forceadd support */
24-
#define IPSET_TYPE_REV_MAX 2 /* skbinfo support */
24+
/* 2 skbinfo support */
25+
#define IPSET_TYPE_REV_MAX 3 /* bucketsize support */
2526

2627
MODULE_LICENSE("GPL");
2728
MODULE_AUTHOR("Vytas Dauksa <vytas.dauksa@smoothwall.net>");
@@ -274,12 +275,13 @@ static struct ip_set_type hash_ipmark_type __read_mostly = {
274275
.family = NFPROTO_UNSPEC,
275276
.revision_min = IPSET_TYPE_REV_MIN,
276277
.revision_max = IPSET_TYPE_REV_MAX,
278+
.create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
277279
.create = hash_ipmark_create,
278280
.create_policy = {
279281
[IPSET_ATTR_MARKMASK] = { .type = NLA_U32 },
280282
[IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
281283
[IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
282-
[IPSET_ATTR_PROBES] = { .type = NLA_U8 },
284+
[IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
283285
[IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
284286
[IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
285287
[IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },

‎net/netfilter/ipset/ip_set_hash_ipport.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
/* 2 Counters support added */
2626
/* 3 Comments support added */
2727
/* 4 Forceadd support added */
28-
#define IPSET_TYPE_REV_MAX 5 /* skbinfo support added */
28+
/* 5 skbinfo support added */
29+
#define IPSET_TYPE_REV_MAX 6 /* bucketsize support added */
2930

3031
MODULE_LICENSE("GPL");
3132
MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
@@ -341,11 +342,12 @@ static struct ip_set_type hash_ipport_type __read_mostly = {
341342
.family = NFPROTO_UNSPEC,
342343
.revision_min = IPSET_TYPE_REV_MIN,
343344
.revision_max = IPSET_TYPE_REV_MAX,
345+
.create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
344346
.create = hash_ipport_create,
345347
.create_policy = {
346348
[IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
347349
[IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
348-
[IPSET_ATTR_PROBES] = { .type = NLA_U8 },
350+
[IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
349351
[IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
350352
[IPSET_ATTR_PROTO] = { .type = NLA_U8 },
351353
[IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },

‎net/netfilter/ipset/ip_set_hash_ipportip.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
/* 2 Counters support added */
2626
/* 3 Comments support added */
2727
/* 4 Forceadd support added */
28-
#define IPSET_TYPE_REV_MAX 5 /* skbinfo support added */
28+
/* 5 skbinfo support added */
29+
#define IPSET_TYPE_REV_MAX 6 /* bucketsize support added */
2930

3031
MODULE_LICENSE("GPL");
3132
MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
@@ -356,11 +357,12 @@ static struct ip_set_type hash_ipportip_type __read_mostly = {
356357
.family = NFPROTO_UNSPEC,
357358
.revision_min = IPSET_TYPE_REV_MIN,
358359
.revision_max = IPSET_TYPE_REV_MAX,
360+
.create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
359361
.create = hash_ipportip_create,
360362
.create_policy = {
361363
[IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
362364
[IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
363-
[IPSET_ATTR_PROBES] = { .type = NLA_U8 },
365+
[IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
364366
[IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
365367
[IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
366368
[IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },

‎net/netfilter/ipset/ip_set_hash_ipportnet.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
/* 4 Counters support added */
2828
/* 5 Comments support added */
2929
/* 6 Forceadd support added */
30-
#define IPSET_TYPE_REV_MAX 7 /* skbinfo support added */
30+
/* 7 skbinfo support added */
31+
#define IPSET_TYPE_REV_MAX 8 /* bucketsize support added */
3132

3233
MODULE_LICENSE("GPL");
3334
MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
@@ -513,11 +514,12 @@ static struct ip_set_type hash_ipportnet_type __read_mostly = {
513514
.family = NFPROTO_UNSPEC,
514515
.revision_min = IPSET_TYPE_REV_MIN,
515516
.revision_max = IPSET_TYPE_REV_MAX,
517+
.create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
516518
.create = hash_ipportnet_create,
517519
.create_policy = {
518520
[IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
519521
[IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
520-
[IPSET_ATTR_PROBES] = { .type = NLA_U8 },
522+
[IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
521523
[IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
522524
[IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
523525
[IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },

0 commit comments

Comments
 (0)