GNU Linux-libre 4.9.326-gnu1
[releases.git] / net / netfilter / nft_set_hash.c
1 /*
2  * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
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>
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>
23
24 /* We target a hash table size of 4, element hint is 75% of final size */
25 #define NFT_HASH_ELEMENT_HINT 3
26
27 struct nft_hash {
28         struct rhashtable               ht;
29         struct delayed_work             gc_work;
30 };
31
32 struct nft_hash_elem {
33         struct rhash_head               node;
34         struct nft_set_ext              ext;
35 };
36
37 struct nft_hash_cmp_arg {
38         const struct nft_set            *set;
39         const u32                       *key;
40         u8                              genmask;
41 };
42
43 static const struct rhashtable_params nft_hash_params;
44
45 static 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
52 static inline u32 nft_hash_obj(const void *data, u32 len, u32 seed)
53 {
54         const struct nft_hash_elem *he = data;
55
56         return jhash(nft_set_ext_key(&he->ext), len, seed);
57 }
58
59 static 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
65         if (memcmp(nft_set_ext_key(&he->ext), x->key, x->set->klen))
66                 return 1;
67         if (nft_set_elem_expired(&he->ext))
68                 return 1;
69         if (!nft_set_elem_active(&he->ext, x->genmask))
70                 return 1;
71         return 0;
72 }
73
74 static bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
75                             const u32 *key, const struct nft_set_ext **ext)
76 {
77         struct nft_hash *priv = nft_set_priv(set);
78         const struct nft_hash_elem *he;
79         struct nft_hash_cmp_arg arg = {
80                 .genmask = nft_genmask_cur(net),
81                 .set     = set,
82                 .key     = key,
83         };
84
85         he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
86         if (he != NULL)
87                 *ext = &he->ext;
88
89         return !!he;
90 }
91
92 static bool nft_hash_update(struct nft_set *set, const u32 *key,
93                             void *(*new)(struct nft_set *,
94                                          const struct nft_expr *,
95                                          struct nft_regs *regs),
96                             const struct nft_expr *expr,
97                             struct nft_regs *regs,
98                             const struct nft_set_ext **ext)
99 {
100         struct nft_hash *priv = nft_set_priv(set);
101         struct nft_hash_elem *he, *prev;
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
112         he = new(set, expr, regs);
113         if (he == NULL)
114                 goto err1;
115
116         prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
117                                                 nft_hash_params);
118         if (IS_ERR(prev))
119                 goto err2;
120
121         /* Another cpu may race to insert the element with the same key */
122         if (prev) {
123                 nft_set_elem_destroy(set, he, true);
124                 atomic_dec(&set->nelems);
125                 he = prev;
126         }
127
128 out:
129         *ext = &he->ext;
130         return true;
131
132 err2:
133         nft_set_elem_destroy(set, he, true);
134         atomic_dec(&set->nelems);
135 err1:
136         return false;
137 }
138
139 static int nft_hash_insert(const struct net *net, const struct nft_set *set,
140                            const struct nft_set_elem *elem,
141                            struct nft_set_ext **ext)
142 {
143         struct nft_hash *priv = nft_set_priv(set);
144         struct nft_hash_elem *he = elem->priv;
145         struct nft_hash_cmp_arg arg = {
146                 .genmask = nft_genmask_next(net),
147                 .set     = set,
148                 .key     = elem->key.val.data,
149         };
150         struct nft_hash_elem *prev;
151
152         prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
153                                                nft_hash_params);
154         if (IS_ERR(prev))
155                 return PTR_ERR(prev);
156         if (prev) {
157                 *ext = &prev->ext;
158                 return -EEXIST;
159         }
160         return 0;
161 }
162
163 static void nft_hash_activate(const struct net *net, const struct nft_set *set,
164                               const struct nft_set_elem *elem)
165 {
166         struct nft_hash_elem *he = elem->priv;
167
168         nft_set_elem_change_active(net, set, &he->ext);
169         nft_set_elem_clear_busy(&he->ext);
170 }
171
172 static void *nft_hash_deactivate(const struct net *net,
173                                  const struct nft_set *set,
174                                  const struct nft_set_elem *elem)
175 {
176         struct nft_hash *priv = nft_set_priv(set);
177         struct nft_hash_elem *he;
178         struct nft_hash_cmp_arg arg = {
179                 .genmask = nft_genmask_next(net),
180                 .set     = set,
181                 .key     = elem->key.val.data,
182         };
183
184         rcu_read_lock();
185         he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
186         if (he != NULL) {
187                 if (!nft_set_elem_mark_busy(&he->ext) ||
188                     !nft_is_active(net, &he->ext))
189                         nft_set_elem_change_active(net, set, &he->ext);
190                 else
191                         he = NULL;
192         }
193         rcu_read_unlock();
194
195         return he;
196 }
197
198 static void nft_hash_remove(const struct nft_set *set,
199                             const struct nft_set_elem *elem)
200 {
201         struct nft_hash *priv = nft_set_priv(set);
202         struct nft_hash_elem *he = elem->priv;
203
204         rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
205 }
206
207 static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
208                           struct nft_set_iter *iter)
209 {
210         struct nft_hash *priv = nft_set_priv(set);
211         struct nft_hash_elem *he;
212         struct rhashtable_iter hti;
213         struct nft_set_elem elem;
214         int err;
215
216         err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL);
217         iter->err = err;
218         if (err)
219                 return;
220
221         err = rhashtable_walk_start(&hti);
222         if (err && err != -EAGAIN) {
223                 iter->err = err;
224                 goto out;
225         }
226
227         while ((he = rhashtable_walk_next(&hti))) {
228                 if (IS_ERR(he)) {
229                         err = PTR_ERR(he);
230                         if (err != -EAGAIN) {
231                                 iter->err = err;
232                                 goto out;
233                         }
234
235                         continue;
236                 }
237
238                 if (iter->count < iter->skip)
239                         goto cont;
240                 if (nft_set_elem_expired(&he->ext))
241                         goto cont;
242                 if (!nft_set_elem_active(&he->ext, iter->genmask))
243                         goto cont;
244
245                 elem.priv = he;
246
247                 iter->err = iter->fn(ctx, set, iter, &elem);
248                 if (iter->err < 0)
249                         goto out;
250
251 cont:
252                 iter->count++;
253         }
254
255 out:
256         rhashtable_walk_stop(&hti);
257         rhashtable_walk_exit(&hti);
258 }
259
260 static void nft_hash_gc(struct work_struct *work)
261 {
262         struct nft_set *set;
263         struct nft_hash_elem *he;
264         struct nft_hash *priv;
265         struct nft_set_gc_batch *gcb = NULL;
266         struct rhashtable_iter hti;
267         int err;
268
269         priv = container_of(work, struct nft_hash, gc_work.work);
270         set  = nft_set_container_of(priv);
271
272         err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL);
273         if (err)
274                 goto schedule;
275
276         err = rhashtable_walk_start(&hti);
277         if (err && err != -EAGAIN)
278                 goto out;
279
280         while ((he = rhashtable_walk_next(&hti))) {
281                 if (IS_ERR(he)) {
282                         if (PTR_ERR(he) != -EAGAIN)
283                                 goto out;
284                         continue;
285                 }
286
287                 if (!nft_set_elem_expired(&he->ext))
288                         continue;
289                 if (nft_set_elem_mark_busy(&he->ext))
290                         continue;
291
292                 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
293                 if (gcb == NULL)
294                         goto out;
295                 rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
296                 atomic_dec(&set->nelems);
297                 nft_set_gc_batch_add(gcb, he);
298         }
299 out:
300         rhashtable_walk_stop(&hti);
301         rhashtable_walk_exit(&hti);
302
303         nft_set_gc_batch_complete(gcb);
304 schedule:
305         queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
306                            nft_set_gc_interval(set));
307 }
308
309 static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
310 {
311         return sizeof(struct nft_hash);
312 }
313
314 static const struct rhashtable_params nft_hash_params = {
315         .head_offset            = offsetof(struct nft_hash_elem, node),
316         .hashfn                 = nft_hash_key,
317         .obj_hashfn             = nft_hash_obj,
318         .obj_cmpfn              = nft_hash_cmp,
319         .automatic_shrinking    = true,
320 };
321
322 static int nft_hash_init(const struct nft_set *set,
323                          const struct nft_set_desc *desc,
324                          const struct nlattr * const tb[])
325 {
326         struct nft_hash *priv = nft_set_priv(set);
327         struct rhashtable_params params = nft_hash_params;
328         int err;
329
330         params.nelem_hint = desc->size ?: NFT_HASH_ELEMENT_HINT;
331         params.key_len    = set->klen;
332
333         err = rhashtable_init(&priv->ht, &params);
334         if (err < 0)
335                 return err;
336
337         INIT_DEFERRABLE_WORK(&priv->gc_work, nft_hash_gc);
338         if (set->flags & NFT_SET_TIMEOUT)
339                 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
340                                    nft_set_gc_interval(set));
341         return 0;
342 }
343
344 static void nft_hash_elem_destroy(void *ptr, void *arg)
345 {
346         nft_set_elem_destroy((const struct nft_set *)arg, ptr, true);
347 }
348
349 static void nft_hash_destroy(const struct nft_set *set)
350 {
351         struct nft_hash *priv = nft_set_priv(set);
352
353         cancel_delayed_work_sync(&priv->gc_work);
354         rhashtable_free_and_destroy(&priv->ht, nft_hash_elem_destroy,
355                                     (void *)set);
356 }
357
358 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
359                               struct nft_set_estimate *est)
360 {
361         unsigned int esize;
362
363         esize = sizeof(struct nft_hash_elem);
364         if (desc->size) {
365                 est->size = sizeof(struct nft_hash) +
366                             roundup_pow_of_two(desc->size * 4 / 3) *
367                             sizeof(struct nft_hash_elem *) +
368                             desc->size * esize;
369         } else {
370                 /* Resizing happens when the load drops below 30% or goes
371                  * above 75%. The average of 52.5% load (approximated by 50%)
372                  * is used for the size estimation of the hash buckets,
373                  * meaning we calculate two buckets per element.
374                  */
375                 est->size = esize + 2 * sizeof(struct nft_hash_elem *);
376         }
377
378         est->class = NFT_SET_CLASS_O_1;
379
380         return true;
381 }
382
383 static struct nft_set_ops nft_hash_ops __read_mostly = {
384         .privsize       = nft_hash_privsize,
385         .elemsize       = offsetof(struct nft_hash_elem, ext),
386         .estimate       = nft_hash_estimate,
387         .init           = nft_hash_init,
388         .destroy        = nft_hash_destroy,
389         .insert         = nft_hash_insert,
390         .activate       = nft_hash_activate,
391         .deactivate     = nft_hash_deactivate,
392         .remove         = nft_hash_remove,
393         .lookup         = nft_hash_lookup,
394         .update         = nft_hash_update,
395         .walk           = nft_hash_walk,
396         .features       = NFT_SET_MAP | NFT_SET_TIMEOUT,
397         .owner          = THIS_MODULE,
398 };
399
400 static int __init nft_hash_module_init(void)
401 {
402         return nft_register_set(&nft_hash_ops);
403 }
404
405 static void __exit nft_hash_module_exit(void)
406 {
407         nft_unregister_set(&nft_hash_ops);
408 }
409
410 module_init(nft_hash_module_init);
411 module_exit(nft_hash_module_exit);
412
413 MODULE_LICENSE("GPL");
414 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
415 MODULE_ALIAS_NFT_SET();