blob: 12a6e1a39fa00f836a30c4db2871ba4371d265d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
18#include <linux/sched.h>
19#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
nikolay@redhat.com07ce76a2013-08-03 22:07:47 +020028#include <linux/if_vlan.h>
John Fastabendc5ad1192017-12-07 09:58:19 -080029#include <linux/skb_array.h>
Chris Dion32d3e512017-12-06 10:50:28 -050030#include <linux/if_macvlan.h>
Jiri Pirko292f1c72013-02-12 00:12:03 +000031#include <net/sch_generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/pkt_sched.h>
Eric Dumazet7fee2262010-05-11 23:19:48 +000033#include <net/dst.h>
Jesper Dangaard Brouere5430022017-08-15 21:11:03 +020034#include <trace/events/qdisc.h>
Steffen Klassertf53c7232017-12-20 10:41:36 +010035#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
stephen hemminger34aedd32013-08-31 10:15:33 -070037/* Qdisc to use by default */
38const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops;
39EXPORT_SYMBOL(default_qdisc_ops);
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* Main transmission queue. */
42
Patrick McHardy0463d4a2007-04-16 17:02:10 -070043/* Modifications to data participating in scheduling must be protected with
David S. Miller5fb66222008-08-02 20:02:43 -070044 * qdisc_lock(qdisc) spinlock.
Patrick McHardy0463d4a2007-04-16 17:02:10 -070045 *
46 * The idea is the following:
David S. Millerc7e4f3b2008-07-16 03:22:39 -070047 * - enqueue, dequeue are serialized via qdisc root lock
48 * - ingress filtering is also serialized via qdisc root lock
Patrick McHardy0463d4a2007-04-16 17:02:10 -070049 * - updates to tree and tree walking are only done under the rtnl mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
John Fastabend70e57d52017-12-07 09:56:23 -080051
52static inline struct sk_buff *__skb_dequeue_bad_txq(struct Qdisc *q)
53{
54 const struct netdev_queue *txq = q->dev_queue;
55 spinlock_t *lock = NULL;
56 struct sk_buff *skb;
57
58 if (q->flags & TCQ_F_NOLOCK) {
59 lock = qdisc_lock(q);
60 spin_lock(lock);
61 }
62
63 skb = skb_peek(&q->skb_bad_txq);
64 if (skb) {
65 /* check the reason of requeuing without tx lock first */
66 txq = skb_get_tx_queue(txq->dev, skb);
67 if (!netif_xmit_frozen_or_stopped(txq)) {
68 skb = __skb_dequeue(&q->skb_bad_txq);
69 if (qdisc_is_percpu_stats(q)) {
70 qdisc_qstats_cpu_backlog_dec(q, skb);
Eric Dumazet46b1c182019-02-28 12:55:43 -080071 qdisc_qstats_atomic_qlen_dec(q);
John Fastabend70e57d52017-12-07 09:56:23 -080072 } else {
73 qdisc_qstats_backlog_dec(q, skb);
74 q->q.qlen--;
75 }
76 } else {
77 skb = NULL;
78 }
79 }
80
81 if (lock)
82 spin_unlock(lock);
83
84 return skb;
85}
86
87static inline struct sk_buff *qdisc_dequeue_skb_bad_txq(struct Qdisc *q)
88{
89 struct sk_buff *skb = skb_peek(&q->skb_bad_txq);
90
91 if (unlikely(skb))
92 skb = __skb_dequeue_bad_txq(q);
93
94 return skb;
95}
96
97static inline void qdisc_enqueue_skb_bad_txq(struct Qdisc *q,
98 struct sk_buff *skb)
99{
100 spinlock_t *lock = NULL;
101
102 if (q->flags & TCQ_F_NOLOCK) {
103 lock = qdisc_lock(q);
104 spin_lock(lock);
105 }
106
107 __skb_queue_tail(&q->skb_bad_txq, skb);
108
Eric Dumazetcce6294c2018-03-14 18:53:00 -0700109 if (qdisc_is_percpu_stats(q)) {
110 qdisc_qstats_cpu_backlog_inc(q, skb);
Eric Dumazet46b1c182019-02-28 12:55:43 -0800111 qdisc_qstats_atomic_qlen_inc(q);
Eric Dumazetcce6294c2018-03-14 18:53:00 -0700112 } else {
113 qdisc_qstats_backlog_inc(q, skb);
114 q->q.qlen++;
115 }
116
John Fastabend70e57d52017-12-07 09:56:23 -0800117 if (lock)
118 spin_unlock(lock);
119}
120
Paolo Abeni9c01c9f2019-04-10 14:32:39 +0200121static inline void dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700122{
Paolo Abeni9c01c9f2019-04-10 14:32:39 +0200123 spinlock_t *lock = NULL;
Wei Yongjun9540d972017-12-27 17:05:52 +0800124
Paolo Abeni9c01c9f2019-04-10 14:32:39 +0200125 if (q->flags & TCQ_F_NOLOCK) {
126 lock = qdisc_lock(q);
127 spin_lock(lock);
Wei Yongjun9540d972017-12-27 17:05:52 +0800128 }
Jarek Poplawski62523522008-10-06 10:41:50 -0700129
Wei Yongjun9540d972017-12-27 17:05:52 +0800130 while (skb) {
131 struct sk_buff *next = skb->next;
132
133 __skb_queue_tail(&q->gso_skb, skb);
134
Paolo Abeni9c01c9f2019-04-10 14:32:39 +0200135 /* it's still part of the queue */
136 if (qdisc_is_percpu_stats(q)) {
137 qdisc_qstats_cpu_requeues_inc(q);
138 qdisc_qstats_cpu_backlog_inc(q, skb);
139 qdisc_qstats_atomic_qlen_inc(q);
140 } else {
141 q->qstats.requeues++;
142 qdisc_qstats_backlog_inc(q, skb);
143 q->q.qlen++;
144 }
Wei Yongjun9540d972017-12-27 17:05:52 +0800145
146 skb = next;
147 }
Paolo Abeni9c01c9f2019-04-10 14:32:39 +0200148 if (lock)
149 spin_unlock(lock);
John Fastabenda53851e2017-12-07 09:55:45 -0800150 __netif_schedule(q);
John Fastabenda53851e2017-12-07 09:55:45 -0800151}
152
Eric Dumazet55a93b32014-10-03 15:31:07 -0700153static void try_bulk_dequeue_skb(struct Qdisc *q,
154 struct sk_buff *skb,
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200155 const struct netdev_queue *txq,
156 int *packets)
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +0200157{
Eric Dumazet55a93b32014-10-03 15:31:07 -0700158 int bytelimit = qdisc_avail_bulklimit(txq) - skb->len;
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +0200159
160 while (bytelimit > 0) {
Eric Dumazet55a93b32014-10-03 15:31:07 -0700161 struct sk_buff *nskb = q->dequeue(q);
162
163 if (!nskb)
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +0200164 break;
165
Eric Dumazet55a93b32014-10-03 15:31:07 -0700166 bytelimit -= nskb->len; /* covers GSO len */
167 skb->next = nskb;
168 skb = nskb;
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200169 (*packets)++; /* GSO counts as one pkt */
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +0200170 }
David S. Millera8305bf2018-07-29 20:42:53 -0700171 skb_mark_not_on_list(skb);
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +0200172}
173
Eric Dumazet4d202a02016-06-21 23:16:52 -0700174/* This variant of try_bulk_dequeue_skb() makes sure
175 * all skbs in the chain are for the same txq
176 */
177static void try_bulk_dequeue_skb_slow(struct Qdisc *q,
178 struct sk_buff *skb,
179 int *packets)
180{
181 int mapping = skb_get_queue_mapping(skb);
182 struct sk_buff *nskb;
183 int cnt = 0;
184
185 do {
186 nskb = q->dequeue(q);
187 if (!nskb)
188 break;
189 if (unlikely(skb_get_queue_mapping(nskb) != mapping)) {
John Fastabend70e57d52017-12-07 09:56:23 -0800190 qdisc_enqueue_skb_bad_txq(q, nskb);
Eric Dumazet4d202a02016-06-21 23:16:52 -0700191 break;
192 }
193 skb->next = nskb;
194 skb = nskb;
195 } while (++cnt < 8);
196 (*packets) += cnt;
David S. Millera8305bf2018-07-29 20:42:53 -0700197 skb_mark_not_on_list(skb);
Eric Dumazet4d202a02016-06-21 23:16:52 -0700198}
199
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +0200200/* Note that dequeue_skb can possibly return a SKB list (via skb->next).
201 * A requeued skb (via q->gso_skb) can also be a SKB list.
202 */
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200203static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
204 int *packets)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700205{
Eric Dumazet1abbe132012-12-11 15:54:33 +0000206 const struct netdev_queue *txq = q->dev_queue;
John Fastabendfd8e8d12017-12-07 09:56:42 -0800207 struct sk_buff *skb = NULL;
Jarek Poplawski554794d2008-10-06 09:54:39 -0700208
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200209 *packets = 1;
John Fastabenda53851e2017-12-07 09:55:45 -0800210 if (unlikely(!skb_queue_empty(&q->gso_skb))) {
211 spinlock_t *lock = NULL;
212
213 if (q->flags & TCQ_F_NOLOCK) {
214 lock = qdisc_lock(q);
215 spin_lock(lock);
216 }
217
218 skb = skb_peek(&q->gso_skb);
219
220 /* skb may be null if another cpu pulls gso_skb off in between
221 * empty check and lock.
222 */
223 if (!skb) {
224 if (lock)
225 spin_unlock(lock);
226 goto validate;
227 }
228
Eric Dumazet4d202a02016-06-21 23:16:52 -0700229 /* skb in gso_skb were already validated */
230 *validate = false;
Steffen Klassertf53c7232017-12-20 10:41:36 +0100231 if (xfrm_offload(skb))
232 *validate = true;
Jarek Poplawskiebf05982008-09-22 22:16:23 -0700233 /* check the reason of requeuing without tx lock first */
Daniel Borkmann10c51b56232014-08-27 11:11:27 +0200234 txq = skb_get_tx_queue(txq->dev, skb);
Tom Herbert734664982011-11-28 16:32:44 +0000235 if (!netif_xmit_frozen_or_stopped(txq)) {
John Fastabenda53851e2017-12-07 09:55:45 -0800236 skb = __skb_dequeue(&q->gso_skb);
237 if (qdisc_is_percpu_stats(q)) {
238 qdisc_qstats_cpu_backlog_dec(q, skb);
Eric Dumazet46b1c182019-02-28 12:55:43 -0800239 qdisc_qstats_atomic_qlen_dec(q);
John Fastabenda53851e2017-12-07 09:55:45 -0800240 } else {
241 qdisc_qstats_backlog_dec(q, skb);
242 q->q.qlen--;
243 }
244 } else {
Jarek Poplawskiebf05982008-09-22 22:16:23 -0700245 skb = NULL;
John Fastabenda53851e2017-12-07 09:55:45 -0800246 }
247 if (lock)
248 spin_unlock(lock);
Jesper Dangaard Brouere5430022017-08-15 21:11:03 +0200249 goto trace;
Eric Dumazet4d202a02016-06-21 23:16:52 -0700250 }
John Fastabenda53851e2017-12-07 09:55:45 -0800251validate:
Eric Dumazet4d202a02016-06-21 23:16:52 -0700252 *validate = true;
John Fastabendfd8e8d12017-12-07 09:56:42 -0800253
254 if ((q->flags & TCQ_F_ONETXQUEUE) &&
255 netif_xmit_frozen_or_stopped(txq))
256 return skb;
257
John Fastabend70e57d52017-12-07 09:56:23 -0800258 skb = qdisc_dequeue_skb_bad_txq(q);
259 if (unlikely(skb))
260 goto bulk;
John Fastabendfd8e8d12017-12-07 09:56:42 -0800261 skb = q->dequeue(q);
Eric Dumazet4d202a02016-06-21 23:16:52 -0700262 if (skb) {
263bulk:
264 if (qdisc_may_bulk(q))
265 try_bulk_dequeue_skb(q, skb, txq, packets);
266 else
267 try_bulk_dequeue_skb_slow(q, skb, packets);
Jarek Poplawskiebf05982008-09-22 22:16:23 -0700268 }
Jesper Dangaard Brouere5430022017-08-15 21:11:03 +0200269trace:
270 trace_qdisc_dequeue(q, txq, *packets, skb);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700271 return skb;
272}
273
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900274/*
Jesper Dangaard Brouer10770bc2014-09-02 16:35:33 +0200275 * Transmit possibly several skbs, and handle the return status as
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -0700276 * required. Owning running seqcount bit guarantees that
Jesper Dangaard Brouer10770bc2014-09-02 16:35:33 +0200277 * only one CPU can execute this function.
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700278 *
279 * Returns to the caller:
John Fastabend29b86cd2017-12-07 09:54:47 -0800280 * false - hardware queue frozen backoff
281 * true - feel free to send more pkts
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700282 */
John Fastabend29b86cd2017-12-07 09:54:47 -0800283bool sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
284 struct net_device *dev, struct netdev_queue *txq,
285 spinlock_t *root_lock, bool validate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Peter P Waskiewicz Jr5f1a4852007-11-13 20:40:55 -0800287 int ret = NETDEV_TX_BUSY;
Steffen Klassertf53c7232017-12-20 10:41:36 +0100288 bool again = false;
David S. Miller7698b4f2008-07-16 01:42:40 -0700289
290 /* And release qdisc */
John Fastabend6b3ba912017-12-07 09:54:25 -0800291 if (root_lock)
292 spin_unlock(root_lock);
Herbert Xud90df3a2007-05-10 04:55:14 -0700293
Eric Dumazet55a93b32014-10-03 15:31:07 -0700294 /* Note that we validate skb (GSO, checksum, ...) outside of locks */
295 if (validate)
Steffen Klassertf53c7232017-12-20 10:41:36 +0100296 skb = validate_xmit_skb_list(skb, dev, &again);
297
298#ifdef CONFIG_XFRM_OFFLOAD
299 if (unlikely(again)) {
300 if (root_lock)
301 spin_lock(root_lock);
302
303 dev_requeue_skb(skb, q);
304 return false;
305 }
306#endif
Patrick McHardy572a9d72009-11-10 06:14:14 +0000307
Lars Persson3dcd493fb2016-04-12 08:45:52 +0200308 if (likely(skb)) {
Eric Dumazet55a93b32014-10-03 15:31:07 -0700309 HARD_TX_LOCK(dev, txq, smp_processor_id());
310 if (!netif_xmit_frozen_or_stopped(txq))
311 skb = dev_hard_start_xmit(skb, dev, txq, &ret);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700312
Eric Dumazet55a93b32014-10-03 15:31:07 -0700313 HARD_TX_UNLOCK(dev, txq);
Lars Persson3dcd493fb2016-04-12 08:45:52 +0200314 } else {
John Fastabend6b3ba912017-12-07 09:54:25 -0800315 if (root_lock)
316 spin_lock(root_lock);
John Fastabend29b86cd2017-12-07 09:54:47 -0800317 return true;
Eric Dumazet55a93b32014-10-03 15:31:07 -0700318 }
John Fastabend6b3ba912017-12-07 09:54:25 -0800319
320 if (root_lock)
321 spin_lock(root_lock);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700322
John Fastabend29b86cd2017-12-07 09:54:47 -0800323 if (!dev_xmit_complete(ret)) {
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700324 /* Driver returned NETDEV_TX_BUSY - requeue skb */
Joe Perchese87cc472012-05-13 21:56:26 +0000325 if (unlikely(ret != NETDEV_TX_BUSY))
326 net_warn_ratelimited("BUG %s code %d qlen %d\n",
327 dev->name, ret, q->q.qlen);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700328
John Fastabend29b86cd2017-12-07 09:54:47 -0800329 dev_requeue_skb(skb, q);
330 return false;
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700331 }
332
John Fastabend29b86cd2017-12-07 09:54:47 -0800333 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000336/*
337 * NOTE: Called under qdisc_lock(q) with locally disabled BH.
338 *
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -0700339 * running seqcount guarantees only one CPU can process
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000340 * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
341 * this queue.
342 *
343 * netif_tx_lock serializes accesses to device driver.
344 *
345 * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
346 * if one is grabbed, another must be free.
347 *
348 * Note, that this procedure can be called by a watchdog timer
349 *
350 * Returns to the caller:
351 * 0 - queue is empty or throttled.
352 * >0 - queue is not empty.
353 *
354 */
John Fastabend29b86cd2017-12-07 09:54:47 -0800355static inline bool qdisc_restart(struct Qdisc *q, int *packets)
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000356{
John Fastabend6b3ba912017-12-07 09:54:25 -0800357 spinlock_t *root_lock = NULL;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000358 struct netdev_queue *txq;
359 struct net_device *dev;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000360 struct sk_buff *skb;
Paolo Abeni32f7b442018-05-15 10:50:31 +0200361 bool validate;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000362
363 /* Dequeue packet */
John Fastabendeb82a992018-03-24 22:25:06 -0700364 skb = dequeue_skb(q, &validate, packets);
Paolo Abeni32f7b442018-05-15 10:50:31 +0200365 if (unlikely(!skb))
John Fastabendeb82a992018-03-24 22:25:06 -0700366 return false;
John Fastabendeb82a992018-03-24 22:25:06 -0700367
Paolo Abeni32f7b442018-05-15 10:50:31 +0200368 if (!(q->flags & TCQ_F_NOLOCK))
John Fastabend6b3ba912017-12-07 09:54:25 -0800369 root_lock = qdisc_lock(q);
370
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000371 dev = qdisc_dev(q);
Daniel Borkmann10c51b56232014-08-27 11:11:27 +0200372 txq = skb_get_tx_queue(dev, skb);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000373
Paolo Abeni32f7b442018-05-15 10:50:31 +0200374 return sch_direct_xmit(skb, q, dev, txq, root_lock, validate);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000375}
376
David S. Miller37437bb2008-07-16 02:15:04 -0700377void __qdisc_run(struct Qdisc *q)
Herbert Xu48d83322006-06-19 23:57:59 -0700378{
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +0100379 int quota = dev_tx_weight;
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200380 int packets;
Herbert Xu2ba25062008-03-28 16:25:26 -0700381
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200382 while (qdisc_restart(q, &packets)) {
Herbert Xu2ba25062008-03-28 16:25:26 -0700383 /*
jamald5b8aa12011-06-26 08:13:54 +0000384 * Ordered by possible occurrence: Postpone processing if
385 * 1. we've exceeded packet quota
386 * 2. another process needs the CPU;
Herbert Xu2ba25062008-03-28 16:25:26 -0700387 */
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200388 quota -= packets;
389 if (quota <= 0 || need_resched()) {
David S. Miller37437bb2008-07-16 02:15:04 -0700390 __netif_schedule(q);
Herbert Xu2ba25062008-03-28 16:25:26 -0700391 break;
392 }
393 }
Herbert Xu48d83322006-06-19 23:57:59 -0700394}
395
Eric Dumazet9d214932009-05-17 20:55:16 -0700396unsigned long dev_trans_start(struct net_device *dev)
397{
nikolay@redhat.com07ce76a2013-08-03 22:07:47 +0200398 unsigned long val, res;
Eric Dumazet9d214932009-05-17 20:55:16 -0700399 unsigned int i;
400
nikolay@redhat.com07ce76a2013-08-03 22:07:47 +0200401 if (is_vlan_dev(dev))
402 dev = vlan_dev_real_dev(dev);
Chris Dion32d3e512017-12-06 10:50:28 -0500403 else if (netif_is_macvlan(dev))
404 dev = macvlan_dev_real_dev(dev);
Florian Westphal9b366272016-05-03 16:33:14 +0200405 res = netdev_get_tx_queue(dev, 0)->trans_start;
406 for (i = 1; i < dev->num_tx_queues; i++) {
Eric Dumazet9d214932009-05-17 20:55:16 -0700407 val = netdev_get_tx_queue(dev, i)->trans_start;
408 if (val && time_after(val, res))
409 res = val;
410 }
nikolay@redhat.com07ce76a2013-08-03 22:07:47 +0200411
Eric Dumazet9d214932009-05-17 20:55:16 -0700412 return res;
413}
414EXPORT_SYMBOL(dev_trans_start);
415
Kees Cookcdeabbb2017-10-16 17:29:17 -0700416static void dev_watchdog(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Kees Cookcdeabbb2017-10-16 17:29:17 -0700418 struct net_device *dev = from_timer(dev, t, watchdog_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Herbert Xu932ff272006-06-09 12:20:56 -0700420 netif_tx_lock(dev);
David S. Millere8a04642008-07-17 00:34:19 -0700421 if (!qdisc_tx_is_noop(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (netif_device_present(dev) &&
423 netif_running(dev) &&
424 netif_carrier_ok(dev)) {
Eric Dumazet9d214932009-05-17 20:55:16 -0700425 int some_queue_timedout = 0;
David S. Millere8a04642008-07-17 00:34:19 -0700426 unsigned int i;
Eric Dumazet9d214932009-05-17 20:55:16 -0700427 unsigned long trans_start;
Stephen Hemminger338f7562006-05-16 15:02:12 -0700428
David S. Millere8a04642008-07-17 00:34:19 -0700429 for (i = 0; i < dev->num_tx_queues; i++) {
430 struct netdev_queue *txq;
431
432 txq = netdev_get_tx_queue(dev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200433 trans_start = txq->trans_start;
Tom Herbert734664982011-11-28 16:32:44 +0000434 if (netif_xmit_stopped(txq) &&
Eric Dumazet9d214932009-05-17 20:55:16 -0700435 time_after(jiffies, (trans_start +
436 dev->watchdog_timeo))) {
437 some_queue_timedout = 1;
david decotignyccf5ff62011-11-16 12:15:10 +0000438 txq->trans_timeout++;
David S. Millere8a04642008-07-17 00:34:19 -0700439 break;
440 }
441 }
442
Eric Dumazet9d214932009-05-17 20:55:16 -0700443 if (some_queue_timedout) {
Eric Dumazet9d214932009-05-17 20:55:16 -0700444 WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
David S. Miller3019de12011-06-06 16:41:33 -0700445 dev->name, netdev_drivername(dev), i);
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800446 dev->netdev_ops->ndo_tx_timeout(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
David S. Millere8a04642008-07-17 00:34:19 -0700448 if (!mod_timer(&dev->watchdog_timer,
449 round_jiffies(jiffies +
450 dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 dev_hold(dev);
452 }
453 }
Herbert Xu932ff272006-06-09 12:20:56 -0700454 netif_tx_unlock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 dev_put(dev);
457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459void __netdev_watchdog_up(struct net_device *dev)
460{
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800461 if (dev->netdev_ops->ndo_tx_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (dev->watchdog_timeo <= 0)
463 dev->watchdog_timeo = 5*HZ;
Venkatesh Pallipadi60468d52007-05-31 21:28:44 -0700464 if (!mod_timer(&dev->watchdog_timer,
465 round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 dev_hold(dev);
467 }
468}
469
470static void dev_watchdog_up(struct net_device *dev)
471{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 __netdev_watchdog_up(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475static void dev_watchdog_down(struct net_device *dev)
476{
Herbert Xu932ff272006-06-09 12:20:56 -0700477 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (del_timer(&dev->watchdog_timer))
Stephen Hemminger15333062006-03-20 22:32:28 -0800479 dev_put(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700480 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700483/**
484 * netif_carrier_on - set carrier
485 * @dev: network device
486 *
Jouke Witteveen989723b2019-02-07 17:14:32 +0100487 * Device has detected acquisition of carrier.
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700488 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700489void netif_carrier_on(struct net_device *dev)
490{
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700491 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
David S. Millerb4730012008-11-19 15:33:54 -0800492 if (dev->reg_state == NETREG_UNINITIALIZED)
493 return;
David Decotignyb2d3bcf2018-01-18 09:59:13 -0800494 atomic_inc(&dev->carrier_up_count);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700495 linkwatch_fire_event(dev);
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700496 if (netif_running(dev))
497 __netdev_watchdog_up(dev);
498 }
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700499}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800500EXPORT_SYMBOL(netif_carrier_on);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700501
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700502/**
503 * netif_carrier_off - clear carrier
504 * @dev: network device
505 *
506 * Device has detected loss of carrier.
507 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700508void netif_carrier_off(struct net_device *dev)
509{
David S. Millerb4730012008-11-19 15:33:54 -0800510 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
511 if (dev->reg_state == NETREG_UNINITIALIZED)
512 return;
David Decotignyb2d3bcf2018-01-18 09:59:13 -0800513 atomic_inc(&dev->carrier_down_count);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700514 linkwatch_fire_event(dev);
David S. Millerb4730012008-11-19 15:33:54 -0800515 }
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700516}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800517EXPORT_SYMBOL(netif_carrier_off);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/* "NOOP" scheduler: the best scheduler, recommended for all interfaces
520 under all circumstances. It is difficult to invent anything faster or
521 cheaper.
522 */
523
Eric Dumazet520ac302016-06-21 23:16:49 -0700524static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
525 struct sk_buff **to_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Eric Dumazet520ac302016-06-21 23:16:49 -0700527 __qdisc_drop(skb, to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return NET_XMIT_CN;
529}
530
Yang Yingliang82d567c2013-12-10 20:55:31 +0800531static struct sk_buff *noop_dequeue(struct Qdisc *qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 return NULL;
534}
535
Eric Dumazet20fea082007-11-14 01:44:41 -0800536struct Qdisc_ops noop_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 .id = "noop",
538 .priv_size = 0,
539 .enqueue = noop_enqueue,
540 .dequeue = noop_dequeue,
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700541 .peek = noop_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 .owner = THIS_MODULE,
543};
544
David S. Miller7698b4f2008-07-16 01:42:40 -0700545static struct netdev_queue noop_netdev_queue = {
Li RongQing3b40bf42019-02-25 10:43:06 +0800546 RCU_POINTER_INITIALIZER(qdisc, &noop_qdisc),
Jarek Poplawski9f3ffae2008-10-19 23:37:47 -0700547 .qdisc_sleeping = &noop_qdisc,
David S. Miller7698b4f2008-07-16 01:42:40 -0700548};
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550struct Qdisc noop_qdisc = {
551 .enqueue = noop_enqueue,
552 .dequeue = noop_dequeue,
553 .flags = TCQ_F_BUILTIN,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900554 .ops = &noop_qdisc_ops,
David S. Miller838740002008-07-17 00:53:03 -0700555 .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
David S. Miller7698b4f2008-07-16 01:42:40 -0700556 .dev_queue = &noop_netdev_queue,
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -0700557 .running = SEQCNT_ZERO(noop_qdisc.running),
Eric Dumazet7b5edbc2010-10-15 19:22:34 +0000558 .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock),
Eric Dumazetf98ebd42018-10-09 15:20:50 -0700559 .gso_skb = {
560 .next = (struct sk_buff *)&noop_qdisc.gso_skb,
561 .prev = (struct sk_buff *)&noop_qdisc.gso_skb,
562 .qlen = 0,
563 .lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.gso_skb.lock),
564 },
565 .skb_bad_txq = {
566 .next = (struct sk_buff *)&noop_qdisc.skb_bad_txq,
567 .prev = (struct sk_buff *)&noop_qdisc.skb_bad_txq,
568 .qlen = 0,
569 .lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.skb_bad_txq.lock),
570 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571};
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800572EXPORT_SYMBOL(noop_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Alexander Aringe63d7df2017-12-20 12:35:13 -0500574static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt,
575 struct netlink_ext_ack *extack)
Phil Sutterd66d6c32015-08-27 21:21:38 +0200576{
577 /* register_qdisc() assigns a default of noop_enqueue if unset,
578 * but __dev_queue_xmit() treats noqueue only as such
579 * if this is NULL - so clear it here. */
580 qdisc->enqueue = NULL;
581 return 0;
582}
583
584struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 .id = "noqueue",
586 .priv_size = 0,
Phil Sutterd66d6c32015-08-27 21:21:38 +0200587 .init = noqueue_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 .enqueue = noop_enqueue,
589 .dequeue = noop_dequeue,
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700590 .peek = noop_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 .owner = THIS_MODULE,
592};
593
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000594static const u8 prio2band[TC_PRIO_MAX + 1] = {
595 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
596};
Thomas Graf321090e2005-06-18 22:58:35 -0700597
David S. Millerd3678b42008-07-21 09:56:13 -0700598/* 3-band FIFO queue: old style, but should be a bit faster than
599 generic prio+fifo combination.
600 */
601
602#define PFIFO_FAST_BANDS 3
603
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000604/*
605 * Private data for a pfifo_fast scheduler containing:
John Fastabendc5ad1192017-12-07 09:58:19 -0800606 * - rings for priority bands
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000607 */
608struct pfifo_fast_priv {
John Fastabendc5ad1192017-12-07 09:58:19 -0800609 struct skb_array q[PFIFO_FAST_BANDS];
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000610};
611
John Fastabendc5ad1192017-12-07 09:58:19 -0800612static inline struct skb_array *band2list(struct pfifo_fast_priv *priv,
613 int band)
David S. Millerd3678b42008-07-21 09:56:13 -0700614{
John Fastabendc5ad1192017-12-07 09:58:19 -0800615 return &priv->q[band];
David S. Millerd3678b42008-07-21 09:56:13 -0700616}
617
Eric Dumazet520ac302016-06-21 23:16:49 -0700618static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
619 struct sk_buff **to_free)
David S. Millerd3678b42008-07-21 09:56:13 -0700620{
John Fastabendc5ad1192017-12-07 09:58:19 -0800621 int band = prio2band[skb->priority & TC_PRIO_MAX];
622 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
623 struct skb_array *q = band2list(priv, band);
Eric Dumazetcce6294c2018-03-14 18:53:00 -0700624 unsigned int pkt_len = qdisc_pkt_len(skb);
John Fastabendc5ad1192017-12-07 09:58:19 -0800625 int err;
David S. Millerd3678b42008-07-21 09:56:13 -0700626
John Fastabendc5ad1192017-12-07 09:58:19 -0800627 err = skb_array_produce(q, skb);
Thomas Graf821d24ae2005-06-18 22:58:15 -0700628
John Fastabendc5ad1192017-12-07 09:58:19 -0800629 if (unlikely(err))
630 return qdisc_drop_cpu(skb, qdisc, to_free);
631
Paolo Abeni8a53e612019-04-10 14:32:40 +0200632 qdisc_update_stats_at_enqueue(qdisc, pkt_len);
John Fastabendc5ad1192017-12-07 09:58:19 -0800633 return NET_XMIT_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000636static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000638 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
John Fastabendc5ad1192017-12-07 09:58:19 -0800639 struct sk_buff *skb = NULL;
640 int band;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
John Fastabendc5ad1192017-12-07 09:58:19 -0800642 for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
643 struct skb_array *q = band2list(priv, band);
Florian Westphalec323362016-09-18 00:57:32 +0200644
John Fastabendc5ad1192017-12-07 09:58:19 -0800645 if (__skb_array_empty(q))
646 continue;
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000647
Paolo Abeni021a17e2018-05-15 16:24:37 +0200648 skb = __skb_array_consume(q);
John Fastabendc5ad1192017-12-07 09:58:19 -0800649 }
650 if (likely(skb)) {
Paolo Abeni8a53e612019-04-10 14:32:40 +0200651 qdisc_update_stats_at_dequeue(qdisc, skb);
Paolo Abeni28cff532019-03-22 16:01:55 +0100652 } else {
653 qdisc->empty = true;
David S. Millerd3678b42008-07-21 09:56:13 -0700654 }
Thomas Graff87a9c32005-06-18 22:58:53 -0700655
John Fastabendc5ad1192017-12-07 09:58:19 -0800656 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000659static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700660{
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000661 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
John Fastabendc5ad1192017-12-07 09:58:19 -0800662 struct sk_buff *skb = NULL;
663 int band;
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700664
John Fastabendc5ad1192017-12-07 09:58:19 -0800665 for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
666 struct skb_array *q = band2list(priv, band);
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000667
John Fastabendc5ad1192017-12-07 09:58:19 -0800668 skb = __skb_array_peek(q);
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700669 }
670
John Fastabendc5ad1192017-12-07 09:58:19 -0800671 return skb;
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700672}
673
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000674static void pfifo_fast_reset(struct Qdisc *qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
John Fastabendc5ad1192017-12-07 09:58:19 -0800676 int i, band;
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000677 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
David S. Millerd3678b42008-07-21 09:56:13 -0700678
John Fastabendc5ad1192017-12-07 09:58:19 -0800679 for (band = 0; band < PFIFO_FAST_BANDS; band++) {
680 struct skb_array *q = band2list(priv, band);
681 struct sk_buff *skb;
David S. Millerd3678b42008-07-21 09:56:13 -0700682
Cong Wang1df94c32017-12-18 14:34:26 -0800683 /* NULL ring is possible if destroy path is due to a failed
684 * skb_array_init() in pfifo_fast_init() case.
685 */
686 if (!q->ring.queue)
687 continue;
688
Paolo Abeni021a17e2018-05-15 16:24:37 +0200689 while ((skb = __skb_array_consume(q)) != NULL)
John Fastabendc5ad1192017-12-07 09:58:19 -0800690 kfree_skb(skb);
691 }
692
693 for_each_possible_cpu(i) {
694 struct gnet_stats_queue *q = per_cpu_ptr(qdisc->cpu_qstats, i);
695
696 q->backlog = 0;
John Fastabendc5ad1192017-12-07 09:58:19 -0800697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
David S. Millerd3678b42008-07-21 09:56:13 -0700700static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
701{
702 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
703
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000704 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1);
David S. Miller1b34ec42012-03-29 05:11:39 -0400705 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
706 goto nla_put_failure;
David S. Millerd3678b42008-07-21 09:56:13 -0700707 return skb->len;
708
709nla_put_failure:
710 return -1;
711}
712
Alexander Aringe63d7df2017-12-20 12:35:13 -0500713static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt,
714 struct netlink_ext_ack *extack)
David S. Millerd3678b42008-07-21 09:56:13 -0700715{
John Fastabendc5ad1192017-12-07 09:58:19 -0800716 unsigned int qlen = qdisc_dev(qdisc)->tx_queue_len;
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000717 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
John Fastabendc5ad1192017-12-07 09:58:19 -0800718 int prio;
David S. Millerd3678b42008-07-21 09:56:13 -0700719
John Fastabendc5ad1192017-12-07 09:58:19 -0800720 /* guard against zero length rings */
721 if (!qlen)
722 return -EINVAL;
723
724 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
725 struct skb_array *q = band2list(priv, prio);
726 int err;
727
728 err = skb_array_init(q, qlen, GFP_KERNEL);
729 if (err)
730 return -ENOMEM;
731 }
David S. Millerd3678b42008-07-21 09:56:13 -0700732
Eric Dumazet23624932011-01-21 16:26:09 -0800733 /* Can by-pass the queue discipline */
734 qdisc->flags |= TCQ_F_CAN_BYPASS;
David S. Millerd3678b42008-07-21 09:56:13 -0700735 return 0;
736}
737
John Fastabendc5ad1192017-12-07 09:58:19 -0800738static void pfifo_fast_destroy(struct Qdisc *sch)
739{
740 struct pfifo_fast_priv *priv = qdisc_priv(sch);
741 int prio;
742
743 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
744 struct skb_array *q = band2list(priv, prio);
745
746 /* NULL ring is possible if destroy path is due to a failed
747 * skb_array_init() in pfifo_fast_init() case.
748 */
Cong Wang1df94c32017-12-18 14:34:26 -0800749 if (!q->ring.queue)
John Fastabendc5ad1192017-12-07 09:58:19 -0800750 continue;
751 /* Destroy ring but no need to kfree_skb because a call to
752 * pfifo_fast_reset() has already done that work.
753 */
754 ptr_ring_cleanup(&q->ring, NULL);
755 }
756}
757
Cong Wang7007ba62018-01-25 18:26:24 -0800758static int pfifo_fast_change_tx_queue_len(struct Qdisc *sch,
759 unsigned int new_len)
760{
761 struct pfifo_fast_priv *priv = qdisc_priv(sch);
762 struct skb_array *bands[PFIFO_FAST_BANDS];
763 int prio;
764
765 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
766 struct skb_array *q = band2list(priv, prio);
767
768 bands[prio] = q;
769 }
770
771 return skb_array_resize_multiple(bands, PFIFO_FAST_BANDS, new_len,
772 GFP_KERNEL);
773}
774
David S. Miller6ec1c692009-09-06 01:58:51 -0700775struct Qdisc_ops pfifo_fast_ops __read_mostly = {
David S. Millerd3678b42008-07-21 09:56:13 -0700776 .id = "pfifo_fast",
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000777 .priv_size = sizeof(struct pfifo_fast_priv),
David S. Millerd3678b42008-07-21 09:56:13 -0700778 .enqueue = pfifo_fast_enqueue,
779 .dequeue = pfifo_fast_dequeue,
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700780 .peek = pfifo_fast_peek,
David S. Millerd3678b42008-07-21 09:56:13 -0700781 .init = pfifo_fast_init,
John Fastabendc5ad1192017-12-07 09:58:19 -0800782 .destroy = pfifo_fast_destroy,
David S. Millerd3678b42008-07-21 09:56:13 -0700783 .reset = pfifo_fast_reset,
784 .dump = pfifo_fast_dump,
Cong Wang7007ba62018-01-25 18:26:24 -0800785 .change_tx_queue_len = pfifo_fast_change_tx_queue_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 .owner = THIS_MODULE,
John Fastabendc5ad1192017-12-07 09:58:19 -0800787 .static_flags = TCQ_F_NOLOCK | TCQ_F_CPUSTATS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788};
Eric Dumazet1f27cde2016-03-02 08:21:43 -0800789EXPORT_SYMBOL(pfifo_fast_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Eric Dumazet23d3b8b2012-09-05 01:02:56 +0000791static struct lock_class_key qdisc_tx_busylock;
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -0700792static struct lock_class_key qdisc_running_key;
Eric Dumazet23d3b8b2012-09-05 01:02:56 +0000793
David S. Miller5ce2d482008-07-08 17:06:30 -0700794struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
Alexander Aringd0bd6842017-12-20 12:35:20 -0500795 const struct Qdisc_ops *ops,
796 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 void *p;
799 struct Qdisc *sch;
Eric Dumazetd2760552011-03-03 11:10:02 -0800800 unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size;
Thomas Graf3d54b822005-07-05 14:15:09 -0700801 int err = -ENOBUFS;
Jesus Sanchez-Palencia26aa0452017-10-16 18:01:23 -0700802 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Jesus Sanchez-Palencia26aa0452017-10-16 18:01:23 -0700804 if (!dev_queue) {
Alexander Aringd0bd6842017-12-20 12:35:20 -0500805 NL_SET_ERR_MSG(extack, "No device queue given");
Jesus Sanchez-Palencia26aa0452017-10-16 18:01:23 -0700806 err = -EINVAL;
807 goto errout;
808 }
809
810 dev = dev_queue->dev;
Eric Dumazetf2cd2d32010-11-29 08:14:37 +0000811 p = kzalloc_node(size, GFP_KERNEL,
812 netdev_queue_numa_node_read(dev_queue));
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (!p)
Thomas Graf3d54b822005-07-05 14:15:09 -0700815 goto errout;
Thomas Graf3d54b822005-07-05 14:15:09 -0700816 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
Eric Dumazetd2760552011-03-03 11:10:02 -0800817 /* if we got non aligned memory, ask more and do alignment ourself */
818 if (sch != p) {
819 kfree(p);
820 p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL,
821 netdev_queue_numa_node_read(dev_queue));
822 if (!p)
823 goto errout;
824 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
825 sch->padded = (char *) sch - (char *) p;
826 }
John Fastabenda53851e2017-12-07 09:55:45 -0800827 __skb_queue_head_init(&sch->gso_skb);
John Fastabend70e57d52017-12-07 09:56:23 -0800828 __skb_queue_head_init(&sch->skb_bad_txq);
Florian Westphal48da34b2016-09-18 00:57:34 +0200829 qdisc_skb_head_init(&sch->q);
830 spin_lock_init(&sch->q.lock);
Eric Dumazet23d3b8b2012-09-05 01:02:56 +0000831
John Fastabendd59f5ff2017-12-07 09:55:26 -0800832 if (ops->static_flags & TCQ_F_CPUSTATS) {
833 sch->cpu_bstats =
834 netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
835 if (!sch->cpu_bstats)
836 goto errout1;
837
838 sch->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
839 if (!sch->cpu_qstats) {
840 free_percpu(sch->cpu_bstats);
841 goto errout1;
842 }
843 }
844
Eric Dumazet79640a42010-06-02 05:09:29 -0700845 spin_lock_init(&sch->busylock);
Eric Dumazet23d3b8b2012-09-05 01:02:56 +0000846 lockdep_set_class(&sch->busylock,
847 dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
848
Paolo Abeni96009c72018-05-15 16:24:36 +0200849 /* seqlock has the same scope of busylock, for NOLOCK qdisc */
850 spin_lock_init(&sch->seqlock);
851 lockdep_set_class(&sch->busylock,
852 dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
853
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -0700854 seqcount_init(&sch->running);
855 lockdep_set_class(&sch->running,
856 dev->qdisc_running_key ?: &qdisc_running_key);
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 sch->ops = ops;
John Fastabendd59f5ff2017-12-07 09:55:26 -0800859 sch->flags = ops->static_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 sch->enqueue = ops->enqueue;
861 sch->dequeue = ops->dequeue;
David S. Millerbb949fb2008-07-08 16:55:56 -0700862 sch->dev_queue = dev_queue;
Paolo Abeni28cff532019-03-22 16:01:55 +0100863 sch->empty = true;
Eric Dumazet23d3b8b2012-09-05 01:02:56 +0000864 dev_hold(dev);
Reshetova, Elena7b936402017-07-04 15:53:07 +0300865 refcount_set(&sch->refcnt, 1);
Thomas Graf3d54b822005-07-05 14:15:09 -0700866
867 return sch;
John Fastabendd59f5ff2017-12-07 09:55:26 -0800868errout1:
869 kfree(p);
Thomas Graf3d54b822005-07-05 14:15:09 -0700870errout:
WANG Cong01e123d2008-06-27 19:51:35 -0700871 return ERR_PTR(err);
Thomas Graf3d54b822005-07-05 14:15:09 -0700872}
873
Changli Gao3511c912010-10-16 13:04:08 +0000874struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
stephen hemmingerd2a7f262013-08-31 10:15:50 -0700875 const struct Qdisc_ops *ops,
Alexander Aringa38a98822017-12-20 12:35:21 -0500876 unsigned int parentid,
877 struct netlink_ext_ack *extack)
Thomas Graf3d54b822005-07-05 14:15:09 -0700878{
879 struct Qdisc *sch;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900880
Alexander Aringa38a98822017-12-20 12:35:21 -0500881 if (!try_module_get(ops->owner)) {
882 NL_SET_ERR_MSG(extack, "Failed to increase module reference counter");
Eric Dumazet166ee5b2016-08-24 09:39:02 -0700883 return NULL;
Alexander Aringa38a98822017-12-20 12:35:21 -0500884 }
stephen hemminger6da7c8f2013-08-27 16:19:08 -0700885
Alexander Aringa38a98822017-12-20 12:35:21 -0500886 sch = qdisc_alloc(dev_queue, ops, extack);
Eric Dumazet166ee5b2016-08-24 09:39:02 -0700887 if (IS_ERR(sch)) {
888 module_put(ops->owner);
889 return NULL;
890 }
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800891 sch->parent = parentid;
Thomas Graf3d54b822005-07-05 14:15:09 -0700892
Alexander Aringa38a98822017-12-20 12:35:21 -0500893 if (!ops->init || ops->init(sch, NULL, extack) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return sch;
895
Vlad Buslov86bd4462018-09-24 19:22:50 +0300896 qdisc_put(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return NULL;
898}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800899EXPORT_SYMBOL(qdisc_create_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
David S. Miller5fb66222008-08-02 20:02:43 -0700901/* Under qdisc_lock(qdisc) and BH! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903void qdisc_reset(struct Qdisc *qdisc)
904{
Eric Dumazet20fea082007-11-14 01:44:41 -0800905 const struct Qdisc_ops *ops = qdisc->ops;
John Fastabenda53851e2017-12-07 09:55:45 -0800906 struct sk_buff *skb, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if (ops->reset)
909 ops->reset(qdisc);
Jarek Poplawski67305eb2008-11-03 02:52:50 -0800910
John Fastabenda53851e2017-12-07 09:55:45 -0800911 skb_queue_walk_safe(&qdisc->gso_skb, skb, tmp) {
912 __skb_unlink(skb, &qdisc->gso_skb);
913 kfree_skb_list(skb);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000914 }
John Fastabenda53851e2017-12-07 09:55:45 -0800915
John Fastabend70e57d52017-12-07 09:56:23 -0800916 skb_queue_walk_safe(&qdisc->skb_bad_txq, skb, tmp) {
917 __skb_unlink(skb, &qdisc->skb_bad_txq);
918 kfree_skb_list(skb);
919 }
920
Eric Dumazet4d202a02016-06-21 23:16:52 -0700921 qdisc->q.qlen = 0;
Konstantin Khlebnikovc8e18122017-09-20 15:45:36 +0300922 qdisc->qstats.backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800924EXPORT_SYMBOL(qdisc_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Daniel Borkmann81d947e2018-01-15 23:12:09 +0100926void qdisc_free(struct Qdisc *qdisc)
Eric Dumazet5d944c62010-03-31 07:06:04 +0000927{
John Fastabend73c20a82016-01-05 09:11:36 -0800928 if (qdisc_is_percpu_stats(qdisc)) {
John Fastabend22e0f8b2014-09-28 11:52:56 -0700929 free_percpu(qdisc->cpu_bstats);
John Fastabend73c20a82016-01-05 09:11:36 -0800930 free_percpu(qdisc->cpu_qstats);
931 }
John Fastabend22e0f8b2014-09-28 11:52:56 -0700932
Eric Dumazet5d944c62010-03-31 07:06:04 +0000933 kfree((char *) qdisc - qdisc->padded);
934}
935
Wei Yongjun53627002018-09-27 14:47:56 +0000936static void qdisc_free_cb(struct rcu_head *head)
Vlad Buslov3a7d0d02018-09-24 19:22:51 +0300937{
938 struct Qdisc *q = container_of(head, struct Qdisc, rcu);
939
940 qdisc_free(q);
941}
942
Vlad Buslov86bd4462018-09-24 19:22:50 +0300943static void qdisc_destroy(struct Qdisc *qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Eric Dumazet20fea082007-11-14 01:44:41 -0800945 const struct Qdisc_ops *ops = qdisc->ops;
John Fastabenda53851e2017-12-07 09:55:45 -0800946 struct sk_buff *skb, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
David S. Miller3a682fb2008-07-20 18:13:01 -0700948#ifdef CONFIG_NET_SCHED
Jiri Kosina59cc1f62016-08-10 11:05:15 +0200949 qdisc_hash_del(qdisc);
Jarek Poplawskif6e0b232008-08-22 03:24:05 -0700950
Eric Dumazeta2da5702011-01-20 03:48:19 +0000951 qdisc_put_stab(rtnl_dereference(qdisc->stab));
David S. Miller3a682fb2008-07-20 18:13:01 -0700952#endif
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800953 gen_kill_estimator(&qdisc->rate_est);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700954 if (ops->reset)
955 ops->reset(qdisc);
956 if (ops->destroy)
957 ops->destroy(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Patrick McHardy85670cc2006-09-27 16:45:45 -0700959 module_put(ops->owner);
David S. Miller5ce2d482008-07-08 17:06:30 -0700960 dev_put(qdisc_dev(qdisc));
David S. Miller8a34c5d2008-07-17 00:47:45 -0700961
John Fastabenda53851e2017-12-07 09:55:45 -0800962 skb_queue_walk_safe(&qdisc->gso_skb, skb, tmp) {
963 __skb_unlink(skb, &qdisc->gso_skb);
964 kfree_skb_list(skb);
965 }
966
John Fastabend70e57d52017-12-07 09:56:23 -0800967 skb_queue_walk_safe(&qdisc->skb_bad_txq, skb, tmp) {
968 __skb_unlink(skb, &qdisc->skb_bad_txq);
969 kfree_skb_list(skb);
970 }
971
Vlad Buslov3a7d0d02018-09-24 19:22:51 +0300972 call_rcu(&qdisc->rcu, qdisc_free_cb);
David S. Miller8a34c5d2008-07-17 00:47:45 -0700973}
Vlad Buslov86bd4462018-09-24 19:22:50 +0300974
975void qdisc_put(struct Qdisc *qdisc)
976{
977 if (qdisc->flags & TCQ_F_BUILTIN ||
978 !refcount_dec_and_test(&qdisc->refcnt))
979 return;
980
981 qdisc_destroy(qdisc);
982}
983EXPORT_SYMBOL(qdisc_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Vlad Buslov3a7d0d02018-09-24 19:22:51 +0300985/* Version of qdisc_put() that is called with rtnl mutex unlocked.
986 * Intended to be used as optimization, this function only takes rtnl lock if
987 * qdisc reference counter reached zero.
988 */
989
990void qdisc_put_unlocked(struct Qdisc *qdisc)
991{
992 if (qdisc->flags & TCQ_F_BUILTIN ||
993 !refcount_dec_and_rtnl_lock(&qdisc->refcnt))
994 return;
995
996 qdisc_destroy(qdisc);
997 rtnl_unlock();
998}
999EXPORT_SYMBOL(qdisc_put_unlocked);
1000
Patrick McHardy589983c2009-09-04 06:41:20 +00001001/* Attach toplevel qdisc to device queue. */
1002struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
1003 struct Qdisc *qdisc)
1004{
1005 struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
1006 spinlock_t *root_lock;
1007
1008 root_lock = qdisc_lock(oqdisc);
1009 spin_lock_bh(root_lock);
1010
Patrick McHardy589983c2009-09-04 06:41:20 +00001011 /* ... and graft new one */
1012 if (qdisc == NULL)
1013 qdisc = &noop_qdisc;
1014 dev_queue->qdisc_sleeping = qdisc;
1015 rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
1016
1017 spin_unlock_bh(root_lock);
1018
1019 return oqdisc;
1020}
John Fastabendb8970f02011-01-17 08:06:09 +00001021EXPORT_SYMBOL(dev_graft_qdisc);
Patrick McHardy589983c2009-09-04 06:41:20 +00001022
David S. Millere8a04642008-07-17 00:34:19 -07001023static void attach_one_default_qdisc(struct net_device *dev,
1024 struct netdev_queue *dev_queue,
1025 void *_unused)
1026{
Phil Sutter3e692f22015-08-27 21:21:39 +02001027 struct Qdisc *qdisc;
1028 const struct Qdisc_ops *ops = default_qdisc_ops;
David S. Millere8a04642008-07-17 00:34:19 -07001029
Phil Sutter3e692f22015-08-27 21:21:39 +02001030 if (dev->priv_flags & IFF_NO_QUEUE)
1031 ops = &noqueue_qdisc_ops;
1032
Alexander Aringa38a98822017-12-20 12:35:21 -05001033 qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT, NULL);
Phil Sutter3e692f22015-08-27 21:21:39 +02001034 if (!qdisc) {
1035 netdev_info(dev, "activation failed\n");
1036 return;
David S. Millere8a04642008-07-17 00:34:19 -07001037 }
Phil Sutter3e692f22015-08-27 21:21:39 +02001038 if (!netif_is_multiqueue(dev))
Eric Dumazet4eaf3b82015-12-01 20:08:51 -08001039 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
David S. Millere8a04642008-07-17 00:34:19 -07001040 dev_queue->qdisc_sleeping = qdisc;
1041}
1042
David S. Miller6ec1c692009-09-06 01:58:51 -07001043static void attach_default_qdiscs(struct net_device *dev)
1044{
1045 struct netdev_queue *txq;
1046 struct Qdisc *qdisc;
1047
1048 txq = netdev_get_tx_queue(dev, 0);
1049
Phil Sutter4b469952015-08-13 19:01:07 +02001050 if (!netif_is_multiqueue(dev) ||
Phil Sutter4b469952015-08-13 19:01:07 +02001051 dev->priv_flags & IFF_NO_QUEUE) {
David S. Miller6ec1c692009-09-06 01:58:51 -07001052 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
1053 dev->qdisc = txq->qdisc_sleeping;
Eric Dumazet551143d2017-08-24 21:12:28 -07001054 qdisc_refcount_inc(dev->qdisc);
David S. Miller6ec1c692009-09-06 01:58:51 -07001055 } else {
Alexander Aringa38a98822017-12-20 12:35:21 -05001056 qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT, NULL);
David S. Miller6ec1c692009-09-06 01:58:51 -07001057 if (qdisc) {
David S. Miller6ec1c692009-09-06 01:58:51 -07001058 dev->qdisc = qdisc;
Eric Dumazete57a7842013-12-12 15:41:56 -08001059 qdisc->ops->attach(qdisc);
David S. Miller6ec1c692009-09-06 01:58:51 -07001060 }
1061 }
Jiri Kosina59cc1f62016-08-10 11:05:15 +02001062#ifdef CONFIG_NET_SCHED
WANG Cong92f91702017-04-04 18:51:30 -07001063 if (dev->qdisc != &noop_qdisc)
Jiri Kosina49b49972017-03-08 16:03:32 +01001064 qdisc_hash_add(dev->qdisc, false);
Jiri Kosina59cc1f62016-08-10 11:05:15 +02001065#endif
David S. Miller6ec1c692009-09-06 01:58:51 -07001066}
1067
David S. Millere8a04642008-07-17 00:34:19 -07001068static void transition_one_qdisc(struct net_device *dev,
1069 struct netdev_queue *dev_queue,
1070 void *_need_watchdog)
1071{
David S. Miller838740002008-07-17 00:53:03 -07001072 struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
David S. Millere8a04642008-07-17 00:34:19 -07001073 int *need_watchdog_p = _need_watchdog;
1074
David S. Millera9312ae2008-08-17 21:51:03 -07001075 if (!(new_qdisc->flags & TCQ_F_BUILTIN))
1076 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
1077
David S. Miller838740002008-07-17 00:53:03 -07001078 rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
Phil Sutter3e692f22015-08-27 21:21:39 +02001079 if (need_watchdog_p) {
Eric Dumazet9d214932009-05-17 20:55:16 -07001080 dev_queue->trans_start = 0;
David S. Millere8a04642008-07-17 00:34:19 -07001081 *need_watchdog_p = 1;
Eric Dumazet9d214932009-05-17 20:55:16 -07001082 }
David S. Millere8a04642008-07-17 00:34:19 -07001083}
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085void dev_activate(struct net_device *dev)
1086{
David S. Millere8a04642008-07-17 00:34:19 -07001087 int need_watchdog;
David S. Millerb0e1e642008-07-08 17:42:10 -07001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /* No queueing discipline is attached to device;
stephen hemminger6da7c8f2013-08-27 16:19:08 -07001090 * create default one for devices, which need queueing
1091 * and noqueue_qdisc for virtual interfaces
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 */
1093
David S. Miller6ec1c692009-09-06 01:58:51 -07001094 if (dev->qdisc == &noop_qdisc)
1095 attach_default_qdiscs(dev);
Patrick McHardyaf356af2009-09-04 06:41:18 +00001096
Tommy S. Christensencacaddf2005-05-03 16:18:52 -07001097 if (!netif_carrier_ok(dev))
1098 /* Delay activation until next carrier-on event */
1099 return;
1100
David S. Millere8a04642008-07-17 00:34:19 -07001101 need_watchdog = 0;
1102 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
Eric Dumazet24824a02010-10-02 06:11:55 +00001103 if (dev_ingress_queue(dev))
1104 transition_one_qdisc(dev, dev_ingress_queue(dev), NULL);
David S. Millere8a04642008-07-17 00:34:19 -07001105
1106 if (need_watchdog) {
Florian Westphal860e9532016-05-03 16:33:13 +02001107 netif_trans_update(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 dev_watchdog_up(dev);
1109 }
David S. Millerb0e1e642008-07-08 17:42:10 -07001110}
John Fastabendb8970f02011-01-17 08:06:09 +00001111EXPORT_SYMBOL(dev_activate);
David S. Millerb0e1e642008-07-08 17:42:10 -07001112
David S. Millere8a04642008-07-17 00:34:19 -07001113static void dev_deactivate_queue(struct net_device *dev,
1114 struct netdev_queue *dev_queue,
1115 void *_qdisc_default)
David S. Millerb0e1e642008-07-08 17:42:10 -07001116{
David S. Millere8a04642008-07-17 00:34:19 -07001117 struct Qdisc *qdisc_default = _qdisc_default;
David S. Miller970565b2008-07-08 23:10:33 -07001118 struct Qdisc *qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -07001119
John Fastabend46e5da40a2014-09-12 20:04:52 -07001120 qdisc = rtnl_dereference(dev_queue->qdisc);
David S. Millerb0e1e642008-07-08 17:42:10 -07001121 if (qdisc) {
Paolo Abeni96009c72018-05-15 16:24:36 +02001122 bool nolock = qdisc->flags & TCQ_F_NOLOCK;
1123
1124 if (nolock)
1125 spin_lock_bh(&qdisc->seqlock);
David S. Miller838740002008-07-17 00:53:03 -07001126 spin_lock_bh(qdisc_lock(qdisc));
1127
David S. Millera9312ae2008-08-17 21:51:03 -07001128 if (!(qdisc->flags & TCQ_F_BUILTIN))
1129 set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
1130
Jarek Poplawskif7a54c12008-08-27 02:22:07 -07001131 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
David S. Millerb0e1e642008-07-08 17:42:10 -07001132 qdisc_reset(qdisc);
David S. Millerd3b753d2008-07-15 20:14:35 -07001133
David S. Miller838740002008-07-17 00:53:03 -07001134 spin_unlock_bh(qdisc_lock(qdisc));
Paolo Abeni96009c72018-05-15 16:24:36 +02001135 if (nolock)
1136 spin_unlock_bh(&qdisc->seqlock);
David S. Millerb0e1e642008-07-08 17:42:10 -07001137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
David S. Miller4335cd22008-08-17 21:58:07 -07001140static bool some_qdisc_is_busy(struct net_device *dev)
David S. Millere8a04642008-07-17 00:34:19 -07001141{
1142 unsigned int i;
1143
1144 for (i = 0; i < dev->num_tx_queues; i++) {
1145 struct netdev_queue *dev_queue;
David S. Miller7698b4f2008-07-16 01:42:40 -07001146 spinlock_t *root_lock;
David S. Millere2627c82008-07-16 00:56:32 -07001147 struct Qdisc *q;
David S. Millere8a04642008-07-17 00:34:19 -07001148 int val;
1149
1150 dev_queue = netdev_get_tx_queue(dev, i);
David S. Millerb9a3b112008-08-13 15:18:38 -07001151 q = dev_queue->qdisc_sleeping;
David S. Millere8a04642008-07-17 00:34:19 -07001152
Paolo Abeni32f7b442018-05-15 10:50:31 +02001153 root_lock = qdisc_lock(q);
1154 spin_lock_bh(root_lock);
David S. Millere8a04642008-07-17 00:34:19 -07001155
Paolo Abeni32f7b442018-05-15 10:50:31 +02001156 val = (qdisc_is_running(q) ||
1157 test_bit(__QDISC_STATE_SCHED, &q->state));
David S. Millere8a04642008-07-17 00:34:19 -07001158
Paolo Abeni32f7b442018-05-15 10:50:31 +02001159 spin_unlock_bh(root_lock);
David S. Millere8a04642008-07-17 00:34:19 -07001160
1161 if (val)
1162 return true;
1163 }
1164 return false;
1165}
1166
John Fastabend7bbde832017-12-07 09:56:04 -08001167static void dev_qdisc_reset(struct net_device *dev,
1168 struct netdev_queue *dev_queue,
1169 void *none)
1170{
1171 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
1172
1173 if (qdisc)
1174 qdisc_reset(qdisc);
1175}
1176
Eric Dumazet31376632011-05-19 23:42:09 +00001177/**
1178 * dev_deactivate_many - deactivate transmissions on several devices
1179 * @head: list of devices to deactivate
1180 *
1181 * This function returns only when all outstanding transmissions
1182 * have completed, unless all devices are in dismantle phase.
1183 */
Octavian Purdila44345722010-12-13 12:44:07 +00001184void dev_deactivate_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Octavian Purdila44345722010-12-13 12:44:07 +00001186 struct net_device *dev;
Herbert Xu41a23b02007-05-10 14:12:47 -07001187
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001188 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001189 netdev_for_each_tx_queue(dev, dev_deactivate_queue,
1190 &noop_qdisc);
1191 if (dev_ingress_queue(dev))
1192 dev_deactivate_queue(dev, dev_ingress_queue(dev),
1193 &noop_qdisc);
1194
1195 dev_watchdog_down(dev);
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Eric Dumazet31376632011-05-19 23:42:09 +00001198 /* Wait for outstanding qdisc-less dev_queue_xmit calls.
1199 * This is avoided if all devices are in dismantle phase :
1200 * Caller will call synchronize_net() for us
1201 */
John Fastabend7bbde832017-12-07 09:56:04 -08001202 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Herbert Xud4828d82006-06-22 02:28:18 -07001204 /* Wait for outstanding qdisc_run calls. */
John Fastabend7bbde832017-12-07 09:56:04 -08001205 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001206 while (some_qdisc_is_busy(dev))
1207 yield();
John Fastabend7bbde832017-12-07 09:56:04 -08001208 /* The new qdisc is assigned at this point so we can safely
1209 * unwind stale skb lists and qdisc statistics
1210 */
1211 netdev_for_each_tx_queue(dev, dev_qdisc_reset, NULL);
1212 if (dev_ingress_queue(dev))
1213 dev_qdisc_reset(dev, dev_ingress_queue(dev), NULL);
1214 }
Octavian Purdila44345722010-12-13 12:44:07 +00001215}
1216
1217void dev_deactivate(struct net_device *dev)
1218{
1219 LIST_HEAD(single);
1220
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001221 list_add(&dev->close_list, &single);
Octavian Purdila44345722010-12-13 12:44:07 +00001222 dev_deactivate_many(&single);
Eric W. Biederman5f04d502011-02-20 11:49:45 -08001223 list_del(&single);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
John Fastabendb8970f02011-01-17 08:06:09 +00001225EXPORT_SYMBOL(dev_deactivate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Cong Wang48bfd552018-01-25 18:26:23 -08001227static int qdisc_change_tx_queue_len(struct net_device *dev,
1228 struct netdev_queue *dev_queue)
1229{
1230 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
1231 const struct Qdisc_ops *ops = qdisc->ops;
1232
1233 if (ops->change_tx_queue_len)
1234 return ops->change_tx_queue_len(qdisc, dev->tx_queue_len);
1235 return 0;
1236}
1237
1238int dev_qdisc_change_tx_queue_len(struct net_device *dev)
1239{
1240 bool up = dev->flags & IFF_UP;
1241 unsigned int i;
1242 int ret = 0;
1243
1244 if (up)
1245 dev_deactivate(dev);
1246
1247 for (i = 0; i < dev->num_tx_queues; i++) {
1248 ret = qdisc_change_tx_queue_len(dev, &dev->_tx[i]);
1249
1250 /* TODO: revert changes on a partial failure */
1251 if (ret)
1252 break;
1253 }
1254
1255 if (up)
1256 dev_activate(dev);
1257 return ret;
1258}
1259
David S. Millerb0e1e642008-07-08 17:42:10 -07001260static void dev_init_scheduler_queue(struct net_device *dev,
1261 struct netdev_queue *dev_queue,
David S. Millere8a04642008-07-17 00:34:19 -07001262 void *_qdisc)
David S. Millerb0e1e642008-07-08 17:42:10 -07001263{
David S. Millere8a04642008-07-17 00:34:19 -07001264 struct Qdisc *qdisc = _qdisc;
1265
John Fastabend46e5da40a2014-09-12 20:04:52 -07001266 rcu_assign_pointer(dev_queue->qdisc, qdisc);
David S. Millerb0e1e642008-07-08 17:42:10 -07001267 dev_queue->qdisc_sleeping = qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -07001268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270void dev_init_scheduler(struct net_device *dev)
1271{
Patrick McHardyaf356af2009-09-04 06:41:18 +00001272 dev->qdisc = &noop_qdisc;
David S. Millere8a04642008-07-17 00:34:19 -07001273 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +00001274 if (dev_ingress_queue(dev))
1275 dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Kees Cookcdeabbb2017-10-16 17:29:17 -07001277 timer_setup(&dev->watchdog_timer, dev_watchdog, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
David S. Millere8a04642008-07-17 00:34:19 -07001280static void shutdown_scheduler_queue(struct net_device *dev,
1281 struct netdev_queue *dev_queue,
1282 void *_qdisc_default)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
David S. Millerb0e1e642008-07-08 17:42:10 -07001284 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
David S. Millere8a04642008-07-17 00:34:19 -07001285 struct Qdisc *qdisc_default = _qdisc_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
David S. Millerb0e1e642008-07-08 17:42:10 -07001287 if (qdisc) {
Jarek Poplawskif7a54c12008-08-27 02:22:07 -07001288 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
David S. Millerb0e1e642008-07-08 17:42:10 -07001289 dev_queue->qdisc_sleeping = qdisc_default;
1290
Vlad Buslov86bd4462018-09-24 19:22:50 +03001291 qdisc_put(qdisc);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001292 }
David S. Millerb0e1e642008-07-08 17:42:10 -07001293}
1294
1295void dev_shutdown(struct net_device *dev)
1296{
David S. Millere8a04642008-07-17 00:34:19 -07001297 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +00001298 if (dev_ingress_queue(dev))
1299 shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
Vlad Buslov86bd4462018-09-24 19:22:50 +03001300 qdisc_put(dev->qdisc);
Patrick McHardyaf356af2009-09-04 06:41:18 +00001301 dev->qdisc = &noop_qdisc;
1302
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001303 WARN_ON(timer_pending(&dev->watchdog_timer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
Jiri Pirko292f1c72013-02-12 00:12:03 +00001305
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001306void psched_ratecfg_precompute(struct psched_ratecfg *r,
Eric Dumazet3e1e3aa2013-09-19 09:10:03 -07001307 const struct tc_ratespec *conf,
1308 u64 rate64)
Jiri Pirko292f1c72013-02-12 00:12:03 +00001309{
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001310 memset(r, 0, sizeof(*r));
1311 r->overhead = conf->overhead;
Eric Dumazet3e1e3aa2013-09-19 09:10:03 -07001312 r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
Jesper Dangaard Brouer8a8e3d82013-08-14 23:47:11 +02001313 r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
Jiri Pirko292f1c72013-02-12 00:12:03 +00001314 r->mult = 1;
1315 /*
Eric Dumazet130d3d62013-06-06 13:56:19 -07001316 * The deal here is to replace a divide by a reciprocal one
1317 * in fast path (a reciprocal divide is a multiply and a shift)
1318 *
1319 * Normal formula would be :
1320 * time_in_ns = (NSEC_PER_SEC * len) / rate_bps
1321 *
1322 * We compute mult/shift to use instead :
1323 * time_in_ns = (len * mult) >> shift;
1324 *
1325 * We try to get the highest possible mult value for accuracy,
1326 * but have to make sure no overflows will ever happen.
Jiri Pirko292f1c72013-02-12 00:12:03 +00001327 */
Eric Dumazet130d3d62013-06-06 13:56:19 -07001328 if (r->rate_bytes_ps > 0) {
1329 u64 factor = NSEC_PER_SEC;
Jiri Pirko292f1c72013-02-12 00:12:03 +00001330
Eric Dumazet130d3d62013-06-06 13:56:19 -07001331 for (;;) {
1332 r->mult = div64_u64(factor, r->rate_bytes_ps);
1333 if (r->mult & (1U << 31) || factor & (1ULL << 63))
1334 break;
1335 factor <<= 1;
1336 r->shift++;
1337 }
Jiri Pirko292f1c72013-02-12 00:12:03 +00001338 }
1339}
1340EXPORT_SYMBOL(psched_ratecfg_precompute);
Jiri Pirko46209402017-11-03 11:46:25 +01001341
1342static void mini_qdisc_rcu_func(struct rcu_head *head)
1343{
1344}
1345
1346void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1347 struct tcf_proto *tp_head)
1348{
Vlad Busloved76f5e2019-02-11 10:55:38 +02001349 /* Protected with chain0->filter_chain_lock.
1350 * Can't access chain directly because tp_head can be NULL.
1351 */
1352 struct mini_Qdisc *miniq_old =
1353 rcu_dereference_protected(*miniqp->p_miniq, 1);
Jiri Pirko46209402017-11-03 11:46:25 +01001354 struct mini_Qdisc *miniq;
1355
1356 if (!tp_head) {
1357 RCU_INIT_POINTER(*miniqp->p_miniq, NULL);
Cong Wangb2fb01f2017-12-20 23:26:24 -08001358 /* Wait for flying RCU callback before it is freed. */
Paul E. McKenneyae0e3342018-11-06 19:40:39 -08001359 rcu_barrier();
Jiri Pirko46209402017-11-03 11:46:25 +01001360 return;
1361 }
1362
1363 miniq = !miniq_old || miniq_old == &miniqp->miniq2 ?
1364 &miniqp->miniq1 : &miniqp->miniq2;
1365
1366 /* We need to make sure that readers won't see the miniq
Paul E. McKenneyae0e3342018-11-06 19:40:39 -08001367 * we are about to modify. So wait until previous call_rcu callback
Jiri Pirko46209402017-11-03 11:46:25 +01001368 * is done.
1369 */
Paul E. McKenneyae0e3342018-11-06 19:40:39 -08001370 rcu_barrier();
Jiri Pirko46209402017-11-03 11:46:25 +01001371 miniq->filter_list = tp_head;
1372 rcu_assign_pointer(*miniqp->p_miniq, miniq);
1373
1374 if (miniq_old)
Cong Wangb2fb01f2017-12-20 23:26:24 -08001375 /* This is counterpart of the rcu barriers above. We need to
Jiri Pirko46209402017-11-03 11:46:25 +01001376 * block potential new user of miniq_old until all readers
1377 * are not seeing it.
1378 */
Paul E. McKenneyae0e3342018-11-06 19:40:39 -08001379 call_rcu(&miniq_old->rcu, mini_qdisc_rcu_func);
Jiri Pirko46209402017-11-03 11:46:25 +01001380}
1381EXPORT_SYMBOL(mini_qdisc_pair_swap);
1382
1383void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1384 struct mini_Qdisc __rcu **p_miniq)
1385{
1386 miniqp->miniq1.cpu_bstats = qdisc->cpu_bstats;
1387 miniqp->miniq1.cpu_qstats = qdisc->cpu_qstats;
1388 miniqp->miniq2.cpu_bstats = qdisc->cpu_bstats;
1389 miniqp->miniq2.cpu_qstats = qdisc->cpu_qstats;
1390 miniqp->p_miniq = p_miniq;
1391}
1392EXPORT_SYMBOL(mini_qdisc_pair_init);