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 <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)
65 static inline int dev_requeue_skb(struct sk_buff *skb,
66 struct netdev_queue *dev_queue,
69 if (unlikely(skb->next))
70 dev_queue->gso_skb = skb;
72 q->ops->requeue(skb, q);
74 netif_schedule_queue(dev_queue);
78 static inline struct sk_buff *dequeue_skb(struct netdev_queue *dev_queue,
83 if ((skb = dev_queue->gso_skb))
84 dev_queue->gso_skb = NULL;
91 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
92 struct netdev_queue *dev_queue,
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.
106 printk(KERN_WARNING "Dead loop on netdevice %s, "
107 "fix it urgently!\n", dev_queue->dev->name);
111 * Another cpu is holding lock, requeue & delay xmits for
114 __get_cpu_var(netdev_rx_stat).cpu_collision++;
115 ret = dev_requeue_skb(skb, dev_queue, q);
122 * NOTE: Called under queue->lock with locally disabled BH.
124 * __LINK_STATE_QDISC_RUNNING guarantees only one CPU can process this
125 * device at a time. queue->lock serializes queue accesses for
126 * this device 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;
148 if (unlikely((skb = dequeue_skb(txq, q)) == NULL))
152 /* And release queue */
153 spin_unlock(&txq->lock);
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);
167 /* Driver sent out skb successfully */
171 case NETDEV_TX_LOCKED:
172 /* Driver try lock failed */
173 ret = handle_dev_cpu_collision(skb, txq, q);
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);
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))
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);
209 clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->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;
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",
227 dev->tx_timeout(dev);
230 if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo)))
234 netif_tx_unlock(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)))
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))
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
297 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
303 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
308 static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
311 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
317 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
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 = {
338 .enqueue = noop_enqueue,
339 .dequeue = noop_dequeue,
340 .requeue = noop_requeue,
341 .owner = THIS_MODULE,
344 static struct Qdisc noqueue_qdisc = {
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,
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) {
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)
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)) {
389 return __qdisc_dequeue_head(qdisc, list + prio);
396 static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
399 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
402 static void pfifo_fast_reset(struct Qdisc* qdisc)
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;
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);
426 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
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);
437 static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
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)
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);
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);
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);
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)
488 sch = qdisc_alloc(dev_queue, ops);
491 sch->parent = parentid;
493 if (!ops->init || ops->init(sch, NULL) == 0)
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;
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))
532 list_del(&qdisc->list);
533 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
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
555 if (txq->qdisc_sleeping == &noop_qdisc) {
557 if (dev->tx_queue_len) {
558 qdisc = qdisc_create_dflt(dev, txq,
562 printk(KERN_INFO "%s: activation failed\n", dev->name);
565 list_add_tail(&qdisc->list, &txq->qdisc_list);
567 qdisc = &noqueue_qdisc;
569 txq->qdisc_sleeping = qdisc;
572 if (!netif_carrier_ok(dev))
573 /* Delay activation until next carrier-on event */
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)
591 spin_lock_bh(&dev_queue->lock);
593 qdisc = dev_queue->qdisc;
595 dev_queue->qdisc = qdisc_default;
598 skb = dev_queue->gso_skb;
599 dev_queue->gso_skb = NULL;
601 spin_unlock_bh(&dev_queue->lock);
606 void dev_deactivate(struct net_device *dev)
610 dev_deactivate_queue(&dev->tx_queue, &noop_qdisc);
612 dev_watchdog_down(dev);
614 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
617 /* Wait for outstanding qdisc_run calls. */
619 while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
623 * Double-check inside queue lock to ensure that all effects
624 * of the queue run are visible when we return.
626 spin_lock_bh(&dev->tx_queue.lock);
627 running = test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
628 spin_unlock_bh(&dev->tx_queue.lock);
631 * The running flag should never be set at this point because
632 * we've already set dev->qdisc to noop_qdisc *inside* the same
633 * pair of spin locks. That is, if any qdisc_run starts after
634 * our initial test it should see the noop_qdisc and then
635 * clear the RUNNING bit before dropping the queue lock. So
636 * if it is set here then we've found a bug.
638 } while (WARN_ON_ONCE(running));
641 static void dev_init_scheduler_queue(struct net_device *dev,
642 struct netdev_queue *dev_queue,
645 dev_queue->qdisc = qdisc;
646 dev_queue->qdisc_sleeping = qdisc;
647 INIT_LIST_HEAD(&dev_queue->qdisc_list);
650 void dev_init_scheduler(struct net_device *dev)
652 qdisc_lock_tree(dev);
653 dev_init_scheduler_queue(dev, &dev->tx_queue, &noop_qdisc);
654 dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
655 qdisc_unlock_tree(dev);
657 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
660 static void dev_shutdown_scheduler_queue(struct net_device *dev,
661 struct netdev_queue *dev_queue,
662 struct Qdisc *qdisc_default)
664 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
667 dev_queue->qdisc = qdisc_default;
668 dev_queue->qdisc_sleeping = qdisc_default;
670 qdisc_destroy(qdisc);
674 void dev_shutdown(struct net_device *dev)
676 qdisc_lock_tree(dev);
677 dev_shutdown_scheduler_queue(dev, &dev->tx_queue, &noop_qdisc);
678 dev_shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
679 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
680 qdisc_unlock_tree(dev);