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/types.h>
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/list.h>
14 #include <linux/errno.h>
15 #include <linux/skbuff.h>
16 #include <linux/math64.h>
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
19 #include <linux/rcupdate.h>
20 #include <net/netlink.h>
21 #include <net/pkt_sched.h>
22 #include <net/pkt_cls.h>
23 #include <net/sch_generic.h>
27 static LIST_HEAD(taprio_list);
28 static DEFINE_SPINLOCK(taprio_list_lock);
30 #define TAPRIO_ALL_GATES_OPEN -1
32 #define FLAGS_VALID(flags) (!((flags) & ~TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST))
33 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
36 struct list_head list;
38 /* The instant that this entry "closes" and the next one
39 * should open, the qdisc will make some effort so that no
40 * packet leaves after this time.
51 struct sched_gate_list {
53 struct list_head entries;
55 ktime_t cycle_close_time;
57 s64 cycle_time_extension;
62 struct Qdisc **qdiscs;
65 enum tk_offsets tk_offset;
67 atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
68 * speeds it's sub-nanoseconds per byte
71 /* Protects the update side of the RCU protected current_entry */
72 spinlock_t current_entry_lock;
73 struct sched_entry __rcu *current_entry;
74 struct sched_gate_list __rcu *oper_sched;
75 struct sched_gate_list __rcu *admin_sched;
76 struct hrtimer advance_timer;
77 struct list_head taprio_list;
81 static ktime_t sched_base_time(const struct sched_gate_list *sched)
86 return ns_to_ktime(sched->base_time);
89 static ktime_t taprio_get_time(struct taprio_sched *q)
91 ktime_t mono = ktime_get();
93 switch (q->tk_offset) {
97 return ktime_mono_to_any(mono, q->tk_offset);
103 static void taprio_free_sched_cb(struct rcu_head *head)
105 struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
106 struct sched_entry *entry, *n;
111 list_for_each_entry_safe(entry, n, &sched->entries, list) {
112 list_del(&entry->list);
119 static void switch_schedules(struct taprio_sched *q,
120 struct sched_gate_list **admin,
121 struct sched_gate_list **oper)
123 rcu_assign_pointer(q->oper_sched, *admin);
124 rcu_assign_pointer(q->admin_sched, NULL);
127 call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
133 /* Get how much time has been already elapsed in the current cycle. */
134 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
136 ktime_t time_since_sched_start;
139 time_since_sched_start = ktime_sub(time, sched->base_time);
140 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
145 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
146 struct sched_gate_list *admin,
147 struct sched_entry *entry,
150 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
151 ktime_t intv_end, cycle_ext_end, cycle_end;
153 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
154 intv_end = ktime_add_ns(intv_start, entry->interval);
155 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
157 if (ktime_before(intv_end, cycle_end))
159 else if (admin && admin != sched &&
160 ktime_after(admin->base_time, cycle_end) &&
161 ktime_before(admin->base_time, cycle_ext_end))
162 return admin->base_time;
167 static int length_to_duration(struct taprio_sched *q, int len)
169 return div_u64(len * atomic64_read(&q->picos_per_byte), 1000);
172 /* Returns the entry corresponding to next available interval. If
173 * validate_interval is set, it only validates whether the timestamp occurs
174 * when the gate corresponding to the skb's traffic class is open.
176 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
178 struct sched_gate_list *sched,
179 struct sched_gate_list *admin,
181 ktime_t *interval_start,
182 ktime_t *interval_end,
183 bool validate_interval)
185 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
186 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
187 struct sched_entry *entry = NULL, *entry_found = NULL;
188 struct taprio_sched *q = qdisc_priv(sch);
189 struct net_device *dev = qdisc_dev(sch);
190 bool entry_available = false;
194 tc = netdev_get_prio_tc_map(dev, skb->priority);
195 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
203 cycle = sched->cycle_time;
204 cycle_elapsed = get_cycle_time_elapsed(sched, time);
205 curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
206 cycle_end = ktime_add_ns(curr_intv_end, cycle);
208 list_for_each_entry(entry, &sched->entries, list) {
209 curr_intv_start = curr_intv_end;
210 curr_intv_end = get_interval_end_time(sched, admin, entry,
213 if (ktime_after(curr_intv_start, cycle_end))
216 if (!(entry->gate_mask & BIT(tc)) ||
217 packet_transmit_time > entry->interval)
220 txtime = entry->next_txtime;
222 if (ktime_before(txtime, time) || validate_interval) {
223 transmit_end_time = ktime_add_ns(time, packet_transmit_time);
224 if ((ktime_before(curr_intv_start, time) &&
225 ktime_before(transmit_end_time, curr_intv_end)) ||
226 (ktime_after(curr_intv_start, time) && !validate_interval)) {
228 *interval_start = curr_intv_start;
229 *interval_end = curr_intv_end;
231 } else if (!entry_available && !validate_interval) {
232 /* Here, we are just trying to find out the
233 * first available interval in the next cycle.
237 *interval_start = ktime_add_ns(curr_intv_start, cycle);
238 *interval_end = ktime_add_ns(curr_intv_end, cycle);
240 } else if (ktime_before(txtime, earliest_txtime) &&
242 earliest_txtime = txtime;
244 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
245 *interval_start = ktime_add(curr_intv_start, n * cycle);
246 *interval_end = ktime_add(curr_intv_end, n * cycle);
253 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
255 struct taprio_sched *q = qdisc_priv(sch);
256 struct sched_gate_list *sched, *admin;
257 ktime_t interval_start, interval_end;
258 struct sched_entry *entry;
261 sched = rcu_dereference(q->oper_sched);
262 admin = rcu_dereference(q->admin_sched);
264 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
265 &interval_start, &interval_end, true);
271 /* This returns the tstamp value set by TCP in terms of the set clock. */
272 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
274 unsigned int offset = skb_network_offset(skb);
275 const struct ipv6hdr *ipv6h;
276 const struct iphdr *iph;
277 struct ipv6hdr _ipv6h;
279 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
283 if (ipv6h->version == 4) {
284 iph = (struct iphdr *)ipv6h;
285 offset += iph->ihl * 4;
287 /* special-case 6in4 tunnelling, as that is a common way to get
288 * v6 connectivity in the home
290 if (iph->protocol == IPPROTO_IPV6) {
291 ipv6h = skb_header_pointer(skb, offset,
292 sizeof(_ipv6h), &_ipv6h);
294 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
296 } else if (iph->protocol != IPPROTO_TCP) {
299 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
303 return ktime_mono_to_any(skb->skb_mstamp_ns, q->tk_offset);
306 /* There are a few scenarios where we will have to modify the txtime from
307 * what is read from next_txtime in sched_entry. They are:
308 * 1. If txtime is in the past,
309 * a. The gate for the traffic class is currently open and packet can be
310 * transmitted before it closes, schedule the packet right away.
311 * b. If the gate corresponding to the traffic class is going to open later
312 * in the cycle, set the txtime of packet to the interval start.
313 * 2. If txtime is in the future, there are packets corresponding to the
314 * current traffic class waiting to be transmitted. So, the following
315 * possibilities exist:
316 * a. We can transmit the packet before the window containing the txtime
318 * b. The window might close before the transmission can be completed
319 * successfully. So, schedule the packet in the next open window.
321 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
323 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
324 struct taprio_sched *q = qdisc_priv(sch);
325 struct sched_gate_list *sched, *admin;
326 ktime_t minimum_time, now, txtime;
327 int len, packet_transmit_time;
328 struct sched_entry *entry;
331 now = taprio_get_time(q);
332 minimum_time = ktime_add_ns(now, q->txtime_delay);
334 tcp_tstamp = get_tcp_tstamp(q, skb);
335 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
338 admin = rcu_dereference(q->admin_sched);
339 sched = rcu_dereference(q->oper_sched);
340 if (admin && ktime_after(minimum_time, admin->base_time))
341 switch_schedules(q, &admin, &sched);
343 /* Until the schedule starts, all the queues are open */
344 if (!sched || ktime_before(minimum_time, sched->base_time)) {
345 txtime = minimum_time;
349 len = qdisc_pkt_len(skb);
350 packet_transmit_time = length_to_duration(q, len);
355 entry = find_entry_to_transmit(skb, sch, sched, admin,
357 &interval_start, &interval_end,
364 txtime = entry->next_txtime;
365 txtime = max_t(ktime_t, txtime, minimum_time);
366 txtime = max_t(ktime_t, txtime, interval_start);
368 if (admin && admin != sched &&
369 ktime_after(txtime, admin->base_time)) {
375 transmit_end_time = ktime_add(txtime, packet_transmit_time);
376 minimum_time = transmit_end_time;
378 /* Update the txtime of current entry to the next time it's
381 if (ktime_after(transmit_end_time, interval_end))
382 entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
383 } while (sched_changed || ktime_after(transmit_end_time, interval_end));
385 entry->next_txtime = transmit_end_time;
392 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
393 struct sk_buff **to_free)
395 struct taprio_sched *q = qdisc_priv(sch);
399 queue = skb_get_queue_mapping(skb);
401 child = q->qdiscs[queue];
402 if (unlikely(!child))
403 return qdisc_drop(skb, sch, to_free);
405 if (skb->sk && sock_flag(skb->sk, SOCK_TXTIME)) {
406 if (!is_valid_interval(skb, sch))
407 return qdisc_drop(skb, sch, to_free);
408 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
409 skb->tstamp = get_packet_txtime(skb, sch);
411 return qdisc_drop(skb, sch, to_free);
414 qdisc_qstats_backlog_inc(sch, skb);
417 return qdisc_enqueue(skb, child, to_free);
420 static struct sk_buff *taprio_peek(struct Qdisc *sch)
422 struct taprio_sched *q = qdisc_priv(sch);
423 struct net_device *dev = qdisc_dev(sch);
424 struct sched_entry *entry;
430 entry = rcu_dereference(q->current_entry);
431 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
437 for (i = 0; i < dev->num_tx_queues; i++) {
438 struct Qdisc *child = q->qdiscs[i];
442 if (unlikely(!child))
445 skb = child->ops->peek(child);
449 if (TXTIME_ASSIST_IS_ENABLED(q->flags))
452 prio = skb->priority;
453 tc = netdev_get_prio_tc_map(dev, prio);
455 if (!(gate_mask & BIT(tc)))
464 static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry)
466 atomic_set(&entry->budget,
467 div64_u64((u64)entry->interval * 1000,
468 atomic64_read(&q->picos_per_byte)));
471 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
473 struct taprio_sched *q = qdisc_priv(sch);
474 struct net_device *dev = qdisc_dev(sch);
475 struct sk_buff *skb = NULL;
476 struct sched_entry *entry;
480 if (atomic64_read(&q->picos_per_byte) == -1) {
481 WARN_ONCE(1, "taprio: dequeue() called with unknown picos per byte.");
486 entry = rcu_dereference(q->current_entry);
487 /* if there's no entry, it means that the schedule didn't
488 * start yet, so force all gates to be open, this is in
489 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
492 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
497 for (i = 0; i < dev->num_tx_queues; i++) {
498 struct Qdisc *child = q->qdiscs[i];
504 if (unlikely(!child))
507 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
508 skb = child->ops->dequeue(child);
514 skb = child->ops->peek(child);
518 prio = skb->priority;
519 tc = netdev_get_prio_tc_map(dev, prio);
521 if (!(gate_mask & BIT(tc)))
524 len = qdisc_pkt_len(skb);
525 guard = ktime_add_ns(taprio_get_time(q),
526 length_to_duration(q, len));
528 /* In the case that there's no gate entry, there's no
531 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
532 ktime_after(guard, entry->close_time))
535 /* ... and no budget. */
536 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
537 atomic_sub_return(len, &entry->budget) < 0)
540 skb = child->ops->dequeue(child);
545 qdisc_bstats_update(sch, skb);
546 qdisc_qstats_backlog_dec(sch, skb);
558 static bool should_restart_cycle(const struct sched_gate_list *oper,
559 const struct sched_entry *entry)
561 if (list_is_last(&entry->list, &oper->entries))
564 if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0)
570 static bool should_change_schedules(const struct sched_gate_list *admin,
571 const struct sched_gate_list *oper,
574 ktime_t next_base_time, extension_time;
579 next_base_time = sched_base_time(admin);
581 /* This is the simple case, the close_time would fall after
582 * the next schedule base_time.
584 if (ktime_compare(next_base_time, close_time) <= 0)
587 /* This is the cycle_time_extension case, if the close_time
588 * plus the amount that can be extended would fall after the
589 * next schedule base_time, we can extend the current schedule
592 extension_time = ktime_add_ns(close_time, oper->cycle_time_extension);
594 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
595 * how precisely the extension should be made. So after
596 * conformance testing, this logic may change.
598 if (ktime_compare(next_base_time, extension_time) <= 0)
604 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
606 struct taprio_sched *q = container_of(timer, struct taprio_sched,
608 struct sched_gate_list *oper, *admin;
609 struct sched_entry *entry, *next;
610 struct Qdisc *sch = q->root;
613 spin_lock(&q->current_entry_lock);
614 entry = rcu_dereference_protected(q->current_entry,
615 lockdep_is_held(&q->current_entry_lock));
616 oper = rcu_dereference_protected(q->oper_sched,
617 lockdep_is_held(&q->current_entry_lock));
618 admin = rcu_dereference_protected(q->admin_sched,
619 lockdep_is_held(&q->current_entry_lock));
622 switch_schedules(q, &admin, &oper);
624 /* This can happen in two cases: 1. this is the very first run
625 * of this function (i.e. we weren't running any schedule
626 * previously); 2. The previous schedule just ended. The first
627 * entry of all schedules are pre-calculated during the
628 * schedule initialization.
630 if (unlikely(!entry || entry->close_time == oper->base_time)) {
631 next = list_first_entry(&oper->entries, struct sched_entry,
633 close_time = next->close_time;
637 if (should_restart_cycle(oper, entry)) {
638 next = list_first_entry(&oper->entries, struct sched_entry,
640 oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time,
643 next = list_next_entry(entry, list);
646 close_time = ktime_add_ns(entry->close_time, next->interval);
647 close_time = min_t(ktime_t, close_time, oper->cycle_close_time);
649 if (should_change_schedules(admin, oper, close_time)) {
650 /* Set things so the next time this runs, the new
653 close_time = sched_base_time(admin);
654 switch_schedules(q, &admin, &oper);
657 next->close_time = close_time;
658 taprio_set_budget(q, next);
661 rcu_assign_pointer(q->current_entry, next);
662 spin_unlock(&q->current_entry_lock);
664 hrtimer_set_expires(&q->advance_timer, close_time);
667 __netif_schedule(sch);
670 return HRTIMER_RESTART;
673 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
674 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
675 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
676 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
677 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
680 static const struct nla_policy entry_list_policy[TCA_TAPRIO_SCHED_MAX + 1] = {
681 [TCA_TAPRIO_SCHED_ENTRY] = { .type = NLA_NESTED },
684 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
685 [TCA_TAPRIO_ATTR_PRIOMAP] = {
686 .len = sizeof(struct tc_mqprio_qopt)
688 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
689 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
690 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
691 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
692 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 },
693 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
696 static int fill_sched_entry(struct nlattr **tb, struct sched_entry *entry,
697 struct netlink_ext_ack *extack)
701 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
702 entry->command = nla_get_u8(
703 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
705 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
706 entry->gate_mask = nla_get_u32(
707 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
709 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
710 interval = nla_get_u32(
711 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
714 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
718 entry->interval = interval;
723 static int parse_sched_entry(struct nlattr *n, struct sched_entry *entry,
724 int index, struct netlink_ext_ack *extack)
726 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
729 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
732 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
736 entry->index = index;
738 return fill_sched_entry(tb, entry, extack);
741 static int parse_sched_list(struct nlattr *list,
742 struct sched_gate_list *sched,
743 struct netlink_ext_ack *extack)
752 nla_for_each_nested(n, list, rem) {
753 struct sched_entry *entry;
755 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
756 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
760 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
762 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
766 err = parse_sched_entry(n, entry, i, extack);
772 list_add_tail(&entry->list, &sched->entries);
776 sched->num_entries = i;
781 static int parse_taprio_schedule(struct nlattr **tb,
782 struct sched_gate_list *new,
783 struct netlink_ext_ack *extack)
787 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
788 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
792 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
793 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
795 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
796 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
798 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
799 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
801 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
802 err = parse_sched_list(
803 tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST], new, extack);
807 if (!new->cycle_time) {
808 struct sched_entry *entry;
811 list_for_each_entry(entry, &new->entries, list)
812 cycle = ktime_add_ns(cycle, entry->interval);
813 new->cycle_time = cycle;
819 static int taprio_parse_mqprio_opt(struct net_device *dev,
820 struct tc_mqprio_qopt *qopt,
821 struct netlink_ext_ack *extack,
826 if (!qopt && !dev->num_tc) {
827 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
831 /* If num_tc is already set, it means that the user already
832 * configured the mqprio part
837 /* Verify num_tc is not out of max range */
838 if (qopt->num_tc > TC_MAX_QUEUE) {
839 NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
843 /* taprio imposes that traffic classes map 1:n to tx queues */
844 if (qopt->num_tc > dev->num_tx_queues) {
845 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
849 /* Verify priority mapping uses valid tcs */
850 for (i = 0; i < TC_BITMASK + 1; i++) {
851 if (qopt->prio_tc_map[i] >= qopt->num_tc) {
852 NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
857 for (i = 0; i < qopt->num_tc; i++) {
858 unsigned int last = qopt->offset[i] + qopt->count[i];
860 /* Verify the queue count is in tx range being equal to the
861 * real_num_tx_queues indicates the last queue is in use.
863 if (qopt->offset[i] >= dev->num_tx_queues ||
865 last > dev->real_num_tx_queues) {
866 NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
870 if (TXTIME_ASSIST_IS_ENABLED(taprio_flags))
873 /* Verify that the offset and counts do not overlap */
874 for (j = i + 1; j < qopt->num_tc; j++) {
875 if (last > qopt->offset[j]) {
876 NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
885 static int taprio_get_start_time(struct Qdisc *sch,
886 struct sched_gate_list *sched,
889 struct taprio_sched *q = qdisc_priv(sch);
890 ktime_t now, base, cycle;
893 base = sched_base_time(sched);
894 now = taprio_get_time(q);
896 if (ktime_after(base, now)) {
901 cycle = sched->cycle_time;
903 /* The qdisc is expected to have at least one sched_entry. Moreover,
904 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
905 * something went really wrong. In that case, we should warn about this
906 * inconsistent state and return error.
911 /* Schedule the start time for the beginning of the next
914 n = div64_s64(ktime_sub_ns(now, base), cycle);
915 *start = ktime_add_ns(base, (n + 1) * cycle);
919 static void setup_first_close_time(struct taprio_sched *q,
920 struct sched_gate_list *sched, ktime_t base)
922 struct sched_entry *first;
925 first = list_first_entry(&sched->entries,
926 struct sched_entry, list);
928 cycle = sched->cycle_time;
930 /* FIXME: find a better place to do this */
931 sched->cycle_close_time = ktime_add_ns(base, cycle);
933 first->close_time = ktime_add_ns(base, first->interval);
934 taprio_set_budget(q, first);
935 rcu_assign_pointer(q->current_entry, NULL);
938 static void taprio_start_sched(struct Qdisc *sch,
939 ktime_t start, struct sched_gate_list *new)
941 struct taprio_sched *q = qdisc_priv(sch);
944 expires = hrtimer_get_expires(&q->advance_timer);
948 /* If the new schedule starts before the next expiration, we
949 * reprogram it to the earliest one, so we change the admin
950 * schedule to the operational one at the right time.
952 start = min_t(ktime_t, start, expires);
954 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
957 static void taprio_set_picos_per_byte(struct net_device *dev,
958 struct taprio_sched *q)
960 struct ethtool_link_ksettings ecmd;
961 int picos_per_byte = -1;
963 if (!__ethtool_get_link_ksettings(dev, &ecmd) &&
964 ecmd.base.speed != SPEED_UNKNOWN)
965 picos_per_byte = div64_s64(NSEC_PER_SEC * 1000LL * 8,
966 ecmd.base.speed * 1000 * 1000);
968 atomic64_set(&q->picos_per_byte, picos_per_byte);
969 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
970 dev->name, (long long)atomic64_read(&q->picos_per_byte),
974 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
977 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
978 struct net_device *qdev;
979 struct taprio_sched *q;
984 if (event != NETDEV_UP && event != NETDEV_CHANGE)
987 spin_lock(&taprio_list_lock);
988 list_for_each_entry(q, &taprio_list, taprio_list) {
989 qdev = qdisc_dev(q->root);
995 spin_unlock(&taprio_list_lock);
998 taprio_set_picos_per_byte(dev, q);
1003 static void setup_txtime(struct taprio_sched *q,
1004 struct sched_gate_list *sched, ktime_t base)
1006 struct sched_entry *entry;
1009 list_for_each_entry(entry, &sched->entries, list) {
1010 entry->next_txtime = ktime_add_ns(base, interval);
1011 interval += entry->interval;
1015 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1016 struct netlink_ext_ack *extack)
1018 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1019 struct sched_gate_list *oper, *admin, *new_admin;
1020 struct taprio_sched *q = qdisc_priv(sch);
1021 struct net_device *dev = qdisc_dev(sch);
1022 struct tc_mqprio_qopt *mqprio = NULL;
1023 u32 taprio_flags = 0;
1024 int i, err, clockid;
1025 unsigned long flags;
1028 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1029 taprio_policy, extack);
1033 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1034 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1036 if (tb[TCA_TAPRIO_ATTR_FLAGS]) {
1037 taprio_flags = nla_get_u32(tb[TCA_TAPRIO_ATTR_FLAGS]);
1039 if (q->flags != 0 && q->flags != taprio_flags) {
1040 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1042 } else if (!FLAGS_VALID(taprio_flags)) {
1043 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1047 q->flags = taprio_flags;
1050 err = taprio_parse_mqprio_opt(dev, mqprio, extack, taprio_flags);
1054 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1056 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1059 INIT_LIST_HEAD(&new_admin->entries);
1062 oper = rcu_dereference(q->oper_sched);
1063 admin = rcu_dereference(q->admin_sched);
1066 if (mqprio && (oper || admin)) {
1067 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1072 err = parse_taprio_schedule(tb, new_admin, extack);
1076 if (new_admin->num_entries == 0) {
1077 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1082 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1083 clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1085 /* We only support static clockids and we don't allow
1086 * for it to be modified after the first init.
1089 (q->clockid != -1 && q->clockid != clockid)) {
1090 NL_SET_ERR_MSG(extack, "Changing the 'clockid' of a running schedule is not supported");
1095 q->clockid = clockid;
1098 if (q->clockid == -1 && !tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1099 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1104 taprio_set_picos_per_byte(dev, q);
1106 /* Protects against enqueue()/dequeue() */
1107 spin_lock_bh(qdisc_lock(sch));
1109 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1110 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1111 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1116 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1119 if (!TXTIME_ASSIST_IS_ENABLED(taprio_flags) &&
1120 !hrtimer_active(&q->advance_timer)) {
1121 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1122 q->advance_timer.function = advance_sched;
1126 netdev_set_num_tc(dev, mqprio->num_tc);
1127 for (i = 0; i < mqprio->num_tc; i++)
1128 netdev_set_tc_queue(dev, i,
1132 /* Always use supplied priority mappings */
1133 for (i = 0; i < TC_BITMASK + 1; i++)
1134 netdev_set_prio_tc_map(dev, i,
1135 mqprio->prio_tc_map[i]);
1138 switch (q->clockid) {
1139 case CLOCK_REALTIME:
1140 q->tk_offset = TK_OFFS_REAL;
1142 case CLOCK_MONOTONIC:
1143 q->tk_offset = TK_OFFS_MAX;
1145 case CLOCK_BOOTTIME:
1146 q->tk_offset = TK_OFFS_BOOT;
1149 q->tk_offset = TK_OFFS_TAI;
1152 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1157 err = taprio_get_start_time(sch, new_admin, &start);
1159 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1163 if (TXTIME_ASSIST_IS_ENABLED(taprio_flags)) {
1164 setup_txtime(q, new_admin, start);
1167 rcu_assign_pointer(q->oper_sched, new_admin);
1173 rcu_assign_pointer(q->admin_sched, new_admin);
1175 call_rcu(&admin->rcu, taprio_free_sched_cb);
1177 setup_first_close_time(q, new_admin, start);
1179 /* Protects against advance_sched() */
1180 spin_lock_irqsave(&q->current_entry_lock, flags);
1182 taprio_start_sched(sch, start, new_admin);
1184 rcu_assign_pointer(q->admin_sched, new_admin);
1186 call_rcu(&admin->rcu, taprio_free_sched_cb);
1188 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1195 spin_unlock_bh(qdisc_lock(sch));
1203 static void taprio_destroy(struct Qdisc *sch)
1205 struct taprio_sched *q = qdisc_priv(sch);
1206 struct net_device *dev = qdisc_dev(sch);
1209 spin_lock(&taprio_list_lock);
1210 list_del(&q->taprio_list);
1211 spin_unlock(&taprio_list_lock);
1213 hrtimer_cancel(&q->advance_timer);
1216 for (i = 0; i < dev->num_tx_queues && q->qdiscs[i]; i++)
1217 qdisc_put(q->qdiscs[i]);
1223 netdev_set_num_tc(dev, 0);
1226 call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb);
1229 call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb);
1232 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1233 struct netlink_ext_ack *extack)
1235 struct taprio_sched *q = qdisc_priv(sch);
1236 struct net_device *dev = qdisc_dev(sch);
1239 spin_lock_init(&q->current_entry_lock);
1241 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1242 q->advance_timer.function = advance_sched;
1246 /* We only support static clockids. Use an invalid value as default
1247 * and get the valid one on taprio_change().
1251 if (sch->parent != TC_H_ROOT)
1254 if (!netif_is_multiqueue(dev))
1257 /* pre-allocate qdisc, attachment can't fail */
1258 q->qdiscs = kcalloc(dev->num_tx_queues,
1259 sizeof(q->qdiscs[0]),
1268 spin_lock(&taprio_list_lock);
1269 list_add(&q->taprio_list, &taprio_list);
1270 spin_unlock(&taprio_list_lock);
1272 for (i = 0; i < dev->num_tx_queues; i++) {
1273 struct netdev_queue *dev_queue;
1274 struct Qdisc *qdisc;
1276 dev_queue = netdev_get_tx_queue(dev, i);
1277 qdisc = qdisc_create_dflt(dev_queue,
1279 TC_H_MAKE(TC_H_MAJ(sch->handle),
1285 if (i < dev->real_num_tx_queues)
1286 qdisc_hash_add(qdisc, false);
1288 q->qdiscs[i] = qdisc;
1291 return taprio_change(sch, opt, extack);
1294 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1297 struct net_device *dev = qdisc_dev(sch);
1298 unsigned long ntx = cl - 1;
1300 if (ntx >= dev->num_tx_queues)
1303 return netdev_get_tx_queue(dev, ntx);
1306 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1307 struct Qdisc *new, struct Qdisc **old,
1308 struct netlink_ext_ack *extack)
1310 struct taprio_sched *q = qdisc_priv(sch);
1311 struct net_device *dev = qdisc_dev(sch);
1312 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1317 if (dev->flags & IFF_UP)
1318 dev_deactivate(dev);
1320 *old = q->qdiscs[cl - 1];
1321 q->qdiscs[cl - 1] = new;
1324 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1326 if (dev->flags & IFF_UP)
1332 static int dump_entry(struct sk_buff *msg,
1333 const struct sched_entry *entry)
1335 struct nlattr *item;
1337 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1341 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1342 goto nla_put_failure;
1344 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1345 goto nla_put_failure;
1347 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1349 goto nla_put_failure;
1351 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1353 goto nla_put_failure;
1355 return nla_nest_end(msg, item);
1358 nla_nest_cancel(msg, item);
1362 static int dump_schedule(struct sk_buff *msg,
1363 const struct sched_gate_list *root)
1365 struct nlattr *entry_list;
1366 struct sched_entry *entry;
1368 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1369 root->base_time, TCA_TAPRIO_PAD))
1372 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1373 root->cycle_time, TCA_TAPRIO_PAD))
1376 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1377 root->cycle_time_extension, TCA_TAPRIO_PAD))
1380 entry_list = nla_nest_start_noflag(msg,
1381 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1385 list_for_each_entry(entry, &root->entries, list) {
1386 if (dump_entry(msg, entry) < 0)
1390 nla_nest_end(msg, entry_list);
1394 nla_nest_cancel(msg, entry_list);
1398 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1400 struct taprio_sched *q = qdisc_priv(sch);
1401 struct net_device *dev = qdisc_dev(sch);
1402 struct sched_gate_list *oper, *admin;
1403 struct tc_mqprio_qopt opt = { 0 };
1404 struct nlattr *nest, *sched_nest;
1408 oper = rcu_dereference(q->oper_sched);
1409 admin = rcu_dereference(q->admin_sched);
1411 opt.num_tc = netdev_get_num_tc(dev);
1412 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
1414 for (i = 0; i < netdev_get_num_tc(dev); i++) {
1415 opt.count[i] = dev->tc_to_txq[i].count;
1416 opt.offset[i] = dev->tc_to_txq[i].offset;
1419 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1423 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
1426 if (nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
1429 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
1432 if (q->txtime_delay &&
1433 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
1436 if (oper && dump_schedule(skb, oper))
1442 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
1446 if (dump_schedule(skb, admin))
1449 nla_nest_end(skb, sched_nest);
1454 return nla_nest_end(skb, nest);
1457 nla_nest_cancel(skb, sched_nest);
1460 nla_nest_cancel(skb, nest);
1467 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
1469 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1474 return dev_queue->qdisc_sleeping;
1477 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
1479 unsigned int ntx = TC_H_MIN(classid);
1481 if (!taprio_queue_get(sch, ntx))
1486 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
1487 struct sk_buff *skb, struct tcmsg *tcm)
1489 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1491 tcm->tcm_parent = TC_H_ROOT;
1492 tcm->tcm_handle |= TC_H_MIN(cl);
1493 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
1498 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
1499 struct gnet_dump *d)
1503 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1505 sch = dev_queue->qdisc_sleeping;
1506 if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 ||
1507 qdisc_qstats_copy(d, sch) < 0)
1512 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1514 struct net_device *dev = qdisc_dev(sch);
1520 arg->count = arg->skip;
1521 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
1522 if (arg->fn(sch, ntx + 1, arg) < 0) {
1530 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
1533 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
1536 static const struct Qdisc_class_ops taprio_class_ops = {
1537 .graft = taprio_graft,
1538 .leaf = taprio_leaf,
1539 .find = taprio_find,
1540 .walk = taprio_walk,
1541 .dump = taprio_dump_class,
1542 .dump_stats = taprio_dump_class_stats,
1543 .select_queue = taprio_select_queue,
1546 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
1547 .cl_ops = &taprio_class_ops,
1549 .priv_size = sizeof(struct taprio_sched),
1550 .init = taprio_init,
1551 .change = taprio_change,
1552 .destroy = taprio_destroy,
1553 .peek = taprio_peek,
1554 .dequeue = taprio_dequeue,
1555 .enqueue = taprio_enqueue,
1556 .dump = taprio_dump,
1557 .owner = THIS_MODULE,
1560 static struct notifier_block taprio_device_notifier = {
1561 .notifier_call = taprio_dev_notifier,
1564 static int __init taprio_module_init(void)
1566 int err = register_netdevice_notifier(&taprio_device_notifier);
1571 return register_qdisc(&taprio_qdisc_ops);
1574 static void __exit taprio_module_exit(void)
1576 unregister_qdisc(&taprio_qdisc_ops);
1577 unregister_netdevice_notifier(&taprio_device_notifier);
1580 module_init(taprio_module_init);
1581 module_exit(taprio_module_exit);
1582 MODULE_LICENSE("GPL");