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
);
114 * Transmit possibly several skbs, and handle the return status as
115 * required. Owning running seqcount bit guarantees that
116 * only one CPU can execute this function.
118 * Returns to the caller:
119 * 0 - queue is empty or throttled.
120 * >0 - queue is not empty.
122 int sch_direct_xmit(struct sk_buff
*skb
, struct Qdisc
*q
,
123 struct net_device
*dev
, struct netdev_queue
*txq
,
124 spinlock_t
*root_lock
, bool validate
)
126 int ret
= NETDEV_TX_BUSY
;
128 /* And release qdisc */
129 spin_unlock(root_lock
);
131 /* Note that we validate skb (GSO, checksum, ...) outside of locks */
133 skb
= validate_xmit_skb_list(skb
, dev
);
136 HARD_TX_LOCK(dev
, txq
, smp_processor_id());
137 if (!netif_xmit_frozen_or_stopped(txq
))
138 skb
= dev_hard_start_xmit(skb
, dev
, txq
, &ret
);
140 HARD_TX_UNLOCK(dev
, txq
);
142 spin_lock(root_lock
);
143 return qdisc_qlen(q
);
145 spin_lock(root_lock
);
147 if (dev_xmit_complete(ret
)) {
148 /* Driver sent out skb successfully or skb was consumed */
151 /* Driver returned NETDEV_TX_BUSY - requeue skb */
152 if (unlikely(ret
!= NETDEV_TX_BUSY
))
153 net_warn_ratelimited("BUG %s code %d qlen %d\n",
154 dev
->name
, ret
, q
->q
.qlen
);
156 ret
= dev_requeue_skb(skb
, q
);
159 if (ret
&& netif_xmit_frozen_or_stopped(txq
))
166 * NOTE: Called under qdisc_lock(q) with locally disabled BH.
168 * running seqcount guarantees only one CPU can process
169 * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
172 * netif_tx_lock serializes accesses to device driver.
174 * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
175 * if one is grabbed, another must be free.
177 * Note, that this procedure can be called by a watchdog timer
179 * Returns to the caller:
180 * 0 - queue is empty or throttled.
181 * >0 - queue is not empty.
184 static inline int qdisc_restart(struct Qdisc
*q
, int *packets
)
186 struct netdev_queue
*txq
;
187 struct net_device
*dev
;
188 spinlock_t
*root_lock
;
193 skb
= dequeue_skb(q
, &validate
, packets
);
197 root_lock
= qdisc_lock(q
);
199 txq
= skb_get_tx_queue(dev
, skb
);
201 return sch_direct_xmit(skb
, q
, dev
, txq
, root_lock
, validate
);
204 void __qdisc_run(struct Qdisc
*q
)
206 int quota
= weight_p
;
209 while (qdisc_restart(q
, &packets
)) {
211 * Ordered by possible occurrence: Postpone processing if
212 * 1. we've exceeded packet quota
213 * 2. another process needs the CPU;
216 if (quota
<= 0 || need_resched()) {
225 unsigned long dev_trans_start(struct net_device
*dev
)
227 unsigned long val
, res
;
230 if (is_vlan_dev(dev
))
231 dev
= vlan_dev_real_dev(dev
);
232 res
= netdev_get_tx_queue(dev
, 0)->trans_start
;
233 for (i
= 1; i
< dev
->num_tx_queues
; i
++) {
234 val
= netdev_get_tx_queue(dev
, i
)->trans_start
;
235 if (val
&& time_after(val
, res
))
241 EXPORT_SYMBOL(dev_trans_start
);
243 static void dev_watchdog(unsigned long arg
)
245 struct net_device
*dev
= (struct net_device
*)arg
;
248 if (!qdisc_tx_is_noop(dev
)) {
249 if (netif_device_present(dev
) &&
250 netif_running(dev
) &&
251 netif_carrier_ok(dev
)) {
252 int some_queue_timedout
= 0;
254 unsigned long trans_start
;
256 for (i
= 0; i
< dev
->num_tx_queues
; i
++) {
257 struct netdev_queue
*txq
;
259 txq
= netdev_get_tx_queue(dev
, i
);
260 trans_start
= txq
->trans_start
;
261 if (netif_xmit_stopped(txq
) &&
262 time_after(jiffies
, (trans_start
+
263 dev
->watchdog_timeo
))) {
264 some_queue_timedout
= 1;
265 txq
->trans_timeout
++;
270 if (some_queue_timedout
) {
271 WARN_ONCE(1, KERN_INFO
"NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
272 dev
->name
, netdev_drivername(dev
), i
);
273 dev
->netdev_ops
->ndo_tx_timeout(dev
);
275 if (!mod_timer(&dev
->watchdog_timer
,
276 round_jiffies(jiffies
+
277 dev
->watchdog_timeo
)))
281 netif_tx_unlock(dev
);
286 void __netdev_watchdog_up(struct net_device
*dev
)
288 if (dev
->netdev_ops
->ndo_tx_timeout
) {
289 if (dev
->watchdog_timeo
<= 0)
290 dev
->watchdog_timeo
= 5*HZ
;
291 if (!mod_timer(&dev
->watchdog_timer
,
292 round_jiffies(jiffies
+ dev
->watchdog_timeo
)))
297 static void dev_watchdog_up(struct net_device
*dev
)
299 __netdev_watchdog_up(dev
);
302 static void dev_watchdog_down(struct net_device
*dev
)
304 netif_tx_lock_bh(dev
);
305 if (del_timer(&dev
->watchdog_timer
))
307 netif_tx_unlock_bh(dev
);
311 * netif_carrier_on - set carrier
312 * @dev: network device
314 * Device has detected that carrier.
316 void netif_carrier_on(struct net_device
*dev
)
318 if (test_and_clear_bit(__LINK_STATE_NOCARRIER
, &dev
->state
)) {
319 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
321 atomic_inc(&dev
->carrier_changes
);
322 linkwatch_fire_event(dev
);
323 if (netif_running(dev
))
324 __netdev_watchdog_up(dev
);
327 EXPORT_SYMBOL(netif_carrier_on
);
330 * netif_carrier_off - clear carrier
331 * @dev: network device
333 * Device has detected loss of carrier.
335 void netif_carrier_off(struct net_device
*dev
)
337 if (!test_and_set_bit(__LINK_STATE_NOCARRIER
, &dev
->state
)) {
338 if (dev
->reg_state
== NETREG_UNINITIALIZED
)
340 atomic_inc(&dev
->carrier_changes
);
341 linkwatch_fire_event(dev
);
344 EXPORT_SYMBOL(netif_carrier_off
);
346 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
347 under all circumstances. It is difficult to invent anything faster or
351 static int noop_enqueue(struct sk_buff
*skb
, struct Qdisc
*qdisc
)
357 static struct sk_buff
*noop_dequeue(struct Qdisc
*qdisc
)
362 struct Qdisc_ops noop_qdisc_ops __read_mostly
= {
365 .enqueue
= noop_enqueue
,
366 .dequeue
= noop_dequeue
,
367 .peek
= noop_dequeue
,
368 .owner
= THIS_MODULE
,
371 static struct netdev_queue noop_netdev_queue
= {
372 .qdisc
= &noop_qdisc
,
373 .qdisc_sleeping
= &noop_qdisc
,
376 struct Qdisc noop_qdisc
= {
377 .enqueue
= noop_enqueue
,
378 .dequeue
= noop_dequeue
,
379 .flags
= TCQ_F_BUILTIN
,
380 .ops
= &noop_qdisc_ops
,
381 .list
= LIST_HEAD_INIT(noop_qdisc
.list
),
382 .q
.lock
= __SPIN_LOCK_UNLOCKED(noop_qdisc
.q
.lock
),
383 .dev_queue
= &noop_netdev_queue
,
384 .running
= SEQCNT_ZERO(noop_qdisc
.running
),
385 .busylock
= __SPIN_LOCK_UNLOCKED(noop_qdisc
.busylock
),
387 EXPORT_SYMBOL(noop_qdisc
);
389 static int noqueue_init(struct Qdisc
*qdisc
, struct nlattr
*opt
)
391 /* register_qdisc() assigns a default of noop_enqueue if unset,
392 * but __dev_queue_xmit() treats noqueue only as such
393 * if this is NULL - so clear it here. */
394 qdisc
->enqueue
= NULL
;
398 struct Qdisc_ops noqueue_qdisc_ops __read_mostly
= {
401 .init
= noqueue_init
,
402 .enqueue
= noop_enqueue
,
403 .dequeue
= noop_dequeue
,
404 .peek
= noop_dequeue
,
405 .owner
= THIS_MODULE
,
408 static const u8 prio2band
[TC_PRIO_MAX
+ 1] = {
409 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
412 /* 3-band FIFO queue: old style, but should be a bit faster than
413 generic prio+fifo combination.
416 #define PFIFO_FAST_BANDS 3
419 * Private data for a pfifo_fast scheduler containing:
420 * - queues for the three band
421 * - bitmap indicating which of the bands contain skbs
423 struct pfifo_fast_priv
{
425 struct sk_buff_head q
[PFIFO_FAST_BANDS
];
429 * Convert a bitmap to the first band number where an skb is queued, where:
430 * bitmap=0 means there are no skbs on any band.
431 * bitmap=1 means there is an skb on band 0.
432 * bitmap=7 means there are skbs on all 3 bands, etc.
434 static const int bitmap2band
[] = {-1, 0, 1, 0, 2, 0, 1, 0};
436 static inline struct sk_buff_head
*band2list(struct pfifo_fast_priv
*priv
,
439 return priv
->q
+ band
;
442 static int pfifo_fast_enqueue(struct sk_buff
*skb
, struct Qdisc
*qdisc
)
444 if (skb_queue_len(&qdisc
->q
) < qdisc_dev(qdisc
)->tx_queue_len
) {
445 int band
= prio2band
[skb
->priority
& TC_PRIO_MAX
];
446 struct pfifo_fast_priv
*priv
= qdisc_priv(qdisc
);
447 struct sk_buff_head
*list
= band2list(priv
, band
);
449 priv
->bitmap
|= (1 << band
);
451 return __qdisc_enqueue_tail(skb
, qdisc
, list
);
454 return qdisc_drop(skb
, qdisc
);
457 static struct sk_buff
*pfifo_fast_dequeue(struct Qdisc
*qdisc
)
459 struct pfifo_fast_priv
*priv
= qdisc_priv(qdisc
);
460 int band
= bitmap2band
[priv
->bitmap
];
462 if (likely(band
>= 0)) {
463 struct sk_buff_head
*list
= band2list(priv
, band
);
464 struct sk_buff
*skb
= __qdisc_dequeue_head(qdisc
, list
);
467 if (skb_queue_empty(list
))
468 priv
->bitmap
&= ~(1 << band
);
476 static struct sk_buff
*pfifo_fast_peek(struct Qdisc
*qdisc
)
478 struct pfifo_fast_priv
*priv
= qdisc_priv(qdisc
);
479 int band
= bitmap2band
[priv
->bitmap
];
482 struct sk_buff_head
*list
= band2list(priv
, band
);
484 return skb_peek(list
);
490 static void pfifo_fast_reset(struct Qdisc
*qdisc
)
493 struct pfifo_fast_priv
*priv
= qdisc_priv(qdisc
);
495 for (prio
= 0; prio
< PFIFO_FAST_BANDS
; prio
++)
496 __qdisc_reset_queue(qdisc
, band2list(priv
, prio
));
499 qdisc
->qstats
.backlog
= 0;
503 static int pfifo_fast_dump(struct Qdisc
*qdisc
, struct sk_buff
*skb
)
505 struct tc_prio_qopt opt
= { .bands
= PFIFO_FAST_BANDS
};
507 memcpy(&opt
.priomap
, prio2band
, TC_PRIO_MAX
+ 1);
508 if (nla_put(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
))
509 goto nla_put_failure
;
516 static int pfifo_fast_init(struct Qdisc
*qdisc
, struct nlattr
*opt
)
519 struct pfifo_fast_priv
*priv
= qdisc_priv(qdisc
);
521 for (prio
= 0; prio
< PFIFO_FAST_BANDS
; prio
++)
522 __skb_queue_head_init(band2list(priv
, prio
));
524 /* Can by-pass the queue discipline */
525 qdisc
->flags
|= TCQ_F_CAN_BYPASS
;
529 struct Qdisc_ops pfifo_fast_ops __read_mostly
= {
531 .priv_size
= sizeof(struct pfifo_fast_priv
),
532 .enqueue
= pfifo_fast_enqueue
,
533 .dequeue
= pfifo_fast_dequeue
,
534 .peek
= pfifo_fast_peek
,
535 .init
= pfifo_fast_init
,
536 .reset
= pfifo_fast_reset
,
537 .dump
= pfifo_fast_dump
,
538 .owner
= THIS_MODULE
,
540 EXPORT_SYMBOL(pfifo_fast_ops
);
542 static struct lock_class_key qdisc_tx_busylock
;
543 static struct lock_class_key qdisc_running_key
;
545 struct Qdisc
*qdisc_alloc(struct netdev_queue
*dev_queue
,
546 const struct Qdisc_ops
*ops
)
550 unsigned int size
= QDISC_ALIGN(sizeof(*sch
)) + ops
->priv_size
;
552 struct net_device
*dev
= dev_queue
->dev
;
554 p
= kzalloc_node(size
, GFP_KERNEL
,
555 netdev_queue_numa_node_read(dev_queue
));
559 sch
= (struct Qdisc
*) QDISC_ALIGN((unsigned long) p
);
560 /* if we got non aligned memory, ask more and do alignment ourself */
563 p
= kzalloc_node(size
+ QDISC_ALIGNTO
- 1, GFP_KERNEL
,
564 netdev_queue_numa_node_read(dev_queue
));
567 sch
= (struct Qdisc
*) QDISC_ALIGN((unsigned long) p
);
568 sch
->padded
= (char *) sch
- (char *) p
;
570 INIT_LIST_HEAD(&sch
->list
);
571 skb_queue_head_init(&sch
->q
);
573 spin_lock_init(&sch
->busylock
);
574 lockdep_set_class(&sch
->busylock
,
575 dev
->qdisc_tx_busylock
?: &qdisc_tx_busylock
);
577 seqcount_init(&sch
->running
);
578 lockdep_set_class(&sch
->running
,
579 dev
->qdisc_running_key
?: &qdisc_running_key
);
582 sch
->enqueue
= ops
->enqueue
;
583 sch
->dequeue
= ops
->dequeue
;
584 sch
->dev_queue
= dev_queue
;
586 atomic_set(&sch
->refcnt
, 1);
593 struct Qdisc
*qdisc_create_dflt(struct netdev_queue
*dev_queue
,
594 const struct Qdisc_ops
*ops
,
595 unsigned int parentid
)
599 if (!try_module_get(ops
->owner
))
602 sch
= qdisc_alloc(dev_queue
, ops
);
605 sch
->parent
= parentid
;
607 if (!ops
->init
|| ops
->init(sch
, NULL
) == 0)
614 EXPORT_SYMBOL(qdisc_create_dflt
);
616 /* Under qdisc_lock(qdisc) and BH! */
618 void qdisc_reset(struct Qdisc
*qdisc
)
620 const struct Qdisc_ops
*ops
= qdisc
->ops
;
625 if (qdisc
->gso_skb
) {
626 kfree_skb_list(qdisc
->gso_skb
);
627 qdisc
->gso_skb
= NULL
;
631 EXPORT_SYMBOL(qdisc_reset
);
633 static void qdisc_rcu_free(struct rcu_head
*head
)
635 struct Qdisc
*qdisc
= container_of(head
, struct Qdisc
, rcu_head
);
637 if (qdisc_is_percpu_stats(qdisc
)) {
638 free_percpu(qdisc
->cpu_bstats
);
639 free_percpu(qdisc
->cpu_qstats
);
642 kfree((char *) qdisc
- qdisc
->padded
);
645 void qdisc_destroy(struct Qdisc
*qdisc
)
647 const struct Qdisc_ops
*ops
= qdisc
->ops
;
649 if (qdisc
->flags
& TCQ_F_BUILTIN
||
650 !atomic_dec_and_test(&qdisc
->refcnt
))
653 #ifdef CONFIG_NET_SCHED
654 qdisc_list_del(qdisc
);
656 qdisc_put_stab(rtnl_dereference(qdisc
->stab
));
658 gen_kill_estimator(&qdisc
->bstats
, &qdisc
->rate_est
);
664 module_put(ops
->owner
);
665 dev_put(qdisc_dev(qdisc
));
667 kfree_skb_list(qdisc
->gso_skb
);
669 * gen_estimator est_timer() might access qdisc->q.lock,
670 * wait a RCU grace period before freeing qdisc.
672 call_rcu(&qdisc
->rcu_head
, qdisc_rcu_free
);
674 EXPORT_SYMBOL(qdisc_destroy
);
676 /* Attach toplevel qdisc to device queue. */
677 struct Qdisc
*dev_graft_qdisc(struct netdev_queue
*dev_queue
,
680 struct Qdisc
*oqdisc
= dev_queue
->qdisc_sleeping
;
681 spinlock_t
*root_lock
;
683 root_lock
= qdisc_lock(oqdisc
);
684 spin_lock_bh(root_lock
);
686 /* Prune old scheduler */
687 if (oqdisc
&& atomic_read(&oqdisc
->refcnt
) <= 1)
690 /* ... and graft new one */
693 dev_queue
->qdisc_sleeping
= qdisc
;
694 rcu_assign_pointer(dev_queue
->qdisc
, &noop_qdisc
);
696 spin_unlock_bh(root_lock
);
700 EXPORT_SYMBOL(dev_graft_qdisc
);
702 static void attach_one_default_qdisc(struct net_device
*dev
,
703 struct netdev_queue
*dev_queue
,
707 const struct Qdisc_ops
*ops
= default_qdisc_ops
;
709 if (dev
->priv_flags
& IFF_NO_QUEUE
)
710 ops
= &noqueue_qdisc_ops
;
712 qdisc
= qdisc_create_dflt(dev_queue
, ops
, TC_H_ROOT
);
714 netdev_info(dev
, "activation failed\n");
717 if (!netif_is_multiqueue(dev
))
718 qdisc
->flags
|= TCQ_F_ONETXQUEUE
| TCQ_F_NOPARENT
;
719 dev_queue
->qdisc_sleeping
= qdisc
;
722 static void attach_default_qdiscs(struct net_device
*dev
)
724 struct netdev_queue
*txq
;
727 txq
= netdev_get_tx_queue(dev
, 0);
729 if (!netif_is_multiqueue(dev
) ||
730 dev
->priv_flags
& IFF_NO_QUEUE
) {
731 netdev_for_each_tx_queue(dev
, attach_one_default_qdisc
, NULL
);
732 dev
->qdisc
= txq
->qdisc_sleeping
;
733 atomic_inc(&dev
->qdisc
->refcnt
);
735 qdisc
= qdisc_create_dflt(txq
, &mq_qdisc_ops
, TC_H_ROOT
);
738 qdisc
->ops
->attach(qdisc
);
743 static void transition_one_qdisc(struct net_device
*dev
,
744 struct netdev_queue
*dev_queue
,
745 void *_need_watchdog
)
747 struct Qdisc
*new_qdisc
= dev_queue
->qdisc_sleeping
;
748 int *need_watchdog_p
= _need_watchdog
;
750 if (!(new_qdisc
->flags
& TCQ_F_BUILTIN
))
751 clear_bit(__QDISC_STATE_DEACTIVATED
, &new_qdisc
->state
);
753 rcu_assign_pointer(dev_queue
->qdisc
, new_qdisc
);
754 if (need_watchdog_p
) {
755 dev_queue
->trans_start
= 0;
756 *need_watchdog_p
= 1;
760 void dev_activate(struct net_device
*dev
)
764 /* No queueing discipline is attached to device;
765 * create default one for devices, which need queueing
766 * and noqueue_qdisc for virtual interfaces
769 if (dev
->qdisc
== &noop_qdisc
)
770 attach_default_qdiscs(dev
);
772 if (!netif_carrier_ok(dev
))
773 /* Delay activation until next carrier-on event */
777 netdev_for_each_tx_queue(dev
, transition_one_qdisc
, &need_watchdog
);
778 if (dev_ingress_queue(dev
))
779 transition_one_qdisc(dev
, dev_ingress_queue(dev
), NULL
);
782 netif_trans_update(dev
);
783 dev_watchdog_up(dev
);
786 EXPORT_SYMBOL(dev_activate
);
788 static void dev_deactivate_queue(struct net_device
*dev
,
789 struct netdev_queue
*dev_queue
,
790 void *_qdisc_default
)
792 struct Qdisc
*qdisc_default
= _qdisc_default
;
795 qdisc
= rtnl_dereference(dev_queue
->qdisc
);
797 spin_lock_bh(qdisc_lock(qdisc
));
799 if (!(qdisc
->flags
& TCQ_F_BUILTIN
))
800 set_bit(__QDISC_STATE_DEACTIVATED
, &qdisc
->state
);
802 rcu_assign_pointer(dev_queue
->qdisc
, qdisc_default
);
805 spin_unlock_bh(qdisc_lock(qdisc
));
809 static bool some_qdisc_is_busy(struct net_device
*dev
)
813 for (i
= 0; i
< dev
->num_tx_queues
; i
++) {
814 struct netdev_queue
*dev_queue
;
815 spinlock_t
*root_lock
;
819 dev_queue
= netdev_get_tx_queue(dev
, i
);
820 q
= dev_queue
->qdisc_sleeping
;
821 root_lock
= qdisc_lock(q
);
823 spin_lock_bh(root_lock
);
825 val
= (qdisc_is_running(q
) ||
826 test_bit(__QDISC_STATE_SCHED
, &q
->state
));
828 spin_unlock_bh(root_lock
);
837 * dev_deactivate_many - deactivate transmissions on several devices
838 * @head: list of devices to deactivate
840 * This function returns only when all outstanding transmissions
841 * have completed, unless all devices are in dismantle phase.
843 void dev_deactivate_many(struct list_head
*head
)
845 struct net_device
*dev
;
846 bool sync_needed
= false;
848 list_for_each_entry(dev
, head
, close_list
) {
849 netdev_for_each_tx_queue(dev
, dev_deactivate_queue
,
851 if (dev_ingress_queue(dev
))
852 dev_deactivate_queue(dev
, dev_ingress_queue(dev
),
855 dev_watchdog_down(dev
);
856 sync_needed
|= !dev
->dismantle
;
859 /* Wait for outstanding qdisc-less dev_queue_xmit calls.
860 * This is avoided if all devices are in dismantle phase :
861 * Caller will call synchronize_net() for us
866 /* Wait for outstanding qdisc_run calls. */
867 list_for_each_entry(dev
, head
, close_list
)
868 while (some_qdisc_is_busy(dev
))
872 void dev_deactivate(struct net_device
*dev
)
876 list_add(&dev
->close_list
, &single
);
877 dev_deactivate_many(&single
);
880 EXPORT_SYMBOL(dev_deactivate
);
882 static void dev_init_scheduler_queue(struct net_device
*dev
,
883 struct netdev_queue
*dev_queue
,
886 struct Qdisc
*qdisc
= _qdisc
;
888 rcu_assign_pointer(dev_queue
->qdisc
, qdisc
);
889 dev_queue
->qdisc_sleeping
= qdisc
;
892 void dev_init_scheduler(struct net_device
*dev
)
894 dev
->qdisc
= &noop_qdisc
;
895 netdev_for_each_tx_queue(dev
, dev_init_scheduler_queue
, &noop_qdisc
);
896 if (dev_ingress_queue(dev
))
897 dev_init_scheduler_queue(dev
, dev_ingress_queue(dev
), &noop_qdisc
);
899 setup_timer(&dev
->watchdog_timer
, dev_watchdog
, (unsigned long)dev
);
902 static void shutdown_scheduler_queue(struct net_device
*dev
,
903 struct netdev_queue
*dev_queue
,
904 void *_qdisc_default
)
906 struct Qdisc
*qdisc
= dev_queue
->qdisc_sleeping
;
907 struct Qdisc
*qdisc_default
= _qdisc_default
;
910 rcu_assign_pointer(dev_queue
->qdisc
, qdisc_default
);
911 dev_queue
->qdisc_sleeping
= qdisc_default
;
913 qdisc_destroy(qdisc
);
917 void dev_shutdown(struct net_device
*dev
)
919 netdev_for_each_tx_queue(dev
, shutdown_scheduler_queue
, &noop_qdisc
);
920 if (dev_ingress_queue(dev
))
921 shutdown_scheduler_queue(dev
, dev_ingress_queue(dev
), &noop_qdisc
);
922 qdisc_destroy(dev
->qdisc
);
923 dev
->qdisc
= &noop_qdisc
;
925 WARN_ON(timer_pending(&dev
->watchdog_timer
));
928 void psched_ratecfg_precompute(struct psched_ratecfg
*r
,
929 const struct tc_ratespec
*conf
,
932 memset(r
, 0, sizeof(*r
));
933 r
->overhead
= conf
->overhead
;
934 r
->rate_bytes_ps
= max_t(u64
, conf
->rate
, rate64
);
935 r
->linklayer
= (conf
->linklayer
& TC_LINKLAYER_MASK
);
938 * The deal here is to replace a divide by a reciprocal one
939 * in fast path (a reciprocal divide is a multiply and a shift)
941 * Normal formula would be :
942 * time_in_ns = (NSEC_PER_SEC * len) / rate_bps
944 * We compute mult/shift to use instead :
945 * time_in_ns = (len * mult) >> shift;
947 * We try to get the highest possible mult value for accuracy,
948 * but have to make sure no overflows will ever happen.
950 if (r
->rate_bytes_ps
> 0) {
951 u64 factor
= NSEC_PER_SEC
;
954 r
->mult
= div64_u64(factor
, r
->rate_bytes_ps
);
955 if (r
->mult
& (1U << 31) || factor
& (1ULL << 63))
962 EXPORT_SYMBOL(psched_ratecfg_precompute
);