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 nft_elem_priv priv;
31 struct rhash_head node;
32 struct nft_set_ext ext;
35 struct nft_rhash_cmp_arg {
36 const struct nft_set *set;
42 static inline u32 nft_rhash_key(const void *data, u32 len, u32 seed)
44 const struct nft_rhash_cmp_arg *arg = data;
46 return jhash(arg->key, len, seed);
49 static inline u32 nft_rhash_obj(const void *data, u32 len, u32 seed)
51 const struct nft_rhash_elem *he = data;
53 return jhash(nft_set_ext_key(&he->ext), len, seed);
56 static inline int nft_rhash_cmp(struct rhashtable_compare_arg *arg,
59 const struct nft_rhash_cmp_arg *x = arg->key;
60 const struct nft_rhash_elem *he = ptr;
62 if (memcmp(nft_set_ext_key(&he->ext), x->key, x->set->klen))
64 if (nft_set_elem_is_dead(&he->ext))
66 if (__nft_set_elem_expired(&he->ext, x->tstamp))
68 if (!nft_set_elem_active(&he->ext, x->genmask))
73 static const struct rhashtable_params nft_rhash_params = {
74 .head_offset = offsetof(struct nft_rhash_elem, node),
75 .hashfn = nft_rhash_key,
76 .obj_hashfn = nft_rhash_obj,
77 .obj_cmpfn = nft_rhash_cmp,
78 .automatic_shrinking = true,
81 INDIRECT_CALLABLE_SCOPE
82 bool nft_rhash_lookup(const struct net *net, const struct nft_set *set,
83 const u32 *key, const struct nft_set_ext **ext)
85 struct nft_rhash *priv = nft_set_priv(set);
86 const struct nft_rhash_elem *he;
87 struct nft_rhash_cmp_arg arg = {
88 .genmask = nft_genmask_cur(net),
91 .tstamp = get_jiffies_64(),
94 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
101 static struct nft_elem_priv *
102 nft_rhash_get(const struct net *net, const struct nft_set *set,
103 const struct nft_set_elem *elem, unsigned int flags)
105 struct nft_rhash *priv = nft_set_priv(set);
106 struct nft_rhash_elem *he;
107 struct nft_rhash_cmp_arg arg = {
108 .genmask = nft_genmask_cur(net),
110 .key = elem->key.val.data,
111 .tstamp = get_jiffies_64(),
114 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
118 return ERR_PTR(-ENOENT);
121 static bool nft_rhash_update(struct nft_set *set, const u32 *key,
122 struct nft_elem_priv *
123 (*new)(struct nft_set *,
124 const struct nft_expr *,
125 struct nft_regs *regs),
126 const struct nft_expr *expr,
127 struct nft_regs *regs,
128 const struct nft_set_ext **ext)
130 struct nft_rhash *priv = nft_set_priv(set);
131 struct nft_rhash_elem *he, *prev;
132 struct nft_elem_priv *elem_priv;
133 struct nft_rhash_cmp_arg arg = {
134 .genmask = NFT_GENMASK_ANY,
137 .tstamp = get_jiffies_64(),
140 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
144 elem_priv = new(set, expr, regs);
148 he = nft_elem_priv_cast(elem_priv);
149 prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
154 /* Another cpu may race to insert the element with the same key */
156 nft_set_elem_destroy(set, &he->priv, true);
157 atomic_dec(&set->nelems);
166 nft_set_elem_destroy(set, &he->priv, true);
167 atomic_dec(&set->nelems);
172 static int nft_rhash_insert(const struct net *net, const struct nft_set *set,
173 const struct nft_set_elem *elem,
174 struct nft_elem_priv **elem_priv)
176 struct nft_rhash_elem *he = nft_elem_priv_cast(elem->priv);
177 struct nft_rhash *priv = nft_set_priv(set);
178 struct nft_rhash_cmp_arg arg = {
179 .genmask = nft_genmask_next(net),
181 .key = elem->key.val.data,
182 .tstamp = nft_net_tstamp(net),
184 struct nft_rhash_elem *prev;
186 prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
189 return PTR_ERR(prev);
191 *elem_priv = &prev->priv;
197 static void nft_rhash_activate(const struct net *net, const struct nft_set *set,
198 struct nft_elem_priv *elem_priv)
200 struct nft_rhash_elem *he = nft_elem_priv_cast(elem_priv);
202 nft_set_elem_change_active(net, set, &he->ext);
205 static void nft_rhash_flush(const struct net *net,
206 const struct nft_set *set,
207 struct nft_elem_priv *elem_priv)
209 struct nft_rhash_elem *he = nft_elem_priv_cast(elem_priv);
211 nft_set_elem_change_active(net, set, &he->ext);
214 static struct nft_elem_priv *
215 nft_rhash_deactivate(const struct net *net, const struct nft_set *set,
216 const struct nft_set_elem *elem)
218 struct nft_rhash *priv = nft_set_priv(set);
219 struct nft_rhash_elem *he;
220 struct nft_rhash_cmp_arg arg = {
221 .genmask = nft_genmask_next(net),
223 .key = elem->key.val.data,
224 .tstamp = nft_net_tstamp(net),
228 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
230 nft_set_elem_change_active(net, set, &he->ext);
237 static void nft_rhash_remove(const struct net *net,
238 const struct nft_set *set,
239 struct nft_elem_priv *elem_priv)
241 struct nft_rhash_elem *he = nft_elem_priv_cast(elem_priv);
242 struct nft_rhash *priv = nft_set_priv(set);
244 rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
247 static bool nft_rhash_delete(const struct nft_set *set,
250 struct nft_rhash *priv = nft_set_priv(set);
251 struct nft_rhash_cmp_arg arg = {
252 .genmask = NFT_GENMASK_ANY,
256 struct nft_rhash_elem *he;
258 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
262 nft_set_elem_dead(&he->ext);
267 static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
268 struct nft_set_iter *iter)
270 struct nft_rhash *priv = nft_set_priv(set);
271 struct nft_rhash_elem *he;
272 struct rhashtable_iter hti;
274 rhashtable_walk_enter(&priv->ht, &hti);
275 rhashtable_walk_start(&hti);
277 while ((he = rhashtable_walk_next(&hti))) {
279 if (PTR_ERR(he) != -EAGAIN) {
280 iter->err = PTR_ERR(he);
287 if (iter->count < iter->skip)
289 if (!nft_set_elem_active(&he->ext, iter->genmask))
292 iter->err = iter->fn(ctx, set, iter, &he->priv);
299 rhashtable_walk_stop(&hti);
300 rhashtable_walk_exit(&hti);
303 static bool nft_rhash_expr_needs_gc_run(const struct nft_set *set,
304 struct nft_set_ext *ext)
306 struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext);
307 struct nft_expr *expr;
310 nft_setelem_expr_foreach(expr, elem_expr, size) {
312 expr->ops->gc(read_pnet(&set->net), expr))
319 static void nft_rhash_gc(struct work_struct *work)
321 struct nftables_pernet *nft_net;
323 struct nft_rhash_elem *he;
324 struct nft_rhash *priv;
325 struct rhashtable_iter hti;
326 struct nft_trans_gc *gc;
330 priv = container_of(work, struct nft_rhash, gc_work.work);
331 set = nft_set_container_of(priv);
332 net = read_pnet(&set->net);
333 nft_net = nft_pernet(net);
334 gc_seq = READ_ONCE(nft_net->gc_seq);
336 if (nft_set_gc_is_pending(set))
339 gc = nft_trans_gc_alloc(set, gc_seq, GFP_KERNEL);
343 rhashtable_walk_enter(&priv->ht, &hti);
344 rhashtable_walk_start(&hti);
346 while ((he = rhashtable_walk_next(&hti))) {
348 nft_trans_gc_destroy(gc);
353 /* Ruleset has been updated, try later. */
354 if (READ_ONCE(nft_net->gc_seq) != gc_seq) {
355 nft_trans_gc_destroy(gc);
360 if (nft_set_elem_is_dead(&he->ext))
363 if (nft_set_ext_exists(&he->ext, NFT_SET_EXT_EXPRESSIONS) &&
364 nft_rhash_expr_needs_gc_run(set, &he->ext))
367 if (!nft_set_elem_expired(&he->ext))
370 nft_set_elem_dead(&he->ext);
372 gc = nft_trans_gc_queue_async(gc, gc_seq, GFP_ATOMIC);
376 nft_trans_gc_elem_add(gc, he);
379 gc = nft_trans_gc_catchall_async(gc, gc_seq);
382 /* catchall list iteration requires rcu read side lock. */
383 rhashtable_walk_stop(&hti);
384 rhashtable_walk_exit(&hti);
387 nft_trans_gc_queue_async_done(gc);
390 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
391 nft_set_gc_interval(set));
394 static u64 nft_rhash_privsize(const struct nlattr * const nla[],
395 const struct nft_set_desc *desc)
397 return sizeof(struct nft_rhash);
400 static void nft_rhash_gc_init(const struct nft_set *set)
402 struct nft_rhash *priv = nft_set_priv(set);
404 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
405 nft_set_gc_interval(set));
408 static int nft_rhash_init(const struct nft_set *set,
409 const struct nft_set_desc *desc,
410 const struct nlattr * const tb[])
412 struct nft_rhash *priv = nft_set_priv(set);
413 struct rhashtable_params params = nft_rhash_params;
416 BUILD_BUG_ON(offsetof(struct nft_rhash_elem, priv) != 0);
418 params.nelem_hint = desc->size ?: NFT_RHASH_ELEMENT_HINT;
419 params.key_len = set->klen;
421 err = rhashtable_init(&priv->ht, ¶ms);
425 INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc);
426 if (set->flags & (NFT_SET_TIMEOUT | NFT_SET_EVAL))
427 nft_rhash_gc_init(set);
432 struct nft_rhash_ctx {
433 const struct nft_ctx ctx;
434 const struct nft_set *set;
437 static void nft_rhash_elem_destroy(void *ptr, void *arg)
439 struct nft_rhash_ctx *rhash_ctx = arg;
440 struct nft_rhash_elem *he = ptr;
442 nf_tables_set_elem_destroy(&rhash_ctx->ctx, rhash_ctx->set, &he->priv);
445 static void nft_rhash_destroy(const struct nft_ctx *ctx,
446 const struct nft_set *set)
448 struct nft_rhash *priv = nft_set_priv(set);
449 struct nft_rhash_ctx rhash_ctx = {
454 cancel_delayed_work_sync(&priv->gc_work);
455 rhashtable_free_and_destroy(&priv->ht, nft_rhash_elem_destroy,
459 /* Number of buckets is stored in u32, so cap our result to 1U<<31 */
460 #define NFT_MAX_BUCKETS (1U << 31)
462 static u32 nft_hash_buckets(u32 size)
464 u64 val = div_u64((u64)size * 4, 3);
466 if (val >= NFT_MAX_BUCKETS)
467 return NFT_MAX_BUCKETS;
469 return roundup_pow_of_two(val);
472 static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features,
473 struct nft_set_estimate *est)
476 est->lookup = NFT_SET_CLASS_O_1;
477 est->space = NFT_SET_CLASS_O_N;
485 struct hlist_head table[];
488 struct nft_hash_elem {
489 struct nft_elem_priv priv;
490 struct hlist_node node;
491 struct nft_set_ext ext;
494 INDIRECT_CALLABLE_SCOPE
495 bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
496 const u32 *key, const struct nft_set_ext **ext)
498 struct nft_hash *priv = nft_set_priv(set);
499 u8 genmask = nft_genmask_cur(net);
500 const struct nft_hash_elem *he;
503 hash = jhash(key, set->klen, priv->seed);
504 hash = reciprocal_scale(hash, priv->buckets);
505 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
506 if (!memcmp(nft_set_ext_key(&he->ext), key, set->klen) &&
507 nft_set_elem_active(&he->ext, genmask)) {
515 static struct nft_elem_priv *
516 nft_hash_get(const struct net *net, const struct nft_set *set,
517 const struct nft_set_elem *elem, unsigned int flags)
519 struct nft_hash *priv = nft_set_priv(set);
520 u8 genmask = nft_genmask_cur(net);
521 struct nft_hash_elem *he;
524 hash = jhash(elem->key.val.data, set->klen, priv->seed);
525 hash = reciprocal_scale(hash, priv->buckets);
526 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
527 if (!memcmp(nft_set_ext_key(&he->ext), elem->key.val.data, set->klen) &&
528 nft_set_elem_active(&he->ext, genmask))
531 return ERR_PTR(-ENOENT);
534 INDIRECT_CALLABLE_SCOPE
535 bool nft_hash_lookup_fast(const struct net *net,
536 const struct nft_set *set,
537 const u32 *key, const struct nft_set_ext **ext)
539 struct nft_hash *priv = nft_set_priv(set);
540 u8 genmask = nft_genmask_cur(net);
541 const struct nft_hash_elem *he;
545 hash = jhash_1word(k1, priv->seed);
546 hash = reciprocal_scale(hash, priv->buckets);
547 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
548 k2 = *(u32 *)nft_set_ext_key(&he->ext)->data;
550 nft_set_elem_active(&he->ext, genmask)) {
558 static u32 nft_jhash(const struct nft_set *set, const struct nft_hash *priv,
559 const struct nft_set_ext *ext)
561 const struct nft_data *key = nft_set_ext_key(ext);
564 if (set->klen == 4) {
566 hash = jhash_1word(k1, priv->seed);
568 hash = jhash(key, set->klen, priv->seed);
570 hash = reciprocal_scale(hash, priv->buckets);
575 static int nft_hash_insert(const struct net *net, const struct nft_set *set,
576 const struct nft_set_elem *elem,
577 struct nft_elem_priv **elem_priv)
579 struct nft_hash_elem *this = nft_elem_priv_cast(elem->priv), *he;
580 struct nft_hash *priv = nft_set_priv(set);
581 u8 genmask = nft_genmask_next(net);
584 hash = nft_jhash(set, priv, &this->ext);
585 hlist_for_each_entry(he, &priv->table[hash], node) {
586 if (!memcmp(nft_set_ext_key(&this->ext),
587 nft_set_ext_key(&he->ext), set->klen) &&
588 nft_set_elem_active(&he->ext, genmask)) {
589 *elem_priv = &he->priv;
593 hlist_add_head_rcu(&this->node, &priv->table[hash]);
597 static void nft_hash_activate(const struct net *net, const struct nft_set *set,
598 struct nft_elem_priv *elem_priv)
600 struct nft_hash_elem *he = nft_elem_priv_cast(elem_priv);
602 nft_set_elem_change_active(net, set, &he->ext);
605 static void nft_hash_flush(const struct net *net,
606 const struct nft_set *set,
607 struct nft_elem_priv *elem_priv)
609 struct nft_hash_elem *he = nft_elem_priv_cast(elem_priv);
611 nft_set_elem_change_active(net, set, &he->ext);
614 static struct nft_elem_priv *
615 nft_hash_deactivate(const struct net *net, const struct nft_set *set,
616 const struct nft_set_elem *elem)
618 struct nft_hash_elem *this = nft_elem_priv_cast(elem->priv), *he;
619 struct nft_hash *priv = nft_set_priv(set);
620 u8 genmask = nft_genmask_next(net);
623 hash = nft_jhash(set, priv, &this->ext);
624 hlist_for_each_entry(he, &priv->table[hash], node) {
625 if (!memcmp(nft_set_ext_key(&he->ext), &elem->key.val,
627 nft_set_elem_active(&he->ext, genmask)) {
628 nft_set_elem_change_active(net, set, &he->ext);
635 static void nft_hash_remove(const struct net *net,
636 const struct nft_set *set,
637 struct nft_elem_priv *elem_priv)
639 struct nft_hash_elem *he = nft_elem_priv_cast(elem_priv);
641 hlist_del_rcu(&he->node);
644 static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set,
645 struct nft_set_iter *iter)
647 struct nft_hash *priv = nft_set_priv(set);
648 struct nft_hash_elem *he;
651 for (i = 0; i < priv->buckets; i++) {
652 hlist_for_each_entry_rcu(he, &priv->table[i], node) {
653 if (iter->count < iter->skip)
655 if (!nft_set_elem_active(&he->ext, iter->genmask))
658 iter->err = iter->fn(ctx, set, iter, &he->priv);
667 static u64 nft_hash_privsize(const struct nlattr * const nla[],
668 const struct nft_set_desc *desc)
670 return sizeof(struct nft_hash) +
671 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head);
674 static int nft_hash_init(const struct nft_set *set,
675 const struct nft_set_desc *desc,
676 const struct nlattr * const tb[])
678 struct nft_hash *priv = nft_set_priv(set);
680 priv->buckets = nft_hash_buckets(desc->size);
681 get_random_bytes(&priv->seed, sizeof(priv->seed));
686 static void nft_hash_destroy(const struct nft_ctx *ctx,
687 const struct nft_set *set)
689 struct nft_hash *priv = nft_set_priv(set);
690 struct nft_hash_elem *he;
691 struct hlist_node *next;
694 for (i = 0; i < priv->buckets; i++) {
695 hlist_for_each_entry_safe(he, next, &priv->table[i], node) {
696 hlist_del_rcu(&he->node);
697 nf_tables_set_elem_destroy(ctx, set, &he->priv);
702 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
703 struct nft_set_estimate *est)
711 est->size = sizeof(struct nft_hash) +
712 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
713 (u64)desc->size * sizeof(struct nft_hash_elem);
714 est->lookup = NFT_SET_CLASS_O_1;
715 est->space = NFT_SET_CLASS_O_N;
720 static bool nft_hash_fast_estimate(const struct nft_set_desc *desc, u32 features,
721 struct nft_set_estimate *est)
729 est->size = sizeof(struct nft_hash) +
730 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
731 (u64)desc->size * sizeof(struct nft_hash_elem);
732 est->lookup = NFT_SET_CLASS_O_1;
733 est->space = NFT_SET_CLASS_O_N;
738 const struct nft_set_type nft_set_rhash_type = {
739 .features = NFT_SET_MAP | NFT_SET_OBJECT |
740 NFT_SET_TIMEOUT | NFT_SET_EVAL,
742 .privsize = nft_rhash_privsize,
743 .elemsize = offsetof(struct nft_rhash_elem, ext),
744 .estimate = nft_rhash_estimate,
745 .init = nft_rhash_init,
746 .gc_init = nft_rhash_gc_init,
747 .destroy = nft_rhash_destroy,
748 .insert = nft_rhash_insert,
749 .activate = nft_rhash_activate,
750 .deactivate = nft_rhash_deactivate,
751 .flush = nft_rhash_flush,
752 .remove = nft_rhash_remove,
753 .lookup = nft_rhash_lookup,
754 .update = nft_rhash_update,
755 .delete = nft_rhash_delete,
756 .walk = nft_rhash_walk,
757 .get = nft_rhash_get,
761 const struct nft_set_type nft_set_hash_type = {
762 .features = NFT_SET_MAP | NFT_SET_OBJECT,
764 .privsize = nft_hash_privsize,
765 .elemsize = offsetof(struct nft_hash_elem, ext),
766 .estimate = nft_hash_estimate,
767 .init = nft_hash_init,
768 .destroy = nft_hash_destroy,
769 .insert = nft_hash_insert,
770 .activate = nft_hash_activate,
771 .deactivate = nft_hash_deactivate,
772 .flush = nft_hash_flush,
773 .remove = nft_hash_remove,
774 .lookup = nft_hash_lookup,
775 .walk = nft_hash_walk,
780 const struct nft_set_type nft_set_hash_fast_type = {
781 .features = NFT_SET_MAP | NFT_SET_OBJECT,
783 .privsize = nft_hash_privsize,
784 .elemsize = offsetof(struct nft_hash_elem, ext),
785 .estimate = nft_hash_fast_estimate,
786 .init = nft_hash_init,
787 .destroy = nft_hash_destroy,
788 .insert = nft_hash_insert,
789 .activate = nft_hash_activate,
790 .deactivate = nft_hash_deactivate,
791 .flush = nft_hash_flush,
792 .remove = nft_hash_remove,
793 .lookup = nft_hash_lookup_fast,
794 .walk = nft_hash_walk,