]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/sched/sch_mqprio.c
Merge tag 'ceph-for-4.12-rc3' of git://github.com/ceph/ceph-client
[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;
2026fecf 24 int hw_offload;
b8970f0b
JF
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
ac7100ba
BH
33 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 }
b8970f0b 40
56f36acd
AN
41 if (priv->hw_offload && dev->netdev_ops->ndo_setup_tc) {
42 struct tc_mqprio_qopt offload = { 0 };
43 struct tc_to_netdev tc = { .type = TC_SETUP_MQPRIO,
44 { .mqprio = &offload } };
45
16e5cc64 46 dev->netdev_ops->ndo_setup_tc(dev, sch->handle, 0, &tc);
56f36acd 47 } else {
b8970f0b 48 netdev_set_num_tc(dev, 0);
56f36acd 49 }
b8970f0b
JF
50}
51
52static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt)
53{
54 int i, j;
55
56 /* Verify num_tc is not out of max range */
57 if (qopt->num_tc > TC_MAX_QUEUE)
58 return -EINVAL;
59
60 /* Verify priority mapping uses valid tcs */
61 for (i = 0; i < TC_BITMASK + 1; i++) {
62 if (qopt->prio_tc_map[i] >= qopt->num_tc)
63 return -EINVAL;
64 }
65
2026fecf
AD
66 /* Limit qopt->hw to maximum supported offload value. Drivers have
67 * the option of overriding this later if they don't support the a
68 * given offload type.
69 */
70 if (qopt->hw > TC_MQPRIO_HW_OFFLOAD_MAX)
71 qopt->hw = TC_MQPRIO_HW_OFFLOAD_MAX;
b8970f0b 72
2026fecf
AD
73 /* If hardware offload is requested we will leave it to the device
74 * to either populate the queue counts itself or to validate the
75 * provided queue counts. If ndo_setup_tc is not present then
76 * hardware doesn't support offload and we should return an error.
b8970f0b
JF
77 */
78 if (qopt->hw)
2026fecf 79 return dev->netdev_ops->ndo_setup_tc ? 0 : -EINVAL;
b8970f0b
JF
80
81 for (i = 0; i < qopt->num_tc; i++) {
82 unsigned int last = qopt->offset[i] + qopt->count[i];
83
84 /* Verify the queue count is in tx range being equal to the
85 * real_num_tx_queues indicates the last queue is in use.
86 */
87 if (qopt->offset[i] >= dev->real_num_tx_queues ||
88 !qopt->count[i] ||
89 last > dev->real_num_tx_queues)
90 return -EINVAL;
91
92 /* Verify that the offset and counts do not overlap */
93 for (j = i + 1; j < qopt->num_tc; j++) {
94 if (last > qopt->offset[j])
95 return -EINVAL;
96 }
97 }
98
99 return 0;
100}
101
102static int mqprio_init(struct Qdisc *sch, struct nlattr *opt)
103{
104 struct net_device *dev = qdisc_dev(sch);
105 struct mqprio_sched *priv = qdisc_priv(sch);
106 struct netdev_queue *dev_queue;
107 struct Qdisc *qdisc;
108 int i, err = -EOPNOTSUPP;
109 struct tc_mqprio_qopt *qopt = NULL;
110
111 BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE);
112 BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK);
113
114 if (sch->parent != TC_H_ROOT)
115 return -EOPNOTSUPP;
116
117 if (!netif_is_multiqueue(dev))
118 return -EOPNOTSUPP;
119
7838f2ce 120 if (!opt || nla_len(opt) < sizeof(*qopt))
b8970f0b
JF
121 return -EINVAL;
122
123 qopt = nla_data(opt);
124 if (mqprio_parse_opt(dev, qopt))
125 return -EINVAL;
126
127 /* pre-allocate qdisc, attachment can't fail */
128 priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
129 GFP_KERNEL);
87b60cfa
ED
130 if (!priv->qdiscs)
131 return -ENOMEM;
b8970f0b
JF
132
133 for (i = 0; i < dev->num_tx_queues; i++) {
134 dev_queue = netdev_get_tx_queue(dev, i);
1f27cde3
ED
135 qdisc = qdisc_create_dflt(dev_queue,
136 get_default_qdisc_ops(dev, i),
b8970f0b
JF
137 TC_H_MAKE(TC_H_MAJ(sch->handle),
138 TC_H_MIN(i + 1)));
87b60cfa
ED
139 if (!qdisc)
140 return -ENOMEM;
141
b8970f0b 142 priv->qdiscs[i] = qdisc;
4eaf3b84 143 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
b8970f0b
JF
144 }
145
146 /* If the mqprio options indicate that hardware should own
147 * the queue mapping then run ndo_setup_tc otherwise use the
148 * supplied and verified mapping
149 */
150 if (qopt->hw) {
56f36acd
AN
151 struct tc_mqprio_qopt offload = *qopt;
152 struct tc_to_netdev tc = { .type = TC_SETUP_MQPRIO,
153 { .mqprio = &offload } };
16e5cc64 154
16e5cc64 155 err = dev->netdev_ops->ndo_setup_tc(dev, sch->handle, 0, &tc);
b8970f0b 156 if (err)
87b60cfa 157 return err;
2026fecf 158
56f36acd 159 priv->hw_offload = offload.hw;
b8970f0b
JF
160 } else {
161 netdev_set_num_tc(dev, qopt->num_tc);
162 for (i = 0; i < qopt->num_tc; i++)
163 netdev_set_tc_queue(dev, i,
164 qopt->count[i], qopt->offset[i]);
165 }
166
167 /* Always use supplied priority mappings */
168 for (i = 0; i < TC_BITMASK + 1; i++)
169 netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]);
170
171 sch->flags |= TCQ_F_MQROOT;
172 return 0;
b8970f0b
JF
173}
174
175static void mqprio_attach(struct Qdisc *sch)
176{
177 struct net_device *dev = qdisc_dev(sch);
178 struct mqprio_sched *priv = qdisc_priv(sch);
95dc1929 179 struct Qdisc *qdisc, *old;
b8970f0b
JF
180 unsigned int ntx;
181
182 /* Attach underlying qdisc */
183 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
184 qdisc = priv->qdiscs[ntx];
95dc1929
ED
185 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
186 if (old)
187 qdisc_destroy(old);
188 if (ntx < dev->real_num_tx_queues)
49b49971 189 qdisc_hash_add(qdisc, false);
b8970f0b
JF
190 }
191 kfree(priv->qdiscs);
192 priv->qdiscs = NULL;
193}
194
195static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch,
196 unsigned long cl)
197{
198 struct net_device *dev = qdisc_dev(sch);
199 unsigned long ntx = cl - 1 - netdev_get_num_tc(dev);
200
201 if (ntx >= dev->num_tx_queues)
202 return NULL;
203 return netdev_get_tx_queue(dev, ntx);
204}
205
206static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
207 struct Qdisc **old)
208{
209 struct net_device *dev = qdisc_dev(sch);
210 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
211
212 if (!dev_queue)
213 return -EINVAL;
214
215 if (dev->flags & IFF_UP)
216 dev_deactivate(dev);
217
218 *old = dev_graft_qdisc(dev_queue, new);
219
1abbe139 220 if (new)
4eaf3b84 221 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1abbe139 222
b8970f0b
JF
223 if (dev->flags & IFF_UP)
224 dev_activate(dev);
225
226 return 0;
227}
228
229static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb)
230{
231 struct net_device *dev = qdisc_dev(sch);
232 struct mqprio_sched *priv = qdisc_priv(sch);
233 unsigned char *b = skb_tail_pointer(skb);
144ce879 234 struct tc_mqprio_qopt opt = { 0 };
b8970f0b
JF
235 struct Qdisc *qdisc;
236 unsigned int i;
237
238 sch->q.qlen = 0;
239 memset(&sch->bstats, 0, sizeof(sch->bstats));
240 memset(&sch->qstats, 0, sizeof(sch->qstats));
241
242 for (i = 0; i < dev->num_tx_queues; i++) {
46e5da40 243 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
b8970f0b
JF
244 spin_lock_bh(qdisc_lock(qdisc));
245 sch->q.qlen += qdisc->q.qlen;
246 sch->bstats.bytes += qdisc->bstats.bytes;
247 sch->bstats.packets += qdisc->bstats.packets;
b8970f0b
JF
248 sch->qstats.backlog += qdisc->qstats.backlog;
249 sch->qstats.drops += qdisc->qstats.drops;
250 sch->qstats.requeues += qdisc->qstats.requeues;
251 sch->qstats.overlimits += qdisc->qstats.overlimits;
252 spin_unlock_bh(qdisc_lock(qdisc));
253 }
254
255 opt.num_tc = netdev_get_num_tc(dev);
256 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
2026fecf 257 opt.hw = priv->hw_offload;
b8970f0b
JF
258
259 for (i = 0; i < netdev_get_num_tc(dev); i++) {
260 opt.count[i] = dev->tc_to_txq[i].count;
261 opt.offset[i] = dev->tc_to_txq[i].offset;
262 }
263
1b34ec43
DM
264 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
265 goto nla_put_failure;
b8970f0b
JF
266
267 return skb->len;
268nla_put_failure:
269 nlmsg_trim(skb, b);
270 return -1;
271}
272
273static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl)
274{
275 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
276
277 if (!dev_queue)
278 return NULL;
279
280 return dev_queue->qdisc_sleeping;
281}
282
283static unsigned long mqprio_get(struct Qdisc *sch, u32 classid)
284{
285 struct net_device *dev = qdisc_dev(sch);
286 unsigned int ntx = TC_H_MIN(classid);
287
288 if (ntx > dev->num_tx_queues + netdev_get_num_tc(dev))
289 return 0;
290 return ntx;
291}
292
293static void mqprio_put(struct Qdisc *sch, unsigned long cl)
294{
295}
296
297static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl,
298 struct sk_buff *skb, struct tcmsg *tcm)
299{
300 struct net_device *dev = qdisc_dev(sch);
301
302 if (cl <= netdev_get_num_tc(dev)) {
303 tcm->tcm_parent = TC_H_ROOT;
304 tcm->tcm_info = 0;
305 } else {
306 int i;
307 struct netdev_queue *dev_queue;
308
309 dev_queue = mqprio_queue_get(sch, cl);
310 tcm->tcm_parent = 0;
311 for (i = 0; i < netdev_get_num_tc(dev); i++) {
312 struct netdev_tc_txq tc = dev->tc_to_txq[i];
313 int q_idx = cl - netdev_get_num_tc(dev);
314
315 if (q_idx > tc.offset &&
316 q_idx <= tc.offset + tc.count) {
317 tcm->tcm_parent =
318 TC_H_MAKE(TC_H_MAJ(sch->handle),
319 TC_H_MIN(i + 1));
320 break;
321 }
322 }
323 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
324 }
325 tcm->tcm_handle |= TC_H_MIN(cl);
326 return 0;
327}
328
329static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
ea18fd95 330 struct gnet_dump *d)
331 __releases(d->lock)
332 __acquires(d->lock)
b8970f0b
JF
333{
334 struct net_device *dev = qdisc_dev(sch);
335
336 if (cl <= netdev_get_num_tc(dev)) {
337 int i;
64015853 338 __u32 qlen = 0;
b8970f0b
JF
339 struct Qdisc *qdisc;
340 struct gnet_stats_queue qstats = {0};
341 struct gnet_stats_basic_packed bstats = {0};
342 struct netdev_tc_txq tc = dev->tc_to_txq[cl - 1];
343
344 /* Drop lock here it will be reclaimed before touching
345 * statistics this is required because the d->lock we
346 * hold here is the look on dev_queue->qdisc_sleeping
347 * also acquired below.
348 */
edb09eb1
ED
349 if (d->lock)
350 spin_unlock_bh(d->lock);
b8970f0b
JF
351
352 for (i = tc.offset; i < tc.offset + tc.count; i++) {
46e5da40
JF
353 struct netdev_queue *q = netdev_get_tx_queue(dev, i);
354
355 qdisc = rtnl_dereference(q->qdisc);
b8970f0b 356 spin_lock_bh(qdisc_lock(qdisc));
64015853 357 qlen += qdisc->q.qlen;
b8970f0b
JF
358 bstats.bytes += qdisc->bstats.bytes;
359 bstats.packets += qdisc->bstats.packets;
b8970f0b
JF
360 qstats.backlog += qdisc->qstats.backlog;
361 qstats.drops += qdisc->qstats.drops;
362 qstats.requeues += qdisc->qstats.requeues;
363 qstats.overlimits += qdisc->qstats.overlimits;
364 spin_unlock_bh(qdisc_lock(qdisc));
365 }
366 /* Reclaim root sleeping lock before completing stats */
edb09eb1
ED
367 if (d->lock)
368 spin_lock_bh(d->lock);
369 if (gnet_stats_copy_basic(NULL, d, NULL, &bstats) < 0 ||
b0ab6f92 370 gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
b8970f0b
JF
371 return -1;
372 } else {
373 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
374
375 sch = dev_queue->qdisc_sleeping;
edb09eb1
ED
376 if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
377 d, NULL, &sch->bstats) < 0 ||
b0ab6f92
JF
378 gnet_stats_copy_queue(d, NULL,
379 &sch->qstats, sch->q.qlen) < 0)
b8970f0b
JF
380 return -1;
381 }
382 return 0;
383}
384
385static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
386{
387 struct net_device *dev = qdisc_dev(sch);
388 unsigned long ntx;
389
390 if (arg->stop)
391 return;
392
393 /* Walk hierarchy with a virtual class per tc */
394 arg->count = arg->skip;
395 for (ntx = arg->skip;
396 ntx < dev->num_tx_queues + netdev_get_num_tc(dev);
397 ntx++) {
398 if (arg->fn(sch, ntx + 1, arg) < 0) {
399 arg->stop = 1;
400 break;
401 }
402 arg->count++;
403 }
404}
405
406static const struct Qdisc_class_ops mqprio_class_ops = {
407 .graft = mqprio_graft,
408 .leaf = mqprio_leaf,
409 .get = mqprio_get,
410 .put = mqprio_put,
411 .walk = mqprio_walk,
412 .dump = mqprio_dump_class,
413 .dump_stats = mqprio_dump_class_stats,
414};
415
ea18fd95 416static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
b8970f0b
JF
417 .cl_ops = &mqprio_class_ops,
418 .id = "mqprio",
419 .priv_size = sizeof(struct mqprio_sched),
420 .init = mqprio_init,
421 .destroy = mqprio_destroy,
422 .attach = mqprio_attach,
423 .dump = mqprio_dump,
424 .owner = THIS_MODULE,
425};
426
427static int __init mqprio_module_init(void)
428{
429 return register_qdisc(&mqprio_qdisc_ops);
430}
431
432static void __exit mqprio_module_exit(void)
433{
434 unregister_qdisc(&mqprio_qdisc_ops);
435}
436
437module_init(mqprio_module_init);
438module_exit(mqprio_module_exit);
439
440MODULE_LICENSE("GPL");