sch_mqprio.c 17 KB

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