sch_mq.c 5.6 KB

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