1 // SPDX-License-Identifier: GPL-2.0-only
3 * net/sched/cls_tcindex.c Packet classifier for skb->tc_index
5 * Written 1998,1999 by Werner Almesberger, EPFL ICA
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/skbuff.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/refcount.h>
15 #include <net/act_api.h>
16 #include <net/netlink.h>
17 #include <net/pkt_cls.h>
18 #include <net/sch_generic.h>
21 * Passing parameters to the root seems to be done more awkwardly than really
22 * necessary. At least, u32 doesn't seem to use such dirty hacks. To be
26 #define PERFECT_HASH_THRESHOLD 64 /* use perfect hash if not bigger */
27 #define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */
32 struct tcindex_filter_result {
34 struct tcf_result res;
35 struct tcindex_data *p;
36 struct rcu_work rwork;
39 struct tcindex_filter {
41 struct tcindex_filter_result result;
42 struct tcindex_filter __rcu *next;
43 struct rcu_work rwork;
48 struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */
49 struct tcindex_filter __rcu **h; /* imperfect hash; */
51 u16 mask; /* AND key with mask */
52 u32 shift; /* shift ANDed key to the right */
53 u32 hash; /* hash table size; 0 if undefined */
54 u32 alloc_hash; /* allocated size */
55 u32 fall_through; /* 0: only classify if explicit match */
56 refcount_t refcnt; /* a temporary refcnt for perfect hash */
57 struct rcu_work rwork;
60 static inline int tcindex_filter_is_set(struct tcindex_filter_result *r)
62 return tcf_exts_has_actions(&r->exts) || r->res.classid;
65 static void tcindex_data_get(struct tcindex_data *p)
67 refcount_inc(&p->refcnt);
70 static void tcindex_data_put(struct tcindex_data *p)
72 if (refcount_dec_and_test(&p->refcnt)) {
79 static struct tcindex_filter_result *tcindex_lookup(struct tcindex_data *p,
83 struct tcindex_filter_result *f = p->perfect + key;
85 return tcindex_filter_is_set(f) ? f : NULL;
87 struct tcindex_filter __rcu **fp;
88 struct tcindex_filter *f;
90 fp = &p->h[key % p->hash];
91 for (f = rcu_dereference_bh_rtnl(*fp);
93 fp = &f->next, f = rcu_dereference_bh_rtnl(*fp))
102 static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp,
103 struct tcf_result *res)
105 struct tcindex_data *p = rcu_dereference_bh(tp->root);
106 struct tcindex_filter_result *f;
107 int key = (skb->tc_index & p->mask) >> p->shift;
109 pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n",
112 f = tcindex_lookup(p, key);
114 struct Qdisc *q = tcf_block_q(tp->chain->block);
116 if (!p->fall_through)
118 res->classid = TC_H_MAKE(TC_H_MAJ(q->handle), key);
120 pr_debug("alg 0x%x\n", res->classid);
124 pr_debug("map 0x%x\n", res->classid);
126 return tcf_exts_exec(skb, &f->exts, res);
130 static void *tcindex_get(struct tcf_proto *tp, u32 handle)
132 struct tcindex_data *p = rtnl_dereference(tp->root);
133 struct tcindex_filter_result *r;
135 pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp, handle);
136 if (p->perfect && handle >= p->alloc_hash)
138 r = tcindex_lookup(p, handle);
139 return r && tcindex_filter_is_set(r) ? r : NULL;
142 static int tcindex_init(struct tcf_proto *tp)
144 struct tcindex_data *p;
146 pr_debug("tcindex_init(tp %p)\n", tp);
147 p = kzalloc(sizeof(struct tcindex_data), GFP_KERNEL);
152 p->hash = DEFAULT_HASH_SIZE;
154 refcount_set(&p->refcnt, 1); /* Paired with tcindex_destroy_work() */
156 rcu_assign_pointer(tp->root, p);
160 static void __tcindex_destroy_rexts(struct tcindex_filter_result *r)
162 tcf_exts_destroy(&r->exts);
163 tcf_exts_put_net(&r->exts);
164 tcindex_data_put(r->p);
167 static void tcindex_destroy_rexts_work(struct work_struct *work)
169 struct tcindex_filter_result *r;
171 r = container_of(to_rcu_work(work),
172 struct tcindex_filter_result,
175 __tcindex_destroy_rexts(r);
179 static void __tcindex_destroy_fexts(struct tcindex_filter *f)
181 tcf_exts_destroy(&f->result.exts);
182 tcf_exts_put_net(&f->result.exts);
186 static void tcindex_destroy_fexts_work(struct work_struct *work)
188 struct tcindex_filter *f = container_of(to_rcu_work(work),
189 struct tcindex_filter,
193 __tcindex_destroy_fexts(f);
197 static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last,
198 bool rtnl_held, struct netlink_ext_ack *extack)
200 struct tcindex_data *p = rtnl_dereference(tp->root);
201 struct tcindex_filter_result *r = arg;
202 struct tcindex_filter __rcu **walk;
203 struct tcindex_filter *f = NULL;
205 pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp, arg, p);
212 for (i = 0; i < p->hash; i++) {
214 for (f = rtnl_dereference(*walk); f;
215 walk = &f->next, f = rtnl_dereference(*walk)) {
223 rcu_assign_pointer(*walk, rtnl_dereference(f->next));
225 tcf_unbind_filter(tp, &r->res);
226 /* all classifiers are required to call tcf_exts_destroy() after rcu
227 * grace period, since converted-to-rcu actions are relying on that
228 * in cleanup() callback
231 if (tcf_exts_get_net(&f->result.exts))
232 tcf_queue_work(&f->rwork, tcindex_destroy_fexts_work);
234 __tcindex_destroy_fexts(f);
238 if (tcf_exts_get_net(&r->exts))
239 tcf_queue_work(&r->rwork, tcindex_destroy_rexts_work);
241 __tcindex_destroy_rexts(r);
248 static void tcindex_destroy_work(struct work_struct *work)
250 struct tcindex_data *p = container_of(to_rcu_work(work),
258 valid_perfect_hash(struct tcindex_data *p)
260 return p->hash > (p->mask >> p->shift);
263 static const struct nla_policy tcindex_policy[TCA_TCINDEX_MAX + 1] = {
264 [TCA_TCINDEX_HASH] = { .type = NLA_U32 },
265 [TCA_TCINDEX_MASK] = { .type = NLA_U16 },
266 [TCA_TCINDEX_SHIFT] = { .type = NLA_U32 },
267 [TCA_TCINDEX_FALL_THROUGH] = { .type = NLA_U32 },
268 [TCA_TCINDEX_CLASSID] = { .type = NLA_U32 },
271 static int tcindex_filter_result_init(struct tcindex_filter_result *r,
272 struct tcindex_data *p,
275 memset(r, 0, sizeof(*r));
277 return tcf_exts_init(&r->exts, net, TCA_TCINDEX_ACT,
281 static void tcindex_free_perfect_hash(struct tcindex_data *cp);
283 static void tcindex_partial_destroy_work(struct work_struct *work)
285 struct tcindex_data *p = container_of(to_rcu_work(work),
291 tcindex_free_perfect_hash(p);
296 static void tcindex_free_perfect_hash(struct tcindex_data *cp)
300 for (i = 0; i < cp->hash; i++)
301 tcf_exts_destroy(&cp->perfect[i].exts);
305 static int tcindex_alloc_perfect_hash(struct net *net, struct tcindex_data *cp)
309 cp->perfect = kcalloc(cp->hash, sizeof(struct tcindex_filter_result),
310 GFP_KERNEL | __GFP_NOWARN);
314 for (i = 0; i < cp->hash; i++) {
315 err = tcf_exts_init(&cp->perfect[i].exts, net,
316 TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
319 cp->perfect[i].p = cp;
325 tcindex_free_perfect_hash(cp);
330 tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
331 u32 handle, struct tcindex_data *p,
332 struct tcindex_filter_result *r, struct nlattr **tb,
333 struct nlattr *est, u32 flags, struct netlink_ext_ack *extack)
335 struct tcindex_filter_result new_filter_result, *old_r = r;
336 struct tcindex_data *cp = NULL, *oldp;
337 struct tcindex_filter *f = NULL; /* make gcc behave */
338 struct tcf_result cr = {};
342 err = tcf_exts_init(&e, net, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
345 err = tcf_exts_validate(net, tp, tb, est, &e, flags, extack);
350 /* tcindex_data attributes must look atomic to classifier/lookup so
351 * allocate new tcindex data and RCU assign it onto root. Keeping
352 * perfect hash and hash pointers from old data.
354 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
359 cp->shift = p->shift;
361 cp->alloc_hash = p->alloc_hash;
362 cp->fall_through = p->fall_through;
364 refcount_set(&cp->refcnt, 1); /* Paired with tcindex_destroy_work() */
366 if (tb[TCA_TCINDEX_HASH])
367 cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
369 if (tb[TCA_TCINDEX_MASK])
370 cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
372 if (tb[TCA_TCINDEX_SHIFT]) {
373 cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
374 if (cp->shift > 16) {
380 /* Hash not specified, use perfect hash if the upper limit
381 * of the hashing index is below the threshold.
383 if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD)
384 cp->hash = (cp->mask >> cp->shift) + 1;
386 cp->hash = DEFAULT_HASH_SIZE;
392 if (tcindex_alloc_perfect_hash(net, cp) < 0)
394 cp->alloc_hash = cp->hash;
395 for (i = 0; i < min(cp->hash, p->hash); i++)
396 cp->perfect[i].res = p->perfect[i].res;
401 err = tcindex_filter_result_init(&new_filter_result, cp, net);
409 /* Hash already allocated, make sure that we still meet the
410 * requirements for the allocated hash.
413 if (!valid_perfect_hash(cp) ||
414 cp->hash > cp->alloc_hash)
416 } else if (cp->h && cp->hash != cp->alloc_hash) {
421 if (tb[TCA_TCINDEX_FALL_THROUGH])
422 cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
424 if (!cp->perfect && !cp->h)
425 cp->alloc_hash = cp->hash;
427 /* Note: this could be as restrictive as if (handle & ~(mask >> shift))
428 * but then, we'd fail handles that may become valid after some future
429 * mask change. While this is extremely unlikely to ever matter,
430 * the check below is safer (and also more backwards-compatible).
432 if (cp->perfect || valid_perfect_hash(cp))
433 if (handle >= cp->alloc_hash)
438 if (!cp->perfect && !cp->h) {
439 if (valid_perfect_hash(cp)) {
440 if (tcindex_alloc_perfect_hash(net, cp) < 0)
444 struct tcindex_filter __rcu **hash;
446 hash = kcalloc(cp->hash,
447 sizeof(struct tcindex_filter *),
459 r = cp->perfect + handle;
461 r = tcindex_lookup(cp, handle) ? : &new_filter_result;
463 if (r == &new_filter_result) {
464 f = kzalloc(sizeof(*f), GFP_KERNEL);
469 err = tcindex_filter_result_init(&f->result, cp, net);
476 if (tb[TCA_TCINDEX_CLASSID]) {
477 cr.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]);
478 tcf_bind_filter(tp, &cr, base);
481 if (old_r && old_r != r) {
482 err = tcindex_filter_result_init(old_r, cp, net);
491 tcf_exts_change(&r->exts, &e);
493 rcu_assign_pointer(tp->root, cp);
495 if (r == &new_filter_result) {
496 struct tcindex_filter *nfp;
497 struct tcindex_filter __rcu **fp;
499 f->result.res = r->res;
500 tcf_exts_change(&f->result.exts, &r->exts);
502 fp = cp->h + (handle % cp->hash);
503 for (nfp = rtnl_dereference(*fp);
505 fp = &nfp->next, nfp = rtnl_dereference(*fp))
508 rcu_assign_pointer(*fp, f);
510 tcf_exts_destroy(&new_filter_result.exts);
514 tcf_queue_work(&oldp->rwork, tcindex_partial_destroy_work);
519 tcindex_free_perfect_hash(cp);
520 else if (balloc == 2)
522 tcf_exts_destroy(&new_filter_result.exts);
525 tcf_exts_destroy(&e);
530 tcindex_change(struct net *net, struct sk_buff *in_skb,
531 struct tcf_proto *tp, unsigned long base, u32 handle,
532 struct nlattr **tca, void **arg, u32 flags,
533 struct netlink_ext_ack *extack)
535 struct nlattr *opt = tca[TCA_OPTIONS];
536 struct nlattr *tb[TCA_TCINDEX_MAX + 1];
537 struct tcindex_data *p = rtnl_dereference(tp->root);
538 struct tcindex_filter_result *r = *arg;
541 pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
542 "p %p,r %p,*arg %p\n",
543 tp, handle, tca, arg, opt, p, r, *arg);
548 err = nla_parse_nested_deprecated(tb, TCA_TCINDEX_MAX, opt,
549 tcindex_policy, NULL);
553 return tcindex_set_parms(net, tp, base, handle, p, r, tb,
554 tca[TCA_RATE], flags, extack);
557 static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker,
560 struct tcindex_data *p = rtnl_dereference(tp->root);
561 struct tcindex_filter *f, *next;
564 pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp, walker, p);
566 for (i = 0; i < p->hash; i++) {
567 if (!p->perfect[i].res.class)
569 if (!tc_cls_stats_dump(tp, walker, p->perfect + i))
575 for (i = 0; i < p->hash; i++) {
576 for (f = rtnl_dereference(p->h[i]); f; f = next) {
577 next = rtnl_dereference(f->next);
578 if (!tc_cls_stats_dump(tp, walker, &f->result))
584 static void tcindex_destroy(struct tcf_proto *tp, bool rtnl_held,
585 struct netlink_ext_ack *extack)
587 struct tcindex_data *p = rtnl_dereference(tp->root);
590 pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
593 for (i = 0; i < p->hash; i++) {
594 struct tcindex_filter_result *r = p->perfect + i;
596 /* tcf_queue_work() does not guarantee the ordering we
597 * want, so we have to take this refcnt temporarily to
598 * ensure 'p' is freed after all tcindex_filter_result
599 * here. Imperfect hash does not need this, because it
600 * uses linked lists rather than an array.
604 tcf_unbind_filter(tp, &r->res);
605 if (tcf_exts_get_net(&r->exts))
606 tcf_queue_work(&r->rwork,
607 tcindex_destroy_rexts_work);
609 __tcindex_destroy_rexts(r);
613 for (i = 0; p->h && i < p->hash; i++) {
614 struct tcindex_filter *f, *next;
617 for (f = rtnl_dereference(p->h[i]); f; f = next) {
618 next = rtnl_dereference(f->next);
619 tcindex_delete(tp, &f->result, &last, rtnl_held, NULL);
623 tcf_queue_work(&p->rwork, tcindex_destroy_work);
627 static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh,
628 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
630 struct tcindex_data *p = rtnl_dereference(tp->root);
631 struct tcindex_filter_result *r = fh;
634 pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n",
635 tp, fh, skb, t, p, r);
636 pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h);
638 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
640 goto nla_put_failure;
643 t->tcm_handle = ~0; /* whatever ... */
644 if (nla_put_u32(skb, TCA_TCINDEX_HASH, p->hash) ||
645 nla_put_u16(skb, TCA_TCINDEX_MASK, p->mask) ||
646 nla_put_u32(skb, TCA_TCINDEX_SHIFT, p->shift) ||
647 nla_put_u32(skb, TCA_TCINDEX_FALL_THROUGH, p->fall_through))
648 goto nla_put_failure;
649 nla_nest_end(skb, nest);
652 t->tcm_handle = r - p->perfect;
654 struct tcindex_filter *f;
655 struct tcindex_filter __rcu **fp;
659 for (i = 0; !t->tcm_handle && i < p->hash; i++) {
661 for (f = rtnl_dereference(*fp);
663 fp = &f->next, f = rtnl_dereference(*fp)) {
665 t->tcm_handle = f->key;
669 pr_debug("handle = %d\n", t->tcm_handle);
671 nla_put_u32(skb, TCA_TCINDEX_CLASSID, r->res.classid))
672 goto nla_put_failure;
674 if (tcf_exts_dump(skb, &r->exts) < 0)
675 goto nla_put_failure;
676 nla_nest_end(skb, nest);
678 if (tcf_exts_dump_stats(skb, &r->exts) < 0)
679 goto nla_put_failure;
685 nla_nest_cancel(skb, nest);
689 static void tcindex_bind_class(void *fh, u32 classid, unsigned long cl,
690 void *q, unsigned long base)
692 struct tcindex_filter_result *r = fh;
694 tc_cls_bind_class(classid, cl, q, &r->res, base);
697 static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
699 .classify = tcindex_classify,
700 .init = tcindex_init,
701 .destroy = tcindex_destroy,
703 .change = tcindex_change,
704 .delete = tcindex_delete,
705 .walk = tcindex_walk,
706 .dump = tcindex_dump,
707 .bind_class = tcindex_bind_class,
708 .owner = THIS_MODULE,
711 static int __init init_tcindex(void)
713 return register_tcf_proto_ops(&cls_tcindex_ops);
716 static void __exit exit_tcindex(void)
718 unregister_tcf_proto_ops(&cls_tcindex_ops);
721 module_init(init_tcindex)
722 module_exit(exit_tcindex)
723 MODULE_LICENSE("GPL");