167fef78bSLawrence Stewart /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni *
467fef78bSLawrence Stewart * Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org>
567fef78bSLawrence Stewart * Copyright (c) 2010 The FreeBSD Foundation
667fef78bSLawrence Stewart * All rights reserved.
767fef78bSLawrence Stewart *
867fef78bSLawrence Stewart * This software was developed by Lawrence Stewart while studying at the Centre
9891b8ed4SLawrence Stewart * for Advanced Internet Architectures, Swinburne University of Technology, made
10891b8ed4SLawrence Stewart * possible in part by a grant from the Cisco University Research Program Fund
11891b8ed4SLawrence Stewart * at Community Foundation Silicon Valley.
1267fef78bSLawrence Stewart *
1367fef78bSLawrence Stewart * Portions of this software were developed at the Centre for Advanced
1467fef78bSLawrence Stewart * Internet Architectures, Swinburne University of Technology, Melbourne,
1567fef78bSLawrence Stewart * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
1667fef78bSLawrence Stewart *
1767fef78bSLawrence Stewart * Redistribution and use in source and binary forms, with or without
1867fef78bSLawrence Stewart * modification, are permitted provided that the following conditions
1967fef78bSLawrence Stewart * are met:
2067fef78bSLawrence Stewart * 1. Redistributions of source code must retain the above copyright
2167fef78bSLawrence Stewart * notice, this list of conditions and the following disclaimer.
2267fef78bSLawrence Stewart * 2. Redistributions in binary form must reproduce the above copyright
2367fef78bSLawrence Stewart * notice, this list of conditions and the following disclaimer in the
2467fef78bSLawrence Stewart * documentation and/or other materials provided with the distribution.
2567fef78bSLawrence Stewart *
2667fef78bSLawrence Stewart * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2767fef78bSLawrence Stewart * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2867fef78bSLawrence Stewart * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2967fef78bSLawrence Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3067fef78bSLawrence Stewart * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3167fef78bSLawrence Stewart * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3267fef78bSLawrence Stewart * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3367fef78bSLawrence Stewart * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3467fef78bSLawrence Stewart * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3567fef78bSLawrence Stewart * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3667fef78bSLawrence Stewart * SUCH DAMAGE.
3767fef78bSLawrence Stewart */
3867fef78bSLawrence Stewart
3967fef78bSLawrence Stewart /*
4067fef78bSLawrence Stewart * An implementation of the CUBIC congestion control algorithm for FreeBSD,
4167fef78bSLawrence Stewart * based on the Internet Draft "draft-rhee-tcpm-cubic-02" by Rhee, Xu and Ha.
4267fef78bSLawrence Stewart * Originally released as part of the NewTCP research project at Swinburne
43891b8ed4SLawrence Stewart * University of Technology's Centre for Advanced Internet Architectures,
44891b8ed4SLawrence Stewart * Melbourne, Australia, which was made possible in part by a grant from the
45891b8ed4SLawrence Stewart * Cisco University Research Program Fund at Community Foundation Silicon
46891b8ed4SLawrence Stewart * Valley. More details are available at:
4767fef78bSLawrence Stewart * http://caia.swin.edu.au/urp/newtcp/
4867fef78bSLawrence Stewart */
4967fef78bSLawrence Stewart
5067fef78bSLawrence Stewart #include <sys/param.h>
5167fef78bSLawrence Stewart #include <sys/kernel.h>
52c968c769SMichael Tuexen #include <sys/limits.h>
5367fef78bSLawrence Stewart #include <sys/malloc.h>
5467fef78bSLawrence Stewart #include <sys/module.h>
5567fef78bSLawrence Stewart #include <sys/socket.h>
5667fef78bSLawrence Stewart #include <sys/socketvar.h>
5767fef78bSLawrence Stewart #include <sys/sysctl.h>
5867fef78bSLawrence Stewart #include <sys/systm.h>
5967fef78bSLawrence Stewart
6067fef78bSLawrence Stewart #include <net/vnet.h>
6167fef78bSLawrence Stewart
62b8d60729SRandall Stewart #include <net/route.h>
63b8d60729SRandall Stewart #include <net/route/nhop.h>
64b8d60729SRandall Stewart
65b8d60729SRandall Stewart #include <netinet/in_pcb.h>
662de3e790SGleb Smirnoff #include <netinet/tcp.h>
6767fef78bSLawrence Stewart #include <netinet/tcp_seq.h>
6867fef78bSLawrence Stewart #include <netinet/tcp_timer.h>
6967fef78bSLawrence Stewart #include <netinet/tcp_var.h>
70a9696510SRandall Stewart #include <netinet/tcp_log_buf.h>
71a9696510SRandall Stewart #include <netinet/tcp_hpts.h>
724644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
7367fef78bSLawrence Stewart #include <netinet/cc/cc_cubic.h>
7467fef78bSLawrence Stewart #include <netinet/cc/cc_module.h>
7567fef78bSLawrence Stewart
76f74352fbSRichard Scheffenegger static void cubic_ack_received(struct cc_var *ccv, ccsignal_t type);
7767fef78bSLawrence Stewart static void cubic_cb_destroy(struct cc_var *ccv);
78b8d60729SRandall Stewart static int cubic_cb_init(struct cc_var *ccv, void *ptr);
79f74352fbSRichard Scheffenegger static void cubic_cong_signal(struct cc_var *ccv, ccsignal_t type);
8067fef78bSLawrence Stewart static void cubic_conn_init(struct cc_var *ccv);
8167fef78bSLawrence Stewart static int cubic_mod_init(void);
8267fef78bSLawrence Stewart static void cubic_post_recovery(struct cc_var *ccv);
8367fef78bSLawrence Stewart static void cubic_record_rtt(struct cc_var *ccv);
8437674273SRichard Scheffenegger static void cubic_ssthresh_update(struct cc_var *ccv, uint32_t maxseg);
8535cd141bSMichael Tuexen static void cubic_after_idle(struct cc_var *ccv);
86b8d60729SRandall Stewart static size_t cubic_data_sz(void);
87a9696510SRandall Stewart static void cubic_newround(struct cc_var *ccv, uint32_t round_cnt);
88a9696510SRandall Stewart static void cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt,
89a9696510SRandall Stewart uint32_t rxtcnt, uint32_t fas);
9067fef78bSLawrence Stewart
9167fef78bSLawrence Stewart struct cc_algo cubic_cc_algo = {
9267fef78bSLawrence Stewart .name = "cubic",
9367fef78bSLawrence Stewart .ack_received = cubic_ack_received,
9467fef78bSLawrence Stewart .cb_destroy = cubic_cb_destroy,
9567fef78bSLawrence Stewart .cb_init = cubic_cb_init,
9667fef78bSLawrence Stewart .cong_signal = cubic_cong_signal,
9767fef78bSLawrence Stewart .conn_init = cubic_conn_init,
9867fef78bSLawrence Stewart .mod_init = cubic_mod_init,
9967fef78bSLawrence Stewart .post_recovery = cubic_post_recovery,
10035cd141bSMichael Tuexen .after_idle = cubic_after_idle,
101a9696510SRandall Stewart .cc_data_sz = cubic_data_sz,
102a9696510SRandall Stewart .rttsample = cubic_rttsample,
103a9696510SRandall Stewart .newround = cubic_newround
10467fef78bSLawrence Stewart };
10567fef78bSLawrence Stewart
10667fef78bSLawrence Stewart static void
cubic_log_hystart_event(struct cc_var * ccv,struct cubic * cubicd,uint8_t mod,uint32_t flex1)107a9696510SRandall Stewart cubic_log_hystart_event(struct cc_var *ccv, struct cubic *cubicd, uint8_t mod, uint32_t flex1)
108a9696510SRandall Stewart {
109a9696510SRandall Stewart /*
110a9696510SRandall Stewart * Types of logs (mod value)
111a9696510SRandall Stewart * 1 - rtt_thresh in flex1, checking to see if RTT is to great.
112a9696510SRandall Stewart * 2 - rtt is too great, rtt_thresh in flex1.
113a9696510SRandall Stewart * 3 - CSS is active incr in flex1
114a9696510SRandall Stewart * 4 - A new round is beginning flex1 is round count
115a9696510SRandall Stewart * 5 - A new RTT measurement flex1 is the new measurement.
116a9696510SRandall Stewart * 6 - We enter CA ssthresh is also in flex1.
117a9696510SRandall Stewart * 7 - Socket option to change hystart executed opt.val in flex1.
118a9696510SRandall Stewart * 8 - Back out of CSS into SS, flex1 is the css_baseline_minrtt
119a9696510SRandall Stewart * 9 - We enter CA, via an ECN mark.
120a9696510SRandall Stewart * 10 - We enter CA, via a loss.
121a9696510SRandall Stewart * 11 - We have slipped out of SS into CA via cwnd growth.
122a9696510SRandall Stewart * 12 - After idle has re-enabled hystart++
123a9696510SRandall Stewart */
124a9696510SRandall Stewart struct tcpcb *tp;
125a9696510SRandall Stewart
126a9696510SRandall Stewart if (hystart_bblogs == 0)
127a9696510SRandall Stewart return;
12800d3b744SMichael Tuexen tp = ccv->tp;
12969c7c811SRandall Stewart if (tcp_bblogging_on(tp)) {
130a9696510SRandall Stewart union tcp_log_stackspecific log;
131a9696510SRandall Stewart struct timeval tv;
132a9696510SRandall Stewart
133a9696510SRandall Stewart memset(&log, 0, sizeof(log));
134a9696510SRandall Stewart log.u_bbr.flex1 = flex1;
135a9696510SRandall Stewart log.u_bbr.flex2 = cubicd->css_current_round_minrtt;
136a9696510SRandall Stewart log.u_bbr.flex3 = cubicd->css_lastround_minrtt;
137a9696510SRandall Stewart log.u_bbr.flex4 = cubicd->css_rttsample_count;
138a9696510SRandall Stewart log.u_bbr.flex5 = cubicd->css_entered_at_round;
139a9696510SRandall Stewart log.u_bbr.flex6 = cubicd->css_baseline_minrtt;
140a9696510SRandall Stewart /* We only need bottom 16 bits of flags */
141a9696510SRandall Stewart log.u_bbr.flex7 = cubicd->flags & 0x0000ffff;
142a9696510SRandall Stewart log.u_bbr.flex8 = mod;
143a9696510SRandall Stewart log.u_bbr.epoch = cubicd->css_current_round;
144a9696510SRandall Stewart log.u_bbr.timeStamp = tcp_get_usecs(&tv);
145a9696510SRandall Stewart log.u_bbr.lt_epoch = cubicd->css_fas_at_css_entry;
146a9696510SRandall Stewart log.u_bbr.pkts_out = cubicd->css_last_fas;
147a9696510SRandall Stewart log.u_bbr.delivered = cubicd->css_lowrtt_fas;
148a9696510SRandall Stewart log.u_bbr.pkt_epoch = ccv->flags;
149a9696510SRandall Stewart TCP_LOG_EVENTP(tp, NULL,
1509eb0e832SGleb Smirnoff &tptosocket(tp)->so_rcv,
1519eb0e832SGleb Smirnoff &tptosocket(tp)->so_snd,
152a9696510SRandall Stewart TCP_HYSTART, 0,
153a9696510SRandall Stewart 0, &log, false, &tv);
154a9696510SRandall Stewart }
155a9696510SRandall Stewart }
156a9696510SRandall Stewart
157a9696510SRandall Stewart static void
cubic_does_slow_start(struct cc_var * ccv,struct cubic * cubicd)158a9696510SRandall Stewart cubic_does_slow_start(struct cc_var *ccv, struct cubic *cubicd)
159a9696510SRandall Stewart {
160a9696510SRandall Stewart /*
161a9696510SRandall Stewart * In slow-start with ABC enabled and no RTO in sight?
162a9696510SRandall Stewart * (Must not use abc_l_var > 1 if slow starting after
163a9696510SRandall Stewart * an RTO. On RTO, snd_nxt = snd_una, so the
164a9696510SRandall Stewart * snd_nxt == snd_max check is sufficient to
165a9696510SRandall Stewart * handle this).
166a9696510SRandall Stewart *
167a9696510SRandall Stewart * XXXLAS: Find a way to signal SS after RTO that
168a9696510SRandall Stewart * doesn't rely on tcpcb vars.
169a9696510SRandall Stewart */
170a9696510SRandall Stewart u_int cw = CCV(ccv, snd_cwnd);
17122dcc812SRichard Scheffenegger uint32_t mss = tcp_fixed_maxseg(ccv->tp);
17222dcc812SRichard Scheffenegger u_int incr = mss;
173a9696510SRandall Stewart uint16_t abc_val;
174a9696510SRandall Stewart
175a9696510SRandall Stewart cubicd->flags |= CUBICFLAG_IN_SLOWSTART;
176a9696510SRandall Stewart if (ccv->flags & CCF_USE_LOCAL_ABC)
177a9696510SRandall Stewart abc_val = ccv->labc;
178a9696510SRandall Stewart else
179a9696510SRandall Stewart abc_val = V_tcp_abc_l_var;
180a9696510SRandall Stewart if ((ccv->flags & CCF_HYSTART_ALLOWED) &&
181a9696510SRandall Stewart (cubicd->flags & CUBICFLAG_HYSTART_ENABLED) &&
182a9696510SRandall Stewart ((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) == 0)) {
183a9696510SRandall Stewart /*
184a9696510SRandall Stewart * Hystart is allowed and still enabled and we are not yet
185a9696510SRandall Stewart * in CSS. Lets check to see if we can make a decision on
186a9696510SRandall Stewart * if we need to go into CSS.
187a9696510SRandall Stewart */
188a9696510SRandall Stewart if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) &&
189a9696510SRandall Stewart (cubicd->css_current_round_minrtt != 0xffffffff) &&
190a9696510SRandall Stewart (cubicd->css_lastround_minrtt != 0xffffffff)) {
191a9696510SRandall Stewart uint32_t rtt_thresh;
192a9696510SRandall Stewart
193a9696510SRandall Stewart /* Clamp (minrtt_thresh, lastround/8, maxrtt_thresh) */
194a9696510SRandall Stewart rtt_thresh = (cubicd->css_lastround_minrtt >> 3);
195a9696510SRandall Stewart if (rtt_thresh < hystart_minrtt_thresh)
196a9696510SRandall Stewart rtt_thresh = hystart_minrtt_thresh;
197a9696510SRandall Stewart if (rtt_thresh > hystart_maxrtt_thresh)
198a9696510SRandall Stewart rtt_thresh = hystart_maxrtt_thresh;
199a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 1, rtt_thresh);
200a9696510SRandall Stewart
201a9696510SRandall Stewart if (cubicd->css_current_round_minrtt >= (cubicd->css_lastround_minrtt + rtt_thresh)) {
202a9696510SRandall Stewart /* Enter CSS */
203a9696510SRandall Stewart cubicd->flags |= CUBICFLAG_HYSTART_IN_CSS;
204a9696510SRandall Stewart cubicd->css_fas_at_css_entry = cubicd->css_lowrtt_fas;
205a9696510SRandall Stewart /*
206a9696510SRandall Stewart * The draft (v4) calls for us to set baseline to css_current_round_min
207a9696510SRandall Stewart * but that can cause an oscillation. We probably shoudl be using
208a9696510SRandall Stewart * css_lastround_minrtt, but the authors insist that will cause
209a9696510SRandall Stewart * issues on exiting early. We will leave the draft version for now
210a9696510SRandall Stewart * but I suspect this is incorrect.
211a9696510SRandall Stewart */
212a9696510SRandall Stewart cubicd->css_baseline_minrtt = cubicd->css_current_round_minrtt;
213a9696510SRandall Stewart cubicd->css_entered_at_round = cubicd->css_current_round;
214a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 2, rtt_thresh);
215a9696510SRandall Stewart }
216a9696510SRandall Stewart }
217a9696510SRandall Stewart }
218a9696510SRandall Stewart if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max))
219a9696510SRandall Stewart incr = min(ccv->bytes_this_ack,
22022dcc812SRichard Scheffenegger ccv->nsegs * abc_val * mss);
221a9696510SRandall Stewart else
22222dcc812SRichard Scheffenegger incr = min(ccv->bytes_this_ack, mss);
223a9696510SRandall Stewart
224a9696510SRandall Stewart /* Only if Hystart is enabled will the flag get set */
225a9696510SRandall Stewart if (cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) {
226a9696510SRandall Stewart incr /= hystart_css_growth_div;
227a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 3, incr);
228a9696510SRandall Stewart }
229a9696510SRandall Stewart /* ABC is on by default, so incr equals 0 frequently. */
230a9696510SRandall Stewart if (incr > 0)
231a9696510SRandall Stewart CCV(ccv, snd_cwnd) = min((cw + incr),
232a9696510SRandall Stewart TCP_MAXWIN << CCV(ccv, snd_scale));
233a9696510SRandall Stewart }
234a9696510SRandall Stewart
235a9696510SRandall Stewart static void
cubic_ack_received(struct cc_var * ccv,ccsignal_t type)236f74352fbSRichard Scheffenegger cubic_ack_received(struct cc_var *ccv, ccsignal_t type)
23767fef78bSLawrence Stewart {
23867fef78bSLawrence Stewart struct cubic *cubic_data;
239eb5bfdd0SRichard Scheffenegger unsigned long W_est, W_cubic;
240eb5bfdd0SRichard Scheffenegger int usecs_since_epoch;
24122dcc812SRichard Scheffenegger uint32_t mss = tcp_fixed_maxseg(ccv->tp);
24267fef78bSLawrence Stewart
24367fef78bSLawrence Stewart cubic_data = ccv->cc_data;
24467fef78bSLawrence Stewart cubic_record_rtt(ccv);
24567fef78bSLawrence Stewart
24667fef78bSLawrence Stewart /*
247ad7a0eb1SRichard Scheffenegger * For a regular ACK and we're not in cong/fast recovery and
248ad7a0eb1SRichard Scheffenegger * we're cwnd limited, always recalculate cwnd.
24967fef78bSLawrence Stewart */
25067fef78bSLawrence Stewart if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
251ad7a0eb1SRichard Scheffenegger (ccv->flags & CCF_CWND_LIMITED)) {
25267fef78bSLawrence Stewart /* Use the logic in NewReno ack_received() for slow start. */
25367fef78bSLawrence Stewart if (CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh) ||
254a3aa6f65SCheng Cui cubic_data->min_rtt_usecs == TCPTV_SRTTBASE) {
255a9696510SRandall Stewart cubic_does_slow_start(ccv, cubic_data);
2566907bbaeSRichard Scheffenegger } else {
257a9696510SRandall Stewart if (cubic_data->flags & CUBICFLAG_HYSTART_IN_CSS) {
258a9696510SRandall Stewart /*
259a9696510SRandall Stewart * We have slipped into CA with
260a9696510SRandall Stewart * CSS active. Deactivate all.
261a9696510SRandall Stewart */
262a9696510SRandall Stewart /* Turn off the CSS flag */
263a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
264a9696510SRandall Stewart /* Disable use of CSS in the future except long idle */
265a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
266a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubic_data, 11, CCV(ccv, snd_ssthresh));
267a9696510SRandall Stewart }
26837674273SRichard Scheffenegger if ((cubic_data->flags & CUBICFLAG_RTO_EVENT) &&
26937674273SRichard Scheffenegger (cubic_data->flags & CUBICFLAG_IN_SLOWSTART)) {
27037674273SRichard Scheffenegger /* RFC8312 Section 4.7 */
27137674273SRichard Scheffenegger cubic_data->flags &= ~(CUBICFLAG_RTO_EVENT |
27237674273SRichard Scheffenegger CUBICFLAG_IN_SLOWSTART);
273eb5bfdd0SRichard Scheffenegger cubic_data->W_max = CCV(ccv, snd_cwnd);
274038699a8SRichard Scheffenegger cubic_data->t_epoch = ticks;
27537674273SRichard Scheffenegger cubic_data->K = 0;
27637674273SRichard Scheffenegger } else if (cubic_data->flags & (CUBICFLAG_IN_SLOWSTART |
2772bb6dfabSRichard Scheffenegger CUBICFLAG_IN_APPLIMIT)) {
2782bb6dfabSRichard Scheffenegger cubic_data->flags &= ~(CUBICFLAG_IN_SLOWSTART |
2792bb6dfabSRichard Scheffenegger CUBICFLAG_IN_APPLIMIT);
280eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = ticks;
28122dcc812SRichard Scheffenegger cubic_data->K = cubic_k(cubic_data->W_max / mss);
2822bb6dfabSRichard Scheffenegger }
283eb5bfdd0SRichard Scheffenegger usecs_since_epoch = (ticks - cubic_data->t_epoch) * tick;
284eb5bfdd0SRichard Scheffenegger if (usecs_since_epoch < 0) {
285c968c769SMichael Tuexen /*
286eb5bfdd0SRichard Scheffenegger * dragging t_epoch along
287c968c769SMichael Tuexen */
288eb5bfdd0SRichard Scheffenegger usecs_since_epoch = INT_MAX;
289eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = ticks - INT_MAX;
290c968c769SMichael Tuexen }
291ee450610SCheng Cui
292ee450610SCheng Cui W_est = tf_cwnd(ccv);
293ee450610SCheng Cui
29467fef78bSLawrence Stewart /*
29567fef78bSLawrence Stewart * The mean RTT is used to best reflect the equations in
296ee450610SCheng Cui * the I-D.
29767fef78bSLawrence Stewart */
298eb5bfdd0SRichard Scheffenegger W_cubic = cubic_cwnd(usecs_since_epoch +
299a3aa6f65SCheng Cui cubic_data->mean_rtt_usecs,
300eb5bfdd0SRichard Scheffenegger cubic_data->W_max,
301*6156da86SCheng Cui mss,
302a3aa6f65SCheng Cui cubic_data->K);
30367fef78bSLawrence Stewart
304eb5bfdd0SRichard Scheffenegger if (W_cubic < W_est) {
30567fef78bSLawrence Stewart /*
30667fef78bSLawrence Stewart * TCP-friendly region, follow tf
30767fef78bSLawrence Stewart * cwnd growth.
30867fef78bSLawrence Stewart */
309eb5bfdd0SRichard Scheffenegger CCV(ccv, snd_cwnd) = ulmin(W_est, INT_MAX);
310ee450610SCheng Cui cubic_data->flags |= CUBICFLAG_IN_TF;
311eb5bfdd0SRichard Scheffenegger } else if (CCV(ccv, snd_cwnd) < W_cubic) {
31267fef78bSLawrence Stewart /*
31367fef78bSLawrence Stewart * Concave or convex region, follow CUBIC
31467fef78bSLawrence Stewart * cwnd growth.
315cce999b3SRichard Scheffenegger * Only update snd_cwnd, if it doesn't shrink.
31667fef78bSLawrence Stewart */
317eb5bfdd0SRichard Scheffenegger CCV(ccv, snd_cwnd) = ulmin(W_cubic, INT_MAX);
318ee450610SCheng Cui cubic_data->flags &= ~CUBICFLAG_IN_TF;
31967fef78bSLawrence Stewart }
32067fef78bSLawrence Stewart
32167fef78bSLawrence Stewart /*
32267fef78bSLawrence Stewart * If we're not in slow start and we're probing for a
32367fef78bSLawrence Stewart * new cwnd limit at the start of a connection
32467fef78bSLawrence Stewart * (happens when hostcache has a relevant entry),
32567fef78bSLawrence Stewart * keep updating our current estimate of the
326eb5bfdd0SRichard Scheffenegger * W_max.
32767fef78bSLawrence Stewart */
3286907bbaeSRichard Scheffenegger if (((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) &&
329eb5bfdd0SRichard Scheffenegger cubic_data->W_max < CCV(ccv, snd_cwnd)) {
330eb5bfdd0SRichard Scheffenegger cubic_data->W_max = CCV(ccv, snd_cwnd);
331*6156da86SCheng Cui cubic_data->K = cubic_k(cubic_data->W_max / mss);
33267fef78bSLawrence Stewart }
33367fef78bSLawrence Stewart }
3342fda0a6fSRichard Scheffenegger } else if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
3352fda0a6fSRichard Scheffenegger !(ccv->flags & CCF_CWND_LIMITED)) {
3362fda0a6fSRichard Scheffenegger cubic_data->flags |= CUBICFLAG_IN_APPLIMIT;
33767fef78bSLawrence Stewart }
3387d87664aSMichael Tuexen }
33967fef78bSLawrence Stewart
34035cd141bSMichael Tuexen /*
341ea6d0de2SRichard Scheffenegger * This is a CUBIC specific implementation of after_idle.
34235cd141bSMichael Tuexen * - Reset cwnd by calling New Reno implementation of after_idle.
343eb5bfdd0SRichard Scheffenegger * - Reset t_epoch.
34435cd141bSMichael Tuexen */
34535cd141bSMichael Tuexen static void
cubic_after_idle(struct cc_var * ccv)34635cd141bSMichael Tuexen cubic_after_idle(struct cc_var *ccv)
34735cd141bSMichael Tuexen {
34835cd141bSMichael Tuexen struct cubic *cubic_data;
34935cd141bSMichael Tuexen
35035cd141bSMichael Tuexen cubic_data = ccv->cc_data;
35135cd141bSMichael Tuexen
352eb5bfdd0SRichard Scheffenegger cubic_data->W_max = ulmax(cubic_data->W_max, CCV(ccv, snd_cwnd));
35322dcc812SRichard Scheffenegger cubic_data->K = cubic_k(cubic_data->W_max / tcp_fixed_maxseg(ccv->tp));
354a9696510SRandall Stewart if ((cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) == 0) {
355a9696510SRandall Stewart /*
356a9696510SRandall Stewart * Re-enable hystart if we have been idle.
357a9696510SRandall Stewart */
358a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
359a9696510SRandall Stewart cubic_data->flags |= CUBICFLAG_HYSTART_ENABLED;
360a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubic_data, 12, CCV(ccv, snd_ssthresh));
361a9696510SRandall Stewart }
362b8d60729SRandall Stewart newreno_cc_after_idle(ccv);
363eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = ticks;
36435cd141bSMichael Tuexen }
36535cd141bSMichael Tuexen
36667fef78bSLawrence Stewart static void
cubic_cb_destroy(struct cc_var * ccv)36767fef78bSLawrence Stewart cubic_cb_destroy(struct cc_var *ccv)
36867fef78bSLawrence Stewart {
369b8d60729SRandall Stewart free(ccv->cc_data, M_CC_MEM);
370b8d60729SRandall Stewart }
371b8d60729SRandall Stewart
372b8d60729SRandall Stewart static size_t
cubic_data_sz(void)373b8d60729SRandall Stewart cubic_data_sz(void)
374b8d60729SRandall Stewart {
375b8d60729SRandall Stewart return (sizeof(struct cubic));
37667fef78bSLawrence Stewart }
37767fef78bSLawrence Stewart
37867fef78bSLawrence Stewart static int
cubic_cb_init(struct cc_var * ccv,void * ptr)379b8d60729SRandall Stewart cubic_cb_init(struct cc_var *ccv, void *ptr)
38067fef78bSLawrence Stewart {
38167fef78bSLawrence Stewart struct cubic *cubic_data;
38267fef78bSLawrence Stewart
38300d3b744SMichael Tuexen INP_WLOCK_ASSERT(tptoinpcb(ccv->tp));
384b8d60729SRandall Stewart if (ptr == NULL) {
385b8d60729SRandall Stewart cubic_data = malloc(sizeof(struct cubic), M_CC_MEM, M_NOWAIT|M_ZERO);
38667fef78bSLawrence Stewart if (cubic_data == NULL)
38767fef78bSLawrence Stewart return (ENOMEM);
388b8d60729SRandall Stewart } else
389b8d60729SRandall Stewart cubic_data = ptr;
39067fef78bSLawrence Stewart
39167fef78bSLawrence Stewart /* Init some key variables with sensible defaults. */
392eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = ticks;
393a3aa6f65SCheng Cui cubic_data->min_rtt_usecs = TCPTV_SRTTBASE;
394a3aa6f65SCheng Cui cubic_data->mean_rtt_usecs = 1;
39567fef78bSLawrence Stewart
39667fef78bSLawrence Stewart ccv->cc_data = cubic_data;
397a9696510SRandall Stewart cubic_data->flags = CUBICFLAG_HYSTART_ENABLED;
398a9696510SRandall Stewart /* At init set both to infinity */
399a9696510SRandall Stewart cubic_data->css_lastround_minrtt = 0xffffffff;
400a9696510SRandall Stewart cubic_data->css_current_round_minrtt = 0xffffffff;
401a9696510SRandall Stewart cubic_data->css_current_round = 0;
402a9696510SRandall Stewart cubic_data->css_baseline_minrtt = 0xffffffff;
403a9696510SRandall Stewart cubic_data->css_rttsample_count = 0;
404a9696510SRandall Stewart cubic_data->css_entered_at_round = 0;
405a9696510SRandall Stewart cubic_data->css_fas_at_css_entry = 0;
406a9696510SRandall Stewart cubic_data->css_lowrtt_fas = 0;
407a9696510SRandall Stewart cubic_data->css_last_fas = 0;
40867fef78bSLawrence Stewart
40967fef78bSLawrence Stewart return (0);
41067fef78bSLawrence Stewart }
41167fef78bSLawrence Stewart
41267fef78bSLawrence Stewart /*
41367fef78bSLawrence Stewart * Perform any necessary tasks before we enter congestion recovery.
41467fef78bSLawrence Stewart */
41567fef78bSLawrence Stewart static void
cubic_cong_signal(struct cc_var * ccv,ccsignal_t type)416f74352fbSRichard Scheffenegger cubic_cong_signal(struct cc_var *ccv, ccsignal_t type)
41767fef78bSLawrence Stewart {
41867fef78bSLawrence Stewart struct cubic *cubic_data;
41932a6df57SRichard Scheffenegger uint32_t mss, pipe;
42067fef78bSLawrence Stewart
42167fef78bSLawrence Stewart cubic_data = ccv->cc_data;
42200d3b744SMichael Tuexen mss = tcp_fixed_maxseg(ccv->tp);
42367fef78bSLawrence Stewart
42467fef78bSLawrence Stewart switch (type) {
42567fef78bSLawrence Stewart case CC_NDUPACK:
426a9696510SRandall Stewart if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) {
427a9696510SRandall Stewart /* Make sure the flags are all off we had a loss */
428a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
429a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
430a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubic_data, 10, CCV(ccv, snd_ssthresh));
431a9696510SRandall Stewart }
43267fef78bSLawrence Stewart if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
43367fef78bSLawrence Stewart if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
43437674273SRichard Scheffenegger cubic_ssthresh_update(ccv, mss);
4356907bbaeSRichard Scheffenegger cubic_data->flags |= CUBICFLAG_CONG_EVENT;
436eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = ticks;
437eb5bfdd0SRichard Scheffenegger cubic_data->K = cubic_k(cubic_data->W_max / mss);
43867fef78bSLawrence Stewart }
43967fef78bSLawrence Stewart ENTER_RECOVERY(CCV(ccv, t_flags));
44067fef78bSLawrence Stewart }
44167fef78bSLawrence Stewart break;
44267fef78bSLawrence Stewart
44367fef78bSLawrence Stewart case CC_ECN:
444a9696510SRandall Stewart if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) {
445a9696510SRandall Stewart /* Make sure the flags are all off we had a loss */
446a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
447a9696510SRandall Stewart cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
448a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubic_data, 9, CCV(ccv, snd_ssthresh));
449a9696510SRandall Stewart }
45067fef78bSLawrence Stewart if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
45137674273SRichard Scheffenegger cubic_ssthresh_update(ccv, mss);
4526907bbaeSRichard Scheffenegger cubic_data->flags |= CUBICFLAG_CONG_EVENT;
453eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = ticks;
454eb5bfdd0SRichard Scheffenegger cubic_data->K = cubic_k(cubic_data->W_max / mss);
45567fef78bSLawrence Stewart CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
45667fef78bSLawrence Stewart ENTER_CONGRECOVERY(CCV(ccv, t_flags));
45767fef78bSLawrence Stewart }
45867fef78bSLawrence Stewart break;
45967fef78bSLawrence Stewart
46067fef78bSLawrence Stewart case CC_RTO:
46137674273SRichard Scheffenegger /* RFC8312 Section 4.7 */
46237674273SRichard Scheffenegger if (CCV(ccv, t_rxtshift) == 1) {
463eb5bfdd0SRichard Scheffenegger /*
464eb5bfdd0SRichard Scheffenegger * Remember the state only for the first RTO event. This
465eb5bfdd0SRichard Scheffenegger * will help us restore the state to the values seen
466eb5bfdd0SRichard Scheffenegger * at the most recent congestion avoidance stage before
467eb5bfdd0SRichard Scheffenegger * the current RTO event.
468eb5bfdd0SRichard Scheffenegger */
469eb5bfdd0SRichard Scheffenegger cubic_data->undo_t_epoch = cubic_data->t_epoch;
470eb5bfdd0SRichard Scheffenegger cubic_data->undo_cwnd_epoch = cubic_data->cwnd_epoch;
471eb5bfdd0SRichard Scheffenegger cubic_data->undo_W_max = cubic_data->W_max;
472eb5bfdd0SRichard Scheffenegger cubic_data->undo_K = cubic_data->K;
47300d3b744SMichael Tuexen pipe = tcp_compute_pipe(ccv->tp);
47432a6df57SRichard Scheffenegger CCV(ccv, snd_ssthresh) = max(2,
47532a6df57SRichard Scheffenegger (((uint64_t)min(CCV(ccv, snd_wnd), pipe) *
47632a6df57SRichard Scheffenegger CUBIC_BETA) >> CUBIC_SHIFT) / mss) * mss;
4771edbb54fSEd Maste }
47837674273SRichard Scheffenegger cubic_data->flags |= CUBICFLAG_CONG_EVENT | CUBICFLAG_RTO_EVENT;
47937674273SRichard Scheffenegger CCV(ccv, snd_cwnd) = mss;
48037674273SRichard Scheffenegger break;
48137674273SRichard Scheffenegger
48237674273SRichard Scheffenegger case CC_RTO_ERR:
48337674273SRichard Scheffenegger cubic_data->flags &= ~(CUBICFLAG_CONG_EVENT | CUBICFLAG_RTO_EVENT);
484eb5bfdd0SRichard Scheffenegger cubic_data->K = cubic_data->undo_K;
485eb5bfdd0SRichard Scheffenegger cubic_data->W_max = cubic_data->undo_W_max;
486eb5bfdd0SRichard Scheffenegger cubic_data->cwnd_epoch = cubic_data->undo_cwnd_epoch;
487eb5bfdd0SRichard Scheffenegger cubic_data->t_epoch = cubic_data->undo_t_epoch;
48867fef78bSLawrence Stewart break;
489f74352fbSRichard Scheffenegger default:
490f74352fbSRichard Scheffenegger break;
49167fef78bSLawrence Stewart }
49267fef78bSLawrence Stewart }
49367fef78bSLawrence Stewart
49467fef78bSLawrence Stewart static void
cubic_conn_init(struct cc_var * ccv)49567fef78bSLawrence Stewart cubic_conn_init(struct cc_var *ccv)
49667fef78bSLawrence Stewart {
49767fef78bSLawrence Stewart struct cubic *cubic_data;
49867fef78bSLawrence Stewart
49967fef78bSLawrence Stewart cubic_data = ccv->cc_data;
50067fef78bSLawrence Stewart
50167fef78bSLawrence Stewart /*
502eb5bfdd0SRichard Scheffenegger * Ensure we have a sane initial value for W_max recorded. Without
50367fef78bSLawrence Stewart * this here bad things happen when entries from the TCP hostcache
50467fef78bSLawrence Stewart * get used.
50567fef78bSLawrence Stewart */
506eb5bfdd0SRichard Scheffenegger cubic_data->W_max = CCV(ccv, snd_cwnd);
50767fef78bSLawrence Stewart }
50867fef78bSLawrence Stewart
50967fef78bSLawrence Stewart static int
cubic_mod_init(void)51067fef78bSLawrence Stewart cubic_mod_init(void)
51167fef78bSLawrence Stewart {
51267fef78bSLawrence Stewart return (0);
51367fef78bSLawrence Stewart }
51467fef78bSLawrence Stewart
51567fef78bSLawrence Stewart /*
51667fef78bSLawrence Stewart * Perform any necessary tasks before we exit congestion recovery.
51767fef78bSLawrence Stewart */
51867fef78bSLawrence Stewart static void
cubic_post_recovery(struct cc_var * ccv)51967fef78bSLawrence Stewart cubic_post_recovery(struct cc_var *ccv)
52067fef78bSLawrence Stewart {
52167fef78bSLawrence Stewart struct cubic *cubic_data;
522f81bc34eSHiren Panchasara int pipe;
52322dcc812SRichard Scheffenegger uint32_t mss = tcp_fixed_maxseg(ccv->tp);
52467fef78bSLawrence Stewart
52567fef78bSLawrence Stewart cubic_data = ccv->cc_data;
526f81bc34eSHiren Panchasara pipe = 0;
52767fef78bSLawrence Stewart
52867fef78bSLawrence Stewart if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
52967fef78bSLawrence Stewart /*
53067fef78bSLawrence Stewart * If inflight data is less than ssthresh, set cwnd
53167fef78bSLawrence Stewart * conservatively to avoid a burst of data, as suggested in
53267fef78bSLawrence Stewart * the NewReno RFC. Otherwise, use the CUBIC method.
53367fef78bSLawrence Stewart */
53400d3b744SMichael Tuexen pipe = tcp_compute_pipe(ccv->tp);
535f81bc34eSHiren Panchasara if (pipe < CCV(ccv, snd_ssthresh))
5365cc11a89SMichael Tuexen /*
5375cc11a89SMichael Tuexen * Ensure that cwnd does not collapse to 1 MSS under
5385cc11a89SMichael Tuexen * adverse conditions. Implements RFC6582
5395cc11a89SMichael Tuexen */
54022dcc812SRichard Scheffenegger CCV(ccv, snd_cwnd) = max(pipe, mss) + mss;
54167fef78bSLawrence Stewart else
542eb5bfdd0SRichard Scheffenegger /* Update cwnd based on beta and adjusted W_max. */
543eb5bfdd0SRichard Scheffenegger CCV(ccv, snd_cwnd) = max(((uint64_t)cubic_data->W_max *
54414558b99SRichard Scheffenegger CUBIC_BETA) >> CUBIC_SHIFT,
54522dcc812SRichard Scheffenegger 2 * mss);
54667fef78bSLawrence Stewart }
54767fef78bSLawrence Stewart
54867fef78bSLawrence Stewart /* Calculate the average RTT between congestion epochs. */
54947f44cddSLawrence Stewart if (cubic_data->epoch_ack_count > 0 &&
550a3aa6f65SCheng Cui cubic_data->sum_rtt_usecs >= cubic_data->epoch_ack_count) {
551a3aa6f65SCheng Cui cubic_data->mean_rtt_usecs = (int)(cubic_data->sum_rtt_usecs /
55267fef78bSLawrence Stewart cubic_data->epoch_ack_count);
55347f44cddSLawrence Stewart }
55467fef78bSLawrence Stewart
55567fef78bSLawrence Stewart cubic_data->epoch_ack_count = 0;
556a3aa6f65SCheng Cui cubic_data->sum_rtt_usecs = 0;
55767fef78bSLawrence Stewart }
55867fef78bSLawrence Stewart
55967fef78bSLawrence Stewart /*
56067fef78bSLawrence Stewart * Record the min RTT and sum samples for the epoch average RTT calculation.
56167fef78bSLawrence Stewart */
56267fef78bSLawrence Stewart static void
cubic_record_rtt(struct cc_var * ccv)56367fef78bSLawrence Stewart cubic_record_rtt(struct cc_var *ccv)
56467fef78bSLawrence Stewart {
56567fef78bSLawrence Stewart struct cubic *cubic_data;
566a3aa6f65SCheng Cui uint32_t t_srtt_usecs;
56767fef78bSLawrence Stewart
56867fef78bSLawrence Stewart /* Ignore srtt until a min number of samples have been taken. */
56967fef78bSLawrence Stewart if (CCV(ccv, t_rttupdated) >= CUBIC_MIN_RTT_SAMPLES) {
57067fef78bSLawrence Stewart cubic_data = ccv->cc_data;
57100d3b744SMichael Tuexen t_srtt_usecs = tcp_get_srtt(ccv->tp,
572a3aa6f65SCheng Cui TCP_TMR_GRANULARITY_USEC);
57367fef78bSLawrence Stewart /*
57467fef78bSLawrence Stewart * Record the current SRTT as our minrtt if it's the smallest
57567fef78bSLawrence Stewart * we've seen or minrtt is currently equal to its initialised
57667fef78bSLawrence Stewart * value.
57767fef78bSLawrence Stewart *
57867fef78bSLawrence Stewart * XXXLAS: Should there be some hysteresis for minrtt?
57967fef78bSLawrence Stewart */
580a3aa6f65SCheng Cui if ((t_srtt_usecs < cubic_data->min_rtt_usecs ||
581a3aa6f65SCheng Cui cubic_data->min_rtt_usecs == TCPTV_SRTTBASE)) {
582a3aa6f65SCheng Cui /* A minimal rtt is a single unshifted tick of a ticks
583a3aa6f65SCheng Cui * timer. */
584a3aa6f65SCheng Cui cubic_data->min_rtt_usecs = max(tick >> TCP_RTT_SHIFT,
585a3aa6f65SCheng Cui t_srtt_usecs);
58667fef78bSLawrence Stewart
58747f44cddSLawrence Stewart /*
58847f44cddSLawrence Stewart * If the connection is within its first congestion
589a3aa6f65SCheng Cui * epoch, ensure we prime mean_rtt_usecs with a
59047f44cddSLawrence Stewart * reasonable value until the epoch average RTT is
59147f44cddSLawrence Stewart * calculated in cubic_post_recovery().
59247f44cddSLawrence Stewart */
593a3aa6f65SCheng Cui if (cubic_data->min_rtt_usecs >
594a3aa6f65SCheng Cui cubic_data->mean_rtt_usecs)
595a3aa6f65SCheng Cui cubic_data->mean_rtt_usecs =
596a3aa6f65SCheng Cui cubic_data->min_rtt_usecs;
59747f44cddSLawrence Stewart }
59847f44cddSLawrence Stewart
59967fef78bSLawrence Stewart /* Sum samples for epoch average RTT calculation. */
600a3aa6f65SCheng Cui cubic_data->sum_rtt_usecs += t_srtt_usecs;
60167fef78bSLawrence Stewart cubic_data->epoch_ack_count++;
60267fef78bSLawrence Stewart }
60367fef78bSLawrence Stewart }
60467fef78bSLawrence Stewart
60567fef78bSLawrence Stewart /*
60667fef78bSLawrence Stewart * Update the ssthresh in the event of congestion.
60767fef78bSLawrence Stewart */
60867fef78bSLawrence Stewart static void
cubic_ssthresh_update(struct cc_var * ccv,uint32_t maxseg)60937674273SRichard Scheffenegger cubic_ssthresh_update(struct cc_var *ccv, uint32_t maxseg)
61067fef78bSLawrence Stewart {
61167fef78bSLawrence Stewart struct cubic *cubic_data;
61214558b99SRichard Scheffenegger uint32_t ssthresh;
613a459638fSRichard Scheffenegger uint32_t cwnd;
61467fef78bSLawrence Stewart
61567fef78bSLawrence Stewart cubic_data = ccv->cc_data;
616a459638fSRichard Scheffenegger cwnd = CCV(ccv, snd_cwnd);
617a459638fSRichard Scheffenegger
618a459638fSRichard Scheffenegger /* Fast convergence heuristic. */
619eb5bfdd0SRichard Scheffenegger if (cwnd < cubic_data->W_max) {
620a459638fSRichard Scheffenegger cwnd = ((uint64_t)cwnd * CUBIC_FC_FACTOR) >> CUBIC_SHIFT;
621a459638fSRichard Scheffenegger }
622eb5bfdd0SRichard Scheffenegger cubic_data->undo_W_max = cubic_data->W_max;
623eb5bfdd0SRichard Scheffenegger cubic_data->W_max = cwnd;
62467fef78bSLawrence Stewart
625ee450610SCheng Cui if (cubic_data->flags & CUBICFLAG_IN_TF) {
626ee450610SCheng Cui /* If in the TCP friendly region, follow what newreno does */
627ee450610SCheng Cui ssthresh = newreno_cc_cwnd_on_multiplicative_decrease(ccv, maxseg);
628ee450610SCheng Cui
629ee450610SCheng Cui } else if ((cubic_data->flags & CUBICFLAG_CONG_EVENT) == 0) {
63067fef78bSLawrence Stewart /*
631a459638fSRichard Scheffenegger * On the first congestion event, set ssthresh to cwnd * 0.5
632ee450610SCheng Cui * and reduce W_max to cwnd * beta. This aligns the cubic
633ee450610SCheng Cui * concave region appropriately.
63467fef78bSLawrence Stewart */
635a459638fSRichard Scheffenegger ssthresh = cwnd >> 1;
636ee450610SCheng Cui cubic_data->W_max = ((uint64_t)cwnd * CUBIC_BETA) >> CUBIC_SHIFT;
637a459638fSRichard Scheffenegger } else {
638ee450610SCheng Cui /*
639ee450610SCheng Cui * On subsequent congestion events, set ssthresh to cwnd * beta.
640ee450610SCheng Cui */
641ee450610SCheng Cui ssthresh = ((uint64_t)cwnd * CUBIC_BETA) >> CUBIC_SHIFT;
642a459638fSRichard Scheffenegger }
64337674273SRichard Scheffenegger CCV(ccv, snd_ssthresh) = max(ssthresh, 2 * maxseg);
64467fef78bSLawrence Stewart }
64567fef78bSLawrence Stewart
646a9696510SRandall Stewart static void
cubic_rttsample(struct cc_var * ccv,uint32_t usec_rtt,uint32_t rxtcnt,uint32_t fas)647a9696510SRandall Stewart cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt, uint32_t rxtcnt, uint32_t fas)
648a9696510SRandall Stewart {
649a9696510SRandall Stewart struct cubic *cubicd;
650a9696510SRandall Stewart
651a9696510SRandall Stewart cubicd = ccv->cc_data;
652a9696510SRandall Stewart if (rxtcnt > 1) {
653a9696510SRandall Stewart /*
654a9696510SRandall Stewart * Only look at RTT's that are non-ambiguous.
655a9696510SRandall Stewart */
656a9696510SRandall Stewart return;
657a9696510SRandall Stewart }
658a9696510SRandall Stewart cubicd->css_rttsample_count++;
659a9696510SRandall Stewart cubicd->css_last_fas = fas;
660a9696510SRandall Stewart if (cubicd->css_current_round_minrtt > usec_rtt) {
661a9696510SRandall Stewart cubicd->css_current_round_minrtt = usec_rtt;
662a9696510SRandall Stewart cubicd->css_lowrtt_fas = cubicd->css_last_fas;
663a9696510SRandall Stewart }
664a9696510SRandall Stewart if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) &&
665a9696510SRandall Stewart (cubicd->css_current_round_minrtt != 0xffffffff) &&
666e88412d8SRandall Stewart (cubicd->css_current_round_minrtt < cubicd->css_baseline_minrtt) &&
667a9696510SRandall Stewart (cubicd->css_lastround_minrtt != 0xffffffff)) {
668a9696510SRandall Stewart /*
669a9696510SRandall Stewart * We were in CSS and the RTT is now less, we
670a9696510SRandall Stewart * entered CSS erroneously.
671a9696510SRandall Stewart */
672a9696510SRandall Stewart cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
673a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 8, cubicd->css_baseline_minrtt);
674a9696510SRandall Stewart cubicd->css_baseline_minrtt = 0xffffffff;
675a9696510SRandall Stewart }
676a9696510SRandall Stewart if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED)
677a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 5, usec_rtt);
678a9696510SRandall Stewart }
679a9696510SRandall Stewart
680a9696510SRandall Stewart static void
cubic_newround(struct cc_var * ccv,uint32_t round_cnt)681a9696510SRandall Stewart cubic_newround(struct cc_var *ccv, uint32_t round_cnt)
682a9696510SRandall Stewart {
683a9696510SRandall Stewart struct cubic *cubicd;
684a9696510SRandall Stewart
685a9696510SRandall Stewart cubicd = ccv->cc_data;
686a9696510SRandall Stewart /* We have entered a new round */
687a9696510SRandall Stewart cubicd->css_lastround_minrtt = cubicd->css_current_round_minrtt;
688a9696510SRandall Stewart cubicd->css_current_round_minrtt = 0xffffffff;
689a9696510SRandall Stewart cubicd->css_rttsample_count = 0;
690a9696510SRandall Stewart cubicd->css_current_round = round_cnt;
691a9696510SRandall Stewart if ((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) &&
692a9696510SRandall Stewart ((round_cnt - cubicd->css_entered_at_round) >= hystart_css_rounds)) {
693a9696510SRandall Stewart /* Enter CA */
694a9696510SRandall Stewart if (ccv->flags & CCF_HYSTART_CAN_SH_CWND) {
695a9696510SRandall Stewart /*
696a9696510SRandall Stewart * We engage more than snd_ssthresh, engage
697a9696510SRandall Stewart * the brakes!! Though we will stay in SS to
698a9696510SRandall Stewart * creep back up again, so lets leave CSS active
699a9696510SRandall Stewart * and give us hystart_css_rounds more rounds.
700a9696510SRandall Stewart */
701a9696510SRandall Stewart if (ccv->flags & CCF_HYSTART_CONS_SSTH) {
702a9696510SRandall Stewart CCV(ccv, snd_ssthresh) = ((cubicd->css_lowrtt_fas + cubicd->css_fas_at_css_entry) / 2);
703a9696510SRandall Stewart } else {
704a9696510SRandall Stewart CCV(ccv, snd_ssthresh) = cubicd->css_lowrtt_fas;
705a9696510SRandall Stewart }
706a9696510SRandall Stewart CCV(ccv, snd_cwnd) = cubicd->css_fas_at_css_entry;
707a9696510SRandall Stewart cubicd->css_entered_at_round = round_cnt;
708a9696510SRandall Stewart } else {
709a9696510SRandall Stewart CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
710a9696510SRandall Stewart /* Turn off the CSS flag */
711a9696510SRandall Stewart cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
712a9696510SRandall Stewart /* Disable use of CSS in the future except long idle */
713a9696510SRandall Stewart cubicd->flags &= ~CUBICFLAG_HYSTART_ENABLED;
714a9696510SRandall Stewart }
715a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 6, CCV(ccv, snd_ssthresh));
716a9696510SRandall Stewart }
717a9696510SRandall Stewart if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED)
718a9696510SRandall Stewart cubic_log_hystart_event(ccv, cubicd, 4, round_cnt);
719a9696510SRandall Stewart }
720a9696510SRandall Stewart
72167fef78bSLawrence Stewart DECLARE_CC_MODULE(cubic, &cubic_cc_algo);
722b8d60729SRandall Stewart MODULE_VERSION(cubic, 2);
723