sch_mq.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/kernel.h>
  12. #include <linux/string.h>
  13. #include <linux/errno.h>
  14. #include <linux/skbuff.h>
  15. #include <net/netlink.h>
  16. #include <net/pkt_sched.h>
  17. struct mq_sched {
  18. struct Qdisc **qdiscs;
  19. };
  20. static void mq_destroy(struct Qdisc *sch)
  21. {
  22. struct net_device *dev = qdisc_dev(sch);
  23. struct mq_sched *priv = qdisc_priv(sch);
  24. unsigned int ntx;
  25. if (!priv->qdiscs)
  26. return;
  27. for (ntx = 0; ntx < dev->num_tx_queues && priv->qdiscs[ntx]; ntx++)
  28. qdisc_destroy(priv->qdiscs[ntx]);
  29. kfree(priv->qdiscs);
  30. }
  31. static int mq_init(struct Qdisc *sch, struct nlattr *opt)
  32. {
  33. struct net_device *dev = qdisc_dev(sch);
  34. struct mq_sched *priv = qdisc_priv(sch);
  35. struct netdev_queue *dev_queue;
  36. struct Qdisc *qdisc;
  37. unsigned int ntx;
  38. if (sch->parent != TC_H_ROOT)
  39. return -EOPNOTSUPP;
  40. if (!netif_is_multiqueue(dev))
  41. return -EOPNOTSUPP;
  42. /* pre-allocate qdiscs, attachment can't fail */
  43. priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
  44. GFP_KERNEL);
  45. if (priv->qdiscs == NULL)
  46. return -ENOMEM;
  47. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  48. dev_queue = netdev_get_tx_queue(dev, ntx);
  49. qdisc = qdisc_create_dflt(dev, dev_queue, &pfifo_fast_ops,
  50. TC_H_MAKE(TC_H_MAJ(sch->handle),
  51. TC_H_MIN(ntx + 1)));
  52. if (qdisc == NULL)
  53. goto err;
  54. qdisc->flags |= TCQ_F_CAN_BYPASS;
  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 unsigned int mq_select_queue(struct Qdisc *sch, struct tcmsg *tcm)
  110. {
  111. unsigned int ntx = TC_H_MIN(tcm->tcm_parent);
  112. if (!mq_queue_get(sch, ntx))
  113. return 0;
  114. return ntx - 1;
  115. }
  116. static int mq_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
  117. struct Qdisc **old)
  118. {
  119. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  120. struct net_device *dev = qdisc_dev(sch);
  121. if (dev->flags & IFF_UP)
  122. dev_deactivate(dev);
  123. *old = dev_graft_qdisc(dev_queue, new);
  124. if (dev->flags & IFF_UP)
  125. dev_activate(dev);
  126. return 0;
  127. }
  128. static struct Qdisc *mq_leaf(struct Qdisc *sch, unsigned long cl)
  129. {
  130. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  131. return dev_queue->qdisc_sleeping;
  132. }
  133. static unsigned long mq_get(struct Qdisc *sch, u32 classid)
  134. {
  135. unsigned int ntx = TC_H_MIN(classid);
  136. if (!mq_queue_get(sch, ntx))
  137. return 0;
  138. return ntx;
  139. }
  140. static void mq_put(struct Qdisc *sch, unsigned long cl)
  141. {
  142. return;
  143. }
  144. static int mq_dump_class(struct Qdisc *sch, unsigned long cl,
  145. struct sk_buff *skb, struct tcmsg *tcm)
  146. {
  147. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  148. tcm->tcm_parent = TC_H_ROOT;
  149. tcm->tcm_handle |= TC_H_MIN(cl);
  150. tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
  151. return 0;
  152. }
  153. static int mq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  154. struct gnet_dump *d)
  155. {
  156. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  157. sch = dev_queue->qdisc_sleeping;
  158. if (gnet_stats_copy_basic(d, &sch->bstats) < 0 ||
  159. gnet_stats_copy_queue(d, &sch->qstats) < 0)
  160. return -1;
  161. return 0;
  162. }
  163. static void mq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  164. {
  165. struct net_device *dev = qdisc_dev(sch);
  166. unsigned int ntx;
  167. if (arg->stop)
  168. return;
  169. arg->count = arg->skip;
  170. for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
  171. if (arg->fn(sch, ntx + 1, arg) < 0) {
  172. arg->stop = 1;
  173. break;
  174. }
  175. arg->count++;
  176. }
  177. }
  178. static const struct Qdisc_class_ops mq_class_ops = {
  179. .select_queue = mq_select_queue,
  180. .graft = mq_graft,
  181. .leaf = mq_leaf,
  182. .get = mq_get,
  183. .put = mq_put,
  184. .walk = mq_walk,
  185. .dump = mq_dump_class,
  186. .dump_stats = mq_dump_class_stats,
  187. };
  188. struct Qdisc_ops mq_qdisc_ops __read_mostly = {
  189. .cl_ops = &mq_class_ops,
  190. .id = "mq",
  191. .priv_size = sizeof(struct mq_sched),
  192. .init = mq_init,
  193. .destroy = mq_destroy,
  194. .attach = mq_attach,
  195. .dump = mq_dump,
  196. .owner = THIS_MODULE,
  197. };