1 // SPDX-License-Identifier: GPL-2.0-only
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 static void nft_rhash_elem_destroy(void *ptr, void *arg)
405 nft_set_elem_destroy(arg, ptr, true);
408 static void nft_rhash_destroy(const struct nft_set *set)
410 struct nft_rhash *priv = nft_set_priv(set);
412 cancel_delayed_work_sync(&priv->gc_work);
414 rhashtable_free_and_destroy(&priv->ht, nft_rhash_elem_destroy,
418 /* Number of buckets is stored in u32, so cap our result to 1U<<31 */
419 #define NFT_MAX_BUCKETS (1U << 31)
421 static u32 nft_hash_buckets(u32 size)
423 u64 val = div_u64((u64)size * 4, 3);
425 if (val >= NFT_MAX_BUCKETS)
426 return NFT_MAX_BUCKETS;
428 return roundup_pow_of_two(val);
431 static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features,
432 struct nft_set_estimate *est)
435 est->lookup = NFT_SET_CLASS_O_1;
436 est->space = NFT_SET_CLASS_O_N;
444 struct hlist_head table[];
447 struct nft_hash_elem {
448 struct hlist_node node;
449 struct nft_set_ext ext;
452 INDIRECT_CALLABLE_SCOPE
453 bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
454 const u32 *key, const struct nft_set_ext **ext)
456 struct nft_hash *priv = nft_set_priv(set);
457 u8 genmask = nft_genmask_cur(net);
458 const struct nft_hash_elem *he;
461 hash = jhash(key, set->klen, priv->seed);
462 hash = reciprocal_scale(hash, priv->buckets);
463 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
464 if (!memcmp(nft_set_ext_key(&he->ext), key, set->klen) &&
465 nft_set_elem_active(&he->ext, genmask)) {
473 static void *nft_hash_get(const struct net *net, const struct nft_set *set,
474 const struct nft_set_elem *elem, unsigned int flags)
476 struct nft_hash *priv = nft_set_priv(set);
477 u8 genmask = nft_genmask_cur(net);
478 struct nft_hash_elem *he;
481 hash = jhash(elem->key.val.data, set->klen, priv->seed);
482 hash = reciprocal_scale(hash, priv->buckets);
483 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
484 if (!memcmp(nft_set_ext_key(&he->ext), elem->key.val.data, set->klen) &&
485 nft_set_elem_active(&he->ext, genmask))
488 return ERR_PTR(-ENOENT);
491 INDIRECT_CALLABLE_SCOPE
492 bool nft_hash_lookup_fast(const struct net *net,
493 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;
502 hash = jhash_1word(k1, priv->seed);
503 hash = reciprocal_scale(hash, priv->buckets);
504 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
505 k2 = *(u32 *)nft_set_ext_key(&he->ext)->data;
507 nft_set_elem_active(&he->ext, genmask)) {
515 static u32 nft_jhash(const struct nft_set *set, const struct nft_hash *priv,
516 const struct nft_set_ext *ext)
518 const struct nft_data *key = nft_set_ext_key(ext);
521 if (set->klen == 4) {
523 hash = jhash_1word(k1, priv->seed);
525 hash = jhash(key, set->klen, priv->seed);
527 hash = reciprocal_scale(hash, priv->buckets);
532 static int nft_hash_insert(const struct net *net, const struct nft_set *set,
533 const struct nft_set_elem *elem,
534 struct nft_set_ext **ext)
536 struct nft_hash_elem *this = elem->priv, *he;
537 struct nft_hash *priv = nft_set_priv(set);
538 u8 genmask = nft_genmask_next(net);
541 hash = nft_jhash(set, priv, &this->ext);
542 hlist_for_each_entry(he, &priv->table[hash], node) {
543 if (!memcmp(nft_set_ext_key(&this->ext),
544 nft_set_ext_key(&he->ext), set->klen) &&
545 nft_set_elem_active(&he->ext, genmask)) {
550 hlist_add_head_rcu(&this->node, &priv->table[hash]);
554 static void nft_hash_activate(const struct net *net, const struct nft_set *set,
555 const struct nft_set_elem *elem)
557 struct nft_hash_elem *he = elem->priv;
559 nft_set_elem_change_active(net, set, &he->ext);
562 static bool nft_hash_flush(const struct net *net,
563 const struct nft_set *set, void *priv)
565 struct nft_hash_elem *he = priv;
567 nft_set_elem_change_active(net, set, &he->ext);
571 static void *nft_hash_deactivate(const struct net *net,
572 const struct nft_set *set,
573 const struct nft_set_elem *elem)
575 struct nft_hash *priv = nft_set_priv(set);
576 struct nft_hash_elem *this = elem->priv, *he;
577 u8 genmask = nft_genmask_next(net);
580 hash = nft_jhash(set, priv, &this->ext);
581 hlist_for_each_entry(he, &priv->table[hash], node) {
582 if (!memcmp(nft_set_ext_key(&he->ext), &elem->key.val,
584 nft_set_elem_active(&he->ext, genmask)) {
585 nft_set_elem_change_active(net, set, &he->ext);
592 static void nft_hash_remove(const struct net *net,
593 const struct nft_set *set,
594 const struct nft_set_elem *elem)
596 struct nft_hash_elem *he = elem->priv;
598 hlist_del_rcu(&he->node);
601 static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set,
602 struct nft_set_iter *iter)
604 struct nft_hash *priv = nft_set_priv(set);
605 struct nft_hash_elem *he;
606 struct nft_set_elem elem;
609 for (i = 0; i < priv->buckets; i++) {
610 hlist_for_each_entry_rcu(he, &priv->table[i], node) {
611 if (iter->count < iter->skip)
613 if (!nft_set_elem_active(&he->ext, iter->genmask))
618 iter->err = iter->fn(ctx, set, iter, &elem);
627 static u64 nft_hash_privsize(const struct nlattr * const nla[],
628 const struct nft_set_desc *desc)
630 return sizeof(struct nft_hash) +
631 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head);
634 static int nft_hash_init(const struct nft_set *set,
635 const struct nft_set_desc *desc,
636 const struct nlattr * const tb[])
638 struct nft_hash *priv = nft_set_priv(set);
640 priv->buckets = nft_hash_buckets(desc->size);
641 get_random_bytes(&priv->seed, sizeof(priv->seed));
646 static void nft_hash_destroy(const struct nft_set *set)
648 struct nft_hash *priv = nft_set_priv(set);
649 struct nft_hash_elem *he;
650 struct hlist_node *next;
653 for (i = 0; i < priv->buckets; i++) {
654 hlist_for_each_entry_safe(he, next, &priv->table[i], node) {
655 hlist_del_rcu(&he->node);
656 nft_set_elem_destroy(set, he, true);
661 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
662 struct nft_set_estimate *est)
670 est->size = sizeof(struct nft_hash) +
671 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
672 (u64)desc->size * sizeof(struct nft_hash_elem);
673 est->lookup = NFT_SET_CLASS_O_1;
674 est->space = NFT_SET_CLASS_O_N;
679 static bool nft_hash_fast_estimate(const struct nft_set_desc *desc, u32 features,
680 struct nft_set_estimate *est)
688 est->size = sizeof(struct nft_hash) +
689 (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
690 (u64)desc->size * sizeof(struct nft_hash_elem);
691 est->lookup = NFT_SET_CLASS_O_1;
692 est->space = NFT_SET_CLASS_O_N;
697 const struct nft_set_type nft_set_rhash_type = {
698 .features = NFT_SET_MAP | NFT_SET_OBJECT |
699 NFT_SET_TIMEOUT | NFT_SET_EVAL,
701 .privsize = nft_rhash_privsize,
702 .elemsize = offsetof(struct nft_rhash_elem, ext),
703 .estimate = nft_rhash_estimate,
704 .init = nft_rhash_init,
705 .gc_init = nft_rhash_gc_init,
706 .destroy = nft_rhash_destroy,
707 .insert = nft_rhash_insert,
708 .activate = nft_rhash_activate,
709 .deactivate = nft_rhash_deactivate,
710 .flush = nft_rhash_flush,
711 .remove = nft_rhash_remove,
712 .lookup = nft_rhash_lookup,
713 .update = nft_rhash_update,
714 .delete = nft_rhash_delete,
715 .walk = nft_rhash_walk,
716 .get = nft_rhash_get,
720 const struct nft_set_type nft_set_hash_type = {
721 .features = NFT_SET_MAP | NFT_SET_OBJECT,
723 .privsize = nft_hash_privsize,
724 .elemsize = offsetof(struct nft_hash_elem, ext),
725 .estimate = nft_hash_estimate,
726 .init = nft_hash_init,
727 .destroy = nft_hash_destroy,
728 .insert = nft_hash_insert,
729 .activate = nft_hash_activate,
730 .deactivate = nft_hash_deactivate,
731 .flush = nft_hash_flush,
732 .remove = nft_hash_remove,
733 .lookup = nft_hash_lookup,
734 .walk = nft_hash_walk,
739 const struct nft_set_type nft_set_hash_fast_type = {
740 .features = NFT_SET_MAP | NFT_SET_OBJECT,
742 .privsize = nft_hash_privsize,
743 .elemsize = offsetof(struct nft_hash_elem, ext),
744 .estimate = nft_hash_fast_estimate,
745 .init = nft_hash_init,
746 .destroy = nft_hash_destroy,
747 .insert = nft_hash_insert,
748 .activate = nft_hash_activate,
749 .deactivate = nft_hash_deactivate,
750 .flush = nft_hash_flush,
751 .remove = nft_hash_remove,
752 .lookup = nft_hash_lookup_fast,
753 .walk = nft_hash_walk,