netdev: Move atomic queue state bits into netdev_queue.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sched / sch_generic.c
blob243de935b182f2696f455f152ab45fba4aa2dbff
1 /*
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
11 * - Ingress support
14 #include <linux/bitops.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/init.h>
25 #include <linux/rcupdate.h>
26 #include <linux/list.h>
27 #include <net/pkt_sched.h>
29 /* Main transmission queue. */
31 /* Modifications to data participating in scheduling must be protected with
32 * queue->lock spinlock.
34 * The idea is the following:
35 * - enqueue, dequeue are serialized via top level device
36 * spinlock queue->lock.
37 * - ingress filtering is serialized via top level device
38 * spinlock dev->rx_queue.lock.
39 * - updates to tree and tree walking are only done under the rtnl mutex.
42 void qdisc_lock_tree(struct net_device *dev)
43 __acquires(dev->tx_queue.lock)
44 __acquires(dev->rx_queue.lock)
46 spin_lock_bh(&dev->tx_queue.lock);
47 spin_lock(&dev->rx_queue.lock);
49 EXPORT_SYMBOL(qdisc_lock_tree);
51 void qdisc_unlock_tree(struct net_device *dev)
52 __releases(dev->rx_queue.lock)
53 __releases(dev->tx_queue.lock)
55 spin_unlock(&dev->rx_queue.lock);
56 spin_unlock_bh(&dev->tx_queue.lock);
58 EXPORT_SYMBOL(qdisc_unlock_tree);
60 static inline int qdisc_qlen(struct Qdisc *q)
62 return q->q.qlen;
65 static inline int dev_requeue_skb(struct sk_buff *skb,
66 struct netdev_queue *dev_queue,
67 struct Qdisc *q)
69 if (unlikely(skb->next))
70 dev_queue->gso_skb = skb;
71 else
72 q->ops->requeue(skb, q);
74 netif_schedule_queue(dev_queue);
75 return 0;
78 static inline struct sk_buff *dequeue_skb(struct netdev_queue *dev_queue,
79 struct Qdisc *q)
81 struct sk_buff *skb;
83 if ((skb = dev_queue->gso_skb))
84 dev_queue->gso_skb = NULL;
85 else
86 skb = q->dequeue(q);
88 return skb;
91 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
92 struct netdev_queue *dev_queue,
93 struct Qdisc *q)
95 int ret;
97 if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
99 * Same CPU holding the lock. It may be a transient
100 * configuration error, when hard_start_xmit() recurses. We
101 * detect it by checking xmit owner and drop the packet when
102 * deadloop is detected. Return OK to try the next skb.
104 kfree_skb(skb);
105 if (net_ratelimit())
106 printk(KERN_WARNING "Dead loop on netdevice %s, "
107 "fix it urgently!\n", dev_queue->dev->name);
108 ret = qdisc_qlen(q);
109 } else {
111 * Another cpu is holding lock, requeue & delay xmits for
112 * some time.
114 __get_cpu_var(netdev_rx_stat).cpu_collision++;
115 ret = dev_requeue_skb(skb, dev_queue, q);
118 return ret;
122 * NOTE: Called under queue->lock with locally disabled BH.
124 * __QUEUE_STATE_QDISC_RUNNING guarantees only one CPU can process
125 * this queue at a time. queue->lock serializes queue accesses for
126 * this queue AND txq->qdisc pointer itself.
128 * netif_tx_lock serializes accesses to device driver.
130 * queue->lock and netif_tx_lock are mutually exclusive,
131 * if one is grabbed, another must be free.
133 * Note, that this procedure can be called by a watchdog timer
135 * Returns to the caller:
136 * 0 - queue is empty or throttled.
137 * >0 - queue is not empty.
140 static inline int qdisc_restart(struct netdev_queue *txq)
142 struct Qdisc *q = txq->qdisc;
143 int ret = NETDEV_TX_BUSY;
144 struct net_device *dev;
145 struct sk_buff *skb;
147 /* Dequeue packet */
148 if (unlikely((skb = dequeue_skb(txq, q)) == NULL))
149 return 0;
152 /* And release queue */
153 spin_unlock(&txq->lock);
155 dev = txq->dev;
157 HARD_TX_LOCK(dev, txq, smp_processor_id());
158 if (!netif_subqueue_stopped(dev, skb))
159 ret = dev_hard_start_xmit(skb, dev);
160 HARD_TX_UNLOCK(dev, txq);
162 spin_lock(&txq->lock);
163 q = txq->qdisc;
165 switch (ret) {
166 case NETDEV_TX_OK:
167 /* Driver sent out skb successfully */
168 ret = qdisc_qlen(q);
169 break;
171 case NETDEV_TX_LOCKED:
172 /* Driver try lock failed */
173 ret = handle_dev_cpu_collision(skb, txq, q);
174 break;
176 default:
177 /* Driver returned NETDEV_TX_BUSY - requeue skb */
178 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
179 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
180 dev->name, ret, q->q.qlen);
182 ret = dev_requeue_skb(skb, txq, q);
183 break;
186 return ret;
189 void __qdisc_run(struct netdev_queue *txq)
191 struct net_device *dev = txq->dev;
192 unsigned long start_time = jiffies;
194 while (qdisc_restart(txq)) {
195 if (netif_queue_stopped(dev))
196 break;
199 * Postpone processing if
200 * 1. another process needs the CPU;
201 * 2. we've been doing it for too long.
203 if (need_resched() || jiffies != start_time) {
204 netif_schedule_queue(txq);
205 break;
209 clear_bit(__QUEUE_STATE_QDISC_RUNNING, &txq->state);
212 static void dev_watchdog(unsigned long arg)
214 struct net_device *dev = (struct net_device *)arg;
215 struct netdev_queue *txq = &dev->tx_queue;
217 netif_tx_lock(dev);
218 if (txq->qdisc != &noop_qdisc) {
219 if (netif_device_present(dev) &&
220 netif_running(dev) &&
221 netif_carrier_ok(dev)) {
222 if (netif_queue_stopped(dev) &&
223 time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) {
225 printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n",
226 dev->name);
227 dev->tx_timeout(dev);
228 WARN_ON_ONCE(1);
230 if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo)))
231 dev_hold(dev);
234 netif_tx_unlock(dev);
236 dev_put(dev);
239 void __netdev_watchdog_up(struct net_device *dev)
241 if (dev->tx_timeout) {
242 if (dev->watchdog_timeo <= 0)
243 dev->watchdog_timeo = 5*HZ;
244 if (!mod_timer(&dev->watchdog_timer,
245 round_jiffies(jiffies + dev->watchdog_timeo)))
246 dev_hold(dev);
250 static void dev_watchdog_up(struct net_device *dev)
252 __netdev_watchdog_up(dev);
255 static void dev_watchdog_down(struct net_device *dev)
257 netif_tx_lock_bh(dev);
258 if (del_timer(&dev->watchdog_timer))
259 dev_put(dev);
260 netif_tx_unlock_bh(dev);
264 * netif_carrier_on - set carrier
265 * @dev: network device
267 * Device has detected that carrier.
269 void netif_carrier_on(struct net_device *dev)
271 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
272 linkwatch_fire_event(dev);
273 if (netif_running(dev))
274 __netdev_watchdog_up(dev);
277 EXPORT_SYMBOL(netif_carrier_on);
280 * netif_carrier_off - clear carrier
281 * @dev: network device
283 * Device has detected loss of carrier.
285 void netif_carrier_off(struct net_device *dev)
287 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
288 linkwatch_fire_event(dev);
290 EXPORT_SYMBOL(netif_carrier_off);
292 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
293 under all circumstances. It is difficult to invent anything faster or
294 cheaper.
297 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
299 kfree_skb(skb);
300 return NET_XMIT_CN;
303 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
305 return NULL;
308 static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
310 if (net_ratelimit())
311 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
312 skb->dev->name);
313 kfree_skb(skb);
314 return NET_XMIT_CN;
317 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
318 .id = "noop",
319 .priv_size = 0,
320 .enqueue = noop_enqueue,
321 .dequeue = noop_dequeue,
322 .requeue = noop_requeue,
323 .owner = THIS_MODULE,
326 struct Qdisc noop_qdisc = {
327 .enqueue = noop_enqueue,
328 .dequeue = noop_dequeue,
329 .flags = TCQ_F_BUILTIN,
330 .ops = &noop_qdisc_ops,
331 .list = LIST_HEAD_INIT(noop_qdisc.list),
333 EXPORT_SYMBOL(noop_qdisc);
335 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
336 .id = "noqueue",
337 .priv_size = 0,
338 .enqueue = noop_enqueue,
339 .dequeue = noop_dequeue,
340 .requeue = noop_requeue,
341 .owner = THIS_MODULE,
344 static struct Qdisc noqueue_qdisc = {
345 .enqueue = NULL,
346 .dequeue = noop_dequeue,
347 .flags = TCQ_F_BUILTIN,
348 .ops = &noqueue_qdisc_ops,
349 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
353 static const u8 prio2band[TC_PRIO_MAX+1] =
354 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
356 /* 3-band FIFO queue: old style, but should be a bit faster than
357 generic prio+fifo combination.
360 #define PFIFO_FAST_BANDS 3
362 static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
363 struct Qdisc *qdisc)
365 struct sk_buff_head *list = qdisc_priv(qdisc);
366 return list + prio2band[skb->priority & TC_PRIO_MAX];
369 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
371 struct sk_buff_head *list = prio2list(skb, qdisc);
373 if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
374 qdisc->q.qlen++;
375 return __qdisc_enqueue_tail(skb, qdisc, list);
378 return qdisc_drop(skb, qdisc);
381 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
383 int prio;
384 struct sk_buff_head *list = qdisc_priv(qdisc);
386 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
387 if (!skb_queue_empty(list + prio)) {
388 qdisc->q.qlen--;
389 return __qdisc_dequeue_head(qdisc, list + prio);
393 return NULL;
396 static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
398 qdisc->q.qlen++;
399 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
402 static void pfifo_fast_reset(struct Qdisc* qdisc)
404 int prio;
405 struct sk_buff_head *list = qdisc_priv(qdisc);
407 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
408 __qdisc_reset_queue(qdisc, list + prio);
410 qdisc->qstats.backlog = 0;
411 qdisc->q.qlen = 0;
414 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
416 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
418 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
419 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
420 return skb->len;
422 nla_put_failure:
423 return -1;
426 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
428 int prio;
429 struct sk_buff_head *list = qdisc_priv(qdisc);
431 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
432 skb_queue_head_init(list + prio);
434 return 0;
437 static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
438 .id = "pfifo_fast",
439 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
440 .enqueue = pfifo_fast_enqueue,
441 .dequeue = pfifo_fast_dequeue,
442 .requeue = pfifo_fast_requeue,
443 .init = pfifo_fast_init,
444 .reset = pfifo_fast_reset,
445 .dump = pfifo_fast_dump,
446 .owner = THIS_MODULE,
449 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
450 struct Qdisc_ops *ops)
452 void *p;
453 struct Qdisc *sch;
454 unsigned int size;
455 int err = -ENOBUFS;
457 /* ensure that the Qdisc and the private data are 32-byte aligned */
458 size = QDISC_ALIGN(sizeof(*sch));
459 size += ops->priv_size + (QDISC_ALIGNTO - 1);
461 p = kzalloc(size, GFP_KERNEL);
462 if (!p)
463 goto errout;
464 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
465 sch->padded = (char *) sch - (char *) p;
467 INIT_LIST_HEAD(&sch->list);
468 skb_queue_head_init(&sch->q);
469 sch->ops = ops;
470 sch->enqueue = ops->enqueue;
471 sch->dequeue = ops->dequeue;
472 sch->dev_queue = dev_queue;
473 dev_hold(qdisc_dev(sch));
474 atomic_set(&sch->refcnt, 1);
476 return sch;
477 errout:
478 return ERR_PTR(err);
481 struct Qdisc * qdisc_create_dflt(struct net_device *dev,
482 struct netdev_queue *dev_queue,
483 struct Qdisc_ops *ops,
484 unsigned int parentid)
486 struct Qdisc *sch;
488 sch = qdisc_alloc(dev_queue, ops);
489 if (IS_ERR(sch))
490 goto errout;
491 sch->parent = parentid;
493 if (!ops->init || ops->init(sch, NULL) == 0)
494 return sch;
496 qdisc_destroy(sch);
497 errout:
498 return NULL;
500 EXPORT_SYMBOL(qdisc_create_dflt);
502 /* Under queue->lock and BH! */
504 void qdisc_reset(struct Qdisc *qdisc)
506 const struct Qdisc_ops *ops = qdisc->ops;
508 if (ops->reset)
509 ops->reset(qdisc);
511 EXPORT_SYMBOL(qdisc_reset);
513 /* this is the rcu callback function to clean up a qdisc when there
514 * are no further references to it */
516 static void __qdisc_destroy(struct rcu_head *head)
518 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
519 kfree((char *) qdisc - qdisc->padded);
522 /* Under queue->lock and BH! */
524 void qdisc_destroy(struct Qdisc *qdisc)
526 const struct Qdisc_ops *ops = qdisc->ops;
528 if (qdisc->flags & TCQ_F_BUILTIN ||
529 !atomic_dec_and_test(&qdisc->refcnt))
530 return;
532 list_del(&qdisc->list);
533 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
534 if (ops->reset)
535 ops->reset(qdisc);
536 if (ops->destroy)
537 ops->destroy(qdisc);
539 module_put(ops->owner);
540 dev_put(qdisc_dev(qdisc));
541 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
543 EXPORT_SYMBOL(qdisc_destroy);
545 void dev_activate(struct net_device *dev)
547 struct netdev_queue *txq = &dev->tx_queue;
549 /* No queueing discipline is attached to device;
550 create default one i.e. pfifo_fast for devices,
551 which need queueing and noqueue_qdisc for
552 virtual interfaces
555 if (txq->qdisc_sleeping == &noop_qdisc) {
556 struct Qdisc *qdisc;
557 if (dev->tx_queue_len) {
558 qdisc = qdisc_create_dflt(dev, txq,
559 &pfifo_fast_ops,
560 TC_H_ROOT);
561 if (qdisc == NULL) {
562 printk(KERN_INFO "%s: activation failed\n", dev->name);
563 return;
565 list_add_tail(&qdisc->list, &txq->qdisc_list);
566 } else {
567 qdisc = &noqueue_qdisc;
569 txq->qdisc_sleeping = qdisc;
572 if (!netif_carrier_ok(dev))
573 /* Delay activation until next carrier-on event */
574 return;
576 spin_lock_bh(&txq->lock);
577 rcu_assign_pointer(txq->qdisc, txq->qdisc_sleeping);
578 if (txq->qdisc != &noqueue_qdisc) {
579 dev->trans_start = jiffies;
580 dev_watchdog_up(dev);
582 spin_unlock_bh(&txq->lock);
585 static void dev_deactivate_queue(struct netdev_queue *dev_queue,
586 struct Qdisc *qdisc_default)
588 struct Qdisc *qdisc;
589 struct sk_buff *skb;
591 spin_lock_bh(&dev_queue->lock);
593 qdisc = dev_queue->qdisc;
594 if (qdisc) {
595 dev_queue->qdisc = qdisc_default;
596 qdisc_reset(qdisc);
598 skb = dev_queue->gso_skb;
599 dev_queue->gso_skb = NULL;
601 spin_unlock_bh(&dev_queue->lock);
603 kfree_skb(skb);
606 void dev_deactivate(struct net_device *dev)
608 struct netdev_queue *dev_queue = &dev->tx_queue;
609 int running;
611 dev_deactivate_queue(dev_queue, &noop_qdisc);
613 dev_watchdog_down(dev);
615 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
616 synchronize_rcu();
618 /* Wait for outstanding qdisc_run calls. */
619 do {
620 while (test_bit(__QUEUE_STATE_QDISC_RUNNING, &dev_queue->state))
621 yield();
624 * Double-check inside queue lock to ensure that all effects
625 * of the queue run are visible when we return.
627 spin_lock_bh(&dev_queue->lock);
628 running = test_bit(__QUEUE_STATE_QDISC_RUNNING,
629 &dev_queue->state);
630 spin_unlock_bh(&dev_queue->lock);
633 * The running flag should never be set at this point because
634 * we've already set dev->qdisc to noop_qdisc *inside* the same
635 * pair of spin locks. That is, if any qdisc_run starts after
636 * our initial test it should see the noop_qdisc and then
637 * clear the RUNNING bit before dropping the queue lock. So
638 * if it is set here then we've found a bug.
640 } while (WARN_ON_ONCE(running));
643 static void dev_init_scheduler_queue(struct net_device *dev,
644 struct netdev_queue *dev_queue,
645 struct Qdisc *qdisc)
647 dev_queue->qdisc = qdisc;
648 dev_queue->qdisc_sleeping = qdisc;
649 INIT_LIST_HEAD(&dev_queue->qdisc_list);
652 void dev_init_scheduler(struct net_device *dev)
654 qdisc_lock_tree(dev);
655 dev_init_scheduler_queue(dev, &dev->tx_queue, &noop_qdisc);
656 dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
657 qdisc_unlock_tree(dev);
659 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
662 static void dev_shutdown_scheduler_queue(struct net_device *dev,
663 struct netdev_queue *dev_queue,
664 struct Qdisc *qdisc_default)
666 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
668 if (qdisc) {
669 dev_queue->qdisc = qdisc_default;
670 dev_queue->qdisc_sleeping = qdisc_default;
672 qdisc_destroy(qdisc);
676 void dev_shutdown(struct net_device *dev)
678 qdisc_lock_tree(dev);
679 dev_shutdown_scheduler_queue(dev, &dev->tx_queue, &noop_qdisc);
680 dev_shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
681 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
682 qdisc_unlock_tree(dev);