sch_mqprio.c 10 KB

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