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_get_time(struct taprio_sched *q)
100 ktime_t mono = ktime_get();
102 switch (q->tk_offset) {
106 return ktime_mono_to_any(mono, q->tk_offset);
112 static void taprio_free_sched_cb(struct rcu_head *head)
114 struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
115 struct sched_entry *entry, *n;
117 list_for_each_entry_safe(entry, n, &sched->entries, list) {
118 list_del(&entry->list);
125 static void switch_schedules(struct taprio_sched *q,
126 struct sched_gate_list **admin,
127 struct sched_gate_list **oper)
129 rcu_assign_pointer(q->oper_sched, *admin);
130 rcu_assign_pointer(q->admin_sched, NULL);
133 call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
139 /* Get how much time has been already elapsed in the current cycle. */
140 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
142 ktime_t time_since_sched_start;
145 time_since_sched_start = ktime_sub(time, sched->base_time);
146 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
151 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
152 struct sched_gate_list *admin,
153 struct sched_entry *entry,
156 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
157 ktime_t intv_end, cycle_ext_end, cycle_end;
159 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
160 intv_end = ktime_add_ns(intv_start, entry->interval);
161 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
163 if (ktime_before(intv_end, cycle_end))
165 else if (admin && admin != sched &&
166 ktime_after(admin->base_time, cycle_end) &&
167 ktime_before(admin->base_time, cycle_ext_end))
168 return admin->base_time;
173 static int length_to_duration(struct taprio_sched *q, int len)
175 return div_u64(len * atomic64_read(&q->picos_per_byte), 1000);
178 /* Returns the entry corresponding to next available interval. If
179 * validate_interval is set, it only validates whether the timestamp occurs
180 * when the gate corresponding to the skb's traffic class is open.
182 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
184 struct sched_gate_list *sched,
185 struct sched_gate_list *admin,
187 ktime_t *interval_start,
188 ktime_t *interval_end,
189 bool validate_interval)
191 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
192 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
193 struct sched_entry *entry = NULL, *entry_found = NULL;
194 struct taprio_sched *q = qdisc_priv(sch);
195 struct net_device *dev = qdisc_dev(sch);
196 bool entry_available = false;
200 tc = netdev_get_prio_tc_map(dev, skb->priority);
201 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
209 cycle = sched->cycle_time;
210 cycle_elapsed = get_cycle_time_elapsed(sched, time);
211 curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
212 cycle_end = ktime_add_ns(curr_intv_end, cycle);
214 list_for_each_entry(entry, &sched->entries, list) {
215 curr_intv_start = curr_intv_end;
216 curr_intv_end = get_interval_end_time(sched, admin, entry,
219 if (ktime_after(curr_intv_start, cycle_end))
222 if (!(entry->gate_mask & BIT(tc)) ||
223 packet_transmit_time > entry->interval)
226 txtime = entry->next_txtime;
228 if (ktime_before(txtime, time) || validate_interval) {
229 transmit_end_time = ktime_add_ns(time, packet_transmit_time);
230 if ((ktime_before(curr_intv_start, time) &&
231 ktime_before(transmit_end_time, curr_intv_end)) ||
232 (ktime_after(curr_intv_start, time) && !validate_interval)) {
234 *interval_start = curr_intv_start;
235 *interval_end = curr_intv_end;
237 } else if (!entry_available && !validate_interval) {
238 /* Here, we are just trying to find out the
239 * first available interval in the next cycle.
241 entry_available = true;
243 *interval_start = ktime_add_ns(curr_intv_start, cycle);
244 *interval_end = ktime_add_ns(curr_intv_end, cycle);
246 } else if (ktime_before(txtime, earliest_txtime) &&
248 earliest_txtime = txtime;
250 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
251 *interval_start = ktime_add(curr_intv_start, n * cycle);
252 *interval_end = ktime_add(curr_intv_end, n * cycle);
259 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
261 struct taprio_sched *q = qdisc_priv(sch);
262 struct sched_gate_list *sched, *admin;
263 ktime_t interval_start, interval_end;
264 struct sched_entry *entry;
267 sched = rcu_dereference(q->oper_sched);
268 admin = rcu_dereference(q->admin_sched);
270 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
271 &interval_start, &interval_end, true);
277 static bool taprio_flags_valid(u32 flags)
279 /* Make sure no other flag bits are set. */
280 if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST |
281 TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
283 /* txtime-assist and full offload are mutually exclusive */
284 if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
285 (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
290 /* This returns the tstamp value set by TCP in terms of the set clock. */
291 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
293 unsigned int offset = skb_network_offset(skb);
294 const struct ipv6hdr *ipv6h;
295 const struct iphdr *iph;
296 struct ipv6hdr _ipv6h;
298 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
302 if (ipv6h->version == 4) {
303 iph = (struct iphdr *)ipv6h;
304 offset += iph->ihl * 4;
306 /* special-case 6in4 tunnelling, as that is a common way to get
307 * v6 connectivity in the home
309 if (iph->protocol == IPPROTO_IPV6) {
310 ipv6h = skb_header_pointer(skb, offset,
311 sizeof(_ipv6h), &_ipv6h);
313 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
315 } else if (iph->protocol != IPPROTO_TCP) {
318 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
322 return ktime_mono_to_any(skb->skb_mstamp_ns, q->tk_offset);
325 /* There are a few scenarios where we will have to modify the txtime from
326 * what is read from next_txtime in sched_entry. They are:
327 * 1. If txtime is in the past,
328 * a. The gate for the traffic class is currently open and packet can be
329 * transmitted before it closes, schedule the packet right away.
330 * b. If the gate corresponding to the traffic class is going to open later
331 * in the cycle, set the txtime of packet to the interval start.
332 * 2. If txtime is in the future, there are packets corresponding to the
333 * current traffic class waiting to be transmitted. So, the following
334 * possibilities exist:
335 * a. We can transmit the packet before the window containing the txtime
337 * b. The window might close before the transmission can be completed
338 * successfully. So, schedule the packet in the next open window.
340 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
342 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
343 struct taprio_sched *q = qdisc_priv(sch);
344 struct sched_gate_list *sched, *admin;
345 ktime_t minimum_time, now, txtime;
346 int len, packet_transmit_time;
347 struct sched_entry *entry;
350 now = taprio_get_time(q);
351 minimum_time = ktime_add_ns(now, q->txtime_delay);
353 tcp_tstamp = get_tcp_tstamp(q, skb);
354 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
357 admin = rcu_dereference(q->admin_sched);
358 sched = rcu_dereference(q->oper_sched);
359 if (admin && ktime_after(minimum_time, admin->base_time))
360 switch_schedules(q, &admin, &sched);
362 /* Until the schedule starts, all the queues are open */
363 if (!sched || ktime_before(minimum_time, sched->base_time)) {
364 txtime = minimum_time;
368 len = qdisc_pkt_len(skb);
369 packet_transmit_time = length_to_duration(q, len);
372 sched_changed = false;
374 entry = find_entry_to_transmit(skb, sch, sched, admin,
376 &interval_start, &interval_end,
383 txtime = entry->next_txtime;
384 txtime = max_t(ktime_t, txtime, minimum_time);
385 txtime = max_t(ktime_t, txtime, interval_start);
387 if (admin && admin != sched &&
388 ktime_after(txtime, admin->base_time)) {
390 sched_changed = true;
394 transmit_end_time = ktime_add(txtime, packet_transmit_time);
395 minimum_time = transmit_end_time;
397 /* Update the txtime of current entry to the next time it's
400 if (ktime_after(transmit_end_time, interval_end))
401 entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
402 } while (sched_changed || ktime_after(transmit_end_time, interval_end));
404 entry->next_txtime = transmit_end_time;
411 static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
412 struct Qdisc *child, struct sk_buff **to_free)
414 struct taprio_sched *q = qdisc_priv(sch);
416 if (skb->sk && sock_flag(skb->sk, SOCK_TXTIME)) {
417 if (!is_valid_interval(skb, sch))
418 return qdisc_drop(skb, sch, to_free);
419 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
420 skb->tstamp = get_packet_txtime(skb, sch);
422 return qdisc_drop(skb, sch, to_free);
425 qdisc_qstats_backlog_inc(sch, skb);
428 return qdisc_enqueue(skb, child, to_free);
431 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
432 struct sk_buff **to_free)
434 struct taprio_sched *q = qdisc_priv(sch);
438 if (unlikely(FULL_OFFLOAD_IS_ENABLED(q->flags))) {
439 WARN_ONCE(1, "Trying to enqueue skb into the root of a taprio qdisc configured with full offload\n");
440 return qdisc_drop(skb, sch, to_free);
443 queue = skb_get_queue_mapping(skb);
445 child = q->qdiscs[queue];
446 if (unlikely(!child))
447 return qdisc_drop(skb, sch, to_free);
449 /* Large packets might not be transmitted when the transmission duration
450 * exceeds any configured interval. Therefore, segment the skb into
451 * smaller chunks. Skip it for the full offload case, as the driver
452 * and/or the hardware is expected to handle this.
454 if (skb_is_gso(skb) && !FULL_OFFLOAD_IS_ENABLED(q->flags)) {
455 unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb);
456 netdev_features_t features = netif_skb_features(skb);
457 struct sk_buff *segs, *nskb;
460 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
461 if (IS_ERR_OR_NULL(segs))
462 return qdisc_drop(skb, sch, to_free);
464 skb_list_walk_safe(segs, segs, nskb) {
465 skb_mark_not_on_list(segs);
466 qdisc_skb_cb(segs)->pkt_len = segs->len;
469 ret = taprio_enqueue_one(segs, sch, child, to_free);
470 if (ret != NET_XMIT_SUCCESS) {
471 if (net_xmit_drop_count(ret))
472 qdisc_qstats_drop(sch);
479 qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
482 return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
485 return taprio_enqueue_one(skb, sch, child, to_free);
488 static struct sk_buff *taprio_peek_soft(struct Qdisc *sch)
490 struct taprio_sched *q = qdisc_priv(sch);
491 struct net_device *dev = qdisc_dev(sch);
492 struct sched_entry *entry;
498 entry = rcu_dereference(q->current_entry);
499 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
505 for (i = 0; i < dev->num_tx_queues; i++) {
506 struct Qdisc *child = q->qdiscs[i];
510 if (unlikely(!child))
513 skb = child->ops->peek(child);
517 if (TXTIME_ASSIST_IS_ENABLED(q->flags))
520 prio = skb->priority;
521 tc = netdev_get_prio_tc_map(dev, prio);
523 if (!(gate_mask & BIT(tc)))
532 static struct sk_buff *taprio_peek_offload(struct Qdisc *sch)
534 WARN_ONCE(1, "Trying to peek into the root of a taprio qdisc configured with full offload\n");
539 static struct sk_buff *taprio_peek(struct Qdisc *sch)
541 struct taprio_sched *q = qdisc_priv(sch);
546 static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry)
548 atomic_set(&entry->budget,
549 div64_u64((u64)entry->interval * 1000,
550 atomic64_read(&q->picos_per_byte)));
553 static struct sk_buff *taprio_dequeue_soft(struct Qdisc *sch)
555 struct taprio_sched *q = qdisc_priv(sch);
556 struct net_device *dev = qdisc_dev(sch);
557 struct sk_buff *skb = NULL;
558 struct sched_entry *entry;
563 entry = rcu_dereference(q->current_entry);
564 /* if there's no entry, it means that the schedule didn't
565 * start yet, so force all gates to be open, this is in
566 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
569 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
574 for (i = 0; i < dev->num_tx_queues; i++) {
575 struct Qdisc *child = q->qdiscs[i];
581 if (unlikely(!child))
584 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
585 skb = child->ops->dequeue(child);
591 skb = child->ops->peek(child);
595 prio = skb->priority;
596 tc = netdev_get_prio_tc_map(dev, prio);
598 if (!(gate_mask & BIT(tc))) {
603 len = qdisc_pkt_len(skb);
604 guard = ktime_add_ns(taprio_get_time(q),
605 length_to_duration(q, len));
607 /* In the case that there's no gate entry, there's no
610 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
611 ktime_after(guard, entry->close_time)) {
616 /* ... and no budget. */
617 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
618 atomic_sub_return(len, &entry->budget) < 0) {
623 skb = child->ops->dequeue(child);
628 qdisc_bstats_update(sch, skb);
629 qdisc_qstats_backlog_dec(sch, skb);
641 static struct sk_buff *taprio_dequeue_offload(struct Qdisc *sch)
643 WARN_ONCE(1, "Trying to dequeue from the root of a taprio qdisc configured with full offload\n");
648 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
650 struct taprio_sched *q = qdisc_priv(sch);
652 return q->dequeue(sch);
655 static bool should_restart_cycle(const struct sched_gate_list *oper,
656 const struct sched_entry *entry)
658 if (list_is_last(&entry->list, &oper->entries))
661 if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0)
667 static bool should_change_schedules(const struct sched_gate_list *admin,
668 const struct sched_gate_list *oper,
671 ktime_t next_base_time, extension_time;
676 next_base_time = sched_base_time(admin);
678 /* This is the simple case, the close_time would fall after
679 * the next schedule base_time.
681 if (ktime_compare(next_base_time, close_time) <= 0)
684 /* This is the cycle_time_extension case, if the close_time
685 * plus the amount that can be extended would fall after the
686 * next schedule base_time, we can extend the current schedule
689 extension_time = ktime_add_ns(close_time, oper->cycle_time_extension);
691 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
692 * how precisely the extension should be made. So after
693 * conformance testing, this logic may change.
695 if (ktime_compare(next_base_time, extension_time) <= 0)
701 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
703 struct taprio_sched *q = container_of(timer, struct taprio_sched,
705 struct sched_gate_list *oper, *admin;
706 struct sched_entry *entry, *next;
707 struct Qdisc *sch = q->root;
710 spin_lock(&q->current_entry_lock);
711 entry = rcu_dereference_protected(q->current_entry,
712 lockdep_is_held(&q->current_entry_lock));
713 oper = rcu_dereference_protected(q->oper_sched,
714 lockdep_is_held(&q->current_entry_lock));
715 admin = rcu_dereference_protected(q->admin_sched,
716 lockdep_is_held(&q->current_entry_lock));
719 switch_schedules(q, &admin, &oper);
721 /* This can happen in two cases: 1. this is the very first run
722 * of this function (i.e. we weren't running any schedule
723 * previously); 2. The previous schedule just ended. The first
724 * entry of all schedules are pre-calculated during the
725 * schedule initialization.
727 if (unlikely(!entry || entry->close_time == oper->base_time)) {
728 next = list_first_entry(&oper->entries, struct sched_entry,
730 close_time = next->close_time;
734 if (should_restart_cycle(oper, entry)) {
735 next = list_first_entry(&oper->entries, struct sched_entry,
737 oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time,
740 next = list_next_entry(entry, list);
743 close_time = ktime_add_ns(entry->close_time, next->interval);
744 close_time = min_t(ktime_t, close_time, oper->cycle_close_time);
746 if (should_change_schedules(admin, oper, close_time)) {
747 /* Set things so the next time this runs, the new
750 close_time = sched_base_time(admin);
751 switch_schedules(q, &admin, &oper);
754 next->close_time = close_time;
755 taprio_set_budget(q, next);
758 rcu_assign_pointer(q->current_entry, next);
759 spin_unlock(&q->current_entry_lock);
761 hrtimer_set_expires(&q->advance_timer, close_time);
764 __netif_schedule(sch);
767 return HRTIMER_RESTART;
770 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
771 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
772 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
773 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
774 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
777 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
778 [TCA_TAPRIO_ATTR_PRIOMAP] = {
779 .len = sizeof(struct tc_mqprio_qopt)
781 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
782 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
783 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
784 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
785 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 },
786 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
787 [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 },
788 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 },
791 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
792 struct sched_entry *entry,
793 struct netlink_ext_ack *extack)
795 int min_duration = length_to_duration(q, ETH_ZLEN);
798 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
799 entry->command = nla_get_u8(
800 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
802 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
803 entry->gate_mask = nla_get_u32(
804 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
806 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
807 interval = nla_get_u32(
808 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
810 /* The interval should allow at least the minimum ethernet
813 if (interval < min_duration) {
814 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
818 entry->interval = interval;
823 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
824 struct sched_entry *entry, int index,
825 struct netlink_ext_ack *extack)
827 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
830 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
833 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
837 entry->index = index;
839 return fill_sched_entry(q, tb, entry, extack);
842 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
843 struct sched_gate_list *sched,
844 struct netlink_ext_ack *extack)
853 nla_for_each_nested(n, list, rem) {
854 struct sched_entry *entry;
856 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
857 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
861 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
863 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
867 err = parse_sched_entry(q, n, entry, i, extack);
873 list_add_tail(&entry->list, &sched->entries);
877 sched->num_entries = i;
882 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
883 struct sched_gate_list *new,
884 struct netlink_ext_ack *extack)
888 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
889 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
893 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
894 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
896 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
897 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
899 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
900 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
902 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
903 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
908 if (!new->cycle_time) {
909 struct sched_entry *entry;
912 list_for_each_entry(entry, &new->entries, list)
913 cycle = ktime_add_ns(cycle, entry->interval);
916 NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
920 new->cycle_time = cycle;
926 static int taprio_parse_mqprio_opt(struct net_device *dev,
927 struct tc_mqprio_qopt *qopt,
928 struct netlink_ext_ack *extack,
933 if (!qopt && !dev->num_tc) {
934 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
938 /* If num_tc is already set, it means that the user already
939 * configured the mqprio part
944 /* Verify num_tc is not out of max range */
945 if (qopt->num_tc > TC_MAX_QUEUE) {
946 NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
950 /* taprio imposes that traffic classes map 1:n to tx queues */
951 if (qopt->num_tc > dev->num_tx_queues) {
952 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
956 /* Verify priority mapping uses valid tcs */
957 for (i = 0; i <= TC_BITMASK; i++) {
958 if (qopt->prio_tc_map[i] >= qopt->num_tc) {
959 NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
964 for (i = 0; i < qopt->num_tc; i++) {
965 unsigned int last = qopt->offset[i] + qopt->count[i];
967 /* Verify the queue count is in tx range being equal to the
968 * real_num_tx_queues indicates the last queue is in use.
970 if (qopt->offset[i] >= dev->num_tx_queues ||
972 last > dev->real_num_tx_queues) {
973 NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
977 if (TXTIME_ASSIST_IS_ENABLED(taprio_flags))
980 /* Verify that the offset and counts do not overlap */
981 for (j = i + 1; j < qopt->num_tc; j++) {
982 if (last > qopt->offset[j]) {
983 NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
992 static int taprio_get_start_time(struct Qdisc *sch,
993 struct sched_gate_list *sched,
996 struct taprio_sched *q = qdisc_priv(sch);
997 ktime_t now, base, cycle;
1000 base = sched_base_time(sched);
1001 now = taprio_get_time(q);
1003 if (ktime_after(base, now)) {
1008 cycle = sched->cycle_time;
1010 /* The qdisc is expected to have at least one sched_entry. Moreover,
1011 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
1012 * something went really wrong. In that case, we should warn about this
1013 * inconsistent state and return error.
1015 if (WARN_ON(!cycle))
1018 /* Schedule the start time for the beginning of the next
1021 n = div64_s64(ktime_sub_ns(now, base), cycle);
1022 *start = ktime_add_ns(base, (n + 1) * cycle);
1026 static void setup_first_close_time(struct taprio_sched *q,
1027 struct sched_gate_list *sched, ktime_t base)
1029 struct sched_entry *first;
1032 first = list_first_entry(&sched->entries,
1033 struct sched_entry, list);
1035 cycle = sched->cycle_time;
1037 /* FIXME: find a better place to do this */
1038 sched->cycle_close_time = ktime_add_ns(base, cycle);
1040 first->close_time = ktime_add_ns(base, first->interval);
1041 taprio_set_budget(q, first);
1042 rcu_assign_pointer(q->current_entry, NULL);
1045 static void taprio_start_sched(struct Qdisc *sch,
1046 ktime_t start, struct sched_gate_list *new)
1048 struct taprio_sched *q = qdisc_priv(sch);
1051 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1054 expires = hrtimer_get_expires(&q->advance_timer);
1056 expires = KTIME_MAX;
1058 /* If the new schedule starts before the next expiration, we
1059 * reprogram it to the earliest one, so we change the admin
1060 * schedule to the operational one at the right time.
1062 start = min_t(ktime_t, start, expires);
1064 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1067 static void taprio_set_picos_per_byte(struct net_device *dev,
1068 struct taprio_sched *q)
1070 struct ethtool_link_ksettings ecmd;
1071 int speed = SPEED_10;
1075 err = __ethtool_get_link_ksettings(dev, &ecmd);
1079 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1080 speed = ecmd.base.speed;
1083 picos_per_byte = (USEC_PER_SEC * 8) / speed;
1085 atomic64_set(&q->picos_per_byte, picos_per_byte);
1086 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1087 dev->name, (long long)atomic64_read(&q->picos_per_byte),
1091 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1094 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1095 struct net_device *qdev;
1096 struct taprio_sched *q;
1101 if (event != NETDEV_UP && event != NETDEV_CHANGE)
1104 spin_lock(&taprio_list_lock);
1105 list_for_each_entry(q, &taprio_list, taprio_list) {
1106 qdev = qdisc_dev(q->root);
1112 spin_unlock(&taprio_list_lock);
1115 taprio_set_picos_per_byte(dev, q);
1120 static void setup_txtime(struct taprio_sched *q,
1121 struct sched_gate_list *sched, ktime_t base)
1123 struct sched_entry *entry;
1126 list_for_each_entry(entry, &sched->entries, list) {
1127 entry->next_txtime = ktime_add_ns(base, interval);
1128 interval += entry->interval;
1132 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1134 struct __tc_taprio_qopt_offload *__offload;
1136 __offload = kzalloc(struct_size(__offload, offload.entries, num_entries),
1141 refcount_set(&__offload->users, 1);
1143 return &__offload->offload;
1146 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1149 struct __tc_taprio_qopt_offload *__offload;
1151 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1154 refcount_inc(&__offload->users);
1158 EXPORT_SYMBOL_GPL(taprio_offload_get);
1160 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1162 struct __tc_taprio_qopt_offload *__offload;
1164 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1167 if (!refcount_dec_and_test(&__offload->users))
1172 EXPORT_SYMBOL_GPL(taprio_offload_free);
1174 /* The function will only serve to keep the pointers to the "oper" and "admin"
1175 * schedules valid in relation to their base times, so when calling dump() the
1176 * users looks at the right schedules.
1177 * When using full offload, the admin configuration is promoted to oper at the
1178 * base_time in the PHC time domain. But because the system time is not
1179 * necessarily in sync with that, we can't just trigger a hrtimer to call
1180 * switch_schedules at the right hardware time.
1181 * At the moment we call this by hand right away from taprio, but in the future
1182 * it will be useful to create a mechanism for drivers to notify taprio of the
1183 * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1184 * This is left as TODO.
1186 static void taprio_offload_config_changed(struct taprio_sched *q)
1188 struct sched_gate_list *oper, *admin;
1190 spin_lock(&q->current_entry_lock);
1192 oper = rcu_dereference_protected(q->oper_sched,
1193 lockdep_is_held(&q->current_entry_lock));
1194 admin = rcu_dereference_protected(q->admin_sched,
1195 lockdep_is_held(&q->current_entry_lock));
1197 switch_schedules(q, &admin, &oper);
1199 spin_unlock(&q->current_entry_lock);
1202 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1204 u32 i, queue_mask = 0;
1206 for (i = 0; i < dev->num_tc; i++) {
1209 if (!(tc_mask & BIT(i)))
1212 offset = dev->tc_to_txq[i].offset;
1213 count = dev->tc_to_txq[i].count;
1215 queue_mask |= GENMASK(offset + count - 1, offset);
1221 static void taprio_sched_to_offload(struct net_device *dev,
1222 struct sched_gate_list *sched,
1223 struct tc_taprio_qopt_offload *offload)
1225 struct sched_entry *entry;
1228 offload->base_time = sched->base_time;
1229 offload->cycle_time = sched->cycle_time;
1230 offload->cycle_time_extension = sched->cycle_time_extension;
1232 list_for_each_entry(entry, &sched->entries, list) {
1233 struct tc_taprio_sched_entry *e = &offload->entries[i];
1235 e->command = entry->command;
1236 e->interval = entry->interval;
1237 e->gate_mask = tc_map_to_queue_mask(dev, entry->gate_mask);
1242 offload->num_entries = i;
1245 static int taprio_enable_offload(struct net_device *dev,
1246 struct taprio_sched *q,
1247 struct sched_gate_list *sched,
1248 struct netlink_ext_ack *extack)
1250 const struct net_device_ops *ops = dev->netdev_ops;
1251 struct tc_taprio_qopt_offload *offload;
1254 if (!ops->ndo_setup_tc) {
1255 NL_SET_ERR_MSG(extack,
1256 "Device does not support taprio offload");
1260 offload = taprio_offload_alloc(sched->num_entries);
1262 NL_SET_ERR_MSG(extack,
1263 "Not enough memory for enabling offload mode");
1266 offload->enable = 1;
1267 taprio_sched_to_offload(dev, sched, offload);
1269 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1271 NL_SET_ERR_MSG(extack,
1272 "Device failed to setup taprio offload");
1277 taprio_offload_free(offload);
1282 static int taprio_disable_offload(struct net_device *dev,
1283 struct taprio_sched *q,
1284 struct netlink_ext_ack *extack)
1286 const struct net_device_ops *ops = dev->netdev_ops;
1287 struct tc_taprio_qopt_offload *offload;
1290 if (!FULL_OFFLOAD_IS_ENABLED(q->flags))
1293 if (!ops->ndo_setup_tc)
1296 offload = taprio_offload_alloc(0);
1298 NL_SET_ERR_MSG(extack,
1299 "Not enough memory to disable offload mode");
1302 offload->enable = 0;
1304 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1306 NL_SET_ERR_MSG(extack,
1307 "Device failed to disable offload");
1312 taprio_offload_free(offload);
1317 /* If full offload is enabled, the only possible clockid is the net device's
1318 * PHC. For that reason, specifying a clockid through netlink is incorrect.
1319 * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1320 * in sync with the specified clockid via a user space daemon such as phc2sys.
1321 * For both software taprio and txtime-assist, the clockid is used for the
1322 * hrtimer that advances the schedule and hence mandatory.
1324 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1325 struct netlink_ext_ack *extack)
1327 struct taprio_sched *q = qdisc_priv(sch);
1328 struct net_device *dev = qdisc_dev(sch);
1331 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1332 const struct ethtool_ops *ops = dev->ethtool_ops;
1333 struct ethtool_ts_info info = {
1334 .cmd = ETHTOOL_GET_TS_INFO,
1338 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1339 NL_SET_ERR_MSG(extack,
1340 "The 'clockid' cannot be specified for full offload");
1344 if (ops && ops->get_ts_info)
1345 err = ops->get_ts_info(dev, &info);
1347 if (err || info.phc_index < 0) {
1348 NL_SET_ERR_MSG(extack,
1349 "Device does not have a PTP clock");
1353 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1354 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1356 /* We only support static clockids and we don't allow
1357 * for it to be modified after the first init.
1360 (q->clockid != -1 && q->clockid != clockid)) {
1361 NL_SET_ERR_MSG(extack,
1362 "Changing the 'clockid' of a running schedule is not supported");
1368 case CLOCK_REALTIME:
1369 q->tk_offset = TK_OFFS_REAL;
1371 case CLOCK_MONOTONIC:
1372 q->tk_offset = TK_OFFS_MAX;
1374 case CLOCK_BOOTTIME:
1375 q->tk_offset = TK_OFFS_BOOT;
1378 q->tk_offset = TK_OFFS_TAI;
1381 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1386 q->clockid = clockid;
1388 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1392 /* Everything went ok, return success. */
1399 static int taprio_mqprio_cmp(const struct net_device *dev,
1400 const struct tc_mqprio_qopt *mqprio)
1404 if (!mqprio || mqprio->num_tc != dev->num_tc)
1407 for (i = 0; i < mqprio->num_tc; i++)
1408 if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1409 dev->tc_to_txq[i].offset != mqprio->offset[i])
1412 for (i = 0; i <= TC_BITMASK; i++)
1413 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1419 /* The semantics of the 'flags' argument in relation to 'change()'
1420 * requests, are interpreted following two rules (which are applied in
1421 * this order): (1) an omitted 'flags' argument is interpreted as
1422 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1425 static int taprio_new_flags(const struct nlattr *attr, u32 old,
1426 struct netlink_ext_ack *extack)
1431 new = nla_get_u32(attr);
1433 if (old != TAPRIO_FLAGS_INVALID && old != new) {
1434 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1438 if (!taprio_flags_valid(new)) {
1439 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1446 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1447 struct netlink_ext_ack *extack)
1449 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1450 struct sched_gate_list *oper, *admin, *new_admin;
1451 struct taprio_sched *q = qdisc_priv(sch);
1452 struct net_device *dev = qdisc_dev(sch);
1453 struct tc_mqprio_qopt *mqprio = NULL;
1454 unsigned long flags;
1458 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1459 taprio_policy, extack);
1463 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1464 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1466 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1473 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1477 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1479 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1482 INIT_LIST_HEAD(&new_admin->entries);
1485 oper = rcu_dereference(q->oper_sched);
1486 admin = rcu_dereference(q->admin_sched);
1489 /* no changes - no new mqprio settings */
1490 if (!taprio_mqprio_cmp(dev, mqprio))
1493 if (mqprio && (oper || admin)) {
1494 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1499 err = parse_taprio_schedule(q, tb, new_admin, extack);
1503 if (new_admin->num_entries == 0) {
1504 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1509 err = taprio_parse_clockid(sch, tb, extack);
1513 taprio_set_picos_per_byte(dev, q);
1516 netdev_set_num_tc(dev, mqprio->num_tc);
1517 for (i = 0; i < mqprio->num_tc; i++)
1518 netdev_set_tc_queue(dev, i,
1522 /* Always use supplied priority mappings */
1523 for (i = 0; i <= TC_BITMASK; i++)
1524 netdev_set_prio_tc_map(dev, i,
1525 mqprio->prio_tc_map[i]);
1528 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1529 err = taprio_enable_offload(dev, q, new_admin, extack);
1531 err = taprio_disable_offload(dev, q, extack);
1535 /* Protects against enqueue()/dequeue() */
1536 spin_lock_bh(qdisc_lock(sch));
1538 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1539 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1540 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1545 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1548 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1549 !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1550 !hrtimer_active(&q->advance_timer)) {
1551 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1552 q->advance_timer.function = advance_sched;
1555 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1556 q->dequeue = taprio_dequeue_offload;
1557 q->peek = taprio_peek_offload;
1559 /* Be sure to always keep the function pointers
1560 * in a consistent state.
1562 q->dequeue = taprio_dequeue_soft;
1563 q->peek = taprio_peek_soft;
1566 err = taprio_get_start_time(sch, new_admin, &start);
1568 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1572 setup_txtime(q, new_admin, start);
1574 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1576 rcu_assign_pointer(q->oper_sched, new_admin);
1582 rcu_assign_pointer(q->admin_sched, new_admin);
1584 call_rcu(&admin->rcu, taprio_free_sched_cb);
1586 setup_first_close_time(q, new_admin, start);
1588 /* Protects against advance_sched() */
1589 spin_lock_irqsave(&q->current_entry_lock, flags);
1591 taprio_start_sched(sch, start, new_admin);
1593 rcu_assign_pointer(q->admin_sched, new_admin);
1595 call_rcu(&admin->rcu, taprio_free_sched_cb);
1597 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1599 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1600 taprio_offload_config_changed(q);
1607 spin_unlock_bh(qdisc_lock(sch));
1611 call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1616 static void taprio_reset(struct Qdisc *sch)
1618 struct taprio_sched *q = qdisc_priv(sch);
1619 struct net_device *dev = qdisc_dev(sch);
1622 hrtimer_cancel(&q->advance_timer);
1624 for (i = 0; i < dev->num_tx_queues; i++)
1626 qdisc_reset(q->qdiscs[i]);
1628 sch->qstats.backlog = 0;
1632 static void taprio_destroy(struct Qdisc *sch)
1634 struct taprio_sched *q = qdisc_priv(sch);
1635 struct net_device *dev = qdisc_dev(sch);
1638 spin_lock(&taprio_list_lock);
1639 list_del(&q->taprio_list);
1640 spin_unlock(&taprio_list_lock);
1643 taprio_disable_offload(dev, q, NULL);
1646 for (i = 0; i < dev->num_tx_queues; i++)
1647 qdisc_put(q->qdiscs[i]);
1653 netdev_reset_tc(dev);
1656 call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb);
1659 call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb);
1662 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1663 struct netlink_ext_ack *extack)
1665 struct taprio_sched *q = qdisc_priv(sch);
1666 struct net_device *dev = qdisc_dev(sch);
1669 spin_lock_init(&q->current_entry_lock);
1671 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1672 q->advance_timer.function = advance_sched;
1674 q->dequeue = taprio_dequeue_soft;
1675 q->peek = taprio_peek_soft;
1679 /* We only support static clockids. Use an invalid value as default
1680 * and get the valid one on taprio_change().
1683 q->flags = TAPRIO_FLAGS_INVALID;
1685 spin_lock(&taprio_list_lock);
1686 list_add(&q->taprio_list, &taprio_list);
1687 spin_unlock(&taprio_list_lock);
1689 if (sch->parent != TC_H_ROOT)
1692 if (!netif_is_multiqueue(dev))
1695 /* pre-allocate qdisc, attachment can't fail */
1696 q->qdiscs = kcalloc(dev->num_tx_queues,
1697 sizeof(q->qdiscs[0]),
1706 for (i = 0; i < dev->num_tx_queues; i++) {
1707 struct netdev_queue *dev_queue;
1708 struct Qdisc *qdisc;
1710 dev_queue = netdev_get_tx_queue(dev, i);
1711 qdisc = qdisc_create_dflt(dev_queue,
1713 TC_H_MAKE(TC_H_MAJ(sch->handle),
1719 if (i < dev->real_num_tx_queues)
1720 qdisc_hash_add(qdisc, false);
1722 q->qdiscs[i] = qdisc;
1725 return taprio_change(sch, opt, extack);
1728 static void taprio_attach(struct Qdisc *sch)
1730 struct taprio_sched *q = qdisc_priv(sch);
1731 struct net_device *dev = qdisc_dev(sch);
1734 /* Attach underlying qdisc */
1735 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
1736 struct Qdisc *qdisc = q->qdiscs[ntx];
1739 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1740 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1741 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
1742 if (ntx < dev->real_num_tx_queues)
1743 qdisc_hash_add(qdisc, false);
1745 old = dev_graft_qdisc(qdisc->dev_queue, sch);
1746 qdisc_refcount_inc(sch);
1752 /* access to the child qdiscs is not needed in offload mode */
1753 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1759 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1762 struct net_device *dev = qdisc_dev(sch);
1763 unsigned long ntx = cl - 1;
1765 if (ntx >= dev->num_tx_queues)
1768 return netdev_get_tx_queue(dev, ntx);
1771 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1772 struct Qdisc *new, struct Qdisc **old,
1773 struct netlink_ext_ack *extack)
1775 struct taprio_sched *q = qdisc_priv(sch);
1776 struct net_device *dev = qdisc_dev(sch);
1777 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1782 if (dev->flags & IFF_UP)
1783 dev_deactivate(dev);
1785 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1786 *old = dev_graft_qdisc(dev_queue, new);
1788 *old = q->qdiscs[cl - 1];
1789 q->qdiscs[cl - 1] = new;
1793 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1795 if (dev->flags & IFF_UP)
1801 static int dump_entry(struct sk_buff *msg,
1802 const struct sched_entry *entry)
1804 struct nlattr *item;
1806 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1810 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1811 goto nla_put_failure;
1813 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1814 goto nla_put_failure;
1816 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1818 goto nla_put_failure;
1820 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1822 goto nla_put_failure;
1824 return nla_nest_end(msg, item);
1827 nla_nest_cancel(msg, item);
1831 static int dump_schedule(struct sk_buff *msg,
1832 const struct sched_gate_list *root)
1834 struct nlattr *entry_list;
1835 struct sched_entry *entry;
1837 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1838 root->base_time, TCA_TAPRIO_PAD))
1841 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1842 root->cycle_time, TCA_TAPRIO_PAD))
1845 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1846 root->cycle_time_extension, TCA_TAPRIO_PAD))
1849 entry_list = nla_nest_start_noflag(msg,
1850 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1854 list_for_each_entry(entry, &root->entries, list) {
1855 if (dump_entry(msg, entry) < 0)
1859 nla_nest_end(msg, entry_list);
1863 nla_nest_cancel(msg, entry_list);
1867 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1869 struct taprio_sched *q = qdisc_priv(sch);
1870 struct net_device *dev = qdisc_dev(sch);
1871 struct sched_gate_list *oper, *admin;
1872 struct tc_mqprio_qopt opt = { 0 };
1873 struct nlattr *nest, *sched_nest;
1877 oper = rcu_dereference(q->oper_sched);
1878 admin = rcu_dereference(q->admin_sched);
1880 opt.num_tc = netdev_get_num_tc(dev);
1881 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
1883 for (i = 0; i < netdev_get_num_tc(dev); i++) {
1884 opt.count[i] = dev->tc_to_txq[i].count;
1885 opt.offset[i] = dev->tc_to_txq[i].offset;
1888 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1892 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
1895 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1896 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
1899 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
1902 if (q->txtime_delay &&
1903 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
1906 if (oper && dump_schedule(skb, oper))
1912 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
1916 if (dump_schedule(skb, admin))
1919 nla_nest_end(skb, sched_nest);
1924 return nla_nest_end(skb, nest);
1927 nla_nest_cancel(skb, sched_nest);
1930 nla_nest_cancel(skb, nest);
1937 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
1939 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1944 return dev_queue->qdisc_sleeping;
1947 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
1949 unsigned int ntx = TC_H_MIN(classid);
1951 if (!taprio_queue_get(sch, ntx))
1956 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
1957 struct sk_buff *skb, struct tcmsg *tcm)
1959 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1961 tcm->tcm_parent = TC_H_ROOT;
1962 tcm->tcm_handle |= TC_H_MIN(cl);
1963 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
1968 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
1969 struct gnet_dump *d)
1973 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1975 sch = dev_queue->qdisc_sleeping;
1976 if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 ||
1977 qdisc_qstats_copy(d, sch) < 0)
1982 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1984 struct net_device *dev = qdisc_dev(sch);
1990 arg->count = arg->skip;
1991 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
1992 if (arg->fn(sch, ntx + 1, arg) < 0) {
2000 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
2003 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
2006 static const struct Qdisc_class_ops taprio_class_ops = {
2007 .graft = taprio_graft,
2008 .leaf = taprio_leaf,
2009 .find = taprio_find,
2010 .walk = taprio_walk,
2011 .dump = taprio_dump_class,
2012 .dump_stats = taprio_dump_class_stats,
2013 .select_queue = taprio_select_queue,
2016 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
2017 .cl_ops = &taprio_class_ops,
2019 .priv_size = sizeof(struct taprio_sched),
2020 .init = taprio_init,
2021 .change = taprio_change,
2022 .destroy = taprio_destroy,
2023 .reset = taprio_reset,
2024 .attach = taprio_attach,
2025 .peek = taprio_peek,
2026 .dequeue = taprio_dequeue,
2027 .enqueue = taprio_enqueue,
2028 .dump = taprio_dump,
2029 .owner = THIS_MODULE,
2032 static struct notifier_block taprio_device_notifier = {
2033 .notifier_call = taprio_dev_notifier,
2036 static int __init taprio_module_init(void)
2038 int err = register_netdevice_notifier(&taprio_device_notifier);
2043 return register_qdisc(&taprio_qdisc_ops);
2046 static void __exit taprio_module_exit(void)
2048 unregister_qdisc(&taprio_qdisc_ops);
2049 unregister_netdevice_notifier(&taprio_device_notifier);
2052 module_init(taprio_module_init);
2053 module_exit(taprio_module_exit);
2054 MODULE_LICENSE("GPL");