167fef78bSLawrence Stewart /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni *
467fef78bSLawrence Stewart * Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org>
567fef78bSLawrence Stewart * Copyright (c) 2010 The FreeBSD Foundation
667fef78bSLawrence Stewart * All rights reserved.
767fef78bSLawrence Stewart *
867fef78bSLawrence Stewart * This software was developed by Lawrence Stewart while studying at the Centre
9891b8ed4SLawrence Stewart * for Advanced Internet Architectures, Swinburne University of Technology, made
10891b8ed4SLawrence Stewart * possible in part by a grant from the Cisco University Research Program Fund
11891b8ed4SLawrence Stewart * at Community Foundation Silicon Valley.
1267fef78bSLawrence Stewart *
1367fef78bSLawrence Stewart * Portions of this software were developed at the Centre for Advanced
1467fef78bSLawrence Stewart * Internet Architectures, Swinburne University of Technology, Melbourne,
1567fef78bSLawrence Stewart * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
1667fef78bSLawrence Stewart *
1767fef78bSLawrence Stewart * Redistribution and use in source and binary forms, with or without
1867fef78bSLawrence Stewart * modification, are permitted provided that the following conditions
1967fef78bSLawrence Stewart * are met:
2067fef78bSLawrence Stewart * 1. Redistributions of source code must retain the above copyright
2167fef78bSLawrence Stewart * notice, this list of conditions and the following disclaimer.
2267fef78bSLawrence Stewart * 2. Redistributions in binary form must reproduce the above copyright
2367fef78bSLawrence Stewart * notice, this list of conditions and the following disclaimer in the
2467fef78bSLawrence Stewart * documentation and/or other materials provided with the distribution.
2567fef78bSLawrence Stewart *
2667fef78bSLawrence Stewart * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2767fef78bSLawrence Stewart * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2867fef78bSLawrence Stewart * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2967fef78bSLawrence Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3067fef78bSLawrence Stewart * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3167fef78bSLawrence Stewart * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3267fef78bSLawrence Stewart * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3367fef78bSLawrence Stewart * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3467fef78bSLawrence Stewart * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3567fef78bSLawrence Stewart * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3667fef78bSLawrence Stewart * SUCH DAMAGE.
3767fef78bSLawrence Stewart */
3867fef78bSLawrence Stewart
3967fef78bSLawrence Stewart #ifndef _NETINET_CC_CUBIC_H_
4067fef78bSLawrence Stewart #define _NETINET_CC_CUBIC_H_
4167fef78bSLawrence Stewart
42c968c769SMichael Tuexen #include <sys/limits.h>
43c968c769SMichael Tuexen
4467fef78bSLawrence Stewart /* Number of bits of precision for fixed point math calcs. */
4567fef78bSLawrence Stewart #define CUBIC_SHIFT 8
4667fef78bSLawrence Stewart
4767fef78bSLawrence Stewart #define CUBIC_SHIFT_4 32
4867fef78bSLawrence Stewart
4967fef78bSLawrence Stewart /* 0.5 << CUBIC_SHIFT. */
5067fef78bSLawrence Stewart #define RENO_BETA 128
5167fef78bSLawrence Stewart
5268ff29afSSean Bruno /* ~0.7 << CUBIC_SHIFT. */
5368ff29afSSean Bruno #define CUBIC_BETA 179
5467fef78bSLawrence Stewart
5568ff29afSSean Bruno /* ~0.3 << CUBIC_SHIFT. */
5668ff29afSSean Bruno #define ONE_SUB_CUBIC_BETA 77
5767fef78bSLawrence Stewart
5867fef78bSLawrence Stewart /* 3 * ONE_SUB_CUBIC_BETA. */
5968ff29afSSean Bruno #define THREE_X_PT3 231
6067fef78bSLawrence Stewart
6167fef78bSLawrence Stewart /* (2 << CUBIC_SHIFT) - ONE_SUB_CUBIC_BETA. */
6268ff29afSSean Bruno #define TWO_SUB_PT3 435
6367fef78bSLawrence Stewart
6467fef78bSLawrence Stewart /* ~0.4 << CUBIC_SHIFT. */
6567fef78bSLawrence Stewart #define CUBIC_C_FACTOR 102
6667fef78bSLawrence Stewart
6768ff29afSSean Bruno /* CUBIC fast convergence factor: (1+beta_cubic)/2. */
6868ff29afSSean Bruno #define CUBIC_FC_FACTOR 217
6967fef78bSLawrence Stewart
7067fef78bSLawrence Stewart /* Don't trust s_rtt until this many rtt samples have been taken. */
7167fef78bSLawrence Stewart #define CUBIC_MIN_RTT_SAMPLES 8
7267fef78bSLawrence Stewart
73c968c769SMichael Tuexen /*
74c968c769SMichael Tuexen * (2^21)^3 is long max. Dividing (2^63) by Cubic_C_factor
75c968c769SMichael Tuexen * and taking cube-root yields 448845 as the effective useful limit
76c968c769SMichael Tuexen */
77c968c769SMichael Tuexen #define CUBED_ROOT_MAX_ULONG 448845
78c968c769SMichael Tuexen
79a9696510SRandall Stewart /* Flags used in the cubic structure */
80a9696510SRandall Stewart #define CUBICFLAG_CONG_EVENT 0x00000001 /* congestion experienced */
81a9696510SRandall Stewart #define CUBICFLAG_IN_SLOWSTART 0x00000002 /* in slow start */
82a9696510SRandall Stewart #define CUBICFLAG_IN_APPLIMIT 0x00000004 /* application limited */
83a9696510SRandall Stewart #define CUBICFLAG_RTO_EVENT 0x00000008 /* RTO experienced */
84a9696510SRandall Stewart #define CUBICFLAG_HYSTART_ENABLED 0x00000010 /* Hystart++ is enabled */
85a9696510SRandall Stewart #define CUBICFLAG_HYSTART_IN_CSS 0x00000020 /* We are in Hystart++ CSS */
86*ee450610SCheng Cui #define CUBICFLAG_IN_TF 0x00000040 /* We are in TCP friendly region */
87a9696510SRandall Stewart
88a9696510SRandall Stewart /* Kernel only bits */
89a9696510SRandall Stewart #ifdef _KERNEL
90a9696510SRandall Stewart struct cubic {
91ea6d0de2SRichard Scheffenegger /* CUBIC K in fixed point form with CUBIC_SHIFT worth of precision. */
92a9696510SRandall Stewart int64_t K;
93a3aa6f65SCheng Cui /* Sum of RTT samples across an epoch in usecs. */
94a3aa6f65SCheng Cui int64_t sum_rtt_usecs;
95eb5bfdd0SRichard Scheffenegger /* Size of cwnd just before cwnd was reduced in the last congestion event */
96eb5bfdd0SRichard Scheffenegger uint64_t W_max;
97eb5bfdd0SRichard Scheffenegger /* The cwnd at the beginning of the current congestion avoidance stage */
98eb5bfdd0SRichard Scheffenegger uint64_t cwnd_epoch;
99a9696510SRandall Stewart /* various flags */
100a9696510SRandall Stewart uint32_t flags;
101a3aa6f65SCheng Cui /* Minimum observed rtt in usecs. */
102a3aa6f65SCheng Cui int min_rtt_usecs;
103a9696510SRandall Stewart /* Mean observed rtt between congestion epochs. */
104a3aa6f65SCheng Cui int mean_rtt_usecs;
105a9696510SRandall Stewart /* ACKs since last congestion event. */
106a9696510SRandall Stewart int epoch_ack_count;
107eb5bfdd0SRichard Scheffenegger /* Timestamp (in ticks) at which the current CA epoch started. */
108eb5bfdd0SRichard Scheffenegger int t_epoch;
109eb5bfdd0SRichard Scheffenegger /* Timestamp (in ticks) at which the previous CA epoch started. */
110eb5bfdd0SRichard Scheffenegger int undo_t_epoch;
111eb5bfdd0SRichard Scheffenegger /* Few variables to restore the state after RTO_ERR */
112eb5bfdd0SRichard Scheffenegger int64_t undo_K;
113eb5bfdd0SRichard Scheffenegger uint64_t undo_W_max;
114eb5bfdd0SRichard Scheffenegger uint64_t undo_cwnd_epoch;
115a9696510SRandall Stewart uint32_t css_baseline_minrtt;
116a9696510SRandall Stewart uint32_t css_current_round_minrtt;
117a9696510SRandall Stewart uint32_t css_lastround_minrtt;
118a9696510SRandall Stewart uint32_t css_rttsample_count;
119a9696510SRandall Stewart uint32_t css_entered_at_round;
120a9696510SRandall Stewart uint32_t css_current_round;
121a9696510SRandall Stewart uint32_t css_fas_at_css_entry;
122a9696510SRandall Stewart uint32_t css_lowrtt_fas;
123a9696510SRandall Stewart uint32_t css_last_fas;
124a9696510SRandall Stewart };
125a9696510SRandall Stewart #endif
126a9696510SRandall Stewart
12767fef78bSLawrence Stewart /* Userland only bits. */
12867fef78bSLawrence Stewart #ifndef _KERNEL
12967fef78bSLawrence Stewart
13067fef78bSLawrence Stewart extern int hz;
13167fef78bSLawrence Stewart
13267fef78bSLawrence Stewart /*
13367fef78bSLawrence Stewart * Implementation based on the formulae found in the CUBIC Internet Draft
13468ff29afSSean Bruno * "draft-ietf-tcpm-cubic-04".
13567fef78bSLawrence Stewart *
13667fef78bSLawrence Stewart */
13767fef78bSLawrence Stewart
13867fef78bSLawrence Stewart static __inline float
theoretical_cubic_k(double wmax_pkts)13967fef78bSLawrence Stewart theoretical_cubic_k(double wmax_pkts)
14067fef78bSLawrence Stewart {
14167fef78bSLawrence Stewart double C;
14267fef78bSLawrence Stewart
14367fef78bSLawrence Stewart C = 0.4;
14467fef78bSLawrence Stewart
14568ff29afSSean Bruno return (pow((wmax_pkts * 0.3) / C, (1.0 / 3.0)) * pow(2, CUBIC_SHIFT));
14667fef78bSLawrence Stewart }
14767fef78bSLawrence Stewart
14867fef78bSLawrence Stewart static __inline unsigned long
theoretical_cubic_cwnd(int ticks_since_epoch,unsigned long wmax,uint32_t smss)149eb5bfdd0SRichard Scheffenegger theoretical_cubic_cwnd(int ticks_since_epoch, unsigned long wmax, uint32_t smss)
15067fef78bSLawrence Stewart {
15167fef78bSLawrence Stewart double C, wmax_pkts;
15267fef78bSLawrence Stewart
15367fef78bSLawrence Stewart C = 0.4;
15467fef78bSLawrence Stewart wmax_pkts = wmax / (double)smss;
15567fef78bSLawrence Stewart
15667fef78bSLawrence Stewart return (smss * (wmax_pkts +
157eb5bfdd0SRichard Scheffenegger (C * pow(ticks_since_epoch / (double)hz -
15867fef78bSLawrence Stewart theoretical_cubic_k(wmax_pkts) / pow(2, CUBIC_SHIFT), 3.0))));
15967fef78bSLawrence Stewart }
16067fef78bSLawrence Stewart
16167fef78bSLawrence Stewart static __inline unsigned long
theoretical_reno_cwnd(int ticks_since_epoch,int rtt_ticks,unsigned long wmax,uint32_t smss)162eb5bfdd0SRichard Scheffenegger theoretical_reno_cwnd(int ticks_since_epoch, int rtt_ticks, unsigned long wmax,
16367fef78bSLawrence Stewart uint32_t smss)
16467fef78bSLawrence Stewart {
16567fef78bSLawrence Stewart
166eb5bfdd0SRichard Scheffenegger return ((wmax * 0.5) + ((ticks_since_epoch / (float)rtt_ticks) * smss));
16767fef78bSLawrence Stewart }
16867fef78bSLawrence Stewart
16967fef78bSLawrence Stewart static __inline unsigned long
theoretical_tf_cwnd(int ticks_since_epoch,int rtt_ticks,unsigned long wmax,uint32_t smss)170eb5bfdd0SRichard Scheffenegger theoretical_tf_cwnd(int ticks_since_epoch, int rtt_ticks, unsigned long wmax,
17167fef78bSLawrence Stewart uint32_t smss)
17267fef78bSLawrence Stewart {
17367fef78bSLawrence Stewart
17468ff29afSSean Bruno return ((wmax * 0.7) + ((3 * 0.3) / (2 - 0.3) *
175eb5bfdd0SRichard Scheffenegger (ticks_since_epoch / (float)rtt_ticks) * smss));
17667fef78bSLawrence Stewart }
17767fef78bSLawrence Stewart
17867fef78bSLawrence Stewart #endif /* !_KERNEL */
17967fef78bSLawrence Stewart
18067fef78bSLawrence Stewart /*
18167fef78bSLawrence Stewart * Compute the CUBIC K value used in the cwnd calculation, using an
18267fef78bSLawrence Stewart * implementation of eqn 2 in the I-D. The method used
18367fef78bSLawrence Stewart * here is adapted from Apple Computer Technical Report #KT-32.
18467fef78bSLawrence Stewart */
18567fef78bSLawrence Stewart static __inline int64_t
cubic_k(unsigned long wmax_pkts)18667fef78bSLawrence Stewart cubic_k(unsigned long wmax_pkts)
18767fef78bSLawrence Stewart {
18867fef78bSLawrence Stewart int64_t s, K;
18967fef78bSLawrence Stewart uint16_t p;
19067fef78bSLawrence Stewart
19167fef78bSLawrence Stewart K = s = 0;
19267fef78bSLawrence Stewart p = 0;
19367fef78bSLawrence Stewart
19467fef78bSLawrence Stewart /* (wmax * beta)/C with CUBIC_SHIFT worth of precision. */
19567fef78bSLawrence Stewart s = ((wmax_pkts * ONE_SUB_CUBIC_BETA) << CUBIC_SHIFT) / CUBIC_C_FACTOR;
19667fef78bSLawrence Stewart
19767fef78bSLawrence Stewart /* Rebase s to be between 1 and 1/8 with a shift of CUBIC_SHIFT. */
19867fef78bSLawrence Stewart while (s >= 256) {
19967fef78bSLawrence Stewart s >>= 3;
20067fef78bSLawrence Stewart p++;
20167fef78bSLawrence Stewart }
20267fef78bSLawrence Stewart
20367fef78bSLawrence Stewart /*
20467fef78bSLawrence Stewart * Some magic constants taken from the Apple TR with appropriate
20567fef78bSLawrence Stewart * shifts: 275 == 1.072302 << CUBIC_SHIFT, 98 == 0.3812513 <<
20667fef78bSLawrence Stewart * CUBIC_SHIFT, 120 == 0.46946116 << CUBIC_SHIFT.
20767fef78bSLawrence Stewart */
20867fef78bSLawrence Stewart K = (((s * 275) >> CUBIC_SHIFT) + 98) -
20967fef78bSLawrence Stewart (((s * s * 120) >> CUBIC_SHIFT) >> CUBIC_SHIFT);
21067fef78bSLawrence Stewart
21167fef78bSLawrence Stewart /* Multiply by 2^p to undo the rebasing of s from above. */
21267fef78bSLawrence Stewart return (K <<= p);
21367fef78bSLawrence Stewart }
21467fef78bSLawrence Stewart
21567fef78bSLawrence Stewart /*
21667fef78bSLawrence Stewart * Compute the new cwnd value using an implementation of eqn 1 from the I-D.
21767fef78bSLawrence Stewart * Thanks to Kip Macy for help debugging this function.
21851e712f8SHiren Panchasara *
21951e712f8SHiren Panchasara * XXXLAS: Characterise bounds for overflow.
22067fef78bSLawrence Stewart */
22167fef78bSLawrence Stewart static __inline unsigned long
cubic_cwnd(int usecs_since_epoch,unsigned long wmax,uint32_t smss,int64_t K)222eb5bfdd0SRichard Scheffenegger cubic_cwnd(int usecs_since_epoch, unsigned long wmax, uint32_t smss, int64_t K)
22367fef78bSLawrence Stewart {
22467fef78bSLawrence Stewart int64_t cwnd;
22567fef78bSLawrence Stewart
22667fef78bSLawrence Stewart /* K is in fixed point form with CUBIC_SHIFT worth of precision. */
22767fef78bSLawrence Stewart
22867fef78bSLawrence Stewart /* t - K, with CUBIC_SHIFT worth of precision. */
229eb5bfdd0SRichard Scheffenegger cwnd = (((int64_t)usecs_since_epoch << CUBIC_SHIFT) - (K * hz * tick)) /
230a3aa6f65SCheng Cui (hz * tick);
231c968c769SMichael Tuexen
232c968c769SMichael Tuexen if (cwnd > CUBED_ROOT_MAX_ULONG)
233c968c769SMichael Tuexen return INT_MAX;
234c968c769SMichael Tuexen if (cwnd < -CUBED_ROOT_MAX_ULONG)
235c968c769SMichael Tuexen return 0;
23667fef78bSLawrence Stewart
23767fef78bSLawrence Stewart /* (t - K)^3, with CUBIC_SHIFT^3 worth of precision. */
23867fef78bSLawrence Stewart cwnd *= (cwnd * cwnd);
23967fef78bSLawrence Stewart
24067fef78bSLawrence Stewart /*
24167fef78bSLawrence Stewart * C(t - K)^3 + wmax
24267fef78bSLawrence Stewart * The down shift by CUBIC_SHIFT_4 is because cwnd has 4 lots of
24367fef78bSLawrence Stewart * CUBIC_SHIFT included in the value. 3 from the cubing of cwnd above,
24467fef78bSLawrence Stewart * and an extra from multiplying through by CUBIC_C_FACTOR.
24567fef78bSLawrence Stewart */
24667fef78bSLawrence Stewart
247c968c769SMichael Tuexen cwnd = ((cwnd * CUBIC_C_FACTOR) >> CUBIC_SHIFT_4) * smss + wmax;
248c968c769SMichael Tuexen
249c968c769SMichael Tuexen /*
250c968c769SMichael Tuexen * for negative cwnd, limiting to zero as lower bound
251c968c769SMichael Tuexen */
252c968c769SMichael Tuexen return (lmax(0,cwnd));
25367fef78bSLawrence Stewart }
25467fef78bSLawrence Stewart
25567fef78bSLawrence Stewart /*
256a3aa6f65SCheng Cui * Compute an approximation of the NewReno cwnd some number of usecs after a
25767fef78bSLawrence Stewart * congestion event. RTT should be the average RTT estimate for the path
25867fef78bSLawrence Stewart * measured over the previous congestion epoch and wmax is the value of cwnd at
25967fef78bSLawrence Stewart * the last congestion event. The "TCP friendly" concept in the CUBIC I-D is
26067fef78bSLawrence Stewart * rather tricky to understand and it turns out this function is not required.
26167fef78bSLawrence Stewart * It is left here for reference.
262a3aa6f65SCheng Cui *
263a3aa6f65SCheng Cui * XXX: Not used
26467fef78bSLawrence Stewart */
26567fef78bSLawrence Stewart static __inline unsigned long
reno_cwnd(int usecs_since_epoch,int rtt_usecs,unsigned long wmax,uint32_t smss)266eb5bfdd0SRichard Scheffenegger reno_cwnd(int usecs_since_epoch, int rtt_usecs, unsigned long wmax,
26767fef78bSLawrence Stewart uint32_t smss)
26867fef78bSLawrence Stewart {
26967fef78bSLawrence Stewart
27067fef78bSLawrence Stewart /*
27167fef78bSLawrence Stewart * For NewReno, beta = 0.5, therefore: W_tcp(t) = wmax*0.5 + t/RTT
27267fef78bSLawrence Stewart * W_tcp(t) deals with cwnd/wmax in pkts, so because our cwnd is in
27367fef78bSLawrence Stewart * bytes, we have to multiply by smss.
27467fef78bSLawrence Stewart */
275eb5bfdd0SRichard Scheffenegger return (((wmax * RENO_BETA) + (((usecs_since_epoch * smss)
276a3aa6f65SCheng Cui << CUBIC_SHIFT) / rtt_usecs)) >> CUBIC_SHIFT);
27767fef78bSLawrence Stewart }
27867fef78bSLawrence Stewart
27967fef78bSLawrence Stewart /*
280*ee450610SCheng Cui * Compute the "TCP friendly" cwnd by newreno in congestion avoidance state.
28167fef78bSLawrence Stewart */
28267fef78bSLawrence Stewart static __inline unsigned long
tf_cwnd(struct cc_var * ccv)283*ee450610SCheng Cui tf_cwnd(struct cc_var *ccv)
28467fef78bSLawrence Stewart {
285*ee450610SCheng Cui /* newreno is "TCP friendly" */
286*ee450610SCheng Cui return newreno_cc_cwnd_in_cong_avoid(ccv);
28767fef78bSLawrence Stewart }
28867fef78bSLawrence Stewart
28967fef78bSLawrence Stewart #endif /* _NETINET_CC_CUBIC_H_ */
290