blob: 3811a745452cf402cd498ed47058fdf655d18cbd [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);
31 unsigned int ntx;
32
Ben Hutchingsac7100b2011-02-14 19:02:23 +000033 if (priv->qdiscs) {
34 for (ntx = 0;
35 ntx < dev->num_tx_queues && priv->qdiscs[ntx];
36 ntx++)
37 qdisc_destroy(priv->qdiscs[ntx]);
38 kfree(priv->qdiscs);
39 }
John Fastabendb8970f02011-01-17 08:06:09 +000040
41 if (priv->hw_owned && dev->netdev_ops->ndo_setup_tc)
42 dev->netdev_ops->ndo_setup_tc(dev, 0);
43 else
44 netdev_set_num_tc(dev, 0);
John Fastabendb8970f02011-01-17 08:06:09 +000045}
46
47static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt)
48{
49 int i, j;
50
51 /* Verify num_tc is not out of max range */
52 if (qopt->num_tc > TC_MAX_QUEUE)
53 return -EINVAL;
54
55 /* Verify priority mapping uses valid tcs */
56 for (i = 0; i < TC_BITMASK + 1; i++) {
57 if (qopt->prio_tc_map[i] >= qopt->num_tc)
58 return -EINVAL;
59 }
60
61 /* net_device does not support requested operation */
62 if (qopt->hw && !dev->netdev_ops->ndo_setup_tc)
63 return -EINVAL;
64
65 /* if hw owned qcount and qoffset are taken from LLD so
66 * no reason to verify them here
67 */
68 if (qopt->hw)
69 return 0;
70
71 for (i = 0; i < qopt->num_tc; i++) {
72 unsigned int last = qopt->offset[i] + qopt->count[i];
73
74 /* Verify the queue count is in tx range being equal to the
75 * real_num_tx_queues indicates the last queue is in use.
76 */
77 if (qopt->offset[i] >= dev->real_num_tx_queues ||
78 !qopt->count[i] ||
79 last > dev->real_num_tx_queues)
80 return -EINVAL;
81
82 /* Verify that the offset and counts do not overlap */
83 for (j = i + 1; j < qopt->num_tc; j++) {
84 if (last > qopt->offset[j])
85 return -EINVAL;
86 }
87 }
88
89 return 0;
90}
91
92static int mqprio_init(struct Qdisc *sch, struct nlattr *opt)
93{
94 struct net_device *dev = qdisc_dev(sch);
95 struct mqprio_sched *priv = qdisc_priv(sch);
96 struct netdev_queue *dev_queue;
97 struct Qdisc *qdisc;
98 int i, err = -EOPNOTSUPP;
99 struct tc_mqprio_qopt *qopt = NULL;
100
101 BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE);
102 BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK);
103
104 if (sch->parent != TC_H_ROOT)
105 return -EOPNOTSUPP;
106
107 if (!netif_is_multiqueue(dev))
108 return -EOPNOTSUPP;
109
Thomas Graf7838f2c2011-12-22 02:05:07 +0000110 if (!opt || nla_len(opt) < sizeof(*qopt))
John Fastabendb8970f02011-01-17 08:06:09 +0000111 return -EINVAL;
112
113 qopt = nla_data(opt);
114 if (mqprio_parse_opt(dev, qopt))
115 return -EINVAL;
116
117 /* pre-allocate qdisc, attachment can't fail */
118 priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
119 GFP_KERNEL);
120 if (priv->qdiscs == NULL) {
121 err = -ENOMEM;
122 goto err;
123 }
124
125 for (i = 0; i < dev->num_tx_queues; i++) {
126 dev_queue = netdev_get_tx_queue(dev, i);
stephen hemminger6da7c8f2013-08-27 16:19:08 -0700127 qdisc = qdisc_create_dflt(dev_queue, default_qdisc_ops,
John Fastabendb8970f02011-01-17 08:06:09 +0000128 TC_H_MAKE(TC_H_MAJ(sch->handle),
129 TC_H_MIN(i + 1)));
130 if (qdisc == NULL) {
131 err = -ENOMEM;
132 goto err;
133 }
John Fastabendb8970f02011-01-17 08:06:09 +0000134 priv->qdiscs[i] = qdisc;
Eric Dumazet1abbe132012-12-11 15:54:33 +0000135 qdisc->flags |= TCQ_F_ONETXQUEUE;
John Fastabendb8970f02011-01-17 08:06:09 +0000136 }
137
138 /* If the mqprio options indicate that hardware should own
139 * the queue mapping then run ndo_setup_tc otherwise use the
140 * supplied and verified mapping
141 */
142 if (qopt->hw) {
143 priv->hw_owned = 1;
144 err = dev->netdev_ops->ndo_setup_tc(dev, qopt->num_tc);
145 if (err)
146 goto err;
147 } else {
148 netdev_set_num_tc(dev, qopt->num_tc);
149 for (i = 0; i < qopt->num_tc; i++)
150 netdev_set_tc_queue(dev, i,
151 qopt->count[i], qopt->offset[i]);
152 }
153
154 /* Always use supplied priority mappings */
155 for (i = 0; i < TC_BITMASK + 1; i++)
156 netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]);
157
158 sch->flags |= TCQ_F_MQROOT;
159 return 0;
160
161err:
162 mqprio_destroy(sch);
163 return err;
164}
165
166static void mqprio_attach(struct Qdisc *sch)
167{
168 struct net_device *dev = qdisc_dev(sch);
169 struct mqprio_sched *priv = qdisc_priv(sch);
Eric Dumazet95dc1922013-12-05 11:12:02 -0800170 struct Qdisc *qdisc, *old;
John Fastabendb8970f02011-01-17 08:06:09 +0000171 unsigned int ntx;
172
173 /* Attach underlying qdisc */
174 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
175 qdisc = priv->qdiscs[ntx];
Eric Dumazet95dc1922013-12-05 11:12:02 -0800176 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
177 if (old)
178 qdisc_destroy(old);
179 if (ntx < dev->real_num_tx_queues)
180 qdisc_list_add(qdisc);
John Fastabendb8970f02011-01-17 08:06:09 +0000181 }
182 kfree(priv->qdiscs);
183 priv->qdiscs = NULL;
184}
185
186static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch,
187 unsigned long cl)
188{
189 struct net_device *dev = qdisc_dev(sch);
190 unsigned long ntx = cl - 1 - netdev_get_num_tc(dev);
191
192 if (ntx >= dev->num_tx_queues)
193 return NULL;
194 return netdev_get_tx_queue(dev, ntx);
195}
196
197static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
198 struct Qdisc **old)
199{
200 struct net_device *dev = qdisc_dev(sch);
201 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
202
203 if (!dev_queue)
204 return -EINVAL;
205
206 if (dev->flags & IFF_UP)
207 dev_deactivate(dev);
208
209 *old = dev_graft_qdisc(dev_queue, new);
210
Eric Dumazet1abbe132012-12-11 15:54:33 +0000211 if (new)
212 new->flags |= TCQ_F_ONETXQUEUE;
213
John Fastabendb8970f02011-01-17 08:06:09 +0000214 if (dev->flags & IFF_UP)
215 dev_activate(dev);
216
217 return 0;
218}
219
220static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb)
221{
222 struct net_device *dev = qdisc_dev(sch);
223 struct mqprio_sched *priv = qdisc_priv(sch);
224 unsigned char *b = skb_tail_pointer(skb);
Eric Dumazet144ce872011-01-26 07:21:57 +0000225 struct tc_mqprio_qopt opt = { 0 };
John Fastabendb8970f02011-01-17 08:06:09 +0000226 struct Qdisc *qdisc;
227 unsigned int i;
228
229 sch->q.qlen = 0;
230 memset(&sch->bstats, 0, sizeof(sch->bstats));
231 memset(&sch->qstats, 0, sizeof(sch->qstats));
232
233 for (i = 0; i < dev->num_tx_queues; i++) {
John Fastabend46e5da42014-09-12 20:04:52 -0700234 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
John Fastabendb8970f02011-01-17 08:06:09 +0000235 spin_lock_bh(qdisc_lock(qdisc));
236 sch->q.qlen += qdisc->q.qlen;
237 sch->bstats.bytes += qdisc->bstats.bytes;
238 sch->bstats.packets += qdisc->bstats.packets;
John Fastabendb8970f02011-01-17 08:06:09 +0000239 sch->qstats.backlog += qdisc->qstats.backlog;
240 sch->qstats.drops += qdisc->qstats.drops;
241 sch->qstats.requeues += qdisc->qstats.requeues;
242 sch->qstats.overlimits += qdisc->qstats.overlimits;
243 spin_unlock_bh(qdisc_lock(qdisc));
244 }
245
246 opt.num_tc = netdev_get_num_tc(dev);
247 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
248 opt.hw = priv->hw_owned;
249
250 for (i = 0; i < netdev_get_num_tc(dev); i++) {
251 opt.count[i] = dev->tc_to_txq[i].count;
252 opt.offset[i] = dev->tc_to_txq[i].offset;
253 }
254
David S. Miller1b34ec42012-03-29 05:11:39 -0400255 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
256 goto nla_put_failure;
John Fastabendb8970f02011-01-17 08:06:09 +0000257
258 return skb->len;
259nla_put_failure:
260 nlmsg_trim(skb, b);
261 return -1;
262}
263
264static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl)
265{
266 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
267
268 if (!dev_queue)
269 return NULL;
270
271 return dev_queue->qdisc_sleeping;
272}
273
274static unsigned long mqprio_get(struct Qdisc *sch, u32 classid)
275{
276 struct net_device *dev = qdisc_dev(sch);
277 unsigned int ntx = TC_H_MIN(classid);
278
279 if (ntx > dev->num_tx_queues + netdev_get_num_tc(dev))
280 return 0;
281 return ntx;
282}
283
284static void mqprio_put(struct Qdisc *sch, unsigned long cl)
285{
286}
287
288static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl,
289 struct sk_buff *skb, struct tcmsg *tcm)
290{
291 struct net_device *dev = qdisc_dev(sch);
292
293 if (cl <= netdev_get_num_tc(dev)) {
294 tcm->tcm_parent = TC_H_ROOT;
295 tcm->tcm_info = 0;
296 } else {
297 int i;
298 struct netdev_queue *dev_queue;
299
300 dev_queue = mqprio_queue_get(sch, cl);
301 tcm->tcm_parent = 0;
302 for (i = 0; i < netdev_get_num_tc(dev); i++) {
303 struct netdev_tc_txq tc = dev->tc_to_txq[i];
304 int q_idx = cl - netdev_get_num_tc(dev);
305
306 if (q_idx > tc.offset &&
307 q_idx <= tc.offset + tc.count) {
308 tcm->tcm_parent =
309 TC_H_MAKE(TC_H_MAJ(sch->handle),
310 TC_H_MIN(i + 1));
311 break;
312 }
313 }
314 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
315 }
316 tcm->tcm_handle |= TC_H_MIN(cl);
317 return 0;
318}
319
320static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
stephen hemmingerea18fd92011-02-23 09:06:51 +0000321 struct gnet_dump *d)
322 __releases(d->lock)
323 __acquires(d->lock)
John Fastabendb8970f02011-01-17 08:06:09 +0000324{
325 struct net_device *dev = qdisc_dev(sch);
326
327 if (cl <= netdev_get_num_tc(dev)) {
328 int i;
John Fastabend64015852014-09-28 11:53:57 -0700329 __u32 qlen = 0;
John Fastabendb8970f02011-01-17 08:06:09 +0000330 struct Qdisc *qdisc;
331 struct gnet_stats_queue qstats = {0};
332 struct gnet_stats_basic_packed bstats = {0};
333 struct netdev_tc_txq tc = dev->tc_to_txq[cl - 1];
334
335 /* Drop lock here it will be reclaimed before touching
336 * statistics this is required because the d->lock we
337 * hold here is the look on dev_queue->qdisc_sleeping
338 * also acquired below.
339 */
340 spin_unlock_bh(d->lock);
341
342 for (i = tc.offset; i < tc.offset + tc.count; i++) {
John Fastabend46e5da42014-09-12 20:04:52 -0700343 struct netdev_queue *q = netdev_get_tx_queue(dev, i);
344
345 qdisc = rtnl_dereference(q->qdisc);
John Fastabendb8970f02011-01-17 08:06:09 +0000346 spin_lock_bh(qdisc_lock(qdisc));
John Fastabend64015852014-09-28 11:53:57 -0700347 qlen += qdisc->q.qlen;
John Fastabendb8970f02011-01-17 08:06:09 +0000348 bstats.bytes += qdisc->bstats.bytes;
349 bstats.packets += qdisc->bstats.packets;
John Fastabendb8970f02011-01-17 08:06:09 +0000350 qstats.backlog += qdisc->qstats.backlog;
351 qstats.drops += qdisc->qstats.drops;
352 qstats.requeues += qdisc->qstats.requeues;
353 qstats.overlimits += qdisc->qstats.overlimits;
354 spin_unlock_bh(qdisc_lock(qdisc));
355 }
356 /* Reclaim root sleeping lock before completing stats */
357 spin_lock_bh(d->lock);
John Fastabend22e0f8b2014-09-28 11:52:56 -0700358 if (gnet_stats_copy_basic(d, NULL, &bstats) < 0 ||
John Fastabendb0ab6f92014-09-28 11:54:24 -0700359 gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
John Fastabendb8970f02011-01-17 08:06:09 +0000360 return -1;
361 } else {
362 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
363
364 sch = dev_queue->qdisc_sleeping;
John Fastabend22e0f8b2014-09-28 11:52:56 -0700365 if (gnet_stats_copy_basic(d, NULL, &sch->bstats) < 0 ||
John Fastabendb0ab6f92014-09-28 11:54:24 -0700366 gnet_stats_copy_queue(d, NULL,
367 &sch->qstats, sch->q.qlen) < 0)
John Fastabendb8970f02011-01-17 08:06:09 +0000368 return -1;
369 }
370 return 0;
371}
372
373static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
374{
375 struct net_device *dev = qdisc_dev(sch);
376 unsigned long ntx;
377
378 if (arg->stop)
379 return;
380
381 /* Walk hierarchy with a virtual class per tc */
382 arg->count = arg->skip;
383 for (ntx = arg->skip;
384 ntx < dev->num_tx_queues + netdev_get_num_tc(dev);
385 ntx++) {
386 if (arg->fn(sch, ntx + 1, arg) < 0) {
387 arg->stop = 1;
388 break;
389 }
390 arg->count++;
391 }
392}
393
394static const struct Qdisc_class_ops mqprio_class_ops = {
395 .graft = mqprio_graft,
396 .leaf = mqprio_leaf,
397 .get = mqprio_get,
398 .put = mqprio_put,
399 .walk = mqprio_walk,
400 .dump = mqprio_dump_class,
401 .dump_stats = mqprio_dump_class_stats,
402};
403
stephen hemmingerea18fd92011-02-23 09:06:51 +0000404static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
John Fastabendb8970f02011-01-17 08:06:09 +0000405 .cl_ops = &mqprio_class_ops,
406 .id = "mqprio",
407 .priv_size = sizeof(struct mqprio_sched),
408 .init = mqprio_init,
409 .destroy = mqprio_destroy,
410 .attach = mqprio_attach,
411 .dump = mqprio_dump,
412 .owner = THIS_MODULE,
413};
414
415static int __init mqprio_module_init(void)
416{
417 return register_qdisc(&mqprio_qdisc_ops);
418}
419
420static void __exit mqprio_module_exit(void)
421{
422 unregister_qdisc(&mqprio_qdisc_ops);
423}
424
425module_init(mqprio_module_init);
426module_exit(mqprio_module_exit);
427
428MODULE_LICENSE("GPL");