blob: f9947d1f4952096049137df70974ea0a26a8fed9 [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>
21
22struct mqprio_sched {
23 struct Qdisc **qdiscs;
24 int hw_owned;
25};
26
27static void mqprio_destroy(struct Qdisc *sch)
28{
29 struct net_device *dev = qdisc_dev(sch);
30 struct mqprio_sched *priv = qdisc_priv(sch);
John Fastabend16e5cc62016-02-16 21:16:43 -080031 struct tc_to_netdev tc = {.type = TC_SETUP_MQPRIO};
John Fastabendb8970f02011-01-17 08:06:09 +000032 unsigned int ntx;
33
Ben Hutchingsac7100b2011-02-14 19:02:23 +000034 if (priv->qdiscs) {
35 for (ntx = 0;
36 ntx < dev->num_tx_queues && priv->qdiscs[ntx];
37 ntx++)
38 qdisc_destroy(priv->qdiscs[ntx]);
39 kfree(priv->qdiscs);
40 }
John Fastabendb8970f02011-01-17 08:06:09 +000041
42 if (priv->hw_owned && dev->netdev_ops->ndo_setup_tc)
John Fastabend16e5cc62016-02-16 21:16:43 -080043 dev->netdev_ops->ndo_setup_tc(dev, sch->handle, 0, &tc);
John Fastabendb8970f02011-01-17 08:06:09 +000044 else
45 netdev_set_num_tc(dev, 0);
John Fastabendb8970f02011-01-17 08:06:09 +000046}
47
48static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt)
49{
50 int i, j;
51
52 /* Verify num_tc is not out of max range */
53 if (qopt->num_tc > TC_MAX_QUEUE)
54 return -EINVAL;
55
56 /* Verify priority mapping uses valid tcs */
57 for (i = 0; i < TC_BITMASK + 1; i++) {
58 if (qopt->prio_tc_map[i] >= qopt->num_tc)
59 return -EINVAL;
60 }
61
62 /* net_device does not support requested operation */
63 if (qopt->hw && !dev->netdev_ops->ndo_setup_tc)
64 return -EINVAL;
65
66 /* if hw owned qcount and qoffset are taken from LLD so
67 * no reason to verify them here
68 */
69 if (qopt->hw)
70 return 0;
71
72 for (i = 0; i < qopt->num_tc; i++) {
73 unsigned int last = qopt->offset[i] + qopt->count[i];
74
75 /* Verify the queue count is in tx range being equal to the
76 * real_num_tx_queues indicates the last queue is in use.
77 */
78 if (qopt->offset[i] >= dev->real_num_tx_queues ||
79 !qopt->count[i] ||
80 last > dev->real_num_tx_queues)
81 return -EINVAL;
82
83 /* Verify that the offset and counts do not overlap */
84 for (j = i + 1; j < qopt->num_tc; j++) {
85 if (last > qopt->offset[j])
86 return -EINVAL;
87 }
88 }
89
90 return 0;
91}
92
93static int mqprio_init(struct Qdisc *sch, struct nlattr *opt)
94{
95 struct net_device *dev = qdisc_dev(sch);
96 struct mqprio_sched *priv = qdisc_priv(sch);
97 struct netdev_queue *dev_queue;
98 struct Qdisc *qdisc;
99 int i, err = -EOPNOTSUPP;
100 struct tc_mqprio_qopt *qopt = NULL;
101
102 BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE);
103 BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK);
104
105 if (sch->parent != TC_H_ROOT)
106 return -EOPNOTSUPP;
107
108 if (!netif_is_multiqueue(dev))
109 return -EOPNOTSUPP;
110
Thomas Graf7838f2c2011-12-22 02:05:07 +0000111 if (!opt || nla_len(opt) < sizeof(*qopt))
John Fastabendb8970f02011-01-17 08:06:09 +0000112 return -EINVAL;
113
114 qopt = nla_data(opt);
115 if (mqprio_parse_opt(dev, qopt))
116 return -EINVAL;
117
118 /* pre-allocate qdisc, attachment can't fail */
119 priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
120 GFP_KERNEL);
121 if (priv->qdiscs == NULL) {
122 err = -ENOMEM;
123 goto err;
124 }
125
126 for (i = 0; i < dev->num_tx_queues; i++) {
127 dev_queue = netdev_get_tx_queue(dev, i);
stephen hemminger6da7c8f2013-08-27 16:19:08 -0700128 qdisc = qdisc_create_dflt(dev_queue, default_qdisc_ops,
John Fastabendb8970f02011-01-17 08:06:09 +0000129 TC_H_MAKE(TC_H_MAJ(sch->handle),
130 TC_H_MIN(i + 1)));
131 if (qdisc == NULL) {
132 err = -ENOMEM;
133 goto err;
134 }
John Fastabendb8970f02011-01-17 08:06:09 +0000135 priv->qdiscs[i] = qdisc;
Eric Dumazet4eaf3b82015-12-01 20:08:51 -0800136 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
John Fastabendb8970f02011-01-17 08:06:09 +0000137 }
138
139 /* If the mqprio options indicate that hardware should own
140 * the queue mapping then run ndo_setup_tc otherwise use the
141 * supplied and verified mapping
142 */
143 if (qopt->hw) {
John Fastabend16e5cc62016-02-16 21:16:43 -0800144 struct tc_to_netdev tc = {.type = TC_SETUP_MQPRIO,
145 .tc = qopt->num_tc};
146
John Fastabendb8970f02011-01-17 08:06:09 +0000147 priv->hw_owned = 1;
John Fastabend16e5cc62016-02-16 21:16:43 -0800148 err = dev->netdev_ops->ndo_setup_tc(dev, sch->handle, 0, &tc);
John Fastabendb8970f02011-01-17 08:06:09 +0000149 if (err)
150 goto err;
151 } else {
152 netdev_set_num_tc(dev, qopt->num_tc);
153 for (i = 0; i < qopt->num_tc; i++)
154 netdev_set_tc_queue(dev, i,
155 qopt->count[i], qopt->offset[i]);
156 }
157
158 /* Always use supplied priority mappings */
159 for (i = 0; i < TC_BITMASK + 1; i++)
160 netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]);
161
162 sch->flags |= TCQ_F_MQROOT;
163 return 0;
164
165err:
166 mqprio_destroy(sch);
167 return err;
168}
169
170static void mqprio_attach(struct Qdisc *sch)
171{
172 struct net_device *dev = qdisc_dev(sch);
173 struct mqprio_sched *priv = qdisc_priv(sch);
Eric Dumazet95dc1922013-12-05 11:12:02 -0800174 struct Qdisc *qdisc, *old;
John Fastabendb8970f02011-01-17 08:06:09 +0000175 unsigned int ntx;
176
177 /* Attach underlying qdisc */
178 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
179 qdisc = priv->qdiscs[ntx];
Eric Dumazet95dc1922013-12-05 11:12:02 -0800180 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
181 if (old)
182 qdisc_destroy(old);
183 if (ntx < dev->real_num_tx_queues)
184 qdisc_list_add(qdisc);
John Fastabendb8970f02011-01-17 08:06:09 +0000185 }
186 kfree(priv->qdiscs);
187 priv->qdiscs = NULL;
188}
189
190static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch,
191 unsigned long cl)
192{
193 struct net_device *dev = qdisc_dev(sch);
194 unsigned long ntx = cl - 1 - netdev_get_num_tc(dev);
195
196 if (ntx >= dev->num_tx_queues)
197 return NULL;
198 return netdev_get_tx_queue(dev, ntx);
199}
200
201static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
202 struct Qdisc **old)
203{
204 struct net_device *dev = qdisc_dev(sch);
205 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
206
207 if (!dev_queue)
208 return -EINVAL;
209
210 if (dev->flags & IFF_UP)
211 dev_deactivate(dev);
212
213 *old = dev_graft_qdisc(dev_queue, new);
214
Eric Dumazet1abbe132012-12-11 15:54:33 +0000215 if (new)
Eric Dumazet4eaf3b82015-12-01 20:08:51 -0800216 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
Eric Dumazet1abbe132012-12-11 15:54:33 +0000217
John Fastabendb8970f02011-01-17 08:06:09 +0000218 if (dev->flags & IFF_UP)
219 dev_activate(dev);
220
221 return 0;
222}
223
224static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb)
225{
226 struct net_device *dev = qdisc_dev(sch);
227 struct mqprio_sched *priv = qdisc_priv(sch);
228 unsigned char *b = skb_tail_pointer(skb);
Eric Dumazet144ce872011-01-26 07:21:57 +0000229 struct tc_mqprio_qopt opt = { 0 };
John Fastabendb8970f02011-01-17 08:06:09 +0000230 struct Qdisc *qdisc;
231 unsigned int i;
232
233 sch->q.qlen = 0;
234 memset(&sch->bstats, 0, sizeof(sch->bstats));
235 memset(&sch->qstats, 0, sizeof(sch->qstats));
236
237 for (i = 0; i < dev->num_tx_queues; i++) {
John Fastabend46e5da42014-09-12 20:04:52 -0700238 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
John Fastabendb8970f02011-01-17 08:06:09 +0000239 spin_lock_bh(qdisc_lock(qdisc));
240 sch->q.qlen += qdisc->q.qlen;
241 sch->bstats.bytes += qdisc->bstats.bytes;
242 sch->bstats.packets += qdisc->bstats.packets;
John Fastabendb8970f02011-01-17 08:06:09 +0000243 sch->qstats.backlog += qdisc->qstats.backlog;
244 sch->qstats.drops += qdisc->qstats.drops;
245 sch->qstats.requeues += qdisc->qstats.requeues;
246 sch->qstats.overlimits += qdisc->qstats.overlimits;
247 spin_unlock_bh(qdisc_lock(qdisc));
248 }
249
250 opt.num_tc = netdev_get_num_tc(dev);
251 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
252 opt.hw = priv->hw_owned;
253
254 for (i = 0; i < netdev_get_num_tc(dev); i++) {
255 opt.count[i] = dev->tc_to_txq[i].count;
256 opt.offset[i] = dev->tc_to_txq[i].offset;
257 }
258
David S. Miller1b34ec42012-03-29 05:11:39 -0400259 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
260 goto nla_put_failure;
John Fastabendb8970f02011-01-17 08:06:09 +0000261
262 return skb->len;
263nla_put_failure:
264 nlmsg_trim(skb, b);
265 return -1;
266}
267
268static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl)
269{
270 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
271
272 if (!dev_queue)
273 return NULL;
274
275 return dev_queue->qdisc_sleeping;
276}
277
278static unsigned long mqprio_get(struct Qdisc *sch, u32 classid)
279{
280 struct net_device *dev = qdisc_dev(sch);
281 unsigned int ntx = TC_H_MIN(classid);
282
283 if (ntx > dev->num_tx_queues + netdev_get_num_tc(dev))
284 return 0;
285 return ntx;
286}
287
288static void mqprio_put(struct Qdisc *sch, unsigned long cl)
289{
290}
291
292static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl,
293 struct sk_buff *skb, struct tcmsg *tcm)
294{
295 struct net_device *dev = qdisc_dev(sch);
296
297 if (cl <= netdev_get_num_tc(dev)) {
298 tcm->tcm_parent = TC_H_ROOT;
299 tcm->tcm_info = 0;
300 } else {
301 int i;
302 struct netdev_queue *dev_queue;
303
304 dev_queue = mqprio_queue_get(sch, cl);
305 tcm->tcm_parent = 0;
306 for (i = 0; i < netdev_get_num_tc(dev); i++) {
307 struct netdev_tc_txq tc = dev->tc_to_txq[i];
308 int q_idx = cl - netdev_get_num_tc(dev);
309
310 if (q_idx > tc.offset &&
311 q_idx <= tc.offset + tc.count) {
312 tcm->tcm_parent =
313 TC_H_MAKE(TC_H_MAJ(sch->handle),
314 TC_H_MIN(i + 1));
315 break;
316 }
317 }
318 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
319 }
320 tcm->tcm_handle |= TC_H_MIN(cl);
321 return 0;
322}
323
324static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
stephen hemmingerea18fd92011-02-23 09:06:51 +0000325 struct gnet_dump *d)
326 __releases(d->lock)
327 __acquires(d->lock)
John Fastabendb8970f02011-01-17 08:06:09 +0000328{
329 struct net_device *dev = qdisc_dev(sch);
330
331 if (cl <= netdev_get_num_tc(dev)) {
332 int i;
John Fastabend64015852014-09-28 11:53:57 -0700333 __u32 qlen = 0;
John Fastabendb8970f02011-01-17 08:06:09 +0000334 struct Qdisc *qdisc;
335 struct gnet_stats_queue qstats = {0};
336 struct gnet_stats_basic_packed bstats = {0};
337 struct netdev_tc_txq tc = dev->tc_to_txq[cl - 1];
338
339 /* Drop lock here it will be reclaimed before touching
340 * statistics this is required because the d->lock we
341 * hold here is the look on dev_queue->qdisc_sleeping
342 * also acquired below.
343 */
344 spin_unlock_bh(d->lock);
345
346 for (i = tc.offset; i < tc.offset + tc.count; i++) {
John Fastabend46e5da42014-09-12 20:04:52 -0700347 struct netdev_queue *q = netdev_get_tx_queue(dev, i);
348
349 qdisc = rtnl_dereference(q->qdisc);
John Fastabendb8970f02011-01-17 08:06:09 +0000350 spin_lock_bh(qdisc_lock(qdisc));
John Fastabend64015852014-09-28 11:53:57 -0700351 qlen += qdisc->q.qlen;
John Fastabendb8970f02011-01-17 08:06:09 +0000352 bstats.bytes += qdisc->bstats.bytes;
353 bstats.packets += qdisc->bstats.packets;
John Fastabendb8970f02011-01-17 08:06:09 +0000354 qstats.backlog += qdisc->qstats.backlog;
355 qstats.drops += qdisc->qstats.drops;
356 qstats.requeues += qdisc->qstats.requeues;
357 qstats.overlimits += qdisc->qstats.overlimits;
358 spin_unlock_bh(qdisc_lock(qdisc));
359 }
360 /* Reclaim root sleeping lock before completing stats */
361 spin_lock_bh(d->lock);
John Fastabend22e0f8b2014-09-28 11:52:56 -0700362 if (gnet_stats_copy_basic(d, NULL, &bstats) < 0 ||
John Fastabendb0ab6f92014-09-28 11:54:24 -0700363 gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
John Fastabendb8970f02011-01-17 08:06:09 +0000364 return -1;
365 } else {
366 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
367
368 sch = dev_queue->qdisc_sleeping;
John Fastabend22e0f8b2014-09-28 11:52:56 -0700369 if (gnet_stats_copy_basic(d, NULL, &sch->bstats) < 0 ||
John Fastabendb0ab6f92014-09-28 11:54:24 -0700370 gnet_stats_copy_queue(d, NULL,
371 &sch->qstats, sch->q.qlen) < 0)
John Fastabendb8970f02011-01-17 08:06:09 +0000372 return -1;
373 }
374 return 0;
375}
376
377static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
378{
379 struct net_device *dev = qdisc_dev(sch);
380 unsigned long ntx;
381
382 if (arg->stop)
383 return;
384
385 /* Walk hierarchy with a virtual class per tc */
386 arg->count = arg->skip;
387 for (ntx = arg->skip;
388 ntx < dev->num_tx_queues + netdev_get_num_tc(dev);
389 ntx++) {
390 if (arg->fn(sch, ntx + 1, arg) < 0) {
391 arg->stop = 1;
392 break;
393 }
394 arg->count++;
395 }
396}
397
398static const struct Qdisc_class_ops mqprio_class_ops = {
399 .graft = mqprio_graft,
400 .leaf = mqprio_leaf,
401 .get = mqprio_get,
402 .put = mqprio_put,
403 .walk = mqprio_walk,
404 .dump = mqprio_dump_class,
405 .dump_stats = mqprio_dump_class_stats,
406};
407
stephen hemmingerea18fd92011-02-23 09:06:51 +0000408static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
John Fastabendb8970f02011-01-17 08:06:09 +0000409 .cl_ops = &mqprio_class_ops,
410 .id = "mqprio",
411 .priv_size = sizeof(struct mqprio_sched),
412 .init = mqprio_init,
413 .destroy = mqprio_destroy,
414 .attach = mqprio_attach,
415 .dump = mqprio_dump,
416 .owner = THIS_MODULE,
417};
418
419static int __init mqprio_module_init(void)
420{
421 return register_qdisc(&mqprio_qdisc_ops);
422}
423
424static void __exit mqprio_module_exit(void)
425{
426 unregister_qdisc(&mqprio_qdisc_ops);
427}
428
429module_init(mqprio_module_init);
430module_exit(mqprio_module_exit);
431
432MODULE_LICENSE("GPL");