Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * TCP Illinois congestion control. |
| 3 | * Home page: |
| 4 | * http://www.ews.uiuc.edu/~shaoliu/tcpillinois/index.html |
| 5 | * |
| 6 | * The algorithm is described in: |
| 7 | * "TCP-Illinois: A Loss and Delay-Based Congestion Control Algorithm |
| 8 | * for High-Speed Networks" |
| 9 | * http://www.ews.uiuc.edu/~shaoliu/papersandslides/liubassri06perf.pdf |
| 10 | * |
| 11 | * Implemented from description in paper and ns-2 simulation. |
| 12 | * Copyright (C) 2007 Stephen Hemminger <shemminger@linux-foundation.org> |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/skbuff.h> |
| 17 | #include <linux/inet_diag.h> |
| 18 | #include <asm/div64.h> |
| 19 | #include <net/tcp.h> |
| 20 | |
| 21 | #define ALPHA_SHIFT 7 |
| 22 | #define ALPHA_SCALE (1u<<ALPHA_SHIFT) |
| 23 | #define ALPHA_MIN ((3*ALPHA_SCALE)/10) /* ~0.3 */ |
| 24 | #define ALPHA_MAX (10*ALPHA_SCALE) /* 10.0 */ |
| 25 | #define ALPHA_BASE ALPHA_SCALE /* 1.0 */ |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 26 | #define U32_MAX ((u32)~0U) |
| 27 | #define RTT_MAX (U32_MAX / ALPHA_MAX) /* 3.3 secs */ |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 28 | |
| 29 | #define BETA_SHIFT 6 |
| 30 | #define BETA_SCALE (1u<<BETA_SHIFT) |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 31 | #define BETA_MIN (BETA_SCALE/8) /* 0.125 */ |
| 32 | #define BETA_MAX (BETA_SCALE/2) /* 0.5 */ |
| 33 | #define BETA_BASE BETA_MAX |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 34 | |
| 35 | static int win_thresh __read_mostly = 15; |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 36 | module_param(win_thresh, int, 0); |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 37 | MODULE_PARM_DESC(win_thresh, "Window threshold for starting adaptive sizing"); |
| 38 | |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 39 | static int theta __read_mostly = 5; |
| 40 | module_param(theta, int, 0); |
| 41 | MODULE_PARM_DESC(theta, "# of fast RTT's before full growth"); |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 42 | |
| 43 | /* TCP Illinois Parameters */ |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 44 | struct illinois { |
| 45 | u64 sum_rtt; /* sum of rtt's measured within last rtt */ |
| 46 | u16 cnt_rtt; /* # of rtts measured within last rtt */ |
| 47 | u32 base_rtt; /* min of all rtt in usec */ |
| 48 | u32 max_rtt; /* max of all rtt in usec */ |
| 49 | u32 end_seq; /* right edge of current RTT */ |
| 50 | u32 alpha; /* Additive increase */ |
| 51 | u32 beta; /* Muliplicative decrease */ |
| 52 | u16 acked; /* # packets acked by current ACK */ |
| 53 | u8 rtt_above; /* average rtt has gone above threshold */ |
| 54 | u8 rtt_low; /* # of rtts measurements below threshold */ |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 57 | static void rtt_reset(struct sock *sk) |
| 58 | { |
| 59 | struct tcp_sock *tp = tcp_sk(sk); |
| 60 | struct illinois *ca = inet_csk_ca(sk); |
| 61 | |
| 62 | ca->end_seq = tp->snd_nxt; |
| 63 | ca->cnt_rtt = 0; |
| 64 | ca->sum_rtt = 0; |
| 65 | |
| 66 | /* TODO: age max_rtt? */ |
| 67 | } |
| 68 | |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 69 | static void tcp_illinois_init(struct sock *sk) |
| 70 | { |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 71 | struct illinois *ca = inet_csk_ca(sk); |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 72 | |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 73 | ca->alpha = ALPHA_MAX; |
| 74 | ca->beta = BETA_BASE; |
| 75 | ca->base_rtt = 0x7fffffff; |
| 76 | ca->max_rtt = 0; |
| 77 | |
| 78 | ca->acked = 0; |
| 79 | ca->rtt_low = 0; |
| 80 | ca->rtt_above = 0; |
| 81 | |
| 82 | rtt_reset(sk); |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 85 | /* Measure RTT for each ack. */ |
Stephen Hemminger | 164891a | 2007-04-23 22:26:16 -0700 | [diff] [blame] | 86 | static void tcp_illinois_acked(struct sock *sk, u32 pkts_acked, ktime_t last) |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 87 | { |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 88 | struct illinois *ca = inet_csk_ca(sk); |
Stephen Hemminger | 164891a | 2007-04-23 22:26:16 -0700 | [diff] [blame] | 89 | u32 rtt; |
| 90 | |
| 91 | ca->acked = pkts_acked; |
| 92 | |
Ilpo Järvinen | b9ce204 | 2007-06-15 15:08:43 -0700 | [diff] [blame^] | 93 | if (ktime_equal(last, net_invalid_timestamp())) |
| 94 | return; |
| 95 | |
YOSHIFUJI Hideaki | 84299b3 | 2007-04-24 16:21:38 -0700 | [diff] [blame] | 96 | rtt = ktime_to_us(net_timedelta(last)); |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 97 | |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 98 | /* ignore bogus values, this prevents wraparound in alpha math */ |
| 99 | if (rtt > RTT_MAX) |
| 100 | rtt = RTT_MAX; |
| 101 | |
| 102 | /* keep track of minimum RTT seen so far */ |
| 103 | if (ca->base_rtt > rtt) |
| 104 | ca->base_rtt = rtt; |
| 105 | |
| 106 | /* and max */ |
| 107 | if (ca->max_rtt < rtt) |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 108 | ca->max_rtt = rtt; |
| 109 | |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 110 | ++ca->cnt_rtt; |
| 111 | ca->sum_rtt += rtt; |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 114 | /* Maximum queuing delay */ |
| 115 | static inline u32 max_delay(const struct illinois *ca) |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 116 | { |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 117 | return ca->max_rtt - ca->base_rtt; |
| 118 | } |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 119 | |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 120 | /* Average queuing delay */ |
| 121 | static inline u32 avg_delay(const struct illinois *ca) |
| 122 | { |
| 123 | u64 t = ca->sum_rtt; |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 124 | |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 125 | do_div(t, ca->cnt_rtt); |
| 126 | return t - ca->base_rtt; |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Compute value of alpha used for additive increase. |
| 131 | * If small window then use 1.0, equivalent to Reno. |
| 132 | * |
| 133 | * For larger windows, adjust based on average delay. |
| 134 | * A. If average delay is at minimum (we are uncongested), |
| 135 | * then use large alpha (10.0) to increase faster. |
| 136 | * B. If average delay is at maximum (getting congested) |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 137 | * then use small alpha (0.3) |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 138 | * |
| 139 | * The result is a convex window growth curve. |
| 140 | */ |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 141 | static u32 alpha(struct illinois *ca, u32 da, u32 dm) |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 142 | { |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 143 | u32 d1 = dm / 100; /* Low threshold */ |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 144 | |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 145 | if (da <= d1) { |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 146 | /* If never got out of low delay zone, then use max */ |
| 147 | if (!ca->rtt_above) |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 148 | return ALPHA_MAX; |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 149 | |
| 150 | /* Wait for 5 good RTT's before allowing alpha to go alpha max. |
| 151 | * This prevents one good RTT from causing sudden window increase. |
| 152 | */ |
| 153 | if (++ca->rtt_low < theta) |
| 154 | return ca->alpha; |
| 155 | |
| 156 | ca->rtt_low = 0; |
| 157 | ca->rtt_above = 0; |
| 158 | return ALPHA_MAX; |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 161 | ca->rtt_above = 1; |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * Based on: |
| 165 | * |
| 166 | * (dm - d1) amin amax |
| 167 | * k1 = ------------------- |
| 168 | * amax - amin |
| 169 | * |
| 170 | * (dm - d1) amin |
| 171 | * k2 = ---------------- - d1 |
| 172 | * amax - amin |
| 173 | * |
| 174 | * k1 |
| 175 | * alpha = ---------- |
| 176 | * k2 + da |
| 177 | */ |
| 178 | |
| 179 | dm -= d1; |
| 180 | da -= d1; |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 181 | return (dm * ALPHA_MAX) / |
| 182 | (dm + (da * (ALPHA_MAX - ALPHA_MIN)) / ALPHA_MIN); |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Beta used for multiplicative decrease. |
| 187 | * For small window sizes returns same value as Reno (0.5) |
| 188 | * |
| 189 | * If delay is small (10% of max) then beta = 1/8 |
| 190 | * If delay is up to 80% of max then beta = 1/2 |
| 191 | * In between is a linear function |
| 192 | */ |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 193 | static u32 beta(u32 da, u32 dm) |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 194 | { |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 195 | u32 d2, d3; |
| 196 | |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 197 | d2 = dm / 10; |
| 198 | if (da <= d2) |
| 199 | return BETA_MIN; |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 200 | |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 201 | d3 = (8 * dm) / 10; |
| 202 | if (da >= d3 || d3 <= d2) |
| 203 | return BETA_MAX; |
| 204 | |
| 205 | /* |
| 206 | * Based on: |
| 207 | * |
| 208 | * bmin d3 - bmax d2 |
| 209 | * k3 = ------------------- |
| 210 | * d3 - d2 |
| 211 | * |
| 212 | * bmax - bmin |
| 213 | * k4 = ------------- |
| 214 | * d3 - d2 |
| 215 | * |
| 216 | * b = k3 + k4 da |
| 217 | */ |
| 218 | return (BETA_MIN * d3 - BETA_MAX * d2 + (BETA_MAX - BETA_MIN) * da) |
| 219 | / (d3 - d2); |
| 220 | } |
| 221 | |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 222 | /* Update alpha and beta values once per RTT */ |
| 223 | static void update_params(struct sock *sk) |
| 224 | { |
| 225 | struct tcp_sock *tp = tcp_sk(sk); |
| 226 | struct illinois *ca = inet_csk_ca(sk); |
| 227 | |
| 228 | if (tp->snd_cwnd < win_thresh) { |
| 229 | ca->alpha = ALPHA_BASE; |
| 230 | ca->beta = BETA_BASE; |
| 231 | } else if (ca->cnt_rtt > 0) { |
| 232 | u32 dm = max_delay(ca); |
| 233 | u32 da = avg_delay(ca); |
| 234 | |
| 235 | ca->alpha = alpha(ca, da, dm); |
| 236 | ca->beta = beta(da, dm); |
| 237 | } |
| 238 | |
| 239 | rtt_reset(sk); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * In case of loss, reset to default values |
| 244 | */ |
| 245 | static void tcp_illinois_state(struct sock *sk, u8 new_state) |
| 246 | { |
| 247 | struct illinois *ca = inet_csk_ca(sk); |
| 248 | |
| 249 | if (new_state == TCP_CA_Loss) { |
| 250 | ca->alpha = ALPHA_BASE; |
| 251 | ca->beta = BETA_BASE; |
| 252 | ca->rtt_low = 0; |
| 253 | ca->rtt_above = 0; |
| 254 | rtt_reset(sk); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Increase window in response to successful acknowledgment. |
| 260 | */ |
| 261 | static void tcp_illinois_cong_avoid(struct sock *sk, u32 ack, u32 rtt, |
| 262 | u32 in_flight, int flag) |
| 263 | { |
| 264 | struct tcp_sock *tp = tcp_sk(sk); |
| 265 | struct illinois *ca = inet_csk_ca(sk); |
| 266 | |
| 267 | if (after(ack, ca->end_seq)) |
| 268 | update_params(sk); |
| 269 | |
| 270 | /* RFC2861 only increase cwnd if fully utilized */ |
| 271 | if (!tcp_is_cwnd_limited(sk, in_flight)) |
| 272 | return; |
| 273 | |
| 274 | /* In slow start */ |
| 275 | if (tp->snd_cwnd <= tp->snd_ssthresh) |
| 276 | tcp_slow_start(tp); |
| 277 | |
| 278 | else { |
| 279 | u32 delta; |
| 280 | |
| 281 | /* snd_cwnd_cnt is # of packets since last cwnd increment */ |
| 282 | tp->snd_cwnd_cnt += ca->acked; |
| 283 | ca->acked = 1; |
| 284 | |
| 285 | /* This is close approximation of: |
| 286 | * tp->snd_cwnd += alpha/tp->snd_cwnd |
| 287 | */ |
| 288 | delta = (tp->snd_cwnd_cnt * ca->alpha) >> ALPHA_SHIFT; |
| 289 | if (delta >= tp->snd_cwnd) { |
| 290 | tp->snd_cwnd = min(tp->snd_cwnd + delta / tp->snd_cwnd, |
| 291 | (u32) tp->snd_cwnd_clamp); |
| 292 | tp->snd_cwnd_cnt = 0; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 297 | static u32 tcp_illinois_ssthresh(struct sock *sk) |
| 298 | { |
| 299 | struct tcp_sock *tp = tcp_sk(sk); |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 300 | struct illinois *ca = inet_csk_ca(sk); |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 301 | |
| 302 | /* Multiplicative decrease */ |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 303 | return max((tp->snd_cwnd * ca->beta) >> BETA_SHIFT, 2U); |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 306 | |
| 307 | /* Extract info for Tcp socket info provided via netlink. */ |
| 308 | static void tcp_illinois_info(struct sock *sk, u32 ext, |
| 309 | struct sk_buff *skb) |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 310 | { |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 311 | const struct illinois *ca = inet_csk_ca(sk); |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 312 | |
| 313 | if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) { |
| 314 | struct tcpvegas_info info = { |
| 315 | .tcpv_enabled = 1, |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 316 | .tcpv_rttcnt = ca->cnt_rtt, |
| 317 | .tcpv_minrtt = ca->base_rtt, |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 318 | }; |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 319 | u64 t = ca->sum_rtt; |
| 320 | |
| 321 | do_div(t, ca->cnt_rtt); |
| 322 | info.tcpv_rtt = t; |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 323 | |
| 324 | nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | static struct tcp_congestion_ops tcp_illinois = { |
Stephen Hemminger | 164891a | 2007-04-23 22:26:16 -0700 | [diff] [blame] | 329 | .flags = TCP_CONG_RTT_STAMP, |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 330 | .init = tcp_illinois_init, |
| 331 | .ssthresh = tcp_illinois_ssthresh, |
| 332 | .min_cwnd = tcp_reno_min_cwnd, |
| 333 | .cong_avoid = tcp_illinois_cong_avoid, |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 334 | .set_state = tcp_illinois_state, |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 335 | .get_info = tcp_illinois_info, |
| 336 | .pkts_acked = tcp_illinois_acked, |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 337 | |
| 338 | .owner = THIS_MODULE, |
| 339 | .name = "illinois", |
| 340 | }; |
| 341 | |
| 342 | static int __init tcp_illinois_register(void) |
| 343 | { |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 344 | BUILD_BUG_ON(sizeof(struct illinois) > ICSK_CA_PRIV_SIZE); |
Stephen Hemminger | c462238 | 2007-04-20 17:07:51 -0700 | [diff] [blame] | 345 | return tcp_register_congestion_control(&tcp_illinois); |
| 346 | } |
| 347 | |
| 348 | static void __exit tcp_illinois_unregister(void) |
| 349 | { |
| 350 | tcp_unregister_congestion_control(&tcp_illinois); |
| 351 | } |
| 352 | |
| 353 | module_init(tcp_illinois_register); |
| 354 | module_exit(tcp_illinois_unregister); |
| 355 | |
| 356 | MODULE_AUTHOR("Stephen Hemminger, Shao Liu"); |
| 357 | MODULE_LICENSE("GPL"); |
| 358 | MODULE_DESCRIPTION("TCP Illinois"); |
Stephen Hemminger | 65d1b4a | 2007-04-23 22:24:32 -0700 | [diff] [blame] | 359 | MODULE_VERSION("1.0"); |