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>
17 #include <vnet/tcp/tcp_inlines.h>
20 #define beta_cubic 0.7
22 #define west_const (3 * (1 - beta_cubic) / (1 + beta_cubic))
24 typedef struct cubic_cfg_
30 static cubic_cfg_t cubic_cfg = {
31 .fast_convergence = 1,
32 .ssthresh = 0x7FFFFFFFU,
35 typedef struct cubic_data_
37 /** time period (in seconds) needed to increase the current window
38 * size to W_max if there are no further congestion events */
41 /** time (in sec) since the start of current congestion avoidance */
44 /** Inflection point of the cubic function (in snd_mss segments) */
47 } __clib_packed cubic_data_t;
49 STATIC_ASSERT (sizeof (cubic_data_t) <= TCP_CC_DATA_SZ, "cubic data len");
52 cubic_time (u32 thread_index)
54 return tcp_time_now_us (thread_index);
60 * CUBIC window increase function. Time and K need to be provided in seconds.
63 W_cubic (cubic_data_t * cd, f64 t)
67 /* W_cubic(t) = C*(t-K)^3 + W_max */
68 return cubic_c * diff * diff * diff + cd->w_max;
75 K_cubic (cubic_data_t * cd, u32 wnd)
77 /* 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);
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
91 W_est (cubic_data_t * cd, f64 t, f64 rtt)
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);
98 cubic_congestion (tcp_connection_t * tc)
100 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
103 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);
108 tc->ssthresh = clib_max (tc->cwnd * beta_cubic, 2 * tc->snd_mss);
109 tc->cwnd = tc->ssthresh;
113 cubic_loss (tcp_connection_t * tc)
115 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
117 tc->cwnd = tcp_loss_wnd (tc);
118 cd->t_start = cubic_time (tc->c_thread_index);
120 cd->w_max = tc->cwnd / tc->snd_mss;
124 cubic_recovered (tcp_connection_t * tc)
126 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
127 cd->t_start = cubic_time (tc->c_thread_index);
128 tc->cwnd = tc->ssthresh;
129 cd->K = K_cubic (cd, tc->cwnd / tc->snd_mss);
133 cubic_cwnd_accumulate (tcp_connection_t * tc, u32 thresh, u32 bytes_acked)
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)
140 tc->cwnd += tc->snd_mss;
141 tc->cwnd_acc_bytes = 0;
144 tcp_cwnd_accumulate (tc, thresh, tc->bytes_acked);
148 cubic_rcv_ack (tcp_connection_t * tc, tcp_rate_sample_t * rs)
150 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
155 /* Constrained by tx fifo, can't grow further */
156 if (tc->cwnd >= tc->tx_fifo_size)
159 if (tcp_in_slowstart (tc))
161 tc->cwnd += tc->bytes_acked;
165 t = cubic_time (tc->c_thread_index) - cd->t_start;
166 rtt_sec = clib_min (tc->mrtt_us, (f64) tc->srtt * TCP_TICK);
168 w_cubic = W_cubic (cd, t + rtt_sec) * tc->snd_mss;
169 w_aimd = (u64) W_est (cd, t, rtt_sec) * tc->snd_mss;
170 if (w_cubic < w_aimd)
172 cubic_cwnd_accumulate (tc, tc->cwnd, tc->bytes_acked);
176 if (w_cubic > tc->cwnd)
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).
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
188 thresh = (tc->snd_mss * tc->cwnd) / (w_cubic - tc->cwnd);
190 /* Make sure we don't increase cwnd more often than every segment */
191 thresh = clib_max (thresh, tc->snd_mss);
195 /* Practically we can't increment so just inflate threshold */
196 thresh = 50 * tc->cwnd;
198 cubic_cwnd_accumulate (tc, thresh, tc->bytes_acked);
203 cubic_conn_init (tcp_connection_t * tc)
205 cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
206 tc->ssthresh = cubic_cfg.ssthresh;
207 tc->cwnd = tcp_initial_cwnd (tc);
210 cd->t_start = cubic_time (tc->c_thread_index);
214 cubic_unformat_config (unformat_input_t * input)
216 u32 ssthresh = 0x7FFFFFFFU;
221 unformat_skip_white_space (input);
223 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
225 if (unformat (input, "no-fast-convergence"))
226 cubic_cfg.fast_convergence = 0;
227 else if (unformat (input, "ssthresh %u", &ssthresh))
228 cubic_cfg.ssthresh = ssthresh;
235 const static tcp_cc_algorithm_t tcp_cubic = {
237 .unformat_cfg = cubic_unformat_config,
238 .congestion = cubic_congestion,
240 .recovered = cubic_recovered,
241 .rcv_ack = cubic_rcv_ack,
242 .rcv_cong_ack = newreno_rcv_cong_ack,
243 .init = cubic_conn_init,
247 cubic_init (vlib_main_t * vm)
249 clib_error_t *error = 0;
251 tcp_cc_algo_register (TCP_CC_CUBIC, &tcp_cubic);
256 VLIB_INIT_FUNCTION (cubic_init);
259 * fd.io coding-style-patch-verification: ON
262 * eval: (c-set-style "gnu")