blob: 9be2f152455a952e0f01fa7d1b31e0d386d0261b [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
32 * dev->queue_lock spinlock.
33 *
34 * The idea is the following:
35 * - enqueue, dequeue are serialized via top level device
36 * spinlock dev->queue_lock.
Patrick McHardyfd44de72007-04-16 17:07:08 -070037 * - ingress filtering is serialized via top level device
38 * spinlock dev->ingress_lock.
Patrick McHardy0463d4a2007-04-16 17:02:10 -070039 * - updates to tree and tree walking are only done under the rtnl mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42void qdisc_lock_tree(struct net_device *dev)
43{
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 spin_lock_bh(&dev->queue_lock);
Patrick McHardyfd44de72007-04-16 17:07:08 -070045 spin_lock(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
48void qdisc_unlock_tree(struct net_device *dev)
49{
Patrick McHardyfd44de72007-04-16 17:07:08 -070050 spin_unlock(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 spin_unlock_bh(&dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070054static inline int qdisc_qlen(struct Qdisc *q)
55{
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070056 return q->q.qlen;
57}
58
Krishna Kumar6c1361a2007-06-24 19:56:09 -070059static inline int dev_requeue_skb(struct sk_buff *skb, struct net_device *dev,
60 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070061{
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070062 if (unlikely(skb->next))
63 dev->gso_skb = skb;
64 else
65 q->ops->requeue(skb, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -070066
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070067 netif_schedule(dev);
68 return 0;
69}
70
Krishna Kumar6c1361a2007-06-24 19:56:09 -070071static inline struct sk_buff *dev_dequeue_skb(struct net_device *dev,
72 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070073{
Krishna Kumar6c1361a2007-06-24 19:56:09 -070074 struct sk_buff *skb;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070075
Krishna Kumar6c1361a2007-06-24 19:56:09 -070076 if ((skb = dev->gso_skb))
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070077 dev->gso_skb = NULL;
78 else
79 skb = q->dequeue(q);
80
81 return skb;
82}
83
Krishna Kumar6c1361a2007-06-24 19:56:09 -070084static inline int handle_dev_cpu_collision(struct sk_buff *skb,
85 struct net_device *dev,
86 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070087{
Krishna Kumar6c1361a2007-06-24 19:56:09 -070088 int ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070089
Krishna Kumar6c1361a2007-06-24 19:56:09 -070090 if (unlikely(dev->xmit_lock_owner == smp_processor_id())) {
91 /*
92 * Same CPU holding the lock. It may be a transient
93 * configuration error, when hard_start_xmit() recurses. We
94 * detect it by checking xmit owner and drop the packet when
95 * deadloop is detected. Return OK to try the next skb.
96 */
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070097 kfree_skb(skb);
Krishna Kumar6c1361a2007-06-24 19:56:09 -070098 if (net_ratelimit())
99 printk(KERN_WARNING "Dead loop on netdevice %s, "
100 "fix it urgently!\n", dev->name);
101 ret = qdisc_qlen(q);
102 } else {
103 /*
104 * Another cpu is holding lock, requeue & delay xmits for
105 * some time.
106 */
107 __get_cpu_var(netdev_rx_stat).cpu_collision++;
108 ret = dev_requeue_skb(skb, dev, q);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700109 }
110
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700111 return ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700112}
113
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900114/*
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700115 * NOTE: Called under dev->queue_lock with locally disabled BH.
116 *
117 * __LINK_STATE_QDISC_RUNNING guarantees only one CPU can process this
118 * device at a time. dev->queue_lock serializes queue accesses for
119 * this device AND dev->qdisc pointer itself.
120 *
121 * netif_tx_lock serializes accesses to device driver.
122 *
123 * dev->queue_lock and netif_tx_lock are mutually exclusive,
124 * if one is grabbed, another must be free.
125 *
126 * Note, that this procedure can be called by a watchdog timer
127 *
128 * Returns to the caller:
129 * 0 - queue is empty or throttled.
130 * >0 - queue is not empty.
131 *
132 */
Herbert Xu48d83322006-06-19 23:57:59 -0700133static inline int qdisc_restart(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct Qdisc *q = dev->qdisc;
136 struct sk_buff *skb;
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800137 int ret = NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700139 /* Dequeue packet */
140 if (unlikely((skb = dev_dequeue_skb(dev, q)) == NULL))
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700141 return 0;
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700142
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700143
144 /* And release queue */
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700145 spin_unlock(&dev->queue_lock);
Herbert Xud90df3a2007-05-10 04:55:14 -0700146
Jamal Hadi Salim82366322007-09-25 19:27:13 -0700147 HARD_TX_LOCK(dev, smp_processor_id());
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800148 if (!netif_subqueue_stopped(dev, skb))
149 ret = dev_hard_start_xmit(skb, dev);
Jamal Hadi Salim82366322007-09-25 19:27:13 -0700150 HARD_TX_UNLOCK(dev);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700151
152 spin_lock(&dev->queue_lock);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700153 q = dev->qdisc;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700154
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700155 switch (ret) {
156 case NETDEV_TX_OK:
157 /* Driver sent out skb successfully */
158 ret = qdisc_qlen(q);
159 break;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700160
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700161 case NETDEV_TX_LOCKED:
162 /* Driver try lock failed */
163 ret = handle_dev_cpu_collision(skb, dev, q);
164 break;
165
166 default:
167 /* Driver returned NETDEV_TX_BUSY - requeue skb */
168 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
169 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
170 dev->name, ret, q->q.qlen);
171
172 ret = dev_requeue_skb(skb, dev, q);
173 break;
174 }
175
176 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Herbert Xu48d83322006-06-19 23:57:59 -0700179void __qdisc_run(struct net_device *dev)
180{
Herbert Xud90df3a2007-05-10 04:55:14 -0700181 do {
182 if (!qdisc_restart(dev))
183 break;
184 } while (!netif_queue_stopped(dev));
Herbert Xu48d83322006-06-19 23:57:59 -0700185
186 clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static void dev_watchdog(unsigned long arg)
190{
191 struct net_device *dev = (struct net_device *)arg;
192
Herbert Xu932ff272006-06-09 12:20:56 -0700193 netif_tx_lock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (dev->qdisc != &noop_qdisc) {
195 if (netif_device_present(dev) &&
196 netif_running(dev) &&
197 netif_carrier_ok(dev)) {
198 if (netif_queue_stopped(dev) &&
Stephen Hemminger338f7562006-05-16 15:02:12 -0700199 time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) {
200
201 printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n",
202 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 dev->tx_timeout(dev);
204 }
Arjan van de Venf5a6e012007-02-05 17:59:51 -0800205 if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 dev_hold(dev);
207 }
208 }
Herbert Xu932ff272006-06-09 12:20:56 -0700209 netif_tx_unlock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 dev_put(dev);
212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214void __netdev_watchdog_up(struct net_device *dev)
215{
216 if (dev->tx_timeout) {
217 if (dev->watchdog_timeo <= 0)
218 dev->watchdog_timeo = 5*HZ;
Venkatesh Pallipadi60468d52007-05-31 21:28:44 -0700219 if (!mod_timer(&dev->watchdog_timer,
220 round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 dev_hold(dev);
222 }
223}
224
225static void dev_watchdog_up(struct net_device *dev)
226{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 __netdev_watchdog_up(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230static void dev_watchdog_down(struct net_device *dev)
231{
Herbert Xu932ff272006-06-09 12:20:56 -0700232 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (del_timer(&dev->watchdog_timer))
Stephen Hemminger15333062006-03-20 22:32:28 -0800234 dev_put(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700235 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700238/**
239 * netif_carrier_on - set carrier
240 * @dev: network device
241 *
242 * Device has detected that carrier.
243 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700244void netif_carrier_on(struct net_device *dev)
245{
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700246 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700247 linkwatch_fire_event(dev);
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700248 if (netif_running(dev))
249 __netdev_watchdog_up(dev);
250 }
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700251}
252
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700253/**
254 * netif_carrier_off - clear carrier
255 * @dev: network device
256 *
257 * Device has detected loss of carrier.
258 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700259void netif_carrier_off(struct net_device *dev)
260{
261 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
262 linkwatch_fire_event(dev);
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/* "NOOP" scheduler: the best scheduler, recommended for all interfaces
266 under all circumstances. It is difficult to invent anything faster or
267 cheaper.
268 */
269
Thomas Graf94df1092005-06-18 22:59:08 -0700270static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 kfree_skb(skb);
273 return NET_XMIT_CN;
274}
275
Thomas Graf94df1092005-06-18 22:59:08 -0700276static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 return NULL;
279}
280
Thomas Graf94df1092005-06-18 22:59:08 -0700281static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 if (net_ratelimit())
Thomas Graf94df1092005-06-18 22:59:08 -0700284 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
285 skb->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 kfree_skb(skb);
287 return NET_XMIT_CN;
288}
289
Eric Dumazet20fea082007-11-14 01:44:41 -0800290struct Qdisc_ops noop_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 .id = "noop",
292 .priv_size = 0,
293 .enqueue = noop_enqueue,
294 .dequeue = noop_dequeue,
295 .requeue = noop_requeue,
296 .owner = THIS_MODULE,
297};
298
299struct Qdisc noop_qdisc = {
300 .enqueue = noop_enqueue,
301 .dequeue = noop_dequeue,
302 .flags = TCQ_F_BUILTIN,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900303 .ops = &noop_qdisc_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 .list = LIST_HEAD_INIT(noop_qdisc.list),
305};
306
Eric Dumazet20fea082007-11-14 01:44:41 -0800307static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 .id = "noqueue",
309 .priv_size = 0,
310 .enqueue = noop_enqueue,
311 .dequeue = noop_dequeue,
312 .requeue = noop_requeue,
313 .owner = THIS_MODULE,
314};
315
316static struct Qdisc noqueue_qdisc = {
317 .enqueue = NULL,
318 .dequeue = noop_dequeue,
319 .flags = TCQ_F_BUILTIN,
320 .ops = &noqueue_qdisc_ops,
321 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
322};
323
324
325static const u8 prio2band[TC_PRIO_MAX+1] =
326 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
327
328/* 3-band FIFO queue: old style, but should be a bit faster than
329 generic prio+fifo combination.
330 */
331
Thomas Graff87a9c32005-06-18 22:58:53 -0700332#define PFIFO_FAST_BANDS 3
333
Thomas Graf321090e2005-06-18 22:58:35 -0700334static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
335 struct Qdisc *qdisc)
336{
337 struct sk_buff_head *list = qdisc_priv(qdisc);
338 return list + prio2band[skb->priority & TC_PRIO_MAX];
339}
340
Thomas Graff87a9c32005-06-18 22:58:53 -0700341static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Thomas Graf321090e2005-06-18 22:58:35 -0700343 struct sk_buff_head *list = prio2list(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Thomas Graf821d24a2005-06-18 22:58:15 -0700345 if (skb_queue_len(list) < qdisc->dev->tx_queue_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 qdisc->q.qlen++;
Thomas Graf821d24a2005-06-18 22:58:15 -0700347 return __qdisc_enqueue_tail(skb, qdisc, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Thomas Graf821d24a2005-06-18 22:58:15 -0700349
350 return qdisc_drop(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Thomas Graff87a9c32005-06-18 22:58:53 -0700353static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 int prio;
356 struct sk_buff_head *list = qdisc_priv(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Thomas Graf452f2992005-07-18 13:30:53 -0700358 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
359 if (!skb_queue_empty(list + prio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 qdisc->q.qlen--;
Thomas Graf452f2992005-07-18 13:30:53 -0700361 return __qdisc_dequeue_head(qdisc, list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363 }
Thomas Graff87a9c32005-06-18 22:58:53 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return NULL;
366}
367
Thomas Graff87a9c32005-06-18 22:58:53 -0700368static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 qdisc->q.qlen++;
Thomas Graf321090e2005-06-18 22:58:35 -0700371 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Thomas Graff87a9c32005-06-18 22:58:53 -0700374static void pfifo_fast_reset(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 int prio;
377 struct sk_buff_head *list = qdisc_priv(qdisc);
378
Thomas Graff87a9c32005-06-18 22:58:53 -0700379 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
Thomas Graf821d24a2005-06-18 22:58:15 -0700380 __qdisc_reset_queue(qdisc, list + prio);
381
382 qdisc->qstats.backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 qdisc->q.qlen = 0;
384}
385
386static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
387{
Thomas Graff87a9c32005-06-18 22:58:53 -0700388 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
391 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
392 return skb->len;
393
394rtattr_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return -1;
396}
397
398static int pfifo_fast_init(struct Qdisc *qdisc, struct rtattr *opt)
399{
Thomas Graff87a9c32005-06-18 22:58:53 -0700400 int prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct sk_buff_head *list = qdisc_priv(qdisc);
402
Thomas Graff87a9c32005-06-18 22:58:53 -0700403 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
404 skb_queue_head_init(list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 return 0;
407}
408
Eric Dumazet20fea082007-11-14 01:44:41 -0800409static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 .id = "pfifo_fast",
Thomas Graff87a9c32005-06-18 22:58:53 -0700411 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 .enqueue = pfifo_fast_enqueue,
413 .dequeue = pfifo_fast_dequeue,
414 .requeue = pfifo_fast_requeue,
415 .init = pfifo_fast_init,
416 .reset = pfifo_fast_reset,
417 .dump = pfifo_fast_dump,
418 .owner = THIS_MODULE,
419};
420
Thomas Graf3d54b822005-07-05 14:15:09 -0700421struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 void *p;
424 struct Qdisc *sch;
Thomas Graf3d54b822005-07-05 14:15:09 -0700425 unsigned int size;
426 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /* ensure that the Qdisc and the private data are 32-byte aligned */
Thomas Graf3d54b822005-07-05 14:15:09 -0700429 size = QDISC_ALIGN(sizeof(*sch));
430 size += ops->priv_size + (QDISC_ALIGNTO - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700432 p = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (!p)
Thomas Graf3d54b822005-07-05 14:15:09 -0700434 goto errout;
Thomas Graf3d54b822005-07-05 14:15:09 -0700435 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
436 sch->padded = (char *) sch - (char *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 INIT_LIST_HEAD(&sch->list);
439 skb_queue_head_init(&sch->q);
440 sch->ops = ops;
441 sch->enqueue = ops->enqueue;
442 sch->dequeue = ops->dequeue;
443 sch->dev = dev;
444 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 atomic_set(&sch->refcnt, 1);
Thomas Graf3d54b822005-07-05 14:15:09 -0700446
447 return sch;
448errout:
449 return ERR_PTR(-err);
450}
451
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800452struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops,
453 unsigned int parentid)
Thomas Graf3d54b822005-07-05 14:15:09 -0700454{
455 struct Qdisc *sch;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900456
Thomas Graf3d54b822005-07-05 14:15:09 -0700457 sch = qdisc_alloc(dev, ops);
458 if (IS_ERR(sch))
459 goto errout;
Patrick McHardyfd44de72007-04-16 17:07:08 -0700460 sch->stats_lock = &dev->queue_lock;
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800461 sch->parent = parentid;
Thomas Graf3d54b822005-07-05 14:15:09 -0700462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (!ops->init || ops->init(sch, NULL) == 0)
464 return sch;
465
Thomas Graf0fbbeb12005-08-23 10:12:44 -0700466 qdisc_destroy(sch);
Thomas Graf3d54b822005-07-05 14:15:09 -0700467errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return NULL;
469}
470
471/* Under dev->queue_lock and BH! */
472
473void qdisc_reset(struct Qdisc *qdisc)
474{
Eric Dumazet20fea082007-11-14 01:44:41 -0800475 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 if (ops->reset)
478 ops->reset(qdisc);
479}
480
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900481/* this is the rcu callback function to clean up a qdisc when there
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * are no further references to it */
483
484static void __qdisc_destroy(struct rcu_head *head)
485{
486 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 kfree((char *) qdisc - qdisc->padded);
488}
489
490/* Under dev->queue_lock and BH! */
491
492void qdisc_destroy(struct Qdisc *qdisc)
493{
Eric Dumazet20fea082007-11-14 01:44:41 -0800494 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 if (qdisc->flags & TCQ_F_BUILTIN ||
Patrick McHardy85670cc2006-09-27 16:45:45 -0700497 !atomic_dec_and_test(&qdisc->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return;
499
Patrick McHardy85670cc2006-09-27 16:45:45 -0700500 list_del(&qdisc->list);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700501 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700502 if (ops->reset)
503 ops->reset(qdisc);
504 if (ops->destroy)
505 ops->destroy(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Patrick McHardy85670cc2006-09-27 16:45:45 -0700507 module_put(ops->owner);
508 dev_put(qdisc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
510}
511
512void dev_activate(struct net_device *dev)
513{
514 /* No queueing discipline is attached to device;
515 create default one i.e. pfifo_fast for devices,
516 which need queueing and noqueue_qdisc for
517 virtual interfaces
518 */
519
520 if (dev->qdisc_sleeping == &noop_qdisc) {
521 struct Qdisc *qdisc;
522 if (dev->tx_queue_len) {
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800523 qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops,
524 TC_H_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (qdisc == NULL) {
526 printk(KERN_INFO "%s: activation failed\n", dev->name);
527 return;
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 list_add_tail(&qdisc->list, &dev->qdisc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 } else {
531 qdisc = &noqueue_qdisc;
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 dev->qdisc_sleeping = qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
Tommy S. Christensencacaddf2005-05-03 16:18:52 -0700536 if (!netif_carrier_ok(dev))
537 /* Delay activation until next carrier-on event */
538 return;
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 spin_lock_bh(&dev->queue_lock);
541 rcu_assign_pointer(dev->qdisc, dev->qdisc_sleeping);
542 if (dev->qdisc != &noqueue_qdisc) {
543 dev->trans_start = jiffies;
544 dev_watchdog_up(dev);
545 }
546 spin_unlock_bh(&dev->queue_lock);
547}
548
549void dev_deactivate(struct net_device *dev)
550{
551 struct Qdisc *qdisc;
Herbert Xu41a23b02007-05-10 14:12:47 -0700552 struct sk_buff *skb;
Herbert Xuce0e32e2007-10-18 22:37:58 -0700553 int running;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 spin_lock_bh(&dev->queue_lock);
556 qdisc = dev->qdisc;
557 dev->qdisc = &noop_qdisc;
558
559 qdisc_reset(qdisc);
560
Herbert Xu41a23b02007-05-10 14:12:47 -0700561 skb = dev->gso_skb;
562 dev->gso_skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 spin_unlock_bh(&dev->queue_lock);
564
Herbert Xu41a23b02007-05-10 14:12:47 -0700565 kfree_skb(skb);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 dev_watchdog_down(dev);
568
Herbert Xuce0e32e2007-10-18 22:37:58 -0700569 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
Herbert Xud4828d82006-06-22 02:28:18 -0700570 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Herbert Xud4828d82006-06-22 02:28:18 -0700572 /* Wait for outstanding qdisc_run calls. */
Herbert Xuce0e32e2007-10-18 22:37:58 -0700573 do {
574 while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
575 yield();
576
577 /*
578 * Double-check inside queue lock to ensure that all effects
579 * of the queue run are visible when we return.
580 */
581 spin_lock_bh(&dev->queue_lock);
582 running = test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
583 spin_unlock_bh(&dev->queue_lock);
584
585 /*
586 * The running flag should never be set at this point because
587 * we've already set dev->qdisc to noop_qdisc *inside* the same
588 * pair of spin locks. That is, if any qdisc_run starts after
589 * our initial test it should see the noop_qdisc and then
590 * clear the RUNNING bit before dropping the queue lock. So
591 * if it is set here then we've found a bug.
592 */
593 } while (WARN_ON_ONCE(running));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
596void dev_init_scheduler(struct net_device *dev)
597{
598 qdisc_lock_tree(dev);
599 dev->qdisc = &noop_qdisc;
600 dev->qdisc_sleeping = &noop_qdisc;
601 INIT_LIST_HEAD(&dev->qdisc_list);
602 qdisc_unlock_tree(dev);
603
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800604 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607void dev_shutdown(struct net_device *dev)
608{
609 struct Qdisc *qdisc;
610
611 qdisc_lock_tree(dev);
612 qdisc = dev->qdisc_sleeping;
613 dev->qdisc = &noop_qdisc;
614 dev->qdisc_sleeping = &noop_qdisc;
615 qdisc_destroy(qdisc);
616#if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900617 if ((qdisc = dev->qdisc_ingress) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 dev->qdisc_ingress = NULL;
619 qdisc_destroy(qdisc);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621#endif
622 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
623 qdisc_unlock_tree(dev);
624}
625
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700626EXPORT_SYMBOL(netif_carrier_on);
627EXPORT_SYMBOL(netif_carrier_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628EXPORT_SYMBOL(noop_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629EXPORT_SYMBOL(qdisc_create_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630EXPORT_SYMBOL(qdisc_destroy);
631EXPORT_SYMBOL(qdisc_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632EXPORT_SYMBOL(qdisc_lock_tree);
633EXPORT_SYMBOL(qdisc_unlock_tree);