1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * TCP Veno congestion control 4 * 5 * This is based on the congestion detection/avoidance scheme described in 6 * C. P. Fu, S. C. Liew. 7 * "TCP Veno: TCP Enhancement for Transmission over Wireless Access Networks." 8 * IEEE Journal on Selected Areas in Communication, 9 * Feb. 2003. 10 * See https://www.ie.cuhk.edu.hk/fileadmin/staff_upload/soung/Journal/J3.pdf 11 */ 12 13 #include <linux/mm.h> 14 #include <linux/module.h> 15 #include <linux/skbuff.h> 16 #include <linux/inet_diag.h> 17 18 #include <net/tcp.h> 19 20 /* Default values of the Veno variables, in fixed-point representation 21 * with V_PARAM_SHIFT bits to the right of the binary point. 22 */ 23 #define V_PARAM_SHIFT 1 24 static const int beta = 3 << V_PARAM_SHIFT; 25 26 /* Veno variables */ 27 struct veno { 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 */ 34 }; 35 36 /* There are several situations when we must "re-start" Veno: 37 * 38 * o when a connection is established 39 * o after an RTO 40 * o after fast recovery 41 * o when we send a packet and there is no outstanding 42 * unacknowledged data (restarting an idle connection) 43 * 44 */ 45 static inline void veno_enable(struct sock *sk) 46 { 47 struct veno *veno = inet_csk_ca(sk); 48 49 /* turn on Veno */ 50 veno->doing_veno_now = 1; 51 52 veno->minrtt = 0x7fffffff; 53 } 54 55 static inline void veno_disable(struct sock *sk) 56 { 57 struct veno *veno = inet_csk_ca(sk); 58 59 /* turn off Veno */ 60 veno->doing_veno_now = 0; 61 } 62 63 static void tcp_veno_init(struct sock *sk) 64 { 65 struct veno *veno = inet_csk_ca(sk); 66 67 veno->basertt = 0x7fffffff; 68 veno->inc = 1; 69 veno_enable(sk); 70 } 71 72 /* Do rtt sampling needed for Veno. */ 73 static void tcp_veno_pkts_acked(struct sock *sk, 74 const struct ack_sample *sample) 75 { 76 struct veno *veno = inet_csk_ca(sk); 77 u32 vrtt; 78 79 if (sample->rtt_us < 0) 80 return; 81 82 /* Never allow zero rtt or baseRTT */ 83 vrtt = sample->rtt_us + 1; 84 85 /* Filter to find propagation delay: */ 86 if (vrtt < veno->basertt) 87 veno->basertt = vrtt; 88 89 /* Find the min rtt during the last rtt to find 90 * the current prop. delay + queuing delay: 91 */ 92 veno->minrtt = min(veno->minrtt, vrtt); 93 veno->cntrtt++; 94 } 95 96 static void tcp_veno_state(struct sock *sk, u8 ca_state) 97 { 98 if (ca_state == TCP_CA_Open) 99 veno_enable(sk); 100 else 101 veno_disable(sk); 102 } 103 104 /* 105 * If the connection is idle and we are restarting, 106 * then we don't want to do any Veno calculations 107 * until we get fresh rtt samples. So when we 108 * restart, we reset our Veno state to a clean 109 * state. After we get acks for this flight of 110 * packets, _then_ we can make Veno calculations 111 * again. 112 */ 113 static void tcp_veno_cwnd_event(struct sock *sk, enum tcp_ca_event event) 114 { 115 if (event == CA_EVENT_CWND_RESTART) 116 tcp_veno_init(sk); 117 } 118 119 static void tcp_veno_cwnd_event_tx_start(struct sock *sk) 120 { 121 tcp_veno_init(sk); 122 } 123 124 static void tcp_veno_cong_avoid(struct sock *sk, u32 ack, u32 acked) 125 { 126 struct tcp_sock *tp = tcp_sk(sk); 127 struct veno *veno = inet_csk_ca(sk); 128 129 if (!veno->doing_veno_now) { 130 tcp_reno_cong_avoid(sk, ack, acked); 131 return; 132 } 133 134 /* limited by applications */ 135 if (!tcp_is_cwnd_limited(sk)) 136 return; 137 138 /* We do the Veno calculations only if we got enough rtt samples */ 139 if (veno->cntrtt <= 2) { 140 /* We don't have enough rtt samples to do the Veno 141 * calculation, so we'll behave like Reno. 142 */ 143 tcp_reno_cong_avoid(sk, ack, acked); 144 } else { 145 u64 target_cwnd; 146 u32 rtt; 147 148 /* We have enough rtt samples, so, using the Veno 149 * algorithm, we determine the state of the network. 150 */ 151 152 rtt = veno->minrtt; 153 154 target_cwnd = (u64)tcp_snd_cwnd(tp) * veno->basertt; 155 target_cwnd <<= V_PARAM_SHIFT; 156 do_div(target_cwnd, rtt); 157 158 veno->diff = (tcp_snd_cwnd(tp) << V_PARAM_SHIFT) - target_cwnd; 159 160 if (tcp_in_slow_start(tp)) { 161 /* Slow start. */ 162 acked = tcp_slow_start(tp, acked); 163 if (!acked) 164 goto done; 165 } 166 167 /* Congestion avoidance. */ 168 if (veno->diff < beta) { 169 /* In the "non-congestive state", increase cwnd 170 * every rtt. 171 */ 172 tcp_cong_avoid_ai(tp, tcp_snd_cwnd(tp), acked); 173 } else { 174 /* In the "congestive state", increase cwnd 175 * every other rtt. 176 */ 177 if (tp->snd_cwnd_cnt >= tcp_snd_cwnd(tp)) { 178 if (veno->inc && 179 tcp_snd_cwnd(tp) < tp->snd_cwnd_clamp) { 180 tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1); 181 veno->inc = 0; 182 } else 183 veno->inc = 1; 184 tp->snd_cwnd_cnt = 0; 185 } else 186 tp->snd_cwnd_cnt += acked; 187 } 188 done: 189 if (tcp_snd_cwnd(tp) < 2) 190 tcp_snd_cwnd_set(tp, 2); 191 else if (tcp_snd_cwnd(tp) > tp->snd_cwnd_clamp) 192 tcp_snd_cwnd_set(tp, tp->snd_cwnd_clamp); 193 } 194 /* Wipe the slate clean for the next rtt. */ 195 /* veno->cntrtt = 0; */ 196 veno->minrtt = 0x7fffffff; 197 } 198 199 /* Veno MD phase */ 200 static u32 tcp_veno_ssthresh(struct sock *sk) 201 { 202 const struct tcp_sock *tp = tcp_sk(sk); 203 struct veno *veno = inet_csk_ca(sk); 204 205 if (veno->diff < beta) 206 /* in "non-congestive state", cut cwnd by 1/5 */ 207 return max(tcp_snd_cwnd(tp) * 4 / 5, 2U); 208 else 209 /* in "congestive state", cut cwnd by 1/2 */ 210 return max(tcp_snd_cwnd(tp) >> 1U, 2U); 211 } 212 213 static struct tcp_congestion_ops tcp_veno __read_mostly = { 214 .init = tcp_veno_init, 215 .ssthresh = tcp_veno_ssthresh, 216 .undo_cwnd = tcp_reno_undo_cwnd, 217 .cong_avoid = tcp_veno_cong_avoid, 218 .pkts_acked = tcp_veno_pkts_acked, 219 .set_state = tcp_veno_state, 220 .cwnd_event = tcp_veno_cwnd_event, 221 .cwnd_event_tx_start = tcp_veno_cwnd_event_tx_start, 222 223 .owner = THIS_MODULE, 224 .name = "veno", 225 }; 226 227 static int __init tcp_veno_register(void) 228 { 229 BUILD_BUG_ON(sizeof(struct veno) > ICSK_CA_PRIV_SIZE); 230 tcp_register_congestion_control(&tcp_veno); 231 return 0; 232 } 233 234 static void __exit tcp_veno_unregister(void) 235 { 236 tcp_unregister_congestion_control(&tcp_veno); 237 } 238 239 module_init(tcp_veno_register); 240 module_exit(tcp_veno_unregister); 241 242 MODULE_AUTHOR("Bin Zhou, Cheng Peng Fu"); 243 MODULE_LICENSE("GPL"); 244 MODULE_DESCRIPTION("TCP Veno"); 245