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