1 /*- 2 * Copyright (c) 2007-2008 3 * Swinburne University of Technology, Melbourne, Australia 4 * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org> 5 * Copyright (c) 2014 Midori Kato <katoon@sfc.wide.ad.jp> 6 * Copyright (c) 2014 The FreeBSD Foundation 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 /* 32 * An implementation of the DCTCP algorithm for FreeBSD, based on 33 * "Data Center TCP (DCTCP)" by M. Alizadeh, A. Greenberg, D. A. Maltz, 34 * J. Padhye, P. Patel, B. Prabhakar, S. Sengupta, and M. Sridharan., 35 * in ACM Conference on SIGCOMM 2010, New York, USA, 36 * Originally released as the contribution of Microsoft Research project. 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include <sys/param.h> 43 #include <sys/kernel.h> 44 #include <sys/malloc.h> 45 #include <sys/module.h> 46 #include <sys/socket.h> 47 #include <sys/socketvar.h> 48 #include <sys/sysctl.h> 49 #include <sys/systm.h> 50 51 #include <net/vnet.h> 52 53 #include <netinet/tcp.h> 54 #include <netinet/tcp_seq.h> 55 #include <netinet/tcp_var.h> 56 #include <netinet/cc/cc.h> 57 #include <netinet/cc/cc_module.h> 58 59 #define DCTCP_SHIFT 10 60 #define MAX_ALPHA_VALUE (1<<DCTCP_SHIFT) 61 VNET_DEFINE_STATIC(uint32_t, dctcp_alpha) = MAX_ALPHA_VALUE; 62 #define V_dctcp_alpha VNET(dctcp_alpha) 63 VNET_DEFINE_STATIC(uint32_t, dctcp_shift_g) = 4; 64 #define V_dctcp_shift_g VNET(dctcp_shift_g) 65 VNET_DEFINE_STATIC(uint32_t, dctcp_slowstart) = 0; 66 #define V_dctcp_slowstart VNET(dctcp_slowstart) 67 68 struct dctcp { 69 uint32_t bytes_ecn; /* # of marked bytes during a RTT */ 70 uint32_t bytes_total; /* # of acked bytes during a RTT */ 71 int alpha; /* the fraction of marked bytes */ 72 int ce_prev; /* CE state of the last segment */ 73 tcp_seq save_sndnxt; /* end sequence number of the current window */ 74 int ece_curr; /* ECE flag in this segment */ 75 int ece_prev; /* ECE flag in the last segment */ 76 uint32_t num_cong_events; /* # of congestion events */ 77 }; 78 79 static MALLOC_DEFINE(M_dctcp, "dctcp data", 80 "Per connection data required for the dctcp algorithm"); 81 82 static void dctcp_ack_received(struct cc_var *ccv, uint16_t type); 83 static void dctcp_after_idle(struct cc_var *ccv); 84 static void dctcp_cb_destroy(struct cc_var *ccv); 85 static int dctcp_cb_init(struct cc_var *ccv); 86 static void dctcp_cong_signal(struct cc_var *ccv, uint32_t type); 87 static void dctcp_conn_init(struct cc_var *ccv); 88 static void dctcp_post_recovery(struct cc_var *ccv); 89 static void dctcp_ecnpkt_handler(struct cc_var *ccv); 90 static void dctcp_update_alpha(struct cc_var *ccv); 91 92 struct cc_algo dctcp_cc_algo = { 93 .name = "dctcp", 94 .ack_received = dctcp_ack_received, 95 .cb_destroy = dctcp_cb_destroy, 96 .cb_init = dctcp_cb_init, 97 .cong_signal = dctcp_cong_signal, 98 .conn_init = dctcp_conn_init, 99 .post_recovery = dctcp_post_recovery, 100 .ecnpkt_handler = dctcp_ecnpkt_handler, 101 .after_idle = dctcp_after_idle, 102 }; 103 104 static void 105 dctcp_ack_received(struct cc_var *ccv, uint16_t type) 106 { 107 struct dctcp *dctcp_data; 108 int bytes_acked = 0; 109 110 dctcp_data = ccv->cc_data; 111 112 if (CCV(ccv, t_flags) & TF_ECN_PERMIT) { 113 /* 114 * DCTCP doesn't treat receipt of ECN marked packet as a 115 * congestion event. Thus, DCTCP always executes the ACK 116 * processing out of congestion recovery. 117 */ 118 if (IN_CONGRECOVERY(CCV(ccv, t_flags))) { 119 EXIT_CONGRECOVERY(CCV(ccv, t_flags)); 120 newreno_cc_algo.ack_received(ccv, type); 121 ENTER_CONGRECOVERY(CCV(ccv, t_flags)); 122 } else 123 newreno_cc_algo.ack_received(ccv, type); 124 125 if (type == CC_DUPACK) 126 bytes_acked = CCV(ccv, t_maxseg); 127 128 if (type == CC_ACK) 129 bytes_acked = ccv->bytes_this_ack; 130 131 /* Update total bytes. */ 132 dctcp_data->bytes_total += bytes_acked; 133 134 /* Update total marked bytes. */ 135 if (dctcp_data->ece_curr) { 136 if (!dctcp_data->ece_prev 137 && bytes_acked > CCV(ccv, t_maxseg)) { 138 dctcp_data->bytes_ecn += 139 (bytes_acked - CCV(ccv, t_maxseg)); 140 } else 141 dctcp_data->bytes_ecn += bytes_acked; 142 dctcp_data->ece_prev = 1; 143 } else { 144 if (dctcp_data->ece_prev 145 && bytes_acked > CCV(ccv, t_maxseg)) 146 dctcp_data->bytes_ecn += CCV(ccv, t_maxseg); 147 dctcp_data->ece_prev = 0; 148 } 149 dctcp_data->ece_curr = 0; 150 151 /* 152 * Update the fraction of marked bytes at the end of 153 * current window size. 154 */ 155 if ((IN_FASTRECOVERY(CCV(ccv, t_flags)) && 156 SEQ_GEQ(ccv->curack, CCV(ccv, snd_recover))) || 157 (!IN_FASTRECOVERY(CCV(ccv, t_flags)) && 158 SEQ_GT(ccv->curack, dctcp_data->save_sndnxt))) 159 dctcp_update_alpha(ccv); 160 } else 161 newreno_cc_algo.ack_received(ccv, type); 162 } 163 164 static void 165 dctcp_after_idle(struct cc_var *ccv) 166 { 167 struct dctcp *dctcp_data; 168 169 dctcp_data = ccv->cc_data; 170 171 /* Initialize internal parameters after idle time */ 172 dctcp_data->bytes_ecn = 0; 173 dctcp_data->bytes_total = 0; 174 dctcp_data->save_sndnxt = CCV(ccv, snd_nxt); 175 dctcp_data->alpha = V_dctcp_alpha; 176 dctcp_data->ece_curr = 0; 177 dctcp_data->ece_prev = 0; 178 dctcp_data->num_cong_events = 0; 179 180 dctcp_cc_algo.after_idle = newreno_cc_algo.after_idle; 181 } 182 183 static void 184 dctcp_cb_destroy(struct cc_var *ccv) 185 { 186 free(ccv->cc_data, M_dctcp); 187 } 188 189 static int 190 dctcp_cb_init(struct cc_var *ccv) 191 { 192 struct dctcp *dctcp_data; 193 194 dctcp_data = malloc(sizeof(struct dctcp), M_dctcp, M_NOWAIT|M_ZERO); 195 196 if (dctcp_data == NULL) 197 return (ENOMEM); 198 199 /* Initialize some key variables with sensible defaults. */ 200 dctcp_data->bytes_ecn = 0; 201 dctcp_data->bytes_total = 0; 202 /* 203 * When alpha is set to 0 in the beginning, DCTCP sender transfers as 204 * much data as possible until the value converges which may expand the 205 * queueing delay at the switch. When alpha is set to 1, queueing delay 206 * is kept small. 207 * Throughput-sensitive applications should have alpha = 0 208 * Latency-sensitive applications should have alpha = 1 209 * 210 * Note: DCTCP draft suggests initial alpha to be 1 but we've decided to 211 * keep it 0 as default. 212 */ 213 dctcp_data->alpha = V_dctcp_alpha; 214 dctcp_data->save_sndnxt = 0; 215 dctcp_data->ce_prev = 0; 216 dctcp_data->ece_curr = 0; 217 dctcp_data->ece_prev = 0; 218 dctcp_data->num_cong_events = 0; 219 220 ccv->cc_data = dctcp_data; 221 return (0); 222 } 223 224 /* 225 * Perform any necessary tasks before we enter congestion recovery. 226 */ 227 static void 228 dctcp_cong_signal(struct cc_var *ccv, uint32_t type) 229 { 230 struct dctcp *dctcp_data; 231 u_int win, mss; 232 233 dctcp_data = ccv->cc_data; 234 win = CCV(ccv, snd_cwnd); 235 mss = CCV(ccv, t_maxseg); 236 237 switch (type) { 238 case CC_NDUPACK: 239 if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) { 240 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) { 241 CCV(ccv, snd_ssthresh) = mss * 242 max(win / 2 / mss, 2); 243 dctcp_data->num_cong_events++; 244 } else { 245 /* cwnd has already updated as congestion 246 * recovery. Reverse cwnd value using 247 * snd_cwnd_prev and recalculate snd_ssthresh 248 */ 249 win = CCV(ccv, snd_cwnd_prev); 250 CCV(ccv, snd_ssthresh) = 251 max(win / 2 / mss, 2) * mss; 252 } 253 ENTER_RECOVERY(CCV(ccv, t_flags)); 254 } 255 break; 256 case CC_ECN: 257 /* 258 * Save current snd_cwnd when the host encounters both 259 * congestion recovery and fast recovery. 260 */ 261 CCV(ccv, snd_cwnd_prev) = win; 262 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) { 263 if (V_dctcp_slowstart && 264 dctcp_data->num_cong_events++ == 0) { 265 CCV(ccv, snd_ssthresh) = 266 mss * max(win / 2 / mss, 2); 267 dctcp_data->alpha = MAX_ALPHA_VALUE; 268 dctcp_data->bytes_ecn = 0; 269 dctcp_data->bytes_total = 0; 270 dctcp_data->save_sndnxt = CCV(ccv, snd_nxt); 271 } else 272 CCV(ccv, snd_ssthresh) = max((win - ((win * 273 dctcp_data->alpha) >> 11)) / mss, 2) * mss; 274 CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh); 275 ENTER_CONGRECOVERY(CCV(ccv, t_flags)); 276 } 277 dctcp_data->ece_curr = 1; 278 break; 279 case CC_RTO: 280 if (CCV(ccv, t_flags) & TF_ECN_PERMIT) { 281 CCV(ccv, t_flags) |= TF_ECN_SND_CWR; 282 dctcp_update_alpha(ccv); 283 dctcp_data->save_sndnxt += CCV(ccv, t_maxseg); 284 dctcp_data->num_cong_events++; 285 } 286 break; 287 } 288 } 289 290 static void 291 dctcp_conn_init(struct cc_var *ccv) 292 { 293 struct dctcp *dctcp_data; 294 295 dctcp_data = ccv->cc_data; 296 297 if (CCV(ccv, t_flags) & TF_ECN_PERMIT) 298 dctcp_data->save_sndnxt = CCV(ccv, snd_nxt); 299 } 300 301 /* 302 * Perform any necessary tasks before we exit congestion recovery. 303 */ 304 static void 305 dctcp_post_recovery(struct cc_var *ccv) 306 { 307 dctcp_cc_algo.post_recovery = newreno_cc_algo.post_recovery; 308 309 if (CCV(ccv, t_flags) & TF_ECN_PERMIT) 310 dctcp_update_alpha(ccv); 311 } 312 313 /* 314 * Execute an additional ECN processing using ECN field in IP header and the CWR 315 * bit in TCP header. 316 * 317 * delay_ack == 0 - Delayed ACK disabled 318 * delay_ack == 1 - Delayed ACK enabled 319 */ 320 321 static void 322 dctcp_ecnpkt_handler(struct cc_var *ccv) 323 { 324 struct dctcp *dctcp_data; 325 uint32_t ccflag; 326 int delay_ack; 327 328 dctcp_data = ccv->cc_data; 329 ccflag = ccv->flags; 330 delay_ack = 1; 331 332 /* 333 * DCTCP responses an ACK immediately when the CE state 334 * in between this segment and the last segment is not same. 335 */ 336 if (ccflag & CCF_IPHDR_CE) { 337 if (!dctcp_data->ce_prev && (ccflag & CCF_DELACK)) 338 delay_ack = 0; 339 dctcp_data->ce_prev = 1; 340 CCV(ccv, t_flags) |= TF_ECN_SND_ECE; 341 } else { 342 if (dctcp_data->ce_prev && (ccflag & CCF_DELACK)) 343 delay_ack = 0; 344 dctcp_data->ce_prev = 0; 345 CCV(ccv, t_flags) &= ~TF_ECN_SND_ECE; 346 } 347 348 /* DCTCP sets delayed ack when this segment sets the CWR flag. */ 349 if ((ccflag & CCF_DELACK) && (ccflag & CCF_TCPHDR_CWR)) 350 delay_ack = 1; 351 352 if (delay_ack == 0) 353 ccv->flags |= CCF_ACKNOW; 354 else 355 ccv->flags &= ~CCF_ACKNOW; 356 } 357 358 /* 359 * Update the fraction of marked bytes represented as 'alpha'. 360 * Also initialize several internal parameters at the end of this function. 361 */ 362 static void 363 dctcp_update_alpha(struct cc_var *ccv) 364 { 365 struct dctcp *dctcp_data; 366 int alpha_prev; 367 368 dctcp_data = ccv->cc_data; 369 alpha_prev = dctcp_data->alpha; 370 dctcp_data->bytes_total = max(dctcp_data->bytes_total, 1); 371 372 /* 373 * Update alpha: alpha = (1 - g) * alpha + g * M. 374 * Here: 375 * g is weight factor 376 * recommaded to be set to 1/16 377 * small g = slow convergence between competitive DCTCP flows 378 * large g = impacts low utilization of bandwidth at switches 379 * M is fraction of marked segments in last RTT 380 * updated every RTT 381 * Alpha must be round to 0 - MAX_ALPHA_VALUE. 382 */ 383 dctcp_data->alpha = ulmin(alpha_prev - (alpha_prev >> V_dctcp_shift_g) + 384 ((uint64_t)dctcp_data->bytes_ecn << (DCTCP_SHIFT - V_dctcp_shift_g)) / 385 dctcp_data->bytes_total, MAX_ALPHA_VALUE); 386 387 /* Initialize internal parameters for next alpha calculation */ 388 dctcp_data->bytes_ecn = 0; 389 dctcp_data->bytes_total = 0; 390 dctcp_data->save_sndnxt = CCV(ccv, snd_nxt); 391 } 392 393 static int 394 dctcp_alpha_handler(SYSCTL_HANDLER_ARGS) 395 { 396 uint32_t new; 397 int error; 398 399 new = V_dctcp_alpha; 400 error = sysctl_handle_int(oidp, &new, 0, req); 401 if (error == 0 && req->newptr != NULL) { 402 if (new > MAX_ALPHA_VALUE) 403 error = EINVAL; 404 else 405 V_dctcp_alpha = new; 406 } 407 408 return (error); 409 } 410 411 static int 412 dctcp_shift_g_handler(SYSCTL_HANDLER_ARGS) 413 { 414 uint32_t new; 415 int error; 416 417 new = V_dctcp_shift_g; 418 error = sysctl_handle_int(oidp, &new, 0, req); 419 if (error == 0 && req->newptr != NULL) { 420 if (new > DCTCP_SHIFT) 421 error = EINVAL; 422 else 423 V_dctcp_shift_g = new; 424 } 425 426 return (error); 427 } 428 429 static int 430 dctcp_slowstart_handler(SYSCTL_HANDLER_ARGS) 431 { 432 uint32_t new; 433 int error; 434 435 new = V_dctcp_slowstart; 436 error = sysctl_handle_int(oidp, &new, 0, req); 437 if (error == 0 && req->newptr != NULL) { 438 if (new > 1) 439 error = EINVAL; 440 else 441 V_dctcp_slowstart = new; 442 } 443 444 return (error); 445 } 446 447 SYSCTL_DECL(_net_inet_tcp_cc_dctcp); 448 SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, dctcp, CTLFLAG_RW, NULL, 449 "dctcp congestion control related settings"); 450 451 SYSCTL_PROC(_net_inet_tcp_cc_dctcp, OID_AUTO, alpha, 452 CTLFLAG_VNET|CTLTYPE_UINT|CTLFLAG_RW, &VNET_NAME(dctcp_alpha), 0, 453 &dctcp_alpha_handler, 454 "IU", "dctcp alpha parameter at start of session"); 455 456 SYSCTL_PROC(_net_inet_tcp_cc_dctcp, OID_AUTO, shift_g, 457 CTLFLAG_VNET|CTLTYPE_UINT|CTLFLAG_RW, &VNET_NAME(dctcp_shift_g), 4, 458 &dctcp_shift_g_handler, 459 "IU", "dctcp shift parameter"); 460 461 SYSCTL_PROC(_net_inet_tcp_cc_dctcp, OID_AUTO, slowstart, 462 CTLFLAG_VNET|CTLTYPE_UINT|CTLFLAG_RW, &VNET_NAME(dctcp_slowstart), 0, 463 &dctcp_slowstart_handler, 464 "IU", "half CWND reduction after the first slow start"); 465 466 DECLARE_CC_MODULE(dctcp, &dctcp_cc_algo); 467