blob: fcc7533f0bccf8603c018f5a575660e05f9e65b8 [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. Millerdc2b4842008-07-08 17:18:23 -070032 * queue->lock spinlock.
Patrick McHardy0463d4a2007-04-16 17:02:10 -070033 *
34 * The idea is the following:
35 * - enqueue, dequeue are serialized via top level device
David S. Millerdc2b4842008-07-08 17:18:23 -070036 * spinlock queue->lock.
Patrick McHardyfd44de72007-04-16 17:07:08 -070037 * - ingress filtering is serialized via top level device
David S. Miller555353c2008-07-08 17:33:13 -070038 * spinlock dev->rx_queue.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)
David S. Millerdc2b4842008-07-08 17:18:23 -070043 __acquires(dev->tx_queue.lock)
David S. Miller555353c2008-07-08 17:33:13 -070044 __acquires(dev->rx_queue.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
David S. Millerdc2b4842008-07-08 17:18:23 -070046 spin_lock_bh(&dev->tx_queue.lock);
David S. Miller555353c2008-07-08 17:33:13 -070047 spin_lock(&dev->rx_queue.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)
David S. Miller555353c2008-07-08 17:33:13 -070052 __releases(dev->rx_queue.lock)
David S. Millerdc2b4842008-07-08 17:18:23 -070053 __releases(dev->tx_queue.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
David S. Miller555353c2008-07-08 17:33:13 -070055 spin_unlock(&dev->rx_queue.lock);
David S. Millerdc2b4842008-07-08 17:18:23 -070056 spin_unlock_bh(&dev->tx_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
David S. Miller86d804e2008-07-08 23:11:25 -070065static inline int dev_requeue_skb(struct sk_buff *skb,
David S. Miller970565b2008-07-08 23:10:33 -070066 struct netdev_queue *dev_queue,
Krishna Kumar6c1361a2007-06-24 19:56:09 -070067 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070068{
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070069 if (unlikely(skb->next))
David S. Miller970565b2008-07-08 23:10:33 -070070 dev_queue->gso_skb = skb;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070071 else
72 q->ops->requeue(skb, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -070073
David S. Miller86d804e2008-07-08 23:11:25 -070074 netif_schedule_queue(dev_queue);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070075 return 0;
76}
77
David S. Millereb6aafe2008-07-08 23:12:38 -070078static inline struct sk_buff *dequeue_skb(struct netdev_queue *dev_queue,
79 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070080{
Krishna Kumar6c1361a2007-06-24 19:56:09 -070081 struct sk_buff *skb;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070082
David S. Miller970565b2008-07-08 23:10:33 -070083 if ((skb = dev_queue->gso_skb))
84 dev_queue->gso_skb = NULL;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070085 else
86 skb = q->dequeue(q);
87
88 return skb;
89}
90
Krishna Kumar6c1361a2007-06-24 19:56:09 -070091static inline int handle_dev_cpu_collision(struct sk_buff *skb,
David S. Miller970565b2008-07-08 23:10:33 -070092 struct netdev_queue *dev_queue,
Krishna Kumar6c1361a2007-06-24 19:56:09 -070093 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070094{
David S. Millereb6aafe2008-07-08 23:12:38 -070095 struct net_device *dev = dev_queue->dev;
Krishna Kumar6c1361a2007-06-24 19:56:09 -070096 int ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070097
Krishna Kumar6c1361a2007-06-24 19:56:09 -070098 if (unlikely(dev->xmit_lock_owner == smp_processor_id())) {
99 /*
100 * Same CPU holding the lock. It may be a transient
101 * configuration error, when hard_start_xmit() recurses. We
102 * detect it by checking xmit owner and drop the packet when
103 * deadloop is detected. Return OK to try the next skb.
104 */
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700105 kfree_skb(skb);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700106 if (net_ratelimit())
107 printk(KERN_WARNING "Dead loop on netdevice %s, "
108 "fix it urgently!\n", dev->name);
109 ret = qdisc_qlen(q);
110 } else {
111 /*
112 * Another cpu is holding lock, requeue & delay xmits for
113 * some time.
114 */
115 __get_cpu_var(netdev_rx_stat).cpu_collision++;
David S. Miller86d804e2008-07-08 23:11:25 -0700116 ret = dev_requeue_skb(skb, dev_queue, q);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700117 }
118
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700119 return ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700120}
121
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900122/*
David S. Millerdc2b4842008-07-08 17:18:23 -0700123 * NOTE: Called under queue->lock with locally disabled BH.
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700124 *
125 * __LINK_STATE_QDISC_RUNNING guarantees only one CPU can process this
David S. Millerdc2b4842008-07-08 17:18:23 -0700126 * device at a time. queue->lock serializes queue accesses for
David S. Millerb0e1e642008-07-08 17:42:10 -0700127 * this device AND txq->qdisc pointer itself.
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700128 *
129 * netif_tx_lock serializes accesses to device driver.
130 *
David S. Millerdc2b4842008-07-08 17:18:23 -0700131 * queue->lock and netif_tx_lock are mutually exclusive,
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700132 * if one is grabbed, another must be free.
133 *
134 * Note, that this procedure can be called by a watchdog timer
135 *
136 * Returns to the caller:
137 * 0 - queue is empty or throttled.
138 * >0 - queue is not empty.
139 *
140 */
David S. Millereb6aafe2008-07-08 23:12:38 -0700141static inline int qdisc_restart(struct netdev_queue *txq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
David S. Millerb0e1e642008-07-08 17:42:10 -0700143 struct Qdisc *q = txq->qdisc;
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800144 int ret = NETDEV_TX_BUSY;
David S. Millereb6aafe2008-07-08 23:12:38 -0700145 struct net_device *dev;
146 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700148 /* Dequeue packet */
David S. Millereb6aafe2008-07-08 23:12:38 -0700149 if (unlikely((skb = dequeue_skb(txq, q)) == NULL))
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700150 return 0;
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700151
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700152
153 /* And release queue */
David S. Millerb0e1e642008-07-08 17:42:10 -0700154 spin_unlock(&txq->lock);
Herbert Xud90df3a2007-05-10 04:55:14 -0700155
David S. Millereb6aafe2008-07-08 23:12:38 -0700156 dev = txq->dev;
157
Jamal Hadi Salim82366322007-09-25 19:27:13 -0700158 HARD_TX_LOCK(dev, smp_processor_id());
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800159 if (!netif_subqueue_stopped(dev, skb))
160 ret = dev_hard_start_xmit(skb, dev);
Jamal Hadi Salim82366322007-09-25 19:27:13 -0700161 HARD_TX_UNLOCK(dev);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700162
David S. Millerb0e1e642008-07-08 17:42:10 -0700163 spin_lock(&txq->lock);
164 q = txq->qdisc;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700165
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700166 switch (ret) {
167 case NETDEV_TX_OK:
168 /* Driver sent out skb successfully */
169 ret = qdisc_qlen(q);
170 break;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700171
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700172 case NETDEV_TX_LOCKED:
173 /* Driver try lock failed */
David S. Millereb6aafe2008-07-08 23:12:38 -0700174 ret = handle_dev_cpu_collision(skb, txq, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700175 break;
176
177 default:
178 /* Driver returned NETDEV_TX_BUSY - requeue skb */
179 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
180 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
181 dev->name, ret, q->q.qlen);
182
David S. Miller86d804e2008-07-08 23:11:25 -0700183 ret = dev_requeue_skb(skb, txq, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700184 break;
185 }
186
187 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
David S. Millereb6aafe2008-07-08 23:12:38 -0700190void __qdisc_run(struct netdev_queue *txq)
Herbert Xu48d83322006-06-19 23:57:59 -0700191{
David S. Millereb6aafe2008-07-08 23:12:38 -0700192 struct net_device *dev = txq->dev;
Herbert Xu2ba25062008-03-28 16:25:26 -0700193 unsigned long start_time = jiffies;
194
David S. Millereb6aafe2008-07-08 23:12:38 -0700195 while (qdisc_restart(txq)) {
Herbert Xu2ba25062008-03-28 16:25:26 -0700196 if (netif_queue_stopped(dev))
Herbert Xud90df3a2007-05-10 04:55:14 -0700197 break;
Herbert Xu2ba25062008-03-28 16:25:26 -0700198
199 /*
200 * Postpone processing if
201 * 1. another process needs the CPU;
202 * 2. we've been doing it for too long.
203 */
204 if (need_resched() || jiffies != start_time) {
David S. Millereb6aafe2008-07-08 23:12:38 -0700205 netif_schedule_queue(txq);
Herbert Xu2ba25062008-03-28 16:25:26 -0700206 break;
207 }
208 }
Herbert Xu48d83322006-06-19 23:57:59 -0700209
210 clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static void dev_watchdog(unsigned long arg)
214{
215 struct net_device *dev = (struct net_device *)arg;
David S. Millerb0e1e642008-07-08 17:42:10 -0700216 struct netdev_queue *txq = &dev->tx_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Herbert Xu932ff272006-06-09 12:20:56 -0700218 netif_tx_lock(dev);
David S. Millerb0e1e642008-07-08 17:42:10 -0700219 if (txq->qdisc != &noop_qdisc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (netif_device_present(dev) &&
221 netif_running(dev) &&
222 netif_carrier_ok(dev)) {
223 if (netif_queue_stopped(dev) &&
Stephen Hemminger338f7562006-05-16 15:02:12 -0700224 time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) {
225
226 printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n",
227 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 dev->tx_timeout(dev);
Arjan van de Venb4192bb2008-05-02 16:21:07 -0700229 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Arjan van de Venf5a6e012007-02-05 17:59:51 -0800231 if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 dev_hold(dev);
233 }
234 }
Herbert Xu932ff272006-06-09 12:20:56 -0700235 netif_tx_unlock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 dev_put(dev);
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240void __netdev_watchdog_up(struct net_device *dev)
241{
242 if (dev->tx_timeout) {
243 if (dev->watchdog_timeo <= 0)
244 dev->watchdog_timeo = 5*HZ;
Venkatesh Pallipadi60468d52007-05-31 21:28:44 -0700245 if (!mod_timer(&dev->watchdog_timer,
246 round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 dev_hold(dev);
248 }
249}
250
251static void dev_watchdog_up(struct net_device *dev)
252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 __netdev_watchdog_up(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256static void dev_watchdog_down(struct net_device *dev)
257{
Herbert Xu932ff272006-06-09 12:20:56 -0700258 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (del_timer(&dev->watchdog_timer))
Stephen Hemminger15333062006-03-20 22:32:28 -0800260 dev_put(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700261 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700264/**
265 * netif_carrier_on - set carrier
266 * @dev: network device
267 *
268 * Device has detected that carrier.
269 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700270void netif_carrier_on(struct net_device *dev)
271{
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700272 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700273 linkwatch_fire_event(dev);
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700274 if (netif_running(dev))
275 __netdev_watchdog_up(dev);
276 }
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700277}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800278EXPORT_SYMBOL(netif_carrier_on);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700279
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700280/**
281 * netif_carrier_off - clear carrier
282 * @dev: network device
283 *
284 * Device has detected loss of carrier.
285 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700286void netif_carrier_off(struct net_device *dev)
287{
288 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
289 linkwatch_fire_event(dev);
290}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800291EXPORT_SYMBOL(netif_carrier_off);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/* "NOOP" scheduler: the best scheduler, recommended for all interfaces
294 under all circumstances. It is difficult to invent anything faster or
295 cheaper.
296 */
297
Thomas Graf94df1092005-06-18 22:59:08 -0700298static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 kfree_skb(skb);
301 return NET_XMIT_CN;
302}
303
Thomas Graf94df1092005-06-18 22:59:08 -0700304static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 return NULL;
307}
308
Thomas Graf94df1092005-06-18 22:59:08 -0700309static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 if (net_ratelimit())
Thomas Graf94df1092005-06-18 22:59:08 -0700312 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
313 skb->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 kfree_skb(skb);
315 return NET_XMIT_CN;
316}
317
Eric Dumazet20fea082007-11-14 01:44:41 -0800318struct Qdisc_ops noop_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 .id = "noop",
320 .priv_size = 0,
321 .enqueue = noop_enqueue,
322 .dequeue = noop_dequeue,
323 .requeue = noop_requeue,
324 .owner = THIS_MODULE,
325};
326
327struct Qdisc noop_qdisc = {
328 .enqueue = noop_enqueue,
329 .dequeue = noop_dequeue,
330 .flags = TCQ_F_BUILTIN,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900331 .ops = &noop_qdisc_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 .list = LIST_HEAD_INIT(noop_qdisc.list),
333};
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800334EXPORT_SYMBOL(noop_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Eric Dumazet20fea082007-11-14 01:44:41 -0800336static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 .id = "noqueue",
338 .priv_size = 0,
339 .enqueue = noop_enqueue,
340 .dequeue = noop_dequeue,
341 .requeue = noop_requeue,
342 .owner = THIS_MODULE,
343};
344
345static struct Qdisc noqueue_qdisc = {
346 .enqueue = NULL,
347 .dequeue = noop_dequeue,
348 .flags = TCQ_F_BUILTIN,
349 .ops = &noqueue_qdisc_ops,
350 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
351};
352
353
354static const u8 prio2band[TC_PRIO_MAX+1] =
355 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
356
357/* 3-band FIFO queue: old style, but should be a bit faster than
358 generic prio+fifo combination.
359 */
360
Thomas Graff87a9c32005-06-18 22:58:53 -0700361#define PFIFO_FAST_BANDS 3
362
Thomas Graf321090e2005-06-18 22:58:35 -0700363static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
364 struct Qdisc *qdisc)
365{
366 struct sk_buff_head *list = qdisc_priv(qdisc);
367 return list + prio2band[skb->priority & TC_PRIO_MAX];
368}
369
Thomas Graff87a9c32005-06-18 22:58:53 -0700370static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Thomas Graf321090e2005-06-18 22:58:35 -0700372 struct sk_buff_head *list = prio2list(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
David S. Miller5ce2d482008-07-08 17:06:30 -0700374 if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 qdisc->q.qlen++;
Thomas Graf821d24a2005-06-18 22:58:15 -0700376 return __qdisc_enqueue_tail(skb, qdisc, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
Thomas Graf821d24a2005-06-18 22:58:15 -0700378
379 return qdisc_drop(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Thomas Graff87a9c32005-06-18 22:58:53 -0700382static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 int prio;
385 struct sk_buff_head *list = qdisc_priv(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Thomas Graf452f2992005-07-18 13:30:53 -0700387 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
388 if (!skb_queue_empty(list + prio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 qdisc->q.qlen--;
Thomas Graf452f2992005-07-18 13:30:53 -0700390 return __qdisc_dequeue_head(qdisc, list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392 }
Thomas Graff87a9c32005-06-18 22:58:53 -0700393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return NULL;
395}
396
Thomas Graff87a9c32005-06-18 22:58:53 -0700397static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 qdisc->q.qlen++;
Thomas Graf321090e2005-06-18 22:58:35 -0700400 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Thomas Graff87a9c32005-06-18 22:58:53 -0700403static void pfifo_fast_reset(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 int prio;
406 struct sk_buff_head *list = qdisc_priv(qdisc);
407
Thomas Graff87a9c32005-06-18 22:58:53 -0700408 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
Thomas Graf821d24a2005-06-18 22:58:15 -0700409 __qdisc_reset_queue(qdisc, list + prio);
410
411 qdisc->qstats.backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 qdisc->q.qlen = 0;
413}
414
415static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
416{
Thomas Graff87a9c32005-06-18 22:58:53 -0700417 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
Patrick McHardy1e904742008-01-22 22:11:17 -0800420 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return skb->len;
422
Patrick McHardy1e904742008-01-22 22:11:17 -0800423nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return -1;
425}
426
Patrick McHardy1e904742008-01-22 22:11:17 -0800427static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Thomas Graff87a9c32005-06-18 22:58:53 -0700429 int prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct sk_buff_head *list = qdisc_priv(qdisc);
431
Thomas Graff87a9c32005-06-18 22:58:53 -0700432 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
433 skb_queue_head_init(list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 return 0;
436}
437
Eric Dumazet20fea082007-11-14 01:44:41 -0800438static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 .id = "pfifo_fast",
Thomas Graff87a9c32005-06-18 22:58:53 -0700440 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 .enqueue = pfifo_fast_enqueue,
442 .dequeue = pfifo_fast_dequeue,
443 .requeue = pfifo_fast_requeue,
444 .init = pfifo_fast_init,
445 .reset = pfifo_fast_reset,
446 .dump = pfifo_fast_dump,
447 .owner = THIS_MODULE,
448};
449
David S. Miller5ce2d482008-07-08 17:06:30 -0700450struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -0700451 struct Qdisc_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 void *p;
454 struct Qdisc *sch;
Thomas Graf3d54b822005-07-05 14:15:09 -0700455 unsigned int size;
456 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* ensure that the Qdisc and the private data are 32-byte aligned */
Thomas Graf3d54b822005-07-05 14:15:09 -0700459 size = QDISC_ALIGN(sizeof(*sch));
460 size += ops->priv_size + (QDISC_ALIGNTO - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700462 p = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (!p)
Thomas Graf3d54b822005-07-05 14:15:09 -0700464 goto errout;
Thomas Graf3d54b822005-07-05 14:15:09 -0700465 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
466 sch->padded = (char *) sch - (char *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 INIT_LIST_HEAD(&sch->list);
469 skb_queue_head_init(&sch->q);
470 sch->ops = ops;
471 sch->enqueue = ops->enqueue;
472 sch->dequeue = ops->dequeue;
David S. Millerbb949fb2008-07-08 16:55:56 -0700473 sch->dev_queue = dev_queue;
David S. Miller5ce2d482008-07-08 17:06:30 -0700474 dev_hold(qdisc_dev(sch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 atomic_set(&sch->refcnt, 1);
Thomas Graf3d54b822005-07-05 14:15:09 -0700476
477 return sch;
478errout:
WANG Cong01e123d2008-06-27 19:51:35 -0700479 return ERR_PTR(err);
Thomas Graf3d54b822005-07-05 14:15:09 -0700480}
481
David S. Millerbb949fb2008-07-08 16:55:56 -0700482struct Qdisc * qdisc_create_dflt(struct net_device *dev,
483 struct netdev_queue *dev_queue,
484 struct Qdisc_ops *ops,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800485 unsigned int parentid)
Thomas Graf3d54b822005-07-05 14:15:09 -0700486{
487 struct Qdisc *sch;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900488
David S. Miller5ce2d482008-07-08 17:06:30 -0700489 sch = qdisc_alloc(dev_queue, ops);
Thomas Graf3d54b822005-07-05 14:15:09 -0700490 if (IS_ERR(sch))
491 goto errout;
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800492 sch->parent = parentid;
Thomas Graf3d54b822005-07-05 14:15:09 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (!ops->init || ops->init(sch, NULL) == 0)
495 return sch;
496
Thomas Graf0fbbeb12005-08-23 10:12:44 -0700497 qdisc_destroy(sch);
Thomas Graf3d54b822005-07-05 14:15:09 -0700498errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return NULL;
500}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800501EXPORT_SYMBOL(qdisc_create_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
David S. Millerdc2b4842008-07-08 17:18:23 -0700503/* Under queue->lock and BH! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505void qdisc_reset(struct Qdisc *qdisc)
506{
Eric Dumazet20fea082007-11-14 01:44:41 -0800507 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (ops->reset)
510 ops->reset(qdisc);
511}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800512EXPORT_SYMBOL(qdisc_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900514/* this is the rcu callback function to clean up a qdisc when there
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * are no further references to it */
516
517static void __qdisc_destroy(struct rcu_head *head)
518{
519 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 kfree((char *) qdisc - qdisc->padded);
521}
522
David S. Millerdc2b4842008-07-08 17:18:23 -0700523/* Under queue->lock and BH! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525void qdisc_destroy(struct Qdisc *qdisc)
526{
Eric Dumazet20fea082007-11-14 01:44:41 -0800527 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (qdisc->flags & TCQ_F_BUILTIN ||
Patrick McHardy85670cc2006-09-27 16:45:45 -0700530 !atomic_dec_and_test(&qdisc->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return;
532
Patrick McHardy85670cc2006-09-27 16:45:45 -0700533 list_del(&qdisc->list);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700534 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700535 if (ops->reset)
536 ops->reset(qdisc);
537 if (ops->destroy)
538 ops->destroy(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Patrick McHardy85670cc2006-09-27 16:45:45 -0700540 module_put(ops->owner);
David S. Miller5ce2d482008-07-08 17:06:30 -0700541 dev_put(qdisc_dev(qdisc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
543}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800544EXPORT_SYMBOL(qdisc_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546void dev_activate(struct net_device *dev)
547{
David S. Millerb0e1e642008-07-08 17:42:10 -0700548 struct netdev_queue *txq = &dev->tx_queue;
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* No queueing discipline is attached to device;
551 create default one i.e. pfifo_fast for devices,
552 which need queueing and noqueue_qdisc for
553 virtual interfaces
554 */
555
David S. Millerb0e1e642008-07-08 17:42:10 -0700556 if (txq->qdisc_sleeping == &noop_qdisc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct Qdisc *qdisc;
558 if (dev->tx_queue_len) {
David S. Millerb0e1e642008-07-08 17:42:10 -0700559 qdisc = qdisc_create_dflt(dev, txq,
David S. Millerbb949fb2008-07-08 16:55:56 -0700560 &pfifo_fast_ops,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800561 TC_H_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (qdisc == NULL) {
563 printk(KERN_INFO "%s: activation failed\n", dev->name);
564 return;
565 }
David S. Millerb0e1e642008-07-08 17:42:10 -0700566 list_add_tail(&qdisc->list, &txq->qdisc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 } else {
568 qdisc = &noqueue_qdisc;
569 }
David S. Millerb0e1e642008-07-08 17:42:10 -0700570 txq->qdisc_sleeping = qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572
Tommy S. Christensencacaddf2005-05-03 16:18:52 -0700573 if (!netif_carrier_ok(dev))
574 /* Delay activation until next carrier-on event */
575 return;
576
David S. Millerb0e1e642008-07-08 17:42:10 -0700577 spin_lock_bh(&txq->lock);
578 rcu_assign_pointer(txq->qdisc, txq->qdisc_sleeping);
579 if (txq->qdisc != &noqueue_qdisc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 dev->trans_start = jiffies;
581 dev_watchdog_up(dev);
582 }
David S. Millerb0e1e642008-07-08 17:42:10 -0700583 spin_unlock_bh(&txq->lock);
584}
585
David S. Miller970565b2008-07-08 23:10:33 -0700586static void dev_deactivate_queue(struct netdev_queue *dev_queue,
David S. Millerb0e1e642008-07-08 17:42:10 -0700587 struct Qdisc *qdisc_default)
588{
David S. Miller970565b2008-07-08 23:10:33 -0700589 struct Qdisc *qdisc;
590 struct sk_buff *skb;
David S. Millerb0e1e642008-07-08 17:42:10 -0700591
David S. Miller970565b2008-07-08 23:10:33 -0700592 spin_lock_bh(&dev_queue->lock);
593
594 qdisc = dev_queue->qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -0700595 if (qdisc) {
596 dev_queue->qdisc = qdisc_default;
597 qdisc_reset(qdisc);
598 }
David S. Miller970565b2008-07-08 23:10:33 -0700599 skb = dev_queue->gso_skb;
600 dev_queue->gso_skb = NULL;
601
602 spin_unlock_bh(&dev_queue->lock);
603
604 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607void dev_deactivate(struct net_device *dev)
608{
Herbert Xuce0e32e2007-10-18 22:37:58 -0700609 int running;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
David S. Miller970565b2008-07-08 23:10:33 -0700611 dev_deactivate_queue(&dev->tx_queue, &noop_qdisc);
Herbert Xu41a23b02007-05-10 14:12:47 -0700612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 dev_watchdog_down(dev);
614
Herbert Xuce0e32e2007-10-18 22:37:58 -0700615 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
Herbert Xud4828d82006-06-22 02:28:18 -0700616 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Herbert Xud4828d82006-06-22 02:28:18 -0700618 /* Wait for outstanding qdisc_run calls. */
Herbert Xuce0e32e2007-10-18 22:37:58 -0700619 do {
620 while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state))
621 yield();
622
623 /*
624 * Double-check inside queue lock to ensure that all effects
625 * of the queue run are visible when we return.
626 */
David S. Millerdc2b4842008-07-08 17:18:23 -0700627 spin_lock_bh(&dev->tx_queue.lock);
Herbert Xuce0e32e2007-10-18 22:37:58 -0700628 running = test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state);
David S. Millerdc2b4842008-07-08 17:18:23 -0700629 spin_unlock_bh(&dev->tx_queue.lock);
Herbert Xuce0e32e2007-10-18 22:37:58 -0700630
631 /*
632 * The running flag should never be set at this point because
633 * we've already set dev->qdisc to noop_qdisc *inside* the same
634 * pair of spin locks. That is, if any qdisc_run starts after
635 * our initial test it should see the noop_qdisc and then
636 * clear the RUNNING bit before dropping the queue lock. So
637 * if it is set here then we've found a bug.
638 */
639 } while (WARN_ON_ONCE(running));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
David S. Millerb0e1e642008-07-08 17:42:10 -0700642static void dev_init_scheduler_queue(struct net_device *dev,
643 struct netdev_queue *dev_queue,
644 struct Qdisc *qdisc)
645{
646 dev_queue->qdisc = qdisc;
647 dev_queue->qdisc_sleeping = qdisc;
648 INIT_LIST_HEAD(&dev_queue->qdisc_list);
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651void dev_init_scheduler(struct net_device *dev)
652{
653 qdisc_lock_tree(dev);
David S. Millerb0e1e642008-07-08 17:42:10 -0700654 dev_init_scheduler_queue(dev, &dev->tx_queue, &noop_qdisc);
655 dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 qdisc_unlock_tree(dev);
657
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800658 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
David S. Millerb0e1e642008-07-08 17:42:10 -0700661static void dev_shutdown_scheduler_queue(struct net_device *dev,
662 struct netdev_queue *dev_queue,
663 struct Qdisc *qdisc_default)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
David S. Millerb0e1e642008-07-08 17:42:10 -0700665 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
David S. Millerb0e1e642008-07-08 17:42:10 -0700667 if (qdisc) {
668 dev_queue->qdisc = qdisc_default;
669 dev_queue->qdisc_sleeping = qdisc_default;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 qdisc_destroy(qdisc);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900672 }
David S. Millerb0e1e642008-07-08 17:42:10 -0700673}
674
675void dev_shutdown(struct net_device *dev)
676{
677 qdisc_lock_tree(dev);
678 dev_shutdown_scheduler_queue(dev, &dev->tx_queue, &noop_qdisc);
679 dev_shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
681 qdisc_unlock_tree(dev);
682}