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