sch_taprio.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* net/sched/sch_taprio.c Time Aware Priority Scheduler
  3. *
  4. * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com>
  5. *
  6. */
  7. #include <linux/types.h>
  8. #include <linux/slab.h>
  9. #include <linux/kernel.h>
  10. #include <linux/string.h>
  11. #include <linux/list.h>
  12. #include <linux/errno.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/module.h>
  15. #include <linux/spinlock.h>
  16. #include <net/netlink.h>
  17. #include <net/pkt_sched.h>
  18. #include <net/pkt_cls.h>
  19. #include <net/sch_generic.h>
  20. #define TAPRIO_ALL_GATES_OPEN -1
  21. struct sched_entry {
  22. struct list_head list;
  23. /* The instant that this entry "closes" and the next one
  24. * should open, the qdisc will make some effort so that no
  25. * packet leaves after this time.
  26. */
  27. ktime_t close_time;
  28. atomic_t budget;
  29. int index;
  30. u32 gate_mask;
  31. u32 interval;
  32. u8 command;
  33. };
  34. struct taprio_sched {
  35. struct Qdisc **qdiscs;
  36. struct Qdisc *root;
  37. s64 base_time;
  38. int clockid;
  39. int picos_per_byte; /* Using picoseconds because for 10Gbps+
  40. * speeds it's sub-nanoseconds per byte
  41. */
  42. size_t num_entries;
  43. /* Protects the update side of the RCU protected current_entry */
  44. spinlock_t current_entry_lock;
  45. struct sched_entry __rcu *current_entry;
  46. struct list_head entries;
  47. ktime_t (*get_time)(void);
  48. struct hrtimer advance_timer;
  49. };
  50. static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  51. struct sk_buff **to_free)
  52. {
  53. struct taprio_sched *q = qdisc_priv(sch);
  54. struct Qdisc *child;
  55. int queue;
  56. queue = skb_get_queue_mapping(skb);
  57. child = q->qdiscs[queue];
  58. if (unlikely(!child))
  59. return qdisc_drop(skb, sch, to_free);
  60. qdisc_qstats_backlog_inc(sch, skb);
  61. sch->q.qlen++;
  62. return qdisc_enqueue(skb, child, to_free);
  63. }
  64. static struct sk_buff *taprio_peek(struct Qdisc *sch)
  65. {
  66. struct taprio_sched *q = qdisc_priv(sch);
  67. struct net_device *dev = qdisc_dev(sch);
  68. struct sched_entry *entry;
  69. struct sk_buff *skb;
  70. u32 gate_mask;
  71. int i;
  72. rcu_read_lock();
  73. entry = rcu_dereference(q->current_entry);
  74. gate_mask = entry ? entry->gate_mask : -1;
  75. rcu_read_unlock();
  76. if (!gate_mask)
  77. return NULL;
  78. for (i = 0; i < dev->num_tx_queues; i++) {
  79. struct Qdisc *child = q->qdiscs[i];
  80. int prio;
  81. u8 tc;
  82. if (unlikely(!child))
  83. continue;
  84. skb = child->ops->peek(child);
  85. if (!skb)
  86. continue;
  87. prio = skb->priority;
  88. tc = netdev_get_prio_tc_map(dev, prio);
  89. if (!(gate_mask & BIT(tc)))
  90. return NULL;
  91. return skb;
  92. }
  93. return NULL;
  94. }
  95. static inline int length_to_duration(struct taprio_sched *q, int len)
  96. {
  97. return (len * q->picos_per_byte) / 1000;
  98. }
  99. static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
  100. {
  101. struct taprio_sched *q = qdisc_priv(sch);
  102. struct net_device *dev = qdisc_dev(sch);
  103. struct sched_entry *entry;
  104. struct sk_buff *skb;
  105. u32 gate_mask;
  106. int i;
  107. rcu_read_lock();
  108. entry = rcu_dereference(q->current_entry);
  109. /* if there's no entry, it means that the schedule didn't
  110. * start yet, so force all gates to be open, this is in
  111. * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
  112. * "AdminGateSates"
  113. */
  114. gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
  115. rcu_read_unlock();
  116. if (!gate_mask)
  117. return NULL;
  118. for (i = 0; i < dev->num_tx_queues; i++) {
  119. struct Qdisc *child = q->qdiscs[i];
  120. ktime_t guard;
  121. int prio;
  122. int len;
  123. u8 tc;
  124. if (unlikely(!child))
  125. continue;
  126. skb = child->ops->peek(child);
  127. if (!skb)
  128. continue;
  129. prio = skb->priority;
  130. tc = netdev_get_prio_tc_map(dev, prio);
  131. if (!(gate_mask & BIT(tc)))
  132. continue;
  133. len = qdisc_pkt_len(skb);
  134. guard = ktime_add_ns(q->get_time(),
  135. length_to_duration(q, len));
  136. /* In the case that there's no gate entry, there's no
  137. * guard band ...
  138. */
  139. if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
  140. ktime_after(guard, entry->close_time))
  141. return NULL;
  142. /* ... and no budget. */
  143. if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
  144. atomic_sub_return(len, &entry->budget) < 0)
  145. return NULL;
  146. skb = child->ops->dequeue(child);
  147. if (unlikely(!skb))
  148. return NULL;
  149. qdisc_bstats_update(sch, skb);
  150. qdisc_qstats_backlog_dec(sch, skb);
  151. sch->q.qlen--;
  152. return skb;
  153. }
  154. return NULL;
  155. }
  156. static bool should_restart_cycle(const struct taprio_sched *q,
  157. const struct sched_entry *entry)
  158. {
  159. WARN_ON(!entry);
  160. return list_is_last(&entry->list, &q->entries);
  161. }
  162. static enum hrtimer_restart advance_sched(struct hrtimer *timer)
  163. {
  164. struct taprio_sched *q = container_of(timer, struct taprio_sched,
  165. advance_timer);
  166. struct sched_entry *entry, *next;
  167. struct Qdisc *sch = q->root;
  168. ktime_t close_time;
  169. spin_lock(&q->current_entry_lock);
  170. entry = rcu_dereference_protected(q->current_entry,
  171. lockdep_is_held(&q->current_entry_lock));
  172. /* This is the case that it's the first time that the schedule
  173. * runs, so it only happens once per schedule. The first entry
  174. * is pre-calculated during the schedule initialization.
  175. */
  176. if (unlikely(!entry)) {
  177. next = list_first_entry(&q->entries, struct sched_entry,
  178. list);
  179. close_time = next->close_time;
  180. goto first_run;
  181. }
  182. if (should_restart_cycle(q, entry))
  183. next = list_first_entry(&q->entries, struct sched_entry,
  184. list);
  185. else
  186. next = list_next_entry(entry, list);
  187. close_time = ktime_add_ns(entry->close_time, next->interval);
  188. next->close_time = close_time;
  189. atomic_set(&next->budget,
  190. (next->interval * 1000) / q->picos_per_byte);
  191. first_run:
  192. rcu_assign_pointer(q->current_entry, next);
  193. spin_unlock(&q->current_entry_lock);
  194. hrtimer_set_expires(&q->advance_timer, close_time);
  195. rcu_read_lock();
  196. __netif_schedule(sch);
  197. rcu_read_unlock();
  198. return HRTIMER_RESTART;
  199. }
  200. static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
  201. [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
  202. [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
  203. [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
  204. [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
  205. };
  206. static const struct nla_policy entry_list_policy[TCA_TAPRIO_SCHED_MAX + 1] = {
  207. [TCA_TAPRIO_SCHED_ENTRY] = { .type = NLA_NESTED },
  208. };
  209. static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
  210. [TCA_TAPRIO_ATTR_PRIOMAP] = {
  211. .len = sizeof(struct tc_mqprio_qopt)
  212. },
  213. [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
  214. [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
  215. [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
  216. [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
  217. };
  218. static int fill_sched_entry(struct nlattr **tb, struct sched_entry *entry,
  219. struct netlink_ext_ack *extack)
  220. {
  221. u32 interval = 0;
  222. if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
  223. entry->command = nla_get_u8(
  224. tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
  225. if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
  226. entry->gate_mask = nla_get_u32(
  227. tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
  228. if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
  229. interval = nla_get_u32(
  230. tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
  231. if (interval == 0) {
  232. NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
  233. return -EINVAL;
  234. }
  235. entry->interval = interval;
  236. return 0;
  237. }
  238. static int parse_sched_entry(struct nlattr *n, struct sched_entry *entry,
  239. int index, struct netlink_ext_ack *extack)
  240. {
  241. struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
  242. int err;
  243. err = nla_parse_nested(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
  244. entry_policy, NULL);
  245. if (err < 0) {
  246. NL_SET_ERR_MSG(extack, "Could not parse nested entry");
  247. return -EINVAL;
  248. }
  249. entry->index = index;
  250. return fill_sched_entry(tb, entry, extack);
  251. }
  252. /* Returns the number of entries in case of success */
  253. static int parse_sched_single_entry(struct nlattr *n,
  254. struct taprio_sched *q,
  255. struct netlink_ext_ack *extack)
  256. {
  257. struct nlattr *tb_entry[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
  258. struct nlattr *tb_list[TCA_TAPRIO_SCHED_MAX + 1] = { };
  259. struct sched_entry *entry;
  260. bool found = false;
  261. u32 index;
  262. int err;
  263. err = nla_parse_nested(tb_list, TCA_TAPRIO_SCHED_MAX,
  264. n, entry_list_policy, NULL);
  265. if (err < 0) {
  266. NL_SET_ERR_MSG(extack, "Could not parse nested entry");
  267. return -EINVAL;
  268. }
  269. if (!tb_list[TCA_TAPRIO_SCHED_ENTRY]) {
  270. NL_SET_ERR_MSG(extack, "Single-entry must include an entry");
  271. return -EINVAL;
  272. }
  273. err = nla_parse_nested(tb_entry, TCA_TAPRIO_SCHED_ENTRY_MAX,
  274. tb_list[TCA_TAPRIO_SCHED_ENTRY],
  275. entry_policy, NULL);
  276. if (err < 0) {
  277. NL_SET_ERR_MSG(extack, "Could not parse nested entry");
  278. return -EINVAL;
  279. }
  280. if (!tb_entry[TCA_TAPRIO_SCHED_ENTRY_INDEX]) {
  281. NL_SET_ERR_MSG(extack, "Entry must specify an index\n");
  282. return -EINVAL;
  283. }
  284. index = nla_get_u32(tb_entry[TCA_TAPRIO_SCHED_ENTRY_INDEX]);
  285. if (index >= q->num_entries) {
  286. NL_SET_ERR_MSG(extack, "Index for single entry exceeds number of entries in schedule");
  287. return -EINVAL;
  288. }
  289. list_for_each_entry(entry, &q->entries, list) {
  290. if (entry->index == index) {
  291. found = true;
  292. break;
  293. }
  294. }
  295. if (!found) {
  296. NL_SET_ERR_MSG(extack, "Could not find entry");
  297. return -ENOENT;
  298. }
  299. err = fill_sched_entry(tb_entry, entry, extack);
  300. if (err < 0)
  301. return err;
  302. return q->num_entries;
  303. }
  304. static int parse_sched_list(struct nlattr *list,
  305. struct taprio_sched *q,
  306. struct netlink_ext_ack *extack)
  307. {
  308. struct nlattr *n;
  309. int err, rem;
  310. int i = 0;
  311. if (!list)
  312. return -EINVAL;
  313. nla_for_each_nested(n, list, rem) {
  314. struct sched_entry *entry;
  315. if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
  316. NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
  317. continue;
  318. }
  319. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  320. if (!entry) {
  321. NL_SET_ERR_MSG(extack, "Not enough memory for entry");
  322. return -ENOMEM;
  323. }
  324. err = parse_sched_entry(n, entry, i, extack);
  325. if (err < 0) {
  326. kfree(entry);
  327. return err;
  328. }
  329. list_add_tail(&entry->list, &q->entries);
  330. i++;
  331. }
  332. q->num_entries = i;
  333. return i;
  334. }
  335. /* Returns the number of entries in case of success */
  336. static int parse_taprio_opt(struct nlattr **tb, struct taprio_sched *q,
  337. struct netlink_ext_ack *extack)
  338. {
  339. int err = 0;
  340. int clockid;
  341. if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] &&
  342. tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY])
  343. return -EINVAL;
  344. if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] && q->num_entries == 0)
  345. return -EINVAL;
  346. if (q->clockid == -1 && !tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID])
  347. return -EINVAL;
  348. if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
  349. q->base_time = nla_get_s64(
  350. tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
  351. if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
  352. clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
  353. /* We only support static clockids and we don't allow
  354. * for it to be modified after the first init.
  355. */
  356. if (clockid < 0 || (q->clockid != -1 && q->clockid != clockid))
  357. return -EINVAL;
  358. q->clockid = clockid;
  359. }
  360. if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
  361. err = parse_sched_list(
  362. tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST], q, extack);
  363. else if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY])
  364. err = parse_sched_single_entry(
  365. tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY], q, extack);
  366. /* parse_sched_* return the number of entries in the schedule,
  367. * a schedule with zero entries is an error.
  368. */
  369. if (err == 0) {
  370. NL_SET_ERR_MSG(extack, "The schedule should contain at least one entry");
  371. return -EINVAL;
  372. }
  373. return err;
  374. }
  375. static int taprio_parse_mqprio_opt(struct net_device *dev,
  376. struct tc_mqprio_qopt *qopt,
  377. struct netlink_ext_ack *extack)
  378. {
  379. int i, j;
  380. if (!qopt) {
  381. NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
  382. return -EINVAL;
  383. }
  384. /* Verify num_tc is not out of max range */
  385. if (qopt->num_tc > TC_MAX_QUEUE) {
  386. NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
  387. return -EINVAL;
  388. }
  389. /* taprio imposes that traffic classes map 1:n to tx queues */
  390. if (qopt->num_tc > dev->num_tx_queues) {
  391. NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
  392. return -EINVAL;
  393. }
  394. /* Verify priority mapping uses valid tcs */
  395. for (i = 0; i < TC_BITMASK + 1; i++) {
  396. if (qopt->prio_tc_map[i] >= qopt->num_tc) {
  397. NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
  398. return -EINVAL;
  399. }
  400. }
  401. for (i = 0; i < qopt->num_tc; i++) {
  402. unsigned int last = qopt->offset[i] + qopt->count[i];
  403. /* Verify the queue count is in tx range being equal to the
  404. * real_num_tx_queues indicates the last queue is in use.
  405. */
  406. if (qopt->offset[i] >= dev->num_tx_queues ||
  407. !qopt->count[i] ||
  408. last > dev->real_num_tx_queues) {
  409. NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
  410. return -EINVAL;
  411. }
  412. /* Verify that the offset and counts do not overlap */
  413. for (j = i + 1; j < qopt->num_tc; j++) {
  414. if (last > qopt->offset[j]) {
  415. NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
  416. return -EINVAL;
  417. }
  418. }
  419. }
  420. return 0;
  421. }
  422. static ktime_t taprio_get_start_time(struct Qdisc *sch)
  423. {
  424. struct taprio_sched *q = qdisc_priv(sch);
  425. struct sched_entry *entry;
  426. ktime_t now, base, cycle;
  427. s64 n;
  428. base = ns_to_ktime(q->base_time);
  429. cycle = 0;
  430. /* Calculate the cycle_time, by summing all the intervals.
  431. */
  432. list_for_each_entry(entry, &q->entries, list)
  433. cycle = ktime_add_ns(cycle, entry->interval);
  434. if (!cycle)
  435. return base;
  436. now = q->get_time();
  437. if (ktime_after(base, now))
  438. return base;
  439. /* Schedule the start time for the beginning of the next
  440. * cycle.
  441. */
  442. n = div64_s64(ktime_sub_ns(now, base), cycle);
  443. return ktime_add_ns(base, (n + 1) * cycle);
  444. }
  445. static void taprio_start_sched(struct Qdisc *sch, ktime_t start)
  446. {
  447. struct taprio_sched *q = qdisc_priv(sch);
  448. struct sched_entry *first;
  449. unsigned long flags;
  450. spin_lock_irqsave(&q->current_entry_lock, flags);
  451. first = list_first_entry(&q->entries, struct sched_entry,
  452. list);
  453. first->close_time = ktime_add_ns(start, first->interval);
  454. atomic_set(&first->budget,
  455. (first->interval * 1000) / q->picos_per_byte);
  456. rcu_assign_pointer(q->current_entry, NULL);
  457. spin_unlock_irqrestore(&q->current_entry_lock, flags);
  458. hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
  459. }
  460. static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
  461. struct netlink_ext_ack *extack)
  462. {
  463. struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
  464. struct taprio_sched *q = qdisc_priv(sch);
  465. struct net_device *dev = qdisc_dev(sch);
  466. struct tc_mqprio_qopt *mqprio = NULL;
  467. struct ethtool_link_ksettings ecmd;
  468. int i, err, size;
  469. s64 link_speed;
  470. ktime_t start;
  471. err = nla_parse_nested(tb, TCA_TAPRIO_ATTR_MAX, opt,
  472. taprio_policy, extack);
  473. if (err < 0)
  474. return err;
  475. err = -EINVAL;
  476. if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
  477. mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
  478. err = taprio_parse_mqprio_opt(dev, mqprio, extack);
  479. if (err < 0)
  480. return err;
  481. /* A schedule with less than one entry is an error */
  482. size = parse_taprio_opt(tb, q, extack);
  483. if (size < 0)
  484. return size;
  485. hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
  486. q->advance_timer.function = advance_sched;
  487. switch (q->clockid) {
  488. case CLOCK_REALTIME:
  489. q->get_time = ktime_get_real;
  490. break;
  491. case CLOCK_MONOTONIC:
  492. q->get_time = ktime_get;
  493. break;
  494. case CLOCK_BOOTTIME:
  495. q->get_time = ktime_get_boottime;
  496. break;
  497. case CLOCK_TAI:
  498. q->get_time = ktime_get_clocktai;
  499. break;
  500. default:
  501. return -ENOTSUPP;
  502. }
  503. for (i = 0; i < dev->num_tx_queues; i++) {
  504. struct netdev_queue *dev_queue;
  505. struct Qdisc *qdisc;
  506. dev_queue = netdev_get_tx_queue(dev, i);
  507. qdisc = qdisc_create_dflt(dev_queue,
  508. &pfifo_qdisc_ops,
  509. TC_H_MAKE(TC_H_MAJ(sch->handle),
  510. TC_H_MIN(i + 1)),
  511. extack);
  512. if (!qdisc)
  513. return -ENOMEM;
  514. if (i < dev->real_num_tx_queues)
  515. qdisc_hash_add(qdisc, false);
  516. q->qdiscs[i] = qdisc;
  517. }
  518. if (mqprio) {
  519. netdev_set_num_tc(dev, mqprio->num_tc);
  520. for (i = 0; i < mqprio->num_tc; i++)
  521. netdev_set_tc_queue(dev, i,
  522. mqprio->count[i],
  523. mqprio->offset[i]);
  524. /* Always use supplied priority mappings */
  525. for (i = 0; i < TC_BITMASK + 1; i++)
  526. netdev_set_prio_tc_map(dev, i,
  527. mqprio->prio_tc_map[i]);
  528. }
  529. if (!__ethtool_get_link_ksettings(dev, &ecmd))
  530. link_speed = ecmd.base.speed;
  531. else
  532. link_speed = SPEED_1000;
  533. q->picos_per_byte = div64_s64(NSEC_PER_SEC * 1000LL * 8,
  534. link_speed * 1000 * 1000);
  535. start = taprio_get_start_time(sch);
  536. if (!start)
  537. return 0;
  538. taprio_start_sched(sch, start);
  539. return 0;
  540. }
  541. static void taprio_destroy(struct Qdisc *sch)
  542. {
  543. struct taprio_sched *q = qdisc_priv(sch);
  544. struct net_device *dev = qdisc_dev(sch);
  545. struct sched_entry *entry, *n;
  546. unsigned int i;
  547. hrtimer_cancel(&q->advance_timer);
  548. if (q->qdiscs) {
  549. for (i = 0; i < dev->num_tx_queues && q->qdiscs[i]; i++)
  550. qdisc_put(q->qdiscs[i]);
  551. kfree(q->qdiscs);
  552. }
  553. q->qdiscs = NULL;
  554. netdev_set_num_tc(dev, 0);
  555. list_for_each_entry_safe(entry, n, &q->entries, list) {
  556. list_del(&entry->list);
  557. kfree(entry);
  558. }
  559. }
  560. static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
  561. struct netlink_ext_ack *extack)
  562. {
  563. struct taprio_sched *q = qdisc_priv(sch);
  564. struct net_device *dev = qdisc_dev(sch);
  565. INIT_LIST_HEAD(&q->entries);
  566. spin_lock_init(&q->current_entry_lock);
  567. /* We may overwrite the configuration later */
  568. hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
  569. q->root = sch;
  570. /* We only support static clockids. Use an invalid value as default
  571. * and get the valid one on taprio_change().
  572. */
  573. q->clockid = -1;
  574. if (sch->parent != TC_H_ROOT)
  575. return -EOPNOTSUPP;
  576. if (!netif_is_multiqueue(dev))
  577. return -EOPNOTSUPP;
  578. /* pre-allocate qdisc, attachment can't fail */
  579. q->qdiscs = kcalloc(dev->num_tx_queues,
  580. sizeof(q->qdiscs[0]),
  581. GFP_KERNEL);
  582. if (!q->qdiscs)
  583. return -ENOMEM;
  584. if (!opt)
  585. return -EINVAL;
  586. return taprio_change(sch, opt, extack);
  587. }
  588. static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
  589. unsigned long cl)
  590. {
  591. struct net_device *dev = qdisc_dev(sch);
  592. unsigned long ntx = cl - 1;
  593. if (ntx >= dev->num_tx_queues)
  594. return NULL;
  595. return netdev_get_tx_queue(dev, ntx);
  596. }
  597. static int taprio_graft(struct Qdisc *sch, unsigned long cl,
  598. struct Qdisc *new, struct Qdisc **old,
  599. struct netlink_ext_ack *extack)
  600. {
  601. struct taprio_sched *q = qdisc_priv(sch);
  602. struct net_device *dev = qdisc_dev(sch);
  603. struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
  604. if (!dev_queue)
  605. return -EINVAL;
  606. if (dev->flags & IFF_UP)
  607. dev_deactivate(dev);
  608. *old = q->qdiscs[cl - 1];
  609. q->qdiscs[cl - 1] = new;
  610. if (new)
  611. new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
  612. if (dev->flags & IFF_UP)
  613. dev_activate(dev);
  614. return 0;
  615. }
  616. static int dump_entry(struct sk_buff *msg,
  617. const struct sched_entry *entry)
  618. {
  619. struct nlattr *item;
  620. item = nla_nest_start(msg, TCA_TAPRIO_SCHED_ENTRY);
  621. if (!item)
  622. return -ENOSPC;
  623. if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
  624. goto nla_put_failure;
  625. if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
  626. goto nla_put_failure;
  627. if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
  628. entry->gate_mask))
  629. goto nla_put_failure;
  630. if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
  631. entry->interval))
  632. goto nla_put_failure;
  633. return nla_nest_end(msg, item);
  634. nla_put_failure:
  635. nla_nest_cancel(msg, item);
  636. return -1;
  637. }
  638. static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
  639. {
  640. struct taprio_sched *q = qdisc_priv(sch);
  641. struct net_device *dev = qdisc_dev(sch);
  642. struct tc_mqprio_qopt opt = { 0 };
  643. struct nlattr *nest, *entry_list;
  644. struct sched_entry *entry;
  645. unsigned int i;
  646. opt.num_tc = netdev_get_num_tc(dev);
  647. memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
  648. for (i = 0; i < netdev_get_num_tc(dev); i++) {
  649. opt.count[i] = dev->tc_to_txq[i].count;
  650. opt.offset[i] = dev->tc_to_txq[i].offset;
  651. }
  652. nest = nla_nest_start(skb, TCA_OPTIONS);
  653. if (!nest)
  654. return -ENOSPC;
  655. if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
  656. goto options_error;
  657. if (nla_put_s64(skb, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
  658. q->base_time, TCA_TAPRIO_PAD))
  659. goto options_error;
  660. if (nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
  661. goto options_error;
  662. entry_list = nla_nest_start(skb, TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
  663. if (!entry_list)
  664. goto options_error;
  665. list_for_each_entry(entry, &q->entries, list) {
  666. if (dump_entry(skb, entry) < 0)
  667. goto options_error;
  668. }
  669. nla_nest_end(skb, entry_list);
  670. return nla_nest_end(skb, nest);
  671. options_error:
  672. nla_nest_cancel(skb, nest);
  673. return -1;
  674. }
  675. static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
  676. {
  677. struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
  678. if (!dev_queue)
  679. return NULL;
  680. return dev_queue->qdisc_sleeping;
  681. }
  682. static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
  683. {
  684. unsigned int ntx = TC_H_MIN(classid);
  685. if (!taprio_queue_get(sch, ntx))
  686. return 0;
  687. return ntx;
  688. }
  689. static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
  690. struct sk_buff *skb, struct tcmsg *tcm)
  691. {
  692. struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
  693. tcm->tcm_parent = TC_H_ROOT;
  694. tcm->tcm_handle |= TC_H_MIN(cl);
  695. tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
  696. return 0;
  697. }
  698. static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  699. struct gnet_dump *d)
  700. __releases(d->lock)
  701. __acquires(d->lock)
  702. {
  703. struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
  704. sch = dev_queue->qdisc_sleeping;
  705. if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 ||
  706. gnet_stats_copy_queue(d, NULL, &sch->qstats, sch->q.qlen) < 0)
  707. return -1;
  708. return 0;
  709. }
  710. static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  711. {
  712. struct net_device *dev = qdisc_dev(sch);
  713. unsigned long ntx;
  714. if (arg->stop)
  715. return;
  716. arg->count = arg->skip;
  717. for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
  718. if (arg->fn(sch, ntx + 1, arg) < 0) {
  719. arg->stop = 1;
  720. break;
  721. }
  722. arg->count++;
  723. }
  724. }
  725. static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
  726. struct tcmsg *tcm)
  727. {
  728. return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
  729. }
  730. static const struct Qdisc_class_ops taprio_class_ops = {
  731. .graft = taprio_graft,
  732. .leaf = taprio_leaf,
  733. .find = taprio_find,
  734. .walk = taprio_walk,
  735. .dump = taprio_dump_class,
  736. .dump_stats = taprio_dump_class_stats,
  737. .select_queue = taprio_select_queue,
  738. };
  739. static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
  740. .cl_ops = &taprio_class_ops,
  741. .id = "taprio",
  742. .priv_size = sizeof(struct taprio_sched),
  743. .init = taprio_init,
  744. .destroy = taprio_destroy,
  745. .peek = taprio_peek,
  746. .dequeue = taprio_dequeue,
  747. .enqueue = taprio_enqueue,
  748. .dump = taprio_dump,
  749. .owner = THIS_MODULE,
  750. };
  751. static int __init taprio_module_init(void)
  752. {
  753. return register_qdisc(&taprio_qdisc_ops);
  754. }
  755. static void __exit taprio_module_exit(void)
  756. {
  757. unregister_qdisc(&taprio_qdisc_ops);
  758. }
  759. module_init(taprio_module_init);
  760. module_exit(taprio_module_exit);
  761. MODULE_LICENSE("GPL");