145a4b79dSSebastien Roy /*
245a4b79dSSebastien Roy * Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org>
345a4b79dSSebastien Roy * Copyright (c) 2010 The FreeBSD Foundation
445a4b79dSSebastien Roy * All rights reserved.
545a4b79dSSebastien Roy * Copyright (c) 2017 by Delphix. All rights reserved.
645a4b79dSSebastien Roy * Copyright 2019 Joyent, Inc.
7*3b0b0a4eSPaul Winder * Copyright 2020 RackTop Systems, Inc.
845a4b79dSSebastien Roy *
945a4b79dSSebastien Roy * This software was developed by Lawrence Stewart while studying at the Centre
1045a4b79dSSebastien Roy * for Advanced Internet Architectures, Swinburne University of Technology, made
1145a4b79dSSebastien Roy * possible in part by a grant from the Cisco University Research Program Fund
1245a4b79dSSebastien Roy * at Community Foundation Silicon Valley.
1345a4b79dSSebastien Roy *
1445a4b79dSSebastien Roy * Portions of this software were developed at the Centre for Advanced
1545a4b79dSSebastien Roy * Internet Architectures, Swinburne University of Technology, Melbourne,
1645a4b79dSSebastien Roy * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
1745a4b79dSSebastien Roy *
1845a4b79dSSebastien Roy * Redistribution and use in source and binary forms, with or without
1945a4b79dSSebastien Roy * modification, are permitted provided that the following conditions
2045a4b79dSSebastien Roy * are met:
2145a4b79dSSebastien Roy * 1. Redistributions of source code must retain the above copyright
2245a4b79dSSebastien Roy * notice, this list of conditions and the following disclaimer.
2345a4b79dSSebastien Roy * 2. Redistributions in binary form must reproduce the above copyright
2445a4b79dSSebastien Roy * notice, this list of conditions and the following disclaimer in the
2545a4b79dSSebastien Roy * documentation and/or other materials provided with the distribution.
2645a4b79dSSebastien Roy *
2745a4b79dSSebastien Roy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2845a4b79dSSebastien Roy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2945a4b79dSSebastien Roy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3045a4b79dSSebastien Roy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3145a4b79dSSebastien Roy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3245a4b79dSSebastien Roy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3345a4b79dSSebastien Roy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3445a4b79dSSebastien Roy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3545a4b79dSSebastien Roy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3645a4b79dSSebastien Roy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3745a4b79dSSebastien Roy * SUCH DAMAGE.
3845a4b79dSSebastien Roy *
3945a4b79dSSebastien Roy * $FreeBSD$
4045a4b79dSSebastien Roy */
4145a4b79dSSebastien Roy
4245a4b79dSSebastien Roy #ifndef _NETINET_CC_CUBIC_H_
4345a4b79dSSebastien Roy #define _NETINET_CC_CUBIC_H_
4445a4b79dSSebastien Roy
4545a4b79dSSebastien Roy /* Number of bits of precision for fixed point math calcs. */
4645a4b79dSSebastien Roy #define CUBIC_SHIFT 8
4745a4b79dSSebastien Roy
4845a4b79dSSebastien Roy #define CUBIC_SHIFT_4 32
4945a4b79dSSebastien Roy
5045a4b79dSSebastien Roy /* 0.5 << CUBIC_SHIFT. */
5145a4b79dSSebastien Roy #define RENO_BETA 128
5245a4b79dSSebastien Roy
5345a4b79dSSebastien Roy /* ~0.8 << CUBIC_SHIFT. */
5445a4b79dSSebastien Roy #define CUBIC_BETA 204
5545a4b79dSSebastien Roy
5645a4b79dSSebastien Roy /* ~0.2 << CUBIC_SHIFT. */
5745a4b79dSSebastien Roy #define ONE_SUB_CUBIC_BETA 51
5845a4b79dSSebastien Roy
5945a4b79dSSebastien Roy /* 3 * ONE_SUB_CUBIC_BETA. */
6045a4b79dSSebastien Roy #define THREE_X_PT2 153
6145a4b79dSSebastien Roy
6245a4b79dSSebastien Roy /* (2 << CUBIC_SHIFT) - ONE_SUB_CUBIC_BETA. */
6345a4b79dSSebastien Roy #define TWO_SUB_PT2 461
6445a4b79dSSebastien Roy
6545a4b79dSSebastien Roy /* ~0.4 << CUBIC_SHIFT. */
6645a4b79dSSebastien Roy #define CUBIC_C_FACTOR 102
6745a4b79dSSebastien Roy
6845a4b79dSSebastien Roy /* CUBIC fast convergence factor: ~0.9 << CUBIC_SHIFT. */
6945a4b79dSSebastien Roy #define CUBIC_FC_FACTOR 230
7045a4b79dSSebastien Roy
7145a4b79dSSebastien Roy /* Don't trust s_rtt until this many rtt samples have been taken. */
7245a4b79dSSebastien Roy #define CUBIC_MIN_RTT_SAMPLES 8
7345a4b79dSSebastien Roy
74*3b0b0a4eSPaul Winder /*
75*3b0b0a4eSPaul Winder * (2^21)^3 is long max. Dividing (2^63) by Cubic_C_factor
76*3b0b0a4eSPaul Winder * and taking cube-root yields 448845 as the effective useful limit
77*3b0b0a4eSPaul Winder */
78*3b0b0a4eSPaul Winder #define CUBED_ROOT_MAX_ULONG 448845
79*3b0b0a4eSPaul Winder
8045a4b79dSSebastien Roy /* Userland only bits. */
8145a4b79dSSebastien Roy #ifndef _KERNEL
8245a4b79dSSebastien Roy
8345a4b79dSSebastien Roy extern int hz;
8445a4b79dSSebastien Roy
8545a4b79dSSebastien Roy /*
8645a4b79dSSebastien Roy * Implementation based on the formulae found in the CUBIC Internet Draft
8745a4b79dSSebastien Roy * "draft-rhee-tcpm-cubic-02".
8845a4b79dSSebastien Roy *
8945a4b79dSSebastien Roy * Note BETA used in cc_cubic is equal to (1-beta) in the I-D
9045a4b79dSSebastien Roy */
9145a4b79dSSebastien Roy
9245a4b79dSSebastien Roy static __inline float
theoretical_cubic_k(double wmax_pkts)9345a4b79dSSebastien Roy theoretical_cubic_k(double wmax_pkts)
9445a4b79dSSebastien Roy {
9545a4b79dSSebastien Roy double C;
9645a4b79dSSebastien Roy
9745a4b79dSSebastien Roy C = 0.4;
9845a4b79dSSebastien Roy
9945a4b79dSSebastien Roy return (pow((wmax_pkts * 0.2) / C, (1.0 / 3.0)) * pow(2, CUBIC_SHIFT));
10045a4b79dSSebastien Roy }
10145a4b79dSSebastien Roy
10245a4b79dSSebastien Roy static __inline uint32_t
theoretical_cubic_cwnd(int ticks_since_cong,uint32_t wmax,uint32_t smss)10345a4b79dSSebastien Roy theoretical_cubic_cwnd(int ticks_since_cong, uint32_t wmax, uint32_t smss)
10445a4b79dSSebastien Roy {
10545a4b79dSSebastien Roy double C, wmax_pkts;
10645a4b79dSSebastien Roy
10745a4b79dSSebastien Roy C = 0.4;
10845a4b79dSSebastien Roy wmax_pkts = wmax / (double)smss;
10945a4b79dSSebastien Roy
11045a4b79dSSebastien Roy return (smss * (wmax_pkts +
11145a4b79dSSebastien Roy (C * pow(ticks_since_cong / (double)hz -
11245a4b79dSSebastien Roy theoretical_cubic_k(wmax_pkts) / pow(2, CUBIC_SHIFT), 3.0))));
11345a4b79dSSebastien Roy }
11445a4b79dSSebastien Roy
11545a4b79dSSebastien Roy static __inline uint32_t
theoretical_reno_cwnd(int ticks_since_cong,int rtt_ticks,uint32_t wmax,uint32_t smss)11645a4b79dSSebastien Roy theoretical_reno_cwnd(int ticks_since_cong, int rtt_ticks, uint32_t wmax,
11745a4b79dSSebastien Roy uint32_t smss)
11845a4b79dSSebastien Roy {
11945a4b79dSSebastien Roy
12045a4b79dSSebastien Roy return ((wmax * 0.5) + ((ticks_since_cong / (float)rtt_ticks) * smss));
12145a4b79dSSebastien Roy }
12245a4b79dSSebastien Roy
12345a4b79dSSebastien Roy static __inline uint32_t
theoretical_tf_cwnd(int ticks_since_cong,int rtt_ticks,unsigned long wmax,uint32_t smss)12445a4b79dSSebastien Roy theoretical_tf_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
12545a4b79dSSebastien Roy uint32_t smss)
12645a4b79dSSebastien Roy {
12745a4b79dSSebastien Roy
12845a4b79dSSebastien Roy return ((wmax * 0.8) + ((3 * 0.2) / (2 - 0.2) *
12945a4b79dSSebastien Roy (ticks_since_cong / (float)rtt_ticks) * smss));
13045a4b79dSSebastien Roy }
13145a4b79dSSebastien Roy
13245a4b79dSSebastien Roy #endif /* !_KERNEL */
13345a4b79dSSebastien Roy
13445a4b79dSSebastien Roy /*
13545a4b79dSSebastien Roy * Compute the CUBIC K value used in the cwnd calculation, using an
13645a4b79dSSebastien Roy * implementation of eqn 2 in the I-D. The method used
13745a4b79dSSebastien Roy * here is adapted from Apple Computer Technical Report #KT-32.
13845a4b79dSSebastien Roy */
13945a4b79dSSebastien Roy static __inline int64_t
cubic_k(uint32_t wmax_pkts)14045a4b79dSSebastien Roy cubic_k(uint32_t wmax_pkts)
14145a4b79dSSebastien Roy {
14245a4b79dSSebastien Roy int64_t s, K;
14345a4b79dSSebastien Roy uint16_t p;
14445a4b79dSSebastien Roy
14545a4b79dSSebastien Roy K = s = 0;
14645a4b79dSSebastien Roy p = 0;
14745a4b79dSSebastien Roy
14845a4b79dSSebastien Roy /* (wmax * beta)/C with CUBIC_SHIFT worth of precision. */
14945a4b79dSSebastien Roy s = ((wmax_pkts * ONE_SUB_CUBIC_BETA) << CUBIC_SHIFT) / CUBIC_C_FACTOR;
15045a4b79dSSebastien Roy
15145a4b79dSSebastien Roy /* Rebase s to be between 1 and 1/8 with a shift of CUBIC_SHIFT. */
15245a4b79dSSebastien Roy while (s >= 256) {
15345a4b79dSSebastien Roy s >>= 3;
15445a4b79dSSebastien Roy p++;
15545a4b79dSSebastien Roy }
15645a4b79dSSebastien Roy
15745a4b79dSSebastien Roy /*
15845a4b79dSSebastien Roy * Some magic constants taken from the Apple TR with appropriate
15945a4b79dSSebastien Roy * shifts: 275 == 1.072302 << CUBIC_SHIFT, 98 == 0.3812513 <<
16045a4b79dSSebastien Roy * CUBIC_SHIFT, 120 == 0.46946116 << CUBIC_SHIFT.
16145a4b79dSSebastien Roy */
16245a4b79dSSebastien Roy K = (((s * 275) >> CUBIC_SHIFT) + 98) -
16345a4b79dSSebastien Roy (((s * s * 120) >> CUBIC_SHIFT) >> CUBIC_SHIFT);
16445a4b79dSSebastien Roy
16545a4b79dSSebastien Roy /* Multiply by 2^p to undo the rebasing of s from above. */
16645a4b79dSSebastien Roy return (K <<= p);
16745a4b79dSSebastien Roy }
16845a4b79dSSebastien Roy
16945a4b79dSSebastien Roy /*
17045a4b79dSSebastien Roy * Compute the new cwnd value using an implementation of eqn 1 from the I-D.
17145a4b79dSSebastien Roy * Thanks to Kip Macy for help debugging this function.
17245a4b79dSSebastien Roy *
17345a4b79dSSebastien Roy * XXXLAS: Characterise bounds for overflow.
17445a4b79dSSebastien Roy */
17545a4b79dSSebastien Roy static __inline uint32_t
cubic_cwnd(hrtime_t nsecs_since_cong,uint32_t wmax,uint32_t smss,int64_t K)17645a4b79dSSebastien Roy cubic_cwnd(hrtime_t nsecs_since_cong, uint32_t wmax, uint32_t smss, int64_t K)
17745a4b79dSSebastien Roy {
17845a4b79dSSebastien Roy int64_t t, cwnd;
17945a4b79dSSebastien Roy
18045a4b79dSSebastien Roy /*
18145a4b79dSSebastien Roy * Convert nsecs_since_cong to milliseconds, with CUBIC_SHIFT worth
18245a4b79dSSebastien Roy * of precision.
18345a4b79dSSebastien Roy */
18445a4b79dSSebastien Roy t = NSEC2MSEC(nsecs_since_cong << CUBIC_SHIFT);
18545a4b79dSSebastien Roy
18645a4b79dSSebastien Roy /*
18745a4b79dSSebastien Roy * K is the time period in seconds that it will take to reach wmax. The
18845a4b79dSSebastien Roy * value is kept in fixed point form with CUBIC_SHIFT worth of
18945a4b79dSSebastien Roy * precision.
19045a4b79dSSebastien Roy *
19145a4b79dSSebastien Roy * For comparison with t, we convert K to milliseconds, and then convert
19245a4b79dSSebastien Roy * the result back to seconds.
19345a4b79dSSebastien Roy *
19445a4b79dSSebastien Roy * cwnd = t - K, with CUBIC_SHIFT worth of precision.
19545a4b79dSSebastien Roy */
19645a4b79dSSebastien Roy cwnd = (t - K * MILLISEC) / MILLISEC;
19745a4b79dSSebastien Roy
198*3b0b0a4eSPaul Winder if (cwnd > CUBED_ROOT_MAX_ULONG)
199*3b0b0a4eSPaul Winder return (INT_MAX);
200*3b0b0a4eSPaul Winder if (cwnd < -CUBED_ROOT_MAX_ULONG)
201*3b0b0a4eSPaul Winder return (0);
202*3b0b0a4eSPaul Winder
20345a4b79dSSebastien Roy /* cwnd = (t - K)^3, with CUBIC_SHIFT^3 worth of precision. */
20445a4b79dSSebastien Roy cwnd *= (cwnd * cwnd);
20545a4b79dSSebastien Roy
20645a4b79dSSebastien Roy /*
20745a4b79dSSebastien Roy * C(t - K)^3 + wmax
20845a4b79dSSebastien Roy * The down shift by CUBIC_SHIFT_4 is because cwnd has 4 lots of
20945a4b79dSSebastien Roy * CUBIC_SHIFT included in the value. 3 from the cubing of cwnd above,
21045a4b79dSSebastien Roy * and an extra from multiplying through by CUBIC_C_FACTOR.
21145a4b79dSSebastien Roy */
21245a4b79dSSebastien Roy cwnd = ((cwnd * CUBIC_C_FACTOR * smss) >> CUBIC_SHIFT_4) + wmax;
21345a4b79dSSebastien Roy
214*3b0b0a4eSPaul Winder /*
215*3b0b0a4eSPaul Winder * for negative cwnd, limiting to zero as lower bound
216*3b0b0a4eSPaul Winder */
217*3b0b0a4eSPaul Winder return (max(0, cwnd));
21845a4b79dSSebastien Roy }
21945a4b79dSSebastien Roy
22045a4b79dSSebastien Roy /*
22145a4b79dSSebastien Roy * Compute an approximation of the "TCP friendly" cwnd some number of
22245a4b79dSSebastien Roy * nanoseconds after a congestion event that is designed to yield the same
22345a4b79dSSebastien Roy * average cwnd as NewReno while using CUBIC's beta of 0.8. RTT should be the
22445a4b79dSSebastien Roy * average RTT estimate for the path measured over the previous congestion
22545a4b79dSSebastien Roy * epoch and wmax is the value of cwnd at the last congestion event.
22645a4b79dSSebastien Roy */
22745a4b79dSSebastien Roy static __inline uint32_t
tf_cwnd(hrtime_t nsecs_since_cong,hrtime_t rtt_nsecs,uint32_t wmax,uint32_t smss)22845a4b79dSSebastien Roy tf_cwnd(hrtime_t nsecs_since_cong, hrtime_t rtt_nsecs, uint32_t wmax,
22945a4b79dSSebastien Roy uint32_t smss)
23045a4b79dSSebastien Roy {
23145a4b79dSSebastien Roy
23245a4b79dSSebastien Roy /* Equation 4 of I-D. */
23345a4b79dSSebastien Roy return (((wmax * CUBIC_BETA) + (((THREE_X_PT2 * nsecs_since_cong *
23445a4b79dSSebastien Roy smss) << CUBIC_SHIFT) / TWO_SUB_PT2 / rtt_nsecs)) >> CUBIC_SHIFT);
23545a4b79dSSebastien Roy }
23645a4b79dSSebastien Roy
23745a4b79dSSebastien Roy #endif /* _NETINET_CC_CUBIC_H_ */
238