18c2ecf20Sopenharmony_ci#ifndef __NET_SCHED_CODEL_IMPL_H
28c2ecf20Sopenharmony_ci#define __NET_SCHED_CODEL_IMPL_H
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci/*
58c2ecf20Sopenharmony_ci * Codel - The Controlled-Delay Active Queue Management algorithm
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Copyright (C) 2011-2012 Kathleen Nichols <nichols@pollere.com>
88c2ecf20Sopenharmony_ci *  Copyright (C) 2011-2012 Van Jacobson <van@pollere.net>
98c2ecf20Sopenharmony_ci *  Copyright (C) 2012 Michael D. Taht <dave.taht@bufferbloat.net>
108c2ecf20Sopenharmony_ci *  Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
138c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
148c2ecf20Sopenharmony_ci * are met:
158c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
168c2ecf20Sopenharmony_ci *    notice, this list of conditions, and the following disclaimer,
178c2ecf20Sopenharmony_ci *    without modification.
188c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
198c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
208c2ecf20Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
218c2ecf20Sopenharmony_ci * 3. The names of the authors may not be used to endorse or promote products
228c2ecf20Sopenharmony_ci *    derived from this software without specific prior written permission.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * Alternatively, provided that this notice is retained in full, this
258c2ecf20Sopenharmony_ci * software may be distributed under the terms of the GNU General
268c2ecf20Sopenharmony_ci * Public License ("GPL") version 2, in which case the provisions of the
278c2ecf20Sopenharmony_ci * GPL apply INSTEAD OF those given above.
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
308c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
318c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
328c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
338c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
348c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
358c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
368c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
378c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
388c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
398c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
408c2ecf20Sopenharmony_ci * DAMAGE.
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* Controlling Queue Delay (CoDel) algorithm
458c2ecf20Sopenharmony_ci * =========================================
468c2ecf20Sopenharmony_ci * Source : Kathleen Nichols and Van Jacobson
478c2ecf20Sopenharmony_ci * http://queue.acm.org/detail.cfm?id=2209336
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * Implemented on linux by Dave Taht and Eric Dumazet
508c2ecf20Sopenharmony_ci */
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic void codel_params_init(struct codel_params *params)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	params->interval = MS2TIME(100);
558c2ecf20Sopenharmony_ci	params->target = MS2TIME(5);
568c2ecf20Sopenharmony_ci	params->ce_threshold = CODEL_DISABLED_THRESHOLD;
578c2ecf20Sopenharmony_ci	params->ecn = false;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic void codel_vars_init(struct codel_vars *vars)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	memset(vars, 0, sizeof(*vars));
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic void codel_stats_init(struct codel_stats *stats)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	stats->maxpacket = 0;
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/*
718c2ecf20Sopenharmony_ci * http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots
728c2ecf20Sopenharmony_ci * new_invsqrt = (invsqrt / 2) * (3 - count * invsqrt^2)
738c2ecf20Sopenharmony_ci *
748c2ecf20Sopenharmony_ci * Here, invsqrt is a fixed point number (< 1.0), 32bit mantissa, aka Q0.32
758c2ecf20Sopenharmony_ci */
768c2ecf20Sopenharmony_cistatic void codel_Newton_step(struct codel_vars *vars)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	u32 invsqrt = ((u32)vars->rec_inv_sqrt) << REC_INV_SQRT_SHIFT;
798c2ecf20Sopenharmony_ci	u32 invsqrt2 = ((u64)invsqrt * invsqrt) >> 32;
808c2ecf20Sopenharmony_ci	u64 val = (3LL << 32) - ((u64)vars->count * invsqrt2);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	val >>= 2; /* avoid overflow in following multiply */
838c2ecf20Sopenharmony_ci	val = (val * invsqrt) >> (32 - 2 + 1);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	vars->rec_inv_sqrt = val >> REC_INV_SQRT_SHIFT;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/*
898c2ecf20Sopenharmony_ci * CoDel control_law is t + interval/sqrt(count)
908c2ecf20Sopenharmony_ci * We maintain in rec_inv_sqrt the reciprocal value of sqrt(count) to avoid
918c2ecf20Sopenharmony_ci * both sqrt() and divide operation.
928c2ecf20Sopenharmony_ci */
938c2ecf20Sopenharmony_cistatic codel_time_t codel_control_law(codel_time_t t,
948c2ecf20Sopenharmony_ci				      codel_time_t interval,
958c2ecf20Sopenharmony_ci				      u32 rec_inv_sqrt)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	return t + reciprocal_scale(interval, rec_inv_sqrt << REC_INV_SQRT_SHIFT);
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic bool codel_should_drop(const struct sk_buff *skb,
1018c2ecf20Sopenharmony_ci			      void *ctx,
1028c2ecf20Sopenharmony_ci			      struct codel_vars *vars,
1038c2ecf20Sopenharmony_ci			      struct codel_params *params,
1048c2ecf20Sopenharmony_ci			      struct codel_stats *stats,
1058c2ecf20Sopenharmony_ci			      codel_skb_len_t skb_len_func,
1068c2ecf20Sopenharmony_ci			      codel_skb_time_t skb_time_func,
1078c2ecf20Sopenharmony_ci			      u32 *backlog,
1088c2ecf20Sopenharmony_ci			      codel_time_t now)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	bool ok_to_drop;
1118c2ecf20Sopenharmony_ci	u32 skb_len;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	if (!skb) {
1148c2ecf20Sopenharmony_ci		vars->first_above_time = 0;
1158c2ecf20Sopenharmony_ci		return false;
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	skb_len = skb_len_func(skb);
1198c2ecf20Sopenharmony_ci	vars->ldelay = now - skb_time_func(skb);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	if (unlikely(skb_len > stats->maxpacket))
1228c2ecf20Sopenharmony_ci		stats->maxpacket = skb_len;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	if (codel_time_before(vars->ldelay, params->target) ||
1258c2ecf20Sopenharmony_ci	    *backlog <= params->mtu) {
1268c2ecf20Sopenharmony_ci		/* went below - stay below for at least interval */
1278c2ecf20Sopenharmony_ci		vars->first_above_time = 0;
1288c2ecf20Sopenharmony_ci		return false;
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci	ok_to_drop = false;
1318c2ecf20Sopenharmony_ci	if (vars->first_above_time == 0) {
1328c2ecf20Sopenharmony_ci		/* just went above from below. If we stay above
1338c2ecf20Sopenharmony_ci		 * for at least interval we'll say it's ok to drop
1348c2ecf20Sopenharmony_ci		 */
1358c2ecf20Sopenharmony_ci		vars->first_above_time = now + params->interval;
1368c2ecf20Sopenharmony_ci	} else if (codel_time_after(now, vars->first_above_time)) {
1378c2ecf20Sopenharmony_ci		ok_to_drop = true;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci	return ok_to_drop;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic struct sk_buff *codel_dequeue(void *ctx,
1438c2ecf20Sopenharmony_ci				     u32 *backlog,
1448c2ecf20Sopenharmony_ci				     struct codel_params *params,
1458c2ecf20Sopenharmony_ci				     struct codel_vars *vars,
1468c2ecf20Sopenharmony_ci				     struct codel_stats *stats,
1478c2ecf20Sopenharmony_ci				     codel_skb_len_t skb_len_func,
1488c2ecf20Sopenharmony_ci				     codel_skb_time_t skb_time_func,
1498c2ecf20Sopenharmony_ci				     codel_skb_drop_t drop_func,
1508c2ecf20Sopenharmony_ci				     codel_skb_dequeue_t dequeue_func)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	struct sk_buff *skb = dequeue_func(vars, ctx);
1538c2ecf20Sopenharmony_ci	codel_time_t now;
1548c2ecf20Sopenharmony_ci	bool drop;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	if (!skb) {
1578c2ecf20Sopenharmony_ci		vars->dropping = false;
1588c2ecf20Sopenharmony_ci		return skb;
1598c2ecf20Sopenharmony_ci	}
1608c2ecf20Sopenharmony_ci	now = codel_get_time();
1618c2ecf20Sopenharmony_ci	drop = codel_should_drop(skb, ctx, vars, params, stats,
1628c2ecf20Sopenharmony_ci				 skb_len_func, skb_time_func, backlog, now);
1638c2ecf20Sopenharmony_ci	if (vars->dropping) {
1648c2ecf20Sopenharmony_ci		if (!drop) {
1658c2ecf20Sopenharmony_ci			/* sojourn time below target - leave dropping state */
1668c2ecf20Sopenharmony_ci			vars->dropping = false;
1678c2ecf20Sopenharmony_ci		} else if (codel_time_after_eq(now, vars->drop_next)) {
1688c2ecf20Sopenharmony_ci			/* It's time for the next drop. Drop the current
1698c2ecf20Sopenharmony_ci			 * packet and dequeue the next. The dequeue might
1708c2ecf20Sopenharmony_ci			 * take us out of dropping state.
1718c2ecf20Sopenharmony_ci			 * If not, schedule the next drop.
1728c2ecf20Sopenharmony_ci			 * A large backlog might result in drop rates so high
1738c2ecf20Sopenharmony_ci			 * that the next drop should happen now,
1748c2ecf20Sopenharmony_ci			 * hence the while loop.
1758c2ecf20Sopenharmony_ci			 */
1768c2ecf20Sopenharmony_ci			while (vars->dropping &&
1778c2ecf20Sopenharmony_ci			       codel_time_after_eq(now, vars->drop_next)) {
1788c2ecf20Sopenharmony_ci				vars->count++; /* dont care of possible wrap
1798c2ecf20Sopenharmony_ci						* since there is no more divide
1808c2ecf20Sopenharmony_ci						*/
1818c2ecf20Sopenharmony_ci				codel_Newton_step(vars);
1828c2ecf20Sopenharmony_ci				if (params->ecn && INET_ECN_set_ce(skb)) {
1838c2ecf20Sopenharmony_ci					stats->ecn_mark++;
1848c2ecf20Sopenharmony_ci					vars->drop_next =
1858c2ecf20Sopenharmony_ci						codel_control_law(vars->drop_next,
1868c2ecf20Sopenharmony_ci								  params->interval,
1878c2ecf20Sopenharmony_ci								  vars->rec_inv_sqrt);
1888c2ecf20Sopenharmony_ci					goto end;
1898c2ecf20Sopenharmony_ci				}
1908c2ecf20Sopenharmony_ci				stats->drop_len += skb_len_func(skb);
1918c2ecf20Sopenharmony_ci				drop_func(skb, ctx);
1928c2ecf20Sopenharmony_ci				stats->drop_count++;
1938c2ecf20Sopenharmony_ci				skb = dequeue_func(vars, ctx);
1948c2ecf20Sopenharmony_ci				if (!codel_should_drop(skb, ctx,
1958c2ecf20Sopenharmony_ci						       vars, params, stats,
1968c2ecf20Sopenharmony_ci						       skb_len_func,
1978c2ecf20Sopenharmony_ci						       skb_time_func,
1988c2ecf20Sopenharmony_ci						       backlog, now)) {
1998c2ecf20Sopenharmony_ci					/* leave dropping state */
2008c2ecf20Sopenharmony_ci					vars->dropping = false;
2018c2ecf20Sopenharmony_ci				} else {
2028c2ecf20Sopenharmony_ci					/* and schedule the next drop */
2038c2ecf20Sopenharmony_ci					vars->drop_next =
2048c2ecf20Sopenharmony_ci						codel_control_law(vars->drop_next,
2058c2ecf20Sopenharmony_ci								  params->interval,
2068c2ecf20Sopenharmony_ci								  vars->rec_inv_sqrt);
2078c2ecf20Sopenharmony_ci				}
2088c2ecf20Sopenharmony_ci			}
2098c2ecf20Sopenharmony_ci		}
2108c2ecf20Sopenharmony_ci	} else if (drop) {
2118c2ecf20Sopenharmony_ci		u32 delta;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci		if (params->ecn && INET_ECN_set_ce(skb)) {
2148c2ecf20Sopenharmony_ci			stats->ecn_mark++;
2158c2ecf20Sopenharmony_ci		} else {
2168c2ecf20Sopenharmony_ci			stats->drop_len += skb_len_func(skb);
2178c2ecf20Sopenharmony_ci			drop_func(skb, ctx);
2188c2ecf20Sopenharmony_ci			stats->drop_count++;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci			skb = dequeue_func(vars, ctx);
2218c2ecf20Sopenharmony_ci			drop = codel_should_drop(skb, ctx, vars, params,
2228c2ecf20Sopenharmony_ci						 stats, skb_len_func,
2238c2ecf20Sopenharmony_ci						 skb_time_func, backlog, now);
2248c2ecf20Sopenharmony_ci		}
2258c2ecf20Sopenharmony_ci		vars->dropping = true;
2268c2ecf20Sopenharmony_ci		/* if min went above target close to when we last went below it
2278c2ecf20Sopenharmony_ci		 * assume that the drop rate that controlled the queue on the
2288c2ecf20Sopenharmony_ci		 * last cycle is a good starting point to control it now.
2298c2ecf20Sopenharmony_ci		 */
2308c2ecf20Sopenharmony_ci		delta = vars->count - vars->lastcount;
2318c2ecf20Sopenharmony_ci		if (delta > 1 &&
2328c2ecf20Sopenharmony_ci		    codel_time_before(now - vars->drop_next,
2338c2ecf20Sopenharmony_ci				      16 * params->interval)) {
2348c2ecf20Sopenharmony_ci			vars->count = delta;
2358c2ecf20Sopenharmony_ci			/* we dont care if rec_inv_sqrt approximation
2368c2ecf20Sopenharmony_ci			 * is not very precise :
2378c2ecf20Sopenharmony_ci			 * Next Newton steps will correct it quadratically.
2388c2ecf20Sopenharmony_ci			 */
2398c2ecf20Sopenharmony_ci			codel_Newton_step(vars);
2408c2ecf20Sopenharmony_ci		} else {
2418c2ecf20Sopenharmony_ci			vars->count = 1;
2428c2ecf20Sopenharmony_ci			vars->rec_inv_sqrt = ~0U >> REC_INV_SQRT_SHIFT;
2438c2ecf20Sopenharmony_ci		}
2448c2ecf20Sopenharmony_ci		vars->lastcount = vars->count;
2458c2ecf20Sopenharmony_ci		vars->drop_next = codel_control_law(now, params->interval,
2468c2ecf20Sopenharmony_ci						    vars->rec_inv_sqrt);
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ciend:
2498c2ecf20Sopenharmony_ci	if (skb && codel_time_after(vars->ldelay, params->ce_threshold) &&
2508c2ecf20Sopenharmony_ci	    INET_ECN_set_ce(skb))
2518c2ecf20Sopenharmony_ci		stats->ce_mark++;
2528c2ecf20Sopenharmony_ci	return skb;
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci#endif
256