blob: 8fccb30e3ee9b4351b8c6b726f92d4f8e9f3e140 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2
3/* net/sched/sch_taprio.c Time Aware Priority Scheduler
4 *
5 * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com>
6 *
7 */
8
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>
24#include <net/sock.h>
25#include <net/tcp.h>
26
27static LIST_HEAD(taprio_list);
28static DEFINE_SPINLOCK(taprio_list_lock);
29
30#define TAPRIO_ALL_GATES_OPEN -1
31
32#define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
33#define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
34#define TAPRIO_FLAGS_INVALID U32_MAX
35
36struct sched_entry {
37 struct list_head list;
38
39 /* The instant that this entry "closes" and the next one
40 * should open, the qdisc will make some effort so that no
41 * packet leaves after this time.
42 */
43 ktime_t close_time;
44 ktime_t next_txtime;
45 atomic_t budget;
46 int index;
47 u32 gate_mask;
48 u32 interval;
49 u8 command;
50};
51
52struct sched_gate_list {
53 struct rcu_head rcu;
54 struct list_head entries;
55 size_t num_entries;
56 ktime_t cycle_close_time;
57 s64 cycle_time;
58 s64 cycle_time_extension;
59 s64 base_time;
60};
61
62struct taprio_sched {
63 struct Qdisc **qdiscs;
64 struct Qdisc *root;
65 u32 flags;
66 enum tk_offsets tk_offset;
67 int clockid;
68 bool offloaded;
69 atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
70 * speeds it's sub-nanoseconds per byte
71 */
72
73 /* Protects the update side of the RCU protected current_entry */
74 spinlock_t current_entry_lock;
75 struct sched_entry __rcu *current_entry;
76 struct sched_gate_list __rcu *oper_sched;
77 struct sched_gate_list __rcu *admin_sched;
78 struct hrtimer advance_timer;
79 struct list_head taprio_list;
80 struct sk_buff *(*dequeue)(struct Qdisc *sch);
81 struct sk_buff *(*peek)(struct Qdisc *sch);
82 u32 txtime_delay;
83};
84
85struct __tc_taprio_qopt_offload {
86 refcount_t users;
87 struct tc_taprio_qopt_offload offload;
88};
89
90static ktime_t sched_base_time(const struct sched_gate_list *sched)
91{
92 if (!sched)
93 return KTIME_MAX;
94
95 return ns_to_ktime(sched->base_time);
96}
97
98static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono)
99{
100 /* This pairs with WRITE_ONCE() in taprio_parse_clockid() */
101 enum tk_offsets tk_offset = READ_ONCE(q->tk_offset);
102
103 switch (tk_offset) {
104 case TK_OFFS_MAX:
105 return mono;
106 default:
107 return ktime_mono_to_any(mono, tk_offset);
108 }
109}
110
111static ktime_t taprio_get_time(const struct taprio_sched *q)
112{
113 return taprio_mono_to_any(q, ktime_get());
114}
115
116static void taprio_free_sched_cb(struct rcu_head *head)
117{
118 struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
119 struct sched_entry *entry, *n;
120
121 if (!sched)
122 return;
123
124 list_for_each_entry_safe(entry, n, &sched->entries, list) {
125 list_del(&entry->list);
126 kfree(entry);
127 }
128
129 kfree(sched);
130}
131
132static void switch_schedules(struct taprio_sched *q,
133 struct sched_gate_list **admin,
134 struct sched_gate_list **oper)
135{
136 rcu_assign_pointer(q->oper_sched, *admin);
137 rcu_assign_pointer(q->admin_sched, NULL);
138
139 if (*oper)
140 call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
141
142 *oper = *admin;
143 *admin = NULL;
144}
145
146/* Get how much time has been already elapsed in the current cycle. */
147static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
148{
149 ktime_t time_since_sched_start;
150 s32 time_elapsed;
151
152 time_since_sched_start = ktime_sub(time, sched->base_time);
153 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
154
155 return time_elapsed;
156}
157
158static ktime_t get_interval_end_time(struct sched_gate_list *sched,
159 struct sched_gate_list *admin,
160 struct sched_entry *entry,
161 ktime_t intv_start)
162{
163 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
164 ktime_t intv_end, cycle_ext_end, cycle_end;
165
166 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
167 intv_end = ktime_add_ns(intv_start, entry->interval);
168 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
169
170 if (ktime_before(intv_end, cycle_end))
171 return intv_end;
172 else if (admin && admin != sched &&
173 ktime_after(admin->base_time, cycle_end) &&
174 ktime_before(admin->base_time, cycle_ext_end))
175 return admin->base_time;
176 else
177 return cycle_end;
178}
179
180static int length_to_duration(struct taprio_sched *q, int len)
181{
182 return div_u64(len * atomic64_read(&q->picos_per_byte), 1000);
183}
184
185/* Returns the entry corresponding to next available interval. If
186 * validate_interval is set, it only validates whether the timestamp occurs
187 * when the gate corresponding to the skb's traffic class is open.
188 */
189static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
190 struct Qdisc *sch,
191 struct sched_gate_list *sched,
192 struct sched_gate_list *admin,
193 ktime_t time,
194 ktime_t *interval_start,
195 ktime_t *interval_end,
196 bool validate_interval)
197{
198 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
199 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
200 struct sched_entry *entry = NULL, *entry_found = NULL;
201 struct taprio_sched *q = qdisc_priv(sch);
202 struct net_device *dev = qdisc_dev(sch);
203 bool entry_available = false;
204 s32 cycle_elapsed;
205 int tc, n;
206
207 tc = netdev_get_prio_tc_map(dev, skb->priority);
208 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
209
210 *interval_start = 0;
211 *interval_end = 0;
212
213 if (!sched)
214 return NULL;
215
216 cycle = sched->cycle_time;
217 cycle_elapsed = get_cycle_time_elapsed(sched, time);
218 curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
219 cycle_end = ktime_add_ns(curr_intv_end, cycle);
220
221 list_for_each_entry(entry, &sched->entries, list) {
222 curr_intv_start = curr_intv_end;
223 curr_intv_end = get_interval_end_time(sched, admin, entry,
224 curr_intv_start);
225
226 if (ktime_after(curr_intv_start, cycle_end))
227 break;
228
229 if (!(entry->gate_mask & BIT(tc)) ||
230 packet_transmit_time > entry->interval)
231 continue;
232
233 txtime = entry->next_txtime;
234
235 if (ktime_before(txtime, time) || validate_interval) {
236 transmit_end_time = ktime_add_ns(time, packet_transmit_time);
237 if ((ktime_before(curr_intv_start, time) &&
238 ktime_before(transmit_end_time, curr_intv_end)) ||
239 (ktime_after(curr_intv_start, time) && !validate_interval)) {
240 entry_found = entry;
241 *interval_start = curr_intv_start;
242 *interval_end = curr_intv_end;
243 break;
244 } else if (!entry_available && !validate_interval) {
245 /* Here, we are just trying to find out the
246 * first available interval in the next cycle.
247 */
248 entry_available = 1;
249 entry_found = entry;
250 *interval_start = ktime_add_ns(curr_intv_start, cycle);
251 *interval_end = ktime_add_ns(curr_intv_end, cycle);
252 }
253 } else if (ktime_before(txtime, earliest_txtime) &&
254 !entry_available) {
255 earliest_txtime = txtime;
256 entry_found = entry;
257 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
258 *interval_start = ktime_add(curr_intv_start, n * cycle);
259 *interval_end = ktime_add(curr_intv_end, n * cycle);
260 }
261 }
262
263 return entry_found;
264}
265
266static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
267{
268 struct taprio_sched *q = qdisc_priv(sch);
269 struct sched_gate_list *sched, *admin;
270 ktime_t interval_start, interval_end;
271 struct sched_entry *entry;
272
273 rcu_read_lock();
274 sched = rcu_dereference(q->oper_sched);
275 admin = rcu_dereference(q->admin_sched);
276
277 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
278 &interval_start, &interval_end, true);
279 rcu_read_unlock();
280
281 return entry;
282}
283
284static bool taprio_flags_valid(u32 flags)
285{
286 /* Make sure no other flag bits are set. */
287 if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST |
288 TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
289 return false;
290 /* txtime-assist and full offload are mutually exclusive */
291 if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
292 (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
293 return false;
294 return true;
295}
296
297/* This returns the tstamp value set by TCP in terms of the set clock. */
298static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
299{
300 unsigned int offset = skb_network_offset(skb);
301 const struct ipv6hdr *ipv6h;
302 const struct iphdr *iph;
303 struct ipv6hdr _ipv6h;
304
305 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
306 if (!ipv6h)
307 return 0;
308
309 if (ipv6h->version == 4) {
310 iph = (struct iphdr *)ipv6h;
311 offset += iph->ihl * 4;
312
313 /* special-case 6in4 tunnelling, as that is a common way to get
314 * v6 connectivity in the home
315 */
316 if (iph->protocol == IPPROTO_IPV6) {
317 ipv6h = skb_header_pointer(skb, offset,
318 sizeof(_ipv6h), &_ipv6h);
319
320 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
321 return 0;
322 } else if (iph->protocol != IPPROTO_TCP) {
323 return 0;
324 }
325 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
326 return 0;
327 }
328
329 return taprio_mono_to_any(q, skb->skb_mstamp_ns);
330}
331
332/* There are a few scenarios where we will have to modify the txtime from
333 * what is read from next_txtime in sched_entry. They are:
334 * 1. If txtime is in the past,
335 * a. The gate for the traffic class is currently open and packet can be
336 * transmitted before it closes, schedule the packet right away.
337 * b. If the gate corresponding to the traffic class is going to open later
338 * in the cycle, set the txtime of packet to the interval start.
339 * 2. If txtime is in the future, there are packets corresponding to the
340 * current traffic class waiting to be transmitted. So, the following
341 * possibilities exist:
342 * a. We can transmit the packet before the window containing the txtime
343 * closes.
344 * b. The window might close before the transmission can be completed
345 * successfully. So, schedule the packet in the next open window.
346 */
347static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
348{
349 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
350 struct taprio_sched *q = qdisc_priv(sch);
351 struct sched_gate_list *sched, *admin;
352 ktime_t minimum_time, now, txtime;
353 int len, packet_transmit_time;
354 struct sched_entry *entry;
355 bool sched_changed;
356
357 now = taprio_get_time(q);
358 minimum_time = ktime_add_ns(now, q->txtime_delay);
359
360 tcp_tstamp = get_tcp_tstamp(q, skb);
361 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
362
363 rcu_read_lock();
364 admin = rcu_dereference(q->admin_sched);
365 sched = rcu_dereference(q->oper_sched);
366 if (admin && ktime_after(minimum_time, admin->base_time))
367 switch_schedules(q, &admin, &sched);
368
369 /* Until the schedule starts, all the queues are open */
370 if (!sched || ktime_before(minimum_time, sched->base_time)) {
371 txtime = minimum_time;
372 goto done;
373 }
374
375 len = qdisc_pkt_len(skb);
376 packet_transmit_time = length_to_duration(q, len);
377
378 do {
379 sched_changed = 0;
380
381 entry = find_entry_to_transmit(skb, sch, sched, admin,
382 minimum_time,
383 &interval_start, &interval_end,
384 false);
385 if (!entry) {
386 txtime = 0;
387 goto done;
388 }
389
390 txtime = entry->next_txtime;
391 txtime = max_t(ktime_t, txtime, minimum_time);
392 txtime = max_t(ktime_t, txtime, interval_start);
393
394 if (admin && admin != sched &&
395 ktime_after(txtime, admin->base_time)) {
396 sched = admin;
397 sched_changed = 1;
398 continue;
399 }
400
401 transmit_end_time = ktime_add(txtime, packet_transmit_time);
402 minimum_time = transmit_end_time;
403
404 /* Update the txtime of current entry to the next time it's
405 * interval starts.
406 */
407 if (ktime_after(transmit_end_time, interval_end))
408 entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
409 } while (sched_changed || ktime_after(transmit_end_time, interval_end));
410
411 entry->next_txtime = transmit_end_time;
412
413done:
414 rcu_read_unlock();
415 return txtime;
416}
417
418static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
419 struct sk_buff **to_free)
420{
421 struct taprio_sched *q = qdisc_priv(sch);
422 struct Qdisc *child;
423 int queue;
424
425 queue = skb_get_queue_mapping(skb);
426
427 child = q->qdiscs[queue];
428 if (unlikely(!child))
429 return qdisc_drop(skb, sch, to_free);
430
431 /* sk_flags are only safe to use on full sockets. */
432 if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) {
433 if (!is_valid_interval(skb, sch))
434 return qdisc_drop(skb, sch, to_free);
435 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
436 skb->tstamp = get_packet_txtime(skb, sch);
437 if (!skb->tstamp)
438 return qdisc_drop(skb, sch, to_free);
439 }
440
441 qdisc_qstats_backlog_inc(sch, skb);
442 sch->q.qlen++;
443
444 return qdisc_enqueue(skb, child, to_free);
445}
446
447static struct sk_buff *taprio_peek_soft(struct Qdisc *sch)
448{
449 struct taprio_sched *q = qdisc_priv(sch);
450 struct net_device *dev = qdisc_dev(sch);
451 struct sched_entry *entry;
452 struct sk_buff *skb;
453 u32 gate_mask;
454 int i;
455
456 rcu_read_lock();
457 entry = rcu_dereference(q->current_entry);
458 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
459 rcu_read_unlock();
460
461 if (!gate_mask)
462 return NULL;
463
464 for (i = 0; i < dev->num_tx_queues; i++) {
465 struct Qdisc *child = q->qdiscs[i];
466 int prio;
467 u8 tc;
468
469 if (unlikely(!child))
470 continue;
471
472 skb = child->ops->peek(child);
473 if (!skb)
474 continue;
475
476 if (TXTIME_ASSIST_IS_ENABLED(q->flags))
477 return skb;
478
479 prio = skb->priority;
480 tc = netdev_get_prio_tc_map(dev, prio);
481
482 if (!(gate_mask & BIT(tc)))
483 continue;
484
485 return skb;
486 }
487
488 return NULL;
489}
490
491static struct sk_buff *taprio_peek_offload(struct Qdisc *sch)
492{
493 struct taprio_sched *q = qdisc_priv(sch);
494 struct net_device *dev = qdisc_dev(sch);
495 struct sk_buff *skb;
496 int i;
497
498 for (i = 0; i < dev->num_tx_queues; i++) {
499 struct Qdisc *child = q->qdiscs[i];
500
501 if (unlikely(!child))
502 continue;
503
504 skb = child->ops->peek(child);
505 if (!skb)
506 continue;
507
508 return skb;
509 }
510
511 return NULL;
512}
513
514static struct sk_buff *taprio_peek(struct Qdisc *sch)
515{
516 struct taprio_sched *q = qdisc_priv(sch);
517
518 return q->peek(sch);
519}
520
521static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry)
522{
523 atomic_set(&entry->budget,
524 div64_u64((u64)entry->interval * 1000,
525 atomic64_read(&q->picos_per_byte)));
526}
527
528static struct sk_buff *taprio_dequeue_soft(struct Qdisc *sch)
529{
530 struct taprio_sched *q = qdisc_priv(sch);
531 struct net_device *dev = qdisc_dev(sch);
532 struct sk_buff *skb = NULL;
533 struct sched_entry *entry;
534 u32 gate_mask;
535 int i;
536
537 rcu_read_lock();
538 entry = rcu_dereference(q->current_entry);
539 /* if there's no entry, it means that the schedule didn't
540 * start yet, so force all gates to be open, this is in
541 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
542 * "AdminGateSates"
543 */
544 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
545
546 if (!gate_mask)
547 goto done;
548
549 for (i = 0; i < dev->num_tx_queues; i++) {
550 struct Qdisc *child = q->qdiscs[i];
551 ktime_t guard;
552 int prio;
553 int len;
554 u8 tc;
555
556 if (unlikely(!child))
557 continue;
558
559 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
560 skb = child->ops->dequeue(child);
561 if (!skb)
562 continue;
563 goto skb_found;
564 }
565
566 skb = child->ops->peek(child);
567 if (!skb)
568 continue;
569
570 prio = skb->priority;
571 tc = netdev_get_prio_tc_map(dev, prio);
572
573 if (!(gate_mask & BIT(tc))) {
574 skb = NULL;
575 continue;
576 }
577
578 len = qdisc_pkt_len(skb);
579 guard = ktime_add_ns(taprio_get_time(q),
580 length_to_duration(q, len));
581
582 /* In the case that there's no gate entry, there's no
583 * guard band ...
584 */
585 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
586 ktime_after(guard, entry->close_time)) {
587 skb = NULL;
588 continue;
589 }
590
591 /* ... and no budget. */
592 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
593 atomic_sub_return(len, &entry->budget) < 0) {
594 skb = NULL;
595 continue;
596 }
597
598 skb = child->ops->dequeue(child);
599 if (unlikely(!skb))
600 goto done;
601
602skb_found:
603 qdisc_bstats_update(sch, skb);
604 qdisc_qstats_backlog_dec(sch, skb);
605 sch->q.qlen--;
606
607 goto done;
608 }
609
610done:
611 rcu_read_unlock();
612
613 return skb;
614}
615
616static struct sk_buff *taprio_dequeue_offload(struct Qdisc *sch)
617{
618 struct taprio_sched *q = qdisc_priv(sch);
619 struct net_device *dev = qdisc_dev(sch);
620 struct sk_buff *skb;
621 int i;
622
623 for (i = 0; i < dev->num_tx_queues; i++) {
624 struct Qdisc *child = q->qdiscs[i];
625
626 if (unlikely(!child))
627 continue;
628
629 skb = child->ops->dequeue(child);
630 if (unlikely(!skb))
631 continue;
632
633 qdisc_bstats_update(sch, skb);
634 qdisc_qstats_backlog_dec(sch, skb);
635 sch->q.qlen--;
636
637 return skb;
638 }
639
640 return NULL;
641}
642
643static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
644{
645 struct taprio_sched *q = qdisc_priv(sch);
646
647 return q->dequeue(sch);
648}
649
650static bool should_restart_cycle(const struct sched_gate_list *oper,
651 const struct sched_entry *entry)
652{
653 if (list_is_last(&entry->list, &oper->entries))
654 return true;
655
656 if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0)
657 return true;
658
659 return false;
660}
661
662static bool should_change_schedules(const struct sched_gate_list *admin,
663 const struct sched_gate_list *oper,
664 ktime_t close_time)
665{
666 ktime_t next_base_time, extension_time;
667
668 if (!admin)
669 return false;
670
671 next_base_time = sched_base_time(admin);
672
673 /* This is the simple case, the close_time would fall after
674 * the next schedule base_time.
675 */
676 if (ktime_compare(next_base_time, close_time) <= 0)
677 return true;
678
679 /* This is the cycle_time_extension case, if the close_time
680 * plus the amount that can be extended would fall after the
681 * next schedule base_time, we can extend the current schedule
682 * for that amount.
683 */
684 extension_time = ktime_add_ns(close_time, oper->cycle_time_extension);
685
686 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
687 * how precisely the extension should be made. So after
688 * conformance testing, this logic may change.
689 */
690 if (ktime_compare(next_base_time, extension_time) <= 0)
691 return true;
692
693 return false;
694}
695
696static enum hrtimer_restart advance_sched(struct hrtimer *timer)
697{
698 struct taprio_sched *q = container_of(timer, struct taprio_sched,
699 advance_timer);
700 struct sched_gate_list *oper, *admin;
701 struct sched_entry *entry, *next;
702 struct Qdisc *sch = q->root;
703 ktime_t close_time;
704
705 spin_lock(&q->current_entry_lock);
706 entry = rcu_dereference_protected(q->current_entry,
707 lockdep_is_held(&q->current_entry_lock));
708 oper = rcu_dereference_protected(q->oper_sched,
709 lockdep_is_held(&q->current_entry_lock));
710 admin = rcu_dereference_protected(q->admin_sched,
711 lockdep_is_held(&q->current_entry_lock));
712
713 if (!oper)
714 switch_schedules(q, &admin, &oper);
715
716 /* This can happen in two cases: 1. this is the very first run
717 * of this function (i.e. we weren't running any schedule
718 * previously); 2. The previous schedule just ended. The first
719 * entry of all schedules are pre-calculated during the
720 * schedule initialization.
721 */
722 if (unlikely(!entry || entry->close_time == oper->base_time)) {
723 next = list_first_entry(&oper->entries, struct sched_entry,
724 list);
725 close_time = next->close_time;
726 goto first_run;
727 }
728
729 if (should_restart_cycle(oper, entry)) {
730 next = list_first_entry(&oper->entries, struct sched_entry,
731 list);
732 oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time,
733 oper->cycle_time);
734 } else {
735 next = list_next_entry(entry, list);
736 }
737
738 close_time = ktime_add_ns(entry->close_time, next->interval);
739 close_time = min_t(ktime_t, close_time, oper->cycle_close_time);
740
741 if (should_change_schedules(admin, oper, close_time)) {
742 /* Set things so the next time this runs, the new
743 * schedule runs.
744 */
745 close_time = sched_base_time(admin);
746 switch_schedules(q, &admin, &oper);
747 }
748
749 next->close_time = close_time;
750 taprio_set_budget(q, next);
751
752first_run:
753 rcu_assign_pointer(q->current_entry, next);
754 spin_unlock(&q->current_entry_lock);
755
756 hrtimer_set_expires(&q->advance_timer, close_time);
757
758 rcu_read_lock();
759 __netif_schedule(sch);
760 rcu_read_unlock();
761
762 return HRTIMER_RESTART;
763}
764
765static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
766 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
767 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
768 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
769 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
770};
771
772static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
773 [TCA_TAPRIO_ATTR_PRIOMAP] = {
774 .len = sizeof(struct tc_mqprio_qopt)
775 },
776 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
777 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
778 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
779 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
780 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 },
781 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
782 [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 },
783 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 },
784};
785
786static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
787 struct sched_entry *entry,
788 struct netlink_ext_ack *extack)
789{
790 int min_duration = length_to_duration(q, ETH_ZLEN);
791 u32 interval = 0;
792
793 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
794 entry->command = nla_get_u8(
795 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
796
797 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
798 entry->gate_mask = nla_get_u32(
799 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
800
801 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
802 interval = nla_get_u32(
803 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
804
805 /* The interval should allow at least the minimum ethernet
806 * frame to go out.
807 */
808 if (interval < min_duration) {
809 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
810 return -EINVAL;
811 }
812
813 entry->interval = interval;
814
815 return 0;
816}
817
818static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
819 struct sched_entry *entry, int index,
820 struct netlink_ext_ack *extack)
821{
822 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
823 int err;
824
825 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
826 entry_policy, NULL);
827 if (err < 0) {
828 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
829 return -EINVAL;
830 }
831
832 entry->index = index;
833
834 return fill_sched_entry(q, tb, entry, extack);
835}
836
837static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
838 struct sched_gate_list *sched,
839 struct netlink_ext_ack *extack)
840{
841 struct nlattr *n;
842 int err, rem;
843 int i = 0;
844
845 if (!list)
846 return -EINVAL;
847
848 nla_for_each_nested(n, list, rem) {
849 struct sched_entry *entry;
850
851 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
852 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
853 continue;
854 }
855
856 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
857 if (!entry) {
858 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
859 return -ENOMEM;
860 }
861
862 err = parse_sched_entry(q, n, entry, i, extack);
863 if (err < 0) {
864 kfree(entry);
865 return err;
866 }
867
868 list_add_tail(&entry->list, &sched->entries);
869 i++;
870 }
871
872 sched->num_entries = i;
873
874 return i;
875}
876
877static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
878 struct sched_gate_list *new,
879 struct netlink_ext_ack *extack)
880{
881 int err = 0;
882
883 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
884 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
885 return -ENOTSUPP;
886 }
887
888 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
889 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
890
891 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
892 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
893
894 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
895 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
896
897 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
898 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
899 new, extack);
900 if (err < 0)
901 return err;
902
903 if (!new->cycle_time) {
904 struct sched_entry *entry;
905 ktime_t cycle = 0;
906
907 list_for_each_entry(entry, &new->entries, list)
908 cycle = ktime_add_ns(cycle, entry->interval);
909
910 if (!cycle) {
911 NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
912 return -EINVAL;
913 }
914
915 new->cycle_time = cycle;
916 }
917
918 return 0;
919}
920
921static int taprio_parse_mqprio_opt(struct net_device *dev,
922 struct tc_mqprio_qopt *qopt,
923 struct netlink_ext_ack *extack,
924 u32 taprio_flags)
925{
926 int i, j;
927
928 if (!qopt) {
929 if (!dev->num_tc) {
930 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
931 return -EINVAL;
932 }
933 return 0;
934 }
935
936 /* Verify num_tc is not out of max range */
937 if (qopt->num_tc > TC_MAX_QUEUE) {
938 NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
939 return -EINVAL;
940 }
941
942 /* taprio imposes that traffic classes map 1:n to tx queues */
943 if (qopt->num_tc > dev->num_tx_queues) {
944 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
945 return -EINVAL;
946 }
947
948 /* Verify priority mapping uses valid tcs */
949 for (i = 0; i <= TC_BITMASK; i++) {
950 if (qopt->prio_tc_map[i] >= qopt->num_tc) {
951 NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
952 return -EINVAL;
953 }
954 }
955
956 for (i = 0; i < qopt->num_tc; i++) {
957 unsigned int last = qopt->offset[i] + qopt->count[i];
958
959 /* Verify the queue count is in tx range being equal to the
960 * real_num_tx_queues indicates the last queue is in use.
961 */
962 if (qopt->offset[i] >= dev->num_tx_queues ||
963 !qopt->count[i] ||
964 last > dev->real_num_tx_queues) {
965 NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
966 return -EINVAL;
967 }
968
969 if (TXTIME_ASSIST_IS_ENABLED(taprio_flags))
970 continue;
971
972 /* Verify that the offset and counts do not overlap */
973 for (j = i + 1; j < qopt->num_tc; j++) {
974 if (last > qopt->offset[j]) {
975 NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
976 return -EINVAL;
977 }
978 }
979 }
980
981 return 0;
982}
983
984static int taprio_get_start_time(struct Qdisc *sch,
985 struct sched_gate_list *sched,
986 ktime_t *start)
987{
988 struct taprio_sched *q = qdisc_priv(sch);
989 ktime_t now, base, cycle;
990 s64 n;
991
992 base = sched_base_time(sched);
993 now = taprio_get_time(q);
994
995 if (ktime_after(base, now)) {
996 *start = base;
997 return 0;
998 }
999
1000 cycle = sched->cycle_time;
1001
1002 /* The qdisc is expected to have at least one sched_entry. Moreover,
1003 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
1004 * something went really wrong. In that case, we should warn about this
1005 * inconsistent state and return error.
1006 */
1007 if (WARN_ON(!cycle))
1008 return -EFAULT;
1009
1010 /* Schedule the start time for the beginning of the next
1011 * cycle.
1012 */
1013 n = div64_s64(ktime_sub_ns(now, base), cycle);
1014 *start = ktime_add_ns(base, (n + 1) * cycle);
1015 return 0;
1016}
1017
1018static void setup_first_close_time(struct taprio_sched *q,
1019 struct sched_gate_list *sched, ktime_t base)
1020{
1021 struct sched_entry *first;
1022 ktime_t cycle;
1023
1024 first = list_first_entry(&sched->entries,
1025 struct sched_entry, list);
1026
1027 cycle = sched->cycle_time;
1028
1029 /* FIXME: find a better place to do this */
1030 sched->cycle_close_time = ktime_add_ns(base, cycle);
1031
1032 first->close_time = ktime_add_ns(base, first->interval);
1033 taprio_set_budget(q, first);
1034 rcu_assign_pointer(q->current_entry, NULL);
1035}
1036
1037static void taprio_start_sched(struct Qdisc *sch,
1038 ktime_t start, struct sched_gate_list *new)
1039{
1040 struct taprio_sched *q = qdisc_priv(sch);
1041 ktime_t expires;
1042
1043 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1044 return;
1045
1046 expires = hrtimer_get_expires(&q->advance_timer);
1047 if (expires == 0)
1048 expires = KTIME_MAX;
1049
1050 /* If the new schedule starts before the next expiration, we
1051 * reprogram it to the earliest one, so we change the admin
1052 * schedule to the operational one at the right time.
1053 */
1054 start = min_t(ktime_t, start, expires);
1055
1056 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1057}
1058
1059static void taprio_set_picos_per_byte(struct net_device *dev,
1060 struct taprio_sched *q)
1061{
1062 struct ethtool_link_ksettings ecmd;
1063 int speed = SPEED_10;
1064 int picos_per_byte;
1065 int err;
1066
1067 err = __ethtool_get_link_ksettings(dev, &ecmd);
1068 if (err < 0)
1069 goto skip;
1070
1071 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1072 speed = ecmd.base.speed;
1073
1074skip:
1075 picos_per_byte = (USEC_PER_SEC * 8) / speed;
1076
1077 atomic64_set(&q->picos_per_byte, picos_per_byte);
1078 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1079 dev->name, (long long)atomic64_read(&q->picos_per_byte),
1080 ecmd.base.speed);
1081}
1082
1083static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1084 void *ptr)
1085{
1086 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1087 struct net_device *qdev;
1088 struct taprio_sched *q;
1089 bool found = false;
1090
1091 ASSERT_RTNL();
1092
1093 if (event != NETDEV_UP && event != NETDEV_CHANGE)
1094 return NOTIFY_DONE;
1095
1096 spin_lock(&taprio_list_lock);
1097 list_for_each_entry(q, &taprio_list, taprio_list) {
1098 qdev = qdisc_dev(q->root);
1099 if (qdev == dev) {
1100 found = true;
1101 break;
1102 }
1103 }
1104 spin_unlock(&taprio_list_lock);
1105
1106 if (found)
1107 taprio_set_picos_per_byte(dev, q);
1108
1109 return NOTIFY_DONE;
1110}
1111
1112static void setup_txtime(struct taprio_sched *q,
1113 struct sched_gate_list *sched, ktime_t base)
1114{
1115 struct sched_entry *entry;
1116 u32 interval = 0;
1117
1118 list_for_each_entry(entry, &sched->entries, list) {
1119 entry->next_txtime = ktime_add_ns(base, interval);
1120 interval += entry->interval;
1121 }
1122}
1123
1124static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1125{
1126 size_t size = sizeof(struct tc_taprio_sched_entry) * num_entries +
1127 sizeof(struct __tc_taprio_qopt_offload);
1128 struct __tc_taprio_qopt_offload *__offload;
1129
1130 __offload = kzalloc(size, GFP_KERNEL);
1131 if (!__offload)
1132 return NULL;
1133
1134 refcount_set(&__offload->users, 1);
1135
1136 return &__offload->offload;
1137}
1138
1139struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1140 *offload)
1141{
1142 struct __tc_taprio_qopt_offload *__offload;
1143
1144 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1145 offload);
1146
1147 refcount_inc(&__offload->users);
1148
1149 return offload;
1150}
1151EXPORT_SYMBOL_GPL(taprio_offload_get);
1152
1153void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1154{
1155 struct __tc_taprio_qopt_offload *__offload;
1156
1157 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1158 offload);
1159
1160 if (!refcount_dec_and_test(&__offload->users))
1161 return;
1162
1163 kfree(__offload);
1164}
1165EXPORT_SYMBOL_GPL(taprio_offload_free);
1166
1167/* The function will only serve to keep the pointers to the "oper" and "admin"
1168 * schedules valid in relation to their base times, so when calling dump() the
1169 * users looks at the right schedules.
1170 * When using full offload, the admin configuration is promoted to oper at the
1171 * base_time in the PHC time domain. But because the system time is not
1172 * necessarily in sync with that, we can't just trigger a hrtimer to call
1173 * switch_schedules at the right hardware time.
1174 * At the moment we call this by hand right away from taprio, but in the future
1175 * it will be useful to create a mechanism for drivers to notify taprio of the
1176 * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1177 * This is left as TODO.
1178 */
1179static void taprio_offload_config_changed(struct taprio_sched *q)
1180{
1181 struct sched_gate_list *oper, *admin;
1182
1183 spin_lock(&q->current_entry_lock);
1184
1185 oper = rcu_dereference_protected(q->oper_sched,
1186 lockdep_is_held(&q->current_entry_lock));
1187 admin = rcu_dereference_protected(q->admin_sched,
1188 lockdep_is_held(&q->current_entry_lock));
1189
1190 switch_schedules(q, &admin, &oper);
1191
1192 spin_unlock(&q->current_entry_lock);
1193}
1194
1195static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1196{
1197 u32 i, queue_mask = 0;
1198
1199 for (i = 0; i < dev->num_tc; i++) {
1200 u32 offset, count;
1201
1202 if (!(tc_mask & BIT(i)))
1203 continue;
1204
1205 offset = dev->tc_to_txq[i].offset;
1206 count = dev->tc_to_txq[i].count;
1207
1208 queue_mask |= GENMASK(offset + count - 1, offset);
1209 }
1210
1211 return queue_mask;
1212}
1213
1214static void taprio_sched_to_offload(struct net_device *dev,
1215 struct sched_gate_list *sched,
1216 struct tc_taprio_qopt_offload *offload)
1217{
1218 struct sched_entry *entry;
1219 int i = 0;
1220
1221 offload->base_time = sched->base_time;
1222 offload->cycle_time = sched->cycle_time;
1223 offload->cycle_time_extension = sched->cycle_time_extension;
1224
1225 list_for_each_entry(entry, &sched->entries, list) {
1226 struct tc_taprio_sched_entry *e = &offload->entries[i];
1227
1228 e->command = entry->command;
1229 e->interval = entry->interval;
1230 e->gate_mask = tc_map_to_queue_mask(dev, entry->gate_mask);
1231
1232 i++;
1233 }
1234
1235 offload->num_entries = i;
1236}
1237
1238static int taprio_enable_offload(struct net_device *dev,
1239 struct taprio_sched *q,
1240 struct sched_gate_list *sched,
1241 struct netlink_ext_ack *extack)
1242{
1243 const struct net_device_ops *ops = dev->netdev_ops;
1244 struct tc_taprio_qopt_offload *offload;
1245 int err = 0;
1246
1247 if (!ops->ndo_setup_tc) {
1248 NL_SET_ERR_MSG(extack,
1249 "Device does not support taprio offload");
1250 return -EOPNOTSUPP;
1251 }
1252
1253 offload = taprio_offload_alloc(sched->num_entries);
1254 if (!offload) {
1255 NL_SET_ERR_MSG(extack,
1256 "Not enough memory for enabling offload mode");
1257 return -ENOMEM;
1258 }
1259 offload->enable = 1;
1260 taprio_sched_to_offload(dev, sched, offload);
1261
1262 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1263 if (err < 0) {
1264 NL_SET_ERR_MSG(extack,
1265 "Device failed to setup taprio offload");
1266 goto done;
1267 }
1268
1269 q->offloaded = true;
1270
1271done:
1272 taprio_offload_free(offload);
1273
1274 return err;
1275}
1276
1277static int taprio_disable_offload(struct net_device *dev,
1278 struct taprio_sched *q,
1279 struct netlink_ext_ack *extack)
1280{
1281 const struct net_device_ops *ops = dev->netdev_ops;
1282 struct tc_taprio_qopt_offload *offload;
1283 int err;
1284
1285 if (!q->offloaded)
1286 return 0;
1287
1288 offload = taprio_offload_alloc(0);
1289 if (!offload) {
1290 NL_SET_ERR_MSG(extack,
1291 "Not enough memory to disable offload mode");
1292 return -ENOMEM;
1293 }
1294 offload->enable = 0;
1295
1296 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1297 if (err < 0) {
1298 NL_SET_ERR_MSG(extack,
1299 "Device failed to disable offload");
1300 goto out;
1301 }
1302
1303 q->offloaded = false;
1304
1305out:
1306 taprio_offload_free(offload);
1307
1308 return err;
1309}
1310
1311/* If full offload is enabled, the only possible clockid is the net device's
1312 * PHC. For that reason, specifying a clockid through netlink is incorrect.
1313 * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1314 * in sync with the specified clockid via a user space daemon such as phc2sys.
1315 * For both software taprio and txtime-assist, the clockid is used for the
1316 * hrtimer that advances the schedule and hence mandatory.
1317 */
1318static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1319 struct netlink_ext_ack *extack)
1320{
1321 struct taprio_sched *q = qdisc_priv(sch);
1322 struct net_device *dev = qdisc_dev(sch);
1323 int err = -EINVAL;
1324
1325 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1326 const struct ethtool_ops *ops = dev->ethtool_ops;
1327 struct ethtool_ts_info info = {
1328 .cmd = ETHTOOL_GET_TS_INFO,
1329 .phc_index = -1,
1330 };
1331
1332 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1333 NL_SET_ERR_MSG(extack,
1334 "The 'clockid' cannot be specified for full offload");
1335 goto out;
1336 }
1337
1338 if (ops && ops->get_ts_info)
1339 err = ops->get_ts_info(dev, &info);
1340
1341 if (err || info.phc_index < 0) {
1342 NL_SET_ERR_MSG(extack,
1343 "Device does not have a PTP clock");
1344 err = -ENOTSUPP;
1345 goto out;
1346 }
1347 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1348 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1349 enum tk_offsets tk_offset;
1350
1351 /* We only support static clockids and we don't allow
1352 * for it to be modified after the first init.
1353 */
1354 if (clockid < 0 ||
1355 (q->clockid != -1 && q->clockid != clockid)) {
1356 NL_SET_ERR_MSG(extack,
1357 "Changing the 'clockid' of a running schedule is not supported");
1358 err = -ENOTSUPP;
1359 goto out;
1360 }
1361
1362 switch (clockid) {
1363 case CLOCK_REALTIME:
1364 tk_offset = TK_OFFS_REAL;
1365 break;
1366 case CLOCK_MONOTONIC:
1367 tk_offset = TK_OFFS_MAX;
1368 break;
1369 case CLOCK_BOOTTIME:
1370 tk_offset = TK_OFFS_BOOT;
1371 break;
1372 case CLOCK_TAI:
1373 tk_offset = TK_OFFS_TAI;
1374 break;
1375 default:
1376 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1377 err = -EINVAL;
1378 goto out;
1379 }
1380 /* This pairs with READ_ONCE() in taprio_mono_to_any */
1381 WRITE_ONCE(q->tk_offset, tk_offset);
1382
1383 q->clockid = clockid;
1384 } else {
1385 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1386 goto out;
1387 }
1388
1389 /* Everything went ok, return success. */
1390 err = 0;
1391
1392out:
1393 return err;
1394}
1395
1396static int taprio_mqprio_cmp(const struct net_device *dev,
1397 const struct tc_mqprio_qopt *mqprio)
1398{
1399 int i;
1400
1401 if (!mqprio || mqprio->num_tc != dev->num_tc)
1402 return -1;
1403
1404 for (i = 0; i < mqprio->num_tc; i++)
1405 if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1406 dev->tc_to_txq[i].offset != mqprio->offset[i])
1407 return -1;
1408
1409 for (i = 0; i <= TC_BITMASK; i++)
1410 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1411 return -1;
1412
1413 return 0;
1414}
1415
1416/* The semantics of the 'flags' argument in relation to 'change()'
1417 * requests, are interpreted following two rules (which are applied in
1418 * this order): (1) an omitted 'flags' argument is interpreted as
1419 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1420 * changed.
1421 */
1422static int taprio_new_flags(const struct nlattr *attr, u32 old,
1423 struct netlink_ext_ack *extack)
1424{
1425 u32 new = 0;
1426
1427 if (attr)
1428 new = nla_get_u32(attr);
1429
1430 if (old != TAPRIO_FLAGS_INVALID && old != new) {
1431 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1432 return -EOPNOTSUPP;
1433 }
1434
1435 if (!taprio_flags_valid(new)) {
1436 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1437 return -EINVAL;
1438 }
1439
1440 return new;
1441}
1442
1443static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1444 struct netlink_ext_ack *extack)
1445{
1446 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1447 struct sched_gate_list *oper, *admin, *new_admin;
1448 struct taprio_sched *q = qdisc_priv(sch);
1449 struct net_device *dev = qdisc_dev(sch);
1450 struct tc_mqprio_qopt *mqprio = NULL;
1451 unsigned long flags;
1452 ktime_t start;
1453 int i, err;
1454
1455 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1456 taprio_policy, extack);
1457 if (err < 0)
1458 return err;
1459
1460 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1461 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1462
1463 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1464 q->flags, extack);
1465 if (err < 0)
1466 return err;
1467
1468 q->flags = err;
1469
1470 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1471 if (err < 0)
1472 return err;
1473
1474 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1475 if (!new_admin) {
1476 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1477 return -ENOMEM;
1478 }
1479 INIT_LIST_HEAD(&new_admin->entries);
1480
1481 rcu_read_lock();
1482 oper = rcu_dereference(q->oper_sched);
1483 admin = rcu_dereference(q->admin_sched);
1484 rcu_read_unlock();
1485
1486 /* no changes - no new mqprio settings */
1487 if (!taprio_mqprio_cmp(dev, mqprio))
1488 mqprio = NULL;
1489
1490 if (mqprio && (oper || admin)) {
1491 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1492 err = -ENOTSUPP;
1493 goto free_sched;
1494 }
1495
1496 err = parse_taprio_schedule(q, tb, new_admin, extack);
1497 if (err < 0)
1498 goto free_sched;
1499
1500 if (new_admin->num_entries == 0) {
1501 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1502 err = -EINVAL;
1503 goto free_sched;
1504 }
1505
1506 err = taprio_parse_clockid(sch, tb, extack);
1507 if (err < 0)
1508 goto free_sched;
1509
1510 taprio_set_picos_per_byte(dev, q);
1511
1512 if (mqprio) {
1513 err = netdev_set_num_tc(dev, mqprio->num_tc);
1514 if (err)
1515 goto free_sched;
1516 for (i = 0; i < mqprio->num_tc; i++)
1517 netdev_set_tc_queue(dev, i,
1518 mqprio->count[i],
1519 mqprio->offset[i]);
1520
1521 /* Always use supplied priority mappings */
1522 for (i = 0; i <= TC_BITMASK; i++)
1523 netdev_set_prio_tc_map(dev, i,
1524 mqprio->prio_tc_map[i]);
1525 }
1526
1527 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1528 err = taprio_enable_offload(dev, q, new_admin, extack);
1529 else
1530 err = taprio_disable_offload(dev, q, extack);
1531 if (err)
1532 goto free_sched;
1533
1534 /* Protects against enqueue()/dequeue() */
1535 spin_lock_bh(qdisc_lock(sch));
1536
1537 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1538 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1539 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1540 err = -EINVAL;
1541 goto unlock;
1542 }
1543
1544 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1545 }
1546
1547 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1548 !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1549 !hrtimer_active(&q->advance_timer)) {
1550 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1551 q->advance_timer.function = advance_sched;
1552 }
1553
1554 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1555 q->dequeue = taprio_dequeue_offload;
1556 q->peek = taprio_peek_offload;
1557 } else {
1558 /* Be sure to always keep the function pointers
1559 * in a consistent state.
1560 */
1561 q->dequeue = taprio_dequeue_soft;
1562 q->peek = taprio_peek_soft;
1563 }
1564
1565 err = taprio_get_start_time(sch, new_admin, &start);
1566 if (err < 0) {
1567 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1568 goto unlock;
1569 }
1570
1571 setup_txtime(q, new_admin, start);
1572
1573 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1574 if (!oper) {
1575 rcu_assign_pointer(q->oper_sched, new_admin);
1576 err = 0;
1577 new_admin = NULL;
1578 goto unlock;
1579 }
1580
1581 /* Not going to race against advance_sched(), but still */
1582 admin = rcu_replace_pointer(q->admin_sched, new_admin,
1583 lockdep_rtnl_is_held());
1584 if (admin)
1585 call_rcu(&admin->rcu, taprio_free_sched_cb);
1586 } else {
1587 setup_first_close_time(q, new_admin, start);
1588
1589 /* Protects against advance_sched() */
1590 spin_lock_irqsave(&q->current_entry_lock, flags);
1591
1592 taprio_start_sched(sch, start, new_admin);
1593
1594 admin = rcu_replace_pointer(q->admin_sched, new_admin,
1595 lockdep_rtnl_is_held());
1596 if (admin)
1597 call_rcu(&admin->rcu, taprio_free_sched_cb);
1598
1599 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1600
1601 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1602 taprio_offload_config_changed(q);
1603 }
1604
1605 new_admin = NULL;
1606 err = 0;
1607
1608unlock:
1609 spin_unlock_bh(qdisc_lock(sch));
1610
1611free_sched:
1612 if (new_admin)
1613 call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1614
1615 return err;
1616}
1617
1618static void taprio_reset(struct Qdisc *sch)
1619{
1620 struct taprio_sched *q = qdisc_priv(sch);
1621 struct net_device *dev = qdisc_dev(sch);
1622 int i;
1623
1624 hrtimer_cancel(&q->advance_timer);
1625
1626 if (q->qdiscs) {
1627 for (i = 0; i < dev->num_tx_queues && q->qdiscs[i]; i++)
1628 qdisc_reset(q->qdiscs[i]);
1629 }
1630 sch->qstats.backlog = 0;
1631 sch->q.qlen = 0;
1632}
1633
1634static void taprio_destroy(struct Qdisc *sch)
1635{
1636 struct taprio_sched *q = qdisc_priv(sch);
1637 struct net_device *dev = qdisc_dev(sch);
1638 unsigned int i;
1639
1640 spin_lock(&taprio_list_lock);
1641 list_del(&q->taprio_list);
1642 spin_unlock(&taprio_list_lock);
1643
1644 /* Note that taprio_reset() might not be called if an error
1645 * happens in qdisc_create(), after taprio_init() has been called.
1646 */
1647 hrtimer_cancel(&q->advance_timer);
1648 qdisc_synchronize(sch);
1649
1650 taprio_disable_offload(dev, q, NULL);
1651
1652 if (q->qdiscs) {
1653 for (i = 0; i < dev->num_tx_queues; i++)
1654 qdisc_put(q->qdiscs[i]);
1655
1656 kfree(q->qdiscs);
1657 }
1658 q->qdiscs = NULL;
1659
1660 netdev_reset_tc(dev);
1661
1662 if (q->oper_sched)
1663 call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb);
1664
1665 if (q->admin_sched)
1666 call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb);
1667}
1668
1669static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1670 struct netlink_ext_ack *extack)
1671{
1672 struct taprio_sched *q = qdisc_priv(sch);
1673 struct net_device *dev = qdisc_dev(sch);
1674 int i;
1675
1676 spin_lock_init(&q->current_entry_lock);
1677
1678 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1679 q->advance_timer.function = advance_sched;
1680
1681 q->dequeue = taprio_dequeue_soft;
1682 q->peek = taprio_peek_soft;
1683
1684 q->root = sch;
1685
1686 /* We only support static clockids. Use an invalid value as default
1687 * and get the valid one on taprio_change().
1688 */
1689 q->clockid = -1;
1690 q->flags = TAPRIO_FLAGS_INVALID;
1691
1692 spin_lock(&taprio_list_lock);
1693 list_add(&q->taprio_list, &taprio_list);
1694 spin_unlock(&taprio_list_lock);
1695
1696 if (sch->parent != TC_H_ROOT)
1697 return -EOPNOTSUPP;
1698
1699 if (!netif_is_multiqueue(dev))
1700 return -EOPNOTSUPP;
1701
1702 /* pre-allocate qdisc, attachment can't fail */
1703 q->qdiscs = kcalloc(dev->num_tx_queues,
1704 sizeof(q->qdiscs[0]),
1705 GFP_KERNEL);
1706
1707 if (!q->qdiscs)
1708 return -ENOMEM;
1709
1710 if (!opt)
1711 return -EINVAL;
1712
1713 for (i = 0; i < dev->num_tx_queues; i++) {
1714 struct netdev_queue *dev_queue;
1715 struct Qdisc *qdisc;
1716
1717 dev_queue = netdev_get_tx_queue(dev, i);
1718 qdisc = qdisc_create_dflt(dev_queue,
1719 &pfifo_qdisc_ops,
1720 TC_H_MAKE(TC_H_MAJ(sch->handle),
1721 TC_H_MIN(i + 1)),
1722 extack);
1723 if (!qdisc)
1724 return -ENOMEM;
1725
1726 if (i < dev->real_num_tx_queues)
1727 qdisc_hash_add(qdisc, false);
1728
1729 q->qdiscs[i] = qdisc;
1730 }
1731
1732 return taprio_change(sch, opt, extack);
1733}
1734
1735static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1736 unsigned long cl)
1737{
1738 struct net_device *dev = qdisc_dev(sch);
1739 unsigned long ntx = cl - 1;
1740
1741 if (ntx >= dev->num_tx_queues)
1742 return NULL;
1743
1744 return netdev_get_tx_queue(dev, ntx);
1745}
1746
1747static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1748 struct Qdisc *new, struct Qdisc **old,
1749 struct netlink_ext_ack *extack)
1750{
1751 struct taprio_sched *q = qdisc_priv(sch);
1752 struct net_device *dev = qdisc_dev(sch);
1753 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1754
1755 if (!dev_queue)
1756 return -EINVAL;
1757
1758 if (dev->flags & IFF_UP)
1759 dev_deactivate(dev);
1760
1761 *old = q->qdiscs[cl - 1];
1762 q->qdiscs[cl - 1] = new;
1763
1764 if (new)
1765 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1766
1767 if (dev->flags & IFF_UP)
1768 dev_activate(dev);
1769
1770 return 0;
1771}
1772
1773static int dump_entry(struct sk_buff *msg,
1774 const struct sched_entry *entry)
1775{
1776 struct nlattr *item;
1777
1778 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1779 if (!item)
1780 return -ENOSPC;
1781
1782 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1783 goto nla_put_failure;
1784
1785 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1786 goto nla_put_failure;
1787
1788 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1789 entry->gate_mask))
1790 goto nla_put_failure;
1791
1792 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1793 entry->interval))
1794 goto nla_put_failure;
1795
1796 return nla_nest_end(msg, item);
1797
1798nla_put_failure:
1799 nla_nest_cancel(msg, item);
1800 return -1;
1801}
1802
1803static int dump_schedule(struct sk_buff *msg,
1804 const struct sched_gate_list *root)
1805{
1806 struct nlattr *entry_list;
1807 struct sched_entry *entry;
1808
1809 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1810 root->base_time, TCA_TAPRIO_PAD))
1811 return -1;
1812
1813 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1814 root->cycle_time, TCA_TAPRIO_PAD))
1815 return -1;
1816
1817 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1818 root->cycle_time_extension, TCA_TAPRIO_PAD))
1819 return -1;
1820
1821 entry_list = nla_nest_start_noflag(msg,
1822 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1823 if (!entry_list)
1824 goto error_nest;
1825
1826 list_for_each_entry(entry, &root->entries, list) {
1827 if (dump_entry(msg, entry) < 0)
1828 goto error_nest;
1829 }
1830
1831 nla_nest_end(msg, entry_list);
1832 return 0;
1833
1834error_nest:
1835 nla_nest_cancel(msg, entry_list);
1836 return -1;
1837}
1838
1839static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1840{
1841 struct taprio_sched *q = qdisc_priv(sch);
1842 struct net_device *dev = qdisc_dev(sch);
1843 struct sched_gate_list *oper, *admin;
1844 struct tc_mqprio_qopt opt = { 0 };
1845 struct nlattr *nest, *sched_nest;
1846 unsigned int i;
1847
1848 rcu_read_lock();
1849 oper = rcu_dereference(q->oper_sched);
1850 admin = rcu_dereference(q->admin_sched);
1851
1852 opt.num_tc = netdev_get_num_tc(dev);
1853 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
1854
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;
1858 }
1859
1860 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1861 if (!nest)
1862 goto start_error;
1863
1864 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
1865 goto options_error;
1866
1867 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1868 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
1869 goto options_error;
1870
1871 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
1872 goto options_error;
1873
1874 if (q->txtime_delay &&
1875 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
1876 goto options_error;
1877
1878 if (oper && dump_schedule(skb, oper))
1879 goto options_error;
1880
1881 if (!admin)
1882 goto done;
1883
1884 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
1885 if (!sched_nest)
1886 goto options_error;
1887
1888 if (dump_schedule(skb, admin))
1889 goto admin_error;
1890
1891 nla_nest_end(skb, sched_nest);
1892
1893done:
1894 rcu_read_unlock();
1895
1896 return nla_nest_end(skb, nest);
1897
1898admin_error:
1899 nla_nest_cancel(skb, sched_nest);
1900
1901options_error:
1902 nla_nest_cancel(skb, nest);
1903
1904start_error:
1905 rcu_read_unlock();
1906 return -ENOSPC;
1907}
1908
1909static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
1910{
1911 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1912
1913 if (!dev_queue)
1914 return NULL;
1915
1916 return dev_queue->qdisc_sleeping;
1917}
1918
1919static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
1920{
1921 unsigned int ntx = TC_H_MIN(classid);
1922
1923 if (!taprio_queue_get(sch, ntx))
1924 return 0;
1925 return ntx;
1926}
1927
1928static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
1929 struct sk_buff *skb, struct tcmsg *tcm)
1930{
1931 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1932
1933 tcm->tcm_parent = TC_H_ROOT;
1934 tcm->tcm_handle |= TC_H_MIN(cl);
1935 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
1936
1937 return 0;
1938}
1939
1940static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
1941 struct gnet_dump *d)
1942 __releases(d->lock)
1943 __acquires(d->lock)
1944{
1945 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1946
1947 sch = dev_queue->qdisc_sleeping;
1948 if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 ||
1949 qdisc_qstats_copy(d, sch) < 0)
1950 return -1;
1951 return 0;
1952}
1953
1954static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1955{
1956 struct net_device *dev = qdisc_dev(sch);
1957 unsigned long ntx;
1958
1959 if (arg->stop)
1960 return;
1961
1962 arg->count = arg->skip;
1963 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
1964 if (arg->fn(sch, ntx + 1, arg) < 0) {
1965 arg->stop = 1;
1966 break;
1967 }
1968 arg->count++;
1969 }
1970}
1971
1972static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
1973 struct tcmsg *tcm)
1974{
1975 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
1976}
1977
1978static const struct Qdisc_class_ops taprio_class_ops = {
1979 .graft = taprio_graft,
1980 .leaf = taprio_leaf,
1981 .find = taprio_find,
1982 .walk = taprio_walk,
1983 .dump = taprio_dump_class,
1984 .dump_stats = taprio_dump_class_stats,
1985 .select_queue = taprio_select_queue,
1986};
1987
1988static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
1989 .cl_ops = &taprio_class_ops,
1990 .id = "taprio",
1991 .priv_size = sizeof(struct taprio_sched),
1992 .init = taprio_init,
1993 .change = taprio_change,
1994 .destroy = taprio_destroy,
1995 .reset = taprio_reset,
1996 .peek = taprio_peek,
1997 .dequeue = taprio_dequeue,
1998 .enqueue = taprio_enqueue,
1999 .dump = taprio_dump,
2000 .owner = THIS_MODULE,
2001};
2002
2003static struct notifier_block taprio_device_notifier = {
2004 .notifier_call = taprio_dev_notifier,
2005};
2006
2007static int __init taprio_module_init(void)
2008{
2009 int err = register_netdevice_notifier(&taprio_device_notifier);
2010
2011 if (err)
2012 return err;
2013
2014 return register_qdisc(&taprio_qdisc_ops);
2015}
2016
2017static void __exit taprio_module_exit(void)
2018{
2019 unregister_qdisc(&taprio_qdisc_ops);
2020 unregister_netdevice_notifier(&taprio_device_notifier);
2021}
2022
2023module_init(taprio_module_init);
2024module_exit(taprio_module_exit);
2025MODULE_LICENSE("GPL");