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