1 // SPDX-License-Identifier: GPL-2.0-only
3 * net/sched/sch_mqprio.c
5 * Copyright (c) 2010 John Fastabend <john.r.fastabend@intel.com>
8 #include <linux/types.h>
9 #include <linux/slab.h>
10 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/errno.h>
13 #include <linux/skbuff.h>
14 #include <linux/module.h>
15 #include <net/netlink.h>
16 #include <net/pkt_sched.h>
17 #include <net/sch_generic.h>
18 #include <net/pkt_cls.h>
20 #include "sch_mqprio_lib.h"
23 struct Qdisc **qdiscs;
28 u64 min_rate[TC_QOPT_MAX_QUEUE];
29 u64 max_rate[TC_QOPT_MAX_QUEUE];
32 static int mqprio_enable_offload(struct Qdisc *sch,
33 const struct tc_mqprio_qopt *qopt,
34 struct netlink_ext_ack *extack)
36 struct tc_mqprio_qopt_offload mqprio = {.qopt = *qopt};
37 struct mqprio_sched *priv = qdisc_priv(sch);
38 struct net_device *dev = qdisc_dev(sch);
42 case TC_MQPRIO_MODE_DCB:
43 if (priv->shaper != TC_MQPRIO_SHAPER_DCB)
46 case TC_MQPRIO_MODE_CHANNEL:
47 mqprio.flags = priv->flags;
48 if (priv->flags & TC_MQPRIO_F_MODE)
49 mqprio.mode = priv->mode;
50 if (priv->flags & TC_MQPRIO_F_SHAPER)
51 mqprio.shaper = priv->shaper;
52 if (priv->flags & TC_MQPRIO_F_MIN_RATE)
53 for (i = 0; i < mqprio.qopt.num_tc; i++)
54 mqprio.min_rate[i] = priv->min_rate[i];
55 if (priv->flags & TC_MQPRIO_F_MAX_RATE)
56 for (i = 0; i < mqprio.qopt.num_tc; i++)
57 mqprio.max_rate[i] = priv->max_rate[i];
63 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_MQPRIO,
68 priv->hw_offload = mqprio.qopt.hw;
73 static void mqprio_disable_offload(struct Qdisc *sch)
75 struct tc_mqprio_qopt_offload mqprio = { { 0 } };
76 struct mqprio_sched *priv = qdisc_priv(sch);
77 struct net_device *dev = qdisc_dev(sch);
80 case TC_MQPRIO_MODE_DCB:
81 case TC_MQPRIO_MODE_CHANNEL:
82 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_MQPRIO,
88 static void mqprio_destroy(struct Qdisc *sch)
90 struct net_device *dev = qdisc_dev(sch);
91 struct mqprio_sched *priv = qdisc_priv(sch);
96 ntx < dev->num_tx_queues && priv->qdiscs[ntx];
98 qdisc_put(priv->qdiscs[ntx]);
102 if (priv->hw_offload && dev->netdev_ops->ndo_setup_tc)
103 mqprio_disable_offload(sch);
105 netdev_set_num_tc(dev, 0);
108 static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt,
109 const struct tc_mqprio_caps *caps,
110 struct netlink_ext_ack *extack)
114 /* Limit qopt->hw to maximum supported offload value. Drivers have
115 * the option of overriding this later if they don't support the a
116 * given offload type.
118 if (qopt->hw > TC_MQPRIO_HW_OFFLOAD_MAX)
119 qopt->hw = TC_MQPRIO_HW_OFFLOAD_MAX;
121 /* If hardware offload is requested, we will leave 3 options to the
123 * - populate the queue counts itself (and ignore what was requested)
124 * - validate the provided queue counts by itself (and apply them)
125 * - request queue count validation here (and apply them)
127 err = mqprio_validate_qopt(dev, qopt,
128 !qopt->hw || caps->validate_queue_counts,
133 /* If ndo_setup_tc is not present then hardware doesn't support offload
134 * and we should return an error.
136 if (qopt->hw && !dev->netdev_ops->ndo_setup_tc)
142 static const struct nla_policy mqprio_policy[TCA_MQPRIO_MAX + 1] = {
143 [TCA_MQPRIO_MODE] = { .len = sizeof(u16) },
144 [TCA_MQPRIO_SHAPER] = { .len = sizeof(u16) },
145 [TCA_MQPRIO_MIN_RATE64] = { .type = NLA_NESTED },
146 [TCA_MQPRIO_MAX_RATE64] = { .type = NLA_NESTED },
149 static int parse_attr(struct nlattr *tb[], int maxtype, struct nlattr *nla,
150 const struct nla_policy *policy, int len)
152 int nested_len = nla_len(nla) - NLA_ALIGN(len);
154 if (nested_len >= nla_attr_size(0))
155 return nla_parse_deprecated(tb, maxtype,
156 nla_data(nla) + NLA_ALIGN(len),
157 nested_len, policy, NULL);
159 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
163 static int mqprio_parse_nlattr(struct Qdisc *sch, struct tc_mqprio_qopt *qopt,
166 struct mqprio_sched *priv = qdisc_priv(sch);
167 struct nlattr *tb[TCA_MQPRIO_MAX + 1];
171 err = parse_attr(tb, TCA_MQPRIO_MAX, opt, mqprio_policy,
179 if (tb[TCA_MQPRIO_MODE]) {
180 priv->flags |= TC_MQPRIO_F_MODE;
181 priv->mode = *(u16 *)nla_data(tb[TCA_MQPRIO_MODE]);
184 if (tb[TCA_MQPRIO_SHAPER]) {
185 priv->flags |= TC_MQPRIO_F_SHAPER;
186 priv->shaper = *(u16 *)nla_data(tb[TCA_MQPRIO_SHAPER]);
189 if (tb[TCA_MQPRIO_MIN_RATE64]) {
190 if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE)
193 nla_for_each_nested(attr, tb[TCA_MQPRIO_MIN_RATE64],
195 if (nla_type(attr) != TCA_MQPRIO_MIN_RATE64)
197 if (i >= qopt->num_tc)
199 priv->min_rate[i] = *(u64 *)nla_data(attr);
202 priv->flags |= TC_MQPRIO_F_MIN_RATE;
205 if (tb[TCA_MQPRIO_MAX_RATE64]) {
206 if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE)
209 nla_for_each_nested(attr, tb[TCA_MQPRIO_MAX_RATE64],
211 if (nla_type(attr) != TCA_MQPRIO_MAX_RATE64)
213 if (i >= qopt->num_tc)
215 priv->max_rate[i] = *(u64 *)nla_data(attr);
218 priv->flags |= TC_MQPRIO_F_MAX_RATE;
224 static int mqprio_init(struct Qdisc *sch, struct nlattr *opt,
225 struct netlink_ext_ack *extack)
227 struct net_device *dev = qdisc_dev(sch);
228 struct mqprio_sched *priv = qdisc_priv(sch);
229 struct netdev_queue *dev_queue;
231 int i, err = -EOPNOTSUPP;
232 struct tc_mqprio_qopt *qopt = NULL;
233 struct tc_mqprio_caps caps;
236 BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE);
237 BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK);
239 if (sch->parent != TC_H_ROOT)
242 if (!netif_is_multiqueue(dev))
245 /* make certain can allocate enough classids to handle queues */
246 if (dev->num_tx_queues >= TC_H_MIN_PRIORITY)
249 if (!opt || nla_len(opt) < sizeof(*qopt))
252 qdisc_offload_query_caps(dev, TC_SETUP_QDISC_MQPRIO,
253 &caps, sizeof(caps));
255 qopt = nla_data(opt);
256 if (mqprio_parse_opt(dev, qopt, &caps, extack))
259 len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt));
261 err = mqprio_parse_nlattr(sch, qopt, opt);
266 /* pre-allocate qdisc, attachment can't fail */
267 priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
272 for (i = 0; i < dev->num_tx_queues; i++) {
273 dev_queue = netdev_get_tx_queue(dev, i);
274 qdisc = qdisc_create_dflt(dev_queue,
275 get_default_qdisc_ops(dev, i),
276 TC_H_MAKE(TC_H_MAJ(sch->handle),
277 TC_H_MIN(i + 1)), extack);
281 priv->qdiscs[i] = qdisc;
282 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
285 /* If the mqprio options indicate that hardware should own
286 * the queue mapping then run ndo_setup_tc otherwise use the
287 * supplied and verified mapping
290 err = mqprio_enable_offload(sch, qopt, extack);
294 netdev_set_num_tc(dev, qopt->num_tc);
295 for (i = 0; i < qopt->num_tc; i++)
296 netdev_set_tc_queue(dev, i,
297 qopt->count[i], qopt->offset[i]);
300 /* Always use supplied priority mappings */
301 for (i = 0; i < TC_BITMASK + 1; i++)
302 netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]);
304 sch->flags |= TCQ_F_MQROOT;
308 static void mqprio_attach(struct Qdisc *sch)
310 struct net_device *dev = qdisc_dev(sch);
311 struct mqprio_sched *priv = qdisc_priv(sch);
312 struct Qdisc *qdisc, *old;
315 /* Attach underlying qdisc */
316 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
317 qdisc = priv->qdiscs[ntx];
318 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
321 if (ntx < dev->real_num_tx_queues)
322 qdisc_hash_add(qdisc, false);
328 static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch,
331 struct net_device *dev = qdisc_dev(sch);
332 unsigned long ntx = cl - 1;
334 if (ntx >= dev->num_tx_queues)
336 return netdev_get_tx_queue(dev, ntx);
339 static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
340 struct Qdisc **old, struct netlink_ext_ack *extack)
342 struct net_device *dev = qdisc_dev(sch);
343 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
348 if (dev->flags & IFF_UP)
351 *old = dev_graft_qdisc(dev_queue, new);
354 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
356 if (dev->flags & IFF_UP)
362 static int dump_rates(struct mqprio_sched *priv,
363 struct tc_mqprio_qopt *opt, struct sk_buff *skb)
368 if (priv->flags & TC_MQPRIO_F_MIN_RATE) {
369 nest = nla_nest_start_noflag(skb, TCA_MQPRIO_MIN_RATE64);
371 goto nla_put_failure;
373 for (i = 0; i < opt->num_tc; i++) {
374 if (nla_put(skb, TCA_MQPRIO_MIN_RATE64,
375 sizeof(priv->min_rate[i]),
377 goto nla_put_failure;
379 nla_nest_end(skb, nest);
382 if (priv->flags & TC_MQPRIO_F_MAX_RATE) {
383 nest = nla_nest_start_noflag(skb, TCA_MQPRIO_MAX_RATE64);
385 goto nla_put_failure;
387 for (i = 0; i < opt->num_tc; i++) {
388 if (nla_put(skb, TCA_MQPRIO_MAX_RATE64,
389 sizeof(priv->max_rate[i]),
391 goto nla_put_failure;
393 nla_nest_end(skb, nest);
398 nla_nest_cancel(skb, nest);
402 static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb)
404 struct net_device *dev = qdisc_dev(sch);
405 struct mqprio_sched *priv = qdisc_priv(sch);
406 struct nlattr *nla = (struct nlattr *)skb_tail_pointer(skb);
407 struct tc_mqprio_qopt opt = { 0 };
412 gnet_stats_basic_sync_init(&sch->bstats);
413 memset(&sch->qstats, 0, sizeof(sch->qstats));
415 /* MQ supports lockless qdiscs. However, statistics accounting needs
416 * to account for all, none, or a mix of locked and unlocked child
417 * qdiscs. Percpu stats are added to counters in-band and locking
418 * qdisc totals are added at end.
420 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
421 qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping;
422 spin_lock_bh(qdisc_lock(qdisc));
424 gnet_stats_add_basic(&sch->bstats, qdisc->cpu_bstats,
425 &qdisc->bstats, false);
426 gnet_stats_add_queue(&sch->qstats, qdisc->cpu_qstats,
428 sch->q.qlen += qdisc_qlen(qdisc);
430 spin_unlock_bh(qdisc_lock(qdisc));
433 mqprio_qopt_reconstruct(dev, &opt);
434 opt.hw = priv->hw_offload;
436 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
437 goto nla_put_failure;
439 if ((priv->flags & TC_MQPRIO_F_MODE) &&
440 nla_put_u16(skb, TCA_MQPRIO_MODE, priv->mode))
441 goto nla_put_failure;
443 if ((priv->flags & TC_MQPRIO_F_SHAPER) &&
444 nla_put_u16(skb, TCA_MQPRIO_SHAPER, priv->shaper))
445 goto nla_put_failure;
447 if ((priv->flags & TC_MQPRIO_F_MIN_RATE ||
448 priv->flags & TC_MQPRIO_F_MAX_RATE) &&
449 (dump_rates(priv, &opt, skb) != 0))
450 goto nla_put_failure;
452 return nla_nest_end(skb, nla);
454 nlmsg_trim(skb, nla);
458 static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl)
460 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
465 return dev_queue->qdisc_sleeping;
468 static unsigned long mqprio_find(struct Qdisc *sch, u32 classid)
470 struct net_device *dev = qdisc_dev(sch);
471 unsigned int ntx = TC_H_MIN(classid);
473 /* There are essentially two regions here that have valid classid
474 * values. The first region will have a classid value of 1 through
475 * num_tx_queues. All of these are backed by actual Qdiscs.
477 if (ntx < TC_H_MIN_PRIORITY)
478 return (ntx <= dev->num_tx_queues) ? ntx : 0;
480 /* The second region represents the hardware traffic classes. These
481 * are represented by classid values of TC_H_MIN_PRIORITY through
482 * TC_H_MIN_PRIORITY + netdev_get_num_tc - 1
484 return ((ntx - TC_H_MIN_PRIORITY) < netdev_get_num_tc(dev)) ? ntx : 0;
487 static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl,
488 struct sk_buff *skb, struct tcmsg *tcm)
490 if (cl < TC_H_MIN_PRIORITY) {
491 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
492 struct net_device *dev = qdisc_dev(sch);
493 int tc = netdev_txq_to_tc(dev, cl - 1);
495 tcm->tcm_parent = (tc < 0) ? 0 :
496 TC_H_MAKE(TC_H_MAJ(sch->handle),
497 TC_H_MIN(tc + TC_H_MIN_PRIORITY));
498 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
500 tcm->tcm_parent = TC_H_ROOT;
503 tcm->tcm_handle |= TC_H_MIN(cl);
507 static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
512 if (cl >= TC_H_MIN_PRIORITY) {
515 struct gnet_stats_queue qstats = {0};
516 struct gnet_stats_basic_sync bstats;
517 struct net_device *dev = qdisc_dev(sch);
518 struct netdev_tc_txq tc = dev->tc_to_txq[cl & TC_BITMASK];
520 gnet_stats_basic_sync_init(&bstats);
521 /* Drop lock here it will be reclaimed before touching
522 * statistics this is required because the d->lock we
523 * hold here is the look on dev_queue->qdisc_sleeping
524 * also acquired below.
527 spin_unlock_bh(d->lock);
529 for (i = tc.offset; i < tc.offset + tc.count; i++) {
530 struct netdev_queue *q = netdev_get_tx_queue(dev, i);
531 struct Qdisc *qdisc = rtnl_dereference(q->qdisc);
533 spin_lock_bh(qdisc_lock(qdisc));
535 gnet_stats_add_basic(&bstats, qdisc->cpu_bstats,
536 &qdisc->bstats, false);
537 gnet_stats_add_queue(&qstats, qdisc->cpu_qstats,
539 sch->q.qlen += qdisc_qlen(qdisc);
541 spin_unlock_bh(qdisc_lock(qdisc));
543 qlen = qdisc_qlen(sch) + qstats.qlen;
545 /* Reclaim root sleeping lock before completing stats */
547 spin_lock_bh(d->lock);
548 if (gnet_stats_copy_basic(d, NULL, &bstats, false) < 0 ||
549 gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
552 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
554 sch = dev_queue->qdisc_sleeping;
555 if (gnet_stats_copy_basic(d, sch->cpu_bstats,
556 &sch->bstats, true) < 0 ||
557 qdisc_qstats_copy(d, sch) < 0)
563 static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
565 struct net_device *dev = qdisc_dev(sch);
571 /* Walk hierarchy with a virtual class per tc */
572 arg->count = arg->skip;
573 for (ntx = arg->skip; ntx < netdev_get_num_tc(dev); ntx++) {
574 if (!tc_qdisc_stats_dump(sch, ntx + TC_H_MIN_PRIORITY, arg))
578 /* Pad the values and skip over unused traffic classes */
579 if (ntx < TC_MAX_QUEUE) {
580 arg->count = TC_MAX_QUEUE;
584 /* Reset offset, sort out remaining per-queue qdiscs */
585 for (ntx -= TC_MAX_QUEUE; ntx < dev->num_tx_queues; ntx++) {
586 if (arg->fn(sch, ntx + 1, arg) < 0) {
594 static struct netdev_queue *mqprio_select_queue(struct Qdisc *sch,
597 return mqprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
600 static const struct Qdisc_class_ops mqprio_class_ops = {
601 .graft = mqprio_graft,
605 .dump = mqprio_dump_class,
606 .dump_stats = mqprio_dump_class_stats,
607 .select_queue = mqprio_select_queue,
610 static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
611 .cl_ops = &mqprio_class_ops,
613 .priv_size = sizeof(struct mqprio_sched),
615 .destroy = mqprio_destroy,
616 .attach = mqprio_attach,
617 .change_real_num_tx = mq_change_real_num_tx,
619 .owner = THIS_MODULE,
622 static int __init mqprio_module_init(void)
624 return register_qdisc(&mqprio_qdisc_ops);
627 static void __exit mqprio_module_exit(void)
629 unregister_qdisc(&mqprio_qdisc_ops);
632 module_init(mqprio_module_init);
633 module_exit(mqprio_module_exit);
635 MODULE_LICENSE("GPL");