1*91336b40SDon Lewis /* 2*91336b40SDon Lewis * Codel - The Controlled-Delay Active Queue Management algorithm. 3*91336b40SDon Lewis * 4*91336b40SDon Lewis * $FreeBSD$ 5*91336b40SDon Lewis * 6*91336b40SDon Lewis * Copyright (C) 2016 Centre for Advanced Internet Architectures, 7*91336b40SDon Lewis * Swinburne University of Technology, Melbourne, Australia. 8*91336b40SDon Lewis * Portions of this code were made possible in part by a gift from 9*91336b40SDon Lewis * The Comcast Innovation Fund. 10*91336b40SDon Lewis * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au> 11*91336b40SDon Lewis * 12*91336b40SDon Lewis * Copyright (C) 2011-2014 Kathleen Nichols <nichols@pollere.com>. 13*91336b40SDon Lewis * 14*91336b40SDon Lewis * Redistribution and use in source and binary forms, with or without 15*91336b40SDon Lewis * modification, are permitted provided that the following conditions 16*91336b40SDon Lewis * are met: 17*91336b40SDon Lewis * 18*91336b40SDon Lewis * o Redistributions of source code must retain the above copyright 19*91336b40SDon Lewis * notice, this list of conditions, and the following disclaimer, 20*91336b40SDon Lewis * without modification. 21*91336b40SDon Lewis * 22*91336b40SDon Lewis * o Redistributions in binary form must reproduce the above copyright 23*91336b40SDon Lewis * notice, this list of conditions and the following disclaimer in 24*91336b40SDon Lewis * the documentation and/or other materials provided with the 25*91336b40SDon Lewis * distribution. 26*91336b40SDon Lewis * 27*91336b40SDon Lewis * o The names of the authors may not be used to endorse or promote 28*91336b40SDon Lewis * products derived from this software without specific prior written 29*91336b40SDon Lewis * permission. 30*91336b40SDon Lewis * 31*91336b40SDon Lewis * Alternatively, provided that this notice is retained in full, this 32*91336b40SDon Lewis * software may be distributed under the terms of the GNU General Public 33*91336b40SDon Lewis * License ("GPL") version 2, in which case the provisions of the GPL 34*91336b40SDon Lewis * apply INSTEAD OF those given above. 35*91336b40SDon Lewis 36*91336b40SDon Lewis * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 37*91336b40SDon Lewis * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 38*91336b40SDon Lewis * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 39*91336b40SDon Lewis * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 40*91336b40SDon Lewis * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41*91336b40SDon Lewis * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 42*91336b40SDon Lewis * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 43*91336b40SDon Lewis * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 44*91336b40SDon Lewis * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 45*91336b40SDon Lewis * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 46*91336b40SDon Lewis * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 47*91336b40SDon Lewis */ 48*91336b40SDon Lewis 49*91336b40SDon Lewis #ifndef _IP_DN_AQM_CODEL_H 50*91336b40SDon Lewis #define _IP_DN_AQM_CODEL_H 51*91336b40SDon Lewis 52*91336b40SDon Lewis 53*91336b40SDon Lewis // XXX How to choose MTAG? 54*91336b40SDon Lewis #define FIX_POINT_BITS 16 55*91336b40SDon Lewis 56*91336b40SDon Lewis enum { 57*91336b40SDon Lewis CODEL_ECN_ENABLED = 1 58*91336b40SDon Lewis }; 59*91336b40SDon Lewis 60*91336b40SDon Lewis /* Codel parameters */ 61*91336b40SDon Lewis struct dn_aqm_codel_parms { 62*91336b40SDon Lewis aqm_time_t target; 63*91336b40SDon Lewis aqm_time_t interval; 64*91336b40SDon Lewis uint32_t flags; 65*91336b40SDon Lewis }; 66*91336b40SDon Lewis 67*91336b40SDon Lewis /* codel status variables */ 68*91336b40SDon Lewis struct codel_status { 69*91336b40SDon Lewis uint32_t count; /* number of dropped pkts since entering drop state */ 70*91336b40SDon Lewis uint16_t dropping; /* dropping state */ 71*91336b40SDon Lewis aqm_time_t drop_next_time; /* time for next drop */ 72*91336b40SDon Lewis aqm_time_t first_above_time; /* time for first ts over target we observed */ 73*91336b40SDon Lewis uint16_t isqrt; /* last isqrt for control low */ 74*91336b40SDon Lewis uint16_t maxpkt_size; /* max packet size seen so far */ 75*91336b40SDon Lewis }; 76*91336b40SDon Lewis 77*91336b40SDon Lewis struct mbuf *codel_extract_head(struct dn_queue *, aqm_time_t *); 78*91336b40SDon Lewis aqm_time_t control_law(struct codel_status *, 79*91336b40SDon Lewis struct dn_aqm_codel_parms *, aqm_time_t ); 80*91336b40SDon Lewis 81*91336b40SDon Lewis __inline static struct mbuf * 82*91336b40SDon Lewis codel_dodequeue(struct dn_queue *q, aqm_time_t now, uint16_t *ok_to_drop) 83*91336b40SDon Lewis { 84*91336b40SDon Lewis struct mbuf * m; 85*91336b40SDon Lewis struct dn_aqm_codel_parms *cprms; 86*91336b40SDon Lewis struct codel_status *cst; 87*91336b40SDon Lewis aqm_time_t pkt_ts, sojourn_time; 88*91336b40SDon Lewis 89*91336b40SDon Lewis *ok_to_drop = 0; 90*91336b40SDon Lewis m = codel_extract_head(q, &pkt_ts); 91*91336b40SDon Lewis 92*91336b40SDon Lewis cst = q->aqm_status; 93*91336b40SDon Lewis 94*91336b40SDon Lewis if (m == NULL) { 95*91336b40SDon Lewis /* queue is empty - we can't be above target */ 96*91336b40SDon Lewis cst->first_above_time= 0; 97*91336b40SDon Lewis return m; 98*91336b40SDon Lewis } 99*91336b40SDon Lewis 100*91336b40SDon Lewis cprms = q->fs->aqmcfg; 101*91336b40SDon Lewis 102*91336b40SDon Lewis /* To span a large range of bandwidths, CoDel runs two 103*91336b40SDon Lewis * different AQMs in parallel. One is sojourn-time-based 104*91336b40SDon Lewis * and takes effect when the time to send an MTU-sized 105*91336b40SDon Lewis * packet is less than target. The 1st term of the "if" 106*91336b40SDon Lewis * below does this. The other is backlog-based and takes 107*91336b40SDon Lewis * effect when the time to send an MTU-sized packet is >= 108*91336b40SDon Lewis * target. The goal here is to keep the output link 109*91336b40SDon Lewis * utilization high by never allowing the queue to get 110*91336b40SDon Lewis * smaller than the amount that arrives in a typical 111*91336b40SDon Lewis * interarrival time (MTU-sized packets arriving spaced 112*91336b40SDon Lewis * by the amount of time it takes to send such a packet on 113*91336b40SDon Lewis * the bottleneck). The 2nd term of the "if" does this. 114*91336b40SDon Lewis */ 115*91336b40SDon Lewis sojourn_time = now - pkt_ts; 116*91336b40SDon Lewis if (sojourn_time < cprms->target || q->ni.len_bytes <= cst->maxpkt_size) { 117*91336b40SDon Lewis /* went below - stay below for at least interval */ 118*91336b40SDon Lewis cst->first_above_time = 0; 119*91336b40SDon Lewis } else { 120*91336b40SDon Lewis if (cst->first_above_time == 0) { 121*91336b40SDon Lewis /* just went above from below. if still above at 122*91336b40SDon Lewis * first_above_time, will say it's ok to drop. */ 123*91336b40SDon Lewis cst->first_above_time = now + cprms->interval; 124*91336b40SDon Lewis } else if (now >= cst->first_above_time) { 125*91336b40SDon Lewis *ok_to_drop = 1; 126*91336b40SDon Lewis } 127*91336b40SDon Lewis } 128*91336b40SDon Lewis return m; 129*91336b40SDon Lewis } 130*91336b40SDon Lewis 131*91336b40SDon Lewis /* 132*91336b40SDon Lewis * Dequeue a packet from queue 'q' 133*91336b40SDon Lewis */ 134*91336b40SDon Lewis __inline static struct mbuf * 135*91336b40SDon Lewis codel_dequeue(struct dn_queue *q) 136*91336b40SDon Lewis { 137*91336b40SDon Lewis struct mbuf *m; 138*91336b40SDon Lewis struct dn_aqm_codel_parms *cprms; 139*91336b40SDon Lewis struct codel_status *cst; 140*91336b40SDon Lewis aqm_time_t now; 141*91336b40SDon Lewis uint16_t ok_to_drop; 142*91336b40SDon Lewis 143*91336b40SDon Lewis cst = q->aqm_status;; 144*91336b40SDon Lewis cprms = q->fs->aqmcfg; 145*91336b40SDon Lewis now = AQM_UNOW; 146*91336b40SDon Lewis 147*91336b40SDon Lewis m = codel_dodequeue(q, now, &ok_to_drop); 148*91336b40SDon Lewis if (cst->dropping) { 149*91336b40SDon Lewis if (!ok_to_drop) { 150*91336b40SDon Lewis /* sojourn time below target - leave dropping state */ 151*91336b40SDon Lewis cst->dropping = false; 152*91336b40SDon Lewis } 153*91336b40SDon Lewis /* 154*91336b40SDon Lewis * Time for the next drop. Drop current packet and dequeue 155*91336b40SDon Lewis * next. If the dequeue doesn't take us out of dropping 156*91336b40SDon Lewis * state, schedule the next drop. A large backlog might 157*91336b40SDon Lewis * result in drop rates so high that the next drop should 158*91336b40SDon Lewis * happen now, hence the 'while' loop. 159*91336b40SDon Lewis */ 160*91336b40SDon Lewis while (now >= cst->drop_next_time && cst->dropping) { 161*91336b40SDon Lewis 162*91336b40SDon Lewis /* mark the packet */ 163*91336b40SDon Lewis if (cprms->flags & CODEL_ECN_ENABLED && ecn_mark(m)) { 164*91336b40SDon Lewis cst->count++; 165*91336b40SDon Lewis /* schedule the next mark. */ 166*91336b40SDon Lewis cst->drop_next_time = control_law(cst, cprms, 167*91336b40SDon Lewis cst->drop_next_time); 168*91336b40SDon Lewis return m; 169*91336b40SDon Lewis } 170*91336b40SDon Lewis 171*91336b40SDon Lewis /* drop the packet */ 172*91336b40SDon Lewis update_stats(q, 0, 1); 173*91336b40SDon Lewis FREE_PKT(m); 174*91336b40SDon Lewis m = codel_dodequeue(q, now, &ok_to_drop); 175*91336b40SDon Lewis 176*91336b40SDon Lewis if (!ok_to_drop) { 177*91336b40SDon Lewis /* leave dropping state */ 178*91336b40SDon Lewis cst->dropping = false; 179*91336b40SDon Lewis } else { 180*91336b40SDon Lewis cst->count++; 181*91336b40SDon Lewis /* schedule the next drop. */ 182*91336b40SDon Lewis cst->drop_next_time = control_law(cst, cprms, 183*91336b40SDon Lewis cst->drop_next_time); 184*91336b40SDon Lewis } 185*91336b40SDon Lewis } 186*91336b40SDon Lewis /* If we get here we're not in dropping state. The 'ok_to_drop' 187*91336b40SDon Lewis * return from dodequeue means that the sojourn time has been 188*91336b40SDon Lewis * above 'target' for 'interval' so enter dropping state. 189*91336b40SDon Lewis */ 190*91336b40SDon Lewis } else if (ok_to_drop) { 191*91336b40SDon Lewis 192*91336b40SDon Lewis /* if ECN option is disabled or the packet cannot be marked, 193*91336b40SDon Lewis * drop the packet and extract another. 194*91336b40SDon Lewis */ 195*91336b40SDon Lewis if (!(cprms->flags & CODEL_ECN_ENABLED) || !ecn_mark(m)) { 196*91336b40SDon Lewis update_stats(q, 0, 1); 197*91336b40SDon Lewis FREE_PKT(m); 198*91336b40SDon Lewis m = codel_dodequeue(q, now, &ok_to_drop); 199*91336b40SDon Lewis } 200*91336b40SDon Lewis 201*91336b40SDon Lewis cst->dropping = true; 202*91336b40SDon Lewis 203*91336b40SDon Lewis /* If min went above target close to when it last went 204*91336b40SDon Lewis * below, assume that the drop rate that controlled the 205*91336b40SDon Lewis * queue on the last cycle is a good starting point to 206*91336b40SDon Lewis * control it now. ('drop_next' will be at most 'interval' 207*91336b40SDon Lewis * later than the time of the last drop so 'now - drop_next' 208*91336b40SDon Lewis * is a good approximation of the time from the last drop 209*91336b40SDon Lewis * until now.) 210*91336b40SDon Lewis */ 211*91336b40SDon Lewis cst->count = (cst->count > 2 && ((aqm_stime_t)now - 212*91336b40SDon Lewis (aqm_stime_t)cst->drop_next_time) < 8* cprms->interval)? 213*91336b40SDon Lewis cst->count - 2 : 1; 214*91336b40SDon Lewis /* we don't have to set initial guess for Newton's method isqrt as 215*91336b40SDon Lewis * we initilaize isqrt in control_law function when count == 1 */ 216*91336b40SDon Lewis cst->drop_next_time = control_law(cst, cprms, now); 217*91336b40SDon Lewis } 218*91336b40SDon Lewis 219*91336b40SDon Lewis return m; 220*91336b40SDon Lewis } 221*91336b40SDon Lewis 222*91336b40SDon Lewis #endif 223