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