1 // SPDX-License-Identifier: GPL-2.0
3 /* net/sched/sch_taprio.c Time Aware Priority Scheduler
5 * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com>
9 #include <linux/ethtool.h>
10 #include <linux/ethtool_netlink.h>
11 #include <linux/types.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/list.h>
16 #include <linux/errno.h>
17 #include <linux/skbuff.h>
18 #include <linux/math64.h>
19 #include <linux/module.h>
20 #include <linux/spinlock.h>
21 #include <linux/rcupdate.h>
22 #include <linux/time.h>
23 #include <net/netlink.h>
24 #include <net/pkt_sched.h>
25 #include <net/pkt_cls.h>
26 #include <net/sch_generic.h>
30 #include "sch_mqprio_lib.h"
32 static LIST_HEAD(taprio_list);
33 static struct static_key_false taprio_have_broken_mqprio;
34 static struct static_key_false taprio_have_working_mqprio;
36 #define TAPRIO_ALL_GATES_OPEN -1
38 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
39 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
40 #define TAPRIO_FLAGS_INVALID U32_MAX
43 /* Durations between this GCL entry and the GCL entry where the
44 * respective traffic class gate closes
46 u64 gate_duration[TC_MAX_QUEUE];
47 atomic_t budget[TC_MAX_QUEUE];
48 /* The qdisc makes some effort so that no packet leaves
51 ktime_t gate_close_time[TC_MAX_QUEUE];
52 struct list_head list;
53 /* Used to calculate when to advance the schedule */
62 struct sched_gate_list {
63 /* Longest non-zero contiguous gate durations per traffic class,
64 * or 0 if a traffic class gate never opens during the schedule.
66 u64 max_open_gate_duration[TC_MAX_QUEUE];
67 u32 max_frm_len[TC_MAX_QUEUE]; /* for the fast path */
68 u32 max_sdu[TC_MAX_QUEUE]; /* for dump */
70 struct list_head entries;
72 ktime_t cycle_end_time;
74 s64 cycle_time_extension;
79 struct Qdisc **qdiscs;
82 enum tk_offsets tk_offset;
87 atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
88 * speeds it's sub-nanoseconds per byte
91 /* Protects the update side of the RCU protected current_entry */
92 spinlock_t current_entry_lock;
93 struct sched_entry __rcu *current_entry;
94 struct sched_gate_list __rcu *oper_sched;
95 struct sched_gate_list __rcu *admin_sched;
96 struct hrtimer advance_timer;
97 struct list_head taprio_list;
98 int cur_txq[TC_MAX_QUEUE];
99 u32 max_sdu[TC_MAX_QUEUE]; /* save info from the user */
100 u32 fp[TC_QOPT_MAX_QUEUE]; /* only for dump and offloading */
104 struct __tc_taprio_qopt_offload {
106 struct tc_taprio_qopt_offload offload;
109 static void taprio_calculate_gate_durations(struct taprio_sched *q,
110 struct sched_gate_list *sched)
112 struct net_device *dev = qdisc_dev(q->root);
113 int num_tc = netdev_get_num_tc(dev);
114 struct sched_entry *entry, *cur;
117 list_for_each_entry(entry, &sched->entries, list) {
118 u32 gates_still_open = entry->gate_mask;
120 /* For each traffic class, calculate each open gate duration,
121 * starting at this schedule entry and ending at the schedule
122 * entry containing a gate close event for that TC.
127 if (!gates_still_open)
130 for (tc = 0; tc < num_tc; tc++) {
131 if (!(gates_still_open & BIT(tc)))
134 if (cur->gate_mask & BIT(tc))
135 entry->gate_duration[tc] += cur->interval;
137 gates_still_open &= ~BIT(tc);
140 cur = list_next_entry_circular(cur, &sched->entries, list);
141 } while (cur != entry);
143 /* Keep track of the maximum gate duration for each traffic
144 * class, taking care to not confuse a traffic class which is
145 * temporarily closed with one that is always closed.
147 for (tc = 0; tc < num_tc; tc++)
148 if (entry->gate_duration[tc] &&
149 sched->max_open_gate_duration[tc] < entry->gate_duration[tc])
150 sched->max_open_gate_duration[tc] = entry->gate_duration[tc];
154 static bool taprio_entry_allows_tx(ktime_t skb_end_time,
155 struct sched_entry *entry, int tc)
157 return ktime_before(skb_end_time, entry->gate_close_time[tc]);
160 static ktime_t sched_base_time(const struct sched_gate_list *sched)
165 return ns_to_ktime(sched->base_time);
168 static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono)
170 /* This pairs with WRITE_ONCE() in taprio_parse_clockid() */
171 enum tk_offsets tk_offset = READ_ONCE(q->tk_offset);
177 return ktime_mono_to_any(mono, tk_offset);
181 static ktime_t taprio_get_time(const struct taprio_sched *q)
183 return taprio_mono_to_any(q, ktime_get());
186 static void taprio_free_sched_cb(struct rcu_head *head)
188 struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
189 struct sched_entry *entry, *n;
191 list_for_each_entry_safe(entry, n, &sched->entries, list) {
192 list_del(&entry->list);
199 static void switch_schedules(struct taprio_sched *q,
200 struct sched_gate_list **admin,
201 struct sched_gate_list **oper)
203 rcu_assign_pointer(q->oper_sched, *admin);
204 rcu_assign_pointer(q->admin_sched, NULL);
207 call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
213 /* Get how much time has been already elapsed in the current cycle. */
214 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
216 ktime_t time_since_sched_start;
219 time_since_sched_start = ktime_sub(time, sched->base_time);
220 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
225 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
226 struct sched_gate_list *admin,
227 struct sched_entry *entry,
230 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
231 ktime_t intv_end, cycle_ext_end, cycle_end;
233 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
234 intv_end = ktime_add_ns(intv_start, entry->interval);
235 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
237 if (ktime_before(intv_end, cycle_end))
239 else if (admin && admin != sched &&
240 ktime_after(admin->base_time, cycle_end) &&
241 ktime_before(admin->base_time, cycle_ext_end))
242 return admin->base_time;
247 static int length_to_duration(struct taprio_sched *q, int len)
249 return div_u64(len * atomic64_read(&q->picos_per_byte), PSEC_PER_NSEC);
252 static int duration_to_length(struct taprio_sched *q, u64 duration)
254 return div_u64(duration * PSEC_PER_NSEC, atomic64_read(&q->picos_per_byte));
257 /* Sets sched->max_sdu[] and sched->max_frm_len[] to the minimum between the
258 * q->max_sdu[] requested by the user and the max_sdu dynamically determined by
259 * the maximum open gate durations at the given link speed.
261 static void taprio_update_queue_max_sdu(struct taprio_sched *q,
262 struct sched_gate_list *sched,
263 struct qdisc_size_table *stab)
265 struct net_device *dev = qdisc_dev(q->root);
266 int num_tc = netdev_get_num_tc(dev);
267 u32 max_sdu_from_user;
272 for (tc = 0; tc < num_tc; tc++) {
273 max_sdu_from_user = q->max_sdu[tc] ?: U32_MAX;
275 /* TC gate never closes => keep the queueMaxSDU
276 * selected by the user
278 if (sched->max_open_gate_duration[tc] == sched->cycle_time) {
279 max_sdu_dynamic = U32_MAX;
283 max_frm_len = duration_to_length(q, sched->max_open_gate_duration[tc]);
284 /* Compensate for L1 overhead from size table,
285 * but don't let the frame size go negative
288 max_frm_len -= stab->szopts.overhead;
289 max_frm_len = max_t(int, max_frm_len,
290 dev->hard_header_len + 1);
292 max_sdu_dynamic = max_frm_len - dev->hard_header_len;
293 if (max_sdu_dynamic > dev->max_mtu)
294 max_sdu_dynamic = U32_MAX;
297 max_sdu = min(max_sdu_dynamic, max_sdu_from_user);
299 if (max_sdu != U32_MAX) {
300 sched->max_frm_len[tc] = max_sdu + dev->hard_header_len;
301 sched->max_sdu[tc] = max_sdu;
303 sched->max_frm_len[tc] = U32_MAX; /* never oversized */
304 sched->max_sdu[tc] = 0;
309 /* Returns the entry corresponding to next available interval. If
310 * validate_interval is set, it only validates whether the timestamp occurs
311 * when the gate corresponding to the skb's traffic class is open.
313 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
315 struct sched_gate_list *sched,
316 struct sched_gate_list *admin,
318 ktime_t *interval_start,
319 ktime_t *interval_end,
320 bool validate_interval)
322 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
323 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
324 struct sched_entry *entry = NULL, *entry_found = NULL;
325 struct taprio_sched *q = qdisc_priv(sch);
326 struct net_device *dev = qdisc_dev(sch);
327 bool entry_available = false;
331 tc = netdev_get_prio_tc_map(dev, skb->priority);
332 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
340 cycle = sched->cycle_time;
341 cycle_elapsed = get_cycle_time_elapsed(sched, time);
342 curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
343 cycle_end = ktime_add_ns(curr_intv_end, cycle);
345 list_for_each_entry(entry, &sched->entries, list) {
346 curr_intv_start = curr_intv_end;
347 curr_intv_end = get_interval_end_time(sched, admin, entry,
350 if (ktime_after(curr_intv_start, cycle_end))
353 if (!(entry->gate_mask & BIT(tc)) ||
354 packet_transmit_time > entry->interval)
357 txtime = entry->next_txtime;
359 if (ktime_before(txtime, time) || validate_interval) {
360 transmit_end_time = ktime_add_ns(time, packet_transmit_time);
361 if ((ktime_before(curr_intv_start, time) &&
362 ktime_before(transmit_end_time, curr_intv_end)) ||
363 (ktime_after(curr_intv_start, time) && !validate_interval)) {
365 *interval_start = curr_intv_start;
366 *interval_end = curr_intv_end;
368 } else if (!entry_available && !validate_interval) {
369 /* Here, we are just trying to find out the
370 * first available interval in the next cycle.
372 entry_available = true;
374 *interval_start = ktime_add_ns(curr_intv_start, cycle);
375 *interval_end = ktime_add_ns(curr_intv_end, cycle);
377 } else if (ktime_before(txtime, earliest_txtime) &&
379 earliest_txtime = txtime;
381 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
382 *interval_start = ktime_add(curr_intv_start, n * cycle);
383 *interval_end = ktime_add(curr_intv_end, n * cycle);
390 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
392 struct taprio_sched *q = qdisc_priv(sch);
393 struct sched_gate_list *sched, *admin;
394 ktime_t interval_start, interval_end;
395 struct sched_entry *entry;
398 sched = rcu_dereference(q->oper_sched);
399 admin = rcu_dereference(q->admin_sched);
401 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
402 &interval_start, &interval_end, true);
408 static bool taprio_flags_valid(u32 flags)
410 /* Make sure no other flag bits are set. */
411 if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST |
412 TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
414 /* txtime-assist and full offload are mutually exclusive */
415 if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
416 (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
421 /* This returns the tstamp value set by TCP in terms of the set clock. */
422 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
424 unsigned int offset = skb_network_offset(skb);
425 const struct ipv6hdr *ipv6h;
426 const struct iphdr *iph;
427 struct ipv6hdr _ipv6h;
429 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
433 if (ipv6h->version == 4) {
434 iph = (struct iphdr *)ipv6h;
435 offset += iph->ihl * 4;
437 /* special-case 6in4 tunnelling, as that is a common way to get
438 * v6 connectivity in the home
440 if (iph->protocol == IPPROTO_IPV6) {
441 ipv6h = skb_header_pointer(skb, offset,
442 sizeof(_ipv6h), &_ipv6h);
444 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
446 } else if (iph->protocol != IPPROTO_TCP) {
449 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
453 return taprio_mono_to_any(q, skb->skb_mstamp_ns);
456 /* There are a few scenarios where we will have to modify the txtime from
457 * what is read from next_txtime in sched_entry. They are:
458 * 1. If txtime is in the past,
459 * a. The gate for the traffic class is currently open and packet can be
460 * transmitted before it closes, schedule the packet right away.
461 * b. If the gate corresponding to the traffic class is going to open later
462 * in the cycle, set the txtime of packet to the interval start.
463 * 2. If txtime is in the future, there are packets corresponding to the
464 * current traffic class waiting to be transmitted. So, the following
465 * possibilities exist:
466 * a. We can transmit the packet before the window containing the txtime
468 * b. The window might close before the transmission can be completed
469 * successfully. So, schedule the packet in the next open window.
471 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
473 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
474 struct taprio_sched *q = qdisc_priv(sch);
475 struct sched_gate_list *sched, *admin;
476 ktime_t minimum_time, now, txtime;
477 int len, packet_transmit_time;
478 struct sched_entry *entry;
481 now = taprio_get_time(q);
482 minimum_time = ktime_add_ns(now, q->txtime_delay);
484 tcp_tstamp = get_tcp_tstamp(q, skb);
485 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
488 admin = rcu_dereference(q->admin_sched);
489 sched = rcu_dereference(q->oper_sched);
490 if (admin && ktime_after(minimum_time, admin->base_time))
491 switch_schedules(q, &admin, &sched);
493 /* Until the schedule starts, all the queues are open */
494 if (!sched || ktime_before(minimum_time, sched->base_time)) {
495 txtime = minimum_time;
499 len = qdisc_pkt_len(skb);
500 packet_transmit_time = length_to_duration(q, len);
503 sched_changed = false;
505 entry = find_entry_to_transmit(skb, sch, sched, admin,
507 &interval_start, &interval_end,
514 txtime = entry->next_txtime;
515 txtime = max_t(ktime_t, txtime, minimum_time);
516 txtime = max_t(ktime_t, txtime, interval_start);
518 if (admin && admin != sched &&
519 ktime_after(txtime, admin->base_time)) {
521 sched_changed = true;
525 transmit_end_time = ktime_add(txtime, packet_transmit_time);
526 minimum_time = transmit_end_time;
528 /* Update the txtime of current entry to the next time it's
531 if (ktime_after(transmit_end_time, interval_end))
532 entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
533 } while (sched_changed || ktime_after(transmit_end_time, interval_end));
535 entry->next_txtime = transmit_end_time;
542 /* Devices with full offload are expected to honor this in hardware */
543 static bool taprio_skb_exceeds_queue_max_sdu(struct Qdisc *sch,
546 struct taprio_sched *q = qdisc_priv(sch);
547 struct net_device *dev = qdisc_dev(sch);
548 struct sched_gate_list *sched;
549 int prio = skb->priority;
550 bool exceeds = false;
553 tc = netdev_get_prio_tc_map(dev, prio);
556 sched = rcu_dereference(q->oper_sched);
557 if (sched && skb->len > sched->max_frm_len[tc])
564 static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
565 struct Qdisc *child, struct sk_buff **to_free)
567 struct taprio_sched *q = qdisc_priv(sch);
569 /* sk_flags are only safe to use on full sockets. */
570 if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) {
571 if (!is_valid_interval(skb, sch))
572 return qdisc_drop(skb, sch, to_free);
573 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
574 skb->tstamp = get_packet_txtime(skb, sch);
576 return qdisc_drop(skb, sch, to_free);
579 qdisc_qstats_backlog_inc(sch, skb);
582 return qdisc_enqueue(skb, child, to_free);
585 static int taprio_enqueue_segmented(struct sk_buff *skb, struct Qdisc *sch,
587 struct sk_buff **to_free)
589 unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb);
590 netdev_features_t features = netif_skb_features(skb);
591 struct sk_buff *segs, *nskb;
594 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
595 if (IS_ERR_OR_NULL(segs))
596 return qdisc_drop(skb, sch, to_free);
598 skb_list_walk_safe(segs, segs, nskb) {
599 skb_mark_not_on_list(segs);
600 qdisc_skb_cb(segs)->pkt_len = segs->len;
603 /* FIXME: we should be segmenting to a smaller size
604 * rather than dropping these
606 if (taprio_skb_exceeds_queue_max_sdu(sch, segs))
607 ret = qdisc_drop(segs, sch, to_free);
609 ret = taprio_enqueue_one(segs, sch, child, to_free);
611 if (ret != NET_XMIT_SUCCESS) {
612 if (net_xmit_drop_count(ret))
613 qdisc_qstats_drop(sch);
620 qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
623 return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
626 /* Will not be called in the full offload case, since the TX queues are
627 * attached to the Qdisc created using qdisc_create_dflt()
629 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
630 struct sk_buff **to_free)
632 struct taprio_sched *q = qdisc_priv(sch);
636 queue = skb_get_queue_mapping(skb);
638 child = q->qdiscs[queue];
639 if (unlikely(!child))
640 return qdisc_drop(skb, sch, to_free);
642 if (taprio_skb_exceeds_queue_max_sdu(sch, skb)) {
643 /* Large packets might not be transmitted when the transmission
644 * duration exceeds any configured interval. Therefore, segment
645 * the skb into smaller chunks. Drivers with full offload are
646 * expected to handle this in hardware.
649 return taprio_enqueue_segmented(skb, sch, child,
652 return qdisc_drop(skb, sch, to_free);
655 return taprio_enqueue_one(skb, sch, child, to_free);
658 static struct sk_buff *taprio_peek(struct Qdisc *sch)
660 WARN_ONCE(1, "taprio only supports operating as root qdisc, peek() not implemented");
664 static void taprio_set_budgets(struct taprio_sched *q,
665 struct sched_gate_list *sched,
666 struct sched_entry *entry)
668 struct net_device *dev = qdisc_dev(q->root);
669 int num_tc = netdev_get_num_tc(dev);
672 for (tc = 0; tc < num_tc; tc++) {
673 /* Traffic classes which never close have infinite budget */
674 if (entry->gate_duration[tc] == sched->cycle_time)
677 budget = div64_u64((u64)entry->gate_duration[tc] * PSEC_PER_NSEC,
678 atomic64_read(&q->picos_per_byte));
680 atomic_set(&entry->budget[tc], budget);
684 /* When an skb is sent, it consumes from the budget of all traffic classes */
685 static int taprio_update_budgets(struct sched_entry *entry, size_t len,
686 int tc_consumed, int num_tc)
688 int tc, budget, new_budget = 0;
690 for (tc = 0; tc < num_tc; tc++) {
691 budget = atomic_read(&entry->budget[tc]);
692 /* Don't consume from infinite budget */
693 if (budget == INT_MAX) {
694 if (tc == tc_consumed)
699 if (tc == tc_consumed)
700 new_budget = atomic_sub_return(len, &entry->budget[tc]);
702 atomic_sub(len, &entry->budget[tc]);
708 static struct sk_buff *taprio_dequeue_from_txq(struct Qdisc *sch, int txq,
709 struct sched_entry *entry,
712 struct taprio_sched *q = qdisc_priv(sch);
713 struct net_device *dev = qdisc_dev(sch);
714 struct Qdisc *child = q->qdiscs[txq];
715 int num_tc = netdev_get_num_tc(dev);
722 if (unlikely(!child))
725 if (TXTIME_ASSIST_IS_ENABLED(q->flags))
726 goto skip_peek_checks;
728 skb = child->ops->peek(child);
732 prio = skb->priority;
733 tc = netdev_get_prio_tc_map(dev, prio);
735 if (!(gate_mask & BIT(tc)))
738 len = qdisc_pkt_len(skb);
739 guard = ktime_add_ns(taprio_get_time(q), length_to_duration(q, len));
741 /* In the case that there's no gate entry, there's no
744 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
745 !taprio_entry_allows_tx(guard, entry, tc))
748 /* ... and no budget. */
749 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
750 taprio_update_budgets(entry, len, tc, num_tc) < 0)
754 skb = child->ops->dequeue(child);
758 qdisc_bstats_update(sch, skb);
759 qdisc_qstats_backlog_dec(sch, skb);
765 static void taprio_next_tc_txq(struct net_device *dev, int tc, int *txq)
767 int offset = dev->tc_to_txq[tc].offset;
768 int count = dev->tc_to_txq[tc].count;
771 if (*txq == offset + count)
775 /* Prioritize higher traffic classes, and select among TXQs belonging to the
776 * same TC using round robin
778 static struct sk_buff *taprio_dequeue_tc_priority(struct Qdisc *sch,
779 struct sched_entry *entry,
782 struct taprio_sched *q = qdisc_priv(sch);
783 struct net_device *dev = qdisc_dev(sch);
784 int num_tc = netdev_get_num_tc(dev);
788 for (tc = num_tc - 1; tc >= 0; tc--) {
789 int first_txq = q->cur_txq[tc];
791 if (!(gate_mask & BIT(tc)))
795 skb = taprio_dequeue_from_txq(sch, q->cur_txq[tc],
798 taprio_next_tc_txq(dev, tc, &q->cur_txq[tc]);
800 if (q->cur_txq[tc] >= dev->num_tx_queues)
801 q->cur_txq[tc] = first_txq;
805 } while (q->cur_txq[tc] != first_txq);
811 /* Broken way of prioritizing smaller TXQ indices and ignoring the traffic
812 * class other than to determine whether the gate is open or not
814 static struct sk_buff *taprio_dequeue_txq_priority(struct Qdisc *sch,
815 struct sched_entry *entry,
818 struct net_device *dev = qdisc_dev(sch);
822 for (i = 0; i < dev->num_tx_queues; i++) {
823 skb = taprio_dequeue_from_txq(sch, i, entry, gate_mask);
831 /* Will not be called in the full offload case, since the TX queues are
832 * attached to the Qdisc created using qdisc_create_dflt()
834 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
836 struct taprio_sched *q = qdisc_priv(sch);
837 struct sk_buff *skb = NULL;
838 struct sched_entry *entry;
842 entry = rcu_dereference(q->current_entry);
843 /* if there's no entry, it means that the schedule didn't
844 * start yet, so force all gates to be open, this is in
845 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
848 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
852 if (static_branch_unlikely(&taprio_have_broken_mqprio) &&
853 !static_branch_likely(&taprio_have_working_mqprio)) {
854 /* Single NIC kind which is broken */
855 skb = taprio_dequeue_txq_priority(sch, entry, gate_mask);
856 } else if (static_branch_likely(&taprio_have_working_mqprio) &&
857 !static_branch_unlikely(&taprio_have_broken_mqprio)) {
858 /* Single NIC kind which prioritizes properly */
859 skb = taprio_dequeue_tc_priority(sch, entry, gate_mask);
861 /* Mixed NIC kinds present in system, need dynamic testing */
862 if (q->broken_mqprio)
863 skb = taprio_dequeue_txq_priority(sch, entry, gate_mask);
865 skb = taprio_dequeue_tc_priority(sch, entry, gate_mask);
874 static bool should_restart_cycle(const struct sched_gate_list *oper,
875 const struct sched_entry *entry)
877 if (list_is_last(&entry->list, &oper->entries))
880 if (ktime_compare(entry->end_time, oper->cycle_end_time) == 0)
886 static bool should_change_schedules(const struct sched_gate_list *admin,
887 const struct sched_gate_list *oper,
890 ktime_t next_base_time, extension_time;
895 next_base_time = sched_base_time(admin);
897 /* This is the simple case, the end_time would fall after
898 * the next schedule base_time.
900 if (ktime_compare(next_base_time, end_time) <= 0)
903 /* This is the cycle_time_extension case, if the end_time
904 * plus the amount that can be extended would fall after the
905 * next schedule base_time, we can extend the current schedule
908 extension_time = ktime_add_ns(end_time, oper->cycle_time_extension);
910 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
911 * how precisely the extension should be made. So after
912 * conformance testing, this logic may change.
914 if (ktime_compare(next_base_time, extension_time) <= 0)
920 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
922 struct taprio_sched *q = container_of(timer, struct taprio_sched,
924 struct net_device *dev = qdisc_dev(q->root);
925 struct sched_gate_list *oper, *admin;
926 int num_tc = netdev_get_num_tc(dev);
927 struct sched_entry *entry, *next;
928 struct Qdisc *sch = q->root;
932 spin_lock(&q->current_entry_lock);
933 entry = rcu_dereference_protected(q->current_entry,
934 lockdep_is_held(&q->current_entry_lock));
935 oper = rcu_dereference_protected(q->oper_sched,
936 lockdep_is_held(&q->current_entry_lock));
937 admin = rcu_dereference_protected(q->admin_sched,
938 lockdep_is_held(&q->current_entry_lock));
941 switch_schedules(q, &admin, &oper);
943 /* This can happen in two cases: 1. this is the very first run
944 * of this function (i.e. we weren't running any schedule
945 * previously); 2. The previous schedule just ended. The first
946 * entry of all schedules are pre-calculated during the
947 * schedule initialization.
949 if (unlikely(!entry || entry->end_time == oper->base_time)) {
950 next = list_first_entry(&oper->entries, struct sched_entry,
952 end_time = next->end_time;
956 if (should_restart_cycle(oper, entry)) {
957 next = list_first_entry(&oper->entries, struct sched_entry,
959 oper->cycle_end_time = ktime_add_ns(oper->cycle_end_time,
962 next = list_next_entry(entry, list);
965 end_time = ktime_add_ns(entry->end_time, next->interval);
966 end_time = min_t(ktime_t, end_time, oper->cycle_end_time);
968 for (tc = 0; tc < num_tc; tc++) {
969 if (next->gate_duration[tc] == oper->cycle_time)
970 next->gate_close_time[tc] = KTIME_MAX;
972 next->gate_close_time[tc] = ktime_add_ns(entry->end_time,
973 next->gate_duration[tc]);
976 if (should_change_schedules(admin, oper, end_time)) {
977 /* Set things so the next time this runs, the new
980 end_time = sched_base_time(admin);
981 switch_schedules(q, &admin, &oper);
984 next->end_time = end_time;
985 taprio_set_budgets(q, oper, next);
988 rcu_assign_pointer(q->current_entry, next);
989 spin_unlock(&q->current_entry_lock);
991 hrtimer_set_expires(&q->advance_timer, end_time);
994 __netif_schedule(sch);
997 return HRTIMER_RESTART;
1000 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
1001 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
1002 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
1003 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
1004 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
1007 static const struct nla_policy taprio_tc_policy[TCA_TAPRIO_TC_ENTRY_MAX + 1] = {
1008 [TCA_TAPRIO_TC_ENTRY_INDEX] = { .type = NLA_U32 },
1009 [TCA_TAPRIO_TC_ENTRY_MAX_SDU] = { .type = NLA_U32 },
1010 [TCA_TAPRIO_TC_ENTRY_FP] = NLA_POLICY_RANGE(NLA_U32,
1015 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
1016 [TCA_TAPRIO_ATTR_PRIOMAP] = {
1017 .len = sizeof(struct tc_mqprio_qopt)
1019 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
1020 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
1021 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
1022 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
1023 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 },
1024 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
1025 [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 },
1026 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 },
1027 [TCA_TAPRIO_ATTR_TC_ENTRY] = { .type = NLA_NESTED },
1030 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
1031 struct sched_entry *entry,
1032 struct netlink_ext_ack *extack)
1034 int min_duration = length_to_duration(q, ETH_ZLEN);
1037 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
1038 entry->command = nla_get_u8(
1039 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
1041 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
1042 entry->gate_mask = nla_get_u32(
1043 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
1045 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
1046 interval = nla_get_u32(
1047 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
1049 /* The interval should allow at least the minimum ethernet
1052 if (interval < min_duration) {
1053 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
1057 entry->interval = interval;
1062 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
1063 struct sched_entry *entry, int index,
1064 struct netlink_ext_ack *extack)
1066 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
1069 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
1070 entry_policy, NULL);
1072 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
1076 entry->index = index;
1078 return fill_sched_entry(q, tb, entry, extack);
1081 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
1082 struct sched_gate_list *sched,
1083 struct netlink_ext_ack *extack)
1092 nla_for_each_nested(n, list, rem) {
1093 struct sched_entry *entry;
1095 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
1096 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
1100 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1102 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
1106 err = parse_sched_entry(q, n, entry, i, extack);
1112 list_add_tail(&entry->list, &sched->entries);
1116 sched->num_entries = i;
1121 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
1122 struct sched_gate_list *new,
1123 struct netlink_ext_ack *extack)
1127 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
1128 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
1132 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
1133 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
1135 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
1136 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
1138 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
1139 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
1141 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
1142 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
1147 if (!new->cycle_time) {
1148 struct sched_entry *entry;
1151 list_for_each_entry(entry, &new->entries, list)
1152 cycle = ktime_add_ns(cycle, entry->interval);
1155 NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
1159 new->cycle_time = cycle;
1162 taprio_calculate_gate_durations(q, new);
1167 static int taprio_parse_mqprio_opt(struct net_device *dev,
1168 struct tc_mqprio_qopt *qopt,
1169 struct netlink_ext_ack *extack,
1172 bool allow_overlapping_txqs = TXTIME_ASSIST_IS_ENABLED(taprio_flags);
1174 if (!qopt && !dev->num_tc) {
1175 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
1179 /* If num_tc is already set, it means that the user already
1180 * configured the mqprio part
1185 /* taprio imposes that traffic classes map 1:n to tx queues */
1186 if (qopt->num_tc > dev->num_tx_queues) {
1187 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
1191 /* For some reason, in txtime-assist mode, we allow TXQ ranges for
1192 * different TCs to overlap, and just validate the TXQ ranges.
1194 return mqprio_validate_qopt(dev, qopt, true, allow_overlapping_txqs,
1198 static int taprio_get_start_time(struct Qdisc *sch,
1199 struct sched_gate_list *sched,
1202 struct taprio_sched *q = qdisc_priv(sch);
1203 ktime_t now, base, cycle;
1206 base = sched_base_time(sched);
1207 now = taprio_get_time(q);
1209 if (ktime_after(base, now)) {
1214 cycle = sched->cycle_time;
1216 /* The qdisc is expected to have at least one sched_entry. Moreover,
1217 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
1218 * something went really wrong. In that case, we should warn about this
1219 * inconsistent state and return error.
1221 if (WARN_ON(!cycle))
1224 /* Schedule the start time for the beginning of the next
1227 n = div64_s64(ktime_sub_ns(now, base), cycle);
1228 *start = ktime_add_ns(base, (n + 1) * cycle);
1232 static void setup_first_end_time(struct taprio_sched *q,
1233 struct sched_gate_list *sched, ktime_t base)
1235 struct net_device *dev = qdisc_dev(q->root);
1236 int num_tc = netdev_get_num_tc(dev);
1237 struct sched_entry *first;
1241 first = list_first_entry(&sched->entries,
1242 struct sched_entry, list);
1244 cycle = sched->cycle_time;
1246 /* FIXME: find a better place to do this */
1247 sched->cycle_end_time = ktime_add_ns(base, cycle);
1249 first->end_time = ktime_add_ns(base, first->interval);
1250 taprio_set_budgets(q, sched, first);
1252 for (tc = 0; tc < num_tc; tc++) {
1253 if (first->gate_duration[tc] == sched->cycle_time)
1254 first->gate_close_time[tc] = KTIME_MAX;
1256 first->gate_close_time[tc] = ktime_add_ns(base, first->gate_duration[tc]);
1259 rcu_assign_pointer(q->current_entry, NULL);
1262 static void taprio_start_sched(struct Qdisc *sch,
1263 ktime_t start, struct sched_gate_list *new)
1265 struct taprio_sched *q = qdisc_priv(sch);
1268 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1271 expires = hrtimer_get_expires(&q->advance_timer);
1273 expires = KTIME_MAX;
1275 /* If the new schedule starts before the next expiration, we
1276 * reprogram it to the earliest one, so we change the admin
1277 * schedule to the operational one at the right time.
1279 start = min_t(ktime_t, start, expires);
1281 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1284 static void taprio_set_picos_per_byte(struct net_device *dev,
1285 struct taprio_sched *q)
1287 struct ethtool_link_ksettings ecmd;
1288 int speed = SPEED_10;
1292 err = __ethtool_get_link_ksettings(dev, &ecmd);
1296 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1297 speed = ecmd.base.speed;
1300 picos_per_byte = (USEC_PER_SEC * 8) / speed;
1302 atomic64_set(&q->picos_per_byte, picos_per_byte);
1303 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1304 dev->name, (long long)atomic64_read(&q->picos_per_byte),
1308 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1311 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1312 struct sched_gate_list *oper, *admin;
1313 struct qdisc_size_table *stab;
1314 struct taprio_sched *q;
1318 if (event != NETDEV_UP && event != NETDEV_CHANGE)
1321 list_for_each_entry(q, &taprio_list, taprio_list) {
1322 if (dev != qdisc_dev(q->root))
1325 taprio_set_picos_per_byte(dev, q);
1327 stab = rtnl_dereference(q->root->stab);
1329 oper = rtnl_dereference(q->oper_sched);
1331 taprio_update_queue_max_sdu(q, oper, stab);
1333 admin = rtnl_dereference(q->admin_sched);
1335 taprio_update_queue_max_sdu(q, admin, stab);
1343 static void setup_txtime(struct taprio_sched *q,
1344 struct sched_gate_list *sched, ktime_t base)
1346 struct sched_entry *entry;
1349 list_for_each_entry(entry, &sched->entries, list) {
1350 entry->next_txtime = ktime_add_ns(base, interval);
1351 interval += entry->interval;
1355 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1357 struct __tc_taprio_qopt_offload *__offload;
1359 __offload = kzalloc(struct_size(__offload, offload.entries, num_entries),
1364 refcount_set(&__offload->users, 1);
1366 return &__offload->offload;
1369 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1372 struct __tc_taprio_qopt_offload *__offload;
1374 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1377 refcount_inc(&__offload->users);
1381 EXPORT_SYMBOL_GPL(taprio_offload_get);
1383 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1385 struct __tc_taprio_qopt_offload *__offload;
1387 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1390 if (!refcount_dec_and_test(&__offload->users))
1395 EXPORT_SYMBOL_GPL(taprio_offload_free);
1397 /* The function will only serve to keep the pointers to the "oper" and "admin"
1398 * schedules valid in relation to their base times, so when calling dump() the
1399 * users looks at the right schedules.
1400 * When using full offload, the admin configuration is promoted to oper at the
1401 * base_time in the PHC time domain. But because the system time is not
1402 * necessarily in sync with that, we can't just trigger a hrtimer to call
1403 * switch_schedules at the right hardware time.
1404 * At the moment we call this by hand right away from taprio, but in the future
1405 * it will be useful to create a mechanism for drivers to notify taprio of the
1406 * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1407 * This is left as TODO.
1409 static void taprio_offload_config_changed(struct taprio_sched *q)
1411 struct sched_gate_list *oper, *admin;
1413 oper = rtnl_dereference(q->oper_sched);
1414 admin = rtnl_dereference(q->admin_sched);
1416 switch_schedules(q, &admin, &oper);
1419 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1421 u32 i, queue_mask = 0;
1423 for (i = 0; i < dev->num_tc; i++) {
1426 if (!(tc_mask & BIT(i)))
1429 offset = dev->tc_to_txq[i].offset;
1430 count = dev->tc_to_txq[i].count;
1432 queue_mask |= GENMASK(offset + count - 1, offset);
1438 static void taprio_sched_to_offload(struct net_device *dev,
1439 struct sched_gate_list *sched,
1440 struct tc_taprio_qopt_offload *offload,
1441 const struct tc_taprio_caps *caps)
1443 struct sched_entry *entry;
1446 offload->base_time = sched->base_time;
1447 offload->cycle_time = sched->cycle_time;
1448 offload->cycle_time_extension = sched->cycle_time_extension;
1450 list_for_each_entry(entry, &sched->entries, list) {
1451 struct tc_taprio_sched_entry *e = &offload->entries[i];
1453 e->command = entry->command;
1454 e->interval = entry->interval;
1455 if (caps->gate_mask_per_txq)
1456 e->gate_mask = tc_map_to_queue_mask(dev,
1459 e->gate_mask = entry->gate_mask;
1464 offload->num_entries = i;
1467 static void taprio_detect_broken_mqprio(struct taprio_sched *q)
1469 struct net_device *dev = qdisc_dev(q->root);
1470 struct tc_taprio_caps caps;
1472 qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO,
1473 &caps, sizeof(caps));
1475 q->broken_mqprio = caps.broken_mqprio;
1476 if (q->broken_mqprio)
1477 static_branch_inc(&taprio_have_broken_mqprio);
1479 static_branch_inc(&taprio_have_working_mqprio);
1481 q->detected_mqprio = true;
1484 static void taprio_cleanup_broken_mqprio(struct taprio_sched *q)
1486 if (!q->detected_mqprio)
1489 if (q->broken_mqprio)
1490 static_branch_dec(&taprio_have_broken_mqprio);
1492 static_branch_dec(&taprio_have_working_mqprio);
1495 static int taprio_enable_offload(struct net_device *dev,
1496 struct taprio_sched *q,
1497 struct sched_gate_list *sched,
1498 struct netlink_ext_ack *extack)
1500 const struct net_device_ops *ops = dev->netdev_ops;
1501 struct tc_taprio_qopt_offload *offload;
1502 struct tc_taprio_caps caps;
1505 if (!ops->ndo_setup_tc) {
1506 NL_SET_ERR_MSG(extack,
1507 "Device does not support taprio offload");
1511 qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO,
1512 &caps, sizeof(caps));
1514 if (!caps.supports_queue_max_sdu) {
1515 for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
1516 if (q->max_sdu[tc]) {
1517 NL_SET_ERR_MSG_MOD(extack,
1518 "Device does not handle queueMaxSDU");
1524 offload = taprio_offload_alloc(sched->num_entries);
1526 NL_SET_ERR_MSG(extack,
1527 "Not enough memory for enabling offload mode");
1530 offload->enable = 1;
1531 offload->extack = extack;
1532 mqprio_qopt_reconstruct(dev, &offload->mqprio.qopt);
1533 offload->mqprio.extack = extack;
1534 taprio_sched_to_offload(dev, sched, offload, &caps);
1535 mqprio_fp_to_offload(q->fp, &offload->mqprio);
1537 for (tc = 0; tc < TC_MAX_QUEUE; tc++)
1538 offload->max_sdu[tc] = q->max_sdu[tc];
1540 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1542 NL_SET_ERR_MSG_WEAK(extack,
1543 "Device failed to setup taprio offload");
1547 q->offloaded = true;
1550 /* The offload structure may linger around via a reference taken by the
1551 * device driver, so clear up the netlink extack pointer so that the
1552 * driver isn't tempted to dereference data which stopped being valid
1554 offload->extack = NULL;
1555 offload->mqprio.extack = NULL;
1556 taprio_offload_free(offload);
1561 static int taprio_disable_offload(struct net_device *dev,
1562 struct taprio_sched *q,
1563 struct netlink_ext_ack *extack)
1565 const struct net_device_ops *ops = dev->netdev_ops;
1566 struct tc_taprio_qopt_offload *offload;
1572 offload = taprio_offload_alloc(0);
1574 NL_SET_ERR_MSG(extack,
1575 "Not enough memory to disable offload mode");
1578 offload->enable = 0;
1580 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1582 NL_SET_ERR_MSG(extack,
1583 "Device failed to disable offload");
1587 q->offloaded = false;
1590 taprio_offload_free(offload);
1595 /* If full offload is enabled, the only possible clockid is the net device's
1596 * PHC. For that reason, specifying a clockid through netlink is incorrect.
1597 * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1598 * in sync with the specified clockid via a user space daemon such as phc2sys.
1599 * For both software taprio and txtime-assist, the clockid is used for the
1600 * hrtimer that advances the schedule and hence mandatory.
1602 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1603 struct netlink_ext_ack *extack)
1605 struct taprio_sched *q = qdisc_priv(sch);
1606 struct net_device *dev = qdisc_dev(sch);
1609 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1610 const struct ethtool_ops *ops = dev->ethtool_ops;
1611 struct ethtool_ts_info info = {
1612 .cmd = ETHTOOL_GET_TS_INFO,
1616 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1617 NL_SET_ERR_MSG(extack,
1618 "The 'clockid' cannot be specified for full offload");
1622 if (ops && ops->get_ts_info)
1623 err = ops->get_ts_info(dev, &info);
1625 if (err || info.phc_index < 0) {
1626 NL_SET_ERR_MSG(extack,
1627 "Device does not have a PTP clock");
1631 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1632 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1633 enum tk_offsets tk_offset;
1635 /* We only support static clockids and we don't allow
1636 * for it to be modified after the first init.
1639 (q->clockid != -1 && q->clockid != clockid)) {
1640 NL_SET_ERR_MSG(extack,
1641 "Changing the 'clockid' of a running schedule is not supported");
1647 case CLOCK_REALTIME:
1648 tk_offset = TK_OFFS_REAL;
1650 case CLOCK_MONOTONIC:
1651 tk_offset = TK_OFFS_MAX;
1653 case CLOCK_BOOTTIME:
1654 tk_offset = TK_OFFS_BOOT;
1657 tk_offset = TK_OFFS_TAI;
1660 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1664 /* This pairs with READ_ONCE() in taprio_mono_to_any */
1665 WRITE_ONCE(q->tk_offset, tk_offset);
1667 q->clockid = clockid;
1669 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1673 /* Everything went ok, return success. */
1680 static int taprio_parse_tc_entry(struct Qdisc *sch,
1682 u32 max_sdu[TC_QOPT_MAX_QUEUE],
1683 u32 fp[TC_QOPT_MAX_QUEUE],
1684 unsigned long *seen_tcs,
1685 struct netlink_ext_ack *extack)
1687 struct nlattr *tb[TCA_TAPRIO_TC_ENTRY_MAX + 1] = { };
1688 struct net_device *dev = qdisc_dev(sch);
1692 err = nla_parse_nested(tb, TCA_TAPRIO_TC_ENTRY_MAX, opt,
1693 taprio_tc_policy, extack);
1697 if (!tb[TCA_TAPRIO_TC_ENTRY_INDEX]) {
1698 NL_SET_ERR_MSG_MOD(extack, "TC entry index missing");
1702 tc = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_INDEX]);
1703 if (tc >= TC_QOPT_MAX_QUEUE) {
1704 NL_SET_ERR_MSG_MOD(extack, "TC entry index out of range");
1708 if (*seen_tcs & BIT(tc)) {
1709 NL_SET_ERR_MSG_MOD(extack, "Duplicate TC entry");
1713 *seen_tcs |= BIT(tc);
1715 if (tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]) {
1716 val = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]);
1717 if (val > dev->max_mtu) {
1718 NL_SET_ERR_MSG_MOD(extack, "TC max SDU exceeds device max MTU");
1725 if (tb[TCA_TAPRIO_TC_ENTRY_FP])
1726 fp[tc] = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_FP]);
1731 static int taprio_parse_tc_entries(struct Qdisc *sch,
1733 struct netlink_ext_ack *extack)
1735 struct taprio_sched *q = qdisc_priv(sch);
1736 struct net_device *dev = qdisc_dev(sch);
1737 u32 max_sdu[TC_QOPT_MAX_QUEUE];
1738 bool have_preemption = false;
1739 unsigned long seen_tcs = 0;
1740 u32 fp[TC_QOPT_MAX_QUEUE];
1745 for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) {
1746 max_sdu[tc] = q->max_sdu[tc];
1750 nla_for_each_nested(n, opt, rem) {
1751 if (nla_type(n) != TCA_TAPRIO_ATTR_TC_ENTRY)
1754 err = taprio_parse_tc_entry(sch, n, max_sdu, fp, &seen_tcs,
1760 for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) {
1761 q->max_sdu[tc] = max_sdu[tc];
1763 if (fp[tc] != TC_FP_EXPRESS)
1764 have_preemption = true;
1767 if (have_preemption) {
1768 if (!FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1769 NL_SET_ERR_MSG(extack,
1770 "Preemption only supported with full offload");
1774 if (!ethtool_dev_mm_supported(dev)) {
1775 NL_SET_ERR_MSG(extack,
1776 "Device does not support preemption");
1784 static int taprio_mqprio_cmp(const struct net_device *dev,
1785 const struct tc_mqprio_qopt *mqprio)
1789 if (!mqprio || mqprio->num_tc != dev->num_tc)
1792 for (i = 0; i < mqprio->num_tc; i++)
1793 if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1794 dev->tc_to_txq[i].offset != mqprio->offset[i])
1797 for (i = 0; i <= TC_BITMASK; i++)
1798 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1804 /* The semantics of the 'flags' argument in relation to 'change()'
1805 * requests, are interpreted following two rules (which are applied in
1806 * this order): (1) an omitted 'flags' argument is interpreted as
1807 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1810 static int taprio_new_flags(const struct nlattr *attr, u32 old,
1811 struct netlink_ext_ack *extack)
1816 new = nla_get_u32(attr);
1818 if (old != TAPRIO_FLAGS_INVALID && old != new) {
1819 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1823 if (!taprio_flags_valid(new)) {
1824 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1831 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1832 struct netlink_ext_ack *extack)
1834 struct qdisc_size_table *stab = rtnl_dereference(sch->stab);
1835 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1836 struct sched_gate_list *oper, *admin, *new_admin;
1837 struct taprio_sched *q = qdisc_priv(sch);
1838 struct net_device *dev = qdisc_dev(sch);
1839 struct tc_mqprio_qopt *mqprio = NULL;
1840 unsigned long flags;
1844 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1845 taprio_policy, extack);
1849 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1850 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1852 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1859 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1863 err = taprio_parse_tc_entries(sch, opt, extack);
1867 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1869 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1872 INIT_LIST_HEAD(&new_admin->entries);
1874 oper = rtnl_dereference(q->oper_sched);
1875 admin = rtnl_dereference(q->admin_sched);
1877 /* no changes - no new mqprio settings */
1878 if (!taprio_mqprio_cmp(dev, mqprio))
1881 if (mqprio && (oper || admin)) {
1882 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1888 err = netdev_set_num_tc(dev, mqprio->num_tc);
1891 for (i = 0; i < mqprio->num_tc; i++) {
1892 netdev_set_tc_queue(dev, i,
1895 q->cur_txq[i] = mqprio->offset[i];
1898 /* Always use supplied priority mappings */
1899 for (i = 0; i <= TC_BITMASK; i++)
1900 netdev_set_prio_tc_map(dev, i,
1901 mqprio->prio_tc_map[i]);
1904 err = parse_taprio_schedule(q, tb, new_admin, extack);
1908 if (new_admin->num_entries == 0) {
1909 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1914 err = taprio_parse_clockid(sch, tb, extack);
1918 taprio_set_picos_per_byte(dev, q);
1919 taprio_update_queue_max_sdu(q, new_admin, stab);
1921 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1922 err = taprio_enable_offload(dev, q, new_admin, extack);
1924 err = taprio_disable_offload(dev, q, extack);
1928 /* Protects against enqueue()/dequeue() */
1929 spin_lock_bh(qdisc_lock(sch));
1931 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1932 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1933 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1938 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1941 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1942 !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1943 !hrtimer_active(&q->advance_timer)) {
1944 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1945 q->advance_timer.function = advance_sched;
1948 err = taprio_get_start_time(sch, new_admin, &start);
1950 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1954 setup_txtime(q, new_admin, start);
1956 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1958 rcu_assign_pointer(q->oper_sched, new_admin);
1964 rcu_assign_pointer(q->admin_sched, new_admin);
1966 call_rcu(&admin->rcu, taprio_free_sched_cb);
1968 setup_first_end_time(q, new_admin, start);
1970 /* Protects against advance_sched() */
1971 spin_lock_irqsave(&q->current_entry_lock, flags);
1973 taprio_start_sched(sch, start, new_admin);
1975 rcu_assign_pointer(q->admin_sched, new_admin);
1977 call_rcu(&admin->rcu, taprio_free_sched_cb);
1979 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1981 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1982 taprio_offload_config_changed(q);
1989 NL_SET_ERR_MSG_MOD(extack,
1990 "Size table not specified, frame length estimations may be inaccurate");
1993 spin_unlock_bh(qdisc_lock(sch));
1997 call_rcu(&new_admin->rcu, taprio_free_sched_cb);
2002 static void taprio_reset(struct Qdisc *sch)
2004 struct taprio_sched *q = qdisc_priv(sch);
2005 struct net_device *dev = qdisc_dev(sch);
2008 hrtimer_cancel(&q->advance_timer);
2011 for (i = 0; i < dev->num_tx_queues; i++)
2013 qdisc_reset(q->qdiscs[i]);
2017 static void taprio_destroy(struct Qdisc *sch)
2019 struct taprio_sched *q = qdisc_priv(sch);
2020 struct net_device *dev = qdisc_dev(sch);
2021 struct sched_gate_list *oper, *admin;
2024 list_del(&q->taprio_list);
2026 /* Note that taprio_reset() might not be called if an error
2027 * happens in qdisc_create(), after taprio_init() has been called.
2029 hrtimer_cancel(&q->advance_timer);
2030 qdisc_synchronize(sch);
2032 taprio_disable_offload(dev, q, NULL);
2035 for (i = 0; i < dev->num_tx_queues; i++)
2036 qdisc_put(q->qdiscs[i]);
2042 netdev_reset_tc(dev);
2044 oper = rtnl_dereference(q->oper_sched);
2045 admin = rtnl_dereference(q->admin_sched);
2048 call_rcu(&oper->rcu, taprio_free_sched_cb);
2051 call_rcu(&admin->rcu, taprio_free_sched_cb);
2053 taprio_cleanup_broken_mqprio(q);
2056 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
2057 struct netlink_ext_ack *extack)
2059 struct taprio_sched *q = qdisc_priv(sch);
2060 struct net_device *dev = qdisc_dev(sch);
2063 spin_lock_init(&q->current_entry_lock);
2065 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
2066 q->advance_timer.function = advance_sched;
2070 /* We only support static clockids. Use an invalid value as default
2071 * and get the valid one on taprio_change().
2074 q->flags = TAPRIO_FLAGS_INVALID;
2076 list_add(&q->taprio_list, &taprio_list);
2078 if (sch->parent != TC_H_ROOT) {
2079 NL_SET_ERR_MSG_MOD(extack, "Can only be attached as root qdisc");
2083 if (!netif_is_multiqueue(dev)) {
2084 NL_SET_ERR_MSG_MOD(extack, "Multi-queue device is required");
2088 /* pre-allocate qdisc, attachment can't fail */
2089 q->qdiscs = kcalloc(dev->num_tx_queues,
2090 sizeof(q->qdiscs[0]),
2099 for (i = 0; i < dev->num_tx_queues; i++) {
2100 struct netdev_queue *dev_queue;
2101 struct Qdisc *qdisc;
2103 dev_queue = netdev_get_tx_queue(dev, i);
2104 qdisc = qdisc_create_dflt(dev_queue,
2106 TC_H_MAKE(TC_H_MAJ(sch->handle),
2112 if (i < dev->real_num_tx_queues)
2113 qdisc_hash_add(qdisc, false);
2115 q->qdiscs[i] = qdisc;
2118 for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++)
2119 q->fp[tc] = TC_FP_EXPRESS;
2121 taprio_detect_broken_mqprio(q);
2123 return taprio_change(sch, opt, extack);
2126 static void taprio_attach(struct Qdisc *sch)
2128 struct taprio_sched *q = qdisc_priv(sch);
2129 struct net_device *dev = qdisc_dev(sch);
2132 /* Attach underlying qdisc */
2133 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
2134 struct Qdisc *qdisc = q->qdiscs[ntx];
2137 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
2138 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
2139 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
2141 old = dev_graft_qdisc(qdisc->dev_queue, sch);
2142 qdisc_refcount_inc(sch);
2148 /* access to the child qdiscs is not needed in offload mode */
2149 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
2155 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
2158 struct net_device *dev = qdisc_dev(sch);
2159 unsigned long ntx = cl - 1;
2161 if (ntx >= dev->num_tx_queues)
2164 return netdev_get_tx_queue(dev, ntx);
2167 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
2168 struct Qdisc *new, struct Qdisc **old,
2169 struct netlink_ext_ack *extack)
2171 struct taprio_sched *q = qdisc_priv(sch);
2172 struct net_device *dev = qdisc_dev(sch);
2173 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
2178 if (dev->flags & IFF_UP)
2179 dev_deactivate(dev);
2181 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
2182 *old = dev_graft_qdisc(dev_queue, new);
2184 *old = q->qdiscs[cl - 1];
2185 q->qdiscs[cl - 1] = new;
2189 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
2191 if (dev->flags & IFF_UP)
2197 static int dump_entry(struct sk_buff *msg,
2198 const struct sched_entry *entry)
2200 struct nlattr *item;
2202 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
2206 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
2207 goto nla_put_failure;
2209 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
2210 goto nla_put_failure;
2212 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
2214 goto nla_put_failure;
2216 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
2218 goto nla_put_failure;
2220 return nla_nest_end(msg, item);
2223 nla_nest_cancel(msg, item);
2227 static int dump_schedule(struct sk_buff *msg,
2228 const struct sched_gate_list *root)
2230 struct nlattr *entry_list;
2231 struct sched_entry *entry;
2233 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
2234 root->base_time, TCA_TAPRIO_PAD))
2237 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
2238 root->cycle_time, TCA_TAPRIO_PAD))
2241 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
2242 root->cycle_time_extension, TCA_TAPRIO_PAD))
2245 entry_list = nla_nest_start_noflag(msg,
2246 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
2250 list_for_each_entry(entry, &root->entries, list) {
2251 if (dump_entry(msg, entry) < 0)
2255 nla_nest_end(msg, entry_list);
2259 nla_nest_cancel(msg, entry_list);
2263 static int taprio_dump_tc_entries(struct sk_buff *skb,
2264 struct taprio_sched *q,
2265 struct sched_gate_list *sched)
2270 for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
2271 n = nla_nest_start(skb, TCA_TAPRIO_ATTR_TC_ENTRY);
2275 if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_INDEX, tc))
2276 goto nla_put_failure;
2278 if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_MAX_SDU,
2279 sched->max_sdu[tc]))
2280 goto nla_put_failure;
2282 if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_FP, q->fp[tc]))
2283 goto nla_put_failure;
2285 nla_nest_end(skb, n);
2291 nla_nest_cancel(skb, n);
2295 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
2297 struct taprio_sched *q = qdisc_priv(sch);
2298 struct net_device *dev = qdisc_dev(sch);
2299 struct sched_gate_list *oper, *admin;
2300 struct tc_mqprio_qopt opt = { 0 };
2301 struct nlattr *nest, *sched_nest;
2303 oper = rtnl_dereference(q->oper_sched);
2304 admin = rtnl_dereference(q->admin_sched);
2306 mqprio_qopt_reconstruct(dev, &opt);
2308 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
2312 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
2315 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
2316 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
2319 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
2322 if (q->txtime_delay &&
2323 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
2326 if (oper && taprio_dump_tc_entries(skb, q, oper))
2329 if (oper && dump_schedule(skb, oper))
2335 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
2339 if (dump_schedule(skb, admin))
2342 nla_nest_end(skb, sched_nest);
2345 return nla_nest_end(skb, nest);
2348 nla_nest_cancel(skb, sched_nest);
2351 nla_nest_cancel(skb, nest);
2357 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
2359 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
2364 return rtnl_dereference(dev_queue->qdisc_sleeping);
2367 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
2369 unsigned int ntx = TC_H_MIN(classid);
2371 if (!taprio_queue_get(sch, ntx))
2376 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
2377 struct sk_buff *skb, struct tcmsg *tcm)
2379 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
2381 tcm->tcm_parent = TC_H_ROOT;
2382 tcm->tcm_handle |= TC_H_MIN(cl);
2383 tcm->tcm_info = rtnl_dereference(dev_queue->qdisc_sleeping)->handle;
2388 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
2389 struct gnet_dump *d)
2393 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
2395 sch = rtnl_dereference(dev_queue->qdisc_sleeping);
2396 if (gnet_stats_copy_basic(d, NULL, &sch->bstats, true) < 0 ||
2397 qdisc_qstats_copy(d, sch) < 0)
2402 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
2404 struct net_device *dev = qdisc_dev(sch);
2410 arg->count = arg->skip;
2411 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
2412 if (!tc_qdisc_stats_dump(sch, ntx + 1, arg))
2417 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
2420 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
2423 static const struct Qdisc_class_ops taprio_class_ops = {
2424 .graft = taprio_graft,
2425 .leaf = taprio_leaf,
2426 .find = taprio_find,
2427 .walk = taprio_walk,
2428 .dump = taprio_dump_class,
2429 .dump_stats = taprio_dump_class_stats,
2430 .select_queue = taprio_select_queue,
2433 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
2434 .cl_ops = &taprio_class_ops,
2436 .priv_size = sizeof(struct taprio_sched),
2437 .init = taprio_init,
2438 .change = taprio_change,
2439 .destroy = taprio_destroy,
2440 .reset = taprio_reset,
2441 .attach = taprio_attach,
2442 .peek = taprio_peek,
2443 .dequeue = taprio_dequeue,
2444 .enqueue = taprio_enqueue,
2445 .dump = taprio_dump,
2446 .owner = THIS_MODULE,
2449 static struct notifier_block taprio_device_notifier = {
2450 .notifier_call = taprio_dev_notifier,
2453 static int __init taprio_module_init(void)
2455 int err = register_netdevice_notifier(&taprio_device_notifier);
2460 return register_qdisc(&taprio_qdisc_ops);
2463 static void __exit taprio_module_exit(void)
2465 unregister_qdisc(&taprio_qdisc_ops);
2466 unregister_netdevice_notifier(&taprio_device_notifier);
2469 module_init(taprio_module_init);
2470 module_exit(taprio_module_exit);
2471 MODULE_LICENSE("GPL");