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
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/inet_diag.h>
19 /* Default values of the Veno variables, in fixed-point representation
20 * with V_PARAM_SHIFT bits to the right of the binary point.
22 #define V_PARAM_SHIFT 1
23 static const int beta = 3 << V_PARAM_SHIFT;
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 */
35 /* There are several situations when we must "re-start" Veno:
37 * o when a connection is established
39 * o after fast recovery
40 * o when we send a packet and there is no outstanding
41 * unacknowledged data (restarting an idle connection)
44 static inline void veno_enable(struct sock *sk)
46 struct veno *veno = inet_csk_ca(sk);
49 veno->doing_veno_now = 1;
51 veno->minrtt = 0x7fffffff;
54 static inline void veno_disable(struct sock *sk)
56 struct veno *veno = inet_csk_ca(sk);
59 veno->doing_veno_now = 0;
62 static void tcp_veno_init(struct sock *sk)
64 struct veno *veno = inet_csk_ca(sk);
66 veno->basertt = 0x7fffffff;
71 /* Do rtt sampling needed for Veno. */
72 static void tcp_veno_pkts_acked(struct sock *sk, u32 cnt, s32 rtt_us)
74 struct veno *veno = inet_csk_ca(sk);
80 /* Never allow zero rtt or baseRTT */
83 /* Filter to find propagation delay: */
84 if (vrtt < veno->basertt)
87 /* Find the min rtt during the last rtt to find
88 * the current prop. delay + queuing delay:
90 veno->minrtt = min(veno->minrtt, vrtt);
94 static void tcp_veno_state(struct sock *sk, u8 ca_state)
96 if (ca_state == TCP_CA_Open)
103 * If the connection is idle and we are restarting,
104 * then we don't want to do any Veno calculations
105 * until we get fresh rtt samples. So when we
106 * restart, we reset our Veno state to a clean
107 * state. After we get acks for this flight of
108 * packets, _then_ we can make Veno calculations
111 static void tcp_veno_cwnd_event(struct sock *sk, enum tcp_ca_event event)
113 if (event == CA_EVENT_CWND_RESTART || event == CA_EVENT_TX_START)
117 static void tcp_veno_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
119 struct tcp_sock *tp = tcp_sk(sk);
120 struct veno *veno = inet_csk_ca(sk);
122 if (!veno->doing_veno_now)
123 return tcp_reno_cong_avoid(sk, ack, in_flight);
125 /* limited by applications */
126 if (!tcp_is_cwnd_limited(sk, in_flight))
129 /* We do the Veno calculations only if we got enough rtt samples */
130 if (veno->cntrtt <= 2) {
131 /* We don't have enough rtt samples to do the Veno
132 * calculation, so we'll behave like Reno.
134 tcp_reno_cong_avoid(sk, ack, in_flight);
136 u32 rtt, target_cwnd;
138 /* We have enough rtt samples, so, using the Veno
139 * algorithm, we determine the state of the network.
144 target_cwnd = ((tp->snd_cwnd * veno->basertt)
145 << V_PARAM_SHIFT) / rtt;
147 veno->diff = (tp->snd_cwnd << V_PARAM_SHIFT) - target_cwnd;
149 if (tp->snd_cwnd <= tp->snd_ssthresh) {
153 /* Congestion avoidance. */
154 if (veno->diff < beta) {
155 /* In the "non-congestive state", increase cwnd
158 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
159 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
161 tp->snd_cwnd_cnt = 0;
165 /* In the "congestive state", increase cwnd
168 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
171 tp->snd_cwnd_clamp) {
176 tp->snd_cwnd_cnt = 0;
182 if (tp->snd_cwnd < 2)
184 else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
185 tp->snd_cwnd = tp->snd_cwnd_clamp;
187 /* Wipe the slate clean for the next rtt. */
188 /* veno->cntrtt = 0; */
189 veno->minrtt = 0x7fffffff;
193 static u32 tcp_veno_ssthresh(struct sock *sk)
195 const struct tcp_sock *tp = tcp_sk(sk);
196 struct veno *veno = inet_csk_ca(sk);
198 if (veno->diff < beta)
199 /* in "non-congestive state", cut cwnd by 1/5 */
200 return max(tp->snd_cwnd * 4 / 5, 2U);
202 /* in "congestive state", cut cwnd by 1/2 */
203 return max(tp->snd_cwnd >> 1U, 2U);
206 static struct tcp_congestion_ops tcp_veno = {
207 .flags = TCP_CONG_RTT_STAMP,
208 .init = tcp_veno_init,
209 .ssthresh = tcp_veno_ssthresh,
210 .cong_avoid = tcp_veno_cong_avoid,
211 .pkts_acked = tcp_veno_pkts_acked,
212 .set_state = tcp_veno_state,
213 .cwnd_event = tcp_veno_cwnd_event,
215 .owner = THIS_MODULE,
219 static int __init tcp_veno_register(void)
221 BUILD_BUG_ON(sizeof(struct veno) > ICSK_CA_PRIV_SIZE);
222 tcp_register_congestion_control(&tcp_veno);
226 static void __exit tcp_veno_unregister(void)
228 tcp_unregister_congestion_control(&tcp_veno);
231 module_init(tcp_veno_register);
232 module_exit(tcp_veno_unregister);
234 MODULE_AUTHOR("Bin Zhou, Cheng Peng Fu");
235 MODULE_LICENSE("GPL");
236 MODULE_DESCRIPTION("TCP Veno");