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/types.h>
11 #include <linux/slab.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/list.h>
15 #include <linux/errno.h>
16 #include <linux/skbuff.h>
17 #include <linux/math64.h>
18 #include <linux/module.h>
19 #include <linux/spinlock.h>
20 #include <linux/rcupdate.h>
21 #include <linux/time.h>
22 #include <net/netlink.h>
23 #include <net/pkt_sched.h>
24 #include <net/pkt_cls.h>
25 #include <net/sch_generic.h>
29 #include "sch_mqprio_lib.h"
31 static LIST_HEAD(taprio_list);
32 static struct static_key_false taprio_have_broken_mqprio;
33 static struct static_key_false taprio_have_working_mqprio;
35 #define TAPRIO_ALL_GATES_OPEN -1
37 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
38 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
39 #define TAPRIO_FLAGS_INVALID U32_MAX
42 /* Durations between this GCL entry and the GCL entry where the
43 * respective traffic class gate closes
45 u64 gate_duration[TC_MAX_QUEUE];
46 atomic_t budget[TC_MAX_QUEUE];
47 /* The qdisc makes some effort so that no packet leaves
50 ktime_t gate_close_time[TC_MAX_QUEUE];
51 struct list_head list;
52 /* Used to calculate when to advance the schedule */
61 struct sched_gate_list {
62 /* Longest non-zero contiguous gate durations per traffic class,
63 * or 0 if a traffic class gate never opens during the schedule.
65 u64 max_open_gate_duration[TC_MAX_QUEUE];
66 u32 max_frm_len[TC_MAX_QUEUE]; /* for the fast path */
67 u32 max_sdu[TC_MAX_QUEUE]; /* for dump */
69 struct list_head entries;
71 ktime_t cycle_end_time;
73 s64 cycle_time_extension;
78 struct Qdisc **qdiscs;
81 enum tk_offsets tk_offset;
86 atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
87 * speeds it's sub-nanoseconds per byte
90 /* Protects the update side of the RCU protected current_entry */
91 spinlock_t current_entry_lock;
92 struct sched_entry __rcu *current_entry;
93 struct sched_gate_list __rcu *oper_sched;
94 struct sched_gate_list __rcu *admin_sched;
95 struct hrtimer advance_timer;
96 struct list_head taprio_list;
97 int cur_txq[TC_MAX_QUEUE];
98 u32 max_sdu[TC_MAX_QUEUE]; /* save info from the user */
102 struct __tc_taprio_qopt_offload {
104 struct tc_taprio_qopt_offload offload;
107 static void taprio_calculate_gate_durations(struct taprio_sched *q,
108 struct sched_gate_list *sched)
110 struct net_device *dev = qdisc_dev(q->root);
111 int num_tc = netdev_get_num_tc(dev);
112 struct sched_entry *entry, *cur;
115 list_for_each_entry(entry, &sched->entries, list) {
116 u32 gates_still_open = entry->gate_mask;
118 /* For each traffic class, calculate each open gate duration,
119 * starting at this schedule entry and ending at the schedule
120 * entry containing a gate close event for that TC.
125 if (!gates_still_open)
128 for (tc = 0; tc < num_tc; tc++) {
129 if (!(gates_still_open & BIT(tc)))
132 if (cur->gate_mask & BIT(tc))
133 entry->gate_duration[tc] += cur->interval;
135 gates_still_open &= ~BIT(tc);
138 cur = list_next_entry_circular(cur, &sched->entries, list);
139 } while (cur != entry);
141 /* Keep track of the maximum gate duration for each traffic
142 * class, taking care to not confuse a traffic class which is
143 * temporarily closed with one that is always closed.
145 for (tc = 0; tc < num_tc; tc++)
146 if (entry->gate_duration[tc] &&
147 sched->max_open_gate_duration[tc] < entry->gate_duration[tc])
148 sched->max_open_gate_duration[tc] = entry->gate_duration[tc];
152 static bool taprio_entry_allows_tx(ktime_t skb_end_time,
153 struct sched_entry *entry, int tc)
155 return ktime_before(skb_end_time, entry->gate_close_time[tc]);
158 static ktime_t sched_base_time(const struct sched_gate_list *sched)
163 return ns_to_ktime(sched->base_time);
166 static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono)
168 /* This pairs with WRITE_ONCE() in taprio_parse_clockid() */
169 enum tk_offsets tk_offset = READ_ONCE(q->tk_offset);
175 return ktime_mono_to_any(mono, tk_offset);
179 static ktime_t taprio_get_time(const struct taprio_sched *q)
181 return taprio_mono_to_any(q, ktime_get());
184 static void taprio_free_sched_cb(struct rcu_head *head)
186 struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
187 struct sched_entry *entry, *n;
189 list_for_each_entry_safe(entry, n, &sched->entries, list) {
190 list_del(&entry->list);
197 static void switch_schedules(struct taprio_sched *q,
198 struct sched_gate_list **admin,
199 struct sched_gate_list **oper)
201 rcu_assign_pointer(q->oper_sched, *admin);
202 rcu_assign_pointer(q->admin_sched, NULL);
205 call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
211 /* Get how much time has been already elapsed in the current cycle. */
212 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
214 ktime_t time_since_sched_start;
217 time_since_sched_start = ktime_sub(time, sched->base_time);
218 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
223 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
224 struct sched_gate_list *admin,
225 struct sched_entry *entry,
228 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
229 ktime_t intv_end, cycle_ext_end, cycle_end;
231 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
232 intv_end = ktime_add_ns(intv_start, entry->interval);
233 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
235 if (ktime_before(intv_end, cycle_end))
237 else if (admin && admin != sched &&
238 ktime_after(admin->base_time, cycle_end) &&
239 ktime_before(admin->base_time, cycle_ext_end))
240 return admin->base_time;
245 static int length_to_duration(struct taprio_sched *q, int len)
247 return div_u64(len * atomic64_read(&q->picos_per_byte), PSEC_PER_NSEC);
250 static int duration_to_length(struct taprio_sched *q, u64 duration)
252 return div_u64(duration * PSEC_PER_NSEC, atomic64_read(&q->picos_per_byte));
255 /* Sets sched->max_sdu[] and sched->max_frm_len[] to the minimum between the
256 * q->max_sdu[] requested by the user and the max_sdu dynamically determined by
257 * the maximum open gate durations at the given link speed.
259 static void taprio_update_queue_max_sdu(struct taprio_sched *q,
260 struct sched_gate_list *sched,
261 struct qdisc_size_table *stab)
263 struct net_device *dev = qdisc_dev(q->root);
264 int num_tc = netdev_get_num_tc(dev);
265 u32 max_sdu_from_user;
270 for (tc = 0; tc < num_tc; tc++) {
271 max_sdu_from_user = q->max_sdu[tc] ?: U32_MAX;
273 /* TC gate never closes => keep the queueMaxSDU
274 * selected by the user
276 if (sched->max_open_gate_duration[tc] == sched->cycle_time) {
277 max_sdu_dynamic = U32_MAX;
281 max_frm_len = duration_to_length(q, sched->max_open_gate_duration[tc]);
282 /* Compensate for L1 overhead from size table,
283 * but don't let the frame size go negative
286 max_frm_len -= stab->szopts.overhead;
287 max_frm_len = max_t(int, max_frm_len,
288 dev->hard_header_len + 1);
290 max_sdu_dynamic = max_frm_len - dev->hard_header_len;
291 if (max_sdu_dynamic > dev->max_mtu)
292 max_sdu_dynamic = U32_MAX;
295 max_sdu = min(max_sdu_dynamic, max_sdu_from_user);
297 if (max_sdu != U32_MAX) {
298 sched->max_frm_len[tc] = max_sdu + dev->hard_header_len;
299 sched->max_sdu[tc] = max_sdu;
301 sched->max_frm_len[tc] = U32_MAX; /* never oversized */
302 sched->max_sdu[tc] = 0;
307 /* Returns the entry corresponding to next available interval. If
308 * validate_interval is set, it only validates whether the timestamp occurs
309 * when the gate corresponding to the skb's traffic class is open.
311 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
313 struct sched_gate_list *sched,
314 struct sched_gate_list *admin,
316 ktime_t *interval_start,
317 ktime_t *interval_end,
318 bool validate_interval)
320 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
321 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
322 struct sched_entry *entry = NULL, *entry_found = NULL;
323 struct taprio_sched *q = qdisc_priv(sch);
324 struct net_device *dev = qdisc_dev(sch);
325 bool entry_available = false;
329 tc = netdev_get_prio_tc_map(dev, skb->priority);
330 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
338 cycle = sched->cycle_time;
339 cycle_elapsed = get_cycle_time_elapsed(sched, time);
340 curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
341 cycle_end = ktime_add_ns(curr_intv_end, cycle);
343 list_for_each_entry(entry, &sched->entries, list) {
344 curr_intv_start = curr_intv_end;
345 curr_intv_end = get_interval_end_time(sched, admin, entry,
348 if (ktime_after(curr_intv_start, cycle_end))
351 if (!(entry->gate_mask & BIT(tc)) ||
352 packet_transmit_time > entry->interval)
355 txtime = entry->next_txtime;
357 if (ktime_before(txtime, time) || validate_interval) {
358 transmit_end_time = ktime_add_ns(time, packet_transmit_time);
359 if ((ktime_before(curr_intv_start, time) &&
360 ktime_before(transmit_end_time, curr_intv_end)) ||
361 (ktime_after(curr_intv_start, time) && !validate_interval)) {
363 *interval_start = curr_intv_start;
364 *interval_end = curr_intv_end;
366 } else if (!entry_available && !validate_interval) {
367 /* Here, we are just trying to find out the
368 * first available interval in the next cycle.
370 entry_available = true;
372 *interval_start = ktime_add_ns(curr_intv_start, cycle);
373 *interval_end = ktime_add_ns(curr_intv_end, cycle);
375 } else if (ktime_before(txtime, earliest_txtime) &&
377 earliest_txtime = txtime;
379 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
380 *interval_start = ktime_add(curr_intv_start, n * cycle);
381 *interval_end = ktime_add(curr_intv_end, n * cycle);
388 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
390 struct taprio_sched *q = qdisc_priv(sch);
391 struct sched_gate_list *sched, *admin;
392 ktime_t interval_start, interval_end;
393 struct sched_entry *entry;
396 sched = rcu_dereference(q->oper_sched);
397 admin = rcu_dereference(q->admin_sched);
399 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
400 &interval_start, &interval_end, true);
406 static bool taprio_flags_valid(u32 flags)
408 /* Make sure no other flag bits are set. */
409 if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST |
410 TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
412 /* txtime-assist and full offload are mutually exclusive */
413 if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
414 (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
419 /* This returns the tstamp value set by TCP in terms of the set clock. */
420 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
422 unsigned int offset = skb_network_offset(skb);
423 const struct ipv6hdr *ipv6h;
424 const struct iphdr *iph;
425 struct ipv6hdr _ipv6h;
427 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
431 if (ipv6h->version == 4) {
432 iph = (struct iphdr *)ipv6h;
433 offset += iph->ihl * 4;
435 /* special-case 6in4 tunnelling, as that is a common way to get
436 * v6 connectivity in the home
438 if (iph->protocol == IPPROTO_IPV6) {
439 ipv6h = skb_header_pointer(skb, offset,
440 sizeof(_ipv6h), &_ipv6h);
442 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
444 } else if (iph->protocol != IPPROTO_TCP) {
447 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
451 return taprio_mono_to_any(q, skb->skb_mstamp_ns);
454 /* There are a few scenarios where we will have to modify the txtime from
455 * what is read from next_txtime in sched_entry. They are:
456 * 1. If txtime is in the past,
457 * a. The gate for the traffic class is currently open and packet can be
458 * transmitted before it closes, schedule the packet right away.
459 * b. If the gate corresponding to the traffic class is going to open later
460 * in the cycle, set the txtime of packet to the interval start.
461 * 2. If txtime is in the future, there are packets corresponding to the
462 * current traffic class waiting to be transmitted. So, the following
463 * possibilities exist:
464 * a. We can transmit the packet before the window containing the txtime
466 * b. The window might close before the transmission can be completed
467 * successfully. So, schedule the packet in the next open window.
469 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
471 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
472 struct taprio_sched *q = qdisc_priv(sch);
473 struct sched_gate_list *sched, *admin;
474 ktime_t minimum_time, now, txtime;
475 int len, packet_transmit_time;
476 struct sched_entry *entry;
479 now = taprio_get_time(q);
480 minimum_time = ktime_add_ns(now, q->txtime_delay);
482 tcp_tstamp = get_tcp_tstamp(q, skb);
483 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
486 admin = rcu_dereference(q->admin_sched);
487 sched = rcu_dereference(q->oper_sched);
488 if (admin && ktime_after(minimum_time, admin->base_time))
489 switch_schedules(q, &admin, &sched);
491 /* Until the schedule starts, all the queues are open */
492 if (!sched || ktime_before(minimum_time, sched->base_time)) {
493 txtime = minimum_time;
497 len = qdisc_pkt_len(skb);
498 packet_transmit_time = length_to_duration(q, len);
501 sched_changed = false;
503 entry = find_entry_to_transmit(skb, sch, sched, admin,
505 &interval_start, &interval_end,
512 txtime = entry->next_txtime;
513 txtime = max_t(ktime_t, txtime, minimum_time);
514 txtime = max_t(ktime_t, txtime, interval_start);
516 if (admin && admin != sched &&
517 ktime_after(txtime, admin->base_time)) {
519 sched_changed = true;
523 transmit_end_time = ktime_add(txtime, packet_transmit_time);
524 minimum_time = transmit_end_time;
526 /* Update the txtime of current entry to the next time it's
529 if (ktime_after(transmit_end_time, interval_end))
530 entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
531 } while (sched_changed || ktime_after(transmit_end_time, interval_end));
533 entry->next_txtime = transmit_end_time;
540 /* Devices with full offload are expected to honor this in hardware */
541 static bool taprio_skb_exceeds_queue_max_sdu(struct Qdisc *sch,
544 struct taprio_sched *q = qdisc_priv(sch);
545 struct net_device *dev = qdisc_dev(sch);
546 struct sched_gate_list *sched;
547 int prio = skb->priority;
548 bool exceeds = false;
551 tc = netdev_get_prio_tc_map(dev, prio);
554 sched = rcu_dereference(q->oper_sched);
555 if (sched && skb->len > sched->max_frm_len[tc])
562 static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
563 struct Qdisc *child, struct sk_buff **to_free)
565 struct taprio_sched *q = qdisc_priv(sch);
567 /* sk_flags are only safe to use on full sockets. */
568 if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) {
569 if (!is_valid_interval(skb, sch))
570 return qdisc_drop(skb, sch, to_free);
571 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
572 skb->tstamp = get_packet_txtime(skb, sch);
574 return qdisc_drop(skb, sch, to_free);
577 qdisc_qstats_backlog_inc(sch, skb);
580 return qdisc_enqueue(skb, child, to_free);
583 static int taprio_enqueue_segmented(struct sk_buff *skb, struct Qdisc *sch,
585 struct sk_buff **to_free)
587 unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb);
588 netdev_features_t features = netif_skb_features(skb);
589 struct sk_buff *segs, *nskb;
592 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
593 if (IS_ERR_OR_NULL(segs))
594 return qdisc_drop(skb, sch, to_free);
596 skb_list_walk_safe(segs, segs, nskb) {
597 skb_mark_not_on_list(segs);
598 qdisc_skb_cb(segs)->pkt_len = segs->len;
601 /* FIXME: we should be segmenting to a smaller size
602 * rather than dropping these
604 if (taprio_skb_exceeds_queue_max_sdu(sch, segs))
605 ret = qdisc_drop(segs, sch, to_free);
607 ret = taprio_enqueue_one(segs, sch, child, to_free);
609 if (ret != NET_XMIT_SUCCESS) {
610 if (net_xmit_drop_count(ret))
611 qdisc_qstats_drop(sch);
618 qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
621 return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
624 /* Will not be called in the full offload case, since the TX queues are
625 * attached to the Qdisc created using qdisc_create_dflt()
627 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
628 struct sk_buff **to_free)
630 struct taprio_sched *q = qdisc_priv(sch);
634 queue = skb_get_queue_mapping(skb);
636 child = q->qdiscs[queue];
637 if (unlikely(!child))
638 return qdisc_drop(skb, sch, to_free);
640 if (taprio_skb_exceeds_queue_max_sdu(sch, skb)) {
641 /* Large packets might not be transmitted when the transmission
642 * duration exceeds any configured interval. Therefore, segment
643 * the skb into smaller chunks. Drivers with full offload are
644 * expected to handle this in hardware.
647 return taprio_enqueue_segmented(skb, sch, child,
650 return qdisc_drop(skb, sch, to_free);
653 return taprio_enqueue_one(skb, sch, child, to_free);
656 static struct sk_buff *taprio_peek(struct Qdisc *sch)
658 WARN_ONCE(1, "taprio only supports operating as root qdisc, peek() not implemented");
662 static void taprio_set_budgets(struct taprio_sched *q,
663 struct sched_gate_list *sched,
664 struct sched_entry *entry)
666 struct net_device *dev = qdisc_dev(q->root);
667 int num_tc = netdev_get_num_tc(dev);
670 for (tc = 0; tc < num_tc; tc++) {
671 /* Traffic classes which never close have infinite budget */
672 if (entry->gate_duration[tc] == sched->cycle_time)
675 budget = div64_u64((u64)entry->gate_duration[tc] * PSEC_PER_NSEC,
676 atomic64_read(&q->picos_per_byte));
678 atomic_set(&entry->budget[tc], budget);
682 /* When an skb is sent, it consumes from the budget of all traffic classes */
683 static int taprio_update_budgets(struct sched_entry *entry, size_t len,
684 int tc_consumed, int num_tc)
686 int tc, budget, new_budget = 0;
688 for (tc = 0; tc < num_tc; tc++) {
689 budget = atomic_read(&entry->budget[tc]);
690 /* Don't consume from infinite budget */
691 if (budget == INT_MAX) {
692 if (tc == tc_consumed)
697 if (tc == tc_consumed)
698 new_budget = atomic_sub_return(len, &entry->budget[tc]);
700 atomic_sub(len, &entry->budget[tc]);
706 static struct sk_buff *taprio_dequeue_from_txq(struct Qdisc *sch, int txq,
707 struct sched_entry *entry,
710 struct taprio_sched *q = qdisc_priv(sch);
711 struct net_device *dev = qdisc_dev(sch);
712 struct Qdisc *child = q->qdiscs[txq];
713 int num_tc = netdev_get_num_tc(dev);
720 if (unlikely(!child))
723 if (TXTIME_ASSIST_IS_ENABLED(q->flags))
724 goto skip_peek_checks;
726 skb = child->ops->peek(child);
730 prio = skb->priority;
731 tc = netdev_get_prio_tc_map(dev, prio);
733 if (!(gate_mask & BIT(tc)))
736 len = qdisc_pkt_len(skb);
737 guard = ktime_add_ns(taprio_get_time(q), length_to_duration(q, len));
739 /* In the case that there's no gate entry, there's no
742 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
743 !taprio_entry_allows_tx(guard, entry, tc))
746 /* ... and no budget. */
747 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
748 taprio_update_budgets(entry, len, tc, num_tc) < 0)
752 skb = child->ops->dequeue(child);
756 qdisc_bstats_update(sch, skb);
757 qdisc_qstats_backlog_dec(sch, skb);
763 static void taprio_next_tc_txq(struct net_device *dev, int tc, int *txq)
765 int offset = dev->tc_to_txq[tc].offset;
766 int count = dev->tc_to_txq[tc].count;
769 if (*txq == offset + count)
773 /* Prioritize higher traffic classes, and select among TXQs belonging to the
774 * same TC using round robin
776 static struct sk_buff *taprio_dequeue_tc_priority(struct Qdisc *sch,
777 struct sched_entry *entry,
780 struct taprio_sched *q = qdisc_priv(sch);
781 struct net_device *dev = qdisc_dev(sch);
782 int num_tc = netdev_get_num_tc(dev);
786 for (tc = num_tc - 1; tc >= 0; tc--) {
787 int first_txq = q->cur_txq[tc];
789 if (!(gate_mask & BIT(tc)))
793 skb = taprio_dequeue_from_txq(sch, q->cur_txq[tc],
796 taprio_next_tc_txq(dev, tc, &q->cur_txq[tc]);
800 } while (q->cur_txq[tc] != first_txq);
806 /* Broken way of prioritizing smaller TXQ indices and ignoring the traffic
807 * class other than to determine whether the gate is open or not
809 static struct sk_buff *taprio_dequeue_txq_priority(struct Qdisc *sch,
810 struct sched_entry *entry,
813 struct net_device *dev = qdisc_dev(sch);
817 for (i = 0; i < dev->num_tx_queues; i++) {
818 skb = taprio_dequeue_from_txq(sch, i, entry, gate_mask);
826 /* Will not be called in the full offload case, since the TX queues are
827 * attached to the Qdisc created using qdisc_create_dflt()
829 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
831 struct taprio_sched *q = qdisc_priv(sch);
832 struct sk_buff *skb = NULL;
833 struct sched_entry *entry;
837 entry = rcu_dereference(q->current_entry);
838 /* if there's no entry, it means that the schedule didn't
839 * start yet, so force all gates to be open, this is in
840 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
843 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
847 if (static_branch_unlikely(&taprio_have_broken_mqprio) &&
848 !static_branch_likely(&taprio_have_working_mqprio)) {
849 /* Single NIC kind which is broken */
850 skb = taprio_dequeue_txq_priority(sch, entry, gate_mask);
851 } else if (static_branch_likely(&taprio_have_working_mqprio) &&
852 !static_branch_unlikely(&taprio_have_broken_mqprio)) {
853 /* Single NIC kind which prioritizes properly */
854 skb = taprio_dequeue_tc_priority(sch, entry, gate_mask);
856 /* Mixed NIC kinds present in system, need dynamic testing */
857 if (q->broken_mqprio)
858 skb = taprio_dequeue_txq_priority(sch, entry, gate_mask);
860 skb = taprio_dequeue_tc_priority(sch, entry, gate_mask);
869 static bool should_restart_cycle(const struct sched_gate_list *oper,
870 const struct sched_entry *entry)
872 if (list_is_last(&entry->list, &oper->entries))
875 if (ktime_compare(entry->end_time, oper->cycle_end_time) == 0)
881 static bool should_change_schedules(const struct sched_gate_list *admin,
882 const struct sched_gate_list *oper,
885 ktime_t next_base_time, extension_time;
890 next_base_time = sched_base_time(admin);
892 /* This is the simple case, the end_time would fall after
893 * the next schedule base_time.
895 if (ktime_compare(next_base_time, end_time) <= 0)
898 /* This is the cycle_time_extension case, if the end_time
899 * plus the amount that can be extended would fall after the
900 * next schedule base_time, we can extend the current schedule
903 extension_time = ktime_add_ns(end_time, oper->cycle_time_extension);
905 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
906 * how precisely the extension should be made. So after
907 * conformance testing, this logic may change.
909 if (ktime_compare(next_base_time, extension_time) <= 0)
915 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
917 struct taprio_sched *q = container_of(timer, struct taprio_sched,
919 struct net_device *dev = qdisc_dev(q->root);
920 struct sched_gate_list *oper, *admin;
921 int num_tc = netdev_get_num_tc(dev);
922 struct sched_entry *entry, *next;
923 struct Qdisc *sch = q->root;
927 spin_lock(&q->current_entry_lock);
928 entry = rcu_dereference_protected(q->current_entry,
929 lockdep_is_held(&q->current_entry_lock));
930 oper = rcu_dereference_protected(q->oper_sched,
931 lockdep_is_held(&q->current_entry_lock));
932 admin = rcu_dereference_protected(q->admin_sched,
933 lockdep_is_held(&q->current_entry_lock));
936 switch_schedules(q, &admin, &oper);
938 /* This can happen in two cases: 1. this is the very first run
939 * of this function (i.e. we weren't running any schedule
940 * previously); 2. The previous schedule just ended. The first
941 * entry of all schedules are pre-calculated during the
942 * schedule initialization.
944 if (unlikely(!entry || entry->end_time == oper->base_time)) {
945 next = list_first_entry(&oper->entries, struct sched_entry,
947 end_time = next->end_time;
951 if (should_restart_cycle(oper, entry)) {
952 next = list_first_entry(&oper->entries, struct sched_entry,
954 oper->cycle_end_time = ktime_add_ns(oper->cycle_end_time,
957 next = list_next_entry(entry, list);
960 end_time = ktime_add_ns(entry->end_time, next->interval);
961 end_time = min_t(ktime_t, end_time, oper->cycle_end_time);
963 for (tc = 0; tc < num_tc; tc++) {
964 if (next->gate_duration[tc] == oper->cycle_time)
965 next->gate_close_time[tc] = KTIME_MAX;
967 next->gate_close_time[tc] = ktime_add_ns(entry->end_time,
968 next->gate_duration[tc]);
971 if (should_change_schedules(admin, oper, end_time)) {
972 /* Set things so the next time this runs, the new
975 end_time = sched_base_time(admin);
976 switch_schedules(q, &admin, &oper);
979 next->end_time = end_time;
980 taprio_set_budgets(q, oper, next);
983 rcu_assign_pointer(q->current_entry, next);
984 spin_unlock(&q->current_entry_lock);
986 hrtimer_set_expires(&q->advance_timer, end_time);
989 __netif_schedule(sch);
992 return HRTIMER_RESTART;
995 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
996 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
997 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
998 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
999 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
1002 static const struct nla_policy taprio_tc_policy[TCA_TAPRIO_TC_ENTRY_MAX + 1] = {
1003 [TCA_TAPRIO_TC_ENTRY_INDEX] = { .type = NLA_U32 },
1004 [TCA_TAPRIO_TC_ENTRY_MAX_SDU] = { .type = NLA_U32 },
1007 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
1008 [TCA_TAPRIO_ATTR_PRIOMAP] = {
1009 .len = sizeof(struct tc_mqprio_qopt)
1011 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
1012 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
1013 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
1014 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
1015 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 },
1016 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
1017 [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 },
1018 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 },
1019 [TCA_TAPRIO_ATTR_TC_ENTRY] = { .type = NLA_NESTED },
1022 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
1023 struct sched_entry *entry,
1024 struct netlink_ext_ack *extack)
1026 int min_duration = length_to_duration(q, ETH_ZLEN);
1029 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
1030 entry->command = nla_get_u8(
1031 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
1033 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
1034 entry->gate_mask = nla_get_u32(
1035 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
1037 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
1038 interval = nla_get_u32(
1039 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
1041 /* The interval should allow at least the minimum ethernet
1044 if (interval < min_duration) {
1045 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
1049 entry->interval = interval;
1054 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
1055 struct sched_entry *entry, int index,
1056 struct netlink_ext_ack *extack)
1058 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
1061 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
1062 entry_policy, NULL);
1064 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
1068 entry->index = index;
1070 return fill_sched_entry(q, tb, entry, extack);
1073 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
1074 struct sched_gate_list *sched,
1075 struct netlink_ext_ack *extack)
1084 nla_for_each_nested(n, list, rem) {
1085 struct sched_entry *entry;
1087 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
1088 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
1092 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1094 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
1098 err = parse_sched_entry(q, n, entry, i, extack);
1104 list_add_tail(&entry->list, &sched->entries);
1108 sched->num_entries = i;
1113 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
1114 struct sched_gate_list *new,
1115 struct netlink_ext_ack *extack)
1119 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
1120 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
1124 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
1125 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
1127 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
1128 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
1130 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
1131 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
1133 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
1134 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
1139 if (!new->cycle_time) {
1140 struct sched_entry *entry;
1143 list_for_each_entry(entry, &new->entries, list)
1144 cycle = ktime_add_ns(cycle, entry->interval);
1147 NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
1151 new->cycle_time = cycle;
1154 taprio_calculate_gate_durations(q, new);
1159 static int taprio_parse_mqprio_opt(struct net_device *dev,
1160 struct tc_mqprio_qopt *qopt,
1161 struct netlink_ext_ack *extack,
1164 bool allow_overlapping_txqs = TXTIME_ASSIST_IS_ENABLED(taprio_flags);
1166 if (!qopt && !dev->num_tc) {
1167 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
1171 /* If num_tc is already set, it means that the user already
1172 * configured the mqprio part
1177 /* taprio imposes that traffic classes map 1:n to tx queues */
1178 if (qopt->num_tc > dev->num_tx_queues) {
1179 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
1183 /* For some reason, in txtime-assist mode, we allow TXQ ranges for
1184 * different TCs to overlap, and just validate the TXQ ranges.
1186 return mqprio_validate_qopt(dev, qopt, true, allow_overlapping_txqs,
1190 static int taprio_get_start_time(struct Qdisc *sch,
1191 struct sched_gate_list *sched,
1194 struct taprio_sched *q = qdisc_priv(sch);
1195 ktime_t now, base, cycle;
1198 base = sched_base_time(sched);
1199 now = taprio_get_time(q);
1201 if (ktime_after(base, now)) {
1206 cycle = sched->cycle_time;
1208 /* The qdisc is expected to have at least one sched_entry. Moreover,
1209 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
1210 * something went really wrong. In that case, we should warn about this
1211 * inconsistent state and return error.
1213 if (WARN_ON(!cycle))
1216 /* Schedule the start time for the beginning of the next
1219 n = div64_s64(ktime_sub_ns(now, base), cycle);
1220 *start = ktime_add_ns(base, (n + 1) * cycle);
1224 static void setup_first_end_time(struct taprio_sched *q,
1225 struct sched_gate_list *sched, ktime_t base)
1227 struct net_device *dev = qdisc_dev(q->root);
1228 int num_tc = netdev_get_num_tc(dev);
1229 struct sched_entry *first;
1233 first = list_first_entry(&sched->entries,
1234 struct sched_entry, list);
1236 cycle = sched->cycle_time;
1238 /* FIXME: find a better place to do this */
1239 sched->cycle_end_time = ktime_add_ns(base, cycle);
1241 first->end_time = ktime_add_ns(base, first->interval);
1242 taprio_set_budgets(q, sched, first);
1244 for (tc = 0; tc < num_tc; tc++) {
1245 if (first->gate_duration[tc] == sched->cycle_time)
1246 first->gate_close_time[tc] = KTIME_MAX;
1248 first->gate_close_time[tc] = ktime_add_ns(base, first->gate_duration[tc]);
1251 rcu_assign_pointer(q->current_entry, NULL);
1254 static void taprio_start_sched(struct Qdisc *sch,
1255 ktime_t start, struct sched_gate_list *new)
1257 struct taprio_sched *q = qdisc_priv(sch);
1260 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1263 expires = hrtimer_get_expires(&q->advance_timer);
1265 expires = KTIME_MAX;
1267 /* If the new schedule starts before the next expiration, we
1268 * reprogram it to the earliest one, so we change the admin
1269 * schedule to the operational one at the right time.
1271 start = min_t(ktime_t, start, expires);
1273 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1276 static void taprio_set_picos_per_byte(struct net_device *dev,
1277 struct taprio_sched *q)
1279 struct ethtool_link_ksettings ecmd;
1280 int speed = SPEED_10;
1284 err = __ethtool_get_link_ksettings(dev, &ecmd);
1288 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1289 speed = ecmd.base.speed;
1292 picos_per_byte = (USEC_PER_SEC * 8) / speed;
1294 atomic64_set(&q->picos_per_byte, picos_per_byte);
1295 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1296 dev->name, (long long)atomic64_read(&q->picos_per_byte),
1300 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1303 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1304 struct sched_gate_list *oper, *admin;
1305 struct qdisc_size_table *stab;
1306 struct taprio_sched *q;
1310 if (event != NETDEV_UP && event != NETDEV_CHANGE)
1313 list_for_each_entry(q, &taprio_list, taprio_list) {
1314 if (dev != qdisc_dev(q->root))
1317 taprio_set_picos_per_byte(dev, q);
1319 stab = rtnl_dereference(q->root->stab);
1321 oper = rtnl_dereference(q->oper_sched);
1323 taprio_update_queue_max_sdu(q, oper, stab);
1325 admin = rtnl_dereference(q->admin_sched);
1327 taprio_update_queue_max_sdu(q, admin, stab);
1335 static void setup_txtime(struct taprio_sched *q,
1336 struct sched_gate_list *sched, ktime_t base)
1338 struct sched_entry *entry;
1341 list_for_each_entry(entry, &sched->entries, list) {
1342 entry->next_txtime = ktime_add_ns(base, interval);
1343 interval += entry->interval;
1347 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1349 struct __tc_taprio_qopt_offload *__offload;
1351 __offload = kzalloc(struct_size(__offload, offload.entries, num_entries),
1356 refcount_set(&__offload->users, 1);
1358 return &__offload->offload;
1361 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1364 struct __tc_taprio_qopt_offload *__offload;
1366 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1369 refcount_inc(&__offload->users);
1373 EXPORT_SYMBOL_GPL(taprio_offload_get);
1375 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1377 struct __tc_taprio_qopt_offload *__offload;
1379 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1382 if (!refcount_dec_and_test(&__offload->users))
1387 EXPORT_SYMBOL_GPL(taprio_offload_free);
1389 /* The function will only serve to keep the pointers to the "oper" and "admin"
1390 * schedules valid in relation to their base times, so when calling dump() the
1391 * users looks at the right schedules.
1392 * When using full offload, the admin configuration is promoted to oper at the
1393 * base_time in the PHC time domain. But because the system time is not
1394 * necessarily in sync with that, we can't just trigger a hrtimer to call
1395 * switch_schedules at the right hardware time.
1396 * At the moment we call this by hand right away from taprio, but in the future
1397 * it will be useful to create a mechanism for drivers to notify taprio of the
1398 * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1399 * This is left as TODO.
1401 static void taprio_offload_config_changed(struct taprio_sched *q)
1403 struct sched_gate_list *oper, *admin;
1405 oper = rtnl_dereference(q->oper_sched);
1406 admin = rtnl_dereference(q->admin_sched);
1408 switch_schedules(q, &admin, &oper);
1411 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1413 u32 i, queue_mask = 0;
1415 for (i = 0; i < dev->num_tc; i++) {
1418 if (!(tc_mask & BIT(i)))
1421 offset = dev->tc_to_txq[i].offset;
1422 count = dev->tc_to_txq[i].count;
1424 queue_mask |= GENMASK(offset + count - 1, offset);
1430 static void taprio_sched_to_offload(struct net_device *dev,
1431 struct sched_gate_list *sched,
1432 struct tc_taprio_qopt_offload *offload,
1433 const struct tc_taprio_caps *caps)
1435 struct sched_entry *entry;
1438 offload->base_time = sched->base_time;
1439 offload->cycle_time = sched->cycle_time;
1440 offload->cycle_time_extension = sched->cycle_time_extension;
1442 list_for_each_entry(entry, &sched->entries, list) {
1443 struct tc_taprio_sched_entry *e = &offload->entries[i];
1445 e->command = entry->command;
1446 e->interval = entry->interval;
1447 if (caps->gate_mask_per_txq)
1448 e->gate_mask = tc_map_to_queue_mask(dev,
1451 e->gate_mask = entry->gate_mask;
1456 offload->num_entries = i;
1459 static void taprio_detect_broken_mqprio(struct taprio_sched *q)
1461 struct net_device *dev = qdisc_dev(q->root);
1462 struct tc_taprio_caps caps;
1464 qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO,
1465 &caps, sizeof(caps));
1467 q->broken_mqprio = caps.broken_mqprio;
1468 if (q->broken_mqprio)
1469 static_branch_inc(&taprio_have_broken_mqprio);
1471 static_branch_inc(&taprio_have_working_mqprio);
1473 q->detected_mqprio = true;
1476 static void taprio_cleanup_broken_mqprio(struct taprio_sched *q)
1478 if (!q->detected_mqprio)
1481 if (q->broken_mqprio)
1482 static_branch_dec(&taprio_have_broken_mqprio);
1484 static_branch_dec(&taprio_have_working_mqprio);
1487 static int taprio_enable_offload(struct net_device *dev,
1488 struct taprio_sched *q,
1489 struct sched_gate_list *sched,
1490 struct netlink_ext_ack *extack)
1492 const struct net_device_ops *ops = dev->netdev_ops;
1493 struct tc_taprio_qopt_offload *offload;
1494 struct tc_taprio_caps caps;
1497 if (!ops->ndo_setup_tc) {
1498 NL_SET_ERR_MSG(extack,
1499 "Device does not support taprio offload");
1503 qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO,
1504 &caps, sizeof(caps));
1506 if (!caps.supports_queue_max_sdu) {
1507 for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
1508 if (q->max_sdu[tc]) {
1509 NL_SET_ERR_MSG_MOD(extack,
1510 "Device does not handle queueMaxSDU");
1516 offload = taprio_offload_alloc(sched->num_entries);
1518 NL_SET_ERR_MSG(extack,
1519 "Not enough memory for enabling offload mode");
1522 offload->enable = 1;
1523 mqprio_qopt_reconstruct(dev, &offload->mqprio.qopt);
1524 taprio_sched_to_offload(dev, sched, offload, &caps);
1526 for (tc = 0; tc < TC_MAX_QUEUE; tc++)
1527 offload->max_sdu[tc] = q->max_sdu[tc];
1529 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1531 NL_SET_ERR_MSG(extack,
1532 "Device failed to setup taprio offload");
1536 q->offloaded = true;
1539 taprio_offload_free(offload);
1544 static int taprio_disable_offload(struct net_device *dev,
1545 struct taprio_sched *q,
1546 struct netlink_ext_ack *extack)
1548 const struct net_device_ops *ops = dev->netdev_ops;
1549 struct tc_taprio_qopt_offload *offload;
1555 offload = taprio_offload_alloc(0);
1557 NL_SET_ERR_MSG(extack,
1558 "Not enough memory to disable offload mode");
1561 offload->enable = 0;
1563 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1565 NL_SET_ERR_MSG(extack,
1566 "Device failed to disable offload");
1570 q->offloaded = false;
1573 taprio_offload_free(offload);
1578 /* If full offload is enabled, the only possible clockid is the net device's
1579 * PHC. For that reason, specifying a clockid through netlink is incorrect.
1580 * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1581 * in sync with the specified clockid via a user space daemon such as phc2sys.
1582 * For both software taprio and txtime-assist, the clockid is used for the
1583 * hrtimer that advances the schedule and hence mandatory.
1585 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1586 struct netlink_ext_ack *extack)
1588 struct taprio_sched *q = qdisc_priv(sch);
1589 struct net_device *dev = qdisc_dev(sch);
1592 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1593 const struct ethtool_ops *ops = dev->ethtool_ops;
1594 struct ethtool_ts_info info = {
1595 .cmd = ETHTOOL_GET_TS_INFO,
1599 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1600 NL_SET_ERR_MSG(extack,
1601 "The 'clockid' cannot be specified for full offload");
1605 if (ops && ops->get_ts_info)
1606 err = ops->get_ts_info(dev, &info);
1608 if (err || info.phc_index < 0) {
1609 NL_SET_ERR_MSG(extack,
1610 "Device does not have a PTP clock");
1614 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1615 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1616 enum tk_offsets tk_offset;
1618 /* We only support static clockids and we don't allow
1619 * for it to be modified after the first init.
1622 (q->clockid != -1 && q->clockid != clockid)) {
1623 NL_SET_ERR_MSG(extack,
1624 "Changing the 'clockid' of a running schedule is not supported");
1630 case CLOCK_REALTIME:
1631 tk_offset = TK_OFFS_REAL;
1633 case CLOCK_MONOTONIC:
1634 tk_offset = TK_OFFS_MAX;
1636 case CLOCK_BOOTTIME:
1637 tk_offset = TK_OFFS_BOOT;
1640 tk_offset = TK_OFFS_TAI;
1643 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1647 /* This pairs with READ_ONCE() in taprio_mono_to_any */
1648 WRITE_ONCE(q->tk_offset, tk_offset);
1650 q->clockid = clockid;
1652 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1656 /* Everything went ok, return success. */
1663 static int taprio_parse_tc_entry(struct Qdisc *sch,
1665 u32 max_sdu[TC_QOPT_MAX_QUEUE],
1666 unsigned long *seen_tcs,
1667 struct netlink_ext_ack *extack)
1669 struct nlattr *tb[TCA_TAPRIO_TC_ENTRY_MAX + 1] = { };
1670 struct net_device *dev = qdisc_dev(sch);
1674 err = nla_parse_nested(tb, TCA_TAPRIO_TC_ENTRY_MAX, opt,
1675 taprio_tc_policy, extack);
1679 if (!tb[TCA_TAPRIO_TC_ENTRY_INDEX]) {
1680 NL_SET_ERR_MSG_MOD(extack, "TC entry index missing");
1684 tc = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_INDEX]);
1685 if (tc >= TC_QOPT_MAX_QUEUE) {
1686 NL_SET_ERR_MSG_MOD(extack, "TC entry index out of range");
1690 if (*seen_tcs & BIT(tc)) {
1691 NL_SET_ERR_MSG_MOD(extack, "Duplicate TC entry");
1695 *seen_tcs |= BIT(tc);
1697 if (tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU])
1698 val = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]);
1700 if (val > dev->max_mtu) {
1701 NL_SET_ERR_MSG_MOD(extack, "TC max SDU exceeds device max MTU");
1710 static int taprio_parse_tc_entries(struct Qdisc *sch,
1712 struct netlink_ext_ack *extack)
1714 struct taprio_sched *q = qdisc_priv(sch);
1715 u32 max_sdu[TC_QOPT_MAX_QUEUE];
1716 unsigned long seen_tcs = 0;
1721 for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++)
1722 max_sdu[tc] = q->max_sdu[tc];
1724 nla_for_each_nested(n, opt, rem) {
1725 if (nla_type(n) != TCA_TAPRIO_ATTR_TC_ENTRY)
1728 err = taprio_parse_tc_entry(sch, n, max_sdu, &seen_tcs,
1734 for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++)
1735 q->max_sdu[tc] = max_sdu[tc];
1741 static int taprio_mqprio_cmp(const struct net_device *dev,
1742 const struct tc_mqprio_qopt *mqprio)
1746 if (!mqprio || mqprio->num_tc != dev->num_tc)
1749 for (i = 0; i < mqprio->num_tc; i++)
1750 if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1751 dev->tc_to_txq[i].offset != mqprio->offset[i])
1754 for (i = 0; i <= TC_BITMASK; i++)
1755 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1761 /* The semantics of the 'flags' argument in relation to 'change()'
1762 * requests, are interpreted following two rules (which are applied in
1763 * this order): (1) an omitted 'flags' argument is interpreted as
1764 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1767 static int taprio_new_flags(const struct nlattr *attr, u32 old,
1768 struct netlink_ext_ack *extack)
1773 new = nla_get_u32(attr);
1775 if (old != TAPRIO_FLAGS_INVALID && old != new) {
1776 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1780 if (!taprio_flags_valid(new)) {
1781 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1788 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1789 struct netlink_ext_ack *extack)
1791 struct qdisc_size_table *stab = rtnl_dereference(sch->stab);
1792 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1793 struct sched_gate_list *oper, *admin, *new_admin;
1794 struct taprio_sched *q = qdisc_priv(sch);
1795 struct net_device *dev = qdisc_dev(sch);
1796 struct tc_mqprio_qopt *mqprio = NULL;
1797 unsigned long flags;
1801 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1802 taprio_policy, extack);
1806 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1807 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1809 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1816 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1820 err = taprio_parse_tc_entries(sch, opt, extack);
1824 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1826 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1829 INIT_LIST_HEAD(&new_admin->entries);
1831 oper = rtnl_dereference(q->oper_sched);
1832 admin = rtnl_dereference(q->admin_sched);
1834 /* no changes - no new mqprio settings */
1835 if (!taprio_mqprio_cmp(dev, mqprio))
1838 if (mqprio && (oper || admin)) {
1839 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1845 err = netdev_set_num_tc(dev, mqprio->num_tc);
1848 for (i = 0; i < mqprio->num_tc; i++) {
1849 netdev_set_tc_queue(dev, i,
1852 q->cur_txq[i] = mqprio->offset[i];
1855 /* Always use supplied priority mappings */
1856 for (i = 0; i <= TC_BITMASK; i++)
1857 netdev_set_prio_tc_map(dev, i,
1858 mqprio->prio_tc_map[i]);
1861 err = parse_taprio_schedule(q, tb, new_admin, extack);
1865 if (new_admin->num_entries == 0) {
1866 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1871 err = taprio_parse_clockid(sch, tb, extack);
1875 taprio_set_picos_per_byte(dev, q);
1876 taprio_update_queue_max_sdu(q, new_admin, stab);
1878 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1879 err = taprio_enable_offload(dev, q, new_admin, extack);
1881 err = taprio_disable_offload(dev, q, extack);
1885 /* Protects against enqueue()/dequeue() */
1886 spin_lock_bh(qdisc_lock(sch));
1888 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1889 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1890 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1895 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1898 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1899 !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1900 !hrtimer_active(&q->advance_timer)) {
1901 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1902 q->advance_timer.function = advance_sched;
1905 err = taprio_get_start_time(sch, new_admin, &start);
1907 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1911 setup_txtime(q, new_admin, start);
1913 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1915 rcu_assign_pointer(q->oper_sched, new_admin);
1921 rcu_assign_pointer(q->admin_sched, new_admin);
1923 call_rcu(&admin->rcu, taprio_free_sched_cb);
1925 setup_first_end_time(q, new_admin, start);
1927 /* Protects against advance_sched() */
1928 spin_lock_irqsave(&q->current_entry_lock, flags);
1930 taprio_start_sched(sch, start, new_admin);
1932 rcu_assign_pointer(q->admin_sched, new_admin);
1934 call_rcu(&admin->rcu, taprio_free_sched_cb);
1936 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1938 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1939 taprio_offload_config_changed(q);
1946 NL_SET_ERR_MSG_MOD(extack,
1947 "Size table not specified, frame length estimations may be inaccurate");
1950 spin_unlock_bh(qdisc_lock(sch));
1954 call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1959 static void taprio_reset(struct Qdisc *sch)
1961 struct taprio_sched *q = qdisc_priv(sch);
1962 struct net_device *dev = qdisc_dev(sch);
1965 hrtimer_cancel(&q->advance_timer);
1968 for (i = 0; i < dev->num_tx_queues; i++)
1970 qdisc_reset(q->qdiscs[i]);
1974 static void taprio_destroy(struct Qdisc *sch)
1976 struct taprio_sched *q = qdisc_priv(sch);
1977 struct net_device *dev = qdisc_dev(sch);
1978 struct sched_gate_list *oper, *admin;
1981 list_del(&q->taprio_list);
1983 /* Note that taprio_reset() might not be called if an error
1984 * happens in qdisc_create(), after taprio_init() has been called.
1986 hrtimer_cancel(&q->advance_timer);
1987 qdisc_synchronize(sch);
1989 taprio_disable_offload(dev, q, NULL);
1992 for (i = 0; i < dev->num_tx_queues; i++)
1993 qdisc_put(q->qdiscs[i]);
1999 netdev_reset_tc(dev);
2001 oper = rtnl_dereference(q->oper_sched);
2002 admin = rtnl_dereference(q->admin_sched);
2005 call_rcu(&oper->rcu, taprio_free_sched_cb);
2008 call_rcu(&admin->rcu, taprio_free_sched_cb);
2010 taprio_cleanup_broken_mqprio(q);
2013 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
2014 struct netlink_ext_ack *extack)
2016 struct taprio_sched *q = qdisc_priv(sch);
2017 struct net_device *dev = qdisc_dev(sch);
2020 spin_lock_init(&q->current_entry_lock);
2022 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
2023 q->advance_timer.function = advance_sched;
2027 /* We only support static clockids. Use an invalid value as default
2028 * and get the valid one on taprio_change().
2031 q->flags = TAPRIO_FLAGS_INVALID;
2033 list_add(&q->taprio_list, &taprio_list);
2035 if (sch->parent != TC_H_ROOT) {
2036 NL_SET_ERR_MSG_MOD(extack, "Can only be attached as root qdisc");
2040 if (!netif_is_multiqueue(dev)) {
2041 NL_SET_ERR_MSG_MOD(extack, "Multi-queue device is required");
2045 /* pre-allocate qdisc, attachment can't fail */
2046 q->qdiscs = kcalloc(dev->num_tx_queues,
2047 sizeof(q->qdiscs[0]),
2056 for (i = 0; i < dev->num_tx_queues; i++) {
2057 struct netdev_queue *dev_queue;
2058 struct Qdisc *qdisc;
2060 dev_queue = netdev_get_tx_queue(dev, i);
2061 qdisc = qdisc_create_dflt(dev_queue,
2063 TC_H_MAKE(TC_H_MAJ(sch->handle),
2069 if (i < dev->real_num_tx_queues)
2070 qdisc_hash_add(qdisc, false);
2072 q->qdiscs[i] = qdisc;
2075 taprio_detect_broken_mqprio(q);
2077 return taprio_change(sch, opt, extack);
2080 static void taprio_attach(struct Qdisc *sch)
2082 struct taprio_sched *q = qdisc_priv(sch);
2083 struct net_device *dev = qdisc_dev(sch);
2086 /* Attach underlying qdisc */
2087 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
2088 struct Qdisc *qdisc = q->qdiscs[ntx];
2091 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
2092 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
2093 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
2095 old = dev_graft_qdisc(qdisc->dev_queue, sch);
2096 qdisc_refcount_inc(sch);
2102 /* access to the child qdiscs is not needed in offload mode */
2103 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
2109 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
2112 struct net_device *dev = qdisc_dev(sch);
2113 unsigned long ntx = cl - 1;
2115 if (ntx >= dev->num_tx_queues)
2118 return netdev_get_tx_queue(dev, ntx);
2121 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
2122 struct Qdisc *new, struct Qdisc **old,
2123 struct netlink_ext_ack *extack)
2125 struct taprio_sched *q = qdisc_priv(sch);
2126 struct net_device *dev = qdisc_dev(sch);
2127 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
2132 if (dev->flags & IFF_UP)
2133 dev_deactivate(dev);
2135 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
2136 *old = dev_graft_qdisc(dev_queue, new);
2138 *old = q->qdiscs[cl - 1];
2139 q->qdiscs[cl - 1] = new;
2143 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
2145 if (dev->flags & IFF_UP)
2151 static int dump_entry(struct sk_buff *msg,
2152 const struct sched_entry *entry)
2154 struct nlattr *item;
2156 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
2160 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
2161 goto nla_put_failure;
2163 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
2164 goto nla_put_failure;
2166 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
2168 goto nla_put_failure;
2170 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
2172 goto nla_put_failure;
2174 return nla_nest_end(msg, item);
2177 nla_nest_cancel(msg, item);
2181 static int dump_schedule(struct sk_buff *msg,
2182 const struct sched_gate_list *root)
2184 struct nlattr *entry_list;
2185 struct sched_entry *entry;
2187 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
2188 root->base_time, TCA_TAPRIO_PAD))
2191 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
2192 root->cycle_time, TCA_TAPRIO_PAD))
2195 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
2196 root->cycle_time_extension, TCA_TAPRIO_PAD))
2199 entry_list = nla_nest_start_noflag(msg,
2200 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
2204 list_for_each_entry(entry, &root->entries, list) {
2205 if (dump_entry(msg, entry) < 0)
2209 nla_nest_end(msg, entry_list);
2213 nla_nest_cancel(msg, entry_list);
2217 static int taprio_dump_tc_entries(struct sk_buff *skb,
2218 struct sched_gate_list *sched)
2223 for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
2224 n = nla_nest_start(skb, TCA_TAPRIO_ATTR_TC_ENTRY);
2228 if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_INDEX, tc))
2229 goto nla_put_failure;
2231 if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_MAX_SDU,
2232 sched->max_sdu[tc]))
2233 goto nla_put_failure;
2235 nla_nest_end(skb, n);
2241 nla_nest_cancel(skb, n);
2245 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
2247 struct taprio_sched *q = qdisc_priv(sch);
2248 struct net_device *dev = qdisc_dev(sch);
2249 struct sched_gate_list *oper, *admin;
2250 struct tc_mqprio_qopt opt = { 0 };
2251 struct nlattr *nest, *sched_nest;
2253 oper = rtnl_dereference(q->oper_sched);
2254 admin = rtnl_dereference(q->admin_sched);
2256 mqprio_qopt_reconstruct(dev, &opt);
2258 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
2262 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
2265 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
2266 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
2269 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
2272 if (q->txtime_delay &&
2273 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
2276 if (oper && taprio_dump_tc_entries(skb, oper))
2279 if (oper && dump_schedule(skb, oper))
2285 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
2289 if (dump_schedule(skb, admin))
2292 nla_nest_end(skb, sched_nest);
2295 return nla_nest_end(skb, nest);
2298 nla_nest_cancel(skb, sched_nest);
2301 nla_nest_cancel(skb, nest);
2307 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
2309 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
2314 return dev_queue->qdisc_sleeping;
2317 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
2319 unsigned int ntx = TC_H_MIN(classid);
2321 if (!taprio_queue_get(sch, ntx))
2326 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
2327 struct sk_buff *skb, struct tcmsg *tcm)
2329 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
2331 tcm->tcm_parent = TC_H_ROOT;
2332 tcm->tcm_handle |= TC_H_MIN(cl);
2333 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
2338 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
2339 struct gnet_dump *d)
2343 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
2345 sch = dev_queue->qdisc_sleeping;
2346 if (gnet_stats_copy_basic(d, NULL, &sch->bstats, true) < 0 ||
2347 qdisc_qstats_copy(d, sch) < 0)
2352 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
2354 struct net_device *dev = qdisc_dev(sch);
2360 arg->count = arg->skip;
2361 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
2362 if (!tc_qdisc_stats_dump(sch, ntx + 1, arg))
2367 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
2370 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
2373 static const struct Qdisc_class_ops taprio_class_ops = {
2374 .graft = taprio_graft,
2375 .leaf = taprio_leaf,
2376 .find = taprio_find,
2377 .walk = taprio_walk,
2378 .dump = taprio_dump_class,
2379 .dump_stats = taprio_dump_class_stats,
2380 .select_queue = taprio_select_queue,
2383 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
2384 .cl_ops = &taprio_class_ops,
2386 .priv_size = sizeof(struct taprio_sched),
2387 .init = taprio_init,
2388 .change = taprio_change,
2389 .destroy = taprio_destroy,
2390 .reset = taprio_reset,
2391 .attach = taprio_attach,
2392 .peek = taprio_peek,
2393 .dequeue = taprio_dequeue,
2394 .enqueue = taprio_enqueue,
2395 .dump = taprio_dump,
2396 .owner = THIS_MODULE,
2399 static struct notifier_block taprio_device_notifier = {
2400 .notifier_call = taprio_dev_notifier,
2403 static int __init taprio_module_init(void)
2405 int err = register_netdevice_notifier(&taprio_device_notifier);
2410 return register_qdisc(&taprio_qdisc_ops);
2413 static void __exit taprio_module_exit(void)
2415 unregister_qdisc(&taprio_qdisc_ops);
2416 unregister_netdevice_notifier(&taprio_device_notifier);
2419 module_init(taprio_module_init);
2420 module_exit(taprio_module_exit);
2421 MODULE_LICENSE("GPL");