xref: /linux/net/ipv4/tcp_veno.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
276f10177SBin Zhou /*
376f10177SBin Zhou  * TCP Veno congestion control
476f10177SBin Zhou  *
576f10177SBin Zhou  * This is based on the congestion detection/avoidance scheme described in
676f10177SBin Zhou  *    C. P. Fu, S. C. Liew.
776f10177SBin Zhou  *    "TCP Veno: TCP Enhancement for Transmission over Wireless Access Networks."
876f10177SBin Zhou  *    IEEE Journal on Selected Areas in Communication,
976f10177SBin Zhou  *    Feb. 2003.
107a6498ebSAlexander A. Klimov  * 	See https://www.ie.cuhk.edu.hk/fileadmin/staff_upload/soung/Journal/J3.pdf
1176f10177SBin Zhou  */
1276f10177SBin Zhou 
1376f10177SBin Zhou #include <linux/mm.h>
1476f10177SBin Zhou #include <linux/module.h>
1576f10177SBin Zhou #include <linux/skbuff.h>
1676f10177SBin Zhou #include <linux/inet_diag.h>
1776f10177SBin Zhou 
1876f10177SBin Zhou #include <net/tcp.h>
1976f10177SBin Zhou 
2076f10177SBin Zhou /* Default values of the Veno variables, in fixed-point representation
2176f10177SBin Zhou  * with V_PARAM_SHIFT bits to the right of the binary point.
2276f10177SBin Zhou  */
2376f10177SBin Zhou #define V_PARAM_SHIFT 1
2476f10177SBin Zhou static const int beta = 3 << V_PARAM_SHIFT;
2576f10177SBin Zhou 
2676f10177SBin Zhou /* Veno variables */
2776f10177SBin Zhou struct veno {
2876f10177SBin Zhou 	u8 doing_veno_now;	/* if true, do veno for this rtt */
2976f10177SBin Zhou 	u16 cntrtt;		/* # of rtts measured within last rtt */
3076f10177SBin Zhou 	u32 minrtt;		/* min of rtts measured within last rtt (in usec) */
3176f10177SBin Zhou 	u32 basertt;		/* the min of all Veno rtt measurements seen (in usec) */
3276f10177SBin Zhou 	u32 inc;		/* decide whether to increase cwnd */
3376f10177SBin Zhou 	u32 diff;		/* calculate the diff rate */
3476f10177SBin Zhou };
3576f10177SBin Zhou 
3676f10177SBin Zhou /* There are several situations when we must "re-start" Veno:
3776f10177SBin Zhou  *
3876f10177SBin Zhou  *  o when a connection is established
3976f10177SBin Zhou  *  o after an RTO
4076f10177SBin Zhou  *  o after fast recovery
4176f10177SBin Zhou  *  o when we send a packet and there is no outstanding
4276f10177SBin Zhou  *    unacknowledged data (restarting an idle connection)
4376f10177SBin Zhou  *
4476f10177SBin Zhou  */
veno_enable(struct sock * sk)4576f10177SBin Zhou static inline void veno_enable(struct sock *sk)
4676f10177SBin Zhou {
4776f10177SBin Zhou 	struct veno *veno = inet_csk_ca(sk);
4876f10177SBin Zhou 
4976f10177SBin Zhou 	/* turn on Veno */
5076f10177SBin Zhou 	veno->doing_veno_now = 1;
5176f10177SBin Zhou 
5276f10177SBin Zhou 	veno->minrtt = 0x7fffffff;
5376f10177SBin Zhou }
5476f10177SBin Zhou 
veno_disable(struct sock * sk)5576f10177SBin Zhou static inline void veno_disable(struct sock *sk)
5676f10177SBin Zhou {
5776f10177SBin Zhou 	struct veno *veno = inet_csk_ca(sk);
5876f10177SBin Zhou 
5976f10177SBin Zhou 	/* turn off Veno */
6076f10177SBin Zhou 	veno->doing_veno_now = 0;
6176f10177SBin Zhou }
6276f10177SBin Zhou 
tcp_veno_init(struct sock * sk)6376f10177SBin Zhou static void tcp_veno_init(struct sock *sk)
6476f10177SBin Zhou {
6576f10177SBin Zhou 	struct veno *veno = inet_csk_ca(sk);
6676f10177SBin Zhou 
6776f10177SBin Zhou 	veno->basertt = 0x7fffffff;
6876f10177SBin Zhou 	veno->inc = 1;
6976f10177SBin Zhou 	veno_enable(sk);
7076f10177SBin Zhou }
7176f10177SBin Zhou 
7276f10177SBin Zhou /* Do rtt sampling needed for Veno. */
tcp_veno_pkts_acked(struct sock * sk,const struct ack_sample * sample)73756ee172SLawrence Brakmo static void tcp_veno_pkts_acked(struct sock *sk,
74756ee172SLawrence Brakmo 				const struct ack_sample *sample)
7576f10177SBin Zhou {
7676f10177SBin Zhou 	struct veno *veno = inet_csk_ca(sk);
77164891aaSStephen Hemminger 	u32 vrtt;
78164891aaSStephen Hemminger 
79756ee172SLawrence Brakmo 	if (sample->rtt_us < 0)
80b9ce204fSIlpo Järvinen 		return;
81b9ce204fSIlpo Järvinen 
82164891aaSStephen Hemminger 	/* Never allow zero rtt or baseRTT */
83756ee172SLawrence Brakmo 	vrtt = sample->rtt_us + 1;
8476f10177SBin Zhou 
8576f10177SBin Zhou 	/* Filter to find propagation delay: */
8676f10177SBin Zhou 	if (vrtt < veno->basertt)
8776f10177SBin Zhou 		veno->basertt = vrtt;
8876f10177SBin Zhou 
8976f10177SBin Zhou 	/* Find the min rtt during the last rtt to find
9076f10177SBin Zhou 	 * the current prop. delay + queuing delay:
9176f10177SBin Zhou 	 */
9276f10177SBin Zhou 	veno->minrtt = min(veno->minrtt, vrtt);
9376f10177SBin Zhou 	veno->cntrtt++;
9476f10177SBin Zhou }
9576f10177SBin Zhou 
tcp_veno_state(struct sock * sk,u8 ca_state)9676f10177SBin Zhou static void tcp_veno_state(struct sock *sk, u8 ca_state)
9776f10177SBin Zhou {
9876f10177SBin Zhou 	if (ca_state == TCP_CA_Open)
9976f10177SBin Zhou 		veno_enable(sk);
10076f10177SBin Zhou 	else
10176f10177SBin Zhou 		veno_disable(sk);
10276f10177SBin Zhou }
10376f10177SBin Zhou 
10476f10177SBin Zhou /*
10576f10177SBin Zhou  * If the connection is idle and we are restarting,
10676f10177SBin Zhou  * then we don't want to do any Veno calculations
10776f10177SBin Zhou  * until we get fresh rtt samples.  So when we
10876f10177SBin Zhou  * restart, we reset our Veno state to a clean
10976f10177SBin Zhou  * state. After we get acks for this flight of
11076f10177SBin Zhou  * packets, _then_ we can make Veno calculations
11176f10177SBin Zhou  * again.
11276f10177SBin Zhou  */
tcp_veno_cwnd_event(struct sock * sk,enum tcp_ca_event event)11376f10177SBin Zhou static void tcp_veno_cwnd_event(struct sock *sk, enum tcp_ca_event event)
11476f10177SBin Zhou {
11576f10177SBin Zhou 	if (event == CA_EVENT_CWND_RESTART || event == CA_EVENT_TX_START)
11676f10177SBin Zhou 		tcp_veno_init(sk);
11776f10177SBin Zhou }
11876f10177SBin Zhou 
tcp_veno_cong_avoid(struct sock * sk,u32 ack,u32 acked)11924901551SEric Dumazet static void tcp_veno_cong_avoid(struct sock *sk, u32 ack, u32 acked)
12076f10177SBin Zhou {
12176f10177SBin Zhou 	struct tcp_sock *tp = tcp_sk(sk);
12276f10177SBin Zhou 	struct veno *veno = inet_csk_ca(sk);
12376f10177SBin Zhou 
124ab59859dSHarvey Harrison 	if (!veno->doing_veno_now) {
12524901551SEric Dumazet 		tcp_reno_cong_avoid(sk, ack, acked);
126ab59859dSHarvey Harrison 		return;
127ab59859dSHarvey Harrison 	}
12876f10177SBin Zhou 
12976f10177SBin Zhou 	/* limited by applications */
13024901551SEric Dumazet 	if (!tcp_is_cwnd_limited(sk))
13176f10177SBin Zhou 		return;
13276f10177SBin Zhou 
13376f10177SBin Zhou 	/* We do the Veno calculations only if we got enough rtt samples */
13476f10177SBin Zhou 	if (veno->cntrtt <= 2) {
13576f10177SBin Zhou 		/* We don't have enough rtt samples to do the Veno
13676f10177SBin Zhou 		 * calculation, so we'll behave like Reno.
13776f10177SBin Zhou 		 */
13824901551SEric Dumazet 		tcp_reno_cong_avoid(sk, ack, acked);
13976f10177SBin Zhou 	} else {
14015913114SLachlan Andrew 		u64 target_cwnd;
14115913114SLachlan Andrew 		u32 rtt;
14276f10177SBin Zhou 
14376f10177SBin Zhou 		/* We have enough rtt samples, so, using the Veno
14476f10177SBin Zhou 		 * algorithm, we determine the state of the network.
14576f10177SBin Zhou 		 */
14676f10177SBin Zhou 
14776f10177SBin Zhou 		rtt = veno->minrtt;
14876f10177SBin Zhou 
149*40570375SEric Dumazet 		target_cwnd = (u64)tcp_snd_cwnd(tp) * veno->basertt;
15015913114SLachlan Andrew 		target_cwnd <<= V_PARAM_SHIFT;
15115913114SLachlan Andrew 		do_div(target_cwnd, rtt);
15276f10177SBin Zhou 
153*40570375SEric Dumazet 		veno->diff = (tcp_snd_cwnd(tp) << V_PARAM_SHIFT) - target_cwnd;
15476f10177SBin Zhou 
155071d5080SYuchung Cheng 		if (tcp_in_slow_start(tp)) {
15676f10177SBin Zhou 			/* Slow start. */
157ca04f5d4SPengcheng Yang 			acked = tcp_slow_start(tp, acked);
158ca04f5d4SPengcheng Yang 			if (!acked)
159d861b5c7SPengcheng Yang 				goto done;
160d861b5c7SPengcheng Yang 		}
161d861b5c7SPengcheng Yang 
16276f10177SBin Zhou 		/* Congestion avoidance. */
16376f10177SBin Zhou 		if (veno->diff < beta) {
16476f10177SBin Zhou 			/* In the "non-congestive state", increase cwnd
16576f10177SBin Zhou 			 * every rtt.
16676f10177SBin Zhou 			 */
167*40570375SEric Dumazet 			tcp_cong_avoid_ai(tp, tcp_snd_cwnd(tp), acked);
16876f10177SBin Zhou 		} else {
16976f10177SBin Zhou 			/* In the "congestive state", increase cwnd
17076f10177SBin Zhou 			 * every other rtt.
17176f10177SBin Zhou 			 */
172*40570375SEric Dumazet 			if (tp->snd_cwnd_cnt >= tcp_snd_cwnd(tp)) {
1739d4fb27dSJoe Perches 				if (veno->inc &&
174*40570375SEric Dumazet 				    tcp_snd_cwnd(tp) < tp->snd_cwnd_clamp) {
175*40570375SEric Dumazet 					tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1);
17676f10177SBin Zhou 					veno->inc = 0;
17776f10177SBin Zhou 				} else
17876f10177SBin Zhou 					veno->inc = 1;
17976f10177SBin Zhou 				tp->snd_cwnd_cnt = 0;
18076f10177SBin Zhou 			} else
181ca04f5d4SPengcheng Yang 				tp->snd_cwnd_cnt += acked;
18276f10177SBin Zhou 		}
183d861b5c7SPengcheng Yang done:
184*40570375SEric Dumazet 		if (tcp_snd_cwnd(tp) < 2)
185*40570375SEric Dumazet 			tcp_snd_cwnd_set(tp, 2);
186*40570375SEric Dumazet 		else if (tcp_snd_cwnd(tp) > tp->snd_cwnd_clamp)
187*40570375SEric Dumazet 			tcp_snd_cwnd_set(tp, tp->snd_cwnd_clamp);
18876f10177SBin Zhou 	}
18976f10177SBin Zhou 	/* Wipe the slate clean for the next rtt. */
19076f10177SBin Zhou 	/* veno->cntrtt = 0; */
19176f10177SBin Zhou 	veno->minrtt = 0x7fffffff;
19276f10177SBin Zhou }
19376f10177SBin Zhou 
19476f10177SBin Zhou /* Veno MD phase */
tcp_veno_ssthresh(struct sock * sk)19576f10177SBin Zhou static u32 tcp_veno_ssthresh(struct sock *sk)
19676f10177SBin Zhou {
19776f10177SBin Zhou 	const struct tcp_sock *tp = tcp_sk(sk);
19876f10177SBin Zhou 	struct veno *veno = inet_csk_ca(sk);
19976f10177SBin Zhou 
20076f10177SBin Zhou 	if (veno->diff < beta)
20176f10177SBin Zhou 		/* in "non-congestive state", cut cwnd by 1/5 */
202*40570375SEric Dumazet 		return max(tcp_snd_cwnd(tp) * 4 / 5, 2U);
20376f10177SBin Zhou 	else
20476f10177SBin Zhou 		/* in "congestive state", cut cwnd by 1/2 */
205*40570375SEric Dumazet 		return max(tcp_snd_cwnd(tp) >> 1U, 2U);
20676f10177SBin Zhou }
20776f10177SBin Zhou 
208a252bebeSStephen Hemminger static struct tcp_congestion_ops tcp_veno __read_mostly = {
20976f10177SBin Zhou 	.init		= tcp_veno_init,
21076f10177SBin Zhou 	.ssthresh	= tcp_veno_ssthresh,
211f1722a1bSYuchung Cheng 	.undo_cwnd	= tcp_reno_undo_cwnd,
21276f10177SBin Zhou 	.cong_avoid	= tcp_veno_cong_avoid,
213164891aaSStephen Hemminger 	.pkts_acked	= tcp_veno_pkts_acked,
21476f10177SBin Zhou 	.set_state	= tcp_veno_state,
21576f10177SBin Zhou 	.cwnd_event	= tcp_veno_cwnd_event,
21676f10177SBin Zhou 
21776f10177SBin Zhou 	.owner		= THIS_MODULE,
21876f10177SBin Zhou 	.name		= "veno",
21976f10177SBin Zhou };
22076f10177SBin Zhou 
tcp_veno_register(void)22176f10177SBin Zhou static int __init tcp_veno_register(void)
22276f10177SBin Zhou {
22374975d40SAlexey Dobriyan 	BUILD_BUG_ON(sizeof(struct veno) > ICSK_CA_PRIV_SIZE);
22476f10177SBin Zhou 	tcp_register_congestion_control(&tcp_veno);
22576f10177SBin Zhou 	return 0;
22676f10177SBin Zhou }
22776f10177SBin Zhou 
tcp_veno_unregister(void)22876f10177SBin Zhou static void __exit tcp_veno_unregister(void)
22976f10177SBin Zhou {
23076f10177SBin Zhou 	tcp_unregister_congestion_control(&tcp_veno);
23176f10177SBin Zhou }
23276f10177SBin Zhou 
23376f10177SBin Zhou module_init(tcp_veno_register);
23476f10177SBin Zhou module_exit(tcp_veno_unregister);
23576f10177SBin Zhou 
23676f10177SBin Zhou MODULE_AUTHOR("Bin Zhou, Cheng Peng Fu");
23776f10177SBin Zhou MODULE_LICENSE("GPL");
23876f10177SBin Zhou MODULE_DESCRIPTION("TCP Veno");
239