blob: 2f575b9017d12f65b45416be366cfafea58ed2a1 [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. Miller555353c2008-07-08 17:33:13 -070043 __acquires(dev->rx_queue.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
David S. Millere8a04642008-07-17 00:34:19 -070045 unsigned int i;
46
47 local_bh_disable();
48 for (i = 0; i < dev->num_tx_queues; i++) {
49 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
50 spin_lock(&txq->lock);
51 }
David S. Miller555353c2008-07-08 17:33:13 -070052 spin_lock(&dev->rx_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
Patrick McHardy62e3ba12008-01-22 22:10:23 -080054EXPORT_SYMBOL(qdisc_lock_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56void qdisc_unlock_tree(struct net_device *dev)
David S. Miller555353c2008-07-08 17:33:13 -070057 __releases(dev->rx_queue.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
David S. Millere8a04642008-07-17 00:34:19 -070059 unsigned int i;
60
David S. Miller555353c2008-07-08 17:33:13 -070061 spin_unlock(&dev->rx_queue.lock);
David S. Millere8a04642008-07-17 00:34:19 -070062 for (i = 0; i < dev->num_tx_queues; i++) {
63 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
64 spin_unlock(&txq->lock);
65 }
66 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
Patrick McHardy62e3ba12008-01-22 22:10:23 -080068EXPORT_SYMBOL(qdisc_unlock_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070070static inline int qdisc_qlen(struct Qdisc *q)
71{
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070072 return q->q.qlen;
73}
74
David S. Miller86d804e2008-07-08 23:11:25 -070075static inline int dev_requeue_skb(struct sk_buff *skb,
David S. Miller970565b2008-07-08 23:10:33 -070076 struct netdev_queue *dev_queue,
Krishna Kumar6c1361a2007-06-24 19:56:09 -070077 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070078{
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070079 if (unlikely(skb->next))
David S. Miller970565b2008-07-08 23:10:33 -070080 dev_queue->gso_skb = skb;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070081 else
82 q->ops->requeue(skb, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -070083
David S. Miller86d804e2008-07-08 23:11:25 -070084 netif_schedule_queue(dev_queue);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070085 return 0;
86}
87
David S. Millereb6aafe2008-07-08 23:12:38 -070088static inline struct sk_buff *dequeue_skb(struct netdev_queue *dev_queue,
89 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070090{
Krishna Kumar6c1361a2007-06-24 19:56:09 -070091 struct sk_buff *skb;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070092
David S. Miller970565b2008-07-08 23:10:33 -070093 if ((skb = dev_queue->gso_skb))
94 dev_queue->gso_skb = NULL;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070095 else
96 skb = q->dequeue(q);
97
98 return skb;
99}
100
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700101static inline int handle_dev_cpu_collision(struct sk_buff *skb,
David S. Miller970565b2008-07-08 23:10:33 -0700102 struct netdev_queue *dev_queue,
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700103 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700104{
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700105 int ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700106
David S. Millerc773e842008-07-08 23:13:53 -0700107 if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700108 /*
109 * Same CPU holding the lock. It may be a transient
110 * configuration error, when hard_start_xmit() recurses. We
111 * detect it by checking xmit owner and drop the packet when
112 * deadloop is detected. Return OK to try the next skb.
113 */
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700114 kfree_skb(skb);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700115 if (net_ratelimit())
116 printk(KERN_WARNING "Dead loop on netdevice %s, "
David S. Millerc773e842008-07-08 23:13:53 -0700117 "fix it urgently!\n", dev_queue->dev->name);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700118 ret = qdisc_qlen(q);
119 } else {
120 /*
121 * Another cpu is holding lock, requeue & delay xmits for
122 * some time.
123 */
124 __get_cpu_var(netdev_rx_stat).cpu_collision++;
David S. Miller86d804e2008-07-08 23:11:25 -0700125 ret = dev_requeue_skb(skb, dev_queue, q);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700126 }
127
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700128 return ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700129}
130
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900131/*
David S. Millerdc2b4842008-07-08 17:18:23 -0700132 * NOTE: Called under queue->lock with locally disabled BH.
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700133 *
David S. Miller79d16382008-07-08 23:14:46 -0700134 * __QUEUE_STATE_QDISC_RUNNING guarantees only one CPU can process
135 * this queue at a time. queue->lock serializes queue accesses for
136 * this queue AND txq->qdisc pointer itself.
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700137 *
138 * netif_tx_lock serializes accesses to device driver.
139 *
David S. Millerdc2b4842008-07-08 17:18:23 -0700140 * queue->lock and netif_tx_lock are mutually exclusive,
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700141 * if one is grabbed, another must be free.
142 *
143 * Note, that this procedure can be called by a watchdog timer
144 *
145 * Returns to the caller:
146 * 0 - queue is empty or throttled.
147 * >0 - queue is not empty.
148 *
149 */
David S. Millereb6aafe2008-07-08 23:12:38 -0700150static inline int qdisc_restart(struct netdev_queue *txq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
David S. Millerb0e1e642008-07-08 17:42:10 -0700152 struct Qdisc *q = txq->qdisc;
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800153 int ret = NETDEV_TX_BUSY;
David S. Millereb6aafe2008-07-08 23:12:38 -0700154 struct net_device *dev;
155 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700157 /* Dequeue packet */
David S. Millereb6aafe2008-07-08 23:12:38 -0700158 if (unlikely((skb = dequeue_skb(txq, q)) == NULL))
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700159 return 0;
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700160
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700161
162 /* And release queue */
David S. Millerb0e1e642008-07-08 17:42:10 -0700163 spin_unlock(&txq->lock);
Herbert Xud90df3a2007-05-10 04:55:14 -0700164
David S. Millereb6aafe2008-07-08 23:12:38 -0700165 dev = txq->dev;
166
David S. Millerc773e842008-07-08 23:13:53 -0700167 HARD_TX_LOCK(dev, txq, smp_processor_id());
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800168 if (!netif_subqueue_stopped(dev, skb))
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700169 ret = dev_hard_start_xmit(skb, dev, txq);
David S. Millerc773e842008-07-08 23:13:53 -0700170 HARD_TX_UNLOCK(dev, txq);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700171
David S. Millerb0e1e642008-07-08 17:42:10 -0700172 spin_lock(&txq->lock);
173 q = txq->qdisc;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700174
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700175 switch (ret) {
176 case NETDEV_TX_OK:
177 /* Driver sent out skb successfully */
178 ret = qdisc_qlen(q);
179 break;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700180
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700181 case NETDEV_TX_LOCKED:
182 /* Driver try lock failed */
David S. Millereb6aafe2008-07-08 23:12:38 -0700183 ret = handle_dev_cpu_collision(skb, txq, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700184 break;
185
186 default:
187 /* Driver returned NETDEV_TX_BUSY - requeue skb */
188 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
189 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
190 dev->name, ret, q->q.qlen);
191
David S. Miller86d804e2008-07-08 23:11:25 -0700192 ret = dev_requeue_skb(skb, txq, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700193 break;
194 }
195
196 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
David S. Millereb6aafe2008-07-08 23:12:38 -0700199void __qdisc_run(struct netdev_queue *txq)
Herbert Xu48d83322006-06-19 23:57:59 -0700200{
Herbert Xu2ba25062008-03-28 16:25:26 -0700201 unsigned long start_time = jiffies;
202
David S. Millereb6aafe2008-07-08 23:12:38 -0700203 while (qdisc_restart(txq)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700204 if (netif_tx_queue_stopped(txq))
Herbert Xud90df3a2007-05-10 04:55:14 -0700205 break;
Herbert Xu2ba25062008-03-28 16:25:26 -0700206
207 /*
208 * Postpone processing if
209 * 1. another process needs the CPU;
210 * 2. we've been doing it for too long.
211 */
212 if (need_resched() || jiffies != start_time) {
David S. Millereb6aafe2008-07-08 23:12:38 -0700213 netif_schedule_queue(txq);
Herbert Xu2ba25062008-03-28 16:25:26 -0700214 break;
215 }
216 }
Herbert Xu48d83322006-06-19 23:57:59 -0700217
David S. Miller79d16382008-07-08 23:14:46 -0700218 clear_bit(__QUEUE_STATE_QDISC_RUNNING, &txq->state);
Herbert Xu48d83322006-06-19 23:57:59 -0700219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static void dev_watchdog(unsigned long arg)
222{
223 struct net_device *dev = (struct net_device *)arg;
224
Herbert Xu932ff272006-06-09 12:20:56 -0700225 netif_tx_lock(dev);
David S. Millere8a04642008-07-17 00:34:19 -0700226 if (!qdisc_tx_is_noop(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (netif_device_present(dev) &&
228 netif_running(dev) &&
229 netif_carrier_ok(dev)) {
David S. Millere8a04642008-07-17 00:34:19 -0700230 int some_queue_stopped = 0;
231 unsigned int i;
Stephen Hemminger338f7562006-05-16 15:02:12 -0700232
David S. Millere8a04642008-07-17 00:34:19 -0700233 for (i = 0; i < dev->num_tx_queues; i++) {
234 struct netdev_queue *txq;
235
236 txq = netdev_get_tx_queue(dev, i);
237 if (netif_tx_queue_stopped(txq)) {
238 some_queue_stopped = 1;
239 break;
240 }
241 }
242
243 if (some_queue_stopped &&
244 time_after(jiffies, (dev->trans_start +
245 dev->watchdog_timeo))) {
246 printk(KERN_INFO "NETDEV WATCHDOG: %s: "
247 "transmit timed out\n",
Stephen Hemminger338f7562006-05-16 15:02:12 -0700248 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 dev->tx_timeout(dev);
Arjan van de Venb4192bb2008-05-02 16:21:07 -0700250 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
David S. Millere8a04642008-07-17 00:34:19 -0700252 if (!mod_timer(&dev->watchdog_timer,
253 round_jiffies(jiffies +
254 dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 dev_hold(dev);
256 }
257 }
Herbert Xu932ff272006-06-09 12:20:56 -0700258 netif_tx_unlock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 dev_put(dev);
261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263void __netdev_watchdog_up(struct net_device *dev)
264{
265 if (dev->tx_timeout) {
266 if (dev->watchdog_timeo <= 0)
267 dev->watchdog_timeo = 5*HZ;
Venkatesh Pallipadi60468d52007-05-31 21:28:44 -0700268 if (!mod_timer(&dev->watchdog_timer,
269 round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dev_hold(dev);
271 }
272}
273
274static void dev_watchdog_up(struct net_device *dev)
275{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 __netdev_watchdog_up(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279static void dev_watchdog_down(struct net_device *dev)
280{
Herbert Xu932ff272006-06-09 12:20:56 -0700281 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (del_timer(&dev->watchdog_timer))
Stephen Hemminger15333062006-03-20 22:32:28 -0800283 dev_put(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700284 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700287/**
288 * netif_carrier_on - set carrier
289 * @dev: network device
290 *
291 * Device has detected that carrier.
292 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700293void netif_carrier_on(struct net_device *dev)
294{
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700295 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700296 linkwatch_fire_event(dev);
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700297 if (netif_running(dev))
298 __netdev_watchdog_up(dev);
299 }
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700300}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800301EXPORT_SYMBOL(netif_carrier_on);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700302
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700303/**
304 * netif_carrier_off - clear carrier
305 * @dev: network device
306 *
307 * Device has detected loss of carrier.
308 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700309void netif_carrier_off(struct net_device *dev)
310{
311 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
312 linkwatch_fire_event(dev);
313}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800314EXPORT_SYMBOL(netif_carrier_off);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/* "NOOP" scheduler: the best scheduler, recommended for all interfaces
317 under all circumstances. It is difficult to invent anything faster or
318 cheaper.
319 */
320
Thomas Graf94df1092005-06-18 22:59:08 -0700321static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 kfree_skb(skb);
324 return NET_XMIT_CN;
325}
326
Thomas Graf94df1092005-06-18 22:59:08 -0700327static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 return NULL;
330}
331
Thomas Graf94df1092005-06-18 22:59:08 -0700332static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 if (net_ratelimit())
Thomas Graf94df1092005-06-18 22:59:08 -0700335 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
336 skb->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 kfree_skb(skb);
338 return NET_XMIT_CN;
339}
340
Eric Dumazet20fea082007-11-14 01:44:41 -0800341struct Qdisc_ops noop_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 .id = "noop",
343 .priv_size = 0,
344 .enqueue = noop_enqueue,
345 .dequeue = noop_dequeue,
346 .requeue = noop_requeue,
347 .owner = THIS_MODULE,
348};
349
350struct Qdisc noop_qdisc = {
351 .enqueue = noop_enqueue,
352 .dequeue = noop_dequeue,
353 .flags = TCQ_F_BUILTIN,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900354 .ops = &noop_qdisc_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 .list = LIST_HEAD_INIT(noop_qdisc.list),
356};
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800357EXPORT_SYMBOL(noop_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Eric Dumazet20fea082007-11-14 01:44:41 -0800359static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 .id = "noqueue",
361 .priv_size = 0,
362 .enqueue = noop_enqueue,
363 .dequeue = noop_dequeue,
364 .requeue = noop_requeue,
365 .owner = THIS_MODULE,
366};
367
368static struct Qdisc noqueue_qdisc = {
369 .enqueue = NULL,
370 .dequeue = noop_dequeue,
371 .flags = TCQ_F_BUILTIN,
372 .ops = &noqueue_qdisc_ops,
373 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
374};
375
376
377static const u8 prio2band[TC_PRIO_MAX+1] =
378 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
379
380/* 3-band FIFO queue: old style, but should be a bit faster than
381 generic prio+fifo combination.
382 */
383
Thomas Graff87a9c32005-06-18 22:58:53 -0700384#define PFIFO_FAST_BANDS 3
385
Thomas Graf321090e2005-06-18 22:58:35 -0700386static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
387 struct Qdisc *qdisc)
388{
389 struct sk_buff_head *list = qdisc_priv(qdisc);
390 return list + prio2band[skb->priority & TC_PRIO_MAX];
391}
392
Thomas Graff87a9c32005-06-18 22:58:53 -0700393static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Thomas Graf321090e2005-06-18 22:58:35 -0700395 struct sk_buff_head *list = prio2list(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
David S. Miller5ce2d482008-07-08 17:06:30 -0700397 if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 qdisc->q.qlen++;
Thomas Graf821d24ae2005-06-18 22:58:15 -0700399 return __qdisc_enqueue_tail(skb, qdisc, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Thomas Graf821d24ae2005-06-18 22:58:15 -0700401
402 return qdisc_drop(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Thomas Graff87a9c32005-06-18 22:58:53 -0700405static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 int prio;
408 struct sk_buff_head *list = qdisc_priv(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Thomas Graf452f2992005-07-18 13:30:53 -0700410 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
411 if (!skb_queue_empty(list + prio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 qdisc->q.qlen--;
Thomas Graf452f2992005-07-18 13:30:53 -0700413 return __qdisc_dequeue_head(qdisc, list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 }
Thomas Graff87a9c32005-06-18 22:58:53 -0700416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return NULL;
418}
419
Thomas Graff87a9c32005-06-18 22:58:53 -0700420static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 qdisc->q.qlen++;
Thomas Graf321090e2005-06-18 22:58:35 -0700423 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Thomas Graff87a9c32005-06-18 22:58:53 -0700426static void pfifo_fast_reset(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 int prio;
429 struct sk_buff_head *list = qdisc_priv(qdisc);
430
Thomas Graff87a9c32005-06-18 22:58:53 -0700431 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
Thomas Graf821d24ae2005-06-18 22:58:15 -0700432 __qdisc_reset_queue(qdisc, list + prio);
433
434 qdisc->qstats.backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 qdisc->q.qlen = 0;
436}
437
438static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
439{
Thomas Graff87a9c32005-06-18 22:58:53 -0700440 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
Patrick McHardy1e904742008-01-22 22:11:17 -0800443 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return skb->len;
445
Patrick McHardy1e904742008-01-22 22:11:17 -0800446nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -1;
448}
449
Patrick McHardy1e904742008-01-22 22:11:17 -0800450static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Thomas Graff87a9c32005-06-18 22:58:53 -0700452 int prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct sk_buff_head *list = qdisc_priv(qdisc);
454
Thomas Graff87a9c32005-06-18 22:58:53 -0700455 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
456 skb_queue_head_init(list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 return 0;
459}
460
Eric Dumazet20fea082007-11-14 01:44:41 -0800461static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 .id = "pfifo_fast",
Thomas Graff87a9c32005-06-18 22:58:53 -0700463 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 .enqueue = pfifo_fast_enqueue,
465 .dequeue = pfifo_fast_dequeue,
466 .requeue = pfifo_fast_requeue,
467 .init = pfifo_fast_init,
468 .reset = pfifo_fast_reset,
469 .dump = pfifo_fast_dump,
470 .owner = THIS_MODULE,
471};
472
David S. Miller5ce2d482008-07-08 17:06:30 -0700473struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -0700474 struct Qdisc_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 void *p;
477 struct Qdisc *sch;
Thomas Graf3d54b822005-07-05 14:15:09 -0700478 unsigned int size;
479 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /* ensure that the Qdisc and the private data are 32-byte aligned */
Thomas Graf3d54b822005-07-05 14:15:09 -0700482 size = QDISC_ALIGN(sizeof(*sch));
483 size += ops->priv_size + (QDISC_ALIGNTO - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700485 p = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (!p)
Thomas Graf3d54b822005-07-05 14:15:09 -0700487 goto errout;
Thomas Graf3d54b822005-07-05 14:15:09 -0700488 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
489 sch->padded = (char *) sch - (char *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 INIT_LIST_HEAD(&sch->list);
492 skb_queue_head_init(&sch->q);
493 sch->ops = ops;
494 sch->enqueue = ops->enqueue;
495 sch->dequeue = ops->dequeue;
David S. Millerbb949fb2008-07-08 16:55:56 -0700496 sch->dev_queue = dev_queue;
David S. Miller5ce2d482008-07-08 17:06:30 -0700497 dev_hold(qdisc_dev(sch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 atomic_set(&sch->refcnt, 1);
Thomas Graf3d54b822005-07-05 14:15:09 -0700499
500 return sch;
501errout:
WANG Cong01e123d2008-06-27 19:51:35 -0700502 return ERR_PTR(err);
Thomas Graf3d54b822005-07-05 14:15:09 -0700503}
504
David S. Millerbb949fb2008-07-08 16:55:56 -0700505struct Qdisc * qdisc_create_dflt(struct net_device *dev,
506 struct netdev_queue *dev_queue,
507 struct Qdisc_ops *ops,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800508 unsigned int parentid)
Thomas Graf3d54b822005-07-05 14:15:09 -0700509{
510 struct Qdisc *sch;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900511
David S. Miller5ce2d482008-07-08 17:06:30 -0700512 sch = qdisc_alloc(dev_queue, ops);
Thomas Graf3d54b822005-07-05 14:15:09 -0700513 if (IS_ERR(sch))
514 goto errout;
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800515 sch->parent = parentid;
Thomas Graf3d54b822005-07-05 14:15:09 -0700516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (!ops->init || ops->init(sch, NULL) == 0)
518 return sch;
519
Thomas Graf0fbbeb12005-08-23 10:12:44 -0700520 qdisc_destroy(sch);
Thomas Graf3d54b822005-07-05 14:15:09 -0700521errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return NULL;
523}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800524EXPORT_SYMBOL(qdisc_create_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
David S. Millerdc2b4842008-07-08 17:18:23 -0700526/* Under queue->lock and BH! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528void qdisc_reset(struct Qdisc *qdisc)
529{
Eric Dumazet20fea082007-11-14 01:44:41 -0800530 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 if (ops->reset)
533 ops->reset(qdisc);
534}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800535EXPORT_SYMBOL(qdisc_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900537/* this is the rcu callback function to clean up a qdisc when there
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * are no further references to it */
539
540static void __qdisc_destroy(struct rcu_head *head)
541{
542 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 kfree((char *) qdisc - qdisc->padded);
544}
545
David S. Millerdc2b4842008-07-08 17:18:23 -0700546/* Under queue->lock and BH! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548void qdisc_destroy(struct Qdisc *qdisc)
549{
Eric Dumazet20fea082007-11-14 01:44:41 -0800550 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 if (qdisc->flags & TCQ_F_BUILTIN ||
Patrick McHardy85670cc2006-09-27 16:45:45 -0700553 !atomic_dec_and_test(&qdisc->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return;
555
Patrick McHardy85670cc2006-09-27 16:45:45 -0700556 list_del(&qdisc->list);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700557 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700558 if (ops->reset)
559 ops->reset(qdisc);
560 if (ops->destroy)
561 ops->destroy(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Patrick McHardy85670cc2006-09-27 16:45:45 -0700563 module_put(ops->owner);
David S. Miller5ce2d482008-07-08 17:06:30 -0700564 dev_put(qdisc_dev(qdisc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
566}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800567EXPORT_SYMBOL(qdisc_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
David S. Millere8a04642008-07-17 00:34:19 -0700569static bool dev_all_qdisc_sleeping_noop(struct net_device *dev)
570{
571 unsigned int i;
572
573 for (i = 0; i < dev->num_tx_queues; i++) {
574 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
575
576 if (txq->qdisc_sleeping != &noop_qdisc)
577 return false;
578 }
579 return true;
580}
581
582static void attach_one_default_qdisc(struct net_device *dev,
583 struct netdev_queue *dev_queue,
584 void *_unused)
585{
586 struct Qdisc *qdisc;
587
588 if (dev->tx_queue_len) {
589 qdisc = qdisc_create_dflt(dev, dev_queue,
590 &pfifo_fast_ops, TC_H_ROOT);
591 if (!qdisc) {
592 printk(KERN_INFO "%s: activation failed\n", dev->name);
593 return;
594 }
595 list_add_tail(&qdisc->list, &dev_queue->qdisc_list);
596 } else {
597 qdisc = &noqueue_qdisc;
598 }
599 dev_queue->qdisc_sleeping = qdisc;
600}
601
602static void transition_one_qdisc(struct net_device *dev,
603 struct netdev_queue *dev_queue,
604 void *_need_watchdog)
605{
606 int *need_watchdog_p = _need_watchdog;
607
608 spin_lock_bh(&dev_queue->lock);
609 rcu_assign_pointer(dev_queue->qdisc, dev_queue->qdisc_sleeping);
610 if (dev_queue->qdisc != &noqueue_qdisc)
611 *need_watchdog_p = 1;
612 spin_unlock_bh(&dev_queue->lock);
613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615void dev_activate(struct net_device *dev)
616{
David S. Millere8a04642008-07-17 00:34:19 -0700617 int need_watchdog;
David S. Millerb0e1e642008-07-08 17:42:10 -0700618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* No queueing discipline is attached to device;
620 create default one i.e. pfifo_fast for devices,
621 which need queueing and noqueue_qdisc for
622 virtual interfaces
623 */
624
David S. Millere8a04642008-07-17 00:34:19 -0700625 if (dev_all_qdisc_sleeping_noop(dev))
626 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Tommy S. Christensencacaddf2005-05-03 16:18:52 -0700628 if (!netif_carrier_ok(dev))
629 /* Delay activation until next carrier-on event */
630 return;
631
David S. Millere8a04642008-07-17 00:34:19 -0700632 need_watchdog = 0;
633 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
634
635 if (need_watchdog) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 dev->trans_start = jiffies;
637 dev_watchdog_up(dev);
638 }
David S. Millerb0e1e642008-07-08 17:42:10 -0700639}
640
David S. Millere8a04642008-07-17 00:34:19 -0700641static void dev_deactivate_queue(struct net_device *dev,
642 struct netdev_queue *dev_queue,
643 void *_qdisc_default)
David S. Millerb0e1e642008-07-08 17:42:10 -0700644{
David S. Millere8a04642008-07-17 00:34:19 -0700645 struct Qdisc *qdisc_default = _qdisc_default;
David S. Miller970565b2008-07-08 23:10:33 -0700646 struct Qdisc *qdisc;
647 struct sk_buff *skb;
David S. Millerb0e1e642008-07-08 17:42:10 -0700648
David S. Miller970565b2008-07-08 23:10:33 -0700649 spin_lock_bh(&dev_queue->lock);
650
651 qdisc = dev_queue->qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -0700652 if (qdisc) {
653 dev_queue->qdisc = qdisc_default;
654 qdisc_reset(qdisc);
655 }
David S. Miller970565b2008-07-08 23:10:33 -0700656 skb = dev_queue->gso_skb;
657 dev_queue->gso_skb = NULL;
658
659 spin_unlock_bh(&dev_queue->lock);
660
661 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
David S. Millere8a04642008-07-17 00:34:19 -0700664static bool some_qdisc_is_running(struct net_device *dev, int lock)
665{
666 unsigned int i;
667
668 for (i = 0; i < dev->num_tx_queues; i++) {
669 struct netdev_queue *dev_queue;
670 int val;
671
672 dev_queue = netdev_get_tx_queue(dev, i);
673
674 if (lock)
675 spin_lock_bh(&dev_queue->lock);
676
677 val = test_bit(__QUEUE_STATE_QDISC_RUNNING, &dev_queue->state);
678
679 if (lock)
680 spin_unlock_bh(&dev_queue->lock);
681
682 if (val)
683 return true;
684 }
685 return false;
686}
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688void dev_deactivate(struct net_device *dev)
689{
David S. Millere8a04642008-07-17 00:34:19 -0700690 bool running;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
David S. Millere8a04642008-07-17 00:34:19 -0700692 netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
Herbert Xu41a23b02007-05-10 14:12:47 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 dev_watchdog_down(dev);
695
Herbert Xuce0e32e2007-10-18 22:37:58 -0700696 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
Herbert Xud4828d82006-06-22 02:28:18 -0700697 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Herbert Xud4828d82006-06-22 02:28:18 -0700699 /* Wait for outstanding qdisc_run calls. */
Herbert Xuce0e32e2007-10-18 22:37:58 -0700700 do {
David S. Millere8a04642008-07-17 00:34:19 -0700701 while (some_qdisc_is_running(dev, 0))
Herbert Xuce0e32e2007-10-18 22:37:58 -0700702 yield();
703
704 /*
705 * Double-check inside queue lock to ensure that all effects
706 * of the queue run are visible when we return.
707 */
David S. Millere8a04642008-07-17 00:34:19 -0700708 running = some_qdisc_is_running(dev, 1);
Herbert Xuce0e32e2007-10-18 22:37:58 -0700709
710 /*
711 * The running flag should never be set at this point because
712 * we've already set dev->qdisc to noop_qdisc *inside* the same
713 * pair of spin locks. That is, if any qdisc_run starts after
714 * our initial test it should see the noop_qdisc and then
715 * clear the RUNNING bit before dropping the queue lock. So
716 * if it is set here then we've found a bug.
717 */
718 } while (WARN_ON_ONCE(running));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
David S. Millerb0e1e642008-07-08 17:42:10 -0700721static void dev_init_scheduler_queue(struct net_device *dev,
722 struct netdev_queue *dev_queue,
David S. Millere8a04642008-07-17 00:34:19 -0700723 void *_qdisc)
David S. Millerb0e1e642008-07-08 17:42:10 -0700724{
David S. Millere8a04642008-07-17 00:34:19 -0700725 struct Qdisc *qdisc = _qdisc;
726
David S. Millerb0e1e642008-07-08 17:42:10 -0700727 dev_queue->qdisc = qdisc;
728 dev_queue->qdisc_sleeping = qdisc;
729 INIT_LIST_HEAD(&dev_queue->qdisc_list);
730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732void dev_init_scheduler(struct net_device *dev)
733{
734 qdisc_lock_tree(dev);
David S. Millere8a04642008-07-17 00:34:19 -0700735 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
David S. Millerb0e1e642008-07-08 17:42:10 -0700736 dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 qdisc_unlock_tree(dev);
738
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800739 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
David S. Millere8a04642008-07-17 00:34:19 -0700742static void shutdown_scheduler_queue(struct net_device *dev,
743 struct netdev_queue *dev_queue,
744 void *_qdisc_default)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
David S. Millerb0e1e642008-07-08 17:42:10 -0700746 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
David S. Millere8a04642008-07-17 00:34:19 -0700747 struct Qdisc *qdisc_default = _qdisc_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
David S. Millerb0e1e642008-07-08 17:42:10 -0700749 if (qdisc) {
750 dev_queue->qdisc = qdisc_default;
751 dev_queue->qdisc_sleeping = qdisc_default;
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 qdisc_destroy(qdisc);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900754 }
David S. Millerb0e1e642008-07-08 17:42:10 -0700755}
756
757void dev_shutdown(struct net_device *dev)
758{
759 qdisc_lock_tree(dev);
David S. Millere8a04642008-07-17 00:34:19 -0700760 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
761 shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
763 qdisc_unlock_tree(dev);
764}