15728a0eaSLawrence Stewart /*- 25728a0eaSLawrence Stewart * Copyright (c) 2007-2008 35728a0eaSLawrence Stewart * Swinburne University of Technology, Melbourne, Australia 45728a0eaSLawrence Stewart * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org> 55728a0eaSLawrence Stewart * Copyright (c) 2010 The FreeBSD Foundation 65728a0eaSLawrence Stewart * All rights reserved. 75728a0eaSLawrence Stewart * 85728a0eaSLawrence Stewart * This software was developed at the Centre for Advanced Internet 9891b8ed4SLawrence Stewart * Architectures, Swinburne University of Technology, by Lawrence Stewart and 10891b8ed4SLawrence Stewart * James Healy, made possible in part by a grant from the Cisco University 11891b8ed4SLawrence Stewart * Research Program Fund at Community Foundation Silicon Valley. 125728a0eaSLawrence Stewart * 135728a0eaSLawrence Stewart * Portions of this software were developed at the Centre for Advanced 145728a0eaSLawrence Stewart * Internet Architectures, Swinburne University of Technology, Melbourne, 155728a0eaSLawrence Stewart * Australia by David Hayes under sponsorship from the FreeBSD Foundation. 165728a0eaSLawrence Stewart * 175728a0eaSLawrence Stewart * Redistribution and use in source and binary forms, with or without 185728a0eaSLawrence Stewart * modification, are permitted provided that the following conditions 195728a0eaSLawrence Stewart * are met: 205728a0eaSLawrence Stewart * 1. Redistributions of source code must retain the above copyright 215728a0eaSLawrence Stewart * notice, this list of conditions and the following disclaimer. 225728a0eaSLawrence Stewart * 2. Redistributions in binary form must reproduce the above copyright 235728a0eaSLawrence Stewart * notice, this list of conditions and the following disclaimer in the 245728a0eaSLawrence Stewart * documentation and/or other materials provided with the distribution. 255728a0eaSLawrence Stewart * 265728a0eaSLawrence Stewart * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 275728a0eaSLawrence Stewart * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 285728a0eaSLawrence Stewart * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 295728a0eaSLawrence Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 305728a0eaSLawrence Stewart * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 315728a0eaSLawrence Stewart * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 325728a0eaSLawrence Stewart * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 335728a0eaSLawrence Stewart * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 345728a0eaSLawrence Stewart * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 355728a0eaSLawrence Stewart * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 365728a0eaSLawrence Stewart * SUCH DAMAGE. 375728a0eaSLawrence Stewart */ 385728a0eaSLawrence Stewart 395728a0eaSLawrence Stewart /* 405728a0eaSLawrence Stewart * An implementation of the H-TCP congestion control algorithm for FreeBSD, 415728a0eaSLawrence Stewart * based on the Internet Draft "draft-leith-tcp-htcp-06.txt" by Leith and 425728a0eaSLawrence Stewart * Shorten. Originally released as part of the NewTCP research project at 43891b8ed4SLawrence Stewart * Swinburne University of Technology's Centre for Advanced Internet 44891b8ed4SLawrence Stewart * Architectures, Melbourne, Australia, which was made possible in part by a 45891b8ed4SLawrence Stewart * grant from the Cisco University Research Program Fund at Community Foundation 46891b8ed4SLawrence Stewart * Silicon Valley. More details are available at: 475728a0eaSLawrence Stewart * http://caia.swin.edu.au/urp/newtcp/ 485728a0eaSLawrence Stewart */ 495728a0eaSLawrence Stewart 505728a0eaSLawrence Stewart #include <sys/cdefs.h> 515728a0eaSLawrence Stewart __FBSDID("$FreeBSD$"); 525728a0eaSLawrence Stewart 535728a0eaSLawrence Stewart #include <sys/param.h> 545728a0eaSLawrence Stewart #include <sys/kernel.h> 555728a0eaSLawrence Stewart #include <sys/limits.h> 565728a0eaSLawrence Stewart #include <sys/malloc.h> 575728a0eaSLawrence Stewart #include <sys/module.h> 585728a0eaSLawrence Stewart #include <sys/socket.h> 595728a0eaSLawrence Stewart #include <sys/socketvar.h> 605728a0eaSLawrence Stewart #include <sys/sysctl.h> 615728a0eaSLawrence Stewart #include <sys/systm.h> 625728a0eaSLawrence Stewart 635728a0eaSLawrence Stewart #include <net/vnet.h> 645728a0eaSLawrence Stewart 65*2de3e790SGleb Smirnoff #include <netinet/tcp.h> 665728a0eaSLawrence Stewart #include <netinet/tcp_seq.h> 675728a0eaSLawrence Stewart #include <netinet/tcp_timer.h> 685728a0eaSLawrence Stewart #include <netinet/tcp_var.h> 69*2de3e790SGleb Smirnoff #include <netinet/tcp_cc.h> 705728a0eaSLawrence Stewart #include <netinet/cc/cc_module.h> 715728a0eaSLawrence Stewart 725728a0eaSLawrence Stewart /* Fixed point math shifts. */ 735728a0eaSLawrence Stewart #define HTCP_SHIFT 8 745728a0eaSLawrence Stewart #define HTCP_ALPHA_INC_SHIFT 4 755728a0eaSLawrence Stewart 765728a0eaSLawrence Stewart #define HTCP_INIT_ALPHA 1 775728a0eaSLawrence Stewart #define HTCP_DELTA_L hz /* 1 sec in ticks. */ 785728a0eaSLawrence Stewart #define HTCP_MINBETA 128 /* 0.5 << HTCP_SHIFT. */ 795728a0eaSLawrence Stewart #define HTCP_MAXBETA 204 /* ~0.8 << HTCP_SHIFT. */ 805728a0eaSLawrence Stewart #define HTCP_MINROWE 26 /* ~0.1 << HTCP_SHIFT. */ 815728a0eaSLawrence Stewart #define HTCP_MAXROWE 512 /* 2 << HTCP_SHIFT. */ 825728a0eaSLawrence Stewart 835728a0eaSLawrence Stewart /* RTT_ref (ms) used in the calculation of alpha if RTT scaling is enabled. */ 845728a0eaSLawrence Stewart #define HTCP_RTT_REF 100 855728a0eaSLawrence Stewart 865728a0eaSLawrence Stewart /* Don't trust SRTT until this many samples have been taken. */ 875728a0eaSLawrence Stewart #define HTCP_MIN_RTT_SAMPLES 8 885728a0eaSLawrence Stewart 895728a0eaSLawrence Stewart /* 905728a0eaSLawrence Stewart * HTCP_CALC_ALPHA performs a fixed point math calculation to determine the 915728a0eaSLawrence Stewart * value of alpha, based on the function defined in the HTCP spec. 925728a0eaSLawrence Stewart * 935728a0eaSLawrence Stewart * i.e. 1 + 10(delta - delta_l) + ((delta - delta_l) / 2) ^ 2 945728a0eaSLawrence Stewart * 955728a0eaSLawrence Stewart * "diff" is passed in to the macro as "delta - delta_l" and is expected to be 965728a0eaSLawrence Stewart * in units of ticks. 975728a0eaSLawrence Stewart * 985728a0eaSLawrence Stewart * The joyousnous of fixed point maths means our function implementation looks a 995728a0eaSLawrence Stewart * little funky... 1005728a0eaSLawrence Stewart * 1015728a0eaSLawrence Stewart * In order to maintain some precision in the calculations, a fixed point shift 1025728a0eaSLawrence Stewart * HTCP_ALPHA_INC_SHIFT is used to ensure the integer divisions don't 1035728a0eaSLawrence Stewart * truncate the results too badly. 1045728a0eaSLawrence Stewart * 1055728a0eaSLawrence Stewart * The "16" value is the "1" term in the alpha function shifted up by 1065728a0eaSLawrence Stewart * HTCP_ALPHA_INC_SHIFT 1075728a0eaSLawrence Stewart * 1085728a0eaSLawrence Stewart * The "160" value is the "10" multiplier in the alpha function multiplied by 1095728a0eaSLawrence Stewart * 2^HTCP_ALPHA_INC_SHIFT 1105728a0eaSLawrence Stewart * 1115728a0eaSLawrence Stewart * Specifying these as constants reduces the computations required. After 1125728a0eaSLawrence Stewart * up-shifting all the terms in the function and performing the required 1135728a0eaSLawrence Stewart * calculations, we down-shift the final result by HTCP_ALPHA_INC_SHIFT to 1145728a0eaSLawrence Stewart * ensure it is back in the correct range. 1155728a0eaSLawrence Stewart * 1165728a0eaSLawrence Stewart * The "hz" terms are required as kernels can be configured to run with 1175728a0eaSLawrence Stewart * different tick timers, which we have to adjust for in the alpha calculation 1185728a0eaSLawrence Stewart * (which originally was defined in terms of seconds). 1195728a0eaSLawrence Stewart * 1205728a0eaSLawrence Stewart * We also have to be careful to constrain the value of diff such that it won't 1215728a0eaSLawrence Stewart * overflow whilst performing the calculation. The middle term i.e. (160 * diff) 1225728a0eaSLawrence Stewart * / hz is the limiting factor in the calculation. We must constrain diff to be 1235728a0eaSLawrence Stewart * less than the max size of an int divided by the constant 160 figure 1245728a0eaSLawrence Stewart * i.e. diff < INT_MAX / 160 1255728a0eaSLawrence Stewart * 1265728a0eaSLawrence Stewart * NB: Changing HTCP_ALPHA_INC_SHIFT will require you to MANUALLY update the 1275728a0eaSLawrence Stewart * constants used in this function! 1285728a0eaSLawrence Stewart */ 1295728a0eaSLawrence Stewart #define HTCP_CALC_ALPHA(diff) \ 1305728a0eaSLawrence Stewart ((\ 1315728a0eaSLawrence Stewart (16) + \ 1325728a0eaSLawrence Stewart ((160 * (diff)) / hz) + \ 1335728a0eaSLawrence Stewart (((diff) / hz) * (((diff) << HTCP_ALPHA_INC_SHIFT) / (4 * hz))) \ 1345728a0eaSLawrence Stewart ) >> HTCP_ALPHA_INC_SHIFT) 1355728a0eaSLawrence Stewart 1365728a0eaSLawrence Stewart static void htcp_ack_received(struct cc_var *ccv, uint16_t type); 1375728a0eaSLawrence Stewart static void htcp_cb_destroy(struct cc_var *ccv); 1385728a0eaSLawrence Stewart static int htcp_cb_init(struct cc_var *ccv); 1395728a0eaSLawrence Stewart static void htcp_cong_signal(struct cc_var *ccv, uint32_t type); 1405728a0eaSLawrence Stewart static int htcp_mod_init(void); 1415728a0eaSLawrence Stewart static void htcp_post_recovery(struct cc_var *ccv); 1425728a0eaSLawrence Stewart static void htcp_recalc_alpha(struct cc_var *ccv); 1435728a0eaSLawrence Stewart static void htcp_recalc_beta(struct cc_var *ccv); 1445728a0eaSLawrence Stewart static void htcp_record_rtt(struct cc_var *ccv); 1455728a0eaSLawrence Stewart static void htcp_ssthresh_update(struct cc_var *ccv); 1465728a0eaSLawrence Stewart 1475728a0eaSLawrence Stewart struct htcp { 1485728a0eaSLawrence Stewart /* cwnd before entering cong recovery. */ 1495728a0eaSLawrence Stewart unsigned long prev_cwnd; 1505728a0eaSLawrence Stewart /* cwnd additive increase parameter. */ 1515728a0eaSLawrence Stewart int alpha; 1525728a0eaSLawrence Stewart /* cwnd multiplicative decrease parameter. */ 1535728a0eaSLawrence Stewart int beta; 1545728a0eaSLawrence Stewart /* Largest rtt seen for the flow. */ 1555728a0eaSLawrence Stewart int maxrtt; 1565728a0eaSLawrence Stewart /* Shortest rtt seen for the flow. */ 1575728a0eaSLawrence Stewart int minrtt; 1585728a0eaSLawrence Stewart /* Time of last congestion event in ticks. */ 1595728a0eaSLawrence Stewart int t_last_cong; 1605728a0eaSLawrence Stewart }; 1615728a0eaSLawrence Stewart 1625728a0eaSLawrence Stewart static int htcp_rtt_ref; 1635728a0eaSLawrence Stewart /* 1645728a0eaSLawrence Stewart * The maximum number of ticks the value of diff can reach in 1655728a0eaSLawrence Stewart * htcp_recalc_alpha() before alpha will stop increasing due to overflow. 1665728a0eaSLawrence Stewart * See comment above HTCP_CALC_ALPHA for more info. 1675728a0eaSLawrence Stewart */ 1685728a0eaSLawrence Stewart static int htcp_max_diff = INT_MAX / ((1 << HTCP_ALPHA_INC_SHIFT) * 10); 1695728a0eaSLawrence Stewart 1705728a0eaSLawrence Stewart /* Per-netstack vars. */ 171f88910cdSMatthew D Fleming static VNET_DEFINE(u_int, htcp_adaptive_backoff) = 0; 172f88910cdSMatthew D Fleming static VNET_DEFINE(u_int, htcp_rtt_scaling) = 0; 1735728a0eaSLawrence Stewart #define V_htcp_adaptive_backoff VNET(htcp_adaptive_backoff) 1745728a0eaSLawrence Stewart #define V_htcp_rtt_scaling VNET(htcp_rtt_scaling) 1755728a0eaSLawrence Stewart 1766bed196cSSergey Kandaurov static MALLOC_DEFINE(M_HTCP, "htcp data", 1775728a0eaSLawrence Stewart "Per connection data required for the HTCP congestion control algorithm"); 1785728a0eaSLawrence Stewart 1795728a0eaSLawrence Stewart struct cc_algo htcp_cc_algo = { 1805728a0eaSLawrence Stewart .name = "htcp", 1815728a0eaSLawrence Stewart .ack_received = htcp_ack_received, 1825728a0eaSLawrence Stewart .cb_destroy = htcp_cb_destroy, 1835728a0eaSLawrence Stewart .cb_init = htcp_cb_init, 1845728a0eaSLawrence Stewart .cong_signal = htcp_cong_signal, 1855728a0eaSLawrence Stewart .mod_init = htcp_mod_init, 1865728a0eaSLawrence Stewart .post_recovery = htcp_post_recovery, 1875728a0eaSLawrence Stewart }; 1885728a0eaSLawrence Stewart 1895728a0eaSLawrence Stewart static void 1905728a0eaSLawrence Stewart htcp_ack_received(struct cc_var *ccv, uint16_t type) 1915728a0eaSLawrence Stewart { 1925728a0eaSLawrence Stewart struct htcp *htcp_data; 1935728a0eaSLawrence Stewart 1945728a0eaSLawrence Stewart htcp_data = ccv->cc_data; 1955728a0eaSLawrence Stewart htcp_record_rtt(ccv); 1965728a0eaSLawrence Stewart 1975728a0eaSLawrence Stewart /* 1985728a0eaSLawrence Stewart * Regular ACK and we're not in cong/fast recovery and we're cwnd 1995728a0eaSLawrence Stewart * limited and we're either not doing ABC or are slow starting or are 2005728a0eaSLawrence Stewart * doing ABC and we've sent a cwnd's worth of bytes. 2015728a0eaSLawrence Stewart */ 2025728a0eaSLawrence Stewart if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) && 2035728a0eaSLawrence Stewart (ccv->flags & CCF_CWND_LIMITED) && (!V_tcp_do_rfc3465 || 2045728a0eaSLawrence Stewart CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) || 2055728a0eaSLawrence Stewart (V_tcp_do_rfc3465 && ccv->flags & CCF_ABC_SENTAWND))) { 2065728a0eaSLawrence Stewart htcp_recalc_beta(ccv); 2075728a0eaSLawrence Stewart htcp_recalc_alpha(ccv); 2085728a0eaSLawrence Stewart /* 2095728a0eaSLawrence Stewart * Use the logic in NewReno ack_received() for slow start and 2105728a0eaSLawrence Stewart * for the first HTCP_DELTA_L ticks after either the flow starts 2115728a0eaSLawrence Stewart * or a congestion event (when alpha equals 1). 2125728a0eaSLawrence Stewart */ 2135728a0eaSLawrence Stewart if (htcp_data->alpha == 1 || 2145728a0eaSLawrence Stewart CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh)) 2155728a0eaSLawrence Stewart newreno_cc_algo.ack_received(ccv, type); 2165728a0eaSLawrence Stewart else { 2175728a0eaSLawrence Stewart if (V_tcp_do_rfc3465) { 2185728a0eaSLawrence Stewart /* Increment cwnd by alpha segments. */ 2195728a0eaSLawrence Stewart CCV(ccv, snd_cwnd) += htcp_data->alpha * 2205728a0eaSLawrence Stewart CCV(ccv, t_maxseg); 2215728a0eaSLawrence Stewart ccv->flags &= ~CCF_ABC_SENTAWND; 2225728a0eaSLawrence Stewart } else 2235728a0eaSLawrence Stewart /* 2245728a0eaSLawrence Stewart * Increment cwnd by alpha/cwnd segments to 2255728a0eaSLawrence Stewart * approximate an increase of alpha segments 2265728a0eaSLawrence Stewart * per RTT. 2275728a0eaSLawrence Stewart */ 2285728a0eaSLawrence Stewart CCV(ccv, snd_cwnd) += (((htcp_data->alpha << 2295728a0eaSLawrence Stewart HTCP_SHIFT) / (CCV(ccv, snd_cwnd) / 2305728a0eaSLawrence Stewart CCV(ccv, t_maxseg))) * CCV(ccv, t_maxseg)) 2315728a0eaSLawrence Stewart >> HTCP_SHIFT; 2325728a0eaSLawrence Stewart } 2335728a0eaSLawrence Stewart } 2345728a0eaSLawrence Stewart } 2355728a0eaSLawrence Stewart 2365728a0eaSLawrence Stewart static void 2375728a0eaSLawrence Stewart htcp_cb_destroy(struct cc_var *ccv) 2385728a0eaSLawrence Stewart { 2395728a0eaSLawrence Stewart 2405728a0eaSLawrence Stewart if (ccv->cc_data != NULL) 2415728a0eaSLawrence Stewart free(ccv->cc_data, M_HTCP); 2425728a0eaSLawrence Stewart } 2435728a0eaSLawrence Stewart 2445728a0eaSLawrence Stewart static int 2455728a0eaSLawrence Stewart htcp_cb_init(struct cc_var *ccv) 2465728a0eaSLawrence Stewart { 2475728a0eaSLawrence Stewart struct htcp *htcp_data; 2485728a0eaSLawrence Stewart 2495728a0eaSLawrence Stewart htcp_data = malloc(sizeof(struct htcp), M_HTCP, M_NOWAIT); 2505728a0eaSLawrence Stewart 2515728a0eaSLawrence Stewart if (htcp_data == NULL) 2525728a0eaSLawrence Stewart return (ENOMEM); 2535728a0eaSLawrence Stewart 2545728a0eaSLawrence Stewart /* Init some key variables with sensible defaults. */ 2555728a0eaSLawrence Stewart htcp_data->alpha = HTCP_INIT_ALPHA; 2565728a0eaSLawrence Stewart htcp_data->beta = HTCP_MINBETA; 2575728a0eaSLawrence Stewart htcp_data->maxrtt = TCPTV_SRTTBASE; 2585728a0eaSLawrence Stewart htcp_data->minrtt = TCPTV_SRTTBASE; 2595728a0eaSLawrence Stewart htcp_data->prev_cwnd = 0; 2605728a0eaSLawrence Stewart htcp_data->t_last_cong = ticks; 2615728a0eaSLawrence Stewart 2625728a0eaSLawrence Stewart ccv->cc_data = htcp_data; 2635728a0eaSLawrence Stewart 2645728a0eaSLawrence Stewart return (0); 2655728a0eaSLawrence Stewart } 2665728a0eaSLawrence Stewart 2675728a0eaSLawrence Stewart /* 2685728a0eaSLawrence Stewart * Perform any necessary tasks before we enter congestion recovery. 2695728a0eaSLawrence Stewart */ 2705728a0eaSLawrence Stewart static void 2715728a0eaSLawrence Stewart htcp_cong_signal(struct cc_var *ccv, uint32_t type) 2725728a0eaSLawrence Stewart { 2735728a0eaSLawrence Stewart struct htcp *htcp_data; 2745728a0eaSLawrence Stewart 2755728a0eaSLawrence Stewart htcp_data = ccv->cc_data; 2765728a0eaSLawrence Stewart 2775728a0eaSLawrence Stewart switch (type) { 2785728a0eaSLawrence Stewart case CC_NDUPACK: 2795728a0eaSLawrence Stewart if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) { 2805728a0eaSLawrence Stewart if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) { 2815728a0eaSLawrence Stewart /* 2825728a0eaSLawrence Stewart * Apply hysteresis to maxrtt to ensure 2835728a0eaSLawrence Stewart * reductions in the RTT are reflected in our 2845728a0eaSLawrence Stewart * measurements. 2855728a0eaSLawrence Stewart */ 2865728a0eaSLawrence Stewart htcp_data->maxrtt = (htcp_data->minrtt + 2875728a0eaSLawrence Stewart (htcp_data->maxrtt - htcp_data->minrtt) * 2885728a0eaSLawrence Stewart 95) / 100; 2895728a0eaSLawrence Stewart htcp_ssthresh_update(ccv); 2905728a0eaSLawrence Stewart htcp_data->t_last_cong = ticks; 2915728a0eaSLawrence Stewart htcp_data->prev_cwnd = CCV(ccv, snd_cwnd); 2925728a0eaSLawrence Stewart } 2935728a0eaSLawrence Stewart ENTER_RECOVERY(CCV(ccv, t_flags)); 2945728a0eaSLawrence Stewart } 2955728a0eaSLawrence Stewart break; 2965728a0eaSLawrence Stewart 2975728a0eaSLawrence Stewart case CC_ECN: 2985728a0eaSLawrence Stewart if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) { 2995728a0eaSLawrence Stewart /* 3005728a0eaSLawrence Stewart * Apply hysteresis to maxrtt to ensure reductions in 3015728a0eaSLawrence Stewart * the RTT are reflected in our measurements. 3025728a0eaSLawrence Stewart */ 3035728a0eaSLawrence Stewart htcp_data->maxrtt = (htcp_data->minrtt + (htcp_data->maxrtt - 3045728a0eaSLawrence Stewart htcp_data->minrtt) * 95) / 100; 3055728a0eaSLawrence Stewart htcp_ssthresh_update(ccv); 3065728a0eaSLawrence Stewart CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh); 3075728a0eaSLawrence Stewart htcp_data->t_last_cong = ticks; 3085728a0eaSLawrence Stewart htcp_data->prev_cwnd = CCV(ccv, snd_cwnd); 3095728a0eaSLawrence Stewart ENTER_CONGRECOVERY(CCV(ccv, t_flags)); 3105728a0eaSLawrence Stewart } 3115728a0eaSLawrence Stewart break; 3125728a0eaSLawrence Stewart 3135728a0eaSLawrence Stewart case CC_RTO: 3145728a0eaSLawrence Stewart /* 3155728a0eaSLawrence Stewart * Grab the current time and record it so we know when the 3165728a0eaSLawrence Stewart * most recent congestion event was. Only record it when the 3175728a0eaSLawrence Stewart * timeout has fired more than once, as there is a reasonable 3185728a0eaSLawrence Stewart * chance the first one is a false alarm and may not indicate 3195728a0eaSLawrence Stewart * congestion. 3205728a0eaSLawrence Stewart */ 3215728a0eaSLawrence Stewart if (CCV(ccv, t_rxtshift) >= 2) 3225728a0eaSLawrence Stewart htcp_data->t_last_cong = ticks; 3235728a0eaSLawrence Stewart break; 3245728a0eaSLawrence Stewart } 3255728a0eaSLawrence Stewart } 3265728a0eaSLawrence Stewart 3275728a0eaSLawrence Stewart static int 3285728a0eaSLawrence Stewart htcp_mod_init(void) 3295728a0eaSLawrence Stewart { 3305728a0eaSLawrence Stewart 3315728a0eaSLawrence Stewart htcp_cc_algo.after_idle = newreno_cc_algo.after_idle; 3325728a0eaSLawrence Stewart 3335728a0eaSLawrence Stewart /* 3345728a0eaSLawrence Stewart * HTCP_RTT_REF is defined in ms, and t_srtt in the tcpcb is stored in 3355728a0eaSLawrence Stewart * units of TCP_RTT_SCALE*hz. Scale HTCP_RTT_REF to be in the same units 3365728a0eaSLawrence Stewart * as t_srtt. 3375728a0eaSLawrence Stewart */ 3385728a0eaSLawrence Stewart htcp_rtt_ref = (HTCP_RTT_REF * TCP_RTT_SCALE * hz) / 1000; 3395728a0eaSLawrence Stewart 3405728a0eaSLawrence Stewart return (0); 3415728a0eaSLawrence Stewart } 3425728a0eaSLawrence Stewart 3435728a0eaSLawrence Stewart /* 3445728a0eaSLawrence Stewart * Perform any necessary tasks before we exit congestion recovery. 3455728a0eaSLawrence Stewart */ 3465728a0eaSLawrence Stewart static void 3475728a0eaSLawrence Stewart htcp_post_recovery(struct cc_var *ccv) 3485728a0eaSLawrence Stewart { 3495728a0eaSLawrence Stewart struct htcp *htcp_data; 3505728a0eaSLawrence Stewart 3515728a0eaSLawrence Stewart htcp_data = ccv->cc_data; 3525728a0eaSLawrence Stewart 3535728a0eaSLawrence Stewart if (IN_FASTRECOVERY(CCV(ccv, t_flags))) { 3545728a0eaSLawrence Stewart /* 3555728a0eaSLawrence Stewart * If inflight data is less than ssthresh, set cwnd 3565728a0eaSLawrence Stewart * conservatively to avoid a burst of data, as suggested in the 3575728a0eaSLawrence Stewart * NewReno RFC. Otherwise, use the HTCP method. 3585728a0eaSLawrence Stewart * 3595728a0eaSLawrence Stewart * XXXLAS: Find a way to do this without needing curack 3605728a0eaSLawrence Stewart */ 3615728a0eaSLawrence Stewart if (SEQ_GT(ccv->curack + CCV(ccv, snd_ssthresh), 3625728a0eaSLawrence Stewart CCV(ccv, snd_max))) 3635728a0eaSLawrence Stewart CCV(ccv, snd_cwnd) = CCV(ccv, snd_max) - ccv->curack + 3645728a0eaSLawrence Stewart CCV(ccv, t_maxseg); 3655728a0eaSLawrence Stewart else 3665728a0eaSLawrence Stewart CCV(ccv, snd_cwnd) = max(1, ((htcp_data->beta * 3675728a0eaSLawrence Stewart htcp_data->prev_cwnd / CCV(ccv, t_maxseg)) 3685728a0eaSLawrence Stewart >> HTCP_SHIFT)) * CCV(ccv, t_maxseg); 3695728a0eaSLawrence Stewart } 3705728a0eaSLawrence Stewart } 3715728a0eaSLawrence Stewart 3725728a0eaSLawrence Stewart static void 3735728a0eaSLawrence Stewart htcp_recalc_alpha(struct cc_var *ccv) 3745728a0eaSLawrence Stewart { 3755728a0eaSLawrence Stewart struct htcp *htcp_data; 3765728a0eaSLawrence Stewart int alpha, diff, now; 3775728a0eaSLawrence Stewart 3785728a0eaSLawrence Stewart htcp_data = ccv->cc_data; 3795728a0eaSLawrence Stewart now = ticks; 3805728a0eaSLawrence Stewart 3815728a0eaSLawrence Stewart /* 3825728a0eaSLawrence Stewart * If ticks has wrapped around (will happen approximately once every 49 3835728a0eaSLawrence Stewart * days on a machine with the default kern.hz=1000) and a flow straddles 3845728a0eaSLawrence Stewart * the wrap point, our alpha calcs will be completely wrong. We cut our 3855728a0eaSLawrence Stewart * losses and restart alpha from scratch by setting t_last_cong = now - 3865728a0eaSLawrence Stewart * HTCP_DELTA_L. 3875728a0eaSLawrence Stewart * 3885728a0eaSLawrence Stewart * This does not deflate our cwnd at all. It simply slows the rate cwnd 3895728a0eaSLawrence Stewart * is growing by until alpha regains the value it held prior to taking 3905728a0eaSLawrence Stewart * this drastic measure. 3915728a0eaSLawrence Stewart */ 3925728a0eaSLawrence Stewart if (now < htcp_data->t_last_cong) 3935728a0eaSLawrence Stewart htcp_data->t_last_cong = now - HTCP_DELTA_L; 3945728a0eaSLawrence Stewart 3955728a0eaSLawrence Stewart diff = now - htcp_data->t_last_cong - HTCP_DELTA_L; 3965728a0eaSLawrence Stewart 3975728a0eaSLawrence Stewart /* Cap alpha if the value of diff would overflow HTCP_CALC_ALPHA(). */ 3985728a0eaSLawrence Stewart if (diff < htcp_max_diff) { 3995728a0eaSLawrence Stewart /* 4005728a0eaSLawrence Stewart * If it has been more than HTCP_DELTA_L ticks since congestion, 4015728a0eaSLawrence Stewart * increase alpha according to the function defined in the spec. 4025728a0eaSLawrence Stewart */ 4035728a0eaSLawrence Stewart if (diff > 0) { 4045728a0eaSLawrence Stewart alpha = HTCP_CALC_ALPHA(diff); 4055728a0eaSLawrence Stewart 4065728a0eaSLawrence Stewart /* 4075728a0eaSLawrence Stewart * Adaptive backoff fairness adjustment: 4085728a0eaSLawrence Stewart * 2 * (1 - beta) * alpha_raw 4095728a0eaSLawrence Stewart */ 4105728a0eaSLawrence Stewart if (V_htcp_adaptive_backoff) 4115728a0eaSLawrence Stewart alpha = max(1, (2 * ((1 << HTCP_SHIFT) - 4125728a0eaSLawrence Stewart htcp_data->beta) * alpha) >> HTCP_SHIFT); 4135728a0eaSLawrence Stewart 4145728a0eaSLawrence Stewart /* 4155728a0eaSLawrence Stewart * RTT scaling: (RTT / RTT_ref) * alpha 4165728a0eaSLawrence Stewart * alpha will be the raw value from HTCP_CALC_ALPHA() if 4175728a0eaSLawrence Stewart * adaptive backoff is off, or the adjusted value if 4185728a0eaSLawrence Stewart * adaptive backoff is on. 4195728a0eaSLawrence Stewart */ 4205728a0eaSLawrence Stewart if (V_htcp_rtt_scaling) 4215728a0eaSLawrence Stewart alpha = max(1, (min(max(HTCP_MINROWE, 4225728a0eaSLawrence Stewart (CCV(ccv, t_srtt) << HTCP_SHIFT) / 4235728a0eaSLawrence Stewart htcp_rtt_ref), HTCP_MAXROWE) * alpha) 4245728a0eaSLawrence Stewart >> HTCP_SHIFT); 4255728a0eaSLawrence Stewart 4265728a0eaSLawrence Stewart } else 4275728a0eaSLawrence Stewart alpha = 1; 4285728a0eaSLawrence Stewart 4295728a0eaSLawrence Stewart htcp_data->alpha = alpha; 4305728a0eaSLawrence Stewart } 4315728a0eaSLawrence Stewart } 4325728a0eaSLawrence Stewart 4335728a0eaSLawrence Stewart static void 4345728a0eaSLawrence Stewart htcp_recalc_beta(struct cc_var *ccv) 4355728a0eaSLawrence Stewart { 4365728a0eaSLawrence Stewart struct htcp *htcp_data; 4375728a0eaSLawrence Stewart 4385728a0eaSLawrence Stewart htcp_data = ccv->cc_data; 4395728a0eaSLawrence Stewart 4405728a0eaSLawrence Stewart /* 4415728a0eaSLawrence Stewart * TCPTV_SRTTBASE is the initialised value of each connection's SRTT, so 4425728a0eaSLawrence Stewart * we only calc beta if the connection's SRTT has been changed from its 4435728a0eaSLawrence Stewart * inital value. beta is bounded to ensure it is always between 4445728a0eaSLawrence Stewart * HTCP_MINBETA and HTCP_MAXBETA. 4455728a0eaSLawrence Stewart */ 4465728a0eaSLawrence Stewart if (V_htcp_adaptive_backoff && htcp_data->minrtt != TCPTV_SRTTBASE && 4475728a0eaSLawrence Stewart htcp_data->maxrtt != TCPTV_SRTTBASE) 4485728a0eaSLawrence Stewart htcp_data->beta = min(max(HTCP_MINBETA, 4495728a0eaSLawrence Stewart (htcp_data->minrtt << HTCP_SHIFT) / htcp_data->maxrtt), 4505728a0eaSLawrence Stewart HTCP_MAXBETA); 4515728a0eaSLawrence Stewart else 4525728a0eaSLawrence Stewart htcp_data->beta = HTCP_MINBETA; 4535728a0eaSLawrence Stewart } 4545728a0eaSLawrence Stewart 4555728a0eaSLawrence Stewart /* 4565728a0eaSLawrence Stewart * Record the minimum and maximum RTT seen for the connection. These are used in 4575728a0eaSLawrence Stewart * the calculation of beta if adaptive backoff is enabled. 4585728a0eaSLawrence Stewart */ 4595728a0eaSLawrence Stewart static void 4605728a0eaSLawrence Stewart htcp_record_rtt(struct cc_var *ccv) 4615728a0eaSLawrence Stewart { 4625728a0eaSLawrence Stewart struct htcp *htcp_data; 4635728a0eaSLawrence Stewart 4645728a0eaSLawrence Stewart htcp_data = ccv->cc_data; 4655728a0eaSLawrence Stewart 4665728a0eaSLawrence Stewart /* XXXLAS: Should there be some hysteresis for minrtt? */ 4675728a0eaSLawrence Stewart 4685728a0eaSLawrence Stewart /* 4695728a0eaSLawrence Stewart * Record the current SRTT as our minrtt if it's the smallest we've seen 4705728a0eaSLawrence Stewart * or minrtt is currently equal to its initialised value. Ignore SRTT 4715728a0eaSLawrence Stewart * until a min number of samples have been taken. 4725728a0eaSLawrence Stewart */ 4735728a0eaSLawrence Stewart if ((CCV(ccv, t_srtt) < htcp_data->minrtt || 4745728a0eaSLawrence Stewart htcp_data->minrtt == TCPTV_SRTTBASE) && 4755728a0eaSLawrence Stewart (CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES)) 4765728a0eaSLawrence Stewart htcp_data->minrtt = CCV(ccv, t_srtt); 4775728a0eaSLawrence Stewart 4785728a0eaSLawrence Stewart /* 4795728a0eaSLawrence Stewart * Record the current SRTT as our maxrtt if it's the largest we've 4805728a0eaSLawrence Stewart * seen. Ignore SRTT until a min number of samples have been taken. 4815728a0eaSLawrence Stewart */ 4825728a0eaSLawrence Stewart if (CCV(ccv, t_srtt) > htcp_data->maxrtt 4835728a0eaSLawrence Stewart && CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES) 4845728a0eaSLawrence Stewart htcp_data->maxrtt = CCV(ccv, t_srtt); 4855728a0eaSLawrence Stewart } 4865728a0eaSLawrence Stewart 4875728a0eaSLawrence Stewart /* 4885728a0eaSLawrence Stewart * Update the ssthresh in the event of congestion. 4895728a0eaSLawrence Stewart */ 4905728a0eaSLawrence Stewart static void 4915728a0eaSLawrence Stewart htcp_ssthresh_update(struct cc_var *ccv) 4925728a0eaSLawrence Stewart { 4935728a0eaSLawrence Stewart struct htcp *htcp_data; 4945728a0eaSLawrence Stewart 4955728a0eaSLawrence Stewart htcp_data = ccv->cc_data; 4965728a0eaSLawrence Stewart 4975728a0eaSLawrence Stewart /* 4985728a0eaSLawrence Stewart * On the first congestion event, set ssthresh to cwnd * 0.5, on 4995728a0eaSLawrence Stewart * subsequent congestion events, set it to cwnd * beta. 5005728a0eaSLawrence Stewart */ 5015728a0eaSLawrence Stewart if (CCV(ccv, snd_ssthresh) == TCP_MAXWIN << TCP_MAX_WINSHIFT) 5025728a0eaSLawrence Stewart CCV(ccv, snd_ssthresh) = (CCV(ccv, snd_cwnd) * HTCP_MINBETA) 5035728a0eaSLawrence Stewart >> HTCP_SHIFT; 5045728a0eaSLawrence Stewart else { 5055728a0eaSLawrence Stewart htcp_recalc_beta(ccv); 5065728a0eaSLawrence Stewart CCV(ccv, snd_ssthresh) = (CCV(ccv, snd_cwnd) * htcp_data->beta) 5075728a0eaSLawrence Stewart >> HTCP_SHIFT; 5085728a0eaSLawrence Stewart } 5095728a0eaSLawrence Stewart } 5105728a0eaSLawrence Stewart 5115728a0eaSLawrence Stewart 5125728a0eaSLawrence Stewart SYSCTL_DECL(_net_inet_tcp_cc_htcp); 5135728a0eaSLawrence Stewart SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, htcp, CTLFLAG_RW, 5145728a0eaSLawrence Stewart NULL, "H-TCP related settings"); 5156df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, adaptive_backoff, 5166df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(htcp_adaptive_backoff), 0, 5176df8a710SGleb Smirnoff "enable H-TCP adaptive backoff"); 5186df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, rtt_scaling, 5196df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(htcp_rtt_scaling), 0, 5206df8a710SGleb Smirnoff "enable H-TCP RTT scaling"); 5215728a0eaSLawrence Stewart 5225728a0eaSLawrence Stewart DECLARE_CC_MODULE(htcp, &htcp_cc_algo); 523