2 * net/sched/sch_mqprio.c
4 * Copyright (c) 2010 John Fastabend <john.r.fastabend@intel.com>
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.
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>
17 #include <linux/module.h>
18 #include <net/netlink.h>
19 #include <net/pkt_sched.h>
20 #include <net/sch_generic.h>
23 struct Qdisc
**qdiscs
;
27 static void mqprio_destroy(struct Qdisc
*sch
)
29 struct net_device
*dev
= qdisc_dev(sch
);
30 struct mqprio_sched
*priv
= qdisc_priv(sch
);
35 ntx
< dev
->num_tx_queues
&& priv
->qdiscs
[ntx
];
37 qdisc_destroy(priv
->qdiscs
[ntx
]);
41 if (priv
->hw_owned
&& dev
->netdev_ops
->ndo_setup_tc
)
42 dev
->netdev_ops
->ndo_setup_tc(dev
, 0);
44 netdev_set_num_tc(dev
, 0);
47 static int mqprio_parse_opt(struct net_device
*dev
, struct tc_mqprio_qopt
*qopt
)
51 /* Verify num_tc is not out of max range */
52 if (qopt
->num_tc
> TC_MAX_QUEUE
)
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
)
61 /* net_device does not support requested operation */
62 if (qopt
->hw
&& !dev
->netdev_ops
->ndo_setup_tc
)
65 /* if hw owned qcount and qoffset are taken from LLD so
66 * no reason to verify them here
71 for (i
= 0; i
< qopt
->num_tc
; i
++) {
72 unsigned int last
= qopt
->offset
[i
] + qopt
->count
[i
];
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.
77 if (qopt
->offset
[i
] >= dev
->real_num_tx_queues
||
79 last
> dev
->real_num_tx_queues
)
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
])
92 static int mqprio_init(struct Qdisc
*sch
, struct nlattr
*opt
)
94 struct net_device
*dev
= qdisc_dev(sch
);
95 struct mqprio_sched
*priv
= qdisc_priv(sch
);
96 struct netdev_queue
*dev_queue
;
98 int i
, err
= -EOPNOTSUPP
;
99 struct tc_mqprio_qopt
*qopt
= NULL
;
101 BUILD_BUG_ON(TC_MAX_QUEUE
!= TC_QOPT_MAX_QUEUE
);
102 BUILD_BUG_ON(TC_BITMASK
!= TC_QOPT_BITMASK
);
104 if (sch
->parent
!= TC_H_ROOT
)
107 if (!netif_is_multiqueue(dev
))
110 if (!opt
|| nla_len(opt
) < sizeof(*qopt
))
113 qopt
= nla_data(opt
);
114 if (mqprio_parse_opt(dev
, qopt
))
117 /* pre-allocate qdisc, attachment can't fail */
118 priv
->qdiscs
= kcalloc(dev
->num_tx_queues
, sizeof(priv
->qdiscs
[0]),
120 if (priv
->qdiscs
== NULL
) {
125 for (i
= 0; i
< dev
->num_tx_queues
; i
++) {
126 dev_queue
= netdev_get_tx_queue(dev
, i
);
127 qdisc
= qdisc_create_dflt(dev_queue
, default_qdisc_ops
,
128 TC_H_MAKE(TC_H_MAJ(sch
->handle
),
134 priv
->qdiscs
[i
] = qdisc
;
135 qdisc
->flags
|= TCQ_F_ONETXQUEUE
;
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
144 err
= dev
->netdev_ops
->ndo_setup_tc(dev
, qopt
->num_tc
);
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
]);
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
]);
158 sch
->flags
|= TCQ_F_MQROOT
;
166 static void mqprio_attach(struct Qdisc
*sch
)
168 struct net_device
*dev
= qdisc_dev(sch
);
169 struct mqprio_sched
*priv
= qdisc_priv(sch
);
170 struct Qdisc
*qdisc
, *old
;
173 /* Attach underlying qdisc */
174 for (ntx
= 0; ntx
< dev
->num_tx_queues
; ntx
++) {
175 qdisc
= priv
->qdiscs
[ntx
];
176 old
= dev_graft_qdisc(qdisc
->dev_queue
, qdisc
);
179 if (ntx
< dev
->real_num_tx_queues
)
180 qdisc_list_add(qdisc
);
186 static struct netdev_queue
*mqprio_queue_get(struct Qdisc
*sch
,
189 struct net_device
*dev
= qdisc_dev(sch
);
190 unsigned long ntx
= cl
- 1 - netdev_get_num_tc(dev
);
192 if (ntx
>= dev
->num_tx_queues
)
194 return netdev_get_tx_queue(dev
, ntx
);
197 static int mqprio_graft(struct Qdisc
*sch
, unsigned long cl
, struct Qdisc
*new,
200 struct net_device
*dev
= qdisc_dev(sch
);
201 struct netdev_queue
*dev_queue
= mqprio_queue_get(sch
, cl
);
206 if (dev
->flags
& IFF_UP
)
209 *old
= dev_graft_qdisc(dev_queue
, new);
212 new->flags
|= TCQ_F_ONETXQUEUE
;
214 if (dev
->flags
& IFF_UP
)
220 static int mqprio_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
222 struct net_device
*dev
= qdisc_dev(sch
);
223 struct mqprio_sched
*priv
= qdisc_priv(sch
);
224 unsigned char *b
= skb_tail_pointer(skb
);
225 struct tc_mqprio_qopt opt
= { 0 };
230 memset(&sch
->bstats
, 0, sizeof(sch
->bstats
));
231 memset(&sch
->qstats
, 0, sizeof(sch
->qstats
));
233 for (i
= 0; i
< dev
->num_tx_queues
; i
++) {
234 qdisc
= netdev_get_tx_queue(dev
, i
)->qdisc
;
235 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
;
239 sch
->qstats
.qlen
+= qdisc
->qstats
.qlen
;
240 sch
->qstats
.backlog
+= qdisc
->qstats
.backlog
;
241 sch
->qstats
.drops
+= qdisc
->qstats
.drops
;
242 sch
->qstats
.requeues
+= qdisc
->qstats
.requeues
;
243 sch
->qstats
.overlimits
+= qdisc
->qstats
.overlimits
;
244 spin_unlock_bh(qdisc_lock(qdisc
));
247 opt
.num_tc
= netdev_get_num_tc(dev
);
248 memcpy(opt
.prio_tc_map
, dev
->prio_tc_map
, sizeof(opt
.prio_tc_map
));
249 opt
.hw
= priv
->hw_owned
;
251 for (i
= 0; i
< netdev_get_num_tc(dev
); i
++) {
252 opt
.count
[i
] = dev
->tc_to_txq
[i
].count
;
253 opt
.offset
[i
] = dev
->tc_to_txq
[i
].offset
;
256 if (nla_put(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
))
257 goto nla_put_failure
;
265 static struct Qdisc
*mqprio_leaf(struct Qdisc
*sch
, unsigned long cl
)
267 struct netdev_queue
*dev_queue
= mqprio_queue_get(sch
, cl
);
272 return dev_queue
->qdisc_sleeping
;
275 static unsigned long mqprio_get(struct Qdisc
*sch
, u32 classid
)
277 struct net_device
*dev
= qdisc_dev(sch
);
278 unsigned int ntx
= TC_H_MIN(classid
);
280 if (ntx
> dev
->num_tx_queues
+ netdev_get_num_tc(dev
))
285 static void mqprio_put(struct Qdisc
*sch
, unsigned long cl
)
289 static int mqprio_dump_class(struct Qdisc
*sch
, unsigned long cl
,
290 struct sk_buff
*skb
, struct tcmsg
*tcm
)
292 struct net_device
*dev
= qdisc_dev(sch
);
294 if (cl
<= netdev_get_num_tc(dev
)) {
295 tcm
->tcm_parent
= TC_H_ROOT
;
299 struct netdev_queue
*dev_queue
;
301 dev_queue
= mqprio_queue_get(sch
, cl
);
303 for (i
= 0; i
< netdev_get_num_tc(dev
); i
++) {
304 struct netdev_tc_txq tc
= dev
->tc_to_txq
[i
];
305 int q_idx
= cl
- netdev_get_num_tc(dev
);
307 if (q_idx
> tc
.offset
&&
308 q_idx
<= tc
.offset
+ tc
.count
) {
310 TC_H_MAKE(TC_H_MAJ(sch
->handle
),
315 tcm
->tcm_info
= dev_queue
->qdisc_sleeping
->handle
;
317 tcm
->tcm_handle
|= TC_H_MIN(cl
);
321 static int mqprio_dump_class_stats(struct Qdisc
*sch
, unsigned long cl
,
326 struct net_device
*dev
= qdisc_dev(sch
);
328 if (cl
<= netdev_get_num_tc(dev
)) {
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];
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.
340 spin_unlock_bh(d
->lock
);
342 for (i
= tc
.offset
; i
< tc
.offset
+ tc
.count
; i
++) {
343 qdisc
= netdev_get_tx_queue(dev
, i
)->qdisc
;
344 spin_lock_bh(qdisc_lock(qdisc
));
345 bstats
.bytes
+= qdisc
->bstats
.bytes
;
346 bstats
.packets
+= qdisc
->bstats
.packets
;
347 qstats
.qlen
+= qdisc
->qstats
.qlen
;
348 qstats
.backlog
+= qdisc
->qstats
.backlog
;
349 qstats
.drops
+= qdisc
->qstats
.drops
;
350 qstats
.requeues
+= qdisc
->qstats
.requeues
;
351 qstats
.overlimits
+= qdisc
->qstats
.overlimits
;
352 spin_unlock_bh(qdisc_lock(qdisc
));
354 /* Reclaim root sleeping lock before completing stats */
355 spin_lock_bh(d
->lock
);
356 if (gnet_stats_copy_basic(d
, &bstats
) < 0 ||
357 gnet_stats_copy_queue(d
, &qstats
) < 0)
360 struct netdev_queue
*dev_queue
= mqprio_queue_get(sch
, cl
);
362 sch
= dev_queue
->qdisc_sleeping
;
363 sch
->qstats
.qlen
= sch
->q
.qlen
;
364 if (gnet_stats_copy_basic(d
, &sch
->bstats
) < 0 ||
365 gnet_stats_copy_queue(d
, &sch
->qstats
) < 0)
371 static void mqprio_walk(struct Qdisc
*sch
, struct qdisc_walker
*arg
)
373 struct net_device
*dev
= qdisc_dev(sch
);
379 /* Walk hierarchy with a virtual class per tc */
380 arg
->count
= arg
->skip
;
381 for (ntx
= arg
->skip
;
382 ntx
< dev
->num_tx_queues
+ netdev_get_num_tc(dev
);
384 if (arg
->fn(sch
, ntx
+ 1, arg
) < 0) {
392 static const struct Qdisc_class_ops mqprio_class_ops
= {
393 .graft
= mqprio_graft
,
398 .dump
= mqprio_dump_class
,
399 .dump_stats
= mqprio_dump_class_stats
,
402 static struct Qdisc_ops mqprio_qdisc_ops __read_mostly
= {
403 .cl_ops
= &mqprio_class_ops
,
405 .priv_size
= sizeof(struct mqprio_sched
),
407 .destroy
= mqprio_destroy
,
408 .attach
= mqprio_attach
,
410 .owner
= THIS_MODULE
,
413 static int __init
mqprio_module_init(void)
415 return register_qdisc(&mqprio_qdisc_ops
);
418 static void __exit
mqprio_module_exit(void)
420 unregister_qdisc(&mqprio_qdisc_ops
);
423 module_init(mqprio_module_init
);
424 module_exit(mqprio_module_exit
);
426 MODULE_LICENSE("GPL");