#ifndef __NET_SCHED_CODEL_IMPL_H1#define __NET_SCHED_CODEL_IMPL_H23/*4* Codel - The Controlled-Delay Active Queue Management algorithm5*6* Copyright (C) 2011-2012 Kathleen Nichols <[email protected]>7* Copyright (C) 2011-2012 Van Jacobson <[email protected]>8* Copyright (C) 2012 Michael D. Taht <[email protected]>9* Copyright (C) 2012,2015 Eric Dumazet <[email protected]>10*11* Redistribution and use in source and binary forms, with or without12* modification, are permitted provided that the following conditions13* are met:14* 1. Redistributions of source code must retain the above copyright15* notice, this list of conditions, and the following disclaimer,16* without modification.17* 2. Redistributions in binary form must reproduce the above copyright18* notice, this list of conditions and the following disclaimer in the19* documentation and/or other materials provided with the distribution.20* 3. The names of the authors may not be used to endorse or promote products21* derived from this software without specific prior written permission.22*23* Alternatively, provided that this notice is retained in full, this24* software may be distributed under the terms of the GNU General25* Public License ("GPL") version 2, in which case the provisions of the26* GPL apply INSTEAD OF those given above.27*28* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS29* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT30* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR31* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT32* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,33* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT34* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,35* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY36* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT37* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE38* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH39* DAMAGE.40*41*/4243/* Controlling Queue Delay (CoDel) algorithm44* =========================================45* Source : Kathleen Nichols and Van Jacobson46* http://queue.acm.org/detail.cfm?id=220933647*48* Implemented on linux by Dave Taht and Eric Dumazet49*/5051#include <net/inet_ecn.h>5253static void codel_params_init(struct codel_params *params)54{55params->interval = MS2TIME(100);56params->target = MS2TIME(5);57params->ce_threshold = CODEL_DISABLED_THRESHOLD;58params->ce_threshold_mask = 0;59params->ce_threshold_selector = 0;60params->ecn = false;61}6263static void codel_vars_init(struct codel_vars *vars)64{65memset(vars, 0, sizeof(*vars));66}6768static void codel_stats_init(struct codel_stats *stats)69{70stats->maxpacket = 0;71}7273/*74* http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots75* new_invsqrt = (invsqrt / 2) * (3 - count * invsqrt^2)76*77* Here, invsqrt is a fixed point number (< 1.0), 32bit mantissa, aka Q0.3278*/79static void codel_Newton_step(struct codel_vars *vars)80{81u32 invsqrt = ((u32)vars->rec_inv_sqrt) << REC_INV_SQRT_SHIFT;82u32 invsqrt2 = ((u64)invsqrt * invsqrt) >> 32;83u64 val = (3LL << 32) - ((u64)vars->count * invsqrt2);8485val >>= 2; /* avoid overflow in following multiply */86val = (val * invsqrt) >> (32 - 2 + 1);8788vars->rec_inv_sqrt = val >> REC_INV_SQRT_SHIFT;89}9091/*92* CoDel control_law is t + interval/sqrt(count)93* We maintain in rec_inv_sqrt the reciprocal value of sqrt(count) to avoid94* both sqrt() and divide operation.95*/96static codel_time_t codel_control_law(codel_time_t t,97codel_time_t interval,98u32 rec_inv_sqrt)99{100return t + reciprocal_scale(interval, rec_inv_sqrt << REC_INV_SQRT_SHIFT);101}102103static bool codel_should_drop(const struct sk_buff *skb,104void *ctx,105struct codel_vars *vars,106struct codel_params *params,107struct codel_stats *stats,108codel_skb_len_t skb_len_func,109codel_skb_time_t skb_time_func,110u32 *backlog,111codel_time_t now)112{113bool ok_to_drop;114u32 skb_len;115116if (!skb) {117vars->first_above_time = 0;118return false;119}120121skb_len = skb_len_func(skb);122vars->ldelay = now - skb_time_func(skb);123124if (unlikely(skb_len > stats->maxpacket))125stats->maxpacket = skb_len;126127if (codel_time_before(vars->ldelay, params->target) ||128*backlog <= params->mtu) {129/* went below - stay below for at least interval */130vars->first_above_time = 0;131return false;132}133ok_to_drop = false;134if (vars->first_above_time == 0) {135/* just went above from below. If we stay above136* for at least interval we'll say it's ok to drop137*/138vars->first_above_time = now + params->interval;139} else if (codel_time_after(now, vars->first_above_time)) {140ok_to_drop = true;141}142return ok_to_drop;143}144145static struct sk_buff *codel_dequeue(void *ctx,146u32 *backlog,147struct codel_params *params,148struct codel_vars *vars,149struct codel_stats *stats,150codel_skb_len_t skb_len_func,151codel_skb_time_t skb_time_func,152codel_skb_drop_t drop_func,153codel_skb_dequeue_t dequeue_func)154{155struct sk_buff *skb = dequeue_func(vars, ctx);156codel_time_t now;157bool drop;158159if (!skb) {160vars->dropping = false;161return skb;162}163now = codel_get_time();164drop = codel_should_drop(skb, ctx, vars, params, stats,165skb_len_func, skb_time_func, backlog, now);166if (vars->dropping) {167if (!drop) {168/* sojourn time below target - leave dropping state */169vars->dropping = false;170} else if (codel_time_after_eq(now, vars->drop_next)) {171/* It's time for the next drop. Drop the current172* packet and dequeue the next. The dequeue might173* take us out of dropping state.174* If not, schedule the next drop.175* A large backlog might result in drop rates so high176* that the next drop should happen now,177* hence the while loop.178*/179while (vars->dropping &&180codel_time_after_eq(now, vars->drop_next)) {181vars->count++; /* dont care of possible wrap182* since there is no more divide183*/184codel_Newton_step(vars);185if (params->ecn && INET_ECN_set_ce(skb)) {186stats->ecn_mark++;187vars->drop_next =188codel_control_law(vars->drop_next,189params->interval,190vars->rec_inv_sqrt);191goto end;192}193stats->drop_len += skb_len_func(skb);194drop_func(skb, ctx);195stats->drop_count++;196skb = dequeue_func(vars, ctx);197if (!codel_should_drop(skb, ctx,198vars, params, stats,199skb_len_func,200skb_time_func,201backlog, now)) {202/* leave dropping state */203vars->dropping = false;204} else {205/* and schedule the next drop */206vars->drop_next =207codel_control_law(vars->drop_next,208params->interval,209vars->rec_inv_sqrt);210}211}212}213} else if (drop) {214u32 delta;215216if (params->ecn && INET_ECN_set_ce(skb)) {217stats->ecn_mark++;218} else {219stats->drop_len += skb_len_func(skb);220drop_func(skb, ctx);221stats->drop_count++;222223skb = dequeue_func(vars, ctx);224drop = codel_should_drop(skb, ctx, vars, params,225stats, skb_len_func,226skb_time_func, backlog, now);227}228vars->dropping = true;229/* if min went above target close to when we last went below it230* assume that the drop rate that controlled the queue on the231* last cycle is a good starting point to control it now.232*/233delta = vars->count - vars->lastcount;234if (delta > 1 &&235codel_time_before(now - vars->drop_next,23616 * params->interval)) {237vars->count = delta;238/* we dont care if rec_inv_sqrt approximation239* is not very precise :240* Next Newton steps will correct it quadratically.241*/242codel_Newton_step(vars);243} else {244vars->count = 1;245vars->rec_inv_sqrt = ~0U >> REC_INV_SQRT_SHIFT;246}247vars->lastcount = vars->count;248vars->drop_next = codel_control_law(now, params->interval,249vars->rec_inv_sqrt);250}251end:252if (skb && codel_time_after(vars->ldelay, params->ce_threshold)) {253bool set_ce = true;254255if (params->ce_threshold_mask) {256int dsfield = skb_get_dsfield(skb);257258set_ce = (dsfield >= 0 &&259(((u8)dsfield & params->ce_threshold_mask) ==260params->ce_threshold_selector));261}262if (set_ce && INET_ECN_set_ce(skb))263stats->ce_mark++;264}265return skb;266}267268#endif269270271