1 // SPDX-License-Identifier: GPL-2.0
3 /* net/sched/sch_taprio.c Time Aware Priority Scheduler
5 * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com>
9 #include <linux/ethtool.h>
10 #include <linux/types.h>
11 #include <linux/slab.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/list.h>
15 #include <linux/errno.h>
16 #include <linux/skbuff.h>
17 #include <linux/math64.h>
18 #include <linux/module.h>
19 #include <linux/spinlock.h>
20 #include <linux/rcupdate.h>
21 #include <linux/time.h>
22 #include <net/netlink.h>
23 #include <net/pkt_sched.h>
24 #include <net/pkt_cls.h>
25 #include <net/sch_generic.h>
29 static LIST_HEAD(taprio_list);
30 static DEFINE_SPINLOCK(taprio_list_lock);
32 #define TAPRIO_ALL_GATES_OPEN -1
34 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
35 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
36 #define TAPRIO_FLAGS_INVALID U32_MAX
39 struct list_head list;
41 /* The instant that this entry "closes" and the next one
42 * should open, the qdisc will make some effort so that no
43 * packet leaves after this time.
54 struct sched_gate_list {
56 struct list_head entries;
58 ktime_t cycle_close_time;
60 s64 cycle_time_extension;
65 struct Qdisc **qdiscs;
68 enum tk_offsets tk_offset;
70 atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
71 * speeds it's sub-nanoseconds per byte
74 /* Protects the update side of the RCU protected current_entry */
75 spinlock_t current_entry_lock;
76 struct sched_entry __rcu *current_entry;
77 struct sched_gate_list __rcu *oper_sched;
78 struct sched_gate_list __rcu *admin_sched;
79 struct hrtimer advance_timer;
80 struct list_head taprio_list;
84 struct __tc_taprio_qopt_offload {
86 struct tc_taprio_qopt_offload offload;
89 static ktime_t sched_base_time(const struct sched_gate_list *sched)
94 return ns_to_ktime(sched->base_time);
97 static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono)
99 /* This pairs with WRITE_ONCE() in taprio_parse_clockid() */
100 enum tk_offsets tk_offset = READ_ONCE(q->tk_offset);
106 return ktime_mono_to_any(mono, tk_offset);
110 static ktime_t taprio_get_time(const struct taprio_sched *q)
112 return taprio_mono_to_any(q, ktime_get());
115 static void taprio_free_sched_cb(struct rcu_head *head)
117 struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
118 struct sched_entry *entry, *n;
120 list_for_each_entry_safe(entry, n, &sched->entries, list) {
121 list_del(&entry->list);
128 static void switch_schedules(struct taprio_sched *q,
129 struct sched_gate_list **admin,
130 struct sched_gate_list **oper)
132 rcu_assign_pointer(q->oper_sched, *admin);
133 rcu_assign_pointer(q->admin_sched, NULL);
136 call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
142 /* Get how much time has been already elapsed in the current cycle. */
143 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
145 ktime_t time_since_sched_start;
148 time_since_sched_start = ktime_sub(time, sched->base_time);
149 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
154 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
155 struct sched_gate_list *admin,
156 struct sched_entry *entry,
159 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
160 ktime_t intv_end, cycle_ext_end, cycle_end;
162 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
163 intv_end = ktime_add_ns(intv_start, entry->interval);
164 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
166 if (ktime_before(intv_end, cycle_end))
168 else if (admin && admin != sched &&
169 ktime_after(admin->base_time, cycle_end) &&
170 ktime_before(admin->base_time, cycle_ext_end))
171 return admin->base_time;
176 static int length_to_duration(struct taprio_sched *q, int len)
178 return div_u64(len * atomic64_read(&q->picos_per_byte), PSEC_PER_NSEC);
181 /* Returns the entry corresponding to next available interval. If
182 * validate_interval is set, it only validates whether the timestamp occurs
183 * when the gate corresponding to the skb's traffic class is open.
185 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
187 struct sched_gate_list *sched,
188 struct sched_gate_list *admin,
190 ktime_t *interval_start,
191 ktime_t *interval_end,
192 bool validate_interval)
194 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
195 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
196 struct sched_entry *entry = NULL, *entry_found = NULL;
197 struct taprio_sched *q = qdisc_priv(sch);
198 struct net_device *dev = qdisc_dev(sch);
199 bool entry_available = false;
203 tc = netdev_get_prio_tc_map(dev, skb->priority);
204 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
212 cycle = sched->cycle_time;
213 cycle_elapsed = get_cycle_time_elapsed(sched, time);
214 curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
215 cycle_end = ktime_add_ns(curr_intv_end, cycle);
217 list_for_each_entry(entry, &sched->entries, list) {
218 curr_intv_start = curr_intv_end;
219 curr_intv_end = get_interval_end_time(sched, admin, entry,
222 if (ktime_after(curr_intv_start, cycle_end))
225 if (!(entry->gate_mask & BIT(tc)) ||
226 packet_transmit_time > entry->interval)
229 txtime = entry->next_txtime;
231 if (ktime_before(txtime, time) || validate_interval) {
232 transmit_end_time = ktime_add_ns(time, packet_transmit_time);
233 if ((ktime_before(curr_intv_start, time) &&
234 ktime_before(transmit_end_time, curr_intv_end)) ||
235 (ktime_after(curr_intv_start, time) && !validate_interval)) {
237 *interval_start = curr_intv_start;
238 *interval_end = curr_intv_end;
240 } else if (!entry_available && !validate_interval) {
241 /* Here, we are just trying to find out the
242 * first available interval in the next cycle.
244 entry_available = true;
246 *interval_start = ktime_add_ns(curr_intv_start, cycle);
247 *interval_end = ktime_add_ns(curr_intv_end, cycle);
249 } else if (ktime_before(txtime, earliest_txtime) &&
251 earliest_txtime = txtime;
253 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
254 *interval_start = ktime_add(curr_intv_start, n * cycle);
255 *interval_end = ktime_add(curr_intv_end, n * cycle);
262 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
264 struct taprio_sched *q = qdisc_priv(sch);
265 struct sched_gate_list *sched, *admin;
266 ktime_t interval_start, interval_end;
267 struct sched_entry *entry;
270 sched = rcu_dereference(q->oper_sched);
271 admin = rcu_dereference(q->admin_sched);
273 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
274 &interval_start, &interval_end, true);
280 static bool taprio_flags_valid(u32 flags)
282 /* Make sure no other flag bits are set. */
283 if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST |
284 TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
286 /* txtime-assist and full offload are mutually exclusive */
287 if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
288 (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
293 /* This returns the tstamp value set by TCP in terms of the set clock. */
294 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
296 unsigned int offset = skb_network_offset(skb);
297 const struct ipv6hdr *ipv6h;
298 const struct iphdr *iph;
299 struct ipv6hdr _ipv6h;
301 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
305 if (ipv6h->version == 4) {
306 iph = (struct iphdr *)ipv6h;
307 offset += iph->ihl * 4;
309 /* special-case 6in4 tunnelling, as that is a common way to get
310 * v6 connectivity in the home
312 if (iph->protocol == IPPROTO_IPV6) {
313 ipv6h = skb_header_pointer(skb, offset,
314 sizeof(_ipv6h), &_ipv6h);
316 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
318 } else if (iph->protocol != IPPROTO_TCP) {
321 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
325 return taprio_mono_to_any(q, skb->skb_mstamp_ns);
328 /* There are a few scenarios where we will have to modify the txtime from
329 * what is read from next_txtime in sched_entry. They are:
330 * 1. If txtime is in the past,
331 * a. The gate for the traffic class is currently open and packet can be
332 * transmitted before it closes, schedule the packet right away.
333 * b. If the gate corresponding to the traffic class is going to open later
334 * in the cycle, set the txtime of packet to the interval start.
335 * 2. If txtime is in the future, there are packets corresponding to the
336 * current traffic class waiting to be transmitted. So, the following
337 * possibilities exist:
338 * a. We can transmit the packet before the window containing the txtime
340 * b. The window might close before the transmission can be completed
341 * successfully. So, schedule the packet in the next open window.
343 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
345 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
346 struct taprio_sched *q = qdisc_priv(sch);
347 struct sched_gate_list *sched, *admin;
348 ktime_t minimum_time, now, txtime;
349 int len, packet_transmit_time;
350 struct sched_entry *entry;
353 now = taprio_get_time(q);
354 minimum_time = ktime_add_ns(now, q->txtime_delay);
356 tcp_tstamp = get_tcp_tstamp(q, skb);
357 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
360 admin = rcu_dereference(q->admin_sched);
361 sched = rcu_dereference(q->oper_sched);
362 if (admin && ktime_after(minimum_time, admin->base_time))
363 switch_schedules(q, &admin, &sched);
365 /* Until the schedule starts, all the queues are open */
366 if (!sched || ktime_before(minimum_time, sched->base_time)) {
367 txtime = minimum_time;
371 len = qdisc_pkt_len(skb);
372 packet_transmit_time = length_to_duration(q, len);
375 sched_changed = false;
377 entry = find_entry_to_transmit(skb, sch, sched, admin,
379 &interval_start, &interval_end,
386 txtime = entry->next_txtime;
387 txtime = max_t(ktime_t, txtime, minimum_time);
388 txtime = max_t(ktime_t, txtime, interval_start);
390 if (admin && admin != sched &&
391 ktime_after(txtime, admin->base_time)) {
393 sched_changed = true;
397 transmit_end_time = ktime_add(txtime, packet_transmit_time);
398 minimum_time = transmit_end_time;
400 /* Update the txtime of current entry to the next time it's
403 if (ktime_after(transmit_end_time, interval_end))
404 entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
405 } while (sched_changed || ktime_after(transmit_end_time, interval_end));
407 entry->next_txtime = transmit_end_time;
414 static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
415 struct Qdisc *child, struct sk_buff **to_free)
417 struct taprio_sched *q = qdisc_priv(sch);
419 /* sk_flags are only safe to use on full sockets. */
420 if (skb->sk && sk_fullsock(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. Drivers with full offload are expected to handle
458 if (skb_is_gso(skb)) {
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(struct Qdisc *sch)
494 struct taprio_sched *q = qdisc_priv(sch);
495 struct net_device *dev = qdisc_dev(sch);
496 struct sched_entry *entry;
501 if (unlikely(FULL_OFFLOAD_IS_ENABLED(q->flags))) {
502 WARN_ONCE(1, "Trying to peek into the root of a taprio qdisc configured with full offload\n");
507 entry = rcu_dereference(q->current_entry);
508 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
514 for (i = 0; i < dev->num_tx_queues; i++) {
515 struct Qdisc *child = q->qdiscs[i];
519 if (unlikely(!child))
522 skb = child->ops->peek(child);
526 if (TXTIME_ASSIST_IS_ENABLED(q->flags))
529 prio = skb->priority;
530 tc = netdev_get_prio_tc_map(dev, prio);
532 if (!(gate_mask & BIT(tc)))
541 static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry)
543 atomic_set(&entry->budget,
544 div64_u64((u64)entry->interval * PSEC_PER_NSEC,
545 atomic64_read(&q->picos_per_byte)));
548 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
550 struct taprio_sched *q = qdisc_priv(sch);
551 struct net_device *dev = qdisc_dev(sch);
552 struct sk_buff *skb = NULL;
553 struct sched_entry *entry;
557 if (unlikely(FULL_OFFLOAD_IS_ENABLED(q->flags))) {
558 WARN_ONCE(1, "Trying to dequeue from the root of a taprio qdisc configured with full offload\n");
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 bool should_restart_cycle(const struct sched_gate_list *oper,
642 const struct sched_entry *entry)
644 if (list_is_last(&entry->list, &oper->entries))
647 if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0)
653 static bool should_change_schedules(const struct sched_gate_list *admin,
654 const struct sched_gate_list *oper,
657 ktime_t next_base_time, extension_time;
662 next_base_time = sched_base_time(admin);
664 /* This is the simple case, the close_time would fall after
665 * the next schedule base_time.
667 if (ktime_compare(next_base_time, close_time) <= 0)
670 /* This is the cycle_time_extension case, if the close_time
671 * plus the amount that can be extended would fall after the
672 * next schedule base_time, we can extend the current schedule
675 extension_time = ktime_add_ns(close_time, oper->cycle_time_extension);
677 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
678 * how precisely the extension should be made. So after
679 * conformance testing, this logic may change.
681 if (ktime_compare(next_base_time, extension_time) <= 0)
687 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
689 struct taprio_sched *q = container_of(timer, struct taprio_sched,
691 struct sched_gate_list *oper, *admin;
692 struct sched_entry *entry, *next;
693 struct Qdisc *sch = q->root;
696 spin_lock(&q->current_entry_lock);
697 entry = rcu_dereference_protected(q->current_entry,
698 lockdep_is_held(&q->current_entry_lock));
699 oper = rcu_dereference_protected(q->oper_sched,
700 lockdep_is_held(&q->current_entry_lock));
701 admin = rcu_dereference_protected(q->admin_sched,
702 lockdep_is_held(&q->current_entry_lock));
705 switch_schedules(q, &admin, &oper);
707 /* This can happen in two cases: 1. this is the very first run
708 * of this function (i.e. we weren't running any schedule
709 * previously); 2. The previous schedule just ended. The first
710 * entry of all schedules are pre-calculated during the
711 * schedule initialization.
713 if (unlikely(!entry || entry->close_time == oper->base_time)) {
714 next = list_first_entry(&oper->entries, struct sched_entry,
716 close_time = next->close_time;
720 if (should_restart_cycle(oper, entry)) {
721 next = list_first_entry(&oper->entries, struct sched_entry,
723 oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time,
726 next = list_next_entry(entry, list);
729 close_time = ktime_add_ns(entry->close_time, next->interval);
730 close_time = min_t(ktime_t, close_time, oper->cycle_close_time);
732 if (should_change_schedules(admin, oper, close_time)) {
733 /* Set things so the next time this runs, the new
736 close_time = sched_base_time(admin);
737 switch_schedules(q, &admin, &oper);
740 next->close_time = close_time;
741 taprio_set_budget(q, next);
744 rcu_assign_pointer(q->current_entry, next);
745 spin_unlock(&q->current_entry_lock);
747 hrtimer_set_expires(&q->advance_timer, close_time);
750 __netif_schedule(sch);
753 return HRTIMER_RESTART;
756 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
757 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
758 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
759 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
760 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
763 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
764 [TCA_TAPRIO_ATTR_PRIOMAP] = {
765 .len = sizeof(struct tc_mqprio_qopt)
767 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
768 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
769 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
770 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
771 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 },
772 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
773 [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 },
774 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 },
777 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
778 struct sched_entry *entry,
779 struct netlink_ext_ack *extack)
781 int min_duration = length_to_duration(q, ETH_ZLEN);
784 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
785 entry->command = nla_get_u8(
786 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
788 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
789 entry->gate_mask = nla_get_u32(
790 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
792 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
793 interval = nla_get_u32(
794 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
796 /* The interval should allow at least the minimum ethernet
799 if (interval < min_duration) {
800 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
804 entry->interval = interval;
809 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
810 struct sched_entry *entry, int index,
811 struct netlink_ext_ack *extack)
813 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
816 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
819 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
823 entry->index = index;
825 return fill_sched_entry(q, tb, entry, extack);
828 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
829 struct sched_gate_list *sched,
830 struct netlink_ext_ack *extack)
839 nla_for_each_nested(n, list, rem) {
840 struct sched_entry *entry;
842 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
843 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
847 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
849 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
853 err = parse_sched_entry(q, n, entry, i, extack);
859 list_add_tail(&entry->list, &sched->entries);
863 sched->num_entries = i;
868 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
869 struct sched_gate_list *new,
870 struct netlink_ext_ack *extack)
874 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
875 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
879 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
880 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
882 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
883 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
885 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
886 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
888 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
889 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
894 if (!new->cycle_time) {
895 struct sched_entry *entry;
898 list_for_each_entry(entry, &new->entries, list)
899 cycle = ktime_add_ns(cycle, entry->interval);
902 NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
906 new->cycle_time = cycle;
912 static int taprio_parse_mqprio_opt(struct net_device *dev,
913 struct tc_mqprio_qopt *qopt,
914 struct netlink_ext_ack *extack,
919 if (!qopt && !dev->num_tc) {
920 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
924 /* If num_tc is already set, it means that the user already
925 * configured the mqprio part
930 /* Verify num_tc is not out of max range */
931 if (qopt->num_tc > TC_MAX_QUEUE) {
932 NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
936 /* taprio imposes that traffic classes map 1:n to tx queues */
937 if (qopt->num_tc > dev->num_tx_queues) {
938 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
942 /* Verify priority mapping uses valid tcs */
943 for (i = 0; i <= TC_BITMASK; i++) {
944 if (qopt->prio_tc_map[i] >= qopt->num_tc) {
945 NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
950 for (i = 0; i < qopt->num_tc; i++) {
951 unsigned int last = qopt->offset[i] + qopt->count[i];
953 /* Verify the queue count is in tx range being equal to the
954 * real_num_tx_queues indicates the last queue is in use.
956 if (qopt->offset[i] >= dev->num_tx_queues ||
958 last > dev->real_num_tx_queues) {
959 NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
963 if (TXTIME_ASSIST_IS_ENABLED(taprio_flags))
966 /* Verify that the offset and counts do not overlap */
967 for (j = i + 1; j < qopt->num_tc; j++) {
968 if (last > qopt->offset[j]) {
969 NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
978 static int taprio_get_start_time(struct Qdisc *sch,
979 struct sched_gate_list *sched,
982 struct taprio_sched *q = qdisc_priv(sch);
983 ktime_t now, base, cycle;
986 base = sched_base_time(sched);
987 now = taprio_get_time(q);
989 if (ktime_after(base, now)) {
994 cycle = sched->cycle_time;
996 /* The qdisc is expected to have at least one sched_entry. Moreover,
997 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
998 * something went really wrong. In that case, we should warn about this
999 * inconsistent state and return error.
1001 if (WARN_ON(!cycle))
1004 /* Schedule the start time for the beginning of the next
1007 n = div64_s64(ktime_sub_ns(now, base), cycle);
1008 *start = ktime_add_ns(base, (n + 1) * cycle);
1012 static void setup_first_close_time(struct taprio_sched *q,
1013 struct sched_gate_list *sched, ktime_t base)
1015 struct sched_entry *first;
1018 first = list_first_entry(&sched->entries,
1019 struct sched_entry, list);
1021 cycle = sched->cycle_time;
1023 /* FIXME: find a better place to do this */
1024 sched->cycle_close_time = ktime_add_ns(base, cycle);
1026 first->close_time = ktime_add_ns(base, first->interval);
1027 taprio_set_budget(q, first);
1028 rcu_assign_pointer(q->current_entry, NULL);
1031 static void taprio_start_sched(struct Qdisc *sch,
1032 ktime_t start, struct sched_gate_list *new)
1034 struct taprio_sched *q = qdisc_priv(sch);
1037 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1040 expires = hrtimer_get_expires(&q->advance_timer);
1042 expires = KTIME_MAX;
1044 /* If the new schedule starts before the next expiration, we
1045 * reprogram it to the earliest one, so we change the admin
1046 * schedule to the operational one at the right time.
1048 start = min_t(ktime_t, start, expires);
1050 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1053 static void taprio_set_picos_per_byte(struct net_device *dev,
1054 struct taprio_sched *q)
1056 struct ethtool_link_ksettings ecmd;
1057 int speed = SPEED_10;
1061 err = __ethtool_get_link_ksettings(dev, &ecmd);
1065 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1066 speed = ecmd.base.speed;
1069 picos_per_byte = (USEC_PER_SEC * 8) / speed;
1071 atomic64_set(&q->picos_per_byte, picos_per_byte);
1072 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1073 dev->name, (long long)atomic64_read(&q->picos_per_byte),
1077 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1080 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1081 struct net_device *qdev;
1082 struct taprio_sched *q;
1087 if (event != NETDEV_UP && event != NETDEV_CHANGE)
1090 spin_lock(&taprio_list_lock);
1091 list_for_each_entry(q, &taprio_list, taprio_list) {
1092 qdev = qdisc_dev(q->root);
1098 spin_unlock(&taprio_list_lock);
1101 taprio_set_picos_per_byte(dev, q);
1106 static void setup_txtime(struct taprio_sched *q,
1107 struct sched_gate_list *sched, ktime_t base)
1109 struct sched_entry *entry;
1112 list_for_each_entry(entry, &sched->entries, list) {
1113 entry->next_txtime = ktime_add_ns(base, interval);
1114 interval += entry->interval;
1118 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1120 struct __tc_taprio_qopt_offload *__offload;
1122 __offload = kzalloc(struct_size(__offload, offload.entries, num_entries),
1127 refcount_set(&__offload->users, 1);
1129 return &__offload->offload;
1132 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1135 struct __tc_taprio_qopt_offload *__offload;
1137 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1140 refcount_inc(&__offload->users);
1144 EXPORT_SYMBOL_GPL(taprio_offload_get);
1146 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1148 struct __tc_taprio_qopt_offload *__offload;
1150 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1153 if (!refcount_dec_and_test(&__offload->users))
1158 EXPORT_SYMBOL_GPL(taprio_offload_free);
1160 /* The function will only serve to keep the pointers to the "oper" and "admin"
1161 * schedules valid in relation to their base times, so when calling dump() the
1162 * users looks at the right schedules.
1163 * When using full offload, the admin configuration is promoted to oper at the
1164 * base_time in the PHC time domain. But because the system time is not
1165 * necessarily in sync with that, we can't just trigger a hrtimer to call
1166 * switch_schedules at the right hardware time.
1167 * At the moment we call this by hand right away from taprio, but in the future
1168 * it will be useful to create a mechanism for drivers to notify taprio of the
1169 * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1170 * This is left as TODO.
1172 static void taprio_offload_config_changed(struct taprio_sched *q)
1174 struct sched_gate_list *oper, *admin;
1176 oper = rtnl_dereference(q->oper_sched);
1177 admin = rtnl_dereference(q->admin_sched);
1179 switch_schedules(q, &admin, &oper);
1182 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1184 u32 i, queue_mask = 0;
1186 for (i = 0; i < dev->num_tc; i++) {
1189 if (!(tc_mask & BIT(i)))
1192 offset = dev->tc_to_txq[i].offset;
1193 count = dev->tc_to_txq[i].count;
1195 queue_mask |= GENMASK(offset + count - 1, offset);
1201 static void taprio_sched_to_offload(struct net_device *dev,
1202 struct sched_gate_list *sched,
1203 struct tc_taprio_qopt_offload *offload)
1205 struct sched_entry *entry;
1208 offload->base_time = sched->base_time;
1209 offload->cycle_time = sched->cycle_time;
1210 offload->cycle_time_extension = sched->cycle_time_extension;
1212 list_for_each_entry(entry, &sched->entries, list) {
1213 struct tc_taprio_sched_entry *e = &offload->entries[i];
1215 e->command = entry->command;
1216 e->interval = entry->interval;
1217 e->gate_mask = tc_map_to_queue_mask(dev, entry->gate_mask);
1222 offload->num_entries = i;
1225 static int taprio_enable_offload(struct net_device *dev,
1226 struct taprio_sched *q,
1227 struct sched_gate_list *sched,
1228 struct netlink_ext_ack *extack)
1230 const struct net_device_ops *ops = dev->netdev_ops;
1231 struct tc_taprio_qopt_offload *offload;
1234 if (!ops->ndo_setup_tc) {
1235 NL_SET_ERR_MSG(extack,
1236 "Device does not support taprio offload");
1240 offload = taprio_offload_alloc(sched->num_entries);
1242 NL_SET_ERR_MSG(extack,
1243 "Not enough memory for enabling offload mode");
1246 offload->enable = 1;
1247 taprio_sched_to_offload(dev, sched, offload);
1249 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1251 NL_SET_ERR_MSG(extack,
1252 "Device failed to setup taprio offload");
1257 taprio_offload_free(offload);
1262 static int taprio_disable_offload(struct net_device *dev,
1263 struct taprio_sched *q,
1264 struct netlink_ext_ack *extack)
1266 const struct net_device_ops *ops = dev->netdev_ops;
1267 struct tc_taprio_qopt_offload *offload;
1270 if (!FULL_OFFLOAD_IS_ENABLED(q->flags))
1273 if (!ops->ndo_setup_tc)
1276 offload = taprio_offload_alloc(0);
1278 NL_SET_ERR_MSG(extack,
1279 "Not enough memory to disable offload mode");
1282 offload->enable = 0;
1284 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1286 NL_SET_ERR_MSG(extack,
1287 "Device failed to disable offload");
1292 taprio_offload_free(offload);
1297 /* If full offload is enabled, the only possible clockid is the net device's
1298 * PHC. For that reason, specifying a clockid through netlink is incorrect.
1299 * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1300 * in sync with the specified clockid via a user space daemon such as phc2sys.
1301 * For both software taprio and txtime-assist, the clockid is used for the
1302 * hrtimer that advances the schedule and hence mandatory.
1304 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1305 struct netlink_ext_ack *extack)
1307 struct taprio_sched *q = qdisc_priv(sch);
1308 struct net_device *dev = qdisc_dev(sch);
1311 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1312 const struct ethtool_ops *ops = dev->ethtool_ops;
1313 struct ethtool_ts_info info = {
1314 .cmd = ETHTOOL_GET_TS_INFO,
1318 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1319 NL_SET_ERR_MSG(extack,
1320 "The 'clockid' cannot be specified for full offload");
1324 if (ops && ops->get_ts_info)
1325 err = ops->get_ts_info(dev, &info);
1327 if (err || info.phc_index < 0) {
1328 NL_SET_ERR_MSG(extack,
1329 "Device does not have a PTP clock");
1333 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1334 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1335 enum tk_offsets tk_offset;
1337 /* We only support static clockids and we don't allow
1338 * for it to be modified after the first init.
1341 (q->clockid != -1 && q->clockid != clockid)) {
1342 NL_SET_ERR_MSG(extack,
1343 "Changing the 'clockid' of a running schedule is not supported");
1349 case CLOCK_REALTIME:
1350 tk_offset = TK_OFFS_REAL;
1352 case CLOCK_MONOTONIC:
1353 tk_offset = TK_OFFS_MAX;
1355 case CLOCK_BOOTTIME:
1356 tk_offset = TK_OFFS_BOOT;
1359 tk_offset = TK_OFFS_TAI;
1362 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1366 /* This pairs with READ_ONCE() in taprio_mono_to_any */
1367 WRITE_ONCE(q->tk_offset, tk_offset);
1369 q->clockid = clockid;
1371 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1375 /* Everything went ok, return success. */
1382 static int taprio_mqprio_cmp(const struct net_device *dev,
1383 const struct tc_mqprio_qopt *mqprio)
1387 if (!mqprio || mqprio->num_tc != dev->num_tc)
1390 for (i = 0; i < mqprio->num_tc; i++)
1391 if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1392 dev->tc_to_txq[i].offset != mqprio->offset[i])
1395 for (i = 0; i <= TC_BITMASK; i++)
1396 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1402 /* The semantics of the 'flags' argument in relation to 'change()'
1403 * requests, are interpreted following two rules (which are applied in
1404 * this order): (1) an omitted 'flags' argument is interpreted as
1405 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1408 static int taprio_new_flags(const struct nlattr *attr, u32 old,
1409 struct netlink_ext_ack *extack)
1414 new = nla_get_u32(attr);
1416 if (old != TAPRIO_FLAGS_INVALID && old != new) {
1417 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1421 if (!taprio_flags_valid(new)) {
1422 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1429 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1430 struct netlink_ext_ack *extack)
1432 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1433 struct sched_gate_list *oper, *admin, *new_admin;
1434 struct taprio_sched *q = qdisc_priv(sch);
1435 struct net_device *dev = qdisc_dev(sch);
1436 struct tc_mqprio_qopt *mqprio = NULL;
1437 unsigned long flags;
1441 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1442 taprio_policy, extack);
1446 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1447 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1449 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1456 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1460 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1462 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1465 INIT_LIST_HEAD(&new_admin->entries);
1467 oper = rtnl_dereference(q->oper_sched);
1468 admin = rtnl_dereference(q->admin_sched);
1470 /* no changes - no new mqprio settings */
1471 if (!taprio_mqprio_cmp(dev, mqprio))
1474 if (mqprio && (oper || admin)) {
1475 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1480 err = parse_taprio_schedule(q, tb, new_admin, extack);
1484 if (new_admin->num_entries == 0) {
1485 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1490 err = taprio_parse_clockid(sch, tb, extack);
1494 taprio_set_picos_per_byte(dev, q);
1497 err = netdev_set_num_tc(dev, mqprio->num_tc);
1500 for (i = 0; i < mqprio->num_tc; i++)
1501 netdev_set_tc_queue(dev, i,
1505 /* Always use supplied priority mappings */
1506 for (i = 0; i <= TC_BITMASK; i++)
1507 netdev_set_prio_tc_map(dev, i,
1508 mqprio->prio_tc_map[i]);
1511 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1512 err = taprio_enable_offload(dev, q, new_admin, extack);
1514 err = taprio_disable_offload(dev, q, extack);
1518 /* Protects against enqueue()/dequeue() */
1519 spin_lock_bh(qdisc_lock(sch));
1521 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1522 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1523 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1528 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1531 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1532 !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1533 !hrtimer_active(&q->advance_timer)) {
1534 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1535 q->advance_timer.function = advance_sched;
1538 err = taprio_get_start_time(sch, new_admin, &start);
1540 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1544 setup_txtime(q, new_admin, start);
1546 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1548 rcu_assign_pointer(q->oper_sched, new_admin);
1554 rcu_assign_pointer(q->admin_sched, new_admin);
1556 call_rcu(&admin->rcu, taprio_free_sched_cb);
1558 setup_first_close_time(q, new_admin, start);
1560 /* Protects against advance_sched() */
1561 spin_lock_irqsave(&q->current_entry_lock, flags);
1563 taprio_start_sched(sch, start, new_admin);
1565 rcu_assign_pointer(q->admin_sched, new_admin);
1567 call_rcu(&admin->rcu, taprio_free_sched_cb);
1569 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1571 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1572 taprio_offload_config_changed(q);
1579 spin_unlock_bh(qdisc_lock(sch));
1583 call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1588 static void taprio_reset(struct Qdisc *sch)
1590 struct taprio_sched *q = qdisc_priv(sch);
1591 struct net_device *dev = qdisc_dev(sch);
1594 hrtimer_cancel(&q->advance_timer);
1596 for (i = 0; i < dev->num_tx_queues; i++)
1598 qdisc_reset(q->qdiscs[i]);
1602 static void taprio_destroy(struct Qdisc *sch)
1604 struct taprio_sched *q = qdisc_priv(sch);
1605 struct net_device *dev = qdisc_dev(sch);
1606 struct sched_gate_list *oper, *admin;
1609 spin_lock(&taprio_list_lock);
1610 list_del(&q->taprio_list);
1611 spin_unlock(&taprio_list_lock);
1613 /* Note that taprio_reset() might not be called if an error
1614 * happens in qdisc_create(), after taprio_init() has been called.
1616 hrtimer_cancel(&q->advance_timer);
1618 taprio_disable_offload(dev, q, NULL);
1621 for (i = 0; i < dev->num_tx_queues; i++)
1622 qdisc_put(q->qdiscs[i]);
1628 netdev_reset_tc(dev);
1630 oper = rtnl_dereference(q->oper_sched);
1631 admin = rtnl_dereference(q->admin_sched);
1634 call_rcu(&oper->rcu, taprio_free_sched_cb);
1637 call_rcu(&admin->rcu, taprio_free_sched_cb);
1640 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1641 struct netlink_ext_ack *extack)
1643 struct taprio_sched *q = qdisc_priv(sch);
1644 struct net_device *dev = qdisc_dev(sch);
1647 spin_lock_init(&q->current_entry_lock);
1649 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1650 q->advance_timer.function = advance_sched;
1654 /* We only support static clockids. Use an invalid value as default
1655 * and get the valid one on taprio_change().
1658 q->flags = TAPRIO_FLAGS_INVALID;
1660 spin_lock(&taprio_list_lock);
1661 list_add(&q->taprio_list, &taprio_list);
1662 spin_unlock(&taprio_list_lock);
1664 if (sch->parent != TC_H_ROOT)
1667 if (!netif_is_multiqueue(dev))
1670 /* pre-allocate qdisc, attachment can't fail */
1671 q->qdiscs = kcalloc(dev->num_tx_queues,
1672 sizeof(q->qdiscs[0]),
1681 for (i = 0; i < dev->num_tx_queues; i++) {
1682 struct netdev_queue *dev_queue;
1683 struct Qdisc *qdisc;
1685 dev_queue = netdev_get_tx_queue(dev, i);
1686 qdisc = qdisc_create_dflt(dev_queue,
1688 TC_H_MAKE(TC_H_MAJ(sch->handle),
1694 if (i < dev->real_num_tx_queues)
1695 qdisc_hash_add(qdisc, false);
1697 q->qdiscs[i] = qdisc;
1700 return taprio_change(sch, opt, extack);
1703 static void taprio_attach(struct Qdisc *sch)
1705 struct taprio_sched *q = qdisc_priv(sch);
1706 struct net_device *dev = qdisc_dev(sch);
1709 /* Attach underlying qdisc */
1710 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
1711 struct Qdisc *qdisc = q->qdiscs[ntx];
1714 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1715 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1716 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
1718 old = dev_graft_qdisc(qdisc->dev_queue, sch);
1719 qdisc_refcount_inc(sch);
1725 /* access to the child qdiscs is not needed in offload mode */
1726 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1732 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1735 struct net_device *dev = qdisc_dev(sch);
1736 unsigned long ntx = cl - 1;
1738 if (ntx >= dev->num_tx_queues)
1741 return netdev_get_tx_queue(dev, ntx);
1744 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1745 struct Qdisc *new, struct Qdisc **old,
1746 struct netlink_ext_ack *extack)
1748 struct taprio_sched *q = qdisc_priv(sch);
1749 struct net_device *dev = qdisc_dev(sch);
1750 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1755 if (dev->flags & IFF_UP)
1756 dev_deactivate(dev);
1758 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1759 *old = dev_graft_qdisc(dev_queue, new);
1761 *old = q->qdiscs[cl - 1];
1762 q->qdiscs[cl - 1] = new;
1766 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1768 if (dev->flags & IFF_UP)
1774 static int dump_entry(struct sk_buff *msg,
1775 const struct sched_entry *entry)
1777 struct nlattr *item;
1779 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1783 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1784 goto nla_put_failure;
1786 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1787 goto nla_put_failure;
1789 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1791 goto nla_put_failure;
1793 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1795 goto nla_put_failure;
1797 return nla_nest_end(msg, item);
1800 nla_nest_cancel(msg, item);
1804 static int dump_schedule(struct sk_buff *msg,
1805 const struct sched_gate_list *root)
1807 struct nlattr *entry_list;
1808 struct sched_entry *entry;
1810 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1811 root->base_time, TCA_TAPRIO_PAD))
1814 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1815 root->cycle_time, TCA_TAPRIO_PAD))
1818 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1819 root->cycle_time_extension, TCA_TAPRIO_PAD))
1822 entry_list = nla_nest_start_noflag(msg,
1823 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1827 list_for_each_entry(entry, &root->entries, list) {
1828 if (dump_entry(msg, entry) < 0)
1832 nla_nest_end(msg, entry_list);
1836 nla_nest_cancel(msg, entry_list);
1840 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1842 struct taprio_sched *q = qdisc_priv(sch);
1843 struct net_device *dev = qdisc_dev(sch);
1844 struct sched_gate_list *oper, *admin;
1845 struct tc_mqprio_qopt opt = { 0 };
1846 struct nlattr *nest, *sched_nest;
1849 oper = rtnl_dereference(q->oper_sched);
1850 admin = rtnl_dereference(q->admin_sched);
1852 opt.num_tc = netdev_get_num_tc(dev);
1853 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
1855 for (i = 0; i < netdev_get_num_tc(dev); i++) {
1856 opt.count[i] = dev->tc_to_txq[i].count;
1857 opt.offset[i] = dev->tc_to_txq[i].offset;
1860 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1864 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
1867 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1868 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
1871 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
1874 if (q->txtime_delay &&
1875 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
1878 if (oper && dump_schedule(skb, oper))
1884 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
1888 if (dump_schedule(skb, admin))
1891 nla_nest_end(skb, sched_nest);
1894 return nla_nest_end(skb, nest);
1897 nla_nest_cancel(skb, sched_nest);
1900 nla_nest_cancel(skb, nest);
1906 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
1908 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1913 return dev_queue->qdisc_sleeping;
1916 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
1918 unsigned int ntx = TC_H_MIN(classid);
1920 if (!taprio_queue_get(sch, ntx))
1925 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
1926 struct sk_buff *skb, struct tcmsg *tcm)
1928 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1930 tcm->tcm_parent = TC_H_ROOT;
1931 tcm->tcm_handle |= TC_H_MIN(cl);
1932 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
1937 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
1938 struct gnet_dump *d)
1942 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1944 sch = dev_queue->qdisc_sleeping;
1945 if (gnet_stats_copy_basic(d, NULL, &sch->bstats, true) < 0 ||
1946 qdisc_qstats_copy(d, sch) < 0)
1951 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1953 struct net_device *dev = qdisc_dev(sch);
1959 arg->count = arg->skip;
1960 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
1961 if (arg->fn(sch, ntx + 1, arg) < 0) {
1969 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
1972 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
1975 static const struct Qdisc_class_ops taprio_class_ops = {
1976 .graft = taprio_graft,
1977 .leaf = taprio_leaf,
1978 .find = taprio_find,
1979 .walk = taprio_walk,
1980 .dump = taprio_dump_class,
1981 .dump_stats = taprio_dump_class_stats,
1982 .select_queue = taprio_select_queue,
1985 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
1986 .cl_ops = &taprio_class_ops,
1988 .priv_size = sizeof(struct taprio_sched),
1989 .init = taprio_init,
1990 .change = taprio_change,
1991 .destroy = taprio_destroy,
1992 .reset = taprio_reset,
1993 .attach = taprio_attach,
1994 .peek = taprio_peek,
1995 .dequeue = taprio_dequeue,
1996 .enqueue = taprio_enqueue,
1997 .dump = taprio_dump,
1998 .owner = THIS_MODULE,
2001 static struct notifier_block taprio_device_notifier = {
2002 .notifier_call = taprio_dev_notifier,
2005 static int __init taprio_module_init(void)
2007 int err = register_netdevice_notifier(&taprio_device_notifier);
2012 return register_qdisc(&taprio_qdisc_ops);
2015 static void __exit taprio_module_exit(void)
2017 unregister_qdisc(&taprio_qdisc_ops);
2018 unregister_netdevice_notifier(&taprio_device_notifier);
2021 module_init(taprio_module_init);
2022 module_exit(taprio_module_exit);
2023 MODULE_LICENSE("GPL");