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