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