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;
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), 1000);
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 * 1000,
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");
1282 q->offloaded = true;
1285 taprio_offload_free(offload);
1290 static int taprio_disable_offload(struct net_device *dev,
1291 struct taprio_sched *q,
1292 struct netlink_ext_ack *extack)
1294 const struct net_device_ops *ops = dev->netdev_ops;
1295 struct tc_taprio_qopt_offload *offload;
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");
1316 q->offloaded = false;
1319 taprio_offload_free(offload);
1324 /* If full offload is enabled, the only possible clockid is the net device's
1325 * PHC. For that reason, specifying a clockid through netlink is incorrect.
1326 * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1327 * in sync with the specified clockid via a user space daemon such as phc2sys.
1328 * For both software taprio and txtime-assist, the clockid is used for the
1329 * hrtimer that advances the schedule and hence mandatory.
1331 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1332 struct netlink_ext_ack *extack)
1334 struct taprio_sched *q = qdisc_priv(sch);
1335 struct net_device *dev = qdisc_dev(sch);
1338 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1339 const struct ethtool_ops *ops = dev->ethtool_ops;
1340 struct ethtool_ts_info info = {
1341 .cmd = ETHTOOL_GET_TS_INFO,
1345 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1346 NL_SET_ERR_MSG(extack,
1347 "The 'clockid' cannot be specified for full offload");
1351 if (ops && ops->get_ts_info)
1352 err = ops->get_ts_info(dev, &info);
1354 if (err || info.phc_index < 0) {
1355 NL_SET_ERR_MSG(extack,
1356 "Device does not have a PTP clock");
1360 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1361 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1362 enum tk_offsets tk_offset;
1364 /* We only support static clockids and we don't allow
1365 * for it to be modified after the first init.
1368 (q->clockid != -1 && q->clockid != clockid)) {
1369 NL_SET_ERR_MSG(extack,
1370 "Changing the 'clockid' of a running schedule is not supported");
1376 case CLOCK_REALTIME:
1377 tk_offset = TK_OFFS_REAL;
1379 case CLOCK_MONOTONIC:
1380 tk_offset = TK_OFFS_MAX;
1382 case CLOCK_BOOTTIME:
1383 tk_offset = TK_OFFS_BOOT;
1386 tk_offset = TK_OFFS_TAI;
1389 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1393 /* This pairs with READ_ONCE() in taprio_mono_to_any */
1394 WRITE_ONCE(q->tk_offset, tk_offset);
1396 q->clockid = clockid;
1398 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1402 /* Everything went ok, return success. */
1409 static int taprio_mqprio_cmp(const struct net_device *dev,
1410 const struct tc_mqprio_qopt *mqprio)
1414 if (!mqprio || mqprio->num_tc != dev->num_tc)
1417 for (i = 0; i < mqprio->num_tc; i++)
1418 if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1419 dev->tc_to_txq[i].offset != mqprio->offset[i])
1422 for (i = 0; i <= TC_BITMASK; i++)
1423 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1429 /* The semantics of the 'flags' argument in relation to 'change()'
1430 * requests, are interpreted following two rules (which are applied in
1431 * this order): (1) an omitted 'flags' argument is interpreted as
1432 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1435 static int taprio_new_flags(const struct nlattr *attr, u32 old,
1436 struct netlink_ext_ack *extack)
1441 new = nla_get_u32(attr);
1443 if (old != TAPRIO_FLAGS_INVALID && old != new) {
1444 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1448 if (!taprio_flags_valid(new)) {
1449 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1456 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1457 struct netlink_ext_ack *extack)
1459 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1460 struct sched_gate_list *oper, *admin, *new_admin;
1461 struct taprio_sched *q = qdisc_priv(sch);
1462 struct net_device *dev = qdisc_dev(sch);
1463 struct tc_mqprio_qopt *mqprio = NULL;
1464 unsigned long flags;
1468 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1469 taprio_policy, extack);
1473 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1474 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1476 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1483 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1487 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1489 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1492 INIT_LIST_HEAD(&new_admin->entries);
1495 oper = rcu_dereference(q->oper_sched);
1496 admin = rcu_dereference(q->admin_sched);
1499 /* no changes - no new mqprio settings */
1500 if (!taprio_mqprio_cmp(dev, mqprio))
1503 if (mqprio && (oper || admin)) {
1504 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1509 err = parse_taprio_schedule(q, tb, new_admin, extack);
1513 if (new_admin->num_entries == 0) {
1514 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1519 err = taprio_parse_clockid(sch, tb, extack);
1523 taprio_set_picos_per_byte(dev, q);
1526 err = netdev_set_num_tc(dev, mqprio->num_tc);
1529 for (i = 0; i < mqprio->num_tc; i++)
1530 netdev_set_tc_queue(dev, i,
1534 /* Always use supplied priority mappings */
1535 for (i = 0; i <= TC_BITMASK; i++)
1536 netdev_set_prio_tc_map(dev, i,
1537 mqprio->prio_tc_map[i]);
1540 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1541 err = taprio_enable_offload(dev, q, new_admin, extack);
1543 err = taprio_disable_offload(dev, q, extack);
1547 /* Protects against enqueue()/dequeue() */
1548 spin_lock_bh(qdisc_lock(sch));
1550 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1551 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1552 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1557 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1560 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1561 !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1562 !hrtimer_active(&q->advance_timer)) {
1563 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1564 q->advance_timer.function = advance_sched;
1567 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1568 q->dequeue = taprio_dequeue_offload;
1569 q->peek = taprio_peek_offload;
1571 /* Be sure to always keep the function pointers
1572 * in a consistent state.
1574 q->dequeue = taprio_dequeue_soft;
1575 q->peek = taprio_peek_soft;
1578 err = taprio_get_start_time(sch, new_admin, &start);
1580 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1584 setup_txtime(q, new_admin, start);
1586 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1588 rcu_assign_pointer(q->oper_sched, new_admin);
1594 rcu_assign_pointer(q->admin_sched, new_admin);
1596 call_rcu(&admin->rcu, taprio_free_sched_cb);
1598 setup_first_close_time(q, new_admin, start);
1600 /* Protects against advance_sched() */
1601 spin_lock_irqsave(&q->current_entry_lock, flags);
1603 taprio_start_sched(sch, start, new_admin);
1605 rcu_assign_pointer(q->admin_sched, new_admin);
1607 call_rcu(&admin->rcu, taprio_free_sched_cb);
1609 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1611 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1612 taprio_offload_config_changed(q);
1619 spin_unlock_bh(qdisc_lock(sch));
1623 call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1628 static void taprio_reset(struct Qdisc *sch)
1630 struct taprio_sched *q = qdisc_priv(sch);
1631 struct net_device *dev = qdisc_dev(sch);
1634 hrtimer_cancel(&q->advance_timer);
1636 for (i = 0; i < dev->num_tx_queues; i++)
1638 qdisc_reset(q->qdiscs[i]);
1640 sch->qstats.backlog = 0;
1644 static void taprio_destroy(struct Qdisc *sch)
1646 struct taprio_sched *q = qdisc_priv(sch);
1647 struct net_device *dev = qdisc_dev(sch);
1650 spin_lock(&taprio_list_lock);
1651 list_del(&q->taprio_list);
1652 spin_unlock(&taprio_list_lock);
1654 /* Note that taprio_reset() might not be called if an error
1655 * happens in qdisc_create(), after taprio_init() has been called.
1657 hrtimer_cancel(&q->advance_timer);
1659 taprio_disable_offload(dev, q, NULL);
1662 for (i = 0; i < dev->num_tx_queues; i++)
1663 qdisc_put(q->qdiscs[i]);
1669 netdev_reset_tc(dev);
1672 call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb);
1675 call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb);
1678 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1679 struct netlink_ext_ack *extack)
1681 struct taprio_sched *q = qdisc_priv(sch);
1682 struct net_device *dev = qdisc_dev(sch);
1685 spin_lock_init(&q->current_entry_lock);
1687 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1688 q->advance_timer.function = advance_sched;
1690 q->dequeue = taprio_dequeue_soft;
1691 q->peek = taprio_peek_soft;
1695 /* We only support static clockids. Use an invalid value as default
1696 * and get the valid one on taprio_change().
1699 q->flags = TAPRIO_FLAGS_INVALID;
1701 spin_lock(&taprio_list_lock);
1702 list_add(&q->taprio_list, &taprio_list);
1703 spin_unlock(&taprio_list_lock);
1705 if (sch->parent != TC_H_ROOT)
1708 if (!netif_is_multiqueue(dev))
1711 /* pre-allocate qdisc, attachment can't fail */
1712 q->qdiscs = kcalloc(dev->num_tx_queues,
1713 sizeof(q->qdiscs[0]),
1722 for (i = 0; i < dev->num_tx_queues; i++) {
1723 struct netdev_queue *dev_queue;
1724 struct Qdisc *qdisc;
1726 dev_queue = netdev_get_tx_queue(dev, i);
1727 qdisc = qdisc_create_dflt(dev_queue,
1729 TC_H_MAKE(TC_H_MAJ(sch->handle),
1735 if (i < dev->real_num_tx_queues)
1736 qdisc_hash_add(qdisc, false);
1738 q->qdiscs[i] = qdisc;
1741 return taprio_change(sch, opt, extack);
1744 static void taprio_attach(struct Qdisc *sch)
1746 struct taprio_sched *q = qdisc_priv(sch);
1747 struct net_device *dev = qdisc_dev(sch);
1750 /* Attach underlying qdisc */
1751 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
1752 struct Qdisc *qdisc = q->qdiscs[ntx];
1755 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1756 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1757 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
1759 old = dev_graft_qdisc(qdisc->dev_queue, sch);
1760 qdisc_refcount_inc(sch);
1766 /* access to the child qdiscs is not needed in offload mode */
1767 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1773 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1776 struct net_device *dev = qdisc_dev(sch);
1777 unsigned long ntx = cl - 1;
1779 if (ntx >= dev->num_tx_queues)
1782 return netdev_get_tx_queue(dev, ntx);
1785 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1786 struct Qdisc *new, struct Qdisc **old,
1787 struct netlink_ext_ack *extack)
1789 struct taprio_sched *q = qdisc_priv(sch);
1790 struct net_device *dev = qdisc_dev(sch);
1791 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1796 if (dev->flags & IFF_UP)
1797 dev_deactivate(dev);
1799 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1800 *old = dev_graft_qdisc(dev_queue, new);
1802 *old = q->qdiscs[cl - 1];
1803 q->qdiscs[cl - 1] = new;
1807 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1809 if (dev->flags & IFF_UP)
1815 static int dump_entry(struct sk_buff *msg,
1816 const struct sched_entry *entry)
1818 struct nlattr *item;
1820 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1824 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1825 goto nla_put_failure;
1827 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1828 goto nla_put_failure;
1830 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1832 goto nla_put_failure;
1834 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1836 goto nla_put_failure;
1838 return nla_nest_end(msg, item);
1841 nla_nest_cancel(msg, item);
1845 static int dump_schedule(struct sk_buff *msg,
1846 const struct sched_gate_list *root)
1848 struct nlattr *entry_list;
1849 struct sched_entry *entry;
1851 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1852 root->base_time, TCA_TAPRIO_PAD))
1855 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1856 root->cycle_time, TCA_TAPRIO_PAD))
1859 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1860 root->cycle_time_extension, TCA_TAPRIO_PAD))
1863 entry_list = nla_nest_start_noflag(msg,
1864 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1868 list_for_each_entry(entry, &root->entries, list) {
1869 if (dump_entry(msg, entry) < 0)
1873 nla_nest_end(msg, entry_list);
1877 nla_nest_cancel(msg, entry_list);
1881 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1883 struct taprio_sched *q = qdisc_priv(sch);
1884 struct net_device *dev = qdisc_dev(sch);
1885 struct sched_gate_list *oper, *admin;
1886 struct tc_mqprio_qopt opt = { 0 };
1887 struct nlattr *nest, *sched_nest;
1891 oper = rcu_dereference(q->oper_sched);
1892 admin = rcu_dereference(q->admin_sched);
1894 opt.num_tc = netdev_get_num_tc(dev);
1895 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
1897 for (i = 0; i < netdev_get_num_tc(dev); i++) {
1898 opt.count[i] = dev->tc_to_txq[i].count;
1899 opt.offset[i] = dev->tc_to_txq[i].offset;
1902 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1906 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
1909 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1910 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
1913 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
1916 if (q->txtime_delay &&
1917 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
1920 if (oper && dump_schedule(skb, oper))
1926 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
1930 if (dump_schedule(skb, admin))
1933 nla_nest_end(skb, sched_nest);
1938 return nla_nest_end(skb, nest);
1941 nla_nest_cancel(skb, sched_nest);
1944 nla_nest_cancel(skb, nest);
1951 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
1953 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1958 return dev_queue->qdisc_sleeping;
1961 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
1963 unsigned int ntx = TC_H_MIN(classid);
1965 if (!taprio_queue_get(sch, ntx))
1970 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
1971 struct sk_buff *skb, struct tcmsg *tcm)
1973 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1975 tcm->tcm_parent = TC_H_ROOT;
1976 tcm->tcm_handle |= TC_H_MIN(cl);
1977 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
1982 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
1983 struct gnet_dump *d)
1987 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1989 sch = dev_queue->qdisc_sleeping;
1990 if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 ||
1991 qdisc_qstats_copy(d, sch) < 0)
1996 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1998 struct net_device *dev = qdisc_dev(sch);
2004 arg->count = arg->skip;
2005 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
2006 if (arg->fn(sch, ntx + 1, arg) < 0) {
2014 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
2017 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
2020 static const struct Qdisc_class_ops taprio_class_ops = {
2021 .graft = taprio_graft,
2022 .leaf = taprio_leaf,
2023 .find = taprio_find,
2024 .walk = taprio_walk,
2025 .dump = taprio_dump_class,
2026 .dump_stats = taprio_dump_class_stats,
2027 .select_queue = taprio_select_queue,
2030 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
2031 .cl_ops = &taprio_class_ops,
2033 .priv_size = sizeof(struct taprio_sched),
2034 .init = taprio_init,
2035 .change = taprio_change,
2036 .destroy = taprio_destroy,
2037 .reset = taprio_reset,
2038 .attach = taprio_attach,
2039 .peek = taprio_peek,
2040 .dequeue = taprio_dequeue,
2041 .enqueue = taprio_enqueue,
2042 .dump = taprio_dump,
2043 .owner = THIS_MODULE,
2046 static struct notifier_block taprio_device_notifier = {
2047 .notifier_call = taprio_dev_notifier,
2050 static int __init taprio_module_init(void)
2052 int err = register_netdevice_notifier(&taprio_device_notifier);
2057 return register_qdisc(&taprio_qdisc_ops);
2060 static void __exit taprio_module_exit(void)
2062 unregister_qdisc(&taprio_qdisc_ops);
2063 unregister_netdevice_notifier(&taprio_device_notifier);
2066 module_init(taprio_module_init);
2067 module_exit(taprio_module_exit);
2068 MODULE_LICENSE("GPL");