162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * TCP Vegas congestion control
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * This is based on the congestion detection/avoidance scheme described in
662306a36Sopenharmony_ci *    Lawrence S. Brakmo and Larry L. Peterson.
762306a36Sopenharmony_ci *    "TCP Vegas: End to end congestion avoidance on a global internet."
862306a36Sopenharmony_ci *    IEEE Journal on Selected Areas in Communication, 13(8):1465--1480,
962306a36Sopenharmony_ci *    October 1995. Available from:
1062306a36Sopenharmony_ci *	ftp://ftp.cs.arizona.edu/xkernel/Papers/jsac.ps
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * See http://www.cs.arizona.edu/xkernel/ for their implementation.
1362306a36Sopenharmony_ci * The main aspects that distinguish this implementation from the
1462306a36Sopenharmony_ci * Arizona Vegas implementation are:
1562306a36Sopenharmony_ci *   o We do not change the loss detection or recovery mechanisms of
1662306a36Sopenharmony_ci *     Linux in any way. Linux already recovers from losses quite well,
1762306a36Sopenharmony_ci *     using fine-grained timers, NewReno, and FACK.
1862306a36Sopenharmony_ci *   o To avoid the performance penalty imposed by increasing cwnd
1962306a36Sopenharmony_ci *     only every-other RTT during slow start, we increase during
2062306a36Sopenharmony_ci *     every RTT during slow start, just like Reno.
2162306a36Sopenharmony_ci *   o Largely to allow continuous cwnd growth during slow start,
2262306a36Sopenharmony_ci *     we use the rate at which ACKs come back as the "actual"
2362306a36Sopenharmony_ci *     rate, rather than the rate at which data is sent.
2462306a36Sopenharmony_ci *   o To speed convergence to the right rate, we set the cwnd
2562306a36Sopenharmony_ci *     to achieve the right ("actual") rate when we exit slow start.
2662306a36Sopenharmony_ci *   o To filter out the noise caused by delayed ACKs, we use the
2762306a36Sopenharmony_ci *     minimum RTT sample observed during the last RTT to calculate
2862306a36Sopenharmony_ci *     the actual rate.
2962306a36Sopenharmony_ci *   o When the sender re-starts from idle, it waits until it has
3062306a36Sopenharmony_ci *     received ACKs for an entire flight of new data before making
3162306a36Sopenharmony_ci *     a cwnd adjustment decision. The original Vegas implementation
3262306a36Sopenharmony_ci *     assumed senders never went idle.
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#include <linux/mm.h>
3662306a36Sopenharmony_ci#include <linux/module.h>
3762306a36Sopenharmony_ci#include <linux/skbuff.h>
3862306a36Sopenharmony_ci#include <linux/inet_diag.h>
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#include <net/tcp.h>
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#include "tcp_vegas.h"
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_cistatic int alpha = 2;
4562306a36Sopenharmony_cistatic int beta  = 4;
4662306a36Sopenharmony_cistatic int gamma = 1;
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cimodule_param(alpha, int, 0644);
4962306a36Sopenharmony_ciMODULE_PARM_DESC(alpha, "lower bound of packets in network");
5062306a36Sopenharmony_cimodule_param(beta, int, 0644);
5162306a36Sopenharmony_ciMODULE_PARM_DESC(beta, "upper bound of packets in network");
5262306a36Sopenharmony_cimodule_param(gamma, int, 0644);
5362306a36Sopenharmony_ciMODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci/* There are several situations when we must "re-start" Vegas:
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci *  o when a connection is established
5862306a36Sopenharmony_ci *  o after an RTO
5962306a36Sopenharmony_ci *  o after fast recovery
6062306a36Sopenharmony_ci *  o when we send a packet and there is no outstanding
6162306a36Sopenharmony_ci *    unacknowledged data (restarting an idle connection)
6262306a36Sopenharmony_ci *
6362306a36Sopenharmony_ci * In these circumstances we cannot do a Vegas calculation at the
6462306a36Sopenharmony_ci * end of the first RTT, because any calculation we do is using
6562306a36Sopenharmony_ci * stale info -- both the saved cwnd and congestion feedback are
6662306a36Sopenharmony_ci * stale.
6762306a36Sopenharmony_ci *
6862306a36Sopenharmony_ci * Instead we must wait until the completion of an RTT during
6962306a36Sopenharmony_ci * which we actually receive ACKs.
7062306a36Sopenharmony_ci */
7162306a36Sopenharmony_cistatic void vegas_enable(struct sock *sk)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	const struct tcp_sock *tp = tcp_sk(sk);
7462306a36Sopenharmony_ci	struct vegas *vegas = inet_csk_ca(sk);
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	/* Begin taking Vegas samples next time we send something. */
7762306a36Sopenharmony_ci	vegas->doing_vegas_now = 1;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	/* Set the beginning of the next send window. */
8062306a36Sopenharmony_ci	vegas->beg_snd_nxt = tp->snd_nxt;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	vegas->cntRTT = 0;
8362306a36Sopenharmony_ci	vegas->minRTT = 0x7fffffff;
8462306a36Sopenharmony_ci}
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci/* Stop taking Vegas samples for now. */
8762306a36Sopenharmony_cistatic inline void vegas_disable(struct sock *sk)
8862306a36Sopenharmony_ci{
8962306a36Sopenharmony_ci	struct vegas *vegas = inet_csk_ca(sk);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	vegas->doing_vegas_now = 0;
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_civoid tcp_vegas_init(struct sock *sk)
9562306a36Sopenharmony_ci{
9662306a36Sopenharmony_ci	struct vegas *vegas = inet_csk_ca(sk);
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	vegas->baseRTT = 0x7fffffff;
9962306a36Sopenharmony_ci	vegas_enable(sk);
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(tcp_vegas_init);
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci/* Do RTT sampling needed for Vegas.
10462306a36Sopenharmony_ci * Basically we:
10562306a36Sopenharmony_ci *   o min-filter RTT samples from within an RTT to get the current
10662306a36Sopenharmony_ci *     propagation delay + queuing delay (we are min-filtering to try to
10762306a36Sopenharmony_ci *     avoid the effects of delayed ACKs)
10862306a36Sopenharmony_ci *   o min-filter RTT samples from a much longer window (forever for now)
10962306a36Sopenharmony_ci *     to find the propagation delay (baseRTT)
11062306a36Sopenharmony_ci */
11162306a36Sopenharmony_civoid tcp_vegas_pkts_acked(struct sock *sk, const struct ack_sample *sample)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	struct vegas *vegas = inet_csk_ca(sk);
11462306a36Sopenharmony_ci	u32 vrtt;
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	if (sample->rtt_us < 0)
11762306a36Sopenharmony_ci		return;
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	/* Never allow zero rtt or baseRTT */
12062306a36Sopenharmony_ci	vrtt = sample->rtt_us + 1;
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	/* Filter to find propagation delay: */
12362306a36Sopenharmony_ci	if (vrtt < vegas->baseRTT)
12462306a36Sopenharmony_ci		vegas->baseRTT = vrtt;
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	/* Find the min RTT during the last RTT to find
12762306a36Sopenharmony_ci	 * the current prop. delay + queuing delay:
12862306a36Sopenharmony_ci	 */
12962306a36Sopenharmony_ci	vegas->minRTT = min(vegas->minRTT, vrtt);
13062306a36Sopenharmony_ci	vegas->cntRTT++;
13162306a36Sopenharmony_ci}
13262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(tcp_vegas_pkts_acked);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_civoid tcp_vegas_state(struct sock *sk, u8 ca_state)
13562306a36Sopenharmony_ci{
13662306a36Sopenharmony_ci	if (ca_state == TCP_CA_Open)
13762306a36Sopenharmony_ci		vegas_enable(sk);
13862306a36Sopenharmony_ci	else
13962306a36Sopenharmony_ci		vegas_disable(sk);
14062306a36Sopenharmony_ci}
14162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(tcp_vegas_state);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci/*
14462306a36Sopenharmony_ci * If the connection is idle and we are restarting,
14562306a36Sopenharmony_ci * then we don't want to do any Vegas calculations
14662306a36Sopenharmony_ci * until we get fresh RTT samples.  So when we
14762306a36Sopenharmony_ci * restart, we reset our Vegas state to a clean
14862306a36Sopenharmony_ci * slate. After we get acks for this flight of
14962306a36Sopenharmony_ci * packets, _then_ we can make Vegas calculations
15062306a36Sopenharmony_ci * again.
15162306a36Sopenharmony_ci */
15262306a36Sopenharmony_civoid tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
15362306a36Sopenharmony_ci{
15462306a36Sopenharmony_ci	if (event == CA_EVENT_CWND_RESTART ||
15562306a36Sopenharmony_ci	    event == CA_EVENT_TX_START)
15662306a36Sopenharmony_ci		tcp_vegas_init(sk);
15762306a36Sopenharmony_ci}
15862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(tcp_vegas_cwnd_event);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_cistatic inline u32 tcp_vegas_ssthresh(struct tcp_sock *tp)
16162306a36Sopenharmony_ci{
16262306a36Sopenharmony_ci	return  min(tp->snd_ssthresh, tcp_snd_cwnd(tp));
16362306a36Sopenharmony_ci}
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_cistatic void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 acked)
16662306a36Sopenharmony_ci{
16762306a36Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
16862306a36Sopenharmony_ci	struct vegas *vegas = inet_csk_ca(sk);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	if (!vegas->doing_vegas_now) {
17162306a36Sopenharmony_ci		tcp_reno_cong_avoid(sk, ack, acked);
17262306a36Sopenharmony_ci		return;
17362306a36Sopenharmony_ci	}
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	if (after(ack, vegas->beg_snd_nxt)) {
17662306a36Sopenharmony_ci		/* Do the Vegas once-per-RTT cwnd adjustment. */
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci		/* Save the extent of the current window so we can use this
17962306a36Sopenharmony_ci		 * at the end of the next RTT.
18062306a36Sopenharmony_ci		 */
18162306a36Sopenharmony_ci		vegas->beg_snd_nxt  = tp->snd_nxt;
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci		/* We do the Vegas calculations only if we got enough RTT
18462306a36Sopenharmony_ci		 * samples that we can be reasonably sure that we got
18562306a36Sopenharmony_ci		 * at least one RTT sample that wasn't from a delayed ACK.
18662306a36Sopenharmony_ci		 * If we only had 2 samples total,
18762306a36Sopenharmony_ci		 * then that means we're getting only 1 ACK per RTT, which
18862306a36Sopenharmony_ci		 * means they're almost certainly delayed ACKs.
18962306a36Sopenharmony_ci		 * If  we have 3 samples, we should be OK.
19062306a36Sopenharmony_ci		 */
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci		if (vegas->cntRTT <= 2) {
19362306a36Sopenharmony_ci			/* We don't have enough RTT samples to do the Vegas
19462306a36Sopenharmony_ci			 * calculation, so we'll behave like Reno.
19562306a36Sopenharmony_ci			 */
19662306a36Sopenharmony_ci			tcp_reno_cong_avoid(sk, ack, acked);
19762306a36Sopenharmony_ci		} else {
19862306a36Sopenharmony_ci			u32 rtt, diff;
19962306a36Sopenharmony_ci			u64 target_cwnd;
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci			/* We have enough RTT samples, so, using the Vegas
20262306a36Sopenharmony_ci			 * algorithm, we determine if we should increase or
20362306a36Sopenharmony_ci			 * decrease cwnd, and by how much.
20462306a36Sopenharmony_ci			 */
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci			/* Pluck out the RTT we are using for the Vegas
20762306a36Sopenharmony_ci			 * calculations. This is the min RTT seen during the
20862306a36Sopenharmony_ci			 * last RTT. Taking the min filters out the effects
20962306a36Sopenharmony_ci			 * of delayed ACKs, at the cost of noticing congestion
21062306a36Sopenharmony_ci			 * a bit later.
21162306a36Sopenharmony_ci			 */
21262306a36Sopenharmony_ci			rtt = vegas->minRTT;
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci			/* Calculate the cwnd we should have, if we weren't
21562306a36Sopenharmony_ci			 * going too fast.
21662306a36Sopenharmony_ci			 *
21762306a36Sopenharmony_ci			 * This is:
21862306a36Sopenharmony_ci			 *     (actual rate in segments) * baseRTT
21962306a36Sopenharmony_ci			 */
22062306a36Sopenharmony_ci			target_cwnd = (u64)tcp_snd_cwnd(tp) * vegas->baseRTT;
22162306a36Sopenharmony_ci			do_div(target_cwnd, rtt);
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci			/* Calculate the difference between the window we had,
22462306a36Sopenharmony_ci			 * and the window we would like to have. This quantity
22562306a36Sopenharmony_ci			 * is the "Diff" from the Arizona Vegas papers.
22662306a36Sopenharmony_ci			 */
22762306a36Sopenharmony_ci			diff = tcp_snd_cwnd(tp) * (rtt-vegas->baseRTT) / vegas->baseRTT;
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci			if (diff > gamma && tcp_in_slow_start(tp)) {
23062306a36Sopenharmony_ci				/* Going too fast. Time to slow down
23162306a36Sopenharmony_ci				 * and switch to congestion avoidance.
23262306a36Sopenharmony_ci				 */
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci				/* Set cwnd to match the actual rate
23562306a36Sopenharmony_ci				 * exactly:
23662306a36Sopenharmony_ci				 *   cwnd = (actual rate) * baseRTT
23762306a36Sopenharmony_ci				 * Then we add 1 because the integer
23862306a36Sopenharmony_ci				 * truncation robs us of full link
23962306a36Sopenharmony_ci				 * utilization.
24062306a36Sopenharmony_ci				 */
24162306a36Sopenharmony_ci				tcp_snd_cwnd_set(tp, min(tcp_snd_cwnd(tp),
24262306a36Sopenharmony_ci							 (u32)target_cwnd + 1));
24362306a36Sopenharmony_ci				tp->snd_ssthresh = tcp_vegas_ssthresh(tp);
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci			} else if (tcp_in_slow_start(tp)) {
24662306a36Sopenharmony_ci				/* Slow start.  */
24762306a36Sopenharmony_ci				tcp_slow_start(tp, acked);
24862306a36Sopenharmony_ci			} else {
24962306a36Sopenharmony_ci				/* Congestion avoidance. */
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci				/* Figure out where we would like cwnd
25262306a36Sopenharmony_ci				 * to be.
25362306a36Sopenharmony_ci				 */
25462306a36Sopenharmony_ci				if (diff > beta) {
25562306a36Sopenharmony_ci					/* The old window was too fast, so
25662306a36Sopenharmony_ci					 * we slow down.
25762306a36Sopenharmony_ci					 */
25862306a36Sopenharmony_ci					tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) - 1);
25962306a36Sopenharmony_ci					tp->snd_ssthresh
26062306a36Sopenharmony_ci						= tcp_vegas_ssthresh(tp);
26162306a36Sopenharmony_ci				} else if (diff < alpha) {
26262306a36Sopenharmony_ci					/* We don't have enough extra packets
26362306a36Sopenharmony_ci					 * in the network, so speed up.
26462306a36Sopenharmony_ci					 */
26562306a36Sopenharmony_ci					tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1);
26662306a36Sopenharmony_ci				} else {
26762306a36Sopenharmony_ci					/* Sending just as fast as we
26862306a36Sopenharmony_ci					 * should be.
26962306a36Sopenharmony_ci					 */
27062306a36Sopenharmony_ci				}
27162306a36Sopenharmony_ci			}
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci			if (tcp_snd_cwnd(tp) < 2)
27462306a36Sopenharmony_ci				tcp_snd_cwnd_set(tp, 2);
27562306a36Sopenharmony_ci			else if (tcp_snd_cwnd(tp) > tp->snd_cwnd_clamp)
27662306a36Sopenharmony_ci				tcp_snd_cwnd_set(tp, tp->snd_cwnd_clamp);
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci			tp->snd_ssthresh = tcp_current_ssthresh(sk);
27962306a36Sopenharmony_ci		}
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci		/* Wipe the slate clean for the next RTT. */
28262306a36Sopenharmony_ci		vegas->cntRTT = 0;
28362306a36Sopenharmony_ci		vegas->minRTT = 0x7fffffff;
28462306a36Sopenharmony_ci	}
28562306a36Sopenharmony_ci	/* Use normal slow start */
28662306a36Sopenharmony_ci	else if (tcp_in_slow_start(tp))
28762306a36Sopenharmony_ci		tcp_slow_start(tp, acked);
28862306a36Sopenharmony_ci}
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci/* Extract info for Tcp socket info provided via netlink. */
29162306a36Sopenharmony_cisize_t tcp_vegas_get_info(struct sock *sk, u32 ext, int *attr,
29262306a36Sopenharmony_ci			  union tcp_cc_info *info)
29362306a36Sopenharmony_ci{
29462306a36Sopenharmony_ci	const struct vegas *ca = inet_csk_ca(sk);
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
29762306a36Sopenharmony_ci		info->vegas.tcpv_enabled = ca->doing_vegas_now;
29862306a36Sopenharmony_ci		info->vegas.tcpv_rttcnt = ca->cntRTT;
29962306a36Sopenharmony_ci		info->vegas.tcpv_rtt = ca->baseRTT;
30062306a36Sopenharmony_ci		info->vegas.tcpv_minrtt = ca->minRTT;
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci		*attr = INET_DIAG_VEGASINFO;
30362306a36Sopenharmony_ci		return sizeof(struct tcpvegas_info);
30462306a36Sopenharmony_ci	}
30562306a36Sopenharmony_ci	return 0;
30662306a36Sopenharmony_ci}
30762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(tcp_vegas_get_info);
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_cistatic struct tcp_congestion_ops tcp_vegas __read_mostly = {
31062306a36Sopenharmony_ci	.init		= tcp_vegas_init,
31162306a36Sopenharmony_ci	.ssthresh	= tcp_reno_ssthresh,
31262306a36Sopenharmony_ci	.undo_cwnd	= tcp_reno_undo_cwnd,
31362306a36Sopenharmony_ci	.cong_avoid	= tcp_vegas_cong_avoid,
31462306a36Sopenharmony_ci	.pkts_acked	= tcp_vegas_pkts_acked,
31562306a36Sopenharmony_ci	.set_state	= tcp_vegas_state,
31662306a36Sopenharmony_ci	.cwnd_event	= tcp_vegas_cwnd_event,
31762306a36Sopenharmony_ci	.get_info	= tcp_vegas_get_info,
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	.owner		= THIS_MODULE,
32062306a36Sopenharmony_ci	.name		= "vegas",
32162306a36Sopenharmony_ci};
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_cistatic int __init tcp_vegas_register(void)
32462306a36Sopenharmony_ci{
32562306a36Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
32662306a36Sopenharmony_ci	tcp_register_congestion_control(&tcp_vegas);
32762306a36Sopenharmony_ci	return 0;
32862306a36Sopenharmony_ci}
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_cistatic void __exit tcp_vegas_unregister(void)
33162306a36Sopenharmony_ci{
33262306a36Sopenharmony_ci	tcp_unregister_congestion_control(&tcp_vegas);
33362306a36Sopenharmony_ci}
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_cimodule_init(tcp_vegas_register);
33662306a36Sopenharmony_cimodule_exit(tcp_vegas_unregister);
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ciMODULE_AUTHOR("Stephen Hemminger");
33962306a36Sopenharmony_ciMODULE_LICENSE("GPL");
34062306a36Sopenharmony_ciMODULE_DESCRIPTION("TCP Vegas");
341