FD.io VPP  v19.04.4-rc0-5-ge88582fac
Vector Packet Processing
tcp_cubic.c
Go to the documentation of this file.
1 /*
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:
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 
23 typedef struct cubic_cfg_
24 {
26 } cubic_cfg_t;
27 
28 static cubic_cfg_t cubic_cfg = {
29  .fast_convergence = 1,
30 };
31 
32 typedef 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 */
40 
41  /** Inflection point of the cubic function (in snd_mss segments) */
43 
44 } __clib_packed cubic_data_t;
45 
46 STATIC_ASSERT (sizeof (cubic_data_t) <= TCP_CC_DATA_SZ, "cubic data len");
47 
48 static inline f64
49 cubic_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  */
59 static inline u64
60 W_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  */
71 static inline f64
72 K_cubic (cubic_data_t * cd, u32 wnd)
73 {
74  /* 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);
78 }
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  */
87 static inline u32
88 W_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 
94 static void
96 {
97  cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
98  u32 w_max;
99 
100  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;
105  tc->ssthresh = clib_max (tc->cwnd * beta_cubic, 2 * tc->snd_mss);
106 }
107 
108 static void
110 {
111  cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
112  cd->t_start = cubic_time (tc->c_thread_index);
113  tc->cwnd = tc->ssthresh;
114  cd->K = K_cubic (cd, tc->cwnd / tc->snd_mss);
115 }
116 
117 static void
118 cubic_cwnd_accumulate (tcp_connection_t * tc, u32 thresh, u32 bytes_acked)
119 {
120  /* We just updated the threshold and don't know how large the previous
121  * one was. Still, optimistically increase cwnd by one segment and
122  * clear the accumulated bytes. */
123  if (tc->cwnd_acc_bytes > thresh)
124  {
125  tc->cwnd += tc->snd_mss;
126  tc->cwnd_acc_bytes = 0;
127  }
128 
129  tcp_cwnd_accumulate (tc, thresh, tc->bytes_acked);
130 }
131 
132 static void
134 {
135  cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
136  u64 w_cubic, w_aimd;
137  f64 t, rtt_sec;
138  u32 thresh;
139 
140  /* Constrained by tx fifo, can't grow further */
141  if (tc->cwnd >= tc->tx_fifo_size)
142  return;
143 
144  if (tcp_in_slowstart (tc))
145  {
146  tc->cwnd += clib_min (tc->snd_mss, tc->bytes_acked);
147  return;
148  }
149 
150  t = cubic_time (tc->c_thread_index) - cd->t_start;
151  rtt_sec = clib_min (tc->mrtt_us, (f64) tc->srtt * TCP_TICK);
152 
153  w_cubic = W_cubic (cd, t + rtt_sec) * tc->snd_mss;
154  w_aimd = (u64) W_est (cd, t, rtt_sec) * tc->snd_mss;
155  if (w_cubic < w_aimd)
156  {
157  cubic_cwnd_accumulate (tc, tc->cwnd, tc->bytes_acked);
158  }
159  else
160  {
161  if (w_cubic > tc->cwnd)
162  {
163  /* For NewReno and slow start, we increment cwnd based on the
164  * number of bytes acked, not the number of acks received. In
165  * particular, for NewReno we increment the cwnd by 1 snd_mss
166  * only after we accumulate 1 cwnd of acked bytes (RFC 3465).
167  *
168  * For Cubic, as per RFC 8312 we should increment cwnd by
169  * (w_cubic - cwnd)/cwnd for each ack. Instead of using that,
170  * we compute the number of packets that need to be acked
171  * before adding snd_mss to cwnd and compute the threshold
172  */
173  thresh = (tc->snd_mss * tc->cwnd) / (w_cubic - tc->cwnd);
174 
175  /* Make sure we don't increase cwnd more often than every segment */
176  thresh = clib_max (thresh, tc->snd_mss);
177  }
178  else
179  {
180  /* Practically we can't increment so just inflate threshold */
181  thresh = 50 * tc->cwnd;
182  }
183  cubic_cwnd_accumulate (tc, thresh, tc->bytes_acked);
184  }
185 }
186 
187 static void
189 {
190  cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
191  tc->ssthresh = tc->snd_wnd;
192  tc->cwnd = tcp_initial_cwnd (tc);
193  cd->w_max = 0;
194  cd->K = 0;
195  cd->t_start = cubic_time (tc->c_thread_index);
196 }
197 
198 static uword
200 {
201  if (!input)
202  return 0;
203 
205 
207  {
208  if (unformat (input, "no-fast-convergence"))
209  cubic_cfg.fast_convergence = 0;
210  else
211  return 0;
212  }
213  return 1;
214 }
215 
216 const static tcp_cc_algorithm_t tcp_cubic = {
217  .name = "cubic",
218  .unformat_cfg = cubic_unformat_config,
219  .congestion = cubic_congestion,
220  .recovered = cubic_recovered,
221  .rcv_ack = cubic_rcv_ack,
222  .rcv_cong_ack = newreno_rcv_cong_ack,
223  .init = cubic_conn_init,
224 };
225 
226 clib_error_t *
228 {
229  clib_error_t *error = 0;
230 
231  tcp_cc_algo_register (TCP_CC_CUBIC, &tcp_cubic);
232 
233  return error;
234 }
235 
237 
238 /*
239  * fd.io coding-style-patch-verification: ON
240  *
241  * Local Variables:
242  * eval: (c-set-style "gnu")
243  * End:
244  */
#define clib_min(x, y)
Definition: clib.h:295
static u64 W_cubic(cubic_data_t *cd, f64 t)
RFC 8312 Eq.
Definition: tcp_cubic.c:60
struct cubic_data_ cubic_data_t
static f64 transport_time_now(u32 thread_index)
Definition: session.h:426
unsigned long u64
Definition: types.h:89
f64 K
time period (in seconds) needed to increase the current window size to W_max if there are no further ...
Definition: tcp_cubic.c:36
static uword cubic_unformat_config(unformat_input_t *input)
Definition: tcp_cubic.c:199
struct _tcp_connection tcp_connection_t
static u32 W_est(cubic_data_t *cd, f64 t, f64 rtt)
RFC 8312 Eq.
Definition: tcp_cubic.c:88
unsigned char u8
Definition: types.h:56
static void cubic_cwnd_accumulate(tcp_connection_t *tc, u32 thresh, u32 bytes_acked)
Definition: tcp_cubic.c:118
double f64
Definition: types.h:142
#define beta_cubic
Definition: tcp_cubic.c:19
#define TCP_TICK
TCP tick period (s)
Definition: tcp.h:27
u32 w_max
Inflection point of the cubic function (in snd_mss segments)
Definition: tcp_cubic.c:42
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
static f64 cubic_time(u32 thread_index)
Definition: tcp_cubic.c:49
#define tcp_in_slowstart(tc)
Definition: tcp.h:345
unsigned int u32
Definition: types.h:88
static void cubic_recovered(tcp_connection_t *tc)
Definition: tcp_cubic.c:109
static void * tcp_cc_data(tcp_connection_t *tc)
Definition: tcp.h:943
struct _unformat_input_t unformat_input_t
static void tcp_cwnd_accumulate(tcp_connection_t *tc, u32 thresh, u32 bytes)
Definition: tcp.h:707
static f64 K_cubic(cubic_data_t *cd, u32 wnd)
RFC 8312 Eq.
Definition: tcp_cubic.c:72
#define west_const
Definition: tcp_cubic.c:21
static void cubic_congestion(tcp_connection_t *tc)
Definition: tcp_cubic.c:95
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
STATIC_ASSERT(sizeof(cubic_data_t)<=TCP_CC_DATA_SZ, "cubic data len")
vlib_main_t * vm
Definition: buffer.c:312
f64 t_start
time (in sec) since the start of current congestion avoidance
Definition: tcp_cubic.c:39
struct cubic_cfg_ cubic_cfg_t
u8 fast_convergence
Definition: tcp_cubic.c:25
void tcp_cc_algo_register(tcp_cc_algorithm_type_e type, const tcp_cc_algorithm_t *vft)
Definition: tcp.c:556
struct _tcp_cc_algorithm tcp_cc_algorithm_t
Definition: tcp.h:239
#define clib_max(x, y)
Definition: clib.h:288
#define cubic_c
Definition: tcp_cubic.c:20
clib_error_t * cubic_init(vlib_main_t *vm)
Definition: tcp_cubic.c:227
static u32 tcp_initial_cwnd(const tcp_connection_t *tc)
Initial cwnd as per RFC5681.
Definition: tcp.h:690
#define TCP_CC_DATA_SZ
Definition: tcp.h:33
u64 uword
Definition: types.h:112
static void cubic_rcv_ack(tcp_connection_t *tc)
Definition: tcp_cubic.c:133
void newreno_rcv_cong_ack(tcp_connection_t *tc, tcp_cc_ack_t ack_type)
Definition: tcp_newreno.c:45
uword unformat_skip_white_space(unformat_input_t *input)
Definition: unformat.c:815
static void cubic_conn_init(tcp_connection_t *tc)
Definition: tcp_cubic.c:188
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170