Commit | Line | Data |
---|---|---|
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 |
27 | struct nft_hash { |
28 | struct rhashtable ht; | |
9d098292 | 29 | struct delayed_work gc_work; |
745f5450 PM |
30 | }; |
31 | ||
96518518 | 32 | struct nft_hash_elem { |
cfe4a9dd | 33 | struct rhash_head node; |
fe2811eb | 34 | struct nft_set_ext ext; |
96518518 PM |
35 | }; |
36 | ||
bfd6e327 PM |
37 | struct 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 |
43 | static const struct rhashtable_params nft_hash_params; |
44 | ||
bfd6e327 PM |
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 | ||
fe2811eb | 56 | return jhash(nft_set_ext_key(&he->ext), len, seed); |
bfd6e327 PM |
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 | ||
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 |
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) | |
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 | 92 | static 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; | |
118 | out: | |
119 | *ext = &he->ext; | |
120 | return true; | |
121 | ||
122 | err2: | |
123 | nft_set_elem_destroy(set, he); | |
124 | err1: | |
125 | return false; | |
126 | } | |
127 | ||
42a55769 | 128 | static int nft_hash_insert(const struct net *net, const struct nft_set *set, |
c016c7e4 PNA |
129 | const struct nft_set_elem *elem, |
130 | struct nft_set_ext **ext) | |
96518518 | 131 | { |
745f5450 | 132 | struct nft_hash *priv = nft_set_priv(set); |
fe2811eb | 133 | struct nft_hash_elem *he = elem->priv; |
bfd6e327 | 134 | struct nft_hash_cmp_arg arg = { |
42a55769 | 135 | .genmask = nft_genmask_next(net), |
bfd6e327 | 136 | .set = set, |
7d740264 | 137 | .key = elem->key.val.data, |
bfd6e327 | 138 | }; |
c016c7e4 PNA |
139 | struct nft_hash_elem *prev; |
140 | ||
141 | prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node, | |
142 | nft_hash_params); | |
143 | if (IS_ERR(prev)) | |
144 | return PTR_ERR(prev); | |
145 | if (prev) { | |
146 | *ext = &prev->ext; | |
147 | return -EEXIST; | |
148 | } | |
149 | return 0; | |
96518518 PM |
150 | } |
151 | ||
42a55769 | 152 | static void nft_hash_activate(const struct net *net, const struct nft_set *set, |
cc02e457 | 153 | const struct nft_set_elem *elem) |
96518518 | 154 | { |
cc02e457 | 155 | struct nft_hash_elem *he = elem->priv; |
ce6eb0d7 | 156 | |
42a55769 | 157 | nft_set_elem_change_active(net, set, &he->ext); |
9d098292 | 158 | nft_set_elem_clear_busy(&he->ext); |
20a69341 | 159 | } |
96518518 | 160 | |
42a55769 PNA |
161 | static void *nft_hash_deactivate(const struct net *net, |
162 | const struct nft_set *set, | |
cc02e457 | 163 | const struct nft_set_elem *elem) |
20a69341 | 164 | { |
745f5450 | 165 | struct nft_hash *priv = nft_set_priv(set); |
fa377321 | 166 | struct nft_hash_elem *he; |
bfd6e327 | 167 | struct nft_hash_cmp_arg arg = { |
8eee54be | 168 | .genmask = nft_genmask_next(net), |
bfd6e327 | 169 | .set = set, |
7d740264 | 170 | .key = elem->key.val.data, |
bfd6e327 | 171 | }; |
fa377321 | 172 | |
9d098292 | 173 | rcu_read_lock(); |
bfd6e327 | 174 | he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params); |
9d098292 | 175 | if (he != NULL) { |
8eee54be PNA |
176 | if (!nft_set_elem_mark_busy(&he->ext) || |
177 | !nft_is_active(net, &he->ext)) | |
42a55769 | 178 | nft_set_elem_change_active(net, set, &he->ext); |
9d098292 PM |
179 | else |
180 | he = NULL; | |
181 | } | |
182 | rcu_read_unlock(); | |
8d24c0b4 | 183 | |
cc02e457 PM |
184 | return he; |
185 | } | |
8d24c0b4 | 186 | |
cc02e457 PM |
187 | static void nft_hash_remove(const struct nft_set *set, |
188 | const struct nft_set_elem *elem) | |
189 | { | |
190 | struct nft_hash *priv = nft_set_priv(set); | |
191 | struct nft_hash_elem *he = elem->priv; | |
192 | ||
193 | rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params); | |
96518518 PM |
194 | } |
195 | ||
20a69341 PM |
196 | static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set, |
197 | struct nft_set_iter *iter) | |
96518518 | 198 | { |
745f5450 | 199 | struct nft_hash *priv = nft_set_priv(set); |
fe2811eb | 200 | struct nft_hash_elem *he; |
9a776628 | 201 | struct rhashtable_iter hti; |
20a69341 | 202 | struct nft_set_elem elem; |
9a776628 | 203 | int err; |
96518518 | 204 | |
8f6fd83c | 205 | err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL); |
9a776628 HX |
206 | iter->err = err; |
207 | if (err) | |
208 | return; | |
88d6ed15 | 209 | |
9a776628 HX |
210 | err = rhashtable_walk_start(&hti); |
211 | if (err && err != -EAGAIN) { | |
212 | iter->err = err; | |
213 | goto out; | |
214 | } | |
215 | ||
216 | while ((he = rhashtable_walk_next(&hti))) { | |
217 | if (IS_ERR(he)) { | |
218 | err = PTR_ERR(he); | |
219 | if (err != -EAGAIN) { | |
220 | iter->err = err; | |
221 | goto out; | |
222 | } | |
d8bdff59 HX |
223 | |
224 | continue; | |
9a776628 HX |
225 | } |
226 | ||
227 | if (iter->count < iter->skip) | |
228 | goto cont; | |
9d098292 PM |
229 | if (nft_set_elem_expired(&he->ext)) |
230 | goto cont; | |
8588ac09 | 231 | if (!nft_set_elem_active(&he->ext, iter->genmask)) |
cc02e457 | 232 | goto cont; |
20a69341 | 233 | |
fe2811eb | 234 | elem.priv = he; |
9a776628 HX |
235 | |
236 | iter->err = iter->fn(ctx, set, iter, &elem); | |
237 | if (iter->err < 0) | |
238 | goto out; | |
20a69341 | 239 | |
20a69341 | 240 | cont: |
9a776628 | 241 | iter->count++; |
96518518 | 242 | } |
9a776628 HX |
243 | |
244 | out: | |
245 | rhashtable_walk_stop(&hti); | |
246 | rhashtable_walk_exit(&hti); | |
96518518 PM |
247 | } |
248 | ||
9d098292 PM |
249 | static void nft_hash_gc(struct work_struct *work) |
250 | { | |
3dd0673a | 251 | struct nft_set *set; |
9d098292 PM |
252 | struct nft_hash_elem *he; |
253 | struct nft_hash *priv; | |
254 | struct nft_set_gc_batch *gcb = NULL; | |
255 | struct rhashtable_iter hti; | |
256 | int err; | |
257 | ||
258 | priv = container_of(work, struct nft_hash, gc_work.work); | |
259 | set = nft_set_container_of(priv); | |
260 | ||
8f6fd83c | 261 | err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL); |
9d098292 PM |
262 | if (err) |
263 | goto schedule; | |
264 | ||
265 | err = rhashtable_walk_start(&hti); | |
266 | if (err && err != -EAGAIN) | |
267 | goto out; | |
268 | ||
269 | while ((he = rhashtable_walk_next(&hti))) { | |
270 | if (IS_ERR(he)) { | |
271 | if (PTR_ERR(he) != -EAGAIN) | |
272 | goto out; | |
273 | continue; | |
274 | } | |
275 | ||
276 | if (!nft_set_elem_expired(&he->ext)) | |
277 | continue; | |
278 | if (nft_set_elem_mark_busy(&he->ext)) | |
279 | continue; | |
280 | ||
281 | gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC); | |
282 | if (gcb == NULL) | |
283 | goto out; | |
284 | rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params); | |
3dd0673a | 285 | atomic_dec(&set->nelems); |
9d098292 PM |
286 | nft_set_gc_batch_add(gcb, he); |
287 | } | |
288 | out: | |
289 | rhashtable_walk_stop(&hti); | |
290 | rhashtable_walk_exit(&hti); | |
291 | ||
292 | nft_set_gc_batch_complete(gcb); | |
293 | schedule: | |
294 | queue_delayed_work(system_power_efficient_wq, &priv->gc_work, | |
295 | nft_set_gc_interval(set)); | |
296 | } | |
297 | ||
20a69341 PM |
298 | static unsigned int nft_hash_privsize(const struct nlattr * const nla[]) |
299 | { | |
745f5450 | 300 | return sizeof(struct nft_hash); |
cfe4a9dd TG |
301 | } |
302 | ||
fa377321 | 303 | static const struct rhashtable_params nft_hash_params = { |
45d84751 | 304 | .head_offset = offsetof(struct nft_hash_elem, node), |
bfd6e327 PM |
305 | .hashfn = nft_hash_key, |
306 | .obj_hashfn = nft_hash_obj, | |
307 | .obj_cmpfn = nft_hash_cmp, | |
45d84751 | 308 | .automatic_shrinking = true, |
fa377321 HX |
309 | }; |
310 | ||
20a69341 | 311 | static int nft_hash_init(const struct nft_set *set, |
c50b960c | 312 | const struct nft_set_desc *desc, |
96518518 PM |
313 | const struct nlattr * const tb[]) |
314 | { | |
745f5450 | 315 | struct nft_hash *priv = nft_set_priv(set); |
fa377321 | 316 | struct rhashtable_params params = nft_hash_params; |
9d098292 | 317 | int err; |
fa377321 HX |
318 | |
319 | params.nelem_hint = desc->size ?: NFT_HASH_ELEMENT_HINT; | |
45d84751 | 320 | params.key_len = set->klen; |
96518518 | 321 | |
9d098292 PM |
322 | err = rhashtable_init(&priv->ht, ¶ms); |
323 | if (err < 0) | |
324 | return err; | |
325 | ||
326 | INIT_DEFERRABLE_WORK(&priv->gc_work, nft_hash_gc); | |
327 | if (set->flags & NFT_SET_TIMEOUT) | |
328 | queue_delayed_work(system_power_efficient_wq, &priv->gc_work, | |
329 | nft_set_gc_interval(set)); | |
330 | return 0; | |
96518518 PM |
331 | } |
332 | ||
61edafbb | 333 | static void nft_hash_elem_destroy(void *ptr, void *arg) |
96518518 | 334 | { |
61edafbb | 335 | nft_set_elem_destroy((const struct nft_set *)arg, ptr); |
6b6f302c | 336 | } |
97defe1e | 337 | |
6b6f302c TG |
338 | static void nft_hash_destroy(const struct nft_set *set) |
339 | { | |
745f5450 PM |
340 | struct nft_hash *priv = nft_set_priv(set); |
341 | ||
9d098292 | 342 | cancel_delayed_work_sync(&priv->gc_work); |
61edafbb PM |
343 | rhashtable_free_and_destroy(&priv->ht, nft_hash_elem_destroy, |
344 | (void *)set); | |
96518518 PM |
345 | } |
346 | ||
c50b960c PM |
347 | static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, |
348 | struct nft_set_estimate *est) | |
349 | { | |
350 | unsigned int esize; | |
351 | ||
352 | esize = sizeof(struct nft_hash_elem); | |
c50b960c | 353 | if (desc->size) { |
745f5450 | 354 | est->size = sizeof(struct nft_hash) + |
cfe4a9dd | 355 | roundup_pow_of_two(desc->size * 4 / 3) * |
c50b960c PM |
356 | sizeof(struct nft_hash_elem *) + |
357 | desc->size * esize; | |
358 | } else { | |
359 | /* Resizing happens when the load drops below 30% or goes | |
360 | * above 75%. The average of 52.5% load (approximated by 50%) | |
361 | * is used for the size estimation of the hash buckets, | |
362 | * meaning we calculate two buckets per element. | |
363 | */ | |
364 | est->size = esize + 2 * sizeof(struct nft_hash_elem *); | |
365 | } | |
366 | ||
367 | est->class = NFT_SET_CLASS_O_1; | |
368 | ||
369 | return true; | |
370 | } | |
371 | ||
20a69341 PM |
372 | static struct nft_set_ops nft_hash_ops __read_mostly = { |
373 | .privsize = nft_hash_privsize, | |
fe2811eb | 374 | .elemsize = offsetof(struct nft_hash_elem, ext), |
c50b960c | 375 | .estimate = nft_hash_estimate, |
96518518 PM |
376 | .init = nft_hash_init, |
377 | .destroy = nft_hash_destroy, | |
20a69341 | 378 | .insert = nft_hash_insert, |
cc02e457 PM |
379 | .activate = nft_hash_activate, |
380 | .deactivate = nft_hash_deactivate, | |
20a69341 PM |
381 | .remove = nft_hash_remove, |
382 | .lookup = nft_hash_lookup, | |
22fe54d5 | 383 | .update = nft_hash_update, |
20a69341 | 384 | .walk = nft_hash_walk, |
9d098292 | 385 | .features = NFT_SET_MAP | NFT_SET_TIMEOUT, |
20a69341 | 386 | .owner = THIS_MODULE, |
96518518 PM |
387 | }; |
388 | ||
389 | static int __init nft_hash_module_init(void) | |
390 | { | |
20a69341 | 391 | return nft_register_set(&nft_hash_ops); |
96518518 PM |
392 | } |
393 | ||
394 | static void __exit nft_hash_module_exit(void) | |
395 | { | |
20a69341 | 396 | nft_unregister_set(&nft_hash_ops); |
96518518 PM |
397 | } |
398 | ||
399 | module_init(nft_hash_module_init); | |
400 | module_exit(nft_hash_module_exit); | |
401 | ||
402 | MODULE_LICENSE("GPL"); | |
403 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
20a69341 | 404 | MODULE_ALIAS_NFT_SET(); |