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 <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <linux/bitops.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
26 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/netdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/init.h>
32 #include <linux/rcupdate.h>
33 #include <linux/list.h>
35 #include <net/pkt_sched.h>
37 /* Main transmission queue. */
39 /* Modifications to data participating in scheduling must be protected with
40 * dev->queue_lock spinlock.
42 * The idea is the following:
43 * - enqueue, dequeue are serialized via top level device
44 * spinlock dev->queue_lock.
45 * - ingress filtering is serialized via top level device
46 * spinlock dev->ingress_lock.
47 * - updates to tree and tree walking are only done under the rtnl mutex.
50 void qdisc_lock_tree(struct net_device
*dev
)
52 spin_lock_bh(&dev
->queue_lock
);
53 spin_lock(&dev
->ingress_lock
);
56 void qdisc_unlock_tree(struct net_device
*dev
)
58 spin_unlock(&dev
->ingress_lock
);
59 spin_unlock_bh(&dev
->queue_lock
);
62 static inline int qdisc_qlen(struct Qdisc
*q
)
64 BUG_ON((int) q
->q
.qlen
< 0);
68 static inline int dev_requeue_skb(struct sk_buff
*skb
, struct net_device
*dev
,
71 if (unlikely(skb
->next
))
74 q
->ops
->requeue(skb
, q
);
80 static inline struct sk_buff
*dev_dequeue_skb(struct net_device
*dev
,
85 if ((skb
= dev
->gso_skb
))
93 static inline int handle_dev_cpu_collision(struct sk_buff
*skb
,
94 struct net_device
*dev
,
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
, q
);
124 * NOTE: Called under dev->queue_lock with locally disabled BH.
126 * __LINK_STATE_QDISC_RUNNING guarantees only one CPU can process this
127 * device at a time. dev->queue_lock serializes queue accesses for
128 * this device AND dev->qdisc pointer itself.
130 * netif_tx_lock serializes accesses to device driver.
132 * dev->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 Qdisc
*q
= dev
->qdisc
;
150 if (unlikely((skb
= dev_dequeue_skb(dev
, q
)) == NULL
))
154 * When the driver has LLTX set, it does its own locking in
155 * start_xmit. These checks are worth it because even uncongested
156 * locks can be quite expensive. The driver can do a trylock, as
157 * is being done here; in case of lock contention it should return
158 * NETDEV_TX_LOCKED and the packet will be requeued.
160 lockless
= (dev
->features
& NETIF_F_LLTX
);
162 if (!lockless
&& !netif_tx_trylock(dev
)) {
163 /* Another CPU grabbed the driver tx lock */
164 return handle_dev_cpu_collision(skb
, dev
, q
);
167 /* And release queue */
168 spin_unlock(&dev
->queue_lock
);
170 ret
= NETDEV_TX_BUSY
;
171 if (!netif_queue_stopped(dev
))
172 ret
= dev_hard_start_xmit(skb
, dev
);
175 netif_tx_unlock(dev
);
177 spin_lock(&dev
->queue_lock
);
182 /* Driver sent out skb successfully */
186 case NETDEV_TX_LOCKED
:
187 /* Driver try lock failed */
188 ret
= handle_dev_cpu_collision(skb
, dev
, q
);
192 /* Driver returned NETDEV_TX_BUSY - requeue skb */
193 if (unlikely (ret
!= NETDEV_TX_BUSY
&& net_ratelimit()))
194 printk(KERN_WARNING
"BUG %s code %d qlen %d\n",
195 dev
->name
, ret
, q
->q
.qlen
);
197 ret
= dev_requeue_skb(skb
, dev
, q
);
204 void __qdisc_run(struct net_device
*dev
)
207 if (!qdisc_restart(dev
))
209 } while (!netif_queue_stopped(dev
));
211 clear_bit(__LINK_STATE_QDISC_RUNNING
, &dev
->state
);
214 static void dev_watchdog(unsigned long arg
)
216 struct net_device
*dev
= (struct net_device
*)arg
;
219 if (dev
->qdisc
!= &noop_qdisc
) {
220 if (netif_device_present(dev
) &&
221 netif_running(dev
) &&
222 netif_carrier_ok(dev
)) {
223 if (netif_queue_stopped(dev
) &&
224 time_after(jiffies
, dev
->trans_start
+ dev
->watchdog_timeo
)) {
226 printk(KERN_INFO
"NETDEV WATCHDOG: %s: transmit timed out\n",
228 dev
->tx_timeout(dev
);
230 if (!mod_timer(&dev
->watchdog_timer
, round_jiffies(jiffies
+ dev
->watchdog_timeo
)))
234 netif_tx_unlock(dev
);
239 static void dev_watchdog_init(struct net_device
*dev
)
241 init_timer(&dev
->watchdog_timer
);
242 dev
->watchdog_timer
.data
= (unsigned long)dev
;
243 dev
->watchdog_timer
.function
= dev_watchdog
;
246 void __netdev_watchdog_up(struct net_device
*dev
)
248 if (dev
->tx_timeout
) {
249 if (dev
->watchdog_timeo
<= 0)
250 dev
->watchdog_timeo
= 5*HZ
;
251 if (!mod_timer(&dev
->watchdog_timer
,
252 round_jiffies(jiffies
+ dev
->watchdog_timeo
)))
257 static void dev_watchdog_up(struct net_device
*dev
)
259 __netdev_watchdog_up(dev
);
262 static void dev_watchdog_down(struct net_device
*dev
)
264 netif_tx_lock_bh(dev
);
265 if (del_timer(&dev
->watchdog_timer
))
267 netif_tx_unlock_bh(dev
);
270 void netif_carrier_on(struct net_device
*dev
)
272 if (test_and_clear_bit(__LINK_STATE_NOCARRIER
, &dev
->state
))
273 linkwatch_fire_event(dev
);
274 if (netif_running(dev
))
275 __netdev_watchdog_up(dev
);
278 void netif_carrier_off(struct net_device
*dev
)
280 if (!test_and_set_bit(__LINK_STATE_NOCARRIER
, &dev
->state
))
281 linkwatch_fire_event(dev
);
284 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
285 under all circumstances. It is difficult to invent anything faster or
289 static int noop_enqueue(struct sk_buff
*skb
, struct Qdisc
* qdisc
)
295 static struct sk_buff
*noop_dequeue(struct Qdisc
* qdisc
)
300 static int noop_requeue(struct sk_buff
*skb
, struct Qdisc
* qdisc
)
303 printk(KERN_DEBUG
"%s deferred output. It is buggy.\n",
309 struct Qdisc_ops noop_qdisc_ops
= {
312 .enqueue
= noop_enqueue
,
313 .dequeue
= noop_dequeue
,
314 .requeue
= noop_requeue
,
315 .owner
= THIS_MODULE
,
318 struct Qdisc noop_qdisc
= {
319 .enqueue
= noop_enqueue
,
320 .dequeue
= noop_dequeue
,
321 .flags
= TCQ_F_BUILTIN
,
322 .ops
= &noop_qdisc_ops
,
323 .list
= LIST_HEAD_INIT(noop_qdisc
.list
),
326 static struct Qdisc_ops noqueue_qdisc_ops
= {
329 .enqueue
= noop_enqueue
,
330 .dequeue
= noop_dequeue
,
331 .requeue
= noop_requeue
,
332 .owner
= THIS_MODULE
,
335 static struct Qdisc noqueue_qdisc
= {
337 .dequeue
= noop_dequeue
,
338 .flags
= TCQ_F_BUILTIN
,
339 .ops
= &noqueue_qdisc_ops
,
340 .list
= LIST_HEAD_INIT(noqueue_qdisc
.list
),
344 static const u8 prio2band
[TC_PRIO_MAX
+1] =
345 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
347 /* 3-band FIFO queue: old style, but should be a bit faster than
348 generic prio+fifo combination.
351 #define PFIFO_FAST_BANDS 3
353 static inline struct sk_buff_head
*prio2list(struct sk_buff
*skb
,
356 struct sk_buff_head
*list
= qdisc_priv(qdisc
);
357 return list
+ prio2band
[skb
->priority
& TC_PRIO_MAX
];
360 static int pfifo_fast_enqueue(struct sk_buff
*skb
, struct Qdisc
* qdisc
)
362 struct sk_buff_head
*list
= prio2list(skb
, qdisc
);
364 if (skb_queue_len(list
) < qdisc
->dev
->tx_queue_len
) {
366 return __qdisc_enqueue_tail(skb
, qdisc
, list
);
369 return qdisc_drop(skb
, qdisc
);
372 static struct sk_buff
*pfifo_fast_dequeue(struct Qdisc
* qdisc
)
375 struct sk_buff_head
*list
= qdisc_priv(qdisc
);
377 for (prio
= 0; prio
< PFIFO_FAST_BANDS
; prio
++) {
378 if (!skb_queue_empty(list
+ prio
)) {
380 return __qdisc_dequeue_head(qdisc
, list
+ prio
);
387 static int pfifo_fast_requeue(struct sk_buff
*skb
, struct Qdisc
* qdisc
)
390 return __qdisc_requeue(skb
, qdisc
, prio2list(skb
, qdisc
));
393 static void pfifo_fast_reset(struct Qdisc
* qdisc
)
396 struct sk_buff_head
*list
= qdisc_priv(qdisc
);
398 for (prio
= 0; prio
< PFIFO_FAST_BANDS
; prio
++)
399 __qdisc_reset_queue(qdisc
, list
+ prio
);
401 qdisc
->qstats
.backlog
= 0;
405 static int pfifo_fast_dump(struct Qdisc
*qdisc
, struct sk_buff
*skb
)
407 struct tc_prio_qopt opt
= { .bands
= PFIFO_FAST_BANDS
};
409 memcpy(&opt
.priomap
, prio2band
, TC_PRIO_MAX
+1);
410 RTA_PUT(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
);
417 static int pfifo_fast_init(struct Qdisc
*qdisc
, struct rtattr
*opt
)
420 struct sk_buff_head
*list
= qdisc_priv(qdisc
);
422 for (prio
= 0; prio
< PFIFO_FAST_BANDS
; prio
++)
423 skb_queue_head_init(list
+ prio
);
428 static struct Qdisc_ops pfifo_fast_ops
= {
430 .priv_size
= PFIFO_FAST_BANDS
* sizeof(struct sk_buff_head
),
431 .enqueue
= pfifo_fast_enqueue
,
432 .dequeue
= pfifo_fast_dequeue
,
433 .requeue
= pfifo_fast_requeue
,
434 .init
= pfifo_fast_init
,
435 .reset
= pfifo_fast_reset
,
436 .dump
= pfifo_fast_dump
,
437 .owner
= THIS_MODULE
,
440 struct Qdisc
*qdisc_alloc(struct net_device
*dev
, struct Qdisc_ops
*ops
)
447 /* ensure that the Qdisc and the private data are 32-byte aligned */
448 size
= QDISC_ALIGN(sizeof(*sch
));
449 size
+= ops
->priv_size
+ (QDISC_ALIGNTO
- 1);
451 p
= kzalloc(size
, GFP_KERNEL
);
454 sch
= (struct Qdisc
*) QDISC_ALIGN((unsigned long) p
);
455 sch
->padded
= (char *) sch
- (char *) p
;
457 INIT_LIST_HEAD(&sch
->list
);
458 skb_queue_head_init(&sch
->q
);
460 sch
->enqueue
= ops
->enqueue
;
461 sch
->dequeue
= ops
->dequeue
;
464 atomic_set(&sch
->refcnt
, 1);
468 return ERR_PTR(-err
);
471 struct Qdisc
* qdisc_create_dflt(struct net_device
*dev
, struct Qdisc_ops
*ops
,
472 unsigned int parentid
)
476 sch
= qdisc_alloc(dev
, ops
);
479 sch
->stats_lock
= &dev
->queue_lock
;
480 sch
->parent
= parentid
;
482 if (!ops
->init
|| ops
->init(sch
, NULL
) == 0)
490 /* Under dev->queue_lock and BH! */
492 void qdisc_reset(struct Qdisc
*qdisc
)
494 struct Qdisc_ops
*ops
= qdisc
->ops
;
500 /* this is the rcu callback function to clean up a qdisc when there
501 * are no further references to it */
503 static void __qdisc_destroy(struct rcu_head
*head
)
505 struct Qdisc
*qdisc
= container_of(head
, struct Qdisc
, q_rcu
);
506 kfree((char *) qdisc
- qdisc
->padded
);
509 /* Under dev->queue_lock and BH! */
511 void qdisc_destroy(struct Qdisc
*qdisc
)
513 struct Qdisc_ops
*ops
= qdisc
->ops
;
515 if (qdisc
->flags
& TCQ_F_BUILTIN
||
516 !atomic_dec_and_test(&qdisc
->refcnt
))
519 list_del(&qdisc
->list
);
520 #ifdef CONFIG_NET_ESTIMATOR
521 gen_kill_estimator(&qdisc
->bstats
, &qdisc
->rate_est
);
528 module_put(ops
->owner
);
530 call_rcu(&qdisc
->q_rcu
, __qdisc_destroy
);
533 void dev_activate(struct net_device
*dev
)
535 /* No queueing discipline is attached to device;
536 create default one i.e. pfifo_fast for devices,
537 which need queueing and noqueue_qdisc for
541 if (dev
->qdisc_sleeping
== &noop_qdisc
) {
543 if (dev
->tx_queue_len
) {
544 qdisc
= qdisc_create_dflt(dev
, &pfifo_fast_ops
,
547 printk(KERN_INFO
"%s: activation failed\n", dev
->name
);
550 list_add_tail(&qdisc
->list
, &dev
->qdisc_list
);
552 qdisc
= &noqueue_qdisc
;
554 dev
->qdisc_sleeping
= qdisc
;
557 if (!netif_carrier_ok(dev
))
558 /* Delay activation until next carrier-on event */
561 spin_lock_bh(&dev
->queue_lock
);
562 rcu_assign_pointer(dev
->qdisc
, dev
->qdisc_sleeping
);
563 if (dev
->qdisc
!= &noqueue_qdisc
) {
564 dev
->trans_start
= jiffies
;
565 dev_watchdog_up(dev
);
567 spin_unlock_bh(&dev
->queue_lock
);
570 void dev_deactivate(struct net_device
*dev
)
575 spin_lock_bh(&dev
->queue_lock
);
577 dev
->qdisc
= &noop_qdisc
;
583 spin_unlock_bh(&dev
->queue_lock
);
587 dev_watchdog_down(dev
);
589 /* Wait for outstanding dev_queue_xmit calls. */
592 /* Wait for outstanding qdisc_run calls. */
593 while (test_bit(__LINK_STATE_QDISC_RUNNING
, &dev
->state
))
597 void dev_init_scheduler(struct net_device
*dev
)
599 qdisc_lock_tree(dev
);
600 dev
->qdisc
= &noop_qdisc
;
601 dev
->qdisc_sleeping
= &noop_qdisc
;
602 INIT_LIST_HEAD(&dev
->qdisc_list
);
603 qdisc_unlock_tree(dev
);
605 dev_watchdog_init(dev
);
608 void dev_shutdown(struct net_device
*dev
)
612 qdisc_lock_tree(dev
);
613 qdisc
= dev
->qdisc_sleeping
;
614 dev
->qdisc
= &noop_qdisc
;
615 dev
->qdisc_sleeping
= &noop_qdisc
;
616 qdisc_destroy(qdisc
);
617 #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
618 if ((qdisc
= dev
->qdisc_ingress
) != NULL
) {
619 dev
->qdisc_ingress
= NULL
;
620 qdisc_destroy(qdisc
);
623 BUG_TRAP(!timer_pending(&dev
->watchdog_timer
));
624 qdisc_unlock_tree(dev
);
627 EXPORT_SYMBOL(netif_carrier_on
);
628 EXPORT_SYMBOL(netif_carrier_off
);
629 EXPORT_SYMBOL(noop_qdisc
);
630 EXPORT_SYMBOL(qdisc_create_dflt
);
631 EXPORT_SYMBOL(qdisc_destroy
);
632 EXPORT_SYMBOL(qdisc_reset
);
633 EXPORT_SYMBOL(qdisc_lock_tree
);
634 EXPORT_SYMBOL(qdisc_unlock_tree
);