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