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