blob: 4d5ed45123f033d865f56f0217dde763fff136bb [file] [log] [blame]
John Fastabendb8970f02011-01-17 08:06:09 +00001/*
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>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040017#include <linux/module.h>
John Fastabendb8970f02011-01-17 08:06:09 +000018#include <net/netlink.h>
19#include <net/pkt_sched.h>
20#include <net/sch_generic.h>
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -070021#include <net/pkt_cls.h>
John Fastabendb8970f02011-01-17 08:06:09 +000022
23struct mqprio_sched {
24 struct Qdisc **qdiscs;
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -070025 u16 mode;
26 u16 shaper;
Alexander Duyck2026fec2017-03-15 10:39:18 -070027 int hw_offload;
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -070028 u32 flags;
29 u64 min_rate[TC_QOPT_MAX_QUEUE];
30 u64 max_rate[TC_QOPT_MAX_QUEUE];
John Fastabendb8970f02011-01-17 08:06:09 +000031};
32
33static 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
Ben Hutchingsac7100b2011-02-14 19:02:23 +000039 if (priv->qdiscs) {
40 for (ntx = 0;
41 ntx < dev->num_tx_queues && priv->qdiscs[ntx];
42 ntx++)
43 qdisc_destroy(priv->qdiscs[ntx]);
44 kfree(priv->qdiscs);
45 }
John Fastabendb8970f02011-01-17 08:06:09 +000046
Amritha Nambiar56f36ac2017-03-15 10:39:25 -070047 if (priv->hw_offload && dev->netdev_ops->ndo_setup_tc) {
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -070048 struct tc_mqprio_qopt_offload mqprio = { { 0 } };
Amritha Nambiar56f36ac2017-03-15 10:39:25 -070049
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -070050 switch (priv->mode) {
51 case TC_MQPRIO_MODE_DCB:
52 case TC_MQPRIO_MODE_CHANNEL:
53 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_MQPRIO,
54 &mqprio);
55 break;
56 default:
57 return;
58 }
Amritha Nambiar56f36ac2017-03-15 10:39:25 -070059 } else {
John Fastabendb8970f02011-01-17 08:06:09 +000060 netdev_set_num_tc(dev, 0);
Amritha Nambiar56f36ac2017-03-15 10:39:25 -070061 }
John Fastabendb8970f02011-01-17 08:06:09 +000062}
63
64static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt)
65{
66 int i, j;
67
68 /* Verify num_tc is not out of max range */
69 if (qopt->num_tc > TC_MAX_QUEUE)
70 return -EINVAL;
71
72 /* Verify priority mapping uses valid tcs */
73 for (i = 0; i < TC_BITMASK + 1; i++) {
74 if (qopt->prio_tc_map[i] >= qopt->num_tc)
75 return -EINVAL;
76 }
77
Alexander Duyck2026fec2017-03-15 10:39:18 -070078 /* Limit qopt->hw to maximum supported offload value. Drivers have
79 * the option of overriding this later if they don't support the a
80 * given offload type.
81 */
82 if (qopt->hw > TC_MQPRIO_HW_OFFLOAD_MAX)
83 qopt->hw = TC_MQPRIO_HW_OFFLOAD_MAX;
John Fastabendb8970f02011-01-17 08:06:09 +000084
Alexander Duyck2026fec2017-03-15 10:39:18 -070085 /* If hardware offload is requested we will leave it to the device
86 * to either populate the queue counts itself or to validate the
87 * provided queue counts. If ndo_setup_tc is not present then
88 * hardware doesn't support offload and we should return an error.
John Fastabendb8970f02011-01-17 08:06:09 +000089 */
90 if (qopt->hw)
Alexander Duyck2026fec2017-03-15 10:39:18 -070091 return dev->netdev_ops->ndo_setup_tc ? 0 : -EINVAL;
John Fastabendb8970f02011-01-17 08:06:09 +000092
93 for (i = 0; i < qopt->num_tc; i++) {
94 unsigned int last = qopt->offset[i] + qopt->count[i];
95
96 /* Verify the queue count is in tx range being equal to the
97 * real_num_tx_queues indicates the last queue is in use.
98 */
99 if (qopt->offset[i] >= dev->real_num_tx_queues ||
100 !qopt->count[i] ||
101 last > dev->real_num_tx_queues)
102 return -EINVAL;
103
104 /* Verify that the offset and counts do not overlap */
105 for (j = i + 1; j < qopt->num_tc; j++) {
106 if (last > qopt->offset[j])
107 return -EINVAL;
108 }
109 }
110
111 return 0;
112}
113
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -0700114static const struct nla_policy mqprio_policy[TCA_MQPRIO_MAX + 1] = {
115 [TCA_MQPRIO_MODE] = { .len = sizeof(u16) },
116 [TCA_MQPRIO_SHAPER] = { .len = sizeof(u16) },
117 [TCA_MQPRIO_MIN_RATE64] = { .type = NLA_NESTED },
118 [TCA_MQPRIO_MAX_RATE64] = { .type = NLA_NESTED },
119};
120
121static int parse_attr(struct nlattr *tb[], int maxtype, struct nlattr *nla,
122 const struct nla_policy *policy, int len)
123{
124 int nested_len = nla_len(nla) - NLA_ALIGN(len);
125
126 if (nested_len >= nla_attr_size(0))
127 return nla_parse(tb, maxtype, nla_data(nla) + NLA_ALIGN(len),
128 nested_len, policy, NULL);
129
130 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
131 return 0;
132}
133
John Fastabendb8970f02011-01-17 08:06:09 +0000134static int mqprio_init(struct Qdisc *sch, struct nlattr *opt)
135{
136 struct net_device *dev = qdisc_dev(sch);
137 struct mqprio_sched *priv = qdisc_priv(sch);
138 struct netdev_queue *dev_queue;
139 struct Qdisc *qdisc;
140 int i, err = -EOPNOTSUPP;
141 struct tc_mqprio_qopt *qopt = NULL;
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -0700142 struct nlattr *tb[TCA_MQPRIO_MAX + 1];
143 struct nlattr *attr;
144 int rem;
Colin Ian King22ce97f2017-10-17 16:01:30 +0100145 int len;
John Fastabendb8970f02011-01-17 08:06:09 +0000146
147 BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE);
148 BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK);
149
150 if (sch->parent != TC_H_ROOT)
151 return -EOPNOTSUPP;
152
153 if (!netif_is_multiqueue(dev))
154 return -EOPNOTSUPP;
155
Alexander Duyck32302902017-10-12 11:38:45 -0700156 /* make certain can allocate enough classids to handle queues */
157 if (dev->num_tx_queues >= TC_H_MIN_PRIORITY)
158 return -ENOMEM;
159
Thomas Graf7838f2c2011-12-22 02:05:07 +0000160 if (!opt || nla_len(opt) < sizeof(*qopt))
John Fastabendb8970f02011-01-17 08:06:09 +0000161 return -EINVAL;
162
163 qopt = nla_data(opt);
164 if (mqprio_parse_opt(dev, qopt))
165 return -EINVAL;
166
Colin Ian King22ce97f2017-10-17 16:01:30 +0100167 len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt));
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -0700168 if (len > 0) {
169 err = parse_attr(tb, TCA_MQPRIO_MAX, opt, mqprio_policy,
170 sizeof(*qopt));
171 if (err < 0)
172 return err;
173
174 if (!qopt->hw)
175 return -EINVAL;
176
177 if (tb[TCA_MQPRIO_MODE]) {
178 priv->flags |= TC_MQPRIO_F_MODE;
179 priv->mode = *(u16 *)nla_data(tb[TCA_MQPRIO_MODE]);
180 }
181
182 if (tb[TCA_MQPRIO_SHAPER]) {
183 priv->flags |= TC_MQPRIO_F_SHAPER;
184 priv->shaper = *(u16 *)nla_data(tb[TCA_MQPRIO_SHAPER]);
185 }
186
187 if (tb[TCA_MQPRIO_MIN_RATE64]) {
188 if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE)
189 return -EINVAL;
190 i = 0;
191 nla_for_each_nested(attr, tb[TCA_MQPRIO_MIN_RATE64],
192 rem) {
193 if (nla_type(attr) != TCA_MQPRIO_MIN_RATE64)
194 return -EINVAL;
195 if (i >= qopt->num_tc)
196 break;
197 priv->min_rate[i] = *(u64 *)nla_data(attr);
198 i++;
199 }
200 priv->flags |= TC_MQPRIO_F_MIN_RATE;
201 }
202
203 if (tb[TCA_MQPRIO_MAX_RATE64]) {
204 if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE)
205 return -EINVAL;
206 i = 0;
207 nla_for_each_nested(attr, tb[TCA_MQPRIO_MAX_RATE64],
208 rem) {
209 if (nla_type(attr) != TCA_MQPRIO_MAX_RATE64)
210 return -EINVAL;
211 if (i >= qopt->num_tc)
212 break;
213 priv->max_rate[i] = *(u64 *)nla_data(attr);
214 i++;
215 }
216 priv->flags |= TC_MQPRIO_F_MAX_RATE;
217 }
218 }
219
John Fastabendb8970f02011-01-17 08:06:09 +0000220 /* pre-allocate qdisc, attachment can't fail */
221 priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
222 GFP_KERNEL);
Eric Dumazet87b60cf2017-02-10 10:31:49 -0800223 if (!priv->qdiscs)
224 return -ENOMEM;
John Fastabendb8970f02011-01-17 08:06:09 +0000225
226 for (i = 0; i < dev->num_tx_queues; i++) {
227 dev_queue = netdev_get_tx_queue(dev, i);
Eric Dumazet1f27cde2016-03-02 08:21:43 -0800228 qdisc = qdisc_create_dflt(dev_queue,
229 get_default_qdisc_ops(dev, i),
John Fastabendb8970f02011-01-17 08:06:09 +0000230 TC_H_MAKE(TC_H_MAJ(sch->handle),
231 TC_H_MIN(i + 1)));
Eric Dumazet87b60cf2017-02-10 10:31:49 -0800232 if (!qdisc)
233 return -ENOMEM;
234
John Fastabendb8970f02011-01-17 08:06:09 +0000235 priv->qdiscs[i] = qdisc;
Eric Dumazet4eaf3b82015-12-01 20:08:51 -0800236 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
John Fastabendb8970f02011-01-17 08:06:09 +0000237 }
238
239 /* If the mqprio options indicate that hardware should own
240 * the queue mapping then run ndo_setup_tc otherwise use the
241 * supplied and verified mapping
242 */
243 if (qopt->hw) {
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -0700244 struct tc_mqprio_qopt_offload mqprio = {.qopt = *qopt};
John Fastabend16e5cc62016-02-16 21:16:43 -0800245
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -0700246 switch (priv->mode) {
247 case TC_MQPRIO_MODE_DCB:
248 if (priv->shaper != TC_MQPRIO_SHAPER_DCB)
249 return -EINVAL;
250 break;
251 case TC_MQPRIO_MODE_CHANNEL:
252 mqprio.flags = priv->flags;
253 if (priv->flags & TC_MQPRIO_F_MODE)
254 mqprio.mode = priv->mode;
255 if (priv->flags & TC_MQPRIO_F_SHAPER)
256 mqprio.shaper = priv->shaper;
257 if (priv->flags & TC_MQPRIO_F_MIN_RATE)
258 for (i = 0; i < mqprio.qopt.num_tc; i++)
259 mqprio.min_rate[i] = priv->min_rate[i];
260 if (priv->flags & TC_MQPRIO_F_MAX_RATE)
261 for (i = 0; i < mqprio.qopt.num_tc; i++)
262 mqprio.max_rate[i] = priv->max_rate[i];
263 break;
264 default:
265 return -EINVAL;
266 }
267 err = dev->netdev_ops->ndo_setup_tc(dev,
268 TC_SETUP_MQPRIO,
Jiri Pirkode4784c2017-08-07 10:15:32 +0200269 &mqprio);
John Fastabendb8970f02011-01-17 08:06:09 +0000270 if (err)
Eric Dumazet87b60cf2017-02-10 10:31:49 -0800271 return err;
Alexander Duyck2026fec2017-03-15 10:39:18 -0700272
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -0700273 priv->hw_offload = mqprio.qopt.hw;
John Fastabendb8970f02011-01-17 08:06:09 +0000274 } else {
275 netdev_set_num_tc(dev, qopt->num_tc);
276 for (i = 0; i < qopt->num_tc; i++)
277 netdev_set_tc_queue(dev, i,
278 qopt->count[i], qopt->offset[i]);
279 }
280
281 /* Always use supplied priority mappings */
282 for (i = 0; i < TC_BITMASK + 1; i++)
283 netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]);
284
285 sch->flags |= TCQ_F_MQROOT;
286 return 0;
John Fastabendb8970f02011-01-17 08:06:09 +0000287}
288
289static void mqprio_attach(struct Qdisc *sch)
290{
291 struct net_device *dev = qdisc_dev(sch);
292 struct mqprio_sched *priv = qdisc_priv(sch);
Eric Dumazet95dc1922013-12-05 11:12:02 -0800293 struct Qdisc *qdisc, *old;
John Fastabendb8970f02011-01-17 08:06:09 +0000294 unsigned int ntx;
295
296 /* Attach underlying qdisc */
297 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
298 qdisc = priv->qdiscs[ntx];
Eric Dumazet95dc1922013-12-05 11:12:02 -0800299 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
300 if (old)
301 qdisc_destroy(old);
302 if (ntx < dev->real_num_tx_queues)
Jiri Kosina49b49972017-03-08 16:03:32 +0100303 qdisc_hash_add(qdisc, false);
John Fastabendb8970f02011-01-17 08:06:09 +0000304 }
305 kfree(priv->qdiscs);
306 priv->qdiscs = NULL;
307}
308
309static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch,
310 unsigned long cl)
311{
312 struct net_device *dev = qdisc_dev(sch);
Alexander Duyck32302902017-10-12 11:38:45 -0700313 unsigned long ntx = cl - 1;
John Fastabendb8970f02011-01-17 08:06:09 +0000314
315 if (ntx >= dev->num_tx_queues)
316 return NULL;
317 return netdev_get_tx_queue(dev, ntx);
318}
319
320static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
321 struct Qdisc **old)
322{
323 struct net_device *dev = qdisc_dev(sch);
324 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
325
326 if (!dev_queue)
327 return -EINVAL;
328
329 if (dev->flags & IFF_UP)
330 dev_deactivate(dev);
331
332 *old = dev_graft_qdisc(dev_queue, new);
333
Eric Dumazet1abbe132012-12-11 15:54:33 +0000334 if (new)
Eric Dumazet4eaf3b82015-12-01 20:08:51 -0800335 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
Eric Dumazet1abbe132012-12-11 15:54:33 +0000336
John Fastabendb8970f02011-01-17 08:06:09 +0000337 if (dev->flags & IFF_UP)
338 dev_activate(dev);
339
340 return 0;
341}
342
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -0700343static int dump_rates(struct mqprio_sched *priv,
344 struct tc_mqprio_qopt *opt, struct sk_buff *skb)
345{
346 struct nlattr *nest;
347 int i;
348
349 if (priv->flags & TC_MQPRIO_F_MIN_RATE) {
350 nest = nla_nest_start(skb, TCA_MQPRIO_MIN_RATE64);
351 if (!nest)
352 goto nla_put_failure;
353
354 for (i = 0; i < opt->num_tc; i++) {
355 if (nla_put(skb, TCA_MQPRIO_MIN_RATE64,
356 sizeof(priv->min_rate[i]),
357 &priv->min_rate[i]))
358 goto nla_put_failure;
359 }
360 nla_nest_end(skb, nest);
361 }
362
363 if (priv->flags & TC_MQPRIO_F_MAX_RATE) {
364 nest = nla_nest_start(skb, TCA_MQPRIO_MAX_RATE64);
365 if (!nest)
366 goto nla_put_failure;
367
368 for (i = 0; i < opt->num_tc; i++) {
369 if (nla_put(skb, TCA_MQPRIO_MAX_RATE64,
370 sizeof(priv->max_rate[i]),
371 &priv->max_rate[i]))
372 goto nla_put_failure;
373 }
374 nla_nest_end(skb, nest);
375 }
376 return 0;
377
378nla_put_failure:
379 nla_nest_cancel(skb, nest);
380 return -1;
381}
382
John Fastabendb8970f02011-01-17 08:06:09 +0000383static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb)
384{
385 struct net_device *dev = qdisc_dev(sch);
386 struct mqprio_sched *priv = qdisc_priv(sch);
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -0700387 struct nlattr *nla = (struct nlattr *)skb_tail_pointer(skb);
Eric Dumazet144ce872011-01-26 07:21:57 +0000388 struct tc_mqprio_qopt opt = { 0 };
John Fastabendb8970f02011-01-17 08:06:09 +0000389 struct Qdisc *qdisc;
390 unsigned int i;
391
392 sch->q.qlen = 0;
393 memset(&sch->bstats, 0, sizeof(sch->bstats));
394 memset(&sch->qstats, 0, sizeof(sch->qstats));
395
396 for (i = 0; i < dev->num_tx_queues; i++) {
John Fastabend46e5da40a2014-09-12 20:04:52 -0700397 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
John Fastabendb8970f02011-01-17 08:06:09 +0000398 spin_lock_bh(qdisc_lock(qdisc));
399 sch->q.qlen += qdisc->q.qlen;
400 sch->bstats.bytes += qdisc->bstats.bytes;
401 sch->bstats.packets += qdisc->bstats.packets;
John Fastabendb8970f02011-01-17 08:06:09 +0000402 sch->qstats.backlog += qdisc->qstats.backlog;
403 sch->qstats.drops += qdisc->qstats.drops;
404 sch->qstats.requeues += qdisc->qstats.requeues;
405 sch->qstats.overlimits += qdisc->qstats.overlimits;
406 spin_unlock_bh(qdisc_lock(qdisc));
407 }
408
409 opt.num_tc = netdev_get_num_tc(dev);
410 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
Alexander Duyck2026fec2017-03-15 10:39:18 -0700411 opt.hw = priv->hw_offload;
John Fastabendb8970f02011-01-17 08:06:09 +0000412
413 for (i = 0; i < netdev_get_num_tc(dev); i++) {
414 opt.count[i] = dev->tc_to_txq[i].count;
415 opt.offset[i] = dev->tc_to_txq[i].offset;
416 }
417
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -0700418 if (nla_put(skb, TCA_OPTIONS, NLA_ALIGN(sizeof(opt)), &opt))
David S. Miller1b34ec42012-03-29 05:11:39 -0400419 goto nla_put_failure;
John Fastabendb8970f02011-01-17 08:06:09 +0000420
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -0700421 if ((priv->flags & TC_MQPRIO_F_MODE) &&
422 nla_put_u16(skb, TCA_MQPRIO_MODE, priv->mode))
423 goto nla_put_failure;
424
425 if ((priv->flags & TC_MQPRIO_F_SHAPER) &&
426 nla_put_u16(skb, TCA_MQPRIO_SHAPER, priv->shaper))
427 goto nla_put_failure;
428
429 if ((priv->flags & TC_MQPRIO_F_MIN_RATE ||
430 priv->flags & TC_MQPRIO_F_MAX_RATE) &&
431 (dump_rates(priv, &opt, skb) != 0))
432 goto nla_put_failure;
433
434 return nla_nest_end(skb, nla);
John Fastabendb8970f02011-01-17 08:06:09 +0000435nla_put_failure:
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -0700436 nlmsg_trim(skb, nla);
John Fastabendb8970f02011-01-17 08:06:09 +0000437 return -1;
438}
439
440static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl)
441{
442 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
443
444 if (!dev_queue)
445 return NULL;
446
447 return dev_queue->qdisc_sleeping;
448}
449
WANG Cong143976c2017-08-24 16:51:29 -0700450static unsigned long mqprio_find(struct Qdisc *sch, u32 classid)
John Fastabendb8970f02011-01-17 08:06:09 +0000451{
452 struct net_device *dev = qdisc_dev(sch);
453 unsigned int ntx = TC_H_MIN(classid);
454
Alexander Duyck32302902017-10-12 11:38:45 -0700455 /* There are essentially two regions here that have valid classid
456 * values. The first region will have a classid value of 1 through
457 * num_tx_queues. All of these are backed by actual Qdiscs.
458 */
459 if (ntx < TC_H_MIN_PRIORITY)
460 return (ntx <= dev->num_tx_queues) ? ntx : 0;
461
462 /* The second region represents the hardware traffic classes. These
463 * are represented by classid values of TC_H_MIN_PRIORITY through
464 * TC_H_MIN_PRIORITY + netdev_get_num_tc - 1
465 */
466 return ((ntx - TC_H_MIN_PRIORITY) < netdev_get_num_tc(dev)) ? ntx : 0;
John Fastabendb8970f02011-01-17 08:06:09 +0000467}
468
John Fastabendb8970f02011-01-17 08:06:09 +0000469static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl,
470 struct sk_buff *skb, struct tcmsg *tcm)
471{
Alexander Duyck32302902017-10-12 11:38:45 -0700472 if (cl < TC_H_MIN_PRIORITY) {
473 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
474 struct net_device *dev = qdisc_dev(sch);
475 int tc = netdev_txq_to_tc(dev, cl - 1);
John Fastabendb8970f02011-01-17 08:06:09 +0000476
Alexander Duyck32302902017-10-12 11:38:45 -0700477 tcm->tcm_parent = (tc < 0) ? 0 :
478 TC_H_MAKE(TC_H_MAJ(sch->handle),
479 TC_H_MIN(tc + TC_H_MIN_PRIORITY));
480 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
481 } else {
John Fastabendb8970f02011-01-17 08:06:09 +0000482 tcm->tcm_parent = TC_H_ROOT;
483 tcm->tcm_info = 0;
John Fastabendb8970f02011-01-17 08:06:09 +0000484 }
485 tcm->tcm_handle |= TC_H_MIN(cl);
486 return 0;
487}
488
489static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
stephen hemmingerea18fd92011-02-23 09:06:51 +0000490 struct gnet_dump *d)
491 __releases(d->lock)
492 __acquires(d->lock)
John Fastabendb8970f02011-01-17 08:06:09 +0000493{
Alexander Duyck32302902017-10-12 11:38:45 -0700494 if (cl >= TC_H_MIN_PRIORITY) {
John Fastabendb8970f02011-01-17 08:06:09 +0000495 int i;
John Fastabend64015852014-09-28 11:53:57 -0700496 __u32 qlen = 0;
John Fastabendb8970f02011-01-17 08:06:09 +0000497 struct Qdisc *qdisc;
498 struct gnet_stats_queue qstats = {0};
499 struct gnet_stats_basic_packed bstats = {0};
Alexander Duyck32302902017-10-12 11:38:45 -0700500 struct net_device *dev = qdisc_dev(sch);
501 struct netdev_tc_txq tc = dev->tc_to_txq[cl & TC_BITMASK];
John Fastabendb8970f02011-01-17 08:06:09 +0000502
503 /* Drop lock here it will be reclaimed before touching
504 * statistics this is required because the d->lock we
505 * hold here is the look on dev_queue->qdisc_sleeping
506 * also acquired below.
507 */
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700508 if (d->lock)
509 spin_unlock_bh(d->lock);
John Fastabendb8970f02011-01-17 08:06:09 +0000510
511 for (i = tc.offset; i < tc.offset + tc.count; i++) {
John Fastabend46e5da40a2014-09-12 20:04:52 -0700512 struct netdev_queue *q = netdev_get_tx_queue(dev, i);
513
514 qdisc = rtnl_dereference(q->qdisc);
John Fastabendb8970f02011-01-17 08:06:09 +0000515 spin_lock_bh(qdisc_lock(qdisc));
John Fastabend64015852014-09-28 11:53:57 -0700516 qlen += qdisc->q.qlen;
John Fastabendb8970f02011-01-17 08:06:09 +0000517 bstats.bytes += qdisc->bstats.bytes;
518 bstats.packets += qdisc->bstats.packets;
John Fastabendb8970f02011-01-17 08:06:09 +0000519 qstats.backlog += qdisc->qstats.backlog;
520 qstats.drops += qdisc->qstats.drops;
521 qstats.requeues += qdisc->qstats.requeues;
522 qstats.overlimits += qdisc->qstats.overlimits;
523 spin_unlock_bh(qdisc_lock(qdisc));
524 }
525 /* Reclaim root sleeping lock before completing stats */
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700526 if (d->lock)
527 spin_lock_bh(d->lock);
528 if (gnet_stats_copy_basic(NULL, d, NULL, &bstats) < 0 ||
John Fastabendb0ab6f92014-09-28 11:54:24 -0700529 gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
John Fastabendb8970f02011-01-17 08:06:09 +0000530 return -1;
531 } else {
532 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
533
534 sch = dev_queue->qdisc_sleeping;
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700535 if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
536 d, NULL, &sch->bstats) < 0 ||
John Fastabendb0ab6f92014-09-28 11:54:24 -0700537 gnet_stats_copy_queue(d, NULL,
538 &sch->qstats, sch->q.qlen) < 0)
John Fastabendb8970f02011-01-17 08:06:09 +0000539 return -1;
540 }
541 return 0;
542}
543
544static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
545{
546 struct net_device *dev = qdisc_dev(sch);
547 unsigned long ntx;
548
549 if (arg->stop)
550 return;
551
552 /* Walk hierarchy with a virtual class per tc */
553 arg->count = arg->skip;
Alexander Duyck32302902017-10-12 11:38:45 -0700554 for (ntx = arg->skip; ntx < netdev_get_num_tc(dev); ntx++) {
555 if (arg->fn(sch, ntx + TC_H_MIN_PRIORITY, arg) < 0) {
556 arg->stop = 1;
557 return;
558 }
559 arg->count++;
560 }
561
562 /* Pad the values and skip over unused traffic classes */
563 if (ntx < TC_MAX_QUEUE) {
564 arg->count = TC_MAX_QUEUE;
565 ntx = TC_MAX_QUEUE;
566 }
567
568 /* Reset offset, sort out remaining per-queue qdiscs */
569 for (ntx -= TC_MAX_QUEUE; ntx < dev->num_tx_queues; ntx++) {
John Fastabendb8970f02011-01-17 08:06:09 +0000570 if (arg->fn(sch, ntx + 1, arg) < 0) {
571 arg->stop = 1;
Alexander Duyck32302902017-10-12 11:38:45 -0700572 return;
John Fastabendb8970f02011-01-17 08:06:09 +0000573 }
574 arg->count++;
575 }
576}
577
Jesus Sanchez-Palencia0f7787b2017-10-16 18:01:25 -0700578static struct netdev_queue *mqprio_select_queue(struct Qdisc *sch,
579 struct tcmsg *tcm)
580{
581 return mqprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
582}
583
John Fastabendb8970f02011-01-17 08:06:09 +0000584static const struct Qdisc_class_ops mqprio_class_ops = {
585 .graft = mqprio_graft,
586 .leaf = mqprio_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700587 .find = mqprio_find,
John Fastabendb8970f02011-01-17 08:06:09 +0000588 .walk = mqprio_walk,
589 .dump = mqprio_dump_class,
590 .dump_stats = mqprio_dump_class_stats,
Jesus Sanchez-Palencia0f7787b2017-10-16 18:01:25 -0700591 .select_queue = mqprio_select_queue,
John Fastabendb8970f02011-01-17 08:06:09 +0000592};
593
stephen hemmingerea18fd92011-02-23 09:06:51 +0000594static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
John Fastabendb8970f02011-01-17 08:06:09 +0000595 .cl_ops = &mqprio_class_ops,
596 .id = "mqprio",
597 .priv_size = sizeof(struct mqprio_sched),
598 .init = mqprio_init,
599 .destroy = mqprio_destroy,
600 .attach = mqprio_attach,
601 .dump = mqprio_dump,
602 .owner = THIS_MODULE,
603};
604
605static int __init mqprio_module_init(void)
606{
607 return register_qdisc(&mqprio_qdisc_ops);
608}
609
610static void __exit mqprio_module_exit(void)
611{
612 unregister_qdisc(&mqprio_qdisc_ops);
613}
614
615module_init(mqprio_module_init);
616module_exit(mqprio_module_exit);
617
618MODULE_LICENSE("GPL");