#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/limits.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <net/vnet.h>
#include <net/route.h>
#include <net/route/nhop.h>
#include <netinet/in_pcb.h>
#include <netinet/tcp.h>
#include <netinet/tcp_seq.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>
#include <netinet/tcp_log_buf.h>
#include <netinet/tcp_hpts.h>
#include <netinet/cc/cc.h>
#include <netinet/cc/cc_cubic.h>
#include <netinet/cc/cc_module.h>
static void cubic_ack_received(struct cc_var *ccv, ccsignal_t type);
static void cubic_cb_destroy(struct cc_var *ccv);
static int cubic_cb_init(struct cc_var *ccv, void *ptr);
static void cubic_cong_signal(struct cc_var *ccv, ccsignal_t type);
static void cubic_conn_init(struct cc_var *ccv);
static int cubic_mod_init(void);
static void cubic_post_recovery(struct cc_var *ccv);
static void cubic_record_rtt(struct cc_var *ccv);
static uint32_t cubic_get_ssthresh(struct cc_var *ccv, uint32_t maxseg);
static void cubic_after_idle(struct cc_var *ccv);
static size_t cubic_data_sz(void);
static void cubic_newround(struct cc_var *ccv, uint32_t round_cnt);
static void cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt,
uint32_t rxtcnt, uint32_t fas);
struct cc_algo cubic_cc_algo = {
.name = "cubic",
.ack_received = cubic_ack_received,
.cb_destroy = cubic_cb_destroy,
.cb_init = cubic_cb_init,
.cong_signal = cubic_cong_signal,
.conn_init = cubic_conn_init,
.mod_init = cubic_mod_init,
.post_recovery = cubic_post_recovery,
.after_idle = cubic_after_idle,
.cc_data_sz = cubic_data_sz,
.rttsample = cubic_rttsample,
.newround = cubic_newround
};
static void
cubic_log_hystart_event(struct cc_var *ccv, struct cubic *cubicd, uint8_t mod, uint32_t flex1)
{
struct tcpcb *tp;
if (hystart_bblogs == 0)
return;
tp = ccv->tp;
if (tcp_bblogging_on(tp)) {
union tcp_log_stackspecific log;
struct timeval tv;
memset(&log, 0, sizeof(log));
log.u_bbr.flex1 = flex1;
log.u_bbr.flex2 = cubicd->css_current_round_minrtt;
log.u_bbr.flex3 = cubicd->css_lastround_minrtt;
log.u_bbr.flex4 = cubicd->css_rttsample_count;
log.u_bbr.flex5 = cubicd->css_entered_at_round;
log.u_bbr.flex6 = cubicd->css_baseline_minrtt;
log.u_bbr.flex7 = cubicd->flags & 0x0000ffff;
log.u_bbr.flex8 = mod;
log.u_bbr.epoch = cubicd->css_current_round;
log.u_bbr.timeStamp = tcp_get_usecs(&tv);
log.u_bbr.lt_epoch = cubicd->css_fas_at_css_entry;
log.u_bbr.pkts_out = cubicd->css_last_fas;
log.u_bbr.delivered = cubicd->css_lowrtt_fas;
log.u_bbr.pkt_epoch = ccv->flags;
TCP_LOG_EVENTP(tp, NULL,
&tptosocket(tp)->so_rcv,
&tptosocket(tp)->so_snd,
TCP_HYSTART, 0,
0, &log, false, &tv);
}
}
static void
cubic_does_slow_start(struct cc_var *ccv, struct cubic *cubicd)
{
u_int cw = CCV(ccv, snd_cwnd);
uint32_t mss = tcp_fixed_maxseg(ccv->tp);
u_int incr = mss;
uint16_t abc_val;
cubicd->flags |= CUBICFLAG_IN_SLOWSTART;
if (ccv->flags & CCF_USE_LOCAL_ABC)
abc_val = ccv->labc;
else
abc_val = V_tcp_abc_l_var;
if ((ccv->flags & CCF_HYSTART_ALLOWED) &&
(cubicd->flags & CUBICFLAG_HYSTART_ENABLED) &&
((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) == 0)) {
if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) &&
(cubicd->css_current_round_minrtt != 0xffffffff) &&
(cubicd->css_lastround_minrtt != 0xffffffff)) {
uint32_t rtt_thresh;
rtt_thresh = (cubicd->css_lastround_minrtt >> 3);
if (rtt_thresh < hystart_minrtt_thresh)
rtt_thresh = hystart_minrtt_thresh;
if (rtt_thresh > hystart_maxrtt_thresh)
rtt_thresh = hystart_maxrtt_thresh;
cubic_log_hystart_event(ccv, cubicd, 1, rtt_thresh);
if (cubicd->css_current_round_minrtt >= (cubicd->css_lastround_minrtt + rtt_thresh)) {
cubicd->flags |= CUBICFLAG_HYSTART_IN_CSS;
cubicd->css_fas_at_css_entry = cubicd->css_lowrtt_fas;
cubicd->css_baseline_minrtt = cubicd->css_current_round_minrtt;
cubicd->css_entered_at_round = cubicd->css_current_round;
cubic_log_hystart_event(ccv, cubicd, 2, rtt_thresh);
}
}
}
if (CCV(ccv, snd_nxt) == CCV(ccv, snd_max))
incr = min(ccv->bytes_this_ack,
ccv->nsegs * abc_val * mss);
else
incr = min(ccv->bytes_this_ack, mss);
if (cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) {
incr /= hystart_css_growth_div;
cubic_log_hystart_event(ccv, cubicd, 3, incr);
}
if (incr > 0)
CCV(ccv, snd_cwnd) = min((cw + incr),
TCP_MAXWIN << CCV(ccv, snd_scale));
}
static void
cubic_ack_received(struct cc_var *ccv, ccsignal_t type)
{
struct cubic *cubic_data;
uint32_t W_est, W_cubic, cwin, target, incr;
int usecs_since_epoch;
uint32_t mss = tcp_fixed_maxseg(ccv->tp);
cwin = CCV(ccv, snd_cwnd);
cubic_data = ccv->cc_data;
cubic_record_rtt(ccv);
if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
(ccv->flags & CCF_CWND_LIMITED)) {
if (cwin <= CCV(ccv, snd_ssthresh) ||
cubic_data->min_rtt_usecs == TCPTV_SRTTBASE) {
cubic_does_slow_start(ccv, cubic_data);
} else {
if (cubic_data->flags & CUBICFLAG_HYSTART_IN_CSS) {
cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
cubic_log_hystart_event(ccv, cubic_data, 11, CCV(ccv, snd_ssthresh));
}
if (cubic_data->flags & (CUBICFLAG_IN_SLOWSTART |
CUBICFLAG_CONG_EVENT |
CUBICFLAG_IN_APPLIMIT)) {
cubic_data->t_epoch = ticks;
cubic_data->cwnd_epoch = cwin;
cubic_data->K = cubic_k(cubic_data->W_max / mss,
cubic_data->cwnd_epoch / mss);
cubic_data->flags &= ~(CUBICFLAG_IN_SLOWSTART |
CUBICFLAG_CONG_EVENT |
CUBICFLAG_IN_APPLIMIT);
if (cubic_data->flags & CUBICFLAG_RTO_EVENT) {
cubic_data->flags &= ~CUBICFLAG_RTO_EVENT;
cubic_data->W_max = cwin;
cubic_data->K = 0;
}
}
usecs_since_epoch = (ticks - cubic_data->t_epoch) * tick;
if (usecs_since_epoch < 0) {
usecs_since_epoch = INT_MAX;
cubic_data->t_epoch = ticks - INT_MAX;
}
W_est = tf_cwnd(ccv);
W_cubic = cubic_cwnd(usecs_since_epoch +
cubic_data->mean_rtt_usecs,
cubic_data->W_max,
mss,
cubic_data->K);
if (W_cubic < W_est) {
CCV(ccv, snd_cwnd) = W_est;
cubic_data->flags |= CUBICFLAG_IN_TF;
} else {
if (W_cubic < cwin) {
target = cwin;
} else if (W_cubic > ((cwin * 3) >> 1)) {
target = (cwin * 3) >> 1;
} else {
target = W_cubic;
}
incr = (((target - cwin) << CUBIC_SHIFT) /
cwin * mss) >> CUBIC_SHIFT;
CCV(ccv, snd_cwnd) = cwin + incr;
}
}
} else if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
!(ccv->flags & CCF_CWND_LIMITED)) {
cubic_data->flags |= CUBICFLAG_IN_APPLIMIT;
}
}
static void
cubic_after_idle(struct cc_var *ccv)
{
struct cubic *cubic_data = ccv->cc_data;
uint32_t mss = tcp_fixed_maxseg(ccv->tp);
cubic_data->W_max = ulmax(cubic_data->W_max, CCV(ccv, snd_cwnd));
cubic_data->K = cubic_k(cubic_data->W_max / mss, cubic_data->cwnd_epoch / mss);
if ((cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) == 0) {
cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
cubic_data->flags |= CUBICFLAG_HYSTART_ENABLED;
cubic_log_hystart_event(ccv, cubic_data, 12, CCV(ccv, snd_ssthresh));
}
newreno_cc_after_idle(ccv);
cubic_data->t_epoch = ticks;
}
static void
cubic_cb_destroy(struct cc_var *ccv)
{
free(ccv->cc_data, M_CC_MEM);
}
static size_t
cubic_data_sz(void)
{
return (sizeof(struct cubic));
}
static int
cubic_cb_init(struct cc_var *ccv, void *ptr)
{
struct cubic *cubic_data;
INP_WLOCK_ASSERT(tptoinpcb(ccv->tp));
if (ptr == NULL) {
cubic_data = malloc(sizeof(struct cubic), M_CC_MEM, M_NOWAIT|M_ZERO);
if (cubic_data == NULL)
return (ENOMEM);
} else
cubic_data = ptr;
cubic_data->t_epoch = 0;
cubic_data->cwnd_epoch = 0;
cubic_data->K = 0;
cubic_data->min_rtt_usecs = TCPTV_SRTTBASE;
cubic_data->mean_rtt_usecs = 1;
ccv->cc_data = cubic_data;
cubic_data->flags = CUBICFLAG_HYSTART_ENABLED;
cubic_data->css_lastround_minrtt = 0xffffffff;
cubic_data->css_current_round_minrtt = 0xffffffff;
cubic_data->css_current_round = 0;
cubic_data->css_baseline_minrtt = 0xffffffff;
cubic_data->css_rttsample_count = 0;
cubic_data->css_entered_at_round = 0;
cubic_data->css_fas_at_css_entry = 0;
cubic_data->css_lowrtt_fas = 0;
cubic_data->css_last_fas = 0;
return (0);
}
static void
cubic_cong_signal(struct cc_var *ccv, ccsignal_t type)
{
struct cubic *cubic_data;
uint32_t mss, pipe, ssthresh;
cubic_data = ccv->cc_data;
mss = tcp_fixed_maxseg(ccv->tp);
switch (type) {
case CC_NDUPACK:
if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) {
cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
cubic_log_hystart_event(ccv, cubic_data, 10, CCV(ccv, snd_ssthresh));
}
if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
ssthresh = cubic_get_ssthresh(ccv, mss);
CCV(ccv, snd_ssthresh) = max(ssthresh, 2 * mss);
cubic_data->flags |= CUBICFLAG_CONG_EVENT;
}
ENTER_RECOVERY(CCV(ccv, t_flags));
}
break;
case CC_ECN:
if (cubic_data->flags & CUBICFLAG_HYSTART_ENABLED) {
cubic_data->flags &= ~CUBICFLAG_HYSTART_ENABLED;
cubic_data->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
cubic_log_hystart_event(ccv, cubic_data, 9, CCV(ccv, snd_ssthresh));
}
if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
ssthresh = cubic_get_ssthresh(ccv, mss);
CCV(ccv, snd_ssthresh) = max(ssthresh, 2 * mss);
CCV(ccv, snd_cwnd) = max(ssthresh, mss);
cubic_data->flags |= CUBICFLAG_CONG_EVENT;
ENTER_CONGRECOVERY(CCV(ccv, t_flags));
}
break;
case CC_RTO:
if (CCV(ccv, t_rxtshift) == 1) {
cubic_data->undo_t_epoch = cubic_data->t_epoch;
cubic_data->undo_cwnd_epoch = cubic_data->cwnd_epoch;
cubic_data->undo_W_max = cubic_data->W_max;
cubic_data->undo_K = cubic_data->K;
pipe = tcp_compute_pipe(ccv->tp);
CCV(ccv, snd_ssthresh) = max(2,
(((uint64_t)min(CCV(ccv, snd_wnd), pipe) *
CUBIC_BETA) >> CUBIC_SHIFT) / mss) * mss;
}
cubic_data->flags |= CUBICFLAG_RTO_EVENT;
CCV(ccv, snd_cwnd) = mss;
break;
case CC_RTO_ERR:
cubic_data->flags &= ~CUBICFLAG_RTO_EVENT;
cubic_data->K = cubic_data->undo_K;
cubic_data->W_max = cubic_data->undo_W_max;
cubic_data->cwnd_epoch = cubic_data->undo_cwnd_epoch;
cubic_data->t_epoch = cubic_data->undo_t_epoch;
break;
default:
break;
}
}
static void
cubic_conn_init(struct cc_var *ccv)
{
struct cubic *cubic_data;
cubic_data = ccv->cc_data;
cubic_data->W_max = UINT_MAX;
}
static int
cubic_mod_init(void)
{
return (0);
}
static void
cubic_post_recovery(struct cc_var *ccv)
{
struct cubic *cubic_data;
int pipe;
uint32_t mss = tcp_fixed_maxseg(ccv->tp);
cubic_data = ccv->cc_data;
pipe = 0;
if (IN_FASTRECOVERY(CCV(ccv, t_flags))) {
pipe = tcp_compute_pipe(ccv->tp);
if (pipe < CCV(ccv, snd_ssthresh))
CCV(ccv, snd_cwnd) = max(pipe, mss) + mss;
else
CCV(ccv, snd_cwnd) = max(((uint64_t)cubic_data->W_max *
CUBIC_BETA) >> CUBIC_SHIFT,
2 * mss);
}
if (cubic_data->epoch_ack_count > 0 &&
cubic_data->sum_rtt_usecs >= cubic_data->epoch_ack_count) {
cubic_data->mean_rtt_usecs = (int)(cubic_data->sum_rtt_usecs /
cubic_data->epoch_ack_count);
}
cubic_data->epoch_ack_count = 0;
cubic_data->sum_rtt_usecs = 0;
}
static void
cubic_record_rtt(struct cc_var *ccv)
{
struct cubic *cubic_data;
uint32_t t_srtt_usecs;
if (CCV(ccv, t_rttupdated) >= CUBIC_MIN_RTT_SAMPLES) {
cubic_data = ccv->cc_data;
t_srtt_usecs = tcp_get_srtt(ccv->tp,
TCP_TMR_GRANULARITY_USEC);
if ((t_srtt_usecs < cubic_data->min_rtt_usecs ||
cubic_data->min_rtt_usecs == TCPTV_SRTTBASE)) {
cubic_data->min_rtt_usecs = max(tick >> TCP_RTT_SHIFT,
t_srtt_usecs);
if (cubic_data->min_rtt_usecs >
cubic_data->mean_rtt_usecs)
cubic_data->mean_rtt_usecs =
cubic_data->min_rtt_usecs;
}
cubic_data->sum_rtt_usecs += t_srtt_usecs;
cubic_data->epoch_ack_count++;
}
}
static uint32_t
cubic_get_ssthresh(struct cc_var *ccv, uint32_t maxseg)
{
struct cubic *cubic_data;
uint32_t cwnd, pipe;
cubic_data = ccv->cc_data;
cwnd = CCV(ccv, snd_cwnd);
if (cwnd < cubic_data->W_max) {
cwnd = ((uint64_t)cwnd * CUBIC_FC_FACTOR) >> CUBIC_SHIFT;
}
cubic_data->W_max = cwnd;
if (cubic_data->flags & CUBICFLAG_IN_TF) {
return (newreno_cc_cwnd_on_multiplicative_decrease(ccv, maxseg));
} else {
pipe = tcp_compute_pipe(ccv->tp);
return ((pipe * CUBIC_BETA) >> CUBIC_SHIFT);
}
}
static void
cubic_rttsample(struct cc_var *ccv, uint32_t usec_rtt, uint32_t rxtcnt, uint32_t fas)
{
struct cubic *cubicd;
cubicd = ccv->cc_data;
if (rxtcnt > 1) {
return;
}
cubicd->css_rttsample_count++;
cubicd->css_last_fas = fas;
if (cubicd->css_current_round_minrtt > usec_rtt) {
cubicd->css_current_round_minrtt = usec_rtt;
cubicd->css_lowrtt_fas = cubicd->css_last_fas;
}
if ((cubicd->css_rttsample_count >= hystart_n_rttsamples) &&
(cubicd->css_current_round_minrtt != 0xffffffff) &&
(cubicd->css_current_round_minrtt < cubicd->css_baseline_minrtt) &&
(cubicd->css_lastround_minrtt != 0xffffffff)) {
cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
cubic_log_hystart_event(ccv, cubicd, 8, cubicd->css_baseline_minrtt);
cubicd->css_baseline_minrtt = 0xffffffff;
}
if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED)
cubic_log_hystart_event(ccv, cubicd, 5, usec_rtt);
}
static void
cubic_newround(struct cc_var *ccv, uint32_t round_cnt)
{
struct cubic *cubicd;
cubicd = ccv->cc_data;
cubicd->css_lastround_minrtt = cubicd->css_current_round_minrtt;
cubicd->css_current_round_minrtt = 0xffffffff;
cubicd->css_rttsample_count = 0;
cubicd->css_current_round = round_cnt;
if ((cubicd->flags & CUBICFLAG_HYSTART_IN_CSS) &&
((round_cnt - cubicd->css_entered_at_round) >= hystart_css_rounds)) {
if (ccv->flags & CCF_HYSTART_CAN_SH_CWND) {
if (ccv->flags & CCF_HYSTART_CONS_SSTH) {
CCV(ccv, snd_ssthresh) = ((cubicd->css_lowrtt_fas + cubicd->css_fas_at_css_entry) / 2);
} else {
CCV(ccv, snd_ssthresh) = cubicd->css_lowrtt_fas;
}
CCV(ccv, snd_cwnd) = cubicd->css_fas_at_css_entry;
cubicd->css_entered_at_round = round_cnt;
} else {
CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
cubicd->flags &= ~CUBICFLAG_HYSTART_IN_CSS;
cubicd->flags &= ~CUBICFLAG_HYSTART_ENABLED;
}
cubic_log_hystart_event(ccv, cubicd, 6, CCV(ccv, snd_ssthresh));
}
if (cubicd->flags & CUBICFLAG_HYSTART_ENABLED)
cubic_log_hystart_event(ccv, cubicd, 4, round_cnt);
}
DECLARE_CC_MODULE(cubic, &cubic_cc_algo);
MODULE_VERSION(cubic, 2);