xref: /freebsd/sys/netinet/cc/cc_hd.c (revision 674956e1997954e8478e68080061c0530b081515)
1ac230a79SLawrence Stewart /*-
24d846d26SWarner 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/param.h>
57ac230a79SLawrence Stewart #include <sys/kernel.h>
58ac230a79SLawrence Stewart #include <sys/khelp.h>
59ac230a79SLawrence Stewart #include <sys/limits.h>
60ac230a79SLawrence Stewart #include <sys/malloc.h>
61ac230a79SLawrence Stewart #include <sys/module.h>
62*674956e1SHenrich Hartzer #include <sys/prng.h>
63ac230a79SLawrence Stewart #include <sys/queue.h>
64ac230a79SLawrence Stewart #include <sys/socket.h>
65ac230a79SLawrence Stewart #include <sys/socketvar.h>
66ac230a79SLawrence Stewart #include <sys/sysctl.h>
67ac230a79SLawrence Stewart #include <sys/systm.h>
68ac230a79SLawrence Stewart 
69ac230a79SLawrence Stewart #include <net/vnet.h>
70ac230a79SLawrence Stewart 
71e68b3792SGleb Smirnoff #include <netinet/in.h>
72e68b3792SGleb Smirnoff #include <netinet/in_pcb.h>
73ac230a79SLawrence Stewart #include <netinet/tcp_seq.h>
74ac230a79SLawrence Stewart #include <netinet/tcp_timer.h>
75ac230a79SLawrence Stewart #include <netinet/tcp_var.h>
764644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
77ac230a79SLawrence Stewart #include <netinet/cc/cc_module.h>
78ac230a79SLawrence Stewart 
79ac230a79SLawrence Stewart #include <netinet/khelp/h_ertt.h>
80ac230a79SLawrence Stewart 
81*674956e1SHenrich Hartzer /* Largest possible number returned by prng32(). */
82*674956e1SHenrich Hartzer #define	RANDOM_MAX	UINT32_MAX
83ac230a79SLawrence Stewart 
84f74352fbSRichard Scheffenegger static void	hd_ack_received(struct cc_var *ccv, ccsignal_t ack_type);
85ac230a79SLawrence Stewart static int	hd_mod_init(void);
86b8d60729SRandall Stewart static size_t	hd_data_sz(void);
87ac230a79SLawrence Stewart 
88ac230a79SLawrence Stewart static int ertt_id;
89ac230a79SLawrence Stewart 
905f901c92SAndrew Turner VNET_DEFINE_STATIC(uint32_t, hd_qthresh) = 20;
915f901c92SAndrew Turner VNET_DEFINE_STATIC(uint32_t, hd_qmin) = 5;
925f901c92SAndrew Turner VNET_DEFINE_STATIC(uint32_t, hd_pmax) = 5;
93ac230a79SLawrence Stewart #define	V_hd_qthresh	VNET(hd_qthresh)
94ac230a79SLawrence Stewart #define	V_hd_qmin	VNET(hd_qmin)
95ac230a79SLawrence Stewart #define	V_hd_pmax	VNET(hd_pmax)
96ac230a79SLawrence Stewart 
97ac230a79SLawrence Stewart struct cc_algo hd_cc_algo = {
98ac230a79SLawrence Stewart 	.name = "hd",
99ac230a79SLawrence Stewart 	.ack_received = hd_ack_received,
100b8d60729SRandall Stewart 	.mod_init = hd_mod_init,
101b8d60729SRandall Stewart 	.cc_data_sz = hd_data_sz,
102b8d60729SRandall Stewart 	.after_idle = newreno_cc_after_idle,
103b8d60729SRandall Stewart 	.cong_signal = newreno_cc_cong_signal,
104b8d60729SRandall Stewart 	.post_recovery = newreno_cc_post_recovery,
105ac230a79SLawrence Stewart };
106ac230a79SLawrence Stewart 
107b8d60729SRandall Stewart static size_t
108b8d60729SRandall Stewart hd_data_sz(void)
109b8d60729SRandall Stewart {
110b8d60729SRandall Stewart 	return (0);
111b8d60729SRandall Stewart }
112b8d60729SRandall Stewart 
113ac230a79SLawrence Stewart /*
114ac230a79SLawrence Stewart  * Hamilton backoff function. Returns 1 if we should backoff or 0 otherwise.
115ac230a79SLawrence Stewart  */
116ac230a79SLawrence Stewart static __inline int
117ac230a79SLawrence Stewart should_backoff(int qdly, int maxqdly)
118ac230a79SLawrence Stewart {
119ac230a79SLawrence Stewart 	unsigned long p;
120ac230a79SLawrence Stewart 
121ac230a79SLawrence Stewart 	if (qdly < V_hd_qthresh) {
122ac230a79SLawrence Stewart 		p = (((RANDOM_MAX / 100) * V_hd_pmax) /
123ac230a79SLawrence Stewart 		    (V_hd_qthresh - V_hd_qmin)) * (qdly - V_hd_qmin);
124ac230a79SLawrence Stewart 	} else {
125ac230a79SLawrence Stewart 		if (qdly > V_hd_qthresh)
126ac230a79SLawrence Stewart 			p = (((RANDOM_MAX / 100) * V_hd_pmax) /
127ac230a79SLawrence Stewart 			    (maxqdly - V_hd_qthresh)) * (maxqdly - qdly);
128ac230a79SLawrence Stewart 		else
129ac230a79SLawrence Stewart 			p = (RANDOM_MAX / 100) * V_hd_pmax;
130ac230a79SLawrence Stewart 	}
131ac230a79SLawrence Stewart 
132*674956e1SHenrich Hartzer 	return (prng32() < p);
133ac230a79SLawrence Stewart }
134ac230a79SLawrence Stewart 
135ac230a79SLawrence Stewart /*
136ac230a79SLawrence Stewart  * If the ack type is CC_ACK, and the inferred queueing delay is greater than
137ac230a79SLawrence Stewart  * the Qmin threshold, cwnd is reduced probabilistically. When backing off due
138ac230a79SLawrence Stewart  * to delay, HD behaves like NewReno when an ECN signal is received. HD behaves
139ac230a79SLawrence Stewart  * as NewReno in all other circumstances.
140ac230a79SLawrence Stewart  */
141ac230a79SLawrence Stewart static void
142f74352fbSRichard Scheffenegger hd_ack_received(struct cc_var *ccv, ccsignal_t ack_type)
143ac230a79SLawrence Stewart {
144ac230a79SLawrence Stewart 	struct ertt *e_t;
145ac230a79SLawrence Stewart 	int qdly;
146ac230a79SLawrence Stewart 
147ac230a79SLawrence Stewart 	if (ack_type == CC_ACK) {
148e68b3792SGleb Smirnoff 		e_t = khelp_get_osd(&CCV(ccv, t_osd), ertt_id);
149ac230a79SLawrence Stewart 
150ac230a79SLawrence Stewart 		if (e_t->rtt && e_t->minrtt && V_hd_qthresh > 0) {
151ac230a79SLawrence Stewart 			qdly = e_t->rtt - e_t->minrtt;
152ac230a79SLawrence Stewart 
153ac230a79SLawrence Stewart 			if (qdly > V_hd_qmin &&
154ac230a79SLawrence Stewart 			    !IN_RECOVERY(CCV(ccv, t_flags))) {
155ac230a79SLawrence Stewart 				/* Probabilistic backoff of cwnd. */
156ac230a79SLawrence Stewart 				if (should_backoff(qdly,
157ac230a79SLawrence Stewart 				    e_t->maxrtt - e_t->minrtt)) {
158ac230a79SLawrence Stewart 					/*
159ac230a79SLawrence Stewart 					 * Update cwnd and ssthresh update to
160ac230a79SLawrence Stewart 					 * half cwnd and behave like an ECN (ie
161ac230a79SLawrence Stewart 					 * not a packet loss).
162ac230a79SLawrence Stewart 					 */
163b8d60729SRandall Stewart 					newreno_cc_cong_signal(ccv,
164ac230a79SLawrence Stewart 					    CC_ECN);
165ac230a79SLawrence Stewart 					return;
166ac230a79SLawrence Stewart 				}
167ac230a79SLawrence Stewart 			}
168ac230a79SLawrence Stewart 		}
169ac230a79SLawrence Stewart 	}
170b8d60729SRandall Stewart 	newreno_cc_ack_received(ccv, ack_type);
171ac230a79SLawrence Stewart }
172ac230a79SLawrence Stewart 
173ac230a79SLawrence Stewart static int
174ac230a79SLawrence Stewart hd_mod_init(void)
175ac230a79SLawrence Stewart {
176ac230a79SLawrence Stewart 
177ac230a79SLawrence Stewart 	ertt_id = khelp_get_id("ertt");
178ac230a79SLawrence Stewart 	if (ertt_id <= 0) {
179ac230a79SLawrence Stewart 		printf("%s: h_ertt module not found\n", __func__);
180ac230a79SLawrence Stewart 		return (ENOENT);
181ac230a79SLawrence Stewart 	}
182ac230a79SLawrence Stewart 	return (0);
183ac230a79SLawrence Stewart }
184ac230a79SLawrence Stewart 
185ac230a79SLawrence Stewart static int
186ac230a79SLawrence Stewart hd_pmax_handler(SYSCTL_HANDLER_ARGS)
187ac230a79SLawrence Stewart {
188ac230a79SLawrence Stewart 	int error;
189ac230a79SLawrence Stewart 	uint32_t new;
190ac230a79SLawrence Stewart 
191ac230a79SLawrence Stewart 	new = V_hd_pmax;
192ac230a79SLawrence Stewart 	error = sysctl_handle_int(oidp, &new, 0, req);
193ac230a79SLawrence Stewart 	if (error == 0 && req->newptr != NULL) {
194855acb84SBrooks Davis 		if (new == 0 || new > 100)
195ac230a79SLawrence Stewart 			error = EINVAL;
196ac230a79SLawrence Stewart 		else
197ac230a79SLawrence Stewart 			V_hd_pmax = new;
198ac230a79SLawrence Stewart 	}
199ac230a79SLawrence Stewart 
200ac230a79SLawrence Stewart 	return (error);
201ac230a79SLawrence Stewart }
202ac230a79SLawrence Stewart 
203ac230a79SLawrence Stewart static int
204ac230a79SLawrence Stewart hd_qmin_handler(SYSCTL_HANDLER_ARGS)
205ac230a79SLawrence Stewart {
206ac230a79SLawrence Stewart 	int error;
207ac230a79SLawrence Stewart 	uint32_t new;
208ac230a79SLawrence Stewart 
209ac230a79SLawrence Stewart 	new = V_hd_qmin;
210ac230a79SLawrence Stewart 	error = sysctl_handle_int(oidp, &new, 0, req);
211ac230a79SLawrence Stewart 	if (error == 0 && req->newptr != NULL) {
212855acb84SBrooks Davis 		if (new > V_hd_qthresh)
213ac230a79SLawrence Stewart 			error = EINVAL;
214ac230a79SLawrence Stewart 		else
215ac230a79SLawrence Stewart 			V_hd_qmin = new;
216ac230a79SLawrence Stewart 	}
217ac230a79SLawrence Stewart 
218ac230a79SLawrence Stewart 	return (error);
219ac230a79SLawrence Stewart }
220ac230a79SLawrence Stewart 
221ac230a79SLawrence Stewart static int
222ac230a79SLawrence Stewart hd_qthresh_handler(SYSCTL_HANDLER_ARGS)
223ac230a79SLawrence Stewart {
224ac230a79SLawrence Stewart 	int error;
225ac230a79SLawrence Stewart 	uint32_t new;
226ac230a79SLawrence Stewart 
227ac230a79SLawrence Stewart 	new = V_hd_qthresh;
228ac230a79SLawrence Stewart 	error = sysctl_handle_int(oidp, &new, 0, req);
229ac230a79SLawrence Stewart 	if (error == 0 && req->newptr != NULL) {
230855acb84SBrooks Davis 		if (new == 0 || new < V_hd_qmin)
231ac230a79SLawrence Stewart 			error = EINVAL;
232ac230a79SLawrence Stewart 		else
233ac230a79SLawrence Stewart 			V_hd_qthresh = new;
234ac230a79SLawrence Stewart 	}
235ac230a79SLawrence Stewart 
236ac230a79SLawrence Stewart 	return (error);
237ac230a79SLawrence Stewart }
238ac230a79SLawrence Stewart 
239ac230a79SLawrence Stewart SYSCTL_DECL(_net_inet_tcp_cc_hd);
2407029da5cSPawel Biernacki SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, hd, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
241ac230a79SLawrence Stewart     "Hamilton delay-based congestion control related settings");
242ac230a79SLawrence Stewart 
2436df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, queue_threshold,
2447029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2457029da5cSPawel Biernacki     &VNET_NAME(hd_qthresh), 20, &hd_qthresh_handler, "IU",
2467029da5cSPawel Biernacki     "queueing congestion threshold (qth) in ticks");
247ac230a79SLawrence Stewart 
2486df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, pmax,
2497029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2507029da5cSPawel Biernacki     &VNET_NAME(hd_pmax), 5, &hd_pmax_handler, "IU",
2516df8a710SGleb Smirnoff     "per packet maximum backoff probability as a percentage");
252ac230a79SLawrence Stewart 
2536df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, queue_min,
2547029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2557029da5cSPawel Biernacki     &VNET_NAME(hd_qmin), 5, &hd_qmin_handler, "IU",
2567029da5cSPawel Biernacki     "minimum queueing delay threshold (qmin) in ticks");
257ac230a79SLawrence Stewart 
258ac230a79SLawrence Stewart DECLARE_CC_MODULE(hd, &hd_cc_algo);
259b8d60729SRandall Stewart MODULE_VERSION(hd, 2);
260ac230a79SLawrence Stewart MODULE_DEPEND(hd, ertt, 1, 1, 1);
261