Blame view
net/netfilter/nft_set_hash.c
17.5 KB
d2912cb15
|
1 |
// SPDX-License-Identifier: GPL-2.0-only |
96518518c
|
2 |
/* |
ce6eb0d7c
|
3 |
* Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> |
96518518c
|
4 |
* |
96518518c
|
5 6 7 8 9 10 11 |
* Development of this code funded by Astaro AG (http://www.astaro.com/) */ #include <linux/kernel.h> #include <linux/init.h> #include <linux/module.h> #include <linux/list.h> |
c50b960cc
|
12 |
#include <linux/log2.h> |
96518518c
|
13 14 |
#include <linux/jhash.h> #include <linux/netlink.h> |
9d0982927
|
15 |
#include <linux/workqueue.h> |
cfe4a9dda
|
16 |
#include <linux/rhashtable.h> |
96518518c
|
17 18 |
#include <linux/netfilter.h> #include <linux/netfilter/nf_tables.h> |
5785cf15f
|
19 |
#include <net/netfilter/nf_tables_core.h> |
96518518c
|
20 |
|
cfe4a9dda
|
21 |
/* We target a hash table size of 4, element hint is 75% of final size */ |
5fc6ced95
|
22 |
#define NFT_RHASH_ELEMENT_HINT 3 |
96518518c
|
23 |
|
5fc6ced95
|
24 |
struct nft_rhash { |
745f5450d
|
25 |
struct rhashtable ht; |
9d0982927
|
26 |
struct delayed_work gc_work; |
745f5450d
|
27 |
}; |
5fc6ced95
|
28 |
struct nft_rhash_elem { |
cfe4a9dda
|
29 |
struct rhash_head node; |
fe2811ebe
|
30 |
struct nft_set_ext ext; |
96518518c
|
31 |
}; |
5fc6ced95
|
32 |
struct nft_rhash_cmp_arg { |
bfd6e327e
|
33 |
const struct nft_set *set; |
8cd8937ac
|
34 |
const u32 *key; |
cc02e457b
|
35 |
u8 genmask; |
bfd6e327e
|
36 |
}; |
5fc6ced95
|
37 |
static inline u32 nft_rhash_key(const void *data, u32 len, u32 seed) |
bfd6e327e
|
38 |
{ |
5fc6ced95
|
39 |
const struct nft_rhash_cmp_arg *arg = data; |
bfd6e327e
|
40 41 42 |
return jhash(arg->key, len, seed); } |
5fc6ced95
|
43 |
static inline u32 nft_rhash_obj(const void *data, u32 len, u32 seed) |
bfd6e327e
|
44 |
{ |
5fc6ced95
|
45 |
const struct nft_rhash_elem *he = data; |
bfd6e327e
|
46 |
|
fe2811ebe
|
47 |
return jhash(nft_set_ext_key(&he->ext), len, seed); |
bfd6e327e
|
48 |
} |
5fc6ced95
|
49 50 |
static inline int nft_rhash_cmp(struct rhashtable_compare_arg *arg, const void *ptr) |
bfd6e327e
|
51 |
{ |
5fc6ced95
|
52 53 |
const struct nft_rhash_cmp_arg *x = arg->key; const struct nft_rhash_elem *he = ptr; |
bfd6e327e
|
54 |
|
e562d860d
|
55 |
if (memcmp(nft_set_ext_key(&he->ext), x->key, x->set->klen)) |
bfd6e327e
|
56 |
return 1; |
9d0982927
|
57 58 |
if (nft_set_elem_expired(&he->ext)) return 1; |
cc02e457b
|
59 60 |
if (!nft_set_elem_active(&he->ext, x->genmask)) return 1; |
bfd6e327e
|
61 62 |
return 0; } |
5fc6ced95
|
63 64 65 66 67 |
static const struct rhashtable_params nft_rhash_params = { .head_offset = offsetof(struct nft_rhash_elem, node), .hashfn = nft_rhash_key, .obj_hashfn = nft_rhash_obj, .obj_cmpfn = nft_rhash_cmp, |
187388bc3
|
68 69 |
.automatic_shrinking = true, }; |
5fc6ced95
|
70 71 |
static bool nft_rhash_lookup(const struct net *net, const struct nft_set *set, const u32 *key, const struct nft_set_ext **ext) |
96518518c
|
72 |
{ |
5fc6ced95
|
73 74 75 |
struct nft_rhash *priv = nft_set_priv(set); const struct nft_rhash_elem *he; struct nft_rhash_cmp_arg arg = { |
42a557691
|
76 |
.genmask = nft_genmask_cur(net), |
bfd6e327e
|
77 78 79 |
.set = set, .key = key, }; |
ce6eb0d7c
|
80 |
|
a2d88182d
|
81 |
he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params); |
b2832dd66
|
82 83 |
if (he != NULL) *ext = &he->ext; |
ce6eb0d7c
|
84 |
|
cfe4a9dda
|
85 |
return !!he; |
96518518c
|
86 |
} |
ba0e4d991
|
87 88 89 90 91 92 93 94 95 96 |
static void *nft_rhash_get(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem, unsigned int flags) { struct nft_rhash *priv = nft_set_priv(set); struct nft_rhash_elem *he; struct nft_rhash_cmp_arg arg = { .genmask = nft_genmask_cur(net), .set = set, .key = elem->key.val.data, }; |
a2d88182d
|
97 |
he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params); |
ba0e4d991
|
98 99 100 101 102 |
if (he != NULL) return he; return ERR_PTR(-ENOENT); } |
5fc6ced95
|
103 104 105 106 107 108 109 |
static bool nft_rhash_update(struct nft_set *set, const u32 *key, void *(*new)(struct nft_set *, const struct nft_expr *, struct nft_regs *regs), const struct nft_expr *expr, struct nft_regs *regs, const struct nft_set_ext **ext) |
22fe54d5f
|
110 |
{ |
5fc6ced95
|
111 112 113 |
struct nft_rhash *priv = nft_set_priv(set); struct nft_rhash_elem *he, *prev; struct nft_rhash_cmp_arg arg = { |
22fe54d5f
|
114 115 116 117 |
.genmask = NFT_GENMASK_ANY, .set = set, .key = key, }; |
a2d88182d
|
118 |
he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params); |
22fe54d5f
|
119 120 |
if (he != NULL) goto out; |
a55e22e92
|
121 |
he = new(set, expr, regs); |
22fe54d5f
|
122 123 |
if (he == NULL) goto err1; |
dab45060a
|
124 125 |
prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node, |
5fc6ced95
|
126 |
nft_rhash_params); |
dab45060a
|
127 |
if (IS_ERR(prev)) |
22fe54d5f
|
128 |
goto err2; |
dab45060a
|
129 130 131 132 133 134 |
/* Another cpu may race to insert the element with the same key */ if (prev) { nft_set_elem_destroy(set, he, true); he = prev; } |
22fe54d5f
|
135 136 137 138 139 |
out: *ext = &he->ext; return true; err2: |
61f9e2924
|
140 |
nft_set_elem_destroy(set, he, true); |
22fe54d5f
|
141 142 143 |
err1: return false; } |
5fc6ced95
|
144 145 146 |
static int nft_rhash_insert(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem, struct nft_set_ext **ext) |
96518518c
|
147 |
{ |
5fc6ced95
|
148 149 150 |
struct nft_rhash *priv = nft_set_priv(set); struct nft_rhash_elem *he = elem->priv; struct nft_rhash_cmp_arg arg = { |
42a557691
|
151 |
.genmask = nft_genmask_next(net), |
bfd6e327e
|
152 |
.set = set, |
7d7402642
|
153 |
.key = elem->key.val.data, |
bfd6e327e
|
154 |
}; |
5fc6ced95
|
155 |
struct nft_rhash_elem *prev; |
c016c7e45
|
156 157 |
prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node, |
5fc6ced95
|
158 |
nft_rhash_params); |
c016c7e45
|
159 160 161 162 163 164 165 |
if (IS_ERR(prev)) return PTR_ERR(prev); if (prev) { *ext = &prev->ext; return -EEXIST; } return 0; |
96518518c
|
166 |
} |
5fc6ced95
|
167 168 |
static void nft_rhash_activate(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem) |
96518518c
|
169 |
{ |
5fc6ced95
|
170 |
struct nft_rhash_elem *he = elem->priv; |
ce6eb0d7c
|
171 |
|
42a557691
|
172 |
nft_set_elem_change_active(net, set, &he->ext); |
9d0982927
|
173 |
nft_set_elem_clear_busy(&he->ext); |
20a69341f
|
174 |
} |
96518518c
|
175 |
|
5fc6ced95
|
176 177 |
static bool nft_rhash_flush(const struct net *net, const struct nft_set *set, void *priv) |
37df5301a
|
178 |
{ |
5fc6ced95
|
179 |
struct nft_rhash_elem *he = priv; |
37df5301a
|
180 181 182 183 184 185 186 187 |
if (!nft_set_elem_mark_busy(&he->ext) || !nft_is_active(net, &he->ext)) { nft_set_elem_change_active(net, set, &he->ext); return true; } return false; } |
5fc6ced95
|
188 189 190 |
static void *nft_rhash_deactivate(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem) |
20a69341f
|
191 |
{ |
5fc6ced95
|
192 193 194 |
struct nft_rhash *priv = nft_set_priv(set); struct nft_rhash_elem *he; struct nft_rhash_cmp_arg arg = { |
8eee54be7
|
195 |
.genmask = nft_genmask_next(net), |
bfd6e327e
|
196 |
.set = set, |
7d7402642
|
197 |
.key = elem->key.val.data, |
bfd6e327e
|
198 |
}; |
fa3773211
|
199 |
|
9d0982927
|
200 |
rcu_read_lock(); |
a2d88182d
|
201 |
he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params); |
37df5301a
|
202 |
if (he != NULL && |
5fc6ced95
|
203 |
!nft_rhash_flush(net, set, he)) |
37df5301a
|
204 |
he = NULL; |
9d0982927
|
205 |
rcu_read_unlock(); |
8d24c0b43
|
206 |
|
cc02e457b
|
207 208 |
return he; } |
8d24c0b43
|
209 |
|
5fc6ced95
|
210 211 212 |
static void nft_rhash_remove(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem) |
cc02e457b
|
213 |
{ |
5fc6ced95
|
214 215 |
struct nft_rhash *priv = nft_set_priv(set); struct nft_rhash_elem *he = elem->priv; |
cc02e457b
|
216 |
|
5fc6ced95
|
217 |
rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params); |
96518518c
|
218 |
} |
d0a8d877d
|
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
static bool nft_rhash_delete(const struct nft_set *set, const u32 *key) { struct nft_rhash *priv = nft_set_priv(set); struct nft_rhash_cmp_arg arg = { .genmask = NFT_GENMASK_ANY, .set = set, .key = key, }; struct nft_rhash_elem *he; he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params); if (he == NULL) return false; return rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params) == 0; } |
5fc6ced95
|
236 237 |
static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set, struct nft_set_iter *iter) |
96518518c
|
238 |
{ |
5fc6ced95
|
239 240 |
struct nft_rhash *priv = nft_set_priv(set); struct nft_rhash_elem *he; |
9a7766288
|
241 |
struct rhashtable_iter hti; |
20a69341f
|
242 |
struct nft_set_elem elem; |
88d6ed15a
|
243 |
|
0de22baab
|
244 |
rhashtable_walk_enter(&priv->ht, &hti); |
97a6ec4ac
|
245 |
rhashtable_walk_start(&hti); |
9a7766288
|
246 247 248 |
while ((he = rhashtable_walk_next(&hti))) { if (IS_ERR(he)) { |
0de22baab
|
249 250 251 |
if (PTR_ERR(he) != -EAGAIN) { iter->err = PTR_ERR(he); break; |
9a7766288
|
252 |
} |
d8bdff59c
|
253 254 |
continue; |
9a7766288
|
255 256 257 258 |
} if (iter->count < iter->skip) goto cont; |
9d0982927
|
259 260 |
if (nft_set_elem_expired(&he->ext)) goto cont; |
8588ac097
|
261 |
if (!nft_set_elem_active(&he->ext, iter->genmask)) |
cc02e457b
|
262 |
goto cont; |
20a69341f
|
263 |
|
fe2811ebe
|
264 |
elem.priv = he; |
9a7766288
|
265 266 267 |
iter->err = iter->fn(ctx, set, iter, &elem); if (iter->err < 0) |
0de22baab
|
268 |
break; |
20a69341f
|
269 |
|
20a69341f
|
270 |
cont: |
9a7766288
|
271 |
iter->count++; |
96518518c
|
272 |
} |
9a7766288
|
273 274 |
rhashtable_walk_stop(&hti); rhashtable_walk_exit(&hti); |
96518518c
|
275 |
} |
5fc6ced95
|
276 |
static void nft_rhash_gc(struct work_struct *work) |
9d0982927
|
277 |
{ |
3dd0673ac
|
278 |
struct nft_set *set; |
5fc6ced95
|
279 280 |
struct nft_rhash_elem *he; struct nft_rhash *priv; |
9d0982927
|
281 282 |
struct nft_set_gc_batch *gcb = NULL; struct rhashtable_iter hti; |
9d0982927
|
283 |
|
5fc6ced95
|
284 |
priv = container_of(work, struct nft_rhash, gc_work.work); |
9d0982927
|
285 |
set = nft_set_container_of(priv); |
0de22baab
|
286 |
rhashtable_walk_enter(&priv->ht, &hti); |
97a6ec4ac
|
287 |
rhashtable_walk_start(&hti); |
9d0982927
|
288 289 290 291 |
while ((he = rhashtable_walk_next(&hti))) { if (IS_ERR(he)) { if (PTR_ERR(he) != -EAGAIN) |
0de22baab
|
292 |
break; |
9d0982927
|
293 294 |
continue; } |
79b174ade
|
295 296 297 298 299 300 301 |
if (nft_set_ext_exists(&he->ext, NFT_SET_EXT_EXPR)) { struct nft_expr *expr = nft_set_ext_expr(&he->ext); if (expr->ops->gc && expr->ops->gc(read_pnet(&set->net), expr)) goto gc; } |
9d0982927
|
302 303 |
if (!nft_set_elem_expired(&he->ext)) continue; |
79b174ade
|
304 |
gc: |
9d0982927
|
305 306 307 308 309 |
if (nft_set_elem_mark_busy(&he->ext)) continue; gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC); if (gcb == NULL) |
0de22baab
|
310 |
break; |
5fc6ced95
|
311 |
rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params); |
3dd0673ac
|
312 |
atomic_dec(&set->nelems); |
9d0982927
|
313 314 |
nft_set_gc_batch_add(gcb, he); } |
9d0982927
|
315 316 317 318 |
rhashtable_walk_stop(&hti); rhashtable_walk_exit(&hti); nft_set_gc_batch_complete(gcb); |
9d0982927
|
319 320 321 |
queue_delayed_work(system_power_efficient_wq, &priv->gc_work, nft_set_gc_interval(set)); } |
4ef360dd6
|
322 323 |
static u64 nft_rhash_privsize(const struct nlattr * const nla[], const struct nft_set_desc *desc) |
20a69341f
|
324 |
{ |
5fc6ced95
|
325 |
return sizeof(struct nft_rhash); |
cfe4a9dda
|
326 |
} |
79b174ade
|
327 328 329 330 331 332 333 |
static void nft_rhash_gc_init(const struct nft_set *set) { struct nft_rhash *priv = nft_set_priv(set); queue_delayed_work(system_power_efficient_wq, &priv->gc_work, nft_set_gc_interval(set)); } |
5fc6ced95
|
334 335 336 |
static int nft_rhash_init(const struct nft_set *set, const struct nft_set_desc *desc, const struct nlattr * const tb[]) |
96518518c
|
337 |
{ |
5fc6ced95
|
338 339 |
struct nft_rhash *priv = nft_set_priv(set); struct rhashtable_params params = nft_rhash_params; |
9d0982927
|
340 |
int err; |
fa3773211
|
341 |
|
5fc6ced95
|
342 |
params.nelem_hint = desc->size ?: NFT_RHASH_ELEMENT_HINT; |
45d84751f
|
343 |
params.key_len = set->klen; |
96518518c
|
344 |
|
9d0982927
|
345 346 347 |
err = rhashtable_init(&priv->ht, ¶ms); if (err < 0) return err; |
5fc6ced95
|
348 |
INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc); |
9d0982927
|
349 |
if (set->flags & NFT_SET_TIMEOUT) |
79b174ade
|
350 |
nft_rhash_gc_init(set); |
9d0982927
|
351 |
return 0; |
96518518c
|
352 |
} |
5fc6ced95
|
353 |
static void nft_rhash_elem_destroy(void *ptr, void *arg) |
96518518c
|
354 |
{ |
68ad546ae
|
355 |
nft_set_elem_destroy(arg, ptr, true); |
6b6f302ce
|
356 |
} |
97defe1ec
|
357 |
|
5fc6ced95
|
358 |
static void nft_rhash_destroy(const struct nft_set *set) |
6b6f302ce
|
359 |
{ |
5fc6ced95
|
360 |
struct nft_rhash *priv = nft_set_priv(set); |
745f5450d
|
361 |
|
9d0982927
|
362 |
cancel_delayed_work_sync(&priv->gc_work); |
9970a8e40
|
363 |
rcu_barrier(); |
5fc6ced95
|
364 |
rhashtable_free_and_destroy(&priv->ht, nft_rhash_elem_destroy, |
61edafbb4
|
365 |
(void *)set); |
96518518c
|
366 |
} |
2111515ab
|
367 368 369 370 |
static u32 nft_hash_buckets(u32 size) { return roundup_pow_of_two(size * 4 / 3); } |
5fc6ced95
|
371 372 |
static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features, struct nft_set_estimate *est) |
c50b960cc
|
373 |
{ |
6c03ae210
|
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
est->size = ~0; est->lookup = NFT_SET_CLASS_O_1; est->space = NFT_SET_CLASS_O_N; return true; } struct nft_hash { u32 seed; u32 buckets; struct hlist_head table[]; }; struct nft_hash_elem { struct hlist_node node; struct nft_set_ext ext; }; static bool nft_hash_lookup(const struct net *net, const struct nft_set *set, const u32 *key, const struct nft_set_ext **ext) { struct nft_hash *priv = nft_set_priv(set); u8 genmask = nft_genmask_cur(net); const struct nft_hash_elem *he; u32 hash; hash = jhash(key, set->klen, priv->seed); hash = reciprocal_scale(hash, priv->buckets); hlist_for_each_entry_rcu(he, &priv->table[hash], node) { if (!memcmp(nft_set_ext_key(&he->ext), key, set->klen) && nft_set_elem_active(&he->ext, genmask)) { *ext = &he->ext; return true; } } return false; } |
ba0e4d991
|
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
static void *nft_hash_get(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem, unsigned int flags) { struct nft_hash *priv = nft_set_priv(set); u8 genmask = nft_genmask_cur(net); struct nft_hash_elem *he; u32 hash; hash = jhash(elem->key.val.data, set->klen, priv->seed); hash = reciprocal_scale(hash, priv->buckets); hlist_for_each_entry_rcu(he, &priv->table[hash], node) { if (!memcmp(nft_set_ext_key(&he->ext), elem->key.val.data, set->klen) && nft_set_elem_active(&he->ext, genmask)) return he; } return ERR_PTR(-ENOENT); } |
446a8268b
|
428 429 430 431 432 433 434 435 |
static bool nft_hash_lookup_fast(const struct net *net, const struct nft_set *set, const u32 *key, const struct nft_set_ext **ext) { struct nft_hash *priv = nft_set_priv(set); u8 genmask = nft_genmask_cur(net); const struct nft_hash_elem *he; u32 hash, k1, k2; |
123f89c8a
|
436 |
k1 = *key; |
446a8268b
|
437 438 439 |
hash = jhash_1word(k1, priv->seed); hash = reciprocal_scale(hash, priv->buckets); hlist_for_each_entry_rcu(he, &priv->table[hash], node) { |
123f89c8a
|
440 |
k2 = *(u32 *)nft_set_ext_key(&he->ext)->data; |
446a8268b
|
441 442 443 444 445 446 447 448 |
if (k1 == k2 && nft_set_elem_active(&he->ext, genmask)) { *ext = &he->ext; return true; } } return false; } |
3b02b0adc
|
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
static u32 nft_jhash(const struct nft_set *set, const struct nft_hash *priv, const struct nft_set_ext *ext) { const struct nft_data *key = nft_set_ext_key(ext); u32 hash, k1; if (set->klen == 4) { k1 = *(u32 *)key; hash = jhash_1word(k1, priv->seed); } else { hash = jhash(key, set->klen, priv->seed); } hash = reciprocal_scale(hash, priv->buckets); return hash; } |
6c03ae210
|
465 466 467 468 469 470 471 472 |
static int nft_hash_insert(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem, struct nft_set_ext **ext) { struct nft_hash_elem *this = elem->priv, *he; struct nft_hash *priv = nft_set_priv(set); u8 genmask = nft_genmask_next(net); u32 hash; |
3b02b0adc
|
473 |
hash = nft_jhash(set, priv, &this->ext); |
6c03ae210
|
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
hlist_for_each_entry(he, &priv->table[hash], node) { if (!memcmp(nft_set_ext_key(&this->ext), nft_set_ext_key(&he->ext), set->klen) && nft_set_elem_active(&he->ext, genmask)) { *ext = &he->ext; return -EEXIST; } } hlist_add_head_rcu(&this->node, &priv->table[hash]); return 0; } static void nft_hash_activate(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem) { struct nft_hash_elem *he = elem->priv; nft_set_elem_change_active(net, set, &he->ext); } static bool nft_hash_flush(const struct net *net, const struct nft_set *set, void *priv) { struct nft_hash_elem *he = priv; nft_set_elem_change_active(net, set, &he->ext); return true; } static void *nft_hash_deactivate(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem) { struct nft_hash *priv = nft_set_priv(set); struct nft_hash_elem *this = elem->priv, *he; u8 genmask = nft_genmask_next(net); u32 hash; |
3b02b0adc
|
511 |
hash = nft_jhash(set, priv, &this->ext); |
6c03ae210
|
512 |
hlist_for_each_entry(he, &priv->table[hash], node) { |
a01cbae57
|
513 |
if (!memcmp(nft_set_ext_key(&he->ext), &elem->key.val, |
7f4dae2d7
|
514 |
set->klen) && |
6c03ae210
|
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
nft_set_elem_active(&he->ext, genmask)) { nft_set_elem_change_active(net, set, &he->ext); return he; } } return NULL; } static void nft_hash_remove(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem) { struct nft_hash_elem *he = elem->priv; hlist_del_rcu(&he->node); } static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set, struct nft_set_iter *iter) { struct nft_hash *priv = nft_set_priv(set); struct nft_hash_elem *he; struct nft_set_elem elem; int i; for (i = 0; i < priv->buckets; i++) { hlist_for_each_entry_rcu(he, &priv->table[i], node) { if (iter->count < iter->skip) goto cont; if (!nft_set_elem_active(&he->ext, iter->genmask)) goto cont; elem.priv = he; iter->err = iter->fn(ctx, set, iter, &elem); if (iter->err < 0) return; cont: iter->count++; } } } |
4ef360dd6
|
557 558 |
static u64 nft_hash_privsize(const struct nlattr * const nla[], const struct nft_set_desc *desc) |
6c03ae210
|
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 |
{ return sizeof(struct nft_hash) + nft_hash_buckets(desc->size) * sizeof(struct hlist_head); } static int nft_hash_init(const struct nft_set *set, const struct nft_set_desc *desc, const struct nlattr * const tb[]) { struct nft_hash *priv = nft_set_priv(set); priv->buckets = nft_hash_buckets(desc->size); get_random_bytes(&priv->seed, sizeof(priv->seed)); return 0; } static void nft_hash_destroy(const struct nft_set *set) { struct nft_hash *priv = nft_set_priv(set); struct nft_hash_elem *he; struct hlist_node *next; int i; for (i = 0; i < priv->buckets; i++) { hlist_for_each_entry_safe(he, next, &priv->table[i], node) { hlist_del_rcu(&he->node); nft_set_elem_destroy(set, he, true); } } } |
c50b960cc
|
590 |
|
6c03ae210
|
591 592 593 |
static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, struct nft_set_estimate *est) { |
71cc0873e
|
594 595 596 597 598 |
if (!desc->size) return false; if (desc->klen == 4) return false; |
6c03ae210
|
599 600 601 |
est->size = sizeof(struct nft_hash) + nft_hash_buckets(desc->size) * sizeof(struct hlist_head) + desc->size * sizeof(struct nft_hash_elem); |
55af753cd
|
602 |
est->lookup = NFT_SET_CLASS_O_1; |
0b5a78749
|
603 |
est->space = NFT_SET_CLASS_O_N; |
c50b960cc
|
604 605 606 |
return true; } |
71cc0873e
|
607 608 609 610 611 |
static bool nft_hash_fast_estimate(const struct nft_set_desc *desc, u32 features, struct nft_set_estimate *est) { if (!desc->size) return false; |
2b664957c
|
612 |
|
71cc0873e
|
613 614 |
if (desc->klen != 4) return false; |
6c03ae210
|
615 |
|
71cc0873e
|
616 617 618 619 620 |
est->size = sizeof(struct nft_hash) + nft_hash_buckets(desc->size) * sizeof(struct hlist_head) + desc->size * sizeof(struct nft_hash_elem); est->lookup = NFT_SET_CLASS_O_1; est->space = NFT_SET_CLASS_O_N; |
6c03ae210
|
621 |
|
71cc0873e
|
622 |
return true; |
6c03ae210
|
623 |
} |
e240cd0df
|
624 |
struct nft_set_type nft_set_rhash_type __read_mostly = { |
71cc0873e
|
625 626 627 628 629 630 631 632 |
.owner = THIS_MODULE, .features = NFT_SET_MAP | NFT_SET_OBJECT | NFT_SET_TIMEOUT | NFT_SET_EVAL, .ops = { .privsize = nft_rhash_privsize, .elemsize = offsetof(struct nft_rhash_elem, ext), .estimate = nft_rhash_estimate, .init = nft_rhash_init, |
79b174ade
|
633 |
.gc_init = nft_rhash_gc_init, |
71cc0873e
|
634 635 636 637 638 639 640 641 |
.destroy = nft_rhash_destroy, .insert = nft_rhash_insert, .activate = nft_rhash_activate, .deactivate = nft_rhash_deactivate, .flush = nft_rhash_flush, .remove = nft_rhash_remove, .lookup = nft_rhash_lookup, .update = nft_rhash_update, |
d0a8d877d
|
642 |
.delete = nft_rhash_delete, |
71cc0873e
|
643 644 645 646 |
.walk = nft_rhash_walk, .get = nft_rhash_get, }, }; |
e240cd0df
|
647 |
struct nft_set_type nft_set_hash_type __read_mostly = { |
20a69341f
|
648 |
.owner = THIS_MODULE, |
71cc0873e
|
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
.features = NFT_SET_MAP | NFT_SET_OBJECT, .ops = { .privsize = nft_hash_privsize, .elemsize = offsetof(struct nft_hash_elem, ext), .estimate = nft_hash_estimate, .init = nft_hash_init, .destroy = nft_hash_destroy, .insert = nft_hash_insert, .activate = nft_hash_activate, .deactivate = nft_hash_deactivate, .flush = nft_hash_flush, .remove = nft_hash_remove, .lookup = nft_hash_lookup, .walk = nft_hash_walk, .get = nft_hash_get, }, }; |
e240cd0df
|
666 |
struct nft_set_type nft_set_hash_fast_type __read_mostly = { |
71cc0873e
|
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 |
.owner = THIS_MODULE, .features = NFT_SET_MAP | NFT_SET_OBJECT, .ops = { .privsize = nft_hash_privsize, .elemsize = offsetof(struct nft_hash_elem, ext), .estimate = nft_hash_fast_estimate, .init = nft_hash_init, .destroy = nft_hash_destroy, .insert = nft_hash_insert, .activate = nft_hash_activate, .deactivate = nft_hash_deactivate, .flush = nft_hash_flush, .remove = nft_hash_remove, .lookup = nft_hash_lookup_fast, .walk = nft_hash_walk, .get = nft_hash_get, }, |
96518518c
|
684 |
}; |