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