]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sched/sch_generic.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / net / sched / sch_generic.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4 14#include <linux/bitops.h>
1da177e4
LT
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
18#include <linux/sched.h>
19#include <linux/string.h>
1da177e4 20#include <linux/errno.h>
1da177e4
LT
21#include <linux/netdevice.h>
22#include <linux/skbuff.h>
23#include <linux/rtnetlink.h>
24#include <linux/init.h>
25#include <linux/rcupdate.h>
26#include <linux/list.h>
5a0e3ad6 27#include <linux/slab.h>
07ce76aa 28#include <linux/if_vlan.h>
32d3e51a 29#include <linux/if_macvlan.h>
292f1c7f 30#include <net/sch_generic.h>
1da177e4 31#include <net/pkt_sched.h>
7fee226a 32#include <net/dst.h>
e543002f 33#include <trace/events/qdisc.h>
1da177e4 34
34aedd3f 35/* Qdisc to use by default */
36const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops;
37EXPORT_SYMBOL(default_qdisc_ops);
38
1da177e4
LT
39/* Main transmission queue. */
40
0463d4ae 41/* Modifications to data participating in scheduling must be protected with
5fb66229 42 * qdisc_lock(qdisc) spinlock.
0463d4ae
PM
43 *
44 * The idea is the following:
c7e4f3bb
DM
45 * - enqueue, dequeue are serialized via qdisc root lock
46 * - ingress filtering is also serialized via qdisc root lock
0463d4ae 47 * - updates to tree and tree walking are only done under the rtnl mutex.
1da177e4 48 */
1da177e4 49
37437bb2 50static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
c716a81a 51{
6252352d 52 q->gso_skb = skb;
53e91503 53 q->qstats.requeues++;
a27758ff 54 qdisc_qstats_backlog_inc(q, skb);
bbd8a0d3 55 q->q.qlen++; /* it's still part of the queue */
37437bb2 56 __netif_schedule(q);
6252352d 57
c716a81a
JHS
58 return 0;
59}
60
55a93b3e
ED
61static void try_bulk_dequeue_skb(struct Qdisc *q,
62 struct sk_buff *skb,
b8358d70
JDB
63 const struct netdev_queue *txq,
64 int *packets)
5772e9a3 65{
55a93b3e 66 int bytelimit = qdisc_avail_bulklimit(txq) - skb->len;
5772e9a3
JDB
67
68 while (bytelimit > 0) {
55a93b3e 69 struct sk_buff *nskb = q->dequeue(q);
5772e9a3 70
55a93b3e 71 if (!nskb)
5772e9a3
JDB
72 break;
73
55a93b3e
ED
74 bytelimit -= nskb->len; /* covers GSO len */
75 skb->next = nskb;
76 skb = nskb;
b8358d70 77 (*packets)++; /* GSO counts as one pkt */
5772e9a3 78 }
55a93b3e 79 skb->next = NULL;
5772e9a3
JDB
80}
81
4d202a0d
ED
82/* This variant of try_bulk_dequeue_skb() makes sure
83 * all skbs in the chain are for the same txq
84 */
85static void try_bulk_dequeue_skb_slow(struct Qdisc *q,
86 struct sk_buff *skb,
87 int *packets)
88{
89 int mapping = skb_get_queue_mapping(skb);
90 struct sk_buff *nskb;
91 int cnt = 0;
92
93 do {
94 nskb = q->dequeue(q);
95 if (!nskb)
96 break;
97 if (unlikely(skb_get_queue_mapping(nskb) != mapping)) {
98 q->skb_bad_txq = nskb;
99 qdisc_qstats_backlog_inc(q, nskb);
100 q->q.qlen++;
101 break;
102 }
103 skb->next = nskb;
104 skb = nskb;
105 } while (++cnt < 8);
106 (*packets) += cnt;
107 skb->next = NULL;
108}
109
5772e9a3
JDB
110/* Note that dequeue_skb can possibly return a SKB list (via skb->next).
111 * A requeued skb (via q->gso_skb) can also be a SKB list.
112 */
b8358d70
JDB
113static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
114 int *packets)
c716a81a 115{
554794de 116 struct sk_buff *skb = q->gso_skb;
1abbe139 117 const struct netdev_queue *txq = q->dev_queue;
554794de 118
b8358d70 119 *packets = 1;
ebf05982 120 if (unlikely(skb)) {
4d202a0d
ED
121 /* skb in gso_skb were already validated */
122 *validate = false;
ebf05982 123 /* check the reason of requeuing without tx lock first */
10c51b56 124 txq = skb_get_tx_queue(txq->dev, skb);
73466498 125 if (!netif_xmit_frozen_or_stopped(txq)) {
6252352d 126 q->gso_skb = NULL;
a27758ff 127 qdisc_qstats_backlog_dec(q, skb);
bbd8a0d3
KK
128 q->q.qlen--;
129 } else
ebf05982 130 skb = NULL;
e543002f 131 goto trace;
4d202a0d
ED
132 }
133 *validate = true;
134 skb = q->skb_bad_txq;
135 if (unlikely(skb)) {
136 /* check the reason of requeuing without tx lock first */
137 txq = skb_get_tx_queue(txq->dev, skb);
138 if (!netif_xmit_frozen_or_stopped(txq)) {
139 q->skb_bad_txq = NULL;
140 qdisc_qstats_backlog_dec(q, skb);
141 q->q.qlen--;
142 goto bulk;
50cbe9ab 143 }
e543002f
JDB
144 skb = NULL;
145 goto trace;
4d202a0d
ED
146 }
147 if (!(q->flags & TCQ_F_ONETXQUEUE) ||
148 !netif_xmit_frozen_or_stopped(txq))
149 skb = q->dequeue(q);
150 if (skb) {
151bulk:
152 if (qdisc_may_bulk(q))
153 try_bulk_dequeue_skb(q, skb, txq, packets);
154 else
155 try_bulk_dequeue_skb_slow(q, skb, packets);
ebf05982 156 }
e543002f
JDB
157trace:
158 trace_qdisc_dequeue(q, txq, *packets, skb);
c716a81a
JHS
159 return skb;
160}
161
10297b99 162/*
10770bc2 163 * Transmit possibly several skbs, and handle the return status as
f9eb8aea 164 * required. Owning running seqcount bit guarantees that
10770bc2 165 * only one CPU can execute this function.
6c1361a6
KK
166 *
167 * Returns to the caller:
168 * 0 - queue is empty or throttled.
169 * >0 - queue is not empty.
6c1361a6 170 */
bbd8a0d3
KK
171int sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
172 struct net_device *dev, struct netdev_queue *txq,
55a93b3e 173 spinlock_t *root_lock, bool validate)
1da177e4 174{
5f1a485d 175 int ret = NETDEV_TX_BUSY;
7698b4fc
DM
176
177 /* And release qdisc */
178 spin_unlock(root_lock);
c716a81a 179
55a93b3e
ED
180 /* Note that we validate skb (GSO, checksum, ...) outside of locks */
181 if (validate)
182 skb = validate_xmit_skb_list(skb, dev);
572a9d7b 183
3dcd493f 184 if (likely(skb)) {
55a93b3e
ED
185 HARD_TX_LOCK(dev, txq, smp_processor_id());
186 if (!netif_xmit_frozen_or_stopped(txq))
187 skb = dev_hard_start_xmit(skb, dev, txq, &ret);
c716a81a 188
55a93b3e 189 HARD_TX_UNLOCK(dev, txq);
3dcd493f 190 } else {
52fbb290 191 spin_lock(root_lock);
3dcd493f 192 return qdisc_qlen(q);
55a93b3e 193 }
52fbb290 194 spin_lock(root_lock);
c716a81a 195
9a1654ba
JP
196 if (dev_xmit_complete(ret)) {
197 /* Driver sent out skb successfully or skb was consumed */
6c1361a6 198 ret = qdisc_qlen(q);
9a1654ba 199 } else {
6c1361a6 200 /* Driver returned NETDEV_TX_BUSY - requeue skb */
e87cc472
JP
201 if (unlikely(ret != NETDEV_TX_BUSY))
202 net_warn_ratelimited("BUG %s code %d qlen %d\n",
203 dev->name, ret, q->q.qlen);
6c1361a6 204
37437bb2 205 ret = dev_requeue_skb(skb, q);
6c1361a6 206 }
c716a81a 207
73466498 208 if (ret && netif_xmit_frozen_or_stopped(txq))
37437bb2
DM
209 ret = 0;
210
6c1361a6 211 return ret;
1da177e4
LT
212}
213
bbd8a0d3
KK
214/*
215 * NOTE: Called under qdisc_lock(q) with locally disabled BH.
216 *
f9eb8aea 217 * running seqcount guarantees only one CPU can process
bbd8a0d3
KK
218 * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
219 * this queue.
220 *
221 * netif_tx_lock serializes accesses to device driver.
222 *
223 * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
224 * if one is grabbed, another must be free.
225 *
226 * Note, that this procedure can be called by a watchdog timer
227 *
228 * Returns to the caller:
229 * 0 - queue is empty or throttled.
230 * >0 - queue is not empty.
231 *
232 */
b8358d70 233static inline int qdisc_restart(struct Qdisc *q, int *packets)
bbd8a0d3
KK
234{
235 struct netdev_queue *txq;
236 struct net_device *dev;
237 spinlock_t *root_lock;
238 struct sk_buff *skb;
55a93b3e 239 bool validate;
bbd8a0d3
KK
240
241 /* Dequeue packet */
b8358d70 242 skb = dequeue_skb(q, &validate, packets);
bbd8a0d3
KK
243 if (unlikely(!skb))
244 return 0;
10c51b56 245
bbd8a0d3
KK
246 root_lock = qdisc_lock(q);
247 dev = qdisc_dev(q);
10c51b56 248 txq = skb_get_tx_queue(dev, skb);
bbd8a0d3 249
55a93b3e 250 return sch_direct_xmit(skb, q, dev, txq, root_lock, validate);
bbd8a0d3
KK
251}
252
37437bb2 253void __qdisc_run(struct Qdisc *q)
48d83325 254{
3d48b53f 255 int quota = dev_tx_weight;
b8358d70 256 int packets;
2ba2506c 257
b8358d70 258 while (qdisc_restart(q, &packets)) {
2ba2506c 259 /*
d5b8aa1d 260 * Ordered by possible occurrence: Postpone processing if
261 * 1. we've exceeded packet quota
262 * 2. another process needs the CPU;
2ba2506c 263 */
b8358d70
JDB
264 quota -= packets;
265 if (quota <= 0 || need_resched()) {
37437bb2 266 __netif_schedule(q);
d90df3ad 267 break;
2ba2506c
HX
268 }
269 }
48d83325 270
bc135b23 271 qdisc_run_end(q);
48d83325
HX
272}
273
9d21493b
ED
274unsigned long dev_trans_start(struct net_device *dev)
275{
07ce76aa 276 unsigned long val, res;
9d21493b
ED
277 unsigned int i;
278
07ce76aa 279 if (is_vlan_dev(dev))
280 dev = vlan_dev_real_dev(dev);
32d3e51a
CD
281 else if (netif_is_macvlan(dev))
282 dev = macvlan_dev_real_dev(dev);
9b36627a
FW
283 res = netdev_get_tx_queue(dev, 0)->trans_start;
284 for (i = 1; i < dev->num_tx_queues; i++) {
9d21493b
ED
285 val = netdev_get_tx_queue(dev, i)->trans_start;
286 if (val && time_after(val, res))
287 res = val;
288 }
07ce76aa 289
9d21493b
ED
290 return res;
291}
292EXPORT_SYMBOL(dev_trans_start);
293
cdeabbb8 294static void dev_watchdog(struct timer_list *t)
1da177e4 295{
cdeabbb8 296 struct net_device *dev = from_timer(dev, t, watchdog_timer);
1da177e4 297
932ff279 298 netif_tx_lock(dev);
e8a0464c 299 if (!qdisc_tx_is_noop(dev)) {
1da177e4
LT
300 if (netif_device_present(dev) &&
301 netif_running(dev) &&
302 netif_carrier_ok(dev)) {
9d21493b 303 int some_queue_timedout = 0;
e8a0464c 304 unsigned int i;
9d21493b 305 unsigned long trans_start;
e8a0464c
DM
306
307 for (i = 0; i < dev->num_tx_queues; i++) {
308 struct netdev_queue *txq;
309
310 txq = netdev_get_tx_queue(dev, i);
9b36627a 311 trans_start = txq->trans_start;
73466498 312 if (netif_xmit_stopped(txq) &&
9d21493b
ED
313 time_after(jiffies, (trans_start +
314 dev->watchdog_timeo))) {
315 some_queue_timedout = 1;
ccf5ff69 316 txq->trans_timeout++;
e8a0464c
DM
317 break;
318 }
319 }
338f7566 320
9d21493b 321 if (some_queue_timedout) {
9d21493b 322 WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
3019de12 323 dev->name, netdev_drivername(dev), i);
d314774c 324 dev->netdev_ops->ndo_tx_timeout(dev);
1da177e4 325 }
e8a0464c
DM
326 if (!mod_timer(&dev->watchdog_timer,
327 round_jiffies(jiffies +
328 dev->watchdog_timeo)))
1da177e4
LT
329 dev_hold(dev);
330 }
331 }
932ff279 332 netif_tx_unlock(dev);
1da177e4
LT
333
334 dev_put(dev);
335}
336
1da177e4
LT
337void __netdev_watchdog_up(struct net_device *dev)
338{
d314774c 339 if (dev->netdev_ops->ndo_tx_timeout) {
1da177e4
LT
340 if (dev->watchdog_timeo <= 0)
341 dev->watchdog_timeo = 5*HZ;
60468d5b
VP
342 if (!mod_timer(&dev->watchdog_timer,
343 round_jiffies(jiffies + dev->watchdog_timeo)))
1da177e4
LT
344 dev_hold(dev);
345 }
346}
347
348static void dev_watchdog_up(struct net_device *dev)
349{
1da177e4 350 __netdev_watchdog_up(dev);
1da177e4
LT
351}
352
353static void dev_watchdog_down(struct net_device *dev)
354{
932ff279 355 netif_tx_lock_bh(dev);
1da177e4 356 if (del_timer(&dev->watchdog_timer))
15333061 357 dev_put(dev);
932ff279 358 netif_tx_unlock_bh(dev);
1da177e4
LT
359}
360
bea3348e
SH
361/**
362 * netif_carrier_on - set carrier
363 * @dev: network device
364 *
365 * Device has detected that carrier.
366 */
0a242efc
DV
367void netif_carrier_on(struct net_device *dev)
368{
bfaae0f0 369 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
b4730016
DM
370 if (dev->reg_state == NETREG_UNINITIALIZED)
371 return;
5e7f6597 372 atomic_inc(&dev->carrier_up_count);
0a242efc 373 linkwatch_fire_event(dev);
bfaae0f0
JG
374 if (netif_running(dev))
375 __netdev_watchdog_up(dev);
376 }
0a242efc 377}
62e3ba1b 378EXPORT_SYMBOL(netif_carrier_on);
0a242efc 379
bea3348e
SH
380/**
381 * netif_carrier_off - clear carrier
382 * @dev: network device
383 *
384 * Device has detected loss of carrier.
385 */
0a242efc
DV
386void netif_carrier_off(struct net_device *dev)
387{
b4730016
DM
388 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
389 if (dev->reg_state == NETREG_UNINITIALIZED)
390 return;
5e7f6597 391 atomic_inc(&dev->carrier_down_count);
0a242efc 392 linkwatch_fire_event(dev);
b4730016 393 }
0a242efc 394}
62e3ba1b 395EXPORT_SYMBOL(netif_carrier_off);
0a242efc 396
1da177e4
LT
397/* "NOOP" scheduler: the best scheduler, recommended for all interfaces
398 under all circumstances. It is difficult to invent anything faster or
399 cheaper.
400 */
401
520ac30f
ED
402static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
403 struct sk_buff **to_free)
1da177e4 404{
520ac30f 405 __qdisc_drop(skb, to_free);
1da177e4
LT
406 return NET_XMIT_CN;
407}
408
82d567c2 409static struct sk_buff *noop_dequeue(struct Qdisc *qdisc)
1da177e4
LT
410{
411 return NULL;
412}
413
20fea08b 414struct Qdisc_ops noop_qdisc_ops __read_mostly = {
1da177e4
LT
415 .id = "noop",
416 .priv_size = 0,
417 .enqueue = noop_enqueue,
418 .dequeue = noop_dequeue,
99c0db26 419 .peek = noop_dequeue,
1da177e4
LT
420 .owner = THIS_MODULE,
421};
422
7698b4fc 423static struct netdev_queue noop_netdev_queue = {
7698b4fc 424 .qdisc = &noop_qdisc,
9f3ffae0 425 .qdisc_sleeping = &noop_qdisc,
7698b4fc
DM
426};
427
1da177e4
LT
428struct Qdisc noop_qdisc = {
429 .enqueue = noop_enqueue,
430 .dequeue = noop_dequeue,
431 .flags = TCQ_F_BUILTIN,
10297b99 432 .ops = &noop_qdisc_ops,
83874000 433 .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
7698b4fc 434 .dev_queue = &noop_netdev_queue,
f9eb8aea 435 .running = SEQCNT_ZERO(noop_qdisc.running),
7b5edbc4 436 .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock),
1da177e4 437};
62e3ba1b 438EXPORT_SYMBOL(noop_qdisc);
1da177e4 439
d66d6c31
PS
440static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt)
441{
442 /* register_qdisc() assigns a default of noop_enqueue if unset,
443 * but __dev_queue_xmit() treats noqueue only as such
444 * if this is NULL - so clear it here. */
445 qdisc->enqueue = NULL;
446 return 0;
447}
448
449struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
1da177e4
LT
450 .id = "noqueue",
451 .priv_size = 0,
d66d6c31 452 .init = noqueue_init,
1da177e4
LT
453 .enqueue = noop_enqueue,
454 .dequeue = noop_dequeue,
99c0db26 455 .peek = noop_dequeue,
1da177e4
LT
456 .owner = THIS_MODULE,
457};
458
cc7ec456
ED
459static const u8 prio2band[TC_PRIO_MAX + 1] = {
460 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
461};
d3678b46
DM
462
463/* 3-band FIFO queue: old style, but should be a bit faster than
464 generic prio+fifo combination.
465 */
466
467#define PFIFO_FAST_BANDS 3
468
fd3ae5e8
KK
469/*
470 * Private data for a pfifo_fast scheduler containing:
471 * - queues for the three band
472 * - bitmap indicating which of the bands contain skbs
473 */
474struct pfifo_fast_priv {
475 u32 bitmap;
48da34b7 476 struct qdisc_skb_head q[PFIFO_FAST_BANDS];
fd3ae5e8
KK
477};
478
479/*
480 * Convert a bitmap to the first band number where an skb is queued, where:
481 * bitmap=0 means there are no skbs on any band.
482 * bitmap=1 means there is an skb on band 0.
483 * bitmap=7 means there are skbs on all 3 bands, etc.
484 */
485static const int bitmap2band[] = {-1, 0, 1, 0, 2, 0, 1, 0};
486
48da34b7 487static inline struct qdisc_skb_head *band2list(struct pfifo_fast_priv *priv,
fd3ae5e8 488 int band)
d3678b46 489{
fd3ae5e8 490 return priv->q + band;
d3678b46
DM
491}
492
520ac30f
ED
493static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
494 struct sk_buff **to_free)
321090e7 495{
97d0678f 496 if (qdisc->q.qlen < qdisc_dev(qdisc)->tx_queue_len) {
a453e068
KK
497 int band = prio2band[skb->priority & TC_PRIO_MAX];
498 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
48da34b7 499 struct qdisc_skb_head *list = band2list(priv, band);
1da177e4 500
fd3ae5e8 501 priv->bitmap |= (1 << band);
d3678b46 502 qdisc->q.qlen++;
821d24ae 503 return __qdisc_enqueue_tail(skb, qdisc, list);
d3678b46 504 }
821d24ae 505
520ac30f 506 return qdisc_drop(skb, qdisc, to_free);
1da177e4
LT
507}
508
cc7ec456 509static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
1da177e4 510{
fd3ae5e8
KK
511 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
512 int band = bitmap2band[priv->bitmap];
1da177e4 513
fd3ae5e8 514 if (likely(band >= 0)) {
48da34b7
FW
515 struct qdisc_skb_head *qh = band2list(priv, band);
516 struct sk_buff *skb = __qdisc_dequeue_head(qh);
ec323368
FW
517
518 if (likely(skb != NULL)) {
519 qdisc_qstats_backlog_dec(qdisc, skb);
520 qdisc_bstats_update(qdisc, skb);
521 }
fd3ae5e8
KK
522
523 qdisc->q.qlen--;
48da34b7 524 if (qh->qlen == 0)
fd3ae5e8
KK
525 priv->bitmap &= ~(1 << band);
526
527 return skb;
d3678b46 528 }
f87a9c3d 529
1da177e4
LT
530 return NULL;
531}
532
cc7ec456 533static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
99c0db26 534{
fd3ae5e8
KK
535 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
536 int band = bitmap2band[priv->bitmap];
537
538 if (band >= 0) {
48da34b7 539 struct qdisc_skb_head *qh = band2list(priv, band);
99c0db26 540
48da34b7 541 return qh->head;
99c0db26
JP
542 }
543
544 return NULL;
545}
546
cc7ec456 547static void pfifo_fast_reset(struct Qdisc *qdisc)
1da177e4 548{
d3678b46 549 int prio;
fd3ae5e8 550 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
d3678b46
DM
551
552 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
1b5c5493 553 __qdisc_reset_queue(band2list(priv, prio));
d3678b46 554
fd3ae5e8 555 priv->bitmap = 0;
821d24ae 556 qdisc->qstats.backlog = 0;
d3678b46 557 qdisc->q.qlen = 0;
1da177e4
LT
558}
559
d3678b46
DM
560static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
561{
562 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
563
cc7ec456 564 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1);
1b34ec43
DM
565 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
566 goto nla_put_failure;
d3678b46
DM
567 return skb->len;
568
569nla_put_failure:
570 return -1;
571}
572
573static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
574{
575 int prio;
fd3ae5e8 576 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
d3678b46
DM
577
578 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
48da34b7 579 qdisc_skb_head_init(band2list(priv, prio));
d3678b46 580
23624935
ED
581 /* Can by-pass the queue discipline */
582 qdisc->flags |= TCQ_F_CAN_BYPASS;
d3678b46
DM
583 return 0;
584}
585
6ec1c69a 586struct Qdisc_ops pfifo_fast_ops __read_mostly = {
d3678b46 587 .id = "pfifo_fast",
fd3ae5e8 588 .priv_size = sizeof(struct pfifo_fast_priv),
d3678b46
DM
589 .enqueue = pfifo_fast_enqueue,
590 .dequeue = pfifo_fast_dequeue,
99c0db26 591 .peek = pfifo_fast_peek,
d3678b46
DM
592 .init = pfifo_fast_init,
593 .reset = pfifo_fast_reset,
594 .dump = pfifo_fast_dump,
1da177e4
LT
595 .owner = THIS_MODULE,
596};
1f27cde3 597EXPORT_SYMBOL(pfifo_fast_ops);
1da177e4 598
23d3b8bf 599static struct lock_class_key qdisc_tx_busylock;
f9eb8aea 600static struct lock_class_key qdisc_running_key;
23d3b8bf 601
5ce2d488 602struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
d2a7f269 603 const struct Qdisc_ops *ops)
1da177e4
LT
604{
605 void *p;
606 struct Qdisc *sch;
d276055c 607 unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size;
3d54b82f 608 int err = -ENOBUFS;
26aa0459
JSP
609 struct net_device *dev;
610
611 if (!dev_queue) {
612 err = -EINVAL;
613 goto errout;
614 }
1da177e4 615
26aa0459 616 dev = dev_queue->dev;
f2cd2d3e
ED
617 p = kzalloc_node(size, GFP_KERNEL,
618 netdev_queue_numa_node_read(dev_queue));
619
1da177e4 620 if (!p)
3d54b82f 621 goto errout;
3d54b82f 622 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
d276055c
ED
623 /* if we got non aligned memory, ask more and do alignment ourself */
624 if (sch != p) {
625 kfree(p);
626 p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL,
627 netdev_queue_numa_node_read(dev_queue));
628 if (!p)
629 goto errout;
630 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
631 sch->padded = (char *) sch - (char *) p;
632 }
48da34b7
FW
633 qdisc_skb_head_init(&sch->q);
634 spin_lock_init(&sch->q.lock);
23d3b8bf 635
81d947e2
DB
636 if (ops->static_flags & TCQ_F_CPUSTATS) {
637 sch->cpu_bstats =
638 netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
639 if (!sch->cpu_bstats)
640 goto errout1;
641
642 sch->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
643 if (!sch->cpu_qstats) {
644 free_percpu(sch->cpu_bstats);
645 goto errout1;
646 }
647 }
648
79640a4c 649 spin_lock_init(&sch->busylock);
23d3b8bf
ED
650 lockdep_set_class(&sch->busylock,
651 dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
652
f9eb8aea
ED
653 seqcount_init(&sch->running);
654 lockdep_set_class(&sch->running,
655 dev->qdisc_running_key ?: &qdisc_running_key);
656
1da177e4 657 sch->ops = ops;
81d947e2 658 sch->flags = ops->static_flags;
1da177e4
LT
659 sch->enqueue = ops->enqueue;
660 sch->dequeue = ops->dequeue;
bb949fbd 661 sch->dev_queue = dev_queue;
23d3b8bf 662 dev_hold(dev);
7b936405 663 refcount_set(&sch->refcnt, 1);
3d54b82f
TG
664
665 return sch;
81d947e2
DB
666errout1:
667 kfree(p);
3d54b82f 668errout:
01e123d7 669 return ERR_PTR(err);
3d54b82f
TG
670}
671
3511c913 672struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
d2a7f269 673 const struct Qdisc_ops *ops,
674 unsigned int parentid)
3d54b82f
TG
675{
676 struct Qdisc *sch;
10297b99 677
6da7c8fc 678 if (!try_module_get(ops->owner))
166ee5b8 679 return NULL;
6da7c8fc 680
5ce2d488 681 sch = qdisc_alloc(dev_queue, ops);
166ee5b8
ED
682 if (IS_ERR(sch)) {
683 module_put(ops->owner);
684 return NULL;
685 }
9f9afec4 686 sch->parent = parentid;
3d54b82f 687
1da177e4
LT
688 if (!ops->init || ops->init(sch, NULL) == 0)
689 return sch;
690
0fbbeb1b 691 qdisc_destroy(sch);
1da177e4
LT
692 return NULL;
693}
62e3ba1b 694EXPORT_SYMBOL(qdisc_create_dflt);
1da177e4 695
5fb66229 696/* Under qdisc_lock(qdisc) and BH! */
1da177e4
LT
697
698void qdisc_reset(struct Qdisc *qdisc)
699{
20fea08b 700 const struct Qdisc_ops *ops = qdisc->ops;
1da177e4
LT
701
702 if (ops->reset)
703 ops->reset(qdisc);
67305ebc 704
4d202a0d
ED
705 kfree_skb(qdisc->skb_bad_txq);
706 qdisc->skb_bad_txq = NULL;
707
bbd8a0d3 708 if (qdisc->gso_skb) {
3f3c7eec 709 kfree_skb_list(qdisc->gso_skb);
bbd8a0d3 710 qdisc->gso_skb = NULL;
bbd8a0d3 711 }
4d202a0d 712 qdisc->q.qlen = 0;
c8e18129 713 qdisc->qstats.backlog = 0;
1da177e4 714}
62e3ba1b 715EXPORT_SYMBOL(qdisc_reset);
1da177e4 716
81d947e2 717void qdisc_free(struct Qdisc *qdisc)
5d944c64 718{
73c20a8b 719 if (qdisc_is_percpu_stats(qdisc)) {
22e0f8b9 720 free_percpu(qdisc->cpu_bstats);
73c20a8b
JF
721 free_percpu(qdisc->cpu_qstats);
722 }
22e0f8b9 723
5d944c64
ED
724 kfree((char *) qdisc - qdisc->padded);
725}
726
1e0d5a57 727void qdisc_destroy(struct Qdisc *qdisc)
1da177e4 728{
eaf7e146
CW
729 const struct Qdisc_ops *ops;
730
731 if (!qdisc)
732 return;
733 ops = qdisc->ops;
8a34c5dc 734
1e0d5a57 735 if (qdisc->flags & TCQ_F_BUILTIN ||
7b936405 736 !refcount_dec_and_test(&qdisc->refcnt))
1e0d5a57
DM
737 return;
738
3a682fbd 739#ifdef CONFIG_NET_SCHED
59cc1f61 740 qdisc_hash_del(qdisc);
f6e0b239 741
a2da570d 742 qdisc_put_stab(rtnl_dereference(qdisc->stab));
3a682fbd 743#endif
1c0d32fd 744 gen_kill_estimator(&qdisc->rate_est);
8a34c5dc
DM
745 if (ops->reset)
746 ops->reset(qdisc);
747 if (ops->destroy)
748 ops->destroy(qdisc);
749
750 module_put(ops->owner);
751 dev_put(qdisc_dev(qdisc));
752
3f3c7eec 753 kfree_skb_list(qdisc->gso_skb);
4d202a0d 754 kfree_skb(qdisc->skb_bad_txq);
752fbcc3 755 qdisc_free(qdisc);
1da177e4 756}
62e3ba1b 757EXPORT_SYMBOL(qdisc_destroy);
1da177e4 758
589983cd
PM
759/* Attach toplevel qdisc to device queue. */
760struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
761 struct Qdisc *qdisc)
762{
763 struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
764 spinlock_t *root_lock;
765
766 root_lock = qdisc_lock(oqdisc);
767 spin_lock_bh(root_lock);
768
589983cd
PM
769 /* ... and graft new one */
770 if (qdisc == NULL)
771 qdisc = &noop_qdisc;
772 dev_queue->qdisc_sleeping = qdisc;
773 rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
774
775 spin_unlock_bh(root_lock);
776
777 return oqdisc;
778}
b8970f0b 779EXPORT_SYMBOL(dev_graft_qdisc);
589983cd 780
e8a0464c
DM
781static void attach_one_default_qdisc(struct net_device *dev,
782 struct netdev_queue *dev_queue,
783 void *_unused)
784{
3e692f21
PS
785 struct Qdisc *qdisc;
786 const struct Qdisc_ops *ops = default_qdisc_ops;
e8a0464c 787
3e692f21
PS
788 if (dev->priv_flags & IFF_NO_QUEUE)
789 ops = &noqueue_qdisc_ops;
790
791 qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT);
792 if (!qdisc) {
793 netdev_info(dev, "activation failed\n");
794 return;
e8a0464c 795 }
3e692f21 796 if (!netif_is_multiqueue(dev))
4eaf3b84 797 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
e8a0464c
DM
798 dev_queue->qdisc_sleeping = qdisc;
799}
800
6ec1c69a
DM
801static void attach_default_qdiscs(struct net_device *dev)
802{
803 struct netdev_queue *txq;
804 struct Qdisc *qdisc;
805
806 txq = netdev_get_tx_queue(dev, 0);
807
4b469955 808 if (!netif_is_multiqueue(dev) ||
4b469955 809 dev->priv_flags & IFF_NO_QUEUE) {
6ec1c69a
DM
810 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
811 dev->qdisc = txq->qdisc_sleeping;
551143d8 812 qdisc_refcount_inc(dev->qdisc);
6ec1c69a 813 } else {
3511c913 814 qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT);
6ec1c69a 815 if (qdisc) {
6ec1c69a 816 dev->qdisc = qdisc;
e57a784d 817 qdisc->ops->attach(qdisc);
6ec1c69a
DM
818 }
819 }
59cc1f61 820#ifdef CONFIG_NET_SCHED
92f91706 821 if (dev->qdisc != &noop_qdisc)
49b49971 822 qdisc_hash_add(dev->qdisc, false);
59cc1f61 823#endif
6ec1c69a
DM
824}
825
e8a0464c
DM
826static void transition_one_qdisc(struct net_device *dev,
827 struct netdev_queue *dev_queue,
828 void *_need_watchdog)
829{
83874000 830 struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
e8a0464c
DM
831 int *need_watchdog_p = _need_watchdog;
832
a9312ae8
DM
833 if (!(new_qdisc->flags & TCQ_F_BUILTIN))
834 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
835
83874000 836 rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
3e692f21 837 if (need_watchdog_p) {
9d21493b 838 dev_queue->trans_start = 0;
e8a0464c 839 *need_watchdog_p = 1;
9d21493b 840 }
e8a0464c
DM
841}
842
1da177e4
LT
843void dev_activate(struct net_device *dev)
844{
e8a0464c 845 int need_watchdog;
b0e1e646 846
1da177e4 847 /* No queueing discipline is attached to device;
6da7c8fc 848 * create default one for devices, which need queueing
849 * and noqueue_qdisc for virtual interfaces
1da177e4
LT
850 */
851
6ec1c69a
DM
852 if (dev->qdisc == &noop_qdisc)
853 attach_default_qdiscs(dev);
af356afa 854
cacaddf5
TC
855 if (!netif_carrier_ok(dev))
856 /* Delay activation until next carrier-on event */
857 return;
858
e8a0464c
DM
859 need_watchdog = 0;
860 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
24824a09
ED
861 if (dev_ingress_queue(dev))
862 transition_one_qdisc(dev, dev_ingress_queue(dev), NULL);
e8a0464c
DM
863
864 if (need_watchdog) {
860e9538 865 netif_trans_update(dev);
1da177e4
LT
866 dev_watchdog_up(dev);
867 }
b0e1e646 868}
b8970f0b 869EXPORT_SYMBOL(dev_activate);
b0e1e646 870
e8a0464c
DM
871static void dev_deactivate_queue(struct net_device *dev,
872 struct netdev_queue *dev_queue,
873 void *_qdisc_default)
b0e1e646 874{
e8a0464c 875 struct Qdisc *qdisc_default = _qdisc_default;
970565bb 876 struct Qdisc *qdisc;
970565bb 877
46e5da40 878 qdisc = rtnl_dereference(dev_queue->qdisc);
b0e1e646 879 if (qdisc) {
83874000
DM
880 spin_lock_bh(qdisc_lock(qdisc));
881
a9312ae8
DM
882 if (!(qdisc->flags & TCQ_F_BUILTIN))
883 set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
884
f7a54c13 885 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
b0e1e646 886 qdisc_reset(qdisc);
d3b753db 887
83874000 888 spin_unlock_bh(qdisc_lock(qdisc));
b0e1e646 889 }
1da177e4
LT
890}
891
4335cd2d 892static bool some_qdisc_is_busy(struct net_device *dev)
e8a0464c
DM
893{
894 unsigned int i;
895
896 for (i = 0; i < dev->num_tx_queues; i++) {
897 struct netdev_queue *dev_queue;
7698b4fc 898 spinlock_t *root_lock;
e2627c8c 899 struct Qdisc *q;
e8a0464c
DM
900 int val;
901
902 dev_queue = netdev_get_tx_queue(dev, i);
b9a3b110 903 q = dev_queue->qdisc_sleeping;
5fb66229 904 root_lock = qdisc_lock(q);
e8a0464c 905
4335cd2d 906 spin_lock_bh(root_lock);
e8a0464c 907
bc135b23 908 val = (qdisc_is_running(q) ||
b9a3b110 909 test_bit(__QDISC_STATE_SCHED, &q->state));
e8a0464c 910
4335cd2d 911 spin_unlock_bh(root_lock);
e8a0464c
DM
912
913 if (val)
914 return true;
915 }
916 return false;
917}
918
da4d0145
JF
919static void dev_qdisc_reset(struct net_device *dev,
920 struct netdev_queue *dev_queue,
921 void *none)
922{
923 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
924
925 if (qdisc)
926 qdisc_reset(qdisc);
927}
928
3137663d
ED
929/**
930 * dev_deactivate_many - deactivate transmissions on several devices
931 * @head: list of devices to deactivate
932 *
933 * This function returns only when all outstanding transmissions
934 * have completed, unless all devices are in dismantle phase.
935 */
44345724 936void dev_deactivate_many(struct list_head *head)
1da177e4 937{
44345724 938 struct net_device *dev;
41a23b07 939
5cde2829 940 list_for_each_entry(dev, head, close_list) {
44345724
OP
941 netdev_for_each_tx_queue(dev, dev_deactivate_queue,
942 &noop_qdisc);
943 if (dev_ingress_queue(dev))
944 dev_deactivate_queue(dev, dev_ingress_queue(dev),
945 &noop_qdisc);
946
947 dev_watchdog_down(dev);
948 }
1da177e4 949
3137663d
ED
950 /* Wait for outstanding qdisc-less dev_queue_xmit calls.
951 * This is avoided if all devices are in dismantle phase :
952 * Caller will call synchronize_net() for us
953 */
da4d0145 954 synchronize_net();
1da177e4 955
d4828d85 956 /* Wait for outstanding qdisc_run calls. */
da4d0145 957 list_for_each_entry(dev, head, close_list) {
44345724
OP
958 while (some_qdisc_is_busy(dev))
959 yield();
da4d0145
JF
960 /* The new qdisc is assigned at this point so we can safely
961 * unwind stale skb lists and qdisc statistics
962 */
963 netdev_for_each_tx_queue(dev, dev_qdisc_reset, NULL);
964 if (dev_ingress_queue(dev))
965 dev_qdisc_reset(dev, dev_ingress_queue(dev), NULL);
966 }
44345724
OP
967}
968
969void dev_deactivate(struct net_device *dev)
970{
971 LIST_HEAD(single);
972
5cde2829 973 list_add(&dev->close_list, &single);
44345724 974 dev_deactivate_many(&single);
5f04d506 975 list_del(&single);
1da177e4 976}
b8970f0b 977EXPORT_SYMBOL(dev_deactivate);
1da177e4 978
b0e1e646
DM
979static void dev_init_scheduler_queue(struct net_device *dev,
980 struct netdev_queue *dev_queue,
e8a0464c 981 void *_qdisc)
b0e1e646 982{
e8a0464c
DM
983 struct Qdisc *qdisc = _qdisc;
984
46e5da40 985 rcu_assign_pointer(dev_queue->qdisc, qdisc);
b0e1e646 986 dev_queue->qdisc_sleeping = qdisc;
b0e1e646
DM
987}
988
1da177e4
LT
989void dev_init_scheduler(struct net_device *dev)
990{
af356afa 991 dev->qdisc = &noop_qdisc;
e8a0464c 992 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
24824a09
ED
993 if (dev_ingress_queue(dev))
994 dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
1da177e4 995
cdeabbb8 996 timer_setup(&dev->watchdog_timer, dev_watchdog, 0);
1da177e4
LT
997}
998
e8a0464c
DM
999static void shutdown_scheduler_queue(struct net_device *dev,
1000 struct netdev_queue *dev_queue,
1001 void *_qdisc_default)
1da177e4 1002{
b0e1e646 1003 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
e8a0464c 1004 struct Qdisc *qdisc_default = _qdisc_default;
b0e1e646
DM
1005
1006 if (qdisc) {
f7a54c13 1007 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
b0e1e646 1008 dev_queue->qdisc_sleeping = qdisc_default;
1da177e4 1009
1da177e4 1010 qdisc_destroy(qdisc);
10297b99 1011 }
b0e1e646
DM
1012}
1013
1014void dev_shutdown(struct net_device *dev)
1015{
e8a0464c 1016 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
24824a09
ED
1017 if (dev_ingress_queue(dev))
1018 shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
af356afa
PM
1019 qdisc_destroy(dev->qdisc);
1020 dev->qdisc = &noop_qdisc;
1021
547b792c 1022 WARN_ON(timer_pending(&dev->watchdog_timer));
1da177e4 1023}
292f1c7f 1024
01cb71d2 1025void psched_ratecfg_precompute(struct psched_ratecfg *r,
3e1e3aae
ED
1026 const struct tc_ratespec *conf,
1027 u64 rate64)
292f1c7f 1028{
01cb71d2
ED
1029 memset(r, 0, sizeof(*r));
1030 r->overhead = conf->overhead;
3e1e3aae 1031 r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
8a8e3d84 1032 r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
292f1c7f
JP
1033 r->mult = 1;
1034 /*
130d3d68
ED
1035 * The deal here is to replace a divide by a reciprocal one
1036 * in fast path (a reciprocal divide is a multiply and a shift)
1037 *
1038 * Normal formula would be :
1039 * time_in_ns = (NSEC_PER_SEC * len) / rate_bps
1040 *
1041 * We compute mult/shift to use instead :
1042 * time_in_ns = (len * mult) >> shift;
1043 *
1044 * We try to get the highest possible mult value for accuracy,
1045 * but have to make sure no overflows will ever happen.
292f1c7f 1046 */
130d3d68
ED
1047 if (r->rate_bytes_ps > 0) {
1048 u64 factor = NSEC_PER_SEC;
1049
1050 for (;;) {
1051 r->mult = div64_u64(factor, r->rate_bytes_ps);
1052 if (r->mult & (1U << 31) || factor & (1ULL << 63))
292f1c7f 1053 break;
130d3d68
ED
1054 factor <<= 1;
1055 r->shift++;
292f1c7f 1056 }
292f1c7f
JP
1057 }
1058}
1059EXPORT_SYMBOL(psched_ratecfg_precompute);
46209401
JP
1060
1061static void mini_qdisc_rcu_func(struct rcu_head *head)
1062{
1063}
1064
1065void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1066 struct tcf_proto *tp_head)
1067{
1068 struct mini_Qdisc *miniq_old = rtnl_dereference(*miniqp->p_miniq);
1069 struct mini_Qdisc *miniq;
1070
1071 if (!tp_head) {
1072 RCU_INIT_POINTER(*miniqp->p_miniq, NULL);
b2fb01f4
CW
1073 /* Wait for flying RCU callback before it is freed. */
1074 rcu_barrier_bh();
46209401
JP
1075 return;
1076 }
1077
1078 miniq = !miniq_old || miniq_old == &miniqp->miniq2 ?
1079 &miniqp->miniq1 : &miniqp->miniq2;
1080
1081 /* We need to make sure that readers won't see the miniq
1082 * we are about to modify. So wait until previous call_rcu_bh callback
1083 * is done.
1084 */
1085 rcu_barrier_bh();
1086 miniq->filter_list = tp_head;
1087 rcu_assign_pointer(*miniqp->p_miniq, miniq);
1088
1089 if (miniq_old)
b2fb01f4 1090 /* This is counterpart of the rcu barriers above. We need to
46209401
JP
1091 * block potential new user of miniq_old until all readers
1092 * are not seeing it.
1093 */
1094 call_rcu_bh(&miniq_old->rcu, mini_qdisc_rcu_func);
1095}
1096EXPORT_SYMBOL(mini_qdisc_pair_swap);
1097
1098void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1099 struct mini_Qdisc __rcu **p_miniq)
1100{
1101 miniqp->miniq1.cpu_bstats = qdisc->cpu_bstats;
1102 miniqp->miniq1.cpu_qstats = qdisc->cpu_qstats;
1103 miniqp->miniq2.cpu_bstats = qdisc->cpu_bstats;
1104 miniqp->miniq2.cpu_qstats = qdisc->cpu_qstats;
1105 miniqp->p_miniq = p_miniq;
1106}
1107EXPORT_SYMBOL(mini_qdisc_pair_init);