1ac230a79SLawrence Stewart /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 732de3e790SGleb Smirnoff #include <netinet/tcp.h> 74ac230a79SLawrence Stewart #include <netinet/tcp_seq.h> 75ac230a79SLawrence Stewart #include <netinet/tcp_timer.h> 76ac230a79SLawrence Stewart #include <netinet/tcp_var.h> 774644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 78ac230a79SLawrence Stewart #include <netinet/cc/cc_module.h> 79ac230a79SLawrence Stewart 80ac230a79SLawrence Stewart #include <netinet/khelp/h_ertt.h> 81ac230a79SLawrence Stewart 82ac230a79SLawrence Stewart /* Largest possible number returned by random(). */ 83ac230a79SLawrence Stewart #define RANDOM_MAX INT_MAX 84ac230a79SLawrence Stewart 85ac230a79SLawrence Stewart static void hd_ack_received(struct cc_var *ccv, uint16_t ack_type); 86ac230a79SLawrence Stewart static int hd_mod_init(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, 100ac230a79SLawrence Stewart .mod_init = hd_mod_init 101ac230a79SLawrence Stewart }; 102ac230a79SLawrence Stewart 103ac230a79SLawrence Stewart /* 104ac230a79SLawrence Stewart * Hamilton backoff function. Returns 1 if we should backoff or 0 otherwise. 105ac230a79SLawrence Stewart */ 106ac230a79SLawrence Stewart static __inline int 107ac230a79SLawrence Stewart should_backoff(int qdly, int maxqdly) 108ac230a79SLawrence Stewart { 109ac230a79SLawrence Stewart unsigned long p; 110ac230a79SLawrence Stewart 111ac230a79SLawrence Stewart if (qdly < V_hd_qthresh) { 112ac230a79SLawrence Stewart p = (((RANDOM_MAX / 100) * V_hd_pmax) / 113ac230a79SLawrence Stewart (V_hd_qthresh - V_hd_qmin)) * (qdly - V_hd_qmin); 114ac230a79SLawrence Stewart } else { 115ac230a79SLawrence Stewart if (qdly > V_hd_qthresh) 116ac230a79SLawrence Stewart p = (((RANDOM_MAX / 100) * V_hd_pmax) / 117ac230a79SLawrence Stewart (maxqdly - V_hd_qthresh)) * (maxqdly - qdly); 118ac230a79SLawrence Stewart else 119ac230a79SLawrence Stewart p = (RANDOM_MAX / 100) * V_hd_pmax; 120ac230a79SLawrence Stewart } 121ac230a79SLawrence Stewart 122ac230a79SLawrence Stewart return (random() < p); 123ac230a79SLawrence Stewart } 124ac230a79SLawrence Stewart 125ac230a79SLawrence Stewart /* 126ac230a79SLawrence Stewart * If the ack type is CC_ACK, and the inferred queueing delay is greater than 127ac230a79SLawrence Stewart * the Qmin threshold, cwnd is reduced probabilistically. When backing off due 128ac230a79SLawrence Stewart * to delay, HD behaves like NewReno when an ECN signal is received. HD behaves 129ac230a79SLawrence Stewart * as NewReno in all other circumstances. 130ac230a79SLawrence Stewart */ 131ac230a79SLawrence Stewart static void 132ac230a79SLawrence Stewart hd_ack_received(struct cc_var *ccv, uint16_t ack_type) 133ac230a79SLawrence Stewart { 134ac230a79SLawrence Stewart struct ertt *e_t; 135ac230a79SLawrence Stewart int qdly; 136ac230a79SLawrence Stewart 137ac230a79SLawrence Stewart if (ack_type == CC_ACK) { 138ac230a79SLawrence Stewart e_t = khelp_get_osd(CCV(ccv, osd), ertt_id); 139ac230a79SLawrence Stewart 140ac230a79SLawrence Stewart if (e_t->rtt && e_t->minrtt && V_hd_qthresh > 0) { 141ac230a79SLawrence Stewart qdly = e_t->rtt - e_t->minrtt; 142ac230a79SLawrence Stewart 143ac230a79SLawrence Stewart if (qdly > V_hd_qmin && 144ac230a79SLawrence Stewart !IN_RECOVERY(CCV(ccv, t_flags))) { 145ac230a79SLawrence Stewart /* Probabilistic backoff of cwnd. */ 146ac230a79SLawrence Stewart if (should_backoff(qdly, 147ac230a79SLawrence Stewart e_t->maxrtt - e_t->minrtt)) { 148ac230a79SLawrence Stewart /* 149ac230a79SLawrence Stewart * Update cwnd and ssthresh update to 150ac230a79SLawrence Stewart * half cwnd and behave like an ECN (ie 151ac230a79SLawrence Stewart * not a packet loss). 152ac230a79SLawrence Stewart */ 153ac230a79SLawrence Stewart newreno_cc_algo.cong_signal(ccv, 154ac230a79SLawrence Stewart CC_ECN); 155ac230a79SLawrence Stewart return; 156ac230a79SLawrence Stewart } 157ac230a79SLawrence Stewart } 158ac230a79SLawrence Stewart } 159ac230a79SLawrence Stewart } 160ac230a79SLawrence Stewart newreno_cc_algo.ack_received(ccv, ack_type); /* As for NewReno. */ 161ac230a79SLawrence Stewart } 162ac230a79SLawrence Stewart 163ac230a79SLawrence Stewart static int 164ac230a79SLawrence Stewart hd_mod_init(void) 165ac230a79SLawrence Stewart { 166ac230a79SLawrence Stewart 167ac230a79SLawrence Stewart ertt_id = khelp_get_id("ertt"); 168ac230a79SLawrence Stewart if (ertt_id <= 0) { 169ac230a79SLawrence Stewart printf("%s: h_ertt module not found\n", __func__); 170ac230a79SLawrence Stewart return (ENOENT); 171ac230a79SLawrence Stewart } 172ac230a79SLawrence Stewart 173ac230a79SLawrence Stewart hd_cc_algo.after_idle = newreno_cc_algo.after_idle; 174ac230a79SLawrence Stewart hd_cc_algo.cong_signal = newreno_cc_algo.cong_signal; 175ac230a79SLawrence Stewart hd_cc_algo.post_recovery = newreno_cc_algo.post_recovery; 176ac230a79SLawrence Stewart 177ac230a79SLawrence Stewart return (0); 178ac230a79SLawrence Stewart } 179ac230a79SLawrence Stewart 180ac230a79SLawrence Stewart static int 181ac230a79SLawrence Stewart hd_pmax_handler(SYSCTL_HANDLER_ARGS) 182ac230a79SLawrence Stewart { 183ac230a79SLawrence Stewart int error; 184ac230a79SLawrence Stewart uint32_t new; 185ac230a79SLawrence Stewart 186ac230a79SLawrence Stewart new = V_hd_pmax; 187ac230a79SLawrence Stewart error = sysctl_handle_int(oidp, &new, 0, req); 188ac230a79SLawrence Stewart if (error == 0 && req->newptr != NULL) { 189*855acb84SBrooks Davis if (new == 0 || new > 100) 190ac230a79SLawrence Stewart error = EINVAL; 191ac230a79SLawrence Stewart else 192ac230a79SLawrence Stewart V_hd_pmax = new; 193ac230a79SLawrence Stewart } 194ac230a79SLawrence Stewart 195ac230a79SLawrence Stewart return (error); 196ac230a79SLawrence Stewart } 197ac230a79SLawrence Stewart 198ac230a79SLawrence Stewart static int 199ac230a79SLawrence Stewart hd_qmin_handler(SYSCTL_HANDLER_ARGS) 200ac230a79SLawrence Stewart { 201ac230a79SLawrence Stewart int error; 202ac230a79SLawrence Stewart uint32_t new; 203ac230a79SLawrence Stewart 204ac230a79SLawrence Stewart new = V_hd_qmin; 205ac230a79SLawrence Stewart error = sysctl_handle_int(oidp, &new, 0, req); 206ac230a79SLawrence Stewart if (error == 0 && req->newptr != NULL) { 207*855acb84SBrooks Davis if (new > V_hd_qthresh) 208ac230a79SLawrence Stewart error = EINVAL; 209ac230a79SLawrence Stewart else 210ac230a79SLawrence Stewart V_hd_qmin = new; 211ac230a79SLawrence Stewart } 212ac230a79SLawrence Stewart 213ac230a79SLawrence Stewart return (error); 214ac230a79SLawrence Stewart } 215ac230a79SLawrence Stewart 216ac230a79SLawrence Stewart static int 217ac230a79SLawrence Stewart hd_qthresh_handler(SYSCTL_HANDLER_ARGS) 218ac230a79SLawrence Stewart { 219ac230a79SLawrence Stewart int error; 220ac230a79SLawrence Stewart uint32_t new; 221ac230a79SLawrence Stewart 222ac230a79SLawrence Stewart new = V_hd_qthresh; 223ac230a79SLawrence Stewart error = sysctl_handle_int(oidp, &new, 0, req); 224ac230a79SLawrence Stewart if (error == 0 && req->newptr != NULL) { 225*855acb84SBrooks Davis if (new == 0 || new < V_hd_qmin) 226ac230a79SLawrence Stewart error = EINVAL; 227ac230a79SLawrence Stewart else 228ac230a79SLawrence Stewart V_hd_qthresh = new; 229ac230a79SLawrence Stewart } 230ac230a79SLawrence Stewart 231ac230a79SLawrence Stewart return (error); 232ac230a79SLawrence Stewart } 233ac230a79SLawrence Stewart 234ac230a79SLawrence Stewart SYSCTL_DECL(_net_inet_tcp_cc_hd); 235ac230a79SLawrence Stewart SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, hd, CTLFLAG_RW, NULL, 236ac230a79SLawrence Stewart "Hamilton delay-based congestion control related settings"); 237ac230a79SLawrence Stewart 2386df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, queue_threshold, 2396df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, &VNET_NAME(hd_qthresh), 20, 2406df8a710SGleb Smirnoff &hd_qthresh_handler, "IU", "queueing congestion threshold (qth) in ticks"); 241ac230a79SLawrence Stewart 2426df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, pmax, 2436df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, &VNET_NAME(hd_pmax), 5, 2446df8a710SGleb Smirnoff &hd_pmax_handler, "IU", 2456df8a710SGleb Smirnoff "per packet maximum backoff probability as a percentage"); 246ac230a79SLawrence Stewart 2476df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_tcp_cc_hd, OID_AUTO, queue_min, 2486df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, &VNET_NAME(hd_qmin), 5, 2496df8a710SGleb Smirnoff &hd_qmin_handler, "IU", "minimum queueing delay threshold (qmin) in ticks"); 250ac230a79SLawrence Stewart 251ac230a79SLawrence Stewart DECLARE_CC_MODULE(hd, &hd_cc_algo); 252ac230a79SLawrence Stewart MODULE_DEPEND(hd, ertt, 1, 1, 1); 253