sch_mq.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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_cls.h>
  19. #include <net/pkt_sched.h>
  20. #include <net/sch_generic.h>
  21. struct mq_sched {
  22. struct Qdisc **qdiscs;
  23. };
  24. static int mq_offload(struct Qdisc *sch, enum tc_mq_command cmd)
  25. {
  26. struct net_device *dev = qdisc_dev(sch);
  27. struct tc_mq_qopt_offload opt = {
  28. .command = cmd,
  29. .handle = sch->handle,
  30. };
  31. if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
  32. return -EOPNOTSUPP;
  33. return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_MQ, &opt);
  34. }
  35. static void mq_offload_stats(struct Qdisc *sch)
  36. {
  37. struct net_device *dev = qdisc_dev(sch);
  38. struct tc_mq_qopt_offload opt = {
  39. .command = TC_MQ_STATS,
  40. .handle = sch->handle,
  41. .stats = {
  42. .bstats = &sch->bstats,
  43. .qstats = &sch->qstats,
  44. },
  45. };
  46. if (tc_can_offload(dev) && dev->netdev_ops->ndo_setup_tc)
  47. dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_MQ, &opt);
  48. }
  49. static void mq_destroy(struct Qdisc *sch)
  50. {
  51. struct net_device *dev = qdisc_dev(sch);
  52. struct mq_sched *priv = qdisc_priv(sch);
  53. unsigned int ntx;
  54. mq_offload(sch, TC_MQ_DESTROY);
  55. if (!priv->qdiscs)
  56. return;
  57. for (ntx = 0; ntx < dev->num_tx_queues && priv->qdiscs[ntx]; ntx++)
  58. qdisc_destroy(priv->qdiscs[ntx]);
  59. kfree(priv->qdiscs);
  60. }
  61. static int mq_init(struct Qdisc *sch, struct nlattr *opt,
  62. struct netlink_ext_ack *extack)
  63. {
  64. struct net_device *dev = qdisc_dev(sch);
  65. struct mq_sched *priv = qdisc_priv(sch);
  66. struct netdev_queue *dev_queue;
  67. struct Qdisc *qdisc;
  68. unsigned int ntx;
  69. if (sch->parent != TC_H_ROOT)
  70. return -EOPNOTSUPP;
  71. if (!netif_is_multiqueue(dev))
  72. return -EOPNOTSUPP;
  73. /* pre-allocate qdiscs, attachment can't fail */
  74. priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
  75. GFP_KERNEL);
  76. if (!priv->qdiscs)
  77. return -ENOMEM;
  78. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  79. dev_queue = netdev_get_tx_queue(dev, ntx);
  80. qdisc = qdisc_create_dflt(dev_queue, get_default_qdisc_ops(dev, ntx),
  81. TC_H_MAKE(TC_H_MAJ(sch->handle),
  82. TC_H_MIN(ntx + 1)),
  83. extack);
  84. if (!qdisc)
  85. return -ENOMEM;
  86. priv->qdiscs[ntx] = qdisc;
  87. qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
  88. }
  89. sch->flags |= TCQ_F_MQROOT;
  90. mq_offload(sch, TC_MQ_CREATE);
  91. return 0;
  92. }
  93. static void mq_attach(struct Qdisc *sch)
  94. {
  95. struct net_device *dev = qdisc_dev(sch);
  96. struct mq_sched *priv = qdisc_priv(sch);
  97. struct Qdisc *qdisc, *old;
  98. unsigned int ntx;
  99. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  100. qdisc = priv->qdiscs[ntx];
  101. old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
  102. if (old)
  103. qdisc_destroy(old);
  104. #ifdef CONFIG_NET_SCHED
  105. if (ntx < dev->real_num_tx_queues)
  106. qdisc_hash_add(qdisc, false);
  107. #endif
  108. }
  109. kfree(priv->qdiscs);
  110. priv->qdiscs = NULL;
  111. }
  112. static int mq_dump(struct Qdisc *sch, struct sk_buff *skb)
  113. {
  114. struct net_device *dev = qdisc_dev(sch);
  115. struct Qdisc *qdisc;
  116. unsigned int ntx;
  117. __u32 qlen = 0;
  118. sch->q.qlen = 0;
  119. memset(&sch->bstats, 0, sizeof(sch->bstats));
  120. memset(&sch->qstats, 0, sizeof(sch->qstats));
  121. /* MQ supports lockless qdiscs. However, statistics accounting needs
  122. * to account for all, none, or a mix of locked and unlocked child
  123. * qdiscs. Percpu stats are added to counters in-band and locking
  124. * qdisc totals are added at end.
  125. */
  126. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  127. qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping;
  128. spin_lock_bh(qdisc_lock(qdisc));
  129. if (qdisc_is_percpu_stats(qdisc)) {
  130. qlen = qdisc_qlen_sum(qdisc);
  131. __gnet_stats_copy_basic(NULL, &sch->bstats,
  132. qdisc->cpu_bstats,
  133. &qdisc->bstats);
  134. __gnet_stats_copy_queue(&sch->qstats,
  135. qdisc->cpu_qstats,
  136. &qdisc->qstats, qlen);
  137. sch->q.qlen += qlen;
  138. } else {
  139. sch->q.qlen += qdisc->q.qlen;
  140. sch->bstats.bytes += qdisc->bstats.bytes;
  141. sch->bstats.packets += qdisc->bstats.packets;
  142. sch->qstats.qlen += qdisc->qstats.qlen;
  143. sch->qstats.backlog += qdisc->qstats.backlog;
  144. sch->qstats.drops += qdisc->qstats.drops;
  145. sch->qstats.requeues += qdisc->qstats.requeues;
  146. sch->qstats.overlimits += qdisc->qstats.overlimits;
  147. }
  148. spin_unlock_bh(qdisc_lock(qdisc));
  149. }
  150. mq_offload_stats(sch);
  151. return 0;
  152. }
  153. static struct netdev_queue *mq_queue_get(struct Qdisc *sch, unsigned long cl)
  154. {
  155. struct net_device *dev = qdisc_dev(sch);
  156. unsigned long ntx = cl - 1;
  157. if (ntx >= dev->num_tx_queues)
  158. return NULL;
  159. return netdev_get_tx_queue(dev, ntx);
  160. }
  161. static struct netdev_queue *mq_select_queue(struct Qdisc *sch,
  162. struct tcmsg *tcm)
  163. {
  164. return mq_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
  165. }
  166. static int mq_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
  167. struct Qdisc **old, struct netlink_ext_ack *extack)
  168. {
  169. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  170. struct net_device *dev = qdisc_dev(sch);
  171. if (dev->flags & IFF_UP)
  172. dev_deactivate(dev);
  173. *old = dev_graft_qdisc(dev_queue, new);
  174. if (new)
  175. new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
  176. if (dev->flags & IFF_UP)
  177. dev_activate(dev);
  178. return 0;
  179. }
  180. static struct Qdisc *mq_leaf(struct Qdisc *sch, unsigned long cl)
  181. {
  182. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  183. return dev_queue->qdisc_sleeping;
  184. }
  185. static unsigned long mq_find(struct Qdisc *sch, u32 classid)
  186. {
  187. unsigned int ntx = TC_H_MIN(classid);
  188. if (!mq_queue_get(sch, ntx))
  189. return 0;
  190. return ntx;
  191. }
  192. static int mq_dump_class(struct Qdisc *sch, unsigned long cl,
  193. struct sk_buff *skb, struct tcmsg *tcm)
  194. {
  195. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  196. tcm->tcm_parent = TC_H_ROOT;
  197. tcm->tcm_handle |= TC_H_MIN(cl);
  198. tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
  199. return 0;
  200. }
  201. static int mq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  202. struct gnet_dump *d)
  203. {
  204. struct netdev_queue *dev_queue = mq_queue_get(sch, cl);
  205. sch = dev_queue->qdisc_sleeping;
  206. if (gnet_stats_copy_basic(&sch->running, d, sch->cpu_bstats,
  207. &sch->bstats) < 0 ||
  208. gnet_stats_copy_queue(d, NULL, &sch->qstats, sch->q.qlen) < 0)
  209. return -1;
  210. return 0;
  211. }
  212. static void mq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  213. {
  214. struct net_device *dev = qdisc_dev(sch);
  215. unsigned int ntx;
  216. if (arg->stop)
  217. return;
  218. arg->count = arg->skip;
  219. for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
  220. if (arg->fn(sch, ntx + 1, arg) < 0) {
  221. arg->stop = 1;
  222. break;
  223. }
  224. arg->count++;
  225. }
  226. }
  227. static const struct Qdisc_class_ops mq_class_ops = {
  228. .select_queue = mq_select_queue,
  229. .graft = mq_graft,
  230. .leaf = mq_leaf,
  231. .find = mq_find,
  232. .walk = mq_walk,
  233. .dump = mq_dump_class,
  234. .dump_stats = mq_dump_class_stats,
  235. };
  236. struct Qdisc_ops mq_qdisc_ops __read_mostly = {
  237. .cl_ops = &mq_class_ops,
  238. .id = "mq",
  239. .priv_size = sizeof(struct mq_sched),
  240. .init = mq_init,
  241. .destroy = mq_destroy,
  242. .attach = mq_attach,
  243. .dump = mq_dump,
  244. .owner = THIS_MODULE,
  245. };