xref: /openbmc/linux/net/sched/sch_generic.c (revision 7587eb18)
1 /*
2  * net/sched/sch_generic.c	Generic packet scheduler routines.
3  *
4  *		This program is free software; you can redistribute it and/or
5  *		modify it under the terms of the GNU General Public License
6  *		as published by the Free Software Foundation; either version
7  *		2 of the License, or (at your option) any later version.
8  *
9  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *              Jamal Hadi Salim, <hadi@cyberus.ca> 990601
11  *              - Ingress support
12  */
13 
14 #include <linux/bitops.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/init.h>
25 #include <linux/rcupdate.h>
26 #include <linux/list.h>
27 #include <linux/slab.h>
28 #include <linux/if_vlan.h>
29 #include <net/sch_generic.h>
30 #include <net/pkt_sched.h>
31 #include <net/dst.h>
32 
33 /* Qdisc to use by default */
34 const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops;
35 EXPORT_SYMBOL(default_qdisc_ops);
36 
37 /* Main transmission queue. */
38 
39 /* Modifications to data participating in scheduling must be protected with
40  * qdisc_lock(qdisc) spinlock.
41  *
42  * The idea is the following:
43  * - enqueue, dequeue are serialized via qdisc root lock
44  * - ingress filtering is also serialized via qdisc root lock
45  * - updates to tree and tree walking are only done under the rtnl mutex.
46  */
47 
48 static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
49 {
50 	q->gso_skb = skb;
51 	q->qstats.requeues++;
52 	qdisc_qstats_backlog_inc(q, skb);
53 	q->q.qlen++;	/* it's still part of the queue */
54 	__netif_schedule(q);
55 
56 	return 0;
57 }
58 
59 static void try_bulk_dequeue_skb(struct Qdisc *q,
60 				 struct sk_buff *skb,
61 				 const struct netdev_queue *txq,
62 				 int *packets)
63 {
64 	int bytelimit = qdisc_avail_bulklimit(txq) - skb->len;
65 
66 	while (bytelimit > 0) {
67 		struct sk_buff *nskb = q->dequeue(q);
68 
69 		if (!nskb)
70 			break;
71 
72 		bytelimit -= nskb->len; /* covers GSO len */
73 		skb->next = nskb;
74 		skb = nskb;
75 		(*packets)++; /* GSO counts as one pkt */
76 	}
77 	skb->next = NULL;
78 }
79 
80 /* Note that dequeue_skb can possibly return a SKB list (via skb->next).
81  * A requeued skb (via q->gso_skb) can also be a SKB list.
82  */
83 static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
84 				   int *packets)
85 {
86 	struct sk_buff *skb = q->gso_skb;
87 	const struct netdev_queue *txq = q->dev_queue;
88 
89 	*packets = 1;
90 	*validate = true;
91 	if (unlikely(skb)) {
92 		/* check the reason of requeuing without tx lock first */
93 		txq = skb_get_tx_queue(txq->dev, skb);
94 		if (!netif_xmit_frozen_or_stopped(txq)) {
95 			q->gso_skb = NULL;
96 			qdisc_qstats_backlog_dec(q, skb);
97 			q->q.qlen--;
98 		} else
99 			skb = NULL;
100 		/* skb in gso_skb were already validated */
101 		*validate = false;
102 	} else {
103 		if (!(q->flags & TCQ_F_ONETXQUEUE) ||
104 		    !netif_xmit_frozen_or_stopped(txq)) {
105 			skb = q->dequeue(q);
106 			if (skb && qdisc_may_bulk(q))
107 				try_bulk_dequeue_skb(q, skb, txq, packets);
108 		}
109 	}
110 	return skb;
111 }
112 
113 /*
114  * Transmit possibly several skbs, and handle the return status as
115  * required. Holding the __QDISC___STATE_RUNNING bit guarantees that
116  * only one CPU can execute this function.
117  *
118  * Returns to the caller:
119  *				0  - queue is empty or throttled.
120  *				>0 - queue is not empty.
121  */
122 int sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
123 		    struct net_device *dev, struct netdev_queue *txq,
124 		    spinlock_t *root_lock, bool validate)
125 {
126 	int ret = NETDEV_TX_BUSY;
127 
128 	/* And release qdisc */
129 	spin_unlock(root_lock);
130 
131 	/* Note that we validate skb (GSO, checksum, ...) outside of locks */
132 	if (validate)
133 		skb = validate_xmit_skb_list(skb, dev);
134 
135 	if (likely(skb)) {
136 		HARD_TX_LOCK(dev, txq, smp_processor_id());
137 		if (!netif_xmit_frozen_or_stopped(txq))
138 			skb = dev_hard_start_xmit(skb, dev, txq, &ret);
139 
140 		HARD_TX_UNLOCK(dev, txq);
141 	} else {
142 		spin_lock(root_lock);
143 		return qdisc_qlen(q);
144 	}
145 	spin_lock(root_lock);
146 
147 	if (dev_xmit_complete(ret)) {
148 		/* Driver sent out skb successfully or skb was consumed */
149 		ret = qdisc_qlen(q);
150 	} else {
151 		/* Driver returned NETDEV_TX_BUSY - requeue skb */
152 		if (unlikely(ret != NETDEV_TX_BUSY))
153 			net_warn_ratelimited("BUG %s code %d qlen %d\n",
154 					     dev->name, ret, q->q.qlen);
155 
156 		ret = dev_requeue_skb(skb, q);
157 	}
158 
159 	if (ret && netif_xmit_frozen_or_stopped(txq))
160 		ret = 0;
161 
162 	return ret;
163 }
164 
165 /*
166  * NOTE: Called under qdisc_lock(q) with locally disabled BH.
167  *
168  * __QDISC___STATE_RUNNING guarantees only one CPU can process
169  * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
170  * this queue.
171  *
172  *  netif_tx_lock serializes accesses to device driver.
173  *
174  *  qdisc_lock(q) and netif_tx_lock are mutually exclusive,
175  *  if one is grabbed, another must be free.
176  *
177  * Note, that this procedure can be called by a watchdog timer
178  *
179  * Returns to the caller:
180  *				0  - queue is empty or throttled.
181  *				>0 - queue is not empty.
182  *
183  */
184 static inline int qdisc_restart(struct Qdisc *q, int *packets)
185 {
186 	struct netdev_queue *txq;
187 	struct net_device *dev;
188 	spinlock_t *root_lock;
189 	struct sk_buff *skb;
190 	bool validate;
191 
192 	/* Dequeue packet */
193 	skb = dequeue_skb(q, &validate, packets);
194 	if (unlikely(!skb))
195 		return 0;
196 
197 	root_lock = qdisc_lock(q);
198 	dev = qdisc_dev(q);
199 	txq = skb_get_tx_queue(dev, skb);
200 
201 	return sch_direct_xmit(skb, q, dev, txq, root_lock, validate);
202 }
203 
204 void __qdisc_run(struct Qdisc *q)
205 {
206 	int quota = weight_p;
207 	int packets;
208 
209 	while (qdisc_restart(q, &packets)) {
210 		/*
211 		 * Ordered by possible occurrence: Postpone processing if
212 		 * 1. we've exceeded packet quota
213 		 * 2. another process needs the CPU;
214 		 */
215 		quota -= packets;
216 		if (quota <= 0 || need_resched()) {
217 			__netif_schedule(q);
218 			break;
219 		}
220 	}
221 
222 	qdisc_run_end(q);
223 }
224 
225 unsigned long dev_trans_start(struct net_device *dev)
226 {
227 	unsigned long val, res;
228 	unsigned int i;
229 
230 	if (is_vlan_dev(dev))
231 		dev = vlan_dev_real_dev(dev);
232 	res = netdev_get_tx_queue(dev, 0)->trans_start;
233 	for (i = 1; i < dev->num_tx_queues; i++) {
234 		val = netdev_get_tx_queue(dev, i)->trans_start;
235 		if (val && time_after(val, res))
236 			res = val;
237 	}
238 
239 	return res;
240 }
241 EXPORT_SYMBOL(dev_trans_start);
242 
243 static void dev_watchdog(unsigned long arg)
244 {
245 	struct net_device *dev = (struct net_device *)arg;
246 
247 	netif_tx_lock(dev);
248 	if (!qdisc_tx_is_noop(dev)) {
249 		if (netif_device_present(dev) &&
250 		    netif_running(dev) &&
251 		    netif_carrier_ok(dev)) {
252 			int some_queue_timedout = 0;
253 			unsigned int i;
254 			unsigned long trans_start;
255 
256 			for (i = 0; i < dev->num_tx_queues; i++) {
257 				struct netdev_queue *txq;
258 
259 				txq = netdev_get_tx_queue(dev, i);
260 				trans_start = txq->trans_start;
261 				if (netif_xmit_stopped(txq) &&
262 				    time_after(jiffies, (trans_start +
263 							 dev->watchdog_timeo))) {
264 					some_queue_timedout = 1;
265 					txq->trans_timeout++;
266 					break;
267 				}
268 			}
269 
270 			if (some_queue_timedout) {
271 				WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
272 				       dev->name, netdev_drivername(dev), i);
273 				dev->netdev_ops->ndo_tx_timeout(dev);
274 			}
275 			if (!mod_timer(&dev->watchdog_timer,
276 				       round_jiffies(jiffies +
277 						     dev->watchdog_timeo)))
278 				dev_hold(dev);
279 		}
280 	}
281 	netif_tx_unlock(dev);
282 
283 	dev_put(dev);
284 }
285 
286 void __netdev_watchdog_up(struct net_device *dev)
287 {
288 	if (dev->netdev_ops->ndo_tx_timeout) {
289 		if (dev->watchdog_timeo <= 0)
290 			dev->watchdog_timeo = 5*HZ;
291 		if (!mod_timer(&dev->watchdog_timer,
292 			       round_jiffies(jiffies + dev->watchdog_timeo)))
293 			dev_hold(dev);
294 	}
295 }
296 
297 static void dev_watchdog_up(struct net_device *dev)
298 {
299 	__netdev_watchdog_up(dev);
300 }
301 
302 static void dev_watchdog_down(struct net_device *dev)
303 {
304 	netif_tx_lock_bh(dev);
305 	if (del_timer(&dev->watchdog_timer))
306 		dev_put(dev);
307 	netif_tx_unlock_bh(dev);
308 }
309 
310 /**
311  *	netif_carrier_on - set carrier
312  *	@dev: network device
313  *
314  * Device has detected that carrier.
315  */
316 void netif_carrier_on(struct net_device *dev)
317 {
318 	if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
319 		if (dev->reg_state == NETREG_UNINITIALIZED)
320 			return;
321 		atomic_inc(&dev->carrier_changes);
322 		linkwatch_fire_event(dev);
323 		if (netif_running(dev))
324 			__netdev_watchdog_up(dev);
325 	}
326 }
327 EXPORT_SYMBOL(netif_carrier_on);
328 
329 /**
330  *	netif_carrier_off - clear carrier
331  *	@dev: network device
332  *
333  * Device has detected loss of carrier.
334  */
335 void netif_carrier_off(struct net_device *dev)
336 {
337 	if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
338 		if (dev->reg_state == NETREG_UNINITIALIZED)
339 			return;
340 		atomic_inc(&dev->carrier_changes);
341 		linkwatch_fire_event(dev);
342 	}
343 }
344 EXPORT_SYMBOL(netif_carrier_off);
345 
346 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
347    under all circumstances. It is difficult to invent anything faster or
348    cheaper.
349  */
350 
351 static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc)
352 {
353 	kfree_skb(skb);
354 	return NET_XMIT_CN;
355 }
356 
357 static struct sk_buff *noop_dequeue(struct Qdisc *qdisc)
358 {
359 	return NULL;
360 }
361 
362 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
363 	.id		=	"noop",
364 	.priv_size	=	0,
365 	.enqueue	=	noop_enqueue,
366 	.dequeue	=	noop_dequeue,
367 	.peek		=	noop_dequeue,
368 	.owner		=	THIS_MODULE,
369 };
370 
371 static struct netdev_queue noop_netdev_queue = {
372 	.qdisc		=	&noop_qdisc,
373 	.qdisc_sleeping	=	&noop_qdisc,
374 };
375 
376 struct Qdisc noop_qdisc = {
377 	.enqueue	=	noop_enqueue,
378 	.dequeue	=	noop_dequeue,
379 	.flags		=	TCQ_F_BUILTIN,
380 	.ops		=	&noop_qdisc_ops,
381 	.list		=	LIST_HEAD_INIT(noop_qdisc.list),
382 	.q.lock		=	__SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
383 	.dev_queue	=	&noop_netdev_queue,
384 	.busylock	=	__SPIN_LOCK_UNLOCKED(noop_qdisc.busylock),
385 };
386 EXPORT_SYMBOL(noop_qdisc);
387 
388 static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt)
389 {
390 	/* register_qdisc() assigns a default of noop_enqueue if unset,
391 	 * but __dev_queue_xmit() treats noqueue only as such
392 	 * if this is NULL - so clear it here. */
393 	qdisc->enqueue = NULL;
394 	return 0;
395 }
396 
397 struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
398 	.id		=	"noqueue",
399 	.priv_size	=	0,
400 	.init		=	noqueue_init,
401 	.enqueue	=	noop_enqueue,
402 	.dequeue	=	noop_dequeue,
403 	.peek		=	noop_dequeue,
404 	.owner		=	THIS_MODULE,
405 };
406 
407 static const u8 prio2band[TC_PRIO_MAX + 1] = {
408 	1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
409 };
410 
411 /* 3-band FIFO queue: old style, but should be a bit faster than
412    generic prio+fifo combination.
413  */
414 
415 #define PFIFO_FAST_BANDS 3
416 
417 /*
418  * Private data for a pfifo_fast scheduler containing:
419  * 	- queues for the three band
420  * 	- bitmap indicating which of the bands contain skbs
421  */
422 struct pfifo_fast_priv {
423 	u32 bitmap;
424 	struct sk_buff_head q[PFIFO_FAST_BANDS];
425 };
426 
427 /*
428  * Convert a bitmap to the first band number where an skb is queued, where:
429  * 	bitmap=0 means there are no skbs on any band.
430  * 	bitmap=1 means there is an skb on band 0.
431  *	bitmap=7 means there are skbs on all 3 bands, etc.
432  */
433 static const int bitmap2band[] = {-1, 0, 1, 0, 2, 0, 1, 0};
434 
435 static inline struct sk_buff_head *band2list(struct pfifo_fast_priv *priv,
436 					     int band)
437 {
438 	return priv->q + band;
439 }
440 
441 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc)
442 {
443 	if (skb_queue_len(&qdisc->q) < qdisc_dev(qdisc)->tx_queue_len) {
444 		int band = prio2band[skb->priority & TC_PRIO_MAX];
445 		struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
446 		struct sk_buff_head *list = band2list(priv, band);
447 
448 		priv->bitmap |= (1 << band);
449 		qdisc->q.qlen++;
450 		return __qdisc_enqueue_tail(skb, qdisc, list);
451 	}
452 
453 	return qdisc_drop(skb, qdisc);
454 }
455 
456 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
457 {
458 	struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
459 	int band = bitmap2band[priv->bitmap];
460 
461 	if (likely(band >= 0)) {
462 		struct sk_buff_head *list = band2list(priv, band);
463 		struct sk_buff *skb = __qdisc_dequeue_head(qdisc, list);
464 
465 		qdisc->q.qlen--;
466 		if (skb_queue_empty(list))
467 			priv->bitmap &= ~(1 << band);
468 
469 		return skb;
470 	}
471 
472 	return NULL;
473 }
474 
475 static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
476 {
477 	struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
478 	int band = bitmap2band[priv->bitmap];
479 
480 	if (band >= 0) {
481 		struct sk_buff_head *list = band2list(priv, band);
482 
483 		return skb_peek(list);
484 	}
485 
486 	return NULL;
487 }
488 
489 static void pfifo_fast_reset(struct Qdisc *qdisc)
490 {
491 	int prio;
492 	struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
493 
494 	for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
495 		__qdisc_reset_queue(qdisc, band2list(priv, prio));
496 
497 	priv->bitmap = 0;
498 	qdisc->qstats.backlog = 0;
499 	qdisc->q.qlen = 0;
500 }
501 
502 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
503 {
504 	struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
505 
506 	memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1);
507 	if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
508 		goto nla_put_failure;
509 	return skb->len;
510 
511 nla_put_failure:
512 	return -1;
513 }
514 
515 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
516 {
517 	int prio;
518 	struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
519 
520 	for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
521 		__skb_queue_head_init(band2list(priv, prio));
522 
523 	/* Can by-pass the queue discipline */
524 	qdisc->flags |= TCQ_F_CAN_BYPASS;
525 	return 0;
526 }
527 
528 struct Qdisc_ops pfifo_fast_ops __read_mostly = {
529 	.id		=	"pfifo_fast",
530 	.priv_size	=	sizeof(struct pfifo_fast_priv),
531 	.enqueue	=	pfifo_fast_enqueue,
532 	.dequeue	=	pfifo_fast_dequeue,
533 	.peek		=	pfifo_fast_peek,
534 	.init		=	pfifo_fast_init,
535 	.reset		=	pfifo_fast_reset,
536 	.dump		=	pfifo_fast_dump,
537 	.owner		=	THIS_MODULE,
538 };
539 EXPORT_SYMBOL(pfifo_fast_ops);
540 
541 static struct lock_class_key qdisc_tx_busylock;
542 
543 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
544 			  const struct Qdisc_ops *ops)
545 {
546 	void *p;
547 	struct Qdisc *sch;
548 	unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size;
549 	int err = -ENOBUFS;
550 	struct net_device *dev = dev_queue->dev;
551 
552 	p = kzalloc_node(size, GFP_KERNEL,
553 			 netdev_queue_numa_node_read(dev_queue));
554 
555 	if (!p)
556 		goto errout;
557 	sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
558 	/* if we got non aligned memory, ask more and do alignment ourself */
559 	if (sch != p) {
560 		kfree(p);
561 		p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL,
562 				 netdev_queue_numa_node_read(dev_queue));
563 		if (!p)
564 			goto errout;
565 		sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
566 		sch->padded = (char *) sch - (char *) p;
567 	}
568 	INIT_LIST_HEAD(&sch->list);
569 	skb_queue_head_init(&sch->q);
570 
571 	spin_lock_init(&sch->busylock);
572 	lockdep_set_class(&sch->busylock,
573 			  dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
574 
575 	sch->ops = ops;
576 	sch->enqueue = ops->enqueue;
577 	sch->dequeue = ops->dequeue;
578 	sch->dev_queue = dev_queue;
579 	dev_hold(dev);
580 	atomic_set(&sch->refcnt, 1);
581 
582 	return sch;
583 errout:
584 	return ERR_PTR(err);
585 }
586 
587 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
588 				const struct Qdisc_ops *ops,
589 				unsigned int parentid)
590 {
591 	struct Qdisc *sch;
592 
593 	if (!try_module_get(ops->owner))
594 		goto errout;
595 
596 	sch = qdisc_alloc(dev_queue, ops);
597 	if (IS_ERR(sch))
598 		goto errout;
599 	sch->parent = parentid;
600 
601 	if (!ops->init || ops->init(sch, NULL) == 0)
602 		return sch;
603 
604 	qdisc_destroy(sch);
605 errout:
606 	return NULL;
607 }
608 EXPORT_SYMBOL(qdisc_create_dflt);
609 
610 /* Under qdisc_lock(qdisc) and BH! */
611 
612 void qdisc_reset(struct Qdisc *qdisc)
613 {
614 	const struct Qdisc_ops *ops = qdisc->ops;
615 
616 	if (ops->reset)
617 		ops->reset(qdisc);
618 
619 	if (qdisc->gso_skb) {
620 		kfree_skb_list(qdisc->gso_skb);
621 		qdisc->gso_skb = NULL;
622 		qdisc->q.qlen = 0;
623 	}
624 }
625 EXPORT_SYMBOL(qdisc_reset);
626 
627 static void qdisc_rcu_free(struct rcu_head *head)
628 {
629 	struct Qdisc *qdisc = container_of(head, struct Qdisc, rcu_head);
630 
631 	if (qdisc_is_percpu_stats(qdisc)) {
632 		free_percpu(qdisc->cpu_bstats);
633 		free_percpu(qdisc->cpu_qstats);
634 	}
635 
636 	kfree((char *) qdisc - qdisc->padded);
637 }
638 
639 void qdisc_destroy(struct Qdisc *qdisc)
640 {
641 	const struct Qdisc_ops  *ops = qdisc->ops;
642 
643 	if (qdisc->flags & TCQ_F_BUILTIN ||
644 	    !atomic_dec_and_test(&qdisc->refcnt))
645 		return;
646 
647 #ifdef CONFIG_NET_SCHED
648 	qdisc_list_del(qdisc);
649 
650 	qdisc_put_stab(rtnl_dereference(qdisc->stab));
651 #endif
652 	gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
653 	if (ops->reset)
654 		ops->reset(qdisc);
655 	if (ops->destroy)
656 		ops->destroy(qdisc);
657 
658 	module_put(ops->owner);
659 	dev_put(qdisc_dev(qdisc));
660 
661 	kfree_skb_list(qdisc->gso_skb);
662 	/*
663 	 * gen_estimator est_timer() might access qdisc->q.lock,
664 	 * wait a RCU grace period before freeing qdisc.
665 	 */
666 	call_rcu(&qdisc->rcu_head, qdisc_rcu_free);
667 }
668 EXPORT_SYMBOL(qdisc_destroy);
669 
670 /* Attach toplevel qdisc to device queue. */
671 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
672 			      struct Qdisc *qdisc)
673 {
674 	struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
675 	spinlock_t *root_lock;
676 
677 	root_lock = qdisc_lock(oqdisc);
678 	spin_lock_bh(root_lock);
679 
680 	/* Prune old scheduler */
681 	if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
682 		qdisc_reset(oqdisc);
683 
684 	/* ... and graft new one */
685 	if (qdisc == NULL)
686 		qdisc = &noop_qdisc;
687 	dev_queue->qdisc_sleeping = qdisc;
688 	rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
689 
690 	spin_unlock_bh(root_lock);
691 
692 	return oqdisc;
693 }
694 EXPORT_SYMBOL(dev_graft_qdisc);
695 
696 static void attach_one_default_qdisc(struct net_device *dev,
697 				     struct netdev_queue *dev_queue,
698 				     void *_unused)
699 {
700 	struct Qdisc *qdisc;
701 	const struct Qdisc_ops *ops = default_qdisc_ops;
702 
703 	if (dev->priv_flags & IFF_NO_QUEUE)
704 		ops = &noqueue_qdisc_ops;
705 
706 	qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT);
707 	if (!qdisc) {
708 		netdev_info(dev, "activation failed\n");
709 		return;
710 	}
711 	if (!netif_is_multiqueue(dev))
712 		qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
713 	dev_queue->qdisc_sleeping = qdisc;
714 }
715 
716 static void attach_default_qdiscs(struct net_device *dev)
717 {
718 	struct netdev_queue *txq;
719 	struct Qdisc *qdisc;
720 
721 	txq = netdev_get_tx_queue(dev, 0);
722 
723 	if (!netif_is_multiqueue(dev) ||
724 	    dev->priv_flags & IFF_NO_QUEUE) {
725 		netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
726 		dev->qdisc = txq->qdisc_sleeping;
727 		atomic_inc(&dev->qdisc->refcnt);
728 	} else {
729 		qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT);
730 		if (qdisc) {
731 			dev->qdisc = qdisc;
732 			qdisc->ops->attach(qdisc);
733 		}
734 	}
735 }
736 
737 static void transition_one_qdisc(struct net_device *dev,
738 				 struct netdev_queue *dev_queue,
739 				 void *_need_watchdog)
740 {
741 	struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
742 	int *need_watchdog_p = _need_watchdog;
743 
744 	if (!(new_qdisc->flags & TCQ_F_BUILTIN))
745 		clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
746 
747 	rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
748 	if (need_watchdog_p) {
749 		dev_queue->trans_start = 0;
750 		*need_watchdog_p = 1;
751 	}
752 }
753 
754 void dev_activate(struct net_device *dev)
755 {
756 	int need_watchdog;
757 
758 	/* No queueing discipline is attached to device;
759 	 * create default one for devices, which need queueing
760 	 * and noqueue_qdisc for virtual interfaces
761 	 */
762 
763 	if (dev->qdisc == &noop_qdisc)
764 		attach_default_qdiscs(dev);
765 
766 	if (!netif_carrier_ok(dev))
767 		/* Delay activation until next carrier-on event */
768 		return;
769 
770 	need_watchdog = 0;
771 	netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
772 	if (dev_ingress_queue(dev))
773 		transition_one_qdisc(dev, dev_ingress_queue(dev), NULL);
774 
775 	if (need_watchdog) {
776 		netif_trans_update(dev);
777 		dev_watchdog_up(dev);
778 	}
779 }
780 EXPORT_SYMBOL(dev_activate);
781 
782 static void dev_deactivate_queue(struct net_device *dev,
783 				 struct netdev_queue *dev_queue,
784 				 void *_qdisc_default)
785 {
786 	struct Qdisc *qdisc_default = _qdisc_default;
787 	struct Qdisc *qdisc;
788 
789 	qdisc = rtnl_dereference(dev_queue->qdisc);
790 	if (qdisc) {
791 		spin_lock_bh(qdisc_lock(qdisc));
792 
793 		if (!(qdisc->flags & TCQ_F_BUILTIN))
794 			set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
795 
796 		rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
797 		qdisc_reset(qdisc);
798 
799 		spin_unlock_bh(qdisc_lock(qdisc));
800 	}
801 }
802 
803 static bool some_qdisc_is_busy(struct net_device *dev)
804 {
805 	unsigned int i;
806 
807 	for (i = 0; i < dev->num_tx_queues; i++) {
808 		struct netdev_queue *dev_queue;
809 		spinlock_t *root_lock;
810 		struct Qdisc *q;
811 		int val;
812 
813 		dev_queue = netdev_get_tx_queue(dev, i);
814 		q = dev_queue->qdisc_sleeping;
815 		root_lock = qdisc_lock(q);
816 
817 		spin_lock_bh(root_lock);
818 
819 		val = (qdisc_is_running(q) ||
820 		       test_bit(__QDISC_STATE_SCHED, &q->state));
821 
822 		spin_unlock_bh(root_lock);
823 
824 		if (val)
825 			return true;
826 	}
827 	return false;
828 }
829 
830 /**
831  * 	dev_deactivate_many - deactivate transmissions on several devices
832  * 	@head: list of devices to deactivate
833  *
834  *	This function returns only when all outstanding transmissions
835  *	have completed, unless all devices are in dismantle phase.
836  */
837 void dev_deactivate_many(struct list_head *head)
838 {
839 	struct net_device *dev;
840 	bool sync_needed = false;
841 
842 	list_for_each_entry(dev, head, close_list) {
843 		netdev_for_each_tx_queue(dev, dev_deactivate_queue,
844 					 &noop_qdisc);
845 		if (dev_ingress_queue(dev))
846 			dev_deactivate_queue(dev, dev_ingress_queue(dev),
847 					     &noop_qdisc);
848 
849 		dev_watchdog_down(dev);
850 		sync_needed |= !dev->dismantle;
851 	}
852 
853 	/* Wait for outstanding qdisc-less dev_queue_xmit calls.
854 	 * This is avoided if all devices are in dismantle phase :
855 	 * Caller will call synchronize_net() for us
856 	 */
857 	if (sync_needed)
858 		synchronize_net();
859 
860 	/* Wait for outstanding qdisc_run calls. */
861 	list_for_each_entry(dev, head, close_list)
862 		while (some_qdisc_is_busy(dev))
863 			yield();
864 }
865 
866 void dev_deactivate(struct net_device *dev)
867 {
868 	LIST_HEAD(single);
869 
870 	list_add(&dev->close_list, &single);
871 	dev_deactivate_many(&single);
872 	list_del(&single);
873 }
874 EXPORT_SYMBOL(dev_deactivate);
875 
876 static void dev_init_scheduler_queue(struct net_device *dev,
877 				     struct netdev_queue *dev_queue,
878 				     void *_qdisc)
879 {
880 	struct Qdisc *qdisc = _qdisc;
881 
882 	rcu_assign_pointer(dev_queue->qdisc, qdisc);
883 	dev_queue->qdisc_sleeping = qdisc;
884 }
885 
886 void dev_init_scheduler(struct net_device *dev)
887 {
888 	dev->qdisc = &noop_qdisc;
889 	netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
890 	if (dev_ingress_queue(dev))
891 		dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
892 
893 	setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
894 }
895 
896 static void shutdown_scheduler_queue(struct net_device *dev,
897 				     struct netdev_queue *dev_queue,
898 				     void *_qdisc_default)
899 {
900 	struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
901 	struct Qdisc *qdisc_default = _qdisc_default;
902 
903 	if (qdisc) {
904 		rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
905 		dev_queue->qdisc_sleeping = qdisc_default;
906 
907 		qdisc_destroy(qdisc);
908 	}
909 }
910 
911 void dev_shutdown(struct net_device *dev)
912 {
913 	netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
914 	if (dev_ingress_queue(dev))
915 		shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
916 	qdisc_destroy(dev->qdisc);
917 	dev->qdisc = &noop_qdisc;
918 
919 	WARN_ON(timer_pending(&dev->watchdog_timer));
920 }
921 
922 void psched_ratecfg_precompute(struct psched_ratecfg *r,
923 			       const struct tc_ratespec *conf,
924 			       u64 rate64)
925 {
926 	memset(r, 0, sizeof(*r));
927 	r->overhead = conf->overhead;
928 	r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
929 	r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
930 	r->mult = 1;
931 	/*
932 	 * The deal here is to replace a divide by a reciprocal one
933 	 * in fast path (a reciprocal divide is a multiply and a shift)
934 	 *
935 	 * Normal formula would be :
936 	 *  time_in_ns = (NSEC_PER_SEC * len) / rate_bps
937 	 *
938 	 * We compute mult/shift to use instead :
939 	 *  time_in_ns = (len * mult) >> shift;
940 	 *
941 	 * We try to get the highest possible mult value for accuracy,
942 	 * but have to make sure no overflows will ever happen.
943 	 */
944 	if (r->rate_bytes_ps > 0) {
945 		u64 factor = NSEC_PER_SEC;
946 
947 		for (;;) {
948 			r->mult = div64_u64(factor, r->rate_bytes_ps);
949 			if (r->mult & (1U << 31) || factor & (1ULL << 63))
950 				break;
951 			factor <<= 1;
952 			r->shift++;
953 		}
954 	}
955 }
956 EXPORT_SYMBOL(psched_ratecfg_precompute);
957