GNU Linux-libre 4.19.286-gnu1
[releases.git] / net / sched / sch_mqprio.c
1 /*
2  * net/sched/sch_mqprio.c
3  *
4  * Copyright (c) 2010 John Fastabend <john.r.fastabend@intel.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  */
10
11 #include <linux/types.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/errno.h>
16 #include <linux/skbuff.h>
17 #include <linux/module.h>
18 #include <net/netlink.h>
19 #include <net/pkt_sched.h>
20 #include <net/sch_generic.h>
21 #include <net/pkt_cls.h>
22
23 struct mqprio_sched {
24         struct Qdisc            **qdiscs;
25         u16 mode;
26         u16 shaper;
27         int hw_offload;
28         u32 flags;
29         u64 min_rate[TC_QOPT_MAX_QUEUE];
30         u64 max_rate[TC_QOPT_MAX_QUEUE];
31 };
32
33 static void mqprio_destroy(struct Qdisc *sch)
34 {
35         struct net_device *dev = qdisc_dev(sch);
36         struct mqprio_sched *priv = qdisc_priv(sch);
37         unsigned int ntx;
38
39         if (priv->qdiscs) {
40                 for (ntx = 0;
41                      ntx < dev->num_tx_queues && priv->qdiscs[ntx];
42                      ntx++)
43                         qdisc_put(priv->qdiscs[ntx]);
44                 kfree(priv->qdiscs);
45         }
46
47         if (priv->hw_offload && dev->netdev_ops->ndo_setup_tc) {
48                 struct tc_mqprio_qopt_offload mqprio = { { 0 } };
49
50                 switch (priv->mode) {
51                 case TC_MQPRIO_MODE_DCB:
52                 case TC_MQPRIO_MODE_CHANNEL:
53                         dev->netdev_ops->ndo_setup_tc(dev,
54                                                       TC_SETUP_QDISC_MQPRIO,
55                                                       &mqprio);
56                         break;
57                 default:
58                         return;
59                 }
60         } else {
61                 netdev_set_num_tc(dev, 0);
62         }
63 }
64
65 static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt)
66 {
67         int i, j;
68
69         /* Verify num_tc is not out of max range */
70         if (qopt->num_tc > TC_MAX_QUEUE)
71                 return -EINVAL;
72
73         /* Verify priority mapping uses valid tcs */
74         for (i = 0; i < TC_BITMASK + 1; i++) {
75                 if (qopt->prio_tc_map[i] >= qopt->num_tc)
76                         return -EINVAL;
77         }
78
79         /* Limit qopt->hw to maximum supported offload value.  Drivers have
80          * the option of overriding this later if they don't support the a
81          * given offload type.
82          */
83         if (qopt->hw > TC_MQPRIO_HW_OFFLOAD_MAX)
84                 qopt->hw = TC_MQPRIO_HW_OFFLOAD_MAX;
85
86         /* If hardware offload is requested we will leave it to the device
87          * to either populate the queue counts itself or to validate the
88          * provided queue counts.  If ndo_setup_tc is not present then
89          * hardware doesn't support offload and we should return an error.
90          */
91         if (qopt->hw)
92                 return dev->netdev_ops->ndo_setup_tc ? 0 : -EINVAL;
93
94         for (i = 0; i < qopt->num_tc; i++) {
95                 unsigned int last = qopt->offset[i] + qopt->count[i];
96
97                 /* Verify the queue count is in tx range being equal to the
98                  * real_num_tx_queues indicates the last queue is in use.
99                  */
100                 if (qopt->offset[i] >= dev->real_num_tx_queues ||
101                     !qopt->count[i] ||
102                     last > dev->real_num_tx_queues)
103                         return -EINVAL;
104
105                 /* Verify that the offset and counts do not overlap */
106                 for (j = i + 1; j < qopt->num_tc; j++) {
107                         if (last > qopt->offset[j])
108                                 return -EINVAL;
109                 }
110         }
111
112         return 0;
113 }
114
115 static const struct nla_policy mqprio_policy[TCA_MQPRIO_MAX + 1] = {
116         [TCA_MQPRIO_MODE]       = { .len = sizeof(u16) },
117         [TCA_MQPRIO_SHAPER]     = { .len = sizeof(u16) },
118         [TCA_MQPRIO_MIN_RATE64] = { .type = NLA_NESTED },
119         [TCA_MQPRIO_MAX_RATE64] = { .type = NLA_NESTED },
120 };
121
122 static int parse_attr(struct nlattr *tb[], int maxtype, struct nlattr *nla,
123                       const struct nla_policy *policy, int len)
124 {
125         int nested_len = nla_len(nla) - NLA_ALIGN(len);
126
127         if (nested_len >= nla_attr_size(0))
128                 return nla_parse(tb, maxtype, nla_data(nla) + NLA_ALIGN(len),
129                                  nested_len, policy, NULL);
130
131         memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
132         return 0;
133 }
134
135 static int mqprio_init(struct Qdisc *sch, struct nlattr *opt,
136                        struct netlink_ext_ack *extack)
137 {
138         struct net_device *dev = qdisc_dev(sch);
139         struct mqprio_sched *priv = qdisc_priv(sch);
140         struct netdev_queue *dev_queue;
141         struct Qdisc *qdisc;
142         int i, err = -EOPNOTSUPP;
143         struct tc_mqprio_qopt *qopt = NULL;
144         struct nlattr *tb[TCA_MQPRIO_MAX + 1];
145         struct nlattr *attr;
146         int rem;
147         int len;
148
149         BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE);
150         BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK);
151
152         if (sch->parent != TC_H_ROOT)
153                 return -EOPNOTSUPP;
154
155         if (!netif_is_multiqueue(dev))
156                 return -EOPNOTSUPP;
157
158         /* make certain can allocate enough classids to handle queues */
159         if (dev->num_tx_queues >= TC_H_MIN_PRIORITY)
160                 return -ENOMEM;
161
162         if (!opt || nla_len(opt) < sizeof(*qopt))
163                 return -EINVAL;
164
165         qopt = nla_data(opt);
166         if (mqprio_parse_opt(dev, qopt))
167                 return -EINVAL;
168
169         len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt));
170         if (len > 0) {
171                 err = parse_attr(tb, TCA_MQPRIO_MAX, opt, mqprio_policy,
172                                  sizeof(*qopt));
173                 if (err < 0)
174                         return err;
175
176                 if (!qopt->hw)
177                         return -EINVAL;
178
179                 if (tb[TCA_MQPRIO_MODE]) {
180                         priv->flags |= TC_MQPRIO_F_MODE;
181                         priv->mode = *(u16 *)nla_data(tb[TCA_MQPRIO_MODE]);
182                 }
183
184                 if (tb[TCA_MQPRIO_SHAPER]) {
185                         priv->flags |= TC_MQPRIO_F_SHAPER;
186                         priv->shaper = *(u16 *)nla_data(tb[TCA_MQPRIO_SHAPER]);
187                 }
188
189                 if (tb[TCA_MQPRIO_MIN_RATE64]) {
190                         if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE)
191                                 return -EINVAL;
192                         i = 0;
193                         nla_for_each_nested(attr, tb[TCA_MQPRIO_MIN_RATE64],
194                                             rem) {
195                                 if (nla_type(attr) != TCA_MQPRIO_MIN_RATE64)
196                                         return -EINVAL;
197                                 if (i >= qopt->num_tc)
198                                         break;
199                                 priv->min_rate[i] = *(u64 *)nla_data(attr);
200                                 i++;
201                         }
202                         priv->flags |= TC_MQPRIO_F_MIN_RATE;
203                 }
204
205                 if (tb[TCA_MQPRIO_MAX_RATE64]) {
206                         if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE)
207                                 return -EINVAL;
208                         i = 0;
209                         nla_for_each_nested(attr, tb[TCA_MQPRIO_MAX_RATE64],
210                                             rem) {
211                                 if (nla_type(attr) != TCA_MQPRIO_MAX_RATE64)
212                                         return -EINVAL;
213                                 if (i >= qopt->num_tc)
214                                         break;
215                                 priv->max_rate[i] = *(u64 *)nla_data(attr);
216                                 i++;
217                         }
218                         priv->flags |= TC_MQPRIO_F_MAX_RATE;
219                 }
220         }
221
222         /* pre-allocate qdisc, attachment can't fail */
223         priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
224                                GFP_KERNEL);
225         if (!priv->qdiscs)
226                 return -ENOMEM;
227
228         for (i = 0; i < dev->num_tx_queues; i++) {
229                 dev_queue = netdev_get_tx_queue(dev, i);
230                 qdisc = qdisc_create_dflt(dev_queue,
231                                           get_default_qdisc_ops(dev, i),
232                                           TC_H_MAKE(TC_H_MAJ(sch->handle),
233                                                     TC_H_MIN(i + 1)), extack);
234                 if (!qdisc)
235                         return -ENOMEM;
236
237                 priv->qdiscs[i] = qdisc;
238                 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
239         }
240
241         /* If the mqprio options indicate that hardware should own
242          * the queue mapping then run ndo_setup_tc otherwise use the
243          * supplied and verified mapping
244          */
245         if (qopt->hw) {
246                 struct tc_mqprio_qopt_offload mqprio = {.qopt = *qopt};
247
248                 switch (priv->mode) {
249                 case TC_MQPRIO_MODE_DCB:
250                         if (priv->shaper != TC_MQPRIO_SHAPER_DCB)
251                                 return -EINVAL;
252                         break;
253                 case TC_MQPRIO_MODE_CHANNEL:
254                         mqprio.flags = priv->flags;
255                         if (priv->flags & TC_MQPRIO_F_MODE)
256                                 mqprio.mode = priv->mode;
257                         if (priv->flags & TC_MQPRIO_F_SHAPER)
258                                 mqprio.shaper = priv->shaper;
259                         if (priv->flags & TC_MQPRIO_F_MIN_RATE)
260                                 for (i = 0; i < mqprio.qopt.num_tc; i++)
261                                         mqprio.min_rate[i] = priv->min_rate[i];
262                         if (priv->flags & TC_MQPRIO_F_MAX_RATE)
263                                 for (i = 0; i < mqprio.qopt.num_tc; i++)
264                                         mqprio.max_rate[i] = priv->max_rate[i];
265                         break;
266                 default:
267                         return -EINVAL;
268                 }
269                 err = dev->netdev_ops->ndo_setup_tc(dev,
270                                                     TC_SETUP_QDISC_MQPRIO,
271                                                     &mqprio);
272                 if (err)
273                         return err;
274
275                 priv->hw_offload = mqprio.qopt.hw;
276         } else {
277                 netdev_set_num_tc(dev, qopt->num_tc);
278                 for (i = 0; i < qopt->num_tc; i++)
279                         netdev_set_tc_queue(dev, i,
280                                             qopt->count[i], qopt->offset[i]);
281         }
282
283         /* Always use supplied priority mappings */
284         for (i = 0; i < TC_BITMASK + 1; i++)
285                 netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]);
286
287         sch->flags |= TCQ_F_MQROOT;
288         return 0;
289 }
290
291 static void mqprio_attach(struct Qdisc *sch)
292 {
293         struct net_device *dev = qdisc_dev(sch);
294         struct mqprio_sched *priv = qdisc_priv(sch);
295         struct Qdisc *qdisc, *old;
296         unsigned int ntx;
297
298         /* Attach underlying qdisc */
299         for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
300                 qdisc = priv->qdiscs[ntx];
301                 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
302                 if (old)
303                         qdisc_put(old);
304                 if (ntx < dev->real_num_tx_queues)
305                         qdisc_hash_add(qdisc, false);
306         }
307         kfree(priv->qdiscs);
308         priv->qdiscs = NULL;
309 }
310
311 static void mqprio_change_real_num_tx(struct Qdisc *sch,
312                                       unsigned int new_real_tx)
313 {
314         struct net_device *dev = qdisc_dev(sch);
315         struct Qdisc *qdisc;
316         unsigned int i;
317
318         for (i = new_real_tx; i < dev->real_num_tx_queues; i++) {
319                 qdisc = netdev_get_tx_queue(dev, i)->qdisc_sleeping;
320                 /* Only update the default qdiscs we created,
321                  * qdiscs with handles are always hashed.
322                  */
323                 if (qdisc != &noop_qdisc && !qdisc->handle)
324                         qdisc_hash_del(qdisc);
325         }
326         for (i = dev->real_num_tx_queues; i < new_real_tx; i++) {
327                 qdisc = netdev_get_tx_queue(dev, i)->qdisc_sleeping;
328                 if (qdisc != &noop_qdisc && !qdisc->handle)
329                         qdisc_hash_add(qdisc, false);
330         }
331 }
332
333 static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch,
334                                              unsigned long cl)
335 {
336         struct net_device *dev = qdisc_dev(sch);
337         unsigned long ntx = cl - 1;
338
339         if (ntx >= dev->num_tx_queues)
340                 return NULL;
341         return netdev_get_tx_queue(dev, ntx);
342 }
343
344 static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
345                         struct Qdisc **old, struct netlink_ext_ack *extack)
346 {
347         struct net_device *dev = qdisc_dev(sch);
348         struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
349
350         if (!dev_queue)
351                 return -EINVAL;
352
353         if (dev->flags & IFF_UP)
354                 dev_deactivate(dev);
355
356         *old = dev_graft_qdisc(dev_queue, new);
357
358         if (new)
359                 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
360
361         if (dev->flags & IFF_UP)
362                 dev_activate(dev);
363
364         return 0;
365 }
366
367 static int dump_rates(struct mqprio_sched *priv,
368                       struct tc_mqprio_qopt *opt, struct sk_buff *skb)
369 {
370         struct nlattr *nest;
371         int i;
372
373         if (priv->flags & TC_MQPRIO_F_MIN_RATE) {
374                 nest = nla_nest_start(skb, TCA_MQPRIO_MIN_RATE64);
375                 if (!nest)
376                         goto nla_put_failure;
377
378                 for (i = 0; i < opt->num_tc; i++) {
379                         if (nla_put(skb, TCA_MQPRIO_MIN_RATE64,
380                                     sizeof(priv->min_rate[i]),
381                                     &priv->min_rate[i]))
382                                 goto nla_put_failure;
383                 }
384                 nla_nest_end(skb, nest);
385         }
386
387         if (priv->flags & TC_MQPRIO_F_MAX_RATE) {
388                 nest = nla_nest_start(skb, TCA_MQPRIO_MAX_RATE64);
389                 if (!nest)
390                         goto nla_put_failure;
391
392                 for (i = 0; i < opt->num_tc; i++) {
393                         if (nla_put(skb, TCA_MQPRIO_MAX_RATE64,
394                                     sizeof(priv->max_rate[i]),
395                                     &priv->max_rate[i]))
396                                 goto nla_put_failure;
397                 }
398                 nla_nest_end(skb, nest);
399         }
400         return 0;
401
402 nla_put_failure:
403         nla_nest_cancel(skb, nest);
404         return -1;
405 }
406
407 static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb)
408 {
409         struct net_device *dev = qdisc_dev(sch);
410         struct mqprio_sched *priv = qdisc_priv(sch);
411         struct nlattr *nla = (struct nlattr *)skb_tail_pointer(skb);
412         struct tc_mqprio_qopt opt = { 0 };
413         struct Qdisc *qdisc;
414         unsigned int ntx, tc;
415
416         sch->q.qlen = 0;
417         memset(&sch->bstats, 0, sizeof(sch->bstats));
418         memset(&sch->qstats, 0, sizeof(sch->qstats));
419
420         /* MQ supports lockless qdiscs. However, statistics accounting needs
421          * to account for all, none, or a mix of locked and unlocked child
422          * qdiscs. Percpu stats are added to counters in-band and locking
423          * qdisc totals are added at end.
424          */
425         for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
426                 qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping;
427                 spin_lock_bh(qdisc_lock(qdisc));
428
429                 if (qdisc_is_percpu_stats(qdisc)) {
430                         __u32 qlen = qdisc_qlen_sum(qdisc);
431
432                         __gnet_stats_copy_basic(NULL, &sch->bstats,
433                                                 qdisc->cpu_bstats,
434                                                 &qdisc->bstats);
435                         __gnet_stats_copy_queue(&sch->qstats,
436                                                 qdisc->cpu_qstats,
437                                                 &qdisc->qstats, qlen);
438                         sch->q.qlen             += qlen;
439                 } else {
440                         sch->q.qlen             += qdisc->q.qlen;
441                         sch->bstats.bytes       += qdisc->bstats.bytes;
442                         sch->bstats.packets     += qdisc->bstats.packets;
443                         sch->qstats.backlog     += qdisc->qstats.backlog;
444                         sch->qstats.drops       += qdisc->qstats.drops;
445                         sch->qstats.requeues    += qdisc->qstats.requeues;
446                         sch->qstats.overlimits  += qdisc->qstats.overlimits;
447                 }
448
449                 spin_unlock_bh(qdisc_lock(qdisc));
450         }
451
452         opt.num_tc = netdev_get_num_tc(dev);
453         memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
454         opt.hw = priv->hw_offload;
455
456         for (tc = 0; tc < netdev_get_num_tc(dev); tc++) {
457                 opt.count[tc] = dev->tc_to_txq[tc].count;
458                 opt.offset[tc] = dev->tc_to_txq[tc].offset;
459         }
460
461         if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
462                 goto nla_put_failure;
463
464         if ((priv->flags & TC_MQPRIO_F_MODE) &&
465             nla_put_u16(skb, TCA_MQPRIO_MODE, priv->mode))
466                 goto nla_put_failure;
467
468         if ((priv->flags & TC_MQPRIO_F_SHAPER) &&
469             nla_put_u16(skb, TCA_MQPRIO_SHAPER, priv->shaper))
470                 goto nla_put_failure;
471
472         if ((priv->flags & TC_MQPRIO_F_MIN_RATE ||
473              priv->flags & TC_MQPRIO_F_MAX_RATE) &&
474             (dump_rates(priv, &opt, skb) != 0))
475                 goto nla_put_failure;
476
477         return nla_nest_end(skb, nla);
478 nla_put_failure:
479         nlmsg_trim(skb, nla);
480         return -1;
481 }
482
483 static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl)
484 {
485         struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
486
487         if (!dev_queue)
488                 return NULL;
489
490         return dev_queue->qdisc_sleeping;
491 }
492
493 static unsigned long mqprio_find(struct Qdisc *sch, u32 classid)
494 {
495         struct net_device *dev = qdisc_dev(sch);
496         unsigned int ntx = TC_H_MIN(classid);
497
498         /* There are essentially two regions here that have valid classid
499          * values. The first region will have a classid value of 1 through
500          * num_tx_queues. All of these are backed by actual Qdiscs.
501          */
502         if (ntx < TC_H_MIN_PRIORITY)
503                 return (ntx <= dev->num_tx_queues) ? ntx : 0;
504
505         /* The second region represents the hardware traffic classes. These
506          * are represented by classid values of TC_H_MIN_PRIORITY through
507          * TC_H_MIN_PRIORITY + netdev_get_num_tc - 1
508          */
509         return ((ntx - TC_H_MIN_PRIORITY) < netdev_get_num_tc(dev)) ? ntx : 0;
510 }
511
512 static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl,
513                          struct sk_buff *skb, struct tcmsg *tcm)
514 {
515         if (cl < TC_H_MIN_PRIORITY) {
516                 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
517                 struct net_device *dev = qdisc_dev(sch);
518                 int tc = netdev_txq_to_tc(dev, cl - 1);
519
520                 tcm->tcm_parent = (tc < 0) ? 0 :
521                         TC_H_MAKE(TC_H_MAJ(sch->handle),
522                                   TC_H_MIN(tc + TC_H_MIN_PRIORITY));
523                 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
524         } else {
525                 tcm->tcm_parent = TC_H_ROOT;
526                 tcm->tcm_info = 0;
527         }
528         tcm->tcm_handle |= TC_H_MIN(cl);
529         return 0;
530 }
531
532 static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
533                                    struct gnet_dump *d)
534         __releases(d->lock)
535         __acquires(d->lock)
536 {
537         if (cl >= TC_H_MIN_PRIORITY) {
538                 int i;
539                 __u32 qlen = 0;
540                 struct gnet_stats_queue qstats = {0};
541                 struct gnet_stats_basic_packed bstats = {0};
542                 struct net_device *dev = qdisc_dev(sch);
543                 struct netdev_tc_txq tc = dev->tc_to_txq[cl & TC_BITMASK];
544
545                 /* Drop lock here it will be reclaimed before touching
546                  * statistics this is required because the d->lock we
547                  * hold here is the look on dev_queue->qdisc_sleeping
548                  * also acquired below.
549                  */
550                 if (d->lock)
551                         spin_unlock_bh(d->lock);
552
553                 for (i = tc.offset; i < tc.offset + tc.count; i++) {
554                         struct netdev_queue *q = netdev_get_tx_queue(dev, i);
555                         struct Qdisc *qdisc = rtnl_dereference(q->qdisc);
556
557                         spin_lock_bh(qdisc_lock(qdisc));
558
559                         if (qdisc_is_percpu_stats(qdisc)) {
560                                 qlen = qdisc_qlen_sum(qdisc);
561
562                                 __gnet_stats_copy_basic(NULL, &bstats,
563                                                         qdisc->cpu_bstats,
564                                                         &qdisc->bstats);
565                                 __gnet_stats_copy_queue(&qstats,
566                                                         qdisc->cpu_qstats,
567                                                         &qdisc->qstats,
568                                                         qlen);
569                         } else {
570                                 qlen            += qdisc->q.qlen;
571                                 bstats.bytes    += qdisc->bstats.bytes;
572                                 bstats.packets  += qdisc->bstats.packets;
573                                 qstats.backlog  += qdisc->qstats.backlog;
574                                 qstats.drops    += qdisc->qstats.drops;
575                                 qstats.requeues += qdisc->qstats.requeues;
576                                 qstats.overlimits += qdisc->qstats.overlimits;
577                         }
578                         spin_unlock_bh(qdisc_lock(qdisc));
579                 }
580
581                 /* Reclaim root sleeping lock before completing stats */
582                 if (d->lock)
583                         spin_lock_bh(d->lock);
584                 if (gnet_stats_copy_basic(NULL, d, NULL, &bstats) < 0 ||
585                     gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
586                         return -1;
587         } else {
588                 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
589
590                 sch = dev_queue->qdisc_sleeping;
591                 if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch), d,
592                                           sch->cpu_bstats, &sch->bstats) < 0 ||
593                     gnet_stats_copy_queue(d, NULL,
594                                           &sch->qstats, sch->q.qlen) < 0)
595                         return -1;
596         }
597         return 0;
598 }
599
600 static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
601 {
602         struct net_device *dev = qdisc_dev(sch);
603         unsigned long ntx;
604
605         if (arg->stop)
606                 return;
607
608         /* Walk hierarchy with a virtual class per tc */
609         arg->count = arg->skip;
610         for (ntx = arg->skip; ntx < netdev_get_num_tc(dev); ntx++) {
611                 if (arg->fn(sch, ntx + TC_H_MIN_PRIORITY, arg) < 0) {
612                         arg->stop = 1;
613                         return;
614                 }
615                 arg->count++;
616         }
617
618         /* Pad the values and skip over unused traffic classes */
619         if (ntx < TC_MAX_QUEUE) {
620                 arg->count = TC_MAX_QUEUE;
621                 ntx = TC_MAX_QUEUE;
622         }
623
624         /* Reset offset, sort out remaining per-queue qdiscs */
625         for (ntx -= TC_MAX_QUEUE; ntx < dev->num_tx_queues; ntx++) {
626                 if (arg->fn(sch, ntx + 1, arg) < 0) {
627                         arg->stop = 1;
628                         return;
629                 }
630                 arg->count++;
631         }
632 }
633
634 static struct netdev_queue *mqprio_select_queue(struct Qdisc *sch,
635                                                 struct tcmsg *tcm)
636 {
637         return mqprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
638 }
639
640 static const struct Qdisc_class_ops mqprio_class_ops = {
641         .graft          = mqprio_graft,
642         .leaf           = mqprio_leaf,
643         .find           = mqprio_find,
644         .walk           = mqprio_walk,
645         .dump           = mqprio_dump_class,
646         .dump_stats     = mqprio_dump_class_stats,
647         .select_queue   = mqprio_select_queue,
648 };
649
650 static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
651         .cl_ops         = &mqprio_class_ops,
652         .id             = "mqprio",
653         .priv_size      = sizeof(struct mqprio_sched),
654         .init           = mqprio_init,
655         .destroy        = mqprio_destroy,
656         .attach         = mqprio_attach,
657         .change_real_num_tx = mqprio_change_real_num_tx,
658         .dump           = mqprio_dump,
659         .owner          = THIS_MODULE,
660 };
661
662 static int __init mqprio_module_init(void)
663 {
664         return register_qdisc(&mqprio_qdisc_ops);
665 }
666
667 static void __exit mqprio_module_exit(void)
668 {
669         unregister_qdisc(&mqprio_qdisc_ops);
670 }
671
672 module_init(mqprio_module_init);
673 module_exit(mqprio_module_exit);
674
675 MODULE_LICENSE("GPL");