sch_mqprio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. } else {
  355. sch->q.qlen += qdisc->q.qlen;
  356. sch->bstats.bytes += qdisc->bstats.bytes;
  357. sch->bstats.packets += qdisc->bstats.packets;
  358. sch->qstats.backlog += qdisc->qstats.backlog;
  359. sch->qstats.drops += qdisc->qstats.drops;
  360. sch->qstats.requeues += qdisc->qstats.requeues;
  361. sch->qstats.overlimits += qdisc->qstats.overlimits;
  362. }
  363. spin_unlock_bh(qdisc_lock(qdisc));
  364. }
  365. opt.num_tc = netdev_get_num_tc(dev);
  366. memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
  367. opt.hw = priv->hw_offload;
  368. for (tc = 0; tc < netdev_get_num_tc(dev); tc++) {
  369. opt.count[tc] = dev->tc_to_txq[tc].count;
  370. opt.offset[tc] = dev->tc_to_txq[tc].offset;
  371. }
  372. if (nla_put(skb, TCA_OPTIONS, NLA_ALIGN(sizeof(opt)), &opt))
  373. goto nla_put_failure;
  374. if ((priv->flags & TC_MQPRIO_F_MODE) &&
  375. nla_put_u16(skb, TCA_MQPRIO_MODE, priv->mode))
  376. goto nla_put_failure;
  377. if ((priv->flags & TC_MQPRIO_F_SHAPER) &&
  378. nla_put_u16(skb, TCA_MQPRIO_SHAPER, priv->shaper))
  379. goto nla_put_failure;
  380. if ((priv->flags & TC_MQPRIO_F_MIN_RATE ||
  381. priv->flags & TC_MQPRIO_F_MAX_RATE) &&
  382. (dump_rates(priv, &opt, skb) != 0))
  383. goto nla_put_failure;
  384. return nla_nest_end(skb, nla);
  385. nla_put_failure:
  386. nlmsg_trim(skb, nla);
  387. return -1;
  388. }
  389. static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl)
  390. {
  391. struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
  392. if (!dev_queue)
  393. return NULL;
  394. return dev_queue->qdisc_sleeping;
  395. }
  396. static unsigned long mqprio_find(struct Qdisc *sch, u32 classid)
  397. {
  398. struct net_device *dev = qdisc_dev(sch);
  399. unsigned int ntx = TC_H_MIN(classid);
  400. /* There are essentially two regions here that have valid classid
  401. * values. The first region will have a classid value of 1 through
  402. * num_tx_queues. All of these are backed by actual Qdiscs.
  403. */
  404. if (ntx < TC_H_MIN_PRIORITY)
  405. return (ntx <= dev->num_tx_queues) ? ntx : 0;
  406. /* The second region represents the hardware traffic classes. These
  407. * are represented by classid values of TC_H_MIN_PRIORITY through
  408. * TC_H_MIN_PRIORITY + netdev_get_num_tc - 1
  409. */
  410. return ((ntx - TC_H_MIN_PRIORITY) < netdev_get_num_tc(dev)) ? ntx : 0;
  411. }
  412. static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl,
  413. struct sk_buff *skb, struct tcmsg *tcm)
  414. {
  415. if (cl < TC_H_MIN_PRIORITY) {
  416. struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
  417. struct net_device *dev = qdisc_dev(sch);
  418. int tc = netdev_txq_to_tc(dev, cl - 1);
  419. tcm->tcm_parent = (tc < 0) ? 0 :
  420. TC_H_MAKE(TC_H_MAJ(sch->handle),
  421. TC_H_MIN(tc + TC_H_MIN_PRIORITY));
  422. tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
  423. } else {
  424. tcm->tcm_parent = TC_H_ROOT;
  425. tcm->tcm_info = 0;
  426. }
  427. tcm->tcm_handle |= TC_H_MIN(cl);
  428. return 0;
  429. }
  430. static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  431. struct gnet_dump *d)
  432. __releases(d->lock)
  433. __acquires(d->lock)
  434. {
  435. if (cl >= TC_H_MIN_PRIORITY) {
  436. int i;
  437. __u32 qlen = 0;
  438. struct gnet_stats_queue qstats = {0};
  439. struct gnet_stats_basic_packed bstats = {0};
  440. struct net_device *dev = qdisc_dev(sch);
  441. struct netdev_tc_txq tc = dev->tc_to_txq[cl & TC_BITMASK];
  442. /* Drop lock here it will be reclaimed before touching
  443. * statistics this is required because the d->lock we
  444. * hold here is the look on dev_queue->qdisc_sleeping
  445. * also acquired below.
  446. */
  447. if (d->lock)
  448. spin_unlock_bh(d->lock);
  449. for (i = tc.offset; i < tc.offset + tc.count; i++) {
  450. struct netdev_queue *q = netdev_get_tx_queue(dev, i);
  451. struct Qdisc *qdisc = rtnl_dereference(q->qdisc);
  452. struct gnet_stats_basic_cpu __percpu *cpu_bstats = NULL;
  453. struct gnet_stats_queue __percpu *cpu_qstats = NULL;
  454. spin_lock_bh(qdisc_lock(qdisc));
  455. if (qdisc_is_percpu_stats(qdisc)) {
  456. cpu_bstats = qdisc->cpu_bstats;
  457. cpu_qstats = qdisc->cpu_qstats;
  458. }
  459. qlen = qdisc_qlen_sum(qdisc);
  460. __gnet_stats_copy_basic(NULL, &sch->bstats,
  461. cpu_bstats, &qdisc->bstats);
  462. __gnet_stats_copy_queue(&sch->qstats,
  463. cpu_qstats,
  464. &qdisc->qstats,
  465. qlen);
  466. spin_unlock_bh(qdisc_lock(qdisc));
  467. }
  468. /* Reclaim root sleeping lock before completing stats */
  469. if (d->lock)
  470. spin_lock_bh(d->lock);
  471. if (gnet_stats_copy_basic(NULL, d, NULL, &bstats) < 0 ||
  472. gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
  473. return -1;
  474. } else {
  475. struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
  476. sch = dev_queue->qdisc_sleeping;
  477. if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
  478. d, NULL, &sch->bstats) < 0 ||
  479. gnet_stats_copy_queue(d, NULL,
  480. &sch->qstats, sch->q.qlen) < 0)
  481. return -1;
  482. }
  483. return 0;
  484. }
  485. static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  486. {
  487. struct net_device *dev = qdisc_dev(sch);
  488. unsigned long ntx;
  489. if (arg->stop)
  490. return;
  491. /* Walk hierarchy with a virtual class per tc */
  492. arg->count = arg->skip;
  493. for (ntx = arg->skip; ntx < netdev_get_num_tc(dev); ntx++) {
  494. if (arg->fn(sch, ntx + TC_H_MIN_PRIORITY, arg) < 0) {
  495. arg->stop = 1;
  496. return;
  497. }
  498. arg->count++;
  499. }
  500. /* Pad the values and skip over unused traffic classes */
  501. if (ntx < TC_MAX_QUEUE) {
  502. arg->count = TC_MAX_QUEUE;
  503. ntx = TC_MAX_QUEUE;
  504. }
  505. /* Reset offset, sort out remaining per-queue qdiscs */
  506. for (ntx -= TC_MAX_QUEUE; ntx < dev->num_tx_queues; ntx++) {
  507. if (arg->fn(sch, ntx + 1, arg) < 0) {
  508. arg->stop = 1;
  509. return;
  510. }
  511. arg->count++;
  512. }
  513. }
  514. static struct netdev_queue *mqprio_select_queue(struct Qdisc *sch,
  515. struct tcmsg *tcm)
  516. {
  517. return mqprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
  518. }
  519. static const struct Qdisc_class_ops mqprio_class_ops = {
  520. .graft = mqprio_graft,
  521. .leaf = mqprio_leaf,
  522. .find = mqprio_find,
  523. .walk = mqprio_walk,
  524. .dump = mqprio_dump_class,
  525. .dump_stats = mqprio_dump_class_stats,
  526. .select_queue = mqprio_select_queue,
  527. };
  528. static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
  529. .cl_ops = &mqprio_class_ops,
  530. .id = "mqprio",
  531. .priv_size = sizeof(struct mqprio_sched),
  532. .init = mqprio_init,
  533. .destroy = mqprio_destroy,
  534. .attach = mqprio_attach,
  535. .dump = mqprio_dump,
  536. .owner = THIS_MODULE,
  537. };
  538. static int __init mqprio_module_init(void)
  539. {
  540. return register_qdisc(&mqprio_qdisc_ops);
  541. }
  542. static void __exit mqprio_module_exit(void)
  543. {
  544. unregister_qdisc(&mqprio_qdisc_ops);
  545. }
  546. module_init(mqprio_module_init);
  547. module_exit(mqprio_module_exit);
  548. MODULE_LICENSE("GPL");