blob: 06ac687b490972cef1bdb8795289a67b6e87f23e [file] [log] [blame]
Eric Dumazet76e3cc12012-05-10 07:51:25 +00001#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 <nichols@pollere.com>
8 * Copyright (C) 2011-2012 Van Jacobson <van@pollere.net>
9 * Copyright (C) 2012 Michael D. Taht <dave.taht@bufferbloat.net>
Eric Dumazet80ba92f2015-05-08 15:05:12 -070010 * Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
Eric Dumazet76e3cc12012-05-10 07:51:25 +000011 *
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
50/* Controlling Queue Delay (CoDel) algorithm
51 * =========================================
52 * Source : Kathleen Nichols and Van Jacobson
53 * http://queue.acm.org/detail.cfm?id=2209336
54 *
55 * Implemented on linux by Dave Taht and Eric Dumazet
56 */
57
58
59/* CoDel uses a 1024 nsec clock, encoded in u32
60 * This gives a range of 2199 seconds, because of signed compares
61 */
62typedef u32 codel_time_t;
63typedef s32 codel_tdiff_t;
64#define CODEL_SHIFT 10
65#define MS2TIME(a) ((a * NSEC_PER_MSEC) >> CODEL_SHIFT)
66
67static inline codel_time_t codel_get_time(void)
68{
Eric Dumazetd2de8752014-08-22 18:32:09 -070069 u64 ns = ktime_get_ns();
Eric Dumazet76e3cc12012-05-10 07:51:25 +000070
71 return ns >> CODEL_SHIFT;
72}
73
Jesper Dangaard Brouer1ba3aab2013-10-31 22:10:55 +010074/* Dealing with timer wrapping, according to RFC 1982, as desc in wikipedia:
75 * https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
76 * codel_time_after(a,b) returns true if the time a is after time b.
77 */
78#define codel_time_after(a, b) \
79 (typecheck(codel_time_t, a) && \
80 typecheck(codel_time_t, b) && \
81 ((s32)((a) - (b)) > 0))
82#define codel_time_before(a, b) codel_time_after(b, a)
83
84#define codel_time_after_eq(a, b) \
85 (typecheck(codel_time_t, a) && \
86 typecheck(codel_time_t, b) && \
87 ((s32)((a) - (b)) >= 0))
88#define codel_time_before_eq(a, b) codel_time_after_eq(b, a)
Eric Dumazet76e3cc12012-05-10 07:51:25 +000089
90/* Qdiscs using codel plugin must use codel_skb_cb in their own cb[] */
91struct codel_skb_cb {
92 codel_time_t enqueue_time;
93};
94
95static struct codel_skb_cb *get_codel_cb(const struct sk_buff *skb)
96{
97 qdisc_cb_private_validate(skb, sizeof(struct codel_skb_cb));
98 return (struct codel_skb_cb *)qdisc_skb_cb(skb)->data;
99}
100
101static codel_time_t codel_get_enqueue_time(const struct sk_buff *skb)
102{
103 return get_codel_cb(skb)->enqueue_time;
104}
105
106static void codel_set_enqueue_time(struct sk_buff *skb)
107{
108 get_codel_cb(skb)->enqueue_time = codel_get_time();
109}
110
111static inline u32 codel_time_to_us(codel_time_t val)
112{
113 u64 valns = ((u64)val << CODEL_SHIFT);
114
115 do_div(valns, NSEC_PER_USEC);
116 return (u32)valns;
117}
118
119/**
120 * struct codel_params - contains codel parameters
121 * @target: target queue size (in time units)
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700122 * @ce_threshold: threshold for marking packets with ECN CE
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000123 * @interval: width of moving time window
Eric Dumazeta5d28092015-04-30 09:40:40 -0700124 * @mtu: device mtu, or minimal queue backlog in bytes.
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000125 * @ecn: is Explicit Congestion Notification enabled
126 */
127struct codel_params {
128 codel_time_t target;
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700129 codel_time_t ce_threshold;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000130 codel_time_t interval;
Eric Dumazeta5d28092015-04-30 09:40:40 -0700131 u32 mtu;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000132 bool ecn;
133};
134
135/**
136 * struct codel_vars - contains codel variables
137 * @count: how many drops we've done since the last time we
138 * entered dropping state
139 * @lastcount: count at entry to dropping state
140 * @dropping: set to true if in dropping state
Eric Dumazet536edd62012-05-12 03:32:13 +0000141 * @rec_inv_sqrt: reciprocal value of sqrt(count) >> 1
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000142 * @first_above_time: when we went (or will go) continuously above target
143 * for interval
144 * @drop_next: time to drop next packet, or when we dropped last
145 * @ldelay: sojourn time of last dequeued packet
146 */
147struct codel_vars {
148 u32 count;
149 u32 lastcount;
Eric Dumazet6ff272c2012-05-12 21:23:23 +0000150 bool dropping;
151 u16 rec_inv_sqrt;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000152 codel_time_t first_above_time;
153 codel_time_t drop_next;
154 codel_time_t ldelay;
155};
156
Eric Dumazet6ff272c2012-05-12 21:23:23 +0000157#define REC_INV_SQRT_BITS (8 * sizeof(u16)) /* or sizeof_in_bits(rec_inv_sqrt) */
158/* needed shift to get a Q0.32 number from rec_inv_sqrt */
159#define REC_INV_SQRT_SHIFT (32 - REC_INV_SQRT_BITS)
160
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000161/**
162 * struct codel_stats - contains codel shared variables and stats
163 * @maxpacket: largest packet we've seen so far
164 * @drop_count: temp count of dropped packets in dequeue()
WANG Cong2ccccf52016-02-25 14:55:01 -0800165 * @drop_len: bytes of dropped packets in dequeue()
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000166 * ecn_mark: number of packets we ECN marked instead of dropping
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700167 * ce_mark: number of packets CE marked because sojourn time was above ce_threshold
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000168 */
169struct codel_stats {
170 u32 maxpacket;
171 u32 drop_count;
WANG Cong2ccccf52016-02-25 14:55:01 -0800172 u32 drop_len;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000173 u32 ecn_mark;
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700174 u32 ce_mark;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000175};
176
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700177#define CODEL_DISABLED_THRESHOLD INT_MAX
178
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200179static void codel_params_init(struct codel_params *params)
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000180{
181 params->interval = MS2TIME(100);
182 params->target = MS2TIME(5);
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700183 params->ce_threshold = CODEL_DISABLED_THRESHOLD;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000184 params->ecn = false;
185}
186
187static void codel_vars_init(struct codel_vars *vars)
188{
Eric Dumazet536edd62012-05-12 03:32:13 +0000189 memset(vars, 0, sizeof(*vars));
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000190}
191
192static void codel_stats_init(struct codel_stats *stats)
193{
Eric Dumazeta5d28092015-04-30 09:40:40 -0700194 stats->maxpacket = 0;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000195}
196
Eric Dumazet536edd62012-05-12 03:32:13 +0000197/*
198 * http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots
199 * new_invsqrt = (invsqrt / 2) * (3 - count * invsqrt^2)
200 *
Eric Dumazet6ff272c2012-05-12 21:23:23 +0000201 * Here, invsqrt is a fixed point number (< 1.0), 32bit mantissa, aka Q0.32
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000202 */
Eric Dumazet536edd62012-05-12 03:32:13 +0000203static void codel_Newton_step(struct codel_vars *vars)
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000204{
Eric Dumazet6ff272c2012-05-12 21:23:23 +0000205 u32 invsqrt = ((u32)vars->rec_inv_sqrt) << REC_INV_SQRT_SHIFT;
206 u32 invsqrt2 = ((u64)invsqrt * invsqrt) >> 32;
207 u64 val = (3LL << 32) - ((u64)vars->count * invsqrt2);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000208
Eric Dumazet6ff272c2012-05-12 21:23:23 +0000209 val >>= 2; /* avoid overflow in following multiply */
210 val = (val * invsqrt) >> (32 - 2 + 1);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000211
Eric Dumazet6ff272c2012-05-12 21:23:23 +0000212 vars->rec_inv_sqrt = val >> REC_INV_SQRT_SHIFT;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000213}
214
Eric Dumazet536edd62012-05-12 03:32:13 +0000215/*
216 * CoDel control_law is t + interval/sqrt(count)
217 * We maintain in rec_inv_sqrt the reciprocal value of sqrt(count) to avoid
218 * both sqrt() and divide operation.
219 */
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000220static codel_time_t codel_control_law(codel_time_t t,
221 codel_time_t interval,
Eric Dumazet536edd62012-05-12 03:32:13 +0000222 u32 rec_inv_sqrt)
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000223{
Daniel Borkmann89770b02014-01-22 02:29:40 +0100224 return t + reciprocal_scale(interval, rec_inv_sqrt << REC_INV_SQRT_SHIFT);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000225}
226
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200227typedef u32 (*codel_skb_len_t)(const struct sk_buff *skb);
228typedef codel_time_t (*codel_skb_time_t)(const struct sk_buff *skb);
229typedef void (*codel_skb_drop_t)(struct sk_buff *skb, void *ctx);
230typedef struct sk_buff * (*codel_skb_dequeue_t)(struct codel_vars *vars,
231 void *ctx);
232
Eric Dumazet536edd62012-05-12 03:32:13 +0000233static bool codel_should_drop(const struct sk_buff *skb,
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200234 void *ctx,
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000235 struct codel_vars *vars,
236 struct codel_params *params,
237 struct codel_stats *stats,
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200238 codel_skb_len_t skb_len_func,
239 codel_skb_time_t skb_time_func,
240 u32 *backlog,
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000241 codel_time_t now)
242{
243 bool ok_to_drop;
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200244 u32 skb_len;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000245
246 if (!skb) {
247 vars->first_above_time = 0;
248 return false;
249 }
250
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200251 skb_len = skb_len_func(skb);
252 vars->ldelay = now - skb_time_func(skb);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000253
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200254 if (unlikely(skb_len > stats->maxpacket))
255 stats->maxpacket = skb_len;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000256
257 if (codel_time_before(vars->ldelay, params->target) ||
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200258 *backlog <= params->mtu) {
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000259 /* went below - stay below for at least interval */
260 vars->first_above_time = 0;
261 return false;
262 }
263 ok_to_drop = false;
264 if (vars->first_above_time == 0) {
265 /* just went above from below. If we stay above
266 * for at least interval we'll say it's ok to drop
267 */
268 vars->first_above_time = now + params->interval;
269 } else if (codel_time_after(now, vars->first_above_time)) {
270 ok_to_drop = true;
271 }
272 return ok_to_drop;
273}
274
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200275static struct sk_buff *codel_dequeue(void *ctx,
276 u32 *backlog,
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000277 struct codel_params *params,
278 struct codel_vars *vars,
279 struct codel_stats *stats,
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200280 codel_skb_len_t skb_len_func,
281 codel_skb_time_t skb_time_func,
282 codel_skb_drop_t drop_func,
Eric Dumazet865ec552012-05-16 04:39:09 +0000283 codel_skb_dequeue_t dequeue_func)
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000284{
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200285 struct sk_buff *skb = dequeue_func(vars, ctx);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000286 codel_time_t now;
287 bool drop;
288
289 if (!skb) {
290 vars->dropping = false;
291 return skb;
292 }
293 now = codel_get_time();
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200294 drop = codel_should_drop(skb, ctx, vars, params, stats,
295 skb_len_func, skb_time_func, backlog, now);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000296 if (vars->dropping) {
297 if (!drop) {
298 /* sojourn time below target - leave dropping state */
299 vars->dropping = false;
300 } else if (codel_time_after_eq(now, vars->drop_next)) {
301 /* It's time for the next drop. Drop the current
302 * packet and dequeue the next. The dequeue might
303 * take us out of dropping state.
304 * If not, schedule the next drop.
305 * A large backlog might result in drop rates so high
306 * that the next drop should happen now,
307 * hence the while loop.
308 */
309 while (vars->dropping &&
310 codel_time_after_eq(now, vars->drop_next)) {
Eric Dumazet536edd62012-05-12 03:32:13 +0000311 vars->count++; /* dont care of possible wrap
312 * since there is no more divide
313 */
314 codel_Newton_step(vars);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000315 if (params->ecn && INET_ECN_set_ce(skb)) {
316 stats->ecn_mark++;
317 vars->drop_next =
318 codel_control_law(vars->drop_next,
319 params->interval,
Eric Dumazet536edd62012-05-12 03:32:13 +0000320 vars->rec_inv_sqrt);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000321 goto end;
322 }
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200323 stats->drop_len += skb_len_func(skb);
324 drop_func(skb, ctx);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000325 stats->drop_count++;
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200326 skb = dequeue_func(vars, ctx);
327 if (!codel_should_drop(skb, ctx,
328 vars, params, stats,
329 skb_len_func,
330 skb_time_func,
331 backlog, now)) {
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000332 /* leave dropping state */
333 vars->dropping = false;
334 } else {
335 /* and schedule the next drop */
336 vars->drop_next =
337 codel_control_law(vars->drop_next,
338 params->interval,
Eric Dumazet536edd62012-05-12 03:32:13 +0000339 vars->rec_inv_sqrt);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000340 }
341 }
342 }
343 } else if (drop) {
Eric Dumazet2359a472012-07-29 20:52:21 +0000344 u32 delta;
345
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000346 if (params->ecn && INET_ECN_set_ce(skb)) {
347 stats->ecn_mark++;
348 } else {
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200349 stats->drop_len += skb_len_func(skb);
350 drop_func(skb, ctx);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000351 stats->drop_count++;
352
Michal Kazior79bdc4c2016-04-22 14:15:58 +0200353 skb = dequeue_func(vars, ctx);
354 drop = codel_should_drop(skb, ctx, vars, params,
355 stats, skb_len_func,
356 skb_time_func, backlog, now);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000357 }
358 vars->dropping = true;
359 /* if min went above target close to when we last went below it
360 * assume that the drop rate that controlled the queue on the
361 * last cycle is a good starting point to control it now.
362 */
Eric Dumazet2359a472012-07-29 20:52:21 +0000363 delta = vars->count - vars->lastcount;
364 if (delta > 1 &&
365 codel_time_before(now - vars->drop_next,
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000366 16 * params->interval)) {
Eric Dumazet2359a472012-07-29 20:52:21 +0000367 vars->count = delta;
Eric Dumazet536edd62012-05-12 03:32:13 +0000368 /* we dont care if rec_inv_sqrt approximation
369 * is not very precise :
370 * Next Newton steps will correct it quadratically.
371 */
372 codel_Newton_step(vars);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000373 } else {
374 vars->count = 1;
Eric Dumazet6ff272c2012-05-12 21:23:23 +0000375 vars->rec_inv_sqrt = ~0U >> REC_INV_SQRT_SHIFT;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000376 }
377 vars->lastcount = vars->count;
378 vars->drop_next = codel_control_law(now, params->interval,
Eric Dumazet536edd62012-05-12 03:32:13 +0000379 vars->rec_inv_sqrt);
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000380 }
381end:
Eric Dumazet80ba92f2015-05-08 15:05:12 -0700382 if (skb && codel_time_after(vars->ldelay, params->ce_threshold) &&
383 INET_ECN_set_ce(skb))
384 stats->ce_mark++;
Eric Dumazet76e3cc12012-05-10 07:51:25 +0000385 return skb;
386}
387#endif