2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org>
5 * Copyright (c) 2010 The FreeBSD Foundation
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.
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.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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
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/
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/kernel.h>
55 #include <sys/limits.h>
56 #include <sys/malloc.h>
57 #include <sys/module.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/sysctl.h>
61 #include <sys/systm.h>
65 #include <netinet/tcp.h>
66 #include <netinet/tcp_seq.h>
67 #include <netinet/tcp_timer.h>
68 #include <netinet/tcp_var.h>
69 #include <netinet/cc/cc.h>
70 #include <netinet/cc/cc_cubic.h>
71 #include <netinet/cc/cc_module.h>
73 static void cubic_ack_received(struct cc_var *ccv, uint16_t type);
74 static void cubic_cb_destroy(struct cc_var *ccv);
75 static int cubic_cb_init(struct cc_var *ccv);
76 static void cubic_cong_signal(struct cc_var *ccv, uint32_t type);
77 static void cubic_conn_init(struct cc_var *ccv);
78 static int cubic_mod_init(void);
79 static void cubic_post_recovery(struct cc_var *ccv);
80 static void cubic_record_rtt(struct cc_var *ccv);
81 static void cubic_ssthresh_update(struct cc_var *ccv);
82 static void cubic_after_idle(struct cc_var *ccv);
85 /* Cubic K in fixed point form with CUBIC_SHIFT worth of precision. */
87 /* Sum of RTT samples across an epoch in ticks. */
88 int64_t sum_rtt_ticks;
89 /* cwnd at the most recent congestion event. */
90 unsigned long max_cwnd;
91 /* cwnd at the previous congestion event. */
92 unsigned long prev_max_cwnd;
95 #define CUBICFLAG_CONG_EVENT 0x00000001 /* congestion experienced */
96 #define CUBICFLAG_IN_SLOWSTART 0x00000002 /* in slow start */
97 #define CUBICFLAG_IN_APPLIMIT 0x00000004 /* application limited */
98 /* Minimum observed rtt in ticks. */
100 /* Mean observed rtt between congestion epochs. */
102 /* ACKs since last congestion event. */
104 /* Time of last congestion event in ticks. */
108 static MALLOC_DEFINE(M_CUBIC, "cubic data",
109 "Per connection data required for the CUBIC congestion control algorithm");
111 struct cc_algo cubic_cc_algo = {
113 .ack_received = cubic_ack_received,
114 .cb_destroy = cubic_cb_destroy,
115 .cb_init = cubic_cb_init,
116 .cong_signal = cubic_cong_signal,
117 .conn_init = cubic_conn_init,
118 .mod_init = cubic_mod_init,
119 .post_recovery = cubic_post_recovery,
120 .after_idle = cubic_after_idle,
124 cubic_ack_received(struct cc_var *ccv, uint16_t type)
126 struct cubic *cubic_data;
127 unsigned long w_tf, w_cubic_next;
128 int ticks_since_cong;
130 cubic_data = ccv->cc_data;
131 cubic_record_rtt(ccv);
134 * Regular ACK and we're not in cong/fast recovery and we're cwnd
135 * limited and we're either not doing ABC or are just coming out
136 * from slow-start or were application limited or are slow starting
137 * or are doing ABC and we've sent a cwnd's worth of bytes.
139 if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
140 (ccv->flags & CCF_CWND_LIMITED) && (!V_tcp_do_rfc3465 ||
141 (cubic_data->flags & (CUBICFLAG_IN_SLOWSTART | CUBICFLAG_IN_APPLIMIT)) ||
142 CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||
143 (V_tcp_do_rfc3465 && (ccv->flags & CCF_ABC_SENTAWND)))) {
144 /* Use the logic in NewReno ack_received() for slow start. */
145 if (CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||
146 cubic_data->min_rtt_ticks == TCPTV_SRTTBASE) {
147 cubic_data->flags |= CUBICFLAG_IN_SLOWSTART;
148 newreno_cc_algo.ack_received(ccv, type);
150 if (cubic_data->flags & (CUBICFLAG_IN_SLOWSTART |
151 CUBICFLAG_IN_APPLIMIT)) {
152 cubic_data->flags &= ~(CUBICFLAG_IN_SLOWSTART |
153 CUBICFLAG_IN_APPLIMIT);
154 cubic_data->t_last_cong = ticks;
155 cubic_data->K = cubic_k(cubic_data->max_cwnd /
158 if ((ticks_since_cong =
159 ticks - cubic_data->t_last_cong) < 0) {
161 * dragging t_last_cong along
163 ticks_since_cong = INT_MAX;
164 cubic_data->t_last_cong = ticks - INT_MAX;
167 * The mean RTT is used to best reflect the equations in
168 * the I-D. Using min_rtt in the tf_cwnd calculation
169 * causes w_tf to grow much faster than it should if the
170 * RTT is dominated by network buffering rather than
173 w_tf = tf_cwnd(ticks_since_cong,
174 cubic_data->mean_rtt_ticks, cubic_data->max_cwnd,
177 w_cubic_next = cubic_cwnd(ticks_since_cong +
178 cubic_data->mean_rtt_ticks, cubic_data->max_cwnd,
179 CCV(ccv, t_maxseg), cubic_data->K);
181 ccv->flags &= ~CCF_ABC_SENTAWND;
183 if (w_cubic_next < w_tf) {
185 * TCP-friendly region, follow tf
188 if (CCV(ccv, snd_cwnd) < w_tf)
189 CCV(ccv, snd_cwnd) = ulmin(w_tf, INT_MAX);
190 } else if (CCV(ccv, snd_cwnd) < w_cubic_next) {
192 * Concave or convex region, follow CUBIC
194 * Only update snd_cwnd, if it doesn't shrink.
196 if (V_tcp_do_rfc3465)
197 CCV(ccv, snd_cwnd) = ulmin(w_cubic_next,
200 CCV(ccv, snd_cwnd) += ulmax(1,
201 ((ulmin(w_cubic_next, INT_MAX) -
202 CCV(ccv, snd_cwnd)) *
203 CCV(ccv, t_maxseg)) /
208 * If we're not in slow start and we're probing for a
209 * new cwnd limit at the start of a connection
210 * (happens when hostcache has a relevant entry),
211 * keep updating our current estimate of the
214 if (((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) &&
215 cubic_data->max_cwnd < CCV(ccv, snd_cwnd)) {
216 cubic_data->max_cwnd = CCV(ccv, snd_cwnd);
217 cubic_data->K = cubic_k(cubic_data->max_cwnd /
221 } else if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
222 !(ccv->flags & CCF_CWND_LIMITED)) {
223 cubic_data->flags |= CUBICFLAG_IN_APPLIMIT;
228 * This is a Cubic specific implementation of after_idle.
229 * - Reset cwnd by calling New Reno implementation of after_idle.
230 * - Reset t_last_cong.
233 cubic_after_idle(struct cc_var *ccv)
235 struct cubic *cubic_data;
237 cubic_data = ccv->cc_data;
239 cubic_data->max_cwnd = ulmax(cubic_data->max_cwnd, CCV(ccv, snd_cwnd));
240 cubic_data->K = cubic_k(cubic_data->max_cwnd / CCV(ccv, t_maxseg));
242 newreno_cc_algo.after_idle(ccv);
243 cubic_data->t_last_cong = ticks;
248 cubic_cb_destroy(struct cc_var *ccv)
250 free(ccv->cc_data, M_CUBIC);
254 cubic_cb_init(struct cc_var *ccv)
256 struct cubic *cubic_data;
258 cubic_data = malloc(sizeof(struct cubic), M_CUBIC, M_NOWAIT|M_ZERO);
260 if (cubic_data == NULL)
263 /* Init some key variables with sensible defaults. */
264 cubic_data->t_last_cong = ticks;
265 cubic_data->min_rtt_ticks = TCPTV_SRTTBASE;
266 cubic_data->mean_rtt_ticks = 1;
268 ccv->cc_data = cubic_data;
274 * Perform any necessary tasks before we enter congestion recovery.
277 cubic_cong_signal(struct cc_var *ccv, uint32_t type)
279 struct cubic *cubic_data;
281 cubic_data = ccv->cc_data;
285 if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
286 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
287 cubic_ssthresh_update(ccv);
288 cubic_data->flags |= CUBICFLAG_CONG_EVENT;
289 cubic_data->t_last_cong = ticks;
290 cubic_data->K = cubic_k(cubic_data->max_cwnd / CCV(ccv, t_maxseg));
292 ENTER_RECOVERY(CCV(ccv, t_flags));
297 if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
298 cubic_ssthresh_update(ccv);
299 cubic_data->flags |= CUBICFLAG_CONG_EVENT;
300 cubic_data->t_last_cong = ticks;
301 cubic_data->K = cubic_k(cubic_data->max_cwnd / CCV(ccv, t_maxseg));
302 CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
303 ENTER_CONGRECOVERY(CCV(ccv, t_flags));
309 * Grab the current time and record it so we know when the
310 * most recent congestion event was. Only record it when the
311 * timeout has fired more than once, as there is a reasonable
312 * chance the first one is a false alarm and may not indicate
314 * This will put Cubic firmly into the concave / TCP friendly
315 * region, for a slower ramp-up after two consecutive RTOs.
317 if (CCV(ccv, t_rxtshift) >= 2) {
318 cubic_data->flags |= CUBICFLAG_CONG_EVENT;
319 cubic_data->t_last_cong = ticks;
320 cubic_data->max_cwnd = CCV(ccv, snd_cwnd_prev);
321 cubic_data->K = cubic_k(cubic_data->max_cwnd /
329 cubic_conn_init(struct cc_var *ccv)
331 struct cubic *cubic_data;
333 cubic_data = ccv->cc_data;
336 * Ensure we have a sane initial value for max_cwnd recorded. Without
337 * this here bad things happen when entries from the TCP hostcache
340 cubic_data->max_cwnd = CCV(ccv, snd_cwnd);
350 * Perform any necessary tasks before we exit congestion recovery.
353 cubic_post_recovery(struct cc_var *ccv)
355 struct cubic *cubic_data;
358 cubic_data = ccv->cc_data;
361 if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
363 * If inflight data is less than ssthresh, set cwnd
364 * conservatively to avoid a burst of data, as suggested in
365 * the NewReno RFC. Otherwise, use the CUBIC method.
367 * XXXLAS: Find a way to do this without needing curack
369 if (V_tcp_do_rfc6675_pipe)
370 pipe = tcp_compute_pipe(ccv->ccvc.tcp);
372 pipe = CCV(ccv, snd_max) - ccv->curack;
374 if (pipe < CCV(ccv, snd_ssthresh))
376 * Ensure that cwnd does not collapse to 1 MSS under
377 * adverse conditions. Implements RFC6582
379 CCV(ccv, snd_cwnd) = max(pipe, CCV(ccv, t_maxseg)) +
382 /* Update cwnd based on beta and adjusted max_cwnd. */
383 CCV(ccv, snd_cwnd) = max(((uint64_t)cubic_data->max_cwnd *
384 CUBIC_BETA) >> CUBIC_SHIFT,
385 2 * CCV(ccv, t_maxseg));
388 /* Calculate the average RTT between congestion epochs. */
389 if (cubic_data->epoch_ack_count > 0 &&
390 cubic_data->sum_rtt_ticks >= cubic_data->epoch_ack_count) {
391 cubic_data->mean_rtt_ticks = (int)(cubic_data->sum_rtt_ticks /
392 cubic_data->epoch_ack_count);
395 cubic_data->epoch_ack_count = 0;
396 cubic_data->sum_rtt_ticks = 0;
400 * Record the min RTT and sum samples for the epoch average RTT calculation.
403 cubic_record_rtt(struct cc_var *ccv)
405 struct cubic *cubic_data;
408 /* Ignore srtt until a min number of samples have been taken. */
409 if (CCV(ccv, t_rttupdated) >= CUBIC_MIN_RTT_SAMPLES) {
410 cubic_data = ccv->cc_data;
411 t_srtt_ticks = CCV(ccv, t_srtt) / TCP_RTT_SCALE;
414 * Record the current SRTT as our minrtt if it's the smallest
415 * we've seen or minrtt is currently equal to its initialised
418 * XXXLAS: Should there be some hysteresis for minrtt?
420 if ((t_srtt_ticks < cubic_data->min_rtt_ticks ||
421 cubic_data->min_rtt_ticks == TCPTV_SRTTBASE)) {
422 cubic_data->min_rtt_ticks = max(1, t_srtt_ticks);
425 * If the connection is within its first congestion
426 * epoch, ensure we prime mean_rtt_ticks with a
427 * reasonable value until the epoch average RTT is
428 * calculated in cubic_post_recovery().
430 if (cubic_data->min_rtt_ticks >
431 cubic_data->mean_rtt_ticks)
432 cubic_data->mean_rtt_ticks =
433 cubic_data->min_rtt_ticks;
436 /* Sum samples for epoch average RTT calculation. */
437 cubic_data->sum_rtt_ticks += t_srtt_ticks;
438 cubic_data->epoch_ack_count++;
443 * Update the ssthresh in the event of congestion.
446 cubic_ssthresh_update(struct cc_var *ccv)
448 struct cubic *cubic_data;
452 cubic_data = ccv->cc_data;
453 cwnd = CCV(ccv, snd_cwnd);
455 /* Fast convergence heuristic. */
456 if (cwnd < cubic_data->max_cwnd) {
457 cwnd = ((uint64_t)cwnd * CUBIC_FC_FACTOR) >> CUBIC_SHIFT;
459 cubic_data->prev_max_cwnd = cubic_data->max_cwnd;
460 cubic_data->max_cwnd = cwnd;
463 * On the first congestion event, set ssthresh to cwnd * 0.5
464 * and reduce max_cwnd to cwnd * beta. This aligns the cubic concave
465 * region appropriately. On subsequent congestion events, set
466 * ssthresh to cwnd * beta.
468 if ((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) {
469 ssthresh = cwnd >> 1;
470 cubic_data->max_cwnd = ((uint64_t)cwnd *
471 CUBIC_BETA) >> CUBIC_SHIFT;
473 ssthresh = ((uint64_t)cwnd *
474 CUBIC_BETA) >> CUBIC_SHIFT;
476 CCV(ccv, snd_ssthresh) = max(ssthresh, 2 * CCV(ccv, t_maxseg));
480 DECLARE_CC_MODULE(cubic, &cubic_cc_algo);
481 MODULE_VERSION(cubic, 1);