sch_mq.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. return 0;
  58. err:
  59. mq_destroy(sch);
  60. return -ENOMEM;
  61. }
  62. static void mq_attach(struct Qdisc *sch)
  63. {
  64. struct net_device *dev = qdisc_dev(sch);
  65. struct mq_sched *priv = qdisc_priv(sch);
  66. struct Qdisc *qdisc;
  67. unsigned int ntx;
  68. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  69. qdisc = priv->qdiscs[ntx];
  70. qdisc = dev_graft_qdisc(qdisc->dev_queue, qdisc);
  71. if (qdisc)
  72. qdisc_destroy(qdisc);
  73. }
  74. kfree(priv->qdiscs);
  75. priv->qdiscs = NULL;
  76. }
  77. static int mq_dump(struct Qdisc *sch, struct sk_buff *skb)
  78. {
  79. struct net_device *dev = qdisc_dev(sch);
  80. struct Qdisc *qdisc;
  81. unsigned int ntx;
  82. sch->q.qlen = 0;
  83. memset(&sch->bstats, 0, sizeof(sch->bstats));
  84. memset(&sch->qstats, 0, sizeof(sch->qstats));
  85. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  86. qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping;
  87. spin_lock_bh(qdisc_lock(qdisc));
  88. sch->q.qlen += qdisc->q.qlen;
  89. sch->bstats.bytes += qdisc->bstats.bytes;
  90. sch->bstats.packets += qdisc->bstats.packets;
  91. sch->qstats.qlen += qdisc->qstats.qlen;
  92. sch->qstats.backlog += qdisc->qstats.backlog;
  93. sch->qstats.drops += qdisc->qstats.drops;
  94. sch->qstats.requeues += qdisc->qstats.requeues;
  95. sch->qstats.overlimits += qdisc->qstats.overlimits;
  96. spin_unlock_bh(qdisc_lock(qdisc));
  97. }
  98. return 0;
  99. }
  100. static struct netdev_queue *mq_queue_get(struct Qdisc *sch, unsigned long cl)
  101. {
  102. struct net_device *dev = qdisc_dev(sch);
  103. unsigned long ntx = cl - 1;
  104. if (ntx >= dev->num_tx_queues)
  105. return NULL;
  106. return netdev_get_tx_queue(dev, ntx);
  107. }
  108. static unsigned int mq_select_queue(struct Qdisc *sch, struct tcmsg *tcm)
  109. {
  110. unsigned int ntx = TC_H_MIN(tcm->tcm_parent);
  111. if (!mq_queue_get(sch, ntx))
  112. return 0;
  113. return ntx - 1;
  114. }
  115. static int mq_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
  116. struct Qdisc **old)
  117. {
  118. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  119. struct net_device *dev = qdisc_dev(sch);
  120. if (dev->flags & IFF_UP)
  121. dev_deactivate(dev);
  122. *old = dev_graft_qdisc(dev_queue, new);
  123. if (dev->flags & IFF_UP)
  124. dev_activate(dev);
  125. return 0;
  126. }
  127. static struct Qdisc *mq_leaf(struct Qdisc *sch, unsigned long cl)
  128. {
  129. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  130. return dev_queue->qdisc_sleeping;
  131. }
  132. static unsigned long mq_get(struct Qdisc *sch, u32 classid)
  133. {
  134. unsigned int ntx = TC_H_MIN(classid);
  135. if (!mq_queue_get(sch, ntx))
  136. return 0;
  137. return ntx;
  138. }
  139. static void mq_put(struct Qdisc *sch, unsigned long cl)
  140. {
  141. return;
  142. }
  143. static int mq_dump_class(struct Qdisc *sch, unsigned long cl,
  144. struct sk_buff *skb, struct tcmsg *tcm)
  145. {
  146. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  147. tcm->tcm_parent = TC_H_ROOT;
  148. tcm->tcm_handle |= TC_H_MIN(cl);
  149. tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
  150. return 0;
  151. }
  152. static int mq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  153. struct gnet_dump *d)
  154. {
  155. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  156. sch = dev_queue->qdisc_sleeping;
  157. if (gnet_stats_copy_basic(d, &sch->bstats) < 0 ||
  158. gnet_stats_copy_queue(d, &sch->qstats) < 0)
  159. return -1;
  160. return 0;
  161. }
  162. static void mq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  163. {
  164. struct net_device *dev = qdisc_dev(sch);
  165. unsigned int ntx;
  166. if (arg->stop)
  167. return;
  168. arg->count = arg->skip;
  169. for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
  170. if (arg->fn(sch, ntx + 1, arg) < 0) {
  171. arg->stop = 1;
  172. break;
  173. }
  174. arg->count++;
  175. }
  176. }
  177. static const struct Qdisc_class_ops mq_class_ops = {
  178. .select_queue = mq_select_queue,
  179. .graft = mq_graft,
  180. .leaf = mq_leaf,
  181. .get = mq_get,
  182. .put = mq_put,
  183. .walk = mq_walk,
  184. .dump = mq_dump_class,
  185. .dump_stats = mq_dump_class_stats,
  186. };
  187. struct Qdisc_ops mq_qdisc_ops __read_mostly = {
  188. .cl_ops = &mq_class_ops,
  189. .id = "mq",
  190. .priv_size = sizeof(struct mq_sched),
  191. .init = mq_init,
  192. .destroy = mq_destroy,
  193. .attach = mq_attach,
  194. .dump = mq_dump,
  195. .owner = THIS_MODULE,
  196. };