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