blob: bc59f05e1a0f4024289b3a3d14c25b7a335a666e [file] [log] [blame]
David S. Miller6ec1c692009-09-06 01:58:51 -07001/*
2 * net/sched/sch_mq.c Classful multiqueue dummy scheduler
3 *
4 * Copyright (c) 2009 Patrick McHardy <kaber@trash.net>
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
David S. Miller6ec1c692009-09-06 01:58:51 -070013#include <linux/kernel.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040014#include <linux/export.h>
David S. Miller6ec1c692009-09-06 01:58:51 -070015#include <linux/string.h>
16#include <linux/errno.h>
17#include <linux/skbuff.h>
18#include <net/netlink.h>
19#include <net/pkt_sched.h>
John Fastabendb01ac092017-12-07 09:57:20 -080020#include <net/sch_generic.h>
David S. Miller6ec1c692009-09-06 01:58:51 -070021
22struct mq_sched {
23 struct Qdisc **qdiscs;
24};
25
26static void mq_destroy(struct Qdisc *sch)
27{
28 struct net_device *dev = qdisc_dev(sch);
29 struct mq_sched *priv = qdisc_priv(sch);
30 unsigned int ntx;
31
32 if (!priv->qdiscs)
33 return;
34 for (ntx = 0; ntx < dev->num_tx_queues && priv->qdiscs[ntx]; ntx++)
35 qdisc_destroy(priv->qdiscs[ntx]);
36 kfree(priv->qdiscs);
37}
38
39static int mq_init(struct Qdisc *sch, struct nlattr *opt)
40{
41 struct net_device *dev = qdisc_dev(sch);
42 struct mq_sched *priv = qdisc_priv(sch);
43 struct netdev_queue *dev_queue;
44 struct Qdisc *qdisc;
45 unsigned int ntx;
46
47 if (sch->parent != TC_H_ROOT)
48 return -EOPNOTSUPP;
49
50 if (!netif_is_multiqueue(dev))
51 return -EOPNOTSUPP;
52
53 /* pre-allocate qdiscs, attachment can't fail */
54 priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
55 GFP_KERNEL);
Eric Dumazet87b60cf2017-02-10 10:31:49 -080056 if (!priv->qdiscs)
David S. Miller6ec1c692009-09-06 01:58:51 -070057 return -ENOMEM;
58
59 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
60 dev_queue = netdev_get_tx_queue(dev, ntx);
Eric Dumazet1f27cde2016-03-02 08:21:43 -080061 qdisc = qdisc_create_dflt(dev_queue, get_default_qdisc_ops(dev, ntx),
David S. Miller6ec1c692009-09-06 01:58:51 -070062 TC_H_MAKE(TC_H_MAJ(sch->handle),
63 TC_H_MIN(ntx + 1)));
Eric Dumazet87b60cf2017-02-10 10:31:49 -080064 if (!qdisc)
65 return -ENOMEM;
David S. Miller6ec1c692009-09-06 01:58:51 -070066 priv->qdiscs[ntx] = qdisc;
Eric Dumazet4eaf3b82015-12-01 20:08:51 -080067 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
David S. Miller6ec1c692009-09-06 01:58:51 -070068 }
69
Patrick McHardy23bcf632009-09-09 18:11:23 -070070 sch->flags |= TCQ_F_MQROOT;
David S. Miller6ec1c692009-09-06 01:58:51 -070071 return 0;
David S. Miller6ec1c692009-09-06 01:58:51 -070072}
73
74static void mq_attach(struct Qdisc *sch)
75{
76 struct net_device *dev = qdisc_dev(sch);
77 struct mq_sched *priv = qdisc_priv(sch);
Eric Dumazet95dc1922013-12-05 11:12:02 -080078 struct Qdisc *qdisc, *old;
David S. Miller6ec1c692009-09-06 01:58:51 -070079 unsigned int ntx;
80
81 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
82 qdisc = priv->qdiscs[ntx];
Eric Dumazet95dc1922013-12-05 11:12:02 -080083 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
84 if (old)
85 qdisc_destroy(old);
86#ifdef CONFIG_NET_SCHED
87 if (ntx < dev->real_num_tx_queues)
Jiri Kosina49b49972017-03-08 16:03:32 +010088 qdisc_hash_add(qdisc, false);
Eric Dumazet95dc1922013-12-05 11:12:02 -080089#endif
90
David S. Miller6ec1c692009-09-06 01:58:51 -070091 }
92 kfree(priv->qdiscs);
93 priv->qdiscs = NULL;
94}
95
96static int mq_dump(struct Qdisc *sch, struct sk_buff *skb)
97{
98 struct net_device *dev = qdisc_dev(sch);
99 struct Qdisc *qdisc;
100 unsigned int ntx;
101
102 sch->q.qlen = 0;
103 memset(&sch->bstats, 0, sizeof(sch->bstats));
104 memset(&sch->qstats, 0, sizeof(sch->qstats));
105
106 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
John Fastabendb01ac092017-12-07 09:57:20 -0800107 struct gnet_stats_basic_cpu __percpu *cpu_bstats = NULL;
108 struct gnet_stats_queue __percpu *cpu_qstats = NULL;
109 __u32 qlen = 0;
110
David S. Miller6ec1c692009-09-06 01:58:51 -0700111 qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping;
112 spin_lock_bh(qdisc_lock(qdisc));
John Fastabendb01ac092017-12-07 09:57:20 -0800113
114 if (qdisc_is_percpu_stats(qdisc)) {
115 cpu_bstats = qdisc->cpu_bstats;
116 cpu_qstats = qdisc->cpu_qstats;
117 }
118
119 qlen = qdisc_qlen_sum(qdisc);
120
121 __gnet_stats_copy_basic(NULL, &sch->bstats,
122 cpu_bstats, &qdisc->bstats);
123 __gnet_stats_copy_queue(&sch->qstats,
124 cpu_qstats, &qdisc->qstats, qlen);
125
David S. Miller6ec1c692009-09-06 01:58:51 -0700126 spin_unlock_bh(qdisc_lock(qdisc));
127 }
128 return 0;
129}
130
131static struct netdev_queue *mq_queue_get(struct Qdisc *sch, unsigned long cl)
132{
133 struct net_device *dev = qdisc_dev(sch);
134 unsigned long ntx = cl - 1;
135
136 if (ntx >= dev->num_tx_queues)
137 return NULL;
138 return netdev_get_tx_queue(dev, ntx);
139}
140
Jarek Poplawski926e61b2009-09-15 02:53:07 -0700141static struct netdev_queue *mq_select_queue(struct Qdisc *sch,
142 struct tcmsg *tcm)
David S. Miller6ec1c692009-09-06 01:58:51 -0700143{
Jesus Sanchez-Palenciace8a75f2017-10-16 18:01:24 -0700144 return mq_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
David S. Miller6ec1c692009-09-06 01:58:51 -0700145}
146
147static int mq_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
148 struct Qdisc **old)
149{
150 struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
151 struct net_device *dev = qdisc_dev(sch);
152
153 if (dev->flags & IFF_UP)
154 dev_deactivate(dev);
155
156 *old = dev_graft_qdisc(dev_queue, new);
Eric Dumazet1abbe132012-12-11 15:54:33 +0000157 if (new)
Eric Dumazet4eaf3b82015-12-01 20:08:51 -0800158 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
David S. Miller6ec1c692009-09-06 01:58:51 -0700159 if (dev->flags & IFF_UP)
160 dev_activate(dev);
161 return 0;
162}
163
164static struct Qdisc *mq_leaf(struct Qdisc *sch, unsigned long cl)
165{
166 struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
167
168 return dev_queue->qdisc_sleeping;
169}
170
WANG Cong143976c2017-08-24 16:51:29 -0700171static unsigned long mq_find(struct Qdisc *sch, u32 classid)
David S. Miller6ec1c692009-09-06 01:58:51 -0700172{
173 unsigned int ntx = TC_H_MIN(classid);
174
175 if (!mq_queue_get(sch, ntx))
176 return 0;
177 return ntx;
178}
179
David S. Miller6ec1c692009-09-06 01:58:51 -0700180static int mq_dump_class(struct Qdisc *sch, unsigned long cl,
181 struct sk_buff *skb, struct tcmsg *tcm)
182{
183 struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
184
185 tcm->tcm_parent = TC_H_ROOT;
186 tcm->tcm_handle |= TC_H_MIN(cl);
187 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
188 return 0;
189}
190
191static int mq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
192 struct gnet_dump *d)
193{
194 struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
195
196 sch = dev_queue->qdisc_sleeping;
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700197 if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 ||
John Fastabendb0ab6f92014-09-28 11:54:24 -0700198 gnet_stats_copy_queue(d, NULL, &sch->qstats, sch->q.qlen) < 0)
David S. Miller6ec1c692009-09-06 01:58:51 -0700199 return -1;
200 return 0;
201}
202
203static void mq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
204{
205 struct net_device *dev = qdisc_dev(sch);
206 unsigned int ntx;
207
208 if (arg->stop)
209 return;
210
211 arg->count = arg->skip;
212 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
213 if (arg->fn(sch, ntx + 1, arg) < 0) {
214 arg->stop = 1;
215 break;
216 }
217 arg->count++;
218 }
219}
220
221static const struct Qdisc_class_ops mq_class_ops = {
222 .select_queue = mq_select_queue,
223 .graft = mq_graft,
224 .leaf = mq_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700225 .find = mq_find,
David S. Miller6ec1c692009-09-06 01:58:51 -0700226 .walk = mq_walk,
227 .dump = mq_dump_class,
228 .dump_stats = mq_dump_class_stats,
229};
230
231struct Qdisc_ops mq_qdisc_ops __read_mostly = {
232 .cl_ops = &mq_class_ops,
233 .id = "mq",
234 .priv_size = sizeof(struct mq_sched),
235 .init = mq_init,
236 .destroy = mq_destroy,
237 .attach = mq_attach,
238 .dump = mq_dump,
239 .owner = THIS_MODULE,
240};