#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/socket.h>
#include <sys/lock.h>
#include <sys/mutex.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/in.h>
#include <netinet/in_pcb.h>
#include <netinet/tcp.h>
#include <netinet/tcp_seq.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_module.h>
#include <netinet/cc/cc_newreno.h>
static void newreno_cb_destroy(struct cc_var *ccv);
static void newreno_ack_received(struct cc_var *ccv, ccsignal_t type);
static void newreno_after_idle(struct cc_var *ccv);
static void newreno_cong_signal(struct cc_var *ccv, ccsignal_t type);
static int newreno_ctl_output(struct cc_var *ccv, struct sockopt *sopt, void *buf);
static void newreno_newround(struct cc_var *ccv, uint32_t round_cnt);
static void newreno_rttsample(struct cc_var *ccv, uint32_t usec_rtt, uint32_t rxtcnt, uint32_t fas);
static int newreno_cb_init(struct cc_var *ccv, void *);
static size_t newreno_data_sz(void);
VNET_DECLARE(uint32_t, newreno_beta);
#define V_newreno_beta VNET(newreno_beta)
VNET_DECLARE(uint32_t, newreno_beta_ecn);
#define V_newreno_beta_ecn VNET(newreno_beta_ecn)
struct cc_algo newreno_cc_algo = {
.name = "newreno",
.cb_destroy = newreno_cb_destroy,
.ack_received = newreno_ack_received,
.after_idle = newreno_after_idle,
.cong_signal = newreno_cong_signal,
.post_recovery = newreno_cc_post_recovery,
.ctl_output = newreno_ctl_output,
.newround = newreno_newround,
.rttsample = newreno_rttsample,
.cb_init = newreno_cb_init,
.cc_data_sz = newreno_data_sz,
};
static void
newreno_log_hystart_event(struct cc_var *ccv, struct newreno *nreno, 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 = nreno->css_current_round_minrtt;
log.u_bbr.flex3 = nreno->css_lastround_minrtt;
log.u_bbr.flex4 = nreno->css_rttsample_count;
log.u_bbr.flex5 = nreno->css_entered_at_round;
log.u_bbr.flex6 = nreno->css_baseline_minrtt;
log.u_bbr.flex7 = nreno->newreno_flags & 0x0000ffff;
log.u_bbr.flex8 = mod;
log.u_bbr.epoch = nreno->css_current_round;
log.u_bbr.timeStamp = tcp_get_usecs(&tv);
log.u_bbr.lt_epoch = nreno->css_fas_at_css_entry;
log.u_bbr.pkts_out = nreno->css_last_fas;
log.u_bbr.delivered = nreno->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 size_t
newreno_data_sz(void)
{
return (sizeof(struct newreno));
}
static int
newreno_cb_init(struct cc_var *ccv, void *ptr)
{
struct newreno *nreno;
INP_WLOCK_ASSERT(tptoinpcb(ccv->tp));
if (ptr == NULL) {
ccv->cc_data = malloc(sizeof(struct newreno), M_CC_MEM, M_NOWAIT);
if (ccv->cc_data == NULL)
return (ENOMEM);
} else
ccv->cc_data = ptr;
nreno = (struct newreno *)ccv->cc_data;
nreno->beta = V_newreno_beta;
nreno->beta_ecn = V_newreno_beta_ecn;
nreno->newreno_flags = CC_NEWRENO_HYSTART_ENABLED;
nreno->css_lastround_minrtt = 0xffffffff;
nreno->css_current_round_minrtt = 0xffffffff;
nreno->css_current_round = 0;
nreno->css_baseline_minrtt = 0xffffffff;
nreno->css_rttsample_count = 0;
nreno->css_entered_at_round = 0;
nreno->css_fas_at_css_entry = 0;
nreno->css_lowrtt_fas = 0;
nreno->css_last_fas = 0;
return (0);
}
static void
newreno_cb_destroy(struct cc_var *ccv)
{
free(ccv->cc_data, M_CC_MEM);
}
static void
newreno_ack_received(struct cc_var *ccv, ccsignal_t type)
{
struct newreno *nreno;
uint32_t mss = tcp_fixed_maxseg(ccv->tp);
nreno = ccv->cc_data;
if (type == CC_ACK && !IN_RECOVERY(CCV(ccv, t_flags)) &&
(ccv->flags & CCF_CWND_LIMITED)) {
u_int cw = CCV(ccv, snd_cwnd);
u_int incr = mss;
if (cw > CCV(ccv, snd_ssthresh)) {
if (nreno->newreno_flags & CC_NEWRENO_HYSTART_IN_CSS) {
nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_ENABLED;
newreno_log_hystart_event(ccv, nreno, 11, CCV(ccv, snd_ssthresh));
}
if (V_tcp_do_rfc3465) {
if (ccv->flags & CCF_ABC_SENTAWND)
ccv->flags &= ~CCF_ABC_SENTAWND;
else
incr = 0;
} else
incr = max((incr * incr / cw), 1);
} else if (V_tcp_do_rfc3465) {
uint16_t abc_val;
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) &&
(nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED) &&
((nreno->newreno_flags & CC_NEWRENO_HYSTART_IN_CSS) == 0)) {
if ((nreno->css_rttsample_count >= hystart_n_rttsamples) &&
(nreno->css_current_round_minrtt != 0xffffffff) &&
(nreno->css_lastround_minrtt != 0xffffffff)) {
uint32_t rtt_thresh;
rtt_thresh = (nreno->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;
newreno_log_hystart_event(ccv, nreno, 1, rtt_thresh);
if (nreno->css_current_round_minrtt >= (nreno->css_lastround_minrtt + rtt_thresh)) {
nreno->newreno_flags |= CC_NEWRENO_HYSTART_IN_CSS;
nreno->css_fas_at_css_entry = nreno->css_lowrtt_fas;
nreno->css_baseline_minrtt = nreno->css_current_round_minrtt;
nreno->css_entered_at_round = nreno->css_current_round;
newreno_log_hystart_event(ccv, nreno, 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 (nreno->newreno_flags & CC_NEWRENO_HYSTART_IN_CSS) {
incr /= hystart_css_growth_div;
newreno_log_hystart_event(ccv, nreno, 3, incr);
}
}
if (incr > 0)
CCV(ccv, snd_cwnd) = min(cw + incr,
TCP_MAXWIN << CCV(ccv, snd_scale));
}
}
static void
newreno_after_idle(struct cc_var *ccv)
{
struct newreno *nreno;
nreno = ccv->cc_data;
newreno_cc_after_idle(ccv);
if ((nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED) == 0) {
nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
nreno->newreno_flags |= CC_NEWRENO_HYSTART_ENABLED;
newreno_log_hystart_event(ccv, nreno, 12, CCV(ccv, snd_ssthresh));
}
}
static void
newreno_cong_signal(struct cc_var *ccv, ccsignal_t type)
{
struct newreno *nreno;
uint32_t beta, beta_ecn, cwin, factor, mss, pipe;
cwin = CCV(ccv, snd_cwnd);
mss = tcp_fixed_maxseg(ccv->tp);
nreno = ccv->cc_data;
beta = (nreno == NULL) ? V_newreno_beta : nreno->beta;
beta_ecn = (nreno == NULL) ? V_newreno_beta_ecn : nreno->beta_ecn;
if ((type == CC_ECN) &&
(V_cc_do_abe ||
((nreno != NULL) && (nreno->newreno_flags & CC_NEWRENO_BETA_ECN_ENABLED))))
factor = beta_ecn;
else
factor = beta;
KASSERT((type & CC_SIGPRIVMASK) == 0,
("%s: congestion signal type 0x%08x is private\n", __func__, type));
cwin = max(((uint64_t)cwin * (uint64_t)factor) / (100ULL * (uint64_t)mss),
2) * mss;
switch (type) {
case CC_NDUPACK:
if (nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED) {
nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_ENABLED;
nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
newreno_log_hystart_event(ccv, nreno, 10, CCV(ccv, snd_ssthresh));
}
if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
if (IN_CONGRECOVERY(CCV(ccv, t_flags) &&
V_cc_do_abe && V_cc_abe_frlossreduce)) {
CCV(ccv, snd_ssthresh) =
((uint64_t)CCV(ccv, snd_ssthresh) *
(uint64_t)beta) / (uint64_t)beta_ecn;
}
if (!IN_CONGRECOVERY(CCV(ccv, t_flags)))
CCV(ccv, snd_ssthresh) = cwin;
ENTER_RECOVERY(CCV(ccv, t_flags));
}
break;
case CC_ECN:
if (nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED) {
nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_ENABLED;
nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
newreno_log_hystart_event(ccv, nreno, 9, CCV(ccv, snd_ssthresh));
}
if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
CCV(ccv, snd_ssthresh) = cwin;
CCV(ccv, snd_cwnd) = cwin;
ENTER_CONGRECOVERY(CCV(ccv, t_flags));
}
break;
case CC_RTO:
if (CCV(ccv, t_rxtshift) == 1) {
pipe = tcp_compute_pipe(ccv->tp);
CCV(ccv, snd_ssthresh) = max(2,
((uint64_t)min(CCV(ccv, snd_wnd), pipe) *
(uint64_t)factor) /
(100ULL * (uint64_t)mss)) * mss;
}
CCV(ccv, snd_cwnd) = mss;
break;
default:
break;
}
}
static int
newreno_ctl_output(struct cc_var *ccv, struct sockopt *sopt, void *buf)
{
struct newreno *nreno;
struct cc_newreno_opts *opt;
if (sopt->sopt_valsize != sizeof(struct cc_newreno_opts))
return (EMSGSIZE);
if (CC_ALGO(ccv->tp) != &newreno_cc_algo)
return (ENOPROTOOPT);
nreno = (struct newreno *)ccv->cc_data;
opt = buf;
switch (sopt->sopt_dir) {
case SOPT_SET:
switch (opt->name) {
case CC_NEWRENO_BETA:
nreno->beta = opt->val;
break;
case CC_NEWRENO_BETA_ECN:
nreno->beta_ecn = opt->val;
nreno->newreno_flags |= CC_NEWRENO_BETA_ECN_ENABLED;
break;
default:
return (ENOPROTOOPT);
}
break;
case SOPT_GET:
switch (opt->name) {
case CC_NEWRENO_BETA:
opt->val = nreno->beta;
break;
case CC_NEWRENO_BETA_ECN:
opt->val = nreno->beta_ecn;
break;
default:
return (ENOPROTOOPT);
}
break;
default:
return (EINVAL);
}
return (0);
}
static int
newreno_beta_handler(SYSCTL_HANDLER_ARGS)
{
int error;
uint32_t new;
new = *(uint32_t *)arg1;
error = sysctl_handle_int(oidp, &new, 0, req);
if (error == 0 && req->newptr != NULL ) {
if (arg1 == &VNET_NAME(newreno_beta_ecn) && !V_cc_do_abe)
error = EACCES;
else if (new == 0 || new > 100)
error = EINVAL;
else
*(uint32_t *)arg1 = new;
}
return (error);
}
static void
newreno_newround(struct cc_var *ccv, uint32_t round_cnt)
{
struct newreno *nreno;
nreno = (struct newreno *)ccv->cc_data;
nreno->css_lastround_minrtt = nreno->css_current_round_minrtt;
nreno->css_current_round_minrtt = 0xffffffff;
nreno->css_rttsample_count = 0;
nreno->css_current_round = round_cnt;
if ((nreno->newreno_flags & CC_NEWRENO_HYSTART_IN_CSS) &&
((round_cnt - nreno->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) = ((nreno->css_lowrtt_fas + nreno->css_fas_at_css_entry) / 2);
} else {
CCV(ccv, snd_ssthresh) = nreno->css_lowrtt_fas;
}
CCV(ccv, snd_cwnd) = nreno->css_fas_at_css_entry;
nreno->css_entered_at_round = round_cnt;
} else {
CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_ENABLED;
}
newreno_log_hystart_event(ccv, nreno, 6, CCV(ccv, snd_ssthresh));
}
if (nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED)
newreno_log_hystart_event(ccv, nreno, 4, round_cnt);
}
static void
newreno_rttsample(struct cc_var *ccv, uint32_t usec_rtt, uint32_t rxtcnt, uint32_t fas)
{
struct newreno *nreno;
nreno = (struct newreno *)ccv->cc_data;
if (rxtcnt > 1) {
return;
}
nreno->css_rttsample_count++;
nreno->css_last_fas = fas;
if (nreno->css_current_round_minrtt > usec_rtt) {
nreno->css_current_round_minrtt = usec_rtt;
nreno->css_lowrtt_fas = nreno->css_last_fas;
}
if ((nreno->css_rttsample_count >= hystart_n_rttsamples) &&
(nreno->css_current_round_minrtt != 0xffffffff) &&
(nreno->css_current_round_minrtt < nreno->css_baseline_minrtt) &&
(nreno->css_lastround_minrtt != 0xffffffff)) {
nreno->newreno_flags &= ~CC_NEWRENO_HYSTART_IN_CSS;
newreno_log_hystart_event(ccv, nreno, 8, nreno->css_baseline_minrtt);
nreno->css_baseline_minrtt = 0xffffffff;
}
if (nreno->newreno_flags & CC_NEWRENO_HYSTART_ENABLED)
newreno_log_hystart_event(ccv, nreno, 5, usec_rtt);
}
SYSCTL_DECL(_net_inet_tcp_cc_newreno);
SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, newreno,
CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
"New Reno related settings");
SYSCTL_PROC(_net_inet_tcp_cc_newreno, OID_AUTO, beta,
CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
&VNET_NAME(newreno_beta), 3, &newreno_beta_handler, "IU",
"New Reno beta, specified as number between 1 and 100");
SYSCTL_PROC(_net_inet_tcp_cc_newreno, OID_AUTO, beta_ecn,
CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
&VNET_NAME(newreno_beta_ecn), 3, &newreno_beta_handler, "IU",
"New Reno beta ecn, specified as number between 1 and 100");
DECLARE_CC_MODULE(newreno, &newreno_cc_algo);
MODULE_VERSION(newreno, 2);