sch_mq.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.qlen += qdisc->qstats.qlen;
  99. sch->qstats.backlog += qdisc->qstats.backlog;
  100. sch->qstats.drops += qdisc->qstats.drops;
  101. sch->qstats.requeues += qdisc->qstats.requeues;
  102. sch->qstats.overlimits += qdisc->qstats.overlimits;
  103. spin_unlock_bh(qdisc_lock(qdisc));
  104. }
  105. return 0;
  106. }
  107. static struct netdev_queue *mq_queue_get(struct Qdisc *sch, unsigned long cl)
  108. {
  109. struct net_device *dev = qdisc_dev(sch);
  110. unsigned long ntx = cl - 1;
  111. if (ntx >= dev->num_tx_queues)
  112. return NULL;
  113. return netdev_get_tx_queue(dev, ntx);
  114. }
  115. static struct netdev_queue *mq_select_queue(struct Qdisc *sch,
  116. struct tcmsg *tcm)
  117. {
  118. unsigned int ntx = TC_H_MIN(tcm->tcm_parent);
  119. struct netdev_queue *dev_queue = mq_queue_get(sch, ntx);
  120. if (!dev_queue) {
  121. struct net_device *dev = qdisc_dev(sch);
  122. return netdev_get_tx_queue(dev, 0);
  123. }
  124. return dev_queue;
  125. }
  126. static int mq_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
  127. struct Qdisc **old)
  128. {
  129. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  130. struct net_device *dev = qdisc_dev(sch);
  131. if (dev->flags & IFF_UP)
  132. dev_deactivate(dev);
  133. *old = dev_graft_qdisc(dev_queue, new);
  134. if (new)
  135. new->flags |= TCQ_F_ONETXQUEUE;
  136. if (dev->flags & IFF_UP)
  137. dev_activate(dev);
  138. return 0;
  139. }
  140. static struct Qdisc *mq_leaf(struct Qdisc *sch, unsigned long cl)
  141. {
  142. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  143. return dev_queue->qdisc_sleeping;
  144. }
  145. static unsigned long mq_get(struct Qdisc *sch, u32 classid)
  146. {
  147. unsigned int ntx = TC_H_MIN(classid);
  148. if (!mq_queue_get(sch, ntx))
  149. return 0;
  150. return ntx;
  151. }
  152. static void mq_put(struct Qdisc *sch, unsigned long cl)
  153. {
  154. }
  155. static int mq_dump_class(struct Qdisc *sch, unsigned long cl,
  156. struct sk_buff *skb, struct tcmsg *tcm)
  157. {
  158. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  159. tcm->tcm_parent = TC_H_ROOT;
  160. tcm->tcm_handle |= TC_H_MIN(cl);
  161. tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
  162. return 0;
  163. }
  164. static int mq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  165. struct gnet_dump *d)
  166. {
  167. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  168. sch = dev_queue->qdisc_sleeping;
  169. sch->qstats.qlen = sch->q.qlen;
  170. if (gnet_stats_copy_basic(d, &sch->bstats) < 0 ||
  171. gnet_stats_copy_queue(d, &sch->qstats) < 0)
  172. return -1;
  173. return 0;
  174. }
  175. static void mq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  176. {
  177. struct net_device *dev = qdisc_dev(sch);
  178. unsigned int ntx;
  179. if (arg->stop)
  180. return;
  181. arg->count = arg->skip;
  182. for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
  183. if (arg->fn(sch, ntx + 1, arg) < 0) {
  184. arg->stop = 1;
  185. break;
  186. }
  187. arg->count++;
  188. }
  189. }
  190. static const struct Qdisc_class_ops mq_class_ops = {
  191. .select_queue = mq_select_queue,
  192. .graft = mq_graft,
  193. .leaf = mq_leaf,
  194. .get = mq_get,
  195. .put = mq_put,
  196. .walk = mq_walk,
  197. .dump = mq_dump_class,
  198. .dump_stats = mq_dump_class_stats,
  199. };
  200. struct Qdisc_ops mq_qdisc_ops __read_mostly = {
  201. .cl_ops = &mq_class_ops,
  202. .id = "mq",
  203. .priv_size = sizeof(struct mq_sched),
  204. .init = mq_init,
  205. .destroy = mq_destroy,
  206. .attach = mq_attach,
  207. .dump = mq_dump,
  208. .owner = THIS_MODULE,
  209. };