cc_htcp.c revision 299280
1169689Skan/*-
2169689Skan * Copyright (c) 2007-2008
3169689Skan * 	Swinburne University of Technology, Melbourne, Australia
4169689Skan * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
5169689Skan * Copyright (c) 2010 The FreeBSD Foundation
6169689Skan * All rights reserved.
7169689Skan *
8169689Skan * This software was developed at the Centre for Advanced Internet
9169689Skan * Architectures, Swinburne University of Technology, by Lawrence Stewart and
10169689Skan * James Healy, made possible in part by a grant from the Cisco University
11169689Skan * Research Program Fund at Community Foundation Silicon Valley.
12169689Skan *
13169689Skan * Portions of this software were developed at the Centre for Advanced
14169689Skan * Internet Architectures, Swinburne University of Technology, Melbourne,
15169689Skan * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
16169689Skan *
17169689Skan * Redistribution and use in source and binary forms, with or without
18169689Skan * modification, are permitted provided that the following conditions
19169689Skan * are met:
20169689Skan * 1. Redistributions of source code must retain the above copyright
21169689Skan *    notice, this list of conditions and the following disclaimer.
22169689Skan * 2. Redistributions in binary form must reproduce the above copyright
23169689Skan *    notice, this list of conditions and the following disclaimer in the
24169689Skan *    documentation and/or other materials provided with the distribution.
25169689Skan *
26169689Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29169689Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36169689Skan * SUCH DAMAGE.
37169689Skan */
38169689Skan
39169689Skan/*
40169689Skan * An implementation of the H-TCP congestion control algorithm for FreeBSD,
41169689Skan * based on the Internet Draft "draft-leith-tcp-htcp-06.txt" by Leith and
42169689Skan * Shorten. Originally released as part of the NewTCP research project at
43169689Skan * Swinburne University of Technology's Centre for Advanced Internet
44169689Skan * Architectures, Melbourne, Australia, which was made possible in part by a
45169689Skan * grant from the Cisco University Research Program Fund at Community Foundation
46169689Skan * Silicon Valley. More details are available at:
47169689Skan *   http://caia.swin.edu.au/urp/newtcp/
48169689Skan */
49169689Skan
50169689Skan#include <sys/cdefs.h>
51169689Skan__FBSDID("$FreeBSD: head/sys/netinet/cc/cc_htcp.c 299280 2016-05-09 19:19:03Z hiren $");
52169689Skan
53169689Skan#include <sys/param.h>
54169689Skan#include <sys/kernel.h>
55169689Skan#include <sys/limits.h>
56169689Skan#include <sys/malloc.h>
57169689Skan#include <sys/module.h>
58169689Skan#include <sys/socket.h>
59169689Skan#include <sys/socketvar.h>
60169689Skan#include <sys/sysctl.h>
61169689Skan#include <sys/systm.h>
62169689Skan
63169689Skan#include <net/vnet.h>
64169689Skan
65169689Skan#include <netinet/tcp.h>
66169689Skan#include <netinet/tcp_seq.h>
67169689Skan#include <netinet/tcp_timer.h>
68169689Skan#include <netinet/tcp_var.h>
69169689Skan#include <netinet/cc/cc.h>
70169689Skan#include <netinet/cc/cc_module.h>
71169689Skan
72169689Skan/* Fixed point math shifts. */
73169689Skan#define HTCP_SHIFT 8
74169689Skan#define HTCP_ALPHA_INC_SHIFT 4
75169689Skan
76169689Skan#define HTCP_INIT_ALPHA 1
77169689Skan#define HTCP_DELTA_L hz		/* 1 sec in ticks. */
78169689Skan#define HTCP_MINBETA 128	/* 0.5 << HTCP_SHIFT. */
79169689Skan#define HTCP_MAXBETA 204	/* ~0.8 << HTCP_SHIFT. */
80169689Skan#define HTCP_MINROWE 26		/* ~0.1 << HTCP_SHIFT. */
81169689Skan#define HTCP_MAXROWE 512	/* 2 << HTCP_SHIFT. */
82169689Skan
83169689Skan/* RTT_ref (ms) used in the calculation of alpha if RTT scaling is enabled. */
84169689Skan#define HTCP_RTT_REF 100
85169689Skan
86169689Skan/* Don't trust SRTT until this many samples have been taken. */
87169689Skan#define HTCP_MIN_RTT_SAMPLES 8
88169689Skan
89169689Skan/*
90169689Skan * HTCP_CALC_ALPHA performs a fixed point math calculation to determine the
91169689Skan * value of alpha, based on the function defined in the HTCP spec.
92169689Skan *
93169689Skan * i.e. 1 + 10(delta - delta_l) + ((delta - delta_l) / 2) ^ 2
94169689Skan *
95169689Skan * "diff" is passed in to the macro as "delta - delta_l" and is expected to be
96169689Skan * in units of ticks.
97169689Skan *
98169689Skan * The joyousnous of fixed point maths means our function implementation looks a
99169689Skan * little funky...
100169689Skan *
101169689Skan * In order to maintain some precision in the calculations, a fixed point shift
102169689Skan * HTCP_ALPHA_INC_SHIFT is used to ensure the integer divisions don't
103169689Skan * truncate the results too badly.
104169689Skan *
105169689Skan * The "16" value is the "1" term in the alpha function shifted up by
106169689Skan * HTCP_ALPHA_INC_SHIFT
107169689Skan *
108169689Skan * The "160" value is the "10" multiplier in the alpha function multiplied by
109169689Skan * 2^HTCP_ALPHA_INC_SHIFT
110169689Skan *
111169689Skan * Specifying these as constants reduces the computations required. After
112169689Skan * up-shifting all the terms in the function and performing the required
113169689Skan * calculations, we down-shift the final result by HTCP_ALPHA_INC_SHIFT to
114169689Skan * ensure it is back in the correct range.
115169689Skan *
116169689Skan * The "hz" terms are required as kernels can be configured to run with
117169689Skan * different tick timers, which we have to adjust for in the alpha calculation
118169689Skan * (which originally was defined in terms of seconds).
119169689Skan *
120169689Skan * We also have to be careful to constrain the value of diff such that it won't
121169689Skan * overflow whilst performing the calculation. The middle term i.e. (160 * diff)
122169689Skan * / hz is the limiting factor in the calculation. We must constrain diff to be
123169689Skan * less than the max size of an int divided by the constant 160 figure
124169689Skan * i.e. diff < INT_MAX / 160
125169689Skan *
126169689Skan * NB: Changing HTCP_ALPHA_INC_SHIFT will require you to MANUALLY update the
127169689Skan * constants used in this function!
128169689Skan */
129169689Skan#define HTCP_CALC_ALPHA(diff) \
130169689Skan((\
131169689Skan	(16) + \
132169689Skan	((160 * (diff)) / hz) + \
133169689Skan	(((diff) / hz) * (((diff) << HTCP_ALPHA_INC_SHIFT) / (4 * hz))) \
134169689Skan) >> HTCP_ALPHA_INC_SHIFT)
135169689Skan
136169689Skanstatic void	htcp_ack_received(struct cc_var *ccv, uint16_t type);
137169689Skanstatic void	htcp_cb_destroy(struct cc_var *ccv);
138169689Skanstatic int	htcp_cb_init(struct cc_var *ccv);
139169689Skanstatic void	htcp_cong_signal(struct cc_var *ccv, uint32_t type);
140169689Skanstatic int	htcp_mod_init(void);
141169689Skanstatic void	htcp_post_recovery(struct cc_var *ccv);
142169689Skanstatic void	htcp_recalc_alpha(struct cc_var *ccv);
143169689Skanstatic void	htcp_recalc_beta(struct cc_var *ccv);
144169689Skanstatic void	htcp_record_rtt(struct cc_var *ccv);
145169689Skanstatic void	htcp_ssthresh_update(struct cc_var *ccv);
146169689Skan
147169689Skanstruct htcp {
148169689Skan	/* cwnd before entering cong recovery. */
149169689Skan	unsigned long	prev_cwnd;
150169689Skan	/* cwnd additive increase parameter. */
151169689Skan	int		alpha;
152169689Skan	/* cwnd multiplicative decrease parameter. */
153169689Skan	int		beta;
154169689Skan	/* Largest rtt seen for the flow. */
155169689Skan	int		maxrtt;
156169689Skan	/* Shortest rtt seen for the flow. */
157169689Skan	int		minrtt;
158169689Skan	/* Time of last congestion event in ticks. */
159169689Skan	int		t_last_cong;
160169689Skan};
161169689Skan
162169689Skanstatic int htcp_rtt_ref;
163169689Skan/*
164169689Skan * The maximum number of ticks the value of diff can reach in
165169689Skan * htcp_recalc_alpha() before alpha will stop increasing due to overflow.
166169689Skan * See comment above HTCP_CALC_ALPHA for more info.
167169689Skan */
168169689Skanstatic int htcp_max_diff = INT_MAX / ((1 << HTCP_ALPHA_INC_SHIFT) * 10);
169169689Skan
170169689Skan/* Per-netstack vars. */
171169689Skanstatic VNET_DEFINE(u_int, htcp_adaptive_backoff) = 0;
172169689Skanstatic VNET_DEFINE(u_int, htcp_rtt_scaling) = 0;
173169689Skan#define	V_htcp_adaptive_backoff    VNET(htcp_adaptive_backoff)
174169689Skan#define	V_htcp_rtt_scaling    VNET(htcp_rtt_scaling)
175169689Skan
176169689Skanstatic MALLOC_DEFINE(M_HTCP, "htcp data",
177169689Skan    "Per connection data required for the HTCP congestion control algorithm");
178169689Skan
179169689Skanstruct cc_algo htcp_cc_algo = {
180169689Skan	.name = "htcp",
181169689Skan	.ack_received = htcp_ack_received,
182169689Skan	.cb_destroy = htcp_cb_destroy,
183169689Skan	.cb_init = htcp_cb_init,
184169689Skan	.cong_signal = htcp_cong_signal,
185169689Skan	.mod_init = htcp_mod_init,
186169689Skan	.post_recovery = htcp_post_recovery,
187169689Skan};
188169689Skan
189169689Skanstatic void
190169689Skanhtcp_ack_received(struct cc_var *ccv, uint16_t type)
191169689Skan{
192169689Skan	struct htcp *htcp_data;
193169689Skan
194169689Skan	htcp_data = ccv->cc_data;
195169689Skan	htcp_record_rtt(ccv);
196169689Skan
197169689Skan	/*
198169689Skan	 * Regular ACK and we're not in cong/fast recovery and we're cwnd
199169689Skan	 * limited and we're either not doing ABC or are slow starting or are
200169689Skan	 * doing ABC and we've sent a cwnd's worth of bytes.
201169689Skan	 */
202169689Skan	if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
203169689Skan	    (ccv->flags & CCF_CWND_LIMITED) && (!V_tcp_do_rfc3465 ||
204169689Skan	    CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||
205169689Skan	    (V_tcp_do_rfc3465 && ccv->flags & CCF_ABC_SENTAWND))) {
206169689Skan		htcp_recalc_beta(ccv);
207169689Skan		htcp_recalc_alpha(ccv);
208169689Skan		/*
209169689Skan		 * Use the logic in NewReno ack_received() for slow start and
210169689Skan		 * for the first HTCP_DELTA_L ticks after either the flow starts
211169689Skan		 * or a congestion event (when alpha equals 1).
212169689Skan		 */
213169689Skan		if (htcp_data->alpha == 1 ||
214169689Skan		    CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh))
215169689Skan			newreno_cc_algo.ack_received(ccv, type);
216169689Skan		else {
217169689Skan			if (V_tcp_do_rfc3465) {
218169689Skan				/* Increment cwnd by alpha segments. */
219169689Skan				CCV(ccv, snd_cwnd) += htcp_data->alpha *
220169689Skan				    CCV(ccv, t_maxseg);
221169689Skan				ccv->flags &= ~CCF_ABC_SENTAWND;
222169689Skan			} else
223169689Skan				/*
224169689Skan				 * Increment cwnd by alpha/cwnd segments to
225169689Skan				 * approximate an increase of alpha segments
226169689Skan				 * per RTT.
227169689Skan				 */
228169689Skan				CCV(ccv, snd_cwnd) += (((htcp_data->alpha <<
229169689Skan				    HTCP_SHIFT) / (CCV(ccv, snd_cwnd) /
230169689Skan				    CCV(ccv, t_maxseg))) * CCV(ccv, t_maxseg))
231169689Skan				    >> HTCP_SHIFT;
232169689Skan		}
233169689Skan	}
234169689Skan}
235169689Skan
236169689Skanstatic void
237169689Skanhtcp_cb_destroy(struct cc_var *ccv)
238169689Skan{
239169689Skan
240169689Skan	if (ccv->cc_data != NULL)
241169689Skan		free(ccv->cc_data, M_HTCP);
242169689Skan}
243169689Skan
244169689Skanstatic int
245169689Skanhtcp_cb_init(struct cc_var *ccv)
246169689Skan{
247169689Skan	struct htcp *htcp_data;
248169689Skan
249169689Skan	htcp_data = malloc(sizeof(struct htcp), M_HTCP, M_NOWAIT);
250169689Skan
251169689Skan	if (htcp_data == NULL)
252169689Skan		return (ENOMEM);
253169689Skan
254169689Skan	/* Init some key variables with sensible defaults. */
255169689Skan	htcp_data->alpha = HTCP_INIT_ALPHA;
256169689Skan	htcp_data->beta = HTCP_MINBETA;
257169689Skan	htcp_data->maxrtt = TCPTV_SRTTBASE;
258169689Skan	htcp_data->minrtt = TCPTV_SRTTBASE;
259169689Skan	htcp_data->prev_cwnd = 0;
260169689Skan	htcp_data->t_last_cong = ticks;
261169689Skan
262169689Skan	ccv->cc_data = htcp_data;
263169689Skan
264169689Skan	return (0);
265169689Skan}
266169689Skan
267169689Skan/*
268169689Skan * Perform any necessary tasks before we enter congestion recovery.
269169689Skan */
270169689Skanstatic void
271169689Skanhtcp_cong_signal(struct cc_var *ccv, uint32_t type)
272169689Skan{
273169689Skan	struct htcp *htcp_data;
274169689Skan
275169689Skan	htcp_data = ccv->cc_data;
276169689Skan
277169689Skan	switch (type) {
278169689Skan	case CC_NDUPACK:
279169689Skan		if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
280169689Skan			if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
281169689Skan				/*
282169689Skan				 * Apply hysteresis to maxrtt to ensure
283169689Skan				 * reductions in the RTT are reflected in our
284169689Skan				 * measurements.
285169689Skan				 */
286169689Skan				htcp_data->maxrtt = (htcp_data->minrtt +
287169689Skan				    (htcp_data->maxrtt - htcp_data->minrtt) *
288169689Skan				    95) / 100;
289169689Skan				htcp_ssthresh_update(ccv);
290169689Skan				htcp_data->t_last_cong = ticks;
291169689Skan				htcp_data->prev_cwnd = CCV(ccv, snd_cwnd);
292169689Skan			}
293169689Skan			ENTER_RECOVERY(CCV(ccv, t_flags));
294169689Skan		}
295169689Skan		break;
296169689Skan
297169689Skan	case CC_ECN:
298169689Skan		if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
299169689Skan			/*
300169689Skan			 * Apply hysteresis to maxrtt to ensure reductions in
301169689Skan			 * the RTT are reflected in our measurements.
302169689Skan			 */
303169689Skan			htcp_data->maxrtt = (htcp_data->minrtt + (htcp_data->maxrtt -
304169689Skan			    htcp_data->minrtt) * 95) / 100;
305169689Skan			htcp_ssthresh_update(ccv);
306169689Skan			CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
307169689Skan			htcp_data->t_last_cong = ticks;
308169689Skan			htcp_data->prev_cwnd = CCV(ccv, snd_cwnd);
309169689Skan			ENTER_CONGRECOVERY(CCV(ccv, t_flags));
310169689Skan		}
311169689Skan		break;
312169689Skan
313169689Skan	case CC_RTO:
314169689Skan		/*
315169689Skan		 * Grab the current time and record it so we know when the
316169689Skan		 * most recent congestion event was. Only record it when the
317169689Skan		 * timeout has fired more than once, as there is a reasonable
318169689Skan		 * chance the first one is a false alarm and may not indicate
319169689Skan		 * congestion.
320169689Skan		 */
321169689Skan		if (CCV(ccv, t_rxtshift) >= 2)
322169689Skan			htcp_data->t_last_cong = ticks;
323169689Skan		break;
324169689Skan	}
325169689Skan}
326169689Skan
327169689Skanstatic int
328169689Skanhtcp_mod_init(void)
329169689Skan{
330169689Skan
331169689Skan	htcp_cc_algo.after_idle = newreno_cc_algo.after_idle;
332169689Skan
333169689Skan	/*
334169689Skan	 * HTCP_RTT_REF is defined in ms, and t_srtt in the tcpcb is stored in
335169689Skan	 * units of TCP_RTT_SCALE*hz. Scale HTCP_RTT_REF to be in the same units
336169689Skan	 * as t_srtt.
337169689Skan	 */
338169689Skan	htcp_rtt_ref = (HTCP_RTT_REF * TCP_RTT_SCALE * hz) / 1000;
339169689Skan
340169689Skan	return (0);
341169689Skan}
342169689Skan
343169689Skan/*
344169689Skan * Perform any necessary tasks before we exit congestion recovery.
345169689Skan */
346169689Skanstatic void
347169689Skanhtcp_post_recovery(struct cc_var *ccv)
348169689Skan{
349169689Skan	int pipe;
350169689Skan	struct htcp *htcp_data;
351169689Skan
352169689Skan	pipe = 0;
353169689Skan	htcp_data = ccv->cc_data;
354169689Skan
355169689Skan	if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
356169689Skan		/*
357169689Skan		 * If inflight data is less than ssthresh, set cwnd
358169689Skan		 * conservatively to avoid a burst of data, as suggested in the
359169689Skan		 * NewReno RFC. Otherwise, use the HTCP method.
360169689Skan		 *
361169689Skan		 * XXXLAS: Find a way to do this without needing curack
362169689Skan		 */
363169689Skan		if (V_tcp_do_rfc6675_pipe)
364169689Skan			pipe = tcp_compute_pipe(ccv->ccvc.tcp);
365169689Skan		else
366169689Skan			pipe = CCV(ccv, snd_max) - ccv->curack;
367169689Skan
368169689Skan		if (pipe < CCV(ccv, snd_ssthresh))
369169689Skan			CCV(ccv, snd_cwnd) = pipe + CCV(ccv, t_maxseg);
370169689Skan		else
371169689Skan			CCV(ccv, snd_cwnd) = max(1, ((htcp_data->beta *
372169689Skan			    htcp_data->prev_cwnd / CCV(ccv, t_maxseg))
373169689Skan			    >> HTCP_SHIFT)) * CCV(ccv, t_maxseg);
374169689Skan	}
375169689Skan}
376169689Skan
377169689Skanstatic void
378169689Skanhtcp_recalc_alpha(struct cc_var *ccv)
379169689Skan{
380169689Skan	struct htcp *htcp_data;
381169689Skan	int alpha, diff, now;
382169689Skan
383169689Skan	htcp_data = ccv->cc_data;
384169689Skan	now = ticks;
385169689Skan
386169689Skan	/*
387169689Skan	 * If ticks has wrapped around (will happen approximately once every 49
388169689Skan	 * days on a machine with the default kern.hz=1000) and a flow straddles
389169689Skan	 * the wrap point, our alpha calcs will be completely wrong. We cut our
390169689Skan	 * losses and restart alpha from scratch by setting t_last_cong = now -
391169689Skan	 * HTCP_DELTA_L.
392169689Skan	 *
393169689Skan	 * This does not deflate our cwnd at all. It simply slows the rate cwnd
394169689Skan	 * is growing by until alpha regains the value it held prior to taking
395169689Skan	 * this drastic measure.
396169689Skan	 */
397169689Skan	if (now < htcp_data->t_last_cong)
398169689Skan		htcp_data->t_last_cong = now - HTCP_DELTA_L;
399169689Skan
400169689Skan	diff = now - htcp_data->t_last_cong - HTCP_DELTA_L;
401169689Skan
402169689Skan	/* Cap alpha if the value of diff would overflow HTCP_CALC_ALPHA(). */
403169689Skan	if (diff < htcp_max_diff) {
404169689Skan		/*
405169689Skan		 * If it has been more than HTCP_DELTA_L ticks since congestion,
406169689Skan		 * increase alpha according to the function defined in the spec.
407169689Skan		 */
408169689Skan		if (diff > 0) {
409169689Skan			alpha = HTCP_CALC_ALPHA(diff);
410169689Skan
411169689Skan			/*
412169689Skan			 * Adaptive backoff fairness adjustment:
413169689Skan			 * 2 * (1 - beta) * alpha_raw
414169689Skan			 */
415169689Skan			if (V_htcp_adaptive_backoff)
416169689Skan				alpha = max(1, (2 * ((1 << HTCP_SHIFT) -
417169689Skan				    htcp_data->beta) * alpha) >> HTCP_SHIFT);
418169689Skan
419169689Skan			/*
420169689Skan			 * RTT scaling: (RTT / RTT_ref) * alpha
421169689Skan			 * alpha will be the raw value from HTCP_CALC_ALPHA() if
422169689Skan			 * adaptive backoff is off, or the adjusted value if
423169689Skan			 * adaptive backoff is on.
424169689Skan			 */
425169689Skan			if (V_htcp_rtt_scaling)
426169689Skan				alpha = max(1, (min(max(HTCP_MINROWE,
427169689Skan				    (CCV(ccv, t_srtt) << HTCP_SHIFT) /
428169689Skan				    htcp_rtt_ref), HTCP_MAXROWE) * alpha)
429169689Skan				    >> HTCP_SHIFT);
430169689Skan
431169689Skan		} else
432169689Skan			alpha = 1;
433169689Skan
434169689Skan		htcp_data->alpha = alpha;
435169689Skan	}
436169689Skan}
437169689Skan
438169689Skanstatic void
439169689Skanhtcp_recalc_beta(struct cc_var *ccv)
440169689Skan{
441169689Skan	struct htcp *htcp_data;
442
443	htcp_data = ccv->cc_data;
444
445	/*
446	 * TCPTV_SRTTBASE is the initialised value of each connection's SRTT, so
447	 * we only calc beta if the connection's SRTT has been changed from its
448	 * initial value. beta is bounded to ensure it is always between
449	 * HTCP_MINBETA and HTCP_MAXBETA.
450	 */
451	if (V_htcp_adaptive_backoff && htcp_data->minrtt != TCPTV_SRTTBASE &&
452	    htcp_data->maxrtt != TCPTV_SRTTBASE)
453		htcp_data->beta = min(max(HTCP_MINBETA,
454		    (htcp_data->minrtt << HTCP_SHIFT) / htcp_data->maxrtt),
455		    HTCP_MAXBETA);
456	else
457		htcp_data->beta = HTCP_MINBETA;
458}
459
460/*
461 * Record the minimum and maximum RTT seen for the connection. These are used in
462 * the calculation of beta if adaptive backoff is enabled.
463 */
464static void
465htcp_record_rtt(struct cc_var *ccv)
466{
467	struct htcp *htcp_data;
468
469	htcp_data = ccv->cc_data;
470
471	/* XXXLAS: Should there be some hysteresis for minrtt? */
472
473	/*
474	 * Record the current SRTT as our minrtt if it's the smallest we've seen
475	 * or minrtt is currently equal to its initialised value. Ignore SRTT
476	 * until a min number of samples have been taken.
477	 */
478	if ((CCV(ccv, t_srtt) < htcp_data->minrtt ||
479	    htcp_data->minrtt == TCPTV_SRTTBASE) &&
480	    (CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES))
481		htcp_data->minrtt = CCV(ccv, t_srtt);
482
483	/*
484	 * Record the current SRTT as our maxrtt if it's the largest we've
485	 * seen. Ignore SRTT until a min number of samples have been taken.
486	 */
487	if (CCV(ccv, t_srtt) > htcp_data->maxrtt
488	    && CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES)
489		htcp_data->maxrtt = CCV(ccv, t_srtt);
490}
491
492/*
493 * Update the ssthresh in the event of congestion.
494 */
495static void
496htcp_ssthresh_update(struct cc_var *ccv)
497{
498	struct htcp *htcp_data;
499
500	htcp_data = ccv->cc_data;
501
502	/*
503	 * On the first congestion event, set ssthresh to cwnd * 0.5, on
504	 * subsequent congestion events, set it to cwnd * beta.
505	 */
506	if (CCV(ccv, snd_ssthresh) == TCP_MAXWIN << TCP_MAX_WINSHIFT)
507		CCV(ccv, snd_ssthresh) = (CCV(ccv, snd_cwnd) * HTCP_MINBETA)
508		    >> HTCP_SHIFT;
509	else {
510		htcp_recalc_beta(ccv);
511		CCV(ccv, snd_ssthresh) = (CCV(ccv, snd_cwnd) * htcp_data->beta)
512		    >> HTCP_SHIFT;
513	}
514}
515
516
517SYSCTL_DECL(_net_inet_tcp_cc_htcp);
518SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, htcp, CTLFLAG_RW,
519    NULL, "H-TCP related settings");
520SYSCTL_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, adaptive_backoff,
521    CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(htcp_adaptive_backoff), 0,
522    "enable H-TCP adaptive backoff");
523SYSCTL_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, rtt_scaling,
524    CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(htcp_rtt_scaling), 0,
525    "enable H-TCP RTT scaling");
526
527DECLARE_CC_MODULE(htcp, &htcp_cc_algo);
528