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, struct Qdisc *q)
77 if (unlikely(skb->next))
80 q->ops->requeue(skb, q);
86 static inline struct sk_buff *dequeue_skb(struct Qdisc *q)
90 if ((skb = q->gso_skb))
98 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
99 struct netdev_queue *dev_queue,
104 if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
106 * Same CPU holding the lock. It may be a transient
107 * configuration error, when hard_start_xmit() recurses. We
108 * detect it by checking xmit owner and drop the packet when
109 * deadloop is detected. Return OK to try the next skb.
113 printk(KERN_WARNING "Dead loop on netdevice %s, "
114 "fix it urgently!\n", dev_queue->dev->name);
118 * Another cpu is holding lock, requeue & delay xmits for
121 __get_cpu_var(netdev_rx_stat).cpu_collision++;
122 ret = dev_requeue_skb(skb, q);
129 * NOTE: Called under queue->lock with locally disabled BH.
131 * __QDISC_STATE_RUNNING guarantees only one CPU can process
132 * this qdisc at a time. queue->lock serializes queue accesses for
133 * this queue AND txq->qdisc pointer itself.
135 * netif_tx_lock serializes accesses to device driver.
137 * queue->lock and netif_tx_lock are mutually exclusive,
138 * if one is grabbed, another must be free.
140 * Note, that this procedure can be called by a watchdog timer
142 * Returns to the caller:
143 * 0 - queue is empty or throttled.
144 * >0 - queue is not empty.
147 static inline int qdisc_restart(struct Qdisc *q)
149 struct netdev_queue *txq;
150 int ret = NETDEV_TX_BUSY;
151 struct net_device *dev;
152 spinlock_t *root_lock;
156 if (unlikely((skb = dequeue_skb(q)) == NULL))
159 root_lock = qdisc_root_lock(q);
161 /* And release qdisc */
162 spin_unlock(root_lock);
165 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
167 HARD_TX_LOCK(dev, txq, smp_processor_id());
168 if (!netif_subqueue_stopped(dev, skb))
169 ret = dev_hard_start_xmit(skb, dev, txq);
170 HARD_TX_UNLOCK(dev, txq);
172 spin_lock(root_lock);
176 /* Driver sent out skb successfully */
180 case NETDEV_TX_LOCKED:
181 /* Driver try lock failed */
182 ret = handle_dev_cpu_collision(skb, txq, q);
186 /* Driver returned NETDEV_TX_BUSY - requeue skb */
187 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
188 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
189 dev->name, ret, q->q.qlen);
191 ret = dev_requeue_skb(skb, q);
195 if (ret && netif_tx_queue_stopped(txq))
201 void __qdisc_run(struct Qdisc *q)
203 unsigned long start_time = jiffies;
205 while (qdisc_restart(q)) {
207 * Postpone processing if
208 * 1. another process needs the CPU;
209 * 2. we've been doing it for too long.
211 if (need_resched() || jiffies != start_time) {
217 clear_bit(__QDISC_STATE_RUNNING, &q->state);
220 static void dev_watchdog(unsigned long arg)
222 struct net_device *dev = (struct net_device *)arg;
225 if (!qdisc_tx_is_noop(dev)) {
226 if (netif_device_present(dev) &&
227 netif_running(dev) &&
228 netif_carrier_ok(dev)) {
229 int some_queue_stopped = 0;
232 for (i = 0; i < dev->num_tx_queues; i++) {
233 struct netdev_queue *txq;
235 txq = netdev_get_tx_queue(dev, i);
236 if (netif_tx_queue_stopped(txq)) {
237 some_queue_stopped = 1;
242 if (some_queue_stopped &&
243 time_after(jiffies, (dev->trans_start +
244 dev->watchdog_timeo))) {
245 printk(KERN_INFO "NETDEV WATCHDOG: %s: "
246 "transmit timed out\n",
248 dev->tx_timeout(dev);
251 if (!mod_timer(&dev->watchdog_timer,
252 round_jiffies(jiffies +
253 dev->watchdog_timeo)))
257 netif_tx_unlock(dev);
262 void __netdev_watchdog_up(struct net_device *dev)
264 if (dev->tx_timeout) {
265 if (dev->watchdog_timeo <= 0)
266 dev->watchdog_timeo = 5*HZ;
267 if (!mod_timer(&dev->watchdog_timer,
268 round_jiffies(jiffies + dev->watchdog_timeo)))
273 static void dev_watchdog_up(struct net_device *dev)
275 __netdev_watchdog_up(dev);
278 static void dev_watchdog_down(struct net_device *dev)
280 netif_tx_lock_bh(dev);
281 if (del_timer(&dev->watchdog_timer))
283 netif_tx_unlock_bh(dev);
287 * netif_carrier_on - set carrier
288 * @dev: network device
290 * Device has detected that carrier.
292 void netif_carrier_on(struct net_device *dev)
294 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
295 linkwatch_fire_event(dev);
296 if (netif_running(dev))
297 __netdev_watchdog_up(dev);
300 EXPORT_SYMBOL(netif_carrier_on);
303 * netif_carrier_off - clear carrier
304 * @dev: network device
306 * Device has detected loss of carrier.
308 void netif_carrier_off(struct net_device *dev)
310 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
311 linkwatch_fire_event(dev);
313 EXPORT_SYMBOL(netif_carrier_off);
315 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
316 under all circumstances. It is difficult to invent anything faster or
320 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
326 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
331 static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
334 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
340 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
343 .enqueue = noop_enqueue,
344 .dequeue = noop_dequeue,
345 .requeue = noop_requeue,
346 .owner = THIS_MODULE,
349 static struct netdev_queue noop_netdev_queue = {
350 .lock = __SPIN_LOCK_UNLOCKED(noop_netdev_queue.lock),
351 .qdisc = &noop_qdisc,
354 struct Qdisc noop_qdisc = {
355 .enqueue = noop_enqueue,
356 .dequeue = noop_dequeue,
357 .flags = TCQ_F_BUILTIN,
358 .ops = &noop_qdisc_ops,
359 .list = LIST_HEAD_INIT(noop_qdisc.list),
360 .dev_queue = &noop_netdev_queue,
362 EXPORT_SYMBOL(noop_qdisc);
364 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
367 .enqueue = noop_enqueue,
368 .dequeue = noop_dequeue,
369 .requeue = noop_requeue,
370 .owner = THIS_MODULE,
373 static struct Qdisc noqueue_qdisc = {
375 .dequeue = noop_dequeue,
376 .flags = TCQ_F_BUILTIN,
377 .ops = &noqueue_qdisc_ops,
378 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
382 static const u8 prio2band[TC_PRIO_MAX+1] =
383 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
385 /* 3-band FIFO queue: old style, but should be a bit faster than
386 generic prio+fifo combination.
389 #define PFIFO_FAST_BANDS 3
391 static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
394 struct sk_buff_head *list = qdisc_priv(qdisc);
395 return list + prio2band[skb->priority & TC_PRIO_MAX];
398 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
400 struct sk_buff_head *list = prio2list(skb, qdisc);
402 if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
404 return __qdisc_enqueue_tail(skb, qdisc, list);
407 return qdisc_drop(skb, qdisc);
410 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
413 struct sk_buff_head *list = qdisc_priv(qdisc);
415 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
416 if (!skb_queue_empty(list + prio)) {
418 return __qdisc_dequeue_head(qdisc, list + prio);
425 static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
428 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
431 static void pfifo_fast_reset(struct Qdisc* qdisc)
434 struct sk_buff_head *list = qdisc_priv(qdisc);
436 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
437 __qdisc_reset_queue(qdisc, list + prio);
439 qdisc->qstats.backlog = 0;
443 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
445 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
447 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
448 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
455 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
458 struct sk_buff_head *list = qdisc_priv(qdisc);
460 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
461 skb_queue_head_init(list + prio);
466 static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
468 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
469 .enqueue = pfifo_fast_enqueue,
470 .dequeue = pfifo_fast_dequeue,
471 .requeue = pfifo_fast_requeue,
472 .init = pfifo_fast_init,
473 .reset = pfifo_fast_reset,
474 .dump = pfifo_fast_dump,
475 .owner = THIS_MODULE,
478 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
479 struct Qdisc_ops *ops)
486 /* ensure that the Qdisc and the private data are 32-byte aligned */
487 size = QDISC_ALIGN(sizeof(*sch));
488 size += ops->priv_size + (QDISC_ALIGNTO - 1);
490 p = kzalloc(size, GFP_KERNEL);
493 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
494 sch->padded = (char *) sch - (char *) p;
496 INIT_LIST_HEAD(&sch->list);
497 skb_queue_head_init(&sch->q);
499 sch->enqueue = ops->enqueue;
500 sch->dequeue = ops->dequeue;
501 sch->dev_queue = dev_queue;
502 dev_hold(qdisc_dev(sch));
503 atomic_set(&sch->refcnt, 1);
510 struct Qdisc * qdisc_create_dflt(struct net_device *dev,
511 struct netdev_queue *dev_queue,
512 struct Qdisc_ops *ops,
513 unsigned int parentid)
517 sch = qdisc_alloc(dev_queue, ops);
520 sch->parent = parentid;
522 if (!ops->init || ops->init(sch, NULL) == 0)
529 EXPORT_SYMBOL(qdisc_create_dflt);
531 /* Under queue->lock and BH! */
533 void qdisc_reset(struct Qdisc *qdisc)
535 const struct Qdisc_ops *ops = qdisc->ops;
540 EXPORT_SYMBOL(qdisc_reset);
542 /* this is the rcu callback function to clean up a qdisc when there
543 * are no further references to it */
545 static void __qdisc_destroy(struct rcu_head *head)
547 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
548 kfree((char *) qdisc - qdisc->padded);
551 /* Under queue->lock and BH! */
553 void qdisc_destroy(struct Qdisc *qdisc)
555 const struct Qdisc_ops *ops = qdisc->ops;
557 if (qdisc->flags & TCQ_F_BUILTIN ||
558 !atomic_dec_and_test(&qdisc->refcnt))
561 list_del(&qdisc->list);
562 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
568 module_put(ops->owner);
569 dev_put(qdisc_dev(qdisc));
570 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
572 EXPORT_SYMBOL(qdisc_destroy);
574 static bool dev_all_qdisc_sleeping_noop(struct net_device *dev)
578 for (i = 0; i < dev->num_tx_queues; i++) {
579 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
581 if (txq->qdisc_sleeping != &noop_qdisc)
587 static void attach_one_default_qdisc(struct net_device *dev,
588 struct netdev_queue *dev_queue,
593 if (dev->tx_queue_len) {
594 qdisc = qdisc_create_dflt(dev, dev_queue,
595 &pfifo_fast_ops, TC_H_ROOT);
597 printk(KERN_INFO "%s: activation failed\n", dev->name);
600 list_add_tail(&qdisc->list, &dev_queue->qdisc_list);
602 qdisc = &noqueue_qdisc;
604 dev_queue->qdisc_sleeping = qdisc;
607 static void transition_one_qdisc(struct net_device *dev,
608 struct netdev_queue *dev_queue,
609 void *_need_watchdog)
611 int *need_watchdog_p = _need_watchdog;
613 spin_lock_bh(&dev_queue->lock);
614 rcu_assign_pointer(dev_queue->qdisc, dev_queue->qdisc_sleeping);
615 if (dev_queue->qdisc != &noqueue_qdisc)
616 *need_watchdog_p = 1;
617 spin_unlock_bh(&dev_queue->lock);
620 void dev_activate(struct net_device *dev)
624 /* No queueing discipline is attached to device;
625 create default one i.e. pfifo_fast for devices,
626 which need queueing and noqueue_qdisc for
630 if (dev_all_qdisc_sleeping_noop(dev))
631 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
633 if (!netif_carrier_ok(dev))
634 /* Delay activation until next carrier-on event */
638 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
641 dev->trans_start = jiffies;
642 dev_watchdog_up(dev);
646 static void dev_deactivate_queue(struct net_device *dev,
647 struct netdev_queue *dev_queue,
648 void *_qdisc_default)
650 struct Qdisc *qdisc_default = _qdisc_default;
651 struct sk_buff *skb = NULL;
654 spin_lock_bh(&dev_queue->lock);
656 qdisc = dev_queue->qdisc;
658 dev_queue->qdisc = qdisc_default;
661 skb = qdisc->gso_skb;
662 qdisc->gso_skb = NULL;
665 spin_unlock_bh(&dev_queue->lock);
670 static bool some_qdisc_is_running(struct net_device *dev, int lock)
674 for (i = 0; i < dev->num_tx_queues; i++) {
675 struct netdev_queue *dev_queue;
676 spinlock_t *root_lock;
680 dev_queue = netdev_get_tx_queue(dev, i);
681 q = dev_queue->qdisc;
682 root_lock = qdisc_root_lock(q);
685 spin_lock_bh(root_lock);
687 val = test_bit(__QDISC_STATE_RUNNING, &q->state);
690 spin_unlock_bh(root_lock);
698 void dev_deactivate(struct net_device *dev)
702 netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
704 dev_watchdog_down(dev);
706 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
709 /* Wait for outstanding qdisc_run calls. */
711 while (some_qdisc_is_running(dev, 0))
715 * Double-check inside queue lock to ensure that all effects
716 * of the queue run are visible when we return.
718 running = some_qdisc_is_running(dev, 1);
721 * The running flag should never be set at this point because
722 * we've already set dev->qdisc to noop_qdisc *inside* the same
723 * pair of spin locks. That is, if any qdisc_run starts after
724 * our initial test it should see the noop_qdisc and then
725 * clear the RUNNING bit before dropping the queue lock. So
726 * if it is set here then we've found a bug.
728 } while (WARN_ON_ONCE(running));
731 static void dev_init_scheduler_queue(struct net_device *dev,
732 struct netdev_queue *dev_queue,
735 struct Qdisc *qdisc = _qdisc;
737 dev_queue->qdisc = qdisc;
738 dev_queue->qdisc_sleeping = qdisc;
739 INIT_LIST_HEAD(&dev_queue->qdisc_list);
742 void dev_init_scheduler(struct net_device *dev)
744 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
745 dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
747 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
750 static void shutdown_scheduler_queue(struct net_device *dev,
751 struct netdev_queue *dev_queue,
752 void *_qdisc_default)
754 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
755 struct Qdisc *qdisc_default = _qdisc_default;
758 dev_queue->qdisc = qdisc_default;
759 dev_queue->qdisc_sleeping = qdisc_default;
761 qdisc_destroy(qdisc);
765 void dev_shutdown(struct net_device *dev)
767 qdisc_lock_tree(dev);
768 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
769 shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
770 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
771 qdisc_unlock_tree(dev);