1 // SPDX-License-Identifier: GPL-2.0
3 /* net/sched/sch_taprio.c Time Aware Priority Scheduler
5 * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com>
9 #include <linux/ethtool.h>
10 #include <linux/types.h>
11 #include <linux/slab.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/list.h>
15 #include <linux/errno.h>
16 #include <linux/skbuff.h>
17 #include <linux/math64.h>
18 #include <linux/module.h>
19 #include <linux/spinlock.h>
20 #include <linux/rcupdate.h>
21 #include <net/netlink.h>
22 #include <net/pkt_sched.h>
23 #include <net/pkt_cls.h>
24 #include <net/sch_generic.h>
28 static LIST_HEAD(taprio_list);
29 static DEFINE_SPINLOCK(taprio_list_lock);
31 #define TAPRIO_ALL_GATES_OPEN -1
33 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
34 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
35 #define TAPRIO_FLAGS_INVALID U32_MAX
38 struct list_head list;
40 /* The instant that this entry "closes" and the next one
41 * should open, the qdisc will make some effort so that no
42 * packet leaves after this time.
53 struct sched_gate_list {
55 struct list_head entries;
57 ktime_t cycle_close_time;
59 s64 cycle_time_extension;
64 struct Qdisc **qdiscs;
67 enum tk_offsets tk_offset;
69 atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
70 * speeds it's sub-nanoseconds per byte
73 /* Protects the update side of the RCU protected current_entry */
74 spinlock_t current_entry_lock;
75 struct sched_entry __rcu *current_entry;
76 struct sched_gate_list __rcu *oper_sched;
77 struct sched_gate_list __rcu *admin_sched;
78 struct hrtimer advance_timer;
79 struct list_head taprio_list;
80 struct sk_buff *(*dequeue)(struct Qdisc *sch);
81 struct sk_buff *(*peek)(struct Qdisc *sch);
85 struct __tc_taprio_qopt_offload {
87 struct tc_taprio_qopt_offload offload;
90 static ktime_t sched_base_time(const struct sched_gate_list *sched)
95 return ns_to_ktime(sched->base_time);
98 static ktime_t taprio_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;
121 list_for_each_entry_safe(entry, n, &sched->entries, list) {
122 list_del(&entry->list);
129 static void switch_schedules(struct taprio_sched *q,
130 struct sched_gate_list **admin,
131 struct sched_gate_list **oper)
133 rcu_assign_pointer(q->oper_sched, *admin);
134 rcu_assign_pointer(q->admin_sched, NULL);
137 call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
143 /* Get how much time has been already elapsed in the current cycle. */
144 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
146 ktime_t time_since_sched_start;
149 time_since_sched_start = ktime_sub(time, sched->base_time);
150 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
155 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
156 struct sched_gate_list *admin,
157 struct sched_entry *entry,
160 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
161 ktime_t intv_end, cycle_ext_end, cycle_end;
163 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
164 intv_end = ktime_add_ns(intv_start, entry->interval);
165 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
167 if (ktime_before(intv_end, cycle_end))
169 else if (admin && admin != sched &&
170 ktime_after(admin->base_time, cycle_end) &&
171 ktime_before(admin->base_time, cycle_ext_end))
172 return admin->base_time;
177 static int length_to_duration(struct taprio_sched *q, int len)
179 return div_u64(len * atomic64_read(&q->picos_per_byte), 1000);
182 /* Returns the entry corresponding to next available interval. If
183 * validate_interval is set, it only validates whether the timestamp occurs
184 * when the gate corresponding to the skb's traffic class is open.
186 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
188 struct sched_gate_list *sched,
189 struct sched_gate_list *admin,
191 ktime_t *interval_start,
192 ktime_t *interval_end,
193 bool validate_interval)
195 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
196 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
197 struct sched_entry *entry = NULL, *entry_found = NULL;
198 struct taprio_sched *q = qdisc_priv(sch);
199 struct net_device *dev = qdisc_dev(sch);
200 bool entry_available = false;
204 tc = netdev_get_prio_tc_map(dev, skb->priority);
205 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
213 cycle = sched->cycle_time;
214 cycle_elapsed = get_cycle_time_elapsed(sched, time);
215 curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
216 cycle_end = ktime_add_ns(curr_intv_end, cycle);
218 list_for_each_entry(entry, &sched->entries, list) {
219 curr_intv_start = curr_intv_end;
220 curr_intv_end = get_interval_end_time(sched, admin, entry,
223 if (ktime_after(curr_intv_start, cycle_end))
226 if (!(entry->gate_mask & BIT(tc)) ||
227 packet_transmit_time > entry->interval)
230 txtime = entry->next_txtime;
232 if (ktime_before(txtime, time) || validate_interval) {
233 transmit_end_time = ktime_add_ns(time, packet_transmit_time);
234 if ((ktime_before(curr_intv_start, time) &&
235 ktime_before(transmit_end_time, curr_intv_end)) ||
236 (ktime_after(curr_intv_start, time) && !validate_interval)) {
238 *interval_start = curr_intv_start;
239 *interval_end = curr_intv_end;
241 } else if (!entry_available && !validate_interval) {
242 /* Here, we are just trying to find out the
243 * first available interval in the next cycle.
245 entry_available = true;
247 *interval_start = ktime_add_ns(curr_intv_start, cycle);
248 *interval_end = ktime_add_ns(curr_intv_end, cycle);
250 } else if (ktime_before(txtime, earliest_txtime) &&
252 earliest_txtime = txtime;
254 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
255 *interval_start = ktime_add(curr_intv_start, n * cycle);
256 *interval_end = ktime_add(curr_intv_end, n * cycle);
263 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
265 struct taprio_sched *q = qdisc_priv(sch);
266 struct sched_gate_list *sched, *admin;
267 ktime_t interval_start, interval_end;
268 struct sched_entry *entry;
271 sched = rcu_dereference(q->oper_sched);
272 admin = rcu_dereference(q->admin_sched);
274 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
275 &interval_start, &interval_end, true);
281 static bool taprio_flags_valid(u32 flags)
283 /* Make sure no other flag bits are set. */
284 if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST |
285 TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
287 /* txtime-assist and full offload are mutually exclusive */
288 if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
289 (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
294 /* This returns the tstamp value set by TCP in terms of the set clock. */
295 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
297 unsigned int offset = skb_network_offset(skb);
298 const struct ipv6hdr *ipv6h;
299 const struct iphdr *iph;
300 struct ipv6hdr _ipv6h;
302 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
306 if (ipv6h->version == 4) {
307 iph = (struct iphdr *)ipv6h;
308 offset += iph->ihl * 4;
310 /* special-case 6in4 tunnelling, as that is a common way to get
311 * v6 connectivity in the home
313 if (iph->protocol == IPPROTO_IPV6) {
314 ipv6h = skb_header_pointer(skb, offset,
315 sizeof(_ipv6h), &_ipv6h);
317 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
319 } else if (iph->protocol != IPPROTO_TCP) {
322 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
326 return taprio_mono_to_any(q, skb->skb_mstamp_ns);
329 /* There are a few scenarios where we will have to modify the txtime from
330 * what is read from next_txtime in sched_entry. They are:
331 * 1. If txtime is in the past,
332 * a. The gate for the traffic class is currently open and packet can be
333 * transmitted before it closes, schedule the packet right away.
334 * b. If the gate corresponding to the traffic class is going to open later
335 * in the cycle, set the txtime of packet to the interval start.
336 * 2. If txtime is in the future, there are packets corresponding to the
337 * current traffic class waiting to be transmitted. So, the following
338 * possibilities exist:
339 * a. We can transmit the packet before the window containing the txtime
341 * b. The window might close before the transmission can be completed
342 * successfully. So, schedule the packet in the next open window.
344 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
346 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
347 struct taprio_sched *q = qdisc_priv(sch);
348 struct sched_gate_list *sched, *admin;
349 ktime_t minimum_time, now, txtime;
350 int len, packet_transmit_time;
351 struct sched_entry *entry;
354 now = taprio_get_time(q);
355 minimum_time = ktime_add_ns(now, q->txtime_delay);
357 tcp_tstamp = get_tcp_tstamp(q, skb);
358 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
361 admin = rcu_dereference(q->admin_sched);
362 sched = rcu_dereference(q->oper_sched);
363 if (admin && ktime_after(minimum_time, admin->base_time))
364 switch_schedules(q, &admin, &sched);
366 /* Until the schedule starts, all the queues are open */
367 if (!sched || ktime_before(minimum_time, sched->base_time)) {
368 txtime = minimum_time;
372 len = qdisc_pkt_len(skb);
373 packet_transmit_time = length_to_duration(q, len);
376 sched_changed = false;
378 entry = find_entry_to_transmit(skb, sch, sched, admin,
380 &interval_start, &interval_end,
387 txtime = entry->next_txtime;
388 txtime = max_t(ktime_t, txtime, minimum_time);
389 txtime = max_t(ktime_t, txtime, interval_start);
391 if (admin && admin != sched &&
392 ktime_after(txtime, admin->base_time)) {
394 sched_changed = true;
398 transmit_end_time = ktime_add(txtime, packet_transmit_time);
399 minimum_time = transmit_end_time;
401 /* Update the txtime of current entry to the next time it's
404 if (ktime_after(transmit_end_time, interval_end))
405 entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
406 } while (sched_changed || ktime_after(transmit_end_time, interval_end));
408 entry->next_txtime = transmit_end_time;
415 static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
416 struct Qdisc *child, struct sk_buff **to_free)
418 struct taprio_sched *q = qdisc_priv(sch);
420 /* sk_flags are only safe to use on full sockets. */
421 if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) {
422 if (!is_valid_interval(skb, sch))
423 return qdisc_drop(skb, sch, to_free);
424 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
425 skb->tstamp = get_packet_txtime(skb, sch);
427 return qdisc_drop(skb, sch, to_free);
430 qdisc_qstats_backlog_inc(sch, skb);
433 return qdisc_enqueue(skb, child, to_free);
436 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
437 struct sk_buff **to_free)
439 struct taprio_sched *q = qdisc_priv(sch);
443 if (unlikely(FULL_OFFLOAD_IS_ENABLED(q->flags))) {
444 WARN_ONCE(1, "Trying to enqueue skb into the root of a taprio qdisc configured with full offload\n");
445 return qdisc_drop(skb, sch, to_free);
448 queue = skb_get_queue_mapping(skb);
450 child = q->qdiscs[queue];
451 if (unlikely(!child))
452 return qdisc_drop(skb, sch, to_free);
454 /* Large packets might not be transmitted when the transmission duration
455 * exceeds any configured interval. Therefore, segment the skb into
456 * smaller chunks. Skip it for the full offload case, as the driver
457 * and/or the hardware is expected to handle this.
459 if (skb_is_gso(skb) && !FULL_OFFLOAD_IS_ENABLED(q->flags)) {
460 unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb);
461 netdev_features_t features = netif_skb_features(skb);
462 struct sk_buff *segs, *nskb;
465 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
466 if (IS_ERR_OR_NULL(segs))
467 return qdisc_drop(skb, sch, to_free);
469 skb_list_walk_safe(segs, segs, nskb) {
470 skb_mark_not_on_list(segs);
471 qdisc_skb_cb(segs)->pkt_len = segs->len;
474 ret = taprio_enqueue_one(segs, sch, child, to_free);
475 if (ret != NET_XMIT_SUCCESS) {
476 if (net_xmit_drop_count(ret))
477 qdisc_qstats_drop(sch);
484 qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
487 return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
490 return taprio_enqueue_one(skb, sch, child, to_free);
493 static struct sk_buff *taprio_peek_soft(struct Qdisc *sch)
495 struct taprio_sched *q = qdisc_priv(sch);
496 struct net_device *dev = qdisc_dev(sch);
497 struct sched_entry *entry;
503 entry = rcu_dereference(q->current_entry);
504 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
510 for (i = 0; i < dev->num_tx_queues; i++) {
511 struct Qdisc *child = q->qdiscs[i];
515 if (unlikely(!child))
518 skb = child->ops->peek(child);
522 if (TXTIME_ASSIST_IS_ENABLED(q->flags))
525 prio = skb->priority;
526 tc = netdev_get_prio_tc_map(dev, prio);
528 if (!(gate_mask & BIT(tc)))
537 static struct sk_buff *taprio_peek_offload(struct Qdisc *sch)
539 WARN_ONCE(1, "Trying to peek into the root of a taprio qdisc configured with full offload\n");
544 static struct sk_buff *taprio_peek(struct Qdisc *sch)
546 struct taprio_sched *q = qdisc_priv(sch);
551 static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry)
553 atomic_set(&entry->budget,
554 div64_u64((u64)entry->interval * 1000,
555 atomic64_read(&q->picos_per_byte)));
558 static struct sk_buff *taprio_dequeue_soft(struct Qdisc *sch)
560 struct taprio_sched *q = qdisc_priv(sch);
561 struct net_device *dev = qdisc_dev(sch);
562 struct sk_buff *skb = NULL;
563 struct sched_entry *entry;
568 entry = rcu_dereference(q->current_entry);
569 /* if there's no entry, it means that the schedule didn't
570 * start yet, so force all gates to be open, this is in
571 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
574 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
579 for (i = 0; i < dev->num_tx_queues; i++) {
580 struct Qdisc *child = q->qdiscs[i];
586 if (unlikely(!child))
589 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
590 skb = child->ops->dequeue(child);
596 skb = child->ops->peek(child);
600 prio = skb->priority;
601 tc = netdev_get_prio_tc_map(dev, prio);
603 if (!(gate_mask & BIT(tc))) {
608 len = qdisc_pkt_len(skb);
609 guard = ktime_add_ns(taprio_get_time(q),
610 length_to_duration(q, len));
612 /* In the case that there's no gate entry, there's no
615 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
616 ktime_after(guard, entry->close_time)) {
621 /* ... and no budget. */
622 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
623 atomic_sub_return(len, &entry->budget) < 0) {
628 skb = child->ops->dequeue(child);
633 qdisc_bstats_update(sch, skb);
634 qdisc_qstats_backlog_dec(sch, skb);
646 static struct sk_buff *taprio_dequeue_offload(struct Qdisc *sch)
648 WARN_ONCE(1, "Trying to dequeue from the root of a taprio qdisc configured with full offload\n");
653 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
655 struct taprio_sched *q = qdisc_priv(sch);
657 return q->dequeue(sch);
660 static bool should_restart_cycle(const struct sched_gate_list *oper,
661 const struct sched_entry *entry)
663 if (list_is_last(&entry->list, &oper->entries))
666 if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0)
672 static bool should_change_schedules(const struct sched_gate_list *admin,
673 const struct sched_gate_list *oper,
676 ktime_t next_base_time, extension_time;
681 next_base_time = sched_base_time(admin);
683 /* This is the simple case, the close_time would fall after
684 * the next schedule base_time.
686 if (ktime_compare(next_base_time, close_time) <= 0)
689 /* This is the cycle_time_extension case, if the close_time
690 * plus the amount that can be extended would fall after the
691 * next schedule base_time, we can extend the current schedule
694 extension_time = ktime_add_ns(close_time, oper->cycle_time_extension);
696 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
697 * how precisely the extension should be made. So after
698 * conformance testing, this logic may change.
700 if (ktime_compare(next_base_time, extension_time) <= 0)
706 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
708 struct taprio_sched *q = container_of(timer, struct taprio_sched,
710 struct sched_gate_list *oper, *admin;
711 struct sched_entry *entry, *next;
712 struct Qdisc *sch = q->root;
715 spin_lock(&q->current_entry_lock);
716 entry = rcu_dereference_protected(q->current_entry,
717 lockdep_is_held(&q->current_entry_lock));
718 oper = rcu_dereference_protected(q->oper_sched,
719 lockdep_is_held(&q->current_entry_lock));
720 admin = rcu_dereference_protected(q->admin_sched,
721 lockdep_is_held(&q->current_entry_lock));
724 switch_schedules(q, &admin, &oper);
726 /* This can happen in two cases: 1. this is the very first run
727 * of this function (i.e. we weren't running any schedule
728 * previously); 2. The previous schedule just ended. The first
729 * entry of all schedules are pre-calculated during the
730 * schedule initialization.
732 if (unlikely(!entry || entry->close_time == oper->base_time)) {
733 next = list_first_entry(&oper->entries, struct sched_entry,
735 close_time = next->close_time;
739 if (should_restart_cycle(oper, entry)) {
740 next = list_first_entry(&oper->entries, struct sched_entry,
742 oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time,
745 next = list_next_entry(entry, list);
748 close_time = ktime_add_ns(entry->close_time, next->interval);
749 close_time = min_t(ktime_t, close_time, oper->cycle_close_time);
751 if (should_change_schedules(admin, oper, close_time)) {
752 /* Set things so the next time this runs, the new
755 close_time = sched_base_time(admin);
756 switch_schedules(q, &admin, &oper);
759 next->close_time = close_time;
760 taprio_set_budget(q, next);
763 rcu_assign_pointer(q->current_entry, next);
764 spin_unlock(&q->current_entry_lock);
766 hrtimer_set_expires(&q->advance_timer, close_time);
769 __netif_schedule(sch);
772 return HRTIMER_RESTART;
775 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
776 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
777 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
778 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
779 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
782 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
783 [TCA_TAPRIO_ATTR_PRIOMAP] = {
784 .len = sizeof(struct tc_mqprio_qopt)
786 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
787 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
788 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
789 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
790 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 },
791 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
792 [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 },
793 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 },
796 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
797 struct sched_entry *entry,
798 struct netlink_ext_ack *extack)
800 int min_duration = length_to_duration(q, ETH_ZLEN);
803 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
804 entry->command = nla_get_u8(
805 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
807 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
808 entry->gate_mask = nla_get_u32(
809 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
811 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
812 interval = nla_get_u32(
813 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
815 /* The interval should allow at least the minimum ethernet
818 if (interval < min_duration) {
819 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
823 entry->interval = interval;
828 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
829 struct sched_entry *entry, int index,
830 struct netlink_ext_ack *extack)
832 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
835 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
838 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
842 entry->index = index;
844 return fill_sched_entry(q, tb, entry, extack);
847 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
848 struct sched_gate_list *sched,
849 struct netlink_ext_ack *extack)
858 nla_for_each_nested(n, list, rem) {
859 struct sched_entry *entry;
861 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
862 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
866 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
868 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
872 err = parse_sched_entry(q, n, entry, i, extack);
878 list_add_tail(&entry->list, &sched->entries);
882 sched->num_entries = i;
887 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
888 struct sched_gate_list *new,
889 struct netlink_ext_ack *extack)
893 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
894 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
898 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
899 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
901 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
902 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
904 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
905 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
907 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
908 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
913 if (!new->cycle_time) {
914 struct sched_entry *entry;
917 list_for_each_entry(entry, &new->entries, list)
918 cycle = ktime_add_ns(cycle, entry->interval);
921 NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
925 new->cycle_time = cycle;
931 static int taprio_parse_mqprio_opt(struct net_device *dev,
932 struct tc_mqprio_qopt *qopt,
933 struct netlink_ext_ack *extack,
938 if (!qopt && !dev->num_tc) {
939 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
943 /* If num_tc is already set, it means that the user already
944 * configured the mqprio part
949 /* Verify num_tc is not out of max range */
950 if (qopt->num_tc > TC_MAX_QUEUE) {
951 NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
955 /* taprio imposes that traffic classes map 1:n to tx queues */
956 if (qopt->num_tc > dev->num_tx_queues) {
957 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
961 /* Verify priority mapping uses valid tcs */
962 for (i = 0; i <= TC_BITMASK; i++) {
963 if (qopt->prio_tc_map[i] >= qopt->num_tc) {
964 NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
969 for (i = 0; i < qopt->num_tc; i++) {
970 unsigned int last = qopt->offset[i] + qopt->count[i];
972 /* Verify the queue count is in tx range being equal to the
973 * real_num_tx_queues indicates the last queue is in use.
975 if (qopt->offset[i] >= dev->num_tx_queues ||
977 last > dev->real_num_tx_queues) {
978 NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
982 if (TXTIME_ASSIST_IS_ENABLED(taprio_flags))
985 /* Verify that the offset and counts do not overlap */
986 for (j = i + 1; j < qopt->num_tc; j++) {
987 if (last > qopt->offset[j]) {
988 NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
997 static int taprio_get_start_time(struct Qdisc *sch,
998 struct sched_gate_list *sched,
1001 struct taprio_sched *q = qdisc_priv(sch);
1002 ktime_t now, base, cycle;
1005 base = sched_base_time(sched);
1006 now = taprio_get_time(q);
1008 if (ktime_after(base, now)) {
1013 cycle = sched->cycle_time;
1015 /* The qdisc is expected to have at least one sched_entry. Moreover,
1016 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
1017 * something went really wrong. In that case, we should warn about this
1018 * inconsistent state and return error.
1020 if (WARN_ON(!cycle))
1023 /* Schedule the start time for the beginning of the next
1026 n = div64_s64(ktime_sub_ns(now, base), cycle);
1027 *start = ktime_add_ns(base, (n + 1) * cycle);
1031 static void setup_first_close_time(struct taprio_sched *q,
1032 struct sched_gate_list *sched, ktime_t base)
1034 struct sched_entry *first;
1037 first = list_first_entry(&sched->entries,
1038 struct sched_entry, list);
1040 cycle = sched->cycle_time;
1042 /* FIXME: find a better place to do this */
1043 sched->cycle_close_time = ktime_add_ns(base, cycle);
1045 first->close_time = ktime_add_ns(base, first->interval);
1046 taprio_set_budget(q, first);
1047 rcu_assign_pointer(q->current_entry, NULL);
1050 static void taprio_start_sched(struct Qdisc *sch,
1051 ktime_t start, struct sched_gate_list *new)
1053 struct taprio_sched *q = qdisc_priv(sch);
1056 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1059 expires = hrtimer_get_expires(&q->advance_timer);
1061 expires = KTIME_MAX;
1063 /* If the new schedule starts before the next expiration, we
1064 * reprogram it to the earliest one, so we change the admin
1065 * schedule to the operational one at the right time.
1067 start = min_t(ktime_t, start, expires);
1069 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1072 static void taprio_set_picos_per_byte(struct net_device *dev,
1073 struct taprio_sched *q)
1075 struct ethtool_link_ksettings ecmd;
1076 int speed = SPEED_10;
1080 err = __ethtool_get_link_ksettings(dev, &ecmd);
1084 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1085 speed = ecmd.base.speed;
1088 picos_per_byte = (USEC_PER_SEC * 8) / speed;
1090 atomic64_set(&q->picos_per_byte, picos_per_byte);
1091 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1092 dev->name, (long long)atomic64_read(&q->picos_per_byte),
1096 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1099 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1100 struct net_device *qdev;
1101 struct taprio_sched *q;
1106 if (event != NETDEV_UP && event != NETDEV_CHANGE)
1109 spin_lock(&taprio_list_lock);
1110 list_for_each_entry(q, &taprio_list, taprio_list) {
1111 qdev = qdisc_dev(q->root);
1117 spin_unlock(&taprio_list_lock);
1120 taprio_set_picos_per_byte(dev, q);
1125 static void setup_txtime(struct taprio_sched *q,
1126 struct sched_gate_list *sched, ktime_t base)
1128 struct sched_entry *entry;
1131 list_for_each_entry(entry, &sched->entries, list) {
1132 entry->next_txtime = ktime_add_ns(base, interval);
1133 interval += entry->interval;
1137 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1139 struct __tc_taprio_qopt_offload *__offload;
1141 __offload = kzalloc(struct_size(__offload, offload.entries, num_entries),
1146 refcount_set(&__offload->users, 1);
1148 return &__offload->offload;
1151 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1154 struct __tc_taprio_qopt_offload *__offload;
1156 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1159 refcount_inc(&__offload->users);
1163 EXPORT_SYMBOL_GPL(taprio_offload_get);
1165 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1167 struct __tc_taprio_qopt_offload *__offload;
1169 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1172 if (!refcount_dec_and_test(&__offload->users))
1177 EXPORT_SYMBOL_GPL(taprio_offload_free);
1179 /* The function will only serve to keep the pointers to the "oper" and "admin"
1180 * schedules valid in relation to their base times, so when calling dump() the
1181 * users looks at the right schedules.
1182 * When using full offload, the admin configuration is promoted to oper at the
1183 * base_time in the PHC time domain. But because the system time is not
1184 * necessarily in sync with that, we can't just trigger a hrtimer to call
1185 * switch_schedules at the right hardware time.
1186 * At the moment we call this by hand right away from taprio, but in the future
1187 * it will be useful to create a mechanism for drivers to notify taprio of the
1188 * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1189 * This is left as TODO.
1191 static void taprio_offload_config_changed(struct taprio_sched *q)
1193 struct sched_gate_list *oper, *admin;
1195 spin_lock(&q->current_entry_lock);
1197 oper = rcu_dereference_protected(q->oper_sched,
1198 lockdep_is_held(&q->current_entry_lock));
1199 admin = rcu_dereference_protected(q->admin_sched,
1200 lockdep_is_held(&q->current_entry_lock));
1202 switch_schedules(q, &admin, &oper);
1204 spin_unlock(&q->current_entry_lock);
1207 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1209 u32 i, queue_mask = 0;
1211 for (i = 0; i < dev->num_tc; i++) {
1214 if (!(tc_mask & BIT(i)))
1217 offset = dev->tc_to_txq[i].offset;
1218 count = dev->tc_to_txq[i].count;
1220 queue_mask |= GENMASK(offset + count - 1, offset);
1226 static void taprio_sched_to_offload(struct net_device *dev,
1227 struct sched_gate_list *sched,
1228 struct tc_taprio_qopt_offload *offload)
1230 struct sched_entry *entry;
1233 offload->base_time = sched->base_time;
1234 offload->cycle_time = sched->cycle_time;
1235 offload->cycle_time_extension = sched->cycle_time_extension;
1237 list_for_each_entry(entry, &sched->entries, list) {
1238 struct tc_taprio_sched_entry *e = &offload->entries[i];
1240 e->command = entry->command;
1241 e->interval = entry->interval;
1242 e->gate_mask = tc_map_to_queue_mask(dev, entry->gate_mask);
1247 offload->num_entries = i;
1250 static int taprio_enable_offload(struct net_device *dev,
1251 struct taprio_sched *q,
1252 struct sched_gate_list *sched,
1253 struct netlink_ext_ack *extack)
1255 const struct net_device_ops *ops = dev->netdev_ops;
1256 struct tc_taprio_qopt_offload *offload;
1259 if (!ops->ndo_setup_tc) {
1260 NL_SET_ERR_MSG(extack,
1261 "Device does not support taprio offload");
1265 offload = taprio_offload_alloc(sched->num_entries);
1267 NL_SET_ERR_MSG(extack,
1268 "Not enough memory for enabling offload mode");
1271 offload->enable = 1;
1272 taprio_sched_to_offload(dev, sched, offload);
1274 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1276 NL_SET_ERR_MSG(extack,
1277 "Device failed to setup taprio offload");
1282 taprio_offload_free(offload);
1287 static int taprio_disable_offload(struct net_device *dev,
1288 struct taprio_sched *q,
1289 struct netlink_ext_ack *extack)
1291 const struct net_device_ops *ops = dev->netdev_ops;
1292 struct tc_taprio_qopt_offload *offload;
1295 if (!FULL_OFFLOAD_IS_ENABLED(q->flags))
1298 if (!ops->ndo_setup_tc)
1301 offload = taprio_offload_alloc(0);
1303 NL_SET_ERR_MSG(extack,
1304 "Not enough memory to disable offload mode");
1307 offload->enable = 0;
1309 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1311 NL_SET_ERR_MSG(extack,
1312 "Device failed to disable offload");
1317 taprio_offload_free(offload);
1322 /* If full offload is enabled, the only possible clockid is the net device's
1323 * PHC. For that reason, specifying a clockid through netlink is incorrect.
1324 * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1325 * in sync with the specified clockid via a user space daemon such as phc2sys.
1326 * For both software taprio and txtime-assist, the clockid is used for the
1327 * hrtimer that advances the schedule and hence mandatory.
1329 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1330 struct netlink_ext_ack *extack)
1332 struct taprio_sched *q = qdisc_priv(sch);
1333 struct net_device *dev = qdisc_dev(sch);
1336 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1337 const struct ethtool_ops *ops = dev->ethtool_ops;
1338 struct ethtool_ts_info info = {
1339 .cmd = ETHTOOL_GET_TS_INFO,
1343 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1344 NL_SET_ERR_MSG(extack,
1345 "The 'clockid' cannot be specified for full offload");
1349 if (ops && ops->get_ts_info)
1350 err = ops->get_ts_info(dev, &info);
1352 if (err || info.phc_index < 0) {
1353 NL_SET_ERR_MSG(extack,
1354 "Device does not have a PTP clock");
1358 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1359 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1360 enum tk_offsets tk_offset;
1362 /* We only support static clockids and we don't allow
1363 * for it to be modified after the first init.
1366 (q->clockid != -1 && q->clockid != clockid)) {
1367 NL_SET_ERR_MSG(extack,
1368 "Changing the 'clockid' of a running schedule is not supported");
1374 case CLOCK_REALTIME:
1375 tk_offset = TK_OFFS_REAL;
1377 case CLOCK_MONOTONIC:
1378 tk_offset = TK_OFFS_MAX;
1380 case CLOCK_BOOTTIME:
1381 tk_offset = TK_OFFS_BOOT;
1384 tk_offset = TK_OFFS_TAI;
1387 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1391 /* This pairs with READ_ONCE() in taprio_mono_to_any */
1392 WRITE_ONCE(q->tk_offset, tk_offset);
1394 q->clockid = clockid;
1396 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1400 /* Everything went ok, return success. */
1407 static int taprio_mqprio_cmp(const struct net_device *dev,
1408 const struct tc_mqprio_qopt *mqprio)
1412 if (!mqprio || mqprio->num_tc != dev->num_tc)
1415 for (i = 0; i < mqprio->num_tc; i++)
1416 if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1417 dev->tc_to_txq[i].offset != mqprio->offset[i])
1420 for (i = 0; i <= TC_BITMASK; i++)
1421 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1427 /* The semantics of the 'flags' argument in relation to 'change()'
1428 * requests, are interpreted following two rules (which are applied in
1429 * this order): (1) an omitted 'flags' argument is interpreted as
1430 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1433 static int taprio_new_flags(const struct nlattr *attr, u32 old,
1434 struct netlink_ext_ack *extack)
1439 new = nla_get_u32(attr);
1441 if (old != TAPRIO_FLAGS_INVALID && old != new) {
1442 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1446 if (!taprio_flags_valid(new)) {
1447 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1454 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1455 struct netlink_ext_ack *extack)
1457 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1458 struct sched_gate_list *oper, *admin, *new_admin;
1459 struct taprio_sched *q = qdisc_priv(sch);
1460 struct net_device *dev = qdisc_dev(sch);
1461 struct tc_mqprio_qopt *mqprio = NULL;
1462 unsigned long flags;
1466 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1467 taprio_policy, extack);
1471 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1472 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1474 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1481 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1485 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1487 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1490 INIT_LIST_HEAD(&new_admin->entries);
1493 oper = rcu_dereference(q->oper_sched);
1494 admin = rcu_dereference(q->admin_sched);
1497 /* no changes - no new mqprio settings */
1498 if (!taprio_mqprio_cmp(dev, mqprio))
1501 if (mqprio && (oper || admin)) {
1502 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1507 err = parse_taprio_schedule(q, tb, new_admin, extack);
1511 if (new_admin->num_entries == 0) {
1512 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1517 err = taprio_parse_clockid(sch, tb, extack);
1521 taprio_set_picos_per_byte(dev, q);
1524 err = netdev_set_num_tc(dev, mqprio->num_tc);
1527 for (i = 0; i < mqprio->num_tc; i++)
1528 netdev_set_tc_queue(dev, i,
1532 /* Always use supplied priority mappings */
1533 for (i = 0; i <= TC_BITMASK; i++)
1534 netdev_set_prio_tc_map(dev, i,
1535 mqprio->prio_tc_map[i]);
1538 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1539 err = taprio_enable_offload(dev, q, new_admin, extack);
1541 err = taprio_disable_offload(dev, q, extack);
1545 /* Protects against enqueue()/dequeue() */
1546 spin_lock_bh(qdisc_lock(sch));
1548 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1549 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1550 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1555 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1558 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1559 !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1560 !hrtimer_active(&q->advance_timer)) {
1561 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1562 q->advance_timer.function = advance_sched;
1565 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1566 q->dequeue = taprio_dequeue_offload;
1567 q->peek = taprio_peek_offload;
1569 /* Be sure to always keep the function pointers
1570 * in a consistent state.
1572 q->dequeue = taprio_dequeue_soft;
1573 q->peek = taprio_peek_soft;
1576 err = taprio_get_start_time(sch, new_admin, &start);
1578 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1582 setup_txtime(q, new_admin, start);
1584 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1586 rcu_assign_pointer(q->oper_sched, new_admin);
1592 rcu_assign_pointer(q->admin_sched, new_admin);
1594 call_rcu(&admin->rcu, taprio_free_sched_cb);
1596 setup_first_close_time(q, new_admin, start);
1598 /* Protects against advance_sched() */
1599 spin_lock_irqsave(&q->current_entry_lock, flags);
1601 taprio_start_sched(sch, start, new_admin);
1603 rcu_assign_pointer(q->admin_sched, new_admin);
1605 call_rcu(&admin->rcu, taprio_free_sched_cb);
1607 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1609 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1610 taprio_offload_config_changed(q);
1617 spin_unlock_bh(qdisc_lock(sch));
1621 call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1626 static void taprio_reset(struct Qdisc *sch)
1628 struct taprio_sched *q = qdisc_priv(sch);
1629 struct net_device *dev = qdisc_dev(sch);
1632 hrtimer_cancel(&q->advance_timer);
1634 for (i = 0; i < dev->num_tx_queues; i++)
1636 qdisc_reset(q->qdiscs[i]);
1638 sch->qstats.backlog = 0;
1642 static void taprio_destroy(struct Qdisc *sch)
1644 struct taprio_sched *q = qdisc_priv(sch);
1645 struct net_device *dev = qdisc_dev(sch);
1648 spin_lock(&taprio_list_lock);
1649 list_del(&q->taprio_list);
1650 spin_unlock(&taprio_list_lock);
1652 /* Note that taprio_reset() might not be called if an error
1653 * happens in qdisc_create(), after taprio_init() has been called.
1655 hrtimer_cancel(&q->advance_timer);
1657 taprio_disable_offload(dev, q, NULL);
1660 for (i = 0; i < dev->num_tx_queues; i++)
1661 qdisc_put(q->qdiscs[i]);
1667 netdev_reset_tc(dev);
1670 call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb);
1673 call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb);
1676 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1677 struct netlink_ext_ack *extack)
1679 struct taprio_sched *q = qdisc_priv(sch);
1680 struct net_device *dev = qdisc_dev(sch);
1683 spin_lock_init(&q->current_entry_lock);
1685 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1686 q->advance_timer.function = advance_sched;
1688 q->dequeue = taprio_dequeue_soft;
1689 q->peek = taprio_peek_soft;
1693 /* We only support static clockids. Use an invalid value as default
1694 * and get the valid one on taprio_change().
1697 q->flags = TAPRIO_FLAGS_INVALID;
1699 spin_lock(&taprio_list_lock);
1700 list_add(&q->taprio_list, &taprio_list);
1701 spin_unlock(&taprio_list_lock);
1703 if (sch->parent != TC_H_ROOT)
1706 if (!netif_is_multiqueue(dev))
1709 /* pre-allocate qdisc, attachment can't fail */
1710 q->qdiscs = kcalloc(dev->num_tx_queues,
1711 sizeof(q->qdiscs[0]),
1720 for (i = 0; i < dev->num_tx_queues; i++) {
1721 struct netdev_queue *dev_queue;
1722 struct Qdisc *qdisc;
1724 dev_queue = netdev_get_tx_queue(dev, i);
1725 qdisc = qdisc_create_dflt(dev_queue,
1727 TC_H_MAKE(TC_H_MAJ(sch->handle),
1733 if (i < dev->real_num_tx_queues)
1734 qdisc_hash_add(qdisc, false);
1736 q->qdiscs[i] = qdisc;
1739 return taprio_change(sch, opt, extack);
1742 static void taprio_attach(struct Qdisc *sch)
1744 struct taprio_sched *q = qdisc_priv(sch);
1745 struct net_device *dev = qdisc_dev(sch);
1748 /* Attach underlying qdisc */
1749 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
1750 struct Qdisc *qdisc = q->qdiscs[ntx];
1753 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1754 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1755 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
1757 old = dev_graft_qdisc(qdisc->dev_queue, sch);
1758 qdisc_refcount_inc(sch);
1764 /* access to the child qdiscs is not needed in offload mode */
1765 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1771 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1774 struct net_device *dev = qdisc_dev(sch);
1775 unsigned long ntx = cl - 1;
1777 if (ntx >= dev->num_tx_queues)
1780 return netdev_get_tx_queue(dev, ntx);
1783 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1784 struct Qdisc *new, struct Qdisc **old,
1785 struct netlink_ext_ack *extack)
1787 struct taprio_sched *q = qdisc_priv(sch);
1788 struct net_device *dev = qdisc_dev(sch);
1789 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1794 if (dev->flags & IFF_UP)
1795 dev_deactivate(dev);
1797 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1798 *old = dev_graft_qdisc(dev_queue, new);
1800 *old = q->qdiscs[cl - 1];
1801 q->qdiscs[cl - 1] = new;
1805 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1807 if (dev->flags & IFF_UP)
1813 static int dump_entry(struct sk_buff *msg,
1814 const struct sched_entry *entry)
1816 struct nlattr *item;
1818 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1822 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1823 goto nla_put_failure;
1825 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1826 goto nla_put_failure;
1828 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1830 goto nla_put_failure;
1832 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1834 goto nla_put_failure;
1836 return nla_nest_end(msg, item);
1839 nla_nest_cancel(msg, item);
1843 static int dump_schedule(struct sk_buff *msg,
1844 const struct sched_gate_list *root)
1846 struct nlattr *entry_list;
1847 struct sched_entry *entry;
1849 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1850 root->base_time, TCA_TAPRIO_PAD))
1853 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1854 root->cycle_time, TCA_TAPRIO_PAD))
1857 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1858 root->cycle_time_extension, TCA_TAPRIO_PAD))
1861 entry_list = nla_nest_start_noflag(msg,
1862 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1866 list_for_each_entry(entry, &root->entries, list) {
1867 if (dump_entry(msg, entry) < 0)
1871 nla_nest_end(msg, entry_list);
1875 nla_nest_cancel(msg, entry_list);
1879 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1881 struct taprio_sched *q = qdisc_priv(sch);
1882 struct net_device *dev = qdisc_dev(sch);
1883 struct sched_gate_list *oper, *admin;
1884 struct tc_mqprio_qopt opt = { 0 };
1885 struct nlattr *nest, *sched_nest;
1889 oper = rcu_dereference(q->oper_sched);
1890 admin = rcu_dereference(q->admin_sched);
1892 opt.num_tc = netdev_get_num_tc(dev);
1893 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
1895 for (i = 0; i < netdev_get_num_tc(dev); i++) {
1896 opt.count[i] = dev->tc_to_txq[i].count;
1897 opt.offset[i] = dev->tc_to_txq[i].offset;
1900 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1904 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
1907 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1908 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
1911 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
1914 if (q->txtime_delay &&
1915 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
1918 if (oper && dump_schedule(skb, oper))
1924 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
1928 if (dump_schedule(skb, admin))
1931 nla_nest_end(skb, sched_nest);
1936 return nla_nest_end(skb, nest);
1939 nla_nest_cancel(skb, sched_nest);
1942 nla_nest_cancel(skb, nest);
1949 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
1951 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1956 return dev_queue->qdisc_sleeping;
1959 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
1961 unsigned int ntx = TC_H_MIN(classid);
1963 if (!taprio_queue_get(sch, ntx))
1968 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
1969 struct sk_buff *skb, struct tcmsg *tcm)
1971 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1973 tcm->tcm_parent = TC_H_ROOT;
1974 tcm->tcm_handle |= TC_H_MIN(cl);
1975 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
1980 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
1981 struct gnet_dump *d)
1985 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1987 sch = dev_queue->qdisc_sleeping;
1988 if (gnet_stats_copy_basic(d, NULL, &sch->bstats, true) < 0 ||
1989 qdisc_qstats_copy(d, sch) < 0)
1994 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1996 struct net_device *dev = qdisc_dev(sch);
2002 arg->count = arg->skip;
2003 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
2004 if (arg->fn(sch, ntx + 1, arg) < 0) {
2012 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
2015 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
2018 static const struct Qdisc_class_ops taprio_class_ops = {
2019 .graft = taprio_graft,
2020 .leaf = taprio_leaf,
2021 .find = taprio_find,
2022 .walk = taprio_walk,
2023 .dump = taprio_dump_class,
2024 .dump_stats = taprio_dump_class_stats,
2025 .select_queue = taprio_select_queue,
2028 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
2029 .cl_ops = &taprio_class_ops,
2031 .priv_size = sizeof(struct taprio_sched),
2032 .init = taprio_init,
2033 .change = taprio_change,
2034 .destroy = taprio_destroy,
2035 .reset = taprio_reset,
2036 .attach = taprio_attach,
2037 .peek = taprio_peek,
2038 .dequeue = taprio_dequeue,
2039 .enqueue = taprio_enqueue,
2040 .dump = taprio_dump,
2041 .owner = THIS_MODULE,
2044 static struct notifier_block taprio_device_notifier = {
2045 .notifier_call = taprio_dev_notifier,
2048 static int __init taprio_module_init(void)
2050 int err = register_netdevice_notifier(&taprio_device_notifier);
2055 return register_qdisc(&taprio_qdisc_ops);
2058 static void __exit taprio_module_exit(void)
2060 unregister_qdisc(&taprio_qdisc_ops);
2061 unregister_netdevice_notifier(&taprio_device_notifier);
2064 module_init(taprio_module_init);
2065 module_exit(taprio_module_exit);
2066 MODULE_LICENSE("GPL");