1/* 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. 9 * See http://www.ntu.edu.sg/home5/ZHOU0022/papers/CPFu03a.pdf 10 */ 11 12#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. */ 72static void tcp_veno_pkts_acked(struct sock *sk, u32 cnt, ktime_t last) 73{ 74 struct veno *veno = inet_csk_ca(sk); 75 u32 vrtt; 76 77 if (ktime_equal(last, net_invalid_timestamp())) 78 return; 79 80 /* Never allow zero rtt or baseRTT */ 81 vrtt = ktime_to_us(net_timedelta(last)) + 1; 82 83 /* Filter to find propagation delay: */ 84 if (vrtt < veno->basertt) 85 veno->basertt = vrtt; 86 87 /* Find the min rtt during the last rtt to find 88 * the current prop. delay + queuing delay: 89 */ 90 veno->minrtt = min(veno->minrtt, vrtt); 91 veno->cntrtt++; 92} 93 94static void tcp_veno_state(struct sock *sk, u8 ca_state) 95{ 96 if (ca_state == TCP_CA_Open) 97 veno_enable(sk); 98 else 99 veno_disable(sk); 100} 101 102/* 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 109 * again. 110 */ 111static void tcp_veno_cwnd_event(struct sock *sk, enum tcp_ca_event event) 112{ 113 if (event == CA_EVENT_CWND_RESTART || event == CA_EVENT_TX_START) 114 tcp_veno_init(sk); 115} 116 117static void tcp_veno_cong_avoid(struct sock *sk, u32 ack, 118 u32 seq_rtt, u32 in_flight, int flag) 119{ 120 struct tcp_sock *tp = tcp_sk(sk); 121 struct veno *veno = inet_csk_ca(sk); 122 123 if (!veno->doing_veno_now) 124 return tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag); 125 126 /* limited by applications */ 127 if (!tcp_is_cwnd_limited(sk, in_flight)) 128 return; 129 130 /* We do the Veno calculations only if we got enough rtt samples */ 131 if (veno->cntrtt <= 2) { 132 /* We don't have enough rtt samples to do the Veno 133 * calculation, so we'll behave like Reno. 134 */ 135 tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag); 136 } else { 137 u32 rtt, target_cwnd; 138 139 /* We have enough rtt samples, so, using the Veno 140 * algorithm, we determine the state of the network. 141 */ 142 143 rtt = veno->minrtt; 144 145 target_cwnd = ((tp->snd_cwnd * veno->basertt) 146 << V_PARAM_SHIFT) / rtt; 147 148 veno->diff = (tp->snd_cwnd << V_PARAM_SHIFT) - target_cwnd; 149 150 if (tp->snd_cwnd <= tp->snd_ssthresh) { 151 /* Slow start. */ 152 tcp_slow_start(tp); 153 } else { 154 /* Congestion avoidance. */ 155 if (veno->diff < beta) { 156 /* In the "non-congestive state", increase cwnd 157 * every rtt. 158 */ 159 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) { 160 if (tp->snd_cwnd < tp->snd_cwnd_clamp) 161 tp->snd_cwnd++; 162 tp->snd_cwnd_cnt = 0; 163 } else 164 tp->snd_cwnd_cnt++; 165 } else { 166 /* In the "congestive state", increase cwnd 167 * every other rtt. 168 */ 169 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) { 170 if (veno->inc 171 && tp->snd_cwnd < 172 tp->snd_cwnd_clamp) { 173 tp->snd_cwnd++; 174 veno->inc = 0; 175 } else 176 veno->inc = 1; 177 tp->snd_cwnd_cnt = 0; 178 } else 179 tp->snd_cwnd_cnt++; 180 } 181 182 } 183 if (tp->snd_cwnd < 2) 184 tp->snd_cwnd = 2; 185 else if (tp->snd_cwnd > tp->snd_cwnd_clamp) 186 tp->snd_cwnd = tp->snd_cwnd_clamp; 187 } 188 /* Wipe the slate clean for the next rtt. */ 189 /* veno->cntrtt = 0; */ 190 veno->minrtt = 0x7fffffff; 191} 192 193/* Veno MD phase */ 194static u32 tcp_veno_ssthresh(struct sock *sk) 195{ 196 const struct tcp_sock *tp = tcp_sk(sk); 197 struct veno *veno = inet_csk_ca(sk); 198 199 if (veno->diff < beta) 200 /* in "non-congestive state", cut cwnd by 1/5 */ 201 return max(tp->snd_cwnd * 4 / 5, 2U); 202 else 203 /* in "congestive state", cut cwnd by 1/2 */ 204 return max(tp->snd_cwnd >> 1U, 2U); 205} 206 207static struct tcp_congestion_ops tcp_veno = { 208 .flags = TCP_CONG_RTT_STAMP, 209 .init = tcp_veno_init, 210 .ssthresh = tcp_veno_ssthresh, 211 .cong_avoid = tcp_veno_cong_avoid, 212 .pkts_acked = tcp_veno_pkts_acked, 213 .set_state = tcp_veno_state, 214 .cwnd_event = tcp_veno_cwnd_event, 215 216 .owner = THIS_MODULE, 217 .name = "veno", 218}; 219 220static int __init tcp_veno_register(void) 221{ 222 BUILD_BUG_ON(sizeof(struct veno) > ICSK_CA_PRIV_SIZE); 223 tcp_register_congestion_control(&tcp_veno); 224 return 0; 225} 226 227static void __exit tcp_veno_unregister(void) 228{ 229 tcp_unregister_congestion_control(&tcp_veno); 230} 231 232module_init(tcp_veno_register); 233module_exit(tcp_veno_unregister); 234 235MODULE_AUTHOR("Bin Zhou, Cheng Peng Fu"); 236MODULE_LICENSE("GPL"); 237MODULE_DESCRIPTION("TCP Veno"); 238