15728a0eaSLawrence Stewart /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni *
45728a0eaSLawrence Stewart * Copyright (c) 2007-2008
55728a0eaSLawrence Stewart * Swinburne University of Technology, Melbourne, Australia
65728a0eaSLawrence Stewart * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
75728a0eaSLawrence Stewart * Copyright (c) 2010 The FreeBSD Foundation
85728a0eaSLawrence Stewart * All rights reserved.
95728a0eaSLawrence Stewart *
105728a0eaSLawrence Stewart * This software was developed at the Centre for Advanced Internet
11891b8ed4SLawrence Stewart * Architectures, Swinburne University of Technology, by Lawrence Stewart and
12891b8ed4SLawrence Stewart * James Healy, made possible in part by a grant from the Cisco University
13891b8ed4SLawrence Stewart * Research Program Fund at Community Foundation Silicon Valley.
145728a0eaSLawrence Stewart *
155728a0eaSLawrence Stewart * Portions of this software were developed at the Centre for Advanced
165728a0eaSLawrence Stewart * Internet Architectures, Swinburne University of Technology, Melbourne,
175728a0eaSLawrence Stewart * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
185728a0eaSLawrence Stewart *
195728a0eaSLawrence Stewart * Redistribution and use in source and binary forms, with or without
205728a0eaSLawrence Stewart * modification, are permitted provided that the following conditions
215728a0eaSLawrence Stewart * are met:
225728a0eaSLawrence Stewart * 1. Redistributions of source code must retain the above copyright
235728a0eaSLawrence Stewart * notice, this list of conditions and the following disclaimer.
245728a0eaSLawrence Stewart * 2. Redistributions in binary form must reproduce the above copyright
255728a0eaSLawrence Stewart * notice, this list of conditions and the following disclaimer in the
265728a0eaSLawrence Stewart * documentation and/or other materials provided with the distribution.
275728a0eaSLawrence Stewart *
285728a0eaSLawrence Stewart * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
295728a0eaSLawrence Stewart * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
305728a0eaSLawrence Stewart * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
315728a0eaSLawrence Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
325728a0eaSLawrence Stewart * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
335728a0eaSLawrence Stewart * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
345728a0eaSLawrence Stewart * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
355728a0eaSLawrence Stewart * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
365728a0eaSLawrence Stewart * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
375728a0eaSLawrence Stewart * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
385728a0eaSLawrence Stewart * SUCH DAMAGE.
395728a0eaSLawrence Stewart */
405728a0eaSLawrence Stewart
415728a0eaSLawrence Stewart /*
425728a0eaSLawrence Stewart * An implementation of the H-TCP congestion control algorithm for FreeBSD,
435728a0eaSLawrence Stewart * based on the Internet Draft "draft-leith-tcp-htcp-06.txt" by Leith and
445728a0eaSLawrence Stewart * Shorten. Originally released as part of the NewTCP research project at
45891b8ed4SLawrence Stewart * Swinburne University of Technology's Centre for Advanced Internet
46891b8ed4SLawrence Stewart * Architectures, Melbourne, Australia, which was made possible in part by a
47891b8ed4SLawrence Stewart * grant from the Cisco University Research Program Fund at Community Foundation
48891b8ed4SLawrence Stewart * Silicon Valley. More details are available at:
495728a0eaSLawrence Stewart * http://caia.swin.edu.au/urp/newtcp/
505728a0eaSLawrence Stewart */
515728a0eaSLawrence Stewart
525728a0eaSLawrence Stewart #include <sys/param.h>
535728a0eaSLawrence Stewart #include <sys/kernel.h>
545728a0eaSLawrence Stewart #include <sys/limits.h>
555728a0eaSLawrence Stewart #include <sys/malloc.h>
565728a0eaSLawrence Stewart #include <sys/module.h>
575728a0eaSLawrence Stewart #include <sys/socket.h>
585728a0eaSLawrence Stewart #include <sys/socketvar.h>
595728a0eaSLawrence Stewart #include <sys/sysctl.h>
605728a0eaSLawrence Stewart #include <sys/systm.h>
615728a0eaSLawrence Stewart
625728a0eaSLawrence Stewart #include <net/vnet.h>
635728a0eaSLawrence Stewart
64b8d60729SRandall Stewart #include <net/route.h>
65b8d60729SRandall Stewart #include <net/route/nhop.h>
66b8d60729SRandall Stewart
67b8d60729SRandall Stewart #include <netinet/in_pcb.h>
682de3e790SGleb Smirnoff #include <netinet/tcp.h>
695728a0eaSLawrence Stewart #include <netinet/tcp_seq.h>
705728a0eaSLawrence Stewart #include <netinet/tcp_timer.h>
715728a0eaSLawrence Stewart #include <netinet/tcp_var.h>
724644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
735728a0eaSLawrence Stewart #include <netinet/cc/cc_module.h>
745728a0eaSLawrence Stewart
755728a0eaSLawrence Stewart /* Fixed point math shifts. */
765728a0eaSLawrence Stewart #define HTCP_SHIFT 8
775728a0eaSLawrence Stewart #define HTCP_ALPHA_INC_SHIFT 4
785728a0eaSLawrence Stewart
795728a0eaSLawrence Stewart #define HTCP_INIT_ALPHA 1
805728a0eaSLawrence Stewart #define HTCP_DELTA_L hz /* 1 sec in ticks. */
815728a0eaSLawrence Stewart #define HTCP_MINBETA 128 /* 0.5 << HTCP_SHIFT. */
825728a0eaSLawrence Stewart #define HTCP_MAXBETA 204 /* ~0.8 << HTCP_SHIFT. */
835728a0eaSLawrence Stewart #define HTCP_MINROWE 26 /* ~0.1 << HTCP_SHIFT. */
845728a0eaSLawrence Stewart #define HTCP_MAXROWE 512 /* 2 << HTCP_SHIFT. */
855728a0eaSLawrence Stewart
865728a0eaSLawrence Stewart /* RTT_ref (ms) used in the calculation of alpha if RTT scaling is enabled. */
875728a0eaSLawrence Stewart #define HTCP_RTT_REF 100
885728a0eaSLawrence Stewart
895728a0eaSLawrence Stewart /* Don't trust SRTT until this many samples have been taken. */
905728a0eaSLawrence Stewart #define HTCP_MIN_RTT_SAMPLES 8
915728a0eaSLawrence Stewart
925728a0eaSLawrence Stewart /*
935728a0eaSLawrence Stewart * HTCP_CALC_ALPHA performs a fixed point math calculation to determine the
945728a0eaSLawrence Stewart * value of alpha, based on the function defined in the HTCP spec.
955728a0eaSLawrence Stewart *
965728a0eaSLawrence Stewart * i.e. 1 + 10(delta - delta_l) + ((delta - delta_l) / 2) ^ 2
975728a0eaSLawrence Stewart *
985728a0eaSLawrence Stewart * "diff" is passed in to the macro as "delta - delta_l" and is expected to be
995728a0eaSLawrence Stewart * in units of ticks.
1005728a0eaSLawrence Stewart *
1015728a0eaSLawrence Stewart * The joyousnous of fixed point maths means our function implementation looks a
1025728a0eaSLawrence Stewart * little funky...
1035728a0eaSLawrence Stewart *
1045728a0eaSLawrence Stewart * In order to maintain some precision in the calculations, a fixed point shift
1055728a0eaSLawrence Stewart * HTCP_ALPHA_INC_SHIFT is used to ensure the integer divisions don't
1065728a0eaSLawrence Stewart * truncate the results too badly.
1075728a0eaSLawrence Stewart *
1085728a0eaSLawrence Stewart * The "16" value is the "1" term in the alpha function shifted up by
1095728a0eaSLawrence Stewart * HTCP_ALPHA_INC_SHIFT
1105728a0eaSLawrence Stewart *
1115728a0eaSLawrence Stewart * The "160" value is the "10" multiplier in the alpha function multiplied by
1125728a0eaSLawrence Stewart * 2^HTCP_ALPHA_INC_SHIFT
1135728a0eaSLawrence Stewart *
1145728a0eaSLawrence Stewart * Specifying these as constants reduces the computations required. After
1155728a0eaSLawrence Stewart * up-shifting all the terms in the function and performing the required
1165728a0eaSLawrence Stewart * calculations, we down-shift the final result by HTCP_ALPHA_INC_SHIFT to
1175728a0eaSLawrence Stewart * ensure it is back in the correct range.
1185728a0eaSLawrence Stewart *
1195728a0eaSLawrence Stewart * The "hz" terms are required as kernels can be configured to run with
1205728a0eaSLawrence Stewart * different tick timers, which we have to adjust for in the alpha calculation
1215728a0eaSLawrence Stewart * (which originally was defined in terms of seconds).
1225728a0eaSLawrence Stewart *
1235728a0eaSLawrence Stewart * We also have to be careful to constrain the value of diff such that it won't
1245728a0eaSLawrence Stewart * overflow whilst performing the calculation. The middle term i.e. (160 * diff)
1255728a0eaSLawrence Stewart * / hz is the limiting factor in the calculation. We must constrain diff to be
1265728a0eaSLawrence Stewart * less than the max size of an int divided by the constant 160 figure
1275728a0eaSLawrence Stewart * i.e. diff < INT_MAX / 160
1285728a0eaSLawrence Stewart *
1295728a0eaSLawrence Stewart * NB: Changing HTCP_ALPHA_INC_SHIFT will require you to MANUALLY update the
1305728a0eaSLawrence Stewart * constants used in this function!
1315728a0eaSLawrence Stewart */
1325728a0eaSLawrence Stewart #define HTCP_CALC_ALPHA(diff) \
1335728a0eaSLawrence Stewart ((\
1345728a0eaSLawrence Stewart (16) + \
1355728a0eaSLawrence Stewart ((160 * (diff)) / hz) + \
1365728a0eaSLawrence Stewart (((diff) / hz) * (((diff) << HTCP_ALPHA_INC_SHIFT) / (4 * hz))) \
1375728a0eaSLawrence Stewart ) >> HTCP_ALPHA_INC_SHIFT)
1385728a0eaSLawrence Stewart
139f74352fbSRichard Scheffenegger static void htcp_ack_received(struct cc_var *ccv, ccsignal_t type);
1405728a0eaSLawrence Stewart static void htcp_cb_destroy(struct cc_var *ccv);
141b8d60729SRandall Stewart static int htcp_cb_init(struct cc_var *ccv, void *ptr);
142f74352fbSRichard Scheffenegger static void htcp_cong_signal(struct cc_var *ccv, ccsignal_t type);
1435728a0eaSLawrence Stewart static int htcp_mod_init(void);
1445728a0eaSLawrence Stewart static void htcp_post_recovery(struct cc_var *ccv);
1455728a0eaSLawrence Stewart static void htcp_recalc_alpha(struct cc_var *ccv);
1465728a0eaSLawrence Stewart static void htcp_recalc_beta(struct cc_var *ccv);
1475728a0eaSLawrence Stewart static void htcp_record_rtt(struct cc_var *ccv);
1485728a0eaSLawrence Stewart static void htcp_ssthresh_update(struct cc_var *ccv);
149b8d60729SRandall Stewart static size_t htcp_data_sz(void);
1505728a0eaSLawrence Stewart
1515728a0eaSLawrence Stewart struct htcp {
1525728a0eaSLawrence Stewart /* cwnd before entering cong recovery. */
1535728a0eaSLawrence Stewart unsigned long prev_cwnd;
1545728a0eaSLawrence Stewart /* cwnd additive increase parameter. */
1555728a0eaSLawrence Stewart int alpha;
1565728a0eaSLawrence Stewart /* cwnd multiplicative decrease parameter. */
1575728a0eaSLawrence Stewart int beta;
1585728a0eaSLawrence Stewart /* Largest rtt seen for the flow. */
1595728a0eaSLawrence Stewart int maxrtt;
1605728a0eaSLawrence Stewart /* Shortest rtt seen for the flow. */
1615728a0eaSLawrence Stewart int minrtt;
1625728a0eaSLawrence Stewart /* Time of last congestion event in ticks. */
1635728a0eaSLawrence Stewart int t_last_cong;
1645728a0eaSLawrence Stewart };
1655728a0eaSLawrence Stewart
1665728a0eaSLawrence Stewart static int htcp_rtt_ref;
1675728a0eaSLawrence Stewart /*
1685728a0eaSLawrence Stewart * The maximum number of ticks the value of diff can reach in
1695728a0eaSLawrence Stewart * htcp_recalc_alpha() before alpha will stop increasing due to overflow.
1705728a0eaSLawrence Stewart * See comment above HTCP_CALC_ALPHA for more info.
1715728a0eaSLawrence Stewart */
1725728a0eaSLawrence Stewart static int htcp_max_diff = INT_MAX / ((1 << HTCP_ALPHA_INC_SHIFT) * 10);
1735728a0eaSLawrence Stewart
1745728a0eaSLawrence Stewart /* Per-netstack vars. */
1755f901c92SAndrew Turner VNET_DEFINE_STATIC(u_int, htcp_adaptive_backoff) = 0;
1765f901c92SAndrew Turner VNET_DEFINE_STATIC(u_int, htcp_rtt_scaling) = 0;
1775728a0eaSLawrence Stewart #define V_htcp_adaptive_backoff VNET(htcp_adaptive_backoff)
1785728a0eaSLawrence Stewart #define V_htcp_rtt_scaling VNET(htcp_rtt_scaling)
1795728a0eaSLawrence Stewart
1805728a0eaSLawrence Stewart struct cc_algo htcp_cc_algo = {
1815728a0eaSLawrence Stewart .name = "htcp",
1825728a0eaSLawrence Stewart .ack_received = htcp_ack_received,
1835728a0eaSLawrence Stewart .cb_destroy = htcp_cb_destroy,
1845728a0eaSLawrence Stewart .cb_init = htcp_cb_init,
1855728a0eaSLawrence Stewart .cong_signal = htcp_cong_signal,
1865728a0eaSLawrence Stewart .mod_init = htcp_mod_init,
1875728a0eaSLawrence Stewart .post_recovery = htcp_post_recovery,
188b8d60729SRandall Stewart .cc_data_sz = htcp_data_sz,
189b8d60729SRandall Stewart .after_idle = newreno_cc_after_idle,
1905728a0eaSLawrence Stewart };
1915728a0eaSLawrence Stewart
1925728a0eaSLawrence Stewart static void
htcp_ack_received(struct cc_var * ccv,ccsignal_t type)193f74352fbSRichard Scheffenegger htcp_ack_received(struct cc_var *ccv, ccsignal_t type)
1945728a0eaSLawrence Stewart {
1955728a0eaSLawrence Stewart struct htcp *htcp_data;
196*22dcc812SRichard Scheffenegger uint32_t mss = tcp_fixed_maxseg(ccv->tp);
1975728a0eaSLawrence Stewart
1985728a0eaSLawrence Stewart htcp_data = ccv->cc_data;
1995728a0eaSLawrence Stewart htcp_record_rtt(ccv);
2005728a0eaSLawrence Stewart
2015728a0eaSLawrence Stewart /*
2025728a0eaSLawrence Stewart * Regular ACK and we're not in cong/fast recovery and we're cwnd
2035728a0eaSLawrence Stewart * limited and we're either not doing ABC or are slow starting or are
2045728a0eaSLawrence Stewart * doing ABC and we've sent a cwnd's worth of bytes.
2055728a0eaSLawrence Stewart */
2065728a0eaSLawrence Stewart if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
2075728a0eaSLawrence Stewart (ccv->flags & CCF_CWND_LIMITED) && (!V_tcp_do_rfc3465 ||
2085728a0eaSLawrence Stewart CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||
2095728a0eaSLawrence Stewart (V_tcp_do_rfc3465 && ccv->flags & CCF_ABC_SENTAWND))) {
2105728a0eaSLawrence Stewart htcp_recalc_beta(ccv);
2115728a0eaSLawrence Stewart htcp_recalc_alpha(ccv);
2125728a0eaSLawrence Stewart /*
2135728a0eaSLawrence Stewart * Use the logic in NewReno ack_received() for slow start and
2145728a0eaSLawrence Stewart * for the first HTCP_DELTA_L ticks after either the flow starts
2155728a0eaSLawrence Stewart * or a congestion event (when alpha equals 1).
2165728a0eaSLawrence Stewart */
2175728a0eaSLawrence Stewart if (htcp_data->alpha == 1 ||
2185728a0eaSLawrence Stewart CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh))
219b8d60729SRandall Stewart newreno_cc_ack_received(ccv, type);
2205728a0eaSLawrence Stewart else {
2215728a0eaSLawrence Stewart if (V_tcp_do_rfc3465) {
2225728a0eaSLawrence Stewart /* Increment cwnd by alpha segments. */
2235728a0eaSLawrence Stewart CCV(ccv, snd_cwnd) += htcp_data->alpha *
224*22dcc812SRichard Scheffenegger mss;
2255728a0eaSLawrence Stewart ccv->flags &= ~CCF_ABC_SENTAWND;
2265728a0eaSLawrence Stewart } else
2275728a0eaSLawrence Stewart /*
2285728a0eaSLawrence Stewart * Increment cwnd by alpha/cwnd segments to
2295728a0eaSLawrence Stewart * approximate an increase of alpha segments
2305728a0eaSLawrence Stewart * per RTT.
2315728a0eaSLawrence Stewart */
2325728a0eaSLawrence Stewart CCV(ccv, snd_cwnd) += (((htcp_data->alpha <<
23338983d40SRichard Scheffenegger HTCP_SHIFT) / (max(1,
234*22dcc812SRichard Scheffenegger CCV(ccv, snd_cwnd) / mss))) *
235*22dcc812SRichard Scheffenegger mss) >> HTCP_SHIFT;
2365728a0eaSLawrence Stewart }
2375728a0eaSLawrence Stewart }
2385728a0eaSLawrence Stewart }
2395728a0eaSLawrence Stewart
2405728a0eaSLawrence Stewart static void
htcp_cb_destroy(struct cc_var * ccv)2415728a0eaSLawrence Stewart htcp_cb_destroy(struct cc_var *ccv)
2425728a0eaSLawrence Stewart {
243b8d60729SRandall Stewart free(ccv->cc_data, M_CC_MEM);
244b8d60729SRandall Stewart }
245b8d60729SRandall Stewart
246b8d60729SRandall Stewart static size_t
htcp_data_sz(void)247b8d60729SRandall Stewart htcp_data_sz(void)
248b8d60729SRandall Stewart {
249b8d60729SRandall Stewart return(sizeof(struct htcp));
2505728a0eaSLawrence Stewart }
2515728a0eaSLawrence Stewart
2525728a0eaSLawrence Stewart static int
htcp_cb_init(struct cc_var * ccv,void * ptr)253b8d60729SRandall Stewart htcp_cb_init(struct cc_var *ccv, void *ptr)
2545728a0eaSLawrence Stewart {
2555728a0eaSLawrence Stewart struct htcp *htcp_data;
2565728a0eaSLawrence Stewart
25700d3b744SMichael Tuexen INP_WLOCK_ASSERT(tptoinpcb(ccv->tp));
258b8d60729SRandall Stewart if (ptr == NULL) {
259b8d60729SRandall Stewart htcp_data = malloc(sizeof(struct htcp), M_CC_MEM, M_NOWAIT);
2605728a0eaSLawrence Stewart if (htcp_data == NULL)
2615728a0eaSLawrence Stewart return (ENOMEM);
262b8d60729SRandall Stewart } else
263b8d60729SRandall Stewart htcp_data = ptr;
2645728a0eaSLawrence Stewart
2655728a0eaSLawrence Stewart /* Init some key variables with sensible defaults. */
2665728a0eaSLawrence Stewart htcp_data->alpha = HTCP_INIT_ALPHA;
2675728a0eaSLawrence Stewart htcp_data->beta = HTCP_MINBETA;
2685728a0eaSLawrence Stewart htcp_data->maxrtt = TCPTV_SRTTBASE;
2695728a0eaSLawrence Stewart htcp_data->minrtt = TCPTV_SRTTBASE;
2705728a0eaSLawrence Stewart htcp_data->prev_cwnd = 0;
2715728a0eaSLawrence Stewart htcp_data->t_last_cong = ticks;
2725728a0eaSLawrence Stewart
2735728a0eaSLawrence Stewart ccv->cc_data = htcp_data;
2745728a0eaSLawrence Stewart
2755728a0eaSLawrence Stewart return (0);
2765728a0eaSLawrence Stewart }
2775728a0eaSLawrence Stewart
2785728a0eaSLawrence Stewart /*
2795728a0eaSLawrence Stewart * Perform any necessary tasks before we enter congestion recovery.
2805728a0eaSLawrence Stewart */
2815728a0eaSLawrence Stewart static void
htcp_cong_signal(struct cc_var * ccv,ccsignal_t type)282f74352fbSRichard Scheffenegger htcp_cong_signal(struct cc_var *ccv, ccsignal_t type)
2835728a0eaSLawrence Stewart {
2845728a0eaSLawrence Stewart struct htcp *htcp_data;
28532a6df57SRichard Scheffenegger uint32_t mss, pipe;
2865728a0eaSLawrence Stewart
2875728a0eaSLawrence Stewart htcp_data = ccv->cc_data;
28800d3b744SMichael Tuexen mss = tcp_fixed_maxseg(ccv->tp);
2895728a0eaSLawrence Stewart
2905728a0eaSLawrence Stewart switch (type) {
2915728a0eaSLawrence Stewart case CC_NDUPACK:
2925728a0eaSLawrence Stewart if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
2935728a0eaSLawrence Stewart if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
2945728a0eaSLawrence Stewart /*
2955728a0eaSLawrence Stewart * Apply hysteresis to maxrtt to ensure
2965728a0eaSLawrence Stewart * reductions in the RTT are reflected in our
2975728a0eaSLawrence Stewart * measurements.
2985728a0eaSLawrence Stewart */
2995728a0eaSLawrence Stewart htcp_data->maxrtt = (htcp_data->minrtt +
3005728a0eaSLawrence Stewart (htcp_data->maxrtt - htcp_data->minrtt) *
3015728a0eaSLawrence Stewart 95) / 100;
3025728a0eaSLawrence Stewart htcp_ssthresh_update(ccv);
3035728a0eaSLawrence Stewart htcp_data->t_last_cong = ticks;
30443053c12SSean Bruno htcp_data->prev_cwnd = CCV(ccv, snd_cwnd);
3055728a0eaSLawrence Stewart }
3065728a0eaSLawrence Stewart ENTER_RECOVERY(CCV(ccv, t_flags));
3075728a0eaSLawrence Stewart }
3085728a0eaSLawrence Stewart break;
3095728a0eaSLawrence Stewart
3105728a0eaSLawrence Stewart case CC_ECN:
3115728a0eaSLawrence Stewart if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
3125728a0eaSLawrence Stewart /*
3135728a0eaSLawrence Stewart * Apply hysteresis to maxrtt to ensure reductions in
3145728a0eaSLawrence Stewart * the RTT are reflected in our measurements.
3155728a0eaSLawrence Stewart */
3165728a0eaSLawrence Stewart htcp_data->maxrtt = (htcp_data->minrtt + (htcp_data->maxrtt -
3175728a0eaSLawrence Stewart htcp_data->minrtt) * 95) / 100;
3185728a0eaSLawrence Stewart htcp_ssthresh_update(ccv);
3195728a0eaSLawrence Stewart CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
3205728a0eaSLawrence Stewart htcp_data->t_last_cong = ticks;
32143053c12SSean Bruno htcp_data->prev_cwnd = CCV(ccv, snd_cwnd);
3225728a0eaSLawrence Stewart ENTER_CONGRECOVERY(CCV(ccv, t_flags));
3235728a0eaSLawrence Stewart }
3245728a0eaSLawrence Stewart break;
3255728a0eaSLawrence Stewart
3265728a0eaSLawrence Stewart case CC_RTO:
32732a6df57SRichard Scheffenegger if (CCV(ccv, t_rxtshift) == 1) {
32832a6df57SRichard Scheffenegger if (V_tcp_do_newsack) {
32900d3b744SMichael Tuexen pipe = tcp_compute_pipe(ccv->tp);
33032a6df57SRichard Scheffenegger } else {
331fcea1cc9SRichard Scheffenegger pipe = CCV(ccv, snd_max) -
33232a6df57SRichard Scheffenegger CCV(ccv, snd_fack) +
33332a6df57SRichard Scheffenegger CCV(ccv, sackhint.sack_bytes_rexmit);
33432a6df57SRichard Scheffenegger }
33532a6df57SRichard Scheffenegger CCV(ccv, snd_ssthresh) = max(2,
33632a6df57SRichard Scheffenegger min(CCV(ccv, snd_wnd), pipe) / 2 / mss) * mss;
33732a6df57SRichard Scheffenegger }
33839a12f01SRichard Scheffenegger CCV(ccv, snd_cwnd) = mss;
3395728a0eaSLawrence Stewart /*
3405728a0eaSLawrence Stewart * Grab the current time and record it so we know when the
3415728a0eaSLawrence Stewart * most recent congestion event was. Only record it when the
3425728a0eaSLawrence Stewart * timeout has fired more than once, as there is a reasonable
3435728a0eaSLawrence Stewart * chance the first one is a false alarm and may not indicate
3445728a0eaSLawrence Stewart * congestion.
3455728a0eaSLawrence Stewart */
3465728a0eaSLawrence Stewart if (CCV(ccv, t_rxtshift) >= 2)
3475728a0eaSLawrence Stewart htcp_data->t_last_cong = ticks;
3485728a0eaSLawrence Stewart break;
349f74352fbSRichard Scheffenegger default:
350f74352fbSRichard Scheffenegger break;
3515728a0eaSLawrence Stewart }
3525728a0eaSLawrence Stewart }
3535728a0eaSLawrence Stewart
3545728a0eaSLawrence Stewart static int
htcp_mod_init(void)3555728a0eaSLawrence Stewart htcp_mod_init(void)
3565728a0eaSLawrence Stewart {
3575728a0eaSLawrence Stewart /*
3585728a0eaSLawrence Stewart * HTCP_RTT_REF is defined in ms, and t_srtt in the tcpcb is stored in
3595728a0eaSLawrence Stewart * units of TCP_RTT_SCALE*hz. Scale HTCP_RTT_REF to be in the same units
3605728a0eaSLawrence Stewart * as t_srtt.
3615728a0eaSLawrence Stewart */
3625728a0eaSLawrence Stewart htcp_rtt_ref = (HTCP_RTT_REF * TCP_RTT_SCALE * hz) / 1000;
3635728a0eaSLawrence Stewart return (0);
3645728a0eaSLawrence Stewart }
3655728a0eaSLawrence Stewart
3665728a0eaSLawrence Stewart /*
3675728a0eaSLawrence Stewart * Perform any necessary tasks before we exit congestion recovery.
3685728a0eaSLawrence Stewart */
3695728a0eaSLawrence Stewart static void
htcp_post_recovery(struct cc_var * ccv)3705728a0eaSLawrence Stewart htcp_post_recovery(struct cc_var *ccv)
3715728a0eaSLawrence Stewart {
3727c375daaSHiren Panchasara int pipe;
3735728a0eaSLawrence Stewart struct htcp *htcp_data;
374*22dcc812SRichard Scheffenegger uint32_t mss = tcp_fixed_maxseg(ccv->tp);
3755728a0eaSLawrence Stewart
3767c375daaSHiren Panchasara pipe = 0;
3775728a0eaSLawrence Stewart htcp_data = ccv->cc_data;
3785728a0eaSLawrence Stewart
3795728a0eaSLawrence Stewart if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
3805728a0eaSLawrence Stewart /*
3815728a0eaSLawrence Stewart * If inflight data is less than ssthresh, set cwnd
3825728a0eaSLawrence Stewart * conservatively to avoid a burst of data, as suggested in the
3835728a0eaSLawrence Stewart * NewReno RFC. Otherwise, use the HTCP method.
3845728a0eaSLawrence Stewart *
3855728a0eaSLawrence Stewart * XXXLAS: Find a way to do this without needing curack
3865728a0eaSLawrence Stewart */
387d1de2b05SRichard Scheffenegger if (V_tcp_do_newsack)
38800d3b744SMichael Tuexen pipe = tcp_compute_pipe(ccv->tp);
3897c375daaSHiren Panchasara else
3907c375daaSHiren Panchasara pipe = CCV(ccv, snd_max) - ccv->curack;
3917c375daaSHiren Panchasara
3927c375daaSHiren Panchasara if (pipe < CCV(ccv, snd_ssthresh))
3935cc11a89SMichael Tuexen /*
3945cc11a89SMichael Tuexen * Ensure that cwnd down not collape to 1 MSS under
3955cc11a89SMichael Tuexen * adverse conditions. Implements RFC6582
3965cc11a89SMichael Tuexen */
397*22dcc812SRichard Scheffenegger CCV(ccv, snd_cwnd) = max(pipe, mss) + mss;
3985728a0eaSLawrence Stewart else
3995728a0eaSLawrence Stewart CCV(ccv, snd_cwnd) = max(1, ((htcp_data->beta *
400*22dcc812SRichard Scheffenegger htcp_data->prev_cwnd / mss)
401*22dcc812SRichard Scheffenegger >> HTCP_SHIFT)) * mss;
4025728a0eaSLawrence Stewart }
4035728a0eaSLawrence Stewart }
4045728a0eaSLawrence Stewart
4055728a0eaSLawrence Stewart static void
htcp_recalc_alpha(struct cc_var * ccv)4065728a0eaSLawrence Stewart htcp_recalc_alpha(struct cc_var *ccv)
4075728a0eaSLawrence Stewart {
4085728a0eaSLawrence Stewart struct htcp *htcp_data;
4095728a0eaSLawrence Stewart int alpha, diff, now;
4105728a0eaSLawrence Stewart
4115728a0eaSLawrence Stewart htcp_data = ccv->cc_data;
4125728a0eaSLawrence Stewart now = ticks;
4135728a0eaSLawrence Stewart
4145728a0eaSLawrence Stewart /*
4155728a0eaSLawrence Stewart * If ticks has wrapped around (will happen approximately once every 49
4165728a0eaSLawrence Stewart * days on a machine with the default kern.hz=1000) and a flow straddles
4175728a0eaSLawrence Stewart * the wrap point, our alpha calcs will be completely wrong. We cut our
4185728a0eaSLawrence Stewart * losses and restart alpha from scratch by setting t_last_cong = now -
4195728a0eaSLawrence Stewart * HTCP_DELTA_L.
4205728a0eaSLawrence Stewart *
4215728a0eaSLawrence Stewart * This does not deflate our cwnd at all. It simply slows the rate cwnd
4225728a0eaSLawrence Stewart * is growing by until alpha regains the value it held prior to taking
4235728a0eaSLawrence Stewart * this drastic measure.
4245728a0eaSLawrence Stewart */
4255728a0eaSLawrence Stewart if (now < htcp_data->t_last_cong)
4265728a0eaSLawrence Stewart htcp_data->t_last_cong = now - HTCP_DELTA_L;
4275728a0eaSLawrence Stewart
4285728a0eaSLawrence Stewart diff = now - htcp_data->t_last_cong - HTCP_DELTA_L;
4295728a0eaSLawrence Stewart
4305728a0eaSLawrence Stewart /* Cap alpha if the value of diff would overflow HTCP_CALC_ALPHA(). */
4315728a0eaSLawrence Stewart if (diff < htcp_max_diff) {
4325728a0eaSLawrence Stewart /*
4335728a0eaSLawrence Stewart * If it has been more than HTCP_DELTA_L ticks since congestion,
4345728a0eaSLawrence Stewart * increase alpha according to the function defined in the spec.
4355728a0eaSLawrence Stewart */
4365728a0eaSLawrence Stewart if (diff > 0) {
4375728a0eaSLawrence Stewart alpha = HTCP_CALC_ALPHA(diff);
4385728a0eaSLawrence Stewart
4395728a0eaSLawrence Stewart /*
4405728a0eaSLawrence Stewart * Adaptive backoff fairness adjustment:
4415728a0eaSLawrence Stewart * 2 * (1 - beta) * alpha_raw
4425728a0eaSLawrence Stewart */
4435728a0eaSLawrence Stewart if (V_htcp_adaptive_backoff)
4445728a0eaSLawrence Stewart alpha = max(1, (2 * ((1 << HTCP_SHIFT) -
4455728a0eaSLawrence Stewart htcp_data->beta) * alpha) >> HTCP_SHIFT);
4465728a0eaSLawrence Stewart
4475728a0eaSLawrence Stewart /*
4485728a0eaSLawrence Stewart * RTT scaling: (RTT / RTT_ref) * alpha
4495728a0eaSLawrence Stewart * alpha will be the raw value from HTCP_CALC_ALPHA() if
4505728a0eaSLawrence Stewart * adaptive backoff is off, or the adjusted value if
4515728a0eaSLawrence Stewart * adaptive backoff is on.
4525728a0eaSLawrence Stewart */
4535728a0eaSLawrence Stewart if (V_htcp_rtt_scaling)
4545728a0eaSLawrence Stewart alpha = max(1, (min(max(HTCP_MINROWE,
45500d3b744SMichael Tuexen (tcp_get_srtt(ccv->tp, TCP_TMR_GRANULARITY_TICKS) << HTCP_SHIFT) /
4565728a0eaSLawrence Stewart htcp_rtt_ref), HTCP_MAXROWE) * alpha)
4575728a0eaSLawrence Stewart >> HTCP_SHIFT);
4585728a0eaSLawrence Stewart
4595728a0eaSLawrence Stewart } else
4605728a0eaSLawrence Stewart alpha = 1;
4615728a0eaSLawrence Stewart
4625728a0eaSLawrence Stewart htcp_data->alpha = alpha;
4635728a0eaSLawrence Stewart }
4645728a0eaSLawrence Stewart }
4655728a0eaSLawrence Stewart
4665728a0eaSLawrence Stewart static void
htcp_recalc_beta(struct cc_var * ccv)4675728a0eaSLawrence Stewart htcp_recalc_beta(struct cc_var *ccv)
4685728a0eaSLawrence Stewart {
4695728a0eaSLawrence Stewart struct htcp *htcp_data;
4705728a0eaSLawrence Stewart
4715728a0eaSLawrence Stewart htcp_data = ccv->cc_data;
4725728a0eaSLawrence Stewart
4735728a0eaSLawrence Stewart /*
4745728a0eaSLawrence Stewart * TCPTV_SRTTBASE is the initialised value of each connection's SRTT, so
4755728a0eaSLawrence Stewart * we only calc beta if the connection's SRTT has been changed from its
476a4641f4eSPedro F. Giffuni * initial value. beta is bounded to ensure it is always between
4775728a0eaSLawrence Stewart * HTCP_MINBETA and HTCP_MAXBETA.
4785728a0eaSLawrence Stewart */
4795728a0eaSLawrence Stewart if (V_htcp_adaptive_backoff && htcp_data->minrtt != TCPTV_SRTTBASE &&
4805728a0eaSLawrence Stewart htcp_data->maxrtt != TCPTV_SRTTBASE)
4815728a0eaSLawrence Stewart htcp_data->beta = min(max(HTCP_MINBETA,
4825728a0eaSLawrence Stewart (htcp_data->minrtt << HTCP_SHIFT) / htcp_data->maxrtt),
4835728a0eaSLawrence Stewart HTCP_MAXBETA);
4845728a0eaSLawrence Stewart else
4855728a0eaSLawrence Stewart htcp_data->beta = HTCP_MINBETA;
4865728a0eaSLawrence Stewart }
4875728a0eaSLawrence Stewart
4885728a0eaSLawrence Stewart /*
4895728a0eaSLawrence Stewart * Record the minimum and maximum RTT seen for the connection. These are used in
4905728a0eaSLawrence Stewart * the calculation of beta if adaptive backoff is enabled.
4915728a0eaSLawrence Stewart */
4925728a0eaSLawrence Stewart static void
htcp_record_rtt(struct cc_var * ccv)4935728a0eaSLawrence Stewart htcp_record_rtt(struct cc_var *ccv)
4945728a0eaSLawrence Stewart {
4955728a0eaSLawrence Stewart struct htcp *htcp_data;
4965728a0eaSLawrence Stewart
4975728a0eaSLawrence Stewart htcp_data = ccv->cc_data;
4985728a0eaSLawrence Stewart
4995728a0eaSLawrence Stewart /* XXXLAS: Should there be some hysteresis for minrtt? */
5005728a0eaSLawrence Stewart
5015728a0eaSLawrence Stewart /*
5025728a0eaSLawrence Stewart * Record the current SRTT as our minrtt if it's the smallest we've seen
5035728a0eaSLawrence Stewart * or minrtt is currently equal to its initialised value. Ignore SRTT
5045728a0eaSLawrence Stewart * until a min number of samples have been taken.
5055728a0eaSLawrence Stewart */
50600d3b744SMichael Tuexen if ((tcp_get_srtt(ccv->tp, TCP_TMR_GRANULARITY_TICKS) < htcp_data->minrtt ||
5075728a0eaSLawrence Stewart htcp_data->minrtt == TCPTV_SRTTBASE) &&
5085728a0eaSLawrence Stewart (CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES))
50900d3b744SMichael Tuexen htcp_data->minrtt = tcp_get_srtt(ccv->tp, TCP_TMR_GRANULARITY_TICKS);
5105728a0eaSLawrence Stewart
5115728a0eaSLawrence Stewart /*
5125728a0eaSLawrence Stewart * Record the current SRTT as our maxrtt if it's the largest we've
5135728a0eaSLawrence Stewart * seen. Ignore SRTT until a min number of samples have been taken.
5145728a0eaSLawrence Stewart */
51500d3b744SMichael Tuexen if (tcp_get_srtt(ccv->tp, TCP_TMR_GRANULARITY_TICKS) > htcp_data->maxrtt
5165728a0eaSLawrence Stewart && CCV(ccv, t_rttupdated) >= HTCP_MIN_RTT_SAMPLES)
51700d3b744SMichael Tuexen htcp_data->maxrtt = tcp_get_srtt(ccv->tp, TCP_TMR_GRANULARITY_TICKS);
5185728a0eaSLawrence Stewart }
5195728a0eaSLawrence Stewart
5205728a0eaSLawrence Stewart /*
5215728a0eaSLawrence Stewart * Update the ssthresh in the event of congestion.
5225728a0eaSLawrence Stewart */
5235728a0eaSLawrence Stewart static void
htcp_ssthresh_update(struct cc_var * ccv)5245728a0eaSLawrence Stewart htcp_ssthresh_update(struct cc_var *ccv)
5255728a0eaSLawrence Stewart {
5265728a0eaSLawrence Stewart struct htcp *htcp_data;
5275728a0eaSLawrence Stewart
5285728a0eaSLawrence Stewart htcp_data = ccv->cc_data;
5295728a0eaSLawrence Stewart
5305728a0eaSLawrence Stewart /*
5315728a0eaSLawrence Stewart * On the first congestion event, set ssthresh to cwnd * 0.5, on
5325728a0eaSLawrence Stewart * subsequent congestion events, set it to cwnd * beta.
5335728a0eaSLawrence Stewart */
5345728a0eaSLawrence Stewart if (CCV(ccv, snd_ssthresh) == TCP_MAXWIN << TCP_MAX_WINSHIFT)
5353ac12506SJonathan T. Looney CCV(ccv, snd_ssthresh) = ((u_long)CCV(ccv, snd_cwnd) *
5363ac12506SJonathan T. Looney HTCP_MINBETA) >> HTCP_SHIFT;
5375728a0eaSLawrence Stewart else {
5385728a0eaSLawrence Stewart htcp_recalc_beta(ccv);
5393ac12506SJonathan T. Looney CCV(ccv, snd_ssthresh) = ((u_long)CCV(ccv, snd_cwnd) *
5403ac12506SJonathan T. Looney htcp_data->beta) >> HTCP_SHIFT;
5415728a0eaSLawrence Stewart }
5425728a0eaSLawrence Stewart }
5435728a0eaSLawrence Stewart
5445728a0eaSLawrence Stewart SYSCTL_DECL(_net_inet_tcp_cc_htcp);
5457029da5cSPawel Biernacki SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, htcp, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
5467029da5cSPawel Biernacki "H-TCP related settings");
5476df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, adaptive_backoff,
5486df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(htcp_adaptive_backoff), 0,
5496df8a710SGleb Smirnoff "enable H-TCP adaptive backoff");
5506df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_cc_htcp, OID_AUTO, rtt_scaling,
5516df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(htcp_rtt_scaling), 0,
5526df8a710SGleb Smirnoff "enable H-TCP RTT scaling");
5535728a0eaSLawrence Stewart
5545728a0eaSLawrence Stewart DECLARE_CC_MODULE(htcp, &htcp_cc_algo);
555b8d60729SRandall Stewart MODULE_VERSION(htcp, 2);
556