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_clear(net, &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)
290 iter->err = iter->fn(ctx, set, iter, &he->priv);
297 rhashtable_walk_stop(&hti);
298 rhashtable_walk_exit(&hti);
301 static bool nft_rhash_expr_needs_gc_run(const struct nft_set *set,
302 struct nft_set_ext *ext)
304 struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext);
305 struct nft_expr *expr;
308 nft_setelem_expr_foreach(expr, elem_expr, size) {
310 expr->ops->gc(read_pnet(&set->net), expr))
317 static void nft_rhash_gc(struct work_struct *work)
319 struct nftables_pernet *nft_net;
321 struct nft_rhash_elem *he;
322 struct nft_rhash *priv;
323 struct rhashtable_iter hti;
324 struct nft_trans_gc *gc;
328 priv = container_of(work, struct nft_rhash, gc_work.work);
329 set = nft_set_container_of(priv);
330 net = read_pnet(&set->net);
331 nft_net = nft_pernet(net);
332 gc_seq = READ_ONCE(nft_net->gc_seq);
334 if (nft_set_gc_is_pending(set))
337 gc = nft_trans_gc_alloc(set, gc_seq, GFP_KERNEL);
341 rhashtable_walk_enter(&priv->ht, &hti);
342 rhashtable_walk_start(&hti);
344 while ((he = rhashtable_walk_next(&hti))) {
346 nft_trans_gc_destroy(gc);
351 /* Ruleset has been updated, try later. */
352 if (READ_ONCE(nft_net->gc_seq) != gc_seq) {
353 nft_trans_gc_destroy(gc);
358 if (nft_set_elem_is_dead(&he->ext))
361 if (nft_set_ext_exists(&he->ext, NFT_SET_EXT_EXPRESSIONS) &&
362 nft_rhash_expr_needs_gc_run(set, &he->ext))
365 if (!nft_set_elem_expired(&he->ext))
368 nft_set_elem_dead(&he->ext);
370 gc = nft_trans_gc_queue_async(gc, gc_seq, GFP_ATOMIC);
374 nft_trans_gc_elem_add(gc, he);
377 gc = nft_trans_gc_catchall_async(gc, gc_seq);
380 /* catchall list iteration requires rcu read side lock. */
381 rhashtable_walk_stop(&hti);
382 rhashtable_walk_exit(&hti);
385 nft_trans_gc_queue_async_done(gc);
388 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
389 nft_set_gc_interval(set));
392 static u64 nft_rhash_privsize(const struct nlattr * const nla[],
393 const struct nft_set_desc *desc)
395 return sizeof(struct nft_rhash);
398 static void nft_rhash_gc_init(const struct nft_set *set)
400 struct nft_rhash *priv = nft_set_priv(set);
402 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
403 nft_set_gc_interval(set));
406 static int nft_rhash_init(const struct nft_set *set,
407 const struct nft_set_desc *desc,
408 const struct nlattr * const tb[])
410 struct nft_rhash *priv = nft_set_priv(set);
411 struct rhashtable_params params = nft_rhash_params;
414 BUILD_BUG_ON(offsetof(struct nft_rhash_elem, priv) != 0);
416 params.nelem_hint = desc->size ?: NFT_RHASH_ELEMENT_HINT;
417 params.key_len = set->klen;
419 err = rhashtable_init(&priv->ht, ¶ms);
423 INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc);
424 if (set->flags & (NFT_SET_TIMEOUT | NFT_SET_EVAL))
425 nft_rhash_gc_init(set);
430 struct nft_rhash_ctx {
431 const struct nft_ctx ctx;
432 const struct nft_set *set;
435 static void nft_rhash_elem_destroy(void *ptr, void *arg)
437 struct nft_rhash_ctx *rhash_ctx = arg;
438 struct nft_rhash_elem *he = ptr;
440 nf_tables_set_elem_destroy(&rhash_ctx->ctx, rhash_ctx->set, &he->priv);
443 static void nft_rhash_destroy(const struct nft_ctx *ctx,
444 const struct nft_set *set)
446 struct nft_rhash *priv = nft_set_priv(set);
447 struct nft_rhash_ctx rhash_ctx = {
452 cancel_delayed_work_sync(&priv->gc_work);
453 rhashtable_free_and_destroy(&priv->ht, nft_rhash_elem_destroy,
457 /* Number of buckets is stored in u32, so cap our result to 1U<<31 */
458 #define NFT_MAX_BUCKETS (1U << 31)
460 static u32 nft_hash_buckets(u32 size)
462 u64 val = div_u64((u64)size * 4, 3);
464 if (val >= NFT_MAX_BUCKETS)
465 return NFT_MAX_BUCKETS;
467 return roundup_pow_of_two(val);
470 static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features,
471 struct nft_set_estimate *est)
474 est->lookup = NFT_SET_CLASS_O_1;
475 est->space = NFT_SET_CLASS_O_N;
483 struct hlist_head table[];
486 struct nft_hash_elem {
487 struct nft_elem_priv priv;
488 struct hlist_node node;
489 struct nft_set_ext ext;
492 INDIRECT_CALLABLE_SCOPE
493 bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
494 const u32 *key, const struct nft_set_ext **ext)
496 struct nft_hash *priv = nft_set_priv(set);
497 u8 genmask = nft_genmask_cur(net);
498 const struct nft_hash_elem *he;
501 hash = jhash(key, set->klen, priv->seed);
502 hash = reciprocal_scale(hash, priv->buckets);
503 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
504 if (!memcmp(nft_set_ext_key(&he->ext), key, set->klen) &&
505 nft_set_elem_active(&he->ext, genmask)) {
513 static struct nft_elem_priv *
514 nft_hash_get(const struct net *net, const struct nft_set *set,
515 const struct nft_set_elem *elem, unsigned int flags)
517 struct nft_hash *priv = nft_set_priv(set);
518 u8 genmask = nft_genmask_cur(net);
519 struct nft_hash_elem *he;
522 hash = jhash(elem->key.val.data, set->klen, priv->seed);
523 hash = reciprocal_scale(hash, priv->buckets);
524 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
525 if (!memcmp(nft_set_ext_key(&he->ext), elem->key.val.data, set->klen) &&
526 nft_set_elem_active(&he->ext, genmask))
529 return ERR_PTR(-ENOENT);
532 INDIRECT_CALLABLE_SCOPE
533 bool nft_hash_lookup_fast(const struct net *net,
534 const struct nft_set *set,
535 const u32 *key, const struct nft_set_ext **ext)
537 struct nft_hash *priv = nft_set_priv(set);
538 u8 genmask = nft_genmask_cur(net);
539 const struct nft_hash_elem *he;
543 hash = jhash_1word(k1, priv->seed);
544 hash = reciprocal_scale(hash, priv->buckets);
545 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
546 k2 = *(u32 *)nft_set_ext_key(&he->ext)->data;
548 nft_set_elem_active(&he->ext, genmask)) {
556 static u32 nft_jhash(const struct nft_set *set, const struct nft_hash *priv,
557 const struct nft_set_ext *ext)
559 const struct nft_data *key = nft_set_ext_key(ext);
562 if (set->klen == 4) {
564 hash = jhash_1word(k1, priv->seed);
566 hash = jhash(key, set->klen, priv->seed);
568 hash = reciprocal_scale(hash, priv->buckets);
573 static int nft_hash_insert(const struct net *net, const struct nft_set *set,
574 const struct nft_set_elem *elem,
575 struct nft_elem_priv **elem_priv)
577 struct nft_hash_elem *this = nft_elem_priv_cast(elem->priv), *he;
578 struct nft_hash *priv = nft_set_priv(set);
579 u8 genmask = nft_genmask_next(net);
582 hash = nft_jhash(set, priv, &this->ext);
583 hlist_for_each_entry(he, &priv->table[hash], node) {
584 if (!memcmp(nft_set_ext_key(&this->ext),
585 nft_set_ext_key(&he->ext), set->klen) &&
586 nft_set_elem_active(&he->ext, genmask)) {
587 *elem_priv = &he->priv;
591 hlist_add_head_rcu(&this->node, &priv->table[hash]);
595 static void nft_hash_activate(const struct net *net, const struct nft_set *set,
596 struct nft_elem_priv *elem_priv)
598 struct nft_hash_elem *he = nft_elem_priv_cast(elem_priv);
600 nft_clear(net, &he->ext);
603 static void nft_hash_flush(const struct net *net,
604 const struct nft_set *set,
605 struct nft_elem_priv *elem_priv)
607 struct nft_hash_elem *he = nft_elem_priv_cast(elem_priv);
609 nft_set_elem_change_active(net, set, &he->ext);
612 static struct nft_elem_priv *
613 nft_hash_deactivate(const struct net *net, const struct nft_set *set,
614 const struct nft_set_elem *elem)
616 struct nft_hash_elem *this = nft_elem_priv_cast(elem->priv), *he;
617 struct nft_hash *priv = nft_set_priv(set);
618 u8 genmask = nft_genmask_next(net);
621 hash = nft_jhash(set, priv, &this->ext);
622 hlist_for_each_entry(he, &priv->table[hash], node) {
623 if (!memcmp(nft_set_ext_key(&he->ext), &elem->key.val,
625 nft_set_elem_active(&he->ext, genmask)) {
626 nft_set_elem_change_active(net, set, &he->ext);
633 static void nft_hash_remove(const struct net *net,
634 const struct nft_set *set,
635 struct nft_elem_priv *elem_priv)
637 struct nft_hash_elem *he = nft_elem_priv_cast(elem_priv);
639 hlist_del_rcu(&he->node);
642 static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set,
643 struct nft_set_iter *iter)
645 struct nft_hash *priv = nft_set_priv(set);
646 struct nft_hash_elem *he;
649 for (i = 0; i < priv->buckets; i++) {
650 hlist_for_each_entry_rcu(he, &priv->table[i], node) {
651 if (iter->count < iter->skip)
654 iter->err = iter->fn(ctx, set, iter, &he->priv);
663 static u64 nft_hash_privsize(const struct nlattr * const nla[],
664 const struct nft_set_desc *desc)
666 return sizeof(struct nft_hash) +
667 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head);
670 static int nft_hash_init(const struct nft_set *set,
671 const struct nft_set_desc *desc,
672 const struct nlattr * const tb[])
674 struct nft_hash *priv = nft_set_priv(set);
676 priv->buckets = nft_hash_buckets(desc->size);
677 get_random_bytes(&priv->seed, sizeof(priv->seed));
682 static void nft_hash_destroy(const struct nft_ctx *ctx,
683 const struct nft_set *set)
685 struct nft_hash *priv = nft_set_priv(set);
686 struct nft_hash_elem *he;
687 struct hlist_node *next;
690 for (i = 0; i < priv->buckets; i++) {
691 hlist_for_each_entry_safe(he, next, &priv->table[i], node) {
692 hlist_del_rcu(&he->node);
693 nf_tables_set_elem_destroy(ctx, set, &he->priv);
698 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
699 struct nft_set_estimate *est)
707 est->size = sizeof(struct nft_hash) +
708 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
709 (u64)desc->size * sizeof(struct nft_hash_elem);
710 est->lookup = NFT_SET_CLASS_O_1;
711 est->space = NFT_SET_CLASS_O_N;
716 static bool nft_hash_fast_estimate(const struct nft_set_desc *desc, u32 features,
717 struct nft_set_estimate *est)
725 est->size = sizeof(struct nft_hash) +
726 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
727 (u64)desc->size * sizeof(struct nft_hash_elem);
728 est->lookup = NFT_SET_CLASS_O_1;
729 est->space = NFT_SET_CLASS_O_N;
734 const struct nft_set_type nft_set_rhash_type = {
735 .features = NFT_SET_MAP | NFT_SET_OBJECT |
736 NFT_SET_TIMEOUT | NFT_SET_EVAL,
738 .privsize = nft_rhash_privsize,
739 .elemsize = offsetof(struct nft_rhash_elem, ext),
740 .estimate = nft_rhash_estimate,
741 .init = nft_rhash_init,
742 .gc_init = nft_rhash_gc_init,
743 .destroy = nft_rhash_destroy,
744 .insert = nft_rhash_insert,
745 .activate = nft_rhash_activate,
746 .deactivate = nft_rhash_deactivate,
747 .flush = nft_rhash_flush,
748 .remove = nft_rhash_remove,
749 .lookup = nft_rhash_lookup,
750 .update = nft_rhash_update,
751 .delete = nft_rhash_delete,
752 .walk = nft_rhash_walk,
753 .get = nft_rhash_get,
757 const struct nft_set_type nft_set_hash_type = {
758 .features = NFT_SET_MAP | NFT_SET_OBJECT,
760 .privsize = nft_hash_privsize,
761 .elemsize = offsetof(struct nft_hash_elem, ext),
762 .estimate = nft_hash_estimate,
763 .init = nft_hash_init,
764 .destroy = nft_hash_destroy,
765 .insert = nft_hash_insert,
766 .activate = nft_hash_activate,
767 .deactivate = nft_hash_deactivate,
768 .flush = nft_hash_flush,
769 .remove = nft_hash_remove,
770 .lookup = nft_hash_lookup,
771 .walk = nft_hash_walk,
776 const struct nft_set_type nft_set_hash_fast_type = {
777 .features = NFT_SET_MAP | NFT_SET_OBJECT,
779 .privsize = nft_hash_privsize,
780 .elemsize = offsetof(struct nft_hash_elem, ext),
781 .estimate = nft_hash_fast_estimate,
782 .init = nft_hash_init,
783 .destroy = nft_hash_destroy,
784 .insert = nft_hash_insert,
785 .activate = nft_hash_activate,
786 .deactivate = nft_hash_deactivate,
787 .flush = nft_hash_flush,
788 .remove = nft_hash_remove,
789 .lookup = nft_hash_lookup_fast,
790 .walk = nft_hash_walk,