xref: /freebsd/sys/netinet/cc/cc_htcp.c (revision a0b9e2e854027e6ff61fb075a1309dbc71c42b54)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007-2008
5  * 	Swinburne University of Technology, Melbourne, Australia
6  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
7  * Copyright (c) 2010 The FreeBSD Foundation
8  * All rights reserved.
9  *
10  * This software was developed at the Centre for Advanced Internet
11  * Architectures, Swinburne University of Technology, by Lawrence Stewart and
12  * James Healy, made possible in part by a grant from the Cisco University
13  * Research Program Fund at Community Foundation Silicon Valley.
14  *
15  * Portions of this software were developed at the Centre for Advanced
16  * Internet Architectures, Swinburne University of Technology, Melbourne,
17  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 /*
42  * An implementation of the H-TCP congestion control algorithm for FreeBSD,
43  * based on the Internet Draft "draft-leith-tcp-htcp-06.txt" by Leith and
44  * Shorten. Originally released as part of the NewTCP research project at
45  * Swinburne University of Technology's Centre for Advanced Internet
46  * Architectures, Melbourne, Australia, which was made possible in part by a
47  * grant from the Cisco University Research Program Fund at Community Foundation
48  * Silicon Valley. More details are available at:
49  *   http://caia.swin.edu.au/urp/newtcp/
50  */
51 
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
54 
55 #include <sys/param.h>
56 #include <sys/kernel.h>
57 #include <sys/limits.h>
58 #include <sys/malloc.h>
59 #include <sys/module.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/sysctl.h>
63 #include <sys/systm.h>
64 
65 #include <net/vnet.h>
66 
67 #include <netinet/tcp.h>
68 #include <netinet/tcp_seq.h>
69 #include <netinet/tcp_timer.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet/cc/cc.h>
72 #include <netinet/cc/cc_module.h>
73 
74 /* Fixed point math shifts. */
75 #define HTCP_SHIFT 8
76 #define HTCP_ALPHA_INC_SHIFT 4
77 
78 #define HTCP_INIT_ALPHA 1
79 #define HTCP_DELTA_L hz		/* 1 sec in ticks. */
80 #define HTCP_MINBETA 128	/* 0.5 << HTCP_SHIFT. */
81 #define HTCP_MAXBETA 204	/* ~0.8 << HTCP_SHIFT. */
82 #define HTCP_MINROWE 26		/* ~0.1 << HTCP_SHIFT. */
83 #define HTCP_MAXROWE 512	/* 2 << HTCP_SHIFT. */
84 
85 /* RTT_ref (ms) used in the calculation of alpha if RTT scaling is enabled. */
86 #define HTCP_RTT_REF 100
87 
88 /* Don't trust SRTT until this many samples have been taken. */
89 #define HTCP_MIN_RTT_SAMPLES 8
90 
91 /*
92  * HTCP_CALC_ALPHA performs a fixed point math calculation to determine the
93  * value of alpha, based on the function defined in the HTCP spec.
94  *
95  * i.e. 1 + 10(delta - delta_l) + ((delta - delta_l) / 2) ^ 2
96  *
97  * "diff" is passed in to the macro as "delta - delta_l" and is expected to be
98  * in units of ticks.
99  *
100  * The joyousnous of fixed point maths means our function implementation looks a
101  * little funky...
102  *
103  * In order to maintain some precision in the calculations, a fixed point shift
104  * HTCP_ALPHA_INC_SHIFT is used to ensure the integer divisions don't
105  * truncate the results too badly.
106  *
107  * The "16" value is the "1" term in the alpha function shifted up by
108  * HTCP_ALPHA_INC_SHIFT
109  *
110  * The "160" value is the "10" multiplier in the alpha function multiplied by
111  * 2^HTCP_ALPHA_INC_SHIFT
112  *
113  * Specifying these as constants reduces the computations required. After
114  * up-shifting all the terms in the function and performing the required
115  * calculations, we down-shift the final result by HTCP_ALPHA_INC_SHIFT to
116  * ensure it is back in the correct range.
117  *
118  * The "hz" terms are required as kernels can be configured to run with
119  * different tick timers, which we have to adjust for in the alpha calculation
120  * (which originally was defined in terms of seconds).
121  *
122  * We also have to be careful to constrain the value of diff such that it won't
123  * overflow whilst performing the calculation. The middle term i.e. (160 * diff)
124  * / hz is the limiting factor in the calculation. We must constrain diff to be
125  * less than the max size of an int divided by the constant 160 figure
126  * i.e. diff < INT_MAX / 160
127  *
128  * NB: Changing HTCP_ALPHA_INC_SHIFT will require you to MANUALLY update the
129  * constants used in this function!
130  */
131 #define HTCP_CALC_ALPHA(diff) \
132 ((\
133 	(16) + \
134 	((160 * (diff)) / hz) + \
135 	(((diff) / hz) * (((diff) << HTCP_ALPHA_INC_SHIFT) / (4 * hz))) \
136 ) >> HTCP_ALPHA_INC_SHIFT)
137 
138 static void	htcp_ack_received(struct cc_var *ccv, uint16_t type);
139 static void	htcp_cb_destroy(struct cc_var *ccv);
140 static int	htcp_cb_init(struct cc_var *ccv);
141 static void	htcp_cong_signal(struct cc_var *ccv, uint32_t type);
142 static int	htcp_mod_init(void);
143 static void	htcp_post_recovery(struct cc_var *ccv);
144 static void	htcp_recalc_alpha(struct cc_var *ccv);
145 static void	htcp_recalc_beta(struct cc_var *ccv);
146 static void	htcp_record_rtt(struct cc_var *ccv);
147 static void	htcp_ssthresh_update(struct cc_var *ccv);
148 
149 struct htcp {
150 	/* cwnd before entering cong recovery. */
151 	unsigned long	prev_cwnd;
152 	/* cwnd additive increase parameter. */
153 	int		alpha;
154 	/* cwnd multiplicative decrease parameter. */
155 	int		beta;
156 	/* Largest rtt seen for the flow. */
157 	int		maxrtt;
158 	/* Shortest rtt seen for the flow. */
159 	int		minrtt;
160 	/* Time of last congestion event in ticks. */
161 	int		t_last_cong;
162 };
163 
164 static int htcp_rtt_ref;
165 /*
166  * The maximum number of ticks the value of diff can reach in
167  * htcp_recalc_alpha() before alpha will stop increasing due to overflow.
168  * See comment above HTCP_CALC_ALPHA for more info.
169  */
170 static int htcp_max_diff = INT_MAX / ((1 << HTCP_ALPHA_INC_SHIFT) * 10);
171 
172 /* Per-netstack vars. */
173 VNET_DEFINE_STATIC(u_int, htcp_adaptive_backoff) = 0;
174 VNET_DEFINE_STATIC(u_int, htcp_rtt_scaling) = 0;
175 #define	V_htcp_adaptive_backoff    VNET(htcp_adaptive_backoff)
176 #define	V_htcp_rtt_scaling    VNET(htcp_rtt_scaling)
177 
178 static MALLOC_DEFINE(M_HTCP, "htcp data",
179     "Per connection data required for the HTCP congestion control algorithm");
180 
181 struct cc_algo htcp_cc_algo = {
182 	.name = "htcp",
183 	.ack_received = htcp_ack_received,
184 	.cb_destroy = htcp_cb_destroy,
185 	.cb_init = htcp_cb_init,
186 	.cong_signal = htcp_cong_signal,
187 	.mod_init = htcp_mod_init,
188 	.post_recovery = htcp_post_recovery,
189 };
190 
191 static void
192 htcp_ack_received(struct cc_var *ccv, uint16_t type)
193 {
194 	struct htcp *htcp_data;
195 
196 	htcp_data = ccv->cc_data;
197 	htcp_record_rtt(ccv);
198 
199 	/*
200 	 * Regular ACK and we're not in cong/fast recovery and we're cwnd
201 	 * limited and we're either not doing ABC or are slow starting or are
202 	 * doing ABC and we've sent a cwnd's worth of bytes.
203 	 */
204 	if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
205 	    (ccv->flags & CCF_CWND_LIMITED) && (!V_tcp_do_rfc3465 ||
206 	    CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||
207 	    (V_tcp_do_rfc3465 && ccv->flags & CCF_ABC_SENTAWND))) {
208 		htcp_recalc_beta(ccv);
209 		htcp_recalc_alpha(ccv);
210 		/*
211 		 * Use the logic in NewReno ack_received() for slow start and
212 		 * for the first HTCP_DELTA_L ticks after either the flow starts
213 		 * or a congestion event (when alpha equals 1).
214 		 */
215 		if (htcp_data->alpha == 1 ||
216 		    CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh))
217 			newreno_cc_algo.ack_received(ccv, type);
218 		else {
219 			if (V_tcp_do_rfc3465) {
220 				/* Increment cwnd by alpha segments. */
221 				CCV(ccv, snd_cwnd) += htcp_data->alpha *
222 				    CCV(ccv, t_maxseg);
223 				ccv->flags &= ~CCF_ABC_SENTAWND;
224 			} else
225 				/*
226 				 * Increment cwnd by alpha/cwnd segments to
227 				 * approximate an increase of alpha segments
228 				 * per RTT.
229 				 */
230 				CCV(ccv, snd_cwnd) += (((htcp_data->alpha <<
231 				    HTCP_SHIFT) / (CCV(ccv, snd_cwnd) /
232 				    CCV(ccv, t_maxseg))) * CCV(ccv, t_maxseg))
233 				    >> HTCP_SHIFT;
234 		}
235 	}
236 }
237 
238 static void
239 htcp_cb_destroy(struct cc_var *ccv)
240 {
241 	free(ccv->cc_data, M_HTCP);
242 }
243 
244 static int
245 htcp_cb_init(struct cc_var *ccv)
246 {
247 	struct htcp *htcp_data;
248 
249 	htcp_data = malloc(sizeof(struct htcp), M_HTCP, M_NOWAIT);
250 
251 	if (htcp_data == NULL)
252 		return (ENOMEM);
253 
254 	/* Init some key variables with sensible defaults. */
255 	htcp_data->alpha = HTCP_INIT_ALPHA;
256 	htcp_data->beta = HTCP_MINBETA;
257 	htcp_data->maxrtt = TCPTV_SRTTBASE;
258 	htcp_data->minrtt = TCPTV_SRTTBASE;
259 	htcp_data->prev_cwnd = 0;
260 	htcp_data->t_last_cong = ticks;
261 
262 	ccv->cc_data = htcp_data;
263 
264 	return (0);
265 }
266 
267 /*
268  * Perform any necessary tasks before we enter congestion recovery.
269  */
270 static void
271 htcp_cong_signal(struct cc_var *ccv, uint32_t type)
272 {
273 	struct htcp *htcp_data;
274 	u_int mss;
275 
276 	htcp_data = ccv->cc_data;
277 	mss = tcp_maxseg(ccv->ccvc.tcp);
278 
279 	switch (type) {
280 	case CC_NDUPACK:
281 		if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
282 			if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
283 				/*
284 				 * Apply hysteresis to maxrtt to ensure
285 				 * reductions in the RTT are reflected in our
286 				 * measurements.
287 				 */
288 				htcp_data->maxrtt = (htcp_data->minrtt +
289 				    (htcp_data->maxrtt - htcp_data->minrtt) *
290 				    95) / 100;
291 				htcp_ssthresh_update(ccv);
292 				htcp_data->t_last_cong = ticks;
293 				htcp_data->prev_cwnd = CCV(ccv, snd_cwnd);
294 			}
295 			ENTER_RECOVERY(CCV(ccv, t_flags));
296 		}
297 		break;
298 
299 	case CC_ECN:
300 		if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
301 			/*
302 			 * Apply hysteresis to maxrtt to ensure reductions in
303 			 * the RTT are reflected in our measurements.
304 			 */
305 			htcp_data->maxrtt = (htcp_data->minrtt + (htcp_data->maxrtt -
306 			    htcp_data->minrtt) * 95) / 100;
307 			htcp_ssthresh_update(ccv);
308 			CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
309 			htcp_data->t_last_cong = ticks;
310 			htcp_data->prev_cwnd = CCV(ccv, snd_cwnd);
311 			ENTER_CONGRECOVERY(CCV(ccv, t_flags));
312 		}
313 		break;
314 
315 	case CC_RTO:
316 		CCV(ccv, snd_ssthresh) = max(min(CCV(ccv, snd_wnd),
317 						 CCV(ccv, snd_cwnd)) / 2 / mss,
318 					     2) * mss;
319 		CCV(ccv, snd_cwnd) = mss;
320 		/*
321 		 * Grab the current time and record it so we know when the
322 		 * most recent congestion event was. Only record it when the
323 		 * timeout has fired more than once, as there is a reasonable
324 		 * chance the first one is a false alarm and may not indicate
325 		 * congestion.
326 		 */
327 		if (CCV(ccv, t_rxtshift) >= 2)
328 			htcp_data->t_last_cong = ticks;
329 		break;
330 	}
331 }
332 
333 static int
334 htcp_mod_init(void)
335 {
336 
337 	htcp_cc_algo.after_idle = newreno_cc_algo.after_idle;
338 
339 	/*
340 	 * HTCP_RTT_REF is defined in ms, and t_srtt in the tcpcb is stored in
341 	 * units of TCP_RTT_SCALE*hz. Scale HTCP_RTT_REF to be in the same units
342 	 * as t_srtt.
343 	 */
344 	htcp_rtt_ref = (HTCP_RTT_REF * TCP_RTT_SCALE * hz) / 1000;
345 
346 	return (0);
347 }
348 
349 /*
350  * Perform any necessary tasks before we exit congestion recovery.
351  */
352 static void
353 htcp_post_recovery(struct cc_var *ccv)
354 {
355 	int pipe;
356 	struct htcp *htcp_data;
357 
358 	pipe = 0;
359 	htcp_data = ccv->cc_data;
360 
361 	if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
362 		/*
363 		 * If inflight data is less than ssthresh, set cwnd
364 		 * conservatively to avoid a burst of data, as suggested in the
365 		 * NewReno RFC. Otherwise, use the HTCP method.
366 		 *
367 		 * XXXLAS: Find a way to do this without needing curack
368 		 */
369 		if (V_tcp_do_rfc6675_pipe)
370 			pipe = tcp_compute_pipe(ccv->ccvc.tcp);
371 		else
372 			pipe = CCV(ccv, snd_max) - ccv->curack;
373 
374 		if (pipe < CCV(ccv, snd_ssthresh))
375 			/*
376 			 * Ensure that cwnd down not collape to 1 MSS under
377 			 * adverse conditions. Implements RFC6582
378 			 */
379 			CCV(ccv, snd_cwnd) = max(pipe, CCV(ccv, t_maxseg)) +
380 			    CCV(ccv, t_maxseg);
381 		else
382 			CCV(ccv, snd_cwnd) = max(1, ((htcp_data->beta *
383 			    htcp_data->prev_cwnd / CCV(ccv, t_maxseg))
384 			    >> HTCP_SHIFT)) * CCV(ccv, t_maxseg);
385 	}
386 }
387 
388 static void
389 htcp_recalc_alpha(struct cc_var *ccv)
390 {
391 	struct htcp *htcp_data;
392 	int alpha, diff, now;
393 
394 	htcp_data = ccv->cc_data;
395 	now = ticks;
396 
397 	/*
398 	 * If ticks has wrapped around (will happen approximately once every 49
399 	 * days on a machine with the default kern.hz=1000) and a flow straddles
400 	 * the wrap point, our alpha calcs will be completely wrong. We cut our
401 	 * losses and restart alpha from scratch by setting t_last_cong = now -
402 	 * HTCP_DELTA_L.
403 	 *
404 	 * This does not deflate our cwnd at all. It simply slows the rate cwnd
405 	 * is growing by until alpha regains the value it held prior to taking
406 	 * this drastic measure.
407 	 */
408 	if (now < htcp_data->t_last_cong)
409 		htcp_data->t_last_cong = now - HTCP_DELTA_L;
410 
411 	diff = now - htcp_data->t_last_cong - HTCP_DELTA_L;
412 
413 	/* Cap alpha if the value of diff would overflow HTCP_CALC_ALPHA(). */
414 	if (diff < htcp_max_diff) {
415 		/*
416 		 * If it has been more than HTCP_DELTA_L ticks since congestion,
417 		 * increase alpha according to the function defined in the spec.
418 		 */
419 		if (diff > 0) {
420 			alpha = HTCP_CALC_ALPHA(diff);
421 
422 			/*
423 			 * Adaptive backoff fairness adjustment:
424 			 * 2 * (1 - beta) * alpha_raw
425 			 */
426 			if (V_htcp_adaptive_backoff)
427 				alpha = max(1, (2 * ((1 << HTCP_SHIFT) -
428 				    htcp_data->beta) * alpha) >> HTCP_SHIFT);
429 
430 			/*
431 			 * RTT scaling: (RTT / RTT_ref) * alpha
432 			 * alpha will be the raw value from HTCP_CALC_ALPHA() if
433 			 * adaptive backoff is off, or the adjusted value if
434 			 * adaptive backoff is on.
435 			 */
436 			if (V_htcp_rtt_scaling)
437 				alpha = max(1, (min(max(HTCP_MINROWE,
438 				    (CCV(ccv, t_srtt) << HTCP_SHIFT) /
439 				    htcp_rtt_ref), HTCP_MAXROWE) * alpha)
440 				    >> HTCP_SHIFT);
441 
442 		} else
443 			alpha = 1;
444 
445 		htcp_data->alpha = alpha;
446 	}
447 }
448 
449 static void
450 htcp_recalc_beta(struct cc_var *ccv)
451 {
452 	struct htcp *htcp_data;
453 
454 	htcp_data = ccv->cc_data;
455 
456 	/*
457 	 * TCPTV_SRTTBASE is the initialised value of each connection's SRTT, so
458 	 * we only calc beta if the connection's SRTT has been changed from its
459 	 * initial value. beta is bounded to ensure it is always between
460 	 * HTCP_MINBETA and HTCP_MAXBETA.
461 	 */
462 	if (V_htcp_adaptive_backoff && htcp_data->minrtt != TCPTV_SRTTBASE &&
463 	    htcp_data->maxrtt != TCPTV_SRTTBASE)
464 		htcp_data->beta = min(max(HTCP_MINBETA,
465 		    (htcp_data->minrtt << HTCP_SHIFT) / htcp_data->maxrtt),
466 		    HTCP_MAXBETA);
467 	else
468 		htcp_data->beta = HTCP_MINBETA;
469 }
470 
471 /*
472  * Record the minimum and maximum RTT seen for the connection. These are used in
473  * the calculation of beta if adaptive backoff is enabled.
474  */
475 static void
476 htcp_record_rtt(struct cc_var *ccv)
477 {
478 	struct htcp *htcp_data;
479 
480 	htcp_data = ccv->cc_data;
481 
482 	/* XXXLAS: Should there be some hysteresis for minrtt? */
483 
484 	/*
485 	 * Record the current SRTT as our minrtt if it's the smallest we've seen
486 	 * or minrtt is currently equal to its initialised value. Ignore SRTT
487 	 * until a min number of samples have been taken.
488 	 */
489 	if ((CCV(ccv, t_srtt) < htcp_data->minrtt ||
490 	    htcp_data->minrtt == TCPTV_SRTTBASE) &&
491 	    (CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES))
492 		htcp_data->minrtt = CCV(ccv, t_srtt);
493 
494 	/*
495 	 * Record the current SRTT as our maxrtt if it's the largest we've
496 	 * seen. Ignore SRTT until a min number of samples have been taken.
497 	 */
498 	if (CCV(ccv, t_srtt) > htcp_data->maxrtt
499 	    && CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES)
500 		htcp_data->maxrtt = CCV(ccv, t_srtt);
501 }
502 
503 /*
504  * Update the ssthresh in the event of congestion.
505  */
506 static void
507 htcp_ssthresh_update(struct cc_var *ccv)
508 {
509 	struct htcp *htcp_data;
510 
511 	htcp_data = ccv->cc_data;
512 
513 	/*
514 	 * On the first congestion event, set ssthresh to cwnd * 0.5, on
515 	 * subsequent congestion events, set it to cwnd * beta.
516 	 */
517 	if (CCV(ccv, snd_ssthresh) == TCP_MAXWIN << TCP_MAX_WINSHIFT)
518 		CCV(ccv, snd_ssthresh) = ((u_long)CCV(ccv, snd_cwnd) *
519 		    HTCP_MINBETA) >> HTCP_SHIFT;
520 	else {
521 		htcp_recalc_beta(ccv);
522 		CCV(ccv, snd_ssthresh) = ((u_long)CCV(ccv, snd_cwnd) *
523 		    htcp_data->beta) >> HTCP_SHIFT;
524 	}
525 }
526 
527 SYSCTL_DECL(_net_inet_tcp_cc_htcp);
528 SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, htcp, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
529     "H-TCP related settings");
530 SYSCTL_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, adaptive_backoff,
531     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(htcp_adaptive_backoff), 0,
532     "enable H-TCP adaptive backoff");
533 SYSCTL_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, rtt_scaling,
534     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(htcp_rtt_scaling), 0,
535     "enable H-TCP RTT scaling");
536 
537 DECLARE_CC_MODULE(htcp, &htcp_cc_algo);
538 MODULE_VERSION(htcp, 1);
539