1 #ifndef __NET_SCHED_CODEL_H 2 #define __NET_SCHED_CODEL_H 3 4 /* 5 * Codel - The Controlled-Delay Active Queue Management algorithm 6 * 7 * Copyright (C) 2011-2012 Kathleen Nichols <[email protected]> 8 * Copyright (C) 2011-2012 Van Jacobson <[email protected]> 9 * Copyright (C) 2012 Michael D. Taht <[email protected]> 10 * Copyright (C) 2012 Eric Dumazet <[email protected]> 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 #include <linux/types.h> 45 #include <linux/ktime.h> 46 #include <linux/skbuff.h> 47 #include <net/pkt_sched.h> 48 #include <net/inet_ecn.h> 49 #include <linux/reciprocal_div.h> 50 51 /* Controlling Queue Delay (CoDel) algorithm 52 * ========================================= 53 * Source : Kathleen Nichols and Van Jacobson 54 * http://queue.acm.org/detail.cfm?id=2209336 55 * 56 * Implemented on linux by Dave Taht and Eric Dumazet 57 */ 58 59 60 /* CoDel uses a 1024 nsec clock, encoded in u32 61 * This gives a range of 2199 seconds, because of signed compares 62 */ 63 typedef u32 codel_time_t; 64 typedef s32 codel_tdiff_t; 65 #define CODEL_SHIFT 10 66 #define MS2TIME(a) ((a * NSEC_PER_MSEC) >> CODEL_SHIFT) 67 68 static inline codel_time_t codel_get_time(void) 69 { 70 u64 ns = ktime_to_ns(ktime_get()); 71 72 return ns >> CODEL_SHIFT; 73 } 74 75 #define codel_time_after(a, b) ((s32)(a) - (s32)(b) > 0) 76 #define codel_time_after_eq(a, b) ((s32)(a) - (s32)(b) >= 0) 77 #define codel_time_before(a, b) ((s32)(a) - (s32)(b) < 0) 78 #define codel_time_before_eq(a, b) ((s32)(a) - (s32)(b) <= 0) 79 80 /* Qdiscs using codel plugin must use codel_skb_cb in their own cb[] */ 81 struct codel_skb_cb { 82 codel_time_t enqueue_time; 83 }; 84 85 static struct codel_skb_cb *get_codel_cb(const struct sk_buff *skb) 86 { 87 qdisc_cb_private_validate(skb, sizeof(struct codel_skb_cb)); 88 return (struct codel_skb_cb *)qdisc_skb_cb(skb)->data; 89 } 90 91 static codel_time_t codel_get_enqueue_time(const struct sk_buff *skb) 92 { 93 return get_codel_cb(skb)->enqueue_time; 94 } 95 96 static void codel_set_enqueue_time(struct sk_buff *skb) 97 { 98 get_codel_cb(skb)->enqueue_time = codel_get_time(); 99 } 100 101 static inline u32 codel_time_to_us(codel_time_t val) 102 { 103 u64 valns = ((u64)val << CODEL_SHIFT); 104 105 do_div(valns, NSEC_PER_USEC); 106 return (u32)valns; 107 } 108 109 /** 110 * struct codel_params - contains codel parameters 111 * @target: target queue size (in time units) 112 * @interval: width of moving time window 113 * @ecn: is Explicit Congestion Notification enabled 114 */ 115 struct codel_params { 116 codel_time_t target; 117 codel_time_t interval; 118 bool ecn; 119 }; 120 121 /** 122 * struct codel_vars - contains codel variables 123 * @count: how many drops we've done since the last time we 124 * entered dropping state 125 * @lastcount: count at entry to dropping state 126 * @dropping: set to true if in dropping state 127 * @rec_inv_sqrt: reciprocal value of sqrt(count) >> 1 128 * @first_above_time: when we went (or will go) continuously above target 129 * for interval 130 * @drop_next: time to drop next packet, or when we dropped last 131 * @ldelay: sojourn time of last dequeued packet 132 */ 133 struct codel_vars { 134 u32 count; 135 u32 lastcount; 136 bool dropping:1; 137 u32 rec_inv_sqrt:31; 138 codel_time_t first_above_time; 139 codel_time_t drop_next; 140 codel_time_t ldelay; 141 }; 142 143 /** 144 * struct codel_stats - contains codel shared variables and stats 145 * @maxpacket: largest packet we've seen so far 146 * @drop_count: temp count of dropped packets in dequeue() 147 * ecn_mark: number of packets we ECN marked instead of dropping 148 */ 149 struct codel_stats { 150 u32 maxpacket; 151 u32 drop_count; 152 u32 ecn_mark; 153 }; 154 155 static void codel_params_init(struct codel_params *params) 156 { 157 params->interval = MS2TIME(100); 158 params->target = MS2TIME(5); 159 params->ecn = false; 160 } 161 162 static void codel_vars_init(struct codel_vars *vars) 163 { 164 memset(vars, 0, sizeof(*vars)); 165 } 166 167 static void codel_stats_init(struct codel_stats *stats) 168 { 169 stats->maxpacket = 256; 170 } 171 172 /* 173 * http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots 174 * new_invsqrt = (invsqrt / 2) * (3 - count * invsqrt^2) 175 * 176 * Here, invsqrt is a fixed point number (< 1.0), 31bit mantissa) 177 */ 178 static void codel_Newton_step(struct codel_vars *vars) 179 { 180 u32 invsqrt = vars->rec_inv_sqrt; 181 u32 invsqrt2 = ((u64)invsqrt * invsqrt) >> 31; 182 u64 val = (3LL << 31) - ((u64)vars->count * invsqrt2); 183 184 val = (val * invsqrt) >> 32; 185 186 vars->rec_inv_sqrt = val; 187 } 188 189 /* 190 * CoDel control_law is t + interval/sqrt(count) 191 * We maintain in rec_inv_sqrt the reciprocal value of sqrt(count) to avoid 192 * both sqrt() and divide operation. 193 */ 194 static codel_time_t codel_control_law(codel_time_t t, 195 codel_time_t interval, 196 u32 rec_inv_sqrt) 197 { 198 return t + reciprocal_divide(interval, rec_inv_sqrt << 1); 199 } 200 201 202 static bool codel_should_drop(const struct sk_buff *skb, 203 unsigned int *backlog, 204 struct codel_vars *vars, 205 struct codel_params *params, 206 struct codel_stats *stats, 207 codel_time_t now) 208 { 209 bool ok_to_drop; 210 211 if (!skb) { 212 vars->first_above_time = 0; 213 return false; 214 } 215 216 vars->ldelay = now - codel_get_enqueue_time(skb); 217 *backlog -= qdisc_pkt_len(skb); 218 219 if (unlikely(qdisc_pkt_len(skb) > stats->maxpacket)) 220 stats->maxpacket = qdisc_pkt_len(skb); 221 222 if (codel_time_before(vars->ldelay, params->target) || 223 *backlog <= stats->maxpacket) { 224 /* went below - stay below for at least interval */ 225 vars->first_above_time = 0; 226 return false; 227 } 228 ok_to_drop = false; 229 if (vars->first_above_time == 0) { 230 /* just went above from below. If we stay above 231 * for at least interval we'll say it's ok to drop 232 */ 233 vars->first_above_time = now + params->interval; 234 } else if (codel_time_after(now, vars->first_above_time)) { 235 ok_to_drop = true; 236 } 237 return ok_to_drop; 238 } 239 240 typedef struct sk_buff * (*codel_skb_dequeue_t)(struct codel_vars *vars, 241 struct Qdisc *sch); 242 243 static struct sk_buff *codel_dequeue(struct Qdisc *sch, 244 struct codel_params *params, 245 struct codel_vars *vars, 246 struct codel_stats *stats, 247 codel_skb_dequeue_t dequeue_func, 248 u32 *backlog) 249 { 250 struct sk_buff *skb = dequeue_func(vars, sch); 251 codel_time_t now; 252 bool drop; 253 254 if (!skb) { 255 vars->dropping = false; 256 return skb; 257 } 258 now = codel_get_time(); 259 drop = codel_should_drop(skb, backlog, vars, params, stats, now); 260 if (vars->dropping) { 261 if (!drop) { 262 /* sojourn time below target - leave dropping state */ 263 vars->dropping = false; 264 } else if (codel_time_after_eq(now, vars->drop_next)) { 265 /* It's time for the next drop. Drop the current 266 * packet and dequeue the next. The dequeue might 267 * take us out of dropping state. 268 * If not, schedule the next drop. 269 * A large backlog might result in drop rates so high 270 * that the next drop should happen now, 271 * hence the while loop. 272 */ 273 while (vars->dropping && 274 codel_time_after_eq(now, vars->drop_next)) { 275 vars->count++; /* dont care of possible wrap 276 * since there is no more divide 277 */ 278 codel_Newton_step(vars); 279 if (params->ecn && INET_ECN_set_ce(skb)) { 280 stats->ecn_mark++; 281 vars->drop_next = 282 codel_control_law(vars->drop_next, 283 params->interval, 284 vars->rec_inv_sqrt); 285 goto end; 286 } 287 qdisc_drop(skb, sch); 288 stats->drop_count++; 289 skb = dequeue_func(vars, sch); 290 if (!codel_should_drop(skb, backlog, 291 vars, params, stats, now)) { 292 /* leave dropping state */ 293 vars->dropping = false; 294 } else { 295 /* and schedule the next drop */ 296 vars->drop_next = 297 codel_control_law(vars->drop_next, 298 params->interval, 299 vars->rec_inv_sqrt); 300 } 301 } 302 } 303 } else if (drop) { 304 if (params->ecn && INET_ECN_set_ce(skb)) { 305 stats->ecn_mark++; 306 } else { 307 qdisc_drop(skb, sch); 308 stats->drop_count++; 309 310 skb = dequeue_func(vars, sch); 311 drop = codel_should_drop(skb, backlog, vars, params, 312 stats, now); 313 } 314 vars->dropping = true; 315 /* if min went above target close to when we last went below it 316 * assume that the drop rate that controlled the queue on the 317 * last cycle is a good starting point to control it now. 318 */ 319 if (codel_time_before(now - vars->drop_next, 320 16 * params->interval)) { 321 vars->count = (vars->count - vars->lastcount) | 1; 322 /* we dont care if rec_inv_sqrt approximation 323 * is not very precise : 324 * Next Newton steps will correct it quadratically. 325 */ 326 codel_Newton_step(vars); 327 } else { 328 vars->count = 1; 329 vars->rec_inv_sqrt = 0x7fffffff; 330 } 331 vars->lastcount = vars->count; 332 vars->drop_next = codel_control_law(now, params->interval, 333 vars->rec_inv_sqrt); 334 } 335 end: 336 return skb; 337 } 338 #endif 339