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 * qdisc_lock(qdisc) spinlock.
34 * The idea is the following:
35 * - enqueue, dequeue are serialized via qdisc root lock
36 * - ingress filtering is also serialized via qdisc root lock
37 * - updates to tree and tree walking are only done under the rtnl mutex.
40 static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
44 q->q.qlen++; /* it's still part of the queue */
50 static inline struct sk_buff *dequeue_skb(struct Qdisc *q)
52 struct sk_buff *skb = q->gso_skb;
55 struct net_device *dev = qdisc_dev(q);
56 struct netdev_queue *txq;
58 /* check the reason of requeuing without tx lock first */
59 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
60 if (!netif_tx_queue_stopped(txq) &&
61 !netif_tx_queue_frozen(txq)) {
73 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
74 struct netdev_queue *dev_queue,
79 if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
81 * Same CPU holding the lock. It may be a transient
82 * configuration error, when hard_start_xmit() recurses. We
83 * detect it by checking xmit owner and drop the packet when
84 * deadloop is detected. Return OK to try the next skb.
88 printk(KERN_WARNING "Dead loop on netdevice %s, "
89 "fix it urgently!\n", dev_queue->dev->name);
93 * Another cpu is holding lock, requeue & delay xmits for
96 __get_cpu_var(netdev_rx_stat).cpu_collision++;
97 ret = dev_requeue_skb(skb, q);
104 * Transmit one skb, and handle the return status as required. Holding the
105 * __QDISC_STATE_RUNNING bit guarantees that only one CPU can execute this
108 * Returns to the caller:
109 * 0 - queue is empty or throttled.
110 * >0 - queue is not empty.
112 int sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
113 struct net_device *dev, struct netdev_queue *txq,
114 spinlock_t *root_lock)
116 int ret = NETDEV_TX_BUSY;
118 /* And release qdisc */
119 spin_unlock(root_lock);
121 HARD_TX_LOCK(dev, txq, smp_processor_id());
122 if (!netif_tx_queue_stopped(txq) &&
123 !netif_tx_queue_frozen(txq)) {
124 ret = dev_hard_start_xmit(skb, dev, txq);
126 /* an error implies that the skb was consumed */
129 /* all NET_XMIT codes map to NETDEV_TX_OK */
130 ret &= ~NET_XMIT_MASK;
132 HARD_TX_UNLOCK(dev, txq);
134 spin_lock(root_lock);
138 /* Driver sent out skb successfully */
142 case NETDEV_TX_LOCKED:
143 /* Driver try lock failed */
144 ret = handle_dev_cpu_collision(skb, txq, q);
148 /* Driver returned NETDEV_TX_BUSY - requeue skb */
149 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
150 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
151 dev->name, ret, q->q.qlen);
153 ret = dev_requeue_skb(skb, q);
157 if (ret && (netif_tx_queue_stopped(txq) ||
158 netif_tx_queue_frozen(txq)))
165 * NOTE: Called under qdisc_lock(q) with locally disabled BH.
167 * __QDISC_STATE_RUNNING guarantees only one CPU can process
168 * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
171 * netif_tx_lock serializes accesses to device driver.
173 * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
174 * if one is grabbed, another must be free.
176 * Note, that this procedure can be called by a watchdog timer
178 * Returns to the caller:
179 * 0 - queue is empty or throttled.
180 * >0 - queue is not empty.
183 static inline int qdisc_restart(struct Qdisc *q)
185 struct netdev_queue *txq;
186 struct net_device *dev;
187 spinlock_t *root_lock;
191 skb = dequeue_skb(q);
195 root_lock = qdisc_lock(q);
197 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
199 return sch_direct_xmit(skb, q, dev, txq, root_lock);
202 void __qdisc_run(struct Qdisc *q)
204 unsigned long start_time = jiffies;
206 while (qdisc_restart(q)) {
208 * Postpone processing if
209 * 1. another process needs the CPU;
210 * 2. we've been doing it for too long.
212 if (need_resched() || jiffies != start_time) {
218 clear_bit(__QDISC_STATE_RUNNING, &q->state);
221 unsigned long dev_trans_start(struct net_device *dev)
223 unsigned long val, res = dev->trans_start;
226 for (i = 0; i < dev->num_tx_queues; i++) {
227 val = netdev_get_tx_queue(dev, i)->trans_start;
228 if (val && time_after(val, res))
231 dev->trans_start = res;
234 EXPORT_SYMBOL(dev_trans_start);
236 static void dev_watchdog(unsigned long arg)
238 struct net_device *dev = (struct net_device *)arg;
241 if (!qdisc_tx_is_noop(dev)) {
242 if (netif_device_present(dev) &&
243 netif_running(dev) &&
244 netif_carrier_ok(dev)) {
245 int some_queue_timedout = 0;
247 unsigned long trans_start;
249 for (i = 0; i < dev->num_tx_queues; i++) {
250 struct netdev_queue *txq;
252 txq = netdev_get_tx_queue(dev, i);
254 * old device drivers set dev->trans_start
256 trans_start = txq->trans_start ? : dev->trans_start;
257 if (netif_tx_queue_stopped(txq) &&
258 time_after(jiffies, (trans_start +
259 dev->watchdog_timeo))) {
260 some_queue_timedout = 1;
265 if (some_queue_timedout) {
267 WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
268 dev->name, netdev_drivername(dev, drivername, 64), i);
269 dev->netdev_ops->ndo_tx_timeout(dev);
271 if (!mod_timer(&dev->watchdog_timer,
272 round_jiffies(jiffies +
273 dev->watchdog_timeo)))
277 netif_tx_unlock(dev);
282 void __netdev_watchdog_up(struct net_device *dev)
284 if (dev->netdev_ops->ndo_tx_timeout) {
285 if (dev->watchdog_timeo <= 0)
286 dev->watchdog_timeo = 5*HZ;
287 if (!mod_timer(&dev->watchdog_timer,
288 round_jiffies(jiffies + dev->watchdog_timeo)))
293 static void dev_watchdog_up(struct net_device *dev)
295 __netdev_watchdog_up(dev);
298 static void dev_watchdog_down(struct net_device *dev)
300 netif_tx_lock_bh(dev);
301 if (del_timer(&dev->watchdog_timer))
303 netif_tx_unlock_bh(dev);
307 * netif_carrier_on - set carrier
308 * @dev: network device
310 * Device has detected that carrier.
312 void netif_carrier_on(struct net_device *dev)
314 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
315 if (dev->reg_state == NETREG_UNINITIALIZED)
317 linkwatch_fire_event(dev);
318 if (netif_running(dev))
319 __netdev_watchdog_up(dev);
322 EXPORT_SYMBOL(netif_carrier_on);
325 * netif_carrier_off - clear carrier
326 * @dev: network device
328 * Device has detected loss of carrier.
330 void netif_carrier_off(struct net_device *dev)
332 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
333 if (dev->reg_state == NETREG_UNINITIALIZED)
335 linkwatch_fire_event(dev);
338 EXPORT_SYMBOL(netif_carrier_off);
340 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
341 under all circumstances. It is difficult to invent anything faster or
345 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
351 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
356 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
359 .enqueue = noop_enqueue,
360 .dequeue = noop_dequeue,
361 .peek = noop_dequeue,
362 .owner = THIS_MODULE,
365 static struct netdev_queue noop_netdev_queue = {
366 .qdisc = &noop_qdisc,
367 .qdisc_sleeping = &noop_qdisc,
370 struct Qdisc noop_qdisc = {
371 .enqueue = noop_enqueue,
372 .dequeue = noop_dequeue,
373 .flags = TCQ_F_BUILTIN,
374 .ops = &noop_qdisc_ops,
375 .list = LIST_HEAD_INIT(noop_qdisc.list),
376 .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
377 .dev_queue = &noop_netdev_queue,
379 EXPORT_SYMBOL(noop_qdisc);
381 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
384 .enqueue = noop_enqueue,
385 .dequeue = noop_dequeue,
386 .peek = noop_dequeue,
387 .owner = THIS_MODULE,
390 static struct Qdisc noqueue_qdisc;
391 static struct netdev_queue noqueue_netdev_queue = {
392 .qdisc = &noqueue_qdisc,
393 .qdisc_sleeping = &noqueue_qdisc,
396 static struct Qdisc noqueue_qdisc = {
398 .dequeue = noop_dequeue,
399 .flags = TCQ_F_BUILTIN,
400 .ops = &noqueue_qdisc_ops,
401 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
402 .q.lock = __SPIN_LOCK_UNLOCKED(noqueue_qdisc.q.lock),
403 .dev_queue = &noqueue_netdev_queue,
407 static const u8 prio2band[TC_PRIO_MAX+1] =
408 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
410 /* 3-band FIFO queue: old style, but should be a bit faster than
411 generic prio+fifo combination.
414 #define PFIFO_FAST_BANDS 3
417 * Private data for a pfifo_fast scheduler containing:
418 * - queues for the three band
419 * - bitmap indicating which of the bands contain skbs
421 struct pfifo_fast_priv {
423 struct sk_buff_head q[PFIFO_FAST_BANDS];
427 * Convert a bitmap to the first band number where an skb is queued, where:
428 * bitmap=0 means there are no skbs on any band.
429 * bitmap=1 means there is an skb on band 0.
430 * bitmap=7 means there are skbs on all 3 bands, etc.
432 static const int bitmap2band[] = {-1, 0, 1, 0, 2, 0, 1, 0};
434 static inline struct sk_buff_head *band2list(struct pfifo_fast_priv *priv,
437 return priv->q + band;
440 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
442 if (skb_queue_len(&qdisc->q) < qdisc_dev(qdisc)->tx_queue_len) {
443 int band = prio2band[skb->priority & TC_PRIO_MAX];
444 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
445 struct sk_buff_head *list = band2list(priv, band);
447 priv->bitmap |= (1 << band);
449 return __qdisc_enqueue_tail(skb, qdisc, list);
452 return qdisc_drop(skb, qdisc);
455 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
457 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
458 int band = bitmap2band[priv->bitmap];
460 if (likely(band >= 0)) {
461 struct sk_buff_head *list = band2list(priv, band);
462 struct sk_buff *skb = __qdisc_dequeue_head(qdisc, list);
465 if (skb_queue_empty(list))
466 priv->bitmap &= ~(1 << band);
474 static struct sk_buff *pfifo_fast_peek(struct Qdisc* qdisc)
476 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
477 int band = bitmap2band[priv->bitmap];
480 struct sk_buff_head *list = band2list(priv, band);
482 return skb_peek(list);
488 static void pfifo_fast_reset(struct Qdisc* qdisc)
491 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
493 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
494 __qdisc_reset_queue(qdisc, band2list(priv, prio));
497 qdisc->qstats.backlog = 0;
501 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
503 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
505 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
506 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
513 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
516 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
518 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
519 skb_queue_head_init(band2list(priv, prio));
524 struct Qdisc_ops pfifo_fast_ops __read_mostly = {
526 .priv_size = sizeof(struct pfifo_fast_priv),
527 .enqueue = pfifo_fast_enqueue,
528 .dequeue = pfifo_fast_dequeue,
529 .peek = pfifo_fast_peek,
530 .init = pfifo_fast_init,
531 .reset = pfifo_fast_reset,
532 .dump = pfifo_fast_dump,
533 .owner = THIS_MODULE,
536 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
537 struct Qdisc_ops *ops)
544 /* ensure that the Qdisc and the private data are 32-byte aligned */
545 size = QDISC_ALIGN(sizeof(*sch));
546 size += ops->priv_size + (QDISC_ALIGNTO - 1);
548 p = kzalloc(size, GFP_KERNEL);
551 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
552 sch->padded = (char *) sch - (char *) p;
554 INIT_LIST_HEAD(&sch->list);
555 skb_queue_head_init(&sch->q);
557 sch->enqueue = ops->enqueue;
558 sch->dequeue = ops->dequeue;
559 sch->dev_queue = dev_queue;
560 dev_hold(qdisc_dev(sch));
561 atomic_set(&sch->refcnt, 1);
568 struct Qdisc * qdisc_create_dflt(struct net_device *dev,
569 struct netdev_queue *dev_queue,
570 struct Qdisc_ops *ops,
571 unsigned int parentid)
575 sch = qdisc_alloc(dev_queue, ops);
578 sch->parent = parentid;
580 if (!ops->init || ops->init(sch, NULL) == 0)
587 EXPORT_SYMBOL(qdisc_create_dflt);
589 /* Under qdisc_lock(qdisc) and BH! */
591 void qdisc_reset(struct Qdisc *qdisc)
593 const struct Qdisc_ops *ops = qdisc->ops;
598 if (qdisc->gso_skb) {
599 kfree_skb(qdisc->gso_skb);
600 qdisc->gso_skb = NULL;
604 EXPORT_SYMBOL(qdisc_reset);
606 void qdisc_destroy(struct Qdisc *qdisc)
608 const struct Qdisc_ops *ops = qdisc->ops;
610 if (qdisc->flags & TCQ_F_BUILTIN ||
611 !atomic_dec_and_test(&qdisc->refcnt))
614 #ifdef CONFIG_NET_SCHED
615 qdisc_list_del(qdisc);
617 qdisc_put_stab(qdisc->stab);
619 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
625 module_put(ops->owner);
626 dev_put(qdisc_dev(qdisc));
628 kfree_skb(qdisc->gso_skb);
629 kfree((char *) qdisc - qdisc->padded);
631 EXPORT_SYMBOL(qdisc_destroy);
633 /* Attach toplevel qdisc to device queue. */
634 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
637 struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
638 spinlock_t *root_lock;
640 root_lock = qdisc_lock(oqdisc);
641 spin_lock_bh(root_lock);
643 /* Prune old scheduler */
644 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
647 /* ... and graft new one */
650 dev_queue->qdisc_sleeping = qdisc;
651 rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
653 spin_unlock_bh(root_lock);
658 static void attach_one_default_qdisc(struct net_device *dev,
659 struct netdev_queue *dev_queue,
664 if (dev->tx_queue_len) {
665 qdisc = qdisc_create_dflt(dev, dev_queue,
666 &pfifo_fast_ops, TC_H_ROOT);
668 printk(KERN_INFO "%s: activation failed\n", dev->name);
672 /* Can by-pass the queue discipline for default qdisc */
673 qdisc->flags |= TCQ_F_CAN_BYPASS;
675 qdisc = &noqueue_qdisc;
677 dev_queue->qdisc_sleeping = qdisc;
680 static void attach_default_qdiscs(struct net_device *dev)
682 struct netdev_queue *txq;
685 txq = netdev_get_tx_queue(dev, 0);
687 if (!netif_is_multiqueue(dev) || dev->tx_queue_len == 0) {
688 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
689 dev->qdisc = txq->qdisc_sleeping;
690 atomic_inc(&dev->qdisc->refcnt);
692 qdisc = qdisc_create_dflt(dev, txq, &mq_qdisc_ops, TC_H_ROOT);
694 qdisc->ops->attach(qdisc);
700 static void transition_one_qdisc(struct net_device *dev,
701 struct netdev_queue *dev_queue,
702 void *_need_watchdog)
704 struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
705 int *need_watchdog_p = _need_watchdog;
707 if (!(new_qdisc->flags & TCQ_F_BUILTIN))
708 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
710 rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
711 if (need_watchdog_p && new_qdisc != &noqueue_qdisc) {
712 dev_queue->trans_start = 0;
713 *need_watchdog_p = 1;
717 void dev_activate(struct net_device *dev)
721 /* No queueing discipline is attached to device;
722 create default one i.e. pfifo_fast for devices,
723 which need queueing and noqueue_qdisc for
727 if (dev->qdisc == &noop_qdisc)
728 attach_default_qdiscs(dev);
730 if (!netif_carrier_ok(dev))
731 /* Delay activation until next carrier-on event */
735 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
736 transition_one_qdisc(dev, &dev->rx_queue, NULL);
739 dev->trans_start = jiffies;
740 dev_watchdog_up(dev);
744 static void dev_deactivate_queue(struct net_device *dev,
745 struct netdev_queue *dev_queue,
746 void *_qdisc_default)
748 struct Qdisc *qdisc_default = _qdisc_default;
751 qdisc = dev_queue->qdisc;
753 spin_lock_bh(qdisc_lock(qdisc));
755 if (!(qdisc->flags & TCQ_F_BUILTIN))
756 set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
758 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
761 spin_unlock_bh(qdisc_lock(qdisc));
765 static bool some_qdisc_is_busy(struct net_device *dev)
769 for (i = 0; i < dev->num_tx_queues; i++) {
770 struct netdev_queue *dev_queue;
771 spinlock_t *root_lock;
775 dev_queue = netdev_get_tx_queue(dev, i);
776 q = dev_queue->qdisc_sleeping;
777 root_lock = qdisc_lock(q);
779 spin_lock_bh(root_lock);
781 val = (test_bit(__QDISC_STATE_RUNNING, &q->state) ||
782 test_bit(__QDISC_STATE_SCHED, &q->state));
784 spin_unlock_bh(root_lock);
792 void dev_deactivate(struct net_device *dev)
794 netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
795 dev_deactivate_queue(dev, &dev->rx_queue, &noop_qdisc);
797 dev_watchdog_down(dev);
799 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
802 /* Wait for outstanding qdisc_run calls. */
803 while (some_qdisc_is_busy(dev))
807 static void dev_init_scheduler_queue(struct net_device *dev,
808 struct netdev_queue *dev_queue,
811 struct Qdisc *qdisc = _qdisc;
813 dev_queue->qdisc = qdisc;
814 dev_queue->qdisc_sleeping = qdisc;
817 void dev_init_scheduler(struct net_device *dev)
819 dev->qdisc = &noop_qdisc;
820 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
821 dev_init_scheduler_queue(dev, &dev->rx_queue, &noop_qdisc);
823 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
826 static void shutdown_scheduler_queue(struct net_device *dev,
827 struct netdev_queue *dev_queue,
828 void *_qdisc_default)
830 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
831 struct Qdisc *qdisc_default = _qdisc_default;
834 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
835 dev_queue->qdisc_sleeping = qdisc_default;
837 qdisc_destroy(qdisc);
841 void dev_shutdown(struct net_device *dev)
843 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
844 shutdown_scheduler_queue(dev, &dev->rx_queue, &noop_qdisc);
845 qdisc_destroy(dev->qdisc);
846 dev->qdisc = &noop_qdisc;
848 WARN_ON(timer_pending(&dev->watchdog_timer));