xref: /freebsd/sys/netinet/cc/cc_cubic.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org>
5  * Copyright (c) 2010 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * This software was developed by Lawrence Stewart while studying at the Centre
9  * for Advanced Internet Architectures, Swinburne University of Technology, made
10  * possible in part by a grant from the Cisco University Research Program Fund
11  * at Community Foundation Silicon Valley.
12  *
13  * Portions of this software were developed at the Centre for Advanced
14  * Internet Architectures, Swinburne University of Technology, Melbourne,
15  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 /*
40  * An implementation of the CUBIC congestion control algorithm for FreeBSD,
41  * based on the Internet Draft "draft-rhee-tcpm-cubic-02" by Rhee, Xu and Ha.
42  * Originally released as part of the NewTCP research project at Swinburne
43  * University of Technology's Centre for Advanced Internet Architectures,
44  * Melbourne, Australia, which was made possible in part by a grant from the
45  * Cisco University Research Program Fund at Community Foundation Silicon
46  * Valley. More details are available at:
47  *   http://caia.swin.edu.au/urp/newtcp/
48  */
49 
50 #include <sys/cdefs.h>
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/limits.h>
54 #include <sys/malloc.h>
55 #include <sys/module.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
60 
61 #include <net/vnet.h>
62 
63 #include <net/route.h>
64 #include <net/route/nhop.h>
65 
66 #include <netinet/in_pcb.h>
67 #include <netinet/tcp.h>
68 #include <netinet/tcp_seq.h>
69 #include <netinet/tcp_timer.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet/tcp_log_buf.h>
72 #include <netinet/tcp_hpts.h>
73 #include <netinet/cc/cc.h>
74 #include <netinet/cc/cc_cubic.h>
75 #include <netinet/cc/cc_module.h>
76 
77 static void	cubic_ack_received(struct cc_var *ccv, uint16_t type);
78 static void	cubic_cb_destroy(struct cc_var *ccv);
79 static int	cubic_cb_init(struct cc_var *ccv, void *ptr);
80 static void	cubic_cong_signal(struct cc_var *ccv, uint32_t type);
81 static void	cubic_conn_init(struct cc_var *ccv);
82 static int	cubic_mod_init(void);
83 static void	cubic_post_recovery(struct cc_var *ccv);
84 static void	cubic_record_rtt(struct cc_var *ccv);
85 static void	cubic_ssthresh_update(struct cc_var *ccv, uint32_t maxseg);
86 static void	cubic_after_idle(struct cc_var *ccv);
87 static size_t	cubic_data_sz(void);
88 static void	cubic_newround(struct cc_var *ccv, uint32_t round_cnt);
89 static void	cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt,
90        uint32_t rxtcnt, uint32_t fas);
91 
92 struct cc_algo cubic_cc_algo = {
93 	.name = "cubic",
94 	.ack_received = cubic_ack_received,
95 	.cb_destroy = cubic_cb_destroy,
96 	.cb_init = cubic_cb_init,
97 	.cong_signal = cubic_cong_signal,
98 	.conn_init = cubic_conn_init,
99 	.mod_init = cubic_mod_init,
100 	.post_recovery = cubic_post_recovery,
101 	.after_idle = cubic_after_idle,
102 	.cc_data_sz = cubic_data_sz,
103 	.rttsample = cubic_rttsample,
104 	.newround = cubic_newround
105 };
106 
107 static void
108 cubic_log_hystart_event(struct cc_var *ccv, struct cubic *cubicd, uint8_t mod, uint32_t flex1)
109 {
110 	/*
111 	 * Types of logs (mod value)
112 	 * 1 - rtt_thresh in flex1, checking to see if RTT is to great.
113 	 * 2 - rtt is too great, rtt_thresh in flex1.
114 	 * 3 - CSS is active incr in flex1
115 	 * 4 - A new round is beginning flex1 is round count
116 	 * 5 - A new RTT measurement flex1 is the new measurement.
117 	 * 6 - We enter CA ssthresh is also in flex1.
118 	 * 7 - Socket option to change hystart executed opt.val in flex1.
119 	 * 8 - Back out of CSS into SS, flex1 is the css_baseline_minrtt
120 	 * 9 - We enter CA, via an ECN mark.
121 	 * 10 - We enter CA, via a loss.
122 	 * 11 - We have slipped out of SS into CA via cwnd growth.
123 	 * 12 - After idle has re-enabled hystart++
124 	 */
125 	struct tcpcb *tp;
126 
127 	if (hystart_bblogs == 0)
128 		return;
129 	tp = ccv->ccvc.tcp;
130 	if (tcp_bblogging_on(tp)) {
131 		union tcp_log_stackspecific log;
132 		struct timeval tv;
133 
134 		memset(&log, 0, sizeof(log));
135 		log.u_bbr.flex1 = flex1;
136 		log.u_bbr.flex2 = cubicd->css_current_round_minrtt;
137 		log.u_bbr.flex3 = cubicd->css_lastround_minrtt;
138 		log.u_bbr.flex4 = cubicd->css_rttsample_count;
139 		log.u_bbr.flex5 = cubicd->css_entered_at_round;
140 		log.u_bbr.flex6 = cubicd->css_baseline_minrtt;
141 		/* We only need bottom 16 bits of flags */
142 		log.u_bbr.flex7 = cubicd->flags & 0x0000ffff;
143 		log.u_bbr.flex8 = mod;
144 		log.u_bbr.epoch = cubicd->css_current_round;
145 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
146 		log.u_bbr.lt_epoch = cubicd->css_fas_at_css_entry;
147 		log.u_bbr.pkts_out = cubicd->css_last_fas;
148 		log.u_bbr.delivered = cubicd->css_lowrtt_fas;
149 		log.u_bbr.pkt_epoch = ccv->flags;
150 		TCP_LOG_EVENTP(tp, NULL,
151 		    &tptosocket(tp)->so_rcv,
152 		    &tptosocket(tp)->so_snd,
153 		    TCP_HYSTART, 0,
154 		    0, &log, false, &tv);
155 	}
156 }
157 
158 static void
159 cubic_does_slow_start(struct cc_var *ccv, struct cubic *cubicd)
160 {
161 	/*
162 	 * In slow-start with ABC enabled and no RTO in sight?
163 	 * (Must not use abc_l_var > 1 if slow starting after
164 	 * an RTO. On RTO, snd_nxt = snd_una, so the
165 	 * snd_nxt == snd_max check is sufficient to
166 	 * handle this).
167 	 *
168 	 * XXXLAS: Find a way to signal SS after RTO that
169 	 * doesn't rely on tcpcb vars.
170 	 */
171 	u_int cw = CCV(ccv, snd_cwnd);
172 	u_int incr = CCV(ccv, t_maxseg);
173 	uint16_t abc_val;
174 
175 	cubicd->flags |= CUBICFLAG_IN_SLOWSTART;
176 	if (ccv->flags & CCF_USE_LOCAL_ABC)
177 		abc_val = ccv->labc;
178 	else
179 		abc_val = V_tcp_abc_l_var;
180 	if ((ccv->flags & CCF_HYSTART_ALLOWED) &&
181 	    (cubicd->flags & CUBICFLAG_HYSTART_ENABLED) &&
182 	    ((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) == 0)) {
183 		/*
184 		 * Hystart is allowed and still enabled and we are not yet
185 		 * in CSS. Lets check to see if we can make a decision on
186 		 * if we need to go into CSS.
187 		 */
188 		if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) &&
189 		    (cubicd->css_current_round_minrtt != 0xffffffff) &&
190 		    (cubicd->css_lastround_minrtt != 0xffffffff)) {
191 			uint32_t rtt_thresh;
192 
193 			/* Clamp (minrtt_thresh, lastround/8, maxrtt_thresh) */
194 			rtt_thresh = (cubicd->css_lastround_minrtt >> 3);
195 			if (rtt_thresh < hystart_minrtt_thresh)
196 				rtt_thresh = hystart_minrtt_thresh;
197 			if (rtt_thresh > hystart_maxrtt_thresh)
198 				rtt_thresh = hystart_maxrtt_thresh;
199 			cubic_log_hystart_event(ccv, cubicd, 1, rtt_thresh);
200 
201 			if (cubicd->css_current_round_minrtt >= (cubicd->css_lastround_minrtt + rtt_thresh)) {
202 				/* Enter CSS */
203 				cubicd->flags |= CUBICFLAG_HYSTART_IN_CSS;
204 				cubicd->css_fas_at_css_entry = cubicd->css_lowrtt_fas;
205 				/*
206 				 * The draft (v4) calls for us to set baseline to css_current_round_min
207 				 * but that can cause an oscillation. We probably shoudl be using
208 				 * css_lastround_minrtt, but the authors insist that will cause
209 				 * issues on exiting early. We will leave the draft version for now
210 				 * but I suspect this is incorrect.
211 				 */
212 				cubicd->css_baseline_minrtt = cubicd->css_current_round_minrtt;
213 				cubicd->css_entered_at_round = cubicd->css_current_round;
214 				cubic_log_hystart_event(ccv, cubicd, 2, rtt_thresh);
215 			}
216 		}
217 	}
218 	if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max))
219 		incr = min(ccv->bytes_this_ack,
220 			   ccv->nsegs * abc_val *
221 			   CCV(ccv, t_maxseg));
222 	else
223 		incr = min(ccv->bytes_this_ack, CCV(ccv, t_maxseg));
224 
225 	/* Only if Hystart is enabled will the flag get set */
226 	if (cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) {
227 		incr /= hystart_css_growth_div;
228 		cubic_log_hystart_event(ccv, cubicd, 3, incr);
229 	}
230 	/* ABC is on by default, so incr equals 0 frequently. */
231 	if (incr > 0)
232 		CCV(ccv, snd_cwnd) = min((cw + incr),
233 					 TCP_MAXWIN << CCV(ccv, snd_scale));
234 }
235 
236 static void
237 cubic_ack_received(struct cc_var *ccv, uint16_t type)
238 {
239 	struct cubic *cubic_data;
240 	unsigned long W_est, W_cubic;
241 	int usecs_since_epoch;
242 
243 	cubic_data = ccv->cc_data;
244 	cubic_record_rtt(ccv);
245 
246 	/*
247 	 * For a regular ACK and we're not in cong/fast recovery and
248 	 * we're cwnd limited, always recalculate cwnd.
249 	 */
250 	if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
251 	    (ccv->flags & CCF_CWND_LIMITED)) {
252 		 /* Use the logic in NewReno ack_received() for slow start. */
253 		if (CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||
254 		    cubic_data->min_rtt_usecs == TCPTV_SRTTBASE) {
255 			cubic_does_slow_start(ccv, cubic_data);
256 		} else {
257 			if (cubic_data->flags & CUBICFLAG_HYSTART_IN_CSS) {
258 				/*
259 				 * We have slipped into CA with
260 				 * CSS active. Deactivate all.
261 				 */
262 				/* Turn off the CSS flag */
263 				cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
264 				/* Disable use of CSS in the future except long idle  */
265 				cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
266 				cubic_log_hystart_event(ccv, cubic_data, 11, CCV(ccv, snd_ssthresh));
267 			}
268 			if ((cubic_data->flags & CUBICFLAG_RTO_EVENT) &&
269 			    (cubic_data->flags & CUBICFLAG_IN_SLOWSTART)) {
270 				/* RFC8312 Section 4.7 */
271 				cubic_data->flags &= ~(CUBICFLAG_RTO_EVENT |
272 						       CUBICFLAG_IN_SLOWSTART);
273 				cubic_data->W_max = CCV(ccv, snd_cwnd);
274 				cubic_data->K = 0;
275 			} else if (cubic_data->flags & (CUBICFLAG_IN_SLOWSTART |
276 						 CUBICFLAG_IN_APPLIMIT)) {
277 				cubic_data->flags &= ~(CUBICFLAG_IN_SLOWSTART |
278 						       CUBICFLAG_IN_APPLIMIT);
279 				cubic_data->t_epoch = ticks;
280 				cubic_data->K = cubic_k(cubic_data->W_max /
281 							CCV(ccv, t_maxseg));
282 			}
283 			usecs_since_epoch = (ticks - cubic_data->t_epoch) * tick;
284 			if (usecs_since_epoch < 0) {
285 				/*
286 				 * dragging t_epoch along
287 				 */
288 				usecs_since_epoch = INT_MAX;
289 				cubic_data->t_epoch = ticks - INT_MAX;
290 			}
291 			/*
292 			 * The mean RTT is used to best reflect the equations in
293 			 * the I-D. Using min_rtt in the tf_cwnd calculation
294 			 * causes W_est to grow much faster than it should if the
295 			 * RTT is dominated by network buffering rather than
296 			 * propagation delay.
297 			 */
298 			W_est = tf_cwnd(usecs_since_epoch, cubic_data->mean_rtt_usecs,
299 				       cubic_data->W_max, CCV(ccv, t_maxseg));
300 
301 			W_cubic = cubic_cwnd(usecs_since_epoch +
302 					     cubic_data->mean_rtt_usecs,
303 					     cubic_data->W_max,
304 					     CCV(ccv, t_maxseg),
305 					     cubic_data->K);
306 
307 			ccv->flags &= ~CCF_ABC_SENTAWND;
308 
309 			if (W_cubic < W_est) {
310 				/*
311 				 * TCP-friendly region, follow tf
312 				 * cwnd growth.
313 				 */
314 				if (CCV(ccv, snd_cwnd) < W_est)
315 					CCV(ccv, snd_cwnd) = ulmin(W_est, INT_MAX);
316 			} else if (CCV(ccv, snd_cwnd) < W_cubic) {
317 				/*
318 				 * Concave or convex region, follow CUBIC
319 				 * cwnd growth.
320 				 * Only update snd_cwnd, if it doesn't shrink.
321 				 */
322 				CCV(ccv, snd_cwnd) = ulmin(W_cubic, INT_MAX);
323 			}
324 
325 			/*
326 			 * If we're not in slow start and we're probing for a
327 			 * new cwnd limit at the start of a connection
328 			 * (happens when hostcache has a relevant entry),
329 			 * keep updating our current estimate of the
330 			 * W_max.
331 			 */
332 			if (((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) &&
333 			    cubic_data->W_max < CCV(ccv, snd_cwnd)) {
334 				cubic_data->W_max = CCV(ccv, snd_cwnd);
335 				cubic_data->K = cubic_k(cubic_data->W_max /
336 				    CCV(ccv, t_maxseg));
337 			}
338 		}
339 	} else if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
340 	    !(ccv->flags & CCF_CWND_LIMITED)) {
341 		cubic_data->flags |= CUBICFLAG_IN_APPLIMIT;
342 	}
343 }
344 
345 /*
346  * This is a CUBIC specific implementation of after_idle.
347  *   - Reset cwnd by calling New Reno implementation of after_idle.
348  *   - Reset t_epoch.
349  */
350 static void
351 cubic_after_idle(struct cc_var *ccv)
352 {
353 	struct cubic *cubic_data;
354 
355 	cubic_data = ccv->cc_data;
356 
357 	cubic_data->W_max = ulmax(cubic_data->W_max, CCV(ccv, snd_cwnd));
358 	cubic_data->K = cubic_k(cubic_data->W_max / CCV(ccv, t_maxseg));
359 	if ((cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) == 0) {
360 		/*
361 		 * Re-enable hystart if we have been idle.
362 		 */
363 		cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
364 		cubic_data->flags |= CUBICFLAG_HYSTART_ENABLED;
365 		cubic_log_hystart_event(ccv, cubic_data, 12, CCV(ccv, snd_ssthresh));
366 	}
367 	newreno_cc_after_idle(ccv);
368 	cubic_data->t_epoch = ticks;
369 }
370 
371 static void
372 cubic_cb_destroy(struct cc_var *ccv)
373 {
374 	free(ccv->cc_data, M_CC_MEM);
375 }
376 
377 static size_t
378 cubic_data_sz(void)
379 {
380 	return (sizeof(struct cubic));
381 }
382 
383 static int
384 cubic_cb_init(struct cc_var *ccv, void *ptr)
385 {
386 	struct cubic *cubic_data;
387 
388 	INP_WLOCK_ASSERT(tptoinpcb(ccv->ccvc.tcp));
389 	if (ptr == NULL) {
390 		cubic_data = malloc(sizeof(struct cubic), M_CC_MEM, M_NOWAIT|M_ZERO);
391 		if (cubic_data == NULL)
392 			return (ENOMEM);
393 	} else
394 		cubic_data = ptr;
395 
396 	/* Init some key variables with sensible defaults. */
397 	cubic_data->t_epoch = ticks;
398 	cubic_data->min_rtt_usecs = TCPTV_SRTTBASE;
399 	cubic_data->mean_rtt_usecs = 1;
400 
401 	ccv->cc_data = cubic_data;
402 	cubic_data->flags = CUBICFLAG_HYSTART_ENABLED;
403 	/* At init set both to infinity */
404 	cubic_data->css_lastround_minrtt = 0xffffffff;
405 	cubic_data->css_current_round_minrtt = 0xffffffff;
406 	cubic_data->css_current_round = 0;
407 	cubic_data->css_baseline_minrtt = 0xffffffff;
408 	cubic_data->css_rttsample_count = 0;
409 	cubic_data->css_entered_at_round = 0;
410 	cubic_data->css_fas_at_css_entry = 0;
411 	cubic_data->css_lowrtt_fas = 0;
412 	cubic_data->css_last_fas = 0;
413 
414 	return (0);
415 }
416 
417 /*
418  * Perform any necessary tasks before we enter congestion recovery.
419  */
420 static void
421 cubic_cong_signal(struct cc_var *ccv, uint32_t type)
422 {
423 	struct cubic *cubic_data;
424 	u_int mss;
425 
426 	cubic_data = ccv->cc_data;
427 	mss = tcp_maxseg(ccv->ccvc.tcp);
428 
429 	switch (type) {
430 	case CC_NDUPACK:
431 		if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) {
432 			/* Make sure the flags are all off we had a loss */
433 			cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
434 			cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
435 			cubic_log_hystart_event(ccv, cubic_data, 10, CCV(ccv, snd_ssthresh));
436 		}
437 		if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
438 			if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
439 				cubic_ssthresh_update(ccv, mss);
440 				cubic_data->flags |= CUBICFLAG_CONG_EVENT;
441 				cubic_data->t_epoch = ticks;
442 				cubic_data->K = cubic_k(cubic_data->W_max / mss);
443 			}
444 			ENTER_RECOVERY(CCV(ccv, t_flags));
445 		}
446 		break;
447 
448 	case CC_ECN:
449 		if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) {
450 			/* Make sure the flags are all off we had a loss */
451 			cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
452 			cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
453 			cubic_log_hystart_event(ccv, cubic_data, 9, CCV(ccv, snd_ssthresh));
454 		}
455 		if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
456 			cubic_ssthresh_update(ccv, mss);
457 			cubic_data->flags |= CUBICFLAG_CONG_EVENT;
458 			cubic_data->t_epoch = ticks;
459 			cubic_data->K = cubic_k(cubic_data->W_max / mss);
460 			CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
461 			ENTER_CONGRECOVERY(CCV(ccv, t_flags));
462 		}
463 		break;
464 
465 	case CC_RTO:
466 		/* RFC8312 Section 4.7 */
467 		if (CCV(ccv, t_rxtshift) == 1) {
468 			/*
469 			 * Remember the state only for the first RTO event. This
470 			 * will help us restore the state to the values seen
471 			 * at the most recent congestion avoidance stage before
472 			 * the current RTO event.
473 			 */
474 			cubic_data->undo_t_epoch = cubic_data->t_epoch;
475 			cubic_data->undo_cwnd_epoch = cubic_data->cwnd_epoch;
476 			cubic_data->undo_W_est = cubic_data->W_est;
477 			cubic_data->undo_cwnd_prior = cubic_data->cwnd_prior;
478 			cubic_data->undo_W_max = cubic_data->W_max;
479 			cubic_data->undo_K = cubic_data->K;
480 		}
481 		cubic_data->flags |= CUBICFLAG_CONG_EVENT | CUBICFLAG_RTO_EVENT;
482 		cubic_data->undo_W_max = cubic_data->W_max;
483 		cubic_data->num_cong_events++;
484 			CCV(ccv, snd_ssthresh) = ((uint64_t)CCV(ccv, snd_cwnd) *
485 					  CUBIC_BETA) >> CUBIC_SHIFT;
486 		CCV(ccv, snd_cwnd) = mss;
487 		break;
488 
489 	case CC_RTO_ERR:
490 		cubic_data->flags &= ~(CUBICFLAG_CONG_EVENT | CUBICFLAG_RTO_EVENT);
491 		cubic_data->num_cong_events--;
492 		cubic_data->K = cubic_data->undo_K;
493 		cubic_data->cwnd_prior = cubic_data->undo_cwnd_prior;
494 		cubic_data->W_max = cubic_data->undo_W_max;
495 		cubic_data->W_est = cubic_data->undo_W_est;
496 		cubic_data->cwnd_epoch = cubic_data->undo_cwnd_epoch;
497 		cubic_data->t_epoch = cubic_data->undo_t_epoch;
498 		break;
499 	}
500 }
501 
502 static void
503 cubic_conn_init(struct cc_var *ccv)
504 {
505 	struct cubic *cubic_data;
506 
507 	cubic_data = ccv->cc_data;
508 
509 	/*
510 	 * Ensure we have a sane initial value for W_max recorded. Without
511 	 * this here bad things happen when entries from the TCP hostcache
512 	 * get used.
513 	 */
514 	cubic_data->W_max = CCV(ccv, snd_cwnd);
515 }
516 
517 static int
518 cubic_mod_init(void)
519 {
520 	return (0);
521 }
522 
523 /*
524  * Perform any necessary tasks before we exit congestion recovery.
525  */
526 static void
527 cubic_post_recovery(struct cc_var *ccv)
528 {
529 	struct cubic *cubic_data;
530 	int pipe;
531 
532 	cubic_data = ccv->cc_data;
533 	pipe = 0;
534 
535 	if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
536 		/*
537 		 * If inflight data is less than ssthresh, set cwnd
538 		 * conservatively to avoid a burst of data, as suggested in
539 		 * the NewReno RFC. Otherwise, use the CUBIC method.
540 		 *
541 		 * XXXLAS: Find a way to do this without needing curack
542 		 */
543 		if (V_tcp_do_newsack)
544 			pipe = tcp_compute_pipe(ccv->ccvc.tcp);
545 		else
546 			pipe = CCV(ccv, snd_max) - ccv->curack;
547 
548 		if (pipe < CCV(ccv, snd_ssthresh))
549 			/*
550 			 * Ensure that cwnd does not collapse to 1 MSS under
551 			 * adverse conditions. Implements RFC6582
552 			 */
553 			CCV(ccv, snd_cwnd) = max(pipe, CCV(ccv, t_maxseg)) +
554 			    CCV(ccv, t_maxseg);
555 		else
556 			/* Update cwnd based on beta and adjusted W_max. */
557 			CCV(ccv, snd_cwnd) = max(((uint64_t)cubic_data->W_max *
558 			    CUBIC_BETA) >> CUBIC_SHIFT,
559 			    2 * CCV(ccv, t_maxseg));
560 	}
561 
562 	/* Calculate the average RTT between congestion epochs. */
563 	if (cubic_data->epoch_ack_count > 0 &&
564 	    cubic_data->sum_rtt_usecs >= cubic_data->epoch_ack_count) {
565 		cubic_data->mean_rtt_usecs = (int)(cubic_data->sum_rtt_usecs /
566 		    cubic_data->epoch_ack_count);
567 	}
568 
569 	cubic_data->epoch_ack_count = 0;
570 	cubic_data->sum_rtt_usecs = 0;
571 }
572 
573 /*
574  * Record the min RTT and sum samples for the epoch average RTT calculation.
575  */
576 static void
577 cubic_record_rtt(struct cc_var *ccv)
578 {
579 	struct cubic *cubic_data;
580 	uint32_t t_srtt_usecs;
581 
582 	/* Ignore srtt until a min number of samples have been taken. */
583 	if (CCV(ccv, t_rttupdated) >= CUBIC_MIN_RTT_SAMPLES) {
584 		cubic_data = ccv->cc_data;
585 		t_srtt_usecs = tcp_get_srtt(ccv->ccvc.tcp,
586 					    TCP_TMR_GRANULARITY_USEC);
587 		/*
588 		 * Record the current SRTT as our minrtt if it's the smallest
589 		 * we've seen or minrtt is currently equal to its initialised
590 		 * value.
591 		 *
592 		 * XXXLAS: Should there be some hysteresis for minrtt?
593 		 */
594 		if ((t_srtt_usecs < cubic_data->min_rtt_usecs ||
595 		    cubic_data->min_rtt_usecs == TCPTV_SRTTBASE)) {
596 			/* A minimal rtt is a single unshifted tick of a ticks
597 			 * timer. */
598 			cubic_data->min_rtt_usecs = max(tick >> TCP_RTT_SHIFT,
599 							t_srtt_usecs);
600 
601 			/*
602 			 * If the connection is within its first congestion
603 			 * epoch, ensure we prime mean_rtt_usecs with a
604 			 * reasonable value until the epoch average RTT is
605 			 * calculated in cubic_post_recovery().
606 			 */
607 			if (cubic_data->min_rtt_usecs >
608 			    cubic_data->mean_rtt_usecs)
609 				cubic_data->mean_rtt_usecs =
610 				    cubic_data->min_rtt_usecs;
611 		}
612 
613 		/* Sum samples for epoch average RTT calculation. */
614 		cubic_data->sum_rtt_usecs += t_srtt_usecs;
615 		cubic_data->epoch_ack_count++;
616 	}
617 }
618 
619 /*
620  * Update the ssthresh in the event of congestion.
621  */
622 static void
623 cubic_ssthresh_update(struct cc_var *ccv, uint32_t maxseg)
624 {
625 	struct cubic *cubic_data;
626 	uint32_t ssthresh;
627 	uint32_t cwnd;
628 
629 	cubic_data = ccv->cc_data;
630 	cwnd = CCV(ccv, snd_cwnd);
631 
632 	/* Fast convergence heuristic. */
633 	if (cwnd < cubic_data->W_max) {
634 		cwnd = ((uint64_t)cwnd * CUBIC_FC_FACTOR) >> CUBIC_SHIFT;
635 	}
636 	cubic_data->undo_W_max = cubic_data->W_max;
637 	cubic_data->W_max = cwnd;
638 
639 	/*
640 	 * On the first congestion event, set ssthresh to cwnd * 0.5
641 	 * and reduce W_max to cwnd * beta. This aligns the cubic concave
642 	 * region appropriately. On subsequent congestion events, set
643 	 * ssthresh to cwnd * beta.
644 	 */
645 	if ((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) {
646 		ssthresh = cwnd >> 1;
647 		cubic_data->W_max = ((uint64_t)cwnd *
648 		    CUBIC_BETA) >> CUBIC_SHIFT;
649 	} else {
650 		ssthresh = ((uint64_t)cwnd *
651 		    CUBIC_BETA) >> CUBIC_SHIFT;
652 	}
653 	CCV(ccv, snd_ssthresh) = max(ssthresh, 2 * maxseg);
654 }
655 
656 static void
657 cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt, uint32_t rxtcnt, uint32_t fas)
658 {
659 	struct cubic *cubicd;
660 
661 	cubicd = ccv->cc_data;
662 	if (rxtcnt > 1) {
663 		/*
664 		 * Only look at RTT's that are non-ambiguous.
665 		 */
666 		return;
667 	}
668 	cubicd->css_rttsample_count++;
669 	cubicd->css_last_fas = fas;
670 	if (cubicd->css_current_round_minrtt > usec_rtt) {
671 		cubicd->css_current_round_minrtt = usec_rtt;
672 		cubicd->css_lowrtt_fas = cubicd->css_last_fas;
673 	}
674 	if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) &&
675 	    (cubicd->css_current_round_minrtt != 0xffffffff) &&
676 	    (cubicd->css_current_round_minrtt < cubicd->css_baseline_minrtt) &&
677 	    (cubicd->css_lastround_minrtt != 0xffffffff)) {
678 		/*
679 		 * We were in CSS and the RTT is now less, we
680 		 * entered CSS erroneously.
681 		 */
682 		cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
683 		cubic_log_hystart_event(ccv, cubicd, 8, cubicd->css_baseline_minrtt);
684 		cubicd->css_baseline_minrtt = 0xffffffff;
685 	}
686 	if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED)
687 		cubic_log_hystart_event(ccv, cubicd, 5, usec_rtt);
688 }
689 
690 static void
691 cubic_newround(struct cc_var *ccv, uint32_t round_cnt)
692 {
693 	struct cubic *cubicd;
694 
695 	cubicd = ccv->cc_data;
696 	/* We have entered a new round */
697 	cubicd->css_lastround_minrtt = cubicd->css_current_round_minrtt;
698 	cubicd->css_current_round_minrtt = 0xffffffff;
699 	cubicd->css_rttsample_count = 0;
700 	cubicd->css_current_round = round_cnt;
701 	if ((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) &&
702 	    ((round_cnt - cubicd->css_entered_at_round) >= hystart_css_rounds)) {
703 		/* Enter CA */
704 		if (ccv->flags & CCF_HYSTART_CAN_SH_CWND) {
705 			/*
706 			 * We engage more than snd_ssthresh, engage
707 			 * the brakes!! Though we will stay in SS to
708 			 * creep back up again, so lets leave CSS active
709 			 * and give us hystart_css_rounds more rounds.
710 			 */
711 			if (ccv->flags & CCF_HYSTART_CONS_SSTH) {
712 				CCV(ccv, snd_ssthresh) = ((cubicd->css_lowrtt_fas + cubicd->css_fas_at_css_entry) / 2);
713 			} else {
714 				CCV(ccv, snd_ssthresh) = cubicd->css_lowrtt_fas;
715 			}
716 			CCV(ccv, snd_cwnd) = cubicd->css_fas_at_css_entry;
717 			cubicd->css_entered_at_round = round_cnt;
718 		} else {
719 			CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
720 			/* Turn off the CSS flag */
721 			cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
722 			/* Disable use of CSS in the future except long idle  */
723 			cubicd->flags &= ~CUBICFLAG_HYSTART_ENABLED;
724 		}
725 		cubic_log_hystart_event(ccv, cubicd, 6, CCV(ccv, snd_ssthresh));
726 	}
727 	if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED)
728 		cubic_log_hystart_event(ccv, cubicd, 4, round_cnt);
729 }
730 
731 DECLARE_CC_MODULE(cubic, &cubic_cc_algo);
732 MODULE_VERSION(cubic, 2);
733