]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/sched/sch_mqprio.c
Merge branch 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszer...
[mirror_ubuntu-artful-kernel.git] / net / sched / sch_mqprio.c
CommitLineData
b8970f0b
JF
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>
3a9a231d 17#include <linux/module.h>
b8970f0b
JF
18#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);
16e5cc64 31 struct tc_to_netdev tc = {.type = TC_SETUP_MQPRIO};
b8970f0b
JF
32 unsigned int ntx;
33
ac7100ba
BH
34 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 }
b8970f0b
JF
41
42 if (priv->hw_owned && dev->netdev_ops->ndo_setup_tc)
16e5cc64 43 dev->netdev_ops->ndo_setup_tc(dev, sch->handle, 0, &tc);
b8970f0b
JF
44 else
45 netdev_set_num_tc(dev, 0);
b8970f0b
JF
46}
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
7838f2ce 111 if (!opt || nla_len(opt) < sizeof(*qopt))
b8970f0b
JF
112 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);
87b60cfa
ED
121 if (!priv->qdiscs)
122 return -ENOMEM;
b8970f0b
JF
123
124 for (i = 0; i < dev->num_tx_queues; i++) {
125 dev_queue = netdev_get_tx_queue(dev, i);
1f27cde3
ED
126 qdisc = qdisc_create_dflt(dev_queue,
127 get_default_qdisc_ops(dev, i),
b8970f0b
JF
128 TC_H_MAKE(TC_H_MAJ(sch->handle),
129 TC_H_MIN(i + 1)));
87b60cfa
ED
130 if (!qdisc)
131 return -ENOMEM;
132
b8970f0b 133 priv->qdiscs[i] = qdisc;
4eaf3b84 134 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
b8970f0b
JF
135 }
136
137 /* If the mqprio options indicate that hardware should own
138 * the queue mapping then run ndo_setup_tc otherwise use the
139 * supplied and verified mapping
140 */
141 if (qopt->hw) {
16e5cc64 142 struct tc_to_netdev tc = {.type = TC_SETUP_MQPRIO,
241deec9 143 { .tc = qopt->num_tc }};
16e5cc64 144
b8970f0b 145 priv->hw_owned = 1;
16e5cc64 146 err = dev->netdev_ops->ndo_setup_tc(dev, sch->handle, 0, &tc);
b8970f0b 147 if (err)
87b60cfa 148 return err;
b8970f0b
JF
149 } else {
150 netdev_set_num_tc(dev, qopt->num_tc);
151 for (i = 0; i < qopt->num_tc; i++)
152 netdev_set_tc_queue(dev, i,
153 qopt->count[i], qopt->offset[i]);
154 }
155
156 /* Always use supplied priority mappings */
157 for (i = 0; i < TC_BITMASK + 1; i++)
158 netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]);
159
160 sch->flags |= TCQ_F_MQROOT;
161 return 0;
b8970f0b
JF
162}
163
164static void mqprio_attach(struct Qdisc *sch)
165{
166 struct net_device *dev = qdisc_dev(sch);
167 struct mqprio_sched *priv = qdisc_priv(sch);
95dc1929 168 struct Qdisc *qdisc, *old;
b8970f0b
JF
169 unsigned int ntx;
170
171 /* Attach underlying qdisc */
172 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
173 qdisc = priv->qdiscs[ntx];
95dc1929
ED
174 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
175 if (old)
176 qdisc_destroy(old);
177 if (ntx < dev->real_num_tx_queues)
59cc1f61 178 qdisc_hash_add(qdisc);
b8970f0b
JF
179 }
180 kfree(priv->qdiscs);
181 priv->qdiscs = NULL;
182}
183
184static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch,
185 unsigned long cl)
186{
187 struct net_device *dev = qdisc_dev(sch);
188 unsigned long ntx = cl - 1 - netdev_get_num_tc(dev);
189
190 if (ntx >= dev->num_tx_queues)
191 return NULL;
192 return netdev_get_tx_queue(dev, ntx);
193}
194
195static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
196 struct Qdisc **old)
197{
198 struct net_device *dev = qdisc_dev(sch);
199 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
200
201 if (!dev_queue)
202 return -EINVAL;
203
204 if (dev->flags & IFF_UP)
205 dev_deactivate(dev);
206
207 *old = dev_graft_qdisc(dev_queue, new);
208
1abbe139 209 if (new)
4eaf3b84 210 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1abbe139 211
b8970f0b
JF
212 if (dev->flags & IFF_UP)
213 dev_activate(dev);
214
215 return 0;
216}
217
218static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb)
219{
220 struct net_device *dev = qdisc_dev(sch);
221 struct mqprio_sched *priv = qdisc_priv(sch);
222 unsigned char *b = skb_tail_pointer(skb);
144ce879 223 struct tc_mqprio_qopt opt = { 0 };
b8970f0b
JF
224 struct Qdisc *qdisc;
225 unsigned int i;
226
227 sch->q.qlen = 0;
228 memset(&sch->bstats, 0, sizeof(sch->bstats));
229 memset(&sch->qstats, 0, sizeof(sch->qstats));
230
231 for (i = 0; i < dev->num_tx_queues; i++) {
46e5da40 232 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
b8970f0b
JF
233 spin_lock_bh(qdisc_lock(qdisc));
234 sch->q.qlen += qdisc->q.qlen;
235 sch->bstats.bytes += qdisc->bstats.bytes;
236 sch->bstats.packets += qdisc->bstats.packets;
b8970f0b
JF
237 sch->qstats.backlog += qdisc->qstats.backlog;
238 sch->qstats.drops += qdisc->qstats.drops;
239 sch->qstats.requeues += qdisc->qstats.requeues;
240 sch->qstats.overlimits += qdisc->qstats.overlimits;
241 spin_unlock_bh(qdisc_lock(qdisc));
242 }
243
244 opt.num_tc = netdev_get_num_tc(dev);
245 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
246 opt.hw = priv->hw_owned;
247
248 for (i = 0; i < netdev_get_num_tc(dev); i++) {
249 opt.count[i] = dev->tc_to_txq[i].count;
250 opt.offset[i] = dev->tc_to_txq[i].offset;
251 }
252
1b34ec43
DM
253 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
254 goto nla_put_failure;
b8970f0b
JF
255
256 return skb->len;
257nla_put_failure:
258 nlmsg_trim(skb, b);
259 return -1;
260}
261
262static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl)
263{
264 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
265
266 if (!dev_queue)
267 return NULL;
268
269 return dev_queue->qdisc_sleeping;
270}
271
272static unsigned long mqprio_get(struct Qdisc *sch, u32 classid)
273{
274 struct net_device *dev = qdisc_dev(sch);
275 unsigned int ntx = TC_H_MIN(classid);
276
277 if (ntx > dev->num_tx_queues + netdev_get_num_tc(dev))
278 return 0;
279 return ntx;
280}
281
282static void mqprio_put(struct Qdisc *sch, unsigned long cl)
283{
284}
285
286static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl,
287 struct sk_buff *skb, struct tcmsg *tcm)
288{
289 struct net_device *dev = qdisc_dev(sch);
290
291 if (cl <= netdev_get_num_tc(dev)) {
292 tcm->tcm_parent = TC_H_ROOT;
293 tcm->tcm_info = 0;
294 } else {
295 int i;
296 struct netdev_queue *dev_queue;
297
298 dev_queue = mqprio_queue_get(sch, cl);
299 tcm->tcm_parent = 0;
300 for (i = 0; i < netdev_get_num_tc(dev); i++) {
301 struct netdev_tc_txq tc = dev->tc_to_txq[i];
302 int q_idx = cl - netdev_get_num_tc(dev);
303
304 if (q_idx > tc.offset &&
305 q_idx <= tc.offset + tc.count) {
306 tcm->tcm_parent =
307 TC_H_MAKE(TC_H_MAJ(sch->handle),
308 TC_H_MIN(i + 1));
309 break;
310 }
311 }
312 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
313 }
314 tcm->tcm_handle |= TC_H_MIN(cl);
315 return 0;
316}
317
318static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
ea18fd95 319 struct gnet_dump *d)
320 __releases(d->lock)
321 __acquires(d->lock)
b8970f0b
JF
322{
323 struct net_device *dev = qdisc_dev(sch);
324
325 if (cl <= netdev_get_num_tc(dev)) {
326 int i;
64015853 327 __u32 qlen = 0;
b8970f0b
JF
328 struct Qdisc *qdisc;
329 struct gnet_stats_queue qstats = {0};
330 struct gnet_stats_basic_packed bstats = {0};
331 struct netdev_tc_txq tc = dev->tc_to_txq[cl - 1];
332
333 /* Drop lock here it will be reclaimed before touching
334 * statistics this is required because the d->lock we
335 * hold here is the look on dev_queue->qdisc_sleeping
336 * also acquired below.
337 */
edb09eb1
ED
338 if (d->lock)
339 spin_unlock_bh(d->lock);
b8970f0b
JF
340
341 for (i = tc.offset; i < tc.offset + tc.count; i++) {
46e5da40
JF
342 struct netdev_queue *q = netdev_get_tx_queue(dev, i);
343
344 qdisc = rtnl_dereference(q->qdisc);
b8970f0b 345 spin_lock_bh(qdisc_lock(qdisc));
64015853 346 qlen += qdisc->q.qlen;
b8970f0b
JF
347 bstats.bytes += qdisc->bstats.bytes;
348 bstats.packets += qdisc->bstats.packets;
b8970f0b
JF
349 qstats.backlog += qdisc->qstats.backlog;
350 qstats.drops += qdisc->qstats.drops;
351 qstats.requeues += qdisc->qstats.requeues;
352 qstats.overlimits += qdisc->qstats.overlimits;
353 spin_unlock_bh(qdisc_lock(qdisc));
354 }
355 /* Reclaim root sleeping lock before completing stats */
edb09eb1
ED
356 if (d->lock)
357 spin_lock_bh(d->lock);
358 if (gnet_stats_copy_basic(NULL, d, NULL, &bstats) < 0 ||
b0ab6f92 359 gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
b8970f0b
JF
360 return -1;
361 } else {
362 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
363
364 sch = dev_queue->qdisc_sleeping;
edb09eb1
ED
365 if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
366 d, NULL, &sch->bstats) < 0 ||
b0ab6f92
JF
367 gnet_stats_copy_queue(d, NULL,
368 &sch->qstats, sch->q.qlen) < 0)
b8970f0b
JF
369 return -1;
370 }
371 return 0;
372}
373
374static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
375{
376 struct net_device *dev = qdisc_dev(sch);
377 unsigned long ntx;
378
379 if (arg->stop)
380 return;
381
382 /* Walk hierarchy with a virtual class per tc */
383 arg->count = arg->skip;
384 for (ntx = arg->skip;
385 ntx < dev->num_tx_queues + netdev_get_num_tc(dev);
386 ntx++) {
387 if (arg->fn(sch, ntx + 1, arg) < 0) {
388 arg->stop = 1;
389 break;
390 }
391 arg->count++;
392 }
393}
394
395static const struct Qdisc_class_ops mqprio_class_ops = {
396 .graft = mqprio_graft,
397 .leaf = mqprio_leaf,
398 .get = mqprio_get,
399 .put = mqprio_put,
400 .walk = mqprio_walk,
401 .dump = mqprio_dump_class,
402 .dump_stats = mqprio_dump_class_stats,
403};
404
ea18fd95 405static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
b8970f0b
JF
406 .cl_ops = &mqprio_class_ops,
407 .id = "mqprio",
408 .priv_size = sizeof(struct mqprio_sched),
409 .init = mqprio_init,
410 .destroy = mqprio_destroy,
411 .attach = mqprio_attach,
412 .dump = mqprio_dump,
413 .owner = THIS_MODULE,
414};
415
416static int __init mqprio_module_init(void)
417{
418 return register_qdisc(&mqprio_qdisc_ops);
419}
420
421static void __exit mqprio_module_exit(void)
422{
423 unregister_qdisc(&mqprio_qdisc_ops);
424}
425
426module_init(mqprio_module_init);
427module_exit(mqprio_module_exit);
428
429MODULE_LICENSE("GPL");