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);
31 #define TAPRIO_ALL_GATES_OPEN -1
33 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
34 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
35 #define TAPRIO_FLAGS_INVALID U32_MAX
38 struct list_head list;
40 /* The instant that this entry "closes" and the next one
41 * should open, the qdisc will make some effort so that no
42 * packet leaves after this time.
53 struct sched_gate_list {
55 struct list_head entries;
57 ktime_t cycle_close_time;
59 s64 cycle_time_extension;
64 struct Qdisc **qdiscs;
67 enum tk_offsets tk_offset;
70 atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
71 * speeds it's sub-nanoseconds per byte
74 /* Protects the update side of the RCU protected current_entry */
75 spinlock_t current_entry_lock;
76 struct sched_entry __rcu *current_entry;
77 struct sched_gate_list __rcu *oper_sched;
78 struct sched_gate_list __rcu *admin_sched;
79 struct hrtimer advance_timer;
80 struct list_head taprio_list;
81 u32 max_frm_len[TC_MAX_QUEUE]; /* for the fast path */
82 u32 max_sdu[TC_MAX_QUEUE]; /* for dump and offloading */
86 struct __tc_taprio_qopt_offload {
88 struct tc_taprio_qopt_offload offload;
91 static ktime_t sched_base_time(const struct sched_gate_list *sched)
96 return ns_to_ktime(sched->base_time);
99 static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono)
101 /* This pairs with WRITE_ONCE() in taprio_parse_clockid() */
102 enum tk_offsets tk_offset = READ_ONCE(q->tk_offset);
108 return ktime_mono_to_any(mono, tk_offset);
112 static ktime_t taprio_get_time(const struct taprio_sched *q)
114 return taprio_mono_to_any(q, ktime_get());
117 static void taprio_free_sched_cb(struct rcu_head *head)
119 struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
120 struct sched_entry *entry, *n;
122 list_for_each_entry_safe(entry, n, &sched->entries, list) {
123 list_del(&entry->list);
130 static void switch_schedules(struct taprio_sched *q,
131 struct sched_gate_list **admin,
132 struct sched_gate_list **oper)
134 rcu_assign_pointer(q->oper_sched, *admin);
135 rcu_assign_pointer(q->admin_sched, NULL);
138 call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
144 /* Get how much time has been already elapsed in the current cycle. */
145 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
147 ktime_t time_since_sched_start;
150 time_since_sched_start = ktime_sub(time, sched->base_time);
151 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
156 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
157 struct sched_gate_list *admin,
158 struct sched_entry *entry,
161 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
162 ktime_t intv_end, cycle_ext_end, cycle_end;
164 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
165 intv_end = ktime_add_ns(intv_start, entry->interval);
166 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
168 if (ktime_before(intv_end, cycle_end))
170 else if (admin && admin != sched &&
171 ktime_after(admin->base_time, cycle_end) &&
172 ktime_before(admin->base_time, cycle_ext_end))
173 return admin->base_time;
178 static int length_to_duration(struct taprio_sched *q, int len)
180 return div_u64(len * atomic64_read(&q->picos_per_byte), PSEC_PER_NSEC);
183 /* Returns the entry corresponding to next available interval. If
184 * validate_interval is set, it only validates whether the timestamp occurs
185 * when the gate corresponding to the skb's traffic class is open.
187 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
189 struct sched_gate_list *sched,
190 struct sched_gate_list *admin,
192 ktime_t *interval_start,
193 ktime_t *interval_end,
194 bool validate_interval)
196 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
197 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
198 struct sched_entry *entry = NULL, *entry_found = NULL;
199 struct taprio_sched *q = qdisc_priv(sch);
200 struct net_device *dev = qdisc_dev(sch);
201 bool entry_available = false;
205 tc = netdev_get_prio_tc_map(dev, skb->priority);
206 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
214 cycle = sched->cycle_time;
215 cycle_elapsed = get_cycle_time_elapsed(sched, time);
216 curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
217 cycle_end = ktime_add_ns(curr_intv_end, cycle);
219 list_for_each_entry(entry, &sched->entries, list) {
220 curr_intv_start = curr_intv_end;
221 curr_intv_end = get_interval_end_time(sched, admin, entry,
224 if (ktime_after(curr_intv_start, cycle_end))
227 if (!(entry->gate_mask & BIT(tc)) ||
228 packet_transmit_time > entry->interval)
231 txtime = entry->next_txtime;
233 if (ktime_before(txtime, time) || validate_interval) {
234 transmit_end_time = ktime_add_ns(time, packet_transmit_time);
235 if ((ktime_before(curr_intv_start, time) &&
236 ktime_before(transmit_end_time, curr_intv_end)) ||
237 (ktime_after(curr_intv_start, time) && !validate_interval)) {
239 *interval_start = curr_intv_start;
240 *interval_end = curr_intv_end;
242 } else if (!entry_available && !validate_interval) {
243 /* Here, we are just trying to find out the
244 * first available interval in the next cycle.
246 entry_available = true;
248 *interval_start = ktime_add_ns(curr_intv_start, cycle);
249 *interval_end = ktime_add_ns(curr_intv_end, cycle);
251 } else if (ktime_before(txtime, earliest_txtime) &&
253 earliest_txtime = txtime;
255 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
256 *interval_start = ktime_add(curr_intv_start, n * cycle);
257 *interval_end = ktime_add(curr_intv_end, n * cycle);
264 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
266 struct taprio_sched *q = qdisc_priv(sch);
267 struct sched_gate_list *sched, *admin;
268 ktime_t interval_start, interval_end;
269 struct sched_entry *entry;
272 sched = rcu_dereference(q->oper_sched);
273 admin = rcu_dereference(q->admin_sched);
275 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
276 &interval_start, &interval_end, true);
282 static bool taprio_flags_valid(u32 flags)
284 /* Make sure no other flag bits are set. */
285 if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST |
286 TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
288 /* txtime-assist and full offload are mutually exclusive */
289 if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
290 (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
295 /* This returns the tstamp value set by TCP in terms of the set clock. */
296 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
298 unsigned int offset = skb_network_offset(skb);
299 const struct ipv6hdr *ipv6h;
300 const struct iphdr *iph;
301 struct ipv6hdr _ipv6h;
303 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
307 if (ipv6h->version == 4) {
308 iph = (struct iphdr *)ipv6h;
309 offset += iph->ihl * 4;
311 /* special-case 6in4 tunnelling, as that is a common way to get
312 * v6 connectivity in the home
314 if (iph->protocol == IPPROTO_IPV6) {
315 ipv6h = skb_header_pointer(skb, offset,
316 sizeof(_ipv6h), &_ipv6h);
318 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
320 } else if (iph->protocol != IPPROTO_TCP) {
323 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
327 return taprio_mono_to_any(q, skb->skb_mstamp_ns);
330 /* There are a few scenarios where we will have to modify the txtime from
331 * what is read from next_txtime in sched_entry. They are:
332 * 1. If txtime is in the past,
333 * a. The gate for the traffic class is currently open and packet can be
334 * transmitted before it closes, schedule the packet right away.
335 * b. If the gate corresponding to the traffic class is going to open later
336 * in the cycle, set the txtime of packet to the interval start.
337 * 2. If txtime is in the future, there are packets corresponding to the
338 * current traffic class waiting to be transmitted. So, the following
339 * possibilities exist:
340 * a. We can transmit the packet before the window containing the txtime
342 * b. The window might close before the transmission can be completed
343 * successfully. So, schedule the packet in the next open window.
345 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
347 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
348 struct taprio_sched *q = qdisc_priv(sch);
349 struct sched_gate_list *sched, *admin;
350 ktime_t minimum_time, now, txtime;
351 int len, packet_transmit_time;
352 struct sched_entry *entry;
355 now = taprio_get_time(q);
356 minimum_time = ktime_add_ns(now, q->txtime_delay);
358 tcp_tstamp = get_tcp_tstamp(q, skb);
359 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
362 admin = rcu_dereference(q->admin_sched);
363 sched = rcu_dereference(q->oper_sched);
364 if (admin && ktime_after(minimum_time, admin->base_time))
365 switch_schedules(q, &admin, &sched);
367 /* Until the schedule starts, all the queues are open */
368 if (!sched || ktime_before(minimum_time, sched->base_time)) {
369 txtime = minimum_time;
373 len = qdisc_pkt_len(skb);
374 packet_transmit_time = length_to_duration(q, len);
377 sched_changed = false;
379 entry = find_entry_to_transmit(skb, sch, sched, admin,
381 &interval_start, &interval_end,
388 txtime = entry->next_txtime;
389 txtime = max_t(ktime_t, txtime, minimum_time);
390 txtime = max_t(ktime_t, txtime, interval_start);
392 if (admin && admin != sched &&
393 ktime_after(txtime, admin->base_time)) {
395 sched_changed = true;
399 transmit_end_time = ktime_add(txtime, packet_transmit_time);
400 minimum_time = transmit_end_time;
402 /* Update the txtime of current entry to the next time it's
405 if (ktime_after(transmit_end_time, interval_end))
406 entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
407 } while (sched_changed || ktime_after(transmit_end_time, interval_end));
409 entry->next_txtime = transmit_end_time;
416 static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
417 struct Qdisc *child, struct sk_buff **to_free)
419 struct taprio_sched *q = qdisc_priv(sch);
420 struct net_device *dev = qdisc_dev(sch);
421 int prio = skb->priority;
424 /* sk_flags are only safe to use on full sockets. */
425 if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) {
426 if (!is_valid_interval(skb, sch))
427 return qdisc_drop(skb, sch, to_free);
428 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
429 skb->tstamp = get_packet_txtime(skb, sch);
431 return qdisc_drop(skb, sch, to_free);
434 /* Devices with full offload are expected to honor this in hardware */
435 tc = netdev_get_prio_tc_map(dev, prio);
436 if (skb->len > q->max_frm_len[tc])
437 return qdisc_drop(skb, sch, to_free);
439 qdisc_qstats_backlog_inc(sch, skb);
442 return qdisc_enqueue(skb, child, to_free);
445 /* Will not be called in the full offload case, since the TX queues are
446 * attached to the Qdisc created using qdisc_create_dflt()
448 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
449 struct sk_buff **to_free)
451 struct taprio_sched *q = qdisc_priv(sch);
455 queue = skb_get_queue_mapping(skb);
457 child = q->qdiscs[queue];
458 if (unlikely(!child))
459 return qdisc_drop(skb, sch, to_free);
461 /* Large packets might not be transmitted when the transmission duration
462 * exceeds any configured interval. Therefore, segment the skb into
463 * smaller chunks. Drivers with full offload are expected to handle
466 if (skb_is_gso(skb)) {
467 unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb);
468 netdev_features_t features = netif_skb_features(skb);
469 struct sk_buff *segs, *nskb;
472 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
473 if (IS_ERR_OR_NULL(segs))
474 return qdisc_drop(skb, sch, to_free);
476 skb_list_walk_safe(segs, segs, nskb) {
477 skb_mark_not_on_list(segs);
478 qdisc_skb_cb(segs)->pkt_len = segs->len;
481 ret = taprio_enqueue_one(segs, sch, child, to_free);
482 if (ret != NET_XMIT_SUCCESS) {
483 if (net_xmit_drop_count(ret))
484 qdisc_qstats_drop(sch);
491 qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
494 return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
497 return taprio_enqueue_one(skb, sch, child, to_free);
500 /* Will not be called in the full offload case, since the TX queues are
501 * attached to the Qdisc created using qdisc_create_dflt()
503 static struct sk_buff *taprio_peek(struct Qdisc *sch)
505 struct taprio_sched *q = qdisc_priv(sch);
506 struct net_device *dev = qdisc_dev(sch);
507 struct sched_entry *entry;
513 entry = rcu_dereference(q->current_entry);
514 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
520 for (i = 0; i < dev->num_tx_queues; i++) {
521 struct Qdisc *child = q->qdiscs[i];
525 if (unlikely(!child))
528 skb = child->ops->peek(child);
532 if (TXTIME_ASSIST_IS_ENABLED(q->flags))
535 prio = skb->priority;
536 tc = netdev_get_prio_tc_map(dev, prio);
538 if (!(gate_mask & BIT(tc)))
547 static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry)
549 atomic_set(&entry->budget,
550 div64_u64((u64)entry->interval * PSEC_PER_NSEC,
551 atomic64_read(&q->picos_per_byte)));
554 /* Will not be called in the full offload case, since the TX queues are
555 * attached to the Qdisc created using qdisc_create_dflt()
557 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
559 struct taprio_sched *q = qdisc_priv(sch);
560 struct net_device *dev = qdisc_dev(sch);
561 struct sk_buff *skb = NULL;
562 struct sched_entry *entry;
567 entry = rcu_dereference(q->current_entry);
568 /* if there's no entry, it means that the schedule didn't
569 * start yet, so force all gates to be open, this is in
570 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
573 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
578 for (i = 0; i < dev->num_tx_queues; i++) {
579 struct Qdisc *child = q->qdiscs[i];
585 if (unlikely(!child))
588 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
589 skb = child->ops->dequeue(child);
595 skb = child->ops->peek(child);
599 prio = skb->priority;
600 tc = netdev_get_prio_tc_map(dev, prio);
602 if (!(gate_mask & BIT(tc))) {
607 len = qdisc_pkt_len(skb);
608 guard = ktime_add_ns(taprio_get_time(q),
609 length_to_duration(q, len));
611 /* In the case that there's no gate entry, there's no
614 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
615 ktime_after(guard, entry->close_time)) {
620 /* ... and no budget. */
621 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
622 atomic_sub_return(len, &entry->budget) < 0) {
627 skb = child->ops->dequeue(child);
632 qdisc_bstats_update(sch, skb);
633 qdisc_qstats_backlog_dec(sch, skb);
645 static bool should_restart_cycle(const struct sched_gate_list *oper,
646 const struct sched_entry *entry)
648 if (list_is_last(&entry->list, &oper->entries))
651 if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0)
657 static bool should_change_schedules(const struct sched_gate_list *admin,
658 const struct sched_gate_list *oper,
661 ktime_t next_base_time, extension_time;
666 next_base_time = sched_base_time(admin);
668 /* This is the simple case, the close_time would fall after
669 * the next schedule base_time.
671 if (ktime_compare(next_base_time, close_time) <= 0)
674 /* This is the cycle_time_extension case, if the close_time
675 * plus the amount that can be extended would fall after the
676 * next schedule base_time, we can extend the current schedule
679 extension_time = ktime_add_ns(close_time, oper->cycle_time_extension);
681 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
682 * how precisely the extension should be made. So after
683 * conformance testing, this logic may change.
685 if (ktime_compare(next_base_time, extension_time) <= 0)
691 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
693 struct taprio_sched *q = container_of(timer, struct taprio_sched,
695 struct sched_gate_list *oper, *admin;
696 struct sched_entry *entry, *next;
697 struct Qdisc *sch = q->root;
700 spin_lock(&q->current_entry_lock);
701 entry = rcu_dereference_protected(q->current_entry,
702 lockdep_is_held(&q->current_entry_lock));
703 oper = rcu_dereference_protected(q->oper_sched,
704 lockdep_is_held(&q->current_entry_lock));
705 admin = rcu_dereference_protected(q->admin_sched,
706 lockdep_is_held(&q->current_entry_lock));
709 switch_schedules(q, &admin, &oper);
711 /* This can happen in two cases: 1. this is the very first run
712 * of this function (i.e. we weren't running any schedule
713 * previously); 2. The previous schedule just ended. The first
714 * entry of all schedules are pre-calculated during the
715 * schedule initialization.
717 if (unlikely(!entry || entry->close_time == oper->base_time)) {
718 next = list_first_entry(&oper->entries, struct sched_entry,
720 close_time = next->close_time;
724 if (should_restart_cycle(oper, entry)) {
725 next = list_first_entry(&oper->entries, struct sched_entry,
727 oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time,
730 next = list_next_entry(entry, list);
733 close_time = ktime_add_ns(entry->close_time, next->interval);
734 close_time = min_t(ktime_t, close_time, oper->cycle_close_time);
736 if (should_change_schedules(admin, oper, close_time)) {
737 /* Set things so the next time this runs, the new
740 close_time = sched_base_time(admin);
741 switch_schedules(q, &admin, &oper);
744 next->close_time = close_time;
745 taprio_set_budget(q, next);
748 rcu_assign_pointer(q->current_entry, next);
749 spin_unlock(&q->current_entry_lock);
751 hrtimer_set_expires(&q->advance_timer, close_time);
754 __netif_schedule(sch);
757 return HRTIMER_RESTART;
760 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
761 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
762 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
763 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
764 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
767 static const struct nla_policy taprio_tc_policy[TCA_TAPRIO_TC_ENTRY_MAX + 1] = {
768 [TCA_TAPRIO_TC_ENTRY_INDEX] = NLA_POLICY_MAX(NLA_U32,
770 [TCA_TAPRIO_TC_ENTRY_MAX_SDU] = { .type = NLA_U32 },
773 static struct netlink_range_validation_signed taprio_cycle_time_range = {
778 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
779 [TCA_TAPRIO_ATTR_PRIOMAP] = {
780 .len = sizeof(struct tc_mqprio_qopt)
782 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
783 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
784 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
785 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
786 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] =
787 NLA_POLICY_FULL_RANGE_SIGNED(NLA_S64, &taprio_cycle_time_range),
788 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
789 [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 },
790 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 },
791 [TCA_TAPRIO_ATTR_TC_ENTRY] = { .type = NLA_NESTED },
794 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
795 struct sched_entry *entry,
796 struct netlink_ext_ack *extack)
798 int min_duration = length_to_duration(q, ETH_ZLEN);
801 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
802 entry->command = nla_get_u8(
803 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
805 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
806 entry->gate_mask = nla_get_u32(
807 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
809 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
810 interval = nla_get_u32(
811 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
813 /* The interval should allow at least the minimum ethernet
816 if (interval < min_duration) {
817 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
821 entry->interval = interval;
826 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
827 struct sched_entry *entry, int index,
828 struct netlink_ext_ack *extack)
830 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
833 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
836 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
840 entry->index = index;
842 return fill_sched_entry(q, tb, entry, extack);
845 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
846 struct sched_gate_list *sched,
847 struct netlink_ext_ack *extack)
856 nla_for_each_nested(n, list, rem) {
857 struct sched_entry *entry;
859 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
860 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
864 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
866 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
870 err = parse_sched_entry(q, n, entry, i, extack);
876 list_add_tail(&entry->list, &sched->entries);
880 sched->num_entries = i;
885 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
886 struct sched_gate_list *new,
887 struct netlink_ext_ack *extack)
891 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
892 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
896 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
897 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
899 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
900 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
902 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
903 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
905 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
906 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
911 if (!new->cycle_time) {
912 struct sched_entry *entry;
915 list_for_each_entry(entry, &new->entries, list)
916 cycle = ktime_add_ns(cycle, entry->interval);
919 NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
923 if (cycle < 0 || cycle > INT_MAX) {
924 NL_SET_ERR_MSG(extack, "'cycle_time' is too big");
928 new->cycle_time = cycle;
934 static int taprio_parse_mqprio_opt(struct net_device *dev,
935 struct tc_mqprio_qopt *qopt,
936 struct netlink_ext_ack *extack,
941 if (!qopt && !dev->num_tc) {
942 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
946 /* If num_tc is already set, it means that the user already
947 * configured the mqprio part
952 /* Verify num_tc is not out of max range */
953 if (qopt->num_tc > TC_MAX_QUEUE) {
954 NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
958 /* taprio imposes that traffic classes map 1:n to tx queues */
959 if (qopt->num_tc > dev->num_tx_queues) {
960 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
964 /* Verify priority mapping uses valid tcs */
965 for (i = 0; i <= TC_BITMASK; i++) {
966 if (qopt->prio_tc_map[i] >= qopt->num_tc) {
967 NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
972 for (i = 0; i < qopt->num_tc; i++) {
973 unsigned int last = qopt->offset[i] + qopt->count[i];
975 /* Verify the queue count is in tx range being equal to the
976 * real_num_tx_queues indicates the last queue is in use.
978 if (qopt->offset[i] >= dev->num_tx_queues ||
980 last > dev->real_num_tx_queues) {
981 NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
985 if (TXTIME_ASSIST_IS_ENABLED(taprio_flags))
988 /* Verify that the offset and counts do not overlap */
989 for (j = i + 1; j < qopt->num_tc; j++) {
990 if (last > qopt->offset[j]) {
991 NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
1000 static int taprio_get_start_time(struct Qdisc *sch,
1001 struct sched_gate_list *sched,
1004 struct taprio_sched *q = qdisc_priv(sch);
1005 ktime_t now, base, cycle;
1008 base = sched_base_time(sched);
1009 now = taprio_get_time(q);
1011 if (ktime_after(base, now)) {
1016 cycle = sched->cycle_time;
1018 /* The qdisc is expected to have at least one sched_entry. Moreover,
1019 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
1020 * something went really wrong. In that case, we should warn about this
1021 * inconsistent state and return error.
1023 if (WARN_ON(!cycle))
1026 /* Schedule the start time for the beginning of the next
1029 n = div64_s64(ktime_sub_ns(now, base), cycle);
1030 *start = ktime_add_ns(base, (n + 1) * cycle);
1034 static void setup_first_close_time(struct taprio_sched *q,
1035 struct sched_gate_list *sched, ktime_t base)
1037 struct sched_entry *first;
1040 first = list_first_entry(&sched->entries,
1041 struct sched_entry, list);
1043 cycle = sched->cycle_time;
1045 /* FIXME: find a better place to do this */
1046 sched->cycle_close_time = ktime_add_ns(base, cycle);
1048 first->close_time = ktime_add_ns(base, first->interval);
1049 taprio_set_budget(q, first);
1050 rcu_assign_pointer(q->current_entry, NULL);
1053 static void taprio_start_sched(struct Qdisc *sch,
1054 ktime_t start, struct sched_gate_list *new)
1056 struct taprio_sched *q = qdisc_priv(sch);
1059 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1062 expires = hrtimer_get_expires(&q->advance_timer);
1064 expires = KTIME_MAX;
1066 /* If the new schedule starts before the next expiration, we
1067 * reprogram it to the earliest one, so we change the admin
1068 * schedule to the operational one at the right time.
1070 start = min_t(ktime_t, start, expires);
1072 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1075 static void taprio_set_picos_per_byte(struct net_device *dev,
1076 struct taprio_sched *q)
1078 struct ethtool_link_ksettings ecmd;
1079 int speed = SPEED_10;
1083 err = __ethtool_get_link_ksettings(dev, &ecmd);
1087 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1088 speed = ecmd.base.speed;
1091 picos_per_byte = (USEC_PER_SEC * 8) / speed;
1093 atomic64_set(&q->picos_per_byte, picos_per_byte);
1094 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1095 dev->name, (long long)atomic64_read(&q->picos_per_byte),
1099 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1102 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1103 struct taprio_sched *q;
1107 if (event != NETDEV_UP && event != NETDEV_CHANGE)
1110 list_for_each_entry(q, &taprio_list, taprio_list) {
1111 if (dev != qdisc_dev(q->root))
1114 taprio_set_picos_per_byte(dev, q);
1121 static void setup_txtime(struct taprio_sched *q,
1122 struct sched_gate_list *sched, ktime_t base)
1124 struct sched_entry *entry;
1127 list_for_each_entry(entry, &sched->entries, list) {
1128 entry->next_txtime = ktime_add_ns(base, interval);
1129 interval += entry->interval;
1133 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1135 struct __tc_taprio_qopt_offload *__offload;
1137 __offload = kzalloc(struct_size(__offload, offload.entries, num_entries),
1142 refcount_set(&__offload->users, 1);
1144 return &__offload->offload;
1147 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1150 struct __tc_taprio_qopt_offload *__offload;
1152 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1155 refcount_inc(&__offload->users);
1159 EXPORT_SYMBOL_GPL(taprio_offload_get);
1161 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1163 struct __tc_taprio_qopt_offload *__offload;
1165 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1168 if (!refcount_dec_and_test(&__offload->users))
1173 EXPORT_SYMBOL_GPL(taprio_offload_free);
1175 /* The function will only serve to keep the pointers to the "oper" and "admin"
1176 * schedules valid in relation to their base times, so when calling dump() the
1177 * users looks at the right schedules.
1178 * When using full offload, the admin configuration is promoted to oper at the
1179 * base_time in the PHC time domain. But because the system time is not
1180 * necessarily in sync with that, we can't just trigger a hrtimer to call
1181 * switch_schedules at the right hardware time.
1182 * At the moment we call this by hand right away from taprio, but in the future
1183 * it will be useful to create a mechanism for drivers to notify taprio of the
1184 * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1185 * This is left as TODO.
1187 static void taprio_offload_config_changed(struct taprio_sched *q)
1189 struct sched_gate_list *oper, *admin;
1191 oper = rtnl_dereference(q->oper_sched);
1192 admin = rtnl_dereference(q->admin_sched);
1194 switch_schedules(q, &admin, &oper);
1197 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1199 u32 i, queue_mask = 0;
1201 for (i = 0; i < dev->num_tc; i++) {
1204 if (!(tc_mask & BIT(i)))
1207 offset = dev->tc_to_txq[i].offset;
1208 count = dev->tc_to_txq[i].count;
1210 queue_mask |= GENMASK(offset + count - 1, offset);
1216 static void taprio_sched_to_offload(struct net_device *dev,
1217 struct sched_gate_list *sched,
1218 struct tc_taprio_qopt_offload *offload)
1220 struct sched_entry *entry;
1223 offload->base_time = sched->base_time;
1224 offload->cycle_time = sched->cycle_time;
1225 offload->cycle_time_extension = sched->cycle_time_extension;
1227 list_for_each_entry(entry, &sched->entries, list) {
1228 struct tc_taprio_sched_entry *e = &offload->entries[i];
1230 e->command = entry->command;
1231 e->interval = entry->interval;
1232 e->gate_mask = tc_map_to_queue_mask(dev, entry->gate_mask);
1237 offload->num_entries = i;
1240 static int taprio_enable_offload(struct net_device *dev,
1241 struct taprio_sched *q,
1242 struct sched_gate_list *sched,
1243 struct netlink_ext_ack *extack)
1245 const struct net_device_ops *ops = dev->netdev_ops;
1246 struct tc_taprio_qopt_offload *offload;
1247 struct tc_taprio_caps caps;
1250 if (!ops->ndo_setup_tc) {
1251 NL_SET_ERR_MSG(extack,
1252 "Device does not support taprio offload");
1256 qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO,
1257 &caps, sizeof(caps));
1259 if (!caps.supports_queue_max_sdu) {
1260 for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
1261 if (q->max_sdu[tc]) {
1262 NL_SET_ERR_MSG_MOD(extack,
1263 "Device does not handle queueMaxSDU");
1269 offload = taprio_offload_alloc(sched->num_entries);
1271 NL_SET_ERR_MSG(extack,
1272 "Not enough memory for enabling offload mode");
1275 offload->enable = 1;
1276 taprio_sched_to_offload(dev, sched, offload);
1278 for (tc = 0; tc < TC_MAX_QUEUE; tc++)
1279 offload->max_sdu[tc] = q->max_sdu[tc];
1281 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1283 NL_SET_ERR_MSG(extack,
1284 "Device failed to setup taprio offload");
1288 q->offloaded = true;
1291 taprio_offload_free(offload);
1296 static int taprio_disable_offload(struct net_device *dev,
1297 struct taprio_sched *q,
1298 struct netlink_ext_ack *extack)
1300 const struct net_device_ops *ops = dev->netdev_ops;
1301 struct tc_taprio_qopt_offload *offload;
1307 offload = taprio_offload_alloc(0);
1309 NL_SET_ERR_MSG(extack,
1310 "Not enough memory to disable offload mode");
1313 offload->enable = 0;
1315 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1317 NL_SET_ERR_MSG(extack,
1318 "Device failed to disable offload");
1322 q->offloaded = false;
1325 taprio_offload_free(offload);
1330 /* If full offload is enabled, the only possible clockid is the net device's
1331 * PHC. For that reason, specifying a clockid through netlink is incorrect.
1332 * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1333 * in sync with the specified clockid via a user space daemon such as phc2sys.
1334 * For both software taprio and txtime-assist, the clockid is used for the
1335 * hrtimer that advances the schedule and hence mandatory.
1337 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1338 struct netlink_ext_ack *extack)
1340 struct taprio_sched *q = qdisc_priv(sch);
1341 struct net_device *dev = qdisc_dev(sch);
1344 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1345 const struct ethtool_ops *ops = dev->ethtool_ops;
1346 struct ethtool_ts_info info = {
1347 .cmd = ETHTOOL_GET_TS_INFO,
1351 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1352 NL_SET_ERR_MSG(extack,
1353 "The 'clockid' cannot be specified for full offload");
1357 if (ops && ops->get_ts_info)
1358 err = ops->get_ts_info(dev, &info);
1360 if (err || info.phc_index < 0) {
1361 NL_SET_ERR_MSG(extack,
1362 "Device does not have a PTP clock");
1366 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1367 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1368 enum tk_offsets tk_offset;
1370 /* We only support static clockids and we don't allow
1371 * for it to be modified after the first init.
1374 (q->clockid != -1 && q->clockid != clockid)) {
1375 NL_SET_ERR_MSG(extack,
1376 "Changing the 'clockid' of a running schedule is not supported");
1382 case CLOCK_REALTIME:
1383 tk_offset = TK_OFFS_REAL;
1385 case CLOCK_MONOTONIC:
1386 tk_offset = TK_OFFS_MAX;
1388 case CLOCK_BOOTTIME:
1389 tk_offset = TK_OFFS_BOOT;
1392 tk_offset = TK_OFFS_TAI;
1395 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1399 /* This pairs with READ_ONCE() in taprio_mono_to_any */
1400 WRITE_ONCE(q->tk_offset, tk_offset);
1402 q->clockid = clockid;
1404 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1408 /* Everything went ok, return success. */
1415 static int taprio_parse_tc_entry(struct Qdisc *sch,
1417 u32 max_sdu[TC_QOPT_MAX_QUEUE],
1418 unsigned long *seen_tcs,
1419 struct netlink_ext_ack *extack)
1421 struct nlattr *tb[TCA_TAPRIO_TC_ENTRY_MAX + 1] = { };
1422 struct net_device *dev = qdisc_dev(sch);
1426 err = nla_parse_nested(tb, TCA_TAPRIO_TC_ENTRY_MAX, opt,
1427 taprio_tc_policy, extack);
1431 if (!tb[TCA_TAPRIO_TC_ENTRY_INDEX]) {
1432 NL_SET_ERR_MSG_MOD(extack, "TC entry index missing");
1436 tc = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_INDEX]);
1437 if (tc >= TC_QOPT_MAX_QUEUE) {
1438 NL_SET_ERR_MSG_MOD(extack, "TC entry index out of range");
1442 if (*seen_tcs & BIT(tc)) {
1443 NL_SET_ERR_MSG_MOD(extack, "Duplicate TC entry");
1447 *seen_tcs |= BIT(tc);
1449 if (tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU])
1450 val = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]);
1452 if (val > dev->max_mtu) {
1453 NL_SET_ERR_MSG_MOD(extack, "TC max SDU exceeds device max MTU");
1462 static int taprio_parse_tc_entries(struct Qdisc *sch,
1464 struct netlink_ext_ack *extack)
1466 struct taprio_sched *q = qdisc_priv(sch);
1467 struct net_device *dev = qdisc_dev(sch);
1468 u32 max_sdu[TC_QOPT_MAX_QUEUE];
1469 unsigned long seen_tcs = 0;
1474 for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++)
1475 max_sdu[tc] = q->max_sdu[tc];
1477 nla_for_each_nested(n, opt, rem) {
1478 if (nla_type(n) != TCA_TAPRIO_ATTR_TC_ENTRY)
1481 err = taprio_parse_tc_entry(sch, n, max_sdu, &seen_tcs, extack);
1486 for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) {
1487 q->max_sdu[tc] = max_sdu[tc];
1489 q->max_frm_len[tc] = max_sdu[tc] + dev->hard_header_len;
1491 q->max_frm_len[tc] = U32_MAX; /* never oversized */
1498 static int taprio_mqprio_cmp(const struct net_device *dev,
1499 const struct tc_mqprio_qopt *mqprio)
1503 if (!mqprio || mqprio->num_tc != dev->num_tc)
1506 for (i = 0; i < mqprio->num_tc; i++)
1507 if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1508 dev->tc_to_txq[i].offset != mqprio->offset[i])
1511 for (i = 0; i <= TC_BITMASK; i++)
1512 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1518 /* The semantics of the 'flags' argument in relation to 'change()'
1519 * requests, are interpreted following two rules (which are applied in
1520 * this order): (1) an omitted 'flags' argument is interpreted as
1521 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1524 static int taprio_new_flags(const struct nlattr *attr, u32 old,
1525 struct netlink_ext_ack *extack)
1530 new = nla_get_u32(attr);
1532 if (old != TAPRIO_FLAGS_INVALID && old != new) {
1533 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1537 if (!taprio_flags_valid(new)) {
1538 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1545 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1546 struct netlink_ext_ack *extack)
1548 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1549 struct sched_gate_list *oper, *admin, *new_admin;
1550 struct taprio_sched *q = qdisc_priv(sch);
1551 struct net_device *dev = qdisc_dev(sch);
1552 struct tc_mqprio_qopt *mqprio = NULL;
1553 unsigned long flags;
1557 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1558 taprio_policy, extack);
1562 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1563 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1565 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1572 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1576 err = taprio_parse_tc_entries(sch, opt, extack);
1580 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1582 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1585 INIT_LIST_HEAD(&new_admin->entries);
1587 oper = rtnl_dereference(q->oper_sched);
1588 admin = rtnl_dereference(q->admin_sched);
1590 /* no changes - no new mqprio settings */
1591 if (!taprio_mqprio_cmp(dev, mqprio))
1594 if (mqprio && (oper || admin)) {
1595 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1600 err = parse_taprio_schedule(q, tb, new_admin, extack);
1604 if (new_admin->num_entries == 0) {
1605 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1610 err = taprio_parse_clockid(sch, tb, extack);
1614 taprio_set_picos_per_byte(dev, q);
1617 err = netdev_set_num_tc(dev, mqprio->num_tc);
1620 for (i = 0; i < mqprio->num_tc; i++)
1621 netdev_set_tc_queue(dev, i,
1625 /* Always use supplied priority mappings */
1626 for (i = 0; i <= TC_BITMASK; i++)
1627 netdev_set_prio_tc_map(dev, i,
1628 mqprio->prio_tc_map[i]);
1631 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1632 err = taprio_enable_offload(dev, q, new_admin, extack);
1634 err = taprio_disable_offload(dev, q, extack);
1638 /* Protects against enqueue()/dequeue() */
1639 spin_lock_bh(qdisc_lock(sch));
1641 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1642 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1643 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1648 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1651 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1652 !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1653 !hrtimer_active(&q->advance_timer)) {
1654 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1655 q->advance_timer.function = advance_sched;
1658 err = taprio_get_start_time(sch, new_admin, &start);
1660 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1664 setup_txtime(q, new_admin, start);
1666 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1668 rcu_assign_pointer(q->oper_sched, new_admin);
1674 rcu_assign_pointer(q->admin_sched, new_admin);
1676 call_rcu(&admin->rcu, taprio_free_sched_cb);
1678 setup_first_close_time(q, new_admin, start);
1680 /* Protects against advance_sched() */
1681 spin_lock_irqsave(&q->current_entry_lock, flags);
1683 taprio_start_sched(sch, start, new_admin);
1685 rcu_assign_pointer(q->admin_sched, new_admin);
1687 call_rcu(&admin->rcu, taprio_free_sched_cb);
1689 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1691 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1692 taprio_offload_config_changed(q);
1699 spin_unlock_bh(qdisc_lock(sch));
1703 call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1708 static void taprio_reset(struct Qdisc *sch)
1710 struct taprio_sched *q = qdisc_priv(sch);
1711 struct net_device *dev = qdisc_dev(sch);
1714 hrtimer_cancel(&q->advance_timer);
1717 for (i = 0; i < dev->num_tx_queues; i++)
1719 qdisc_reset(q->qdiscs[i]);
1723 static void taprio_destroy(struct Qdisc *sch)
1725 struct taprio_sched *q = qdisc_priv(sch);
1726 struct net_device *dev = qdisc_dev(sch);
1727 struct sched_gate_list *oper, *admin;
1730 list_del(&q->taprio_list);
1732 /* Note that taprio_reset() might not be called if an error
1733 * happens in qdisc_create(), after taprio_init() has been called.
1735 hrtimer_cancel(&q->advance_timer);
1736 qdisc_synchronize(sch);
1738 taprio_disable_offload(dev, q, NULL);
1741 for (i = 0; i < dev->num_tx_queues; i++)
1742 qdisc_put(q->qdiscs[i]);
1748 netdev_reset_tc(dev);
1750 oper = rtnl_dereference(q->oper_sched);
1751 admin = rtnl_dereference(q->admin_sched);
1754 call_rcu(&oper->rcu, taprio_free_sched_cb);
1757 call_rcu(&admin->rcu, taprio_free_sched_cb);
1760 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1761 struct netlink_ext_ack *extack)
1763 struct taprio_sched *q = qdisc_priv(sch);
1764 struct net_device *dev = qdisc_dev(sch);
1767 spin_lock_init(&q->current_entry_lock);
1769 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1770 q->advance_timer.function = advance_sched;
1774 /* We only support static clockids. Use an invalid value as default
1775 * and get the valid one on taprio_change().
1778 q->flags = TAPRIO_FLAGS_INVALID;
1780 list_add(&q->taprio_list, &taprio_list);
1782 if (sch->parent != TC_H_ROOT) {
1783 NL_SET_ERR_MSG_MOD(extack, "Can only be attached as root qdisc");
1787 if (!netif_is_multiqueue(dev)) {
1788 NL_SET_ERR_MSG_MOD(extack, "Multi-queue device is required");
1792 /* pre-allocate qdisc, attachment can't fail */
1793 q->qdiscs = kcalloc(dev->num_tx_queues,
1794 sizeof(q->qdiscs[0]),
1803 for (i = 0; i < dev->num_tx_queues; i++) {
1804 struct netdev_queue *dev_queue;
1805 struct Qdisc *qdisc;
1807 dev_queue = netdev_get_tx_queue(dev, i);
1808 qdisc = qdisc_create_dflt(dev_queue,
1810 TC_H_MAKE(TC_H_MAJ(sch->handle),
1816 if (i < dev->real_num_tx_queues)
1817 qdisc_hash_add(qdisc, false);
1819 q->qdiscs[i] = qdisc;
1822 return taprio_change(sch, opt, extack);
1825 static void taprio_attach(struct Qdisc *sch)
1827 struct taprio_sched *q = qdisc_priv(sch);
1828 struct net_device *dev = qdisc_dev(sch);
1831 /* Attach underlying qdisc */
1832 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
1833 struct Qdisc *qdisc = q->qdiscs[ntx];
1836 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1837 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1838 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
1840 old = dev_graft_qdisc(qdisc->dev_queue, sch);
1841 qdisc_refcount_inc(sch);
1847 /* access to the child qdiscs is not needed in offload mode */
1848 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1854 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1857 struct net_device *dev = qdisc_dev(sch);
1858 unsigned long ntx = cl - 1;
1860 if (ntx >= dev->num_tx_queues)
1863 return netdev_get_tx_queue(dev, ntx);
1866 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1867 struct Qdisc *new, struct Qdisc **old,
1868 struct netlink_ext_ack *extack)
1870 struct taprio_sched *q = qdisc_priv(sch);
1871 struct net_device *dev = qdisc_dev(sch);
1872 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1877 if (dev->flags & IFF_UP)
1878 dev_deactivate(dev);
1880 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1881 *old = dev_graft_qdisc(dev_queue, new);
1883 *old = q->qdiscs[cl - 1];
1884 q->qdiscs[cl - 1] = new;
1888 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1890 if (dev->flags & IFF_UP)
1896 static int dump_entry(struct sk_buff *msg,
1897 const struct sched_entry *entry)
1899 struct nlattr *item;
1901 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1905 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1906 goto nla_put_failure;
1908 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1909 goto nla_put_failure;
1911 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1913 goto nla_put_failure;
1915 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1917 goto nla_put_failure;
1919 return nla_nest_end(msg, item);
1922 nla_nest_cancel(msg, item);
1926 static int dump_schedule(struct sk_buff *msg,
1927 const struct sched_gate_list *root)
1929 struct nlattr *entry_list;
1930 struct sched_entry *entry;
1932 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1933 root->base_time, TCA_TAPRIO_PAD))
1936 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1937 root->cycle_time, TCA_TAPRIO_PAD))
1940 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1941 root->cycle_time_extension, TCA_TAPRIO_PAD))
1944 entry_list = nla_nest_start_noflag(msg,
1945 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1949 list_for_each_entry(entry, &root->entries, list) {
1950 if (dump_entry(msg, entry) < 0)
1954 nla_nest_end(msg, entry_list);
1958 nla_nest_cancel(msg, entry_list);
1962 static int taprio_dump_tc_entries(struct taprio_sched *q, struct sk_buff *skb)
1967 for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
1968 n = nla_nest_start(skb, TCA_TAPRIO_ATTR_TC_ENTRY);
1972 if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_INDEX, tc))
1973 goto nla_put_failure;
1975 if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_MAX_SDU,
1977 goto nla_put_failure;
1979 nla_nest_end(skb, n);
1985 nla_nest_cancel(skb, n);
1989 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1991 struct taprio_sched *q = qdisc_priv(sch);
1992 struct net_device *dev = qdisc_dev(sch);
1993 struct sched_gate_list *oper, *admin;
1994 struct tc_mqprio_qopt opt = { 0 };
1995 struct nlattr *nest, *sched_nest;
1998 oper = rtnl_dereference(q->oper_sched);
1999 admin = rtnl_dereference(q->admin_sched);
2001 opt.num_tc = netdev_get_num_tc(dev);
2002 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
2004 for (i = 0; i < netdev_get_num_tc(dev); i++) {
2005 opt.count[i] = dev->tc_to_txq[i].count;
2006 opt.offset[i] = dev->tc_to_txq[i].offset;
2009 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
2013 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
2016 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
2017 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
2020 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
2023 if (q->txtime_delay &&
2024 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
2027 if (taprio_dump_tc_entries(q, skb))
2030 if (oper && dump_schedule(skb, oper))
2036 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
2040 if (dump_schedule(skb, admin))
2043 nla_nest_end(skb, sched_nest);
2046 return nla_nest_end(skb, nest);
2049 nla_nest_cancel(skb, sched_nest);
2052 nla_nest_cancel(skb, nest);
2058 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
2060 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
2065 return rtnl_dereference(dev_queue->qdisc_sleeping);
2068 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
2070 unsigned int ntx = TC_H_MIN(classid);
2072 if (!taprio_queue_get(sch, ntx))
2077 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
2078 struct sk_buff *skb, struct tcmsg *tcm)
2080 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
2082 tcm->tcm_parent = TC_H_ROOT;
2083 tcm->tcm_handle |= TC_H_MIN(cl);
2084 tcm->tcm_info = rtnl_dereference(dev_queue->qdisc_sleeping)->handle;
2089 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
2090 struct gnet_dump *d)
2094 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
2096 sch = rtnl_dereference(dev_queue->qdisc_sleeping);
2097 if (gnet_stats_copy_basic(d, NULL, &sch->bstats, true) < 0 ||
2098 qdisc_qstats_copy(d, sch) < 0)
2103 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
2105 struct net_device *dev = qdisc_dev(sch);
2111 arg->count = arg->skip;
2112 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
2113 if (!tc_qdisc_stats_dump(sch, ntx + 1, arg))
2118 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
2121 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
2124 static const struct Qdisc_class_ops taprio_class_ops = {
2125 .graft = taprio_graft,
2126 .leaf = taprio_leaf,
2127 .find = taprio_find,
2128 .walk = taprio_walk,
2129 .dump = taprio_dump_class,
2130 .dump_stats = taprio_dump_class_stats,
2131 .select_queue = taprio_select_queue,
2134 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
2135 .cl_ops = &taprio_class_ops,
2137 .priv_size = sizeof(struct taprio_sched),
2138 .init = taprio_init,
2139 .change = taprio_change,
2140 .destroy = taprio_destroy,
2141 .reset = taprio_reset,
2142 .attach = taprio_attach,
2143 .peek = taprio_peek,
2144 .dequeue = taprio_dequeue,
2145 .enqueue = taprio_enqueue,
2146 .dump = taprio_dump,
2147 .owner = THIS_MODULE,
2150 static struct notifier_block taprio_device_notifier = {
2151 .notifier_call = taprio_dev_notifier,
2154 static int __init taprio_module_init(void)
2156 int err = register_netdevice_notifier(&taprio_device_notifier);
2161 return register_qdisc(&taprio_qdisc_ops);
2164 static void __exit taprio_module_exit(void)
2166 unregister_qdisc(&taprio_qdisc_ops);
2167 unregister_netdevice_notifier(&taprio_device_notifier);
2170 module_init(taprio_module_init);
2171 module_exit(taprio_module_exit);
2172 MODULE_LICENSE("GPL");