blob: dd5c4e70abe4950050385248c48617918fd4533c [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. Miller37437bb2008-07-16 02:15:04 -070075static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070076{
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070077 if (unlikely(skb->next))
David S. Millerd3b753d2008-07-15 20:14:35 -070078 q->gso_skb = skb;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070079 else
80 q->ops->requeue(skb, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -070081
David S. Miller37437bb2008-07-16 02:15:04 -070082 __netif_schedule(q);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070083 return 0;
84}
85
David S. Millerd3b753d2008-07-15 20:14:35 -070086static inline struct sk_buff *dequeue_skb(struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070087{
Krishna Kumar6c1361a2007-06-24 19:56:09 -070088 struct sk_buff *skb;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070089
David S. Millerd3b753d2008-07-15 20:14:35 -070090 if ((skb = q->gso_skb))
91 q->gso_skb = NULL;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -070092 else
93 skb = q->dequeue(q);
94
95 return skb;
96}
97
Krishna Kumar6c1361a2007-06-24 19:56:09 -070098static inline int handle_dev_cpu_collision(struct sk_buff *skb,
David S. Miller970565b2008-07-08 23:10:33 -070099 struct netdev_queue *dev_queue,
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700100 struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700101{
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700102 int ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700103
David S. Millerc773e842008-07-08 23:13:53 -0700104 if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700105 /*
106 * Same CPU holding the lock. It may be a transient
107 * configuration error, when hard_start_xmit() recurses. We
108 * detect it by checking xmit owner and drop the packet when
109 * deadloop is detected. Return OK to try the next skb.
110 */
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700111 kfree_skb(skb);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700112 if (net_ratelimit())
113 printk(KERN_WARNING "Dead loop on netdevice %s, "
David S. Millerc773e842008-07-08 23:13:53 -0700114 "fix it urgently!\n", dev_queue->dev->name);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700115 ret = qdisc_qlen(q);
116 } else {
117 /*
118 * Another cpu is holding lock, requeue & delay xmits for
119 * some time.
120 */
121 __get_cpu_var(netdev_rx_stat).cpu_collision++;
David S. Miller37437bb2008-07-16 02:15:04 -0700122 ret = dev_requeue_skb(skb, q);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700123 }
124
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700125 return ret;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700126}
127
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900128/*
David S. Millerdc2b4842008-07-08 17:18:23 -0700129 * NOTE: Called under queue->lock with locally disabled BH.
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700130 *
David S. Millere2627c82008-07-16 00:56:32 -0700131 * __QDISC_STATE_RUNNING guarantees only one CPU can process
132 * this qdisc at a time. queue->lock serializes queue accesses for
David S. Miller79d16382008-07-08 23:14:46 -0700133 * this queue AND txq->qdisc pointer itself.
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700134 *
135 * netif_tx_lock serializes accesses to device driver.
136 *
David S. Millerdc2b4842008-07-08 17:18:23 -0700137 * queue->lock and netif_tx_lock are mutually exclusive,
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700138 * if one is grabbed, another must be free.
139 *
140 * Note, that this procedure can be called by a watchdog timer
141 *
142 * Returns to the caller:
143 * 0 - queue is empty or throttled.
144 * >0 - queue is not empty.
145 *
146 */
David S. Miller37437bb2008-07-16 02:15:04 -0700147static inline int qdisc_restart(struct Qdisc *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
David S. Miller37437bb2008-07-16 02:15:04 -0700149 struct netdev_queue *txq;
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800150 int ret = NETDEV_TX_BUSY;
David S. Millereb6aafe2008-07-08 23:12:38 -0700151 struct net_device *dev;
David S. Miller7698b4f2008-07-16 01:42:40 -0700152 spinlock_t *root_lock;
David S. Millereb6aafe2008-07-08 23:12:38 -0700153 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700155 /* Dequeue packet */
David S. Millerd3b753d2008-07-15 20:14:35 -0700156 if (unlikely((skb = dequeue_skb(q)) == NULL))
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700157 return 0;
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700158
David S. Miller7698b4f2008-07-16 01:42:40 -0700159 root_lock = qdisc_root_lock(q);
160
161 /* And release qdisc */
162 spin_unlock(root_lock);
Herbert Xud90df3a2007-05-10 04:55:14 -0700163
David S. Miller37437bb2008-07-16 02:15:04 -0700164 dev = qdisc_dev(q);
165 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
David S. Millereb6aafe2008-07-08 23:12:38 -0700166
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. Miller7698b4f2008-07-16 01:42:40 -0700172 spin_lock(root_lock);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700173
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700174 switch (ret) {
175 case NETDEV_TX_OK:
176 /* Driver sent out skb successfully */
177 ret = qdisc_qlen(q);
178 break;
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700179
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700180 case NETDEV_TX_LOCKED:
181 /* Driver try lock failed */
David S. Millereb6aafe2008-07-08 23:12:38 -0700182 ret = handle_dev_cpu_collision(skb, txq, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700183 break;
184
185 default:
186 /* Driver returned NETDEV_TX_BUSY - requeue skb */
187 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
188 printk(KERN_WARNING "BUG %s code %d qlen %d\n",
189 dev->name, ret, q->q.qlen);
190
David S. Miller37437bb2008-07-16 02:15:04 -0700191 ret = dev_requeue_skb(skb, q);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700192 break;
193 }
194
David S. Miller37437bb2008-07-16 02:15:04 -0700195 if (ret && netif_tx_queue_stopped(txq))
196 ret = 0;
197
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700198 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
David S. Miller37437bb2008-07-16 02:15:04 -0700201void __qdisc_run(struct Qdisc *q)
Herbert Xu48d83322006-06-19 23:57:59 -0700202{
Herbert Xu2ba25062008-03-28 16:25:26 -0700203 unsigned long start_time = jiffies;
204
David S. Miller37437bb2008-07-16 02:15:04 -0700205 while (qdisc_restart(q)) {
Herbert Xu2ba25062008-03-28 16:25:26 -0700206 /*
207 * Postpone processing if
208 * 1. another process needs the CPU;
209 * 2. we've been doing it for too long.
210 */
211 if (need_resched() || jiffies != start_time) {
David S. Miller37437bb2008-07-16 02:15:04 -0700212 __netif_schedule(q);
Herbert Xu2ba25062008-03-28 16:25:26 -0700213 break;
214 }
215 }
Herbert Xu48d83322006-06-19 23:57:59 -0700216
David S. Millere2627c82008-07-16 00:56:32 -0700217 clear_bit(__QDISC_STATE_RUNNING, &q->state);
Herbert Xu48d83322006-06-19 23:57:59 -0700218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static void dev_watchdog(unsigned long arg)
221{
222 struct net_device *dev = (struct net_device *)arg;
223
Herbert Xu932ff272006-06-09 12:20:56 -0700224 netif_tx_lock(dev);
David S. Millere8a04642008-07-17 00:34:19 -0700225 if (!qdisc_tx_is_noop(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (netif_device_present(dev) &&
227 netif_running(dev) &&
228 netif_carrier_ok(dev)) {
David S. Millere8a04642008-07-17 00:34:19 -0700229 int some_queue_stopped = 0;
230 unsigned int i;
Stephen Hemminger338f7562006-05-16 15:02:12 -0700231
David S. Millere8a04642008-07-17 00:34:19 -0700232 for (i = 0; i < dev->num_tx_queues; i++) {
233 struct netdev_queue *txq;
234
235 txq = netdev_get_tx_queue(dev, i);
236 if (netif_tx_queue_stopped(txq)) {
237 some_queue_stopped = 1;
238 break;
239 }
240 }
241
242 if (some_queue_stopped &&
243 time_after(jiffies, (dev->trans_start +
244 dev->watchdog_timeo))) {
245 printk(KERN_INFO "NETDEV WATCHDOG: %s: "
246 "transmit timed out\n",
Stephen Hemminger338f7562006-05-16 15:02:12 -0700247 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dev->tx_timeout(dev);
Arjan van de Venb4192bb2008-05-02 16:21:07 -0700249 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
David S. Millere8a04642008-07-17 00:34:19 -0700251 if (!mod_timer(&dev->watchdog_timer,
252 round_jiffies(jiffies +
253 dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 dev_hold(dev);
255 }
256 }
Herbert Xu932ff272006-06-09 12:20:56 -0700257 netif_tx_unlock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 dev_put(dev);
260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262void __netdev_watchdog_up(struct net_device *dev)
263{
264 if (dev->tx_timeout) {
265 if (dev->watchdog_timeo <= 0)
266 dev->watchdog_timeo = 5*HZ;
Venkatesh Pallipadi60468d52007-05-31 21:28:44 -0700267 if (!mod_timer(&dev->watchdog_timer,
268 round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 dev_hold(dev);
270 }
271}
272
273static void dev_watchdog_up(struct net_device *dev)
274{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 __netdev_watchdog_up(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278static void dev_watchdog_down(struct net_device *dev)
279{
Herbert Xu932ff272006-06-09 12:20:56 -0700280 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (del_timer(&dev->watchdog_timer))
Stephen Hemminger15333062006-03-20 22:32:28 -0800282 dev_put(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700283 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700286/**
287 * netif_carrier_on - set carrier
288 * @dev: network device
289 *
290 * Device has detected that carrier.
291 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700292void netif_carrier_on(struct net_device *dev)
293{
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700294 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700295 linkwatch_fire_event(dev);
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700296 if (netif_running(dev))
297 __netdev_watchdog_up(dev);
298 }
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700299}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800300EXPORT_SYMBOL(netif_carrier_on);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700301
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700302/**
303 * netif_carrier_off - clear carrier
304 * @dev: network device
305 *
306 * Device has detected loss of carrier.
307 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700308void netif_carrier_off(struct net_device *dev)
309{
310 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
311 linkwatch_fire_event(dev);
312}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800313EXPORT_SYMBOL(netif_carrier_off);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/* "NOOP" scheduler: the best scheduler, recommended for all interfaces
316 under all circumstances. It is difficult to invent anything faster or
317 cheaper.
318 */
319
Thomas Graf94df1092005-06-18 22:59:08 -0700320static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 kfree_skb(skb);
323 return NET_XMIT_CN;
324}
325
Thomas Graf94df1092005-06-18 22:59:08 -0700326static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 return NULL;
329}
330
Thomas Graf94df1092005-06-18 22:59:08 -0700331static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 if (net_ratelimit())
Thomas Graf94df1092005-06-18 22:59:08 -0700334 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
335 skb->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 kfree_skb(skb);
337 return NET_XMIT_CN;
338}
339
Eric Dumazet20fea082007-11-14 01:44:41 -0800340struct Qdisc_ops noop_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 .id = "noop",
342 .priv_size = 0,
343 .enqueue = noop_enqueue,
344 .dequeue = noop_dequeue,
345 .requeue = noop_requeue,
346 .owner = THIS_MODULE,
347};
348
David S. Miller7698b4f2008-07-16 01:42:40 -0700349static struct netdev_queue noop_netdev_queue = {
350 .lock = __SPIN_LOCK_UNLOCKED(noop_netdev_queue.lock),
351 .qdisc = &noop_qdisc,
352};
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354struct Qdisc noop_qdisc = {
355 .enqueue = noop_enqueue,
356 .dequeue = noop_dequeue,
357 .flags = TCQ_F_BUILTIN,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900358 .ops = &noop_qdisc_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 .list = LIST_HEAD_INIT(noop_qdisc.list),
David S. Miller7698b4f2008-07-16 01:42:40 -0700360 .dev_queue = &noop_netdev_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361};
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800362EXPORT_SYMBOL(noop_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Eric Dumazet20fea082007-11-14 01:44:41 -0800364static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 .id = "noqueue",
366 .priv_size = 0,
367 .enqueue = noop_enqueue,
368 .dequeue = noop_dequeue,
369 .requeue = noop_requeue,
370 .owner = THIS_MODULE,
371};
372
373static struct Qdisc noqueue_qdisc = {
374 .enqueue = NULL,
375 .dequeue = noop_dequeue,
376 .flags = TCQ_F_BUILTIN,
377 .ops = &noqueue_qdisc_ops,
378 .list = LIST_HEAD_INIT(noqueue_qdisc.list),
379};
380
381
382static const u8 prio2band[TC_PRIO_MAX+1] =
383 { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
384
385/* 3-band FIFO queue: old style, but should be a bit faster than
386 generic prio+fifo combination.
387 */
388
Thomas Graff87a9c32005-06-18 22:58:53 -0700389#define PFIFO_FAST_BANDS 3
390
Thomas Graf321090e2005-06-18 22:58:35 -0700391static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
392 struct Qdisc *qdisc)
393{
394 struct sk_buff_head *list = qdisc_priv(qdisc);
395 return list + prio2band[skb->priority & TC_PRIO_MAX];
396}
397
Thomas Graff87a9c32005-06-18 22:58:53 -0700398static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Thomas Graf321090e2005-06-18 22:58:35 -0700400 struct sk_buff_head *list = prio2list(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
David S. Miller5ce2d482008-07-08 17:06:30 -0700402 if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 qdisc->q.qlen++;
Thomas Graf821d24a2005-06-18 22:58:15 -0700404 return __qdisc_enqueue_tail(skb, qdisc, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Thomas Graf821d24a2005-06-18 22:58:15 -0700406
407 return qdisc_drop(skb, qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Thomas Graff87a9c32005-06-18 22:58:53 -0700410static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 int prio;
413 struct sk_buff_head *list = qdisc_priv(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Thomas Graf452f2992005-07-18 13:30:53 -0700415 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
416 if (!skb_queue_empty(list + prio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 qdisc->q.qlen--;
Thomas Graf452f2992005-07-18 13:30:53 -0700418 return __qdisc_dequeue_head(qdisc, list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 }
Thomas Graff87a9c32005-06-18 22:58:53 -0700421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return NULL;
423}
424
Thomas Graff87a9c32005-06-18 22:58:53 -0700425static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 qdisc->q.qlen++;
Thomas Graf321090e2005-06-18 22:58:35 -0700428 return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Thomas Graff87a9c32005-06-18 22:58:53 -0700431static void pfifo_fast_reset(struct Qdisc* qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 int prio;
434 struct sk_buff_head *list = qdisc_priv(qdisc);
435
Thomas Graff87a9c32005-06-18 22:58:53 -0700436 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
Thomas Graf821d24a2005-06-18 22:58:15 -0700437 __qdisc_reset_queue(qdisc, list + prio);
438
439 qdisc->qstats.backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 qdisc->q.qlen = 0;
441}
442
443static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
444{
Thomas Graff87a9c32005-06-18 22:58:53 -0700445 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
Patrick McHardy1e904742008-01-22 22:11:17 -0800448 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return skb->len;
450
Patrick McHardy1e904742008-01-22 22:11:17 -0800451nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return -1;
453}
454
Patrick McHardy1e904742008-01-22 22:11:17 -0800455static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Thomas Graff87a9c32005-06-18 22:58:53 -0700457 int prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct sk_buff_head *list = qdisc_priv(qdisc);
459
Thomas Graff87a9c32005-06-18 22:58:53 -0700460 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
461 skb_queue_head_init(list + prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 return 0;
464}
465
Eric Dumazet20fea082007-11-14 01:44:41 -0800466static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 .id = "pfifo_fast",
Thomas Graff87a9c32005-06-18 22:58:53 -0700468 .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 .enqueue = pfifo_fast_enqueue,
470 .dequeue = pfifo_fast_dequeue,
471 .requeue = pfifo_fast_requeue,
472 .init = pfifo_fast_init,
473 .reset = pfifo_fast_reset,
474 .dump = pfifo_fast_dump,
475 .owner = THIS_MODULE,
476};
477
David S. Miller5ce2d482008-07-08 17:06:30 -0700478struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -0700479 struct Qdisc_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 void *p;
482 struct Qdisc *sch;
Thomas Graf3d54b822005-07-05 14:15:09 -0700483 unsigned int size;
484 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 /* ensure that the Qdisc and the private data are 32-byte aligned */
Thomas Graf3d54b822005-07-05 14:15:09 -0700487 size = QDISC_ALIGN(sizeof(*sch));
488 size += ops->priv_size + (QDISC_ALIGNTO - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700490 p = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (!p)
Thomas Graf3d54b822005-07-05 14:15:09 -0700492 goto errout;
Thomas Graf3d54b822005-07-05 14:15:09 -0700493 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
494 sch->padded = (char *) sch - (char *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 INIT_LIST_HEAD(&sch->list);
497 skb_queue_head_init(&sch->q);
498 sch->ops = ops;
499 sch->enqueue = ops->enqueue;
500 sch->dequeue = ops->dequeue;
David S. Millerbb949fb2008-07-08 16:55:56 -0700501 sch->dev_queue = dev_queue;
David S. Miller5ce2d482008-07-08 17:06:30 -0700502 dev_hold(qdisc_dev(sch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 atomic_set(&sch->refcnt, 1);
Thomas Graf3d54b822005-07-05 14:15:09 -0700504
505 return sch;
506errout:
WANG Cong01e123d2008-06-27 19:51:35 -0700507 return ERR_PTR(err);
Thomas Graf3d54b822005-07-05 14:15:09 -0700508}
509
David S. Millerbb949fb2008-07-08 16:55:56 -0700510struct Qdisc * qdisc_create_dflt(struct net_device *dev,
511 struct netdev_queue *dev_queue,
512 struct Qdisc_ops *ops,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800513 unsigned int parentid)
Thomas Graf3d54b822005-07-05 14:15:09 -0700514{
515 struct Qdisc *sch;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900516
David S. Miller5ce2d482008-07-08 17:06:30 -0700517 sch = qdisc_alloc(dev_queue, ops);
Thomas Graf3d54b822005-07-05 14:15:09 -0700518 if (IS_ERR(sch))
519 goto errout;
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800520 sch->parent = parentid;
Thomas Graf3d54b822005-07-05 14:15:09 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!ops->init || ops->init(sch, NULL) == 0)
523 return sch;
524
Thomas Graf0fbbeb12005-08-23 10:12:44 -0700525 qdisc_destroy(sch);
Thomas Graf3d54b822005-07-05 14:15:09 -0700526errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return NULL;
528}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800529EXPORT_SYMBOL(qdisc_create_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
David S. Millerdc2b4842008-07-08 17:18:23 -0700531/* Under queue->lock and BH! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533void qdisc_reset(struct Qdisc *qdisc)
534{
Eric Dumazet20fea082007-11-14 01:44:41 -0800535 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (ops->reset)
538 ops->reset(qdisc);
539}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800540EXPORT_SYMBOL(qdisc_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900542/* this is the rcu callback function to clean up a qdisc when there
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 * are no further references to it */
544
545static void __qdisc_destroy(struct rcu_head *head)
546{
547 struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 kfree((char *) qdisc - qdisc->padded);
549}
550
David S. Millerdc2b4842008-07-08 17:18:23 -0700551/* Under queue->lock and BH! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553void qdisc_destroy(struct Qdisc *qdisc)
554{
Eric Dumazet20fea082007-11-14 01:44:41 -0800555 const struct Qdisc_ops *ops = qdisc->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 if (qdisc->flags & TCQ_F_BUILTIN ||
Patrick McHardy85670cc2006-09-27 16:45:45 -0700558 !atomic_dec_and_test(&qdisc->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return;
560
Patrick McHardy85670cc2006-09-27 16:45:45 -0700561 list_del(&qdisc->list);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700562 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700563 if (ops->reset)
564 ops->reset(qdisc);
565 if (ops->destroy)
566 ops->destroy(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Patrick McHardy85670cc2006-09-27 16:45:45 -0700568 module_put(ops->owner);
David S. Miller5ce2d482008-07-08 17:06:30 -0700569 dev_put(qdisc_dev(qdisc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 call_rcu(&qdisc->q_rcu, __qdisc_destroy);
571}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800572EXPORT_SYMBOL(qdisc_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
David S. Millere8a04642008-07-17 00:34:19 -0700574static bool dev_all_qdisc_sleeping_noop(struct net_device *dev)
575{
576 unsigned int i;
577
578 for (i = 0; i < dev->num_tx_queues; i++) {
579 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
580
581 if (txq->qdisc_sleeping != &noop_qdisc)
582 return false;
583 }
584 return true;
585}
586
587static void attach_one_default_qdisc(struct net_device *dev,
588 struct netdev_queue *dev_queue,
589 void *_unused)
590{
591 struct Qdisc *qdisc;
592
593 if (dev->tx_queue_len) {
594 qdisc = qdisc_create_dflt(dev, dev_queue,
595 &pfifo_fast_ops, TC_H_ROOT);
596 if (!qdisc) {
597 printk(KERN_INFO "%s: activation failed\n", dev->name);
598 return;
599 }
600 list_add_tail(&qdisc->list, &dev_queue->qdisc_list);
601 } else {
602 qdisc = &noqueue_qdisc;
603 }
604 dev_queue->qdisc_sleeping = qdisc;
605}
606
607static void transition_one_qdisc(struct net_device *dev,
608 struct netdev_queue *dev_queue,
609 void *_need_watchdog)
610{
611 int *need_watchdog_p = _need_watchdog;
612
613 spin_lock_bh(&dev_queue->lock);
614 rcu_assign_pointer(dev_queue->qdisc, dev_queue->qdisc_sleeping);
615 if (dev_queue->qdisc != &noqueue_qdisc)
616 *need_watchdog_p = 1;
617 spin_unlock_bh(&dev_queue->lock);
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620void dev_activate(struct net_device *dev)
621{
David S. Millere8a04642008-07-17 00:34:19 -0700622 int need_watchdog;
David S. Millerb0e1e642008-07-08 17:42:10 -0700623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /* No queueing discipline is attached to device;
625 create default one i.e. pfifo_fast for devices,
626 which need queueing and noqueue_qdisc for
627 virtual interfaces
628 */
629
David S. Millere8a04642008-07-17 00:34:19 -0700630 if (dev_all_qdisc_sleeping_noop(dev))
631 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Tommy S. Christensencacaddf2005-05-03 16:18:52 -0700633 if (!netif_carrier_ok(dev))
634 /* Delay activation until next carrier-on event */
635 return;
636
David S. Millere8a04642008-07-17 00:34:19 -0700637 need_watchdog = 0;
638 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
639
640 if (need_watchdog) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 dev->trans_start = jiffies;
642 dev_watchdog_up(dev);
643 }
David S. Millerb0e1e642008-07-08 17:42:10 -0700644}
645
David S. Millere8a04642008-07-17 00:34:19 -0700646static void dev_deactivate_queue(struct net_device *dev,
647 struct netdev_queue *dev_queue,
648 void *_qdisc_default)
David S. Millerb0e1e642008-07-08 17:42:10 -0700649{
David S. Millere8a04642008-07-17 00:34:19 -0700650 struct Qdisc *qdisc_default = _qdisc_default;
David S. Millerd3b753d2008-07-15 20:14:35 -0700651 struct sk_buff *skb = NULL;
David S. Miller970565b2008-07-08 23:10:33 -0700652 struct Qdisc *qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -0700653
David S. Miller970565b2008-07-08 23:10:33 -0700654 spin_lock_bh(&dev_queue->lock);
655
656 qdisc = dev_queue->qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -0700657 if (qdisc) {
658 dev_queue->qdisc = qdisc_default;
659 qdisc_reset(qdisc);
David S. Millerd3b753d2008-07-15 20:14:35 -0700660
661 skb = qdisc->gso_skb;
662 qdisc->gso_skb = NULL;
David S. Millerb0e1e642008-07-08 17:42:10 -0700663 }
David S. Miller970565b2008-07-08 23:10:33 -0700664
665 spin_unlock_bh(&dev_queue->lock);
666
667 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
David S. Millere8a04642008-07-17 00:34:19 -0700670static bool some_qdisc_is_running(struct net_device *dev, int lock)
671{
672 unsigned int i;
673
674 for (i = 0; i < dev->num_tx_queues; i++) {
675 struct netdev_queue *dev_queue;
David S. Miller7698b4f2008-07-16 01:42:40 -0700676 spinlock_t *root_lock;
David S. Millere2627c82008-07-16 00:56:32 -0700677 struct Qdisc *q;
David S. Millere8a04642008-07-17 00:34:19 -0700678 int val;
679
680 dev_queue = netdev_get_tx_queue(dev, i);
David S. Millere2627c82008-07-16 00:56:32 -0700681 q = dev_queue->qdisc;
David S. Miller7698b4f2008-07-16 01:42:40 -0700682 root_lock = qdisc_root_lock(q);
David S. Millere8a04642008-07-17 00:34:19 -0700683
684 if (lock)
David S. Miller7698b4f2008-07-16 01:42:40 -0700685 spin_lock_bh(root_lock);
David S. Millere8a04642008-07-17 00:34:19 -0700686
David S. Millere2627c82008-07-16 00:56:32 -0700687 val = test_bit(__QDISC_STATE_RUNNING, &q->state);
David S. Millere8a04642008-07-17 00:34:19 -0700688
689 if (lock)
David S. Miller7698b4f2008-07-16 01:42:40 -0700690 spin_unlock_bh(root_lock);
David S. Millere8a04642008-07-17 00:34:19 -0700691
692 if (val)
693 return true;
694 }
695 return false;
696}
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698void dev_deactivate(struct net_device *dev)
699{
David S. Millere8a04642008-07-17 00:34:19 -0700700 bool running;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
David S. Millere8a04642008-07-17 00:34:19 -0700702 netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
Herbert Xu41a23b02007-05-10 14:12:47 -0700703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 dev_watchdog_down(dev);
705
Herbert Xuce0e32e2007-10-18 22:37:58 -0700706 /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
Herbert Xud4828d82006-06-22 02:28:18 -0700707 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Herbert Xud4828d82006-06-22 02:28:18 -0700709 /* Wait for outstanding qdisc_run calls. */
Herbert Xuce0e32e2007-10-18 22:37:58 -0700710 do {
David S. Millere8a04642008-07-17 00:34:19 -0700711 while (some_qdisc_is_running(dev, 0))
Herbert Xuce0e32e2007-10-18 22:37:58 -0700712 yield();
713
714 /*
715 * Double-check inside queue lock to ensure that all effects
716 * of the queue run are visible when we return.
717 */
David S. Millere8a04642008-07-17 00:34:19 -0700718 running = some_qdisc_is_running(dev, 1);
Herbert Xuce0e32e2007-10-18 22:37:58 -0700719
720 /*
721 * The running flag should never be set at this point because
722 * we've already set dev->qdisc to noop_qdisc *inside* the same
723 * pair of spin locks. That is, if any qdisc_run starts after
724 * our initial test it should see the noop_qdisc and then
725 * clear the RUNNING bit before dropping the queue lock. So
726 * if it is set here then we've found a bug.
727 */
728 } while (WARN_ON_ONCE(running));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
David S. Millerb0e1e642008-07-08 17:42:10 -0700731static void dev_init_scheduler_queue(struct net_device *dev,
732 struct netdev_queue *dev_queue,
David S. Millere8a04642008-07-17 00:34:19 -0700733 void *_qdisc)
David S. Millerb0e1e642008-07-08 17:42:10 -0700734{
David S. Millere8a04642008-07-17 00:34:19 -0700735 struct Qdisc *qdisc = _qdisc;
736
David S. Millerb0e1e642008-07-08 17:42:10 -0700737 dev_queue->qdisc = qdisc;
738 dev_queue->qdisc_sleeping = qdisc;
739 INIT_LIST_HEAD(&dev_queue->qdisc_list);
740}
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742void dev_init_scheduler(struct net_device *dev)
743{
744 qdisc_lock_tree(dev);
David S. Millere8a04642008-07-17 00:34:19 -0700745 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
David S. Millerb0e1e642008-07-08 17:42:10 -0700746 dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 qdisc_unlock_tree(dev);
748
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800749 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
David S. Millere8a04642008-07-17 00:34:19 -0700752static void shutdown_scheduler_queue(struct net_device *dev,
753 struct netdev_queue *dev_queue,
754 void *_qdisc_default)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
David S. Millerb0e1e642008-07-08 17:42:10 -0700756 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
David S. Millere8a04642008-07-17 00:34:19 -0700757 struct Qdisc *qdisc_default = _qdisc_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
David S. Millerb0e1e642008-07-08 17:42:10 -0700759 if (qdisc) {
760 dev_queue->qdisc = qdisc_default;
761 dev_queue->qdisc_sleeping = qdisc_default;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 qdisc_destroy(qdisc);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900764 }
David S. Millerb0e1e642008-07-08 17:42:10 -0700765}
766
767void dev_shutdown(struct net_device *dev)
768{
769 qdisc_lock_tree(dev);
David S. Millere8a04642008-07-17 00:34:19 -0700770 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
771 shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 BUG_TRAP(!timer_pending(&dev->watchdog_timer));
773 qdisc_unlock_tree(dev);
774}