Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / net / netfilter / nft_hash.c
CommitLineData
96518518 1/*
ce6eb0d7 2 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
96518518
PM
3 *
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.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/list.h>
c50b960c 15#include <linux/log2.h>
96518518
PM
16#include <linux/jhash.h>
17#include <linux/netlink.h>
9d098292 18#include <linux/workqueue.h>
cfe4a9dd 19#include <linux/rhashtable.h>
96518518
PM
20#include <linux/netfilter.h>
21#include <linux/netfilter/nf_tables.h>
22#include <net/netfilter/nf_tables.h>
23
cfe4a9dd
TG
24/* We target a hash table size of 4, element hint is 75% of final size */
25#define NFT_HASH_ELEMENT_HINT 3
96518518 26
745f5450
PM
27struct nft_hash {
28 struct rhashtable ht;
9d098292 29 struct delayed_work gc_work;
745f5450
PM
30};
31
96518518 32struct nft_hash_elem {
cfe4a9dd 33 struct rhash_head node;
fe2811eb 34 struct nft_set_ext ext;
96518518
PM
35};
36
bfd6e327
PM
37struct nft_hash_cmp_arg {
38 const struct nft_set *set;
8cd8937a 39 const u32 *key;
cc02e457 40 u8 genmask;
bfd6e327
PM
41};
42
fa377321
HX
43static const struct rhashtable_params nft_hash_params;
44
bfd6e327
PM
45static inline u32 nft_hash_key(const void *data, u32 len, u32 seed)
46{
47 const struct nft_hash_cmp_arg *arg = data;
48
49 return jhash(arg->key, len, seed);
50}
51
52static inline u32 nft_hash_obj(const void *data, u32 len, u32 seed)
53{
54 const struct nft_hash_elem *he = data;
55
fe2811eb 56 return jhash(nft_set_ext_key(&he->ext), len, seed);
bfd6e327
PM
57}
58
59static inline int nft_hash_cmp(struct rhashtable_compare_arg *arg,
60 const void *ptr)
61{
62 const struct nft_hash_cmp_arg *x = arg->key;
63 const struct nft_hash_elem *he = ptr;
64
e562d860 65 if (memcmp(nft_set_ext_key(&he->ext), x->key, x->set->klen))
bfd6e327 66 return 1;
9d098292
PM
67 if (nft_set_elem_expired(&he->ext))
68 return 1;
cc02e457
PM
69 if (!nft_set_elem_active(&he->ext, x->genmask))
70 return 1;
bfd6e327
PM
71 return 0;
72}
73
42a55769
PNA
74static bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
75 const u32 *key, const struct nft_set_ext **ext)
96518518 76{
745f5450 77 struct nft_hash *priv = nft_set_priv(set);
20a69341 78 const struct nft_hash_elem *he;
bfd6e327 79 struct nft_hash_cmp_arg arg = {
42a55769 80 .genmask = nft_genmask_cur(net),
bfd6e327
PM
81 .set = set,
82 .key = key,
83 };
ce6eb0d7 84
bfd6e327 85 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
b2832dd6
PM
86 if (he != NULL)
87 *ext = &he->ext;
ce6eb0d7 88
cfe4a9dd 89 return !!he;
96518518
PM
90}
91
8cd8937a 92static bool nft_hash_update(struct nft_set *set, const u32 *key,
22fe54d5
PM
93 void *(*new)(struct nft_set *,
94 const struct nft_expr *,
a55e22e9 95 struct nft_regs *regs),
22fe54d5 96 const struct nft_expr *expr,
a55e22e9 97 struct nft_regs *regs,
22fe54d5
PM
98 const struct nft_set_ext **ext)
99{
100 struct nft_hash *priv = nft_set_priv(set);
101 struct nft_hash_elem *he;
102 struct nft_hash_cmp_arg arg = {
103 .genmask = NFT_GENMASK_ANY,
104 .set = set,
105 .key = key,
106 };
107
108 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
109 if (he != NULL)
110 goto out;
111
a55e22e9 112 he = new(set, expr, regs);
22fe54d5
PM
113 if (he == NULL)
114 goto err1;
115 if (rhashtable_lookup_insert_key(&priv->ht, &arg, &he->node,
116 nft_hash_params))
117 goto err2;
118out:
119 *ext = &he->ext;
120 return true;
121
122err2:
123 nft_set_elem_destroy(set, he);
124err1:
125 return false;
126}
127
42a55769 128static int nft_hash_insert(const struct net *net, const struct nft_set *set,
20a69341 129 const struct nft_set_elem *elem)
96518518 130{
745f5450 131 struct nft_hash *priv = nft_set_priv(set);
fe2811eb 132 struct nft_hash_elem *he = elem->priv;
bfd6e327 133 struct nft_hash_cmp_arg arg = {
42a55769 134 .genmask = nft_genmask_next(net),
bfd6e327 135 .set = set,
7d740264 136 .key = elem->key.val.data,
bfd6e327 137 };
ce6eb0d7 138
fe2811eb
PM
139 return rhashtable_lookup_insert_key(&priv->ht, &arg, &he->node,
140 nft_hash_params);
96518518
PM
141}
142
42a55769 143static void nft_hash_activate(const struct net *net, const struct nft_set *set,
cc02e457 144 const struct nft_set_elem *elem)
96518518 145{
cc02e457 146 struct nft_hash_elem *he = elem->priv;
ce6eb0d7 147
42a55769 148 nft_set_elem_change_active(net, set, &he->ext);
9d098292 149 nft_set_elem_clear_busy(&he->ext);
20a69341 150}
96518518 151
42a55769
PNA
152static void *nft_hash_deactivate(const struct net *net,
153 const struct nft_set *set,
cc02e457 154 const struct nft_set_elem *elem)
20a69341 155{
745f5450 156 struct nft_hash *priv = nft_set_priv(set);
fa377321 157 struct nft_hash_elem *he;
bfd6e327 158 struct nft_hash_cmp_arg arg = {
8eee54be 159 .genmask = nft_genmask_next(net),
bfd6e327 160 .set = set,
7d740264 161 .key = elem->key.val.data,
bfd6e327 162 };
fa377321 163
9d098292 164 rcu_read_lock();
bfd6e327 165 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
9d098292 166 if (he != NULL) {
8eee54be
PNA
167 if (!nft_set_elem_mark_busy(&he->ext) ||
168 !nft_is_active(net, &he->ext))
42a55769 169 nft_set_elem_change_active(net, set, &he->ext);
9d098292
PM
170 else
171 he = NULL;
172 }
173 rcu_read_unlock();
8d24c0b4 174
cc02e457
PM
175 return he;
176}
8d24c0b4 177
cc02e457
PM
178static void nft_hash_remove(const struct nft_set *set,
179 const struct nft_set_elem *elem)
180{
181 struct nft_hash *priv = nft_set_priv(set);
182 struct nft_hash_elem *he = elem->priv;
183
184 rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
96518518
PM
185}
186
20a69341
PM
187static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
188 struct nft_set_iter *iter)
96518518 189{
745f5450 190 struct nft_hash *priv = nft_set_priv(set);
fe2811eb 191 struct nft_hash_elem *he;
9a776628 192 struct rhashtable_iter hti;
20a69341 193 struct nft_set_elem elem;
9a776628 194 int err;
96518518 195
8f6fd83c 196 err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL);
9a776628
HX
197 iter->err = err;
198 if (err)
199 return;
88d6ed15 200
9a776628
HX
201 err = rhashtable_walk_start(&hti);
202 if (err && err != -EAGAIN) {
203 iter->err = err;
204 goto out;
205 }
206
207 while ((he = rhashtable_walk_next(&hti))) {
208 if (IS_ERR(he)) {
209 err = PTR_ERR(he);
210 if (err != -EAGAIN) {
211 iter->err = err;
212 goto out;
213 }
d8bdff59
HX
214
215 continue;
9a776628
HX
216 }
217
218 if (iter->count < iter->skip)
219 goto cont;
9d098292
PM
220 if (nft_set_elem_expired(&he->ext))
221 goto cont;
8588ac09 222 if (!nft_set_elem_active(&he->ext, iter->genmask))
cc02e457 223 goto cont;
20a69341 224
fe2811eb 225 elem.priv = he;
9a776628
HX
226
227 iter->err = iter->fn(ctx, set, iter, &elem);
228 if (iter->err < 0)
229 goto out;
20a69341 230
20a69341 231cont:
9a776628 232 iter->count++;
96518518 233 }
9a776628
HX
234
235out:
236 rhashtable_walk_stop(&hti);
237 rhashtable_walk_exit(&hti);
96518518
PM
238}
239
9d098292
PM
240static void nft_hash_gc(struct work_struct *work)
241{
3dd0673a 242 struct nft_set *set;
9d098292
PM
243 struct nft_hash_elem *he;
244 struct nft_hash *priv;
245 struct nft_set_gc_batch *gcb = NULL;
246 struct rhashtable_iter hti;
247 int err;
248
249 priv = container_of(work, struct nft_hash, gc_work.work);
250 set = nft_set_container_of(priv);
251
8f6fd83c 252 err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL);
9d098292
PM
253 if (err)
254 goto schedule;
255
256 err = rhashtable_walk_start(&hti);
257 if (err && err != -EAGAIN)
258 goto out;
259
260 while ((he = rhashtable_walk_next(&hti))) {
261 if (IS_ERR(he)) {
262 if (PTR_ERR(he) != -EAGAIN)
263 goto out;
264 continue;
265 }
266
267 if (!nft_set_elem_expired(&he->ext))
268 continue;
269 if (nft_set_elem_mark_busy(&he->ext))
270 continue;
271
272 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
273 if (gcb == NULL)
274 goto out;
275 rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
3dd0673a 276 atomic_dec(&set->nelems);
9d098292
PM
277 nft_set_gc_batch_add(gcb, he);
278 }
279out:
280 rhashtable_walk_stop(&hti);
281 rhashtable_walk_exit(&hti);
282
283 nft_set_gc_batch_complete(gcb);
284schedule:
285 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
286 nft_set_gc_interval(set));
287}
288
20a69341
PM
289static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
290{
745f5450 291 return sizeof(struct nft_hash);
cfe4a9dd
TG
292}
293
fa377321 294static const struct rhashtable_params nft_hash_params = {
45d84751 295 .head_offset = offsetof(struct nft_hash_elem, node),
bfd6e327
PM
296 .hashfn = nft_hash_key,
297 .obj_hashfn = nft_hash_obj,
298 .obj_cmpfn = nft_hash_cmp,
45d84751 299 .automatic_shrinking = true,
fa377321
HX
300};
301
20a69341 302static int nft_hash_init(const struct nft_set *set,
c50b960c 303 const struct nft_set_desc *desc,
96518518
PM
304 const struct nlattr * const tb[])
305{
745f5450 306 struct nft_hash *priv = nft_set_priv(set);
fa377321 307 struct rhashtable_params params = nft_hash_params;
9d098292 308 int err;
fa377321
HX
309
310 params.nelem_hint = desc->size ?: NFT_HASH_ELEMENT_HINT;
45d84751 311 params.key_len = set->klen;
96518518 312
9d098292
PM
313 err = rhashtable_init(&priv->ht, &params);
314 if (err < 0)
315 return err;
316
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));
321 return 0;
96518518
PM
322}
323
61edafbb 324static void nft_hash_elem_destroy(void *ptr, void *arg)
96518518 325{
61edafbb 326 nft_set_elem_destroy((const struct nft_set *)arg, ptr);
6b6f302c 327}
97defe1e 328
6b6f302c
TG
329static void nft_hash_destroy(const struct nft_set *set)
330{
745f5450
PM
331 struct nft_hash *priv = nft_set_priv(set);
332
9d098292 333 cancel_delayed_work_sync(&priv->gc_work);
61edafbb
PM
334 rhashtable_free_and_destroy(&priv->ht, nft_hash_elem_destroy,
335 (void *)set);
96518518
PM
336}
337
c50b960c
PM
338static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
339 struct nft_set_estimate *est)
340{
341 unsigned int esize;
342
343 esize = sizeof(struct nft_hash_elem);
c50b960c 344 if (desc->size) {
745f5450 345 est->size = sizeof(struct nft_hash) +
cfe4a9dd 346 roundup_pow_of_two(desc->size * 4 / 3) *
c50b960c
PM
347 sizeof(struct nft_hash_elem *) +
348 desc->size * esize;
349 } else {
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.
354 */
355 est->size = esize + 2 * sizeof(struct nft_hash_elem *);
356 }
357
358 est->class = NFT_SET_CLASS_O_1;
359
360 return true;
361}
362
20a69341
PM
363static struct nft_set_ops nft_hash_ops __read_mostly = {
364 .privsize = nft_hash_privsize,
fe2811eb 365 .elemsize = offsetof(struct nft_hash_elem, ext),
c50b960c 366 .estimate = nft_hash_estimate,
96518518
PM
367 .init = nft_hash_init,
368 .destroy = nft_hash_destroy,
20a69341 369 .insert = nft_hash_insert,
cc02e457
PM
370 .activate = nft_hash_activate,
371 .deactivate = nft_hash_deactivate,
20a69341
PM
372 .remove = nft_hash_remove,
373 .lookup = nft_hash_lookup,
22fe54d5 374 .update = nft_hash_update,
20a69341 375 .walk = nft_hash_walk,
9d098292 376 .features = NFT_SET_MAP | NFT_SET_TIMEOUT,
20a69341 377 .owner = THIS_MODULE,
96518518
PM
378};
379
380static int __init nft_hash_module_init(void)
381{
20a69341 382 return nft_register_set(&nft_hash_ops);
96518518
PM
383}
384
385static void __exit nft_hash_module_exit(void)
386{
20a69341 387 nft_unregister_set(&nft_hash_ops);
96518518
PM
388}
389
390module_init(nft_hash_module_init);
391module_exit(nft_hash_module_exit);
392
393MODULE_LICENSE("GPL");
394MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
20a69341 395MODULE_ALIAS_NFT_SET();
This page took 0.256156 seconds and 5 git commands to generate.