2 * Copyright (c) 2018-2019 Cisco and/or its affiliates.
3 * 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/tcp/tcp.h>
19 #define beta_cubic 0.7
21 #define west_const (3 * (1 - beta_cubic) / (1 + beta_cubic))
23 typedef struct cubic_cfg_
29 static cubic_cfg_t cubic_cfg = {
30 .fast_convergence = 1,
31 .ssthresh = 0x7FFFFFFFU,
34 typedef struct cubic_data_
36 /** time period (in seconds) needed to increase the current window
37 * size to W_max if there are no further congestion events */
40 /** time (in sec) since the start of current congestion avoidance */
43 /** Inflection point of the cubic function (in snd_mss segments) */
46 } __clib_packed cubic_data_t;
48 STATIC_ASSERT (sizeof (cubic_data_t) <= TCP_CC_DATA_SZ, "cubic data len");
51 cubic_time (u32 thread_index)
53 return transport_time_now (thread_index);
59 * CUBIC window increase function. Time and K need to be provided in seconds.
62 W_cubic (cubic_data_t * cd, f64 t)
66 /* W_cubic(t) = C*(t-K)^3 + W_max */
67 return cubic_c * diff * diff * diff + cd->w_max;
74 K_cubic (cubic_data_t * cd, u32 wnd)
76 /* 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);
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
90 W_est (cubic_data_t * cd, f64 t, f64 rtt)
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);
97 cubic_congestion (tcp_connection_t * tc)
99 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
102 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);
107 tc->ssthresh = clib_max (tc->cwnd * beta_cubic, 2 * tc->snd_mss);
108 tc->cwnd = tc->ssthresh;
112 cubic_loss (tcp_connection_t * tc)
114 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
116 tc->cwnd = tcp_loss_wnd (tc);
117 cd->t_start = cubic_time (tc->c_thread_index);
119 cd->w_max = tc->cwnd / tc->snd_mss;
123 cubic_recovered (tcp_connection_t * tc)
125 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
126 cd->t_start = cubic_time (tc->c_thread_index);
127 tc->cwnd = tc->ssthresh;
128 cd->K = K_cubic (cd, tc->cwnd / tc->snd_mss);
132 cubic_cwnd_accumulate (tcp_connection_t * tc, u32 thresh, u32 bytes_acked)
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)
139 tc->cwnd += tc->snd_mss;
140 tc->cwnd_acc_bytes = 0;
143 tcp_cwnd_accumulate (tc, thresh, tc->bytes_acked);
147 cubic_rcv_ack (tcp_connection_t * tc, tcp_rate_sample_t * rs)
149 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
154 /* Constrained by tx fifo, can't grow further */
155 if (tc->cwnd >= tc->tx_fifo_size)
158 if (tcp_in_slowstart (tc))
160 tc->cwnd += tc->bytes_acked;
164 t = cubic_time (tc->c_thread_index) - cd->t_start;
165 rtt_sec = clib_min (tc->mrtt_us, (f64) tc->srtt * TCP_TICK);
167 w_cubic = W_cubic (cd, t + rtt_sec) * tc->snd_mss;
168 w_aimd = (u64) W_est (cd, t, rtt_sec) * tc->snd_mss;
169 if (w_cubic < w_aimd)
171 cubic_cwnd_accumulate (tc, tc->cwnd, tc->bytes_acked);
175 if (w_cubic > tc->cwnd)
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).
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
187 thresh = (tc->snd_mss * tc->cwnd) / (w_cubic - tc->cwnd);
189 /* Make sure we don't increase cwnd more often than every segment */
190 thresh = clib_max (thresh, tc->snd_mss);
194 /* Practically we can't increment so just inflate threshold */
195 thresh = 50 * tc->cwnd;
197 cubic_cwnd_accumulate (tc, thresh, tc->bytes_acked);
202 cubic_conn_init (tcp_connection_t * tc)
204 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
205 tc->ssthresh = cubic_cfg.ssthresh;
206 tc->cwnd = tcp_initial_cwnd (tc);
209 cd->t_start = cubic_time (tc->c_thread_index);
213 cubic_unformat_config (unformat_input_t * input)
215 u32 ssthresh = 0x7FFFFFFFU;
220 unformat_skip_white_space (input);
222 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
224 if (unformat (input, "no-fast-convergence"))
225 cubic_cfg.fast_convergence = 0;
226 else if (unformat (input, "ssthresh %u", &ssthresh))
227 cubic_cfg.ssthresh = ssthresh;
234 const static tcp_cc_algorithm_t tcp_cubic = {
236 .unformat_cfg = cubic_unformat_config,
237 .congestion = cubic_congestion,
239 .recovered = cubic_recovered,
240 .rcv_ack = cubic_rcv_ack,
241 .rcv_cong_ack = newreno_rcv_cong_ack,
242 .init = cubic_conn_init,
246 cubic_init (vlib_main_t * vm)
248 clib_error_t *error = 0;
250 tcp_cc_algo_register (TCP_CC_CUBIC, &tcp_cubic);
255 VLIB_INIT_FUNCTION (cubic_init);
258 * fd.io coding-style-patch-verification: ON
261 * eval: (c-set-style "gnu")