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 void *nft_rhash_get(const struct net *net, const struct nft_set *set,
99 const struct nft_set_elem *elem, unsigned int flags)
101 struct nft_rhash *priv = nft_set_priv(set);
102 struct nft_rhash_elem *he;
103 struct nft_rhash_cmp_arg arg = {
104 .genmask = nft_genmask_cur(net),
106 .key = elem->key.val.data,
109 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_rhash_params);
113 return ERR_PTR(-ENOENT);
116 static bool nft_rhash_update(struct nft_set *set, const u32 *key,
117 void *(*new)(struct nft_set *,
118 const struct nft_expr *,
119 struct nft_regs *regs),
120 const struct nft_expr *expr,
121 struct nft_regs *regs,
122 const struct nft_set_ext **ext)
124 struct nft_rhash *priv = nft_set_priv(set);
125 struct nft_rhash_elem *he, *prev;
126 struct nft_rhash_cmp_arg arg = {
127 .genmask = NFT_GENMASK_ANY,
132 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_rhash_params);
136 he = new(set, expr, regs);
140 prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
145 /* Another cpu may race to insert the element with the same key */
147 nft_set_elem_destroy(set, he, true);
156 nft_set_elem_destroy(set, he, true);
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_fast(&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 void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
241 struct nft_set_iter *iter)
243 struct nft_rhash *priv = nft_set_priv(set);
244 struct nft_rhash_elem *he;
245 struct rhashtable_iter hti;
246 struct nft_set_elem elem;
249 err = rhashtable_walk_init(&priv->ht, &hti, GFP_ATOMIC);
254 rhashtable_walk_start(&hti);
256 while ((he = rhashtable_walk_next(&hti))) {
259 if (err != -EAGAIN) {
267 if (iter->count < iter->skip)
269 if (nft_set_elem_expired(&he->ext))
271 if (!nft_set_elem_active(&he->ext, iter->genmask))
276 iter->err = iter->fn(ctx, set, iter, &elem);
285 rhashtable_walk_stop(&hti);
286 rhashtable_walk_exit(&hti);
289 static void nft_rhash_gc(struct work_struct *work)
292 struct nft_rhash_elem *he;
293 struct nft_rhash *priv;
294 struct nft_set_gc_batch *gcb = NULL;
295 struct rhashtable_iter hti;
298 priv = container_of(work, struct nft_rhash, gc_work.work);
299 set = nft_set_container_of(priv);
301 err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL);
305 rhashtable_walk_start(&hti);
307 while ((he = rhashtable_walk_next(&hti))) {
309 if (PTR_ERR(he) != -EAGAIN)
314 if (nft_set_ext_exists(&he->ext, NFT_SET_EXT_EXPR)) {
315 struct nft_expr *expr = nft_set_ext_expr(&he->ext);
318 expr->ops->gc(read_pnet(&set->net), expr))
321 if (!nft_set_elem_expired(&he->ext))
324 if (nft_set_elem_mark_busy(&he->ext))
327 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
330 rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
331 atomic_dec(&set->nelems);
332 nft_set_gc_batch_add(gcb, he);
335 rhashtable_walk_stop(&hti);
336 rhashtable_walk_exit(&hti);
338 nft_set_gc_batch_complete(gcb);
340 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
341 nft_set_gc_interval(set));
344 static unsigned int nft_rhash_privsize(const struct nlattr * const nla[],
345 const struct nft_set_desc *desc)
347 return sizeof(struct nft_rhash);
350 static void nft_rhash_gc_init(const struct nft_set *set)
352 struct nft_rhash *priv = nft_set_priv(set);
354 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
355 nft_set_gc_interval(set));
358 static int nft_rhash_init(const struct nft_set *set,
359 const struct nft_set_desc *desc,
360 const struct nlattr * const tb[])
362 struct nft_rhash *priv = nft_set_priv(set);
363 struct rhashtable_params params = nft_rhash_params;
366 params.nelem_hint = desc->size ?: NFT_RHASH_ELEMENT_HINT;
367 params.key_len = set->klen;
369 err = rhashtable_init(&priv->ht, ¶ms);
373 INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc);
374 if (set->flags & NFT_SET_TIMEOUT)
375 nft_rhash_gc_init(set);
380 static void nft_rhash_elem_destroy(void *ptr, void *arg)
382 nft_set_elem_destroy(arg, ptr, true);
385 static void nft_rhash_destroy(const struct nft_set *set)
387 struct nft_rhash *priv = nft_set_priv(set);
389 cancel_delayed_work_sync(&priv->gc_work);
391 rhashtable_free_and_destroy(&priv->ht, nft_rhash_elem_destroy,
395 static u32 nft_hash_buckets(u32 size)
397 return roundup_pow_of_two(size * 4 / 3);
400 static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features,
401 struct nft_set_estimate *est)
404 est->lookup = NFT_SET_CLASS_O_1;
405 est->space = NFT_SET_CLASS_O_N;
413 struct hlist_head table[];
416 struct nft_hash_elem {
417 struct hlist_node node;
418 struct nft_set_ext ext;
421 static bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
422 const u32 *key, const struct nft_set_ext **ext)
424 struct nft_hash *priv = nft_set_priv(set);
425 u8 genmask = nft_genmask_cur(net);
426 const struct nft_hash_elem *he;
429 hash = jhash(key, set->klen, priv->seed);
430 hash = reciprocal_scale(hash, priv->buckets);
431 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
432 if (!memcmp(nft_set_ext_key(&he->ext), key, set->klen) &&
433 nft_set_elem_active(&he->ext, genmask)) {
441 static void *nft_hash_get(const struct net *net, const struct nft_set *set,
442 const struct nft_set_elem *elem, unsigned int flags)
444 struct nft_hash *priv = nft_set_priv(set);
445 u8 genmask = nft_genmask_cur(net);
446 struct nft_hash_elem *he;
449 hash = jhash(elem->key.val.data, set->klen, priv->seed);
450 hash = reciprocal_scale(hash, priv->buckets);
451 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
452 if (!memcmp(nft_set_ext_key(&he->ext), elem->key.val.data, set->klen) &&
453 nft_set_elem_active(&he->ext, genmask))
456 return ERR_PTR(-ENOENT);
459 /* nft_hash_select_ops() makes sure key size can be either 2 or 4 bytes . */
460 static inline u32 nft_hash_key(const u32 *key, u32 klen)
468 static bool nft_hash_lookup_fast(const struct net *net,
469 const struct nft_set *set,
470 const u32 *key, const struct nft_set_ext **ext)
472 struct nft_hash *priv = nft_set_priv(set);
473 u8 genmask = nft_genmask_cur(net);
474 const struct nft_hash_elem *he;
477 k1 = nft_hash_key(key, set->klen);
478 hash = jhash_1word(k1, priv->seed);
479 hash = reciprocal_scale(hash, priv->buckets);
480 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
481 k2 = nft_hash_key(nft_set_ext_key(&he->ext)->data, set->klen);
483 nft_set_elem_active(&he->ext, genmask)) {
491 static int nft_hash_insert(const struct net *net, const struct nft_set *set,
492 const struct nft_set_elem *elem,
493 struct nft_set_ext **ext)
495 struct nft_hash_elem *this = elem->priv, *he;
496 struct nft_hash *priv = nft_set_priv(set);
497 u8 genmask = nft_genmask_next(net);
500 hash = jhash(nft_set_ext_key(&this->ext), set->klen, priv->seed);
501 hash = reciprocal_scale(hash, priv->buckets);
502 hlist_for_each_entry(he, &priv->table[hash], node) {
503 if (!memcmp(nft_set_ext_key(&this->ext),
504 nft_set_ext_key(&he->ext), set->klen) &&
505 nft_set_elem_active(&he->ext, genmask)) {
510 hlist_add_head_rcu(&this->node, &priv->table[hash]);
514 static void nft_hash_activate(const struct net *net, const struct nft_set *set,
515 const struct nft_set_elem *elem)
517 struct nft_hash_elem *he = elem->priv;
519 nft_set_elem_change_active(net, set, &he->ext);
522 static bool nft_hash_flush(const struct net *net,
523 const struct nft_set *set, void *priv)
525 struct nft_hash_elem *he = priv;
527 nft_set_elem_change_active(net, set, &he->ext);
531 static void *nft_hash_deactivate(const struct net *net,
532 const struct nft_set *set,
533 const struct nft_set_elem *elem)
535 struct nft_hash *priv = nft_set_priv(set);
536 struct nft_hash_elem *this = elem->priv, *he;
537 u8 genmask = nft_genmask_next(net);
540 hash = jhash(nft_set_ext_key(&this->ext), set->klen, priv->seed);
541 hash = reciprocal_scale(hash, priv->buckets);
542 hlist_for_each_entry(he, &priv->table[hash], node) {
543 if (!memcmp(nft_set_ext_key(&this->ext), &elem->key.val,
545 nft_set_elem_active(&he->ext, genmask)) {
546 nft_set_elem_change_active(net, set, &he->ext);
553 static void nft_hash_remove(const struct net *net,
554 const struct nft_set *set,
555 const struct nft_set_elem *elem)
557 struct nft_hash_elem *he = elem->priv;
559 hlist_del_rcu(&he->node);
562 static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set,
563 struct nft_set_iter *iter)
565 struct nft_hash *priv = nft_set_priv(set);
566 struct nft_hash_elem *he;
567 struct nft_set_elem elem;
570 for (i = 0; i < priv->buckets; i++) {
571 hlist_for_each_entry_rcu(he, &priv->table[i], node) {
572 if (iter->count < iter->skip)
574 if (!nft_set_elem_active(&he->ext, iter->genmask))
579 iter->err = iter->fn(ctx, set, iter, &elem);
588 static unsigned int nft_hash_privsize(const struct nlattr * const nla[],
589 const struct nft_set_desc *desc)
591 return sizeof(struct nft_hash) +
592 nft_hash_buckets(desc->size) * sizeof(struct hlist_head);
595 static int nft_hash_init(const struct nft_set *set,
596 const struct nft_set_desc *desc,
597 const struct nlattr * const tb[])
599 struct nft_hash *priv = nft_set_priv(set);
601 priv->buckets = nft_hash_buckets(desc->size);
602 get_random_bytes(&priv->seed, sizeof(priv->seed));
607 static void nft_hash_destroy(const struct nft_set *set)
609 struct nft_hash *priv = nft_set_priv(set);
610 struct nft_hash_elem *he;
611 struct hlist_node *next;
614 for (i = 0; i < priv->buckets; i++) {
615 hlist_for_each_entry_safe(he, next, &priv->table[i], node) {
616 hlist_del_rcu(&he->node);
617 nft_set_elem_destroy(set, he, true);
622 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
623 struct nft_set_estimate *est)
631 est->size = sizeof(struct nft_hash) +
632 nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
633 desc->size * sizeof(struct nft_hash_elem);
634 est->lookup = NFT_SET_CLASS_O_1;
635 est->space = NFT_SET_CLASS_O_N;
640 static bool nft_hash_fast_estimate(const struct nft_set_desc *desc, u32 features,
641 struct nft_set_estimate *est)
649 est->size = sizeof(struct nft_hash) +
650 nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
651 desc->size * sizeof(struct nft_hash_elem);
652 est->lookup = NFT_SET_CLASS_O_1;
653 est->space = NFT_SET_CLASS_O_N;
658 struct nft_set_type nft_set_rhash_type __read_mostly = {
659 .owner = THIS_MODULE,
660 .features = NFT_SET_MAP | NFT_SET_OBJECT |
661 NFT_SET_TIMEOUT | NFT_SET_EVAL,
663 .privsize = nft_rhash_privsize,
664 .elemsize = offsetof(struct nft_rhash_elem, ext),
665 .estimate = nft_rhash_estimate,
666 .init = nft_rhash_init,
667 .gc_init = nft_rhash_gc_init,
668 .destroy = nft_rhash_destroy,
669 .insert = nft_rhash_insert,
670 .activate = nft_rhash_activate,
671 .deactivate = nft_rhash_deactivate,
672 .flush = nft_rhash_flush,
673 .remove = nft_rhash_remove,
674 .lookup = nft_rhash_lookup,
675 .update = nft_rhash_update,
676 .walk = nft_rhash_walk,
677 .get = nft_rhash_get,
681 struct nft_set_type nft_set_hash_type __read_mostly = {
682 .owner = THIS_MODULE,
683 .features = NFT_SET_MAP | NFT_SET_OBJECT,
685 .privsize = nft_hash_privsize,
686 .elemsize = offsetof(struct nft_hash_elem, ext),
687 .estimate = nft_hash_estimate,
688 .init = nft_hash_init,
689 .destroy = nft_hash_destroy,
690 .insert = nft_hash_insert,
691 .activate = nft_hash_activate,
692 .deactivate = nft_hash_deactivate,
693 .flush = nft_hash_flush,
694 .remove = nft_hash_remove,
695 .lookup = nft_hash_lookup,
696 .walk = nft_hash_walk,
701 struct nft_set_type nft_set_hash_fast_type __read_mostly = {
702 .owner = THIS_MODULE,
703 .features = NFT_SET_MAP | NFT_SET_OBJECT,
705 .privsize = nft_hash_privsize,
706 .elemsize = offsetof(struct nft_hash_elem, ext),
707 .estimate = nft_hash_fast_estimate,
708 .init = nft_hash_init,
709 .destroy = nft_hash_destroy,
710 .insert = nft_hash_insert,
711 .activate = nft_hash_activate,
712 .deactivate = nft_hash_deactivate,
713 .flush = nft_hash_flush,
714 .remove = nft_hash_remove,
715 .lookup = nft_hash_lookup_fast,
716 .walk = nft_hash_walk,