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