1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/list.h>
12 #include <linux/log2.h>
13 #include <linux/jhash.h>
14 #include <linux/netlink.h>
15 #include <linux/workqueue.h>
16 #include <linux/rhashtable.h>
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_tables_core.h>
21 /* We target a hash table size of 4, element hint is 75% of final size */
22 #define NFT_RHASH_ELEMENT_HINT 3
26 struct delayed_work gc_work;
29 struct nft_rhash_elem {
30 struct rhash_head node;
31 struct nft_set_ext ext;
34 struct nft_rhash_cmp_arg {
35 const struct nft_set *set;
40 static inline u32 nft_rhash_key(const void *data, u32 len, u32 seed)
42 const struct nft_rhash_cmp_arg *arg = data;
44 return jhash(arg->key, len, seed);
47 static inline u32 nft_rhash_obj(const void *data, u32 len, u32 seed)
49 const struct nft_rhash_elem *he = data;
51 return jhash(nft_set_ext_key(&he->ext), len, seed);
54 static inline int nft_rhash_cmp(struct rhashtable_compare_arg *arg,
57 const struct nft_rhash_cmp_arg *x = arg->key;
58 const struct nft_rhash_elem *he = ptr;
60 if (memcmp(nft_set_ext_key(&he->ext), x->key, x->set->klen))
62 if (nft_set_elem_is_dead(&he->ext))
64 if (nft_set_elem_expired(&he->ext))
66 if (!nft_set_elem_active(&he->ext, x->genmask))
71 static const struct rhashtable_params nft_rhash_params = {
72 .head_offset = offsetof(struct nft_rhash_elem, node),
73 .hashfn = nft_rhash_key,
74 .obj_hashfn = nft_rhash_obj,
75 .obj_cmpfn = nft_rhash_cmp,
76 .automatic_shrinking = true,
79 INDIRECT_CALLABLE_SCOPE
80 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(&priv->ht, &arg, nft_rhash_params);
98 static void *nft_rhash_get(const struct net *net, const struct nft_set *set,
99 const struct nft_set_elem *elem, unsigned int flags)
101 struct nft_rhash *priv = nft_set_priv(set);
102 struct nft_rhash_elem *he;
103 struct nft_rhash_cmp_arg arg = {
104 .genmask = nft_genmask_cur(net),
106 .key = elem->key.val.data,
109 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
113 return ERR_PTR(-ENOENT);
116 static bool nft_rhash_update(struct nft_set *set, const u32 *key,
117 void *(*new)(struct nft_set *,
118 const struct nft_expr *,
119 struct nft_regs *regs),
120 const struct nft_expr *expr,
121 struct nft_regs *regs,
122 const struct nft_set_ext **ext)
124 struct nft_rhash *priv = nft_set_priv(set);
125 struct nft_rhash_elem *he, *prev;
126 struct nft_rhash_cmp_arg arg = {
127 .genmask = NFT_GENMASK_ANY,
132 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
136 he = new(set, expr, regs);
140 prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
145 /* Another cpu may race to insert the element with the same key */
147 nft_set_elem_destroy(set, he, true);
148 atomic_dec(&set->nelems);
157 nft_set_elem_destroy(set, he, true);
158 atomic_dec(&set->nelems);
163 static int nft_rhash_insert(const struct net *net, const struct nft_set *set,
164 const struct nft_set_elem *elem,
165 struct nft_set_ext **ext)
167 struct nft_rhash *priv = nft_set_priv(set);
168 struct nft_rhash_elem *he = elem->priv;
169 struct nft_rhash_cmp_arg arg = {
170 .genmask = nft_genmask_next(net),
172 .key = elem->key.val.data,
174 struct nft_rhash_elem *prev;
176 prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
179 return PTR_ERR(prev);
187 static void nft_rhash_activate(const struct net *net, const struct nft_set *set,
188 const struct nft_set_elem *elem)
190 struct nft_rhash_elem *he = elem->priv;
192 nft_set_elem_change_active(net, set, &he->ext);
195 static bool nft_rhash_flush(const struct net *net,
196 const struct nft_set *set, void *priv)
198 struct nft_rhash_elem *he = priv;
200 nft_set_elem_change_active(net, set, &he->ext);
205 static void *nft_rhash_deactivate(const struct net *net,
206 const struct nft_set *set,
207 const struct nft_set_elem *elem)
209 struct nft_rhash *priv = nft_set_priv(set);
210 struct nft_rhash_elem *he;
211 struct nft_rhash_cmp_arg arg = {
212 .genmask = nft_genmask_next(net),
214 .key = elem->key.val.data,
218 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
220 nft_set_elem_change_active(net, set, &he->ext);
227 static void nft_rhash_remove(const struct net *net,
228 const struct nft_set *set,
229 const struct nft_set_elem *elem)
231 struct nft_rhash *priv = nft_set_priv(set);
232 struct nft_rhash_elem *he = elem->priv;
234 rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
237 static bool nft_rhash_delete(const struct nft_set *set,
240 struct nft_rhash *priv = nft_set_priv(set);
241 struct nft_rhash_cmp_arg arg = {
242 .genmask = NFT_GENMASK_ANY,
246 struct nft_rhash_elem *he;
248 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
252 nft_set_elem_dead(&he->ext);
257 static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
258 struct nft_set_iter *iter)
260 struct nft_rhash *priv = nft_set_priv(set);
261 struct nft_rhash_elem *he;
262 struct rhashtable_iter hti;
263 struct nft_set_elem elem;
265 rhashtable_walk_enter(&priv->ht, &hti);
266 rhashtable_walk_start(&hti);
268 while ((he = rhashtable_walk_next(&hti))) {
270 if (PTR_ERR(he) != -EAGAIN) {
271 iter->err = PTR_ERR(he);
278 if (iter->count < iter->skip)
280 if (!nft_set_elem_active(&he->ext, iter->genmask))
285 iter->err = iter->fn(ctx, set, iter, &elem);
292 rhashtable_walk_stop(&hti);
293 rhashtable_walk_exit(&hti);
296 static bool nft_rhash_expr_needs_gc_run(const struct nft_set *set,
297 struct nft_set_ext *ext)
299 struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext);
300 struct nft_expr *expr;
303 nft_setelem_expr_foreach(expr, elem_expr, size) {
305 expr->ops->gc(read_pnet(&set->net), expr))
312 static void nft_rhash_gc(struct work_struct *work)
314 struct nftables_pernet *nft_net;
316 struct nft_rhash_elem *he;
317 struct nft_rhash *priv;
318 struct rhashtable_iter hti;
319 struct nft_trans_gc *gc;
323 priv = container_of(work, struct nft_rhash, gc_work.work);
324 set = nft_set_container_of(priv);
325 net = read_pnet(&set->net);
326 nft_net = nft_pernet(net);
327 gc_seq = READ_ONCE(nft_net->gc_seq);
329 if (nft_set_gc_is_pending(set))
332 gc = nft_trans_gc_alloc(set, gc_seq, GFP_KERNEL);
336 rhashtable_walk_enter(&priv->ht, &hti);
337 rhashtable_walk_start(&hti);
339 while ((he = rhashtable_walk_next(&hti))) {
341 nft_trans_gc_destroy(gc);
346 /* Ruleset has been updated, try later. */
347 if (READ_ONCE(nft_net->gc_seq) != gc_seq) {
348 nft_trans_gc_destroy(gc);
353 if (nft_set_elem_is_dead(&he->ext))
356 if (nft_set_ext_exists(&he->ext, NFT_SET_EXT_EXPRESSIONS) &&
357 nft_rhash_expr_needs_gc_run(set, &he->ext))
360 if (!nft_set_elem_expired(&he->ext))
363 nft_set_elem_dead(&he->ext);
365 gc = nft_trans_gc_queue_async(gc, gc_seq, GFP_ATOMIC);
369 nft_trans_gc_elem_add(gc, he);
372 gc = nft_trans_gc_catchall_async(gc, gc_seq);
375 /* catchall list iteration requires rcu read side lock. */
376 rhashtable_walk_stop(&hti);
377 rhashtable_walk_exit(&hti);
380 nft_trans_gc_queue_async_done(gc);
383 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
384 nft_set_gc_interval(set));
387 static u64 nft_rhash_privsize(const struct nlattr * const nla[],
388 const struct nft_set_desc *desc)
390 return sizeof(struct nft_rhash);
393 static void nft_rhash_gc_init(const struct nft_set *set)
395 struct nft_rhash *priv = nft_set_priv(set);
397 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
398 nft_set_gc_interval(set));
401 static int nft_rhash_init(const struct nft_set *set,
402 const struct nft_set_desc *desc,
403 const struct nlattr * const tb[])
405 struct nft_rhash *priv = nft_set_priv(set);
406 struct rhashtable_params params = nft_rhash_params;
409 params.nelem_hint = desc->size ?: NFT_RHASH_ELEMENT_HINT;
410 params.key_len = set->klen;
412 err = rhashtable_init(&priv->ht, ¶ms);
416 INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc);
417 if (set->flags & (NFT_SET_TIMEOUT | NFT_SET_EVAL))
418 nft_rhash_gc_init(set);
423 struct nft_rhash_ctx {
424 const struct nft_ctx ctx;
425 const struct nft_set *set;
428 static void nft_rhash_elem_destroy(void *ptr, void *arg)
430 struct nft_rhash_ctx *rhash_ctx = arg;
432 nf_tables_set_elem_destroy(&rhash_ctx->ctx, rhash_ctx->set, ptr);
435 static void nft_rhash_destroy(const struct nft_ctx *ctx,
436 const struct nft_set *set)
438 struct nft_rhash *priv = nft_set_priv(set);
439 struct nft_rhash_ctx rhash_ctx = {
444 cancel_delayed_work_sync(&priv->gc_work);
445 rhashtable_free_and_destroy(&priv->ht, nft_rhash_elem_destroy,
449 /* Number of buckets is stored in u32, so cap our result to 1U<<31 */
450 #define NFT_MAX_BUCKETS (1U << 31)
452 static u32 nft_hash_buckets(u32 size)
454 u64 val = div_u64((u64)size * 4, 3);
456 if (val >= NFT_MAX_BUCKETS)
457 return NFT_MAX_BUCKETS;
459 return roundup_pow_of_two(val);
462 static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features,
463 struct nft_set_estimate *est)
466 est->lookup = NFT_SET_CLASS_O_1;
467 est->space = NFT_SET_CLASS_O_N;
475 struct hlist_head table[];
478 struct nft_hash_elem {
479 struct hlist_node node;
480 struct nft_set_ext ext;
483 INDIRECT_CALLABLE_SCOPE
484 bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
485 const u32 *key, const struct nft_set_ext **ext)
487 struct nft_hash *priv = nft_set_priv(set);
488 u8 genmask = nft_genmask_cur(net);
489 const struct nft_hash_elem *he;
492 hash = jhash(key, set->klen, priv->seed);
493 hash = reciprocal_scale(hash, priv->buckets);
494 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
495 if (!memcmp(nft_set_ext_key(&he->ext), key, set->klen) &&
496 nft_set_elem_active(&he->ext, genmask)) {
504 static void *nft_hash_get(const struct net *net, const struct nft_set *set,
505 const struct nft_set_elem *elem, unsigned int flags)
507 struct nft_hash *priv = nft_set_priv(set);
508 u8 genmask = nft_genmask_cur(net);
509 struct nft_hash_elem *he;
512 hash = jhash(elem->key.val.data, set->klen, priv->seed);
513 hash = reciprocal_scale(hash, priv->buckets);
514 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
515 if (!memcmp(nft_set_ext_key(&he->ext), elem->key.val.data, set->klen) &&
516 nft_set_elem_active(&he->ext, genmask))
519 return ERR_PTR(-ENOENT);
522 INDIRECT_CALLABLE_SCOPE
523 bool nft_hash_lookup_fast(const struct net *net,
524 const struct nft_set *set,
525 const u32 *key, const struct nft_set_ext **ext)
527 struct nft_hash *priv = nft_set_priv(set);
528 u8 genmask = nft_genmask_cur(net);
529 const struct nft_hash_elem *he;
533 hash = jhash_1word(k1, priv->seed);
534 hash = reciprocal_scale(hash, priv->buckets);
535 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
536 k2 = *(u32 *)nft_set_ext_key(&he->ext)->data;
538 nft_set_elem_active(&he->ext, genmask)) {
546 static u32 nft_jhash(const struct nft_set *set, const struct nft_hash *priv,
547 const struct nft_set_ext *ext)
549 const struct nft_data *key = nft_set_ext_key(ext);
552 if (set->klen == 4) {
554 hash = jhash_1word(k1, priv->seed);
556 hash = jhash(key, set->klen, priv->seed);
558 hash = reciprocal_scale(hash, priv->buckets);
563 static int nft_hash_insert(const struct net *net, const struct nft_set *set,
564 const struct nft_set_elem *elem,
565 struct nft_set_ext **ext)
567 struct nft_hash_elem *this = elem->priv, *he;
568 struct nft_hash *priv = nft_set_priv(set);
569 u8 genmask = nft_genmask_next(net);
572 hash = nft_jhash(set, priv, &this->ext);
573 hlist_for_each_entry(he, &priv->table[hash], node) {
574 if (!memcmp(nft_set_ext_key(&this->ext),
575 nft_set_ext_key(&he->ext), set->klen) &&
576 nft_set_elem_active(&he->ext, genmask)) {
581 hlist_add_head_rcu(&this->node, &priv->table[hash]);
585 static void nft_hash_activate(const struct net *net, const struct nft_set *set,
586 const struct nft_set_elem *elem)
588 struct nft_hash_elem *he = elem->priv;
590 nft_set_elem_change_active(net, set, &he->ext);
593 static bool nft_hash_flush(const struct net *net,
594 const struct nft_set *set, void *priv)
596 struct nft_hash_elem *he = priv;
598 nft_set_elem_change_active(net, set, &he->ext);
602 static void *nft_hash_deactivate(const struct net *net,
603 const struct nft_set *set,
604 const struct nft_set_elem *elem)
606 struct nft_hash *priv = nft_set_priv(set);
607 struct nft_hash_elem *this = elem->priv, *he;
608 u8 genmask = nft_genmask_next(net);
611 hash = nft_jhash(set, priv, &this->ext);
612 hlist_for_each_entry(he, &priv->table[hash], node) {
613 if (!memcmp(nft_set_ext_key(&he->ext), &elem->key.val,
615 nft_set_elem_active(&he->ext, genmask)) {
616 nft_set_elem_change_active(net, set, &he->ext);
623 static void nft_hash_remove(const struct net *net,
624 const struct nft_set *set,
625 const struct nft_set_elem *elem)
627 struct nft_hash_elem *he = elem->priv;
629 hlist_del_rcu(&he->node);
632 static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set,
633 struct nft_set_iter *iter)
635 struct nft_hash *priv = nft_set_priv(set);
636 struct nft_hash_elem *he;
637 struct nft_set_elem elem;
640 for (i = 0; i < priv->buckets; i++) {
641 hlist_for_each_entry_rcu(he, &priv->table[i], node) {
642 if (iter->count < iter->skip)
644 if (!nft_set_elem_active(&he->ext, iter->genmask))
649 iter->err = iter->fn(ctx, set, iter, &elem);
658 static u64 nft_hash_privsize(const struct nlattr * const nla[],
659 const struct nft_set_desc *desc)
661 return sizeof(struct nft_hash) +
662 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head);
665 static int nft_hash_init(const struct nft_set *set,
666 const struct nft_set_desc *desc,
667 const struct nlattr * const tb[])
669 struct nft_hash *priv = nft_set_priv(set);
671 priv->buckets = nft_hash_buckets(desc->size);
672 get_random_bytes(&priv->seed, sizeof(priv->seed));
677 static void nft_hash_destroy(const struct nft_ctx *ctx,
678 const struct nft_set *set)
680 struct nft_hash *priv = nft_set_priv(set);
681 struct nft_hash_elem *he;
682 struct hlist_node *next;
685 for (i = 0; i < priv->buckets; i++) {
686 hlist_for_each_entry_safe(he, next, &priv->table[i], node) {
687 hlist_del_rcu(&he->node);
688 nf_tables_set_elem_destroy(ctx, set, he);
693 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
694 struct nft_set_estimate *est)
702 est->size = sizeof(struct nft_hash) +
703 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
704 (u64)desc->size * sizeof(struct nft_hash_elem);
705 est->lookup = NFT_SET_CLASS_O_1;
706 est->space = NFT_SET_CLASS_O_N;
711 static bool nft_hash_fast_estimate(const struct nft_set_desc *desc, u32 features,
712 struct nft_set_estimate *est)
720 est->size = sizeof(struct nft_hash) +
721 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
722 (u64)desc->size * sizeof(struct nft_hash_elem);
723 est->lookup = NFT_SET_CLASS_O_1;
724 est->space = NFT_SET_CLASS_O_N;
729 const struct nft_set_type nft_set_rhash_type = {
730 .features = NFT_SET_MAP | NFT_SET_OBJECT |
731 NFT_SET_TIMEOUT | NFT_SET_EVAL,
733 .privsize = nft_rhash_privsize,
734 .elemsize = offsetof(struct nft_rhash_elem, ext),
735 .estimate = nft_rhash_estimate,
736 .init = nft_rhash_init,
737 .gc_init = nft_rhash_gc_init,
738 .destroy = nft_rhash_destroy,
739 .insert = nft_rhash_insert,
740 .activate = nft_rhash_activate,
741 .deactivate = nft_rhash_deactivate,
742 .flush = nft_rhash_flush,
743 .remove = nft_rhash_remove,
744 .lookup = nft_rhash_lookup,
745 .update = nft_rhash_update,
746 .delete = nft_rhash_delete,
747 .walk = nft_rhash_walk,
748 .get = nft_rhash_get,
752 const struct nft_set_type nft_set_hash_type = {
753 .features = NFT_SET_MAP | NFT_SET_OBJECT,
755 .privsize = nft_hash_privsize,
756 .elemsize = offsetof(struct nft_hash_elem, ext),
757 .estimate = nft_hash_estimate,
758 .init = nft_hash_init,
759 .destroy = nft_hash_destroy,
760 .insert = nft_hash_insert,
761 .activate = nft_hash_activate,
762 .deactivate = nft_hash_deactivate,
763 .flush = nft_hash_flush,
764 .remove = nft_hash_remove,
765 .lookup = nft_hash_lookup,
766 .walk = nft_hash_walk,
771 const struct nft_set_type nft_set_hash_fast_type = {
772 .features = NFT_SET_MAP | NFT_SET_OBJECT,
774 .privsize = nft_hash_privsize,
775 .elemsize = offsetof(struct nft_hash_elem, ext),
776 .estimate = nft_hash_fast_estimate,
777 .init = nft_hash_init,
778 .destroy = nft_hash_destroy,
779 .insert = nft_hash_insert,
780 .activate = nft_hash_activate,
781 .deactivate = nft_hash_deactivate,
782 .flush = nft_hash_flush,
783 .remove = nft_hash_remove,
784 .lookup = nft_hash_lookup_fast,
785 .walk = nft_hash_walk,