blob: cf2b9a17d18b664495e0f3c179b54625b1661a89 [file] [log] [blame]
Florin Coras2e31cc32018-09-25 14:00:34 -07001/*
Florin Corasc5df8c72019-04-08 07:42:30 -07002 * Copyright (c) 2018-2019 Cisco and/or its affiliates.
Florin Coras2e31cc32018-09-25 14:00:34 -07003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/tcp/tcp.h>
Florin Coras999840c2020-03-18 20:31:34 +000017#include <vnet/tcp/tcp_inlines.h>
Florin Coras2e31cc32018-09-25 14:00:34 -070018#include <math.h>
19
20#define beta_cubic 0.7
21#define cubic_c 0.4
22#define west_const (3 * (1 - beta_cubic) / (1 + beta_cubic))
23
Florin Corasaa01abb2018-11-12 09:13:10 -080024typedef struct cubic_cfg_
25{
26 u8 fast_convergence;
Sergey Ivanushkinc30318d2019-10-17 10:16:27 +010027 u32 ssthresh;
Florin Corasaa01abb2018-11-12 09:13:10 -080028} cubic_cfg_t;
29
30static cubic_cfg_t cubic_cfg = {
31 .fast_convergence = 1,
Sergey Ivanushkinc30318d2019-10-17 10:16:27 +010032 .ssthresh = 0x7FFFFFFFU,
Florin Corasaa01abb2018-11-12 09:13:10 -080033};
34
Florin Coras2e31cc32018-09-25 14:00:34 -070035typedef struct cubic_data_
36{
37 /** time period (in seconds) needed to increase the current window
38 * size to W_max if there are no further congestion events */
39 f64 K;
40
41 /** time (in sec) since the start of current congestion avoidance */
42 f64 t_start;
43
Florin Corasaa01abb2018-11-12 09:13:10 -080044 /** Inflection point of the cubic function (in snd_mss segments) */
Florin Coras2e31cc32018-09-25 14:00:34 -070045 u32 w_max;
46
47} __clib_packed cubic_data_t;
48
49STATIC_ASSERT (sizeof (cubic_data_t) <= TCP_CC_DATA_SZ, "cubic data len");
50
51static inline f64
52cubic_time (u32 thread_index)
53{
Florin Coras8f10b902021-04-02 18:32:00 -070054 return tcp_time_now_us (thread_index);
Florin Coras2e31cc32018-09-25 14:00:34 -070055}
56
57/**
58 * RFC 8312 Eq. 1
59 *
60 * CUBIC window increase function. Time and K need to be provided in seconds.
61 */
62static inline u64
63W_cubic (cubic_data_t * cd, f64 t)
64{
65 f64 diff = t - cd->K;
66
67 /* W_cubic(t) = C*(t-K)^3 + W_max */
68 return cubic_c * diff * diff * diff + cd->w_max;
69}
70
71/**
72 * RFC 8312 Eq. 2
73 */
74static inline f64
Florin Coras6ebd4392018-11-13 00:03:43 -080075K_cubic (cubic_data_t * cd, u32 wnd)
Florin Coras2e31cc32018-09-25 14:00:34 -070076{
Florin Coras6ebd4392018-11-13 00:03:43 -080077 /* K = cubic_root(W_max*(1-beta_cubic)/C)
78 * Because the current window may be less than W_max * beta_cubic because
79 * of fast convergence, we pass it as parameter */
80 return pow ((f64) (cd->w_max - wnd) / cubic_c, 1 / 3.0);
Florin Coras2e31cc32018-09-25 14:00:34 -070081}
82
83/**
84 * RFC 8312 Eq. 4
85 *
86 * Estimates the window size of AIMD(alpha_aimd, beta_aimd) for
87 * alpha_aimd=3*(1-beta_cubic)/(1+beta_cubic) and beta_aimd=beta_cubic.
88 * Time (t) and rtt should be provided in seconds
89 */
90static inline u32
91W_est (cubic_data_t * cd, f64 t, f64 rtt)
92{
93 /* W_est(t) = W_max*beta_cubic+[3*(1-beta_cubic)/(1+beta_cubic)]*(t/RTT) */
94 return cd->w_max * beta_cubic + west_const * (t / rtt);
95}
96
97static void
98cubic_congestion (tcp_connection_t * tc)
99{
100 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
Florin Corasaa01abb2018-11-12 09:13:10 -0800101 u32 w_max;
Florin Coras2e31cc32018-09-25 14:00:34 -0700102
Florin Corasaa01abb2018-11-12 09:13:10 -0800103 w_max = tc->cwnd / tc->snd_mss;
104 if (cubic_cfg.fast_convergence && w_max < cd->w_max)
105 w_max = w_max * ((1.0 + beta_cubic) / 2.0);
106
107 cd->w_max = w_max;
Florin Coras2e31cc32018-09-25 14:00:34 -0700108 tc->ssthresh = clib_max (tc->cwnd * beta_cubic, 2 * tc->snd_mss);
Florin Coras8b4114e2019-09-03 12:37:11 -0700109 tc->cwnd = tc->ssthresh;
Florin Coras2e31cc32018-09-25 14:00:34 -0700110}
111
112static void
Florin Corasa3c32652019-07-03 17:47:22 -0700113cubic_loss (tcp_connection_t * tc)
114{
115 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
116
Florin Corasa3c32652019-07-03 17:47:22 -0700117 tc->cwnd = tcp_loss_wnd (tc);
118 cd->t_start = cubic_time (tc->c_thread_index);
119 cd->K = 0;
Florin Coras36ebcff2019-09-12 18:36:44 -0700120 cd->w_max = tc->cwnd / tc->snd_mss;
Florin Corasa3c32652019-07-03 17:47:22 -0700121}
122
123static void
Florin Coras2e31cc32018-09-25 14:00:34 -0700124cubic_recovered (tcp_connection_t * tc)
125{
126 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
127 cd->t_start = cubic_time (tc->c_thread_index);
Florin Coras2e31cc32018-09-25 14:00:34 -0700128 tc->cwnd = tc->ssthresh;
Florin Coras6ebd4392018-11-13 00:03:43 -0800129 cd->K = K_cubic (cd, tc->cwnd / tc->snd_mss);
130}
131
132static void
133cubic_cwnd_accumulate (tcp_connection_t * tc, u32 thresh, u32 bytes_acked)
134{
135 /* We just updated the threshold and don't know how large the previous
136 * one was. Still, optimistically increase cwnd by one segment and
137 * clear the accumulated bytes. */
138 if (tc->cwnd_acc_bytes > thresh)
139 {
140 tc->cwnd += tc->snd_mss;
141 tc->cwnd_acc_bytes = 0;
142 }
143
Florin Corasded32ed2022-01-07 15:06:42 -0800144 tcp_cwnd_accumulate (tc, thresh, bytes_acked);
Florin Coras2e31cc32018-09-25 14:00:34 -0700145}
146
147static void
Florin Coras52814732019-06-12 15:38:19 -0700148cubic_rcv_ack (tcp_connection_t * tc, tcp_rate_sample_t * rs)
Florin Coras2e31cc32018-09-25 14:00:34 -0700149{
150 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
151 u64 w_cubic, w_aimd;
152 f64 t, rtt_sec;
153 u32 thresh;
154
155 /* Constrained by tx fifo, can't grow further */
156 if (tc->cwnd >= tc->tx_fifo_size)
157 return;
158
159 if (tcp_in_slowstart (tc))
160 {
Florin Coras0f8b91f2022-01-05 08:47:11 -0800161 tc->cwnd += rs->delivered;
Florin Coras2e31cc32018-09-25 14:00:34 -0700162 return;
163 }
164
165 t = cubic_time (tc->c_thread_index) - cd->t_start;
166 rtt_sec = clib_min (tc->mrtt_us, (f64) tc->srtt * TCP_TICK);
167
168 w_cubic = W_cubic (cd, t + rtt_sec) * tc->snd_mss;
Florin Corasaa01abb2018-11-12 09:13:10 -0800169 w_aimd = (u64) W_est (cd, t, rtt_sec) * tc->snd_mss;
Florin Coras2e31cc32018-09-25 14:00:34 -0700170 if (w_cubic < w_aimd)
171 {
Florin Coras0f8b91f2022-01-05 08:47:11 -0800172 cubic_cwnd_accumulate (tc, tc->cwnd, rs->delivered);
Florin Coras2e31cc32018-09-25 14:00:34 -0700173 }
174 else
175 {
176 if (w_cubic > tc->cwnd)
177 {
178 /* For NewReno and slow start, we increment cwnd based on the
179 * number of bytes acked, not the number of acks received. In
180 * particular, for NewReno we increment the cwnd by 1 snd_mss
181 * only after we accumulate 1 cwnd of acked bytes (RFC 3465).
182 *
183 * For Cubic, as per RFC 8312 we should increment cwnd by
184 * (w_cubic - cwnd)/cwnd for each ack. Instead of using that,
185 * we compute the number of packets that need to be acked
186 * before adding snd_mss to cwnd and compute the threshold
187 */
188 thresh = (tc->snd_mss * tc->cwnd) / (w_cubic - tc->cwnd);
189
Florin Coras6ebd4392018-11-13 00:03:43 -0800190 /* Make sure we don't increase cwnd more often than every segment */
191 thresh = clib_max (thresh, tc->snd_mss);
Florin Coras2e31cc32018-09-25 14:00:34 -0700192 }
193 else
194 {
195 /* Practically we can't increment so just inflate threshold */
Florin Corasaa01abb2018-11-12 09:13:10 -0800196 thresh = 50 * tc->cwnd;
Florin Coras2e31cc32018-09-25 14:00:34 -0700197 }
Florin Coras0f8b91f2022-01-05 08:47:11 -0800198 cubic_cwnd_accumulate (tc, thresh, rs->delivered);
Florin Coras2e31cc32018-09-25 14:00:34 -0700199 }
200}
201
202static void
203cubic_conn_init (tcp_connection_t * tc)
204{
205 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
Sergey Ivanushkinc30318d2019-10-17 10:16:27 +0100206 tc->ssthresh = cubic_cfg.ssthresh;
Florin Coras2e31cc32018-09-25 14:00:34 -0700207 tc->cwnd = tcp_initial_cwnd (tc);
208 cd->w_max = 0;
209 cd->K = 0;
210 cd->t_start = cubic_time (tc->c_thread_index);
211}
212
Florin Corasaa01abb2018-11-12 09:13:10 -0800213static uword
214cubic_unformat_config (unformat_input_t * input)
215{
Sergey Ivanushkinc30318d2019-10-17 10:16:27 +0100216 u32 ssthresh = 0x7FFFFFFFU;
217
Florin Corasaa01abb2018-11-12 09:13:10 -0800218 if (!input)
219 return 0;
220
221 unformat_skip_white_space (input);
222
223 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
224 {
225 if (unformat (input, "no-fast-convergence"))
226 cubic_cfg.fast_convergence = 0;
Sergey Ivanushkinc30318d2019-10-17 10:16:27 +0100227 else if (unformat (input, "ssthresh %u", &ssthresh))
228 cubic_cfg.ssthresh = ssthresh;
Florin Corasaa01abb2018-11-12 09:13:10 -0800229 else
230 return 0;
231 }
232 return 1;
233}
234
Florin Coras8e1ada32022-01-05 09:48:36 -0800235void
236cubic_event (tcp_connection_t *tc, tcp_cc_event_t evt)
237{
238 cubic_data_t *cd;
239 f64 now;
240
241 if (evt != TCP_CC_EVT_START_TX)
242 return;
243
244 /* App was idle so update t_start to avoid artificially
245 * inflating cwnd if nothing recently sent and acked */
246 cd = (cubic_data_t *) tcp_cc_data (tc);
247 now = cubic_time (tc->c_thread_index);
248 if (now > tc->mrtt_us + 1)
249 cd->t_start = now;
250}
251
Florin Coras2e31cc32018-09-25 14:00:34 -0700252const static tcp_cc_algorithm_t tcp_cubic = {
Florin Corasaa01abb2018-11-12 09:13:10 -0800253 .name = "cubic",
254 .unformat_cfg = cubic_unformat_config,
Florin Coras2e31cc32018-09-25 14:00:34 -0700255 .congestion = cubic_congestion,
Florin Corasa3c32652019-07-03 17:47:22 -0700256 .loss = cubic_loss,
Florin Coras2e31cc32018-09-25 14:00:34 -0700257 .recovered = cubic_recovered,
258 .rcv_ack = cubic_rcv_ack,
259 .rcv_cong_ack = newreno_rcv_cong_ack,
Florin Coras8e1ada32022-01-05 09:48:36 -0800260 .event = cubic_event,
Florin Corasaa01abb2018-11-12 09:13:10 -0800261 .init = cubic_conn_init,
Florin Coras2e31cc32018-09-25 14:00:34 -0700262};
263
264clib_error_t *
265cubic_init (vlib_main_t * vm)
266{
267 clib_error_t *error = 0;
268
269 tcp_cc_algo_register (TCP_CC_CUBIC, &tcp_cubic);
270
271 return error;
272}
273
274VLIB_INIT_FUNCTION (cubic_init);
275
276/*
277 * fd.io coding-style-patch-verification: ON
278 *
279 * Local Variables:
280 * eval: (c-set-style "gnu")
281 * End:
282 */