xref: /freebsd/sys/netinet/cc/cc_chd.c (revision a91a246563dffa876a52f53a98de4af9fa364c52)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009-2010
5  *	Swinburne University of Technology, Melbourne, Australia
6  * Copyright (c) 2010-2011 The FreeBSD Foundation
7  * All rights reserved.
8  *
9  * This software was developed at the Centre for Advanced Internet
10  * Architectures, Swinburne University of Technology, by David Hayes and
11  * Lawrence Stewart, made possible in part by a grant from the Cisco University
12  * Research Program Fund at Community Foundation Silicon Valley.
13  *
14  * Portions of this software were developed at the Centre for Advanced Internet
15  * Architectures, Swinburne University of Technology, Melbourne, Australia by
16  * David Hayes under sponsorship from the FreeBSD Foundation.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 /*
41  * An implementation of the CAIA-Hamilton delay based congestion control
42  * algorithm, based on "Improved coexistence and loss tolerance for delay based
43  * TCP congestion control" by D. A. Hayes and G. Armitage., in 35th Annual IEEE
44  * Conference on Local Computer Networks (LCN 2010), Denver, Colorado, USA,
45  * 11-14 October 2010.
46  *
47  * Originally released as part of the NewTCP research project at Swinburne
48  * University of Technology's Centre for Advanced Internet Architectures,
49  * Melbourne, Australia, which was made possible in part by a grant from the
50  * Cisco University Research Program Fund at Community Foundation Silicon
51  * Valley. More details are available at:
52  *   http://caia.swin.edu.au/urp/newtcp/
53  */
54 
55 #include <sys/param.h>
56 #include <sys/kernel.h>
57 #include <sys/khelp.h>
58 #include <sys/limits.h>
59 #include <sys/malloc.h>
60 #include <sys/module.h>
61 #include <sys/queue.h>
62 #include <sys/socket.h>
63 #include <sys/socketvar.h>
64 #include <sys/sysctl.h>
65 #include <sys/systm.h>
66 
67 #include <net/vnet.h>
68 
69 #include <net/route.h>
70 #include <net/route/nhop.h>
71 
72 #include <netinet/in_pcb.h>
73 #include <netinet/tcp.h>
74 #include <netinet/tcp_seq.h>
75 #include <netinet/tcp_timer.h>
76 #include <netinet/tcp_var.h>
77 #include <netinet/cc/cc.h>
78 #include <netinet/cc/cc_module.h>
79 
80 #include <netinet/khelp/h_ertt.h>
81 
82 /*
83  * Private signal type for rate based congestion signal.
84  * See <netinet/cc.h> for appropriate bit-range to use for private signals.
85  */
86 #define	CC_CHD_DELAY	0x02000000
87 
88 /* Largest possible number returned by random(). */
89 #define	RANDOM_MAX	INT_MAX
90 
91 static void	chd_ack_received(struct cc_var *ccv, ccsignal_t ack_type);
92 static void	chd_cb_destroy(struct cc_var *ccv);
93 static int	chd_cb_init(struct cc_var *ccv, void *ptr);
94 static void	chd_cong_signal(struct cc_var *ccv, ccsignal_t signal_type);
95 static void	chd_conn_init(struct cc_var *ccv);
96 static int	chd_mod_init(void);
97 static size_t	chd_data_sz(void);
98 
99 struct chd {
100 	/*
101 	 * Shadow window - keeps track of what the NewReno congestion window
102 	 * would have been if delay-based cwnd backoffs had not been made. This
103 	 * functionality aids coexistence with loss-based TCP flows which may be
104 	 * sharing links along the path.
105 	 */
106 	unsigned long shadow_w;
107 	/*
108 	 * Loss-based TCP compatibility flag - When set, it turns on the shadow
109 	 * window functionality.
110 	 */
111 	int loss_compete;
112 	 /* The maximum round trip time seen within a measured rtt period. */
113 	int maxrtt_in_rtt;
114 	/* The previous qdly that caused cwnd to backoff. */
115 	int prev_backoff_qdly;
116 };
117 
118 static int ertt_id;
119 
120 VNET_DEFINE_STATIC(uint32_t, chd_qmin) = 5;
121 VNET_DEFINE_STATIC(uint32_t, chd_pmax) = 50;
122 VNET_DEFINE_STATIC(uint32_t, chd_loss_fair) = 1;
123 VNET_DEFINE_STATIC(uint32_t, chd_use_max) = 1;
124 VNET_DEFINE_STATIC(uint32_t, chd_qthresh) = 20;
125 #define	V_chd_qthresh	VNET(chd_qthresh)
126 #define	V_chd_qmin	VNET(chd_qmin)
127 #define	V_chd_pmax	VNET(chd_pmax)
128 #define	V_chd_loss_fair	VNET(chd_loss_fair)
129 #define	V_chd_use_max	VNET(chd_use_max)
130 
131 
132 struct cc_algo chd_cc_algo = {
133 	.name = "chd",
134 	.ack_received = chd_ack_received,
135 	.cb_destroy = chd_cb_destroy,
136 	.cb_init = chd_cb_init,
137 	.cong_signal = chd_cong_signal,
138 	.conn_init = chd_conn_init,
139 	.mod_init = chd_mod_init,
140 	.cc_data_sz = chd_data_sz,
141 	.after_idle = newreno_cc_after_idle,
142 	.post_recovery = newreno_cc_post_recovery,
143 };
144 
145 static __inline void
146 chd_window_decrease(struct cc_var *ccv)
147 {
148 	unsigned long win;
149 
150 	win = min(CCV(ccv, snd_wnd), CCV(ccv, snd_cwnd)) / CCV(ccv, t_maxseg);
151 	win -= max((win / 2), 1);
152 	CCV(ccv, snd_ssthresh) = max(win, 2) * CCV(ccv, t_maxseg);
153 }
154 
155 /*
156  * Probabilistic backoff function. Returns 1 if we should backoff or 0
157  * otherwise. The calculation of p is similar to the calculation of p in cc_hd.
158  */
159 static __inline int
160 should_backoff(int qdly, int maxqdly, struct chd *chd_data)
161 {
162 	unsigned long p, rand;
163 
164 	rand = random();
165 
166 	if (qdly < V_chd_qthresh) {
167 		chd_data->loss_compete = 0;
168 		p = (((RANDOM_MAX / 100) * V_chd_pmax) /
169 		    (V_chd_qthresh - V_chd_qmin)) *
170 		    (qdly - V_chd_qmin);
171 	} else {
172 		if (qdly > V_chd_qthresh) {
173 			p = (((RANDOM_MAX / 100) * V_chd_pmax) /
174 			    (maxqdly - V_chd_qthresh)) *
175 			    (maxqdly - qdly);
176 			if (V_chd_loss_fair && rand < p)
177 				chd_data->loss_compete = 1;
178 		} else {
179 			p = (RANDOM_MAX / 100) * V_chd_pmax;
180 			chd_data->loss_compete = 0;
181 		}
182 	}
183 
184 	return (rand < p);
185 }
186 
187 static __inline void
188 chd_window_increase(struct cc_var *ccv, int new_measurement)
189 {
190 	struct chd *chd_data;
191 	int incr;
192 
193 	chd_data = ccv->cc_data;
194 	incr = 0;
195 
196 	if (CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh)) {
197 		/* Adapted from NewReno slow start. */
198 		if (V_tcp_do_rfc3465) {
199 			/* In slow-start with ABC enabled. */
200 			if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max)) {
201 				/* Not due to RTO. */
202 				incr = min(ccv->bytes_this_ack,
203 				    V_tcp_abc_l_var * CCV(ccv, t_maxseg));
204 			} else {
205 				/* Due to RTO. */
206 				incr = min(ccv->bytes_this_ack,
207 				    CCV(ccv, t_maxseg));
208 			}
209 		} else
210 			incr = CCV(ccv, t_maxseg);
211 
212 	} else { /* Congestion avoidance. */
213 		if (V_tcp_do_rfc3465) {
214 			if (ccv->flags & CCF_ABC_SENTAWND) {
215 				ccv->flags &= ~CCF_ABC_SENTAWND;
216 				incr = CCV(ccv, t_maxseg);
217 			}
218 		} else if (new_measurement)
219 			incr = CCV(ccv, t_maxseg);
220 	}
221 
222 	if (chd_data->shadow_w > 0) {
223 		/* Track NewReno window. */
224 		chd_data->shadow_w = min(chd_data->shadow_w + incr,
225 		    TCP_MAXWIN << CCV(ccv, snd_scale));
226 	}
227 
228 	CCV(ccv,snd_cwnd) = min(CCV(ccv, snd_cwnd) + incr,
229 	    TCP_MAXWIN << CCV(ccv, snd_scale));
230 }
231 
232 /*
233  * All ACK signals are used for timing measurements to determine delay-based
234  * congestion. However, window increases are only performed when
235  * ack_type == CC_ACK.
236  */
237 static void
238 chd_ack_received(struct cc_var *ccv, ccsignal_t ack_type)
239 {
240 	struct chd *chd_data;
241 	struct ertt *e_t;
242 	int backoff, new_measurement, qdly, rtt;
243 
244 	e_t = khelp_get_osd(&CCV(ccv, t_osd), ertt_id);
245 	chd_data = ccv->cc_data;
246 	new_measurement = e_t->flags & ERTT_NEW_MEASUREMENT;
247 	backoff = qdly = 0;
248 
249 	chd_data->maxrtt_in_rtt = imax(e_t->rtt, chd_data->maxrtt_in_rtt);
250 
251 	if (new_measurement) {
252 		/*
253 		 * There is a new per RTT measurement, so check to see if there
254 		 * is delay based congestion.
255 		 */
256 		rtt = V_chd_use_max ? chd_data->maxrtt_in_rtt : e_t->rtt;
257 		chd_data->maxrtt_in_rtt = 0;
258 
259 		if (rtt && e_t->minrtt && !IN_RECOVERY(CCV(ccv, t_flags))) {
260 			qdly = rtt - e_t->minrtt;
261 			if (qdly > V_chd_qmin) {
262 				/*
263 				 * Probabilistic delay based congestion
264 				 * indication.
265 				 */
266 				backoff = should_backoff(qdly,
267 				    e_t->maxrtt - e_t->minrtt, chd_data);
268 			} else
269 				chd_data->loss_compete = 0;
270 		}
271 		/* Reset per RTT measurement flag to start a new measurement. */
272 		e_t->flags &= ~ERTT_NEW_MEASUREMENT;
273 	}
274 
275 	if (backoff) {
276 		/*
277 		 * Update shadow_w before delay based backoff.
278 		 */
279 		if (chd_data->loss_compete ||
280 		    qdly > chd_data->prev_backoff_qdly) {
281 			/*
282 			 * Delay is higher than when we backed off previously,
283 			 * so it is possible that this flow is competing with
284 			 * loss based flows.
285 			 */
286 			chd_data->shadow_w = max(CCV(ccv, snd_cwnd),
287 			    chd_data->shadow_w);
288 		} else {
289 			/*
290 			 * Reset shadow_w, as it is probable that this flow is
291 			 * not competing with loss based flows at the moment.
292 			 */
293 			chd_data->shadow_w = 0;
294 		}
295 
296 		chd_data->prev_backoff_qdly = qdly;
297 		/*
298 		 * Send delay-based congestion signal to the congestion signal
299 		 * handler.
300 		 */
301 		chd_cong_signal(ccv, CC_CHD_DELAY);
302 
303 	} else if (ack_type == CC_ACK)
304 		chd_window_increase(ccv, new_measurement);
305 }
306 
307 static void
308 chd_cb_destroy(struct cc_var *ccv)
309 {
310 	free(ccv->cc_data, M_CC_MEM);
311 }
312 
313 size_t
314 chd_data_sz(void)
315 {
316 	return (sizeof(struct chd));
317 }
318 
319 static int
320 chd_cb_init(struct cc_var *ccv, void *ptr)
321 {
322 	struct chd *chd_data;
323 
324 	INP_WLOCK_ASSERT(tptoinpcb(ccv->ccvc.tcp));
325 	if (ptr == NULL) {
326 		chd_data = malloc(sizeof(struct chd), M_CC_MEM, M_NOWAIT);
327 		if (chd_data == NULL)
328 			return (ENOMEM);
329 	} else
330 		chd_data = ptr;
331 
332 	chd_data->shadow_w = 0;
333 	ccv->cc_data = chd_data;
334 
335 	return (0);
336 }
337 
338 static void
339 chd_cong_signal(struct cc_var *ccv, ccsignal_t signal_type)
340 {
341 	struct ertt *e_t;
342 	struct chd *chd_data;
343 	int qdly;
344 
345 	e_t = khelp_get_osd(&CCV(ccv, t_osd), ertt_id);
346 	chd_data = ccv->cc_data;
347 	qdly = imax(e_t->rtt, chd_data->maxrtt_in_rtt) - e_t->minrtt;
348 
349 	switch((int)signal_type) {
350 	case CC_CHD_DELAY:
351 		chd_window_decrease(ccv); /* Set new ssthresh. */
352 		CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
353 		CCV(ccv, snd_recover) = CCV(ccv, snd_max);
354 		ENTER_CONGRECOVERY(CCV(ccv, t_flags));
355 		break;
356 
357 	case CC_NDUPACK: /* Packet loss. */
358 		/*
359 		 * Only react to loss as a congestion signal if qdly >
360 		 * V_chd_qthresh.  If qdly is less than qthresh, presume that
361 		 * this is a non congestion related loss. If qdly is greater
362 		 * than qthresh, assume that we are competing with loss based
363 		 * tcp flows and restore window from any unnecessary backoffs,
364 		 * before the decrease.
365 		 */
366 		if (!IN_RECOVERY(CCV(ccv, t_flags)) && qdly > V_chd_qthresh) {
367 			if (chd_data->loss_compete) {
368 				CCV(ccv, snd_cwnd) = max(CCV(ccv, snd_cwnd),
369 				    chd_data->shadow_w);
370 			}
371 			chd_window_decrease(ccv);
372 		} else {
373 			 /*
374 			  * This loss isn't congestion related, or already
375 			  * recovering from congestion.
376 			  */
377 			CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
378 			CCV(ccv, snd_recover) = CCV(ccv, snd_max);
379 		}
380 
381 		if (chd_data->shadow_w > 0) {
382 			chd_data->shadow_w = max(chd_data->shadow_w /
383 			    CCV(ccv, t_maxseg) / 2, 2) * CCV(ccv, t_maxseg);
384 		}
385 		ENTER_FASTRECOVERY(CCV(ccv, t_flags));
386 		break;
387 
388 	default:
389 		newreno_cc_cong_signal(ccv, signal_type);
390 		break;
391 	}
392 }
393 
394 static void
395 chd_conn_init(struct cc_var *ccv)
396 {
397 	struct chd *chd_data;
398 
399 	chd_data = ccv->cc_data;
400 	chd_data->prev_backoff_qdly = 0;
401 	chd_data->maxrtt_in_rtt = 0;
402 	chd_data->loss_compete = 0;
403 	/*
404 	 * Initialise the shadow_cwnd to be equal to snd_cwnd in case we are
405 	 * competing with loss based flows from the start.
406 	 */
407 	chd_data->shadow_w = CCV(ccv, snd_cwnd);
408 }
409 
410 static int
411 chd_mod_init(void)
412 {
413 
414 	ertt_id = khelp_get_id("ertt");
415 	if (ertt_id <= 0) {
416 		printf("%s: h_ertt module not found\n", __func__);
417 		return (ENOENT);
418 	}
419 	return (0);
420 }
421 
422 static int
423 chd_loss_fair_handler(SYSCTL_HANDLER_ARGS)
424 {
425 	int error;
426 	uint32_t new;
427 
428 	new = V_chd_loss_fair;
429 	error = sysctl_handle_int(oidp, &new, 0, req);
430 	if (error == 0 && req->newptr != NULL) {
431 		if (new > 1)
432 			error = EINVAL;
433 		else
434 			V_chd_loss_fair = new;
435 	}
436 
437 	return (error);
438 }
439 
440 static int
441 chd_pmax_handler(SYSCTL_HANDLER_ARGS)
442 {
443 	int error;
444 	uint32_t new;
445 
446 	new = V_chd_pmax;
447 	error = sysctl_handle_int(oidp, &new, 0, req);
448 	if (error == 0 && req->newptr != NULL) {
449 		if (new == 0 || new > 100)
450 			error = EINVAL;
451 		else
452 			V_chd_pmax = new;
453 	}
454 
455 	return (error);
456 }
457 
458 static int
459 chd_qthresh_handler(SYSCTL_HANDLER_ARGS)
460 {
461 	int error;
462 	uint32_t new;
463 
464 	new = V_chd_qthresh;
465 	error = sysctl_handle_int(oidp, &new, 0, req);
466 	if (error == 0 && req->newptr != NULL) {
467 		if (new <= V_chd_qmin)
468 			error = EINVAL;
469 		else
470 			V_chd_qthresh = new;
471 	}
472 
473 	return (error);
474 }
475 
476 SYSCTL_DECL(_net_inet_tcp_cc_chd);
477 SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, chd, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
478     "CAIA Hamilton delay-based congestion control related settings");
479 
480 SYSCTL_PROC(_net_inet_tcp_cc_chd, OID_AUTO, loss_fair,
481     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
482     &VNET_NAME(chd_loss_fair), 1, &chd_loss_fair_handler,
483     "IU", "Flag to enable shadow window functionality.");
484 
485 SYSCTL_PROC(_net_inet_tcp_cc_chd, OID_AUTO, pmax,
486     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
487     &VNET_NAME(chd_pmax), 5, &chd_pmax_handler,
488     "IU", "Per RTT maximum backoff probability as a percentage");
489 
490 SYSCTL_PROC(_net_inet_tcp_cc_chd, OID_AUTO, queue_threshold,
491     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
492     &VNET_NAME(chd_qthresh), 20, &chd_qthresh_handler,
493     "IU", "Queueing congestion threshold in ticks");
494 
495 SYSCTL_UINT(_net_inet_tcp_cc_chd, OID_AUTO, queue_min,
496     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(chd_qmin), 5,
497     "Minimum queueing delay threshold in ticks");
498 
499 SYSCTL_UINT(_net_inet_tcp_cc_chd,  OID_AUTO, use_max,
500     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(chd_use_max), 1,
501     "Use the maximum RTT seen within the measurement period (RTT) "
502     "as the basic delay measurement for the algorithm.");
503 
504 DECLARE_CC_MODULE(chd, &chd_cc_algo);
505 MODULE_VERSION(chd, 2);
506 MODULE_DEPEND(chd, ertt, 1, 1, 1);
507