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