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