Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
1da177e4 | 14 | #include <linux/bitops.h> |
1da177e4 LT |
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> | |
1da177e4 | 20 | #include <linux/errno.h> |
1da177e4 LT |
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> | |
1da177e4 LT |
27 | #include <net/pkt_sched.h> |
28 | ||
29 | /* Main transmission queue. */ | |
30 | ||
0463d4ae PM |
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. | |
fd44de7c PM |
37 | * - ingress filtering is serialized via top level device |
38 | * spinlock dev->ingress_lock. | |
0463d4ae | 39 | * - updates to tree and tree walking are only done under the rtnl mutex. |
1da177e4 | 40 | */ |
1da177e4 LT |
41 | |
42 | void qdisc_lock_tree(struct net_device *dev) | |
43 | { | |
1da177e4 | 44 | spin_lock_bh(&dev->queue_lock); |
fd44de7c | 45 | spin_lock(&dev->ingress_lock); |
1da177e4 LT |
46 | } |
47 | ||
48 | void qdisc_unlock_tree(struct net_device *dev) | |
49 | { | |
fd44de7c | 50 | spin_unlock(&dev->ingress_lock); |
1da177e4 | 51 | spin_unlock_bh(&dev->queue_lock); |
1da177e4 LT |
52 | } |
53 | ||
c716a81a JHS |
54 | static inline int qdisc_qlen(struct Qdisc *q) |
55 | { | |
c716a81a JHS |
56 | return q->q.qlen; |
57 | } | |
58 | ||
6c1361a6 KK |
59 | static inline int dev_requeue_skb(struct sk_buff *skb, struct net_device *dev, |
60 | struct Qdisc *q) | |
c716a81a | 61 | { |
c716a81a JHS |
62 | if (unlikely(skb->next)) |
63 | dev->gso_skb = skb; | |
64 | else | |
65 | q->ops->requeue(skb, q); | |
6c1361a6 | 66 | |
c716a81a JHS |
67 | netif_schedule(dev); |
68 | return 0; | |
69 | } | |
70 | ||
6c1361a6 KK |
71 | static inline struct sk_buff *dev_dequeue_skb(struct net_device *dev, |
72 | struct Qdisc *q) | |
c716a81a | 73 | { |
6c1361a6 | 74 | struct sk_buff *skb; |
c716a81a | 75 | |
6c1361a6 | 76 | if ((skb = dev->gso_skb)) |
c716a81a JHS |
77 | dev->gso_skb = NULL; |
78 | else | |
79 | skb = q->dequeue(q); | |
80 | ||
81 | return skb; | |
82 | } | |
83 | ||
6c1361a6 KK |
84 | static inline int handle_dev_cpu_collision(struct sk_buff *skb, |
85 | struct net_device *dev, | |
86 | struct Qdisc *q) | |
c716a81a | 87 | { |
6c1361a6 | 88 | int ret; |
c716a81a | 89 | |
6c1361a6 KK |
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 | */ | |
c716a81a | 97 | kfree_skb(skb); |
6c1361a6 KK |
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); | |
c716a81a JHS |
109 | } |
110 | ||
6c1361a6 | 111 | return ret; |
c716a81a JHS |
112 | } |
113 | ||
10297b99 | 114 | /* |
6c1361a6 KK |
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 | */ | |
48d83325 | 133 | static inline int qdisc_restart(struct net_device *dev) |
1da177e4 LT |
134 | { |
135 | struct Qdisc *q = dev->qdisc; | |
136 | struct sk_buff *skb; | |
5f1a485d | 137 | int ret = NETDEV_TX_BUSY; |
1da177e4 | 138 | |
6c1361a6 KK |
139 | /* Dequeue packet */ |
140 | if (unlikely((skb = dev_dequeue_skb(dev, q)) == NULL)) | |
c716a81a | 141 | return 0; |
f6a78bfc | 142 | |
6c1361a6 KK |
143 | |
144 | /* And release queue */ | |
c716a81a JHS |
145 | spin_unlock(&dev->queue_lock); |
146 | ||
8236632f | 147 | HARD_TX_LOCK(dev, smp_processor_id()); |
5f1a485d PWJ |
148 | if (!netif_subqueue_stopped(dev, skb)) |
149 | ret = dev_hard_start_xmit(skb, dev); | |
8236632f | 150 | HARD_TX_UNLOCK(dev); |
c716a81a JHS |
151 | |
152 | spin_lock(&dev->queue_lock); | |
c716a81a | 153 | q = dev->qdisc; |
c716a81a | 154 | |
6c1361a6 KK |
155 | switch (ret) { |
156 | case NETDEV_TX_OK: | |
157 | /* Driver sent out skb successfully */ | |
158 | ret = qdisc_qlen(q); | |
159 | break; | |
160 | ||
161 | case NETDEV_TX_LOCKED: | |
162 | /* Driver try lock failed */ | |
163 | ret = handle_dev_cpu_collision(skb, dev, q); | |
164 | break; | |
165 | ||
166 | default: | |
167 | /* Driver returned NETDEV_TX_BUSY - requeue skb */ | |
168 | if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit())) | |
169 | printk(KERN_WARNING "BUG %s code %d qlen %d\n", | |
170 | dev->name, ret, q->q.qlen); | |
171 | ||
172 | ret = dev_requeue_skb(skb, dev, q); | |
173 | break; | |
174 | } | |
c716a81a | 175 | |
6c1361a6 | 176 | return ret; |
1da177e4 LT |
177 | } |
178 | ||
48d83325 HX |
179 | void __qdisc_run(struct net_device *dev) |
180 | { | |
d90df3ad HX |
181 | do { |
182 | if (!qdisc_restart(dev)) | |
183 | break; | |
184 | } while (!netif_queue_stopped(dev)); | |
48d83325 HX |
185 | |
186 | clear_bit(__LINK_STATE_QDISC_RUNNING, &dev->state); | |
187 | } | |
188 | ||
1da177e4 LT |
189 | static void dev_watchdog(unsigned long arg) |
190 | { | |
191 | struct net_device *dev = (struct net_device *)arg; | |
192 | ||
932ff279 | 193 | netif_tx_lock(dev); |
1da177e4 LT |
194 | if (dev->qdisc != &noop_qdisc) { |
195 | if (netif_device_present(dev) && | |
196 | netif_running(dev) && | |
197 | netif_carrier_ok(dev)) { | |
198 | if (netif_queue_stopped(dev) && | |
338f7566 SH |
199 | time_after(jiffies, dev->trans_start + dev->watchdog_timeo)) { |
200 | ||
201 | printk(KERN_INFO "NETDEV WATCHDOG: %s: transmit timed out\n", | |
202 | dev->name); | |
1da177e4 LT |
203 | dev->tx_timeout(dev); |
204 | } | |
f5a6e01c | 205 | if (!mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + dev->watchdog_timeo))) |
1da177e4 LT |
206 | dev_hold(dev); |
207 | } | |
208 | } | |
932ff279 | 209 | netif_tx_unlock(dev); |
1da177e4 LT |
210 | |
211 | dev_put(dev); | |
212 | } | |
213 | ||
214 | static void dev_watchdog_init(struct net_device *dev) | |
215 | { | |
216 | init_timer(&dev->watchdog_timer); | |
217 | dev->watchdog_timer.data = (unsigned long)dev; | |
218 | dev->watchdog_timer.function = dev_watchdog; | |
219 | } | |
220 | ||
221 | void __netdev_watchdog_up(struct net_device *dev) | |
222 | { | |
223 | if (dev->tx_timeout) { | |
224 | if (dev->watchdog_timeo <= 0) | |
225 | dev->watchdog_timeo = 5*HZ; | |
60468d5b VP |
226 | if (!mod_timer(&dev->watchdog_timer, |
227 | round_jiffies(jiffies + dev->watchdog_timeo))) | |
1da177e4 LT |
228 | dev_hold(dev); |
229 | } | |
230 | } | |
231 | ||
232 | static void dev_watchdog_up(struct net_device *dev) | |
233 | { | |
1da177e4 | 234 | __netdev_watchdog_up(dev); |
1da177e4 LT |
235 | } |
236 | ||
237 | static void dev_watchdog_down(struct net_device *dev) | |
238 | { | |
932ff279 | 239 | netif_tx_lock_bh(dev); |
1da177e4 | 240 | if (del_timer(&dev->watchdog_timer)) |
15333061 | 241 | dev_put(dev); |
932ff279 | 242 | netif_tx_unlock_bh(dev); |
1da177e4 LT |
243 | } |
244 | ||
bea3348e SH |
245 | /** |
246 | * netif_carrier_on - set carrier | |
247 | * @dev: network device | |
248 | * | |
249 | * Device has detected that carrier. | |
250 | */ | |
0a242efc DV |
251 | void netif_carrier_on(struct net_device *dev) |
252 | { | |
bfaae0f0 | 253 | if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) { |
0a242efc | 254 | linkwatch_fire_event(dev); |
bfaae0f0 JG |
255 | if (netif_running(dev)) |
256 | __netdev_watchdog_up(dev); | |
257 | } | |
0a242efc DV |
258 | } |
259 | ||
bea3348e SH |
260 | /** |
261 | * netif_carrier_off - clear carrier | |
262 | * @dev: network device | |
263 | * | |
264 | * Device has detected loss of carrier. | |
265 | */ | |
0a242efc DV |
266 | void netif_carrier_off(struct net_device *dev) |
267 | { | |
268 | if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) | |
269 | linkwatch_fire_event(dev); | |
270 | } | |
271 | ||
1da177e4 LT |
272 | /* "NOOP" scheduler: the best scheduler, recommended for all interfaces |
273 | under all circumstances. It is difficult to invent anything faster or | |
274 | cheaper. | |
275 | */ | |
276 | ||
94df109a | 277 | static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc) |
1da177e4 LT |
278 | { |
279 | kfree_skb(skb); | |
280 | return NET_XMIT_CN; | |
281 | } | |
282 | ||
94df109a | 283 | static struct sk_buff *noop_dequeue(struct Qdisc * qdisc) |
1da177e4 LT |
284 | { |
285 | return NULL; | |
286 | } | |
287 | ||
94df109a | 288 | static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc) |
1da177e4 LT |
289 | { |
290 | if (net_ratelimit()) | |
94df109a TG |
291 | printk(KERN_DEBUG "%s deferred output. It is buggy.\n", |
292 | skb->dev->name); | |
1da177e4 LT |
293 | kfree_skb(skb); |
294 | return NET_XMIT_CN; | |
295 | } | |
296 | ||
297 | struct Qdisc_ops noop_qdisc_ops = { | |
1da177e4 LT |
298 | .id = "noop", |
299 | .priv_size = 0, | |
300 | .enqueue = noop_enqueue, | |
301 | .dequeue = noop_dequeue, | |
302 | .requeue = noop_requeue, | |
303 | .owner = THIS_MODULE, | |
304 | }; | |
305 | ||
306 | struct Qdisc noop_qdisc = { | |
307 | .enqueue = noop_enqueue, | |
308 | .dequeue = noop_dequeue, | |
309 | .flags = TCQ_F_BUILTIN, | |
10297b99 | 310 | .ops = &noop_qdisc_ops, |
1da177e4 LT |
311 | .list = LIST_HEAD_INIT(noop_qdisc.list), |
312 | }; | |
313 | ||
314 | static struct Qdisc_ops noqueue_qdisc_ops = { | |
1da177e4 LT |
315 | .id = "noqueue", |
316 | .priv_size = 0, | |
317 | .enqueue = noop_enqueue, | |
318 | .dequeue = noop_dequeue, | |
319 | .requeue = noop_requeue, | |
320 | .owner = THIS_MODULE, | |
321 | }; | |
322 | ||
323 | static struct Qdisc noqueue_qdisc = { | |
324 | .enqueue = NULL, | |
325 | .dequeue = noop_dequeue, | |
326 | .flags = TCQ_F_BUILTIN, | |
327 | .ops = &noqueue_qdisc_ops, | |
328 | .list = LIST_HEAD_INIT(noqueue_qdisc.list), | |
329 | }; | |
330 | ||
331 | ||
332 | static const u8 prio2band[TC_PRIO_MAX+1] = | |
333 | { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 }; | |
334 | ||
335 | /* 3-band FIFO queue: old style, but should be a bit faster than | |
336 | generic prio+fifo combination. | |
337 | */ | |
338 | ||
f87a9c3d TG |
339 | #define PFIFO_FAST_BANDS 3 |
340 | ||
321090e7 TG |
341 | static inline struct sk_buff_head *prio2list(struct sk_buff *skb, |
342 | struct Qdisc *qdisc) | |
1da177e4 LT |
343 | { |
344 | struct sk_buff_head *list = qdisc_priv(qdisc); | |
321090e7 TG |
345 | return list + prio2band[skb->priority & TC_PRIO_MAX]; |
346 | } | |
1da177e4 | 347 | |
f87a9c3d | 348 | static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc) |
321090e7 TG |
349 | { |
350 | struct sk_buff_head *list = prio2list(skb, qdisc); | |
1da177e4 | 351 | |
821d24ae | 352 | if (skb_queue_len(list) < qdisc->dev->tx_queue_len) { |
1da177e4 | 353 | qdisc->q.qlen++; |
821d24ae | 354 | return __qdisc_enqueue_tail(skb, qdisc, list); |
1da177e4 | 355 | } |
821d24ae TG |
356 | |
357 | return qdisc_drop(skb, qdisc); | |
1da177e4 LT |
358 | } |
359 | ||
f87a9c3d | 360 | static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc) |
1da177e4 LT |
361 | { |
362 | int prio; | |
363 | struct sk_buff_head *list = qdisc_priv(qdisc); | |
1da177e4 | 364 | |
452f299d TG |
365 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) { |
366 | if (!skb_queue_empty(list + prio)) { | |
1da177e4 | 367 | qdisc->q.qlen--; |
452f299d | 368 | return __qdisc_dequeue_head(qdisc, list + prio); |
1da177e4 LT |
369 | } |
370 | } | |
f87a9c3d | 371 | |
1da177e4 LT |
372 | return NULL; |
373 | } | |
374 | ||
f87a9c3d | 375 | static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc) |
1da177e4 | 376 | { |
1da177e4 | 377 | qdisc->q.qlen++; |
321090e7 | 378 | return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc)); |
1da177e4 LT |
379 | } |
380 | ||
f87a9c3d | 381 | static void pfifo_fast_reset(struct Qdisc* qdisc) |
1da177e4 LT |
382 | { |
383 | int prio; | |
384 | struct sk_buff_head *list = qdisc_priv(qdisc); | |
385 | ||
f87a9c3d | 386 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) |
821d24ae TG |
387 | __qdisc_reset_queue(qdisc, list + prio); |
388 | ||
389 | qdisc->qstats.backlog = 0; | |
1da177e4 LT |
390 | qdisc->q.qlen = 0; |
391 | } | |
392 | ||
393 | static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb) | |
394 | { | |
f87a9c3d | 395 | struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS }; |
1da177e4 | 396 | |
1da177e4 LT |
397 | memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1); |
398 | RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt); | |
399 | return skb->len; | |
400 | ||
401 | rtattr_failure: | |
1da177e4 LT |
402 | return -1; |
403 | } | |
404 | ||
405 | static int pfifo_fast_init(struct Qdisc *qdisc, struct rtattr *opt) | |
406 | { | |
f87a9c3d | 407 | int prio; |
1da177e4 LT |
408 | struct sk_buff_head *list = qdisc_priv(qdisc); |
409 | ||
f87a9c3d TG |
410 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) |
411 | skb_queue_head_init(list + prio); | |
1da177e4 LT |
412 | |
413 | return 0; | |
414 | } | |
415 | ||
416 | static struct Qdisc_ops pfifo_fast_ops = { | |
1da177e4 | 417 | .id = "pfifo_fast", |
f87a9c3d | 418 | .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head), |
1da177e4 LT |
419 | .enqueue = pfifo_fast_enqueue, |
420 | .dequeue = pfifo_fast_dequeue, | |
421 | .requeue = pfifo_fast_requeue, | |
422 | .init = pfifo_fast_init, | |
423 | .reset = pfifo_fast_reset, | |
424 | .dump = pfifo_fast_dump, | |
425 | .owner = THIS_MODULE, | |
426 | }; | |
427 | ||
3d54b82f | 428 | struct Qdisc *qdisc_alloc(struct net_device *dev, struct Qdisc_ops *ops) |
1da177e4 LT |
429 | { |
430 | void *p; | |
431 | struct Qdisc *sch; | |
3d54b82f TG |
432 | unsigned int size; |
433 | int err = -ENOBUFS; | |
1da177e4 LT |
434 | |
435 | /* ensure that the Qdisc and the private data are 32-byte aligned */ | |
3d54b82f TG |
436 | size = QDISC_ALIGN(sizeof(*sch)); |
437 | size += ops->priv_size + (QDISC_ALIGNTO - 1); | |
1da177e4 | 438 | |
0da974f4 | 439 | p = kzalloc(size, GFP_KERNEL); |
1da177e4 | 440 | if (!p) |
3d54b82f | 441 | goto errout; |
3d54b82f TG |
442 | sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p); |
443 | sch->padded = (char *) sch - (char *) p; | |
1da177e4 LT |
444 | |
445 | INIT_LIST_HEAD(&sch->list); | |
446 | skb_queue_head_init(&sch->q); | |
447 | sch->ops = ops; | |
448 | sch->enqueue = ops->enqueue; | |
449 | sch->dequeue = ops->dequeue; | |
450 | sch->dev = dev; | |
451 | dev_hold(dev); | |
1da177e4 | 452 | atomic_set(&sch->refcnt, 1); |
3d54b82f TG |
453 | |
454 | return sch; | |
455 | errout: | |
456 | return ERR_PTR(-err); | |
457 | } | |
458 | ||
9f9afec4 PM |
459 | struct Qdisc * qdisc_create_dflt(struct net_device *dev, struct Qdisc_ops *ops, |
460 | unsigned int parentid) | |
3d54b82f TG |
461 | { |
462 | struct Qdisc *sch; | |
10297b99 | 463 | |
3d54b82f TG |
464 | sch = qdisc_alloc(dev, ops); |
465 | if (IS_ERR(sch)) | |
466 | goto errout; | |
fd44de7c | 467 | sch->stats_lock = &dev->queue_lock; |
9f9afec4 | 468 | sch->parent = parentid; |
3d54b82f | 469 | |
1da177e4 LT |
470 | if (!ops->init || ops->init(sch, NULL) == 0) |
471 | return sch; | |
472 | ||
0fbbeb1b | 473 | qdisc_destroy(sch); |
3d54b82f | 474 | errout: |
1da177e4 LT |
475 | return NULL; |
476 | } | |
477 | ||
478 | /* Under dev->queue_lock and BH! */ | |
479 | ||
480 | void qdisc_reset(struct Qdisc *qdisc) | |
481 | { | |
482 | struct Qdisc_ops *ops = qdisc->ops; | |
483 | ||
484 | if (ops->reset) | |
485 | ops->reset(qdisc); | |
486 | } | |
487 | ||
10297b99 | 488 | /* this is the rcu callback function to clean up a qdisc when there |
1da177e4 LT |
489 | * are no further references to it */ |
490 | ||
491 | static void __qdisc_destroy(struct rcu_head *head) | |
492 | { | |
493 | struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu); | |
1da177e4 LT |
494 | kfree((char *) qdisc - qdisc->padded); |
495 | } | |
496 | ||
497 | /* Under dev->queue_lock and BH! */ | |
498 | ||
499 | void qdisc_destroy(struct Qdisc *qdisc) | |
500 | { | |
85670cc1 | 501 | struct Qdisc_ops *ops = qdisc->ops; |
1da177e4 LT |
502 | |
503 | if (qdisc->flags & TCQ_F_BUILTIN || | |
85670cc1 | 504 | !atomic_dec_and_test(&qdisc->refcnt)) |
1da177e4 LT |
505 | return; |
506 | ||
85670cc1 | 507 | list_del(&qdisc->list); |
85670cc1 | 508 | gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est); |
85670cc1 PM |
509 | if (ops->reset) |
510 | ops->reset(qdisc); | |
511 | if (ops->destroy) | |
512 | ops->destroy(qdisc); | |
1da177e4 | 513 | |
85670cc1 PM |
514 | module_put(ops->owner); |
515 | dev_put(qdisc->dev); | |
1da177e4 LT |
516 | call_rcu(&qdisc->q_rcu, __qdisc_destroy); |
517 | } | |
518 | ||
519 | void dev_activate(struct net_device *dev) | |
520 | { | |
521 | /* No queueing discipline is attached to device; | |
522 | create default one i.e. pfifo_fast for devices, | |
523 | which need queueing and noqueue_qdisc for | |
524 | virtual interfaces | |
525 | */ | |
526 | ||
527 | if (dev->qdisc_sleeping == &noop_qdisc) { | |
528 | struct Qdisc *qdisc; | |
529 | if (dev->tx_queue_len) { | |
9f9afec4 PM |
530 | qdisc = qdisc_create_dflt(dev, &pfifo_fast_ops, |
531 | TC_H_ROOT); | |
1da177e4 LT |
532 | if (qdisc == NULL) { |
533 | printk(KERN_INFO "%s: activation failed\n", dev->name); | |
534 | return; | |
535 | } | |
1da177e4 | 536 | list_add_tail(&qdisc->list, &dev->qdisc_list); |
1da177e4 LT |
537 | } else { |
538 | qdisc = &noqueue_qdisc; | |
539 | } | |
1da177e4 | 540 | dev->qdisc_sleeping = qdisc; |
1da177e4 LT |
541 | } |
542 | ||
cacaddf5 TC |
543 | if (!netif_carrier_ok(dev)) |
544 | /* Delay activation until next carrier-on event */ | |
545 | return; | |
546 | ||
1da177e4 LT |
547 | spin_lock_bh(&dev->queue_lock); |
548 | rcu_assign_pointer(dev->qdisc, dev->qdisc_sleeping); | |
549 | if (dev->qdisc != &noqueue_qdisc) { | |
550 | dev->trans_start = jiffies; | |
551 | dev_watchdog_up(dev); | |
552 | } | |
553 | spin_unlock_bh(&dev->queue_lock); | |
554 | } | |
555 | ||
556 | void dev_deactivate(struct net_device *dev) | |
557 | { | |
558 | struct Qdisc *qdisc; | |
41a23b07 | 559 | struct sk_buff *skb; |
ce0e32e6 | 560 | int running; |
1da177e4 LT |
561 | |
562 | spin_lock_bh(&dev->queue_lock); | |
563 | qdisc = dev->qdisc; | |
564 | dev->qdisc = &noop_qdisc; | |
565 | ||
566 | qdisc_reset(qdisc); | |
567 | ||
41a23b07 HX |
568 | skb = dev->gso_skb; |
569 | dev->gso_skb = NULL; | |
1da177e4 LT |
570 | spin_unlock_bh(&dev->queue_lock); |
571 | ||
41a23b07 HX |
572 | kfree_skb(skb); |
573 | ||
1da177e4 LT |
574 | dev_watchdog_down(dev); |
575 | ||
ce0e32e6 | 576 | /* Wait for outstanding qdisc-less dev_queue_xmit calls. */ |
d4828d85 | 577 | synchronize_rcu(); |
1da177e4 | 578 | |
d4828d85 | 579 | /* Wait for outstanding qdisc_run calls. */ |
ce0e32e6 HX |
580 | do { |
581 | while (test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state)) | |
582 | yield(); | |
583 | ||
584 | /* | |
585 | * Double-check inside queue lock to ensure that all effects | |
586 | * of the queue run are visible when we return. | |
587 | */ | |
588 | spin_lock_bh(&dev->queue_lock); | |
589 | running = test_bit(__LINK_STATE_QDISC_RUNNING, &dev->state); | |
590 | spin_unlock_bh(&dev->queue_lock); | |
591 | ||
592 | /* | |
593 | * The running flag should never be set at this point because | |
594 | * we've already set dev->qdisc to noop_qdisc *inside* the same | |
595 | * pair of spin locks. That is, if any qdisc_run starts after | |
596 | * our initial test it should see the noop_qdisc and then | |
597 | * clear the RUNNING bit before dropping the queue lock. So | |
598 | * if it is set here then we've found a bug. | |
599 | */ | |
600 | } while (WARN_ON_ONCE(running)); | |
1da177e4 LT |
601 | } |
602 | ||
603 | void dev_init_scheduler(struct net_device *dev) | |
604 | { | |
605 | qdisc_lock_tree(dev); | |
606 | dev->qdisc = &noop_qdisc; | |
607 | dev->qdisc_sleeping = &noop_qdisc; | |
608 | INIT_LIST_HEAD(&dev->qdisc_list); | |
609 | qdisc_unlock_tree(dev); | |
610 | ||
611 | dev_watchdog_init(dev); | |
612 | } | |
613 | ||
614 | void dev_shutdown(struct net_device *dev) | |
615 | { | |
616 | struct Qdisc *qdisc; | |
617 | ||
618 | qdisc_lock_tree(dev); | |
619 | qdisc = dev->qdisc_sleeping; | |
620 | dev->qdisc = &noop_qdisc; | |
621 | dev->qdisc_sleeping = &noop_qdisc; | |
622 | qdisc_destroy(qdisc); | |
623 | #if defined(CONFIG_NET_SCH_INGRESS) || defined(CONFIG_NET_SCH_INGRESS_MODULE) | |
10297b99 | 624 | if ((qdisc = dev->qdisc_ingress) != NULL) { |
1da177e4 LT |
625 | dev->qdisc_ingress = NULL; |
626 | qdisc_destroy(qdisc); | |
10297b99 | 627 | } |
1da177e4 LT |
628 | #endif |
629 | BUG_TRAP(!timer_pending(&dev->watchdog_timer)); | |
630 | qdisc_unlock_tree(dev); | |
631 | } | |
632 | ||
0a242efc DV |
633 | EXPORT_SYMBOL(netif_carrier_on); |
634 | EXPORT_SYMBOL(netif_carrier_off); | |
1da177e4 | 635 | EXPORT_SYMBOL(noop_qdisc); |
1da177e4 LT |
636 | EXPORT_SYMBOL(qdisc_create_dflt); |
637 | EXPORT_SYMBOL(qdisc_destroy); | |
638 | EXPORT_SYMBOL(qdisc_reset); | |
1da177e4 LT |
639 | EXPORT_SYMBOL(qdisc_lock_tree); |
640 | EXPORT_SYMBOL(qdisc_unlock_tree); |