2 * TCP Veno congestion control
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,
9 * See http://www.ntu.edu.sg/home5/ZHOU0022/papers/CPFu03a.pdf
12 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/skbuff.h>
16 #include <linux/inet_diag.h>
20 /* Default values of the Veno variables, in fixed-point representation
21 * with V_PARAM_SHIFT bits to the right of the binary point.
23 #define V_PARAM_SHIFT 1
24 static const int beta = 3 << V_PARAM_SHIFT;
28 u8 doing_veno_now; /* if true, do veno for this rtt */
29 u16 cntrtt; /* # of rtts measured within last rtt */
30 u32 minrtt; /* min of rtts measured within last rtt (in usec) */
31 u32 basertt; /* the min of all Veno rtt measurements seen (in usec) */
32 u32 inc; /* decide whether to increase cwnd */
33 u32 diff; /* calculate the diff rate */
36 /* There are several situations when we must "re-start" Veno:
38 * o when a connection is established
40 * o after fast recovery
41 * o when we send a packet and there is no outstanding
42 * unacknowledged data (restarting an idle connection)
45 static inline void veno_enable(struct sock *sk)
47 struct veno *veno = inet_csk_ca(sk);
50 veno->doing_veno_now = 1;
52 veno->minrtt = 0x7fffffff;
55 static inline void veno_disable(struct sock *sk)
57 struct veno *veno = inet_csk_ca(sk);
60 veno->doing_veno_now = 0;
63 static void tcp_veno_init(struct sock *sk)
65 struct veno *veno = inet_csk_ca(sk);
67 veno->basertt = 0x7fffffff;
72 /* Do rtt sampling needed for Veno. */
73 static void tcp_veno_rtt_calc(struct sock *sk, u32 usrtt)
75 struct veno *veno = inet_csk_ca(sk);
76 u32 vrtt = usrtt + 1; /* Never allow zero rtt or basertt */
78 /* Filter to find propagation delay: */
79 if (vrtt < veno->basertt)
82 /* Find the min rtt during the last rtt to find
83 * the current prop. delay + queuing delay:
85 veno->minrtt = min(veno->minrtt, vrtt);
89 static void tcp_veno_state(struct sock *sk, u8 ca_state)
91 if (ca_state == TCP_CA_Open)
98 * If the connection is idle and we are restarting,
99 * then we don't want to do any Veno calculations
100 * until we get fresh rtt samples. So when we
101 * restart, we reset our Veno state to a clean
102 * state. After we get acks for this flight of
103 * packets, _then_ we can make Veno calculations
106 static void tcp_veno_cwnd_event(struct sock *sk, enum tcp_ca_event event)
108 if (event == CA_EVENT_CWND_RESTART || event == CA_EVENT_TX_START)
112 static void tcp_veno_cong_avoid(struct sock *sk, u32 ack,
113 u32 seq_rtt, u32 in_flight, int flag)
115 struct tcp_sock *tp = tcp_sk(sk);
116 struct veno *veno = inet_csk_ca(sk);
118 if (!veno->doing_veno_now)
119 return tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
121 /* limited by applications */
122 if (!tcp_is_cwnd_limited(sk, in_flight))
125 /* We do the Veno calculations only if we got enough rtt samples */
126 if (veno->cntrtt <= 2) {
127 /* We don't have enough rtt samples to do the Veno
128 * calculation, so we'll behave like Reno.
130 tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
132 u32 rtt, target_cwnd;
134 /* We have enough rtt samples, so, using the Veno
135 * algorithm, we determine the state of the network.
140 target_cwnd = ((tp->snd_cwnd * veno->basertt)
141 << V_PARAM_SHIFT) / rtt;
143 veno->diff = (tp->snd_cwnd << V_PARAM_SHIFT) - target_cwnd;
145 if (tp->snd_cwnd <= tp->snd_ssthresh) {
149 /* Congestion avoidance. */
150 if (veno->diff < beta) {
151 /* In the "non-congestive state", increase cwnd
154 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
155 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
157 tp->snd_cwnd_cnt = 0;
161 /* In the "congestive state", increase cwnd
164 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
167 tp->snd_cwnd_clamp) {
172 tp->snd_cwnd_cnt = 0;
178 if (tp->snd_cwnd < 2)
180 else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
181 tp->snd_cwnd = tp->snd_cwnd_clamp;
183 /* Wipe the slate clean for the next rtt. */
184 /* veno->cntrtt = 0; */
185 veno->minrtt = 0x7fffffff;
189 static u32 tcp_veno_ssthresh(struct sock *sk)
191 const struct tcp_sock *tp = tcp_sk(sk);
192 struct veno *veno = inet_csk_ca(sk);
194 if (veno->diff < beta)
195 /* in "non-congestive state", cut cwnd by 1/5 */
196 return max(tp->snd_cwnd * 4 / 5, 2U);
198 /* in "congestive state", cut cwnd by 1/2 */
199 return max(tp->snd_cwnd >> 1U, 2U);
202 static struct tcp_congestion_ops tcp_veno = {
203 .init = tcp_veno_init,
204 .ssthresh = tcp_veno_ssthresh,
205 .cong_avoid = tcp_veno_cong_avoid,
206 .rtt_sample = tcp_veno_rtt_calc,
207 .set_state = tcp_veno_state,
208 .cwnd_event = tcp_veno_cwnd_event,
210 .owner = THIS_MODULE,
214 static int __init tcp_veno_register(void)
216 BUG_ON(sizeof(struct veno) > ICSK_CA_PRIV_SIZE);
217 tcp_register_congestion_control(&tcp_veno);
221 static void __exit tcp_veno_unregister(void)
223 tcp_unregister_congestion_control(&tcp_veno);
226 module_init(tcp_veno_register);
227 module_exit(tcp_veno_unregister);
229 MODULE_AUTHOR("Bin Zhou, Cheng Peng Fu");
230 MODULE_LICENSE("GPL");
231 MODULE_DESCRIPTION("TCP Veno");