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
))
80 dev_queue
->gso_skb
= skb
;
82 q
->ops
->requeue(skb
, q
);
84 netif_schedule_queue(dev_queue
);
88 static inline struct sk_buff
*dequeue_skb(struct netdev_queue
*dev_queue
,
93 if ((skb
= dev_queue
->gso_skb
))
94 dev_queue
->gso_skb
= NULL
;
101 static inline int handle_dev_cpu_collision(struct sk_buff
*skb
,
102 struct netdev_queue
*dev_queue
,
107 if (unlikely(dev_queue
->xmit_lock_owner
== smp_processor_id())) {
109 * Same CPU holding the lock. It may be a transient
110 * configuration error, when hard_start_xmit() recurses. We
111 * detect it by checking xmit owner and drop the packet when
112 * deadloop is detected. Return OK to try the next skb.
116 printk(KERN_WARNING
"Dead loop on netdevice %s, "
117 "fix it urgently!\n", dev_queue
->dev
->name
);
121 * Another cpu is holding lock, requeue & delay xmits for
124 __get_cpu_var(netdev_rx_stat
).cpu_collision
++;
125 ret
= dev_requeue_skb(skb
, dev_queue
, q
);
132 * NOTE: Called under queue->lock with locally disabled BH.
134 * __QUEUE_STATE_QDISC_RUNNING guarantees only one CPU can process
135 * this queue at a time. queue->lock serializes queue accesses for
136 * this queue AND txq->qdisc pointer itself.
138 * netif_tx_lock serializes accesses to device driver.
140 * queue->lock and netif_tx_lock are mutually exclusive,
141 * if one is grabbed, another must be free.
143 * Note, that this procedure can be called by a watchdog timer
145 * Returns to the caller:
146 * 0 - queue is empty or throttled.
147 * >0 - queue is not empty.
150 static inline int qdisc_restart(struct netdev_queue
*txq
)
152 struct Qdisc
*q
= txq
->qdisc
;
153 int ret
= NETDEV_TX_BUSY
;
154 struct net_device
*dev
;
158 if (unlikely((skb
= dequeue_skb(txq
, q
)) == NULL
))
162 /* And release queue */
163 spin_unlock(&txq
->lock
);
167 HARD_TX_LOCK(dev
, txq
, smp_processor_id());
168 if (!netif_subqueue_stopped(dev
, skb
))
169 ret
= dev_hard_start_xmit(skb
, dev
);
170 HARD_TX_UNLOCK(dev
, txq
);
172 spin_lock(&txq
->lock
);
177 /* Driver sent out skb successfully */
181 case NETDEV_TX_LOCKED
:
182 /* Driver try lock failed */
183 ret
= handle_dev_cpu_collision(skb
, txq
, q
);
187 /* Driver returned NETDEV_TX_BUSY - requeue skb */
188 if (unlikely (ret
!= NETDEV_TX_BUSY
&& net_ratelimit()))
189 printk(KERN_WARNING
"BUG %s code %d qlen %d\n",
190 dev
->name
, ret
, q
->q
.qlen
);
192 ret
= dev_requeue_skb(skb
, txq
, q
);
199 void __qdisc_run(struct netdev_queue
*txq
)
201 struct net_device
*dev
= txq
->dev
;
202 unsigned long start_time
= jiffies
;
204 while (qdisc_restart(txq
)) {
205 if (netif_queue_stopped(dev
))
209 * Postpone processing if
210 * 1. another process needs the CPU;
211 * 2. we've been doing it for too long.
213 if (need_resched() || jiffies
!= start_time
) {
214 netif_schedule_queue(txq
);
219 clear_bit(__QUEUE_STATE_QDISC_RUNNING
, &txq
->state
);
222 static void dev_watchdog(unsigned long arg
)
224 struct net_device
*dev
= (struct net_device
*)arg
;
227 if (!qdisc_tx_is_noop(dev
)) {
228 if (netif_device_present(dev
) &&
229 netif_running(dev
) &&
230 netif_carrier_ok(dev
)) {
231 int some_queue_stopped
= 0;
234 for (i
= 0; i
< dev
->num_tx_queues
; i
++) {
235 struct netdev_queue
*txq
;
237 txq
= netdev_get_tx_queue(dev
, i
);
238 if (netif_tx_queue_stopped(txq
)) {
239 some_queue_stopped
= 1;
244 if (some_queue_stopped
&&
245 time_after(jiffies
, (dev
->trans_start
+
246 dev
->watchdog_timeo
))) {
247 printk(KERN_INFO
"NETDEV WATCHDOG: %s: "
248 "transmit timed out\n",
250 dev
->tx_timeout(dev
);
253 if (!mod_timer(&dev
->watchdog_timer
,
254 round_jiffies(jiffies
+
255 dev
->watchdog_timeo
)))
259 netif_tx_unlock(dev
);
264 void __netdev_watchdog_up(struct net_device
*dev
)
266 if (dev
->tx_timeout
) {
267 if (dev
->watchdog_timeo
<= 0)
268 dev
->watchdog_timeo
= 5*HZ
;
269 if (!mod_timer(&dev
->watchdog_timer
,
270 round_jiffies(jiffies
+ dev
->watchdog_timeo
)))
275 static void dev_watchdog_up(struct net_device
*dev
)
277 __netdev_watchdog_up(dev
);
280 static void dev_watchdog_down(struct net_device
*dev
)
282 netif_tx_lock_bh(dev
);
283 if (del_timer(&dev
->watchdog_timer
))
285 netif_tx_unlock_bh(dev
);
289 * netif_carrier_on - set carrier
290 * @dev: network device
292 * Device has detected that carrier.
294 void netif_carrier_on(struct net_device
*dev
)
296 if (test_and_clear_bit(__LINK_STATE_NOCARRIER
, &dev
->state
)) {
297 linkwatch_fire_event(dev
);
298 if (netif_running(dev
))
299 __netdev_watchdog_up(dev
);
302 EXPORT_SYMBOL(netif_carrier_on
);
305 * netif_carrier_off - clear carrier
306 * @dev: network device
308 * Device has detected loss of carrier.
310 void netif_carrier_off(struct net_device
*dev
)
312 if (!test_and_set_bit(__LINK_STATE_NOCARRIER
, &dev
->state
))
313 linkwatch_fire_event(dev
);
315 EXPORT_SYMBOL(netif_carrier_off
);
317 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
318 under all circumstances. It is difficult to invent anything faster or
322 static int noop_enqueue(struct sk_buff
*skb
, struct Qdisc
* qdisc
)
328 static struct sk_buff
*noop_dequeue(struct Qdisc
* qdisc
)
333 static int noop_requeue(struct sk_buff
*skb
, struct Qdisc
* qdisc
)
336 printk(KERN_DEBUG
"%s deferred output. It is buggy.\n",
342 struct Qdisc_ops noop_qdisc_ops __read_mostly
= {
345 .enqueue
= noop_enqueue
,
346 .dequeue
= noop_dequeue
,
347 .requeue
= noop_requeue
,
348 .owner
= THIS_MODULE
,
351 struct Qdisc noop_qdisc
= {
352 .enqueue
= noop_enqueue
,
353 .dequeue
= noop_dequeue
,
354 .flags
= TCQ_F_BUILTIN
,
355 .ops
= &noop_qdisc_ops
,
356 .list
= LIST_HEAD_INIT(noop_qdisc
.list
),
358 EXPORT_SYMBOL(noop_qdisc
);
360 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly
= {
363 .enqueue
= noop_enqueue
,
364 .dequeue
= noop_dequeue
,
365 .requeue
= noop_requeue
,
366 .owner
= THIS_MODULE
,
369 static struct Qdisc noqueue_qdisc
= {
371 .dequeue
= noop_dequeue
,
372 .flags
= TCQ_F_BUILTIN
,
373 .ops
= &noqueue_qdisc_ops
,
374 .list
= LIST_HEAD_INIT(noqueue_qdisc
.list
),
378 static const u8 prio2band
[TC_PRIO_MAX
+1] =
379 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
381 /* 3-band FIFO queue: old style, but should be a bit faster than
382 generic prio+fifo combination.
385 #define PFIFO_FAST_BANDS 3
387 static inline struct sk_buff_head
*prio2list(struct sk_buff
*skb
,
390 struct sk_buff_head
*list
= qdisc_priv(qdisc
);
391 return list
+ prio2band
[skb
->priority
& TC_PRIO_MAX
];
394 static int pfifo_fast_enqueue(struct sk_buff
*skb
, struct Qdisc
* qdisc
)
396 struct sk_buff_head
*list
= prio2list(skb
, qdisc
);
398 if (skb_queue_len(list
) < qdisc_dev(qdisc
)->tx_queue_len
) {
400 return __qdisc_enqueue_tail(skb
, qdisc
, list
);
403 return qdisc_drop(skb
, qdisc
);
406 static struct sk_buff
*pfifo_fast_dequeue(struct Qdisc
* qdisc
)
409 struct sk_buff_head
*list
= qdisc_priv(qdisc
);
411 for (prio
= 0; prio
< PFIFO_FAST_BANDS
; prio
++) {
412 if (!skb_queue_empty(list
+ prio
)) {
414 return __qdisc_dequeue_head(qdisc
, list
+ prio
);
421 static int pfifo_fast_requeue(struct sk_buff
*skb
, struct Qdisc
* qdisc
)
424 return __qdisc_requeue(skb
, qdisc
, prio2list(skb
, qdisc
));
427 static void pfifo_fast_reset(struct Qdisc
* qdisc
)
430 struct sk_buff_head
*list
= qdisc_priv(qdisc
);
432 for (prio
= 0; prio
< PFIFO_FAST_BANDS
; prio
++)
433 __qdisc_reset_queue(qdisc
, list
+ prio
);
435 qdisc
->qstats
.backlog
= 0;
439 static int pfifo_fast_dump(struct Qdisc
*qdisc
, struct sk_buff
*skb
)
441 struct tc_prio_qopt opt
= { .bands
= PFIFO_FAST_BANDS
};
443 memcpy(&opt
.priomap
, prio2band
, TC_PRIO_MAX
+1);
444 NLA_PUT(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
);
451 static int pfifo_fast_init(struct Qdisc
*qdisc
, struct nlattr
*opt
)
454 struct sk_buff_head
*list
= qdisc_priv(qdisc
);
456 for (prio
= 0; prio
< PFIFO_FAST_BANDS
; prio
++)
457 skb_queue_head_init(list
+ prio
);
462 static struct Qdisc_ops pfifo_fast_ops __read_mostly
= {
464 .priv_size
= PFIFO_FAST_BANDS
* sizeof(struct sk_buff_head
),
465 .enqueue
= pfifo_fast_enqueue
,
466 .dequeue
= pfifo_fast_dequeue
,
467 .requeue
= pfifo_fast_requeue
,
468 .init
= pfifo_fast_init
,
469 .reset
= pfifo_fast_reset
,
470 .dump
= pfifo_fast_dump
,
471 .owner
= THIS_MODULE
,
474 struct Qdisc
*qdisc_alloc(struct netdev_queue
*dev_queue
,
475 struct Qdisc_ops
*ops
)
482 /* ensure that the Qdisc and the private data are 32-byte aligned */
483 size
= QDISC_ALIGN(sizeof(*sch
));
484 size
+= ops
->priv_size
+ (QDISC_ALIGNTO
- 1);
486 p
= kzalloc(size
, GFP_KERNEL
);
489 sch
= (struct Qdisc
*) QDISC_ALIGN((unsigned long) p
);
490 sch
->padded
= (char *) sch
- (char *) p
;
492 INIT_LIST_HEAD(&sch
->list
);
493 skb_queue_head_init(&sch
->q
);
495 sch
->enqueue
= ops
->enqueue
;
496 sch
->dequeue
= ops
->dequeue
;
497 sch
->dev_queue
= dev_queue
;
498 dev_hold(qdisc_dev(sch
));
499 atomic_set(&sch
->refcnt
, 1);
506 struct Qdisc
* qdisc_create_dflt(struct net_device
*dev
,
507 struct netdev_queue
*dev_queue
,
508 struct Qdisc_ops
*ops
,
509 unsigned int parentid
)
513 sch
= qdisc_alloc(dev_queue
, ops
);
516 sch
->parent
= parentid
;
518 if (!ops
->init
|| ops
->init(sch
, NULL
) == 0)
525 EXPORT_SYMBOL(qdisc_create_dflt
);
527 /* Under queue->lock and BH! */
529 void qdisc_reset(struct Qdisc
*qdisc
)
531 const struct Qdisc_ops
*ops
= qdisc
->ops
;
536 EXPORT_SYMBOL(qdisc_reset
);
538 /* this is the rcu callback function to clean up a qdisc when there
539 * are no further references to it */
541 static void __qdisc_destroy(struct rcu_head
*head
)
543 struct Qdisc
*qdisc
= container_of(head
, struct Qdisc
, q_rcu
);
544 kfree((char *) qdisc
- qdisc
->padded
);
547 /* Under queue->lock and BH! */
549 void qdisc_destroy(struct Qdisc
*qdisc
)
551 const struct Qdisc_ops
*ops
= qdisc
->ops
;
553 if (qdisc
->flags
& TCQ_F_BUILTIN
||
554 !atomic_dec_and_test(&qdisc
->refcnt
))
557 list_del(&qdisc
->list
);
558 gen_kill_estimator(&qdisc
->bstats
, &qdisc
->rate_est
);
564 module_put(ops
->owner
);
565 dev_put(qdisc_dev(qdisc
));
566 call_rcu(&qdisc
->q_rcu
, __qdisc_destroy
);
568 EXPORT_SYMBOL(qdisc_destroy
);
570 static bool dev_all_qdisc_sleeping_noop(struct net_device
*dev
)
574 for (i
= 0; i
< dev
->num_tx_queues
; i
++) {
575 struct netdev_queue
*txq
= netdev_get_tx_queue(dev
, i
);
577 if (txq
->qdisc_sleeping
!= &noop_qdisc
)
583 static void attach_one_default_qdisc(struct net_device
*dev
,
584 struct netdev_queue
*dev_queue
,
589 if (dev
->tx_queue_len
) {
590 qdisc
= qdisc_create_dflt(dev
, dev_queue
,
591 &pfifo_fast_ops
, TC_H_ROOT
);
593 printk(KERN_INFO
"%s: activation failed\n", dev
->name
);
596 list_add_tail(&qdisc
->list
, &dev_queue
->qdisc_list
);
598 qdisc
= &noqueue_qdisc
;
600 dev_queue
->qdisc_sleeping
= qdisc
;
603 static void transition_one_qdisc(struct net_device
*dev
,
604 struct netdev_queue
*dev_queue
,
605 void *_need_watchdog
)
607 int *need_watchdog_p
= _need_watchdog
;
609 spin_lock_bh(&dev_queue
->lock
);
610 rcu_assign_pointer(dev_queue
->qdisc
, dev_queue
->qdisc_sleeping
);
611 if (dev_queue
->qdisc
!= &noqueue_qdisc
)
612 *need_watchdog_p
= 1;
613 spin_unlock_bh(&dev_queue
->lock
);
616 void dev_activate(struct net_device
*dev
)
620 /* No queueing discipline is attached to device;
621 create default one i.e. pfifo_fast for devices,
622 which need queueing and noqueue_qdisc for
626 if (dev_all_qdisc_sleeping_noop(dev
))
627 netdev_for_each_tx_queue(dev
, attach_one_default_qdisc
, NULL
);
629 if (!netif_carrier_ok(dev
))
630 /* Delay activation until next carrier-on event */
634 netdev_for_each_tx_queue(dev
, transition_one_qdisc
, &need_watchdog
);
637 dev
->trans_start
= jiffies
;
638 dev_watchdog_up(dev
);
642 static void dev_deactivate_queue(struct net_device
*dev
,
643 struct netdev_queue
*dev_queue
,
644 void *_qdisc_default
)
646 struct Qdisc
*qdisc_default
= _qdisc_default
;
650 spin_lock_bh(&dev_queue
->lock
);
652 qdisc
= dev_queue
->qdisc
;
654 dev_queue
->qdisc
= qdisc_default
;
657 skb
= dev_queue
->gso_skb
;
658 dev_queue
->gso_skb
= NULL
;
660 spin_unlock_bh(&dev_queue
->lock
);
665 static bool some_qdisc_is_running(struct net_device
*dev
, int lock
)
669 for (i
= 0; i
< dev
->num_tx_queues
; i
++) {
670 struct netdev_queue
*dev_queue
;
673 dev_queue
= netdev_get_tx_queue(dev
, i
);
676 spin_lock_bh(&dev_queue
->lock
);
678 val
= test_bit(__QUEUE_STATE_QDISC_RUNNING
, &dev_queue
->state
);
681 spin_unlock_bh(&dev_queue
->lock
);
689 void dev_deactivate(struct net_device
*dev
)
693 netdev_for_each_tx_queue(dev
, dev_deactivate_queue
, &noop_qdisc
);
695 dev_watchdog_down(dev
);
697 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
700 /* Wait for outstanding qdisc_run calls. */
702 while (some_qdisc_is_running(dev
, 0))
706 * Double-check inside queue lock to ensure that all effects
707 * of the queue run are visible when we return.
709 running
= some_qdisc_is_running(dev
, 1);
712 * The running flag should never be set at this point because
713 * we've already set dev->qdisc to noop_qdisc *inside* the same
714 * pair of spin locks. That is, if any qdisc_run starts after
715 * our initial test it should see the noop_qdisc and then
716 * clear the RUNNING bit before dropping the queue lock. So
717 * if it is set here then we've found a bug.
719 } while (WARN_ON_ONCE(running
));
722 static void dev_init_scheduler_queue(struct net_device
*dev
,
723 struct netdev_queue
*dev_queue
,
726 struct Qdisc
*qdisc
= _qdisc
;
728 dev_queue
->qdisc
= qdisc
;
729 dev_queue
->qdisc_sleeping
= qdisc
;
730 INIT_LIST_HEAD(&dev_queue
->qdisc_list
);
733 void dev_init_scheduler(struct net_device
*dev
)
735 qdisc_lock_tree(dev
);
736 netdev_for_each_tx_queue(dev
, dev_init_scheduler_queue
, &noop_qdisc
);
737 dev_init_scheduler_queue(dev
, &dev
->rx_queue
, NULL
);
738 qdisc_unlock_tree(dev
);
740 setup_timer(&dev
->watchdog_timer
, dev_watchdog
, (unsigned long)dev
);
743 static void shutdown_scheduler_queue(struct net_device
*dev
,
744 struct netdev_queue
*dev_queue
,
745 void *_qdisc_default
)
747 struct Qdisc
*qdisc
= dev_queue
->qdisc_sleeping
;
748 struct Qdisc
*qdisc_default
= _qdisc_default
;
751 dev_queue
->qdisc
= qdisc_default
;
752 dev_queue
->qdisc_sleeping
= qdisc_default
;
754 qdisc_destroy(qdisc
);
758 void dev_shutdown(struct net_device
*dev
)
760 qdisc_lock_tree(dev
);
761 netdev_for_each_tx_queue(dev
, shutdown_scheduler_queue
, &noop_qdisc
);
762 shutdown_scheduler_queue(dev
, &dev
->rx_queue
, NULL
);
763 BUG_TRAP(!timer_pending(&dev
->watchdog_timer
));
764 qdisc_unlock_tree(dev
);