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