xref: /freebsd/sys/netinet/cc/cc_vegas.c (revision 22dcc81293854c4d39df639a329fecded175b2b0)
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 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 Vegas congestion control algorithm for FreeBSD,
43  * based on L. S. Brakmo and L. L. Peterson, "TCP Vegas: end to end congestion
44  * avoidance on a global internet", IEEE J. Sel. Areas Commun., vol. 13, no. 8,
45  * pp. 1465-1480, Oct. 1995. The original Vegas duplicate ack policy has not
46  * been implemented, since clock ticks are not as coarse as they were (i.e.
47  * 500ms) when Vegas was designed. Also, packets are timed once per RTT as in
48  * the original paper.
49  *
50  * Originally released as part of the NewTCP research project at Swinburne
51  * University of Technology's Centre for Advanced Internet Architectures,
52  * Melbourne, Australia, which was made possible in part by a grant from the
53  * Cisco University Research Program Fund at Community Foundation Silicon
54  * Valley. More details are available at:
55  *   http://caia.swin.edu.au/urp/newtcp/
56  */
57 
58 #include <sys/param.h>
59 #include <sys/kernel.h>
60 #include <sys/khelp.h>
61 #include <sys/malloc.h>
62 #include <sys/module.h>
63 #include <sys/queue.h>
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
66 #include <sys/sysctl.h>
67 #include <sys/systm.h>
68 
69 #include <net/vnet.h>
70 
71 #include <net/route.h>
72 #include <net/route/nhop.h>
73 
74 #include <netinet/in_pcb.h>
75 #include <netinet/tcp.h>
76 #include <netinet/tcp_timer.h>
77 #include <netinet/tcp_var.h>
78 #include <netinet/cc/cc.h>
79 #include <netinet/cc/cc_module.h>
80 
81 #include <netinet/khelp/h_ertt.h>
82 
83 /*
84  * Private signal type for rate based congestion signal.
85  * See <netinet/cc.h> for appropriate bit-range to use for private signals.
86  */
87 #define	CC_VEGAS_RATE	0x04000000
88 
89 static void	vegas_ack_received(struct cc_var *ccv, ccsignal_t ack_type);
90 static void	vegas_cb_destroy(struct cc_var *ccv);
91 static int	vegas_cb_init(struct cc_var *ccv, void *ptr);
92 static void	vegas_cong_signal(struct cc_var *ccv, ccsignal_t signal_type);
93 static void	vegas_conn_init(struct cc_var *ccv);
94 static int	vegas_mod_init(void);
95 static size_t	vegas_data_sz(void);
96 
97 struct vegas {
98 	int slow_start_toggle;
99 };
100 
101 static int32_t ertt_id;
102 
103 VNET_DEFINE_STATIC(uint32_t, vegas_alpha) = 1;
104 VNET_DEFINE_STATIC(uint32_t, vegas_beta) = 3;
105 #define	V_vegas_alpha	VNET(vegas_alpha)
106 #define	V_vegas_beta	VNET(vegas_beta)
107 
108 struct cc_algo vegas_cc_algo = {
109 	.name = "vegas",
110 	.ack_received = vegas_ack_received,
111 	.cb_destroy = vegas_cb_destroy,
112 	.cb_init = vegas_cb_init,
113 	.cong_signal = vegas_cong_signal,
114 	.conn_init = vegas_conn_init,
115 	.mod_init = vegas_mod_init,
116 	.cc_data_sz = vegas_data_sz,
117 	.after_idle = newreno_cc_after_idle,
118 	.post_recovery = newreno_cc_post_recovery,
119 };
120 
121 /*
122  * The vegas window adjustment is done once every RTT, as indicated by the
123  * ERTT_NEW_MEASUREMENT flag. This flag is reset once the new measurement data
124  * has been used.
125  */
126 static void
vegas_ack_received(struct cc_var * ccv,ccsignal_t ack_type)127 vegas_ack_received(struct cc_var *ccv, ccsignal_t ack_type)
128 {
129 	struct ertt *e_t;
130 	struct vegas *vegas_data;
131 	long actual_tx_rate, expected_tx_rate, ndiff;
132 	uint32_t mss = tcp_fixed_maxseg(ccv->tp);
133 
134 	e_t = khelp_get_osd(&CCV(ccv, t_osd), ertt_id);
135 	vegas_data = ccv->cc_data;
136 
137 	if (e_t->flags & ERTT_NEW_MEASUREMENT) { /* Once per RTT. */
138 		if (e_t->minrtt && e_t->markedpkt_rtt) {
139 			expected_tx_rate = e_t->marked_snd_cwnd / e_t->minrtt;
140 			actual_tx_rate = e_t->bytes_tx_in_marked_rtt /
141 			    e_t->markedpkt_rtt;
142 			ndiff = (expected_tx_rate - actual_tx_rate) *
143 			    e_t->minrtt / mss;
144 
145 			if (ndiff < V_vegas_alpha) {
146 				if (CCV(ccv, snd_cwnd) <=
147 				    CCV(ccv, snd_ssthresh)) {
148 					vegas_data->slow_start_toggle =
149 					    vegas_data->slow_start_toggle ?
150 					    0 : 1;
151 				} else {
152 					vegas_data->slow_start_toggle = 0;
153 					CCV(ccv, snd_cwnd) =
154 					    min(CCV(ccv, snd_cwnd) + mss,
155 					    TCP_MAXWIN << CCV(ccv, snd_scale));
156 				}
157 			} else if (ndiff > V_vegas_beta) {
158 				/* Rate-based congestion. */
159 				vegas_cong_signal(ccv, CC_VEGAS_RATE);
160 				vegas_data->slow_start_toggle = 0;
161 			}
162 		}
163 		e_t->flags &= ~ERTT_NEW_MEASUREMENT;
164 	}
165 
166 	if (vegas_data->slow_start_toggle)
167 		newreno_cc_ack_received(ccv, ack_type);
168 }
169 
170 static void
vegas_cb_destroy(struct cc_var * ccv)171 vegas_cb_destroy(struct cc_var *ccv)
172 {
173 	free(ccv->cc_data, M_CC_MEM);
174 }
175 
176 static size_t
vegas_data_sz(void)177 vegas_data_sz(void)
178 {
179 	return (sizeof(struct vegas));
180 }
181 
182 static int
vegas_cb_init(struct cc_var * ccv,void * ptr)183 vegas_cb_init(struct cc_var *ccv, void *ptr)
184 {
185 	struct vegas *vegas_data;
186 
187 	INP_WLOCK_ASSERT(tptoinpcb(ccv->tp));
188 	if (ptr == NULL) {
189 		vegas_data = malloc(sizeof(struct vegas), M_CC_MEM, M_NOWAIT);
190 		if (vegas_data == NULL)
191 			return (ENOMEM);
192 	} else
193 		vegas_data = ptr;
194 
195 	vegas_data->slow_start_toggle = 1;
196 	ccv->cc_data = vegas_data;
197 
198 	return (0);
199 }
200 
201 /*
202  * If congestion has been triggered triggered by the Vegas measured rates, it is
203  * handled here, otherwise it falls back to newreno's congestion handling.
204  */
205 static void
vegas_cong_signal(struct cc_var * ccv,ccsignal_t signal_type)206 vegas_cong_signal(struct cc_var *ccv, ccsignal_t signal_type)
207 {
208 	struct vegas *vegas_data;
209 	int presignalrecov;
210 	uint32_t mss = tcp_fixed_maxseg(ccv->tp);
211 
212 	vegas_data = ccv->cc_data;
213 
214 	if (IN_RECOVERY(CCV(ccv, t_flags)))
215 		presignalrecov = 1;
216 	else
217 		presignalrecov = 0;
218 
219 	switch((int)signal_type) {
220 	case CC_VEGAS_RATE:
221 		if (!IN_RECOVERY(CCV(ccv, t_flags))) {
222 			CCV(ccv, snd_cwnd) = max(2 * mss,
223 			    CCV(ccv, snd_cwnd) - mss);
224 			if (CCV(ccv, snd_cwnd) < CCV(ccv, snd_ssthresh))
225 				/* Exit slow start. */
226 				CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
227 		}
228 		break;
229 
230 	default:
231 		newreno_cc_cong_signal(ccv, signal_type);
232 		break;
233 	}
234 
235 	if (IN_RECOVERY(CCV(ccv, t_flags)) && !presignalrecov)
236 		vegas_data->slow_start_toggle =
237 		    (CCV(ccv, snd_cwnd) < CCV(ccv, snd_ssthresh)) ? 1 : 0;
238 }
239 
240 static void
vegas_conn_init(struct cc_var * ccv)241 vegas_conn_init(struct cc_var *ccv)
242 {
243 	struct vegas *vegas_data;
244 
245 	vegas_data = ccv->cc_data;
246 	vegas_data->slow_start_toggle = 1;
247 }
248 
249 static int
vegas_mod_init(void)250 vegas_mod_init(void)
251 {
252 	ertt_id = khelp_get_id("ertt");
253 	if (ertt_id <= 0) {
254 		printf("%s: h_ertt module not found\n", __func__);
255 		return (ENOENT);
256 	}
257 	return (0);
258 }
259 
260 static int
vegas_alpha_handler(SYSCTL_HANDLER_ARGS)261 vegas_alpha_handler(SYSCTL_HANDLER_ARGS)
262 {
263 	int error;
264 	uint32_t new;
265 
266 	new = V_vegas_alpha;
267 	error = sysctl_handle_int(oidp, &new, 0, req);
268 	if (error == 0 && req->newptr != NULL) {
269 		if (new == 0 || new > V_vegas_beta)
270 			error = EINVAL;
271 		else
272 			V_vegas_alpha = new;
273 	}
274 
275 	return (error);
276 }
277 
278 static int
vegas_beta_handler(SYSCTL_HANDLER_ARGS)279 vegas_beta_handler(SYSCTL_HANDLER_ARGS)
280 {
281 	int error;
282 	uint32_t new;
283 
284 	new = V_vegas_beta;
285 	error = sysctl_handle_int(oidp, &new, 0, req);
286 	if (error == 0 && req->newptr != NULL) {
287 		if (new == 0 || new < V_vegas_alpha)
288 			 error = EINVAL;
289 		else
290 			V_vegas_beta = new;
291 	}
292 
293 	return (error);
294 }
295 
296 SYSCTL_DECL(_net_inet_tcp_cc_vegas);
297 SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, vegas,
298     CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
299     "Vegas related settings");
300 
301 SYSCTL_PROC(_net_inet_tcp_cc_vegas, OID_AUTO, alpha,
302     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
303     &VNET_NAME(vegas_alpha), 1, &vegas_alpha_handler, "IU",
304     "vegas alpha, specified as number of \"buffers\" (0 < alpha < beta)");
305 
306 SYSCTL_PROC(_net_inet_tcp_cc_vegas, OID_AUTO, beta,
307     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
308     &VNET_NAME(vegas_beta), 3, &vegas_beta_handler, "IU",
309     "vegas beta, specified as number of \"buffers\" (0 < alpha < beta)");
310 
311 DECLARE_CC_MODULE(vegas, &vegas_cc_algo);
312 MODULE_VERSION(vegas, 2);
313 MODULE_DEPEND(vegas, ertt, 1, 1, 1);
314