]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/cc/cc_cubic.h
Upgrade Unbound to 1.7.0. More to follow.
[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 #include <sys/limits.h>
45
46 /* Number of bits of precision for fixed point math calcs. */
47 #define CUBIC_SHIFT             8
48
49 #define CUBIC_SHIFT_4           32
50
51 /* 0.5 << CUBIC_SHIFT. */
52 #define RENO_BETA               128
53
54 /* ~0.7 << CUBIC_SHIFT. */
55 #define CUBIC_BETA              179
56
57 /* ~0.3 << CUBIC_SHIFT. */
58 #define ONE_SUB_CUBIC_BETA      77
59
60 /* 3 * ONE_SUB_CUBIC_BETA. */
61 #define THREE_X_PT3             231
62
63 /* (2 << CUBIC_SHIFT) - ONE_SUB_CUBIC_BETA. */
64 #define TWO_SUB_PT3             435
65
66 /* ~0.4 << CUBIC_SHIFT. */
67 #define CUBIC_C_FACTOR          102
68
69 /* CUBIC fast convergence factor: (1+beta_cubic)/2. */
70 #define CUBIC_FC_FACTOR         217
71
72 /* Don't trust s_rtt until this many rtt samples have been taken. */
73 #define CUBIC_MIN_RTT_SAMPLES   8
74
75 /* Userland only bits. */
76 #ifndef _KERNEL
77
78 extern int hz;
79
80 /*
81  * Implementation based on the formulae found in the CUBIC Internet Draft
82  * "draft-ietf-tcpm-cubic-04".
83  *
84  */
85
86 static __inline float
87 theoretical_cubic_k(double wmax_pkts)
88 {
89         double C;
90
91         C = 0.4;
92
93         return (pow((wmax_pkts * 0.3) / C, (1.0 / 3.0)) * pow(2, CUBIC_SHIFT));
94 }
95
96 static __inline unsigned long
97 theoretical_cubic_cwnd(int ticks_since_cong, unsigned long wmax, uint32_t smss)
98 {
99         double C, wmax_pkts;
100
101         C = 0.4;
102         wmax_pkts = wmax / (double)smss;
103
104         return (smss * (wmax_pkts +
105             (C * pow(ticks_since_cong / (double)hz -
106             theoretical_cubic_k(wmax_pkts) / pow(2, CUBIC_SHIFT), 3.0))));
107 }
108
109 static __inline unsigned long
110 theoretical_reno_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
111     uint32_t smss)
112 {
113
114         return ((wmax * 0.5) + ((ticks_since_cong / (float)rtt_ticks) * smss));
115 }
116
117 static __inline unsigned long
118 theoretical_tf_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
119     uint32_t smss)
120 {
121
122         return ((wmax * 0.7) + ((3 * 0.3) / (2 - 0.3) *
123             (ticks_since_cong / (float)rtt_ticks) * smss));
124 }
125
126 #endif /* !_KERNEL */
127
128 /*
129  * Compute the CUBIC K value used in the cwnd calculation, using an
130  * implementation of eqn 2 in the I-D. The method used
131  * here is adapted from Apple Computer Technical Report #KT-32.
132  */
133 static __inline int64_t
134 cubic_k(unsigned long wmax_pkts)
135 {
136         int64_t s, K;
137         uint16_t p;
138
139         K = s = 0;
140         p = 0;
141
142         /* (wmax * beta)/C with CUBIC_SHIFT worth of precision. */
143         s = ((wmax_pkts * ONE_SUB_CUBIC_BETA) << CUBIC_SHIFT) / CUBIC_C_FACTOR;
144
145         /* Rebase s to be between 1 and 1/8 with a shift of CUBIC_SHIFT. */
146         while (s >= 256) {
147                 s >>= 3;
148                 p++;
149         }
150
151         /*
152          * Some magic constants taken from the Apple TR with appropriate
153          * shifts: 275 == 1.072302 << CUBIC_SHIFT, 98 == 0.3812513 <<
154          * CUBIC_SHIFT, 120 == 0.46946116 << CUBIC_SHIFT.
155          */
156         K = (((s * 275) >> CUBIC_SHIFT) + 98) -
157             (((s * s * 120) >> CUBIC_SHIFT) >> CUBIC_SHIFT);
158
159         /* Multiply by 2^p to undo the rebasing of s from above. */
160         return (K <<= p);
161 }
162
163 /*
164  * Compute the new cwnd value using an implementation of eqn 1 from the I-D.
165  * Thanks to Kip Macy for help debugging this function.
166  */
167 static __inline unsigned long
168 cubic_cwnd(int ticks_since_cong, unsigned long wmax, uint32_t smss, int64_t K)
169 {
170         int64_t cwnd;
171
172         /* K is in fixed point form with CUBIC_SHIFT worth of precision. */
173
174         /* t - K, with CUBIC_SHIFT worth of precision. */
175         cwnd = ((int64_t)(ticks_since_cong << CUBIC_SHIFT) - (K * hz)) / hz;
176
177         /* moved this calculation up because it cannot overflow or underflow */
178         cwnd *= CUBIC_C_FACTOR * smss;
179
180         if (cwnd > 2097151) /* 2^21 cubed is long max */
181                 return INT_MAX;
182
183         if (cwnd < -2097152) /* -2^21 cubed is long min */
184                 return smss;
185
186         /* (t - K)^3, with CUBIC_SHIFT^3 worth of precision. */
187         cwnd *= (cwnd * cwnd);
188
189         /*
190          * C(t - K)^3 + wmax
191          * The down shift by CUBIC_SHIFT_4 is because cwnd has 4 lots of
192          * CUBIC_SHIFT included in the value. 3 from the cubing of cwnd above,
193          * and an extra from multiplying through by CUBIC_C_FACTOR.
194          *
195          * The original formula was this:
196          * cwnd = ((cwnd * CUBIC_C_FACTOR * smss) >> CUBIC_SHIFT_4) + wmax;
197          *
198          * CUBIC_C_FACTOR and smss factors were moved up to an earlier
199          * calculation to simplify overflow and underflow detection.
200          */
201         cwnd = (cwnd >> CUBIC_SHIFT_4) + wmax;
202
203         if (cwnd < 0)
204                 return 1;
205
206         return ((unsigned long)cwnd);
207 }
208
209 /*
210  * Compute an approximation of the NewReno cwnd some number of ticks after a
211  * congestion event. RTT should be the average RTT estimate for the path
212  * measured over the previous congestion epoch and wmax is the value of cwnd at
213  * the last congestion event. The "TCP friendly" concept in the CUBIC I-D is
214  * rather tricky to understand and it turns out this function is not required.
215  * It is left here for reference.
216  */
217 static __inline unsigned long
218 reno_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
219     uint32_t smss)
220 {
221
222         /*
223          * For NewReno, beta = 0.5, therefore: W_tcp(t) = wmax*0.5 + t/RTT
224          * W_tcp(t) deals with cwnd/wmax in pkts, so because our cwnd is in
225          * bytes, we have to multiply by smss.
226          */
227         return (((wmax * RENO_BETA) + (((ticks_since_cong * smss)
228             << CUBIC_SHIFT) / rtt_ticks)) >> CUBIC_SHIFT);
229 }
230
231 /*
232  * Compute an approximation of the "TCP friendly" cwnd some number of ticks
233  * after a congestion event that is designed to yield the same average cwnd as
234  * NewReno while using CUBIC's beta of 0.7. RTT should be the average RTT
235  * estimate for the path measured over the previous congestion epoch and wmax is
236  * the value of cwnd at the last congestion event.
237  */
238 static __inline unsigned long
239 tf_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
240     uint32_t smss)
241 {
242
243         /* Equation 4 of I-D. */
244         return (((wmax * CUBIC_BETA) + (((THREE_X_PT3 * ticks_since_cong *
245             smss) << CUBIC_SHIFT) / TWO_SUB_PT3 / rtt_ticks)) >> CUBIC_SHIFT);
246 }
247
248 #endif /* _NETINET_CC_CUBIC_H_ */