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/types.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.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/list.h>
34 #include <net/pkt_sched.h>
36 /* Main transmission queue. */
38 /* Main qdisc structure lock.
40 However, modifications
41 to data, participating in scheduling must be additionally
42 protected with dev->queue_lock spinlock.
44 The idea is the following:
45 - enqueue, dequeue are serialized via top level device
46 spinlock dev->queue_lock.
47 - tree walking is protected by read_lock(qdisc_tree_lock)
48 and this lock is used only in process context.
49 - updates to tree are made only under rtnl semaphore,
50 hence this lock may be made without local bh disabling.
52 qdisc_tree_lock must be grabbed BEFORE dev->queue_lock!
54 rwlock_t qdisc_tree_lock = RW_LOCK_UNLOCKED;
57 dev->queue_lock serializes queue accesses for this device
58 AND dev->qdisc pointer itself.
60 dev->xmit_lock serializes accesses to device driver.
62 dev->queue_lock and dev->xmit_lock are mutually exclusive,
63 if one is grabbed, another must be free.
68 Note, that this procedure can be called by a watchdog timer, so that
69 we do not check dev->tbusy flag here.
71 Returns: 0 - queue is empty.
72 >0 - queue is not empty, but throttled.
73 <0 - queue is not empty. Device is throttled, if dev->tbusy != 0.
75 NOTE: Called under dev->queue_lock with locally disabled BH.
78 int qdisc_restart(struct net_device *dev)
80 struct Qdisc *q = dev->qdisc;
84 if ((skb = q->dequeue(q)) != NULL) {
85 if (spin_trylock(&dev->xmit_lock)) {
86 /* Remember that the driver is grabbed by us. */
87 dev->xmit_lock_owner = smp_processor_id();
89 /* And release queue */
90 spin_unlock(&dev->queue_lock);
92 if (!netif_queue_stopped(dev)) {
94 dev_queue_xmit_nit(skb, dev);
96 if (dev->hard_start_xmit(skb, dev) == 0) {
97 dev->xmit_lock_owner = -1;
98 spin_unlock(&dev->xmit_lock);
100 spin_lock(&dev->queue_lock);
105 /* Release the driver */
106 dev->xmit_lock_owner = -1;
107 spin_unlock(&dev->xmit_lock);
108 spin_lock(&dev->queue_lock);
111 /* So, someone grabbed the driver. */
113 /* It may be transient configuration error,
114 when hard_start_xmit() recurses. We detect
115 it by checking xmit owner and drop the
116 packet when deadloop is detected.
118 if (dev->xmit_lock_owner == smp_processor_id()) {
121 printk(KERN_DEBUG "Dead loop on netdevice %s, fix it urgently!\n", dev->name);
124 netdev_rx_stat[smp_processor_id()].cpu_collision++;
127 /* Device kicked us out :(
128 This is possible in three cases:
131 1. fastroute is enabled
132 2. device cannot determine busy state
133 before start of transmission (f.e. dialout)
134 3. device is buggy (ppp)
137 q->ops->requeue(skb, q);
144 static void dev_watchdog(unsigned long arg)
146 struct net_device *dev = (struct net_device *)arg;
148 spin_lock(&dev->xmit_lock);
149 if (dev->qdisc != &noop_qdisc) {
150 if (netif_device_present(dev) &&
151 netif_running(dev) &&
152 netif_carrier_ok(dev)) {
153 if (netif_queue_stopped(dev) &&
154 (jiffies - dev->trans_start) > dev->watchdog_timeo) {
155 printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n", dev->name);
156 dev->tx_timeout(dev);
158 if (!mod_timer(&dev->watchdog_timer, jiffies + dev->watchdog_timeo))
162 spin_unlock(&dev->xmit_lock);
167 static void dev_watchdog_init(struct net_device *dev)
169 init_timer(&dev->watchdog_timer);
170 dev->watchdog_timer.data = (unsigned long)dev;
171 dev->watchdog_timer.function = dev_watchdog;
174 void __netdev_watchdog_up(struct net_device *dev)
176 if (dev->tx_timeout) {
177 if (dev->watchdog_timeo <= 0)
178 dev->watchdog_timeo = 5*HZ;
179 if (!mod_timer(&dev->watchdog_timer, jiffies + dev->watchdog_timeo))
184 static void dev_watchdog_up(struct net_device *dev)
186 spin_lock_bh(&dev->xmit_lock);
187 __netdev_watchdog_up(dev);
188 spin_unlock_bh(&dev->xmit_lock);
191 static void dev_watchdog_down(struct net_device *dev)
193 spin_lock_bh(&dev->xmit_lock);
194 if (del_timer(&dev->watchdog_timer))
196 spin_unlock_bh(&dev->xmit_lock);
199 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
200 under all circumstances. It is difficult to invent anything faster or
205 noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
211 static struct sk_buff *
212 noop_dequeue(struct Qdisc * qdisc)
218 noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
221 printk(KERN_DEBUG "%s deferred output. It is buggy.\n", skb->dev->name);
226 struct Qdisc_ops noop_qdisc_ops =
238 struct Qdisc noop_qdisc =
247 struct Qdisc_ops noqueue_qdisc_ops =
260 struct Qdisc noqueue_qdisc =
269 static const u8 prio2band[TC_PRIO_MAX+1] =
270 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
272 /* 3-band FIFO queue: old style, but should be a bit faster than
273 generic prio+fifo combination.
277 pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
279 struct sk_buff_head *list;
281 list = ((struct sk_buff_head*)qdisc->data) +
282 prio2band[skb->priority&TC_PRIO_MAX];
284 if (list->qlen < qdisc->dev->tx_queue_len) {
285 __skb_queue_tail(list, skb);
287 qdisc->stats.bytes += skb->len;
288 qdisc->stats.packets++;
291 qdisc->stats.drops++;
293 return NET_XMIT_DROP;
296 static struct sk_buff *
297 pfifo_fast_dequeue(struct Qdisc* qdisc)
300 struct sk_buff_head *list = ((struct sk_buff_head*)qdisc->data);
303 for (prio = 0; prio < 3; prio++, list++) {
304 skb = __skb_dequeue(list);
314 pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
316 struct sk_buff_head *list;
318 list = ((struct sk_buff_head*)qdisc->data) +
319 prio2band[skb->priority&TC_PRIO_MAX];
321 __skb_queue_head(list, skb);
327 pfifo_fast_reset(struct Qdisc* qdisc)
330 struct sk_buff_head *list = ((struct sk_buff_head*)qdisc->data);
332 for (prio=0; prio < 3; prio++)
333 skb_queue_purge(list+prio);
337 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
339 unsigned char *b = skb->tail;
340 struct tc_prio_qopt opt;
343 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
344 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
348 skb_trim(skb, b - skb->data);
352 static int pfifo_fast_init(struct Qdisc *qdisc, struct rtattr *opt)
355 struct sk_buff_head *list;
357 list = ((struct sk_buff_head*)qdisc->data);
360 skb_queue_head_init(list+i);
365 static struct Qdisc_ops pfifo_fast_ops =
370 3 * sizeof(struct sk_buff_head),
385 struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops)
388 int size = sizeof(*sch) + ops->priv_size;
390 sch = kmalloc(size, GFP_KERNEL);
393 memset(sch, 0, size);
395 INIT_LIST_HEAD(&sch->list);
396 skb_queue_head_init(&sch->q);
398 sch->enqueue = ops->enqueue;
399 sch->dequeue = ops->dequeue;
401 sch->stats.lock = &dev->queue_lock;
402 atomic_set(&sch->refcnt, 1);
403 if (!ops->init || ops->init(sch, NULL) == 0)
410 /* Under dev->queue_lock and BH! */
412 void qdisc_reset(struct Qdisc *qdisc)
414 struct Qdisc_ops *ops = qdisc->ops;
420 /* Under dev->queue_lock and BH! */
422 void qdisc_destroy(struct Qdisc *qdisc)
424 struct Qdisc_ops *ops = qdisc->ops;
426 if (qdisc->flags&TCQ_F_BUILTIN ||
427 !atomic_dec_and_test(&qdisc->refcnt))
429 list_del(&qdisc->list);
430 #ifdef CONFIG_NET_ESTIMATOR
431 qdisc_kill_estimator(&qdisc->stats);
441 void dev_activate(struct net_device *dev)
443 /* No queueing discipline is attached to device;
444 create default one i.e. pfifo_fast for devices,
445 which need queueing and noqueue_qdisc for
449 if (dev->qdisc_sleeping == &noop_qdisc) {
451 if (dev->tx_queue_len) {
452 qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops);
454 printk(KERN_INFO "%s: activation failed\n", dev->name);
457 write_lock(&qdisc_tree_lock);
458 list_add_tail(&qdisc->list, &dev->qdisc_list);
459 write_unlock(&qdisc_tree_lock);
462 qdisc = &noqueue_qdisc;
464 write_lock(&qdisc_tree_lock);
465 dev->qdisc_sleeping = qdisc;
466 write_unlock(&qdisc_tree_lock);
469 spin_lock_bh(&dev->queue_lock);
470 if ((dev->qdisc = dev->qdisc_sleeping) != &noqueue_qdisc) {
471 dev->trans_start = jiffies;
472 dev_watchdog_up(dev);
474 spin_unlock_bh(&dev->queue_lock);
477 void dev_deactivate(struct net_device *dev)
481 spin_lock_bh(&dev->queue_lock);
483 dev->qdisc = &noop_qdisc;
487 spin_unlock_bh(&dev->queue_lock);
489 dev_watchdog_down(dev);
491 while (test_bit(__LINK_STATE_SCHED, &dev->state))
494 spin_unlock_wait(&dev->xmit_lock);
497 void dev_init_scheduler(struct net_device *dev)
499 write_lock(&qdisc_tree_lock);
500 spin_lock_bh(&dev->queue_lock);
501 dev->qdisc = &noop_qdisc;
502 spin_unlock_bh(&dev->queue_lock);
503 dev->qdisc_sleeping = &noop_qdisc;
504 INIT_LIST_HEAD(&dev->qdisc_list);
505 write_unlock(&qdisc_tree_lock);
507 dev_watchdog_init(dev);
510 void dev_shutdown(struct net_device *dev)
514 write_lock(&qdisc_tree_lock);
515 spin_lock_bh(&dev->queue_lock);
516 qdisc = dev->qdisc_sleeping;
517 dev->qdisc = &noop_qdisc;
518 dev->qdisc_sleeping = &noop_qdisc;
519 qdisc_destroy(qdisc);
520 #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
521 if ((qdisc = dev->qdisc_ingress) != NULL) {
522 dev->qdisc_ingress = NULL;
523 qdisc_destroy(qdisc);
526 BUG_TRAP(list_empty(&dev->qdisc_list));
527 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
528 spin_unlock_bh(&dev->queue_lock);
529 write_unlock(&qdisc_tree_lock);