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