blob: 876fab2604b89023bcb64c0fdebd70aaf968be14 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
stephen hemminger34aedd32013-08-31 10:15:33 -070036/* Qdisc to use by default */
37const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops;
38EXPORT_SYMBOL(default_qdisc_ops);
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/* Main transmission queue. */
41
Patrick McHardy0463d4a2007-04-16 17:02:10 -070042/* Modifications to data participating in scheduling must be protected with
David S. Miller5fb66222008-08-02 20:02:43 -070043 * qdisc_lock(qdisc) spinlock.
Patrick McHardy0463d4a2007-04-16 17:02:10 -070044 *
45 * The idea is the following:
David S. Millerc7e4f3b2008-07-16 03:22:39 -070046 * - enqueue, dequeue are serialized via qdisc root lock
47 * - ingress filtering is also serialized via qdisc root lock
Patrick McHardy0463d4a2007-04-16 17:02:10 -070048 * - updates to tree and tree walking are only done under the rtnl mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 */
John Fastabend70e57d52017-12-07 09:56:23 -080050
51static inline struct sk_buff *__skb_dequeue_bad_txq(struct Qdisc *q)
52{
53 const struct netdev_queue *txq = q->dev_queue;
54 spinlock_t *lock = NULL;
55 struct sk_buff *skb;
56
57 if (q->flags & TCQ_F_NOLOCK) {
58 lock = qdisc_lock(q);
59 spin_lock(lock);
60 }
61
62 skb = skb_peek(&q->skb_bad_txq);
63 if (skb) {
64 /* check the reason of requeuing without tx lock first */
65 txq = skb_get_tx_queue(txq->dev, skb);
66 if (!netif_xmit_frozen_or_stopped(txq)) {
67 skb = __skb_dequeue(&q->skb_bad_txq);
68 if (qdisc_is_percpu_stats(q)) {
69 qdisc_qstats_cpu_backlog_dec(q, skb);
70 qdisc_qstats_cpu_qlen_dec(q);
71 } else {
72 qdisc_qstats_backlog_dec(q, skb);
73 q->q.qlen--;
74 }
75 } else {
76 skb = NULL;
77 }
78 }
79
80 if (lock)
81 spin_unlock(lock);
82
83 return skb;
84}
85
86static inline struct sk_buff *qdisc_dequeue_skb_bad_txq(struct Qdisc *q)
87{
88 struct sk_buff *skb = skb_peek(&q->skb_bad_txq);
89
90 if (unlikely(skb))
91 skb = __skb_dequeue_bad_txq(q);
92
93 return skb;
94}
95
96static inline void qdisc_enqueue_skb_bad_txq(struct Qdisc *q,
97 struct sk_buff *skb)
98{
99 spinlock_t *lock = NULL;
100
101 if (q->flags & TCQ_F_NOLOCK) {
102 lock = qdisc_lock(q);
103 spin_lock(lock);
104 }
105
106 __skb_queue_tail(&q->skb_bad_txq, skb);
107
108 if (lock)
109 spin_unlock(lock);
110}
111
John Fastabenda53851e2017-12-07 09:55:45 -0800112static inline int __dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700113{
John Fastabenda53851e2017-12-07 09:55:45 -0800114 __skb_queue_head(&q->gso_skb, skb);
Jarek Poplawski53e91502008-10-08 11:36:22 -0700115 q->qstats.requeues++;
WANG Conga27758f2016-06-03 15:05:57 -0700116 qdisc_qstats_backlog_inc(q, skb);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000117 q->q.qlen++; /* it's still part of the queue */
David S. Miller37437bb2008-07-16 02:15:04 -0700118 __netif_schedule(q);
Jarek Poplawski62523522008-10-06 10:41:50 -0700119
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700120 return 0;
121}
122
John Fastabenda53851e2017-12-07 09:55:45 -0800123static inline int dev_requeue_skb_locked(struct sk_buff *skb, struct Qdisc *q)
124{
125 spinlock_t *lock = qdisc_lock(q);
126
127 spin_lock(lock);
128 __skb_queue_tail(&q->gso_skb, skb);
129 spin_unlock(lock);
130
131 qdisc_qstats_cpu_requeues_inc(q);
132 qdisc_qstats_cpu_backlog_inc(q, skb);
133 qdisc_qstats_cpu_qlen_inc(q);
134 __netif_schedule(q);
135
136 return 0;
137}
138
139static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
140{
141 if (q->flags & TCQ_F_NOLOCK)
142 return dev_requeue_skb_locked(skb, q);
143 else
144 return __dev_requeue_skb(skb, q);
145}
146
Eric Dumazet55a93b32014-10-03 15:31:07 -0700147static void try_bulk_dequeue_skb(struct Qdisc *q,
148 struct sk_buff *skb,
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200149 const struct netdev_queue *txq,
150 int *packets)
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +0200151{
Eric Dumazet55a93b32014-10-03 15:31:07 -0700152 int bytelimit = qdisc_avail_bulklimit(txq) - skb->len;
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +0200153
154 while (bytelimit > 0) {
Eric Dumazet55a93b32014-10-03 15:31:07 -0700155 struct sk_buff *nskb = q->dequeue(q);
156
157 if (!nskb)
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +0200158 break;
159
Eric Dumazet55a93b32014-10-03 15:31:07 -0700160 bytelimit -= nskb->len; /* covers GSO len */
161 skb->next = nskb;
162 skb = nskb;
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200163 (*packets)++; /* GSO counts as one pkt */
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +0200164 }
Eric Dumazet55a93b32014-10-03 15:31:07 -0700165 skb->next = NULL;
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +0200166}
167
Eric Dumazet4d202a02016-06-21 23:16:52 -0700168/* This variant of try_bulk_dequeue_skb() makes sure
169 * all skbs in the chain are for the same txq
170 */
171static void try_bulk_dequeue_skb_slow(struct Qdisc *q,
172 struct sk_buff *skb,
173 int *packets)
174{
175 int mapping = skb_get_queue_mapping(skb);
176 struct sk_buff *nskb;
177 int cnt = 0;
178
179 do {
180 nskb = q->dequeue(q);
181 if (!nskb)
182 break;
183 if (unlikely(skb_get_queue_mapping(nskb) != mapping)) {
John Fastabend70e57d52017-12-07 09:56:23 -0800184 qdisc_enqueue_skb_bad_txq(q, nskb);
185
186 if (qdisc_is_percpu_stats(q)) {
187 qdisc_qstats_cpu_backlog_inc(q, nskb);
188 qdisc_qstats_cpu_qlen_inc(q);
189 } else {
190 qdisc_qstats_backlog_inc(q, nskb);
191 q->q.qlen++;
192 }
Eric Dumazet4d202a02016-06-21 23:16:52 -0700193 break;
194 }
195 skb->next = nskb;
196 skb = nskb;
197 } while (++cnt < 8);
198 (*packets) += cnt;
199 skb->next = NULL;
200}
201
Jesper Dangaard Brouer5772e9a2014-10-01 22:35:59 +0200202/* Note that dequeue_skb can possibly return a SKB list (via skb->next).
203 * A requeued skb (via q->gso_skb) can also be a SKB list.
204 */
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200205static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
206 int *packets)
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700207{
Eric Dumazet1abbe132012-12-11 15:54:33 +0000208 const struct netdev_queue *txq = q->dev_queue;
John Fastabendfd8e8d12017-12-07 09:56:42 -0800209 struct sk_buff *skb = NULL;
Jarek Poplawski554794d2008-10-06 09:54:39 -0700210
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200211 *packets = 1;
John Fastabenda53851e2017-12-07 09:55:45 -0800212 if (unlikely(!skb_queue_empty(&q->gso_skb))) {
213 spinlock_t *lock = NULL;
214
215 if (q->flags & TCQ_F_NOLOCK) {
216 lock = qdisc_lock(q);
217 spin_lock(lock);
218 }
219
220 skb = skb_peek(&q->gso_skb);
221
222 /* skb may be null if another cpu pulls gso_skb off in between
223 * empty check and lock.
224 */
225 if (!skb) {
226 if (lock)
227 spin_unlock(lock);
228 goto validate;
229 }
230
Eric Dumazet4d202a02016-06-21 23:16:52 -0700231 /* skb in gso_skb were already validated */
232 *validate = false;
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);
239 qdisc_qstats_cpu_qlen_dec(q);
240 } 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;
David S. Miller7698b4f2008-07-16 01:42:40 -0700288
289 /* And release qdisc */
John Fastabend6b3ba912017-12-07 09:54:25 -0800290 if (root_lock)
291 spin_unlock(root_lock);
Herbert Xud90df3a2007-05-10 04:55:14 -0700292
Eric Dumazet55a93b32014-10-03 15:31:07 -0700293 /* Note that we validate skb (GSO, checksum, ...) outside of locks */
294 if (validate)
295 skb = validate_xmit_skb_list(skb, dev);
Patrick McHardy572a9d72009-11-10 06:14:14 +0000296
Lars Persson3dcd493fb2016-04-12 08:45:52 +0200297 if (likely(skb)) {
Eric Dumazet55a93b32014-10-03 15:31:07 -0700298 HARD_TX_LOCK(dev, txq, smp_processor_id());
299 if (!netif_xmit_frozen_or_stopped(txq))
300 skb = dev_hard_start_xmit(skb, dev, txq, &ret);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700301
Eric Dumazet55a93b32014-10-03 15:31:07 -0700302 HARD_TX_UNLOCK(dev, txq);
Lars Persson3dcd493fb2016-04-12 08:45:52 +0200303 } else {
John Fastabend6b3ba912017-12-07 09:54:25 -0800304 if (root_lock)
305 spin_lock(root_lock);
John Fastabend29b86cd2017-12-07 09:54:47 -0800306 return true;
Eric Dumazet55a93b32014-10-03 15:31:07 -0700307 }
John Fastabend6b3ba912017-12-07 09:54:25 -0800308
309 if (root_lock)
310 spin_lock(root_lock);
Jamal Hadi Salimc716a812007-06-10 17:31:24 -0700311
John Fastabend29b86cd2017-12-07 09:54:47 -0800312 if (!dev_xmit_complete(ret)) {
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700313 /* Driver returned NETDEV_TX_BUSY - requeue skb */
Joe Perchese87cc472012-05-13 21:56:26 +0000314 if (unlikely(ret != NETDEV_TX_BUSY))
315 net_warn_ratelimited("BUG %s code %d qlen %d\n",
316 dev->name, ret, q->q.qlen);
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700317
John Fastabend29b86cd2017-12-07 09:54:47 -0800318 dev_requeue_skb(skb, q);
319 return false;
Krishna Kumar6c1361a2007-06-24 19:56:09 -0700320 }
321
Tom Herbert734664982011-11-28 16:32:44 +0000322 if (ret && netif_xmit_frozen_or_stopped(txq))
John Fastabend29b86cd2017-12-07 09:54:47 -0800323 return false;
David S. Miller37437bb2008-07-16 02:15:04 -0700324
John Fastabend29b86cd2017-12-07 09:54:47 -0800325 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000328/*
329 * NOTE: Called under qdisc_lock(q) with locally disabled BH.
330 *
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -0700331 * running seqcount guarantees only one CPU can process
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000332 * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
333 * this queue.
334 *
335 * netif_tx_lock serializes accesses to device driver.
336 *
337 * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
338 * if one is grabbed, another must be free.
339 *
340 * Note, that this procedure can be called by a watchdog timer
341 *
342 * Returns to the caller:
343 * 0 - queue is empty or throttled.
344 * >0 - queue is not empty.
345 *
346 */
John Fastabend29b86cd2017-12-07 09:54:47 -0800347static inline bool qdisc_restart(struct Qdisc *q, int *packets)
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000348{
John Fastabend6b3ba912017-12-07 09:54:25 -0800349 spinlock_t *root_lock = NULL;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000350 struct netdev_queue *txq;
351 struct net_device *dev;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000352 struct sk_buff *skb;
Eric Dumazet55a93b32014-10-03 15:31:07 -0700353 bool validate;
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000354
355 /* Dequeue packet */
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200356 skb = dequeue_skb(q, &validate, packets);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000357 if (unlikely(!skb))
John Fastabend29b86cd2017-12-07 09:54:47 -0800358 return false;
Daniel Borkmann10c51b56232014-08-27 11:11:27 +0200359
John Fastabend6b3ba912017-12-07 09:54:25 -0800360 if (!(q->flags & TCQ_F_NOLOCK))
361 root_lock = qdisc_lock(q);
362
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000363 dev = qdisc_dev(q);
Daniel Borkmann10c51b56232014-08-27 11:11:27 +0200364 txq = skb_get_tx_queue(dev, skb);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000365
Eric Dumazet55a93b32014-10-03 15:31:07 -0700366 return sch_direct_xmit(skb, q, dev, txq, root_lock, validate);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000367}
368
David S. Miller37437bb2008-07-16 02:15:04 -0700369void __qdisc_run(struct Qdisc *q)
Herbert Xu48d83322006-06-19 23:57:59 -0700370{
Matthias Tafelmeier3d48b532016-12-29 21:37:21 +0100371 int quota = dev_tx_weight;
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200372 int packets;
Herbert Xu2ba25062008-03-28 16:25:26 -0700373
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200374 while (qdisc_restart(q, &packets)) {
Herbert Xu2ba25062008-03-28 16:25:26 -0700375 /*
jamald5b8aa12011-06-26 08:13:54 +0000376 * Ordered by possible occurrence: Postpone processing if
377 * 1. we've exceeded packet quota
378 * 2. another process needs the CPU;
Herbert Xu2ba25062008-03-28 16:25:26 -0700379 */
Jesper Dangaard Brouerb8358d72014-10-09 12:18:10 +0200380 quota -= packets;
381 if (quota <= 0 || need_resched()) {
David S. Miller37437bb2008-07-16 02:15:04 -0700382 __netif_schedule(q);
Herbert Xu2ba25062008-03-28 16:25:26 -0700383 break;
384 }
385 }
Herbert Xu48d83322006-06-19 23:57:59 -0700386}
387
Eric Dumazet9d214932009-05-17 20:55:16 -0700388unsigned long dev_trans_start(struct net_device *dev)
389{
nikolay@redhat.com07ce76a2013-08-03 22:07:47 +0200390 unsigned long val, res;
Eric Dumazet9d214932009-05-17 20:55:16 -0700391 unsigned int i;
392
nikolay@redhat.com07ce76a2013-08-03 22:07:47 +0200393 if (is_vlan_dev(dev))
394 dev = vlan_dev_real_dev(dev);
Chris Dion32d3e512017-12-06 10:50:28 -0500395 else if (netif_is_macvlan(dev))
396 dev = macvlan_dev_real_dev(dev);
Florian Westphal9b366272016-05-03 16:33:14 +0200397 res = netdev_get_tx_queue(dev, 0)->trans_start;
398 for (i = 1; i < dev->num_tx_queues; i++) {
Eric Dumazet9d214932009-05-17 20:55:16 -0700399 val = netdev_get_tx_queue(dev, i)->trans_start;
400 if (val && time_after(val, res))
401 res = val;
402 }
nikolay@redhat.com07ce76a2013-08-03 22:07:47 +0200403
Eric Dumazet9d214932009-05-17 20:55:16 -0700404 return res;
405}
406EXPORT_SYMBOL(dev_trans_start);
407
Kees Cookcdeabbb2017-10-16 17:29:17 -0700408static void dev_watchdog(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Kees Cookcdeabbb2017-10-16 17:29:17 -0700410 struct net_device *dev = from_timer(dev, t, watchdog_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Herbert Xu932ff272006-06-09 12:20:56 -0700412 netif_tx_lock(dev);
David S. Millere8a04642008-07-17 00:34:19 -0700413 if (!qdisc_tx_is_noop(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (netif_device_present(dev) &&
415 netif_running(dev) &&
416 netif_carrier_ok(dev)) {
Eric Dumazet9d214932009-05-17 20:55:16 -0700417 int some_queue_timedout = 0;
David S. Millere8a04642008-07-17 00:34:19 -0700418 unsigned int i;
Eric Dumazet9d214932009-05-17 20:55:16 -0700419 unsigned long trans_start;
Stephen Hemminger338f7562006-05-16 15:02:12 -0700420
David S. Millere8a04642008-07-17 00:34:19 -0700421 for (i = 0; i < dev->num_tx_queues; i++) {
422 struct netdev_queue *txq;
423
424 txq = netdev_get_tx_queue(dev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200425 trans_start = txq->trans_start;
Tom Herbert734664982011-11-28 16:32:44 +0000426 if (netif_xmit_stopped(txq) &&
Eric Dumazet9d214932009-05-17 20:55:16 -0700427 time_after(jiffies, (trans_start +
428 dev->watchdog_timeo))) {
429 some_queue_timedout = 1;
david decotignyccf5ff62011-11-16 12:15:10 +0000430 txq->trans_timeout++;
David S. Millere8a04642008-07-17 00:34:19 -0700431 break;
432 }
433 }
434
Eric Dumazet9d214932009-05-17 20:55:16 -0700435 if (some_queue_timedout) {
Eric Dumazet9d214932009-05-17 20:55:16 -0700436 WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
David S. Miller3019de12011-06-06 16:41:33 -0700437 dev->name, netdev_drivername(dev), i);
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800438 dev->netdev_ops->ndo_tx_timeout(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
David S. Millere8a04642008-07-17 00:34:19 -0700440 if (!mod_timer(&dev->watchdog_timer,
441 round_jiffies(jiffies +
442 dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 dev_hold(dev);
444 }
445 }
Herbert Xu932ff272006-06-09 12:20:56 -0700446 netif_tx_unlock(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 dev_put(dev);
449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451void __netdev_watchdog_up(struct net_device *dev)
452{
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800453 if (dev->netdev_ops->ndo_tx_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (dev->watchdog_timeo <= 0)
455 dev->watchdog_timeo = 5*HZ;
Venkatesh Pallipadi60468d52007-05-31 21:28:44 -0700456 if (!mod_timer(&dev->watchdog_timer,
457 round_jiffies(jiffies + dev->watchdog_timeo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 dev_hold(dev);
459 }
460}
461
462static void dev_watchdog_up(struct net_device *dev)
463{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 __netdev_watchdog_up(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467static void dev_watchdog_down(struct net_device *dev)
468{
Herbert Xu932ff272006-06-09 12:20:56 -0700469 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (del_timer(&dev->watchdog_timer))
Stephen Hemminger15333062006-03-20 22:32:28 -0800471 dev_put(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700472 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700475/**
476 * netif_carrier_on - set carrier
477 * @dev: network device
478 *
479 * Device has detected that carrier.
480 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700481void netif_carrier_on(struct net_device *dev)
482{
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700483 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
David S. Millerb4730012008-11-19 15:33:54 -0800484 if (dev->reg_state == NETREG_UNINITIALIZED)
485 return;
david decotigny2d3b4792014-03-29 09:48:35 -0700486 atomic_inc(&dev->carrier_changes);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700487 linkwatch_fire_event(dev);
Jeff Garzikbfaae0f2007-10-17 23:26:43 -0700488 if (netif_running(dev))
489 __netdev_watchdog_up(dev);
490 }
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700491}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800492EXPORT_SYMBOL(netif_carrier_on);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700493
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700494/**
495 * netif_carrier_off - clear carrier
496 * @dev: network device
497 *
498 * Device has detected loss of carrier.
499 */
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700500void netif_carrier_off(struct net_device *dev)
501{
David S. Millerb4730012008-11-19 15:33:54 -0800502 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
503 if (dev->reg_state == NETREG_UNINITIALIZED)
504 return;
david decotigny2d3b4792014-03-29 09:48:35 -0700505 atomic_inc(&dev->carrier_changes);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700506 linkwatch_fire_event(dev);
David S. Millerb4730012008-11-19 15:33:54 -0800507 }
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700508}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800509EXPORT_SYMBOL(netif_carrier_off);
Denis Vlasenko0a242ef2005-08-11 15:32:53 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/* "NOOP" scheduler: the best scheduler, recommended for all interfaces
512 under all circumstances. It is difficult to invent anything faster or
513 cheaper.
514 */
515
Eric Dumazet520ac302016-06-21 23:16:49 -0700516static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
517 struct sk_buff **to_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Eric Dumazet520ac302016-06-21 23:16:49 -0700519 __qdisc_drop(skb, to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return NET_XMIT_CN;
521}
522
Yang Yingliang82d567c2013-12-10 20:55:31 +0800523static struct sk_buff *noop_dequeue(struct Qdisc *qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 return NULL;
526}
527
Eric Dumazet20fea082007-11-14 01:44:41 -0800528struct Qdisc_ops noop_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 .id = "noop",
530 .priv_size = 0,
531 .enqueue = noop_enqueue,
532 .dequeue = noop_dequeue,
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700533 .peek = noop_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 .owner = THIS_MODULE,
535};
536
David S. Miller7698b4f2008-07-16 01:42:40 -0700537static struct netdev_queue noop_netdev_queue = {
David S. Miller7698b4f2008-07-16 01:42:40 -0700538 .qdisc = &noop_qdisc,
Jarek Poplawski9f3ffae2008-10-19 23:37:47 -0700539 .qdisc_sleeping = &noop_qdisc,
David S. Miller7698b4f2008-07-16 01:42:40 -0700540};
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542struct Qdisc noop_qdisc = {
543 .enqueue = noop_enqueue,
544 .dequeue = noop_dequeue,
545 .flags = TCQ_F_BUILTIN,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900546 .ops = &noop_qdisc_ops,
David S. Miller838740002008-07-17 00:53:03 -0700547 .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
David S. Miller7698b4f2008-07-16 01:42:40 -0700548 .dev_queue = &noop_netdev_queue,
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -0700549 .running = SEQCNT_ZERO(noop_qdisc.running),
Eric Dumazet7b5edbc2010-10-15 19:22:34 +0000550 .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551};
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800552EXPORT_SYMBOL(noop_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Phil Sutterd66d6c32015-08-27 21:21:38 +0200554static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt)
555{
556 /* register_qdisc() assigns a default of noop_enqueue if unset,
557 * but __dev_queue_xmit() treats noqueue only as such
558 * if this is NULL - so clear it here. */
559 qdisc->enqueue = NULL;
560 return 0;
561}
562
563struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 .id = "noqueue",
565 .priv_size = 0,
Phil Sutterd66d6c32015-08-27 21:21:38 +0200566 .init = noqueue_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 .enqueue = noop_enqueue,
568 .dequeue = noop_dequeue,
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700569 .peek = noop_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 .owner = THIS_MODULE,
571};
572
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000573static const u8 prio2band[TC_PRIO_MAX + 1] = {
574 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
575};
Thomas Graf321090e2005-06-18 22:58:35 -0700576
David S. Millerd3678b42008-07-21 09:56:13 -0700577/* 3-band FIFO queue: old style, but should be a bit faster than
578 generic prio+fifo combination.
579 */
580
581#define PFIFO_FAST_BANDS 3
582
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000583/*
584 * Private data for a pfifo_fast scheduler containing:
John Fastabendc5ad1192017-12-07 09:58:19 -0800585 * - rings for priority bands
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000586 */
587struct pfifo_fast_priv {
John Fastabendc5ad1192017-12-07 09:58:19 -0800588 struct skb_array q[PFIFO_FAST_BANDS];
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000589};
590
John Fastabendc5ad1192017-12-07 09:58:19 -0800591static inline struct skb_array *band2list(struct pfifo_fast_priv *priv,
592 int band)
David S. Millerd3678b42008-07-21 09:56:13 -0700593{
John Fastabendc5ad1192017-12-07 09:58:19 -0800594 return &priv->q[band];
David S. Millerd3678b42008-07-21 09:56:13 -0700595}
596
Eric Dumazet520ac302016-06-21 23:16:49 -0700597static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
598 struct sk_buff **to_free)
David S. Millerd3678b42008-07-21 09:56:13 -0700599{
John Fastabendc5ad1192017-12-07 09:58:19 -0800600 int band = prio2band[skb->priority & TC_PRIO_MAX];
601 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
602 struct skb_array *q = band2list(priv, band);
603 int err;
David S. Millerd3678b42008-07-21 09:56:13 -0700604
John Fastabendc5ad1192017-12-07 09:58:19 -0800605 err = skb_array_produce(q, skb);
Thomas Graf821d24ae2005-06-18 22:58:15 -0700606
John Fastabendc5ad1192017-12-07 09:58:19 -0800607 if (unlikely(err))
608 return qdisc_drop_cpu(skb, qdisc, to_free);
609
610 qdisc_qstats_cpu_qlen_inc(qdisc);
611 qdisc_qstats_cpu_backlog_inc(qdisc, skb);
612 return NET_XMIT_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000615static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000617 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
John Fastabendc5ad1192017-12-07 09:58:19 -0800618 struct sk_buff *skb = NULL;
619 int band;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
John Fastabendc5ad1192017-12-07 09:58:19 -0800621 for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
622 struct skb_array *q = band2list(priv, band);
Florian Westphalec323362016-09-18 00:57:32 +0200623
John Fastabendc5ad1192017-12-07 09:58:19 -0800624 if (__skb_array_empty(q))
625 continue;
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000626
John Fastabendc5ad1192017-12-07 09:58:19 -0800627 skb = skb_array_consume_bh(q);
628 }
629 if (likely(skb)) {
630 qdisc_qstats_cpu_backlog_dec(qdisc, skb);
631 qdisc_bstats_cpu_update(qdisc, skb);
632 qdisc_qstats_cpu_qlen_dec(qdisc);
David S. Millerd3678b42008-07-21 09:56:13 -0700633 }
Thomas Graff87a9c32005-06-18 22:58:53 -0700634
John Fastabendc5ad1192017-12-07 09:58:19 -0800635 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000638static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700639{
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000640 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
John Fastabendc5ad1192017-12-07 09:58:19 -0800641 struct sk_buff *skb = NULL;
642 int band;
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700643
John Fastabendc5ad1192017-12-07 09:58:19 -0800644 for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
645 struct skb_array *q = band2list(priv, band);
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000646
John Fastabendc5ad1192017-12-07 09:58:19 -0800647 skb = __skb_array_peek(q);
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700648 }
649
John Fastabendc5ad1192017-12-07 09:58:19 -0800650 return skb;
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700651}
652
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000653static void pfifo_fast_reset(struct Qdisc *qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
John Fastabendc5ad1192017-12-07 09:58:19 -0800655 int i, band;
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000656 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
David S. Millerd3678b42008-07-21 09:56:13 -0700657
John Fastabendc5ad1192017-12-07 09:58:19 -0800658 for (band = 0; band < PFIFO_FAST_BANDS; band++) {
659 struct skb_array *q = band2list(priv, band);
660 struct sk_buff *skb;
David S. Millerd3678b42008-07-21 09:56:13 -0700661
Cong Wang1df94c32017-12-18 14:34:26 -0800662 /* NULL ring is possible if destroy path is due to a failed
663 * skb_array_init() in pfifo_fast_init() case.
664 */
665 if (!q->ring.queue)
666 continue;
667
John Fastabendc5ad1192017-12-07 09:58:19 -0800668 while ((skb = skb_array_consume_bh(q)) != NULL)
669 kfree_skb(skb);
670 }
671
672 for_each_possible_cpu(i) {
673 struct gnet_stats_queue *q = per_cpu_ptr(qdisc->cpu_qstats, i);
674
675 q->backlog = 0;
676 q->qlen = 0;
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
David S. Millerd3678b42008-07-21 09:56:13 -0700680static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
681{
682 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
683
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000684 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1);
David S. Miller1b34ec42012-03-29 05:11:39 -0400685 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
686 goto nla_put_failure;
David S. Millerd3678b42008-07-21 09:56:13 -0700687 return skb->len;
688
689nla_put_failure:
690 return -1;
691}
692
693static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
694{
John Fastabendc5ad1192017-12-07 09:58:19 -0800695 unsigned int qlen = qdisc_dev(qdisc)->tx_queue_len;
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000696 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
John Fastabendc5ad1192017-12-07 09:58:19 -0800697 int prio;
David S. Millerd3678b42008-07-21 09:56:13 -0700698
John Fastabendc5ad1192017-12-07 09:58:19 -0800699 /* guard against zero length rings */
700 if (!qlen)
701 return -EINVAL;
702
703 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
704 struct skb_array *q = band2list(priv, prio);
705 int err;
706
707 err = skb_array_init(q, qlen, GFP_KERNEL);
708 if (err)
709 return -ENOMEM;
710 }
David S. Millerd3678b42008-07-21 09:56:13 -0700711
Eric Dumazet23624932011-01-21 16:26:09 -0800712 /* Can by-pass the queue discipline */
713 qdisc->flags |= TCQ_F_CAN_BYPASS;
David S. Millerd3678b42008-07-21 09:56:13 -0700714 return 0;
715}
716
John Fastabendc5ad1192017-12-07 09:58:19 -0800717static void pfifo_fast_destroy(struct Qdisc *sch)
718{
719 struct pfifo_fast_priv *priv = qdisc_priv(sch);
720 int prio;
721
722 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
723 struct skb_array *q = band2list(priv, prio);
724
725 /* NULL ring is possible if destroy path is due to a failed
726 * skb_array_init() in pfifo_fast_init() case.
727 */
Cong Wang1df94c32017-12-18 14:34:26 -0800728 if (!q->ring.queue)
John Fastabendc5ad1192017-12-07 09:58:19 -0800729 continue;
730 /* Destroy ring but no need to kfree_skb because a call to
731 * pfifo_fast_reset() has already done that work.
732 */
733 ptr_ring_cleanup(&q->ring, NULL);
734 }
735}
736
David S. Miller6ec1c692009-09-06 01:58:51 -0700737struct Qdisc_ops pfifo_fast_ops __read_mostly = {
David S. Millerd3678b42008-07-21 09:56:13 -0700738 .id = "pfifo_fast",
Krishna Kumarfd3ae5e2009-08-18 21:55:59 +0000739 .priv_size = sizeof(struct pfifo_fast_priv),
David S. Millerd3678b42008-07-21 09:56:13 -0700740 .enqueue = pfifo_fast_enqueue,
741 .dequeue = pfifo_fast_dequeue,
Jarek Poplawski99c0db22008-10-31 00:45:27 -0700742 .peek = pfifo_fast_peek,
David S. Millerd3678b42008-07-21 09:56:13 -0700743 .init = pfifo_fast_init,
John Fastabendc5ad1192017-12-07 09:58:19 -0800744 .destroy = pfifo_fast_destroy,
David S. Millerd3678b42008-07-21 09:56:13 -0700745 .reset = pfifo_fast_reset,
746 .dump = pfifo_fast_dump,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 .owner = THIS_MODULE,
John Fastabendc5ad1192017-12-07 09:58:19 -0800748 .static_flags = TCQ_F_NOLOCK | TCQ_F_CPUSTATS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749};
Eric Dumazet1f27cde2016-03-02 08:21:43 -0800750EXPORT_SYMBOL(pfifo_fast_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Eric Dumazet23d3b8b2012-09-05 01:02:56 +0000752static struct lock_class_key qdisc_tx_busylock;
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -0700753static struct lock_class_key qdisc_running_key;
Eric Dumazet23d3b8b2012-09-05 01:02:56 +0000754
David S. Miller5ce2d482008-07-08 17:06:30 -0700755struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
stephen hemmingerd2a7f262013-08-31 10:15:50 -0700756 const struct Qdisc_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 void *p;
759 struct Qdisc *sch;
Eric Dumazetd2760552011-03-03 11:10:02 -0800760 unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size;
Thomas Graf3d54b822005-07-05 14:15:09 -0700761 int err = -ENOBUFS;
Jesus Sanchez-Palencia26aa0452017-10-16 18:01:23 -0700762 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Jesus Sanchez-Palencia26aa0452017-10-16 18:01:23 -0700764 if (!dev_queue) {
765 err = -EINVAL;
766 goto errout;
767 }
768
769 dev = dev_queue->dev;
Eric Dumazetf2cd2d32010-11-29 08:14:37 +0000770 p = kzalloc_node(size, GFP_KERNEL,
771 netdev_queue_numa_node_read(dev_queue));
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (!p)
Thomas Graf3d54b822005-07-05 14:15:09 -0700774 goto errout;
Thomas Graf3d54b822005-07-05 14:15:09 -0700775 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
Eric Dumazetd2760552011-03-03 11:10:02 -0800776 /* if we got non aligned memory, ask more and do alignment ourself */
777 if (sch != p) {
778 kfree(p);
779 p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL,
780 netdev_queue_numa_node_read(dev_queue));
781 if (!p)
782 goto errout;
783 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
784 sch->padded = (char *) sch - (char *) p;
785 }
John Fastabenda53851e2017-12-07 09:55:45 -0800786 __skb_queue_head_init(&sch->gso_skb);
John Fastabend70e57d52017-12-07 09:56:23 -0800787 __skb_queue_head_init(&sch->skb_bad_txq);
Florian Westphal48da34b2016-09-18 00:57:34 +0200788 qdisc_skb_head_init(&sch->q);
789 spin_lock_init(&sch->q.lock);
Eric Dumazet23d3b8b2012-09-05 01:02:56 +0000790
John Fastabendd59f5ff2017-12-07 09:55:26 -0800791 if (ops->static_flags & TCQ_F_CPUSTATS) {
792 sch->cpu_bstats =
793 netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
794 if (!sch->cpu_bstats)
795 goto errout1;
796
797 sch->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
798 if (!sch->cpu_qstats) {
799 free_percpu(sch->cpu_bstats);
800 goto errout1;
801 }
802 }
803
Eric Dumazet79640a42010-06-02 05:09:29 -0700804 spin_lock_init(&sch->busylock);
Eric Dumazet23d3b8b2012-09-05 01:02:56 +0000805 lockdep_set_class(&sch->busylock,
806 dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
807
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -0700808 seqcount_init(&sch->running);
809 lockdep_set_class(&sch->running,
810 dev->qdisc_running_key ?: &qdisc_running_key);
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 sch->ops = ops;
John Fastabendd59f5ff2017-12-07 09:55:26 -0800813 sch->flags = ops->static_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 sch->enqueue = ops->enqueue;
815 sch->dequeue = ops->dequeue;
David S. Millerbb949fb2008-07-08 16:55:56 -0700816 sch->dev_queue = dev_queue;
Eric Dumazet23d3b8b2012-09-05 01:02:56 +0000817 dev_hold(dev);
Reshetova, Elena7b936402017-07-04 15:53:07 +0300818 refcount_set(&sch->refcnt, 1);
Thomas Graf3d54b822005-07-05 14:15:09 -0700819
820 return sch;
John Fastabendd59f5ff2017-12-07 09:55:26 -0800821errout1:
822 kfree(p);
Thomas Graf3d54b822005-07-05 14:15:09 -0700823errout:
WANG Cong01e123d2008-06-27 19:51:35 -0700824 return ERR_PTR(err);
Thomas Graf3d54b822005-07-05 14:15:09 -0700825}
826
Changli Gao3511c912010-10-16 13:04:08 +0000827struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
stephen hemmingerd2a7f262013-08-31 10:15:50 -0700828 const struct Qdisc_ops *ops,
829 unsigned int parentid)
Thomas Graf3d54b822005-07-05 14:15:09 -0700830{
831 struct Qdisc *sch;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900832
stephen hemminger6da7c8f2013-08-27 16:19:08 -0700833 if (!try_module_get(ops->owner))
Eric Dumazet166ee5b2016-08-24 09:39:02 -0700834 return NULL;
stephen hemminger6da7c8f2013-08-27 16:19:08 -0700835
David S. Miller5ce2d482008-07-08 17:06:30 -0700836 sch = qdisc_alloc(dev_queue, ops);
Eric Dumazet166ee5b2016-08-24 09:39:02 -0700837 if (IS_ERR(sch)) {
838 module_put(ops->owner);
839 return NULL;
840 }
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800841 sch->parent = parentid;
Thomas Graf3d54b822005-07-05 14:15:09 -0700842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (!ops->init || ops->init(sch, NULL) == 0)
844 return sch;
845
Thomas Graf0fbbeb12005-08-23 10:12:44 -0700846 qdisc_destroy(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return NULL;
848}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800849EXPORT_SYMBOL(qdisc_create_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
David S. Miller5fb66222008-08-02 20:02:43 -0700851/* Under qdisc_lock(qdisc) and BH! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853void qdisc_reset(struct Qdisc *qdisc)
854{
Eric Dumazet20fea082007-11-14 01:44:41 -0800855 const struct Qdisc_ops *ops = qdisc->ops;
John Fastabenda53851e2017-12-07 09:55:45 -0800856 struct sk_buff *skb, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 if (ops->reset)
859 ops->reset(qdisc);
Jarek Poplawski67305eb2008-11-03 02:52:50 -0800860
John Fastabenda53851e2017-12-07 09:55:45 -0800861 skb_queue_walk_safe(&qdisc->gso_skb, skb, tmp) {
862 __skb_unlink(skb, &qdisc->gso_skb);
863 kfree_skb_list(skb);
Krishna Kumarbbd8a0d2009-08-06 01:44:21 +0000864 }
John Fastabenda53851e2017-12-07 09:55:45 -0800865
John Fastabend70e57d52017-12-07 09:56:23 -0800866 skb_queue_walk_safe(&qdisc->skb_bad_txq, skb, tmp) {
867 __skb_unlink(skb, &qdisc->skb_bad_txq);
868 kfree_skb_list(skb);
869 }
870
Eric Dumazet4d202a02016-06-21 23:16:52 -0700871 qdisc->q.qlen = 0;
Konstantin Khlebnikovc8e18122017-09-20 15:45:36 +0300872 qdisc->qstats.backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800874EXPORT_SYMBOL(qdisc_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Cong Wang752fbcc2017-09-19 13:15:42 -0700876static void qdisc_free(struct Qdisc *qdisc)
Eric Dumazet5d944c62010-03-31 07:06:04 +0000877{
John Fastabend73c20a82016-01-05 09:11:36 -0800878 if (qdisc_is_percpu_stats(qdisc)) {
John Fastabend22e0f8b2014-09-28 11:52:56 -0700879 free_percpu(qdisc->cpu_bstats);
John Fastabend73c20a82016-01-05 09:11:36 -0800880 free_percpu(qdisc->cpu_qstats);
881 }
John Fastabend22e0f8b2014-09-28 11:52:56 -0700882
Eric Dumazet5d944c62010-03-31 07:06:04 +0000883 kfree((char *) qdisc - qdisc->padded);
884}
885
David S. Miller1e0d5a52008-08-17 22:31:26 -0700886void qdisc_destroy(struct Qdisc *qdisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Eric Dumazet20fea082007-11-14 01:44:41 -0800888 const struct Qdisc_ops *ops = qdisc->ops;
John Fastabenda53851e2017-12-07 09:55:45 -0800889 struct sk_buff *skb, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
David S. Miller1e0d5a52008-08-17 22:31:26 -0700891 if (qdisc->flags & TCQ_F_BUILTIN ||
Reshetova, Elena7b936402017-07-04 15:53:07 +0300892 !refcount_dec_and_test(&qdisc->refcnt))
David S. Miller1e0d5a52008-08-17 22:31:26 -0700893 return;
894
David S. Miller3a682fb2008-07-20 18:13:01 -0700895#ifdef CONFIG_NET_SCHED
Jiri Kosina59cc1f62016-08-10 11:05:15 +0200896 qdisc_hash_del(qdisc);
Jarek Poplawskif6e0b232008-08-22 03:24:05 -0700897
Eric Dumazeta2da5702011-01-20 03:48:19 +0000898 qdisc_put_stab(rtnl_dereference(qdisc->stab));
David S. Miller3a682fb2008-07-20 18:13:01 -0700899#endif
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800900 gen_kill_estimator(&qdisc->rate_est);
Patrick McHardy85670cc2006-09-27 16:45:45 -0700901 if (ops->reset)
902 ops->reset(qdisc);
903 if (ops->destroy)
904 ops->destroy(qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Patrick McHardy85670cc2006-09-27 16:45:45 -0700906 module_put(ops->owner);
David S. Miller5ce2d482008-07-08 17:06:30 -0700907 dev_put(qdisc_dev(qdisc));
David S. Miller8a34c5d2008-07-17 00:47:45 -0700908
John Fastabenda53851e2017-12-07 09:55:45 -0800909 skb_queue_walk_safe(&qdisc->gso_skb, skb, tmp) {
910 __skb_unlink(skb, &qdisc->gso_skb);
911 kfree_skb_list(skb);
912 }
913
John Fastabend70e57d52017-12-07 09:56:23 -0800914 skb_queue_walk_safe(&qdisc->skb_bad_txq, skb, tmp) {
915 __skb_unlink(skb, &qdisc->skb_bad_txq);
916 kfree_skb_list(skb);
917 }
918
Cong Wang752fbcc2017-09-19 13:15:42 -0700919 qdisc_free(qdisc);
David S. Miller8a34c5d2008-07-17 00:47:45 -0700920}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800921EXPORT_SYMBOL(qdisc_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Patrick McHardy589983c2009-09-04 06:41:20 +0000923/* Attach toplevel qdisc to device queue. */
924struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
925 struct Qdisc *qdisc)
926{
927 struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
928 spinlock_t *root_lock;
929
930 root_lock = qdisc_lock(oqdisc);
931 spin_lock_bh(root_lock);
932
Patrick McHardy589983c2009-09-04 06:41:20 +0000933 /* ... and graft new one */
934 if (qdisc == NULL)
935 qdisc = &noop_qdisc;
936 dev_queue->qdisc_sleeping = qdisc;
937 rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
938
939 spin_unlock_bh(root_lock);
940
941 return oqdisc;
942}
John Fastabendb8970f02011-01-17 08:06:09 +0000943EXPORT_SYMBOL(dev_graft_qdisc);
Patrick McHardy589983c2009-09-04 06:41:20 +0000944
David S. Millere8a04642008-07-17 00:34:19 -0700945static void attach_one_default_qdisc(struct net_device *dev,
946 struct netdev_queue *dev_queue,
947 void *_unused)
948{
Phil Sutter3e692f22015-08-27 21:21:39 +0200949 struct Qdisc *qdisc;
950 const struct Qdisc_ops *ops = default_qdisc_ops;
David S. Millere8a04642008-07-17 00:34:19 -0700951
Phil Sutter3e692f22015-08-27 21:21:39 +0200952 if (dev->priv_flags & IFF_NO_QUEUE)
953 ops = &noqueue_qdisc_ops;
954
955 qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT);
956 if (!qdisc) {
957 netdev_info(dev, "activation failed\n");
958 return;
David S. Millere8a04642008-07-17 00:34:19 -0700959 }
Phil Sutter3e692f22015-08-27 21:21:39 +0200960 if (!netif_is_multiqueue(dev))
Eric Dumazet4eaf3b82015-12-01 20:08:51 -0800961 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
David S. Millere8a04642008-07-17 00:34:19 -0700962 dev_queue->qdisc_sleeping = qdisc;
963}
964
David S. Miller6ec1c692009-09-06 01:58:51 -0700965static void attach_default_qdiscs(struct net_device *dev)
966{
967 struct netdev_queue *txq;
968 struct Qdisc *qdisc;
969
970 txq = netdev_get_tx_queue(dev, 0);
971
Phil Sutter4b469952015-08-13 19:01:07 +0200972 if (!netif_is_multiqueue(dev) ||
Phil Sutter4b469952015-08-13 19:01:07 +0200973 dev->priv_flags & IFF_NO_QUEUE) {
David S. Miller6ec1c692009-09-06 01:58:51 -0700974 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
975 dev->qdisc = txq->qdisc_sleeping;
Eric Dumazet551143d2017-08-24 21:12:28 -0700976 qdisc_refcount_inc(dev->qdisc);
David S. Miller6ec1c692009-09-06 01:58:51 -0700977 } else {
Changli Gao3511c912010-10-16 13:04:08 +0000978 qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT);
David S. Miller6ec1c692009-09-06 01:58:51 -0700979 if (qdisc) {
David S. Miller6ec1c692009-09-06 01:58:51 -0700980 dev->qdisc = qdisc;
Eric Dumazete57a7842013-12-12 15:41:56 -0800981 qdisc->ops->attach(qdisc);
David S. Miller6ec1c692009-09-06 01:58:51 -0700982 }
983 }
Jiri Kosina59cc1f62016-08-10 11:05:15 +0200984#ifdef CONFIG_NET_SCHED
WANG Cong92f91702017-04-04 18:51:30 -0700985 if (dev->qdisc != &noop_qdisc)
Jiri Kosina49b49972017-03-08 16:03:32 +0100986 qdisc_hash_add(dev->qdisc, false);
Jiri Kosina59cc1f62016-08-10 11:05:15 +0200987#endif
David S. Miller6ec1c692009-09-06 01:58:51 -0700988}
989
David S. Millere8a04642008-07-17 00:34:19 -0700990static void transition_one_qdisc(struct net_device *dev,
991 struct netdev_queue *dev_queue,
992 void *_need_watchdog)
993{
David S. Miller838740002008-07-17 00:53:03 -0700994 struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
David S. Millere8a04642008-07-17 00:34:19 -0700995 int *need_watchdog_p = _need_watchdog;
996
David S. Millera9312ae2008-08-17 21:51:03 -0700997 if (!(new_qdisc->flags & TCQ_F_BUILTIN))
998 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
999
David S. Miller838740002008-07-17 00:53:03 -07001000 rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
Phil Sutter3e692f22015-08-27 21:21:39 +02001001 if (need_watchdog_p) {
Eric Dumazet9d214932009-05-17 20:55:16 -07001002 dev_queue->trans_start = 0;
David S. Millere8a04642008-07-17 00:34:19 -07001003 *need_watchdog_p = 1;
Eric Dumazet9d214932009-05-17 20:55:16 -07001004 }
David S. Millere8a04642008-07-17 00:34:19 -07001005}
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007void dev_activate(struct net_device *dev)
1008{
David S. Millere8a04642008-07-17 00:34:19 -07001009 int need_watchdog;
David S. Millerb0e1e642008-07-08 17:42:10 -07001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* No queueing discipline is attached to device;
stephen hemminger6da7c8f2013-08-27 16:19:08 -07001012 * create default one for devices, which need queueing
1013 * and noqueue_qdisc for virtual interfaces
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 */
1015
David S. Miller6ec1c692009-09-06 01:58:51 -07001016 if (dev->qdisc == &noop_qdisc)
1017 attach_default_qdiscs(dev);
Patrick McHardyaf356af2009-09-04 06:41:18 +00001018
Tommy S. Christensencacaddf2005-05-03 16:18:52 -07001019 if (!netif_carrier_ok(dev))
1020 /* Delay activation until next carrier-on event */
1021 return;
1022
David S. Millere8a04642008-07-17 00:34:19 -07001023 need_watchdog = 0;
1024 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
Eric Dumazet24824a02010-10-02 06:11:55 +00001025 if (dev_ingress_queue(dev))
1026 transition_one_qdisc(dev, dev_ingress_queue(dev), NULL);
David S. Millere8a04642008-07-17 00:34:19 -07001027
1028 if (need_watchdog) {
Florian Westphal860e9532016-05-03 16:33:13 +02001029 netif_trans_update(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 dev_watchdog_up(dev);
1031 }
David S. Millerb0e1e642008-07-08 17:42:10 -07001032}
John Fastabendb8970f02011-01-17 08:06:09 +00001033EXPORT_SYMBOL(dev_activate);
David S. Millerb0e1e642008-07-08 17:42:10 -07001034
David S. Millere8a04642008-07-17 00:34:19 -07001035static void dev_deactivate_queue(struct net_device *dev,
1036 struct netdev_queue *dev_queue,
1037 void *_qdisc_default)
David S. Millerb0e1e642008-07-08 17:42:10 -07001038{
David S. Millere8a04642008-07-17 00:34:19 -07001039 struct Qdisc *qdisc_default = _qdisc_default;
David S. Miller970565b2008-07-08 23:10:33 -07001040 struct Qdisc *qdisc;
David S. Millerb0e1e642008-07-08 17:42:10 -07001041
John Fastabend46e5da40a2014-09-12 20:04:52 -07001042 qdisc = rtnl_dereference(dev_queue->qdisc);
David S. Millerb0e1e642008-07-08 17:42:10 -07001043 if (qdisc) {
David S. Miller838740002008-07-17 00:53:03 -07001044 spin_lock_bh(qdisc_lock(qdisc));
1045
David S. Millera9312ae2008-08-17 21:51:03 -07001046 if (!(qdisc->flags & TCQ_F_BUILTIN))
1047 set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
1048
Jarek Poplawskif7a54c12008-08-27 02:22:07 -07001049 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
David S. Millerb0e1e642008-07-08 17:42:10 -07001050 qdisc_reset(qdisc);
David S. Millerd3b753d2008-07-15 20:14:35 -07001051
David S. Miller838740002008-07-17 00:53:03 -07001052 spin_unlock_bh(qdisc_lock(qdisc));
David S. Millerb0e1e642008-07-08 17:42:10 -07001053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
David S. Miller4335cd22008-08-17 21:58:07 -07001056static bool some_qdisc_is_busy(struct net_device *dev)
David S. Millere8a04642008-07-17 00:34:19 -07001057{
1058 unsigned int i;
1059
1060 for (i = 0; i < dev->num_tx_queues; i++) {
1061 struct netdev_queue *dev_queue;
David S. Miller7698b4f2008-07-16 01:42:40 -07001062 spinlock_t *root_lock;
David S. Millere2627c82008-07-16 00:56:32 -07001063 struct Qdisc *q;
David S. Millere8a04642008-07-17 00:34:19 -07001064 int val;
1065
1066 dev_queue = netdev_get_tx_queue(dev, i);
David S. Millerb9a3b112008-08-13 15:18:38 -07001067 q = dev_queue->qdisc_sleeping;
David S. Millere8a04642008-07-17 00:34:19 -07001068
John Fastabend6b3ba912017-12-07 09:54:25 -08001069 if (q->flags & TCQ_F_NOLOCK) {
1070 val = test_bit(__QDISC_STATE_SCHED, &q->state);
1071 } else {
1072 root_lock = qdisc_lock(q);
1073 spin_lock_bh(root_lock);
David S. Millere8a04642008-07-17 00:34:19 -07001074
John Fastabend6b3ba912017-12-07 09:54:25 -08001075 val = (qdisc_is_running(q) ||
1076 test_bit(__QDISC_STATE_SCHED, &q->state));
David S. Millere8a04642008-07-17 00:34:19 -07001077
John Fastabend6b3ba912017-12-07 09:54:25 -08001078 spin_unlock_bh(root_lock);
1079 }
David S. Millere8a04642008-07-17 00:34:19 -07001080
1081 if (val)
1082 return true;
1083 }
1084 return false;
1085}
1086
John Fastabend7bbde832017-12-07 09:56:04 -08001087static void dev_qdisc_reset(struct net_device *dev,
1088 struct netdev_queue *dev_queue,
1089 void *none)
1090{
1091 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
1092
1093 if (qdisc)
1094 qdisc_reset(qdisc);
1095}
1096
Eric Dumazet31376632011-05-19 23:42:09 +00001097/**
1098 * dev_deactivate_many - deactivate transmissions on several devices
1099 * @head: list of devices to deactivate
1100 *
1101 * This function returns only when all outstanding transmissions
1102 * have completed, unless all devices are in dismantle phase.
1103 */
Octavian Purdila44345722010-12-13 12:44:07 +00001104void dev_deactivate_many(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Octavian Purdila44345722010-12-13 12:44:07 +00001106 struct net_device *dev;
Herbert Xu41a23b02007-05-10 14:12:47 -07001107
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001108 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001109 netdev_for_each_tx_queue(dev, dev_deactivate_queue,
1110 &noop_qdisc);
1111 if (dev_ingress_queue(dev))
1112 dev_deactivate_queue(dev, dev_ingress_queue(dev),
1113 &noop_qdisc);
1114
1115 dev_watchdog_down(dev);
1116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Eric Dumazet31376632011-05-19 23:42:09 +00001118 /* Wait for outstanding qdisc-less dev_queue_xmit calls.
1119 * This is avoided if all devices are in dismantle phase :
1120 * Caller will call synchronize_net() for us
1121 */
John Fastabend7bbde832017-12-07 09:56:04 -08001122 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Herbert Xud4828d82006-06-22 02:28:18 -07001124 /* Wait for outstanding qdisc_run calls. */
John Fastabend7bbde832017-12-07 09:56:04 -08001125 list_for_each_entry(dev, head, close_list) {
Octavian Purdila44345722010-12-13 12:44:07 +00001126 while (some_qdisc_is_busy(dev))
1127 yield();
John Fastabend7bbde832017-12-07 09:56:04 -08001128 /* The new qdisc is assigned at this point so we can safely
1129 * unwind stale skb lists and qdisc statistics
1130 */
1131 netdev_for_each_tx_queue(dev, dev_qdisc_reset, NULL);
1132 if (dev_ingress_queue(dev))
1133 dev_qdisc_reset(dev, dev_ingress_queue(dev), NULL);
1134 }
Octavian Purdila44345722010-12-13 12:44:07 +00001135}
1136
1137void dev_deactivate(struct net_device *dev)
1138{
1139 LIST_HEAD(single);
1140
Eric W. Biederman5cde2822013-10-05 19:26:05 -07001141 list_add(&dev->close_list, &single);
Octavian Purdila44345722010-12-13 12:44:07 +00001142 dev_deactivate_many(&single);
Eric W. Biederman5f04d502011-02-20 11:49:45 -08001143 list_del(&single);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
John Fastabendb8970f02011-01-17 08:06:09 +00001145EXPORT_SYMBOL(dev_deactivate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
David S. Millerb0e1e642008-07-08 17:42:10 -07001147static void dev_init_scheduler_queue(struct net_device *dev,
1148 struct netdev_queue *dev_queue,
David S. Millere8a04642008-07-17 00:34:19 -07001149 void *_qdisc)
David S. Millerb0e1e642008-07-08 17:42:10 -07001150{
David S. Millere8a04642008-07-17 00:34:19 -07001151 struct Qdisc *qdisc = _qdisc;
1152
John Fastabend46e5da40a2014-09-12 20:04:52 -07001153 rcu_assign_pointer(dev_queue->qdisc, qdisc);
David S. Millerb0e1e642008-07-08 17:42:10 -07001154 dev_queue->qdisc_sleeping = qdisc;
John Fastabenda53851e2017-12-07 09:55:45 -08001155 __skb_queue_head_init(&qdisc->gso_skb);
John Fastabend70e57d52017-12-07 09:56:23 -08001156 __skb_queue_head_init(&qdisc->skb_bad_txq);
David S. Millerb0e1e642008-07-08 17:42:10 -07001157}
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159void dev_init_scheduler(struct net_device *dev)
1160{
Patrick McHardyaf356af2009-09-04 06:41:18 +00001161 dev->qdisc = &noop_qdisc;
David S. Millere8a04642008-07-17 00:34:19 -07001162 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +00001163 if (dev_ingress_queue(dev))
1164 dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Kees Cookcdeabbb2017-10-16 17:29:17 -07001166 timer_setup(&dev->watchdog_timer, dev_watchdog, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
David S. Millere8a04642008-07-17 00:34:19 -07001169static void shutdown_scheduler_queue(struct net_device *dev,
1170 struct netdev_queue *dev_queue,
1171 void *_qdisc_default)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
David S. Millerb0e1e642008-07-08 17:42:10 -07001173 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
David S. Millere8a04642008-07-17 00:34:19 -07001174 struct Qdisc *qdisc_default = _qdisc_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
David S. Millerb0e1e642008-07-08 17:42:10 -07001176 if (qdisc) {
Jarek Poplawskif7a54c12008-08-27 02:22:07 -07001177 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
David S. Millerb0e1e642008-07-08 17:42:10 -07001178 dev_queue->qdisc_sleeping = qdisc_default;
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 qdisc_destroy(qdisc);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001181 }
David S. Millerb0e1e642008-07-08 17:42:10 -07001182}
1183
1184void dev_shutdown(struct net_device *dev)
1185{
David S. Millere8a04642008-07-17 00:34:19 -07001186 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
Eric Dumazet24824a02010-10-02 06:11:55 +00001187 if (dev_ingress_queue(dev))
1188 shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
Patrick McHardyaf356af2009-09-04 06:41:18 +00001189 qdisc_destroy(dev->qdisc);
1190 dev->qdisc = &noop_qdisc;
1191
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001192 WARN_ON(timer_pending(&dev->watchdog_timer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
Jiri Pirko292f1c72013-02-12 00:12:03 +00001194
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001195void psched_ratecfg_precompute(struct psched_ratecfg *r,
Eric Dumazet3e1e3aa2013-09-19 09:10:03 -07001196 const struct tc_ratespec *conf,
1197 u64 rate64)
Jiri Pirko292f1c72013-02-12 00:12:03 +00001198{
Eric Dumazet01cb71d2013-06-02 13:55:05 +00001199 memset(r, 0, sizeof(*r));
1200 r->overhead = conf->overhead;
Eric Dumazet3e1e3aa2013-09-19 09:10:03 -07001201 r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
Jesper Dangaard Brouer8a8e3d82013-08-14 23:47:11 +02001202 r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
Jiri Pirko292f1c72013-02-12 00:12:03 +00001203 r->mult = 1;
1204 /*
Eric Dumazet130d3d62013-06-06 13:56:19 -07001205 * The deal here is to replace a divide by a reciprocal one
1206 * in fast path (a reciprocal divide is a multiply and a shift)
1207 *
1208 * Normal formula would be :
1209 * time_in_ns = (NSEC_PER_SEC * len) / rate_bps
1210 *
1211 * We compute mult/shift to use instead :
1212 * time_in_ns = (len * mult) >> shift;
1213 *
1214 * We try to get the highest possible mult value for accuracy,
1215 * but have to make sure no overflows will ever happen.
Jiri Pirko292f1c72013-02-12 00:12:03 +00001216 */
Eric Dumazet130d3d62013-06-06 13:56:19 -07001217 if (r->rate_bytes_ps > 0) {
1218 u64 factor = NSEC_PER_SEC;
Jiri Pirko292f1c72013-02-12 00:12:03 +00001219
Eric Dumazet130d3d62013-06-06 13:56:19 -07001220 for (;;) {
1221 r->mult = div64_u64(factor, r->rate_bytes_ps);
1222 if (r->mult & (1U << 31) || factor & (1ULL << 63))
1223 break;
1224 factor <<= 1;
1225 r->shift++;
1226 }
Jiri Pirko292f1c72013-02-12 00:12:03 +00001227 }
1228}
1229EXPORT_SYMBOL(psched_ratecfg_precompute);
Jiri Pirko46209402017-11-03 11:46:25 +01001230
1231static void mini_qdisc_rcu_func(struct rcu_head *head)
1232{
1233}
1234
1235void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1236 struct tcf_proto *tp_head)
1237{
1238 struct mini_Qdisc *miniq_old = rtnl_dereference(*miniqp->p_miniq);
1239 struct mini_Qdisc *miniq;
1240
1241 if (!tp_head) {
1242 RCU_INIT_POINTER(*miniqp->p_miniq, NULL);
1243 return;
1244 }
1245
1246 miniq = !miniq_old || miniq_old == &miniqp->miniq2 ?
1247 &miniqp->miniq1 : &miniqp->miniq2;
1248
1249 /* We need to make sure that readers won't see the miniq
1250 * we are about to modify. So wait until previous call_rcu_bh callback
1251 * is done.
1252 */
1253 rcu_barrier_bh();
1254 miniq->filter_list = tp_head;
1255 rcu_assign_pointer(*miniqp->p_miniq, miniq);
1256
1257 if (miniq_old)
1258 /* This is counterpart of the rcu barrier above. We need to
1259 * block potential new user of miniq_old until all readers
1260 * are not seeing it.
1261 */
1262 call_rcu_bh(&miniq_old->rcu, mini_qdisc_rcu_func);
1263}
1264EXPORT_SYMBOL(mini_qdisc_pair_swap);
1265
1266void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1267 struct mini_Qdisc __rcu **p_miniq)
1268{
1269 miniqp->miniq1.cpu_bstats = qdisc->cpu_bstats;
1270 miniqp->miniq1.cpu_qstats = qdisc->cpu_qstats;
1271 miniqp->miniq2.cpu_bstats = qdisc->cpu_bstats;
1272 miniqp->miniq2.cpu_qstats = qdisc->cpu_qstats;
1273 miniqp->p_miniq = p_miniq;
1274}
1275EXPORT_SYMBOL(mini_qdisc_pair_init);