blob: 6ccf6daa250344f7127c191ba1beee316d4b5cf8 [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>
Jakub Kicinskif971b132018-05-25 21:53:35 -070019#include <net/pkt_cls.h>
David S. Miller6ec1c692009-09-06 01:58:51 -070020#include <net/pkt_sched.h>
John Fastabendb01ac092017-12-07 09:57:20 -080021#include <net/sch_generic.h>
David S. Miller6ec1c692009-09-06 01:58:51 -070022
23struct mq_sched {
24 struct Qdisc **qdiscs;
25};
26
Jakub Kicinskif971b132018-05-25 21:53:35 -070027static int mq_offload(struct Qdisc *sch, enum tc_mq_command cmd)
28{
29 struct net_device *dev = qdisc_dev(sch);
30 struct tc_mq_qopt_offload opt = {
31 .command = cmd,
32 .handle = sch->handle,
33 };
34
35 if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
36 return -EOPNOTSUPP;
37
38 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_MQ, &opt);
39}
40
David S. Miller6ec1c692009-09-06 01:58:51 -070041static void mq_destroy(struct Qdisc *sch)
42{
43 struct net_device *dev = qdisc_dev(sch);
44 struct mq_sched *priv = qdisc_priv(sch);
45 unsigned int ntx;
46
Jakub Kicinskif971b132018-05-25 21:53:35 -070047 mq_offload(sch, TC_MQ_DESTROY);
48
David S. Miller6ec1c692009-09-06 01:58:51 -070049 if (!priv->qdiscs)
50 return;
51 for (ntx = 0; ntx < dev->num_tx_queues && priv->qdiscs[ntx]; ntx++)
52 qdisc_destroy(priv->qdiscs[ntx]);
53 kfree(priv->qdiscs);
54}
55
Alexander Aringe63d7df2017-12-20 12:35:13 -050056static int mq_init(struct Qdisc *sch, struct nlattr *opt,
57 struct netlink_ext_ack *extack)
David S. Miller6ec1c692009-09-06 01:58:51 -070058{
59 struct net_device *dev = qdisc_dev(sch);
60 struct mq_sched *priv = qdisc_priv(sch);
61 struct netdev_queue *dev_queue;
62 struct Qdisc *qdisc;
63 unsigned int ntx;
64
65 if (sch->parent != TC_H_ROOT)
66 return -EOPNOTSUPP;
67
68 if (!netif_is_multiqueue(dev))
69 return -EOPNOTSUPP;
70
71 /* pre-allocate qdiscs, attachment can't fail */
72 priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
73 GFP_KERNEL);
Eric Dumazet87b60cf2017-02-10 10:31:49 -080074 if (!priv->qdiscs)
David S. Miller6ec1c692009-09-06 01:58:51 -070075 return -ENOMEM;
76
77 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
78 dev_queue = netdev_get_tx_queue(dev, ntx);
Eric Dumazet1f27cde2016-03-02 08:21:43 -080079 qdisc = qdisc_create_dflt(dev_queue, get_default_qdisc_ops(dev, ntx),
David S. Miller6ec1c692009-09-06 01:58:51 -070080 TC_H_MAKE(TC_H_MAJ(sch->handle),
Alexander Aringa38a9882017-12-20 12:35:21 -050081 TC_H_MIN(ntx + 1)),
82 extack);
Eric Dumazet87b60cf2017-02-10 10:31:49 -080083 if (!qdisc)
84 return -ENOMEM;
David S. Miller6ec1c692009-09-06 01:58:51 -070085 priv->qdiscs[ntx] = qdisc;
Eric Dumazet4eaf3b82015-12-01 20:08:51 -080086 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
David S. Miller6ec1c692009-09-06 01:58:51 -070087 }
88
Patrick McHardy23bcf632009-09-09 18:11:23 -070089 sch->flags |= TCQ_F_MQROOT;
Jakub Kicinskif971b132018-05-25 21:53:35 -070090
91 mq_offload(sch, TC_MQ_CREATE);
David S. Miller6ec1c692009-09-06 01:58:51 -070092 return 0;
David S. Miller6ec1c692009-09-06 01:58:51 -070093}
94
95static void mq_attach(struct Qdisc *sch)
96{
97 struct net_device *dev = qdisc_dev(sch);
98 struct mq_sched *priv = qdisc_priv(sch);
Eric Dumazet95dc1922013-12-05 11:12:02 -080099 struct Qdisc *qdisc, *old;
David S. Miller6ec1c692009-09-06 01:58:51 -0700100 unsigned int ntx;
101
102 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
103 qdisc = priv->qdiscs[ntx];
Eric Dumazet95dc1922013-12-05 11:12:02 -0800104 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
105 if (old)
106 qdisc_destroy(old);
107#ifdef CONFIG_NET_SCHED
108 if (ntx < dev->real_num_tx_queues)
Jiri Kosina49b49972017-03-08 16:03:32 +0100109 qdisc_hash_add(qdisc, false);
Eric Dumazet95dc1922013-12-05 11:12:02 -0800110#endif
111
David S. Miller6ec1c692009-09-06 01:58:51 -0700112 }
113 kfree(priv->qdiscs);
114 priv->qdiscs = NULL;
115}
116
117static int mq_dump(struct Qdisc *sch, struct sk_buff *skb)
118{
119 struct net_device *dev = qdisc_dev(sch);
120 struct Qdisc *qdisc;
121 unsigned int ntx;
John Fastabendce679e82017-12-07 09:57:39 -0800122 __u32 qlen = 0;
David S. Miller6ec1c692009-09-06 01:58:51 -0700123
124 sch->q.qlen = 0;
125 memset(&sch->bstats, 0, sizeof(sch->bstats));
126 memset(&sch->qstats, 0, sizeof(sch->qstats));
127
John Fastabendce679e82017-12-07 09:57:39 -0800128 /* MQ supports lockless qdiscs. However, statistics accounting needs
129 * to account for all, none, or a mix of locked and unlocked child
130 * qdiscs. Percpu stats are added to counters in-band and locking
131 * qdisc totals are added at end.
132 */
David S. Miller6ec1c692009-09-06 01:58:51 -0700133 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
134 qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping;
135 spin_lock_bh(qdisc_lock(qdisc));
John Fastabendb01ac092017-12-07 09:57:20 -0800136
137 if (qdisc_is_percpu_stats(qdisc)) {
John Fastabendce679e82017-12-07 09:57:39 -0800138 qlen = qdisc_qlen_sum(qdisc);
139 __gnet_stats_copy_basic(NULL, &sch->bstats,
140 qdisc->cpu_bstats,
141 &qdisc->bstats);
142 __gnet_stats_copy_queue(&sch->qstats,
143 qdisc->cpu_qstats,
144 &qdisc->qstats, qlen);
145 } else {
146 sch->q.qlen += qdisc->q.qlen;
147 sch->bstats.bytes += qdisc->bstats.bytes;
148 sch->bstats.packets += qdisc->bstats.packets;
149 sch->qstats.backlog += qdisc->qstats.backlog;
150 sch->qstats.drops += qdisc->qstats.drops;
151 sch->qstats.requeues += qdisc->qstats.requeues;
152 sch->qstats.overlimits += qdisc->qstats.overlimits;
John Fastabendb01ac092017-12-07 09:57:20 -0800153 }
154
David S. Miller6ec1c692009-09-06 01:58:51 -0700155 spin_unlock_bh(qdisc_lock(qdisc));
156 }
John Fastabendce679e82017-12-07 09:57:39 -0800157
David S. Miller6ec1c692009-09-06 01:58:51 -0700158 return 0;
159}
160
161static struct netdev_queue *mq_queue_get(struct Qdisc *sch, unsigned long cl)
162{
163 struct net_device *dev = qdisc_dev(sch);
164 unsigned long ntx = cl - 1;
165
166 if (ntx >= dev->num_tx_queues)
167 return NULL;
168 return netdev_get_tx_queue(dev, ntx);
169}
170
Jarek Poplawski926e61b2009-09-15 02:53:07 -0700171static struct netdev_queue *mq_select_queue(struct Qdisc *sch,
172 struct tcmsg *tcm)
David S. Miller6ec1c692009-09-06 01:58:51 -0700173{
Jesus Sanchez-Palenciace8a75f2017-10-16 18:01:24 -0700174 return mq_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
David S. Miller6ec1c692009-09-06 01:58:51 -0700175}
176
177static int mq_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
Alexander Aring653d6fd2017-12-20 12:35:17 -0500178 struct Qdisc **old, struct netlink_ext_ack *extack)
David S. Miller6ec1c692009-09-06 01:58:51 -0700179{
180 struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
181 struct net_device *dev = qdisc_dev(sch);
182
183 if (dev->flags & IFF_UP)
184 dev_deactivate(dev);
185
186 *old = dev_graft_qdisc(dev_queue, new);
Eric Dumazet1abbe132012-12-11 15:54:33 +0000187 if (new)
Eric Dumazet4eaf3b82015-12-01 20:08:51 -0800188 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
David S. Miller6ec1c692009-09-06 01:58:51 -0700189 if (dev->flags & IFF_UP)
190 dev_activate(dev);
191 return 0;
192}
193
194static struct Qdisc *mq_leaf(struct Qdisc *sch, unsigned long cl)
195{
196 struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
197
198 return dev_queue->qdisc_sleeping;
199}
200
WANG Cong143976c2017-08-24 16:51:29 -0700201static unsigned long mq_find(struct Qdisc *sch, u32 classid)
David S. Miller6ec1c692009-09-06 01:58:51 -0700202{
203 unsigned int ntx = TC_H_MIN(classid);
204
205 if (!mq_queue_get(sch, ntx))
206 return 0;
207 return ntx;
208}
209
David S. Miller6ec1c692009-09-06 01:58:51 -0700210static int mq_dump_class(struct Qdisc *sch, unsigned long cl,
211 struct sk_buff *skb, struct tcmsg *tcm)
212{
213 struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
214
215 tcm->tcm_parent = TC_H_ROOT;
216 tcm->tcm_handle |= TC_H_MIN(cl);
217 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
218 return 0;
219}
220
221static int mq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
222 struct gnet_dump *d)
223{
224 struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
225
226 sch = dev_queue->qdisc_sleeping;
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700227 if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 ||
John Fastabendb0ab6f92014-09-28 11:54:24 -0700228 gnet_stats_copy_queue(d, NULL, &sch->qstats, sch->q.qlen) < 0)
David S. Miller6ec1c692009-09-06 01:58:51 -0700229 return -1;
230 return 0;
231}
232
233static void mq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
234{
235 struct net_device *dev = qdisc_dev(sch);
236 unsigned int ntx;
237
238 if (arg->stop)
239 return;
240
241 arg->count = arg->skip;
242 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
243 if (arg->fn(sch, ntx + 1, arg) < 0) {
244 arg->stop = 1;
245 break;
246 }
247 arg->count++;
248 }
249}
250
251static const struct Qdisc_class_ops mq_class_ops = {
252 .select_queue = mq_select_queue,
253 .graft = mq_graft,
254 .leaf = mq_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700255 .find = mq_find,
David S. Miller6ec1c692009-09-06 01:58:51 -0700256 .walk = mq_walk,
257 .dump = mq_dump_class,
258 .dump_stats = mq_dump_class_stats,
259};
260
261struct Qdisc_ops mq_qdisc_ops __read_mostly = {
262 .cl_ops = &mq_class_ops,
263 .id = "mq",
264 .priv_size = sizeof(struct mq_sched),
265 .init = mq_init,
266 .destroy = mq_destroy,
267 .attach = mq_attach,
268 .dump = mq_dump,
269 .owner = THIS_MODULE,
270};