2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org>
5 * Copyright (c) 2010 The FreeBSD Foundation
8 * This software was developed by Lawrence Stewart while studying at the Centre
9 * for Advanced Internet Architectures, Swinburne University of Technology, made
10 * possible in part by a grant from the Cisco University Research Program Fund
11 * at Community Foundation Silicon Valley.
13 * Portions of this software were developed at the Centre for Advanced
14 * Internet Architectures, Swinburne University of Technology, Melbourne,
15 * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #ifndef _NETINET_CC_CUBIC_H_
42 #define _NETINET_CC_CUBIC_H_
44 #include <sys/limits.h>
46 /* Number of bits of precision for fixed point math calcs. */
49 #define CUBIC_SHIFT_4 32
51 /* 0.5 << CUBIC_SHIFT. */
54 /* ~0.7 << CUBIC_SHIFT. */
55 #define CUBIC_BETA 179
57 /* ~0.3 << CUBIC_SHIFT. */
58 #define ONE_SUB_CUBIC_BETA 77
60 /* 3 * ONE_SUB_CUBIC_BETA. */
61 #define THREE_X_PT3 231
63 /* (2 << CUBIC_SHIFT) - ONE_SUB_CUBIC_BETA. */
64 #define TWO_SUB_PT3 435
66 /* ~0.4 << CUBIC_SHIFT. */
67 #define CUBIC_C_FACTOR 102
69 /* CUBIC fast convergence factor: (1+beta_cubic)/2. */
70 #define CUBIC_FC_FACTOR 217
72 /* Don't trust s_rtt until this many rtt samples have been taken. */
73 #define CUBIC_MIN_RTT_SAMPLES 8
76 * (2^21)^3 is long max. Dividing (2^63) by Cubic_C_factor
77 * and taking cube-root yields 448845 as the effective useful limit
79 #define CUBED_ROOT_MAX_ULONG 448845
81 /* Userland only bits. */
87 * Implementation based on the formulae found in the CUBIC Internet Draft
88 * "draft-ietf-tcpm-cubic-04".
93 theoretical_cubic_k(double wmax_pkts)
99 return (pow((wmax_pkts * 0.3) / C, (1.0 / 3.0)) * pow(2, CUBIC_SHIFT));
102 static __inline unsigned long
103 theoretical_cubic_cwnd(int ticks_since_cong, unsigned long wmax, uint32_t smss)
108 wmax_pkts = wmax / (double)smss;
110 return (smss * (wmax_pkts +
111 (C * pow(ticks_since_cong / (double)hz -
112 theoretical_cubic_k(wmax_pkts) / pow(2, CUBIC_SHIFT), 3.0))));
115 static __inline unsigned long
116 theoretical_reno_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
120 return ((wmax * 0.5) + ((ticks_since_cong / (float)rtt_ticks) * smss));
123 static __inline unsigned long
124 theoretical_tf_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
128 return ((wmax * 0.7) + ((3 * 0.3) / (2 - 0.3) *
129 (ticks_since_cong / (float)rtt_ticks) * smss));
132 #endif /* !_KERNEL */
135 * Compute the CUBIC K value used in the cwnd calculation, using an
136 * implementation of eqn 2 in the I-D. The method used
137 * here is adapted from Apple Computer Technical Report #KT-32.
139 static __inline int64_t
140 cubic_k(unsigned long wmax_pkts)
148 /* (wmax * beta)/C with CUBIC_SHIFT worth of precision. */
149 s = ((wmax_pkts * ONE_SUB_CUBIC_BETA) << CUBIC_SHIFT) / CUBIC_C_FACTOR;
151 /* Rebase s to be between 1 and 1/8 with a shift of CUBIC_SHIFT. */
158 * Some magic constants taken from the Apple TR with appropriate
159 * shifts: 275 == 1.072302 << CUBIC_SHIFT, 98 == 0.3812513 <<
160 * CUBIC_SHIFT, 120 == 0.46946116 << CUBIC_SHIFT.
162 K = (((s * 275) >> CUBIC_SHIFT) + 98) -
163 (((s * s * 120) >> CUBIC_SHIFT) >> CUBIC_SHIFT);
165 /* Multiply by 2^p to undo the rebasing of s from above. */
170 * Compute the new cwnd value using an implementation of eqn 1 from the I-D.
171 * Thanks to Kip Macy for help debugging this function.
173 * XXXLAS: Characterise bounds for overflow.
175 static __inline unsigned long
176 cubic_cwnd(int ticks_since_cong, unsigned long wmax, uint32_t smss, int64_t K)
180 /* K is in fixed point form with CUBIC_SHIFT worth of precision. */
182 /* t - K, with CUBIC_SHIFT worth of precision. */
183 cwnd = (((int64_t)ticks_since_cong << CUBIC_SHIFT) - (K * hz)) / hz;
185 if (cwnd > CUBED_ROOT_MAX_ULONG)
187 if (cwnd < -CUBED_ROOT_MAX_ULONG)
190 /* (t - K)^3, with CUBIC_SHIFT^3 worth of precision. */
191 cwnd *= (cwnd * cwnd);
195 * The down shift by CUBIC_SHIFT_4 is because cwnd has 4 lots of
196 * CUBIC_SHIFT included in the value. 3 from the cubing of cwnd above,
197 * and an extra from multiplying through by CUBIC_C_FACTOR.
200 cwnd = ((cwnd * CUBIC_C_FACTOR) >> CUBIC_SHIFT_4) * smss + wmax;
203 * for negative cwnd, limiting to zero as lower bound
205 return (lmax(0,cwnd));
209 * Compute an approximation of the NewReno cwnd some number of ticks after a
210 * congestion event. RTT should be the average RTT estimate for the path
211 * measured over the previous congestion epoch and wmax is the value of cwnd at
212 * the last congestion event. The "TCP friendly" concept in the CUBIC I-D is
213 * rather tricky to understand and it turns out this function is not required.
214 * It is left here for reference.
216 static __inline unsigned long
217 reno_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
222 * For NewReno, beta = 0.5, therefore: W_tcp(t) = wmax*0.5 + t/RTT
223 * W_tcp(t) deals with cwnd/wmax in pkts, so because our cwnd is in
224 * bytes, we have to multiply by smss.
226 return (((wmax * RENO_BETA) + (((ticks_since_cong * smss)
227 << CUBIC_SHIFT) / rtt_ticks)) >> CUBIC_SHIFT);
231 * Compute an approximation of the "TCP friendly" cwnd some number of ticks
232 * after a congestion event that is designed to yield the same average cwnd as
233 * NewReno while using CUBIC's beta of 0.7. RTT should be the average RTT
234 * estimate for the path measured over the previous congestion epoch and wmax is
235 * the value of cwnd at the last congestion event.
237 static __inline unsigned long
238 tf_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
242 /* Equation 4 of I-D. */
243 return (((wmax * CUBIC_BETA) +
244 (((THREE_X_PT3 * (unsigned long)ticks_since_cong *
245 (unsigned long)smss) << CUBIC_SHIFT) / (TWO_SUB_PT3 * rtt_ticks)))
249 #endif /* _NETINET_CC_CUBIC_H_ */