sch_mqprio.c 11 KB

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