]>
Commit | Line | Data |
---|---|---|
6ec1c69a DM |
1 | /* |
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> | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
6ec1c69a | 13 | #include <linux/kernel.h> |
bc3b2d7f | 14 | #include <linux/export.h> |
6ec1c69a DM |
15 | #include <linux/string.h> |
16 | #include <linux/errno.h> | |
17 | #include <linux/skbuff.h> | |
18 | #include <net/netlink.h> | |
f971b132 | 19 | #include <net/pkt_cls.h> |
6ec1c69a | 20 | #include <net/pkt_sched.h> |
b01ac095 | 21 | #include <net/sch_generic.h> |
6ec1c69a DM |
22 | |
23 | struct mq_sched { | |
24 | struct Qdisc **qdiscs; | |
25 | }; | |
26 | ||
f971b132 JK |
27 | static 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 | ||
58f89273 | 41 | static int mq_offload_stats(struct Qdisc *sch) |
47c669a4 | 42 | { |
47c669a4 JK |
43 | struct tc_mq_qopt_offload opt = { |
44 | .command = TC_MQ_STATS, | |
45 | .handle = sch->handle, | |
46 | .stats = { | |
47 | .bstats = &sch->bstats, | |
48 | .qstats = &sch->qstats, | |
49 | }, | |
50 | }; | |
51 | ||
58f89273 | 52 | return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_MQ, &opt); |
47c669a4 JK |
53 | } |
54 | ||
6ec1c69a DM |
55 | static void mq_destroy(struct Qdisc *sch) |
56 | { | |
57 | struct net_device *dev = qdisc_dev(sch); | |
58 | struct mq_sched *priv = qdisc_priv(sch); | |
59 | unsigned int ntx; | |
60 | ||
f971b132 JK |
61 | mq_offload(sch, TC_MQ_DESTROY); |
62 | ||
6ec1c69a DM |
63 | if (!priv->qdiscs) |
64 | return; | |
65 | for (ntx = 0; ntx < dev->num_tx_queues && priv->qdiscs[ntx]; ntx++) | |
86bd446b | 66 | qdisc_put(priv->qdiscs[ntx]); |
6ec1c69a DM |
67 | kfree(priv->qdiscs); |
68 | } | |
69 | ||
e63d7dfd AA |
70 | static int mq_init(struct Qdisc *sch, struct nlattr *opt, |
71 | struct netlink_ext_ack *extack) | |
6ec1c69a DM |
72 | { |
73 | struct net_device *dev = qdisc_dev(sch); | |
74 | struct mq_sched *priv = qdisc_priv(sch); | |
75 | struct netdev_queue *dev_queue; | |
76 | struct Qdisc *qdisc; | |
77 | unsigned int ntx; | |
78 | ||
79 | if (sch->parent != TC_H_ROOT) | |
80 | return -EOPNOTSUPP; | |
81 | ||
82 | if (!netif_is_multiqueue(dev)) | |
83 | return -EOPNOTSUPP; | |
84 | ||
85 | /* pre-allocate qdiscs, attachment can't fail */ | |
86 | priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]), | |
87 | GFP_KERNEL); | |
87b60cfa | 88 | if (!priv->qdiscs) |
6ec1c69a DM |
89 | return -ENOMEM; |
90 | ||
91 | for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { | |
92 | dev_queue = netdev_get_tx_queue(dev, ntx); | |
1f27cde3 | 93 | qdisc = qdisc_create_dflt(dev_queue, get_default_qdisc_ops(dev, ntx), |
6ec1c69a | 94 | TC_H_MAKE(TC_H_MAJ(sch->handle), |
a38a9882 AA |
95 | TC_H_MIN(ntx + 1)), |
96 | extack); | |
87b60cfa ED |
97 | if (!qdisc) |
98 | return -ENOMEM; | |
6ec1c69a | 99 | priv->qdiscs[ntx] = qdisc; |
4eaf3b84 | 100 | qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; |
6ec1c69a DM |
101 | } |
102 | ||
23bcf634 | 103 | sch->flags |= TCQ_F_MQROOT; |
f971b132 JK |
104 | |
105 | mq_offload(sch, TC_MQ_CREATE); | |
6ec1c69a | 106 | return 0; |
6ec1c69a DM |
107 | } |
108 | ||
109 | static void mq_attach(struct Qdisc *sch) | |
110 | { | |
111 | struct net_device *dev = qdisc_dev(sch); | |
112 | struct mq_sched *priv = qdisc_priv(sch); | |
95dc1929 | 113 | struct Qdisc *qdisc, *old; |
6ec1c69a DM |
114 | unsigned int ntx; |
115 | ||
116 | for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { | |
117 | qdisc = priv->qdiscs[ntx]; | |
95dc1929 ED |
118 | old = dev_graft_qdisc(qdisc->dev_queue, qdisc); |
119 | if (old) | |
86bd446b | 120 | qdisc_put(old); |
95dc1929 ED |
121 | #ifdef CONFIG_NET_SCHED |
122 | if (ntx < dev->real_num_tx_queues) | |
49b49971 | 123 | qdisc_hash_add(qdisc, false); |
95dc1929 ED |
124 | #endif |
125 | ||
6ec1c69a DM |
126 | } |
127 | kfree(priv->qdiscs); | |
128 | priv->qdiscs = NULL; | |
129 | } | |
130 | ||
131 | static int mq_dump(struct Qdisc *sch, struct sk_buff *skb) | |
132 | { | |
133 | struct net_device *dev = qdisc_dev(sch); | |
134 | struct Qdisc *qdisc; | |
135 | unsigned int ntx; | |
ce679e8d | 136 | __u32 qlen = 0; |
6ec1c69a DM |
137 | |
138 | sch->q.qlen = 0; | |
139 | memset(&sch->bstats, 0, sizeof(sch->bstats)); | |
140 | memset(&sch->qstats, 0, sizeof(sch->qstats)); | |
141 | ||
ce679e8d JF |
142 | /* MQ supports lockless qdiscs. However, statistics accounting needs |
143 | * to account for all, none, or a mix of locked and unlocked child | |
144 | * qdiscs. Percpu stats are added to counters in-band and locking | |
145 | * qdisc totals are added at end. | |
146 | */ | |
6ec1c69a DM |
147 | for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { |
148 | qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping; | |
149 | spin_lock_bh(qdisc_lock(qdisc)); | |
b01ac095 JF |
150 | |
151 | if (qdisc_is_percpu_stats(qdisc)) { | |
ce679e8d JF |
152 | qlen = qdisc_qlen_sum(qdisc); |
153 | __gnet_stats_copy_basic(NULL, &sch->bstats, | |
154 | qdisc->cpu_bstats, | |
155 | &qdisc->bstats); | |
156 | __gnet_stats_copy_queue(&sch->qstats, | |
157 | qdisc->cpu_qstats, | |
158 | &qdisc->qstats, qlen); | |
159 | } else { | |
160 | sch->q.qlen += qdisc->q.qlen; | |
161 | sch->bstats.bytes += qdisc->bstats.bytes; | |
162 | sch->bstats.packets += qdisc->bstats.packets; | |
47c669a4 | 163 | sch->qstats.qlen += qdisc->qstats.qlen; |
ce679e8d JF |
164 | sch->qstats.backlog += qdisc->qstats.backlog; |
165 | sch->qstats.drops += qdisc->qstats.drops; | |
166 | sch->qstats.requeues += qdisc->qstats.requeues; | |
167 | sch->qstats.overlimits += qdisc->qstats.overlimits; | |
b01ac095 JF |
168 | } |
169 | ||
6ec1c69a DM |
170 | spin_unlock_bh(qdisc_lock(qdisc)); |
171 | } | |
ce679e8d | 172 | |
58f89273 | 173 | return mq_offload_stats(sch); |
6ec1c69a DM |
174 | } |
175 | ||
176 | static struct netdev_queue *mq_queue_get(struct Qdisc *sch, unsigned long cl) | |
177 | { | |
178 | struct net_device *dev = qdisc_dev(sch); | |
179 | unsigned long ntx = cl - 1; | |
180 | ||
181 | if (ntx >= dev->num_tx_queues) | |
182 | return NULL; | |
183 | return netdev_get_tx_queue(dev, ntx); | |
184 | } | |
185 | ||
926e61b7 JP |
186 | static struct netdev_queue *mq_select_queue(struct Qdisc *sch, |
187 | struct tcmsg *tcm) | |
6ec1c69a | 188 | { |
ce8a75f6 | 189 | return mq_queue_get(sch, TC_H_MIN(tcm->tcm_parent)); |
6ec1c69a DM |
190 | } |
191 | ||
192 | static int mq_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new, | |
653d6fd6 | 193 | struct Qdisc **old, struct netlink_ext_ack *extack) |
6ec1c69a DM |
194 | { |
195 | struct netdev_queue *dev_queue = mq_queue_get(sch, cl); | |
d577a3d2 | 196 | struct tc_mq_qopt_offload graft_offload; |
6ec1c69a DM |
197 | struct net_device *dev = qdisc_dev(sch); |
198 | ||
199 | if (dev->flags & IFF_UP) | |
200 | dev_deactivate(dev); | |
201 | ||
202 | *old = dev_graft_qdisc(dev_queue, new); | |
1abbe139 | 203 | if (new) |
4eaf3b84 | 204 | new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; |
6ec1c69a DM |
205 | if (dev->flags & IFF_UP) |
206 | dev_activate(dev); | |
d577a3d2 JK |
207 | |
208 | graft_offload.handle = sch->handle; | |
209 | graft_offload.graft_params.queue = cl - 1; | |
210 | graft_offload.graft_params.child_handle = new ? new->handle : 0; | |
211 | graft_offload.command = TC_MQ_GRAFT; | |
212 | ||
213 | qdisc_offload_graft_helper(qdisc_dev(sch), sch, new, *old, | |
214 | TC_SETUP_QDISC_MQ, &graft_offload, extack); | |
6ec1c69a DM |
215 | return 0; |
216 | } | |
217 | ||
218 | static struct Qdisc *mq_leaf(struct Qdisc *sch, unsigned long cl) | |
219 | { | |
220 | struct netdev_queue *dev_queue = mq_queue_get(sch, cl); | |
221 | ||
222 | return dev_queue->qdisc_sleeping; | |
223 | } | |
224 | ||
143976ce | 225 | static unsigned long mq_find(struct Qdisc *sch, u32 classid) |
6ec1c69a DM |
226 | { |
227 | unsigned int ntx = TC_H_MIN(classid); | |
228 | ||
229 | if (!mq_queue_get(sch, ntx)) | |
230 | return 0; | |
231 | return ntx; | |
232 | } | |
233 | ||
6ec1c69a DM |
234 | static int mq_dump_class(struct Qdisc *sch, unsigned long cl, |
235 | struct sk_buff *skb, struct tcmsg *tcm) | |
236 | { | |
237 | struct netdev_queue *dev_queue = mq_queue_get(sch, cl); | |
238 | ||
239 | tcm->tcm_parent = TC_H_ROOT; | |
240 | tcm->tcm_handle |= TC_H_MIN(cl); | |
241 | tcm->tcm_info = dev_queue->qdisc_sleeping->handle; | |
242 | return 0; | |
243 | } | |
244 | ||
245 | static int mq_dump_class_stats(struct Qdisc *sch, unsigned long cl, | |
246 | struct gnet_dump *d) | |
247 | { | |
248 | struct netdev_queue *dev_queue = mq_queue_get(sch, cl); | |
249 | ||
250 | sch = dev_queue->qdisc_sleeping; | |
edb09eb1 | 251 | if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 || |
5dd431b6 | 252 | qdisc_qstats_copy(d, sch) < 0) |
6ec1c69a DM |
253 | return -1; |
254 | return 0; | |
255 | } | |
256 | ||
257 | static void mq_walk(struct Qdisc *sch, struct qdisc_walker *arg) | |
258 | { | |
259 | struct net_device *dev = qdisc_dev(sch); | |
260 | unsigned int ntx; | |
261 | ||
262 | if (arg->stop) | |
263 | return; | |
264 | ||
265 | arg->count = arg->skip; | |
266 | for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) { | |
267 | if (arg->fn(sch, ntx + 1, arg) < 0) { | |
268 | arg->stop = 1; | |
269 | break; | |
270 | } | |
271 | arg->count++; | |
272 | } | |
273 | } | |
274 | ||
275 | static const struct Qdisc_class_ops mq_class_ops = { | |
276 | .select_queue = mq_select_queue, | |
277 | .graft = mq_graft, | |
278 | .leaf = mq_leaf, | |
143976ce | 279 | .find = mq_find, |
6ec1c69a DM |
280 | .walk = mq_walk, |
281 | .dump = mq_dump_class, | |
282 | .dump_stats = mq_dump_class_stats, | |
283 | }; | |
284 | ||
285 | struct Qdisc_ops mq_qdisc_ops __read_mostly = { | |
286 | .cl_ops = &mq_class_ops, | |
287 | .id = "mq", | |
288 | .priv_size = sizeof(struct mq_sched), | |
289 | .init = mq_init, | |
290 | .destroy = mq_destroy, | |
291 | .attach = mq_attach, | |
292 | .dump = mq_dump, | |
293 | .owner = THIS_MODULE, | |
294 | }; |