xref: /linux/net/sched/sch_taprio.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
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/ethtool.h>
10 #include <linux/ethtool_netlink.h>
11 #include <linux/types.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/list.h>
16 #include <linux/errno.h>
17 #include <linux/skbuff.h>
18 #include <linux/math64.h>
19 #include <linux/module.h>
20 #include <linux/spinlock.h>
21 #include <linux/rcupdate.h>
22 #include <linux/time.h>
23 #include <net/gso.h>
24 #include <net/netlink.h>
25 #include <net/pkt_sched.h>
26 #include <net/pkt_cls.h>
27 #include <net/sch_generic.h>
28 #include <net/sock.h>
29 #include <net/tcp.h>
30 
31 #define TAPRIO_STAT_NOT_SET	(~0ULL)
32 
33 #include "sch_mqprio_lib.h"
34 
35 static LIST_HEAD(taprio_list);
36 static struct static_key_false taprio_have_broken_mqprio;
37 static struct static_key_false taprio_have_working_mqprio;
38 
39 #define TAPRIO_ALL_GATES_OPEN -1
40 
41 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
42 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
43 #define TAPRIO_SUPPORTED_FLAGS \
44 	(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST | TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
45 #define TAPRIO_FLAGS_INVALID U32_MAX
46 /* Minimum value for picos_per_byte to ensure non-zero duration
47  * for minimum-sized Ethernet frames (ETH_ZLEN = 60).
48  * 60 * 17 > PSEC_PER_NSEC (1000)
49  */
50 #define TAPRIO_PICOS_PER_BYTE_MIN 17
51 
52 struct sched_entry {
53 	/* Durations between this GCL entry and the GCL entry where the
54 	 * respective traffic class gate closes
55 	 */
56 	u64 gate_duration[TC_MAX_QUEUE];
57 	atomic_t budget[TC_MAX_QUEUE];
58 	/* The qdisc makes some effort so that no packet leaves
59 	 * after this time
60 	 */
61 	ktime_t gate_close_time[TC_MAX_QUEUE];
62 	struct list_head list;
63 	/* Used to calculate when to advance the schedule */
64 	ktime_t end_time;
65 	ktime_t next_txtime;
66 	int index;
67 	u32 gate_mask;
68 	u32 interval;
69 	u8 command;
70 };
71 
72 struct sched_gate_list {
73 	/* Longest non-zero contiguous gate durations per traffic class,
74 	 * or 0 if a traffic class gate never opens during the schedule.
75 	 */
76 	u64 max_open_gate_duration[TC_MAX_QUEUE];
77 	u32 max_frm_len[TC_MAX_QUEUE]; /* for the fast path */
78 	u32 max_sdu[TC_MAX_QUEUE]; /* for dump */
79 	struct rcu_head rcu;
80 	struct list_head entries;
81 	size_t num_entries;
82 	ktime_t cycle_end_time;
83 	s64 cycle_time;
84 	s64 cycle_time_extension;
85 	s64 base_time;
86 };
87 
88 struct taprio_sched {
89 	struct Qdisc **qdiscs;
90 	struct Qdisc *root;
91 	u32 flags;
92 	enum tk_offsets tk_offset;
93 	int clockid;
94 	bool offloaded;
95 	bool detected_mqprio;
96 	bool broken_mqprio;
97 	atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
98 				    * speeds it's sub-nanoseconds per byte
99 				    */
100 
101 	/* Protects the update side of the RCU protected current_entry */
102 	spinlock_t current_entry_lock;
103 	struct sched_entry __rcu *current_entry;
104 	struct sched_gate_list __rcu *oper_sched;
105 	struct sched_gate_list __rcu *admin_sched;
106 	struct hrtimer advance_timer;
107 	struct list_head taprio_list;
108 	int cur_txq[TC_MAX_QUEUE];
109 	u32 max_sdu[TC_MAX_QUEUE]; /* save info from the user */
110 	u32 fp[TC_QOPT_MAX_QUEUE]; /* only for dump and offloading */
111 	u32 txtime_delay;
112 };
113 
114 struct __tc_taprio_qopt_offload {
115 	refcount_t users;
116 	struct tc_taprio_qopt_offload offload;
117 };
118 
taprio_calculate_gate_durations(struct taprio_sched * q,struct sched_gate_list * sched)119 static void taprio_calculate_gate_durations(struct taprio_sched *q,
120 					    struct sched_gate_list *sched)
121 {
122 	struct net_device *dev = qdisc_dev(q->root);
123 	int num_tc = netdev_get_num_tc(dev);
124 	struct sched_entry *entry, *cur;
125 	int tc;
126 
127 	list_for_each_entry(entry, &sched->entries, list) {
128 		u32 gates_still_open = entry->gate_mask;
129 
130 		/* For each traffic class, calculate each open gate duration,
131 		 * starting at this schedule entry and ending at the schedule
132 		 * entry containing a gate close event for that TC.
133 		 */
134 		cur = entry;
135 
136 		do {
137 			if (!gates_still_open)
138 				break;
139 
140 			for (tc = 0; tc < num_tc; tc++) {
141 				if (!(gates_still_open & BIT(tc)))
142 					continue;
143 
144 				if (cur->gate_mask & BIT(tc))
145 					entry->gate_duration[tc] += cur->interval;
146 				else
147 					gates_still_open &= ~BIT(tc);
148 			}
149 
150 			cur = list_next_entry_circular(cur, &sched->entries, list);
151 		} while (cur != entry);
152 
153 		/* Keep track of the maximum gate duration for each traffic
154 		 * class, taking care to not confuse a traffic class which is
155 		 * temporarily closed with one that is always closed.
156 		 */
157 		for (tc = 0; tc < num_tc; tc++)
158 			if (entry->gate_duration[tc] &&
159 			    sched->max_open_gate_duration[tc] < entry->gate_duration[tc])
160 				sched->max_open_gate_duration[tc] = entry->gate_duration[tc];
161 	}
162 }
163 
taprio_entry_allows_tx(ktime_t skb_end_time,struct sched_entry * entry,int tc)164 static bool taprio_entry_allows_tx(ktime_t skb_end_time,
165 				   struct sched_entry *entry, int tc)
166 {
167 	return ktime_before(skb_end_time, entry->gate_close_time[tc]);
168 }
169 
sched_base_time(const struct sched_gate_list * sched)170 static ktime_t sched_base_time(const struct sched_gate_list *sched)
171 {
172 	if (!sched)
173 		return KTIME_MAX;
174 
175 	return ns_to_ktime(sched->base_time);
176 }
177 
taprio_mono_to_any(const struct taprio_sched * q,ktime_t mono)178 static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono)
179 {
180 	/* This pairs with WRITE_ONCE() in taprio_parse_clockid() */
181 	enum tk_offsets tk_offset = READ_ONCE(q->tk_offset);
182 
183 	switch (tk_offset) {
184 	case TK_OFFS_MAX:
185 		return mono;
186 	default:
187 		return ktime_mono_to_any(mono, tk_offset);
188 	}
189 }
190 
taprio_get_time(const struct taprio_sched * q)191 static ktime_t taprio_get_time(const struct taprio_sched *q)
192 {
193 	return taprio_mono_to_any(q, ktime_get());
194 }
195 
taprio_free_sched_cb(struct rcu_head * head)196 static void taprio_free_sched_cb(struct rcu_head *head)
197 {
198 	struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
199 	struct sched_entry *entry, *n;
200 
201 	list_for_each_entry_safe(entry, n, &sched->entries, list) {
202 		list_del(&entry->list);
203 		kfree(entry);
204 	}
205 
206 	kfree(sched);
207 }
208 
switch_schedules(struct taprio_sched * q,struct sched_gate_list ** admin,struct sched_gate_list ** oper)209 static void switch_schedules(struct taprio_sched *q,
210 			     struct sched_gate_list **admin,
211 			     struct sched_gate_list **oper)
212 {
213 	rcu_assign_pointer(q->oper_sched, *admin);
214 	rcu_assign_pointer(q->admin_sched, NULL);
215 
216 	if (*oper)
217 		call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
218 
219 	*oper = *admin;
220 	*admin = NULL;
221 }
222 
223 /* Get how much time has been already elapsed in the current cycle. */
get_cycle_time_elapsed(struct sched_gate_list * sched,ktime_t time)224 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
225 {
226 	ktime_t time_since_sched_start;
227 	s32 time_elapsed;
228 
229 	time_since_sched_start = ktime_sub(time, sched->base_time);
230 	div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
231 
232 	return time_elapsed;
233 }
234 
get_interval_end_time(struct sched_gate_list * sched,struct sched_gate_list * admin,struct sched_entry * entry,ktime_t intv_start)235 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
236 				     struct sched_gate_list *admin,
237 				     struct sched_entry *entry,
238 				     ktime_t intv_start)
239 {
240 	s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
241 	ktime_t intv_end, cycle_ext_end, cycle_end;
242 
243 	cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
244 	intv_end = ktime_add_ns(intv_start, entry->interval);
245 	cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
246 
247 	if (ktime_before(intv_end, cycle_end))
248 		return intv_end;
249 	else if (admin && admin != sched &&
250 		 ktime_after(admin->base_time, cycle_end) &&
251 		 ktime_before(admin->base_time, cycle_ext_end))
252 		return admin->base_time;
253 	else
254 		return cycle_end;
255 }
256 
length_to_duration(struct taprio_sched * q,int len)257 static int length_to_duration(struct taprio_sched *q, int len)
258 {
259 	return div_u64(len * atomic64_read(&q->picos_per_byte), PSEC_PER_NSEC);
260 }
261 
duration_to_length(struct taprio_sched * q,u64 duration)262 static int duration_to_length(struct taprio_sched *q, u64 duration)
263 {
264 	return div_u64(duration * PSEC_PER_NSEC, atomic64_read(&q->picos_per_byte));
265 }
266 
267 /* Sets sched->max_sdu[] and sched->max_frm_len[] to the minimum between the
268  * q->max_sdu[] requested by the user and the max_sdu dynamically determined by
269  * the maximum open gate durations at the given link speed.
270  */
taprio_update_queue_max_sdu(struct taprio_sched * q,struct sched_gate_list * sched,struct qdisc_size_table * stab)271 static void taprio_update_queue_max_sdu(struct taprio_sched *q,
272 					struct sched_gate_list *sched,
273 					struct qdisc_size_table *stab)
274 {
275 	struct net_device *dev = qdisc_dev(q->root);
276 	int num_tc = netdev_get_num_tc(dev);
277 	u32 max_sdu_from_user;
278 	u32 max_sdu_dynamic;
279 	u32 max_sdu;
280 	int tc;
281 
282 	for (tc = 0; tc < num_tc; tc++) {
283 		max_sdu_from_user = q->max_sdu[tc] ?: U32_MAX;
284 
285 		/* TC gate never closes => keep the queueMaxSDU
286 		 * selected by the user
287 		 */
288 		if (sched->max_open_gate_duration[tc] == sched->cycle_time) {
289 			max_sdu_dynamic = U32_MAX;
290 		} else {
291 			u32 max_frm_len;
292 
293 			max_frm_len = duration_to_length(q, sched->max_open_gate_duration[tc]);
294 			/* Compensate for L1 overhead from size table,
295 			 * but don't let the frame size go negative
296 			 */
297 			if (stab) {
298 				max_frm_len -= stab->szopts.overhead;
299 				max_frm_len = max_t(int, max_frm_len,
300 						    dev->hard_header_len + 1);
301 			}
302 			max_sdu_dynamic = max_frm_len - dev->hard_header_len;
303 			if (max_sdu_dynamic > dev->max_mtu)
304 				max_sdu_dynamic = U32_MAX;
305 		}
306 
307 		max_sdu = min(max_sdu_dynamic, max_sdu_from_user);
308 
309 		if (max_sdu != U32_MAX) {
310 			sched->max_frm_len[tc] = max_sdu + dev->hard_header_len;
311 			WRITE_ONCE(sched->max_sdu[tc], max_sdu);
312 		} else {
313 			sched->max_frm_len[tc] = U32_MAX; /* never oversized */
314 			WRITE_ONCE(sched->max_sdu[tc], 0);
315 		}
316 	}
317 }
318 
319 /* Returns the entry corresponding to next available interval. If
320  * validate_interval is set, it only validates whether the timestamp occurs
321  * when the gate corresponding to the skb's traffic class is open.
322  */
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)323 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
324 						  struct Qdisc *sch,
325 						  struct sched_gate_list *sched,
326 						  struct sched_gate_list *admin,
327 						  ktime_t time,
328 						  ktime_t *interval_start,
329 						  ktime_t *interval_end,
330 						  bool validate_interval)
331 {
332 	ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
333 	ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
334 	struct sched_entry *entry = NULL, *entry_found = NULL;
335 	struct taprio_sched *q = qdisc_priv(sch);
336 	struct net_device *dev = qdisc_dev(sch);
337 	bool entry_available = false;
338 	s32 cycle_elapsed;
339 	int tc, n;
340 
341 	tc = netdev_get_prio_tc_map(dev, skb->priority);
342 	packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
343 
344 	*interval_start = 0;
345 	*interval_end = 0;
346 
347 	if (!sched)
348 		return NULL;
349 
350 	cycle = sched->cycle_time;
351 	cycle_elapsed = get_cycle_time_elapsed(sched, time);
352 	curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
353 	cycle_end = ktime_add_ns(curr_intv_end, cycle);
354 
355 	list_for_each_entry(entry, &sched->entries, list) {
356 		curr_intv_start = curr_intv_end;
357 		curr_intv_end = get_interval_end_time(sched, admin, entry,
358 						      curr_intv_start);
359 
360 		if (ktime_after(curr_intv_start, cycle_end))
361 			break;
362 
363 		if (!(entry->gate_mask & BIT(tc)) ||
364 		    packet_transmit_time > entry->interval)
365 			continue;
366 
367 		txtime = entry->next_txtime;
368 
369 		if (ktime_before(txtime, time) || validate_interval) {
370 			transmit_end_time = ktime_add_ns(time, packet_transmit_time);
371 			if ((ktime_before(curr_intv_start, time) &&
372 			     ktime_before(transmit_end_time, curr_intv_end)) ||
373 			    (ktime_after(curr_intv_start, time) && !validate_interval)) {
374 				entry_found = entry;
375 				*interval_start = curr_intv_start;
376 				*interval_end = curr_intv_end;
377 				break;
378 			} else if (!entry_available && !validate_interval) {
379 				/* Here, we are just trying to find out the
380 				 * first available interval in the next cycle.
381 				 */
382 				entry_available = true;
383 				entry_found = entry;
384 				*interval_start = ktime_add_ns(curr_intv_start, cycle);
385 				*interval_end = ktime_add_ns(curr_intv_end, cycle);
386 			}
387 		} else if (ktime_before(txtime, earliest_txtime) &&
388 			   !entry_available) {
389 			earliest_txtime = txtime;
390 			entry_found = entry;
391 			n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
392 			*interval_start = ktime_add(curr_intv_start, n * cycle);
393 			*interval_end = ktime_add(curr_intv_end, n * cycle);
394 		}
395 	}
396 
397 	return entry_found;
398 }
399 
is_valid_interval(struct sk_buff * skb,struct Qdisc * sch)400 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
401 {
402 	struct taprio_sched *q = qdisc_priv(sch);
403 	struct sched_gate_list *sched, *admin;
404 	ktime_t interval_start, interval_end;
405 	struct sched_entry *entry;
406 
407 	rcu_read_lock();
408 	sched = rcu_dereference(q->oper_sched);
409 	admin = rcu_dereference(q->admin_sched);
410 
411 	entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
412 				       &interval_start, &interval_end, true);
413 	rcu_read_unlock();
414 
415 	return entry;
416 }
417 
418 /* 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)419 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
420 {
421 	unsigned int offset = skb_network_offset(skb);
422 	const struct ipv6hdr *ipv6h;
423 	const struct iphdr *iph;
424 	struct ipv6hdr _ipv6h;
425 
426 	ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
427 	if (!ipv6h)
428 		return 0;
429 
430 	if (ipv6h->version == 4) {
431 		iph = (struct iphdr *)ipv6h;
432 		offset += iph->ihl * 4;
433 
434 		/* special-case 6in4 tunnelling, as that is a common way to get
435 		 * v6 connectivity in the home
436 		 */
437 		if (iph->protocol == IPPROTO_IPV6) {
438 			ipv6h = skb_header_pointer(skb, offset,
439 						   sizeof(_ipv6h), &_ipv6h);
440 
441 			if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
442 				return 0;
443 		} else if (iph->protocol != IPPROTO_TCP) {
444 			return 0;
445 		}
446 	} else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
447 		return 0;
448 	}
449 
450 	return taprio_mono_to_any(q, skb->skb_mstamp_ns);
451 }
452 
453 /* There are a few scenarios where we will have to modify the txtime from
454  * what is read from next_txtime in sched_entry. They are:
455  * 1. If txtime is in the past,
456  *    a. The gate for the traffic class is currently open and packet can be
457  *       transmitted before it closes, schedule the packet right away.
458  *    b. If the gate corresponding to the traffic class is going to open later
459  *       in the cycle, set the txtime of packet to the interval start.
460  * 2. If txtime is in the future, there are packets corresponding to the
461  *    current traffic class waiting to be transmitted. So, the following
462  *    possibilities exist:
463  *    a. We can transmit the packet before the window containing the txtime
464  *       closes.
465  *    b. The window might close before the transmission can be completed
466  *       successfully. So, schedule the packet in the next open window.
467  */
get_packet_txtime(struct sk_buff * skb,struct Qdisc * sch)468 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
469 {
470 	ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
471 	struct taprio_sched *q = qdisc_priv(sch);
472 	struct sched_gate_list *sched, *admin;
473 	ktime_t minimum_time, now, txtime;
474 	int len, packet_transmit_time;
475 	struct sched_entry *entry;
476 	bool sched_changed;
477 
478 	now = taprio_get_time(q);
479 	minimum_time = ktime_add_ns(now, q->txtime_delay);
480 
481 	tcp_tstamp = get_tcp_tstamp(q, skb);
482 	minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
483 
484 	rcu_read_lock();
485 	admin = rcu_dereference(q->admin_sched);
486 	sched = rcu_dereference(q->oper_sched);
487 	if (admin && ktime_after(minimum_time, admin->base_time))
488 		switch_schedules(q, &admin, &sched);
489 
490 	/* Until the schedule starts, all the queues are open */
491 	if (!sched || ktime_before(minimum_time, sched->base_time)) {
492 		txtime = minimum_time;
493 		goto done;
494 	}
495 
496 	len = qdisc_pkt_len(skb);
497 	packet_transmit_time = length_to_duration(q, len);
498 
499 	do {
500 		sched_changed = false;
501 
502 		entry = find_entry_to_transmit(skb, sch, sched, admin,
503 					       minimum_time,
504 					       &interval_start, &interval_end,
505 					       false);
506 		if (!entry) {
507 			txtime = 0;
508 			goto done;
509 		}
510 
511 		txtime = entry->next_txtime;
512 		txtime = max_t(ktime_t, txtime, minimum_time);
513 		txtime = max_t(ktime_t, txtime, interval_start);
514 
515 		if (admin && admin != sched &&
516 		    ktime_after(txtime, admin->base_time)) {
517 			sched = admin;
518 			sched_changed = true;
519 			continue;
520 		}
521 
522 		transmit_end_time = ktime_add(txtime, packet_transmit_time);
523 		minimum_time = transmit_end_time;
524 
525 		/* Update the txtime of current entry to the next time it's
526 		 * interval starts.
527 		 */
528 		if (ktime_after(transmit_end_time, interval_end))
529 			entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
530 	} while (sched_changed || ktime_after(transmit_end_time, interval_end));
531 
532 	entry->next_txtime = transmit_end_time;
533 
534 done:
535 	rcu_read_unlock();
536 	return txtime;
537 }
538 
539 /* Devices with full offload are expected to honor this in hardware */
taprio_skb_exceeds_queue_max_sdu(struct Qdisc * sch,struct sk_buff * skb)540 static bool taprio_skb_exceeds_queue_max_sdu(struct Qdisc *sch,
541 					     struct sk_buff *skb)
542 {
543 	struct taprio_sched *q = qdisc_priv(sch);
544 	struct net_device *dev = qdisc_dev(sch);
545 	struct sched_gate_list *sched;
546 	int prio = skb->priority;
547 	bool exceeds = false;
548 	u8 tc;
549 
550 	tc = netdev_get_prio_tc_map(dev, prio);
551 
552 	rcu_read_lock();
553 	sched = rcu_dereference(q->oper_sched);
554 	if (sched && skb->len > sched->max_frm_len[tc])
555 		exceeds = true;
556 	rcu_read_unlock();
557 
558 	return exceeds;
559 }
560 
taprio_enqueue_one(struct sk_buff * skb,struct Qdisc * sch,struct Qdisc * child,struct sk_buff ** to_free)561 static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
562 			      struct Qdisc *child, struct sk_buff **to_free)
563 {
564 	struct taprio_sched *q = qdisc_priv(sch);
565 
566 	/* sk_flags are only safe to use on full sockets. */
567 	if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) {
568 		if (!is_valid_interval(skb, sch))
569 			return qdisc_drop(skb, sch, to_free);
570 	} else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
571 		skb->tstamp = get_packet_txtime(skb, sch);
572 		if (!skb->tstamp)
573 			return qdisc_drop(skb, sch, to_free);
574 	}
575 
576 	qdisc_qstats_backlog_inc(sch, skb);
577 	qdisc_qlen_inc(sch);
578 
579 	return qdisc_enqueue(skb, child, to_free);
580 }
581 
taprio_enqueue_segmented(struct sk_buff * skb,struct Qdisc * sch,struct Qdisc * child,struct sk_buff ** to_free)582 static int taprio_enqueue_segmented(struct sk_buff *skb, struct Qdisc *sch,
583 				    struct Qdisc *child,
584 				    struct sk_buff **to_free)
585 {
586 	unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb);
587 	netdev_features_t features = netif_skb_features(skb);
588 	struct sk_buff *segs, *nskb;
589 	int ret;
590 
591 	segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
592 	if (IS_ERR_OR_NULL(segs))
593 		return qdisc_drop(skb, sch, to_free);
594 
595 	skb_list_walk_safe(segs, segs, nskb) {
596 		skb_mark_not_on_list(segs);
597 		qdisc_skb_cb(segs)->pkt_len = segs->len;
598 		qdisc_skb_cb(segs)->pkt_segs = 1;
599 		slen += segs->len;
600 
601 		/* FIXME: we should be segmenting to a smaller size
602 		 * rather than dropping these
603 		 */
604 		if (taprio_skb_exceeds_queue_max_sdu(sch, segs))
605 			ret = qdisc_drop(segs, sch, to_free);
606 		else
607 			ret = taprio_enqueue_one(segs, sch, child, to_free);
608 
609 		if (ret != NET_XMIT_SUCCESS) {
610 			if (net_xmit_drop_count(ret))
611 				qdisc_qstats_drop(sch);
612 		} else {
613 			numsegs++;
614 		}
615 	}
616 
617 	if (numsegs > 1)
618 		qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
619 	consume_skb(skb);
620 
621 	return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
622 }
623 
624 /* Will not be called in the full offload case, since the TX queues are
625  * attached to the Qdisc created using qdisc_create_dflt()
626  */
taprio_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)627 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
628 			  struct sk_buff **to_free)
629 {
630 	struct taprio_sched *q = qdisc_priv(sch);
631 	struct Qdisc *child;
632 	int queue;
633 
634 	queue = skb_get_queue_mapping(skb);
635 
636 	child = q->qdiscs[queue];
637 	if (unlikely(child == &noop_qdisc))
638 		return qdisc_drop(skb, sch, to_free);
639 
640 	if (taprio_skb_exceeds_queue_max_sdu(sch, skb)) {
641 		/* Large packets might not be transmitted when the transmission
642 		 * duration exceeds any configured interval. Therefore, segment
643 		 * the skb into smaller chunks. Drivers with full offload are
644 		 * expected to handle this in hardware.
645 		 */
646 		if (skb_is_gso(skb))
647 			return taprio_enqueue_segmented(skb, sch, child,
648 							to_free);
649 
650 		return qdisc_drop(skb, sch, to_free);
651 	}
652 
653 	return taprio_enqueue_one(skb, sch, child, to_free);
654 }
655 
taprio_peek(struct Qdisc * sch)656 static struct sk_buff *taprio_peek(struct Qdisc *sch)
657 {
658 	WARN_ONCE(1, "taprio only supports operating as root qdisc, peek() not implemented");
659 	return NULL;
660 }
661 
taprio_set_budgets(struct taprio_sched * q,struct sched_gate_list * sched,struct sched_entry * entry)662 static void taprio_set_budgets(struct taprio_sched *q,
663 			       struct sched_gate_list *sched,
664 			       struct sched_entry *entry)
665 {
666 	struct net_device *dev = qdisc_dev(q->root);
667 	int num_tc = netdev_get_num_tc(dev);
668 	int tc, budget;
669 
670 	for (tc = 0; tc < num_tc; tc++) {
671 		/* Traffic classes which never close have infinite budget */
672 		if (entry->gate_duration[tc] == sched->cycle_time)
673 			budget = INT_MAX;
674 		else
675 			budget = div64_u64((u64)entry->gate_duration[tc] * PSEC_PER_NSEC,
676 					   atomic64_read(&q->picos_per_byte));
677 
678 		atomic_set(&entry->budget[tc], budget);
679 	}
680 }
681 
682 /* When an skb is sent, it consumes from the budget of all traffic classes */
taprio_update_budgets(struct sched_entry * entry,size_t len,int tc_consumed,int num_tc)683 static int taprio_update_budgets(struct sched_entry *entry, size_t len,
684 				 int tc_consumed, int num_tc)
685 {
686 	int tc, budget, new_budget = 0;
687 
688 	for (tc = 0; tc < num_tc; tc++) {
689 		budget = atomic_read(&entry->budget[tc]);
690 		/* Don't consume from infinite budget */
691 		if (budget == INT_MAX) {
692 			if (tc == tc_consumed)
693 				new_budget = budget;
694 			continue;
695 		}
696 
697 		if (tc == tc_consumed)
698 			new_budget = atomic_sub_return(len, &entry->budget[tc]);
699 		else
700 			atomic_sub(len, &entry->budget[tc]);
701 	}
702 
703 	return new_budget;
704 }
705 
taprio_dequeue_from_txq(struct Qdisc * sch,int txq,struct sched_entry * entry,u32 gate_mask)706 static struct sk_buff *taprio_dequeue_from_txq(struct Qdisc *sch, int txq,
707 					       struct sched_entry *entry,
708 					       u32 gate_mask)
709 {
710 	struct taprio_sched *q = qdisc_priv(sch);
711 	struct net_device *dev = qdisc_dev(sch);
712 	struct Qdisc *child = q->qdiscs[txq];
713 	int num_tc = netdev_get_num_tc(dev);
714 	struct sk_buff *skb;
715 	ktime_t guard;
716 	int prio;
717 	int len;
718 	u8 tc;
719 
720 	if (unlikely(child == &noop_qdisc))
721 		return NULL;
722 
723 	if (TXTIME_ASSIST_IS_ENABLED(q->flags))
724 		goto skip_peek_checks;
725 
726 	skb = child->ops->peek(child);
727 	if (!skb)
728 		return NULL;
729 
730 	prio = skb->priority;
731 	tc = netdev_get_prio_tc_map(dev, prio);
732 
733 	if (!(gate_mask & BIT(tc)))
734 		return NULL;
735 
736 	len = qdisc_pkt_len(skb);
737 	guard = ktime_add_ns(taprio_get_time(q), length_to_duration(q, len));
738 
739 	/* In the case that there's no gate entry, there's no
740 	 * guard band ...
741 	 */
742 	if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
743 	    !taprio_entry_allows_tx(guard, entry, tc))
744 		return NULL;
745 
746 	/* ... and no budget. */
747 	if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
748 	    taprio_update_budgets(entry, len, tc, num_tc) < 0)
749 		return NULL;
750 
751 skip_peek_checks:
752 	skb = qdisc_dequeue_peeked(child);
753 	if (unlikely(!skb))
754 		return NULL;
755 
756 	qdisc_bstats_update(sch, skb);
757 	qdisc_qstats_backlog_dec(sch, skb);
758 	qdisc_qlen_dec(sch);
759 
760 	return skb;
761 }
762 
taprio_next_tc_txq(struct net_device * dev,int tc,int * txq)763 static void taprio_next_tc_txq(struct net_device *dev, int tc, int *txq)
764 {
765 	struct netdev_tc_txq res;
766 
767 	res.combined = READ_ONCE(dev->tc_to_txq[tc].combined);
768 
769 	(*txq)++;
770 	if (*txq == res.offset + res.count)
771 		*txq = res.offset;
772 }
773 
774 /* Prioritize higher traffic classes, and select among TXQs belonging to the
775  * same TC using round robin
776  */
taprio_dequeue_tc_priority(struct Qdisc * sch,struct sched_entry * entry,u32 gate_mask)777 static struct sk_buff *taprio_dequeue_tc_priority(struct Qdisc *sch,
778 						  struct sched_entry *entry,
779 						  u32 gate_mask)
780 {
781 	struct taprio_sched *q = qdisc_priv(sch);
782 	struct net_device *dev = qdisc_dev(sch);
783 	int num_tc = netdev_get_num_tc(dev);
784 	struct sk_buff *skb;
785 	int tc;
786 
787 	for (tc = num_tc - 1; tc >= 0; tc--) {
788 		int first_txq = q->cur_txq[tc];
789 
790 		if (!(gate_mask & BIT(tc)))
791 			continue;
792 
793 		do {
794 			skb = taprio_dequeue_from_txq(sch, q->cur_txq[tc],
795 						      entry, gate_mask);
796 
797 			taprio_next_tc_txq(dev, tc, &q->cur_txq[tc]);
798 
799 			if (q->cur_txq[tc] >= dev->num_tx_queues)
800 				q->cur_txq[tc] = first_txq;
801 
802 			if (skb)
803 				return skb;
804 		} while (q->cur_txq[tc] != first_txq);
805 	}
806 
807 	return NULL;
808 }
809 
810 /* Broken way of prioritizing smaller TXQ indices and ignoring the traffic
811  * class other than to determine whether the gate is open or not
812  */
taprio_dequeue_txq_priority(struct Qdisc * sch,struct sched_entry * entry,u32 gate_mask)813 static struct sk_buff *taprio_dequeue_txq_priority(struct Qdisc *sch,
814 						   struct sched_entry *entry,
815 						   u32 gate_mask)
816 {
817 	struct net_device *dev = qdisc_dev(sch);
818 	struct sk_buff *skb;
819 	int i;
820 
821 	for (i = 0; i < dev->num_tx_queues; i++) {
822 		skb = taprio_dequeue_from_txq(sch, i, entry, gate_mask);
823 		if (skb)
824 			return skb;
825 	}
826 
827 	return NULL;
828 }
829 
830 /* Will not be called in the full offload case, since the TX queues are
831  * attached to the Qdisc created using qdisc_create_dflt()
832  */
taprio_dequeue(struct Qdisc * sch)833 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
834 {
835 	struct taprio_sched *q = qdisc_priv(sch);
836 	struct sk_buff *skb = NULL;
837 	struct sched_entry *entry;
838 	u32 gate_mask;
839 
840 	rcu_read_lock();
841 	entry = rcu_dereference(q->current_entry);
842 	/* if there's no entry, it means that the schedule didn't
843 	 * start yet, so force all gates to be open, this is in
844 	 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
845 	 * "AdminGateStates"
846 	 */
847 	gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
848 	if (!gate_mask)
849 		goto done;
850 
851 	if (static_branch_unlikely(&taprio_have_broken_mqprio) &&
852 	    !static_branch_likely(&taprio_have_working_mqprio)) {
853 		/* Single NIC kind which is broken */
854 		skb = taprio_dequeue_txq_priority(sch, entry, gate_mask);
855 	} else if (static_branch_likely(&taprio_have_working_mqprio) &&
856 		   !static_branch_unlikely(&taprio_have_broken_mqprio)) {
857 		/* Single NIC kind which prioritizes properly */
858 		skb = taprio_dequeue_tc_priority(sch, entry, gate_mask);
859 	} else {
860 		/* Mixed NIC kinds present in system, need dynamic testing */
861 		if (q->broken_mqprio)
862 			skb = taprio_dequeue_txq_priority(sch, entry, gate_mask);
863 		else
864 			skb = taprio_dequeue_tc_priority(sch, entry, gate_mask);
865 	}
866 
867 done:
868 	rcu_read_unlock();
869 
870 	return skb;
871 }
872 
should_restart_cycle(const struct sched_gate_list * oper,const struct sched_entry * entry)873 static bool should_restart_cycle(const struct sched_gate_list *oper,
874 				 const struct sched_entry *entry)
875 {
876 	if (list_is_last(&entry->list, &oper->entries))
877 		return true;
878 
879 	if (ktime_compare(entry->end_time, oper->cycle_end_time) == 0)
880 		return true;
881 
882 	return false;
883 }
884 
should_change_schedules(const struct sched_gate_list * admin,const struct sched_gate_list * oper,ktime_t end_time)885 static bool should_change_schedules(const struct sched_gate_list *admin,
886 				    const struct sched_gate_list *oper,
887 				    ktime_t end_time)
888 {
889 	ktime_t next_base_time, extension_time;
890 
891 	if (!admin)
892 		return false;
893 
894 	next_base_time = sched_base_time(admin);
895 
896 	/* This is the simple case, the end_time would fall after
897 	 * the next schedule base_time.
898 	 */
899 	if (ktime_compare(next_base_time, end_time) <= 0)
900 		return true;
901 
902 	/* This is the cycle_time_extension case, if the end_time
903 	 * plus the amount that can be extended would fall after the
904 	 * next schedule base_time, we can extend the current schedule
905 	 * for that amount.
906 	 */
907 	extension_time = ktime_add_ns(end_time, oper->cycle_time_extension);
908 
909 	/* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
910 	 * how precisely the extension should be made. So after
911 	 * conformance testing, this logic may change.
912 	 */
913 	if (ktime_compare(next_base_time, extension_time) <= 0)
914 		return true;
915 
916 	return false;
917 }
918 
advance_sched(struct hrtimer * timer)919 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
920 {
921 	struct taprio_sched *q = container_of(timer, struct taprio_sched,
922 					      advance_timer);
923 	struct net_device *dev = qdisc_dev(q->root);
924 	struct sched_gate_list *oper, *admin;
925 	int num_tc = netdev_get_num_tc(dev);
926 	struct sched_entry *entry, *next;
927 	struct Qdisc *sch = q->root;
928 	ktime_t end_time;
929 	int tc;
930 
931 	spin_lock(&q->current_entry_lock);
932 	entry = rcu_dereference_protected(q->current_entry,
933 					  lockdep_is_held(&q->current_entry_lock));
934 	oper = rcu_dereference_protected(q->oper_sched,
935 					 lockdep_is_held(&q->current_entry_lock));
936 	admin = rcu_dereference_protected(q->admin_sched,
937 					  lockdep_is_held(&q->current_entry_lock));
938 
939 	if (!oper)
940 		switch_schedules(q, &admin, &oper);
941 
942 	/* This can happen in two cases: 1. this is the very first run
943 	 * of this function (i.e. we weren't running any schedule
944 	 * previously); 2. The previous schedule just ended. The first
945 	 * entry of all schedules are pre-calculated during the
946 	 * schedule initialization.
947 	 */
948 	if (unlikely(!entry || entry->end_time == oper->base_time)) {
949 		next = list_first_entry(&oper->entries, struct sched_entry,
950 					list);
951 		end_time = next->end_time;
952 		goto first_run;
953 	}
954 
955 	if (should_restart_cycle(oper, entry)) {
956 		next = list_first_entry(&oper->entries, struct sched_entry,
957 					list);
958 		oper->cycle_end_time = ktime_add_ns(oper->cycle_end_time,
959 						    oper->cycle_time);
960 	} else {
961 		next = list_next_entry(entry, list);
962 	}
963 
964 	end_time = ktime_add_ns(entry->end_time, next->interval);
965 	end_time = min_t(ktime_t, end_time, oper->cycle_end_time);
966 
967 	for (tc = 0; tc < num_tc; tc++) {
968 		if (next->gate_duration[tc] == oper->cycle_time)
969 			next->gate_close_time[tc] = KTIME_MAX;
970 		else
971 			next->gate_close_time[tc] = ktime_add_ns(entry->end_time,
972 								 next->gate_duration[tc]);
973 	}
974 
975 	if (should_change_schedules(admin, oper, end_time)) {
976 		switch_schedules(q, &admin, &oper);
977 		/* After changing schedules, the next entry is the first one
978 		 * in the new schedule, with a pre-calculated end_time.
979 		 */
980 		next = list_first_entry(&oper->entries, struct sched_entry, list);
981 		end_time = next->end_time;
982 	}
983 
984 	next->end_time = end_time;
985 	taprio_set_budgets(q, oper, next);
986 
987 first_run:
988 	rcu_assign_pointer(q->current_entry, next);
989 	spin_unlock(&q->current_entry_lock);
990 
991 	hrtimer_set_expires(&q->advance_timer, end_time);
992 
993 	rcu_read_lock();
994 	__netif_schedule(sch);
995 	rcu_read_unlock();
996 
997 	return HRTIMER_RESTART;
998 }
999 
1000 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
1001 	[TCA_TAPRIO_SCHED_ENTRY_INDEX]	   = { .type = NLA_U32 },
1002 	[TCA_TAPRIO_SCHED_ENTRY_CMD]	   = { .type = NLA_U8 },
1003 	[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
1004 	[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]  = { .type = NLA_U32 },
1005 };
1006 
1007 static const struct nla_policy taprio_tc_policy[TCA_TAPRIO_TC_ENTRY_MAX + 1] = {
1008 	[TCA_TAPRIO_TC_ENTRY_INDEX]	   = NLA_POLICY_MAX(NLA_U32,
1009 							    TC_QOPT_MAX_QUEUE - 1),
1010 	[TCA_TAPRIO_TC_ENTRY_MAX_SDU]	   = { .type = NLA_U32 },
1011 	[TCA_TAPRIO_TC_ENTRY_FP]	   = NLA_POLICY_RANGE(NLA_U32,
1012 							      TC_FP_EXPRESS,
1013 							      TC_FP_PREEMPTIBLE),
1014 };
1015 
1016 static const struct netlink_range_validation_signed taprio_cycle_time_range = {
1017 	.min = 0,
1018 	.max = INT_MAX,
1019 };
1020 
1021 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
1022 	[TCA_TAPRIO_ATTR_PRIOMAP]	       = {
1023 		.len = sizeof(struct tc_mqprio_qopt)
1024 	},
1025 	[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST]           = { .type = NLA_NESTED },
1026 	[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]            = { .type = NLA_S64 },
1027 	[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]         = { .type = NLA_NESTED },
1028 	[TCA_TAPRIO_ATTR_SCHED_CLOCKID]              = { .type = NLA_S32 },
1029 	[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]           =
1030 		NLA_POLICY_FULL_RANGE_SIGNED(NLA_S64, &taprio_cycle_time_range),
1031 	[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
1032 	[TCA_TAPRIO_ATTR_FLAGS]                      =
1033 		NLA_POLICY_MASK(NLA_U32, TAPRIO_SUPPORTED_FLAGS),
1034 	[TCA_TAPRIO_ATTR_TXTIME_DELAY]		     = { .type = NLA_U32 },
1035 	[TCA_TAPRIO_ATTR_TC_ENTRY]		     = { .type = NLA_NESTED },
1036 };
1037 
fill_sched_entry(struct taprio_sched * q,struct nlattr ** tb,struct sched_entry * entry,struct netlink_ext_ack * extack)1038 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
1039 			    struct sched_entry *entry,
1040 			    struct netlink_ext_ack *extack)
1041 {
1042 	int min_duration = length_to_duration(q, ETH_ZLEN);
1043 	u32 interval = 0;
1044 
1045 	if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
1046 		entry->command = nla_get_u8(
1047 			tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
1048 
1049 	if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
1050 		entry->gate_mask = nla_get_u32(
1051 			tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
1052 
1053 	if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
1054 		interval = nla_get_u32(
1055 			tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
1056 
1057 	/* The interval should allow at least the minimum ethernet
1058 	 * frame to go out.
1059 	 */
1060 	if (interval < min_duration) {
1061 		NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
1062 		return -EINVAL;
1063 	}
1064 
1065 	entry->interval = interval;
1066 
1067 	return 0;
1068 }
1069 
parse_sched_entry(struct taprio_sched * q,struct nlattr * n,struct sched_entry * entry,int index,struct netlink_ext_ack * extack)1070 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
1071 			     struct sched_entry *entry, int index,
1072 			     struct netlink_ext_ack *extack)
1073 {
1074 	struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
1075 	int err;
1076 
1077 	err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
1078 					  entry_policy, NULL);
1079 	if (err < 0) {
1080 		NL_SET_ERR_MSG(extack, "Could not parse nested entry");
1081 		return -EINVAL;
1082 	}
1083 
1084 	entry->index = index;
1085 
1086 	return fill_sched_entry(q, tb, entry, extack);
1087 }
1088 
parse_sched_list(struct taprio_sched * q,struct nlattr * list,struct sched_gate_list * sched,struct netlink_ext_ack * extack)1089 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
1090 			    struct sched_gate_list *sched,
1091 			    struct netlink_ext_ack *extack)
1092 {
1093 	struct nlattr *n;
1094 	int err, rem;
1095 	int i = 0;
1096 
1097 	if (!list)
1098 		return -EINVAL;
1099 
1100 	nla_for_each_nested(n, list, rem) {
1101 		struct sched_entry *entry;
1102 
1103 		if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
1104 			NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
1105 			continue;
1106 		}
1107 
1108 		entry = kzalloc_obj(*entry);
1109 		if (!entry) {
1110 			NL_SET_ERR_MSG(extack, "Not enough memory for entry");
1111 			return -ENOMEM;
1112 		}
1113 
1114 		err = parse_sched_entry(q, n, entry, i, extack);
1115 		if (err < 0) {
1116 			kfree(entry);
1117 			return err;
1118 		}
1119 
1120 		list_add_tail(&entry->list, &sched->entries);
1121 		i++;
1122 	}
1123 
1124 	sched->num_entries = i;
1125 
1126 	return i;
1127 }
1128 
parse_taprio_schedule(struct taprio_sched * q,struct nlattr ** tb,struct sched_gate_list * new,struct netlink_ext_ack * extack)1129 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
1130 				 struct sched_gate_list *new,
1131 				 struct netlink_ext_ack *extack)
1132 {
1133 	int err = 0;
1134 
1135 	if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
1136 		NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
1137 		return -ENOTSUPP;
1138 	}
1139 
1140 	if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
1141 		new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
1142 
1143 	if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
1144 		new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
1145 
1146 	if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
1147 		new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
1148 
1149 	if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
1150 		err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
1151 				       new, extack);
1152 	if (err < 0)
1153 		return err;
1154 
1155 	if (!new->cycle_time) {
1156 		struct sched_entry *entry;
1157 		ktime_t cycle = 0;
1158 
1159 		list_for_each_entry(entry, &new->entries, list)
1160 			cycle = ktime_add_ns(cycle, entry->interval);
1161 
1162 		if (cycle < 0 || cycle > INT_MAX) {
1163 			NL_SET_ERR_MSG(extack, "'cycle_time' is too big");
1164 			return -EINVAL;
1165 		}
1166 
1167 		new->cycle_time = cycle;
1168 	}
1169 
1170 	if (new->cycle_time < new->num_entries * length_to_duration(q, ETH_ZLEN)) {
1171 		NL_SET_ERR_MSG(extack, "'cycle_time' is too small");
1172 		return -EINVAL;
1173 	}
1174 
1175 	taprio_calculate_gate_durations(q, new);
1176 
1177 	return 0;
1178 }
1179 
taprio_parse_mqprio_opt(struct net_device * dev,struct tc_mqprio_qopt * qopt,struct netlink_ext_ack * extack,u32 taprio_flags)1180 static int taprio_parse_mqprio_opt(struct net_device *dev,
1181 				   struct tc_mqprio_qopt *qopt,
1182 				   struct netlink_ext_ack *extack,
1183 				   u32 taprio_flags)
1184 {
1185 	bool allow_overlapping_txqs = TXTIME_ASSIST_IS_ENABLED(taprio_flags);
1186 
1187 	if (!qopt) {
1188 		if (!netdev_get_num_tc(dev)) {
1189 			NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
1190 			return -EINVAL;
1191 		}
1192 		return 0;
1193 	}
1194 
1195 	/* taprio imposes that traffic classes map 1:n to tx queues */
1196 	if (qopt->num_tc > dev->num_tx_queues) {
1197 		NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
1198 		return -EINVAL;
1199 	}
1200 
1201 	/* For some reason, in txtime-assist mode, we allow TXQ ranges for
1202 	 * different TCs to overlap, and just validate the TXQ ranges.
1203 	 */
1204 	return mqprio_validate_qopt(dev, qopt, true, allow_overlapping_txqs,
1205 				    extack);
1206 }
1207 
taprio_get_start_time(struct Qdisc * sch,struct sched_gate_list * sched,ktime_t * start)1208 static int taprio_get_start_time(struct Qdisc *sch,
1209 				 struct sched_gate_list *sched,
1210 				 ktime_t *start)
1211 {
1212 	struct taprio_sched *q = qdisc_priv(sch);
1213 	ktime_t now, base, cycle;
1214 	s64 n;
1215 
1216 	base = sched_base_time(sched);
1217 	now = taprio_get_time(q);
1218 
1219 	if (ktime_after(base, now)) {
1220 		*start = base;
1221 		return 0;
1222 	}
1223 
1224 	cycle = sched->cycle_time;
1225 
1226 	/* The qdisc is expected to have at least one sched_entry.  Moreover,
1227 	 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
1228 	 * something went really wrong. In that case, we should warn about this
1229 	 * inconsistent state and return error.
1230 	 */
1231 	if (WARN_ON(!cycle))
1232 		return -EFAULT;
1233 
1234 	/* Schedule the start time for the beginning of the next
1235 	 * cycle.
1236 	 */
1237 	n = div64_s64(ktime_sub_ns(now, base), cycle);
1238 	*start = ktime_add_ns(base, (n + 1) * cycle);
1239 	return 0;
1240 }
1241 
setup_first_end_time(struct taprio_sched * q,struct sched_gate_list * sched,ktime_t base)1242 static void setup_first_end_time(struct taprio_sched *q,
1243 				 struct sched_gate_list *sched, ktime_t base)
1244 {
1245 	struct net_device *dev = qdisc_dev(q->root);
1246 	int num_tc = netdev_get_num_tc(dev);
1247 	struct sched_entry *first;
1248 	ktime_t cycle;
1249 	int tc;
1250 
1251 	first = list_first_entry(&sched->entries,
1252 				 struct sched_entry, list);
1253 
1254 	cycle = sched->cycle_time;
1255 
1256 	/* FIXME: find a better place to do this */
1257 	sched->cycle_end_time = ktime_add_ns(base, cycle);
1258 
1259 	first->end_time = ktime_add_ns(base, first->interval);
1260 	taprio_set_budgets(q, sched, first);
1261 
1262 	for (tc = 0; tc < num_tc; tc++) {
1263 		if (first->gate_duration[tc] == sched->cycle_time)
1264 			first->gate_close_time[tc] = KTIME_MAX;
1265 		else
1266 			first->gate_close_time[tc] = ktime_add_ns(base, first->gate_duration[tc]);
1267 	}
1268 
1269 	rcu_assign_pointer(q->current_entry, NULL);
1270 }
1271 
taprio_start_sched(struct Qdisc * sch,ktime_t start,struct sched_gate_list * new)1272 static void taprio_start_sched(struct Qdisc *sch,
1273 			       ktime_t start, struct sched_gate_list *new)
1274 {
1275 	struct taprio_sched *q = qdisc_priv(sch);
1276 	ktime_t expires;
1277 
1278 	if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1279 		return;
1280 
1281 	expires = hrtimer_get_expires(&q->advance_timer);
1282 	if (expires == 0)
1283 		expires = KTIME_MAX;
1284 
1285 	/* If the new schedule starts before the next expiration, we
1286 	 * reprogram it to the earliest one, so we change the admin
1287 	 * schedule to the operational one at the right time.
1288 	 */
1289 	start = min_t(ktime_t, start, expires);
1290 
1291 	hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1292 }
1293 
taprio_set_picos_per_byte(struct net_device * dev,struct taprio_sched * q,struct netlink_ext_ack * extack)1294 static void taprio_set_picos_per_byte(struct net_device *dev,
1295 				      struct taprio_sched *q,
1296 				      struct netlink_ext_ack *extack)
1297 {
1298 	struct ethtool_link_ksettings ecmd;
1299 	int speed = SPEED_10;
1300 	int picos_per_byte;
1301 	int err;
1302 
1303 	err = netif_get_link_ksettings(dev, &ecmd);
1304 	if (err < 0)
1305 		goto skip;
1306 
1307 	if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1308 		speed = ecmd.base.speed;
1309 
1310 skip:
1311 	picos_per_byte = (USEC_PER_SEC * 8) / speed;
1312 	if (picos_per_byte < TAPRIO_PICOS_PER_BYTE_MIN) {
1313 		if (!extack)
1314 			pr_warn("Link speed %d is too high. Schedule may be inaccurate.\n",
1315 				speed);
1316 		NL_SET_ERR_MSG_FMT_MOD(extack,
1317 				       "Link speed %d is too high. Schedule may be inaccurate.",
1318 				       speed);
1319 		picos_per_byte = TAPRIO_PICOS_PER_BYTE_MIN;
1320 	}
1321 
1322 	atomic64_set(&q->picos_per_byte, picos_per_byte);
1323 	netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1324 		   dev->name, (long long)atomic64_read(&q->picos_per_byte),
1325 		   speed);
1326 }
1327 
taprio_dev_notifier(struct notifier_block * nb,unsigned long event,void * ptr)1328 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1329 			       void *ptr)
1330 {
1331 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1332 	struct sched_gate_list *oper, *admin;
1333 	struct qdisc_size_table *stab;
1334 	struct taprio_sched *q;
1335 
1336 	ASSERT_RTNL();
1337 
1338 	if (event != NETDEV_UP && event != NETDEV_CHANGE)
1339 		return NOTIFY_DONE;
1340 
1341 	list_for_each_entry(q, &taprio_list, taprio_list) {
1342 		if (dev != qdisc_dev(q->root))
1343 			continue;
1344 
1345 		taprio_set_picos_per_byte(dev, q, NULL);
1346 
1347 		stab = rtnl_dereference(q->root->stab);
1348 
1349 		rcu_read_lock();
1350 		oper = rcu_dereference(q->oper_sched);
1351 		if (oper)
1352 			taprio_update_queue_max_sdu(q, oper, stab);
1353 
1354 		admin = rcu_dereference(q->admin_sched);
1355 		if (admin)
1356 			taprio_update_queue_max_sdu(q, admin, stab);
1357 		rcu_read_unlock();
1358 
1359 		break;
1360 	}
1361 
1362 	return NOTIFY_DONE;
1363 }
1364 
setup_txtime(struct taprio_sched * q,struct sched_gate_list * sched,ktime_t base)1365 static void setup_txtime(struct taprio_sched *q,
1366 			 struct sched_gate_list *sched, ktime_t base)
1367 {
1368 	struct sched_entry *entry;
1369 	u64 interval = 0;
1370 
1371 	list_for_each_entry(entry, &sched->entries, list) {
1372 		entry->next_txtime = ktime_add_ns(base, interval);
1373 		interval += entry->interval;
1374 	}
1375 }
1376 
taprio_offload_alloc(int num_entries)1377 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1378 {
1379 	struct __tc_taprio_qopt_offload *__offload;
1380 
1381 	__offload = kzalloc_flex(*__offload, offload.entries, num_entries);
1382 	if (!__offload)
1383 		return NULL;
1384 
1385 	refcount_set(&__offload->users, 1);
1386 
1387 	return &__offload->offload;
1388 }
1389 
taprio_offload_get(struct tc_taprio_qopt_offload * offload)1390 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1391 						  *offload)
1392 {
1393 	struct __tc_taprio_qopt_offload *__offload;
1394 
1395 	__offload = container_of(offload, struct __tc_taprio_qopt_offload,
1396 				 offload);
1397 
1398 	refcount_inc(&__offload->users);
1399 
1400 	return offload;
1401 }
1402 EXPORT_SYMBOL_GPL(taprio_offload_get);
1403 
taprio_offload_free(struct tc_taprio_qopt_offload * offload)1404 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1405 {
1406 	struct __tc_taprio_qopt_offload *__offload;
1407 
1408 	__offload = container_of(offload, struct __tc_taprio_qopt_offload,
1409 				 offload);
1410 
1411 	if (!refcount_dec_and_test(&__offload->users))
1412 		return;
1413 
1414 	kfree(__offload);
1415 }
1416 EXPORT_SYMBOL_GPL(taprio_offload_free);
1417 
1418 /* The function will only serve to keep the pointers to the "oper" and "admin"
1419  * schedules valid in relation to their base times, so when calling dump() the
1420  * users looks at the right schedules.
1421  * When using full offload, the admin configuration is promoted to oper at the
1422  * base_time in the PHC time domain.  But because the system time is not
1423  * necessarily in sync with that, we can't just trigger a hrtimer to call
1424  * switch_schedules at the right hardware time.
1425  * At the moment we call this by hand right away from taprio, but in the future
1426  * it will be useful to create a mechanism for drivers to notify taprio of the
1427  * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1428  * This is left as TODO.
1429  */
taprio_offload_config_changed(struct taprio_sched * q)1430 static void taprio_offload_config_changed(struct taprio_sched *q)
1431 {
1432 	struct sched_gate_list *oper, *admin;
1433 
1434 	oper = rtnl_dereference(q->oper_sched);
1435 	admin = rtnl_dereference(q->admin_sched);
1436 
1437 	switch_schedules(q, &admin, &oper);
1438 }
1439 
tc_map_to_queue_mask(struct net_device * dev,u32 tc_mask)1440 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1441 {
1442 	int num_tc = netdev_get_num_tc(dev);
1443 	u32 i, queue_mask = 0;
1444 
1445 	for (i = 0; i < num_tc; i++) {
1446 		struct netdev_tc_txq res;
1447 
1448 		if (!(tc_mask & BIT(i)))
1449 			continue;
1450 
1451 		res.combined = READ_ONCE(dev->tc_to_txq[i].combined);
1452 
1453 		queue_mask |= GENMASK(res.offset + res.count - 1, res.offset);
1454 	}
1455 
1456 	return queue_mask;
1457 }
1458 
taprio_sched_to_offload(struct net_device * dev,struct sched_gate_list * sched,struct tc_taprio_qopt_offload * offload,const struct tc_taprio_caps * caps)1459 static void taprio_sched_to_offload(struct net_device *dev,
1460 				    struct sched_gate_list *sched,
1461 				    struct tc_taprio_qopt_offload *offload,
1462 				    const struct tc_taprio_caps *caps)
1463 {
1464 	struct sched_entry *entry;
1465 	int i = 0;
1466 
1467 	offload->base_time = sched->base_time;
1468 	offload->cycle_time = sched->cycle_time;
1469 	offload->cycle_time_extension = sched->cycle_time_extension;
1470 
1471 	list_for_each_entry(entry, &sched->entries, list) {
1472 		struct tc_taprio_sched_entry *e = &offload->entries[i];
1473 
1474 		e->command = entry->command;
1475 		e->interval = entry->interval;
1476 		if (caps->gate_mask_per_txq)
1477 			e->gate_mask = tc_map_to_queue_mask(dev,
1478 							    entry->gate_mask);
1479 		else
1480 			e->gate_mask = entry->gate_mask;
1481 
1482 		i++;
1483 	}
1484 
1485 	offload->num_entries = i;
1486 }
1487 
taprio_detect_broken_mqprio(struct taprio_sched * q)1488 static void taprio_detect_broken_mqprio(struct taprio_sched *q)
1489 {
1490 	struct net_device *dev = qdisc_dev(q->root);
1491 	struct tc_taprio_caps caps;
1492 
1493 	qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO,
1494 				 &caps, sizeof(caps));
1495 
1496 	q->broken_mqprio = caps.broken_mqprio;
1497 	if (q->broken_mqprio)
1498 		static_branch_inc(&taprio_have_broken_mqprio);
1499 	else
1500 		static_branch_inc(&taprio_have_working_mqprio);
1501 
1502 	q->detected_mqprio = true;
1503 }
1504 
taprio_cleanup_broken_mqprio(struct taprio_sched * q)1505 static void taprio_cleanup_broken_mqprio(struct taprio_sched *q)
1506 {
1507 	if (!q->detected_mqprio)
1508 		return;
1509 
1510 	if (q->broken_mqprio)
1511 		static_branch_dec(&taprio_have_broken_mqprio);
1512 	else
1513 		static_branch_dec(&taprio_have_working_mqprio);
1514 }
1515 
taprio_enable_offload(struct net_device * dev,struct taprio_sched * q,struct sched_gate_list * sched,struct netlink_ext_ack * extack)1516 static int taprio_enable_offload(struct net_device *dev,
1517 				 struct taprio_sched *q,
1518 				 struct sched_gate_list *sched,
1519 				 struct netlink_ext_ack *extack)
1520 {
1521 	const struct net_device_ops *ops = dev->netdev_ops;
1522 	struct tc_taprio_qopt_offload *offload;
1523 	struct tc_taprio_caps caps;
1524 	int tc, err = 0;
1525 
1526 	if (!ops->ndo_setup_tc) {
1527 		NL_SET_ERR_MSG(extack,
1528 			       "Device does not support taprio offload");
1529 		return -EOPNOTSUPP;
1530 	}
1531 
1532 	qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO,
1533 				 &caps, sizeof(caps));
1534 
1535 	if (!caps.supports_queue_max_sdu) {
1536 		for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
1537 			if (q->max_sdu[tc]) {
1538 				NL_SET_ERR_MSG_MOD(extack,
1539 						   "Device does not handle queueMaxSDU");
1540 				return -EOPNOTSUPP;
1541 			}
1542 		}
1543 	}
1544 
1545 	offload = taprio_offload_alloc(sched->num_entries);
1546 	if (!offload) {
1547 		NL_SET_ERR_MSG(extack,
1548 			       "Not enough memory for enabling offload mode");
1549 		return -ENOMEM;
1550 	}
1551 	offload->cmd = TAPRIO_CMD_REPLACE;
1552 	offload->extack = extack;
1553 	mqprio_qopt_reconstruct(dev, &offload->mqprio.qopt);
1554 	offload->mqprio.extack = extack;
1555 	taprio_sched_to_offload(dev, sched, offload, &caps);
1556 	mqprio_fp_to_offload(q->fp, &offload->mqprio);
1557 
1558 	for (tc = 0; tc < TC_MAX_QUEUE; tc++)
1559 		offload->max_sdu[tc] = q->max_sdu[tc];
1560 
1561 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1562 	if (err < 0) {
1563 		NL_SET_ERR_MSG_WEAK(extack,
1564 				    "Device failed to setup taprio offload");
1565 		goto done;
1566 	}
1567 
1568 	q->offloaded = true;
1569 
1570 done:
1571 	/* The offload structure may linger around via a reference taken by the
1572 	 * device driver, so clear up the netlink extack pointer so that the
1573 	 * driver isn't tempted to dereference data which stopped being valid
1574 	 */
1575 	offload->extack = NULL;
1576 	offload->mqprio.extack = NULL;
1577 	taprio_offload_free(offload);
1578 
1579 	return err;
1580 }
1581 
taprio_disable_offload(struct net_device * dev,struct taprio_sched * q,struct netlink_ext_ack * extack)1582 static int taprio_disable_offload(struct net_device *dev,
1583 				  struct taprio_sched *q,
1584 				  struct netlink_ext_ack *extack)
1585 {
1586 	const struct net_device_ops *ops = dev->netdev_ops;
1587 	struct tc_taprio_qopt_offload *offload;
1588 	int err;
1589 
1590 	if (!q->offloaded)
1591 		return 0;
1592 
1593 	offload = taprio_offload_alloc(0);
1594 	if (!offload) {
1595 		NL_SET_ERR_MSG(extack,
1596 			       "Not enough memory to disable offload mode");
1597 		return -ENOMEM;
1598 	}
1599 	offload->cmd = TAPRIO_CMD_DESTROY;
1600 
1601 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1602 	if (err < 0) {
1603 		NL_SET_ERR_MSG(extack,
1604 			       "Device failed to disable offload");
1605 		goto out;
1606 	}
1607 
1608 	q->offloaded = false;
1609 
1610 out:
1611 	taprio_offload_free(offload);
1612 
1613 	return err;
1614 }
1615 
1616 /* If full offload is enabled, the only possible clockid is the net device's
1617  * PHC. For that reason, specifying a clockid through netlink is incorrect.
1618  * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1619  * in sync with the specified clockid via a user space daemon such as phc2sys.
1620  * For both software taprio and txtime-assist, the clockid is used for the
1621  * hrtimer that advances the schedule and hence mandatory.
1622  */
taprio_parse_clockid(struct Qdisc * sch,struct nlattr ** tb,struct netlink_ext_ack * extack)1623 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1624 				struct netlink_ext_ack *extack)
1625 {
1626 	struct taprio_sched *q = qdisc_priv(sch);
1627 	struct net_device *dev = qdisc_dev(sch);
1628 	int err = -EINVAL;
1629 
1630 	if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1631 		const struct ethtool_ops *ops = dev->ethtool_ops;
1632 		struct kernel_ethtool_ts_info info = {
1633 			.cmd = ETHTOOL_GET_TS_INFO,
1634 			.phc_index = -1,
1635 		};
1636 
1637 		if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1638 			NL_SET_ERR_MSG(extack,
1639 				       "The 'clockid' cannot be specified for full offload");
1640 			goto out;
1641 		}
1642 
1643 		if (ops && ops->get_ts_info)
1644 			err = ops->get_ts_info(dev, &info);
1645 
1646 		if (err || info.phc_index < 0) {
1647 			NL_SET_ERR_MSG(extack,
1648 				       "Device does not have a PTP clock");
1649 			err = -ENOTSUPP;
1650 			goto out;
1651 		}
1652 	} else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1653 		int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1654 		enum tk_offsets tk_offset;
1655 
1656 		/* We only support static clockids and we don't allow
1657 		 * for it to be modified after the first init.
1658 		 */
1659 		if (clockid < 0 ||
1660 		    (q->clockid != -1 && q->clockid != clockid)) {
1661 			NL_SET_ERR_MSG(extack,
1662 				       "Changing the 'clockid' of a running schedule is not supported");
1663 			err = -ENOTSUPP;
1664 			goto out;
1665 		}
1666 
1667 		switch (clockid) {
1668 		case CLOCK_REALTIME:
1669 			tk_offset = TK_OFFS_REAL;
1670 			break;
1671 		case CLOCK_MONOTONIC:
1672 			tk_offset = TK_OFFS_MAX;
1673 			break;
1674 		case CLOCK_BOOTTIME:
1675 			tk_offset = TK_OFFS_BOOT;
1676 			break;
1677 		case CLOCK_TAI:
1678 			tk_offset = TK_OFFS_TAI;
1679 			break;
1680 		default:
1681 			NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1682 			err = -EINVAL;
1683 			goto out;
1684 		}
1685 		/* This pairs with READ_ONCE() in taprio_mono_to_any */
1686 		WRITE_ONCE(q->tk_offset, tk_offset);
1687 
1688 		q->clockid = clockid;
1689 	} else {
1690 		NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1691 		goto out;
1692 	}
1693 
1694 	/* Everything went ok, return success. */
1695 	err = 0;
1696 
1697 out:
1698 	return err;
1699 }
1700 
taprio_parse_tc_entry(struct Qdisc * sch,struct nlattr * opt,u32 max_sdu[TC_QOPT_MAX_QUEUE],u32 fp[TC_QOPT_MAX_QUEUE],unsigned long * seen_tcs,struct netlink_ext_ack * extack)1701 static int taprio_parse_tc_entry(struct Qdisc *sch,
1702 				 struct nlattr *opt,
1703 				 u32 max_sdu[TC_QOPT_MAX_QUEUE],
1704 				 u32 fp[TC_QOPT_MAX_QUEUE],
1705 				 unsigned long *seen_tcs,
1706 				 struct netlink_ext_ack *extack)
1707 {
1708 	struct nlattr *tb[TCA_TAPRIO_TC_ENTRY_MAX + 1] = { };
1709 	struct net_device *dev = qdisc_dev(sch);
1710 	int err, tc;
1711 	u32 val;
1712 
1713 	err = nla_parse_nested(tb, TCA_TAPRIO_TC_ENTRY_MAX, opt,
1714 			       taprio_tc_policy, extack);
1715 	if (err < 0)
1716 		return err;
1717 
1718 	if (NL_REQ_ATTR_CHECK(extack, opt, tb, TCA_TAPRIO_TC_ENTRY_INDEX)) {
1719 		NL_SET_ERR_MSG_MOD(extack, "TC entry index missing");
1720 		return -EINVAL;
1721 	}
1722 
1723 	tc = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_INDEX]);
1724 	if (*seen_tcs & BIT(tc)) {
1725 		NL_SET_ERR_MSG_ATTR(extack, tb[TCA_TAPRIO_TC_ENTRY_INDEX],
1726 				    "Duplicate tc entry");
1727 		return -EINVAL;
1728 	}
1729 
1730 	*seen_tcs |= BIT(tc);
1731 
1732 	if (tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]) {
1733 		val = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]);
1734 		if (val > dev->max_mtu) {
1735 			NL_SET_ERR_MSG_MOD(extack, "TC max SDU exceeds device max MTU");
1736 			return -ERANGE;
1737 		}
1738 
1739 		max_sdu[tc] = val;
1740 	}
1741 
1742 	if (tb[TCA_TAPRIO_TC_ENTRY_FP])
1743 		fp[tc] = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_FP]);
1744 
1745 	return 0;
1746 }
1747 
taprio_parse_tc_entries(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)1748 static int taprio_parse_tc_entries(struct Qdisc *sch,
1749 				   struct nlattr *opt,
1750 				   struct netlink_ext_ack *extack)
1751 {
1752 	struct taprio_sched *q = qdisc_priv(sch);
1753 	struct net_device *dev = qdisc_dev(sch);
1754 	u32 max_sdu[TC_QOPT_MAX_QUEUE];
1755 	bool have_preemption = false;
1756 	unsigned long seen_tcs = 0;
1757 	u32 fp[TC_QOPT_MAX_QUEUE];
1758 	struct nlattr *n;
1759 	int tc, rem;
1760 	int err = 0;
1761 
1762 	for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) {
1763 		max_sdu[tc] = q->max_sdu[tc];
1764 		fp[tc] = q->fp[tc];
1765 	}
1766 
1767 	nla_for_each_nested_type(n, TCA_TAPRIO_ATTR_TC_ENTRY, opt, rem) {
1768 		err = taprio_parse_tc_entry(sch, n, max_sdu, fp, &seen_tcs,
1769 					    extack);
1770 		if (err)
1771 			return err;
1772 	}
1773 
1774 	for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) {
1775 		WRITE_ONCE(q->max_sdu[tc], max_sdu[tc]);
1776 		WRITE_ONCE(q->fp[tc], fp[tc]);
1777 		if (fp[tc] != TC_FP_EXPRESS)
1778 			have_preemption = true;
1779 	}
1780 
1781 	if (have_preemption) {
1782 		if (!FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1783 			NL_SET_ERR_MSG(extack,
1784 				       "Preemption only supported with full offload");
1785 			return -EOPNOTSUPP;
1786 		}
1787 
1788 		if (!ethtool_dev_mm_supported(dev)) {
1789 			NL_SET_ERR_MSG(extack,
1790 				       "Device does not support preemption");
1791 			return -EOPNOTSUPP;
1792 		}
1793 	}
1794 
1795 	return err;
1796 }
1797 
taprio_mqprio_cmp(const struct net_device * dev,const struct tc_mqprio_qopt * mqprio)1798 static int taprio_mqprio_cmp(const struct net_device *dev,
1799 			     const struct tc_mqprio_qopt *mqprio)
1800 {
1801 	int i;
1802 
1803 	if (!mqprio || mqprio->num_tc != netdev_get_num_tc(dev))
1804 		return -1;
1805 
1806 	for (i = 0; i < mqprio->num_tc; i++) {
1807 		struct netdev_tc_txq res;
1808 
1809 		res.combined = READ_ONCE(dev->tc_to_txq[i].combined);
1810 		if (res.count != mqprio->count[i] ||
1811 		    res.offset != mqprio->offset[i])
1812 			return -1;
1813 	}
1814 
1815 	for (i = 0; i <= TC_BITMASK; i++)
1816 		if (netdev_get_prio_tc_map(dev, i) != mqprio->prio_tc_map[i])
1817 			return -1;
1818 
1819 	return 0;
1820 }
1821 
taprio_change(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)1822 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1823 			 struct netlink_ext_ack *extack)
1824 {
1825 	struct qdisc_size_table *stab = rtnl_dereference(sch->stab);
1826 	struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1827 	struct sched_gate_list *oper, *admin, *new_admin;
1828 	struct taprio_sched *q = qdisc_priv(sch);
1829 	struct net_device *dev = qdisc_dev(sch);
1830 	struct tc_mqprio_qopt *mqprio = NULL;
1831 	unsigned long flags;
1832 	u32 taprio_flags;
1833 	ktime_t start;
1834 	int i, err;
1835 
1836 	err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1837 					  taprio_policy, extack);
1838 	if (err < 0)
1839 		return err;
1840 
1841 	if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1842 		mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1843 
1844 	/* The semantics of the 'flags' argument in relation to 'change()'
1845 	 * requests, are interpreted following two rules (which are applied in
1846 	 * this order): (1) an omitted 'flags' argument is interpreted as
1847 	 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1848 	 * changed.
1849 	 */
1850 	taprio_flags = nla_get_u32_default(tb[TCA_TAPRIO_ATTR_FLAGS], 0);
1851 
1852 	/* txtime-assist and full offload are mutually exclusive */
1853 	if ((taprio_flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
1854 	    (taprio_flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)) {
1855 		NL_SET_ERR_MSG_ATTR(extack, tb[TCA_TAPRIO_ATTR_FLAGS],
1856 				    "TXTIME_ASSIST and FULL_OFFLOAD are mutually exclusive");
1857 		return -EINVAL;
1858 	}
1859 
1860 	if (q->flags != taprio_flags) {
1861 		if (q->flags != TAPRIO_FLAGS_INVALID) {
1862 			NL_SET_ERR_MSG_MOD(extack,
1863 					   "Changing 'flags' of a running schedule is not supported");
1864 			return -EOPNOTSUPP;
1865 		}
1866 		WRITE_ONCE(q->flags, taprio_flags);
1867 	}
1868 
1869 	/* Needed for length_to_duration() during netlink attribute parsing */
1870 	taprio_set_picos_per_byte(dev, q, extack);
1871 
1872 	err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1873 	if (err < 0)
1874 		return err;
1875 
1876 	err = taprio_parse_tc_entries(sch, opt, extack);
1877 	if (err)
1878 		return err;
1879 
1880 	new_admin = kzalloc_obj(*new_admin);
1881 	if (!new_admin) {
1882 		NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1883 		return -ENOMEM;
1884 	}
1885 	INIT_LIST_HEAD(&new_admin->entries);
1886 
1887 	oper = rtnl_dereference(q->oper_sched);
1888 	admin = rtnl_dereference(q->admin_sched);
1889 
1890 	/* no changes - no new mqprio settings */
1891 	if (!taprio_mqprio_cmp(dev, mqprio))
1892 		mqprio = NULL;
1893 
1894 	if (mqprio && (oper || admin)) {
1895 		NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1896 		err = -ENOTSUPP;
1897 		goto free_sched;
1898 	}
1899 
1900 	if (mqprio) {
1901 		err = netdev_set_num_tc(dev, mqprio->num_tc);
1902 		if (err)
1903 			goto free_sched;
1904 		for (i = 0; i < mqprio->num_tc; i++) {
1905 			netdev_set_tc_queue(dev, i,
1906 					    mqprio->count[i],
1907 					    mqprio->offset[i]);
1908 			q->cur_txq[i] = mqprio->offset[i];
1909 		}
1910 
1911 		/* Always use supplied priority mappings */
1912 		for (i = 0; i <= TC_BITMASK; i++)
1913 			netdev_set_prio_tc_map(dev, i,
1914 					       mqprio->prio_tc_map[i]);
1915 	}
1916 
1917 	err = parse_taprio_schedule(q, tb, new_admin, extack);
1918 	if (err < 0)
1919 		goto free_sched;
1920 
1921 	if (new_admin->num_entries == 0) {
1922 		NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1923 		err = -EINVAL;
1924 		goto free_sched;
1925 	}
1926 
1927 	err = taprio_parse_clockid(sch, tb, extack);
1928 	if (err < 0)
1929 		goto free_sched;
1930 
1931 	taprio_update_queue_max_sdu(q, new_admin, stab);
1932 
1933 	if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1934 		err = taprio_enable_offload(dev, q, new_admin, extack);
1935 	else
1936 		err = taprio_disable_offload(dev, q, extack);
1937 	if (err)
1938 		goto free_sched;
1939 
1940 	/* Protects against enqueue()/dequeue() */
1941 	spin_lock_bh(qdisc_lock(sch));
1942 
1943 	if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1944 		if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1945 			NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1946 			err = -EINVAL;
1947 			goto unlock;
1948 		}
1949 
1950 		WRITE_ONCE(q->txtime_delay,
1951 			   nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]));
1952 	}
1953 
1954 	if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1955 	    !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1956 	    !hrtimer_active(&q->advance_timer)) {
1957 		hrtimer_setup(&q->advance_timer, advance_sched, q->clockid, HRTIMER_MODE_ABS);
1958 	}
1959 
1960 	err = taprio_get_start_time(sch, new_admin, &start);
1961 	if (err < 0) {
1962 		NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1963 		goto unlock;
1964 	}
1965 
1966 	setup_txtime(q, new_admin, start);
1967 
1968 	if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1969 		if (!oper) {
1970 			rcu_assign_pointer(q->oper_sched, new_admin);
1971 			err = 0;
1972 			new_admin = NULL;
1973 			goto unlock;
1974 		}
1975 
1976 		/* Not going to race against advance_sched(), but still */
1977 		admin = rcu_replace_pointer(q->admin_sched, new_admin,
1978 					    lockdep_rtnl_is_held());
1979 		if (admin)
1980 			call_rcu(&admin->rcu, taprio_free_sched_cb);
1981 	} else {
1982 		setup_first_end_time(q, new_admin, start);
1983 
1984 		/* Protects against advance_sched() */
1985 		spin_lock_irqsave(&q->current_entry_lock, flags);
1986 
1987 		taprio_start_sched(sch, start, new_admin);
1988 
1989 		admin = rcu_replace_pointer(q->admin_sched, new_admin,
1990 					    lockdep_rtnl_is_held());
1991 		if (admin)
1992 			call_rcu(&admin->rcu, taprio_free_sched_cb);
1993 
1994 		spin_unlock_irqrestore(&q->current_entry_lock, flags);
1995 
1996 		if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1997 			taprio_offload_config_changed(q);
1998 	}
1999 
2000 	new_admin = NULL;
2001 	err = 0;
2002 
2003 	if (!stab)
2004 		NL_SET_ERR_MSG_MOD(extack,
2005 				   "Size table not specified, frame length estimations may be inaccurate");
2006 
2007 unlock:
2008 	spin_unlock_bh(qdisc_lock(sch));
2009 
2010 free_sched:
2011 	if (new_admin)
2012 		call_rcu(&new_admin->rcu, taprio_free_sched_cb);
2013 
2014 	return err;
2015 }
2016 
taprio_reset(struct Qdisc * sch)2017 static void taprio_reset(struct Qdisc *sch)
2018 {
2019 	struct taprio_sched *q = qdisc_priv(sch);
2020 	struct net_device *dev = qdisc_dev(sch);
2021 	int i;
2022 
2023 	hrtimer_cancel(&q->advance_timer);
2024 
2025 	if (q->qdiscs) {
2026 		for (i = 0; i < dev->num_tx_queues; i++)
2027 			if (q->qdiscs[i])
2028 				qdisc_reset(q->qdiscs[i]);
2029 	}
2030 }
2031 
taprio_destroy(struct Qdisc * sch)2032 static void taprio_destroy(struct Qdisc *sch)
2033 {
2034 	struct taprio_sched *q = qdisc_priv(sch);
2035 	struct net_device *dev = qdisc_dev(sch);
2036 	struct sched_gate_list *oper, *admin;
2037 	unsigned int i;
2038 
2039 	list_del(&q->taprio_list);
2040 
2041 	/* Note that taprio_reset() might not be called if an error
2042 	 * happens in qdisc_create(), after taprio_init() has been called.
2043 	 */
2044 	hrtimer_cancel(&q->advance_timer);
2045 	qdisc_synchronize(sch);
2046 
2047 	taprio_disable_offload(dev, q, NULL);
2048 
2049 	if (q->qdiscs) {
2050 		for (i = 0; i < dev->num_tx_queues; i++)
2051 			qdisc_put(q->qdiscs[i]);
2052 
2053 		kfree(q->qdiscs);
2054 	}
2055 	q->qdiscs = NULL;
2056 
2057 	netdev_reset_tc(dev);
2058 
2059 	oper = rtnl_dereference(q->oper_sched);
2060 	admin = rtnl_dereference(q->admin_sched);
2061 
2062 	if (oper)
2063 		call_rcu(&oper->rcu, taprio_free_sched_cb);
2064 
2065 	if (admin)
2066 		call_rcu(&admin->rcu, taprio_free_sched_cb);
2067 
2068 	taprio_cleanup_broken_mqprio(q);
2069 }
2070 
taprio_init(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)2071 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
2072 		       struct netlink_ext_ack *extack)
2073 {
2074 	struct taprio_sched *q = qdisc_priv(sch);
2075 	struct net_device *dev = qdisc_dev(sch);
2076 	int i, tc;
2077 
2078 	spin_lock_init(&q->current_entry_lock);
2079 
2080 	hrtimer_setup(&q->advance_timer, advance_sched, CLOCK_TAI, HRTIMER_MODE_ABS);
2081 
2082 	q->root = sch;
2083 
2084 	/* We only support static clockids. Use an invalid value as default
2085 	 * and get the valid one on taprio_change().
2086 	 */
2087 	q->clockid = -1;
2088 	q->flags = TAPRIO_FLAGS_INVALID;
2089 
2090 	list_add(&q->taprio_list, &taprio_list);
2091 
2092 	if (sch->parent != TC_H_ROOT) {
2093 		NL_SET_ERR_MSG_MOD(extack, "Can only be attached as root qdisc");
2094 		return -EOPNOTSUPP;
2095 	}
2096 
2097 	if (!netif_is_multiqueue(dev)) {
2098 		NL_SET_ERR_MSG_MOD(extack, "Multi-queue device is required");
2099 		return -EOPNOTSUPP;
2100 	}
2101 
2102 	q->qdiscs = kzalloc_objs(q->qdiscs[0], dev->num_tx_queues);
2103 	if (!q->qdiscs)
2104 		return -ENOMEM;
2105 
2106 	if (!opt)
2107 		return -EINVAL;
2108 
2109 	for (i = 0; i < dev->num_tx_queues; i++) {
2110 		struct netdev_queue *dev_queue;
2111 		struct Qdisc *qdisc;
2112 
2113 		dev_queue = netdev_get_tx_queue(dev, i);
2114 		qdisc = qdisc_create_dflt(dev_queue,
2115 					  &pfifo_qdisc_ops,
2116 					  TC_H_MAKE(TC_H_MAJ(sch->handle),
2117 						    TC_H_MIN(i + 1)),
2118 					  extack);
2119 		if (!qdisc)
2120 			return -ENOMEM;
2121 
2122 		if (i < dev->real_num_tx_queues)
2123 			qdisc_hash_add(qdisc, false);
2124 
2125 		q->qdiscs[i] = qdisc;
2126 	}
2127 
2128 	for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++)
2129 		q->fp[tc] = TC_FP_EXPRESS;
2130 
2131 	taprio_detect_broken_mqprio(q);
2132 
2133 	return taprio_change(sch, opt, extack);
2134 }
2135 
taprio_attach(struct Qdisc * sch)2136 static void taprio_attach(struct Qdisc *sch)
2137 {
2138 	struct taprio_sched *q = qdisc_priv(sch);
2139 	struct net_device *dev = qdisc_dev(sch);
2140 	unsigned int ntx;
2141 
2142 	/* Attach underlying qdisc */
2143 	for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
2144 		struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, ntx);
2145 		struct Qdisc *old, *dev_queue_qdisc;
2146 
2147 		if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
2148 			struct Qdisc *qdisc = q->qdiscs[ntx];
2149 
2150 			/* In offload mode, the root taprio qdisc is bypassed
2151 			 * and the netdev TX queues see the children directly
2152 			 */
2153 			qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
2154 			dev_queue_qdisc = qdisc;
2155 		} else {
2156 			/* In software mode, attach the root taprio qdisc
2157 			 * to all netdev TX queues, so that dev_qdisc_enqueue()
2158 			 * goes through taprio_enqueue().
2159 			 */
2160 			dev_queue_qdisc = sch;
2161 		}
2162 		old = dev_graft_qdisc(dev_queue, dev_queue_qdisc);
2163 		/* The qdisc's refcount requires to be elevated once
2164 		 * for each netdev TX queue it is grafted onto
2165 		 */
2166 		qdisc_refcount_inc(dev_queue_qdisc);
2167 		if (old)
2168 			qdisc_put(old);
2169 	}
2170 }
2171 
taprio_queue_get(struct Qdisc * sch,unsigned long cl)2172 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
2173 					     unsigned long cl)
2174 {
2175 	struct net_device *dev = qdisc_dev(sch);
2176 	unsigned long ntx = cl - 1;
2177 
2178 	if (ntx >= dev->num_tx_queues)
2179 		return NULL;
2180 
2181 	return netdev_get_tx_queue(dev, ntx);
2182 }
2183 
taprio_graft(struct Qdisc * sch,unsigned long cl,struct Qdisc * new,struct Qdisc ** old,struct netlink_ext_ack * extack)2184 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
2185 			struct Qdisc *new, struct Qdisc **old,
2186 			struct netlink_ext_ack *extack)
2187 {
2188 	struct taprio_sched *q = qdisc_priv(sch);
2189 	struct net_device *dev = qdisc_dev(sch);
2190 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
2191 
2192 	if (!dev_queue)
2193 		return -EINVAL;
2194 
2195 	if (!new)
2196 		new = &noop_qdisc;
2197 
2198 	if (dev->flags & IFF_UP)
2199 		dev_deactivate(dev, false);
2200 
2201 	/* In offload mode, the child Qdisc is directly attached to the netdev
2202 	 * TX queue, and thus, we need to keep its refcount elevated in order
2203 	 * to counteract qdisc_graft()'s call to qdisc_put() once per TX queue.
2204 	 * However, save the reference to the new qdisc in the private array in
2205 	 * both software and offload cases, to have an up-to-date reference to
2206 	 * our children.
2207 	 */
2208 	*old = q->qdiscs[cl - 1];
2209 	if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
2210 		WARN_ON_ONCE(dev_graft_qdisc(dev_queue, new) != *old);
2211 		if (new != &noop_qdisc)
2212 			qdisc_refcount_inc(new);
2213 		if (*old && *old != &noop_qdisc)
2214 			qdisc_put(*old);
2215 	}
2216 
2217 	q->qdiscs[cl - 1] = new;
2218 	if (new != &noop_qdisc)
2219 		new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
2220 
2221 	if (dev->flags & IFF_UP)
2222 		dev_activate(dev);
2223 
2224 	return 0;
2225 }
2226 
dump_entry(struct sk_buff * msg,const struct sched_entry * entry)2227 static int dump_entry(struct sk_buff *msg,
2228 		      const struct sched_entry *entry)
2229 {
2230 	struct nlattr *item;
2231 
2232 	item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
2233 	if (!item)
2234 		return -ENOSPC;
2235 
2236 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
2237 		goto nla_put_failure;
2238 
2239 	if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
2240 		goto nla_put_failure;
2241 
2242 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
2243 			entry->gate_mask))
2244 		goto nla_put_failure;
2245 
2246 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
2247 			entry->interval))
2248 		goto nla_put_failure;
2249 
2250 	return nla_nest_end(msg, item);
2251 
2252 nla_put_failure:
2253 	nla_nest_cancel(msg, item);
2254 	return -1;
2255 }
2256 
dump_schedule(struct sk_buff * msg,const struct sched_gate_list * root)2257 static int dump_schedule(struct sk_buff *msg,
2258 			 const struct sched_gate_list *root)
2259 {
2260 	struct nlattr *entry_list;
2261 	struct sched_entry *entry;
2262 
2263 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
2264 			root->base_time, TCA_TAPRIO_PAD))
2265 		return -1;
2266 
2267 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
2268 			root->cycle_time, TCA_TAPRIO_PAD))
2269 		return -1;
2270 
2271 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
2272 			root->cycle_time_extension, TCA_TAPRIO_PAD))
2273 		return -1;
2274 
2275 	entry_list = nla_nest_start_noflag(msg,
2276 					   TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
2277 	if (!entry_list)
2278 		goto error_nest;
2279 
2280 	list_for_each_entry(entry, &root->entries, list) {
2281 		if (dump_entry(msg, entry) < 0)
2282 			goto error_nest;
2283 	}
2284 
2285 	nla_nest_end(msg, entry_list);
2286 	return 0;
2287 
2288 error_nest:
2289 	nla_nest_cancel(msg, entry_list);
2290 	return -1;
2291 }
2292 
taprio_dump_tc_entries(struct sk_buff * skb,const struct taprio_sched * q,const struct sched_gate_list * sched)2293 static int taprio_dump_tc_entries(struct sk_buff *skb,
2294 				  const struct taprio_sched *q,
2295 				  const struct sched_gate_list *sched)
2296 {
2297 	struct nlattr *n;
2298 	int tc;
2299 
2300 	for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
2301 		n = nla_nest_start(skb, TCA_TAPRIO_ATTR_TC_ENTRY);
2302 		if (!n)
2303 			return -EMSGSIZE;
2304 
2305 		if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_INDEX, tc))
2306 			goto nla_put_failure;
2307 
2308 		if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_MAX_SDU,
2309 				READ_ONCE(sched->max_sdu[tc])))
2310 			goto nla_put_failure;
2311 
2312 		if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_FP,
2313 				READ_ONCE(q->fp[tc])))
2314 			goto nla_put_failure;
2315 
2316 		nla_nest_end(skb, n);
2317 	}
2318 
2319 	return 0;
2320 
2321 nla_put_failure:
2322 	nla_nest_cancel(skb, n);
2323 	return -EMSGSIZE;
2324 }
2325 
taprio_put_stat(struct sk_buff * skb,u64 val,u16 attrtype)2326 static int taprio_put_stat(struct sk_buff *skb, u64 val, u16 attrtype)
2327 {
2328 	if (val == TAPRIO_STAT_NOT_SET)
2329 		return 0;
2330 	if (nla_put_u64_64bit(skb, attrtype, val, TCA_TAPRIO_OFFLOAD_STATS_PAD))
2331 		return -EMSGSIZE;
2332 	return 0;
2333 }
2334 
taprio_dump_xstats(struct Qdisc * sch,struct gnet_dump * d,struct tc_taprio_qopt_offload * offload,struct tc_taprio_qopt_stats * stats)2335 static int taprio_dump_xstats(struct Qdisc *sch, struct gnet_dump *d,
2336 			      struct tc_taprio_qopt_offload *offload,
2337 			      struct tc_taprio_qopt_stats *stats)
2338 {
2339 	struct net_device *dev = qdisc_dev(sch);
2340 	const struct net_device_ops *ops;
2341 	struct sk_buff *skb = d->skb;
2342 	struct nlattr *xstats;
2343 	int err;
2344 
2345 	ops = qdisc_dev(sch)->netdev_ops;
2346 
2347 	/* FIXME I could use qdisc_offload_dump_helper(), but that messes
2348 	 * with sch->flags depending on whether the device reports taprio
2349 	 * stats, and I'm not sure whether that's a good idea, considering
2350 	 * that stats are optional to the offload itself
2351 	 */
2352 	if (!ops->ndo_setup_tc)
2353 		return 0;
2354 
2355 	memset(stats, 0xff, sizeof(*stats));
2356 
2357 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
2358 	if (err == -EOPNOTSUPP)
2359 		return 0;
2360 	if (err)
2361 		return err;
2362 
2363 	xstats = nla_nest_start(skb, TCA_STATS_APP);
2364 	if (!xstats)
2365 		goto err;
2366 
2367 	if (taprio_put_stat(skb, stats->window_drops,
2368 			    TCA_TAPRIO_OFFLOAD_STATS_WINDOW_DROPS) ||
2369 	    taprio_put_stat(skb, stats->tx_overruns,
2370 			    TCA_TAPRIO_OFFLOAD_STATS_TX_OVERRUNS))
2371 		goto err_cancel;
2372 
2373 	nla_nest_end(skb, xstats);
2374 
2375 	return 0;
2376 
2377 err_cancel:
2378 	nla_nest_cancel(skb, xstats);
2379 err:
2380 	return -EMSGSIZE;
2381 }
2382 
taprio_dump_stats(struct Qdisc * sch,struct gnet_dump * d)2383 static int taprio_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
2384 {
2385 	struct tc_taprio_qopt_offload offload = {
2386 		.cmd = TAPRIO_CMD_STATS,
2387 	};
2388 
2389 	return taprio_dump_xstats(sch, d, &offload, &offload.stats);
2390 }
2391 
taprio_dump(struct Qdisc * sch,struct sk_buff * skb)2392 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
2393 {
2394 	struct taprio_sched *q = qdisc_priv(sch);
2395 	struct net_device *dev = qdisc_dev(sch);
2396 	struct sched_gate_list *oper, *admin;
2397 	struct tc_mqprio_qopt opt = { 0 };
2398 	struct nlattr *nest, *sched_nest;
2399 	u32 txtime_delay;
2400 
2401 	mqprio_qopt_reconstruct(dev, &opt);
2402 
2403 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
2404 	if (!nest)
2405 		goto start_error;
2406 
2407 	if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
2408 		goto options_error;
2409 
2410 	if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
2411 	    nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
2412 		goto options_error;
2413 
2414 	if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
2415 		goto options_error;
2416 
2417 	txtime_delay = READ_ONCE(q->txtime_delay);
2418 	if (txtime_delay &&
2419 	    nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, txtime_delay))
2420 		goto options_error;
2421 
2422 	rcu_read_lock();
2423 
2424 	oper = rcu_dereference(q->oper_sched);
2425 	admin = rcu_dereference(q->admin_sched);
2426 
2427 	if (oper && taprio_dump_tc_entries(skb, q, oper))
2428 		goto options_error_rcu;
2429 
2430 	if (oper && dump_schedule(skb, oper))
2431 		goto options_error_rcu;
2432 
2433 	if (!admin)
2434 		goto done;
2435 
2436 	sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
2437 	if (!sched_nest)
2438 		goto options_error_rcu;
2439 
2440 	if (dump_schedule(skb, admin))
2441 		goto admin_error;
2442 
2443 	nla_nest_end(skb, sched_nest);
2444 
2445 done:
2446 	rcu_read_unlock();
2447 	return nla_nest_end(skb, nest);
2448 
2449 admin_error:
2450 	nla_nest_cancel(skb, sched_nest);
2451 
2452 options_error_rcu:
2453 	rcu_read_unlock();
2454 
2455 options_error:
2456 	nla_nest_cancel(skb, nest);
2457 
2458 start_error:
2459 	return -ENOSPC;
2460 }
2461 
taprio_leaf(struct Qdisc * sch,unsigned long cl)2462 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
2463 {
2464 	struct taprio_sched *q = qdisc_priv(sch);
2465 	struct net_device *dev = qdisc_dev(sch);
2466 	unsigned int ntx = cl - 1;
2467 
2468 	if (ntx >= dev->num_tx_queues)
2469 		return NULL;
2470 
2471 	return q->qdiscs[ntx];
2472 }
2473 
taprio_find(struct Qdisc * sch,u32 classid)2474 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
2475 {
2476 	unsigned int ntx = TC_H_MIN(classid);
2477 
2478 	if (!taprio_queue_get(sch, ntx))
2479 		return 0;
2480 	return ntx;
2481 }
2482 
taprio_dump_class(struct Qdisc * sch,unsigned long cl,struct sk_buff * skb,struct tcmsg * tcm)2483 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
2484 			     struct sk_buff *skb, struct tcmsg *tcm)
2485 {
2486 	struct Qdisc *child = taprio_leaf(sch, cl);
2487 
2488 	tcm->tcm_parent = TC_H_ROOT;
2489 	tcm->tcm_handle |= TC_H_MIN(cl);
2490 	tcm->tcm_info = child->handle;
2491 
2492 	return 0;
2493 }
2494 
taprio_dump_class_stats(struct Qdisc * sch,unsigned long cl,struct gnet_dump * d)2495 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
2496 				   struct gnet_dump *d)
2497 	__releases(d->lock)
2498 	__acquires(d->lock)
2499 {
2500 	struct Qdisc *child = taprio_leaf(sch, cl);
2501 	struct tc_taprio_qopt_offload offload = {
2502 		.cmd = TAPRIO_CMD_QUEUE_STATS,
2503 		.queue_stats = {
2504 			.queue = cl - 1,
2505 		},
2506 	};
2507 
2508 	if (gnet_stats_copy_basic(d, NULL, &child->bstats, true) < 0 ||
2509 	    qdisc_qstats_copy(d, child) < 0)
2510 		return -1;
2511 
2512 	return taprio_dump_xstats(sch, d, &offload, &offload.queue_stats.stats);
2513 }
2514 
taprio_walk(struct Qdisc * sch,struct qdisc_walker * arg)2515 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
2516 {
2517 	struct net_device *dev = qdisc_dev(sch);
2518 	unsigned long ntx;
2519 
2520 	if (arg->stop)
2521 		return;
2522 
2523 	arg->count = arg->skip;
2524 	for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
2525 		if (!tc_qdisc_stats_dump(sch, ntx + 1, arg))
2526 			break;
2527 	}
2528 }
2529 
taprio_select_queue(struct Qdisc * sch,struct tcmsg * tcm)2530 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
2531 						struct tcmsg *tcm)
2532 {
2533 	return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
2534 }
2535 
2536 static const struct Qdisc_class_ops taprio_class_ops = {
2537 	.graft		= taprio_graft,
2538 	.leaf		= taprio_leaf,
2539 	.find		= taprio_find,
2540 	.walk		= taprio_walk,
2541 	.dump		= taprio_dump_class,
2542 	.dump_stats	= taprio_dump_class_stats,
2543 	.select_queue	= taprio_select_queue,
2544 };
2545 
2546 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
2547 	.cl_ops		= &taprio_class_ops,
2548 	.id		= "taprio",
2549 	.priv_size	= sizeof(struct taprio_sched),
2550 	.init		= taprio_init,
2551 	.change		= taprio_change,
2552 	.destroy	= taprio_destroy,
2553 	.reset		= taprio_reset,
2554 	.attach		= taprio_attach,
2555 	.peek		= taprio_peek,
2556 	.dequeue	= taprio_dequeue,
2557 	.enqueue	= taprio_enqueue,
2558 	.dump		= taprio_dump,
2559 	.dump_stats	= taprio_dump_stats,
2560 	.owner		= THIS_MODULE,
2561 };
2562 MODULE_ALIAS_NET_SCH("taprio");
2563 
2564 static struct notifier_block taprio_device_notifier = {
2565 	.notifier_call = taprio_dev_notifier,
2566 };
2567 
taprio_module_init(void)2568 static int __init taprio_module_init(void)
2569 {
2570 	int err = register_netdevice_notifier(&taprio_device_notifier);
2571 
2572 	if (err)
2573 		return err;
2574 
2575 	return register_qdisc(&taprio_qdisc_ops);
2576 }
2577 
taprio_module_exit(void)2578 static void __exit taprio_module_exit(void)
2579 {
2580 	unregister_qdisc(&taprio_qdisc_ops);
2581 	unregister_netdevice_notifier(&taprio_device_notifier);
2582 }
2583 
2584 module_init(taprio_module_init);
2585 module_exit(taprio_module_exit);
2586 MODULE_LICENSE("GPL");
2587 MODULE_DESCRIPTION("Time Aware Priority qdisc");
2588