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