John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * net/sched/sch_mqprio.c |
| 3 | * |
| 4 | * Copyright (c) 2010 John Fastabend <john.r.fastabend@intel.com> |
| 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/string.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/skbuff.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 18 | #include <net/netlink.h> |
| 19 | #include <net/pkt_sched.h> |
| 20 | #include <net/sch_generic.h> |
| 21 | |
| 22 | struct mqprio_sched { |
| 23 | struct Qdisc **qdiscs; |
| 24 | int hw_owned; |
| 25 | }; |
| 26 | |
| 27 | static void mqprio_destroy(struct Qdisc *sch) |
| 28 | { |
| 29 | struct net_device *dev = qdisc_dev(sch); |
| 30 | struct mqprio_sched *priv = qdisc_priv(sch); |
| 31 | unsigned int ntx; |
| 32 | |
Ben Hutchings | ac7100b | 2011-02-14 19:02:23 +0000 | [diff] [blame] | 33 | if (priv->qdiscs) { |
| 34 | for (ntx = 0; |
| 35 | ntx < dev->num_tx_queues && priv->qdiscs[ntx]; |
| 36 | ntx++) |
| 37 | qdisc_destroy(priv->qdiscs[ntx]); |
| 38 | kfree(priv->qdiscs); |
| 39 | } |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 40 | |
| 41 | if (priv->hw_owned && dev->netdev_ops->ndo_setup_tc) |
| 42 | dev->netdev_ops->ndo_setup_tc(dev, 0); |
| 43 | else |
| 44 | netdev_set_num_tc(dev, 0); |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt) |
| 48 | { |
| 49 | int i, j; |
| 50 | |
| 51 | /* Verify num_tc is not out of max range */ |
| 52 | if (qopt->num_tc > TC_MAX_QUEUE) |
| 53 | return -EINVAL; |
| 54 | |
| 55 | /* Verify priority mapping uses valid tcs */ |
| 56 | for (i = 0; i < TC_BITMASK + 1; i++) { |
| 57 | if (qopt->prio_tc_map[i] >= qopt->num_tc) |
| 58 | return -EINVAL; |
| 59 | } |
| 60 | |
| 61 | /* net_device does not support requested operation */ |
| 62 | if (qopt->hw && !dev->netdev_ops->ndo_setup_tc) |
| 63 | return -EINVAL; |
| 64 | |
| 65 | /* if hw owned qcount and qoffset are taken from LLD so |
| 66 | * no reason to verify them here |
| 67 | */ |
| 68 | if (qopt->hw) |
| 69 | return 0; |
| 70 | |
| 71 | for (i = 0; i < qopt->num_tc; i++) { |
| 72 | unsigned int last = qopt->offset[i] + qopt->count[i]; |
| 73 | |
| 74 | /* Verify the queue count is in tx range being equal to the |
| 75 | * real_num_tx_queues indicates the last queue is in use. |
| 76 | */ |
| 77 | if (qopt->offset[i] >= dev->real_num_tx_queues || |
| 78 | !qopt->count[i] || |
| 79 | last > dev->real_num_tx_queues) |
| 80 | return -EINVAL; |
| 81 | |
| 82 | /* Verify that the offset and counts do not overlap */ |
| 83 | for (j = i + 1; j < qopt->num_tc; j++) { |
| 84 | if (last > qopt->offset[j]) |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int mqprio_init(struct Qdisc *sch, struct nlattr *opt) |
| 93 | { |
| 94 | struct net_device *dev = qdisc_dev(sch); |
| 95 | struct mqprio_sched *priv = qdisc_priv(sch); |
| 96 | struct netdev_queue *dev_queue; |
| 97 | struct Qdisc *qdisc; |
| 98 | int i, err = -EOPNOTSUPP; |
| 99 | struct tc_mqprio_qopt *qopt = NULL; |
| 100 | |
| 101 | BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE); |
| 102 | BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK); |
| 103 | |
| 104 | if (sch->parent != TC_H_ROOT) |
| 105 | return -EOPNOTSUPP; |
| 106 | |
| 107 | if (!netif_is_multiqueue(dev)) |
| 108 | return -EOPNOTSUPP; |
| 109 | |
Thomas Graf | 7838f2c | 2011-12-22 02:05:07 +0000 | [diff] [blame] | 110 | if (!opt || nla_len(opt) < sizeof(*qopt)) |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 111 | return -EINVAL; |
| 112 | |
| 113 | qopt = nla_data(opt); |
| 114 | if (mqprio_parse_opt(dev, qopt)) |
| 115 | return -EINVAL; |
| 116 | |
| 117 | /* pre-allocate qdisc, attachment can't fail */ |
| 118 | priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]), |
| 119 | GFP_KERNEL); |
| 120 | if (priv->qdiscs == NULL) { |
| 121 | err = -ENOMEM; |
| 122 | goto err; |
| 123 | } |
| 124 | |
| 125 | for (i = 0; i < dev->num_tx_queues; i++) { |
| 126 | dev_queue = netdev_get_tx_queue(dev, i); |
| 127 | qdisc = qdisc_create_dflt(dev_queue, &pfifo_fast_ops, |
| 128 | TC_H_MAKE(TC_H_MAJ(sch->handle), |
| 129 | TC_H_MIN(i + 1))); |
| 130 | if (qdisc == NULL) { |
| 131 | err = -ENOMEM; |
| 132 | goto err; |
| 133 | } |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 134 | priv->qdiscs[i] = qdisc; |
| 135 | } |
| 136 | |
| 137 | /* If the mqprio options indicate that hardware should own |
| 138 | * the queue mapping then run ndo_setup_tc otherwise use the |
| 139 | * supplied and verified mapping |
| 140 | */ |
| 141 | if (qopt->hw) { |
| 142 | priv->hw_owned = 1; |
| 143 | err = dev->netdev_ops->ndo_setup_tc(dev, qopt->num_tc); |
| 144 | if (err) |
| 145 | goto err; |
| 146 | } else { |
| 147 | netdev_set_num_tc(dev, qopt->num_tc); |
| 148 | for (i = 0; i < qopt->num_tc; i++) |
| 149 | netdev_set_tc_queue(dev, i, |
| 150 | qopt->count[i], qopt->offset[i]); |
| 151 | } |
| 152 | |
| 153 | /* Always use supplied priority mappings */ |
| 154 | for (i = 0; i < TC_BITMASK + 1; i++) |
| 155 | netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]); |
| 156 | |
| 157 | sch->flags |= TCQ_F_MQROOT; |
| 158 | return 0; |
| 159 | |
| 160 | err: |
| 161 | mqprio_destroy(sch); |
| 162 | return err; |
| 163 | } |
| 164 | |
| 165 | static void mqprio_attach(struct Qdisc *sch) |
| 166 | { |
| 167 | struct net_device *dev = qdisc_dev(sch); |
| 168 | struct mqprio_sched *priv = qdisc_priv(sch); |
| 169 | struct Qdisc *qdisc; |
| 170 | unsigned int ntx; |
| 171 | |
| 172 | /* Attach underlying qdisc */ |
| 173 | for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { |
| 174 | qdisc = priv->qdiscs[ntx]; |
| 175 | qdisc = dev_graft_qdisc(qdisc->dev_queue, qdisc); |
| 176 | if (qdisc) |
| 177 | qdisc_destroy(qdisc); |
| 178 | } |
| 179 | kfree(priv->qdiscs); |
| 180 | priv->qdiscs = NULL; |
| 181 | } |
| 182 | |
| 183 | static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch, |
| 184 | unsigned long cl) |
| 185 | { |
| 186 | struct net_device *dev = qdisc_dev(sch); |
| 187 | unsigned long ntx = cl - 1 - netdev_get_num_tc(dev); |
| 188 | |
| 189 | if (ntx >= dev->num_tx_queues) |
| 190 | return NULL; |
| 191 | return netdev_get_tx_queue(dev, ntx); |
| 192 | } |
| 193 | |
| 194 | static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new, |
| 195 | struct Qdisc **old) |
| 196 | { |
| 197 | struct net_device *dev = qdisc_dev(sch); |
| 198 | struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); |
| 199 | |
| 200 | if (!dev_queue) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | if (dev->flags & IFF_UP) |
| 204 | dev_deactivate(dev); |
| 205 | |
| 206 | *old = dev_graft_qdisc(dev_queue, new); |
| 207 | |
| 208 | if (dev->flags & IFF_UP) |
| 209 | dev_activate(dev); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 215 | { |
| 216 | struct net_device *dev = qdisc_dev(sch); |
| 217 | struct mqprio_sched *priv = qdisc_priv(sch); |
| 218 | unsigned char *b = skb_tail_pointer(skb); |
Eric Dumazet | 144ce87 | 2011-01-26 07:21:57 +0000 | [diff] [blame] | 219 | struct tc_mqprio_qopt opt = { 0 }; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 220 | struct Qdisc *qdisc; |
| 221 | unsigned int i; |
| 222 | |
| 223 | sch->q.qlen = 0; |
| 224 | memset(&sch->bstats, 0, sizeof(sch->bstats)); |
| 225 | memset(&sch->qstats, 0, sizeof(sch->qstats)); |
| 226 | |
| 227 | for (i = 0; i < dev->num_tx_queues; i++) { |
| 228 | qdisc = netdev_get_tx_queue(dev, i)->qdisc; |
| 229 | spin_lock_bh(qdisc_lock(qdisc)); |
| 230 | sch->q.qlen += qdisc->q.qlen; |
| 231 | sch->bstats.bytes += qdisc->bstats.bytes; |
| 232 | sch->bstats.packets += qdisc->bstats.packets; |
| 233 | sch->qstats.qlen += qdisc->qstats.qlen; |
| 234 | sch->qstats.backlog += qdisc->qstats.backlog; |
| 235 | sch->qstats.drops += qdisc->qstats.drops; |
| 236 | sch->qstats.requeues += qdisc->qstats.requeues; |
| 237 | sch->qstats.overlimits += qdisc->qstats.overlimits; |
| 238 | spin_unlock_bh(qdisc_lock(qdisc)); |
| 239 | } |
| 240 | |
| 241 | opt.num_tc = netdev_get_num_tc(dev); |
| 242 | memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map)); |
| 243 | opt.hw = priv->hw_owned; |
| 244 | |
| 245 | for (i = 0; i < netdev_get_num_tc(dev); i++) { |
| 246 | opt.count[i] = dev->tc_to_txq[i].count; |
| 247 | opt.offset[i] = dev->tc_to_txq[i].offset; |
| 248 | } |
| 249 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 250 | if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt)) |
| 251 | goto nla_put_failure; |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 252 | |
| 253 | return skb->len; |
| 254 | nla_put_failure: |
| 255 | nlmsg_trim(skb, b); |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl) |
| 260 | { |
| 261 | struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); |
| 262 | |
| 263 | if (!dev_queue) |
| 264 | return NULL; |
| 265 | |
| 266 | return dev_queue->qdisc_sleeping; |
| 267 | } |
| 268 | |
| 269 | static unsigned long mqprio_get(struct Qdisc *sch, u32 classid) |
| 270 | { |
| 271 | struct net_device *dev = qdisc_dev(sch); |
| 272 | unsigned int ntx = TC_H_MIN(classid); |
| 273 | |
| 274 | if (ntx > dev->num_tx_queues + netdev_get_num_tc(dev)) |
| 275 | return 0; |
| 276 | return ntx; |
| 277 | } |
| 278 | |
| 279 | static void mqprio_put(struct Qdisc *sch, unsigned long cl) |
| 280 | { |
| 281 | } |
| 282 | |
| 283 | static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl, |
| 284 | struct sk_buff *skb, struct tcmsg *tcm) |
| 285 | { |
| 286 | struct net_device *dev = qdisc_dev(sch); |
| 287 | |
| 288 | if (cl <= netdev_get_num_tc(dev)) { |
| 289 | tcm->tcm_parent = TC_H_ROOT; |
| 290 | tcm->tcm_info = 0; |
| 291 | } else { |
| 292 | int i; |
| 293 | struct netdev_queue *dev_queue; |
| 294 | |
| 295 | dev_queue = mqprio_queue_get(sch, cl); |
| 296 | tcm->tcm_parent = 0; |
| 297 | for (i = 0; i < netdev_get_num_tc(dev); i++) { |
| 298 | struct netdev_tc_txq tc = dev->tc_to_txq[i]; |
| 299 | int q_idx = cl - netdev_get_num_tc(dev); |
| 300 | |
| 301 | if (q_idx > tc.offset && |
| 302 | q_idx <= tc.offset + tc.count) { |
| 303 | tcm->tcm_parent = |
| 304 | TC_H_MAKE(TC_H_MAJ(sch->handle), |
| 305 | TC_H_MIN(i + 1)); |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | tcm->tcm_info = dev_queue->qdisc_sleeping->handle; |
| 310 | } |
| 311 | tcm->tcm_handle |= TC_H_MIN(cl); |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl, |
stephen hemminger | ea18fd9 | 2011-02-23 09:06:51 +0000 | [diff] [blame] | 316 | struct gnet_dump *d) |
| 317 | __releases(d->lock) |
| 318 | __acquires(d->lock) |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 319 | { |
| 320 | struct net_device *dev = qdisc_dev(sch); |
| 321 | |
| 322 | if (cl <= netdev_get_num_tc(dev)) { |
| 323 | int i; |
| 324 | struct Qdisc *qdisc; |
| 325 | struct gnet_stats_queue qstats = {0}; |
| 326 | struct gnet_stats_basic_packed bstats = {0}; |
| 327 | struct netdev_tc_txq tc = dev->tc_to_txq[cl - 1]; |
| 328 | |
| 329 | /* Drop lock here it will be reclaimed before touching |
| 330 | * statistics this is required because the d->lock we |
| 331 | * hold here is the look on dev_queue->qdisc_sleeping |
| 332 | * also acquired below. |
| 333 | */ |
| 334 | spin_unlock_bh(d->lock); |
| 335 | |
| 336 | for (i = tc.offset; i < tc.offset + tc.count; i++) { |
| 337 | qdisc = netdev_get_tx_queue(dev, i)->qdisc; |
| 338 | spin_lock_bh(qdisc_lock(qdisc)); |
| 339 | bstats.bytes += qdisc->bstats.bytes; |
| 340 | bstats.packets += qdisc->bstats.packets; |
| 341 | qstats.qlen += qdisc->qstats.qlen; |
| 342 | qstats.backlog += qdisc->qstats.backlog; |
| 343 | qstats.drops += qdisc->qstats.drops; |
| 344 | qstats.requeues += qdisc->qstats.requeues; |
| 345 | qstats.overlimits += qdisc->qstats.overlimits; |
| 346 | spin_unlock_bh(qdisc_lock(qdisc)); |
| 347 | } |
| 348 | /* Reclaim root sleeping lock before completing stats */ |
| 349 | spin_lock_bh(d->lock); |
| 350 | if (gnet_stats_copy_basic(d, &bstats) < 0 || |
| 351 | gnet_stats_copy_queue(d, &qstats) < 0) |
| 352 | return -1; |
| 353 | } else { |
| 354 | struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl); |
| 355 | |
| 356 | sch = dev_queue->qdisc_sleeping; |
| 357 | sch->qstats.qlen = sch->q.qlen; |
| 358 | if (gnet_stats_copy_basic(d, &sch->bstats) < 0 || |
| 359 | gnet_stats_copy_queue(d, &sch->qstats) < 0) |
| 360 | return -1; |
| 361 | } |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg) |
| 366 | { |
| 367 | struct net_device *dev = qdisc_dev(sch); |
| 368 | unsigned long ntx; |
| 369 | |
| 370 | if (arg->stop) |
| 371 | return; |
| 372 | |
| 373 | /* Walk hierarchy with a virtual class per tc */ |
| 374 | arg->count = arg->skip; |
| 375 | for (ntx = arg->skip; |
| 376 | ntx < dev->num_tx_queues + netdev_get_num_tc(dev); |
| 377 | ntx++) { |
| 378 | if (arg->fn(sch, ntx + 1, arg) < 0) { |
| 379 | arg->stop = 1; |
| 380 | break; |
| 381 | } |
| 382 | arg->count++; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | static const struct Qdisc_class_ops mqprio_class_ops = { |
| 387 | .graft = mqprio_graft, |
| 388 | .leaf = mqprio_leaf, |
| 389 | .get = mqprio_get, |
| 390 | .put = mqprio_put, |
| 391 | .walk = mqprio_walk, |
| 392 | .dump = mqprio_dump_class, |
| 393 | .dump_stats = mqprio_dump_class_stats, |
| 394 | }; |
| 395 | |
stephen hemminger | ea18fd9 | 2011-02-23 09:06:51 +0000 | [diff] [blame] | 396 | static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = { |
John Fastabend | b8970f0 | 2011-01-17 08:06:09 +0000 | [diff] [blame] | 397 | .cl_ops = &mqprio_class_ops, |
| 398 | .id = "mqprio", |
| 399 | .priv_size = sizeof(struct mqprio_sched), |
| 400 | .init = mqprio_init, |
| 401 | .destroy = mqprio_destroy, |
| 402 | .attach = mqprio_attach, |
| 403 | .dump = mqprio_dump, |
| 404 | .owner = THIS_MODULE, |
| 405 | }; |
| 406 | |
| 407 | static int __init mqprio_module_init(void) |
| 408 | { |
| 409 | return register_qdisc(&mqprio_qdisc_ops); |
| 410 | } |
| 411 | |
| 412 | static void __exit mqprio_module_exit(void) |
| 413 | { |
| 414 | unregister_qdisc(&mqprio_qdisc_ops); |
| 415 | } |
| 416 | |
| 417 | module_init(mqprio_module_init); |
| 418 | module_exit(mqprio_module_exit); |
| 419 | |
| 420 | MODULE_LICENSE("GPL"); |