xref: /freebsd/sys/netinet/cc/cc_hd.c (revision 190cef3d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009-2010
5  *	Swinburne University of Technology, Melbourne, Australia
6  * Copyright (c) 2010 Lawrence Stewart <lstewart@freebsd.org>
7  * Copyright (c) 2010-2011 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 David Hayes and
12  * Lawrence Stewart, 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 Internet
16  * Architectures, Swinburne University of Technology, Melbourne, Australia by
17  * 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 Hamilton Institute's delay-based congestion control
43  * algorithm for FreeBSD, based on "A strategy for fair coexistence of loss and
44  * delay-based congestion control algorithms," by L. Budzisz, R. Stanojevic, R.
45  * Shorten, and F. Baker, IEEE Commun. Lett., vol. 13, no. 7, pp. 555--557, Jul.
46  * 2009.
47  *
48  * Originally released as part of the NewTCP research project at Swinburne
49  * University of Technology's Centre for Advanced Internet Architectures,
50  * Melbourne, Australia, which was made possible in part by a grant from the
51  * Cisco University Research Program Fund at Community Foundation Silicon
52  * Valley. More details are available at:
53  *   http://caia.swin.edu.au/urp/newtcp/
54  */
55 
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
58 
59 #include <sys/param.h>
60 #include <sys/kernel.h>
61 #include <sys/khelp.h>
62 #include <sys/limits.h>
63 #include <sys/malloc.h>
64 #include <sys/module.h>
65 #include <sys/queue.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/sysctl.h>
69 #include <sys/systm.h>
70 
71 #include <net/vnet.h>
72 
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 #define	CAST_PTR_INT(X)	(*((int*)(X)))
83 
84 /* Largest possible number returned by random(). */
85 #define	RANDOM_MAX	INT_MAX
86 
87 static void	hd_ack_received(struct cc_var *ccv, uint16_t ack_type);
88 static int	hd_mod_init(void);
89 
90 static int ertt_id;
91 
92 VNET_DEFINE_STATIC(uint32_t, hd_qthresh) = 20;
93 VNET_DEFINE_STATIC(uint32_t, hd_qmin) = 5;
94 VNET_DEFINE_STATIC(uint32_t, hd_pmax) = 5;
95 #define	V_hd_qthresh	VNET(hd_qthresh)
96 #define	V_hd_qmin	VNET(hd_qmin)
97 #define	V_hd_pmax	VNET(hd_pmax)
98 
99 struct cc_algo hd_cc_algo = {
100 	.name = "hd",
101 	.ack_received = hd_ack_received,
102 	.mod_init = hd_mod_init
103 };
104 
105 /*
106  * Hamilton backoff function. Returns 1 if we should backoff or 0 otherwise.
107  */
108 static __inline int
109 should_backoff(int qdly, int maxqdly)
110 {
111 	unsigned long p;
112 
113 	if (qdly < V_hd_qthresh) {
114 		p = (((RANDOM_MAX / 100) * V_hd_pmax) /
115 		    (V_hd_qthresh - V_hd_qmin)) * (qdly - V_hd_qmin);
116 	} else {
117 		if (qdly > V_hd_qthresh)
118 			p = (((RANDOM_MAX / 100) * V_hd_pmax) /
119 			    (maxqdly - V_hd_qthresh)) * (maxqdly - qdly);
120 		else
121 			p = (RANDOM_MAX / 100) * V_hd_pmax;
122 	}
123 
124 	return (random() < p);
125 }
126 
127 /*
128  * If the ack type is CC_ACK, and the inferred queueing delay is greater than
129  * the Qmin threshold, cwnd is reduced probabilistically. When backing off due
130  * to delay, HD behaves like NewReno when an ECN signal is received. HD behaves
131  * as NewReno in all other circumstances.
132  */
133 static void
134 hd_ack_received(struct cc_var *ccv, uint16_t ack_type)
135 {
136 	struct ertt *e_t;
137 	int qdly;
138 
139 	if (ack_type == CC_ACK) {
140 		e_t = khelp_get_osd(CCV(ccv, osd), ertt_id);
141 
142 		if (e_t->rtt && e_t->minrtt && V_hd_qthresh > 0) {
143 			qdly = e_t->rtt - e_t->minrtt;
144 
145 			if (qdly > V_hd_qmin &&
146 			    !IN_RECOVERY(CCV(ccv, t_flags))) {
147 				/* Probabilistic backoff of cwnd. */
148 				if (should_backoff(qdly,
149 				    e_t->maxrtt - e_t->minrtt)) {
150 					/*
151 					 * Update cwnd and ssthresh update to
152 					 * half cwnd and behave like an ECN (ie
153 					 * not a packet loss).
154 					 */
155 					newreno_cc_algo.cong_signal(ccv,
156 					    CC_ECN);
157 					return;
158 				}
159 			}
160 		}
161 	}
162 	newreno_cc_algo.ack_received(ccv, ack_type); /* As for NewReno. */
163 }
164 
165 static int
166 hd_mod_init(void)
167 {
168 
169 	ertt_id = khelp_get_id("ertt");
170 	if (ertt_id <= 0) {
171 		printf("%s: h_ertt module not found\n", __func__);
172 		return (ENOENT);
173 	}
174 
175 	hd_cc_algo.after_idle = newreno_cc_algo.after_idle;
176 	hd_cc_algo.cong_signal = newreno_cc_algo.cong_signal;
177 	hd_cc_algo.post_recovery = newreno_cc_algo.post_recovery;
178 
179 	return (0);
180 }
181 
182 static int
183 hd_pmax_handler(SYSCTL_HANDLER_ARGS)
184 {
185 	int error;
186 	uint32_t new;
187 
188 	new = V_hd_pmax;
189 	error = sysctl_handle_int(oidp, &new, 0, req);
190 	if (error == 0 && req->newptr != NULL) {
191 		if (CAST_PTR_INT(req->newptr) == 0 ||
192 		    CAST_PTR_INT(req->newptr) > 100)
193 			error = EINVAL;
194 		else
195 			V_hd_pmax = new;
196 	}
197 
198 	return (error);
199 }
200 
201 static int
202 hd_qmin_handler(SYSCTL_HANDLER_ARGS)
203 {
204 	int error;
205 	uint32_t new;
206 
207 	new = V_hd_qmin;
208 	error = sysctl_handle_int(oidp, &new, 0, req);
209 	if (error == 0 && req->newptr != NULL) {
210 		if (CAST_PTR_INT(req->newptr) > V_hd_qthresh)
211 			error = EINVAL;
212 		else
213 			V_hd_qmin = new;
214 	}
215 
216 	return (error);
217 }
218 
219 static int
220 hd_qthresh_handler(SYSCTL_HANDLER_ARGS)
221 {
222 	int error;
223 	uint32_t new;
224 
225 	new = V_hd_qthresh;
226 	error = sysctl_handle_int(oidp, &new, 0, req);
227 	if (error == 0 && req->newptr != NULL) {
228 		if (CAST_PTR_INT(req->newptr) < 1 ||
229 		    CAST_PTR_INT(req->newptr) < V_hd_qmin)
230 			error = EINVAL;
231 		else
232 			V_hd_qthresh = new;
233 	}
234 
235 	return (error);
236 }
237 
238 SYSCTL_DECL(_net_inet_tcp_cc_hd);
239 SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, hd, CTLFLAG_RW, NULL,
240     "Hamilton delay-based congestion control related settings");
241 
242 SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, queue_threshold,
243     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, &VNET_NAME(hd_qthresh), 20,
244     &hd_qthresh_handler, "IU", "queueing congestion threshold (qth) in ticks");
245 
246 SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, pmax,
247     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, &VNET_NAME(hd_pmax), 5,
248     &hd_pmax_handler, "IU",
249     "per packet maximum backoff probability as a percentage");
250 
251 SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, queue_min,
252     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, &VNET_NAME(hd_qmin), 5,
253     &hd_qmin_handler, "IU", "minimum queueing delay threshold (qmin) in ticks");
254 
255 DECLARE_CC_MODULE(hd, &hd_cc_algo);
256 MODULE_DEPEND(hd, ertt, 1, 1, 1);
257