xref: /freebsd/sys/netinet/cc/cc_newreno.c (revision 848ee2a3a8b47c9905fc51fefcf60eb371edbb98)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
60 
61 #include <sys/param.h>
62 #include <sys/kernel.h>
63 #include <sys/malloc.h>
64 #include <sys/module.h>
65 #include <sys/socket.h>
66 #include <sys/lock.h>
67 #include <sys/mutex.h>
68 #include <sys/socketvar.h>
69 #include <sys/sysctl.h>
70 #include <sys/systm.h>
71 
72 #include <net/vnet.h>
73 
74 #include <net/route.h>
75 #include <net/route/nhop.h>
76 
77 #include <netinet/in_pcb.h>
78 #include <netinet/in.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/tcp.h>
81 #include <netinet/tcp_seq.h>
82 #include <netinet/tcp_var.h>
83 #include <netinet/tcp_log_buf.h>
84 #include <netinet/tcp_hpts.h>
85 #include <netinet/cc/cc.h>
86 #include <netinet/cc/cc_module.h>
87 #include <netinet/cc/cc_newreno.h>
88 
89 static void	newreno_cb_destroy(struct cc_var *ccv);
90 static void	newreno_ack_received(struct cc_var *ccv, uint16_t type);
91 static void	newreno_after_idle(struct cc_var *ccv);
92 static void	newreno_cong_signal(struct cc_var *ccv, uint32_t type);
93 static int newreno_ctl_output(struct cc_var *ccv, struct sockopt *sopt, void *buf);
94 static void	newreno_newround(struct cc_var *ccv, uint32_t round_cnt);
95 static void	newreno_rttsample(struct cc_var *ccv, uint32_t usec_rtt, uint32_t rxtcnt, uint32_t fas);
96 static 	int	newreno_cb_init(struct cc_var *ccv, void *);
97 static size_t	newreno_data_sz(void);
98 
99 
100 VNET_DECLARE(uint32_t, newreno_beta);
101 #define V_newreno_beta VNET(newreno_beta)
102 VNET_DEFINE(uint32_t, newreno_beta_ecn) = 80;
103 #define V_newreno_beta_ecn VNET(newreno_beta_ecn)
104 
105 struct cc_algo newreno_cc_algo = {
106 	.name = "newreno",
107 	.cb_destroy = newreno_cb_destroy,
108 	.ack_received = newreno_ack_received,
109 	.after_idle = newreno_after_idle,
110 	.cong_signal = newreno_cong_signal,
111 	.post_recovery = newreno_cc_post_recovery,
112 	.ctl_output = newreno_ctl_output,
113 	.newround = newreno_newround,
114 	.rttsample = newreno_rttsample,
115 	.cb_init = newreno_cb_init,
116 	.cc_data_sz = newreno_data_sz,
117 };
118 
119 static uint32_t hystart_lowcwnd = 16;
120 static uint32_t hystart_minrtt_thresh = 4000;
121 static uint32_t hystart_maxrtt_thresh = 16000;
122 static uint32_t hystart_n_rttsamples = 8;
123 static uint32_t hystart_css_growth_div = 4;
124 static uint32_t hystart_css_rounds = 5;
125 static uint32_t hystart_bblogs = 0;
126 
127 static void
128 newreno_log_hystart_event(struct cc_var *ccv, struct newreno *nreno, uint8_t mod, uint32_t flex1)
129 {
130 	/*
131 	 * Types of logs (mod value)
132 	 * 1 - rtt_thresh in flex1, checking to see if RTT is to great.
133 	 * 2 - rtt is too great, rtt_thresh in flex1.
134 	 * 3 - CSS is active incr in flex1
135 	 * 4 - A new round is beginning flex1 is round count
136 	 * 5 - A new RTT measurement flex1 is the new measurement.
137 	 * 6 - We enter CA ssthresh is also in flex1.
138 	 * 7 - Socket option to change hystart executed opt.val in flex1.
139 	 * 8 - Back out of CSS into SS, flex1 is the css_baseline_minrtt
140 	 */
141 	struct tcpcb *tp;
142 
143 	if (hystart_bblogs == 0)
144 		return;
145 	tp = ccv->ccvc.tcp;
146 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
147 		union tcp_log_stackspecific log;
148 		struct timeval tv;
149 
150 		memset(&log, 0, sizeof(log));
151 		log.u_bbr.flex1 = flex1;
152 		log.u_bbr.flex2 = nreno->css_current_round_minrtt;
153 		log.u_bbr.flex3 = nreno->css_lastround_minrtt;
154 		log.u_bbr.flex4 = nreno->css_rttsample_count;
155 		log.u_bbr.flex5 = nreno->css_entered_at_round;
156 		log.u_bbr.flex6 = nreno->css_baseline_minrtt;
157 		/* We only need bottom 16 bits of flags */
158 		log.u_bbr.flex7 = nreno->newreno_flags & 0x0000ffff;
159 		log.u_bbr.flex8 = mod;
160 		log.u_bbr.epoch = nreno->css_current_round;
161 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
162 		log.u_bbr.lt_epoch = nreno->css_fas_at_css_entry;
163 		log.u_bbr.pkts_out = nreno->css_last_fas;
164 		log.u_bbr.delivered = nreno->css_lowrtt_fas;
165 		TCP_LOG_EVENTP(tp, NULL,
166 		    &tp->t_inpcb->inp_socket->so_rcv,
167 		    &tp->t_inpcb->inp_socket->so_snd,
168 		    TCP_HYSTART, 0,
169 		    0, &log, false, &tv);
170 	}
171 }
172 
173 static size_t
174 newreno_data_sz(void)
175 {
176 	return (sizeof(struct newreno));
177 }
178 
179 static int
180 newreno_cb_init(struct cc_var *ccv, void *ptr)
181 {
182 	struct newreno *nreno;
183 
184 	INP_WLOCK_ASSERT(ccv->ccvc.tcp->t_inpcb);
185 	if (ptr == NULL) {
186 		ccv->cc_data = malloc(sizeof(struct newreno), M_CC_MEM, M_NOWAIT);
187 		if (ccv->cc_data == NULL)
188 			return (ENOMEM);
189 	} else
190 		ccv->cc_data = ptr;
191 	nreno = (struct newreno *)ccv->cc_data;
192 	/* NB: nreno is not zeroed, so initialise all fields. */
193 	nreno->beta = V_newreno_beta;
194 	nreno->beta_ecn = V_newreno_beta_ecn;
195 	/*
196 	 * We set the enabled flag so that if
197 	 * the socket option gets strobed and
198 	 * we have not hit a loss
199 	 */
200 	nreno->newreno_flags = CC_NEWRENO_HYSTART_ENABLED;
201 	/* At init set both to infinity */
202 	nreno->css_lastround_minrtt = 0xffffffff;
203 	nreno->css_current_round_minrtt = 0xffffffff;
204 	nreno->css_current_round = 0;
205 	nreno->css_baseline_minrtt = 0xffffffff;
206 	nreno->css_rttsample_count = 0;
207 	nreno->css_entered_at_round = 0;
208 	nreno->css_fas_at_css_entry = 0;
209 	nreno->css_lowrtt_fas = 0;
210 	nreno->css_last_fas = 0;
211 	return (0);
212 }
213 
214 static void
215 newreno_cb_destroy(struct cc_var *ccv)
216 {
217 	free(ccv->cc_data, M_CC_MEM);
218 }
219 
220 static void
221 newreno_ack_received(struct cc_var *ccv, uint16_t type)
222 {
223 	struct newreno *nreno;
224 
225 	nreno = ccv->cc_data;
226 	if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
227 	    (ccv->flags & CCF_CWND_LIMITED)) {
228 		u_int cw = CCV(ccv, snd_cwnd);
229 		u_int incr = CCV(ccv, t_maxseg);
230 
231 		/*
232 		 * Regular in-order ACK, open the congestion window.
233 		 * Method depends on which congestion control state we're
234 		 * in (slow start or cong avoid) and if ABC (RFC 3465) is
235 		 * enabled.
236 		 *
237 		 * slow start: cwnd <= ssthresh
238 		 * cong avoid: cwnd > ssthresh
239 		 *
240 		 * slow start and ABC (RFC 3465):
241 		 *   Grow cwnd exponentially by the amount of data
242 		 *   ACKed capping the max increment per ACK to
243 		 *   (abc_l_var * maxseg) bytes.
244 		 *
245 		 * slow start without ABC (RFC 5681):
246 		 *   Grow cwnd exponentially by maxseg per ACK.
247 		 *
248 		 * cong avoid and ABC (RFC 3465):
249 		 *   Grow cwnd linearly by maxseg per RTT for each
250 		 *   cwnd worth of ACKed data.
251 		 *
252 		 * cong avoid without ABC (RFC 5681):
253 		 *   Grow cwnd linearly by approximately maxseg per RTT using
254 		 *   maxseg^2 / cwnd per ACK as the increment.
255 		 *   If cwnd > maxseg^2, fix the cwnd increment at 1 byte to
256 		 *   avoid capping cwnd.
257 		 */
258 		if (cw > CCV(ccv, snd_ssthresh)) {
259 			if (nreno->newreno_flags & CC_NEWRENO_HYSTART_IN_CSS) {
260 				/*
261 				 * We have slipped into CA with
262 				 * CSS active. Deactivate all.
263 				 */
264 				/* Turn off the CSS flag */
265 				nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
266 				/* Disable use of CSS in the future except long idle  */
267 				nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_ENABLED;
268 			}
269 			if (V_tcp_do_rfc3465) {
270 				if (ccv->flags & CCF_ABC_SENTAWND)
271 					ccv->flags &= ~CCF_ABC_SENTAWND;
272 				else
273 					incr = 0;
274 			} else
275 				incr = max((incr * incr / cw), 1);
276 		} else if (V_tcp_do_rfc3465) {
277 			/*
278 			 * In slow-start with ABC enabled and no RTO in sight?
279 			 * (Must not use abc_l_var > 1 if slow starting after
280 			 * an RTO. On RTO, snd_nxt = snd_una, so the
281 			 * snd_nxt == snd_max check is sufficient to
282 			 * handle this).
283 			 *
284 			 * XXXLAS: Find a way to signal SS after RTO that
285 			 * doesn't rely on tcpcb vars.
286 			 */
287 			uint16_t abc_val;
288 
289 			if (ccv->flags & CCF_USE_LOCAL_ABC)
290 				abc_val = ccv->labc;
291 			else
292 				abc_val = V_tcp_abc_l_var;
293 			if ((nreno->newreno_flags & CC_NEWRENO_HYSTART_ALLOWED) &&
294 			    (nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED) &&
295 			    ((nreno->newreno_flags & CC_NEWRENO_HYSTART_IN_CSS) == 0)) {
296 				/*
297 				 * Hystart is allowed and still enabled and we are not yet
298 				 * in CSS. Lets check to see if we can make a decision on
299 				 * if we need to go into CSS.
300 				 */
301 				if ((nreno->css_rttsample_count >= hystart_n_rttsamples) &&
302 				    (CCV(ccv, snd_cwnd) >
303 				     (hystart_lowcwnd * tcp_fixed_maxseg(ccv->ccvc.tcp)))) {
304 					uint32_t rtt_thresh;
305 
306 					/* Clamp (minrtt_thresh, lastround/8, maxrtt_thresh) */
307 					rtt_thresh = (nreno->css_lastround_minrtt >> 3);
308 					if (rtt_thresh < hystart_minrtt_thresh)
309 						rtt_thresh = hystart_minrtt_thresh;
310 					if (rtt_thresh > hystart_maxrtt_thresh)
311 						rtt_thresh = hystart_maxrtt_thresh;
312 					newreno_log_hystart_event(ccv, nreno, 1, rtt_thresh);
313 					if (nreno->css_current_round_minrtt >= (nreno->css_lastround_minrtt + rtt_thresh)) {
314 						/* Enter CSS */
315 						nreno->newreno_flags |= CC_NEWRENO_HYSTART_IN_CSS;
316 						nreno->css_fas_at_css_entry = nreno->css_lowrtt_fas;
317 						nreno->css_baseline_minrtt = nreno->css_current_round_minrtt;
318 						nreno->css_entered_at_round = nreno->css_current_round;
319 						newreno_log_hystart_event(ccv, nreno, 2, rtt_thresh);
320 					}
321 				}
322 			}
323 			if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max))
324 				incr = min(ccv->bytes_this_ack,
325 				    ccv->nsegs * abc_val *
326 				    CCV(ccv, t_maxseg));
327 			else
328 				incr = min(ccv->bytes_this_ack, CCV(ccv, t_maxseg));
329 
330 			/* Only if Hystart is enabled will the flag get set */
331 			if (nreno->newreno_flags & CC_NEWRENO_HYSTART_IN_CSS) {
332 				incr /= hystart_css_growth_div;
333 				newreno_log_hystart_event(ccv, nreno, 3, incr);
334 			}
335 		}
336 		/* ABC is on by default, so incr equals 0 frequently. */
337 		if (incr > 0)
338 			CCV(ccv, snd_cwnd) = min(cw + incr,
339 			    TCP_MAXWIN << CCV(ccv, snd_scale));
340 	}
341 }
342 
343 static void
344 newreno_after_idle(struct cc_var *ccv)
345 {
346 	struct newreno *nreno;
347 
348 	nreno = ccv->cc_data;
349 	newreno_cc_after_idle(ccv);
350 	if ((nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED) == 0) {
351 		if (CCV(ccv, snd_cwnd) <= (hystart_lowcwnd * tcp_fixed_maxseg(ccv->ccvc.tcp))) {
352 			/*
353 			 * Re-enable hystart if our cwnd has fallen below
354 			 * the hystart lowcwnd point.
355 			 */
356 			nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
357 			nreno->newreno_flags |= CC_NEWRENO_HYSTART_ENABLED;
358 		}
359 	}
360 }
361 
362 /*
363  * Perform any necessary tasks before we enter congestion recovery.
364  */
365 static void
366 newreno_cong_signal(struct cc_var *ccv, uint32_t type)
367 {
368 	struct newreno *nreno;
369 	uint32_t beta, beta_ecn, cwin, factor;
370 	u_int mss;
371 
372 	cwin = CCV(ccv, snd_cwnd);
373 	mss = tcp_fixed_maxseg(ccv->ccvc.tcp);
374 	nreno = ccv->cc_data;
375 	beta = (nreno == NULL) ? V_newreno_beta : nreno->beta;;
376 	beta_ecn = (nreno == NULL) ? V_newreno_beta_ecn : nreno->beta_ecn;
377 	/*
378 	 * Note that we only change the backoff for ECN if the
379 	 * global sysctl V_cc_do_abe is set <or> the stack itself
380 	 * has set a flag in our newreno_flags (due to pacing) telling
381 	 * us to use the lower valued back-off.
382 	 */
383 	if ((type == CC_ECN) &&
384 	    (V_cc_do_abe ||
385 	    ((nreno != NULL) && (nreno->newreno_flags & CC_NEWRENO_BETA_ECN_ENABLED))))
386 		factor = beta_ecn;
387 	else
388 		factor = beta;
389 
390 	/* Catch algos which mistakenly leak private signal types. */
391 	KASSERT((type & CC_SIGPRIVMASK) == 0,
392 	    ("%s: congestion signal type 0x%08x is private\n", __func__, type));
393 
394 	cwin = max(((uint64_t)cwin * (uint64_t)factor) / (100ULL * (uint64_t)mss),
395 	    2) * mss;
396 
397 	switch (type) {
398 	case CC_NDUPACK:
399 		if (nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED) {
400 			/* Make sure the flags are all off we had a loss */
401 			nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_ENABLED;
402 			nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
403 		}
404 		if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
405 			if (IN_CONGRECOVERY(CCV(ccv, t_flags) &&
406 			    V_cc_do_abe && V_cc_abe_frlossreduce)) {
407 				CCV(ccv, snd_ssthresh) =
408 				    ((uint64_t)CCV(ccv, snd_ssthresh) *
409 				     (uint64_t)beta) / (uint64_t)beta_ecn;
410 			}
411 			if (!IN_CONGRECOVERY(CCV(ccv, t_flags)))
412 				CCV(ccv, snd_ssthresh) = cwin;
413 			ENTER_RECOVERY(CCV(ccv, t_flags));
414 		}
415 		break;
416 	case CC_ECN:
417 		if (nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED) {
418 			/* Make sure the flags are all off we had a loss */
419 			nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_ENABLED;
420 			nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
421 		}
422 		if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
423 			CCV(ccv, snd_ssthresh) = cwin;
424 			CCV(ccv, snd_cwnd) = cwin;
425 			ENTER_CONGRECOVERY(CCV(ccv, t_flags));
426 		}
427 		break;
428 	case CC_RTO:
429 		CCV(ccv, snd_ssthresh) = max(min(CCV(ccv, snd_wnd),
430 						 CCV(ccv, snd_cwnd)) / 2 / mss,
431 					     2) * mss;
432 		CCV(ccv, snd_cwnd) = mss;
433 		break;
434 	}
435 }
436 
437 static int
438 newreno_ctl_output(struct cc_var *ccv, struct sockopt *sopt, void *buf)
439 {
440 	struct newreno *nreno;
441 	struct cc_newreno_opts *opt;
442 
443 	if (sopt->sopt_valsize != sizeof(struct cc_newreno_opts))
444 		return (EMSGSIZE);
445 
446 	if (CC_ALGO(ccv->ccvc.tcp) != &newreno_cc_algo)
447 		return (ENOPROTOOPT);
448 
449 	nreno = (struct newreno *)ccv->cc_data;
450 	opt = buf;
451 	switch (sopt->sopt_dir) {
452 	case SOPT_SET:
453 		switch (opt->name) {
454 		case CC_NEWRENO_BETA:
455 			nreno->beta = opt->val;
456 			break;
457 		case CC_NEWRENO_BETA_ECN:
458 			if ((!V_cc_do_abe) && ((nreno->newreno_flags & CC_NEWRENO_BETA_ECN) == 0))
459 				return (EACCES);
460 			nreno->beta_ecn = opt->val;
461 			nreno->newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED;
462 			break;
463 		case CC_NEWRENO_ENABLE_HYSTART:
464 			/* Allow hystart on this connection */
465 			if (opt->val != 0) {
466 				nreno->newreno_flags |= CC_NEWRENO_HYSTART_ALLOWED;
467 				if (opt->val > 1)
468 					nreno->newreno_flags |= CC_NEWRENO_HYSTART_CAN_SH_CWND;
469 				if (opt->val > 2)
470 					nreno->newreno_flags |= CC_NEWRENO_HYSTART_CONS_SSTH;
471 			} else
472 				nreno->newreno_flags &= ~(CC_NEWRENO_HYSTART_ALLOWED|CC_NEWRENO_HYSTART_CAN_SH_CWND|CC_NEWRENO_HYSTART_CONS_SSTH);
473 			newreno_log_hystart_event(ccv, nreno, 7, opt->val);
474 			break;
475 		default:
476 			return (ENOPROTOOPT);
477 		}
478 		break;
479 	case SOPT_GET:
480 		switch (opt->name) {
481 		case CC_NEWRENO_BETA:
482 			opt->val = (nreno == NULL) ?
483 			    V_newreno_beta : nreno->beta;
484 			break;
485 		case CC_NEWRENO_BETA_ECN:
486 			opt->val = (nreno == NULL) ?
487 			    V_newreno_beta_ecn : nreno->beta_ecn;
488 			break;
489 		case CC_NEWRENO_ENABLE_HYSTART:
490 			if (nreno->newreno_flags & CC_NEWRENO_HYSTART_ALLOWED) {
491 				if (nreno->newreno_flags & CC_NEWRENO_HYSTART_CONS_SSTH)
492 					opt->val = 3;
493 				else if (nreno->newreno_flags & CC_NEWRENO_HYSTART_CAN_SH_CWND)
494 					opt->val = 2;
495 				else
496 					opt->val = 1;
497 			} else
498 				opt->val = 0;
499 			break;
500 		default:
501 			return (ENOPROTOOPT);
502 		}
503 		break;
504 	default:
505 		return (EINVAL);
506 	}
507 
508 	return (0);
509 }
510 
511 static int
512 newreno_beta_handler(SYSCTL_HANDLER_ARGS)
513 {
514 	int error;
515 	uint32_t new;
516 
517 	new = *(uint32_t *)arg1;
518 	error = sysctl_handle_int(oidp, &new, 0, req);
519 	if (error == 0 && req->newptr != NULL ) {
520 		if (arg1 == &VNET_NAME(newreno_beta_ecn) && !V_cc_do_abe)
521 			error = EACCES;
522 		else if (new == 0 || new > 100)
523 			error = EINVAL;
524 		else
525 			*(uint32_t *)arg1 = new;
526 	}
527 
528 	return (error);
529 }
530 
531 static void
532 newreno_newround(struct cc_var *ccv, uint32_t round_cnt)
533 {
534 	struct newreno *nreno;
535 
536 	nreno = (struct newreno *)ccv->cc_data;
537 	/* We have entered a new round */
538 	nreno->css_lastround_minrtt = nreno->css_current_round_minrtt;
539 	nreno->css_current_round_minrtt = 0xffffffff;
540 	nreno->css_rttsample_count = 0;
541 	nreno->css_current_round = round_cnt;
542 	if ((nreno->newreno_flags & CC_NEWRENO_HYSTART_IN_CSS) &&
543 	    ((round_cnt - nreno->css_entered_at_round) >= hystart_css_rounds)) {
544 		/* Enter CA */
545 		if (nreno->newreno_flags & CC_NEWRENO_HYSTART_CAN_SH_CWND) {
546 			/*
547 			 * We engage more than snd_ssthresh, engage
548 			 * the brakes!! Though we will stay in SS to
549 			 * creep back up again, so lets leave CSS active
550 			 * and give us hystart_css_rounds more rounds.
551 			 */
552 			if (nreno->newreno_flags & CC_NEWRENO_HYSTART_CONS_SSTH) {
553 				CCV(ccv, snd_ssthresh) = ((nreno->css_lowrtt_fas + nreno->css_fas_at_css_entry) / 2);
554 			} else {
555 				CCV(ccv, snd_ssthresh) = nreno->css_lowrtt_fas;
556 			}
557 			CCV(ccv, snd_cwnd) = nreno->css_fas_at_css_entry;
558 			nreno->css_entered_at_round = round_cnt;
559 		} else {
560 			CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
561 			/* Turn off the CSS flag */
562 			nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
563 			/* Disable use of CSS in the future except long idle  */
564 			nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_ENABLED;
565 		}
566 		newreno_log_hystart_event(ccv, nreno, 6, CCV(ccv, snd_ssthresh));
567 	}
568 	newreno_log_hystart_event(ccv, nreno, 4, round_cnt);
569 }
570 
571 static void
572 newreno_rttsample(struct cc_var *ccv, uint32_t usec_rtt, uint32_t rxtcnt, uint32_t fas)
573 {
574 	struct newreno *nreno;
575 
576 	nreno = (struct newreno *)ccv->cc_data;
577 	if (rxtcnt > 1) {
578 		/*
579 		 * Only look at RTT's that are non-ambiguous.
580 		 */
581 		return;
582 	}
583 	nreno->css_rttsample_count++;
584 	nreno->css_last_fas = fas;
585 	if (nreno->css_current_round_minrtt > usec_rtt) {
586 		nreno->css_current_round_minrtt = usec_rtt;
587 		nreno->css_lowrtt_fas = nreno->css_last_fas;
588 	}
589 	if ((nreno->newreno_flags & CC_NEWRENO_HYSTART_IN_CSS) &&
590 	    (nreno->css_rttsample_count >= hystart_n_rttsamples) &&
591 	    (nreno->css_baseline_minrtt > nreno->css_current_round_minrtt)) {
592 		/*
593 		 * We were in CSS and the RTT is now less, we
594 		 * entered CSS erroneously.
595 		 */
596 		nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
597 		newreno_log_hystart_event(ccv, nreno, 8, nreno->css_baseline_minrtt);
598 		nreno->css_baseline_minrtt = 0xffffffff;
599 	}
600 	newreno_log_hystart_event(ccv, nreno, 5, usec_rtt);
601 }
602 
603 SYSCTL_DECL(_net_inet_tcp_cc_newreno);
604 SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, newreno,
605     CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
606     "New Reno related settings");
607 
608 SYSCTL_PROC(_net_inet_tcp_cc_newreno, OID_AUTO, beta,
609     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
610     &VNET_NAME(newreno_beta), 3, &newreno_beta_handler, "IU",
611     "New Reno beta, specified as number between 1 and 100");
612 
613 SYSCTL_PROC(_net_inet_tcp_cc_newreno, OID_AUTO, beta_ecn,
614     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
615     &VNET_NAME(newreno_beta_ecn), 3, &newreno_beta_handler, "IU",
616     "New Reno beta ecn, specified as number between 1 and 100");
617 
618 SYSCTL_NODE(_net_inet_tcp_cc_newreno, OID_AUTO, hystartplusplus,
619     CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
620     "New Reno related HyStart++ settings");
621 
622 SYSCTL_UINT(_net_inet_tcp_cc_newreno_hystartplusplus, OID_AUTO, lowcwnd,
623     CTLFLAG_RW,
624     &hystart_lowcwnd, 16,
625    "The number of MSS in the CWND before HyStart++ is active");
626 
627 SYSCTL_UINT(_net_inet_tcp_cc_newreno_hystartplusplus, OID_AUTO, minrtt_thresh,
628     CTLFLAG_RW,
629     &hystart_minrtt_thresh, 4000,
630    "HyStarts++ minimum RTT thresh used in clamp (in microseconds)");
631 
632 SYSCTL_UINT(_net_inet_tcp_cc_newreno_hystartplusplus, OID_AUTO, maxrtt_thresh,
633     CTLFLAG_RW,
634     &hystart_maxrtt_thresh, 16000,
635    "HyStarts++ maximum RTT thresh used in clamp (in microseconds)");
636 
637 SYSCTL_UINT(_net_inet_tcp_cc_newreno_hystartplusplus, OID_AUTO, n_rttsamples,
638     CTLFLAG_RW,
639     &hystart_n_rttsamples, 8,
640    "The number of RTT samples that must be seen to consider HyStart++");
641 
642 SYSCTL_UINT(_net_inet_tcp_cc_newreno_hystartplusplus, OID_AUTO, css_growth_div,
643     CTLFLAG_RW,
644     &hystart_css_growth_div, 4,
645    "The divisor to the growth when in Hystart++ CSS");
646 
647 SYSCTL_UINT(_net_inet_tcp_cc_newreno_hystartplusplus, OID_AUTO, css_rounds,
648     CTLFLAG_RW,
649     &hystart_css_rounds, 5,
650    "The number of rounds HyStart++ lasts in CSS before falling to CA");
651 
652 SYSCTL_UINT(_net_inet_tcp_cc_newreno_hystartplusplus, OID_AUTO, bblogs,
653     CTLFLAG_RW,
654     &hystart_bblogs, 0,
655    "Do we enable HyStart++ Black Box logs to be generated if BB logging is on");
656 
657 
658 DECLARE_CC_MODULE(newreno, &newreno_cc_algo);
659 MODULE_VERSION(newreno, 2);
660