2 * net/sched/sch_generic.c Generic packet scheduler routines.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * Jamal Hadi Salim, <hadi@cyberus.ca> 990601
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>
33 /* Qdisc to use by default */
34 const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops;
35 EXPORT_SYMBOL(default_qdisc_ops);
37 /* Main transmission queue. */
39 /* Modifications to data participating in scheduling must be protected with
40 * qdisc_lock(qdisc) spinlock.
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.
48 static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
52 qdisc_qstats_backlog_inc(q, skb);
53 q->q.qlen++; /* it's still part of the queue */
59 static void try_bulk_dequeue_skb(struct Qdisc *q,
61 const struct netdev_queue *txq,
64 int bytelimit = qdisc_avail_bulklimit(txq) - skb->len;
66 while (bytelimit > 0) {
67 struct sk_buff *nskb = q->dequeue(q);
72 bytelimit -= nskb->len; /* covers GSO len */
75 (*packets)++; /* GSO counts as one pkt */
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.
83 static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
86 struct sk_buff *skb = q->gso_skb;
87 const struct netdev_queue *txq = q->dev_queue;
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)) {
96 qdisc_qstats_backlog_dec(q, skb);
100 /* skb in gso_skb were already validated */
103 if (!(q->flags & TCQ_F_ONETXQUEUE) ||
104 !netif_xmit_frozen_or_stopped(txq)) {
106 if (skb && qdisc_may_bulk(q))
107 try_bulk_dequeue_skb(q, skb, txq, packets);
113 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
114 struct netdev_queue *dev_queue,
119 if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
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.
127 net_warn_ratelimited("Dead loop on netdevice %s, fix it urgently!\n",
128 dev_queue->dev->name);
132 * Another cpu is holding lock, requeue & delay xmits for
135 __this_cpu_inc(softnet_data.cpu_collision);
136 ret = dev_requeue_skb(skb, q);
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.
147 * Returns to the caller:
148 * 0 - queue is empty or throttled.
149 * >0 - queue is not empty.
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)
155 int ret = NETDEV_TX_BUSY;
157 /* And release qdisc */
158 spin_unlock(root_lock);
160 /* Note that we validate skb (GSO, checksum, ...) outside of locks */
162 skb = validate_xmit_skb_list(skb, dev);
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);
169 HARD_TX_UNLOCK(dev, txq);
171 spin_lock(root_lock);
172 return qdisc_qlen(q);
174 spin_lock(root_lock);
176 if (dev_xmit_complete(ret)) {
177 /* Driver sent out skb successfully or skb was consumed */
179 } else if (ret == NETDEV_TX_LOCKED) {
180 /* Driver try lock failed */
181 ret = handle_dev_cpu_collision(skb, txq, q);
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);
188 ret = dev_requeue_skb(skb, q);
191 if (ret && netif_xmit_frozen_or_stopped(txq))
198 * NOTE: Called under qdisc_lock(q) with locally disabled BH.
200 * __QDISC___STATE_RUNNING guarantees only one CPU can process
201 * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
204 * netif_tx_lock serializes accesses to device driver.
206 * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
207 * if one is grabbed, another must be free.
209 * Note, that this procedure can be called by a watchdog timer
211 * Returns to the caller:
212 * 0 - queue is empty or throttled.
213 * >0 - queue is not empty.
216 static inline int qdisc_restart(struct Qdisc *q, int *packets)
218 struct netdev_queue *txq;
219 struct net_device *dev;
220 spinlock_t *root_lock;
225 skb = dequeue_skb(q, &validate, packets);
229 root_lock = qdisc_lock(q);
231 txq = skb_get_tx_queue(dev, skb);
233 return sch_direct_xmit(skb, q, dev, txq, root_lock, validate);
236 void __qdisc_run(struct Qdisc *q)
238 int quota = weight_p;
241 while (qdisc_restart(q, &packets)) {
243 * Ordered by possible occurrence: Postpone processing if
244 * 1. we've exceeded packet quota
245 * 2. another process needs the CPU;
248 if (quota <= 0 || need_resched()) {
257 unsigned long dev_trans_start(struct net_device *dev)
259 unsigned long val, res;
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))
270 dev->trans_start = res;
274 EXPORT_SYMBOL(dev_trans_start);
276 static void dev_watchdog(unsigned long arg)
278 struct net_device *dev = (struct net_device *)arg;
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;
287 unsigned long trans_start;
289 for (i = 0; i < dev->num_tx_queues; i++) {
290 struct netdev_queue *txq;
292 txq = netdev_get_tx_queue(dev, i);
294 * old device drivers set dev->trans_start
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++;
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);
311 if (!mod_timer(&dev->watchdog_timer,
312 round_jiffies(jiffies +
313 dev->watchdog_timeo)))
317 netif_tx_unlock(dev);
322 void __netdev_watchdog_up(struct net_device *dev)
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)))
332 EXPORT_SYMBOL_GPL(__netdev_watchdog_up);
334 static void dev_watchdog_up(struct net_device *dev)
336 __netdev_watchdog_up(dev);
339 static void dev_watchdog_down(struct net_device *dev)
341 netif_tx_lock_bh(dev);
342 if (del_timer(&dev->watchdog_timer))
344 netif_tx_unlock_bh(dev);
348 * netif_carrier_on - set carrier
349 * @dev: network device
351 * Device has detected that carrier.
353 void netif_carrier_on(struct net_device *dev)
355 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
356 if (dev->reg_state == NETREG_UNINITIALIZED)
358 atomic_inc(&dev->carrier_changes);
359 linkwatch_fire_event(dev);
360 if (netif_running(dev))
361 __netdev_watchdog_up(dev);
364 EXPORT_SYMBOL(netif_carrier_on);
367 * netif_carrier_off - clear carrier
368 * @dev: network device
370 * Device has detected loss of carrier.
372 void netif_carrier_off(struct net_device *dev)
374 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
375 if (dev->reg_state == NETREG_UNINITIALIZED)
377 atomic_inc(&dev->carrier_changes);
378 linkwatch_fire_event(dev);
381 EXPORT_SYMBOL(netif_carrier_off);
383 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
384 under all circumstances. It is difficult to invent anything faster or
388 static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc)
394 static struct sk_buff *noop_dequeue(struct Qdisc *qdisc)
399 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
402 .enqueue = noop_enqueue,
403 .dequeue = noop_dequeue,
404 .peek = noop_dequeue,
405 .owner = THIS_MODULE,
408 static struct netdev_queue noop_netdev_queue = {
409 .qdisc = &noop_qdisc,
410 .qdisc_sleeping = &noop_qdisc,
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),
423 EXPORT_SYMBOL(noop_qdisc);
425 static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt)
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;
434 struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
437 .init = noqueue_init,
438 .enqueue = noop_enqueue,
439 .dequeue = noop_dequeue,
440 .peek = noop_dequeue,
441 .owner = THIS_MODULE,
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
448 /* 3-band FIFO queue: old style, but should be a bit faster than
449 generic prio+fifo combination.
452 #define PFIFO_FAST_BANDS 3
455 * Private data for a pfifo_fast scheduler containing:
456 * - queues for the three band
457 * - bitmap indicating which of the bands contain skbs
459 struct pfifo_fast_priv {
461 struct sk_buff_head q[PFIFO_FAST_BANDS];
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.
470 static const int bitmap2band[] = {-1, 0, 1, 0, 2, 0, 1, 0};
472 static inline struct sk_buff_head *band2list(struct pfifo_fast_priv *priv,
475 return priv->q + band;
478 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc)
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);
485 priv->bitmap |= (1 << band);
487 return __qdisc_enqueue_tail(skb, qdisc, list);
490 return qdisc_drop(skb, qdisc);
493 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
495 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
496 int band = bitmap2band[priv->bitmap];
498 if (likely(band >= 0)) {
499 struct sk_buff_head *list = band2list(priv, band);
500 struct sk_buff *skb = __qdisc_dequeue_head(qdisc, list);
503 if (skb_queue_empty(list))
504 priv->bitmap &= ~(1 << band);
512 static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
514 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
515 int band = bitmap2band[priv->bitmap];
518 struct sk_buff_head *list = band2list(priv, band);
520 return skb_peek(list);
526 static void pfifo_fast_reset(struct Qdisc *qdisc)
529 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
531 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
532 __qdisc_reset_queue(qdisc, band2list(priv, prio));
535 qdisc->qstats.backlog = 0;
539 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
541 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
543 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1);
544 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
545 goto nla_put_failure;
552 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
555 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
557 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
558 __skb_queue_head_init(band2list(priv, prio));
560 /* Can by-pass the queue discipline */
561 qdisc->flags |= TCQ_F_CAN_BYPASS;
565 struct Qdisc_ops pfifo_fast_ops __read_mostly = {
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,
577 static struct lock_class_key qdisc_tx_busylock;
579 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
580 const struct Qdisc_ops *ops)
584 unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size;
586 struct net_device *dev = dev_queue->dev;
588 p = kzalloc_node(size, GFP_KERNEL,
589 netdev_queue_numa_node_read(dev_queue));
593 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
594 /* if we got non aligned memory, ask more and do alignment ourself */
597 p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL,
598 netdev_queue_numa_node_read(dev_queue));
601 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
602 sch->padded = (char *) sch - (char *) p;
604 INIT_LIST_HEAD(&sch->list);
605 skb_queue_head_init(&sch->q);
607 spin_lock_init(&sch->busylock);
608 lockdep_set_class(&sch->busylock,
609 dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
612 sch->enqueue = ops->enqueue;
613 sch->dequeue = ops->dequeue;
614 sch->dev_queue = dev_queue;
616 atomic_set(&sch->refcnt, 1);
623 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
624 const struct Qdisc_ops *ops,
625 unsigned int parentid)
629 if (!try_module_get(ops->owner))
632 sch = qdisc_alloc(dev_queue, ops);
634 module_put(ops->owner);
637 sch->parent = parentid;
639 if (!ops->init || ops->init(sch, NULL) == 0)
645 EXPORT_SYMBOL(qdisc_create_dflt);
647 /* Under qdisc_lock(qdisc) and BH! */
649 void qdisc_reset(struct Qdisc *qdisc)
651 const struct Qdisc_ops *ops = qdisc->ops;
656 if (qdisc->gso_skb) {
657 kfree_skb_list(qdisc->gso_skb);
658 qdisc->gso_skb = NULL;
662 EXPORT_SYMBOL(qdisc_reset);
664 static void qdisc_rcu_free(struct rcu_head *head)
666 struct Qdisc *qdisc = container_of(head, struct Qdisc, rcu_head);
668 if (qdisc_is_percpu_stats(qdisc)) {
669 free_percpu(qdisc->cpu_bstats);
670 free_percpu(qdisc->cpu_qstats);
673 kfree((char *) qdisc - qdisc->padded);
676 void qdisc_destroy(struct Qdisc *qdisc)
678 const struct Qdisc_ops *ops;
684 if (qdisc->flags & TCQ_F_BUILTIN ||
685 !atomic_dec_and_test(&qdisc->refcnt))
688 #ifdef CONFIG_NET_SCHED
689 qdisc_list_del(qdisc);
691 qdisc_put_stab(rtnl_dereference(qdisc->stab));
693 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
699 module_put(ops->owner);
700 dev_put(qdisc_dev(qdisc));
702 kfree_skb_list(qdisc->gso_skb);
704 * gen_estimator est_timer() might access qdisc->q.lock,
705 * wait a RCU grace period before freeing qdisc.
707 call_rcu(&qdisc->rcu_head, qdisc_rcu_free);
709 EXPORT_SYMBOL(qdisc_destroy);
711 /* Attach toplevel qdisc to device queue. */
712 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
715 struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
716 spinlock_t *root_lock;
718 root_lock = qdisc_lock(oqdisc);
719 spin_lock_bh(root_lock);
721 /* Prune old scheduler */
722 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
725 /* ... and graft new one */
728 dev_queue->qdisc_sleeping = qdisc;
729 rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
731 spin_unlock_bh(root_lock);
735 EXPORT_SYMBOL(dev_graft_qdisc);
737 static void attach_one_default_qdisc(struct net_device *dev,
738 struct netdev_queue *dev_queue,
742 const struct Qdisc_ops *ops = default_qdisc_ops;
744 if (dev->priv_flags & IFF_NO_QUEUE)
745 ops = &noqueue_qdisc_ops;
747 qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT);
749 netdev_info(dev, "activation failed\n");
752 if (!netif_is_multiqueue(dev))
753 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
754 dev_queue->qdisc_sleeping = qdisc;
757 static void attach_default_qdiscs(struct net_device *dev)
759 struct netdev_queue *txq;
762 txq = netdev_get_tx_queue(dev, 0);
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);
770 qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT);
773 qdisc->ops->attach(qdisc);
778 static void transition_one_qdisc(struct net_device *dev,
779 struct netdev_queue *dev_queue,
780 void *_need_watchdog)
782 struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
783 int *need_watchdog_p = _need_watchdog;
785 if (!(new_qdisc->flags & TCQ_F_BUILTIN))
786 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
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;
795 void dev_activate(struct net_device *dev)
799 /* No queueing discipline is attached to device;
800 * create default one for devices, which need queueing
801 * and noqueue_qdisc for virtual interfaces
804 if (dev->qdisc == &noop_qdisc)
805 attach_default_qdiscs(dev);
807 if (!netif_carrier_ok(dev))
808 /* Delay activation until next carrier-on event */
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);
817 dev->trans_start = jiffies;
818 dev_watchdog_up(dev);
821 EXPORT_SYMBOL(dev_activate);
823 static void dev_deactivate_queue(struct net_device *dev,
824 struct netdev_queue *dev_queue,
825 void *_qdisc_default)
827 struct Qdisc *qdisc_default = _qdisc_default;
830 qdisc = rtnl_dereference(dev_queue->qdisc);
832 spin_lock_bh(qdisc_lock(qdisc));
834 if (!(qdisc->flags & TCQ_F_BUILTIN))
835 set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
837 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
840 spin_unlock_bh(qdisc_lock(qdisc));
844 static bool some_qdisc_is_busy(struct net_device *dev)
848 for (i = 0; i < dev->num_tx_queues; i++) {
849 struct netdev_queue *dev_queue;
850 spinlock_t *root_lock;
854 dev_queue = netdev_get_tx_queue(dev, i);
855 q = dev_queue->qdisc_sleeping;
856 root_lock = qdisc_lock(q);
858 spin_lock_bh(root_lock);
860 val = (qdisc_is_running(q) ||
861 test_bit(__QDISC_STATE_SCHED, &q->state));
863 spin_unlock_bh(root_lock);
872 * dev_deactivate_many - deactivate transmissions on several devices
873 * @head: list of devices to deactivate
875 * This function returns only when all outstanding transmissions
876 * have completed, unless all devices are in dismantle phase.
878 void dev_deactivate_many(struct list_head *head)
880 struct net_device *dev;
881 bool sync_needed = false;
883 list_for_each_entry(dev, head, close_list) {
884 netdev_for_each_tx_queue(dev, dev_deactivate_queue,
886 if (dev_ingress_queue(dev))
887 dev_deactivate_queue(dev, dev_ingress_queue(dev),
890 dev_watchdog_down(dev);
891 sync_needed |= !dev->dismantle;
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
901 /* Wait for outstanding qdisc_run calls. */
902 list_for_each_entry(dev, head, close_list)
903 while (some_qdisc_is_busy(dev))
907 void dev_deactivate(struct net_device *dev)
911 list_add(&dev->close_list, &single);
912 dev_deactivate_many(&single);
915 EXPORT_SYMBOL(dev_deactivate);
917 static void dev_init_scheduler_queue(struct net_device *dev,
918 struct netdev_queue *dev_queue,
921 struct Qdisc *qdisc = _qdisc;
923 rcu_assign_pointer(dev_queue->qdisc, qdisc);
924 dev_queue->qdisc_sleeping = qdisc;
927 void dev_init_scheduler(struct net_device *dev)
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);
934 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
937 static void shutdown_scheduler_queue(struct net_device *dev,
938 struct netdev_queue *dev_queue,
939 void *_qdisc_default)
941 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
942 struct Qdisc *qdisc_default = _qdisc_default;
945 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
946 dev_queue->qdisc_sleeping = qdisc_default;
948 qdisc_destroy(qdisc);
952 void dev_shutdown(struct net_device *dev)
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;
960 WARN_ON(timer_pending(&dev->watchdog_timer));
963 void psched_ratecfg_precompute(struct psched_ratecfg *r,
964 const struct tc_ratespec *conf,
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);
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)
976 * Normal formula would be :
977 * time_in_ns = (NSEC_PER_SEC * len) / rate_bps
979 * We compute mult/shift to use instead :
980 * time_in_ns = (len * mult) >> shift;
982 * We try to get the highest possible mult value for accuracy,
983 * but have to make sure no overflows will ever happen.
985 if (r->rate_bytes_ps > 0) {
986 u64 factor = NSEC_PER_SEC;
989 r->mult = div64_u64(factor, r->rate_bytes_ps);
990 if (r->mult & (1U << 31) || factor & (1ULL << 63))
997 EXPORT_SYMBOL(psched_ratecfg_precompute);