netfilter: nf_tables: support different set binding types
[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;
39 const struct nft_data *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
fe2811eb 65 if (nft_data_cmp(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
20a69341
PM
74static bool nft_hash_lookup(const struct nft_set *set,
75 const struct nft_data *key,
b2832dd6 76 const struct nft_set_ext **ext)
96518518 77{
745f5450 78 struct nft_hash *priv = nft_set_priv(set);
20a69341 79 const struct nft_hash_elem *he;
bfd6e327 80 struct nft_hash_cmp_arg arg = {
cc02e457 81 .genmask = nft_genmask_cur(read_pnet(&set->pnet)),
bfd6e327
PM
82 .set = set,
83 .key = key,
84 };
ce6eb0d7 85
bfd6e327 86 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
b2832dd6
PM
87 if (he != NULL)
88 *ext = &he->ext;
ce6eb0d7 89
cfe4a9dd 90 return !!he;
96518518
PM
91}
92
20a69341
PM
93static int nft_hash_insert(const struct nft_set *set,
94 const struct nft_set_elem *elem)
96518518 95{
745f5450 96 struct nft_hash *priv = nft_set_priv(set);
fe2811eb 97 struct nft_hash_elem *he = elem->priv;
bfd6e327 98 struct nft_hash_cmp_arg arg = {
cc02e457 99 .genmask = nft_genmask_next(read_pnet(&set->pnet)),
bfd6e327
PM
100 .set = set,
101 .key = &elem->key,
102 };
ce6eb0d7 103
fe2811eb
PM
104 return rhashtable_lookup_insert_key(&priv->ht, &arg, &he->node,
105 nft_hash_params);
96518518
PM
106}
107
cc02e457
PM
108static void nft_hash_activate(const struct nft_set *set,
109 const struct nft_set_elem *elem)
96518518 110{
cc02e457 111 struct nft_hash_elem *he = elem->priv;
ce6eb0d7 112
cc02e457 113 nft_set_elem_change_active(set, &he->ext);
9d098292 114 nft_set_elem_clear_busy(&he->ext);
20a69341 115}
96518518 116
cc02e457
PM
117static void *nft_hash_deactivate(const struct nft_set *set,
118 const struct nft_set_elem *elem)
20a69341 119{
745f5450 120 struct nft_hash *priv = nft_set_priv(set);
fa377321 121 struct nft_hash_elem *he;
bfd6e327 122 struct nft_hash_cmp_arg arg = {
cc02e457 123 .genmask = nft_genmask_next(read_pnet(&set->pnet)),
bfd6e327
PM
124 .set = set,
125 .key = &elem->key,
126 };
fa377321 127
9d098292 128 rcu_read_lock();
bfd6e327 129 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
9d098292
PM
130 if (he != NULL) {
131 if (!nft_set_elem_mark_busy(&he->ext))
132 nft_set_elem_change_active(set, &he->ext);
133 else
134 he = NULL;
135 }
136 rcu_read_unlock();
8d24c0b4 137
cc02e457
PM
138 return he;
139}
8d24c0b4 140
cc02e457
PM
141static void nft_hash_remove(const struct nft_set *set,
142 const struct nft_set_elem *elem)
143{
144 struct nft_hash *priv = nft_set_priv(set);
145 struct nft_hash_elem *he = elem->priv;
146
147 rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
96518518
PM
148}
149
20a69341
PM
150static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
151 struct nft_set_iter *iter)
96518518 152{
745f5450 153 struct nft_hash *priv = nft_set_priv(set);
fe2811eb 154 struct nft_hash_elem *he;
9a776628 155 struct rhashtable_iter hti;
20a69341 156 struct nft_set_elem elem;
cc02e457 157 u8 genmask = nft_genmask_cur(read_pnet(&set->pnet));
9a776628 158 int err;
96518518 159
745f5450 160 err = rhashtable_walk_init(&priv->ht, &hti);
9a776628
HX
161 iter->err = err;
162 if (err)
163 return;
88d6ed15 164
9a776628
HX
165 err = rhashtable_walk_start(&hti);
166 if (err && err != -EAGAIN) {
167 iter->err = err;
168 goto out;
169 }
170
171 while ((he = rhashtable_walk_next(&hti))) {
172 if (IS_ERR(he)) {
173 err = PTR_ERR(he);
174 if (err != -EAGAIN) {
175 iter->err = err;
176 goto out;
177 }
d8bdff59
HX
178
179 continue;
9a776628
HX
180 }
181
182 if (iter->count < iter->skip)
183 goto cont;
9d098292
PM
184 if (nft_set_elem_expired(&he->ext))
185 goto cont;
cc02e457
PM
186 if (!nft_set_elem_active(&he->ext, genmask))
187 goto cont;
20a69341 188
fe2811eb 189 elem.priv = he;
9a776628
HX
190
191 iter->err = iter->fn(ctx, set, iter, &elem);
192 if (iter->err < 0)
193 goto out;
20a69341 194
20a69341 195cont:
9a776628 196 iter->count++;
96518518 197 }
9a776628
HX
198
199out:
200 rhashtable_walk_stop(&hti);
201 rhashtable_walk_exit(&hti);
96518518
PM
202}
203
9d098292
PM
204static void nft_hash_gc(struct work_struct *work)
205{
3dd0673a 206 struct nft_set *set;
9d098292
PM
207 struct nft_hash_elem *he;
208 struct nft_hash *priv;
209 struct nft_set_gc_batch *gcb = NULL;
210 struct rhashtable_iter hti;
211 int err;
212
213 priv = container_of(work, struct nft_hash, gc_work.work);
214 set = nft_set_container_of(priv);
215
216 err = rhashtable_walk_init(&priv->ht, &hti);
217 if (err)
218 goto schedule;
219
220 err = rhashtable_walk_start(&hti);
221 if (err && err != -EAGAIN)
222 goto out;
223
224 while ((he = rhashtable_walk_next(&hti))) {
225 if (IS_ERR(he)) {
226 if (PTR_ERR(he) != -EAGAIN)
227 goto out;
228 continue;
229 }
230
231 if (!nft_set_elem_expired(&he->ext))
232 continue;
233 if (nft_set_elem_mark_busy(&he->ext))
234 continue;
235
236 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
237 if (gcb == NULL)
238 goto out;
239 rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
3dd0673a 240 atomic_dec(&set->nelems);
9d098292
PM
241 nft_set_gc_batch_add(gcb, he);
242 }
243out:
244 rhashtable_walk_stop(&hti);
245 rhashtable_walk_exit(&hti);
246
247 nft_set_gc_batch_complete(gcb);
248schedule:
249 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
250 nft_set_gc_interval(set));
251}
252
20a69341
PM
253static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
254{
745f5450 255 return sizeof(struct nft_hash);
cfe4a9dd
TG
256}
257
fa377321 258static const struct rhashtable_params nft_hash_params = {
45d84751 259 .head_offset = offsetof(struct nft_hash_elem, node),
bfd6e327
PM
260 .hashfn = nft_hash_key,
261 .obj_hashfn = nft_hash_obj,
262 .obj_cmpfn = nft_hash_cmp,
45d84751 263 .automatic_shrinking = true,
fa377321
HX
264};
265
20a69341 266static int nft_hash_init(const struct nft_set *set,
c50b960c 267 const struct nft_set_desc *desc,
96518518
PM
268 const struct nlattr * const tb[])
269{
745f5450 270 struct nft_hash *priv = nft_set_priv(set);
fa377321 271 struct rhashtable_params params = nft_hash_params;
9d098292 272 int err;
fa377321
HX
273
274 params.nelem_hint = desc->size ?: NFT_HASH_ELEMENT_HINT;
45d84751 275 params.key_len = set->klen;
96518518 276
9d098292
PM
277 err = rhashtable_init(&priv->ht, &params);
278 if (err < 0)
279 return err;
280
281 INIT_DEFERRABLE_WORK(&priv->gc_work, nft_hash_gc);
282 if (set->flags & NFT_SET_TIMEOUT)
283 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
284 nft_set_gc_interval(set));
285 return 0;
96518518
PM
286}
287
61edafbb 288static void nft_hash_elem_destroy(void *ptr, void *arg)
96518518 289{
61edafbb 290 nft_set_elem_destroy((const struct nft_set *)arg, ptr);
6b6f302c 291}
97defe1e 292
6b6f302c
TG
293static void nft_hash_destroy(const struct nft_set *set)
294{
745f5450
PM
295 struct nft_hash *priv = nft_set_priv(set);
296
9d098292 297 cancel_delayed_work_sync(&priv->gc_work);
61edafbb
PM
298 rhashtable_free_and_destroy(&priv->ht, nft_hash_elem_destroy,
299 (void *)set);
96518518
PM
300}
301
c50b960c
PM
302static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
303 struct nft_set_estimate *est)
304{
305 unsigned int esize;
306
307 esize = sizeof(struct nft_hash_elem);
c50b960c 308 if (desc->size) {
745f5450 309 est->size = sizeof(struct nft_hash) +
cfe4a9dd 310 roundup_pow_of_two(desc->size * 4 / 3) *
c50b960c
PM
311 sizeof(struct nft_hash_elem *) +
312 desc->size * esize;
313 } else {
314 /* Resizing happens when the load drops below 30% or goes
315 * above 75%. The average of 52.5% load (approximated by 50%)
316 * is used for the size estimation of the hash buckets,
317 * meaning we calculate two buckets per element.
318 */
319 est->size = esize + 2 * sizeof(struct nft_hash_elem *);
320 }
321
322 est->class = NFT_SET_CLASS_O_1;
323
324 return true;
325}
326
20a69341
PM
327static struct nft_set_ops nft_hash_ops __read_mostly = {
328 .privsize = nft_hash_privsize,
fe2811eb 329 .elemsize = offsetof(struct nft_hash_elem, ext),
c50b960c 330 .estimate = nft_hash_estimate,
96518518
PM
331 .init = nft_hash_init,
332 .destroy = nft_hash_destroy,
20a69341 333 .insert = nft_hash_insert,
cc02e457
PM
334 .activate = nft_hash_activate,
335 .deactivate = nft_hash_deactivate,
20a69341
PM
336 .remove = nft_hash_remove,
337 .lookup = nft_hash_lookup,
338 .walk = nft_hash_walk,
9d098292 339 .features = NFT_SET_MAP | NFT_SET_TIMEOUT,
20a69341 340 .owner = THIS_MODULE,
96518518
PM
341};
342
343static int __init nft_hash_module_init(void)
344{
20a69341 345 return nft_register_set(&nft_hash_ops);
96518518
PM
346}
347
348static void __exit nft_hash_module_exit(void)
349{
20a69341 350 nft_unregister_set(&nft_hash_ops);
96518518
PM
351}
352
353module_init(nft_hash_module_init);
354module_exit(nft_hash_module_exit);
355
356MODULE_LICENSE("GPL");
357MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
20a69341 358MODULE_ALIAS_NFT_SET();
This page took 0.129783 seconds and 5 git commands to generate.