blob: 40171e163cffa723f3eb1539740a8a7c3e963f53 [file] [log] [blame]
Bin Zhou76f10172006-06-05 17:28:30 -07001/*
2 * TCP Veno congestion control
3 *
4 * This is based on the congestion detection/avoidance scheme described in
5 * C. P. Fu, S. C. Liew.
6 * "TCP Veno: TCP Enhancement for Transmission over Wireless Access Networks."
7 * IEEE Journal on Selected Areas in Communication,
8 * Feb. 2003.
Justin P. Mattock631dd1a2010-10-18 11:03:14 +02009 * See http://www.ie.cuhk.edu.hk/fileadmin/staff_upload/soung/Journal/J3.pdf
Bin Zhou76f10172006-06-05 17:28:30 -070010 */
11
Bin Zhou76f10172006-06-05 17:28:30 -070012#include <linux/mm.h>
13#include <linux/module.h>
14#include <linux/skbuff.h>
15#include <linux/inet_diag.h>
16
17#include <net/tcp.h>
18
19/* Default values of the Veno variables, in fixed-point representation
20 * with V_PARAM_SHIFT bits to the right of the binary point.
21 */
22#define V_PARAM_SHIFT 1
23static const int beta = 3 << V_PARAM_SHIFT;
24
25/* Veno variables */
26struct veno {
27 u8 doing_veno_now; /* if true, do veno for this rtt */
28 u16 cntrtt; /* # of rtts measured within last rtt */
29 u32 minrtt; /* min of rtts measured within last rtt (in usec) */
30 u32 basertt; /* the min of all Veno rtt measurements seen (in usec) */
31 u32 inc; /* decide whether to increase cwnd */
32 u32 diff; /* calculate the diff rate */
33};
34
35/* There are several situations when we must "re-start" Veno:
36 *
37 * o when a connection is established
38 * o after an RTO
39 * o after fast recovery
40 * o when we send a packet and there is no outstanding
41 * unacknowledged data (restarting an idle connection)
42 *
43 */
44static inline void veno_enable(struct sock *sk)
45{
46 struct veno *veno = inet_csk_ca(sk);
47
48 /* turn on Veno */
49 veno->doing_veno_now = 1;
50
51 veno->minrtt = 0x7fffffff;
52}
53
54static inline void veno_disable(struct sock *sk)
55{
56 struct veno *veno = inet_csk_ca(sk);
57
58 /* turn off Veno */
59 veno->doing_veno_now = 0;
60}
61
62static void tcp_veno_init(struct sock *sk)
63{
64 struct veno *veno = inet_csk_ca(sk);
65
66 veno->basertt = 0x7fffffff;
67 veno->inc = 1;
68 veno_enable(sk);
69}
70
71/* Do rtt sampling needed for Veno. */
Lawrence Brakmo756ee172016-05-11 10:02:13 -070072static void tcp_veno_pkts_acked(struct sock *sk,
73 const struct ack_sample *sample)
Bin Zhou76f10172006-06-05 17:28:30 -070074{
75 struct veno *veno = inet_csk_ca(sk);
Stephen Hemminger164891a2007-04-23 22:26:16 -070076 u32 vrtt;
77
Lawrence Brakmo756ee172016-05-11 10:02:13 -070078 if (sample->rtt_us < 0)
Ilpo Järvinenb9ce2042007-06-15 15:08:43 -070079 return;
80
Stephen Hemminger164891a2007-04-23 22:26:16 -070081 /* Never allow zero rtt or baseRTT */
Lawrence Brakmo756ee172016-05-11 10:02:13 -070082 vrtt = sample->rtt_us + 1;
Bin Zhou76f10172006-06-05 17:28:30 -070083
84 /* Filter to find propagation delay: */
85 if (vrtt < veno->basertt)
86 veno->basertt = vrtt;
87
88 /* Find the min rtt during the last rtt to find
89 * the current prop. delay + queuing delay:
90 */
91 veno->minrtt = min(veno->minrtt, vrtt);
92 veno->cntrtt++;
93}
94
95static void tcp_veno_state(struct sock *sk, u8 ca_state)
96{
97 if (ca_state == TCP_CA_Open)
98 veno_enable(sk);
99 else
100 veno_disable(sk);
101}
102
103/*
104 * If the connection is idle and we are restarting,
105 * then we don't want to do any Veno calculations
106 * until we get fresh rtt samples. So when we
107 * restart, we reset our Veno state to a clean
108 * state. After we get acks for this flight of
109 * packets, _then_ we can make Veno calculations
110 * again.
111 */
112static void tcp_veno_cwnd_event(struct sock *sk, enum tcp_ca_event event)
113{
114 if (event == CA_EVENT_CWND_RESTART || event == CA_EVENT_TX_START)
115 tcp_veno_init(sk);
116}
117
Eric Dumazet24901552014-05-02 21:18:05 -0700118static void tcp_veno_cong_avoid(struct sock *sk, u32 ack, u32 acked)
Bin Zhou76f10172006-06-05 17:28:30 -0700119{
120 struct tcp_sock *tp = tcp_sk(sk);
121 struct veno *veno = inet_csk_ca(sk);
122
Harvey Harrisonab598592008-05-01 02:47:38 -0700123 if (!veno->doing_veno_now) {
Eric Dumazet24901552014-05-02 21:18:05 -0700124 tcp_reno_cong_avoid(sk, ack, acked);
Harvey Harrisonab598592008-05-01 02:47:38 -0700125 return;
126 }
Bin Zhou76f10172006-06-05 17:28:30 -0700127
128 /* limited by applications */
Eric Dumazet24901552014-05-02 21:18:05 -0700129 if (!tcp_is_cwnd_limited(sk))
Bin Zhou76f10172006-06-05 17:28:30 -0700130 return;
131
132 /* We do the Veno calculations only if we got enough rtt samples */
133 if (veno->cntrtt <= 2) {
134 /* We don't have enough rtt samples to do the Veno
135 * calculation, so we'll behave like Reno.
136 */
Eric Dumazet24901552014-05-02 21:18:05 -0700137 tcp_reno_cong_avoid(sk, ack, acked);
Bin Zhou76f10172006-06-05 17:28:30 -0700138 } else {
Lachlan Andrew15913112008-04-30 01:04:03 -0700139 u64 target_cwnd;
140 u32 rtt;
Bin Zhou76f10172006-06-05 17:28:30 -0700141
142 /* We have enough rtt samples, so, using the Veno
143 * algorithm, we determine the state of the network.
144 */
145
146 rtt = veno->minrtt;
147
Christoph Paasch45a07692014-07-29 12:07:27 +0200148 target_cwnd = (u64)tp->snd_cwnd * veno->basertt;
Lachlan Andrew15913112008-04-30 01:04:03 -0700149 target_cwnd <<= V_PARAM_SHIFT;
150 do_div(target_cwnd, rtt);
Bin Zhou76f10172006-06-05 17:28:30 -0700151
152 veno->diff = (tp->snd_cwnd << V_PARAM_SHIFT) - target_cwnd;
153
Yuchung Cheng071d5082015-07-09 13:16:29 -0700154 if (tcp_in_slow_start(tp)) {
Bin Zhou76f10172006-06-05 17:28:30 -0700155 /* Slow start. */
Yuchung Cheng9f9843a72013-10-31 11:07:31 -0700156 tcp_slow_start(tp, acked);
Bin Zhou76f10172006-06-05 17:28:30 -0700157 } else {
158 /* Congestion avoidance. */
159 if (veno->diff < beta) {
160 /* In the "non-congestive state", increase cwnd
161 * every rtt.
162 */
Neal Cardwelle73ebb02015-01-28 20:01:35 -0500163 tcp_cong_avoid_ai(tp, tp->snd_cwnd, 1);
Bin Zhou76f10172006-06-05 17:28:30 -0700164 } else {
165 /* In the "congestive state", increase cwnd
166 * every other rtt.
167 */
168 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
Joe Perches9d4fb272009-11-23 10:41:23 -0800169 if (veno->inc &&
170 tp->snd_cwnd < tp->snd_cwnd_clamp) {
Bin Zhou76f10172006-06-05 17:28:30 -0700171 tp->snd_cwnd++;
172 veno->inc = 0;
173 } else
174 veno->inc = 1;
175 tp->snd_cwnd_cnt = 0;
176 } else
177 tp->snd_cwnd_cnt++;
178 }
Bin Zhou76f10172006-06-05 17:28:30 -0700179 }
180 if (tp->snd_cwnd < 2)
181 tp->snd_cwnd = 2;
182 else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
183 tp->snd_cwnd = tp->snd_cwnd_clamp;
184 }
185 /* Wipe the slate clean for the next rtt. */
186 /* veno->cntrtt = 0; */
187 veno->minrtt = 0x7fffffff;
188}
189
190/* Veno MD phase */
191static u32 tcp_veno_ssthresh(struct sock *sk)
192{
193 const struct tcp_sock *tp = tcp_sk(sk);
194 struct veno *veno = inet_csk_ca(sk);
195
196 if (veno->diff < beta)
197 /* in "non-congestive state", cut cwnd by 1/5 */
198 return max(tp->snd_cwnd * 4 / 5, 2U);
199 else
200 /* in "congestive state", cut cwnd by 1/2 */
201 return max(tp->snd_cwnd >> 1U, 2U);
202}
203
Stephen Hemmingera252beb2011-03-10 00:40:17 -0800204static struct tcp_congestion_ops tcp_veno __read_mostly = {
Bin Zhou76f10172006-06-05 17:28:30 -0700205 .init = tcp_veno_init,
206 .ssthresh = tcp_veno_ssthresh,
207 .cong_avoid = tcp_veno_cong_avoid,
Stephen Hemminger164891a2007-04-23 22:26:16 -0700208 .pkts_acked = tcp_veno_pkts_acked,
Bin Zhou76f10172006-06-05 17:28:30 -0700209 .set_state = tcp_veno_state,
210 .cwnd_event = tcp_veno_cwnd_event,
211
212 .owner = THIS_MODULE,
213 .name = "veno",
214};
215
216static int __init tcp_veno_register(void)
217{
Alexey Dobriyan74975d42006-08-25 17:10:33 -0700218 BUILD_BUG_ON(sizeof(struct veno) > ICSK_CA_PRIV_SIZE);
Bin Zhou76f10172006-06-05 17:28:30 -0700219 tcp_register_congestion_control(&tcp_veno);
220 return 0;
221}
222
223static void __exit tcp_veno_unregister(void)
224{
225 tcp_unregister_congestion_control(&tcp_veno);
226}
227
228module_init(tcp_veno_register);
229module_exit(tcp_veno_unregister);
230
231MODULE_AUTHOR("Bin Zhou, Cheng Peng Fu");
232MODULE_LICENSE("GPL");
233MODULE_DESCRIPTION("TCP Veno");