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_HASH_ELEMENT_HINT 3
29 struct delayed_work gc_work;
32 struct nft_hash_elem {
33 struct rhash_head node;
34 struct nft_set_ext ext;
37 struct nft_hash_cmp_arg {
38 const struct nft_set *set;
39 const struct nft_data *key;
43 static const struct rhashtable_params nft_hash_params;
45 static inline u32 nft_hash_key(const void *data, u32 len, u32 seed)
47 const struct nft_hash_cmp_arg *arg = data;
49 return jhash(arg->key, len, seed);
52 static inline u32 nft_hash_obj(const void *data, u32 len, u32 seed)
54 const struct nft_hash_elem *he = data;
56 return jhash(nft_set_ext_key(&he->ext), len, seed);
59 static inline int nft_hash_cmp(struct rhashtable_compare_arg *arg,
62 const struct nft_hash_cmp_arg *x = arg->key;
63 const struct nft_hash_elem *he = ptr;
65 if (nft_data_cmp(nft_set_ext_key(&he->ext), x->key, x->set->klen))
67 if (nft_set_elem_expired(&he->ext))
69 if (!nft_set_elem_active(&he->ext, x->genmask))
74 static bool nft_hash_lookup(const struct nft_set *set,
75 const struct nft_data *key,
76 const struct nft_set_ext **ext)
78 struct nft_hash *priv = nft_set_priv(set);
79 const struct nft_hash_elem *he;
80 struct nft_hash_cmp_arg arg = {
81 .genmask = nft_genmask_cur(read_pnet(&set->pnet)),
86 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
93 static bool nft_hash_update(struct nft_set *set, const struct nft_data *key,
94 void *(*new)(struct nft_set *,
95 const struct nft_expr *,
97 const struct nft_expr *expr,
98 struct nft_data data[],
99 const struct nft_set_ext **ext)
101 struct nft_hash *priv = nft_set_priv(set);
102 struct nft_hash_elem *he;
103 struct nft_hash_cmp_arg arg = {
104 .genmask = NFT_GENMASK_ANY,
109 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
113 he = new(set, expr, data);
116 if (rhashtable_lookup_insert_key(&priv->ht, &arg, &he->node,
124 nft_set_elem_destroy(set, he);
129 static int nft_hash_insert(const struct nft_set *set,
130 const struct nft_set_elem *elem)
132 struct nft_hash *priv = nft_set_priv(set);
133 struct nft_hash_elem *he = elem->priv;
134 struct nft_hash_cmp_arg arg = {
135 .genmask = nft_genmask_next(read_pnet(&set->pnet)),
140 return rhashtable_lookup_insert_key(&priv->ht, &arg, &he->node,
144 static void nft_hash_activate(const struct nft_set *set,
145 const struct nft_set_elem *elem)
147 struct nft_hash_elem *he = elem->priv;
149 nft_set_elem_change_active(set, &he->ext);
150 nft_set_elem_clear_busy(&he->ext);
153 static void *nft_hash_deactivate(const struct nft_set *set,
154 const struct nft_set_elem *elem)
156 struct nft_hash *priv = nft_set_priv(set);
157 struct nft_hash_elem *he;
158 struct nft_hash_cmp_arg arg = {
159 .genmask = nft_genmask_next(read_pnet(&set->pnet)),
165 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
167 if (!nft_set_elem_mark_busy(&he->ext))
168 nft_set_elem_change_active(set, &he->ext);
177 static void nft_hash_remove(const struct nft_set *set,
178 const struct nft_set_elem *elem)
180 struct nft_hash *priv = nft_set_priv(set);
181 struct nft_hash_elem *he = elem->priv;
183 rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
186 static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
187 struct nft_set_iter *iter)
189 struct nft_hash *priv = nft_set_priv(set);
190 struct nft_hash_elem *he;
191 struct rhashtable_iter hti;
192 struct nft_set_elem elem;
193 u8 genmask = nft_genmask_cur(read_pnet(&set->pnet));
196 err = rhashtable_walk_init(&priv->ht, &hti);
201 err = rhashtable_walk_start(&hti);
202 if (err && err != -EAGAIN) {
207 while ((he = rhashtable_walk_next(&hti))) {
210 if (err != -EAGAIN) {
218 if (iter->count < iter->skip)
220 if (nft_set_elem_expired(&he->ext))
222 if (!nft_set_elem_active(&he->ext, genmask))
227 iter->err = iter->fn(ctx, set, iter, &elem);
236 rhashtable_walk_stop(&hti);
237 rhashtable_walk_exit(&hti);
240 static void nft_hash_gc(struct work_struct *work)
243 struct nft_hash_elem *he;
244 struct nft_hash *priv;
245 struct nft_set_gc_batch *gcb = NULL;
246 struct rhashtable_iter hti;
249 priv = container_of(work, struct nft_hash, gc_work.work);
250 set = nft_set_container_of(priv);
252 err = rhashtable_walk_init(&priv->ht, &hti);
256 err = rhashtable_walk_start(&hti);
257 if (err && err != -EAGAIN)
260 while ((he = rhashtable_walk_next(&hti))) {
262 if (PTR_ERR(he) != -EAGAIN)
267 if (!nft_set_elem_expired(&he->ext))
269 if (nft_set_elem_mark_busy(&he->ext))
272 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
275 rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
276 atomic_dec(&set->nelems);
277 nft_set_gc_batch_add(gcb, he);
280 rhashtable_walk_stop(&hti);
281 rhashtable_walk_exit(&hti);
283 nft_set_gc_batch_complete(gcb);
285 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
286 nft_set_gc_interval(set));
289 static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
291 return sizeof(struct nft_hash);
294 static const struct rhashtable_params nft_hash_params = {
295 .head_offset = offsetof(struct nft_hash_elem, node),
296 .hashfn = nft_hash_key,
297 .obj_hashfn = nft_hash_obj,
298 .obj_cmpfn = nft_hash_cmp,
299 .automatic_shrinking = true,
302 static int nft_hash_init(const struct nft_set *set,
303 const struct nft_set_desc *desc,
304 const struct nlattr * const tb[])
306 struct nft_hash *priv = nft_set_priv(set);
307 struct rhashtable_params params = nft_hash_params;
310 params.nelem_hint = desc->size ?: NFT_HASH_ELEMENT_HINT;
311 params.key_len = set->klen;
313 err = rhashtable_init(&priv->ht, ¶ms);
317 INIT_DEFERRABLE_WORK(&priv->gc_work, nft_hash_gc);
318 if (set->flags & NFT_SET_TIMEOUT)
319 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
320 nft_set_gc_interval(set));
324 static void nft_hash_elem_destroy(void *ptr, void *arg)
326 nft_set_elem_destroy((const struct nft_set *)arg, ptr);
329 static void nft_hash_destroy(const struct nft_set *set)
331 struct nft_hash *priv = nft_set_priv(set);
333 cancel_delayed_work_sync(&priv->gc_work);
334 rhashtable_free_and_destroy(&priv->ht, nft_hash_elem_destroy,
338 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
339 struct nft_set_estimate *est)
343 esize = sizeof(struct nft_hash_elem);
345 est->size = sizeof(struct nft_hash) +
346 roundup_pow_of_two(desc->size * 4 / 3) *
347 sizeof(struct nft_hash_elem *) +
350 /* Resizing happens when the load drops below 30% or goes
351 * above 75%. The average of 52.5% load (approximated by 50%)
352 * is used for the size estimation of the hash buckets,
353 * meaning we calculate two buckets per element.
355 est->size = esize + 2 * sizeof(struct nft_hash_elem *);
358 est->class = NFT_SET_CLASS_O_1;
363 static struct nft_set_ops nft_hash_ops __read_mostly = {
364 .privsize = nft_hash_privsize,
365 .elemsize = offsetof(struct nft_hash_elem, ext),
366 .estimate = nft_hash_estimate,
367 .init = nft_hash_init,
368 .destroy = nft_hash_destroy,
369 .insert = nft_hash_insert,
370 .activate = nft_hash_activate,
371 .deactivate = nft_hash_deactivate,
372 .remove = nft_hash_remove,
373 .lookup = nft_hash_lookup,
374 .update = nft_hash_update,
375 .walk = nft_hash_walk,
376 .features = NFT_SET_MAP | NFT_SET_TIMEOUT,
377 .owner = THIS_MODULE,
380 static int __init nft_hash_module_init(void)
382 return nft_register_set(&nft_hash_ops);
385 static void __exit nft_hash_module_exit(void)
387 nft_unregister_set(&nft_hash_ops);
390 module_init(nft_hash_module_init);
391 module_exit(nft_hash_module_exit);
393 MODULE_LICENSE("GPL");
394 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
395 MODULE_ALIAS_NFT_SET();