/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2007-20084* Swinburne University of Technology, Melbourne, Australia5* Copyright (c) 2009-2010 Lawrence Stewart <[email protected]>6* Copyright (c) 2010 The FreeBSD Foundation7* All rights reserved.8*9* This software was developed at the Centre for Advanced Internet10* Architectures, Swinburne University of Technology, by Lawrence Stewart and11* James Healy, made possible in part by a grant from the Cisco University12* Research Program Fund at Community Foundation Silicon Valley.13*14* Portions of this software were developed at the Centre for Advanced15* Internet Architectures, Swinburne University of Technology, Melbourne,16* Australia by David Hayes under sponsorship from the FreeBSD Foundation.17*18* Redistribution and use in source and binary forms, with or without19* modification, are permitted provided that the following conditions20* are met:21* 1. Redistributions of source code must retain the above copyright22* notice, this list of conditions and the following disclaimer.23* 2. Redistributions in binary form must reproduce the above copyright24* notice, this list of conditions and the following disclaimer in the25* documentation and/or other materials provided with the distribution.26*27* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND28* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE29* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE30* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE31* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL32* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS33* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)34* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT35* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY36* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF37* SUCH DAMAGE.38*/3940/*41* An implementation of the H-TCP congestion control algorithm for FreeBSD,42* based on the Internet Draft "draft-leith-tcp-htcp-06.txt" by Leith and43* Shorten. Originally released as part of the NewTCP research project at44* Swinburne University of Technology's Centre for Advanced Internet45* Architectures, Melbourne, Australia, which was made possible in part by a46* grant from the Cisco University Research Program Fund at Community Foundation47* Silicon Valley. More details are available at:48* http://caia.swin.edu.au/urp/newtcp/49*/5051#include <sys/param.h>52#include <sys/kernel.h>53#include <sys/limits.h>54#include <sys/malloc.h>55#include <sys/module.h>56#include <sys/socket.h>57#include <sys/socketvar.h>58#include <sys/sysctl.h>59#include <sys/systm.h>6061#include <net/vnet.h>6263#include <net/route.h>64#include <net/route/nhop.h>6566#include <netinet/in_pcb.h>67#include <netinet/tcp.h>68#include <netinet/tcp_seq.h>69#include <netinet/tcp_timer.h>70#include <netinet/tcp_var.h>71#include <netinet/cc/cc.h>72#include <netinet/cc/cc_module.h>7374/* Fixed point math shifts. */75#define HTCP_SHIFT 876#define HTCP_ALPHA_INC_SHIFT 47778#define HTCP_INIT_ALPHA 179#define HTCP_DELTA_L hz /* 1 sec in ticks. */80#define HTCP_MINBETA 128 /* 0.5 << HTCP_SHIFT. */81#define HTCP_MAXBETA 204 /* ~0.8 << HTCP_SHIFT. */82#define HTCP_MINROWE 26 /* ~0.1 << HTCP_SHIFT. */83#define HTCP_MAXROWE 512 /* 2 << HTCP_SHIFT. */8485/* RTT_ref (ms) used in the calculation of alpha if RTT scaling is enabled. */86#define HTCP_RTT_REF 1008788/* Don't trust SRTT until this many samples have been taken. */89#define HTCP_MIN_RTT_SAMPLES 89091/*92* HTCP_CALC_ALPHA performs a fixed point math calculation to determine the93* value of alpha, based on the function defined in the HTCP spec.94*95* i.e. 1 + 10(delta - delta_l) + ((delta - delta_l) / 2) ^ 296*97* "diff" is passed in to the macro as "delta - delta_l" and is expected to be98* in units of ticks.99*100* The joyousnous of fixed point maths means our function implementation looks a101* little funky...102*103* In order to maintain some precision in the calculations, a fixed point shift104* HTCP_ALPHA_INC_SHIFT is used to ensure the integer divisions don't105* truncate the results too badly.106*107* The "16" value is the "1" term in the alpha function shifted up by108* HTCP_ALPHA_INC_SHIFT109*110* The "160" value is the "10" multiplier in the alpha function multiplied by111* 2^HTCP_ALPHA_INC_SHIFT112*113* Specifying these as constants reduces the computations required. After114* up-shifting all the terms in the function and performing the required115* calculations, we down-shift the final result by HTCP_ALPHA_INC_SHIFT to116* ensure it is back in the correct range.117*118* The "hz" terms are required as kernels can be configured to run with119* different tick timers, which we have to adjust for in the alpha calculation120* (which originally was defined in terms of seconds).121*122* We also have to be careful to constrain the value of diff such that it won't123* overflow whilst performing the calculation. The middle term i.e. (160 * diff)124* / hz is the limiting factor in the calculation. We must constrain diff to be125* less than the max size of an int divided by the constant 160 figure126* i.e. diff < INT_MAX / 160127*128* NB: Changing HTCP_ALPHA_INC_SHIFT will require you to MANUALLY update the129* constants used in this function!130*/131#define HTCP_CALC_ALPHA(diff) \132((\133(16) + \134((160 * (diff)) / hz) + \135(((diff) / hz) * (((diff) << HTCP_ALPHA_INC_SHIFT) / (4 * hz))) \136) >> HTCP_ALPHA_INC_SHIFT)137138static void htcp_ack_received(struct cc_var *ccv, ccsignal_t type);139static void htcp_cb_destroy(struct cc_var *ccv);140static int htcp_cb_init(struct cc_var *ccv, void *ptr);141static void htcp_cong_signal(struct cc_var *ccv, ccsignal_t type);142static int htcp_mod_init(void);143static void htcp_post_recovery(struct cc_var *ccv);144static void htcp_recalc_alpha(struct cc_var *ccv);145static void htcp_recalc_beta(struct cc_var *ccv);146static void htcp_record_rtt(struct cc_var *ccv);147static void htcp_ssthresh_update(struct cc_var *ccv);148static size_t htcp_data_sz(void);149150struct htcp {151/* cwnd before entering cong recovery. */152unsigned long prev_cwnd;153/* cwnd additive increase parameter. */154int alpha;155/* cwnd multiplicative decrease parameter. */156int beta;157/* Largest rtt seen for the flow. */158int maxrtt;159/* Shortest rtt seen for the flow. */160int minrtt;161/* Time of last congestion event in ticks. */162int t_last_cong;163};164165static int htcp_rtt_ref;166/*167* The maximum number of ticks the value of diff can reach in168* htcp_recalc_alpha() before alpha will stop increasing due to overflow.169* See comment above HTCP_CALC_ALPHA for more info.170*/171static int htcp_max_diff = INT_MAX / ((1 << HTCP_ALPHA_INC_SHIFT) * 10);172173/* Per-netstack vars. */174VNET_DEFINE_STATIC(u_int, htcp_adaptive_backoff) = 0;175VNET_DEFINE_STATIC(u_int, htcp_rtt_scaling) = 0;176#define V_htcp_adaptive_backoff VNET(htcp_adaptive_backoff)177#define V_htcp_rtt_scaling VNET(htcp_rtt_scaling)178179struct cc_algo htcp_cc_algo = {180.name = "htcp",181.ack_received = htcp_ack_received,182.cb_destroy = htcp_cb_destroy,183.cb_init = htcp_cb_init,184.cong_signal = htcp_cong_signal,185.mod_init = htcp_mod_init,186.post_recovery = htcp_post_recovery,187.cc_data_sz = htcp_data_sz,188.after_idle = newreno_cc_after_idle,189};190191static void192htcp_ack_received(struct cc_var *ccv, ccsignal_t type)193{194struct htcp *htcp_data;195uint32_t mss = tcp_fixed_maxseg(ccv->tp);196197htcp_data = ccv->cc_data;198htcp_record_rtt(ccv);199200/*201* Regular ACK and we're not in cong/fast recovery and we're cwnd202* limited and we're either not doing ABC or are slow starting or are203* doing ABC and we've sent a cwnd's worth of bytes.204*/205if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&206(ccv->flags & CCF_CWND_LIMITED) && (!V_tcp_do_rfc3465 ||207CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||208(V_tcp_do_rfc3465 && ccv->flags & CCF_ABC_SENTAWND))) {209htcp_recalc_beta(ccv);210htcp_recalc_alpha(ccv);211/*212* Use the logic in NewReno ack_received() for slow start and213* for the first HTCP_DELTA_L ticks after either the flow starts214* or a congestion event (when alpha equals 1).215*/216if (htcp_data->alpha == 1 ||217CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh))218newreno_cc_ack_received(ccv, type);219else {220if (V_tcp_do_rfc3465) {221/* Increment cwnd by alpha segments. */222CCV(ccv, snd_cwnd) += htcp_data->alpha *223mss;224ccv->flags &= ~CCF_ABC_SENTAWND;225} else226/*227* Increment cwnd by alpha/cwnd segments to228* approximate an increase of alpha segments229* per RTT.230*/231CCV(ccv, snd_cwnd) += (((htcp_data->alpha <<232HTCP_SHIFT) / (max(1,233CCV(ccv, snd_cwnd) / mss))) *234mss) >> HTCP_SHIFT;235}236}237}238239static void240htcp_cb_destroy(struct cc_var *ccv)241{242free(ccv->cc_data, M_CC_MEM);243}244245static size_t246htcp_data_sz(void)247{248return(sizeof(struct htcp));249}250251static int252htcp_cb_init(struct cc_var *ccv, void *ptr)253{254struct htcp *htcp_data;255256INP_WLOCK_ASSERT(tptoinpcb(ccv->tp));257if (ptr == NULL) {258htcp_data = malloc(sizeof(struct htcp), M_CC_MEM, M_NOWAIT);259if (htcp_data == NULL)260return (ENOMEM);261} else262htcp_data = ptr;263264/* Init some key variables with sensible defaults. */265htcp_data->alpha = HTCP_INIT_ALPHA;266htcp_data->beta = HTCP_MINBETA;267htcp_data->maxrtt = TCPTV_SRTTBASE;268htcp_data->minrtt = TCPTV_SRTTBASE;269htcp_data->prev_cwnd = 0;270htcp_data->t_last_cong = ticks;271272ccv->cc_data = htcp_data;273274return (0);275}276277/*278* Perform any necessary tasks before we enter congestion recovery.279*/280static void281htcp_cong_signal(struct cc_var *ccv, ccsignal_t type)282{283struct htcp *htcp_data;284uint32_t mss, pipe;285286htcp_data = ccv->cc_data;287mss = tcp_fixed_maxseg(ccv->tp);288289switch (type) {290case CC_NDUPACK:291if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {292if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {293/*294* Apply hysteresis to maxrtt to ensure295* reductions in the RTT are reflected in our296* measurements.297*/298htcp_data->maxrtt = (htcp_data->minrtt +299(htcp_data->maxrtt - htcp_data->minrtt) *30095) / 100;301htcp_ssthresh_update(ccv);302htcp_data->t_last_cong = ticks;303htcp_data->prev_cwnd = CCV(ccv, snd_cwnd);304}305ENTER_RECOVERY(CCV(ccv, t_flags));306}307break;308309case CC_ECN:310if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {311/*312* Apply hysteresis to maxrtt to ensure reductions in313* the RTT are reflected in our measurements.314*/315htcp_data->maxrtt = (htcp_data->minrtt + (htcp_data->maxrtt -316htcp_data->minrtt) * 95) / 100;317htcp_ssthresh_update(ccv);318CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);319htcp_data->t_last_cong = ticks;320htcp_data->prev_cwnd = CCV(ccv, snd_cwnd);321ENTER_CONGRECOVERY(CCV(ccv, t_flags));322}323break;324325case CC_RTO:326if (CCV(ccv, t_rxtshift) == 1) {327pipe = tcp_compute_pipe(ccv->tp);328CCV(ccv, snd_ssthresh) = max(2,329min(CCV(ccv, snd_wnd), pipe) / 2 / mss) * mss;330}331CCV(ccv, snd_cwnd) = mss;332/*333* Grab the current time and record it so we know when the334* most recent congestion event was. Only record it when the335* timeout has fired more than once, as there is a reasonable336* chance the first one is a false alarm and may not indicate337* congestion.338*/339if (CCV(ccv, t_rxtshift) >= 2)340htcp_data->t_last_cong = ticks;341break;342default:343break;344}345}346347static int348htcp_mod_init(void)349{350/*351* HTCP_RTT_REF is defined in ms, and t_srtt in the tcpcb is stored in352* units of TCP_RTT_SCALE*hz. Scale HTCP_RTT_REF to be in the same units353* as t_srtt.354*/355htcp_rtt_ref = (HTCP_RTT_REF * TCP_RTT_SCALE * hz) / 1000;356return (0);357}358359/*360* Perform any necessary tasks before we exit congestion recovery.361*/362static void363htcp_post_recovery(struct cc_var *ccv)364{365int pipe;366struct htcp *htcp_data;367uint32_t mss = tcp_fixed_maxseg(ccv->tp);368369pipe = 0;370htcp_data = ccv->cc_data;371372if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {373/*374* If inflight data is less than ssthresh, set cwnd375* conservatively to avoid a burst of data, as suggested in the376* NewReno RFC. Otherwise, use the HTCP method.377*/378pipe = tcp_compute_pipe(ccv->tp);379if (pipe < CCV(ccv, snd_ssthresh))380/*381* Ensure that cwnd down not collape to 1 MSS under382* adverse conditions. Implements RFC6582383*/384CCV(ccv, snd_cwnd) = max(pipe, mss) + mss;385else386CCV(ccv, snd_cwnd) = max(1, ((htcp_data->beta *387htcp_data->prev_cwnd / mss)388>> HTCP_SHIFT)) * mss;389}390}391392static void393htcp_recalc_alpha(struct cc_var *ccv)394{395struct htcp *htcp_data;396int alpha, diff, now;397398htcp_data = ccv->cc_data;399now = ticks;400401/*402* If ticks has wrapped around (will happen approximately once every 49403* days on a machine with the default kern.hz=1000) and a flow straddles404* the wrap point, our alpha calcs will be completely wrong. We cut our405* losses and restart alpha from scratch by setting t_last_cong = now -406* HTCP_DELTA_L.407*408* This does not deflate our cwnd at all. It simply slows the rate cwnd409* is growing by until alpha regains the value it held prior to taking410* this drastic measure.411*/412if (now < htcp_data->t_last_cong)413htcp_data->t_last_cong = now - HTCP_DELTA_L;414415diff = now - htcp_data->t_last_cong - HTCP_DELTA_L;416417/* Cap alpha if the value of diff would overflow HTCP_CALC_ALPHA(). */418if (diff < htcp_max_diff) {419/*420* If it has been more than HTCP_DELTA_L ticks since congestion,421* increase alpha according to the function defined in the spec.422*/423if (diff > 0) {424alpha = HTCP_CALC_ALPHA(diff);425426/*427* Adaptive backoff fairness adjustment:428* 2 * (1 - beta) * alpha_raw429*/430if (V_htcp_adaptive_backoff)431alpha = max(1, (2 * ((1 << HTCP_SHIFT) -432htcp_data->beta) * alpha) >> HTCP_SHIFT);433434/*435* RTT scaling: (RTT / RTT_ref) * alpha436* alpha will be the raw value from HTCP_CALC_ALPHA() if437* adaptive backoff is off, or the adjusted value if438* adaptive backoff is on.439*/440if (V_htcp_rtt_scaling)441alpha = max(1, (min(max(HTCP_MINROWE,442(tcp_get_srtt(ccv->tp, TCP_TMR_GRANULARITY_TICKS) << HTCP_SHIFT) /443htcp_rtt_ref), HTCP_MAXROWE) * alpha)444>> HTCP_SHIFT);445446} else447alpha = 1;448449htcp_data->alpha = alpha;450}451}452453static void454htcp_recalc_beta(struct cc_var *ccv)455{456struct htcp *htcp_data;457458htcp_data = ccv->cc_data;459460/*461* TCPTV_SRTTBASE is the initialised value of each connection's SRTT, so462* we only calc beta if the connection's SRTT has been changed from its463* initial value. beta is bounded to ensure it is always between464* HTCP_MINBETA and HTCP_MAXBETA.465*/466if (V_htcp_adaptive_backoff && htcp_data->minrtt != TCPTV_SRTTBASE &&467htcp_data->maxrtt != TCPTV_SRTTBASE)468htcp_data->beta = min(max(HTCP_MINBETA,469(htcp_data->minrtt << HTCP_SHIFT) / htcp_data->maxrtt),470HTCP_MAXBETA);471else472htcp_data->beta = HTCP_MINBETA;473}474475/*476* Record the minimum and maximum RTT seen for the connection. These are used in477* the calculation of beta if adaptive backoff is enabled.478*/479static void480htcp_record_rtt(struct cc_var *ccv)481{482struct htcp *htcp_data;483484htcp_data = ccv->cc_data;485486/* XXXLAS: Should there be some hysteresis for minrtt? */487488/*489* Record the current SRTT as our minrtt if it's the smallest we've seen490* or minrtt is currently equal to its initialised value. Ignore SRTT491* until a min number of samples have been taken.492*/493if ((tcp_get_srtt(ccv->tp, TCP_TMR_GRANULARITY_TICKS) < htcp_data->minrtt ||494htcp_data->minrtt == TCPTV_SRTTBASE) &&495(CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES))496htcp_data->minrtt = tcp_get_srtt(ccv->tp, TCP_TMR_GRANULARITY_TICKS);497498/*499* Record the current SRTT as our maxrtt if it's the largest we've500* seen. Ignore SRTT until a min number of samples have been taken.501*/502if (tcp_get_srtt(ccv->tp, TCP_TMR_GRANULARITY_TICKS) > htcp_data->maxrtt503&& CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES)504htcp_data->maxrtt = tcp_get_srtt(ccv->tp, TCP_TMR_GRANULARITY_TICKS);505}506507/*508* Update the ssthresh in the event of congestion.509*/510static void511htcp_ssthresh_update(struct cc_var *ccv)512{513struct htcp *htcp_data;514515htcp_data = ccv->cc_data;516517/*518* On the first congestion event, set ssthresh to cwnd * 0.5, on519* subsequent congestion events, set it to cwnd * beta.520*/521if (CCV(ccv, snd_ssthresh) == TCP_MAXWIN << TCP_MAX_WINSHIFT)522CCV(ccv, snd_ssthresh) = ((u_long)CCV(ccv, snd_cwnd) *523HTCP_MINBETA) >> HTCP_SHIFT;524else {525htcp_recalc_beta(ccv);526CCV(ccv, snd_ssthresh) = ((u_long)CCV(ccv, snd_cwnd) *527htcp_data->beta) >> HTCP_SHIFT;528}529}530531SYSCTL_DECL(_net_inet_tcp_cc_htcp);532SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, htcp, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,533"H-TCP related settings");534SYSCTL_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, adaptive_backoff,535CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(htcp_adaptive_backoff), 0,536"enable H-TCP adaptive backoff");537SYSCTL_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, rtt_scaling,538CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(htcp_rtt_scaling), 0,539"enable H-TCP RTT scaling");540541DECLARE_CC_MODULE(htcp, &htcp_cc_algo);542MODULE_VERSION(htcp, 2);543544545