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