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