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 qdisc_qlen(struct Qdisc *q)
45 static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
47 __skb_queue_head(&q->requeue, skb);
53 static inline struct sk_buff *dequeue_skb(struct Qdisc *q)
57 skb = __skb_dequeue(&q->requeue);
64 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
65 struct netdev_queue *dev_queue,
70 if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
72 * Same CPU holding the lock. It may be a transient
73 * configuration error, when hard_start_xmit() recurses. We
74 * detect it by checking xmit owner and drop the packet when
75 * deadloop is detected. Return OK to try the next skb.
79 printk(KERN_WARNING "Dead loop on netdevice %s, "
80 "fix it urgently!\n", dev_queue->dev->name);
84 * Another cpu is holding lock, requeue & delay xmits for
87 __get_cpu_var(netdev_rx_stat).cpu_collision++;
88 ret = dev_requeue_skb(skb, q);
95 * NOTE: Called under qdisc_lock(q) with locally disabled BH.
97 * __QDISC_STATE_RUNNING guarantees only one CPU can process
98 * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
101 * netif_tx_lock serializes accesses to device driver.
103 * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
104 * if one is grabbed, another must be free.
106 * Note, that this procedure can be called by a watchdog timer
108 * Returns to the caller:
109 * 0 - queue is empty or throttled.
110 * >0 - queue is not empty.
113 static inline int qdisc_restart(struct Qdisc *q)
115 struct netdev_queue *txq;
116 int ret = NETDEV_TX_BUSY;
117 struct net_device *dev;
118 spinlock_t *root_lock;
122 if (unlikely((skb = dequeue_skb(q)) == NULL))
125 root_lock = qdisc_lock(q);
127 /* And release qdisc */
128 spin_unlock(root_lock);
131 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
133 HARD_TX_LOCK(dev, txq, smp_processor_id());
134 if (!netif_tx_queue_stopped(txq) &&
135 !netif_tx_queue_frozen(txq))
136 ret = dev_hard_start_xmit(skb, dev, txq);
137 HARD_TX_UNLOCK(dev, txq);
139 spin_lock(root_lock);
143 /* Driver sent out skb successfully */
147 case NETDEV_TX_LOCKED:
148 /* Driver try lock failed */
149 ret = handle_dev_cpu_collision(skb, txq, q);
153 /* Driver returned NETDEV_TX_BUSY - requeue skb */
154 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
155 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
156 dev->name, ret, q->q.qlen);
158 ret = dev_requeue_skb(skb, q);
162 if (ret && (netif_tx_queue_stopped(txq) ||
163 netif_tx_queue_frozen(txq)))
169 void __qdisc_run(struct Qdisc *q)
171 unsigned long start_time = jiffies;
173 while (qdisc_restart(q)) {
175 * Postpone processing if
176 * 1. another process needs the CPU;
177 * 2. we've been doing it for too long.
179 if (need_resched() || jiffies != start_time) {
185 clear_bit(__QDISC_STATE_RUNNING, &q->state);
188 static void dev_watchdog(unsigned long arg)
190 struct net_device *dev = (struct net_device *)arg;
193 if (!qdisc_tx_is_noop(dev)) {
194 if (netif_device_present(dev) &&
195 netif_running(dev) &&
196 netif_carrier_ok(dev)) {
197 int some_queue_stopped = 0;
200 for (i = 0; i < dev->num_tx_queues; i++) {
201 struct netdev_queue *txq;
203 txq = netdev_get_tx_queue(dev, i);
204 if (netif_tx_queue_stopped(txq)) {
205 some_queue_stopped = 1;
210 if (some_queue_stopped &&
211 time_after(jiffies, (dev->trans_start +
212 dev->watchdog_timeo))) {
214 WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit timed out\n",
215 dev->name, netdev_drivername(dev, drivername, 64));
216 dev->tx_timeout(dev);
218 if (!mod_timer(&dev->watchdog_timer,
219 round_jiffies(jiffies +
220 dev->watchdog_timeo)))
224 netif_tx_unlock(dev);
229 void __netdev_watchdog_up(struct net_device *dev)
231 if (dev->tx_timeout) {
232 if (dev->watchdog_timeo <= 0)
233 dev->watchdog_timeo = 5*HZ;
234 if (!mod_timer(&dev->watchdog_timer,
235 round_jiffies(jiffies + dev->watchdog_timeo)))
240 static void dev_watchdog_up(struct net_device *dev)
242 __netdev_watchdog_up(dev);
245 static void dev_watchdog_down(struct net_device *dev)
247 netif_tx_lock_bh(dev);
248 if (del_timer(&dev->watchdog_timer))
250 netif_tx_unlock_bh(dev);
254 * netif_carrier_on - set carrier
255 * @dev: network device
257 * Device has detected that carrier.
259 void netif_carrier_on(struct net_device *dev)
261 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
262 linkwatch_fire_event(dev);
263 if (netif_running(dev))
264 __netdev_watchdog_up(dev);
267 EXPORT_SYMBOL(netif_carrier_on);
270 * netif_carrier_off - clear carrier
271 * @dev: network device
273 * Device has detected loss of carrier.
275 void netif_carrier_off(struct net_device *dev)
277 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
278 linkwatch_fire_event(dev);
280 EXPORT_SYMBOL(netif_carrier_off);
282 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
283 under all circumstances. It is difficult to invent anything faster or
287 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
293 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
298 static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
301 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
307 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
310 .enqueue = noop_enqueue,
311 .dequeue = noop_dequeue,
312 .requeue = noop_requeue,
313 .owner = THIS_MODULE,
316 static struct netdev_queue noop_netdev_queue = {
317 .qdisc = &noop_qdisc,
320 struct Qdisc noop_qdisc = {
321 .enqueue = noop_enqueue,
322 .dequeue = noop_dequeue,
323 .flags = TCQ_F_BUILTIN,
324 .ops = &noop_qdisc_ops,
325 .list = LIST_HEAD_INIT(noop_qdisc.list),
326 .requeue.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
327 .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
328 .dev_queue = &noop_netdev_queue,
330 EXPORT_SYMBOL(noop_qdisc);
332 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
335 .enqueue = noop_enqueue,
336 .dequeue = noop_dequeue,
337 .requeue = noop_requeue,
338 .owner = THIS_MODULE,
341 static struct Qdisc noqueue_qdisc;
342 static struct netdev_queue noqueue_netdev_queue = {
343 .qdisc = &noqueue_qdisc,
346 static struct Qdisc noqueue_qdisc = {
348 .dequeue = noop_dequeue,
349 .flags = TCQ_F_BUILTIN,
350 .ops = &noqueue_qdisc_ops,
351 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
352 .requeue.lock = __SPIN_LOCK_UNLOCKED(noqueue_qdisc.q.lock),
353 .q.lock = __SPIN_LOCK_UNLOCKED(noqueue_qdisc.q.lock),
354 .dev_queue = &noqueue_netdev_queue,
358 static const u8 prio2band[TC_PRIO_MAX+1] =
359 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
361 /* 3-band FIFO queue: old style, but should be a bit faster than
362 generic prio+fifo combination.
365 #define PFIFO_FAST_BANDS 3
367 static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
370 struct sk_buff_head *list = qdisc_priv(qdisc);
371 return list + prio2band[skb->priority & TC_PRIO_MAX];
374 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
376 struct sk_buff_head *list = prio2list(skb, qdisc);
378 if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
380 return __qdisc_enqueue_tail(skb, qdisc, list);
383 return qdisc_drop(skb, qdisc);
386 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
389 struct sk_buff_head *list = qdisc_priv(qdisc);
391 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
392 if (!skb_queue_empty(list + prio)) {
394 return __qdisc_dequeue_head(qdisc, list + prio);
401 static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
404 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
407 static void pfifo_fast_reset(struct Qdisc* qdisc)
410 struct sk_buff_head *list = qdisc_priv(qdisc);
412 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
413 __qdisc_reset_queue(qdisc, list + prio);
415 qdisc->qstats.backlog = 0;
419 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
421 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
423 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
424 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
431 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
434 struct sk_buff_head *list = qdisc_priv(qdisc);
436 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
437 skb_queue_head_init(list + prio);
442 static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
444 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
445 .enqueue = pfifo_fast_enqueue,
446 .dequeue = pfifo_fast_dequeue,
447 .requeue = pfifo_fast_requeue,
448 .init = pfifo_fast_init,
449 .reset = pfifo_fast_reset,
450 .dump = pfifo_fast_dump,
451 .owner = THIS_MODULE,
454 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
455 struct Qdisc_ops *ops)
462 /* ensure that the Qdisc and the private data are 32-byte aligned */
463 size = QDISC_ALIGN(sizeof(*sch));
464 size += ops->priv_size + (QDISC_ALIGNTO - 1);
466 p = kzalloc(size, GFP_KERNEL);
469 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
470 sch->padded = (char *) sch - (char *) p;
472 INIT_LIST_HEAD(&sch->list);
473 skb_queue_head_init(&sch->requeue);
474 skb_queue_head_init(&sch->q);
476 sch->enqueue = ops->enqueue;
477 sch->dequeue = ops->dequeue;
478 sch->dev_queue = dev_queue;
479 dev_hold(qdisc_dev(sch));
480 atomic_set(&sch->refcnt, 1);
487 struct Qdisc * qdisc_create_dflt(struct net_device *dev,
488 struct netdev_queue *dev_queue,
489 struct Qdisc_ops *ops,
490 unsigned int parentid)
494 sch = qdisc_alloc(dev_queue, ops);
497 sch->parent = parentid;
499 if (!ops->init || ops->init(sch, NULL) == 0)
506 EXPORT_SYMBOL(qdisc_create_dflt);
508 /* Under qdisc_lock(qdisc) and BH! */
510 void qdisc_reset(struct Qdisc *qdisc)
512 const struct Qdisc_ops *ops = qdisc->ops;
517 EXPORT_SYMBOL(qdisc_reset);
519 void qdisc_destroy(struct Qdisc *qdisc)
521 const struct Qdisc_ops *ops = qdisc->ops;
523 if (qdisc->flags & TCQ_F_BUILTIN ||
524 !atomic_dec_and_test(&qdisc->refcnt))
527 #ifdef CONFIG_NET_SCHED
528 qdisc_list_del(qdisc);
530 qdisc_put_stab(qdisc->stab);
532 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
538 module_put(ops->owner);
539 dev_put(qdisc_dev(qdisc));
541 __skb_queue_purge(&qdisc->requeue);
543 kfree((char *) qdisc - qdisc->padded);
545 EXPORT_SYMBOL(qdisc_destroy);
547 static bool dev_all_qdisc_sleeping_noop(struct net_device *dev)
551 for (i = 0; i < dev->num_tx_queues; i++) {
552 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
554 if (txq->qdisc_sleeping != &noop_qdisc)
560 static void attach_one_default_qdisc(struct net_device *dev,
561 struct netdev_queue *dev_queue,
566 if (dev->tx_queue_len) {
567 qdisc = qdisc_create_dflt(dev, dev_queue,
568 &pfifo_fast_ops, TC_H_ROOT);
570 printk(KERN_INFO "%s: activation failed\n", dev->name);
574 qdisc = &noqueue_qdisc;
576 dev_queue->qdisc_sleeping = qdisc;
579 static void transition_one_qdisc(struct net_device *dev,
580 struct netdev_queue *dev_queue,
581 void *_need_watchdog)
583 struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
584 int *need_watchdog_p = _need_watchdog;
586 if (!(new_qdisc->flags & TCQ_F_BUILTIN))
587 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
589 rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
590 if (need_watchdog_p && new_qdisc != &noqueue_qdisc)
591 *need_watchdog_p = 1;
594 void dev_activate(struct net_device *dev)
598 /* No queueing discipline is attached to device;
599 create default one i.e. pfifo_fast for devices,
600 which need queueing and noqueue_qdisc for
604 if (dev_all_qdisc_sleeping_noop(dev))
605 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
607 if (!netif_carrier_ok(dev))
608 /* Delay activation until next carrier-on event */
612 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
613 transition_one_qdisc(dev, &dev->rx_queue, NULL);
616 dev->trans_start = jiffies;
617 dev_watchdog_up(dev);
621 static void dev_deactivate_queue(struct net_device *dev,
622 struct netdev_queue *dev_queue,
623 void *_qdisc_default)
625 struct Qdisc *qdisc_default = _qdisc_default;
628 qdisc = dev_queue->qdisc;
630 spin_lock_bh(qdisc_lock(qdisc));
632 if (!(qdisc->flags & TCQ_F_BUILTIN))
633 set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
635 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
638 spin_unlock_bh(qdisc_lock(qdisc));
642 static bool some_qdisc_is_busy(struct net_device *dev)
646 for (i = 0; i < dev->num_tx_queues; i++) {
647 struct netdev_queue *dev_queue;
648 spinlock_t *root_lock;
652 dev_queue = netdev_get_tx_queue(dev, i);
653 q = dev_queue->qdisc_sleeping;
654 root_lock = qdisc_lock(q);
656 spin_lock_bh(root_lock);
658 val = (test_bit(__QDISC_STATE_RUNNING, &q->state) ||
659 test_bit(__QDISC_STATE_SCHED, &q->state));
661 spin_unlock_bh(root_lock);
669 void dev_deactivate(struct net_device *dev)
671 netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
672 dev_deactivate_queue(dev, &dev->rx_queue, &noop_qdisc);
674 dev_watchdog_down(dev);
676 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
679 /* Wait for outstanding qdisc_run calls. */
680 while (some_qdisc_is_busy(dev))
684 static void dev_init_scheduler_queue(struct net_device *dev,
685 struct netdev_queue *dev_queue,
688 struct Qdisc *qdisc = _qdisc;
690 dev_queue->qdisc = qdisc;
691 dev_queue->qdisc_sleeping = qdisc;
694 void dev_init_scheduler(struct net_device *dev)
696 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
697 dev_init_scheduler_queue(dev, &dev->rx_queue, &noop_qdisc);
699 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
702 static void shutdown_scheduler_queue(struct net_device *dev,
703 struct netdev_queue *dev_queue,
704 void *_qdisc_default)
706 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
707 struct Qdisc *qdisc_default = _qdisc_default;
710 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
711 dev_queue->qdisc_sleeping = qdisc_default;
713 qdisc_destroy(qdisc);
717 void dev_shutdown(struct net_device *dev)
719 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
720 shutdown_scheduler_queue(dev, &dev->rx_queue, &noop_qdisc);
721 WARN_ON(timer_pending(&dev->watchdog_timer));