blob: ea5a05b172c4d137ef39250f8aa6c27e07830056 [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}
49
50void qdisc_unlock_tree(struct net_device *dev)
Eric Dumazet9a429c42008-01-01 21:58:02 -080051 __releases(dev->ingress_lock)
52 __releases(dev->queue_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Patrick McHardyfd44de72007-04-16 17:07:08 -070054 spin_unlock(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 spin_unlock_bh(&dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070058static inline int qdisc_qlen(struct Qdisc *q)
59{
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070060 return q->q.qlen;
61}
62
Krishna Kumar6c1361a2007-06-24 19:56:09 -070063static inline int dev_requeue_skb(struct sk_buff *skb, struct net_device *dev,
64 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070065{
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070066 if (unlikely(skb->next))
67 dev->gso_skb = skb;
68 else
69 q->ops->requeue(skb, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -070070
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070071 netif_schedule(dev);
72 return 0;
73}
74
Krishna Kumar6c1361a2007-06-24 19:56:09 -070075static inline struct sk_buff *dev_dequeue_skb(struct net_device *dev,
76 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070077{
Krishna Kumar6c1361a2007-06-24 19:56:09 -070078 struct sk_buff *skb;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070079
Krishna Kumar6c1361a2007-06-24 19:56:09 -070080 if ((skb = dev->gso_skb))
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070081 dev->gso_skb = NULL;
82 else
83 skb = q->dequeue(q);
84
85 return skb;
86}
87
Krishna Kumar6c1361a2007-06-24 19:56:09 -070088static inline int handle_dev_cpu_collision(struct sk_buff *skb,
89 struct net_device *dev,
90 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070091{
Krishna Kumar6c1361a2007-06-24 19:56:09 -070092 int ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070093
Krishna Kumar6c1361a2007-06-24 19:56:09 -070094 if (unlikely(dev->xmit_lock_owner == smp_processor_id())) {
95 /*
96 * Same CPU holding the lock. It may be a transient
97 * configuration error, when hard_start_xmit() recurses. We
98 * detect it by checking xmit owner and drop the packet when
99 * deadloop is detected. Return OK to try the next skb.
100 */
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700101 kfree_skb(skb);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700102 if (net_ratelimit())
103 printk(KERN_WARNING "Dead loop on netdevice %s, "
104 "fix it urgently!\n", dev->name);
105 ret = qdisc_qlen(q);
106 } else {
107 /*
108 * Another cpu is holding lock, requeue & delay xmits for
109 * some time.
110 */
111 __get_cpu_var(netdev_rx_stat).cpu_collision++;
112 ret = dev_requeue_skb(skb, dev, q);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700113 }
114
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700115 return ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700116}
117
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900118/*
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700119 * NOTE: Called under dev->queue_lock with locally disabled BH.
120 *
121 * __LINK_STATE_QDISC_RUNNING guarantees only one CPU can process this
122 * device at a time. dev->queue_lock serializes queue accesses for
123 * this device AND dev->qdisc pointer itself.
124 *
125 * netif_tx_lock serializes accesses to device driver.
126 *
127 * dev->queue_lock and netif_tx_lock are mutually exclusive,
128 * if one is grabbed, another must be free.
129 *
130 * Note, that this procedure can be called by a watchdog timer
131 *
132 * Returns to the caller:
133 * 0 - queue is empty or throttled.
134 * >0 - queue is not empty.
135 *
136 */
Herbert Xu48d83322006-06-19 23:57:59 -0700137static inline int qdisc_restart(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 struct Qdisc *q = dev->qdisc;
140 struct sk_buff *skb;
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800141 int ret = NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700143 /* Dequeue packet */
144 if (unlikely((skb = dev_dequeue_skb(dev, q)) == NULL))
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700145 return 0;
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700146
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700147
148 /* And release queue */
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700149 spin_unlock(&dev->queue_lock);
Herbert Xud90df3a2007-05-10 04:55:14 -0700150
Jamal Hadi Salim82366322007-09-25 19:27:13 -0700151 HARD_TX_LOCK(dev, smp_processor_id());
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800152 if (!netif_subqueue_stopped(dev, skb))
153 ret = dev_hard_start_xmit(skb, dev);
Jamal Hadi Salim82366322007-09-25 19:27:13 -0700154 HARD_TX_UNLOCK(dev);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700155
156 spin_lock(&dev->queue_lock);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700157 q = dev->qdisc;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700158
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700159 switch (ret) {
160 case NETDEV_TX_OK:
161 /* Driver sent out skb successfully */
162 ret = qdisc_qlen(q);
163 break;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700164
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700165 case NETDEV_TX_LOCKED:
166 /* Driver try lock failed */
167 ret = handle_dev_cpu_collision(skb, dev, q);
168 break;
169
170 default:
171 /* Driver returned NETDEV_TX_BUSY - requeue skb */
172 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
173 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
174 dev->name, ret, q->q.qlen);
175
176 ret = dev_requeue_skb(skb, dev, q);
177 break;
178 }
179
180 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Herbert Xu48d83322006-06-19 23:57:59 -0700183void __qdisc_run(struct net_device *dev)
184{
Herbert Xud90df3a2007-05-10 04:55:14 -0700185 do {
186 if (!qdisc_restart(dev))
187 break;
188 } while (!netif_queue_stopped(dev));
Herbert Xu48d83322006-06-19 23:57:59 -0700189
190 clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static void dev_watchdog(unsigned long arg)
194{
195 struct net_device *dev = (struct net_device *)arg;
196
Herbert Xu932ff272006-06-09 12:20:56 -0700197 netif_tx_lock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (dev->qdisc != &noop_qdisc) {
199 if (netif_device_present(dev) &&
200 netif_running(dev) &&
201 netif_carrier_ok(dev)) {
202 if (netif_queue_stopped(dev) &&
Stephen Hemminger338f7562006-05-16 15:02:12 -0700203 time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) {
204
205 printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n",
206 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 dev->tx_timeout(dev);
208 }
Arjan van de Venf5a6e012007-02-05 17:59:51 -0800209 if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 dev_hold(dev);
211 }
212 }
Herbert Xu932ff272006-06-09 12:20:56 -0700213 netif_tx_unlock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 dev_put(dev);
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218void __netdev_watchdog_up(struct net_device *dev)
219{
220 if (dev->tx_timeout) {
221 if (dev->watchdog_timeo <= 0)
222 dev->watchdog_timeo = 5*HZ;
Venkatesh Pallipadi60468d52007-05-31 21:28:44 -0700223 if (!mod_timer(&dev->watchdog_timer,
224 round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 dev_hold(dev);
226 }
227}
228
229static void dev_watchdog_up(struct net_device *dev)
230{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 __netdev_watchdog_up(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234static void dev_watchdog_down(struct net_device *dev)
235{
Herbert Xu932ff272006-06-09 12:20:56 -0700236 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (del_timer(&dev->watchdog_timer))
Stephen Hemminger15333062006-03-20 22:32:28 -0800238 dev_put(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700239 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700242/**
243 * netif_carrier_on - set carrier
244 * @dev: network device
245 *
246 * Device has detected that carrier.
247 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700248void netif_carrier_on(struct net_device *dev)
249{
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700250 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700251 linkwatch_fire_event(dev);
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700252 if (netif_running(dev))
253 __netdev_watchdog_up(dev);
254 }
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700255}
256
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700257/**
258 * netif_carrier_off - clear carrier
259 * @dev: network device
260 *
261 * Device has detected loss of carrier.
262 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700263void netif_carrier_off(struct net_device *dev)
264{
265 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
266 linkwatch_fire_event(dev);
267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/* "NOOP" scheduler: the best scheduler, recommended for all interfaces
270 under all circumstances. It is difficult to invent anything faster or
271 cheaper.
272 */
273
Thomas Graf94df1092005-06-18 22:59:08 -0700274static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 kfree_skb(skb);
277 return NET_XMIT_CN;
278}
279
Thomas Graf94df1092005-06-18 22:59:08 -0700280static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 return NULL;
283}
284
Thomas Graf94df1092005-06-18 22:59:08 -0700285static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 if (net_ratelimit())
Thomas Graf94df1092005-06-18 22:59:08 -0700288 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
289 skb->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 kfree_skb(skb);
291 return NET_XMIT_CN;
292}
293
Eric Dumazet20fea082007-11-14 01:44:41 -0800294struct Qdisc_ops noop_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 .id = "noop",
296 .priv_size = 0,
297 .enqueue = noop_enqueue,
298 .dequeue = noop_dequeue,
299 .requeue = noop_requeue,
300 .owner = THIS_MODULE,
301};
302
303struct Qdisc noop_qdisc = {
304 .enqueue = noop_enqueue,
305 .dequeue = noop_dequeue,
306 .flags = TCQ_F_BUILTIN,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900307 .ops = &noop_qdisc_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 .list = LIST_HEAD_INIT(noop_qdisc.list),
309};
310
Eric Dumazet20fea082007-11-14 01:44:41 -0800311static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 .id = "noqueue",
313 .priv_size = 0,
314 .enqueue = noop_enqueue,
315 .dequeue = noop_dequeue,
316 .requeue = noop_requeue,
317 .owner = THIS_MODULE,
318};
319
320static struct Qdisc noqueue_qdisc = {
321 .enqueue = NULL,
322 .dequeue = noop_dequeue,
323 .flags = TCQ_F_BUILTIN,
324 .ops = &noqueue_qdisc_ops,
325 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
326};
327
328
329static const u8 prio2band[TC_PRIO_MAX+1] =
330 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
331
332/* 3-band FIFO queue: old style, but should be a bit faster than
333 generic prio+fifo combination.
334 */
335
Thomas Graff87a9c32005-06-18 22:58:53 -0700336#define PFIFO_FAST_BANDS 3
337
Thomas Graf321090e2005-06-18 22:58:35 -0700338static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
339 struct Qdisc *qdisc)
340{
341 struct sk_buff_head *list = qdisc_priv(qdisc);
342 return list + prio2band[skb->priority & TC_PRIO_MAX];
343}
344
Thomas Graff87a9c32005-06-18 22:58:53 -0700345static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Thomas Graf321090e2005-06-18 22:58:35 -0700347 struct sk_buff_head *list = prio2list(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Thomas Graf821d24a2005-06-18 22:58:15 -0700349 if (skb_queue_len(list) < qdisc->dev->tx_queue_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 qdisc->q.qlen++;
Thomas Graf821d24a2005-06-18 22:58:15 -0700351 return __qdisc_enqueue_tail(skb, qdisc, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Thomas Graf821d24a2005-06-18 22:58:15 -0700353
354 return qdisc_drop(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Thomas Graff87a9c32005-06-18 22:58:53 -0700357static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 int prio;
360 struct sk_buff_head *list = qdisc_priv(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Thomas Graf452f2992005-07-18 13:30:53 -0700362 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
363 if (!skb_queue_empty(list + prio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 qdisc->q.qlen--;
Thomas Graf452f2992005-07-18 13:30:53 -0700365 return __qdisc_dequeue_head(qdisc, list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367 }
Thomas Graff87a9c32005-06-18 22:58:53 -0700368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return NULL;
370}
371
Thomas Graff87a9c32005-06-18 22:58:53 -0700372static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 qdisc->q.qlen++;
Thomas Graf321090e2005-06-18 22:58:35 -0700375 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Thomas Graff87a9c32005-06-18 22:58:53 -0700378static void pfifo_fast_reset(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 int prio;
381 struct sk_buff_head *list = qdisc_priv(qdisc);
382
Thomas Graff87a9c32005-06-18 22:58:53 -0700383 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
Thomas Graf821d24a2005-06-18 22:58:15 -0700384 __qdisc_reset_queue(qdisc, list + prio);
385
386 qdisc->qstats.backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 qdisc->q.qlen = 0;
388}
389
390static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
391{
Thomas Graff87a9c32005-06-18 22:58:53 -0700392 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
395 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
396 return skb->len;
397
398rtattr_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return -1;
400}
401
402static int pfifo_fast_init(struct Qdisc *qdisc, struct rtattr *opt)
403{
Thomas Graff87a9c32005-06-18 22:58:53 -0700404 int prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct sk_buff_head *list = qdisc_priv(qdisc);
406
Thomas Graff87a9c32005-06-18 22:58:53 -0700407 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
408 skb_queue_head_init(list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 return 0;
411}
412
Eric Dumazet20fea082007-11-14 01:44:41 -0800413static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 .id = "pfifo_fast",
Thomas Graff87a9c32005-06-18 22:58:53 -0700415 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 .enqueue = pfifo_fast_enqueue,
417 .dequeue = pfifo_fast_dequeue,
418 .requeue = pfifo_fast_requeue,
419 .init = pfifo_fast_init,
420 .reset = pfifo_fast_reset,
421 .dump = pfifo_fast_dump,
422 .owner = THIS_MODULE,
423};
424
Thomas Graf3d54b822005-07-05 14:15:09 -0700425struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 void *p;
428 struct Qdisc *sch;
Thomas Graf3d54b822005-07-05 14:15:09 -0700429 unsigned int size;
430 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* ensure that the Qdisc and the private data are 32-byte aligned */
Thomas Graf3d54b822005-07-05 14:15:09 -0700433 size = QDISC_ALIGN(sizeof(*sch));
434 size += ops->priv_size + (QDISC_ALIGNTO - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700436 p = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!p)
Thomas Graf3d54b822005-07-05 14:15:09 -0700438 goto errout;
Thomas Graf3d54b822005-07-05 14:15:09 -0700439 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
440 sch->padded = (char *) sch - (char *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 INIT_LIST_HEAD(&sch->list);
443 skb_queue_head_init(&sch->q);
444 sch->ops = ops;
445 sch->enqueue = ops->enqueue;
446 sch->dequeue = ops->dequeue;
447 sch->dev = dev;
448 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 atomic_set(&sch->refcnt, 1);
Thomas Graf3d54b822005-07-05 14:15:09 -0700450
451 return sch;
452errout:
453 return ERR_PTR(-err);
454}
455
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800456struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops,
457 unsigned int parentid)
Thomas Graf3d54b822005-07-05 14:15:09 -0700458{
459 struct Qdisc *sch;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900460
Thomas Graf3d54b822005-07-05 14:15:09 -0700461 sch = qdisc_alloc(dev, ops);
462 if (IS_ERR(sch))
463 goto errout;
Patrick McHardyfd44de72007-04-16 17:07:08 -0700464 sch->stats_lock = &dev->queue_lock;
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800465 sch->parent = parentid;
Thomas Graf3d54b822005-07-05 14:15:09 -0700466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!ops->init || ops->init(sch, NULL) == 0)
468 return sch;
469
Thomas Graf0fbbeb12005-08-23 10:12:44 -0700470 qdisc_destroy(sch);
Thomas Graf3d54b822005-07-05 14:15:09 -0700471errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return NULL;
473}
474
475/* Under dev->queue_lock and BH! */
476
477void qdisc_reset(struct Qdisc *qdisc)
478{
Eric Dumazet20fea082007-11-14 01:44:41 -0800479 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (ops->reset)
482 ops->reset(qdisc);
483}
484
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900485/* this is the rcu callback function to clean up a qdisc when there
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * are no further references to it */
487
488static void __qdisc_destroy(struct rcu_head *head)
489{
490 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 kfree((char *) qdisc - qdisc->padded);
492}
493
494/* Under dev->queue_lock and BH! */
495
496void qdisc_destroy(struct Qdisc *qdisc)
497{
Eric Dumazet20fea082007-11-14 01:44:41 -0800498 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 if (qdisc->flags & TCQ_F_BUILTIN ||
Patrick McHardy85670cc2006-09-27 16:45:45 -0700501 !atomic_dec_and_test(&qdisc->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return;
503
Patrick McHardy85670cc2006-09-27 16:45:45 -0700504 list_del(&qdisc->list);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700505 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700506 if (ops->reset)
507 ops->reset(qdisc);
508 if (ops->destroy)
509 ops->destroy(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Patrick McHardy85670cc2006-09-27 16:45:45 -0700511 module_put(ops->owner);
512 dev_put(qdisc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
514}
515
516void dev_activate(struct net_device *dev)
517{
518 /* No queueing discipline is attached to device;
519 create default one i.e. pfifo_fast for devices,
520 which need queueing and noqueue_qdisc for
521 virtual interfaces
522 */
523
524 if (dev->qdisc_sleeping == &noop_qdisc) {
525 struct Qdisc *qdisc;
526 if (dev->tx_queue_len) {
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800527 qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops,
528 TC_H_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (qdisc == NULL) {
530 printk(KERN_INFO "%s: activation failed\n", dev->name);
531 return;
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 list_add_tail(&qdisc->list, &dev->qdisc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 } else {
535 qdisc = &noqueue_qdisc;
536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 dev->qdisc_sleeping = qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
Tommy S. Christensencacaddf2005-05-03 16:18:52 -0700540 if (!netif_carrier_ok(dev))
541 /* Delay activation until next carrier-on event */
542 return;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 spin_lock_bh(&dev->queue_lock);
545 rcu_assign_pointer(dev->qdisc, dev->qdisc_sleeping);
546 if (dev->qdisc != &noqueue_qdisc) {
547 dev->trans_start = jiffies;
548 dev_watchdog_up(dev);
549 }
550 spin_unlock_bh(&dev->queue_lock);
551}
552
553void dev_deactivate(struct net_device *dev)
554{
555 struct Qdisc *qdisc;
Herbert Xu41a23b02007-05-10 14:12:47 -0700556 struct sk_buff *skb;
Herbert Xuce0e32e2007-10-18 22:37:58 -0700557 int running;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 spin_lock_bh(&dev->queue_lock);
560 qdisc = dev->qdisc;
561 dev->qdisc = &noop_qdisc;
562
563 qdisc_reset(qdisc);
564
Herbert Xu41a23b02007-05-10 14:12:47 -0700565 skb = dev->gso_skb;
566 dev->gso_skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 spin_unlock_bh(&dev->queue_lock);
568
Herbert Xu41a23b02007-05-10 14:12:47 -0700569 kfree_skb(skb);
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 dev_watchdog_down(dev);
572
Herbert Xuce0e32e2007-10-18 22:37:58 -0700573 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
Herbert Xud4828d82006-06-22 02:28:18 -0700574 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Herbert Xud4828d82006-06-22 02:28:18 -0700576 /* Wait for outstanding qdisc_run calls. */
Herbert Xuce0e32e2007-10-18 22:37:58 -0700577 do {
578 while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
579 yield();
580
581 /*
582 * Double-check inside queue lock to ensure that all effects
583 * of the queue run are visible when we return.
584 */
585 spin_lock_bh(&dev->queue_lock);
586 running = test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
587 spin_unlock_bh(&dev->queue_lock);
588
589 /*
590 * The running flag should never be set at this point because
591 * we've already set dev->qdisc to noop_qdisc *inside* the same
592 * pair of spin locks. That is, if any qdisc_run starts after
593 * our initial test it should see the noop_qdisc and then
594 * clear the RUNNING bit before dropping the queue lock. So
595 * if it is set here then we've found a bug.
596 */
597 } while (WARN_ON_ONCE(running));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600void dev_init_scheduler(struct net_device *dev)
601{
602 qdisc_lock_tree(dev);
603 dev->qdisc = &noop_qdisc;
604 dev->qdisc_sleeping = &noop_qdisc;
605 INIT_LIST_HEAD(&dev->qdisc_list);
606 qdisc_unlock_tree(dev);
607
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800608 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611void dev_shutdown(struct net_device *dev)
612{
613 struct Qdisc *qdisc;
614
615 qdisc_lock_tree(dev);
616 qdisc = dev->qdisc_sleeping;
617 dev->qdisc = &noop_qdisc;
618 dev->qdisc_sleeping = &noop_qdisc;
619 qdisc_destroy(qdisc);
620#if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE)
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900621 if ((qdisc = dev->qdisc_ingress) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 dev->qdisc_ingress = NULL;
623 qdisc_destroy(qdisc);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625#endif
626 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
627 qdisc_unlock_tree(dev);
628}
629
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700630EXPORT_SYMBOL(netif_carrier_on);
631EXPORT_SYMBOL(netif_carrier_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632EXPORT_SYMBOL(noop_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633EXPORT_SYMBOL(qdisc_create_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634EXPORT_SYMBOL(qdisc_destroy);
635EXPORT_SYMBOL(qdisc_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636EXPORT_SYMBOL(qdisc_lock_tree);
637EXPORT_SYMBOL(qdisc_unlock_tree);