freebsd-dev/sys/netinet/cc/cc_vegas.c
Gleb Smirnoff e68b379244 tcp: embed inpcb into tcpcb
For the TCP protocol inpcb storage specify allocation size that would
provide space to most of the data a TCP connection needs, embedding
into struct tcpcb several structures, that previously were allocated
separately.

The most import one is the inpcb itself.  With embedding we can provide
strong guarantee that with a valid TCP inpcb the tcpcb is always valid
and vice versa.  Also we reduce number of allocs/frees per connection.
The embedded inpcb is placed in the beginning of the struct tcpcb,
since in_pcballoc() requires that.  However, later we may want to move
it around for cache line efficiency, and this can be done with a little
effort.  The new intotcpcb() macro is ready for such move.

The congestion algorithm data, the TCP timers and osd(9) data are
also embedded into tcpcb, and temprorary struct tcpcb_mem goes away.
There was no extra allocation here, but we went through extra pointer
every time we accessed this data.

One interesting side effect is that now TCP data is allocated from
SMR-protected zone.  Potentially this allows the TCP stacks or other
TCP related modules to utilize that for their own synchronization.

Large part of the change was done with sed script:

s/tp->ccv->/tp->t_ccv./g
s/tp->ccv/\&tp->t_ccv/g
s/tp->cc_algo/tp->t_cc/g
s/tp->t_timers->tt_/tp->tt_/g
s/CCV\(ccv, osd\)/\&CCV(ccv, t_osd)/g

Dependency side effect is that code that needs to know struct tcpcb
should also know struct inpcb, that added several <netinet/in_pcb.h>.

Differential revision:	https://reviews.freebsd.org/D37127
2022-12-07 09:00:48 -08:00

315 lines
9.1 KiB
C

