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 static LIST_HEAD(taprio_list);
30 static DEFINE_SPINLOCK(taprio_list_lock);
32 #define TAPRIO_ALL_GATES_OPEN -1
34 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
35 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
36 #define TAPRIO_FLAGS_INVALID U32_MAX
39 struct list_head list;
41 /* The instant that this entry "closes" and the next one
42 * should open, the qdisc will make some effort so that no
43 * packet leaves after this time.
54 struct sched_gate_list {
56 struct list_head entries;
58 ktime_t cycle_close_time;
60 s64 cycle_time_extension;
65 struct Qdisc **qdiscs;
68 enum tk_offsets tk_offset;
70 atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
71 * speeds it's sub-nanoseconds per byte
74 /* Protects the update side of the RCU protected current_entry */
75 spinlock_t current_entry_lock;
76 struct sched_entry __rcu *current_entry;
77 struct sched_gate_list __rcu *oper_sched;
78 struct sched_gate_list __rcu *admin_sched;
79 struct hrtimer advance_timer;
80 struct list_head taprio_list;
81 struct sk_buff *(*dequeue)(struct Qdisc *sch);
82 struct sk_buff *(*peek)(struct Qdisc *sch);
86 struct __tc_taprio_qopt_offload {
88 struct tc_taprio_qopt_offload offload;
91 static ktime_t sched_base_time(const struct sched_gate_list *sched)
96 return ns_to_ktime(sched->base_time);
99 static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono)
101 /* This pairs with WRITE_ONCE() in taprio_parse_clockid() */
102 enum tk_offsets tk_offset = READ_ONCE(q->tk_offset);
108 return ktime_mono_to_any(mono, tk_offset);
112 static ktime_t taprio_get_time(const struct taprio_sched *q)
114 return taprio_mono_to_any(q, ktime_get());
117 static void taprio_free_sched_cb(struct rcu_head *head)
119 struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
120 struct sched_entry *entry, *n;
122 list_for_each_entry_safe(entry, n, &sched->entries, list) {
123 list_del(&entry->list);
130 static void switch_schedules(struct taprio_sched *q,
131 struct sched_gate_list **admin,
132 struct sched_gate_list **oper)
134 rcu_assign_pointer(q->oper_sched, *admin);
135 rcu_assign_pointer(q->admin_sched, NULL);
138 call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
144 /* Get how much time has been already elapsed in the current cycle. */
145 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
147 ktime_t time_since_sched_start;
150 time_since_sched_start = ktime_sub(time, sched->base_time);
151 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
156 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
157 struct sched_gate_list *admin,
158 struct sched_entry *entry,
161 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
162 ktime_t intv_end, cycle_ext_end, cycle_end;
164 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
165 intv_end = ktime_add_ns(intv_start, entry->interval);
166 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
168 if (ktime_before(intv_end, cycle_end))
170 else if (admin && admin != sched &&
171 ktime_after(admin->base_time, cycle_end) &&
172 ktime_before(admin->base_time, cycle_ext_end))
173 return admin->base_time;
178 static int length_to_duration(struct taprio_sched *q, int len)
180 return div_u64(len * atomic64_read(&q->picos_per_byte), PSEC_PER_NSEC);
183 /* Returns the entry corresponding to next available interval. If
184 * validate_interval is set, it only validates whether the timestamp occurs
185 * when the gate corresponding to the skb's traffic class is open.
187 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
189 struct sched_gate_list *sched,
190 struct sched_gate_list *admin,
192 ktime_t *interval_start,
193 ktime_t *interval_end,
194 bool validate_interval)
196 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
197 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
198 struct sched_entry *entry = NULL, *entry_found = NULL;
199 struct taprio_sched *q = qdisc_priv(sch);
200 struct net_device *dev = qdisc_dev(sch);
201 bool entry_available = false;
205 tc = netdev_get_prio_tc_map(dev, skb->priority);
206 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
214 cycle = sched->cycle_time;
215 cycle_elapsed = get_cycle_time_elapsed(sched, time);
216 curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
217 cycle_end = ktime_add_ns(curr_intv_end, cycle);
219 list_for_each_entry(entry, &sched->entries, list) {
220 curr_intv_start = curr_intv_end;
221 curr_intv_end = get_interval_end_time(sched, admin, entry,
224 if (ktime_after(curr_intv_start, cycle_end))
227 if (!(entry->gate_mask & BIT(tc)) ||
228 packet_transmit_time > entry->interval)
231 txtime = entry->next_txtime;
233 if (ktime_before(txtime, time) || validate_interval) {
234 transmit_end_time = ktime_add_ns(time, packet_transmit_time);
235 if ((ktime_before(curr_intv_start, time) &&
236 ktime_before(transmit_end_time, curr_intv_end)) ||
237 (ktime_after(curr_intv_start, time) && !validate_interval)) {
239 *interval_start = curr_intv_start;
240 *interval_end = curr_intv_end;
242 } else if (!entry_available && !validate_interval) {
243 /* Here, we are just trying to find out the
244 * first available interval in the next cycle.
246 entry_available = true;
248 *interval_start = ktime_add_ns(curr_intv_start, cycle);
249 *interval_end = ktime_add_ns(curr_intv_end, cycle);
251 } else if (ktime_before(txtime, earliest_txtime) &&
253 earliest_txtime = txtime;
255 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
256 *interval_start = ktime_add(curr_intv_start, n * cycle);
257 *interval_end = ktime_add(curr_intv_end, n * cycle);
264 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
266 struct taprio_sched *q = qdisc_priv(sch);
267 struct sched_gate_list *sched, *admin;
268 ktime_t interval_start, interval_end;
269 struct sched_entry *entry;
272 sched = rcu_dereference(q->oper_sched);
273 admin = rcu_dereference(q->admin_sched);
275 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
276 &interval_start, &interval_end, true);
282 static bool taprio_flags_valid(u32 flags)
284 /* Make sure no other flag bits are set. */
285 if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST |
286 TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
288 /* txtime-assist and full offload are mutually exclusive */
289 if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
290 (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
295 /* This returns the tstamp value set by TCP in terms of the set clock. */
296 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
298 unsigned int offset = skb_network_offset(skb);
299 const struct ipv6hdr *ipv6h;
300 const struct iphdr *iph;
301 struct ipv6hdr _ipv6h;
303 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
307 if (ipv6h->version == 4) {
308 iph = (struct iphdr *)ipv6h;
309 offset += iph->ihl * 4;
311 /* special-case 6in4 tunnelling, as that is a common way to get
312 * v6 connectivity in the home
314 if (iph->protocol == IPPROTO_IPV6) {
315 ipv6h = skb_header_pointer(skb, offset,
316 sizeof(_ipv6h), &_ipv6h);
318 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
320 } else if (iph->protocol != IPPROTO_TCP) {
323 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
327 return taprio_mono_to_any(q, skb->skb_mstamp_ns);
330 /* There are a few scenarios where we will have to modify the txtime from
331 * what is read from next_txtime in sched_entry. They are:
332 * 1. If txtime is in the past,
333 * a. The gate for the traffic class is currently open and packet can be
334 * transmitted before it closes, schedule the packet right away.
335 * b. If the gate corresponding to the traffic class is going to open later
336 * in the cycle, set the txtime of packet to the interval start.
337 * 2. If txtime is in the future, there are packets corresponding to the
338 * current traffic class waiting to be transmitted. So, the following
339 * possibilities exist:
340 * a. We can transmit the packet before the window containing the txtime
342 * b. The window might close before the transmission can be completed
343 * successfully. So, schedule the packet in the next open window.
345 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
347 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
348 struct taprio_sched *q = qdisc_priv(sch);
349 struct sched_gate_list *sched, *admin;
350 ktime_t minimum_time, now, txtime;
351 int len, packet_transmit_time;
352 struct sched_entry *entry;
355 now = taprio_get_time(q);
356 minimum_time = ktime_add_ns(now, q->txtime_delay);
358 tcp_tstamp = get_tcp_tstamp(q, skb);
359 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
362 admin = rcu_dereference(q->admin_sched);
363 sched = rcu_dereference(q->oper_sched);
364 if (admin && ktime_after(minimum_time, admin->base_time))
365 switch_schedules(q, &admin, &sched);
367 /* Until the schedule starts, all the queues are open */
368 if (!sched || ktime_before(minimum_time, sched->base_time)) {
369 txtime = minimum_time;
373 len = qdisc_pkt_len(skb);
374 packet_transmit_time = length_to_duration(q, len);
377 sched_changed = false;
379 entry = find_entry_to_transmit(skb, sch, sched, admin,
381 &interval_start, &interval_end,
388 txtime = entry->next_txtime;
389 txtime = max_t(ktime_t, txtime, minimum_time);
390 txtime = max_t(ktime_t, txtime, interval_start);
392 if (admin && admin != sched &&
393 ktime_after(txtime, admin->base_time)) {
395 sched_changed = true;
399 transmit_end_time = ktime_add(txtime, packet_transmit_time);
400 minimum_time = transmit_end_time;
402 /* Update the txtime of current entry to the next time it's
405 if (ktime_after(transmit_end_time, interval_end))
406 entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
407 } while (sched_changed || ktime_after(transmit_end_time, interval_end));
409 entry->next_txtime = transmit_end_time;
416 static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
417 struct Qdisc *child, struct sk_buff **to_free)
419 struct taprio_sched *q = qdisc_priv(sch);
421 /* sk_flags are only safe to use on full sockets. */
422 if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) {
423 if (!is_valid_interval(skb, sch))
424 return qdisc_drop(skb, sch, to_free);
425 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
426 skb->tstamp = get_packet_txtime(skb, sch);
428 return qdisc_drop(skb, sch, to_free);
431 qdisc_qstats_backlog_inc(sch, skb);
434 return qdisc_enqueue(skb, child, to_free);
437 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
438 struct sk_buff **to_free)
440 struct taprio_sched *q = qdisc_priv(sch);
444 if (unlikely(FULL_OFFLOAD_IS_ENABLED(q->flags))) {
445 WARN_ONCE(1, "Trying to enqueue skb into the root of a taprio qdisc configured with full offload\n");
446 return qdisc_drop(skb, sch, to_free);
449 queue = skb_get_queue_mapping(skb);
451 child = q->qdiscs[queue];
452 if (unlikely(!child))
453 return qdisc_drop(skb, sch, to_free);
455 /* Large packets might not be transmitted when the transmission duration
456 * exceeds any configured interval. Therefore, segment the skb into
457 * smaller chunks. Skip it for the full offload case, as the driver
458 * and/or the hardware is expected to handle this.
460 if (skb_is_gso(skb) && !FULL_OFFLOAD_IS_ENABLED(q->flags)) {
461 unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb);
462 netdev_features_t features = netif_skb_features(skb);
463 struct sk_buff *segs, *nskb;
466 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
467 if (IS_ERR_OR_NULL(segs))
468 return qdisc_drop(skb, sch, to_free);
470 skb_list_walk_safe(segs, segs, nskb) {
471 skb_mark_not_on_list(segs);
472 qdisc_skb_cb(segs)->pkt_len = segs->len;
475 ret = taprio_enqueue_one(segs, sch, child, to_free);
476 if (ret != NET_XMIT_SUCCESS) {
477 if (net_xmit_drop_count(ret))
478 qdisc_qstats_drop(sch);
485 qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
488 return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
491 return taprio_enqueue_one(skb, sch, child, to_free);
494 static struct sk_buff *taprio_peek_soft(struct Qdisc *sch)
496 struct taprio_sched *q = qdisc_priv(sch);
497 struct net_device *dev = qdisc_dev(sch);
498 struct sched_entry *entry;
504 entry = rcu_dereference(q->current_entry);
505 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
511 for (i = 0; i < dev->num_tx_queues; i++) {
512 struct Qdisc *child = q->qdiscs[i];
516 if (unlikely(!child))
519 skb = child->ops->peek(child);
523 if (TXTIME_ASSIST_IS_ENABLED(q->flags))
526 prio = skb->priority;
527 tc = netdev_get_prio_tc_map(dev, prio);
529 if (!(gate_mask & BIT(tc)))
538 static struct sk_buff *taprio_peek_offload(struct Qdisc *sch)
540 WARN_ONCE(1, "Trying to peek into the root of a taprio qdisc configured with full offload\n");
545 static struct sk_buff *taprio_peek(struct Qdisc *sch)
547 struct taprio_sched *q = qdisc_priv(sch);
552 static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry)
554 atomic_set(&entry->budget,
555 div64_u64((u64)entry->interval * PSEC_PER_NSEC,
556 atomic64_read(&q->picos_per_byte)));
559 static struct sk_buff *taprio_dequeue_soft(struct Qdisc *sch)
561 struct taprio_sched *q = qdisc_priv(sch);
562 struct net_device *dev = qdisc_dev(sch);
563 struct sk_buff *skb = NULL;
564 struct sched_entry *entry;
569 entry = rcu_dereference(q->current_entry);
570 /* if there's no entry, it means that the schedule didn't
571 * start yet, so force all gates to be open, this is in
572 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
575 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
580 for (i = 0; i < dev->num_tx_queues; i++) {
581 struct Qdisc *child = q->qdiscs[i];
587 if (unlikely(!child))
590 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
591 skb = child->ops->dequeue(child);
597 skb = child->ops->peek(child);
601 prio = skb->priority;
602 tc = netdev_get_prio_tc_map(dev, prio);
604 if (!(gate_mask & BIT(tc))) {
609 len = qdisc_pkt_len(skb);
610 guard = ktime_add_ns(taprio_get_time(q),
611 length_to_duration(q, len));
613 /* In the case that there's no gate entry, there's no
616 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
617 ktime_after(guard, entry->close_time)) {
622 /* ... and no budget. */
623 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
624 atomic_sub_return(len, &entry->budget) < 0) {
629 skb = child->ops->dequeue(child);
634 qdisc_bstats_update(sch, skb);
635 qdisc_qstats_backlog_dec(sch, skb);
647 static struct sk_buff *taprio_dequeue_offload(struct Qdisc *sch)
649 WARN_ONCE(1, "Trying to dequeue from the root of a taprio qdisc configured with full offload\n");
654 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
656 struct taprio_sched *q = qdisc_priv(sch);
658 return q->dequeue(sch);
661 static bool should_restart_cycle(const struct sched_gate_list *oper,
662 const struct sched_entry *entry)
664 if (list_is_last(&entry->list, &oper->entries))
667 if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0)
673 static bool should_change_schedules(const struct sched_gate_list *admin,
674 const struct sched_gate_list *oper,
677 ktime_t next_base_time, extension_time;
682 next_base_time = sched_base_time(admin);
684 /* This is the simple case, the close_time would fall after
685 * the next schedule base_time.
687 if (ktime_compare(next_base_time, close_time) <= 0)
690 /* This is the cycle_time_extension case, if the close_time
691 * plus the amount that can be extended would fall after the
692 * next schedule base_time, we can extend the current schedule
695 extension_time = ktime_add_ns(close_time, oper->cycle_time_extension);
697 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
698 * how precisely the extension should be made. So after
699 * conformance testing, this logic may change.
701 if (ktime_compare(next_base_time, extension_time) <= 0)
707 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
709 struct taprio_sched *q = container_of(timer, struct taprio_sched,
711 struct sched_gate_list *oper, *admin;
712 struct sched_entry *entry, *next;
713 struct Qdisc *sch = q->root;
716 spin_lock(&q->current_entry_lock);
717 entry = rcu_dereference_protected(q->current_entry,
718 lockdep_is_held(&q->current_entry_lock));
719 oper = rcu_dereference_protected(q->oper_sched,
720 lockdep_is_held(&q->current_entry_lock));
721 admin = rcu_dereference_protected(q->admin_sched,
722 lockdep_is_held(&q->current_entry_lock));
725 switch_schedules(q, &admin, &oper);
727 /* This can happen in two cases: 1. this is the very first run
728 * of this function (i.e. we weren't running any schedule
729 * previously); 2. The previous schedule just ended. The first
730 * entry of all schedules are pre-calculated during the
731 * schedule initialization.
733 if (unlikely(!entry || entry->close_time == oper->base_time)) {
734 next = list_first_entry(&oper->entries, struct sched_entry,
736 close_time = next->close_time;
740 if (should_restart_cycle(oper, entry)) {
741 next = list_first_entry(&oper->entries, struct sched_entry,
743 oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time,
746 next = list_next_entry(entry, list);
749 close_time = ktime_add_ns(entry->close_time, next->interval);
750 close_time = min_t(ktime_t, close_time, oper->cycle_close_time);
752 if (should_change_schedules(admin, oper, close_time)) {
753 /* Set things so the next time this runs, the new
756 close_time = sched_base_time(admin);
757 switch_schedules(q, &admin, &oper);
760 next->close_time = close_time;
761 taprio_set_budget(q, next);
764 rcu_assign_pointer(q->current_entry, next);
765 spin_unlock(&q->current_entry_lock);
767 hrtimer_set_expires(&q->advance_timer, close_time);
770 __netif_schedule(sch);
773 return HRTIMER_RESTART;
776 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
777 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
778 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
779 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
780 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
783 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
784 [TCA_TAPRIO_ATTR_PRIOMAP] = {
785 .len = sizeof(struct tc_mqprio_qopt)
787 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
788 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
789 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
790 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
791 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 },
792 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
793 [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 },
794 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 },
797 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
798 struct sched_entry *entry,
799 struct netlink_ext_ack *extack)
801 int min_duration = length_to_duration(q, ETH_ZLEN);
804 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
805 entry->command = nla_get_u8(
806 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
808 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
809 entry->gate_mask = nla_get_u32(
810 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
812 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
813 interval = nla_get_u32(
814 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
816 /* The interval should allow at least the minimum ethernet
819 if (interval < min_duration) {
820 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
824 entry->interval = interval;
829 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
830 struct sched_entry *entry, int index,
831 struct netlink_ext_ack *extack)
833 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
836 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
839 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
843 entry->index = index;
845 return fill_sched_entry(q, tb, entry, extack);
848 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
849 struct sched_gate_list *sched,
850 struct netlink_ext_ack *extack)
859 nla_for_each_nested(n, list, rem) {
860 struct sched_entry *entry;
862 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
863 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
867 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
869 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
873 err = parse_sched_entry(q, n, entry, i, extack);
879 list_add_tail(&entry->list, &sched->entries);
883 sched->num_entries = i;
888 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
889 struct sched_gate_list *new,
890 struct netlink_ext_ack *extack)
894 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
895 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
899 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
900 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
902 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
903 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
905 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
906 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
908 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
909 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
914 if (!new->cycle_time) {
915 struct sched_entry *entry;
918 list_for_each_entry(entry, &new->entries, list)
919 cycle = ktime_add_ns(cycle, entry->interval);
922 NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
926 new->cycle_time = cycle;
932 static int taprio_parse_mqprio_opt(struct net_device *dev,
933 struct tc_mqprio_qopt *qopt,
934 struct netlink_ext_ack *extack,
939 if (!qopt && !dev->num_tc) {
940 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
944 /* If num_tc is already set, it means that the user already
945 * configured the mqprio part
950 /* Verify num_tc is not out of max range */
951 if (qopt->num_tc > TC_MAX_QUEUE) {
952 NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
956 /* taprio imposes that traffic classes map 1:n to tx queues */
957 if (qopt->num_tc > dev->num_tx_queues) {
958 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
962 /* Verify priority mapping uses valid tcs */
963 for (i = 0; i <= TC_BITMASK; i++) {
964 if (qopt->prio_tc_map[i] >= qopt->num_tc) {
965 NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
970 for (i = 0; i < qopt->num_tc; i++) {
971 unsigned int last = qopt->offset[i] + qopt->count[i];
973 /* Verify the queue count is in tx range being equal to the
974 * real_num_tx_queues indicates the last queue is in use.
976 if (qopt->offset[i] >= dev->num_tx_queues ||
978 last > dev->real_num_tx_queues) {
979 NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
983 if (TXTIME_ASSIST_IS_ENABLED(taprio_flags))
986 /* Verify that the offset and counts do not overlap */
987 for (j = i + 1; j < qopt->num_tc; j++) {
988 if (last > qopt->offset[j]) {
989 NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
998 static int taprio_get_start_time(struct Qdisc *sch,
999 struct sched_gate_list *sched,
1002 struct taprio_sched *q = qdisc_priv(sch);
1003 ktime_t now, base, cycle;
1006 base = sched_base_time(sched);
1007 now = taprio_get_time(q);
1009 if (ktime_after(base, now)) {
1014 cycle = sched->cycle_time;
1016 /* The qdisc is expected to have at least one sched_entry. Moreover,
1017 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
1018 * something went really wrong. In that case, we should warn about this
1019 * inconsistent state and return error.
1021 if (WARN_ON(!cycle))
1024 /* Schedule the start time for the beginning of the next
1027 n = div64_s64(ktime_sub_ns(now, base), cycle);
1028 *start = ktime_add_ns(base, (n + 1) * cycle);
1032 static void setup_first_close_time(struct taprio_sched *q,
1033 struct sched_gate_list *sched, ktime_t base)
1035 struct sched_entry *first;
1038 first = list_first_entry(&sched->entries,
1039 struct sched_entry, list);
1041 cycle = sched->cycle_time;
1043 /* FIXME: find a better place to do this */
1044 sched->cycle_close_time = ktime_add_ns(base, cycle);
1046 first->close_time = ktime_add_ns(base, first->interval);
1047 taprio_set_budget(q, first);
1048 rcu_assign_pointer(q->current_entry, NULL);
1051 static void taprio_start_sched(struct Qdisc *sch,
1052 ktime_t start, struct sched_gate_list *new)
1054 struct taprio_sched *q = qdisc_priv(sch);
1057 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1060 expires = hrtimer_get_expires(&q->advance_timer);
1062 expires = KTIME_MAX;
1064 /* If the new schedule starts before the next expiration, we
1065 * reprogram it to the earliest one, so we change the admin
1066 * schedule to the operational one at the right time.
1068 start = min_t(ktime_t, start, expires);
1070 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1073 static void taprio_set_picos_per_byte(struct net_device *dev,
1074 struct taprio_sched *q)
1076 struct ethtool_link_ksettings ecmd;
1077 int speed = SPEED_10;
1081 err = __ethtool_get_link_ksettings(dev, &ecmd);
1085 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1086 speed = ecmd.base.speed;
1089 picos_per_byte = (USEC_PER_SEC * 8) / speed;
1091 atomic64_set(&q->picos_per_byte, picos_per_byte);
1092 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1093 dev->name, (long long)atomic64_read(&q->picos_per_byte),
1097 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1100 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1101 struct net_device *qdev;
1102 struct taprio_sched *q;
1107 if (event != NETDEV_UP && event != NETDEV_CHANGE)
1110 spin_lock(&taprio_list_lock);
1111 list_for_each_entry(q, &taprio_list, taprio_list) {
1112 qdev = qdisc_dev(q->root);
1118 spin_unlock(&taprio_list_lock);
1121 taprio_set_picos_per_byte(dev, q);
1126 static void setup_txtime(struct taprio_sched *q,
1127 struct sched_gate_list *sched, ktime_t base)
1129 struct sched_entry *entry;
1132 list_for_each_entry(entry, &sched->entries, list) {
1133 entry->next_txtime = ktime_add_ns(base, interval);
1134 interval += entry->interval;
1138 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1140 struct __tc_taprio_qopt_offload *__offload;
1142 __offload = kzalloc(struct_size(__offload, offload.entries, num_entries),
1147 refcount_set(&__offload->users, 1);
1149 return &__offload->offload;
1152 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1155 struct __tc_taprio_qopt_offload *__offload;
1157 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1160 refcount_inc(&__offload->users);
1164 EXPORT_SYMBOL_GPL(taprio_offload_get);
1166 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1168 struct __tc_taprio_qopt_offload *__offload;
1170 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1173 if (!refcount_dec_and_test(&__offload->users))
1178 EXPORT_SYMBOL_GPL(taprio_offload_free);
1180 /* The function will only serve to keep the pointers to the "oper" and "admin"
1181 * schedules valid in relation to their base times, so when calling dump() the
1182 * users looks at the right schedules.
1183 * When using full offload, the admin configuration is promoted to oper at the
1184 * base_time in the PHC time domain. But because the system time is not
1185 * necessarily in sync with that, we can't just trigger a hrtimer to call
1186 * switch_schedules at the right hardware time.
1187 * At the moment we call this by hand right away from taprio, but in the future
1188 * it will be useful to create a mechanism for drivers to notify taprio of the
1189 * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1190 * This is left as TODO.
1192 static void taprio_offload_config_changed(struct taprio_sched *q)
1194 struct sched_gate_list *oper, *admin;
1196 spin_lock(&q->current_entry_lock);
1198 oper = rcu_dereference_protected(q->oper_sched,
1199 lockdep_is_held(&q->current_entry_lock));
1200 admin = rcu_dereference_protected(q->admin_sched,
1201 lockdep_is_held(&q->current_entry_lock));
1203 switch_schedules(q, &admin, &oper);
1205 spin_unlock(&q->current_entry_lock);
1208 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1210 u32 i, queue_mask = 0;
1212 for (i = 0; i < dev->num_tc; i++) {
1215 if (!(tc_mask & BIT(i)))
1218 offset = dev->tc_to_txq[i].offset;
1219 count = dev->tc_to_txq[i].count;
1221 queue_mask |= GENMASK(offset + count - 1, offset);
1227 static void taprio_sched_to_offload(struct net_device *dev,
1228 struct sched_gate_list *sched,
1229 struct tc_taprio_qopt_offload *offload)
1231 struct sched_entry *entry;
1234 offload->base_time = sched->base_time;
1235 offload->cycle_time = sched->cycle_time;
1236 offload->cycle_time_extension = sched->cycle_time_extension;
1238 list_for_each_entry(entry, &sched->entries, list) {
1239 struct tc_taprio_sched_entry *e = &offload->entries[i];
1241 e->command = entry->command;
1242 e->interval = entry->interval;
1243 e->gate_mask = tc_map_to_queue_mask(dev, entry->gate_mask);
1248 offload->num_entries = i;
1251 static int taprio_enable_offload(struct net_device *dev,
1252 struct taprio_sched *q,
1253 struct sched_gate_list *sched,
1254 struct netlink_ext_ack *extack)
1256 const struct net_device_ops *ops = dev->netdev_ops;
1257 struct tc_taprio_qopt_offload *offload;
1260 if (!ops->ndo_setup_tc) {
1261 NL_SET_ERR_MSG(extack,
1262 "Device does not support taprio offload");
1266 offload = taprio_offload_alloc(sched->num_entries);
1268 NL_SET_ERR_MSG(extack,
1269 "Not enough memory for enabling offload mode");
1272 offload->enable = 1;
1273 taprio_sched_to_offload(dev, sched, offload);
1275 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1277 NL_SET_ERR_MSG(extack,
1278 "Device failed to setup taprio offload");
1283 taprio_offload_free(offload);
1288 static int taprio_disable_offload(struct net_device *dev,
1289 struct taprio_sched *q,
1290 struct netlink_ext_ack *extack)
1292 const struct net_device_ops *ops = dev->netdev_ops;
1293 struct tc_taprio_qopt_offload *offload;
1296 if (!FULL_OFFLOAD_IS_ENABLED(q->flags))
1299 if (!ops->ndo_setup_tc)
1302 offload = taprio_offload_alloc(0);
1304 NL_SET_ERR_MSG(extack,
1305 "Not enough memory to disable offload mode");
1308 offload->enable = 0;
1310 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1312 NL_SET_ERR_MSG(extack,
1313 "Device failed to disable offload");
1318 taprio_offload_free(offload);
1323 /* If full offload is enabled, the only possible clockid is the net device's
1324 * PHC. For that reason, specifying a clockid through netlink is incorrect.
1325 * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1326 * in sync with the specified clockid via a user space daemon such as phc2sys.
1327 * For both software taprio and txtime-assist, the clockid is used for the
1328 * hrtimer that advances the schedule and hence mandatory.
1330 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1331 struct netlink_ext_ack *extack)
1333 struct taprio_sched *q = qdisc_priv(sch);
1334 struct net_device *dev = qdisc_dev(sch);
1337 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1338 const struct ethtool_ops *ops = dev->ethtool_ops;
1339 struct ethtool_ts_info info = {
1340 .cmd = ETHTOOL_GET_TS_INFO,
1344 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1345 NL_SET_ERR_MSG(extack,
1346 "The 'clockid' cannot be specified for full offload");
1350 if (ops && ops->get_ts_info)
1351 err = ops->get_ts_info(dev, &info);
1353 if (err || info.phc_index < 0) {
1354 NL_SET_ERR_MSG(extack,
1355 "Device does not have a PTP clock");
1359 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1360 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1361 enum tk_offsets tk_offset;
1363 /* We only support static clockids and we don't allow
1364 * for it to be modified after the first init.
1367 (q->clockid != -1 && q->clockid != clockid)) {
1368 NL_SET_ERR_MSG(extack,
1369 "Changing the 'clockid' of a running schedule is not supported");
1375 case CLOCK_REALTIME:
1376 tk_offset = TK_OFFS_REAL;
1378 case CLOCK_MONOTONIC:
1379 tk_offset = TK_OFFS_MAX;
1381 case CLOCK_BOOTTIME:
1382 tk_offset = TK_OFFS_BOOT;
1385 tk_offset = TK_OFFS_TAI;
1388 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1392 /* This pairs with READ_ONCE() in taprio_mono_to_any */
1393 WRITE_ONCE(q->tk_offset, tk_offset);
1395 q->clockid = clockid;
1397 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1401 /* Everything went ok, return success. */
1408 static int taprio_mqprio_cmp(const struct net_device *dev,
1409 const struct tc_mqprio_qopt *mqprio)
1413 if (!mqprio || mqprio->num_tc != dev->num_tc)
1416 for (i = 0; i < mqprio->num_tc; i++)
1417 if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1418 dev->tc_to_txq[i].offset != mqprio->offset[i])
1421 for (i = 0; i <= TC_BITMASK; i++)
1422 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1428 /* The semantics of the 'flags' argument in relation to 'change()'
1429 * requests, are interpreted following two rules (which are applied in
1430 * this order): (1) an omitted 'flags' argument is interpreted as
1431 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1434 static int taprio_new_flags(const struct nlattr *attr, u32 old,
1435 struct netlink_ext_ack *extack)
1440 new = nla_get_u32(attr);
1442 if (old != TAPRIO_FLAGS_INVALID && old != new) {
1443 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1447 if (!taprio_flags_valid(new)) {
1448 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1455 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1456 struct netlink_ext_ack *extack)
1458 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1459 struct sched_gate_list *oper, *admin, *new_admin;
1460 struct taprio_sched *q = qdisc_priv(sch);
1461 struct net_device *dev = qdisc_dev(sch);
1462 struct tc_mqprio_qopt *mqprio = NULL;
1463 unsigned long flags;
1467 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1468 taprio_policy, extack);
1472 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1473 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1475 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1482 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1486 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1488 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1491 INIT_LIST_HEAD(&new_admin->entries);
1494 oper = rcu_dereference(q->oper_sched);
1495 admin = rcu_dereference(q->admin_sched);
1498 /* no changes - no new mqprio settings */
1499 if (!taprio_mqprio_cmp(dev, mqprio))
1502 if (mqprio && (oper || admin)) {
1503 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1508 err = parse_taprio_schedule(q, tb, new_admin, extack);
1512 if (new_admin->num_entries == 0) {
1513 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1518 err = taprio_parse_clockid(sch, tb, extack);
1522 taprio_set_picos_per_byte(dev, q);
1525 err = netdev_set_num_tc(dev, mqprio->num_tc);
1528 for (i = 0; i < mqprio->num_tc; i++)
1529 netdev_set_tc_queue(dev, i,
1533 /* Always use supplied priority mappings */
1534 for (i = 0; i <= TC_BITMASK; i++)
1535 netdev_set_prio_tc_map(dev, i,
1536 mqprio->prio_tc_map[i]);
1539 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1540 err = taprio_enable_offload(dev, q, new_admin, extack);
1542 err = taprio_disable_offload(dev, q, extack);
1546 /* Protects against enqueue()/dequeue() */
1547 spin_lock_bh(qdisc_lock(sch));
1549 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1550 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1551 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1556 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1559 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1560 !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1561 !hrtimer_active(&q->advance_timer)) {
1562 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1563 q->advance_timer.function = advance_sched;
1566 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1567 q->dequeue = taprio_dequeue_offload;
1568 q->peek = taprio_peek_offload;
1570 /* Be sure to always keep the function pointers
1571 * in a consistent state.
1573 q->dequeue = taprio_dequeue_soft;
1574 q->peek = taprio_peek_soft;
1577 err = taprio_get_start_time(sch, new_admin, &start);
1579 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1583 setup_txtime(q, new_admin, start);
1585 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1587 rcu_assign_pointer(q->oper_sched, new_admin);
1593 rcu_assign_pointer(q->admin_sched, new_admin);
1595 call_rcu(&admin->rcu, taprio_free_sched_cb);
1597 setup_first_close_time(q, new_admin, start);
1599 /* Protects against advance_sched() */
1600 spin_lock_irqsave(&q->current_entry_lock, flags);
1602 taprio_start_sched(sch, start, new_admin);
1604 rcu_assign_pointer(q->admin_sched, new_admin);
1606 call_rcu(&admin->rcu, taprio_free_sched_cb);
1608 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1610 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1611 taprio_offload_config_changed(q);
1618 spin_unlock_bh(qdisc_lock(sch));
1622 call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1627 static void taprio_reset(struct Qdisc *sch)
1629 struct taprio_sched *q = qdisc_priv(sch);
1630 struct net_device *dev = qdisc_dev(sch);
1633 hrtimer_cancel(&q->advance_timer);
1635 for (i = 0; i < dev->num_tx_queues; i++)
1637 qdisc_reset(q->qdiscs[i]);
1639 sch->qstats.backlog = 0;
1643 static void taprio_destroy(struct Qdisc *sch)
1645 struct taprio_sched *q = qdisc_priv(sch);
1646 struct net_device *dev = qdisc_dev(sch);
1649 spin_lock(&taprio_list_lock);
1650 list_del(&q->taprio_list);
1651 spin_unlock(&taprio_list_lock);
1653 /* Note that taprio_reset() might not be called if an error
1654 * happens in qdisc_create(), after taprio_init() has been called.
1656 hrtimer_cancel(&q->advance_timer);
1658 taprio_disable_offload(dev, q, NULL);
1661 for (i = 0; i < dev->num_tx_queues; i++)
1662 qdisc_put(q->qdiscs[i]);
1668 netdev_reset_tc(dev);
1671 call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb);
1674 call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb);
1677 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1678 struct netlink_ext_ack *extack)
1680 struct taprio_sched *q = qdisc_priv(sch);
1681 struct net_device *dev = qdisc_dev(sch);
1684 spin_lock_init(&q->current_entry_lock);
1686 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1687 q->advance_timer.function = advance_sched;
1689 q->dequeue = taprio_dequeue_soft;
1690 q->peek = taprio_peek_soft;
1694 /* We only support static clockids. Use an invalid value as default
1695 * and get the valid one on taprio_change().
1698 q->flags = TAPRIO_FLAGS_INVALID;
1700 spin_lock(&taprio_list_lock);
1701 list_add(&q->taprio_list, &taprio_list);
1702 spin_unlock(&taprio_list_lock);
1704 if (sch->parent != TC_H_ROOT)
1707 if (!netif_is_multiqueue(dev))
1710 /* pre-allocate qdisc, attachment can't fail */
1711 q->qdiscs = kcalloc(dev->num_tx_queues,
1712 sizeof(q->qdiscs[0]),
1721 for (i = 0; i < dev->num_tx_queues; i++) {
1722 struct netdev_queue *dev_queue;
1723 struct Qdisc *qdisc;
1725 dev_queue = netdev_get_tx_queue(dev, i);
1726 qdisc = qdisc_create_dflt(dev_queue,
1728 TC_H_MAKE(TC_H_MAJ(sch->handle),
1734 if (i < dev->real_num_tx_queues)
1735 qdisc_hash_add(qdisc, false);
1737 q->qdiscs[i] = qdisc;
1740 return taprio_change(sch, opt, extack);
1743 static void taprio_attach(struct Qdisc *sch)
1745 struct taprio_sched *q = qdisc_priv(sch);
1746 struct net_device *dev = qdisc_dev(sch);
1749 /* Attach underlying qdisc */
1750 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
1751 struct Qdisc *qdisc = q->qdiscs[ntx];
1754 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1755 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1756 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
1758 old = dev_graft_qdisc(qdisc->dev_queue, sch);
1759 qdisc_refcount_inc(sch);
1765 /* access to the child qdiscs is not needed in offload mode */
1766 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1772 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1775 struct net_device *dev = qdisc_dev(sch);
1776 unsigned long ntx = cl - 1;
1778 if (ntx >= dev->num_tx_queues)
1781 return netdev_get_tx_queue(dev, ntx);
1784 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1785 struct Qdisc *new, struct Qdisc **old,
1786 struct netlink_ext_ack *extack)
1788 struct taprio_sched *q = qdisc_priv(sch);
1789 struct net_device *dev = qdisc_dev(sch);
1790 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1795 if (dev->flags & IFF_UP)
1796 dev_deactivate(dev);
1798 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1799 *old = dev_graft_qdisc(dev_queue, new);
1801 *old = q->qdiscs[cl - 1];
1802 q->qdiscs[cl - 1] = new;
1806 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1808 if (dev->flags & IFF_UP)
1814 static int dump_entry(struct sk_buff *msg,
1815 const struct sched_entry *entry)
1817 struct nlattr *item;
1819 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1823 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1824 goto nla_put_failure;
1826 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1827 goto nla_put_failure;
1829 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1831 goto nla_put_failure;
1833 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1835 goto nla_put_failure;
1837 return nla_nest_end(msg, item);
1840 nla_nest_cancel(msg, item);
1844 static int dump_schedule(struct sk_buff *msg,
1845 const struct sched_gate_list *root)
1847 struct nlattr *entry_list;
1848 struct sched_entry *entry;
1850 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1851 root->base_time, TCA_TAPRIO_PAD))
1854 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1855 root->cycle_time, TCA_TAPRIO_PAD))
1858 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1859 root->cycle_time_extension, TCA_TAPRIO_PAD))
1862 entry_list = nla_nest_start_noflag(msg,
1863 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1867 list_for_each_entry(entry, &root->entries, list) {
1868 if (dump_entry(msg, entry) < 0)
1872 nla_nest_end(msg, entry_list);
1876 nla_nest_cancel(msg, entry_list);
1880 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1882 struct taprio_sched *q = qdisc_priv(sch);
1883 struct net_device *dev = qdisc_dev(sch);
1884 struct sched_gate_list *oper, *admin;
1885 struct tc_mqprio_qopt opt = { 0 };
1886 struct nlattr *nest, *sched_nest;
1890 oper = rcu_dereference(q->oper_sched);
1891 admin = rcu_dereference(q->admin_sched);
1893 opt.num_tc = netdev_get_num_tc(dev);
1894 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
1896 for (i = 0; i < netdev_get_num_tc(dev); i++) {
1897 opt.count[i] = dev->tc_to_txq[i].count;
1898 opt.offset[i] = dev->tc_to_txq[i].offset;
1901 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1905 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
1908 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1909 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
1912 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
1915 if (q->txtime_delay &&
1916 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
1919 if (oper && dump_schedule(skb, oper))
1925 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
1929 if (dump_schedule(skb, admin))
1932 nla_nest_end(skb, sched_nest);
1937 return nla_nest_end(skb, nest);
1940 nla_nest_cancel(skb, sched_nest);
1943 nla_nest_cancel(skb, nest);
1950 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
1952 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1957 return dev_queue->qdisc_sleeping;
1960 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
1962 unsigned int ntx = TC_H_MIN(classid);
1964 if (!taprio_queue_get(sch, ntx))
1969 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
1970 struct sk_buff *skb, struct tcmsg *tcm)
1972 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1974 tcm->tcm_parent = TC_H_ROOT;
1975 tcm->tcm_handle |= TC_H_MIN(cl);
1976 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
1981 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
1982 struct gnet_dump *d)
1986 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1988 sch = dev_queue->qdisc_sleeping;
1989 if (gnet_stats_copy_basic(d, NULL, &sch->bstats, true) < 0 ||
1990 qdisc_qstats_copy(d, sch) < 0)
1995 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1997 struct net_device *dev = qdisc_dev(sch);
2003 arg->count = arg->skip;
2004 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
2005 if (arg->fn(sch, ntx + 1, arg) < 0) {
2013 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
2016 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
2019 static const struct Qdisc_class_ops taprio_class_ops = {
2020 .graft = taprio_graft,
2021 .leaf = taprio_leaf,
2022 .find = taprio_find,
2023 .walk = taprio_walk,
2024 .dump = taprio_dump_class,
2025 .dump_stats = taprio_dump_class_stats,
2026 .select_queue = taprio_select_queue,
2029 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
2030 .cl_ops = &taprio_class_ops,
2032 .priv_size = sizeof(struct taprio_sched),
2033 .init = taprio_init,
2034 .change = taprio_change,
2035 .destroy = taprio_destroy,
2036 .reset = taprio_reset,
2037 .attach = taprio_attach,
2038 .peek = taprio_peek,
2039 .dequeue = taprio_dequeue,
2040 .enqueue = taprio_enqueue,
2041 .dump = taprio_dump,
2042 .owner = THIS_MODULE,
2045 static struct notifier_block taprio_device_notifier = {
2046 .notifier_call = taprio_dev_notifier,
2049 static int __init taprio_module_init(void)
2051 int err = register_netdevice_notifier(&taprio_device_notifier);
2056 return register_qdisc(&taprio_qdisc_ops);
2059 static void __exit taprio_module_exit(void)
2061 unregister_qdisc(&taprio_qdisc_ops);
2062 unregister_netdevice_notifier(&taprio_device_notifier);
2065 module_init(taprio_module_init);
2066 module_exit(taprio_module_exit);
2067 MODULE_LICENSE("GPL");