Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 14 | #include <asm/uaccess.h> |
| 15 | #include <asm/system.h> |
| 16 | #include <linux/bitops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/socket.h> |
| 24 | #include <linux/sockios.h> |
| 25 | #include <linux/in.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/netdevice.h> |
| 29 | #include <linux/skbuff.h> |
| 30 | #include <linux/rtnetlink.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/rcupdate.h> |
| 33 | #include <linux/list.h> |
| 34 | #include <net/sock.h> |
| 35 | #include <net/pkt_sched.h> |
| 36 | |
| 37 | /* Main transmission queue. */ |
| 38 | |
Patrick McHardy | 0463d4a | 2007-04-16 17:02:10 -0700 | [diff] [blame^] | 39 | /* Modifications to data participating in scheduling must be protected with |
| 40 | * dev->queue_lock spinlock. |
| 41 | * |
| 42 | * The idea is the following: |
| 43 | * - enqueue, dequeue are serialized via top level device |
| 44 | * spinlock dev->queue_lock. |
| 45 | * - updates to tree and tree walking are only done under the rtnl mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | void qdisc_lock_tree(struct net_device *dev) |
| 49 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | spin_lock_bh(&dev->queue_lock); |
| 51 | } |
| 52 | |
| 53 | void qdisc_unlock_tree(struct net_device *dev) |
| 54 | { |
| 55 | spin_unlock_bh(&dev->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 58 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | dev->queue_lock serializes queue accesses for this device |
| 60 | AND dev->qdisc pointer itself. |
| 61 | |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 62 | netif_tx_lock serializes accesses to device driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 64 | dev->queue_lock and netif_tx_lock are mutually exclusive, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | if one is grabbed, another must be free. |
| 66 | */ |
| 67 | |
| 68 | |
| 69 | /* Kick device. |
| 70 | Note, that this procedure can be called by a watchdog timer, so that |
| 71 | we do not check dev->tbusy flag here. |
| 72 | |
| 73 | Returns: 0 - queue is empty. |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 74 | >0 - queue is not empty, but throttled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | <0 - queue is not empty. Device is throttled, if dev->tbusy != 0. |
| 76 | |
| 77 | NOTE: Called under dev->queue_lock with locally disabled BH. |
| 78 | */ |
| 79 | |
Herbert Xu | 48d8332 | 2006-06-19 23:57:59 -0700 | [diff] [blame] | 80 | static inline int qdisc_restart(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
| 82 | struct Qdisc *q = dev->qdisc; |
| 83 | struct sk_buff *skb; |
| 84 | |
| 85 | /* Dequeue packet */ |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 86 | if (((skb = dev->gso_skb)) || ((skb = q->dequeue(q)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | unsigned nolock = (dev->features & NETIF_F_LLTX); |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 88 | |
| 89 | dev->gso_skb = NULL; |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | /* |
| 92 | * When the driver has LLTX set it does its own locking |
| 93 | * in start_xmit. No need to add additional overhead by |
| 94 | * locking again. These checks are worth it because |
| 95 | * even uncongested locks can be quite expensive. |
| 96 | * The driver can do trylock like here too, in case |
| 97 | * of lock congestion it should return -1 and the packet |
| 98 | * will be requeued. |
| 99 | */ |
| 100 | if (!nolock) { |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 101 | if (!netif_tx_trylock(dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | collision: |
| 103 | /* So, someone grabbed the driver. */ |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | /* It may be transient configuration error, |
| 106 | when hard_start_xmit() recurses. We detect |
| 107 | it by checking xmit owner and drop the |
| 108 | packet when deadloop is detected. |
| 109 | */ |
| 110 | if (dev->xmit_lock_owner == smp_processor_id()) { |
| 111 | kfree_skb(skb); |
| 112 | if (net_ratelimit()) |
| 113 | printk(KERN_DEBUG "Dead loop on netdevice %s, fix it urgently!\n", dev->name); |
| 114 | return -1; |
| 115 | } |
| 116 | __get_cpu_var(netdev_rx_stat).cpu_collision++; |
| 117 | goto requeue; |
| 118 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
| 122 | /* And release queue */ |
| 123 | spin_unlock(&dev->queue_lock); |
| 124 | |
| 125 | if (!netif_queue_stopped(dev)) { |
| 126 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 128 | ret = dev_hard_start_xmit(skb, dev); |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 129 | if (ret == NETDEV_TX_OK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | if (!nolock) { |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 131 | netif_tx_unlock(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | spin_lock(&dev->queue_lock); |
| 134 | return -1; |
| 135 | } |
| 136 | if (ret == NETDEV_TX_LOCKED && nolock) { |
| 137 | spin_lock(&dev->queue_lock); |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 138 | goto collision; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | /* NETDEV_TX_BUSY - we need to requeue */ |
| 143 | /* Release the driver */ |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 144 | if (!nolock) { |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 145 | netif_tx_unlock(dev); |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | spin_lock(&dev->queue_lock); |
| 148 | q = dev->qdisc; |
| 149 | } |
| 150 | |
| 151 | /* Device kicked us out :( |
| 152 | This is possible in three cases: |
| 153 | |
| 154 | 0. driver is locked |
| 155 | 1. fastroute is enabled |
| 156 | 2. device cannot determine busy state |
| 157 | before start of transmission (f.e. dialout) |
| 158 | 3. device is buggy (ppp) |
| 159 | */ |
| 160 | |
| 161 | requeue: |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 162 | if (skb->next) |
| 163 | dev->gso_skb = skb; |
| 164 | else |
| 165 | q->ops->requeue(skb, q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | netif_schedule(dev); |
| 167 | return 1; |
| 168 | } |
Stephen Hemminger | 8cbe1d4 | 2005-05-03 16:24:03 -0700 | [diff] [blame] | 169 | BUG_ON((int) q->q.qlen < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return q->q.qlen; |
| 171 | } |
| 172 | |
Herbert Xu | 48d8332 | 2006-06-19 23:57:59 -0700 | [diff] [blame] | 173 | void __qdisc_run(struct net_device *dev) |
| 174 | { |
Herbert Xu | d4828d8 | 2006-06-22 02:28:18 -0700 | [diff] [blame] | 175 | if (unlikely(dev->qdisc == &noop_qdisc)) |
| 176 | goto out; |
| 177 | |
Herbert Xu | 48d8332 | 2006-06-19 23:57:59 -0700 | [diff] [blame] | 178 | while (qdisc_restart(dev) < 0 && !netif_queue_stopped(dev)) |
| 179 | /* NOTHING */; |
| 180 | |
Herbert Xu | d4828d8 | 2006-06-22 02:28:18 -0700 | [diff] [blame] | 181 | out: |
Herbert Xu | 48d8332 | 2006-06-19 23:57:59 -0700 | [diff] [blame] | 182 | clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state); |
| 183 | } |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | static void dev_watchdog(unsigned long arg) |
| 186 | { |
| 187 | struct net_device *dev = (struct net_device *)arg; |
| 188 | |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 189 | netif_tx_lock(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | if (dev->qdisc != &noop_qdisc) { |
| 191 | if (netif_device_present(dev) && |
| 192 | netif_running(dev) && |
| 193 | netif_carrier_ok(dev)) { |
| 194 | if (netif_queue_stopped(dev) && |
Stephen Hemminger | 338f756 | 2006-05-16 15:02:12 -0700 | [diff] [blame] | 195 | time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) { |
| 196 | |
| 197 | printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n", |
| 198 | dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | dev->tx_timeout(dev); |
| 200 | } |
Arjan van de Ven | f5a6e01 | 2007-02-05 17:59:51 -0800 | [diff] [blame] | 201 | if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | dev_hold(dev); |
| 203 | } |
| 204 | } |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 205 | netif_tx_unlock(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | dev_put(dev); |
| 208 | } |
| 209 | |
| 210 | static void dev_watchdog_init(struct net_device *dev) |
| 211 | { |
| 212 | init_timer(&dev->watchdog_timer); |
| 213 | dev->watchdog_timer.data = (unsigned long)dev; |
| 214 | dev->watchdog_timer.function = dev_watchdog; |
| 215 | } |
| 216 | |
| 217 | void __netdev_watchdog_up(struct net_device *dev) |
| 218 | { |
| 219 | if (dev->tx_timeout) { |
| 220 | if (dev->watchdog_timeo <= 0) |
| 221 | dev->watchdog_timeo = 5*HZ; |
| 222 | if (!mod_timer(&dev->watchdog_timer, jiffies + dev->watchdog_timeo)) |
| 223 | dev_hold(dev); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | static void dev_watchdog_up(struct net_device *dev) |
| 228 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | __netdev_watchdog_up(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static void dev_watchdog_down(struct net_device *dev) |
| 233 | { |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 234 | netif_tx_lock_bh(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | if (del_timer(&dev->watchdog_timer)) |
Stephen Hemminger | 1533306 | 2006-03-20 22:32:28 -0800 | [diff] [blame] | 236 | dev_put(dev); |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 237 | netif_tx_unlock_bh(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Denis Vlasenko | 0a242ef | 2005-08-11 15:32:53 -0700 | [diff] [blame] | 240 | void netif_carrier_on(struct net_device *dev) |
| 241 | { |
| 242 | if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) |
| 243 | linkwatch_fire_event(dev); |
| 244 | if (netif_running(dev)) |
| 245 | __netdev_watchdog_up(dev); |
| 246 | } |
| 247 | |
| 248 | void netif_carrier_off(struct net_device *dev) |
| 249 | { |
| 250 | if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) |
| 251 | linkwatch_fire_event(dev); |
| 252 | } |
| 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | /* "NOOP" scheduler: the best scheduler, recommended for all interfaces |
| 255 | under all circumstances. It is difficult to invent anything faster or |
| 256 | cheaper. |
| 257 | */ |
| 258 | |
Thomas Graf | 94df109 | 2005-06-18 22:59:08 -0700 | [diff] [blame] | 259 | static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
| 261 | kfree_skb(skb); |
| 262 | return NET_XMIT_CN; |
| 263 | } |
| 264 | |
Thomas Graf | 94df109 | 2005-06-18 22:59:08 -0700 | [diff] [blame] | 265 | static struct sk_buff *noop_dequeue(struct Qdisc * qdisc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
| 267 | return NULL; |
| 268 | } |
| 269 | |
Thomas Graf | 94df109 | 2005-06-18 22:59:08 -0700 | [diff] [blame] | 270 | static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
| 272 | if (net_ratelimit()) |
Thomas Graf | 94df109 | 2005-06-18 22:59:08 -0700 | [diff] [blame] | 273 | printk(KERN_DEBUG "%s deferred output. It is buggy.\n", |
| 274 | skb->dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | kfree_skb(skb); |
| 276 | return NET_XMIT_CN; |
| 277 | } |
| 278 | |
| 279 | struct Qdisc_ops noop_qdisc_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | .id = "noop", |
| 281 | .priv_size = 0, |
| 282 | .enqueue = noop_enqueue, |
| 283 | .dequeue = noop_dequeue, |
| 284 | .requeue = noop_requeue, |
| 285 | .owner = THIS_MODULE, |
| 286 | }; |
| 287 | |
| 288 | struct Qdisc noop_qdisc = { |
| 289 | .enqueue = noop_enqueue, |
| 290 | .dequeue = noop_dequeue, |
| 291 | .flags = TCQ_F_BUILTIN, |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 292 | .ops = &noop_qdisc_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | .list = LIST_HEAD_INIT(noop_qdisc.list), |
| 294 | }; |
| 295 | |
| 296 | static struct Qdisc_ops noqueue_qdisc_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | .id = "noqueue", |
| 298 | .priv_size = 0, |
| 299 | .enqueue = noop_enqueue, |
| 300 | .dequeue = noop_dequeue, |
| 301 | .requeue = noop_requeue, |
| 302 | .owner = THIS_MODULE, |
| 303 | }; |
| 304 | |
| 305 | static struct Qdisc noqueue_qdisc = { |
| 306 | .enqueue = NULL, |
| 307 | .dequeue = noop_dequeue, |
| 308 | .flags = TCQ_F_BUILTIN, |
| 309 | .ops = &noqueue_qdisc_ops, |
| 310 | .list = LIST_HEAD_INIT(noqueue_qdisc.list), |
| 311 | }; |
| 312 | |
| 313 | |
| 314 | static const u8 prio2band[TC_PRIO_MAX+1] = |
| 315 | { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 }; |
| 316 | |
| 317 | /* 3-band FIFO queue: old style, but should be a bit faster than |
| 318 | generic prio+fifo combination. |
| 319 | */ |
| 320 | |
Thomas Graf | f87a9c3 | 2005-06-18 22:58:53 -0700 | [diff] [blame] | 321 | #define PFIFO_FAST_BANDS 3 |
| 322 | |
Thomas Graf | 321090e | 2005-06-18 22:58:35 -0700 | [diff] [blame] | 323 | static inline struct sk_buff_head *prio2list(struct sk_buff *skb, |
| 324 | struct Qdisc *qdisc) |
| 325 | { |
| 326 | struct sk_buff_head *list = qdisc_priv(qdisc); |
| 327 | return list + prio2band[skb->priority & TC_PRIO_MAX]; |
| 328 | } |
| 329 | |
Thomas Graf | f87a9c3 | 2005-06-18 22:58:53 -0700 | [diff] [blame] | 330 | static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | { |
Thomas Graf | 321090e | 2005-06-18 22:58:35 -0700 | [diff] [blame] | 332 | struct sk_buff_head *list = prio2list(skb, qdisc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Thomas Graf | 821d24a | 2005-06-18 22:58:15 -0700 | [diff] [blame] | 334 | if (skb_queue_len(list) < qdisc->dev->tx_queue_len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | qdisc->q.qlen++; |
Thomas Graf | 821d24a | 2005-06-18 22:58:15 -0700 | [diff] [blame] | 336 | return __qdisc_enqueue_tail(skb, qdisc, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
Thomas Graf | 821d24a | 2005-06-18 22:58:15 -0700 | [diff] [blame] | 338 | |
| 339 | return qdisc_drop(skb, qdisc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Thomas Graf | f87a9c3 | 2005-06-18 22:58:53 -0700 | [diff] [blame] | 342 | static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
| 344 | int prio; |
| 345 | struct sk_buff_head *list = qdisc_priv(qdisc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Thomas Graf | 452f299 | 2005-07-18 13:30:53 -0700 | [diff] [blame] | 347 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) { |
| 348 | if (!skb_queue_empty(list + prio)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | qdisc->q.qlen--; |
Thomas Graf | 452f299 | 2005-07-18 13:30:53 -0700 | [diff] [blame] | 350 | return __qdisc_dequeue_head(qdisc, list + prio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
Thomas Graf | f87a9c3 | 2005-06-18 22:58:53 -0700 | [diff] [blame] | 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return NULL; |
| 355 | } |
| 356 | |
Thomas Graf | f87a9c3 | 2005-06-18 22:58:53 -0700 | [diff] [blame] | 357 | static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | qdisc->q.qlen++; |
Thomas Graf | 321090e | 2005-06-18 22:58:35 -0700 | [diff] [blame] | 360 | return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Thomas Graf | f87a9c3 | 2005-06-18 22:58:53 -0700 | [diff] [blame] | 363 | static void pfifo_fast_reset(struct Qdisc* qdisc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
| 365 | int prio; |
| 366 | struct sk_buff_head *list = qdisc_priv(qdisc); |
| 367 | |
Thomas Graf | f87a9c3 | 2005-06-18 22:58:53 -0700 | [diff] [blame] | 368 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) |
Thomas Graf | 821d24a | 2005-06-18 22:58:15 -0700 | [diff] [blame] | 369 | __qdisc_reset_queue(qdisc, list + prio); |
| 370 | |
| 371 | qdisc->qstats.backlog = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | qdisc->q.qlen = 0; |
| 373 | } |
| 374 | |
| 375 | static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb) |
| 376 | { |
Thomas Graf | f87a9c3 | 2005-06-18 22:58:53 -0700 | [diff] [blame] | 377 | struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1); |
| 380 | RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt); |
| 381 | return skb->len; |
| 382 | |
| 383 | rtattr_failure: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | return -1; |
| 385 | } |
| 386 | |
| 387 | static int pfifo_fast_init(struct Qdisc *qdisc, struct rtattr *opt) |
| 388 | { |
Thomas Graf | f87a9c3 | 2005-06-18 22:58:53 -0700 | [diff] [blame] | 389 | int prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | struct sk_buff_head *list = qdisc_priv(qdisc); |
| 391 | |
Thomas Graf | f87a9c3 | 2005-06-18 22:58:53 -0700 | [diff] [blame] | 392 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) |
| 393 | skb_queue_head_init(list + prio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static struct Qdisc_ops pfifo_fast_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | .id = "pfifo_fast", |
Thomas Graf | f87a9c3 | 2005-06-18 22:58:53 -0700 | [diff] [blame] | 400 | .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | .enqueue = pfifo_fast_enqueue, |
| 402 | .dequeue = pfifo_fast_dequeue, |
| 403 | .requeue = pfifo_fast_requeue, |
| 404 | .init = pfifo_fast_init, |
| 405 | .reset = pfifo_fast_reset, |
| 406 | .dump = pfifo_fast_dump, |
| 407 | .owner = THIS_MODULE, |
| 408 | }; |
| 409 | |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 410 | struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
| 412 | void *p; |
| 413 | struct Qdisc *sch; |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 414 | unsigned int size; |
| 415 | int err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
| 417 | /* ensure that the Qdisc and the private data are 32-byte aligned */ |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 418 | size = QDISC_ALIGN(sizeof(*sch)); |
| 419 | size += ops->priv_size + (QDISC_ALIGNTO - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 421 | p = kzalloc(size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | if (!p) |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 423 | goto errout; |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 424 | sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p); |
| 425 | sch->padded = (char *) sch - (char *) p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | INIT_LIST_HEAD(&sch->list); |
| 428 | skb_queue_head_init(&sch->q); |
| 429 | sch->ops = ops; |
| 430 | sch->enqueue = ops->enqueue; |
| 431 | sch->dequeue = ops->dequeue; |
| 432 | sch->dev = dev; |
| 433 | dev_hold(dev); |
| 434 | sch->stats_lock = &dev->queue_lock; |
| 435 | atomic_set(&sch->refcnt, 1); |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 436 | |
| 437 | return sch; |
| 438 | errout: |
| 439 | return ERR_PTR(-err); |
| 440 | } |
| 441 | |
Patrick McHardy | 9f9afec | 2006-11-29 17:35:18 -0800 | [diff] [blame] | 442 | struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops, |
| 443 | unsigned int parentid) |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 444 | { |
| 445 | struct Qdisc *sch; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 446 | |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 447 | sch = qdisc_alloc(dev, ops); |
| 448 | if (IS_ERR(sch)) |
| 449 | goto errout; |
Patrick McHardy | 9f9afec | 2006-11-29 17:35:18 -0800 | [diff] [blame] | 450 | sch->parent = parentid; |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | if (!ops->init || ops->init(sch, NULL) == 0) |
| 453 | return sch; |
| 454 | |
Thomas Graf | 0fbbeb1 | 2005-08-23 10:12:44 -0700 | [diff] [blame] | 455 | qdisc_destroy(sch); |
Thomas Graf | 3d54b82 | 2005-07-05 14:15:09 -0700 | [diff] [blame] | 456 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | return NULL; |
| 458 | } |
| 459 | |
| 460 | /* Under dev->queue_lock and BH! */ |
| 461 | |
| 462 | void qdisc_reset(struct Qdisc *qdisc) |
| 463 | { |
| 464 | struct Qdisc_ops *ops = qdisc->ops; |
| 465 | |
| 466 | if (ops->reset) |
| 467 | ops->reset(qdisc); |
| 468 | } |
| 469 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 470 | /* this is the rcu callback function to clean up a qdisc when there |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | * are no further references to it */ |
| 472 | |
| 473 | static void __qdisc_destroy(struct rcu_head *head) |
| 474 | { |
| 475 | struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | kfree((char *) qdisc - qdisc->padded); |
| 477 | } |
| 478 | |
| 479 | /* Under dev->queue_lock and BH! */ |
| 480 | |
| 481 | void qdisc_destroy(struct Qdisc *qdisc) |
| 482 | { |
Patrick McHardy | 85670cc | 2006-09-27 16:45:45 -0700 | [diff] [blame] | 483 | struct Qdisc_ops *ops = qdisc->ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
| 485 | if (qdisc->flags & TCQ_F_BUILTIN || |
Patrick McHardy | 85670cc | 2006-09-27 16:45:45 -0700 | [diff] [blame] | 486 | !atomic_dec_and_test(&qdisc->refcnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | return; |
| 488 | |
Patrick McHardy | 85670cc | 2006-09-27 16:45:45 -0700 | [diff] [blame] | 489 | list_del(&qdisc->list); |
| 490 | #ifdef CONFIG_NET_ESTIMATOR |
| 491 | gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est); |
| 492 | #endif |
| 493 | if (ops->reset) |
| 494 | ops->reset(qdisc); |
| 495 | if (ops->destroy) |
| 496 | ops->destroy(qdisc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Patrick McHardy | 85670cc | 2006-09-27 16:45:45 -0700 | [diff] [blame] | 498 | module_put(ops->owner); |
| 499 | dev_put(qdisc->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | call_rcu(&qdisc->q_rcu, __qdisc_destroy); |
| 501 | } |
| 502 | |
| 503 | void dev_activate(struct net_device *dev) |
| 504 | { |
| 505 | /* No queueing discipline is attached to device; |
| 506 | create default one i.e. pfifo_fast for devices, |
| 507 | which need queueing and noqueue_qdisc for |
| 508 | virtual interfaces |
| 509 | */ |
| 510 | |
| 511 | if (dev->qdisc_sleeping == &noop_qdisc) { |
| 512 | struct Qdisc *qdisc; |
| 513 | if (dev->tx_queue_len) { |
Patrick McHardy | 9f9afec | 2006-11-29 17:35:18 -0800 | [diff] [blame] | 514 | qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops, |
| 515 | TC_H_ROOT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | if (qdisc == NULL) { |
| 517 | printk(KERN_INFO "%s: activation failed\n", dev->name); |
| 518 | return; |
| 519 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | list_add_tail(&qdisc->list, &dev->qdisc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } else { |
| 522 | qdisc = &noqueue_qdisc; |
| 523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | dev->qdisc_sleeping = qdisc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Tommy S. Christensen | cacaddf | 2005-05-03 16:18:52 -0700 | [diff] [blame] | 527 | if (!netif_carrier_ok(dev)) |
| 528 | /* Delay activation until next carrier-on event */ |
| 529 | return; |
| 530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | spin_lock_bh(&dev->queue_lock); |
| 532 | rcu_assign_pointer(dev->qdisc, dev->qdisc_sleeping); |
| 533 | if (dev->qdisc != &noqueue_qdisc) { |
| 534 | dev->trans_start = jiffies; |
| 535 | dev_watchdog_up(dev); |
| 536 | } |
| 537 | spin_unlock_bh(&dev->queue_lock); |
| 538 | } |
| 539 | |
| 540 | void dev_deactivate(struct net_device *dev) |
| 541 | { |
| 542 | struct Qdisc *qdisc; |
| 543 | |
| 544 | spin_lock_bh(&dev->queue_lock); |
| 545 | qdisc = dev->qdisc; |
| 546 | dev->qdisc = &noop_qdisc; |
| 547 | |
| 548 | qdisc_reset(qdisc); |
| 549 | |
| 550 | spin_unlock_bh(&dev->queue_lock); |
| 551 | |
| 552 | dev_watchdog_down(dev); |
| 553 | |
Herbert Xu | d4828d8 | 2006-06-22 02:28:18 -0700 | [diff] [blame] | 554 | /* Wait for outstanding dev_queue_xmit calls. */ |
| 555 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Herbert Xu | d4828d8 | 2006-06-22 02:28:18 -0700 | [diff] [blame] | 557 | /* Wait for outstanding qdisc_run calls. */ |
| 558 | while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state)) |
| 559 | yield(); |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 560 | |
| 561 | if (dev->gso_skb) { |
| 562 | kfree_skb(dev->gso_skb); |
| 563 | dev->gso_skb = NULL; |
| 564 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | void dev_init_scheduler(struct net_device *dev) |
| 568 | { |
| 569 | qdisc_lock_tree(dev); |
| 570 | dev->qdisc = &noop_qdisc; |
| 571 | dev->qdisc_sleeping = &noop_qdisc; |
| 572 | INIT_LIST_HEAD(&dev->qdisc_list); |
| 573 | qdisc_unlock_tree(dev); |
| 574 | |
| 575 | dev_watchdog_init(dev); |
| 576 | } |
| 577 | |
| 578 | void dev_shutdown(struct net_device *dev) |
| 579 | { |
| 580 | struct Qdisc *qdisc; |
| 581 | |
| 582 | qdisc_lock_tree(dev); |
| 583 | qdisc = dev->qdisc_sleeping; |
| 584 | dev->qdisc = &noop_qdisc; |
| 585 | dev->qdisc_sleeping = &noop_qdisc; |
| 586 | qdisc_destroy(qdisc); |
| 587 | #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE) |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 588 | if ((qdisc = dev->qdisc_ingress) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | dev->qdisc_ingress = NULL; |
| 590 | qdisc_destroy(qdisc); |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | #endif |
| 593 | BUG_TRAP(!timer_pending(&dev->watchdog_timer)); |
| 594 | qdisc_unlock_tree(dev); |
| 595 | } |
| 596 | |
Denis Vlasenko | 0a242ef | 2005-08-11 15:32:53 -0700 | [diff] [blame] | 597 | EXPORT_SYMBOL(netif_carrier_on); |
| 598 | EXPORT_SYMBOL(netif_carrier_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | EXPORT_SYMBOL(noop_qdisc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | EXPORT_SYMBOL(qdisc_create_dflt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | EXPORT_SYMBOL(qdisc_destroy); |
| 602 | EXPORT_SYMBOL(qdisc_reset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | EXPORT_SYMBOL(qdisc_lock_tree); |
| 604 | EXPORT_SYMBOL(qdisc_unlock_tree); |