xref: /freebsd/sys/netinet/cc/cc_cubic.c (revision a3aa6f65290482cedf4aeda1d0875ca6433c7f04)
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/cdefs.h>
5167fef78bSLawrence Stewart __FBSDID("$FreeBSD$");
5267fef78bSLawrence Stewart 
5367fef78bSLawrence Stewart #include <sys/param.h>
5467fef78bSLawrence Stewart #include <sys/kernel.h>
55c968c769SMichael Tuexen #include <sys/limits.h>
5667fef78bSLawrence Stewart #include <sys/malloc.h>
5767fef78bSLawrence Stewart #include <sys/module.h>
5867fef78bSLawrence Stewart #include <sys/socket.h>
5967fef78bSLawrence Stewart #include <sys/socketvar.h>
6067fef78bSLawrence Stewart #include <sys/sysctl.h>
6167fef78bSLawrence Stewart #include <sys/systm.h>
6267fef78bSLawrence Stewart 
6367fef78bSLawrence Stewart #include <net/vnet.h>
6467fef78bSLawrence Stewart 
65b8d60729SRandall Stewart #include <net/route.h>
66b8d60729SRandall Stewart #include <net/route/nhop.h>
67b8d60729SRandall Stewart 
68b8d60729SRandall Stewart #include <netinet/in_pcb.h>
692de3e790SGleb Smirnoff #include <netinet/tcp.h>
7067fef78bSLawrence Stewart #include <netinet/tcp_seq.h>
7167fef78bSLawrence Stewart #include <netinet/tcp_timer.h>
7267fef78bSLawrence Stewart #include <netinet/tcp_var.h>
73a9696510SRandall Stewart #include <netinet/tcp_log_buf.h>
74a9696510SRandall Stewart #include <netinet/tcp_hpts.h>
754644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
7667fef78bSLawrence Stewart #include <netinet/cc/cc_cubic.h>
7767fef78bSLawrence Stewart #include <netinet/cc/cc_module.h>
7867fef78bSLawrence Stewart 
7967fef78bSLawrence Stewart static void	cubic_ack_received(struct cc_var *ccv, uint16_t type);
8067fef78bSLawrence Stewart static void	cubic_cb_destroy(struct cc_var *ccv);
81b8d60729SRandall Stewart static int	cubic_cb_init(struct cc_var *ccv, void *ptr);
8267fef78bSLawrence Stewart static void	cubic_cong_signal(struct cc_var *ccv, uint32_t type);
8367fef78bSLawrence Stewart static void	cubic_conn_init(struct cc_var *ccv);
8467fef78bSLawrence Stewart static int	cubic_mod_init(void);
8567fef78bSLawrence Stewart static void	cubic_post_recovery(struct cc_var *ccv);
8667fef78bSLawrence Stewart static void	cubic_record_rtt(struct cc_var *ccv);
8737674273SRichard Scheffenegger static void	cubic_ssthresh_update(struct cc_var *ccv, uint32_t maxseg);
8835cd141bSMichael Tuexen static void	cubic_after_idle(struct cc_var *ccv);
89b8d60729SRandall Stewart static size_t	cubic_data_sz(void);
90a9696510SRandall Stewart static void	cubic_newround(struct cc_var *ccv, uint32_t round_cnt);
91a9696510SRandall Stewart static void	cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt,
92a9696510SRandall Stewart        uint32_t rxtcnt, uint32_t fas);
9367fef78bSLawrence Stewart 
9467fef78bSLawrence Stewart struct cc_algo cubic_cc_algo = {
9567fef78bSLawrence Stewart 	.name = "cubic",
9667fef78bSLawrence Stewart 	.ack_received = cubic_ack_received,
9767fef78bSLawrence Stewart 	.cb_destroy = cubic_cb_destroy,
9867fef78bSLawrence Stewart 	.cb_init = cubic_cb_init,
9967fef78bSLawrence Stewart 	.cong_signal = cubic_cong_signal,
10067fef78bSLawrence Stewart 	.conn_init = cubic_conn_init,
10167fef78bSLawrence Stewart 	.mod_init = cubic_mod_init,
10267fef78bSLawrence Stewart 	.post_recovery = cubic_post_recovery,
10335cd141bSMichael Tuexen 	.after_idle = cubic_after_idle,
104a9696510SRandall Stewart 	.cc_data_sz = cubic_data_sz,
105a9696510SRandall Stewart 	.rttsample = cubic_rttsample,
106a9696510SRandall Stewart 	.newround = cubic_newround
10767fef78bSLawrence Stewart };
10867fef78bSLawrence Stewart 
10967fef78bSLawrence Stewart static void
110a9696510SRandall Stewart cubic_log_hystart_event(struct cc_var *ccv, struct cubic *cubicd, uint8_t mod, uint32_t flex1)
111a9696510SRandall Stewart {
112a9696510SRandall Stewart 	/*
113a9696510SRandall Stewart 	 * Types of logs (mod value)
114a9696510SRandall Stewart 	 * 1 - rtt_thresh in flex1, checking to see if RTT is to great.
115a9696510SRandall Stewart 	 * 2 - rtt is too great, rtt_thresh in flex1.
116a9696510SRandall Stewart 	 * 3 - CSS is active incr in flex1
117a9696510SRandall Stewart 	 * 4 - A new round is beginning flex1 is round count
118a9696510SRandall Stewart 	 * 5 - A new RTT measurement flex1 is the new measurement.
119a9696510SRandall Stewart 	 * 6 - We enter CA ssthresh is also in flex1.
120a9696510SRandall Stewart 	 * 7 - Socket option to change hystart executed opt.val in flex1.
121a9696510SRandall Stewart 	 * 8 - Back out of CSS into SS, flex1 is the css_baseline_minrtt
122a9696510SRandall Stewart 	 * 9 - We enter CA, via an ECN mark.
123a9696510SRandall Stewart 	 * 10 - We enter CA, via a loss.
124a9696510SRandall Stewart 	 * 11 - We have slipped out of SS into CA via cwnd growth.
125a9696510SRandall Stewart 	 * 12 - After idle has re-enabled hystart++
126a9696510SRandall Stewart 	 */
127a9696510SRandall Stewart 	struct tcpcb *tp;
128a9696510SRandall Stewart 
129a9696510SRandall Stewart 	if (hystart_bblogs == 0)
130a9696510SRandall Stewart 		return;
131a9696510SRandall Stewart 	tp = ccv->ccvc.tcp;
13269c7c811SRandall Stewart 	if (tcp_bblogging_on(tp)) {
133a9696510SRandall Stewart 		union tcp_log_stackspecific log;
134a9696510SRandall Stewart 		struct timeval tv;
135a9696510SRandall Stewart 
136a9696510SRandall Stewart 		memset(&log, 0, sizeof(log));
137a9696510SRandall Stewart 		log.u_bbr.flex1 = flex1;
138a9696510SRandall Stewart 		log.u_bbr.flex2 = cubicd->css_current_round_minrtt;
139a9696510SRandall Stewart 		log.u_bbr.flex3 = cubicd->css_lastround_minrtt;
140a9696510SRandall Stewart 		log.u_bbr.flex4 = cubicd->css_rttsample_count;
141a9696510SRandall Stewart 		log.u_bbr.flex5 = cubicd->css_entered_at_round;
142a9696510SRandall Stewart 		log.u_bbr.flex6 = cubicd->css_baseline_minrtt;
143a9696510SRandall Stewart 		/* We only need bottom 16 bits of flags */
144a9696510SRandall Stewart 		log.u_bbr.flex7 = cubicd->flags & 0x0000ffff;
145a9696510SRandall Stewart 		log.u_bbr.flex8 = mod;
146a9696510SRandall Stewart 		log.u_bbr.epoch = cubicd->css_current_round;
147a9696510SRandall Stewart 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
148a9696510SRandall Stewart 		log.u_bbr.lt_epoch = cubicd->css_fas_at_css_entry;
149a9696510SRandall Stewart 		log.u_bbr.pkts_out = cubicd->css_last_fas;
150a9696510SRandall Stewart 		log.u_bbr.delivered = cubicd->css_lowrtt_fas;
151a9696510SRandall Stewart 		log.u_bbr.pkt_epoch = ccv->flags;
152a9696510SRandall Stewart 		TCP_LOG_EVENTP(tp, NULL,
1539eb0e832SGleb Smirnoff 		    &tptosocket(tp)->so_rcv,
1549eb0e832SGleb Smirnoff 		    &tptosocket(tp)->so_snd,
155a9696510SRandall Stewart 		    TCP_HYSTART, 0,
156a9696510SRandall Stewart 		    0, &log, false, &tv);
157a9696510SRandall Stewart 	}
158a9696510SRandall Stewart }
159a9696510SRandall Stewart 
160a9696510SRandall Stewart static void
161a9696510SRandall Stewart cubic_does_slow_start(struct cc_var *ccv, struct cubic *cubicd)
162a9696510SRandall Stewart {
163a9696510SRandall Stewart 	/*
164a9696510SRandall Stewart 	 * In slow-start with ABC enabled and no RTO in sight?
165a9696510SRandall Stewart 	 * (Must not use abc_l_var > 1 if slow starting after
166a9696510SRandall Stewart 	 * an RTO. On RTO, snd_nxt = snd_una, so the
167a9696510SRandall Stewart 	 * snd_nxt == snd_max check is sufficient to
168a9696510SRandall Stewart 	 * handle this).
169a9696510SRandall Stewart 	 *
170a9696510SRandall Stewart 	 * XXXLAS: Find a way to signal SS after RTO that
171a9696510SRandall Stewart 	 * doesn't rely on tcpcb vars.
172a9696510SRandall Stewart 	 */
173a9696510SRandall Stewart 	u_int cw = CCV(ccv, snd_cwnd);
174a9696510SRandall Stewart 	u_int incr = CCV(ccv, t_maxseg);
175a9696510SRandall Stewart 	uint16_t abc_val;
176a9696510SRandall Stewart 
177a9696510SRandall Stewart 	cubicd->flags |= CUBICFLAG_IN_SLOWSTART;
178a9696510SRandall Stewart 	if (ccv->flags & CCF_USE_LOCAL_ABC)
179a9696510SRandall Stewart 		abc_val = ccv->labc;
180a9696510SRandall Stewart 	else
181a9696510SRandall Stewart 		abc_val = V_tcp_abc_l_var;
182a9696510SRandall Stewart 	if ((ccv->flags & CCF_HYSTART_ALLOWED) &&
183a9696510SRandall Stewart 	    (cubicd->flags & CUBICFLAG_HYSTART_ENABLED) &&
184a9696510SRandall Stewart 	    ((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) == 0)) {
185a9696510SRandall Stewart 		/*
186a9696510SRandall Stewart 		 * Hystart is allowed and still enabled and we are not yet
187a9696510SRandall Stewart 		 * in CSS. Lets check to see if we can make a decision on
188a9696510SRandall Stewart 		 * if we need to go into CSS.
189a9696510SRandall Stewart 		 */
190a9696510SRandall Stewart 		if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) &&
191a9696510SRandall Stewart 		    (cubicd->css_current_round_minrtt != 0xffffffff) &&
192a9696510SRandall Stewart 		    (cubicd->css_lastround_minrtt != 0xffffffff)) {
193a9696510SRandall Stewart 			uint32_t rtt_thresh;
194a9696510SRandall Stewart 
195a9696510SRandall Stewart 			/* Clamp (minrtt_thresh, lastround/8, maxrtt_thresh) */
196a9696510SRandall Stewart 			rtt_thresh = (cubicd->css_lastround_minrtt >> 3);
197a9696510SRandall Stewart 			if (rtt_thresh < hystart_minrtt_thresh)
198a9696510SRandall Stewart 				rtt_thresh = hystart_minrtt_thresh;
199a9696510SRandall Stewart 			if (rtt_thresh > hystart_maxrtt_thresh)
200a9696510SRandall Stewart 				rtt_thresh = hystart_maxrtt_thresh;
201a9696510SRandall Stewart 			cubic_log_hystart_event(ccv, cubicd, 1, rtt_thresh);
202a9696510SRandall Stewart 
203a9696510SRandall Stewart 			if (cubicd->css_current_round_minrtt >= (cubicd->css_lastround_minrtt + rtt_thresh)) {
204a9696510SRandall Stewart 				/* Enter CSS */
205a9696510SRandall Stewart 				cubicd->flags |= CUBICFLAG_HYSTART_IN_CSS;
206a9696510SRandall Stewart 				cubicd->css_fas_at_css_entry = cubicd->css_lowrtt_fas;
207a9696510SRandall Stewart 				/*
208a9696510SRandall Stewart 				 * The draft (v4) calls for us to set baseline to css_current_round_min
209a9696510SRandall Stewart 				 * but that can cause an oscillation. We probably shoudl be using
210a9696510SRandall Stewart 				 * css_lastround_minrtt, but the authors insist that will cause
211a9696510SRandall Stewart 				 * issues on exiting early. We will leave the draft version for now
212a9696510SRandall Stewart 				 * but I suspect this is incorrect.
213a9696510SRandall Stewart 				 */
214a9696510SRandall Stewart 				cubicd->css_baseline_minrtt = cubicd->css_current_round_minrtt;
215a9696510SRandall Stewart 				cubicd->css_entered_at_round = cubicd->css_current_round;
216a9696510SRandall Stewart 				cubic_log_hystart_event(ccv, cubicd, 2, rtt_thresh);
217a9696510SRandall Stewart 			}
218a9696510SRandall Stewart 		}
219a9696510SRandall Stewart 	}
220a9696510SRandall Stewart 	if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max))
221a9696510SRandall Stewart 		incr = min(ccv->bytes_this_ack,
222a9696510SRandall Stewart 			   ccv->nsegs * abc_val *
223a9696510SRandall Stewart 			   CCV(ccv, t_maxseg));
224a9696510SRandall Stewart 	else
225a9696510SRandall Stewart 		incr = min(ccv->bytes_this_ack, CCV(ccv, t_maxseg));
226a9696510SRandall Stewart 
227a9696510SRandall Stewart 	/* Only if Hystart is enabled will the flag get set */
228a9696510SRandall Stewart 	if (cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) {
229a9696510SRandall Stewart 		incr /= hystart_css_growth_div;
230a9696510SRandall Stewart 		cubic_log_hystart_event(ccv, cubicd, 3, incr);
231a9696510SRandall Stewart 	}
232a9696510SRandall Stewart 	/* ABC is on by default, so incr equals 0 frequently. */
233a9696510SRandall Stewart 	if (incr > 0)
234a9696510SRandall Stewart 		CCV(ccv, snd_cwnd) = min((cw + incr),
235a9696510SRandall Stewart 					 TCP_MAXWIN << CCV(ccv, snd_scale));
236a9696510SRandall Stewart }
237a9696510SRandall Stewart 
238a9696510SRandall Stewart static void
23967fef78bSLawrence Stewart cubic_ack_received(struct cc_var *ccv, uint16_t type)
24067fef78bSLawrence Stewart {
24167fef78bSLawrence Stewart 	struct cubic *cubic_data;
24267fef78bSLawrence Stewart 	unsigned long w_tf, w_cubic_next;
243*a3aa6f65SCheng Cui 	int usecs_since_cong;
24467fef78bSLawrence Stewart 
24567fef78bSLawrence Stewart 	cubic_data = ccv->cc_data;
24667fef78bSLawrence Stewart 	cubic_record_rtt(ccv);
24767fef78bSLawrence Stewart 
24867fef78bSLawrence Stewart 	/*
249ad7a0eb1SRichard Scheffenegger 	 * For a regular ACK and we're not in cong/fast recovery and
250ad7a0eb1SRichard Scheffenegger 	 * we're cwnd limited, always recalculate cwnd.
25167fef78bSLawrence Stewart 	 */
25267fef78bSLawrence Stewart 	if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
253ad7a0eb1SRichard Scheffenegger 	    (ccv->flags & CCF_CWND_LIMITED)) {
25467fef78bSLawrence Stewart 		 /* Use the logic in NewReno ack_received() for slow start. */
25567fef78bSLawrence Stewart 		if (CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||
256*a3aa6f65SCheng Cui 		    cubic_data->min_rtt_usecs == TCPTV_SRTTBASE) {
257a9696510SRandall Stewart 			cubic_does_slow_start(ccv, cubic_data);
2586907bbaeSRichard Scheffenegger 		} else {
259a9696510SRandall Stewart 			if (cubic_data->flags & CUBICFLAG_HYSTART_IN_CSS) {
260a9696510SRandall Stewart 				/*
261a9696510SRandall Stewart 				 * We have slipped into CA with
262a9696510SRandall Stewart 				 * CSS active. Deactivate all.
263a9696510SRandall Stewart 				 */
264a9696510SRandall Stewart 				/* Turn off the CSS flag */
265a9696510SRandall Stewart 				cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
266a9696510SRandall Stewart 				/* Disable use of CSS in the future except long idle  */
267a9696510SRandall Stewart 				cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
268a9696510SRandall Stewart 				cubic_log_hystart_event(ccv, cubic_data, 11, CCV(ccv, snd_ssthresh));
269a9696510SRandall Stewart 			}
27037674273SRichard Scheffenegger 			if ((cubic_data->flags & CUBICFLAG_RTO_EVENT) &&
27137674273SRichard Scheffenegger 			    (cubic_data->flags & CUBICFLAG_IN_SLOWSTART)) {
27237674273SRichard Scheffenegger 				/* RFC8312 Section 4.7 */
27337674273SRichard Scheffenegger 				cubic_data->flags &= ~(CUBICFLAG_RTO_EVENT |
27437674273SRichard Scheffenegger 						       CUBICFLAG_IN_SLOWSTART);
27537674273SRichard Scheffenegger 				cubic_data->max_cwnd = CCV(ccv, snd_cwnd);
27637674273SRichard Scheffenegger 				cubic_data->K = 0;
27737674273SRichard Scheffenegger 			} else if (cubic_data->flags & (CUBICFLAG_IN_SLOWSTART |
2782bb6dfabSRichard Scheffenegger 						 CUBICFLAG_IN_APPLIMIT)) {
2792bb6dfabSRichard Scheffenegger 				cubic_data->flags &= ~(CUBICFLAG_IN_SLOWSTART |
2802bb6dfabSRichard Scheffenegger 						       CUBICFLAG_IN_APPLIMIT);
2812bb6dfabSRichard Scheffenegger 				cubic_data->t_last_cong = ticks;
2822bb6dfabSRichard Scheffenegger 				cubic_data->K = cubic_k(cubic_data->max_cwnd /
2832bb6dfabSRichard Scheffenegger 							CCV(ccv, t_maxseg));
2842bb6dfabSRichard Scheffenegger 			}
285*a3aa6f65SCheng Cui 			usecs_since_cong = (ticks - cubic_data->t_last_cong) * tick;
286*a3aa6f65SCheng Cui 			if (usecs_since_cong < 0) {
287c968c769SMichael Tuexen 				/*
288c968c769SMichael Tuexen 				 * dragging t_last_cong along
289c968c769SMichael Tuexen 				 */
290*a3aa6f65SCheng Cui 				usecs_since_cong = INT_MAX;
291c968c769SMichael Tuexen 				cubic_data->t_last_cong = ticks - INT_MAX;
292c968c769SMichael Tuexen 			}
29367fef78bSLawrence Stewart 			/*
29467fef78bSLawrence Stewart 			 * The mean RTT is used to best reflect the equations in
29567fef78bSLawrence Stewart 			 * the I-D. Using min_rtt in the tf_cwnd calculation
29667fef78bSLawrence Stewart 			 * causes w_tf to grow much faster than it should if the
29767fef78bSLawrence Stewart 			 * RTT is dominated by network buffering rather than
298a4641f4eSPedro F. Giffuni 			 * propagation delay.
29967fef78bSLawrence Stewart 			 */
300*a3aa6f65SCheng Cui 			w_tf = tf_cwnd(usecs_since_cong, cubic_data->mean_rtt_usecs,
301*a3aa6f65SCheng Cui 				       cubic_data->max_cwnd, CCV(ccv, t_maxseg));
30267fef78bSLawrence Stewart 
303*a3aa6f65SCheng Cui 			w_cubic_next = cubic_cwnd(usecs_since_cong +
304*a3aa6f65SCheng Cui 						  cubic_data->mean_rtt_usecs,
305*a3aa6f65SCheng Cui 						  cubic_data->max_cwnd,
306*a3aa6f65SCheng Cui 						  CCV(ccv, t_maxseg),
307*a3aa6f65SCheng Cui 						  cubic_data->K);
30867fef78bSLawrence Stewart 
30967fef78bSLawrence Stewart 			ccv->flags &= ~CCF_ABC_SENTAWND;
31067fef78bSLawrence Stewart 
311c968c769SMichael Tuexen 			if (w_cubic_next < w_tf) {
31267fef78bSLawrence Stewart 				/*
31367fef78bSLawrence Stewart 				 * TCP-friendly region, follow tf
31467fef78bSLawrence Stewart 				 * cwnd growth.
31567fef78bSLawrence Stewart 				 */
316c968c769SMichael Tuexen 				if (CCV(ccv, snd_cwnd) < w_tf)
317c968c769SMichael Tuexen 					CCV(ccv, snd_cwnd) = ulmin(w_tf, INT_MAX);
318cce999b3SRichard Scheffenegger 			} else if (CCV(ccv, snd_cwnd) < w_cubic_next) {
31967fef78bSLawrence Stewart 				/*
32067fef78bSLawrence Stewart 				 * Concave or convex region, follow CUBIC
32167fef78bSLawrence Stewart 				 * cwnd growth.
322cce999b3SRichard Scheffenegger 				 * Only update snd_cwnd, if it doesn't shrink.
32367fef78bSLawrence Stewart 				 */
324c968c769SMichael Tuexen 				CCV(ccv, snd_cwnd) = ulmin(w_cubic_next,
325c968c769SMichael Tuexen 				    INT_MAX);
32667fef78bSLawrence Stewart 			}
32767fef78bSLawrence Stewart 
32867fef78bSLawrence Stewart 			/*
32967fef78bSLawrence Stewart 			 * If we're not in slow start and we're probing for a
33067fef78bSLawrence Stewart 			 * new cwnd limit at the start of a connection
33167fef78bSLawrence Stewart 			 * (happens when hostcache has a relevant entry),
33267fef78bSLawrence Stewart 			 * keep updating our current estimate of the
33367fef78bSLawrence Stewart 			 * max_cwnd.
33467fef78bSLawrence Stewart 			 */
3356907bbaeSRichard Scheffenegger 			if (((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) &&
3367d87664aSMichael Tuexen 			    cubic_data->max_cwnd < CCV(ccv, snd_cwnd)) {
33767fef78bSLawrence Stewart 				cubic_data->max_cwnd = CCV(ccv, snd_cwnd);
338b0c1a13eSMichael Tuexen 				cubic_data->K = cubic_k(cubic_data->max_cwnd /
339b0c1a13eSMichael Tuexen 				    CCV(ccv, t_maxseg));
34067fef78bSLawrence Stewart 			}
34167fef78bSLawrence Stewart 		}
3422fda0a6fSRichard Scheffenegger 	} else if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
3432fda0a6fSRichard Scheffenegger 	    !(ccv->flags & CCF_CWND_LIMITED)) {
3442fda0a6fSRichard Scheffenegger 		cubic_data->flags |= CUBICFLAG_IN_APPLIMIT;
34567fef78bSLawrence Stewart 	}
3467d87664aSMichael Tuexen }
34767fef78bSLawrence Stewart 
34835cd141bSMichael Tuexen /*
349ea6d0de2SRichard Scheffenegger  * This is a CUBIC specific implementation of after_idle.
35035cd141bSMichael Tuexen  *   - Reset cwnd by calling New Reno implementation of after_idle.
35135cd141bSMichael Tuexen  *   - Reset t_last_cong.
35235cd141bSMichael Tuexen  */
35335cd141bSMichael Tuexen static void
35435cd141bSMichael Tuexen cubic_after_idle(struct cc_var *ccv)
35535cd141bSMichael Tuexen {
35635cd141bSMichael Tuexen 	struct cubic *cubic_data;
35735cd141bSMichael Tuexen 
35835cd141bSMichael Tuexen 	cubic_data = ccv->cc_data;
35935cd141bSMichael Tuexen 
360b0c1a13eSMichael Tuexen 	cubic_data->max_cwnd = ulmax(cubic_data->max_cwnd, CCV(ccv, snd_cwnd));
361b0c1a13eSMichael Tuexen 	cubic_data->K = cubic_k(cubic_data->max_cwnd / CCV(ccv, t_maxseg));
362a9696510SRandall Stewart 	if ((cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) == 0) {
363a9696510SRandall Stewart 		/*
364a9696510SRandall Stewart 		 * Re-enable hystart if we have been idle.
365a9696510SRandall Stewart 		 */
366a9696510SRandall Stewart 		cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
367a9696510SRandall Stewart 		cubic_data->flags |= CUBICFLAG_HYSTART_ENABLED;
368a9696510SRandall Stewart 		cubic_log_hystart_event(ccv, cubic_data, 12, CCV(ccv, snd_ssthresh));
369a9696510SRandall Stewart 	}
370b8d60729SRandall Stewart 	newreno_cc_after_idle(ccv);
37135cd141bSMichael Tuexen 	cubic_data->t_last_cong = ticks;
37235cd141bSMichael Tuexen }
37335cd141bSMichael Tuexen 
37467fef78bSLawrence Stewart static void
37567fef78bSLawrence Stewart cubic_cb_destroy(struct cc_var *ccv)
37667fef78bSLawrence Stewart {
377b8d60729SRandall Stewart 	free(ccv->cc_data, M_CC_MEM);
378b8d60729SRandall Stewart }
379b8d60729SRandall Stewart 
380b8d60729SRandall Stewart static size_t
381b8d60729SRandall Stewart cubic_data_sz(void)
382b8d60729SRandall Stewart {
383b8d60729SRandall Stewart 	return (sizeof(struct cubic));
38467fef78bSLawrence Stewart }
38567fef78bSLawrence Stewart 
38667fef78bSLawrence Stewart static int
387b8d60729SRandall Stewart cubic_cb_init(struct cc_var *ccv, void *ptr)
38867fef78bSLawrence Stewart {
38967fef78bSLawrence Stewart 	struct cubic *cubic_data;
39067fef78bSLawrence Stewart 
3919eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(ccv->ccvc.tcp));
392b8d60729SRandall Stewart 	if (ptr == NULL) {
393b8d60729SRandall Stewart 		cubic_data = malloc(sizeof(struct cubic), M_CC_MEM, M_NOWAIT|M_ZERO);
39467fef78bSLawrence Stewart 		if (cubic_data == NULL)
39567fef78bSLawrence Stewart 			return (ENOMEM);
396b8d60729SRandall Stewart 	} else
397b8d60729SRandall Stewart 		cubic_data = ptr;
39867fef78bSLawrence Stewart 
39967fef78bSLawrence Stewart 	/* Init some key variables with sensible defaults. */
40067fef78bSLawrence Stewart 	cubic_data->t_last_cong = ticks;
401*a3aa6f65SCheng Cui 	cubic_data->min_rtt_usecs = TCPTV_SRTTBASE;
402*a3aa6f65SCheng Cui 	cubic_data->mean_rtt_usecs = 1;
40367fef78bSLawrence Stewart 
40467fef78bSLawrence Stewart 	ccv->cc_data = cubic_data;
405a9696510SRandall Stewart 	cubic_data->flags = CUBICFLAG_HYSTART_ENABLED;
406a9696510SRandall Stewart 	/* At init set both to infinity */
407a9696510SRandall Stewart 	cubic_data->css_lastround_minrtt = 0xffffffff;
408a9696510SRandall Stewart 	cubic_data->css_current_round_minrtt = 0xffffffff;
409a9696510SRandall Stewart 	cubic_data->css_current_round = 0;
410a9696510SRandall Stewart 	cubic_data->css_baseline_minrtt = 0xffffffff;
411a9696510SRandall Stewart 	cubic_data->css_rttsample_count = 0;
412a9696510SRandall Stewart 	cubic_data->css_entered_at_round = 0;
413a9696510SRandall Stewart 	cubic_data->css_fas_at_css_entry = 0;
414a9696510SRandall Stewart 	cubic_data->css_lowrtt_fas = 0;
415a9696510SRandall Stewart 	cubic_data->css_last_fas = 0;
41667fef78bSLawrence Stewart 
41767fef78bSLawrence Stewart 	return (0);
41867fef78bSLawrence Stewart }
41967fef78bSLawrence Stewart 
42067fef78bSLawrence Stewart /*
42167fef78bSLawrence Stewart  * Perform any necessary tasks before we enter congestion recovery.
42267fef78bSLawrence Stewart  */
42367fef78bSLawrence Stewart static void
42467fef78bSLawrence Stewart cubic_cong_signal(struct cc_var *ccv, uint32_t type)
42567fef78bSLawrence Stewart {
42667fef78bSLawrence Stewart 	struct cubic *cubic_data;
42739a12f01SRichard Scheffenegger 	u_int mss;
42867fef78bSLawrence Stewart 
42967fef78bSLawrence Stewart 	cubic_data = ccv->cc_data;
43039a12f01SRichard Scheffenegger 	mss = tcp_maxseg(ccv->ccvc.tcp);
43167fef78bSLawrence Stewart 
43267fef78bSLawrence Stewart 	switch (type) {
43367fef78bSLawrence Stewart 	case CC_NDUPACK:
434a9696510SRandall Stewart 		if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) {
435a9696510SRandall Stewart 			/* Make sure the flags are all off we had a loss */
436a9696510SRandall Stewart 			cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
437a9696510SRandall Stewart 			cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
438a9696510SRandall Stewart 			cubic_log_hystart_event(ccv, cubic_data, 10, CCV(ccv, snd_ssthresh));
439a9696510SRandall Stewart 		}
44067fef78bSLawrence Stewart 		if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
44167fef78bSLawrence Stewart 			if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
44237674273SRichard Scheffenegger 				cubic_ssthresh_update(ccv, mss);
4436907bbaeSRichard Scheffenegger 				cubic_data->flags |= CUBICFLAG_CONG_EVENT;
444a459638fSRichard Scheffenegger 				cubic_data->t_last_cong = ticks;
44537674273SRichard Scheffenegger 				cubic_data->K = cubic_k(cubic_data->max_cwnd / mss);
44667fef78bSLawrence Stewart 			}
44767fef78bSLawrence Stewart 			ENTER_RECOVERY(CCV(ccv, t_flags));
44867fef78bSLawrence Stewart 		}
44967fef78bSLawrence Stewart 		break;
45067fef78bSLawrence Stewart 
45167fef78bSLawrence Stewart 	case CC_ECN:
452a9696510SRandall Stewart 		if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) {
453a9696510SRandall Stewart 			/* Make sure the flags are all off we had a loss */
454a9696510SRandall Stewart 			cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
455a9696510SRandall Stewart 			cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
456a9696510SRandall Stewart 			cubic_log_hystart_event(ccv, cubic_data, 9, CCV(ccv, snd_ssthresh));
457a9696510SRandall Stewart 		}
45867fef78bSLawrence Stewart 		if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
45937674273SRichard Scheffenegger 			cubic_ssthresh_update(ccv, mss);
4606907bbaeSRichard Scheffenegger 			cubic_data->flags |= CUBICFLAG_CONG_EVENT;
46167fef78bSLawrence Stewart 			cubic_data->t_last_cong = ticks;
46237674273SRichard Scheffenegger 			cubic_data->K = cubic_k(cubic_data->max_cwnd / mss);
46367fef78bSLawrence Stewart 			CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
46467fef78bSLawrence Stewart 			ENTER_CONGRECOVERY(CCV(ccv, t_flags));
46567fef78bSLawrence Stewart 		}
46667fef78bSLawrence Stewart 		break;
46767fef78bSLawrence Stewart 
46867fef78bSLawrence Stewart 	case CC_RTO:
46937674273SRichard Scheffenegger 		/* RFC8312 Section 4.7 */
47037674273SRichard Scheffenegger 		if (CCV(ccv, t_rxtshift) == 1) {
47137674273SRichard Scheffenegger 			cubic_data->t_last_cong_prev = cubic_data->t_last_cong;
47237674273SRichard Scheffenegger 			cubic_data->prev_max_cwnd_cp = cubic_data->prev_max_cwnd;
4731edbb54fSEd Maste 		}
47437674273SRichard Scheffenegger 		cubic_data->flags |= CUBICFLAG_CONG_EVENT | CUBICFLAG_RTO_EVENT;
47537674273SRichard Scheffenegger 		cubic_data->prev_max_cwnd = cubic_data->max_cwnd;
47637674273SRichard Scheffenegger 		CCV(ccv, snd_ssthresh) = ((uint64_t)CCV(ccv, snd_cwnd) *
47737674273SRichard Scheffenegger 					  CUBIC_BETA) >> CUBIC_SHIFT;
47837674273SRichard Scheffenegger 		CCV(ccv, snd_cwnd) = mss;
47937674273SRichard Scheffenegger 		break;
48037674273SRichard Scheffenegger 
48137674273SRichard Scheffenegger 	case CC_RTO_ERR:
48237674273SRichard Scheffenegger 		cubic_data->flags &= ~(CUBICFLAG_CONG_EVENT | CUBICFLAG_RTO_EVENT);
48337674273SRichard Scheffenegger 		cubic_data->max_cwnd = cubic_data->prev_max_cwnd;
48437674273SRichard Scheffenegger 		cubic_data->prev_max_cwnd = cubic_data->prev_max_cwnd_cp;
48537674273SRichard Scheffenegger 		cubic_data->t_last_cong = cubic_data->t_last_cong_prev;
48637674273SRichard Scheffenegger 		cubic_data->K = cubic_k(cubic_data->max_cwnd / mss);
48767fef78bSLawrence Stewart 		break;
48867fef78bSLawrence Stewart 	}
48967fef78bSLawrence Stewart }
49067fef78bSLawrence Stewart 
49167fef78bSLawrence Stewart static void
49267fef78bSLawrence Stewart cubic_conn_init(struct cc_var *ccv)
49367fef78bSLawrence Stewart {
49467fef78bSLawrence Stewart 	struct cubic *cubic_data;
49567fef78bSLawrence Stewart 
49667fef78bSLawrence Stewart 	cubic_data = ccv->cc_data;
49767fef78bSLawrence Stewart 
49867fef78bSLawrence Stewart 	/*
49967fef78bSLawrence Stewart 	 * Ensure we have a sane initial value for max_cwnd recorded. Without
50067fef78bSLawrence Stewart 	 * this here bad things happen when entries from the TCP hostcache
50167fef78bSLawrence Stewart 	 * get used.
50267fef78bSLawrence Stewart 	 */
50367fef78bSLawrence Stewart 	cubic_data->max_cwnd = CCV(ccv, snd_cwnd);
50467fef78bSLawrence Stewart }
50567fef78bSLawrence Stewart 
50667fef78bSLawrence Stewart static int
50767fef78bSLawrence Stewart cubic_mod_init(void)
50867fef78bSLawrence Stewart {
50967fef78bSLawrence Stewart 	return (0);
51067fef78bSLawrence Stewart }
51167fef78bSLawrence Stewart 
51267fef78bSLawrence Stewart /*
51367fef78bSLawrence Stewart  * Perform any necessary tasks before we exit congestion recovery.
51467fef78bSLawrence Stewart  */
51567fef78bSLawrence Stewart static void
51667fef78bSLawrence Stewart cubic_post_recovery(struct cc_var *ccv)
51767fef78bSLawrence Stewart {
51867fef78bSLawrence Stewart 	struct cubic *cubic_data;
519f81bc34eSHiren Panchasara 	int pipe;
52067fef78bSLawrence Stewart 
52167fef78bSLawrence Stewart 	cubic_data = ccv->cc_data;
522f81bc34eSHiren Panchasara 	pipe = 0;
52367fef78bSLawrence Stewart 
52467fef78bSLawrence Stewart 	if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
52567fef78bSLawrence Stewart 		/*
52667fef78bSLawrence Stewart 		 * If inflight data is less than ssthresh, set cwnd
52767fef78bSLawrence Stewart 		 * conservatively to avoid a burst of data, as suggested in
52867fef78bSLawrence Stewart 		 * the NewReno RFC. Otherwise, use the CUBIC method.
52967fef78bSLawrence Stewart 		 *
53067fef78bSLawrence Stewart 		 * XXXLAS: Find a way to do this without needing curack
53167fef78bSLawrence Stewart 		 */
532d1de2b05SRichard Scheffenegger 		if (V_tcp_do_newsack)
533f81bc34eSHiren Panchasara 			pipe = tcp_compute_pipe(ccv->ccvc.tcp);
534f81bc34eSHiren Panchasara 		else
535f81bc34eSHiren Panchasara 			pipe = CCV(ccv, snd_max) - ccv->curack;
536f81bc34eSHiren Panchasara 
537f81bc34eSHiren Panchasara 		if (pipe < CCV(ccv, snd_ssthresh))
5385cc11a89SMichael Tuexen 			/*
5395cc11a89SMichael Tuexen 			 * Ensure that cwnd does not collapse to 1 MSS under
5405cc11a89SMichael Tuexen 			 * adverse conditions. Implements RFC6582
5415cc11a89SMichael Tuexen 			 */
5425cc11a89SMichael Tuexen 			CCV(ccv, snd_cwnd) = max(pipe, CCV(ccv, t_maxseg)) +
5435cc11a89SMichael Tuexen 			    CCV(ccv, t_maxseg);
54467fef78bSLawrence Stewart 		else
54567fef78bSLawrence Stewart 			/* Update cwnd based on beta and adjusted max_cwnd. */
54614558b99SRichard Scheffenegger 			CCV(ccv, snd_cwnd) = max(((uint64_t)cubic_data->max_cwnd *
54714558b99SRichard Scheffenegger 			    CUBIC_BETA) >> CUBIC_SHIFT,
54814558b99SRichard Scheffenegger 			    2 * CCV(ccv, t_maxseg));
54967fef78bSLawrence Stewart 	}
55067fef78bSLawrence Stewart 
55167fef78bSLawrence Stewart 	/* Calculate the average RTT between congestion epochs. */
55247f44cddSLawrence Stewart 	if (cubic_data->epoch_ack_count > 0 &&
553*a3aa6f65SCheng Cui 	    cubic_data->sum_rtt_usecs >= cubic_data->epoch_ack_count) {
554*a3aa6f65SCheng Cui 		cubic_data->mean_rtt_usecs = (int)(cubic_data->sum_rtt_usecs /
55567fef78bSLawrence Stewart 		    cubic_data->epoch_ack_count);
55647f44cddSLawrence Stewart 	}
55767fef78bSLawrence Stewart 
55867fef78bSLawrence Stewart 	cubic_data->epoch_ack_count = 0;
559*a3aa6f65SCheng Cui 	cubic_data->sum_rtt_usecs = 0;
56067fef78bSLawrence Stewart }
56167fef78bSLawrence Stewart 
56267fef78bSLawrence Stewart /*
56367fef78bSLawrence Stewart  * Record the min RTT and sum samples for the epoch average RTT calculation.
56467fef78bSLawrence Stewart  */
56567fef78bSLawrence Stewart static void
56667fef78bSLawrence Stewart cubic_record_rtt(struct cc_var *ccv)
56767fef78bSLawrence Stewart {
56867fef78bSLawrence Stewart 	struct cubic *cubic_data;
569*a3aa6f65SCheng Cui 	uint32_t t_srtt_usecs;
57067fef78bSLawrence Stewart 
57167fef78bSLawrence Stewart 	/* Ignore srtt until a min number of samples have been taken. */
57267fef78bSLawrence Stewart 	if (CCV(ccv, t_rttupdated) >= CUBIC_MIN_RTT_SAMPLES) {
57367fef78bSLawrence Stewart 		cubic_data = ccv->cc_data;
574*a3aa6f65SCheng Cui 		t_srtt_usecs = tcp_get_srtt(ccv->ccvc.tcp,
575*a3aa6f65SCheng Cui 					    TCP_TMR_GRANULARITY_USEC);
57667fef78bSLawrence Stewart 		/*
57767fef78bSLawrence Stewart 		 * Record the current SRTT as our minrtt if it's the smallest
57867fef78bSLawrence Stewart 		 * we've seen or minrtt is currently equal to its initialised
57967fef78bSLawrence Stewart 		 * value.
58067fef78bSLawrence Stewart 		 *
58167fef78bSLawrence Stewart 		 * XXXLAS: Should there be some hysteresis for minrtt?
58267fef78bSLawrence Stewart 		 */
583*a3aa6f65SCheng Cui 		if ((t_srtt_usecs < cubic_data->min_rtt_usecs ||
584*a3aa6f65SCheng Cui 		    cubic_data->min_rtt_usecs == TCPTV_SRTTBASE)) {
585*a3aa6f65SCheng Cui 			/* A minimal rtt is a single unshifted tick of a ticks
586*a3aa6f65SCheng Cui 			 * timer. */
587*a3aa6f65SCheng Cui 			cubic_data->min_rtt_usecs = max(tick >> TCP_RTT_SHIFT,
588*a3aa6f65SCheng Cui 							t_srtt_usecs);
58967fef78bSLawrence Stewart 
59047f44cddSLawrence Stewart 			/*
59147f44cddSLawrence Stewart 			 * If the connection is within its first congestion
592*a3aa6f65SCheng Cui 			 * epoch, ensure we prime mean_rtt_usecs with a
59347f44cddSLawrence Stewart 			 * reasonable value until the epoch average RTT is
59447f44cddSLawrence Stewart 			 * calculated in cubic_post_recovery().
59547f44cddSLawrence Stewart 			 */
596*a3aa6f65SCheng Cui 			if (cubic_data->min_rtt_usecs >
597*a3aa6f65SCheng Cui 			    cubic_data->mean_rtt_usecs)
598*a3aa6f65SCheng Cui 				cubic_data->mean_rtt_usecs =
599*a3aa6f65SCheng Cui 				    cubic_data->min_rtt_usecs;
60047f44cddSLawrence Stewart 		}
60147f44cddSLawrence Stewart 
60267fef78bSLawrence Stewart 		/* Sum samples for epoch average RTT calculation. */
603*a3aa6f65SCheng Cui 		cubic_data->sum_rtt_usecs += t_srtt_usecs;
60467fef78bSLawrence Stewart 		cubic_data->epoch_ack_count++;
60567fef78bSLawrence Stewart 	}
60667fef78bSLawrence Stewart }
60767fef78bSLawrence Stewart 
60867fef78bSLawrence Stewart /*
60967fef78bSLawrence Stewart  * Update the ssthresh in the event of congestion.
61067fef78bSLawrence Stewart  */
61167fef78bSLawrence Stewart static void
61237674273SRichard Scheffenegger cubic_ssthresh_update(struct cc_var *ccv, uint32_t maxseg)
61367fef78bSLawrence Stewart {
61467fef78bSLawrence Stewart 	struct cubic *cubic_data;
61514558b99SRichard Scheffenegger 	uint32_t ssthresh;
616a459638fSRichard Scheffenegger 	uint32_t cwnd;
61767fef78bSLawrence Stewart 
61867fef78bSLawrence Stewart 	cubic_data = ccv->cc_data;
619a459638fSRichard Scheffenegger 	cwnd = CCV(ccv, snd_cwnd);
620a459638fSRichard Scheffenegger 
621a459638fSRichard Scheffenegger 	/* Fast convergence heuristic. */
622a459638fSRichard Scheffenegger 	if (cwnd < cubic_data->max_cwnd) {
623a459638fSRichard Scheffenegger 		cwnd = ((uint64_t)cwnd * CUBIC_FC_FACTOR) >> CUBIC_SHIFT;
624a459638fSRichard Scheffenegger 	}
625a459638fSRichard Scheffenegger 	cubic_data->prev_max_cwnd = cubic_data->max_cwnd;
626a459638fSRichard Scheffenegger 	cubic_data->max_cwnd = cwnd;
62767fef78bSLawrence Stewart 
62867fef78bSLawrence Stewart 	/*
629a459638fSRichard Scheffenegger 	 * On the first congestion event, set ssthresh to cwnd * 0.5
630a459638fSRichard Scheffenegger 	 * and reduce max_cwnd to cwnd * beta. This aligns the cubic concave
631a459638fSRichard Scheffenegger 	 * region appropriately. On subsequent congestion events, set
632a459638fSRichard Scheffenegger 	 * ssthresh to cwnd * beta.
63367fef78bSLawrence Stewart 	 */
634a459638fSRichard Scheffenegger 	if ((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) {
635a459638fSRichard Scheffenegger 		ssthresh = cwnd >> 1;
636a459638fSRichard Scheffenegger 		cubic_data->max_cwnd = ((uint64_t)cwnd *
6373ac12506SJonathan T. Looney 		    CUBIC_BETA) >> CUBIC_SHIFT;
638a459638fSRichard Scheffenegger 	} else {
639a459638fSRichard Scheffenegger 		ssthresh = ((uint64_t)cwnd *
640a459638fSRichard Scheffenegger 		    CUBIC_BETA) >> CUBIC_SHIFT;
641a459638fSRichard Scheffenegger 	}
64237674273SRichard Scheffenegger 	CCV(ccv, snd_ssthresh) = max(ssthresh, 2 * maxseg);
64367fef78bSLawrence Stewart }
64467fef78bSLawrence Stewart 
645a9696510SRandall Stewart static void
646a9696510SRandall Stewart cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt, uint32_t rxtcnt, uint32_t fas)
647a9696510SRandall Stewart {
648a9696510SRandall Stewart 	struct cubic *cubicd;
649a9696510SRandall Stewart 
650a9696510SRandall Stewart 	cubicd = ccv->cc_data;
651a9696510SRandall Stewart 	if (rxtcnt > 1) {
652a9696510SRandall Stewart 		/*
653a9696510SRandall Stewart 		 * Only look at RTT's that are non-ambiguous.
654a9696510SRandall Stewart 		 */
655a9696510SRandall Stewart 		return;
656a9696510SRandall Stewart 	}
657a9696510SRandall Stewart 	cubicd->css_rttsample_count++;
658a9696510SRandall Stewart 	cubicd->css_last_fas = fas;
659a9696510SRandall Stewart 	if (cubicd->css_current_round_minrtt > usec_rtt) {
660a9696510SRandall Stewart 		cubicd->css_current_round_minrtt = usec_rtt;
661a9696510SRandall Stewart 		cubicd->css_lowrtt_fas = cubicd->css_last_fas;
662a9696510SRandall Stewart 	}
663a9696510SRandall Stewart 	if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) &&
664a9696510SRandall Stewart 	    (cubicd->css_current_round_minrtt != 0xffffffff) &&
665e88412d8SRandall Stewart 	    (cubicd->css_current_round_minrtt < cubicd->css_baseline_minrtt) &&
666a9696510SRandall Stewart 	    (cubicd->css_lastround_minrtt != 0xffffffff)) {
667a9696510SRandall Stewart 		/*
668a9696510SRandall Stewart 		 * We were in CSS and the RTT is now less, we
669a9696510SRandall Stewart 		 * entered CSS erroneously.
670a9696510SRandall Stewart 		 */
671a9696510SRandall Stewart 		cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
672a9696510SRandall Stewart 		cubic_log_hystart_event(ccv, cubicd, 8, cubicd->css_baseline_minrtt);
673a9696510SRandall Stewart 		cubicd->css_baseline_minrtt = 0xffffffff;
674a9696510SRandall Stewart 	}
675a9696510SRandall Stewart 	if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED)
676a9696510SRandall Stewart 		cubic_log_hystart_event(ccv, cubicd, 5, usec_rtt);
677a9696510SRandall Stewart }
678a9696510SRandall Stewart 
679a9696510SRandall Stewart static void
680a9696510SRandall Stewart cubic_newround(struct cc_var *ccv, uint32_t round_cnt)
681a9696510SRandall Stewart {
682a9696510SRandall Stewart 	struct cubic *cubicd;
683a9696510SRandall Stewart 
684a9696510SRandall Stewart 	cubicd = ccv->cc_data;
685a9696510SRandall Stewart 	/* We have entered a new round */
686a9696510SRandall Stewart 	cubicd->css_lastround_minrtt = cubicd->css_current_round_minrtt;
687a9696510SRandall Stewart 	cubicd->css_current_round_minrtt = 0xffffffff;
688a9696510SRandall Stewart 	cubicd->css_rttsample_count = 0;
689a9696510SRandall Stewart 	cubicd->css_current_round = round_cnt;
690a9696510SRandall Stewart 	if ((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) &&
691a9696510SRandall Stewart 	    ((round_cnt - cubicd->css_entered_at_round) >= hystart_css_rounds)) {
692a9696510SRandall Stewart 		/* Enter CA */
693a9696510SRandall Stewart 		if (ccv->flags & CCF_HYSTART_CAN_SH_CWND) {
694a9696510SRandall Stewart 			/*
695a9696510SRandall Stewart 			 * We engage more than snd_ssthresh, engage
696a9696510SRandall Stewart 			 * the brakes!! Though we will stay in SS to
697a9696510SRandall Stewart 			 * creep back up again, so lets leave CSS active
698a9696510SRandall Stewart 			 * and give us hystart_css_rounds more rounds.
699a9696510SRandall Stewart 			 */
700a9696510SRandall Stewart 			if (ccv->flags & CCF_HYSTART_CONS_SSTH) {
701a9696510SRandall Stewart 				CCV(ccv, snd_ssthresh) = ((cubicd->css_lowrtt_fas + cubicd->css_fas_at_css_entry) / 2);
702a9696510SRandall Stewart 			} else {
703a9696510SRandall Stewart 				CCV(ccv, snd_ssthresh) = cubicd->css_lowrtt_fas;
704a9696510SRandall Stewart 			}
705a9696510SRandall Stewart 			CCV(ccv, snd_cwnd) = cubicd->css_fas_at_css_entry;
706a9696510SRandall Stewart 			cubicd->css_entered_at_round = round_cnt;
707a9696510SRandall Stewart 		} else {
708a9696510SRandall Stewart 			CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
709a9696510SRandall Stewart 			/* Turn off the CSS flag */
710a9696510SRandall Stewart 			cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
711a9696510SRandall Stewart 			/* Disable use of CSS in the future except long idle  */
712a9696510SRandall Stewart 			cubicd->flags &= ~CUBICFLAG_HYSTART_ENABLED;
713a9696510SRandall Stewart 		}
714a9696510SRandall Stewart 		cubic_log_hystart_event(ccv, cubicd, 6, CCV(ccv, snd_ssthresh));
715a9696510SRandall Stewart 	}
716a9696510SRandall Stewart 	if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED)
717a9696510SRandall Stewart 		cubic_log_hystart_event(ccv, cubicd, 4, round_cnt);
718a9696510SRandall Stewart }
719a9696510SRandall Stewart 
72067fef78bSLawrence Stewart DECLARE_CC_MODULE(cubic, &cubic_cc_algo);
721b8d60729SRandall Stewart MODULE_VERSION(cubic, 2);
722