blob: b13821ad2fb64f0f881d22991a2ec5be369f6ed5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/sch_generic.c Generic packet scheduler routines.
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * Jamal Hadi Salim, <hadi@cyberus.ca> 990601
11 * - Ingress support
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
18#include <linux/sched.h>
19#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/pkt_sched.h>
28
29/* Main transmission queue. */
30
Patrick McHardy0463d4a2007-04-16 17:02:10 -070031/* Modifications to data participating in scheduling must be protected with
David S. Miller5fb66222008-08-02 20:02:43 -070032 * qdisc_lock(qdisc) spinlock.
Patrick McHardy0463d4a2007-04-16 17:02:10 -070033 *
34 * The idea is the following:
David S. Millerc7e4f3b2008-07-16 03:22:39 -070035 * - enqueue, dequeue are serialized via qdisc root lock
36 * - ingress filtering is also serialized via qdisc root lock
Patrick McHardy0463d4a2007-04-16 17:02:10 -070037 * - updates to tree and tree walking are only done under the rtnl mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
David S. Miller37437bb2008-07-16 02:15:04 -070040static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070041{
Jarek Poplawski62523522008-10-06 10:41:50 -070042 q->gso_skb = skb;
Jarek Poplawski53e91502008-10-08 11:36:22 -070043 q->qstats.requeues++;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +000044 q->q.qlen++; /* it's still part of the queue */
David S. Miller37437bb2008-07-16 02:15:04 -070045 __netif_schedule(q);
Jarek Poplawski62523522008-10-06 10:41:50 -070046
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070047 return 0;
48}
49
David S. Millerd3b753d2008-07-15 20:14:35 -070050static inline struct sk_buff *dequeue_skb(struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070051{
Jarek Poplawski554794d2008-10-06 09:54:39 -070052 struct sk_buff *skb = q->gso_skb;
53
Jarek Poplawskiebf05982008-09-22 22:16:23 -070054 if (unlikely(skb)) {
55 struct net_device *dev = qdisc_dev(q);
56 struct netdev_queue *txq;
57
58 /* check the reason of requeuing without tx lock first */
59 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +000060 if (!netif_tx_queue_stopped(txq) &&
61 !netif_tx_queue_frozen(txq)) {
Jarek Poplawski62523522008-10-06 10:41:50 -070062 q->gso_skb = NULL;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +000063 q->q.qlen--;
64 } else
Jarek Poplawskiebf05982008-09-22 22:16:23 -070065 skb = NULL;
66 } else {
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070067 skb = q->dequeue(q);
Jarek Poplawskiebf05982008-09-22 22:16:23 -070068 }
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070069
70 return skb;
71}
72
Krishna Kumar6c1361a2007-06-24 19:56:09 -070073static inline int handle_dev_cpu_collision(struct sk_buff *skb,
David S. Miller970565b2008-07-08 23:10:33 -070074 struct netdev_queue *dev_queue,
Krishna Kumar6c1361a2007-06-24 19:56:09 -070075 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070076{
Krishna Kumar6c1361a2007-06-24 19:56:09 -070077 int ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070078
David S. Millerc773e842008-07-08 23:13:53 -070079 if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
Krishna Kumar6c1361a2007-06-24 19:56:09 -070080 /*
81 * Same CPU holding the lock. It may be a transient
82 * configuration error, when hard_start_xmit() recurses. We
83 * detect it by checking xmit owner and drop the packet when
84 * deadloop is detected. Return OK to try the next skb.
85 */
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070086 kfree_skb(skb);
Krishna Kumar6c1361a2007-06-24 19:56:09 -070087 if (net_ratelimit())
88 printk(KERN_WARNING "Dead loop on netdevice %s, "
David S. Millerc773e842008-07-08 23:13:53 -070089 "fix it urgently!\n", dev_queue->dev->name);
Krishna Kumar6c1361a2007-06-24 19:56:09 -070090 ret = qdisc_qlen(q);
91 } else {
92 /*
93 * Another cpu is holding lock, requeue & delay xmits for
94 * some time.
95 */
96 __get_cpu_var(netdev_rx_stat).cpu_collision++;
David S. Miller37437bb2008-07-16 02:15:04 -070097 ret = dev_requeue_skb(skb, q);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070098 }
99
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700100 return ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700101}
102
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900103/*
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000104 * Transmit one skb, and handle the return status as required. Holding the
105 * __QDISC_STATE_RUNNING bit guarantees that only one CPU can execute this
106 * function.
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700107 *
108 * Returns to the caller:
109 * 0 - queue is empty or throttled.
110 * >0 - queue is not empty.
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700111 */
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000112int sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
113 struct net_device *dev, struct netdev_queue *txq,
114 spinlock_t *root_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800116 int ret = NETDEV_TX_BUSY;
David S. Miller7698b4f2008-07-16 01:42:40 -0700117
118 /* And release qdisc */
119 spin_unlock(root_lock);
Herbert Xud90df3a2007-05-10 04:55:14 -0700120
David S. Millerc773e842008-07-08 23:13:53 -0700121 HARD_TX_LOCK(dev, txq, smp_processor_id());
David S. Millerc3f26a22008-07-31 16:58:50 -0700122 if (!netif_tx_queue_stopped(txq) &&
Patrick McHardy572a9d72009-11-10 06:14:14 +0000123 !netif_tx_queue_frozen(txq)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700124 ret = dev_hard_start_xmit(skb, dev, txq);
Patrick McHardy572a9d72009-11-10 06:14:14 +0000125
126 /* an error implies that the skb was consumed */
127 if (ret < 0)
128 ret = NETDEV_TX_OK;
129 /* all NET_XMIT codes map to NETDEV_TX_OK */
130 ret &= ~NET_XMIT_MASK;
131 }
David S. Millerc773e842008-07-08 23:13:53 -0700132 HARD_TX_UNLOCK(dev, txq);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700133
David S. Miller7698b4f2008-07-16 01:42:40 -0700134 spin_lock(root_lock);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700135
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700136 switch (ret) {
137 case NETDEV_TX_OK:
138 /* Driver sent out skb successfully */
139 ret = qdisc_qlen(q);
140 break;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700141
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700142 case NETDEV_TX_LOCKED:
143 /* Driver try lock failed */
David S. Millereb6aafe2008-07-08 23:12:38 -0700144 ret = handle_dev_cpu_collision(skb, txq, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700145 break;
146
147 default:
148 /* Driver returned NETDEV_TX_BUSY - requeue skb */
149 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
150 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
151 dev->name, ret, q->q.qlen);
152
David S. Miller37437bb2008-07-16 02:15:04 -0700153 ret = dev_requeue_skb(skb, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700154 break;
155 }
156
David S. Millerc3f26a22008-07-31 16:58:50 -0700157 if (ret && (netif_tx_queue_stopped(txq) ||
158 netif_tx_queue_frozen(txq)))
David S. Miller37437bb2008-07-16 02:15:04 -0700159 ret = 0;
160
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700161 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000164/*
165 * NOTE: Called under qdisc_lock(q) with locally disabled BH.
166 *
167 * __QDISC_STATE_RUNNING guarantees only one CPU can process
168 * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
169 * this queue.
170 *
171 * netif_tx_lock serializes accesses to device driver.
172 *
173 * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
174 * if one is grabbed, another must be free.
175 *
176 * Note, that this procedure can be called by a watchdog timer
177 *
178 * Returns to the caller:
179 * 0 - queue is empty or throttled.
180 * >0 - queue is not empty.
181 *
182 */
183static inline int qdisc_restart(struct Qdisc *q)
184{
185 struct netdev_queue *txq;
186 struct net_device *dev;
187 spinlock_t *root_lock;
188 struct sk_buff *skb;
189
190 /* Dequeue packet */
191 skb = dequeue_skb(q);
192 if (unlikely(!skb))
193 return 0;
194
195 root_lock = qdisc_lock(q);
196 dev = qdisc_dev(q);
197 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
198
199 return sch_direct_xmit(skb, q, dev, txq, root_lock);
200}
201
David S. Miller37437bb2008-07-16 02:15:04 -0700202void __qdisc_run(struct Qdisc *q)
Herbert Xu48d83322006-06-19 23:57:59 -0700203{
Herbert Xu2ba25062008-03-28 16:25:26 -0700204 unsigned long start_time = jiffies;
205
David S. Miller37437bb2008-07-16 02:15:04 -0700206 while (qdisc_restart(q)) {
Herbert Xu2ba25062008-03-28 16:25:26 -0700207 /*
208 * Postpone processing if
209 * 1. another process needs the CPU;
210 * 2. we've been doing it for too long.
211 */
212 if (need_resched() || jiffies != start_time) {
David S. Miller37437bb2008-07-16 02:15:04 -0700213 __netif_schedule(q);
Herbert Xu2ba25062008-03-28 16:25:26 -0700214 break;
215 }
216 }
Herbert Xu48d83322006-06-19 23:57:59 -0700217
David S. Millere2627c82008-07-16 00:56:32 -0700218 clear_bit(__QDISC_STATE_RUNNING, &q->state);
Herbert Xu48d83322006-06-19 23:57:59 -0700219}
220
Eric Dumazet9d214932009-05-17 20:55:16 -0700221unsigned long dev_trans_start(struct net_device *dev)
222{
223 unsigned long val, res = dev->trans_start;
224 unsigned int i;
225
226 for (i = 0; i < dev->num_tx_queues; i++) {
227 val = netdev_get_tx_queue(dev, i)->trans_start;
228 if (val && time_after(val, res))
229 res = val;
230 }
231 dev->trans_start = res;
232 return res;
233}
234EXPORT_SYMBOL(dev_trans_start);
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static void dev_watchdog(unsigned long arg)
237{
238 struct net_device *dev = (struct net_device *)arg;
239
Herbert Xu932ff272006-06-09 12:20:56 -0700240 netif_tx_lock(dev);
David S. Millere8a04642008-07-17 00:34:19 -0700241 if (!qdisc_tx_is_noop(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (netif_device_present(dev) &&
243 netif_running(dev) &&
244 netif_carrier_ok(dev)) {
Eric Dumazet9d214932009-05-17 20:55:16 -0700245 int some_queue_timedout = 0;
David S. Millere8a04642008-07-17 00:34:19 -0700246 unsigned int i;
Eric Dumazet9d214932009-05-17 20:55:16 -0700247 unsigned long trans_start;
Stephen Hemminger338f7562006-05-16 15:02:12 -0700248
David S. Millere8a04642008-07-17 00:34:19 -0700249 for (i = 0; i < dev->num_tx_queues; i++) {
250 struct netdev_queue *txq;
251
252 txq = netdev_get_tx_queue(dev, i);
Eric Dumazet9d214932009-05-17 20:55:16 -0700253 /*
254 * old device drivers set dev->trans_start
255 */
256 trans_start = txq->trans_start ? : dev->trans_start;
257 if (netif_tx_queue_stopped(txq) &&
258 time_after(jiffies, (trans_start +
259 dev->watchdog_timeo))) {
260 some_queue_timedout = 1;
David S. Millere8a04642008-07-17 00:34:19 -0700261 break;
262 }
263 }
264
Eric Dumazet9d214932009-05-17 20:55:16 -0700265 if (some_queue_timedout) {
Arjan van de Ven6579e572008-07-21 13:31:48 -0700266 char drivername[64];
Eric Dumazet9d214932009-05-17 20:55:16 -0700267 WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
268 dev->name, netdev_drivername(dev, drivername, 64), i);
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800269 dev->netdev_ops->ndo_tx_timeout(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
David S. Millere8a04642008-07-17 00:34:19 -0700271 if (!mod_timer(&dev->watchdog_timer,
272 round_jiffies(jiffies +
273 dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 dev_hold(dev);
275 }
276 }
Herbert Xu932ff272006-06-09 12:20:56 -0700277 netif_tx_unlock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 dev_put(dev);
280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282void __netdev_watchdog_up(struct net_device *dev)
283{
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800284 if (dev->netdev_ops->ndo_tx_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (dev->watchdog_timeo <= 0)
286 dev->watchdog_timeo = 5*HZ;
Venkatesh Pallipadi60468d52007-05-31 21:28:44 -0700287 if (!mod_timer(&dev->watchdog_timer,
288 round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 dev_hold(dev);
290 }
291}
292
293static void dev_watchdog_up(struct net_device *dev)
294{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 __netdev_watchdog_up(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298static void dev_watchdog_down(struct net_device *dev)
299{
Herbert Xu932ff272006-06-09 12:20:56 -0700300 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (del_timer(&dev->watchdog_timer))
Stephen Hemminger15333062006-03-20 22:32:28 -0800302 dev_put(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700303 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700306/**
307 * netif_carrier_on - set carrier
308 * @dev: network device
309 *
310 * Device has detected that carrier.
311 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700312void netif_carrier_on(struct net_device *dev)
313{
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700314 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
David S. Millerb4730012008-11-19 15:33:54 -0800315 if (dev->reg_state == NETREG_UNINITIALIZED)
316 return;
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700317 linkwatch_fire_event(dev);
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700318 if (netif_running(dev))
319 __netdev_watchdog_up(dev);
320 }
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700321}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800322EXPORT_SYMBOL(netif_carrier_on);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700323
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700324/**
325 * netif_carrier_off - clear carrier
326 * @dev: network device
327 *
328 * Device has detected loss of carrier.
329 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700330void netif_carrier_off(struct net_device *dev)
331{
David S. Millerb4730012008-11-19 15:33:54 -0800332 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
333 if (dev->reg_state == NETREG_UNINITIALIZED)
334 return;
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700335 linkwatch_fire_event(dev);
David S. Millerb4730012008-11-19 15:33:54 -0800336 }
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700337}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800338EXPORT_SYMBOL(netif_carrier_off);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/* "NOOP" scheduler: the best scheduler, recommended for all interfaces
341 under all circumstances. It is difficult to invent anything faster or
342 cheaper.
343 */
344
Thomas Graf94df1092005-06-18 22:59:08 -0700345static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 kfree_skb(skb);
348 return NET_XMIT_CN;
349}
350
Thomas Graf94df1092005-06-18 22:59:08 -0700351static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 return NULL;
354}
355
Eric Dumazet20fea082007-11-14 01:44:41 -0800356struct Qdisc_ops noop_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 .id = "noop",
358 .priv_size = 0,
359 .enqueue = noop_enqueue,
360 .dequeue = noop_dequeue,
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700361 .peek = noop_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 .owner = THIS_MODULE,
363};
364
David S. Miller7698b4f2008-07-16 01:42:40 -0700365static struct netdev_queue noop_netdev_queue = {
David S. Miller7698b4f2008-07-16 01:42:40 -0700366 .qdisc = &noop_qdisc,
Jarek Poplawski9f3ffae2008-10-19 23:37:47 -0700367 .qdisc_sleeping = &noop_qdisc,
David S. Miller7698b4f2008-07-16 01:42:40 -0700368};
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370struct Qdisc noop_qdisc = {
371 .enqueue = noop_enqueue,
372 .dequeue = noop_dequeue,
373 .flags = TCQ_F_BUILTIN,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900374 .ops = &noop_qdisc_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 .list = LIST_HEAD_INIT(noop_qdisc.list),
David S. Miller83874002008-07-17 00:53:03 -0700376 .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
David S. Miller7698b4f2008-07-16 01:42:40 -0700377 .dev_queue = &noop_netdev_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378};
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800379EXPORT_SYMBOL(noop_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Eric Dumazet20fea082007-11-14 01:44:41 -0800381static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 .id = "noqueue",
383 .priv_size = 0,
384 .enqueue = noop_enqueue,
385 .dequeue = noop_dequeue,
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700386 .peek = noop_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 .owner = THIS_MODULE,
388};
389
David S. Miller30ee42b2008-07-18 23:00:11 -0700390static struct Qdisc noqueue_qdisc;
391static struct netdev_queue noqueue_netdev_queue = {
392 .qdisc = &noqueue_qdisc,
Jarek Poplawski9f3ffae2008-10-19 23:37:47 -0700393 .qdisc_sleeping = &noqueue_qdisc,
David S. Miller30ee42b2008-07-18 23:00:11 -0700394};
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396static struct Qdisc noqueue_qdisc = {
397 .enqueue = NULL,
398 .dequeue = noop_dequeue,
399 .flags = TCQ_F_BUILTIN,
400 .ops = &noqueue_qdisc_ops,
401 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
David S. Miller30ee42b2008-07-18 23:00:11 -0700402 .q.lock = __SPIN_LOCK_UNLOCKED(noqueue_qdisc.q.lock),
403 .dev_queue = &noqueue_netdev_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};
405
406
David S. Millerd3678b42008-07-21 09:56:13 -0700407static const u8 prio2band[TC_PRIO_MAX+1] =
408 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
Thomas Graf321090e2005-06-18 22:58:35 -0700409
David S. Millerd3678b42008-07-21 09:56:13 -0700410/* 3-band FIFO queue: old style, but should be a bit faster than
411 generic prio+fifo combination.
412 */
413
414#define PFIFO_FAST_BANDS 3
415
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000416/*
417 * Private data for a pfifo_fast scheduler containing:
418 * - queues for the three band
419 * - bitmap indicating which of the bands contain skbs
420 */
421struct pfifo_fast_priv {
422 u32 bitmap;
423 struct sk_buff_head q[PFIFO_FAST_BANDS];
424};
425
426/*
427 * Convert a bitmap to the first band number where an skb is queued, where:
428 * bitmap=0 means there are no skbs on any band.
429 * bitmap=1 means there is an skb on band 0.
430 * bitmap=7 means there are skbs on all 3 bands, etc.
431 */
432static const int bitmap2band[] = {-1, 0, 1, 0, 2, 0, 1, 0};
433
434static inline struct sk_buff_head *band2list(struct pfifo_fast_priv *priv,
435 int band)
David S. Millerd3678b42008-07-21 09:56:13 -0700436{
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000437 return priv->q + band;
David S. Millerd3678b42008-07-21 09:56:13 -0700438}
439
440static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
441{
Krishna Kumara453e062009-08-30 22:20:28 -0700442 if (skb_queue_len(&qdisc->q) < qdisc_dev(qdisc)->tx_queue_len) {
443 int band = prio2band[skb->priority & TC_PRIO_MAX];
444 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
445 struct sk_buff_head *list = band2list(priv, band);
David S. Millerd3678b42008-07-21 09:56:13 -0700446
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000447 priv->bitmap |= (1 << band);
David S. Millerd3678b42008-07-21 09:56:13 -0700448 qdisc->q.qlen++;
Thomas Graf821d24a2005-06-18 22:58:15 -0700449 return __qdisc_enqueue_tail(skb, qdisc, list);
David S. Millerd3678b42008-07-21 09:56:13 -0700450 }
Thomas Graf821d24a2005-06-18 22:58:15 -0700451
452 return qdisc_drop(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
David S. Millerd3678b42008-07-21 09:56:13 -0700455static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000457 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
458 int band = bitmap2band[priv->bitmap];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000460 if (likely(band >= 0)) {
461 struct sk_buff_head *list = band2list(priv, band);
462 struct sk_buff *skb = __qdisc_dequeue_head(qdisc, list);
463
464 qdisc->q.qlen--;
465 if (skb_queue_empty(list))
466 priv->bitmap &= ~(1 << band);
467
468 return skb;
David S. Millerd3678b42008-07-21 09:56:13 -0700469 }
Thomas Graff87a9c32005-06-18 22:58:53 -0700470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return NULL;
472}
473
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700474static struct sk_buff *pfifo_fast_peek(struct Qdisc* qdisc)
475{
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000476 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
477 int band = bitmap2band[priv->bitmap];
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700478
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000479 if (band >= 0) {
480 struct sk_buff_head *list = band2list(priv, band);
481
482 return skb_peek(list);
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700483 }
484
485 return NULL;
486}
487
David S. Millerd3678b42008-07-21 09:56:13 -0700488static void pfifo_fast_reset(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
David S. Millerd3678b42008-07-21 09:56:13 -0700490 int prio;
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000491 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
David S. Millerd3678b42008-07-21 09:56:13 -0700492
493 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000494 __qdisc_reset_queue(qdisc, band2list(priv, prio));
David S. Millerd3678b42008-07-21 09:56:13 -0700495
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000496 priv->bitmap = 0;
Thomas Graf821d24a2005-06-18 22:58:15 -0700497 qdisc->qstats.backlog = 0;
David S. Millerd3678b42008-07-21 09:56:13 -0700498 qdisc->q.qlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
David S. Millerd3678b42008-07-21 09:56:13 -0700501static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
502{
503 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
504
505 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
506 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
507 return skb->len;
508
509nla_put_failure:
510 return -1;
511}
512
513static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
514{
515 int prio;
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000516 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
David S. Millerd3678b42008-07-21 09:56:13 -0700517
518 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000519 skb_queue_head_init(band2list(priv, prio));
David S. Millerd3678b42008-07-21 09:56:13 -0700520
521 return 0;
522}
523
David S. Miller6ec1c692009-09-06 01:58:51 -0700524struct Qdisc_ops pfifo_fast_ops __read_mostly = {
David S. Millerd3678b42008-07-21 09:56:13 -0700525 .id = "pfifo_fast",
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000526 .priv_size = sizeof(struct pfifo_fast_priv),
David S. Millerd3678b42008-07-21 09:56:13 -0700527 .enqueue = pfifo_fast_enqueue,
528 .dequeue = pfifo_fast_dequeue,
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700529 .peek = pfifo_fast_peek,
David S. Millerd3678b42008-07-21 09:56:13 -0700530 .init = pfifo_fast_init,
531 .reset = pfifo_fast_reset,
532 .dump = pfifo_fast_dump,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 .owner = THIS_MODULE,
534};
535
David S. Miller5ce2d482008-07-08 17:06:30 -0700536struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -0700537 struct Qdisc_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 void *p;
540 struct Qdisc *sch;
Thomas Graf3d54b822005-07-05 14:15:09 -0700541 unsigned int size;
542 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* ensure that the Qdisc and the private data are 32-byte aligned */
Thomas Graf3d54b822005-07-05 14:15:09 -0700545 size = QDISC_ALIGN(sizeof(*sch));
546 size += ops->priv_size + (QDISC_ALIGNTO - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700548 p = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (!p)
Thomas Graf3d54b822005-07-05 14:15:09 -0700550 goto errout;
Thomas Graf3d54b822005-07-05 14:15:09 -0700551 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
552 sch->padded = (char *) sch - (char *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 INIT_LIST_HEAD(&sch->list);
555 skb_queue_head_init(&sch->q);
556 sch->ops = ops;
557 sch->enqueue = ops->enqueue;
558 sch->dequeue = ops->dequeue;
David S. Millerbb949fb2008-07-08 16:55:56 -0700559 sch->dev_queue = dev_queue;
David S. Miller5ce2d482008-07-08 17:06:30 -0700560 dev_hold(qdisc_dev(sch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 atomic_set(&sch->refcnt, 1);
Thomas Graf3d54b822005-07-05 14:15:09 -0700562
563 return sch;
564errout:
WANG Cong01e123d2008-06-27 19:51:35 -0700565 return ERR_PTR(err);
Thomas Graf3d54b822005-07-05 14:15:09 -0700566}
567
David S. Millerbb949fb2008-07-08 16:55:56 -0700568struct Qdisc * qdisc_create_dflt(struct net_device *dev,
569 struct netdev_queue *dev_queue,
570 struct Qdisc_ops *ops,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800571 unsigned int parentid)
Thomas Graf3d54b822005-07-05 14:15:09 -0700572{
573 struct Qdisc *sch;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900574
David S. Miller5ce2d482008-07-08 17:06:30 -0700575 sch = qdisc_alloc(dev_queue, ops);
Thomas Graf3d54b822005-07-05 14:15:09 -0700576 if (IS_ERR(sch))
577 goto errout;
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800578 sch->parent = parentid;
Thomas Graf3d54b822005-07-05 14:15:09 -0700579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (!ops->init || ops->init(sch, NULL) == 0)
581 return sch;
582
Thomas Graf0fbbeb12005-08-23 10:12:44 -0700583 qdisc_destroy(sch);
Thomas Graf3d54b822005-07-05 14:15:09 -0700584errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return NULL;
586}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800587EXPORT_SYMBOL(qdisc_create_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
David S. Miller5fb66222008-08-02 20:02:43 -0700589/* Under qdisc_lock(qdisc) and BH! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591void qdisc_reset(struct Qdisc *qdisc)
592{
Eric Dumazet20fea082007-11-14 01:44:41 -0800593 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (ops->reset)
596 ops->reset(qdisc);
Jarek Poplawski67305eb2008-11-03 02:52:50 -0800597
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000598 if (qdisc->gso_skb) {
599 kfree_skb(qdisc->gso_skb);
600 qdisc->gso_skb = NULL;
601 qdisc->q.qlen = 0;
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800604EXPORT_SYMBOL(qdisc_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
David S. Miller1e0d5a52008-08-17 22:31:26 -0700606void qdisc_destroy(struct Qdisc *qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Eric Dumazet20fea082007-11-14 01:44:41 -0800608 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
David S. Miller1e0d5a52008-08-17 22:31:26 -0700610 if (qdisc->flags & TCQ_F_BUILTIN ||
611 !atomic_dec_and_test(&qdisc->refcnt))
612 return;
613
David S. Miller3a682fb2008-07-20 18:13:01 -0700614#ifdef CONFIG_NET_SCHED
Jarek Poplawskif6e0b232008-08-22 03:24:05 -0700615 qdisc_list_del(qdisc);
616
Jussi Kivilinna175f9c12008-07-20 00:08:47 -0700617 qdisc_put_stab(qdisc->stab);
David S. Miller3a682fb2008-07-20 18:13:01 -0700618#endif
Patrick McHardy85670cc2006-09-27 16:45:45 -0700619 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700620 if (ops->reset)
621 ops->reset(qdisc);
622 if (ops->destroy)
623 ops->destroy(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Patrick McHardy85670cc2006-09-27 16:45:45 -0700625 module_put(ops->owner);
David S. Miller5ce2d482008-07-08 17:06:30 -0700626 dev_put(qdisc_dev(qdisc));
David S. Miller8a34c5d2008-07-17 00:47:45 -0700627
Jarek Poplawski554794d2008-10-06 09:54:39 -0700628 kfree_skb(qdisc->gso_skb);
David S. Miller8a34c5d2008-07-17 00:47:45 -0700629 kfree((char *) qdisc - qdisc->padded);
630}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800631EXPORT_SYMBOL(qdisc_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Patrick McHardy589983c2009-09-04 06:41:20 +0000633/* Attach toplevel qdisc to device queue. */
634struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
635 struct Qdisc *qdisc)
636{
637 struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
638 spinlock_t *root_lock;
639
640 root_lock = qdisc_lock(oqdisc);
641 spin_lock_bh(root_lock);
642
643 /* Prune old scheduler */
644 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
645 qdisc_reset(oqdisc);
646
647 /* ... and graft new one */
648 if (qdisc == NULL)
649 qdisc = &noop_qdisc;
650 dev_queue->qdisc_sleeping = qdisc;
651 rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
652
653 spin_unlock_bh(root_lock);
654
655 return oqdisc;
656}
657
David S. Millere8a04642008-07-17 00:34:19 -0700658static void attach_one_default_qdisc(struct net_device *dev,
659 struct netdev_queue *dev_queue,
660 void *_unused)
661{
662 struct Qdisc *qdisc;
663
664 if (dev->tx_queue_len) {
665 qdisc = qdisc_create_dflt(dev, dev_queue,
David S. Millerd3678b42008-07-21 09:56:13 -0700666 &pfifo_fast_ops, TC_H_ROOT);
David S. Millere8a04642008-07-17 00:34:19 -0700667 if (!qdisc) {
668 printk(KERN_INFO "%s: activation failed\n", dev->name);
669 return;
670 }
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000671
672 /* Can by-pass the queue discipline for default qdisc */
673 qdisc->flags |= TCQ_F_CAN_BYPASS;
David S. Millere8a04642008-07-17 00:34:19 -0700674 } else {
675 qdisc = &noqueue_qdisc;
676 }
677 dev_queue->qdisc_sleeping = qdisc;
678}
679
David S. Miller6ec1c692009-09-06 01:58:51 -0700680static void attach_default_qdiscs(struct net_device *dev)
681{
682 struct netdev_queue *txq;
683 struct Qdisc *qdisc;
684
685 txq = netdev_get_tx_queue(dev, 0);
686
687 if (!netif_is_multiqueue(dev) || dev->tx_queue_len == 0) {
688 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
689 dev->qdisc = txq->qdisc_sleeping;
690 atomic_inc(&dev->qdisc->refcnt);
691 } else {
692 qdisc = qdisc_create_dflt(dev, txq, &mq_qdisc_ops, TC_H_ROOT);
693 if (qdisc) {
694 qdisc->ops->attach(qdisc);
695 dev->qdisc = qdisc;
696 }
697 }
698}
699
David S. Millere8a04642008-07-17 00:34:19 -0700700static void transition_one_qdisc(struct net_device *dev,
701 struct netdev_queue *dev_queue,
702 void *_need_watchdog)
703{
David S. Miller83874002008-07-17 00:53:03 -0700704 struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
David S. Millere8a04642008-07-17 00:34:19 -0700705 int *need_watchdog_p = _need_watchdog;
706
David S. Millera9312ae2008-08-17 21:51:03 -0700707 if (!(new_qdisc->flags & TCQ_F_BUILTIN))
708 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
709
David S. Miller83874002008-07-17 00:53:03 -0700710 rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
Eric Dumazet9d214932009-05-17 20:55:16 -0700711 if (need_watchdog_p && new_qdisc != &noqueue_qdisc) {
712 dev_queue->trans_start = 0;
David S. Millere8a04642008-07-17 00:34:19 -0700713 *need_watchdog_p = 1;
Eric Dumazet9d214932009-05-17 20:55:16 -0700714 }
David S. Millere8a04642008-07-17 00:34:19 -0700715}
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717void dev_activate(struct net_device *dev)
718{
David S. Millere8a04642008-07-17 00:34:19 -0700719 int need_watchdog;
David S. Millerb0e1e642008-07-08 17:42:10 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* No queueing discipline is attached to device;
David S. Millerd3678b42008-07-21 09:56:13 -0700722 create default one i.e. pfifo_fast for devices,
723 which need queueing and noqueue_qdisc for
724 virtual interfaces
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
726
David S. Miller6ec1c692009-09-06 01:58:51 -0700727 if (dev->qdisc == &noop_qdisc)
728 attach_default_qdiscs(dev);
Patrick McHardyaf356af2009-09-04 06:41:18 +0000729
Tommy S. Christensencacaddf2005-05-03 16:18:52 -0700730 if (!netif_carrier_ok(dev))
731 /* Delay activation until next carrier-on event */
732 return;
733
David S. Millere8a04642008-07-17 00:34:19 -0700734 need_watchdog = 0;
735 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
David S. Miller8d50b532008-07-30 02:37:46 -0700736 transition_one_qdisc(dev, &dev->rx_queue, NULL);
David S. Millere8a04642008-07-17 00:34:19 -0700737
738 if (need_watchdog) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 dev->trans_start = jiffies;
740 dev_watchdog_up(dev);
741 }
David S. Millerb0e1e642008-07-08 17:42:10 -0700742}
743
David S. Millere8a04642008-07-17 00:34:19 -0700744static void dev_deactivate_queue(struct net_device *dev,
745 struct netdev_queue *dev_queue,
746 void *_qdisc_default)
David S. Millerb0e1e642008-07-08 17:42:10 -0700747{
David S. Millere8a04642008-07-17 00:34:19 -0700748 struct Qdisc *qdisc_default = _qdisc_default;
David S. Miller970565b2008-07-08 23:10:33 -0700749 struct Qdisc *qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -0700750
David S. Miller970565b2008-07-08 23:10:33 -0700751 qdisc = dev_queue->qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -0700752 if (qdisc) {
David S. Miller83874002008-07-17 00:53:03 -0700753 spin_lock_bh(qdisc_lock(qdisc));
754
David S. Millera9312ae2008-08-17 21:51:03 -0700755 if (!(qdisc->flags & TCQ_F_BUILTIN))
756 set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
757
Jarek Poplawskif7a54c12008-08-27 02:22:07 -0700758 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
David S. Millerb0e1e642008-07-08 17:42:10 -0700759 qdisc_reset(qdisc);
David S. Millerd3b753d2008-07-15 20:14:35 -0700760
David S. Miller83874002008-07-17 00:53:03 -0700761 spin_unlock_bh(qdisc_lock(qdisc));
David S. Millerb0e1e642008-07-08 17:42:10 -0700762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
David S. Miller4335cd22008-08-17 21:58:07 -0700765static bool some_qdisc_is_busy(struct net_device *dev)
David S. Millere8a04642008-07-17 00:34:19 -0700766{
767 unsigned int i;
768
769 for (i = 0; i < dev->num_tx_queues; i++) {
770 struct netdev_queue *dev_queue;
David S. Miller7698b4f2008-07-16 01:42:40 -0700771 spinlock_t *root_lock;
David S. Millere2627c82008-07-16 00:56:32 -0700772 struct Qdisc *q;
David S. Millere8a04642008-07-17 00:34:19 -0700773 int val;
774
775 dev_queue = netdev_get_tx_queue(dev, i);
David S. Millerb9a3b112008-08-13 15:18:38 -0700776 q = dev_queue->qdisc_sleeping;
David S. Miller5fb66222008-08-02 20:02:43 -0700777 root_lock = qdisc_lock(q);
David S. Millere8a04642008-07-17 00:34:19 -0700778
David S. Miller4335cd22008-08-17 21:58:07 -0700779 spin_lock_bh(root_lock);
David S. Millere8a04642008-07-17 00:34:19 -0700780
David S. Millerb9a3b112008-08-13 15:18:38 -0700781 val = (test_bit(__QDISC_STATE_RUNNING, &q->state) ||
782 test_bit(__QDISC_STATE_SCHED, &q->state));
David S. Millere8a04642008-07-17 00:34:19 -0700783
David S. Miller4335cd22008-08-17 21:58:07 -0700784 spin_unlock_bh(root_lock);
David S. Millere8a04642008-07-17 00:34:19 -0700785
786 if (val)
787 return true;
788 }
789 return false;
790}
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792void dev_deactivate(struct net_device *dev)
793{
David S. Millere8a04642008-07-17 00:34:19 -0700794 netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
David S. Miller8d50b532008-07-30 02:37:46 -0700795 dev_deactivate_queue(dev, &dev->rx_queue, &noop_qdisc);
Herbert Xu41a23b02007-05-10 14:12:47 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 dev_watchdog_down(dev);
798
Herbert Xuce0e32e2007-10-18 22:37:58 -0700799 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
Herbert Xud4828d82006-06-22 02:28:18 -0700800 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Herbert Xud4828d82006-06-22 02:28:18 -0700802 /* Wait for outstanding qdisc_run calls. */
David S. Miller4335cd22008-08-17 21:58:07 -0700803 while (some_qdisc_is_busy(dev))
804 yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
David S. Millerb0e1e642008-07-08 17:42:10 -0700807static void dev_init_scheduler_queue(struct net_device *dev,
808 struct netdev_queue *dev_queue,
David S. Millere8a04642008-07-17 00:34:19 -0700809 void *_qdisc)
David S. Millerb0e1e642008-07-08 17:42:10 -0700810{
David S. Millere8a04642008-07-17 00:34:19 -0700811 struct Qdisc *qdisc = _qdisc;
812
David S. Millerb0e1e642008-07-08 17:42:10 -0700813 dev_queue->qdisc = qdisc;
814 dev_queue->qdisc_sleeping = qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -0700815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817void dev_init_scheduler(struct net_device *dev)
818{
Patrick McHardyaf356af2009-09-04 06:41:18 +0000819 dev->qdisc = &noop_qdisc;
David S. Millere8a04642008-07-17 00:34:19 -0700820 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
David S. Miller8d50b532008-07-30 02:37:46 -0700821 dev_init_scheduler_queue(dev, &dev->rx_queue, &noop_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800823 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
David S. Millere8a04642008-07-17 00:34:19 -0700826static void shutdown_scheduler_queue(struct net_device *dev,
827 struct netdev_queue *dev_queue,
828 void *_qdisc_default)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
David S. Millerb0e1e642008-07-08 17:42:10 -0700830 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
David S. Millere8a04642008-07-17 00:34:19 -0700831 struct Qdisc *qdisc_default = _qdisc_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
David S. Millerb0e1e642008-07-08 17:42:10 -0700833 if (qdisc) {
Jarek Poplawskif7a54c12008-08-27 02:22:07 -0700834 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
David S. Millerb0e1e642008-07-08 17:42:10 -0700835 dev_queue->qdisc_sleeping = qdisc_default;
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 qdisc_destroy(qdisc);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900838 }
David S. Millerb0e1e642008-07-08 17:42:10 -0700839}
840
841void dev_shutdown(struct net_device *dev)
842{
David S. Millere8a04642008-07-17 00:34:19 -0700843 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
David S. Miller8d50b532008-07-30 02:37:46 -0700844 shutdown_scheduler_queue(dev, &dev->rx_queue, &noop_qdisc);
Patrick McHardyaf356af2009-09-04 06:41:18 +0000845 qdisc_destroy(dev->qdisc);
846 dev->qdisc = &noop_qdisc;
847
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700848 WARN_ON(timer_pending(&dev->watchdog_timer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}