of_mdio: kill useless variable in of_mdiobus_register()
[deliverable/linux.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);
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);
6da7c8fc 128 qdisc = qdisc_create_dflt(dev_queue, default_qdisc_ops,
b8970f0b
JF
129 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 }
b8970f0b 135 priv->qdiscs[i] = qdisc;
4eaf3b84 136 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
b8970f0b
JF
137 }
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) {
16e5cc64
JF
144 struct tc_to_netdev tc = {.type = TC_SETUP_MQPRIO,
145 .tc = qopt->num_tc};
146
b8970f0b 147 priv->hw_owned = 1;
16e5cc64 148 err = dev->netdev_ops->ndo_setup_tc(dev, sch->handle, 0, &tc);
b8970f0b
JF
149 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);
95dc1929 174 struct Qdisc *qdisc, *old;
b8970f0b
JF
175 unsigned int ntx;
176
177 /* Attach underlying qdisc */
178 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
179 qdisc = priv->qdiscs[ntx];
95dc1929
ED
180 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);
b8970f0b
JF
185 }
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
1abbe139 215 if (new)
4eaf3b84 216 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1abbe139 217
b8970f0b
JF
218 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);
144ce879 229 struct tc_mqprio_qopt opt = { 0 };
b8970f0b
JF
230 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++) {
46e5da40 238 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
b8970f0b
JF
239 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;
b8970f0b
JF
243 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
1b34ec43
DM
259 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
260 goto nla_put_failure;
b8970f0b
JF
261
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,
ea18fd95 325 struct gnet_dump *d)
326 __releases(d->lock)
327 __acquires(d->lock)
b8970f0b
JF
328{
329 struct net_device *dev = qdisc_dev(sch);
330
331 if (cl <= netdev_get_num_tc(dev)) {
332 int i;
64015853 333 __u32 qlen = 0;
b8970f0b
JF
334 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++) {
46e5da40
JF
347 struct netdev_queue *q = netdev_get_tx_queue(dev, i);
348
349 qdisc = rtnl_dereference(q->qdisc);
b8970f0b 350 spin_lock_bh(qdisc_lock(qdisc));
64015853 351 qlen += qdisc->q.qlen;
b8970f0b
JF
352 bstats.bytes += qdisc->bstats.bytes;
353 bstats.packets += qdisc->bstats.packets;
b8970f0b
JF
354 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);
22e0f8b9 362 if (gnet_stats_copy_basic(d, NULL, &bstats) < 0 ||
b0ab6f92 363 gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
b8970f0b
JF
364 return -1;
365 } else {
366 struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
367
368 sch = dev_queue->qdisc_sleeping;
22e0f8b9 369 if (gnet_stats_copy_basic(d, NULL, &sch->bstats) < 0 ||
b0ab6f92
JF
370 gnet_stats_copy_queue(d, NULL,
371 &sch->qstats, sch->q.qlen) < 0)
b8970f0b
JF
372 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
ea18fd95 408static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
b8970f0b
JF
409 .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");
This page took 0.457763 seconds and 5 git commands to generate.