/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2009-2010
* Swinburne University of Technology, Melbourne, Australia
* Copyright (c) 2010 Lawrence Stewart <lstewart@freebsd.org>
* Copyright (c) 2010-2011 The FreeBSD Foundation
* All rights reserved.
*
* This software was developed at the Centre for Advanced Internet
* Architectures, Swinburne University of Technology, by David Hayes and
* Lawrence Stewart, made possible in part by a grant from the Cisco University
* Research Program Fund at Community Foundation Silicon Valley.
*
* Portions of this software were developed at the Centre for Advanced Internet
* Architectures, Swinburne University of Technology, Melbourne, Australia by
* David Hayes under sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* An implementation of the Vegas congestion control algorithm for FreeBSD,
* based on L. S. Brakmo and L. L. Peterson, "TCP Vegas: end to end congestion
* avoidance on a global internet", IEEE J. Sel. Areas Commun., vol. 13, no. 8,
* pp. 1465-1480, Oct. 1995. The original Vegas duplicate ack policy has not
* been implemented, since clock ticks are not as coarse as they were (i.e.
* 500ms) when Vegas was designed. Also, packets are timed once per RTT as in
* the original paper.
*
* Originally released as part of the NewTCP research project at Swinburne
* University of Technology's Centre for Advanced Internet Architectures,
* Melbourne, Australia, which was made possible in part by a grant from the
* Cisco University Research Program Fund at Community Foundation Silicon
* Valley. More details are available at:
* http://caia.swin.edu.au/urp/newtcp/
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/khelp.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/queue.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_timer.h>
#include <netinet/tcp_var.h>
#include <netinet/cc/cc.h>
#include <netinet/cc/cc_module.h>
#include <netinet/khelp/h_ertt.h>
/*
* Private signal type for rate based congestion signal.
* See <netinet/cc.h> for appropriate bit-range to use for private signals.
*/
#define CC_VEGAS_RATE 0x01000000
static void vegas_ack_received(struct cc_var *ccv, uint16_t ack_type);
static void vegas_cb_destroy(struct cc_var *ccv);
static int vegas_cb_init(struct cc_var *ccv, void *ptr);
static void vegas_cong_signal(struct cc_var *ccv, uint32_t signal_type);
static void vegas_conn_init(struct cc_var *ccv);
static int vegas_mod_init(void);
static size_t vegas_data_sz(void);
struct vegas {
int slow_start_toggle;
};
static int32_t ertt_id;
VNET_DEFINE_STATIC(uint32_t, vegas_alpha) = 1;
VNET_DEFINE_STATIC(uint32_t, vegas_beta) = 3;
#define V_vegas_alpha VNET(vegas_alpha)
#define V_vegas_beta VNET(vegas_beta)
struct cc_algo vegas_cc_algo = {
.name = "vegas",
.ack_received = vegas_ack_received,
.cb_destroy = vegas_cb_destroy,
.cb_init = vegas_cb_init,
.cong_signal = vegas_cong_signal,
.conn_init = vegas_conn_init,
.mod_init = vegas_mod_init,
.cc_data_sz = vegas_data_sz,
.after_idle = newreno_cc_after_idle,
.post_recovery = newreno_cc_post_recovery,
};
/*
* The vegas window adjustment is done once every RTT, as indicated by the
* ERTT_NEW_MEASUREMENT flag. This flag is reset once the new measurement data
* has been used.
*/
static void
vegas_ack_received(struct cc_var *ccv, uint16_t ack_type)
{
struct ertt *e_t;
struct vegas *vegas_data;
long actual_tx_rate, expected_tx_rate, ndiff;
e_t = khelp_get_osd(&CCV(ccv, t_osd), ertt_id);
vegas_data = ccv->cc_data;
if (e_t->flags & ERTT_NEW_MEASUREMENT) { /* Once per RTT. */
if (e_t->minrtt && e_t->markedpkt_rtt) {
expected_tx_rate = e_t->marked_snd_cwnd / e_t->minrtt;
actual_tx_rate = e_t->bytes_tx_in_marked_rtt /
e_t->markedpkt_rtt;
ndiff = (expected_tx_rate - actual_tx_rate) *
e_t->minrtt / CCV(ccv, t_maxseg);
if (ndiff < V_vegas_alpha) {
if (CCV(ccv, snd_cwnd) <=
CCV(ccv, snd_ssthresh)) {
vegas_data->slow_start_toggle =
vegas_data->slow_start_toggle ?
0 : 1;
} else {
vegas_data->slow_start_toggle = 0;
CCV(ccv, snd_cwnd) =
min(CCV(ccv, snd_cwnd) +
CCV(ccv, t_maxseg),
TCP_MAXWIN << CCV(ccv, snd_scale));
}
} else if (ndiff > V_vegas_beta) {
/* Rate-based congestion. */
vegas_cong_signal(ccv, CC_VEGAS_RATE);
vegas_data->slow_start_toggle = 0;
}
}
e_t->flags &= ~ERTT_NEW_MEASUREMENT;
}
if (vegas_data->slow_start_toggle)
newreno_cc_ack_received(ccv, ack_type);
}
static void
vegas_cb_destroy(struct cc_var *ccv)
{
free(ccv->cc_data, M_CC_MEM);
}
static size_t
vegas_data_sz(void)
{
return (sizeof(struct vegas));
}
static int
vegas_cb_init(struct cc_var *ccv, void *ptr)
{
struct vegas *vegas_data;
INP_WLOCK_ASSERT(tptoinpcb(ccv->ccvc.tcp));
if (ptr == NULL) {
vegas_data = malloc(sizeof(struct vegas), M_CC_MEM, M_NOWAIT);
if (vegas_data == NULL)
return (ENOMEM);
} else
vegas_data = ptr;
vegas_data->slow_start_toggle = 1;
ccv->cc_data = vegas_data;
return (0);
}
/*
* If congestion has been triggered triggered by the Vegas measured rates, it is
* handled here, otherwise it falls back to newreno's congestion handling.
*/
static void
vegas_cong_signal(struct cc_var *ccv, uint32_t signal_type)
{
struct vegas *vegas_data;
int presignalrecov;
vegas_data = ccv->cc_data;
if (IN_RECOVERY(CCV(ccv, t_flags)))
presignalrecov = 1;
else
presignalrecov = 0;
switch(signal_type) {
case CC_VEGAS_RATE:
if (!IN_RECOVERY(CCV(ccv, t_flags))) {
CCV(ccv, snd_cwnd) = max(2 * CCV(ccv, t_maxseg),
CCV(ccv, snd_cwnd) - CCV(ccv, t_maxseg));
if (CCV(ccv, snd_cwnd) < CCV(ccv, snd_ssthresh))
/* Exit slow start. */
CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
}
break;
default:
newreno_cc_cong_signal(ccv, signal_type);
}
if (IN_RECOVERY(CCV(ccv, t_flags)) && !presignalrecov)
vegas_data->slow_start_toggle =
(CCV(ccv, snd_cwnd) < CCV(ccv, snd_ssthresh)) ? 1 : 0;
}
static void
vegas_conn_init(struct cc_var *ccv)
{
struct vegas *vegas_data;
vegas_data = ccv->cc_data;
vegas_data->slow_start_toggle = 1;
}
static int
vegas_mod_init(void)
{
ertt_id = khelp_get_id("ertt");
if (ertt_id <= 0) {
printf("%s: h_ertt module not found\n", __func__);
return (ENOENT);
}
return (0);
}
static int
vegas_alpha_handler(SYSCTL_HANDLER_ARGS)
{
int error;
uint32_t new;
new = V_vegas_alpha;
error = sysctl_handle_int(oidp, &new, 0, req);
if (error == 0 && req->newptr != NULL) {
if (new == 0 || new > V_vegas_beta)
error = EINVAL;
else
V_vegas_alpha = new;
}
return (error);
}
static int
vegas_beta_handler(SYSCTL_HANDLER_ARGS)
{
int error;
uint32_t new;
new = V_vegas_beta;
error = sysctl_handle_int(oidp, &new, 0, req);
if (error == 0 && req->newptr != NULL) {
if (new == 0 || new < V_vegas_alpha)
error = EINVAL;
else
V_vegas_beta = new;
}
return (error);
}
SYSCTL_DECL(_net_inet_tcp_cc_vegas);
SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, vegas,
CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
"Vegas related settings");
SYSCTL_PROC(_net_inet_tcp_cc_vegas, OID_AUTO, alpha,
CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
&VNET_NAME(vegas_alpha), 1, &vegas_alpha_handler, "IU",
"vegas alpha, specified as number of \"buffers\" (0 < alpha < beta)");
SYSCTL_PROC(_net_inet_tcp_cc_vegas, OID_AUTO, beta,
CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
&VNET_NAME(vegas_beta), 3, &vegas_beta_handler, "IU",
"vegas beta, specified as number of \"buffers\" (0 < alpha < beta)");
DECLARE_CC_MODULE(vegas, &vegas_cc_algo);
MODULE_VERSION(vegas, 2);
MODULE_DEPEND(vegas, ertt, 1, 1, 1);