]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/cc/cc_cubic.h
MFV: xz 5.2.9
[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 /*
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
78  */
79 #define CUBED_ROOT_MAX_ULONG    448845
80
81 /* Flags used in the cubic structure */
82 #define CUBICFLAG_CONG_EVENT            0x00000001      /* congestion experienced */
83 #define CUBICFLAG_IN_SLOWSTART          0x00000002      /* in slow start */
84 #define CUBICFLAG_IN_APPLIMIT           0x00000004      /* application limited */
85 #define CUBICFLAG_RTO_EVENT             0x00000008      /* RTO experienced */
86 #define CUBICFLAG_HYSTART_ENABLED       0x00000010      /* Hystart++ is enabled */
87 #define CUBICFLAG_HYSTART_IN_CSS        0x00000020      /* We are in Hystart++ CSS */
88
89 /* Kernel only bits */
90 #ifdef _KERNEL
91 struct cubic {
92         /* CUBIC K in fixed point form with CUBIC_SHIFT worth of precision. */
93         int64_t         K;
94         /* Sum of RTT samples across an epoch in ticks. */
95         int64_t         sum_rtt_ticks;
96         /* cwnd at the most recent congestion event. */
97         unsigned long   max_cwnd;
98         /* cwnd at the previous congestion event. */
99         unsigned long   prev_max_cwnd;
100         /* A copy of prev_max_cwnd. Used for CC_RTO_ERR */
101         unsigned long   prev_max_cwnd_cp;
102         /* various flags */
103         uint32_t        flags;
104         /* Minimum observed rtt in ticks. */
105         int             min_rtt_ticks;
106         /* Mean observed rtt between congestion epochs. */
107         int             mean_rtt_ticks;
108         /* ACKs since last congestion event. */
109         int             epoch_ack_count;
110         /* Timestamp (in ticks) of arriving in congestion avoidance from last
111          * congestion event.
112          */
113         int             t_last_cong;
114         /* Timestamp (in ticks) of a previous congestion event. Used for
115          * CC_RTO_ERR.
116          */
117         int             t_last_cong_prev;
118         uint32_t css_baseline_minrtt;
119         uint32_t css_current_round_minrtt;
120         uint32_t css_lastround_minrtt;
121         uint32_t css_rttsample_count;
122         uint32_t css_entered_at_round;
123         uint32_t css_current_round;
124         uint32_t css_fas_at_css_entry;
125         uint32_t css_lowrtt_fas;
126         uint32_t css_last_fas;
127 };
128 #endif
129
130 /* Userland only bits. */
131 #ifndef _KERNEL
132
133 extern int hz;
134
135 /*
136  * Implementation based on the formulae found in the CUBIC Internet Draft
137  * "draft-ietf-tcpm-cubic-04".
138  *
139  */
140
141 static __inline float
142 theoretical_cubic_k(double wmax_pkts)
143 {
144         double C;
145
146         C = 0.4;
147
148         return (pow((wmax_pkts * 0.3) / C, (1.0 / 3.0)) * pow(2, CUBIC_SHIFT));
149 }
150
151 static __inline unsigned long
152 theoretical_cubic_cwnd(int ticks_since_cong, unsigned long wmax, uint32_t smss)
153 {
154         double C, wmax_pkts;
155
156         C = 0.4;
157         wmax_pkts = wmax / (double)smss;
158
159         return (smss * (wmax_pkts +
160             (C * pow(ticks_since_cong / (double)hz -
161             theoretical_cubic_k(wmax_pkts) / pow(2, CUBIC_SHIFT), 3.0))));
162 }
163
164 static __inline unsigned long
165 theoretical_reno_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
166     uint32_t smss)
167 {
168
169         return ((wmax * 0.5) + ((ticks_since_cong / (float)rtt_ticks) * smss));
170 }
171
172 static __inline unsigned long
173 theoretical_tf_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
174     uint32_t smss)
175 {
176
177         return ((wmax * 0.7) + ((3 * 0.3) / (2 - 0.3) *
178             (ticks_since_cong / (float)rtt_ticks) * smss));
179 }
180
181 #endif /* !_KERNEL */
182
183 /*
184  * Compute the CUBIC K value used in the cwnd calculation, using an
185  * implementation of eqn 2 in the I-D. The method used
186  * here is adapted from Apple Computer Technical Report #KT-32.
187  */
188 static __inline int64_t
189 cubic_k(unsigned long wmax_pkts)
190 {
191         int64_t s, K;
192         uint16_t p;
193
194         K = s = 0;
195         p = 0;
196
197         /* (wmax * beta)/C with CUBIC_SHIFT worth of precision. */
198         s = ((wmax_pkts * ONE_SUB_CUBIC_BETA) << CUBIC_SHIFT) / CUBIC_C_FACTOR;
199
200         /* Rebase s to be between 1 and 1/8 with a shift of CUBIC_SHIFT. */
201         while (s >= 256) {
202                 s >>= 3;
203                 p++;
204         }
205
206         /*
207          * Some magic constants taken from the Apple TR with appropriate
208          * shifts: 275 == 1.072302 << CUBIC_SHIFT, 98 == 0.3812513 <<
209          * CUBIC_SHIFT, 120 == 0.46946116 << CUBIC_SHIFT.
210          */
211         K = (((s * 275) >> CUBIC_SHIFT) + 98) -
212             (((s * s * 120) >> CUBIC_SHIFT) >> CUBIC_SHIFT);
213
214         /* Multiply by 2^p to undo the rebasing of s from above. */
215         return (K <<= p);
216 }
217
218 /*
219  * Compute the new cwnd value using an implementation of eqn 1 from the I-D.
220  * Thanks to Kip Macy for help debugging this function.
221  *
222  * XXXLAS: Characterise bounds for overflow.
223  */
224 static __inline unsigned long
225 cubic_cwnd(int ticks_since_cong, unsigned long wmax, uint32_t smss, int64_t K)
226 {
227         int64_t cwnd;
228
229         /* K is in fixed point form with CUBIC_SHIFT worth of precision. */
230
231         /* t - K, with CUBIC_SHIFT worth of precision. */
232         cwnd = (((int64_t)ticks_since_cong << CUBIC_SHIFT) - (K * hz)) / hz;
233
234         if (cwnd > CUBED_ROOT_MAX_ULONG)
235                 return INT_MAX;
236         if (cwnd < -CUBED_ROOT_MAX_ULONG)
237                 return 0;
238
239         /* (t - K)^3, with CUBIC_SHIFT^3 worth of precision. */
240         cwnd *= (cwnd * cwnd);
241
242         /*
243          * C(t - K)^3 + wmax
244          * The down shift by CUBIC_SHIFT_4 is because cwnd has 4 lots of
245          * CUBIC_SHIFT included in the value. 3 from the cubing of cwnd above,
246          * and an extra from multiplying through by CUBIC_C_FACTOR.
247          */
248
249         cwnd = ((cwnd * CUBIC_C_FACTOR) >> CUBIC_SHIFT_4) * smss + wmax;
250
251         /*
252          * for negative cwnd, limiting to zero as lower bound
253          */
254         return (lmax(0,cwnd));
255 }
256
257 /*
258  * Compute an approximation of the NewReno cwnd some number of ticks after a
259  * congestion event. RTT should be the average RTT estimate for the path
260  * measured over the previous congestion epoch and wmax is the value of cwnd at
261  * the last congestion event. The "TCP friendly" concept in the CUBIC I-D is
262  * rather tricky to understand and it turns out this function is not required.
263  * It is left here for reference.
264  */
265 static __inline unsigned long
266 reno_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
267     uint32_t smss)
268 {
269
270         /*
271          * For NewReno, beta = 0.5, therefore: W_tcp(t) = wmax*0.5 + t/RTT
272          * W_tcp(t) deals with cwnd/wmax in pkts, so because our cwnd is in
273          * bytes, we have to multiply by smss.
274          */
275         return (((wmax * RENO_BETA) + (((ticks_since_cong * smss)
276             << CUBIC_SHIFT) / rtt_ticks)) >> CUBIC_SHIFT);
277 }
278
279 /*
280  * Compute an approximation of the "TCP friendly" cwnd some number of ticks
281  * after a congestion event that is designed to yield the same average cwnd as
282  * NewReno while using CUBIC's beta of 0.7. RTT should be the average RTT
283  * estimate for the path measured over the previous congestion epoch and wmax is
284  * the value of cwnd at the last congestion event.
285  */
286 static __inline unsigned long
287 tf_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
288     uint32_t smss)
289 {
290
291         /* Equation 4 of I-D. */
292         return (((wmax * CUBIC_BETA) +
293             (((THREE_X_PT3 * (unsigned long)ticks_since_cong *
294             (unsigned long)smss) << CUBIC_SHIFT) / (TWO_SUB_PT3 * rtt_ticks)))
295             >> CUBIC_SHIFT);
296 }
297
298 #endif /* _NETINET_CC_CUBIC_H_ */