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 <net/netlink.h>
22 #include <net/pkt_sched.h>
23 #include <net/pkt_cls.h>
24 #include <net/sch_generic.h>
28 static LIST_HEAD(taprio_list);
29 static DEFINE_SPINLOCK(taprio_list_lock);
31 #define TAPRIO_ALL_GATES_OPEN -1
33 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
34 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
35 #define TAPRIO_FLAGS_INVALID U32_MAX
38 struct list_head list;
40 /* The instant that this entry "closes" and the next one
41 * should open, the qdisc will make some effort so that no
42 * packet leaves after this time.
53 struct sched_gate_list {
55 struct list_head entries;
57 ktime_t cycle_close_time;
59 s64 cycle_time_extension;
64 struct Qdisc **qdiscs;
67 enum tk_offsets tk_offset;
69 atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
70 * speeds it's sub-nanoseconds per byte
73 /* Protects the update side of the RCU protected current_entry */
74 spinlock_t current_entry_lock;
75 struct sched_entry __rcu *current_entry;
76 struct sched_gate_list __rcu *oper_sched;
77 struct sched_gate_list __rcu *admin_sched;
78 struct hrtimer advance_timer;
79 struct list_head taprio_list;
80 struct sk_buff *(*dequeue)(struct Qdisc *sch);
81 struct sk_buff *(*peek)(struct Qdisc *sch);
85 struct __tc_taprio_qopt_offload {
87 struct tc_taprio_qopt_offload offload;
90 static ktime_t sched_base_time(const struct sched_gate_list *sched)
95 return ns_to_ktime(sched->base_time);
98 static ktime_t taprio_get_time(struct taprio_sched *q)
100 ktime_t mono = ktime_get();
102 switch (q->tk_offset) {
106 return ktime_mono_to_any(mono, q->tk_offset);
112 static void taprio_free_sched_cb(struct rcu_head *head)
114 struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
115 struct sched_entry *entry, *n;
120 list_for_each_entry_safe(entry, n, &sched->entries, list) {
121 list_del(&entry->list);
128 static void switch_schedules(struct taprio_sched *q,
129 struct sched_gate_list **admin,
130 struct sched_gate_list **oper)
132 rcu_assign_pointer(q->oper_sched, *admin);
133 rcu_assign_pointer(q->admin_sched, NULL);
136 call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
142 /* Get how much time has been already elapsed in the current cycle. */
143 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
145 ktime_t time_since_sched_start;
148 time_since_sched_start = ktime_sub(time, sched->base_time);
149 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
154 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
155 struct sched_gate_list *admin,
156 struct sched_entry *entry,
159 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
160 ktime_t intv_end, cycle_ext_end, cycle_end;
162 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
163 intv_end = ktime_add_ns(intv_start, entry->interval);
164 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
166 if (ktime_before(intv_end, cycle_end))
168 else if (admin && admin != sched &&
169 ktime_after(admin->base_time, cycle_end) &&
170 ktime_before(admin->base_time, cycle_ext_end))
171 return admin->base_time;
176 static int length_to_duration(struct taprio_sched *q, int len)
178 return div_u64(len * atomic64_read(&q->picos_per_byte), 1000);
181 /* Returns the entry corresponding to next available interval. If
182 * validate_interval is set, it only validates whether the timestamp occurs
183 * when the gate corresponding to the skb's traffic class is open.
185 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
187 struct sched_gate_list *sched,
188 struct sched_gate_list *admin,
190 ktime_t *interval_start,
191 ktime_t *interval_end,
192 bool validate_interval)
194 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
195 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
196 struct sched_entry *entry = NULL, *entry_found = NULL;
197 struct taprio_sched *q = qdisc_priv(sch);
198 struct net_device *dev = qdisc_dev(sch);
199 bool entry_available = false;
203 tc = netdev_get_prio_tc_map(dev, skb->priority);
204 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
212 cycle = sched->cycle_time;
213 cycle_elapsed = get_cycle_time_elapsed(sched, time);
214 curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
215 cycle_end = ktime_add_ns(curr_intv_end, cycle);
217 list_for_each_entry(entry, &sched->entries, list) {
218 curr_intv_start = curr_intv_end;
219 curr_intv_end = get_interval_end_time(sched, admin, entry,
222 if (ktime_after(curr_intv_start, cycle_end))
225 if (!(entry->gate_mask & BIT(tc)) ||
226 packet_transmit_time > entry->interval)
229 txtime = entry->next_txtime;
231 if (ktime_before(txtime, time) || validate_interval) {
232 transmit_end_time = ktime_add_ns(time, packet_transmit_time);
233 if ((ktime_before(curr_intv_start, time) &&
234 ktime_before(transmit_end_time, curr_intv_end)) ||
235 (ktime_after(curr_intv_start, time) && !validate_interval)) {
237 *interval_start = curr_intv_start;
238 *interval_end = curr_intv_end;
240 } else if (!entry_available && !validate_interval) {
241 /* Here, we are just trying to find out the
242 * first available interval in the next cycle.
244 entry_available = true;
246 *interval_start = ktime_add_ns(curr_intv_start, cycle);
247 *interval_end = ktime_add_ns(curr_intv_end, cycle);
249 } else if (ktime_before(txtime, earliest_txtime) &&
251 earliest_txtime = txtime;
253 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
254 *interval_start = ktime_add(curr_intv_start, n * cycle);
255 *interval_end = ktime_add(curr_intv_end, n * cycle);
262 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
264 struct taprio_sched *q = qdisc_priv(sch);
265 struct sched_gate_list *sched, *admin;
266 ktime_t interval_start, interval_end;
267 struct sched_entry *entry;
270 sched = rcu_dereference(q->oper_sched);
271 admin = rcu_dereference(q->admin_sched);
273 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
274 &interval_start, &interval_end, true);
280 static bool taprio_flags_valid(u32 flags)
282 /* Make sure no other flag bits are set. */
283 if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST |
284 TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
286 /* txtime-assist and full offload are mutually exclusive */
287 if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
288 (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
293 /* This returns the tstamp value set by TCP in terms of the set clock. */
294 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
296 unsigned int offset = skb_network_offset(skb);
297 const struct ipv6hdr *ipv6h;
298 const struct iphdr *iph;
299 struct ipv6hdr _ipv6h;
301 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
305 if (ipv6h->version == 4) {
306 iph = (struct iphdr *)ipv6h;
307 offset += iph->ihl * 4;
309 /* special-case 6in4 tunnelling, as that is a common way to get
310 * v6 connectivity in the home
312 if (iph->protocol == IPPROTO_IPV6) {
313 ipv6h = skb_header_pointer(skb, offset,
314 sizeof(_ipv6h), &_ipv6h);
316 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
318 } else if (iph->protocol != IPPROTO_TCP) {
321 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
325 return ktime_mono_to_any(skb->skb_mstamp_ns, q->tk_offset);
328 /* There are a few scenarios where we will have to modify the txtime from
329 * what is read from next_txtime in sched_entry. They are:
330 * 1. If txtime is in the past,
331 * a. The gate for the traffic class is currently open and packet can be
332 * transmitted before it closes, schedule the packet right away.
333 * b. If the gate corresponding to the traffic class is going to open later
334 * in the cycle, set the txtime of packet to the interval start.
335 * 2. If txtime is in the future, there are packets corresponding to the
336 * current traffic class waiting to be transmitted. So, the following
337 * possibilities exist:
338 * a. We can transmit the packet before the window containing the txtime
340 * b. The window might close before the transmission can be completed
341 * successfully. So, schedule the packet in the next open window.
343 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
345 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
346 struct taprio_sched *q = qdisc_priv(sch);
347 struct sched_gate_list *sched, *admin;
348 ktime_t minimum_time, now, txtime;
349 int len, packet_transmit_time;
350 struct sched_entry *entry;
353 now = taprio_get_time(q);
354 minimum_time = ktime_add_ns(now, q->txtime_delay);
356 tcp_tstamp = get_tcp_tstamp(q, skb);
357 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
360 admin = rcu_dereference(q->admin_sched);
361 sched = rcu_dereference(q->oper_sched);
362 if (admin && ktime_after(minimum_time, admin->base_time))
363 switch_schedules(q, &admin, &sched);
365 /* Until the schedule starts, all the queues are open */
366 if (!sched || ktime_before(minimum_time, sched->base_time)) {
367 txtime = minimum_time;
371 len = qdisc_pkt_len(skb);
372 packet_transmit_time = length_to_duration(q, len);
375 sched_changed = false;
377 entry = find_entry_to_transmit(skb, sch, sched, admin,
379 &interval_start, &interval_end,
386 txtime = entry->next_txtime;
387 txtime = max_t(ktime_t, txtime, minimum_time);
388 txtime = max_t(ktime_t, txtime, interval_start);
390 if (admin && admin != sched &&
391 ktime_after(txtime, admin->base_time)) {
393 sched_changed = true;
397 transmit_end_time = ktime_add(txtime, packet_transmit_time);
398 minimum_time = transmit_end_time;
400 /* Update the txtime of current entry to the next time it's
403 if (ktime_after(transmit_end_time, interval_end))
404 entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
405 } while (sched_changed || ktime_after(transmit_end_time, interval_end));
407 entry->next_txtime = transmit_end_time;
414 static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
415 struct Qdisc *child, struct sk_buff **to_free)
417 struct taprio_sched *q = qdisc_priv(sch);
419 if (skb->sk && sock_flag(skb->sk, SOCK_TXTIME)) {
420 if (!is_valid_interval(skb, sch))
421 return qdisc_drop(skb, sch, to_free);
422 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
423 skb->tstamp = get_packet_txtime(skb, sch);
425 return qdisc_drop(skb, sch, to_free);
428 qdisc_qstats_backlog_inc(sch, skb);
431 return qdisc_enqueue(skb, child, to_free);
434 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
435 struct sk_buff **to_free)
437 struct taprio_sched *q = qdisc_priv(sch);
441 queue = skb_get_queue_mapping(skb);
443 child = q->qdiscs[queue];
444 if (unlikely(!child))
445 return qdisc_drop(skb, sch, to_free);
447 /* Large packets might not be transmitted when the transmission duration
448 * exceeds any configured interval. Therefore, segment the skb into
449 * smaller chunks. Skip it for the full offload case, as the driver
450 * and/or the hardware is expected to handle this.
452 if (skb_is_gso(skb) && !FULL_OFFLOAD_IS_ENABLED(q->flags)) {
453 unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb);
454 netdev_features_t features = netif_skb_features(skb);
455 struct sk_buff *segs, *nskb;
458 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
459 if (IS_ERR_OR_NULL(segs))
460 return qdisc_drop(skb, sch, to_free);
462 skb_list_walk_safe(segs, segs, nskb) {
463 skb_mark_not_on_list(segs);
464 qdisc_skb_cb(segs)->pkt_len = segs->len;
467 ret = taprio_enqueue_one(segs, sch, child, to_free);
468 if (ret != NET_XMIT_SUCCESS) {
469 if (net_xmit_drop_count(ret))
470 qdisc_qstats_drop(sch);
477 qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
480 return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
483 return taprio_enqueue_one(skb, sch, child, to_free);
486 static struct sk_buff *taprio_peek_soft(struct Qdisc *sch)
488 struct taprio_sched *q = qdisc_priv(sch);
489 struct net_device *dev = qdisc_dev(sch);
490 struct sched_entry *entry;
496 entry = rcu_dereference(q->current_entry);
497 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
503 for (i = 0; i < dev->num_tx_queues; i++) {
504 struct Qdisc *child = q->qdiscs[i];
508 if (unlikely(!child))
511 skb = child->ops->peek(child);
515 if (TXTIME_ASSIST_IS_ENABLED(q->flags))
518 prio = skb->priority;
519 tc = netdev_get_prio_tc_map(dev, prio);
521 if (!(gate_mask & BIT(tc)))
530 static struct sk_buff *taprio_peek_offload(struct Qdisc *sch)
532 struct taprio_sched *q = qdisc_priv(sch);
533 struct net_device *dev = qdisc_dev(sch);
537 for (i = 0; i < dev->num_tx_queues; i++) {
538 struct Qdisc *child = q->qdiscs[i];
540 if (unlikely(!child))
543 skb = child->ops->peek(child);
553 static struct sk_buff *taprio_peek(struct Qdisc *sch)
555 struct taprio_sched *q = qdisc_priv(sch);
560 static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry)
562 atomic_set(&entry->budget,
563 div64_u64((u64)entry->interval * 1000,
564 atomic64_read(&q->picos_per_byte)));
567 static struct sk_buff *taprio_dequeue_soft(struct Qdisc *sch)
569 struct taprio_sched *q = qdisc_priv(sch);
570 struct net_device *dev = qdisc_dev(sch);
571 struct sk_buff *skb = NULL;
572 struct sched_entry *entry;
577 entry = rcu_dereference(q->current_entry);
578 /* if there's no entry, it means that the schedule didn't
579 * start yet, so force all gates to be open, this is in
580 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
583 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
588 for (i = 0; i < dev->num_tx_queues; i++) {
589 struct Qdisc *child = q->qdiscs[i];
595 if (unlikely(!child))
598 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
599 skb = child->ops->dequeue(child);
605 skb = child->ops->peek(child);
609 prio = skb->priority;
610 tc = netdev_get_prio_tc_map(dev, prio);
612 if (!(gate_mask & BIT(tc))) {
617 len = qdisc_pkt_len(skb);
618 guard = ktime_add_ns(taprio_get_time(q),
619 length_to_duration(q, len));
621 /* In the case that there's no gate entry, there's no
624 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
625 ktime_after(guard, entry->close_time)) {
630 /* ... and no budget. */
631 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
632 atomic_sub_return(len, &entry->budget) < 0) {
637 skb = child->ops->dequeue(child);
642 qdisc_bstats_update(sch, skb);
643 qdisc_qstats_backlog_dec(sch, skb);
655 static struct sk_buff *taprio_dequeue_offload(struct Qdisc *sch)
657 struct taprio_sched *q = qdisc_priv(sch);
658 struct net_device *dev = qdisc_dev(sch);
662 for (i = 0; i < dev->num_tx_queues; i++) {
663 struct Qdisc *child = q->qdiscs[i];
665 if (unlikely(!child))
668 skb = child->ops->dequeue(child);
672 qdisc_bstats_update(sch, skb);
673 qdisc_qstats_backlog_dec(sch, skb);
682 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
684 struct taprio_sched *q = qdisc_priv(sch);
686 return q->dequeue(sch);
689 static bool should_restart_cycle(const struct sched_gate_list *oper,
690 const struct sched_entry *entry)
692 if (list_is_last(&entry->list, &oper->entries))
695 if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0)
701 static bool should_change_schedules(const struct sched_gate_list *admin,
702 const struct sched_gate_list *oper,
705 ktime_t next_base_time, extension_time;
710 next_base_time = sched_base_time(admin);
712 /* This is the simple case, the close_time would fall after
713 * the next schedule base_time.
715 if (ktime_compare(next_base_time, close_time) <= 0)
718 /* This is the cycle_time_extension case, if the close_time
719 * plus the amount that can be extended would fall after the
720 * next schedule base_time, we can extend the current schedule
723 extension_time = ktime_add_ns(close_time, oper->cycle_time_extension);
725 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
726 * how precisely the extension should be made. So after
727 * conformance testing, this logic may change.
729 if (ktime_compare(next_base_time, extension_time) <= 0)
735 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
737 struct taprio_sched *q = container_of(timer, struct taprio_sched,
739 struct sched_gate_list *oper, *admin;
740 struct sched_entry *entry, *next;
741 struct Qdisc *sch = q->root;
744 spin_lock(&q->current_entry_lock);
745 entry = rcu_dereference_protected(q->current_entry,
746 lockdep_is_held(&q->current_entry_lock));
747 oper = rcu_dereference_protected(q->oper_sched,
748 lockdep_is_held(&q->current_entry_lock));
749 admin = rcu_dereference_protected(q->admin_sched,
750 lockdep_is_held(&q->current_entry_lock));
753 switch_schedules(q, &admin, &oper);
755 /* This can happen in two cases: 1. this is the very first run
756 * of this function (i.e. we weren't running any schedule
757 * previously); 2. The previous schedule just ended. The first
758 * entry of all schedules are pre-calculated during the
759 * schedule initialization.
761 if (unlikely(!entry || entry->close_time == oper->base_time)) {
762 next = list_first_entry(&oper->entries, struct sched_entry,
764 close_time = next->close_time;
768 if (should_restart_cycle(oper, entry)) {
769 next = list_first_entry(&oper->entries, struct sched_entry,
771 oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time,
774 next = list_next_entry(entry, list);
777 close_time = ktime_add_ns(entry->close_time, next->interval);
778 close_time = min_t(ktime_t, close_time, oper->cycle_close_time);
780 if (should_change_schedules(admin, oper, close_time)) {
781 /* Set things so the next time this runs, the new
784 close_time = sched_base_time(admin);
785 switch_schedules(q, &admin, &oper);
788 next->close_time = close_time;
789 taprio_set_budget(q, next);
792 rcu_assign_pointer(q->current_entry, next);
793 spin_unlock(&q->current_entry_lock);
795 hrtimer_set_expires(&q->advance_timer, close_time);
798 __netif_schedule(sch);
801 return HRTIMER_RESTART;
804 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
805 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
806 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
807 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
808 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
811 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
812 [TCA_TAPRIO_ATTR_PRIOMAP] = {
813 .len = sizeof(struct tc_mqprio_qopt)
815 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
816 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
817 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
818 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
819 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 },
820 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
821 [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 },
822 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 },
825 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
826 struct sched_entry *entry,
827 struct netlink_ext_ack *extack)
829 int min_duration = length_to_duration(q, ETH_ZLEN);
832 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
833 entry->command = nla_get_u8(
834 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
836 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
837 entry->gate_mask = nla_get_u32(
838 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
840 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
841 interval = nla_get_u32(
842 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
844 /* The interval should allow at least the minimum ethernet
847 if (interval < min_duration) {
848 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
852 entry->interval = interval;
857 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
858 struct sched_entry *entry, int index,
859 struct netlink_ext_ack *extack)
861 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
864 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
867 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
871 entry->index = index;
873 return fill_sched_entry(q, tb, entry, extack);
876 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
877 struct sched_gate_list *sched,
878 struct netlink_ext_ack *extack)
887 nla_for_each_nested(n, list, rem) {
888 struct sched_entry *entry;
890 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
891 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
895 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
897 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
901 err = parse_sched_entry(q, n, entry, i, extack);
907 list_add_tail(&entry->list, &sched->entries);
911 sched->num_entries = i;
916 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
917 struct sched_gate_list *new,
918 struct netlink_ext_ack *extack)
922 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
923 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
927 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
928 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
930 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
931 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
933 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
934 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
936 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
937 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
942 if (!new->cycle_time) {
943 struct sched_entry *entry;
946 list_for_each_entry(entry, &new->entries, list)
947 cycle = ktime_add_ns(cycle, entry->interval);
950 NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
954 new->cycle_time = cycle;
960 static int taprio_parse_mqprio_opt(struct net_device *dev,
961 struct tc_mqprio_qopt *qopt,
962 struct netlink_ext_ack *extack,
967 if (!qopt && !dev->num_tc) {
968 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
972 /* If num_tc is already set, it means that the user already
973 * configured the mqprio part
978 /* Verify num_tc is not out of max range */
979 if (qopt->num_tc > TC_MAX_QUEUE) {
980 NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
984 /* taprio imposes that traffic classes map 1:n to tx queues */
985 if (qopt->num_tc > dev->num_tx_queues) {
986 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
990 /* Verify priority mapping uses valid tcs */
991 for (i = 0; i <= TC_BITMASK; i++) {
992 if (qopt->prio_tc_map[i] >= qopt->num_tc) {
993 NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
998 for (i = 0; i < qopt->num_tc; i++) {
999 unsigned int last = qopt->offset[i] + qopt->count[i];
1001 /* Verify the queue count is in tx range being equal to the
1002 * real_num_tx_queues indicates the last queue is in use.
1004 if (qopt->offset[i] >= dev->num_tx_queues ||
1006 last > dev->real_num_tx_queues) {
1007 NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
1011 if (TXTIME_ASSIST_IS_ENABLED(taprio_flags))
1014 /* Verify that the offset and counts do not overlap */
1015 for (j = i + 1; j < qopt->num_tc; j++) {
1016 if (last > qopt->offset[j]) {
1017 NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
1026 static int taprio_get_start_time(struct Qdisc *sch,
1027 struct sched_gate_list *sched,
1030 struct taprio_sched *q = qdisc_priv(sch);
1031 ktime_t now, base, cycle;
1034 base = sched_base_time(sched);
1035 now = taprio_get_time(q);
1037 if (ktime_after(base, now)) {
1042 cycle = sched->cycle_time;
1044 /* The qdisc is expected to have at least one sched_entry. Moreover,
1045 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
1046 * something went really wrong. In that case, we should warn about this
1047 * inconsistent state and return error.
1049 if (WARN_ON(!cycle))
1052 /* Schedule the start time for the beginning of the next
1055 n = div64_s64(ktime_sub_ns(now, base), cycle);
1056 *start = ktime_add_ns(base, (n + 1) * cycle);
1060 static void setup_first_close_time(struct taprio_sched *q,
1061 struct sched_gate_list *sched, ktime_t base)
1063 struct sched_entry *first;
1066 first = list_first_entry(&sched->entries,
1067 struct sched_entry, list);
1069 cycle = sched->cycle_time;
1071 /* FIXME: find a better place to do this */
1072 sched->cycle_close_time = ktime_add_ns(base, cycle);
1074 first->close_time = ktime_add_ns(base, first->interval);
1075 taprio_set_budget(q, first);
1076 rcu_assign_pointer(q->current_entry, NULL);
1079 static void taprio_start_sched(struct Qdisc *sch,
1080 ktime_t start, struct sched_gate_list *new)
1082 struct taprio_sched *q = qdisc_priv(sch);
1085 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1088 expires = hrtimer_get_expires(&q->advance_timer);
1090 expires = KTIME_MAX;
1092 /* If the new schedule starts before the next expiration, we
1093 * reprogram it to the earliest one, so we change the admin
1094 * schedule to the operational one at the right time.
1096 start = min_t(ktime_t, start, expires);
1098 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1101 static void taprio_set_picos_per_byte(struct net_device *dev,
1102 struct taprio_sched *q)
1104 struct ethtool_link_ksettings ecmd;
1105 int speed = SPEED_10;
1109 err = __ethtool_get_link_ksettings(dev, &ecmd);
1113 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1114 speed = ecmd.base.speed;
1117 picos_per_byte = (USEC_PER_SEC * 8) / speed;
1119 atomic64_set(&q->picos_per_byte, picos_per_byte);
1120 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1121 dev->name, (long long)atomic64_read(&q->picos_per_byte),
1125 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1128 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1129 struct net_device *qdev;
1130 struct taprio_sched *q;
1135 if (event != NETDEV_UP && event != NETDEV_CHANGE)
1138 spin_lock(&taprio_list_lock);
1139 list_for_each_entry(q, &taprio_list, taprio_list) {
1140 qdev = qdisc_dev(q->root);
1146 spin_unlock(&taprio_list_lock);
1149 taprio_set_picos_per_byte(dev, q);
1154 static void setup_txtime(struct taprio_sched *q,
1155 struct sched_gate_list *sched, ktime_t base)
1157 struct sched_entry *entry;
1160 list_for_each_entry(entry, &sched->entries, list) {
1161 entry->next_txtime = ktime_add_ns(base, interval);
1162 interval += entry->interval;
1166 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1168 struct __tc_taprio_qopt_offload *__offload;
1170 __offload = kzalloc(struct_size(__offload, offload.entries, num_entries),
1175 refcount_set(&__offload->users, 1);
1177 return &__offload->offload;
1180 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1183 struct __tc_taprio_qopt_offload *__offload;
1185 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1188 refcount_inc(&__offload->users);
1192 EXPORT_SYMBOL_GPL(taprio_offload_get);
1194 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1196 struct __tc_taprio_qopt_offload *__offload;
1198 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1201 if (!refcount_dec_and_test(&__offload->users))
1206 EXPORT_SYMBOL_GPL(taprio_offload_free);
1208 /* The function will only serve to keep the pointers to the "oper" and "admin"
1209 * schedules valid in relation to their base times, so when calling dump() the
1210 * users looks at the right schedules.
1211 * When using full offload, the admin configuration is promoted to oper at the
1212 * base_time in the PHC time domain. But because the system time is not
1213 * necessarily in sync with that, we can't just trigger a hrtimer to call
1214 * switch_schedules at the right hardware time.
1215 * At the moment we call this by hand right away from taprio, but in the future
1216 * it will be useful to create a mechanism for drivers to notify taprio of the
1217 * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1218 * This is left as TODO.
1220 static void taprio_offload_config_changed(struct taprio_sched *q)
1222 struct sched_gate_list *oper, *admin;
1224 spin_lock(&q->current_entry_lock);
1226 oper = rcu_dereference_protected(q->oper_sched,
1227 lockdep_is_held(&q->current_entry_lock));
1228 admin = rcu_dereference_protected(q->admin_sched,
1229 lockdep_is_held(&q->current_entry_lock));
1231 switch_schedules(q, &admin, &oper);
1233 spin_unlock(&q->current_entry_lock);
1236 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1238 u32 i, queue_mask = 0;
1240 for (i = 0; i < dev->num_tc; i++) {
1243 if (!(tc_mask & BIT(i)))
1246 offset = dev->tc_to_txq[i].offset;
1247 count = dev->tc_to_txq[i].count;
1249 queue_mask |= GENMASK(offset + count - 1, offset);
1255 static void taprio_sched_to_offload(struct net_device *dev,
1256 struct sched_gate_list *sched,
1257 struct tc_taprio_qopt_offload *offload)
1259 struct sched_entry *entry;
1262 offload->base_time = sched->base_time;
1263 offload->cycle_time = sched->cycle_time;
1264 offload->cycle_time_extension = sched->cycle_time_extension;
1266 list_for_each_entry(entry, &sched->entries, list) {
1267 struct tc_taprio_sched_entry *e = &offload->entries[i];
1269 e->command = entry->command;
1270 e->interval = entry->interval;
1271 e->gate_mask = tc_map_to_queue_mask(dev, entry->gate_mask);
1276 offload->num_entries = i;
1279 static int taprio_enable_offload(struct net_device *dev,
1280 struct taprio_sched *q,
1281 struct sched_gate_list *sched,
1282 struct netlink_ext_ack *extack)
1284 const struct net_device_ops *ops = dev->netdev_ops;
1285 struct tc_taprio_qopt_offload *offload;
1288 if (!ops->ndo_setup_tc) {
1289 NL_SET_ERR_MSG(extack,
1290 "Device does not support taprio offload");
1294 offload = taprio_offload_alloc(sched->num_entries);
1296 NL_SET_ERR_MSG(extack,
1297 "Not enough memory for enabling offload mode");
1300 offload->enable = 1;
1301 taprio_sched_to_offload(dev, sched, offload);
1303 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1305 NL_SET_ERR_MSG(extack,
1306 "Device failed to setup taprio offload");
1311 taprio_offload_free(offload);
1316 static int taprio_disable_offload(struct net_device *dev,
1317 struct taprio_sched *q,
1318 struct netlink_ext_ack *extack)
1320 const struct net_device_ops *ops = dev->netdev_ops;
1321 struct tc_taprio_qopt_offload *offload;
1324 if (!FULL_OFFLOAD_IS_ENABLED(q->flags))
1327 if (!ops->ndo_setup_tc)
1330 offload = taprio_offload_alloc(0);
1332 NL_SET_ERR_MSG(extack,
1333 "Not enough memory to disable offload mode");
1336 offload->enable = 0;
1338 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1340 NL_SET_ERR_MSG(extack,
1341 "Device failed to disable offload");
1346 taprio_offload_free(offload);
1351 /* If full offload is enabled, the only possible clockid is the net device's
1352 * PHC. For that reason, specifying a clockid through netlink is incorrect.
1353 * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1354 * in sync with the specified clockid via a user space daemon such as phc2sys.
1355 * For both software taprio and txtime-assist, the clockid is used for the
1356 * hrtimer that advances the schedule and hence mandatory.
1358 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1359 struct netlink_ext_ack *extack)
1361 struct taprio_sched *q = qdisc_priv(sch);
1362 struct net_device *dev = qdisc_dev(sch);
1365 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1366 const struct ethtool_ops *ops = dev->ethtool_ops;
1367 struct ethtool_ts_info info = {
1368 .cmd = ETHTOOL_GET_TS_INFO,
1372 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1373 NL_SET_ERR_MSG(extack,
1374 "The 'clockid' cannot be specified for full offload");
1378 if (ops && ops->get_ts_info)
1379 err = ops->get_ts_info(dev, &info);
1381 if (err || info.phc_index < 0) {
1382 NL_SET_ERR_MSG(extack,
1383 "Device does not have a PTP clock");
1387 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1388 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1390 /* We only support static clockids and we don't allow
1391 * for it to be modified after the first init.
1394 (q->clockid != -1 && q->clockid != clockid)) {
1395 NL_SET_ERR_MSG(extack,
1396 "Changing the 'clockid' of a running schedule is not supported");
1402 case CLOCK_REALTIME:
1403 q->tk_offset = TK_OFFS_REAL;
1405 case CLOCK_MONOTONIC:
1406 q->tk_offset = TK_OFFS_MAX;
1408 case CLOCK_BOOTTIME:
1409 q->tk_offset = TK_OFFS_BOOT;
1412 q->tk_offset = TK_OFFS_TAI;
1415 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1420 q->clockid = clockid;
1422 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1426 /* Everything went ok, return success. */
1433 static int taprio_mqprio_cmp(const struct net_device *dev,
1434 const struct tc_mqprio_qopt *mqprio)
1438 if (!mqprio || mqprio->num_tc != dev->num_tc)
1441 for (i = 0; i < mqprio->num_tc; i++)
1442 if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1443 dev->tc_to_txq[i].offset != mqprio->offset[i])
1446 for (i = 0; i <= TC_BITMASK; i++)
1447 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1453 /* The semantics of the 'flags' argument in relation to 'change()'
1454 * requests, are interpreted following two rules (which are applied in
1455 * this order): (1) an omitted 'flags' argument is interpreted as
1456 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1459 static int taprio_new_flags(const struct nlattr *attr, u32 old,
1460 struct netlink_ext_ack *extack)
1465 new = nla_get_u32(attr);
1467 if (old != TAPRIO_FLAGS_INVALID && old != new) {
1468 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1472 if (!taprio_flags_valid(new)) {
1473 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1480 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1481 struct netlink_ext_ack *extack)
1483 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1484 struct sched_gate_list *oper, *admin, *new_admin;
1485 struct taprio_sched *q = qdisc_priv(sch);
1486 struct net_device *dev = qdisc_dev(sch);
1487 struct tc_mqprio_qopt *mqprio = NULL;
1488 unsigned long flags;
1492 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1493 taprio_policy, extack);
1497 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1498 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1500 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1507 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1511 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1513 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1516 INIT_LIST_HEAD(&new_admin->entries);
1519 oper = rcu_dereference(q->oper_sched);
1520 admin = rcu_dereference(q->admin_sched);
1523 /* no changes - no new mqprio settings */
1524 if (!taprio_mqprio_cmp(dev, mqprio))
1527 if (mqprio && (oper || admin)) {
1528 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1533 err = parse_taprio_schedule(q, tb, new_admin, extack);
1537 if (new_admin->num_entries == 0) {
1538 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1543 err = taprio_parse_clockid(sch, tb, extack);
1547 taprio_set_picos_per_byte(dev, q);
1550 netdev_set_num_tc(dev, mqprio->num_tc);
1551 for (i = 0; i < mqprio->num_tc; i++)
1552 netdev_set_tc_queue(dev, i,
1556 /* Always use supplied priority mappings */
1557 for (i = 0; i <= TC_BITMASK; i++)
1558 netdev_set_prio_tc_map(dev, i,
1559 mqprio->prio_tc_map[i]);
1562 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1563 err = taprio_enable_offload(dev, q, new_admin, extack);
1565 err = taprio_disable_offload(dev, q, extack);
1569 /* Protects against enqueue()/dequeue() */
1570 spin_lock_bh(qdisc_lock(sch));
1572 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1573 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1574 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1579 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1582 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1583 !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1584 !hrtimer_active(&q->advance_timer)) {
1585 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1586 q->advance_timer.function = advance_sched;
1589 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1590 q->dequeue = taprio_dequeue_offload;
1591 q->peek = taprio_peek_offload;
1593 /* Be sure to always keep the function pointers
1594 * in a consistent state.
1596 q->dequeue = taprio_dequeue_soft;
1597 q->peek = taprio_peek_soft;
1600 err = taprio_get_start_time(sch, new_admin, &start);
1602 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1606 setup_txtime(q, new_admin, start);
1608 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1610 rcu_assign_pointer(q->oper_sched, new_admin);
1616 rcu_assign_pointer(q->admin_sched, new_admin);
1618 call_rcu(&admin->rcu, taprio_free_sched_cb);
1620 setup_first_close_time(q, new_admin, start);
1622 /* Protects against advance_sched() */
1623 spin_lock_irqsave(&q->current_entry_lock, flags);
1625 taprio_start_sched(sch, start, new_admin);
1627 rcu_assign_pointer(q->admin_sched, new_admin);
1629 call_rcu(&admin->rcu, taprio_free_sched_cb);
1631 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1633 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1634 taprio_offload_config_changed(q);
1641 spin_unlock_bh(qdisc_lock(sch));
1645 call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1650 static void taprio_reset(struct Qdisc *sch)
1652 struct taprio_sched *q = qdisc_priv(sch);
1653 struct net_device *dev = qdisc_dev(sch);
1656 hrtimer_cancel(&q->advance_timer);
1658 for (i = 0; i < dev->num_tx_queues; i++)
1660 qdisc_reset(q->qdiscs[i]);
1662 sch->qstats.backlog = 0;
1666 static void taprio_destroy(struct Qdisc *sch)
1668 struct taprio_sched *q = qdisc_priv(sch);
1669 struct net_device *dev = qdisc_dev(sch);
1672 spin_lock(&taprio_list_lock);
1673 list_del(&q->taprio_list);
1674 spin_unlock(&taprio_list_lock);
1677 taprio_disable_offload(dev, q, NULL);
1680 for (i = 0; i < dev->num_tx_queues; i++)
1681 qdisc_put(q->qdiscs[i]);
1687 netdev_reset_tc(dev);
1690 call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb);
1693 call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb);
1696 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1697 struct netlink_ext_ack *extack)
1699 struct taprio_sched *q = qdisc_priv(sch);
1700 struct net_device *dev = qdisc_dev(sch);
1703 spin_lock_init(&q->current_entry_lock);
1705 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1706 q->advance_timer.function = advance_sched;
1708 q->dequeue = taprio_dequeue_soft;
1709 q->peek = taprio_peek_soft;
1713 /* We only support static clockids. Use an invalid value as default
1714 * and get the valid one on taprio_change().
1717 q->flags = TAPRIO_FLAGS_INVALID;
1719 spin_lock(&taprio_list_lock);
1720 list_add(&q->taprio_list, &taprio_list);
1721 spin_unlock(&taprio_list_lock);
1723 if (sch->parent != TC_H_ROOT)
1726 if (!netif_is_multiqueue(dev))
1729 /* pre-allocate qdisc, attachment can't fail */
1730 q->qdiscs = kcalloc(dev->num_tx_queues,
1731 sizeof(q->qdiscs[0]),
1740 for (i = 0; i < dev->num_tx_queues; i++) {
1741 struct netdev_queue *dev_queue;
1742 struct Qdisc *qdisc;
1744 dev_queue = netdev_get_tx_queue(dev, i);
1745 qdisc = qdisc_create_dflt(dev_queue,
1747 TC_H_MAKE(TC_H_MAJ(sch->handle),
1753 if (i < dev->real_num_tx_queues)
1754 qdisc_hash_add(qdisc, false);
1756 q->qdiscs[i] = qdisc;
1759 return taprio_change(sch, opt, extack);
1762 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1765 struct net_device *dev = qdisc_dev(sch);
1766 unsigned long ntx = cl - 1;
1768 if (ntx >= dev->num_tx_queues)
1771 return netdev_get_tx_queue(dev, ntx);
1774 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1775 struct Qdisc *new, struct Qdisc **old,
1776 struct netlink_ext_ack *extack)
1778 struct taprio_sched *q = qdisc_priv(sch);
1779 struct net_device *dev = qdisc_dev(sch);
1780 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1785 if (dev->flags & IFF_UP)
1786 dev_deactivate(dev);
1788 *old = q->qdiscs[cl - 1];
1789 q->qdiscs[cl - 1] = new;
1792 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1794 if (dev->flags & IFF_UP)
1800 static int dump_entry(struct sk_buff *msg,
1801 const struct sched_entry *entry)
1803 struct nlattr *item;
1805 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1809 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1810 goto nla_put_failure;
1812 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1813 goto nla_put_failure;
1815 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1817 goto nla_put_failure;
1819 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1821 goto nla_put_failure;
1823 return nla_nest_end(msg, item);
1826 nla_nest_cancel(msg, item);
1830 static int dump_schedule(struct sk_buff *msg,
1831 const struct sched_gate_list *root)
1833 struct nlattr *entry_list;
1834 struct sched_entry *entry;
1836 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1837 root->base_time, TCA_TAPRIO_PAD))
1840 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1841 root->cycle_time, TCA_TAPRIO_PAD))
1844 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1845 root->cycle_time_extension, TCA_TAPRIO_PAD))
1848 entry_list = nla_nest_start_noflag(msg,
1849 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1853 list_for_each_entry(entry, &root->entries, list) {
1854 if (dump_entry(msg, entry) < 0)
1858 nla_nest_end(msg, entry_list);
1862 nla_nest_cancel(msg, entry_list);
1866 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1868 struct taprio_sched *q = qdisc_priv(sch);
1869 struct net_device *dev = qdisc_dev(sch);
1870 struct sched_gate_list *oper, *admin;
1871 struct tc_mqprio_qopt opt = { 0 };
1872 struct nlattr *nest, *sched_nest;
1876 oper = rcu_dereference(q->oper_sched);
1877 admin = rcu_dereference(q->admin_sched);
1879 opt.num_tc = netdev_get_num_tc(dev);
1880 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
1882 for (i = 0; i < netdev_get_num_tc(dev); i++) {
1883 opt.count[i] = dev->tc_to_txq[i].count;
1884 opt.offset[i] = dev->tc_to_txq[i].offset;
1887 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1891 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
1894 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1895 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
1898 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
1901 if (q->txtime_delay &&
1902 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
1905 if (oper && dump_schedule(skb, oper))
1911 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
1915 if (dump_schedule(skb, admin))
1918 nla_nest_end(skb, sched_nest);
1923 return nla_nest_end(skb, nest);
1926 nla_nest_cancel(skb, sched_nest);
1929 nla_nest_cancel(skb, nest);
1936 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
1938 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1943 return dev_queue->qdisc_sleeping;
1946 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
1948 unsigned int ntx = TC_H_MIN(classid);
1950 if (!taprio_queue_get(sch, ntx))
1955 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
1956 struct sk_buff *skb, struct tcmsg *tcm)
1958 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1960 tcm->tcm_parent = TC_H_ROOT;
1961 tcm->tcm_handle |= TC_H_MIN(cl);
1962 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
1967 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
1968 struct gnet_dump *d)
1972 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1974 sch = dev_queue->qdisc_sleeping;
1975 if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 ||
1976 qdisc_qstats_copy(d, sch) < 0)
1981 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1983 struct net_device *dev = qdisc_dev(sch);
1989 arg->count = arg->skip;
1990 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
1991 if (arg->fn(sch, ntx + 1, arg) < 0) {
1999 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
2002 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
2005 static const struct Qdisc_class_ops taprio_class_ops = {
2006 .graft = taprio_graft,
2007 .leaf = taprio_leaf,
2008 .find = taprio_find,
2009 .walk = taprio_walk,
2010 .dump = taprio_dump_class,
2011 .dump_stats = taprio_dump_class_stats,
2012 .select_queue = taprio_select_queue,
2015 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
2016 .cl_ops = &taprio_class_ops,
2018 .priv_size = sizeof(struct taprio_sched),
2019 .init = taprio_init,
2020 .change = taprio_change,
2021 .destroy = taprio_destroy,
2022 .reset = taprio_reset,
2023 .peek = taprio_peek,
2024 .dequeue = taprio_dequeue,
2025 .enqueue = taprio_enqueue,
2026 .dump = taprio_dump,
2027 .owner = THIS_MODULE,
2030 static struct notifier_block taprio_device_notifier = {
2031 .notifier_call = taprio_dev_notifier,
2034 static int __init taprio_module_init(void)
2036 int err = register_netdevice_notifier(&taprio_device_notifier);
2041 return register_qdisc(&taprio_qdisc_ops);
2044 static void __exit taprio_module_exit(void)
2046 unregister_qdisc(&taprio_qdisc_ops);
2047 unregister_netdevice_notifier(&taprio_device_notifier);
2050 module_init(taprio_module_init);
2051 module_exit(taprio_module_exit);
2052 MODULE_LICENSE("GPL");