#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: tcp_congctl.c,v 1.29 2024/05/14 19:00:44 andvar Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
#include "opt_tcp_debug.h"
#include "opt_tcp_congctl.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/errno.h>
#include <sys/syslog.h>
#include <sys/pool.h>
#include <sys/domain.h>
#include <sys/kernel.h>
#include <sys/mutex.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#include <netinet/in_var.h>
#include <netinet/ip_var.h>
#ifdef INET6
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet6/in6_pcb.h>
#include <netinet6/ip6_var.h>
#include <netinet6/in6_var.h>
#include <netinet/icmp6.h>
#endif
#include <netinet/tcp.h>
#include <netinet/tcp_fsm.h>
#include <netinet/tcp_seq.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>
#include <netinet/tcp_congctl.h>
#ifdef TCP_DEBUG
#include <netinet/tcp_debug.h>
#endif
static void tcp_common_congestion_exp(struct tcpcb *, int, int);
static int tcp_reno_do_fast_retransmit(struct tcpcb *, const struct tcphdr *);
static int tcp_reno_fast_retransmit(struct tcpcb *, const struct tcphdr *);
static void tcp_reno_slow_retransmit(struct tcpcb *);
static void tcp_reno_fast_retransmit_newack(struct tcpcb *,
const struct tcphdr *);
static void tcp_reno_newack(struct tcpcb *, const struct tcphdr *);
static void tcp_reno_congestion_exp(struct tcpcb *tp);
static int tcp_newreno_fast_retransmit(struct tcpcb *, const struct tcphdr *);
static void tcp_newreno_fast_retransmit_newack(struct tcpcb *,
const struct tcphdr *);
static void tcp_newreno_newack(struct tcpcb *, const struct tcphdr *);
static int tcp_cubic_fast_retransmit(struct tcpcb *, const struct tcphdr *);
static void tcp_cubic_slow_retransmit(struct tcpcb *tp);
static void tcp_cubic_newack(struct tcpcb *, const struct tcphdr *);
static void tcp_cubic_congestion_exp(struct tcpcb *);
static void tcp_congctl_fillnames(void);
extern int tcprexmtthresh;
MALLOC_DEFINE(M_TCPCONGCTL, "tcpcongctl", "TCP congestion control structures");
char tcp_congctl_global_name[TCPCC_MAXLEN];
char tcp_congctl_avail[10 * TCPCC_MAXLEN];
TAILQ_HEAD(, tcp_congctlent) tcp_congctlhd =
TAILQ_HEAD_INITIALIZER(tcp_congctlhd);
static struct tcp_congctlent * tcp_congctl_global;
static kmutex_t tcp_congctl_mtx;
void
tcp_congctl_init(void)
{
int r __diagused;
mutex_init(&tcp_congctl_mtx, MUTEX_DEFAULT, IPL_NONE);
r = tcp_congctl_register("reno", &tcp_reno_ctl);
KASSERT(r == 0);
r = tcp_congctl_register("newreno", &tcp_newreno_ctl);
KASSERT(r == 0);
r = tcp_congctl_register("cubic", &tcp_cubic_ctl);
KASSERT(r == 0);
#ifndef TCP_CONGCTL_DEFAULT
#define TCP_CONGCTL_DEFAULT "newreno"
#endif
r = tcp_congctl_select(NULL, TCP_CONGCTL_DEFAULT);
KASSERT(r == 0);
}
int
tcp_congctl_register(const char *name, const struct tcp_congctl *tcc)
{
struct tcp_congctlent *ntcc, *tccp;
TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent)
if (!strcmp(name, tccp->congctl_name)) {
return EEXIST;
}
ntcc = malloc(sizeof(*ntcc), M_TCPCONGCTL, M_WAITOK|M_ZERO);
strlcpy(ntcc->congctl_name, name, sizeof(ntcc->congctl_name) - 1);
ntcc->congctl_ctl = tcc;
TAILQ_INSERT_TAIL(&tcp_congctlhd, ntcc, congctl_ent);
tcp_congctl_fillnames();
if (TAILQ_FIRST(&tcp_congctlhd) == ntcc)
tcp_congctl_select(NULL, name);
return 0;
}
int
tcp_congctl_unregister(const char *name)
{
struct tcp_congctlent *tccp, *rtccp;
unsigned int size;
rtccp = NULL;
size = 0;
TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent) {
if (!strcmp(name, tccp->congctl_name))
rtccp = tccp;
size++;
}
if (!rtccp)
return ENOENT;
if (size <= 1 || tcp_congctl_global == rtccp || rtccp->congctl_refcnt)
return EBUSY;
TAILQ_REMOVE(&tcp_congctlhd, rtccp, congctl_ent);
free(rtccp, M_TCPCONGCTL);
tcp_congctl_fillnames();
return 0;
}
int
tcp_congctl_select(struct tcpcb *tp, const char *name)
{
struct tcp_congctlent *tccp, *old_tccp, *new_tccp;
bool old_found, new_found;
KASSERT(name);
old_found = (tp == NULL || tp->t_congctl == NULL);
old_tccp = NULL;
new_found = false;
new_tccp = NULL;
TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent) {
if (!old_found && tccp->congctl_ctl == tp->t_congctl) {
old_tccp = tccp;
old_found = true;
}
if (!new_found && !strcmp(name, tccp->congctl_name)) {
new_tccp = tccp;
new_found = true;
}
if (new_found && old_found) {
if (tp) {
mutex_enter(&tcp_congctl_mtx);
if (old_tccp)
old_tccp->congctl_refcnt--;
tp->t_congctl = new_tccp->congctl_ctl;
new_tccp->congctl_refcnt++;
mutex_exit(&tcp_congctl_mtx);
} else {
tcp_congctl_global = new_tccp;
strlcpy(tcp_congctl_global_name,
new_tccp->congctl_name,
sizeof(tcp_congctl_global_name) - 1);
}
return 0;
}
}
return EINVAL;
}
void
tcp_congctl_release(struct tcpcb *tp)
{
struct tcp_congctlent *tccp;
KASSERT(tp->t_congctl);
TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent) {
if (tccp->congctl_ctl == tp->t_congctl) {
tccp->congctl_refcnt--;
return;
}
}
}
const char *
tcp_congctl_bystruct(const struct tcp_congctl *tcc)
{
struct tcp_congctlent *tccp;
KASSERT(tcc);
TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent)
if (tccp->congctl_ctl == tcc)
return tccp->congctl_name;
return NULL;
}
static void
tcp_congctl_fillnames(void)
{
struct tcp_congctlent *tccp;
const char *delim = " ";
tcp_congctl_avail[0] = '\0';
TAILQ_FOREACH(tccp, &tcp_congctlhd, congctl_ent) {
strlcat(tcp_congctl_avail, tccp->congctl_name,
sizeof(tcp_congctl_avail) - 1);
if (TAILQ_NEXT(tccp, congctl_ent))
strlcat(tcp_congctl_avail, delim,
sizeof(tcp_congctl_avail) - 1);
}
}
#define RENO_BETAA 1
#define RENO_BETAB 2
#define CUBIC_BETAA 4
#define CUBIC_BETAB 5
#define CUBIC_CA 4
#define CUBIC_CB 10
static void
tcp_common_congestion_exp(struct tcpcb *tp, int betaa, int betab)
{
u_long win;
win = ulmin(tp->snd_wnd, tp->snd_cwnd) * betaa / betab / tp->t_segsz;
if (win < 2)
win = 2;
tp->snd_ssthresh = win * tp->t_segsz;
tp->snd_recover = tp->snd_max;
tp->snd_cwnd = tp->snd_ssthresh;
if (TCP_ECN_ALLOWED(tp))
tp->t_flags |= TF_ECN_SND_CWR;
}
static void
tcp_reno_congestion_exp(struct tcpcb *tp)
{
tcp_common_congestion_exp(tp, RENO_BETAA, RENO_BETAB);
}
static int
tcp_reno_do_fast_retransmit(struct tcpcb *tp, const struct tcphdr *th)
{
tcp_seq onxt = tp->snd_nxt;
tp->t_partialacks = 0;
TCP_TIMER_DISARM(tp, TCPT_REXMT);
tp->t_rtttime = 0;
if (TCP_SACK_ENABLED(tp)) {
tp->t_dupacks = tcprexmtthresh;
tp->sack_newdata = tp->snd_nxt;
tp->snd_cwnd = tp->t_segsz;
(void) tcp_output(tp);
return 0;
}
tp->snd_nxt = th->th_ack;
tp->snd_cwnd = tp->t_segsz;
(void) tcp_output(tp);
tp->snd_cwnd = tp->snd_ssthresh + tp->t_segsz * tp->t_dupacks;
if (SEQ_GT(onxt, tp->snd_nxt))
tp->snd_nxt = onxt;
return 0;
}
static int
tcp_reno_fast_retransmit(struct tcpcb *tp, const struct tcphdr *th)
{
tcp_reno_congestion_exp(tp);
return tcp_reno_do_fast_retransmit(tp, th);
}
static void
tcp_reno_slow_retransmit(struct tcpcb *tp)
{
u_long win;
win = ulmin(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_segsz;
if (win < 2)
win = 2;
tp->snd_cwnd = tp->t_segsz;
tp->snd_ssthresh = win * tp->t_segsz;
tp->t_partialacks = -1;
tp->t_dupacks = 0;
tp->t_bytes_acked = 0;
if (TCP_ECN_ALLOWED(tp))
tp->t_flags |= TF_ECN_SND_CWR;
}
static void
tcp_reno_fast_retransmit_newack(struct tcpcb *tp,
const struct tcphdr *th)
{
if (tp->t_partialacks < 0) {
tp->t_dupacks = 0;
} else {
if (tp->snd_cwnd > tp->snd_ssthresh)
tp->snd_cwnd = tp->snd_ssthresh;
tp->t_partialacks = -1;
tp->t_dupacks = 0;
tp->t_bytes_acked = 0;
if (TCP_SACK_ENABLED(tp) && SEQ_GT(th->th_ack, tp->snd_fack))
tp->snd_fack = th->th_ack;
}
}
static void
tcp_reno_newack(struct tcpcb *tp, const struct tcphdr *th)
{
u_int cw = tp->snd_cwnd;
u_int incr = tp->t_segsz;
if (tcp_do_abc) {
int acked = th->th_ack - tp->snd_una;
if (cw >= tp->snd_ssthresh) {
tp->t_bytes_acked += acked;
if (tp->t_bytes_acked >= cw) {
tp->t_bytes_acked -= cw;
} else {
incr = 0;
}
} else {
u_int abc_lim;
abc_lim = (tcp_abc_aggressive == 0 ||
tp->snd_nxt != tp->snd_max) ? incr : incr * 2;
incr = uimin(acked, abc_lim);
}
} else {
if (cw >= tp->snd_ssthresh) {
incr = incr * incr / cw;
}
}
tp->snd_cwnd = uimin(cw + incr, TCP_MAXWIN << tp->snd_scale);
}
const struct tcp_congctl tcp_reno_ctl = {
.fast_retransmit = tcp_reno_fast_retransmit,
.slow_retransmit = tcp_reno_slow_retransmit,
.fast_retransmit_newack = tcp_reno_fast_retransmit_newack,
.newack = tcp_reno_newack,
.cong_exp = tcp_reno_congestion_exp,
};
static int
tcp_newreno_fast_retransmit(struct tcpcb *tp, const struct tcphdr *th)
{
if (SEQ_LT(th->th_ack, tp->snd_high)) {
tp->t_dupacks = 0;
return 1;
}
return tcp_reno_fast_retransmit(tp, th);
}
static void
tcp_newreno_fast_retransmit_newack(struct tcpcb *tp, const struct tcphdr *th)
{
if (tp->t_partialacks < 0) {
tp->t_dupacks = 0;
} else if (SEQ_LT(th->th_ack, tp->snd_recover)) {
tcp_seq onxt = tp->snd_nxt;
u_long ocwnd = tp->snd_cwnd;
int sack_num_segs = 1, sack_bytes_rxmt = 0;
tp->t_partialacks++;
TCP_TIMER_DISARM(tp, TCPT_REXMT);
tp->t_rtttime = 0;
if (TCP_SACK_ENABLED(tp)) {
if (((th->th_ack - tp->snd_una) / tp->t_segsz) > 2)
sack_num_segs = 2;
(void)tcp_sack_output(tp, &sack_bytes_rxmt);
tp->snd_cwnd = sack_bytes_rxmt +
(tp->snd_nxt - tp->sack_newdata) +
sack_num_segs * tp->t_segsz;
tp->t_flags |= TF_ACKNOW;
(void) tcp_output(tp);
} else {
tp->snd_nxt = th->th_ack;
tp->snd_cwnd = tp->t_segsz + (th->th_ack - tp->snd_una);
(void) tcp_output(tp);
tp->snd_cwnd = ocwnd;
if (SEQ_GT(onxt, tp->snd_nxt))
tp->snd_nxt = onxt;
tp->snd_cwnd -= (th->th_ack - tp->snd_una -
tp->t_segsz);
}
} else {
if (SEQ_SUB(tp->snd_max, th->th_ack) < tp->snd_ssthresh)
tp->snd_cwnd = SEQ_SUB(tp->snd_max, th->th_ack)
+ tp->t_segsz;
else
tp->snd_cwnd = tp->snd_ssthresh;
tp->t_partialacks = -1;
tp->t_dupacks = 0;
tp->t_bytes_acked = 0;
if (TCP_SACK_ENABLED(tp) && SEQ_GT(th->th_ack, tp->snd_fack))
tp->snd_fack = th->th_ack;
}
}
static void
tcp_newreno_newack(struct tcpcb *tp, const struct tcphdr *th)
{
if (tp->t_partialacks < 0)
tcp_reno_newack(tp, th);
}
const struct tcp_congctl tcp_newreno_ctl = {
.fast_retransmit = tcp_newreno_fast_retransmit,
.slow_retransmit = tcp_reno_slow_retransmit,
.fast_retransmit_newack = tcp_newreno_fast_retransmit_newack,
.newack = tcp_newreno_newack,
.cong_exp = tcp_reno_congestion_exp,
};
static void tcp_cubic_update_ctime(struct tcpcb *tp);
static uint32_t tcp_cubic_diff_ctime(struct tcpcb *);
static uint32_t tcp_cubic_cbrt(uint32_t);
static ulong tcp_cubic_getW(struct tcpcb *, uint32_t, uint32_t);
static void
tcp_cubic_update_ctime(struct tcpcb *tp)
{
struct timeval now_timeval;
getmicrouptime(&now_timeval);
tp->snd_cubic_ctime = now_timeval.tv_sec * 1000 +
now_timeval.tv_usec / 1000;
}
static uint32_t
tcp_cubic_diff_ctime(struct tcpcb *tp)
{
struct timeval now_timeval;
getmicrouptime(&now_timeval);
return now_timeval.tv_sec * 1000 + now_timeval.tv_usec / 1000 -
tp->snd_cubic_ctime;
}
#define CBRT_ROUNDS 30
static uint32_t
tcp_cubic_cbrt(uint32_t v)
{
int i, rounds = CBRT_ROUNDS;
uint64_t x = v / 3;
if (v == 0)
return 0;
else if (v < 4)
return 1;
if (x > 2097151)
rounds += 10;
for (i = 0; i < rounds; i++)
if (rounds == CBRT_ROUNDS)
x = (v + 2 * x * x * x) / (3 * x * x);
else
x = v / (3 * x * x) + 2 * x / 3;
return (uint32_t)x;
}
static ulong
tcp_cubic_getW(struct tcpcb *tp, uint32_t ms_elapsed, uint32_t rtt)
{
uint32_t K;
long tK3;
K = tcp_cubic_cbrt(tp->snd_cubic_wmax / CUBIC_BETAB *
CUBIC_CB / CUBIC_CA);
tK3 = (long)(ms_elapsed + rtt) - (long)K;
tK3 = tK3 * tK3 * tK3;
return CUBIC_CA * tK3 / CUBIC_CB + tp->snd_cubic_wmax;
}
static void
tcp_cubic_congestion_exp(struct tcpcb *tp)
{
tcp_cubic_update_ctime(tp);
if (tp->snd_cubic_wmax < tp->snd_cubic_wmax_last) {
tp->snd_cubic_wmax_last = tp->snd_cubic_wmax;
tp->snd_cubic_wmax = tp->snd_cubic_wmax / 2 +
tp->snd_cubic_wmax * CUBIC_BETAA / CUBIC_BETAB / 2;
} else {
tp->snd_cubic_wmax_last = tp->snd_cubic_wmax;
tp->snd_cubic_wmax = tp->snd_cwnd;
}
tp->snd_cubic_wmax = uimax(tp->t_segsz, tp->snd_cubic_wmax);
tcp_common_congestion_exp(tp, CUBIC_BETAA, CUBIC_BETAB);
}
static int
tcp_cubic_fast_retransmit(struct tcpcb *tp, const struct tcphdr *th)
{
if (SEQ_LT(th->th_ack, tp->snd_high)) {
tp->t_dupacks = 0;
return 1;
}
tcp_cubic_congestion_exp(tp);
return tcp_reno_do_fast_retransmit(tp, th);
}
static void
tcp_cubic_newack(struct tcpcb *tp, const struct tcphdr *th)
{
uint32_t ms_elapsed, rtt;
u_long w_tcp;
if (tp->snd_cwnd > tp->snd_ssthresh && tp->t_partialacks < 0 &&
(rtt = (tp->t_srtt << 5) / PR_SLOWHZ) > 0) {
ms_elapsed = tcp_cubic_diff_ctime(tp);
w_tcp = tp->snd_cubic_wmax * CUBIC_BETAA / CUBIC_BETAB +
ms_elapsed / rtt / 3;
if (tp->snd_cwnd > w_tcp) {
tp->snd_cwnd += (tcp_cubic_getW(tp, ms_elapsed, rtt) -
tp->snd_cwnd) / tp->snd_cwnd;
} else {
tp->snd_cwnd = w_tcp;
}
tp->snd_cwnd = uimax(tp->snd_cwnd, tp->t_segsz);
tp->snd_cwnd = uimin(tp->snd_cwnd, TCP_MAXWIN << tp->snd_scale);
} else {
tcp_newreno_newack(tp, th);
}
}
static void
tcp_cubic_slow_retransmit(struct tcpcb *tp)
{
tcp_cubic_congestion_exp(tp);
tp->snd_cwnd = tp->t_segsz;
tp->t_partialacks = -1;
tp->t_dupacks = 0;
tp->t_bytes_acked = 0;
if (TCP_ECN_ALLOWED(tp))
tp->t_flags |= TF_ECN_SND_CWR;
}
const struct tcp_congctl tcp_cubic_ctl = {
.fast_retransmit = tcp_cubic_fast_retransmit,
.slow_retransmit = tcp_cubic_slow_retransmit,
.fast_retransmit_newack = tcp_newreno_fast_retransmit_newack,
.newack = tcp_cubic_newack,
.cong_exp = tcp_cubic_congestion_exp,
};