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