David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 15 | #include <linux/string.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <net/netlink.h> |
Jakub Kicinski | f971b13 | 2018-05-25 21:53:35 -0700 | [diff] [blame] | 19 | #include <net/pkt_cls.h> |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 20 | #include <net/pkt_sched.h> |
John Fastabend | b01ac09 | 2017-12-07 09:57:20 -0800 | [diff] [blame] | 21 | #include <net/sch_generic.h> |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 22 | |
| 23 | struct mq_sched { |
| 24 | struct Qdisc **qdiscs; |
| 25 | }; |
| 26 | |
Jakub Kicinski | f971b13 | 2018-05-25 21:53:35 -0700 | [diff] [blame] | 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 | |
Jakub Kicinski | 58f8927 | 2018-11-07 17:33:36 -0800 | [diff] [blame] | 41 | static int mq_offload_stats(struct Qdisc *sch) |
Jakub Kicinski | 47c669a4 | 2018-05-25 21:53:37 -0700 | [diff] [blame] | 42 | { |
Jakub Kicinski | 47c669a4 | 2018-05-25 21:53:37 -0700 | [diff] [blame] | 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 | |
Jakub Kicinski | 58f8927 | 2018-11-07 17:33:36 -0800 | [diff] [blame] | 52 | return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_MQ, &opt); |
Jakub Kicinski | 47c669a4 | 2018-05-25 21:53:37 -0700 | [diff] [blame] | 53 | } |
| 54 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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 | |
Jakub Kicinski | f971b13 | 2018-05-25 21:53:35 -0700 | [diff] [blame] | 61 | mq_offload(sch, TC_MQ_DESTROY); |
| 62 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 63 | if (!priv->qdiscs) |
| 64 | return; |
| 65 | for (ntx = 0; ntx < dev->num_tx_queues && priv->qdiscs[ntx]; ntx++) |
Vlad Buslov | 86bd446 | 2018-09-24 19:22:50 +0300 | [diff] [blame] | 66 | qdisc_put(priv->qdiscs[ntx]); |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 67 | kfree(priv->qdiscs); |
| 68 | } |
| 69 | |
Alexander Aring | e63d7df | 2017-12-20 12:35:13 -0500 | [diff] [blame] | 70 | static int mq_init(struct Qdisc *sch, struct nlattr *opt, |
| 71 | struct netlink_ext_ack *extack) |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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); |
Eric Dumazet | 87b60cf | 2017-02-10 10:31:49 -0800 | [diff] [blame] | 88 | if (!priv->qdiscs) |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 89 | return -ENOMEM; |
| 90 | |
| 91 | for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { |
| 92 | dev_queue = netdev_get_tx_queue(dev, ntx); |
Eric Dumazet | 1f27cde | 2016-03-02 08:21:43 -0800 | [diff] [blame] | 93 | qdisc = qdisc_create_dflt(dev_queue, get_default_qdisc_ops(dev, ntx), |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 94 | TC_H_MAKE(TC_H_MAJ(sch->handle), |
Alexander Aring | a38a9882 | 2017-12-20 12:35:21 -0500 | [diff] [blame] | 95 | TC_H_MIN(ntx + 1)), |
| 96 | extack); |
Eric Dumazet | 87b60cf | 2017-02-10 10:31:49 -0800 | [diff] [blame] | 97 | if (!qdisc) |
| 98 | return -ENOMEM; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 99 | priv->qdiscs[ntx] = qdisc; |
Eric Dumazet | 4eaf3b8 | 2015-12-01 20:08:51 -0800 | [diff] [blame] | 100 | qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Patrick McHardy | 23bcf63 | 2009-09-09 18:11:23 -0700 | [diff] [blame] | 103 | sch->flags |= TCQ_F_MQROOT; |
Jakub Kicinski | f971b13 | 2018-05-25 21:53:35 -0700 | [diff] [blame] | 104 | |
| 105 | mq_offload(sch, TC_MQ_CREATE); |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 106 | return 0; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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); |
Eric Dumazet | 95dc192 | 2013-12-05 11:12:02 -0800 | [diff] [blame] | 113 | struct Qdisc *qdisc, *old; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 114 | unsigned int ntx; |
| 115 | |
| 116 | for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { |
| 117 | qdisc = priv->qdiscs[ntx]; |
Eric Dumazet | 95dc192 | 2013-12-05 11:12:02 -0800 | [diff] [blame] | 118 | old = dev_graft_qdisc(qdisc->dev_queue, qdisc); |
| 119 | if (old) |
Vlad Buslov | 86bd446 | 2018-09-24 19:22:50 +0300 | [diff] [blame] | 120 | qdisc_put(old); |
Eric Dumazet | 95dc192 | 2013-12-05 11:12:02 -0800 | [diff] [blame] | 121 | #ifdef CONFIG_NET_SCHED |
| 122 | if (ntx < dev->real_num_tx_queues) |
Jiri Kosina | 49b4997 | 2017-03-08 16:03:32 +0100 | [diff] [blame] | 123 | qdisc_hash_add(qdisc, false); |
Eric Dumazet | 95dc192 | 2013-12-05 11:12:02 -0800 | [diff] [blame] | 124 | #endif |
| 125 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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; |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 136 | __u32 qlen = 0; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 137 | |
| 138 | sch->q.qlen = 0; |
| 139 | memset(&sch->bstats, 0, sizeof(sch->bstats)); |
| 140 | memset(&sch->qstats, 0, sizeof(sch->qstats)); |
| 141 | |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 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 | */ |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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)); |
John Fastabend | b01ac09 | 2017-12-07 09:57:20 -0800 | [diff] [blame] | 150 | |
| 151 | if (qdisc_is_percpu_stats(qdisc)) { |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 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; |
Jakub Kicinski | 47c669a4 | 2018-05-25 21:53:37 -0700 | [diff] [blame] | 163 | sch->qstats.qlen += qdisc->qstats.qlen; |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 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; |
John Fastabend | b01ac09 | 2017-12-07 09:57:20 -0800 | [diff] [blame] | 168 | } |
| 169 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 170 | spin_unlock_bh(qdisc_lock(qdisc)); |
| 171 | } |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 172 | |
Jakub Kicinski | 58f8927 | 2018-11-07 17:33:36 -0800 | [diff] [blame] | 173 | return mq_offload_stats(sch); |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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 | |
Jarek Poplawski | 926e61b | 2009-09-15 02:53:07 -0700 | [diff] [blame] | 186 | static struct netdev_queue *mq_select_queue(struct Qdisc *sch, |
| 187 | struct tcmsg *tcm) |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 188 | { |
Jesus Sanchez-Palencia | ce8a75f | 2017-10-16 18:01:24 -0700 | [diff] [blame] | 189 | return mq_queue_get(sch, TC_H_MIN(tcm->tcm_parent)); |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static int mq_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new, |
Alexander Aring | 653d6fd | 2017-12-20 12:35:17 -0500 | [diff] [blame] | 193 | struct Qdisc **old, struct netlink_ext_ack *extack) |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 194 | { |
| 195 | struct netdev_queue *dev_queue = mq_queue_get(sch, cl); |
Jakub Kicinski | d577a3d | 2018-11-12 14:58:14 -0800 | [diff] [blame] | 196 | struct tc_mq_qopt_offload graft_offload; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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); |
Eric Dumazet | 1abbe13 | 2012-12-11 15:54:33 +0000 | [diff] [blame] | 203 | if (new) |
Eric Dumazet | 4eaf3b8 | 2015-12-01 20:08:51 -0800 | [diff] [blame] | 204 | new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 205 | if (dev->flags & IFF_UP) |
| 206 | dev_activate(dev); |
Jakub Kicinski | d577a3d | 2018-11-12 14:58:14 -0800 | [diff] [blame] | 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); |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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 | |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 225 | static unsigned long mq_find(struct Qdisc *sch, u32 classid) |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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; |
Eric Dumazet | edb09eb | 2016-06-06 09:37:16 -0700 | [diff] [blame] | 251 | if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 || |
John Fastabend | b0ab6f9 | 2014-09-28 11:54:24 -0700 | [diff] [blame] | 252 | gnet_stats_copy_queue(d, NULL, &sch->qstats, sch->q.qlen) < 0) |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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, |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 279 | .find = mq_find, |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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 | }; |