xref: /freebsd/sys/netinet/cc/cc_hd.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1ac230a79SLawrence Stewart /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
4ac230a79SLawrence Stewart  * Copyright (c) 2009-2010
5ac230a79SLawrence Stewart  *	Swinburne University of Technology, Melbourne, Australia
6ac230a79SLawrence Stewart  * Copyright (c) 2010 Lawrence Stewart <lstewart@freebsd.org>
7ac230a79SLawrence Stewart  * Copyright (c) 2010-2011 The FreeBSD Foundation
8ac230a79SLawrence Stewart  * All rights reserved.
9ac230a79SLawrence Stewart  *
10ac230a79SLawrence Stewart  * This software was developed at the Centre for Advanced Internet
11891b8ed4SLawrence Stewart  * Architectures, Swinburne University of Technology, by David Hayes and
12891b8ed4SLawrence Stewart  * Lawrence Stewart, made possible in part by a grant from the Cisco University
13891b8ed4SLawrence Stewart  * Research Program Fund at Community Foundation Silicon Valley.
14ac230a79SLawrence Stewart  *
15ac230a79SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced Internet
16ac230a79SLawrence Stewart  * Architectures, Swinburne University of Technology, Melbourne, Australia by
17ac230a79SLawrence Stewart  * David Hayes under sponsorship from the FreeBSD Foundation.
18ac230a79SLawrence Stewart  *
19ac230a79SLawrence Stewart  * Redistribution and use in source and binary forms, with or without
20ac230a79SLawrence Stewart  * modification, are permitted provided that the following conditions
21ac230a79SLawrence Stewart  * are met:
22ac230a79SLawrence Stewart  * 1. Redistributions of source code must retain the above copyright
23ac230a79SLawrence Stewart  *    notice, this list of conditions and the following disclaimer.
24ac230a79SLawrence Stewart  * 2. Redistributions in binary form must reproduce the above copyright
25ac230a79SLawrence Stewart  *    notice, this list of conditions and the following disclaimer in the
26ac230a79SLawrence Stewart  *    documentation and/or other materials provided with the distribution.
27ac230a79SLawrence Stewart  *
28ac230a79SLawrence Stewart  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29ac230a79SLawrence Stewart  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30ac230a79SLawrence Stewart  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31ac230a79SLawrence Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32ac230a79SLawrence Stewart  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33ac230a79SLawrence Stewart  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34ac230a79SLawrence Stewart  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35ac230a79SLawrence Stewart  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36ac230a79SLawrence Stewart  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37ac230a79SLawrence Stewart  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38ac230a79SLawrence Stewart  * SUCH DAMAGE.
39ac230a79SLawrence Stewart  */
40ac230a79SLawrence Stewart 
41ac230a79SLawrence Stewart /*
42ac230a79SLawrence Stewart  * An implementation of the Hamilton Institute's delay-based congestion control
43ac230a79SLawrence Stewart  * algorithm for FreeBSD, based on "A strategy for fair coexistence of loss and
44ac230a79SLawrence Stewart  * delay-based congestion control algorithms," by L. Budzisz, R. Stanojevic, R.
45ac230a79SLawrence Stewart  * Shorten, and F. Baker, IEEE Commun. Lett., vol. 13, no. 7, pp. 555--557, Jul.
46ac230a79SLawrence Stewart  * 2009.
47ac230a79SLawrence Stewart  *
48ac230a79SLawrence Stewart  * Originally released as part of the NewTCP research project at Swinburne
49891b8ed4SLawrence Stewart  * University of Technology's Centre for Advanced Internet Architectures,
50891b8ed4SLawrence Stewart  * Melbourne, Australia, which was made possible in part by a grant from the
51891b8ed4SLawrence Stewart  * Cisco University Research Program Fund at Community Foundation Silicon
52891b8ed4SLawrence Stewart  * Valley. More details are available at:
53ac230a79SLawrence Stewart  *   http://caia.swin.edu.au/urp/newtcp/
54ac230a79SLawrence Stewart  */
55ac230a79SLawrence Stewart 
56ac230a79SLawrence Stewart #include <sys/cdefs.h>
57ac230a79SLawrence Stewart __FBSDID("$FreeBSD$");
58ac230a79SLawrence Stewart 
59ac230a79SLawrence Stewart #include <sys/param.h>
60ac230a79SLawrence Stewart #include <sys/kernel.h>
61ac230a79SLawrence Stewart #include <sys/khelp.h>
62ac230a79SLawrence Stewart #include <sys/limits.h>
63ac230a79SLawrence Stewart #include <sys/malloc.h>
64ac230a79SLawrence Stewart #include <sys/module.h>
65ac230a79SLawrence Stewart #include <sys/queue.h>
66ac230a79SLawrence Stewart #include <sys/socket.h>
67ac230a79SLawrence Stewart #include <sys/socketvar.h>
68ac230a79SLawrence Stewart #include <sys/sysctl.h>
69ac230a79SLawrence Stewart #include <sys/systm.h>
70ac230a79SLawrence Stewart 
71ac230a79SLawrence Stewart #include <net/vnet.h>
72ac230a79SLawrence Stewart 
73e68b3792SGleb Smirnoff #include <netinet/in.h>
74e68b3792SGleb Smirnoff #include <netinet/in_pcb.h>
75ac230a79SLawrence Stewart #include <netinet/tcp_seq.h>
76ac230a79SLawrence Stewart #include <netinet/tcp_timer.h>
77ac230a79SLawrence Stewart #include <netinet/tcp_var.h>
784644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
79ac230a79SLawrence Stewart #include <netinet/cc/cc_module.h>
80ac230a79SLawrence Stewart 
81ac230a79SLawrence Stewart #include <netinet/khelp/h_ertt.h>
82ac230a79SLawrence Stewart 
83ac230a79SLawrence Stewart /* Largest possible number returned by random(). */
84ac230a79SLawrence Stewart #define	RANDOM_MAX	INT_MAX
85ac230a79SLawrence Stewart 
86ac230a79SLawrence Stewart static void	hd_ack_received(struct cc_var *ccv, uint16_t ack_type);
87ac230a79SLawrence Stewart static int	hd_mod_init(void);
88b8d60729SRandall Stewart static size_t	hd_data_sz(void);
89ac230a79SLawrence Stewart 
90ac230a79SLawrence Stewart static int ertt_id;
91ac230a79SLawrence Stewart 
925f901c92SAndrew Turner VNET_DEFINE_STATIC(uint32_t, hd_qthresh) = 20;
935f901c92SAndrew Turner VNET_DEFINE_STATIC(uint32_t, hd_qmin) = 5;
945f901c92SAndrew Turner VNET_DEFINE_STATIC(uint32_t, hd_pmax) = 5;
95ac230a79SLawrence Stewart #define	V_hd_qthresh	VNET(hd_qthresh)
96ac230a79SLawrence Stewart #define	V_hd_qmin	VNET(hd_qmin)
97ac230a79SLawrence Stewart #define	V_hd_pmax	VNET(hd_pmax)
98ac230a79SLawrence Stewart 
99ac230a79SLawrence Stewart struct cc_algo hd_cc_algo = {
100ac230a79SLawrence Stewart 	.name = "hd",
101ac230a79SLawrence Stewart 	.ack_received = hd_ack_received,
102b8d60729SRandall Stewart 	.mod_init = hd_mod_init,
103b8d60729SRandall Stewart 	.cc_data_sz = hd_data_sz,
104b8d60729SRandall Stewart 	.after_idle = newreno_cc_after_idle,
105b8d60729SRandall Stewart 	.cong_signal = newreno_cc_cong_signal,
106b8d60729SRandall Stewart 	.post_recovery = newreno_cc_post_recovery,
107ac230a79SLawrence Stewart };
108ac230a79SLawrence Stewart 
109b8d60729SRandall Stewart static size_t
110b8d60729SRandall Stewart hd_data_sz(void)
111b8d60729SRandall Stewart {
112b8d60729SRandall Stewart 	return (0);
113b8d60729SRandall Stewart }
114b8d60729SRandall Stewart 
115ac230a79SLawrence Stewart /*
116ac230a79SLawrence Stewart  * Hamilton backoff function. Returns 1 if we should backoff or 0 otherwise.
117ac230a79SLawrence Stewart  */
118ac230a79SLawrence Stewart static __inline int
119ac230a79SLawrence Stewart should_backoff(int qdly, int maxqdly)
120ac230a79SLawrence Stewart {
121ac230a79SLawrence Stewart 	unsigned long p;
122ac230a79SLawrence Stewart 
123ac230a79SLawrence Stewart 	if (qdly < V_hd_qthresh) {
124ac230a79SLawrence Stewart 		p = (((RANDOM_MAX / 100) * V_hd_pmax) /
125ac230a79SLawrence Stewart 		    (V_hd_qthresh - V_hd_qmin)) * (qdly - V_hd_qmin);
126ac230a79SLawrence Stewart 	} else {
127ac230a79SLawrence Stewart 		if (qdly > V_hd_qthresh)
128ac230a79SLawrence Stewart 			p = (((RANDOM_MAX / 100) * V_hd_pmax) /
129ac230a79SLawrence Stewart 			    (maxqdly - V_hd_qthresh)) * (maxqdly - qdly);
130ac230a79SLawrence Stewart 		else
131ac230a79SLawrence Stewart 			p = (RANDOM_MAX / 100) * V_hd_pmax;
132ac230a79SLawrence Stewart 	}
133ac230a79SLawrence Stewart 
134ac230a79SLawrence Stewart 	return (random() < p);
135ac230a79SLawrence Stewart }
136ac230a79SLawrence Stewart 
137ac230a79SLawrence Stewart /*
138ac230a79SLawrence Stewart  * If the ack type is CC_ACK, and the inferred queueing delay is greater than
139ac230a79SLawrence Stewart  * the Qmin threshold, cwnd is reduced probabilistically. When backing off due
140ac230a79SLawrence Stewart  * to delay, HD behaves like NewReno when an ECN signal is received. HD behaves
141ac230a79SLawrence Stewart  * as NewReno in all other circumstances.
142ac230a79SLawrence Stewart  */
143ac230a79SLawrence Stewart static void
144ac230a79SLawrence Stewart hd_ack_received(struct cc_var *ccv, uint16_t ack_type)
145ac230a79SLawrence Stewart {
146ac230a79SLawrence Stewart 	struct ertt *e_t;
147ac230a79SLawrence Stewart 	int qdly;
148ac230a79SLawrence Stewart 
149ac230a79SLawrence Stewart 	if (ack_type == CC_ACK) {
150e68b3792SGleb Smirnoff 		e_t = khelp_get_osd(&CCV(ccv, t_osd), ertt_id);
151ac230a79SLawrence Stewart 
152ac230a79SLawrence Stewart 		if (e_t->rtt && e_t->minrtt && V_hd_qthresh > 0) {
153ac230a79SLawrence Stewart 			qdly = e_t->rtt - e_t->minrtt;
154ac230a79SLawrence Stewart 
155ac230a79SLawrence Stewart 			if (qdly > V_hd_qmin &&
156ac230a79SLawrence Stewart 			    !IN_RECOVERY(CCV(ccv, t_flags))) {
157ac230a79SLawrence Stewart 				/* Probabilistic backoff of cwnd. */
158ac230a79SLawrence Stewart 				if (should_backoff(qdly,
159ac230a79SLawrence Stewart 				    e_t->maxrtt - e_t->minrtt)) {
160ac230a79SLawrence Stewart 					/*
161ac230a79SLawrence Stewart 					 * Update cwnd and ssthresh update to
162ac230a79SLawrence Stewart 					 * half cwnd and behave like an ECN (ie
163ac230a79SLawrence Stewart 					 * not a packet loss).
164ac230a79SLawrence Stewart 					 */
165b8d60729SRandall Stewart 					newreno_cc_cong_signal(ccv,
166ac230a79SLawrence Stewart 					    CC_ECN);
167ac230a79SLawrence Stewart 					return;
168ac230a79SLawrence Stewart 				}
169ac230a79SLawrence Stewart 			}
170ac230a79SLawrence Stewart 		}
171ac230a79SLawrence Stewart 	}
172b8d60729SRandall Stewart 	newreno_cc_ack_received(ccv, ack_type);
173ac230a79SLawrence Stewart }
174ac230a79SLawrence Stewart 
175ac230a79SLawrence Stewart static int
176ac230a79SLawrence Stewart hd_mod_init(void)
177ac230a79SLawrence Stewart {
178ac230a79SLawrence Stewart 
179ac230a79SLawrence Stewart 	ertt_id = khelp_get_id("ertt");
180ac230a79SLawrence Stewart 	if (ertt_id <= 0) {
181ac230a79SLawrence Stewart 		printf("%s: h_ertt module not found\n", __func__);
182ac230a79SLawrence Stewart 		return (ENOENT);
183ac230a79SLawrence Stewart 	}
184ac230a79SLawrence Stewart 	return (0);
185ac230a79SLawrence Stewart }
186ac230a79SLawrence Stewart 
187ac230a79SLawrence Stewart static int
188ac230a79SLawrence Stewart hd_pmax_handler(SYSCTL_HANDLER_ARGS)
189ac230a79SLawrence Stewart {
190ac230a79SLawrence Stewart 	int error;
191ac230a79SLawrence Stewart 	uint32_t new;
192ac230a79SLawrence Stewart 
193ac230a79SLawrence Stewart 	new = V_hd_pmax;
194ac230a79SLawrence Stewart 	error = sysctl_handle_int(oidp, &new, 0, req);
195ac230a79SLawrence Stewart 	if (error == 0 && req->newptr != NULL) {
196855acb84SBrooks Davis 		if (new == 0 || new > 100)
197ac230a79SLawrence Stewart 			error = EINVAL;
198ac230a79SLawrence Stewart 		else
199ac230a79SLawrence Stewart 			V_hd_pmax = new;
200ac230a79SLawrence Stewart 	}
201ac230a79SLawrence Stewart 
202ac230a79SLawrence Stewart 	return (error);
203ac230a79SLawrence Stewart }
204ac230a79SLawrence Stewart 
205ac230a79SLawrence Stewart static int
206ac230a79SLawrence Stewart hd_qmin_handler(SYSCTL_HANDLER_ARGS)
207ac230a79SLawrence Stewart {
208ac230a79SLawrence Stewart 	int error;
209ac230a79SLawrence Stewart 	uint32_t new;
210ac230a79SLawrence Stewart 
211ac230a79SLawrence Stewart 	new = V_hd_qmin;
212ac230a79SLawrence Stewart 	error = sysctl_handle_int(oidp, &new, 0, req);
213ac230a79SLawrence Stewart 	if (error == 0 && req->newptr != NULL) {
214855acb84SBrooks Davis 		if (new > V_hd_qthresh)
215ac230a79SLawrence Stewart 			error = EINVAL;
216ac230a79SLawrence Stewart 		else
217ac230a79SLawrence Stewart 			V_hd_qmin = new;
218ac230a79SLawrence Stewart 	}
219ac230a79SLawrence Stewart 
220ac230a79SLawrence Stewart 	return (error);
221ac230a79SLawrence Stewart }
222ac230a79SLawrence Stewart 
223ac230a79SLawrence Stewart static int
224ac230a79SLawrence Stewart hd_qthresh_handler(SYSCTL_HANDLER_ARGS)
225ac230a79SLawrence Stewart {
226ac230a79SLawrence Stewart 	int error;
227ac230a79SLawrence Stewart 	uint32_t new;
228ac230a79SLawrence Stewart 
229ac230a79SLawrence Stewart 	new = V_hd_qthresh;
230ac230a79SLawrence Stewart 	error = sysctl_handle_int(oidp, &new, 0, req);
231ac230a79SLawrence Stewart 	if (error == 0 && req->newptr != NULL) {
232855acb84SBrooks Davis 		if (new == 0 || new < V_hd_qmin)
233ac230a79SLawrence Stewart 			error = EINVAL;
234ac230a79SLawrence Stewart 		else
235ac230a79SLawrence Stewart 			V_hd_qthresh = new;
236ac230a79SLawrence Stewart 	}
237ac230a79SLawrence Stewart 
238ac230a79SLawrence Stewart 	return (error);
239ac230a79SLawrence Stewart }
240ac230a79SLawrence Stewart 
241ac230a79SLawrence Stewart SYSCTL_DECL(_net_inet_tcp_cc_hd);
2427029da5cSPawel Biernacki SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, hd, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
243ac230a79SLawrence Stewart     "Hamilton delay-based congestion control related settings");
244ac230a79SLawrence Stewart 
2456df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, queue_threshold,
2467029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2477029da5cSPawel Biernacki     &VNET_NAME(hd_qthresh), 20, &hd_qthresh_handler, "IU",
2487029da5cSPawel Biernacki     "queueing congestion threshold (qth) in ticks");
249ac230a79SLawrence Stewart 
2506df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, pmax,
2517029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2527029da5cSPawel Biernacki     &VNET_NAME(hd_pmax), 5, &hd_pmax_handler, "IU",
2536df8a710SGleb Smirnoff     "per packet maximum backoff probability as a percentage");
254ac230a79SLawrence Stewart 
2556df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, queue_min,
2567029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2577029da5cSPawel Biernacki     &VNET_NAME(hd_qmin), 5, &hd_qmin_handler, "IU",
2587029da5cSPawel Biernacki     "minimum queueing delay threshold (qmin) in ticks");
259ac230a79SLawrence Stewart 
260ac230a79SLawrence Stewart DECLARE_CC_MODULE(hd, &hd_cc_algo);
261b8d60729SRandall Stewart MODULE_VERSION(hd, 2);
262ac230a79SLawrence Stewart MODULE_DEPEND(hd, ertt, 1, 1, 1);
263