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
->tx_queue
.lock
)
44 __acquires(dev
->rx_queue
.lock
)
46 spin_lock_bh(&dev
->tx_queue
.lock
);
47 spin_lock(&dev
->rx_queue
.lock
);
49 EXPORT_SYMBOL(qdisc_lock_tree
);
51 void qdisc_unlock_tree(struct net_device
*dev
)
52 __releases(dev
->rx_queue
.lock
)
53 __releases(dev
->tx_queue
.lock
)
55 spin_unlock(&dev
->rx_queue
.lock
);
56 spin_unlock_bh(&dev
->tx_queue
.lock
);
58 EXPORT_SYMBOL(qdisc_unlock_tree
);
60 static inline int qdisc_qlen(struct Qdisc
*q
)
65 static inline int dev_requeue_skb(struct sk_buff
*skb
, struct net_device
*dev
,
66 struct netdev_queue
*dev_queue
,
69 if (unlikely(skb
->next
))
70 dev_queue
->gso_skb
= skb
;
72 q
->ops
->requeue(skb
, q
);
78 static inline struct sk_buff
*dev_dequeue_skb(struct net_device
*dev
,
79 struct netdev_queue
*dev_queue
,
84 if ((skb
= dev_queue
->gso_skb
))
85 dev_queue
->gso_skb
= NULL
;
92 static inline int handle_dev_cpu_collision(struct sk_buff
*skb
,
93 struct net_device
*dev
,
94 struct netdev_queue
*dev_queue
,
99 if (unlikely(dev
->xmit_lock_owner
== smp_processor_id())) {
101 * Same CPU holding the lock. It may be a transient
102 * configuration error, when hard_start_xmit() recurses. We
103 * detect it by checking xmit owner and drop the packet when
104 * deadloop is detected. Return OK to try the next skb.
108 printk(KERN_WARNING
"Dead loop on netdevice %s, "
109 "fix it urgently!\n", dev
->name
);
113 * Another cpu is holding lock, requeue & delay xmits for
116 __get_cpu_var(netdev_rx_stat
).cpu_collision
++;
117 ret
= dev_requeue_skb(skb
, dev
, dev_queue
, q
);
124 * NOTE: Called under queue->lock with locally disabled BH.
126 * __LINK_STATE_QDISC_RUNNING guarantees only one CPU can process this
127 * device at a time. queue->lock serializes queue accesses for
128 * this device AND txq->qdisc pointer itself.
130 * netif_tx_lock serializes accesses to device driver.
132 * queue->lock and netif_tx_lock are mutually exclusive,
133 * if one is grabbed, another must be free.
135 * Note, that this procedure can be called by a watchdog timer
137 * Returns to the caller:
138 * 0 - queue is empty or throttled.
139 * >0 - queue is not empty.
142 static inline int qdisc_restart(struct net_device
*dev
)
144 struct netdev_queue
*txq
= &dev
->tx_queue
;
145 struct Qdisc
*q
= txq
->qdisc
;
147 int ret
= NETDEV_TX_BUSY
;
150 if (unlikely((skb
= dev_dequeue_skb(dev
, txq
, q
)) == NULL
))
154 /* And release queue */
155 spin_unlock(&txq
->lock
);
157 HARD_TX_LOCK(dev
, smp_processor_id());
158 if (!netif_subqueue_stopped(dev
, skb
))
159 ret
= dev_hard_start_xmit(skb
, dev
);
162 spin_lock(&txq
->lock
);
167 /* Driver sent out skb successfully */
171 case NETDEV_TX_LOCKED
:
172 /* Driver try lock failed */
173 ret
= handle_dev_cpu_collision(skb
, dev
, txq
, q
);
177 /* Driver returned NETDEV_TX_BUSY - requeue skb */
178 if (unlikely (ret
!= NETDEV_TX_BUSY
&& net_ratelimit()))
179 printk(KERN_WARNING
"BUG %s code %d qlen %d\n",
180 dev
->name
, ret
, q
->q
.qlen
);
182 ret
= dev_requeue_skb(skb
, dev
, txq
, q
);
189 void __qdisc_run(struct net_device
*dev
)
191 unsigned long start_time
= jiffies
;
193 while (qdisc_restart(dev
)) {
194 if (netif_queue_stopped(dev
))
198 * Postpone processing if
199 * 1. another process needs the CPU;
200 * 2. we've been doing it for too long.
202 if (need_resched() || jiffies
!= start_time
) {
208 clear_bit(__LINK_STATE_QDISC_RUNNING
, &dev
->state
);
211 static void dev_watchdog(unsigned long arg
)
213 struct net_device
*dev
= (struct net_device
*)arg
;
214 struct netdev_queue
*txq
= &dev
->tx_queue
;
217 if (txq
->qdisc
!= &noop_qdisc
) {
218 if (netif_device_present(dev
) &&
219 netif_running(dev
) &&
220 netif_carrier_ok(dev
)) {
221 if (netif_queue_stopped(dev
) &&
222 time_after(jiffies
, dev
->trans_start
+ dev
->watchdog_timeo
)) {
224 printk(KERN_INFO
"NETDEV WATCHDOG: %s: transmit timed out\n",
226 dev
->tx_timeout(dev
);
229 if (!mod_timer(&dev
->watchdog_timer
, round_jiffies(jiffies
+ dev
->watchdog_timeo
)))
233 netif_tx_unlock(dev
);
238 void __netdev_watchdog_up(struct net_device
*dev
)
240 if (dev
->tx_timeout
) {
241 if (dev
->watchdog_timeo
<= 0)
242 dev
->watchdog_timeo
= 5*HZ
;
243 if (!mod_timer(&dev
->watchdog_timer
,
244 round_jiffies(jiffies
+ dev
->watchdog_timeo
)))
249 static void dev_watchdog_up(struct net_device
*dev
)
251 __netdev_watchdog_up(dev
);
254 static void dev_watchdog_down(struct net_device
*dev
)
256 netif_tx_lock_bh(dev
);
257 if (del_timer(&dev
->watchdog_timer
))
259 netif_tx_unlock_bh(dev
);
263 * netif_carrier_on - set carrier
264 * @dev: network device
266 * Device has detected that carrier.
268 void netif_carrier_on(struct net_device
*dev
)
270 if (test_and_clear_bit(__LINK_STATE_NOCARRIER
, &dev
->state
)) {
271 linkwatch_fire_event(dev
);
272 if (netif_running(dev
))
273 __netdev_watchdog_up(dev
);
276 EXPORT_SYMBOL(netif_carrier_on
);
279 * netif_carrier_off - clear carrier
280 * @dev: network device
282 * Device has detected loss of carrier.
284 void netif_carrier_off(struct net_device
*dev
)
286 if (!test_and_set_bit(__LINK_STATE_NOCARRIER
, &dev
->state
))
287 linkwatch_fire_event(dev
);
289 EXPORT_SYMBOL(netif_carrier_off
);
291 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
292 under all circumstances. It is difficult to invent anything faster or
296 static int noop_enqueue(struct sk_buff
*skb
, struct Qdisc
* qdisc
)
302 static struct sk_buff
*noop_dequeue(struct Qdisc
* qdisc
)
307 static int noop_requeue(struct sk_buff
*skb
, struct Qdisc
* qdisc
)
310 printk(KERN_DEBUG
"%s deferred output. It is buggy.\n",
316 struct Qdisc_ops noop_qdisc_ops __read_mostly
= {
319 .enqueue
= noop_enqueue
,
320 .dequeue
= noop_dequeue
,
321 .requeue
= noop_requeue
,
322 .owner
= THIS_MODULE
,
325 struct Qdisc noop_qdisc
= {
326 .enqueue
= noop_enqueue
,
327 .dequeue
= noop_dequeue
,
328 .flags
= TCQ_F_BUILTIN
,
329 .ops
= &noop_qdisc_ops
,
330 .list
= LIST_HEAD_INIT(noop_qdisc
.list
),
332 EXPORT_SYMBOL(noop_qdisc
);
334 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly
= {
337 .enqueue
= noop_enqueue
,
338 .dequeue
= noop_dequeue
,
339 .requeue
= noop_requeue
,
340 .owner
= THIS_MODULE
,
343 static struct Qdisc noqueue_qdisc
= {
345 .dequeue
= noop_dequeue
,
346 .flags
= TCQ_F_BUILTIN
,
347 .ops
= &noqueue_qdisc_ops
,
348 .list
= LIST_HEAD_INIT(noqueue_qdisc
.list
),
352 static const u8 prio2band
[TC_PRIO_MAX
+1] =
353 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
355 /* 3-band FIFO queue: old style, but should be a bit faster than
356 generic prio+fifo combination.
359 #define PFIFO_FAST_BANDS 3
361 static inline struct sk_buff_head
*prio2list(struct sk_buff
*skb
,
364 struct sk_buff_head
*list
= qdisc_priv(qdisc
);
365 return list
+ prio2band
[skb
->priority
& TC_PRIO_MAX
];
368 static int pfifo_fast_enqueue(struct sk_buff
*skb
, struct Qdisc
* qdisc
)
370 struct sk_buff_head
*list
= prio2list(skb
, qdisc
);
372 if (skb_queue_len(list
) < qdisc_dev(qdisc
)->tx_queue_len
) {
374 return __qdisc_enqueue_tail(skb
, qdisc
, list
);
377 return qdisc_drop(skb
, qdisc
);
380 static struct sk_buff
*pfifo_fast_dequeue(struct Qdisc
* qdisc
)
383 struct sk_buff_head
*list
= qdisc_priv(qdisc
);
385 for (prio
= 0; prio
< PFIFO_FAST_BANDS
; prio
++) {
386 if (!skb_queue_empty(list
+ prio
)) {
388 return __qdisc_dequeue_head(qdisc
, list
+ prio
);
395 static int pfifo_fast_requeue(struct sk_buff
*skb
, struct Qdisc
* qdisc
)
398 return __qdisc_requeue(skb
, qdisc
, prio2list(skb
, qdisc
));
401 static void pfifo_fast_reset(struct Qdisc
* qdisc
)
404 struct sk_buff_head
*list
= qdisc_priv(qdisc
);
406 for (prio
= 0; prio
< PFIFO_FAST_BANDS
; prio
++)
407 __qdisc_reset_queue(qdisc
, list
+ prio
);
409 qdisc
->qstats
.backlog
= 0;
413 static int pfifo_fast_dump(struct Qdisc
*qdisc
, struct sk_buff
*skb
)
415 struct tc_prio_qopt opt
= { .bands
= PFIFO_FAST_BANDS
};
417 memcpy(&opt
.priomap
, prio2band
, TC_PRIO_MAX
+1);
418 NLA_PUT(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
);
425 static int pfifo_fast_init(struct Qdisc
*qdisc
, struct nlattr
*opt
)
428 struct sk_buff_head
*list
= qdisc_priv(qdisc
);
430 for (prio
= 0; prio
< PFIFO_FAST_BANDS
; prio
++)
431 skb_queue_head_init(list
+ prio
);
436 static struct Qdisc_ops pfifo_fast_ops __read_mostly
= {
438 .priv_size
= PFIFO_FAST_BANDS
* sizeof(struct sk_buff_head
),
439 .enqueue
= pfifo_fast_enqueue
,
440 .dequeue
= pfifo_fast_dequeue
,
441 .requeue
= pfifo_fast_requeue
,
442 .init
= pfifo_fast_init
,
443 .reset
= pfifo_fast_reset
,
444 .dump
= pfifo_fast_dump
,
445 .owner
= THIS_MODULE
,
448 struct Qdisc
*qdisc_alloc(struct netdev_queue
*dev_queue
,
449 struct Qdisc_ops
*ops
)
456 /* ensure that the Qdisc and the private data are 32-byte aligned */
457 size
= QDISC_ALIGN(sizeof(*sch
));
458 size
+= ops
->priv_size
+ (QDISC_ALIGNTO
- 1);
460 p
= kzalloc(size
, GFP_KERNEL
);
463 sch
= (struct Qdisc
*) QDISC_ALIGN((unsigned long) p
);
464 sch
->padded
= (char *) sch
- (char *) p
;
466 INIT_LIST_HEAD(&sch
->list
);
467 skb_queue_head_init(&sch
->q
);
469 sch
->enqueue
= ops
->enqueue
;
470 sch
->dequeue
= ops
->dequeue
;
471 sch
->dev_queue
= dev_queue
;
472 dev_hold(qdisc_dev(sch
));
473 atomic_set(&sch
->refcnt
, 1);
480 struct Qdisc
* qdisc_create_dflt(struct net_device
*dev
,
481 struct netdev_queue
*dev_queue
,
482 struct Qdisc_ops
*ops
,
483 unsigned int parentid
)
487 sch
= qdisc_alloc(dev_queue
, ops
);
490 sch
->parent
= parentid
;
492 if (!ops
->init
|| ops
->init(sch
, NULL
) == 0)
499 EXPORT_SYMBOL(qdisc_create_dflt
);
501 /* Under queue->lock and BH! */
503 void qdisc_reset(struct Qdisc
*qdisc
)
505 const struct Qdisc_ops
*ops
= qdisc
->ops
;
510 EXPORT_SYMBOL(qdisc_reset
);
512 /* this is the rcu callback function to clean up a qdisc when there
513 * are no further references to it */
515 static void __qdisc_destroy(struct rcu_head
*head
)
517 struct Qdisc
*qdisc
= container_of(head
, struct Qdisc
, q_rcu
);
518 kfree((char *) qdisc
- qdisc
->padded
);
521 /* Under queue->lock and BH! */
523 void qdisc_destroy(struct Qdisc
*qdisc
)
525 const struct Qdisc_ops
*ops
= qdisc
->ops
;
527 if (qdisc
->flags
& TCQ_F_BUILTIN
||
528 !atomic_dec_and_test(&qdisc
->refcnt
))
531 list_del(&qdisc
->list
);
532 gen_kill_estimator(&qdisc
->bstats
, &qdisc
->rate_est
);
538 module_put(ops
->owner
);
539 dev_put(qdisc_dev(qdisc
));
540 call_rcu(&qdisc
->q_rcu
, __qdisc_destroy
);
542 EXPORT_SYMBOL(qdisc_destroy
);
544 void dev_activate(struct net_device
*dev
)
546 struct netdev_queue
*txq
= &dev
->tx_queue
;
548 /* No queueing discipline is attached to device;
549 create default one i.e. pfifo_fast for devices,
550 which need queueing and noqueue_qdisc for
554 if (txq
->qdisc_sleeping
== &noop_qdisc
) {
556 if (dev
->tx_queue_len
) {
557 qdisc
= qdisc_create_dflt(dev
, txq
,
561 printk(KERN_INFO
"%s: activation failed\n", dev
->name
);
564 list_add_tail(&qdisc
->list
, &txq
->qdisc_list
);
566 qdisc
= &noqueue_qdisc
;
568 txq
->qdisc_sleeping
= qdisc
;
571 if (!netif_carrier_ok(dev
))
572 /* Delay activation until next carrier-on event */
575 spin_lock_bh(&txq
->lock
);
576 rcu_assign_pointer(txq
->qdisc
, txq
->qdisc_sleeping
);
577 if (txq
->qdisc
!= &noqueue_qdisc
) {
578 dev
->trans_start
= jiffies
;
579 dev_watchdog_up(dev
);
581 spin_unlock_bh(&txq
->lock
);
584 static void dev_deactivate_queue(struct netdev_queue
*dev_queue
,
585 struct Qdisc
*qdisc_default
)
590 spin_lock_bh(&dev_queue
->lock
);
592 qdisc
= dev_queue
->qdisc
;
594 dev_queue
->qdisc
= qdisc_default
;
597 skb
= dev_queue
->gso_skb
;
598 dev_queue
->gso_skb
= NULL
;
600 spin_unlock_bh(&dev_queue
->lock
);
605 void dev_deactivate(struct net_device
*dev
)
609 dev_deactivate_queue(&dev
->tx_queue
, &noop_qdisc
);
611 dev_watchdog_down(dev
);
613 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
616 /* Wait for outstanding qdisc_run calls. */
618 while (test_bit(__LINK_STATE_QDISC_RUNNING
, &dev
->state
))
622 * Double-check inside queue lock to ensure that all effects
623 * of the queue run are visible when we return.
625 spin_lock_bh(&dev
->tx_queue
.lock
);
626 running
= test_bit(__LINK_STATE_QDISC_RUNNING
, &dev
->state
);
627 spin_unlock_bh(&dev
->tx_queue
.lock
);
630 * The running flag should never be set at this point because
631 * we've already set dev->qdisc to noop_qdisc *inside* the same
632 * pair of spin locks. That is, if any qdisc_run starts after
633 * our initial test it should see the noop_qdisc and then
634 * clear the RUNNING bit before dropping the queue lock. So
635 * if it is set here then we've found a bug.
637 } while (WARN_ON_ONCE(running
));
640 static void dev_init_scheduler_queue(struct net_device
*dev
,
641 struct netdev_queue
*dev_queue
,
644 dev_queue
->qdisc
= qdisc
;
645 dev_queue
->qdisc_sleeping
= qdisc
;
646 INIT_LIST_HEAD(&dev_queue
->qdisc_list
);
649 void dev_init_scheduler(struct net_device
*dev
)
651 qdisc_lock_tree(dev
);
652 dev_init_scheduler_queue(dev
, &dev
->tx_queue
, &noop_qdisc
);
653 dev_init_scheduler_queue(dev
, &dev
->rx_queue
, NULL
);
654 qdisc_unlock_tree(dev
);
656 setup_timer(&dev
->watchdog_timer
, dev_watchdog
, (unsigned long)dev
);
659 static void dev_shutdown_scheduler_queue(struct net_device
*dev
,
660 struct netdev_queue
*dev_queue
,
661 struct Qdisc
*qdisc_default
)
663 struct Qdisc
*qdisc
= dev_queue
->qdisc_sleeping
;
666 dev_queue
->qdisc
= qdisc_default
;
667 dev_queue
->qdisc_sleeping
= qdisc_default
;
669 qdisc_destroy(qdisc
);
673 void dev_shutdown(struct net_device
*dev
)
675 qdisc_lock_tree(dev
);
676 dev_shutdown_scheduler_queue(dev
, &dev
->tx_queue
, &noop_qdisc
);
677 dev_shutdown_scheduler_queue(dev
, &dev
->rx_queue
, NULL
);
678 BUG_TRAP(!timer_pending(&dev
->watchdog_timer
));
679 qdisc_unlock_tree(dev
);