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