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