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 * __QUEUE_STATE_QDISC_RUNNING guarantees only one CPU can process
134 * this queue 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)
151 struct Qdisc *q = txq->qdisc;
152 int ret = NETDEV_TX_BUSY;
153 struct net_device *dev;
157 if (unlikely((skb = dequeue_skb(q)) == NULL))
160 /* And release queue */
161 spin_unlock(&txq->lock);
165 HARD_TX_LOCK(dev, txq, smp_processor_id());
166 if (!netif_subqueue_stopped(dev, skb))
167 ret = dev_hard_start_xmit(skb, dev, txq);
168 HARD_TX_UNLOCK(dev, txq);
170 spin_lock(&txq->lock);
175 /* Driver sent out skb successfully */
179 case NETDEV_TX_LOCKED:
180 /* Driver try lock failed */
181 ret = handle_dev_cpu_collision(skb, txq, q);
185 /* Driver returned NETDEV_TX_BUSY - requeue skb */
186 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
187 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
188 dev->name, ret, q->q.qlen);
190 ret = dev_requeue_skb(skb, txq, q);
197 void __qdisc_run(struct netdev_queue *txq)
199 unsigned long start_time = jiffies;
201 while (qdisc_restart(txq)) {
202 if (netif_tx_queue_stopped(txq))
206 * Postpone processing if
207 * 1. another process needs the CPU;
208 * 2. we've been doing it for too long.
210 if (need_resched() || jiffies != start_time) {
211 netif_schedule_queue(txq);
216 clear_bit(__QUEUE_STATE_QDISC_RUNNING, &txq->state);
219 static void dev_watchdog(unsigned long arg)
221 struct net_device *dev = (struct net_device *)arg;
224 if (!qdisc_tx_is_noop(dev)) {
225 if (netif_device_present(dev) &&
226 netif_running(dev) &&
227 netif_carrier_ok(dev)) {
228 int some_queue_stopped = 0;
231 for (i = 0; i < dev->num_tx_queues; i++) {
232 struct netdev_queue *txq;
234 txq = netdev_get_tx_queue(dev, i);
235 if (netif_tx_queue_stopped(txq)) {
236 some_queue_stopped = 1;
241 if (some_queue_stopped &&
242 time_after(jiffies, (dev->trans_start +
243 dev->watchdog_timeo))) {
244 printk(KERN_INFO "NETDEV WATCHDOG: %s: "
245 "transmit timed out\n",
247 dev->tx_timeout(dev);
250 if (!mod_timer(&dev->watchdog_timer,
251 round_jiffies(jiffies +
252 dev->watchdog_timeo)))
256 netif_tx_unlock(dev);
261 void __netdev_watchdog_up(struct net_device *dev)
263 if (dev->tx_timeout) {
264 if (dev->watchdog_timeo <= 0)
265 dev->watchdog_timeo = 5*HZ;
266 if (!mod_timer(&dev->watchdog_timer,
267 round_jiffies(jiffies + dev->watchdog_timeo)))
272 static void dev_watchdog_up(struct net_device *dev)
274 __netdev_watchdog_up(dev);
277 static void dev_watchdog_down(struct net_device *dev)
279 netif_tx_lock_bh(dev);
280 if (del_timer(&dev->watchdog_timer))
282 netif_tx_unlock_bh(dev);
286 * netif_carrier_on - set carrier
287 * @dev: network device
289 * Device has detected that carrier.
291 void netif_carrier_on(struct net_device *dev)
293 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
294 linkwatch_fire_event(dev);
295 if (netif_running(dev))
296 __netdev_watchdog_up(dev);
299 EXPORT_SYMBOL(netif_carrier_on);
302 * netif_carrier_off - clear carrier
303 * @dev: network device
305 * Device has detected loss of carrier.
307 void netif_carrier_off(struct net_device *dev)
309 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
310 linkwatch_fire_event(dev);
312 EXPORT_SYMBOL(netif_carrier_off);
314 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
315 under all circumstances. It is difficult to invent anything faster or
319 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
325 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
330 static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
333 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
339 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
342 .enqueue = noop_enqueue,
343 .dequeue = noop_dequeue,
344 .requeue = noop_requeue,
345 .owner = THIS_MODULE,
348 struct Qdisc noop_qdisc = {
349 .enqueue = noop_enqueue,
350 .dequeue = noop_dequeue,
351 .flags = TCQ_F_BUILTIN,
352 .ops = &noop_qdisc_ops,
353 .list = LIST_HEAD_INIT(noop_qdisc.list),
355 EXPORT_SYMBOL(noop_qdisc);
357 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
360 .enqueue = noop_enqueue,
361 .dequeue = noop_dequeue,
362 .requeue = noop_requeue,
363 .owner = THIS_MODULE,
366 static struct Qdisc noqueue_qdisc = {
368 .dequeue = noop_dequeue,
369 .flags = TCQ_F_BUILTIN,
370 .ops = &noqueue_qdisc_ops,
371 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
375 static const u8 prio2band[TC_PRIO_MAX+1] =
376 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
378 /* 3-band FIFO queue: old style, but should be a bit faster than
379 generic prio+fifo combination.
382 #define PFIFO_FAST_BANDS 3
384 static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
387 struct sk_buff_head *list = qdisc_priv(qdisc);
388 return list + prio2band[skb->priority & TC_PRIO_MAX];
391 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
393 struct sk_buff_head *list = prio2list(skb, qdisc);
395 if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
397 return __qdisc_enqueue_tail(skb, qdisc, list);
400 return qdisc_drop(skb, qdisc);
403 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
406 struct sk_buff_head *list = qdisc_priv(qdisc);
408 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
409 if (!skb_queue_empty(list + prio)) {
411 return __qdisc_dequeue_head(qdisc, list + prio);
418 static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
421 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
424 static void pfifo_fast_reset(struct Qdisc* qdisc)
427 struct sk_buff_head *list = qdisc_priv(qdisc);
429 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
430 __qdisc_reset_queue(qdisc, list + prio);
432 qdisc->qstats.backlog = 0;
436 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
438 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
440 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
441 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
448 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
451 struct sk_buff_head *list = qdisc_priv(qdisc);
453 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
454 skb_queue_head_init(list + prio);
459 static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
461 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
462 .enqueue = pfifo_fast_enqueue,
463 .dequeue = pfifo_fast_dequeue,
464 .requeue = pfifo_fast_requeue,
465 .init = pfifo_fast_init,
466 .reset = pfifo_fast_reset,
467 .dump = pfifo_fast_dump,
468 .owner = THIS_MODULE,
471 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
472 struct Qdisc_ops *ops)
479 /* ensure that the Qdisc and the private data are 32-byte aligned */
480 size = QDISC_ALIGN(sizeof(*sch));
481 size += ops->priv_size + (QDISC_ALIGNTO - 1);
483 p = kzalloc(size, GFP_KERNEL);
486 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
487 sch->padded = (char *) sch - (char *) p;
489 INIT_LIST_HEAD(&sch->list);
490 skb_queue_head_init(&sch->q);
492 sch->enqueue = ops->enqueue;
493 sch->dequeue = ops->dequeue;
494 sch->dev_queue = dev_queue;
495 dev_hold(qdisc_dev(sch));
496 atomic_set(&sch->refcnt, 1);
503 struct Qdisc * qdisc_create_dflt(struct net_device *dev,
504 struct netdev_queue *dev_queue,
505 struct Qdisc_ops *ops,
506 unsigned int parentid)
510 sch = qdisc_alloc(dev_queue, ops);
513 sch->parent = parentid;
515 if (!ops->init || ops->init(sch, NULL) == 0)
522 EXPORT_SYMBOL(qdisc_create_dflt);
524 /* Under queue->lock and BH! */
526 void qdisc_reset(struct Qdisc *qdisc)
528 const struct Qdisc_ops *ops = qdisc->ops;
533 EXPORT_SYMBOL(qdisc_reset);
535 /* this is the rcu callback function to clean up a qdisc when there
536 * are no further references to it */
538 static void __qdisc_destroy(struct rcu_head *head)
540 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
541 kfree((char *) qdisc - qdisc->padded);
544 /* Under queue->lock and BH! */
546 void qdisc_destroy(struct Qdisc *qdisc)
548 const struct Qdisc_ops *ops = qdisc->ops;
550 if (qdisc->flags & TCQ_F_BUILTIN ||
551 !atomic_dec_and_test(&qdisc->refcnt))
554 list_del(&qdisc->list);
555 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
561 module_put(ops->owner);
562 dev_put(qdisc_dev(qdisc));
563 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
565 EXPORT_SYMBOL(qdisc_destroy);
567 static bool dev_all_qdisc_sleeping_noop(struct net_device *dev)
571 for (i = 0; i < dev->num_tx_queues; i++) {
572 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
574 if (txq->qdisc_sleeping != &noop_qdisc)
580 static void attach_one_default_qdisc(struct net_device *dev,
581 struct netdev_queue *dev_queue,
586 if (dev->tx_queue_len) {
587 qdisc = qdisc_create_dflt(dev, dev_queue,
588 &pfifo_fast_ops, TC_H_ROOT);
590 printk(KERN_INFO "%s: activation failed\n", dev->name);
593 list_add_tail(&qdisc->list, &dev_queue->qdisc_list);
595 qdisc = &noqueue_qdisc;
597 dev_queue->qdisc_sleeping = qdisc;
600 static void transition_one_qdisc(struct net_device *dev,
601 struct netdev_queue *dev_queue,
602 void *_need_watchdog)
604 int *need_watchdog_p = _need_watchdog;
606 spin_lock_bh(&dev_queue->lock);
607 rcu_assign_pointer(dev_queue->qdisc, dev_queue->qdisc_sleeping);
608 if (dev_queue->qdisc != &noqueue_qdisc)
609 *need_watchdog_p = 1;
610 spin_unlock_bh(&dev_queue->lock);
613 void dev_activate(struct net_device *dev)
617 /* No queueing discipline is attached to device;
618 create default one i.e. pfifo_fast for devices,
619 which need queueing and noqueue_qdisc for
623 if (dev_all_qdisc_sleeping_noop(dev))
624 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
626 if (!netif_carrier_ok(dev))
627 /* Delay activation until next carrier-on event */
631 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
634 dev->trans_start = jiffies;
635 dev_watchdog_up(dev);
639 static void dev_deactivate_queue(struct net_device *dev,
640 struct netdev_queue *dev_queue,
641 void *_qdisc_default)
643 struct Qdisc *qdisc_default = _qdisc_default;
644 struct sk_buff *skb = NULL;
647 spin_lock_bh(&dev_queue->lock);
649 qdisc = dev_queue->qdisc;
651 dev_queue->qdisc = qdisc_default;
654 skb = qdisc->gso_skb;
655 qdisc->gso_skb = NULL;
658 spin_unlock_bh(&dev_queue->lock);
663 static bool some_qdisc_is_running(struct net_device *dev, int lock)
667 for (i = 0; i < dev->num_tx_queues; i++) {
668 struct netdev_queue *dev_queue;
671 dev_queue = netdev_get_tx_queue(dev, i);
674 spin_lock_bh(&dev_queue->lock);
676 val = test_bit(__QUEUE_STATE_QDISC_RUNNING, &dev_queue->state);
679 spin_unlock_bh(&dev_queue->lock);
687 void dev_deactivate(struct net_device *dev)
691 netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
693 dev_watchdog_down(dev);
695 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
698 /* Wait for outstanding qdisc_run calls. */
700 while (some_qdisc_is_running(dev, 0))
704 * Double-check inside queue lock to ensure that all effects
705 * of the queue run are visible when we return.
707 running = some_qdisc_is_running(dev, 1);
710 * The running flag should never be set at this point because
711 * we've already set dev->qdisc to noop_qdisc *inside* the same
712 * pair of spin locks. That is, if any qdisc_run starts after
713 * our initial test it should see the noop_qdisc and then
714 * clear the RUNNING bit before dropping the queue lock. So
715 * if it is set here then we've found a bug.
717 } while (WARN_ON_ONCE(running));
720 static void dev_init_scheduler_queue(struct net_device *dev,
721 struct netdev_queue *dev_queue,
724 struct Qdisc *qdisc = _qdisc;
726 dev_queue->qdisc = qdisc;
727 dev_queue->qdisc_sleeping = qdisc;
728 INIT_LIST_HEAD(&dev_queue->qdisc_list);
731 void dev_init_scheduler(struct net_device *dev)
733 qdisc_lock_tree(dev);
734 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
735 dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
736 qdisc_unlock_tree(dev);
738 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
741 static void shutdown_scheduler_queue(struct net_device *dev,
742 struct netdev_queue *dev_queue,
743 void *_qdisc_default)
745 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
746 struct Qdisc *qdisc_default = _qdisc_default;
749 dev_queue->qdisc = qdisc_default;
750 dev_queue->qdisc_sleeping = qdisc_default;
752 qdisc_destroy(qdisc);
756 void dev_shutdown(struct net_device *dev)
758 qdisc_lock_tree(dev);
759 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
760 shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
761 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
762 qdisc_unlock_tree(dev);