xref: /openbmc/linux/net/sched/sch_taprio.c (revision 8bf3cbe32b180836720f735e6de5dee700052317)
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 FLAGS_VALID(flags) (!((flags) & ~TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST))
33 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
34 
35 struct sched_entry {
36 	struct list_head list;
37 
38 	/* The instant that this entry "closes" and the next one
39 	 * should open, the qdisc will make some effort so that no
40 	 * packet leaves after this time.
41 	 */
42 	ktime_t close_time;
43 	ktime_t next_txtime;
44 	atomic_t budget;
45 	int index;
46 	u32 gate_mask;
47 	u32 interval;
48 	u8 command;
49 };
50 
51 struct sched_gate_list {
52 	struct rcu_head rcu;
53 	struct list_head entries;
54 	size_t num_entries;
55 	ktime_t cycle_close_time;
56 	s64 cycle_time;
57 	s64 cycle_time_extension;
58 	s64 base_time;
59 };
60 
61 struct taprio_sched {
62 	struct Qdisc **qdiscs;
63 	struct Qdisc *root;
64 	u32 flags;
65 	enum tk_offsets tk_offset;
66 	int clockid;
67 	atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
68 				    * speeds it's sub-nanoseconds per byte
69 				    */
70 
71 	/* Protects the update side of the RCU protected current_entry */
72 	spinlock_t current_entry_lock;
73 	struct sched_entry __rcu *current_entry;
74 	struct sched_gate_list __rcu *oper_sched;
75 	struct sched_gate_list __rcu *admin_sched;
76 	struct hrtimer advance_timer;
77 	struct list_head taprio_list;
78 	u32 txtime_delay;
79 };
80 
81 static ktime_t sched_base_time(const struct sched_gate_list *sched)
82 {
83 	if (!sched)
84 		return KTIME_MAX;
85 
86 	return ns_to_ktime(sched->base_time);
87 }
88 
89 static ktime_t taprio_get_time(struct taprio_sched *q)
90 {
91 	ktime_t mono = ktime_get();
92 
93 	switch (q->tk_offset) {
94 	case TK_OFFS_MAX:
95 		return mono;
96 	default:
97 		return ktime_mono_to_any(mono, q->tk_offset);
98 	}
99 
100 	return KTIME_MAX;
101 }
102 
103 static void taprio_free_sched_cb(struct rcu_head *head)
104 {
105 	struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
106 	struct sched_entry *entry, *n;
107 
108 	if (!sched)
109 		return;
110 
111 	list_for_each_entry_safe(entry, n, &sched->entries, list) {
112 		list_del(&entry->list);
113 		kfree(entry);
114 	}
115 
116 	kfree(sched);
117 }
118 
119 static void switch_schedules(struct taprio_sched *q,
120 			     struct sched_gate_list **admin,
121 			     struct sched_gate_list **oper)
122 {
123 	rcu_assign_pointer(q->oper_sched, *admin);
124 	rcu_assign_pointer(q->admin_sched, NULL);
125 
126 	if (*oper)
127 		call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
128 
129 	*oper = *admin;
130 	*admin = NULL;
131 }
132 
133 /* Get how much time has been already elapsed in the current cycle. */
134 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
135 {
136 	ktime_t time_since_sched_start;
137 	s32 time_elapsed;
138 
139 	time_since_sched_start = ktime_sub(time, sched->base_time);
140 	div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
141 
142 	return time_elapsed;
143 }
144 
145 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
146 				     struct sched_gate_list *admin,
147 				     struct sched_entry *entry,
148 				     ktime_t intv_start)
149 {
150 	s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
151 	ktime_t intv_end, cycle_ext_end, cycle_end;
152 
153 	cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
154 	intv_end = ktime_add_ns(intv_start, entry->interval);
155 	cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
156 
157 	if (ktime_before(intv_end, cycle_end))
158 		return intv_end;
159 	else if (admin && admin != sched &&
160 		 ktime_after(admin->base_time, cycle_end) &&
161 		 ktime_before(admin->base_time, cycle_ext_end))
162 		return admin->base_time;
163 	else
164 		return cycle_end;
165 }
166 
167 static int length_to_duration(struct taprio_sched *q, int len)
168 {
169 	return div_u64(len * atomic64_read(&q->picos_per_byte), 1000);
170 }
171 
172 /* Returns the entry corresponding to next available interval. If
173  * validate_interval is set, it only validates whether the timestamp occurs
174  * when the gate corresponding to the skb's traffic class is open.
175  */
176 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
177 						  struct Qdisc *sch,
178 						  struct sched_gate_list *sched,
179 						  struct sched_gate_list *admin,
180 						  ktime_t time,
181 						  ktime_t *interval_start,
182 						  ktime_t *interval_end,
183 						  bool validate_interval)
184 {
185 	ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
186 	ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
187 	struct sched_entry *entry = NULL, *entry_found = NULL;
188 	struct taprio_sched *q = qdisc_priv(sch);
189 	struct net_device *dev = qdisc_dev(sch);
190 	bool entry_available = false;
191 	s32 cycle_elapsed;
192 	int tc, n;
193 
194 	tc = netdev_get_prio_tc_map(dev, skb->priority);
195 	packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
196 
197 	*interval_start = 0;
198 	*interval_end = 0;
199 
200 	if (!sched)
201 		return NULL;
202 
203 	cycle = sched->cycle_time;
204 	cycle_elapsed = get_cycle_time_elapsed(sched, time);
205 	curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
206 	cycle_end = ktime_add_ns(curr_intv_end, cycle);
207 
208 	list_for_each_entry(entry, &sched->entries, list) {
209 		curr_intv_start = curr_intv_end;
210 		curr_intv_end = get_interval_end_time(sched, admin, entry,
211 						      curr_intv_start);
212 
213 		if (ktime_after(curr_intv_start, cycle_end))
214 			break;
215 
216 		if (!(entry->gate_mask & BIT(tc)) ||
217 		    packet_transmit_time > entry->interval)
218 			continue;
219 
220 		txtime = entry->next_txtime;
221 
222 		if (ktime_before(txtime, time) || validate_interval) {
223 			transmit_end_time = ktime_add_ns(time, packet_transmit_time);
224 			if ((ktime_before(curr_intv_start, time) &&
225 			     ktime_before(transmit_end_time, curr_intv_end)) ||
226 			    (ktime_after(curr_intv_start, time) && !validate_interval)) {
227 				entry_found = entry;
228 				*interval_start = curr_intv_start;
229 				*interval_end = curr_intv_end;
230 				break;
231 			} else if (!entry_available && !validate_interval) {
232 				/* Here, we are just trying to find out the
233 				 * first available interval in the next cycle.
234 				 */
235 				entry_available = 1;
236 				entry_found = entry;
237 				*interval_start = ktime_add_ns(curr_intv_start, cycle);
238 				*interval_end = ktime_add_ns(curr_intv_end, cycle);
239 			}
240 		} else if (ktime_before(txtime, earliest_txtime) &&
241 			   !entry_available) {
242 			earliest_txtime = txtime;
243 			entry_found = entry;
244 			n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
245 			*interval_start = ktime_add(curr_intv_start, n * cycle);
246 			*interval_end = ktime_add(curr_intv_end, n * cycle);
247 		}
248 	}
249 
250 	return entry_found;
251 }
252 
253 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
254 {
255 	struct taprio_sched *q = qdisc_priv(sch);
256 	struct sched_gate_list *sched, *admin;
257 	ktime_t interval_start, interval_end;
258 	struct sched_entry *entry;
259 
260 	rcu_read_lock();
261 	sched = rcu_dereference(q->oper_sched);
262 	admin = rcu_dereference(q->admin_sched);
263 
264 	entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
265 				       &interval_start, &interval_end, true);
266 	rcu_read_unlock();
267 
268 	return entry;
269 }
270 
271 /* This returns the tstamp value set by TCP in terms of the set clock. */
272 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
273 {
274 	unsigned int offset = skb_network_offset(skb);
275 	const struct ipv6hdr *ipv6h;
276 	const struct iphdr *iph;
277 	struct ipv6hdr _ipv6h;
278 
279 	ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
280 	if (!ipv6h)
281 		return 0;
282 
283 	if (ipv6h->version == 4) {
284 		iph = (struct iphdr *)ipv6h;
285 		offset += iph->ihl * 4;
286 
287 		/* special-case 6in4 tunnelling, as that is a common way to get
288 		 * v6 connectivity in the home
289 		 */
290 		if (iph->protocol == IPPROTO_IPV6) {
291 			ipv6h = skb_header_pointer(skb, offset,
292 						   sizeof(_ipv6h), &_ipv6h);
293 
294 			if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
295 				return 0;
296 		} else if (iph->protocol != IPPROTO_TCP) {
297 			return 0;
298 		}
299 	} else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
300 		return 0;
301 	}
302 
303 	return ktime_mono_to_any(skb->skb_mstamp_ns, q->tk_offset);
304 }
305 
306 /* There are a few scenarios where we will have to modify the txtime from
307  * what is read from next_txtime in sched_entry. They are:
308  * 1. If txtime is in the past,
309  *    a. The gate for the traffic class is currently open and packet can be
310  *       transmitted before it closes, schedule the packet right away.
311  *    b. If the gate corresponding to the traffic class is going to open later
312  *       in the cycle, set the txtime of packet to the interval start.
313  * 2. If txtime is in the future, there are packets corresponding to the
314  *    current traffic class waiting to be transmitted. So, the following
315  *    possibilities exist:
316  *    a. We can transmit the packet before the window containing the txtime
317  *       closes.
318  *    b. The window might close before the transmission can be completed
319  *       successfully. So, schedule the packet in the next open window.
320  */
321 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
322 {
323 	ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
324 	struct taprio_sched *q = qdisc_priv(sch);
325 	struct sched_gate_list *sched, *admin;
326 	ktime_t minimum_time, now, txtime;
327 	int len, packet_transmit_time;
328 	struct sched_entry *entry;
329 	bool sched_changed;
330 
331 	now = taprio_get_time(q);
332 	minimum_time = ktime_add_ns(now, q->txtime_delay);
333 
334 	tcp_tstamp = get_tcp_tstamp(q, skb);
335 	minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
336 
337 	rcu_read_lock();
338 	admin = rcu_dereference(q->admin_sched);
339 	sched = rcu_dereference(q->oper_sched);
340 	if (admin && ktime_after(minimum_time, admin->base_time))
341 		switch_schedules(q, &admin, &sched);
342 
343 	/* Until the schedule starts, all the queues are open */
344 	if (!sched || ktime_before(minimum_time, sched->base_time)) {
345 		txtime = minimum_time;
346 		goto done;
347 	}
348 
349 	len = qdisc_pkt_len(skb);
350 	packet_transmit_time = length_to_duration(q, len);
351 
352 	do {
353 		sched_changed = 0;
354 
355 		entry = find_entry_to_transmit(skb, sch, sched, admin,
356 					       minimum_time,
357 					       &interval_start, &interval_end,
358 					       false);
359 		if (!entry) {
360 			txtime = 0;
361 			goto done;
362 		}
363 
364 		txtime = entry->next_txtime;
365 		txtime = max_t(ktime_t, txtime, minimum_time);
366 		txtime = max_t(ktime_t, txtime, interval_start);
367 
368 		if (admin && admin != sched &&
369 		    ktime_after(txtime, admin->base_time)) {
370 			sched = admin;
371 			sched_changed = 1;
372 			continue;
373 		}
374 
375 		transmit_end_time = ktime_add(txtime, packet_transmit_time);
376 		minimum_time = transmit_end_time;
377 
378 		/* Update the txtime of current entry to the next time it's
379 		 * interval starts.
380 		 */
381 		if (ktime_after(transmit_end_time, interval_end))
382 			entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
383 	} while (sched_changed || ktime_after(transmit_end_time, interval_end));
384 
385 	entry->next_txtime = transmit_end_time;
386 
387 done:
388 	rcu_read_unlock();
389 	return txtime;
390 }
391 
392 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
393 			  struct sk_buff **to_free)
394 {
395 	struct taprio_sched *q = qdisc_priv(sch);
396 	struct Qdisc *child;
397 	int queue;
398 
399 	queue = skb_get_queue_mapping(skb);
400 
401 	child = q->qdiscs[queue];
402 	if (unlikely(!child))
403 		return qdisc_drop(skb, sch, to_free);
404 
405 	if (skb->sk && sock_flag(skb->sk, SOCK_TXTIME)) {
406 		if (!is_valid_interval(skb, sch))
407 			return qdisc_drop(skb, sch, to_free);
408 	} else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
409 		skb->tstamp = get_packet_txtime(skb, sch);
410 		if (!skb->tstamp)
411 			return qdisc_drop(skb, sch, to_free);
412 	}
413 
414 	qdisc_qstats_backlog_inc(sch, skb);
415 	sch->q.qlen++;
416 
417 	return qdisc_enqueue(skb, child, to_free);
418 }
419 
420 static struct sk_buff *taprio_peek(struct Qdisc *sch)
421 {
422 	struct taprio_sched *q = qdisc_priv(sch);
423 	struct net_device *dev = qdisc_dev(sch);
424 	struct sched_entry *entry;
425 	struct sk_buff *skb;
426 	u32 gate_mask;
427 	int i;
428 
429 	rcu_read_lock();
430 	entry = rcu_dereference(q->current_entry);
431 	gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
432 	rcu_read_unlock();
433 
434 	if (!gate_mask)
435 		return NULL;
436 
437 	for (i = 0; i < dev->num_tx_queues; i++) {
438 		struct Qdisc *child = q->qdiscs[i];
439 		int prio;
440 		u8 tc;
441 
442 		if (unlikely(!child))
443 			continue;
444 
445 		skb = child->ops->peek(child);
446 		if (!skb)
447 			continue;
448 
449 		if (TXTIME_ASSIST_IS_ENABLED(q->flags))
450 			return skb;
451 
452 		prio = skb->priority;
453 		tc = netdev_get_prio_tc_map(dev, prio);
454 
455 		if (!(gate_mask & BIT(tc)))
456 			continue;
457 
458 		return skb;
459 	}
460 
461 	return NULL;
462 }
463 
464 static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry)
465 {
466 	atomic_set(&entry->budget,
467 		   div64_u64((u64)entry->interval * 1000,
468 			     atomic64_read(&q->picos_per_byte)));
469 }
470 
471 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
472 {
473 	struct taprio_sched *q = qdisc_priv(sch);
474 	struct net_device *dev = qdisc_dev(sch);
475 	struct sk_buff *skb = NULL;
476 	struct sched_entry *entry;
477 	u32 gate_mask;
478 	int i;
479 
480 	rcu_read_lock();
481 	entry = rcu_dereference(q->current_entry);
482 	/* if there's no entry, it means that the schedule didn't
483 	 * start yet, so force all gates to be open, this is in
484 	 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
485 	 * "AdminGateSates"
486 	 */
487 	gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
488 
489 	if (!gate_mask)
490 		goto done;
491 
492 	for (i = 0; i < dev->num_tx_queues; i++) {
493 		struct Qdisc *child = q->qdiscs[i];
494 		ktime_t guard;
495 		int prio;
496 		int len;
497 		u8 tc;
498 
499 		if (unlikely(!child))
500 			continue;
501 
502 		if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
503 			skb = child->ops->dequeue(child);
504 			if (!skb)
505 				continue;
506 			goto skb_found;
507 		}
508 
509 		skb = child->ops->peek(child);
510 		if (!skb)
511 			continue;
512 
513 		prio = skb->priority;
514 		tc = netdev_get_prio_tc_map(dev, prio);
515 
516 		if (!(gate_mask & BIT(tc)))
517 			continue;
518 
519 		len = qdisc_pkt_len(skb);
520 		guard = ktime_add_ns(taprio_get_time(q),
521 				     length_to_duration(q, len));
522 
523 		/* In the case that there's no gate entry, there's no
524 		 * guard band ...
525 		 */
526 		if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
527 		    ktime_after(guard, entry->close_time))
528 			continue;
529 
530 		/* ... and no budget. */
531 		if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
532 		    atomic_sub_return(len, &entry->budget) < 0)
533 			continue;
534 
535 		skb = child->ops->dequeue(child);
536 		if (unlikely(!skb))
537 			goto done;
538 
539 skb_found:
540 		qdisc_bstats_update(sch, skb);
541 		qdisc_qstats_backlog_dec(sch, skb);
542 		sch->q.qlen--;
543 
544 		goto done;
545 	}
546 
547 done:
548 	rcu_read_unlock();
549 
550 	return skb;
551 }
552 
553 static bool should_restart_cycle(const struct sched_gate_list *oper,
554 				 const struct sched_entry *entry)
555 {
556 	if (list_is_last(&entry->list, &oper->entries))
557 		return true;
558 
559 	if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0)
560 		return true;
561 
562 	return false;
563 }
564 
565 static bool should_change_schedules(const struct sched_gate_list *admin,
566 				    const struct sched_gate_list *oper,
567 				    ktime_t close_time)
568 {
569 	ktime_t next_base_time, extension_time;
570 
571 	if (!admin)
572 		return false;
573 
574 	next_base_time = sched_base_time(admin);
575 
576 	/* This is the simple case, the close_time would fall after
577 	 * the next schedule base_time.
578 	 */
579 	if (ktime_compare(next_base_time, close_time) <= 0)
580 		return true;
581 
582 	/* This is the cycle_time_extension case, if the close_time
583 	 * plus the amount that can be extended would fall after the
584 	 * next schedule base_time, we can extend the current schedule
585 	 * for that amount.
586 	 */
587 	extension_time = ktime_add_ns(close_time, oper->cycle_time_extension);
588 
589 	/* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
590 	 * how precisely the extension should be made. So after
591 	 * conformance testing, this logic may change.
592 	 */
593 	if (ktime_compare(next_base_time, extension_time) <= 0)
594 		return true;
595 
596 	return false;
597 }
598 
599 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
600 {
601 	struct taprio_sched *q = container_of(timer, struct taprio_sched,
602 					      advance_timer);
603 	struct sched_gate_list *oper, *admin;
604 	struct sched_entry *entry, *next;
605 	struct Qdisc *sch = q->root;
606 	ktime_t close_time;
607 
608 	spin_lock(&q->current_entry_lock);
609 	entry = rcu_dereference_protected(q->current_entry,
610 					  lockdep_is_held(&q->current_entry_lock));
611 	oper = rcu_dereference_protected(q->oper_sched,
612 					 lockdep_is_held(&q->current_entry_lock));
613 	admin = rcu_dereference_protected(q->admin_sched,
614 					  lockdep_is_held(&q->current_entry_lock));
615 
616 	if (!oper)
617 		switch_schedules(q, &admin, &oper);
618 
619 	/* This can happen in two cases: 1. this is the very first run
620 	 * of this function (i.e. we weren't running any schedule
621 	 * previously); 2. The previous schedule just ended. The first
622 	 * entry of all schedules are pre-calculated during the
623 	 * schedule initialization.
624 	 */
625 	if (unlikely(!entry || entry->close_time == oper->base_time)) {
626 		next = list_first_entry(&oper->entries, struct sched_entry,
627 					list);
628 		close_time = next->close_time;
629 		goto first_run;
630 	}
631 
632 	if (should_restart_cycle(oper, entry)) {
633 		next = list_first_entry(&oper->entries, struct sched_entry,
634 					list);
635 		oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time,
636 						      oper->cycle_time);
637 	} else {
638 		next = list_next_entry(entry, list);
639 	}
640 
641 	close_time = ktime_add_ns(entry->close_time, next->interval);
642 	close_time = min_t(ktime_t, close_time, oper->cycle_close_time);
643 
644 	if (should_change_schedules(admin, oper, close_time)) {
645 		/* Set things so the next time this runs, the new
646 		 * schedule runs.
647 		 */
648 		close_time = sched_base_time(admin);
649 		switch_schedules(q, &admin, &oper);
650 	}
651 
652 	next->close_time = close_time;
653 	taprio_set_budget(q, next);
654 
655 first_run:
656 	rcu_assign_pointer(q->current_entry, next);
657 	spin_unlock(&q->current_entry_lock);
658 
659 	hrtimer_set_expires(&q->advance_timer, close_time);
660 
661 	rcu_read_lock();
662 	__netif_schedule(sch);
663 	rcu_read_unlock();
664 
665 	return HRTIMER_RESTART;
666 }
667 
668 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
669 	[TCA_TAPRIO_SCHED_ENTRY_INDEX]	   = { .type = NLA_U32 },
670 	[TCA_TAPRIO_SCHED_ENTRY_CMD]	   = { .type = NLA_U8 },
671 	[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
672 	[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]  = { .type = NLA_U32 },
673 };
674 
675 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
676 	[TCA_TAPRIO_ATTR_PRIOMAP]	       = {
677 		.len = sizeof(struct tc_mqprio_qopt)
678 	},
679 	[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST]           = { .type = NLA_NESTED },
680 	[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]            = { .type = NLA_S64 },
681 	[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]         = { .type = NLA_NESTED },
682 	[TCA_TAPRIO_ATTR_SCHED_CLOCKID]              = { .type = NLA_S32 },
683 	[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]           = { .type = NLA_S64 },
684 	[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
685 };
686 
687 static int fill_sched_entry(struct nlattr **tb, struct sched_entry *entry,
688 			    struct netlink_ext_ack *extack)
689 {
690 	u32 interval = 0;
691 
692 	if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
693 		entry->command = nla_get_u8(
694 			tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
695 
696 	if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
697 		entry->gate_mask = nla_get_u32(
698 			tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
699 
700 	if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
701 		interval = nla_get_u32(
702 			tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
703 
704 	if (interval == 0) {
705 		NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
706 		return -EINVAL;
707 	}
708 
709 	entry->interval = interval;
710 
711 	return 0;
712 }
713 
714 static int parse_sched_entry(struct nlattr *n, struct sched_entry *entry,
715 			     int index, struct netlink_ext_ack *extack)
716 {
717 	struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
718 	int err;
719 
720 	err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
721 					  entry_policy, NULL);
722 	if (err < 0) {
723 		NL_SET_ERR_MSG(extack, "Could not parse nested entry");
724 		return -EINVAL;
725 	}
726 
727 	entry->index = index;
728 
729 	return fill_sched_entry(tb, entry, extack);
730 }
731 
732 static int parse_sched_list(struct nlattr *list,
733 			    struct sched_gate_list *sched,
734 			    struct netlink_ext_ack *extack)
735 {
736 	struct nlattr *n;
737 	int err, rem;
738 	int i = 0;
739 
740 	if (!list)
741 		return -EINVAL;
742 
743 	nla_for_each_nested(n, list, rem) {
744 		struct sched_entry *entry;
745 
746 		if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
747 			NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
748 			continue;
749 		}
750 
751 		entry = kzalloc(sizeof(*entry), GFP_KERNEL);
752 		if (!entry) {
753 			NL_SET_ERR_MSG(extack, "Not enough memory for entry");
754 			return -ENOMEM;
755 		}
756 
757 		err = parse_sched_entry(n, entry, i, extack);
758 		if (err < 0) {
759 			kfree(entry);
760 			return err;
761 		}
762 
763 		list_add_tail(&entry->list, &sched->entries);
764 		i++;
765 	}
766 
767 	sched->num_entries = i;
768 
769 	return i;
770 }
771 
772 static int parse_taprio_schedule(struct nlattr **tb,
773 				 struct sched_gate_list *new,
774 				 struct netlink_ext_ack *extack)
775 {
776 	int err = 0;
777 
778 	if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
779 		NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
780 		return -ENOTSUPP;
781 	}
782 
783 	if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
784 		new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
785 
786 	if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
787 		new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
788 
789 	if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
790 		new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
791 
792 	if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
793 		err = parse_sched_list(
794 			tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST], new, extack);
795 	if (err < 0)
796 		return err;
797 
798 	if (!new->cycle_time) {
799 		struct sched_entry *entry;
800 		ktime_t cycle = 0;
801 
802 		list_for_each_entry(entry, &new->entries, list)
803 			cycle = ktime_add_ns(cycle, entry->interval);
804 		new->cycle_time = cycle;
805 	}
806 
807 	return 0;
808 }
809 
810 static int taprio_parse_mqprio_opt(struct net_device *dev,
811 				   struct tc_mqprio_qopt *qopt,
812 				   struct netlink_ext_ack *extack,
813 				   u32 taprio_flags)
814 {
815 	int i, j;
816 
817 	if (!qopt && !dev->num_tc) {
818 		NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
819 		return -EINVAL;
820 	}
821 
822 	/* If num_tc is already set, it means that the user already
823 	 * configured the mqprio part
824 	 */
825 	if (dev->num_tc)
826 		return 0;
827 
828 	/* Verify num_tc is not out of max range */
829 	if (qopt->num_tc > TC_MAX_QUEUE) {
830 		NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
831 		return -EINVAL;
832 	}
833 
834 	/* taprio imposes that traffic classes map 1:n to tx queues */
835 	if (qopt->num_tc > dev->num_tx_queues) {
836 		NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
837 		return -EINVAL;
838 	}
839 
840 	/* Verify priority mapping uses valid tcs */
841 	for (i = 0; i < TC_BITMASK + 1; i++) {
842 		if (qopt->prio_tc_map[i] >= qopt->num_tc) {
843 			NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
844 			return -EINVAL;
845 		}
846 	}
847 
848 	for (i = 0; i < qopt->num_tc; i++) {
849 		unsigned int last = qopt->offset[i] + qopt->count[i];
850 
851 		/* Verify the queue count is in tx range being equal to the
852 		 * real_num_tx_queues indicates the last queue is in use.
853 		 */
854 		if (qopt->offset[i] >= dev->num_tx_queues ||
855 		    !qopt->count[i] ||
856 		    last > dev->real_num_tx_queues) {
857 			NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
858 			return -EINVAL;
859 		}
860 
861 		if (TXTIME_ASSIST_IS_ENABLED(taprio_flags))
862 			continue;
863 
864 		/* Verify that the offset and counts do not overlap */
865 		for (j = i + 1; j < qopt->num_tc; j++) {
866 			if (last > qopt->offset[j]) {
867 				NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
868 				return -EINVAL;
869 			}
870 		}
871 	}
872 
873 	return 0;
874 }
875 
876 static int taprio_get_start_time(struct Qdisc *sch,
877 				 struct sched_gate_list *sched,
878 				 ktime_t *start)
879 {
880 	struct taprio_sched *q = qdisc_priv(sch);
881 	ktime_t now, base, cycle;
882 	s64 n;
883 
884 	base = sched_base_time(sched);
885 	now = taprio_get_time(q);
886 
887 	if (ktime_after(base, now)) {
888 		*start = base;
889 		return 0;
890 	}
891 
892 	cycle = sched->cycle_time;
893 
894 	/* The qdisc is expected to have at least one sched_entry.  Moreover,
895 	 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
896 	 * something went really wrong. In that case, we should warn about this
897 	 * inconsistent state and return error.
898 	 */
899 	if (WARN_ON(!cycle))
900 		return -EFAULT;
901 
902 	/* Schedule the start time for the beginning of the next
903 	 * cycle.
904 	 */
905 	n = div64_s64(ktime_sub_ns(now, base), cycle);
906 	*start = ktime_add_ns(base, (n + 1) * cycle);
907 	return 0;
908 }
909 
910 static void setup_first_close_time(struct taprio_sched *q,
911 				   struct sched_gate_list *sched, ktime_t base)
912 {
913 	struct sched_entry *first;
914 	ktime_t cycle;
915 
916 	first = list_first_entry(&sched->entries,
917 				 struct sched_entry, list);
918 
919 	cycle = sched->cycle_time;
920 
921 	/* FIXME: find a better place to do this */
922 	sched->cycle_close_time = ktime_add_ns(base, cycle);
923 
924 	first->close_time = ktime_add_ns(base, first->interval);
925 	taprio_set_budget(q, first);
926 	rcu_assign_pointer(q->current_entry, NULL);
927 }
928 
929 static void taprio_start_sched(struct Qdisc *sch,
930 			       ktime_t start, struct sched_gate_list *new)
931 {
932 	struct taprio_sched *q = qdisc_priv(sch);
933 	ktime_t expires;
934 
935 	expires = hrtimer_get_expires(&q->advance_timer);
936 	if (expires == 0)
937 		expires = KTIME_MAX;
938 
939 	/* If the new schedule starts before the next expiration, we
940 	 * reprogram it to the earliest one, so we change the admin
941 	 * schedule to the operational one at the right time.
942 	 */
943 	start = min_t(ktime_t, start, expires);
944 
945 	hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
946 }
947 
948 static void taprio_set_picos_per_byte(struct net_device *dev,
949 				      struct taprio_sched *q)
950 {
951 	struct ethtool_link_ksettings ecmd;
952 	int speed = SPEED_10;
953 	int picos_per_byte;
954 	int err;
955 
956 	err = __ethtool_get_link_ksettings(dev, &ecmd);
957 	if (err < 0)
958 		goto skip;
959 
960 	if (ecmd.base.speed != SPEED_UNKNOWN)
961 		speed = ecmd.base.speed;
962 
963 skip:
964 	picos_per_byte = div64_s64(NSEC_PER_SEC * 1000LL * 8,
965 				   speed * 1000 * 1000);
966 
967 	atomic64_set(&q->picos_per_byte, picos_per_byte);
968 	netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
969 		   dev->name, (long long)atomic64_read(&q->picos_per_byte),
970 		   ecmd.base.speed);
971 }
972 
973 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
974 			       void *ptr)
975 {
976 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
977 	struct net_device *qdev;
978 	struct taprio_sched *q;
979 	bool found = false;
980 
981 	ASSERT_RTNL();
982 
983 	if (event != NETDEV_UP && event != NETDEV_CHANGE)
984 		return NOTIFY_DONE;
985 
986 	spin_lock(&taprio_list_lock);
987 	list_for_each_entry(q, &taprio_list, taprio_list) {
988 		qdev = qdisc_dev(q->root);
989 		if (qdev == dev) {
990 			found = true;
991 			break;
992 		}
993 	}
994 	spin_unlock(&taprio_list_lock);
995 
996 	if (found)
997 		taprio_set_picos_per_byte(dev, q);
998 
999 	return NOTIFY_DONE;
1000 }
1001 
1002 static void setup_txtime(struct taprio_sched *q,
1003 			 struct sched_gate_list *sched, ktime_t base)
1004 {
1005 	struct sched_entry *entry;
1006 	u32 interval = 0;
1007 
1008 	list_for_each_entry(entry, &sched->entries, list) {
1009 		entry->next_txtime = ktime_add_ns(base, interval);
1010 		interval += entry->interval;
1011 	}
1012 }
1013 
1014 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1015 			 struct netlink_ext_ack *extack)
1016 {
1017 	struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1018 	struct sched_gate_list *oper, *admin, *new_admin;
1019 	struct taprio_sched *q = qdisc_priv(sch);
1020 	struct net_device *dev = qdisc_dev(sch);
1021 	struct tc_mqprio_qopt *mqprio = NULL;
1022 	u32 taprio_flags = 0;
1023 	int i, err, clockid;
1024 	unsigned long flags;
1025 	ktime_t start;
1026 
1027 	err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1028 					  taprio_policy, extack);
1029 	if (err < 0)
1030 		return err;
1031 
1032 	if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1033 		mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1034 
1035 	if (tb[TCA_TAPRIO_ATTR_FLAGS]) {
1036 		taprio_flags = nla_get_u32(tb[TCA_TAPRIO_ATTR_FLAGS]);
1037 
1038 		if (q->flags != 0 && q->flags != taprio_flags) {
1039 			NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1040 			return -EOPNOTSUPP;
1041 		} else if (!FLAGS_VALID(taprio_flags)) {
1042 			NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1043 			return -EINVAL;
1044 		}
1045 
1046 		q->flags = taprio_flags;
1047 	}
1048 
1049 	err = taprio_parse_mqprio_opt(dev, mqprio, extack, taprio_flags);
1050 	if (err < 0)
1051 		return err;
1052 
1053 	new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1054 	if (!new_admin) {
1055 		NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1056 		return -ENOMEM;
1057 	}
1058 	INIT_LIST_HEAD(&new_admin->entries);
1059 
1060 	rcu_read_lock();
1061 	oper = rcu_dereference(q->oper_sched);
1062 	admin = rcu_dereference(q->admin_sched);
1063 	rcu_read_unlock();
1064 
1065 	if (mqprio && (oper || admin)) {
1066 		NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1067 		err = -ENOTSUPP;
1068 		goto free_sched;
1069 	}
1070 
1071 	err = parse_taprio_schedule(tb, new_admin, extack);
1072 	if (err < 0)
1073 		goto free_sched;
1074 
1075 	if (new_admin->num_entries == 0) {
1076 		NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1077 		err = -EINVAL;
1078 		goto free_sched;
1079 	}
1080 
1081 	if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1082 		clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1083 
1084 		/* We only support static clockids and we don't allow
1085 		 * for it to be modified after the first init.
1086 		 */
1087 		if (clockid < 0 ||
1088 		    (q->clockid != -1 && q->clockid != clockid)) {
1089 			NL_SET_ERR_MSG(extack, "Changing the 'clockid' of a running schedule is not supported");
1090 			err = -ENOTSUPP;
1091 			goto free_sched;
1092 		}
1093 
1094 		q->clockid = clockid;
1095 	}
1096 
1097 	if (q->clockid == -1 && !tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1098 		NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1099 		err = -EINVAL;
1100 		goto free_sched;
1101 	}
1102 
1103 	taprio_set_picos_per_byte(dev, q);
1104 
1105 	/* Protects against enqueue()/dequeue() */
1106 	spin_lock_bh(qdisc_lock(sch));
1107 
1108 	if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1109 		if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1110 			NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1111 			err = -EINVAL;
1112 			goto unlock;
1113 		}
1114 
1115 		q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1116 	}
1117 
1118 	if (!TXTIME_ASSIST_IS_ENABLED(taprio_flags) &&
1119 	    !hrtimer_active(&q->advance_timer)) {
1120 		hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1121 		q->advance_timer.function = advance_sched;
1122 	}
1123 
1124 	if (mqprio) {
1125 		netdev_set_num_tc(dev, mqprio->num_tc);
1126 		for (i = 0; i < mqprio->num_tc; i++)
1127 			netdev_set_tc_queue(dev, i,
1128 					    mqprio->count[i],
1129 					    mqprio->offset[i]);
1130 
1131 		/* Always use supplied priority mappings */
1132 		for (i = 0; i < TC_BITMASK + 1; i++)
1133 			netdev_set_prio_tc_map(dev, i,
1134 					       mqprio->prio_tc_map[i]);
1135 	}
1136 
1137 	switch (q->clockid) {
1138 	case CLOCK_REALTIME:
1139 		q->tk_offset = TK_OFFS_REAL;
1140 		break;
1141 	case CLOCK_MONOTONIC:
1142 		q->tk_offset = TK_OFFS_MAX;
1143 		break;
1144 	case CLOCK_BOOTTIME:
1145 		q->tk_offset = TK_OFFS_BOOT;
1146 		break;
1147 	case CLOCK_TAI:
1148 		q->tk_offset = TK_OFFS_TAI;
1149 		break;
1150 	default:
1151 		NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1152 		err = -EINVAL;
1153 		goto unlock;
1154 	}
1155 
1156 	err = taprio_get_start_time(sch, new_admin, &start);
1157 	if (err < 0) {
1158 		NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1159 		goto unlock;
1160 	}
1161 
1162 	if (TXTIME_ASSIST_IS_ENABLED(taprio_flags)) {
1163 		setup_txtime(q, new_admin, start);
1164 
1165 		if (!oper) {
1166 			rcu_assign_pointer(q->oper_sched, new_admin);
1167 			err = 0;
1168 			new_admin = NULL;
1169 			goto unlock;
1170 		}
1171 
1172 		rcu_assign_pointer(q->admin_sched, new_admin);
1173 		if (admin)
1174 			call_rcu(&admin->rcu, taprio_free_sched_cb);
1175 	} else {
1176 		setup_first_close_time(q, new_admin, start);
1177 
1178 		/* Protects against advance_sched() */
1179 		spin_lock_irqsave(&q->current_entry_lock, flags);
1180 
1181 		taprio_start_sched(sch, start, new_admin);
1182 
1183 		rcu_assign_pointer(q->admin_sched, new_admin);
1184 		if (admin)
1185 			call_rcu(&admin->rcu, taprio_free_sched_cb);
1186 
1187 		spin_unlock_irqrestore(&q->current_entry_lock, flags);
1188 	}
1189 
1190 	new_admin = NULL;
1191 	err = 0;
1192 
1193 unlock:
1194 	spin_unlock_bh(qdisc_lock(sch));
1195 
1196 free_sched:
1197 	if (new_admin)
1198 		call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1199 
1200 	return err;
1201 }
1202 
1203 static void taprio_destroy(struct Qdisc *sch)
1204 {
1205 	struct taprio_sched *q = qdisc_priv(sch);
1206 	struct net_device *dev = qdisc_dev(sch);
1207 	unsigned int i;
1208 
1209 	spin_lock(&taprio_list_lock);
1210 	list_del(&q->taprio_list);
1211 	spin_unlock(&taprio_list_lock);
1212 
1213 	hrtimer_cancel(&q->advance_timer);
1214 
1215 	if (q->qdiscs) {
1216 		for (i = 0; i < dev->num_tx_queues && q->qdiscs[i]; i++)
1217 			qdisc_put(q->qdiscs[i]);
1218 
1219 		kfree(q->qdiscs);
1220 	}
1221 	q->qdiscs = NULL;
1222 
1223 	netdev_set_num_tc(dev, 0);
1224 
1225 	if (q->oper_sched)
1226 		call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb);
1227 
1228 	if (q->admin_sched)
1229 		call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb);
1230 }
1231 
1232 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1233 		       struct netlink_ext_ack *extack)
1234 {
1235 	struct taprio_sched *q = qdisc_priv(sch);
1236 	struct net_device *dev = qdisc_dev(sch);
1237 	int i;
1238 
1239 	spin_lock_init(&q->current_entry_lock);
1240 
1241 	hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1242 	q->advance_timer.function = advance_sched;
1243 
1244 	q->root = sch;
1245 
1246 	/* We only support static clockids. Use an invalid value as default
1247 	 * and get the valid one on taprio_change().
1248 	 */
1249 	q->clockid = -1;
1250 
1251 	spin_lock(&taprio_list_lock);
1252 	list_add(&q->taprio_list, &taprio_list);
1253 	spin_unlock(&taprio_list_lock);
1254 
1255 	if (sch->parent != TC_H_ROOT)
1256 		return -EOPNOTSUPP;
1257 
1258 	if (!netif_is_multiqueue(dev))
1259 		return -EOPNOTSUPP;
1260 
1261 	/* pre-allocate qdisc, attachment can't fail */
1262 	q->qdiscs = kcalloc(dev->num_tx_queues,
1263 			    sizeof(q->qdiscs[0]),
1264 			    GFP_KERNEL);
1265 
1266 	if (!q->qdiscs)
1267 		return -ENOMEM;
1268 
1269 	if (!opt)
1270 		return -EINVAL;
1271 
1272 	for (i = 0; i < dev->num_tx_queues; i++) {
1273 		struct netdev_queue *dev_queue;
1274 		struct Qdisc *qdisc;
1275 
1276 		dev_queue = netdev_get_tx_queue(dev, i);
1277 		qdisc = qdisc_create_dflt(dev_queue,
1278 					  &pfifo_qdisc_ops,
1279 					  TC_H_MAKE(TC_H_MAJ(sch->handle),
1280 						    TC_H_MIN(i + 1)),
1281 					  extack);
1282 		if (!qdisc)
1283 			return -ENOMEM;
1284 
1285 		if (i < dev->real_num_tx_queues)
1286 			qdisc_hash_add(qdisc, false);
1287 
1288 		q->qdiscs[i] = qdisc;
1289 	}
1290 
1291 	return taprio_change(sch, opt, extack);
1292 }
1293 
1294 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1295 					     unsigned long cl)
1296 {
1297 	struct net_device *dev = qdisc_dev(sch);
1298 	unsigned long ntx = cl - 1;
1299 
1300 	if (ntx >= dev->num_tx_queues)
1301 		return NULL;
1302 
1303 	return netdev_get_tx_queue(dev, ntx);
1304 }
1305 
1306 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1307 			struct Qdisc *new, struct Qdisc **old,
1308 			struct netlink_ext_ack *extack)
1309 {
1310 	struct taprio_sched *q = qdisc_priv(sch);
1311 	struct net_device *dev = qdisc_dev(sch);
1312 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1313 
1314 	if (!dev_queue)
1315 		return -EINVAL;
1316 
1317 	if (dev->flags & IFF_UP)
1318 		dev_deactivate(dev);
1319 
1320 	*old = q->qdiscs[cl - 1];
1321 	q->qdiscs[cl - 1] = new;
1322 
1323 	if (new)
1324 		new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1325 
1326 	if (dev->flags & IFF_UP)
1327 		dev_activate(dev);
1328 
1329 	return 0;
1330 }
1331 
1332 static int dump_entry(struct sk_buff *msg,
1333 		      const struct sched_entry *entry)
1334 {
1335 	struct nlattr *item;
1336 
1337 	item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1338 	if (!item)
1339 		return -ENOSPC;
1340 
1341 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1342 		goto nla_put_failure;
1343 
1344 	if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1345 		goto nla_put_failure;
1346 
1347 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1348 			entry->gate_mask))
1349 		goto nla_put_failure;
1350 
1351 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1352 			entry->interval))
1353 		goto nla_put_failure;
1354 
1355 	return nla_nest_end(msg, item);
1356 
1357 nla_put_failure:
1358 	nla_nest_cancel(msg, item);
1359 	return -1;
1360 }
1361 
1362 static int dump_schedule(struct sk_buff *msg,
1363 			 const struct sched_gate_list *root)
1364 {
1365 	struct nlattr *entry_list;
1366 	struct sched_entry *entry;
1367 
1368 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1369 			root->base_time, TCA_TAPRIO_PAD))
1370 		return -1;
1371 
1372 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1373 			root->cycle_time, TCA_TAPRIO_PAD))
1374 		return -1;
1375 
1376 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1377 			root->cycle_time_extension, TCA_TAPRIO_PAD))
1378 		return -1;
1379 
1380 	entry_list = nla_nest_start_noflag(msg,
1381 					   TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1382 	if (!entry_list)
1383 		goto error_nest;
1384 
1385 	list_for_each_entry(entry, &root->entries, list) {
1386 		if (dump_entry(msg, entry) < 0)
1387 			goto error_nest;
1388 	}
1389 
1390 	nla_nest_end(msg, entry_list);
1391 	return 0;
1392 
1393 error_nest:
1394 	nla_nest_cancel(msg, entry_list);
1395 	return -1;
1396 }
1397 
1398 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1399 {
1400 	struct taprio_sched *q = qdisc_priv(sch);
1401 	struct net_device *dev = qdisc_dev(sch);
1402 	struct sched_gate_list *oper, *admin;
1403 	struct tc_mqprio_qopt opt = { 0 };
1404 	struct nlattr *nest, *sched_nest;
1405 	unsigned int i;
1406 
1407 	rcu_read_lock();
1408 	oper = rcu_dereference(q->oper_sched);
1409 	admin = rcu_dereference(q->admin_sched);
1410 
1411 	opt.num_tc = netdev_get_num_tc(dev);
1412 	memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
1413 
1414 	for (i = 0; i < netdev_get_num_tc(dev); i++) {
1415 		opt.count[i] = dev->tc_to_txq[i].count;
1416 		opt.offset[i] = dev->tc_to_txq[i].offset;
1417 	}
1418 
1419 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1420 	if (!nest)
1421 		goto start_error;
1422 
1423 	if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
1424 		goto options_error;
1425 
1426 	if (nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
1427 		goto options_error;
1428 
1429 	if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
1430 		goto options_error;
1431 
1432 	if (q->txtime_delay &&
1433 	    nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
1434 		goto options_error;
1435 
1436 	if (oper && dump_schedule(skb, oper))
1437 		goto options_error;
1438 
1439 	if (!admin)
1440 		goto done;
1441 
1442 	sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
1443 	if (!sched_nest)
1444 		goto options_error;
1445 
1446 	if (dump_schedule(skb, admin))
1447 		goto admin_error;
1448 
1449 	nla_nest_end(skb, sched_nest);
1450 
1451 done:
1452 	rcu_read_unlock();
1453 
1454 	return nla_nest_end(skb, nest);
1455 
1456 admin_error:
1457 	nla_nest_cancel(skb, sched_nest);
1458 
1459 options_error:
1460 	nla_nest_cancel(skb, nest);
1461 
1462 start_error:
1463 	rcu_read_unlock();
1464 	return -ENOSPC;
1465 }
1466 
1467 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
1468 {
1469 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1470 
1471 	if (!dev_queue)
1472 		return NULL;
1473 
1474 	return dev_queue->qdisc_sleeping;
1475 }
1476 
1477 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
1478 {
1479 	unsigned int ntx = TC_H_MIN(classid);
1480 
1481 	if (!taprio_queue_get(sch, ntx))
1482 		return 0;
1483 	return ntx;
1484 }
1485 
1486 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
1487 			     struct sk_buff *skb, struct tcmsg *tcm)
1488 {
1489 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1490 
1491 	tcm->tcm_parent = TC_H_ROOT;
1492 	tcm->tcm_handle |= TC_H_MIN(cl);
1493 	tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
1494 
1495 	return 0;
1496 }
1497 
1498 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
1499 				   struct gnet_dump *d)
1500 	__releases(d->lock)
1501 	__acquires(d->lock)
1502 {
1503 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1504 
1505 	sch = dev_queue->qdisc_sleeping;
1506 	if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 ||
1507 	    qdisc_qstats_copy(d, sch) < 0)
1508 		return -1;
1509 	return 0;
1510 }
1511 
1512 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1513 {
1514 	struct net_device *dev = qdisc_dev(sch);
1515 	unsigned long ntx;
1516 
1517 	if (arg->stop)
1518 		return;
1519 
1520 	arg->count = arg->skip;
1521 	for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
1522 		if (arg->fn(sch, ntx + 1, arg) < 0) {
1523 			arg->stop = 1;
1524 			break;
1525 		}
1526 		arg->count++;
1527 	}
1528 }
1529 
1530 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
1531 						struct tcmsg *tcm)
1532 {
1533 	return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
1534 }
1535 
1536 static const struct Qdisc_class_ops taprio_class_ops = {
1537 	.graft		= taprio_graft,
1538 	.leaf		= taprio_leaf,
1539 	.find		= taprio_find,
1540 	.walk		= taprio_walk,
1541 	.dump		= taprio_dump_class,
1542 	.dump_stats	= taprio_dump_class_stats,
1543 	.select_queue	= taprio_select_queue,
1544 };
1545 
1546 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
1547 	.cl_ops		= &taprio_class_ops,
1548 	.id		= "taprio",
1549 	.priv_size	= sizeof(struct taprio_sched),
1550 	.init		= taprio_init,
1551 	.change		= taprio_change,
1552 	.destroy	= taprio_destroy,
1553 	.peek		= taprio_peek,
1554 	.dequeue	= taprio_dequeue,
1555 	.enqueue	= taprio_enqueue,
1556 	.dump		= taprio_dump,
1557 	.owner		= THIS_MODULE,
1558 };
1559 
1560 static struct notifier_block taprio_device_notifier = {
1561 	.notifier_call = taprio_dev_notifier,
1562 };
1563 
1564 static int __init taprio_module_init(void)
1565 {
1566 	int err = register_netdevice_notifier(&taprio_device_notifier);
1567 
1568 	if (err)
1569 		return err;
1570 
1571 	return register_qdisc(&taprio_qdisc_ops);
1572 }
1573 
1574 static void __exit taprio_module_exit(void)
1575 {
1576 	unregister_qdisc(&taprio_qdisc_ops);
1577 	unregister_netdevice_notifier(&taprio_device_notifier);
1578 }
1579 
1580 module_init(taprio_module_init);
1581 module_exit(taprio_module_exit);
1582 MODULE_LICENSE("GPL");
1583