sch_mq.c 5.8 KB

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