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/rhashtable.h>
19 #include <linux/netfilter.h>
20 #include <linux/netfilter/nf_tables.h>
21 #include <net/netfilter/nf_tables.h>
23 /* We target a hash table size of 4, element hint is 75% of final size */
24 #define NFT_HASH_ELEMENT_HINT 3
26 struct nft_hash_elem {
27 struct rhash_head node;
29 struct nft_data data[];
32 static bool nft_hash_lookup(const struct nft_set *set,
33 const struct nft_data *key,
34 struct nft_data *data)
36 struct rhashtable *priv = nft_set_priv(set);
37 const struct nft_hash_elem *he;
39 he = rhashtable_lookup(priv, key);
40 if (he && set->flags & NFT_SET_MAP)
41 nft_data_copy(data, he->data);
46 static int nft_hash_insert(const struct nft_set *set,
47 const struct nft_set_elem *elem)
49 struct rhashtable *priv = nft_set_priv(set);
50 struct nft_hash_elem *he;
57 if (set->flags & NFT_SET_MAP)
58 size += sizeof(he->data[0]);
60 he = kzalloc(size, GFP_KERNEL);
64 nft_data_copy(&he->key, &elem->key);
65 if (set->flags & NFT_SET_MAP)
66 nft_data_copy(he->data, &elem->data);
68 rhashtable_insert(priv, &he->node);
73 static void nft_hash_elem_destroy(const struct nft_set *set,
74 struct nft_hash_elem *he)
76 nft_data_uninit(&he->key, NFT_DATA_VALUE);
77 if (set->flags & NFT_SET_MAP)
78 nft_data_uninit(he->data, set->dtype);
82 static void nft_hash_remove(const struct nft_set *set,
83 const struct nft_set_elem *elem)
85 struct rhashtable *priv = nft_set_priv(set);
87 rhashtable_remove(priv, elem->cookie);
92 struct nft_compare_arg {
93 const struct nft_set *set;
94 struct nft_set_elem *elem;
97 static bool nft_hash_compare(void *ptr, void *arg)
99 struct nft_hash_elem *he = ptr;
100 struct nft_compare_arg *x = arg;
102 if (!nft_data_cmp(&he->key, &x->elem->key, x->set->klen)) {
103 x->elem->cookie = he;
105 if (x->set->flags & NFT_SET_MAP)
106 nft_data_copy(&x->elem->data, he->data);
114 static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem)
116 struct rhashtable *priv = nft_set_priv(set);
117 struct nft_compare_arg arg = {
122 if (rhashtable_lookup_compare(priv, &elem->key,
123 &nft_hash_compare, &arg))
129 static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
130 struct nft_set_iter *iter)
132 struct rhashtable *priv = nft_set_priv(set);
133 const struct bucket_table *tbl;
134 const struct nft_hash_elem *he;
135 struct nft_set_elem elem;
138 tbl = rht_dereference_rcu(priv->tbl, priv);
139 for (i = 0; i < tbl->size; i++) {
140 struct rhash_head *pos;
142 rht_for_each_entry_rcu(he, pos, tbl, i, node) {
143 if (iter->count < iter->skip)
146 memcpy(&elem.key, &he->key, sizeof(elem.key));
147 if (set->flags & NFT_SET_MAP)
148 memcpy(&elem.data, he->data, sizeof(elem.data));
151 iter->err = iter->fn(ctx, set, iter, &elem);
160 static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
162 return sizeof(struct rhashtable);
165 static int nft_hash_init(const struct nft_set *set,
166 const struct nft_set_desc *desc,
167 const struct nlattr * const tb[])
169 struct rhashtable *priv = nft_set_priv(set);
170 struct rhashtable_params params = {
171 .nelem_hint = desc->size ? : NFT_HASH_ELEMENT_HINT,
172 .head_offset = offsetof(struct nft_hash_elem, node),
173 .key_offset = offsetof(struct nft_hash_elem, key),
174 .key_len = set->klen,
176 .grow_decision = rht_grow_above_75,
177 .shrink_decision = rht_shrink_below_30,
180 return rhashtable_init(priv, ¶ms);
183 static void nft_hash_destroy(const struct nft_set *set)
185 struct rhashtable *priv = nft_set_priv(set);
186 const struct bucket_table *tbl;
187 struct nft_hash_elem *he;
188 struct rhash_head *pos, *next;
191 /* Stop an eventual async resizing */
192 priv->being_destroyed = true;
193 mutex_lock(&priv->mutex);
195 tbl = rht_dereference(priv->tbl, priv);
196 for (i = 0; i < tbl->size; i++) {
197 rht_for_each_entry_safe(he, pos, next, tbl, i, node)
198 nft_hash_elem_destroy(set, he);
200 mutex_unlock(&priv->mutex);
202 rhashtable_destroy(priv);
205 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
206 struct nft_set_estimate *est)
210 esize = sizeof(struct nft_hash_elem);
211 if (features & NFT_SET_MAP)
212 esize += FIELD_SIZEOF(struct nft_hash_elem, data[0]);
215 est->size = sizeof(struct rhashtable) +
216 roundup_pow_of_two(desc->size * 4 / 3) *
217 sizeof(struct nft_hash_elem *) +
220 /* Resizing happens when the load drops below 30% or goes
221 * above 75%. The average of 52.5% load (approximated by 50%)
222 * is used for the size estimation of the hash buckets,
223 * meaning we calculate two buckets per element.
225 est->size = esize + 2 * sizeof(struct nft_hash_elem *);
228 est->class = NFT_SET_CLASS_O_1;
233 static struct nft_set_ops nft_hash_ops __read_mostly = {
234 .privsize = nft_hash_privsize,
235 .estimate = nft_hash_estimate,
236 .init = nft_hash_init,
237 .destroy = nft_hash_destroy,
239 .insert = nft_hash_insert,
240 .remove = nft_hash_remove,
241 .lookup = nft_hash_lookup,
242 .walk = nft_hash_walk,
243 .features = NFT_SET_MAP,
244 .owner = THIS_MODULE,
247 static int __init nft_hash_module_init(void)
249 return nft_register_set(&nft_hash_ops);
252 static void __exit nft_hash_module_exit(void)
254 nft_unregister_set(&nft_hash_ops);
257 module_init(nft_hash_module_init);
258 module_exit(nft_hash_module_exit);
260 MODULE_LICENSE("GPL");
261 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
262 MODULE_ALIAS_NFT_SET();