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>
35 #include <net/pkt_sched.h>
37 /* Main transmission queue. */
39 /* Main qdisc structure lock.
41 However, modifications
42 to data, participating in scheduling must be additionally
43 protected with dev->queue_lock spinlock.
45 The idea is the following:
46 - enqueue, dequeue are serialized via top level device
47 spinlock dev->queue_lock.
48 - tree walking is protected by read_lock(qdisc_tree_lock)
49 and this lock is used only in process context.
50 - updates to tree are made only under rtnl semaphore,
51 hence this lock may be made without local bh disabling.
53 qdisc_tree_lock must be grabbed BEFORE dev->queue_lock!
55 rwlock_t qdisc_tree_lock = RW_LOCK_UNLOCKED;
58 dev->queue_lock serializes queue accesses for this device
59 AND dev->qdisc pointer itself.
61 dev->xmit_lock serializes accesses to device driver.
63 dev->queue_lock and dev->xmit_lock are mutually exclusive,
64 if one is grabbed, another must be free.
69 Note, that this procedure can be called by a watchdog timer, so that
70 we do not check dev->tbusy flag here.
72 Returns: 0 - queue is empty.
73 >0 - queue is not empty, but throttled.
74 <0 - queue is not empty. Device is throttled, if dev->tbusy != 0.
76 NOTE: Called under dev->queue_lock with locally disabled BH.
79 int qdisc_restart(struct net_device *dev)
81 struct Qdisc *q = dev->qdisc;
85 if ((skb = q->dequeue(q)) != NULL) {
86 if (spin_trylock(&dev->xmit_lock)) {
87 /* Remember that the driver is grabbed by us. */
88 dev->xmit_lock_owner = smp_processor_id();
90 /* And release queue */
91 spin_unlock(&dev->queue_lock);
93 if (!netif_queue_stopped(dev)) {
95 dev_queue_xmit_nit(skb, dev);
97 if (dev->hard_start_xmit(skb, dev) == 0) {
98 dev->xmit_lock_owner = -1;
99 spin_unlock(&dev->xmit_lock);
101 spin_lock(&dev->queue_lock);
106 /* Release the driver */
107 dev->xmit_lock_owner = -1;
108 spin_unlock(&dev->xmit_lock);
109 spin_lock(&dev->queue_lock);
112 /* So, someone grabbed the driver. */
114 /* It may be transient configuration error,
115 when hard_start_xmit() recurses. We detect
116 it by checking xmit owner and drop the
117 packet when deadloop is detected.
119 if (dev->xmit_lock_owner == smp_processor_id()) {
122 printk(KERN_DEBUG "Dead loop on netdevice %s, fix it urgently!\n", dev->name);
125 __get_cpu_var(netdev_rx_stat).cpu_collision++;
128 /* Device kicked us out :(
129 This is possible in three cases:
132 1. fastroute is enabled
133 2. device cannot determine busy state
134 before start of transmission (f.e. dialout)
135 3. device is buggy (ppp)
138 q->ops->requeue(skb, q);
145 static void dev_watchdog(unsigned long arg)
147 struct net_device *dev = (struct net_device *)arg;
149 spin_lock(&dev->xmit_lock);
150 if (dev->qdisc != &noop_qdisc) {
151 if (netif_device_present(dev) &&
152 netif_running(dev) &&
153 netif_carrier_ok(dev)) {
154 if (netif_queue_stopped(dev) &&
155 (jiffies - dev->trans_start) > dev->watchdog_timeo) {
156 printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n", dev->name);
157 dev->tx_timeout(dev);
159 if (!mod_timer(&dev->watchdog_timer, jiffies + dev->watchdog_timeo))
163 spin_unlock(&dev->xmit_lock);
168 static void dev_watchdog_init(struct net_device *dev)
170 init_timer(&dev->watchdog_timer);
171 dev->watchdog_timer.data = (unsigned long)dev;
172 dev->watchdog_timer.function = dev_watchdog;
175 void __netdev_watchdog_up(struct net_device *dev)
177 if (dev->tx_timeout) {
178 if (dev->watchdog_timeo <= 0)
179 dev->watchdog_timeo = 5*HZ;
180 if (!mod_timer(&dev->watchdog_timer, jiffies + dev->watchdog_timeo))
185 static void dev_watchdog_up(struct net_device *dev)
187 spin_lock_bh(&dev->xmit_lock);
188 __netdev_watchdog_up(dev);
189 spin_unlock_bh(&dev->xmit_lock);
192 static void dev_watchdog_down(struct net_device *dev)
194 spin_lock_bh(&dev->xmit_lock);
195 if (del_timer(&dev->watchdog_timer))
197 spin_unlock_bh(&dev->xmit_lock);
200 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
201 under all circumstances. It is difficult to invent anything faster or
206 noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
212 static struct sk_buff *
213 noop_dequeue(struct Qdisc * qdisc)
219 noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
222 printk(KERN_DEBUG "%s deferred output. It is buggy.\n", skb->dev->name);
227 struct Qdisc_ops noop_qdisc_ops = {
232 .enqueue = noop_enqueue,
233 .dequeue = noop_dequeue,
234 .requeue = noop_requeue,
235 .owner = THIS_MODULE,
238 struct Qdisc noop_qdisc = {
239 .enqueue = noop_enqueue,
240 .dequeue = noop_dequeue,
241 .flags = TCQ_F_BUILTIN,
242 .ops = &noop_qdisc_ops,
245 struct Qdisc_ops noqueue_qdisc_ops = {
250 .enqueue = noop_enqueue,
251 .dequeue = noop_dequeue,
252 .requeue = noop_requeue,
253 .owner = THIS_MODULE,
256 struct Qdisc noqueue_qdisc = {
258 .dequeue = noop_dequeue,
259 .flags = TCQ_F_BUILTIN,
260 .ops = &noqueue_qdisc_ops,
264 static const u8 prio2band[TC_PRIO_MAX+1] =
265 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
267 /* 3-band FIFO queue: old style, but should be a bit faster than
268 generic prio+fifo combination.
272 pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
274 struct sk_buff_head *list;
276 list = ((struct sk_buff_head*)qdisc->data) +
277 prio2band[skb->priority&TC_PRIO_MAX];
279 if (list->qlen < qdisc->dev->tx_queue_len) {
280 __skb_queue_tail(list, skb);
282 qdisc->stats.bytes += skb->len;
283 qdisc->stats.packets++;
286 qdisc->stats.drops++;
288 return NET_XMIT_DROP;
291 static struct sk_buff *
292 pfifo_fast_dequeue(struct Qdisc* qdisc)
295 struct sk_buff_head *list = ((struct sk_buff_head*)qdisc->data);
298 for (prio = 0; prio < 3; prio++, list++) {
299 skb = __skb_dequeue(list);
309 pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
311 struct sk_buff_head *list;
313 list = ((struct sk_buff_head*)qdisc->data) +
314 prio2band[skb->priority&TC_PRIO_MAX];
316 __skb_queue_head(list, skb);
322 pfifo_fast_reset(struct Qdisc* qdisc)
325 struct sk_buff_head *list = ((struct sk_buff_head*)qdisc->data);
327 for (prio=0; prio < 3; prio++)
328 skb_queue_purge(list+prio);
332 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
334 unsigned char *b = skb->tail;
335 struct tc_prio_qopt opt;
338 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
339 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
343 skb_trim(skb, b - skb->data);
347 static int pfifo_fast_init(struct Qdisc *qdisc, struct rtattr *opt)
350 struct sk_buff_head *list;
352 list = ((struct sk_buff_head*)qdisc->data);
355 skb_queue_head_init(list+i);
360 static struct Qdisc_ops pfifo_fast_ops = {
364 .priv_size = 3 * sizeof(struct sk_buff_head),
365 .enqueue = pfifo_fast_enqueue,
366 .dequeue = pfifo_fast_dequeue,
367 .requeue = pfifo_fast_requeue,
368 .init = pfifo_fast_init,
369 .reset = pfifo_fast_reset,
370 .dump = pfifo_fast_dump,
371 .owner = THIS_MODULE,
374 struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops)
377 int size = sizeof(*sch) + ops->priv_size;
379 sch = kmalloc(size, GFP_KERNEL);
382 memset(sch, 0, size);
384 skb_queue_head_init(&sch->q);
386 sch->enqueue = ops->enqueue;
387 sch->dequeue = ops->dequeue;
389 sch->stats_lock = &dev->queue_lock;
390 atomic_set(&sch->refcnt, 1);
391 /* enqueue is accessed locklessly - make sure it's visible
392 * before we set a netdevice's qdisc pointer to sch */
394 if (!ops->init || ops->init(sch, NULL) == 0)
401 /* Under dev->queue_lock and BH! */
403 void qdisc_reset(struct Qdisc *qdisc)
405 struct Qdisc_ops *ops = qdisc->ops;
411 /* this is the rcu callback function to clean up a qdisc when there
412 * are no further references to it */
414 static void __qdisc_destroy(struct rcu_head *head)
416 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
417 struct Qdisc_ops *ops = qdisc->ops;
419 #ifdef CONFIG_NET_ESTIMATOR
420 qdisc_kill_estimator(&qdisc->stats);
426 module_put(ops->owner);
428 if (!(qdisc->flags&TCQ_F_BUILTIN))
432 /* Under dev->queue_lock and BH! */
434 void qdisc_destroy(struct Qdisc *qdisc)
436 struct net_device *dev = qdisc->dev;
438 if (!atomic_dec_and_test(&qdisc->refcnt))
442 struct Qdisc *q, **qp;
443 for (qp = &qdisc->dev->qdisc_list; (q=*qp) != NULL; qp = &q->next) {
451 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
456 void dev_activate(struct net_device *dev)
458 /* No queueing discipline is attached to device;
459 create default one i.e. pfifo_fast for devices,
460 which need queueing and noqueue_qdisc for
464 if (dev->qdisc_sleeping == &noop_qdisc) {
466 if (dev->tx_queue_len) {
467 qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops);
469 printk(KERN_INFO "%s: activation failed\n", dev->name);
473 write_lock(&qdisc_tree_lock);
474 qdisc->next = dev->qdisc_list;
475 dev->qdisc_list = qdisc;
476 write_unlock(&qdisc_tree_lock);
479 qdisc = &noqueue_qdisc;
481 write_lock(&qdisc_tree_lock);
482 dev->qdisc_sleeping = qdisc;
483 write_unlock(&qdisc_tree_lock);
486 spin_lock_bh(&dev->queue_lock);
487 if ((dev->qdisc = dev->qdisc_sleeping) != &noqueue_qdisc) {
488 dev->trans_start = jiffies;
489 dev_watchdog_up(dev);
491 spin_unlock_bh(&dev->queue_lock);
494 void dev_deactivate(struct net_device *dev)
498 spin_lock_bh(&dev->queue_lock);
500 dev->qdisc = &noop_qdisc;
504 spin_unlock_bh(&dev->queue_lock);
506 dev_watchdog_down(dev);
508 while (test_bit(__LINK_STATE_SCHED, &dev->state))
511 spin_unlock_wait(&dev->xmit_lock);
514 void dev_init_scheduler(struct net_device *dev)
516 write_lock(&qdisc_tree_lock);
517 spin_lock_bh(&dev->queue_lock);
518 dev->qdisc = &noop_qdisc;
519 spin_unlock_bh(&dev->queue_lock);
520 dev->qdisc_sleeping = &noop_qdisc;
521 dev->qdisc_list = NULL;
522 write_unlock(&qdisc_tree_lock);
524 dev_watchdog_init(dev);
527 void dev_shutdown(struct net_device *dev)
531 write_lock(&qdisc_tree_lock);
532 spin_lock_bh(&dev->queue_lock);
533 qdisc = dev->qdisc_sleeping;
534 dev->qdisc = &noop_qdisc;
535 dev->qdisc_sleeping = &noop_qdisc;
536 qdisc_destroy(qdisc);
537 #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
538 if ((qdisc = dev->qdisc_ingress) != NULL) {
539 dev->qdisc_ingress = NULL;
540 qdisc_destroy(qdisc);
543 BUG_TRAP(dev->qdisc_list == NULL);
544 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
545 dev->qdisc_list = NULL;
546 spin_unlock_bh(&dev->queue_lock);
547 write_unlock(&qdisc_tree_lock);
550 EXPORT_SYMBOL(__netdev_watchdog_up);
551 EXPORT_SYMBOL(noop_qdisc);
552 EXPORT_SYMBOL(noop_qdisc_ops);
553 EXPORT_SYMBOL(qdisc_create_dflt);
554 EXPORT_SYMBOL(qdisc_destroy);
555 EXPORT_SYMBOL(qdisc_reset);
556 EXPORT_SYMBOL(qdisc_restart);
557 EXPORT_SYMBOL(qdisc_tree_lock);