2 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/list.h>
15 #include <linux/log2.h>
16 #include <linux/jhash.h>
17 #include <linux/netlink.h>
18 #include <linux/workqueue.h>
19 #include <linux/rhashtable.h>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/nf_tables.h>
22 #include <net/netfilter/nf_tables.h>
24 /* We target a hash table size of 4, element hint is 75% of final size */
25 #define NFT_RHASH_ELEMENT_HINT 3
29 struct delayed_work gc_work
;
32 struct nft_rhash_elem
{
33 struct rhash_head node
;
34 struct nft_set_ext ext
;
37 struct nft_rhash_cmp_arg
{
38 const struct nft_set
*set
;
43 static inline u32
nft_rhash_key(const void *data
, u32 len
, u32 seed
)
45 const struct nft_rhash_cmp_arg
*arg
= data
;
47 return jhash(arg
->key
, len
, seed
);
50 static inline u32
nft_rhash_obj(const void *data
, u32 len
, u32 seed
)
52 const struct nft_rhash_elem
*he
= data
;
54 return jhash(nft_set_ext_key(&he
->ext
), len
, seed
);
57 static inline int nft_rhash_cmp(struct rhashtable_compare_arg
*arg
,
60 const struct nft_rhash_cmp_arg
*x
= arg
->key
;
61 const struct nft_rhash_elem
*he
= ptr
;
63 if (memcmp(nft_set_ext_key(&he
->ext
), x
->key
, x
->set
->klen
))
65 if (nft_set_elem_expired(&he
->ext
))
67 if (!nft_set_elem_active(&he
->ext
, x
->genmask
))
72 static const struct rhashtable_params nft_rhash_params
= {
73 .head_offset
= offsetof(struct nft_rhash_elem
, node
),
74 .hashfn
= nft_rhash_key
,
75 .obj_hashfn
= nft_rhash_obj
,
76 .obj_cmpfn
= nft_rhash_cmp
,
77 .automatic_shrinking
= true,
80 static bool nft_rhash_lookup(const struct net
*net
, const struct nft_set
*set
,
81 const u32
*key
, const struct nft_set_ext
**ext
)
83 struct nft_rhash
*priv
= nft_set_priv(set
);
84 const struct nft_rhash_elem
*he
;
85 struct nft_rhash_cmp_arg arg
= {
86 .genmask
= nft_genmask_cur(net
),
91 he
= rhashtable_lookup_fast(&priv
->ht
, &arg
, nft_rhash_params
);
98 static bool nft_rhash_update(struct nft_set
*set
, const u32
*key
,
99 void *(*new)(struct nft_set
*,
100 const struct nft_expr
*,
101 struct nft_regs
*regs
),
102 const struct nft_expr
*expr
,
103 struct nft_regs
*regs
,
104 const struct nft_set_ext
**ext
)
106 struct nft_rhash
*priv
= nft_set_priv(set
);
107 struct nft_rhash_elem
*he
, *prev
;
108 struct nft_rhash_cmp_arg arg
= {
109 .genmask
= NFT_GENMASK_ANY
,
114 he
= rhashtable_lookup_fast(&priv
->ht
, &arg
, nft_rhash_params
);
118 he
= new(set
, expr
, regs
);
122 prev
= rhashtable_lookup_get_insert_key(&priv
->ht
, &arg
, &he
->node
,
127 /* Another cpu may race to insert the element with the same key */
129 nft_set_elem_destroy(set
, he
, true);
138 nft_set_elem_destroy(set
, he
, true);
143 static int nft_rhash_insert(const struct net
*net
, const struct nft_set
*set
,
144 const struct nft_set_elem
*elem
,
145 struct nft_set_ext
**ext
)
147 struct nft_rhash
*priv
= nft_set_priv(set
);
148 struct nft_rhash_elem
*he
= elem
->priv
;
149 struct nft_rhash_cmp_arg arg
= {
150 .genmask
= nft_genmask_next(net
),
152 .key
= elem
->key
.val
.data
,
154 struct nft_rhash_elem
*prev
;
156 prev
= rhashtable_lookup_get_insert_key(&priv
->ht
, &arg
, &he
->node
,
159 return PTR_ERR(prev
);
167 static void nft_rhash_activate(const struct net
*net
, const struct nft_set
*set
,
168 const struct nft_set_elem
*elem
)
170 struct nft_rhash_elem
*he
= elem
->priv
;
172 nft_set_elem_change_active(net
, set
, &he
->ext
);
173 nft_set_elem_clear_busy(&he
->ext
);
176 static bool nft_rhash_flush(const struct net
*net
,
177 const struct nft_set
*set
, void *priv
)
179 struct nft_rhash_elem
*he
= priv
;
181 if (!nft_set_elem_mark_busy(&he
->ext
) ||
182 !nft_is_active(net
, &he
->ext
)) {
183 nft_set_elem_change_active(net
, set
, &he
->ext
);
189 static void *nft_rhash_deactivate(const struct net
*net
,
190 const struct nft_set
*set
,
191 const struct nft_set_elem
*elem
)
193 struct nft_rhash
*priv
= nft_set_priv(set
);
194 struct nft_rhash_elem
*he
;
195 struct nft_rhash_cmp_arg arg
= {
196 .genmask
= nft_genmask_next(net
),
198 .key
= elem
->key
.val
.data
,
202 he
= rhashtable_lookup_fast(&priv
->ht
, &arg
, nft_rhash_params
);
204 !nft_rhash_flush(net
, set
, he
))
212 static void nft_rhash_remove(const struct net
*net
,
213 const struct nft_set
*set
,
214 const struct nft_set_elem
*elem
)
216 struct nft_rhash
*priv
= nft_set_priv(set
);
217 struct nft_rhash_elem
*he
= elem
->priv
;
219 rhashtable_remove_fast(&priv
->ht
, &he
->node
, nft_rhash_params
);
222 static void nft_rhash_walk(const struct nft_ctx
*ctx
, struct nft_set
*set
,
223 struct nft_set_iter
*iter
)
225 struct nft_rhash
*priv
= nft_set_priv(set
);
226 struct nft_rhash_elem
*he
;
227 struct rhashtable_iter hti
;
228 struct nft_set_elem elem
;
231 err
= rhashtable_walk_init(&priv
->ht
, &hti
, GFP_ATOMIC
);
236 err
= rhashtable_walk_start(&hti
);
237 if (err
&& err
!= -EAGAIN
) {
242 while ((he
= rhashtable_walk_next(&hti
))) {
245 if (err
!= -EAGAIN
) {
253 if (iter
->count
< iter
->skip
)
255 if (nft_set_elem_expired(&he
->ext
))
257 if (!nft_set_elem_active(&he
->ext
, iter
->genmask
))
262 iter
->err
= iter
->fn(ctx
, set
, iter
, &elem
);
271 rhashtable_walk_stop(&hti
);
272 rhashtable_walk_exit(&hti
);
275 static void nft_rhash_gc(struct work_struct
*work
)
278 struct nft_rhash_elem
*he
;
279 struct nft_rhash
*priv
;
280 struct nft_set_gc_batch
*gcb
= NULL
;
281 struct rhashtable_iter hti
;
284 priv
= container_of(work
, struct nft_rhash
, gc_work
.work
);
285 set
= nft_set_container_of(priv
);
287 err
= rhashtable_walk_init(&priv
->ht
, &hti
, GFP_KERNEL
);
291 err
= rhashtable_walk_start(&hti
);
292 if (err
&& err
!= -EAGAIN
)
295 while ((he
= rhashtable_walk_next(&hti
))) {
297 if (PTR_ERR(he
) != -EAGAIN
)
302 if (!nft_set_elem_expired(&he
->ext
))
304 if (nft_set_elem_mark_busy(&he
->ext
))
307 gcb
= nft_set_gc_batch_check(set
, gcb
, GFP_ATOMIC
);
310 rhashtable_remove_fast(&priv
->ht
, &he
->node
, nft_rhash_params
);
311 atomic_dec(&set
->nelems
);
312 nft_set_gc_batch_add(gcb
, he
);
315 rhashtable_walk_stop(&hti
);
316 rhashtable_walk_exit(&hti
);
318 nft_set_gc_batch_complete(gcb
);
320 queue_delayed_work(system_power_efficient_wq
, &priv
->gc_work
,
321 nft_set_gc_interval(set
));
324 static unsigned int nft_rhash_privsize(const struct nlattr
* const nla
[],
325 const struct nft_set_desc
*desc
)
327 return sizeof(struct nft_rhash
);
330 static int nft_rhash_init(const struct nft_set
*set
,
331 const struct nft_set_desc
*desc
,
332 const struct nlattr
* const tb
[])
334 struct nft_rhash
*priv
= nft_set_priv(set
);
335 struct rhashtable_params params
= nft_rhash_params
;
338 params
.nelem_hint
= desc
->size
?: NFT_RHASH_ELEMENT_HINT
;
339 params
.key_len
= set
->klen
;
341 err
= rhashtable_init(&priv
->ht
, ¶ms
);
345 INIT_DEFERRABLE_WORK(&priv
->gc_work
, nft_rhash_gc
);
346 if (set
->flags
& NFT_SET_TIMEOUT
)
347 queue_delayed_work(system_power_efficient_wq
, &priv
->gc_work
,
348 nft_set_gc_interval(set
));
352 static void nft_rhash_elem_destroy(void *ptr
, void *arg
)
354 nft_set_elem_destroy(arg
, ptr
, true);
357 static void nft_rhash_destroy(const struct nft_set
*set
)
359 struct nft_rhash
*priv
= nft_set_priv(set
);
361 cancel_delayed_work_sync(&priv
->gc_work
);
362 rhashtable_free_and_destroy(&priv
->ht
, nft_rhash_elem_destroy
,
366 static u32
nft_hash_buckets(u32 size
)
368 return roundup_pow_of_two(size
* 4 / 3);
371 static bool nft_rhash_estimate(const struct nft_set_desc
*desc
, u32 features
,
372 struct nft_set_estimate
*est
)
375 est
->lookup
= NFT_SET_CLASS_O_1
;
376 est
->space
= NFT_SET_CLASS_O_N
;
384 struct hlist_head table
[];
387 struct nft_hash_elem
{
388 struct hlist_node node
;
389 struct nft_set_ext ext
;
392 static bool nft_hash_lookup(const struct net
*net
, const struct nft_set
*set
,
393 const u32
*key
, const struct nft_set_ext
**ext
)
395 struct nft_hash
*priv
= nft_set_priv(set
);
396 u8 genmask
= nft_genmask_cur(net
);
397 const struct nft_hash_elem
*he
;
400 hash
= jhash(key
, set
->klen
, priv
->seed
);
401 hash
= reciprocal_scale(hash
, priv
->buckets
);
402 hlist_for_each_entry_rcu(he
, &priv
->table
[hash
], node
) {
403 if (!memcmp(nft_set_ext_key(&he
->ext
), key
, set
->klen
) &&
404 nft_set_elem_active(&he
->ext
, genmask
)) {
412 /* nft_hash_select_ops() makes sure key size can be either 2 or 4 bytes . */
413 static inline u32
nft_hash_key(const u32
*key
, u32 klen
)
421 static bool nft_hash_lookup_fast(const struct net
*net
,
422 const struct nft_set
*set
,
423 const u32
*key
, const struct nft_set_ext
**ext
)
425 struct nft_hash
*priv
= nft_set_priv(set
);
426 u8 genmask
= nft_genmask_cur(net
);
427 const struct nft_hash_elem
*he
;
430 k1
= nft_hash_key(key
, set
->klen
);
431 hash
= jhash_1word(k1
, priv
->seed
);
432 hash
= reciprocal_scale(hash
, priv
->buckets
);
433 hlist_for_each_entry_rcu(he
, &priv
->table
[hash
], node
) {
434 k2
= nft_hash_key(nft_set_ext_key(&he
->ext
)->data
, set
->klen
);
436 nft_set_elem_active(&he
->ext
, genmask
)) {
444 static int nft_hash_insert(const struct net
*net
, const struct nft_set
*set
,
445 const struct nft_set_elem
*elem
,
446 struct nft_set_ext
**ext
)
448 struct nft_hash_elem
*this = elem
->priv
, *he
;
449 struct nft_hash
*priv
= nft_set_priv(set
);
450 u8 genmask
= nft_genmask_next(net
);
453 hash
= jhash(nft_set_ext_key(&this->ext
), set
->klen
, priv
->seed
);
454 hash
= reciprocal_scale(hash
, priv
->buckets
);
455 hlist_for_each_entry(he
, &priv
->table
[hash
], node
) {
456 if (!memcmp(nft_set_ext_key(&this->ext
),
457 nft_set_ext_key(&he
->ext
), set
->klen
) &&
458 nft_set_elem_active(&he
->ext
, genmask
)) {
463 hlist_add_head_rcu(&this->node
, &priv
->table
[hash
]);
467 static void nft_hash_activate(const struct net
*net
, const struct nft_set
*set
,
468 const struct nft_set_elem
*elem
)
470 struct nft_hash_elem
*he
= elem
->priv
;
472 nft_set_elem_change_active(net
, set
, &he
->ext
);
475 static bool nft_hash_flush(const struct net
*net
,
476 const struct nft_set
*set
, void *priv
)
478 struct nft_hash_elem
*he
= priv
;
480 nft_set_elem_change_active(net
, set
, &he
->ext
);
484 static void *nft_hash_deactivate(const struct net
*net
,
485 const struct nft_set
*set
,
486 const struct nft_set_elem
*elem
)
488 struct nft_hash
*priv
= nft_set_priv(set
);
489 struct nft_hash_elem
*this = elem
->priv
, *he
;
490 u8 genmask
= nft_genmask_next(net
);
493 hash
= jhash(nft_set_ext_key(&this->ext
), set
->klen
, priv
->seed
);
494 hash
= reciprocal_scale(hash
, priv
->buckets
);
495 hlist_for_each_entry(he
, &priv
->table
[hash
], node
) {
496 if (!memcmp(nft_set_ext_key(&this->ext
), &elem
->key
.val
,
498 nft_set_elem_active(&he
->ext
, genmask
)) {
499 nft_set_elem_change_active(net
, set
, &he
->ext
);
506 static void nft_hash_remove(const struct net
*net
,
507 const struct nft_set
*set
,
508 const struct nft_set_elem
*elem
)
510 struct nft_hash_elem
*he
= elem
->priv
;
512 hlist_del_rcu(&he
->node
);
515 static void nft_hash_walk(const struct nft_ctx
*ctx
, struct nft_set
*set
,
516 struct nft_set_iter
*iter
)
518 struct nft_hash
*priv
= nft_set_priv(set
);
519 struct nft_hash_elem
*he
;
520 struct nft_set_elem elem
;
523 for (i
= 0; i
< priv
->buckets
; i
++) {
524 hlist_for_each_entry_rcu(he
, &priv
->table
[i
], node
) {
525 if (iter
->count
< iter
->skip
)
527 if (!nft_set_elem_active(&he
->ext
, iter
->genmask
))
532 iter
->err
= iter
->fn(ctx
, set
, iter
, &elem
);
541 static unsigned int nft_hash_privsize(const struct nlattr
* const nla
[],
542 const struct nft_set_desc
*desc
)
544 return sizeof(struct nft_hash
) +
545 nft_hash_buckets(desc
->size
) * sizeof(struct hlist_head
);
548 static int nft_hash_init(const struct nft_set
*set
,
549 const struct nft_set_desc
*desc
,
550 const struct nlattr
* const tb
[])
552 struct nft_hash
*priv
= nft_set_priv(set
);
554 priv
->buckets
= nft_hash_buckets(desc
->size
);
555 get_random_bytes(&priv
->seed
, sizeof(priv
->seed
));
560 static void nft_hash_destroy(const struct nft_set
*set
)
562 struct nft_hash
*priv
= nft_set_priv(set
);
563 struct nft_hash_elem
*he
;
564 struct hlist_node
*next
;
567 for (i
= 0; i
< priv
->buckets
; i
++) {
568 hlist_for_each_entry_safe(he
, next
, &priv
->table
[i
], node
) {
569 hlist_del_rcu(&he
->node
);
570 nft_set_elem_destroy(set
, he
, true);
575 static bool nft_hash_estimate(const struct nft_set_desc
*desc
, u32 features
,
576 struct nft_set_estimate
*est
)
578 est
->size
= sizeof(struct nft_hash
) +
579 nft_hash_buckets(desc
->size
) * sizeof(struct hlist_head
) +
580 desc
->size
* sizeof(struct nft_hash_elem
);
581 est
->lookup
= NFT_SET_CLASS_O_1
;
582 est
->space
= NFT_SET_CLASS_O_N
;
587 static struct nft_set_type nft_hash_type
;
588 static struct nft_set_ops nft_rhash_ops __read_mostly
= {
589 .type
= &nft_hash_type
,
590 .privsize
= nft_rhash_privsize
,
591 .elemsize
= offsetof(struct nft_rhash_elem
, ext
),
592 .estimate
= nft_rhash_estimate
,
593 .init
= nft_rhash_init
,
594 .destroy
= nft_rhash_destroy
,
595 .insert
= nft_rhash_insert
,
596 .activate
= nft_rhash_activate
,
597 .deactivate
= nft_rhash_deactivate
,
598 .flush
= nft_rhash_flush
,
599 .remove
= nft_rhash_remove
,
600 .lookup
= nft_rhash_lookup
,
601 .update
= nft_rhash_update
,
602 .walk
= nft_rhash_walk
,
603 .features
= NFT_SET_MAP
| NFT_SET_OBJECT
| NFT_SET_TIMEOUT
,
606 static struct nft_set_ops nft_hash_ops __read_mostly
= {
607 .type
= &nft_hash_type
,
608 .privsize
= nft_hash_privsize
,
609 .elemsize
= offsetof(struct nft_hash_elem
, ext
),
610 .estimate
= nft_hash_estimate
,
611 .init
= nft_hash_init
,
612 .destroy
= nft_hash_destroy
,
613 .insert
= nft_hash_insert
,
614 .activate
= nft_hash_activate
,
615 .deactivate
= nft_hash_deactivate
,
616 .flush
= nft_hash_flush
,
617 .remove
= nft_hash_remove
,
618 .lookup
= nft_hash_lookup
,
619 .walk
= nft_hash_walk
,
620 .features
= NFT_SET_MAP
| NFT_SET_OBJECT
,
623 static struct nft_set_ops nft_hash_fast_ops __read_mostly
= {
624 .type
= &nft_hash_type
,
625 .privsize
= nft_hash_privsize
,
626 .elemsize
= offsetof(struct nft_hash_elem
, ext
),
627 .estimate
= nft_hash_estimate
,
628 .init
= nft_hash_init
,
629 .destroy
= nft_hash_destroy
,
630 .insert
= nft_hash_insert
,
631 .activate
= nft_hash_activate
,
632 .deactivate
= nft_hash_deactivate
,
633 .flush
= nft_hash_flush
,
634 .remove
= nft_hash_remove
,
635 .lookup
= nft_hash_lookup_fast
,
636 .walk
= nft_hash_walk
,
637 .features
= NFT_SET_MAP
| NFT_SET_OBJECT
,
640 static const struct nft_set_ops
*
641 nft_hash_select_ops(const struct nft_ctx
*ctx
, const struct nft_set_desc
*desc
,
645 switch (desc
->klen
) {
647 return &nft_hash_fast_ops
;
649 return &nft_hash_ops
;
653 return &nft_rhash_ops
;
656 static struct nft_set_type nft_hash_type __read_mostly
= {
657 .select_ops
= nft_hash_select_ops
,
658 .owner
= THIS_MODULE
,
661 static int __init
nft_hash_module_init(void)
663 return nft_register_set(&nft_hash_type
);
666 static void __exit
nft_hash_module_exit(void)
668 nft_unregister_set(&nft_hash_type
);
671 module_init(nft_hash_module_init
);
672 module_exit(nft_hash_module_exit
);
674 MODULE_LICENSE("GPL");
675 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
676 MODULE_ALIAS_NFT_SET();