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);
154 nft_set_elem_destroy(set, he, true);
159 static int nft_rhash_insert(const struct net *net, const struct nft_set *set,
160 const struct nft_set_elem *elem,
161 struct nft_set_ext **ext)
163 struct nft_rhash *priv = nft_set_priv(set);
164 struct nft_rhash_elem *he = elem->priv;
165 struct nft_rhash_cmp_arg arg = {
166 .genmask = nft_genmask_next(net),
168 .key = elem->key.val.data,
170 struct nft_rhash_elem *prev;
172 prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
175 return PTR_ERR(prev);
183 static void nft_rhash_activate(const struct net *net, const struct nft_set *set,
184 const struct nft_set_elem *elem)
186 struct nft_rhash_elem *he = elem->priv;
188 nft_set_elem_change_active(net, set, &he->ext);
189 nft_set_elem_clear_busy(&he->ext);
192 static bool nft_rhash_flush(const struct net *net,
193 const struct nft_set *set, void *priv)
195 struct nft_rhash_elem *he = priv;
197 if (!nft_set_elem_mark_busy(&he->ext) ||
198 !nft_is_active(net, &he->ext)) {
199 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_rhash_flush(net, set, he))
228 static void nft_rhash_remove(const struct net *net,
229 const struct nft_set *set,
230 const struct nft_set_elem *elem)
232 struct nft_rhash *priv = nft_set_priv(set);
233 struct nft_rhash_elem *he = elem->priv;
235 rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
238 static bool nft_rhash_delete(const struct nft_set *set,
241 struct nft_rhash *priv = nft_set_priv(set);
242 struct nft_rhash_cmp_arg arg = {
243 .genmask = NFT_GENMASK_ANY,
247 struct nft_rhash_elem *he;
249 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
253 return rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params) == 0;
256 static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
257 struct nft_set_iter *iter)
259 struct nft_rhash *priv = nft_set_priv(set);
260 struct nft_rhash_elem *he;
261 struct rhashtable_iter hti;
262 struct nft_set_elem elem;
264 rhashtable_walk_enter(&priv->ht, &hti);
265 rhashtable_walk_start(&hti);
267 while ((he = rhashtable_walk_next(&hti))) {
269 if (PTR_ERR(he) != -EAGAIN) {
270 iter->err = PTR_ERR(he);
277 if (iter->count < iter->skip)
279 if (nft_set_elem_expired(&he->ext))
281 if (!nft_set_elem_active(&he->ext, iter->genmask))
286 iter->err = iter->fn(ctx, set, iter, &elem);
293 rhashtable_walk_stop(&hti);
294 rhashtable_walk_exit(&hti);
297 static bool nft_rhash_expr_needs_gc_run(const struct nft_set *set,
298 struct nft_set_ext *ext)
300 struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext);
301 struct nft_expr *expr;
304 nft_setelem_expr_foreach(expr, elem_expr, size) {
306 expr->ops->gc(read_pnet(&set->net), expr))
313 static void nft_rhash_gc(struct work_struct *work)
316 struct nft_rhash_elem *he;
317 struct nft_rhash *priv;
318 struct nft_set_gc_batch *gcb = NULL;
319 struct rhashtable_iter hti;
321 priv = container_of(work, struct nft_rhash, gc_work.work);
322 set = nft_set_container_of(priv);
324 rhashtable_walk_enter(&priv->ht, &hti);
325 rhashtable_walk_start(&hti);
327 while ((he = rhashtable_walk_next(&hti))) {
329 if (PTR_ERR(he) != -EAGAIN)
334 if (nft_set_ext_exists(&he->ext, NFT_SET_EXT_EXPRESSIONS) &&
335 nft_rhash_expr_needs_gc_run(set, &he->ext))
338 if (!nft_set_elem_expired(&he->ext))
341 if (nft_set_elem_mark_busy(&he->ext))
344 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
347 rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
348 atomic_dec(&set->nelems);
349 nft_set_gc_batch_add(gcb, he);
351 rhashtable_walk_stop(&hti);
352 rhashtable_walk_exit(&hti);
354 he = nft_set_catchall_gc(set);
356 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
358 nft_set_gc_batch_add(gcb, he);
360 nft_set_gc_batch_complete(gcb);
361 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
362 nft_set_gc_interval(set));
365 static u64 nft_rhash_privsize(const struct nlattr * const nla[],
366 const struct nft_set_desc *desc)
368 return sizeof(struct nft_rhash);
371 static void nft_rhash_gc_init(const struct nft_set *set)
373 struct nft_rhash *priv = nft_set_priv(set);
375 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
376 nft_set_gc_interval(set));
379 static int nft_rhash_init(const struct nft_set *set,
380 const struct nft_set_desc *desc,
381 const struct nlattr * const tb[])
383 struct nft_rhash *priv = nft_set_priv(set);
384 struct rhashtable_params params = nft_rhash_params;
387 params.nelem_hint = desc->size ?: NFT_RHASH_ELEMENT_HINT;
388 params.key_len = set->klen;
390 err = rhashtable_init(&priv->ht, ¶ms);
394 INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc);
395 if (set->flags & NFT_SET_TIMEOUT)
396 nft_rhash_gc_init(set);
401 static void nft_rhash_elem_destroy(void *ptr, void *arg)
403 nft_set_elem_destroy(arg, ptr, true);
406 static void nft_rhash_destroy(const struct nft_set *set)
408 struct nft_rhash *priv = nft_set_priv(set);
410 cancel_delayed_work_sync(&priv->gc_work);
412 rhashtable_free_and_destroy(&priv->ht, nft_rhash_elem_destroy,
416 /* Number of buckets is stored in u32, so cap our result to 1U<<31 */
417 #define NFT_MAX_BUCKETS (1U << 31)
419 static u32 nft_hash_buckets(u32 size)
421 u64 val = div_u64((u64)size * 4, 3);
423 if (val >= NFT_MAX_BUCKETS)
424 return NFT_MAX_BUCKETS;
426 return roundup_pow_of_two(val);
429 static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features,
430 struct nft_set_estimate *est)
433 est->lookup = NFT_SET_CLASS_O_1;
434 est->space = NFT_SET_CLASS_O_N;
442 struct hlist_head table[];
445 struct nft_hash_elem {
446 struct hlist_node node;
447 struct nft_set_ext ext;
450 INDIRECT_CALLABLE_SCOPE
451 bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
452 const u32 *key, const struct nft_set_ext **ext)
454 struct nft_hash *priv = nft_set_priv(set);
455 u8 genmask = nft_genmask_cur(net);
456 const struct nft_hash_elem *he;
459 hash = jhash(key, set->klen, priv->seed);
460 hash = reciprocal_scale(hash, priv->buckets);
461 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
462 if (!memcmp(nft_set_ext_key(&he->ext), key, set->klen) &&
463 nft_set_elem_active(&he->ext, genmask)) {
471 static void *nft_hash_get(const struct net *net, const struct nft_set *set,
472 const struct nft_set_elem *elem, unsigned int flags)
474 struct nft_hash *priv = nft_set_priv(set);
475 u8 genmask = nft_genmask_cur(net);
476 struct nft_hash_elem *he;
479 hash = jhash(elem->key.val.data, set->klen, priv->seed);
480 hash = reciprocal_scale(hash, priv->buckets);
481 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
482 if (!memcmp(nft_set_ext_key(&he->ext), elem->key.val.data, set->klen) &&
483 nft_set_elem_active(&he->ext, genmask))
486 return ERR_PTR(-ENOENT);
489 INDIRECT_CALLABLE_SCOPE
490 bool nft_hash_lookup_fast(const struct net *net,
491 const struct nft_set *set,
492 const u32 *key, const struct nft_set_ext **ext)
494 struct nft_hash *priv = nft_set_priv(set);
495 u8 genmask = nft_genmask_cur(net);
496 const struct nft_hash_elem *he;
500 hash = jhash_1word(k1, priv->seed);
501 hash = reciprocal_scale(hash, priv->buckets);
502 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
503 k2 = *(u32 *)nft_set_ext_key(&he->ext)->data;
505 nft_set_elem_active(&he->ext, genmask)) {
513 static u32 nft_jhash(const struct nft_set *set, const struct nft_hash *priv,
514 const struct nft_set_ext *ext)
516 const struct nft_data *key = nft_set_ext_key(ext);
519 if (set->klen == 4) {
521 hash = jhash_1word(k1, priv->seed);
523 hash = jhash(key, set->klen, priv->seed);
525 hash = reciprocal_scale(hash, priv->buckets);
530 static int nft_hash_insert(const struct net *net, const struct nft_set *set,
531 const struct nft_set_elem *elem,
532 struct nft_set_ext **ext)
534 struct nft_hash_elem *this = elem->priv, *he;
535 struct nft_hash *priv = nft_set_priv(set);
536 u8 genmask = nft_genmask_next(net);
539 hash = nft_jhash(set, priv, &this->ext);
540 hlist_for_each_entry(he, &priv->table[hash], node) {
541 if (!memcmp(nft_set_ext_key(&this->ext),
542 nft_set_ext_key(&he->ext), set->klen) &&
543 nft_set_elem_active(&he->ext, genmask)) {
548 hlist_add_head_rcu(&this->node, &priv->table[hash]);
552 static void nft_hash_activate(const struct net *net, const struct nft_set *set,
553 const struct nft_set_elem *elem)
555 struct nft_hash_elem *he = elem->priv;
557 nft_set_elem_change_active(net, set, &he->ext);
560 static bool nft_hash_flush(const struct net *net,
561 const struct nft_set *set, void *priv)
563 struct nft_hash_elem *he = priv;
565 nft_set_elem_change_active(net, set, &he->ext);
569 static void *nft_hash_deactivate(const struct net *net,
570 const struct nft_set *set,
571 const struct nft_set_elem *elem)
573 struct nft_hash *priv = nft_set_priv(set);
574 struct nft_hash_elem *this = elem->priv, *he;
575 u8 genmask = nft_genmask_next(net);
578 hash = nft_jhash(set, priv, &this->ext);
579 hlist_for_each_entry(he, &priv->table[hash], node) {
580 if (!memcmp(nft_set_ext_key(&he->ext), &elem->key.val,
582 nft_set_elem_active(&he->ext, genmask)) {
583 nft_set_elem_change_active(net, set, &he->ext);
590 static void nft_hash_remove(const struct net *net,
591 const struct nft_set *set,
592 const struct nft_set_elem *elem)
594 struct nft_hash_elem *he = elem->priv;
596 hlist_del_rcu(&he->node);
599 static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set,
600 struct nft_set_iter *iter)
602 struct nft_hash *priv = nft_set_priv(set);
603 struct nft_hash_elem *he;
604 struct nft_set_elem elem;
607 for (i = 0; i < priv->buckets; i++) {
608 hlist_for_each_entry_rcu(he, &priv->table[i], node) {
609 if (iter->count < iter->skip)
611 if (!nft_set_elem_active(&he->ext, iter->genmask))
616 iter->err = iter->fn(ctx, set, iter, &elem);
625 static u64 nft_hash_privsize(const struct nlattr * const nla[],
626 const struct nft_set_desc *desc)
628 return sizeof(struct nft_hash) +
629 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head);
632 static int nft_hash_init(const struct nft_set *set,
633 const struct nft_set_desc *desc,
634 const struct nlattr * const tb[])
636 struct nft_hash *priv = nft_set_priv(set);
638 priv->buckets = nft_hash_buckets(desc->size);
639 get_random_bytes(&priv->seed, sizeof(priv->seed));
644 static void nft_hash_destroy(const struct nft_set *set)
646 struct nft_hash *priv = nft_set_priv(set);
647 struct nft_hash_elem *he;
648 struct hlist_node *next;
651 for (i = 0; i < priv->buckets; i++) {
652 hlist_for_each_entry_safe(he, next, &priv->table[i], node) {
653 hlist_del_rcu(&he->node);
654 nft_set_elem_destroy(set, he, true);
659 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
660 struct nft_set_estimate *est)
668 est->size = sizeof(struct nft_hash) +
669 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
670 (u64)desc->size * sizeof(struct nft_hash_elem);
671 est->lookup = NFT_SET_CLASS_O_1;
672 est->space = NFT_SET_CLASS_O_N;
677 static bool nft_hash_fast_estimate(const struct nft_set_desc *desc, u32 features,
678 struct nft_set_estimate *est)
686 est->size = sizeof(struct nft_hash) +
687 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
688 (u64)desc->size * sizeof(struct nft_hash_elem);
689 est->lookup = NFT_SET_CLASS_O_1;
690 est->space = NFT_SET_CLASS_O_N;
695 const struct nft_set_type nft_set_rhash_type = {
696 .features = NFT_SET_MAP | NFT_SET_OBJECT |
697 NFT_SET_TIMEOUT | NFT_SET_EVAL,
699 .privsize = nft_rhash_privsize,
700 .elemsize = offsetof(struct nft_rhash_elem, ext),
701 .estimate = nft_rhash_estimate,
702 .init = nft_rhash_init,
703 .gc_init = nft_rhash_gc_init,
704 .destroy = nft_rhash_destroy,
705 .insert = nft_rhash_insert,
706 .activate = nft_rhash_activate,
707 .deactivate = nft_rhash_deactivate,
708 .flush = nft_rhash_flush,
709 .remove = nft_rhash_remove,
710 .lookup = nft_rhash_lookup,
711 .update = nft_rhash_update,
712 .delete = nft_rhash_delete,
713 .walk = nft_rhash_walk,
714 .get = nft_rhash_get,
718 const struct nft_set_type nft_set_hash_type = {
719 .features = NFT_SET_MAP | NFT_SET_OBJECT,
721 .privsize = nft_hash_privsize,
722 .elemsize = offsetof(struct nft_hash_elem, ext),
723 .estimate = nft_hash_estimate,
724 .init = nft_hash_init,
725 .destroy = nft_hash_destroy,
726 .insert = nft_hash_insert,
727 .activate = nft_hash_activate,
728 .deactivate = nft_hash_deactivate,
729 .flush = nft_hash_flush,
730 .remove = nft_hash_remove,
731 .lookup = nft_hash_lookup,
732 .walk = nft_hash_walk,
737 const struct nft_set_type nft_set_hash_fast_type = {
738 .features = NFT_SET_MAP | NFT_SET_OBJECT,
740 .privsize = nft_hash_privsize,
741 .elemsize = offsetof(struct nft_hash_elem, ext),
742 .estimate = nft_hash_fast_estimate,
743 .init = nft_hash_init,
744 .destroy = nft_hash_destroy,
745 .insert = nft_hash_insert,
746 .activate = nft_hash_activate,
747 .deactivate = nft_hash_deactivate,
748 .flush = nft_hash_flush,
749 .remove = nft_hash_remove,
750 .lookup = nft_hash_lookup_fast,
751 .walk = nft_hash_walk,