cc_htcp.c revision 298995
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
9220560Slstewart * Architectures, Swinburne University of Technology, by Lawrence Stewart and
10220560Slstewart * James Healy, made possible in part by a grant from the Cisco University
11220560Slstewart * Research Program 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
43220560Slstewart * Swinburne University of Technology's Centre for Advanced Internet
44220560Slstewart * Architectures, Melbourne, Australia, which was made possible in part by a
45220560Slstewart * grant from the Cisco University Research Program Fund at Community Foundation
46220560Slstewart * Silicon Valley. More 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 298995 2016-05-03 18:05:43Z pfg $");
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
65294535Sglebius#include <netinet/tcp.h>
66216115Slstewart#include <netinet/tcp_seq.h>
67216115Slstewart#include <netinet/tcp_timer.h>
68216115Slstewart#include <netinet/tcp_var.h>
69294931Sglebius#include <netinet/cc/cc.h>
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
176220592Spluknetstatic MALLOC_DEFINE(M_HTCP, "htcp data",
177216115Slstewart    "Per connection data required for the HTCP congestion control algorithm");
178216115Slstewart
179216115Slstewartstruct cc_algo htcp_cc_algo = {
180216115Slstewart	.name = "htcp",
181216115Slstewart	.ack_received = htcp_ack_received,
182216115Slstewart	.cb_destroy = htcp_cb_destroy,
183216115Slstewart	.cb_init = htcp_cb_init,
184216115Slstewart	.cong_signal = htcp_cong_signal,
185216115Slstewart	.mod_init = htcp_mod_init,
186216115Slstewart	.post_recovery = htcp_post_recovery,
187216115Slstewart};
188216115Slstewart
189216115Slstewartstatic void
190216115Slstewarthtcp_ack_received(struct cc_var *ccv, uint16_t type)
191216115Slstewart{
192216115Slstewart	struct htcp *htcp_data;
193216115Slstewart
194216115Slstewart	htcp_data = ccv->cc_data;
195216115Slstewart	htcp_record_rtt(ccv);
196216115Slstewart
197216115Slstewart	/*
198216115Slstewart	 * Regular ACK and we're not in cong/fast recovery and we're cwnd
199216115Slstewart	 * limited and we're either not doing ABC or are slow starting or are
200216115Slstewart	 * doing ABC and we've sent a cwnd's worth of bytes.
201216115Slstewart	 */
202216115Slstewart	if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
203216115Slstewart	    (ccv->flags & CCF_CWND_LIMITED) && (!V_tcp_do_rfc3465 ||
204216115Slstewart	    CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||
205216115Slstewart	    (V_tcp_do_rfc3465 && ccv->flags & CCF_ABC_SENTAWND))) {
206216115Slstewart		htcp_recalc_beta(ccv);
207216115Slstewart		htcp_recalc_alpha(ccv);
208216115Slstewart		/*
209216115Slstewart		 * Use the logic in NewReno ack_received() for slow start and
210216115Slstewart		 * for the first HTCP_DELTA_L ticks after either the flow starts
211216115Slstewart		 * or a congestion event (when alpha equals 1).
212216115Slstewart		 */
213216115Slstewart		if (htcp_data->alpha == 1 ||
214216115Slstewart		    CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh))
215216115Slstewart			newreno_cc_algo.ack_received(ccv, type);
216216115Slstewart		else {
217216115Slstewart			if (V_tcp_do_rfc3465) {
218216115Slstewart				/* Increment cwnd by alpha segments. */
219216115Slstewart				CCV(ccv, snd_cwnd) += htcp_data->alpha *
220216115Slstewart				    CCV(ccv, t_maxseg);
221216115Slstewart				ccv->flags &= ~CCF_ABC_SENTAWND;
222216115Slstewart			} else
223216115Slstewart				/*
224216115Slstewart				 * Increment cwnd by alpha/cwnd segments to
225216115Slstewart				 * approximate an increase of alpha segments
226216115Slstewart				 * per RTT.
227216115Slstewart				 */
228216115Slstewart				CCV(ccv, snd_cwnd) += (((htcp_data->alpha <<
229216115Slstewart				    HTCP_SHIFT) / (CCV(ccv, snd_cwnd) /
230216115Slstewart				    CCV(ccv, t_maxseg))) * CCV(ccv, t_maxseg))
231216115Slstewart				    >> HTCP_SHIFT;
232216115Slstewart		}
233216115Slstewart	}
234216115Slstewart}
235216115Slstewart
236216115Slstewartstatic void
237216115Slstewarthtcp_cb_destroy(struct cc_var *ccv)
238216115Slstewart{
239216115Slstewart
240216115Slstewart	if (ccv->cc_data != NULL)
241216115Slstewart		free(ccv->cc_data, M_HTCP);
242216115Slstewart}
243216115Slstewart
244216115Slstewartstatic int
245216115Slstewarthtcp_cb_init(struct cc_var *ccv)
246216115Slstewart{
247216115Slstewart	struct htcp *htcp_data;
248216115Slstewart
249216115Slstewart	htcp_data = malloc(sizeof(struct htcp), M_HTCP, M_NOWAIT);
250216115Slstewart
251216115Slstewart	if (htcp_data == NULL)
252216115Slstewart		return (ENOMEM);
253216115Slstewart
254216115Slstewart	/* Init some key variables with sensible defaults. */
255216115Slstewart	htcp_data->alpha = HTCP_INIT_ALPHA;
256216115Slstewart	htcp_data->beta = HTCP_MINBETA;
257216115Slstewart	htcp_data->maxrtt = TCPTV_SRTTBASE;
258216115Slstewart	htcp_data->minrtt = TCPTV_SRTTBASE;
259216115Slstewart	htcp_data->prev_cwnd = 0;
260216115Slstewart	htcp_data->t_last_cong = ticks;
261216115Slstewart
262216115Slstewart	ccv->cc_data = htcp_data;
263216115Slstewart
264216115Slstewart	return (0);
265216115Slstewart}
266216115Slstewart
267216115Slstewart/*
268216115Slstewart * Perform any necessary tasks before we enter congestion recovery.
269216115Slstewart */
270216115Slstewartstatic void
271216115Slstewarthtcp_cong_signal(struct cc_var *ccv, uint32_t type)
272216115Slstewart{
273216115Slstewart	struct htcp *htcp_data;
274216115Slstewart
275216115Slstewart	htcp_data = ccv->cc_data;
276216115Slstewart
277216115Slstewart	switch (type) {
278216115Slstewart	case CC_NDUPACK:
279216115Slstewart		if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
280216115Slstewart			if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
281216115Slstewart				/*
282216115Slstewart				 * Apply hysteresis to maxrtt to ensure
283216115Slstewart				 * reductions in the RTT are reflected in our
284216115Slstewart				 * measurements.
285216115Slstewart				 */
286216115Slstewart				htcp_data->maxrtt = (htcp_data->minrtt +
287216115Slstewart				    (htcp_data->maxrtt - htcp_data->minrtt) *
288216115Slstewart				    95) / 100;
289216115Slstewart				htcp_ssthresh_update(ccv);
290216115Slstewart				htcp_data->t_last_cong = ticks;
291216115Slstewart				htcp_data->prev_cwnd = CCV(ccv, snd_cwnd);
292216115Slstewart			}
293216115Slstewart			ENTER_RECOVERY(CCV(ccv, t_flags));
294216115Slstewart		}
295216115Slstewart		break;
296216115Slstewart
297216115Slstewart	case CC_ECN:
298216115Slstewart		if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
299216115Slstewart			/*
300216115Slstewart			 * Apply hysteresis to maxrtt to ensure reductions in
301216115Slstewart			 * the RTT are reflected in our measurements.
302216115Slstewart			 */
303216115Slstewart			htcp_data->maxrtt = (htcp_data->minrtt + (htcp_data->maxrtt -
304216115Slstewart			    htcp_data->minrtt) * 95) / 100;
305216115Slstewart			htcp_ssthresh_update(ccv);
306216115Slstewart			CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
307216115Slstewart			htcp_data->t_last_cong = ticks;
308216115Slstewart			htcp_data->prev_cwnd = CCV(ccv, snd_cwnd);
309216115Slstewart			ENTER_CONGRECOVERY(CCV(ccv, t_flags));
310216115Slstewart		}
311216115Slstewart		break;
312216115Slstewart
313216115Slstewart	case CC_RTO:
314216115Slstewart		/*
315216115Slstewart		 * Grab the current time and record it so we know when the
316216115Slstewart		 * most recent congestion event was. Only record it when the
317216115Slstewart		 * timeout has fired more than once, as there is a reasonable
318216115Slstewart		 * chance the first one is a false alarm and may not indicate
319216115Slstewart		 * congestion.
320216115Slstewart		 */
321216115Slstewart		if (CCV(ccv, t_rxtshift) >= 2)
322216115Slstewart			htcp_data->t_last_cong = ticks;
323216115Slstewart		break;
324216115Slstewart	}
325216115Slstewart}
326216115Slstewart
327216115Slstewartstatic int
328216115Slstewarthtcp_mod_init(void)
329216115Slstewart{
330216115Slstewart
331216115Slstewart	htcp_cc_algo.after_idle = newreno_cc_algo.after_idle;
332216115Slstewart
333216115Slstewart	/*
334216115Slstewart	 * HTCP_RTT_REF is defined in ms, and t_srtt in the tcpcb is stored in
335216115Slstewart	 * units of TCP_RTT_SCALE*hz. Scale HTCP_RTT_REF to be in the same units
336216115Slstewart	 * as t_srtt.
337216115Slstewart	 */
338216115Slstewart	htcp_rtt_ref = (HTCP_RTT_REF * TCP_RTT_SCALE * hz) / 1000;
339216115Slstewart
340216115Slstewart	return (0);
341216115Slstewart}
342216115Slstewart
343216115Slstewart/*
344216115Slstewart * Perform any necessary tasks before we exit congestion recovery.
345216115Slstewart */
346216115Slstewartstatic void
347216115Slstewarthtcp_post_recovery(struct cc_var *ccv)
348216115Slstewart{
349216115Slstewart	struct htcp *htcp_data;
350216115Slstewart
351216115Slstewart	htcp_data = ccv->cc_data;
352216115Slstewart
353216115Slstewart	if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
354216115Slstewart		/*
355216115Slstewart		 * If inflight data is less than ssthresh, set cwnd
356216115Slstewart		 * conservatively to avoid a burst of data, as suggested in the
357216115Slstewart		 * NewReno RFC. Otherwise, use the HTCP method.
358216115Slstewart		 *
359216115Slstewart		 * XXXLAS: Find a way to do this without needing curack
360216115Slstewart		 */
361216115Slstewart		if (SEQ_GT(ccv->curack + CCV(ccv, snd_ssthresh),
362216115Slstewart		    CCV(ccv, snd_max)))
363216115Slstewart			CCV(ccv, snd_cwnd) = CCV(ccv, snd_max) - ccv->curack +
364216115Slstewart			    CCV(ccv, t_maxseg);
365216115Slstewart		else
366216115Slstewart			CCV(ccv, snd_cwnd) = max(1, ((htcp_data->beta *
367216115Slstewart			    htcp_data->prev_cwnd / CCV(ccv, t_maxseg))
368216115Slstewart			    >> HTCP_SHIFT)) * CCV(ccv, t_maxseg);
369216115Slstewart	}
370216115Slstewart}
371216115Slstewart
372216115Slstewartstatic void
373216115Slstewarthtcp_recalc_alpha(struct cc_var *ccv)
374216115Slstewart{
375216115Slstewart	struct htcp *htcp_data;
376216115Slstewart	int alpha, diff, now;
377216115Slstewart
378216115Slstewart	htcp_data = ccv->cc_data;
379216115Slstewart	now = ticks;
380216115Slstewart
381216115Slstewart	/*
382216115Slstewart	 * If ticks has wrapped around (will happen approximately once every 49
383216115Slstewart	 * days on a machine with the default kern.hz=1000) and a flow straddles
384216115Slstewart	 * the wrap point, our alpha calcs will be completely wrong. We cut our
385216115Slstewart	 * losses and restart alpha from scratch by setting t_last_cong = now -
386216115Slstewart	 * HTCP_DELTA_L.
387216115Slstewart	 *
388216115Slstewart	 * This does not deflate our cwnd at all. It simply slows the rate cwnd
389216115Slstewart	 * is growing by until alpha regains the value it held prior to taking
390216115Slstewart	 * this drastic measure.
391216115Slstewart	 */
392216115Slstewart	if (now < htcp_data->t_last_cong)
393216115Slstewart		htcp_data->t_last_cong = now - HTCP_DELTA_L;
394216115Slstewart
395216115Slstewart	diff = now - htcp_data->t_last_cong - HTCP_DELTA_L;
396216115Slstewart
397216115Slstewart	/* Cap alpha if the value of diff would overflow HTCP_CALC_ALPHA(). */
398216115Slstewart	if (diff < htcp_max_diff) {
399216115Slstewart		/*
400216115Slstewart		 * If it has been more than HTCP_DELTA_L ticks since congestion,
401216115Slstewart		 * increase alpha according to the function defined in the spec.
402216115Slstewart		 */
403216115Slstewart		if (diff > 0) {
404216115Slstewart			alpha = HTCP_CALC_ALPHA(diff);
405216115Slstewart
406216115Slstewart			/*
407216115Slstewart			 * Adaptive backoff fairness adjustment:
408216115Slstewart			 * 2 * (1 - beta) * alpha_raw
409216115Slstewart			 */
410216115Slstewart			if (V_htcp_adaptive_backoff)
411216115Slstewart				alpha = max(1, (2 * ((1 << HTCP_SHIFT) -
412216115Slstewart				    htcp_data->beta) * alpha) >> HTCP_SHIFT);
413216115Slstewart
414216115Slstewart			/*
415216115Slstewart			 * RTT scaling: (RTT / RTT_ref) * alpha
416216115Slstewart			 * alpha will be the raw value from HTCP_CALC_ALPHA() if
417216115Slstewart			 * adaptive backoff is off, or the adjusted value if
418216115Slstewart			 * adaptive backoff is on.
419216115Slstewart			 */
420216115Slstewart			if (V_htcp_rtt_scaling)
421216115Slstewart				alpha = max(1, (min(max(HTCP_MINROWE,
422216115Slstewart				    (CCV(ccv, t_srtt) << HTCP_SHIFT) /
423216115Slstewart				    htcp_rtt_ref), HTCP_MAXROWE) * alpha)
424216115Slstewart				    >> HTCP_SHIFT);
425216115Slstewart
426216115Slstewart		} else
427216115Slstewart			alpha = 1;
428216115Slstewart
429216115Slstewart		htcp_data->alpha = alpha;
430216115Slstewart	}
431216115Slstewart}
432216115Slstewart
433216115Slstewartstatic void
434216115Slstewarthtcp_recalc_beta(struct cc_var *ccv)
435216115Slstewart{
436216115Slstewart	struct htcp *htcp_data;
437216115Slstewart
438216115Slstewart	htcp_data = ccv->cc_data;
439216115Slstewart
440216115Slstewart	/*
441216115Slstewart	 * TCPTV_SRTTBASE is the initialised value of each connection's SRTT, so
442216115Slstewart	 * we only calc beta if the connection's SRTT has been changed from its
443298995Spfg	 * initial value. beta is bounded to ensure it is always between
444216115Slstewart	 * HTCP_MINBETA and HTCP_MAXBETA.
445216115Slstewart	 */
446216115Slstewart	if (V_htcp_adaptive_backoff && htcp_data->minrtt != TCPTV_SRTTBASE &&
447216115Slstewart	    htcp_data->maxrtt != TCPTV_SRTTBASE)
448216115Slstewart		htcp_data->beta = min(max(HTCP_MINBETA,
449216115Slstewart		    (htcp_data->minrtt << HTCP_SHIFT) / htcp_data->maxrtt),
450216115Slstewart		    HTCP_MAXBETA);
451216115Slstewart	else
452216115Slstewart		htcp_data->beta = HTCP_MINBETA;
453216115Slstewart}
454216115Slstewart
455216115Slstewart/*
456216115Slstewart * Record the minimum and maximum RTT seen for the connection. These are used in
457216115Slstewart * the calculation of beta if adaptive backoff is enabled.
458216115Slstewart */
459216115Slstewartstatic void
460216115Slstewarthtcp_record_rtt(struct cc_var *ccv)
461216115Slstewart{
462216115Slstewart	struct htcp *htcp_data;
463216115Slstewart
464216115Slstewart	htcp_data = ccv->cc_data;
465216115Slstewart
466216115Slstewart	/* XXXLAS: Should there be some hysteresis for minrtt? */
467216115Slstewart
468216115Slstewart	/*
469216115Slstewart	 * Record the current SRTT as our minrtt if it's the smallest we've seen
470216115Slstewart	 * or minrtt is currently equal to its initialised value. Ignore SRTT
471216115Slstewart	 * until a min number of samples have been taken.
472216115Slstewart	 */
473216115Slstewart	if ((CCV(ccv, t_srtt) < htcp_data->minrtt ||
474216115Slstewart	    htcp_data->minrtt == TCPTV_SRTTBASE) &&
475216115Slstewart	    (CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES))
476216115Slstewart		htcp_data->minrtt = CCV(ccv, t_srtt);
477216115Slstewart
478216115Slstewart	/*
479216115Slstewart	 * Record the current SRTT as our maxrtt if it's the largest we've
480216115Slstewart	 * seen. Ignore SRTT until a min number of samples have been taken.
481216115Slstewart	 */
482216115Slstewart	if (CCV(ccv, t_srtt) > htcp_data->maxrtt
483216115Slstewart	    && CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES)
484216115Slstewart		htcp_data->maxrtt = CCV(ccv, t_srtt);
485216115Slstewart}
486216115Slstewart
487216115Slstewart/*
488216115Slstewart * Update the ssthresh in the event of congestion.
489216115Slstewart */
490216115Slstewartstatic void
491216115Slstewarthtcp_ssthresh_update(struct cc_var *ccv)
492216115Slstewart{
493216115Slstewart	struct htcp *htcp_data;
494216115Slstewart
495216115Slstewart	htcp_data = ccv->cc_data;
496216115Slstewart
497216115Slstewart	/*
498216115Slstewart	 * On the first congestion event, set ssthresh to cwnd * 0.5, on
499216115Slstewart	 * subsequent congestion events, set it to cwnd * beta.
500216115Slstewart	 */
501216115Slstewart	if (CCV(ccv, snd_ssthresh) == TCP_MAXWIN << TCP_MAX_WINSHIFT)
502216115Slstewart		CCV(ccv, snd_ssthresh) = (CCV(ccv, snd_cwnd) * HTCP_MINBETA)
503216115Slstewart		    >> HTCP_SHIFT;
504216115Slstewart	else {
505216115Slstewart		htcp_recalc_beta(ccv);
506216115Slstewart		CCV(ccv, snd_ssthresh) = (CCV(ccv, snd_cwnd) * htcp_data->beta)
507216115Slstewart		    >> HTCP_SHIFT;
508216115Slstewart	}
509216115Slstewart}
510216115Slstewart
511216115Slstewart
512216115SlstewartSYSCTL_DECL(_net_inet_tcp_cc_htcp);
513216115SlstewartSYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, htcp, CTLFLAG_RW,
514216115Slstewart    NULL, "H-TCP related settings");
515274225SglebiusSYSCTL_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, adaptive_backoff,
516274225Sglebius    CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(htcp_adaptive_backoff), 0,
517274225Sglebius    "enable H-TCP adaptive backoff");
518274225SglebiusSYSCTL_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, rtt_scaling,
519274225Sglebius    CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(htcp_rtt_scaling), 0,
520274225Sglebius    "enable H-TCP RTT scaling");
521216115Slstewart
522216115SlstewartDECLARE_CC_MODULE(htcp, &htcp_cc_algo);
523