blob: 2bd75befa066262b5aad3dc677381534058655c9 [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. Millerd3b753d2008-07-15 20:14:35 -070080 q->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. Millerd3b753d2008-07-15 20:14:35 -070088static inline struct sk_buff *dequeue_skb(struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070089{
Krishna Kumar6c1361a2007-06-24 19:56:09 -070090 struct sk_buff *skb;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070091
David S. Millerd3b753d2008-07-15 20:14:35 -070092 if ((skb = q->gso_skb))
93 q->gso_skb = NULL;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070094 else
95 skb = q->dequeue(q);
96
97 return skb;
98}
99
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700100static inline int handle_dev_cpu_collision(struct sk_buff *skb,
David S. Miller970565b2008-07-08 23:10:33 -0700101 struct netdev_queue *dev_queue,
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700102 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700103{
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700104 int ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700105
David S. Millerc773e842008-07-08 23:13:53 -0700106 if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700107 /*
108 * Same CPU holding the lock. It may be a transient
109 * configuration error, when hard_start_xmit() recurses. We
110 * detect it by checking xmit owner and drop the packet when
111 * deadloop is detected. Return OK to try the next skb.
112 */
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700113 kfree_skb(skb);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700114 if (net_ratelimit())
115 printk(KERN_WARNING "Dead loop on netdevice %s, "
David S. Millerc773e842008-07-08 23:13:53 -0700116 "fix it urgently!\n", dev_queue->dev->name);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700117 ret = qdisc_qlen(q);
118 } else {
119 /*
120 * Another cpu is holding lock, requeue & delay xmits for
121 * some time.
122 */
123 __get_cpu_var(netdev_rx_stat).cpu_collision++;
David S. Miller86d804e2008-07-08 23:11:25 -0700124 ret = dev_requeue_skb(skb, dev_queue, q);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700125 }
126
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700127 return ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700128}
129
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900130/*
David S. Millerdc2b4842008-07-08 17:18:23 -0700131 * NOTE: Called under queue->lock with locally disabled BH.
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700132 *
David S. Miller79d16382008-07-08 23:14:46 -0700133 * __QUEUE_STATE_QDISC_RUNNING guarantees only one CPU can process
134 * this queue at a time. queue->lock serializes queue accesses for
135 * this queue AND txq->qdisc pointer itself.
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700136 *
137 * netif_tx_lock serializes accesses to device driver.
138 *
David S. Millerdc2b4842008-07-08 17:18:23 -0700139 * queue->lock and netif_tx_lock are mutually exclusive,
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700140 * if one is grabbed, another must be free.
141 *
142 * Note, that this procedure can be called by a watchdog timer
143 *
144 * Returns to the caller:
145 * 0 - queue is empty or throttled.
146 * >0 - queue is not empty.
147 *
148 */
David S. Millereb6aafe2008-07-08 23:12:38 -0700149static inline int qdisc_restart(struct netdev_queue *txq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
David S. Millerb0e1e642008-07-08 17:42:10 -0700151 struct Qdisc *q = txq->qdisc;
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800152 int ret = NETDEV_TX_BUSY;
David S. Millereb6aafe2008-07-08 23:12:38 -0700153 struct net_device *dev;
154 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700156 /* Dequeue packet */
David S. Millerd3b753d2008-07-15 20:14:35 -0700157 if (unlikely((skb = dequeue_skb(q)) == NULL))
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700158 return 0;
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700159
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700160 /* And release queue */
David S. Millerb0e1e642008-07-08 17:42:10 -0700161 spin_unlock(&txq->lock);
Herbert Xud90df3a2007-05-10 04:55:14 -0700162
David S. Millereb6aafe2008-07-08 23:12:38 -0700163 dev = txq->dev;
164
David S. Millerc773e842008-07-08 23:13:53 -0700165 HARD_TX_LOCK(dev, txq, smp_processor_id());
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800166 if (!netif_subqueue_stopped(dev, skb))
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700167 ret = dev_hard_start_xmit(skb, dev, txq);
David S. Millerc773e842008-07-08 23:13:53 -0700168 HARD_TX_UNLOCK(dev, txq);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700169
David S. Millerb0e1e642008-07-08 17:42:10 -0700170 spin_lock(&txq->lock);
171 q = txq->qdisc;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700172
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700173 switch (ret) {
174 case NETDEV_TX_OK:
175 /* Driver sent out skb successfully */
176 ret = qdisc_qlen(q);
177 break;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700178
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700179 case NETDEV_TX_LOCKED:
180 /* Driver try lock failed */
David S. Millereb6aafe2008-07-08 23:12:38 -0700181 ret = handle_dev_cpu_collision(skb, txq, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700182 break;
183
184 default:
185 /* Driver returned NETDEV_TX_BUSY - requeue skb */
186 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
187 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
188 dev->name, ret, q->q.qlen);
189
David S. Miller86d804e2008-07-08 23:11:25 -0700190 ret = dev_requeue_skb(skb, txq, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700191 break;
192 }
193
194 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
David S. Millereb6aafe2008-07-08 23:12:38 -0700197void __qdisc_run(struct netdev_queue *txq)
Herbert Xu48d83322006-06-19 23:57:59 -0700198{
Herbert Xu2ba25062008-03-28 16:25:26 -0700199 unsigned long start_time = jiffies;
200
David S. Millereb6aafe2008-07-08 23:12:38 -0700201 while (qdisc_restart(txq)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700202 if (netif_tx_queue_stopped(txq))
Herbert Xud90df3a2007-05-10 04:55:14 -0700203 break;
Herbert Xu2ba25062008-03-28 16:25:26 -0700204
205 /*
206 * Postpone processing if
207 * 1. another process needs the CPU;
208 * 2. we've been doing it for too long.
209 */
210 if (need_resched() || jiffies != start_time) {
David S. Millereb6aafe2008-07-08 23:12:38 -0700211 netif_schedule_queue(txq);
Herbert Xu2ba25062008-03-28 16:25:26 -0700212 break;
213 }
214 }
Herbert Xu48d83322006-06-19 23:57:59 -0700215
David S. Miller79d16382008-07-08 23:14:46 -0700216 clear_bit(__QUEUE_STATE_QDISC_RUNNING, &txq->state);
Herbert Xu48d83322006-06-19 23:57:59 -0700217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static void dev_watchdog(unsigned long arg)
220{
221 struct net_device *dev = (struct net_device *)arg;
222
Herbert Xu932ff272006-06-09 12:20:56 -0700223 netif_tx_lock(dev);
David S. Millere8a04642008-07-17 00:34:19 -0700224 if (!qdisc_tx_is_noop(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (netif_device_present(dev) &&
226 netif_running(dev) &&
227 netif_carrier_ok(dev)) {
David S. Millere8a04642008-07-17 00:34:19 -0700228 int some_queue_stopped = 0;
229 unsigned int i;
Stephen Hemminger338f7562006-05-16 15:02:12 -0700230
David S. Millere8a04642008-07-17 00:34:19 -0700231 for (i = 0; i < dev->num_tx_queues; i++) {
232 struct netdev_queue *txq;
233
234 txq = netdev_get_tx_queue(dev, i);
235 if (netif_tx_queue_stopped(txq)) {
236 some_queue_stopped = 1;
237 break;
238 }
239 }
240
241 if (some_queue_stopped &&
242 time_after(jiffies, (dev->trans_start +
243 dev->watchdog_timeo))) {
244 printk(KERN_INFO "NETDEV WATCHDOG: %s: "
245 "transmit timed out\n",
Stephen Hemminger338f7562006-05-16 15:02:12 -0700246 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 dev->tx_timeout(dev);
Arjan van de Venb4192bb2008-05-02 16:21:07 -0700248 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
David S. Millere8a04642008-07-17 00:34:19 -0700250 if (!mod_timer(&dev->watchdog_timer,
251 round_jiffies(jiffies +
252 dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 dev_hold(dev);
254 }
255 }
Herbert Xu932ff272006-06-09 12:20:56 -0700256 netif_tx_unlock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 dev_put(dev);
259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261void __netdev_watchdog_up(struct net_device *dev)
262{
263 if (dev->tx_timeout) {
264 if (dev->watchdog_timeo <= 0)
265 dev->watchdog_timeo = 5*HZ;
Venkatesh Pallipadi60468d52007-05-31 21:28:44 -0700266 if (!mod_timer(&dev->watchdog_timer,
267 round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 dev_hold(dev);
269 }
270}
271
272static void dev_watchdog_up(struct net_device *dev)
273{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 __netdev_watchdog_up(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277static void dev_watchdog_down(struct net_device *dev)
278{
Herbert Xu932ff272006-06-09 12:20:56 -0700279 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (del_timer(&dev->watchdog_timer))
Stephen Hemminger15333062006-03-20 22:32:28 -0800281 dev_put(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700282 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700285/**
286 * netif_carrier_on - set carrier
287 * @dev: network device
288 *
289 * Device has detected that carrier.
290 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700291void netif_carrier_on(struct net_device *dev)
292{
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700293 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700294 linkwatch_fire_event(dev);
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700295 if (netif_running(dev))
296 __netdev_watchdog_up(dev);
297 }
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700298}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800299EXPORT_SYMBOL(netif_carrier_on);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700300
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700301/**
302 * netif_carrier_off - clear carrier
303 * @dev: network device
304 *
305 * Device has detected loss of carrier.
306 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700307void netif_carrier_off(struct net_device *dev)
308{
309 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
310 linkwatch_fire_event(dev);
311}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800312EXPORT_SYMBOL(netif_carrier_off);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/* "NOOP" scheduler: the best scheduler, recommended for all interfaces
315 under all circumstances. It is difficult to invent anything faster or
316 cheaper.
317 */
318
Thomas Graf94df1092005-06-18 22:59:08 -0700319static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 kfree_skb(skb);
322 return NET_XMIT_CN;
323}
324
Thomas Graf94df1092005-06-18 22:59:08 -0700325static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 return NULL;
328}
329
Thomas Graf94df1092005-06-18 22:59:08 -0700330static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 if (net_ratelimit())
Thomas Graf94df1092005-06-18 22:59:08 -0700333 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
334 skb->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 kfree_skb(skb);
336 return NET_XMIT_CN;
337}
338
Eric Dumazet20fea082007-11-14 01:44:41 -0800339struct Qdisc_ops noop_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 .id = "noop",
341 .priv_size = 0,
342 .enqueue = noop_enqueue,
343 .dequeue = noop_dequeue,
344 .requeue = noop_requeue,
345 .owner = THIS_MODULE,
346};
347
348struct Qdisc noop_qdisc = {
349 .enqueue = noop_enqueue,
350 .dequeue = noop_dequeue,
351 .flags = TCQ_F_BUILTIN,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900352 .ops = &noop_qdisc_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 .list = LIST_HEAD_INIT(noop_qdisc.list),
354};
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800355EXPORT_SYMBOL(noop_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Eric Dumazet20fea082007-11-14 01:44:41 -0800357static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 .id = "noqueue",
359 .priv_size = 0,
360 .enqueue = noop_enqueue,
361 .dequeue = noop_dequeue,
362 .requeue = noop_requeue,
363 .owner = THIS_MODULE,
364};
365
366static struct Qdisc noqueue_qdisc = {
367 .enqueue = NULL,
368 .dequeue = noop_dequeue,
369 .flags = TCQ_F_BUILTIN,
370 .ops = &noqueue_qdisc_ops,
371 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
372};
373
374
375static const u8 prio2band[TC_PRIO_MAX+1] =
376 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
377
378/* 3-band FIFO queue: old style, but should be a bit faster than
379 generic prio+fifo combination.
380 */
381
Thomas Graff87a9c32005-06-18 22:58:53 -0700382#define PFIFO_FAST_BANDS 3
383
Thomas Graf321090e2005-06-18 22:58:35 -0700384static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
385 struct Qdisc *qdisc)
386{
387 struct sk_buff_head *list = qdisc_priv(qdisc);
388 return list + prio2band[skb->priority & TC_PRIO_MAX];
389}
390
Thomas Graff87a9c32005-06-18 22:58:53 -0700391static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Thomas Graf321090e2005-06-18 22:58:35 -0700393 struct sk_buff_head *list = prio2list(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
David S. Miller5ce2d482008-07-08 17:06:30 -0700395 if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 qdisc->q.qlen++;
Thomas Graf821d24ae2005-06-18 22:58:15 -0700397 return __qdisc_enqueue_tail(skb, qdisc, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Thomas Graf821d24ae2005-06-18 22:58:15 -0700399
400 return qdisc_drop(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Thomas Graff87a9c32005-06-18 22:58:53 -0700403static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 int prio;
406 struct sk_buff_head *list = qdisc_priv(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Thomas Graf452f2992005-07-18 13:30:53 -0700408 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
409 if (!skb_queue_empty(list + prio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 qdisc->q.qlen--;
Thomas Graf452f2992005-07-18 13:30:53 -0700411 return __qdisc_dequeue_head(qdisc, list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 }
Thomas Graff87a9c32005-06-18 22:58:53 -0700414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return NULL;
416}
417
Thomas Graff87a9c32005-06-18 22:58:53 -0700418static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 qdisc->q.qlen++;
Thomas Graf321090e2005-06-18 22:58:35 -0700421 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Thomas Graff87a9c32005-06-18 22:58:53 -0700424static void pfifo_fast_reset(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 int prio;
427 struct sk_buff_head *list = qdisc_priv(qdisc);
428
Thomas Graff87a9c32005-06-18 22:58:53 -0700429 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
Thomas Graf821d24ae2005-06-18 22:58:15 -0700430 __qdisc_reset_queue(qdisc, list + prio);
431
432 qdisc->qstats.backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 qdisc->q.qlen = 0;
434}
435
436static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
437{
Thomas Graff87a9c32005-06-18 22:58:53 -0700438 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
Patrick McHardy1e904742008-01-22 22:11:17 -0800441 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return skb->len;
443
Patrick McHardy1e904742008-01-22 22:11:17 -0800444nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return -1;
446}
447
Patrick McHardy1e904742008-01-22 22:11:17 -0800448static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Thomas Graff87a9c32005-06-18 22:58:53 -0700450 int prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct sk_buff_head *list = qdisc_priv(qdisc);
452
Thomas Graff87a9c32005-06-18 22:58:53 -0700453 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
454 skb_queue_head_init(list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 return 0;
457}
458
Eric Dumazet20fea082007-11-14 01:44:41 -0800459static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 .id = "pfifo_fast",
Thomas Graff87a9c32005-06-18 22:58:53 -0700461 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 .enqueue = pfifo_fast_enqueue,
463 .dequeue = pfifo_fast_dequeue,
464 .requeue = pfifo_fast_requeue,
465 .init = pfifo_fast_init,
466 .reset = pfifo_fast_reset,
467 .dump = pfifo_fast_dump,
468 .owner = THIS_MODULE,
469};
470
David S. Miller5ce2d482008-07-08 17:06:30 -0700471struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -0700472 struct Qdisc_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 void *p;
475 struct Qdisc *sch;
Thomas Graf3d54b822005-07-05 14:15:09 -0700476 unsigned int size;
477 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /* ensure that the Qdisc and the private data are 32-byte aligned */
Thomas Graf3d54b822005-07-05 14:15:09 -0700480 size = QDISC_ALIGN(sizeof(*sch));
481 size += ops->priv_size + (QDISC_ALIGNTO - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700483 p = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (!p)
Thomas Graf3d54b822005-07-05 14:15:09 -0700485 goto errout;
Thomas Graf3d54b822005-07-05 14:15:09 -0700486 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
487 sch->padded = (char *) sch - (char *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 INIT_LIST_HEAD(&sch->list);
490 skb_queue_head_init(&sch->q);
491 sch->ops = ops;
492 sch->enqueue = ops->enqueue;
493 sch->dequeue = ops->dequeue;
David S. Millerbb949fb2008-07-08 16:55:56 -0700494 sch->dev_queue = dev_queue;
David S. Miller5ce2d482008-07-08 17:06:30 -0700495 dev_hold(qdisc_dev(sch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 atomic_set(&sch->refcnt, 1);
Thomas Graf3d54b822005-07-05 14:15:09 -0700497
498 return sch;
499errout:
WANG Cong01e123d2008-06-27 19:51:35 -0700500 return ERR_PTR(err);
Thomas Graf3d54b822005-07-05 14:15:09 -0700501}
502
David S. Millerbb949fb2008-07-08 16:55:56 -0700503struct Qdisc * qdisc_create_dflt(struct net_device *dev,
504 struct netdev_queue *dev_queue,
505 struct Qdisc_ops *ops,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800506 unsigned int parentid)
Thomas Graf3d54b822005-07-05 14:15:09 -0700507{
508 struct Qdisc *sch;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900509
David S. Miller5ce2d482008-07-08 17:06:30 -0700510 sch = qdisc_alloc(dev_queue, ops);
Thomas Graf3d54b822005-07-05 14:15:09 -0700511 if (IS_ERR(sch))
512 goto errout;
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800513 sch->parent = parentid;
Thomas Graf3d54b822005-07-05 14:15:09 -0700514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (!ops->init || ops->init(sch, NULL) == 0)
516 return sch;
517
Thomas Graf0fbbeb12005-08-23 10:12:44 -0700518 qdisc_destroy(sch);
Thomas Graf3d54b822005-07-05 14:15:09 -0700519errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return NULL;
521}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800522EXPORT_SYMBOL(qdisc_create_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
David S. Millerdc2b4842008-07-08 17:18:23 -0700524/* Under queue->lock and BH! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526void qdisc_reset(struct Qdisc *qdisc)
527{
Eric Dumazet20fea082007-11-14 01:44:41 -0800528 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (ops->reset)
531 ops->reset(qdisc);
532}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800533EXPORT_SYMBOL(qdisc_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900535/* this is the rcu callback function to clean up a qdisc when there
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * are no further references to it */
537
538static void __qdisc_destroy(struct rcu_head *head)
539{
540 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 kfree((char *) qdisc - qdisc->padded);
542}
543
David S. Millerdc2b4842008-07-08 17:18:23 -0700544/* Under queue->lock and BH! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546void qdisc_destroy(struct Qdisc *qdisc)
547{
Eric Dumazet20fea082007-11-14 01:44:41 -0800548 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 if (qdisc->flags & TCQ_F_BUILTIN ||
Patrick McHardy85670cc2006-09-27 16:45:45 -0700551 !atomic_dec_and_test(&qdisc->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return;
553
Patrick McHardy85670cc2006-09-27 16:45:45 -0700554 list_del(&qdisc->list);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700555 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700556 if (ops->reset)
557 ops->reset(qdisc);
558 if (ops->destroy)
559 ops->destroy(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Patrick McHardy85670cc2006-09-27 16:45:45 -0700561 module_put(ops->owner);
David S. Miller5ce2d482008-07-08 17:06:30 -0700562 dev_put(qdisc_dev(qdisc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
564}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800565EXPORT_SYMBOL(qdisc_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
David S. Millere8a04642008-07-17 00:34:19 -0700567static bool dev_all_qdisc_sleeping_noop(struct net_device *dev)
568{
569 unsigned int i;
570
571 for (i = 0; i < dev->num_tx_queues; i++) {
572 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
573
574 if (txq->qdisc_sleeping != &noop_qdisc)
575 return false;
576 }
577 return true;
578}
579
580static void attach_one_default_qdisc(struct net_device *dev,
581 struct netdev_queue *dev_queue,
582 void *_unused)
583{
584 struct Qdisc *qdisc;
585
586 if (dev->tx_queue_len) {
587 qdisc = qdisc_create_dflt(dev, dev_queue,
588 &pfifo_fast_ops, TC_H_ROOT);
589 if (!qdisc) {
590 printk(KERN_INFO "%s: activation failed\n", dev->name);
591 return;
592 }
593 list_add_tail(&qdisc->list, &dev_queue->qdisc_list);
594 } else {
595 qdisc = &noqueue_qdisc;
596 }
597 dev_queue->qdisc_sleeping = qdisc;
598}
599
600static void transition_one_qdisc(struct net_device *dev,
601 struct netdev_queue *dev_queue,
602 void *_need_watchdog)
603{
604 int *need_watchdog_p = _need_watchdog;
605
606 spin_lock_bh(&dev_queue->lock);
607 rcu_assign_pointer(dev_queue->qdisc, dev_queue->qdisc_sleeping);
608 if (dev_queue->qdisc != &noqueue_qdisc)
609 *need_watchdog_p = 1;
610 spin_unlock_bh(&dev_queue->lock);
611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613void dev_activate(struct net_device *dev)
614{
David S. Millere8a04642008-07-17 00:34:19 -0700615 int need_watchdog;
David S. Millerb0e1e642008-07-08 17:42:10 -0700616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* No queueing discipline is attached to device;
618 create default one i.e. pfifo_fast for devices,
619 which need queueing and noqueue_qdisc for
620 virtual interfaces
621 */
622
David S. Millere8a04642008-07-17 00:34:19 -0700623 if (dev_all_qdisc_sleeping_noop(dev))
624 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Tommy S. Christensencacaddf2005-05-03 16:18:52 -0700626 if (!netif_carrier_ok(dev))
627 /* Delay activation until next carrier-on event */
628 return;
629
David S. Millere8a04642008-07-17 00:34:19 -0700630 need_watchdog = 0;
631 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
632
633 if (need_watchdog) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 dev->trans_start = jiffies;
635 dev_watchdog_up(dev);
636 }
David S. Millerb0e1e642008-07-08 17:42:10 -0700637}
638
David S. Millere8a04642008-07-17 00:34:19 -0700639static void dev_deactivate_queue(struct net_device *dev,
640 struct netdev_queue *dev_queue,
641 void *_qdisc_default)
David S. Millerb0e1e642008-07-08 17:42:10 -0700642{
David S. Millere8a04642008-07-17 00:34:19 -0700643 struct Qdisc *qdisc_default = _qdisc_default;
David S. Millerd3b753d2008-07-15 20:14:35 -0700644 struct sk_buff *skb = NULL;
David S. Miller970565b2008-07-08 23:10:33 -0700645 struct Qdisc *qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -0700646
David S. Miller970565b2008-07-08 23:10:33 -0700647 spin_lock_bh(&dev_queue->lock);
648
649 qdisc = dev_queue->qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -0700650 if (qdisc) {
651 dev_queue->qdisc = qdisc_default;
652 qdisc_reset(qdisc);
David S. Millerd3b753d2008-07-15 20:14:35 -0700653
654 skb = qdisc->gso_skb;
655 qdisc->gso_skb = NULL;
David S. Millerb0e1e642008-07-08 17:42:10 -0700656 }
David S. Miller970565b2008-07-08 23:10:33 -0700657
658 spin_unlock_bh(&dev_queue->lock);
659
660 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
David S. Millere8a04642008-07-17 00:34:19 -0700663static bool some_qdisc_is_running(struct net_device *dev, int lock)
664{
665 unsigned int i;
666
667 for (i = 0; i < dev->num_tx_queues; i++) {
668 struct netdev_queue *dev_queue;
669 int val;
670
671 dev_queue = netdev_get_tx_queue(dev, i);
672
673 if (lock)
674 spin_lock_bh(&dev_queue->lock);
675
676 val = test_bit(__QUEUE_STATE_QDISC_RUNNING, &dev_queue->state);
677
678 if (lock)
679 spin_unlock_bh(&dev_queue->lock);
680
681 if (val)
682 return true;
683 }
684 return false;
685}
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687void dev_deactivate(struct net_device *dev)
688{
David S. Millere8a04642008-07-17 00:34:19 -0700689 bool running;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
David S. Millere8a04642008-07-17 00:34:19 -0700691 netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
Herbert Xu41a23b02007-05-10 14:12:47 -0700692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 dev_watchdog_down(dev);
694
Herbert Xuce0e32e2007-10-18 22:37:58 -0700695 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
Herbert Xud4828d82006-06-22 02:28:18 -0700696 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Herbert Xud4828d82006-06-22 02:28:18 -0700698 /* Wait for outstanding qdisc_run calls. */
Herbert Xuce0e32e2007-10-18 22:37:58 -0700699 do {
David S. Millere8a04642008-07-17 00:34:19 -0700700 while (some_qdisc_is_running(dev, 0))
Herbert Xuce0e32e2007-10-18 22:37:58 -0700701 yield();
702
703 /*
704 * Double-check inside queue lock to ensure that all effects
705 * of the queue run are visible when we return.
706 */
David S. Millere8a04642008-07-17 00:34:19 -0700707 running = some_qdisc_is_running(dev, 1);
Herbert Xuce0e32e2007-10-18 22:37:58 -0700708
709 /*
710 * The running flag should never be set at this point because
711 * we've already set dev->qdisc to noop_qdisc *inside* the same
712 * pair of spin locks. That is, if any qdisc_run starts after
713 * our initial test it should see the noop_qdisc and then
714 * clear the RUNNING bit before dropping the queue lock. So
715 * if it is set here then we've found a bug.
716 */
717 } while (WARN_ON_ONCE(running));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
David S. Millerb0e1e642008-07-08 17:42:10 -0700720static void dev_init_scheduler_queue(struct net_device *dev,
721 struct netdev_queue *dev_queue,
David S. Millere8a04642008-07-17 00:34:19 -0700722 void *_qdisc)
David S. Millerb0e1e642008-07-08 17:42:10 -0700723{
David S. Millere8a04642008-07-17 00:34:19 -0700724 struct Qdisc *qdisc = _qdisc;
725
David S. Millerb0e1e642008-07-08 17:42:10 -0700726 dev_queue->qdisc = qdisc;
727 dev_queue->qdisc_sleeping = qdisc;
728 INIT_LIST_HEAD(&dev_queue->qdisc_list);
729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731void dev_init_scheduler(struct net_device *dev)
732{
733 qdisc_lock_tree(dev);
David S. Millere8a04642008-07-17 00:34:19 -0700734 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
David S. Millerb0e1e642008-07-08 17:42:10 -0700735 dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 qdisc_unlock_tree(dev);
737
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800738 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
David S. Millere8a04642008-07-17 00:34:19 -0700741static void shutdown_scheduler_queue(struct net_device *dev,
742 struct netdev_queue *dev_queue,
743 void *_qdisc_default)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
David S. Millerb0e1e642008-07-08 17:42:10 -0700745 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
David S. Millere8a04642008-07-17 00:34:19 -0700746 struct Qdisc *qdisc_default = _qdisc_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
David S. Millerb0e1e642008-07-08 17:42:10 -0700748 if (qdisc) {
749 dev_queue->qdisc = qdisc_default;
750 dev_queue->qdisc_sleeping = qdisc_default;
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 qdisc_destroy(qdisc);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900753 }
David S. Millerb0e1e642008-07-08 17:42:10 -0700754}
755
756void dev_shutdown(struct net_device *dev)
757{
758 qdisc_lock_tree(dev);
David S. Millere8a04642008-07-17 00:34:19 -0700759 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
760 shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
762 qdisc_unlock_tree(dev);
763}