net: allow to propagate errors through ->ndo_hard_start_xmit()
[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  * qdisc_lock(qdisc) spinlock.
33  *
34  * The idea is the following:
35  * - enqueue, dequeue are serialized via qdisc root lock
36  * - ingress filtering is also serialized via qdisc root lock
37  * - updates to tree and tree walking are only done under the rtnl mutex.
38  */
39
40 static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
41 {
42         q->gso_skb = skb;
43         q->qstats.requeues++;
44         q->q.qlen++;    /* it's still part of the queue */
45         __netif_schedule(q);
46
47         return 0;
48 }
49
50 static inline struct sk_buff *dequeue_skb(struct Qdisc *q)
51 {
52         struct sk_buff *skb = q->gso_skb;
53
54         if (unlikely(skb)) {
55                 struct net_device *dev = qdisc_dev(q);
56                 struct netdev_queue *txq;
57
58                 /* check the reason of requeuing without tx lock first */
59                 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
60                 if (!netif_tx_queue_stopped(txq) &&
61                     !netif_tx_queue_frozen(txq)) {
62                         q->gso_skb = NULL;
63                         q->q.qlen--;
64                 } else
65                         skb = NULL;
66         } else {
67                 skb = q->dequeue(q);
68         }
69
70         return skb;
71 }
72
73 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
74                                            struct netdev_queue *dev_queue,
75                                            struct Qdisc *q)
76 {
77         int ret;
78
79         if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
80                 /*
81                  * Same CPU holding the lock. It may be a transient
82                  * configuration error, when hard_start_xmit() recurses. We
83                  * detect it by checking xmit owner and drop the packet when
84                  * deadloop is detected. Return OK to try the next skb.
85                  */
86                 kfree_skb(skb);
87                 if (net_ratelimit())
88                         printk(KERN_WARNING "Dead loop on netdevice %s, "
89                                "fix it urgently!\n", dev_queue->dev->name);
90                 ret = qdisc_qlen(q);
91         } else {
92                 /*
93                  * Another cpu is holding lock, requeue & delay xmits for
94                  * some time.
95                  */
96                 __get_cpu_var(netdev_rx_stat).cpu_collision++;
97                 ret = dev_requeue_skb(skb, q);
98         }
99
100         return ret;
101 }
102
103 /*
104  * Transmit one skb, and handle the return status as required. Holding the
105  * __QDISC_STATE_RUNNING bit guarantees that only one CPU can execute this
106  * function.
107  *
108  * Returns to the caller:
109  *                              0  - queue is empty or throttled.
110  *                              >0 - queue is not empty.
111  */
112 int sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
113                     struct net_device *dev, struct netdev_queue *txq,
114                     spinlock_t *root_lock)
115 {
116         int ret = NETDEV_TX_BUSY;
117
118         /* And release qdisc */
119         spin_unlock(root_lock);
120
121         HARD_TX_LOCK(dev, txq, smp_processor_id());
122         if (!netif_tx_queue_stopped(txq) &&
123             !netif_tx_queue_frozen(txq)) {
124                 ret = dev_hard_start_xmit(skb, dev, txq);
125
126                 /* an error implies that the skb was consumed */
127                 if (ret < 0)
128                         ret = NETDEV_TX_OK;
129                 /* all NET_XMIT codes map to NETDEV_TX_OK */
130                 ret &= ~NET_XMIT_MASK;
131         }
132         HARD_TX_UNLOCK(dev, txq);
133
134         spin_lock(root_lock);
135
136         switch (ret) {
137         case NETDEV_TX_OK:
138                 /* Driver sent out skb successfully */
139                 ret = qdisc_qlen(q);
140                 break;
141
142         case NETDEV_TX_LOCKED:
143                 /* Driver try lock failed */
144                 ret = handle_dev_cpu_collision(skb, txq, q);
145                 break;
146
147         default:
148                 /* Driver returned NETDEV_TX_BUSY - requeue skb */
149                 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
150                         printk(KERN_WARNING "BUG %s code %d qlen %d\n",
151                                dev->name, ret, q->q.qlen);
152
153                 ret = dev_requeue_skb(skb, q);
154                 break;
155         }
156
157         if (ret && (netif_tx_queue_stopped(txq) ||
158                     netif_tx_queue_frozen(txq)))
159                 ret = 0;
160
161         return ret;
162 }
163
164 /*
165  * NOTE: Called under qdisc_lock(q) with locally disabled BH.
166  *
167  * __QDISC_STATE_RUNNING guarantees only one CPU can process
168  * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
169  * this queue.
170  *
171  *  netif_tx_lock serializes accesses to device driver.
172  *
173  *  qdisc_lock(q) and netif_tx_lock are mutually exclusive,
174  *  if one is grabbed, another must be free.
175  *
176  * Note, that this procedure can be called by a watchdog timer
177  *
178  * Returns to the caller:
179  *                              0  - queue is empty or throttled.
180  *                              >0 - queue is not empty.
181  *
182  */
183 static inline int qdisc_restart(struct Qdisc *q)
184 {
185         struct netdev_queue *txq;
186         struct net_device *dev;
187         spinlock_t *root_lock;
188         struct sk_buff *skb;
189
190         /* Dequeue packet */
191         skb = dequeue_skb(q);
192         if (unlikely(!skb))
193                 return 0;
194
195         root_lock = qdisc_lock(q);
196         dev = qdisc_dev(q);
197         txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
198
199         return sch_direct_xmit(skb, q, dev, txq, root_lock);
200 }
201
202 void __qdisc_run(struct Qdisc *q)
203 {
204         unsigned long start_time = jiffies;
205
206         while (qdisc_restart(q)) {
207                 /*
208                  * Postpone processing if
209                  * 1. another process needs the CPU;
210                  * 2. we've been doing it for too long.
211                  */
212                 if (need_resched() || jiffies != start_time) {
213                         __netif_schedule(q);
214                         break;
215                 }
216         }
217
218         clear_bit(__QDISC_STATE_RUNNING, &q->state);
219 }
220
221 unsigned long dev_trans_start(struct net_device *dev)
222 {
223         unsigned long val, res = dev->trans_start;
224         unsigned int i;
225
226         for (i = 0; i < dev->num_tx_queues; i++) {
227                 val = netdev_get_tx_queue(dev, i)->trans_start;
228                 if (val && time_after(val, res))
229                         res = val;
230         }
231         dev->trans_start = res;
232         return res;
233 }
234 EXPORT_SYMBOL(dev_trans_start);
235
236 static void dev_watchdog(unsigned long arg)
237 {
238         struct net_device *dev = (struct net_device *)arg;
239
240         netif_tx_lock(dev);
241         if (!qdisc_tx_is_noop(dev)) {
242                 if (netif_device_present(dev) &&
243                     netif_running(dev) &&
244                     netif_carrier_ok(dev)) {
245                         int some_queue_timedout = 0;
246                         unsigned int i;
247                         unsigned long trans_start;
248
249                         for (i = 0; i < dev->num_tx_queues; i++) {
250                                 struct netdev_queue *txq;
251
252                                 txq = netdev_get_tx_queue(dev, i);
253                                 /*
254                                  * old device drivers set dev->trans_start
255                                  */
256                                 trans_start = txq->trans_start ? : dev->trans_start;
257                                 if (netif_tx_queue_stopped(txq) &&
258                                     time_after(jiffies, (trans_start +
259                                                          dev->watchdog_timeo))) {
260                                         some_queue_timedout = 1;
261                                         break;
262                                 }
263                         }
264
265                         if (some_queue_timedout) {
266                                 char drivername[64];
267                                 WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
268                                        dev->name, netdev_drivername(dev, drivername, 64), i);
269                                 dev->netdev_ops->ndo_tx_timeout(dev);
270                         }
271                         if (!mod_timer(&dev->watchdog_timer,
272                                        round_jiffies(jiffies +
273                                                      dev->watchdog_timeo)))
274                                 dev_hold(dev);
275                 }
276         }
277         netif_tx_unlock(dev);
278
279         dev_put(dev);
280 }
281
282 void __netdev_watchdog_up(struct net_device *dev)
283 {
284         if (dev->netdev_ops->ndo_tx_timeout) {
285                 if (dev->watchdog_timeo <= 0)
286                         dev->watchdog_timeo = 5*HZ;
287                 if (!mod_timer(&dev->watchdog_timer,
288                                round_jiffies(jiffies + dev->watchdog_timeo)))
289                         dev_hold(dev);
290         }
291 }
292
293 static void dev_watchdog_up(struct net_device *dev)
294 {
295         __netdev_watchdog_up(dev);
296 }
297
298 static void dev_watchdog_down(struct net_device *dev)
299 {
300         netif_tx_lock_bh(dev);
301         if (del_timer(&dev->watchdog_timer))
302                 dev_put(dev);
303         netif_tx_unlock_bh(dev);
304 }
305
306 /**
307  *      netif_carrier_on - set carrier
308  *      @dev: network device
309  *
310  * Device has detected that carrier.
311  */
312 void netif_carrier_on(struct net_device *dev)
313 {
314         if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
315                 if (dev->reg_state == NETREG_UNINITIALIZED)
316                         return;
317                 linkwatch_fire_event(dev);
318                 if (netif_running(dev))
319                         __netdev_watchdog_up(dev);
320         }
321 }
322 EXPORT_SYMBOL(netif_carrier_on);
323
324 /**
325  *      netif_carrier_off - clear carrier
326  *      @dev: network device
327  *
328  * Device has detected loss of carrier.
329  */
330 void netif_carrier_off(struct net_device *dev)
331 {
332         if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
333                 if (dev->reg_state == NETREG_UNINITIALIZED)
334                         return;
335                 linkwatch_fire_event(dev);
336         }
337 }
338 EXPORT_SYMBOL(netif_carrier_off);
339
340 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
341    under all circumstances. It is difficult to invent anything faster or
342    cheaper.
343  */
344
345 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
346 {
347         kfree_skb(skb);
348         return NET_XMIT_CN;
349 }
350
351 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
352 {
353         return NULL;
354 }
355
356 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
357         .id             =       "noop",
358         .priv_size      =       0,
359         .enqueue        =       noop_enqueue,
360         .dequeue        =       noop_dequeue,
361         .peek           =       noop_dequeue,
362         .owner          =       THIS_MODULE,
363 };
364
365 static struct netdev_queue noop_netdev_queue = {
366         .qdisc          =       &noop_qdisc,
367         .qdisc_sleeping =       &noop_qdisc,
368 };
369
370 struct Qdisc noop_qdisc = {
371         .enqueue        =       noop_enqueue,
372         .dequeue        =       noop_dequeue,
373         .flags          =       TCQ_F_BUILTIN,
374         .ops            =       &noop_qdisc_ops,
375         .list           =       LIST_HEAD_INIT(noop_qdisc.list),
376         .q.lock         =       __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
377         .dev_queue      =       &noop_netdev_queue,
378 };
379 EXPORT_SYMBOL(noop_qdisc);
380
381 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
382         .id             =       "noqueue",
383         .priv_size      =       0,
384         .enqueue        =       noop_enqueue,
385         .dequeue        =       noop_dequeue,
386         .peek           =       noop_dequeue,
387         .owner          =       THIS_MODULE,
388 };
389
390 static struct Qdisc noqueue_qdisc;
391 static struct netdev_queue noqueue_netdev_queue = {
392         .qdisc          =       &noqueue_qdisc,
393         .qdisc_sleeping =       &noqueue_qdisc,
394 };
395
396 static struct Qdisc noqueue_qdisc = {
397         .enqueue        =       NULL,
398         .dequeue        =       noop_dequeue,
399         .flags          =       TCQ_F_BUILTIN,
400         .ops            =       &noqueue_qdisc_ops,
401         .list           =       LIST_HEAD_INIT(noqueue_qdisc.list),
402         .q.lock         =       __SPIN_LOCK_UNLOCKED(noqueue_qdisc.q.lock),
403         .dev_queue      =       &noqueue_netdev_queue,
404 };
405
406
407 static const u8 prio2band[TC_PRIO_MAX+1] =
408         { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
409
410 /* 3-band FIFO queue: old style, but should be a bit faster than
411    generic prio+fifo combination.
412  */
413
414 #define PFIFO_FAST_BANDS 3
415
416 /*
417  * Private data for a pfifo_fast scheduler containing:
418  *      - queues for the three band
419  *      - bitmap indicating which of the bands contain skbs
420  */
421 struct pfifo_fast_priv {
422         u32 bitmap;
423         struct sk_buff_head q[PFIFO_FAST_BANDS];
424 };
425
426 /*
427  * Convert a bitmap to the first band number where an skb is queued, where:
428  *      bitmap=0 means there are no skbs on any band.
429  *      bitmap=1 means there is an skb on band 0.
430  *      bitmap=7 means there are skbs on all 3 bands, etc.
431  */
432 static const int bitmap2band[] = {-1, 0, 1, 0, 2, 0, 1, 0};
433
434 static inline struct sk_buff_head *band2list(struct pfifo_fast_priv *priv,
435                                              int band)
436 {
437         return priv->q + band;
438 }
439
440 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
441 {
442         if (skb_queue_len(&qdisc->q) < qdisc_dev(qdisc)->tx_queue_len) {
443                 int band = prio2band[skb->priority & TC_PRIO_MAX];
444                 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
445                 struct sk_buff_head *list = band2list(priv, band);
446
447                 priv->bitmap |= (1 << band);
448                 qdisc->q.qlen++;
449                 return __qdisc_enqueue_tail(skb, qdisc, list);
450         }
451
452         return qdisc_drop(skb, qdisc);
453 }
454
455 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
456 {
457         struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
458         int band = bitmap2band[priv->bitmap];
459
460         if (likely(band >= 0)) {
461                 struct sk_buff_head *list = band2list(priv, band);
462                 struct sk_buff *skb = __qdisc_dequeue_head(qdisc, list);
463
464                 qdisc->q.qlen--;
465                 if (skb_queue_empty(list))
466                         priv->bitmap &= ~(1 << band);
467
468                 return skb;
469         }
470
471         return NULL;
472 }
473
474 static struct sk_buff *pfifo_fast_peek(struct Qdisc* qdisc)
475 {
476         struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
477         int band = bitmap2band[priv->bitmap];
478
479         if (band >= 0) {
480                 struct sk_buff_head *list = band2list(priv, band);
481
482                 return skb_peek(list);
483         }
484
485         return NULL;
486 }
487
488 static void pfifo_fast_reset(struct Qdisc* qdisc)
489 {
490         int prio;
491         struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
492
493         for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
494                 __qdisc_reset_queue(qdisc, band2list(priv, prio));
495
496         priv->bitmap = 0;
497         qdisc->qstats.backlog = 0;
498         qdisc->q.qlen = 0;
499 }
500
501 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
502 {
503         struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
504
505         memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
506         NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
507         return skb->len;
508
509 nla_put_failure:
510         return -1;
511 }
512
513 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
514 {
515         int prio;
516         struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
517
518         for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
519                 skb_queue_head_init(band2list(priv, prio));
520
521         return 0;
522 }
523
524 struct Qdisc_ops pfifo_fast_ops __read_mostly = {
525         .id             =       "pfifo_fast",
526         .priv_size      =       sizeof(struct pfifo_fast_priv),
527         .enqueue        =       pfifo_fast_enqueue,
528         .dequeue        =       pfifo_fast_dequeue,
529         .peek           =       pfifo_fast_peek,
530         .init           =       pfifo_fast_init,
531         .reset          =       pfifo_fast_reset,
532         .dump           =       pfifo_fast_dump,
533         .owner          =       THIS_MODULE,
534 };
535
536 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
537                           struct Qdisc_ops *ops)
538 {
539         void *p;
540         struct Qdisc *sch;
541         unsigned int size;
542         int err = -ENOBUFS;
543
544         /* ensure that the Qdisc and the private data are 32-byte aligned */
545         size = QDISC_ALIGN(sizeof(*sch));
546         size += ops->priv_size + (QDISC_ALIGNTO - 1);
547
548         p = kzalloc(size, GFP_KERNEL);
549         if (!p)
550                 goto errout;
551         sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
552         sch->padded = (char *) sch - (char *) p;
553
554         INIT_LIST_HEAD(&sch->list);
555         skb_queue_head_init(&sch->q);
556         sch->ops = ops;
557         sch->enqueue = ops->enqueue;
558         sch->dequeue = ops->dequeue;
559         sch->dev_queue = dev_queue;
560         dev_hold(qdisc_dev(sch));
561         atomic_set(&sch->refcnt, 1);
562
563         return sch;
564 errout:
565         return ERR_PTR(err);
566 }
567
568 struct Qdisc * qdisc_create_dflt(struct net_device *dev,
569                                  struct netdev_queue *dev_queue,
570                                  struct Qdisc_ops *ops,
571                                  unsigned int parentid)
572 {
573         struct Qdisc *sch;
574
575         sch = qdisc_alloc(dev_queue, ops);
576         if (IS_ERR(sch))
577                 goto errout;
578         sch->parent = parentid;
579
580         if (!ops->init || ops->init(sch, NULL) == 0)
581                 return sch;
582
583         qdisc_destroy(sch);
584 errout:
585         return NULL;
586 }
587 EXPORT_SYMBOL(qdisc_create_dflt);
588
589 /* Under qdisc_lock(qdisc) and BH! */
590
591 void qdisc_reset(struct Qdisc *qdisc)
592 {
593         const struct Qdisc_ops *ops = qdisc->ops;
594
595         if (ops->reset)
596                 ops->reset(qdisc);
597
598         if (qdisc->gso_skb) {
599                 kfree_skb(qdisc->gso_skb);
600                 qdisc->gso_skb = NULL;
601                 qdisc->q.qlen = 0;
602         }
603 }
604 EXPORT_SYMBOL(qdisc_reset);
605
606 void qdisc_destroy(struct Qdisc *qdisc)
607 {
608         const struct Qdisc_ops  *ops = qdisc->ops;
609
610         if (qdisc->flags & TCQ_F_BUILTIN ||
611             !atomic_dec_and_test(&qdisc->refcnt))
612                 return;
613
614 #ifdef CONFIG_NET_SCHED
615         qdisc_list_del(qdisc);
616
617         qdisc_put_stab(qdisc->stab);
618 #endif
619         gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
620         if (ops->reset)
621                 ops->reset(qdisc);
622         if (ops->destroy)
623                 ops->destroy(qdisc);
624
625         module_put(ops->owner);
626         dev_put(qdisc_dev(qdisc));
627
628         kfree_skb(qdisc->gso_skb);
629         kfree((char *) qdisc - qdisc->padded);
630 }
631 EXPORT_SYMBOL(qdisc_destroy);
632
633 /* Attach toplevel qdisc to device queue. */
634 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
635                               struct Qdisc *qdisc)
636 {
637         struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
638         spinlock_t *root_lock;
639
640         root_lock = qdisc_lock(oqdisc);
641         spin_lock_bh(root_lock);
642
643         /* Prune old scheduler */
644         if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
645                 qdisc_reset(oqdisc);
646
647         /* ... and graft new one */
648         if (qdisc == NULL)
649                 qdisc = &noop_qdisc;
650         dev_queue->qdisc_sleeping = qdisc;
651         rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
652
653         spin_unlock_bh(root_lock);
654
655         return oqdisc;
656 }
657
658 static void attach_one_default_qdisc(struct net_device *dev,
659                                      struct netdev_queue *dev_queue,
660                                      void *_unused)
661 {
662         struct Qdisc *qdisc;
663
664         if (dev->tx_queue_len) {
665                 qdisc = qdisc_create_dflt(dev, dev_queue,
666                                           &pfifo_fast_ops, TC_H_ROOT);
667                 if (!qdisc) {
668                         printk(KERN_INFO "%s: activation failed\n", dev->name);
669                         return;
670                 }
671
672                 /* Can by-pass the queue discipline for default qdisc */
673                 qdisc->flags |= TCQ_F_CAN_BYPASS;
674         } else {
675                 qdisc =  &noqueue_qdisc;
676         }
677         dev_queue->qdisc_sleeping = qdisc;
678 }
679
680 static void attach_default_qdiscs(struct net_device *dev)
681 {
682         struct netdev_queue *txq;
683         struct Qdisc *qdisc;
684
685         txq = netdev_get_tx_queue(dev, 0);
686
687         if (!netif_is_multiqueue(dev) || dev->tx_queue_len == 0) {
688                 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
689                 dev->qdisc = txq->qdisc_sleeping;
690                 atomic_inc(&dev->qdisc->refcnt);
691         } else {
692                 qdisc = qdisc_create_dflt(dev, txq, &mq_qdisc_ops, TC_H_ROOT);
693                 if (qdisc) {
694                         qdisc->ops->attach(qdisc);
695                         dev->qdisc = qdisc;
696                 }
697         }
698 }
699
700 static void transition_one_qdisc(struct net_device *dev,
701                                  struct netdev_queue *dev_queue,
702                                  void *_need_watchdog)
703 {
704         struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
705         int *need_watchdog_p = _need_watchdog;
706
707         if (!(new_qdisc->flags & TCQ_F_BUILTIN))
708                 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
709
710         rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
711         if (need_watchdog_p && new_qdisc != &noqueue_qdisc) {
712                 dev_queue->trans_start = 0;
713                 *need_watchdog_p = 1;
714         }
715 }
716
717 void dev_activate(struct net_device *dev)
718 {
719         int need_watchdog;
720
721         /* No queueing discipline is attached to device;
722            create default one i.e. pfifo_fast for devices,
723            which need queueing and noqueue_qdisc for
724            virtual interfaces
725          */
726
727         if (dev->qdisc == &noop_qdisc)
728                 attach_default_qdiscs(dev);
729
730         if (!netif_carrier_ok(dev))
731                 /* Delay activation until next carrier-on event */
732                 return;
733
734         need_watchdog = 0;
735         netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
736         transition_one_qdisc(dev, &dev->rx_queue, NULL);
737
738         if (need_watchdog) {
739                 dev->trans_start = jiffies;
740                 dev_watchdog_up(dev);
741         }
742 }
743
744 static void dev_deactivate_queue(struct net_device *dev,
745                                  struct netdev_queue *dev_queue,
746                                  void *_qdisc_default)
747 {
748         struct Qdisc *qdisc_default = _qdisc_default;
749         struct Qdisc *qdisc;
750
751         qdisc = dev_queue->qdisc;
752         if (qdisc) {
753                 spin_lock_bh(qdisc_lock(qdisc));
754
755                 if (!(qdisc->flags & TCQ_F_BUILTIN))
756                         set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
757
758                 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
759                 qdisc_reset(qdisc);
760
761                 spin_unlock_bh(qdisc_lock(qdisc));
762         }
763 }
764
765 static bool some_qdisc_is_busy(struct net_device *dev)
766 {
767         unsigned int i;
768
769         for (i = 0; i < dev->num_tx_queues; i++) {
770                 struct netdev_queue *dev_queue;
771                 spinlock_t *root_lock;
772                 struct Qdisc *q;
773                 int val;
774
775                 dev_queue = netdev_get_tx_queue(dev, i);
776                 q = dev_queue->qdisc_sleeping;
777                 root_lock = qdisc_lock(q);
778
779                 spin_lock_bh(root_lock);
780
781                 val = (test_bit(__QDISC_STATE_RUNNING, &q->state) ||
782                        test_bit(__QDISC_STATE_SCHED, &q->state));
783
784                 spin_unlock_bh(root_lock);
785
786                 if (val)
787                         return true;
788         }
789         return false;
790 }
791
792 void dev_deactivate(struct net_device *dev)
793 {
794         netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
795         dev_deactivate_queue(dev, &dev->rx_queue, &noop_qdisc);
796
797         dev_watchdog_down(dev);
798
799         /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
800         synchronize_rcu();
801
802         /* Wait for outstanding qdisc_run calls. */
803         while (some_qdisc_is_busy(dev))
804                 yield();
805 }
806
807 static void dev_init_scheduler_queue(struct net_device *dev,
808                                      struct netdev_queue *dev_queue,
809                                      void *_qdisc)
810 {
811         struct Qdisc *qdisc = _qdisc;
812
813         dev_queue->qdisc = qdisc;
814         dev_queue->qdisc_sleeping = qdisc;
815 }
816
817 void dev_init_scheduler(struct net_device *dev)
818 {
819         dev->qdisc = &noop_qdisc;
820         netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
821         dev_init_scheduler_queue(dev, &dev->rx_queue, &noop_qdisc);
822
823         setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
824 }
825
826 static void shutdown_scheduler_queue(struct net_device *dev,
827                                      struct netdev_queue *dev_queue,
828                                      void *_qdisc_default)
829 {
830         struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
831         struct Qdisc *qdisc_default = _qdisc_default;
832
833         if (qdisc) {
834                 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
835                 dev_queue->qdisc_sleeping = qdisc_default;
836
837                 qdisc_destroy(qdisc);
838         }
839 }
840
841 void dev_shutdown(struct net_device *dev)
842 {
843         netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
844         shutdown_scheduler_queue(dev, &dev->rx_queue, &noop_qdisc);
845         qdisc_destroy(dev->qdisc);
846         dev->qdisc = &noop_qdisc;
847
848         WARN_ON(timer_pending(&dev->watchdog_timer));
849 }