Blame view
net/netfilter/nft_set_hash.c
16.2 KB
96518518c netfilter: add nf... |
1 |
/* |
ce6eb0d7c netfilter: nft_ha... |
2 |
* Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> |
96518518c netfilter: add nf... |
3 4 5 6 7 8 9 10 11 12 13 14 |
* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * 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 netfilter: nf_tab... |
15 |
#include <linux/log2.h> |
96518518c netfilter: add nf... |
16 17 |
#include <linux/jhash.h> #include <linux/netlink.h> |
9d0982927 netfilter: nft_ha... |
18 |
#include <linux/workqueue.h> |
cfe4a9dda nftables: Convert... |
19 |
#include <linux/rhashtable.h> |
96518518c netfilter: add nf... |
20 21 22 |
#include <linux/netfilter.h> #include <linux/netfilter/nf_tables.h> #include <net/netfilter/nf_tables.h> |
cfe4a9dda nftables: Convert... |
23 |
/* We target a hash table size of 4, element hint is 75% of final size */ |
5fc6ced95 netfilter: nft_se... |
24 |
#define NFT_RHASH_ELEMENT_HINT 3 |
96518518c netfilter: add nf... |
25 |
|
5fc6ced95 netfilter: nft_se... |
26 |
struct nft_rhash { |
745f5450d netfilter: nft_ha... |
27 |
struct rhashtable ht; |
9d0982927 netfilter: nft_ha... |
28 |
struct delayed_work gc_work; |
745f5450d netfilter: nft_ha... |
29 |
}; |
5fc6ced95 netfilter: nft_se... |
30 |
struct nft_rhash_elem { |
cfe4a9dda nftables: Convert... |
31 |
struct rhash_head node; |
fe2811ebe netfilter: nf_tab... |
32 |
struct nft_set_ext ext; |
96518518c netfilter: add nf... |
33 |
}; |
5fc6ced95 netfilter: nft_se... |
34 |
struct nft_rhash_cmp_arg { |
bfd6e327e netfilter: nft_ha... |
35 |
const struct nft_set *set; |
8cd8937ac netfilter: nf_tab... |
36 |
const u32 *key; |
cc02e457b netfilter: nf_tab... |
37 |
u8 genmask; |
bfd6e327e netfilter: nft_ha... |
38 |
}; |
5fc6ced95 netfilter: nft_se... |
39 |
static inline u32 nft_rhash_key(const void *data, u32 len, u32 seed) |
bfd6e327e netfilter: nft_ha... |
40 |
{ |
5fc6ced95 netfilter: nft_se... |
41 |
const struct nft_rhash_cmp_arg *arg = data; |
bfd6e327e netfilter: nft_ha... |
42 43 44 |
return jhash(arg->key, len, seed); } |
5fc6ced95 netfilter: nft_se... |
45 |
static inline u32 nft_rhash_obj(const void *data, u32 len, u32 seed) |
bfd6e327e netfilter: nft_ha... |
46 |
{ |
5fc6ced95 netfilter: nft_se... |
47 |
const struct nft_rhash_elem *he = data; |
bfd6e327e netfilter: nft_ha... |
48 |
|
fe2811ebe netfilter: nf_tab... |
49 |
return jhash(nft_set_ext_key(&he->ext), len, seed); |
bfd6e327e netfilter: nft_ha... |
50 |
} |
5fc6ced95 netfilter: nft_se... |
51 52 |
static inline int nft_rhash_cmp(struct rhashtable_compare_arg *arg, const void *ptr) |
bfd6e327e netfilter: nft_ha... |
53 |
{ |
5fc6ced95 netfilter: nft_se... |
54 55 |
const struct nft_rhash_cmp_arg *x = arg->key; const struct nft_rhash_elem *he = ptr; |
bfd6e327e netfilter: nft_ha... |
56 |
|
e562d860d netfilter: nf_tab... |
57 |
if (memcmp(nft_set_ext_key(&he->ext), x->key, x->set->klen)) |
bfd6e327e netfilter: nft_ha... |
58 |
return 1; |
9d0982927 netfilter: nft_ha... |
59 60 |
if (nft_set_elem_expired(&he->ext)) return 1; |
cc02e457b netfilter: nf_tab... |
61 62 |
if (!nft_set_elem_active(&he->ext, x->genmask)) return 1; |
bfd6e327e netfilter: nft_ha... |
63 64 |
return 0; } |
5fc6ced95 netfilter: nft_se... |
65 66 67 68 69 |
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 netfilter: nft_se... |
70 71 |
.automatic_shrinking = true, }; |
5fc6ced95 netfilter: nft_se... |
72 73 |
static bool nft_rhash_lookup(const struct net *net, const struct nft_set *set, const u32 *key, const struct nft_set_ext **ext) |
96518518c netfilter: add nf... |
74 |
{ |
5fc6ced95 netfilter: nft_se... |
75 76 77 |
struct nft_rhash *priv = nft_set_priv(set); const struct nft_rhash_elem *he; struct nft_rhash_cmp_arg arg = { |
42a557691 netfilter: nf_tab... |
78 |
.genmask = nft_genmask_cur(net), |
bfd6e327e netfilter: nft_ha... |
79 80 81 |
.set = set, .key = key, }; |
ce6eb0d7c netfilter: nft_ha... |
82 |
|
5fc6ced95 netfilter: nft_se... |
83 |
he = rhashtable_lookup_fast(&priv->ht, &arg, nft_rhash_params); |
b2832dd66 netfilter: nf_tab... |
84 85 |
if (he != NULL) *ext = &he->ext; |
ce6eb0d7c netfilter: nft_ha... |
86 |
|
cfe4a9dda nftables: Convert... |
87 |
return !!he; |
96518518c netfilter: add nf... |
88 |
} |
5fc6ced95 netfilter: nft_se... |
89 90 91 92 93 94 95 |
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 netfilter: nf_tab... |
96 |
{ |
5fc6ced95 netfilter: nft_se... |
97 98 99 |
struct nft_rhash *priv = nft_set_priv(set); struct nft_rhash_elem *he, *prev; struct nft_rhash_cmp_arg arg = { |
22fe54d5f netfilter: nf_tab... |
100 101 102 103 |
.genmask = NFT_GENMASK_ANY, .set = set, .key = key, }; |
5fc6ced95 netfilter: nft_se... |
104 |
he = rhashtable_lookup_fast(&priv->ht, &arg, nft_rhash_params); |
22fe54d5f netfilter: nf_tab... |
105 106 |
if (he != NULL) goto out; |
a55e22e92 netfilter: nf_tab... |
107 |
he = new(set, expr, regs); |
22fe54d5f netfilter: nf_tab... |
108 109 |
if (he == NULL) goto err1; |
dab45060a netfilter: nf_tab... |
110 111 |
prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node, |
5fc6ced95 netfilter: nft_se... |
112 |
nft_rhash_params); |
dab45060a netfilter: nf_tab... |
113 |
if (IS_ERR(prev)) |
22fe54d5f netfilter: nf_tab... |
114 |
goto err2; |
dab45060a netfilter: nf_tab... |
115 116 117 118 119 120 |
/* Another cpu may race to insert the element with the same key */ if (prev) { nft_set_elem_destroy(set, he, true); he = prev; } |
22fe54d5f netfilter: nf_tab... |
121 122 123 124 125 |
out: *ext = &he->ext; return true; err2: |
61f9e2924 netfilter: nf_tab... |
126 |
nft_set_elem_destroy(set, he, true); |
22fe54d5f netfilter: nf_tab... |
127 128 129 |
err1: return false; } |
5fc6ced95 netfilter: nft_se... |
130 131 132 |
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 netfilter: add nf... |
133 |
{ |
5fc6ced95 netfilter: nft_se... |
134 135 136 |
struct nft_rhash *priv = nft_set_priv(set); struct nft_rhash_elem *he = elem->priv; struct nft_rhash_cmp_arg arg = { |
42a557691 netfilter: nf_tab... |
137 |
.genmask = nft_genmask_next(net), |
bfd6e327e netfilter: nft_ha... |
138 |
.set = set, |
7d7402642 netfilter: nf_tab... |
139 |
.key = elem->key.val.data, |
bfd6e327e netfilter: nft_ha... |
140 |
}; |
5fc6ced95 netfilter: nft_se... |
141 |
struct nft_rhash_elem *prev; |
c016c7e45 netfilter: nf_tab... |
142 143 |
prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node, |
5fc6ced95 netfilter: nft_se... |
144 |
nft_rhash_params); |
c016c7e45 netfilter: nf_tab... |
145 146 147 148 149 150 151 |
if (IS_ERR(prev)) return PTR_ERR(prev); if (prev) { *ext = &prev->ext; return -EEXIST; } return 0; |
96518518c netfilter: add nf... |
152 |
} |
5fc6ced95 netfilter: nft_se... |
153 154 |
static void nft_rhash_activate(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem) |
96518518c netfilter: add nf... |
155 |
{ |
5fc6ced95 netfilter: nft_se... |
156 |
struct nft_rhash_elem *he = elem->priv; |
ce6eb0d7c netfilter: nft_ha... |
157 |
|
42a557691 netfilter: nf_tab... |
158 |
nft_set_elem_change_active(net, set, &he->ext); |
9d0982927 netfilter: nft_ha... |
159 |
nft_set_elem_clear_busy(&he->ext); |
20a69341f netfilter: nf_tab... |
160 |
} |
96518518c netfilter: add nf... |
161 |
|
5fc6ced95 netfilter: nft_se... |
162 163 |
static bool nft_rhash_flush(const struct net *net, const struct nft_set *set, void *priv) |
37df5301a netfilter: nft_se... |
164 |
{ |
5fc6ced95 netfilter: nft_se... |
165 |
struct nft_rhash_elem *he = priv; |
37df5301a netfilter: nft_se... |
166 167 168 169 170 171 172 173 |
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 netfilter: nft_se... |
174 175 176 |
static void *nft_rhash_deactivate(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem) |
20a69341f netfilter: nf_tab... |
177 |
{ |
5fc6ced95 netfilter: nft_se... |
178 179 180 |
struct nft_rhash *priv = nft_set_priv(set); struct nft_rhash_elem *he; struct nft_rhash_cmp_arg arg = { |
8eee54be7 netfilter: nft_ha... |
181 |
.genmask = nft_genmask_next(net), |
bfd6e327e netfilter: nft_ha... |
182 |
.set = set, |
7d7402642 netfilter: nf_tab... |
183 |
.key = elem->key.val.data, |
bfd6e327e netfilter: nft_ha... |
184 |
}; |
fa3773211 netfilter: Conver... |
185 |
|
9d0982927 netfilter: nft_ha... |
186 |
rcu_read_lock(); |
5fc6ced95 netfilter: nft_se... |
187 |
he = rhashtable_lookup_fast(&priv->ht, &arg, nft_rhash_params); |
37df5301a netfilter: nft_se... |
188 |
if (he != NULL && |
5fc6ced95 netfilter: nft_se... |
189 |
!nft_rhash_flush(net, set, he)) |
37df5301a netfilter: nft_se... |
190 |
he = NULL; |
9d0982927 netfilter: nft_ha... |
191 |
rcu_read_unlock(); |
8d24c0b43 rhashtable: Do ha... |
192 |
|
cc02e457b netfilter: nf_tab... |
193 194 |
return he; } |
8d24c0b43 rhashtable: Do ha... |
195 |
|
5fc6ced95 netfilter: nft_se... |
196 197 198 |
static void nft_rhash_remove(const struct net *net, const struct nft_set *set, const struct nft_set_elem *elem) |
cc02e457b netfilter: nf_tab... |
199 |
{ |
5fc6ced95 netfilter: nft_se... |
200 201 |
struct nft_rhash *priv = nft_set_priv(set); struct nft_rhash_elem *he = elem->priv; |
cc02e457b netfilter: nf_tab... |
202 |
|
5fc6ced95 netfilter: nft_se... |
203 |
rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params); |
96518518c netfilter: add nf... |
204 |
} |
5fc6ced95 netfilter: nft_se... |
205 206 |
static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set, struct nft_set_iter *iter) |
96518518c netfilter: add nf... |
207 |
{ |
5fc6ced95 netfilter: nft_se... |
208 209 |
struct nft_rhash *priv = nft_set_priv(set); struct nft_rhash_elem *he; |
9a7766288 netfilter: Use rh... |
210 |
struct rhashtable_iter hti; |
20a69341f netfilter: nf_tab... |
211 |
struct nft_set_elem elem; |
9a7766288 netfilter: Use rh... |
212 |
int err; |
96518518c netfilter: add nf... |
213 |
|
fa803605e netfilter: nf_tab... |
214 |
err = rhashtable_walk_init(&priv->ht, &hti, GFP_ATOMIC); |
9a7766288 netfilter: Use rh... |
215 216 217 |
iter->err = err; if (err) return; |
88d6ed15a rhashtable: Conve... |
218 |
|
9a7766288 netfilter: Use rh... |
219 220 221 222 223 224 225 226 227 228 229 230 231 |
err = rhashtable_walk_start(&hti); if (err && err != -EAGAIN) { iter->err = err; goto out; } while ((he = rhashtable_walk_next(&hti))) { if (IS_ERR(he)) { err = PTR_ERR(he); if (err != -EAGAIN) { iter->err = err; goto out; } |
d8bdff59c netfilter: Fix po... |
232 233 |
continue; |
9a7766288 netfilter: Use rh... |
234 235 236 237 |
} if (iter->count < iter->skip) goto cont; |
9d0982927 netfilter: nft_ha... |
238 239 |
if (nft_set_elem_expired(&he->ext)) goto cont; |
8588ac097 netfilter: nf_tab... |
240 |
if (!nft_set_elem_active(&he->ext, iter->genmask)) |
cc02e457b netfilter: nf_tab... |
241 |
goto cont; |
20a69341f netfilter: nf_tab... |
242 |
|
fe2811ebe netfilter: nf_tab... |
243 |
elem.priv = he; |
9a7766288 netfilter: Use rh... |
244 245 246 247 |
iter->err = iter->fn(ctx, set, iter, &elem); if (iter->err < 0) goto out; |
20a69341f netfilter: nf_tab... |
248 |
|
20a69341f netfilter: nf_tab... |
249 |
cont: |
9a7766288 netfilter: Use rh... |
250 |
iter->count++; |
96518518c netfilter: add nf... |
251 |
} |
9a7766288 netfilter: Use rh... |
252 253 254 255 |
out: rhashtable_walk_stop(&hti); rhashtable_walk_exit(&hti); |
96518518c netfilter: add nf... |
256 |
} |
5fc6ced95 netfilter: nft_se... |
257 |
static void nft_rhash_gc(struct work_struct *work) |
9d0982927 netfilter: nft_ha... |
258 |
{ |
3dd0673ac netfilter: nf_tab... |
259 |
struct nft_set *set; |
5fc6ced95 netfilter: nft_se... |
260 261 |
struct nft_rhash_elem *he; struct nft_rhash *priv; |
9d0982927 netfilter: nft_ha... |
262 263 264 |
struct nft_set_gc_batch *gcb = NULL; struct rhashtable_iter hti; int err; |
5fc6ced95 netfilter: nft_se... |
265 |
priv = container_of(work, struct nft_rhash, gc_work.work); |
9d0982927 netfilter: nft_ha... |
266 |
set = nft_set_container_of(priv); |
8f6fd83c6 rhashtable: accep... |
267 |
err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL); |
9d0982927 netfilter: nft_ha... |
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
if (err) goto schedule; err = rhashtable_walk_start(&hti); if (err && err != -EAGAIN) goto out; while ((he = rhashtable_walk_next(&hti))) { if (IS_ERR(he)) { if (PTR_ERR(he) != -EAGAIN) goto out; continue; } if (!nft_set_elem_expired(&he->ext)) continue; if (nft_set_elem_mark_busy(&he->ext)) continue; gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC); if (gcb == NULL) goto out; |
5fc6ced95 netfilter: nft_se... |
290 |
rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params); |
3dd0673ac netfilter: nf_tab... |
291 |
atomic_dec(&set->nelems); |
9d0982927 netfilter: nft_ha... |
292 293 294 295 296 297 298 299 300 301 302 |
nft_set_gc_batch_add(gcb, he); } out: rhashtable_walk_stop(&hti); rhashtable_walk_exit(&hti); nft_set_gc_batch_complete(gcb); schedule: queue_delayed_work(system_power_efficient_wq, &priv->gc_work, nft_set_gc_interval(set)); } |
347b408d5 netfilter: nf_tab... |
303 304 |
static unsigned int nft_rhash_privsize(const struct nlattr * const nla[], const struct nft_set_desc *desc) |
20a69341f netfilter: nf_tab... |
305 |
{ |
5fc6ced95 netfilter: nft_se... |
306 |
return sizeof(struct nft_rhash); |
cfe4a9dda nftables: Convert... |
307 |
} |
5fc6ced95 netfilter: nft_se... |
308 309 310 |
static int nft_rhash_init(const struct nft_set *set, const struct nft_set_desc *desc, const struct nlattr * const tb[]) |
96518518c netfilter: add nf... |
311 |
{ |
5fc6ced95 netfilter: nft_se... |
312 313 |
struct nft_rhash *priv = nft_set_priv(set); struct rhashtable_params params = nft_rhash_params; |
9d0982927 netfilter: nft_ha... |
314 |
int err; |
fa3773211 netfilter: Conver... |
315 |
|
5fc6ced95 netfilter: nft_se... |
316 |
params.nelem_hint = desc->size ?: NFT_RHASH_ELEMENT_HINT; |
45d84751f netfilter: nft_ha... |
317 |
params.key_len = set->klen; |
96518518c netfilter: add nf... |
318 |
|
9d0982927 netfilter: nft_ha... |
319 320 321 |
err = rhashtable_init(&priv->ht, ¶ms); if (err < 0) return err; |
5fc6ced95 netfilter: nft_se... |
322 |
INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc); |
9d0982927 netfilter: nft_ha... |
323 324 325 326 |
if (set->flags & NFT_SET_TIMEOUT) queue_delayed_work(system_power_efficient_wq, &priv->gc_work, nft_set_gc_interval(set)); return 0; |
96518518c netfilter: add nf... |
327 |
} |
5fc6ced95 netfilter: nft_se... |
328 |
static void nft_rhash_elem_destroy(void *ptr, void *arg) |
96518518c netfilter: add nf... |
329 |
{ |
68ad546ae netfilter: Remove... |
330 |
nft_set_elem_destroy(arg, ptr, true); |
6b6f302ce rhashtable: Add r... |
331 |
} |
97defe1ec rhashtable: Per b... |
332 |
|
5fc6ced95 netfilter: nft_se... |
333 |
static void nft_rhash_destroy(const struct nft_set *set) |
6b6f302ce rhashtable: Add r... |
334 |
{ |
5fc6ced95 netfilter: nft_se... |
335 |
struct nft_rhash *priv = nft_set_priv(set); |
745f5450d netfilter: nft_ha... |
336 |
|
9d0982927 netfilter: nft_ha... |
337 |
cancel_delayed_work_sync(&priv->gc_work); |
a685c4c4d netfilter: nft_se... |
338 |
rcu_barrier(); |
5fc6ced95 netfilter: nft_se... |
339 |
rhashtable_free_and_destroy(&priv->ht, nft_rhash_elem_destroy, |
61edafbb4 netfilter: nf_tab... |
340 |
(void *)set); |
96518518c netfilter: add nf... |
341 |
} |
2111515ab netfilter: nft_se... |
342 343 344 345 |
static u32 nft_hash_buckets(u32 size) { return roundup_pow_of_two(size * 4 / 3); } |
5fc6ced95 netfilter: nft_se... |
346 347 |
static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features, struct nft_set_estimate *est) |
c50b960cc netfilter: nf_tab... |
348 |
{ |
6c03ae210 netfilter: nft_se... |
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
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; } |
446a8268b netfilter: nft_se... |
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 411 412 413 414 415 416 |
/* nft_hash_select_ops() makes sure key size can be either 2 or 4 bytes . */ static inline u32 nft_hash_key(const u32 *key, u32 klen) { if (klen == 4) return *key; return *(u16 *)key; } 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; k1 = nft_hash_key(key, set->klen); hash = jhash_1word(k1, priv->seed); hash = reciprocal_scale(hash, priv->buckets); hlist_for_each_entry_rcu(he, &priv->table[hash], node) { k2 = nft_hash_key(nft_set_ext_key(&he->ext)->data, set->klen); if (k1 == k2 && nft_set_elem_active(&he->ext, genmask)) { *ext = &he->ext; return true; } } return false; } |
6c03ae210 netfilter: nft_se... |
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 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 511 512 513 514 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 |
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; hash = jhash(nft_set_ext_key(&this->ext), set->klen, priv->seed); hash = reciprocal_scale(hash, priv->buckets); 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; hash = jhash(nft_set_ext_key(&this->ext), set->klen, priv->seed); hash = reciprocal_scale(hash, priv->buckets); hlist_for_each_entry(he, &priv->table[hash], node) { if (!memcmp(nft_set_ext_key(&this->ext), &elem->key.val, set->klen) || 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++; } } } static unsigned int nft_hash_privsize(const struct nlattr * const nla[], const struct nft_set_desc *desc) { 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 netfilter: nf_tab... |
547 |
|
6c03ae210 netfilter: nft_se... |
548 549 550 551 552 553 |
static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, struct nft_set_estimate *est) { est->size = sizeof(struct nft_hash) + nft_hash_buckets(desc->size) * sizeof(struct hlist_head) + desc->size * sizeof(struct nft_hash_elem); |
55af753cd netfilter: nf_tab... |
554 |
est->lookup = NFT_SET_CLASS_O_1; |
0b5a78749 netfilter: nf_tab... |
555 |
est->space = NFT_SET_CLASS_O_N; |
c50b960cc netfilter: nf_tab... |
556 557 558 |
return true; } |
2b664957c netfilter: nf_tab... |
559 |
static struct nft_set_type nft_hash_type; |
5fc6ced95 netfilter: nft_se... |
560 |
static struct nft_set_ops nft_rhash_ops __read_mostly = { |
2b664957c netfilter: nf_tab... |
561 |
.type = &nft_hash_type, |
5fc6ced95 netfilter: nft_se... |
562 563 564 565 566 567 568 569 570 571 572 573 574 |
.privsize = nft_rhash_privsize, .elemsize = offsetof(struct nft_rhash_elem, ext), .estimate = nft_rhash_estimate, .init = nft_rhash_init, .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, .walk = nft_rhash_walk, |
7286ff7fd netfilter: nf_tab... |
575 |
.features = NFT_SET_MAP | NFT_SET_OBJECT | NFT_SET_TIMEOUT, |
2b664957c netfilter: nf_tab... |
576 |
}; |
6c03ae210 netfilter: nft_se... |
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
static struct nft_set_ops nft_hash_ops __read_mostly = { .type = &nft_hash_type, .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, .features = NFT_SET_MAP | NFT_SET_OBJECT, }; |
446a8268b netfilter: nft_se... |
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
static struct nft_set_ops nft_hash_fast_ops __read_mostly = { .type = &nft_hash_type, .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_fast, .walk = nft_hash_walk, .features = NFT_SET_MAP | NFT_SET_OBJECT, }; |
6c03ae210 netfilter: nft_se... |
609 610 611 612 |
static const struct nft_set_ops * nft_hash_select_ops(const struct nft_ctx *ctx, const struct nft_set_desc *desc, u32 flags) { |
446a8268b netfilter: nft_se... |
613 614 |
if (desc->size) { switch (desc->klen) { |
446a8268b netfilter: nft_se... |
615 616 617 618 619 620 |
case 4: return &nft_hash_fast_ops; default: return &nft_hash_ops; } } |
6c03ae210 netfilter: nft_se... |
621 622 623 |
return &nft_rhash_ops; } |
2b664957c netfilter: nf_tab... |
624 |
static struct nft_set_type nft_hash_type __read_mostly = { |
6c03ae210 netfilter: nft_se... |
625 |
.select_ops = nft_hash_select_ops, |
20a69341f netfilter: nf_tab... |
626 |
.owner = THIS_MODULE, |
96518518c netfilter: add nf... |
627 628 629 630 |
}; static int __init nft_hash_module_init(void) { |
2b664957c netfilter: nf_tab... |
631 |
return nft_register_set(&nft_hash_type); |
96518518c netfilter: add nf... |
632 633 634 635 |
} static void __exit nft_hash_module_exit(void) { |
2b664957c netfilter: nf_tab... |
636 |
nft_unregister_set(&nft_hash_type); |
96518518c netfilter: add nf... |
637 638 639 640 641 642 643 |
} module_init(nft_hash_module_init); module_exit(nft_hash_module_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
20a69341f netfilter: nf_tab... |
644 |
MODULE_ALIAS_NFT_SET(); |