xref: /freebsd/sys/netinet/cc/cc_cubic.c (revision 038699a8f18a0a651ee06b85fa1dbbee1eab56f1)
167fef78bSLawrence Stewart /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
467fef78bSLawrence Stewart  * Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org>
567fef78bSLawrence Stewart  * Copyright (c) 2010 The FreeBSD Foundation
667fef78bSLawrence Stewart  * All rights reserved.
767fef78bSLawrence Stewart  *
867fef78bSLawrence Stewart  * This software was developed by Lawrence Stewart while studying at the Centre
9891b8ed4SLawrence Stewart  * for Advanced Internet Architectures, Swinburne University of Technology, made
10891b8ed4SLawrence Stewart  * possible in part by a grant from the Cisco University Research Program Fund
11891b8ed4SLawrence Stewart  * at Community Foundation Silicon Valley.
1267fef78bSLawrence Stewart  *
1367fef78bSLawrence Stewart  * Portions of this software were developed at the Centre for Advanced
1467fef78bSLawrence Stewart  * Internet Architectures, Swinburne University of Technology, Melbourne,
1567fef78bSLawrence Stewart  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
1667fef78bSLawrence Stewart  *
1767fef78bSLawrence Stewart  * Redistribution and use in source and binary forms, with or without
1867fef78bSLawrence Stewart  * modification, are permitted provided that the following conditions
1967fef78bSLawrence Stewart  * are met:
2067fef78bSLawrence Stewart  * 1. Redistributions of source code must retain the above copyright
2167fef78bSLawrence Stewart  *    notice, this list of conditions and the following disclaimer.
2267fef78bSLawrence Stewart  * 2. Redistributions in binary form must reproduce the above copyright
2367fef78bSLawrence Stewart  *    notice, this list of conditions and the following disclaimer in the
2467fef78bSLawrence Stewart  *    documentation and/or other materials provided with the distribution.
2567fef78bSLawrence Stewart  *
2667fef78bSLawrence Stewart  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2767fef78bSLawrence Stewart  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2867fef78bSLawrence Stewart  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2967fef78bSLawrence Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3067fef78bSLawrence Stewart  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3167fef78bSLawrence Stewart  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3267fef78bSLawrence Stewart  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3367fef78bSLawrence Stewart  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3467fef78bSLawrence Stewart  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3567fef78bSLawrence Stewart  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3667fef78bSLawrence Stewart  * SUCH DAMAGE.
3767fef78bSLawrence Stewart  */
3867fef78bSLawrence Stewart 
3967fef78bSLawrence Stewart /*
4067fef78bSLawrence Stewart  * An implementation of the CUBIC congestion control algorithm for FreeBSD,
4167fef78bSLawrence Stewart  * based on the Internet Draft "draft-rhee-tcpm-cubic-02" by Rhee, Xu and Ha.
4267fef78bSLawrence Stewart  * Originally released as part of the NewTCP research project at Swinburne
43891b8ed4SLawrence Stewart  * University of Technology's Centre for Advanced Internet Architectures,
44891b8ed4SLawrence Stewart  * Melbourne, Australia, which was made possible in part by a grant from the
45891b8ed4SLawrence Stewart  * Cisco University Research Program Fund at Community Foundation Silicon
46891b8ed4SLawrence Stewart  * Valley. More details are available at:
4767fef78bSLawrence Stewart  *   http://caia.swin.edu.au/urp/newtcp/
4867fef78bSLawrence Stewart  */
4967fef78bSLawrence Stewart 
5067fef78bSLawrence Stewart #include <sys/param.h>
5167fef78bSLawrence Stewart #include <sys/kernel.h>
52c968c769SMichael Tuexen #include <sys/limits.h>
5367fef78bSLawrence Stewart #include <sys/malloc.h>
5467fef78bSLawrence Stewart #include <sys/module.h>
5567fef78bSLawrence Stewart #include <sys/socket.h>
5667fef78bSLawrence Stewart #include <sys/socketvar.h>
5767fef78bSLawrence Stewart #include <sys/sysctl.h>
5867fef78bSLawrence Stewart #include <sys/systm.h>
5967fef78bSLawrence Stewart 
6067fef78bSLawrence Stewart #include <net/vnet.h>
6167fef78bSLawrence Stewart 
62b8d60729SRandall Stewart #include <net/route.h>
63b8d60729SRandall Stewart #include <net/route/nhop.h>
64b8d60729SRandall Stewart 
65b8d60729SRandall Stewart #include <netinet/in_pcb.h>
662de3e790SGleb Smirnoff #include <netinet/tcp.h>
6767fef78bSLawrence Stewart #include <netinet/tcp_seq.h>
6867fef78bSLawrence Stewart #include <netinet/tcp_timer.h>
6967fef78bSLawrence Stewart #include <netinet/tcp_var.h>
70a9696510SRandall Stewart #include <netinet/tcp_log_buf.h>
71a9696510SRandall Stewart #include <netinet/tcp_hpts.h>
724644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
7367fef78bSLawrence Stewart #include <netinet/cc/cc_cubic.h>
7467fef78bSLawrence Stewart #include <netinet/cc/cc_module.h>
7567fef78bSLawrence Stewart 
76f74352fbSRichard Scheffenegger static void	cubic_ack_received(struct cc_var *ccv, ccsignal_t type);
7767fef78bSLawrence Stewart static void	cubic_cb_destroy(struct cc_var *ccv);
78b8d60729SRandall Stewart static int	cubic_cb_init(struct cc_var *ccv, void *ptr);
79f74352fbSRichard Scheffenegger static void	cubic_cong_signal(struct cc_var *ccv, ccsignal_t type);
8067fef78bSLawrence Stewart static void	cubic_conn_init(struct cc_var *ccv);
8167fef78bSLawrence Stewart static int	cubic_mod_init(void);
8267fef78bSLawrence Stewart static void	cubic_post_recovery(struct cc_var *ccv);
8367fef78bSLawrence Stewart static void	cubic_record_rtt(struct cc_var *ccv);
8437674273SRichard Scheffenegger static void	cubic_ssthresh_update(struct cc_var *ccv, uint32_t maxseg);
8535cd141bSMichael Tuexen static void	cubic_after_idle(struct cc_var *ccv);
86b8d60729SRandall Stewart static size_t	cubic_data_sz(void);
87a9696510SRandall Stewart static void	cubic_newround(struct cc_var *ccv, uint32_t round_cnt);
88a9696510SRandall Stewart static void	cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt,
89a9696510SRandall Stewart        uint32_t rxtcnt, uint32_t fas);
9067fef78bSLawrence Stewart 
9167fef78bSLawrence Stewart struct cc_algo cubic_cc_algo = {
9267fef78bSLawrence Stewart 	.name = "cubic",
9367fef78bSLawrence Stewart 	.ack_received = cubic_ack_received,
9467fef78bSLawrence Stewart 	.cb_destroy = cubic_cb_destroy,
9567fef78bSLawrence Stewart 	.cb_init = cubic_cb_init,
9667fef78bSLawrence Stewart 	.cong_signal = cubic_cong_signal,
9767fef78bSLawrence Stewart 	.conn_init = cubic_conn_init,
9867fef78bSLawrence Stewart 	.mod_init = cubic_mod_init,
9967fef78bSLawrence Stewart 	.post_recovery = cubic_post_recovery,
10035cd141bSMichael Tuexen 	.after_idle = cubic_after_idle,
101a9696510SRandall Stewart 	.cc_data_sz = cubic_data_sz,
102a9696510SRandall Stewart 	.rttsample = cubic_rttsample,
103a9696510SRandall Stewart 	.newround = cubic_newround
10467fef78bSLawrence Stewart };
10567fef78bSLawrence Stewart 
10667fef78bSLawrence Stewart static void
107a9696510SRandall Stewart cubic_log_hystart_event(struct cc_var *ccv, struct cubic *cubicd, uint8_t mod, uint32_t flex1)
108a9696510SRandall Stewart {
109a9696510SRandall Stewart 	/*
110a9696510SRandall Stewart 	 * Types of logs (mod value)
111a9696510SRandall Stewart 	 * 1 - rtt_thresh in flex1, checking to see if RTT is to great.
112a9696510SRandall Stewart 	 * 2 - rtt is too great, rtt_thresh in flex1.
113a9696510SRandall Stewart 	 * 3 - CSS is active incr in flex1
114a9696510SRandall Stewart 	 * 4 - A new round is beginning flex1 is round count
115a9696510SRandall Stewart 	 * 5 - A new RTT measurement flex1 is the new measurement.
116a9696510SRandall Stewart 	 * 6 - We enter CA ssthresh is also in flex1.
117a9696510SRandall Stewart 	 * 7 - Socket option to change hystart executed opt.val in flex1.
118a9696510SRandall Stewart 	 * 8 - Back out of CSS into SS, flex1 is the css_baseline_minrtt
119a9696510SRandall Stewart 	 * 9 - We enter CA, via an ECN mark.
120a9696510SRandall Stewart 	 * 10 - We enter CA, via a loss.
121a9696510SRandall Stewart 	 * 11 - We have slipped out of SS into CA via cwnd growth.
122a9696510SRandall Stewart 	 * 12 - After idle has re-enabled hystart++
123a9696510SRandall Stewart 	 */
124a9696510SRandall Stewart 	struct tcpcb *tp;
125a9696510SRandall Stewart 
126a9696510SRandall Stewart 	if (hystart_bblogs == 0)
127a9696510SRandall Stewart 		return;
128a9696510SRandall Stewart 	tp = ccv->ccvc.tcp;
12969c7c811SRandall Stewart 	if (tcp_bblogging_on(tp)) {
130a9696510SRandall Stewart 		union tcp_log_stackspecific log;
131a9696510SRandall Stewart 		struct timeval tv;
132a9696510SRandall Stewart 
133a9696510SRandall Stewart 		memset(&log, 0, sizeof(log));
134a9696510SRandall Stewart 		log.u_bbr.flex1 = flex1;
135a9696510SRandall Stewart 		log.u_bbr.flex2 = cubicd->css_current_round_minrtt;
136a9696510SRandall Stewart 		log.u_bbr.flex3 = cubicd->css_lastround_minrtt;
137a9696510SRandall Stewart 		log.u_bbr.flex4 = cubicd->css_rttsample_count;
138a9696510SRandall Stewart 		log.u_bbr.flex5 = cubicd->css_entered_at_round;
139a9696510SRandall Stewart 		log.u_bbr.flex6 = cubicd->css_baseline_minrtt;
140a9696510SRandall Stewart 		/* We only need bottom 16 bits of flags */
141a9696510SRandall Stewart 		log.u_bbr.flex7 = cubicd->flags & 0x0000ffff;
142a9696510SRandall Stewart 		log.u_bbr.flex8 = mod;
143a9696510SRandall Stewart 		log.u_bbr.epoch = cubicd->css_current_round;
144a9696510SRandall Stewart 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
145a9696510SRandall Stewart 		log.u_bbr.lt_epoch = cubicd->css_fas_at_css_entry;
146a9696510SRandall Stewart 		log.u_bbr.pkts_out = cubicd->css_last_fas;
147a9696510SRandall Stewart 		log.u_bbr.delivered = cubicd->css_lowrtt_fas;
148a9696510SRandall Stewart 		log.u_bbr.pkt_epoch = ccv->flags;
149a9696510SRandall Stewart 		TCP_LOG_EVENTP(tp, NULL,
1509eb0e832SGleb Smirnoff 		    &tptosocket(tp)->so_rcv,
1519eb0e832SGleb Smirnoff 		    &tptosocket(tp)->so_snd,
152a9696510SRandall Stewart 		    TCP_HYSTART, 0,
153a9696510SRandall Stewart 		    0, &log, false, &tv);
154a9696510SRandall Stewart 	}
155a9696510SRandall Stewart }
156a9696510SRandall Stewart 
157a9696510SRandall Stewart static void
158a9696510SRandall Stewart cubic_does_slow_start(struct cc_var *ccv, struct cubic *cubicd)
159a9696510SRandall Stewart {
160a9696510SRandall Stewart 	/*
161a9696510SRandall Stewart 	 * In slow-start with ABC enabled and no RTO in sight?
162a9696510SRandall Stewart 	 * (Must not use abc_l_var > 1 if slow starting after
163a9696510SRandall Stewart 	 * an RTO. On RTO, snd_nxt = snd_una, so the
164a9696510SRandall Stewart 	 * snd_nxt == snd_max check is sufficient to
165a9696510SRandall Stewart 	 * handle this).
166a9696510SRandall Stewart 	 *
167a9696510SRandall Stewart 	 * XXXLAS: Find a way to signal SS after RTO that
168a9696510SRandall Stewart 	 * doesn't rely on tcpcb vars.
169a9696510SRandall Stewart 	 */
170a9696510SRandall Stewart 	u_int cw = CCV(ccv, snd_cwnd);
171a9696510SRandall Stewart 	u_int incr = CCV(ccv, t_maxseg);
172a9696510SRandall Stewart 	uint16_t abc_val;
173a9696510SRandall Stewart 
174a9696510SRandall Stewart 	cubicd->flags |= CUBICFLAG_IN_SLOWSTART;
175a9696510SRandall Stewart 	if (ccv->flags & CCF_USE_LOCAL_ABC)
176a9696510SRandall Stewart 		abc_val = ccv->labc;
177a9696510SRandall Stewart 	else
178a9696510SRandall Stewart 		abc_val = V_tcp_abc_l_var;
179a9696510SRandall Stewart 	if ((ccv->flags & CCF_HYSTART_ALLOWED) &&
180a9696510SRandall Stewart 	    (cubicd->flags & CUBICFLAG_HYSTART_ENABLED) &&
181a9696510SRandall Stewart 	    ((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) == 0)) {
182a9696510SRandall Stewart 		/*
183a9696510SRandall Stewart 		 * Hystart is allowed and still enabled and we are not yet
184a9696510SRandall Stewart 		 * in CSS. Lets check to see if we can make a decision on
185a9696510SRandall Stewart 		 * if we need to go into CSS.
186a9696510SRandall Stewart 		 */
187a9696510SRandall Stewart 		if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) &&
188a9696510SRandall Stewart 		    (cubicd->css_current_round_minrtt != 0xffffffff) &&
189a9696510SRandall Stewart 		    (cubicd->css_lastround_minrtt != 0xffffffff)) {
190a9696510SRandall Stewart 			uint32_t rtt_thresh;
191a9696510SRandall Stewart 
192a9696510SRandall Stewart 			/* Clamp (minrtt_thresh, lastround/8, maxrtt_thresh) */
193a9696510SRandall Stewart 			rtt_thresh = (cubicd->css_lastround_minrtt >> 3);
194a9696510SRandall Stewart 			if (rtt_thresh < hystart_minrtt_thresh)
195a9696510SRandall Stewart 				rtt_thresh = hystart_minrtt_thresh;
196a9696510SRandall Stewart 			if (rtt_thresh > hystart_maxrtt_thresh)
197a9696510SRandall Stewart 				rtt_thresh = hystart_maxrtt_thresh;
198a9696510SRandall Stewart 			cubic_log_hystart_event(ccv, cubicd, 1, rtt_thresh);
199a9696510SRandall Stewart 
200a9696510SRandall Stewart 			if (cubicd->css_current_round_minrtt >= (cubicd->css_lastround_minrtt + rtt_thresh)) {
201a9696510SRandall Stewart 				/* Enter CSS */
202a9696510SRandall Stewart 				cubicd->flags |= CUBICFLAG_HYSTART_IN_CSS;
203a9696510SRandall Stewart 				cubicd->css_fas_at_css_entry = cubicd->css_lowrtt_fas;
204a9696510SRandall Stewart 				/*
205a9696510SRandall Stewart 				 * The draft (v4) calls for us to set baseline to css_current_round_min
206a9696510SRandall Stewart 				 * but that can cause an oscillation. We probably shoudl be using
207a9696510SRandall Stewart 				 * css_lastround_minrtt, but the authors insist that will cause
208a9696510SRandall Stewart 				 * issues on exiting early. We will leave the draft version for now
209a9696510SRandall Stewart 				 * but I suspect this is incorrect.
210a9696510SRandall Stewart 				 */
211a9696510SRandall Stewart 				cubicd->css_baseline_minrtt = cubicd->css_current_round_minrtt;
212a9696510SRandall Stewart 				cubicd->css_entered_at_round = cubicd->css_current_round;
213a9696510SRandall Stewart 				cubic_log_hystart_event(ccv, cubicd, 2, rtt_thresh);
214a9696510SRandall Stewart 			}
215a9696510SRandall Stewart 		}
216a9696510SRandall Stewart 	}
217a9696510SRandall Stewart 	if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max))
218a9696510SRandall Stewart 		incr = min(ccv->bytes_this_ack,
219a9696510SRandall Stewart 			   ccv->nsegs * abc_val *
220a9696510SRandall Stewart 			   CCV(ccv, t_maxseg));
221a9696510SRandall Stewart 	else
222a9696510SRandall Stewart 		incr = min(ccv->bytes_this_ack, CCV(ccv, t_maxseg));
223a9696510SRandall Stewart 
224a9696510SRandall Stewart 	/* Only if Hystart is enabled will the flag get set */
225a9696510SRandall Stewart 	if (cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) {
226a9696510SRandall Stewart 		incr /= hystart_css_growth_div;
227a9696510SRandall Stewart 		cubic_log_hystart_event(ccv, cubicd, 3, incr);
228a9696510SRandall Stewart 	}
229a9696510SRandall Stewart 	/* ABC is on by default, so incr equals 0 frequently. */
230a9696510SRandall Stewart 	if (incr > 0)
231a9696510SRandall Stewart 		CCV(ccv, snd_cwnd) = min((cw + incr),
232a9696510SRandall Stewart 					 TCP_MAXWIN << CCV(ccv, snd_scale));
233a9696510SRandall Stewart }
234a9696510SRandall Stewart 
235a9696510SRandall Stewart static void
236f74352fbSRichard Scheffenegger cubic_ack_received(struct cc_var *ccv, ccsignal_t type)
23767fef78bSLawrence Stewart {
23867fef78bSLawrence Stewart 	struct cubic *cubic_data;
239eb5bfdd0SRichard Scheffenegger 	unsigned long W_est, W_cubic;
240eb5bfdd0SRichard Scheffenegger 	int usecs_since_epoch;
24167fef78bSLawrence Stewart 
24267fef78bSLawrence Stewart 	cubic_data = ccv->cc_data;
24367fef78bSLawrence Stewart 	cubic_record_rtt(ccv);
24467fef78bSLawrence Stewart 
24567fef78bSLawrence Stewart 	/*
246ad7a0eb1SRichard Scheffenegger 	 * For a regular ACK and we're not in cong/fast recovery and
247ad7a0eb1SRichard Scheffenegger 	 * we're cwnd limited, always recalculate cwnd.
24867fef78bSLawrence Stewart 	 */
24967fef78bSLawrence Stewart 	if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
250ad7a0eb1SRichard Scheffenegger 	    (ccv->flags & CCF_CWND_LIMITED)) {
25167fef78bSLawrence Stewart 		 /* Use the logic in NewReno ack_received() for slow start. */
25267fef78bSLawrence Stewart 		if (CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||
253a3aa6f65SCheng Cui 		    cubic_data->min_rtt_usecs == TCPTV_SRTTBASE) {
254a9696510SRandall Stewart 			cubic_does_slow_start(ccv, cubic_data);
2556907bbaeSRichard Scheffenegger 		} else {
256a9696510SRandall Stewart 			if (cubic_data->flags & CUBICFLAG_HYSTART_IN_CSS) {
257a9696510SRandall Stewart 				/*
258a9696510SRandall Stewart 				 * We have slipped into CA with
259a9696510SRandall Stewart 				 * CSS active. Deactivate all.
260a9696510SRandall Stewart 				 */
261a9696510SRandall Stewart 				/* Turn off the CSS flag */
262a9696510SRandall Stewart 				cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
263a9696510SRandall Stewart 				/* Disable use of CSS in the future except long idle  */
264a9696510SRandall Stewart 				cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
265a9696510SRandall Stewart 				cubic_log_hystart_event(ccv, cubic_data, 11, CCV(ccv, snd_ssthresh));
266a9696510SRandall Stewart 			}
26737674273SRichard Scheffenegger 			if ((cubic_data->flags & CUBICFLAG_RTO_EVENT) &&
26837674273SRichard Scheffenegger 			    (cubic_data->flags & CUBICFLAG_IN_SLOWSTART)) {
26937674273SRichard Scheffenegger 				/* RFC8312 Section 4.7 */
27037674273SRichard Scheffenegger 				cubic_data->flags &= ~(CUBICFLAG_RTO_EVENT |
27137674273SRichard Scheffenegger 						       CUBICFLAG_IN_SLOWSTART);
272eb5bfdd0SRichard Scheffenegger 				cubic_data->W_max = CCV(ccv, snd_cwnd);
273*038699a8SRichard Scheffenegger 				cubic_data->t_epoch = ticks;
27437674273SRichard Scheffenegger 				cubic_data->K = 0;
27537674273SRichard Scheffenegger 			} else if (cubic_data->flags & (CUBICFLAG_IN_SLOWSTART |
2762bb6dfabSRichard Scheffenegger 						 CUBICFLAG_IN_APPLIMIT)) {
2772bb6dfabSRichard Scheffenegger 				cubic_data->flags &= ~(CUBICFLAG_IN_SLOWSTART |
2782bb6dfabSRichard Scheffenegger 						       CUBICFLAG_IN_APPLIMIT);
279eb5bfdd0SRichard Scheffenegger 				cubic_data->t_epoch = ticks;
280eb5bfdd0SRichard Scheffenegger 				cubic_data->K = cubic_k(cubic_data->W_max /
2812bb6dfabSRichard Scheffenegger 							CCV(ccv, t_maxseg));
2822bb6dfabSRichard Scheffenegger 			}
283eb5bfdd0SRichard Scheffenegger 			usecs_since_epoch = (ticks - cubic_data->t_epoch) * tick;
284eb5bfdd0SRichard Scheffenegger 			if (usecs_since_epoch < 0) {
285c968c769SMichael Tuexen 				/*
286eb5bfdd0SRichard Scheffenegger 				 * dragging t_epoch along
287c968c769SMichael Tuexen 				 */
288eb5bfdd0SRichard Scheffenegger 				usecs_since_epoch = INT_MAX;
289eb5bfdd0SRichard Scheffenegger 				cubic_data->t_epoch = ticks - INT_MAX;
290c968c769SMichael Tuexen 			}
29167fef78bSLawrence Stewart 			/*
29267fef78bSLawrence Stewart 			 * The mean RTT is used to best reflect the equations in
29367fef78bSLawrence Stewart 			 * the I-D. Using min_rtt in the tf_cwnd calculation
294eb5bfdd0SRichard Scheffenegger 			 * causes W_est to grow much faster than it should if the
29567fef78bSLawrence Stewart 			 * RTT is dominated by network buffering rather than
296a4641f4eSPedro F. Giffuni 			 * propagation delay.
29767fef78bSLawrence Stewart 			 */
298eb5bfdd0SRichard Scheffenegger 			W_est = tf_cwnd(usecs_since_epoch, cubic_data->mean_rtt_usecs,
299eb5bfdd0SRichard Scheffenegger 				       cubic_data->W_max, CCV(ccv, t_maxseg));
30067fef78bSLawrence Stewart 
301eb5bfdd0SRichard Scheffenegger 			W_cubic = cubic_cwnd(usecs_since_epoch +
302a3aa6f65SCheng Cui 					     cubic_data->mean_rtt_usecs,
303eb5bfdd0SRichard Scheffenegger 					     cubic_data->W_max,
304a3aa6f65SCheng Cui 					     CCV(ccv, t_maxseg),
305a3aa6f65SCheng Cui 					     cubic_data->K);
30667fef78bSLawrence Stewart 
30767fef78bSLawrence Stewart 			ccv->flags &= ~CCF_ABC_SENTAWND;
30867fef78bSLawrence Stewart 
309eb5bfdd0SRichard Scheffenegger 			if (W_cubic < W_est) {
31067fef78bSLawrence Stewart 				/*
31167fef78bSLawrence Stewart 				 * TCP-friendly region, follow tf
31267fef78bSLawrence Stewart 				 * cwnd growth.
31367fef78bSLawrence Stewart 				 */
314eb5bfdd0SRichard Scheffenegger 				if (CCV(ccv, snd_cwnd) < W_est)
315eb5bfdd0SRichard Scheffenegger 					CCV(ccv, snd_cwnd) = ulmin(W_est, INT_MAX);
316eb5bfdd0SRichard Scheffenegger 			} else if (CCV(ccv, snd_cwnd) < W_cubic) {
31767fef78bSLawrence Stewart 				/*
31867fef78bSLawrence Stewart 				 * Concave or convex region, follow CUBIC
31967fef78bSLawrence Stewart 				 * cwnd growth.
320cce999b3SRichard Scheffenegger 				 * Only update snd_cwnd, if it doesn't shrink.
32167fef78bSLawrence Stewart 				 */
322eb5bfdd0SRichard Scheffenegger 				CCV(ccv, snd_cwnd) = ulmin(W_cubic, INT_MAX);
32367fef78bSLawrence Stewart 			}
32467fef78bSLawrence Stewart 
32567fef78bSLawrence Stewart 			/*
32667fef78bSLawrence Stewart 			 * If we're not in slow start and we're probing for a
32767fef78bSLawrence Stewart 			 * new cwnd limit at the start of a connection
32867fef78bSLawrence Stewart 			 * (happens when hostcache has a relevant entry),
32967fef78bSLawrence Stewart 			 * keep updating our current estimate of the
330eb5bfdd0SRichard Scheffenegger 			 * W_max.
33167fef78bSLawrence Stewart 			 */
3326907bbaeSRichard Scheffenegger 			if (((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) &&
333eb5bfdd0SRichard Scheffenegger 			    cubic_data->W_max < CCV(ccv, snd_cwnd)) {
334eb5bfdd0SRichard Scheffenegger 				cubic_data->W_max = CCV(ccv, snd_cwnd);
335eb5bfdd0SRichard Scheffenegger 				cubic_data->K = cubic_k(cubic_data->W_max /
336b0c1a13eSMichael Tuexen 				    CCV(ccv, t_maxseg));
33767fef78bSLawrence Stewart 			}
33867fef78bSLawrence Stewart 		}
3392fda0a6fSRichard Scheffenegger 	} else if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
3402fda0a6fSRichard Scheffenegger 	    !(ccv->flags & CCF_CWND_LIMITED)) {
3412fda0a6fSRichard Scheffenegger 		cubic_data->flags |= CUBICFLAG_IN_APPLIMIT;
34267fef78bSLawrence Stewart 	}
3437d87664aSMichael Tuexen }
34467fef78bSLawrence Stewart 
34535cd141bSMichael Tuexen /*
346ea6d0de2SRichard Scheffenegger  * This is a CUBIC specific implementation of after_idle.
34735cd141bSMichael Tuexen  *   - Reset cwnd by calling New Reno implementation of after_idle.
348eb5bfdd0SRichard Scheffenegger  *   - Reset t_epoch.
34935cd141bSMichael Tuexen  */
35035cd141bSMichael Tuexen static void
35135cd141bSMichael Tuexen cubic_after_idle(struct cc_var *ccv)
35235cd141bSMichael Tuexen {
35335cd141bSMichael Tuexen 	struct cubic *cubic_data;
35435cd141bSMichael Tuexen 
35535cd141bSMichael Tuexen 	cubic_data = ccv->cc_data;
35635cd141bSMichael Tuexen 
357eb5bfdd0SRichard Scheffenegger 	cubic_data->W_max = ulmax(cubic_data->W_max, CCV(ccv, snd_cwnd));
358eb5bfdd0SRichard Scheffenegger 	cubic_data->K = cubic_k(cubic_data->W_max / CCV(ccv, t_maxseg));
359a9696510SRandall Stewart 	if ((cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) == 0) {
360a9696510SRandall Stewart 		/*
361a9696510SRandall Stewart 		 * Re-enable hystart if we have been idle.
362a9696510SRandall Stewart 		 */
363a9696510SRandall Stewart 		cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
364a9696510SRandall Stewart 		cubic_data->flags |= CUBICFLAG_HYSTART_ENABLED;
365a9696510SRandall Stewart 		cubic_log_hystart_event(ccv, cubic_data, 12, CCV(ccv, snd_ssthresh));
366a9696510SRandall Stewart 	}
367b8d60729SRandall Stewart 	newreno_cc_after_idle(ccv);
368eb5bfdd0SRichard Scheffenegger 	cubic_data->t_epoch = ticks;
36935cd141bSMichael Tuexen }
37035cd141bSMichael Tuexen 
37167fef78bSLawrence Stewart static void
37267fef78bSLawrence Stewart cubic_cb_destroy(struct cc_var *ccv)
37367fef78bSLawrence Stewart {
374b8d60729SRandall Stewart 	free(ccv->cc_data, M_CC_MEM);
375b8d60729SRandall Stewart }
376b8d60729SRandall Stewart 
377b8d60729SRandall Stewart static size_t
378b8d60729SRandall Stewart cubic_data_sz(void)
379b8d60729SRandall Stewart {
380b8d60729SRandall Stewart 	return (sizeof(struct cubic));
38167fef78bSLawrence Stewart }
38267fef78bSLawrence Stewart 
38367fef78bSLawrence Stewart static int
384b8d60729SRandall Stewart cubic_cb_init(struct cc_var *ccv, void *ptr)
38567fef78bSLawrence Stewart {
38667fef78bSLawrence Stewart 	struct cubic *cubic_data;
38767fef78bSLawrence Stewart 
3889eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(ccv->ccvc.tcp));
389b8d60729SRandall Stewart 	if (ptr == NULL) {
390b8d60729SRandall Stewart 		cubic_data = malloc(sizeof(struct cubic), M_CC_MEM, M_NOWAIT|M_ZERO);
39167fef78bSLawrence Stewart 		if (cubic_data == NULL)
39267fef78bSLawrence Stewart 			return (ENOMEM);
393b8d60729SRandall Stewart 	} else
394b8d60729SRandall Stewart 		cubic_data = ptr;
39567fef78bSLawrence Stewart 
39667fef78bSLawrence Stewart 	/* Init some key variables with sensible defaults. */
397eb5bfdd0SRichard Scheffenegger 	cubic_data->t_epoch = ticks;
398a3aa6f65SCheng Cui 	cubic_data->min_rtt_usecs = TCPTV_SRTTBASE;
399a3aa6f65SCheng Cui 	cubic_data->mean_rtt_usecs = 1;
40067fef78bSLawrence Stewart 
40167fef78bSLawrence Stewart 	ccv->cc_data = cubic_data;
402a9696510SRandall Stewart 	cubic_data->flags = CUBICFLAG_HYSTART_ENABLED;
403a9696510SRandall Stewart 	/* At init set both to infinity */
404a9696510SRandall Stewart 	cubic_data->css_lastround_minrtt = 0xffffffff;
405a9696510SRandall Stewart 	cubic_data->css_current_round_minrtt = 0xffffffff;
406a9696510SRandall Stewart 	cubic_data->css_current_round = 0;
407a9696510SRandall Stewart 	cubic_data->css_baseline_minrtt = 0xffffffff;
408a9696510SRandall Stewart 	cubic_data->css_rttsample_count = 0;
409a9696510SRandall Stewart 	cubic_data->css_entered_at_round = 0;
410a9696510SRandall Stewart 	cubic_data->css_fas_at_css_entry = 0;
411a9696510SRandall Stewart 	cubic_data->css_lowrtt_fas = 0;
412a9696510SRandall Stewart 	cubic_data->css_last_fas = 0;
41367fef78bSLawrence Stewart 
41467fef78bSLawrence Stewart 	return (0);
41567fef78bSLawrence Stewart }
41667fef78bSLawrence Stewart 
41767fef78bSLawrence Stewart /*
41867fef78bSLawrence Stewart  * Perform any necessary tasks before we enter congestion recovery.
41967fef78bSLawrence Stewart  */
42067fef78bSLawrence Stewart static void
421f74352fbSRichard Scheffenegger cubic_cong_signal(struct cc_var *ccv, ccsignal_t type)
42267fef78bSLawrence Stewart {
42367fef78bSLawrence Stewart 	struct cubic *cubic_data;
42432a6df57SRichard Scheffenegger 	uint32_t mss, pipe;
42567fef78bSLawrence Stewart 
42667fef78bSLawrence Stewart 	cubic_data = ccv->cc_data;
4271adab814SRichard Scheffenegger 	mss = tcp_fixed_maxseg(ccv->ccvc.tcp);
42867fef78bSLawrence Stewart 
42967fef78bSLawrence Stewart 	switch (type) {
43067fef78bSLawrence Stewart 	case CC_NDUPACK:
431a9696510SRandall Stewart 		if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) {
432a9696510SRandall Stewart 			/* Make sure the flags are all off we had a loss */
433a9696510SRandall Stewart 			cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
434a9696510SRandall Stewart 			cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
435a9696510SRandall Stewart 			cubic_log_hystart_event(ccv, cubic_data, 10, CCV(ccv, snd_ssthresh));
436a9696510SRandall Stewart 		}
43767fef78bSLawrence Stewart 		if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
43867fef78bSLawrence Stewart 			if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
43937674273SRichard Scheffenegger 				cubic_ssthresh_update(ccv, mss);
4406907bbaeSRichard Scheffenegger 				cubic_data->flags |= CUBICFLAG_CONG_EVENT;
441eb5bfdd0SRichard Scheffenegger 				cubic_data->t_epoch = ticks;
442eb5bfdd0SRichard Scheffenegger 				cubic_data->K = cubic_k(cubic_data->W_max / mss);
44367fef78bSLawrence Stewart 			}
44467fef78bSLawrence Stewart 			ENTER_RECOVERY(CCV(ccv, t_flags));
44567fef78bSLawrence Stewart 		}
44667fef78bSLawrence Stewart 		break;
44767fef78bSLawrence Stewart 
44867fef78bSLawrence Stewart 	case CC_ECN:
449a9696510SRandall Stewart 		if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) {
450a9696510SRandall Stewart 			/* Make sure the flags are all off we had a loss */
451a9696510SRandall Stewart 			cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
452a9696510SRandall Stewart 			cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
453a9696510SRandall Stewart 			cubic_log_hystart_event(ccv, cubic_data, 9, CCV(ccv, snd_ssthresh));
454a9696510SRandall Stewart 		}
45567fef78bSLawrence Stewart 		if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
45637674273SRichard Scheffenegger 			cubic_ssthresh_update(ccv, mss);
4576907bbaeSRichard Scheffenegger 			cubic_data->flags |= CUBICFLAG_CONG_EVENT;
458eb5bfdd0SRichard Scheffenegger 			cubic_data->t_epoch = ticks;
459eb5bfdd0SRichard Scheffenegger 			cubic_data->K = cubic_k(cubic_data->W_max / mss);
46067fef78bSLawrence Stewart 			CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
46167fef78bSLawrence Stewart 			ENTER_CONGRECOVERY(CCV(ccv, t_flags));
46267fef78bSLawrence Stewart 		}
46367fef78bSLawrence Stewart 		break;
46467fef78bSLawrence Stewart 
46567fef78bSLawrence Stewart 	case CC_RTO:
46637674273SRichard Scheffenegger 		/* RFC8312 Section 4.7 */
46737674273SRichard Scheffenegger 		if (CCV(ccv, t_rxtshift) == 1) {
468eb5bfdd0SRichard Scheffenegger 			/*
469eb5bfdd0SRichard Scheffenegger 			 * Remember the state only for the first RTO event. This
470eb5bfdd0SRichard Scheffenegger 			 * will help us restore the state to the values seen
471eb5bfdd0SRichard Scheffenegger 			 * at the most recent congestion avoidance stage before
472eb5bfdd0SRichard Scheffenegger 			 * the current RTO event.
473eb5bfdd0SRichard Scheffenegger 			 */
474eb5bfdd0SRichard Scheffenegger 			cubic_data->undo_t_epoch = cubic_data->t_epoch;
475eb5bfdd0SRichard Scheffenegger 			cubic_data->undo_cwnd_epoch = cubic_data->cwnd_epoch;
476eb5bfdd0SRichard Scheffenegger 			cubic_data->undo_W_est = cubic_data->W_est;
477eb5bfdd0SRichard Scheffenegger 			cubic_data->undo_cwnd_prior = cubic_data->cwnd_prior;
478eb5bfdd0SRichard Scheffenegger 			cubic_data->undo_W_max = cubic_data->W_max;
479eb5bfdd0SRichard Scheffenegger 			cubic_data->undo_K = cubic_data->K;
48032a6df57SRichard Scheffenegger 			if (V_tcp_do_newsack) {
48132a6df57SRichard Scheffenegger 				pipe = tcp_compute_pipe(ccv->ccvc.tcp);
48232a6df57SRichard Scheffenegger 			} else {
483fcea1cc9SRichard Scheffenegger 				pipe = CCV(ccv, snd_max) -
48432a6df57SRichard Scheffenegger 					CCV(ccv, snd_fack) +
48532a6df57SRichard Scheffenegger 					CCV(ccv, sackhint.sack_bytes_rexmit);
48632a6df57SRichard Scheffenegger 			}
48732a6df57SRichard Scheffenegger 			CCV(ccv, snd_ssthresh) = max(2,
48832a6df57SRichard Scheffenegger 				(((uint64_t)min(CCV(ccv, snd_wnd), pipe) *
48932a6df57SRichard Scheffenegger 				CUBIC_BETA) >> CUBIC_SHIFT) / mss) * mss;
4901edbb54fSEd Maste 		}
49137674273SRichard Scheffenegger 		cubic_data->flags |= CUBICFLAG_CONG_EVENT | CUBICFLAG_RTO_EVENT;
492eb5bfdd0SRichard Scheffenegger 		cubic_data->undo_W_max = cubic_data->W_max;
493eb5bfdd0SRichard Scheffenegger 		cubic_data->num_cong_events++;
49437674273SRichard Scheffenegger 		CCV(ccv, snd_cwnd) = mss;
49537674273SRichard Scheffenegger 		break;
49637674273SRichard Scheffenegger 
49737674273SRichard Scheffenegger 	case CC_RTO_ERR:
49837674273SRichard Scheffenegger 		cubic_data->flags &= ~(CUBICFLAG_CONG_EVENT | CUBICFLAG_RTO_EVENT);
499eb5bfdd0SRichard Scheffenegger 		cubic_data->num_cong_events--;
500eb5bfdd0SRichard Scheffenegger 		cubic_data->K = cubic_data->undo_K;
501eb5bfdd0SRichard Scheffenegger 		cubic_data->cwnd_prior = cubic_data->undo_cwnd_prior;
502eb5bfdd0SRichard Scheffenegger 		cubic_data->W_max = cubic_data->undo_W_max;
503eb5bfdd0SRichard Scheffenegger 		cubic_data->W_est = cubic_data->undo_W_est;
504eb5bfdd0SRichard Scheffenegger 		cubic_data->cwnd_epoch = cubic_data->undo_cwnd_epoch;
505eb5bfdd0SRichard Scheffenegger 		cubic_data->t_epoch = cubic_data->undo_t_epoch;
50667fef78bSLawrence Stewart 		break;
507f74352fbSRichard Scheffenegger 	default:
508f74352fbSRichard Scheffenegger 		break;
50967fef78bSLawrence Stewart 	}
51067fef78bSLawrence Stewart }
51167fef78bSLawrence Stewart 
51267fef78bSLawrence Stewart static void
51367fef78bSLawrence Stewart cubic_conn_init(struct cc_var *ccv)
51467fef78bSLawrence Stewart {
51567fef78bSLawrence Stewart 	struct cubic *cubic_data;
51667fef78bSLawrence Stewart 
51767fef78bSLawrence Stewart 	cubic_data = ccv->cc_data;
51867fef78bSLawrence Stewart 
51967fef78bSLawrence Stewart 	/*
520eb5bfdd0SRichard Scheffenegger 	 * Ensure we have a sane initial value for W_max recorded. Without
52167fef78bSLawrence Stewart 	 * this here bad things happen when entries from the TCP hostcache
52267fef78bSLawrence Stewart 	 * get used.
52367fef78bSLawrence Stewart 	 */
524eb5bfdd0SRichard Scheffenegger 	cubic_data->W_max = CCV(ccv, snd_cwnd);
52567fef78bSLawrence Stewart }
52667fef78bSLawrence Stewart 
52767fef78bSLawrence Stewart static int
52867fef78bSLawrence Stewart cubic_mod_init(void)
52967fef78bSLawrence Stewart {
53067fef78bSLawrence Stewart 	return (0);
53167fef78bSLawrence Stewart }
53267fef78bSLawrence Stewart 
53367fef78bSLawrence Stewart /*
53467fef78bSLawrence Stewart  * Perform any necessary tasks before we exit congestion recovery.
53567fef78bSLawrence Stewart  */
53667fef78bSLawrence Stewart static void
53767fef78bSLawrence Stewart cubic_post_recovery(struct cc_var *ccv)
53867fef78bSLawrence Stewart {
53967fef78bSLawrence Stewart 	struct cubic *cubic_data;
540f81bc34eSHiren Panchasara 	int pipe;
54167fef78bSLawrence Stewart 
54267fef78bSLawrence Stewart 	cubic_data = ccv->cc_data;
543f81bc34eSHiren Panchasara 	pipe = 0;
54467fef78bSLawrence Stewart 
54567fef78bSLawrence Stewart 	if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
54667fef78bSLawrence Stewart 		/*
54767fef78bSLawrence Stewart 		 * If inflight data is less than ssthresh, set cwnd
54867fef78bSLawrence Stewart 		 * conservatively to avoid a burst of data, as suggested in
54967fef78bSLawrence Stewart 		 * the NewReno RFC. Otherwise, use the CUBIC method.
55067fef78bSLawrence Stewart 		 *
55167fef78bSLawrence Stewart 		 * XXXLAS: Find a way to do this without needing curack
55267fef78bSLawrence Stewart 		 */
553d1de2b05SRichard Scheffenegger 		if (V_tcp_do_newsack)
554f81bc34eSHiren Panchasara 			pipe = tcp_compute_pipe(ccv->ccvc.tcp);
555f81bc34eSHiren Panchasara 		else
556f81bc34eSHiren Panchasara 			pipe = CCV(ccv, snd_max) - ccv->curack;
557f81bc34eSHiren Panchasara 
558f81bc34eSHiren Panchasara 		if (pipe < CCV(ccv, snd_ssthresh))
5595cc11a89SMichael Tuexen 			/*
5605cc11a89SMichael Tuexen 			 * Ensure that cwnd does not collapse to 1 MSS under
5615cc11a89SMichael Tuexen 			 * adverse conditions. Implements RFC6582
5625cc11a89SMichael Tuexen 			 */
5635cc11a89SMichael Tuexen 			CCV(ccv, snd_cwnd) = max(pipe, CCV(ccv, t_maxseg)) +
5645cc11a89SMichael Tuexen 			    CCV(ccv, t_maxseg);
56567fef78bSLawrence Stewart 		else
566eb5bfdd0SRichard Scheffenegger 			/* Update cwnd based on beta and adjusted W_max. */
567eb5bfdd0SRichard Scheffenegger 			CCV(ccv, snd_cwnd) = max(((uint64_t)cubic_data->W_max *
56814558b99SRichard Scheffenegger 			    CUBIC_BETA) >> CUBIC_SHIFT,
56914558b99SRichard Scheffenegger 			    2 * CCV(ccv, t_maxseg));
57067fef78bSLawrence Stewart 	}
57167fef78bSLawrence Stewart 
57267fef78bSLawrence Stewart 	/* Calculate the average RTT between congestion epochs. */
57347f44cddSLawrence Stewart 	if (cubic_data->epoch_ack_count > 0 &&
574a3aa6f65SCheng Cui 	    cubic_data->sum_rtt_usecs >= cubic_data->epoch_ack_count) {
575a3aa6f65SCheng Cui 		cubic_data->mean_rtt_usecs = (int)(cubic_data->sum_rtt_usecs /
57667fef78bSLawrence Stewart 		    cubic_data->epoch_ack_count);
57747f44cddSLawrence Stewart 	}
57867fef78bSLawrence Stewart 
57967fef78bSLawrence Stewart 	cubic_data->epoch_ack_count = 0;
580a3aa6f65SCheng Cui 	cubic_data->sum_rtt_usecs = 0;
58167fef78bSLawrence Stewart }
58267fef78bSLawrence Stewart 
58367fef78bSLawrence Stewart /*
58467fef78bSLawrence Stewart  * Record the min RTT and sum samples for the epoch average RTT calculation.
58567fef78bSLawrence Stewart  */
58667fef78bSLawrence Stewart static void
58767fef78bSLawrence Stewart cubic_record_rtt(struct cc_var *ccv)
58867fef78bSLawrence Stewart {
58967fef78bSLawrence Stewart 	struct cubic *cubic_data;
590a3aa6f65SCheng Cui 	uint32_t t_srtt_usecs;
59167fef78bSLawrence Stewart 
59267fef78bSLawrence Stewart 	/* Ignore srtt until a min number of samples have been taken. */
59367fef78bSLawrence Stewart 	if (CCV(ccv, t_rttupdated) >= CUBIC_MIN_RTT_SAMPLES) {
59467fef78bSLawrence Stewart 		cubic_data = ccv->cc_data;
595a3aa6f65SCheng Cui 		t_srtt_usecs = tcp_get_srtt(ccv->ccvc.tcp,
596a3aa6f65SCheng Cui 					    TCP_TMR_GRANULARITY_USEC);
59767fef78bSLawrence Stewart 		/*
59867fef78bSLawrence Stewart 		 * Record the current SRTT as our minrtt if it's the smallest
59967fef78bSLawrence Stewart 		 * we've seen or minrtt is currently equal to its initialised
60067fef78bSLawrence Stewart 		 * value.
60167fef78bSLawrence Stewart 		 *
60267fef78bSLawrence Stewart 		 * XXXLAS: Should there be some hysteresis for minrtt?
60367fef78bSLawrence Stewart 		 */
604a3aa6f65SCheng Cui 		if ((t_srtt_usecs < cubic_data->min_rtt_usecs ||
605a3aa6f65SCheng Cui 		    cubic_data->min_rtt_usecs == TCPTV_SRTTBASE)) {
606a3aa6f65SCheng Cui 			/* A minimal rtt is a single unshifted tick of a ticks
607a3aa6f65SCheng Cui 			 * timer. */
608a3aa6f65SCheng Cui 			cubic_data->min_rtt_usecs = max(tick >> TCP_RTT_SHIFT,
609a3aa6f65SCheng Cui 							t_srtt_usecs);
61067fef78bSLawrence Stewart 
61147f44cddSLawrence Stewart 			/*
61247f44cddSLawrence Stewart 			 * If the connection is within its first congestion
613a3aa6f65SCheng Cui 			 * epoch, ensure we prime mean_rtt_usecs with a
61447f44cddSLawrence Stewart 			 * reasonable value until the epoch average RTT is
61547f44cddSLawrence Stewart 			 * calculated in cubic_post_recovery().
61647f44cddSLawrence Stewart 			 */
617a3aa6f65SCheng Cui 			if (cubic_data->min_rtt_usecs >
618a3aa6f65SCheng Cui 			    cubic_data->mean_rtt_usecs)
619a3aa6f65SCheng Cui 				cubic_data->mean_rtt_usecs =
620a3aa6f65SCheng Cui 				    cubic_data->min_rtt_usecs;
62147f44cddSLawrence Stewart 		}
62247f44cddSLawrence Stewart 
62367fef78bSLawrence Stewart 		/* Sum samples for epoch average RTT calculation. */
624a3aa6f65SCheng Cui 		cubic_data->sum_rtt_usecs += t_srtt_usecs;
62567fef78bSLawrence Stewart 		cubic_data->epoch_ack_count++;
62667fef78bSLawrence Stewart 	}
62767fef78bSLawrence Stewart }
62867fef78bSLawrence Stewart 
62967fef78bSLawrence Stewart /*
63067fef78bSLawrence Stewart  * Update the ssthresh in the event of congestion.
63167fef78bSLawrence Stewart  */
63267fef78bSLawrence Stewart static void
63337674273SRichard Scheffenegger cubic_ssthresh_update(struct cc_var *ccv, uint32_t maxseg)
63467fef78bSLawrence Stewart {
63567fef78bSLawrence Stewart 	struct cubic *cubic_data;
63614558b99SRichard Scheffenegger 	uint32_t ssthresh;
637a459638fSRichard Scheffenegger 	uint32_t cwnd;
63867fef78bSLawrence Stewart 
63967fef78bSLawrence Stewart 	cubic_data = ccv->cc_data;
640a459638fSRichard Scheffenegger 	cwnd = CCV(ccv, snd_cwnd);
641a459638fSRichard Scheffenegger 
642a459638fSRichard Scheffenegger 	/* Fast convergence heuristic. */
643eb5bfdd0SRichard Scheffenegger 	if (cwnd < cubic_data->W_max) {
644a459638fSRichard Scheffenegger 		cwnd = ((uint64_t)cwnd * CUBIC_FC_FACTOR) >> CUBIC_SHIFT;
645a459638fSRichard Scheffenegger 	}
646eb5bfdd0SRichard Scheffenegger 	cubic_data->undo_W_max = cubic_data->W_max;
647eb5bfdd0SRichard Scheffenegger 	cubic_data->W_max = cwnd;
64867fef78bSLawrence Stewart 
64967fef78bSLawrence Stewart 	/*
650a459638fSRichard Scheffenegger 	 * On the first congestion event, set ssthresh to cwnd * 0.5
651eb5bfdd0SRichard Scheffenegger 	 * and reduce W_max to cwnd * beta. This aligns the cubic concave
652a459638fSRichard Scheffenegger 	 * region appropriately. On subsequent congestion events, set
653a459638fSRichard Scheffenegger 	 * ssthresh to cwnd * beta.
65467fef78bSLawrence Stewart 	 */
655a459638fSRichard Scheffenegger 	if ((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) {
656a459638fSRichard Scheffenegger 		ssthresh = cwnd >> 1;
657eb5bfdd0SRichard Scheffenegger 		cubic_data->W_max = ((uint64_t)cwnd *
6583ac12506SJonathan T. Looney 		    CUBIC_BETA) >> CUBIC_SHIFT;
659a459638fSRichard Scheffenegger 	} else {
660a459638fSRichard Scheffenegger 		ssthresh = ((uint64_t)cwnd *
661a459638fSRichard Scheffenegger 		    CUBIC_BETA) >> CUBIC_SHIFT;
662a459638fSRichard Scheffenegger 	}
66337674273SRichard Scheffenegger 	CCV(ccv, snd_ssthresh) = max(ssthresh, 2 * maxseg);
66467fef78bSLawrence Stewart }
66567fef78bSLawrence Stewart 
666a9696510SRandall Stewart static void
667a9696510SRandall Stewart cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt, uint32_t rxtcnt, uint32_t fas)
668a9696510SRandall Stewart {
669a9696510SRandall Stewart 	struct cubic *cubicd;
670a9696510SRandall Stewart 
671a9696510SRandall Stewart 	cubicd = ccv->cc_data;
672a9696510SRandall Stewart 	if (rxtcnt > 1) {
673a9696510SRandall Stewart 		/*
674a9696510SRandall Stewart 		 * Only look at RTT's that are non-ambiguous.
675a9696510SRandall Stewart 		 */
676a9696510SRandall Stewart 		return;
677a9696510SRandall Stewart 	}
678a9696510SRandall Stewart 	cubicd->css_rttsample_count++;
679a9696510SRandall Stewart 	cubicd->css_last_fas = fas;
680a9696510SRandall Stewart 	if (cubicd->css_current_round_minrtt > usec_rtt) {
681a9696510SRandall Stewart 		cubicd->css_current_round_minrtt = usec_rtt;
682a9696510SRandall Stewart 		cubicd->css_lowrtt_fas = cubicd->css_last_fas;
683a9696510SRandall Stewart 	}
684a9696510SRandall Stewart 	if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) &&
685a9696510SRandall Stewart 	    (cubicd->css_current_round_minrtt != 0xffffffff) &&
686e88412d8SRandall Stewart 	    (cubicd->css_current_round_minrtt < cubicd->css_baseline_minrtt) &&
687a9696510SRandall Stewart 	    (cubicd->css_lastround_minrtt != 0xffffffff)) {
688a9696510SRandall Stewart 		/*
689a9696510SRandall Stewart 		 * We were in CSS and the RTT is now less, we
690a9696510SRandall Stewart 		 * entered CSS erroneously.
691a9696510SRandall Stewart 		 */
692a9696510SRandall Stewart 		cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
693a9696510SRandall Stewart 		cubic_log_hystart_event(ccv, cubicd, 8, cubicd->css_baseline_minrtt);
694a9696510SRandall Stewart 		cubicd->css_baseline_minrtt = 0xffffffff;
695a9696510SRandall Stewart 	}
696a9696510SRandall Stewart 	if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED)
697a9696510SRandall Stewart 		cubic_log_hystart_event(ccv, cubicd, 5, usec_rtt);
698a9696510SRandall Stewart }
699a9696510SRandall Stewart 
700a9696510SRandall Stewart static void
701a9696510SRandall Stewart cubic_newround(struct cc_var *ccv, uint32_t round_cnt)
702a9696510SRandall Stewart {
703a9696510SRandall Stewart 	struct cubic *cubicd;
704a9696510SRandall Stewart 
705a9696510SRandall Stewart 	cubicd = ccv->cc_data;
706a9696510SRandall Stewart 	/* We have entered a new round */
707a9696510SRandall Stewart 	cubicd->css_lastround_minrtt = cubicd->css_current_round_minrtt;
708a9696510SRandall Stewart 	cubicd->css_current_round_minrtt = 0xffffffff;
709a9696510SRandall Stewart 	cubicd->css_rttsample_count = 0;
710a9696510SRandall Stewart 	cubicd->css_current_round = round_cnt;
711a9696510SRandall Stewart 	if ((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) &&
712a9696510SRandall Stewart 	    ((round_cnt - cubicd->css_entered_at_round) >= hystart_css_rounds)) {
713a9696510SRandall Stewart 		/* Enter CA */
714a9696510SRandall Stewart 		if (ccv->flags & CCF_HYSTART_CAN_SH_CWND) {
715a9696510SRandall Stewart 			/*
716a9696510SRandall Stewart 			 * We engage more than snd_ssthresh, engage
717a9696510SRandall Stewart 			 * the brakes!! Though we will stay in SS to
718a9696510SRandall Stewart 			 * creep back up again, so lets leave CSS active
719a9696510SRandall Stewart 			 * and give us hystart_css_rounds more rounds.
720a9696510SRandall Stewart 			 */
721a9696510SRandall Stewart 			if (ccv->flags & CCF_HYSTART_CONS_SSTH) {
722a9696510SRandall Stewart 				CCV(ccv, snd_ssthresh) = ((cubicd->css_lowrtt_fas + cubicd->css_fas_at_css_entry) / 2);
723a9696510SRandall Stewart 			} else {
724a9696510SRandall Stewart 				CCV(ccv, snd_ssthresh) = cubicd->css_lowrtt_fas;
725a9696510SRandall Stewart 			}
726a9696510SRandall Stewart 			CCV(ccv, snd_cwnd) = cubicd->css_fas_at_css_entry;
727a9696510SRandall Stewart 			cubicd->css_entered_at_round = round_cnt;
728a9696510SRandall Stewart 		} else {
729a9696510SRandall Stewart 			CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
730a9696510SRandall Stewart 			/* Turn off the CSS flag */
731a9696510SRandall Stewart 			cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
732a9696510SRandall Stewart 			/* Disable use of CSS in the future except long idle  */
733a9696510SRandall Stewart 			cubicd->flags &= ~CUBICFLAG_HYSTART_ENABLED;
734a9696510SRandall Stewart 		}
735a9696510SRandall Stewart 		cubic_log_hystart_event(ccv, cubicd, 6, CCV(ccv, snd_ssthresh));
736a9696510SRandall Stewart 	}
737a9696510SRandall Stewart 	if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED)
738a9696510SRandall Stewart 		cubic_log_hystart_event(ccv, cubicd, 4, round_cnt);
739a9696510SRandall Stewart }
740a9696510SRandall Stewart 
74167fef78bSLawrence Stewart DECLARE_CC_MODULE(cubic, &cubic_cc_algo);
742b8d60729SRandall Stewart MODULE_VERSION(cubic, 2);
743