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 if (skb->sk && sock_flag(skb->sk, SOCK_TXTIME)) {
421 if (!is_valid_interval(skb, sch))
422 return qdisc_drop(skb, sch, to_free);
423 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
424 skb->tstamp = get_packet_txtime(skb, sch);
426 return qdisc_drop(skb, sch, to_free);
429 qdisc_qstats_backlog_inc(sch, skb);
432 return qdisc_enqueue(skb, child, to_free);
435 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
436 struct sk_buff **to_free)
438 struct taprio_sched *q = qdisc_priv(sch);
442 if (unlikely(FULL_OFFLOAD_IS_ENABLED(q->flags))) {
443 WARN_ONCE(1, "Trying to enqueue skb into the root of a taprio qdisc configured with full offload\n");
444 return qdisc_drop(skb, sch, to_free);
447 queue = skb_get_queue_mapping(skb);
449 child = q->qdiscs[queue];
450 if (unlikely(!child))
451 return qdisc_drop(skb, sch, to_free);
453 /* Large packets might not be transmitted when the transmission duration
454 * exceeds any configured interval. Therefore, segment the skb into
455 * smaller chunks. Skip it for the full offload case, as the driver
456 * and/or the hardware is expected to handle this.
458 if (skb_is_gso(skb) && !FULL_OFFLOAD_IS_ENABLED(q->flags)) {
459 unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb);
460 netdev_features_t features = netif_skb_features(skb);
461 struct sk_buff *segs, *nskb;
464 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
465 if (IS_ERR_OR_NULL(segs))
466 return qdisc_drop(skb, sch, to_free);
468 skb_list_walk_safe(segs, segs, nskb) {
469 skb_mark_not_on_list(segs);
470 qdisc_skb_cb(segs)->pkt_len = segs->len;
473 ret = taprio_enqueue_one(segs, sch, child, to_free);
474 if (ret != NET_XMIT_SUCCESS) {
475 if (net_xmit_drop_count(ret))
476 qdisc_qstats_drop(sch);
483 qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
486 return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
489 return taprio_enqueue_one(skb, sch, child, to_free);
492 static struct sk_buff *taprio_peek_soft(struct Qdisc *sch)
494 struct taprio_sched *q = qdisc_priv(sch);
495 struct net_device *dev = qdisc_dev(sch);
496 struct sched_entry *entry;
502 entry = rcu_dereference(q->current_entry);
503 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
509 for (i = 0; i < dev->num_tx_queues; i++) {
510 struct Qdisc *child = q->qdiscs[i];
514 if (unlikely(!child))
517 skb = child->ops->peek(child);
521 if (TXTIME_ASSIST_IS_ENABLED(q->flags))
524 prio = skb->priority;
525 tc = netdev_get_prio_tc_map(dev, prio);
527 if (!(gate_mask & BIT(tc)))
536 static struct sk_buff *taprio_peek_offload(struct Qdisc *sch)
538 WARN_ONCE(1, "Trying to peek into the root of a taprio qdisc configured with full offload\n");
543 static struct sk_buff *taprio_peek(struct Qdisc *sch)
545 struct taprio_sched *q = qdisc_priv(sch);
550 static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry)
552 atomic_set(&entry->budget,
553 div64_u64((u64)entry->interval * 1000,
554 atomic64_read(&q->picos_per_byte)));
557 static struct sk_buff *taprio_dequeue_soft(struct Qdisc *sch)
559 struct taprio_sched *q = qdisc_priv(sch);
560 struct net_device *dev = qdisc_dev(sch);
561 struct sk_buff *skb = NULL;
562 struct sched_entry *entry;
567 entry = rcu_dereference(q->current_entry);
568 /* if there's no entry, it means that the schedule didn't
569 * start yet, so force all gates to be open, this is in
570 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
573 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
578 for (i = 0; i < dev->num_tx_queues; i++) {
579 struct Qdisc *child = q->qdiscs[i];
585 if (unlikely(!child))
588 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
589 skb = child->ops->dequeue(child);
595 skb = child->ops->peek(child);
599 prio = skb->priority;
600 tc = netdev_get_prio_tc_map(dev, prio);
602 if (!(gate_mask & BIT(tc))) {
607 len = qdisc_pkt_len(skb);
608 guard = ktime_add_ns(taprio_get_time(q),
609 length_to_duration(q, len));
611 /* In the case that there's no gate entry, there's no
614 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
615 ktime_after(guard, entry->close_time)) {
620 /* ... and no budget. */
621 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
622 atomic_sub_return(len, &entry->budget) < 0) {
627 skb = child->ops->dequeue(child);
632 qdisc_bstats_update(sch, skb);
633 qdisc_qstats_backlog_dec(sch, skb);
645 static struct sk_buff *taprio_dequeue_offload(struct Qdisc *sch)
647 WARN_ONCE(1, "Trying to dequeue from the root of a taprio qdisc configured with full offload\n");
652 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
654 struct taprio_sched *q = qdisc_priv(sch);
656 return q->dequeue(sch);
659 static bool should_restart_cycle(const struct sched_gate_list *oper,
660 const struct sched_entry *entry)
662 if (list_is_last(&entry->list, &oper->entries))
665 if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0)
671 static bool should_change_schedules(const struct sched_gate_list *admin,
672 const struct sched_gate_list *oper,
675 ktime_t next_base_time, extension_time;
680 next_base_time = sched_base_time(admin);
682 /* This is the simple case, the close_time would fall after
683 * the next schedule base_time.
685 if (ktime_compare(next_base_time, close_time) <= 0)
688 /* This is the cycle_time_extension case, if the close_time
689 * plus the amount that can be extended would fall after the
690 * next schedule base_time, we can extend the current schedule
693 extension_time = ktime_add_ns(close_time, oper->cycle_time_extension);
695 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
696 * how precisely the extension should be made. So after
697 * conformance testing, this logic may change.
699 if (ktime_compare(next_base_time, extension_time) <= 0)
705 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
707 struct taprio_sched *q = container_of(timer, struct taprio_sched,
709 struct sched_gate_list *oper, *admin;
710 struct sched_entry *entry, *next;
711 struct Qdisc *sch = q->root;
714 spin_lock(&q->current_entry_lock);
715 entry = rcu_dereference_protected(q->current_entry,
716 lockdep_is_held(&q->current_entry_lock));
717 oper = rcu_dereference_protected(q->oper_sched,
718 lockdep_is_held(&q->current_entry_lock));
719 admin = rcu_dereference_protected(q->admin_sched,
720 lockdep_is_held(&q->current_entry_lock));
723 switch_schedules(q, &admin, &oper);
725 /* This can happen in two cases: 1. this is the very first run
726 * of this function (i.e. we weren't running any schedule
727 * previously); 2. The previous schedule just ended. The first
728 * entry of all schedules are pre-calculated during the
729 * schedule initialization.
731 if (unlikely(!entry || entry->close_time == oper->base_time)) {
732 next = list_first_entry(&oper->entries, struct sched_entry,
734 close_time = next->close_time;
738 if (should_restart_cycle(oper, entry)) {
739 next = list_first_entry(&oper->entries, struct sched_entry,
741 oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time,
744 next = list_next_entry(entry, list);
747 close_time = ktime_add_ns(entry->close_time, next->interval);
748 close_time = min_t(ktime_t, close_time, oper->cycle_close_time);
750 if (should_change_schedules(admin, oper, close_time)) {
751 /* Set things so the next time this runs, the new
754 close_time = sched_base_time(admin);
755 switch_schedules(q, &admin, &oper);
758 next->close_time = close_time;
759 taprio_set_budget(q, next);
762 rcu_assign_pointer(q->current_entry, next);
763 spin_unlock(&q->current_entry_lock);
765 hrtimer_set_expires(&q->advance_timer, close_time);
768 __netif_schedule(sch);
771 return HRTIMER_RESTART;
774 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
775 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
776 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
777 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
778 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
781 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
782 [TCA_TAPRIO_ATTR_PRIOMAP] = {
783 .len = sizeof(struct tc_mqprio_qopt)
785 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
786 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
787 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
788 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
789 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 },
790 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
791 [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 },
792 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 },
795 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
796 struct sched_entry *entry,
797 struct netlink_ext_ack *extack)
799 int min_duration = length_to_duration(q, ETH_ZLEN);
802 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
803 entry->command = nla_get_u8(
804 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
806 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
807 entry->gate_mask = nla_get_u32(
808 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
810 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
811 interval = nla_get_u32(
812 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
814 /* The interval should allow at least the minimum ethernet
817 if (interval < min_duration) {
818 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
822 entry->interval = interval;
827 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
828 struct sched_entry *entry, int index,
829 struct netlink_ext_ack *extack)
831 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
834 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
837 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
841 entry->index = index;
843 return fill_sched_entry(q, tb, entry, extack);
846 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
847 struct sched_gate_list *sched,
848 struct netlink_ext_ack *extack)
857 nla_for_each_nested(n, list, rem) {
858 struct sched_entry *entry;
860 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
861 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
865 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
867 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
871 err = parse_sched_entry(q, n, entry, i, extack);
877 list_add_tail(&entry->list, &sched->entries);
881 sched->num_entries = i;
886 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
887 struct sched_gate_list *new,
888 struct netlink_ext_ack *extack)
892 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
893 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
897 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
898 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
900 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
901 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
903 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
904 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
906 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
907 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
912 if (!new->cycle_time) {
913 struct sched_entry *entry;
916 list_for_each_entry(entry, &new->entries, list)
917 cycle = ktime_add_ns(cycle, entry->interval);
920 NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
924 new->cycle_time = cycle;
930 static int taprio_parse_mqprio_opt(struct net_device *dev,
931 struct tc_mqprio_qopt *qopt,
932 struct netlink_ext_ack *extack,
937 if (!qopt && !dev->num_tc) {
938 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
942 /* If num_tc is already set, it means that the user already
943 * configured the mqprio part
948 /* Verify num_tc is not out of max range */
949 if (qopt->num_tc > TC_MAX_QUEUE) {
950 NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
954 /* taprio imposes that traffic classes map 1:n to tx queues */
955 if (qopt->num_tc > dev->num_tx_queues) {
956 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
960 /* Verify priority mapping uses valid tcs */
961 for (i = 0; i <= TC_BITMASK; i++) {
962 if (qopt->prio_tc_map[i] >= qopt->num_tc) {
963 NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
968 for (i = 0; i < qopt->num_tc; i++) {
969 unsigned int last = qopt->offset[i] + qopt->count[i];
971 /* Verify the queue count is in tx range being equal to the
972 * real_num_tx_queues indicates the last queue is in use.
974 if (qopt->offset[i] >= dev->num_tx_queues ||
976 last > dev->real_num_tx_queues) {
977 NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
981 if (TXTIME_ASSIST_IS_ENABLED(taprio_flags))
984 /* Verify that the offset and counts do not overlap */
985 for (j = i + 1; j < qopt->num_tc; j++) {
986 if (last > qopt->offset[j]) {
987 NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
996 static int taprio_get_start_time(struct Qdisc *sch,
997 struct sched_gate_list *sched,
1000 struct taprio_sched *q = qdisc_priv(sch);
1001 ktime_t now, base, cycle;
1004 base = sched_base_time(sched);
1005 now = taprio_get_time(q);
1007 if (ktime_after(base, now)) {
1012 cycle = sched->cycle_time;
1014 /* The qdisc is expected to have at least one sched_entry. Moreover,
1015 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
1016 * something went really wrong. In that case, we should warn about this
1017 * inconsistent state and return error.
1019 if (WARN_ON(!cycle))
1022 /* Schedule the start time for the beginning of the next
1025 n = div64_s64(ktime_sub_ns(now, base), cycle);
1026 *start = ktime_add_ns(base, (n + 1) * cycle);
1030 static void setup_first_close_time(struct taprio_sched *q,
1031 struct sched_gate_list *sched, ktime_t base)
1033 struct sched_entry *first;
1036 first = list_first_entry(&sched->entries,
1037 struct sched_entry, list);
1039 cycle = sched->cycle_time;
1041 /* FIXME: find a better place to do this */
1042 sched->cycle_close_time = ktime_add_ns(base, cycle);
1044 first->close_time = ktime_add_ns(base, first->interval);
1045 taprio_set_budget(q, first);
1046 rcu_assign_pointer(q->current_entry, NULL);
1049 static void taprio_start_sched(struct Qdisc *sch,
1050 ktime_t start, struct sched_gate_list *new)
1052 struct taprio_sched *q = qdisc_priv(sch);
1055 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1058 expires = hrtimer_get_expires(&q->advance_timer);
1060 expires = KTIME_MAX;
1062 /* If the new schedule starts before the next expiration, we
1063 * reprogram it to the earliest one, so we change the admin
1064 * schedule to the operational one at the right time.
1066 start = min_t(ktime_t, start, expires);
1068 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1071 static void taprio_set_picos_per_byte(struct net_device *dev,
1072 struct taprio_sched *q)
1074 struct ethtool_link_ksettings ecmd;
1075 int speed = SPEED_10;
1079 err = __ethtool_get_link_ksettings(dev, &ecmd);
1083 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1084 speed = ecmd.base.speed;
1087 picos_per_byte = (USEC_PER_SEC * 8) / speed;
1089 atomic64_set(&q->picos_per_byte, picos_per_byte);
1090 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1091 dev->name, (long long)atomic64_read(&q->picos_per_byte),
1095 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1098 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1099 struct net_device *qdev;
1100 struct taprio_sched *q;
1105 if (event != NETDEV_UP && event != NETDEV_CHANGE)
1108 spin_lock(&taprio_list_lock);
1109 list_for_each_entry(q, &taprio_list, taprio_list) {
1110 qdev = qdisc_dev(q->root);
1116 spin_unlock(&taprio_list_lock);
1119 taprio_set_picos_per_byte(dev, q);
1124 static void setup_txtime(struct taprio_sched *q,
1125 struct sched_gate_list *sched, ktime_t base)
1127 struct sched_entry *entry;
1130 list_for_each_entry(entry, &sched->entries, list) {
1131 entry->next_txtime = ktime_add_ns(base, interval);
1132 interval += entry->interval;
1136 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1138 struct __tc_taprio_qopt_offload *__offload;
1140 __offload = kzalloc(struct_size(__offload, offload.entries, num_entries),
1145 refcount_set(&__offload->users, 1);
1147 return &__offload->offload;
1150 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1153 struct __tc_taprio_qopt_offload *__offload;
1155 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1158 refcount_inc(&__offload->users);
1162 EXPORT_SYMBOL_GPL(taprio_offload_get);
1164 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1166 struct __tc_taprio_qopt_offload *__offload;
1168 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1171 if (!refcount_dec_and_test(&__offload->users))
1176 EXPORT_SYMBOL_GPL(taprio_offload_free);
1178 /* The function will only serve to keep the pointers to the "oper" and "admin"
1179 * schedules valid in relation to their base times, so when calling dump() the
1180 * users looks at the right schedules.
1181 * When using full offload, the admin configuration is promoted to oper at the
1182 * base_time in the PHC time domain. But because the system time is not
1183 * necessarily in sync with that, we can't just trigger a hrtimer to call
1184 * switch_schedules at the right hardware time.
1185 * At the moment we call this by hand right away from taprio, but in the future
1186 * it will be useful to create a mechanism for drivers to notify taprio of the
1187 * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1188 * This is left as TODO.
1190 static void taprio_offload_config_changed(struct taprio_sched *q)
1192 struct sched_gate_list *oper, *admin;
1194 spin_lock(&q->current_entry_lock);
1196 oper = rcu_dereference_protected(q->oper_sched,
1197 lockdep_is_held(&q->current_entry_lock));
1198 admin = rcu_dereference_protected(q->admin_sched,
1199 lockdep_is_held(&q->current_entry_lock));
1201 switch_schedules(q, &admin, &oper);
1203 spin_unlock(&q->current_entry_lock);
1206 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1208 u32 i, queue_mask = 0;
1210 for (i = 0; i < dev->num_tc; i++) {
1213 if (!(tc_mask & BIT(i)))
1216 offset = dev->tc_to_txq[i].offset;
1217 count = dev->tc_to_txq[i].count;
1219 queue_mask |= GENMASK(offset + count - 1, offset);
1225 static void taprio_sched_to_offload(struct net_device *dev,
1226 struct sched_gate_list *sched,
1227 struct tc_taprio_qopt_offload *offload)
1229 struct sched_entry *entry;
1232 offload->base_time = sched->base_time;
1233 offload->cycle_time = sched->cycle_time;
1234 offload->cycle_time_extension = sched->cycle_time_extension;
1236 list_for_each_entry(entry, &sched->entries, list) {
1237 struct tc_taprio_sched_entry *e = &offload->entries[i];
1239 e->command = entry->command;
1240 e->interval = entry->interval;
1241 e->gate_mask = tc_map_to_queue_mask(dev, entry->gate_mask);
1246 offload->num_entries = i;
1249 static int taprio_enable_offload(struct net_device *dev,
1250 struct taprio_sched *q,
1251 struct sched_gate_list *sched,
1252 struct netlink_ext_ack *extack)
1254 const struct net_device_ops *ops = dev->netdev_ops;
1255 struct tc_taprio_qopt_offload *offload;
1258 if (!ops->ndo_setup_tc) {
1259 NL_SET_ERR_MSG(extack,
1260 "Device does not support taprio offload");
1264 offload = taprio_offload_alloc(sched->num_entries);
1266 NL_SET_ERR_MSG(extack,
1267 "Not enough memory for enabling offload mode");
1270 offload->enable = 1;
1271 taprio_sched_to_offload(dev, sched, offload);
1273 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1275 NL_SET_ERR_MSG(extack,
1276 "Device failed to setup taprio offload");
1281 taprio_offload_free(offload);
1286 static int taprio_disable_offload(struct net_device *dev,
1287 struct taprio_sched *q,
1288 struct netlink_ext_ack *extack)
1290 const struct net_device_ops *ops = dev->netdev_ops;
1291 struct tc_taprio_qopt_offload *offload;
1294 if (!FULL_OFFLOAD_IS_ENABLED(q->flags))
1297 if (!ops->ndo_setup_tc)
1300 offload = taprio_offload_alloc(0);
1302 NL_SET_ERR_MSG(extack,
1303 "Not enough memory to disable offload mode");
1306 offload->enable = 0;
1308 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1310 NL_SET_ERR_MSG(extack,
1311 "Device failed to disable offload");
1316 taprio_offload_free(offload);
1321 /* If full offload is enabled, the only possible clockid is the net device's
1322 * PHC. For that reason, specifying a clockid through netlink is incorrect.
1323 * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1324 * in sync with the specified clockid via a user space daemon such as phc2sys.
1325 * For both software taprio and txtime-assist, the clockid is used for the
1326 * hrtimer that advances the schedule and hence mandatory.
1328 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1329 struct netlink_ext_ack *extack)
1331 struct taprio_sched *q = qdisc_priv(sch);
1332 struct net_device *dev = qdisc_dev(sch);
1335 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1336 const struct ethtool_ops *ops = dev->ethtool_ops;
1337 struct ethtool_ts_info info = {
1338 .cmd = ETHTOOL_GET_TS_INFO,
1342 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1343 NL_SET_ERR_MSG(extack,
1344 "The 'clockid' cannot be specified for full offload");
1348 if (ops && ops->get_ts_info)
1349 err = ops->get_ts_info(dev, &info);
1351 if (err || info.phc_index < 0) {
1352 NL_SET_ERR_MSG(extack,
1353 "Device does not have a PTP clock");
1357 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1358 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1359 enum tk_offsets tk_offset;
1361 /* We only support static clockids and we don't allow
1362 * for it to be modified after the first init.
1365 (q->clockid != -1 && q->clockid != clockid)) {
1366 NL_SET_ERR_MSG(extack,
1367 "Changing the 'clockid' of a running schedule is not supported");
1373 case CLOCK_REALTIME:
1374 tk_offset = TK_OFFS_REAL;
1376 case CLOCK_MONOTONIC:
1377 tk_offset = TK_OFFS_MAX;
1379 case CLOCK_BOOTTIME:
1380 tk_offset = TK_OFFS_BOOT;
1383 tk_offset = TK_OFFS_TAI;
1386 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1390 /* This pairs with READ_ONCE() in taprio_mono_to_any */
1391 WRITE_ONCE(q->tk_offset, tk_offset);
1393 q->clockid = clockid;
1395 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1399 /* Everything went ok, return success. */
1406 static int taprio_mqprio_cmp(const struct net_device *dev,
1407 const struct tc_mqprio_qopt *mqprio)
1411 if (!mqprio || mqprio->num_tc != dev->num_tc)
1414 for (i = 0; i < mqprio->num_tc; i++)
1415 if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1416 dev->tc_to_txq[i].offset != mqprio->offset[i])
1419 for (i = 0; i <= TC_BITMASK; i++)
1420 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1426 /* The semantics of the 'flags' argument in relation to 'change()'
1427 * requests, are interpreted following two rules (which are applied in
1428 * this order): (1) an omitted 'flags' argument is interpreted as
1429 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1432 static int taprio_new_flags(const struct nlattr *attr, u32 old,
1433 struct netlink_ext_ack *extack)
1438 new = nla_get_u32(attr);
1440 if (old != TAPRIO_FLAGS_INVALID && old != new) {
1441 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1445 if (!taprio_flags_valid(new)) {
1446 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1453 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1454 struct netlink_ext_ack *extack)
1456 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1457 struct sched_gate_list *oper, *admin, *new_admin;
1458 struct taprio_sched *q = qdisc_priv(sch);
1459 struct net_device *dev = qdisc_dev(sch);
1460 struct tc_mqprio_qopt *mqprio = NULL;
1461 unsigned long flags;
1465 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1466 taprio_policy, extack);
1470 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1471 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1473 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1480 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1484 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1486 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1489 INIT_LIST_HEAD(&new_admin->entries);
1492 oper = rcu_dereference(q->oper_sched);
1493 admin = rcu_dereference(q->admin_sched);
1496 /* no changes - no new mqprio settings */
1497 if (!taprio_mqprio_cmp(dev, mqprio))
1500 if (mqprio && (oper || admin)) {
1501 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1506 err = parse_taprio_schedule(q, tb, new_admin, extack);
1510 if (new_admin->num_entries == 0) {
1511 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1516 err = taprio_parse_clockid(sch, tb, extack);
1520 taprio_set_picos_per_byte(dev, q);
1523 err = netdev_set_num_tc(dev, mqprio->num_tc);
1526 for (i = 0; i < mqprio->num_tc; i++)
1527 netdev_set_tc_queue(dev, i,
1531 /* Always use supplied priority mappings */
1532 for (i = 0; i <= TC_BITMASK; i++)
1533 netdev_set_prio_tc_map(dev, i,
1534 mqprio->prio_tc_map[i]);
1537 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1538 err = taprio_enable_offload(dev, q, new_admin, extack);
1540 err = taprio_disable_offload(dev, q, extack);
1544 /* Protects against enqueue()/dequeue() */
1545 spin_lock_bh(qdisc_lock(sch));
1547 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1548 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1549 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1554 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1557 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1558 !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1559 !hrtimer_active(&q->advance_timer)) {
1560 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1561 q->advance_timer.function = advance_sched;
1564 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1565 q->dequeue = taprio_dequeue_offload;
1566 q->peek = taprio_peek_offload;
1568 /* Be sure to always keep the function pointers
1569 * in a consistent state.
1571 q->dequeue = taprio_dequeue_soft;
1572 q->peek = taprio_peek_soft;
1575 err = taprio_get_start_time(sch, new_admin, &start);
1577 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1581 setup_txtime(q, new_admin, start);
1583 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1585 rcu_assign_pointer(q->oper_sched, new_admin);
1591 rcu_assign_pointer(q->admin_sched, new_admin);
1593 call_rcu(&admin->rcu, taprio_free_sched_cb);
1595 setup_first_close_time(q, new_admin, start);
1597 /* Protects against advance_sched() */
1598 spin_lock_irqsave(&q->current_entry_lock, flags);
1600 taprio_start_sched(sch, start, new_admin);
1602 rcu_assign_pointer(q->admin_sched, new_admin);
1604 call_rcu(&admin->rcu, taprio_free_sched_cb);
1606 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1608 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1609 taprio_offload_config_changed(q);
1616 spin_unlock_bh(qdisc_lock(sch));
1620 call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1625 static void taprio_reset(struct Qdisc *sch)
1627 struct taprio_sched *q = qdisc_priv(sch);
1628 struct net_device *dev = qdisc_dev(sch);
1631 hrtimer_cancel(&q->advance_timer);
1633 for (i = 0; i < dev->num_tx_queues; i++)
1635 qdisc_reset(q->qdiscs[i]);
1637 sch->qstats.backlog = 0;
1641 static void taprio_destroy(struct Qdisc *sch)
1643 struct taprio_sched *q = qdisc_priv(sch);
1644 struct net_device *dev = qdisc_dev(sch);
1647 spin_lock(&taprio_list_lock);
1648 list_del(&q->taprio_list);
1649 spin_unlock(&taprio_list_lock);
1651 /* Note that taprio_reset() might not be called if an error
1652 * happens in qdisc_create(), after taprio_init() has been called.
1654 hrtimer_cancel(&q->advance_timer);
1656 taprio_disable_offload(dev, q, NULL);
1659 for (i = 0; i < dev->num_tx_queues; i++)
1660 qdisc_put(q->qdiscs[i]);
1666 netdev_reset_tc(dev);
1669 call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb);
1672 call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb);
1675 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1676 struct netlink_ext_ack *extack)
1678 struct taprio_sched *q = qdisc_priv(sch);
1679 struct net_device *dev = qdisc_dev(sch);
1682 spin_lock_init(&q->current_entry_lock);
1684 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1685 q->advance_timer.function = advance_sched;
1687 q->dequeue = taprio_dequeue_soft;
1688 q->peek = taprio_peek_soft;
1692 /* We only support static clockids. Use an invalid value as default
1693 * and get the valid one on taprio_change().
1696 q->flags = TAPRIO_FLAGS_INVALID;
1698 spin_lock(&taprio_list_lock);
1699 list_add(&q->taprio_list, &taprio_list);
1700 spin_unlock(&taprio_list_lock);
1702 if (sch->parent != TC_H_ROOT)
1705 if (!netif_is_multiqueue(dev))
1708 /* pre-allocate qdisc, attachment can't fail */
1709 q->qdiscs = kcalloc(dev->num_tx_queues,
1710 sizeof(q->qdiscs[0]),
1719 for (i = 0; i < dev->num_tx_queues; i++) {
1720 struct netdev_queue *dev_queue;
1721 struct Qdisc *qdisc;
1723 dev_queue = netdev_get_tx_queue(dev, i);
1724 qdisc = qdisc_create_dflt(dev_queue,
1726 TC_H_MAKE(TC_H_MAJ(sch->handle),
1732 if (i < dev->real_num_tx_queues)
1733 qdisc_hash_add(qdisc, false);
1735 q->qdiscs[i] = qdisc;
1738 return taprio_change(sch, opt, extack);
1741 static void taprio_attach(struct Qdisc *sch)
1743 struct taprio_sched *q = qdisc_priv(sch);
1744 struct net_device *dev = qdisc_dev(sch);
1747 /* Attach underlying qdisc */
1748 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
1749 struct Qdisc *qdisc = q->qdiscs[ntx];
1752 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1753 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1754 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
1756 old = dev_graft_qdisc(qdisc->dev_queue, sch);
1757 qdisc_refcount_inc(sch);
1763 /* access to the child qdiscs is not needed in offload mode */
1764 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1770 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1773 struct net_device *dev = qdisc_dev(sch);
1774 unsigned long ntx = cl - 1;
1776 if (ntx >= dev->num_tx_queues)
1779 return netdev_get_tx_queue(dev, ntx);
1782 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1783 struct Qdisc *new, struct Qdisc **old,
1784 struct netlink_ext_ack *extack)
1786 struct taprio_sched *q = qdisc_priv(sch);
1787 struct net_device *dev = qdisc_dev(sch);
1788 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1793 if (dev->flags & IFF_UP)
1794 dev_deactivate(dev);
1796 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1797 *old = dev_graft_qdisc(dev_queue, new);
1799 *old = q->qdiscs[cl - 1];
1800 q->qdiscs[cl - 1] = new;
1804 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1806 if (dev->flags & IFF_UP)
1812 static int dump_entry(struct sk_buff *msg,
1813 const struct sched_entry *entry)
1815 struct nlattr *item;
1817 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1821 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1822 goto nla_put_failure;
1824 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1825 goto nla_put_failure;
1827 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1829 goto nla_put_failure;
1831 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1833 goto nla_put_failure;
1835 return nla_nest_end(msg, item);
1838 nla_nest_cancel(msg, item);
1842 static int dump_schedule(struct sk_buff *msg,
1843 const struct sched_gate_list *root)
1845 struct nlattr *entry_list;
1846 struct sched_entry *entry;
1848 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1849 root->base_time, TCA_TAPRIO_PAD))
1852 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1853 root->cycle_time, TCA_TAPRIO_PAD))
1856 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1857 root->cycle_time_extension, TCA_TAPRIO_PAD))
1860 entry_list = nla_nest_start_noflag(msg,
1861 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1865 list_for_each_entry(entry, &root->entries, list) {
1866 if (dump_entry(msg, entry) < 0)
1870 nla_nest_end(msg, entry_list);
1874 nla_nest_cancel(msg, entry_list);
1878 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1880 struct taprio_sched *q = qdisc_priv(sch);
1881 struct net_device *dev = qdisc_dev(sch);
1882 struct sched_gate_list *oper, *admin;
1883 struct tc_mqprio_qopt opt = { 0 };
1884 struct nlattr *nest, *sched_nest;
1888 oper = rcu_dereference(q->oper_sched);
1889 admin = rcu_dereference(q->admin_sched);
1891 opt.num_tc = netdev_get_num_tc(dev);
1892 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
1894 for (i = 0; i < netdev_get_num_tc(dev); i++) {
1895 opt.count[i] = dev->tc_to_txq[i].count;
1896 opt.offset[i] = dev->tc_to_txq[i].offset;
1899 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1903 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
1906 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1907 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
1910 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
1913 if (q->txtime_delay &&
1914 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
1917 if (oper && dump_schedule(skb, oper))
1923 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
1927 if (dump_schedule(skb, admin))
1930 nla_nest_end(skb, sched_nest);
1935 return nla_nest_end(skb, nest);
1938 nla_nest_cancel(skb, sched_nest);
1941 nla_nest_cancel(skb, nest);
1948 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
1950 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1955 return dev_queue->qdisc_sleeping;
1958 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
1960 unsigned int ntx = TC_H_MIN(classid);
1962 if (!taprio_queue_get(sch, ntx))
1967 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
1968 struct sk_buff *skb, struct tcmsg *tcm)
1970 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1972 tcm->tcm_parent = TC_H_ROOT;
1973 tcm->tcm_handle |= TC_H_MIN(cl);
1974 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
1979 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
1980 struct gnet_dump *d)
1984 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1986 sch = dev_queue->qdisc_sleeping;
1987 if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 ||
1988 qdisc_qstats_copy(d, sch) < 0)
1993 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1995 struct net_device *dev = qdisc_dev(sch);
2001 arg->count = arg->skip;
2002 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
2003 if (arg->fn(sch, ntx + 1, arg) < 0) {
2011 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
2014 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
2017 static const struct Qdisc_class_ops taprio_class_ops = {
2018 .graft = taprio_graft,
2019 .leaf = taprio_leaf,
2020 .find = taprio_find,
2021 .walk = taprio_walk,
2022 .dump = taprio_dump_class,
2023 .dump_stats = taprio_dump_class_stats,
2024 .select_queue = taprio_select_queue,
2027 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
2028 .cl_ops = &taprio_class_ops,
2030 .priv_size = sizeof(struct taprio_sched),
2031 .init = taprio_init,
2032 .change = taprio_change,
2033 .destroy = taprio_destroy,
2034 .reset = taprio_reset,
2035 .attach = taprio_attach,
2036 .peek = taprio_peek,
2037 .dequeue = taprio_dequeue,
2038 .enqueue = taprio_enqueue,
2039 .dump = taprio_dump,
2040 .owner = THIS_MODULE,
2043 static struct notifier_block taprio_device_notifier = {
2044 .notifier_call = taprio_dev_notifier,
2047 static int __init taprio_module_init(void)
2049 int err = register_netdevice_notifier(&taprio_device_notifier);
2054 return register_qdisc(&taprio_qdisc_ops);
2057 static void __exit taprio_module_exit(void)
2059 unregister_qdisc(&taprio_qdisc_ops);
2060 unregister_netdevice_notifier(&taprio_device_notifier);
2063 module_init(taprio_module_init);
2064 module_exit(taprio_module_exit);
2065 MODULE_LICENSE("GPL");