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