xref: /openbmc/linux/net/sched/sch_generic.c (revision 643d1f7f)
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 <net/pkt_sched.h>
28 
29 /* Main transmission queue. */
30 
31 /* Modifications to data participating in scheduling must be protected with
32  * dev->queue_lock spinlock.
33  *
34  * The idea is the following:
35  * - enqueue, dequeue are serialized via top level device
36  *   spinlock dev->queue_lock.
37  * - ingress filtering is serialized via top level device
38  *   spinlock dev->ingress_lock.
39  * - updates to tree and tree walking are only done under the rtnl mutex.
40  */
41 
42 void qdisc_lock_tree(struct net_device *dev)
43 	__acquires(dev->queue_lock)
44 	__acquires(dev->ingress_lock)
45 {
46 	spin_lock_bh(&dev->queue_lock);
47 	spin_lock(&dev->ingress_lock);
48 }
49 EXPORT_SYMBOL(qdisc_lock_tree);
50 
51 void qdisc_unlock_tree(struct net_device *dev)
52 	__releases(dev->ingress_lock)
53 	__releases(dev->queue_lock)
54 {
55 	spin_unlock(&dev->ingress_lock);
56 	spin_unlock_bh(&dev->queue_lock);
57 }
58 EXPORT_SYMBOL(qdisc_unlock_tree);
59 
60 static inline int qdisc_qlen(struct Qdisc *q)
61 {
62 	return q->q.qlen;
63 }
64 
65 static inline int dev_requeue_skb(struct sk_buff *skb, struct net_device *dev,
66 				  struct Qdisc *q)
67 {
68 	if (unlikely(skb->next))
69 		dev->gso_skb = skb;
70 	else
71 		q->ops->requeue(skb, q);
72 
73 	netif_schedule(dev);
74 	return 0;
75 }
76 
77 static inline struct sk_buff *dev_dequeue_skb(struct net_device *dev,
78 					      struct Qdisc *q)
79 {
80 	struct sk_buff *skb;
81 
82 	if ((skb = dev->gso_skb))
83 		dev->gso_skb = NULL;
84 	else
85 		skb = q->dequeue(q);
86 
87 	return skb;
88 }
89 
90 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
91 					   struct net_device *dev,
92 					   struct Qdisc *q)
93 {
94 	int ret;
95 
96 	if (unlikely(dev->xmit_lock_owner == smp_processor_id())) {
97 		/*
98 		 * Same CPU holding the lock. It may be a transient
99 		 * configuration error, when hard_start_xmit() recurses. We
100 		 * detect it by checking xmit owner and drop the packet when
101 		 * deadloop is detected. Return OK to try the next skb.
102 		 */
103 		kfree_skb(skb);
104 		if (net_ratelimit())
105 			printk(KERN_WARNING "Dead loop on netdevice %s, "
106 			       "fix it urgently!\n", dev->name);
107 		ret = qdisc_qlen(q);
108 	} else {
109 		/*
110 		 * Another cpu is holding lock, requeue & delay xmits for
111 		 * some time.
112 		 */
113 		__get_cpu_var(netdev_rx_stat).cpu_collision++;
114 		ret = dev_requeue_skb(skb, dev, q);
115 	}
116 
117 	return ret;
118 }
119 
120 /*
121  * NOTE: Called under dev->queue_lock with locally disabled BH.
122  *
123  * __LINK_STATE_QDISC_RUNNING guarantees only one CPU can process this
124  * device at a time. dev->queue_lock serializes queue accesses for
125  * this device AND dev->qdisc pointer itself.
126  *
127  *  netif_tx_lock serializes accesses to device driver.
128  *
129  *  dev->queue_lock and netif_tx_lock are mutually exclusive,
130  *  if one is grabbed, another must be free.
131  *
132  * Note, that this procedure can be called by a watchdog timer
133  *
134  * Returns to the caller:
135  *				0  - queue is empty or throttled.
136  *				>0 - queue is not empty.
137  *
138  */
139 static inline int qdisc_restart(struct net_device *dev)
140 {
141 	struct Qdisc *q = dev->qdisc;
142 	struct sk_buff *skb;
143 	int ret = NETDEV_TX_BUSY;
144 
145 	/* Dequeue packet */
146 	if (unlikely((skb = dev_dequeue_skb(dev, q)) == NULL))
147 		return 0;
148 
149 
150 	/* And release queue */
151 	spin_unlock(&dev->queue_lock);
152 
153 	HARD_TX_LOCK(dev, smp_processor_id());
154 	if (!netif_subqueue_stopped(dev, skb))
155 		ret = dev_hard_start_xmit(skb, dev);
156 	HARD_TX_UNLOCK(dev);
157 
158 	spin_lock(&dev->queue_lock);
159 	q = dev->qdisc;
160 
161 	switch (ret) {
162 	case NETDEV_TX_OK:
163 		/* Driver sent out skb successfully */
164 		ret = qdisc_qlen(q);
165 		break;
166 
167 	case NETDEV_TX_LOCKED:
168 		/* Driver try lock failed */
169 		ret = handle_dev_cpu_collision(skb, dev, q);
170 		break;
171 
172 	default:
173 		/* Driver returned NETDEV_TX_BUSY - requeue skb */
174 		if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
175 			printk(KERN_WARNING "BUG %s code %d qlen %d\n",
176 			       dev->name, ret, q->q.qlen);
177 
178 		ret = dev_requeue_skb(skb, dev, q);
179 		break;
180 	}
181 
182 	return ret;
183 }
184 
185 void __qdisc_run(struct net_device *dev)
186 {
187 	do {
188 		if (!qdisc_restart(dev))
189 			break;
190 	} while (!netif_queue_stopped(dev));
191 
192 	clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
193 }
194 
195 static void dev_watchdog(unsigned long arg)
196 {
197 	struct net_device *dev = (struct net_device *)arg;
198 
199 	netif_tx_lock(dev);
200 	if (dev->qdisc != &noop_qdisc) {
201 		if (netif_device_present(dev) &&
202 		    netif_running(dev) &&
203 		    netif_carrier_ok(dev)) {
204 			if (netif_queue_stopped(dev) &&
205 			    time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) {
206 
207 				printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n",
208 				       dev->name);
209 				dev->tx_timeout(dev);
210 			}
211 			if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo)))
212 				dev_hold(dev);
213 		}
214 	}
215 	netif_tx_unlock(dev);
216 
217 	dev_put(dev);
218 }
219 
220 void __netdev_watchdog_up(struct net_device *dev)
221 {
222 	if (dev->tx_timeout) {
223 		if (dev->watchdog_timeo <= 0)
224 			dev->watchdog_timeo = 5*HZ;
225 		if (!mod_timer(&dev->watchdog_timer,
226 			       round_jiffies(jiffies + dev->watchdog_timeo)))
227 			dev_hold(dev);
228 	}
229 }
230 
231 static void dev_watchdog_up(struct net_device *dev)
232 {
233 	__netdev_watchdog_up(dev);
234 }
235 
236 static void dev_watchdog_down(struct net_device *dev)
237 {
238 	netif_tx_lock_bh(dev);
239 	if (del_timer(&dev->watchdog_timer))
240 		dev_put(dev);
241 	netif_tx_unlock_bh(dev);
242 }
243 
244 /**
245  *	netif_carrier_on - set carrier
246  *	@dev: network device
247  *
248  * Device has detected that carrier.
249  */
250 void netif_carrier_on(struct net_device *dev)
251 {
252 	if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
253 		linkwatch_fire_event(dev);
254 		if (netif_running(dev))
255 			__netdev_watchdog_up(dev);
256 	}
257 }
258 EXPORT_SYMBOL(netif_carrier_on);
259 
260 /**
261  *	netif_carrier_off - clear carrier
262  *	@dev: network device
263  *
264  * Device has detected loss of carrier.
265  */
266 void netif_carrier_off(struct net_device *dev)
267 {
268 	if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
269 		linkwatch_fire_event(dev);
270 }
271 EXPORT_SYMBOL(netif_carrier_off);
272 
273 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
274    under all circumstances. It is difficult to invent anything faster or
275    cheaper.
276  */
277 
278 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
279 {
280 	kfree_skb(skb);
281 	return NET_XMIT_CN;
282 }
283 
284 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
285 {
286 	return NULL;
287 }
288 
289 static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
290 {
291 	if (net_ratelimit())
292 		printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
293 		       skb->dev->name);
294 	kfree_skb(skb);
295 	return NET_XMIT_CN;
296 }
297 
298 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
299 	.id		=	"noop",
300 	.priv_size	=	0,
301 	.enqueue	=	noop_enqueue,
302 	.dequeue	=	noop_dequeue,
303 	.requeue	=	noop_requeue,
304 	.owner		=	THIS_MODULE,
305 };
306 
307 struct Qdisc noop_qdisc = {
308 	.enqueue	=	noop_enqueue,
309 	.dequeue	=	noop_dequeue,
310 	.flags		=	TCQ_F_BUILTIN,
311 	.ops		=	&noop_qdisc_ops,
312 	.list		=	LIST_HEAD_INIT(noop_qdisc.list),
313 };
314 EXPORT_SYMBOL(noop_qdisc);
315 
316 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
317 	.id		=	"noqueue",
318 	.priv_size	=	0,
319 	.enqueue	=	noop_enqueue,
320 	.dequeue	=	noop_dequeue,
321 	.requeue	=	noop_requeue,
322 	.owner		=	THIS_MODULE,
323 };
324 
325 static struct Qdisc noqueue_qdisc = {
326 	.enqueue	=	NULL,
327 	.dequeue	=	noop_dequeue,
328 	.flags		=	TCQ_F_BUILTIN,
329 	.ops		=	&noqueue_qdisc_ops,
330 	.list		=	LIST_HEAD_INIT(noqueue_qdisc.list),
331 };
332 
333 
334 static const u8 prio2band[TC_PRIO_MAX+1] =
335 	{ 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
336 
337 /* 3-band FIFO queue: old style, but should be a bit faster than
338    generic prio+fifo combination.
339  */
340 
341 #define PFIFO_FAST_BANDS 3
342 
343 static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
344 					     struct Qdisc *qdisc)
345 {
346 	struct sk_buff_head *list = qdisc_priv(qdisc);
347 	return list + prio2band[skb->priority & TC_PRIO_MAX];
348 }
349 
350 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
351 {
352 	struct sk_buff_head *list = prio2list(skb, qdisc);
353 
354 	if (skb_queue_len(list) < qdisc->dev->tx_queue_len) {
355 		qdisc->q.qlen++;
356 		return __qdisc_enqueue_tail(skb, qdisc, list);
357 	}
358 
359 	return qdisc_drop(skb, qdisc);
360 }
361 
362 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
363 {
364 	int prio;
365 	struct sk_buff_head *list = qdisc_priv(qdisc);
366 
367 	for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
368 		if (!skb_queue_empty(list + prio)) {
369 			qdisc->q.qlen--;
370 			return __qdisc_dequeue_head(qdisc, list + prio);
371 		}
372 	}
373 
374 	return NULL;
375 }
376 
377 static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
378 {
379 	qdisc->q.qlen++;
380 	return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
381 }
382 
383 static void pfifo_fast_reset(struct Qdisc* qdisc)
384 {
385 	int prio;
386 	struct sk_buff_head *list = qdisc_priv(qdisc);
387 
388 	for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
389 		__qdisc_reset_queue(qdisc, list + prio);
390 
391 	qdisc->qstats.backlog = 0;
392 	qdisc->q.qlen = 0;
393 }
394 
395 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
396 {
397 	struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
398 
399 	memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
400 	NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
401 	return skb->len;
402 
403 nla_put_failure:
404 	return -1;
405 }
406 
407 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
408 {
409 	int prio;
410 	struct sk_buff_head *list = qdisc_priv(qdisc);
411 
412 	for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
413 		skb_queue_head_init(list + prio);
414 
415 	return 0;
416 }
417 
418 static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
419 	.id		=	"pfifo_fast",
420 	.priv_size	=	PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
421 	.enqueue	=	pfifo_fast_enqueue,
422 	.dequeue	=	pfifo_fast_dequeue,
423 	.requeue	=	pfifo_fast_requeue,
424 	.init		=	pfifo_fast_init,
425 	.reset		=	pfifo_fast_reset,
426 	.dump		=	pfifo_fast_dump,
427 	.owner		=	THIS_MODULE,
428 };
429 
430 struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops)
431 {
432 	void *p;
433 	struct Qdisc *sch;
434 	unsigned int size;
435 	int err = -ENOBUFS;
436 
437 	/* ensure that the Qdisc and the private data are 32-byte aligned */
438 	size = QDISC_ALIGN(sizeof(*sch));
439 	size += ops->priv_size + (QDISC_ALIGNTO - 1);
440 
441 	p = kzalloc(size, GFP_KERNEL);
442 	if (!p)
443 		goto errout;
444 	sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
445 	sch->padded = (char *) sch - (char *) p;
446 
447 	INIT_LIST_HEAD(&sch->list);
448 	skb_queue_head_init(&sch->q);
449 	sch->ops = ops;
450 	sch->enqueue = ops->enqueue;
451 	sch->dequeue = ops->dequeue;
452 	sch->dev = dev;
453 	dev_hold(dev);
454 	atomic_set(&sch->refcnt, 1);
455 
456 	return sch;
457 errout:
458 	return ERR_PTR(-err);
459 }
460 
461 struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops,
462 				 unsigned int parentid)
463 {
464 	struct Qdisc *sch;
465 
466 	sch = qdisc_alloc(dev, ops);
467 	if (IS_ERR(sch))
468 		goto errout;
469 	sch->stats_lock = &dev->queue_lock;
470 	sch->parent = parentid;
471 
472 	if (!ops->init || ops->init(sch, NULL) == 0)
473 		return sch;
474 
475 	qdisc_destroy(sch);
476 errout:
477 	return NULL;
478 }
479 EXPORT_SYMBOL(qdisc_create_dflt);
480 
481 /* Under dev->queue_lock and BH! */
482 
483 void qdisc_reset(struct Qdisc *qdisc)
484 {
485 	const struct Qdisc_ops *ops = qdisc->ops;
486 
487 	if (ops->reset)
488 		ops->reset(qdisc);
489 }
490 EXPORT_SYMBOL(qdisc_reset);
491 
492 /* this is the rcu callback function to clean up a qdisc when there
493  * are no further references to it */
494 
495 static void __qdisc_destroy(struct rcu_head *head)
496 {
497 	struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
498 	kfree((char *) qdisc - qdisc->padded);
499 }
500 
501 /* Under dev->queue_lock and BH! */
502 
503 void qdisc_destroy(struct Qdisc *qdisc)
504 {
505 	const struct Qdisc_ops  *ops = qdisc->ops;
506 
507 	if (qdisc->flags & TCQ_F_BUILTIN ||
508 	    !atomic_dec_and_test(&qdisc->refcnt))
509 		return;
510 
511 	list_del(&qdisc->list);
512 	gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
513 	if (ops->reset)
514 		ops->reset(qdisc);
515 	if (ops->destroy)
516 		ops->destroy(qdisc);
517 
518 	module_put(ops->owner);
519 	dev_put(qdisc->dev);
520 	call_rcu(&qdisc->q_rcu, __qdisc_destroy);
521 }
522 EXPORT_SYMBOL(qdisc_destroy);
523 
524 void dev_activate(struct net_device *dev)
525 {
526 	/* No queueing discipline is attached to device;
527 	   create default one i.e. pfifo_fast for devices,
528 	   which need queueing and noqueue_qdisc for
529 	   virtual interfaces
530 	 */
531 
532 	if (dev->qdisc_sleeping == &noop_qdisc) {
533 		struct Qdisc *qdisc;
534 		if (dev->tx_queue_len) {
535 			qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops,
536 						  TC_H_ROOT);
537 			if (qdisc == NULL) {
538 				printk(KERN_INFO "%s: activation failed\n", dev->name);
539 				return;
540 			}
541 			list_add_tail(&qdisc->list, &dev->qdisc_list);
542 		} else {
543 			qdisc =  &noqueue_qdisc;
544 		}
545 		dev->qdisc_sleeping = qdisc;
546 	}
547 
548 	if (!netif_carrier_ok(dev))
549 		/* Delay activation until next carrier-on event */
550 		return;
551 
552 	spin_lock_bh(&dev->queue_lock);
553 	rcu_assign_pointer(dev->qdisc, dev->qdisc_sleeping);
554 	if (dev->qdisc != &noqueue_qdisc) {
555 		dev->trans_start = jiffies;
556 		dev_watchdog_up(dev);
557 	}
558 	spin_unlock_bh(&dev->queue_lock);
559 }
560 
561 void dev_deactivate(struct net_device *dev)
562 {
563 	struct Qdisc *qdisc;
564 	struct sk_buff *skb;
565 	int running;
566 
567 	spin_lock_bh(&dev->queue_lock);
568 	qdisc = dev->qdisc;
569 	dev->qdisc = &noop_qdisc;
570 
571 	qdisc_reset(qdisc);
572 
573 	skb = dev->gso_skb;
574 	dev->gso_skb = NULL;
575 	spin_unlock_bh(&dev->queue_lock);
576 
577 	kfree_skb(skb);
578 
579 	dev_watchdog_down(dev);
580 
581 	/* Wait for outstanding qdisc-less dev_queue_xmit calls. */
582 	synchronize_rcu();
583 
584 	/* Wait for outstanding qdisc_run calls. */
585 	do {
586 		while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
587 			yield();
588 
589 		/*
590 		 * Double-check inside queue lock to ensure that all effects
591 		 * of the queue run are visible when we return.
592 		 */
593 		spin_lock_bh(&dev->queue_lock);
594 		running = test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
595 		spin_unlock_bh(&dev->queue_lock);
596 
597 		/*
598 		 * The running flag should never be set at this point because
599 		 * we've already set dev->qdisc to noop_qdisc *inside* the same
600 		 * pair of spin locks.  That is, if any qdisc_run starts after
601 		 * our initial test it should see the noop_qdisc and then
602 		 * clear the RUNNING bit before dropping the queue lock.  So
603 		 * if it is set here then we've found a bug.
604 		 */
605 	} while (WARN_ON_ONCE(running));
606 }
607 
608 void dev_init_scheduler(struct net_device *dev)
609 {
610 	qdisc_lock_tree(dev);
611 	dev->qdisc = &noop_qdisc;
612 	dev->qdisc_sleeping = &noop_qdisc;
613 	INIT_LIST_HEAD(&dev->qdisc_list);
614 	qdisc_unlock_tree(dev);
615 
616 	setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
617 }
618 
619 void dev_shutdown(struct net_device *dev)
620 {
621 	struct Qdisc *qdisc;
622 
623 	qdisc_lock_tree(dev);
624 	qdisc = dev->qdisc_sleeping;
625 	dev->qdisc = &noop_qdisc;
626 	dev->qdisc_sleeping = &noop_qdisc;
627 	qdisc_destroy(qdisc);
628 #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
629 	if ((qdisc = dev->qdisc_ingress) != NULL) {
630 		dev->qdisc_ingress = NULL;
631 		qdisc_destroy(qdisc);
632 	}
633 #endif
634 	BUG_TRAP(!timer_pending(&dev->watchdog_timer));
635 	qdisc_unlock_tree(dev);
636 }
637