1 #ifndef __NET_SCHED_CODEL_IMPL_H 2 #define __NET_SCHED_CODEL_IMPL_H 3 4 /* 5 * Codel - The Controlled-Delay Active Queue Management algorithm 6 * 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> 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 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. 23 * 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. 28 * 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 40 * DAMAGE. 41 * 42 */ 43 44 /* Controlling Queue Delay (CoDel) algorithm 45 * ========================================= 46 * Source : Kathleen Nichols and Van Jacobson 47 * http://queue.acm.org/detail.cfm?id=2209336 48 * 49 * Implemented on linux by Dave Taht and Eric Dumazet 50 */ 51 52 static void codel_params_init(struct codel_params *params) 53 { 54 params->interval = MS2TIME(100); 55 params->target = MS2TIME(5); 56 params->ce_threshold = CODEL_DISABLED_THRESHOLD; 57 params->ce_threshold_ect1 = false; 58 params->ecn = false; 59 } 60 61 static void codel_vars_init(struct codel_vars *vars) 62 { 63 memset(vars, 0, sizeof(*vars)); 64 } 65 66 static void codel_stats_init(struct codel_stats *stats) 67 { 68 stats->maxpacket = 0; 69 } 70 71 /* 72 * http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots 73 * new_invsqrt = (invsqrt / 2) * (3 - count * invsqrt^2) 74 * 75 * Here, invsqrt is a fixed point number (< 1.0), 32bit mantissa, aka Q0.32 76 */ 77 static void codel_Newton_step(struct codel_vars *vars) 78 { 79 u32 invsqrt = ((u32)vars->rec_inv_sqrt) << REC_INV_SQRT_SHIFT; 80 u32 invsqrt2 = ((u64)invsqrt * invsqrt) >> 32; 81 u64 val = (3LL << 32) - ((u64)vars->count * invsqrt2); 82 83 val >>= 2; /* avoid overflow in following multiply */ 84 val = (val * invsqrt) >> (32 - 2 + 1); 85 86 vars->rec_inv_sqrt = val >> REC_INV_SQRT_SHIFT; 87 } 88 89 /* 90 * CoDel control_law is t + interval/sqrt(count) 91 * We maintain in rec_inv_sqrt the reciprocal value of sqrt(count) to avoid 92 * both sqrt() and divide operation. 93 */ 94 static codel_time_t codel_control_law(codel_time_t t, 95 codel_time_t interval, 96 u32 rec_inv_sqrt) 97 { 98 return t + reciprocal_scale(interval, rec_inv_sqrt << REC_INV_SQRT_SHIFT); 99 } 100 101 static bool codel_should_drop(const struct sk_buff *skb, 102 void *ctx, 103 struct codel_vars *vars, 104 struct codel_params *params, 105 struct codel_stats *stats, 106 codel_skb_len_t skb_len_func, 107 codel_skb_time_t skb_time_func, 108 u32 *backlog, 109 codel_time_t now) 110 { 111 bool ok_to_drop; 112 u32 skb_len; 113 114 if (!skb) { 115 vars->first_above_time = 0; 116 return false; 117 } 118 119 skb_len = skb_len_func(skb); 120 vars->ldelay = now - skb_time_func(skb); 121 122 if (unlikely(skb_len > stats->maxpacket)) 123 stats->maxpacket = skb_len; 124 125 if (codel_time_before(vars->ldelay, params->target) || 126 *backlog <= params->mtu) { 127 /* went below - stay below for at least interval */ 128 vars->first_above_time = 0; 129 return false; 130 } 131 ok_to_drop = false; 132 if (vars->first_above_time == 0) { 133 /* just went above from below. If we stay above 134 * for at least interval we'll say it's ok to drop 135 */ 136 vars->first_above_time = now + params->interval; 137 } else if (codel_time_after(now, vars->first_above_time)) { 138 ok_to_drop = true; 139 } 140 return ok_to_drop; 141 } 142 143 static struct sk_buff *codel_dequeue(void *ctx, 144 u32 *backlog, 145 struct codel_params *params, 146 struct codel_vars *vars, 147 struct codel_stats *stats, 148 codel_skb_len_t skb_len_func, 149 codel_skb_time_t skb_time_func, 150 codel_skb_drop_t drop_func, 151 codel_skb_dequeue_t dequeue_func) 152 { 153 struct sk_buff *skb = dequeue_func(vars, ctx); 154 codel_time_t now; 155 bool drop; 156 157 if (!skb) { 158 vars->dropping = false; 159 return skb; 160 } 161 now = codel_get_time(); 162 drop = codel_should_drop(skb, ctx, vars, params, stats, 163 skb_len_func, skb_time_func, backlog, now); 164 if (vars->dropping) { 165 if (!drop) { 166 /* sojourn time below target - leave dropping state */ 167 vars->dropping = false; 168 } else if (codel_time_after_eq(now, vars->drop_next)) { 169 /* It's time for the next drop. Drop the current 170 * packet and dequeue the next. The dequeue might 171 * take us out of dropping state. 172 * If not, schedule the next drop. 173 * A large backlog might result in drop rates so high 174 * that the next drop should happen now, 175 * hence the while loop. 176 */ 177 while (vars->dropping && 178 codel_time_after_eq(now, vars->drop_next)) { 179 vars->count++; /* dont care of possible wrap 180 * since there is no more divide 181 */ 182 codel_Newton_step(vars); 183 if (params->ecn && INET_ECN_set_ce(skb)) { 184 stats->ecn_mark++; 185 vars->drop_next = 186 codel_control_law(vars->drop_next, 187 params->interval, 188 vars->rec_inv_sqrt); 189 goto end; 190 } 191 stats->drop_len += skb_len_func(skb); 192 drop_func(skb, ctx); 193 stats->drop_count++; 194 skb = dequeue_func(vars, ctx); 195 if (!codel_should_drop(skb, ctx, 196 vars, params, stats, 197 skb_len_func, 198 skb_time_func, 199 backlog, now)) { 200 /* leave dropping state */ 201 vars->dropping = false; 202 } else { 203 /* and schedule the next drop */ 204 vars->drop_next = 205 codel_control_law(vars->drop_next, 206 params->interval, 207 vars->rec_inv_sqrt); 208 } 209 } 210 } 211 } else if (drop) { 212 u32 delta; 213 214 if (params->ecn && INET_ECN_set_ce(skb)) { 215 stats->ecn_mark++; 216 } else { 217 stats->drop_len += skb_len_func(skb); 218 drop_func(skb, ctx); 219 stats->drop_count++; 220 221 skb = dequeue_func(vars, ctx); 222 drop = codel_should_drop(skb, ctx, vars, params, 223 stats, skb_len_func, 224 skb_time_func, backlog, now); 225 } 226 vars->dropping = true; 227 /* if min went above target close to when we last went below it 228 * assume that the drop rate that controlled the queue on the 229 * last cycle is a good starting point to control it now. 230 */ 231 delta = vars->count - vars->lastcount; 232 if (delta > 1 && 233 codel_time_before(now - vars->drop_next, 234 16 * params->interval)) { 235 vars->count = delta; 236 /* we dont care if rec_inv_sqrt approximation 237 * is not very precise : 238 * Next Newton steps will correct it quadratically. 239 */ 240 codel_Newton_step(vars); 241 } else { 242 vars->count = 1; 243 vars->rec_inv_sqrt = ~0U >> REC_INV_SQRT_SHIFT; 244 } 245 vars->lastcount = vars->count; 246 vars->drop_next = codel_control_law(now, params->interval, 247 vars->rec_inv_sqrt); 248 } 249 end: 250 if (skb && codel_time_after(vars->ldelay, params->ce_threshold)) { 251 bool set_ce = true; 252 253 if (params->ce_threshold_ect1) { 254 /* Note: if skb_get_dsfield() returns -1, following 255 * gives INET_ECN_MASK, which is != INET_ECN_ECT_1. 256 */ 257 u8 ecn = skb_get_dsfield(skb) & INET_ECN_MASK; 258 259 set_ce = (ecn == INET_ECN_ECT_1); 260 } 261 if (set_ce && INET_ECN_set_ce(skb)) 262 stats->ce_mark++; 263 } 264 return skb; 265 } 266 267 #endif 268