Linux 4.19.168
[linux/fpc-iii.git] / net / sched / sch_generic.c
blob4e15913e7519e0e2fd2150dfd3ba3edde4abfdd7
1 /*
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
11 * - Ingress support
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 <linux/skb_array.h>
30 #include <linux/if_macvlan.h>
31 #include <net/sch_generic.h>
32 #include <net/pkt_sched.h>
33 #include <net/dst.h>
34 #include <trace/events/qdisc.h>
35 #include <net/xfrm.h>
37 /* Qdisc to use by default */
38 const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops;
39 EXPORT_SYMBOL(default_qdisc_ops);
41 /* Main transmission queue. */
43 /* Modifications to data participating in scheduling must be protected with
44 * qdisc_lock(qdisc) spinlock.
46 * The idea is the following:
47 * - enqueue, dequeue are serialized via qdisc root lock
48 * - ingress filtering is also serialized via qdisc root lock
49 * - updates to tree and tree walking are only done under the rtnl mutex.
52 #define SKB_XOFF_MAGIC ((struct sk_buff *)1UL)
54 static inline struct sk_buff *__skb_dequeue_bad_txq(struct Qdisc *q)
56 const struct netdev_queue *txq = q->dev_queue;
57 spinlock_t *lock = NULL;
58 struct sk_buff *skb;
60 if (q->flags & TCQ_F_NOLOCK) {
61 lock = qdisc_lock(q);
62 spin_lock(lock);
65 skb = skb_peek(&q->skb_bad_txq);
66 if (skb) {
67 /* check the reason of requeuing without tx lock first */
68 txq = skb_get_tx_queue(txq->dev, skb);
69 if (!netif_xmit_frozen_or_stopped(txq)) {
70 skb = __skb_dequeue(&q->skb_bad_txq);
71 if (qdisc_is_percpu_stats(q)) {
72 qdisc_qstats_cpu_backlog_dec(q, skb);
73 qdisc_qstats_atomic_qlen_dec(q);
74 } else {
75 qdisc_qstats_backlog_dec(q, skb);
76 q->q.qlen--;
78 } else {
79 skb = SKB_XOFF_MAGIC;
83 if (lock)
84 spin_unlock(lock);
86 return skb;
89 static inline struct sk_buff *qdisc_dequeue_skb_bad_txq(struct Qdisc *q)
91 struct sk_buff *skb = skb_peek(&q->skb_bad_txq);
93 if (unlikely(skb))
94 skb = __skb_dequeue_bad_txq(q);
96 return skb;
99 static inline void qdisc_enqueue_skb_bad_txq(struct Qdisc *q,
100 struct sk_buff *skb)
102 spinlock_t *lock = NULL;
104 if (q->flags & TCQ_F_NOLOCK) {
105 lock = qdisc_lock(q);
106 spin_lock(lock);
109 __skb_queue_tail(&q->skb_bad_txq, skb);
111 if (qdisc_is_percpu_stats(q)) {
112 qdisc_qstats_cpu_backlog_inc(q, skb);
113 qdisc_qstats_atomic_qlen_inc(q);
114 } else {
115 qdisc_qstats_backlog_inc(q, skb);
116 q->q.qlen++;
119 if (lock)
120 spin_unlock(lock);
123 static inline int __dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
125 while (skb) {
126 struct sk_buff *next = skb->next;
128 __skb_queue_tail(&q->gso_skb, skb);
129 q->qstats.requeues++;
130 qdisc_qstats_backlog_inc(q, skb);
131 q->q.qlen++; /* it's still part of the queue */
133 skb = next;
135 __netif_schedule(q);
137 return 0;
140 static inline int dev_requeue_skb_locked(struct sk_buff *skb, struct Qdisc *q)
142 spinlock_t *lock = qdisc_lock(q);
144 spin_lock(lock);
145 while (skb) {
146 struct sk_buff *next = skb->next;
148 __skb_queue_tail(&q->gso_skb, skb);
150 qdisc_qstats_cpu_requeues_inc(q);
151 qdisc_qstats_cpu_backlog_inc(q, skb);
152 qdisc_qstats_atomic_qlen_inc(q);
154 skb = next;
156 spin_unlock(lock);
158 __netif_schedule(q);
160 return 0;
163 static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
165 if (q->flags & TCQ_F_NOLOCK)
166 return dev_requeue_skb_locked(skb, q);
167 else
168 return __dev_requeue_skb(skb, q);
171 static void try_bulk_dequeue_skb(struct Qdisc *q,
172 struct sk_buff *skb,
173 const struct netdev_queue *txq,
174 int *packets)
176 int bytelimit = qdisc_avail_bulklimit(txq) - skb->len;
178 while (bytelimit > 0) {
179 struct sk_buff *nskb = q->dequeue(q);
181 if (!nskb)
182 break;
184 bytelimit -= nskb->len; /* covers GSO len */
185 skb->next = nskb;
186 skb = nskb;
187 (*packets)++; /* GSO counts as one pkt */
189 skb->next = NULL;
192 /* This variant of try_bulk_dequeue_skb() makes sure
193 * all skbs in the chain are for the same txq
195 static void try_bulk_dequeue_skb_slow(struct Qdisc *q,
196 struct sk_buff *skb,
197 int *packets)
199 int mapping = skb_get_queue_mapping(skb);
200 struct sk_buff *nskb;
201 int cnt = 0;
203 do {
204 nskb = q->dequeue(q);
205 if (!nskb)
206 break;
207 if (unlikely(skb_get_queue_mapping(nskb) != mapping)) {
208 qdisc_enqueue_skb_bad_txq(q, nskb);
209 break;
211 skb->next = nskb;
212 skb = nskb;
213 } while (++cnt < 8);
214 (*packets) += cnt;
215 skb->next = NULL;
218 /* Note that dequeue_skb can possibly return a SKB list (via skb->next).
219 * A requeued skb (via q->gso_skb) can also be a SKB list.
221 static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
222 int *packets)
224 const struct netdev_queue *txq = q->dev_queue;
225 struct sk_buff *skb = NULL;
227 *packets = 1;
228 if (unlikely(!skb_queue_empty(&q->gso_skb))) {
229 spinlock_t *lock = NULL;
231 if (q->flags & TCQ_F_NOLOCK) {
232 lock = qdisc_lock(q);
233 spin_lock(lock);
236 skb = skb_peek(&q->gso_skb);
238 /* skb may be null if another cpu pulls gso_skb off in between
239 * empty check and lock.
241 if (!skb) {
242 if (lock)
243 spin_unlock(lock);
244 goto validate;
247 /* skb in gso_skb were already validated */
248 *validate = false;
249 if (xfrm_offload(skb))
250 *validate = true;
251 /* check the reason of requeuing without tx lock first */
252 txq = skb_get_tx_queue(txq->dev, skb);
253 if (!netif_xmit_frozen_or_stopped(txq)) {
254 skb = __skb_dequeue(&q->gso_skb);
255 if (qdisc_is_percpu_stats(q)) {
256 qdisc_qstats_cpu_backlog_dec(q, skb);
257 qdisc_qstats_atomic_qlen_dec(q);
258 } else {
259 qdisc_qstats_backlog_dec(q, skb);
260 q->q.qlen--;
262 } else {
263 skb = NULL;
265 if (lock)
266 spin_unlock(lock);
267 goto trace;
269 validate:
270 *validate = true;
272 if ((q->flags & TCQ_F_ONETXQUEUE) &&
273 netif_xmit_frozen_or_stopped(txq))
274 return skb;
276 skb = qdisc_dequeue_skb_bad_txq(q);
277 if (unlikely(skb)) {
278 if (skb == SKB_XOFF_MAGIC)
279 return NULL;
280 goto bulk;
282 skb = q->dequeue(q);
283 if (skb) {
284 bulk:
285 if (qdisc_may_bulk(q))
286 try_bulk_dequeue_skb(q, skb, txq, packets);
287 else
288 try_bulk_dequeue_skb_slow(q, skb, packets);
290 trace:
291 trace_qdisc_dequeue(q, txq, *packets, skb);
292 return skb;
296 * Transmit possibly several skbs, and handle the return status as
297 * required. Owning running seqcount bit guarantees that
298 * only one CPU can execute this function.
300 * Returns to the caller:
301 * false - hardware queue frozen backoff
302 * true - feel free to send more pkts
304 bool sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
305 struct net_device *dev, struct netdev_queue *txq,
306 spinlock_t *root_lock, bool validate)
308 int ret = NETDEV_TX_BUSY;
309 bool again = false;
311 /* And release qdisc */
312 if (root_lock)
313 spin_unlock(root_lock);
315 /* Note that we validate skb (GSO, checksum, ...) outside of locks */
316 if (validate)
317 skb = validate_xmit_skb_list(skb, dev, &again);
319 #ifdef CONFIG_XFRM_OFFLOAD
320 if (unlikely(again)) {
321 if (root_lock)
322 spin_lock(root_lock);
324 dev_requeue_skb(skb, q);
325 return false;
327 #endif
329 if (likely(skb)) {
330 HARD_TX_LOCK(dev, txq, smp_processor_id());
331 if (!netif_xmit_frozen_or_stopped(txq))
332 skb = dev_hard_start_xmit(skb, dev, txq, &ret);
334 HARD_TX_UNLOCK(dev, txq);
335 } else {
336 if (root_lock)
337 spin_lock(root_lock);
338 return true;
341 if (root_lock)
342 spin_lock(root_lock);
344 if (!dev_xmit_complete(ret)) {
345 /* Driver returned NETDEV_TX_BUSY - requeue skb */
346 if (unlikely(ret != NETDEV_TX_BUSY))
347 net_warn_ratelimited("BUG %s code %d qlen %d\n",
348 dev->name, ret, q->q.qlen);
350 dev_requeue_skb(skb, q);
351 return false;
354 return true;
358 * NOTE: Called under qdisc_lock(q) with locally disabled BH.
360 * running seqcount guarantees only one CPU can process
361 * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
362 * this queue.
364 * netif_tx_lock serializes accesses to device driver.
366 * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
367 * if one is grabbed, another must be free.
369 * Note, that this procedure can be called by a watchdog timer
371 * Returns to the caller:
372 * 0 - queue is empty or throttled.
373 * >0 - queue is not empty.
376 static inline bool qdisc_restart(struct Qdisc *q, int *packets)
378 spinlock_t *root_lock = NULL;
379 struct netdev_queue *txq;
380 struct net_device *dev;
381 struct sk_buff *skb;
382 bool validate;
384 /* Dequeue packet */
385 skb = dequeue_skb(q, &validate, packets);
386 if (unlikely(!skb))
387 return false;
389 if (!(q->flags & TCQ_F_NOLOCK))
390 root_lock = qdisc_lock(q);
392 dev = qdisc_dev(q);
393 txq = skb_get_tx_queue(dev, skb);
395 return sch_direct_xmit(skb, q, dev, txq, root_lock, validate);
398 void __qdisc_run(struct Qdisc *q)
400 int quota = dev_tx_weight;
401 int packets;
403 while (qdisc_restart(q, &packets)) {
405 * Ordered by possible occurrence: Postpone processing if
406 * 1. we've exceeded packet quota
407 * 2. another process needs the CPU;
409 quota -= packets;
410 if (quota <= 0 || need_resched()) {
411 __netif_schedule(q);
412 break;
417 unsigned long dev_trans_start(struct net_device *dev)
419 unsigned long val, res;
420 unsigned int i;
422 if (is_vlan_dev(dev))
423 dev = vlan_dev_real_dev(dev);
424 else if (netif_is_macvlan(dev))
425 dev = macvlan_dev_real_dev(dev);
426 res = netdev_get_tx_queue(dev, 0)->trans_start;
427 for (i = 1; i < dev->num_tx_queues; i++) {
428 val = netdev_get_tx_queue(dev, i)->trans_start;
429 if (val && time_after(val, res))
430 res = val;
433 return res;
435 EXPORT_SYMBOL(dev_trans_start);
437 static void dev_watchdog(struct timer_list *t)
439 struct net_device *dev = from_timer(dev, t, watchdog_timer);
441 netif_tx_lock(dev);
442 if (!qdisc_tx_is_noop(dev)) {
443 if (netif_device_present(dev) &&
444 netif_running(dev) &&
445 netif_carrier_ok(dev)) {
446 int some_queue_timedout = 0;
447 unsigned int i;
448 unsigned long trans_start;
450 for (i = 0; i < dev->num_tx_queues; i++) {
451 struct netdev_queue *txq;
453 txq = netdev_get_tx_queue(dev, i);
454 trans_start = txq->trans_start;
455 if (netif_xmit_stopped(txq) &&
456 time_after(jiffies, (trans_start +
457 dev->watchdog_timeo))) {
458 some_queue_timedout = 1;
459 txq->trans_timeout++;
460 break;
464 if (some_queue_timedout) {
465 WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
466 dev->name, netdev_drivername(dev), i);
467 dev->netdev_ops->ndo_tx_timeout(dev);
469 if (!mod_timer(&dev->watchdog_timer,
470 round_jiffies(jiffies +
471 dev->watchdog_timeo)))
472 dev_hold(dev);
475 netif_tx_unlock(dev);
477 dev_put(dev);
480 void __netdev_watchdog_up(struct net_device *dev)
482 if (dev->netdev_ops->ndo_tx_timeout) {
483 if (dev->watchdog_timeo <= 0)
484 dev->watchdog_timeo = 5*HZ;
485 if (!mod_timer(&dev->watchdog_timer,
486 round_jiffies(jiffies + dev->watchdog_timeo)))
487 dev_hold(dev);
490 EXPORT_SYMBOL_GPL(__netdev_watchdog_up);
492 static void dev_watchdog_up(struct net_device *dev)
494 __netdev_watchdog_up(dev);
497 static void dev_watchdog_down(struct net_device *dev)
499 netif_tx_lock_bh(dev);
500 if (del_timer(&dev->watchdog_timer))
501 dev_put(dev);
502 netif_tx_unlock_bh(dev);
506 * netif_carrier_on - set carrier
507 * @dev: network device
509 * Device has detected that carrier.
511 void netif_carrier_on(struct net_device *dev)
513 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
514 if (dev->reg_state == NETREG_UNINITIALIZED)
515 return;
516 atomic_inc(&dev->carrier_up_count);
517 linkwatch_fire_event(dev);
518 if (netif_running(dev))
519 __netdev_watchdog_up(dev);
522 EXPORT_SYMBOL(netif_carrier_on);
525 * netif_carrier_off - clear carrier
526 * @dev: network device
528 * Device has detected loss of carrier.
530 void netif_carrier_off(struct net_device *dev)
532 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
533 if (dev->reg_state == NETREG_UNINITIALIZED)
534 return;
535 atomic_inc(&dev->carrier_down_count);
536 linkwatch_fire_event(dev);
539 EXPORT_SYMBOL(netif_carrier_off);
541 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
542 under all circumstances. It is difficult to invent anything faster or
543 cheaper.
546 static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
547 struct sk_buff **to_free)
549 __qdisc_drop(skb, to_free);
550 return NET_XMIT_CN;
553 static struct sk_buff *noop_dequeue(struct Qdisc *qdisc)
555 return NULL;
558 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
559 .id = "noop",
560 .priv_size = 0,
561 .enqueue = noop_enqueue,
562 .dequeue = noop_dequeue,
563 .peek = noop_dequeue,
564 .owner = THIS_MODULE,
567 static struct netdev_queue noop_netdev_queue = {
568 .qdisc = &noop_qdisc,
569 .qdisc_sleeping = &noop_qdisc,
572 struct Qdisc noop_qdisc = {
573 .enqueue = noop_enqueue,
574 .dequeue = noop_dequeue,
575 .flags = TCQ_F_BUILTIN,
576 .ops = &noop_qdisc_ops,
577 .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
578 .dev_queue = &noop_netdev_queue,
579 .running = SEQCNT_ZERO(noop_qdisc.running),
580 .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock),
581 .gso_skb = {
582 .next = (struct sk_buff *)&noop_qdisc.gso_skb,
583 .prev = (struct sk_buff *)&noop_qdisc.gso_skb,
584 .qlen = 0,
585 .lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.gso_skb.lock),
587 .skb_bad_txq = {
588 .next = (struct sk_buff *)&noop_qdisc.skb_bad_txq,
589 .prev = (struct sk_buff *)&noop_qdisc.skb_bad_txq,
590 .qlen = 0,
591 .lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.skb_bad_txq.lock),
594 EXPORT_SYMBOL(noop_qdisc);
596 static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt,
597 struct netlink_ext_ack *extack)
599 /* register_qdisc() assigns a default of noop_enqueue if unset,
600 * but __dev_queue_xmit() treats noqueue only as such
601 * if this is NULL - so clear it here. */
602 qdisc->enqueue = NULL;
603 return 0;
606 struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
607 .id = "noqueue",
608 .priv_size = 0,
609 .init = noqueue_init,
610 .enqueue = noop_enqueue,
611 .dequeue = noop_dequeue,
612 .peek = noop_dequeue,
613 .owner = THIS_MODULE,
616 static const u8 prio2band[TC_PRIO_MAX + 1] = {
617 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
620 /* 3-band FIFO queue: old style, but should be a bit faster than
621 generic prio+fifo combination.
624 #define PFIFO_FAST_BANDS 3
627 * Private data for a pfifo_fast scheduler containing:
628 * - rings for priority bands
630 struct pfifo_fast_priv {
631 struct skb_array q[PFIFO_FAST_BANDS];
634 static inline struct skb_array *band2list(struct pfifo_fast_priv *priv,
635 int band)
637 return &priv->q[band];
640 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
641 struct sk_buff **to_free)
643 int band = prio2band[skb->priority & TC_PRIO_MAX];
644 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
645 struct skb_array *q = band2list(priv, band);
646 unsigned int pkt_len = qdisc_pkt_len(skb);
647 int err;
649 err = skb_array_produce(q, skb);
651 if (unlikely(err))
652 return qdisc_drop_cpu(skb, qdisc, to_free);
654 qdisc_qstats_atomic_qlen_inc(qdisc);
655 /* Note: skb can not be used after skb_array_produce(),
656 * so we better not use qdisc_qstats_cpu_backlog_inc()
658 this_cpu_add(qdisc->cpu_qstats->backlog, pkt_len);
659 return NET_XMIT_SUCCESS;
662 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
664 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
665 struct sk_buff *skb = NULL;
666 int band;
668 for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
669 struct skb_array *q = band2list(priv, band);
671 if (__skb_array_empty(q))
672 continue;
674 skb = __skb_array_consume(q);
676 if (likely(skb)) {
677 qdisc_qstats_cpu_backlog_dec(qdisc, skb);
678 qdisc_bstats_cpu_update(qdisc, skb);
679 qdisc_qstats_atomic_qlen_dec(qdisc);
682 return skb;
685 static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
687 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
688 struct sk_buff *skb = NULL;
689 int band;
691 for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
692 struct skb_array *q = band2list(priv, band);
694 skb = __skb_array_peek(q);
697 return skb;
700 static void pfifo_fast_reset(struct Qdisc *qdisc)
702 int i, band;
703 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
705 for (band = 0; band < PFIFO_FAST_BANDS; band++) {
706 struct skb_array *q = band2list(priv, band);
707 struct sk_buff *skb;
709 /* NULL ring is possible if destroy path is due to a failed
710 * skb_array_init() in pfifo_fast_init() case.
712 if (!q->ring.queue)
713 continue;
715 while ((skb = __skb_array_consume(q)) != NULL)
716 kfree_skb(skb);
719 for_each_possible_cpu(i) {
720 struct gnet_stats_queue *q = per_cpu_ptr(qdisc->cpu_qstats, i);
722 q->backlog = 0;
726 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
728 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
730 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1);
731 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
732 goto nla_put_failure;
733 return skb->len;
735 nla_put_failure:
736 return -1;
739 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt,
740 struct netlink_ext_ack *extack)
742 unsigned int qlen = qdisc_dev(qdisc)->tx_queue_len;
743 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
744 int prio;
746 /* guard against zero length rings */
747 if (!qlen)
748 return -EINVAL;
750 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
751 struct skb_array *q = band2list(priv, prio);
752 int err;
754 err = skb_array_init(q, qlen, GFP_KERNEL);
755 if (err)
756 return -ENOMEM;
759 /* Can by-pass the queue discipline */
760 qdisc->flags |= TCQ_F_CAN_BYPASS;
761 return 0;
764 static void pfifo_fast_destroy(struct Qdisc *sch)
766 struct pfifo_fast_priv *priv = qdisc_priv(sch);
767 int prio;
769 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
770 struct skb_array *q = band2list(priv, prio);
772 /* NULL ring is possible if destroy path is due to a failed
773 * skb_array_init() in pfifo_fast_init() case.
775 if (!q->ring.queue)
776 continue;
777 /* Destroy ring but no need to kfree_skb because a call to
778 * pfifo_fast_reset() has already done that work.
780 ptr_ring_cleanup(&q->ring, NULL);
784 static int pfifo_fast_change_tx_queue_len(struct Qdisc *sch,
785 unsigned int new_len)
787 struct pfifo_fast_priv *priv = qdisc_priv(sch);
788 struct skb_array *bands[PFIFO_FAST_BANDS];
789 int prio;
791 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
792 struct skb_array *q = band2list(priv, prio);
794 bands[prio] = q;
797 return skb_array_resize_multiple(bands, PFIFO_FAST_BANDS, new_len,
798 GFP_KERNEL);
801 struct Qdisc_ops pfifo_fast_ops __read_mostly = {
802 .id = "pfifo_fast",
803 .priv_size = sizeof(struct pfifo_fast_priv),
804 .enqueue = pfifo_fast_enqueue,
805 .dequeue = pfifo_fast_dequeue,
806 .peek = pfifo_fast_peek,
807 .init = pfifo_fast_init,
808 .destroy = pfifo_fast_destroy,
809 .reset = pfifo_fast_reset,
810 .dump = pfifo_fast_dump,
811 .change_tx_queue_len = pfifo_fast_change_tx_queue_len,
812 .owner = THIS_MODULE,
813 .static_flags = TCQ_F_NOLOCK | TCQ_F_CPUSTATS,
815 EXPORT_SYMBOL(pfifo_fast_ops);
817 static struct lock_class_key qdisc_tx_busylock;
818 static struct lock_class_key qdisc_running_key;
820 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
821 const struct Qdisc_ops *ops,
822 struct netlink_ext_ack *extack)
824 void *p;
825 struct Qdisc *sch;
826 unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size;
827 int err = -ENOBUFS;
828 struct net_device *dev;
830 if (!dev_queue) {
831 NL_SET_ERR_MSG(extack, "No device queue given");
832 err = -EINVAL;
833 goto errout;
836 dev = dev_queue->dev;
837 p = kzalloc_node(size, GFP_KERNEL,
838 netdev_queue_numa_node_read(dev_queue));
840 if (!p)
841 goto errout;
842 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
843 /* if we got non aligned memory, ask more and do alignment ourself */
844 if (sch != p) {
845 kfree(p);
846 p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL,
847 netdev_queue_numa_node_read(dev_queue));
848 if (!p)
849 goto errout;
850 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
851 sch->padded = (char *) sch - (char *) p;
853 __skb_queue_head_init(&sch->gso_skb);
854 __skb_queue_head_init(&sch->skb_bad_txq);
855 qdisc_skb_head_init(&sch->q);
856 spin_lock_init(&sch->q.lock);
858 if (ops->static_flags & TCQ_F_CPUSTATS) {
859 sch->cpu_bstats =
860 netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
861 if (!sch->cpu_bstats)
862 goto errout1;
864 sch->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
865 if (!sch->cpu_qstats) {
866 free_percpu(sch->cpu_bstats);
867 goto errout1;
871 spin_lock_init(&sch->busylock);
872 lockdep_set_class(&sch->busylock,
873 dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
875 /* seqlock has the same scope of busylock, for NOLOCK qdisc */
876 spin_lock_init(&sch->seqlock);
877 lockdep_set_class(&sch->busylock,
878 dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
880 seqcount_init(&sch->running);
881 lockdep_set_class(&sch->running,
882 dev->qdisc_running_key ?: &qdisc_running_key);
884 sch->ops = ops;
885 sch->flags = ops->static_flags;
886 sch->enqueue = ops->enqueue;
887 sch->dequeue = ops->dequeue;
888 sch->dev_queue = dev_queue;
889 dev_hold(dev);
890 refcount_set(&sch->refcnt, 1);
892 return sch;
893 errout1:
894 kfree(p);
895 errout:
896 return ERR_PTR(err);
899 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
900 const struct Qdisc_ops *ops,
901 unsigned int parentid,
902 struct netlink_ext_ack *extack)
904 struct Qdisc *sch;
906 if (!try_module_get(ops->owner)) {
907 NL_SET_ERR_MSG(extack, "Failed to increase module reference counter");
908 return NULL;
911 sch = qdisc_alloc(dev_queue, ops, extack);
912 if (IS_ERR(sch)) {
913 module_put(ops->owner);
914 return NULL;
916 sch->parent = parentid;
918 if (!ops->init || ops->init(sch, NULL, extack) == 0)
919 return sch;
921 qdisc_destroy(sch);
922 return NULL;
924 EXPORT_SYMBOL(qdisc_create_dflt);
926 /* Under qdisc_lock(qdisc) and BH! */
928 void qdisc_reset(struct Qdisc *qdisc)
930 const struct Qdisc_ops *ops = qdisc->ops;
931 struct sk_buff *skb, *tmp;
933 if (ops->reset)
934 ops->reset(qdisc);
936 skb_queue_walk_safe(&qdisc->gso_skb, skb, tmp) {
937 __skb_unlink(skb, &qdisc->gso_skb);
938 kfree_skb_list(skb);
941 skb_queue_walk_safe(&qdisc->skb_bad_txq, skb, tmp) {
942 __skb_unlink(skb, &qdisc->skb_bad_txq);
943 kfree_skb_list(skb);
946 qdisc->q.qlen = 0;
947 qdisc->qstats.backlog = 0;
949 EXPORT_SYMBOL(qdisc_reset);
951 void qdisc_free(struct Qdisc *qdisc)
953 if (qdisc_is_percpu_stats(qdisc)) {
954 free_percpu(qdisc->cpu_bstats);
955 free_percpu(qdisc->cpu_qstats);
958 kfree((char *) qdisc - qdisc->padded);
961 void qdisc_destroy(struct Qdisc *qdisc)
963 const struct Qdisc_ops *ops;
964 struct sk_buff *skb, *tmp;
966 if (!qdisc)
967 return;
968 ops = qdisc->ops;
970 if (qdisc->flags & TCQ_F_BUILTIN ||
971 !refcount_dec_and_test(&qdisc->refcnt))
972 return;
974 #ifdef CONFIG_NET_SCHED
975 qdisc_hash_del(qdisc);
977 qdisc_put_stab(rtnl_dereference(qdisc->stab));
978 #endif
979 gen_kill_estimator(&qdisc->rate_est);
980 if (ops->reset)
981 ops->reset(qdisc);
982 if (ops->destroy)
983 ops->destroy(qdisc);
985 module_put(ops->owner);
986 dev_put(qdisc_dev(qdisc));
988 skb_queue_walk_safe(&qdisc->gso_skb, skb, tmp) {
989 __skb_unlink(skb, &qdisc->gso_skb);
990 kfree_skb_list(skb);
993 skb_queue_walk_safe(&qdisc->skb_bad_txq, skb, tmp) {
994 __skb_unlink(skb, &qdisc->skb_bad_txq);
995 kfree_skb_list(skb);
998 qdisc_free(qdisc);
1000 EXPORT_SYMBOL(qdisc_destroy);
1002 /* Attach toplevel qdisc to device queue. */
1003 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
1004 struct Qdisc *qdisc)
1006 struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
1007 spinlock_t *root_lock;
1009 root_lock = qdisc_lock(oqdisc);
1010 spin_lock_bh(root_lock);
1012 /* ... and graft new one */
1013 if (qdisc == NULL)
1014 qdisc = &noop_qdisc;
1015 dev_queue->qdisc_sleeping = qdisc;
1016 rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
1018 spin_unlock_bh(root_lock);
1020 return oqdisc;
1022 EXPORT_SYMBOL(dev_graft_qdisc);
1024 static void attach_one_default_qdisc(struct net_device *dev,
1025 struct netdev_queue *dev_queue,
1026 void *_unused)
1028 struct Qdisc *qdisc;
1029 const struct Qdisc_ops *ops = default_qdisc_ops;
1031 if (dev->priv_flags & IFF_NO_QUEUE)
1032 ops = &noqueue_qdisc_ops;
1034 qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT, NULL);
1035 if (!qdisc) {
1036 netdev_info(dev, "activation failed\n");
1037 return;
1039 if (!netif_is_multiqueue(dev))
1040 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1041 dev_queue->qdisc_sleeping = qdisc;
1044 static void attach_default_qdiscs(struct net_device *dev)
1046 struct netdev_queue *txq;
1047 struct Qdisc *qdisc;
1049 txq = netdev_get_tx_queue(dev, 0);
1051 if (!netif_is_multiqueue(dev) ||
1052 dev->priv_flags & IFF_NO_QUEUE) {
1053 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
1054 dev->qdisc = txq->qdisc_sleeping;
1055 qdisc_refcount_inc(dev->qdisc);
1056 } else {
1057 qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT, NULL);
1058 if (qdisc) {
1059 dev->qdisc = qdisc;
1060 qdisc->ops->attach(qdisc);
1063 #ifdef CONFIG_NET_SCHED
1064 if (dev->qdisc != &noop_qdisc)
1065 qdisc_hash_add(dev->qdisc, false);
1066 #endif
1069 static void transition_one_qdisc(struct net_device *dev,
1070 struct netdev_queue *dev_queue,
1071 void *_need_watchdog)
1073 struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
1074 int *need_watchdog_p = _need_watchdog;
1076 if (!(new_qdisc->flags & TCQ_F_BUILTIN))
1077 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
1079 rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
1080 if (need_watchdog_p) {
1081 dev_queue->trans_start = 0;
1082 *need_watchdog_p = 1;
1086 void dev_activate(struct net_device *dev)
1088 int need_watchdog;
1090 /* No queueing discipline is attached to device;
1091 * create default one for devices, which need queueing
1092 * and noqueue_qdisc for virtual interfaces
1095 if (dev->qdisc == &noop_qdisc)
1096 attach_default_qdiscs(dev);
1098 if (!netif_carrier_ok(dev))
1099 /* Delay activation until next carrier-on event */
1100 return;
1102 need_watchdog = 0;
1103 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
1104 if (dev_ingress_queue(dev))
1105 transition_one_qdisc(dev, dev_ingress_queue(dev), NULL);
1107 if (need_watchdog) {
1108 netif_trans_update(dev);
1109 dev_watchdog_up(dev);
1112 EXPORT_SYMBOL(dev_activate);
1114 static void dev_deactivate_queue(struct net_device *dev,
1115 struct netdev_queue *dev_queue,
1116 void *_qdisc_default)
1118 struct Qdisc *qdisc = rtnl_dereference(dev_queue->qdisc);
1119 struct Qdisc *qdisc_default = _qdisc_default;
1121 if (qdisc) {
1122 if (!(qdisc->flags & TCQ_F_BUILTIN))
1123 set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
1125 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
1129 static void dev_reset_queue(struct net_device *dev,
1130 struct netdev_queue *dev_queue,
1131 void *_unused)
1133 struct Qdisc *qdisc;
1134 bool nolock;
1136 qdisc = dev_queue->qdisc_sleeping;
1137 if (!qdisc)
1138 return;
1140 nolock = qdisc->flags & TCQ_F_NOLOCK;
1142 if (nolock)
1143 spin_lock_bh(&qdisc->seqlock);
1144 spin_lock_bh(qdisc_lock(qdisc));
1146 qdisc_reset(qdisc);
1148 spin_unlock_bh(qdisc_lock(qdisc));
1149 if (nolock)
1150 spin_unlock_bh(&qdisc->seqlock);
1153 static bool some_qdisc_is_busy(struct net_device *dev)
1155 unsigned int i;
1157 for (i = 0; i < dev->num_tx_queues; i++) {
1158 struct netdev_queue *dev_queue;
1159 spinlock_t *root_lock;
1160 struct Qdisc *q;
1161 int val;
1163 dev_queue = netdev_get_tx_queue(dev, i);
1164 q = dev_queue->qdisc_sleeping;
1166 root_lock = qdisc_lock(q);
1167 spin_lock_bh(root_lock);
1169 val = (qdisc_is_running(q) ||
1170 test_bit(__QDISC_STATE_SCHED, &q->state));
1172 spin_unlock_bh(root_lock);
1174 if (val)
1175 return true;
1177 return false;
1180 static void dev_qdisc_reset(struct net_device *dev,
1181 struct netdev_queue *dev_queue,
1182 void *none)
1184 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
1186 if (qdisc)
1187 qdisc_reset(qdisc);
1191 * dev_deactivate_many - deactivate transmissions on several devices
1192 * @head: list of devices to deactivate
1194 * This function returns only when all outstanding transmissions
1195 * have completed, unless all devices are in dismantle phase.
1197 void dev_deactivate_many(struct list_head *head)
1199 struct net_device *dev;
1201 list_for_each_entry(dev, head, close_list) {
1202 netdev_for_each_tx_queue(dev, dev_deactivate_queue,
1203 &noop_qdisc);
1204 if (dev_ingress_queue(dev))
1205 dev_deactivate_queue(dev, dev_ingress_queue(dev),
1206 &noop_qdisc);
1208 dev_watchdog_down(dev);
1211 /* Wait for outstanding qdisc-less dev_queue_xmit calls or
1212 * outstanding qdisc enqueuing calls.
1213 * This is avoided if all devices are in dismantle phase :
1214 * Caller will call synchronize_net() for us
1216 synchronize_net();
1218 list_for_each_entry(dev, head, close_list) {
1219 netdev_for_each_tx_queue(dev, dev_reset_queue, NULL);
1221 if (dev_ingress_queue(dev))
1222 dev_reset_queue(dev, dev_ingress_queue(dev), NULL);
1225 /* Wait for outstanding qdisc_run calls. */
1226 list_for_each_entry(dev, head, close_list) {
1227 while (some_qdisc_is_busy(dev))
1228 yield();
1229 /* The new qdisc is assigned at this point so we can safely
1230 * unwind stale skb lists and qdisc statistics
1232 netdev_for_each_tx_queue(dev, dev_qdisc_reset, NULL);
1233 if (dev_ingress_queue(dev))
1234 dev_qdisc_reset(dev, dev_ingress_queue(dev), NULL);
1238 void dev_deactivate(struct net_device *dev)
1240 LIST_HEAD(single);
1242 list_add(&dev->close_list, &single);
1243 dev_deactivate_many(&single);
1244 list_del(&single);
1246 EXPORT_SYMBOL(dev_deactivate);
1248 static int qdisc_change_tx_queue_len(struct net_device *dev,
1249 struct netdev_queue *dev_queue)
1251 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
1252 const struct Qdisc_ops *ops = qdisc->ops;
1254 if (ops->change_tx_queue_len)
1255 return ops->change_tx_queue_len(qdisc, dev->tx_queue_len);
1256 return 0;
1259 int dev_qdisc_change_tx_queue_len(struct net_device *dev)
1261 bool up = dev->flags & IFF_UP;
1262 unsigned int i;
1263 int ret = 0;
1265 if (up)
1266 dev_deactivate(dev);
1268 for (i = 0; i < dev->num_tx_queues; i++) {
1269 ret = qdisc_change_tx_queue_len(dev, &dev->_tx[i]);
1271 /* TODO: revert changes on a partial failure */
1272 if (ret)
1273 break;
1276 if (up)
1277 dev_activate(dev);
1278 return ret;
1281 static void dev_init_scheduler_queue(struct net_device *dev,
1282 struct netdev_queue *dev_queue,
1283 void *_qdisc)
1285 struct Qdisc *qdisc = _qdisc;
1287 rcu_assign_pointer(dev_queue->qdisc, qdisc);
1288 dev_queue->qdisc_sleeping = qdisc;
1291 void dev_init_scheduler(struct net_device *dev)
1293 dev->qdisc = &noop_qdisc;
1294 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
1295 if (dev_ingress_queue(dev))
1296 dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
1298 timer_setup(&dev->watchdog_timer, dev_watchdog, 0);
1301 static void shutdown_scheduler_queue(struct net_device *dev,
1302 struct netdev_queue *dev_queue,
1303 void *_qdisc_default)
1305 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
1306 struct Qdisc *qdisc_default = _qdisc_default;
1308 if (qdisc) {
1309 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
1310 dev_queue->qdisc_sleeping = qdisc_default;
1312 qdisc_destroy(qdisc);
1316 void dev_shutdown(struct net_device *dev)
1318 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
1319 if (dev_ingress_queue(dev))
1320 shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
1321 qdisc_destroy(dev->qdisc);
1322 dev->qdisc = &noop_qdisc;
1324 WARN_ON(timer_pending(&dev->watchdog_timer));
1327 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1328 const struct tc_ratespec *conf,
1329 u64 rate64)
1331 memset(r, 0, sizeof(*r));
1332 r->overhead = conf->overhead;
1333 r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
1334 r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
1335 r->mult = 1;
1337 * The deal here is to replace a divide by a reciprocal one
1338 * in fast path (a reciprocal divide is a multiply and a shift)
1340 * Normal formula would be :
1341 * time_in_ns = (NSEC_PER_SEC * len) / rate_bps
1343 * We compute mult/shift to use instead :
1344 * time_in_ns = (len * mult) >> shift;
1346 * We try to get the highest possible mult value for accuracy,
1347 * but have to make sure no overflows will ever happen.
1349 if (r->rate_bytes_ps > 0) {
1350 u64 factor = NSEC_PER_SEC;
1352 for (;;) {
1353 r->mult = div64_u64(factor, r->rate_bytes_ps);
1354 if (r->mult & (1U << 31) || factor & (1ULL << 63))
1355 break;
1356 factor <<= 1;
1357 r->shift++;
1361 EXPORT_SYMBOL(psched_ratecfg_precompute);
1363 static void mini_qdisc_rcu_func(struct rcu_head *head)
1367 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1368 struct tcf_proto *tp_head)
1370 struct mini_Qdisc *miniq_old = rtnl_dereference(*miniqp->p_miniq);
1371 struct mini_Qdisc *miniq;
1373 if (!tp_head) {
1374 RCU_INIT_POINTER(*miniqp->p_miniq, NULL);
1375 /* Wait for flying RCU callback before it is freed. */
1376 rcu_barrier_bh();
1377 return;
1380 miniq = !miniq_old || miniq_old == &miniqp->miniq2 ?
1381 &miniqp->miniq1 : &miniqp->miniq2;
1383 /* We need to make sure that readers won't see the miniq
1384 * we are about to modify. So wait until previous call_rcu_bh callback
1385 * is done.
1387 rcu_barrier_bh();
1388 miniq->filter_list = tp_head;
1389 rcu_assign_pointer(*miniqp->p_miniq, miniq);
1391 if (miniq_old)
1392 /* This is counterpart of the rcu barriers above. We need to
1393 * block potential new user of miniq_old until all readers
1394 * are not seeing it.
1396 call_rcu_bh(&miniq_old->rcu, mini_qdisc_rcu_func);
1398 EXPORT_SYMBOL(mini_qdisc_pair_swap);
1400 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1401 struct mini_Qdisc __rcu **p_miniq)
1403 miniqp->miniq1.cpu_bstats = qdisc->cpu_bstats;
1404 miniqp->miniq1.cpu_qstats = qdisc->cpu_qstats;
1405 miniqp->miniq2.cpu_bstats = qdisc->cpu_bstats;
1406 miniqp->miniq2.cpu_qstats = qdisc->cpu_qstats;
1407 miniqp->p_miniq = p_miniq;
1409 EXPORT_SYMBOL(mini_qdisc_pair_init);