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