| 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 <[email protected]>
|
| 8 | * Copyright (C) 2011-2012 Van Jacobson <[email protected]>
|
| 9 | * Copyright (C) 2012 Michael D. Taht <[email protected]>
|
| 10 | * Copyright (C) 2012,2015 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 | |
| 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;
|
| 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 | * Clamp the increment to at least 1 tick: a very small interval (or a
|
| 98 | * large count) can truncate it to zero, stalling the dropping loop.
|
| 99 |
|
| 100 | static codel_time_t codel_control_law(codel_time_t t,
|
| 101 | codel_time_t interval,
|
| 102 | u32 rec_inv_sqrt)
|
| 103 | {
|
| 104 | return t + max_t(u32, 1,
|
| 105 | reciprocal_scale(interval,
|
| 106 | rec_inv_sqrt << REC_INV_SQRT_SHIFT));
|
| 107 | }
|
| 108 |
|
| 109 | static bool codel_should_drop(const struct sk_buff *skb,
|
| 110 | void *ctx,
|
| 111 | struct codel_vars *vars,
|
| 112 | struct codel_params *params,
|
| 113 | struct codel_stats *stats,
|
| 114 | codel_skb_len_t skb_len_func,
|
| 115 | codel_skb_time_t skb_time_func,
|
| 116 | u32 *backlog,
|
| 117 | codel_time_t now)
|
| 118 | {
|
| 119 | bool ok_to_drop;
|
| 120 | u32 skb_len;
|
| 121 |
|
| 122 | if (!skb) {
|
| 123 | vars->first_above_time = 0;
|
| 124 | return false;
|
| 125 | }
|
| 126 |
|
| 127 | skb_len = skb_len_func(skb);
|
| 128 | WRITE_ONCE(vars->ldelay, now - skb_time_func(skb));
|
| 129 |
|
| 130 | if (unlikely(skb_len > stats->maxpacket))
|
| 131 | WRITE_ONCE(stats->maxpacket, skb_len);
|
| 132 |
|
| 133 | if (codel_time_before(vars->ldelay, params->target) ||
|
| 134 | *backlog <= params->mtu) {
|
| 135 |
|
| 136 | vars->first_above_time = 0;
|
| 137 | return false;
|
| 138 | }
|
| 139 | ok_to_drop = false;
|
| 140 | if (vars->first_above_time == 0) {
|
| 141 | |
| 142 | * for at least interval we'll say it's ok to drop
|
| 143 |
|
| 144 | vars->first_above_time = now + params->interval;
|
| 145 | } else if (codel_time_after(now, vars->first_above_time)) {
|
| 146 | ok_to_drop = true;
|
| 147 | }
|
| 148 | return ok_to_drop;
|
| 149 | }
|
| 150 |
|
| 151 | static struct sk_buff *codel_dequeue(void *ctx,
|
| 152 | u32 *backlog,
|
| 153 | struct codel_params *params,
|
| 154 | struct codel_vars *vars,
|
| 155 | struct codel_stats *stats,
|
| 156 | codel_skb_len_t skb_len_func,
|
| 157 | codel_skb_time_t skb_time_func,
|
| 158 | codel_skb_drop_t drop_func,
|
| 159 | codel_skb_dequeue_t dequeue_func)
|
| 160 | {
|
| 161 | struct sk_buff *skb = dequeue_func(vars, ctx);
|
| 162 | unsigned int drops = 0;
|
| 163 | codel_time_t now;
|
| 164 | bool drop;
|
| 165 |
|
| 166 | if (!skb) {
|
| 167 | vars->first_above_time = 0;
|
| 168 | WRITE_ONCE(vars->dropping, false);
|
| 169 | return skb;
|
| 170 | }
|
| 171 | now = codel_get_time();
|
| 172 | drop = codel_should_drop(skb, ctx, vars, params, stats,
|
| 173 | skb_len_func, skb_time_func, backlog, now);
|
| 174 | if (vars->dropping) {
|
| 175 | if (!drop) {
|
| 176 |
|
| 177 | WRITE_ONCE(vars->dropping, false);
|
| 178 | } else if (codel_time_after_eq(now, vars->drop_next)) {
|
| 179 | |
| 180 | * packet and dequeue the next. The dequeue might
|
| 181 | * take us out of dropping state.
|
| 182 | * If not, schedule the next drop.
|
| 183 | * A large backlog might result in drop rates so high
|
| 184 | * that the next drop should happen now,
|
| 185 | * hence the while loop.
|
| 186 |
|
| 187 | while (vars->dropping &&
|
| 188 | codel_time_after_eq(now, vars->drop_next)) {
|
| 189 | if (++drops > CODEL_MAX_DROPS_PER_DEQUEUE) {
|
| 190 |
|
| 191 | WRITE_ONCE(vars->drop_next,
|
| 192 | codel_control_law(now,
|
| 193 | params->interval,
|
| 194 | vars->rec_inv_sqrt));
|
| 195 | break;
|
| 196 | }
|
| 197 | |
| 198 | * since there is no more divide.
|
| 199 |
|
| 200 | WRITE_ONCE(vars->count, vars->count + 1);
|
| 201 | codel_Newton_step(vars);
|
| 202 | if (params->ecn && INET_ECN_set_ce(skb)) {
|
| 203 | WRITE_ONCE(stats->ecn_mark,
|
| 204 | stats->ecn_mark + 1);
|
| 205 | WRITE_ONCE(vars->drop_next,
|
| 206 | codel_control_law(vars->drop_next,
|
| 207 | params->interval,
|
| 208 | vars->rec_inv_sqrt));
|
| 209 | goto end;
|
| 210 | }
|
| 211 | stats->drop_len += skb_len_func(skb);
|
| 212 | drop_func(skb, ctx);
|
| 213 | stats->drop_count++;
|
| 214 | skb = dequeue_func(vars, ctx);
|
| 215 | if (!codel_should_drop(skb, ctx,
|
| 216 | vars, params, stats,
|
| 217 | skb_len_func,
|
| 218 | skb_time_func,
|
| 219 | backlog, now)) {
|
| 220 |
|
| 221 | WRITE_ONCE(vars->dropping, false);
|
| 222 | } else {
|
| 223 |
|
| 224 | WRITE_ONCE(vars->drop_next,
|
| 225 | codel_control_law(vars->drop_next,
|
| 226 | params->interval,
|
| 227 | vars->rec_inv_sqrt));
|
| 228 | }
|
| 229 | }
|
| 230 | }
|
| 231 | } else if (drop) {
|
| 232 | u32 delta;
|
| 233 |
|
| 234 | if (params->ecn && INET_ECN_set_ce(skb)) {
|
| 235 | WRITE_ONCE(stats->ecn_mark, stats->ecn_mark + 1);
|
| 236 | } else {
|
| 237 | stats->drop_len += skb_len_func(skb);
|
| 238 | drop_func(skb, ctx);
|
| 239 | stats->drop_count++;
|
| 240 |
|
| 241 | skb = dequeue_func(vars, ctx);
|
| 242 | drop = codel_should_drop(skb, ctx, vars, params,
|
| 243 | stats, skb_len_func,
|
| 244 | skb_time_func, backlog, now);
|
| 245 | }
|
| 246 | WRITE_ONCE(vars->dropping, true);
|
| 247 | |
| 248 | * assume that the drop rate that controlled the queue on the
|
| 249 | * last cycle is a good starting point to control it now.
|
| 250 |
|
| 251 | delta = vars->count - vars->lastcount;
|
| 252 | if (delta > 1 &&
|
| 253 | codel_time_before(now - vars->drop_next,
|
| 254 | 16 * params->interval)) {
|
| 255 | WRITE_ONCE(vars->count, delta);
|
| 256 | |
| 257 | * is not very precise :
|
| 258 | * Next Newton steps will correct it quadratically.
|
| 259 |
|
| 260 | codel_Newton_step(vars);
|
| 261 | } else {
|
| 262 | WRITE_ONCE(vars->count, 1);
|
| 263 | vars->rec_inv_sqrt = ~0U >> REC_INV_SQRT_SHIFT;
|
| 264 | }
|
| 265 | WRITE_ONCE(vars->lastcount, vars->count);
|
| 266 | WRITE_ONCE(vars->drop_next,
|
| 267 | codel_control_law(now, params->interval,
|
| 268 | vars->rec_inv_sqrt));
|
| 269 | }
|
| 270 | end:
|
| 271 | if (skb && codel_time_after(vars->ldelay, params->ce_threshold)) {
|
| 272 | bool set_ce = true;
|
| 273 |
|
| 274 | if (params->ce_threshold_mask) {
|
| 275 | int dsfield = skb_get_dsfield(skb);
|
| 276 |
|
| 277 | set_ce = (dsfield >= 0 &&
|
| 278 | (((u8)dsfield & params->ce_threshold_mask) ==
|
| 279 | params->ce_threshold_selector));
|
| 280 | }
|
| 281 | if (set_ce && INET_ECN_set_ce(skb))
|
| 282 | WRITE_ONCE(stats->ce_mark, stats->ce_mark + 1);
|
| 283 | }
|
| 284 | return skb;
|
| 285 | }
|
| 286 |
|
| 287 | #endif
|