b9a1c3da06a8b3dfd7092f3e66455e3eba12886f
[vpp.git] / src / vnet / tcp / tcp_cubic.c
1 /*
2  * Copyright (c) 2018 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_data_
24 {
25   /** time period (in seconds) needed to increase the current window
26    *  size to W_max if there are no further congestion events */
27   f64 K;
28
29   /** time (in sec) since the start of current congestion avoidance */
30   f64 t_start;
31
32   /** Inflection point of the cubic function */
33   u32 w_max;
34
35 } __clib_packed cubic_data_t;
36
37 STATIC_ASSERT (sizeof (cubic_data_t) <= TCP_CC_DATA_SZ, "cubic data len");
38
39 static inline f64
40 cubic_time (u32 thread_index)
41 {
42   return transport_time_now (thread_index);
43 }
44
45 /**
46  * RFC 8312 Eq. 1
47  *
48  * CUBIC window increase function. Time and K need to be provided in seconds.
49  */
50 static inline u64
51 W_cubic (cubic_data_t * cd, f64 t)
52 {
53   f64 diff = t - cd->K;
54
55   /* W_cubic(t) = C*(t-K)^3 + W_max */
56   return cubic_c * diff * diff * diff + cd->w_max;
57 }
58
59 /**
60  * RFC 8312 Eq. 2
61  */
62 static inline f64
63 K_cubic (cubic_data_t * cd)
64 {
65   /* K = cubic_root(W_max*(1-beta_cubic)/C) */
66   return pow (cd->w_max * (1 - beta_cubic) / cubic_c, 1 / 3.0);
67 }
68
69 /**
70  * RFC 8312 Eq. 4
71  *
72  * Estimates the window size of AIMD(alpha_aimd, beta_aimd) for
73  * alpha_aimd=3*(1-beta_cubic)/(1+beta_cubic) and beta_aimd=beta_cubic.
74  * Time (t) and rtt should be provided in seconds
75  */
76 static inline u32
77 W_est (cubic_data_t * cd, f64 t, f64 rtt)
78 {
79   /* W_est(t) = W_max*beta_cubic+[3*(1-beta_cubic)/(1+beta_cubic)]*(t/RTT) */
80   return cd->w_max * beta_cubic + west_const * (t / rtt);
81 }
82
83 static void
84 cubic_congestion (tcp_connection_t * tc)
85 {
86   cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
87
88   cd->w_max = tc->cwnd / tc->snd_mss;
89   tc->ssthresh = clib_max (tc->cwnd * beta_cubic, 2 * tc->snd_mss);
90 }
91
92 static void
93 cubic_recovered (tcp_connection_t * tc)
94 {
95   cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
96   cd->t_start = cubic_time (tc->c_thread_index);
97   cd->K = K_cubic (cd);
98   tc->cwnd = tc->ssthresh;
99 }
100
101 static void
102 cubic_rcv_ack (tcp_connection_t * tc)
103 {
104   cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
105   u64 w_cubic, w_aimd;
106   f64 t, rtt_sec;
107   u32 thresh;
108
109   /* Constrained by tx fifo, can't grow further */
110   if (tc->cwnd >= tc->tx_fifo_size)
111     return;
112
113   if (tcp_in_slowstart (tc))
114     {
115       tc->cwnd += clib_min (tc->snd_mss, tc->bytes_acked);
116       return;
117     }
118
119   t = cubic_time (tc->c_thread_index) - cd->t_start;
120   rtt_sec = clib_min (tc->mrtt_us, (f64) tc->srtt * TCP_TICK);
121
122   w_cubic = W_cubic (cd, t + rtt_sec) * tc->snd_mss;
123   w_aimd = W_est (cd, t, rtt_sec) * tc->snd_mss;
124   if (w_cubic < w_aimd)
125     {
126       tcp_cwnd_accumulate (tc, tc->cwnd, tc->bytes_acked);
127     }
128   else
129     {
130       if (w_cubic > tc->cwnd)
131         {
132           /* For NewReno and slow start, we increment cwnd based on the
133            * number of bytes acked, not the number of acks received. In
134            * particular, for NewReno we increment the cwnd by 1 snd_mss
135            * only after we accumulate 1 cwnd of acked bytes (RFC 3465).
136            *
137            * For Cubic, as per RFC 8312 we should increment cwnd by
138            * (w_cubic - cwnd)/cwnd for each ack. Instead of using that,
139            * we compute the number of packets that need to be acked
140            * before adding snd_mss to cwnd and compute the threshold
141            */
142           thresh = (tc->snd_mss * tc->cwnd) / (w_cubic - tc->cwnd);
143
144           /* Make sure we don't increase cwnd more often than every
145            * 2 segments */
146           thresh = clib_max (thresh, 2 * tc->snd_mss);
147         }
148       else
149         {
150           /* Practically we can't increment so just inflate threshold */
151           thresh = 1000 * tc->cwnd;
152         }
153       tcp_cwnd_accumulate (tc, thresh, tc->bytes_acked);
154     }
155 }
156
157 static void
158 cubic_conn_init (tcp_connection_t * tc)
159 {
160   cubic_data_t *cd = (cubic_data_t *) tcp_cc_data (tc);
161   tc->ssthresh = tc->snd_wnd;
162   tc->cwnd = tcp_initial_cwnd (tc);
163   cd->w_max = 0;
164   cd->K = 0;
165   cd->t_start = cubic_time (tc->c_thread_index);
166 }
167
168 const static tcp_cc_algorithm_t tcp_cubic = {
169   .congestion = cubic_congestion,
170   .recovered = cubic_recovered,
171   .rcv_ack = cubic_rcv_ack,
172   .rcv_cong_ack = newreno_rcv_cong_ack,
173   .init = cubic_conn_init
174 };
175
176 clib_error_t *
177 cubic_init (vlib_main_t * vm)
178 {
179   clib_error_t *error = 0;
180
181   tcp_cc_algo_register (TCP_CC_CUBIC, &tcp_cubic);
182
183   return error;
184 }
185
186 VLIB_INIT_FUNCTION (cubic_init);
187
188 /*
189  * fd.io coding-style-patch-verification: ON
190  *
191  * Local Variables:
192  * eval: (c-set-style "gnu")
193  * End:
194  */