treewide: replace dev->trans_start update with helper
[cascardo/linux.git] / net / sched / sch_generic.c
1 /*
2  * net/sched/sch_generic.c      Generic packet scheduler routines.
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *              Jamal Hadi Salim, <hadi@cyberus.ca> 990601
11  *              - Ingress support
12  */
13
14 #include <linux/bitops.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/init.h>
25 #include <linux/rcupdate.h>
26 #include <linux/list.h>
27 #include <linux/slab.h>
28 #include <linux/if_vlan.h>
29 #include <net/sch_generic.h>
30 #include <net/pkt_sched.h>
31 #include <net/dst.h>
32
33 /* Qdisc to use by default */
34 const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops;
35 EXPORT_SYMBOL(default_qdisc_ops);
36
37 /* Main transmission queue. */
38
39 /* Modifications to data participating in scheduling must be protected with
40  * qdisc_lock(qdisc) spinlock.
41  *
42  * The idea is the following:
43  * - enqueue, dequeue are serialized via qdisc root lock
44  * - ingress filtering is also serialized via qdisc root lock
45  * - updates to tree and tree walking are only done under the rtnl mutex.
46  */
47
48 static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
49 {
50         q->gso_skb = skb;
51         q->qstats.requeues++;
52         q->q.qlen++;    /* it's still part of the queue */
53         __netif_schedule(q);
54
55         return 0;
56 }
57
58 static void try_bulk_dequeue_skb(struct Qdisc *q,
59                                  struct sk_buff *skb,
60                                  const struct netdev_queue *txq,
61                                  int *packets)
62 {
63         int bytelimit = qdisc_avail_bulklimit(txq) - skb->len;
64
65         while (bytelimit > 0) {
66                 struct sk_buff *nskb = q->dequeue(q);
67
68                 if (!nskb)
69                         break;
70
71                 bytelimit -= nskb->len; /* covers GSO len */
72                 skb->next = nskb;
73                 skb = nskb;
74                 (*packets)++; /* GSO counts as one pkt */
75         }
76         skb->next = NULL;
77 }
78
79 /* Note that dequeue_skb can possibly return a SKB list (via skb->next).
80  * A requeued skb (via q->gso_skb) can also be a SKB list.
81  */
82 static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
83                                    int *packets)
84 {
85         struct sk_buff *skb = q->gso_skb;
86         const struct netdev_queue *txq = q->dev_queue;
87
88         *packets = 1;
89         *validate = true;
90         if (unlikely(skb)) {
91                 /* check the reason of requeuing without tx lock first */
92                 txq = skb_get_tx_queue(txq->dev, skb);
93                 if (!netif_xmit_frozen_or_stopped(txq)) {
94                         q->gso_skb = NULL;
95                         q->q.qlen--;
96                 } else
97                         skb = NULL;
98                 /* skb in gso_skb were already validated */
99                 *validate = false;
100         } else {
101                 if (!(q->flags & TCQ_F_ONETXQUEUE) ||
102                     !netif_xmit_frozen_or_stopped(txq)) {
103                         skb = q->dequeue(q);
104                         if (skb && qdisc_may_bulk(q))
105                                 try_bulk_dequeue_skb(q, skb, txq, packets);
106                 }
107         }
108         return skb;
109 }
110
111 /*
112  * Transmit possibly several skbs, and handle the return status as
113  * required. Holding the __QDISC___STATE_RUNNING bit guarantees that
114  * only one CPU can execute this function.
115  *
116  * Returns to the caller:
117  *                              0  - queue is empty or throttled.
118  *                              >0 - queue is not empty.
119  */
120 int sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
121                     struct net_device *dev, struct netdev_queue *txq,
122                     spinlock_t *root_lock, bool validate)
123 {
124         int ret = NETDEV_TX_BUSY;
125
126         /* And release qdisc */
127         spin_unlock(root_lock);
128
129         /* Note that we validate skb (GSO, checksum, ...) outside of locks */
130         if (validate)
131                 skb = validate_xmit_skb_list(skb, dev);
132
133         if (likely(skb)) {
134                 HARD_TX_LOCK(dev, txq, smp_processor_id());
135                 if (!netif_xmit_frozen_or_stopped(txq))
136                         skb = dev_hard_start_xmit(skb, dev, txq, &ret);
137
138                 HARD_TX_UNLOCK(dev, txq);
139         } else {
140                 spin_lock(root_lock);
141                 return qdisc_qlen(q);
142         }
143         spin_lock(root_lock);
144
145         if (dev_xmit_complete(ret)) {
146                 /* Driver sent out skb successfully or skb was consumed */
147                 ret = qdisc_qlen(q);
148         } else {
149                 /* Driver returned NETDEV_TX_BUSY - requeue skb */
150                 if (unlikely(ret != NETDEV_TX_BUSY))
151                         net_warn_ratelimited("BUG %s code %d qlen %d\n",
152                                              dev->name, ret, q->q.qlen);
153
154                 ret = dev_requeue_skb(skb, q);
155         }
156
157         if (ret && netif_xmit_frozen_or_stopped(txq))
158                 ret = 0;
159
160         return ret;
161 }
162
163 /*
164  * NOTE: Called under qdisc_lock(q) with locally disabled BH.
165  *
166  * __QDISC___STATE_RUNNING guarantees only one CPU can process
167  * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
168  * this queue.
169  *
170  *  netif_tx_lock serializes accesses to device driver.
171  *
172  *  qdisc_lock(q) and netif_tx_lock are mutually exclusive,
173  *  if one is grabbed, another must be free.
174  *
175  * Note, that this procedure can be called by a watchdog timer
176  *
177  * Returns to the caller:
178  *                              0  - queue is empty or throttled.
179  *                              >0 - queue is not empty.
180  *
181  */
182 static inline int qdisc_restart(struct Qdisc *q, int *packets)
183 {
184         struct netdev_queue *txq;
185         struct net_device *dev;
186         spinlock_t *root_lock;
187         struct sk_buff *skb;
188         bool validate;
189
190         /* Dequeue packet */
191         skb = dequeue_skb(q, &validate, packets);
192         if (unlikely(!skb))
193                 return 0;
194
195         root_lock = qdisc_lock(q);
196         dev = qdisc_dev(q);
197         txq = skb_get_tx_queue(dev, skb);
198
199         return sch_direct_xmit(skb, q, dev, txq, root_lock, validate);
200 }
201
202 void __qdisc_run(struct Qdisc *q)
203 {
204         int quota = weight_p;
205         int packets;
206
207         while (qdisc_restart(q, &packets)) {
208                 /*
209                  * Ordered by possible occurrence: Postpone processing if
210                  * 1. we've exceeded packet quota
211                  * 2. another process needs the CPU;
212                  */
213                 quota -= packets;
214                 if (quota <= 0 || need_resched()) {
215                         __netif_schedule(q);
216                         break;
217                 }
218         }
219
220         qdisc_run_end(q);
221 }
222
223 unsigned long dev_trans_start(struct net_device *dev)
224 {
225         unsigned long val, res;
226         unsigned int i;
227
228         if (is_vlan_dev(dev))
229                 dev = vlan_dev_real_dev(dev);
230         res = dev->trans_start;
231         for (i = 0; i < dev->num_tx_queues; i++) {
232                 val = netdev_get_tx_queue(dev, i)->trans_start;
233                 if (val && time_after(val, res))
234                         res = val;
235         }
236         dev->trans_start = res;
237
238         return res;
239 }
240 EXPORT_SYMBOL(dev_trans_start);
241
242 static void dev_watchdog(unsigned long arg)
243 {
244         struct net_device *dev = (struct net_device *)arg;
245
246         netif_tx_lock(dev);
247         if (!qdisc_tx_is_noop(dev)) {
248                 if (netif_device_present(dev) &&
249                     netif_running(dev) &&
250                     netif_carrier_ok(dev)) {
251                         int some_queue_timedout = 0;
252                         unsigned int i;
253                         unsigned long trans_start;
254
255                         for (i = 0; i < dev->num_tx_queues; i++) {
256                                 struct netdev_queue *txq;
257
258                                 txq = netdev_get_tx_queue(dev, i);
259                                 /*
260                                  * old device drivers set dev->trans_start
261                                  */
262                                 trans_start = txq->trans_start ? : dev->trans_start;
263                                 if (netif_xmit_stopped(txq) &&
264                                     time_after(jiffies, (trans_start +
265                                                          dev->watchdog_timeo))) {
266                                         some_queue_timedout = 1;
267                                         txq->trans_timeout++;
268                                         break;
269                                 }
270                         }
271
272                         if (some_queue_timedout) {
273                                 WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
274                                        dev->name, netdev_drivername(dev), i);
275                                 dev->netdev_ops->ndo_tx_timeout(dev);
276                         }
277                         if (!mod_timer(&dev->watchdog_timer,
278                                        round_jiffies(jiffies +
279                                                      dev->watchdog_timeo)))
280                                 dev_hold(dev);
281                 }
282         }
283         netif_tx_unlock(dev);
284
285         dev_put(dev);
286 }
287
288 void __netdev_watchdog_up(struct net_device *dev)
289 {
290         if (dev->netdev_ops->ndo_tx_timeout) {
291                 if (dev->watchdog_timeo <= 0)
292                         dev->watchdog_timeo = 5*HZ;
293                 if (!mod_timer(&dev->watchdog_timer,
294                                round_jiffies(jiffies + dev->watchdog_timeo)))
295                         dev_hold(dev);
296         }
297 }
298
299 static void dev_watchdog_up(struct net_device *dev)
300 {
301         __netdev_watchdog_up(dev);
302 }
303
304 static void dev_watchdog_down(struct net_device *dev)
305 {
306         netif_tx_lock_bh(dev);
307         if (del_timer(&dev->watchdog_timer))
308                 dev_put(dev);
309         netif_tx_unlock_bh(dev);
310 }
311
312 /**
313  *      netif_carrier_on - set carrier
314  *      @dev: network device
315  *
316  * Device has detected that carrier.
317  */
318 void netif_carrier_on(struct net_device *dev)
319 {
320         if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
321                 if (dev->reg_state == NETREG_UNINITIALIZED)
322                         return;
323                 atomic_inc(&dev->carrier_changes);
324                 linkwatch_fire_event(dev);
325                 if (netif_running(dev))
326                         __netdev_watchdog_up(dev);
327         }
328 }
329 EXPORT_SYMBOL(netif_carrier_on);
330
331 /**
332  *      netif_carrier_off - clear carrier
333  *      @dev: network device
334  *
335  * Device has detected loss of carrier.
336  */
337 void netif_carrier_off(struct net_device *dev)
338 {
339         if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
340                 if (dev->reg_state == NETREG_UNINITIALIZED)
341                         return;
342                 atomic_inc(&dev->carrier_changes);
343                 linkwatch_fire_event(dev);
344         }
345 }
346 EXPORT_SYMBOL(netif_carrier_off);
347
348 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
349    under all circumstances. It is difficult to invent anything faster or
350    cheaper.
351  */
352
353 static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc)
354 {
355         kfree_skb(skb);
356         return NET_XMIT_CN;
357 }
358
359 static struct sk_buff *noop_dequeue(struct Qdisc *qdisc)
360 {
361         return NULL;
362 }
363
364 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
365         .id             =       "noop",
366         .priv_size      =       0,
367         .enqueue        =       noop_enqueue,
368         .dequeue        =       noop_dequeue,
369         .peek           =       noop_dequeue,
370         .owner          =       THIS_MODULE,
371 };
372
373 static struct netdev_queue noop_netdev_queue = {
374         .qdisc          =       &noop_qdisc,
375         .qdisc_sleeping =       &noop_qdisc,
376 };
377
378 struct Qdisc noop_qdisc = {
379         .enqueue        =       noop_enqueue,
380         .dequeue        =       noop_dequeue,
381         .flags          =       TCQ_F_BUILTIN,
382         .ops            =       &noop_qdisc_ops,
383         .list           =       LIST_HEAD_INIT(noop_qdisc.list),
384         .q.lock         =       __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
385         .dev_queue      =       &noop_netdev_queue,
386         .busylock       =       __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock),
387 };
388 EXPORT_SYMBOL(noop_qdisc);
389
390 static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt)
391 {
392         /* register_qdisc() assigns a default of noop_enqueue if unset,
393          * but __dev_queue_xmit() treats noqueue only as such
394          * if this is NULL - so clear it here. */
395         qdisc->enqueue = NULL;
396         return 0;
397 }
398
399 struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
400         .id             =       "noqueue",
401         .priv_size      =       0,
402         .init           =       noqueue_init,
403         .enqueue        =       noop_enqueue,
404         .dequeue        =       noop_dequeue,
405         .peek           =       noop_dequeue,
406         .owner          =       THIS_MODULE,
407 };
408
409 static const u8 prio2band[TC_PRIO_MAX + 1] = {
410         1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
411 };
412
413 /* 3-band FIFO queue: old style, but should be a bit faster than
414    generic prio+fifo combination.
415  */
416
417 #define PFIFO_FAST_BANDS 3
418
419 /*
420  * Private data for a pfifo_fast scheduler containing:
421  *      - queues for the three band
422  *      - bitmap indicating which of the bands contain skbs
423  */
424 struct pfifo_fast_priv {
425         u32 bitmap;
426         struct sk_buff_head q[PFIFO_FAST_BANDS];
427 };
428
429 /*
430  * Convert a bitmap to the first band number where an skb is queued, where:
431  *      bitmap=0 means there are no skbs on any band.
432  *      bitmap=1 means there is an skb on band 0.
433  *      bitmap=7 means there are skbs on all 3 bands, etc.
434  */
435 static const int bitmap2band[] = {-1, 0, 1, 0, 2, 0, 1, 0};
436
437 static inline struct sk_buff_head *band2list(struct pfifo_fast_priv *priv,
438                                              int band)
439 {
440         return priv->q + band;
441 }
442
443 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc)
444 {
445         if (skb_queue_len(&qdisc->q) < qdisc_dev(qdisc)->tx_queue_len) {
446                 int band = prio2band[skb->priority & TC_PRIO_MAX];
447                 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
448                 struct sk_buff_head *list = band2list(priv, band);
449
450                 priv->bitmap |= (1 << band);
451                 qdisc->q.qlen++;
452                 return __qdisc_enqueue_tail(skb, qdisc, list);
453         }
454
455         return qdisc_drop(skb, qdisc);
456 }
457
458 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
459 {
460         struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
461         int band = bitmap2band[priv->bitmap];
462
463         if (likely(band >= 0)) {
464                 struct sk_buff_head *list = band2list(priv, band);
465                 struct sk_buff *skb = __qdisc_dequeue_head(qdisc, list);
466
467                 qdisc->q.qlen--;
468                 if (skb_queue_empty(list))
469                         priv->bitmap &= ~(1 << band);
470
471                 return skb;
472         }
473
474         return NULL;
475 }
476
477 static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
478 {
479         struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
480         int band = bitmap2band[priv->bitmap];
481
482         if (band >= 0) {
483                 struct sk_buff_head *list = band2list(priv, band);
484
485                 return skb_peek(list);
486         }
487
488         return NULL;
489 }
490
491 static void pfifo_fast_reset(struct Qdisc *qdisc)
492 {
493         int prio;
494         struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
495
496         for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
497                 __qdisc_reset_queue(qdisc, band2list(priv, prio));
498
499         priv->bitmap = 0;
500         qdisc->qstats.backlog = 0;
501         qdisc->q.qlen = 0;
502 }
503
504 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
505 {
506         struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
507
508         memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1);
509         if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
510                 goto nla_put_failure;
511         return skb->len;
512
513 nla_put_failure:
514         return -1;
515 }
516
517 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
518 {
519         int prio;
520         struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
521
522         for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
523                 __skb_queue_head_init(band2list(priv, prio));
524
525         /* Can by-pass the queue discipline */
526         qdisc->flags |= TCQ_F_CAN_BYPASS;
527         return 0;
528 }
529
530 struct Qdisc_ops pfifo_fast_ops __read_mostly = {
531         .id             =       "pfifo_fast",
532         .priv_size      =       sizeof(struct pfifo_fast_priv),
533         .enqueue        =       pfifo_fast_enqueue,
534         .dequeue        =       pfifo_fast_dequeue,
535         .peek           =       pfifo_fast_peek,
536         .init           =       pfifo_fast_init,
537         .reset          =       pfifo_fast_reset,
538         .dump           =       pfifo_fast_dump,
539         .owner          =       THIS_MODULE,
540 };
541 EXPORT_SYMBOL(pfifo_fast_ops);
542
543 static struct lock_class_key qdisc_tx_busylock;
544
545 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
546                           const struct Qdisc_ops *ops)
547 {
548         void *p;
549         struct Qdisc *sch;
550         unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size;
551         int err = -ENOBUFS;
552         struct net_device *dev = dev_queue->dev;
553
554         p = kzalloc_node(size, GFP_KERNEL,
555                          netdev_queue_numa_node_read(dev_queue));
556
557         if (!p)
558                 goto errout;
559         sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
560         /* if we got non aligned memory, ask more and do alignment ourself */
561         if (sch != p) {
562                 kfree(p);
563                 p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL,
564                                  netdev_queue_numa_node_read(dev_queue));
565                 if (!p)
566                         goto errout;
567                 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
568                 sch->padded = (char *) sch - (char *) p;
569         }
570         INIT_LIST_HEAD(&sch->list);
571         skb_queue_head_init(&sch->q);
572
573         spin_lock_init(&sch->busylock);
574         lockdep_set_class(&sch->busylock,
575                           dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
576
577         sch->ops = ops;
578         sch->enqueue = ops->enqueue;
579         sch->dequeue = ops->dequeue;
580         sch->dev_queue = dev_queue;
581         dev_hold(dev);
582         atomic_set(&sch->refcnt, 1);
583
584         return sch;
585 errout:
586         return ERR_PTR(err);
587 }
588
589 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
590                                 const struct Qdisc_ops *ops,
591                                 unsigned int parentid)
592 {
593         struct Qdisc *sch;
594
595         if (!try_module_get(ops->owner))
596                 goto errout;
597
598         sch = qdisc_alloc(dev_queue, ops);
599         if (IS_ERR(sch))
600                 goto errout;
601         sch->parent = parentid;
602
603         if (!ops->init || ops->init(sch, NULL) == 0)
604                 return sch;
605
606         qdisc_destroy(sch);
607 errout:
608         return NULL;
609 }
610 EXPORT_SYMBOL(qdisc_create_dflt);
611
612 /* Under qdisc_lock(qdisc) and BH! */
613
614 void qdisc_reset(struct Qdisc *qdisc)
615 {
616         const struct Qdisc_ops *ops = qdisc->ops;
617
618         if (ops->reset)
619                 ops->reset(qdisc);
620
621         if (qdisc->gso_skb) {
622                 kfree_skb_list(qdisc->gso_skb);
623                 qdisc->gso_skb = NULL;
624                 qdisc->q.qlen = 0;
625         }
626 }
627 EXPORT_SYMBOL(qdisc_reset);
628
629 static void qdisc_rcu_free(struct rcu_head *head)
630 {
631         struct Qdisc *qdisc = container_of(head, struct Qdisc, rcu_head);
632
633         if (qdisc_is_percpu_stats(qdisc)) {
634                 free_percpu(qdisc->cpu_bstats);
635                 free_percpu(qdisc->cpu_qstats);
636         }
637
638         kfree((char *) qdisc - qdisc->padded);
639 }
640
641 void qdisc_destroy(struct Qdisc *qdisc)
642 {
643         const struct Qdisc_ops  *ops = qdisc->ops;
644
645         if (qdisc->flags & TCQ_F_BUILTIN ||
646             !atomic_dec_and_test(&qdisc->refcnt))
647                 return;
648
649 #ifdef CONFIG_NET_SCHED
650         qdisc_list_del(qdisc);
651
652         qdisc_put_stab(rtnl_dereference(qdisc->stab));
653 #endif
654         gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
655         if (ops->reset)
656                 ops->reset(qdisc);
657         if (ops->destroy)
658                 ops->destroy(qdisc);
659
660         module_put(ops->owner);
661         dev_put(qdisc_dev(qdisc));
662
663         kfree_skb_list(qdisc->gso_skb);
664         /*
665          * gen_estimator est_timer() might access qdisc->q.lock,
666          * wait a RCU grace period before freeing qdisc.
667          */
668         call_rcu(&qdisc->rcu_head, qdisc_rcu_free);
669 }
670 EXPORT_SYMBOL(qdisc_destroy);
671
672 /* Attach toplevel qdisc to device queue. */
673 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
674                               struct Qdisc *qdisc)
675 {
676         struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
677         spinlock_t *root_lock;
678
679         root_lock = qdisc_lock(oqdisc);
680         spin_lock_bh(root_lock);
681
682         /* Prune old scheduler */
683         if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
684                 qdisc_reset(oqdisc);
685
686         /* ... and graft new one */
687         if (qdisc == NULL)
688                 qdisc = &noop_qdisc;
689         dev_queue->qdisc_sleeping = qdisc;
690         rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
691
692         spin_unlock_bh(root_lock);
693
694         return oqdisc;
695 }
696 EXPORT_SYMBOL(dev_graft_qdisc);
697
698 static void attach_one_default_qdisc(struct net_device *dev,
699                                      struct netdev_queue *dev_queue,
700                                      void *_unused)
701 {
702         struct Qdisc *qdisc;
703         const struct Qdisc_ops *ops = default_qdisc_ops;
704
705         if (dev->priv_flags & IFF_NO_QUEUE)
706                 ops = &noqueue_qdisc_ops;
707
708         qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT);
709         if (!qdisc) {
710                 netdev_info(dev, "activation failed\n");
711                 return;
712         }
713         if (!netif_is_multiqueue(dev))
714                 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
715         dev_queue->qdisc_sleeping = qdisc;
716 }
717
718 static void attach_default_qdiscs(struct net_device *dev)
719 {
720         struct netdev_queue *txq;
721         struct Qdisc *qdisc;
722
723         txq = netdev_get_tx_queue(dev, 0);
724
725         if (!netif_is_multiqueue(dev) ||
726             dev->priv_flags & IFF_NO_QUEUE) {
727                 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
728                 dev->qdisc = txq->qdisc_sleeping;
729                 atomic_inc(&dev->qdisc->refcnt);
730         } else {
731                 qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT);
732                 if (qdisc) {
733                         dev->qdisc = qdisc;
734                         qdisc->ops->attach(qdisc);
735                 }
736         }
737 }
738
739 static void transition_one_qdisc(struct net_device *dev,
740                                  struct netdev_queue *dev_queue,
741                                  void *_need_watchdog)
742 {
743         struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
744         int *need_watchdog_p = _need_watchdog;
745
746         if (!(new_qdisc->flags & TCQ_F_BUILTIN))
747                 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
748
749         rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
750         if (need_watchdog_p) {
751                 dev_queue->trans_start = 0;
752                 *need_watchdog_p = 1;
753         }
754 }
755
756 void dev_activate(struct net_device *dev)
757 {
758         int need_watchdog;
759
760         /* No queueing discipline is attached to device;
761          * create default one for devices, which need queueing
762          * and noqueue_qdisc for virtual interfaces
763          */
764
765         if (dev->qdisc == &noop_qdisc)
766                 attach_default_qdiscs(dev);
767
768         if (!netif_carrier_ok(dev))
769                 /* Delay activation until next carrier-on event */
770                 return;
771
772         need_watchdog = 0;
773         netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
774         if (dev_ingress_queue(dev))
775                 transition_one_qdisc(dev, dev_ingress_queue(dev), NULL);
776
777         if (need_watchdog) {
778                 netif_trans_update(dev);
779                 dev_watchdog_up(dev);
780         }
781 }
782 EXPORT_SYMBOL(dev_activate);
783
784 static void dev_deactivate_queue(struct net_device *dev,
785                                  struct netdev_queue *dev_queue,
786                                  void *_qdisc_default)
787 {
788         struct Qdisc *qdisc_default = _qdisc_default;
789         struct Qdisc *qdisc;
790
791         qdisc = rtnl_dereference(dev_queue->qdisc);
792         if (qdisc) {
793                 spin_lock_bh(qdisc_lock(qdisc));
794
795                 if (!(qdisc->flags & TCQ_F_BUILTIN))
796                         set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
797
798                 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
799                 qdisc_reset(qdisc);
800
801                 spin_unlock_bh(qdisc_lock(qdisc));
802         }
803 }
804
805 static bool some_qdisc_is_busy(struct net_device *dev)
806 {
807         unsigned int i;
808
809         for (i = 0; i < dev->num_tx_queues; i++) {
810                 struct netdev_queue *dev_queue;
811                 spinlock_t *root_lock;
812                 struct Qdisc *q;
813                 int val;
814
815                 dev_queue = netdev_get_tx_queue(dev, i);
816                 q = dev_queue->qdisc_sleeping;
817                 root_lock = qdisc_lock(q);
818
819                 spin_lock_bh(root_lock);
820
821                 val = (qdisc_is_running(q) ||
822                        test_bit(__QDISC_STATE_SCHED, &q->state));
823
824                 spin_unlock_bh(root_lock);
825
826                 if (val)
827                         return true;
828         }
829         return false;
830 }
831
832 /**
833  *      dev_deactivate_many - deactivate transmissions on several devices
834  *      @head: list of devices to deactivate
835  *
836  *      This function returns only when all outstanding transmissions
837  *      have completed, unless all devices are in dismantle phase.
838  */
839 void dev_deactivate_many(struct list_head *head)
840 {
841         struct net_device *dev;
842         bool sync_needed = false;
843
844         list_for_each_entry(dev, head, close_list) {
845                 netdev_for_each_tx_queue(dev, dev_deactivate_queue,
846                                          &noop_qdisc);
847                 if (dev_ingress_queue(dev))
848                         dev_deactivate_queue(dev, dev_ingress_queue(dev),
849                                              &noop_qdisc);
850
851                 dev_watchdog_down(dev);
852                 sync_needed |= !dev->dismantle;
853         }
854
855         /* Wait for outstanding qdisc-less dev_queue_xmit calls.
856          * This is avoided if all devices are in dismantle phase :
857          * Caller will call synchronize_net() for us
858          */
859         if (sync_needed)
860                 synchronize_net();
861
862         /* Wait for outstanding qdisc_run calls. */
863         list_for_each_entry(dev, head, close_list)
864                 while (some_qdisc_is_busy(dev))
865                         yield();
866 }
867
868 void dev_deactivate(struct net_device *dev)
869 {
870         LIST_HEAD(single);
871
872         list_add(&dev->close_list, &single);
873         dev_deactivate_many(&single);
874         list_del(&single);
875 }
876 EXPORT_SYMBOL(dev_deactivate);
877
878 static void dev_init_scheduler_queue(struct net_device *dev,
879                                      struct netdev_queue *dev_queue,
880                                      void *_qdisc)
881 {
882         struct Qdisc *qdisc = _qdisc;
883
884         rcu_assign_pointer(dev_queue->qdisc, qdisc);
885         dev_queue->qdisc_sleeping = qdisc;
886 }
887
888 void dev_init_scheduler(struct net_device *dev)
889 {
890         dev->qdisc = &noop_qdisc;
891         netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
892         if (dev_ingress_queue(dev))
893                 dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
894
895         setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
896 }
897
898 static void shutdown_scheduler_queue(struct net_device *dev,
899                                      struct netdev_queue *dev_queue,
900                                      void *_qdisc_default)
901 {
902         struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
903         struct Qdisc *qdisc_default = _qdisc_default;
904
905         if (qdisc) {
906                 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
907                 dev_queue->qdisc_sleeping = qdisc_default;
908
909                 qdisc_destroy(qdisc);
910         }
911 }
912
913 void dev_shutdown(struct net_device *dev)
914 {
915         netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
916         if (dev_ingress_queue(dev))
917                 shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
918         qdisc_destroy(dev->qdisc);
919         dev->qdisc = &noop_qdisc;
920
921         WARN_ON(timer_pending(&dev->watchdog_timer));
922 }
923
924 void psched_ratecfg_precompute(struct psched_ratecfg *r,
925                                const struct tc_ratespec *conf,
926                                u64 rate64)
927 {
928         memset(r, 0, sizeof(*r));
929         r->overhead = conf->overhead;
930         r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
931         r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
932         r->mult = 1;
933         /*
934          * The deal here is to replace a divide by a reciprocal one
935          * in fast path (a reciprocal divide is a multiply and a shift)
936          *
937          * Normal formula would be :
938          *  time_in_ns = (NSEC_PER_SEC * len) / rate_bps
939          *
940          * We compute mult/shift to use instead :
941          *  time_in_ns = (len * mult) >> shift;
942          *
943          * We try to get the highest possible mult value for accuracy,
944          * but have to make sure no overflows will ever happen.
945          */
946         if (r->rate_bytes_ps > 0) {
947                 u64 factor = NSEC_PER_SEC;
948
949                 for (;;) {
950                         r->mult = div64_u64(factor, r->rate_bytes_ps);
951                         if (r->mult & (1U << 31) || factor & (1ULL << 63))
952                                 break;
953                         factor <<= 1;
954                         r->shift++;
955                 }
956         }
957 }
958 EXPORT_SYMBOL(psched_ratecfg_precompute);