xref: /linux/net/sched/sch_generic.c (revision d603517771d8e08a2d8fc9e1f7682ce393d3973a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/sch_generic.c	Generic packet scheduler routines.
4  *
5  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6  *              Jamal Hadi Salim, <hadi@cyberus.ca> 990601
7  *              - Ingress support
8  */
9 
10 #include <linux/bitops.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/netdevice.h>
18 #include <linux/skbuff.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/init.h>
21 #include <linux/rcupdate.h>
22 #include <linux/list.h>
23 #include <linux/slab.h>
24 #include <linux/if_vlan.h>
25 #include <linux/skb_array.h>
26 #include <linux/if_macvlan.h>
27 #include <linux/bpf.h>
28 #include <trace/events/qdisc.h>
29 #include <net/sch_generic.h>
30 #include <net/pkt_sched.h>
31 #include <net/dst.h>
32 #include <net/hotdata.h>
33 #include <trace/events/net.h>
34 #include <net/xfrm.h>
35 
36 /* Qdisc to use by default */
37 const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops;
38 EXPORT_SYMBOL(default_qdisc_ops);
39 
40 void __tcf_kfree_skb_list(struct sk_buff *skb, struct Qdisc *q,
41 			  struct netdev_queue *txq, struct net_device *dev)
42 {
43 	while (skb) {
44 		u32 reason = tc_skb_cb(skb)->drop_reason;
45 		struct sk_buff *next = skb->next;
46 		enum skb_drop_reason skb_reason;
47 
48 		prefetch(next);
49 		/* TC classifier and qdisc share drop_reason storage.
50 		 * Check subsystem mask to identify qdisc drop reasons,
51 		 * else pass through skb_drop_reason set by TC classifier.
52 		 */
53 		if ((reason & SKB_DROP_REASON_SUBSYS_MASK) == __QDISC_DROP_REASON) {
54 			trace_qdisc_drop(q, txq, dev, skb, (enum qdisc_drop_reason)reason);
55 			skb_reason = SKB_DROP_REASON_QDISC_DROP;
56 		} else {
57 			skb_reason = (enum skb_drop_reason)reason;
58 		}
59 		kfree_skb_reason(skb, skb_reason);
60 		skb = next;
61 	}
62 }
63 EXPORT_SYMBOL(__tcf_kfree_skb_list);
64 
65 static void qdisc_maybe_clear_missed(struct Qdisc *q,
66 				     const struct netdev_queue *txq)
67 {
68 	clear_bit(__QDISC_STATE_MISSED, &q->state);
69 
70 	/* Make sure the below netif_xmit_frozen_or_stopped()
71 	 * checking happens after clearing STATE_MISSED.
72 	 */
73 	smp_mb__after_atomic();
74 
75 	/* Checking netif_xmit_frozen_or_stopped() again to
76 	 * make sure STATE_MISSED is set if the STATE_MISSED
77 	 * set by netif_tx_wake_queue()'s rescheduling of
78 	 * net_tx_action() is cleared by the above clear_bit().
79 	 */
80 	if (!netif_xmit_frozen_or_stopped(txq))
81 		set_bit(__QDISC_STATE_MISSED, &q->state);
82 	else
83 		set_bit(__QDISC_STATE_DRAINING, &q->state);
84 }
85 
86 /* Main transmission queue. */
87 
88 /* Modifications to data participating in scheduling must be protected with
89  * qdisc_lock(qdisc) spinlock.
90  *
91  * The idea is the following:
92  * - enqueue, dequeue are serialized via qdisc root lock
93  * - ingress filtering is also serialized via qdisc root lock
94  * - updates to tree and tree walking are only done under the rtnl mutex.
95  */
96 
97 #define SKB_XOFF_MAGIC ((struct sk_buff *)1UL)
98 
99 static inline struct sk_buff *__skb_dequeue_bad_txq(struct Qdisc *q)
100 {
101 	const struct netdev_queue *txq = q->dev_queue;
102 	spinlock_t *lock = NULL;
103 	struct sk_buff *skb;
104 
105 	if (q->flags & TCQ_F_NOLOCK) {
106 		lock = qdisc_lock(q);
107 		spin_lock(lock);
108 	}
109 
110 	skb = skb_peek(&q->skb_bad_txq);
111 	if (skb) {
112 		/* check the reason of requeuing without tx lock first */
113 		txq = skb_get_tx_queue(txq->dev, skb);
114 		if (!netif_xmit_frozen_or_stopped(txq)) {
115 			skb = __skb_dequeue(&q->skb_bad_txq);
116 			if (qdisc_is_percpu_stats(q)) {
117 				qdisc_qstats_cpu_backlog_dec(q, skb);
118 				qdisc_qstats_cpu_qlen_dec(q);
119 			} else {
120 				qdisc_qstats_backlog_dec(q, skb);
121 				qdisc_qlen_dec(q);
122 			}
123 		} else {
124 			skb = SKB_XOFF_MAGIC;
125 			qdisc_maybe_clear_missed(q, txq);
126 		}
127 	}
128 
129 	if (lock)
130 		spin_unlock(lock);
131 
132 	return skb;
133 }
134 
135 static inline struct sk_buff *qdisc_dequeue_skb_bad_txq(struct Qdisc *q)
136 {
137 	struct sk_buff *skb = skb_peek(&q->skb_bad_txq);
138 
139 	if (unlikely(skb))
140 		skb = __skb_dequeue_bad_txq(q);
141 
142 	return skb;
143 }
144 
145 static inline void qdisc_enqueue_skb_bad_txq(struct Qdisc *q,
146 					     struct sk_buff *skb)
147 {
148 	spinlock_t *lock = NULL;
149 
150 	if (q->flags & TCQ_F_NOLOCK) {
151 		lock = qdisc_lock(q);
152 		spin_lock(lock);
153 	}
154 
155 	__skb_queue_tail(&q->skb_bad_txq, skb);
156 
157 	if (qdisc_is_percpu_stats(q)) {
158 		qdisc_qstats_cpu_backlog_inc(q, skb);
159 		qdisc_qstats_cpu_qlen_inc(q);
160 	} else {
161 		qdisc_qstats_backlog_inc(q, skb);
162 		qdisc_qlen_inc(q);
163 	}
164 
165 	if (lock)
166 		spin_unlock(lock);
167 }
168 
169 static inline void dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
170 {
171 	spinlock_t *lock = NULL;
172 
173 	if (q->flags & TCQ_F_NOLOCK) {
174 		lock = qdisc_lock(q);
175 		spin_lock(lock);
176 	}
177 
178 	while (skb) {
179 		struct sk_buff *next = skb->next;
180 
181 		__skb_queue_tail(&q->gso_skb, skb);
182 
183 		/* it's still part of the queue */
184 		if (qdisc_is_percpu_stats(q)) {
185 			qdisc_qstats_cpu_requeues_inc(q);
186 			qdisc_qstats_cpu_backlog_inc(q, skb);
187 			qdisc_qstats_cpu_qlen_inc(q);
188 		} else {
189 			q->qstats.requeues++;
190 			qdisc_qstats_backlog_inc(q, skb);
191 			qdisc_qlen_inc(q);
192 		}
193 
194 		skb = next;
195 	}
196 
197 	if (lock) {
198 		spin_unlock(lock);
199 		set_bit(__QDISC_STATE_MISSED, &q->state);
200 	} else {
201 		__netif_schedule(q);
202 	}
203 }
204 
205 static void try_bulk_dequeue_skb(struct Qdisc *q,
206 				 struct sk_buff *skb,
207 				 const struct netdev_queue *txq,
208 				 int *packets, int budget)
209 {
210 	int bytelimit = qdisc_avail_bulklimit(txq) - skb->len;
211 	int cnt = 0;
212 
213 	while (bytelimit > 0) {
214 		struct sk_buff *nskb = q->dequeue(q);
215 
216 		if (!nskb)
217 			break;
218 
219 		bytelimit -= nskb->len; /* covers GSO len */
220 		skb->next = nskb;
221 		skb = nskb;
222 		if (++cnt >= budget)
223 			break;
224 	}
225 	(*packets) += cnt;
226 	skb_mark_not_on_list(skb);
227 }
228 
229 /* This variant of try_bulk_dequeue_skb() makes sure
230  * all skbs in the chain are for the same txq
231  */
232 static void try_bulk_dequeue_skb_slow(struct Qdisc *q,
233 				      struct sk_buff *skb,
234 				      int *packets)
235 {
236 	int mapping = skb_get_queue_mapping(skb);
237 	struct sk_buff *nskb;
238 	int cnt = 0;
239 
240 	do {
241 		nskb = q->dequeue(q);
242 		if (!nskb)
243 			break;
244 		if (unlikely(skb_get_queue_mapping(nskb) != mapping)) {
245 			qdisc_enqueue_skb_bad_txq(q, nskb);
246 			break;
247 		}
248 		skb->next = nskb;
249 		skb = nskb;
250 	} while (++cnt < 8);
251 	(*packets) += cnt;
252 	skb_mark_not_on_list(skb);
253 }
254 
255 /* Note that dequeue_skb can possibly return a SKB list (via skb->next).
256  * A requeued skb (via q->gso_skb) can also be a SKB list.
257  */
258 static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
259 				   int *packets, int budget)
260 {
261 	const struct netdev_queue *txq = q->dev_queue;
262 	struct sk_buff *skb = NULL;
263 
264 	*packets = 1;
265 	if (unlikely(!skb_queue_empty(&q->gso_skb))) {
266 		spinlock_t *lock = NULL;
267 
268 		if (q->flags & TCQ_F_NOLOCK) {
269 			lock = qdisc_lock(q);
270 			spin_lock(lock);
271 		}
272 
273 		skb = skb_peek(&q->gso_skb);
274 
275 		/* skb may be null if another cpu pulls gso_skb off in between
276 		 * empty check and lock.
277 		 */
278 		if (!skb) {
279 			if (lock)
280 				spin_unlock(lock);
281 			goto validate;
282 		}
283 
284 		/* skb in gso_skb were already validated */
285 		*validate = false;
286 		if (xfrm_offload(skb))
287 			*validate = true;
288 		/* check the reason of requeuing without tx lock first */
289 		txq = skb_get_tx_queue(txq->dev, skb);
290 		if (!netif_xmit_frozen_or_stopped(txq)) {
291 			skb = __skb_dequeue(&q->gso_skb);
292 			if (qdisc_is_percpu_stats(q)) {
293 				qdisc_qstats_cpu_backlog_dec(q, skb);
294 				qdisc_qstats_cpu_qlen_dec(q);
295 			} else {
296 				qdisc_qstats_backlog_dec(q, skb);
297 				qdisc_qlen_dec(q);
298 			}
299 		} else {
300 			skb = NULL;
301 			qdisc_maybe_clear_missed(q, txq);
302 		}
303 		if (lock)
304 			spin_unlock(lock);
305 		goto trace;
306 	}
307 validate:
308 	*validate = true;
309 
310 	if ((q->flags & TCQ_F_ONETXQUEUE) &&
311 	    netif_xmit_frozen_or_stopped(txq)) {
312 		qdisc_maybe_clear_missed(q, txq);
313 		return skb;
314 	}
315 
316 	skb = qdisc_dequeue_skb_bad_txq(q);
317 	if (unlikely(skb)) {
318 		if (skb == SKB_XOFF_MAGIC)
319 			return NULL;
320 		goto bulk;
321 	}
322 	skb = q->dequeue(q);
323 	if (skb) {
324 bulk:
325 		if (qdisc_may_bulk(q))
326 			try_bulk_dequeue_skb(q, skb, txq, packets, budget);
327 		else
328 			try_bulk_dequeue_skb_slow(q, skb, packets);
329 	}
330 trace:
331 	trace_qdisc_dequeue(q, txq, *packets, skb);
332 	return skb;
333 }
334 
335 /*
336  * Transmit possibly several skbs, and handle the return status as
337  * required. Owning qdisc running bit guarantees that only one CPU
338  * can execute this function.
339  *
340  * Returns to the caller:
341  *				false  - hardware queue frozen backoff
342  *				true   - feel free to send more pkts
343  */
344 bool sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
345 		     struct net_device *dev, struct netdev_queue *txq,
346 		     spinlock_t *root_lock, bool validate)
347 {
348 	int ret = NETDEV_TX_BUSY;
349 	bool again = false;
350 
351 	/* And release qdisc */
352 	if (root_lock)
353 		spin_unlock(root_lock);
354 
355 	/* Note that we validate skb (GSO, checksum, ...) outside of locks */
356 	if (validate)
357 		skb = validate_xmit_skb_list(skb, dev, &again);
358 
359 #ifdef CONFIG_XFRM_OFFLOAD
360 	if (unlikely(again)) {
361 		if (root_lock)
362 			spin_lock(root_lock);
363 
364 		dev_requeue_skb(skb, q);
365 		return false;
366 	}
367 #endif
368 
369 	if (likely(skb)) {
370 		HARD_TX_LOCK(dev, txq, smp_processor_id());
371 		if (!netif_xmit_frozen_or_stopped(txq))
372 			skb = dev_hard_start_xmit(skb, dev, txq, &ret);
373 		else
374 			qdisc_maybe_clear_missed(q, txq);
375 
376 		HARD_TX_UNLOCK(dev, txq);
377 	} else {
378 		if (root_lock)
379 			spin_lock(root_lock);
380 		return true;
381 	}
382 
383 	if (root_lock)
384 		spin_lock(root_lock);
385 
386 	if (!dev_xmit_complete(ret)) {
387 		/* Driver returned NETDEV_TX_BUSY - requeue skb */
388 		if (unlikely(ret != NETDEV_TX_BUSY))
389 			net_warn_ratelimited("BUG %s code %d qlen %d\n",
390 					     dev->name, ret, q->q.qlen);
391 
392 		dev_requeue_skb(skb, q);
393 		return false;
394 	}
395 
396 	return true;
397 }
398 
399 /*
400  * NOTE: Called under qdisc_lock(q) with locally disabled BH.
401  *
402  * running seqcount guarantees only one CPU can process
403  * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
404  * this queue.
405  *
406  *  netif_tx_lock serializes accesses to device driver.
407  *
408  *  qdisc_lock(q) and netif_tx_lock are mutually exclusive,
409  *  if one is grabbed, another must be free.
410  *
411  * Note, that this procedure can be called by a watchdog timer
412  *
413  * Returns to the caller:
414  *				0  - queue is empty or throttled.
415  *				>0 - queue is not empty.
416  *
417  */
418 static inline bool qdisc_restart(struct Qdisc *q, int *packets, int budget)
419 {
420 	spinlock_t *root_lock = NULL;
421 	struct netdev_queue *txq;
422 	struct net_device *dev;
423 	struct sk_buff *skb;
424 	bool validate;
425 
426 	/* Dequeue packet */
427 	skb = dequeue_skb(q, &validate, packets, budget);
428 	if (unlikely(!skb))
429 		return false;
430 
431 	if (!(q->flags & TCQ_F_NOLOCK))
432 		root_lock = qdisc_lock(q);
433 
434 	dev = qdisc_dev(q);
435 	txq = skb_get_tx_queue(dev, skb);
436 
437 	return sch_direct_xmit(skb, q, dev, txq, root_lock, validate);
438 }
439 
440 void __qdisc_run(struct Qdisc *q)
441 {
442 	int quota = READ_ONCE(net_hotdata.dev_tx_weight);
443 	int packets;
444 
445 	while (qdisc_restart(q, &packets, quota)) {
446 		quota -= packets;
447 		if (quota <= 0) {
448 			if (q->flags & TCQ_F_NOLOCK)
449 				set_bit(__QDISC_STATE_MISSED, &q->state);
450 			else
451 				__netif_schedule(q);
452 
453 			break;
454 		}
455 	}
456 }
457 
458 unsigned long dev_trans_start(struct net_device *dev)
459 {
460 	unsigned long res = READ_ONCE(netdev_get_tx_queue(dev, 0)->trans_start);
461 	unsigned long val;
462 	unsigned int i;
463 
464 	for (i = 1; i < dev->num_tx_queues; i++) {
465 		val = READ_ONCE(netdev_get_tx_queue(dev, i)->trans_start);
466 		if (val && time_after(val, res))
467 			res = val;
468 	}
469 
470 	return res;
471 }
472 EXPORT_SYMBOL(dev_trans_start);
473 
474 static void netif_freeze_queues(struct net_device *dev)
475 {
476 	unsigned int i;
477 	int cpu;
478 
479 	cpu = smp_processor_id();
480 	for (i = 0; i < dev->num_tx_queues; i++) {
481 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
482 
483 		/* We are the only thread of execution doing a
484 		 * freeze, but we have to grab the _xmit_lock in
485 		 * order to synchronize with threads which are in
486 		 * the ->hard_start_xmit() handler and already
487 		 * checked the frozen bit.
488 		 */
489 		__netif_tx_lock(txq, cpu);
490 		set_bit(__QUEUE_STATE_FROZEN, &txq->state);
491 		__netif_tx_unlock(txq);
492 	}
493 }
494 
495 void netif_tx_lock(struct net_device *dev)
496 {
497 	spin_lock(&dev->tx_global_lock);
498 	netif_freeze_queues(dev);
499 }
500 EXPORT_SYMBOL(netif_tx_lock);
501 
502 static void netif_unfreeze_queues(struct net_device *dev)
503 {
504 	unsigned int i;
505 
506 	for (i = 0; i < dev->num_tx_queues; i++) {
507 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
508 
509 		/* No need to grab the _xmit_lock here.  If the
510 		 * queue is not stopped for another reason, we
511 		 * force a schedule.
512 		 */
513 		clear_bit(__QUEUE_STATE_FROZEN, &txq->state);
514 		netif_schedule_queue(txq);
515 	}
516 }
517 
518 void netif_tx_unlock(struct net_device *dev)
519 {
520 	netif_unfreeze_queues(dev);
521 	spin_unlock(&dev->tx_global_lock);
522 }
523 EXPORT_SYMBOL(netif_tx_unlock);
524 
525 static void dev_watchdog(struct timer_list *t)
526 {
527 	struct net_device *dev = timer_container_of(dev, t, watchdog_timer);
528 	bool release = true;
529 
530 	spin_lock(&dev->tx_global_lock);
531 	if (!qdisc_tx_is_noop(dev)) {
532 		if (netif_device_present(dev) &&
533 		    netif_running(dev) &&
534 		    netif_carrier_ok(dev)) {
535 			unsigned int timedout_ms = 0;
536 			unsigned int i;
537 			unsigned long trans_start;
538 			unsigned long oldest_start = jiffies;
539 
540 			for (i = 0; i < dev->num_tx_queues; i++) {
541 				struct netdev_queue *txq;
542 
543 				txq = netdev_get_tx_queue(dev, i);
544 				if (!netif_xmit_stopped(txq))
545 					continue;
546 
547 				/* Paired with WRITE_ONCE() + smp_mb...() in
548 				 * netdev_tx_sent_queue() and netif_tx_stop_queue().
549 				 */
550 				smp_mb();
551 				trans_start = READ_ONCE(txq->trans_start);
552 
553 				if (time_after(jiffies, trans_start + dev->watchdog_timeo)) {
554 					timedout_ms = jiffies_to_msecs(jiffies - trans_start);
555 					atomic_long_inc(&txq->trans_timeout);
556 					break;
557 				}
558 				if (time_after(oldest_start, trans_start))
559 					oldest_start = trans_start;
560 			}
561 
562 			if (unlikely(timedout_ms)) {
563 				trace_net_dev_xmit_timeout(dev, i);
564 				netdev_crit(dev, "NETDEV WATCHDOG: CPU: %d: transmit queue %u timed out %u ms\n",
565 					    raw_smp_processor_id(),
566 					    i, timedout_ms);
567 				netif_freeze_queues(dev);
568 				dev->netdev_ops->ndo_tx_timeout(dev, i);
569 				netif_unfreeze_queues(dev);
570 			}
571 			if (!mod_timer(&dev->watchdog_timer,
572 				       round_jiffies(oldest_start +
573 						     dev->watchdog_timeo)))
574 				release = false;
575 		}
576 	}
577 	spin_unlock(&dev->tx_global_lock);
578 
579 	if (release)
580 		netdev_put(dev, &dev->watchdog_dev_tracker);
581 }
582 
583 void netdev_watchdog_up(struct net_device *dev)
584 {
585 	if (!dev->netdev_ops->ndo_tx_timeout)
586 		return;
587 	if (dev->watchdog_timeo <= 0)
588 		dev->watchdog_timeo = 5*HZ;
589 	if (!mod_timer(&dev->watchdog_timer,
590 		       round_jiffies(jiffies + dev->watchdog_timeo)))
591 		netdev_hold(dev, &dev->watchdog_dev_tracker,
592 			    GFP_ATOMIC);
593 }
594 EXPORT_SYMBOL_GPL(netdev_watchdog_up);
595 
596 static void netdev_watchdog_down(struct net_device *dev)
597 {
598 	netif_tx_lock_bh(dev);
599 	if (timer_delete(&dev->watchdog_timer))
600 		netdev_put(dev, &dev->watchdog_dev_tracker);
601 	netif_tx_unlock_bh(dev);
602 }
603 
604 /**
605  *	netif_carrier_on - set carrier
606  *	@dev: network device
607  *
608  * Device has detected acquisition of carrier.
609  */
610 void netif_carrier_on(struct net_device *dev)
611 {
612 	if (READ_ONCE(dev->proto_down))
613 		return;
614 
615 	if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
616 		if (dev->reg_state == NETREG_UNINITIALIZED)
617 			return;
618 		atomic_inc(&dev->carrier_up_count);
619 		linkwatch_fire_event(dev);
620 		if (netif_running(dev))
621 			netdev_watchdog_up(dev);
622 	}
623 }
624 EXPORT_SYMBOL(netif_carrier_on);
625 
626 /**
627  *	netif_carrier_off - clear carrier
628  *	@dev: network device
629  *
630  * Device has detected loss of carrier.
631  */
632 void netif_carrier_off(struct net_device *dev)
633 {
634 	if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
635 		if (dev->reg_state == NETREG_UNINITIALIZED)
636 			return;
637 		atomic_inc(&dev->carrier_down_count);
638 		linkwatch_fire_event(dev);
639 	}
640 }
641 EXPORT_SYMBOL(netif_carrier_off);
642 
643 /**
644  *	netif_carrier_event - report carrier state event
645  *	@dev: network device
646  *
647  * Device has detected a carrier event but the carrier state wasn't changed.
648  * Use in drivers when querying carrier state asynchronously, to avoid missing
649  * events (link flaps) if link recovers before it's queried.
650  */
651 void netif_carrier_event(struct net_device *dev)
652 {
653 	if (dev->reg_state == NETREG_UNINITIALIZED)
654 		return;
655 	atomic_inc(&dev->carrier_up_count);
656 	atomic_inc(&dev->carrier_down_count);
657 	linkwatch_fire_event(dev);
658 }
659 EXPORT_SYMBOL_GPL(netif_carrier_event);
660 
661 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
662    under all circumstances. It is difficult to invent anything faster or
663    cheaper.
664  */
665 
666 static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
667 			struct sk_buff **to_free)
668 {
669 	dev_core_stats_tx_dropped_inc(skb->dev);
670 	__qdisc_drop(skb, to_free);
671 	return NET_XMIT_CN;
672 }
673 
674 static struct sk_buff *noop_dequeue(struct Qdisc *qdisc)
675 {
676 	return NULL;
677 }
678 
679 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
680 	.id		=	"noop",
681 	.priv_size	=	0,
682 	.enqueue	=	noop_enqueue,
683 	.dequeue	=	noop_dequeue,
684 	.peek		=	noop_dequeue,
685 	.owner		=	THIS_MODULE,
686 };
687 
688 static struct netdev_queue noop_netdev_queue = {
689 	RCU_POINTER_INITIALIZER(qdisc, &noop_qdisc),
690 	RCU_POINTER_INITIALIZER(qdisc_sleeping, &noop_qdisc),
691 };
692 
693 struct Qdisc noop_qdisc = {
694 	.enqueue	=	noop_enqueue,
695 	.dequeue	=	noop_dequeue,
696 	.flags		=	TCQ_F_BUILTIN,
697 	.ops		=	&noop_qdisc_ops,
698 	.q.lock		=	__SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
699 	.dev_queue	=	&noop_netdev_queue,
700 	.gso_skb = {
701 		.next = (struct sk_buff *)&noop_qdisc.gso_skb,
702 		.prev = (struct sk_buff *)&noop_qdisc.gso_skb,
703 		.qlen = 0,
704 		.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.gso_skb.lock),
705 	},
706 	.skb_bad_txq = {
707 		.next = (struct sk_buff *)&noop_qdisc.skb_bad_txq,
708 		.prev = (struct sk_buff *)&noop_qdisc.skb_bad_txq,
709 		.qlen = 0,
710 		.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.skb_bad_txq.lock),
711 	},
712 };
713 EXPORT_SYMBOL(noop_qdisc);
714 
715 static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt,
716 			struct netlink_ext_ack *extack)
717 {
718 	/* register_qdisc() assigns a default of noop_enqueue if unset,
719 	 * but __dev_queue_xmit() treats noqueue only as such
720 	 * if this is NULL - so clear it here. */
721 	qdisc->enqueue = NULL;
722 	return 0;
723 }
724 
725 struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
726 	.id		=	"noqueue",
727 	.priv_size	=	0,
728 	.init		=	noqueue_init,
729 	.enqueue	=	noop_enqueue,
730 	.dequeue	=	noop_dequeue,
731 	.peek		=	noop_dequeue,
732 	.owner		=	THIS_MODULE,
733 };
734 
735 const u8 sch_default_prio2band[TC_PRIO_MAX + 1] = {
736 	1, 2, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1
737 };
738 EXPORT_SYMBOL(sch_default_prio2band);
739 
740 /* 3-band FIFO queue: old style, but should be a bit faster than
741    generic prio+fifo combination.
742  */
743 
744 #define PFIFO_FAST_BANDS 3
745 
746 /*
747  * Private data for a pfifo_fast scheduler containing:
748  *	- rings for priority bands
749  */
750 struct pfifo_fast_priv {
751 	struct skb_array q[PFIFO_FAST_BANDS];
752 };
753 
754 static inline struct skb_array *band2list(struct pfifo_fast_priv *priv,
755 					  int band)
756 {
757 	return &priv->q[band];
758 }
759 
760 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
761 			      struct sk_buff **to_free)
762 {
763 	int band = sch_default_prio2band[skb->priority & TC_PRIO_MAX];
764 	struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
765 	struct skb_array *q = band2list(priv, band);
766 	unsigned int pkt_len = qdisc_pkt_len(skb);
767 	int err;
768 
769 	err = skb_array_produce(q, skb);
770 
771 	if (unlikely(err)) {
772 		tcf_set_qdisc_drop_reason(skb, QDISC_DROP_OVERLIMIT);
773 
774 		if (qdisc_is_percpu_stats(qdisc))
775 			return qdisc_drop_cpu(skb, qdisc, to_free);
776 		else
777 			return qdisc_drop(skb, qdisc, to_free);
778 	}
779 
780 	qdisc_update_stats_at_enqueue(qdisc, pkt_len);
781 	return NET_XMIT_SUCCESS;
782 }
783 
784 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
785 {
786 	struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
787 	struct sk_buff *skb = NULL;
788 	bool need_retry = true;
789 	int band;
790 
791 retry:
792 	for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
793 		struct skb_array *q = band2list(priv, band);
794 
795 		if (__skb_array_empty(q))
796 			continue;
797 
798 		skb = __skb_array_consume(q);
799 	}
800 	if (likely(skb)) {
801 		qdisc_update_stats_at_dequeue(qdisc, skb);
802 	} else if (need_retry &&
803 		   READ_ONCE(qdisc->state) & QDISC_STATE_NON_EMPTY) {
804 		/* Delay clearing the STATE_MISSED here to reduce
805 		 * the overhead of the second spin_trylock() in
806 		 * qdisc_run_begin() and __netif_schedule() calling
807 		 * in qdisc_run_end().
808 		 */
809 		clear_bit(__QDISC_STATE_MISSED, &qdisc->state);
810 		clear_bit(__QDISC_STATE_DRAINING, &qdisc->state);
811 
812 		/* Make sure dequeuing happens after clearing
813 		 * STATE_MISSED.
814 		 */
815 		smp_mb__after_atomic();
816 
817 		need_retry = false;
818 
819 		goto retry;
820 	}
821 
822 	return skb;
823 }
824 
825 static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
826 {
827 	struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
828 	struct sk_buff *skb = NULL;
829 	int band;
830 
831 	for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
832 		struct skb_array *q = band2list(priv, band);
833 
834 		skb = __skb_array_peek(q);
835 	}
836 
837 	return skb;
838 }
839 
840 static void pfifo_fast_reset(struct Qdisc *qdisc)
841 {
842 	int i, band;
843 	struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
844 
845 	for (band = 0; band < PFIFO_FAST_BANDS; band++) {
846 		struct skb_array *q = band2list(priv, band);
847 		struct sk_buff *skb;
848 
849 		/* NULL ring is possible if destroy path is due to a failed
850 		 * skb_array_init() in pfifo_fast_init() case.
851 		 */
852 		if (!q->ring.queue)
853 			continue;
854 
855 		while ((skb = __skb_array_consume(q)) != NULL)
856 			rtnl_kfree_skbs(skb, skb);
857 	}
858 
859 	if (qdisc_is_percpu_stats(qdisc)) {
860 		for_each_possible_cpu(i) {
861 			struct gnet_stats_queue *q;
862 
863 			q = per_cpu_ptr(qdisc->cpu_qstats, i);
864 			q->backlog = 0;
865 			q->qlen = 0;
866 		}
867 	}
868 }
869 
870 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
871 {
872 	struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
873 
874 	memcpy(&opt.priomap, sch_default_prio2band, TC_PRIO_MAX + 1);
875 	if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
876 		goto nla_put_failure;
877 	return skb->len;
878 
879 nla_put_failure:
880 	return -1;
881 }
882 
883 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt,
884 			   struct netlink_ext_ack *extack)
885 {
886 	unsigned int qlen = qdisc_dev(qdisc)->tx_queue_len;
887 	struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
888 	int prio;
889 
890 	/* guard against zero length rings */
891 	if (!qlen)
892 		return -EINVAL;
893 
894 	for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
895 		struct skb_array *q = band2list(priv, prio);
896 		int err;
897 
898 		err = skb_array_init(q, qlen, GFP_KERNEL);
899 		if (err)
900 			return -ENOMEM;
901 	}
902 
903 	/* Can by-pass the queue discipline */
904 	qdisc->flags |= TCQ_F_CAN_BYPASS;
905 	return 0;
906 }
907 
908 static void pfifo_fast_destroy(struct Qdisc *sch)
909 {
910 	struct pfifo_fast_priv *priv = qdisc_priv(sch);
911 	int prio;
912 
913 	for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
914 		struct skb_array *q = band2list(priv, prio);
915 
916 		/* NULL ring is possible if destroy path is due to a failed
917 		 * skb_array_init() in pfifo_fast_init() case.
918 		 */
919 		if (!q->ring.queue)
920 			continue;
921 		/* Destroy ring but no need to kfree_skb because a call to
922 		 * pfifo_fast_reset() has already done that work.
923 		 */
924 		ptr_ring_cleanup(&q->ring, NULL);
925 	}
926 }
927 
928 static int pfifo_fast_change_tx_queue_len(struct Qdisc *sch,
929 					  unsigned int new_len)
930 {
931 	struct pfifo_fast_priv *priv = qdisc_priv(sch);
932 	struct skb_array *bands[PFIFO_FAST_BANDS];
933 	int prio;
934 
935 	for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
936 		struct skb_array *q = band2list(priv, prio);
937 
938 		bands[prio] = q;
939 	}
940 
941 	return skb_array_resize_multiple_bh(bands, PFIFO_FAST_BANDS, new_len,
942 					    GFP_KERNEL);
943 }
944 
945 struct Qdisc_ops pfifo_fast_ops __read_mostly = {
946 	.id		=	"pfifo_fast",
947 	.priv_size	=	sizeof(struct pfifo_fast_priv),
948 	.enqueue	=	pfifo_fast_enqueue,
949 	.dequeue	=	pfifo_fast_dequeue,
950 	.peek		=	pfifo_fast_peek,
951 	.init		=	pfifo_fast_init,
952 	.destroy	=	pfifo_fast_destroy,
953 	.reset		=	pfifo_fast_reset,
954 	.dump		=	pfifo_fast_dump,
955 	.change_tx_queue_len =  pfifo_fast_change_tx_queue_len,
956 	.owner		=	THIS_MODULE,
957 	.static_flags	=	TCQ_F_NOLOCK | TCQ_F_CPUSTATS,
958 };
959 EXPORT_SYMBOL(pfifo_fast_ops);
960 
961 static struct lock_class_key qdisc_tx_busylock;
962 
963 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
964 			  const struct Qdisc_ops *ops,
965 			  struct netlink_ext_ack *extack)
966 {
967 	struct Qdisc *sch;
968 	unsigned int size = sizeof(*sch) + ops->priv_size;
969 	int err = -ENOBUFS;
970 	struct net_device *dev;
971 
972 	if (!dev_queue) {
973 		NL_SET_ERR_MSG(extack, "No device queue given");
974 		err = -EINVAL;
975 		goto errout;
976 	}
977 
978 	dev = dev_queue->dev;
979 	sch = kzalloc_node(size, GFP_KERNEL, netdev_queue_numa_node_read(dev_queue));
980 
981 	if (!sch)
982 		goto errout;
983 	__skb_queue_head_init(&sch->gso_skb);
984 	__skb_queue_head_init(&sch->skb_bad_txq);
985 	gnet_stats_basic_sync_init(&sch->bstats);
986 	qdisc_lock_init(sch, ops);
987 
988 	if (ops->static_flags & TCQ_F_CPUSTATS) {
989 		sch->cpu_bstats =
990 			netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
991 		if (!sch->cpu_bstats)
992 			goto errout1;
993 
994 		sch->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
995 		if (!sch->cpu_qstats) {
996 			free_percpu(sch->cpu_bstats);
997 			goto errout1;
998 		}
999 	}
1000 
1001 	/* seqlock has the same scope of busylock, for NOLOCK qdisc */
1002 	spin_lock_init(&sch->seqlock);
1003 	lockdep_set_class(&sch->seqlock,
1004 			  dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
1005 
1006 	sch->ops = ops;
1007 	sch->flags = ops->static_flags;
1008 	sch->enqueue = ops->enqueue;
1009 	sch->dequeue = ops->dequeue;
1010 	sch->dev_queue = dev_queue;
1011 	netdev_hold(dev, &sch->dev_tracker, GFP_KERNEL);
1012 	refcount_set(&sch->refcnt, 1);
1013 
1014 	return sch;
1015 errout1:
1016 	qdisc_lock_uninit(sch, ops);
1017 	kfree(sch);
1018 errout:
1019 	return ERR_PTR(err);
1020 }
1021 
1022 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
1023 				const struct Qdisc_ops *ops,
1024 				unsigned int parentid,
1025 				struct netlink_ext_ack *extack)
1026 {
1027 	struct Qdisc *sch;
1028 
1029 	if (!bpf_try_module_get(ops, ops->owner)) {
1030 		NL_SET_ERR_MSG(extack, "Failed to increase module reference counter");
1031 		return NULL;
1032 	}
1033 
1034 	sch = qdisc_alloc(dev_queue, ops, extack);
1035 	if (IS_ERR(sch)) {
1036 		bpf_module_put(ops, ops->owner);
1037 		return NULL;
1038 	}
1039 	sch->parent = parentid;
1040 
1041 	if (!ops->init || ops->init(sch, NULL, extack) == 0) {
1042 		trace_qdisc_create(ops, dev_queue->dev, parentid);
1043 		return sch;
1044 	}
1045 
1046 	qdisc_put(sch);
1047 	return NULL;
1048 }
1049 EXPORT_SYMBOL(qdisc_create_dflt);
1050 
1051 /* Under qdisc_lock(qdisc) and BH! */
1052 
1053 void qdisc_reset(struct Qdisc *qdisc)
1054 {
1055 	const struct Qdisc_ops *ops = qdisc->ops;
1056 
1057 	trace_qdisc_reset(qdisc);
1058 
1059 	if (ops->reset)
1060 		ops->reset(qdisc);
1061 
1062 	__skb_queue_purge(&qdisc->gso_skb);
1063 	__skb_queue_purge(&qdisc->skb_bad_txq);
1064 
1065 	WRITE_ONCE(qdisc->q.qlen, 0);
1066 	WRITE_ONCE(qdisc->qstats.backlog, 0);
1067 }
1068 EXPORT_SYMBOL(qdisc_reset);
1069 
1070 void qdisc_free(struct Qdisc *qdisc)
1071 {
1072 	if (qdisc_is_percpu_stats(qdisc)) {
1073 		free_percpu(qdisc->cpu_bstats);
1074 		free_percpu(qdisc->cpu_qstats);
1075 	}
1076 
1077 	kfree(qdisc);
1078 }
1079 
1080 static void qdisc_free_cb(struct rcu_head *head)
1081 {
1082 	struct Qdisc *q = container_of(head, struct Qdisc, rcu);
1083 
1084 	qdisc_free(q);
1085 }
1086 
1087 static void __qdisc_destroy(struct Qdisc *qdisc)
1088 {
1089 	const struct Qdisc_ops  *ops = qdisc->ops;
1090 	struct net_device *dev = qdisc_dev(qdisc);
1091 
1092 #ifdef CONFIG_NET_SCHED
1093 	qdisc_hash_del(qdisc);
1094 
1095 	qdisc_put_stab(rtnl_dereference(qdisc->stab));
1096 #endif
1097 	gen_kill_estimator(&qdisc->rate_est);
1098 
1099 	qdisc_reset(qdisc);
1100 
1101 
1102 	if (ops->destroy)
1103 		ops->destroy(qdisc);
1104 
1105 	qdisc_lock_uninit(qdisc, ops);
1106 	bpf_module_put(ops, ops->owner);
1107 	netdev_put(dev, &qdisc->dev_tracker);
1108 
1109 	trace_qdisc_destroy(qdisc);
1110 
1111 	call_rcu(&qdisc->rcu, qdisc_free_cb);
1112 }
1113 
1114 void qdisc_destroy(struct Qdisc *qdisc)
1115 {
1116 	if (qdisc->flags & TCQ_F_BUILTIN)
1117 		return;
1118 
1119 	__qdisc_destroy(qdisc);
1120 }
1121 
1122 void qdisc_put(struct Qdisc *qdisc)
1123 {
1124 	if (!qdisc)
1125 		return;
1126 
1127 	if (qdisc->flags & TCQ_F_BUILTIN ||
1128 	    !refcount_dec_and_test(&qdisc->refcnt))
1129 		return;
1130 
1131 	__qdisc_destroy(qdisc);
1132 }
1133 EXPORT_SYMBOL(qdisc_put);
1134 
1135 /* Version of qdisc_put() that is called with rtnl mutex unlocked.
1136  * Intended to be used as optimization, this function only takes rtnl lock if
1137  * qdisc reference counter reached zero.
1138  */
1139 
1140 void qdisc_put_unlocked(struct Qdisc *qdisc)
1141 {
1142 	if (qdisc->flags & TCQ_F_BUILTIN ||
1143 	    !refcount_dec_and_rtnl_lock(&qdisc->refcnt))
1144 		return;
1145 
1146 	__qdisc_destroy(qdisc);
1147 	rtnl_unlock();
1148 }
1149 EXPORT_SYMBOL(qdisc_put_unlocked);
1150 
1151 /* Attach toplevel qdisc to device queue. */
1152 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
1153 			      struct Qdisc *qdisc)
1154 {
1155 	struct Qdisc *oqdisc = rtnl_dereference(dev_queue->qdisc_sleeping);
1156 	spinlock_t *root_lock;
1157 
1158 	root_lock = qdisc_lock(oqdisc);
1159 	spin_lock_bh(root_lock);
1160 
1161 	/* ... and graft new one */
1162 	if (qdisc == NULL)
1163 		qdisc = &noop_qdisc;
1164 	rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc);
1165 	rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
1166 
1167 	spin_unlock_bh(root_lock);
1168 
1169 	return oqdisc;
1170 }
1171 EXPORT_SYMBOL(dev_graft_qdisc);
1172 
1173 static void shutdown_scheduler_queue(struct net_device *dev,
1174 				     struct netdev_queue *dev_queue,
1175 				     void *_qdisc_default)
1176 {
1177 	struct Qdisc *qdisc = rtnl_dereference(dev_queue->qdisc_sleeping);
1178 	struct Qdisc *qdisc_default = _qdisc_default;
1179 
1180 	if (qdisc) {
1181 		rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
1182 		rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc_default);
1183 
1184 		qdisc_put(qdisc);
1185 	}
1186 }
1187 
1188 static void attach_one_default_qdisc(struct net_device *dev,
1189 				     struct netdev_queue *dev_queue,
1190 				     void *_unused)
1191 {
1192 	struct Qdisc *qdisc;
1193 	const struct Qdisc_ops *ops = default_qdisc_ops;
1194 
1195 	if (dev->priv_flags & IFF_NO_QUEUE)
1196 		ops = &noqueue_qdisc_ops;
1197 	else if(dev->type == ARPHRD_CAN)
1198 		ops = &pfifo_fast_ops;
1199 
1200 	qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT, NULL);
1201 	if (!qdisc)
1202 		return;
1203 
1204 	if (!netif_is_multiqueue(dev))
1205 		qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1206 	rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc);
1207 }
1208 
1209 static void attach_default_qdiscs(struct net_device *dev)
1210 {
1211 	struct netdev_queue *txq;
1212 	struct Qdisc *qdisc;
1213 
1214 	txq = netdev_get_tx_queue(dev, 0);
1215 
1216 	if (!netif_is_multiqueue(dev) ||
1217 	    dev->priv_flags & IFF_NO_QUEUE) {
1218 		netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
1219 		qdisc = rtnl_dereference(txq->qdisc_sleeping);
1220 		rcu_assign_pointer(dev->qdisc, qdisc);
1221 		qdisc_refcount_inc(qdisc);
1222 	} else {
1223 		qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT, NULL);
1224 		if (qdisc) {
1225 			rcu_assign_pointer(dev->qdisc, qdisc);
1226 			qdisc->ops->attach(qdisc);
1227 		}
1228 	}
1229 	qdisc = rtnl_dereference(dev->qdisc);
1230 
1231 	/* Detect default qdisc setup/init failed and fallback to "noqueue" */
1232 	if (qdisc == &noop_qdisc) {
1233 		netdev_warn(dev, "default qdisc (%s) fail, fallback to %s\n",
1234 			    default_qdisc_ops->id, noqueue_qdisc_ops.id);
1235 		netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
1236 		dev->priv_flags |= IFF_NO_QUEUE;
1237 		netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
1238 		qdisc = rtnl_dereference(txq->qdisc_sleeping);
1239 		rcu_assign_pointer(dev->qdisc, qdisc);
1240 		qdisc_refcount_inc(qdisc);
1241 		dev->priv_flags ^= IFF_NO_QUEUE;
1242 	}
1243 
1244 #ifdef CONFIG_NET_SCHED
1245 	if (qdisc != &noop_qdisc)
1246 		qdisc_hash_add(qdisc, false);
1247 #endif
1248 }
1249 
1250 static void transition_one_qdisc(struct net_device *dev,
1251 				 struct netdev_queue *dev_queue,
1252 				 void *_need_watchdog)
1253 {
1254 	struct Qdisc *new_qdisc = rtnl_dereference(dev_queue->qdisc_sleeping);
1255 	int *need_watchdog_p = _need_watchdog;
1256 
1257 	if (!(new_qdisc->flags & TCQ_F_BUILTIN))
1258 		clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
1259 
1260 	rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
1261 	if (need_watchdog_p) {
1262 		WRITE_ONCE(dev_queue->trans_start, 0);
1263 		*need_watchdog_p = 1;
1264 	}
1265 }
1266 
1267 void dev_activate(struct net_device *dev)
1268 {
1269 	int need_watchdog;
1270 
1271 	/* No queueing discipline is attached to device;
1272 	 * create default one for devices, which need queueing
1273 	 * and noqueue_qdisc for virtual interfaces
1274 	 */
1275 
1276 	if (rtnl_dereference(dev->qdisc) == &noop_qdisc)
1277 		attach_default_qdiscs(dev);
1278 
1279 	if (!netif_carrier_ok(dev))
1280 		/* Delay activation until next carrier-on event */
1281 		return;
1282 
1283 	need_watchdog = 0;
1284 	netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
1285 	if (dev_ingress_queue(dev))
1286 		transition_one_qdisc(dev, dev_ingress_queue(dev), NULL);
1287 
1288 	if (need_watchdog) {
1289 		netif_trans_update(dev);
1290 		netdev_watchdog_up(dev);
1291 	}
1292 }
1293 EXPORT_SYMBOL(dev_activate);
1294 
1295 static void qdisc_deactivate(struct Qdisc *qdisc)
1296 {
1297 	if (qdisc->flags & TCQ_F_BUILTIN)
1298 		return;
1299 
1300 	set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
1301 }
1302 
1303 static void dev_deactivate_queue(struct net_device *dev,
1304 				 struct netdev_queue *dev_queue,
1305 				 void *_sync_needed)
1306 {
1307 	bool *sync_needed = _sync_needed;
1308 	struct Qdisc *qdisc;
1309 
1310 	qdisc = rtnl_dereference(dev_queue->qdisc);
1311 	if (qdisc) {
1312 		if (qdisc->enqueue)
1313 			*sync_needed = true;
1314 		qdisc_deactivate(qdisc);
1315 		rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
1316 	}
1317 }
1318 
1319 static bool some_qdisc_is_busy(struct net_device *dev)
1320 {
1321 	unsigned int i;
1322 
1323 	for (i = 0; i < dev->num_tx_queues; i++) {
1324 		struct netdev_queue *dev_queue;
1325 		spinlock_t *root_lock;
1326 		struct Qdisc *q;
1327 		int val;
1328 
1329 		dev_queue = netdev_get_tx_queue(dev, i);
1330 		q = rtnl_dereference(dev_queue->qdisc_sleeping);
1331 
1332 		root_lock = qdisc_lock(q);
1333 		spin_lock_bh(root_lock);
1334 
1335 		val = (qdisc_is_running(q) ||
1336 		       test_bit(__QDISC_STATE_SCHED, &q->state));
1337 
1338 		spin_unlock_bh(root_lock);
1339 
1340 		if (val)
1341 			return true;
1342 	}
1343 	return false;
1344 }
1345 
1346 /**
1347  * 	dev_deactivate_many - deactivate transmissions on several devices
1348  * 	@head: list of devices to deactivate
1349  *	@reset_needed: qdisc should be reset if true.
1350  *
1351  *	This function returns only when all outstanding transmissions
1352  *	have completed, unless all devices are in dismantle phase.
1353  */
1354 void dev_deactivate_many(struct list_head *head, bool reset_needed)
1355 {
1356 	bool sync_needed = false;
1357 	struct net_device *dev;
1358 
1359 	list_for_each_entry(dev, head, close_list) {
1360 		netdev_for_each_tx_queue(dev, dev_deactivate_queue,
1361 					 &sync_needed);
1362 		if (dev_ingress_queue(dev))
1363 			dev_deactivate_queue(dev, dev_ingress_queue(dev),
1364 					     &sync_needed);
1365 
1366 		netdev_watchdog_down(dev);
1367 	}
1368 
1369 	/* Wait for outstanding qdisc enqueuing calls. */
1370 	if (sync_needed)
1371 		synchronize_net();
1372 
1373 	if (reset_needed) {
1374 		list_for_each_entry(dev, head, close_list) {
1375 			netdev_for_each_tx_queue(dev, dev_reset_queue, NULL);
1376 
1377 			if (dev_ingress_queue(dev))
1378 				dev_reset_queue(dev, dev_ingress_queue(dev),
1379 						NULL);
1380 		}
1381 	}
1382 
1383 	/* Wait for outstanding qdisc_run calls. */
1384 	list_for_each_entry(dev, head, close_list) {
1385 		while (some_qdisc_is_busy(dev)) {
1386 			/* wait_event() would avoid this sleep-loop but would
1387 			 * require expensive checks in the fast paths of packet
1388 			 * processing which isn't worth it.
1389 			 */
1390 			schedule_timeout_uninterruptible(1);
1391 		}
1392 	}
1393 }
1394 
1395 void dev_deactivate(struct net_device *dev, bool reset_needed)
1396 {
1397 	LIST_HEAD(single);
1398 
1399 	list_add(&dev->close_list, &single);
1400 	dev_deactivate_many(&single, reset_needed);
1401 	list_del(&single);
1402 }
1403 EXPORT_SYMBOL(dev_deactivate);
1404 
1405 static int qdisc_change_tx_queue_len(struct net_device *dev,
1406 				     struct netdev_queue *dev_queue)
1407 {
1408 	struct Qdisc *qdisc = rtnl_dereference(dev_queue->qdisc_sleeping);
1409 	const struct Qdisc_ops *ops = qdisc->ops;
1410 
1411 	if (ops->change_tx_queue_len)
1412 		return ops->change_tx_queue_len(qdisc, dev->tx_queue_len);
1413 	return 0;
1414 }
1415 
1416 void dev_qdisc_change_real_num_tx(struct net_device *dev,
1417 				  unsigned int new_real_tx)
1418 {
1419 	struct Qdisc *qdisc = rtnl_dereference(dev->qdisc);
1420 
1421 	if (qdisc->ops->change_real_num_tx)
1422 		qdisc->ops->change_real_num_tx(qdisc, new_real_tx);
1423 }
1424 
1425 void mq_change_real_num_tx(struct Qdisc *sch, unsigned int new_real_tx)
1426 {
1427 #ifdef CONFIG_NET_SCHED
1428 	struct net_device *dev = qdisc_dev(sch);
1429 	struct Qdisc *qdisc;
1430 	unsigned int i;
1431 
1432 	for (i = new_real_tx; i < dev->real_num_tx_queues; i++) {
1433 		qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc_sleeping);
1434 		/* Only update the default qdiscs we created,
1435 		 * qdiscs with handles are always hashed.
1436 		 */
1437 		if (qdisc != &noop_qdisc && !qdisc->handle)
1438 			qdisc_hash_del(qdisc);
1439 	}
1440 	for (i = dev->real_num_tx_queues; i < new_real_tx; i++) {
1441 		qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc_sleeping);
1442 		if (qdisc != &noop_qdisc && !qdisc->handle)
1443 			qdisc_hash_add(qdisc, false);
1444 	}
1445 #endif
1446 }
1447 EXPORT_SYMBOL(mq_change_real_num_tx);
1448 
1449 int dev_qdisc_change_tx_queue_len(struct net_device *dev)
1450 {
1451 	bool up = dev->flags & IFF_UP;
1452 	unsigned int i;
1453 	int ret = 0;
1454 
1455 	if (up)
1456 		dev_deactivate(dev, false);
1457 
1458 	for (i = 0; i < dev->num_tx_queues; i++) {
1459 		ret = qdisc_change_tx_queue_len(dev, &dev->_tx[i]);
1460 
1461 		/* TODO: revert changes on a partial failure */
1462 		if (ret)
1463 			break;
1464 	}
1465 
1466 	if (up)
1467 		dev_activate(dev);
1468 	return ret;
1469 }
1470 
1471 static void dev_init_scheduler_queue(struct net_device *dev,
1472 				     struct netdev_queue *dev_queue,
1473 				     void *_qdisc)
1474 {
1475 	struct Qdisc *qdisc = _qdisc;
1476 
1477 	rcu_assign_pointer(dev_queue->qdisc, qdisc);
1478 	rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc);
1479 }
1480 
1481 void dev_init_scheduler(struct net_device *dev)
1482 {
1483 	rcu_assign_pointer(dev->qdisc, &noop_qdisc);
1484 	netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
1485 	if (dev_ingress_queue(dev))
1486 		dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
1487 
1488 	timer_setup(&dev->watchdog_timer, dev_watchdog, 0);
1489 }
1490 
1491 void dev_shutdown(struct net_device *dev)
1492 {
1493 	netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
1494 	if (dev_ingress_queue(dev))
1495 		shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
1496 	qdisc_put(rtnl_dereference(dev->qdisc));
1497 	rcu_assign_pointer(dev->qdisc, &noop_qdisc);
1498 
1499 	WARN_ON(timer_pending(&dev->watchdog_timer));
1500 }
1501 
1502 /**
1503  * psched_ratecfg_precompute__() - Pre-compute values for reciprocal division
1504  * @rate:   Rate to compute reciprocal division values of
1505  * @mult:   Multiplier for reciprocal division
1506  * @shift:  Shift for reciprocal division
1507  *
1508  * The multiplier and shift for reciprocal division by rate are stored
1509  * in mult and shift.
1510  *
1511  * The deal here is to replace a divide by a reciprocal one
1512  * in fast path (a reciprocal divide is a multiply and a shift)
1513  *
1514  * Normal formula would be :
1515  *  time_in_ns = (NSEC_PER_SEC * len) / rate_bps
1516  *
1517  * We compute mult/shift to use instead :
1518  *  time_in_ns = (len * mult) >> shift;
1519  *
1520  * We try to get the highest possible mult value for accuracy,
1521  * but have to make sure no overflows will ever happen.
1522  *
1523  * reciprocal_value() is not used here it doesn't handle 64-bit values.
1524  */
1525 static void psched_ratecfg_precompute__(u64 rate, u32 *mult, u8 *shift)
1526 {
1527 	u64 factor = NSEC_PER_SEC;
1528 
1529 	*mult = 1;
1530 	*shift = 0;
1531 
1532 	if (rate <= 0)
1533 		return;
1534 
1535 	for (;;) {
1536 		*mult = div64_u64(factor, rate);
1537 		if (*mult & (1U << 31) || factor & (1ULL << 63))
1538 			break;
1539 		factor <<= 1;
1540 		(*shift)++;
1541 	}
1542 }
1543 
1544 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1545 			       const struct tc_ratespec *conf,
1546 			       u64 rate64)
1547 {
1548 	memset(r, 0, sizeof(*r));
1549 	r->overhead = conf->overhead;
1550 	r->mpu = conf->mpu;
1551 	r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
1552 	r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
1553 	psched_ratecfg_precompute__(r->rate_bytes_ps, &r->mult, &r->shift);
1554 }
1555 EXPORT_SYMBOL(psched_ratecfg_precompute);
1556 
1557 void psched_ppscfg_precompute(struct psched_pktrate *r, u64 pktrate64)
1558 {
1559 	r->rate_pkts_ps = pktrate64;
1560 	psched_ratecfg_precompute__(r->rate_pkts_ps, &r->mult, &r->shift);
1561 }
1562 EXPORT_SYMBOL(psched_ppscfg_precompute);
1563 
1564 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1565 			  struct tcf_proto *tp_head)
1566 {
1567 	/* Protected with chain0->filter_chain_lock.
1568 	 * Can't access chain directly because tp_head can be NULL.
1569 	 */
1570 	struct mini_Qdisc *miniq_old =
1571 		rcu_dereference_protected(*miniqp->p_miniq, 1);
1572 	struct mini_Qdisc *miniq;
1573 
1574 	if (!tp_head) {
1575 		RCU_INIT_POINTER(*miniqp->p_miniq, NULL);
1576 	} else {
1577 		miniq = miniq_old != &miniqp->miniq1 ?
1578 			&miniqp->miniq1 : &miniqp->miniq2;
1579 
1580 		/* We need to make sure that readers won't see the miniq
1581 		 * we are about to modify. So ensure that at least one RCU
1582 		 * grace period has elapsed since the miniq was made
1583 		 * inactive.
1584 		 */
1585 		if (IS_ENABLED(CONFIG_PREEMPT_RT))
1586 			cond_synchronize_rcu(miniq->rcu_state);
1587 		else if (!poll_state_synchronize_rcu(miniq->rcu_state))
1588 			synchronize_rcu_expedited();
1589 
1590 		miniq->filter_list = tp_head;
1591 		rcu_assign_pointer(*miniqp->p_miniq, miniq);
1592 	}
1593 
1594 	if (miniq_old)
1595 		/* This is counterpart of the rcu sync above. We need to
1596 		 * block potential new user of miniq_old until all readers
1597 		 * are not seeing it.
1598 		 */
1599 		miniq_old->rcu_state = start_poll_synchronize_rcu();
1600 }
1601 EXPORT_SYMBOL(mini_qdisc_pair_swap);
1602 
1603 void mini_qdisc_pair_block_init(struct mini_Qdisc_pair *miniqp,
1604 				struct tcf_block *block)
1605 {
1606 	miniqp->miniq1.block = block;
1607 	miniqp->miniq2.block = block;
1608 }
1609 EXPORT_SYMBOL(mini_qdisc_pair_block_init);
1610 
1611 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1612 			  struct mini_Qdisc __rcu **p_miniq)
1613 {
1614 	miniqp->miniq1.cpu_bstats = qdisc->cpu_bstats;
1615 	miniqp->miniq1.cpu_qstats = qdisc->cpu_qstats;
1616 	miniqp->miniq2.cpu_bstats = qdisc->cpu_bstats;
1617 	miniqp->miniq2.cpu_qstats = qdisc->cpu_qstats;
1618 	miniqp->miniq1.rcu_state = get_state_synchronize_rcu();
1619 	miniqp->miniq2.rcu_state = miniqp->miniq1.rcu_state;
1620 	miniqp->p_miniq = p_miniq;
1621 }
1622 EXPORT_SYMBOL(mini_qdisc_pair_init);
1623