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
);
31 struct tc_to_netdev tc
= {.type
= TC_SETUP_MQPRIO
};
36 ntx
< dev
->num_tx_queues
&& priv
->qdiscs
[ntx
];
38 qdisc_destroy(priv
->qdiscs
[ntx
]);
42 if (priv
->hw_owned
&& dev
->netdev_ops
->ndo_setup_tc
)
43 dev
->netdev_ops
->ndo_setup_tc(dev
, sch
->handle
, 0, &tc
);
45 netdev_set_num_tc(dev
, 0);
48 static int mqprio_parse_opt(struct net_device
*dev
, struct tc_mqprio_qopt
*qopt
)
52 /* Verify num_tc is not out of max range */
53 if (qopt
->num_tc
> TC_MAX_QUEUE
)
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
)
62 /* net_device does not support requested operation */
63 if (qopt
->hw
&& !dev
->netdev_ops
->ndo_setup_tc
)
66 /* if hw owned qcount and qoffset are taken from LLD so
67 * no reason to verify them here
72 for (i
= 0; i
< qopt
->num_tc
; i
++) {
73 unsigned int last
= qopt
->offset
[i
] + qopt
->count
[i
];
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.
78 if (qopt
->offset
[i
] >= dev
->real_num_tx_queues
||
80 last
> dev
->real_num_tx_queues
)
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
])
93 static int mqprio_init(struct Qdisc
*sch
, struct nlattr
*opt
)
95 struct net_device
*dev
= qdisc_dev(sch
);
96 struct mqprio_sched
*priv
= qdisc_priv(sch
);
97 struct netdev_queue
*dev_queue
;
99 int i
, err
= -EOPNOTSUPP
;
100 struct tc_mqprio_qopt
*qopt
= NULL
;
102 BUILD_BUG_ON(TC_MAX_QUEUE
!= TC_QOPT_MAX_QUEUE
);
103 BUILD_BUG_ON(TC_BITMASK
!= TC_QOPT_BITMASK
);
105 if (sch
->parent
!= TC_H_ROOT
)
108 if (!netif_is_multiqueue(dev
))
111 if (!opt
|| nla_len(opt
) < sizeof(*qopt
))
114 qopt
= nla_data(opt
);
115 if (mqprio_parse_opt(dev
, qopt
))
118 /* pre-allocate qdisc, attachment can't fail */
119 priv
->qdiscs
= kcalloc(dev
->num_tx_queues
, sizeof(priv
->qdiscs
[0]),
121 if (priv
->qdiscs
== NULL
) {
126 for (i
= 0; i
< dev
->num_tx_queues
; i
++) {
127 dev_queue
= netdev_get_tx_queue(dev
, i
);
128 qdisc
= qdisc_create_dflt(dev_queue
,
129 get_default_qdisc_ops(dev
, i
),
130 TC_H_MAKE(TC_H_MAJ(sch
->handle
),
136 priv
->qdiscs
[i
] = qdisc
;
137 qdisc
->flags
|= TCQ_F_ONETXQUEUE
| TCQ_F_NOPARENT
;
140 /* If the mqprio options indicate that hardware should own
141 * the queue mapping then run ndo_setup_tc otherwise use the
142 * supplied and verified mapping
145 struct tc_to_netdev tc
= {.type
= TC_SETUP_MQPRIO
,
146 { .tc
= qopt
->num_tc
}};
149 err
= dev
->netdev_ops
->ndo_setup_tc(dev
, sch
->handle
, 0, &tc
);
153 netdev_set_num_tc(dev
, qopt
->num_tc
);
154 for (i
= 0; i
< qopt
->num_tc
; i
++)
155 netdev_set_tc_queue(dev
, i
,
156 qopt
->count
[i
], qopt
->offset
[i
]);
159 /* Always use supplied priority mappings */
160 for (i
= 0; i
< TC_BITMASK
+ 1; i
++)
161 netdev_set_prio_tc_map(dev
, i
, qopt
->prio_tc_map
[i
]);
163 sch
->flags
|= TCQ_F_MQROOT
;
171 static void mqprio_attach(struct Qdisc
*sch
)
173 struct net_device
*dev
= qdisc_dev(sch
);
174 struct mqprio_sched
*priv
= qdisc_priv(sch
);
175 struct Qdisc
*qdisc
, *old
;
178 /* Attach underlying qdisc */
179 for (ntx
= 0; ntx
< dev
->num_tx_queues
; ntx
++) {
180 qdisc
= priv
->qdiscs
[ntx
];
181 old
= dev_graft_qdisc(qdisc
->dev_queue
, qdisc
);
184 if (ntx
< dev
->real_num_tx_queues
)
185 qdisc_list_add(qdisc
);
191 static struct netdev_queue
*mqprio_queue_get(struct Qdisc
*sch
,
194 struct net_device
*dev
= qdisc_dev(sch
);
195 unsigned long ntx
= cl
- 1 - netdev_get_num_tc(dev
);
197 if (ntx
>= dev
->num_tx_queues
)
199 return netdev_get_tx_queue(dev
, ntx
);
202 static int mqprio_graft(struct Qdisc
*sch
, unsigned long cl
, struct Qdisc
*new,
205 struct net_device
*dev
= qdisc_dev(sch
);
206 struct netdev_queue
*dev_queue
= mqprio_queue_get(sch
, cl
);
211 if (dev
->flags
& IFF_UP
)
214 *old
= dev_graft_qdisc(dev_queue
, new);
217 new->flags
|= TCQ_F_ONETXQUEUE
| TCQ_F_NOPARENT
;
219 if (dev
->flags
& IFF_UP
)
225 static int mqprio_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
227 struct net_device
*dev
= qdisc_dev(sch
);
228 struct mqprio_sched
*priv
= qdisc_priv(sch
);
229 unsigned char *b
= skb_tail_pointer(skb
);
230 struct tc_mqprio_qopt opt
= { 0 };
235 memset(&sch
->bstats
, 0, sizeof(sch
->bstats
));
236 memset(&sch
->qstats
, 0, sizeof(sch
->qstats
));
238 for (i
= 0; i
< dev
->num_tx_queues
; i
++) {
239 qdisc
= rtnl_dereference(netdev_get_tx_queue(dev
, i
)->qdisc
);
240 spin_lock_bh(qdisc_lock(qdisc
));
241 sch
->q
.qlen
+= qdisc
->q
.qlen
;
242 sch
->bstats
.bytes
+= qdisc
->bstats
.bytes
;
243 sch
->bstats
.packets
+= qdisc
->bstats
.packets
;
244 sch
->qstats
.backlog
+= qdisc
->qstats
.backlog
;
245 sch
->qstats
.drops
+= qdisc
->qstats
.drops
;
246 sch
->qstats
.requeues
+= qdisc
->qstats
.requeues
;
247 sch
->qstats
.overlimits
+= qdisc
->qstats
.overlimits
;
248 spin_unlock_bh(qdisc_lock(qdisc
));
251 opt
.num_tc
= netdev_get_num_tc(dev
);
252 memcpy(opt
.prio_tc_map
, dev
->prio_tc_map
, sizeof(opt
.prio_tc_map
));
253 opt
.hw
= priv
->hw_owned
;
255 for (i
= 0; i
< netdev_get_num_tc(dev
); i
++) {
256 opt
.count
[i
] = dev
->tc_to_txq
[i
].count
;
257 opt
.offset
[i
] = dev
->tc_to_txq
[i
].offset
;
260 if (nla_put(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
))
261 goto nla_put_failure
;
269 static struct Qdisc
*mqprio_leaf(struct Qdisc
*sch
, unsigned long cl
)
271 struct netdev_queue
*dev_queue
= mqprio_queue_get(sch
, cl
);
276 return dev_queue
->qdisc_sleeping
;
279 static unsigned long mqprio_get(struct Qdisc
*sch
, u32 classid
)
281 struct net_device
*dev
= qdisc_dev(sch
);
282 unsigned int ntx
= TC_H_MIN(classid
);
284 if (ntx
> dev
->num_tx_queues
+ netdev_get_num_tc(dev
))
289 static void mqprio_put(struct Qdisc
*sch
, unsigned long cl
)
293 static int mqprio_dump_class(struct Qdisc
*sch
, unsigned long cl
,
294 struct sk_buff
*skb
, struct tcmsg
*tcm
)
296 struct net_device
*dev
= qdisc_dev(sch
);
298 if (cl
<= netdev_get_num_tc(dev
)) {
299 tcm
->tcm_parent
= TC_H_ROOT
;
303 struct netdev_queue
*dev_queue
;
305 dev_queue
= mqprio_queue_get(sch
, cl
);
307 for (i
= 0; i
< netdev_get_num_tc(dev
); i
++) {
308 struct netdev_tc_txq tc
= dev
->tc_to_txq
[i
];
309 int q_idx
= cl
- netdev_get_num_tc(dev
);
311 if (q_idx
> tc
.offset
&&
312 q_idx
<= tc
.offset
+ tc
.count
) {
314 TC_H_MAKE(TC_H_MAJ(sch
->handle
),
319 tcm
->tcm_info
= dev_queue
->qdisc_sleeping
->handle
;
321 tcm
->tcm_handle
|= TC_H_MIN(cl
);
325 static int mqprio_dump_class_stats(struct Qdisc
*sch
, unsigned long cl
,
330 struct net_device
*dev
= qdisc_dev(sch
);
332 if (cl
<= netdev_get_num_tc(dev
)) {
336 struct gnet_stats_queue qstats
= {0};
337 struct gnet_stats_basic_packed bstats
= {0};
338 struct netdev_tc_txq tc
= dev
->tc_to_txq
[cl
- 1];
340 /* Drop lock here it will be reclaimed before touching
341 * statistics this is required because the d->lock we
342 * hold here is the look on dev_queue->qdisc_sleeping
343 * also acquired below.
346 spin_unlock_bh(d
->lock
);
348 for (i
= tc
.offset
; i
< tc
.offset
+ tc
.count
; i
++) {
349 struct netdev_queue
*q
= netdev_get_tx_queue(dev
, i
);
351 qdisc
= rtnl_dereference(q
->qdisc
);
352 spin_lock_bh(qdisc_lock(qdisc
));
353 qlen
+= qdisc
->q
.qlen
;
354 bstats
.bytes
+= qdisc
->bstats
.bytes
;
355 bstats
.packets
+= qdisc
->bstats
.packets
;
356 qstats
.backlog
+= qdisc
->qstats
.backlog
;
357 qstats
.drops
+= qdisc
->qstats
.drops
;
358 qstats
.requeues
+= qdisc
->qstats
.requeues
;
359 qstats
.overlimits
+= qdisc
->qstats
.overlimits
;
360 spin_unlock_bh(qdisc_lock(qdisc
));
362 /* Reclaim root sleeping lock before completing stats */
364 spin_lock_bh(d
->lock
);
365 if (gnet_stats_copy_basic(NULL
, d
, NULL
, &bstats
) < 0 ||
366 gnet_stats_copy_queue(d
, NULL
, &qstats
, qlen
) < 0)
369 struct netdev_queue
*dev_queue
= mqprio_queue_get(sch
, cl
);
371 sch
= dev_queue
->qdisc_sleeping
;
372 if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch
),
373 d
, NULL
, &sch
->bstats
) < 0 ||
374 gnet_stats_copy_queue(d
, NULL
,
375 &sch
->qstats
, sch
->q
.qlen
) < 0)
381 static void mqprio_walk(struct Qdisc
*sch
, struct qdisc_walker
*arg
)
383 struct net_device
*dev
= qdisc_dev(sch
);
389 /* Walk hierarchy with a virtual class per tc */
390 arg
->count
= arg
->skip
;
391 for (ntx
= arg
->skip
;
392 ntx
< dev
->num_tx_queues
+ netdev_get_num_tc(dev
);
394 if (arg
->fn(sch
, ntx
+ 1, arg
) < 0) {
402 static const struct Qdisc_class_ops mqprio_class_ops
= {
403 .graft
= mqprio_graft
,
408 .dump
= mqprio_dump_class
,
409 .dump_stats
= mqprio_dump_class_stats
,
412 static struct Qdisc_ops mqprio_qdisc_ops __read_mostly
= {
413 .cl_ops
= &mqprio_class_ops
,
415 .priv_size
= sizeof(struct mqprio_sched
),
417 .destroy
= mqprio_destroy
,
418 .attach
= mqprio_attach
,
420 .owner
= THIS_MODULE
,
423 static int __init
mqprio_module_init(void)
425 return register_qdisc(&mqprio_qdisc_ops
);
428 static void __exit
mqprio_module_exit(void)
430 unregister_qdisc(&mqprio_qdisc_ops
);
433 module_init(mqprio_module_init
);
434 module_exit(mqprio_module_exit
);
436 MODULE_LICENSE("GPL");