FD.io VPP  v20.05-21-gb1500e9ff
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 <vnet/tcp/tcp_inlines.h>
18 #include <math.h>
19 
20 #define beta_cubic 0.7
21 #define cubic_c 0.4
22 #define west_const (3 * (1 - beta_cubic) / (1 + beta_cubic))
23 
24 typedef struct cubic_cfg_
25 {
28 } cubic_cfg_t;
29 
30 static cubic_cfg_t cubic_cfg = {
31  .fast_convergence = 1,
32  .ssthresh = 0x7FFFFFFFU,
33 };
34 
35 typedef struct cubic_data_
36 {
37  /** time period (in seconds) needed to increase the current window
38  * size to W_max if there are no further congestion events */
39  f64 K;
40 
41  /** time (in sec) since the start of current congestion avoidance */
43 
44  /** Inflection point of the cubic function (in snd_mss segments) */
46 
47 } __clib_packed cubic_data_t;
48 
49 STATIC_ASSERT (sizeof (cubic_data_t) <= TCP_CC_DATA_SZ, "cubic data len");
50 
51 static inline f64
52 cubic_time (u32 thread_index)
53 {
54  return transport_time_now (thread_index);
55 }
56 
57 /**
58  * RFC 8312 Eq. 1
59  *
60  * CUBIC window increase function. Time and K need to be provided in seconds.
61  */
62 static inline u64
63 W_cubic (cubic_data_t * cd, f64 t)
64 {
65  f64 diff = t - cd->K;
66 
67  /* W_cubic(t) = C*(t-K)^3 + W_max */
68  return cubic_c * diff * diff * diff + cd->w_max;
69 }
70 
71 /**
72  * RFC 8312 Eq. 2
73  */
74 static inline f64
75 K_cubic (cubic_data_t * cd, u32 wnd)
76 {
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);
81 }
82 
83 /**
84  * RFC 8312 Eq. 4
85  *
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
89  */
90 static inline u32
91 W_est (cubic_data_t * cd, f64 t, f64 rtt)
92 {
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);
95 }
96 
97 static void
99 {
100  cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
101  u32 w_max;
102 
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);
106 
107  cd->w_max = w_max;
108  tc->ssthresh = clib_max (tc->cwnd * beta_cubic, 2 * tc->snd_mss);
109  tc->cwnd = tc->ssthresh;
110 }
111 
112 static void
114 {
115  cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
116 
117  tc->cwnd = tcp_loss_wnd (tc);
118  cd->t_start = cubic_time (tc->c_thread_index);
119  cd->K = 0;
120  cd->w_max = tc->cwnd / tc->snd_mss;
121 }
122 
123 static void
125 {
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);
130 }
131 
132 static void
133 cubic_cwnd_accumulate (tcp_connection_t * tc, u32 thresh, u32 bytes_acked)
134 {
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)
139  {
140  tc->cwnd += tc->snd_mss;
141  tc->cwnd_acc_bytes = 0;
142  }
143 
144  tcp_cwnd_accumulate (tc, thresh, tc->bytes_acked);
145 }
146 
147 static void
149 {
150  cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
151  u64 w_cubic, w_aimd;
152  f64 t, rtt_sec;
153  u32 thresh;
154 
155  /* Constrained by tx fifo, can't grow further */
156  if (tc->cwnd >= tc->tx_fifo_size)
157  return;
158 
159  if (tcp_in_slowstart (tc))
160  {
161  tc->cwnd += tc->bytes_acked;
162  return;
163  }
164 
165  t = cubic_time (tc->c_thread_index) - cd->t_start;
166  rtt_sec = clib_min (tc->mrtt_us, (f64) tc->srtt * TCP_TICK);
167 
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)
171  {
172  cubic_cwnd_accumulate (tc, tc->cwnd, tc->bytes_acked);
173  }
174  else
175  {
176  if (w_cubic > tc->cwnd)
177  {
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).
182  *
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
187  */
188  thresh = (tc->snd_mss * tc->cwnd) / (w_cubic - tc->cwnd);
189 
190  /* Make sure we don't increase cwnd more often than every segment */
191  thresh = clib_max (thresh, tc->snd_mss);
192  }
193  else
194  {
195  /* Practically we can't increment so just inflate threshold */
196  thresh = 50 * tc->cwnd;
197  }
198  cubic_cwnd_accumulate (tc, thresh, tc->bytes_acked);
199  }
200 }
201 
202 static void
204 {
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);
208  cd->w_max = 0;
209  cd->K = 0;
210  cd->t_start = cubic_time (tc->c_thread_index);
211 }
212 
213 static uword
215 {
216  u32 ssthresh = 0x7FFFFFFFU;
217 
218  if (!input)
219  return 0;
220 
222 
224  {
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;
229  else
230  return 0;
231  }
232  return 1;
233 }
234 
235 const static tcp_cc_algorithm_t tcp_cubic = {
236  .name = "cubic",
237  .unformat_cfg = cubic_unformat_config,
238  .congestion = cubic_congestion,
239  .loss = cubic_loss,
240  .recovered = cubic_recovered,
241  .rcv_ack = cubic_rcv_ack,
242  .rcv_cong_ack = newreno_rcv_cong_ack,
243  .init = cubic_conn_init,
244 };
245 
246 clib_error_t *
248 {
249  clib_error_t *error = 0;
250 
251  tcp_cc_algo_register (TCP_CC_CUBIC, &tcp_cubic);
252 
253  return error;
254 }
255 
257 
258 /*
259  * fd.io coding-style-patch-verification: ON
260  *
261  * Local Variables:
262  * eval: (c-set-style "gnu")
263  * End:
264  */
static void tcp_cwnd_accumulate(tcp_connection_t *tc, u32 thresh, u32 bytes)
Definition: tcp_inlines.h:130
static u32 tcp_loss_wnd(const tcp_connection_t *tc)
Definition: tcp_inlines.h:143
#define clib_min(x, y)
Definition: clib.h:319
static u64 W_cubic(cubic_data_t *cd, f64 t)
RFC 8312 Eq.
Definition: tcp_cubic.c:63
static clib_time_type_t transport_time_now(u32 thread_index)
Definition: session.h:538
struct cubic_data_ cubic_data_t
unsigned long u64
Definition: types.h:89
#define tcp_in_slowstart(tc)
Definition: tcp_types.h:415
void newreno_rcv_cong_ack(tcp_connection_t *tc, tcp_cc_ack_t ack_type, tcp_rate_sample_t *rs)
Definition: tcp_newreno.c:62
struct _tcp_connection tcp_connection_t
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:39
static uword cubic_unformat_config(unformat_input_t *input)
Definition: tcp_cubic.c:214
#define TCP_TICK
TCP tick period (s)
Definition: tcp_types.h:25
static u32 W_est(cubic_data_t *cd, f64 t, f64 rtt)
RFC 8312 Eq.
Definition: tcp_cubic.c:91
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:133
double f64
Definition: types.h:142
#define beta_cubic
Definition: tcp_cubic.c:20
#define TCP_CC_DATA_SZ
Definition: tcp_types.h:31
struct _tcp_cc_algorithm tcp_cc_algorithm_t
Definition: tcp_types.h:251
u32 w_max
Inflection point of the cubic function (in snd_mss segments)
Definition: tcp_cubic.c:45
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static f64 cubic_time(u32 thread_index)
Definition: tcp_cubic.c:52
unsigned int u32
Definition: types.h:88
static void cubic_recovered(tcp_connection_t *tc)
Definition: tcp_cubic.c:124
static void cubic_loss(tcp_connection_t *tc)
Definition: tcp_cubic.c:113
struct _unformat_input_t unformat_input_t
static u32 tcp_initial_cwnd(const tcp_connection_t *tc)
Initial cwnd as per RFC5681.
Definition: tcp_inlines.h:110
vlib_main_t * vm
Definition: in2out_ed.c:1599
static f64 K_cubic(cubic_data_t *cd, u32 wnd)
RFC 8312 Eq.
Definition: tcp_cubic.c:75
#define west_const
Definition: tcp_cubic.c:22
static void cubic_congestion(tcp_connection_t *tc)
Definition: tcp_cubic.c:98
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
STATIC_ASSERT(sizeof(cubic_data_t)<=TCP_CC_DATA_SZ, "cubic data len")
f64 t_start
time (in sec) since the start of current congestion avoidance
Definition: tcp_cubic.c:42
struct cubic_cfg_ cubic_cfg_t
static void cubic_rcv_ack(tcp_connection_t *tc, tcp_rate_sample_t *rs)
Definition: tcp_cubic.c:148
u8 fast_convergence
Definition: tcp_cubic.c:26
void tcp_cc_algo_register(tcp_cc_algorithm_type_e type, const tcp_cc_algorithm_t *vft)
Register exiting cc algo type.
Definition: tcp.c:85
static void * tcp_cc_data(tcp_connection_t *tc)
Definition: tcp_cc.h:81
#define clib_max(x, y)
Definition: clib.h:312
#define cubic_c
Definition: tcp_cubic.c:21
clib_error_t * cubic_init(vlib_main_t *vm)
Definition: tcp_cubic.c:247
u64 uword
Definition: types.h:112
uword unformat_skip_white_space(unformat_input_t *input)
Definition: unformat.c:821
static void cubic_conn_init(tcp_connection_t *tc)
Definition: tcp_cubic.c:203
u32 ssthresh
Definition: tcp_cubic.c:27
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171