5848d2fda214bfea0e70a6e89da8f2713b55c20e
[openwrt/svn-archive/archive.git] / target / linux / generic / patches-3.3 / 040-Controlled-Delay-AQM.patch
1 From 76e3cc126bb223013a6b9a0e2a51238d1ef2e409 Mon Sep 17 00:00:00 2001
2 From: Eric Dumazet <edumazet@google.com>
3 Date: Thu, 10 May 2012 07:51:25 +0000
4 Subject: [PATCH 01/41] codel: Controlled Delay AQM
5
6 An implementation of CoDel AQM, from Kathleen Nichols and Van Jacobson.
7
8 http://queue.acm.org/detail.cfm?id=2209336
9
10 This AQM main input is no longer queue size in bytes or packets, but the
11 delay packets stay in (FIFO) queue.
12
13 As we don't have infinite memory, we still can drop packets in enqueue()
14 in case of massive load, but mean of CoDel is to drop packets in
15 dequeue(), using a control law based on two simple parameters :
16
17 target : target sojourn time (default 5ms)
18 interval : width of moving time window (default 100ms)
19
20 Based on initial work from Dave Taht.
21
22 Refactored to help future codel inclusion as a plugin for other linux
23 qdisc (FQ_CODEL, ...), like RED.
24
25 include/net/codel.h contains codel algorithm as close as possible than
26 Kathleen reference.
27
28 net/sched/sch_codel.c contains the linux qdisc specific glue.
29
30 Separate structures permit a memory efficient implementation of fq_codel
31 (to be sent as a separate work) : Each flow has its own struct
32 codel_vars.
33
34 timestamps are taken at enqueue() time with 1024 ns precision, allowing
35 a range of 2199 seconds in queue, and 100Gb links support. iproute2 uses
36 usec as base unit.
37
38 Selected packets are dropped, unless ECN is enabled and packets can get
39 ECN mark instead.
40
41 Tested from 2Mb to 10Gb speeds with no particular problems, on ixgbe and
42 tg3 drivers (BQL enabled).
43
44 Usage: tc qdisc ... codel [ limit PACKETS ] [ target TIME ]
45 [ interval TIME ] [ ecn ]
46
47 qdisc codel 10: parent 1:1 limit 2000p target 3.0ms interval 60.0ms ecn
48 Sent 13347099587 bytes 8815805 pkt (dropped 0, overlimits 0 requeues 0)
49 rate 202365Kbit 16708pps backlog 113550b 75p requeues 0
50 count 116 lastcount 98 ldelay 4.3ms dropping drop_next 816us
51 maxpacket 1514 ecn_mark 84399 drop_overlimit 0
52
53 CoDel must be seen as a base module, and should be used keeping in mind
54 there is still a FIFO queue. So a typical setup will probably need a
55 hierarchy of several qdiscs and packet classifiers to be able to meet
56 whatever constraints a user might have.
57
58 One possible example would be to use fq_codel, which combines Fair
59 Queueing and CoDel, in replacement of sfq / sfq_red.
60
61 Signed-off-by: Eric Dumazet <edumazet@google.com>
62 Signed-off-by: Dave Taht <dave.taht@bufferbloat.net>
63 Cc: Kathleen Nichols <nichols@pollere.com>
64 Cc: Van Jacobson <van@pollere.net>
65 Cc: Tom Herbert <therbert@google.com>
66 Cc: Matt Mathis <mattmathis@google.com>
67 Cc: Yuchung Cheng <ycheng@google.com>
68 Cc: Stephen Hemminger <shemminger@vyatta.com>
69 Signed-off-by: David S. Miller <davem@davemloft.net>
70 ---
71 include/linux/pkt_sched.h | 26 ++++
72 include/net/codel.h | 332 +++++++++++++++++++++++++++++++++++++++++++++
73 net/sched/Kconfig | 11 ++
74 net/sched/Makefile | 1 +
75 net/sched/sch_codel.c | 275 +++++++++++++++++++++++++++++++++++++
76 5 files changed, 645 insertions(+)
77 create mode 100644 include/net/codel.h
78 create mode 100644 net/sched/sch_codel.c
79
80 --- a/include/linux/pkt_sched.h
81 +++ b/include/linux/pkt_sched.h
82 @@ -633,4 +633,30 @@ struct tc_qfq_stats {
83 __u32 lmax;
84 };
85
86 +/* CODEL */
87 +
88 +enum {
89 + TCA_CODEL_UNSPEC,
90 + TCA_CODEL_TARGET,
91 + TCA_CODEL_LIMIT,
92 + TCA_CODEL_INTERVAL,
93 + TCA_CODEL_ECN,
94 + __TCA_CODEL_MAX
95 +};
96 +
97 +#define TCA_CODEL_MAX (__TCA_CODEL_MAX - 1)
98 +
99 +struct tc_codel_xstats {
100 + __u32 maxpacket; /* largest packet we've seen so far */
101 + __u32 count; /* how many drops we've done since the last time we
102 + * entered dropping state
103 + */
104 + __u32 lastcount; /* count at entry to dropping state */
105 + __u32 ldelay; /* in-queue delay seen by most recently dequeued packet */
106 + __s32 drop_next; /* time to drop next packet */
107 + __u32 drop_overlimit; /* number of time max qdisc packet limit was hit */
108 + __u32 ecn_mark; /* number of packets we ECN marked instead of dropped */
109 + __u32 dropping; /* are we in dropping state ? */
110 +};
111 +
112 #endif
113 --- /dev/null
114 +++ b/include/net/codel.h
115 @@ -0,0 +1,332 @@
116 +#ifndef __NET_SCHED_CODEL_H
117 +#define __NET_SCHED_CODEL_H
118 +
119 +/*
120 + * Codel - The Controlled-Delay Active Queue Management algorithm
121 + *
122 + * Copyright (C) 2011-2012 Kathleen Nichols <nichols@pollere.com>
123 + * Copyright (C) 2011-2012 Van Jacobson <van@pollere.net>
124 + * Copyright (C) 2012 Michael D. Taht <dave.taht@bufferbloat.net>
125 + * Copyright (C) 2012 Eric Dumazet <edumazet@google.com>
126 + *
127 + * Redistribution and use in source and binary forms, with or without
128 + * modification, are permitted provided that the following conditions
129 + * are met:
130 + * 1. Redistributions of source code must retain the above copyright
131 + * notice, this list of conditions, and the following disclaimer,
132 + * without modification.
133 + * 2. Redistributions in binary form must reproduce the above copyright
134 + * notice, this list of conditions and the following disclaimer in the
135 + * documentation and/or other materials provided with the distribution.
136 + * 3. The names of the authors may not be used to endorse or promote products
137 + * derived from this software without specific prior written permission.
138 + *
139 + * Alternatively, provided that this notice is retained in full, this
140 + * software may be distributed under the terms of the GNU General
141 + * Public License ("GPL") version 2, in which case the provisions of the
142 + * GPL apply INSTEAD OF those given above.
143 + *
144 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
145 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
146 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
147 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
148 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
149 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
150 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
151 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
152 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
153 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
154 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
155 + * DAMAGE.
156 + *
157 + */
158 +
159 +#include <linux/types.h>
160 +#include <linux/ktime.h>
161 +#include <linux/skbuff.h>
162 +#include <net/pkt_sched.h>
163 +#include <net/inet_ecn.h>
164 +
165 +/* Controlling Queue Delay (CoDel) algorithm
166 + * =========================================
167 + * Source : Kathleen Nichols and Van Jacobson
168 + * http://queue.acm.org/detail.cfm?id=2209336
169 + *
170 + * Implemented on linux by Dave Taht and Eric Dumazet
171 + */
172 +
173 +
174 +/* CoDel uses a 1024 nsec clock, encoded in u32
175 + * This gives a range of 2199 seconds, because of signed compares
176 + */
177 +typedef u32 codel_time_t;
178 +typedef s32 codel_tdiff_t;
179 +#define CODEL_SHIFT 10
180 +#define MS2TIME(a) ((a * NSEC_PER_MSEC) >> CODEL_SHIFT)
181 +
182 +static inline codel_time_t codel_get_time(void)
183 +{
184 + u64 ns = ktime_to_ns(ktime_get());
185 +
186 + return ns >> CODEL_SHIFT;
187 +}
188 +
189 +#define codel_time_after(a, b) ((s32)(a) - (s32)(b) > 0)
190 +#define codel_time_after_eq(a, b) ((s32)(a) - (s32)(b) >= 0)
191 +#define codel_time_before(a, b) ((s32)(a) - (s32)(b) < 0)
192 +#define codel_time_before_eq(a, b) ((s32)(a) - (s32)(b) <= 0)
193 +
194 +/* Qdiscs using codel plugin must use codel_skb_cb in their own cb[] */
195 +struct codel_skb_cb {
196 + codel_time_t enqueue_time;
197 +};
198 +
199 +static struct codel_skb_cb *get_codel_cb(const struct sk_buff *skb)
200 +{
201 + qdisc_cb_private_validate(skb, sizeof(struct codel_skb_cb));
202 + return (struct codel_skb_cb *)qdisc_skb_cb(skb)->data;
203 +}
204 +
205 +static codel_time_t codel_get_enqueue_time(const struct sk_buff *skb)
206 +{
207 + return get_codel_cb(skb)->enqueue_time;
208 +}
209 +
210 +static void codel_set_enqueue_time(struct sk_buff *skb)
211 +{
212 + get_codel_cb(skb)->enqueue_time = codel_get_time();
213 +}
214 +
215 +static inline u32 codel_time_to_us(codel_time_t val)
216 +{
217 + u64 valns = ((u64)val << CODEL_SHIFT);
218 +
219 + do_div(valns, NSEC_PER_USEC);
220 + return (u32)valns;
221 +}
222 +
223 +/**
224 + * struct codel_params - contains codel parameters
225 + * @target: target queue size (in time units)
226 + * @interval: width of moving time window
227 + * @ecn: is Explicit Congestion Notification enabled
228 + */
229 +struct codel_params {
230 + codel_time_t target;
231 + codel_time_t interval;
232 + bool ecn;
233 +};
234 +
235 +/**
236 + * struct codel_vars - contains codel variables
237 + * @count: how many drops we've done since the last time we
238 + * entered dropping state
239 + * @lastcount: count at entry to dropping state
240 + * @dropping: set to true if in dropping state
241 + * @first_above_time: when we went (or will go) continuously above target
242 + * for interval
243 + * @drop_next: time to drop next packet, or when we dropped last
244 + * @ldelay: sojourn time of last dequeued packet
245 + */
246 +struct codel_vars {
247 + u32 count;
248 + u32 lastcount;
249 + bool dropping;
250 + codel_time_t first_above_time;
251 + codel_time_t drop_next;
252 + codel_time_t ldelay;
253 +};
254 +
255 +/**
256 + * struct codel_stats - contains codel shared variables and stats
257 + * @maxpacket: largest packet we've seen so far
258 + * @drop_count: temp count of dropped packets in dequeue()
259 + * ecn_mark: number of packets we ECN marked instead of dropping
260 + */
261 +struct codel_stats {
262 + u32 maxpacket;
263 + u32 drop_count;
264 + u32 ecn_mark;
265 +};
266 +
267 +static void codel_params_init(struct codel_params *params)
268 +{
269 + params->interval = MS2TIME(100);
270 + params->target = MS2TIME(5);
271 + params->ecn = false;
272 +}
273 +
274 +static void codel_vars_init(struct codel_vars *vars)
275 +{
276 + vars->drop_next = 0;
277 + vars->first_above_time = 0;
278 + vars->dropping = false; /* exit dropping state */
279 + vars->count = 0;
280 + vars->lastcount = 0;
281 +}
282 +
283 +static void codel_stats_init(struct codel_stats *stats)
284 +{
285 + stats->maxpacket = 256;
286 +}
287 +
288 +/* return interval/sqrt(x) with good precision
289 + * relies on int_sqrt(unsigned long x) kernel implementation
290 + */
291 +static u32 codel_inv_sqrt(u32 _interval, u32 _x)
292 +{
293 + u64 interval = _interval;
294 + unsigned long x = _x;
295 +
296 + /* Scale operands for max precision */
297 +
298 +#if BITS_PER_LONG == 64
299 + x <<= 32; /* On 64bit arches, we can prescale x by 32bits */
300 + interval <<= 16;
301 +#endif
302 +
303 + while (x < (1UL << (BITS_PER_LONG - 2))) {
304 + x <<= 2;
305 + interval <<= 1;
306 + }
307 + do_div(interval, int_sqrt(x));
308 + return (u32)interval;
309 +}
310 +
311 +static codel_time_t codel_control_law(codel_time_t t,
312 + codel_time_t interval,
313 + u32 count)
314 +{
315 + return t + codel_inv_sqrt(interval, count);
316 +}
317 +
318 +
319 +static bool codel_should_drop(struct sk_buff *skb,
320 + unsigned int *backlog,
321 + struct codel_vars *vars,
322 + struct codel_params *params,
323 + struct codel_stats *stats,
324 + codel_time_t now)
325 +{
326 + bool ok_to_drop;
327 +
328 + if (!skb) {
329 + vars->first_above_time = 0;
330 + return false;
331 + }
332 +
333 + vars->ldelay = now - codel_get_enqueue_time(skb);
334 + *backlog -= qdisc_pkt_len(skb);
335 +
336 + if (unlikely(qdisc_pkt_len(skb) > stats->maxpacket))
337 + stats->maxpacket = qdisc_pkt_len(skb);
338 +
339 + if (codel_time_before(vars->ldelay, params->target) ||
340 + *backlog <= stats->maxpacket) {
341 + /* went below - stay below for at least interval */
342 + vars->first_above_time = 0;
343 + return false;
344 + }
345 + ok_to_drop = false;
346 + if (vars->first_above_time == 0) {
347 + /* just went above from below. If we stay above
348 + * for at least interval we'll say it's ok to drop
349 + */
350 + vars->first_above_time = now + params->interval;
351 + } else if (codel_time_after(now, vars->first_above_time)) {
352 + ok_to_drop = true;
353 + }
354 + return ok_to_drop;
355 +}
356 +
357 +typedef struct sk_buff * (*codel_skb_dequeue_t)(struct codel_vars *vars,
358 + struct Qdisc *sch);
359 +
360 +static struct sk_buff *codel_dequeue(struct Qdisc *sch,
361 + struct codel_params *params,
362 + struct codel_vars *vars,
363 + struct codel_stats *stats,
364 + codel_skb_dequeue_t dequeue_func,
365 + u32 *backlog)
366 +{
367 + struct sk_buff *skb = dequeue_func(vars, sch);
368 + codel_time_t now;
369 + bool drop;
370 +
371 + if (!skb) {
372 + vars->dropping = false;
373 + return skb;
374 + }
375 + now = codel_get_time();
376 + drop = codel_should_drop(skb, backlog, vars, params, stats, now);
377 + if (vars->dropping) {
378 + if (!drop) {
379 + /* sojourn time below target - leave dropping state */
380 + vars->dropping = false;
381 + } else if (codel_time_after_eq(now, vars->drop_next)) {
382 + /* It's time for the next drop. Drop the current
383 + * packet and dequeue the next. The dequeue might
384 + * take us out of dropping state.
385 + * If not, schedule the next drop.
386 + * A large backlog might result in drop rates so high
387 + * that the next drop should happen now,
388 + * hence the while loop.
389 + */
390 + while (vars->dropping &&
391 + codel_time_after_eq(now, vars->drop_next)) {
392 + if (++vars->count == 0) /* avoid zero divides */
393 + vars->count = ~0U;
394 + if (params->ecn && INET_ECN_set_ce(skb)) {
395 + stats->ecn_mark++;
396 + vars->drop_next =
397 + codel_control_law(vars->drop_next,
398 + params->interval,
399 + vars->count);
400 + goto end;
401 + }
402 + qdisc_drop(skb, sch);
403 + stats->drop_count++;
404 + skb = dequeue_func(vars, sch);
405 + if (!codel_should_drop(skb, backlog,
406 + vars, params, stats, now)) {
407 + /* leave dropping state */
408 + vars->dropping = false;
409 + } else {
410 + /* and schedule the next drop */
411 + vars->drop_next =
412 + codel_control_law(vars->drop_next,
413 + params->interval,
414 + vars->count);
415 + }
416 + }
417 + }
418 + } else if (drop) {
419 + if (params->ecn && INET_ECN_set_ce(skb)) {
420 + stats->ecn_mark++;
421 + } else {
422 + qdisc_drop(skb, sch);
423 + stats->drop_count++;
424 +
425 + skb = dequeue_func(vars, sch);
426 + drop = codel_should_drop(skb, backlog, vars, params,
427 + stats, now);
428 + }
429 + vars->dropping = true;
430 + /* if min went above target close to when we last went below it
431 + * assume that the drop rate that controlled the queue on the
432 + * last cycle is a good starting point to control it now.
433 + */
434 + if (codel_time_before(now - vars->drop_next,
435 + 16 * params->interval)) {
436 + vars->count = (vars->count - vars->lastcount) | 1;
437 + } else {
438 + vars->count = 1;
439 + }
440 + vars->lastcount = vars->count;
441 + vars->drop_next = codel_control_law(now, params->interval,
442 + vars->count);
443 + }
444 +end:
445 + return skb;
446 +}
447 +#endif
448 --- a/net/sched/Kconfig
449 +++ b/net/sched/Kconfig
450 @@ -250,6 +250,17 @@ config NET_SCH_QFQ
451
452 If unsure, say N.
453
454 +config NET_SCH_CODEL
455 + tristate "Controlled Delay AQM (CODEL)"
456 + help
457 + Say Y here if you want to use the Controlled Delay (CODEL)
458 + packet scheduling algorithm.
459 +
460 + To compile this driver as a module, choose M here: the module
461 + will be called sch_codel.
462 +
463 + If unsure, say N.
464 +
465 config NET_SCH_INGRESS
466 tristate "Ingress Qdisc"
467 depends on NET_CLS_ACT
468 --- a/net/sched/Makefile
469 +++ b/net/sched/Makefile
470 @@ -36,6 +36,7 @@ obj-$(CONFIG_NET_SCH_DRR) += sch_drr.o
471 obj-$(CONFIG_NET_SCH_MQPRIO) += sch_mqprio.o
472 obj-$(CONFIG_NET_SCH_CHOKE) += sch_choke.o
473 obj-$(CONFIG_NET_SCH_QFQ) += sch_qfq.o
474 +obj-$(CONFIG_NET_SCH_CODEL) += sch_codel.o
475
476 obj-$(CONFIG_NET_CLS_U32) += cls_u32.o
477 obj-$(CONFIG_NET_CLS_ROUTE4) += cls_route.o
478 --- /dev/null
479 +++ b/net/sched/sch_codel.c
480 @@ -0,0 +1,275 @@
481 +/*
482 + * Codel - The Controlled-Delay Active Queue Management algorithm
483 + *
484 + * Copyright (C) 2011-2012 Kathleen Nichols <nichols@pollere.com>
485 + * Copyright (C) 2011-2012 Van Jacobson <van@pollere.net>
486 + *
487 + * Implemented on linux by :
488 + * Copyright (C) 2012 Michael D. Taht <dave.taht@bufferbloat.net>
489 + * Copyright (C) 2012 Eric Dumazet <edumazet@google.com>
490 + *
491 + * Redistribution and use in source and binary forms, with or without
492 + * modification, are permitted provided that the following conditions
493 + * are met:
494 + * 1. Redistributions of source code must retain the above copyright
495 + * notice, this list of conditions, and the following disclaimer,
496 + * without modification.
497 + * 2. Redistributions in binary form must reproduce the above copyright
498 + * notice, this list of conditions and the following disclaimer in the
499 + * documentation and/or other materials provided with the distribution.
500 + * 3. The names of the authors may not be used to endorse or promote products
501 + * derived from this software without specific prior written permission.
502 + *
503 + * Alternatively, provided that this notice is retained in full, this
504 + * software may be distributed under the terms of the GNU General
505 + * Public License ("GPL") version 2, in which case the provisions of the
506 + * GPL apply INSTEAD OF those given above.
507 + *
508 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
509 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
510 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
511 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
512 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
513 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
514 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
515 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
516 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
517 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
518 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
519 + * DAMAGE.
520 + *
521 + */
522 +
523 +#include <linux/module.h>
524 +#include <linux/slab.h>
525 +#include <linux/types.h>
526 +#include <linux/kernel.h>
527 +#include <linux/errno.h>
528 +#include <linux/skbuff.h>
529 +#include <net/pkt_sched.h>
530 +#include <net/codel.h>
531 +
532 +
533 +#define DEFAULT_CODEL_LIMIT 1000
534 +
535 +struct codel_sched_data {
536 + struct codel_params params;
537 + struct codel_vars vars;
538 + struct codel_stats stats;
539 + u32 drop_overlimit;
540 +};
541 +
542 +/* This is the specific function called from codel_dequeue()
543 + * to dequeue a packet from queue. Note: backlog is handled in
544 + * codel, we dont need to reduce it here.
545 + */
546 +static struct sk_buff *dequeue(struct codel_vars *vars, struct Qdisc *sch)
547 +{
548 + struct sk_buff *skb = __skb_dequeue(&sch->q);
549 +
550 + prefetch(&skb->end); /* we'll need skb_shinfo() */
551 + return skb;
552 +}
553 +
554 +static struct sk_buff *codel_qdisc_dequeue(struct Qdisc *sch)
555 +{
556 + struct codel_sched_data *q = qdisc_priv(sch);
557 + struct sk_buff *skb;
558 +
559 + skb = codel_dequeue(sch, &q->params, &q->vars, &q->stats,
560 + dequeue, &sch->qstats.backlog);
561 + /* We cant call qdisc_tree_decrease_qlen() if our qlen is 0,
562 + * or HTB crashes. Defer it for next round.
563 + */
564 + if (q->stats.drop_count && sch->q.qlen) {
565 + qdisc_tree_decrease_qlen(sch, q->stats.drop_count);
566 + q->stats.drop_count = 0;
567 + }
568 + if (skb)
569 + qdisc_bstats_update(sch, skb);
570 + return skb;
571 +}
572 +
573 +static int codel_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
574 +{
575 + struct codel_sched_data *q;
576 +
577 + if (likely(qdisc_qlen(sch) < sch->limit)) {
578 + codel_set_enqueue_time(skb);
579 + return qdisc_enqueue_tail(skb, sch);
580 + }
581 + q = qdisc_priv(sch);
582 + q->drop_overlimit++;
583 + return qdisc_drop(skb, sch);
584 +}
585 +
586 +static const struct nla_policy codel_policy[TCA_CODEL_MAX + 1] = {
587 + [TCA_CODEL_TARGET] = { .type = NLA_U32 },
588 + [TCA_CODEL_LIMIT] = { .type = NLA_U32 },
589 + [TCA_CODEL_INTERVAL] = { .type = NLA_U32 },
590 + [TCA_CODEL_ECN] = { .type = NLA_U32 },
591 +};
592 +
593 +static int codel_change(struct Qdisc *sch, struct nlattr *opt)
594 +{
595 + struct codel_sched_data *q = qdisc_priv(sch);
596 + struct nlattr *tb[TCA_CODEL_MAX + 1];
597 + unsigned int qlen;
598 + int err;
599 +
600 + if (!opt)
601 + return -EINVAL;
602 +
603 + err = nla_parse_nested(tb, TCA_CODEL_MAX, opt, codel_policy);
604 + if (err < 0)
605 + return err;
606 +
607 + sch_tree_lock(sch);
608 +
609 + if (tb[TCA_CODEL_TARGET]) {
610 + u32 target = nla_get_u32(tb[TCA_CODEL_TARGET]);
611 +
612 + q->params.target = ((u64)target * NSEC_PER_USEC) >> CODEL_SHIFT;
613 + }
614 +
615 + if (tb[TCA_CODEL_INTERVAL]) {
616 + u32 interval = nla_get_u32(tb[TCA_CODEL_INTERVAL]);
617 +
618 + q->params.interval = ((u64)interval * NSEC_PER_USEC) >> CODEL_SHIFT;
619 + }
620 +
621 + if (tb[TCA_CODEL_LIMIT])
622 + sch->limit = nla_get_u32(tb[TCA_CODEL_LIMIT]);
623 +
624 + if (tb[TCA_CODEL_ECN])
625 + q->params.ecn = !!nla_get_u32(tb[TCA_CODEL_ECN]);
626 +
627 + qlen = sch->q.qlen;
628 + while (sch->q.qlen > sch->limit) {
629 + struct sk_buff *skb = __skb_dequeue(&sch->q);
630 +
631 + sch->qstats.backlog -= qdisc_pkt_len(skb);
632 + qdisc_drop(skb, sch);
633 + }
634 + qdisc_tree_decrease_qlen(sch, qlen - sch->q.qlen);
635 +
636 + sch_tree_unlock(sch);
637 + return 0;
638 +}
639 +
640 +static int codel_init(struct Qdisc *sch, struct nlattr *opt)
641 +{
642 + struct codel_sched_data *q = qdisc_priv(sch);
643 +
644 + sch->limit = DEFAULT_CODEL_LIMIT;
645 +
646 + codel_params_init(&q->params);
647 + codel_vars_init(&q->vars);
648 + codel_stats_init(&q->stats);
649 +
650 + if (opt) {
651 + int err = codel_change(sch, opt);
652 +
653 + if (err)
654 + return err;
655 + }
656 +
657 + if (sch->limit >= 1)
658 + sch->flags |= TCQ_F_CAN_BYPASS;
659 + else
660 + sch->flags &= ~TCQ_F_CAN_BYPASS;
661 +
662 + return 0;
663 +}
664 +
665 +static int codel_dump(struct Qdisc *sch, struct sk_buff *skb)
666 +{
667 + struct codel_sched_data *q = qdisc_priv(sch);
668 + struct nlattr *opts;
669 +
670 + opts = nla_nest_start(skb, TCA_OPTIONS);
671 + if (opts == NULL)
672 + goto nla_put_failure;
673 +
674 + if (nla_put_u32(skb, TCA_CODEL_TARGET,
675 + codel_time_to_us(q->params.target)) ||
676 + nla_put_u32(skb, TCA_CODEL_LIMIT,
677 + sch->limit) ||
678 + nla_put_u32(skb, TCA_CODEL_INTERVAL,
679 + codel_time_to_us(q->params.interval)) ||
680 + nla_put_u32(skb, TCA_CODEL_ECN,
681 + q->params.ecn))
682 + goto nla_put_failure;
683 +
684 + return nla_nest_end(skb, opts);
685 +
686 +nla_put_failure:
687 + nla_nest_cancel(skb, opts);
688 + return -1;
689 +}
690 +
691 +static int codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
692 +{
693 + const struct codel_sched_data *q = qdisc_priv(sch);
694 + struct tc_codel_xstats st = {
695 + .maxpacket = q->stats.maxpacket,
696 + .count = q->vars.count,
697 + .lastcount = q->vars.lastcount,
698 + .drop_overlimit = q->drop_overlimit,
699 + .ldelay = codel_time_to_us(q->vars.ldelay),
700 + .dropping = q->vars.dropping,
701 + .ecn_mark = q->stats.ecn_mark,
702 + };
703 +
704 + if (q->vars.dropping) {
705 + codel_tdiff_t delta = q->vars.drop_next - codel_get_time();
706 +
707 + if (delta >= 0)
708 + st.drop_next = codel_time_to_us(delta);
709 + else
710 + st.drop_next = -codel_time_to_us(-delta);
711 + }
712 +
713 + return gnet_stats_copy_app(d, &st, sizeof(st));
714 +}
715 +
716 +static void codel_reset(struct Qdisc *sch)
717 +{
718 + struct codel_sched_data *q = qdisc_priv(sch);
719 +
720 + qdisc_reset_queue(sch);
721 + codel_vars_init(&q->vars);
722 +}
723 +
724 +static struct Qdisc_ops codel_qdisc_ops __read_mostly = {
725 + .id = "codel",
726 + .priv_size = sizeof(struct codel_sched_data),
727 +
728 + .enqueue = codel_qdisc_enqueue,
729 + .dequeue = codel_qdisc_dequeue,
730 + .peek = qdisc_peek_dequeued,
731 + .init = codel_init,
732 + .reset = codel_reset,
733 + .change = codel_change,
734 + .dump = codel_dump,
735 + .dump_stats = codel_dump_stats,
736 + .owner = THIS_MODULE,
737 +};
738 +
739 +static int __init codel_module_init(void)
740 +{
741 + return register_qdisc(&codel_qdisc_ops);
742 +}
743 +
744 +static void __exit codel_module_exit(void)
745 +{
746 + unregister_qdisc(&codel_qdisc_ops);
747 +}
748 +
749 +module_init(codel_module_init)
750 +module_exit(codel_module_exit)
751 +
752 +MODULE_DESCRIPTION("Controlled Delay queue discipline");
753 +MODULE_AUTHOR("Dave Taht");
754 +MODULE_AUTHOR("Eric Dumazet");
755 +MODULE_LICENSE("Dual BSD/GPL");