blob: aa3189615335ce024958164270eaaec1bc6ee587 [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;
26} cubic_cfg_t;
27
28static cubic_cfg_t cubic_cfg = {
29 .fast_convergence = 1,
30};
31
Florin Coras2e31cc32018-09-25 14:00:34 -070032typedef struct cubic_data_
33{
34 /** time period (in seconds) needed to increase the current window
35 * size to W_max if there are no further congestion events */
36 f64 K;
37
38 /** time (in sec) since the start of current congestion avoidance */
39 f64 t_start;
40
Florin Corasaa01abb2018-11-12 09:13:10 -080041 /** Inflection point of the cubic function (in snd_mss segments) */
Florin Coras2e31cc32018-09-25 14:00:34 -070042 u32 w_max;
43
44} __clib_packed cubic_data_t;
45
46STATIC_ASSERT (sizeof (cubic_data_t) <= TCP_CC_DATA_SZ, "cubic data len");
47
48static inline f64
49cubic_time (u32 thread_index)
50{
51 return transport_time_now (thread_index);
52}
53
54/**
55 * RFC 8312 Eq. 1
56 *
57 * CUBIC window increase function. Time and K need to be provided in seconds.
58 */
59static inline u64
60W_cubic (cubic_data_t * cd, f64 t)
61{
62 f64 diff = t - cd->K;
63
64 /* W_cubic(t) = C*(t-K)^3 + W_max */
65 return cubic_c * diff * diff * diff + cd->w_max;
66}
67
68/**
69 * RFC 8312 Eq. 2
70 */
71static inline f64
Florin Coras6ebd4392018-11-13 00:03:43 -080072K_cubic (cubic_data_t * cd, u32 wnd)
Florin Coras2e31cc32018-09-25 14:00:34 -070073{
Florin Coras6ebd4392018-11-13 00:03:43 -080074 /* K = cubic_root(W_max*(1-beta_cubic)/C)
75 * Because the current window may be less than W_max * beta_cubic because
76 * of fast convergence, we pass it as parameter */
77 return pow ((f64) (cd->w_max - wnd) / cubic_c, 1 / 3.0);
Florin Coras2e31cc32018-09-25 14:00:34 -070078}
79
80/**
81 * RFC 8312 Eq. 4
82 *
83 * Estimates the window size of AIMD(alpha_aimd, beta_aimd) for
84 * alpha_aimd=3*(1-beta_cubic)/(1+beta_cubic) and beta_aimd=beta_cubic.
85 * Time (t) and rtt should be provided in seconds
86 */
87static inline u32
88W_est (cubic_data_t * cd, f64 t, f64 rtt)
89{
90 /* W_est(t) = W_max*beta_cubic+[3*(1-beta_cubic)/(1+beta_cubic)]*(t/RTT) */
91 return cd->w_max * beta_cubic + west_const * (t / rtt);
92}
93
94static void
95cubic_congestion (tcp_connection_t * tc)
96{
97 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
Florin Corasaa01abb2018-11-12 09:13:10 -080098 u32 w_max;
Florin Coras2e31cc32018-09-25 14:00:34 -070099
Florin Corasaa01abb2018-11-12 09:13:10 -0800100 w_max = tc->cwnd / tc->snd_mss;
101 if (cubic_cfg.fast_convergence && w_max < cd->w_max)
102 w_max = w_max * ((1.0 + beta_cubic) / 2.0);
103
104 cd->w_max = w_max;
Florin Coras2e31cc32018-09-25 14:00:34 -0700105 tc->ssthresh = clib_max (tc->cwnd * beta_cubic, 2 * tc->snd_mss);
Florin Coras8b4114e2019-09-03 12:37:11 -0700106
107 tc->cwnd = tc->ssthresh;
108 if (!tcp_opts_sack_permitted (&tc->rcv_opts))
109 tc->cwnd += 3 * tc->snd_mss;
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
117 tc->ssthresh = clib_max (tc->cwnd * beta_cubic, 2 * tc->snd_mss);
118 tc->cwnd = tcp_loss_wnd (tc);
119 cd->t_start = cubic_time (tc->c_thread_index);
120 cd->K = 0;
121 cd->w_max = 0;
122}
123
124static void
Florin Coras2e31cc32018-09-25 14:00:34 -0700125cubic_recovered (tcp_connection_t * tc)
126{
127 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
128 cd->t_start = cubic_time (tc->c_thread_index);
Florin Coras2e31cc32018-09-25 14:00:34 -0700129 tc->cwnd = tc->ssthresh;
Florin Coras6ebd4392018-11-13 00:03:43 -0800130 cd->K = K_cubic (cd, tc->cwnd / tc->snd_mss);
131}
132
133static void
134cubic_cwnd_accumulate (tcp_connection_t * tc, u32 thresh, u32 bytes_acked)
135{
136 /* We just updated the threshold and don't know how large the previous
137 * one was. Still, optimistically increase cwnd by one segment and
138 * clear the accumulated bytes. */
139 if (tc->cwnd_acc_bytes > thresh)
140 {
141 tc->cwnd += tc->snd_mss;
142 tc->cwnd_acc_bytes = 0;
143 }
144
145 tcp_cwnd_accumulate (tc, thresh, tc->bytes_acked);
Florin Coras2e31cc32018-09-25 14:00:34 -0700146}
147
148static void
Florin Coras52814732019-06-12 15:38:19 -0700149cubic_rcv_ack (tcp_connection_t * tc, tcp_rate_sample_t * rs)
Florin Coras2e31cc32018-09-25 14:00:34 -0700150{
151 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
152 u64 w_cubic, w_aimd;
153 f64 t, rtt_sec;
154 u32 thresh;
155
156 /* Constrained by tx fifo, can't grow further */
157 if (tc->cwnd >= tc->tx_fifo_size)
158 return;
159
160 if (tcp_in_slowstart (tc))
161 {
162 tc->cwnd += clib_min (tc->snd_mss, tc->bytes_acked);
163 return;
164 }
165
166 t = cubic_time (tc->c_thread_index) - cd->t_start;
167 rtt_sec = clib_min (tc->mrtt_us, (f64) tc->srtt * TCP_TICK);
168
169 w_cubic = W_cubic (cd, t + rtt_sec) * tc->snd_mss;
Florin Corasaa01abb2018-11-12 09:13:10 -0800170 w_aimd = (u64) W_est (cd, t, rtt_sec) * tc->snd_mss;
Florin Coras2e31cc32018-09-25 14:00:34 -0700171 if (w_cubic < w_aimd)
172 {
Florin Coras6ebd4392018-11-13 00:03:43 -0800173 cubic_cwnd_accumulate (tc, tc->cwnd, tc->bytes_acked);
Florin Coras2e31cc32018-09-25 14:00:34 -0700174 }
175 else
176 {
177 if (w_cubic > tc->cwnd)
178 {
179 /* For NewReno and slow start, we increment cwnd based on the
180 * number of bytes acked, not the number of acks received. In
181 * particular, for NewReno we increment the cwnd by 1 snd_mss
182 * only after we accumulate 1 cwnd of acked bytes (RFC 3465).
183 *
184 * For Cubic, as per RFC 8312 we should increment cwnd by
185 * (w_cubic - cwnd)/cwnd for each ack. Instead of using that,
186 * we compute the number of packets that need to be acked
187 * before adding snd_mss to cwnd and compute the threshold
188 */
189 thresh = (tc->snd_mss * tc->cwnd) / (w_cubic - tc->cwnd);
190
Florin Coras6ebd4392018-11-13 00:03:43 -0800191 /* Make sure we don't increase cwnd more often than every segment */
192 thresh = clib_max (thresh, tc->snd_mss);
Florin Coras2e31cc32018-09-25 14:00:34 -0700193 }
194 else
195 {
196 /* Practically we can't increment so just inflate threshold */
Florin Corasaa01abb2018-11-12 09:13:10 -0800197 thresh = 50 * tc->cwnd;
Florin Coras2e31cc32018-09-25 14:00:34 -0700198 }
Florin Coras6ebd4392018-11-13 00:03:43 -0800199 cubic_cwnd_accumulate (tc, thresh, tc->bytes_acked);
Florin Coras2e31cc32018-09-25 14:00:34 -0700200 }
201}
202
203static void
204cubic_conn_init (tcp_connection_t * tc)
205{
206 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
207 tc->ssthresh = tc->snd_wnd;
208 tc->cwnd = tcp_initial_cwnd (tc);
209 cd->w_max = 0;
210 cd->K = 0;
211 cd->t_start = cubic_time (tc->c_thread_index);
212}
213
Florin Corasaa01abb2018-11-12 09:13:10 -0800214static uword
215cubic_unformat_config (unformat_input_t * input)
216{
217 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;
226 else
227 return 0;
228 }
229 return 1;
230}
231
Florin Coras2e31cc32018-09-25 14:00:34 -0700232const static tcp_cc_algorithm_t tcp_cubic = {
Florin Corasaa01abb2018-11-12 09:13:10 -0800233 .name = "cubic",
234 .unformat_cfg = cubic_unformat_config,
Florin Coras2e31cc32018-09-25 14:00:34 -0700235 .congestion = cubic_congestion,
Florin Corasa3c32652019-07-03 17:47:22 -0700236 .loss = cubic_loss,
Florin Coras2e31cc32018-09-25 14:00:34 -0700237 .recovered = cubic_recovered,
238 .rcv_ack = cubic_rcv_ack,
239 .rcv_cong_ack = newreno_rcv_cong_ack,
Florin Corasaa01abb2018-11-12 09:13:10 -0800240 .init = cubic_conn_init,
Florin Coras2e31cc32018-09-25 14:00:34 -0700241};
242
243clib_error_t *
244cubic_init (vlib_main_t * vm)
245{
246 clib_error_t *error = 0;
247
248 tcp_cc_algo_register (TCP_CC_CUBIC, &tcp_cubic);
249
250 return error;
251}
252
253VLIB_INIT_FUNCTION (cubic_init);
254
255/*
256 * fd.io coding-style-patch-verification: ON
257 *
258 * Local Variables:
259 * eval: (c-set-style "gnu")
260 * End:
261 */