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