]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/cc/cc_cubic.c
MFC r354773: Improve TCP CUBIC specific after idle reaction.
[FreeBSD/FreeBSD.git] / sys / netinet / cc / cc_cubic.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org>
5  * Copyright (c) 2010 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * This software was developed by Lawrence Stewart while studying at the Centre
9  * for Advanced Internet Architectures, Swinburne University of Technology, made
10  * possible in part by a grant from the Cisco University Research Program Fund
11  * at Community Foundation Silicon Valley.
12  *
13  * Portions of this software were developed at the Centre for Advanced
14  * Internet Architectures, Swinburne University of Technology, Melbourne,
15  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38
39 /*
40  * An implementation of the CUBIC congestion control algorithm for FreeBSD,
41  * based on the Internet Draft "draft-rhee-tcpm-cubic-02" by Rhee, Xu and Ha.
42  * Originally released as part of the NewTCP research project at Swinburne
43  * University of Technology's Centre for Advanced Internet Architectures,
44  * Melbourne, Australia, which was made possible in part by a grant from the
45  * Cisco University Research Program Fund at Community Foundation Silicon
46  * Valley. More details are available at:
47  *   http://caia.swin.edu.au/urp/newtcp/
48  */
49
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
52
53 #include <sys/param.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/module.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
61
62 #include <net/vnet.h>
63
64 #include <netinet/tcp.h>
65 #include <netinet/tcp_seq.h>
66 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet/cc/cc.h>
69 #include <netinet/cc/cc_cubic.h>
70 #include <netinet/cc/cc_module.h>
71
72 static void     cubic_ack_received(struct cc_var *ccv, uint16_t type);
73 static void     cubic_cb_destroy(struct cc_var *ccv);
74 static int      cubic_cb_init(struct cc_var *ccv);
75 static void     cubic_cong_signal(struct cc_var *ccv, uint32_t type);
76 static void     cubic_conn_init(struct cc_var *ccv);
77 static int      cubic_mod_init(void);
78 static void     cubic_post_recovery(struct cc_var *ccv);
79 static void     cubic_record_rtt(struct cc_var *ccv);
80 static void     cubic_ssthresh_update(struct cc_var *ccv);
81 static void     cubic_after_idle(struct cc_var *ccv);
82
83 struct cubic {
84         /* Cubic K in fixed point form with CUBIC_SHIFT worth of precision. */
85         int64_t         K;
86         /* Sum of RTT samples across an epoch in ticks. */
87         int64_t         sum_rtt_ticks;
88         /* cwnd at the most recent congestion event. */
89         unsigned long   max_cwnd;
90         /* cwnd at the previous congestion event. */
91         unsigned long   prev_max_cwnd;
92         /* various flags */
93         uint32_t        flags;
94 #define CUBICFLAG_CONG_EVENT    0x00000001      /* congestion experienced */
95 #define CUBICFLAG_IN_SLOWSTART  0x00000002      /* in slow start */
96 #define CUBICFLAG_IN_APPLIMIT   0x00000004      /* application limited */
97         /* Minimum observed rtt in ticks. */
98         int             min_rtt_ticks;
99         /* Mean observed rtt between congestion epochs. */
100         int             mean_rtt_ticks;
101         /* ACKs since last congestion event. */
102         int             epoch_ack_count;
103         /* Time of last congestion event in ticks. */
104         int             t_last_cong;
105 };
106
107 static MALLOC_DEFINE(M_CUBIC, "cubic data",
108     "Per connection data required for the CUBIC congestion control algorithm");
109
110 struct cc_algo cubic_cc_algo = {
111         .name = "cubic",
112         .ack_received = cubic_ack_received,
113         .cb_destroy = cubic_cb_destroy,
114         .cb_init = cubic_cb_init,
115         .cong_signal = cubic_cong_signal,
116         .conn_init = cubic_conn_init,
117         .mod_init = cubic_mod_init,
118         .post_recovery = cubic_post_recovery,
119         .after_idle = cubic_after_idle,
120 };
121
122 static void
123 cubic_ack_received(struct cc_var *ccv, uint16_t type)
124 {
125         struct cubic *cubic_data;
126         unsigned long w_tf, w_cubic_next;
127         int ticks_since_cong;
128
129         cubic_data = ccv->cc_data;
130         cubic_record_rtt(ccv);
131
132         /*
133          * Regular ACK and we're not in cong/fast recovery and we're cwnd
134          * limited and we're either not doing ABC or are slow starting or are
135          * doing ABC and we've sent a cwnd's worth of bytes.
136          */
137         if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
138             (ccv->flags & CCF_CWND_LIMITED) && (!V_tcp_do_rfc3465 ||
139             CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||
140             (V_tcp_do_rfc3465 && ccv->flags & CCF_ABC_SENTAWND))) {
141                  /* Use the logic in NewReno ack_received() for slow start. */
142                 if (CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||
143                     cubic_data->min_rtt_ticks == TCPTV_SRTTBASE) {
144                         cubic_data->flags |= CUBICFLAG_IN_SLOWSTART;
145                         newreno_cc_algo.ack_received(ccv, type);
146                 } else {
147                         ticks_since_cong = ticks - cubic_data->t_last_cong;
148
149                         if (cubic_data->flags & (CUBICFLAG_IN_SLOWSTART |
150                                                  CUBICFLAG_IN_APPLIMIT)) {
151                                 cubic_data->flags &= ~(CUBICFLAG_IN_SLOWSTART |
152                                                        CUBICFLAG_IN_APPLIMIT);
153                                 cubic_data->t_last_cong = ticks;
154                                 cubic_data->K = 0;
155                         }
156                         /*
157                          * The mean RTT is used to best reflect the equations in
158                          * the I-D. Using min_rtt in the tf_cwnd calculation
159                          * causes w_tf to grow much faster than it should if the
160                          * RTT is dominated by network buffering rather than
161                          * propagation delay.
162                          */
163                         w_tf = tf_cwnd(ticks_since_cong,
164                             cubic_data->mean_rtt_ticks, cubic_data->max_cwnd,
165                             CCV(ccv, t_maxseg));
166
167                         w_cubic_next = cubic_cwnd(ticks_since_cong +
168                             cubic_data->mean_rtt_ticks, cubic_data->max_cwnd,
169                             CCV(ccv, t_maxseg), cubic_data->K);
170
171                         ccv->flags &= ~CCF_ABC_SENTAWND;
172
173                         if (w_cubic_next < w_tf)
174                                 /*
175                                  * TCP-friendly region, follow tf
176                                  * cwnd growth.
177                                  */
178                                 CCV(ccv, snd_cwnd) = w_tf;
179
180                         else if (CCV(ccv, snd_cwnd) < w_cubic_next) {
181                                 /*
182                                  * Concave or convex region, follow CUBIC
183                                  * cwnd growth.
184                                  */
185                                 if (V_tcp_do_rfc3465)
186                                         CCV(ccv, snd_cwnd) = w_cubic_next;
187                                 else
188                                         CCV(ccv, snd_cwnd) += ((w_cubic_next -
189                                             CCV(ccv, snd_cwnd)) *
190                                             CCV(ccv, t_maxseg)) /
191                                             CCV(ccv, snd_cwnd);
192                         }
193
194                         /*
195                          * If we're not in slow start and we're probing for a
196                          * new cwnd limit at the start of a connection
197                          * (happens when hostcache has a relevant entry),
198                          * keep updating our current estimate of the
199                          * max_cwnd.
200                          */
201                         if (((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) &&
202                             cubic_data->max_cwnd < CCV(ccv, snd_cwnd)) {
203                                 cubic_data->max_cwnd = CCV(ccv, snd_cwnd);
204                                 cubic_data->K = cubic_k(cubic_data->max_cwnd /
205                                     CCV(ccv, t_maxseg));
206                         }
207                 }
208         } else if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
209             !(ccv->flags & CCF_CWND_LIMITED)) {
210                 cubic_data->flags |= CUBICFLAG_IN_APPLIMIT;
211         }
212 }
213
214 /*
215  * This is a Cubic specific implementation of after_idle.
216  *   - Reset cwnd by calling New Reno implementation of after_idle.
217  *   - Reset t_last_cong.
218  */
219 static void
220 cubic_after_idle(struct cc_var *ccv)
221 {
222         struct cubic *cubic_data;
223
224         cubic_data = ccv->cc_data;
225
226         cubic_data->max_cwnd = ulmax(cubic_data->max_cwnd, CCV(ccv, snd_cwnd));
227         cubic_data->K = cubic_k(cubic_data->max_cwnd / CCV(ccv, t_maxseg));
228
229         newreno_cc_algo.after_idle(ccv);
230         cubic_data->t_last_cong = ticks;
231 }
232
233
234 static void
235 cubic_cb_destroy(struct cc_var *ccv)
236 {
237         free(ccv->cc_data, M_CUBIC);
238 }
239
240 static int
241 cubic_cb_init(struct cc_var *ccv)
242 {
243         struct cubic *cubic_data;
244
245         cubic_data = malloc(sizeof(struct cubic), M_CUBIC, M_NOWAIT|M_ZERO);
246
247         if (cubic_data == NULL)
248                 return (ENOMEM);
249
250         /* Init some key variables with sensible defaults. */
251         cubic_data->t_last_cong = ticks;
252         cubic_data->min_rtt_ticks = TCPTV_SRTTBASE;
253         cubic_data->mean_rtt_ticks = 1;
254
255         ccv->cc_data = cubic_data;
256
257         return (0);
258 }
259
260 /*
261  * Perform any necessary tasks before we enter congestion recovery.
262  */
263 static void
264 cubic_cong_signal(struct cc_var *ccv, uint32_t type)
265 {
266         struct cubic *cubic_data;
267
268         cubic_data = ccv->cc_data;
269
270         switch (type) {
271         case CC_NDUPACK:
272                 if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
273                         if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
274                                 cubic_ssthresh_update(ccv);
275                                 cubic_data->flags |= CUBICFLAG_CONG_EVENT;
276                                 cubic_data->prev_max_cwnd = cubic_data->max_cwnd;
277                                 cubic_data->max_cwnd = CCV(ccv, snd_cwnd);
278                                 cubic_data->K = cubic_k(cubic_data->max_cwnd / CCV(ccv, t_maxseg));
279                         }
280                         ENTER_RECOVERY(CCV(ccv, t_flags));
281                 }
282                 break;
283
284         case CC_ECN:
285                 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
286                         cubic_ssthresh_update(ccv);
287                         cubic_data->flags |= CUBICFLAG_CONG_EVENT;
288                         cubic_data->prev_max_cwnd = cubic_data->max_cwnd;
289                         cubic_data->max_cwnd = CCV(ccv, snd_cwnd);
290                         cubic_data->t_last_cong = ticks;
291                         cubic_data->K = cubic_k(cubic_data->max_cwnd / CCV(ccv, t_maxseg));
292                         CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
293                         ENTER_CONGRECOVERY(CCV(ccv, t_flags));
294                 }
295                 break;
296
297         case CC_RTO:
298                 /*
299                  * Grab the current time and record it so we know when the
300                  * most recent congestion event was. Only record it when the
301                  * timeout has fired more than once, as there is a reasonable
302                  * chance the first one is a false alarm and may not indicate
303                  * congestion.
304                  */
305                 if (CCV(ccv, t_rxtshift) >= 2) {
306                         cubic_data->flags |= CUBICFLAG_CONG_EVENT;
307                         cubic_data->t_last_cong = ticks;
308                 }
309                 break;
310         }
311 }
312
313 static void
314 cubic_conn_init(struct cc_var *ccv)
315 {
316         struct cubic *cubic_data;
317
318         cubic_data = ccv->cc_data;
319
320         /*
321          * Ensure we have a sane initial value for max_cwnd recorded. Without
322          * this here bad things happen when entries from the TCP hostcache
323          * get used.
324          */
325         cubic_data->max_cwnd = CCV(ccv, snd_cwnd);
326 }
327
328 static int
329 cubic_mod_init(void)
330 {
331         return (0);
332 }
333
334 /*
335  * Perform any necessary tasks before we exit congestion recovery.
336  */
337 static void
338 cubic_post_recovery(struct cc_var *ccv)
339 {
340         struct cubic *cubic_data;
341         int pipe;
342
343         cubic_data = ccv->cc_data;
344         pipe = 0;
345
346         /* Fast convergence heuristic. */
347         if (cubic_data->max_cwnd < cubic_data->prev_max_cwnd)
348                 cubic_data->max_cwnd = (cubic_data->max_cwnd * CUBIC_FC_FACTOR)
349                     >> CUBIC_SHIFT;
350
351         if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
352                 /*
353                  * If inflight data is less than ssthresh, set cwnd
354                  * conservatively to avoid a burst of data, as suggested in
355                  * the NewReno RFC. Otherwise, use the CUBIC method.
356                  *
357                  * XXXLAS: Find a way to do this without needing curack
358                  */
359                 if (V_tcp_do_rfc6675_pipe)
360                         pipe = tcp_compute_pipe(ccv->ccvc.tcp);
361                 else
362                         pipe = CCV(ccv, snd_max) - ccv->curack;
363
364                 if (pipe < CCV(ccv, snd_ssthresh))
365                         /*
366                          * Ensure that cwnd does not collapse to 1 MSS under
367                          * adverse conditions. Implements RFC6582
368                          */
369                         CCV(ccv, snd_cwnd) = max(pipe, CCV(ccv, t_maxseg)) +
370                             CCV(ccv, t_maxseg);
371                 else
372                         /* Update cwnd based on beta and adjusted max_cwnd. */
373                         CCV(ccv, snd_cwnd) = max(((uint64_t)cubic_data->max_cwnd *
374                             CUBIC_BETA) >> CUBIC_SHIFT,
375                             2 * CCV(ccv, t_maxseg));
376         }
377         cubic_data->t_last_cong = ticks;
378
379         /* Calculate the average RTT between congestion epochs. */
380         if (cubic_data->epoch_ack_count > 0 &&
381             cubic_data->sum_rtt_ticks >= cubic_data->epoch_ack_count) {
382                 cubic_data->mean_rtt_ticks = (int)(cubic_data->sum_rtt_ticks /
383                     cubic_data->epoch_ack_count);
384         }
385
386         cubic_data->epoch_ack_count = 0;
387         cubic_data->sum_rtt_ticks = 0;
388         cubic_data->K = cubic_k(cubic_data->max_cwnd / CCV(ccv, t_maxseg));
389 }
390
391 /*
392  * Record the min RTT and sum samples for the epoch average RTT calculation.
393  */
394 static void
395 cubic_record_rtt(struct cc_var *ccv)
396 {
397         struct cubic *cubic_data;
398         int t_srtt_ticks;
399
400         /* Ignore srtt until a min number of samples have been taken. */
401         if (CCV(ccv, t_rttupdated) >= CUBIC_MIN_RTT_SAMPLES) {
402                 cubic_data = ccv->cc_data;
403                 t_srtt_ticks = CCV(ccv, t_srtt) / TCP_RTT_SCALE;
404
405                 /*
406                  * Record the current SRTT as our minrtt if it's the smallest
407                  * we've seen or minrtt is currently equal to its initialised
408                  * value.
409                  *
410                  * XXXLAS: Should there be some hysteresis for minrtt?
411                  */
412                 if ((t_srtt_ticks < cubic_data->min_rtt_ticks ||
413                     cubic_data->min_rtt_ticks == TCPTV_SRTTBASE)) {
414                         cubic_data->min_rtt_ticks = max(1, t_srtt_ticks);
415
416                         /*
417                          * If the connection is within its first congestion
418                          * epoch, ensure we prime mean_rtt_ticks with a
419                          * reasonable value until the epoch average RTT is
420                          * calculated in cubic_post_recovery().
421                          */
422                         if (cubic_data->min_rtt_ticks >
423                             cubic_data->mean_rtt_ticks)
424                                 cubic_data->mean_rtt_ticks =
425                                     cubic_data->min_rtt_ticks;
426                 }
427
428                 /* Sum samples for epoch average RTT calculation. */
429                 cubic_data->sum_rtt_ticks += t_srtt_ticks;
430                 cubic_data->epoch_ack_count++;
431         }
432 }
433
434 /*
435  * Update the ssthresh in the event of congestion.
436  */
437 static void
438 cubic_ssthresh_update(struct cc_var *ccv)
439 {
440         struct cubic *cubic_data;
441         uint32_t ssthresh;
442
443         cubic_data = ccv->cc_data;
444
445         /*
446          * On the first congestion event, set ssthresh to cwnd * 0.5, on
447          * subsequent congestion events, set it to cwnd * beta.
448          */
449         if ((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0)
450                 ssthresh = CCV(ccv, snd_cwnd) >> 1;
451         else
452                 ssthresh = ((uint64_t)CCV(ccv, snd_cwnd) *
453                     CUBIC_BETA) >> CUBIC_SHIFT;
454         CCV(ccv, snd_ssthresh) = max(ssthresh, 2 * CCV(ccv, t_maxseg));
455 }
456
457
458 DECLARE_CC_MODULE(cubic, &cubic_cc_algo);