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 <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>
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/rcupdate.h>
33 #include <linux/list.h>
35 #include <net/pkt_sched.h>
37 /* Main transmission queue. */
39 /* Modifications to data participating in scheduling must be protected with
40 * dev->queue_lock spinlock.
42 * The idea is the following:
43 * - enqueue, dequeue are serialized via top level device
44 * spinlock dev->queue_lock.
45 * - updates to tree and tree walking are only done under the rtnl mutex.
48 void qdisc_lock_tree(struct net_device *dev)
50 spin_lock_bh(&dev->queue_lock);
53 void qdisc_unlock_tree(struct net_device *dev)
55 spin_unlock_bh(&dev->queue_lock);
59 dev->queue_lock serializes queue accesses for this device
60 AND dev->qdisc pointer itself.
62 netif_tx_lock serializes accesses to device driver.
64 dev->queue_lock and netif_tx_lock are mutually exclusive,
65 if one is grabbed, another must be free.
70 Note, that this procedure can be called by a watchdog timer, so that
71 we do not check dev->tbusy flag here.
73 Returns: 0 - queue is empty.
74 >0 - queue is not empty, but throttled.
75 <0 - queue is not empty. Device is throttled, if dev->tbusy != 0.
77 NOTE: Called under dev->queue_lock with locally disabled BH.
80 static inline int qdisc_restart(struct net_device *dev)
82 struct Qdisc *q = dev->qdisc;
86 if (((skb = dev->gso_skb)) || ((skb = q->dequeue(q)))) {
87 unsigned nolock = (dev->features & NETIF_F_LLTX);
92 * When the driver has LLTX set it does its own locking
93 * in start_xmit. No need to add additional overhead by
94 * locking again. These checks are worth it because
95 * even uncongested locks can be quite expensive.
96 * The driver can do trylock like here too, in case
97 * of lock congestion it should return -1 and the packet
101 if (!netif_tx_trylock(dev)) {
103 /* So, someone grabbed the driver. */
105 /* It may be transient configuration error,
106 when hard_start_xmit() recurses. We detect
107 it by checking xmit owner and drop the
108 packet when deadloop is detected.
110 if (dev->xmit_lock_owner == smp_processor_id()) {
113 printk(KERN_DEBUG "Dead loop on netdevice %s, fix it urgently!\n", dev->name);
116 __get_cpu_var(netdev_rx_stat).cpu_collision++;
122 /* And release queue */
123 spin_unlock(&dev->queue_lock);
125 if (!netif_queue_stopped(dev)) {
128 ret = dev_hard_start_xmit(skb, dev);
129 if (ret == NETDEV_TX_OK) {
131 netif_tx_unlock(dev);
133 spin_lock(&dev->queue_lock);
136 if (ret == NETDEV_TX_LOCKED && nolock) {
137 spin_lock(&dev->queue_lock);
142 /* NETDEV_TX_BUSY - we need to requeue */
143 /* Release the driver */
145 netif_tx_unlock(dev);
147 spin_lock(&dev->queue_lock);
151 /* Device kicked us out :(
152 This is possible in three cases:
155 1. fastroute is enabled
156 2. device cannot determine busy state
157 before start of transmission (f.e. dialout)
158 3. device is buggy (ppp)
165 q->ops->requeue(skb, q);
169 BUG_ON((int) q->q.qlen < 0);
173 void __qdisc_run(struct net_device *dev)
175 if (unlikely(dev->qdisc == &noop_qdisc))
178 while (qdisc_restart(dev) < 0 && !netif_queue_stopped(dev))
182 clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
185 static void dev_watchdog(unsigned long arg)
187 struct net_device *dev = (struct net_device *)arg;
190 if (dev->qdisc != &noop_qdisc) {
191 if (netif_device_present(dev) &&
192 netif_running(dev) &&
193 netif_carrier_ok(dev)) {
194 if (netif_queue_stopped(dev) &&
195 time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) {
197 printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n",
199 dev->tx_timeout(dev);
201 if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo)))
205 netif_tx_unlock(dev);
210 static void dev_watchdog_init(struct net_device *dev)
212 init_timer(&dev->watchdog_timer);
213 dev->watchdog_timer.data = (unsigned long)dev;
214 dev->watchdog_timer.function = dev_watchdog;
217 void __netdev_watchdog_up(struct net_device *dev)
219 if (dev->tx_timeout) {
220 if (dev->watchdog_timeo <= 0)
221 dev->watchdog_timeo = 5*HZ;
222 if (!mod_timer(&dev->watchdog_timer, jiffies + dev->watchdog_timeo))
227 static void dev_watchdog_up(struct net_device *dev)
229 __netdev_watchdog_up(dev);
232 static void dev_watchdog_down(struct net_device *dev)
234 netif_tx_lock_bh(dev);
235 if (del_timer(&dev->watchdog_timer))
237 netif_tx_unlock_bh(dev);
240 void netif_carrier_on(struct net_device *dev)
242 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state))
243 linkwatch_fire_event(dev);
244 if (netif_running(dev))
245 __netdev_watchdog_up(dev);
248 void netif_carrier_off(struct net_device *dev)
250 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
251 linkwatch_fire_event(dev);
254 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
255 under all circumstances. It is difficult to invent anything faster or
259 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
265 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
270 static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
273 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
279 struct Qdisc_ops noop_qdisc_ops = {
282 .enqueue = noop_enqueue,
283 .dequeue = noop_dequeue,
284 .requeue = noop_requeue,
285 .owner = THIS_MODULE,
288 struct Qdisc noop_qdisc = {
289 .enqueue = noop_enqueue,
290 .dequeue = noop_dequeue,
291 .flags = TCQ_F_BUILTIN,
292 .ops = &noop_qdisc_ops,
293 .list = LIST_HEAD_INIT(noop_qdisc.list),
296 static struct Qdisc_ops noqueue_qdisc_ops = {
299 .enqueue = noop_enqueue,
300 .dequeue = noop_dequeue,
301 .requeue = noop_requeue,
302 .owner = THIS_MODULE,
305 static struct Qdisc noqueue_qdisc = {
307 .dequeue = noop_dequeue,
308 .flags = TCQ_F_BUILTIN,
309 .ops = &noqueue_qdisc_ops,
310 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
314 static const u8 prio2band[TC_PRIO_MAX+1] =
315 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
317 /* 3-band FIFO queue: old style, but should be a bit faster than
318 generic prio+fifo combination.
321 #define PFIFO_FAST_BANDS 3
323 static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
326 struct sk_buff_head *list = qdisc_priv(qdisc);
327 return list + prio2band[skb->priority & TC_PRIO_MAX];
330 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
332 struct sk_buff_head *list = prio2list(skb, qdisc);
334 if (skb_queue_len(list) < qdisc->dev->tx_queue_len) {
336 return __qdisc_enqueue_tail(skb, qdisc, list);
339 return qdisc_drop(skb, qdisc);
342 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
345 struct sk_buff_head *list = qdisc_priv(qdisc);
347 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
348 if (!skb_queue_empty(list + prio)) {
350 return __qdisc_dequeue_head(qdisc, list + prio);
357 static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
360 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
363 static void pfifo_fast_reset(struct Qdisc* qdisc)
366 struct sk_buff_head *list = qdisc_priv(qdisc);
368 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
369 __qdisc_reset_queue(qdisc, list + prio);
371 qdisc->qstats.backlog = 0;
375 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
377 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
379 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
380 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
387 static int pfifo_fast_init(struct Qdisc *qdisc, struct rtattr *opt)
390 struct sk_buff_head *list = qdisc_priv(qdisc);
392 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
393 skb_queue_head_init(list + prio);
398 static struct Qdisc_ops pfifo_fast_ops = {
400 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
401 .enqueue = pfifo_fast_enqueue,
402 .dequeue = pfifo_fast_dequeue,
403 .requeue = pfifo_fast_requeue,
404 .init = pfifo_fast_init,
405 .reset = pfifo_fast_reset,
406 .dump = pfifo_fast_dump,
407 .owner = THIS_MODULE,
410 struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops)
417 /* ensure that the Qdisc and the private data are 32-byte aligned */
418 size = QDISC_ALIGN(sizeof(*sch));
419 size += ops->priv_size + (QDISC_ALIGNTO - 1);
421 p = kzalloc(size, GFP_KERNEL);
424 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
425 sch->padded = (char *) sch - (char *) p;
427 INIT_LIST_HEAD(&sch->list);
428 skb_queue_head_init(&sch->q);
430 sch->enqueue = ops->enqueue;
431 sch->dequeue = ops->dequeue;
434 sch->stats_lock = &dev->queue_lock;
435 atomic_set(&sch->refcnt, 1);
439 return ERR_PTR(-err);
442 struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops,
443 unsigned int parentid)
447 sch = qdisc_alloc(dev, ops);
450 sch->parent = parentid;
452 if (!ops->init || ops->init(sch, NULL) == 0)
460 /* Under dev->queue_lock and BH! */
462 void qdisc_reset(struct Qdisc *qdisc)
464 struct Qdisc_ops *ops = qdisc->ops;
470 /* this is the rcu callback function to clean up a qdisc when there
471 * are no further references to it */
473 static void __qdisc_destroy(struct rcu_head *head)
475 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
476 kfree((char *) qdisc - qdisc->padded);
479 /* Under dev->queue_lock and BH! */
481 void qdisc_destroy(struct Qdisc *qdisc)
483 struct Qdisc_ops *ops = qdisc->ops;
485 if (qdisc->flags & TCQ_F_BUILTIN ||
486 !atomic_dec_and_test(&qdisc->refcnt))
489 list_del(&qdisc->list);
490 #ifdef CONFIG_NET_ESTIMATOR
491 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
498 module_put(ops->owner);
500 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
503 void dev_activate(struct net_device *dev)
505 /* No queueing discipline is attached to device;
506 create default one i.e. pfifo_fast for devices,
507 which need queueing and noqueue_qdisc for
511 if (dev->qdisc_sleeping == &noop_qdisc) {
513 if (dev->tx_queue_len) {
514 qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops,
517 printk(KERN_INFO "%s: activation failed\n", dev->name);
520 list_add_tail(&qdisc->list, &dev->qdisc_list);
522 qdisc = &noqueue_qdisc;
524 dev->qdisc_sleeping = qdisc;
527 if (!netif_carrier_ok(dev))
528 /* Delay activation until next carrier-on event */
531 spin_lock_bh(&dev->queue_lock);
532 rcu_assign_pointer(dev->qdisc, dev->qdisc_sleeping);
533 if (dev->qdisc != &noqueue_qdisc) {
534 dev->trans_start = jiffies;
535 dev_watchdog_up(dev);
537 spin_unlock_bh(&dev->queue_lock);
540 void dev_deactivate(struct net_device *dev)
544 spin_lock_bh(&dev->queue_lock);
546 dev->qdisc = &noop_qdisc;
550 spin_unlock_bh(&dev->queue_lock);
552 dev_watchdog_down(dev);
554 /* Wait for outstanding dev_queue_xmit calls. */
557 /* Wait for outstanding qdisc_run calls. */
558 while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
562 kfree_skb(dev->gso_skb);
567 void dev_init_scheduler(struct net_device *dev)
569 qdisc_lock_tree(dev);
570 dev->qdisc = &noop_qdisc;
571 dev->qdisc_sleeping = &noop_qdisc;
572 INIT_LIST_HEAD(&dev->qdisc_list);
573 qdisc_unlock_tree(dev);
575 dev_watchdog_init(dev);
578 void dev_shutdown(struct net_device *dev)
582 qdisc_lock_tree(dev);
583 qdisc = dev->qdisc_sleeping;
584 dev->qdisc = &noop_qdisc;
585 dev->qdisc_sleeping = &noop_qdisc;
586 qdisc_destroy(qdisc);
587 #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
588 if ((qdisc = dev->qdisc_ingress) != NULL) {
589 dev->qdisc_ingress = NULL;
590 qdisc_destroy(qdisc);
593 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
594 qdisc_unlock_tree(dev);
597 EXPORT_SYMBOL(netif_carrier_on);
598 EXPORT_SYMBOL(netif_carrier_off);
599 EXPORT_SYMBOL(noop_qdisc);
600 EXPORT_SYMBOL(qdisc_create_dflt);
601 EXPORT_SYMBOL(qdisc_destroy);
602 EXPORT_SYMBOL(qdisc_reset);
603 EXPORT_SYMBOL(qdisc_lock_tree);
604 EXPORT_SYMBOL(qdisc_unlock_tree);