cc_htcp.c revision 217322
1216115Slstewart/*-
2216115Slstewart * Copyright (c) 2007-2008
3216115Slstewart * 	Swinburne University of Technology, Melbourne, Australia
4216115Slstewart * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
5216115Slstewart * Copyright (c) 2010 The FreeBSD Foundation
6216115Slstewart * All rights reserved.
7216115Slstewart *
8216115Slstewart * This software was developed at the Centre for Advanced Internet
9216115Slstewart * Architectures, Swinburne University, by Lawrence Stewart and James Healy,
10216115Slstewart * made possible in part by a grant from the Cisco University Research Program
11216115Slstewart * Fund at Community Foundation Silicon Valley.
12216115Slstewart *
13216115Slstewart * Portions of this software were developed at the Centre for Advanced
14216115Slstewart * Internet Architectures, Swinburne University of Technology, Melbourne,
15216115Slstewart * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
16216115Slstewart *
17216115Slstewart * Redistribution and use in source and binary forms, with or without
18216115Slstewart * modification, are permitted provided that the following conditions
19216115Slstewart * are met:
20216115Slstewart * 1. Redistributions of source code must retain the above copyright
21216115Slstewart *    notice, this list of conditions and the following disclaimer.
22216115Slstewart * 2. Redistributions in binary form must reproduce the above copyright
23216115Slstewart *    notice, this list of conditions and the following disclaimer in the
24216115Slstewart *    documentation and/or other materials provided with the distribution.
25216115Slstewart *
26216115Slstewart * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27216115Slstewart * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28216115Slstewart * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29216115Slstewart * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30216115Slstewart * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31216115Slstewart * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32216115Slstewart * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33216115Slstewart * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34216115Slstewart * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35216115Slstewart * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36216115Slstewart * SUCH DAMAGE.
37216115Slstewart */
38216115Slstewart
39216115Slstewart/*
40216115Slstewart * An implementation of the H-TCP congestion control algorithm for FreeBSD,
41216115Slstewart * based on the Internet Draft "draft-leith-tcp-htcp-06.txt" by Leith and
42216115Slstewart * Shorten. Originally released as part of the NewTCP research project at
43216115Slstewart * Swinburne University's Centre for Advanced Internet Architectures, Melbourne,
44216115Slstewart * Australia, which was made possible in part by a grant from the Cisco
45216115Slstewart * University Research Program Fund at Community Foundation Silicon Valley. More
46216115Slstewart * details are available at:
47216115Slstewart *   http://caia.swin.edu.au/urp/newtcp/
48216115Slstewart */
49216115Slstewart
50216115Slstewart#include <sys/cdefs.h>
51216115Slstewart__FBSDID("$FreeBSD: head/sys/netinet/cc/cc_htcp.c 217322 2011-01-12 19:53:50Z mdf $");
52216115Slstewart
53216115Slstewart#include <sys/param.h>
54216115Slstewart#include <sys/kernel.h>
55216115Slstewart#include <sys/limits.h>
56216115Slstewart#include <sys/malloc.h>
57216115Slstewart#include <sys/module.h>
58216115Slstewart#include <sys/socket.h>
59216115Slstewart#include <sys/socketvar.h>
60216115Slstewart#include <sys/sysctl.h>
61216115Slstewart#include <sys/systm.h>
62216115Slstewart
63216115Slstewart#include <net/vnet.h>
64216115Slstewart
65216115Slstewart#include <netinet/cc.h>
66216115Slstewart#include <netinet/tcp_seq.h>
67216115Slstewart#include <netinet/tcp_timer.h>
68216115Slstewart#include <netinet/tcp_var.h>
69216115Slstewart
70216115Slstewart#include <netinet/cc/cc_module.h>
71216115Slstewart
72216115Slstewart/* Fixed point math shifts. */
73216115Slstewart#define HTCP_SHIFT 8
74216115Slstewart#define HTCP_ALPHA_INC_SHIFT 4
75216115Slstewart
76216115Slstewart#define HTCP_INIT_ALPHA 1
77216115Slstewart#define HTCP_DELTA_L hz		/* 1 sec in ticks. */
78216115Slstewart#define HTCP_MINBETA 128	/* 0.5 << HTCP_SHIFT. */
79216115Slstewart#define HTCP_MAXBETA 204	/* ~0.8 << HTCP_SHIFT. */
80216115Slstewart#define HTCP_MINROWE 26		/* ~0.1 << HTCP_SHIFT. */
81216115Slstewart#define HTCP_MAXROWE 512	/* 2 << HTCP_SHIFT. */
82216115Slstewart
83216115Slstewart/* RTT_ref (ms) used in the calculation of alpha if RTT scaling is enabled. */
84216115Slstewart#define HTCP_RTT_REF 100
85216115Slstewart
86216115Slstewart/* Don't trust SRTT until this many samples have been taken. */
87216115Slstewart#define HTCP_MIN_RTT_SAMPLES 8
88216115Slstewart
89216115Slstewart/*
90216115Slstewart * HTCP_CALC_ALPHA performs a fixed point math calculation to determine the
91216115Slstewart * value of alpha, based on the function defined in the HTCP spec.
92216115Slstewart *
93216115Slstewart * i.e. 1 + 10(delta - delta_l) + ((delta - delta_l) / 2) ^ 2
94216115Slstewart *
95216115Slstewart * "diff" is passed in to the macro as "delta - delta_l" and is expected to be
96216115Slstewart * in units of ticks.
97216115Slstewart *
98216115Slstewart * The joyousnous of fixed point maths means our function implementation looks a
99216115Slstewart * little funky...
100216115Slstewart *
101216115Slstewart * In order to maintain some precision in the calculations, a fixed point shift
102216115Slstewart * HTCP_ALPHA_INC_SHIFT is used to ensure the integer divisions don't
103216115Slstewart * truncate the results too badly.
104216115Slstewart *
105216115Slstewart * The "16" value is the "1" term in the alpha function shifted up by
106216115Slstewart * HTCP_ALPHA_INC_SHIFT
107216115Slstewart *
108216115Slstewart * The "160" value is the "10" multiplier in the alpha function multiplied by
109216115Slstewart * 2^HTCP_ALPHA_INC_SHIFT
110216115Slstewart *
111216115Slstewart * Specifying these as constants reduces the computations required. After
112216115Slstewart * up-shifting all the terms in the function and performing the required
113216115Slstewart * calculations, we down-shift the final result by HTCP_ALPHA_INC_SHIFT to
114216115Slstewart * ensure it is back in the correct range.
115216115Slstewart *
116216115Slstewart * The "hz" terms are required as kernels can be configured to run with
117216115Slstewart * different tick timers, which we have to adjust for in the alpha calculation
118216115Slstewart * (which originally was defined in terms of seconds).
119216115Slstewart *
120216115Slstewart * We also have to be careful to constrain the value of diff such that it won't
121216115Slstewart * overflow whilst performing the calculation. The middle term i.e. (160 * diff)
122216115Slstewart * / hz is the limiting factor in the calculation. We must constrain diff to be
123216115Slstewart * less than the max size of an int divided by the constant 160 figure
124216115Slstewart * i.e. diff < INT_MAX / 160
125216115Slstewart *
126216115Slstewart * NB: Changing HTCP_ALPHA_INC_SHIFT will require you to MANUALLY update the
127216115Slstewart * constants used in this function!
128216115Slstewart */
129216115Slstewart#define HTCP_CALC_ALPHA(diff) \
130216115Slstewart((\
131216115Slstewart	(16) + \
132216115Slstewart	((160 * (diff)) / hz) + \
133216115Slstewart	(((diff) / hz) * (((diff) << HTCP_ALPHA_INC_SHIFT) / (4 * hz))) \
134216115Slstewart) >> HTCP_ALPHA_INC_SHIFT)
135216115Slstewart
136216115Slstewartstatic void	htcp_ack_received(struct cc_var *ccv, uint16_t type);
137216115Slstewartstatic void	htcp_cb_destroy(struct cc_var *ccv);
138216115Slstewartstatic int	htcp_cb_init(struct cc_var *ccv);
139216115Slstewartstatic void	htcp_cong_signal(struct cc_var *ccv, uint32_t type);
140216115Slstewartstatic int	htcp_mod_init(void);
141216115Slstewartstatic void	htcp_post_recovery(struct cc_var *ccv);
142216115Slstewartstatic void	htcp_recalc_alpha(struct cc_var *ccv);
143216115Slstewartstatic void	htcp_recalc_beta(struct cc_var *ccv);
144216115Slstewartstatic void	htcp_record_rtt(struct cc_var *ccv);
145216115Slstewartstatic void	htcp_ssthresh_update(struct cc_var *ccv);
146216115Slstewart
147216115Slstewartstruct htcp {
148216115Slstewart	/* cwnd before entering cong recovery. */
149216115Slstewart	unsigned long	prev_cwnd;
150216115Slstewart	/* cwnd additive increase parameter. */
151216115Slstewart	int		alpha;
152216115Slstewart	/* cwnd multiplicative decrease parameter. */
153216115Slstewart	int		beta;
154216115Slstewart	/* Largest rtt seen for the flow. */
155216115Slstewart	int		maxrtt;
156216115Slstewart	/* Shortest rtt seen for the flow. */
157216115Slstewart	int		minrtt;
158216115Slstewart	/* Time of last congestion event in ticks. */
159216115Slstewart	int		t_last_cong;
160216115Slstewart};
161216115Slstewart
162216115Slstewartstatic int htcp_rtt_ref;
163216115Slstewart/*
164216115Slstewart * The maximum number of ticks the value of diff can reach in
165216115Slstewart * htcp_recalc_alpha() before alpha will stop increasing due to overflow.
166216115Slstewart * See comment above HTCP_CALC_ALPHA for more info.
167216115Slstewart */
168216115Slstewartstatic int htcp_max_diff = INT_MAX / ((1 << HTCP_ALPHA_INC_SHIFT) * 10);
169216115Slstewart
170216115Slstewart/* Per-netstack vars. */
171217322Smdfstatic VNET_DEFINE(u_int, htcp_adaptive_backoff) = 0;
172217322Smdfstatic VNET_DEFINE(u_int, htcp_rtt_scaling) = 0;
173216115Slstewart#define	V_htcp_adaptive_backoff    VNET(htcp_adaptive_backoff)
174216115Slstewart#define	V_htcp_rtt_scaling    VNET(htcp_rtt_scaling)
175216115Slstewart
176216115SlstewartMALLOC_DECLARE(M_HTCP);
177216115SlstewartMALLOC_DEFINE(M_HTCP, "htcp data",
178216115Slstewart    "Per connection data required for the HTCP congestion control algorithm");
179216115Slstewart
180216115Slstewartstruct cc_algo htcp_cc_algo = {
181216115Slstewart	.name = "htcp",
182216115Slstewart	.ack_received = htcp_ack_received,
183216115Slstewart	.cb_destroy = htcp_cb_destroy,
184216115Slstewart	.cb_init = htcp_cb_init,
185216115Slstewart	.cong_signal = htcp_cong_signal,
186216115Slstewart	.mod_init = htcp_mod_init,
187216115Slstewart	.post_recovery = htcp_post_recovery,
188216115Slstewart};
189216115Slstewart
190216115Slstewartstatic void
191216115Slstewarthtcp_ack_received(struct cc_var *ccv, uint16_t type)
192216115Slstewart{
193216115Slstewart	struct htcp *htcp_data;
194216115Slstewart
195216115Slstewart	htcp_data = ccv->cc_data;
196216115Slstewart	htcp_record_rtt(ccv);
197216115Slstewart
198216115Slstewart	/*
199216115Slstewart	 * Regular ACK and we're not in cong/fast recovery and we're cwnd
200216115Slstewart	 * limited and we're either not doing ABC or are slow starting or are
201216115Slstewart	 * doing ABC and we've sent a cwnd's worth of bytes.
202216115Slstewart	 */
203216115Slstewart	if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
204216115Slstewart	    (ccv->flags & CCF_CWND_LIMITED) && (!V_tcp_do_rfc3465 ||
205216115Slstewart	    CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||
206216115Slstewart	    (V_tcp_do_rfc3465 && ccv->flags & CCF_ABC_SENTAWND))) {
207216115Slstewart		htcp_recalc_beta(ccv);
208216115Slstewart		htcp_recalc_alpha(ccv);
209216115Slstewart		/*
210216115Slstewart		 * Use the logic in NewReno ack_received() for slow start and
211216115Slstewart		 * for the first HTCP_DELTA_L ticks after either the flow starts
212216115Slstewart		 * or a congestion event (when alpha equals 1).
213216115Slstewart		 */
214216115Slstewart		if (htcp_data->alpha == 1 ||
215216115Slstewart		    CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh))
216216115Slstewart			newreno_cc_algo.ack_received(ccv, type);
217216115Slstewart		else {
218216115Slstewart			if (V_tcp_do_rfc3465) {
219216115Slstewart				/* Increment cwnd by alpha segments. */
220216115Slstewart				CCV(ccv, snd_cwnd) += htcp_data->alpha *
221216115Slstewart				    CCV(ccv, t_maxseg);
222216115Slstewart				ccv->flags &= ~CCF_ABC_SENTAWND;
223216115Slstewart			} else
224216115Slstewart				/*
225216115Slstewart				 * Increment cwnd by alpha/cwnd segments to
226216115Slstewart				 * approximate an increase of alpha segments
227216115Slstewart				 * per RTT.
228216115Slstewart				 */
229216115Slstewart				CCV(ccv, snd_cwnd) += (((htcp_data->alpha <<
230216115Slstewart				    HTCP_SHIFT) / (CCV(ccv, snd_cwnd) /
231216115Slstewart				    CCV(ccv, t_maxseg))) * CCV(ccv, t_maxseg))
232216115Slstewart				    >> HTCP_SHIFT;
233216115Slstewart		}
234216115Slstewart	}
235216115Slstewart}
236216115Slstewart
237216115Slstewartstatic void
238216115Slstewarthtcp_cb_destroy(struct cc_var *ccv)
239216115Slstewart{
240216115Slstewart
241216115Slstewart	if (ccv->cc_data != NULL)
242216115Slstewart		free(ccv->cc_data, M_HTCP);
243216115Slstewart}
244216115Slstewart
245216115Slstewartstatic int
246216115Slstewarthtcp_cb_init(struct cc_var *ccv)
247216115Slstewart{
248216115Slstewart	struct htcp *htcp_data;
249216115Slstewart
250216115Slstewart	htcp_data = malloc(sizeof(struct htcp), M_HTCP, M_NOWAIT);
251216115Slstewart
252216115Slstewart	if (htcp_data == NULL)
253216115Slstewart		return (ENOMEM);
254216115Slstewart
255216115Slstewart	/* Init some key variables with sensible defaults. */
256216115Slstewart	htcp_data->alpha = HTCP_INIT_ALPHA;
257216115Slstewart	htcp_data->beta = HTCP_MINBETA;
258216115Slstewart	htcp_data->maxrtt = TCPTV_SRTTBASE;
259216115Slstewart	htcp_data->minrtt = TCPTV_SRTTBASE;
260216115Slstewart	htcp_data->prev_cwnd = 0;
261216115Slstewart	htcp_data->t_last_cong = ticks;
262216115Slstewart
263216115Slstewart	ccv->cc_data = htcp_data;
264216115Slstewart
265216115Slstewart	return (0);
266216115Slstewart}
267216115Slstewart
268216115Slstewart/*
269216115Slstewart * Perform any necessary tasks before we enter congestion recovery.
270216115Slstewart */
271216115Slstewartstatic void
272216115Slstewarthtcp_cong_signal(struct cc_var *ccv, uint32_t type)
273216115Slstewart{
274216115Slstewart	struct htcp *htcp_data;
275216115Slstewart
276216115Slstewart	htcp_data = ccv->cc_data;
277216115Slstewart
278216115Slstewart	switch (type) {
279216115Slstewart	case CC_NDUPACK:
280216115Slstewart		if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
281216115Slstewart			if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
282216115Slstewart				/*
283216115Slstewart				 * Apply hysteresis to maxrtt to ensure
284216115Slstewart				 * reductions in the RTT are reflected in our
285216115Slstewart				 * measurements.
286216115Slstewart				 */
287216115Slstewart				htcp_data->maxrtt = (htcp_data->minrtt +
288216115Slstewart				    (htcp_data->maxrtt - htcp_data->minrtt) *
289216115Slstewart				    95) / 100;
290216115Slstewart				htcp_ssthresh_update(ccv);
291216115Slstewart				htcp_data->t_last_cong = ticks;
292216115Slstewart				htcp_data->prev_cwnd = CCV(ccv, snd_cwnd);
293216115Slstewart			}
294216115Slstewart			ENTER_RECOVERY(CCV(ccv, t_flags));
295216115Slstewart		}
296216115Slstewart		break;
297216115Slstewart
298216115Slstewart	case CC_ECN:
299216115Slstewart		if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
300216115Slstewart			/*
301216115Slstewart			 * Apply hysteresis to maxrtt to ensure reductions in
302216115Slstewart			 * the RTT are reflected in our measurements.
303216115Slstewart			 */
304216115Slstewart			htcp_data->maxrtt = (htcp_data->minrtt + (htcp_data->maxrtt -
305216115Slstewart			    htcp_data->minrtt) * 95) / 100;
306216115Slstewart			htcp_ssthresh_update(ccv);
307216115Slstewart			CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
308216115Slstewart			htcp_data->t_last_cong = ticks;
309216115Slstewart			htcp_data->prev_cwnd = CCV(ccv, snd_cwnd);
310216115Slstewart			ENTER_CONGRECOVERY(CCV(ccv, t_flags));
311216115Slstewart		}
312216115Slstewart		break;
313216115Slstewart
314216115Slstewart	case CC_RTO:
315216115Slstewart		/*
316216115Slstewart		 * Grab the current time and record it so we know when the
317216115Slstewart		 * most recent congestion event was. Only record it when the
318216115Slstewart		 * timeout has fired more than once, as there is a reasonable
319216115Slstewart		 * chance the first one is a false alarm and may not indicate
320216115Slstewart		 * congestion.
321216115Slstewart		 */
322216115Slstewart		if (CCV(ccv, t_rxtshift) >= 2)
323216115Slstewart			htcp_data->t_last_cong = ticks;
324216115Slstewart		break;
325216115Slstewart	}
326216115Slstewart}
327216115Slstewart
328216115Slstewartstatic int
329216115Slstewarthtcp_mod_init(void)
330216115Slstewart{
331216115Slstewart
332216115Slstewart	htcp_cc_algo.after_idle = newreno_cc_algo.after_idle;
333216115Slstewart
334216115Slstewart	/*
335216115Slstewart	 * HTCP_RTT_REF is defined in ms, and t_srtt in the tcpcb is stored in
336216115Slstewart	 * units of TCP_RTT_SCALE*hz. Scale HTCP_RTT_REF to be in the same units
337216115Slstewart	 * as t_srtt.
338216115Slstewart	 */
339216115Slstewart	htcp_rtt_ref = (HTCP_RTT_REF * TCP_RTT_SCALE * hz) / 1000;
340216115Slstewart
341216115Slstewart	return (0);
342216115Slstewart}
343216115Slstewart
344216115Slstewart/*
345216115Slstewart * Perform any necessary tasks before we exit congestion recovery.
346216115Slstewart */
347216115Slstewartstatic void
348216115Slstewarthtcp_post_recovery(struct cc_var *ccv)
349216115Slstewart{
350216115Slstewart	struct htcp *htcp_data;
351216115Slstewart
352216115Slstewart	htcp_data = ccv->cc_data;
353216115Slstewart
354216115Slstewart	if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
355216115Slstewart		/*
356216115Slstewart		 * If inflight data is less than ssthresh, set cwnd
357216115Slstewart		 * conservatively to avoid a burst of data, as suggested in the
358216115Slstewart		 * NewReno RFC. Otherwise, use the HTCP method.
359216115Slstewart		 *
360216115Slstewart		 * XXXLAS: Find a way to do this without needing curack
361216115Slstewart		 */
362216115Slstewart		if (SEQ_GT(ccv->curack + CCV(ccv, snd_ssthresh),
363216115Slstewart		    CCV(ccv, snd_max)))
364216115Slstewart			CCV(ccv, snd_cwnd) = CCV(ccv, snd_max) - ccv->curack +
365216115Slstewart			    CCV(ccv, t_maxseg);
366216115Slstewart		else
367216115Slstewart			CCV(ccv, snd_cwnd) = max(1, ((htcp_data->beta *
368216115Slstewart			    htcp_data->prev_cwnd / CCV(ccv, t_maxseg))
369216115Slstewart			    >> HTCP_SHIFT)) * CCV(ccv, t_maxseg);
370216115Slstewart	}
371216115Slstewart}
372216115Slstewart
373216115Slstewartstatic void
374216115Slstewarthtcp_recalc_alpha(struct cc_var *ccv)
375216115Slstewart{
376216115Slstewart	struct htcp *htcp_data;
377216115Slstewart	int alpha, diff, now;
378216115Slstewart
379216115Slstewart	htcp_data = ccv->cc_data;
380216115Slstewart	now = ticks;
381216115Slstewart
382216115Slstewart	/*
383216115Slstewart	 * If ticks has wrapped around (will happen approximately once every 49
384216115Slstewart	 * days on a machine with the default kern.hz=1000) and a flow straddles
385216115Slstewart	 * the wrap point, our alpha calcs will be completely wrong. We cut our
386216115Slstewart	 * losses and restart alpha from scratch by setting t_last_cong = now -
387216115Slstewart	 * HTCP_DELTA_L.
388216115Slstewart	 *
389216115Slstewart	 * This does not deflate our cwnd at all. It simply slows the rate cwnd
390216115Slstewart	 * is growing by until alpha regains the value it held prior to taking
391216115Slstewart	 * this drastic measure.
392216115Slstewart	 */
393216115Slstewart	if (now < htcp_data->t_last_cong)
394216115Slstewart		htcp_data->t_last_cong = now - HTCP_DELTA_L;
395216115Slstewart
396216115Slstewart	diff = now - htcp_data->t_last_cong - HTCP_DELTA_L;
397216115Slstewart
398216115Slstewart	/* Cap alpha if the value of diff would overflow HTCP_CALC_ALPHA(). */
399216115Slstewart	if (diff < htcp_max_diff) {
400216115Slstewart		/*
401216115Slstewart		 * If it has been more than HTCP_DELTA_L ticks since congestion,
402216115Slstewart		 * increase alpha according to the function defined in the spec.
403216115Slstewart		 */
404216115Slstewart		if (diff > 0) {
405216115Slstewart			alpha = HTCP_CALC_ALPHA(diff);
406216115Slstewart
407216115Slstewart			/*
408216115Slstewart			 * Adaptive backoff fairness adjustment:
409216115Slstewart			 * 2 * (1 - beta) * alpha_raw
410216115Slstewart			 */
411216115Slstewart			if (V_htcp_adaptive_backoff)
412216115Slstewart				alpha = max(1, (2 * ((1 << HTCP_SHIFT) -
413216115Slstewart				    htcp_data->beta) * alpha) >> HTCP_SHIFT);
414216115Slstewart
415216115Slstewart			/*
416216115Slstewart			 * RTT scaling: (RTT / RTT_ref) * alpha
417216115Slstewart			 * alpha will be the raw value from HTCP_CALC_ALPHA() if
418216115Slstewart			 * adaptive backoff is off, or the adjusted value if
419216115Slstewart			 * adaptive backoff is on.
420216115Slstewart			 */
421216115Slstewart			if (V_htcp_rtt_scaling)
422216115Slstewart				alpha = max(1, (min(max(HTCP_MINROWE,
423216115Slstewart				    (CCV(ccv, t_srtt) << HTCP_SHIFT) /
424216115Slstewart				    htcp_rtt_ref), HTCP_MAXROWE) * alpha)
425216115Slstewart				    >> HTCP_SHIFT);
426216115Slstewart
427216115Slstewart		} else
428216115Slstewart			alpha = 1;
429216115Slstewart
430216115Slstewart		htcp_data->alpha = alpha;
431216115Slstewart	}
432216115Slstewart}
433216115Slstewart
434216115Slstewartstatic void
435216115Slstewarthtcp_recalc_beta(struct cc_var *ccv)
436216115Slstewart{
437216115Slstewart	struct htcp *htcp_data;
438216115Slstewart
439216115Slstewart	htcp_data = ccv->cc_data;
440216115Slstewart
441216115Slstewart	/*
442216115Slstewart	 * TCPTV_SRTTBASE is the initialised value of each connection's SRTT, so
443216115Slstewart	 * we only calc beta if the connection's SRTT has been changed from its
444216115Slstewart	 * inital value. beta is bounded to ensure it is always between
445216115Slstewart	 * HTCP_MINBETA and HTCP_MAXBETA.
446216115Slstewart	 */
447216115Slstewart	if (V_htcp_adaptive_backoff && htcp_data->minrtt != TCPTV_SRTTBASE &&
448216115Slstewart	    htcp_data->maxrtt != TCPTV_SRTTBASE)
449216115Slstewart		htcp_data->beta = min(max(HTCP_MINBETA,
450216115Slstewart		    (htcp_data->minrtt << HTCP_SHIFT) / htcp_data->maxrtt),
451216115Slstewart		    HTCP_MAXBETA);
452216115Slstewart	else
453216115Slstewart		htcp_data->beta = HTCP_MINBETA;
454216115Slstewart}
455216115Slstewart
456216115Slstewart/*
457216115Slstewart * Record the minimum and maximum RTT seen for the connection. These are used in
458216115Slstewart * the calculation of beta if adaptive backoff is enabled.
459216115Slstewart */
460216115Slstewartstatic void
461216115Slstewarthtcp_record_rtt(struct cc_var *ccv)
462216115Slstewart{
463216115Slstewart	struct htcp *htcp_data;
464216115Slstewart
465216115Slstewart	htcp_data = ccv->cc_data;
466216115Slstewart
467216115Slstewart	/* XXXLAS: Should there be some hysteresis for minrtt? */
468216115Slstewart
469216115Slstewart	/*
470216115Slstewart	 * Record the current SRTT as our minrtt if it's the smallest we've seen
471216115Slstewart	 * or minrtt is currently equal to its initialised value. Ignore SRTT
472216115Slstewart	 * until a min number of samples have been taken.
473216115Slstewart	 */
474216115Slstewart	if ((CCV(ccv, t_srtt) < htcp_data->minrtt ||
475216115Slstewart	    htcp_data->minrtt == TCPTV_SRTTBASE) &&
476216115Slstewart	    (CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES))
477216115Slstewart		htcp_data->minrtt = CCV(ccv, t_srtt);
478216115Slstewart
479216115Slstewart	/*
480216115Slstewart	 * Record the current SRTT as our maxrtt if it's the largest we've
481216115Slstewart	 * seen. Ignore SRTT until a min number of samples have been taken.
482216115Slstewart	 */
483216115Slstewart	if (CCV(ccv, t_srtt) > htcp_data->maxrtt
484216115Slstewart	    && CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES)
485216115Slstewart		htcp_data->maxrtt = CCV(ccv, t_srtt);
486216115Slstewart}
487216115Slstewart
488216115Slstewart/*
489216115Slstewart * Update the ssthresh in the event of congestion.
490216115Slstewart */
491216115Slstewartstatic void
492216115Slstewarthtcp_ssthresh_update(struct cc_var *ccv)
493216115Slstewart{
494216115Slstewart	struct htcp *htcp_data;
495216115Slstewart
496216115Slstewart	htcp_data = ccv->cc_data;
497216115Slstewart
498216115Slstewart	/*
499216115Slstewart	 * On the first congestion event, set ssthresh to cwnd * 0.5, on
500216115Slstewart	 * subsequent congestion events, set it to cwnd * beta.
501216115Slstewart	 */
502216115Slstewart	if (CCV(ccv, snd_ssthresh) == TCP_MAXWIN << TCP_MAX_WINSHIFT)
503216115Slstewart		CCV(ccv, snd_ssthresh) = (CCV(ccv, snd_cwnd) * HTCP_MINBETA)
504216115Slstewart		    >> HTCP_SHIFT;
505216115Slstewart	else {
506216115Slstewart		htcp_recalc_beta(ccv);
507216115Slstewart		CCV(ccv, snd_ssthresh) = (CCV(ccv, snd_cwnd) * htcp_data->beta)
508216115Slstewart		    >> HTCP_SHIFT;
509216115Slstewart	}
510216115Slstewart}
511216115Slstewart
512216115Slstewart
513216115SlstewartSYSCTL_DECL(_net_inet_tcp_cc_htcp);
514216115SlstewartSYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, htcp, CTLFLAG_RW,
515216115Slstewart    NULL, "H-TCP related settings");
516216115SlstewartSYSCTL_VNET_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, adaptive_backoff, CTLFLAG_RW,
517216115Slstewart    &VNET_NAME(htcp_adaptive_backoff), 0, "enable H-TCP adaptive backoff");
518216115SlstewartSYSCTL_VNET_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, rtt_scaling, CTLFLAG_RW,
519216115Slstewart    &VNET_NAME(htcp_rtt_scaling), 0, "enable H-TCP RTT scaling");
520216115Slstewart
521216115SlstewartDECLARE_CC_MODULE(htcp, &htcp_cc_algo);
522