sch_mqprio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * net/sched/sch_mqprio.c
  3. *
  4. * Copyright (c) 2010 John Fastabend <john.r.fastabend@intel.com>
  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/string.h>
  14. #include <linux/errno.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/module.h>
  17. #include <net/netlink.h>
  18. #include <net/pkt_sched.h>
  19. #include <net/sch_generic.h>
  20. struct mqprio_sched {
  21. struct Qdisc **qdiscs;
  22. int hw_offload;
  23. };
  24. static void mqprio_destroy(struct Qdisc *sch)
  25. {
  26. struct net_device *dev = qdisc_dev(sch);
  27. struct mqprio_sched *priv = qdisc_priv(sch);
  28. unsigned int ntx;
  29. if (priv->qdiscs) {
  30. for (ntx = 0;
  31. ntx < dev->num_tx_queues && priv->qdiscs[ntx];
  32. ntx++)
  33. qdisc_destroy(priv->qdiscs[ntx]);
  34. kfree(priv->qdiscs);
  35. }
  36. if (priv->hw_offload && dev->netdev_ops->ndo_setup_tc) {
  37. struct tc_mqprio_qopt mqprio = {};
  38. dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_MQPRIO, &mqprio);
  39. } else {
  40. netdev_set_num_tc(dev, 0);
  41. }
  42. }
  43. static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt)
  44. {
  45. int i, j;
  46. /* Verify num_tc is not out of max range */
  47. if (qopt->num_tc > TC_MAX_QUEUE)
  48. return -EINVAL;
  49. /* Verify priority mapping uses valid tcs */
  50. for (i = 0; i < TC_BITMASK + 1; i++) {
  51. if (qopt->prio_tc_map[i] >= qopt->num_tc)
  52. return -EINVAL;
  53. }
  54. /* Limit qopt->hw to maximum supported offload value. Drivers have
  55. * the option of overriding this later if they don't support the a
  56. * given offload type.
  57. */
  58. if (qopt->hw > TC_MQPRIO_HW_OFFLOAD_MAX)
  59. qopt->hw = TC_MQPRIO_HW_OFFLOAD_MAX;
  60. /* If hardware offload is requested we will leave it to the device
  61. * to either populate the queue counts itself or to validate the
  62. * provided queue counts. If ndo_setup_tc is not present then
  63. * hardware doesn't support offload and we should return an error.
  64. */
  65. if (qopt->hw)
  66. return dev->netdev_ops->ndo_setup_tc ? 0 : -EINVAL;
  67. for (i = 0; i < qopt->num_tc; i++) {
  68. unsigned int last = qopt->offset[i] + qopt->count[i];
  69. /* Verify the queue count is in tx range being equal to the
  70. * real_num_tx_queues indicates the last queue is in use.
  71. */
  72. if (qopt->offset[i] >= dev->real_num_tx_queues ||
  73. !qopt->count[i] ||
  74. last > dev->real_num_tx_queues)
  75. return -EINVAL;
  76. /* Verify that the offset and counts do not overlap */
  77. for (j = i + 1; j < qopt->num_tc; j++) {
  78. if (last > qopt->offset[j])
  79. return -EINVAL;
  80. }
  81. }
  82. return 0;
  83. }
  84. static int mqprio_init(struct Qdisc *sch, struct nlattr *opt)
  85. {
  86. struct net_device *dev = qdisc_dev(sch);
  87. struct mqprio_sched *priv = qdisc_priv(sch);
  88. struct netdev_queue *dev_queue;
  89. struct Qdisc *qdisc;
  90. int i, err = -EOPNOTSUPP;
  91. struct tc_mqprio_qopt *qopt = NULL;
  92. BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE);
  93. BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK);
  94. if (sch->parent != TC_H_ROOT)
  95. return -EOPNOTSUPP;
  96. if (!netif_is_multiqueue(dev))
  97. return -EOPNOTSUPP;
  98. if (!opt || nla_len(opt) < sizeof(*qopt))
  99. return -EINVAL;
  100. qopt = nla_data(opt);
  101. if (mqprio_parse_opt(dev, qopt))
  102. return -EINVAL;
  103. /* pre-allocate qdisc, attachment can't fail */
  104. priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
  105. GFP_KERNEL);
  106. if (!priv->qdiscs)
  107. return -ENOMEM;
  108. for (i = 0; i < dev->num_tx_queues; i++) {
  109. dev_queue = netdev_get_tx_queue(dev, i);
  110. qdisc = qdisc_create_dflt(dev_queue,
  111. get_default_qdisc_ops(dev, i),
  112. TC_H_MAKE(TC_H_MAJ(sch->handle),
  113. TC_H_MIN(i + 1)));
  114. if (!qdisc)
  115. return -ENOMEM;
  116. priv->qdiscs[i] = qdisc;
  117. qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
  118. }
  119. /* If the mqprio options indicate that hardware should own
  120. * the queue mapping then run ndo_setup_tc otherwise use the
  121. * supplied and verified mapping
  122. */
  123. if (qopt->hw) {
  124. struct tc_mqprio_qopt mqprio = *qopt;
  125. err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_MQPRIO,
  126. &mqprio);
  127. if (err)
  128. return err;
  129. priv->hw_offload = mqprio.hw;
  130. } else {
  131. netdev_set_num_tc(dev, qopt->num_tc);
  132. for (i = 0; i < qopt->num_tc; i++)
  133. netdev_set_tc_queue(dev, i,
  134. qopt->count[i], qopt->offset[i]);
  135. }
  136. /* Always use supplied priority mappings */
  137. for (i = 0; i < TC_BITMASK + 1; i++)
  138. netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]);
  139. sch->flags |= TCQ_F_MQROOT;
  140. return 0;
  141. }
  142. static void mqprio_attach(struct Qdisc *sch)
  143. {
  144. struct net_device *dev = qdisc_dev(sch);
  145. struct mqprio_sched *priv = qdisc_priv(sch);
  146. struct Qdisc *qdisc, *old;
  147. unsigned int ntx;
  148. /* Attach underlying qdisc */
  149. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  150. qdisc = priv->qdiscs[ntx];
  151. old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
  152. if (old)
  153. qdisc_destroy(old);
  154. if (ntx < dev->real_num_tx_queues)
  155. qdisc_hash_add(qdisc, false);
  156. }
  157. kfree(priv->qdiscs);
  158. priv->qdiscs = NULL;
  159. }
  160. static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch,
  161. unsigned long cl)
  162. {
  163. struct net_device *dev = qdisc_dev(sch);
  164. unsigned long ntx = cl - 1 - netdev_get_num_tc(dev);
  165. if (ntx >= dev->num_tx_queues)
  166. return NULL;
  167. return netdev_get_tx_queue(dev, ntx);
  168. }
  169. static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
  170. struct Qdisc **old)
  171. {
  172. struct net_device *dev = qdisc_dev(sch);
  173. struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
  174. if (!dev_queue)
  175. return -EINVAL;
  176. if (dev->flags & IFF_UP)
  177. dev_deactivate(dev);
  178. *old = dev_graft_qdisc(dev_queue, new);
  179. if (new)
  180. new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
  181. if (dev->flags & IFF_UP)
  182. dev_activate(dev);
  183. return 0;
  184. }
  185. static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb)
  186. {
  187. struct net_device *dev = qdisc_dev(sch);
  188. struct mqprio_sched *priv = qdisc_priv(sch);
  189. unsigned char *b = skb_tail_pointer(skb);
  190. struct tc_mqprio_qopt opt = { 0 };
  191. struct Qdisc *qdisc;
  192. unsigned int i;
  193. sch->q.qlen = 0;
  194. memset(&sch->bstats, 0, sizeof(sch->bstats));
  195. memset(&sch->qstats, 0, sizeof(sch->qstats));
  196. for (i = 0; i < dev->num_tx_queues; i++) {
  197. qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
  198. spin_lock_bh(qdisc_lock(qdisc));
  199. sch->q.qlen += qdisc->q.qlen;
  200. sch->bstats.bytes += qdisc->bstats.bytes;
  201. sch->bstats.packets += qdisc->bstats.packets;
  202. sch->qstats.backlog += qdisc->qstats.backlog;
  203. sch->qstats.drops += qdisc->qstats.drops;
  204. sch->qstats.requeues += qdisc->qstats.requeues;
  205. sch->qstats.overlimits += qdisc->qstats.overlimits;
  206. spin_unlock_bh(qdisc_lock(qdisc));
  207. }
  208. opt.num_tc = netdev_get_num_tc(dev);
  209. memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
  210. opt.hw = priv->hw_offload;
  211. for (i = 0; i < netdev_get_num_tc(dev); i++) {
  212. opt.count[i] = dev->tc_to_txq[i].count;
  213. opt.offset[i] = dev->tc_to_txq[i].offset;
  214. }
  215. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  216. goto nla_put_failure;
  217. return skb->len;
  218. nla_put_failure:
  219. nlmsg_trim(skb, b);
  220. return -1;
  221. }
  222. static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl)
  223. {
  224. struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
  225. if (!dev_queue)
  226. return NULL;
  227. return dev_queue->qdisc_sleeping;
  228. }
  229. static unsigned long mqprio_find(struct Qdisc *sch, u32 classid)
  230. {
  231. struct net_device *dev = qdisc_dev(sch);
  232. unsigned int ntx = TC_H_MIN(classid);
  233. if (ntx > dev->num_tx_queues + netdev_get_num_tc(dev))
  234. return 0;
  235. return ntx;
  236. }
  237. static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl,
  238. struct sk_buff *skb, struct tcmsg *tcm)
  239. {
  240. struct net_device *dev = qdisc_dev(sch);
  241. if (cl <= netdev_get_num_tc(dev)) {
  242. tcm->tcm_parent = TC_H_ROOT;
  243. tcm->tcm_info = 0;
  244. } else {
  245. int i;
  246. struct netdev_queue *dev_queue;
  247. dev_queue = mqprio_queue_get(sch, cl);
  248. tcm->tcm_parent = 0;
  249. for (i = 0; i < netdev_get_num_tc(dev); i++) {
  250. struct netdev_tc_txq tc = dev->tc_to_txq[i];
  251. int q_idx = cl - netdev_get_num_tc(dev);
  252. if (q_idx > tc.offset &&
  253. q_idx <= tc.offset + tc.count) {
  254. tcm->tcm_parent =
  255. TC_H_MAKE(TC_H_MAJ(sch->handle),
  256. TC_H_MIN(i + 1));
  257. break;
  258. }
  259. }
  260. tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
  261. }
  262. tcm->tcm_handle |= TC_H_MIN(cl);
  263. return 0;
  264. }
  265. static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  266. struct gnet_dump *d)
  267. __releases(d->lock)
  268. __acquires(d->lock)
  269. {
  270. struct net_device *dev = qdisc_dev(sch);
  271. if (cl <= netdev_get_num_tc(dev)) {
  272. int i;
  273. __u32 qlen = 0;
  274. struct Qdisc *qdisc;
  275. struct gnet_stats_queue qstats = {0};
  276. struct gnet_stats_basic_packed bstats = {0};
  277. struct netdev_tc_txq tc = dev->tc_to_txq[cl - 1];
  278. /* Drop lock here it will be reclaimed before touching
  279. * statistics this is required because the d->lock we
  280. * hold here is the look on dev_queue->qdisc_sleeping
  281. * also acquired below.
  282. */
  283. if (d->lock)
  284. spin_unlock_bh(d->lock);
  285. for (i = tc.offset; i < tc.offset + tc.count; i++) {
  286. struct netdev_queue *q = netdev_get_tx_queue(dev, i);
  287. qdisc = rtnl_dereference(q->qdisc);
  288. spin_lock_bh(qdisc_lock(qdisc));
  289. qlen += qdisc->q.qlen;
  290. bstats.bytes += qdisc->bstats.bytes;
  291. bstats.packets += qdisc->bstats.packets;
  292. qstats.backlog += qdisc->qstats.backlog;
  293. qstats.drops += qdisc->qstats.drops;
  294. qstats.requeues += qdisc->qstats.requeues;
  295. qstats.overlimits += qdisc->qstats.overlimits;
  296. spin_unlock_bh(qdisc_lock(qdisc));
  297. }
  298. /* Reclaim root sleeping lock before completing stats */
  299. if (d->lock)
  300. spin_lock_bh(d->lock);
  301. if (gnet_stats_copy_basic(NULL, d, NULL, &bstats) < 0 ||
  302. gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
  303. return -1;
  304. } else {
  305. struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
  306. sch = dev_queue->qdisc_sleeping;
  307. if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch), d,
  308. sch->cpu_bstats, &sch->bstats) < 0 ||
  309. gnet_stats_copy_queue(d, NULL,
  310. &sch->qstats, sch->q.qlen) < 0)
  311. return -1;
  312. }
  313. return 0;
  314. }
  315. static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  316. {
  317. struct net_device *dev = qdisc_dev(sch);
  318. unsigned long ntx;
  319. if (arg->stop)
  320. return;
  321. /* Walk hierarchy with a virtual class per tc */
  322. arg->count = arg->skip;
  323. for (ntx = arg->skip;
  324. ntx < dev->num_tx_queues + netdev_get_num_tc(dev);
  325. ntx++) {
  326. if (arg->fn(sch, ntx + 1, arg) < 0) {
  327. arg->stop = 1;
  328. break;
  329. }
  330. arg->count++;
  331. }
  332. }
  333. static const struct Qdisc_class_ops mqprio_class_ops = {
  334. .graft = mqprio_graft,
  335. .leaf = mqprio_leaf,
  336. .find = mqprio_find,
  337. .walk = mqprio_walk,
  338. .dump = mqprio_dump_class,
  339. .dump_stats = mqprio_dump_class_stats,
  340. };
  341. static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
  342. .cl_ops = &mqprio_class_ops,
  343. .id = "mqprio",
  344. .priv_size = sizeof(struct mqprio_sched),
  345. .init = mqprio_init,
  346. .destroy = mqprio_destroy,
  347. .attach = mqprio_attach,
  348. .dump = mqprio_dump,
  349. .owner = THIS_MODULE,
  350. };
  351. static int __init mqprio_module_init(void)
  352. {
  353. return register_qdisc(&mqprio_qdisc_ops);
  354. }
  355. static void __exit mqprio_module_exit(void)
  356. {
  357. unregister_qdisc(&mqprio_qdisc_ops);
  358. }
  359. module_init(mqprio_module_init);
  360. module_exit(mqprio_module_exit);
  361. MODULE_LICENSE("GPL");