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