2 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/list.h>
15 #include <linux/log2.h>
16 #include <linux/jhash.h>
17 #include <linux/netlink.h>
18 #include <linux/workqueue.h>
19 #include <linux/rhashtable.h>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/nf_tables.h>
22 #include <net/netfilter/nf_tables.h>
24 /* We target a hash table size of 4, element hint is 75% of final size */
25 #define NFT_RHASH_ELEMENT_HINT 3
29 struct delayed_work gc_work;
32 struct nft_rhash_elem {
33 struct rhash_head node;
34 struct nft_set_ext ext;
37 struct nft_rhash_cmp_arg {
38 const struct nft_set *set;
43 static inline u32 nft_rhash_key(const void *data, u32 len, u32 seed)
45 const struct nft_rhash_cmp_arg *arg = data;
47 return jhash(arg->key, len, seed);
50 static inline u32 nft_rhash_obj(const void *data, u32 len, u32 seed)
52 const struct nft_rhash_elem *he = data;
54 return jhash(nft_set_ext_key(&he->ext), len, seed);
57 static inline int nft_rhash_cmp(struct rhashtable_compare_arg *arg,
60 const struct nft_rhash_cmp_arg *x = arg->key;
61 const struct nft_rhash_elem *he = ptr;
63 if (memcmp(nft_set_ext_key(&he->ext), x->key, x->set->klen))
65 if (nft_set_elem_expired(&he->ext))
67 if (!nft_set_elem_active(&he->ext, x->genmask))
72 static const struct rhashtable_params nft_rhash_params = {
73 .head_offset = offsetof(struct nft_rhash_elem, node),
74 .hashfn = nft_rhash_key,
75 .obj_hashfn = nft_rhash_obj,
76 .obj_cmpfn = nft_rhash_cmp,
77 .automatic_shrinking = true,
80 static bool nft_rhash_lookup(const struct net *net, const struct nft_set *set,
81 const u32 *key, const struct nft_set_ext **ext)
83 struct nft_rhash *priv = nft_set_priv(set);
84 const struct nft_rhash_elem *he;
85 struct nft_rhash_cmp_arg arg = {
86 .genmask = nft_genmask_cur(net),
91 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_rhash_params);
98 static bool nft_rhash_update(struct nft_set *set, const u32 *key,
99 void *(*new)(struct nft_set *,
100 const struct nft_expr *,
101 struct nft_regs *regs),
102 const struct nft_expr *expr,
103 struct nft_regs *regs,
104 const struct nft_set_ext **ext)
106 struct nft_rhash *priv = nft_set_priv(set);
107 struct nft_rhash_elem *he, *prev;
108 struct nft_rhash_cmp_arg arg = {
109 .genmask = NFT_GENMASK_ANY,
114 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_rhash_params);
118 he = new(set, expr, regs);
122 prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
127 /* Another cpu may race to insert the element with the same key */
129 nft_set_elem_destroy(set, he, true);
130 atomic_dec(&set->nelems);
139 nft_set_elem_destroy(set, he, true);
140 atomic_dec(&set->nelems);
145 static int nft_rhash_insert(const struct net *net, const struct nft_set *set,
146 const struct nft_set_elem *elem,
147 struct nft_set_ext **ext)
149 struct nft_rhash *priv = nft_set_priv(set);
150 struct nft_rhash_elem *he = elem->priv;
151 struct nft_rhash_cmp_arg arg = {
152 .genmask = nft_genmask_next(net),
154 .key = elem->key.val.data,
156 struct nft_rhash_elem *prev;
158 prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
161 return PTR_ERR(prev);
169 static void nft_rhash_activate(const struct net *net, const struct nft_set *set,
170 const struct nft_set_elem *elem)
172 struct nft_rhash_elem *he = elem->priv;
174 nft_set_elem_change_active(net, set, &he->ext);
175 nft_set_elem_clear_busy(&he->ext);
178 static bool nft_rhash_flush(const struct net *net,
179 const struct nft_set *set, void *priv)
181 struct nft_rhash_elem *he = priv;
183 if (!nft_set_elem_mark_busy(&he->ext) ||
184 !nft_is_active(net, &he->ext)) {
185 nft_set_elem_change_active(net, set, &he->ext);
191 static void *nft_rhash_deactivate(const struct net *net,
192 const struct nft_set *set,
193 const struct nft_set_elem *elem)
195 struct nft_rhash *priv = nft_set_priv(set);
196 struct nft_rhash_elem *he;
197 struct nft_rhash_cmp_arg arg = {
198 .genmask = nft_genmask_next(net),
200 .key = elem->key.val.data,
204 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_rhash_params);
206 !nft_rhash_flush(net, set, he))
214 static void nft_rhash_remove(const struct net *net,
215 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 = elem->priv;
221 rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
224 static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
225 struct nft_set_iter *iter)
227 struct nft_rhash *priv = nft_set_priv(set);
228 struct nft_rhash_elem *he;
229 struct rhashtable_iter hti;
230 struct nft_set_elem elem;
233 err = rhashtable_walk_init(&priv->ht, &hti, GFP_ATOMIC);
238 err = rhashtable_walk_start(&hti);
239 if (err && err != -EAGAIN) {
244 while ((he = rhashtable_walk_next(&hti))) {
247 if (err != -EAGAIN) {
255 if (iter->count < iter->skip)
257 if (nft_set_elem_expired(&he->ext))
259 if (!nft_set_elem_active(&he->ext, iter->genmask))
264 iter->err = iter->fn(ctx, set, iter, &elem);
273 rhashtable_walk_stop(&hti);
274 rhashtable_walk_exit(&hti);
277 static void nft_rhash_gc(struct work_struct *work)
280 struct nft_rhash_elem *he;
281 struct nft_rhash *priv;
282 struct nft_set_gc_batch *gcb = NULL;
283 struct rhashtable_iter hti;
286 priv = container_of(work, struct nft_rhash, gc_work.work);
287 set = nft_set_container_of(priv);
289 err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL);
293 err = rhashtable_walk_start(&hti);
294 if (err && err != -EAGAIN)
297 while ((he = rhashtable_walk_next(&hti))) {
299 if (PTR_ERR(he) != -EAGAIN)
304 if (!nft_set_elem_expired(&he->ext))
306 if (nft_set_elem_mark_busy(&he->ext))
309 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
312 rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
313 atomic_dec(&set->nelems);
314 nft_set_gc_batch_add(gcb, he);
317 rhashtable_walk_stop(&hti);
318 rhashtable_walk_exit(&hti);
320 nft_set_gc_batch_complete(gcb);
322 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
323 nft_set_gc_interval(set));
326 static unsigned int nft_rhash_privsize(const struct nlattr * const nla[],
327 const struct nft_set_desc *desc)
329 return sizeof(struct nft_rhash);
332 static int nft_rhash_init(const struct nft_set *set,
333 const struct nft_set_desc *desc,
334 const struct nlattr * const tb[])
336 struct nft_rhash *priv = nft_set_priv(set);
337 struct rhashtable_params params = nft_rhash_params;
340 params.nelem_hint = desc->size ?: NFT_RHASH_ELEMENT_HINT;
341 params.key_len = set->klen;
343 err = rhashtable_init(&priv->ht, ¶ms);
347 INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc);
348 if (set->flags & NFT_SET_TIMEOUT)
349 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
350 nft_set_gc_interval(set));
354 static void nft_rhash_elem_destroy(void *ptr, void *arg)
356 nft_set_elem_destroy(arg, ptr, true);
359 static void nft_rhash_destroy(const struct nft_set *set)
361 struct nft_rhash *priv = nft_set_priv(set);
363 cancel_delayed_work_sync(&priv->gc_work);
365 rhashtable_free_and_destroy(&priv->ht, nft_rhash_elem_destroy,
369 /* Number of buckets is stored in u32, so cap our result to 1U<<31 */
370 #define NFT_MAX_BUCKETS (1U << 31)
372 static u32 nft_hash_buckets(u32 size)
374 u64 val = div_u64((u64)size * 4, 3);
376 if (val >= NFT_MAX_BUCKETS)
377 return NFT_MAX_BUCKETS;
379 return roundup_pow_of_two(val);
382 static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features,
383 struct nft_set_estimate *est)
386 est->lookup = NFT_SET_CLASS_O_1;
387 est->space = NFT_SET_CLASS_O_N;
395 struct hlist_head table[];
398 struct nft_hash_elem {
399 struct hlist_node node;
400 struct nft_set_ext ext;
403 static bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
404 const u32 *key, const struct nft_set_ext **ext)
406 struct nft_hash *priv = nft_set_priv(set);
407 u8 genmask = nft_genmask_cur(net);
408 const struct nft_hash_elem *he;
411 hash = jhash(key, set->klen, priv->seed);
412 hash = reciprocal_scale(hash, priv->buckets);
413 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
414 if (!memcmp(nft_set_ext_key(&he->ext), key, set->klen) &&
415 nft_set_elem_active(&he->ext, genmask)) {
423 /* nft_hash_select_ops() makes sure key size can be either 2 or 4 bytes . */
424 static inline u32 nft_hash_key(const u32 *key, u32 klen)
432 static bool nft_hash_lookup_fast(const struct net *net,
433 const struct nft_set *set,
434 const u32 *key, const struct nft_set_ext **ext)
436 struct nft_hash *priv = nft_set_priv(set);
437 u8 genmask = nft_genmask_cur(net);
438 const struct nft_hash_elem *he;
441 k1 = nft_hash_key(key, set->klen);
442 hash = jhash_1word(k1, priv->seed);
443 hash = reciprocal_scale(hash, priv->buckets);
444 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
445 k2 = nft_hash_key(nft_set_ext_key(&he->ext)->data, set->klen);
447 nft_set_elem_active(&he->ext, genmask)) {
455 static u32 nft_jhash(const struct nft_set *set, const struct nft_hash *priv,
456 const struct nft_set_ext *ext)
458 const struct nft_data *key = nft_set_ext_key(ext);
461 if (set->klen == 4) {
463 hash = jhash_1word(k1, priv->seed);
465 hash = jhash(key, set->klen, priv->seed);
467 hash = reciprocal_scale(hash, priv->buckets);
472 static int nft_hash_insert(const struct net *net, const struct nft_set *set,
473 const struct nft_set_elem *elem,
474 struct nft_set_ext **ext)
476 struct nft_hash_elem *this = elem->priv, *he;
477 struct nft_hash *priv = nft_set_priv(set);
478 u8 genmask = nft_genmask_next(net);
481 hash = nft_jhash(set, priv, &this->ext);
482 hlist_for_each_entry(he, &priv->table[hash], node) {
483 if (!memcmp(nft_set_ext_key(&this->ext),
484 nft_set_ext_key(&he->ext), set->klen) &&
485 nft_set_elem_active(&he->ext, genmask)) {
490 hlist_add_head_rcu(&this->node, &priv->table[hash]);
494 static void nft_hash_activate(const struct net *net, const struct nft_set *set,
495 const struct nft_set_elem *elem)
497 struct nft_hash_elem *he = elem->priv;
499 nft_set_elem_change_active(net, set, &he->ext);
502 static bool nft_hash_flush(const struct net *net,
503 const struct nft_set *set, void *priv)
505 struct nft_hash_elem *he = priv;
507 nft_set_elem_change_active(net, set, &he->ext);
511 static void *nft_hash_deactivate(const struct net *net,
512 const struct nft_set *set,
513 const struct nft_set_elem *elem)
515 struct nft_hash *priv = nft_set_priv(set);
516 struct nft_hash_elem *this = elem->priv, *he;
517 u8 genmask = nft_genmask_next(net);
520 hash = nft_jhash(set, priv, &this->ext);
521 hlist_for_each_entry(he, &priv->table[hash], node) {
522 if (!memcmp(nft_set_ext_key(&this->ext), &elem->key.val,
524 nft_set_elem_active(&he->ext, genmask)) {
525 nft_set_elem_change_active(net, set, &he->ext);
532 static void nft_hash_remove(const struct net *net,
533 const struct nft_set *set,
534 const struct nft_set_elem *elem)
536 struct nft_hash_elem *he = elem->priv;
538 hlist_del_rcu(&he->node);
541 static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set,
542 struct nft_set_iter *iter)
544 struct nft_hash *priv = nft_set_priv(set);
545 struct nft_hash_elem *he;
546 struct nft_set_elem elem;
549 for (i = 0; i < priv->buckets; i++) {
550 hlist_for_each_entry_rcu(he, &priv->table[i], node) {
551 if (iter->count < iter->skip)
553 if (!nft_set_elem_active(&he->ext, iter->genmask))
558 iter->err = iter->fn(ctx, set, iter, &elem);
567 static unsigned int nft_hash_privsize(const struct nlattr * const nla[],
568 const struct nft_set_desc *desc)
570 return sizeof(struct nft_hash) +
571 nft_hash_buckets(desc->size) * sizeof(struct hlist_head);
574 static int nft_hash_init(const struct nft_set *set,
575 const struct nft_set_desc *desc,
576 const struct nlattr * const tb[])
578 struct nft_hash *priv = nft_set_priv(set);
580 priv->buckets = nft_hash_buckets(desc->size);
581 get_random_bytes(&priv->seed, sizeof(priv->seed));
586 static void nft_hash_destroy(const struct nft_set *set)
588 struct nft_hash *priv = nft_set_priv(set);
589 struct nft_hash_elem *he;
590 struct hlist_node *next;
593 for (i = 0; i < priv->buckets; i++) {
594 hlist_for_each_entry_safe(he, next, &priv->table[i], node) {
595 hlist_del_rcu(&he->node);
596 nft_set_elem_destroy(set, he, true);
601 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
602 struct nft_set_estimate *est)
604 est->size = sizeof(struct nft_hash) +
605 nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
606 desc->size * sizeof(struct nft_hash_elem);
607 est->lookup = NFT_SET_CLASS_O_1;
608 est->space = NFT_SET_CLASS_O_N;
613 static struct nft_set_type nft_hash_type;
614 static struct nft_set_ops nft_rhash_ops __read_mostly = {
615 .type = &nft_hash_type,
616 .privsize = nft_rhash_privsize,
617 .elemsize = offsetof(struct nft_rhash_elem, ext),
618 .estimate = nft_rhash_estimate,
619 .init = nft_rhash_init,
620 .destroy = nft_rhash_destroy,
621 .insert = nft_rhash_insert,
622 .activate = nft_rhash_activate,
623 .deactivate = nft_rhash_deactivate,
624 .flush = nft_rhash_flush,
625 .remove = nft_rhash_remove,
626 .lookup = nft_rhash_lookup,
627 .update = nft_rhash_update,
628 .walk = nft_rhash_walk,
629 .features = NFT_SET_MAP | NFT_SET_OBJECT | NFT_SET_TIMEOUT,
632 static struct nft_set_ops nft_hash_ops __read_mostly = {
633 .type = &nft_hash_type,
634 .privsize = nft_hash_privsize,
635 .elemsize = offsetof(struct nft_hash_elem, ext),
636 .estimate = nft_hash_estimate,
637 .init = nft_hash_init,
638 .destroy = nft_hash_destroy,
639 .insert = nft_hash_insert,
640 .activate = nft_hash_activate,
641 .deactivate = nft_hash_deactivate,
642 .flush = nft_hash_flush,
643 .remove = nft_hash_remove,
644 .lookup = nft_hash_lookup,
645 .walk = nft_hash_walk,
646 .features = NFT_SET_MAP | NFT_SET_OBJECT,
649 static struct nft_set_ops nft_hash_fast_ops __read_mostly = {
650 .type = &nft_hash_type,
651 .privsize = nft_hash_privsize,
652 .elemsize = offsetof(struct nft_hash_elem, ext),
653 .estimate = nft_hash_estimate,
654 .init = nft_hash_init,
655 .destroy = nft_hash_destroy,
656 .insert = nft_hash_insert,
657 .activate = nft_hash_activate,
658 .deactivate = nft_hash_deactivate,
659 .flush = nft_hash_flush,
660 .remove = nft_hash_remove,
661 .lookup = nft_hash_lookup_fast,
662 .walk = nft_hash_walk,
663 .features = NFT_SET_MAP | NFT_SET_OBJECT,
666 static const struct nft_set_ops *
667 nft_hash_select_ops(const struct nft_ctx *ctx, const struct nft_set_desc *desc,
671 switch (desc->klen) {
673 return &nft_hash_fast_ops;
675 return &nft_hash_ops;
679 return &nft_rhash_ops;
682 static struct nft_set_type nft_hash_type __read_mostly = {
683 .select_ops = nft_hash_select_ops,
684 .owner = THIS_MODULE,
687 static int __init nft_hash_module_init(void)
689 return nft_register_set(&nft_hash_type);
692 static void __exit nft_hash_module_exit(void)
694 nft_unregister_set(&nft_hash_type);
697 module_init(nft_hash_module_init);
698 module_exit(nft_hash_module_exit);
700 MODULE_LICENSE("GPL");
701 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
702 MODULE_ALIAS_NFT_SET();