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>
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 __get_cpu_var(netdev_rx_stat).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 = {
231 .enqueue = noop_enqueue,
232 .dequeue = noop_dequeue,
233 .requeue = noop_requeue,
234 .owner = THIS_MODULE,
237 struct Qdisc noop_qdisc = {
238 .enqueue = noop_enqueue,
239 .dequeue = noop_dequeue,
240 .flags = TCQ_F_BUILTIN,
241 .ops = &noop_qdisc_ops,
244 struct Qdisc_ops noqueue_qdisc_ops = {
249 .enqueue = noop_enqueue,
250 .dequeue = noop_dequeue,
251 .requeue = noop_requeue,
252 .owner = THIS_MODULE,
255 struct Qdisc noqueue_qdisc = {
257 .dequeue = noop_dequeue,
258 .flags = TCQ_F_BUILTIN,
259 .ops = &noqueue_qdisc_ops,
263 static const u8 prio2band[TC_PRIO_MAX+1] =
264 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
266 /* 3-band FIFO queue: old style, but should be a bit faster than
267 generic prio+fifo combination.
271 pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
273 struct sk_buff_head *list;
275 list = ((struct sk_buff_head*)qdisc->data) +
276 prio2band[skb->priority&TC_PRIO_MAX];
278 if (list->qlen < qdisc->dev->tx_queue_len) {
279 __skb_queue_tail(list, skb);
281 qdisc->stats.bytes += skb->len;
282 qdisc->stats.packets++;
285 qdisc->stats.drops++;
287 return NET_XMIT_DROP;
290 static struct sk_buff *
291 pfifo_fast_dequeue(struct Qdisc* qdisc)
294 struct sk_buff_head *list = ((struct sk_buff_head*)qdisc->data);
297 for (prio = 0; prio < 3; prio++, list++) {
298 skb = __skb_dequeue(list);
308 pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
310 struct sk_buff_head *list;
312 list = ((struct sk_buff_head*)qdisc->data) +
313 prio2band[skb->priority&TC_PRIO_MAX];
315 __skb_queue_head(list, skb);
321 pfifo_fast_reset(struct Qdisc* qdisc)
324 struct sk_buff_head *list = ((struct sk_buff_head*)qdisc->data);
326 for (prio=0; prio < 3; prio++)
327 skb_queue_purge(list+prio);
331 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
333 unsigned char *b = skb->tail;
334 struct tc_prio_qopt opt;
337 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
338 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
342 skb_trim(skb, b - skb->data);
346 static int pfifo_fast_init(struct Qdisc *qdisc, struct rtattr *opt)
349 struct sk_buff_head *list;
351 list = ((struct sk_buff_head*)qdisc->data);
354 skb_queue_head_init(list+i);
359 static struct Qdisc_ops pfifo_fast_ops = {
363 .priv_size = 3 * sizeof(struct sk_buff_head),
364 .enqueue = pfifo_fast_enqueue,
365 .dequeue = pfifo_fast_dequeue,
366 .requeue = pfifo_fast_requeue,
367 .init = pfifo_fast_init,
368 .reset = pfifo_fast_reset,
369 .dump = pfifo_fast_dump,
370 .owner = THIS_MODULE,
373 struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops)
376 int size = sizeof(*sch) + ops->priv_size;
378 sch = kmalloc(size, GFP_KERNEL);
381 memset(sch, 0, size);
383 skb_queue_head_init(&sch->q);
385 sch->enqueue = ops->enqueue;
386 sch->dequeue = ops->dequeue;
388 sch->stats.lock = &dev->queue_lock;
389 atomic_set(&sch->refcnt, 1);
390 if (!ops->init || ops->init(sch, NULL) == 0)
397 /* Under dev->queue_lock and BH! */
399 void qdisc_reset(struct Qdisc *qdisc)
401 struct Qdisc_ops *ops = qdisc->ops;
407 /* Under dev->queue_lock and BH! */
409 void qdisc_destroy(struct Qdisc *qdisc)
411 struct Qdisc_ops *ops = qdisc->ops;
412 struct net_device *dev;
414 if (!atomic_dec_and_test(&qdisc->refcnt))
420 struct Qdisc *q, **qp;
421 for (qp = &qdisc->dev->qdisc_list; (q=*qp) != NULL; qp = &q->next) {
428 #ifdef CONFIG_NET_ESTIMATOR
429 qdisc_kill_estimator(&qdisc->stats);
435 module_put(ops->owner);
436 if (!(qdisc->flags&TCQ_F_BUILTIN))
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);
458 write_lock(&qdisc_tree_lock);
459 qdisc->next = dev->qdisc_list;
460 dev->qdisc_list = qdisc;
461 write_unlock(&qdisc_tree_lock);
464 qdisc = &noqueue_qdisc;
466 write_lock(&qdisc_tree_lock);
467 dev->qdisc_sleeping = qdisc;
468 write_unlock(&qdisc_tree_lock);
471 spin_lock_bh(&dev->queue_lock);
472 if ((dev->qdisc = dev->qdisc_sleeping) != &noqueue_qdisc) {
473 dev->trans_start = jiffies;
474 dev_watchdog_up(dev);
476 spin_unlock_bh(&dev->queue_lock);
479 void dev_deactivate(struct net_device *dev)
483 spin_lock_bh(&dev->queue_lock);
485 dev->qdisc = &noop_qdisc;
489 spin_unlock_bh(&dev->queue_lock);
491 dev_watchdog_down(dev);
493 while (test_bit(__LINK_STATE_SCHED, &dev->state))
496 spin_unlock_wait(&dev->xmit_lock);
499 void dev_init_scheduler(struct net_device *dev)
501 write_lock(&qdisc_tree_lock);
502 spin_lock_bh(&dev->queue_lock);
503 dev->qdisc = &noop_qdisc;
504 spin_unlock_bh(&dev->queue_lock);
505 dev->qdisc_sleeping = &noop_qdisc;
506 dev->qdisc_list = NULL;
507 write_unlock(&qdisc_tree_lock);
509 dev_watchdog_init(dev);
512 void dev_shutdown(struct net_device *dev)
516 write_lock(&qdisc_tree_lock);
517 spin_lock_bh(&dev->queue_lock);
518 qdisc = dev->qdisc_sleeping;
519 dev->qdisc = &noop_qdisc;
520 dev->qdisc_sleeping = &noop_qdisc;
521 qdisc_destroy(qdisc);
522 #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
523 if ((qdisc = dev->qdisc_ingress) != NULL) {
524 dev->qdisc_ingress = NULL;
525 qdisc_destroy(qdisc);
528 BUG_TRAP(dev->qdisc_list == NULL);
529 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
530 dev->qdisc_list = NULL;
531 spin_unlock_bh(&dev->queue_lock);
532 write_unlock(&qdisc_tree_lock);
535 EXPORT_SYMBOL(__netdev_watchdog_up);
536 EXPORT_SYMBOL(noop_qdisc);
537 EXPORT_SYMBOL(noop_qdisc_ops);
538 EXPORT_SYMBOL(qdisc_create_dflt);
539 EXPORT_SYMBOL(qdisc_destroy);
540 EXPORT_SYMBOL(qdisc_reset);
541 EXPORT_SYMBOL(qdisc_restart);
542 EXPORT_SYMBOL(qdisc_tree_lock);