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>
19 #include <net/tc_wrapper.h>
22 * Passing parameters to the root seems to be done more awkwardly than really
23 * necessary. At least, u32 doesn't seem to use such dirty hacks. To be
27 #define PERFECT_HASH_THRESHOLD 64 /* use perfect hash if not bigger */
28 #define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */
33 struct tcindex_filter_result {
35 struct tcf_result res;
36 struct tcindex_data *p;
37 struct rcu_work rwork;
40 struct tcindex_filter {
42 struct tcindex_filter_result result;
43 struct tcindex_filter __rcu *next;
44 struct rcu_work rwork;
49 struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */
50 struct tcindex_filter __rcu **h; /* imperfect hash; */
52 u16 mask; /* AND key with mask */
53 u32 shift; /* shift ANDed key to the right */
54 u32 hash; /* hash table size; 0 if undefined */
55 u32 alloc_hash; /* allocated size */
56 u32 fall_through; /* 0: only classify if explicit match */
57 refcount_t refcnt; /* a temporary refcnt for perfect hash */
58 struct rcu_work rwork;
61 static inline int tcindex_filter_is_set(struct tcindex_filter_result *r)
63 return tcf_exts_has_actions(&r->exts) || r->res.classid;
66 static void tcindex_data_get(struct tcindex_data *p)
68 refcount_inc(&p->refcnt);
71 static void tcindex_data_put(struct tcindex_data *p)
73 if (refcount_dec_and_test(&p->refcnt)) {
80 static struct tcindex_filter_result *tcindex_lookup(struct tcindex_data *p,
84 struct tcindex_filter_result *f = p->perfect + key;
86 return tcindex_filter_is_set(f) ? f : NULL;
88 struct tcindex_filter __rcu **fp;
89 struct tcindex_filter *f;
91 fp = &p->h[key % p->hash];
92 for (f = rcu_dereference_bh_rtnl(*fp);
94 fp = &f->next, f = rcu_dereference_bh_rtnl(*fp))
102 TC_INDIRECT_SCOPE int tcindex_classify(struct sk_buff *skb,
103 const struct tcf_proto *tp,
104 struct tcf_result *res)
106 struct tcindex_data *p = rcu_dereference_bh(tp->root);
107 struct tcindex_filter_result *f;
108 int key = (skb->tc_index & p->mask) >> p->shift;
110 pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n",
113 f = tcindex_lookup(p, key);
115 struct Qdisc *q = tcf_block_q(tp->chain->block);
117 if (!p->fall_through)
119 res->classid = TC_H_MAKE(TC_H_MAJ(q->handle), key);
121 pr_debug("alg 0x%x\n", res->classid);
125 pr_debug("map 0x%x\n", res->classid);
127 return tcf_exts_exec(skb, &f->exts, res);
131 static void *tcindex_get(struct tcf_proto *tp, u32 handle)
133 struct tcindex_data *p = rtnl_dereference(tp->root);
134 struct tcindex_filter_result *r;
136 pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp, handle);
137 if (p->perfect && handle >= p->alloc_hash)
139 r = tcindex_lookup(p, handle);
140 return r && tcindex_filter_is_set(r) ? r : NULL;
143 static int tcindex_init(struct tcf_proto *tp)
145 struct tcindex_data *p;
147 pr_debug("tcindex_init(tp %p)\n", tp);
148 p = kzalloc(sizeof(struct tcindex_data), GFP_KERNEL);
153 p->hash = DEFAULT_HASH_SIZE;
155 refcount_set(&p->refcnt, 1); /* Paired with tcindex_destroy_work() */
157 rcu_assign_pointer(tp->root, p);
161 static void __tcindex_destroy_rexts(struct tcindex_filter_result *r)
163 tcf_exts_destroy(&r->exts);
164 tcf_exts_put_net(&r->exts);
165 tcindex_data_put(r->p);
168 static void tcindex_destroy_rexts_work(struct work_struct *work)
170 struct tcindex_filter_result *r;
172 r = container_of(to_rcu_work(work),
173 struct tcindex_filter_result,
176 __tcindex_destroy_rexts(r);
180 static void __tcindex_destroy_fexts(struct tcindex_filter *f)
182 tcf_exts_destroy(&f->result.exts);
183 tcf_exts_put_net(&f->result.exts);
187 static void tcindex_destroy_fexts_work(struct work_struct *work)
189 struct tcindex_filter *f = container_of(to_rcu_work(work),
190 struct tcindex_filter,
194 __tcindex_destroy_fexts(f);
198 static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last,
199 bool rtnl_held, struct netlink_ext_ack *extack)
201 struct tcindex_data *p = rtnl_dereference(tp->root);
202 struct tcindex_filter_result *r = arg;
203 struct tcindex_filter __rcu **walk;
204 struct tcindex_filter *f = NULL;
206 pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp, arg, p);
213 for (i = 0; i < p->hash; i++) {
215 for (f = rtnl_dereference(*walk); f;
216 walk = &f->next, f = rtnl_dereference(*walk)) {
224 rcu_assign_pointer(*walk, rtnl_dereference(f->next));
226 tcf_unbind_filter(tp, &r->res);
227 /* all classifiers are required to call tcf_exts_destroy() after rcu
228 * grace period, since converted-to-rcu actions are relying on that
229 * in cleanup() callback
232 if (tcf_exts_get_net(&f->result.exts))
233 tcf_queue_work(&f->rwork, tcindex_destroy_fexts_work);
235 __tcindex_destroy_fexts(f);
239 if (tcf_exts_get_net(&r->exts))
240 tcf_queue_work(&r->rwork, tcindex_destroy_rexts_work);
242 __tcindex_destroy_rexts(r);
249 static void tcindex_destroy_work(struct work_struct *work)
251 struct tcindex_data *p = container_of(to_rcu_work(work),
259 valid_perfect_hash(struct tcindex_data *p)
261 return p->hash > (p->mask >> p->shift);
264 static const struct nla_policy tcindex_policy[TCA_TCINDEX_MAX + 1] = {
265 [TCA_TCINDEX_HASH] = { .type = NLA_U32 },
266 [TCA_TCINDEX_MASK] = { .type = NLA_U16 },
267 [TCA_TCINDEX_SHIFT] = { .type = NLA_U32 },
268 [TCA_TCINDEX_FALL_THROUGH] = { .type = NLA_U32 },
269 [TCA_TCINDEX_CLASSID] = { .type = NLA_U32 },
272 static int tcindex_filter_result_init(struct tcindex_filter_result *r,
273 struct tcindex_data *p,
276 memset(r, 0, sizeof(*r));
278 return tcf_exts_init(&r->exts, net, TCA_TCINDEX_ACT,
282 static void tcindex_free_perfect_hash(struct tcindex_data *cp);
284 static void tcindex_partial_destroy_work(struct work_struct *work)
286 struct tcindex_data *p = container_of(to_rcu_work(work),
292 tcindex_free_perfect_hash(p);
297 static void tcindex_free_perfect_hash(struct tcindex_data *cp)
301 for (i = 0; i < cp->hash; i++)
302 tcf_exts_destroy(&cp->perfect[i].exts);
306 static int tcindex_alloc_perfect_hash(struct net *net, struct tcindex_data *cp)
310 cp->perfect = kcalloc(cp->hash, sizeof(struct tcindex_filter_result),
311 GFP_KERNEL | __GFP_NOWARN);
315 for (i = 0; i < cp->hash; i++) {
316 err = tcf_exts_init(&cp->perfect[i].exts, net,
317 TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
320 cp->perfect[i].p = cp;
326 tcindex_free_perfect_hash(cp);
331 tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
332 u32 handle, struct tcindex_data *p,
333 struct tcindex_filter_result *r, struct nlattr **tb,
334 struct nlattr *est, u32 flags, struct netlink_ext_ack *extack)
336 struct tcindex_filter_result new_filter_result;
337 struct tcindex_data *cp = NULL, *oldp;
338 struct tcindex_filter *f = NULL; /* make gcc behave */
339 struct tcf_result cr = {};
343 err = tcf_exts_init(&e, net, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
346 err = tcf_exts_validate(net, tp, tb, est, &e, flags, extack);
351 /* tcindex_data attributes must look atomic to classifier/lookup so
352 * allocate new tcindex data and RCU assign it onto root. Keeping
353 * perfect hash and hash pointers from old data.
355 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
360 cp->shift = p->shift;
362 cp->alloc_hash = p->alloc_hash;
363 cp->fall_through = p->fall_through;
365 refcount_set(&cp->refcnt, 1); /* Paired with tcindex_destroy_work() */
367 if (tb[TCA_TCINDEX_HASH])
368 cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
370 if (tb[TCA_TCINDEX_MASK])
371 cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
373 if (tb[TCA_TCINDEX_SHIFT]) {
374 cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
375 if (cp->shift > 16) {
381 /* Hash not specified, use perfect hash if the upper limit
382 * of the hashing index is below the threshold.
384 if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD)
385 cp->hash = (cp->mask >> cp->shift) + 1;
387 cp->hash = DEFAULT_HASH_SIZE;
393 if (tcindex_alloc_perfect_hash(net, cp) < 0)
395 cp->alloc_hash = cp->hash;
396 for (i = 0; i < min(cp->hash, p->hash); i++)
397 cp->perfect[i].res = p->perfect[i].res;
402 err = tcindex_filter_result_init(&new_filter_result, cp, net);
410 /* Hash already allocated, make sure that we still meet the
411 * requirements for the allocated hash.
414 if (!valid_perfect_hash(cp) ||
415 cp->hash > cp->alloc_hash)
417 } else if (cp->h && cp->hash != cp->alloc_hash) {
422 if (tb[TCA_TCINDEX_FALL_THROUGH])
423 cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
425 if (!cp->perfect && !cp->h)
426 cp->alloc_hash = cp->hash;
428 /* Note: this could be as restrictive as if (handle & ~(mask >> shift))
429 * but then, we'd fail handles that may become valid after some future
430 * mask change. While this is extremely unlikely to ever matter,
431 * the check below is safer (and also more backwards-compatible).
433 if (cp->perfect || valid_perfect_hash(cp))
434 if (handle >= cp->alloc_hash)
439 if (!cp->perfect && !cp->h) {
440 if (valid_perfect_hash(cp)) {
441 if (tcindex_alloc_perfect_hash(net, cp) < 0)
445 struct tcindex_filter __rcu **hash;
447 hash = kcalloc(cp->hash,
448 sizeof(struct tcindex_filter *),
460 r = cp->perfect + handle;
462 r = tcindex_lookup(cp, handle) ? : &new_filter_result;
464 if (r == &new_filter_result) {
465 f = kzalloc(sizeof(*f), GFP_KERNEL);
470 err = tcindex_filter_result_init(&f->result, cp, net);
477 if (tb[TCA_TCINDEX_CLASSID]) {
478 cr.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]);
479 tcf_bind_filter(tp, &cr, base);
484 tcf_exts_change(&r->exts, &e);
486 rcu_assign_pointer(tp->root, cp);
488 if (r == &new_filter_result) {
489 struct tcindex_filter *nfp;
490 struct tcindex_filter __rcu **fp;
492 f->result.res = r->res;
493 tcf_exts_change(&f->result.exts, &r->exts);
495 fp = cp->h + (handle % cp->hash);
496 for (nfp = rtnl_dereference(*fp);
498 fp = &nfp->next, nfp = rtnl_dereference(*fp))
501 rcu_assign_pointer(*fp, f);
503 tcf_exts_destroy(&new_filter_result.exts);
507 tcf_queue_work(&oldp->rwork, tcindex_partial_destroy_work);
512 tcindex_free_perfect_hash(cp);
513 else if (balloc == 2)
515 tcf_exts_destroy(&new_filter_result.exts);
518 tcf_exts_destroy(&e);
523 tcindex_change(struct net *net, struct sk_buff *in_skb,
524 struct tcf_proto *tp, unsigned long base, u32 handle,
525 struct nlattr **tca, void **arg, u32 flags,
526 struct netlink_ext_ack *extack)
528 struct nlattr *opt = tca[TCA_OPTIONS];
529 struct nlattr *tb[TCA_TCINDEX_MAX + 1];
530 struct tcindex_data *p = rtnl_dereference(tp->root);
531 struct tcindex_filter_result *r = *arg;
534 pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
535 "p %p,r %p,*arg %p\n",
536 tp, handle, tca, arg, opt, p, r, *arg);
541 err = nla_parse_nested_deprecated(tb, TCA_TCINDEX_MAX, opt,
542 tcindex_policy, NULL);
546 return tcindex_set_parms(net, tp, base, handle, p, r, tb,
547 tca[TCA_RATE], flags, extack);
550 static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker,
553 struct tcindex_data *p = rtnl_dereference(tp->root);
554 struct tcindex_filter *f, *next;
557 pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp, walker, p);
559 for (i = 0; i < p->hash; i++) {
560 if (!p->perfect[i].res.class)
562 if (!tc_cls_stats_dump(tp, walker, p->perfect + i))
568 for (i = 0; i < p->hash; i++) {
569 for (f = rtnl_dereference(p->h[i]); f; f = next) {
570 next = rtnl_dereference(f->next);
571 if (!tc_cls_stats_dump(tp, walker, &f->result))
577 static void tcindex_destroy(struct tcf_proto *tp, bool rtnl_held,
578 struct netlink_ext_ack *extack)
580 struct tcindex_data *p = rtnl_dereference(tp->root);
583 pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
586 for (i = 0; i < p->hash; i++) {
587 struct tcindex_filter_result *r = p->perfect + i;
589 /* tcf_queue_work() does not guarantee the ordering we
590 * want, so we have to take this refcnt temporarily to
591 * ensure 'p' is freed after all tcindex_filter_result
592 * here. Imperfect hash does not need this, because it
593 * uses linked lists rather than an array.
597 tcf_unbind_filter(tp, &r->res);
598 if (tcf_exts_get_net(&r->exts))
599 tcf_queue_work(&r->rwork,
600 tcindex_destroy_rexts_work);
602 __tcindex_destroy_rexts(r);
606 for (i = 0; p->h && i < p->hash; i++) {
607 struct tcindex_filter *f, *next;
610 for (f = rtnl_dereference(p->h[i]); f; f = next) {
611 next = rtnl_dereference(f->next);
612 tcindex_delete(tp, &f->result, &last, rtnl_held, NULL);
616 tcf_queue_work(&p->rwork, tcindex_destroy_work);
620 static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh,
621 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
623 struct tcindex_data *p = rtnl_dereference(tp->root);
624 struct tcindex_filter_result *r = fh;
627 pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n",
628 tp, fh, skb, t, p, r);
629 pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h);
631 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
633 goto nla_put_failure;
636 t->tcm_handle = ~0; /* whatever ... */
637 if (nla_put_u32(skb, TCA_TCINDEX_HASH, p->hash) ||
638 nla_put_u16(skb, TCA_TCINDEX_MASK, p->mask) ||
639 nla_put_u32(skb, TCA_TCINDEX_SHIFT, p->shift) ||
640 nla_put_u32(skb, TCA_TCINDEX_FALL_THROUGH, p->fall_through))
641 goto nla_put_failure;
642 nla_nest_end(skb, nest);
645 t->tcm_handle = r - p->perfect;
647 struct tcindex_filter *f;
648 struct tcindex_filter __rcu **fp;
652 for (i = 0; !t->tcm_handle && i < p->hash; i++) {
654 for (f = rtnl_dereference(*fp);
656 fp = &f->next, f = rtnl_dereference(*fp)) {
658 t->tcm_handle = f->key;
662 pr_debug("handle = %d\n", t->tcm_handle);
664 nla_put_u32(skb, TCA_TCINDEX_CLASSID, r->res.classid))
665 goto nla_put_failure;
667 if (tcf_exts_dump(skb, &r->exts) < 0)
668 goto nla_put_failure;
669 nla_nest_end(skb, nest);
671 if (tcf_exts_dump_stats(skb, &r->exts) < 0)
672 goto nla_put_failure;
678 nla_nest_cancel(skb, nest);
682 static void tcindex_bind_class(void *fh, u32 classid, unsigned long cl,
683 void *q, unsigned long base)
685 struct tcindex_filter_result *r = fh;
687 tc_cls_bind_class(classid, cl, q, &r->res, base);
690 static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
692 .classify = tcindex_classify,
693 .init = tcindex_init,
694 .destroy = tcindex_destroy,
696 .change = tcindex_change,
697 .delete = tcindex_delete,
698 .walk = tcindex_walk,
699 .dump = tcindex_dump,
700 .bind_class = tcindex_bind_class,
701 .owner = THIS_MODULE,
704 static int __init init_tcindex(void)
706 return register_tcf_proto_ops(&cls_tcindex_ops);
709 static void __exit exit_tcindex(void)
711 unregister_tcf_proto_ops(&cls_tcindex_ops);
714 module_init(init_tcindex)
715 module_exit(exit_tcindex)
716 MODULE_LICENSE("GPL");