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 | 47c669a4 | 2018-05-25 21:53:37 -0700 | [diff] [blame] | 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 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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 | |
Jakub Kicinski | f971b13 | 2018-05-25 21:53:35 -0700 | [diff] [blame] | 63 | mq_offload(sch, TC_MQ_DESTROY); |
| 64 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 65 | if (!priv->qdiscs) |
| 66 | return; |
| 67 | for (ntx = 0; ntx < dev->num_tx_queues && priv->qdiscs[ntx]; ntx++) |
| 68 | qdisc_destroy(priv->qdiscs[ntx]); |
| 69 | kfree(priv->qdiscs); |
| 70 | } |
| 71 | |
Alexander Aring | e63d7df | 2017-12-20 12:35:13 -0500 | [diff] [blame] | 72 | static int mq_init(struct Qdisc *sch, struct nlattr *opt, |
| 73 | struct netlink_ext_ack *extack) |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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); |
Eric Dumazet | 87b60cf | 2017-02-10 10:31:49 -0800 | [diff] [blame] | 90 | if (!priv->qdiscs) |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 91 | return -ENOMEM; |
| 92 | |
| 93 | for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { |
| 94 | dev_queue = netdev_get_tx_queue(dev, ntx); |
Eric Dumazet | 1f27cde | 2016-03-02 08:21:43 -0800 | [diff] [blame] | 95 | 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] | 96 | TC_H_MAKE(TC_H_MAJ(sch->handle), |
Alexander Aring | a38a988 | 2017-12-20 12:35:21 -0500 | [diff] [blame] | 97 | TC_H_MIN(ntx + 1)), |
| 98 | extack); |
Eric Dumazet | 87b60cf | 2017-02-10 10:31:49 -0800 | [diff] [blame] | 99 | if (!qdisc) |
| 100 | return -ENOMEM; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 101 | priv->qdiscs[ntx] = qdisc; |
Eric Dumazet | 4eaf3b8 | 2015-12-01 20:08:51 -0800 | [diff] [blame] | 102 | qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Patrick McHardy | 23bcf63 | 2009-09-09 18:11:23 -0700 | [diff] [blame] | 105 | sch->flags |= TCQ_F_MQROOT; |
Jakub Kicinski | f971b13 | 2018-05-25 21:53:35 -0700 | [diff] [blame] | 106 | |
| 107 | mq_offload(sch, TC_MQ_CREATE); |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 108 | return 0; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 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); |
Eric Dumazet | 95dc192 | 2013-12-05 11:12:02 -0800 | [diff] [blame] | 115 | struct Qdisc *qdisc, *old; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 116 | unsigned int ntx; |
| 117 | |
| 118 | for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { |
| 119 | qdisc = priv->qdiscs[ntx]; |
Eric Dumazet | 95dc192 | 2013-12-05 11:12:02 -0800 | [diff] [blame] | 120 | old = dev_graft_qdisc(qdisc->dev_queue, qdisc); |
| 121 | if (old) |
| 122 | qdisc_destroy(old); |
| 123 | #ifdef CONFIG_NET_SCHED |
| 124 | if (ntx < dev->real_num_tx_queues) |
Jiri Kosina | 49b4997 | 2017-03-08 16:03:32 +0100 | [diff] [blame] | 125 | qdisc_hash_add(qdisc, false); |
Eric Dumazet | 95dc192 | 2013-12-05 11:12:02 -0800 | [diff] [blame] | 126 | #endif |
| 127 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 128 | } |
| 129 | kfree(priv->qdiscs); |
| 130 | priv->qdiscs = NULL; |
| 131 | } |
| 132 | |
| 133 | static int mq_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 134 | { |
| 135 | struct net_device *dev = qdisc_dev(sch); |
| 136 | struct Qdisc *qdisc; |
| 137 | unsigned int ntx; |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 138 | __u32 qlen = 0; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 139 | |
| 140 | sch->q.qlen = 0; |
| 141 | memset(&sch->bstats, 0, sizeof(sch->bstats)); |
| 142 | memset(&sch->qstats, 0, sizeof(sch->qstats)); |
| 143 | |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 144 | /* MQ supports lockless qdiscs. However, statistics accounting needs |
| 145 | * to account for all, none, or a mix of locked and unlocked child |
| 146 | * qdiscs. Percpu stats are added to counters in-band and locking |
| 147 | * qdisc totals are added at end. |
| 148 | */ |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 149 | for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { |
| 150 | qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping; |
| 151 | spin_lock_bh(qdisc_lock(qdisc)); |
John Fastabend | b01ac09 | 2017-12-07 09:57:20 -0800 | [diff] [blame] | 152 | |
| 153 | if (qdisc_is_percpu_stats(qdisc)) { |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 154 | qlen = qdisc_qlen_sum(qdisc); |
| 155 | __gnet_stats_copy_basic(NULL, &sch->bstats, |
| 156 | qdisc->cpu_bstats, |
| 157 | &qdisc->bstats); |
| 158 | __gnet_stats_copy_queue(&sch->qstats, |
| 159 | qdisc->cpu_qstats, |
| 160 | &qdisc->qstats, qlen); |
Dust Li | 0c5a4dd | 2019-12-03 11:17:40 +0800 | [diff] [blame] | 161 | sch->q.qlen += qlen; |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 162 | } else { |
| 163 | sch->q.qlen += qdisc->q.qlen; |
| 164 | sch->bstats.bytes += qdisc->bstats.bytes; |
| 165 | sch->bstats.packets += qdisc->bstats.packets; |
Jakub Kicinski | 47c669a4 | 2018-05-25 21:53:37 -0700 | [diff] [blame] | 166 | sch->qstats.qlen += qdisc->qstats.qlen; |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 167 | sch->qstats.backlog += qdisc->qstats.backlog; |
| 168 | sch->qstats.drops += qdisc->qstats.drops; |
| 169 | sch->qstats.requeues += qdisc->qstats.requeues; |
| 170 | sch->qstats.overlimits += qdisc->qstats.overlimits; |
John Fastabend | b01ac09 | 2017-12-07 09:57:20 -0800 | [diff] [blame] | 171 | } |
| 172 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 173 | spin_unlock_bh(qdisc_lock(qdisc)); |
| 174 | } |
Jakub Kicinski | 47c669a4 | 2018-05-25 21:53:37 -0700 | [diff] [blame] | 175 | mq_offload_stats(sch); |
John Fastabend | ce679e8 | 2017-12-07 09:57:39 -0800 | [diff] [blame] | 176 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static struct netdev_queue *mq_queue_get(struct Qdisc *sch, unsigned long cl) |
| 181 | { |
| 182 | struct net_device *dev = qdisc_dev(sch); |
| 183 | unsigned long ntx = cl - 1; |
| 184 | |
| 185 | if (ntx >= dev->num_tx_queues) |
| 186 | return NULL; |
| 187 | return netdev_get_tx_queue(dev, ntx); |
| 188 | } |
| 189 | |
Jarek Poplawski | 926e61b | 2009-09-15 02:53:07 -0700 | [diff] [blame] | 190 | static struct netdev_queue *mq_select_queue(struct Qdisc *sch, |
| 191 | struct tcmsg *tcm) |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 192 | { |
Jesus Sanchez-Palencia | ce8a75f | 2017-10-16 18:01:24 -0700 | [diff] [blame] | 193 | return mq_queue_get(sch, TC_H_MIN(tcm->tcm_parent)); |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | 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] | 197 | struct Qdisc **old, struct netlink_ext_ack *extack) |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 198 | { |
| 199 | struct netdev_queue *dev_queue = mq_queue_get(sch, cl); |
| 200 | struct net_device *dev = qdisc_dev(sch); |
| 201 | |
| 202 | if (dev->flags & IFF_UP) |
| 203 | dev_deactivate(dev); |
| 204 | |
| 205 | *old = dev_graft_qdisc(dev_queue, new); |
Eric Dumazet | 1abbe13 | 2012-12-11 15:54:33 +0000 | [diff] [blame] | 206 | if (new) |
Eric Dumazet | 4eaf3b8 | 2015-12-01 20:08:51 -0800 | [diff] [blame] | 207 | new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 208 | if (dev->flags & IFF_UP) |
| 209 | dev_activate(dev); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static struct Qdisc *mq_leaf(struct Qdisc *sch, unsigned long cl) |
| 214 | { |
| 215 | struct netdev_queue *dev_queue = mq_queue_get(sch, cl); |
| 216 | |
| 217 | return dev_queue->qdisc_sleeping; |
| 218 | } |
| 219 | |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 220 | static unsigned long mq_find(struct Qdisc *sch, u32 classid) |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 221 | { |
| 222 | unsigned int ntx = TC_H_MIN(classid); |
| 223 | |
| 224 | if (!mq_queue_get(sch, ntx)) |
| 225 | return 0; |
| 226 | return ntx; |
| 227 | } |
| 228 | |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 229 | static int mq_dump_class(struct Qdisc *sch, unsigned long cl, |
| 230 | struct sk_buff *skb, struct tcmsg *tcm) |
| 231 | { |
| 232 | struct netdev_queue *dev_queue = mq_queue_get(sch, cl); |
| 233 | |
| 234 | tcm->tcm_parent = TC_H_ROOT; |
| 235 | tcm->tcm_handle |= TC_H_MIN(cl); |
| 236 | tcm->tcm_info = dev_queue->qdisc_sleeping->handle; |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static int mq_dump_class_stats(struct Qdisc *sch, unsigned long cl, |
| 241 | struct gnet_dump *d) |
| 242 | { |
| 243 | struct netdev_queue *dev_queue = mq_queue_get(sch, cl); |
| 244 | |
| 245 | sch = dev_queue->qdisc_sleeping; |
Dust Li | 50ee7a4 | 2019-11-28 14:29:09 +0800 | [diff] [blame] | 246 | if (gnet_stats_copy_basic(&sch->running, d, sch->cpu_bstats, |
| 247 | &sch->bstats) < 0 || |
John Fastabend | b0ab6f9 | 2014-09-28 11:54:24 -0700 | [diff] [blame] | 248 | 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] | 249 | return -1; |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static void mq_walk(struct Qdisc *sch, struct qdisc_walker *arg) |
| 254 | { |
| 255 | struct net_device *dev = qdisc_dev(sch); |
| 256 | unsigned int ntx; |
| 257 | |
| 258 | if (arg->stop) |
| 259 | return; |
| 260 | |
| 261 | arg->count = arg->skip; |
| 262 | for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) { |
| 263 | if (arg->fn(sch, ntx + 1, arg) < 0) { |
| 264 | arg->stop = 1; |
| 265 | break; |
| 266 | } |
| 267 | arg->count++; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static const struct Qdisc_class_ops mq_class_ops = { |
| 272 | .select_queue = mq_select_queue, |
| 273 | .graft = mq_graft, |
| 274 | .leaf = mq_leaf, |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 275 | .find = mq_find, |
David S. Miller | 6ec1c69 | 2009-09-06 01:58:51 -0700 | [diff] [blame] | 276 | .walk = mq_walk, |
| 277 | .dump = mq_dump_class, |
| 278 | .dump_stats = mq_dump_class_stats, |
| 279 | }; |
| 280 | |
| 281 | struct Qdisc_ops mq_qdisc_ops __read_mostly = { |
| 282 | .cl_ops = &mq_class_ops, |
| 283 | .id = "mq", |
| 284 | .priv_size = sizeof(struct mq_sched), |
| 285 | .init = mq_init, |
| 286 | .destroy = mq_destroy, |
| 287 | .attach = mq_attach, |
| 288 | .dump = mq_dump, |
| 289 | .owner = THIS_MODULE, |
| 290 | }; |