]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/cc/cc_cubic.h
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
[FreeBSD/FreeBSD.git] / sys / netinet / cc / cc_cubic.h
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org>
5  * Copyright (c) 2010 The FreeBSD Foundation
6  * All rights reserved.
7  *
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.
12  *
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.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
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.
25  *
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
36  * SUCH DAMAGE.
37  *
38  * $FreeBSD$
39  */
40
41 #ifndef _NETINET_CC_CUBIC_H_
42 #define _NETINET_CC_CUBIC_H_
43
44 /* Number of bits of precision for fixed point math calcs. */
45 #define CUBIC_SHIFT             8
46
47 #define CUBIC_SHIFT_4           32
48
49 /* 0.5 << CUBIC_SHIFT. */
50 #define RENO_BETA               128
51
52 /* ~0.8 << CUBIC_SHIFT. */
53 #define CUBIC_BETA              204
54
55 /* ~0.2 << CUBIC_SHIFT. */
56 #define ONE_SUB_CUBIC_BETA      51
57
58 /* 3 * ONE_SUB_CUBIC_BETA. */
59 #define THREE_X_PT2             153
60
61 /* (2 << CUBIC_SHIFT) - ONE_SUB_CUBIC_BETA. */
62 #define TWO_SUB_PT2             461
63
64 /* ~0.4 << CUBIC_SHIFT. */
65 #define CUBIC_C_FACTOR          102
66
67 /* CUBIC fast convergence factor: ~0.9 << CUBIC_SHIFT. */
68 #define CUBIC_FC_FACTOR         230
69
70 /* Don't trust s_rtt until this many rtt samples have been taken. */
71 #define CUBIC_MIN_RTT_SAMPLES   8
72
73 /* Userland only bits. */
74 #ifndef _KERNEL
75
76 extern int hz;
77
78 /*
79  * Implementation based on the formulae found in the CUBIC Internet Draft
80  * "draft-rhee-tcpm-cubic-02".
81  *
82  * Note BETA used in cc_cubic is equal to (1-beta) in the I-D
83  */
84
85 static __inline float
86 theoretical_cubic_k(double wmax_pkts)
87 {
88         double C;
89
90         C = 0.4;
91
92         return (pow((wmax_pkts * 0.2) / C, (1.0 / 3.0)) * pow(2, CUBIC_SHIFT));
93 }
94
95 static __inline unsigned long
96 theoretical_cubic_cwnd(int ticks_since_cong, unsigned long wmax, uint32_t smss)
97 {
98         double C, wmax_pkts;
99
100         C = 0.4;
101         wmax_pkts = wmax / (double)smss;
102
103         return (smss * (wmax_pkts +
104             (C * pow(ticks_since_cong / (double)hz -
105             theoretical_cubic_k(wmax_pkts) / pow(2, CUBIC_SHIFT), 3.0))));
106 }
107
108 static __inline unsigned long
109 theoretical_reno_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
110     uint32_t smss)
111 {
112
113         return ((wmax * 0.5) + ((ticks_since_cong / (float)rtt_ticks) * smss));
114 }
115
116 static __inline unsigned long
117 theoretical_tf_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
118     uint32_t smss)
119 {
120
121         return ((wmax * 0.8) + ((3 * 0.2) / (2 - 0.2) *
122             (ticks_since_cong / (float)rtt_ticks) * smss));
123 }
124
125 #endif /* !_KERNEL */
126
127 /*
128  * Compute the CUBIC K value used in the cwnd calculation, using an
129  * implementation of eqn 2 in the I-D. The method used
130  * here is adapted from Apple Computer Technical Report #KT-32.
131  */
132 static __inline int64_t
133 cubic_k(unsigned long wmax_pkts)
134 {
135         int64_t s, K;
136         uint16_t p;
137
138         K = s = 0;
139         p = 0;
140
141         /* (wmax * beta)/C with CUBIC_SHIFT worth of precision. */
142         s = ((wmax_pkts * ONE_SUB_CUBIC_BETA) << CUBIC_SHIFT) / CUBIC_C_FACTOR;
143
144         /* Rebase s to be between 1 and 1/8 with a shift of CUBIC_SHIFT. */
145         while (s >= 256) {
146                 s >>= 3;
147                 p++;
148         }
149
150         /*
151          * Some magic constants taken from the Apple TR with appropriate
152          * shifts: 275 == 1.072302 << CUBIC_SHIFT, 98 == 0.3812513 <<
153          * CUBIC_SHIFT, 120 == 0.46946116 << CUBIC_SHIFT.
154          */
155         K = (((s * 275) >> CUBIC_SHIFT) + 98) -
156             (((s * s * 120) >> CUBIC_SHIFT) >> CUBIC_SHIFT);
157
158         /* Multiply by 2^p to undo the rebasing of s from above. */
159         return (K <<= p);
160 }
161
162 /*
163  * Compute the new cwnd value using an implementation of eqn 1 from the I-D.
164  * Thanks to Kip Macy for help debugging this function.
165  *
166  * XXXLAS: Characterise bounds for overflow.
167  */
168 static __inline unsigned long
169 cubic_cwnd(int ticks_since_cong, unsigned long wmax, uint32_t smss, int64_t K)
170 {
171         int64_t cwnd;
172
173         /* K is in fixed point form with CUBIC_SHIFT worth of precision. */
174
175         /* t - K, with CUBIC_SHIFT worth of precision. */
176         cwnd = ((int64_t)(ticks_since_cong << CUBIC_SHIFT) - (K * hz)) / hz;
177
178         /* (t - K)^3, with CUBIC_SHIFT^3 worth of precision. */
179         cwnd *= (cwnd * cwnd);
180
181         /*
182          * C(t - K)^3 + wmax
183          * The down shift by CUBIC_SHIFT_4 is because cwnd has 4 lots of
184          * CUBIC_SHIFT included in the value. 3 from the cubing of cwnd above,
185          * and an extra from multiplying through by CUBIC_C_FACTOR.
186          */
187         cwnd = ((cwnd * CUBIC_C_FACTOR * smss) >> CUBIC_SHIFT_4) + wmax;
188
189         return ((unsigned long)cwnd);
190 }
191
192 /*
193  * Compute an approximation of the NewReno cwnd some number of ticks after a
194  * congestion event. RTT should be the average RTT estimate for the path
195  * measured over the previous congestion epoch and wmax is the value of cwnd at
196  * the last congestion event. The "TCP friendly" concept in the CUBIC I-D is
197  * rather tricky to understand and it turns out this function is not required.
198  * It is left here for reference.
199  */
200 static __inline unsigned long
201 reno_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
202     uint32_t smss)
203 {
204
205         /*
206          * For NewReno, beta = 0.5, therefore: W_tcp(t) = wmax*0.5 + t/RTT
207          * W_tcp(t) deals with cwnd/wmax in pkts, so because our cwnd is in
208          * bytes, we have to multiply by smss.
209          */
210         return (((wmax * RENO_BETA) + (((ticks_since_cong * smss)
211             << CUBIC_SHIFT) / rtt_ticks)) >> CUBIC_SHIFT);
212 }
213
214 /*
215  * Compute an approximation of the "TCP friendly" cwnd some number of ticks
216  * after a congestion event that is designed to yield the same average cwnd as
217  * NewReno while using CUBIC's beta of 0.8. RTT should be the average RTT
218  * estimate for the path measured over the previous congestion epoch and wmax is
219  * the value of cwnd at the last congestion event.
220  */
221 static __inline unsigned long
222 tf_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
223     uint32_t smss)
224 {
225
226         /* Equation 4 of I-D. */
227         return (((wmax * CUBIC_BETA) + (((THREE_X_PT2 * ticks_since_cong *
228             smss) << CUBIC_SHIFT) / TWO_SUB_PT2 / rtt_ticks)) >> CUBIC_SHIFT);
229 }
230
231 #endif /* _NETINET_CC_CUBIC_H_ */