/*1* Codel - The Controlled-Delay Active Queue Management algorithm.2*3* Copyright (C) 2016 Centre for Advanced Internet Architectures,4* Swinburne University of Technology, Melbourne, Australia.5* Portions of this code were made possible in part by a gift from6* The Comcast Innovation Fund.7* Implemented by Rasool Al-Saadi <[email protected]>8*9* Copyright (C) 2011-2014 Kathleen Nichols <[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*15* o Redistributions of source code must retain the above copyright16* notice, this list of conditions, and the following disclaimer,17* without modification.18*19* o Redistributions in binary form must reproduce the above copyright20* notice, this list of conditions and the following disclaimer in21* the documentation and/or other materials provided with the22* distribution.23*24* o The names of the authors may not be used to endorse or promote25* products derived from this software without specific prior written26* permission.27*28* Alternatively, provided that this notice is retained in full, this29* software may be distributed under the terms of the GNU General Public30* License ("GPL") version 2, in which case the provisions of the GPL31* apply INSTEAD OF those given above.3233* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS34* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT35* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR36* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT37* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,38* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT39* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,40* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY41* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT42* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE43* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.44*/4546#ifndef _IP_DN_AQM_CODEL_H47#define _IP_DN_AQM_CODEL_H4849// XXX How to choose MTAG?50#define FIX_POINT_BITS 165152enum {53CODEL_ECN_ENABLED = 154};5556/* Codel parameters */57struct dn_aqm_codel_parms {58aqm_time_t target;59aqm_time_t interval;60uint32_t flags;61};6263/* codel status variables */64struct codel_status {65uint32_t count; /* number of dropped pkts since entering drop state */66uint16_t dropping; /* dropping state */67aqm_time_t drop_next_time; /* time for next drop */68aqm_time_t first_above_time; /* time for first ts over target we observed */69uint16_t isqrt; /* last isqrt for control low */70uint16_t maxpkt_size; /* max packet size seen so far */71};7273struct mbuf *codel_extract_head(struct dn_queue *, aqm_time_t *);74aqm_time_t control_law(struct codel_status *,75struct dn_aqm_codel_parms *, aqm_time_t );7677__inline static struct mbuf *78codel_dodequeue(struct dn_queue *q, aqm_time_t now, uint16_t *ok_to_drop)79{80struct mbuf * m;81struct dn_aqm_codel_parms *cprms;82struct codel_status *cst;83aqm_time_t pkt_ts, sojourn_time;8485*ok_to_drop = 0;86m = codel_extract_head(q, &pkt_ts);8788cst = q->aqm_status;8990if (m == NULL) {91/* queue is empty - we can't be above target */92cst->first_above_time= 0;93return m;94}9596cprms = q->fs->aqmcfg;9798/* To span a large range of bandwidths, CoDel runs two99* different AQMs in parallel. One is sojourn-time-based100* and takes effect when the time to send an MTU-sized101* packet is less than target. The 1st term of the "if"102* below does this. The other is backlog-based and takes103* effect when the time to send an MTU-sized packet is >=104* target. The goal here is to keep the output link105* utilization high by never allowing the queue to get106* smaller than the amount that arrives in a typical107* interarrival time (MTU-sized packets arriving spaced108* by the amount of time it takes to send such a packet on109* the bottleneck). The 2nd term of the "if" does this.110*/111sojourn_time = now - pkt_ts;112if (sojourn_time < cprms->target || q->ni.len_bytes <= cst->maxpkt_size) {113/* went below - stay below for at least interval */114cst->first_above_time = 0;115} else {116if (cst->first_above_time == 0) {117/* just went above from below. if still above at118* first_above_time, will say it's ok to drop. */119cst->first_above_time = now + cprms->interval;120} else if (now >= cst->first_above_time) {121*ok_to_drop = 1;122}123}124return m;125}126127/*128* Dequeue a packet from queue 'q'129*/130__inline static struct mbuf *131codel_dequeue(struct dn_queue *q)132{133struct mbuf *m;134struct dn_aqm_codel_parms *cprms;135struct codel_status *cst;136aqm_time_t now;137uint16_t ok_to_drop;138139cst = q->aqm_status;140cprms = q->fs->aqmcfg;141now = AQM_UNOW;142143m = codel_dodequeue(q, now, &ok_to_drop);144if (cst->dropping) {145if (!ok_to_drop) {146/* sojourn time below target - leave dropping state */147cst->dropping = false;148}149/*150* Time for the next drop. Drop current packet and dequeue151* next. If the dequeue doesn't take us out of dropping152* state, schedule the next drop. A large backlog might153* result in drop rates so high that the next drop should154* happen now, hence the 'while' loop.155*/156while (now >= cst->drop_next_time && cst->dropping) {157/* mark the packet */158if (cprms->flags & CODEL_ECN_ENABLED && ecn_mark(m)) {159cst->count++;160/* schedule the next mark. */161cst->drop_next_time = control_law(cst, cprms,162cst->drop_next_time);163return m;164}165166/* drop the packet */167update_stats(q, 0, 1);168FREE_PKT(m);169m = codel_dodequeue(q, now, &ok_to_drop);170171if (!ok_to_drop) {172/* leave dropping state */173cst->dropping = false;174} else {175cst->count++;176/* schedule the next drop. */177cst->drop_next_time = control_law(cst, cprms,178cst->drop_next_time);179}180}181/* If we get here we're not in dropping state. The 'ok_to_drop'182* return from dodequeue means that the sojourn time has been183* above 'target' for 'interval' so enter dropping state.184*/185} else if (ok_to_drop) {186/* if ECN option is disabled or the packet cannot be marked,187* drop the packet and extract another.188*/189if (!(cprms->flags & CODEL_ECN_ENABLED) || !ecn_mark(m)) {190update_stats(q, 0, 1);191FREE_PKT(m);192m = codel_dodequeue(q, now, &ok_to_drop);193}194195cst->dropping = true;196197/* If min went above target close to when it last went198* below, assume that the drop rate that controlled the199* queue on the last cycle is a good starting point to200* control it now. ('drop_next' will be at most 'interval'201* later than the time of the last drop so 'now - drop_next'202* is a good approximation of the time from the last drop203* until now.)204*/205cst->count = (cst->count > 2 && ((aqm_stime_t)now -206(aqm_stime_t)cst->drop_next_time) < 8* cprms->interval)?207cst->count - 2 : 1;208/* we don't have to set initial guess for Newton's method isqrt as209* we initilaize isqrt in control_law function when count == 1 */210cst->drop_next_time = control_law(cst, cprms, now);211}212213return m;214}215216#endif217218219