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
30 struct nft_hash_elem {
31 struct rhash_head node;
32 struct nft_set_ext ext;
35 struct nft_hash_cmp_arg {
36 const struct nft_set *set;
37 const struct nft_data *key;
40 static const struct rhashtable_params nft_hash_params;
42 static inline u32 nft_hash_key(const void *data, u32 len, u32 seed)
44 const struct nft_hash_cmp_arg *arg = data;
46 return jhash(arg->key, len, seed);
49 static inline u32 nft_hash_obj(const void *data, u32 len, u32 seed)
51 const struct nft_hash_elem *he = data;
53 return jhash(nft_set_ext_key(&he->ext), len, seed);
56 static inline int nft_hash_cmp(struct rhashtable_compare_arg *arg,
59 const struct nft_hash_cmp_arg *x = arg->key;
60 const struct nft_hash_elem *he = ptr;
62 if (nft_data_cmp(nft_set_ext_key(&he->ext), x->key, x->set->klen))
67 static bool nft_hash_lookup(const struct nft_set *set,
68 const struct nft_data *key,
69 struct nft_data *data)
71 struct nft_hash *priv = nft_set_priv(set);
72 const struct nft_hash_elem *he;
73 struct nft_hash_cmp_arg arg = {
78 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
79 if (he && set->flags & NFT_SET_MAP)
80 nft_data_copy(data, nft_set_ext_data(&he->ext));
85 static int nft_hash_insert(const struct nft_set *set,
86 const struct nft_set_elem *elem)
88 struct nft_hash *priv = nft_set_priv(set);
89 struct nft_hash_elem *he = elem->priv;
90 struct nft_hash_cmp_arg arg = {
95 return rhashtable_lookup_insert_key(&priv->ht, &arg, &he->node,
99 static void nft_hash_elem_destroy(const struct nft_set *set,
100 struct nft_hash_elem *he)
102 nft_data_uninit(nft_set_ext_key(&he->ext), NFT_DATA_VALUE);
103 if (set->flags & NFT_SET_MAP)
104 nft_data_uninit(nft_set_ext_data(&he->ext), set->dtype);
108 static void nft_hash_remove(const struct nft_set *set,
109 const struct nft_set_elem *elem)
111 struct nft_hash *priv = nft_set_priv(set);
113 rhashtable_remove_fast(&priv->ht, elem->cookie, nft_hash_params);
118 static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem)
120 struct nft_hash *priv = nft_set_priv(set);
121 struct nft_hash_elem *he;
122 struct nft_hash_cmp_arg arg = {
127 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
136 static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
137 struct nft_set_iter *iter)
139 struct nft_hash *priv = nft_set_priv(set);
140 struct nft_hash_elem *he;
141 struct rhashtable_iter hti;
142 struct nft_set_elem elem;
145 err = rhashtable_walk_init(&priv->ht, &hti);
150 err = rhashtable_walk_start(&hti);
151 if (err && err != -EAGAIN) {
156 while ((he = rhashtable_walk_next(&hti))) {
159 if (err != -EAGAIN) {
167 if (iter->count < iter->skip)
172 iter->err = iter->fn(ctx, set, iter, &elem);
181 rhashtable_walk_stop(&hti);
182 rhashtable_walk_exit(&hti);
185 static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
187 return sizeof(struct nft_hash);
190 static const struct rhashtable_params nft_hash_params = {
191 .head_offset = offsetof(struct nft_hash_elem, node),
192 .hashfn = nft_hash_key,
193 .obj_hashfn = nft_hash_obj,
194 .obj_cmpfn = nft_hash_cmp,
195 .automatic_shrinking = true,
198 static int nft_hash_init(const struct nft_set *set,
199 const struct nft_set_desc *desc,
200 const struct nlattr * const tb[])
202 struct nft_hash *priv = nft_set_priv(set);
203 struct rhashtable_params params = nft_hash_params;
205 params.nelem_hint = desc->size ?: NFT_HASH_ELEMENT_HINT;
206 params.key_len = set->klen;
208 return rhashtable_init(&priv->ht, ¶ms);
211 static void nft_free_element(void *ptr, void *arg)
213 nft_hash_elem_destroy((const struct nft_set *)arg, ptr);
216 static void nft_hash_destroy(const struct nft_set *set)
218 struct nft_hash *priv = nft_set_priv(set);
220 rhashtable_free_and_destroy(&priv->ht, nft_free_element, (void *)set);
223 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
224 struct nft_set_estimate *est)
228 esize = sizeof(struct nft_hash_elem);
230 est->size = sizeof(struct nft_hash) +
231 roundup_pow_of_two(desc->size * 4 / 3) *
232 sizeof(struct nft_hash_elem *) +
235 /* Resizing happens when the load drops below 30% or goes
236 * above 75%. The average of 52.5% load (approximated by 50%)
237 * is used for the size estimation of the hash buckets,
238 * meaning we calculate two buckets per element.
240 est->size = esize + 2 * sizeof(struct nft_hash_elem *);
243 est->class = NFT_SET_CLASS_O_1;
248 static struct nft_set_ops nft_hash_ops __read_mostly = {
249 .privsize = nft_hash_privsize,
250 .elemsize = offsetof(struct nft_hash_elem, ext),
251 .estimate = nft_hash_estimate,
252 .init = nft_hash_init,
253 .destroy = nft_hash_destroy,
255 .insert = nft_hash_insert,
256 .remove = nft_hash_remove,
257 .lookup = nft_hash_lookup,
258 .walk = nft_hash_walk,
259 .features = NFT_SET_MAP,
260 .owner = THIS_MODULE,
263 static int __init nft_hash_module_init(void)
265 return nft_register_set(&nft_hash_ops);
268 static void __exit nft_hash_module_exit(void)
270 nft_unregister_set(&nft_hash_ops);
273 module_init(nft_hash_module_init);
274 module_exit(nft_hash_module_exit);
276 MODULE_LICENSE("GPL");
277 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
278 MODULE_ALIAS_NFT_SET();