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_expired(&he->ext))
64 if (!nft_set_elem_active(&he->ext, x->genmask))
69 static const struct rhashtable_params nft_rhash_params = {
70 .head_offset = offsetof(struct nft_rhash_elem, node),
71 .hashfn = nft_rhash_key,
72 .obj_hashfn = nft_rhash_obj,
73 .obj_cmpfn = nft_rhash_cmp,
74 .automatic_shrinking = true,
77 INDIRECT_CALLABLE_SCOPE
78 bool nft_rhash_lookup(const struct net *net, const struct nft_set *set,
79 const u32 *key, const struct nft_set_ext **ext)
81 struct nft_rhash *priv = nft_set_priv(set);
82 const struct nft_rhash_elem *he;
83 struct nft_rhash_cmp_arg arg = {
84 .genmask = nft_genmask_cur(net),
89 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
96 static void *nft_rhash_get(const struct net *net, const struct nft_set *set,
97 const struct nft_set_elem *elem, unsigned int flags)
99 struct nft_rhash *priv = nft_set_priv(set);
100 struct nft_rhash_elem *he;
101 struct nft_rhash_cmp_arg arg = {
102 .genmask = nft_genmask_cur(net),
104 .key = elem->key.val.data,
107 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
111 return ERR_PTR(-ENOENT);
114 static bool nft_rhash_update(struct nft_set *set, const u32 *key,
115 void *(*new)(struct nft_set *,
116 const struct nft_expr *,
117 struct nft_regs *regs),
118 const struct nft_expr *expr,
119 struct nft_regs *regs,
120 const struct nft_set_ext **ext)
122 struct nft_rhash *priv = nft_set_priv(set);
123 struct nft_rhash_elem *he, *prev;
124 struct nft_rhash_cmp_arg arg = {
125 .genmask = NFT_GENMASK_ANY,
130 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
134 he = new(set, expr, regs);
138 prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
143 /* Another cpu may race to insert the element with the same key */
145 nft_set_elem_destroy(set, he, true);
146 atomic_dec(&set->nelems);
155 nft_set_elem_destroy(set, he, true);
156 atomic_dec(&set->nelems);
161 static int nft_rhash_insert(const struct net *net, const struct nft_set *set,
162 const struct nft_set_elem *elem,
163 struct nft_set_ext **ext)
165 struct nft_rhash *priv = nft_set_priv(set);
166 struct nft_rhash_elem *he = elem->priv;
167 struct nft_rhash_cmp_arg arg = {
168 .genmask = nft_genmask_next(net),
170 .key = elem->key.val.data,
172 struct nft_rhash_elem *prev;
174 prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
177 return PTR_ERR(prev);
185 static void nft_rhash_activate(const struct net *net, const struct nft_set *set,
186 const struct nft_set_elem *elem)
188 struct nft_rhash_elem *he = elem->priv;
190 nft_set_elem_change_active(net, set, &he->ext);
191 nft_set_elem_clear_busy(&he->ext);
194 static bool nft_rhash_flush(const struct net *net,
195 const struct nft_set *set, void *priv)
197 struct nft_rhash_elem *he = priv;
199 if (!nft_set_elem_mark_busy(&he->ext) ||
200 !nft_is_active(net, &he->ext)) {
201 nft_set_elem_change_active(net, set, &he->ext);
207 static void *nft_rhash_deactivate(const struct net *net,
208 const struct nft_set *set,
209 const struct nft_set_elem *elem)
211 struct nft_rhash *priv = nft_set_priv(set);
212 struct nft_rhash_elem *he;
213 struct nft_rhash_cmp_arg arg = {
214 .genmask = nft_genmask_next(net),
216 .key = elem->key.val.data,
220 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
222 !nft_rhash_flush(net, set, he))
230 static void nft_rhash_remove(const struct net *net,
231 const struct nft_set *set,
232 const struct nft_set_elem *elem)
234 struct nft_rhash *priv = nft_set_priv(set);
235 struct nft_rhash_elem *he = elem->priv;
237 rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
240 static bool nft_rhash_delete(const struct nft_set *set,
243 struct nft_rhash *priv = nft_set_priv(set);
244 struct nft_rhash_cmp_arg arg = {
245 .genmask = NFT_GENMASK_ANY,
249 struct nft_rhash_elem *he;
251 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
255 return rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params) == 0;
258 static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
259 struct nft_set_iter *iter)
261 struct nft_rhash *priv = nft_set_priv(set);
262 struct nft_rhash_elem *he;
263 struct rhashtable_iter hti;
264 struct nft_set_elem elem;
266 rhashtable_walk_enter(&priv->ht, &hti);
267 rhashtable_walk_start(&hti);
269 while ((he = rhashtable_walk_next(&hti))) {
271 if (PTR_ERR(he) != -EAGAIN) {
272 iter->err = PTR_ERR(he);
279 if (iter->count < iter->skip)
281 if (nft_set_elem_expired(&he->ext))
283 if (!nft_set_elem_active(&he->ext, iter->genmask))
288 iter->err = iter->fn(ctx, set, iter, &elem);
295 rhashtable_walk_stop(&hti);
296 rhashtable_walk_exit(&hti);
299 static bool nft_rhash_expr_needs_gc_run(const struct nft_set *set,
300 struct nft_set_ext *ext)
302 struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext);
303 struct nft_expr *expr;
306 nft_setelem_expr_foreach(expr, elem_expr, size) {
308 expr->ops->gc(read_pnet(&set->net), expr))
315 static void nft_rhash_gc(struct work_struct *work)
318 struct nft_rhash_elem *he;
319 struct nft_rhash *priv;
320 struct nft_set_gc_batch *gcb = NULL;
321 struct rhashtable_iter hti;
323 priv = container_of(work, struct nft_rhash, gc_work.work);
324 set = nft_set_container_of(priv);
326 rhashtable_walk_enter(&priv->ht, &hti);
327 rhashtable_walk_start(&hti);
329 while ((he = rhashtable_walk_next(&hti))) {
331 if (PTR_ERR(he) != -EAGAIN)
336 if (nft_set_ext_exists(&he->ext, NFT_SET_EXT_EXPRESSIONS) &&
337 nft_rhash_expr_needs_gc_run(set, &he->ext))
340 if (!nft_set_elem_expired(&he->ext))
343 if (nft_set_elem_mark_busy(&he->ext))
346 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
349 rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
350 atomic_dec(&set->nelems);
351 nft_set_gc_batch_add(gcb, he);
353 rhashtable_walk_stop(&hti);
354 rhashtable_walk_exit(&hti);
356 he = nft_set_catchall_gc(set);
358 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
360 nft_set_gc_batch_add(gcb, he);
362 nft_set_gc_batch_complete(gcb);
363 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
364 nft_set_gc_interval(set));
367 static u64 nft_rhash_privsize(const struct nlattr * const nla[],
368 const struct nft_set_desc *desc)
370 return sizeof(struct nft_rhash);
373 static void nft_rhash_gc_init(const struct nft_set *set)
375 struct nft_rhash *priv = nft_set_priv(set);
377 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
378 nft_set_gc_interval(set));
381 static int nft_rhash_init(const struct nft_set *set,
382 const struct nft_set_desc *desc,
383 const struct nlattr * const tb[])
385 struct nft_rhash *priv = nft_set_priv(set);
386 struct rhashtable_params params = nft_rhash_params;
389 params.nelem_hint = desc->size ?: NFT_RHASH_ELEMENT_HINT;
390 params.key_len = set->klen;
392 err = rhashtable_init(&priv->ht, ¶ms);
396 INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc);
397 if (set->flags & NFT_SET_TIMEOUT)
398 nft_rhash_gc_init(set);
403 struct nft_rhash_ctx {
404 const struct nft_ctx ctx;
405 const struct nft_set *set;
408 static void nft_rhash_elem_destroy(void *ptr, void *arg)
410 struct nft_rhash_ctx *rhash_ctx = arg;
412 nf_tables_set_elem_destroy(&rhash_ctx->ctx, rhash_ctx->set, ptr);
415 static void nft_rhash_destroy(const struct nft_ctx *ctx,
416 const struct nft_set *set)
418 struct nft_rhash *priv = nft_set_priv(set);
419 struct nft_rhash_ctx rhash_ctx = {
424 cancel_delayed_work_sync(&priv->gc_work);
426 rhashtable_free_and_destroy(&priv->ht, nft_rhash_elem_destroy,
430 /* Number of buckets is stored in u32, so cap our result to 1U<<31 */
431 #define NFT_MAX_BUCKETS (1U << 31)
433 static u32 nft_hash_buckets(u32 size)
435 u64 val = div_u64((u64)size * 4, 3);
437 if (val >= NFT_MAX_BUCKETS)
438 return NFT_MAX_BUCKETS;
440 return roundup_pow_of_two(val);
443 static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features,
444 struct nft_set_estimate *est)
447 est->lookup = NFT_SET_CLASS_O_1;
448 est->space = NFT_SET_CLASS_O_N;
456 struct hlist_head table[];
459 struct nft_hash_elem {
460 struct hlist_node node;
461 struct nft_set_ext ext;
464 INDIRECT_CALLABLE_SCOPE
465 bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
466 const u32 *key, const struct nft_set_ext **ext)
468 struct nft_hash *priv = nft_set_priv(set);
469 u8 genmask = nft_genmask_cur(net);
470 const struct nft_hash_elem *he;
473 hash = jhash(key, set->klen, priv->seed);
474 hash = reciprocal_scale(hash, priv->buckets);
475 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
476 if (!memcmp(nft_set_ext_key(&he->ext), key, set->klen) &&
477 nft_set_elem_active(&he->ext, genmask)) {
485 static void *nft_hash_get(const struct net *net, const struct nft_set *set,
486 const struct nft_set_elem *elem, unsigned int flags)
488 struct nft_hash *priv = nft_set_priv(set);
489 u8 genmask = nft_genmask_cur(net);
490 struct nft_hash_elem *he;
493 hash = jhash(elem->key.val.data, set->klen, priv->seed);
494 hash = reciprocal_scale(hash, priv->buckets);
495 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
496 if (!memcmp(nft_set_ext_key(&he->ext), elem->key.val.data, set->klen) &&
497 nft_set_elem_active(&he->ext, genmask))
500 return ERR_PTR(-ENOENT);
503 INDIRECT_CALLABLE_SCOPE
504 bool nft_hash_lookup_fast(const struct net *net,
505 const struct nft_set *set,
506 const u32 *key, const struct nft_set_ext **ext)
508 struct nft_hash *priv = nft_set_priv(set);
509 u8 genmask = nft_genmask_cur(net);
510 const struct nft_hash_elem *he;
514 hash = jhash_1word(k1, priv->seed);
515 hash = reciprocal_scale(hash, priv->buckets);
516 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
517 k2 = *(u32 *)nft_set_ext_key(&he->ext)->data;
519 nft_set_elem_active(&he->ext, genmask)) {
527 static u32 nft_jhash(const struct nft_set *set, const struct nft_hash *priv,
528 const struct nft_set_ext *ext)
530 const struct nft_data *key = nft_set_ext_key(ext);
533 if (set->klen == 4) {
535 hash = jhash_1word(k1, priv->seed);
537 hash = jhash(key, set->klen, priv->seed);
539 hash = reciprocal_scale(hash, priv->buckets);
544 static int nft_hash_insert(const struct net *net, const struct nft_set *set,
545 const struct nft_set_elem *elem,
546 struct nft_set_ext **ext)
548 struct nft_hash_elem *this = elem->priv, *he;
549 struct nft_hash *priv = nft_set_priv(set);
550 u8 genmask = nft_genmask_next(net);
553 hash = nft_jhash(set, priv, &this->ext);
554 hlist_for_each_entry(he, &priv->table[hash], node) {
555 if (!memcmp(nft_set_ext_key(&this->ext),
556 nft_set_ext_key(&he->ext), set->klen) &&
557 nft_set_elem_active(&he->ext, genmask)) {
562 hlist_add_head_rcu(&this->node, &priv->table[hash]);
566 static void nft_hash_activate(const struct net *net, const struct nft_set *set,
567 const struct nft_set_elem *elem)
569 struct nft_hash_elem *he = elem->priv;
571 nft_set_elem_change_active(net, set, &he->ext);
574 static bool nft_hash_flush(const struct net *net,
575 const struct nft_set *set, void *priv)
577 struct nft_hash_elem *he = priv;
579 nft_set_elem_change_active(net, set, &he->ext);
583 static void *nft_hash_deactivate(const struct net *net,
584 const struct nft_set *set,
585 const struct nft_set_elem *elem)
587 struct nft_hash *priv = nft_set_priv(set);
588 struct nft_hash_elem *this = elem->priv, *he;
589 u8 genmask = nft_genmask_next(net);
592 hash = nft_jhash(set, priv, &this->ext);
593 hlist_for_each_entry(he, &priv->table[hash], node) {
594 if (!memcmp(nft_set_ext_key(&he->ext), &elem->key.val,
596 nft_set_elem_active(&he->ext, genmask)) {
597 nft_set_elem_change_active(net, set, &he->ext);
604 static void nft_hash_remove(const struct net *net,
605 const struct nft_set *set,
606 const struct nft_set_elem *elem)
608 struct nft_hash_elem *he = elem->priv;
610 hlist_del_rcu(&he->node);
613 static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set,
614 struct nft_set_iter *iter)
616 struct nft_hash *priv = nft_set_priv(set);
617 struct nft_hash_elem *he;
618 struct nft_set_elem elem;
621 for (i = 0; i < priv->buckets; i++) {
622 hlist_for_each_entry_rcu(he, &priv->table[i], node) {
623 if (iter->count < iter->skip)
625 if (!nft_set_elem_active(&he->ext, iter->genmask))
630 iter->err = iter->fn(ctx, set, iter, &elem);
639 static u64 nft_hash_privsize(const struct nlattr * const nla[],
640 const struct nft_set_desc *desc)
642 return sizeof(struct nft_hash) +
643 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head);
646 static int nft_hash_init(const struct nft_set *set,
647 const struct nft_set_desc *desc,
648 const struct nlattr * const tb[])
650 struct nft_hash *priv = nft_set_priv(set);
652 priv->buckets = nft_hash_buckets(desc->size);
653 get_random_bytes(&priv->seed, sizeof(priv->seed));
658 static void nft_hash_destroy(const struct nft_ctx *ctx,
659 const struct nft_set *set)
661 struct nft_hash *priv = nft_set_priv(set);
662 struct nft_hash_elem *he;
663 struct hlist_node *next;
666 for (i = 0; i < priv->buckets; i++) {
667 hlist_for_each_entry_safe(he, next, &priv->table[i], node) {
668 hlist_del_rcu(&he->node);
669 nf_tables_set_elem_destroy(ctx, set, he);
674 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
675 struct nft_set_estimate *est)
683 est->size = sizeof(struct nft_hash) +
684 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
685 (u64)desc->size * sizeof(struct nft_hash_elem);
686 est->lookup = NFT_SET_CLASS_O_1;
687 est->space = NFT_SET_CLASS_O_N;
692 static bool nft_hash_fast_estimate(const struct nft_set_desc *desc, u32 features,
693 struct nft_set_estimate *est)
701 est->size = sizeof(struct nft_hash) +
702 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
703 (u64)desc->size * sizeof(struct nft_hash_elem);
704 est->lookup = NFT_SET_CLASS_O_1;
705 est->space = NFT_SET_CLASS_O_N;
710 const struct nft_set_type nft_set_rhash_type = {
711 .features = NFT_SET_MAP | NFT_SET_OBJECT |
712 NFT_SET_TIMEOUT | NFT_SET_EVAL,
714 .privsize = nft_rhash_privsize,
715 .elemsize = offsetof(struct nft_rhash_elem, ext),
716 .estimate = nft_rhash_estimate,
717 .init = nft_rhash_init,
718 .gc_init = nft_rhash_gc_init,
719 .destroy = nft_rhash_destroy,
720 .insert = nft_rhash_insert,
721 .activate = nft_rhash_activate,
722 .deactivate = nft_rhash_deactivate,
723 .flush = nft_rhash_flush,
724 .remove = nft_rhash_remove,
725 .lookup = nft_rhash_lookup,
726 .update = nft_rhash_update,
727 .delete = nft_rhash_delete,
728 .walk = nft_rhash_walk,
729 .get = nft_rhash_get,
733 const struct nft_set_type nft_set_hash_type = {
734 .features = NFT_SET_MAP | NFT_SET_OBJECT,
736 .privsize = nft_hash_privsize,
737 .elemsize = offsetof(struct nft_hash_elem, ext),
738 .estimate = nft_hash_estimate,
739 .init = nft_hash_init,
740 .destroy = nft_hash_destroy,
741 .insert = nft_hash_insert,
742 .activate = nft_hash_activate,
743 .deactivate = nft_hash_deactivate,
744 .flush = nft_hash_flush,
745 .remove = nft_hash_remove,
746 .lookup = nft_hash_lookup,
747 .walk = nft_hash_walk,
752 const struct nft_set_type nft_set_hash_fast_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_fast_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_fast,
766 .walk = nft_hash_walk,