]>
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 | 31 | /* Modifications to data participating in scheduling must be protected with |
dc2b4847 | 32 | * queue->lock spinlock. |
0463d4ae PM |
33 | * |
34 | * The idea is the following: | |
35 | * - enqueue, dequeue are serialized via top level device | |
dc2b4847 | 36 | * spinlock queue->lock. |
fd44de7c | 37 | * - ingress filtering is serialized via top level device |
555353cf | 38 | * spinlock dev->rx_queue.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) | |
555353cf | 43 | __acquires(dev->rx_queue.lock) |
1da177e4 | 44 | { |
e8a0464c DM |
45 | 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 | } | |
555353cf | 52 | spin_lock(&dev->rx_queue.lock); |
1da177e4 | 53 | } |
62e3ba1b | 54 | EXPORT_SYMBOL(qdisc_lock_tree); |
1da177e4 LT |
55 | |
56 | void qdisc_unlock_tree(struct net_device *dev) | |
555353cf | 57 | __releases(dev->rx_queue.lock) |
1da177e4 | 58 | { |
e8a0464c DM |
59 | unsigned int i; |
60 | ||
555353cf | 61 | spin_unlock(&dev->rx_queue.lock); |
e8a0464c DM |
62 | 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(); | |
1da177e4 | 67 | } |
62e3ba1b | 68 | EXPORT_SYMBOL(qdisc_unlock_tree); |
1da177e4 | 69 | |
c716a81a JHS |
70 | static inline int qdisc_qlen(struct Qdisc *q) |
71 | { | |
c716a81a JHS |
72 | return q->q.qlen; |
73 | } | |
74 | ||
86d804e1 | 75 | static inline int dev_requeue_skb(struct sk_buff *skb, |
970565bb | 76 | struct netdev_queue *dev_queue, |
6c1361a6 | 77 | struct Qdisc *q) |
c716a81a | 78 | { |
c716a81a | 79 | if (unlikely(skb->next)) |
d3b753db | 80 | q->gso_skb = skb; |
c716a81a JHS |
81 | else |
82 | q->ops->requeue(skb, q); | |
6c1361a6 | 83 | |
86d804e1 | 84 | netif_schedule_queue(dev_queue); |
c716a81a JHS |
85 | return 0; |
86 | } | |
87 | ||
d3b753db | 88 | static inline struct sk_buff *dequeue_skb(struct Qdisc *q) |
c716a81a | 89 | { |
6c1361a6 | 90 | struct sk_buff *skb; |
c716a81a | 91 | |
d3b753db DM |
92 | if ((skb = q->gso_skb)) |
93 | q->gso_skb = NULL; | |
c716a81a JHS |
94 | else |
95 | skb = q->dequeue(q); | |
96 | ||
97 | return skb; | |
98 | } | |
99 | ||
6c1361a6 | 100 | static inline int handle_dev_cpu_collision(struct sk_buff *skb, |
970565bb | 101 | struct netdev_queue *dev_queue, |
6c1361a6 | 102 | struct Qdisc *q) |
c716a81a | 103 | { |
6c1361a6 | 104 | int ret; |
c716a81a | 105 | |
c773e847 | 106 | if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) { |
6c1361a6 KK |
107 | /* |
108 | * Same CPU holding the lock. It may be a transient | |
109 | * configuration error, when hard_start_xmit() recurses. We | |
110 | * detect it by checking xmit owner and drop the packet when | |
111 | * deadloop is detected. Return OK to try the next skb. | |
112 | */ | |
c716a81a | 113 | kfree_skb(skb); |
6c1361a6 KK |
114 | if (net_ratelimit()) |
115 | printk(KERN_WARNING "Dead loop on netdevice %s, " | |
c773e847 | 116 | "fix it urgently!\n", dev_queue->dev->name); |
6c1361a6 KK |
117 | ret = qdisc_qlen(q); |
118 | } else { | |
119 | /* | |
120 | * Another cpu is holding lock, requeue & delay xmits for | |
121 | * some time. | |
122 | */ | |
123 | __get_cpu_var(netdev_rx_stat).cpu_collision++; | |
86d804e1 | 124 | ret = dev_requeue_skb(skb, dev_queue, q); |
c716a81a JHS |
125 | } |
126 | ||
6c1361a6 | 127 | return ret; |
c716a81a JHS |
128 | } |
129 | ||
10297b99 | 130 | /* |
dc2b4847 | 131 | * NOTE: Called under queue->lock with locally disabled BH. |
6c1361a6 | 132 | * |
e2627c8c DM |
133 | * __QDISC_STATE_RUNNING guarantees only one CPU can process |
134 | * this qdisc at a time. queue->lock serializes queue accesses for | |
79d16385 | 135 | * this queue AND txq->qdisc pointer itself. |
6c1361a6 KK |
136 | * |
137 | * netif_tx_lock serializes accesses to device driver. | |
138 | * | |
dc2b4847 | 139 | * queue->lock and netif_tx_lock are mutually exclusive, |
6c1361a6 KK |
140 | * if one is grabbed, another must be free. |
141 | * | |
142 | * Note, that this procedure can be called by a watchdog timer | |
143 | * | |
144 | * Returns to the caller: | |
145 | * 0 - queue is empty or throttled. | |
146 | * >0 - queue is not empty. | |
147 | * | |
148 | */ | |
e2627c8c DM |
149 | static inline int qdisc_restart(struct netdev_queue *txq, |
150 | struct Qdisc *q) | |
1da177e4 | 151 | { |
5f1a485d | 152 | int ret = NETDEV_TX_BUSY; |
eb6aafe3 DM |
153 | struct net_device *dev; |
154 | struct sk_buff *skb; | |
1da177e4 | 155 | |
6c1361a6 | 156 | /* Dequeue packet */ |
d3b753db | 157 | if (unlikely((skb = dequeue_skb(q)) == NULL)) |
c716a81a | 158 | return 0; |
f6a78bfc | 159 | |
6c1361a6 | 160 | /* And release queue */ |
b0e1e646 | 161 | spin_unlock(&txq->lock); |
c716a81a | 162 | |
eb6aafe3 DM |
163 | dev = txq->dev; |
164 | ||
c773e847 | 165 | HARD_TX_LOCK(dev, txq, smp_processor_id()); |
5f1a485d | 166 | if (!netif_subqueue_stopped(dev, skb)) |
fd2ea0a7 | 167 | ret = dev_hard_start_xmit(skb, dev, txq); |
c773e847 | 168 | HARD_TX_UNLOCK(dev, txq); |
c716a81a | 169 | |
b0e1e646 | 170 | spin_lock(&txq->lock); |
c716a81a | 171 | |
6c1361a6 KK |
172 | switch (ret) { |
173 | case NETDEV_TX_OK: | |
174 | /* Driver sent out skb successfully */ | |
175 | ret = qdisc_qlen(q); | |
176 | break; | |
177 | ||
178 | case NETDEV_TX_LOCKED: | |
179 | /* Driver try lock failed */ | |
eb6aafe3 | 180 | ret = handle_dev_cpu_collision(skb, txq, q); |
6c1361a6 KK |
181 | break; |
182 | ||
183 | default: | |
184 | /* Driver returned NETDEV_TX_BUSY - requeue skb */ | |
185 | if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit())) | |
186 | printk(KERN_WARNING "BUG %s code %d qlen %d\n", | |
187 | dev->name, ret, q->q.qlen); | |
188 | ||
86d804e1 | 189 | ret = dev_requeue_skb(skb, txq, q); |
6c1361a6 KK |
190 | break; |
191 | } | |
c716a81a | 192 | |
6c1361a6 | 193 | return ret; |
1da177e4 LT |
194 | } |
195 | ||
eb6aafe3 | 196 | void __qdisc_run(struct netdev_queue *txq) |
48d83325 | 197 | { |
2ba2506c | 198 | unsigned long start_time = jiffies; |
e2627c8c | 199 | struct Qdisc *q = txq->qdisc; |
2ba2506c | 200 | |
e2627c8c | 201 | while (qdisc_restart(txq, q)) { |
fd2ea0a7 | 202 | if (netif_tx_queue_stopped(txq)) |
2ba2506c HX |
203 | break; |
204 | ||
205 | /* | |
206 | * Postpone processing if | |
207 | * 1. another process needs the CPU; | |
208 | * 2. we've been doing it for too long. | |
209 | */ | |
210 | if (need_resched() || jiffies != start_time) { | |
eb6aafe3 | 211 | netif_schedule_queue(txq); |
d90df3ad | 212 | break; |
2ba2506c HX |
213 | } |
214 | } | |
48d83325 | 215 | |
e2627c8c | 216 | clear_bit(__QDISC_STATE_RUNNING, &q->state); |
48d83325 HX |
217 | } |
218 | ||
1da177e4 LT |
219 | static void dev_watchdog(unsigned long arg) |
220 | { | |
221 | struct net_device *dev = (struct net_device *)arg; | |
222 | ||
932ff279 | 223 | netif_tx_lock(dev); |
e8a0464c | 224 | if (!qdisc_tx_is_noop(dev)) { |
1da177e4 LT |
225 | if (netif_device_present(dev) && |
226 | netif_running(dev) && | |
227 | netif_carrier_ok(dev)) { | |
e8a0464c DM |
228 | int some_queue_stopped = 0; |
229 | unsigned int i; | |
230 | ||
231 | for (i = 0; i < dev->num_tx_queues; i++) { | |
232 | struct netdev_queue *txq; | |
233 | ||
234 | txq = netdev_get_tx_queue(dev, i); | |
235 | if (netif_tx_queue_stopped(txq)) { | |
236 | some_queue_stopped = 1; | |
237 | break; | |
238 | } | |
239 | } | |
338f7566 | 240 | |
e8a0464c DM |
241 | if (some_queue_stopped && |
242 | time_after(jiffies, (dev->trans_start + | |
243 | dev->watchdog_timeo))) { | |
244 | printk(KERN_INFO "NETDEV WATCHDOG: %s: " | |
245 | "transmit timed out\n", | |
338f7566 | 246 | dev->name); |
1da177e4 | 247 | dev->tx_timeout(dev); |
b4192bbd | 248 | WARN_ON_ONCE(1); |
1da177e4 | 249 | } |
e8a0464c DM |
250 | if (!mod_timer(&dev->watchdog_timer, |
251 | round_jiffies(jiffies + | |
252 | dev->watchdog_timeo))) | |
1da177e4 LT |
253 | dev_hold(dev); |
254 | } | |
255 | } | |
932ff279 | 256 | netif_tx_unlock(dev); |
1da177e4 LT |
257 | |
258 | dev_put(dev); | |
259 | } | |
260 | ||
1da177e4 LT |
261 | void __netdev_watchdog_up(struct net_device *dev) |
262 | { | |
263 | if (dev->tx_timeout) { | |
264 | if (dev->watchdog_timeo <= 0) | |
265 | dev->watchdog_timeo = 5*HZ; | |
60468d5b VP |
266 | if (!mod_timer(&dev->watchdog_timer, |
267 | round_jiffies(jiffies + dev->watchdog_timeo))) | |
1da177e4 LT |
268 | dev_hold(dev); |
269 | } | |
270 | } | |
271 | ||
272 | static void dev_watchdog_up(struct net_device *dev) | |
273 | { | |
1da177e4 | 274 | __netdev_watchdog_up(dev); |
1da177e4 LT |
275 | } |
276 | ||
277 | static void dev_watchdog_down(struct net_device *dev) | |
278 | { | |
932ff279 | 279 | netif_tx_lock_bh(dev); |
1da177e4 | 280 | if (del_timer(&dev->watchdog_timer)) |
15333061 | 281 | dev_put(dev); |
932ff279 | 282 | netif_tx_unlock_bh(dev); |
1da177e4 LT |
283 | } |
284 | ||
bea3348e SH |
285 | /** |
286 | * netif_carrier_on - set carrier | |
287 | * @dev: network device | |
288 | * | |
289 | * Device has detected that carrier. | |
290 | */ | |
0a242efc DV |
291 | void netif_carrier_on(struct net_device *dev) |
292 | { | |
bfaae0f0 | 293 | if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) { |
0a242efc | 294 | linkwatch_fire_event(dev); |
bfaae0f0 JG |
295 | if (netif_running(dev)) |
296 | __netdev_watchdog_up(dev); | |
297 | } | |
0a242efc | 298 | } |
62e3ba1b | 299 | EXPORT_SYMBOL(netif_carrier_on); |
0a242efc | 300 | |
bea3348e SH |
301 | /** |
302 | * netif_carrier_off - clear carrier | |
303 | * @dev: network device | |
304 | * | |
305 | * Device has detected loss of carrier. | |
306 | */ | |
0a242efc DV |
307 | void netif_carrier_off(struct net_device *dev) |
308 | { | |
309 | if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) | |
310 | linkwatch_fire_event(dev); | |
311 | } | |
62e3ba1b | 312 | EXPORT_SYMBOL(netif_carrier_off); |
0a242efc | 313 | |
1da177e4 LT |
314 | /* "NOOP" scheduler: the best scheduler, recommended for all interfaces |
315 | under all circumstances. It is difficult to invent anything faster or | |
316 | cheaper. | |
317 | */ | |
318 | ||
94df109a | 319 | static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc) |
1da177e4 LT |
320 | { |
321 | kfree_skb(skb); | |
322 | return NET_XMIT_CN; | |
323 | } | |
324 | ||
94df109a | 325 | static struct sk_buff *noop_dequeue(struct Qdisc * qdisc) |
1da177e4 LT |
326 | { |
327 | return NULL; | |
328 | } | |
329 | ||
94df109a | 330 | static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc) |
1da177e4 LT |
331 | { |
332 | if (net_ratelimit()) | |
94df109a TG |
333 | printk(KERN_DEBUG "%s deferred output. It is buggy.\n", |
334 | skb->dev->name); | |
1da177e4 LT |
335 | kfree_skb(skb); |
336 | return NET_XMIT_CN; | |
337 | } | |
338 | ||
20fea08b | 339 | struct Qdisc_ops noop_qdisc_ops __read_mostly = { |
1da177e4 LT |
340 | .id = "noop", |
341 | .priv_size = 0, | |
342 | .enqueue = noop_enqueue, | |
343 | .dequeue = noop_dequeue, | |
344 | .requeue = noop_requeue, | |
345 | .owner = THIS_MODULE, | |
346 | }; | |
347 | ||
348 | struct Qdisc noop_qdisc = { | |
349 | .enqueue = noop_enqueue, | |
350 | .dequeue = noop_dequeue, | |
351 | .flags = TCQ_F_BUILTIN, | |
10297b99 | 352 | .ops = &noop_qdisc_ops, |
1da177e4 LT |
353 | .list = LIST_HEAD_INIT(noop_qdisc.list), |
354 | }; | |
62e3ba1b | 355 | EXPORT_SYMBOL(noop_qdisc); |
1da177e4 | 356 | |
20fea08b | 357 | static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = { |
1da177e4 LT |
358 | .id = "noqueue", |
359 | .priv_size = 0, | |
360 | .enqueue = noop_enqueue, | |
361 | .dequeue = noop_dequeue, | |
362 | .requeue = noop_requeue, | |
363 | .owner = THIS_MODULE, | |
364 | }; | |
365 | ||
366 | static struct Qdisc noqueue_qdisc = { | |
367 | .enqueue = NULL, | |
368 | .dequeue = noop_dequeue, | |
369 | .flags = TCQ_F_BUILTIN, | |
370 | .ops = &noqueue_qdisc_ops, | |
371 | .list = LIST_HEAD_INIT(noqueue_qdisc.list), | |
372 | }; | |
373 | ||
374 | ||
375 | static const u8 prio2band[TC_PRIO_MAX+1] = | |
376 | { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 }; | |
377 | ||
378 | /* 3-band FIFO queue: old style, but should be a bit faster than | |
379 | generic prio+fifo combination. | |
380 | */ | |
381 | ||
f87a9c3d TG |
382 | #define PFIFO_FAST_BANDS 3 |
383 | ||
321090e7 TG |
384 | static inline struct sk_buff_head *prio2list(struct sk_buff *skb, |
385 | struct Qdisc *qdisc) | |
1da177e4 LT |
386 | { |
387 | struct sk_buff_head *list = qdisc_priv(qdisc); | |
321090e7 TG |
388 | return list + prio2band[skb->priority & TC_PRIO_MAX]; |
389 | } | |
1da177e4 | 390 | |
f87a9c3d | 391 | static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc) |
321090e7 TG |
392 | { |
393 | struct sk_buff_head *list = prio2list(skb, qdisc); | |
1da177e4 | 394 | |
5ce2d488 | 395 | if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) { |
1da177e4 | 396 | qdisc->q.qlen++; |
821d24ae | 397 | return __qdisc_enqueue_tail(skb, qdisc, list); |
1da177e4 | 398 | } |
821d24ae TG |
399 | |
400 | return qdisc_drop(skb, qdisc); | |
1da177e4 LT |
401 | } |
402 | ||
f87a9c3d | 403 | static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc) |
1da177e4 LT |
404 | { |
405 | int prio; | |
406 | struct sk_buff_head *list = qdisc_priv(qdisc); | |
1da177e4 | 407 | |
452f299d TG |
408 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) { |
409 | if (!skb_queue_empty(list + prio)) { | |
1da177e4 | 410 | qdisc->q.qlen--; |
452f299d | 411 | return __qdisc_dequeue_head(qdisc, list + prio); |
1da177e4 LT |
412 | } |
413 | } | |
f87a9c3d | 414 | |
1da177e4 LT |
415 | return NULL; |
416 | } | |
417 | ||
f87a9c3d | 418 | static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc) |
1da177e4 | 419 | { |
1da177e4 | 420 | qdisc->q.qlen++; |
321090e7 | 421 | return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc)); |
1da177e4 LT |
422 | } |
423 | ||
f87a9c3d | 424 | static void pfifo_fast_reset(struct Qdisc* qdisc) |
1da177e4 LT |
425 | { |
426 | int prio; | |
427 | struct sk_buff_head *list = qdisc_priv(qdisc); | |
428 | ||
f87a9c3d | 429 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) |
821d24ae TG |
430 | __qdisc_reset_queue(qdisc, list + prio); |
431 | ||
432 | qdisc->qstats.backlog = 0; | |
1da177e4 LT |
433 | qdisc->q.qlen = 0; |
434 | } | |
435 | ||
436 | static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb) | |
437 | { | |
f87a9c3d | 438 | struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS }; |
1da177e4 | 439 | |
1da177e4 | 440 | memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1); |
1e90474c | 441 | NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt); |
1da177e4 LT |
442 | return skb->len; |
443 | ||
1e90474c | 444 | nla_put_failure: |
1da177e4 LT |
445 | return -1; |
446 | } | |
447 | ||
1e90474c | 448 | static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt) |
1da177e4 | 449 | { |
f87a9c3d | 450 | int prio; |
1da177e4 LT |
451 | struct sk_buff_head *list = qdisc_priv(qdisc); |
452 | ||
f87a9c3d TG |
453 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) |
454 | skb_queue_head_init(list + prio); | |
1da177e4 LT |
455 | |
456 | return 0; | |
457 | } | |
458 | ||
20fea08b | 459 | static struct Qdisc_ops pfifo_fast_ops __read_mostly = { |
1da177e4 | 460 | .id = "pfifo_fast", |
f87a9c3d | 461 | .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head), |
1da177e4 LT |
462 | .enqueue = pfifo_fast_enqueue, |
463 | .dequeue = pfifo_fast_dequeue, | |
464 | .requeue = pfifo_fast_requeue, | |
465 | .init = pfifo_fast_init, | |
466 | .reset = pfifo_fast_reset, | |
467 | .dump = pfifo_fast_dump, | |
468 | .owner = THIS_MODULE, | |
469 | }; | |
470 | ||
5ce2d488 | 471 | struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue, |
bb949fbd | 472 | struct Qdisc_ops *ops) |
1da177e4 LT |
473 | { |
474 | void *p; | |
475 | struct Qdisc *sch; | |
3d54b82f TG |
476 | unsigned int size; |
477 | int err = -ENOBUFS; | |
1da177e4 LT |
478 | |
479 | /* ensure that the Qdisc and the private data are 32-byte aligned */ | |
3d54b82f TG |
480 | size = QDISC_ALIGN(sizeof(*sch)); |
481 | size += ops->priv_size + (QDISC_ALIGNTO - 1); | |
1da177e4 | 482 | |
0da974f4 | 483 | p = kzalloc(size, GFP_KERNEL); |
1da177e4 | 484 | if (!p) |
3d54b82f | 485 | goto errout; |
3d54b82f TG |
486 | sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p); |
487 | sch->padded = (char *) sch - (char *) p; | |
1da177e4 LT |
488 | |
489 | INIT_LIST_HEAD(&sch->list); | |
490 | skb_queue_head_init(&sch->q); | |
491 | sch->ops = ops; | |
492 | sch->enqueue = ops->enqueue; | |
493 | sch->dequeue = ops->dequeue; | |
bb949fbd | 494 | sch->dev_queue = dev_queue; |
5ce2d488 | 495 | dev_hold(qdisc_dev(sch)); |
1da177e4 | 496 | atomic_set(&sch->refcnt, 1); |
3d54b82f TG |
497 | |
498 | return sch; | |
499 | errout: | |
01e123d7 | 500 | return ERR_PTR(err); |
3d54b82f TG |
501 | } |
502 | ||
bb949fbd DM |
503 | struct Qdisc * qdisc_create_dflt(struct net_device *dev, |
504 | struct netdev_queue *dev_queue, | |
505 | struct Qdisc_ops *ops, | |
9f9afec4 | 506 | unsigned int parentid) |
3d54b82f TG |
507 | { |
508 | struct Qdisc *sch; | |
10297b99 | 509 | |
5ce2d488 | 510 | sch = qdisc_alloc(dev_queue, ops); |
3d54b82f TG |
511 | if (IS_ERR(sch)) |
512 | goto errout; | |
9f9afec4 | 513 | sch->parent = parentid; |
3d54b82f | 514 | |
1da177e4 LT |
515 | if (!ops->init || ops->init(sch, NULL) == 0) |
516 | return sch; | |
517 | ||
0fbbeb1b | 518 | qdisc_destroy(sch); |
3d54b82f | 519 | errout: |
1da177e4 LT |
520 | return NULL; |
521 | } | |
62e3ba1b | 522 | EXPORT_SYMBOL(qdisc_create_dflt); |
1da177e4 | 523 | |
dc2b4847 | 524 | /* Under queue->lock and BH! */ |
1da177e4 LT |
525 | |
526 | void qdisc_reset(struct Qdisc *qdisc) | |
527 | { | |
20fea08b | 528 | const struct Qdisc_ops *ops = qdisc->ops; |
1da177e4 LT |
529 | |
530 | if (ops->reset) | |
531 | ops->reset(qdisc); | |
532 | } | |
62e3ba1b | 533 | EXPORT_SYMBOL(qdisc_reset); |
1da177e4 | 534 | |
10297b99 | 535 | /* this is the rcu callback function to clean up a qdisc when there |
1da177e4 LT |
536 | * are no further references to it */ |
537 | ||
538 | static void __qdisc_destroy(struct rcu_head *head) | |
539 | { | |
540 | struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu); | |
1da177e4 LT |
541 | kfree((char *) qdisc - qdisc->padded); |
542 | } | |
543 | ||
dc2b4847 | 544 | /* Under queue->lock and BH! */ |
1da177e4 LT |
545 | |
546 | void qdisc_destroy(struct Qdisc *qdisc) | |
547 | { | |
20fea08b | 548 | const struct Qdisc_ops *ops = qdisc->ops; |
1da177e4 LT |
549 | |
550 | if (qdisc->flags & TCQ_F_BUILTIN || | |
85670cc1 | 551 | !atomic_dec_and_test(&qdisc->refcnt)) |
1da177e4 LT |
552 | return; |
553 | ||
85670cc1 | 554 | list_del(&qdisc->list); |
85670cc1 | 555 | gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est); |
85670cc1 PM |
556 | if (ops->reset) |
557 | ops->reset(qdisc); | |
558 | if (ops->destroy) | |
559 | ops->destroy(qdisc); | |
1da177e4 | 560 | |
85670cc1 | 561 | module_put(ops->owner); |
5ce2d488 | 562 | dev_put(qdisc_dev(qdisc)); |
1da177e4 LT |
563 | call_rcu(&qdisc->q_rcu, __qdisc_destroy); |
564 | } | |
62e3ba1b | 565 | EXPORT_SYMBOL(qdisc_destroy); |
1da177e4 | 566 | |
e8a0464c DM |
567 | static bool dev_all_qdisc_sleeping_noop(struct net_device *dev) |
568 | { | |
569 | unsigned int i; | |
570 | ||
571 | for (i = 0; i < dev->num_tx_queues; i++) { | |
572 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
573 | ||
574 | if (txq->qdisc_sleeping != &noop_qdisc) | |
575 | return false; | |
576 | } | |
577 | return true; | |
578 | } | |
579 | ||
580 | static void attach_one_default_qdisc(struct net_device *dev, | |
581 | struct netdev_queue *dev_queue, | |
582 | void *_unused) | |
583 | { | |
584 | struct Qdisc *qdisc; | |
585 | ||
586 | if (dev->tx_queue_len) { | |
587 | qdisc = qdisc_create_dflt(dev, dev_queue, | |
588 | &pfifo_fast_ops, TC_H_ROOT); | |
589 | if (!qdisc) { | |
590 | printk(KERN_INFO "%s: activation failed\n", dev->name); | |
591 | return; | |
592 | } | |
593 | list_add_tail(&qdisc->list, &dev_queue->qdisc_list); | |
594 | } else { | |
595 | qdisc = &noqueue_qdisc; | |
596 | } | |
597 | dev_queue->qdisc_sleeping = qdisc; | |
598 | } | |
599 | ||
600 | static void transition_one_qdisc(struct net_device *dev, | |
601 | struct netdev_queue *dev_queue, | |
602 | void *_need_watchdog) | |
603 | { | |
604 | int *need_watchdog_p = _need_watchdog; | |
605 | ||
606 | spin_lock_bh(&dev_queue->lock); | |
607 | rcu_assign_pointer(dev_queue->qdisc, dev_queue->qdisc_sleeping); | |
608 | if (dev_queue->qdisc != &noqueue_qdisc) | |
609 | *need_watchdog_p = 1; | |
610 | spin_unlock_bh(&dev_queue->lock); | |
611 | } | |
612 | ||
1da177e4 LT |
613 | void dev_activate(struct net_device *dev) |
614 | { | |
e8a0464c | 615 | int need_watchdog; |
b0e1e646 | 616 | |
1da177e4 LT |
617 | /* No queueing discipline is attached to device; |
618 | create default one i.e. pfifo_fast for devices, | |
619 | which need queueing and noqueue_qdisc for | |
620 | virtual interfaces | |
621 | */ | |
622 | ||
e8a0464c DM |
623 | if (dev_all_qdisc_sleeping_noop(dev)) |
624 | netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL); | |
1da177e4 | 625 | |
cacaddf5 TC |
626 | if (!netif_carrier_ok(dev)) |
627 | /* Delay activation until next carrier-on event */ | |
628 | return; | |
629 | ||
e8a0464c DM |
630 | need_watchdog = 0; |
631 | netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog); | |
632 | ||
633 | if (need_watchdog) { | |
1da177e4 LT |
634 | dev->trans_start = jiffies; |
635 | dev_watchdog_up(dev); | |
636 | } | |
b0e1e646 DM |
637 | } |
638 | ||
e8a0464c DM |
639 | static void dev_deactivate_queue(struct net_device *dev, |
640 | struct netdev_queue *dev_queue, | |
641 | void *_qdisc_default) | |
b0e1e646 | 642 | { |
e8a0464c | 643 | struct Qdisc *qdisc_default = _qdisc_default; |
d3b753db | 644 | struct sk_buff *skb = NULL; |
970565bb | 645 | struct Qdisc *qdisc; |
970565bb DM |
646 | |
647 | spin_lock_bh(&dev_queue->lock); | |
b0e1e646 | 648 | |
970565bb | 649 | qdisc = dev_queue->qdisc; |
b0e1e646 DM |
650 | if (qdisc) { |
651 | dev_queue->qdisc = qdisc_default; | |
652 | qdisc_reset(qdisc); | |
d3b753db DM |
653 | |
654 | skb = qdisc->gso_skb; | |
655 | qdisc->gso_skb = NULL; | |
b0e1e646 | 656 | } |
970565bb DM |
657 | |
658 | spin_unlock_bh(&dev_queue->lock); | |
659 | ||
660 | kfree_skb(skb); | |
1da177e4 LT |
661 | } |
662 | ||
e8a0464c DM |
663 | static bool some_qdisc_is_running(struct net_device *dev, int lock) |
664 | { | |
665 | unsigned int i; | |
666 | ||
667 | for (i = 0; i < dev->num_tx_queues; i++) { | |
668 | struct netdev_queue *dev_queue; | |
e2627c8c | 669 | struct Qdisc *q; |
e8a0464c DM |
670 | int val; |
671 | ||
672 | dev_queue = netdev_get_tx_queue(dev, i); | |
e2627c8c | 673 | q = dev_queue->qdisc; |
e8a0464c DM |
674 | |
675 | if (lock) | |
676 | spin_lock_bh(&dev_queue->lock); | |
677 | ||
e2627c8c | 678 | val = test_bit(__QDISC_STATE_RUNNING, &q->state); |
e8a0464c DM |
679 | |
680 | if (lock) | |
681 | spin_unlock_bh(&dev_queue->lock); | |
682 | ||
683 | if (val) | |
684 | return true; | |
685 | } | |
686 | return false; | |
687 | } | |
688 | ||
1da177e4 LT |
689 | void dev_deactivate(struct net_device *dev) |
690 | { | |
e8a0464c | 691 | bool running; |
1da177e4 | 692 | |
e8a0464c | 693 | netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc); |
41a23b07 | 694 | |
1da177e4 LT |
695 | dev_watchdog_down(dev); |
696 | ||
ce0e32e6 | 697 | /* Wait for outstanding qdisc-less dev_queue_xmit calls. */ |
d4828d85 | 698 | synchronize_rcu(); |
1da177e4 | 699 | |
d4828d85 | 700 | /* Wait for outstanding qdisc_run calls. */ |
ce0e32e6 | 701 | do { |
e8a0464c | 702 | while (some_qdisc_is_running(dev, 0)) |
ce0e32e6 HX |
703 | yield(); |
704 | ||
705 | /* | |
706 | * Double-check inside queue lock to ensure that all effects | |
707 | * of the queue run are visible when we return. | |
708 | */ | |
e8a0464c | 709 | running = some_qdisc_is_running(dev, 1); |
ce0e32e6 HX |
710 | |
711 | /* | |
712 | * The running flag should never be set at this point because | |
713 | * we've already set dev->qdisc to noop_qdisc *inside* the same | |
714 | * pair of spin locks. That is, if any qdisc_run starts after | |
715 | * our initial test it should see the noop_qdisc and then | |
716 | * clear the RUNNING bit before dropping the queue lock. So | |
717 | * if it is set here then we've found a bug. | |
718 | */ | |
719 | } while (WARN_ON_ONCE(running)); | |
1da177e4 LT |
720 | } |
721 | ||
b0e1e646 DM |
722 | static void dev_init_scheduler_queue(struct net_device *dev, |
723 | struct netdev_queue *dev_queue, | |
e8a0464c | 724 | void *_qdisc) |
b0e1e646 | 725 | { |
e8a0464c DM |
726 | struct Qdisc *qdisc = _qdisc; |
727 | ||
b0e1e646 DM |
728 | dev_queue->qdisc = qdisc; |
729 | dev_queue->qdisc_sleeping = qdisc; | |
730 | INIT_LIST_HEAD(&dev_queue->qdisc_list); | |
731 | } | |
732 | ||
1da177e4 LT |
733 | void dev_init_scheduler(struct net_device *dev) |
734 | { | |
735 | qdisc_lock_tree(dev); | |
e8a0464c | 736 | netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc); |
b0e1e646 | 737 | dev_init_scheduler_queue(dev, &dev->rx_queue, NULL); |
1da177e4 LT |
738 | qdisc_unlock_tree(dev); |
739 | ||
b24b8a24 | 740 | setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev); |
1da177e4 LT |
741 | } |
742 | ||
e8a0464c DM |
743 | static void shutdown_scheduler_queue(struct net_device *dev, |
744 | struct netdev_queue *dev_queue, | |
745 | void *_qdisc_default) | |
1da177e4 | 746 | { |
b0e1e646 | 747 | struct Qdisc *qdisc = dev_queue->qdisc_sleeping; |
e8a0464c | 748 | struct Qdisc *qdisc_default = _qdisc_default; |
b0e1e646 DM |
749 | |
750 | if (qdisc) { | |
751 | dev_queue->qdisc = qdisc_default; | |
752 | dev_queue->qdisc_sleeping = qdisc_default; | |
1da177e4 | 753 | |
1da177e4 | 754 | qdisc_destroy(qdisc); |
10297b99 | 755 | } |
b0e1e646 DM |
756 | } |
757 | ||
758 | void dev_shutdown(struct net_device *dev) | |
759 | { | |
760 | qdisc_lock_tree(dev); | |
e8a0464c DM |
761 | netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc); |
762 | shutdown_scheduler_queue(dev, &dev->rx_queue, NULL); | |
1da177e4 LT |
763 | BUG_TRAP(!timer_pending(&dev->watchdog_timer)); |
764 | qdisc_unlock_tree(dev); | |
765 | } |