1 // SPDX-License-Identifier: GPL-2.0-only
3 * net/sched/sch_ets.c Enhanced Transmission Selection scheduler
8 * The Enhanced Transmission Selection scheduler is a classful queuing
9 * discipline that merges functionality of PRIO and DRR qdiscs in one scheduler.
10 * ETS makes it easy to configure a set of strict and bandwidth-sharing bands to
11 * implement the transmission selection described in 802.1Qaz.
13 * Although ETS is technically classful, it's not possible to add and remove
14 * classes at will. Instead one specifies number of classes, how many are
15 * PRIO-like and how many DRR-like, and quanta for the latter.
20 * The strict classes, if any, are tried for traffic first: first band 0, if it
21 * has no traffic then band 1, etc.
23 * When there is no traffic in any of the strict queues, the bandwidth-sharing
24 * ones are tried next. Each band is assigned a deficit counter, initialized to
25 * "quantum" of that band. ETS maintains a list of active bandwidth-sharing
26 * bands whose qdiscs are non-empty. A packet is dequeued from the band at the
27 * head of the list if the packet size is smaller or equal to the deficit
28 * counter. If the counter is too small, it is increased by "quantum" and the
29 * scheduler moves on to the next band in the active list.
32 #include <linux/module.h>
33 #include <net/gen_stats.h>
34 #include <net/netlink.h>
35 #include <net/pkt_cls.h>
36 #include <net/pkt_sched.h>
37 #include <net/sch_generic.h>
40 struct list_head alist; /* In struct ets_sched.active. */
44 struct gnet_stats_basic_sync bstats;
45 struct gnet_stats_queue qstats;
49 struct list_head active;
50 struct tcf_proto __rcu *filter_list;
51 struct tcf_block *block;
54 u8 prio2band[TC_PRIO_MAX + 1];
55 struct ets_class classes[TCQ_ETS_MAX_BANDS];
58 static const struct nla_policy ets_policy[TCA_ETS_MAX + 1] = {
59 [TCA_ETS_NBANDS] = { .type = NLA_U8 },
60 [TCA_ETS_NSTRICT] = { .type = NLA_U8 },
61 [TCA_ETS_QUANTA] = { .type = NLA_NESTED },
62 [TCA_ETS_PRIOMAP] = { .type = NLA_NESTED },
65 static const struct nla_policy ets_priomap_policy[TCA_ETS_MAX + 1] = {
66 [TCA_ETS_PRIOMAP_BAND] = { .type = NLA_U8 },
69 static const struct nla_policy ets_quanta_policy[TCA_ETS_MAX + 1] = {
70 [TCA_ETS_QUANTA_BAND] = { .type = NLA_U32 },
73 static const struct nla_policy ets_class_policy[TCA_ETS_MAX + 1] = {
74 [TCA_ETS_QUANTA_BAND] = { .type = NLA_U32 },
77 static int ets_quantum_parse(struct Qdisc *sch, const struct nlattr *attr,
78 unsigned int *quantum,
79 struct netlink_ext_ack *extack)
81 *quantum = nla_get_u32(attr);
83 NL_SET_ERR_MSG(extack, "ETS quantum cannot be zero");
89 static struct ets_class *
90 ets_class_from_arg(struct Qdisc *sch, unsigned long arg)
92 struct ets_sched *q = qdisc_priv(sch);
94 return &q->classes[arg - 1];
97 static u32 ets_class_id(struct Qdisc *sch, const struct ets_class *cl)
99 struct ets_sched *q = qdisc_priv(sch);
100 int band = cl - q->classes;
102 return TC_H_MAKE(sch->handle, band + 1);
105 static void ets_offload_change(struct Qdisc *sch)
107 struct net_device *dev = qdisc_dev(sch);
108 struct ets_sched *q = qdisc_priv(sch);
109 struct tc_ets_qopt_offload qopt;
110 unsigned int w_psum_prev = 0;
111 unsigned int q_psum = 0;
112 unsigned int q_sum = 0;
113 unsigned int quantum;
118 if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
121 qopt.command = TC_ETS_REPLACE;
122 qopt.handle = sch->handle;
123 qopt.parent = sch->parent;
124 qopt.replace_params.bands = q->nbands;
125 qopt.replace_params.qstats = &sch->qstats;
126 memcpy(&qopt.replace_params.priomap,
127 q->prio2band, sizeof(q->prio2band));
129 for (i = 0; i < q->nbands; i++)
130 q_sum += q->classes[i].quantum;
132 for (i = 0; i < q->nbands; i++) {
133 quantum = q->classes[i].quantum;
135 w_psum = quantum ? q_psum * 100 / q_sum : 0;
136 weight = w_psum - w_psum_prev;
137 w_psum_prev = w_psum;
139 qopt.replace_params.quanta[i] = quantum;
140 qopt.replace_params.weights[i] = weight;
143 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_ETS, &qopt);
146 static void ets_offload_destroy(struct Qdisc *sch)
148 struct net_device *dev = qdisc_dev(sch);
149 struct tc_ets_qopt_offload qopt;
151 if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
154 qopt.command = TC_ETS_DESTROY;
155 qopt.handle = sch->handle;
156 qopt.parent = sch->parent;
157 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_ETS, &qopt);
160 static void ets_offload_graft(struct Qdisc *sch, struct Qdisc *new,
161 struct Qdisc *old, unsigned long arg,
162 struct netlink_ext_ack *extack)
164 struct net_device *dev = qdisc_dev(sch);
165 struct tc_ets_qopt_offload qopt;
167 qopt.command = TC_ETS_GRAFT;
168 qopt.handle = sch->handle;
169 qopt.parent = sch->parent;
170 qopt.graft_params.band = arg - 1;
171 qopt.graft_params.child_handle = new->handle;
173 qdisc_offload_graft_helper(dev, sch, new, old, TC_SETUP_QDISC_ETS,
177 static int ets_offload_dump(struct Qdisc *sch)
179 struct tc_ets_qopt_offload qopt;
181 qopt.command = TC_ETS_STATS;
182 qopt.handle = sch->handle;
183 qopt.parent = sch->parent;
184 qopt.stats.bstats = &sch->bstats;
185 qopt.stats.qstats = &sch->qstats;
187 return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_ETS, &qopt);
190 static bool ets_class_is_strict(struct ets_sched *q, const struct ets_class *cl)
192 unsigned int band = cl - q->classes;
194 return band < q->nstrict;
197 static int ets_class_change(struct Qdisc *sch, u32 classid, u32 parentid,
198 struct nlattr **tca, unsigned long *arg,
199 struct netlink_ext_ack *extack)
201 struct ets_class *cl = ets_class_from_arg(sch, *arg);
202 struct ets_sched *q = qdisc_priv(sch);
203 struct nlattr *opt = tca[TCA_OPTIONS];
204 struct nlattr *tb[TCA_ETS_MAX + 1];
205 unsigned int quantum;
208 /* Classes can be added and removed only through Qdisc_ops.change
212 NL_SET_ERR_MSG(extack, "Fine-grained class addition and removal is not supported");
217 NL_SET_ERR_MSG(extack, "ETS options are required for this operation");
221 err = nla_parse_nested(tb, TCA_ETS_MAX, opt, ets_class_policy, extack);
225 if (!tb[TCA_ETS_QUANTA_BAND])
226 /* Nothing to configure. */
229 if (ets_class_is_strict(q, cl)) {
230 NL_SET_ERR_MSG(extack, "Strict bands do not have a configurable quantum");
234 err = ets_quantum_parse(sch, tb[TCA_ETS_QUANTA_BAND], &quantum,
240 cl->quantum = quantum;
241 sch_tree_unlock(sch);
243 ets_offload_change(sch);
247 static int ets_class_graft(struct Qdisc *sch, unsigned long arg,
248 struct Qdisc *new, struct Qdisc **old,
249 struct netlink_ext_ack *extack)
251 struct ets_class *cl = ets_class_from_arg(sch, arg);
254 new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
255 ets_class_id(sch, cl), NULL);
259 qdisc_hash_add(new, true);
262 *old = qdisc_replace(sch, new, &cl->qdisc);
263 ets_offload_graft(sch, new, *old, arg, extack);
267 static struct Qdisc *ets_class_leaf(struct Qdisc *sch, unsigned long arg)
269 struct ets_class *cl = ets_class_from_arg(sch, arg);
274 static unsigned long ets_class_find(struct Qdisc *sch, u32 classid)
276 unsigned long band = TC_H_MIN(classid);
277 struct ets_sched *q = qdisc_priv(sch);
279 if (band - 1 >= q->nbands)
284 static void ets_class_qlen_notify(struct Qdisc *sch, unsigned long arg)
286 struct ets_class *cl = ets_class_from_arg(sch, arg);
287 struct ets_sched *q = qdisc_priv(sch);
289 /* We get notified about zero-length child Qdiscs as well if they are
290 * offloaded. Those aren't on the active list though, so don't attempt
293 if (!ets_class_is_strict(q, cl) && sch->q.qlen)
294 list_del(&cl->alist);
297 static int ets_class_dump(struct Qdisc *sch, unsigned long arg,
298 struct sk_buff *skb, struct tcmsg *tcm)
300 struct ets_class *cl = ets_class_from_arg(sch, arg);
301 struct ets_sched *q = qdisc_priv(sch);
304 tcm->tcm_parent = TC_H_ROOT;
305 tcm->tcm_handle = ets_class_id(sch, cl);
306 tcm->tcm_info = cl->qdisc->handle;
308 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
310 goto nla_put_failure;
311 if (!ets_class_is_strict(q, cl)) {
312 if (nla_put_u32(skb, TCA_ETS_QUANTA_BAND, cl->quantum))
313 goto nla_put_failure;
315 return nla_nest_end(skb, nest);
318 nla_nest_cancel(skb, nest);
322 static int ets_class_dump_stats(struct Qdisc *sch, unsigned long arg,
325 struct ets_class *cl = ets_class_from_arg(sch, arg);
326 struct Qdisc *cl_q = cl->qdisc;
328 if (gnet_stats_copy_basic(d, NULL, &cl_q->bstats, true) < 0 ||
329 qdisc_qstats_copy(d, cl_q) < 0)
335 static void ets_qdisc_walk(struct Qdisc *sch, struct qdisc_walker *arg)
337 struct ets_sched *q = qdisc_priv(sch);
343 for (i = 0; i < q->nbands; i++) {
344 if (arg->count < arg->skip) {
348 if (arg->fn(sch, i + 1, arg) < 0) {
356 static struct tcf_block *
357 ets_qdisc_tcf_block(struct Qdisc *sch, unsigned long cl,
358 struct netlink_ext_ack *extack)
360 struct ets_sched *q = qdisc_priv(sch);
363 NL_SET_ERR_MSG(extack, "ETS classid must be zero");
370 static unsigned long ets_qdisc_bind_tcf(struct Qdisc *sch, unsigned long parent,
373 return ets_class_find(sch, classid);
376 static void ets_qdisc_unbind_tcf(struct Qdisc *sch, unsigned long arg)
380 static struct ets_class *ets_classify(struct sk_buff *skb, struct Qdisc *sch,
383 struct ets_sched *q = qdisc_priv(sch);
384 u32 band = skb->priority;
385 struct tcf_result res;
386 struct tcf_proto *fl;
389 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
390 if (TC_H_MAJ(skb->priority) != sch->handle) {
391 fl = rcu_dereference_bh(q->filter_list);
392 err = tcf_classify(skb, NULL, fl, &res, false);
393 #ifdef CONFIG_NET_CLS_ACT
398 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
404 if (!fl || err < 0) {
407 return &q->classes[q->prio2band[band & TC_PRIO_MAX]];
411 band = TC_H_MIN(band) - 1;
412 if (band >= q->nbands)
413 return &q->classes[q->prio2band[0]];
414 return &q->classes[band];
417 static int ets_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
418 struct sk_buff **to_free)
420 unsigned int len = qdisc_pkt_len(skb);
421 struct ets_sched *q = qdisc_priv(sch);
422 struct ets_class *cl;
426 cl = ets_classify(skb, sch, &err);
428 if (err & __NET_XMIT_BYPASS)
429 qdisc_qstats_drop(sch);
430 __qdisc_drop(skb, to_free);
434 first = !cl->qdisc->q.qlen;
435 err = qdisc_enqueue(skb, cl->qdisc, to_free);
436 if (unlikely(err != NET_XMIT_SUCCESS)) {
437 if (net_xmit_drop_count(err)) {
439 qdisc_qstats_drop(sch);
444 if (first && !ets_class_is_strict(q, cl)) {
445 list_add_tail(&cl->alist, &q->active);
446 cl->deficit = cl->quantum;
449 sch->qstats.backlog += len;
454 static struct sk_buff *
455 ets_qdisc_dequeue_skb(struct Qdisc *sch, struct sk_buff *skb)
457 qdisc_bstats_update(sch, skb);
458 qdisc_qstats_backlog_dec(sch, skb);
463 static struct sk_buff *ets_qdisc_dequeue(struct Qdisc *sch)
465 struct ets_sched *q = qdisc_priv(sch);
466 struct ets_class *cl;
472 for (band = 0; band < q->nstrict; band++) {
473 cl = &q->classes[band];
474 skb = qdisc_dequeue_peeked(cl->qdisc);
476 return ets_qdisc_dequeue_skb(sch, skb);
479 if (list_empty(&q->active))
482 cl = list_first_entry(&q->active, struct ets_class, alist);
483 skb = cl->qdisc->ops->peek(cl->qdisc);
485 qdisc_warn_nonwc(__func__, cl->qdisc);
489 len = qdisc_pkt_len(skb);
490 if (len <= cl->deficit) {
492 skb = qdisc_dequeue_peeked(cl->qdisc);
495 if (cl->qdisc->q.qlen == 0)
496 list_del(&cl->alist);
497 return ets_qdisc_dequeue_skb(sch, skb);
500 cl->deficit += cl->quantum;
501 list_move_tail(&cl->alist, &q->active);
507 static int ets_qdisc_priomap_parse(struct nlattr *priomap_attr,
508 unsigned int nbands, u8 *priomap,
509 struct netlink_ext_ack *extack)
511 const struct nlattr *attr;
517 err = __nla_validate_nested(priomap_attr, TCA_ETS_MAX,
518 ets_priomap_policy, NL_VALIDATE_STRICT,
523 nla_for_each_nested(attr, priomap_attr, rem) {
524 switch (nla_type(attr)) {
525 case TCA_ETS_PRIOMAP_BAND:
526 if (prio > TC_PRIO_MAX) {
527 NL_SET_ERR_MSG_MOD(extack, "Too many priorities in ETS priomap");
530 band = nla_get_u8(attr);
531 if (band >= nbands) {
532 NL_SET_ERR_MSG_MOD(extack, "Invalid band number in ETS priomap");
535 priomap[prio++] = band;
538 WARN_ON_ONCE(1); /* Validate should have caught this. */
546 static int ets_qdisc_quanta_parse(struct Qdisc *sch, struct nlattr *quanta_attr,
547 unsigned int nbands, unsigned int nstrict,
548 unsigned int *quanta,
549 struct netlink_ext_ack *extack)
551 const struct nlattr *attr;
556 err = __nla_validate_nested(quanta_attr, TCA_ETS_MAX,
557 ets_quanta_policy, NL_VALIDATE_STRICT,
562 nla_for_each_nested(attr, quanta_attr, rem) {
563 switch (nla_type(attr)) {
564 case TCA_ETS_QUANTA_BAND:
565 if (band >= nbands) {
566 NL_SET_ERR_MSG_MOD(extack, "ETS quanta has more values than bands");
569 err = ets_quantum_parse(sch, attr, &quanta[band++],
575 WARN_ON_ONCE(1); /* Validate should have caught this. */
583 static int ets_qdisc_change(struct Qdisc *sch, struct nlattr *opt,
584 struct netlink_ext_ack *extack)
586 unsigned int quanta[TCQ_ETS_MAX_BANDS] = {0};
587 struct Qdisc *queues[TCQ_ETS_MAX_BANDS];
588 struct ets_sched *q = qdisc_priv(sch);
589 struct nlattr *tb[TCA_ETS_MAX + 1];
590 unsigned int oldbands = q->nbands;
591 u8 priomap[TC_PRIO_MAX + 1];
592 unsigned int nstrict = 0;
597 err = nla_parse_nested(tb, TCA_ETS_MAX, opt, ets_policy, extack);
601 if (!tb[TCA_ETS_NBANDS]) {
602 NL_SET_ERR_MSG_MOD(extack, "Number of bands is a required argument");
605 nbands = nla_get_u8(tb[TCA_ETS_NBANDS]);
606 if (nbands < 1 || nbands > TCQ_ETS_MAX_BANDS) {
607 NL_SET_ERR_MSG_MOD(extack, "Invalid number of bands");
610 /* Unless overridden, traffic goes to the last band. */
611 memset(priomap, nbands - 1, sizeof(priomap));
613 if (tb[TCA_ETS_NSTRICT]) {
614 nstrict = nla_get_u8(tb[TCA_ETS_NSTRICT]);
615 if (nstrict > nbands) {
616 NL_SET_ERR_MSG_MOD(extack, "Invalid number of strict bands");
621 if (tb[TCA_ETS_PRIOMAP]) {
622 err = ets_qdisc_priomap_parse(tb[TCA_ETS_PRIOMAP],
623 nbands, priomap, extack);
628 if (tb[TCA_ETS_QUANTA]) {
629 err = ets_qdisc_quanta_parse(sch, tb[TCA_ETS_QUANTA],
630 nbands, nstrict, quanta, extack);
634 /* If there are more bands than strict + quanta provided, the remaining
635 * ones are ETS with quantum of MTU. Initialize the missing values here.
637 for (i = nstrict; i < nbands; i++) {
639 quanta[i] = psched_mtu(qdisc_dev(sch));
642 /* Before commit, make sure we can allocate all new qdiscs */
643 for (i = oldbands; i < nbands; i++) {
644 queues[i] = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
645 ets_class_id(sch, &q->classes[i]),
649 qdisc_put(queues[--i]);
657 for (i = nstrict; i < q->nstrict; i++) {
658 if (q->classes[i].qdisc->q.qlen) {
659 list_add_tail(&q->classes[i].alist, &q->active);
660 q->classes[i].deficit = quanta[i];
663 for (i = q->nbands; i < oldbands; i++) {
664 if (i >= q->nstrict && q->classes[i].qdisc->q.qlen)
665 list_del(&q->classes[i].alist);
666 qdisc_tree_flush_backlog(q->classes[i].qdisc);
668 q->nstrict = nstrict;
669 memcpy(q->prio2band, priomap, sizeof(priomap));
671 for (i = 0; i < q->nbands; i++)
672 q->classes[i].quantum = quanta[i];
674 for (i = oldbands; i < q->nbands; i++) {
675 q->classes[i].qdisc = queues[i];
676 if (q->classes[i].qdisc != &noop_qdisc)
677 qdisc_hash_add(q->classes[i].qdisc, true);
680 sch_tree_unlock(sch);
682 ets_offload_change(sch);
683 for (i = q->nbands; i < oldbands; i++) {
684 qdisc_put(q->classes[i].qdisc);
685 q->classes[i].qdisc = NULL;
686 q->classes[i].quantum = 0;
687 q->classes[i].deficit = 0;
688 gnet_stats_basic_sync_init(&q->classes[i].bstats);
689 memset(&q->classes[i].qstats, 0, sizeof(q->classes[i].qstats));
694 static int ets_qdisc_init(struct Qdisc *sch, struct nlattr *opt,
695 struct netlink_ext_ack *extack)
697 struct ets_sched *q = qdisc_priv(sch);
703 err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
707 INIT_LIST_HEAD(&q->active);
708 for (i = 0; i < TCQ_ETS_MAX_BANDS; i++)
709 INIT_LIST_HEAD(&q->classes[i].alist);
711 return ets_qdisc_change(sch, opt, extack);
714 static void ets_qdisc_reset(struct Qdisc *sch)
716 struct ets_sched *q = qdisc_priv(sch);
719 for (band = q->nstrict; band < q->nbands; band++) {
720 if (q->classes[band].qdisc->q.qlen)
721 list_del(&q->classes[band].alist);
723 for (band = 0; band < q->nbands; band++)
724 qdisc_reset(q->classes[band].qdisc);
727 static void ets_qdisc_destroy(struct Qdisc *sch)
729 struct ets_sched *q = qdisc_priv(sch);
732 ets_offload_destroy(sch);
733 tcf_block_put(q->block);
734 for (band = 0; band < q->nbands; band++)
735 qdisc_put(q->classes[band].qdisc);
738 static int ets_qdisc_dump(struct Qdisc *sch, struct sk_buff *skb)
740 struct ets_sched *q = qdisc_priv(sch);
747 err = ets_offload_dump(sch);
751 opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
755 if (nla_put_u8(skb, TCA_ETS_NBANDS, q->nbands))
759 nla_put_u8(skb, TCA_ETS_NSTRICT, q->nstrict))
762 if (q->nbands > q->nstrict) {
763 nest = nla_nest_start(skb, TCA_ETS_QUANTA);
767 for (band = q->nstrict; band < q->nbands; band++) {
768 if (nla_put_u32(skb, TCA_ETS_QUANTA_BAND,
769 q->classes[band].quantum))
773 nla_nest_end(skb, nest);
776 nest = nla_nest_start(skb, TCA_ETS_PRIOMAP);
780 for (prio = 0; prio <= TC_PRIO_MAX; prio++) {
781 if (nla_put_u8(skb, TCA_ETS_PRIOMAP_BAND, q->prio2band[prio]))
785 nla_nest_end(skb, nest);
787 return nla_nest_end(skb, opts);
790 nla_nest_cancel(skb, opts);
794 static const struct Qdisc_class_ops ets_class_ops = {
795 .change = ets_class_change,
796 .graft = ets_class_graft,
797 .leaf = ets_class_leaf,
798 .find = ets_class_find,
799 .qlen_notify = ets_class_qlen_notify,
800 .dump = ets_class_dump,
801 .dump_stats = ets_class_dump_stats,
802 .walk = ets_qdisc_walk,
803 .tcf_block = ets_qdisc_tcf_block,
804 .bind_tcf = ets_qdisc_bind_tcf,
805 .unbind_tcf = ets_qdisc_unbind_tcf,
808 static struct Qdisc_ops ets_qdisc_ops __read_mostly = {
809 .cl_ops = &ets_class_ops,
811 .priv_size = sizeof(struct ets_sched),
812 .enqueue = ets_qdisc_enqueue,
813 .dequeue = ets_qdisc_dequeue,
814 .peek = qdisc_peek_dequeued,
815 .change = ets_qdisc_change,
816 .init = ets_qdisc_init,
817 .reset = ets_qdisc_reset,
818 .destroy = ets_qdisc_destroy,
819 .dump = ets_qdisc_dump,
820 .owner = THIS_MODULE,
823 static int __init ets_init(void)
825 return register_qdisc(&ets_qdisc_ops);
828 static void __exit ets_exit(void)
830 unregister_qdisc(&ets_qdisc_ops);
833 module_init(ets_init);
834 module_exit(ets_exit);
835 MODULE_LICENSE("GPL");