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->rx_queue.lock)
48 for (i = 0; i < dev->num_tx_queues; i++) {
49 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
50 spin_lock(&txq->lock);
52 spin_lock(&dev->rx_queue.lock);
54 EXPORT_SYMBOL(qdisc_lock_tree);
56 void qdisc_unlock_tree(struct net_device *dev)
57 __releases(dev->rx_queue.lock)
61 spin_unlock(&dev->rx_queue.lock);
62 for (i = 0; i < dev->num_tx_queues; i++) {
63 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
64 spin_unlock(&txq->lock);
68 EXPORT_SYMBOL(qdisc_unlock_tree);
70 static inline int qdisc_qlen(struct Qdisc *q)
75 static inline int dev_requeue_skb(struct sk_buff *skb,
76 struct netdev_queue *dev_queue,
79 if (unlikely(skb->next))
82 q->ops->requeue(skb, q);
84 netif_schedule_queue(dev_queue);
88 static inline struct sk_buff *dequeue_skb(struct Qdisc *q)
92 if ((skb = q->gso_skb))
100 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
101 struct netdev_queue *dev_queue,
106 if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
108 * Same CPU holding the lock. It may be a transient
109 * configuration error, when hard_start_xmit() recurses. We
110 * detect it by checking xmit owner and drop the packet when
111 * deadloop is detected. Return OK to try the next skb.
115 printk(KERN_WARNING "Dead loop on netdevice %s, "
116 "fix it urgently!\n", dev_queue->dev->name);
120 * Another cpu is holding lock, requeue & delay xmits for
123 __get_cpu_var(netdev_rx_stat).cpu_collision++;
124 ret = dev_requeue_skb(skb, dev_queue, q);
131 * NOTE: Called under queue->lock with locally disabled BH.
133 * __QDISC_STATE_RUNNING guarantees only one CPU can process
134 * this qdisc at a time. queue->lock serializes queue accesses for
135 * this queue AND txq->qdisc pointer itself.
137 * netif_tx_lock serializes accesses to device driver.
139 * queue->lock and netif_tx_lock are mutually exclusive,
140 * if one is grabbed, another must be free.
142 * Note, that this procedure can be called by a watchdog timer
144 * Returns to the caller:
145 * 0 - queue is empty or throttled.
146 * >0 - queue is not empty.
149 static inline int qdisc_restart(struct netdev_queue *txq,
152 int ret = NETDEV_TX_BUSY;
153 struct net_device *dev;
154 spinlock_t *root_lock;
158 if (unlikely((skb = dequeue_skb(q)) == NULL))
161 root_lock = qdisc_root_lock(q);
163 /* And release qdisc */
164 spin_unlock(root_lock);
168 HARD_TX_LOCK(dev, txq, smp_processor_id());
169 if (!netif_subqueue_stopped(dev, skb))
170 ret = dev_hard_start_xmit(skb, dev, txq);
171 HARD_TX_UNLOCK(dev, txq);
173 spin_lock(root_lock);
177 /* Driver sent out skb successfully */
181 case NETDEV_TX_LOCKED:
182 /* Driver try lock failed */
183 ret = handle_dev_cpu_collision(skb, txq, q);
187 /* Driver returned NETDEV_TX_BUSY - requeue skb */
188 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
189 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
190 dev->name, ret, q->q.qlen);
192 ret = dev_requeue_skb(skb, txq, q);
199 void __qdisc_run(struct netdev_queue *txq)
201 unsigned long start_time = jiffies;
202 struct Qdisc *q = txq->qdisc;
204 while (qdisc_restart(txq, q)) {
205 if (netif_tx_queue_stopped(txq))
209 * Postpone processing if
210 * 1. another process needs the CPU;
211 * 2. we've been doing it for too long.
213 if (need_resched() || jiffies != start_time) {
214 netif_schedule_queue(txq);
219 clear_bit(__QDISC_STATE_RUNNING, &q->state);
222 static void dev_watchdog(unsigned long arg)
224 struct net_device *dev = (struct net_device *)arg;
227 if (!qdisc_tx_is_noop(dev)) {
228 if (netif_device_present(dev) &&
229 netif_running(dev) &&
230 netif_carrier_ok(dev)) {
231 int some_queue_stopped = 0;
234 for (i = 0; i < dev->num_tx_queues; i++) {
235 struct netdev_queue *txq;
237 txq = netdev_get_tx_queue(dev, i);
238 if (netif_tx_queue_stopped(txq)) {
239 some_queue_stopped = 1;
244 if (some_queue_stopped &&
245 time_after(jiffies, (dev->trans_start +
246 dev->watchdog_timeo))) {
247 printk(KERN_INFO "NETDEV WATCHDOG: %s: "
248 "transmit timed out\n",
250 dev->tx_timeout(dev);
253 if (!mod_timer(&dev->watchdog_timer,
254 round_jiffies(jiffies +
255 dev->watchdog_timeo)))
259 netif_tx_unlock(dev);
264 void __netdev_watchdog_up(struct net_device *dev)
266 if (dev->tx_timeout) {
267 if (dev->watchdog_timeo <= 0)
268 dev->watchdog_timeo = 5*HZ;
269 if (!mod_timer(&dev->watchdog_timer,
270 round_jiffies(jiffies + dev->watchdog_timeo)))
275 static void dev_watchdog_up(struct net_device *dev)
277 __netdev_watchdog_up(dev);
280 static void dev_watchdog_down(struct net_device *dev)
282 netif_tx_lock_bh(dev);
283 if (del_timer(&dev->watchdog_timer))
285 netif_tx_unlock_bh(dev);
289 * netif_carrier_on - set carrier
290 * @dev: network device
292 * Device has detected that carrier.
294 void netif_carrier_on(struct net_device *dev)
296 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
297 linkwatch_fire_event(dev);
298 if (netif_running(dev))
299 __netdev_watchdog_up(dev);
302 EXPORT_SYMBOL(netif_carrier_on);
305 * netif_carrier_off - clear carrier
306 * @dev: network device
308 * Device has detected loss of carrier.
310 void netif_carrier_off(struct net_device *dev)
312 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
313 linkwatch_fire_event(dev);
315 EXPORT_SYMBOL(netif_carrier_off);
317 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
318 under all circumstances. It is difficult to invent anything faster or
322 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
328 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
333 static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
336 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
342 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
345 .enqueue = noop_enqueue,
346 .dequeue = noop_dequeue,
347 .requeue = noop_requeue,
348 .owner = THIS_MODULE,
351 static struct netdev_queue noop_netdev_queue = {
352 .lock = __SPIN_LOCK_UNLOCKED(noop_netdev_queue.lock),
353 .qdisc = &noop_qdisc,
356 struct Qdisc noop_qdisc = {
357 .enqueue = noop_enqueue,
358 .dequeue = noop_dequeue,
359 .flags = TCQ_F_BUILTIN,
360 .ops = &noop_qdisc_ops,
361 .list = LIST_HEAD_INIT(noop_qdisc.list),
362 .dev_queue = &noop_netdev_queue,
364 EXPORT_SYMBOL(noop_qdisc);
366 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
369 .enqueue = noop_enqueue,
370 .dequeue = noop_dequeue,
371 .requeue = noop_requeue,
372 .owner = THIS_MODULE,
375 static struct Qdisc noqueue_qdisc = {
377 .dequeue = noop_dequeue,
378 .flags = TCQ_F_BUILTIN,
379 .ops = &noqueue_qdisc_ops,
380 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
384 static const u8 prio2band[TC_PRIO_MAX+1] =
385 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
387 /* 3-band FIFO queue: old style, but should be a bit faster than
388 generic prio+fifo combination.
391 #define PFIFO_FAST_BANDS 3
393 static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
396 struct sk_buff_head *list = qdisc_priv(qdisc);
397 return list + prio2band[skb->priority & TC_PRIO_MAX];
400 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
402 struct sk_buff_head *list = prio2list(skb, qdisc);
404 if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
406 return __qdisc_enqueue_tail(skb, qdisc, list);
409 return qdisc_drop(skb, qdisc);
412 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
415 struct sk_buff_head *list = qdisc_priv(qdisc);
417 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
418 if (!skb_queue_empty(list + prio)) {
420 return __qdisc_dequeue_head(qdisc, list + prio);
427 static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
430 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
433 static void pfifo_fast_reset(struct Qdisc* qdisc)
436 struct sk_buff_head *list = qdisc_priv(qdisc);
438 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
439 __qdisc_reset_queue(qdisc, list + prio);
441 qdisc->qstats.backlog = 0;
445 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
447 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
449 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
450 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
457 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
460 struct sk_buff_head *list = qdisc_priv(qdisc);
462 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
463 skb_queue_head_init(list + prio);
468 static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
470 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
471 .enqueue = pfifo_fast_enqueue,
472 .dequeue = pfifo_fast_dequeue,
473 .requeue = pfifo_fast_requeue,
474 .init = pfifo_fast_init,
475 .reset = pfifo_fast_reset,
476 .dump = pfifo_fast_dump,
477 .owner = THIS_MODULE,
480 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
481 struct Qdisc_ops *ops)
488 /* ensure that the Qdisc and the private data are 32-byte aligned */
489 size = QDISC_ALIGN(sizeof(*sch));
490 size += ops->priv_size + (QDISC_ALIGNTO - 1);
492 p = kzalloc(size, GFP_KERNEL);
495 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
496 sch->padded = (char *) sch - (char *) p;
498 INIT_LIST_HEAD(&sch->list);
499 skb_queue_head_init(&sch->q);
501 sch->enqueue = ops->enqueue;
502 sch->dequeue = ops->dequeue;
503 sch->dev_queue = dev_queue;
504 dev_hold(qdisc_dev(sch));
505 atomic_set(&sch->refcnt, 1);
512 struct Qdisc * qdisc_create_dflt(struct net_device *dev,
513 struct netdev_queue *dev_queue,
514 struct Qdisc_ops *ops,
515 unsigned int parentid)
519 sch = qdisc_alloc(dev_queue, ops);
522 sch->parent = parentid;
524 if (!ops->init || ops->init(sch, NULL) == 0)
531 EXPORT_SYMBOL(qdisc_create_dflt);
533 /* Under queue->lock and BH! */
535 void qdisc_reset(struct Qdisc *qdisc)
537 const struct Qdisc_ops *ops = qdisc->ops;
542 EXPORT_SYMBOL(qdisc_reset);
544 /* this is the rcu callback function to clean up a qdisc when there
545 * are no further references to it */
547 static void __qdisc_destroy(struct rcu_head *head)
549 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
550 kfree((char *) qdisc - qdisc->padded);
553 /* Under queue->lock and BH! */
555 void qdisc_destroy(struct Qdisc *qdisc)
557 const struct Qdisc_ops *ops = qdisc->ops;
559 if (qdisc->flags & TCQ_F_BUILTIN ||
560 !atomic_dec_and_test(&qdisc->refcnt))
563 list_del(&qdisc->list);
564 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
570 module_put(ops->owner);
571 dev_put(qdisc_dev(qdisc));
572 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
574 EXPORT_SYMBOL(qdisc_destroy);
576 static bool dev_all_qdisc_sleeping_noop(struct net_device *dev)
580 for (i = 0; i < dev->num_tx_queues; i++) {
581 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
583 if (txq->qdisc_sleeping != &noop_qdisc)
589 static void attach_one_default_qdisc(struct net_device *dev,
590 struct netdev_queue *dev_queue,
595 if (dev->tx_queue_len) {
596 qdisc = qdisc_create_dflt(dev, dev_queue,
597 &pfifo_fast_ops, TC_H_ROOT);
599 printk(KERN_INFO "%s: activation failed\n", dev->name);
602 list_add_tail(&qdisc->list, &dev_queue->qdisc_list);
604 qdisc = &noqueue_qdisc;
606 dev_queue->qdisc_sleeping = qdisc;
609 static void transition_one_qdisc(struct net_device *dev,
610 struct netdev_queue *dev_queue,
611 void *_need_watchdog)
613 int *need_watchdog_p = _need_watchdog;
615 spin_lock_bh(&dev_queue->lock);
616 rcu_assign_pointer(dev_queue->qdisc, dev_queue->qdisc_sleeping);
617 if (dev_queue->qdisc != &noqueue_qdisc)
618 *need_watchdog_p = 1;
619 spin_unlock_bh(&dev_queue->lock);
622 void dev_activate(struct net_device *dev)
626 /* No queueing discipline is attached to device;
627 create default one i.e. pfifo_fast for devices,
628 which need queueing and noqueue_qdisc for
632 if (dev_all_qdisc_sleeping_noop(dev))
633 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
635 if (!netif_carrier_ok(dev))
636 /* Delay activation until next carrier-on event */
640 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
643 dev->trans_start = jiffies;
644 dev_watchdog_up(dev);
648 static void dev_deactivate_queue(struct net_device *dev,
649 struct netdev_queue *dev_queue,
650 void *_qdisc_default)
652 struct Qdisc *qdisc_default = _qdisc_default;
653 struct sk_buff *skb = NULL;
656 spin_lock_bh(&dev_queue->lock);
658 qdisc = dev_queue->qdisc;
660 dev_queue->qdisc = qdisc_default;
663 skb = qdisc->gso_skb;
664 qdisc->gso_skb = NULL;
667 spin_unlock_bh(&dev_queue->lock);
672 static bool some_qdisc_is_running(struct net_device *dev, int lock)
676 for (i = 0; i < dev->num_tx_queues; i++) {
677 struct netdev_queue *dev_queue;
678 spinlock_t *root_lock;
682 dev_queue = netdev_get_tx_queue(dev, i);
683 q = dev_queue->qdisc;
684 root_lock = qdisc_root_lock(q);
687 spin_lock_bh(root_lock);
689 val = test_bit(__QDISC_STATE_RUNNING, &q->state);
692 spin_unlock_bh(root_lock);
700 void dev_deactivate(struct net_device *dev)
704 netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
706 dev_watchdog_down(dev);
708 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
711 /* Wait for outstanding qdisc_run calls. */
713 while (some_qdisc_is_running(dev, 0))
717 * Double-check inside queue lock to ensure that all effects
718 * of the queue run are visible when we return.
720 running = some_qdisc_is_running(dev, 1);
723 * The running flag should never be set at this point because
724 * we've already set dev->qdisc to noop_qdisc *inside* the same
725 * pair of spin locks. That is, if any qdisc_run starts after
726 * our initial test it should see the noop_qdisc and then
727 * clear the RUNNING bit before dropping the queue lock. So
728 * if it is set here then we've found a bug.
730 } while (WARN_ON_ONCE(running));
733 static void dev_init_scheduler_queue(struct net_device *dev,
734 struct netdev_queue *dev_queue,
737 struct Qdisc *qdisc = _qdisc;
739 dev_queue->qdisc = qdisc;
740 dev_queue->qdisc_sleeping = qdisc;
741 INIT_LIST_HEAD(&dev_queue->qdisc_list);
744 void dev_init_scheduler(struct net_device *dev)
746 qdisc_lock_tree(dev);
747 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
748 dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
749 qdisc_unlock_tree(dev);
751 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
754 static void shutdown_scheduler_queue(struct net_device *dev,
755 struct netdev_queue *dev_queue,
756 void *_qdisc_default)
758 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
759 struct Qdisc *qdisc_default = _qdisc_default;
762 dev_queue->qdisc = qdisc_default;
763 dev_queue->qdisc_sleeping = qdisc_default;
765 qdisc_destroy(qdisc);
769 void dev_shutdown(struct net_device *dev)
771 qdisc_lock_tree(dev);
772 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
773 shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
774 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
775 qdisc_unlock_tree(dev);