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 const struct rhashtable_params nft_hash_params;
34 static bool nft_hash_lookup(const struct nft_set *set,
35 const struct nft_data *key,
36 struct nft_data *data)
38 struct rhashtable *priv = nft_set_priv(set);
39 const struct nft_hash_elem *he;
41 he = rhashtable_lookup_fast(priv, key, nft_hash_params);
42 if (he && set->flags & NFT_SET_MAP)
43 nft_data_copy(data, he->data);
48 static int nft_hash_insert(const struct nft_set *set,
49 const struct nft_set_elem *elem)
51 struct rhashtable *priv = nft_set_priv(set);
52 struct nft_hash_elem *he;
60 if (set->flags & NFT_SET_MAP)
61 size += sizeof(he->data[0]);
63 he = kzalloc(size, GFP_KERNEL);
67 nft_data_copy(&he->key, &elem->key);
68 if (set->flags & NFT_SET_MAP)
69 nft_data_copy(he->data, &elem->data);
71 err = rhashtable_insert_fast(priv, &he->node, nft_hash_params);
78 static void nft_hash_elem_destroy(const struct nft_set *set,
79 struct nft_hash_elem *he)
81 nft_data_uninit(&he->key, NFT_DATA_VALUE);
82 if (set->flags & NFT_SET_MAP)
83 nft_data_uninit(he->data, set->dtype);
87 static void nft_hash_remove(const struct nft_set *set,
88 const struct nft_set_elem *elem)
90 struct rhashtable *priv = nft_set_priv(set);
92 rhashtable_remove_fast(priv, elem->cookie, nft_hash_params);
97 static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem)
99 struct rhashtable *priv = nft_set_priv(set);
100 struct nft_hash_elem *he;
102 he = rhashtable_lookup_fast(priv, &elem->key, nft_hash_params);
108 if (set->flags & NFT_SET_MAP)
109 nft_data_copy(&elem->data, he->data);
114 static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
115 struct nft_set_iter *iter)
117 struct rhashtable *priv = nft_set_priv(set);
118 const struct nft_hash_elem *he;
119 struct rhashtable_iter hti;
120 struct nft_set_elem elem;
123 err = rhashtable_walk_init(priv, &hti);
128 err = rhashtable_walk_start(&hti);
129 if (err && err != -EAGAIN) {
134 while ((he = rhashtable_walk_next(&hti))) {
137 if (err != -EAGAIN) {
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 rhashtable_walk_stop(&hti);
161 rhashtable_walk_exit(&hti);
164 static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
166 return sizeof(struct rhashtable);
169 static const struct rhashtable_params nft_hash_params = {
170 .head_offset = offsetof(struct nft_hash_elem, node),
171 .key_offset = offsetof(struct nft_hash_elem, key),
175 static int nft_hash_init(const struct nft_set *set,
176 const struct nft_set_desc *desc,
177 const struct nlattr * const tb[])
179 struct rhashtable *priv = nft_set_priv(set);
180 struct rhashtable_params params = nft_hash_params;
182 params.nelem_hint = desc->size ?: NFT_HASH_ELEMENT_HINT;
183 params.key_len = set->klen;
185 return rhashtable_init(priv, ¶ms);
188 static void nft_hash_destroy(const struct nft_set *set)
190 struct rhashtable *priv = nft_set_priv(set);
191 const struct bucket_table *tbl;
192 struct nft_hash_elem *he;
193 struct rhash_head *pos, *next;
196 /* Stop an eventual async resizing */
197 priv->being_destroyed = true;
198 mutex_lock(&priv->mutex);
200 tbl = rht_dereference(priv->tbl, priv);
201 for (i = 0; i < tbl->size; i++) {
202 rht_for_each_entry_safe(he, pos, next, tbl, i, node)
203 nft_hash_elem_destroy(set, he);
205 mutex_unlock(&priv->mutex);
207 rhashtable_destroy(priv);
210 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
211 struct nft_set_estimate *est)
215 esize = sizeof(struct nft_hash_elem);
216 if (features & NFT_SET_MAP)
217 esize += FIELD_SIZEOF(struct nft_hash_elem, data[0]);
220 est->size = sizeof(struct rhashtable) +
221 roundup_pow_of_two(desc->size * 4 / 3) *
222 sizeof(struct nft_hash_elem *) +
225 /* Resizing happens when the load drops below 30% or goes
226 * above 75%. The average of 52.5% load (approximated by 50%)
227 * is used for the size estimation of the hash buckets,
228 * meaning we calculate two buckets per element.
230 est->size = esize + 2 * sizeof(struct nft_hash_elem *);
233 est->class = NFT_SET_CLASS_O_1;
238 static struct nft_set_ops nft_hash_ops __read_mostly = {
239 .privsize = nft_hash_privsize,
240 .estimate = nft_hash_estimate,
241 .init = nft_hash_init,
242 .destroy = nft_hash_destroy,
244 .insert = nft_hash_insert,
245 .remove = nft_hash_remove,
246 .lookup = nft_hash_lookup,
247 .walk = nft_hash_walk,
248 .features = NFT_SET_MAP,
249 .owner = THIS_MODULE,
252 static int __init nft_hash_module_init(void)
254 return nft_register_set(&nft_hash_ops);
257 static void __exit nft_hash_module_exit(void)
259 nft_unregister_set(&nft_hash_ops);
262 module_init(nft_hash_module_init);
263 module_exit(nft_hash_module_exit);
265 MODULE_LICENSE("GPL");
266 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
267 MODULE_ALIAS_NFT_SET();