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/types.h>
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/list.h>
14 #include <linux/errno.h>
15 #include <linux/skbuff.h>
16 #include <linux/math64.h>
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
19 #include <linux/rcupdate.h>
20 #include <net/netlink.h>
21 #include <net/pkt_sched.h>
22 #include <net/pkt_cls.h>
23 #include <net/sch_generic.h>
27 static LIST_HEAD(taprio_list);
28 static DEFINE_SPINLOCK(taprio_list_lock);
30 #define TAPRIO_ALL_GATES_OPEN -1
32 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
33 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
34 #define TAPRIO_FLAGS_INVALID U32_MAX
37 struct list_head list;
39 /* The instant that this entry "closes" and the next one
40 * should open, the qdisc will make some effort so that no
41 * packet leaves after this time.
52 struct sched_gate_list {
54 struct list_head entries;
56 ktime_t cycle_close_time;
58 s64 cycle_time_extension;
63 struct Qdisc **qdiscs;
66 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_mono_to_any(const struct taprio_sched *q, ktime_t mono)
100 /* This pairs with WRITE_ONCE() in taprio_parse_clockid() */
101 enum tk_offsets tk_offset = READ_ONCE(q->tk_offset);
107 return ktime_mono_to_any(mono, tk_offset);
111 static ktime_t taprio_get_time(const struct taprio_sched *q)
113 return taprio_mono_to_any(q, ktime_get());
116 static void taprio_free_sched_cb(struct rcu_head *head)
118 struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
119 struct sched_entry *entry, *n;
124 list_for_each_entry_safe(entry, n, &sched->entries, list) {
125 list_del(&entry->list);
132 static void switch_schedules(struct taprio_sched *q,
133 struct sched_gate_list **admin,
134 struct sched_gate_list **oper)
136 rcu_assign_pointer(q->oper_sched, *admin);
137 rcu_assign_pointer(q->admin_sched, NULL);
140 call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
146 /* Get how much time has been already elapsed in the current cycle. */
147 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
149 ktime_t time_since_sched_start;
152 time_since_sched_start = ktime_sub(time, sched->base_time);
153 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
158 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
159 struct sched_gate_list *admin,
160 struct sched_entry *entry,
163 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
164 ktime_t intv_end, cycle_ext_end, cycle_end;
166 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
167 intv_end = ktime_add_ns(intv_start, entry->interval);
168 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
170 if (ktime_before(intv_end, cycle_end))
172 else if (admin && admin != sched &&
173 ktime_after(admin->base_time, cycle_end) &&
174 ktime_before(admin->base_time, cycle_ext_end))
175 return admin->base_time;
180 static int length_to_duration(struct taprio_sched *q, int len)
182 return div_u64(len * atomic64_read(&q->picos_per_byte), 1000);
185 /* Returns the entry corresponding to next available interval. If
186 * validate_interval is set, it only validates whether the timestamp occurs
187 * when the gate corresponding to the skb's traffic class is open.
189 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
191 struct sched_gate_list *sched,
192 struct sched_gate_list *admin,
194 ktime_t *interval_start,
195 ktime_t *interval_end,
196 bool validate_interval)
198 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
199 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
200 struct sched_entry *entry = NULL, *entry_found = NULL;
201 struct taprio_sched *q = qdisc_priv(sch);
202 struct net_device *dev = qdisc_dev(sch);
203 bool entry_available = false;
207 tc = netdev_get_prio_tc_map(dev, skb->priority);
208 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
216 cycle = sched->cycle_time;
217 cycle_elapsed = get_cycle_time_elapsed(sched, time);
218 curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
219 cycle_end = ktime_add_ns(curr_intv_end, cycle);
221 list_for_each_entry(entry, &sched->entries, list) {
222 curr_intv_start = curr_intv_end;
223 curr_intv_end = get_interval_end_time(sched, admin, entry,
226 if (ktime_after(curr_intv_start, cycle_end))
229 if (!(entry->gate_mask & BIT(tc)) ||
230 packet_transmit_time > entry->interval)
233 txtime = entry->next_txtime;
235 if (ktime_before(txtime, time) || validate_interval) {
236 transmit_end_time = ktime_add_ns(time, packet_transmit_time);
237 if ((ktime_before(curr_intv_start, time) &&
238 ktime_before(transmit_end_time, curr_intv_end)) ||
239 (ktime_after(curr_intv_start, time) && !validate_interval)) {
241 *interval_start = curr_intv_start;
242 *interval_end = curr_intv_end;
244 } else if (!entry_available && !validate_interval) {
245 /* Here, we are just trying to find out the
246 * first available interval in the next cycle.
250 *interval_start = ktime_add_ns(curr_intv_start, cycle);
251 *interval_end = ktime_add_ns(curr_intv_end, cycle);
253 } else if (ktime_before(txtime, earliest_txtime) &&
255 earliest_txtime = txtime;
257 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
258 *interval_start = ktime_add(curr_intv_start, n * cycle);
259 *interval_end = ktime_add(curr_intv_end, n * cycle);
266 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
268 struct taprio_sched *q = qdisc_priv(sch);
269 struct sched_gate_list *sched, *admin;
270 ktime_t interval_start, interval_end;
271 struct sched_entry *entry;
274 sched = rcu_dereference(q->oper_sched);
275 admin = rcu_dereference(q->admin_sched);
277 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
278 &interval_start, &interval_end, true);
284 static bool taprio_flags_valid(u32 flags)
286 /* Make sure no other flag bits are set. */
287 if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST |
288 TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
290 /* txtime-assist and full offload are mutually exclusive */
291 if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
292 (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
297 /* This returns the tstamp value set by TCP in terms of the set clock. */
298 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
300 unsigned int offset = skb_network_offset(skb);
301 const struct ipv6hdr *ipv6h;
302 const struct iphdr *iph;
303 struct ipv6hdr _ipv6h;
305 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
309 if (ipv6h->version == 4) {
310 iph = (struct iphdr *)ipv6h;
311 offset += iph->ihl * 4;
313 /* special-case 6in4 tunnelling, as that is a common way to get
314 * v6 connectivity in the home
316 if (iph->protocol == IPPROTO_IPV6) {
317 ipv6h = skb_header_pointer(skb, offset,
318 sizeof(_ipv6h), &_ipv6h);
320 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
322 } else if (iph->protocol != IPPROTO_TCP) {
325 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
329 return taprio_mono_to_any(q, skb->skb_mstamp_ns);
332 /* There are a few scenarios where we will have to modify the txtime from
333 * what is read from next_txtime in sched_entry. They are:
334 * 1. If txtime is in the past,
335 * a. The gate for the traffic class is currently open and packet can be
336 * transmitted before it closes, schedule the packet right away.
337 * b. If the gate corresponding to the traffic class is going to open later
338 * in the cycle, set the txtime of packet to the interval start.
339 * 2. If txtime is in the future, there are packets corresponding to the
340 * current traffic class waiting to be transmitted. So, the following
341 * possibilities exist:
342 * a. We can transmit the packet before the window containing the txtime
344 * b. The window might close before the transmission can be completed
345 * successfully. So, schedule the packet in the next open window.
347 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
349 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
350 struct taprio_sched *q = qdisc_priv(sch);
351 struct sched_gate_list *sched, *admin;
352 ktime_t minimum_time, now, txtime;
353 int len, packet_transmit_time;
354 struct sched_entry *entry;
357 now = taprio_get_time(q);
358 minimum_time = ktime_add_ns(now, q->txtime_delay);
360 tcp_tstamp = get_tcp_tstamp(q, skb);
361 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
364 admin = rcu_dereference(q->admin_sched);
365 sched = rcu_dereference(q->oper_sched);
366 if (admin && ktime_after(minimum_time, admin->base_time))
367 switch_schedules(q, &admin, &sched);
369 /* Until the schedule starts, all the queues are open */
370 if (!sched || ktime_before(minimum_time, sched->base_time)) {
371 txtime = minimum_time;
375 len = qdisc_pkt_len(skb);
376 packet_transmit_time = length_to_duration(q, len);
381 entry = find_entry_to_transmit(skb, sch, sched, admin,
383 &interval_start, &interval_end,
390 txtime = entry->next_txtime;
391 txtime = max_t(ktime_t, txtime, minimum_time);
392 txtime = max_t(ktime_t, txtime, interval_start);
394 if (admin && admin != sched &&
395 ktime_after(txtime, admin->base_time)) {
401 transmit_end_time = ktime_add(txtime, packet_transmit_time);
402 minimum_time = transmit_end_time;
404 /* Update the txtime of current entry to the next time it's
407 if (ktime_after(transmit_end_time, interval_end))
408 entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
409 } while (sched_changed || ktime_after(transmit_end_time, interval_end));
411 entry->next_txtime = transmit_end_time;
418 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
419 struct sk_buff **to_free)
421 struct taprio_sched *q = qdisc_priv(sch);
425 queue = skb_get_queue_mapping(skb);
427 child = q->qdiscs[queue];
428 if (unlikely(!child))
429 return qdisc_drop(skb, sch, to_free);
431 /* sk_flags are only safe to use on full sockets. */
432 if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) {
433 if (!is_valid_interval(skb, sch))
434 return qdisc_drop(skb, sch, to_free);
435 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
436 skb->tstamp = get_packet_txtime(skb, sch);
438 return qdisc_drop(skb, sch, to_free);
441 qdisc_qstats_backlog_inc(sch, skb);
444 return qdisc_enqueue(skb, child, to_free);
447 static struct sk_buff *taprio_peek_soft(struct Qdisc *sch)
449 struct taprio_sched *q = qdisc_priv(sch);
450 struct net_device *dev = qdisc_dev(sch);
451 struct sched_entry *entry;
457 entry = rcu_dereference(q->current_entry);
458 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
464 for (i = 0; i < dev->num_tx_queues; i++) {
465 struct Qdisc *child = q->qdiscs[i];
469 if (unlikely(!child))
472 skb = child->ops->peek(child);
476 if (TXTIME_ASSIST_IS_ENABLED(q->flags))
479 prio = skb->priority;
480 tc = netdev_get_prio_tc_map(dev, prio);
482 if (!(gate_mask & BIT(tc)))
491 static struct sk_buff *taprio_peek_offload(struct Qdisc *sch)
493 struct taprio_sched *q = qdisc_priv(sch);
494 struct net_device *dev = qdisc_dev(sch);
498 for (i = 0; i < dev->num_tx_queues; i++) {
499 struct Qdisc *child = q->qdiscs[i];
501 if (unlikely(!child))
504 skb = child->ops->peek(child);
514 static struct sk_buff *taprio_peek(struct Qdisc *sch)
516 struct taprio_sched *q = qdisc_priv(sch);
521 static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry)
523 atomic_set(&entry->budget,
524 div64_u64((u64)entry->interval * 1000,
525 atomic64_read(&q->picos_per_byte)));
528 static struct sk_buff *taprio_dequeue_soft(struct Qdisc *sch)
530 struct taprio_sched *q = qdisc_priv(sch);
531 struct net_device *dev = qdisc_dev(sch);
532 struct sk_buff *skb = NULL;
533 struct sched_entry *entry;
538 entry = rcu_dereference(q->current_entry);
539 /* if there's no entry, it means that the schedule didn't
540 * start yet, so force all gates to be open, this is in
541 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
544 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
549 for (i = 0; i < dev->num_tx_queues; i++) {
550 struct Qdisc *child = q->qdiscs[i];
556 if (unlikely(!child))
559 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
560 skb = child->ops->dequeue(child);
566 skb = child->ops->peek(child);
570 prio = skb->priority;
571 tc = netdev_get_prio_tc_map(dev, prio);
573 if (!(gate_mask & BIT(tc))) {
578 len = qdisc_pkt_len(skb);
579 guard = ktime_add_ns(taprio_get_time(q),
580 length_to_duration(q, len));
582 /* In the case that there's no gate entry, there's no
585 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
586 ktime_after(guard, entry->close_time)) {
591 /* ... and no budget. */
592 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
593 atomic_sub_return(len, &entry->budget) < 0) {
598 skb = child->ops->dequeue(child);
603 qdisc_bstats_update(sch, skb);
604 qdisc_qstats_backlog_dec(sch, skb);
616 static struct sk_buff *taprio_dequeue_offload(struct Qdisc *sch)
618 struct taprio_sched *q = qdisc_priv(sch);
619 struct net_device *dev = qdisc_dev(sch);
623 for (i = 0; i < dev->num_tx_queues; i++) {
624 struct Qdisc *child = q->qdiscs[i];
626 if (unlikely(!child))
629 skb = child->ops->dequeue(child);
633 qdisc_bstats_update(sch, skb);
634 qdisc_qstats_backlog_dec(sch, skb);
643 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
645 struct taprio_sched *q = qdisc_priv(sch);
647 return q->dequeue(sch);
650 static bool should_restart_cycle(const struct sched_gate_list *oper,
651 const struct sched_entry *entry)
653 if (list_is_last(&entry->list, &oper->entries))
656 if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0)
662 static bool should_change_schedules(const struct sched_gate_list *admin,
663 const struct sched_gate_list *oper,
666 ktime_t next_base_time, extension_time;
671 next_base_time = sched_base_time(admin);
673 /* This is the simple case, the close_time would fall after
674 * the next schedule base_time.
676 if (ktime_compare(next_base_time, close_time) <= 0)
679 /* This is the cycle_time_extension case, if the close_time
680 * plus the amount that can be extended would fall after the
681 * next schedule base_time, we can extend the current schedule
684 extension_time = ktime_add_ns(close_time, oper->cycle_time_extension);
686 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
687 * how precisely the extension should be made. So after
688 * conformance testing, this logic may change.
690 if (ktime_compare(next_base_time, extension_time) <= 0)
696 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
698 struct taprio_sched *q = container_of(timer, struct taprio_sched,
700 struct sched_gate_list *oper, *admin;
701 struct sched_entry *entry, *next;
702 struct Qdisc *sch = q->root;
705 spin_lock(&q->current_entry_lock);
706 entry = rcu_dereference_protected(q->current_entry,
707 lockdep_is_held(&q->current_entry_lock));
708 oper = rcu_dereference_protected(q->oper_sched,
709 lockdep_is_held(&q->current_entry_lock));
710 admin = rcu_dereference_protected(q->admin_sched,
711 lockdep_is_held(&q->current_entry_lock));
714 switch_schedules(q, &admin, &oper);
716 /* This can happen in two cases: 1. this is the very first run
717 * of this function (i.e. we weren't running any schedule
718 * previously); 2. The previous schedule just ended. The first
719 * entry of all schedules are pre-calculated during the
720 * schedule initialization.
722 if (unlikely(!entry || entry->close_time == oper->base_time)) {
723 next = list_first_entry(&oper->entries, struct sched_entry,
725 close_time = next->close_time;
729 if (should_restart_cycle(oper, entry)) {
730 next = list_first_entry(&oper->entries, struct sched_entry,
732 oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time,
735 next = list_next_entry(entry, list);
738 close_time = ktime_add_ns(entry->close_time, next->interval);
739 close_time = min_t(ktime_t, close_time, oper->cycle_close_time);
741 if (should_change_schedules(admin, oper, close_time)) {
742 /* Set things so the next time this runs, the new
745 close_time = sched_base_time(admin);
746 switch_schedules(q, &admin, &oper);
749 next->close_time = close_time;
750 taprio_set_budget(q, next);
753 rcu_assign_pointer(q->current_entry, next);
754 spin_unlock(&q->current_entry_lock);
756 hrtimer_set_expires(&q->advance_timer, close_time);
759 __netif_schedule(sch);
762 return HRTIMER_RESTART;
765 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
766 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
767 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
768 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
769 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
772 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
773 [TCA_TAPRIO_ATTR_PRIOMAP] = {
774 .len = sizeof(struct tc_mqprio_qopt)
776 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
777 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
778 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
779 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
780 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 },
781 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
782 [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 },
783 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 },
786 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
787 struct sched_entry *entry,
788 struct netlink_ext_ack *extack)
790 int min_duration = length_to_duration(q, ETH_ZLEN);
793 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
794 entry->command = nla_get_u8(
795 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
797 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
798 entry->gate_mask = nla_get_u32(
799 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
801 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
802 interval = nla_get_u32(
803 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
805 /* The interval should allow at least the minimum ethernet
808 if (interval < min_duration) {
809 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
813 entry->interval = interval;
818 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
819 struct sched_entry *entry, int index,
820 struct netlink_ext_ack *extack)
822 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
825 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
828 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
832 entry->index = index;
834 return fill_sched_entry(q, tb, entry, extack);
837 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
838 struct sched_gate_list *sched,
839 struct netlink_ext_ack *extack)
848 nla_for_each_nested(n, list, rem) {
849 struct sched_entry *entry;
851 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
852 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
856 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
858 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
862 err = parse_sched_entry(q, n, entry, i, extack);
868 list_add_tail(&entry->list, &sched->entries);
872 sched->num_entries = i;
877 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
878 struct sched_gate_list *new,
879 struct netlink_ext_ack *extack)
883 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
884 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
888 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
889 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
891 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
892 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
894 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
895 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
897 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
898 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
903 if (!new->cycle_time) {
904 struct sched_entry *entry;
907 list_for_each_entry(entry, &new->entries, list)
908 cycle = ktime_add_ns(cycle, entry->interval);
911 NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
915 new->cycle_time = cycle;
921 static int taprio_parse_mqprio_opt(struct net_device *dev,
922 struct tc_mqprio_qopt *qopt,
923 struct netlink_ext_ack *extack,
928 if (!qopt && !dev->num_tc) {
929 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
933 /* If num_tc is already set, it means that the user already
934 * configured the mqprio part
939 /* Verify num_tc is not out of max range */
940 if (qopt->num_tc > TC_MAX_QUEUE) {
941 NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
945 /* taprio imposes that traffic classes map 1:n to tx queues */
946 if (qopt->num_tc > dev->num_tx_queues) {
947 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
951 /* Verify priority mapping uses valid tcs */
952 for (i = 0; i <= TC_BITMASK; i++) {
953 if (qopt->prio_tc_map[i] >= qopt->num_tc) {
954 NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
959 for (i = 0; i < qopt->num_tc; i++) {
960 unsigned int last = qopt->offset[i] + qopt->count[i];
962 /* Verify the queue count is in tx range being equal to the
963 * real_num_tx_queues indicates the last queue is in use.
965 if (qopt->offset[i] >= dev->num_tx_queues ||
967 last > dev->real_num_tx_queues) {
968 NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
972 if (TXTIME_ASSIST_IS_ENABLED(taprio_flags))
975 /* Verify that the offset and counts do not overlap */
976 for (j = i + 1; j < qopt->num_tc; j++) {
977 if (last > qopt->offset[j]) {
978 NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
987 static int taprio_get_start_time(struct Qdisc *sch,
988 struct sched_gate_list *sched,
991 struct taprio_sched *q = qdisc_priv(sch);
992 ktime_t now, base, cycle;
995 base = sched_base_time(sched);
996 now = taprio_get_time(q);
998 if (ktime_after(base, now)) {
1003 cycle = sched->cycle_time;
1005 /* The qdisc is expected to have at least one sched_entry. Moreover,
1006 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
1007 * something went really wrong. In that case, we should warn about this
1008 * inconsistent state and return error.
1010 if (WARN_ON(!cycle))
1013 /* Schedule the start time for the beginning of the next
1016 n = div64_s64(ktime_sub_ns(now, base), cycle);
1017 *start = ktime_add_ns(base, (n + 1) * cycle);
1021 static void setup_first_close_time(struct taprio_sched *q,
1022 struct sched_gate_list *sched, ktime_t base)
1024 struct sched_entry *first;
1027 first = list_first_entry(&sched->entries,
1028 struct sched_entry, list);
1030 cycle = sched->cycle_time;
1032 /* FIXME: find a better place to do this */
1033 sched->cycle_close_time = ktime_add_ns(base, cycle);
1035 first->close_time = ktime_add_ns(base, first->interval);
1036 taprio_set_budget(q, first);
1037 rcu_assign_pointer(q->current_entry, NULL);
1040 static void taprio_start_sched(struct Qdisc *sch,
1041 ktime_t start, struct sched_gate_list *new)
1043 struct taprio_sched *q = qdisc_priv(sch);
1046 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1049 expires = hrtimer_get_expires(&q->advance_timer);
1051 expires = KTIME_MAX;
1053 /* If the new schedule starts before the next expiration, we
1054 * reprogram it to the earliest one, so we change the admin
1055 * schedule to the operational one at the right time.
1057 start = min_t(ktime_t, start, expires);
1059 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1062 static void taprio_set_picos_per_byte(struct net_device *dev,
1063 struct taprio_sched *q)
1065 struct ethtool_link_ksettings ecmd;
1066 int speed = SPEED_10;
1070 err = __ethtool_get_link_ksettings(dev, &ecmd);
1074 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1075 speed = ecmd.base.speed;
1078 picos_per_byte = (USEC_PER_SEC * 8) / speed;
1080 atomic64_set(&q->picos_per_byte, picos_per_byte);
1081 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1082 dev->name, (long long)atomic64_read(&q->picos_per_byte),
1086 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1089 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1090 struct net_device *qdev;
1091 struct taprio_sched *q;
1096 if (event != NETDEV_UP && event != NETDEV_CHANGE)
1099 spin_lock(&taprio_list_lock);
1100 list_for_each_entry(q, &taprio_list, taprio_list) {
1101 qdev = qdisc_dev(q->root);
1107 spin_unlock(&taprio_list_lock);
1110 taprio_set_picos_per_byte(dev, q);
1115 static void setup_txtime(struct taprio_sched *q,
1116 struct sched_gate_list *sched, ktime_t base)
1118 struct sched_entry *entry;
1121 list_for_each_entry(entry, &sched->entries, list) {
1122 entry->next_txtime = ktime_add_ns(base, interval);
1123 interval += entry->interval;
1127 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1129 size_t size = sizeof(struct tc_taprio_sched_entry) * num_entries +
1130 sizeof(struct __tc_taprio_qopt_offload);
1131 struct __tc_taprio_qopt_offload *__offload;
1133 __offload = kzalloc(size, GFP_KERNEL);
1137 refcount_set(&__offload->users, 1);
1139 return &__offload->offload;
1142 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1145 struct __tc_taprio_qopt_offload *__offload;
1147 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1150 refcount_inc(&__offload->users);
1154 EXPORT_SYMBOL_GPL(taprio_offload_get);
1156 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1158 struct __tc_taprio_qopt_offload *__offload;
1160 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1163 if (!refcount_dec_and_test(&__offload->users))
1168 EXPORT_SYMBOL_GPL(taprio_offload_free);
1170 /* The function will only serve to keep the pointers to the "oper" and "admin"
1171 * schedules valid in relation to their base times, so when calling dump() the
1172 * users looks at the right schedules.
1173 * When using full offload, the admin configuration is promoted to oper at the
1174 * base_time in the PHC time domain. But because the system time is not
1175 * necessarily in sync with that, we can't just trigger a hrtimer to call
1176 * switch_schedules at the right hardware time.
1177 * At the moment we call this by hand right away from taprio, but in the future
1178 * it will be useful to create a mechanism for drivers to notify taprio of the
1179 * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1180 * This is left as TODO.
1182 static void taprio_offload_config_changed(struct taprio_sched *q)
1184 struct sched_gate_list *oper, *admin;
1186 spin_lock(&q->current_entry_lock);
1188 oper = rcu_dereference_protected(q->oper_sched,
1189 lockdep_is_held(&q->current_entry_lock));
1190 admin = rcu_dereference_protected(q->admin_sched,
1191 lockdep_is_held(&q->current_entry_lock));
1193 switch_schedules(q, &admin, &oper);
1195 spin_unlock(&q->current_entry_lock);
1198 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1200 u32 i, queue_mask = 0;
1202 for (i = 0; i < dev->num_tc; i++) {
1205 if (!(tc_mask & BIT(i)))
1208 offset = dev->tc_to_txq[i].offset;
1209 count = dev->tc_to_txq[i].count;
1211 queue_mask |= GENMASK(offset + count - 1, offset);
1217 static void taprio_sched_to_offload(struct net_device *dev,
1218 struct sched_gate_list *sched,
1219 struct tc_taprio_qopt_offload *offload)
1221 struct sched_entry *entry;
1224 offload->base_time = sched->base_time;
1225 offload->cycle_time = sched->cycle_time;
1226 offload->cycle_time_extension = sched->cycle_time_extension;
1228 list_for_each_entry(entry, &sched->entries, list) {
1229 struct tc_taprio_sched_entry *e = &offload->entries[i];
1231 e->command = entry->command;
1232 e->interval = entry->interval;
1233 e->gate_mask = tc_map_to_queue_mask(dev, entry->gate_mask);
1238 offload->num_entries = i;
1241 static int taprio_enable_offload(struct net_device *dev,
1242 struct taprio_sched *q,
1243 struct sched_gate_list *sched,
1244 struct netlink_ext_ack *extack)
1246 const struct net_device_ops *ops = dev->netdev_ops;
1247 struct tc_taprio_qopt_offload *offload;
1250 if (!ops->ndo_setup_tc) {
1251 NL_SET_ERR_MSG(extack,
1252 "Device does not support taprio offload");
1256 offload = taprio_offload_alloc(sched->num_entries);
1258 NL_SET_ERR_MSG(extack,
1259 "Not enough memory for enabling offload mode");
1262 offload->enable = 1;
1263 taprio_sched_to_offload(dev, sched, offload);
1265 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1267 NL_SET_ERR_MSG(extack,
1268 "Device failed to setup taprio offload");
1272 q->offloaded = true;
1275 taprio_offload_free(offload);
1280 static int taprio_disable_offload(struct net_device *dev,
1281 struct taprio_sched *q,
1282 struct netlink_ext_ack *extack)
1284 const struct net_device_ops *ops = dev->netdev_ops;
1285 struct tc_taprio_qopt_offload *offload;
1291 offload = taprio_offload_alloc(0);
1293 NL_SET_ERR_MSG(extack,
1294 "Not enough memory to disable offload mode");
1297 offload->enable = 0;
1299 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1301 NL_SET_ERR_MSG(extack,
1302 "Device failed to disable offload");
1306 q->offloaded = false;
1309 taprio_offload_free(offload);
1314 /* If full offload is enabled, the only possible clockid is the net device's
1315 * PHC. For that reason, specifying a clockid through netlink is incorrect.
1316 * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1317 * in sync with the specified clockid via a user space daemon such as phc2sys.
1318 * For both software taprio and txtime-assist, the clockid is used for the
1319 * hrtimer that advances the schedule and hence mandatory.
1321 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1322 struct netlink_ext_ack *extack)
1324 struct taprio_sched *q = qdisc_priv(sch);
1325 struct net_device *dev = qdisc_dev(sch);
1328 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1329 const struct ethtool_ops *ops = dev->ethtool_ops;
1330 struct ethtool_ts_info info = {
1331 .cmd = ETHTOOL_GET_TS_INFO,
1335 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1336 NL_SET_ERR_MSG(extack,
1337 "The 'clockid' cannot be specified for full offload");
1341 if (ops && ops->get_ts_info)
1342 err = ops->get_ts_info(dev, &info);
1344 if (err || info.phc_index < 0) {
1345 NL_SET_ERR_MSG(extack,
1346 "Device does not have a PTP clock");
1350 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1351 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1352 enum tk_offsets tk_offset;
1354 /* We only support static clockids and we don't allow
1355 * for it to be modified after the first init.
1358 (q->clockid != -1 && q->clockid != clockid)) {
1359 NL_SET_ERR_MSG(extack,
1360 "Changing the 'clockid' of a running schedule is not supported");
1366 case CLOCK_REALTIME:
1367 tk_offset = TK_OFFS_REAL;
1369 case CLOCK_MONOTONIC:
1370 tk_offset = TK_OFFS_MAX;
1372 case CLOCK_BOOTTIME:
1373 tk_offset = TK_OFFS_BOOT;
1376 tk_offset = TK_OFFS_TAI;
1379 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1383 /* This pairs with READ_ONCE() in taprio_mono_to_any */
1384 WRITE_ONCE(q->tk_offset, tk_offset);
1386 q->clockid = clockid;
1388 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1392 /* Everything went ok, return success. */
1399 static int taprio_mqprio_cmp(const struct net_device *dev,
1400 const struct tc_mqprio_qopt *mqprio)
1404 if (!mqprio || mqprio->num_tc != dev->num_tc)
1407 for (i = 0; i < mqprio->num_tc; i++)
1408 if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1409 dev->tc_to_txq[i].offset != mqprio->offset[i])
1412 for (i = 0; i <= TC_BITMASK; i++)
1413 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1419 /* The semantics of the 'flags' argument in relation to 'change()'
1420 * requests, are interpreted following two rules (which are applied in
1421 * this order): (1) an omitted 'flags' argument is interpreted as
1422 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1425 static int taprio_new_flags(const struct nlattr *attr, u32 old,
1426 struct netlink_ext_ack *extack)
1431 new = nla_get_u32(attr);
1433 if (old != TAPRIO_FLAGS_INVALID && old != new) {
1434 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1438 if (!taprio_flags_valid(new)) {
1439 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1446 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1447 struct netlink_ext_ack *extack)
1449 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1450 struct sched_gate_list *oper, *admin, *new_admin;
1451 struct taprio_sched *q = qdisc_priv(sch);
1452 struct net_device *dev = qdisc_dev(sch);
1453 struct tc_mqprio_qopt *mqprio = NULL;
1454 unsigned long flags;
1458 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1459 taprio_policy, extack);
1463 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1464 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1466 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1473 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1477 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1479 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1482 INIT_LIST_HEAD(&new_admin->entries);
1485 oper = rcu_dereference(q->oper_sched);
1486 admin = rcu_dereference(q->admin_sched);
1489 /* no changes - no new mqprio settings */
1490 if (!taprio_mqprio_cmp(dev, mqprio))
1493 if (mqprio && (oper || admin)) {
1494 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1499 err = parse_taprio_schedule(q, tb, new_admin, extack);
1503 if (new_admin->num_entries == 0) {
1504 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1509 err = taprio_parse_clockid(sch, tb, extack);
1513 taprio_set_picos_per_byte(dev, q);
1516 err = netdev_set_num_tc(dev, mqprio->num_tc);
1519 for (i = 0; i < mqprio->num_tc; i++)
1520 netdev_set_tc_queue(dev, i,
1524 /* Always use supplied priority mappings */
1525 for (i = 0; i <= TC_BITMASK; i++)
1526 netdev_set_prio_tc_map(dev, i,
1527 mqprio->prio_tc_map[i]);
1530 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1531 err = taprio_enable_offload(dev, q, new_admin, extack);
1533 err = taprio_disable_offload(dev, q, extack);
1537 /* Protects against enqueue()/dequeue() */
1538 spin_lock_bh(qdisc_lock(sch));
1540 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1541 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1542 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1547 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1550 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1551 !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1552 !hrtimer_active(&q->advance_timer)) {
1553 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1554 q->advance_timer.function = advance_sched;
1557 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1558 q->dequeue = taprio_dequeue_offload;
1559 q->peek = taprio_peek_offload;
1561 /* Be sure to always keep the function pointers
1562 * in a consistent state.
1564 q->dequeue = taprio_dequeue_soft;
1565 q->peek = taprio_peek_soft;
1568 err = taprio_get_start_time(sch, new_admin, &start);
1570 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1574 setup_txtime(q, new_admin, start);
1576 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1578 rcu_assign_pointer(q->oper_sched, new_admin);
1584 rcu_assign_pointer(q->admin_sched, new_admin);
1586 call_rcu(&admin->rcu, taprio_free_sched_cb);
1588 setup_first_close_time(q, new_admin, start);
1590 /* Protects against advance_sched() */
1591 spin_lock_irqsave(&q->current_entry_lock, flags);
1593 taprio_start_sched(sch, start, new_admin);
1595 rcu_assign_pointer(q->admin_sched, new_admin);
1597 call_rcu(&admin->rcu, taprio_free_sched_cb);
1599 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1601 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1602 taprio_offload_config_changed(q);
1609 spin_unlock_bh(qdisc_lock(sch));
1613 call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1618 static void taprio_reset(struct Qdisc *sch)
1620 struct taprio_sched *q = qdisc_priv(sch);
1621 struct net_device *dev = qdisc_dev(sch);
1624 hrtimer_cancel(&q->advance_timer);
1627 for (i = 0; i < dev->num_tx_queues && q->qdiscs[i]; i++)
1628 qdisc_reset(q->qdiscs[i]);
1630 sch->qstats.backlog = 0;
1634 static void taprio_destroy(struct Qdisc *sch)
1636 struct taprio_sched *q = qdisc_priv(sch);
1637 struct net_device *dev = qdisc_dev(sch);
1640 spin_lock(&taprio_list_lock);
1641 list_del(&q->taprio_list);
1642 spin_unlock(&taprio_list_lock);
1644 /* Note that taprio_reset() might not be called if an error
1645 * happens in qdisc_create(), after taprio_init() has been called.
1647 hrtimer_cancel(&q->advance_timer);
1648 qdisc_synchronize(sch);
1650 taprio_disable_offload(dev, q, NULL);
1653 for (i = 0; i < dev->num_tx_queues; i++)
1654 qdisc_put(q->qdiscs[i]);
1660 netdev_reset_tc(dev);
1663 call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb);
1666 call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb);
1669 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1670 struct netlink_ext_ack *extack)
1672 struct taprio_sched *q = qdisc_priv(sch);
1673 struct net_device *dev = qdisc_dev(sch);
1676 spin_lock_init(&q->current_entry_lock);
1678 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1679 q->advance_timer.function = advance_sched;
1681 q->dequeue = taprio_dequeue_soft;
1682 q->peek = taprio_peek_soft;
1686 /* We only support static clockids. Use an invalid value as default
1687 * and get the valid one on taprio_change().
1690 q->flags = TAPRIO_FLAGS_INVALID;
1692 spin_lock(&taprio_list_lock);
1693 list_add(&q->taprio_list, &taprio_list);
1694 spin_unlock(&taprio_list_lock);
1696 if (sch->parent != TC_H_ROOT)
1699 if (!netif_is_multiqueue(dev))
1702 /* pre-allocate qdisc, attachment can't fail */
1703 q->qdiscs = kcalloc(dev->num_tx_queues,
1704 sizeof(q->qdiscs[0]),
1713 for (i = 0; i < dev->num_tx_queues; i++) {
1714 struct netdev_queue *dev_queue;
1715 struct Qdisc *qdisc;
1717 dev_queue = netdev_get_tx_queue(dev, i);
1718 qdisc = qdisc_create_dflt(dev_queue,
1720 TC_H_MAKE(TC_H_MAJ(sch->handle),
1726 if (i < dev->real_num_tx_queues)
1727 qdisc_hash_add(qdisc, false);
1729 q->qdiscs[i] = qdisc;
1732 return taprio_change(sch, opt, extack);
1735 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1738 struct net_device *dev = qdisc_dev(sch);
1739 unsigned long ntx = cl - 1;
1741 if (ntx >= dev->num_tx_queues)
1744 return netdev_get_tx_queue(dev, ntx);
1747 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1748 struct Qdisc *new, struct Qdisc **old,
1749 struct netlink_ext_ack *extack)
1751 struct taprio_sched *q = qdisc_priv(sch);
1752 struct net_device *dev = qdisc_dev(sch);
1753 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1758 if (dev->flags & IFF_UP)
1759 dev_deactivate(dev);
1761 *old = q->qdiscs[cl - 1];
1762 q->qdiscs[cl - 1] = new;
1765 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1767 if (dev->flags & IFF_UP)
1773 static int dump_entry(struct sk_buff *msg,
1774 const struct sched_entry *entry)
1776 struct nlattr *item;
1778 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1782 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1783 goto nla_put_failure;
1785 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1786 goto nla_put_failure;
1788 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1790 goto nla_put_failure;
1792 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1794 goto nla_put_failure;
1796 return nla_nest_end(msg, item);
1799 nla_nest_cancel(msg, item);
1803 static int dump_schedule(struct sk_buff *msg,
1804 const struct sched_gate_list *root)
1806 struct nlattr *entry_list;
1807 struct sched_entry *entry;
1809 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1810 root->base_time, TCA_TAPRIO_PAD))
1813 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1814 root->cycle_time, TCA_TAPRIO_PAD))
1817 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1818 root->cycle_time_extension, TCA_TAPRIO_PAD))
1821 entry_list = nla_nest_start_noflag(msg,
1822 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1826 list_for_each_entry(entry, &root->entries, list) {
1827 if (dump_entry(msg, entry) < 0)
1831 nla_nest_end(msg, entry_list);
1835 nla_nest_cancel(msg, entry_list);
1839 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1841 struct taprio_sched *q = qdisc_priv(sch);
1842 struct net_device *dev = qdisc_dev(sch);
1843 struct sched_gate_list *oper, *admin;
1844 struct tc_mqprio_qopt opt = { 0 };
1845 struct nlattr *nest, *sched_nest;
1849 oper = rcu_dereference(q->oper_sched);
1850 admin = rcu_dereference(q->admin_sched);
1852 opt.num_tc = netdev_get_num_tc(dev);
1853 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
1855 for (i = 0; i < netdev_get_num_tc(dev); i++) {
1856 opt.count[i] = dev->tc_to_txq[i].count;
1857 opt.offset[i] = dev->tc_to_txq[i].offset;
1860 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1864 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
1867 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1868 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
1871 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
1874 if (q->txtime_delay &&
1875 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
1878 if (oper && dump_schedule(skb, oper))
1884 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
1888 if (dump_schedule(skb, admin))
1891 nla_nest_end(skb, sched_nest);
1896 return nla_nest_end(skb, nest);
1899 nla_nest_cancel(skb, sched_nest);
1902 nla_nest_cancel(skb, nest);
1909 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
1911 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1916 return dev_queue->qdisc_sleeping;
1919 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
1921 unsigned int ntx = TC_H_MIN(classid);
1923 if (!taprio_queue_get(sch, ntx))
1928 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
1929 struct sk_buff *skb, struct tcmsg *tcm)
1931 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1933 tcm->tcm_parent = TC_H_ROOT;
1934 tcm->tcm_handle |= TC_H_MIN(cl);
1935 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
1940 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
1941 struct gnet_dump *d)
1945 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1947 sch = dev_queue->qdisc_sleeping;
1948 if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 ||
1949 qdisc_qstats_copy(d, sch) < 0)
1954 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1956 struct net_device *dev = qdisc_dev(sch);
1962 arg->count = arg->skip;
1963 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
1964 if (arg->fn(sch, ntx + 1, arg) < 0) {
1972 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
1975 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
1978 static const struct Qdisc_class_ops taprio_class_ops = {
1979 .graft = taprio_graft,
1980 .leaf = taprio_leaf,
1981 .find = taprio_find,
1982 .walk = taprio_walk,
1983 .dump = taprio_dump_class,
1984 .dump_stats = taprio_dump_class_stats,
1985 .select_queue = taprio_select_queue,
1988 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
1989 .cl_ops = &taprio_class_ops,
1991 .priv_size = sizeof(struct taprio_sched),
1992 .init = taprio_init,
1993 .change = taprio_change,
1994 .destroy = taprio_destroy,
1995 .reset = taprio_reset,
1996 .peek = taprio_peek,
1997 .dequeue = taprio_dequeue,
1998 .enqueue = taprio_enqueue,
1999 .dump = taprio_dump,
2000 .owner = THIS_MODULE,
2003 static struct notifier_block taprio_device_notifier = {
2004 .notifier_call = taprio_dev_notifier,
2007 static int __init taprio_module_init(void)
2009 int err = register_netdevice_notifier(&taprio_device_notifier);
2014 return register_qdisc(&taprio_qdisc_ops);
2017 static void __exit taprio_module_exit(void)
2019 unregister_qdisc(&taprio_qdisc_ops);
2020 unregister_netdevice_notifier(&taprio_device_notifier);
2023 module_init(taprio_module_init);
2024 module_exit(taprio_module_exit);
2025 MODULE_LICENSE("GPL");