sch_mq.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include <linux/types.h>
  11. #include <linux/slab.h>
  12. #include <linux/kernel.h>
  13. #include <linux/export.h>
  14. #include <linux/string.h>
  15. #include <linux/errno.h>
  16. #include <linux/skbuff.h>
  17. #include <net/netlink.h>
  18. #include <net/pkt_sched.h>
  19. struct mq_sched {
  20. struct Qdisc **qdiscs;
  21. };
  22. static void mq_destroy(struct Qdisc *sch)
  23. {
  24. struct net_device *dev = qdisc_dev(sch);
  25. struct mq_sched *priv = qdisc_priv(sch);
  26. unsigned int ntx;
  27. if (!priv->qdiscs)
  28. return;
  29. for (ntx = 0; ntx < dev->num_tx_queues && priv->qdiscs[ntx]; ntx++)
  30. qdisc_destroy(priv->qdiscs[ntx]);
  31. kfree(priv->qdiscs);
  32. }
  33. static int mq_init(struct Qdisc *sch, struct nlattr *opt)
  34. {
  35. struct net_device *dev = qdisc_dev(sch);
  36. struct mq_sched *priv = qdisc_priv(sch);
  37. struct netdev_queue *dev_queue;
  38. struct Qdisc *qdisc;
  39. unsigned int ntx;
  40. if (sch->parent != TC_H_ROOT)
  41. return -EOPNOTSUPP;
  42. if (!netif_is_multiqueue(dev))
  43. return -EOPNOTSUPP;
  44. /* pre-allocate qdiscs, attachment can't fail */
  45. priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
  46. GFP_KERNEL);
  47. if (priv->qdiscs == NULL)
  48. return -ENOMEM;
  49. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  50. dev_queue = netdev_get_tx_queue(dev, ntx);
  51. qdisc = qdisc_create_dflt(dev_queue, &pfifo_fast_ops,
  52. TC_H_MAKE(TC_H_MAJ(sch->handle),
  53. TC_H_MIN(ntx + 1)));
  54. if (qdisc == NULL)
  55. goto err;
  56. priv->qdiscs[ntx] = qdisc;
  57. }
  58. sch->flags |= TCQ_F_MQROOT;
  59. return 0;
  60. err:
  61. mq_destroy(sch);
  62. return -ENOMEM;
  63. }
  64. static void mq_attach(struct Qdisc *sch)
  65. {
  66. struct net_device *dev = qdisc_dev(sch);
  67. struct mq_sched *priv = qdisc_priv(sch);
  68. struct Qdisc *qdisc;
  69. unsigned int ntx;
  70. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  71. qdisc = priv->qdiscs[ntx];
  72. qdisc = dev_graft_qdisc(qdisc->dev_queue, qdisc);
  73. if (qdisc)
  74. qdisc_destroy(qdisc);
  75. }
  76. kfree(priv->qdiscs);
  77. priv->qdiscs = NULL;
  78. }
  79. static int mq_dump(struct Qdisc *sch, struct sk_buff *skb)
  80. {
  81. struct net_device *dev = qdisc_dev(sch);
  82. struct Qdisc *qdisc;
  83. unsigned int ntx;
  84. sch->q.qlen = 0;
  85. memset(&sch->bstats, 0, sizeof(sch->bstats));
  86. memset(&sch->qstats, 0, sizeof(sch->qstats));
  87. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  88. qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping;
  89. spin_lock_bh(qdisc_lock(qdisc));
  90. sch->q.qlen += qdisc->q.qlen;
  91. sch->bstats.bytes += qdisc->bstats.bytes;
  92. sch->bstats.packets += qdisc->bstats.packets;
  93. sch->qstats.qlen += qdisc->qstats.qlen;
  94. sch->qstats.backlog += qdisc->qstats.backlog;
  95. sch->qstats.drops += qdisc->qstats.drops;
  96. sch->qstats.requeues += qdisc->qstats.requeues;
  97. sch->qstats.overlimits += qdisc->qstats.overlimits;
  98. spin_unlock_bh(qdisc_lock(qdisc));
  99. }
  100. return 0;
  101. }
  102. static struct netdev_queue *mq_queue_get(struct Qdisc *sch, unsigned long cl)
  103. {
  104. struct net_device *dev = qdisc_dev(sch);
  105. unsigned long ntx = cl - 1;
  106. if (ntx >= dev->num_tx_queues)
  107. return NULL;
  108. return netdev_get_tx_queue(dev, ntx);
  109. }
  110. static struct netdev_queue *mq_select_queue(struct Qdisc *sch,
  111. struct tcmsg *tcm)
  112. {
  113. unsigned int ntx = TC_H_MIN(tcm->tcm_parent);
  114. struct netdev_queue *dev_queue = mq_queue_get(sch, ntx);
  115. if (!dev_queue) {
  116. struct net_device *dev = qdisc_dev(sch);
  117. return netdev_get_tx_queue(dev, 0);
  118. }
  119. return dev_queue;
  120. }
  121. static int mq_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
  122. struct Qdisc **old)
  123. {
  124. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  125. struct net_device *dev = qdisc_dev(sch);
  126. if (dev->flags & IFF_UP)
  127. dev_deactivate(dev);
  128. *old = dev_graft_qdisc(dev_queue, new);
  129. if (dev->flags & IFF_UP)
  130. dev_activate(dev);
  131. return 0;
  132. }
  133. static struct Qdisc *mq_leaf(struct Qdisc *sch, unsigned long cl)
  134. {
  135. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  136. return dev_queue->qdisc_sleeping;
  137. }
  138. static unsigned long mq_get(struct Qdisc *sch, u32 classid)
  139. {
  140. unsigned int ntx = TC_H_MIN(classid);
  141. if (!mq_queue_get(sch, ntx))
  142. return 0;
  143. return ntx;
  144. }
  145. static void mq_put(struct Qdisc *sch, unsigned long cl)
  146. {
  147. }
  148. static int mq_dump_class(struct Qdisc *sch, unsigned long cl,
  149. struct sk_buff *skb, struct tcmsg *tcm)
  150. {
  151. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  152. tcm->tcm_parent = TC_H_ROOT;
  153. tcm->tcm_handle |= TC_H_MIN(cl);
  154. tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
  155. return 0;
  156. }
  157. static int mq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  158. struct gnet_dump *d)
  159. {
  160. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  161. sch = dev_queue->qdisc_sleeping;
  162. sch->qstats.qlen = sch->q.qlen;
  163. if (gnet_stats_copy_basic(d, &sch->bstats) < 0 ||
  164. gnet_stats_copy_queue(d, &sch->qstats) < 0)
  165. return -1;
  166. return 0;
  167. }
  168. static void mq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  169. {
  170. struct net_device *dev = qdisc_dev(sch);
  171. unsigned int ntx;
  172. if (arg->stop)
  173. return;
  174. arg->count = arg->skip;
  175. for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
  176. if (arg->fn(sch, ntx + 1, arg) < 0) {
  177. arg->stop = 1;
  178. break;
  179. }
  180. arg->count++;
  181. }
  182. }
  183. static const struct Qdisc_class_ops mq_class_ops = {
  184. .select_queue = mq_select_queue,
  185. .graft = mq_graft,
  186. .leaf = mq_leaf,
  187. .get = mq_get,
  188. .put = mq_put,
  189. .walk = mq_walk,
  190. .dump = mq_dump_class,
  191. .dump_stats = mq_dump_class_stats,
  192. };
  193. struct Qdisc_ops mq_qdisc_ops __read_mostly = {
  194. .cl_ops = &mq_class_ops,
  195. .id = "mq",
  196. .priv_size = sizeof(struct mq_sched),
  197. .init = mq_init,
  198. .destroy = mq_destroy,
  199. .attach = mq_attach,
  200. .dump = mq_dump,
  201. .owner = THIS_MODULE,
  202. };