blob: b79ef8342d30f7a0b1fd1e7b342df649ff85f3eb [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>
17#include <math.h>
18
19#define beta_cubic 0.7
20#define cubic_c 0.4
21#define west_const (3 * (1 - beta_cubic) / (1 + beta_cubic))
22
Florin Corasaa01abb2018-11-12 09:13:10 -080023typedef struct cubic_cfg_
24{
25 u8 fast_convergence;
Sergey Ivanushkinc30318d2019-10-17 10:16:27 +010026 u32 ssthresh;
Florin Corasaa01abb2018-11-12 09:13:10 -080027} cubic_cfg_t;
28
29static cubic_cfg_t cubic_cfg = {
30 .fast_convergence = 1,
Sergey Ivanushkinc30318d2019-10-17 10:16:27 +010031 .ssthresh = 0x7FFFFFFFU,
Florin Corasaa01abb2018-11-12 09:13:10 -080032};
33
Florin Coras2e31cc32018-09-25 14:00:34 -070034typedef struct cubic_data_
35{
36 /** time period (in seconds) needed to increase the current window
37 * size to W_max if there are no further congestion events */
38 f64 K;
39
40 /** time (in sec) since the start of current congestion avoidance */
41 f64 t_start;
42
Florin Corasaa01abb2018-11-12 09:13:10 -080043 /** Inflection point of the cubic function (in snd_mss segments) */
Florin Coras2e31cc32018-09-25 14:00:34 -070044 u32 w_max;
45
46} __clib_packed cubic_data_t;
47
48STATIC_ASSERT (sizeof (cubic_data_t) <= TCP_CC_DATA_SZ, "cubic data len");
49
50static inline f64
51cubic_time (u32 thread_index)
52{
53 return transport_time_now (thread_index);
54}
55
56/**
57 * RFC 8312 Eq. 1
58 *
59 * CUBIC window increase function. Time and K need to be provided in seconds.
60 */
61static inline u64
62W_cubic (cubic_data_t * cd, f64 t)
63{
64 f64 diff = t - cd->K;
65
66 /* W_cubic(t) = C*(t-K)^3 + W_max */
67 return cubic_c * diff * diff * diff + cd->w_max;
68}
69
70/**
71 * RFC 8312 Eq. 2
72 */
73static inline f64
Florin Coras6ebd4392018-11-13 00:03:43 -080074K_cubic (cubic_data_t * cd, u32 wnd)
Florin Coras2e31cc32018-09-25 14:00:34 -070075{
Florin Coras6ebd4392018-11-13 00:03:43 -080076 /* K = cubic_root(W_max*(1-beta_cubic)/C)
77 * Because the current window may be less than W_max * beta_cubic because
78 * of fast convergence, we pass it as parameter */
79 return pow ((f64) (cd->w_max - wnd) / cubic_c, 1 / 3.0);
Florin Coras2e31cc32018-09-25 14:00:34 -070080}
81
82/**
83 * RFC 8312 Eq. 4
84 *
85 * Estimates the window size of AIMD(alpha_aimd, beta_aimd) for
86 * alpha_aimd=3*(1-beta_cubic)/(1+beta_cubic) and beta_aimd=beta_cubic.
87 * Time (t) and rtt should be provided in seconds
88 */
89static inline u32
90W_est (cubic_data_t * cd, f64 t, f64 rtt)
91{
92 /* W_est(t) = W_max*beta_cubic+[3*(1-beta_cubic)/(1+beta_cubic)]*(t/RTT) */
93 return cd->w_max * beta_cubic + west_const * (t / rtt);
94}
95
96static void
97cubic_congestion (tcp_connection_t * tc)
98{
99 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
Florin Corasaa01abb2018-11-12 09:13:10 -0800100 u32 w_max;
Florin Coras2e31cc32018-09-25 14:00:34 -0700101
Florin Corasaa01abb2018-11-12 09:13:10 -0800102 w_max = tc->cwnd / tc->snd_mss;
103 if (cubic_cfg.fast_convergence && w_max < cd->w_max)
104 w_max = w_max * ((1.0 + beta_cubic) / 2.0);
105
106 cd->w_max = w_max;
Florin Coras2e31cc32018-09-25 14:00:34 -0700107 tc->ssthresh = clib_max (tc->cwnd * beta_cubic, 2 * tc->snd_mss);
Florin Coras8b4114e2019-09-03 12:37:11 -0700108 tc->cwnd = tc->ssthresh;
Florin Coras2e31cc32018-09-25 14:00:34 -0700109}
110
111static void
Florin Corasa3c32652019-07-03 17:47:22 -0700112cubic_loss (tcp_connection_t * tc)
113{
114 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
115
Florin Corasa3c32652019-07-03 17:47:22 -0700116 tc->cwnd = tcp_loss_wnd (tc);
117 cd->t_start = cubic_time (tc->c_thread_index);
118 cd->K = 0;
Florin Coras36ebcff2019-09-12 18:36:44 -0700119 cd->w_max = tc->cwnd / tc->snd_mss;
Florin Corasa3c32652019-07-03 17:47:22 -0700120}
121
122static void
Florin Coras2e31cc32018-09-25 14:00:34 -0700123cubic_recovered (tcp_connection_t * tc)
124{
125 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
126 cd->t_start = cubic_time (tc->c_thread_index);
Florin Coras2e31cc32018-09-25 14:00:34 -0700127 tc->cwnd = tc->ssthresh;
Florin Coras6ebd4392018-11-13 00:03:43 -0800128 cd->K = K_cubic (cd, tc->cwnd / tc->snd_mss);
129}
130
131static void
132cubic_cwnd_accumulate (tcp_connection_t * tc, u32 thresh, u32 bytes_acked)
133{
134 /* We just updated the threshold and don't know how large the previous
135 * one was. Still, optimistically increase cwnd by one segment and
136 * clear the accumulated bytes. */
137 if (tc->cwnd_acc_bytes > thresh)
138 {
139 tc->cwnd += tc->snd_mss;
140 tc->cwnd_acc_bytes = 0;
141 }
142
143 tcp_cwnd_accumulate (tc, thresh, tc->bytes_acked);
Florin Coras2e31cc32018-09-25 14:00:34 -0700144}
145
146static void
Florin Coras52814732019-06-12 15:38:19 -0700147cubic_rcv_ack (tcp_connection_t * tc, tcp_rate_sample_t * rs)
Florin Coras2e31cc32018-09-25 14:00:34 -0700148{
149 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
150 u64 w_cubic, w_aimd;
151 f64 t, rtt_sec;
152 u32 thresh;
153
154 /* Constrained by tx fifo, can't grow further */
155 if (tc->cwnd >= tc->tx_fifo_size)
156 return;
157
158 if (tcp_in_slowstart (tc))
159 {
Florin Coras36ebcff2019-09-12 18:36:44 -0700160 tc->cwnd += tc->bytes_acked;
Florin Coras2e31cc32018-09-25 14:00:34 -0700161 return;
162 }
163
164 t = cubic_time (tc->c_thread_index) - cd->t_start;
165 rtt_sec = clib_min (tc->mrtt_us, (f64) tc->srtt * TCP_TICK);
166
167 w_cubic = W_cubic (cd, t + rtt_sec) * tc->snd_mss;
Florin Corasaa01abb2018-11-12 09:13:10 -0800168 w_aimd = (u64) W_est (cd, t, rtt_sec) * tc->snd_mss;
Florin Coras2e31cc32018-09-25 14:00:34 -0700169 if (w_cubic < w_aimd)
170 {
Florin Coras6ebd4392018-11-13 00:03:43 -0800171 cubic_cwnd_accumulate (tc, tc->cwnd, tc->bytes_acked);
Florin Coras2e31cc32018-09-25 14:00:34 -0700172 }
173 else
174 {
175 if (w_cubic > tc->cwnd)
176 {
177 /* For NewReno and slow start, we increment cwnd based on the
178 * number of bytes acked, not the number of acks received. In
179 * particular, for NewReno we increment the cwnd by 1 snd_mss
180 * only after we accumulate 1 cwnd of acked bytes (RFC 3465).
181 *
182 * For Cubic, as per RFC 8312 we should increment cwnd by
183 * (w_cubic - cwnd)/cwnd for each ack. Instead of using that,
184 * we compute the number of packets that need to be acked
185 * before adding snd_mss to cwnd and compute the threshold
186 */
187 thresh = (tc->snd_mss * tc->cwnd) / (w_cubic - tc->cwnd);
188
Florin Coras6ebd4392018-11-13 00:03:43 -0800189 /* Make sure we don't increase cwnd more often than every segment */
190 thresh = clib_max (thresh, tc->snd_mss);
Florin Coras2e31cc32018-09-25 14:00:34 -0700191 }
192 else
193 {
194 /* Practically we can't increment so just inflate threshold */
Florin Corasaa01abb2018-11-12 09:13:10 -0800195 thresh = 50 * tc->cwnd;
Florin Coras2e31cc32018-09-25 14:00:34 -0700196 }
Florin Coras6ebd4392018-11-13 00:03:43 -0800197 cubic_cwnd_accumulate (tc, thresh, tc->bytes_acked);
Florin Coras2e31cc32018-09-25 14:00:34 -0700198 }
199}
200
201static void
202cubic_conn_init (tcp_connection_t * tc)
203{
204 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
Sergey Ivanushkinc30318d2019-10-17 10:16:27 +0100205 tc->ssthresh = cubic_cfg.ssthresh;
Florin Coras2e31cc32018-09-25 14:00:34 -0700206 tc->cwnd = tcp_initial_cwnd (tc);
207 cd->w_max = 0;
208 cd->K = 0;
209 cd->t_start = cubic_time (tc->c_thread_index);
210}
211
Florin Corasaa01abb2018-11-12 09:13:10 -0800212static uword
213cubic_unformat_config (unformat_input_t * input)
214{
Sergey Ivanushkinc30318d2019-10-17 10:16:27 +0100215 u32 ssthresh = 0x7FFFFFFFU;
216
Florin Corasaa01abb2018-11-12 09:13:10 -0800217 if (!input)
218 return 0;
219
220 unformat_skip_white_space (input);
221
222 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
223 {
224 if (unformat (input, "no-fast-convergence"))
225 cubic_cfg.fast_convergence = 0;
Sergey Ivanushkinc30318d2019-10-17 10:16:27 +0100226 else if (unformat (input, "ssthresh %u", &ssthresh))
227 cubic_cfg.ssthresh = ssthresh;
Florin Corasaa01abb2018-11-12 09:13:10 -0800228 else
229 return 0;
230 }
231 return 1;
232}
233
Florin Coras2e31cc32018-09-25 14:00:34 -0700234const static tcp_cc_algorithm_t tcp_cubic = {
Florin Corasaa01abb2018-11-12 09:13:10 -0800235 .name = "cubic",
236 .unformat_cfg = cubic_unformat_config,
Florin Coras2e31cc32018-09-25 14:00:34 -0700237 .congestion = cubic_congestion,
Florin Corasa3c32652019-07-03 17:47:22 -0700238 .loss = cubic_loss,
Florin Coras2e31cc32018-09-25 14:00:34 -0700239 .recovered = cubic_recovered,
240 .rcv_ack = cubic_rcv_ack,
241 .rcv_cong_ack = newreno_rcv_cong_ack,
Florin Corasaa01abb2018-11-12 09:13:10 -0800242 .init = cubic_conn_init,
Florin Coras2e31cc32018-09-25 14:00:34 -0700243};
244
245clib_error_t *
246cubic_init (vlib_main_t * vm)
247{
248 clib_error_t *error = 0;
249
250 tcp_cc_algo_register (TCP_CC_CUBIC, &tcp_cubic);
251
252 return error;
253}
254
255VLIB_INIT_FUNCTION (cubic_init);
256
257/*
258 * fd.io coding-style-patch-verification: ON
259 *
260 * Local Variables:
261 * eval: (c-set-style "gnu")
262 * End:
263 */