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, struct net_device *dev,
68 if (unlikely(skb->next))
71 q->ops->requeue(skb, q);
77 static inline struct sk_buff *dev_dequeue_skb(struct net_device *dev,
82 if ((skb = dev->gso_skb))
90 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
91 struct net_device *dev,
96 if (unlikely(dev->xmit_lock_owner == smp_processor_id())) {
98 * Same CPU holding the lock. It may be a transient
99 * configuration error, when hard_start_xmit() recurses. We
100 * detect it by checking xmit owner and drop the packet when
101 * deadloop is detected. Return OK to try the next skb.
105 printk(KERN_WARNING "Dead loop on netdevice %s, "
106 "fix it urgently!\n", dev->name);
110 * Another cpu is holding lock, requeue & delay xmits for
113 __get_cpu_var(netdev_rx_stat).cpu_collision++;
114 ret = dev_requeue_skb(skb, dev, q);
121 * NOTE: Called under queue->lock with locally disabled BH.
123 * __LINK_STATE_QDISC_RUNNING guarantees only one CPU can process this
124 * device at a time. queue->lock serializes queue accesses for
125 * this device AND txq->qdisc pointer itself.
127 * netif_tx_lock serializes accesses to device driver.
129 * queue->lock and netif_tx_lock are mutually exclusive,
130 * if one is grabbed, another must be free.
132 * Note, that this procedure can be called by a watchdog timer
134 * Returns to the caller:
135 * 0 - queue is empty or throttled.
136 * >0 - queue is not empty.
139 static inline int qdisc_restart(struct net_device *dev)
141 struct netdev_queue *txq = &dev->tx_queue;
142 struct Qdisc *q = txq->qdisc;
144 int ret = NETDEV_TX_BUSY;
147 if (unlikely((skb = dev_dequeue_skb(dev, q)) == NULL))
151 /* And release queue */
152 spin_unlock(&txq->lock);
154 HARD_TX_LOCK(dev, smp_processor_id());
155 if (!netif_subqueue_stopped(dev, skb))
156 ret = dev_hard_start_xmit(skb, dev);
159 spin_lock(&txq->lock);
164 /* Driver sent out skb successfully */
168 case NETDEV_TX_LOCKED:
169 /* Driver try lock failed */
170 ret = handle_dev_cpu_collision(skb, dev, q);
174 /* Driver returned NETDEV_TX_BUSY - requeue skb */
175 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
176 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
177 dev->name, ret, q->q.qlen);
179 ret = dev_requeue_skb(skb, dev, q);
186 void __qdisc_run(struct net_device *dev)
188 unsigned long start_time = jiffies;
190 while (qdisc_restart(dev)) {
191 if (netif_queue_stopped(dev))
195 * Postpone processing if
196 * 1. another process needs the CPU;
197 * 2. we've been doing it for too long.
199 if (need_resched() || jiffies != start_time) {
205 clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
208 static void dev_watchdog(unsigned long arg)
210 struct net_device *dev = (struct net_device *)arg;
211 struct netdev_queue *txq = &dev->tx_queue;
214 if (txq->qdisc != &noop_qdisc) {
215 if (netif_device_present(dev) &&
216 netif_running(dev) &&
217 netif_carrier_ok(dev)) {
218 if (netif_queue_stopped(dev) &&
219 time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) {
221 printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n",
223 dev->tx_timeout(dev);
226 if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo)))
230 netif_tx_unlock(dev);
235 void __netdev_watchdog_up(struct net_device *dev)
237 if (dev->tx_timeout) {
238 if (dev->watchdog_timeo <= 0)
239 dev->watchdog_timeo = 5*HZ;
240 if (!mod_timer(&dev->watchdog_timer,
241 round_jiffies(jiffies + dev->watchdog_timeo)))
246 static void dev_watchdog_up(struct net_device *dev)
248 __netdev_watchdog_up(dev);
251 static void dev_watchdog_down(struct net_device *dev)
253 netif_tx_lock_bh(dev);
254 if (del_timer(&dev->watchdog_timer))
256 netif_tx_unlock_bh(dev);
260 * netif_carrier_on - set carrier
261 * @dev: network device
263 * Device has detected that carrier.
265 void netif_carrier_on(struct net_device *dev)
267 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
268 linkwatch_fire_event(dev);
269 if (netif_running(dev))
270 __netdev_watchdog_up(dev);
273 EXPORT_SYMBOL(netif_carrier_on);
276 * netif_carrier_off - clear carrier
277 * @dev: network device
279 * Device has detected loss of carrier.
281 void netif_carrier_off(struct net_device *dev)
283 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
284 linkwatch_fire_event(dev);
286 EXPORT_SYMBOL(netif_carrier_off);
288 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
289 under all circumstances. It is difficult to invent anything faster or
293 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
299 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
304 static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
307 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
313 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
316 .enqueue = noop_enqueue,
317 .dequeue = noop_dequeue,
318 .requeue = noop_requeue,
319 .owner = THIS_MODULE,
322 struct Qdisc noop_qdisc = {
323 .enqueue = noop_enqueue,
324 .dequeue = noop_dequeue,
325 .flags = TCQ_F_BUILTIN,
326 .ops = &noop_qdisc_ops,
327 .list = LIST_HEAD_INIT(noop_qdisc.list),
329 EXPORT_SYMBOL(noop_qdisc);
331 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
334 .enqueue = noop_enqueue,
335 .dequeue = noop_dequeue,
336 .requeue = noop_requeue,
337 .owner = THIS_MODULE,
340 static struct Qdisc noqueue_qdisc = {
342 .dequeue = noop_dequeue,
343 .flags = TCQ_F_BUILTIN,
344 .ops = &noqueue_qdisc_ops,
345 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
349 static const u8 prio2band[TC_PRIO_MAX+1] =
350 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
352 /* 3-band FIFO queue: old style, but should be a bit faster than
353 generic prio+fifo combination.
356 #define PFIFO_FAST_BANDS 3
358 static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
361 struct sk_buff_head *list = qdisc_priv(qdisc);
362 return list + prio2band[skb->priority & TC_PRIO_MAX];
365 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
367 struct sk_buff_head *list = prio2list(skb, qdisc);
369 if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
371 return __qdisc_enqueue_tail(skb, qdisc, list);
374 return qdisc_drop(skb, qdisc);
377 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
380 struct sk_buff_head *list = qdisc_priv(qdisc);
382 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
383 if (!skb_queue_empty(list + prio)) {
385 return __qdisc_dequeue_head(qdisc, list + prio);
392 static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
395 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
398 static void pfifo_fast_reset(struct Qdisc* qdisc)
401 struct sk_buff_head *list = qdisc_priv(qdisc);
403 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
404 __qdisc_reset_queue(qdisc, list + prio);
406 qdisc->qstats.backlog = 0;
410 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
412 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
414 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
415 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
422 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
425 struct sk_buff_head *list = qdisc_priv(qdisc);
427 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
428 skb_queue_head_init(list + prio);
433 static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
435 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
436 .enqueue = pfifo_fast_enqueue,
437 .dequeue = pfifo_fast_dequeue,
438 .requeue = pfifo_fast_requeue,
439 .init = pfifo_fast_init,
440 .reset = pfifo_fast_reset,
441 .dump = pfifo_fast_dump,
442 .owner = THIS_MODULE,
445 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
446 struct Qdisc_ops *ops)
453 /* ensure that the Qdisc and the private data are 32-byte aligned */
454 size = QDISC_ALIGN(sizeof(*sch));
455 size += ops->priv_size + (QDISC_ALIGNTO - 1);
457 p = kzalloc(size, GFP_KERNEL);
460 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
461 sch->padded = (char *) sch - (char *) p;
463 INIT_LIST_HEAD(&sch->list);
464 skb_queue_head_init(&sch->q);
466 sch->enqueue = ops->enqueue;
467 sch->dequeue = ops->dequeue;
468 sch->dev_queue = dev_queue;
469 dev_hold(qdisc_dev(sch));
470 atomic_set(&sch->refcnt, 1);
477 struct Qdisc * qdisc_create_dflt(struct net_device *dev,
478 struct netdev_queue *dev_queue,
479 struct Qdisc_ops *ops,
480 unsigned int parentid)
484 sch = qdisc_alloc(dev_queue, ops);
487 sch->stats_lock = &dev_queue->lock;
488 sch->parent = parentid;
490 if (!ops->init || ops->init(sch, NULL) == 0)
497 EXPORT_SYMBOL(qdisc_create_dflt);
499 /* Under queue->lock and BH! */
501 void qdisc_reset(struct Qdisc *qdisc)
503 const struct Qdisc_ops *ops = qdisc->ops;
508 EXPORT_SYMBOL(qdisc_reset);
510 /* this is the rcu callback function to clean up a qdisc when there
511 * are no further references to it */
513 static void __qdisc_destroy(struct rcu_head *head)
515 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
516 kfree((char *) qdisc - qdisc->padded);
519 /* Under queue->lock and BH! */
521 void qdisc_destroy(struct Qdisc *qdisc)
523 const struct Qdisc_ops *ops = qdisc->ops;
525 if (qdisc->flags & TCQ_F_BUILTIN ||
526 !atomic_dec_and_test(&qdisc->refcnt))
529 list_del(&qdisc->list);
530 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
536 module_put(ops->owner);
537 dev_put(qdisc_dev(qdisc));
538 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
540 EXPORT_SYMBOL(qdisc_destroy);
542 void dev_activate(struct net_device *dev)
544 struct netdev_queue *txq = &dev->tx_queue;
546 /* No queueing discipline is attached to device;
547 create default one i.e. pfifo_fast for devices,
548 which need queueing and noqueue_qdisc for
552 if (txq->qdisc_sleeping == &noop_qdisc) {
554 if (dev->tx_queue_len) {
555 qdisc = qdisc_create_dflt(dev, txq,
559 printk(KERN_INFO "%s: activation failed\n", dev->name);
562 list_add_tail(&qdisc->list, &txq->qdisc_list);
564 qdisc = &noqueue_qdisc;
566 txq->qdisc_sleeping = qdisc;
569 if (!netif_carrier_ok(dev))
570 /* Delay activation until next carrier-on event */
573 spin_lock_bh(&txq->lock);
574 rcu_assign_pointer(txq->qdisc, txq->qdisc_sleeping);
575 if (txq->qdisc != &noqueue_qdisc) {
576 dev->trans_start = jiffies;
577 dev_watchdog_up(dev);
579 spin_unlock_bh(&txq->lock);
582 static void dev_deactivate_queue(struct net_device *dev,
583 struct netdev_queue *dev_queue,
584 struct Qdisc *qdisc_default)
586 struct Qdisc *qdisc = dev_queue->qdisc;
589 dev_queue->qdisc = qdisc_default;
594 void dev_deactivate(struct net_device *dev)
599 spin_lock_bh(&dev->tx_queue.lock);
600 dev_deactivate_queue(dev, &dev->tx_queue, &noop_qdisc);
604 spin_unlock_bh(&dev->tx_queue.lock);
608 dev_watchdog_down(dev);
610 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
613 /* Wait for outstanding qdisc_run calls. */
615 while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
619 * Double-check inside queue lock to ensure that all effects
620 * of the queue run are visible when we return.
622 spin_lock_bh(&dev->tx_queue.lock);
623 running = test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
624 spin_unlock_bh(&dev->tx_queue.lock);
627 * The running flag should never be set at this point because
628 * we've already set dev->qdisc to noop_qdisc *inside* the same
629 * pair of spin locks. That is, if any qdisc_run starts after
630 * our initial test it should see the noop_qdisc and then
631 * clear the RUNNING bit before dropping the queue lock. So
632 * if it is set here then we've found a bug.
634 } while (WARN_ON_ONCE(running));
637 static void dev_init_scheduler_queue(struct net_device *dev,
638 struct netdev_queue *dev_queue,
641 dev_queue->qdisc = qdisc;
642 dev_queue->qdisc_sleeping = qdisc;
643 INIT_LIST_HEAD(&dev_queue->qdisc_list);
646 void dev_init_scheduler(struct net_device *dev)
648 qdisc_lock_tree(dev);
649 dev_init_scheduler_queue(dev, &dev->tx_queue, &noop_qdisc);
650 dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
651 qdisc_unlock_tree(dev);
653 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
656 static void dev_shutdown_scheduler_queue(struct net_device *dev,
657 struct netdev_queue *dev_queue,
658 struct Qdisc *qdisc_default)
660 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
663 dev_queue->qdisc = qdisc_default;
664 dev_queue->qdisc_sleeping = qdisc_default;
666 qdisc_destroy(qdisc);
670 void dev_shutdown(struct net_device *dev)
672 qdisc_lock_tree(dev);
673 dev_shutdown_scheduler_queue(dev, &dev->tx_queue, &noop_qdisc);
674 dev_shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
675 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
676 qdisc_unlock_tree(dev);