2 * net/sched/sch_generic.c Generic packet scheduler routines.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * Jamal Hadi Salim, <hadi@cyberus.ca> 990601
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <asm/bitops.h>
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/string.h>
24 #include <linux/socket.h>
25 #include <linux/sockios.h>
27 #include <linux/errno.h>
28 #include <linux/interrupt.h>
29 #include <linux/netdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/rtnetlink.h>
32 #include <linux/init.h>
33 #include <linux/rcupdate.h>
34 #include <linux/list.h>
36 #include <net/pkt_sched.h>
38 /* Main transmission queue. */
40 /* Main qdisc structure lock.
42 However, modifications
43 to data, participating in scheduling must be additionally
44 protected with dev->queue_lock spinlock.
46 The idea is the following:
47 - enqueue, dequeue are serialized via top level device
48 spinlock dev->queue_lock.
49 - tree walking is protected by read_lock_bh(qdisc_tree_lock)
50 and this lock is used only in process context.
51 - updates to tree are made under rtnl semaphore or
52 from softirq context (__qdisc_destroy rcu-callback)
53 hence this lock needs local bh disabling.
55 qdisc_tree_lock must be grabbed BEFORE dev->queue_lock!
57 rwlock_t qdisc_tree_lock = RW_LOCK_UNLOCKED;
59 void qdisc_lock_tree(struct net_device *dev)
61 write_lock_bh(&qdisc_tree_lock);
62 spin_lock_bh(&dev->queue_lock);
65 void qdisc_unlock_tree(struct net_device *dev)
67 spin_unlock_bh(&dev->queue_lock);
68 write_unlock_bh(&qdisc_tree_lock);
72 dev->queue_lock serializes queue accesses for this device
73 AND dev->qdisc pointer itself.
75 dev->xmit_lock serializes accesses to device driver.
77 dev->queue_lock and dev->xmit_lock are mutually exclusive,
78 if one is grabbed, another must be free.
83 Note, that this procedure can be called by a watchdog timer, so that
84 we do not check dev->tbusy flag here.
86 Returns: 0 - queue is empty.
87 >0 - queue is not empty, but throttled.
88 <0 - queue is not empty. Device is throttled, if dev->tbusy != 0.
90 NOTE: Called under dev->queue_lock with locally disabled BH.
93 int qdisc_restart(struct net_device *dev)
95 struct Qdisc *q = dev->qdisc;
99 if ((skb = q->dequeue(q)) != NULL) {
100 if (spin_trylock(&dev->xmit_lock)) {
101 /* Remember that the driver is grabbed by us. */
102 dev->xmit_lock_owner = smp_processor_id();
104 /* And release queue */
105 spin_unlock(&dev->queue_lock);
107 if (!netif_queue_stopped(dev)) {
109 dev_queue_xmit_nit(skb, dev);
111 if (dev->hard_start_xmit(skb, dev) == 0) {
112 dev->xmit_lock_owner = -1;
113 spin_unlock(&dev->xmit_lock);
115 spin_lock(&dev->queue_lock);
120 /* Release the driver */
121 dev->xmit_lock_owner = -1;
122 spin_unlock(&dev->xmit_lock);
123 spin_lock(&dev->queue_lock);
126 /* So, someone grabbed the driver. */
128 /* It may be transient configuration error,
129 when hard_start_xmit() recurses. We detect
130 it by checking xmit owner and drop the
131 packet when deadloop is detected.
133 if (dev->xmit_lock_owner == smp_processor_id()) {
136 printk(KERN_DEBUG "Dead loop on netdevice %s, fix it urgently!\n", dev->name);
139 __get_cpu_var(netdev_rx_stat).cpu_collision++;
142 /* Device kicked us out :(
143 This is possible in three cases:
146 1. fastroute is enabled
147 2. device cannot determine busy state
148 before start of transmission (f.e. dialout)
149 3. device is buggy (ppp)
152 q->ops->requeue(skb, q);
159 static void dev_watchdog(unsigned long arg)
161 struct net_device *dev = (struct net_device *)arg;
163 spin_lock(&dev->xmit_lock);
164 if (dev->qdisc != &noop_qdisc) {
165 if (netif_device_present(dev) &&
166 netif_running(dev) &&
167 netif_carrier_ok(dev)) {
168 if (netif_queue_stopped(dev) &&
169 (jiffies - dev->trans_start) > dev->watchdog_timeo) {
170 printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n", dev->name);
171 dev->tx_timeout(dev);
173 if (!mod_timer(&dev->watchdog_timer, jiffies + dev->watchdog_timeo))
177 spin_unlock(&dev->xmit_lock);
182 static void dev_watchdog_init(struct net_device *dev)
184 init_timer(&dev->watchdog_timer);
185 dev->watchdog_timer.data = (unsigned long)dev;
186 dev->watchdog_timer.function = dev_watchdog;
189 void __netdev_watchdog_up(struct net_device *dev)
191 if (dev->tx_timeout) {
192 if (dev->watchdog_timeo <= 0)
193 dev->watchdog_timeo = 5*HZ;
194 if (!mod_timer(&dev->watchdog_timer, jiffies + dev->watchdog_timeo))
199 static void dev_watchdog_up(struct net_device *dev)
201 spin_lock_bh(&dev->xmit_lock);
202 __netdev_watchdog_up(dev);
203 spin_unlock_bh(&dev->xmit_lock);
206 static void dev_watchdog_down(struct net_device *dev)
208 spin_lock_bh(&dev->xmit_lock);
209 if (del_timer(&dev->watchdog_timer))
211 spin_unlock_bh(&dev->xmit_lock);
214 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
215 under all circumstances. It is difficult to invent anything faster or
220 noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
226 static struct sk_buff *
227 noop_dequeue(struct Qdisc * qdisc)
233 noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
236 printk(KERN_DEBUG "%s deferred output. It is buggy.\n", skb->dev->name);
241 struct Qdisc_ops noop_qdisc_ops = {
246 .enqueue = noop_enqueue,
247 .dequeue = noop_dequeue,
248 .requeue = noop_requeue,
249 .owner = THIS_MODULE,
252 struct Qdisc noop_qdisc = {
253 .enqueue = noop_enqueue,
254 .dequeue = noop_dequeue,
255 .flags = TCQ_F_BUILTIN,
256 .ops = &noop_qdisc_ops,
259 struct Qdisc_ops noqueue_qdisc_ops = {
264 .enqueue = noop_enqueue,
265 .dequeue = noop_dequeue,
266 .requeue = noop_requeue,
267 .owner = THIS_MODULE,
270 struct Qdisc noqueue_qdisc = {
272 .dequeue = noop_dequeue,
273 .flags = TCQ_F_BUILTIN,
274 .ops = &noqueue_qdisc_ops,
278 static const u8 prio2band[TC_PRIO_MAX+1] =
279 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
281 /* 3-band FIFO queue: old style, but should be a bit faster than
282 generic prio+fifo combination.
286 pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
288 struct sk_buff_head *list = qdisc_priv(qdisc);
290 list += prio2band[skb->priority&TC_PRIO_MAX];
292 if (list->qlen < qdisc->dev->tx_queue_len) {
293 __skb_queue_tail(list, skb);
295 qdisc->stats.bytes += skb->len;
296 qdisc->stats.packets++;
299 qdisc->stats.drops++;
301 return NET_XMIT_DROP;
304 static struct sk_buff *
305 pfifo_fast_dequeue(struct Qdisc* qdisc)
308 struct sk_buff_head *list = qdisc_priv(qdisc);
311 for (prio = 0; prio < 3; prio++, list++) {
312 skb = __skb_dequeue(list);
322 pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
324 struct sk_buff_head *list = qdisc_priv(qdisc);
326 list += prio2band[skb->priority&TC_PRIO_MAX];
328 __skb_queue_head(list, skb);
334 pfifo_fast_reset(struct Qdisc* qdisc)
337 struct sk_buff_head *list = qdisc_priv(qdisc);
339 for (prio=0; prio < 3; prio++)
340 skb_queue_purge(list+prio);
344 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
346 unsigned char *b = skb->tail;
347 struct tc_prio_qopt opt;
350 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
351 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
355 skb_trim(skb, b - skb->data);
359 static int pfifo_fast_init(struct Qdisc *qdisc, struct rtattr *opt)
362 struct sk_buff_head *list = qdisc_priv(qdisc);
365 skb_queue_head_init(list+i);
370 static struct Qdisc_ops pfifo_fast_ops = {
374 .priv_size = 3 * sizeof(struct sk_buff_head),
375 .enqueue = pfifo_fast_enqueue,
376 .dequeue = pfifo_fast_dequeue,
377 .requeue = pfifo_fast_requeue,
378 .init = pfifo_fast_init,
379 .reset = pfifo_fast_reset,
380 .dump = pfifo_fast_dump,
381 .owner = THIS_MODULE,
384 struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops)
390 /* ensure that the Qdisc and the private data are 32-byte aligned */
391 size = ((sizeof(*sch) + QDISC_ALIGN_CONST) & ~QDISC_ALIGN_CONST);
392 size += ops->priv_size + QDISC_ALIGN_CONST;
394 p = kmalloc(size, GFP_KERNEL);
399 sch = (struct Qdisc *)(((unsigned long)p + QDISC_ALIGN_CONST)
400 & ~QDISC_ALIGN_CONST);
401 sch->padded = (char *)sch - (char *)p;
403 INIT_LIST_HEAD(&sch->list);
404 skb_queue_head_init(&sch->q);
406 sch->enqueue = ops->enqueue;
407 sch->dequeue = ops->dequeue;
410 sch->stats_lock = &dev->queue_lock;
411 atomic_set(&sch->refcnt, 1);
412 /* enqueue is accessed locklessly - make sure it's visible
413 * before we set a netdevice's qdisc pointer to sch */
415 if (!ops->init || ops->init(sch, NULL) == 0)
422 /* Under dev->queue_lock and BH! */
424 void qdisc_reset(struct Qdisc *qdisc)
426 struct Qdisc_ops *ops = qdisc->ops;
432 /* this is the rcu callback function to clean up a qdisc when there
433 * are no further references to it */
435 static void __qdisc_destroy(struct rcu_head *head)
437 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
438 struct Qdisc_ops *ops = qdisc->ops;
440 #ifdef CONFIG_NET_ESTIMATOR
441 qdisc_kill_estimator(&qdisc->stats);
443 write_lock(&qdisc_tree_lock);
448 write_unlock(&qdisc_tree_lock);
449 module_put(ops->owner);
452 if (!(qdisc->flags&TCQ_F_BUILTIN))
453 kfree((char *) qdisc - qdisc->padded);
456 /* Under dev->queue_lock and BH! */
458 void qdisc_destroy(struct Qdisc *qdisc)
460 if (!atomic_dec_and_test(&qdisc->refcnt))
462 list_del(&qdisc->list);
463 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
466 void dev_activate(struct net_device *dev)
468 /* No queueing discipline is attached to device;
469 create default one i.e. pfifo_fast for devices,
470 which need queueing and noqueue_qdisc for
474 if (dev->qdisc_sleeping == &noop_qdisc) {
476 if (dev->tx_queue_len) {
477 qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops);
479 printk(KERN_INFO "%s: activation failed\n", dev->name);
482 write_lock_bh(&qdisc_tree_lock);
483 list_add_tail(&qdisc->list, &dev->qdisc_list);
484 write_unlock_bh(&qdisc_tree_lock);
486 qdisc = &noqueue_qdisc;
488 write_lock_bh(&qdisc_tree_lock);
489 dev->qdisc_sleeping = qdisc;
490 write_unlock_bh(&qdisc_tree_lock);
493 spin_lock_bh(&dev->queue_lock);
494 if ((dev->qdisc = dev->qdisc_sleeping) != &noqueue_qdisc) {
495 dev->trans_start = jiffies;
496 dev_watchdog_up(dev);
498 spin_unlock_bh(&dev->queue_lock);
501 void dev_deactivate(struct net_device *dev)
505 spin_lock_bh(&dev->queue_lock);
507 dev->qdisc = &noop_qdisc;
511 spin_unlock_bh(&dev->queue_lock);
513 dev_watchdog_down(dev);
515 while (test_bit(__LINK_STATE_SCHED, &dev->state))
518 spin_unlock_wait(&dev->xmit_lock);
521 void dev_init_scheduler(struct net_device *dev)
523 qdisc_lock_tree(dev);
524 dev->qdisc = &noop_qdisc;
525 dev->qdisc_sleeping = &noop_qdisc;
526 INIT_LIST_HEAD(&dev->qdisc_list);
527 qdisc_unlock_tree(dev);
529 dev_watchdog_init(dev);
532 void dev_shutdown(struct net_device *dev)
536 qdisc_lock_tree(dev);
537 qdisc = dev->qdisc_sleeping;
538 dev->qdisc = &noop_qdisc;
539 dev->qdisc_sleeping = &noop_qdisc;
540 qdisc_destroy(qdisc);
541 #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
542 if ((qdisc = dev->qdisc_ingress) != NULL) {
543 dev->qdisc_ingress = NULL;
544 qdisc_destroy(qdisc);
547 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
548 qdisc_unlock_tree(dev);
551 EXPORT_SYMBOL(__netdev_watchdog_up);
552 EXPORT_SYMBOL(noop_qdisc);
553 EXPORT_SYMBOL(noop_qdisc_ops);
554 EXPORT_SYMBOL(qdisc_create_dflt);
555 EXPORT_SYMBOL(qdisc_destroy);
556 EXPORT_SYMBOL(qdisc_reset);
557 EXPORT_SYMBOL(qdisc_restart);
558 EXPORT_SYMBOL(qdisc_lock_tree);
559 EXPORT_SYMBOL(qdisc_unlock_tree);