1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
4 *
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
6 */
7
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/list.h>
12#include <linux/log2.h>
13#include <linux/jhash.h>
14#include <linux/netlink.h>
15#include <linux/workqueue.h>
16#include <linux/rhashtable.h>
17#include <linux/netfilter.h>
18#include <linux/netfilter/nf_tables.h>
19#include <net/netfilter/nf_tables_core.h>
20
21/* We target a hash table size of 4, element hint is 75% of final size */
22#define NFT_RHASH_ELEMENT_HINT 3
23
24struct nft_rhash {
25 struct rhashtable ht;
26 struct delayed_work gc_work;
27};
28
29struct nft_rhash_elem {
30 struct nft_elem_priv priv;
31 struct rhash_head node;
32 struct nft_set_ext ext;
33};
34
35struct nft_rhash_cmp_arg {
36 const struct nft_set *set;
37 const u32 *key;
38 u8 genmask;
39 u64 tstamp;
40};
41
42static inline u32 nft_rhash_key(const void *data, u32 len, u32 seed)
43{
44 const struct nft_rhash_cmp_arg *arg = data;
45
46 return jhash(key: arg->key, length: len, initval: seed);
47}
48
49static inline u32 nft_rhash_obj(const void *data, u32 len, u32 seed)
50{
51 const struct nft_rhash_elem *he = data;
52
53 return jhash(key: nft_set_ext_key(ext: &he->ext), length: len, initval: seed);
54}
55
56static inline int nft_rhash_cmp(struct rhashtable_compare_arg *arg,
57 const void *ptr)
58{
59 const struct nft_rhash_cmp_arg *x = arg->key;
60 const struct nft_rhash_elem *he = ptr;
61
62 if (memcmp(p: nft_set_ext_key(ext: &he->ext), q: x->key, size: x->set->klen))
63 return 1;
64 if (nft_set_elem_is_dead(ext: &he->ext))
65 return 1;
66 if (__nft_set_elem_expired(ext: &he->ext, tstamp: x->tstamp))
67 return 1;
68 if (!nft_set_elem_active(ext: &he->ext, genmask: x->genmask))
69 return 1;
70 return 0;
71}
72
73static const struct rhashtable_params nft_rhash_params = {
74 .head_offset = offsetof(struct nft_rhash_elem, node),
75 .hashfn = nft_rhash_key,
76 .obj_hashfn = nft_rhash_obj,
77 .obj_cmpfn = nft_rhash_cmp,
78 .automatic_shrinking = true,
79};
80
81INDIRECT_CALLABLE_SCOPE
82bool nft_rhash_lookup(const struct net *net, const struct nft_set *set,
83 const u32 *key, const struct nft_set_ext **ext)
84{
85 struct nft_rhash *priv = nft_set_priv(set);
86 const struct nft_rhash_elem *he;
87 struct nft_rhash_cmp_arg arg = {
88 .genmask = nft_genmask_cur(net),
89 .set = set,
90 .key = key,
91 .tstamp = get_jiffies_64(),
92 };
93
94 he = rhashtable_lookup(ht: &priv->ht, key: &arg, params: nft_rhash_params);
95 if (he != NULL)
96 *ext = &he->ext;
97
98 return !!he;
99}
100
101static struct nft_elem_priv *
102nft_rhash_get(const struct net *net, const struct nft_set *set,
103 const struct nft_set_elem *elem, unsigned int flags)
104{
105 struct nft_rhash *priv = nft_set_priv(set);
106 struct nft_rhash_elem *he;
107 struct nft_rhash_cmp_arg arg = {
108 .genmask = nft_genmask_cur(net),
109 .set = set,
110 .key = elem->key.val.data,
111 .tstamp = get_jiffies_64(),
112 };
113
114 he = rhashtable_lookup(ht: &priv->ht, key: &arg, params: nft_rhash_params);
115 if (he != NULL)
116 return &he->priv;
117
118 return ERR_PTR(error: -ENOENT);
119}
120
121static bool nft_rhash_update(struct nft_set *set, const u32 *key,
122 struct nft_elem_priv *
123 (*new)(struct nft_set *,
124 const struct nft_expr *,
125 struct nft_regs *regs),
126 const struct nft_expr *expr,
127 struct nft_regs *regs,
128 const struct nft_set_ext **ext)
129{
130 struct nft_rhash *priv = nft_set_priv(set);
131 struct nft_rhash_elem *he, *prev;
132 struct nft_elem_priv *elem_priv;
133 struct nft_rhash_cmp_arg arg = {
134 .genmask = NFT_GENMASK_ANY,
135 .set = set,
136 .key = key,
137 .tstamp = get_jiffies_64(),
138 };
139
140 he = rhashtable_lookup(ht: &priv->ht, key: &arg, params: nft_rhash_params);
141 if (he != NULL)
142 goto out;
143
144 elem_priv = new(set, expr, regs);
145 if (!elem_priv)
146 goto err1;
147
148 he = nft_elem_priv_cast(priv: elem_priv);
149 prev = rhashtable_lookup_get_insert_key(ht: &priv->ht, key: &arg, obj: &he->node,
150 params: nft_rhash_params);
151 if (IS_ERR(ptr: prev))
152 goto err2;
153
154 /* Another cpu may race to insert the element with the same key */
155 if (prev) {
156 nft_set_elem_destroy(set, elem_priv: &he->priv, destroy_expr: true);
157 atomic_dec(v: &set->nelems);
158 he = prev;
159 }
160
161out:
162 *ext = &he->ext;
163 return true;
164
165err2:
166 nft_set_elem_destroy(set, elem_priv: &he->priv, destroy_expr: true);
167 atomic_dec(v: &set->nelems);
168err1:
169 return false;
170}
171
172static int nft_rhash_insert(const struct net *net, const struct nft_set *set,
173 const struct nft_set_elem *elem,
174 struct nft_elem_priv **elem_priv)
175{
176 struct nft_rhash_elem *he = nft_elem_priv_cast(priv: elem->priv);
177 struct nft_rhash *priv = nft_set_priv(set);
178 struct nft_rhash_cmp_arg arg = {
179 .genmask = nft_genmask_next(net),
180 .set = set,
181 .key = elem->key.val.data,
182 .tstamp = nft_net_tstamp(net),
183 };
184 struct nft_rhash_elem *prev;
185
186 prev = rhashtable_lookup_get_insert_key(ht: &priv->ht, key: &arg, obj: &he->node,
187 params: nft_rhash_params);
188 if (IS_ERR(ptr: prev))
189 return PTR_ERR(ptr: prev);
190 if (prev) {
191 *elem_priv = &prev->priv;
192 return -EEXIST;
193 }
194 return 0;
195}
196
197static void nft_rhash_activate(const struct net *net, const struct nft_set *set,
198 struct nft_elem_priv *elem_priv)
199{
200 struct nft_rhash_elem *he = nft_elem_priv_cast(priv: elem_priv);
201
202 nft_clear(net, &he->ext);
203}
204
205static void nft_rhash_flush(const struct net *net,
206 const struct nft_set *set,
207 struct nft_elem_priv *elem_priv)
208{
209 struct nft_rhash_elem *he = nft_elem_priv_cast(priv: elem_priv);
210
211 nft_set_elem_change_active(net, set, ext: &he->ext);
212}
213
214static struct nft_elem_priv *
215nft_rhash_deactivate(const struct net *net, const struct nft_set *set,
216 const struct nft_set_elem *elem)
217{
218 struct nft_rhash *priv = nft_set_priv(set);
219 struct nft_rhash_elem *he;
220 struct nft_rhash_cmp_arg arg = {
221 .genmask = nft_genmask_next(net),
222 .set = set,
223 .key = elem->key.val.data,
224 .tstamp = nft_net_tstamp(net),
225 };
226
227 rcu_read_lock();
228 he = rhashtable_lookup(ht: &priv->ht, key: &arg, params: nft_rhash_params);
229 if (he)
230 nft_set_elem_change_active(net, set, ext: &he->ext);
231
232 rcu_read_unlock();
233
234 return &he->priv;
235}
236
237static void nft_rhash_remove(const struct net *net,
238 const struct nft_set *set,
239 struct nft_elem_priv *elem_priv)
240{
241 struct nft_rhash_elem *he = nft_elem_priv_cast(priv: elem_priv);
242 struct nft_rhash *priv = nft_set_priv(set);
243
244 rhashtable_remove_fast(ht: &priv->ht, obj: &he->node, params: nft_rhash_params);
245}
246
247static bool nft_rhash_delete(const struct nft_set *set,
248 const u32 *key)
249{
250 struct nft_rhash *priv = nft_set_priv(set);
251 struct nft_rhash_cmp_arg arg = {
252 .genmask = NFT_GENMASK_ANY,
253 .set = set,
254 .key = key,
255 };
256 struct nft_rhash_elem *he;
257
258 he = rhashtable_lookup(ht: &priv->ht, key: &arg, params: nft_rhash_params);
259 if (he == NULL)
260 return false;
261
262 nft_set_elem_dead(ext: &he->ext);
263
264 return true;
265}
266
267static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
268 struct nft_set_iter *iter)
269{
270 struct nft_rhash *priv = nft_set_priv(set);
271 struct nft_rhash_elem *he;
272 struct rhashtable_iter hti;
273
274 rhashtable_walk_enter(ht: &priv->ht, iter: &hti);
275 rhashtable_walk_start(iter: &hti);
276
277 while ((he = rhashtable_walk_next(iter: &hti))) {
278 if (IS_ERR(ptr: he)) {
279 if (PTR_ERR(ptr: he) != -EAGAIN) {
280 iter->err = PTR_ERR(ptr: he);
281 break;
282 }
283
284 continue;
285 }
286
287 if (iter->count < iter->skip)
288 goto cont;
289
290 iter->err = iter->fn(ctx, set, iter, &he->priv);
291 if (iter->err < 0)
292 break;
293
294cont:
295 iter->count++;
296 }
297 rhashtable_walk_stop(iter: &hti);
298 rhashtable_walk_exit(iter: &hti);
299}
300
301static bool nft_rhash_expr_needs_gc_run(const struct nft_set *set,
302 struct nft_set_ext *ext)
303{
304 struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext);
305 struct nft_expr *expr;
306 u32 size;
307
308 nft_setelem_expr_foreach(expr, elem_expr, size) {
309 if (expr->ops->gc &&
310 expr->ops->gc(read_pnet(pnet: &set->net), expr))
311 return true;
312 }
313
314 return false;
315}
316
317static void nft_rhash_gc(struct work_struct *work)
318{
319 struct nftables_pernet *nft_net;
320 struct nft_set *set;
321 struct nft_rhash_elem *he;
322 struct nft_rhash *priv;
323 struct rhashtable_iter hti;
324 struct nft_trans_gc *gc;
325 struct net *net;
326 u32 gc_seq;
327
328 priv = container_of(work, struct nft_rhash, gc_work.work);
329 set = nft_set_container_of(priv);
330 net = read_pnet(pnet: &set->net);
331 nft_net = nft_pernet(net);
332 gc_seq = READ_ONCE(nft_net->gc_seq);
333
334 if (nft_set_gc_is_pending(s: set))
335 goto done;
336
337 gc = nft_trans_gc_alloc(set, gc_seq, GFP_KERNEL);
338 if (!gc)
339 goto done;
340
341 rhashtable_walk_enter(ht: &priv->ht, iter: &hti);
342 rhashtable_walk_start(iter: &hti);
343
344 while ((he = rhashtable_walk_next(iter: &hti))) {
345 if (IS_ERR(ptr: he)) {
346 nft_trans_gc_destroy(trans: gc);
347 gc = NULL;
348 goto try_later;
349 }
350
351 /* Ruleset has been updated, try later. */
352 if (READ_ONCE(nft_net->gc_seq) != gc_seq) {
353 nft_trans_gc_destroy(trans: gc);
354 gc = NULL;
355 goto try_later;
356 }
357
358 if (nft_set_elem_is_dead(ext: &he->ext))
359 goto dead_elem;
360
361 if (nft_set_ext_exists(ext: &he->ext, id: NFT_SET_EXT_EXPRESSIONS) &&
362 nft_rhash_expr_needs_gc_run(set, ext: &he->ext))
363 goto needs_gc_run;
364
365 if (!nft_set_elem_expired(ext: &he->ext))
366 continue;
367needs_gc_run:
368 nft_set_elem_dead(ext: &he->ext);
369dead_elem:
370 gc = nft_trans_gc_queue_async(gc, gc_seq, GFP_ATOMIC);
371 if (!gc)
372 goto try_later;
373
374 nft_trans_gc_elem_add(gc, priv: he);
375 }
376
377 gc = nft_trans_gc_catchall_async(gc, gc_seq);
378
379try_later:
380 /* catchall list iteration requires rcu read side lock. */
381 rhashtable_walk_stop(iter: &hti);
382 rhashtable_walk_exit(iter: &hti);
383
384 if (gc)
385 nft_trans_gc_queue_async_done(gc);
386
387done:
388 queue_delayed_work(wq: system_power_efficient_wq, dwork: &priv->gc_work,
389 delay: nft_set_gc_interval(set));
390}
391
392static u64 nft_rhash_privsize(const struct nlattr * const nla[],
393 const struct nft_set_desc *desc)
394{
395 return sizeof(struct nft_rhash);
396}
397
398static void nft_rhash_gc_init(const struct nft_set *set)
399{
400 struct nft_rhash *priv = nft_set_priv(set);
401
402 queue_delayed_work(wq: system_power_efficient_wq, dwork: &priv->gc_work,
403 delay: nft_set_gc_interval(set));
404}
405
406static int nft_rhash_init(const struct nft_set *set,
407 const struct nft_set_desc *desc,
408 const struct nlattr * const tb[])
409{
410 struct nft_rhash *priv = nft_set_priv(set);
411 struct rhashtable_params params = nft_rhash_params;
412 int err;
413
414 BUILD_BUG_ON(offsetof(struct nft_rhash_elem, priv) != 0);
415
416 params.nelem_hint = desc->size ?: NFT_RHASH_ELEMENT_HINT;
417 params.key_len = set->klen;
418
419 err = rhashtable_init(ht: &priv->ht, params: &params);
420 if (err < 0)
421 return err;
422
423 INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc);
424 if (set->flags & (NFT_SET_TIMEOUT | NFT_SET_EVAL))
425 nft_rhash_gc_init(set);
426
427 return 0;
428}
429
430struct nft_rhash_ctx {
431 const struct nft_ctx ctx;
432 const struct nft_set *set;
433};
434
435static void nft_rhash_elem_destroy(void *ptr, void *arg)
436{
437 struct nft_rhash_ctx *rhash_ctx = arg;
438 struct nft_rhash_elem *he = ptr;
439
440 nf_tables_set_elem_destroy(ctx: &rhash_ctx->ctx, set: rhash_ctx->set, elem_priv: &he->priv);
441}
442
443static void nft_rhash_destroy(const struct nft_ctx *ctx,
444 const struct nft_set *set)
445{
446 struct nft_rhash *priv = nft_set_priv(set);
447 struct nft_rhash_ctx rhash_ctx = {
448 .ctx = *ctx,
449 .set = set,
450 };
451
452 cancel_delayed_work_sync(dwork: &priv->gc_work);
453 rhashtable_free_and_destroy(ht: &priv->ht, free_fn: nft_rhash_elem_destroy,
454 arg: (void *)&rhash_ctx);
455}
456
457/* Number of buckets is stored in u32, so cap our result to 1U<<31 */
458#define NFT_MAX_BUCKETS (1U << 31)
459
460static u32 nft_hash_buckets(u32 size)
461{
462 u64 val = div_u64(dividend: (u64)size * 4, divisor: 3);
463
464 if (val >= NFT_MAX_BUCKETS)
465 return NFT_MAX_BUCKETS;
466
467 return roundup_pow_of_two(val);
468}
469
470static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features,
471 struct nft_set_estimate *est)
472{
473 est->size = ~0;
474 est->lookup = NFT_SET_CLASS_O_1;
475 est->space = NFT_SET_CLASS_O_N;
476
477 return true;
478}
479
480struct nft_hash {
481 u32 seed;
482 u32 buckets;
483 struct hlist_head table[];
484};
485
486struct nft_hash_elem {
487 struct nft_elem_priv priv;
488 struct hlist_node node;
489 struct nft_set_ext ext;
490};
491
492INDIRECT_CALLABLE_SCOPE
493bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
494 const u32 *key, const struct nft_set_ext **ext)
495{
496 struct nft_hash *priv = nft_set_priv(set);
497 u8 genmask = nft_genmask_cur(net);
498 const struct nft_hash_elem *he;
499 u32 hash;
500
501 hash = jhash(key, length: set->klen, initval: priv->seed);
502 hash = reciprocal_scale(val: hash, ep_ro: priv->buckets);
503 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
504 if (!memcmp(p: nft_set_ext_key(ext: &he->ext), q: key, size: set->klen) &&
505 nft_set_elem_active(ext: &he->ext, genmask)) {
506 *ext = &he->ext;
507 return true;
508 }
509 }
510 return false;
511}
512
513static struct nft_elem_priv *
514nft_hash_get(const struct net *net, const struct nft_set *set,
515 const struct nft_set_elem *elem, unsigned int flags)
516{
517 struct nft_hash *priv = nft_set_priv(set);
518 u8 genmask = nft_genmask_cur(net);
519 struct nft_hash_elem *he;
520 u32 hash;
521
522 hash = jhash(key: elem->key.val.data, length: set->klen, initval: priv->seed);
523 hash = reciprocal_scale(val: hash, ep_ro: priv->buckets);
524 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
525 if (!memcmp(p: nft_set_ext_key(ext: &he->ext), q: elem->key.val.data, size: set->klen) &&
526 nft_set_elem_active(ext: &he->ext, genmask))
527 return &he->priv;
528 }
529 return ERR_PTR(error: -ENOENT);
530}
531
532INDIRECT_CALLABLE_SCOPE
533bool nft_hash_lookup_fast(const struct net *net,
534 const struct nft_set *set,
535 const u32 *key, const struct nft_set_ext **ext)
536{
537 struct nft_hash *priv = nft_set_priv(set);
538 u8 genmask = nft_genmask_cur(net);
539 const struct nft_hash_elem *he;
540 u32 hash, k1, k2;
541
542 k1 = *key;
543 hash = jhash_1word(a: k1, initval: priv->seed);
544 hash = reciprocal_scale(val: hash, ep_ro: priv->buckets);
545 hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
546 k2 = *(u32 *)nft_set_ext_key(ext: &he->ext)->data;
547 if (k1 == k2 &&
548 nft_set_elem_active(ext: &he->ext, genmask)) {
549 *ext = &he->ext;
550 return true;
551 }
552 }
553 return false;
554}
555
556static u32 nft_jhash(const struct nft_set *set, const struct nft_hash *priv,
557 const struct nft_set_ext *ext)
558{
559 const struct nft_data *key = nft_set_ext_key(ext);
560 u32 hash, k1;
561
562 if (set->klen == 4) {
563 k1 = *(u32 *)key;
564 hash = jhash_1word(a: k1, initval: priv->seed);
565 } else {
566 hash = jhash(key, length: set->klen, initval: priv->seed);
567 }
568 hash = reciprocal_scale(val: hash, ep_ro: priv->buckets);
569
570 return hash;
571}
572
573static int nft_hash_insert(const struct net *net, const struct nft_set *set,
574 const struct nft_set_elem *elem,
575 struct nft_elem_priv **elem_priv)
576{
577 struct nft_hash_elem *this = nft_elem_priv_cast(priv: elem->priv), *he;
578 struct nft_hash *priv = nft_set_priv(set);
579 u8 genmask = nft_genmask_next(net);
580 u32 hash;
581
582 hash = nft_jhash(set, priv, ext: &this->ext);
583 hlist_for_each_entry(he, &priv->table[hash], node) {
584 if (!memcmp(p: nft_set_ext_key(ext: &this->ext),
585 q: nft_set_ext_key(ext: &he->ext), size: set->klen) &&
586 nft_set_elem_active(ext: &he->ext, genmask)) {
587 *elem_priv = &he->priv;
588 return -EEXIST;
589 }
590 }
591 hlist_add_head_rcu(n: &this->node, h: &priv->table[hash]);
592 return 0;
593}
594
595static void nft_hash_activate(const struct net *net, const struct nft_set *set,
596 struct nft_elem_priv *elem_priv)
597{
598 struct nft_hash_elem *he = nft_elem_priv_cast(priv: elem_priv);
599
600 nft_clear(net, &he->ext);
601}
602
603static void nft_hash_flush(const struct net *net,
604 const struct nft_set *set,
605 struct nft_elem_priv *elem_priv)
606{
607 struct nft_hash_elem *he = nft_elem_priv_cast(priv: elem_priv);
608
609 nft_set_elem_change_active(net, set, ext: &he->ext);
610}
611
612static struct nft_elem_priv *
613nft_hash_deactivate(const struct net *net, const struct nft_set *set,
614 const struct nft_set_elem *elem)
615{
616 struct nft_hash_elem *this = nft_elem_priv_cast(priv: elem->priv), *he;
617 struct nft_hash *priv = nft_set_priv(set);
618 u8 genmask = nft_genmask_next(net);
619 u32 hash;
620
621 hash = nft_jhash(set, priv, ext: &this->ext);
622 hlist_for_each_entry(he, &priv->table[hash], node) {
623 if (!memcmp(p: nft_set_ext_key(ext: &he->ext), q: &elem->key.val,
624 size: set->klen) &&
625 nft_set_elem_active(ext: &he->ext, genmask)) {
626 nft_set_elem_change_active(net, set, ext: &he->ext);
627 return &he->priv;
628 }
629 }
630 return NULL;
631}
632
633static void nft_hash_remove(const struct net *net,
634 const struct nft_set *set,
635 struct nft_elem_priv *elem_priv)
636{
637 struct nft_hash_elem *he = nft_elem_priv_cast(priv: elem_priv);
638
639 hlist_del_rcu(n: &he->node);
640}
641
642static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set,
643 struct nft_set_iter *iter)
644{
645 struct nft_hash *priv = nft_set_priv(set);
646 struct nft_hash_elem *he;
647 int i;
648
649 for (i = 0; i < priv->buckets; i++) {
650 hlist_for_each_entry_rcu(he, &priv->table[i], node) {
651 if (iter->count < iter->skip)
652 goto cont;
653
654 iter->err = iter->fn(ctx, set, iter, &he->priv);
655 if (iter->err < 0)
656 return;
657cont:
658 iter->count++;
659 }
660 }
661}
662
663static u64 nft_hash_privsize(const struct nlattr * const nla[],
664 const struct nft_set_desc *desc)
665{
666 return sizeof(struct nft_hash) +
667 (u64)nft_hash_buckets(size: desc->size) * sizeof(struct hlist_head);
668}
669
670static int nft_hash_init(const struct nft_set *set,
671 const struct nft_set_desc *desc,
672 const struct nlattr * const tb[])
673{
674 struct nft_hash *priv = nft_set_priv(set);
675
676 priv->buckets = nft_hash_buckets(size: desc->size);
677 get_random_bytes(buf: &priv->seed, len: sizeof(priv->seed));
678
679 return 0;
680}
681
682static void nft_hash_destroy(const struct nft_ctx *ctx,
683 const struct nft_set *set)
684{
685 struct nft_hash *priv = nft_set_priv(set);
686 struct nft_hash_elem *he;
687 struct hlist_node *next;
688 int i;
689
690 for (i = 0; i < priv->buckets; i++) {
691 hlist_for_each_entry_safe(he, next, &priv->table[i], node) {
692 hlist_del_rcu(n: &he->node);
693 nf_tables_set_elem_destroy(ctx, set, elem_priv: &he->priv);
694 }
695 }
696}
697
698static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
699 struct nft_set_estimate *est)
700{
701 if (!desc->size)
702 return false;
703
704 if (desc->klen == 4)
705 return false;
706
707 est->size = sizeof(struct nft_hash) +
708 (u64)nft_hash_buckets(size: desc->size) * sizeof(struct hlist_head) +
709 (u64)desc->size * sizeof(struct nft_hash_elem);
710 est->lookup = NFT_SET_CLASS_O_1;
711 est->space = NFT_SET_CLASS_O_N;
712
713 return true;
714}
715
716static bool nft_hash_fast_estimate(const struct nft_set_desc *desc, u32 features,
717 struct nft_set_estimate *est)
718{
719 if (!desc->size)
720 return false;
721
722 if (desc->klen != 4)
723 return false;
724
725 est->size = sizeof(struct nft_hash) +
726 (u64)nft_hash_buckets(size: desc->size) * sizeof(struct hlist_head) +
727 (u64)desc->size * sizeof(struct nft_hash_elem);
728 est->lookup = NFT_SET_CLASS_O_1;
729 est->space = NFT_SET_CLASS_O_N;
730
731 return true;
732}
733
734const struct nft_set_type nft_set_rhash_type = {
735 .features = NFT_SET_MAP | NFT_SET_OBJECT |
736 NFT_SET_TIMEOUT | NFT_SET_EVAL,
737 .ops = {
738 .privsize = nft_rhash_privsize,
739 .elemsize = offsetof(struct nft_rhash_elem, ext),
740 .estimate = nft_rhash_estimate,
741 .init = nft_rhash_init,
742 .gc_init = nft_rhash_gc_init,
743 .destroy = nft_rhash_destroy,
744 .insert = nft_rhash_insert,
745 .activate = nft_rhash_activate,
746 .deactivate = nft_rhash_deactivate,
747 .flush = nft_rhash_flush,
748 .remove = nft_rhash_remove,
749 .lookup = nft_rhash_lookup,
750 .update = nft_rhash_update,
751 .delete = nft_rhash_delete,
752 .walk = nft_rhash_walk,
753 .get = nft_rhash_get,
754 },
755};
756
757const struct nft_set_type nft_set_hash_type = {
758 .features = NFT_SET_MAP | NFT_SET_OBJECT,
759 .ops = {
760 .privsize = nft_hash_privsize,
761 .elemsize = offsetof(struct nft_hash_elem, ext),
762 .estimate = nft_hash_estimate,
763 .init = nft_hash_init,
764 .destroy = nft_hash_destroy,
765 .insert = nft_hash_insert,
766 .activate = nft_hash_activate,
767 .deactivate = nft_hash_deactivate,
768 .flush = nft_hash_flush,
769 .remove = nft_hash_remove,
770 .lookup = nft_hash_lookup,
771 .walk = nft_hash_walk,
772 .get = nft_hash_get,
773 },
774};
775
776const struct nft_set_type nft_set_hash_fast_type = {
777 .features = NFT_SET_MAP | NFT_SET_OBJECT,
778 .ops = {
779 .privsize = nft_hash_privsize,
780 .elemsize = offsetof(struct nft_hash_elem, ext),
781 .estimate = nft_hash_fast_estimate,
782 .init = nft_hash_init,
783 .destroy = nft_hash_destroy,
784 .insert = nft_hash_insert,
785 .activate = nft_hash_activate,
786 .deactivate = nft_hash_deactivate,
787 .flush = nft_hash_flush,
788 .remove = nft_hash_remove,
789 .lookup = nft_hash_lookup_fast,
790 .walk = nft_hash_walk,
791 .get = nft_hash_get,
792 },
793};
794

source code of linux/net/netfilter/nft_set_hash.c