sch_mqprio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. #include <net/pkt_cls.h>
  21. struct mqprio_sched {
  22. struct Qdisc **qdiscs;
  23. u16 mode;
  24. u16 shaper;
  25. int hw_offload;
  26. u32 flags;
  27. u64 min_rate[TC_QOPT_MAX_QUEUE];
  28. u64 max_rate[TC_QOPT_MAX_QUEUE];
  29. };
  30. static void mqprio_destroy(struct Qdisc *sch)
  31. {
  32. struct net_device *dev = qdisc_dev(sch);
  33. struct mqprio_sched *priv = qdisc_priv(sch);
  34. unsigned int ntx;
  35. if (priv->qdiscs) {
  36. for (ntx = 0;
  37. ntx < dev->num_tx_queues && priv->qdiscs[ntx];
  38. ntx++)
  39. qdisc_destroy(priv->qdiscs[ntx]);
  40. kfree(priv->qdiscs);
  41. }
  42. if (priv->hw_offload && dev->netdev_ops->ndo_setup_tc) {
  43. struct tc_mqprio_qopt_offload mqprio = { { 0 } };
  44. switch (priv->mode) {
  45. case TC_MQPRIO_MODE_DCB:
  46. case TC_MQPRIO_MODE_CHANNEL:
  47. dev->netdev_ops->ndo_setup_tc(dev,
  48. TC_SETUP_QDISC_MQPRIO,
  49. &mqprio);
  50. break;
  51. default:
  52. return;
  53. }
  54. } else {
  55. netdev_set_num_tc(dev, 0);
  56. }
  57. }
  58. static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt)
  59. {
  60. int i, j;
  61. /* Verify num_tc is not out of max range */
  62. if (qopt->num_tc > TC_MAX_QUEUE)
  63. return -EINVAL;
  64. /* Verify priority mapping uses valid tcs */
  65. for (i = 0; i < TC_BITMASK + 1; i++) {
  66. if (qopt->prio_tc_map[i] >= qopt->num_tc)
  67. return -EINVAL;
  68. }
  69. /* Limit qopt->hw to maximum supported offload value. Drivers have
  70. * the option of overriding this later if they don't support the a
  71. * given offload type.
  72. */
  73. if (qopt->hw > TC_MQPRIO_HW_OFFLOAD_MAX)
  74. qopt->hw = TC_MQPRIO_HW_OFFLOAD_MAX;
  75. /* If hardware offload is requested we will leave it to the device
  76. * to either populate the queue counts itself or to validate the
  77. * provided queue counts. If ndo_setup_tc is not present then
  78. * hardware doesn't support offload and we should return an error.
  79. */
  80. if (qopt->hw)
  81. return dev->netdev_ops->ndo_setup_tc ? 0 : -EINVAL;
  82. for (i = 0; i < qopt->num_tc; i++) {
  83. unsigned int last = qopt->offset[i] + qopt->count[i];
  84. /* Verify the queue count is in tx range being equal to the
  85. * real_num_tx_queues indicates the last queue is in use.
  86. */
  87. if (qopt->offset[i] >= dev->real_num_tx_queues ||
  88. !qopt->count[i] ||
  89. last > dev->real_num_tx_queues)
  90. return -EINVAL;
  91. /* Verify that the offset and counts do not overlap */
  92. for (j = i + 1; j < qopt->num_tc; j++) {
  93. if (last > qopt->offset[j])
  94. return -EINVAL;
  95. }
  96. }
  97. return 0;
  98. }
  99. static const struct nla_policy mqprio_policy[TCA_MQPRIO_MAX + 1] = {
  100. [TCA_MQPRIO_MODE] = { .len = sizeof(u16) },
  101. [TCA_MQPRIO_SHAPER] = { .len = sizeof(u16) },
  102. [TCA_MQPRIO_MIN_RATE64] = { .type = NLA_NESTED },
  103. [TCA_MQPRIO_MAX_RATE64] = { .type = NLA_NESTED },
  104. };
  105. static int parse_attr(struct nlattr *tb[], int maxtype, struct nlattr *nla,
  106. const struct nla_policy *policy, int len)
  107. {
  108. int nested_len = nla_len(nla) - NLA_ALIGN(len);
  109. if (nested_len >= nla_attr_size(0))
  110. return nla_parse(tb, maxtype, nla_data(nla) + NLA_ALIGN(len),
  111. nested_len, policy, NULL);
  112. memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
  113. return 0;
  114. }
  115. static int mqprio_init(struct Qdisc *sch, struct nlattr *opt,
  116. struct netlink_ext_ack *extack)
  117. {
  118. struct net_device *dev = qdisc_dev(sch);
  119. struct mqprio_sched *priv = qdisc_priv(sch);
  120. struct netdev_queue *dev_queue;
  121. struct Qdisc *qdisc;
  122. int i, err = -EOPNOTSUPP;
  123. struct tc_mqprio_qopt *qopt = NULL;
  124. struct nlattr *tb[TCA_MQPRIO_MAX + 1];
  125. struct nlattr *attr;
  126. int rem;
  127. int len;
  128. BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE);
  129. BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK);
  130. if (sch->parent != TC_H_ROOT)
  131. return -EOPNOTSUPP;
  132. if (!netif_is_multiqueue(dev))
  133. return -EOPNOTSUPP;
  134. /* make certain can allocate enough classids to handle queues */
  135. if (dev->num_tx_queues >= TC_H_MIN_PRIORITY)
  136. return -ENOMEM;
  137. if (!opt || nla_len(opt) < sizeof(*qopt))
  138. return -EINVAL;
  139. qopt = nla_data(opt);
  140. if (mqprio_parse_opt(dev, qopt))
  141. return -EINVAL;
  142. len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt));
  143. if (len > 0) {
  144. err = parse_attr(tb, TCA_MQPRIO_MAX, opt, mqprio_policy,
  145. sizeof(*qopt));
  146. if (err < 0)
  147. return err;
  148. if (!qopt->hw)
  149. return -EINVAL;
  150. if (tb[TCA_MQPRIO_MODE]) {
  151. priv->flags |= TC_MQPRIO_F_MODE;
  152. priv->mode = *(u16 *)nla_data(tb[TCA_MQPRIO_MODE]);
  153. }
  154. if (tb[TCA_MQPRIO_SHAPER]) {
  155. priv->flags |= TC_MQPRIO_F_SHAPER;
  156. priv->shaper = *(u16 *)nla_data(tb[TCA_MQPRIO_SHAPER]);
  157. }
  158. if (tb[TCA_MQPRIO_MIN_RATE64]) {
  159. if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE)
  160. return -EINVAL;
  161. i = 0;
  162. nla_for_each_nested(attr, tb[TCA_MQPRIO_MIN_RATE64],
  163. rem) {
  164. if (nla_type(attr) != TCA_MQPRIO_MIN_RATE64)
  165. return -EINVAL;
  166. if (i >= qopt->num_tc)
  167. break;
  168. priv->min_rate[i] = *(u64 *)nla_data(attr);
  169. i++;
  170. }
  171. priv->flags |= TC_MQPRIO_F_MIN_RATE;
  172. }
  173. if (tb[TCA_MQPRIO_MAX_RATE64]) {
  174. if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE)
  175. return -EINVAL;
  176. i = 0;
  177. nla_for_each_nested(attr, tb[TCA_MQPRIO_MAX_RATE64],
  178. rem) {
  179. if (nla_type(attr) != TCA_MQPRIO_MAX_RATE64)
  180. return -EINVAL;
  181. if (i >= qopt->num_tc)
  182. break;
  183. priv->max_rate[i] = *(u64 *)nla_data(attr);
  184. i++;
  185. }
  186. priv->flags |= TC_MQPRIO_F_MAX_RATE;
  187. }
  188. }
  189. /* pre-allocate qdisc, attachment can't fail */
  190. priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
  191. GFP_KERNEL);
  192. if (!priv->qdiscs)
  193. return -ENOMEM;
  194. for (i = 0; i < dev->num_tx_queues; i++) {
  195. dev_queue = netdev_get_tx_queue(dev, i);
  196. qdisc = qdisc_create_dflt(dev_queue,
  197. get_default_qdisc_ops(dev, i),
  198. TC_H_MAKE(TC_H_MAJ(sch->handle),
  199. TC_H_MIN(i + 1)), extack);
  200. if (!qdisc)
  201. return -ENOMEM;
  202. priv->qdiscs[i] = qdisc;
  203. qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
  204. }
  205. /* If the mqprio options indicate that hardware should own
  206. * the queue mapping then run ndo_setup_tc otherwise use the
  207. * supplied and verified mapping
  208. */
  209. if (qopt->hw) {
  210. struct tc_mqprio_qopt_offload mqprio = {.qopt = *qopt};
  211. switch (priv->mode) {
  212. case TC_MQPRIO_MODE_DCB:
  213. if (priv->shaper != TC_MQPRIO_SHAPER_DCB)
  214. return -EINVAL;
  215. break;
  216. case TC_MQPRIO_MODE_CHANNEL:
  217. mqprio.flags = priv->flags;
  218. if (priv->flags & TC_MQPRIO_F_MODE)
  219. mqprio.mode = priv->mode;
  220. if (priv->flags & TC_MQPRIO_F_SHAPER)
  221. mqprio.shaper = priv->shaper;
  222. if (priv->flags & TC_MQPRIO_F_MIN_RATE)
  223. for (i = 0; i < mqprio.qopt.num_tc; i++)
  224. mqprio.min_rate[i] = priv->min_rate[i];
  225. if (priv->flags & TC_MQPRIO_F_MAX_RATE)
  226. for (i = 0; i < mqprio.qopt.num_tc; i++)
  227. mqprio.max_rate[i] = priv->max_rate[i];
  228. break;
  229. default:
  230. return -EINVAL;
  231. }
  232. err = dev->netdev_ops->ndo_setup_tc(dev,
  233. TC_SETUP_QDISC_MQPRIO,
  234. &mqprio);
  235. if (err)
  236. return err;
  237. priv->hw_offload = mqprio.qopt.hw;
  238. } else {
  239. netdev_set_num_tc(dev, qopt->num_tc);
  240. for (i = 0; i < qopt->num_tc; i++)
  241. netdev_set_tc_queue(dev, i,
  242. qopt->count[i], qopt->offset[i]);
  243. }
  244. /* Always use supplied priority mappings */
  245. for (i = 0; i < TC_BITMASK + 1; i++)
  246. netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]);
  247. sch->flags |= TCQ_F_MQROOT;
  248. return 0;
  249. }
  250. static void mqprio_attach(struct Qdisc *sch)
  251. {
  252. struct net_device *dev = qdisc_dev(sch);
  253. struct mqprio_sched *priv = qdisc_priv(sch);
  254. struct Qdisc *qdisc, *old;
  255. unsigned int ntx;
  256. /* Attach underlying qdisc */
  257. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  258. qdisc = priv->qdiscs[ntx];
  259. old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
  260. if (old)
  261. qdisc_destroy(old);
  262. if (ntx < dev->real_num_tx_queues)
  263. qdisc_hash_add(qdisc, false);
  264. }
  265. kfree(priv->qdiscs);
  266. priv->qdiscs = NULL;
  267. }
  268. static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch,
  269. unsigned long cl)
  270. {
  271. struct net_device *dev = qdisc_dev(sch);
  272. unsigned long ntx = cl - 1;
  273. if (ntx >= dev->num_tx_queues)
  274. return NULL;
  275. return netdev_get_tx_queue(dev, ntx);
  276. }
  277. static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
  278. struct Qdisc **old, struct netlink_ext_ack *extack)
  279. {
  280. struct net_device *dev = qdisc_dev(sch);
  281. struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
  282. if (!dev_queue)
  283. return -EINVAL;
  284. if (dev->flags & IFF_UP)
  285. dev_deactivate(dev);
  286. *old = dev_graft_qdisc(dev_queue, new);
  287. if (new)
  288. new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
  289. if (dev->flags & IFF_UP)
  290. dev_activate(dev);
  291. return 0;
  292. }
  293. static int dump_rates(struct mqprio_sched *priv,
  294. struct tc_mqprio_qopt *opt, struct sk_buff *skb)
  295. {
  296. struct nlattr *nest;
  297. int i;
  298. if (priv->flags & TC_MQPRIO_F_MIN_RATE) {
  299. nest = nla_nest_start(skb, TCA_MQPRIO_MIN_RATE64);
  300. if (!nest)
  301. goto nla_put_failure;
  302. for (i = 0; i < opt->num_tc; i++) {
  303. if (nla_put(skb, TCA_MQPRIO_MIN_RATE64,
  304. sizeof(priv->min_rate[i]),
  305. &priv->min_rate[i]))
  306. goto nla_put_failure;
  307. }
  308. nla_nest_end(skb, nest);
  309. }
  310. if (priv->flags & TC_MQPRIO_F_MAX_RATE) {
  311. nest = nla_nest_start(skb, TCA_MQPRIO_MAX_RATE64);
  312. if (!nest)
  313. goto nla_put_failure;
  314. for (i = 0; i < opt->num_tc; i++) {
  315. if (nla_put(skb, TCA_MQPRIO_MAX_RATE64,
  316. sizeof(priv->max_rate[i]),
  317. &priv->max_rate[i]))
  318. goto nla_put_failure;
  319. }
  320. nla_nest_end(skb, nest);
  321. }
  322. return 0;
  323. nla_put_failure:
  324. nla_nest_cancel(skb, nest);
  325. return -1;
  326. }
  327. static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb)
  328. {
  329. struct net_device *dev = qdisc_dev(sch);
  330. struct mqprio_sched *priv = qdisc_priv(sch);
  331. struct nlattr *nla = (struct nlattr *)skb_tail_pointer(skb);
  332. struct tc_mqprio_qopt opt = { 0 };
  333. struct Qdisc *qdisc;
  334. unsigned int ntx, tc;
  335. sch->q.qlen = 0;
  336. memset(&sch->bstats, 0, sizeof(sch->bstats));
  337. memset(&sch->qstats, 0, sizeof(sch->qstats));
  338. /* MQ supports lockless qdiscs. However, statistics accounting needs
  339. * to account for all, none, or a mix of locked and unlocked child
  340. * qdiscs. Percpu stats are added to counters in-band and locking
  341. * qdisc totals are added at end.
  342. */
  343. for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
  344. qdisc = netdev_get_tx_queue(dev, ntx)->qdisc_sleeping;
  345. spin_lock_bh(qdisc_lock(qdisc));
  346. if (qdisc_is_percpu_stats(qdisc)) {
  347. __u32 qlen = qdisc_qlen_sum(qdisc);
  348. __gnet_stats_copy_basic(NULL, &sch->bstats,
  349. qdisc->cpu_bstats,
  350. &qdisc->bstats);
  351. __gnet_stats_copy_queue(&sch->qstats,
  352. qdisc->cpu_qstats,
  353. &qdisc->qstats, qlen);
  354. sch->q.qlen += qlen;
  355. } else {
  356. sch->q.qlen += qdisc->q.qlen;
  357. sch->bstats.bytes += qdisc->bstats.bytes;
  358. sch->bstats.packets += qdisc->bstats.packets;
  359. sch->qstats.backlog += qdisc->qstats.backlog;
  360. sch->qstats.drops += qdisc->qstats.drops;
  361. sch->qstats.requeues += qdisc->qstats.requeues;
  362. sch->qstats.overlimits += qdisc->qstats.overlimits;
  363. }
  364. spin_unlock_bh(qdisc_lock(qdisc));
  365. }
  366. opt.num_tc = netdev_get_num_tc(dev);
  367. memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
  368. opt.hw = priv->hw_offload;
  369. for (tc = 0; tc < netdev_get_num_tc(dev); tc++) {
  370. opt.count[tc] = dev->tc_to_txq[tc].count;
  371. opt.offset[tc] = dev->tc_to_txq[tc].offset;
  372. }
  373. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  374. goto nla_put_failure;
  375. if ((priv->flags & TC_MQPRIO_F_MODE) &&
  376. nla_put_u16(skb, TCA_MQPRIO_MODE, priv->mode))
  377. goto nla_put_failure;
  378. if ((priv->flags & TC_MQPRIO_F_SHAPER) &&
  379. nla_put_u16(skb, TCA_MQPRIO_SHAPER, priv->shaper))
  380. goto nla_put_failure;
  381. if ((priv->flags & TC_MQPRIO_F_MIN_RATE ||
  382. priv->flags & TC_MQPRIO_F_MAX_RATE) &&
  383. (dump_rates(priv, &opt, skb) != 0))
  384. goto nla_put_failure;
  385. return nla_nest_end(skb, nla);
  386. nla_put_failure:
  387. nlmsg_trim(skb, nla);
  388. return -1;
  389. }
  390. static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl)
  391. {
  392. struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
  393. if (!dev_queue)
  394. return NULL;
  395. return dev_queue->qdisc_sleeping;
  396. }
  397. static unsigned long mqprio_find(struct Qdisc *sch, u32 classid)
  398. {
  399. struct net_device *dev = qdisc_dev(sch);
  400. unsigned int ntx = TC_H_MIN(classid);
  401. /* There are essentially two regions here that have valid classid
  402. * values. The first region will have a classid value of 1 through
  403. * num_tx_queues. All of these are backed by actual Qdiscs.
  404. */
  405. if (ntx < TC_H_MIN_PRIORITY)
  406. return (ntx <= dev->num_tx_queues) ? ntx : 0;
  407. /* The second region represents the hardware traffic classes. These
  408. * are represented by classid values of TC_H_MIN_PRIORITY through
  409. * TC_H_MIN_PRIORITY + netdev_get_num_tc - 1
  410. */
  411. return ((ntx - TC_H_MIN_PRIORITY) < netdev_get_num_tc(dev)) ? ntx : 0;
  412. }
  413. static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl,
  414. struct sk_buff *skb, struct tcmsg *tcm)
  415. {
  416. if (cl < TC_H_MIN_PRIORITY) {
  417. struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
  418. struct net_device *dev = qdisc_dev(sch);
  419. int tc = netdev_txq_to_tc(dev, cl - 1);
  420. tcm->tcm_parent = (tc < 0) ? 0 :
  421. TC_H_MAKE(TC_H_MAJ(sch->handle),
  422. TC_H_MIN(tc + TC_H_MIN_PRIORITY));
  423. tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
  424. } else {
  425. tcm->tcm_parent = TC_H_ROOT;
  426. tcm->tcm_info = 0;
  427. }
  428. tcm->tcm_handle |= TC_H_MIN(cl);
  429. return 0;
  430. }
  431. static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  432. struct gnet_dump *d)
  433. __releases(d->lock)
  434. __acquires(d->lock)
  435. {
  436. if (cl >= TC_H_MIN_PRIORITY) {
  437. int i;
  438. __u32 qlen = 0;
  439. struct gnet_stats_queue qstats = {0};
  440. struct gnet_stats_basic_packed bstats = {0};
  441. struct net_device *dev = qdisc_dev(sch);
  442. struct netdev_tc_txq tc = dev->tc_to_txq[cl & TC_BITMASK];
  443. /* Drop lock here it will be reclaimed before touching
  444. * statistics this is required because the d->lock we
  445. * hold here is the look on dev_queue->qdisc_sleeping
  446. * also acquired below.
  447. */
  448. if (d->lock)
  449. spin_unlock_bh(d->lock);
  450. for (i = tc.offset; i < tc.offset + tc.count; i++) {
  451. struct netdev_queue *q = netdev_get_tx_queue(dev, i);
  452. struct Qdisc *qdisc = rtnl_dereference(q->qdisc);
  453. struct gnet_stats_basic_cpu __percpu *cpu_bstats = NULL;
  454. struct gnet_stats_queue __percpu *cpu_qstats = NULL;
  455. spin_lock_bh(qdisc_lock(qdisc));
  456. if (qdisc_is_percpu_stats(qdisc)) {
  457. cpu_bstats = qdisc->cpu_bstats;
  458. cpu_qstats = qdisc->cpu_qstats;
  459. }
  460. qlen = qdisc_qlen_sum(qdisc);
  461. __gnet_stats_copy_basic(NULL, &sch->bstats,
  462. cpu_bstats, &qdisc->bstats);
  463. __gnet_stats_copy_queue(&sch->qstats,
  464. cpu_qstats,
  465. &qdisc->qstats,
  466. qlen);
  467. spin_unlock_bh(qdisc_lock(qdisc));
  468. }
  469. /* Reclaim root sleeping lock before completing stats */
  470. if (d->lock)
  471. spin_lock_bh(d->lock);
  472. if (gnet_stats_copy_basic(NULL, d, NULL, &bstats) < 0 ||
  473. gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
  474. return -1;
  475. } else {
  476. struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
  477. sch = dev_queue->qdisc_sleeping;
  478. if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch), d,
  479. sch->cpu_bstats, &sch->bstats) < 0 ||
  480. gnet_stats_copy_queue(d, NULL,
  481. &sch->qstats, sch->q.qlen) < 0)
  482. return -1;
  483. }
  484. return 0;
  485. }
  486. static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  487. {
  488. struct net_device *dev = qdisc_dev(sch);
  489. unsigned long ntx;
  490. if (arg->stop)
  491. return;
  492. /* Walk hierarchy with a virtual class per tc */
  493. arg->count = arg->skip;
  494. for (ntx = arg->skip; ntx < netdev_get_num_tc(dev); ntx++) {
  495. if (arg->fn(sch, ntx + TC_H_MIN_PRIORITY, arg) < 0) {
  496. arg->stop = 1;
  497. return;
  498. }
  499. arg->count++;
  500. }
  501. /* Pad the values and skip over unused traffic classes */
  502. if (ntx < TC_MAX_QUEUE) {
  503. arg->count = TC_MAX_QUEUE;
  504. ntx = TC_MAX_QUEUE;
  505. }
  506. /* Reset offset, sort out remaining per-queue qdiscs */
  507. for (ntx -= TC_MAX_QUEUE; ntx < dev->num_tx_queues; ntx++) {
  508. if (arg->fn(sch, ntx + 1, arg) < 0) {
  509. arg->stop = 1;
  510. return;
  511. }
  512. arg->count++;
  513. }
  514. }
  515. static struct netdev_queue *mqprio_select_queue(struct Qdisc *sch,
  516. struct tcmsg *tcm)
  517. {
  518. return mqprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
  519. }
  520. static const struct Qdisc_class_ops mqprio_class_ops = {
  521. .graft = mqprio_graft,
  522. .leaf = mqprio_leaf,
  523. .find = mqprio_find,
  524. .walk = mqprio_walk,
  525. .dump = mqprio_dump_class,
  526. .dump_stats = mqprio_dump_class_stats,
  527. .select_queue = mqprio_select_queue,
  528. };
  529. static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
  530. .cl_ops = &mqprio_class_ops,
  531. .id = "mqprio",
  532. .priv_size = sizeof(struct mqprio_sched),
  533. .init = mqprio_init,
  534. .destroy = mqprio_destroy,
  535. .attach = mqprio_attach,
  536. .dump = mqprio_dump,
  537. .owner = THIS_MODULE,
  538. };
  539. static int __init mqprio_module_init(void)
  540. {
  541. return register_qdisc(&mqprio_qdisc_ops);
  542. }
  543. static void __exit mqprio_module_exit(void)
  544. {
  545. unregister_qdisc(&mqprio_qdisc_ops);
  546. }
  547. module_init(mqprio_module_init);
  548. module_exit(mqprio_module_exit);
  549. MODULE_LICENSE("GPL");