xref: /openbmc/linux/net/sched/sch_mqprio.c (revision 143976ce992fcf3bfc0f4d15d5726bb492dcf262)
1b8970f0bSJohn Fastabend /*
2b8970f0bSJohn Fastabend  * net/sched/sch_mqprio.c
3b8970f0bSJohn Fastabend  *
4b8970f0bSJohn Fastabend  * Copyright (c) 2010 John Fastabend <john.r.fastabend@intel.com>
5b8970f0bSJohn Fastabend  *
6b8970f0bSJohn Fastabend  * This program is free software; you can redistribute it and/or
7b8970f0bSJohn Fastabend  * modify it under the terms of the GNU General Public License
8b8970f0bSJohn Fastabend  * version 2 as published by the Free Software Foundation.
9b8970f0bSJohn Fastabend  */
10b8970f0bSJohn Fastabend 
11b8970f0bSJohn Fastabend #include <linux/types.h>
12b8970f0bSJohn Fastabend #include <linux/slab.h>
13b8970f0bSJohn Fastabend #include <linux/kernel.h>
14b8970f0bSJohn Fastabend #include <linux/string.h>
15b8970f0bSJohn Fastabend #include <linux/errno.h>
16b8970f0bSJohn Fastabend #include <linux/skbuff.h>
173a9a231dSPaul Gortmaker #include <linux/module.h>
18b8970f0bSJohn Fastabend #include <net/netlink.h>
19b8970f0bSJohn Fastabend #include <net/pkt_sched.h>
20b8970f0bSJohn Fastabend #include <net/sch_generic.h>
21b8970f0bSJohn Fastabend 
22b8970f0bSJohn Fastabend struct mqprio_sched {
23b8970f0bSJohn Fastabend 	struct Qdisc		**qdiscs;
242026fecfSAlexander Duyck 	int hw_offload;
25b8970f0bSJohn Fastabend };
26b8970f0bSJohn Fastabend 
27b8970f0bSJohn Fastabend static void mqprio_destroy(struct Qdisc *sch)
28b8970f0bSJohn Fastabend {
29b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
30b8970f0bSJohn Fastabend 	struct mqprio_sched *priv = qdisc_priv(sch);
31b8970f0bSJohn Fastabend 	unsigned int ntx;
32b8970f0bSJohn Fastabend 
33ac7100baSBen Hutchings 	if (priv->qdiscs) {
34ac7100baSBen Hutchings 		for (ntx = 0;
35ac7100baSBen Hutchings 		     ntx < dev->num_tx_queues && priv->qdiscs[ntx];
36ac7100baSBen Hutchings 		     ntx++)
37b8970f0bSJohn Fastabend 			qdisc_destroy(priv->qdiscs[ntx]);
38ac7100baSBen Hutchings 		kfree(priv->qdiscs);
39ac7100baSBen Hutchings 	}
40b8970f0bSJohn Fastabend 
4156f36acdSAmritha Nambiar 	if (priv->hw_offload && dev->netdev_ops->ndo_setup_tc) {
42de4784caSJiri Pirko 		struct tc_mqprio_qopt mqprio = {};
4356f36acdSAmritha Nambiar 
44de4784caSJiri Pirko 		dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_MQPRIO, &mqprio);
4556f36acdSAmritha Nambiar 	} else {
46b8970f0bSJohn Fastabend 		netdev_set_num_tc(dev, 0);
47b8970f0bSJohn Fastabend 	}
4856f36acdSAmritha Nambiar }
49b8970f0bSJohn Fastabend 
50b8970f0bSJohn Fastabend static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt)
51b8970f0bSJohn Fastabend {
52b8970f0bSJohn Fastabend 	int i, j;
53b8970f0bSJohn Fastabend 
54b8970f0bSJohn Fastabend 	/* Verify num_tc is not out of max range */
55b8970f0bSJohn Fastabend 	if (qopt->num_tc > TC_MAX_QUEUE)
56b8970f0bSJohn Fastabend 		return -EINVAL;
57b8970f0bSJohn Fastabend 
58b8970f0bSJohn Fastabend 	/* Verify priority mapping uses valid tcs */
59b8970f0bSJohn Fastabend 	for (i = 0; i < TC_BITMASK + 1; i++) {
60b8970f0bSJohn Fastabend 		if (qopt->prio_tc_map[i] >= qopt->num_tc)
61b8970f0bSJohn Fastabend 			return -EINVAL;
62b8970f0bSJohn Fastabend 	}
63b8970f0bSJohn Fastabend 
642026fecfSAlexander Duyck 	/* Limit qopt->hw to maximum supported offload value.  Drivers have
652026fecfSAlexander Duyck 	 * the option of overriding this later if they don't support the a
662026fecfSAlexander Duyck 	 * given offload type.
672026fecfSAlexander Duyck 	 */
682026fecfSAlexander Duyck 	if (qopt->hw > TC_MQPRIO_HW_OFFLOAD_MAX)
692026fecfSAlexander Duyck 		qopt->hw = TC_MQPRIO_HW_OFFLOAD_MAX;
70b8970f0bSJohn Fastabend 
712026fecfSAlexander Duyck 	/* If hardware offload is requested we will leave it to the device
722026fecfSAlexander Duyck 	 * to either populate the queue counts itself or to validate the
732026fecfSAlexander Duyck 	 * provided queue counts.  If ndo_setup_tc is not present then
742026fecfSAlexander Duyck 	 * hardware doesn't support offload and we should return an error.
75b8970f0bSJohn Fastabend 	 */
76b8970f0bSJohn Fastabend 	if (qopt->hw)
772026fecfSAlexander Duyck 		return dev->netdev_ops->ndo_setup_tc ? 0 : -EINVAL;
78b8970f0bSJohn Fastabend 
79b8970f0bSJohn Fastabend 	for (i = 0; i < qopt->num_tc; i++) {
80b8970f0bSJohn Fastabend 		unsigned int last = qopt->offset[i] + qopt->count[i];
81b8970f0bSJohn Fastabend 
82b8970f0bSJohn Fastabend 		/* Verify the queue count is in tx range being equal to the
83b8970f0bSJohn Fastabend 		 * real_num_tx_queues indicates the last queue is in use.
84b8970f0bSJohn Fastabend 		 */
85b8970f0bSJohn Fastabend 		if (qopt->offset[i] >= dev->real_num_tx_queues ||
86b8970f0bSJohn Fastabend 		    !qopt->count[i] ||
87b8970f0bSJohn Fastabend 		    last > dev->real_num_tx_queues)
88b8970f0bSJohn Fastabend 			return -EINVAL;
89b8970f0bSJohn Fastabend 
90b8970f0bSJohn Fastabend 		/* Verify that the offset and counts do not overlap */
91b8970f0bSJohn Fastabend 		for (j = i + 1; j < qopt->num_tc; j++) {
92b8970f0bSJohn Fastabend 			if (last > qopt->offset[j])
93b8970f0bSJohn Fastabend 				return -EINVAL;
94b8970f0bSJohn Fastabend 		}
95b8970f0bSJohn Fastabend 	}
96b8970f0bSJohn Fastabend 
97b8970f0bSJohn Fastabend 	return 0;
98b8970f0bSJohn Fastabend }
99b8970f0bSJohn Fastabend 
100b8970f0bSJohn Fastabend static int mqprio_init(struct Qdisc *sch, struct nlattr *opt)
101b8970f0bSJohn Fastabend {
102b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
103b8970f0bSJohn Fastabend 	struct mqprio_sched *priv = qdisc_priv(sch);
104b8970f0bSJohn Fastabend 	struct netdev_queue *dev_queue;
105b8970f0bSJohn Fastabend 	struct Qdisc *qdisc;
106b8970f0bSJohn Fastabend 	int i, err = -EOPNOTSUPP;
107b8970f0bSJohn Fastabend 	struct tc_mqprio_qopt *qopt = NULL;
108b8970f0bSJohn Fastabend 
109b8970f0bSJohn Fastabend 	BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE);
110b8970f0bSJohn Fastabend 	BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK);
111b8970f0bSJohn Fastabend 
112b8970f0bSJohn Fastabend 	if (sch->parent != TC_H_ROOT)
113b8970f0bSJohn Fastabend 		return -EOPNOTSUPP;
114b8970f0bSJohn Fastabend 
115b8970f0bSJohn Fastabend 	if (!netif_is_multiqueue(dev))
116b8970f0bSJohn Fastabend 		return -EOPNOTSUPP;
117b8970f0bSJohn Fastabend 
1187838f2ceSThomas Graf 	if (!opt || nla_len(opt) < sizeof(*qopt))
119b8970f0bSJohn Fastabend 		return -EINVAL;
120b8970f0bSJohn Fastabend 
121b8970f0bSJohn Fastabend 	qopt = nla_data(opt);
122b8970f0bSJohn Fastabend 	if (mqprio_parse_opt(dev, qopt))
123b8970f0bSJohn Fastabend 		return -EINVAL;
124b8970f0bSJohn Fastabend 
125b8970f0bSJohn Fastabend 	/* pre-allocate qdisc, attachment can't fail */
126b8970f0bSJohn Fastabend 	priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
127b8970f0bSJohn Fastabend 			       GFP_KERNEL);
12887b60cfaSEric Dumazet 	if (!priv->qdiscs)
12987b60cfaSEric Dumazet 		return -ENOMEM;
130b8970f0bSJohn Fastabend 
131b8970f0bSJohn Fastabend 	for (i = 0; i < dev->num_tx_queues; i++) {
132b8970f0bSJohn Fastabend 		dev_queue = netdev_get_tx_queue(dev, i);
1331f27cde3SEric Dumazet 		qdisc = qdisc_create_dflt(dev_queue,
1341f27cde3SEric Dumazet 					  get_default_qdisc_ops(dev, i),
135b8970f0bSJohn Fastabend 					  TC_H_MAKE(TC_H_MAJ(sch->handle),
136b8970f0bSJohn Fastabend 						    TC_H_MIN(i + 1)));
13787b60cfaSEric Dumazet 		if (!qdisc)
13887b60cfaSEric Dumazet 			return -ENOMEM;
13987b60cfaSEric Dumazet 
140b8970f0bSJohn Fastabend 		priv->qdiscs[i] = qdisc;
1414eaf3b84SEric Dumazet 		qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
142b8970f0bSJohn Fastabend 	}
143b8970f0bSJohn Fastabend 
144b8970f0bSJohn Fastabend 	/* If the mqprio options indicate that hardware should own
145b8970f0bSJohn Fastabend 	 * the queue mapping then run ndo_setup_tc otherwise use the
146b8970f0bSJohn Fastabend 	 * supplied and verified mapping
147b8970f0bSJohn Fastabend 	 */
148b8970f0bSJohn Fastabend 	if (qopt->hw) {
149de4784caSJiri Pirko 		struct tc_mqprio_qopt mqprio = *qopt;
15016e5cc64SJohn Fastabend 
151de4784caSJiri Pirko 		err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_MQPRIO,
152de4784caSJiri Pirko 						    &mqprio);
153b8970f0bSJohn Fastabend 		if (err)
15487b60cfaSEric Dumazet 			return err;
1552026fecfSAlexander Duyck 
156de4784caSJiri Pirko 		priv->hw_offload = mqprio.hw;
157b8970f0bSJohn Fastabend 	} else {
158b8970f0bSJohn Fastabend 		netdev_set_num_tc(dev, qopt->num_tc);
159b8970f0bSJohn Fastabend 		for (i = 0; i < qopt->num_tc; i++)
160b8970f0bSJohn Fastabend 			netdev_set_tc_queue(dev, i,
161b8970f0bSJohn Fastabend 					    qopt->count[i], qopt->offset[i]);
162b8970f0bSJohn Fastabend 	}
163b8970f0bSJohn Fastabend 
164b8970f0bSJohn Fastabend 	/* Always use supplied priority mappings */
165b8970f0bSJohn Fastabend 	for (i = 0; i < TC_BITMASK + 1; i++)
166b8970f0bSJohn Fastabend 		netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]);
167b8970f0bSJohn Fastabend 
168b8970f0bSJohn Fastabend 	sch->flags |= TCQ_F_MQROOT;
169b8970f0bSJohn Fastabend 	return 0;
170b8970f0bSJohn Fastabend }
171b8970f0bSJohn Fastabend 
172b8970f0bSJohn Fastabend static void mqprio_attach(struct Qdisc *sch)
173b8970f0bSJohn Fastabend {
174b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
175b8970f0bSJohn Fastabend 	struct mqprio_sched *priv = qdisc_priv(sch);
17695dc1929SEric Dumazet 	struct Qdisc *qdisc, *old;
177b8970f0bSJohn Fastabend 	unsigned int ntx;
178b8970f0bSJohn Fastabend 
179b8970f0bSJohn Fastabend 	/* Attach underlying qdisc */
180b8970f0bSJohn Fastabend 	for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
181b8970f0bSJohn Fastabend 		qdisc = priv->qdiscs[ntx];
18295dc1929SEric Dumazet 		old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
18395dc1929SEric Dumazet 		if (old)
18495dc1929SEric Dumazet 			qdisc_destroy(old);
18595dc1929SEric Dumazet 		if (ntx < dev->real_num_tx_queues)
18649b49971SJiri Kosina 			qdisc_hash_add(qdisc, false);
187b8970f0bSJohn Fastabend 	}
188b8970f0bSJohn Fastabend 	kfree(priv->qdiscs);
189b8970f0bSJohn Fastabend 	priv->qdiscs = NULL;
190b8970f0bSJohn Fastabend }
191b8970f0bSJohn Fastabend 
192b8970f0bSJohn Fastabend static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch,
193b8970f0bSJohn Fastabend 					     unsigned long cl)
194b8970f0bSJohn Fastabend {
195b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
196b8970f0bSJohn Fastabend 	unsigned long ntx = cl - 1 - netdev_get_num_tc(dev);
197b8970f0bSJohn Fastabend 
198b8970f0bSJohn Fastabend 	if (ntx >= dev->num_tx_queues)
199b8970f0bSJohn Fastabend 		return NULL;
200b8970f0bSJohn Fastabend 	return netdev_get_tx_queue(dev, ntx);
201b8970f0bSJohn Fastabend }
202b8970f0bSJohn Fastabend 
203b8970f0bSJohn Fastabend static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
204b8970f0bSJohn Fastabend 		    struct Qdisc **old)
205b8970f0bSJohn Fastabend {
206b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
207b8970f0bSJohn Fastabend 	struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
208b8970f0bSJohn Fastabend 
209b8970f0bSJohn Fastabend 	if (!dev_queue)
210b8970f0bSJohn Fastabend 		return -EINVAL;
211b8970f0bSJohn Fastabend 
212b8970f0bSJohn Fastabend 	if (dev->flags & IFF_UP)
213b8970f0bSJohn Fastabend 		dev_deactivate(dev);
214b8970f0bSJohn Fastabend 
215b8970f0bSJohn Fastabend 	*old = dev_graft_qdisc(dev_queue, new);
216b8970f0bSJohn Fastabend 
2171abbe139SEric Dumazet 	if (new)
2184eaf3b84SEric Dumazet 		new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
2191abbe139SEric Dumazet 
220b8970f0bSJohn Fastabend 	if (dev->flags & IFF_UP)
221b8970f0bSJohn Fastabend 		dev_activate(dev);
222b8970f0bSJohn Fastabend 
223b8970f0bSJohn Fastabend 	return 0;
224b8970f0bSJohn Fastabend }
225b8970f0bSJohn Fastabend 
226b8970f0bSJohn Fastabend static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb)
227b8970f0bSJohn Fastabend {
228b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
229b8970f0bSJohn Fastabend 	struct mqprio_sched *priv = qdisc_priv(sch);
230b8970f0bSJohn Fastabend 	unsigned char *b = skb_tail_pointer(skb);
231144ce879SEric Dumazet 	struct tc_mqprio_qopt opt = { 0 };
232b8970f0bSJohn Fastabend 	struct Qdisc *qdisc;
233b8970f0bSJohn Fastabend 	unsigned int i;
234b8970f0bSJohn Fastabend 
235b8970f0bSJohn Fastabend 	sch->q.qlen = 0;
236b8970f0bSJohn Fastabend 	memset(&sch->bstats, 0, sizeof(sch->bstats));
237b8970f0bSJohn Fastabend 	memset(&sch->qstats, 0, sizeof(sch->qstats));
238b8970f0bSJohn Fastabend 
239b8970f0bSJohn Fastabend 	for (i = 0; i < dev->num_tx_queues; i++) {
24046e5da40SJohn Fastabend 		qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
241b8970f0bSJohn Fastabend 		spin_lock_bh(qdisc_lock(qdisc));
242b8970f0bSJohn Fastabend 		sch->q.qlen		+= qdisc->q.qlen;
243b8970f0bSJohn Fastabend 		sch->bstats.bytes	+= qdisc->bstats.bytes;
244b8970f0bSJohn Fastabend 		sch->bstats.packets	+= qdisc->bstats.packets;
245b8970f0bSJohn Fastabend 		sch->qstats.backlog	+= qdisc->qstats.backlog;
246b8970f0bSJohn Fastabend 		sch->qstats.drops	+= qdisc->qstats.drops;
247b8970f0bSJohn Fastabend 		sch->qstats.requeues	+= qdisc->qstats.requeues;
248b8970f0bSJohn Fastabend 		sch->qstats.overlimits	+= qdisc->qstats.overlimits;
249b8970f0bSJohn Fastabend 		spin_unlock_bh(qdisc_lock(qdisc));
250b8970f0bSJohn Fastabend 	}
251b8970f0bSJohn Fastabend 
252b8970f0bSJohn Fastabend 	opt.num_tc = netdev_get_num_tc(dev);
253b8970f0bSJohn Fastabend 	memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
2542026fecfSAlexander Duyck 	opt.hw = priv->hw_offload;
255b8970f0bSJohn Fastabend 
256b8970f0bSJohn Fastabend 	for (i = 0; i < netdev_get_num_tc(dev); i++) {
257b8970f0bSJohn Fastabend 		opt.count[i] = dev->tc_to_txq[i].count;
258b8970f0bSJohn Fastabend 		opt.offset[i] = dev->tc_to_txq[i].offset;
259b8970f0bSJohn Fastabend 	}
260b8970f0bSJohn Fastabend 
2611b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
2621b34ec43SDavid S. Miller 		goto nla_put_failure;
263b8970f0bSJohn Fastabend 
264b8970f0bSJohn Fastabend 	return skb->len;
265b8970f0bSJohn Fastabend nla_put_failure:
266b8970f0bSJohn Fastabend 	nlmsg_trim(skb, b);
267b8970f0bSJohn Fastabend 	return -1;
268b8970f0bSJohn Fastabend }
269b8970f0bSJohn Fastabend 
270b8970f0bSJohn Fastabend static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl)
271b8970f0bSJohn Fastabend {
272b8970f0bSJohn Fastabend 	struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
273b8970f0bSJohn Fastabend 
274b8970f0bSJohn Fastabend 	if (!dev_queue)
275b8970f0bSJohn Fastabend 		return NULL;
276b8970f0bSJohn Fastabend 
277b8970f0bSJohn Fastabend 	return dev_queue->qdisc_sleeping;
278b8970f0bSJohn Fastabend }
279b8970f0bSJohn Fastabend 
280*143976ceSWANG Cong static unsigned long mqprio_find(struct Qdisc *sch, u32 classid)
281b8970f0bSJohn Fastabend {
282b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
283b8970f0bSJohn Fastabend 	unsigned int ntx = TC_H_MIN(classid);
284b8970f0bSJohn Fastabend 
285b8970f0bSJohn Fastabend 	if (ntx > dev->num_tx_queues + netdev_get_num_tc(dev))
286b8970f0bSJohn Fastabend 		return 0;
287b8970f0bSJohn Fastabend 	return ntx;
288b8970f0bSJohn Fastabend }
289b8970f0bSJohn Fastabend 
290b8970f0bSJohn Fastabend static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl,
291b8970f0bSJohn Fastabend 			 struct sk_buff *skb, struct tcmsg *tcm)
292b8970f0bSJohn Fastabend {
293b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
294b8970f0bSJohn Fastabend 
295b8970f0bSJohn Fastabend 	if (cl <= netdev_get_num_tc(dev)) {
296b8970f0bSJohn Fastabend 		tcm->tcm_parent = TC_H_ROOT;
297b8970f0bSJohn Fastabend 		tcm->tcm_info = 0;
298b8970f0bSJohn Fastabend 	} else {
299b8970f0bSJohn Fastabend 		int i;
300b8970f0bSJohn Fastabend 		struct netdev_queue *dev_queue;
301b8970f0bSJohn Fastabend 
302b8970f0bSJohn Fastabend 		dev_queue = mqprio_queue_get(sch, cl);
303b8970f0bSJohn Fastabend 		tcm->tcm_parent = 0;
304b8970f0bSJohn Fastabend 		for (i = 0; i < netdev_get_num_tc(dev); i++) {
305b8970f0bSJohn Fastabend 			struct netdev_tc_txq tc = dev->tc_to_txq[i];
306b8970f0bSJohn Fastabend 			int q_idx = cl - netdev_get_num_tc(dev);
307b8970f0bSJohn Fastabend 
308b8970f0bSJohn Fastabend 			if (q_idx > tc.offset &&
309b8970f0bSJohn Fastabend 			    q_idx <= tc.offset + tc.count) {
310b8970f0bSJohn Fastabend 				tcm->tcm_parent =
311b8970f0bSJohn Fastabend 					TC_H_MAKE(TC_H_MAJ(sch->handle),
312b8970f0bSJohn Fastabend 						  TC_H_MIN(i + 1));
313b8970f0bSJohn Fastabend 				break;
314b8970f0bSJohn Fastabend 			}
315b8970f0bSJohn Fastabend 		}
316b8970f0bSJohn Fastabend 		tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
317b8970f0bSJohn Fastabend 	}
318b8970f0bSJohn Fastabend 	tcm->tcm_handle |= TC_H_MIN(cl);
319b8970f0bSJohn Fastabend 	return 0;
320b8970f0bSJohn Fastabend }
321b8970f0bSJohn Fastabend 
322b8970f0bSJohn Fastabend static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
323b8970f0bSJohn Fastabend 				   struct gnet_dump *d)
324ea18fd95Sstephen hemminger 	__releases(d->lock)
325ea18fd95Sstephen hemminger 	__acquires(d->lock)
326b8970f0bSJohn Fastabend {
327b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
328b8970f0bSJohn Fastabend 
329b8970f0bSJohn Fastabend 	if (cl <= netdev_get_num_tc(dev)) {
330b8970f0bSJohn Fastabend 		int i;
33164015853SJohn Fastabend 		__u32 qlen = 0;
332b8970f0bSJohn Fastabend 		struct Qdisc *qdisc;
333b8970f0bSJohn Fastabend 		struct gnet_stats_queue qstats = {0};
334b8970f0bSJohn Fastabend 		struct gnet_stats_basic_packed bstats = {0};
335b8970f0bSJohn Fastabend 		struct netdev_tc_txq tc = dev->tc_to_txq[cl - 1];
336b8970f0bSJohn Fastabend 
337b8970f0bSJohn Fastabend 		/* Drop lock here it will be reclaimed before touching
338b8970f0bSJohn Fastabend 		 * statistics this is required because the d->lock we
339b8970f0bSJohn Fastabend 		 * hold here is the look on dev_queue->qdisc_sleeping
340b8970f0bSJohn Fastabend 		 * also acquired below.
341b8970f0bSJohn Fastabend 		 */
342edb09eb1SEric Dumazet 		if (d->lock)
343b8970f0bSJohn Fastabend 			spin_unlock_bh(d->lock);
344b8970f0bSJohn Fastabend 
345b8970f0bSJohn Fastabend 		for (i = tc.offset; i < tc.offset + tc.count; i++) {
34646e5da40SJohn Fastabend 			struct netdev_queue *q = netdev_get_tx_queue(dev, i);
34746e5da40SJohn Fastabend 
34846e5da40SJohn Fastabend 			qdisc = rtnl_dereference(q->qdisc);
349b8970f0bSJohn Fastabend 			spin_lock_bh(qdisc_lock(qdisc));
35064015853SJohn Fastabend 			qlen		  += qdisc->q.qlen;
351b8970f0bSJohn Fastabend 			bstats.bytes      += qdisc->bstats.bytes;
352b8970f0bSJohn Fastabend 			bstats.packets    += qdisc->bstats.packets;
353b8970f0bSJohn Fastabend 			qstats.backlog    += qdisc->qstats.backlog;
354b8970f0bSJohn Fastabend 			qstats.drops      += qdisc->qstats.drops;
355b8970f0bSJohn Fastabend 			qstats.requeues   += qdisc->qstats.requeues;
356b8970f0bSJohn Fastabend 			qstats.overlimits += qdisc->qstats.overlimits;
357b8970f0bSJohn Fastabend 			spin_unlock_bh(qdisc_lock(qdisc));
358b8970f0bSJohn Fastabend 		}
359b8970f0bSJohn Fastabend 		/* Reclaim root sleeping lock before completing stats */
360edb09eb1SEric Dumazet 		if (d->lock)
361b8970f0bSJohn Fastabend 			spin_lock_bh(d->lock);
362edb09eb1SEric Dumazet 		if (gnet_stats_copy_basic(NULL, d, NULL, &bstats) < 0 ||
363b0ab6f92SJohn Fastabend 		    gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
364b8970f0bSJohn Fastabend 			return -1;
365b8970f0bSJohn Fastabend 	} else {
366b8970f0bSJohn Fastabend 		struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
367b8970f0bSJohn Fastabend 
368b8970f0bSJohn Fastabend 		sch = dev_queue->qdisc_sleeping;
369edb09eb1SEric Dumazet 		if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
370edb09eb1SEric Dumazet 					  d, NULL, &sch->bstats) < 0 ||
371b0ab6f92SJohn Fastabend 		    gnet_stats_copy_queue(d, NULL,
372b0ab6f92SJohn Fastabend 					  &sch->qstats, sch->q.qlen) < 0)
373b8970f0bSJohn Fastabend 			return -1;
374b8970f0bSJohn Fastabend 	}
375b8970f0bSJohn Fastabend 	return 0;
376b8970f0bSJohn Fastabend }
377b8970f0bSJohn Fastabend 
378b8970f0bSJohn Fastabend static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
379b8970f0bSJohn Fastabend {
380b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
381b8970f0bSJohn Fastabend 	unsigned long ntx;
382b8970f0bSJohn Fastabend 
383b8970f0bSJohn Fastabend 	if (arg->stop)
384b8970f0bSJohn Fastabend 		return;
385b8970f0bSJohn Fastabend 
386b8970f0bSJohn Fastabend 	/* Walk hierarchy with a virtual class per tc */
387b8970f0bSJohn Fastabend 	arg->count = arg->skip;
388b8970f0bSJohn Fastabend 	for (ntx = arg->skip;
389b8970f0bSJohn Fastabend 	     ntx < dev->num_tx_queues + netdev_get_num_tc(dev);
390b8970f0bSJohn Fastabend 	     ntx++) {
391b8970f0bSJohn Fastabend 		if (arg->fn(sch, ntx + 1, arg) < 0) {
392b8970f0bSJohn Fastabend 			arg->stop = 1;
393b8970f0bSJohn Fastabend 			break;
394b8970f0bSJohn Fastabend 		}
395b8970f0bSJohn Fastabend 		arg->count++;
396b8970f0bSJohn Fastabend 	}
397b8970f0bSJohn Fastabend }
398b8970f0bSJohn Fastabend 
399b8970f0bSJohn Fastabend static const struct Qdisc_class_ops mqprio_class_ops = {
400b8970f0bSJohn Fastabend 	.graft		= mqprio_graft,
401b8970f0bSJohn Fastabend 	.leaf		= mqprio_leaf,
402*143976ceSWANG Cong 	.find		= mqprio_find,
403b8970f0bSJohn Fastabend 	.walk		= mqprio_walk,
404b8970f0bSJohn Fastabend 	.dump		= mqprio_dump_class,
405b8970f0bSJohn Fastabend 	.dump_stats	= mqprio_dump_class_stats,
406b8970f0bSJohn Fastabend };
407b8970f0bSJohn Fastabend 
408ea18fd95Sstephen hemminger static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
409b8970f0bSJohn Fastabend 	.cl_ops		= &mqprio_class_ops,
410b8970f0bSJohn Fastabend 	.id		= "mqprio",
411b8970f0bSJohn Fastabend 	.priv_size	= sizeof(struct mqprio_sched),
412b8970f0bSJohn Fastabend 	.init		= mqprio_init,
413b8970f0bSJohn Fastabend 	.destroy	= mqprio_destroy,
414b8970f0bSJohn Fastabend 	.attach		= mqprio_attach,
415b8970f0bSJohn Fastabend 	.dump		= mqprio_dump,
416b8970f0bSJohn Fastabend 	.owner		= THIS_MODULE,
417b8970f0bSJohn Fastabend };
418b8970f0bSJohn Fastabend 
419b8970f0bSJohn Fastabend static int __init mqprio_module_init(void)
420b8970f0bSJohn Fastabend {
421b8970f0bSJohn Fastabend 	return register_qdisc(&mqprio_qdisc_ops);
422b8970f0bSJohn Fastabend }
423b8970f0bSJohn Fastabend 
424b8970f0bSJohn Fastabend static void __exit mqprio_module_exit(void)
425b8970f0bSJohn Fastabend {
426b8970f0bSJohn Fastabend 	unregister_qdisc(&mqprio_qdisc_ops);
427b8970f0bSJohn Fastabend }
428b8970f0bSJohn Fastabend 
429b8970f0bSJohn Fastabend module_init(mqprio_module_init);
430b8970f0bSJohn Fastabend module_exit(mqprio_module_exit);
431b8970f0bSJohn Fastabend 
432b8970f0bSJohn Fastabend MODULE_LICENSE("GPL");
433