1 #ifndef __NET_SCHED_CODEL_IMPL_H
2 #define __NET_SCHED_CODEL_IMPL_H
5 * Codel - The Controlled-Delay Active Queue Management algorithm
7 * Copyright (C) 2011-2012 Kathleen Nichols <nichols@pollere.com>
8 * Copyright (C) 2011-2012 Van Jacobson <van@pollere.net>
9 * Copyright (C) 2012 Michael D. Taht <dave.taht@bufferbloat.net>
10 * Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The names of the authors may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * Alternatively, provided that this notice is retained in full, this
25 * software may be distributed under the terms of the GNU General
26 * Public License ("GPL") version 2, in which case the provisions of the
27 * GPL apply INSTEAD OF those given above.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
44 /* Controlling Queue Delay (CoDel) algorithm
45 * =========================================
46 * Source : Kathleen Nichols and Van Jacobson
47 * http://queue.acm.org/detail.cfm?id=2209336
49 * Implemented on linux by Dave Taht and Eric Dumazet
52 static void codel_params_init(struct codel_params *params)
54 params->interval = MS2TIME(100);
55 params->target = MS2TIME(5);
56 params->ce_threshold = CODEL_DISABLED_THRESHOLD;
57 params->ce_threshold_mask = 0;
58 params->ce_threshold_selector = 0;
62 static void codel_vars_init(struct codel_vars *vars)
64 memset(vars, 0, sizeof(*vars));
67 static void codel_stats_init(struct codel_stats *stats)
73 * http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots
74 * new_invsqrt = (invsqrt / 2) * (3 - count * invsqrt^2)
76 * Here, invsqrt is a fixed point number (< 1.0), 32bit mantissa, aka Q0.32
78 static void codel_Newton_step(struct codel_vars *vars)
80 u32 invsqrt = ((u32)vars->rec_inv_sqrt) << REC_INV_SQRT_SHIFT;
81 u32 invsqrt2 = ((u64)invsqrt * invsqrt) >> 32;
82 u64 val = (3LL << 32) - ((u64)vars->count * invsqrt2);
84 val >>= 2; /* avoid overflow in following multiply */
85 val = (val * invsqrt) >> (32 - 2 + 1);
87 vars->rec_inv_sqrt = val >> REC_INV_SQRT_SHIFT;
91 * CoDel control_law is t + interval/sqrt(count)
92 * We maintain in rec_inv_sqrt the reciprocal value of sqrt(count) to avoid
93 * both sqrt() and divide operation.
95 static codel_time_t codel_control_law(codel_time_t t,
96 codel_time_t interval,
99 return t + reciprocal_scale(interval, rec_inv_sqrt << REC_INV_SQRT_SHIFT);
102 static bool codel_should_drop(const struct sk_buff *skb,
104 struct codel_vars *vars,
105 struct codel_params *params,
106 struct codel_stats *stats,
107 codel_skb_len_t skb_len_func,
108 codel_skb_time_t skb_time_func,
116 vars->first_above_time = 0;
120 skb_len = skb_len_func(skb);
121 vars->ldelay = now - skb_time_func(skb);
123 if (unlikely(skb_len > stats->maxpacket))
124 stats->maxpacket = skb_len;
126 if (codel_time_before(vars->ldelay, params->target) ||
127 *backlog <= params->mtu) {
128 /* went below - stay below for at least interval */
129 vars->first_above_time = 0;
133 if (vars->first_above_time == 0) {
134 /* just went above from below. If we stay above
135 * for at least interval we'll say it's ok to drop
137 vars->first_above_time = now + params->interval;
138 } else if (codel_time_after(now, vars->first_above_time)) {
144 static struct sk_buff *codel_dequeue(void *ctx,
146 struct codel_params *params,
147 struct codel_vars *vars,
148 struct codel_stats *stats,
149 codel_skb_len_t skb_len_func,
150 codel_skb_time_t skb_time_func,
151 codel_skb_drop_t drop_func,
152 codel_skb_dequeue_t dequeue_func)
154 struct sk_buff *skb = dequeue_func(vars, ctx);
159 vars->dropping = false;
162 now = codel_get_time();
163 drop = codel_should_drop(skb, ctx, vars, params, stats,
164 skb_len_func, skb_time_func, backlog, now);
165 if (vars->dropping) {
167 /* sojourn time below target - leave dropping state */
168 vars->dropping = false;
169 } else if (codel_time_after_eq(now, vars->drop_next)) {
170 /* It's time for the next drop. Drop the current
171 * packet and dequeue the next. The dequeue might
172 * take us out of dropping state.
173 * If not, schedule the next drop.
174 * A large backlog might result in drop rates so high
175 * that the next drop should happen now,
176 * hence the while loop.
178 while (vars->dropping &&
179 codel_time_after_eq(now, vars->drop_next)) {
180 vars->count++; /* dont care of possible wrap
181 * since there is no more divide
183 codel_Newton_step(vars);
184 if (params->ecn && INET_ECN_set_ce(skb)) {
187 codel_control_law(vars->drop_next,
192 stats->drop_len += skb_len_func(skb);
195 skb = dequeue_func(vars, ctx);
196 if (!codel_should_drop(skb, ctx,
201 /* leave dropping state */
202 vars->dropping = false;
204 /* and schedule the next drop */
206 codel_control_law(vars->drop_next,
215 if (params->ecn && INET_ECN_set_ce(skb)) {
218 stats->drop_len += skb_len_func(skb);
222 skb = dequeue_func(vars, ctx);
223 drop = codel_should_drop(skb, ctx, vars, params,
225 skb_time_func, backlog, now);
227 vars->dropping = true;
228 /* if min went above target close to when we last went below it
229 * assume that the drop rate that controlled the queue on the
230 * last cycle is a good starting point to control it now.
232 delta = vars->count - vars->lastcount;
234 codel_time_before(now - vars->drop_next,
235 16 * params->interval)) {
237 /* we dont care if rec_inv_sqrt approximation
238 * is not very precise :
239 * Next Newton steps will correct it quadratically.
241 codel_Newton_step(vars);
244 vars->rec_inv_sqrt = ~0U >> REC_INV_SQRT_SHIFT;
246 vars->lastcount = vars->count;
247 vars->drop_next = codel_control_law(now, params->interval,
251 if (skb && codel_time_after(vars->ldelay, params->ce_threshold)) {
254 if (params->ce_threshold_mask) {
255 int dsfield = skb_get_dsfield(skb);
257 set_ce = (dsfield >= 0 &&
258 (((u8)dsfield & params->ce_threshold_mask) ==
259 params->ce_threshold_selector));
261 if (set_ce && INET_ECN_set_ce(skb))