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 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/cdefs.h>
59__FBSDID("$FreeBSD$");
60
61#include <sys/param.h>
62#include <sys/kernel.h>
63#include <sys/khelp.h>
64#include <sys/malloc.h>
65#include <sys/module.h>
66#include <sys/queue.h>
67#include <sys/socket.h>
68#include <sys/socketvar.h>
69#include <sys/sysctl.h>
70#include <sys/systm.h>
71
72#include <net/vnet.h>
73
74#include <netinet/tcp.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_VEGAS_RATE	0x01000000
87
88static void	vegas_ack_received(struct cc_var *ccv, uint16_t ack_type);
89static void	vegas_cb_destroy(struct cc_var *ccv);
90static int	vegas_cb_init(struct cc_var *ccv);
91static void	vegas_cong_signal(struct cc_var *ccv, uint32_t signal_type);
92static void	vegas_conn_init(struct cc_var *ccv);
93static int	vegas_mod_init(void);
94
95struct vegas {
96	int slow_start_toggle;
97};
98
99static int32_t ertt_id;
100
101VNET_DEFINE_STATIC(uint32_t, vegas_alpha) = 1;
102VNET_DEFINE_STATIC(uint32_t, vegas_beta) = 3;
103#define	V_vegas_alpha	VNET(vegas_alpha)
104#define	V_vegas_beta	VNET(vegas_beta)
105
106static MALLOC_DEFINE(M_VEGAS, "vegas data",
107    "Per connection data required for the Vegas congestion control algorithm");
108
109struct cc_algo vegas_cc_algo = {
110	.name = "vegas",
111	.ack_received = vegas_ack_received,
112	.cb_destroy = vegas_cb_destroy,
113	.cb_init = vegas_cb_init,
114	.cong_signal = vegas_cong_signal,
115	.conn_init = vegas_conn_init,
116	.mod_init = vegas_mod_init
117};
118
119/*
120 * The vegas window adjustment is done once every RTT, as indicated by the
121 * ERTT_NEW_MEASUREMENT flag. This flag is reset once the new measurment data
122 * has been used.
123 */
124static void
125vegas_ack_received(struct cc_var *ccv, uint16_t ack_type)
126{
127	struct ertt *e_t;
128	struct vegas *vegas_data;
129	long actual_tx_rate, expected_tx_rate, ndiff;
130
131	e_t = khelp_get_osd(CCV(ccv, osd), ertt_id);
132	vegas_data = ccv->cc_data;
133
134	if (e_t->flags & ERTT_NEW_MEASUREMENT) { /* Once per RTT. */
135		if (e_t->minrtt && e_t->markedpkt_rtt) {
136			expected_tx_rate = e_t->marked_snd_cwnd / e_t->minrtt;
137			actual_tx_rate = e_t->bytes_tx_in_marked_rtt /
138			    e_t->markedpkt_rtt;
139			ndiff = (expected_tx_rate - actual_tx_rate) *
140			    e_t->minrtt / CCV(ccv, t_maxseg);
141
142			if (ndiff < V_vegas_alpha) {
143				if (CCV(ccv, snd_cwnd) <=
144				    CCV(ccv, snd_ssthresh)) {
145					vegas_data->slow_start_toggle =
146					    vegas_data->slow_start_toggle ?
147					    0 : 1;
148				} else {
149					vegas_data->slow_start_toggle = 0;
150					CCV(ccv, snd_cwnd) =
151					    min(CCV(ccv, snd_cwnd) +
152					    CCV(ccv, t_maxseg),
153					    TCP_MAXWIN << CCV(ccv, snd_scale));
154				}
155			} else if (ndiff > V_vegas_beta) {
156				/* Rate-based congestion. */
157				vegas_cong_signal(ccv, CC_VEGAS_RATE);
158				vegas_data->slow_start_toggle = 0;
159			}
160		}
161		e_t->flags &= ~ERTT_NEW_MEASUREMENT;
162	}
163
164	if (vegas_data->slow_start_toggle)
165		newreno_cc_algo.ack_received(ccv, ack_type);
166}
167
168static void
169vegas_cb_destroy(struct cc_var *ccv)
170{
171	free(ccv->cc_data, M_VEGAS);
172}
173
174static int
175vegas_cb_init(struct cc_var *ccv)
176{
177	struct vegas *vegas_data;
178
179	vegas_data = malloc(sizeof(struct vegas), M_VEGAS, M_NOWAIT);
180
181	if (vegas_data == NULL)
182		return (ENOMEM);
183
184	vegas_data->slow_start_toggle = 1;
185	ccv->cc_data = vegas_data;
186
187	return (0);
188}
189
190/*
191 * If congestion has been triggered triggered by the Vegas measured rates, it is
192 * handled here, otherwise it falls back to newreno's congestion handling.
193 */
194static void
195vegas_cong_signal(struct cc_var *ccv, uint32_t signal_type)
196{
197	struct vegas *vegas_data;
198	int presignalrecov;
199
200	vegas_data = ccv->cc_data;
201
202	if (IN_RECOVERY(CCV(ccv, t_flags)))
203		presignalrecov = 1;
204	else
205		presignalrecov = 0;
206
207	switch(signal_type) {
208	case CC_VEGAS_RATE:
209		if (!IN_RECOVERY(CCV(ccv, t_flags))) {
210			CCV(ccv, snd_cwnd) = max(2 * CCV(ccv, t_maxseg),
211			    CCV(ccv, snd_cwnd) - CCV(ccv, t_maxseg));
212			if (CCV(ccv, snd_cwnd) < CCV(ccv, snd_ssthresh))
213				/* Exit slow start. */
214				CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
215		}
216		break;
217
218	default:
219		newreno_cc_algo.cong_signal(ccv, signal_type);
220	}
221
222	if (IN_RECOVERY(CCV(ccv, t_flags)) && !presignalrecov)
223		vegas_data->slow_start_toggle =
224		    (CCV(ccv, snd_cwnd) < CCV(ccv, snd_ssthresh)) ? 1 : 0;
225}
226
227static void
228vegas_conn_init(struct cc_var *ccv)
229{
230	struct vegas *vegas_data;
231
232	vegas_data = ccv->cc_data;
233	vegas_data->slow_start_toggle = 1;
234}
235
236static int
237vegas_mod_init(void)
238{
239
240	ertt_id = khelp_get_id("ertt");
241	if (ertt_id <= 0) {
242		printf("%s: h_ertt module not found\n", __func__);
243		return (ENOENT);
244	}
245
246	vegas_cc_algo.after_idle = newreno_cc_algo.after_idle;
247	vegas_cc_algo.post_recovery = newreno_cc_algo.post_recovery;
248
249	return (0);
250}
251
252static int
253vegas_alpha_handler(SYSCTL_HANDLER_ARGS)
254{
255	int error;
256	uint32_t new;
257
258	new = V_vegas_alpha;
259	error = sysctl_handle_int(oidp, &new, 0, req);
260	if (error == 0 && req->newptr != NULL) {
261		if (new == 0 || new > V_vegas_beta)
262			error = EINVAL;
263		else
264			V_vegas_alpha = new;
265	}
266
267	return (error);
268}
269
270static int
271vegas_beta_handler(SYSCTL_HANDLER_ARGS)
272{
273	int error;
274	uint32_t new;
275
276	new = V_vegas_beta;
277	error = sysctl_handle_int(oidp, &new, 0, req);
278	if (error == 0 && req->newptr != NULL) {
279		if (new == 0 || new < V_vegas_alpha)
280			 error = EINVAL;
281		else
282			V_vegas_beta = new;
283	}
284
285	return (error);
286}
287
288SYSCTL_DECL(_net_inet_tcp_cc_vegas);
289SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, vegas, CTLFLAG_RW, NULL,
290    "Vegas related settings");
291
292SYSCTL_PROC(_net_inet_tcp_cc_vegas, OID_AUTO, alpha,
293    CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW,
294    &VNET_NAME(vegas_alpha), 1, &vegas_alpha_handler, "IU",
295    "vegas alpha, specified as number of \"buffers\" (0 < alpha < beta)");
296
297SYSCTL_PROC(_net_inet_tcp_cc_vegas, OID_AUTO, beta,
298    CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW,
299    &VNET_NAME(vegas_beta), 3, &vegas_beta_handler, "IU",
300    "vegas beta, specified as number of \"buffers\" (0 < alpha < beta)");
301
302DECLARE_CC_MODULE(vegas, &vegas_cc_algo);
303MODULE_VERSION(vegas, 1);
304MODULE_DEPEND(vegas, ertt, 1, 1, 1);
305