sch_mqprio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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_owned;
  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_owned && dev->netdev_ops->ndo_setup_tc)
  37. dev->netdev_ops->ndo_setup_tc(dev, 0);
  38. else
  39. netdev_set_num_tc(dev, 0);
  40. }
  41. static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt)
  42. {
  43. int i, j;
  44. /* Verify num_tc is not out of max range */
  45. if (qopt->num_tc > TC_MAX_QUEUE)
  46. return -EINVAL;
  47. /* Verify priority mapping uses valid tcs */
  48. for (i = 0; i < TC_BITMASK + 1; i++) {
  49. if (qopt->prio_tc_map[i] >= qopt->num_tc)
  50. return -EINVAL;
  51. }
  52. /* net_device does not support requested operation */
  53. if (qopt->hw && !dev->netdev_ops->ndo_setup_tc)
  54. return -EINVAL;
  55. /* if hw owned qcount and qoffset are taken from LLD so
  56. * no reason to verify them here
  57. */
  58. if (qopt->hw)
  59. return 0;
  60. for (i = 0; i < qopt->num_tc; i++) {
  61. unsigned int last = qopt->offset[i] + qopt->count[i];
  62. /* Verify the queue count is in tx range being equal to the
  63. * real_num_tx_queues indicates the last queue is in use.
  64. */
  65. if (qopt->offset[i] >= dev->real_num_tx_queues ||
  66. !qopt->count[i] ||
  67. last > dev->real_num_tx_queues)
  68. return -EINVAL;
  69. /* Verify that the offset and counts do not overlap */
  70. for (j = i + 1; j < qopt->num_tc; j++) {
  71. if (last > qopt->offset[j])
  72. return -EINVAL;
  73. }
  74. }
  75. return 0;
  76. }
  77. static int mqprio_init(struct Qdisc *sch, struct nlattr *opt)
  78. {
  79. struct net_device *dev = qdisc_dev(sch);
  80. struct mqprio_sched *priv = qdisc_priv(sch);
  81. struct netdev_queue *dev_queue;
  82. struct Qdisc *qdisc;
  83. int i, err = -EOPNOTSUPP;
  84. struct tc_mqprio_qopt *qopt = NULL;
  85. BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE);
  86. BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK);
  87. if (sch->parent != TC_H_ROOT)
  88. return -EOPNOTSUPP;
  89. if (!netif_is_multiqueue(dev))
  90. return -EOPNOTSUPP;
  91. if (!opt || nla_len(opt) < sizeof(*qopt))
  92. return -EINVAL;
  93. qopt = nla_data(opt);
  94. if (mqprio_parse_opt(dev, qopt))
  95. return -EINVAL;
  96. /* pre-allocate qdisc, attachment can't fail */
  97. priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
  98. GFP_KERNEL);
  99. if (priv->qdiscs == NULL) {
  100. err = -ENOMEM;
  101. goto err;
  102. }
  103. for (i = 0; i < dev->num_tx_queues; i++) {
  104. dev_queue = netdev_get_tx_queue(dev, i);
  105. qdisc = qdisc_create_dflt(dev_queue, default_qdisc_ops,
  106. TC_H_MAKE(TC_H_MAJ(sch->handle),
  107. TC_H_MIN(i + 1)));
  108. if (qdisc == NULL) {
  109. err = -ENOMEM;
  110. goto err;
  111. }
  112. priv->qdiscs[i] = qdisc;
  113. qdisc->flags |= TCQ_F_ONETXQUEUE;
  114. }
  115. /* If the mqprio options indicate that hardware should own
  116. * the queue mapping then run ndo_setup_tc otherwise use the
  117. * supplied and verified mapping
  118. */
  119. if (qopt->hw) {
  120. priv->hw_owned = 1;
  121. err = dev->netdev_ops->ndo_setup_tc(dev, qopt->num_tc);
  122. if (err)
  123. goto err;
  124. } else {
  125. netdev_set_num_tc(dev, qopt->num_tc);
  126. for (i = 0; i < qopt->num_tc; i++)
  127. netdev_set_tc_queue(dev, i,
  128. qopt->count[i], qopt->offset[i]);
  129. }
  130. /* Always use supplied priority mappings */
  131. for (i = 0; i < TC_BITMASK + 1; i++)
  132. netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]);
  133. sch->flags |= TCQ_F_MQROOT;
  134. return 0;
  135. err:
  136. mqprio_destroy(sch);
  137. return err;
  138. }
  139. static void mqprio_attach(struct Qdisc *sch)
  140. {
  141. struct net_device *dev = qdisc_dev(sch);
  142. struct mqprio_sched *priv = qdisc_priv(sch);
  143. struct Qdisc *qdisc, *old;
  144. unsigned int ntx;
  145. /* Attach underlying qdisc */
  146. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  147. qdisc = priv->qdiscs[ntx];
  148. old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
  149. if (old)
  150. qdisc_destroy(old);
  151. if (ntx < dev->real_num_tx_queues)
  152. qdisc_list_add(qdisc);
  153. }
  154. kfree(priv->qdiscs);
  155. priv->qdiscs = NULL;
  156. }
  157. static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch,
  158. unsigned long cl)
  159. {
  160. struct net_device *dev = qdisc_dev(sch);
  161. unsigned long ntx = cl - 1 - netdev_get_num_tc(dev);
  162. if (ntx >= dev->num_tx_queues)
  163. return NULL;
  164. return netdev_get_tx_queue(dev, ntx);
  165. }
  166. static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
  167. struct Qdisc **old)
  168. {
  169. struct net_device *dev = qdisc_dev(sch);
  170. struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
  171. if (!dev_queue)
  172. return -EINVAL;
  173. if (dev->flags & IFF_UP)
  174. dev_deactivate(dev);
  175. *old = dev_graft_qdisc(dev_queue, new);
  176. if (new)
  177. new->flags |= TCQ_F_ONETXQUEUE;
  178. if (dev->flags & IFF_UP)
  179. dev_activate(dev);
  180. return 0;
  181. }
  182. static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb)
  183. {
  184. struct net_device *dev = qdisc_dev(sch);
  185. struct mqprio_sched *priv = qdisc_priv(sch);
  186. unsigned char *b = skb_tail_pointer(skb);
  187. struct tc_mqprio_qopt opt = { 0 };
  188. struct Qdisc *qdisc;
  189. unsigned int i;
  190. sch->q.qlen = 0;
  191. memset(&sch->bstats, 0, sizeof(sch->bstats));
  192. memset(&sch->qstats, 0, sizeof(sch->qstats));
  193. for (i = 0; i < dev->num_tx_queues; i++) {
  194. qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
  195. spin_lock_bh(qdisc_lock(qdisc));
  196. sch->q.qlen += qdisc->q.qlen;
  197. sch->bstats.bytes += qdisc->bstats.bytes;
  198. sch->bstats.packets += qdisc->bstats.packets;
  199. sch->qstats.backlog += qdisc->qstats.backlog;
  200. sch->qstats.drops += qdisc->qstats.drops;
  201. sch->qstats.requeues += qdisc->qstats.requeues;
  202. sch->qstats.overlimits += qdisc->qstats.overlimits;
  203. spin_unlock_bh(qdisc_lock(qdisc));
  204. }
  205. opt.num_tc = netdev_get_num_tc(dev);
  206. memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
  207. opt.hw = priv->hw_owned;
  208. for (i = 0; i < netdev_get_num_tc(dev); i++) {
  209. opt.count[i] = dev->tc_to_txq[i].count;
  210. opt.offset[i] = dev->tc_to_txq[i].offset;
  211. }
  212. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  213. goto nla_put_failure;
  214. return skb->len;
  215. nla_put_failure:
  216. nlmsg_trim(skb, b);
  217. return -1;
  218. }
  219. static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl)
  220. {
  221. struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
  222. if (!dev_queue)
  223. return NULL;
  224. return dev_queue->qdisc_sleeping;
  225. }
  226. static unsigned long mqprio_get(struct Qdisc *sch, u32 classid)
  227. {
  228. struct net_device *dev = qdisc_dev(sch);
  229. unsigned int ntx = TC_H_MIN(classid);
  230. if (ntx > dev->num_tx_queues + netdev_get_num_tc(dev))
  231. return 0;
  232. return ntx;
  233. }
  234. static void mqprio_put(struct Qdisc *sch, unsigned long cl)
  235. {
  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. spin_unlock_bh(d->lock);
  284. for (i = tc.offset; i < tc.offset + tc.count; i++) {
  285. struct netdev_queue *q = netdev_get_tx_queue(dev, i);
  286. qdisc = rtnl_dereference(q->qdisc);
  287. spin_lock_bh(qdisc_lock(qdisc));
  288. qlen += qdisc->q.qlen;
  289. bstats.bytes += qdisc->bstats.bytes;
  290. bstats.packets += qdisc->bstats.packets;
  291. qstats.backlog += qdisc->qstats.backlog;
  292. qstats.drops += qdisc->qstats.drops;
  293. qstats.requeues += qdisc->qstats.requeues;
  294. qstats.overlimits += qdisc->qstats.overlimits;
  295. spin_unlock_bh(qdisc_lock(qdisc));
  296. }
  297. /* Reclaim root sleeping lock before completing stats */
  298. spin_lock_bh(d->lock);
  299. if (gnet_stats_copy_basic(d, NULL, &bstats) < 0 ||
  300. gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
  301. return -1;
  302. } else {
  303. struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
  304. sch = dev_queue->qdisc_sleeping;
  305. if (gnet_stats_copy_basic(d, NULL, &sch->bstats) < 0 ||
  306. gnet_stats_copy_queue(d, NULL,
  307. &sch->qstats, sch->q.qlen) < 0)
  308. return -1;
  309. }
  310. return 0;
  311. }
  312. static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  313. {
  314. struct net_device *dev = qdisc_dev(sch);
  315. unsigned long ntx;
  316. if (arg->stop)
  317. return;
  318. /* Walk hierarchy with a virtual class per tc */
  319. arg->count = arg->skip;
  320. for (ntx = arg->skip;
  321. ntx < dev->num_tx_queues + netdev_get_num_tc(dev);
  322. ntx++) {
  323. if (arg->fn(sch, ntx + 1, arg) < 0) {
  324. arg->stop = 1;
  325. break;
  326. }
  327. arg->count++;
  328. }
  329. }
  330. static const struct Qdisc_class_ops mqprio_class_ops = {
  331. .graft = mqprio_graft,
  332. .leaf = mqprio_leaf,
  333. .get = mqprio_get,
  334. .put = mqprio_put,
  335. .walk = mqprio_walk,
  336. .dump = mqprio_dump_class,
  337. .dump_stats = mqprio_dump_class_stats,
  338. };
  339. static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
  340. .cl_ops = &mqprio_class_ops,
  341. .id = "mqprio",
  342. .priv_size = sizeof(struct mqprio_sched),
  343. .init = mqprio_init,
  344. .destroy = mqprio_destroy,
  345. .attach = mqprio_attach,
  346. .dump = mqprio_dump,
  347. .owner = THIS_MODULE,
  348. };
  349. static int __init mqprio_module_init(void)
  350. {
  351. return register_qdisc(&mqprio_qdisc_ops);
  352. }
  353. static void __exit mqprio_module_exit(void)
  354. {
  355. unregister_qdisc(&mqprio_qdisc_ops);
  356. }
  357. module_init(mqprio_module_init);
  358. module_exit(mqprio_module_exit);
  359. MODULE_LICENSE("GPL");