191336b40SDon Lewis /*
291336b40SDon Lewis * Codel - The Controlled-Delay Active Queue Management algorithm.
391336b40SDon Lewis *
491336b40SDon Lewis * Copyright (C) 2016 Centre for Advanced Internet Architectures,
591336b40SDon Lewis * Swinburne University of Technology, Melbourne, Australia.
691336b40SDon Lewis * Portions of this code were made possible in part by a gift from
791336b40SDon Lewis * The Comcast Innovation Fund.
891336b40SDon Lewis * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au>
991336b40SDon Lewis *
1091336b40SDon Lewis * Copyright (C) 2011-2014 Kathleen Nichols <nichols@pollere.com>.
1191336b40SDon Lewis *
1291336b40SDon Lewis * Redistribution and use in source and binary forms, with or without
1391336b40SDon Lewis * modification, are permitted provided that the following conditions
1491336b40SDon Lewis * are met:
1591336b40SDon Lewis *
1691336b40SDon Lewis * o Redistributions of source code must retain the above copyright
1791336b40SDon Lewis * notice, this list of conditions, and the following disclaimer,
1891336b40SDon Lewis * without modification.
1991336b40SDon Lewis *
2091336b40SDon Lewis * o Redistributions in binary form must reproduce the above copyright
2191336b40SDon Lewis * notice, this list of conditions and the following disclaimer in
2291336b40SDon Lewis * the documentation and/or other materials provided with the
2391336b40SDon Lewis * distribution.
2491336b40SDon Lewis *
2591336b40SDon Lewis * o The names of the authors may not be used to endorse or promote
2691336b40SDon Lewis * products derived from this software without specific prior written
2791336b40SDon Lewis * permission.
2891336b40SDon Lewis *
2991336b40SDon Lewis * Alternatively, provided that this notice is retained in full, this
3091336b40SDon Lewis * software may be distributed under the terms of the GNU General Public
3191336b40SDon Lewis * License ("GPL") version 2, in which case the provisions of the GPL
3291336b40SDon Lewis * apply INSTEAD OF those given above.
3391336b40SDon Lewis
3491336b40SDon Lewis * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3591336b40SDon Lewis * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3691336b40SDon Lewis * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3791336b40SDon Lewis * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3891336b40SDon Lewis * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3991336b40SDon Lewis * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
4091336b40SDon Lewis * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
4191336b40SDon Lewis * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
4291336b40SDon Lewis * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4391336b40SDon Lewis * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
4491336b40SDon Lewis * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4591336b40SDon Lewis */
4691336b40SDon Lewis
4791336b40SDon Lewis #ifndef _IP_DN_AQM_CODEL_H
4891336b40SDon Lewis #define _IP_DN_AQM_CODEL_H
4991336b40SDon Lewis
5091336b40SDon Lewis // XXX How to choose MTAG?
5191336b40SDon Lewis #define FIX_POINT_BITS 16
5291336b40SDon Lewis
5391336b40SDon Lewis enum {
5491336b40SDon Lewis CODEL_ECN_ENABLED = 1
5591336b40SDon Lewis };
5691336b40SDon Lewis
5791336b40SDon Lewis /* Codel parameters */
5891336b40SDon Lewis struct dn_aqm_codel_parms {
5991336b40SDon Lewis aqm_time_t target;
6091336b40SDon Lewis aqm_time_t interval;
6191336b40SDon Lewis uint32_t flags;
6291336b40SDon Lewis };
6391336b40SDon Lewis
6491336b40SDon Lewis /* codel status variables */
6591336b40SDon Lewis struct codel_status {
6691336b40SDon Lewis uint32_t count; /* number of dropped pkts since entering drop state */
6791336b40SDon Lewis uint16_t dropping; /* dropping state */
6891336b40SDon Lewis aqm_time_t drop_next_time; /* time for next drop */
6991336b40SDon Lewis aqm_time_t first_above_time; /* time for first ts over target we observed */
7091336b40SDon Lewis uint16_t isqrt; /* last isqrt for control low */
7191336b40SDon Lewis uint16_t maxpkt_size; /* max packet size seen so far */
7291336b40SDon Lewis };
7391336b40SDon Lewis
7491336b40SDon Lewis struct mbuf *codel_extract_head(struct dn_queue *, aqm_time_t *);
7591336b40SDon Lewis aqm_time_t control_law(struct codel_status *,
7691336b40SDon Lewis struct dn_aqm_codel_parms *, aqm_time_t );
7791336b40SDon Lewis
7891336b40SDon Lewis __inline static struct mbuf *
codel_dodequeue(struct dn_queue * q,aqm_time_t now,uint16_t * ok_to_drop)7991336b40SDon Lewis codel_dodequeue(struct dn_queue *q, aqm_time_t now, uint16_t *ok_to_drop)
8091336b40SDon Lewis {
8191336b40SDon Lewis struct mbuf * m;
8291336b40SDon Lewis struct dn_aqm_codel_parms *cprms;
8391336b40SDon Lewis struct codel_status *cst;
8491336b40SDon Lewis aqm_time_t pkt_ts, sojourn_time;
8591336b40SDon Lewis
8691336b40SDon Lewis *ok_to_drop = 0;
8791336b40SDon Lewis m = codel_extract_head(q, &pkt_ts);
8891336b40SDon Lewis
8991336b40SDon Lewis cst = q->aqm_status;
9091336b40SDon Lewis
9191336b40SDon Lewis if (m == NULL) {
9291336b40SDon Lewis /* queue is empty - we can't be above target */
9391336b40SDon Lewis cst->first_above_time= 0;
9491336b40SDon Lewis return m;
9591336b40SDon Lewis }
9691336b40SDon Lewis
9791336b40SDon Lewis cprms = q->fs->aqmcfg;
9891336b40SDon Lewis
9991336b40SDon Lewis /* To span a large range of bandwidths, CoDel runs two
10091336b40SDon Lewis * different AQMs in parallel. One is sojourn-time-based
10191336b40SDon Lewis * and takes effect when the time to send an MTU-sized
10291336b40SDon Lewis * packet is less than target. The 1st term of the "if"
10391336b40SDon Lewis * below does this. The other is backlog-based and takes
10491336b40SDon Lewis * effect when the time to send an MTU-sized packet is >=
10591336b40SDon Lewis * target. The goal here is to keep the output link
10691336b40SDon Lewis * utilization high by never allowing the queue to get
10791336b40SDon Lewis * smaller than the amount that arrives in a typical
10891336b40SDon Lewis * interarrival time (MTU-sized packets arriving spaced
10991336b40SDon Lewis * by the amount of time it takes to send such a packet on
11091336b40SDon Lewis * the bottleneck). The 2nd term of the "if" does this.
11191336b40SDon Lewis */
11291336b40SDon Lewis sojourn_time = now - pkt_ts;
11391336b40SDon Lewis if (sojourn_time < cprms->target || q->ni.len_bytes <= cst->maxpkt_size) {
11491336b40SDon Lewis /* went below - stay below for at least interval */
11591336b40SDon Lewis cst->first_above_time = 0;
11691336b40SDon Lewis } else {
11791336b40SDon Lewis if (cst->first_above_time == 0) {
11891336b40SDon Lewis /* just went above from below. if still above at
11991336b40SDon Lewis * first_above_time, will say it's ok to drop. */
12091336b40SDon Lewis cst->first_above_time = now + cprms->interval;
12191336b40SDon Lewis } else if (now >= cst->first_above_time) {
12291336b40SDon Lewis *ok_to_drop = 1;
12391336b40SDon Lewis }
12491336b40SDon Lewis }
12591336b40SDon Lewis return m;
12691336b40SDon Lewis }
12791336b40SDon Lewis
12891336b40SDon Lewis /*
12991336b40SDon Lewis * Dequeue a packet from queue 'q'
13091336b40SDon Lewis */
13191336b40SDon Lewis __inline static struct mbuf *
codel_dequeue(struct dn_queue * q)13291336b40SDon Lewis codel_dequeue(struct dn_queue *q)
13391336b40SDon Lewis {
13491336b40SDon Lewis struct mbuf *m;
13591336b40SDon Lewis struct dn_aqm_codel_parms *cprms;
13691336b40SDon Lewis struct codel_status *cst;
13791336b40SDon Lewis aqm_time_t now;
13891336b40SDon Lewis uint16_t ok_to_drop;
13991336b40SDon Lewis
140*21cc0918SElliott Mitchell cst = q->aqm_status;
14191336b40SDon Lewis cprms = q->fs->aqmcfg;
14291336b40SDon Lewis now = AQM_UNOW;
14391336b40SDon Lewis
14491336b40SDon Lewis m = codel_dodequeue(q, now, &ok_to_drop);
14591336b40SDon Lewis if (cst->dropping) {
14691336b40SDon Lewis if (!ok_to_drop) {
14791336b40SDon Lewis /* sojourn time below target - leave dropping state */
14891336b40SDon Lewis cst->dropping = false;
14991336b40SDon Lewis }
15091336b40SDon Lewis /*
15191336b40SDon Lewis * Time for the next drop. Drop current packet and dequeue
15291336b40SDon Lewis * next. If the dequeue doesn't take us out of dropping
15391336b40SDon Lewis * state, schedule the next drop. A large backlog might
15491336b40SDon Lewis * result in drop rates so high that the next drop should
15591336b40SDon Lewis * happen now, hence the 'while' loop.
15691336b40SDon Lewis */
15791336b40SDon Lewis while (now >= cst->drop_next_time && cst->dropping) {
15891336b40SDon Lewis /* mark the packet */
15991336b40SDon Lewis if (cprms->flags & CODEL_ECN_ENABLED && ecn_mark(m)) {
16091336b40SDon Lewis cst->count++;
16191336b40SDon Lewis /* schedule the next mark. */
16291336b40SDon Lewis cst->drop_next_time = control_law(cst, cprms,
16391336b40SDon Lewis cst->drop_next_time);
16491336b40SDon Lewis return m;
16591336b40SDon Lewis }
16691336b40SDon Lewis
16791336b40SDon Lewis /* drop the packet */
16891336b40SDon Lewis update_stats(q, 0, 1);
16991336b40SDon Lewis FREE_PKT(m);
17091336b40SDon Lewis m = codel_dodequeue(q, now, &ok_to_drop);
17191336b40SDon Lewis
17291336b40SDon Lewis if (!ok_to_drop) {
17391336b40SDon Lewis /* leave dropping state */
17491336b40SDon Lewis cst->dropping = false;
17591336b40SDon Lewis } else {
17691336b40SDon Lewis cst->count++;
17791336b40SDon Lewis /* schedule the next drop. */
17891336b40SDon Lewis cst->drop_next_time = control_law(cst, cprms,
17991336b40SDon Lewis cst->drop_next_time);
18091336b40SDon Lewis }
18191336b40SDon Lewis }
18291336b40SDon Lewis /* If we get here we're not in dropping state. The 'ok_to_drop'
18391336b40SDon Lewis * return from dodequeue means that the sojourn time has been
18491336b40SDon Lewis * above 'target' for 'interval' so enter dropping state.
18591336b40SDon Lewis */
18691336b40SDon Lewis } else if (ok_to_drop) {
18791336b40SDon Lewis /* if ECN option is disabled or the packet cannot be marked,
18891336b40SDon Lewis * drop the packet and extract another.
18991336b40SDon Lewis */
19091336b40SDon Lewis if (!(cprms->flags & CODEL_ECN_ENABLED) || !ecn_mark(m)) {
19191336b40SDon Lewis update_stats(q, 0, 1);
19291336b40SDon Lewis FREE_PKT(m);
19391336b40SDon Lewis m = codel_dodequeue(q, now, &ok_to_drop);
19491336b40SDon Lewis }
19591336b40SDon Lewis
19691336b40SDon Lewis cst->dropping = true;
19791336b40SDon Lewis
19891336b40SDon Lewis /* If min went above target close to when it last went
19991336b40SDon Lewis * below, assume that the drop rate that controlled the
20091336b40SDon Lewis * queue on the last cycle is a good starting point to
20191336b40SDon Lewis * control it now. ('drop_next' will be at most 'interval'
20291336b40SDon Lewis * later than the time of the last drop so 'now - drop_next'
20391336b40SDon Lewis * is a good approximation of the time from the last drop
20491336b40SDon Lewis * until now.)
20591336b40SDon Lewis */
20691336b40SDon Lewis cst->count = (cst->count > 2 && ((aqm_stime_t)now -
20791336b40SDon Lewis (aqm_stime_t)cst->drop_next_time) < 8* cprms->interval)?
20891336b40SDon Lewis cst->count - 2 : 1;
20991336b40SDon Lewis /* we don't have to set initial guess for Newton's method isqrt as
21091336b40SDon Lewis * we initilaize isqrt in control_law function when count == 1 */
21191336b40SDon Lewis cst->drop_next_time = control_law(cst, cprms, now);
21291336b40SDon Lewis }
21391336b40SDon Lewis
21491336b40SDon Lewis return m;
21591336b40SDon Lewis }
21691336b40SDon Lewis
21791336b40SDon Lewis #endif
218