xref: /freebsd/sys/netinet/cc/cc_newreno.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
5  *	The Regents of the University of California.
6  * Copyright (c) 2007-2008,2010,2014
7  *	Swinburne University of Technology, Melbourne, Australia.
8  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
9  * Copyright (c) 2010 The FreeBSD Foundation
10  * All rights reserved.
11  *
12  * This software was developed at the Centre for Advanced Internet
13  * Architectures, Swinburne University of Technology, by Lawrence Stewart, James
14  * Healy and David Hayes, made possible in part by a grant from the Cisco
15  * University Research Program Fund at Community Foundation Silicon Valley.
16  *
17  * Portions of this software were developed at the Centre for Advanced
18  * Internet Architectures, Swinburne University of Technology, Melbourne,
19  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  * 1. Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  * 2. Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in the
28  *    documentation and/or other materials provided with the distribution.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  */
42 
43 /*
44  * This software was first released in 2007 by James Healy and Lawrence Stewart
45  * whilst working on the NewTCP research project at Swinburne University of
46  * Technology's Centre for Advanced Internet Architectures, Melbourne,
47  * Australia, which was made possible in part by a grant from the Cisco
48  * University Research Program Fund at Community Foundation Silicon Valley.
49  * More details are available at:
50  *   http://caia.swin.edu.au/urp/newtcp/
51  *
52  * Dec 2014 garmitage@swin.edu.au
53  * Borrowed code fragments from cc_cdg.c to add modifiable beta
54  * via sysctls.
55  *
56  */
57 
58 #include <sys/cdefs.h>
59 #include <sys/param.h>
60 #include <sys/kernel.h>
61 #include <sys/malloc.h>
62 #include <sys/module.h>
63 #include <sys/socket.h>
64 #include <sys/lock.h>
65 #include <sys/mutex.h>
66 #include <sys/socketvar.h>
67 #include <sys/sysctl.h>
68 #include <sys/systm.h>
69 
70 #include <net/vnet.h>
71 
72 #include <net/route.h>
73 #include <net/route/nhop.h>
74 
75 #include <netinet/in_pcb.h>
76 #include <netinet/in.h>
77 #include <netinet/in_pcb.h>
78 #include <netinet/tcp.h>
79 #include <netinet/tcp_seq.h>
80 #include <netinet/tcp_var.h>
81 #include <netinet/tcp_log_buf.h>
82 #include <netinet/tcp_hpts.h>
83 #include <netinet/cc/cc.h>
84 #include <netinet/cc/cc_module.h>
85 #include <netinet/cc/cc_newreno.h>
86 
87 static void	newreno_cb_destroy(struct cc_var *ccv);
88 static void	newreno_ack_received(struct cc_var *ccv, uint16_t type);
89 static void	newreno_after_idle(struct cc_var *ccv);
90 static void	newreno_cong_signal(struct cc_var *ccv, uint32_t type);
91 static int newreno_ctl_output(struct cc_var *ccv, struct sockopt *sopt, void *buf);
92 static void	newreno_newround(struct cc_var *ccv, uint32_t round_cnt);
93 static void	newreno_rttsample(struct cc_var *ccv, uint32_t usec_rtt, uint32_t rxtcnt, uint32_t fas);
94 static 	int	newreno_cb_init(struct cc_var *ccv, void *);
95 static size_t	newreno_data_sz(void);
96 
97 
98 VNET_DECLARE(uint32_t, newreno_beta);
99 #define V_newreno_beta VNET(newreno_beta)
100 VNET_DECLARE(uint32_t, newreno_beta_ecn);
101 #define V_newreno_beta_ecn VNET(newreno_beta_ecn)
102 
103 struct cc_algo newreno_cc_algo = {
104 	.name = "newreno",
105 	.cb_destroy = newreno_cb_destroy,
106 	.ack_received = newreno_ack_received,
107 	.after_idle = newreno_after_idle,
108 	.cong_signal = newreno_cong_signal,
109 	.post_recovery = newreno_cc_post_recovery,
110 	.ctl_output = newreno_ctl_output,
111 	.newround = newreno_newround,
112 	.rttsample = newreno_rttsample,
113 	.cb_init = newreno_cb_init,
114 	.cc_data_sz = newreno_data_sz,
115 };
116 
117 static void
118 newreno_log_hystart_event(struct cc_var *ccv, struct newreno *nreno, uint8_t mod, uint32_t flex1)
119 {
120 	/*
121 	 * Types of logs (mod value)
122 	 * 1 - rtt_thresh in flex1, checking to see if RTT is to great.
123 	 * 2 - rtt is too great, rtt_thresh in flex1.
124 	 * 3 - CSS is active incr in flex1
125 	 * 4 - A new round is beginning flex1 is round count
126 	 * 5 - A new RTT measurement flex1 is the new measurement.
127 	 * 6 - We enter CA ssthresh is also in flex1.
128 	 * 7 - Socket option to change hystart executed opt.val in flex1.
129 	 * 8 - Back out of CSS into SS, flex1 is the css_baseline_minrtt
130 	 * 9 - We enter CA, via an ECN mark.
131 	 * 10 - We enter CA, via a loss.
132 	 * 11 - We have slipped out of SS into CA via cwnd growth.
133 	 * 12 - After idle has re-enabled hystart++
134 	 */
135 	struct tcpcb *tp;
136 
137 	if (hystart_bblogs == 0)
138 		return;
139 	tp = ccv->ccvc.tcp;
140 	if (tcp_bblogging_on(tp)) {
141 		union tcp_log_stackspecific log;
142 		struct timeval tv;
143 
144 		memset(&log, 0, sizeof(log));
145 		log.u_bbr.flex1 = flex1;
146 		log.u_bbr.flex2 = nreno->css_current_round_minrtt;
147 		log.u_bbr.flex3 = nreno->css_lastround_minrtt;
148 		log.u_bbr.flex4 = nreno->css_rttsample_count;
149 		log.u_bbr.flex5 = nreno->css_entered_at_round;
150 		log.u_bbr.flex6 = nreno->css_baseline_minrtt;
151 		/* We only need bottom 16 bits of flags */
152 		log.u_bbr.flex7 = nreno->newreno_flags & 0x0000ffff;
153 		log.u_bbr.flex8 = mod;
154 		log.u_bbr.epoch = nreno->css_current_round;
155 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
156 		log.u_bbr.lt_epoch = nreno->css_fas_at_css_entry;
157 		log.u_bbr.pkts_out = nreno->css_last_fas;
158 		log.u_bbr.delivered = nreno->css_lowrtt_fas;
159 		log.u_bbr.pkt_epoch = ccv->flags;
160 		TCP_LOG_EVENTP(tp, NULL,
161 		    &tptosocket(tp)->so_rcv,
162 		    &tptosocket(tp)->so_snd,
163 		    TCP_HYSTART, 0,
164 		    0, &log, false, &tv);
165 	}
166 }
167 
168 static size_t
169 newreno_data_sz(void)
170 {
171 	return (sizeof(struct newreno));
172 }
173 
174 static int
175 newreno_cb_init(struct cc_var *ccv, void *ptr)
176 {
177 	struct newreno *nreno;
178 
179 	INP_WLOCK_ASSERT(tptoinpcb(ccv->ccvc.tcp));
180 	if (ptr == NULL) {
181 		ccv->cc_data = malloc(sizeof(struct newreno), M_CC_MEM, M_NOWAIT);
182 		if (ccv->cc_data == NULL)
183 			return (ENOMEM);
184 	} else
185 		ccv->cc_data = ptr;
186 	nreno = (struct newreno *)ccv->cc_data;
187 	/* NB: nreno is not zeroed, so initialise all fields. */
188 	nreno->beta = V_newreno_beta;
189 	nreno->beta_ecn = V_newreno_beta_ecn;
190 	/*
191 	 * We set the enabled flag so that if
192 	 * the socket option gets strobed and
193 	 * we have not hit a loss
194 	 */
195 	nreno->newreno_flags = CC_NEWRENO_HYSTART_ENABLED;
196 	/* At init set both to infinity */
197 	nreno->css_lastround_minrtt = 0xffffffff;
198 	nreno->css_current_round_minrtt = 0xffffffff;
199 	nreno->css_current_round = 0;
200 	nreno->css_baseline_minrtt = 0xffffffff;
201 	nreno->css_rttsample_count = 0;
202 	nreno->css_entered_at_round = 0;
203 	nreno->css_fas_at_css_entry = 0;
204 	nreno->css_lowrtt_fas = 0;
205 	nreno->css_last_fas = 0;
206 	return (0);
207 }
208 
209 static void
210 newreno_cb_destroy(struct cc_var *ccv)
211 {
212 	free(ccv->cc_data, M_CC_MEM);
213 }
214 
215 static void
216 newreno_ack_received(struct cc_var *ccv, uint16_t type)
217 {
218 	struct newreno *nreno;
219 
220 	nreno = ccv->cc_data;
221 	if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
222 	    (ccv->flags & CCF_CWND_LIMITED)) {
223 		u_int cw = CCV(ccv, snd_cwnd);
224 		u_int incr = CCV(ccv, t_maxseg);
225 
226 		/*
227 		 * Regular in-order ACK, open the congestion window.
228 		 * Method depends on which congestion control state we're
229 		 * in (slow start or cong avoid) and if ABC (RFC 3465) is
230 		 * enabled.
231 		 *
232 		 * slow start: cwnd <= ssthresh
233 		 * cong avoid: cwnd > ssthresh
234 		 *
235 		 * slow start and ABC (RFC 3465):
236 		 *   Grow cwnd exponentially by the amount of data
237 		 *   ACKed capping the max increment per ACK to
238 		 *   (abc_l_var * maxseg) bytes.
239 		 *
240 		 * slow start without ABC (RFC 5681):
241 		 *   Grow cwnd exponentially by maxseg per ACK.
242 		 *
243 		 * cong avoid and ABC (RFC 3465):
244 		 *   Grow cwnd linearly by maxseg per RTT for each
245 		 *   cwnd worth of ACKed data.
246 		 *
247 		 * cong avoid without ABC (RFC 5681):
248 		 *   Grow cwnd linearly by approximately maxseg per RTT using
249 		 *   maxseg^2 / cwnd per ACK as the increment.
250 		 *   If cwnd > maxseg^2, fix the cwnd increment at 1 byte to
251 		 *   avoid capping cwnd.
252 		 */
253 		if (cw > CCV(ccv, snd_ssthresh)) {
254 			if (nreno->newreno_flags & CC_NEWRENO_HYSTART_IN_CSS) {
255 				/*
256 				 * We have slipped into CA with
257 				 * CSS active. Deactivate all.
258 				 */
259 				/* Turn off the CSS flag */
260 				nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
261 				/* Disable use of CSS in the future except long idle  */
262 				nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_ENABLED;
263 				newreno_log_hystart_event(ccv, nreno, 11, CCV(ccv, snd_ssthresh));
264 			}
265 			if (V_tcp_do_rfc3465) {
266 				if (ccv->flags & CCF_ABC_SENTAWND)
267 					ccv->flags &= ~CCF_ABC_SENTAWND;
268 				else
269 					incr = 0;
270 			} else
271 				incr = max((incr * incr / cw), 1);
272 		} else if (V_tcp_do_rfc3465) {
273 			/*
274 			 * In slow-start with ABC enabled and no RTO in sight?
275 			 * (Must not use abc_l_var > 1 if slow starting after
276 			 * an RTO. On RTO, snd_nxt = snd_una, so the
277 			 * snd_nxt == snd_max check is sufficient to
278 			 * handle this).
279 			 *
280 			 * XXXLAS: Find a way to signal SS after RTO that
281 			 * doesn't rely on tcpcb vars.
282 			 */
283 			uint16_t abc_val;
284 
285 			if (ccv->flags & CCF_USE_LOCAL_ABC)
286 				abc_val = ccv->labc;
287 			else
288 				abc_val = V_tcp_abc_l_var;
289 			if ((ccv->flags & CCF_HYSTART_ALLOWED) &&
290 			    (nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED) &&
291 			    ((nreno->newreno_flags & CC_NEWRENO_HYSTART_IN_CSS) == 0)) {
292 				/*
293 				 * Hystart is allowed and still enabled and we are not yet
294 				 * in CSS. Lets check to see if we can make a decision on
295 				 * if we need to go into CSS.
296 				 */
297 				if ((nreno->css_rttsample_count >= hystart_n_rttsamples) &&
298 				    (nreno->css_current_round_minrtt != 0xffffffff) &&
299 				    (nreno->css_lastround_minrtt != 0xffffffff)) {
300 					uint32_t rtt_thresh;
301 
302 					/* Clamp (minrtt_thresh, lastround/8, maxrtt_thresh) */
303 					rtt_thresh = (nreno->css_lastround_minrtt >> 3);
304 					if (rtt_thresh < hystart_minrtt_thresh)
305 						rtt_thresh = hystart_minrtt_thresh;
306 					if (rtt_thresh > hystart_maxrtt_thresh)
307 						rtt_thresh = hystart_maxrtt_thresh;
308 					newreno_log_hystart_event(ccv, nreno, 1, rtt_thresh);
309 					if (nreno->css_current_round_minrtt >= (nreno->css_lastround_minrtt + rtt_thresh)) {
310 						/* Enter CSS */
311 						nreno->newreno_flags |= CC_NEWRENO_HYSTART_IN_CSS;
312 						nreno->css_fas_at_css_entry = nreno->css_lowrtt_fas;
313 						/*
314 						 * The draft (v4) calls for us to set baseline to css_current_round_min
315 						 * but that can cause an oscillation. We probably shoudl be using
316 						 * css_lastround_minrtt, but the authors insist that will cause
317 						 * issues on exiting early. We will leave the draft version for now
318 						 * but I suspect this is incorrect.
319 						 */
320 						nreno->css_baseline_minrtt = nreno->css_current_round_minrtt;
321 						nreno->css_entered_at_round = nreno->css_current_round;
322 						newreno_log_hystart_event(ccv, nreno, 2, rtt_thresh);
323 					}
324 				}
325 			}
326 			if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max))
327 				incr = min(ccv->bytes_this_ack,
328 				    ccv->nsegs * abc_val *
329 				    CCV(ccv, t_maxseg));
330 			else
331 				incr = min(ccv->bytes_this_ack, CCV(ccv, t_maxseg));
332 
333 			/* Only if Hystart is enabled will the flag get set */
334 			if (nreno->newreno_flags & CC_NEWRENO_HYSTART_IN_CSS) {
335 				incr /= hystart_css_growth_div;
336 				newreno_log_hystart_event(ccv, nreno, 3, incr);
337 			}
338 		}
339 		/* ABC is on by default, so incr equals 0 frequently. */
340 		if (incr > 0)
341 			CCV(ccv, snd_cwnd) = min(cw + incr,
342 			    TCP_MAXWIN << CCV(ccv, snd_scale));
343 	}
344 }
345 
346 static void
347 newreno_after_idle(struct cc_var *ccv)
348 {
349 	struct newreno *nreno;
350 
351 	nreno = ccv->cc_data;
352 	newreno_cc_after_idle(ccv);
353 	if ((nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED) == 0) {
354 		/*
355 		 * Re-enable hystart if we have been idle.
356 		 */
357 		nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
358 		nreno->newreno_flags |= CC_NEWRENO_HYSTART_ENABLED;
359 		newreno_log_hystart_event(ccv, nreno, 12, CCV(ccv, snd_ssthresh));
360 	}
361 }
362 
363 /*
364  * Perform any necessary tasks before we enter congestion recovery.
365  */
366 static void
367 newreno_cong_signal(struct cc_var *ccv, uint32_t type)
368 {
369 	struct newreno *nreno;
370 	uint32_t beta, beta_ecn, cwin, factor;
371 	u_int mss;
372 
373 	cwin = CCV(ccv, snd_cwnd);
374 	mss = tcp_fixed_maxseg(ccv->ccvc.tcp);
375 	nreno = ccv->cc_data;
376 	beta = (nreno == NULL) ? V_newreno_beta : nreno->beta;;
377 	beta_ecn = (nreno == NULL) ? V_newreno_beta_ecn : nreno->beta_ecn;
378 	/*
379 	 * Note that we only change the backoff for ECN if the
380 	 * global sysctl V_cc_do_abe is set <or> the stack itself
381 	 * has set a flag in our newreno_flags (due to pacing) telling
382 	 * us to use the lower valued back-off.
383 	 */
384 	if ((type == CC_ECN) &&
385 	    (V_cc_do_abe ||
386 	    ((nreno != NULL) && (nreno->newreno_flags & CC_NEWRENO_BETA_ECN_ENABLED))))
387 		factor = beta_ecn;
388 	else
389 		factor = beta;
390 
391 	/* Catch algos which mistakenly leak private signal types. */
392 	KASSERT((type & CC_SIGPRIVMASK) == 0,
393 	    ("%s: congestion signal type 0x%08x is private\n", __func__, type));
394 
395 	cwin = max(((uint64_t)cwin * (uint64_t)factor) / (100ULL * (uint64_t)mss),
396 	    2) * mss;
397 
398 	switch (type) {
399 	case CC_NDUPACK:
400 		if (nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED) {
401 			/* Make sure the flags are all off we had a loss */
402 			nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_ENABLED;
403 			nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
404 			newreno_log_hystart_event(ccv, nreno, 10, CCV(ccv, snd_ssthresh));
405 		}
406 		if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
407 			if (IN_CONGRECOVERY(CCV(ccv, t_flags) &&
408 			    V_cc_do_abe && V_cc_abe_frlossreduce)) {
409 				CCV(ccv, snd_ssthresh) =
410 				    ((uint64_t)CCV(ccv, snd_ssthresh) *
411 				     (uint64_t)beta) / (uint64_t)beta_ecn;
412 			}
413 			if (!IN_CONGRECOVERY(CCV(ccv, t_flags)))
414 				CCV(ccv, snd_ssthresh) = cwin;
415 			ENTER_RECOVERY(CCV(ccv, t_flags));
416 		}
417 		break;
418 	case CC_ECN:
419 		if (nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED) {
420 			/* Make sure the flags are all off we had a loss */
421 			nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_ENABLED;
422 			nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
423 			newreno_log_hystart_event(ccv, nreno, 9, CCV(ccv, snd_ssthresh));
424 		}
425 		if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
426 			CCV(ccv, snd_ssthresh) = cwin;
427 			CCV(ccv, snd_cwnd) = cwin;
428 			ENTER_CONGRECOVERY(CCV(ccv, t_flags));
429 		}
430 		break;
431 	case CC_RTO:
432 		CCV(ccv, snd_ssthresh) = max(min(CCV(ccv, snd_wnd),
433 						 CCV(ccv, snd_cwnd)) / 2 / mss,
434 					     2) * mss;
435 		CCV(ccv, snd_cwnd) = mss;
436 		break;
437 	}
438 }
439 
440 static int
441 newreno_ctl_output(struct cc_var *ccv, struct sockopt *sopt, void *buf)
442 {
443 	struct newreno *nreno;
444 	struct cc_newreno_opts *opt;
445 
446 	if (sopt->sopt_valsize != sizeof(struct cc_newreno_opts))
447 		return (EMSGSIZE);
448 
449 	if (CC_ALGO(ccv->ccvc.tcp) != &newreno_cc_algo)
450 		return (ENOPROTOOPT);
451 
452 	nreno = (struct newreno *)ccv->cc_data;
453 	opt = buf;
454 	switch (sopt->sopt_dir) {
455 	case SOPT_SET:
456 		switch (opt->name) {
457 		case CC_NEWRENO_BETA:
458 			nreno->beta = opt->val;
459 			break;
460 		case CC_NEWRENO_BETA_ECN:
461 			nreno->beta_ecn = opt->val;
462 			nreno->newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED;
463 			break;
464 		default:
465 			return (ENOPROTOOPT);
466 		}
467 		break;
468 	case SOPT_GET:
469 		switch (opt->name) {
470 		case CC_NEWRENO_BETA:
471 			opt->val =  nreno->beta;
472 			break;
473 		case CC_NEWRENO_BETA_ECN:
474 			opt->val = nreno->beta_ecn;
475 			break;
476 		default:
477 			return (ENOPROTOOPT);
478 		}
479 		break;
480 	default:
481 		return (EINVAL);
482 	}
483 
484 	return (0);
485 }
486 
487 static int
488 newreno_beta_handler(SYSCTL_HANDLER_ARGS)
489 {
490 	int error;
491 	uint32_t new;
492 
493 	new = *(uint32_t *)arg1;
494 	error = sysctl_handle_int(oidp, &new, 0, req);
495 	if (error == 0 && req->newptr != NULL ) {
496 		if (arg1 == &VNET_NAME(newreno_beta_ecn) && !V_cc_do_abe)
497 			error = EACCES;
498 		else if (new == 0 || new > 100)
499 			error = EINVAL;
500 		else
501 			*(uint32_t *)arg1 = new;
502 	}
503 
504 	return (error);
505 }
506 
507 static void
508 newreno_newround(struct cc_var *ccv, uint32_t round_cnt)
509 {
510 	struct newreno *nreno;
511 
512 	nreno = (struct newreno *)ccv->cc_data;
513 	/* We have entered a new round */
514 	nreno->css_lastround_minrtt = nreno->css_current_round_minrtt;
515 	nreno->css_current_round_minrtt = 0xffffffff;
516 	nreno->css_rttsample_count = 0;
517 	nreno->css_current_round = round_cnt;
518 	if ((nreno->newreno_flags & CC_NEWRENO_HYSTART_IN_CSS) &&
519 	    ((round_cnt - nreno->css_entered_at_round) >= hystart_css_rounds)) {
520 		/* Enter CA */
521 		if (ccv->flags & CCF_HYSTART_CAN_SH_CWND) {
522 			/*
523 			 * We engage more than snd_ssthresh, engage
524 			 * the brakes!! Though we will stay in SS to
525 			 * creep back up again, so lets leave CSS active
526 			 * and give us hystart_css_rounds more rounds.
527 			 */
528 			if (ccv->flags & CCF_HYSTART_CONS_SSTH) {
529 				CCV(ccv, snd_ssthresh) = ((nreno->css_lowrtt_fas + nreno->css_fas_at_css_entry) / 2);
530 			} else {
531 				CCV(ccv, snd_ssthresh) = nreno->css_lowrtt_fas;
532 			}
533 			CCV(ccv, snd_cwnd) = nreno->css_fas_at_css_entry;
534 			nreno->css_entered_at_round = round_cnt;
535 		} else {
536 			CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
537 			/* Turn off the CSS flag */
538 			nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
539 			/* Disable use of CSS in the future except long idle  */
540 			nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_ENABLED;
541 		}
542 		newreno_log_hystart_event(ccv, nreno, 6, CCV(ccv, snd_ssthresh));
543 	}
544 	if (nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED)
545 		newreno_log_hystart_event(ccv, nreno, 4, round_cnt);
546 }
547 
548 static void
549 newreno_rttsample(struct cc_var *ccv, uint32_t usec_rtt, uint32_t rxtcnt, uint32_t fas)
550 {
551 	struct newreno *nreno;
552 
553 	nreno = (struct newreno *)ccv->cc_data;
554 	if (rxtcnt > 1) {
555 		/*
556 		 * Only look at RTT's that are non-ambiguous.
557 		 */
558 		return;
559 	}
560 	nreno->css_rttsample_count++;
561 	nreno->css_last_fas = fas;
562 	if (nreno->css_current_round_minrtt > usec_rtt) {
563 		nreno->css_current_round_minrtt = usec_rtt;
564 		nreno->css_lowrtt_fas = nreno->css_last_fas;
565 	}
566 	if ((nreno->css_rttsample_count >= hystart_n_rttsamples) &&
567 	    (nreno->css_current_round_minrtt != 0xffffffff) &&
568 	    (nreno->css_current_round_minrtt < nreno->css_baseline_minrtt) &&
569 	    (nreno->css_lastround_minrtt != 0xffffffff)) {
570 		/*
571 		 * We were in CSS and the RTT is now less, we
572 		 * entered CSS erroneously.
573 		 */
574 		nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
575 		newreno_log_hystart_event(ccv, nreno, 8, nreno->css_baseline_minrtt);
576 		nreno->css_baseline_minrtt = 0xffffffff;
577 	}
578 	if (nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED)
579 		newreno_log_hystart_event(ccv, nreno, 5, usec_rtt);
580 }
581 
582 SYSCTL_DECL(_net_inet_tcp_cc_newreno);
583 SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, newreno,
584     CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
585     "New Reno related settings");
586 
587 SYSCTL_PROC(_net_inet_tcp_cc_newreno, OID_AUTO, beta,
588     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
589     &VNET_NAME(newreno_beta), 3, &newreno_beta_handler, "IU",
590     "New Reno beta, specified as number between 1 and 100");
591 
592 SYSCTL_PROC(_net_inet_tcp_cc_newreno, OID_AUTO, beta_ecn,
593     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
594     &VNET_NAME(newreno_beta_ecn), 3, &newreno_beta_handler, "IU",
595     "New Reno beta ecn, specified as number between 1 and 100");
596 
597 DECLARE_CC_MODULE(newreno, &newreno_cc_algo);
598 MODULE_VERSION(newreno, 2);
599