blob: b741618e4d54048e67dcf90e7d393e72a0541423 [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)
Eric Dumazet9a429c42008-01-01 21:58:02 -080043 __acquires(dev->queue_lock)
44 __acquires(dev->ingress_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 spin_lock_bh(&dev->queue_lock);
Patrick McHardyfd44de72007-04-16 17:07:08 -070047 spin_lock(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
Patrick McHardy62e3ba12008-01-22 22:10:23 -080049EXPORT_SYMBOL(qdisc_lock_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51void qdisc_unlock_tree(struct net_device *dev)
Eric Dumazet9a429c42008-01-01 21:58:02 -080052 __releases(dev->ingress_lock)
53 __releases(dev->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Patrick McHardyfd44de72007-04-16 17:07:08 -070055 spin_unlock(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 spin_unlock_bh(&dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
Patrick McHardy62e3ba12008-01-22 22:10:23 -080058EXPORT_SYMBOL(qdisc_unlock_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070060static inline int qdisc_qlen(struct Qdisc *q)
61{
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070062 return q->q.qlen;
63}
64
Krishna Kumar6c1361a2007-06-24 19:56:09 -070065static inline int dev_requeue_skb(struct sk_buff *skb, struct net_device *dev,
66 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070067{
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070068 if (unlikely(skb->next))
69 dev->gso_skb = skb;
70 else
71 q->ops->requeue(skb, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -070072
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070073 netif_schedule(dev);
74 return 0;
75}
76
Krishna Kumar6c1361a2007-06-24 19:56:09 -070077static inline struct sk_buff *dev_dequeue_skb(struct net_device *dev,
78 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070079{
Krishna Kumar6c1361a2007-06-24 19:56:09 -070080 struct sk_buff *skb;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070081
Krishna Kumar6c1361a2007-06-24 19:56:09 -070082 if ((skb = dev->gso_skb))
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070083 dev->gso_skb = NULL;
84 else
85 skb = q->dequeue(q);
86
87 return skb;
88}
89
Krishna Kumar6c1361a2007-06-24 19:56:09 -070090static inline int handle_dev_cpu_collision(struct sk_buff *skb,
91 struct net_device *dev,
92 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070093{
Krishna Kumar6c1361a2007-06-24 19:56:09 -070094 int ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070095
Krishna Kumar6c1361a2007-06-24 19:56:09 -070096 if (unlikely(dev->xmit_lock_owner == smp_processor_id())) {
97 /*
98 * Same CPU holding the lock. It may be a transient
99 * configuration error, when hard_start_xmit() recurses. We
100 * detect it by checking xmit owner and drop the packet when
101 * deadloop is detected. Return OK to try the next skb.
102 */
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700103 kfree_skb(skb);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700104 if (net_ratelimit())
105 printk(KERN_WARNING "Dead loop on netdevice %s, "
106 "fix it urgently!\n", dev->name);
107 ret = qdisc_qlen(q);
108 } else {
109 /*
110 * Another cpu is holding lock, requeue & delay xmits for
111 * some time.
112 */
113 __get_cpu_var(netdev_rx_stat).cpu_collision++;
114 ret = dev_requeue_skb(skb, dev, q);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700115 }
116
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700117 return ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700118}
119
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900120/*
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700121 * NOTE: Called under dev->queue_lock with locally disabled BH.
122 *
123 * __LINK_STATE_QDISC_RUNNING guarantees only one CPU can process this
124 * device at a time. dev->queue_lock serializes queue accesses for
125 * this device AND dev->qdisc pointer itself.
126 *
127 * netif_tx_lock serializes accesses to device driver.
128 *
129 * dev->queue_lock and netif_tx_lock are mutually exclusive,
130 * if one is grabbed, another must be free.
131 *
132 * Note, that this procedure can be called by a watchdog timer
133 *
134 * Returns to the caller:
135 * 0 - queue is empty or throttled.
136 * >0 - queue is not empty.
137 *
138 */
Herbert Xu48d83322006-06-19 23:57:59 -0700139static inline int qdisc_restart(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 struct Qdisc *q = dev->qdisc;
142 struct sk_buff *skb;
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800143 int ret = NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700145 /* Dequeue packet */
146 if (unlikely((skb = dev_dequeue_skb(dev, q)) == NULL))
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700147 return 0;
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700148
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700149
150 /* And release queue */
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700151 spin_unlock(&dev->queue_lock);
Herbert Xud90df3a2007-05-10 04:55:14 -0700152
Jamal Hadi Salim82366322007-09-25 19:27:13 -0700153 HARD_TX_LOCK(dev, smp_processor_id());
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800154 if (!netif_subqueue_stopped(dev, skb))
155 ret = dev_hard_start_xmit(skb, dev);
Jamal Hadi Salim82366322007-09-25 19:27:13 -0700156 HARD_TX_UNLOCK(dev);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700157
158 spin_lock(&dev->queue_lock);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700159 q = dev->qdisc;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700160
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700161 switch (ret) {
162 case NETDEV_TX_OK:
163 /* Driver sent out skb successfully */
164 ret = qdisc_qlen(q);
165 break;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700166
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700167 case NETDEV_TX_LOCKED:
168 /* Driver try lock failed */
169 ret = handle_dev_cpu_collision(skb, dev, q);
170 break;
171
172 default:
173 /* Driver returned NETDEV_TX_BUSY - requeue skb */
174 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
175 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
176 dev->name, ret, q->q.qlen);
177
178 ret = dev_requeue_skb(skb, dev, q);
179 break;
180 }
181
182 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Herbert Xu48d83322006-06-19 23:57:59 -0700185void __qdisc_run(struct net_device *dev)
186{
Herbert Xu2ba25062008-03-28 16:25:26 -0700187 unsigned long start_time = jiffies;
188
189 while (qdisc_restart(dev)) {
190 if (netif_queue_stopped(dev))
Herbert Xud90df3a2007-05-10 04:55:14 -0700191 break;
Herbert Xu2ba25062008-03-28 16:25:26 -0700192
193 /*
194 * Postpone processing if
195 * 1. another process needs the CPU;
196 * 2. we've been doing it for too long.
197 */
198 if (need_resched() || jiffies != start_time) {
199 netif_schedule(dev);
200 break;
201 }
202 }
Herbert Xu48d83322006-06-19 23:57:59 -0700203
204 clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static void dev_watchdog(unsigned long arg)
208{
209 struct net_device *dev = (struct net_device *)arg;
210
Herbert Xu932ff272006-06-09 12:20:56 -0700211 netif_tx_lock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (dev->qdisc != &noop_qdisc) {
213 if (netif_device_present(dev) &&
214 netif_running(dev) &&
215 netif_carrier_ok(dev)) {
216 if (netif_queue_stopped(dev) &&
Stephen Hemminger338f7562006-05-16 15:02:12 -0700217 time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) {
218
219 printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n",
220 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 dev->tx_timeout(dev);
222 }
Arjan van de Venf5a6e012007-02-05 17:59:51 -0800223 if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 dev_hold(dev);
225 }
226 }
Herbert Xu932ff272006-06-09 12:20:56 -0700227 netif_tx_unlock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 dev_put(dev);
230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232void __netdev_watchdog_up(struct net_device *dev)
233{
234 if (dev->tx_timeout) {
235 if (dev->watchdog_timeo <= 0)
236 dev->watchdog_timeo = 5*HZ;
Venkatesh Pallipadi60468d52007-05-31 21:28:44 -0700237 if (!mod_timer(&dev->watchdog_timer,
238 round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 dev_hold(dev);
240 }
241}
242
243static void dev_watchdog_up(struct net_device *dev)
244{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 __netdev_watchdog_up(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248static void dev_watchdog_down(struct net_device *dev)
249{
Herbert Xu932ff272006-06-09 12:20:56 -0700250 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (del_timer(&dev->watchdog_timer))
Stephen Hemminger15333062006-03-20 22:32:28 -0800252 dev_put(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700253 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700256/**
257 * netif_carrier_on - set carrier
258 * @dev: network device
259 *
260 * Device has detected that carrier.
261 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700262void netif_carrier_on(struct net_device *dev)
263{
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700264 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700265 linkwatch_fire_event(dev);
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700266 if (netif_running(dev))
267 __netdev_watchdog_up(dev);
268 }
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700269}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800270EXPORT_SYMBOL(netif_carrier_on);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700271
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700272/**
273 * netif_carrier_off - clear carrier
274 * @dev: network device
275 *
276 * Device has detected loss of carrier.
277 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700278void netif_carrier_off(struct net_device *dev)
279{
280 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
281 linkwatch_fire_event(dev);
282}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800283EXPORT_SYMBOL(netif_carrier_off);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/* "NOOP" scheduler: the best scheduler, recommended for all interfaces
286 under all circumstances. It is difficult to invent anything faster or
287 cheaper.
288 */
289
Thomas Graf94df1092005-06-18 22:59:08 -0700290static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 kfree_skb(skb);
293 return NET_XMIT_CN;
294}
295
Thomas Graf94df1092005-06-18 22:59:08 -0700296static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 return NULL;
299}
300
Thomas Graf94df1092005-06-18 22:59:08 -0700301static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 if (net_ratelimit())
Thomas Graf94df1092005-06-18 22:59:08 -0700304 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
305 skb->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 kfree_skb(skb);
307 return NET_XMIT_CN;
308}
309
Eric Dumazet20fea082007-11-14 01:44:41 -0800310struct Qdisc_ops noop_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 .id = "noop",
312 .priv_size = 0,
313 .enqueue = noop_enqueue,
314 .dequeue = noop_dequeue,
315 .requeue = noop_requeue,
316 .owner = THIS_MODULE,
317};
318
319struct Qdisc noop_qdisc = {
320 .enqueue = noop_enqueue,
321 .dequeue = noop_dequeue,
322 .flags = TCQ_F_BUILTIN,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900323 .ops = &noop_qdisc_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 .list = LIST_HEAD_INIT(noop_qdisc.list),
325};
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800326EXPORT_SYMBOL(noop_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Eric Dumazet20fea082007-11-14 01:44:41 -0800328static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 .id = "noqueue",
330 .priv_size = 0,
331 .enqueue = noop_enqueue,
332 .dequeue = noop_dequeue,
333 .requeue = noop_requeue,
334 .owner = THIS_MODULE,
335};
336
337static struct Qdisc noqueue_qdisc = {
338 .enqueue = NULL,
339 .dequeue = noop_dequeue,
340 .flags = TCQ_F_BUILTIN,
341 .ops = &noqueue_qdisc_ops,
342 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
343};
344
345
346static const u8 prio2band[TC_PRIO_MAX+1] =
347 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
348
349/* 3-band FIFO queue: old style, but should be a bit faster than
350 generic prio+fifo combination.
351 */
352
Thomas Graff87a9c32005-06-18 22:58:53 -0700353#define PFIFO_FAST_BANDS 3
354
Thomas Graf321090e2005-06-18 22:58:35 -0700355static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
356 struct Qdisc *qdisc)
357{
358 struct sk_buff_head *list = qdisc_priv(qdisc);
359 return list + prio2band[skb->priority & TC_PRIO_MAX];
360}
361
Thomas Graff87a9c32005-06-18 22:58:53 -0700362static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Thomas Graf321090e2005-06-18 22:58:35 -0700364 struct sk_buff_head *list = prio2list(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Thomas Graf821d24a2005-06-18 22:58:15 -0700366 if (skb_queue_len(list) < qdisc->dev->tx_queue_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 qdisc->q.qlen++;
Thomas Graf821d24a2005-06-18 22:58:15 -0700368 return __qdisc_enqueue_tail(skb, qdisc, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Thomas Graf821d24a2005-06-18 22:58:15 -0700370
371 return qdisc_drop(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Thomas Graff87a9c32005-06-18 22:58:53 -0700374static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 int prio;
377 struct sk_buff_head *list = qdisc_priv(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Thomas Graf452f2992005-07-18 13:30:53 -0700379 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
380 if (!skb_queue_empty(list + prio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 qdisc->q.qlen--;
Thomas Graf452f2992005-07-18 13:30:53 -0700382 return __qdisc_dequeue_head(qdisc, list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 }
Thomas Graff87a9c32005-06-18 22:58:53 -0700385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return NULL;
387}
388
Thomas Graff87a9c32005-06-18 22:58:53 -0700389static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 qdisc->q.qlen++;
Thomas Graf321090e2005-06-18 22:58:35 -0700392 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Thomas Graff87a9c32005-06-18 22:58:53 -0700395static void pfifo_fast_reset(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 int prio;
398 struct sk_buff_head *list = qdisc_priv(qdisc);
399
Thomas Graff87a9c32005-06-18 22:58:53 -0700400 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
Thomas Graf821d24a2005-06-18 22:58:15 -0700401 __qdisc_reset_queue(qdisc, list + prio);
402
403 qdisc->qstats.backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 qdisc->q.qlen = 0;
405}
406
407static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
408{
Thomas Graff87a9c32005-06-18 22:58:53 -0700409 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
Patrick McHardy1e904742008-01-22 22:11:17 -0800412 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return skb->len;
414
Patrick McHardy1e904742008-01-22 22:11:17 -0800415nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -1;
417}
418
Patrick McHardy1e904742008-01-22 22:11:17 -0800419static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Thomas Graff87a9c32005-06-18 22:58:53 -0700421 int prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct sk_buff_head *list = qdisc_priv(qdisc);
423
Thomas Graff87a9c32005-06-18 22:58:53 -0700424 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
425 skb_queue_head_init(list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 return 0;
428}
429
Eric Dumazet20fea082007-11-14 01:44:41 -0800430static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 .id = "pfifo_fast",
Thomas Graff87a9c32005-06-18 22:58:53 -0700432 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 .enqueue = pfifo_fast_enqueue,
434 .dequeue = pfifo_fast_dequeue,
435 .requeue = pfifo_fast_requeue,
436 .init = pfifo_fast_init,
437 .reset = pfifo_fast_reset,
438 .dump = pfifo_fast_dump,
439 .owner = THIS_MODULE,
440};
441
Thomas Graf3d54b822005-07-05 14:15:09 -0700442struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 void *p;
445 struct Qdisc *sch;
Thomas Graf3d54b822005-07-05 14:15:09 -0700446 unsigned int size;
447 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* ensure that the Qdisc and the private data are 32-byte aligned */
Thomas Graf3d54b822005-07-05 14:15:09 -0700450 size = QDISC_ALIGN(sizeof(*sch));
451 size += ops->priv_size + (QDISC_ALIGNTO - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700453 p = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!p)
Thomas Graf3d54b822005-07-05 14:15:09 -0700455 goto errout;
Thomas Graf3d54b822005-07-05 14:15:09 -0700456 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
457 sch->padded = (char *) sch - (char *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 INIT_LIST_HEAD(&sch->list);
460 skb_queue_head_init(&sch->q);
461 sch->ops = ops;
462 sch->enqueue = ops->enqueue;
463 sch->dequeue = ops->dequeue;
464 sch->dev = dev;
465 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 atomic_set(&sch->refcnt, 1);
Thomas Graf3d54b822005-07-05 14:15:09 -0700467
468 return sch;
469errout:
470 return ERR_PTR(-err);
471}
472
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800473struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops,
474 unsigned int parentid)
Thomas Graf3d54b822005-07-05 14:15:09 -0700475{
476 struct Qdisc *sch;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900477
Thomas Graf3d54b822005-07-05 14:15:09 -0700478 sch = qdisc_alloc(dev, ops);
479 if (IS_ERR(sch))
480 goto errout;
Patrick McHardyfd44de72007-04-16 17:07:08 -0700481 sch->stats_lock = &dev->queue_lock;
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800482 sch->parent = parentid;
Thomas Graf3d54b822005-07-05 14:15:09 -0700483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (!ops->init || ops->init(sch, NULL) == 0)
485 return sch;
486
Thomas Graf0fbbeb12005-08-23 10:12:44 -0700487 qdisc_destroy(sch);
Thomas Graf3d54b822005-07-05 14:15:09 -0700488errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return NULL;
490}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800491EXPORT_SYMBOL(qdisc_create_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493/* Under dev->queue_lock and BH! */
494
495void qdisc_reset(struct Qdisc *qdisc)
496{
Eric Dumazet20fea082007-11-14 01:44:41 -0800497 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (ops->reset)
500 ops->reset(qdisc);
501}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800502EXPORT_SYMBOL(qdisc_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900504/* this is the rcu callback function to clean up a qdisc when there
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 * are no further references to it */
506
507static void __qdisc_destroy(struct rcu_head *head)
508{
509 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 kfree((char *) qdisc - qdisc->padded);
511}
512
513/* Under dev->queue_lock and BH! */
514
515void qdisc_destroy(struct Qdisc *qdisc)
516{
Eric Dumazet20fea082007-11-14 01:44:41 -0800517 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (qdisc->flags & TCQ_F_BUILTIN ||
Patrick McHardy85670cc2006-09-27 16:45:45 -0700520 !atomic_dec_and_test(&qdisc->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return;
522
Patrick McHardy85670cc2006-09-27 16:45:45 -0700523 list_del(&qdisc->list);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700524 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700525 if (ops->reset)
526 ops->reset(qdisc);
527 if (ops->destroy)
528 ops->destroy(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Patrick McHardy85670cc2006-09-27 16:45:45 -0700530 module_put(ops->owner);
531 dev_put(qdisc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
533}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800534EXPORT_SYMBOL(qdisc_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536void dev_activate(struct net_device *dev)
537{
538 /* No queueing discipline is attached to device;
539 create default one i.e. pfifo_fast for devices,
540 which need queueing and noqueue_qdisc for
541 virtual interfaces
542 */
543
544 if (dev->qdisc_sleeping == &noop_qdisc) {
545 struct Qdisc *qdisc;
546 if (dev->tx_queue_len) {
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800547 qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops,
548 TC_H_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (qdisc == NULL) {
550 printk(KERN_INFO "%s: activation failed\n", dev->name);
551 return;
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 list_add_tail(&qdisc->list, &dev->qdisc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 } else {
555 qdisc = &noqueue_qdisc;
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 dev->qdisc_sleeping = qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
Tommy S. Christensencacaddf2005-05-03 16:18:52 -0700560 if (!netif_carrier_ok(dev))
561 /* Delay activation until next carrier-on event */
562 return;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 spin_lock_bh(&dev->queue_lock);
565 rcu_assign_pointer(dev->qdisc, dev->qdisc_sleeping);
566 if (dev->qdisc != &noqueue_qdisc) {
567 dev->trans_start = jiffies;
568 dev_watchdog_up(dev);
569 }
570 spin_unlock_bh(&dev->queue_lock);
571}
572
573void dev_deactivate(struct net_device *dev)
574{
575 struct Qdisc *qdisc;
Herbert Xu41a23b02007-05-10 14:12:47 -0700576 struct sk_buff *skb;
Herbert Xuce0e32e2007-10-18 22:37:58 -0700577 int running;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 spin_lock_bh(&dev->queue_lock);
580 qdisc = dev->qdisc;
581 dev->qdisc = &noop_qdisc;
582
583 qdisc_reset(qdisc);
584
Herbert Xu41a23b02007-05-10 14:12:47 -0700585 skb = dev->gso_skb;
586 dev->gso_skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 spin_unlock_bh(&dev->queue_lock);
588
Herbert Xu41a23b02007-05-10 14:12:47 -0700589 kfree_skb(skb);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 dev_watchdog_down(dev);
592
Herbert Xuce0e32e2007-10-18 22:37:58 -0700593 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
Herbert Xud4828d82006-06-22 02:28:18 -0700594 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Herbert Xud4828d82006-06-22 02:28:18 -0700596 /* Wait for outstanding qdisc_run calls. */
Herbert Xuce0e32e2007-10-18 22:37:58 -0700597 do {
598 while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
599 yield();
600
601 /*
602 * Double-check inside queue lock to ensure that all effects
603 * of the queue run are visible when we return.
604 */
605 spin_lock_bh(&dev->queue_lock);
606 running = test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
607 spin_unlock_bh(&dev->queue_lock);
608
609 /*
610 * The running flag should never be set at this point because
611 * we've already set dev->qdisc to noop_qdisc *inside* the same
612 * pair of spin locks. That is, if any qdisc_run starts after
613 * our initial test it should see the noop_qdisc and then
614 * clear the RUNNING bit before dropping the queue lock. So
615 * if it is set here then we've found a bug.
616 */
617 } while (WARN_ON_ONCE(running));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620void dev_init_scheduler(struct net_device *dev)
621{
622 qdisc_lock_tree(dev);
623 dev->qdisc = &noop_qdisc;
624 dev->qdisc_sleeping = &noop_qdisc;
625 INIT_LIST_HEAD(&dev->qdisc_list);
626 qdisc_unlock_tree(dev);
627
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800628 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631void dev_shutdown(struct net_device *dev)
632{
633 struct Qdisc *qdisc;
634
635 qdisc_lock_tree(dev);
636 qdisc = dev->qdisc_sleeping;
637 dev->qdisc = &noop_qdisc;
638 dev->qdisc_sleeping = &noop_qdisc;
639 qdisc_destroy(qdisc);
640#if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900641 if ((qdisc = dev->qdisc_ingress) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 dev->qdisc_ingress = NULL;
643 qdisc_destroy(qdisc);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645#endif
646 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
647 qdisc_unlock_tree(dev);
648}