Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 3 | * operating system. INET is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * Implementation of the Transmission Control Protocol(TCP). |
| 7 | * |
| 8 | * Version: $Id: tcp_input.c,v 1.243 2002/02/01 22:01:04 davem Exp $ |
| 9 | * |
| 10 | * Authors: Ross Biro, <bir7@leland.Stanford.Edu> |
| 11 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
| 12 | * Mark Evans, <evansmp@uhura.aston.ac.uk> |
| 13 | * Corey Minyard <wf-rch!minyard@relay.EU.net> |
| 14 | * Florian La Roche, <flla@stud.uni-sb.de> |
| 15 | * Charles Hedrick, <hedrick@klinzhai.rutgers.edu> |
| 16 | * Linus Torvalds, <torvalds@cs.helsinki.fi> |
| 17 | * Alan Cox, <gw4pts@gw4pts.ampr.org> |
| 18 | * Matthew Dillon, <dillon@apollo.west.oic.com> |
| 19 | * Arnt Gulbrandsen, <agulbra@nvg.unit.no> |
| 20 | * Jorge Cwik, <jorge@laser.satlink.net> |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * Changes: |
| 25 | * Pedro Roque : Fast Retransmit/Recovery. |
| 26 | * Two receive queues. |
| 27 | * Retransmit queue handled by TCP. |
| 28 | * Better retransmit timer handling. |
| 29 | * New congestion avoidance. |
| 30 | * Header prediction. |
| 31 | * Variable renaming. |
| 32 | * |
| 33 | * Eric : Fast Retransmit. |
| 34 | * Randy Scott : MSS option defines. |
| 35 | * Eric Schenk : Fixes to slow start algorithm. |
| 36 | * Eric Schenk : Yet another double ACK bug. |
| 37 | * Eric Schenk : Delayed ACK bug fixes. |
| 38 | * Eric Schenk : Floyd style fast retrans war avoidance. |
| 39 | * David S. Miller : Don't allow zero congestion window. |
| 40 | * Eric Schenk : Fix retransmitter so that it sends |
| 41 | * next packet on ack of previous packet. |
| 42 | * Andi Kleen : Moved open_request checking here |
| 43 | * and process RSTs for open_requests. |
| 44 | * Andi Kleen : Better prune_queue, and other fixes. |
| 45 | * Andrey Savochkin: Fix RTT measurements in the presnce of |
| 46 | * timestamps. |
| 47 | * Andrey Savochkin: Check sequence numbers correctly when |
| 48 | * removing SACKs due to in sequence incoming |
| 49 | * data segments. |
| 50 | * Andi Kleen: Make sure we never ack data there is not |
| 51 | * enough room for. Also make this condition |
| 52 | * a fatal error if it might still happen. |
| 53 | * Andi Kleen: Add tcp_measure_rcv_mss to make |
| 54 | * connections with MSS<min(MTU,ann. MSS) |
| 55 | * work without delayed acks. |
| 56 | * Andi Kleen: Process packets with PSH set in the |
| 57 | * fast path. |
| 58 | * J Hadi Salim: ECN support |
| 59 | * Andrei Gurtov, |
| 60 | * Pasi Sarolahti, |
| 61 | * Panu Kuhlberg: Experimental audit of TCP (re)transmission |
| 62 | * engine. Lots of bugs are found. |
| 63 | * Pasi Sarolahti: F-RTO for dealing with spurious RTOs |
| 64 | * Angelo Dell'Aera: TCP Westwood+ support |
| 65 | */ |
| 66 | |
| 67 | #include <linux/config.h> |
| 68 | #include <linux/mm.h> |
| 69 | #include <linux/module.h> |
| 70 | #include <linux/sysctl.h> |
| 71 | #include <net/tcp.h> |
| 72 | #include <net/inet_common.h> |
| 73 | #include <linux/ipsec.h> |
| 74 | #include <asm/unaligned.h> |
| 75 | |
| 76 | int sysctl_tcp_timestamps = 1; |
| 77 | int sysctl_tcp_window_scaling = 1; |
| 78 | int sysctl_tcp_sack = 1; |
| 79 | int sysctl_tcp_fack = 1; |
| 80 | int sysctl_tcp_reordering = TCP_FASTRETRANS_THRESH; |
| 81 | int sysctl_tcp_ecn; |
| 82 | int sysctl_tcp_dsack = 1; |
| 83 | int sysctl_tcp_app_win = 31; |
| 84 | int sysctl_tcp_adv_win_scale = 2; |
| 85 | |
| 86 | int sysctl_tcp_stdurg; |
| 87 | int sysctl_tcp_rfc1337; |
| 88 | int sysctl_tcp_max_orphans = NR_FILE; |
| 89 | int sysctl_tcp_frto; |
| 90 | int sysctl_tcp_nometrics_save; |
| 91 | int sysctl_tcp_westwood; |
| 92 | int sysctl_tcp_vegas_cong_avoid; |
| 93 | |
| 94 | int sysctl_tcp_moderate_rcvbuf = 1; |
| 95 | |
| 96 | /* Default values of the Vegas variables, in fixed-point representation |
| 97 | * with V_PARAM_SHIFT bits to the right of the binary point. |
| 98 | */ |
| 99 | #define V_PARAM_SHIFT 1 |
| 100 | int sysctl_tcp_vegas_alpha = 1<<V_PARAM_SHIFT; |
| 101 | int sysctl_tcp_vegas_beta = 3<<V_PARAM_SHIFT; |
| 102 | int sysctl_tcp_vegas_gamma = 1<<V_PARAM_SHIFT; |
| 103 | int sysctl_tcp_bic = 1; |
| 104 | int sysctl_tcp_bic_fast_convergence = 1; |
| 105 | int sysctl_tcp_bic_low_window = 14; |
| 106 | int sysctl_tcp_bic_beta = 819; /* = 819/1024 (BICTCP_BETA_SCALE) */ |
| 107 | |
| 108 | #define FLAG_DATA 0x01 /* Incoming frame contained data. */ |
| 109 | #define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */ |
| 110 | #define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */ |
| 111 | #define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted. */ |
| 112 | #define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */ |
| 113 | #define FLAG_DATA_SACKED 0x20 /* New SACK. */ |
| 114 | #define FLAG_ECE 0x40 /* ECE in this ACK */ |
| 115 | #define FLAG_DATA_LOST 0x80 /* SACK detected data lossage. */ |
| 116 | #define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/ |
| 117 | |
| 118 | #define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED) |
| 119 | #define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED) |
| 120 | #define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE) |
| 121 | #define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED) |
| 122 | |
| 123 | #define IsReno(tp) ((tp)->rx_opt.sack_ok == 0) |
| 124 | #define IsFack(tp) ((tp)->rx_opt.sack_ok & 2) |
| 125 | #define IsDSack(tp) ((tp)->rx_opt.sack_ok & 4) |
| 126 | |
| 127 | #define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH) |
| 128 | |
| 129 | /* Adapt the MSS value used to make delayed ack decision to the |
| 130 | * real world. |
| 131 | */ |
| 132 | static inline void tcp_measure_rcv_mss(struct tcp_sock *tp, |
| 133 | struct sk_buff *skb) |
| 134 | { |
| 135 | unsigned int len, lss; |
| 136 | |
| 137 | lss = tp->ack.last_seg_size; |
| 138 | tp->ack.last_seg_size = 0; |
| 139 | |
| 140 | /* skb->len may jitter because of SACKs, even if peer |
| 141 | * sends good full-sized frames. |
| 142 | */ |
| 143 | len = skb->len; |
| 144 | if (len >= tp->ack.rcv_mss) { |
| 145 | tp->ack.rcv_mss = len; |
| 146 | } else { |
| 147 | /* Otherwise, we make more careful check taking into account, |
| 148 | * that SACKs block is variable. |
| 149 | * |
| 150 | * "len" is invariant segment length, including TCP header. |
| 151 | */ |
| 152 | len += skb->data - skb->h.raw; |
| 153 | if (len >= TCP_MIN_RCVMSS + sizeof(struct tcphdr) || |
| 154 | /* If PSH is not set, packet should be |
| 155 | * full sized, provided peer TCP is not badly broken. |
| 156 | * This observation (if it is correct 8)) allows |
| 157 | * to handle super-low mtu links fairly. |
| 158 | */ |
| 159 | (len >= TCP_MIN_MSS + sizeof(struct tcphdr) && |
| 160 | !(tcp_flag_word(skb->h.th)&TCP_REMNANT))) { |
| 161 | /* Subtract also invariant (if peer is RFC compliant), |
| 162 | * tcp header plus fixed timestamp option length. |
| 163 | * Resulting "len" is MSS free of SACK jitter. |
| 164 | */ |
| 165 | len -= tp->tcp_header_len; |
| 166 | tp->ack.last_seg_size = len; |
| 167 | if (len == lss) { |
| 168 | tp->ack.rcv_mss = len; |
| 169 | return; |
| 170 | } |
| 171 | } |
| 172 | tp->ack.pending |= TCP_ACK_PUSHED; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | static void tcp_incr_quickack(struct tcp_sock *tp) |
| 177 | { |
| 178 | unsigned quickacks = tp->rcv_wnd/(2*tp->ack.rcv_mss); |
| 179 | |
| 180 | if (quickacks==0) |
| 181 | quickacks=2; |
| 182 | if (quickacks > tp->ack.quick) |
| 183 | tp->ack.quick = min(quickacks, TCP_MAX_QUICKACKS); |
| 184 | } |
| 185 | |
| 186 | void tcp_enter_quickack_mode(struct tcp_sock *tp) |
| 187 | { |
| 188 | tcp_incr_quickack(tp); |
| 189 | tp->ack.pingpong = 0; |
| 190 | tp->ack.ato = TCP_ATO_MIN; |
| 191 | } |
| 192 | |
| 193 | /* Send ACKs quickly, if "quick" count is not exhausted |
| 194 | * and the session is not interactive. |
| 195 | */ |
| 196 | |
| 197 | static __inline__ int tcp_in_quickack_mode(struct tcp_sock *tp) |
| 198 | { |
| 199 | return (tp->ack.quick && !tp->ack.pingpong); |
| 200 | } |
| 201 | |
| 202 | /* Buffer size and advertised window tuning. |
| 203 | * |
| 204 | * 1. Tuning sk->sk_sndbuf, when connection enters established state. |
| 205 | */ |
| 206 | |
| 207 | static void tcp_fixup_sndbuf(struct sock *sk) |
| 208 | { |
| 209 | int sndmem = tcp_sk(sk)->rx_opt.mss_clamp + MAX_TCP_HEADER + 16 + |
| 210 | sizeof(struct sk_buff); |
| 211 | |
| 212 | if (sk->sk_sndbuf < 3 * sndmem) |
| 213 | sk->sk_sndbuf = min(3 * sndmem, sysctl_tcp_wmem[2]); |
| 214 | } |
| 215 | |
| 216 | /* 2. Tuning advertised window (window_clamp, rcv_ssthresh) |
| 217 | * |
| 218 | * All tcp_full_space() is split to two parts: "network" buffer, allocated |
| 219 | * forward and advertised in receiver window (tp->rcv_wnd) and |
| 220 | * "application buffer", required to isolate scheduling/application |
| 221 | * latencies from network. |
| 222 | * window_clamp is maximal advertised window. It can be less than |
| 223 | * tcp_full_space(), in this case tcp_full_space() - window_clamp |
| 224 | * is reserved for "application" buffer. The less window_clamp is |
| 225 | * the smoother our behaviour from viewpoint of network, but the lower |
| 226 | * throughput and the higher sensitivity of the connection to losses. 8) |
| 227 | * |
| 228 | * rcv_ssthresh is more strict window_clamp used at "slow start" |
| 229 | * phase to predict further behaviour of this connection. |
| 230 | * It is used for two goals: |
| 231 | * - to enforce header prediction at sender, even when application |
| 232 | * requires some significant "application buffer". It is check #1. |
| 233 | * - to prevent pruning of receive queue because of misprediction |
| 234 | * of receiver window. Check #2. |
| 235 | * |
| 236 | * The scheme does not work when sender sends good segments opening |
| 237 | * window and then starts to feed us spagetti. But it should work |
| 238 | * in common situations. Otherwise, we have to rely on queue collapsing. |
| 239 | */ |
| 240 | |
| 241 | /* Slow part of check#2. */ |
| 242 | static int __tcp_grow_window(struct sock *sk, struct tcp_sock *tp, |
| 243 | struct sk_buff *skb) |
| 244 | { |
| 245 | /* Optimize this! */ |
| 246 | int truesize = tcp_win_from_space(skb->truesize)/2; |
| 247 | int window = tcp_full_space(sk)/2; |
| 248 | |
| 249 | while (tp->rcv_ssthresh <= window) { |
| 250 | if (truesize <= skb->len) |
| 251 | return 2*tp->ack.rcv_mss; |
| 252 | |
| 253 | truesize >>= 1; |
| 254 | window >>= 1; |
| 255 | } |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static inline void tcp_grow_window(struct sock *sk, struct tcp_sock *tp, |
| 260 | struct sk_buff *skb) |
| 261 | { |
| 262 | /* Check #1 */ |
| 263 | if (tp->rcv_ssthresh < tp->window_clamp && |
| 264 | (int)tp->rcv_ssthresh < tcp_space(sk) && |
| 265 | !tcp_memory_pressure) { |
| 266 | int incr; |
| 267 | |
| 268 | /* Check #2. Increase window, if skb with such overhead |
| 269 | * will fit to rcvbuf in future. |
| 270 | */ |
| 271 | if (tcp_win_from_space(skb->truesize) <= skb->len) |
| 272 | incr = 2*tp->advmss; |
| 273 | else |
| 274 | incr = __tcp_grow_window(sk, tp, skb); |
| 275 | |
| 276 | if (incr) { |
| 277 | tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr, tp->window_clamp); |
| 278 | tp->ack.quick |= 1; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /* 3. Tuning rcvbuf, when connection enters established state. */ |
| 284 | |
| 285 | static void tcp_fixup_rcvbuf(struct sock *sk) |
| 286 | { |
| 287 | struct tcp_sock *tp = tcp_sk(sk); |
| 288 | int rcvmem = tp->advmss + MAX_TCP_HEADER + 16 + sizeof(struct sk_buff); |
| 289 | |
| 290 | /* Try to select rcvbuf so that 4 mss-sized segments |
| 291 | * will fit to window and correspoding skbs will fit to our rcvbuf. |
| 292 | * (was 3; 4 is minimum to allow fast retransmit to work.) |
| 293 | */ |
| 294 | while (tcp_win_from_space(rcvmem) < tp->advmss) |
| 295 | rcvmem += 128; |
| 296 | if (sk->sk_rcvbuf < 4 * rcvmem) |
| 297 | sk->sk_rcvbuf = min(4 * rcvmem, sysctl_tcp_rmem[2]); |
| 298 | } |
| 299 | |
| 300 | /* 4. Try to fixup all. It is made iimediately after connection enters |
| 301 | * established state. |
| 302 | */ |
| 303 | static void tcp_init_buffer_space(struct sock *sk) |
| 304 | { |
| 305 | struct tcp_sock *tp = tcp_sk(sk); |
| 306 | int maxwin; |
| 307 | |
| 308 | if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) |
| 309 | tcp_fixup_rcvbuf(sk); |
| 310 | if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK)) |
| 311 | tcp_fixup_sndbuf(sk); |
| 312 | |
| 313 | tp->rcvq_space.space = tp->rcv_wnd; |
| 314 | |
| 315 | maxwin = tcp_full_space(sk); |
| 316 | |
| 317 | if (tp->window_clamp >= maxwin) { |
| 318 | tp->window_clamp = maxwin; |
| 319 | |
| 320 | if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss) |
| 321 | tp->window_clamp = max(maxwin - |
| 322 | (maxwin >> sysctl_tcp_app_win), |
| 323 | 4 * tp->advmss); |
| 324 | } |
| 325 | |
| 326 | /* Force reservation of one segment. */ |
| 327 | if (sysctl_tcp_app_win && |
| 328 | tp->window_clamp > 2 * tp->advmss && |
| 329 | tp->window_clamp + tp->advmss > maxwin) |
| 330 | tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss); |
| 331 | |
| 332 | tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp); |
| 333 | tp->snd_cwnd_stamp = tcp_time_stamp; |
| 334 | } |
| 335 | |
| 336 | static void init_bictcp(struct tcp_sock *tp) |
| 337 | { |
| 338 | tp->bictcp.cnt = 0; |
| 339 | |
| 340 | tp->bictcp.last_max_cwnd = 0; |
| 341 | tp->bictcp.last_cwnd = 0; |
| 342 | tp->bictcp.last_stamp = 0; |
| 343 | } |
| 344 | |
| 345 | /* 5. Recalculate window clamp after socket hit its memory bounds. */ |
| 346 | static void tcp_clamp_window(struct sock *sk, struct tcp_sock *tp) |
| 347 | { |
| 348 | struct sk_buff *skb; |
| 349 | unsigned int app_win = tp->rcv_nxt - tp->copied_seq; |
| 350 | int ofo_win = 0; |
| 351 | |
| 352 | tp->ack.quick = 0; |
| 353 | |
| 354 | skb_queue_walk(&tp->out_of_order_queue, skb) { |
| 355 | ofo_win += skb->len; |
| 356 | } |
| 357 | |
| 358 | /* If overcommit is due to out of order segments, |
| 359 | * do not clamp window. Try to expand rcvbuf instead. |
| 360 | */ |
| 361 | if (ofo_win) { |
| 362 | if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] && |
| 363 | !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) && |
| 364 | !tcp_memory_pressure && |
| 365 | atomic_read(&tcp_memory_allocated) < sysctl_tcp_mem[0]) |
| 366 | sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc), |
| 367 | sysctl_tcp_rmem[2]); |
| 368 | } |
| 369 | if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf) { |
| 370 | app_win += ofo_win; |
| 371 | if (atomic_read(&sk->sk_rmem_alloc) >= 2 * sk->sk_rcvbuf) |
| 372 | app_win >>= 1; |
| 373 | if (app_win > tp->ack.rcv_mss) |
| 374 | app_win -= tp->ack.rcv_mss; |
| 375 | app_win = max(app_win, 2U*tp->advmss); |
| 376 | |
| 377 | if (!ofo_win) |
| 378 | tp->window_clamp = min(tp->window_clamp, app_win); |
| 379 | tp->rcv_ssthresh = min(tp->window_clamp, 2U*tp->advmss); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /* Receiver "autotuning" code. |
| 384 | * |
| 385 | * The algorithm for RTT estimation w/o timestamps is based on |
| 386 | * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL. |
| 387 | * <http://www.lanl.gov/radiant/website/pubs/drs/lacsi2001.ps> |
| 388 | * |
| 389 | * More detail on this code can be found at |
| 390 | * <http://www.psc.edu/~jheffner/senior_thesis.ps>, |
| 391 | * though this reference is out of date. A new paper |
| 392 | * is pending. |
| 393 | */ |
| 394 | static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep) |
| 395 | { |
| 396 | u32 new_sample = tp->rcv_rtt_est.rtt; |
| 397 | long m = sample; |
| 398 | |
| 399 | if (m == 0) |
| 400 | m = 1; |
| 401 | |
| 402 | if (new_sample != 0) { |
| 403 | /* If we sample in larger samples in the non-timestamp |
| 404 | * case, we could grossly overestimate the RTT especially |
| 405 | * with chatty applications or bulk transfer apps which |
| 406 | * are stalled on filesystem I/O. |
| 407 | * |
| 408 | * Also, since we are only going for a minimum in the |
| 409 | * non-timestamp case, we do not smoothe things out |
| 410 | * else with timestamps disabled convergance takes too |
| 411 | * long. |
| 412 | */ |
| 413 | if (!win_dep) { |
| 414 | m -= (new_sample >> 3); |
| 415 | new_sample += m; |
| 416 | } else if (m < new_sample) |
| 417 | new_sample = m << 3; |
| 418 | } else { |
| 419 | /* No previous mesaure. */ |
| 420 | new_sample = m << 3; |
| 421 | } |
| 422 | |
| 423 | if (tp->rcv_rtt_est.rtt != new_sample) |
| 424 | tp->rcv_rtt_est.rtt = new_sample; |
| 425 | } |
| 426 | |
| 427 | static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp) |
| 428 | { |
| 429 | if (tp->rcv_rtt_est.time == 0) |
| 430 | goto new_measure; |
| 431 | if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq)) |
| 432 | return; |
| 433 | tcp_rcv_rtt_update(tp, |
| 434 | jiffies - tp->rcv_rtt_est.time, |
| 435 | 1); |
| 436 | |
| 437 | new_measure: |
| 438 | tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd; |
| 439 | tp->rcv_rtt_est.time = tcp_time_stamp; |
| 440 | } |
| 441 | |
| 442 | static inline void tcp_rcv_rtt_measure_ts(struct tcp_sock *tp, struct sk_buff *skb) |
| 443 | { |
| 444 | if (tp->rx_opt.rcv_tsecr && |
| 445 | (TCP_SKB_CB(skb)->end_seq - |
| 446 | TCP_SKB_CB(skb)->seq >= tp->ack.rcv_mss)) |
| 447 | tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rx_opt.rcv_tsecr, 0); |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * This function should be called every time data is copied to user space. |
| 452 | * It calculates the appropriate TCP receive buffer space. |
| 453 | */ |
| 454 | void tcp_rcv_space_adjust(struct sock *sk) |
| 455 | { |
| 456 | struct tcp_sock *tp = tcp_sk(sk); |
| 457 | int time; |
| 458 | int space; |
| 459 | |
| 460 | if (tp->rcvq_space.time == 0) |
| 461 | goto new_measure; |
| 462 | |
| 463 | time = tcp_time_stamp - tp->rcvq_space.time; |
| 464 | if (time < (tp->rcv_rtt_est.rtt >> 3) || |
| 465 | tp->rcv_rtt_est.rtt == 0) |
| 466 | return; |
| 467 | |
| 468 | space = 2 * (tp->copied_seq - tp->rcvq_space.seq); |
| 469 | |
| 470 | space = max(tp->rcvq_space.space, space); |
| 471 | |
| 472 | if (tp->rcvq_space.space != space) { |
| 473 | int rcvmem; |
| 474 | |
| 475 | tp->rcvq_space.space = space; |
| 476 | |
| 477 | if (sysctl_tcp_moderate_rcvbuf) { |
| 478 | int new_clamp = space; |
| 479 | |
| 480 | /* Receive space grows, normalize in order to |
| 481 | * take into account packet headers and sk_buff |
| 482 | * structure overhead. |
| 483 | */ |
| 484 | space /= tp->advmss; |
| 485 | if (!space) |
| 486 | space = 1; |
| 487 | rcvmem = (tp->advmss + MAX_TCP_HEADER + |
| 488 | 16 + sizeof(struct sk_buff)); |
| 489 | while (tcp_win_from_space(rcvmem) < tp->advmss) |
| 490 | rcvmem += 128; |
| 491 | space *= rcvmem; |
| 492 | space = min(space, sysctl_tcp_rmem[2]); |
| 493 | if (space > sk->sk_rcvbuf) { |
| 494 | sk->sk_rcvbuf = space; |
| 495 | |
| 496 | /* Make the window clamp follow along. */ |
| 497 | tp->window_clamp = new_clamp; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | new_measure: |
| 503 | tp->rcvq_space.seq = tp->copied_seq; |
| 504 | tp->rcvq_space.time = tcp_time_stamp; |
| 505 | } |
| 506 | |
| 507 | /* There is something which you must keep in mind when you analyze the |
| 508 | * behavior of the tp->ato delayed ack timeout interval. When a |
| 509 | * connection starts up, we want to ack as quickly as possible. The |
| 510 | * problem is that "good" TCP's do slow start at the beginning of data |
| 511 | * transmission. The means that until we send the first few ACK's the |
| 512 | * sender will sit on his end and only queue most of his data, because |
| 513 | * he can only send snd_cwnd unacked packets at any given time. For |
| 514 | * each ACK we send, he increments snd_cwnd and transmits more of his |
| 515 | * queue. -DaveM |
| 516 | */ |
| 517 | static void tcp_event_data_recv(struct sock *sk, struct tcp_sock *tp, struct sk_buff *skb) |
| 518 | { |
| 519 | u32 now; |
| 520 | |
| 521 | tcp_schedule_ack(tp); |
| 522 | |
| 523 | tcp_measure_rcv_mss(tp, skb); |
| 524 | |
| 525 | tcp_rcv_rtt_measure(tp); |
| 526 | |
| 527 | now = tcp_time_stamp; |
| 528 | |
| 529 | if (!tp->ack.ato) { |
| 530 | /* The _first_ data packet received, initialize |
| 531 | * delayed ACK engine. |
| 532 | */ |
| 533 | tcp_incr_quickack(tp); |
| 534 | tp->ack.ato = TCP_ATO_MIN; |
| 535 | } else { |
| 536 | int m = now - tp->ack.lrcvtime; |
| 537 | |
| 538 | if (m <= TCP_ATO_MIN/2) { |
| 539 | /* The fastest case is the first. */ |
| 540 | tp->ack.ato = (tp->ack.ato>>1) + TCP_ATO_MIN/2; |
| 541 | } else if (m < tp->ack.ato) { |
| 542 | tp->ack.ato = (tp->ack.ato>>1) + m; |
| 543 | if (tp->ack.ato > tp->rto) |
| 544 | tp->ack.ato = tp->rto; |
| 545 | } else if (m > tp->rto) { |
| 546 | /* Too long gap. Apparently sender falled to |
| 547 | * restart window, so that we send ACKs quickly. |
| 548 | */ |
| 549 | tcp_incr_quickack(tp); |
| 550 | sk_stream_mem_reclaim(sk); |
| 551 | } |
| 552 | } |
| 553 | tp->ack.lrcvtime = now; |
| 554 | |
| 555 | TCP_ECN_check_ce(tp, skb); |
| 556 | |
| 557 | if (skb->len >= 128) |
| 558 | tcp_grow_window(sk, tp, skb); |
| 559 | } |
| 560 | |
| 561 | /* When starting a new connection, pin down the current choice of |
| 562 | * congestion algorithm. |
| 563 | */ |
| 564 | void tcp_ca_init(struct tcp_sock *tp) |
| 565 | { |
| 566 | if (sysctl_tcp_westwood) |
| 567 | tp->adv_cong = TCP_WESTWOOD; |
| 568 | else if (sysctl_tcp_bic) |
| 569 | tp->adv_cong = TCP_BIC; |
| 570 | else if (sysctl_tcp_vegas_cong_avoid) { |
| 571 | tp->adv_cong = TCP_VEGAS; |
| 572 | tp->vegas.baseRTT = 0x7fffffff; |
| 573 | tcp_vegas_enable(tp); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /* Do RTT sampling needed for Vegas. |
| 578 | * Basically we: |
| 579 | * o min-filter RTT samples from within an RTT to get the current |
| 580 | * propagation delay + queuing delay (we are min-filtering to try to |
| 581 | * avoid the effects of delayed ACKs) |
| 582 | * o min-filter RTT samples from a much longer window (forever for now) |
| 583 | * to find the propagation delay (baseRTT) |
| 584 | */ |
| 585 | static inline void vegas_rtt_calc(struct tcp_sock *tp, __u32 rtt) |
| 586 | { |
| 587 | __u32 vrtt = rtt + 1; /* Never allow zero rtt or baseRTT */ |
| 588 | |
| 589 | /* Filter to find propagation delay: */ |
| 590 | if (vrtt < tp->vegas.baseRTT) |
| 591 | tp->vegas.baseRTT = vrtt; |
| 592 | |
| 593 | /* Find the min RTT during the last RTT to find |
| 594 | * the current prop. delay + queuing delay: |
| 595 | */ |
| 596 | tp->vegas.minRTT = min(tp->vegas.minRTT, vrtt); |
| 597 | tp->vegas.cntRTT++; |
| 598 | } |
| 599 | |
| 600 | /* Called to compute a smoothed rtt estimate. The data fed to this |
| 601 | * routine either comes from timestamps, or from segments that were |
| 602 | * known _not_ to have been retransmitted [see Karn/Partridge |
| 603 | * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88 |
| 604 | * piece by Van Jacobson. |
| 605 | * NOTE: the next three routines used to be one big routine. |
| 606 | * To save cycles in the RFC 1323 implementation it was better to break |
| 607 | * it up into three procedures. -- erics |
| 608 | */ |
| 609 | static void tcp_rtt_estimator(struct tcp_sock *tp, __u32 mrtt) |
| 610 | { |
| 611 | long m = mrtt; /* RTT */ |
| 612 | |
| 613 | if (tcp_vegas_enabled(tp)) |
| 614 | vegas_rtt_calc(tp, mrtt); |
| 615 | |
| 616 | /* The following amusing code comes from Jacobson's |
| 617 | * article in SIGCOMM '88. Note that rtt and mdev |
| 618 | * are scaled versions of rtt and mean deviation. |
| 619 | * This is designed to be as fast as possible |
| 620 | * m stands for "measurement". |
| 621 | * |
| 622 | * On a 1990 paper the rto value is changed to: |
| 623 | * RTO = rtt + 4 * mdev |
| 624 | * |
| 625 | * Funny. This algorithm seems to be very broken. |
| 626 | * These formulae increase RTO, when it should be decreased, increase |
| 627 | * too slowly, when it should be incresed fastly, decrease too fastly |
| 628 | * etc. I guess in BSD RTO takes ONE value, so that it is absolutely |
| 629 | * does not matter how to _calculate_ it. Seems, it was trap |
| 630 | * that VJ failed to avoid. 8) |
| 631 | */ |
| 632 | if(m == 0) |
| 633 | m = 1; |
| 634 | if (tp->srtt != 0) { |
| 635 | m -= (tp->srtt >> 3); /* m is now error in rtt est */ |
| 636 | tp->srtt += m; /* rtt = 7/8 rtt + 1/8 new */ |
| 637 | if (m < 0) { |
| 638 | m = -m; /* m is now abs(error) */ |
| 639 | m -= (tp->mdev >> 2); /* similar update on mdev */ |
| 640 | /* This is similar to one of Eifel findings. |
| 641 | * Eifel blocks mdev updates when rtt decreases. |
| 642 | * This solution is a bit different: we use finer gain |
| 643 | * for mdev in this case (alpha*beta). |
| 644 | * Like Eifel it also prevents growth of rto, |
| 645 | * but also it limits too fast rto decreases, |
| 646 | * happening in pure Eifel. |
| 647 | */ |
| 648 | if (m > 0) |
| 649 | m >>= 3; |
| 650 | } else { |
| 651 | m -= (tp->mdev >> 2); /* similar update on mdev */ |
| 652 | } |
| 653 | tp->mdev += m; /* mdev = 3/4 mdev + 1/4 new */ |
| 654 | if (tp->mdev > tp->mdev_max) { |
| 655 | tp->mdev_max = tp->mdev; |
| 656 | if (tp->mdev_max > tp->rttvar) |
| 657 | tp->rttvar = tp->mdev_max; |
| 658 | } |
| 659 | if (after(tp->snd_una, tp->rtt_seq)) { |
| 660 | if (tp->mdev_max < tp->rttvar) |
| 661 | tp->rttvar -= (tp->rttvar-tp->mdev_max)>>2; |
| 662 | tp->rtt_seq = tp->snd_nxt; |
| 663 | tp->mdev_max = TCP_RTO_MIN; |
| 664 | } |
| 665 | } else { |
| 666 | /* no previous measure. */ |
| 667 | tp->srtt = m<<3; /* take the measured time to be rtt */ |
| 668 | tp->mdev = m<<1; /* make sure rto = 3*rtt */ |
| 669 | tp->mdev_max = tp->rttvar = max(tp->mdev, TCP_RTO_MIN); |
| 670 | tp->rtt_seq = tp->snd_nxt; |
| 671 | } |
| 672 | |
| 673 | tcp_westwood_update_rtt(tp, tp->srtt >> 3); |
| 674 | } |
| 675 | |
| 676 | /* Calculate rto without backoff. This is the second half of Van Jacobson's |
| 677 | * routine referred to above. |
| 678 | */ |
| 679 | static inline void tcp_set_rto(struct tcp_sock *tp) |
| 680 | { |
| 681 | /* Old crap is replaced with new one. 8) |
| 682 | * |
| 683 | * More seriously: |
| 684 | * 1. If rtt variance happened to be less 50msec, it is hallucination. |
| 685 | * It cannot be less due to utterly erratic ACK generation made |
| 686 | * at least by solaris and freebsd. "Erratic ACKs" has _nothing_ |
| 687 | * to do with delayed acks, because at cwnd>2 true delack timeout |
| 688 | * is invisible. Actually, Linux-2.4 also generates erratic |
| 689 | * ACKs in some curcumstances. |
| 690 | */ |
| 691 | tp->rto = (tp->srtt >> 3) + tp->rttvar; |
| 692 | |
| 693 | /* 2. Fixups made earlier cannot be right. |
| 694 | * If we do not estimate RTO correctly without them, |
| 695 | * all the algo is pure shit and should be replaced |
| 696 | * with correct one. It is exaclty, which we pretend to do. |
| 697 | */ |
| 698 | } |
| 699 | |
| 700 | /* NOTE: clamping at TCP_RTO_MIN is not required, current algo |
| 701 | * guarantees that rto is higher. |
| 702 | */ |
| 703 | static inline void tcp_bound_rto(struct tcp_sock *tp) |
| 704 | { |
| 705 | if (tp->rto > TCP_RTO_MAX) |
| 706 | tp->rto = TCP_RTO_MAX; |
| 707 | } |
| 708 | |
| 709 | /* Save metrics learned by this TCP session. |
| 710 | This function is called only, when TCP finishes successfully |
| 711 | i.e. when it enters TIME-WAIT or goes from LAST-ACK to CLOSE. |
| 712 | */ |
| 713 | void tcp_update_metrics(struct sock *sk) |
| 714 | { |
| 715 | struct tcp_sock *tp = tcp_sk(sk); |
| 716 | struct dst_entry *dst = __sk_dst_get(sk); |
| 717 | |
| 718 | if (sysctl_tcp_nometrics_save) |
| 719 | return; |
| 720 | |
| 721 | dst_confirm(dst); |
| 722 | |
| 723 | if (dst && (dst->flags&DST_HOST)) { |
| 724 | int m; |
| 725 | |
| 726 | if (tp->backoff || !tp->srtt) { |
| 727 | /* This session failed to estimate rtt. Why? |
| 728 | * Probably, no packets returned in time. |
| 729 | * Reset our results. |
| 730 | */ |
| 731 | if (!(dst_metric_locked(dst, RTAX_RTT))) |
| 732 | dst->metrics[RTAX_RTT-1] = 0; |
| 733 | return; |
| 734 | } |
| 735 | |
| 736 | m = dst_metric(dst, RTAX_RTT) - tp->srtt; |
| 737 | |
| 738 | /* If newly calculated rtt larger than stored one, |
| 739 | * store new one. Otherwise, use EWMA. Remember, |
| 740 | * rtt overestimation is always better than underestimation. |
| 741 | */ |
| 742 | if (!(dst_metric_locked(dst, RTAX_RTT))) { |
| 743 | if (m <= 0) |
| 744 | dst->metrics[RTAX_RTT-1] = tp->srtt; |
| 745 | else |
| 746 | dst->metrics[RTAX_RTT-1] -= (m>>3); |
| 747 | } |
| 748 | |
| 749 | if (!(dst_metric_locked(dst, RTAX_RTTVAR))) { |
| 750 | if (m < 0) |
| 751 | m = -m; |
| 752 | |
| 753 | /* Scale deviation to rttvar fixed point */ |
| 754 | m >>= 1; |
| 755 | if (m < tp->mdev) |
| 756 | m = tp->mdev; |
| 757 | |
| 758 | if (m >= dst_metric(dst, RTAX_RTTVAR)) |
| 759 | dst->metrics[RTAX_RTTVAR-1] = m; |
| 760 | else |
| 761 | dst->metrics[RTAX_RTTVAR-1] -= |
| 762 | (dst->metrics[RTAX_RTTVAR-1] - m)>>2; |
| 763 | } |
| 764 | |
| 765 | if (tp->snd_ssthresh >= 0xFFFF) { |
| 766 | /* Slow start still did not finish. */ |
| 767 | if (dst_metric(dst, RTAX_SSTHRESH) && |
| 768 | !dst_metric_locked(dst, RTAX_SSTHRESH) && |
| 769 | (tp->snd_cwnd >> 1) > dst_metric(dst, RTAX_SSTHRESH)) |
| 770 | dst->metrics[RTAX_SSTHRESH-1] = tp->snd_cwnd >> 1; |
| 771 | if (!dst_metric_locked(dst, RTAX_CWND) && |
| 772 | tp->snd_cwnd > dst_metric(dst, RTAX_CWND)) |
| 773 | dst->metrics[RTAX_CWND-1] = tp->snd_cwnd; |
| 774 | } else if (tp->snd_cwnd > tp->snd_ssthresh && |
| 775 | tp->ca_state == TCP_CA_Open) { |
| 776 | /* Cong. avoidance phase, cwnd is reliable. */ |
| 777 | if (!dst_metric_locked(dst, RTAX_SSTHRESH)) |
| 778 | dst->metrics[RTAX_SSTHRESH-1] = |
| 779 | max(tp->snd_cwnd >> 1, tp->snd_ssthresh); |
| 780 | if (!dst_metric_locked(dst, RTAX_CWND)) |
| 781 | dst->metrics[RTAX_CWND-1] = (dst->metrics[RTAX_CWND-1] + tp->snd_cwnd) >> 1; |
| 782 | } else { |
| 783 | /* Else slow start did not finish, cwnd is non-sense, |
| 784 | ssthresh may be also invalid. |
| 785 | */ |
| 786 | if (!dst_metric_locked(dst, RTAX_CWND)) |
| 787 | dst->metrics[RTAX_CWND-1] = (dst->metrics[RTAX_CWND-1] + tp->snd_ssthresh) >> 1; |
| 788 | if (dst->metrics[RTAX_SSTHRESH-1] && |
| 789 | !dst_metric_locked(dst, RTAX_SSTHRESH) && |
| 790 | tp->snd_ssthresh > dst->metrics[RTAX_SSTHRESH-1]) |
| 791 | dst->metrics[RTAX_SSTHRESH-1] = tp->snd_ssthresh; |
| 792 | } |
| 793 | |
| 794 | if (!dst_metric_locked(dst, RTAX_REORDERING)) { |
| 795 | if (dst->metrics[RTAX_REORDERING-1] < tp->reordering && |
| 796 | tp->reordering != sysctl_tcp_reordering) |
| 797 | dst->metrics[RTAX_REORDERING-1] = tp->reordering; |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | /* Numbers are taken from RFC2414. */ |
| 803 | __u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst) |
| 804 | { |
| 805 | __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0); |
| 806 | |
| 807 | if (!cwnd) { |
| 808 | if (tp->mss_cache_std > 1460) |
| 809 | cwnd = 2; |
| 810 | else |
| 811 | cwnd = (tp->mss_cache_std > 1095) ? 3 : 4; |
| 812 | } |
| 813 | return min_t(__u32, cwnd, tp->snd_cwnd_clamp); |
| 814 | } |
| 815 | |
| 816 | /* Initialize metrics on socket. */ |
| 817 | |
| 818 | static void tcp_init_metrics(struct sock *sk) |
| 819 | { |
| 820 | struct tcp_sock *tp = tcp_sk(sk); |
| 821 | struct dst_entry *dst = __sk_dst_get(sk); |
| 822 | |
| 823 | if (dst == NULL) |
| 824 | goto reset; |
| 825 | |
| 826 | dst_confirm(dst); |
| 827 | |
| 828 | if (dst_metric_locked(dst, RTAX_CWND)) |
| 829 | tp->snd_cwnd_clamp = dst_metric(dst, RTAX_CWND); |
| 830 | if (dst_metric(dst, RTAX_SSTHRESH)) { |
| 831 | tp->snd_ssthresh = dst_metric(dst, RTAX_SSTHRESH); |
| 832 | if (tp->snd_ssthresh > tp->snd_cwnd_clamp) |
| 833 | tp->snd_ssthresh = tp->snd_cwnd_clamp; |
| 834 | } |
| 835 | if (dst_metric(dst, RTAX_REORDERING) && |
| 836 | tp->reordering != dst_metric(dst, RTAX_REORDERING)) { |
| 837 | tp->rx_opt.sack_ok &= ~2; |
| 838 | tp->reordering = dst_metric(dst, RTAX_REORDERING); |
| 839 | } |
| 840 | |
| 841 | if (dst_metric(dst, RTAX_RTT) == 0) |
| 842 | goto reset; |
| 843 | |
| 844 | if (!tp->srtt && dst_metric(dst, RTAX_RTT) < (TCP_TIMEOUT_INIT << 3)) |
| 845 | goto reset; |
| 846 | |
| 847 | /* Initial rtt is determined from SYN,SYN-ACK. |
| 848 | * The segment is small and rtt may appear much |
| 849 | * less than real one. Use per-dst memory |
| 850 | * to make it more realistic. |
| 851 | * |
| 852 | * A bit of theory. RTT is time passed after "normal" sized packet |
| 853 | * is sent until it is ACKed. In normal curcumstances sending small |
| 854 | * packets force peer to delay ACKs and calculation is correct too. |
| 855 | * The algorithm is adaptive and, provided we follow specs, it |
| 856 | * NEVER underestimate RTT. BUT! If peer tries to make some clever |
| 857 | * tricks sort of "quick acks" for time long enough to decrease RTT |
| 858 | * to low value, and then abruptly stops to do it and starts to delay |
| 859 | * ACKs, wait for troubles. |
| 860 | */ |
| 861 | if (dst_metric(dst, RTAX_RTT) > tp->srtt) { |
| 862 | tp->srtt = dst_metric(dst, RTAX_RTT); |
| 863 | tp->rtt_seq = tp->snd_nxt; |
| 864 | } |
| 865 | if (dst_metric(dst, RTAX_RTTVAR) > tp->mdev) { |
| 866 | tp->mdev = dst_metric(dst, RTAX_RTTVAR); |
| 867 | tp->mdev_max = tp->rttvar = max(tp->mdev, TCP_RTO_MIN); |
| 868 | } |
| 869 | tcp_set_rto(tp); |
| 870 | tcp_bound_rto(tp); |
| 871 | if (tp->rto < TCP_TIMEOUT_INIT && !tp->rx_opt.saw_tstamp) |
| 872 | goto reset; |
| 873 | tp->snd_cwnd = tcp_init_cwnd(tp, dst); |
| 874 | tp->snd_cwnd_stamp = tcp_time_stamp; |
| 875 | return; |
| 876 | |
| 877 | reset: |
| 878 | /* Play conservative. If timestamps are not |
| 879 | * supported, TCP will fail to recalculate correct |
| 880 | * rtt, if initial rto is too small. FORGET ALL AND RESET! |
| 881 | */ |
| 882 | if (!tp->rx_opt.saw_tstamp && tp->srtt) { |
| 883 | tp->srtt = 0; |
| 884 | tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_INIT; |
| 885 | tp->rto = TCP_TIMEOUT_INIT; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | static void tcp_update_reordering(struct tcp_sock *tp, int metric, int ts) |
| 890 | { |
| 891 | if (metric > tp->reordering) { |
| 892 | tp->reordering = min(TCP_MAX_REORDERING, metric); |
| 893 | |
| 894 | /* This exciting event is worth to be remembered. 8) */ |
| 895 | if (ts) |
| 896 | NET_INC_STATS_BH(LINUX_MIB_TCPTSREORDER); |
| 897 | else if (IsReno(tp)) |
| 898 | NET_INC_STATS_BH(LINUX_MIB_TCPRENOREORDER); |
| 899 | else if (IsFack(tp)) |
| 900 | NET_INC_STATS_BH(LINUX_MIB_TCPFACKREORDER); |
| 901 | else |
| 902 | NET_INC_STATS_BH(LINUX_MIB_TCPSACKREORDER); |
| 903 | #if FASTRETRANS_DEBUG > 1 |
| 904 | printk(KERN_DEBUG "Disorder%d %d %u f%u s%u rr%d\n", |
| 905 | tp->rx_opt.sack_ok, tp->ca_state, |
| 906 | tp->reordering, |
| 907 | tp->fackets_out, |
| 908 | tp->sacked_out, |
| 909 | tp->undo_marker ? tp->undo_retrans : 0); |
| 910 | #endif |
| 911 | /* Disable FACK yet. */ |
| 912 | tp->rx_opt.sack_ok &= ~2; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | /* This procedure tags the retransmission queue when SACKs arrive. |
| 917 | * |
| 918 | * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L). |
| 919 | * Packets in queue with these bits set are counted in variables |
| 920 | * sacked_out, retrans_out and lost_out, correspondingly. |
| 921 | * |
| 922 | * Valid combinations are: |
| 923 | * Tag InFlight Description |
| 924 | * 0 1 - orig segment is in flight. |
| 925 | * S 0 - nothing flies, orig reached receiver. |
| 926 | * L 0 - nothing flies, orig lost by net. |
| 927 | * R 2 - both orig and retransmit are in flight. |
| 928 | * L|R 1 - orig is lost, retransmit is in flight. |
| 929 | * S|R 1 - orig reached receiver, retrans is still in flight. |
| 930 | * (L|S|R is logically valid, it could occur when L|R is sacked, |
| 931 | * but it is equivalent to plain S and code short-curcuits it to S. |
| 932 | * L|S is logically invalid, it would mean -1 packet in flight 8)) |
| 933 | * |
| 934 | * These 6 states form finite state machine, controlled by the following events: |
| 935 | * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue()) |
| 936 | * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue()) |
| 937 | * 3. Loss detection event of one of three flavors: |
| 938 | * A. Scoreboard estimator decided the packet is lost. |
| 939 | * A'. Reno "three dupacks" marks head of queue lost. |
| 940 | * A''. Its FACK modfication, head until snd.fack is lost. |
| 941 | * B. SACK arrives sacking data transmitted after never retransmitted |
| 942 | * hole was sent out. |
| 943 | * C. SACK arrives sacking SND.NXT at the moment, when the |
| 944 | * segment was retransmitted. |
| 945 | * 4. D-SACK added new rule: D-SACK changes any tag to S. |
| 946 | * |
| 947 | * It is pleasant to note, that state diagram turns out to be commutative, |
| 948 | * so that we are allowed not to be bothered by order of our actions, |
| 949 | * when multiple events arrive simultaneously. (see the function below). |
| 950 | * |
| 951 | * Reordering detection. |
| 952 | * -------------------- |
| 953 | * Reordering metric is maximal distance, which a packet can be displaced |
| 954 | * in packet stream. With SACKs we can estimate it: |
| 955 | * |
| 956 | * 1. SACK fills old hole and the corresponding segment was not |
| 957 | * ever retransmitted -> reordering. Alas, we cannot use it |
| 958 | * when segment was retransmitted. |
| 959 | * 2. The last flaw is solved with D-SACK. D-SACK arrives |
| 960 | * for retransmitted and already SACKed segment -> reordering.. |
| 961 | * Both of these heuristics are not used in Loss state, when we cannot |
| 962 | * account for retransmits accurately. |
| 963 | */ |
| 964 | static int |
| 965 | tcp_sacktag_write_queue(struct sock *sk, struct sk_buff *ack_skb, u32 prior_snd_una) |
| 966 | { |
| 967 | struct tcp_sock *tp = tcp_sk(sk); |
| 968 | unsigned char *ptr = ack_skb->h.raw + TCP_SKB_CB(ack_skb)->sacked; |
| 969 | struct tcp_sack_block *sp = (struct tcp_sack_block *)(ptr+2); |
| 970 | int num_sacks = (ptr[1] - TCPOLEN_SACK_BASE)>>3; |
| 971 | int reord = tp->packets_out; |
| 972 | int prior_fackets; |
| 973 | u32 lost_retrans = 0; |
| 974 | int flag = 0; |
| 975 | int i; |
| 976 | |
| 977 | /* So, SACKs for already sent large segments will be lost. |
| 978 | * Not good, but alternative is to resegment the queue. */ |
| 979 | if (sk->sk_route_caps & NETIF_F_TSO) { |
| 980 | sk->sk_route_caps &= ~NETIF_F_TSO; |
| 981 | sock_set_flag(sk, SOCK_NO_LARGESEND); |
| 982 | tp->mss_cache = tp->mss_cache_std; |
| 983 | } |
| 984 | |
| 985 | if (!tp->sacked_out) |
| 986 | tp->fackets_out = 0; |
| 987 | prior_fackets = tp->fackets_out; |
| 988 | |
| 989 | for (i=0; i<num_sacks; i++, sp++) { |
| 990 | struct sk_buff *skb; |
| 991 | __u32 start_seq = ntohl(sp->start_seq); |
| 992 | __u32 end_seq = ntohl(sp->end_seq); |
| 993 | int fack_count = 0; |
| 994 | int dup_sack = 0; |
| 995 | |
| 996 | /* Check for D-SACK. */ |
| 997 | if (i == 0) { |
| 998 | u32 ack = TCP_SKB_CB(ack_skb)->ack_seq; |
| 999 | |
| 1000 | if (before(start_seq, ack)) { |
| 1001 | dup_sack = 1; |
| 1002 | tp->rx_opt.sack_ok |= 4; |
| 1003 | NET_INC_STATS_BH(LINUX_MIB_TCPDSACKRECV); |
| 1004 | } else if (num_sacks > 1 && |
| 1005 | !after(end_seq, ntohl(sp[1].end_seq)) && |
| 1006 | !before(start_seq, ntohl(sp[1].start_seq))) { |
| 1007 | dup_sack = 1; |
| 1008 | tp->rx_opt.sack_ok |= 4; |
| 1009 | NET_INC_STATS_BH(LINUX_MIB_TCPDSACKOFORECV); |
| 1010 | } |
| 1011 | |
| 1012 | /* D-SACK for already forgotten data... |
| 1013 | * Do dumb counting. */ |
| 1014 | if (dup_sack && |
| 1015 | !after(end_seq, prior_snd_una) && |
| 1016 | after(end_seq, tp->undo_marker)) |
| 1017 | tp->undo_retrans--; |
| 1018 | |
| 1019 | /* Eliminate too old ACKs, but take into |
| 1020 | * account more or less fresh ones, they can |
| 1021 | * contain valid SACK info. |
| 1022 | */ |
| 1023 | if (before(ack, prior_snd_una - tp->max_window)) |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | /* Event "B" in the comment above. */ |
| 1028 | if (after(end_seq, tp->high_seq)) |
| 1029 | flag |= FLAG_DATA_LOST; |
| 1030 | |
| 1031 | sk_stream_for_retrans_queue(skb, sk) { |
| 1032 | u8 sacked = TCP_SKB_CB(skb)->sacked; |
| 1033 | int in_sack; |
| 1034 | |
| 1035 | /* The retransmission queue is always in order, so |
| 1036 | * we can short-circuit the walk early. |
| 1037 | */ |
| 1038 | if(!before(TCP_SKB_CB(skb)->seq, end_seq)) |
| 1039 | break; |
| 1040 | |
| 1041 | fack_count += tcp_skb_pcount(skb); |
| 1042 | |
| 1043 | in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) && |
| 1044 | !before(end_seq, TCP_SKB_CB(skb)->end_seq); |
| 1045 | |
| 1046 | /* Account D-SACK for retransmitted packet. */ |
| 1047 | if ((dup_sack && in_sack) && |
| 1048 | (sacked & TCPCB_RETRANS) && |
| 1049 | after(TCP_SKB_CB(skb)->end_seq, tp->undo_marker)) |
| 1050 | tp->undo_retrans--; |
| 1051 | |
| 1052 | /* The frame is ACKed. */ |
| 1053 | if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una)) { |
| 1054 | if (sacked&TCPCB_RETRANS) { |
| 1055 | if ((dup_sack && in_sack) && |
| 1056 | (sacked&TCPCB_SACKED_ACKED)) |
| 1057 | reord = min(fack_count, reord); |
| 1058 | } else { |
| 1059 | /* If it was in a hole, we detected reordering. */ |
| 1060 | if (fack_count < prior_fackets && |
| 1061 | !(sacked&TCPCB_SACKED_ACKED)) |
| 1062 | reord = min(fack_count, reord); |
| 1063 | } |
| 1064 | |
| 1065 | /* Nothing to do; acked frame is about to be dropped. */ |
| 1066 | continue; |
| 1067 | } |
| 1068 | |
| 1069 | if ((sacked&TCPCB_SACKED_RETRANS) && |
| 1070 | after(end_seq, TCP_SKB_CB(skb)->ack_seq) && |
| 1071 | (!lost_retrans || after(end_seq, lost_retrans))) |
| 1072 | lost_retrans = end_seq; |
| 1073 | |
| 1074 | if (!in_sack) |
| 1075 | continue; |
| 1076 | |
| 1077 | if (!(sacked&TCPCB_SACKED_ACKED)) { |
| 1078 | if (sacked & TCPCB_SACKED_RETRANS) { |
| 1079 | /* If the segment is not tagged as lost, |
| 1080 | * we do not clear RETRANS, believing |
| 1081 | * that retransmission is still in flight. |
| 1082 | */ |
| 1083 | if (sacked & TCPCB_LOST) { |
| 1084 | TCP_SKB_CB(skb)->sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS); |
| 1085 | tp->lost_out -= tcp_skb_pcount(skb); |
| 1086 | tp->retrans_out -= tcp_skb_pcount(skb); |
| 1087 | } |
| 1088 | } else { |
| 1089 | /* New sack for not retransmitted frame, |
| 1090 | * which was in hole. It is reordering. |
| 1091 | */ |
| 1092 | if (!(sacked & TCPCB_RETRANS) && |
| 1093 | fack_count < prior_fackets) |
| 1094 | reord = min(fack_count, reord); |
| 1095 | |
| 1096 | if (sacked & TCPCB_LOST) { |
| 1097 | TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST; |
| 1098 | tp->lost_out -= tcp_skb_pcount(skb); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | TCP_SKB_CB(skb)->sacked |= TCPCB_SACKED_ACKED; |
| 1103 | flag |= FLAG_DATA_SACKED; |
| 1104 | tp->sacked_out += tcp_skb_pcount(skb); |
| 1105 | |
| 1106 | if (fack_count > tp->fackets_out) |
| 1107 | tp->fackets_out = fack_count; |
| 1108 | } else { |
| 1109 | if (dup_sack && (sacked&TCPCB_RETRANS)) |
| 1110 | reord = min(fack_count, reord); |
| 1111 | } |
| 1112 | |
| 1113 | /* D-SACK. We can detect redundant retransmission |
| 1114 | * in S|R and plain R frames and clear it. |
| 1115 | * undo_retrans is decreased above, L|R frames |
| 1116 | * are accounted above as well. |
| 1117 | */ |
| 1118 | if (dup_sack && |
| 1119 | (TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS)) { |
| 1120 | TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS; |
| 1121 | tp->retrans_out -= tcp_skb_pcount(skb); |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | /* Check for lost retransmit. This superb idea is |
| 1127 | * borrowed from "ratehalving". Event "C". |
| 1128 | * Later note: FACK people cheated me again 8), |
| 1129 | * we have to account for reordering! Ugly, |
| 1130 | * but should help. |
| 1131 | */ |
| 1132 | if (lost_retrans && tp->ca_state == TCP_CA_Recovery) { |
| 1133 | struct sk_buff *skb; |
| 1134 | |
| 1135 | sk_stream_for_retrans_queue(skb, sk) { |
| 1136 | if (after(TCP_SKB_CB(skb)->seq, lost_retrans)) |
| 1137 | break; |
| 1138 | if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una)) |
| 1139 | continue; |
| 1140 | if ((TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS) && |
| 1141 | after(lost_retrans, TCP_SKB_CB(skb)->ack_seq) && |
| 1142 | (IsFack(tp) || |
| 1143 | !before(lost_retrans, |
| 1144 | TCP_SKB_CB(skb)->ack_seq + tp->reordering * |
| 1145 | tp->mss_cache_std))) { |
| 1146 | TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS; |
| 1147 | tp->retrans_out -= tcp_skb_pcount(skb); |
| 1148 | |
| 1149 | if (!(TCP_SKB_CB(skb)->sacked&(TCPCB_LOST|TCPCB_SACKED_ACKED))) { |
| 1150 | tp->lost_out += tcp_skb_pcount(skb); |
| 1151 | TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; |
| 1152 | flag |= FLAG_DATA_SACKED; |
| 1153 | NET_INC_STATS_BH(LINUX_MIB_TCPLOSTRETRANSMIT); |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | tp->left_out = tp->sacked_out + tp->lost_out; |
| 1160 | |
| 1161 | if ((reord < tp->fackets_out) && tp->ca_state != TCP_CA_Loss) |
| 1162 | tcp_update_reordering(tp, ((tp->fackets_out + 1) - reord), 0); |
| 1163 | |
| 1164 | #if FASTRETRANS_DEBUG > 0 |
| 1165 | BUG_TRAP((int)tp->sacked_out >= 0); |
| 1166 | BUG_TRAP((int)tp->lost_out >= 0); |
| 1167 | BUG_TRAP((int)tp->retrans_out >= 0); |
| 1168 | BUG_TRAP((int)tcp_packets_in_flight(tp) >= 0); |
| 1169 | #endif |
| 1170 | return flag; |
| 1171 | } |
| 1172 | |
| 1173 | /* RTO occurred, but do not yet enter loss state. Instead, transmit two new |
| 1174 | * segments to see from the next ACKs whether any data was really missing. |
| 1175 | * If the RTO was spurious, new ACKs should arrive. |
| 1176 | */ |
| 1177 | void tcp_enter_frto(struct sock *sk) |
| 1178 | { |
| 1179 | struct tcp_sock *tp = tcp_sk(sk); |
| 1180 | struct sk_buff *skb; |
| 1181 | |
| 1182 | tp->frto_counter = 1; |
| 1183 | |
| 1184 | if (tp->ca_state <= TCP_CA_Disorder || |
| 1185 | tp->snd_una == tp->high_seq || |
| 1186 | (tp->ca_state == TCP_CA_Loss && !tp->retransmits)) { |
| 1187 | tp->prior_ssthresh = tcp_current_ssthresh(tp); |
| 1188 | if (!tcp_westwood_ssthresh(tp)) |
| 1189 | tp->snd_ssthresh = tcp_recalc_ssthresh(tp); |
| 1190 | } |
| 1191 | |
| 1192 | /* Have to clear retransmission markers here to keep the bookkeeping |
| 1193 | * in shape, even though we are not yet in Loss state. |
| 1194 | * If something was really lost, it is eventually caught up |
| 1195 | * in tcp_enter_frto_loss. |
| 1196 | */ |
| 1197 | tp->retrans_out = 0; |
| 1198 | tp->undo_marker = tp->snd_una; |
| 1199 | tp->undo_retrans = 0; |
| 1200 | |
| 1201 | sk_stream_for_retrans_queue(skb, sk) { |
| 1202 | TCP_SKB_CB(skb)->sacked &= ~TCPCB_RETRANS; |
| 1203 | } |
| 1204 | tcp_sync_left_out(tp); |
| 1205 | |
| 1206 | tcp_set_ca_state(tp, TCP_CA_Open); |
| 1207 | tp->frto_highmark = tp->snd_nxt; |
| 1208 | } |
| 1209 | |
| 1210 | /* Enter Loss state after F-RTO was applied. Dupack arrived after RTO, |
| 1211 | * which indicates that we should follow the traditional RTO recovery, |
| 1212 | * i.e. mark everything lost and do go-back-N retransmission. |
| 1213 | */ |
| 1214 | static void tcp_enter_frto_loss(struct sock *sk) |
| 1215 | { |
| 1216 | struct tcp_sock *tp = tcp_sk(sk); |
| 1217 | struct sk_buff *skb; |
| 1218 | int cnt = 0; |
| 1219 | |
| 1220 | tp->sacked_out = 0; |
| 1221 | tp->lost_out = 0; |
| 1222 | tp->fackets_out = 0; |
| 1223 | |
| 1224 | sk_stream_for_retrans_queue(skb, sk) { |
| 1225 | cnt += tcp_skb_pcount(skb); |
| 1226 | TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST; |
| 1227 | if (!(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED)) { |
| 1228 | |
| 1229 | /* Do not mark those segments lost that were |
| 1230 | * forward transmitted after RTO |
| 1231 | */ |
| 1232 | if (!after(TCP_SKB_CB(skb)->end_seq, |
| 1233 | tp->frto_highmark)) { |
| 1234 | TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; |
| 1235 | tp->lost_out += tcp_skb_pcount(skb); |
| 1236 | } |
| 1237 | } else { |
| 1238 | tp->sacked_out += tcp_skb_pcount(skb); |
| 1239 | tp->fackets_out = cnt; |
| 1240 | } |
| 1241 | } |
| 1242 | tcp_sync_left_out(tp); |
| 1243 | |
| 1244 | tp->snd_cwnd = tp->frto_counter + tcp_packets_in_flight(tp)+1; |
| 1245 | tp->snd_cwnd_cnt = 0; |
| 1246 | tp->snd_cwnd_stamp = tcp_time_stamp; |
| 1247 | tp->undo_marker = 0; |
| 1248 | tp->frto_counter = 0; |
| 1249 | |
| 1250 | tp->reordering = min_t(unsigned int, tp->reordering, |
| 1251 | sysctl_tcp_reordering); |
| 1252 | tcp_set_ca_state(tp, TCP_CA_Loss); |
| 1253 | tp->high_seq = tp->frto_highmark; |
| 1254 | TCP_ECN_queue_cwr(tp); |
| 1255 | |
| 1256 | init_bictcp(tp); |
| 1257 | } |
| 1258 | |
| 1259 | void tcp_clear_retrans(struct tcp_sock *tp) |
| 1260 | { |
| 1261 | tp->left_out = 0; |
| 1262 | tp->retrans_out = 0; |
| 1263 | |
| 1264 | tp->fackets_out = 0; |
| 1265 | tp->sacked_out = 0; |
| 1266 | tp->lost_out = 0; |
| 1267 | |
| 1268 | tp->undo_marker = 0; |
| 1269 | tp->undo_retrans = 0; |
| 1270 | } |
| 1271 | |
| 1272 | /* Enter Loss state. If "how" is not zero, forget all SACK information |
| 1273 | * and reset tags completely, otherwise preserve SACKs. If receiver |
| 1274 | * dropped its ofo queue, we will know this due to reneging detection. |
| 1275 | */ |
| 1276 | void tcp_enter_loss(struct sock *sk, int how) |
| 1277 | { |
| 1278 | struct tcp_sock *tp = tcp_sk(sk); |
| 1279 | struct sk_buff *skb; |
| 1280 | int cnt = 0; |
| 1281 | |
| 1282 | /* Reduce ssthresh if it has not yet been made inside this window. */ |
| 1283 | if (tp->ca_state <= TCP_CA_Disorder || tp->snd_una == tp->high_seq || |
| 1284 | (tp->ca_state == TCP_CA_Loss && !tp->retransmits)) { |
| 1285 | tp->prior_ssthresh = tcp_current_ssthresh(tp); |
| 1286 | tp->snd_ssthresh = tcp_recalc_ssthresh(tp); |
| 1287 | } |
| 1288 | tp->snd_cwnd = 1; |
| 1289 | tp->snd_cwnd_cnt = 0; |
| 1290 | tp->snd_cwnd_stamp = tcp_time_stamp; |
| 1291 | |
| 1292 | tcp_clear_retrans(tp); |
| 1293 | |
| 1294 | /* Push undo marker, if it was plain RTO and nothing |
| 1295 | * was retransmitted. */ |
| 1296 | if (!how) |
| 1297 | tp->undo_marker = tp->snd_una; |
| 1298 | |
| 1299 | sk_stream_for_retrans_queue(skb, sk) { |
| 1300 | cnt += tcp_skb_pcount(skb); |
| 1301 | if (TCP_SKB_CB(skb)->sacked&TCPCB_RETRANS) |
| 1302 | tp->undo_marker = 0; |
| 1303 | TCP_SKB_CB(skb)->sacked &= (~TCPCB_TAGBITS)|TCPCB_SACKED_ACKED; |
| 1304 | if (!(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED) || how) { |
| 1305 | TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED; |
| 1306 | TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; |
| 1307 | tp->lost_out += tcp_skb_pcount(skb); |
| 1308 | } else { |
| 1309 | tp->sacked_out += tcp_skb_pcount(skb); |
| 1310 | tp->fackets_out = cnt; |
| 1311 | } |
| 1312 | } |
| 1313 | tcp_sync_left_out(tp); |
| 1314 | |
| 1315 | tp->reordering = min_t(unsigned int, tp->reordering, |
| 1316 | sysctl_tcp_reordering); |
| 1317 | tcp_set_ca_state(tp, TCP_CA_Loss); |
| 1318 | tp->high_seq = tp->snd_nxt; |
| 1319 | TCP_ECN_queue_cwr(tp); |
| 1320 | } |
| 1321 | |
| 1322 | static int tcp_check_sack_reneging(struct sock *sk, struct tcp_sock *tp) |
| 1323 | { |
| 1324 | struct sk_buff *skb; |
| 1325 | |
| 1326 | /* If ACK arrived pointing to a remembered SACK, |
| 1327 | * it means that our remembered SACKs do not reflect |
| 1328 | * real state of receiver i.e. |
| 1329 | * receiver _host_ is heavily congested (or buggy). |
| 1330 | * Do processing similar to RTO timeout. |
| 1331 | */ |
| 1332 | if ((skb = skb_peek(&sk->sk_write_queue)) != NULL && |
| 1333 | (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) { |
| 1334 | NET_INC_STATS_BH(LINUX_MIB_TCPSACKRENEGING); |
| 1335 | |
| 1336 | tcp_enter_loss(sk, 1); |
| 1337 | tp->retransmits++; |
| 1338 | tcp_retransmit_skb(sk, skb_peek(&sk->sk_write_queue)); |
| 1339 | tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto); |
| 1340 | return 1; |
| 1341 | } |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | static inline int tcp_fackets_out(struct tcp_sock *tp) |
| 1346 | { |
| 1347 | return IsReno(tp) ? tp->sacked_out+1 : tp->fackets_out; |
| 1348 | } |
| 1349 | |
| 1350 | static inline int tcp_skb_timedout(struct tcp_sock *tp, struct sk_buff *skb) |
| 1351 | { |
| 1352 | return (tcp_time_stamp - TCP_SKB_CB(skb)->when > tp->rto); |
| 1353 | } |
| 1354 | |
| 1355 | static inline int tcp_head_timedout(struct sock *sk, struct tcp_sock *tp) |
| 1356 | { |
| 1357 | return tp->packets_out && |
| 1358 | tcp_skb_timedout(tp, skb_peek(&sk->sk_write_queue)); |
| 1359 | } |
| 1360 | |
| 1361 | /* Linux NewReno/SACK/FACK/ECN state machine. |
| 1362 | * -------------------------------------- |
| 1363 | * |
| 1364 | * "Open" Normal state, no dubious events, fast path. |
| 1365 | * "Disorder" In all the respects it is "Open", |
| 1366 | * but requires a bit more attention. It is entered when |
| 1367 | * we see some SACKs or dupacks. It is split of "Open" |
| 1368 | * mainly to move some processing from fast path to slow one. |
| 1369 | * "CWR" CWND was reduced due to some Congestion Notification event. |
| 1370 | * It can be ECN, ICMP source quench, local device congestion. |
| 1371 | * "Recovery" CWND was reduced, we are fast-retransmitting. |
| 1372 | * "Loss" CWND was reduced due to RTO timeout or SACK reneging. |
| 1373 | * |
| 1374 | * tcp_fastretrans_alert() is entered: |
| 1375 | * - each incoming ACK, if state is not "Open" |
| 1376 | * - when arrived ACK is unusual, namely: |
| 1377 | * * SACK |
| 1378 | * * Duplicate ACK. |
| 1379 | * * ECN ECE. |
| 1380 | * |
| 1381 | * Counting packets in flight is pretty simple. |
| 1382 | * |
| 1383 | * in_flight = packets_out - left_out + retrans_out |
| 1384 | * |
| 1385 | * packets_out is SND.NXT-SND.UNA counted in packets. |
| 1386 | * |
| 1387 | * retrans_out is number of retransmitted segments. |
| 1388 | * |
| 1389 | * left_out is number of segments left network, but not ACKed yet. |
| 1390 | * |
| 1391 | * left_out = sacked_out + lost_out |
| 1392 | * |
| 1393 | * sacked_out: Packets, which arrived to receiver out of order |
| 1394 | * and hence not ACKed. With SACKs this number is simply |
| 1395 | * amount of SACKed data. Even without SACKs |
| 1396 | * it is easy to give pretty reliable estimate of this number, |
| 1397 | * counting duplicate ACKs. |
| 1398 | * |
| 1399 | * lost_out: Packets lost by network. TCP has no explicit |
| 1400 | * "loss notification" feedback from network (for now). |
| 1401 | * It means that this number can be only _guessed_. |
| 1402 | * Actually, it is the heuristics to predict lossage that |
| 1403 | * distinguishes different algorithms. |
| 1404 | * |
| 1405 | * F.e. after RTO, when all the queue is considered as lost, |
| 1406 | * lost_out = packets_out and in_flight = retrans_out. |
| 1407 | * |
| 1408 | * Essentially, we have now two algorithms counting |
| 1409 | * lost packets. |
| 1410 | * |
| 1411 | * FACK: It is the simplest heuristics. As soon as we decided |
| 1412 | * that something is lost, we decide that _all_ not SACKed |
| 1413 | * packets until the most forward SACK are lost. I.e. |
| 1414 | * lost_out = fackets_out - sacked_out and left_out = fackets_out. |
| 1415 | * It is absolutely correct estimate, if network does not reorder |
| 1416 | * packets. And it loses any connection to reality when reordering |
| 1417 | * takes place. We use FACK by default until reordering |
| 1418 | * is suspected on the path to this destination. |
| 1419 | * |
| 1420 | * NewReno: when Recovery is entered, we assume that one segment |
| 1421 | * is lost (classic Reno). While we are in Recovery and |
| 1422 | * a partial ACK arrives, we assume that one more packet |
| 1423 | * is lost (NewReno). This heuristics are the same in NewReno |
| 1424 | * and SACK. |
| 1425 | * |
| 1426 | * Imagine, that's all! Forget about all this shamanism about CWND inflation |
| 1427 | * deflation etc. CWND is real congestion window, never inflated, changes |
| 1428 | * only according to classic VJ rules. |
| 1429 | * |
| 1430 | * Really tricky (and requiring careful tuning) part of algorithm |
| 1431 | * is hidden in functions tcp_time_to_recover() and tcp_xmit_retransmit_queue(). |
| 1432 | * The first determines the moment _when_ we should reduce CWND and, |
| 1433 | * hence, slow down forward transmission. In fact, it determines the moment |
| 1434 | * when we decide that hole is caused by loss, rather than by a reorder. |
| 1435 | * |
| 1436 | * tcp_xmit_retransmit_queue() decides, _what_ we should retransmit to fill |
| 1437 | * holes, caused by lost packets. |
| 1438 | * |
| 1439 | * And the most logically complicated part of algorithm is undo |
| 1440 | * heuristics. We detect false retransmits due to both too early |
| 1441 | * fast retransmit (reordering) and underestimated RTO, analyzing |
| 1442 | * timestamps and D-SACKs. When we detect that some segments were |
| 1443 | * retransmitted by mistake and CWND reduction was wrong, we undo |
| 1444 | * window reduction and abort recovery phase. This logic is hidden |
| 1445 | * inside several functions named tcp_try_undo_<something>. |
| 1446 | */ |
| 1447 | |
| 1448 | /* This function decides, when we should leave Disordered state |
| 1449 | * and enter Recovery phase, reducing congestion window. |
| 1450 | * |
| 1451 | * Main question: may we further continue forward transmission |
| 1452 | * with the same cwnd? |
| 1453 | */ |
| 1454 | static int tcp_time_to_recover(struct sock *sk, struct tcp_sock *tp) |
| 1455 | { |
| 1456 | __u32 packets_out; |
| 1457 | |
| 1458 | /* Trick#1: The loss is proven. */ |
| 1459 | if (tp->lost_out) |
| 1460 | return 1; |
| 1461 | |
| 1462 | /* Not-A-Trick#2 : Classic rule... */ |
| 1463 | if (tcp_fackets_out(tp) > tp->reordering) |
| 1464 | return 1; |
| 1465 | |
| 1466 | /* Trick#3 : when we use RFC2988 timer restart, fast |
| 1467 | * retransmit can be triggered by timeout of queue head. |
| 1468 | */ |
| 1469 | if (tcp_head_timedout(sk, tp)) |
| 1470 | return 1; |
| 1471 | |
| 1472 | /* Trick#4: It is still not OK... But will it be useful to delay |
| 1473 | * recovery more? |
| 1474 | */ |
| 1475 | packets_out = tp->packets_out; |
| 1476 | if (packets_out <= tp->reordering && |
| 1477 | tp->sacked_out >= max_t(__u32, packets_out/2, sysctl_tcp_reordering) && |
| 1478 | !tcp_may_send_now(sk, tp)) { |
| 1479 | /* We have nothing to send. This connection is limited |
| 1480 | * either by receiver window or by application. |
| 1481 | */ |
| 1482 | return 1; |
| 1483 | } |
| 1484 | |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | /* If we receive more dupacks than we expected counting segments |
| 1489 | * in assumption of absent reordering, interpret this as reordering. |
| 1490 | * The only another reason could be bug in receiver TCP. |
| 1491 | */ |
| 1492 | static void tcp_check_reno_reordering(struct tcp_sock *tp, int addend) |
| 1493 | { |
| 1494 | u32 holes; |
| 1495 | |
| 1496 | holes = max(tp->lost_out, 1U); |
| 1497 | holes = min(holes, tp->packets_out); |
| 1498 | |
| 1499 | if ((tp->sacked_out + holes) > tp->packets_out) { |
| 1500 | tp->sacked_out = tp->packets_out - holes; |
| 1501 | tcp_update_reordering(tp, tp->packets_out+addend, 0); |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | /* Emulate SACKs for SACKless connection: account for a new dupack. */ |
| 1506 | |
| 1507 | static void tcp_add_reno_sack(struct tcp_sock *tp) |
| 1508 | { |
| 1509 | tp->sacked_out++; |
| 1510 | tcp_check_reno_reordering(tp, 0); |
| 1511 | tcp_sync_left_out(tp); |
| 1512 | } |
| 1513 | |
| 1514 | /* Account for ACK, ACKing some data in Reno Recovery phase. */ |
| 1515 | |
| 1516 | static void tcp_remove_reno_sacks(struct sock *sk, struct tcp_sock *tp, int acked) |
| 1517 | { |
| 1518 | if (acked > 0) { |
| 1519 | /* One ACK acked hole. The rest eat duplicate ACKs. */ |
| 1520 | if (acked-1 >= tp->sacked_out) |
| 1521 | tp->sacked_out = 0; |
| 1522 | else |
| 1523 | tp->sacked_out -= acked-1; |
| 1524 | } |
| 1525 | tcp_check_reno_reordering(tp, acked); |
| 1526 | tcp_sync_left_out(tp); |
| 1527 | } |
| 1528 | |
| 1529 | static inline void tcp_reset_reno_sack(struct tcp_sock *tp) |
| 1530 | { |
| 1531 | tp->sacked_out = 0; |
| 1532 | tp->left_out = tp->lost_out; |
| 1533 | } |
| 1534 | |
| 1535 | /* Mark head of queue up as lost. */ |
| 1536 | static void tcp_mark_head_lost(struct sock *sk, struct tcp_sock *tp, |
| 1537 | int packets, u32 high_seq) |
| 1538 | { |
| 1539 | struct sk_buff *skb; |
| 1540 | int cnt = packets; |
| 1541 | |
| 1542 | BUG_TRAP(cnt <= tp->packets_out); |
| 1543 | |
| 1544 | sk_stream_for_retrans_queue(skb, sk) { |
| 1545 | cnt -= tcp_skb_pcount(skb); |
| 1546 | if (cnt < 0 || after(TCP_SKB_CB(skb)->end_seq, high_seq)) |
| 1547 | break; |
| 1548 | if (!(TCP_SKB_CB(skb)->sacked&TCPCB_TAGBITS)) { |
| 1549 | TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; |
| 1550 | tp->lost_out += tcp_skb_pcount(skb); |
| 1551 | } |
| 1552 | } |
| 1553 | tcp_sync_left_out(tp); |
| 1554 | } |
| 1555 | |
| 1556 | /* Account newly detected lost packet(s) */ |
| 1557 | |
| 1558 | static void tcp_update_scoreboard(struct sock *sk, struct tcp_sock *tp) |
| 1559 | { |
| 1560 | if (IsFack(tp)) { |
| 1561 | int lost = tp->fackets_out - tp->reordering; |
| 1562 | if (lost <= 0) |
| 1563 | lost = 1; |
| 1564 | tcp_mark_head_lost(sk, tp, lost, tp->high_seq); |
| 1565 | } else { |
| 1566 | tcp_mark_head_lost(sk, tp, 1, tp->high_seq); |
| 1567 | } |
| 1568 | |
| 1569 | /* New heuristics: it is possible only after we switched |
| 1570 | * to restart timer each time when something is ACKed. |
| 1571 | * Hence, we can detect timed out packets during fast |
| 1572 | * retransmit without falling to slow start. |
| 1573 | */ |
| 1574 | if (tcp_head_timedout(sk, tp)) { |
| 1575 | struct sk_buff *skb; |
| 1576 | |
| 1577 | sk_stream_for_retrans_queue(skb, sk) { |
| 1578 | if (tcp_skb_timedout(tp, skb) && |
| 1579 | !(TCP_SKB_CB(skb)->sacked&TCPCB_TAGBITS)) { |
| 1580 | TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; |
| 1581 | tp->lost_out += tcp_skb_pcount(skb); |
| 1582 | } |
| 1583 | } |
| 1584 | tcp_sync_left_out(tp); |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | /* CWND moderation, preventing bursts due to too big ACKs |
| 1589 | * in dubious situations. |
| 1590 | */ |
| 1591 | static inline void tcp_moderate_cwnd(struct tcp_sock *tp) |
| 1592 | { |
| 1593 | tp->snd_cwnd = min(tp->snd_cwnd, |
| 1594 | tcp_packets_in_flight(tp)+tcp_max_burst(tp)); |
| 1595 | tp->snd_cwnd_stamp = tcp_time_stamp; |
| 1596 | } |
| 1597 | |
| 1598 | /* Decrease cwnd each second ack. */ |
| 1599 | |
| 1600 | static void tcp_cwnd_down(struct tcp_sock *tp) |
| 1601 | { |
| 1602 | int decr = tp->snd_cwnd_cnt + 1; |
| 1603 | __u32 limit; |
| 1604 | |
| 1605 | /* |
| 1606 | * TCP Westwood |
| 1607 | * Here limit is evaluated as BWestimation*RTTmin (for obtaining it |
| 1608 | * in packets we use mss_cache). If sysctl_tcp_westwood is off |
| 1609 | * tcp_westwood_bw_rttmin() returns 0. In such case snd_ssthresh is |
| 1610 | * still used as usual. It prevents other strange cases in which |
| 1611 | * BWE*RTTmin could assume value 0. It should not happen but... |
| 1612 | */ |
| 1613 | |
| 1614 | if (!(limit = tcp_westwood_bw_rttmin(tp))) |
| 1615 | limit = tp->snd_ssthresh/2; |
| 1616 | |
| 1617 | tp->snd_cwnd_cnt = decr&1; |
| 1618 | decr >>= 1; |
| 1619 | |
| 1620 | if (decr && tp->snd_cwnd > limit) |
| 1621 | tp->snd_cwnd -= decr; |
| 1622 | |
| 1623 | tp->snd_cwnd = min(tp->snd_cwnd, tcp_packets_in_flight(tp)+1); |
| 1624 | tp->snd_cwnd_stamp = tcp_time_stamp; |
| 1625 | } |
| 1626 | |
| 1627 | /* Nothing was retransmitted or returned timestamp is less |
| 1628 | * than timestamp of the first retransmission. |
| 1629 | */ |
| 1630 | static inline int tcp_packet_delayed(struct tcp_sock *tp) |
| 1631 | { |
| 1632 | return !tp->retrans_stamp || |
| 1633 | (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && |
| 1634 | (__s32)(tp->rx_opt.rcv_tsecr - tp->retrans_stamp) < 0); |
| 1635 | } |
| 1636 | |
| 1637 | /* Undo procedures. */ |
| 1638 | |
| 1639 | #if FASTRETRANS_DEBUG > 1 |
| 1640 | static void DBGUNDO(struct sock *sk, struct tcp_sock *tp, const char *msg) |
| 1641 | { |
| 1642 | struct inet_sock *inet = inet_sk(sk); |
| 1643 | printk(KERN_DEBUG "Undo %s %u.%u.%u.%u/%u c%u l%u ss%u/%u p%u\n", |
| 1644 | msg, |
| 1645 | NIPQUAD(inet->daddr), ntohs(inet->dport), |
| 1646 | tp->snd_cwnd, tp->left_out, |
| 1647 | tp->snd_ssthresh, tp->prior_ssthresh, |
| 1648 | tp->packets_out); |
| 1649 | } |
| 1650 | #else |
| 1651 | #define DBGUNDO(x...) do { } while (0) |
| 1652 | #endif |
| 1653 | |
| 1654 | static void tcp_undo_cwr(struct tcp_sock *tp, int undo) |
| 1655 | { |
| 1656 | if (tp->prior_ssthresh) { |
| 1657 | if (tcp_is_bic(tp)) |
| 1658 | tp->snd_cwnd = max(tp->snd_cwnd, tp->bictcp.last_max_cwnd); |
| 1659 | else |
| 1660 | tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh<<1); |
| 1661 | |
| 1662 | if (undo && tp->prior_ssthresh > tp->snd_ssthresh) { |
| 1663 | tp->snd_ssthresh = tp->prior_ssthresh; |
| 1664 | TCP_ECN_withdraw_cwr(tp); |
| 1665 | } |
| 1666 | } else { |
| 1667 | tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh); |
| 1668 | } |
| 1669 | tcp_moderate_cwnd(tp); |
| 1670 | tp->snd_cwnd_stamp = tcp_time_stamp; |
| 1671 | } |
| 1672 | |
| 1673 | static inline int tcp_may_undo(struct tcp_sock *tp) |
| 1674 | { |
| 1675 | return tp->undo_marker && |
| 1676 | (!tp->undo_retrans || tcp_packet_delayed(tp)); |
| 1677 | } |
| 1678 | |
| 1679 | /* People celebrate: "We love our President!" */ |
| 1680 | static int tcp_try_undo_recovery(struct sock *sk, struct tcp_sock *tp) |
| 1681 | { |
| 1682 | if (tcp_may_undo(tp)) { |
| 1683 | /* Happy end! We did not retransmit anything |
| 1684 | * or our original transmission succeeded. |
| 1685 | */ |
| 1686 | DBGUNDO(sk, tp, tp->ca_state == TCP_CA_Loss ? "loss" : "retrans"); |
| 1687 | tcp_undo_cwr(tp, 1); |
| 1688 | if (tp->ca_state == TCP_CA_Loss) |
| 1689 | NET_INC_STATS_BH(LINUX_MIB_TCPLOSSUNDO); |
| 1690 | else |
| 1691 | NET_INC_STATS_BH(LINUX_MIB_TCPFULLUNDO); |
| 1692 | tp->undo_marker = 0; |
| 1693 | } |
| 1694 | if (tp->snd_una == tp->high_seq && IsReno(tp)) { |
| 1695 | /* Hold old state until something *above* high_seq |
| 1696 | * is ACKed. For Reno it is MUST to prevent false |
| 1697 | * fast retransmits (RFC2582). SACK TCP is safe. */ |
| 1698 | tcp_moderate_cwnd(tp); |
| 1699 | return 1; |
| 1700 | } |
| 1701 | tcp_set_ca_state(tp, TCP_CA_Open); |
| 1702 | return 0; |
| 1703 | } |
| 1704 | |
| 1705 | /* Try to undo cwnd reduction, because D-SACKs acked all retransmitted data */ |
| 1706 | static void tcp_try_undo_dsack(struct sock *sk, struct tcp_sock *tp) |
| 1707 | { |
| 1708 | if (tp->undo_marker && !tp->undo_retrans) { |
| 1709 | DBGUNDO(sk, tp, "D-SACK"); |
| 1710 | tcp_undo_cwr(tp, 1); |
| 1711 | tp->undo_marker = 0; |
| 1712 | NET_INC_STATS_BH(LINUX_MIB_TCPDSACKUNDO); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | /* Undo during fast recovery after partial ACK. */ |
| 1717 | |
| 1718 | static int tcp_try_undo_partial(struct sock *sk, struct tcp_sock *tp, |
| 1719 | int acked) |
| 1720 | { |
| 1721 | /* Partial ACK arrived. Force Hoe's retransmit. */ |
| 1722 | int failed = IsReno(tp) || tp->fackets_out>tp->reordering; |
| 1723 | |
| 1724 | if (tcp_may_undo(tp)) { |
| 1725 | /* Plain luck! Hole if filled with delayed |
| 1726 | * packet, rather than with a retransmit. |
| 1727 | */ |
| 1728 | if (tp->retrans_out == 0) |
| 1729 | tp->retrans_stamp = 0; |
| 1730 | |
| 1731 | tcp_update_reordering(tp, tcp_fackets_out(tp)+acked, 1); |
| 1732 | |
| 1733 | DBGUNDO(sk, tp, "Hoe"); |
| 1734 | tcp_undo_cwr(tp, 0); |
| 1735 | NET_INC_STATS_BH(LINUX_MIB_TCPPARTIALUNDO); |
| 1736 | |
| 1737 | /* So... Do not make Hoe's retransmit yet. |
| 1738 | * If the first packet was delayed, the rest |
| 1739 | * ones are most probably delayed as well. |
| 1740 | */ |
| 1741 | failed = 0; |
| 1742 | } |
| 1743 | return failed; |
| 1744 | } |
| 1745 | |
| 1746 | /* Undo during loss recovery after partial ACK. */ |
| 1747 | static int tcp_try_undo_loss(struct sock *sk, struct tcp_sock *tp) |
| 1748 | { |
| 1749 | if (tcp_may_undo(tp)) { |
| 1750 | struct sk_buff *skb; |
| 1751 | sk_stream_for_retrans_queue(skb, sk) { |
| 1752 | TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST; |
| 1753 | } |
| 1754 | DBGUNDO(sk, tp, "partial loss"); |
| 1755 | tp->lost_out = 0; |
| 1756 | tp->left_out = tp->sacked_out; |
| 1757 | tcp_undo_cwr(tp, 1); |
| 1758 | NET_INC_STATS_BH(LINUX_MIB_TCPLOSSUNDO); |
| 1759 | tp->retransmits = 0; |
| 1760 | tp->undo_marker = 0; |
| 1761 | if (!IsReno(tp)) |
| 1762 | tcp_set_ca_state(tp, TCP_CA_Open); |
| 1763 | return 1; |
| 1764 | } |
| 1765 | return 0; |
| 1766 | } |
| 1767 | |
| 1768 | static inline void tcp_complete_cwr(struct tcp_sock *tp) |
| 1769 | { |
| 1770 | if (tcp_westwood_cwnd(tp)) |
| 1771 | tp->snd_ssthresh = tp->snd_cwnd; |
| 1772 | else |
| 1773 | tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh); |
| 1774 | tp->snd_cwnd_stamp = tcp_time_stamp; |
| 1775 | } |
| 1776 | |
| 1777 | static void tcp_try_to_open(struct sock *sk, struct tcp_sock *tp, int flag) |
| 1778 | { |
| 1779 | tp->left_out = tp->sacked_out; |
| 1780 | |
| 1781 | if (tp->retrans_out == 0) |
| 1782 | tp->retrans_stamp = 0; |
| 1783 | |
| 1784 | if (flag&FLAG_ECE) |
| 1785 | tcp_enter_cwr(tp); |
| 1786 | |
| 1787 | if (tp->ca_state != TCP_CA_CWR) { |
| 1788 | int state = TCP_CA_Open; |
| 1789 | |
| 1790 | if (tp->left_out || tp->retrans_out || tp->undo_marker) |
| 1791 | state = TCP_CA_Disorder; |
| 1792 | |
| 1793 | if (tp->ca_state != state) { |
| 1794 | tcp_set_ca_state(tp, state); |
| 1795 | tp->high_seq = tp->snd_nxt; |
| 1796 | } |
| 1797 | tcp_moderate_cwnd(tp); |
| 1798 | } else { |
| 1799 | tcp_cwnd_down(tp); |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | /* Process an event, which can update packets-in-flight not trivially. |
| 1804 | * Main goal of this function is to calculate new estimate for left_out, |
| 1805 | * taking into account both packets sitting in receiver's buffer and |
| 1806 | * packets lost by network. |
| 1807 | * |
| 1808 | * Besides that it does CWND reduction, when packet loss is detected |
| 1809 | * and changes state of machine. |
| 1810 | * |
| 1811 | * It does _not_ decide what to send, it is made in function |
| 1812 | * tcp_xmit_retransmit_queue(). |
| 1813 | */ |
| 1814 | static void |
| 1815 | tcp_fastretrans_alert(struct sock *sk, u32 prior_snd_una, |
| 1816 | int prior_packets, int flag) |
| 1817 | { |
| 1818 | struct tcp_sock *tp = tcp_sk(sk); |
| 1819 | int is_dupack = (tp->snd_una == prior_snd_una && !(flag&FLAG_NOT_DUP)); |
| 1820 | |
| 1821 | /* Some technical things: |
| 1822 | * 1. Reno does not count dupacks (sacked_out) automatically. */ |
| 1823 | if (!tp->packets_out) |
| 1824 | tp->sacked_out = 0; |
| 1825 | /* 2. SACK counts snd_fack in packets inaccurately. */ |
| 1826 | if (tp->sacked_out == 0) |
| 1827 | tp->fackets_out = 0; |
| 1828 | |
| 1829 | /* Now state machine starts. |
| 1830 | * A. ECE, hence prohibit cwnd undoing, the reduction is required. */ |
| 1831 | if (flag&FLAG_ECE) |
| 1832 | tp->prior_ssthresh = 0; |
| 1833 | |
| 1834 | /* B. In all the states check for reneging SACKs. */ |
| 1835 | if (tp->sacked_out && tcp_check_sack_reneging(sk, tp)) |
| 1836 | return; |
| 1837 | |
| 1838 | /* C. Process data loss notification, provided it is valid. */ |
| 1839 | if ((flag&FLAG_DATA_LOST) && |
| 1840 | before(tp->snd_una, tp->high_seq) && |
| 1841 | tp->ca_state != TCP_CA_Open && |
| 1842 | tp->fackets_out > tp->reordering) { |
| 1843 | tcp_mark_head_lost(sk, tp, tp->fackets_out-tp->reordering, tp->high_seq); |
| 1844 | NET_INC_STATS_BH(LINUX_MIB_TCPLOSS); |
| 1845 | } |
| 1846 | |
| 1847 | /* D. Synchronize left_out to current state. */ |
| 1848 | tcp_sync_left_out(tp); |
| 1849 | |
| 1850 | /* E. Check state exit conditions. State can be terminated |
| 1851 | * when high_seq is ACKed. */ |
| 1852 | if (tp->ca_state == TCP_CA_Open) { |
| 1853 | if (!sysctl_tcp_frto) |
| 1854 | BUG_TRAP(tp->retrans_out == 0); |
| 1855 | tp->retrans_stamp = 0; |
| 1856 | } else if (!before(tp->snd_una, tp->high_seq)) { |
| 1857 | switch (tp->ca_state) { |
| 1858 | case TCP_CA_Loss: |
| 1859 | tp->retransmits = 0; |
| 1860 | if (tcp_try_undo_recovery(sk, tp)) |
| 1861 | return; |
| 1862 | break; |
| 1863 | |
| 1864 | case TCP_CA_CWR: |
| 1865 | /* CWR is to be held something *above* high_seq |
| 1866 | * is ACKed for CWR bit to reach receiver. */ |
| 1867 | if (tp->snd_una != tp->high_seq) { |
| 1868 | tcp_complete_cwr(tp); |
| 1869 | tcp_set_ca_state(tp, TCP_CA_Open); |
| 1870 | } |
| 1871 | break; |
| 1872 | |
| 1873 | case TCP_CA_Disorder: |
| 1874 | tcp_try_undo_dsack(sk, tp); |
| 1875 | if (!tp->undo_marker || |
| 1876 | /* For SACK case do not Open to allow to undo |
| 1877 | * catching for all duplicate ACKs. */ |
| 1878 | IsReno(tp) || tp->snd_una != tp->high_seq) { |
| 1879 | tp->undo_marker = 0; |
| 1880 | tcp_set_ca_state(tp, TCP_CA_Open); |
| 1881 | } |
| 1882 | break; |
| 1883 | |
| 1884 | case TCP_CA_Recovery: |
| 1885 | if (IsReno(tp)) |
| 1886 | tcp_reset_reno_sack(tp); |
| 1887 | if (tcp_try_undo_recovery(sk, tp)) |
| 1888 | return; |
| 1889 | tcp_complete_cwr(tp); |
| 1890 | break; |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | /* F. Process state. */ |
| 1895 | switch (tp->ca_state) { |
| 1896 | case TCP_CA_Recovery: |
| 1897 | if (prior_snd_una == tp->snd_una) { |
| 1898 | if (IsReno(tp) && is_dupack) |
| 1899 | tcp_add_reno_sack(tp); |
| 1900 | } else { |
| 1901 | int acked = prior_packets - tp->packets_out; |
| 1902 | if (IsReno(tp)) |
| 1903 | tcp_remove_reno_sacks(sk, tp, acked); |
| 1904 | is_dupack = tcp_try_undo_partial(sk, tp, acked); |
| 1905 | } |
| 1906 | break; |
| 1907 | case TCP_CA_Loss: |
| 1908 | if (flag&FLAG_DATA_ACKED) |
| 1909 | tp->retransmits = 0; |
| 1910 | if (!tcp_try_undo_loss(sk, tp)) { |
| 1911 | tcp_moderate_cwnd(tp); |
| 1912 | tcp_xmit_retransmit_queue(sk); |
| 1913 | return; |
| 1914 | } |
| 1915 | if (tp->ca_state != TCP_CA_Open) |
| 1916 | return; |
| 1917 | /* Loss is undone; fall through to processing in Open state. */ |
| 1918 | default: |
| 1919 | if (IsReno(tp)) { |
| 1920 | if (tp->snd_una != prior_snd_una) |
| 1921 | tcp_reset_reno_sack(tp); |
| 1922 | if (is_dupack) |
| 1923 | tcp_add_reno_sack(tp); |
| 1924 | } |
| 1925 | |
| 1926 | if (tp->ca_state == TCP_CA_Disorder) |
| 1927 | tcp_try_undo_dsack(sk, tp); |
| 1928 | |
| 1929 | if (!tcp_time_to_recover(sk, tp)) { |
| 1930 | tcp_try_to_open(sk, tp, flag); |
| 1931 | return; |
| 1932 | } |
| 1933 | |
| 1934 | /* Otherwise enter Recovery state */ |
| 1935 | |
| 1936 | if (IsReno(tp)) |
| 1937 | NET_INC_STATS_BH(LINUX_MIB_TCPRENORECOVERY); |
| 1938 | else |
| 1939 | NET_INC_STATS_BH(LINUX_MIB_TCPSACKRECOVERY); |
| 1940 | |
| 1941 | tp->high_seq = tp->snd_nxt; |
| 1942 | tp->prior_ssthresh = 0; |
| 1943 | tp->undo_marker = tp->snd_una; |
| 1944 | tp->undo_retrans = tp->retrans_out; |
| 1945 | |
| 1946 | if (tp->ca_state < TCP_CA_CWR) { |
| 1947 | if (!(flag&FLAG_ECE)) |
| 1948 | tp->prior_ssthresh = tcp_current_ssthresh(tp); |
| 1949 | tp->snd_ssthresh = tcp_recalc_ssthresh(tp); |
| 1950 | TCP_ECN_queue_cwr(tp); |
| 1951 | } |
| 1952 | |
| 1953 | tp->snd_cwnd_cnt = 0; |
| 1954 | tcp_set_ca_state(tp, TCP_CA_Recovery); |
| 1955 | } |
| 1956 | |
| 1957 | if (is_dupack || tcp_head_timedout(sk, tp)) |
| 1958 | tcp_update_scoreboard(sk, tp); |
| 1959 | tcp_cwnd_down(tp); |
| 1960 | tcp_xmit_retransmit_queue(sk); |
| 1961 | } |
| 1962 | |
| 1963 | /* Read draft-ietf-tcplw-high-performance before mucking |
| 1964 | * with this code. (Superceeds RFC1323) |
| 1965 | */ |
| 1966 | static void tcp_ack_saw_tstamp(struct tcp_sock *tp, int flag) |
| 1967 | { |
| 1968 | __u32 seq_rtt; |
| 1969 | |
| 1970 | /* RTTM Rule: A TSecr value received in a segment is used to |
| 1971 | * update the averaged RTT measurement only if the segment |
| 1972 | * acknowledges some new data, i.e., only if it advances the |
| 1973 | * left edge of the send window. |
| 1974 | * |
| 1975 | * See draft-ietf-tcplw-high-performance-00, section 3.3. |
| 1976 | * 1998/04/10 Andrey V. Savochkin <saw@msu.ru> |
| 1977 | * |
| 1978 | * Changed: reset backoff as soon as we see the first valid sample. |
| 1979 | * If we do not, we get strongly overstimated rto. With timestamps |
| 1980 | * samples are accepted even from very old segments: f.e., when rtt=1 |
| 1981 | * increases to 8, we retransmit 5 times and after 8 seconds delayed |
| 1982 | * answer arrives rto becomes 120 seconds! If at least one of segments |
| 1983 | * in window is lost... Voila. --ANK (010210) |
| 1984 | */ |
| 1985 | seq_rtt = tcp_time_stamp - tp->rx_opt.rcv_tsecr; |
| 1986 | tcp_rtt_estimator(tp, seq_rtt); |
| 1987 | tcp_set_rto(tp); |
| 1988 | tp->backoff = 0; |
| 1989 | tcp_bound_rto(tp); |
| 1990 | } |
| 1991 | |
| 1992 | static void tcp_ack_no_tstamp(struct tcp_sock *tp, u32 seq_rtt, int flag) |
| 1993 | { |
| 1994 | /* We don't have a timestamp. Can only use |
| 1995 | * packets that are not retransmitted to determine |
| 1996 | * rtt estimates. Also, we must not reset the |
| 1997 | * backoff for rto until we get a non-retransmitted |
| 1998 | * packet. This allows us to deal with a situation |
| 1999 | * where the network delay has increased suddenly. |
| 2000 | * I.e. Karn's algorithm. (SIGCOMM '87, p5.) |
| 2001 | */ |
| 2002 | |
| 2003 | if (flag & FLAG_RETRANS_DATA_ACKED) |
| 2004 | return; |
| 2005 | |
| 2006 | tcp_rtt_estimator(tp, seq_rtt); |
| 2007 | tcp_set_rto(tp); |
| 2008 | tp->backoff = 0; |
| 2009 | tcp_bound_rto(tp); |
| 2010 | } |
| 2011 | |
| 2012 | static inline void tcp_ack_update_rtt(struct tcp_sock *tp, |
| 2013 | int flag, s32 seq_rtt) |
| 2014 | { |
| 2015 | /* Note that peer MAY send zero echo. In this case it is ignored. (rfc1323) */ |
| 2016 | if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) |
| 2017 | tcp_ack_saw_tstamp(tp, flag); |
| 2018 | else if (seq_rtt >= 0) |
| 2019 | tcp_ack_no_tstamp(tp, seq_rtt, flag); |
| 2020 | } |
| 2021 | |
| 2022 | /* |
| 2023 | * Compute congestion window to use. |
| 2024 | * |
| 2025 | * This is from the implementation of BICTCP in |
| 2026 | * Lison-Xu, Kahaled Harfoush, and Injog Rhee. |
| 2027 | * "Binary Increase Congestion Control for Fast, Long Distance |
| 2028 | * Networks" in InfoComm 2004 |
| 2029 | * Available from: |
| 2030 | * http://www.csc.ncsu.edu/faculty/rhee/export/bitcp.pdf |
| 2031 | * |
| 2032 | * Unless BIC is enabled and congestion window is large |
| 2033 | * this behaves the same as the original Reno. |
| 2034 | */ |
| 2035 | static inline __u32 bictcp_cwnd(struct tcp_sock *tp) |
| 2036 | { |
| 2037 | /* orignal Reno behaviour */ |
| 2038 | if (!tcp_is_bic(tp)) |
| 2039 | return tp->snd_cwnd; |
| 2040 | |
| 2041 | if (tp->bictcp.last_cwnd == tp->snd_cwnd && |
| 2042 | (s32)(tcp_time_stamp - tp->bictcp.last_stamp) <= (HZ>>5)) |
| 2043 | return tp->bictcp.cnt; |
| 2044 | |
| 2045 | tp->bictcp.last_cwnd = tp->snd_cwnd; |
| 2046 | tp->bictcp.last_stamp = tcp_time_stamp; |
| 2047 | |
| 2048 | /* start off normal */ |
| 2049 | if (tp->snd_cwnd <= sysctl_tcp_bic_low_window) |
| 2050 | tp->bictcp.cnt = tp->snd_cwnd; |
| 2051 | |
| 2052 | /* binary increase */ |
| 2053 | else if (tp->snd_cwnd < tp->bictcp.last_max_cwnd) { |
| 2054 | __u32 dist = (tp->bictcp.last_max_cwnd - tp->snd_cwnd) |
| 2055 | / BICTCP_B; |
| 2056 | |
| 2057 | if (dist > BICTCP_MAX_INCREMENT) |
| 2058 | /* linear increase */ |
| 2059 | tp->bictcp.cnt = tp->snd_cwnd / BICTCP_MAX_INCREMENT; |
| 2060 | else if (dist <= 1U) |
| 2061 | /* binary search increase */ |
| 2062 | tp->bictcp.cnt = tp->snd_cwnd * BICTCP_FUNC_OF_MIN_INCR |
| 2063 | / BICTCP_B; |
| 2064 | else |
| 2065 | /* binary search increase */ |
| 2066 | tp->bictcp.cnt = tp->snd_cwnd / dist; |
| 2067 | } else { |
| 2068 | /* slow start amd linear increase */ |
| 2069 | if (tp->snd_cwnd < tp->bictcp.last_max_cwnd + BICTCP_B) |
| 2070 | /* slow start */ |
| 2071 | tp->bictcp.cnt = tp->snd_cwnd * BICTCP_FUNC_OF_MIN_INCR |
| 2072 | / BICTCP_B; |
| 2073 | else if (tp->snd_cwnd < tp->bictcp.last_max_cwnd |
| 2074 | + BICTCP_MAX_INCREMENT*(BICTCP_B-1)) |
| 2075 | /* slow start */ |
| 2076 | tp->bictcp.cnt = tp->snd_cwnd * (BICTCP_B-1) |
| 2077 | / (tp->snd_cwnd-tp->bictcp.last_max_cwnd); |
| 2078 | else |
| 2079 | /* linear increase */ |
| 2080 | tp->bictcp.cnt = tp->snd_cwnd / BICTCP_MAX_INCREMENT; |
| 2081 | } |
| 2082 | return tp->bictcp.cnt; |
| 2083 | } |
| 2084 | |
| 2085 | /* This is Jacobson's slow start and congestion avoidance. |
| 2086 | * SIGCOMM '88, p. 328. |
| 2087 | */ |
| 2088 | static inline void reno_cong_avoid(struct tcp_sock *tp) |
| 2089 | { |
| 2090 | if (tp->snd_cwnd <= tp->snd_ssthresh) { |
| 2091 | /* In "safe" area, increase. */ |
| 2092 | if (tp->snd_cwnd < tp->snd_cwnd_clamp) |
| 2093 | tp->snd_cwnd++; |
| 2094 | } else { |
| 2095 | /* In dangerous area, increase slowly. |
| 2096 | * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd |
| 2097 | */ |
| 2098 | if (tp->snd_cwnd_cnt >= bictcp_cwnd(tp)) { |
| 2099 | if (tp->snd_cwnd < tp->snd_cwnd_clamp) |
| 2100 | tp->snd_cwnd++; |
| 2101 | tp->snd_cwnd_cnt=0; |
| 2102 | } else |
| 2103 | tp->snd_cwnd_cnt++; |
| 2104 | } |
| 2105 | tp->snd_cwnd_stamp = tcp_time_stamp; |
| 2106 | } |
| 2107 | |
| 2108 | /* This is based on the congestion detection/avoidance scheme described in |
| 2109 | * Lawrence S. Brakmo and Larry L. Peterson. |
| 2110 | * "TCP Vegas: End to end congestion avoidance on a global internet." |
| 2111 | * IEEE Journal on Selected Areas in Communication, 13(8):1465--1480, |
| 2112 | * October 1995. Available from: |
| 2113 | * ftp://ftp.cs.arizona.edu/xkernel/Papers/jsac.ps |
| 2114 | * |
| 2115 | * See http://www.cs.arizona.edu/xkernel/ for their implementation. |
| 2116 | * The main aspects that distinguish this implementation from the |
| 2117 | * Arizona Vegas implementation are: |
| 2118 | * o We do not change the loss detection or recovery mechanisms of |
| 2119 | * Linux in any way. Linux already recovers from losses quite well, |
| 2120 | * using fine-grained timers, NewReno, and FACK. |
| 2121 | * o To avoid the performance penalty imposed by increasing cwnd |
| 2122 | * only every-other RTT during slow start, we increase during |
| 2123 | * every RTT during slow start, just like Reno. |
| 2124 | * o Largely to allow continuous cwnd growth during slow start, |
| 2125 | * we use the rate at which ACKs come back as the "actual" |
| 2126 | * rate, rather than the rate at which data is sent. |
| 2127 | * o To speed convergence to the right rate, we set the cwnd |
| 2128 | * to achieve the right ("actual") rate when we exit slow start. |
| 2129 | * o To filter out the noise caused by delayed ACKs, we use the |
| 2130 | * minimum RTT sample observed during the last RTT to calculate |
| 2131 | * the actual rate. |
| 2132 | * o When the sender re-starts from idle, it waits until it has |
| 2133 | * received ACKs for an entire flight of new data before making |
| 2134 | * a cwnd adjustment decision. The original Vegas implementation |
| 2135 | * assumed senders never went idle. |
| 2136 | */ |
| 2137 | static void vegas_cong_avoid(struct tcp_sock *tp, u32 ack, u32 seq_rtt) |
| 2138 | { |
| 2139 | /* The key players are v_beg_snd_una and v_beg_snd_nxt. |
| 2140 | * |
| 2141 | * These are so named because they represent the approximate values |
| 2142 | * of snd_una and snd_nxt at the beginning of the current RTT. More |
| 2143 | * precisely, they represent the amount of data sent during the RTT. |
| 2144 | * At the end of the RTT, when we receive an ACK for v_beg_snd_nxt, |
| 2145 | * we will calculate that (v_beg_snd_nxt - v_beg_snd_una) outstanding |
| 2146 | * bytes of data have been ACKed during the course of the RTT, giving |
| 2147 | * an "actual" rate of: |
| 2148 | * |
| 2149 | * (v_beg_snd_nxt - v_beg_snd_una) / (rtt duration) |
| 2150 | * |
| 2151 | * Unfortunately, v_beg_snd_una is not exactly equal to snd_una, |
| 2152 | * because delayed ACKs can cover more than one segment, so they |
| 2153 | * don't line up nicely with the boundaries of RTTs. |
| 2154 | * |
| 2155 | * Another unfortunate fact of life is that delayed ACKs delay the |
| 2156 | * advance of the left edge of our send window, so that the number |
| 2157 | * of bytes we send in an RTT is often less than our cwnd will allow. |
| 2158 | * So we keep track of our cwnd separately, in v_beg_snd_cwnd. |
| 2159 | */ |
| 2160 | |
| 2161 | if (after(ack, tp->vegas.beg_snd_nxt)) { |
| 2162 | /* Do the Vegas once-per-RTT cwnd adjustment. */ |
| 2163 | u32 old_wnd, old_snd_cwnd; |
| 2164 | |
| 2165 | |
| 2166 | /* Here old_wnd is essentially the window of data that was |
| 2167 | * sent during the previous RTT, and has all |
| 2168 | * been acknowledged in the course of the RTT that ended |
| 2169 | * with the ACK we just received. Likewise, old_snd_cwnd |
| 2170 | * is the cwnd during the previous RTT. |
| 2171 | */ |
| 2172 | old_wnd = (tp->vegas.beg_snd_nxt - tp->vegas.beg_snd_una) / |
| 2173 | tp->mss_cache_std; |
| 2174 | old_snd_cwnd = tp->vegas.beg_snd_cwnd; |
| 2175 | |
| 2176 | /* Save the extent of the current window so we can use this |
| 2177 | * at the end of the next RTT. |
| 2178 | */ |
| 2179 | tp->vegas.beg_snd_una = tp->vegas.beg_snd_nxt; |
| 2180 | tp->vegas.beg_snd_nxt = tp->snd_nxt; |
| 2181 | tp->vegas.beg_snd_cwnd = tp->snd_cwnd; |
| 2182 | |
| 2183 | /* Take into account the current RTT sample too, to |
| 2184 | * decrease the impact of delayed acks. This double counts |
| 2185 | * this sample since we count it for the next window as well, |
| 2186 | * but that's not too awful, since we're taking the min, |
| 2187 | * rather than averaging. |
| 2188 | */ |
| 2189 | vegas_rtt_calc(tp, seq_rtt); |
| 2190 | |
| 2191 | /* We do the Vegas calculations only if we got enough RTT |
| 2192 | * samples that we can be reasonably sure that we got |
| 2193 | * at least one RTT sample that wasn't from a delayed ACK. |
| 2194 | * If we only had 2 samples total, |
| 2195 | * then that means we're getting only 1 ACK per RTT, which |
| 2196 | * means they're almost certainly delayed ACKs. |
| 2197 | * If we have 3 samples, we should be OK. |
| 2198 | */ |
| 2199 | |
| 2200 | if (tp->vegas.cntRTT <= 2) { |
| 2201 | /* We don't have enough RTT samples to do the Vegas |
| 2202 | * calculation, so we'll behave like Reno. |
| 2203 | */ |
| 2204 | if (tp->snd_cwnd > tp->snd_ssthresh) |
| 2205 | tp->snd_cwnd++; |
| 2206 | } else { |
| 2207 | u32 rtt, target_cwnd, diff; |
| 2208 | |
| 2209 | /* We have enough RTT samples, so, using the Vegas |
| 2210 | * algorithm, we determine if we should increase or |
| 2211 | * decrease cwnd, and by how much. |
| 2212 | */ |
| 2213 | |
| 2214 | /* Pluck out the RTT we are using for the Vegas |
| 2215 | * calculations. This is the min RTT seen during the |
| 2216 | * last RTT. Taking the min filters out the effects |
| 2217 | * of delayed ACKs, at the cost of noticing congestion |
| 2218 | * a bit later. |
| 2219 | */ |
| 2220 | rtt = tp->vegas.minRTT; |
| 2221 | |
| 2222 | /* Calculate the cwnd we should have, if we weren't |
| 2223 | * going too fast. |
| 2224 | * |
| 2225 | * This is: |
| 2226 | * (actual rate in segments) * baseRTT |
| 2227 | * We keep it as a fixed point number with |
| 2228 | * V_PARAM_SHIFT bits to the right of the binary point. |
| 2229 | */ |
| 2230 | target_cwnd = ((old_wnd * tp->vegas.baseRTT) |
| 2231 | << V_PARAM_SHIFT) / rtt; |
| 2232 | |
| 2233 | /* Calculate the difference between the window we had, |
| 2234 | * and the window we would like to have. This quantity |
| 2235 | * is the "Diff" from the Arizona Vegas papers. |
| 2236 | * |
| 2237 | * Again, this is a fixed point number with |
| 2238 | * V_PARAM_SHIFT bits to the right of the binary |
| 2239 | * point. |
| 2240 | */ |
| 2241 | diff = (old_wnd << V_PARAM_SHIFT) - target_cwnd; |
| 2242 | |
| 2243 | if (tp->snd_cwnd < tp->snd_ssthresh) { |
| 2244 | /* Slow start. */ |
| 2245 | if (diff > sysctl_tcp_vegas_gamma) { |
| 2246 | /* Going too fast. Time to slow down |
| 2247 | * and switch to congestion avoidance. |
| 2248 | */ |
| 2249 | tp->snd_ssthresh = 2; |
| 2250 | |
| 2251 | /* Set cwnd to match the actual rate |
| 2252 | * exactly: |
| 2253 | * cwnd = (actual rate) * baseRTT |
| 2254 | * Then we add 1 because the integer |
| 2255 | * truncation robs us of full link |
| 2256 | * utilization. |
| 2257 | */ |
| 2258 | tp->snd_cwnd = min(tp->snd_cwnd, |
| 2259 | (target_cwnd >> |
| 2260 | V_PARAM_SHIFT)+1); |
| 2261 | |
| 2262 | } |
| 2263 | } else { |
| 2264 | /* Congestion avoidance. */ |
| 2265 | u32 next_snd_cwnd; |
| 2266 | |
| 2267 | /* Figure out where we would like cwnd |
| 2268 | * to be. |
| 2269 | */ |
| 2270 | if (diff > sysctl_tcp_vegas_beta) { |
| 2271 | /* The old window was too fast, so |
| 2272 | * we slow down. |
| 2273 | */ |
| 2274 | next_snd_cwnd = old_snd_cwnd - 1; |
| 2275 | } else if (diff < sysctl_tcp_vegas_alpha) { |
| 2276 | /* We don't have enough extra packets |
| 2277 | * in the network, so speed up. |
| 2278 | */ |
| 2279 | next_snd_cwnd = old_snd_cwnd + 1; |
| 2280 | } else { |
| 2281 | /* Sending just as fast as we |
| 2282 | * should be. |
| 2283 | */ |
| 2284 | next_snd_cwnd = old_snd_cwnd; |
| 2285 | } |
| 2286 | |
| 2287 | /* Adjust cwnd upward or downward, toward the |
| 2288 | * desired value. |
| 2289 | */ |
| 2290 | if (next_snd_cwnd > tp->snd_cwnd) |
| 2291 | tp->snd_cwnd++; |
| 2292 | else if (next_snd_cwnd < tp->snd_cwnd) |
| 2293 | tp->snd_cwnd--; |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | /* Wipe the slate clean for the next RTT. */ |
| 2298 | tp->vegas.cntRTT = 0; |
| 2299 | tp->vegas.minRTT = 0x7fffffff; |
| 2300 | } |
| 2301 | |
| 2302 | /* The following code is executed for every ack we receive, |
| 2303 | * except for conditions checked in should_advance_cwnd() |
| 2304 | * before the call to tcp_cong_avoid(). Mainly this means that |
| 2305 | * we only execute this code if the ack actually acked some |
| 2306 | * data. |
| 2307 | */ |
| 2308 | |
| 2309 | /* If we are in slow start, increase our cwnd in response to this ACK. |
| 2310 | * (If we are not in slow start then we are in congestion avoidance, |
| 2311 | * and adjust our congestion window only once per RTT. See the code |
| 2312 | * above.) |
| 2313 | */ |
| 2314 | if (tp->snd_cwnd <= tp->snd_ssthresh) |
| 2315 | tp->snd_cwnd++; |
| 2316 | |
| 2317 | /* to keep cwnd from growing without bound */ |
| 2318 | tp->snd_cwnd = min_t(u32, tp->snd_cwnd, tp->snd_cwnd_clamp); |
| 2319 | |
| 2320 | /* Make sure that we are never so timid as to reduce our cwnd below |
| 2321 | * 2 MSS. |
| 2322 | * |
| 2323 | * Going below 2 MSS would risk huge delayed ACKs from our receiver. |
| 2324 | */ |
| 2325 | tp->snd_cwnd = max(tp->snd_cwnd, 2U); |
| 2326 | |
| 2327 | tp->snd_cwnd_stamp = tcp_time_stamp; |
| 2328 | } |
| 2329 | |
| 2330 | static inline void tcp_cong_avoid(struct tcp_sock *tp, u32 ack, u32 seq_rtt) |
| 2331 | { |
| 2332 | if (tcp_vegas_enabled(tp)) |
| 2333 | vegas_cong_avoid(tp, ack, seq_rtt); |
| 2334 | else |
| 2335 | reno_cong_avoid(tp); |
| 2336 | } |
| 2337 | |
| 2338 | /* Restart timer after forward progress on connection. |
| 2339 | * RFC2988 recommends to restart timer to now+rto. |
| 2340 | */ |
| 2341 | |
| 2342 | static inline void tcp_ack_packets_out(struct sock *sk, struct tcp_sock *tp) |
| 2343 | { |
| 2344 | if (!tp->packets_out) { |
| 2345 | tcp_clear_xmit_timer(sk, TCP_TIME_RETRANS); |
| 2346 | } else { |
| 2347 | tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto); |
| 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | /* There is one downside to this scheme. Although we keep the |
| 2352 | * ACK clock ticking, adjusting packet counters and advancing |
| 2353 | * congestion window, we do not liberate socket send buffer |
| 2354 | * space. |
| 2355 | * |
| 2356 | * Mucking with skb->truesize and sk->sk_wmem_alloc et al. |
| 2357 | * then making a write space wakeup callback is a possible |
| 2358 | * future enhancement. WARNING: it is not trivial to make. |
| 2359 | */ |
| 2360 | static int tcp_tso_acked(struct sock *sk, struct sk_buff *skb, |
| 2361 | __u32 now, __s32 *seq_rtt) |
| 2362 | { |
| 2363 | struct tcp_sock *tp = tcp_sk(sk); |
| 2364 | struct tcp_skb_cb *scb = TCP_SKB_CB(skb); |
| 2365 | __u32 seq = tp->snd_una; |
| 2366 | __u32 packets_acked; |
| 2367 | int acked = 0; |
| 2368 | |
| 2369 | /* If we get here, the whole TSO packet has not been |
| 2370 | * acked. |
| 2371 | */ |
| 2372 | BUG_ON(!after(scb->end_seq, seq)); |
| 2373 | |
| 2374 | packets_acked = tcp_skb_pcount(skb); |
| 2375 | if (tcp_trim_head(sk, skb, seq - scb->seq)) |
| 2376 | return 0; |
| 2377 | packets_acked -= tcp_skb_pcount(skb); |
| 2378 | |
| 2379 | if (packets_acked) { |
| 2380 | __u8 sacked = scb->sacked; |
| 2381 | |
| 2382 | acked |= FLAG_DATA_ACKED; |
| 2383 | if (sacked) { |
| 2384 | if (sacked & TCPCB_RETRANS) { |
| 2385 | if (sacked & TCPCB_SACKED_RETRANS) |
| 2386 | tp->retrans_out -= packets_acked; |
| 2387 | acked |= FLAG_RETRANS_DATA_ACKED; |
| 2388 | *seq_rtt = -1; |
| 2389 | } else if (*seq_rtt < 0) |
| 2390 | *seq_rtt = now - scb->when; |
| 2391 | if (sacked & TCPCB_SACKED_ACKED) |
| 2392 | tp->sacked_out -= packets_acked; |
| 2393 | if (sacked & TCPCB_LOST) |
| 2394 | tp->lost_out -= packets_acked; |
| 2395 | if (sacked & TCPCB_URG) { |
| 2396 | if (tp->urg_mode && |
| 2397 | !before(seq, tp->snd_up)) |
| 2398 | tp->urg_mode = 0; |
| 2399 | } |
| 2400 | } else if (*seq_rtt < 0) |
| 2401 | *seq_rtt = now - scb->when; |
| 2402 | |
| 2403 | if (tp->fackets_out) { |
| 2404 | __u32 dval = min(tp->fackets_out, packets_acked); |
| 2405 | tp->fackets_out -= dval; |
| 2406 | } |
| 2407 | tp->packets_out -= packets_acked; |
| 2408 | |
| 2409 | BUG_ON(tcp_skb_pcount(skb) == 0); |
| 2410 | BUG_ON(!before(scb->seq, scb->end_seq)); |
| 2411 | } |
| 2412 | |
| 2413 | return acked; |
| 2414 | } |
| 2415 | |
| 2416 | |
| 2417 | /* Remove acknowledged frames from the retransmission queue. */ |
| 2418 | static int tcp_clean_rtx_queue(struct sock *sk, __s32 *seq_rtt_p) |
| 2419 | { |
| 2420 | struct tcp_sock *tp = tcp_sk(sk); |
| 2421 | struct sk_buff *skb; |
| 2422 | __u32 now = tcp_time_stamp; |
| 2423 | int acked = 0; |
| 2424 | __s32 seq_rtt = -1; |
| 2425 | |
| 2426 | while ((skb = skb_peek(&sk->sk_write_queue)) && |
| 2427 | skb != sk->sk_send_head) { |
| 2428 | struct tcp_skb_cb *scb = TCP_SKB_CB(skb); |
| 2429 | __u8 sacked = scb->sacked; |
| 2430 | |
| 2431 | /* If our packet is before the ack sequence we can |
| 2432 | * discard it as it's confirmed to have arrived at |
| 2433 | * the other end. |
| 2434 | */ |
| 2435 | if (after(scb->end_seq, tp->snd_una)) { |
| 2436 | if (tcp_skb_pcount(skb) > 1) |
| 2437 | acked |= tcp_tso_acked(sk, skb, |
| 2438 | now, &seq_rtt); |
| 2439 | break; |
| 2440 | } |
| 2441 | |
| 2442 | /* Initial outgoing SYN's get put onto the write_queue |
| 2443 | * just like anything else we transmit. It is not |
| 2444 | * true data, and if we misinform our callers that |
| 2445 | * this ACK acks real data, we will erroneously exit |
| 2446 | * connection startup slow start one packet too |
| 2447 | * quickly. This is severely frowned upon behavior. |
| 2448 | */ |
| 2449 | if (!(scb->flags & TCPCB_FLAG_SYN)) { |
| 2450 | acked |= FLAG_DATA_ACKED; |
| 2451 | } else { |
| 2452 | acked |= FLAG_SYN_ACKED; |
| 2453 | tp->retrans_stamp = 0; |
| 2454 | } |
| 2455 | |
| 2456 | if (sacked) { |
| 2457 | if (sacked & TCPCB_RETRANS) { |
| 2458 | if(sacked & TCPCB_SACKED_RETRANS) |
| 2459 | tp->retrans_out -= tcp_skb_pcount(skb); |
| 2460 | acked |= FLAG_RETRANS_DATA_ACKED; |
| 2461 | seq_rtt = -1; |
| 2462 | } else if (seq_rtt < 0) |
| 2463 | seq_rtt = now - scb->when; |
| 2464 | if (sacked & TCPCB_SACKED_ACKED) |
| 2465 | tp->sacked_out -= tcp_skb_pcount(skb); |
| 2466 | if (sacked & TCPCB_LOST) |
| 2467 | tp->lost_out -= tcp_skb_pcount(skb); |
| 2468 | if (sacked & TCPCB_URG) { |
| 2469 | if (tp->urg_mode && |
| 2470 | !before(scb->end_seq, tp->snd_up)) |
| 2471 | tp->urg_mode = 0; |
| 2472 | } |
| 2473 | } else if (seq_rtt < 0) |
| 2474 | seq_rtt = now - scb->when; |
| 2475 | tcp_dec_pcount_approx(&tp->fackets_out, skb); |
| 2476 | tcp_packets_out_dec(tp, skb); |
| 2477 | __skb_unlink(skb, skb->list); |
| 2478 | sk_stream_free_skb(sk, skb); |
| 2479 | } |
| 2480 | |
| 2481 | if (acked&FLAG_ACKED) { |
| 2482 | tcp_ack_update_rtt(tp, acked, seq_rtt); |
| 2483 | tcp_ack_packets_out(sk, tp); |
| 2484 | } |
| 2485 | |
| 2486 | #if FASTRETRANS_DEBUG > 0 |
| 2487 | BUG_TRAP((int)tp->sacked_out >= 0); |
| 2488 | BUG_TRAP((int)tp->lost_out >= 0); |
| 2489 | BUG_TRAP((int)tp->retrans_out >= 0); |
| 2490 | if (!tp->packets_out && tp->rx_opt.sack_ok) { |
| 2491 | if (tp->lost_out) { |
| 2492 | printk(KERN_DEBUG "Leak l=%u %d\n", |
| 2493 | tp->lost_out, tp->ca_state); |
| 2494 | tp->lost_out = 0; |
| 2495 | } |
| 2496 | if (tp->sacked_out) { |
| 2497 | printk(KERN_DEBUG "Leak s=%u %d\n", |
| 2498 | tp->sacked_out, tp->ca_state); |
| 2499 | tp->sacked_out = 0; |
| 2500 | } |
| 2501 | if (tp->retrans_out) { |
| 2502 | printk(KERN_DEBUG "Leak r=%u %d\n", |
| 2503 | tp->retrans_out, tp->ca_state); |
| 2504 | tp->retrans_out = 0; |
| 2505 | } |
| 2506 | } |
| 2507 | #endif |
| 2508 | *seq_rtt_p = seq_rtt; |
| 2509 | return acked; |
| 2510 | } |
| 2511 | |
| 2512 | static void tcp_ack_probe(struct sock *sk) |
| 2513 | { |
| 2514 | struct tcp_sock *tp = tcp_sk(sk); |
| 2515 | |
| 2516 | /* Was it a usable window open? */ |
| 2517 | |
| 2518 | if (!after(TCP_SKB_CB(sk->sk_send_head)->end_seq, |
| 2519 | tp->snd_una + tp->snd_wnd)) { |
| 2520 | tp->backoff = 0; |
| 2521 | tcp_clear_xmit_timer(sk, TCP_TIME_PROBE0); |
| 2522 | /* Socket must be waked up by subsequent tcp_data_snd_check(). |
| 2523 | * This function is not for random using! |
| 2524 | */ |
| 2525 | } else { |
| 2526 | tcp_reset_xmit_timer(sk, TCP_TIME_PROBE0, |
| 2527 | min(tp->rto << tp->backoff, TCP_RTO_MAX)); |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | static inline int tcp_ack_is_dubious(struct tcp_sock *tp, int flag) |
| 2532 | { |
| 2533 | return (!(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) || |
| 2534 | tp->ca_state != TCP_CA_Open); |
| 2535 | } |
| 2536 | |
| 2537 | static inline int tcp_may_raise_cwnd(struct tcp_sock *tp, int flag) |
| 2538 | { |
| 2539 | return (!(flag & FLAG_ECE) || tp->snd_cwnd < tp->snd_ssthresh) && |
| 2540 | !((1<<tp->ca_state)&(TCPF_CA_Recovery|TCPF_CA_CWR)); |
| 2541 | } |
| 2542 | |
| 2543 | /* Check that window update is acceptable. |
| 2544 | * The function assumes that snd_una<=ack<=snd_next. |
| 2545 | */ |
| 2546 | static inline int tcp_may_update_window(struct tcp_sock *tp, u32 ack, |
| 2547 | u32 ack_seq, u32 nwin) |
| 2548 | { |
| 2549 | return (after(ack, tp->snd_una) || |
| 2550 | after(ack_seq, tp->snd_wl1) || |
| 2551 | (ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd)); |
| 2552 | } |
| 2553 | |
| 2554 | /* Update our send window. |
| 2555 | * |
| 2556 | * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2 |
| 2557 | * and in FreeBSD. NetBSD's one is even worse.) is wrong. |
| 2558 | */ |
| 2559 | static int tcp_ack_update_window(struct sock *sk, struct tcp_sock *tp, |
| 2560 | struct sk_buff *skb, u32 ack, u32 ack_seq) |
| 2561 | { |
| 2562 | int flag = 0; |
| 2563 | u32 nwin = ntohs(skb->h.th->window); |
| 2564 | |
| 2565 | if (likely(!skb->h.th->syn)) |
| 2566 | nwin <<= tp->rx_opt.snd_wscale; |
| 2567 | |
| 2568 | if (tcp_may_update_window(tp, ack, ack_seq, nwin)) { |
| 2569 | flag |= FLAG_WIN_UPDATE; |
| 2570 | tcp_update_wl(tp, ack, ack_seq); |
| 2571 | |
| 2572 | if (tp->snd_wnd != nwin) { |
| 2573 | tp->snd_wnd = nwin; |
| 2574 | |
| 2575 | /* Note, it is the only place, where |
| 2576 | * fast path is recovered for sending TCP. |
| 2577 | */ |
| 2578 | tcp_fast_path_check(sk, tp); |
| 2579 | |
| 2580 | if (nwin > tp->max_window) { |
| 2581 | tp->max_window = nwin; |
| 2582 | tcp_sync_mss(sk, tp->pmtu_cookie); |
| 2583 | } |
| 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | tp->snd_una = ack; |
| 2588 | |
| 2589 | return flag; |
| 2590 | } |
| 2591 | |
| 2592 | static void tcp_process_frto(struct sock *sk, u32 prior_snd_una) |
| 2593 | { |
| 2594 | struct tcp_sock *tp = tcp_sk(sk); |
| 2595 | |
| 2596 | tcp_sync_left_out(tp); |
| 2597 | |
| 2598 | if (tp->snd_una == prior_snd_una || |
| 2599 | !before(tp->snd_una, tp->frto_highmark)) { |
| 2600 | /* RTO was caused by loss, start retransmitting in |
| 2601 | * go-back-N slow start |
| 2602 | */ |
| 2603 | tcp_enter_frto_loss(sk); |
| 2604 | return; |
| 2605 | } |
| 2606 | |
| 2607 | if (tp->frto_counter == 1) { |
| 2608 | /* First ACK after RTO advances the window: allow two new |
| 2609 | * segments out. |
| 2610 | */ |
| 2611 | tp->snd_cwnd = tcp_packets_in_flight(tp) + 2; |
| 2612 | } else { |
| 2613 | /* Also the second ACK after RTO advances the window. |
| 2614 | * The RTO was likely spurious. Reduce cwnd and continue |
| 2615 | * in congestion avoidance |
| 2616 | */ |
| 2617 | tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh); |
| 2618 | tcp_moderate_cwnd(tp); |
| 2619 | } |
| 2620 | |
| 2621 | /* F-RTO affects on two new ACKs following RTO. |
| 2622 | * At latest on third ACK the TCP behavor is back to normal. |
| 2623 | */ |
| 2624 | tp->frto_counter = (tp->frto_counter + 1) % 3; |
| 2625 | } |
| 2626 | |
| 2627 | /* |
| 2628 | * TCP Westwood+ |
| 2629 | */ |
| 2630 | |
| 2631 | /* |
| 2632 | * @init_westwood |
| 2633 | * This function initializes fields used in TCP Westwood+. We can't |
| 2634 | * get no information about RTTmin at this time so we simply set it to |
| 2635 | * TCP_WESTWOOD_INIT_RTT. This value was chosen to be too conservative |
| 2636 | * since in this way we're sure it will be updated in a consistent |
| 2637 | * way as soon as possible. It will reasonably happen within the first |
| 2638 | * RTT period of the connection lifetime. |
| 2639 | */ |
| 2640 | |
| 2641 | static void init_westwood(struct sock *sk) |
| 2642 | { |
| 2643 | struct tcp_sock *tp = tcp_sk(sk); |
| 2644 | |
| 2645 | tp->westwood.bw_ns_est = 0; |
| 2646 | tp->westwood.bw_est = 0; |
| 2647 | tp->westwood.accounted = 0; |
| 2648 | tp->westwood.cumul_ack = 0; |
| 2649 | tp->westwood.rtt_win_sx = tcp_time_stamp; |
| 2650 | tp->westwood.rtt = TCP_WESTWOOD_INIT_RTT; |
| 2651 | tp->westwood.rtt_min = TCP_WESTWOOD_INIT_RTT; |
| 2652 | tp->westwood.snd_una = tp->snd_una; |
| 2653 | } |
| 2654 | |
| 2655 | /* |
| 2656 | * @westwood_do_filter |
| 2657 | * Low-pass filter. Implemented using constant coeffients. |
| 2658 | */ |
| 2659 | |
| 2660 | static inline __u32 westwood_do_filter(__u32 a, __u32 b) |
| 2661 | { |
| 2662 | return (((7 * a) + b) >> 3); |
| 2663 | } |
| 2664 | |
| 2665 | static void westwood_filter(struct sock *sk, __u32 delta) |
| 2666 | { |
| 2667 | struct tcp_sock *tp = tcp_sk(sk); |
| 2668 | |
| 2669 | tp->westwood.bw_ns_est = |
| 2670 | westwood_do_filter(tp->westwood.bw_ns_est, |
| 2671 | tp->westwood.bk / delta); |
| 2672 | tp->westwood.bw_est = |
| 2673 | westwood_do_filter(tp->westwood.bw_est, |
| 2674 | tp->westwood.bw_ns_est); |
| 2675 | } |
| 2676 | |
| 2677 | /* |
| 2678 | * @westwood_update_rttmin |
| 2679 | * It is used to update RTTmin. In this case we MUST NOT use |
| 2680 | * WESTWOOD_RTT_MIN minimum bound since we could be on a LAN! |
| 2681 | */ |
| 2682 | |
| 2683 | static inline __u32 westwood_update_rttmin(const struct sock *sk) |
| 2684 | { |
| 2685 | const struct tcp_sock *tp = tcp_sk(sk); |
| 2686 | __u32 rttmin = tp->westwood.rtt_min; |
| 2687 | |
| 2688 | if (tp->westwood.rtt != 0 && |
| 2689 | (tp->westwood.rtt < tp->westwood.rtt_min || !rttmin)) |
| 2690 | rttmin = tp->westwood.rtt; |
| 2691 | |
| 2692 | return rttmin; |
| 2693 | } |
| 2694 | |
| 2695 | /* |
| 2696 | * @westwood_acked |
| 2697 | * Evaluate increases for dk. |
| 2698 | */ |
| 2699 | |
| 2700 | static inline __u32 westwood_acked(const struct sock *sk) |
| 2701 | { |
| 2702 | const struct tcp_sock *tp = tcp_sk(sk); |
| 2703 | |
| 2704 | return tp->snd_una - tp->westwood.snd_una; |
| 2705 | } |
| 2706 | |
| 2707 | /* |
| 2708 | * @westwood_new_window |
| 2709 | * It evaluates if we are receiving data inside the same RTT window as |
| 2710 | * when we started. |
| 2711 | * Return value: |
| 2712 | * It returns 0 if we are still evaluating samples in the same RTT |
| 2713 | * window, 1 if the sample has to be considered in the next window. |
| 2714 | */ |
| 2715 | |
| 2716 | static int westwood_new_window(const struct sock *sk) |
| 2717 | { |
| 2718 | const struct tcp_sock *tp = tcp_sk(sk); |
| 2719 | __u32 left_bound; |
| 2720 | __u32 rtt; |
| 2721 | int ret = 0; |
| 2722 | |
| 2723 | left_bound = tp->westwood.rtt_win_sx; |
| 2724 | rtt = max(tp->westwood.rtt, (u32) TCP_WESTWOOD_RTT_MIN); |
| 2725 | |
| 2726 | /* |
| 2727 | * A RTT-window has passed. Be careful since if RTT is less than |
| 2728 | * 50ms we don't filter but we continue 'building the sample'. |
| 2729 | * This minimum limit was choosen since an estimation on small |
| 2730 | * time intervals is better to avoid... |
| 2731 | * Obvioulsy on a LAN we reasonably will always have |
| 2732 | * right_bound = left_bound + WESTWOOD_RTT_MIN |
| 2733 | */ |
| 2734 | |
| 2735 | if ((left_bound + rtt) < tcp_time_stamp) |
| 2736 | ret = 1; |
| 2737 | |
| 2738 | return ret; |
| 2739 | } |
| 2740 | |
| 2741 | /* |
| 2742 | * @westwood_update_window |
| 2743 | * It updates RTT evaluation window if it is the right moment to do |
| 2744 | * it. If so it calls filter for evaluating bandwidth. |
| 2745 | */ |
| 2746 | |
| 2747 | static void __westwood_update_window(struct sock *sk, __u32 now) |
| 2748 | { |
| 2749 | struct tcp_sock *tp = tcp_sk(sk); |
| 2750 | __u32 delta = now - tp->westwood.rtt_win_sx; |
| 2751 | |
| 2752 | if (delta) { |
| 2753 | if (tp->westwood.rtt) |
| 2754 | westwood_filter(sk, delta); |
| 2755 | |
| 2756 | tp->westwood.bk = 0; |
| 2757 | tp->westwood.rtt_win_sx = tcp_time_stamp; |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | |
| 2762 | static void westwood_update_window(struct sock *sk, __u32 now) |
| 2763 | { |
| 2764 | if (westwood_new_window(sk)) |
| 2765 | __westwood_update_window(sk, now); |
| 2766 | } |
| 2767 | |
| 2768 | /* |
| 2769 | * @__tcp_westwood_fast_bw |
| 2770 | * It is called when we are in fast path. In particular it is called when |
| 2771 | * header prediction is successfull. In such case infact update is |
| 2772 | * straight forward and doesn't need any particular care. |
| 2773 | */ |
| 2774 | |
| 2775 | static void __tcp_westwood_fast_bw(struct sock *sk, struct sk_buff *skb) |
| 2776 | { |
| 2777 | struct tcp_sock *tp = tcp_sk(sk); |
| 2778 | |
| 2779 | westwood_update_window(sk, tcp_time_stamp); |
| 2780 | |
| 2781 | tp->westwood.bk += westwood_acked(sk); |
| 2782 | tp->westwood.snd_una = tp->snd_una; |
| 2783 | tp->westwood.rtt_min = westwood_update_rttmin(sk); |
| 2784 | } |
| 2785 | |
| 2786 | static inline void tcp_westwood_fast_bw(struct sock *sk, struct sk_buff *skb) |
| 2787 | { |
| 2788 | if (tcp_is_westwood(tcp_sk(sk))) |
| 2789 | __tcp_westwood_fast_bw(sk, skb); |
| 2790 | } |
| 2791 | |
| 2792 | |
| 2793 | /* |
| 2794 | * @westwood_dupack_update |
| 2795 | * It updates accounted and cumul_ack when receiving a dupack. |
| 2796 | */ |
| 2797 | |
| 2798 | static void westwood_dupack_update(struct sock *sk) |
| 2799 | { |
| 2800 | struct tcp_sock *tp = tcp_sk(sk); |
| 2801 | |
| 2802 | tp->westwood.accounted += tp->mss_cache_std; |
| 2803 | tp->westwood.cumul_ack = tp->mss_cache_std; |
| 2804 | } |
| 2805 | |
| 2806 | static inline int westwood_may_change_cumul(struct tcp_sock *tp) |
| 2807 | { |
| 2808 | return (tp->westwood.cumul_ack > tp->mss_cache_std); |
| 2809 | } |
| 2810 | |
| 2811 | static inline void westwood_partial_update(struct tcp_sock *tp) |
| 2812 | { |
| 2813 | tp->westwood.accounted -= tp->westwood.cumul_ack; |
| 2814 | tp->westwood.cumul_ack = tp->mss_cache_std; |
| 2815 | } |
| 2816 | |
| 2817 | static inline void westwood_complete_update(struct tcp_sock *tp) |
| 2818 | { |
| 2819 | tp->westwood.cumul_ack -= tp->westwood.accounted; |
| 2820 | tp->westwood.accounted = 0; |
| 2821 | } |
| 2822 | |
| 2823 | /* |
| 2824 | * @westwood_acked_count |
| 2825 | * This function evaluates cumul_ack for evaluating dk in case of |
| 2826 | * delayed or partial acks. |
| 2827 | */ |
| 2828 | |
| 2829 | static inline __u32 westwood_acked_count(struct sock *sk) |
| 2830 | { |
| 2831 | struct tcp_sock *tp = tcp_sk(sk); |
| 2832 | |
| 2833 | tp->westwood.cumul_ack = westwood_acked(sk); |
| 2834 | |
| 2835 | /* If cumul_ack is 0 this is a dupack since it's not moving |
| 2836 | * tp->snd_una. |
| 2837 | */ |
| 2838 | if (!(tp->westwood.cumul_ack)) |
| 2839 | westwood_dupack_update(sk); |
| 2840 | |
| 2841 | if (westwood_may_change_cumul(tp)) { |
| 2842 | /* Partial or delayed ack */ |
| 2843 | if (tp->westwood.accounted >= tp->westwood.cumul_ack) |
| 2844 | westwood_partial_update(tp); |
| 2845 | else |
| 2846 | westwood_complete_update(tp); |
| 2847 | } |
| 2848 | |
| 2849 | tp->westwood.snd_una = tp->snd_una; |
| 2850 | |
| 2851 | return tp->westwood.cumul_ack; |
| 2852 | } |
| 2853 | |
| 2854 | |
| 2855 | /* |
| 2856 | * @__tcp_westwood_slow_bw |
| 2857 | * It is called when something is going wrong..even if there could |
| 2858 | * be no problems! Infact a simple delayed packet may trigger a |
| 2859 | * dupack. But we need to be careful in such case. |
| 2860 | */ |
| 2861 | |
| 2862 | static void __tcp_westwood_slow_bw(struct sock *sk, struct sk_buff *skb) |
| 2863 | { |
| 2864 | struct tcp_sock *tp = tcp_sk(sk); |
| 2865 | |
| 2866 | westwood_update_window(sk, tcp_time_stamp); |
| 2867 | |
| 2868 | tp->westwood.bk += westwood_acked_count(sk); |
| 2869 | tp->westwood.rtt_min = westwood_update_rttmin(sk); |
| 2870 | } |
| 2871 | |
| 2872 | static inline void tcp_westwood_slow_bw(struct sock *sk, struct sk_buff *skb) |
| 2873 | { |
| 2874 | if (tcp_is_westwood(tcp_sk(sk))) |
| 2875 | __tcp_westwood_slow_bw(sk, skb); |
| 2876 | } |
| 2877 | |
| 2878 | /* This routine deals with incoming acks, but not outgoing ones. */ |
| 2879 | static int tcp_ack(struct sock *sk, struct sk_buff *skb, int flag) |
| 2880 | { |
| 2881 | struct tcp_sock *tp = tcp_sk(sk); |
| 2882 | u32 prior_snd_una = tp->snd_una; |
| 2883 | u32 ack_seq = TCP_SKB_CB(skb)->seq; |
| 2884 | u32 ack = TCP_SKB_CB(skb)->ack_seq; |
| 2885 | u32 prior_in_flight; |
| 2886 | s32 seq_rtt; |
| 2887 | int prior_packets; |
| 2888 | |
| 2889 | /* If the ack is newer than sent or older than previous acks |
| 2890 | * then we can probably ignore it. |
| 2891 | */ |
| 2892 | if (after(ack, tp->snd_nxt)) |
| 2893 | goto uninteresting_ack; |
| 2894 | |
| 2895 | if (before(ack, prior_snd_una)) |
| 2896 | goto old_ack; |
| 2897 | |
| 2898 | if (!(flag&FLAG_SLOWPATH) && after(ack, prior_snd_una)) { |
| 2899 | /* Window is constant, pure forward advance. |
| 2900 | * No more checks are required. |
| 2901 | * Note, we use the fact that SND.UNA>=SND.WL2. |
| 2902 | */ |
| 2903 | tcp_update_wl(tp, ack, ack_seq); |
| 2904 | tp->snd_una = ack; |
| 2905 | tcp_westwood_fast_bw(sk, skb); |
| 2906 | flag |= FLAG_WIN_UPDATE; |
| 2907 | |
| 2908 | NET_INC_STATS_BH(LINUX_MIB_TCPHPACKS); |
| 2909 | } else { |
| 2910 | if (ack_seq != TCP_SKB_CB(skb)->end_seq) |
| 2911 | flag |= FLAG_DATA; |
| 2912 | else |
| 2913 | NET_INC_STATS_BH(LINUX_MIB_TCPPUREACKS); |
| 2914 | |
| 2915 | flag |= tcp_ack_update_window(sk, tp, skb, ack, ack_seq); |
| 2916 | |
| 2917 | if (TCP_SKB_CB(skb)->sacked) |
| 2918 | flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una); |
| 2919 | |
| 2920 | if (TCP_ECN_rcv_ecn_echo(tp, skb->h.th)) |
| 2921 | flag |= FLAG_ECE; |
| 2922 | |
| 2923 | tcp_westwood_slow_bw(sk,skb); |
| 2924 | } |
| 2925 | |
| 2926 | /* We passed data and got it acked, remove any soft error |
| 2927 | * log. Something worked... |
| 2928 | */ |
| 2929 | sk->sk_err_soft = 0; |
| 2930 | tp->rcv_tstamp = tcp_time_stamp; |
| 2931 | prior_packets = tp->packets_out; |
| 2932 | if (!prior_packets) |
| 2933 | goto no_queue; |
| 2934 | |
| 2935 | prior_in_flight = tcp_packets_in_flight(tp); |
| 2936 | |
| 2937 | /* See if we can take anything off of the retransmit queue. */ |
| 2938 | flag |= tcp_clean_rtx_queue(sk, &seq_rtt); |
| 2939 | |
| 2940 | if (tp->frto_counter) |
| 2941 | tcp_process_frto(sk, prior_snd_una); |
| 2942 | |
| 2943 | if (tcp_ack_is_dubious(tp, flag)) { |
| 2944 | /* Advanve CWND, if state allows this. */ |
| 2945 | if ((flag & FLAG_DATA_ACKED) && |
| 2946 | (tcp_vegas_enabled(tp) || prior_in_flight >= tp->snd_cwnd) && |
| 2947 | tcp_may_raise_cwnd(tp, flag)) |
| 2948 | tcp_cong_avoid(tp, ack, seq_rtt); |
| 2949 | tcp_fastretrans_alert(sk, prior_snd_una, prior_packets, flag); |
| 2950 | } else { |
| 2951 | if ((flag & FLAG_DATA_ACKED) && |
| 2952 | (tcp_vegas_enabled(tp) || prior_in_flight >= tp->snd_cwnd)) |
| 2953 | tcp_cong_avoid(tp, ack, seq_rtt); |
| 2954 | } |
| 2955 | |
| 2956 | if ((flag & FLAG_FORWARD_PROGRESS) || !(flag&FLAG_NOT_DUP)) |
| 2957 | dst_confirm(sk->sk_dst_cache); |
| 2958 | |
| 2959 | return 1; |
| 2960 | |
| 2961 | no_queue: |
| 2962 | tp->probes_out = 0; |
| 2963 | |
| 2964 | /* If this ack opens up a zero window, clear backoff. It was |
| 2965 | * being used to time the probes, and is probably far higher than |
| 2966 | * it needs to be for normal retransmission. |
| 2967 | */ |
| 2968 | if (sk->sk_send_head) |
| 2969 | tcp_ack_probe(sk); |
| 2970 | return 1; |
| 2971 | |
| 2972 | old_ack: |
| 2973 | if (TCP_SKB_CB(skb)->sacked) |
| 2974 | tcp_sacktag_write_queue(sk, skb, prior_snd_una); |
| 2975 | |
| 2976 | uninteresting_ack: |
| 2977 | SOCK_DEBUG(sk, "Ack %u out of %u:%u\n", ack, tp->snd_una, tp->snd_nxt); |
| 2978 | return 0; |
| 2979 | } |
| 2980 | |
| 2981 | |
| 2982 | /* Look for tcp options. Normally only called on SYN and SYNACK packets. |
| 2983 | * But, this can also be called on packets in the established flow when |
| 2984 | * the fast version below fails. |
| 2985 | */ |
| 2986 | void tcp_parse_options(struct sk_buff *skb, struct tcp_options_received *opt_rx, int estab) |
| 2987 | { |
| 2988 | unsigned char *ptr; |
| 2989 | struct tcphdr *th = skb->h.th; |
| 2990 | int length=(th->doff*4)-sizeof(struct tcphdr); |
| 2991 | |
| 2992 | ptr = (unsigned char *)(th + 1); |
| 2993 | opt_rx->saw_tstamp = 0; |
| 2994 | |
| 2995 | while(length>0) { |
| 2996 | int opcode=*ptr++; |
| 2997 | int opsize; |
| 2998 | |
| 2999 | switch (opcode) { |
| 3000 | case TCPOPT_EOL: |
| 3001 | return; |
| 3002 | case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */ |
| 3003 | length--; |
| 3004 | continue; |
| 3005 | default: |
| 3006 | opsize=*ptr++; |
| 3007 | if (opsize < 2) /* "silly options" */ |
| 3008 | return; |
| 3009 | if (opsize > length) |
| 3010 | return; /* don't parse partial options */ |
| 3011 | switch(opcode) { |
| 3012 | case TCPOPT_MSS: |
| 3013 | if(opsize==TCPOLEN_MSS && th->syn && !estab) { |
| 3014 | u16 in_mss = ntohs(get_unaligned((__u16 *)ptr)); |
| 3015 | if (in_mss) { |
| 3016 | if (opt_rx->user_mss && opt_rx->user_mss < in_mss) |
| 3017 | in_mss = opt_rx->user_mss; |
| 3018 | opt_rx->mss_clamp = in_mss; |
| 3019 | } |
| 3020 | } |
| 3021 | break; |
| 3022 | case TCPOPT_WINDOW: |
| 3023 | if(opsize==TCPOLEN_WINDOW && th->syn && !estab) |
| 3024 | if (sysctl_tcp_window_scaling) { |
| 3025 | __u8 snd_wscale = *(__u8 *) ptr; |
| 3026 | opt_rx->wscale_ok = 1; |
| 3027 | if (snd_wscale > 14) { |
| 3028 | if(net_ratelimit()) |
| 3029 | printk(KERN_INFO "tcp_parse_options: Illegal window " |
| 3030 | "scaling value %d >14 received.\n", |
| 3031 | snd_wscale); |
| 3032 | snd_wscale = 14; |
| 3033 | } |
| 3034 | opt_rx->snd_wscale = snd_wscale; |
| 3035 | } |
| 3036 | break; |
| 3037 | case TCPOPT_TIMESTAMP: |
| 3038 | if(opsize==TCPOLEN_TIMESTAMP) { |
| 3039 | if ((estab && opt_rx->tstamp_ok) || |
| 3040 | (!estab && sysctl_tcp_timestamps)) { |
| 3041 | opt_rx->saw_tstamp = 1; |
| 3042 | opt_rx->rcv_tsval = ntohl(get_unaligned((__u32 *)ptr)); |
| 3043 | opt_rx->rcv_tsecr = ntohl(get_unaligned((__u32 *)(ptr+4))); |
| 3044 | } |
| 3045 | } |
| 3046 | break; |
| 3047 | case TCPOPT_SACK_PERM: |
| 3048 | if(opsize==TCPOLEN_SACK_PERM && th->syn && !estab) { |
| 3049 | if (sysctl_tcp_sack) { |
| 3050 | opt_rx->sack_ok = 1; |
| 3051 | tcp_sack_reset(opt_rx); |
| 3052 | } |
| 3053 | } |
| 3054 | break; |
| 3055 | |
| 3056 | case TCPOPT_SACK: |
| 3057 | if((opsize >= (TCPOLEN_SACK_BASE + TCPOLEN_SACK_PERBLOCK)) && |
| 3058 | !((opsize - TCPOLEN_SACK_BASE) % TCPOLEN_SACK_PERBLOCK) && |
| 3059 | opt_rx->sack_ok) { |
| 3060 | TCP_SKB_CB(skb)->sacked = (ptr - 2) - (unsigned char *)th; |
| 3061 | } |
| 3062 | }; |
| 3063 | ptr+=opsize-2; |
| 3064 | length-=opsize; |
| 3065 | }; |
| 3066 | } |
| 3067 | } |
| 3068 | |
| 3069 | /* Fast parse options. This hopes to only see timestamps. |
| 3070 | * If it is wrong it falls back on tcp_parse_options(). |
| 3071 | */ |
| 3072 | static inline int tcp_fast_parse_options(struct sk_buff *skb, struct tcphdr *th, |
| 3073 | struct tcp_sock *tp) |
| 3074 | { |
| 3075 | if (th->doff == sizeof(struct tcphdr)>>2) { |
| 3076 | tp->rx_opt.saw_tstamp = 0; |
| 3077 | return 0; |
| 3078 | } else if (tp->rx_opt.tstamp_ok && |
| 3079 | th->doff == (sizeof(struct tcphdr)>>2)+(TCPOLEN_TSTAMP_ALIGNED>>2)) { |
| 3080 | __u32 *ptr = (__u32 *)(th + 1); |
| 3081 | if (*ptr == ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
| 3082 | | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) { |
| 3083 | tp->rx_opt.saw_tstamp = 1; |
| 3084 | ++ptr; |
| 3085 | tp->rx_opt.rcv_tsval = ntohl(*ptr); |
| 3086 | ++ptr; |
| 3087 | tp->rx_opt.rcv_tsecr = ntohl(*ptr); |
| 3088 | return 1; |
| 3089 | } |
| 3090 | } |
| 3091 | tcp_parse_options(skb, &tp->rx_opt, 1); |
| 3092 | return 1; |
| 3093 | } |
| 3094 | |
| 3095 | static inline void tcp_store_ts_recent(struct tcp_sock *tp) |
| 3096 | { |
| 3097 | tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval; |
| 3098 | tp->rx_opt.ts_recent_stamp = xtime.tv_sec; |
| 3099 | } |
| 3100 | |
| 3101 | static inline void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq) |
| 3102 | { |
| 3103 | if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) { |
| 3104 | /* PAWS bug workaround wrt. ACK frames, the PAWS discard |
| 3105 | * extra check below makes sure this can only happen |
| 3106 | * for pure ACK frames. -DaveM |
| 3107 | * |
| 3108 | * Not only, also it occurs for expired timestamps. |
| 3109 | */ |
| 3110 | |
| 3111 | if((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) >= 0 || |
| 3112 | xtime.tv_sec >= tp->rx_opt.ts_recent_stamp + TCP_PAWS_24DAYS) |
| 3113 | tcp_store_ts_recent(tp); |
| 3114 | } |
| 3115 | } |
| 3116 | |
| 3117 | /* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM |
| 3118 | * |
| 3119 | * It is not fatal. If this ACK does _not_ change critical state (seqs, window) |
| 3120 | * it can pass through stack. So, the following predicate verifies that |
| 3121 | * this segment is not used for anything but congestion avoidance or |
| 3122 | * fast retransmit. Moreover, we even are able to eliminate most of such |
| 3123 | * second order effects, if we apply some small "replay" window (~RTO) |
| 3124 | * to timestamp space. |
| 3125 | * |
| 3126 | * All these measures still do not guarantee that we reject wrapped ACKs |
| 3127 | * on networks with high bandwidth, when sequence space is recycled fastly, |
| 3128 | * but it guarantees that such events will be very rare and do not affect |
| 3129 | * connection seriously. This doesn't look nice, but alas, PAWS is really |
| 3130 | * buggy extension. |
| 3131 | * |
| 3132 | * [ Later note. Even worse! It is buggy for segments _with_ data. RFC |
| 3133 | * states that events when retransmit arrives after original data are rare. |
| 3134 | * It is a blatant lie. VJ forgot about fast retransmit! 8)8) It is |
| 3135 | * the biggest problem on large power networks even with minor reordering. |
| 3136 | * OK, let's give it small replay window. If peer clock is even 1hz, it is safe |
| 3137 | * up to bandwidth of 18Gigabit/sec. 8) ] |
| 3138 | */ |
| 3139 | |
| 3140 | static int tcp_disordered_ack(struct tcp_sock *tp, struct sk_buff *skb) |
| 3141 | { |
| 3142 | struct tcphdr *th = skb->h.th; |
| 3143 | u32 seq = TCP_SKB_CB(skb)->seq; |
| 3144 | u32 ack = TCP_SKB_CB(skb)->ack_seq; |
| 3145 | |
| 3146 | return (/* 1. Pure ACK with correct sequence number. */ |
| 3147 | (th->ack && seq == TCP_SKB_CB(skb)->end_seq && seq == tp->rcv_nxt) && |
| 3148 | |
| 3149 | /* 2. ... and duplicate ACK. */ |
| 3150 | ack == tp->snd_una && |
| 3151 | |
| 3152 | /* 3. ... and does not update window. */ |
| 3153 | !tcp_may_update_window(tp, ack, seq, ntohs(th->window) << tp->rx_opt.snd_wscale) && |
| 3154 | |
| 3155 | /* 4. ... and sits in replay window. */ |
| 3156 | (s32)(tp->rx_opt.ts_recent - tp->rx_opt.rcv_tsval) <= (tp->rto*1024)/HZ); |
| 3157 | } |
| 3158 | |
| 3159 | static inline int tcp_paws_discard(struct tcp_sock *tp, struct sk_buff *skb) |
| 3160 | { |
| 3161 | return ((s32)(tp->rx_opt.ts_recent - tp->rx_opt.rcv_tsval) > TCP_PAWS_WINDOW && |
| 3162 | xtime.tv_sec < tp->rx_opt.ts_recent_stamp + TCP_PAWS_24DAYS && |
| 3163 | !tcp_disordered_ack(tp, skb)); |
| 3164 | } |
| 3165 | |
| 3166 | /* Check segment sequence number for validity. |
| 3167 | * |
| 3168 | * Segment controls are considered valid, if the segment |
| 3169 | * fits to the window after truncation to the window. Acceptability |
| 3170 | * of data (and SYN, FIN, of course) is checked separately. |
| 3171 | * See tcp_data_queue(), for example. |
| 3172 | * |
| 3173 | * Also, controls (RST is main one) are accepted using RCV.WUP instead |
| 3174 | * of RCV.NXT. Peer still did not advance his SND.UNA when we |
| 3175 | * delayed ACK, so that hisSND.UNA<=ourRCV.WUP. |
| 3176 | * (borrowed from freebsd) |
| 3177 | */ |
| 3178 | |
| 3179 | static inline int tcp_sequence(struct tcp_sock *tp, u32 seq, u32 end_seq) |
| 3180 | { |
| 3181 | return !before(end_seq, tp->rcv_wup) && |
| 3182 | !after(seq, tp->rcv_nxt + tcp_receive_window(tp)); |
| 3183 | } |
| 3184 | |
| 3185 | /* When we get a reset we do this. */ |
| 3186 | static void tcp_reset(struct sock *sk) |
| 3187 | { |
| 3188 | /* We want the right error as BSD sees it (and indeed as we do). */ |
| 3189 | switch (sk->sk_state) { |
| 3190 | case TCP_SYN_SENT: |
| 3191 | sk->sk_err = ECONNREFUSED; |
| 3192 | break; |
| 3193 | case TCP_CLOSE_WAIT: |
| 3194 | sk->sk_err = EPIPE; |
| 3195 | break; |
| 3196 | case TCP_CLOSE: |
| 3197 | return; |
| 3198 | default: |
| 3199 | sk->sk_err = ECONNRESET; |
| 3200 | } |
| 3201 | |
| 3202 | if (!sock_flag(sk, SOCK_DEAD)) |
| 3203 | sk->sk_error_report(sk); |
| 3204 | |
| 3205 | tcp_done(sk); |
| 3206 | } |
| 3207 | |
| 3208 | /* |
| 3209 | * Process the FIN bit. This now behaves as it is supposed to work |
| 3210 | * and the FIN takes effect when it is validly part of sequence |
| 3211 | * space. Not before when we get holes. |
| 3212 | * |
| 3213 | * If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT |
| 3214 | * (and thence onto LAST-ACK and finally, CLOSE, we never enter |
| 3215 | * TIME-WAIT) |
| 3216 | * |
| 3217 | * If we are in FINWAIT-1, a received FIN indicates simultaneous |
| 3218 | * close and we go into CLOSING (and later onto TIME-WAIT) |
| 3219 | * |
| 3220 | * If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT. |
| 3221 | */ |
| 3222 | static void tcp_fin(struct sk_buff *skb, struct sock *sk, struct tcphdr *th) |
| 3223 | { |
| 3224 | struct tcp_sock *tp = tcp_sk(sk); |
| 3225 | |
| 3226 | tcp_schedule_ack(tp); |
| 3227 | |
| 3228 | sk->sk_shutdown |= RCV_SHUTDOWN; |
| 3229 | sock_set_flag(sk, SOCK_DONE); |
| 3230 | |
| 3231 | switch (sk->sk_state) { |
| 3232 | case TCP_SYN_RECV: |
| 3233 | case TCP_ESTABLISHED: |
| 3234 | /* Move to CLOSE_WAIT */ |
| 3235 | tcp_set_state(sk, TCP_CLOSE_WAIT); |
| 3236 | tp->ack.pingpong = 1; |
| 3237 | break; |
| 3238 | |
| 3239 | case TCP_CLOSE_WAIT: |
| 3240 | case TCP_CLOSING: |
| 3241 | /* Received a retransmission of the FIN, do |
| 3242 | * nothing. |
| 3243 | */ |
| 3244 | break; |
| 3245 | case TCP_LAST_ACK: |
| 3246 | /* RFC793: Remain in the LAST-ACK state. */ |
| 3247 | break; |
| 3248 | |
| 3249 | case TCP_FIN_WAIT1: |
| 3250 | /* This case occurs when a simultaneous close |
| 3251 | * happens, we must ack the received FIN and |
| 3252 | * enter the CLOSING state. |
| 3253 | */ |
| 3254 | tcp_send_ack(sk); |
| 3255 | tcp_set_state(sk, TCP_CLOSING); |
| 3256 | break; |
| 3257 | case TCP_FIN_WAIT2: |
| 3258 | /* Received a FIN -- send ACK and enter TIME_WAIT. */ |
| 3259 | tcp_send_ack(sk); |
| 3260 | tcp_time_wait(sk, TCP_TIME_WAIT, 0); |
| 3261 | break; |
| 3262 | default: |
| 3263 | /* Only TCP_LISTEN and TCP_CLOSE are left, in these |
| 3264 | * cases we should never reach this piece of code. |
| 3265 | */ |
| 3266 | printk(KERN_ERR "%s: Impossible, sk->sk_state=%d\n", |
| 3267 | __FUNCTION__, sk->sk_state); |
| 3268 | break; |
| 3269 | }; |
| 3270 | |
| 3271 | /* It _is_ possible, that we have something out-of-order _after_ FIN. |
| 3272 | * Probably, we should reset in this case. For now drop them. |
| 3273 | */ |
| 3274 | __skb_queue_purge(&tp->out_of_order_queue); |
| 3275 | if (tp->rx_opt.sack_ok) |
| 3276 | tcp_sack_reset(&tp->rx_opt); |
| 3277 | sk_stream_mem_reclaim(sk); |
| 3278 | |
| 3279 | if (!sock_flag(sk, SOCK_DEAD)) { |
| 3280 | sk->sk_state_change(sk); |
| 3281 | |
| 3282 | /* Do not send POLL_HUP for half duplex close. */ |
| 3283 | if (sk->sk_shutdown == SHUTDOWN_MASK || |
| 3284 | sk->sk_state == TCP_CLOSE) |
| 3285 | sk_wake_async(sk, 1, POLL_HUP); |
| 3286 | else |
| 3287 | sk_wake_async(sk, 1, POLL_IN); |
| 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | static __inline__ int |
| 3292 | tcp_sack_extend(struct tcp_sack_block *sp, u32 seq, u32 end_seq) |
| 3293 | { |
| 3294 | if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) { |
| 3295 | if (before(seq, sp->start_seq)) |
| 3296 | sp->start_seq = seq; |
| 3297 | if (after(end_seq, sp->end_seq)) |
| 3298 | sp->end_seq = end_seq; |
| 3299 | return 1; |
| 3300 | } |
| 3301 | return 0; |
| 3302 | } |
| 3303 | |
| 3304 | static inline void tcp_dsack_set(struct tcp_sock *tp, u32 seq, u32 end_seq) |
| 3305 | { |
| 3306 | if (tp->rx_opt.sack_ok && sysctl_tcp_dsack) { |
| 3307 | if (before(seq, tp->rcv_nxt)) |
| 3308 | NET_INC_STATS_BH(LINUX_MIB_TCPDSACKOLDSENT); |
| 3309 | else |
| 3310 | NET_INC_STATS_BH(LINUX_MIB_TCPDSACKOFOSENT); |
| 3311 | |
| 3312 | tp->rx_opt.dsack = 1; |
| 3313 | tp->duplicate_sack[0].start_seq = seq; |
| 3314 | tp->duplicate_sack[0].end_seq = end_seq; |
| 3315 | tp->rx_opt.eff_sacks = min(tp->rx_opt.num_sacks + 1, 4 - tp->rx_opt.tstamp_ok); |
| 3316 | } |
| 3317 | } |
| 3318 | |
| 3319 | static inline void tcp_dsack_extend(struct tcp_sock *tp, u32 seq, u32 end_seq) |
| 3320 | { |
| 3321 | if (!tp->rx_opt.dsack) |
| 3322 | tcp_dsack_set(tp, seq, end_seq); |
| 3323 | else |
| 3324 | tcp_sack_extend(tp->duplicate_sack, seq, end_seq); |
| 3325 | } |
| 3326 | |
| 3327 | static void tcp_send_dupack(struct sock *sk, struct sk_buff *skb) |
| 3328 | { |
| 3329 | struct tcp_sock *tp = tcp_sk(sk); |
| 3330 | |
| 3331 | if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && |
| 3332 | before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) { |
| 3333 | NET_INC_STATS_BH(LINUX_MIB_DELAYEDACKLOST); |
| 3334 | tcp_enter_quickack_mode(tp); |
| 3335 | |
| 3336 | if (tp->rx_opt.sack_ok && sysctl_tcp_dsack) { |
| 3337 | u32 end_seq = TCP_SKB_CB(skb)->end_seq; |
| 3338 | |
| 3339 | if (after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) |
| 3340 | end_seq = tp->rcv_nxt; |
| 3341 | tcp_dsack_set(tp, TCP_SKB_CB(skb)->seq, end_seq); |
| 3342 | } |
| 3343 | } |
| 3344 | |
| 3345 | tcp_send_ack(sk); |
| 3346 | } |
| 3347 | |
| 3348 | /* These routines update the SACK block as out-of-order packets arrive or |
| 3349 | * in-order packets close up the sequence space. |
| 3350 | */ |
| 3351 | static void tcp_sack_maybe_coalesce(struct tcp_sock *tp) |
| 3352 | { |
| 3353 | int this_sack; |
| 3354 | struct tcp_sack_block *sp = &tp->selective_acks[0]; |
| 3355 | struct tcp_sack_block *swalk = sp+1; |
| 3356 | |
| 3357 | /* See if the recent change to the first SACK eats into |
| 3358 | * or hits the sequence space of other SACK blocks, if so coalesce. |
| 3359 | */ |
| 3360 | for (this_sack = 1; this_sack < tp->rx_opt.num_sacks; ) { |
| 3361 | if (tcp_sack_extend(sp, swalk->start_seq, swalk->end_seq)) { |
| 3362 | int i; |
| 3363 | |
| 3364 | /* Zap SWALK, by moving every further SACK up by one slot. |
| 3365 | * Decrease num_sacks. |
| 3366 | */ |
| 3367 | tp->rx_opt.num_sacks--; |
| 3368 | tp->rx_opt.eff_sacks = min(tp->rx_opt.num_sacks + tp->rx_opt.dsack, 4 - tp->rx_opt.tstamp_ok); |
| 3369 | for(i=this_sack; i < tp->rx_opt.num_sacks; i++) |
| 3370 | sp[i] = sp[i+1]; |
| 3371 | continue; |
| 3372 | } |
| 3373 | this_sack++, swalk++; |
| 3374 | } |
| 3375 | } |
| 3376 | |
| 3377 | static __inline__ void tcp_sack_swap(struct tcp_sack_block *sack1, struct tcp_sack_block *sack2) |
| 3378 | { |
| 3379 | __u32 tmp; |
| 3380 | |
| 3381 | tmp = sack1->start_seq; |
| 3382 | sack1->start_seq = sack2->start_seq; |
| 3383 | sack2->start_seq = tmp; |
| 3384 | |
| 3385 | tmp = sack1->end_seq; |
| 3386 | sack1->end_seq = sack2->end_seq; |
| 3387 | sack2->end_seq = tmp; |
| 3388 | } |
| 3389 | |
| 3390 | static void tcp_sack_new_ofo_skb(struct sock *sk, u32 seq, u32 end_seq) |
| 3391 | { |
| 3392 | struct tcp_sock *tp = tcp_sk(sk); |
| 3393 | struct tcp_sack_block *sp = &tp->selective_acks[0]; |
| 3394 | int cur_sacks = tp->rx_opt.num_sacks; |
| 3395 | int this_sack; |
| 3396 | |
| 3397 | if (!cur_sacks) |
| 3398 | goto new_sack; |
| 3399 | |
| 3400 | for (this_sack=0; this_sack<cur_sacks; this_sack++, sp++) { |
| 3401 | if (tcp_sack_extend(sp, seq, end_seq)) { |
| 3402 | /* Rotate this_sack to the first one. */ |
| 3403 | for (; this_sack>0; this_sack--, sp--) |
| 3404 | tcp_sack_swap(sp, sp-1); |
| 3405 | if (cur_sacks > 1) |
| 3406 | tcp_sack_maybe_coalesce(tp); |
| 3407 | return; |
| 3408 | } |
| 3409 | } |
| 3410 | |
| 3411 | /* Could not find an adjacent existing SACK, build a new one, |
| 3412 | * put it at the front, and shift everyone else down. We |
| 3413 | * always know there is at least one SACK present already here. |
| 3414 | * |
| 3415 | * If the sack array is full, forget about the last one. |
| 3416 | */ |
| 3417 | if (this_sack >= 4) { |
| 3418 | this_sack--; |
| 3419 | tp->rx_opt.num_sacks--; |
| 3420 | sp--; |
| 3421 | } |
| 3422 | for(; this_sack > 0; this_sack--, sp--) |
| 3423 | *sp = *(sp-1); |
| 3424 | |
| 3425 | new_sack: |
| 3426 | /* Build the new head SACK, and we're done. */ |
| 3427 | sp->start_seq = seq; |
| 3428 | sp->end_seq = end_seq; |
| 3429 | tp->rx_opt.num_sacks++; |
| 3430 | tp->rx_opt.eff_sacks = min(tp->rx_opt.num_sacks + tp->rx_opt.dsack, 4 - tp->rx_opt.tstamp_ok); |
| 3431 | } |
| 3432 | |
| 3433 | /* RCV.NXT advances, some SACKs should be eaten. */ |
| 3434 | |
| 3435 | static void tcp_sack_remove(struct tcp_sock *tp) |
| 3436 | { |
| 3437 | struct tcp_sack_block *sp = &tp->selective_acks[0]; |
| 3438 | int num_sacks = tp->rx_opt.num_sacks; |
| 3439 | int this_sack; |
| 3440 | |
| 3441 | /* Empty ofo queue, hence, all the SACKs are eaten. Clear. */ |
| 3442 | if (skb_queue_len(&tp->out_of_order_queue) == 0) { |
| 3443 | tp->rx_opt.num_sacks = 0; |
| 3444 | tp->rx_opt.eff_sacks = tp->rx_opt.dsack; |
| 3445 | return; |
| 3446 | } |
| 3447 | |
| 3448 | for(this_sack = 0; this_sack < num_sacks; ) { |
| 3449 | /* Check if the start of the sack is covered by RCV.NXT. */ |
| 3450 | if (!before(tp->rcv_nxt, sp->start_seq)) { |
| 3451 | int i; |
| 3452 | |
| 3453 | /* RCV.NXT must cover all the block! */ |
| 3454 | BUG_TRAP(!before(tp->rcv_nxt, sp->end_seq)); |
| 3455 | |
| 3456 | /* Zap this SACK, by moving forward any other SACKS. */ |
| 3457 | for (i=this_sack+1; i < num_sacks; i++) |
| 3458 | tp->selective_acks[i-1] = tp->selective_acks[i]; |
| 3459 | num_sacks--; |
| 3460 | continue; |
| 3461 | } |
| 3462 | this_sack++; |
| 3463 | sp++; |
| 3464 | } |
| 3465 | if (num_sacks != tp->rx_opt.num_sacks) { |
| 3466 | tp->rx_opt.num_sacks = num_sacks; |
| 3467 | tp->rx_opt.eff_sacks = min(tp->rx_opt.num_sacks + tp->rx_opt.dsack, 4 - tp->rx_opt.tstamp_ok); |
| 3468 | } |
| 3469 | } |
| 3470 | |
| 3471 | /* This one checks to see if we can put data from the |
| 3472 | * out_of_order queue into the receive_queue. |
| 3473 | */ |
| 3474 | static void tcp_ofo_queue(struct sock *sk) |
| 3475 | { |
| 3476 | struct tcp_sock *tp = tcp_sk(sk); |
| 3477 | __u32 dsack_high = tp->rcv_nxt; |
| 3478 | struct sk_buff *skb; |
| 3479 | |
| 3480 | while ((skb = skb_peek(&tp->out_of_order_queue)) != NULL) { |
| 3481 | if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) |
| 3482 | break; |
| 3483 | |
| 3484 | if (before(TCP_SKB_CB(skb)->seq, dsack_high)) { |
| 3485 | __u32 dsack = dsack_high; |
| 3486 | if (before(TCP_SKB_CB(skb)->end_seq, dsack_high)) |
| 3487 | dsack_high = TCP_SKB_CB(skb)->end_seq; |
| 3488 | tcp_dsack_extend(tp, TCP_SKB_CB(skb)->seq, dsack); |
| 3489 | } |
| 3490 | |
| 3491 | if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) { |
| 3492 | SOCK_DEBUG(sk, "ofo packet was already received \n"); |
| 3493 | __skb_unlink(skb, skb->list); |
| 3494 | __kfree_skb(skb); |
| 3495 | continue; |
| 3496 | } |
| 3497 | SOCK_DEBUG(sk, "ofo requeuing : rcv_next %X seq %X - %X\n", |
| 3498 | tp->rcv_nxt, TCP_SKB_CB(skb)->seq, |
| 3499 | TCP_SKB_CB(skb)->end_seq); |
| 3500 | |
| 3501 | __skb_unlink(skb, skb->list); |
| 3502 | __skb_queue_tail(&sk->sk_receive_queue, skb); |
| 3503 | tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; |
| 3504 | if(skb->h.th->fin) |
| 3505 | tcp_fin(skb, sk, skb->h.th); |
| 3506 | } |
| 3507 | } |
| 3508 | |
| 3509 | static int tcp_prune_queue(struct sock *sk); |
| 3510 | |
| 3511 | static void tcp_data_queue(struct sock *sk, struct sk_buff *skb) |
| 3512 | { |
| 3513 | struct tcphdr *th = skb->h.th; |
| 3514 | struct tcp_sock *tp = tcp_sk(sk); |
| 3515 | int eaten = -1; |
| 3516 | |
| 3517 | if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) |
| 3518 | goto drop; |
| 3519 | |
| 3520 | th = skb->h.th; |
| 3521 | __skb_pull(skb, th->doff*4); |
| 3522 | |
| 3523 | TCP_ECN_accept_cwr(tp, skb); |
| 3524 | |
| 3525 | if (tp->rx_opt.dsack) { |
| 3526 | tp->rx_opt.dsack = 0; |
| 3527 | tp->rx_opt.eff_sacks = min_t(unsigned int, tp->rx_opt.num_sacks, |
| 3528 | 4 - tp->rx_opt.tstamp_ok); |
| 3529 | } |
| 3530 | |
| 3531 | /* Queue data for delivery to the user. |
| 3532 | * Packets in sequence go to the receive queue. |
| 3533 | * Out of sequence packets to the out_of_order_queue. |
| 3534 | */ |
| 3535 | if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) { |
| 3536 | if (tcp_receive_window(tp) == 0) |
| 3537 | goto out_of_window; |
| 3538 | |
| 3539 | /* Ok. In sequence. In window. */ |
| 3540 | if (tp->ucopy.task == current && |
| 3541 | tp->copied_seq == tp->rcv_nxt && tp->ucopy.len && |
| 3542 | sock_owned_by_user(sk) && !tp->urg_data) { |
| 3543 | int chunk = min_t(unsigned int, skb->len, |
| 3544 | tp->ucopy.len); |
| 3545 | |
| 3546 | __set_current_state(TASK_RUNNING); |
| 3547 | |
| 3548 | local_bh_enable(); |
| 3549 | if (!skb_copy_datagram_iovec(skb, 0, tp->ucopy.iov, chunk)) { |
| 3550 | tp->ucopy.len -= chunk; |
| 3551 | tp->copied_seq += chunk; |
| 3552 | eaten = (chunk == skb->len && !th->fin); |
| 3553 | tcp_rcv_space_adjust(sk); |
| 3554 | } |
| 3555 | local_bh_disable(); |
| 3556 | } |
| 3557 | |
| 3558 | if (eaten <= 0) { |
| 3559 | queue_and_out: |
| 3560 | if (eaten < 0 && |
| 3561 | (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || |
| 3562 | !sk_stream_rmem_schedule(sk, skb))) { |
| 3563 | if (tcp_prune_queue(sk) < 0 || |
| 3564 | !sk_stream_rmem_schedule(sk, skb)) |
| 3565 | goto drop; |
| 3566 | } |
| 3567 | sk_stream_set_owner_r(skb, sk); |
| 3568 | __skb_queue_tail(&sk->sk_receive_queue, skb); |
| 3569 | } |
| 3570 | tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; |
| 3571 | if(skb->len) |
| 3572 | tcp_event_data_recv(sk, tp, skb); |
| 3573 | if(th->fin) |
| 3574 | tcp_fin(skb, sk, th); |
| 3575 | |
| 3576 | if (skb_queue_len(&tp->out_of_order_queue)) { |
| 3577 | tcp_ofo_queue(sk); |
| 3578 | |
| 3579 | /* RFC2581. 4.2. SHOULD send immediate ACK, when |
| 3580 | * gap in queue is filled. |
| 3581 | */ |
| 3582 | if (!skb_queue_len(&tp->out_of_order_queue)) |
| 3583 | tp->ack.pingpong = 0; |
| 3584 | } |
| 3585 | |
| 3586 | if (tp->rx_opt.num_sacks) |
| 3587 | tcp_sack_remove(tp); |
| 3588 | |
| 3589 | tcp_fast_path_check(sk, tp); |
| 3590 | |
| 3591 | if (eaten > 0) |
| 3592 | __kfree_skb(skb); |
| 3593 | else if (!sock_flag(sk, SOCK_DEAD)) |
| 3594 | sk->sk_data_ready(sk, 0); |
| 3595 | return; |
| 3596 | } |
| 3597 | |
| 3598 | if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) { |
| 3599 | /* A retransmit, 2nd most common case. Force an immediate ack. */ |
| 3600 | NET_INC_STATS_BH(LINUX_MIB_DELAYEDACKLOST); |
| 3601 | tcp_dsack_set(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq); |
| 3602 | |
| 3603 | out_of_window: |
| 3604 | tcp_enter_quickack_mode(tp); |
| 3605 | tcp_schedule_ack(tp); |
| 3606 | drop: |
| 3607 | __kfree_skb(skb); |
| 3608 | return; |
| 3609 | } |
| 3610 | |
| 3611 | /* Out of window. F.e. zero window probe. */ |
| 3612 | if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt + tcp_receive_window(tp))) |
| 3613 | goto out_of_window; |
| 3614 | |
| 3615 | tcp_enter_quickack_mode(tp); |
| 3616 | |
| 3617 | if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) { |
| 3618 | /* Partial packet, seq < rcv_next < end_seq */ |
| 3619 | SOCK_DEBUG(sk, "partial packet: rcv_next %X seq %X - %X\n", |
| 3620 | tp->rcv_nxt, TCP_SKB_CB(skb)->seq, |
| 3621 | TCP_SKB_CB(skb)->end_seq); |
| 3622 | |
| 3623 | tcp_dsack_set(tp, TCP_SKB_CB(skb)->seq, tp->rcv_nxt); |
| 3624 | |
| 3625 | /* If window is closed, drop tail of packet. But after |
| 3626 | * remembering D-SACK for its head made in previous line. |
| 3627 | */ |
| 3628 | if (!tcp_receive_window(tp)) |
| 3629 | goto out_of_window; |
| 3630 | goto queue_and_out; |
| 3631 | } |
| 3632 | |
| 3633 | TCP_ECN_check_ce(tp, skb); |
| 3634 | |
| 3635 | if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || |
| 3636 | !sk_stream_rmem_schedule(sk, skb)) { |
| 3637 | if (tcp_prune_queue(sk) < 0 || |
| 3638 | !sk_stream_rmem_schedule(sk, skb)) |
| 3639 | goto drop; |
| 3640 | } |
| 3641 | |
| 3642 | /* Disable header prediction. */ |
| 3643 | tp->pred_flags = 0; |
| 3644 | tcp_schedule_ack(tp); |
| 3645 | |
| 3646 | SOCK_DEBUG(sk, "out of order segment: rcv_next %X seq %X - %X\n", |
| 3647 | tp->rcv_nxt, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq); |
| 3648 | |
| 3649 | sk_stream_set_owner_r(skb, sk); |
| 3650 | |
| 3651 | if (!skb_peek(&tp->out_of_order_queue)) { |
| 3652 | /* Initial out of order segment, build 1 SACK. */ |
| 3653 | if (tp->rx_opt.sack_ok) { |
| 3654 | tp->rx_opt.num_sacks = 1; |
| 3655 | tp->rx_opt.dsack = 0; |
| 3656 | tp->rx_opt.eff_sacks = 1; |
| 3657 | tp->selective_acks[0].start_seq = TCP_SKB_CB(skb)->seq; |
| 3658 | tp->selective_acks[0].end_seq = |
| 3659 | TCP_SKB_CB(skb)->end_seq; |
| 3660 | } |
| 3661 | __skb_queue_head(&tp->out_of_order_queue,skb); |
| 3662 | } else { |
| 3663 | struct sk_buff *skb1 = tp->out_of_order_queue.prev; |
| 3664 | u32 seq = TCP_SKB_CB(skb)->seq; |
| 3665 | u32 end_seq = TCP_SKB_CB(skb)->end_seq; |
| 3666 | |
| 3667 | if (seq == TCP_SKB_CB(skb1)->end_seq) { |
| 3668 | __skb_append(skb1, skb); |
| 3669 | |
| 3670 | if (!tp->rx_opt.num_sacks || |
| 3671 | tp->selective_acks[0].end_seq != seq) |
| 3672 | goto add_sack; |
| 3673 | |
| 3674 | /* Common case: data arrive in order after hole. */ |
| 3675 | tp->selective_acks[0].end_seq = end_seq; |
| 3676 | return; |
| 3677 | } |
| 3678 | |
| 3679 | /* Find place to insert this segment. */ |
| 3680 | do { |
| 3681 | if (!after(TCP_SKB_CB(skb1)->seq, seq)) |
| 3682 | break; |
| 3683 | } while ((skb1 = skb1->prev) != |
| 3684 | (struct sk_buff*)&tp->out_of_order_queue); |
| 3685 | |
| 3686 | /* Do skb overlap to previous one? */ |
| 3687 | if (skb1 != (struct sk_buff*)&tp->out_of_order_queue && |
| 3688 | before(seq, TCP_SKB_CB(skb1)->end_seq)) { |
| 3689 | if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) { |
| 3690 | /* All the bits are present. Drop. */ |
| 3691 | __kfree_skb(skb); |
| 3692 | tcp_dsack_set(tp, seq, end_seq); |
| 3693 | goto add_sack; |
| 3694 | } |
| 3695 | if (after(seq, TCP_SKB_CB(skb1)->seq)) { |
| 3696 | /* Partial overlap. */ |
| 3697 | tcp_dsack_set(tp, seq, TCP_SKB_CB(skb1)->end_seq); |
| 3698 | } else { |
| 3699 | skb1 = skb1->prev; |
| 3700 | } |
| 3701 | } |
| 3702 | __skb_insert(skb, skb1, skb1->next, &tp->out_of_order_queue); |
| 3703 | |
| 3704 | /* And clean segments covered by new one as whole. */ |
| 3705 | while ((skb1 = skb->next) != |
| 3706 | (struct sk_buff*)&tp->out_of_order_queue && |
| 3707 | after(end_seq, TCP_SKB_CB(skb1)->seq)) { |
| 3708 | if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) { |
| 3709 | tcp_dsack_extend(tp, TCP_SKB_CB(skb1)->seq, end_seq); |
| 3710 | break; |
| 3711 | } |
| 3712 | __skb_unlink(skb1, skb1->list); |
| 3713 | tcp_dsack_extend(tp, TCP_SKB_CB(skb1)->seq, TCP_SKB_CB(skb1)->end_seq); |
| 3714 | __kfree_skb(skb1); |
| 3715 | } |
| 3716 | |
| 3717 | add_sack: |
| 3718 | if (tp->rx_opt.sack_ok) |
| 3719 | tcp_sack_new_ofo_skb(sk, seq, end_seq); |
| 3720 | } |
| 3721 | } |
| 3722 | |
| 3723 | /* Collapse contiguous sequence of skbs head..tail with |
| 3724 | * sequence numbers start..end. |
| 3725 | * Segments with FIN/SYN are not collapsed (only because this |
| 3726 | * simplifies code) |
| 3727 | */ |
| 3728 | static void |
| 3729 | tcp_collapse(struct sock *sk, struct sk_buff *head, |
| 3730 | struct sk_buff *tail, u32 start, u32 end) |
| 3731 | { |
| 3732 | struct sk_buff *skb; |
| 3733 | |
| 3734 | /* First, check that queue is collapsable and find |
| 3735 | * the point where collapsing can be useful. */ |
| 3736 | for (skb = head; skb != tail; ) { |
| 3737 | /* No new bits? It is possible on ofo queue. */ |
| 3738 | if (!before(start, TCP_SKB_CB(skb)->end_seq)) { |
| 3739 | struct sk_buff *next = skb->next; |
| 3740 | __skb_unlink(skb, skb->list); |
| 3741 | __kfree_skb(skb); |
| 3742 | NET_INC_STATS_BH(LINUX_MIB_TCPRCVCOLLAPSED); |
| 3743 | skb = next; |
| 3744 | continue; |
| 3745 | } |
| 3746 | |
| 3747 | /* The first skb to collapse is: |
| 3748 | * - not SYN/FIN and |
| 3749 | * - bloated or contains data before "start" or |
| 3750 | * overlaps to the next one. |
| 3751 | */ |
| 3752 | if (!skb->h.th->syn && !skb->h.th->fin && |
| 3753 | (tcp_win_from_space(skb->truesize) > skb->len || |
| 3754 | before(TCP_SKB_CB(skb)->seq, start) || |
| 3755 | (skb->next != tail && |
| 3756 | TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb->next)->seq))) |
| 3757 | break; |
| 3758 | |
| 3759 | /* Decided to skip this, advance start seq. */ |
| 3760 | start = TCP_SKB_CB(skb)->end_seq; |
| 3761 | skb = skb->next; |
| 3762 | } |
| 3763 | if (skb == tail || skb->h.th->syn || skb->h.th->fin) |
| 3764 | return; |
| 3765 | |
| 3766 | while (before(start, end)) { |
| 3767 | struct sk_buff *nskb; |
| 3768 | int header = skb_headroom(skb); |
| 3769 | int copy = SKB_MAX_ORDER(header, 0); |
| 3770 | |
| 3771 | /* Too big header? This can happen with IPv6. */ |
| 3772 | if (copy < 0) |
| 3773 | return; |
| 3774 | if (end-start < copy) |
| 3775 | copy = end-start; |
| 3776 | nskb = alloc_skb(copy+header, GFP_ATOMIC); |
| 3777 | if (!nskb) |
| 3778 | return; |
| 3779 | skb_reserve(nskb, header); |
| 3780 | memcpy(nskb->head, skb->head, header); |
| 3781 | nskb->nh.raw = nskb->head + (skb->nh.raw-skb->head); |
| 3782 | nskb->h.raw = nskb->head + (skb->h.raw-skb->head); |
| 3783 | nskb->mac.raw = nskb->head + (skb->mac.raw-skb->head); |
| 3784 | memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); |
| 3785 | TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start; |
| 3786 | __skb_insert(nskb, skb->prev, skb, skb->list); |
| 3787 | sk_stream_set_owner_r(nskb, sk); |
| 3788 | |
| 3789 | /* Copy data, releasing collapsed skbs. */ |
| 3790 | while (copy > 0) { |
| 3791 | int offset = start - TCP_SKB_CB(skb)->seq; |
| 3792 | int size = TCP_SKB_CB(skb)->end_seq - start; |
| 3793 | |
| 3794 | if (offset < 0) BUG(); |
| 3795 | if (size > 0) { |
| 3796 | size = min(copy, size); |
| 3797 | if (skb_copy_bits(skb, offset, skb_put(nskb, size), size)) |
| 3798 | BUG(); |
| 3799 | TCP_SKB_CB(nskb)->end_seq += size; |
| 3800 | copy -= size; |
| 3801 | start += size; |
| 3802 | } |
| 3803 | if (!before(start, TCP_SKB_CB(skb)->end_seq)) { |
| 3804 | struct sk_buff *next = skb->next; |
| 3805 | __skb_unlink(skb, skb->list); |
| 3806 | __kfree_skb(skb); |
| 3807 | NET_INC_STATS_BH(LINUX_MIB_TCPRCVCOLLAPSED); |
| 3808 | skb = next; |
| 3809 | if (skb == tail || skb->h.th->syn || skb->h.th->fin) |
| 3810 | return; |
| 3811 | } |
| 3812 | } |
| 3813 | } |
| 3814 | } |
| 3815 | |
| 3816 | /* Collapse ofo queue. Algorithm: select contiguous sequence of skbs |
| 3817 | * and tcp_collapse() them until all the queue is collapsed. |
| 3818 | */ |
| 3819 | static void tcp_collapse_ofo_queue(struct sock *sk) |
| 3820 | { |
| 3821 | struct tcp_sock *tp = tcp_sk(sk); |
| 3822 | struct sk_buff *skb = skb_peek(&tp->out_of_order_queue); |
| 3823 | struct sk_buff *head; |
| 3824 | u32 start, end; |
| 3825 | |
| 3826 | if (skb == NULL) |
| 3827 | return; |
| 3828 | |
| 3829 | start = TCP_SKB_CB(skb)->seq; |
| 3830 | end = TCP_SKB_CB(skb)->end_seq; |
| 3831 | head = skb; |
| 3832 | |
| 3833 | for (;;) { |
| 3834 | skb = skb->next; |
| 3835 | |
| 3836 | /* Segment is terminated when we see gap or when |
| 3837 | * we are at the end of all the queue. */ |
| 3838 | if (skb == (struct sk_buff *)&tp->out_of_order_queue || |
| 3839 | after(TCP_SKB_CB(skb)->seq, end) || |
| 3840 | before(TCP_SKB_CB(skb)->end_seq, start)) { |
| 3841 | tcp_collapse(sk, head, skb, start, end); |
| 3842 | head = skb; |
| 3843 | if (skb == (struct sk_buff *)&tp->out_of_order_queue) |
| 3844 | break; |
| 3845 | /* Start new segment */ |
| 3846 | start = TCP_SKB_CB(skb)->seq; |
| 3847 | end = TCP_SKB_CB(skb)->end_seq; |
| 3848 | } else { |
| 3849 | if (before(TCP_SKB_CB(skb)->seq, start)) |
| 3850 | start = TCP_SKB_CB(skb)->seq; |
| 3851 | if (after(TCP_SKB_CB(skb)->end_seq, end)) |
| 3852 | end = TCP_SKB_CB(skb)->end_seq; |
| 3853 | } |
| 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | /* Reduce allocated memory if we can, trying to get |
| 3858 | * the socket within its memory limits again. |
| 3859 | * |
| 3860 | * Return less than zero if we should start dropping frames |
| 3861 | * until the socket owning process reads some of the data |
| 3862 | * to stabilize the situation. |
| 3863 | */ |
| 3864 | static int tcp_prune_queue(struct sock *sk) |
| 3865 | { |
| 3866 | struct tcp_sock *tp = tcp_sk(sk); |
| 3867 | |
| 3868 | SOCK_DEBUG(sk, "prune_queue: c=%x\n", tp->copied_seq); |
| 3869 | |
| 3870 | NET_INC_STATS_BH(LINUX_MIB_PRUNECALLED); |
| 3871 | |
| 3872 | if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) |
| 3873 | tcp_clamp_window(sk, tp); |
| 3874 | else if (tcp_memory_pressure) |
| 3875 | tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss); |
| 3876 | |
| 3877 | tcp_collapse_ofo_queue(sk); |
| 3878 | tcp_collapse(sk, sk->sk_receive_queue.next, |
| 3879 | (struct sk_buff*)&sk->sk_receive_queue, |
| 3880 | tp->copied_seq, tp->rcv_nxt); |
| 3881 | sk_stream_mem_reclaim(sk); |
| 3882 | |
| 3883 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) |
| 3884 | return 0; |
| 3885 | |
| 3886 | /* Collapsing did not help, destructive actions follow. |
| 3887 | * This must not ever occur. */ |
| 3888 | |
| 3889 | /* First, purge the out_of_order queue. */ |
| 3890 | if (skb_queue_len(&tp->out_of_order_queue)) { |
| 3891 | NET_ADD_STATS_BH(LINUX_MIB_OFOPRUNED, |
| 3892 | skb_queue_len(&tp->out_of_order_queue)); |
| 3893 | __skb_queue_purge(&tp->out_of_order_queue); |
| 3894 | |
| 3895 | /* Reset SACK state. A conforming SACK implementation will |
| 3896 | * do the same at a timeout based retransmit. When a connection |
| 3897 | * is in a sad state like this, we care only about integrity |
| 3898 | * of the connection not performance. |
| 3899 | */ |
| 3900 | if (tp->rx_opt.sack_ok) |
| 3901 | tcp_sack_reset(&tp->rx_opt); |
| 3902 | sk_stream_mem_reclaim(sk); |
| 3903 | } |
| 3904 | |
| 3905 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) |
| 3906 | return 0; |
| 3907 | |
| 3908 | /* If we are really being abused, tell the caller to silently |
| 3909 | * drop receive data on the floor. It will get retransmitted |
| 3910 | * and hopefully then we'll have sufficient space. |
| 3911 | */ |
| 3912 | NET_INC_STATS_BH(LINUX_MIB_RCVPRUNED); |
| 3913 | |
| 3914 | /* Massive buffer overcommit. */ |
| 3915 | tp->pred_flags = 0; |
| 3916 | return -1; |
| 3917 | } |
| 3918 | |
| 3919 | |
| 3920 | /* RFC2861, slow part. Adjust cwnd, after it was not full during one rto. |
| 3921 | * As additional protections, we do not touch cwnd in retransmission phases, |
| 3922 | * and if application hit its sndbuf limit recently. |
| 3923 | */ |
| 3924 | void tcp_cwnd_application_limited(struct sock *sk) |
| 3925 | { |
| 3926 | struct tcp_sock *tp = tcp_sk(sk); |
| 3927 | |
| 3928 | if (tp->ca_state == TCP_CA_Open && |
| 3929 | sk->sk_socket && !test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { |
| 3930 | /* Limited by application or receiver window. */ |
| 3931 | u32 win_used = max(tp->snd_cwnd_used, 2U); |
| 3932 | if (win_used < tp->snd_cwnd) { |
| 3933 | tp->snd_ssthresh = tcp_current_ssthresh(tp); |
| 3934 | tp->snd_cwnd = (tp->snd_cwnd + win_used) >> 1; |
| 3935 | } |
| 3936 | tp->snd_cwnd_used = 0; |
| 3937 | } |
| 3938 | tp->snd_cwnd_stamp = tcp_time_stamp; |
| 3939 | } |
| 3940 | |
| 3941 | |
| 3942 | /* When incoming ACK allowed to free some skb from write_queue, |
| 3943 | * we remember this event in flag SOCK_QUEUE_SHRUNK and wake up socket |
| 3944 | * on the exit from tcp input handler. |
| 3945 | * |
| 3946 | * PROBLEM: sndbuf expansion does not work well with largesend. |
| 3947 | */ |
| 3948 | static void tcp_new_space(struct sock *sk) |
| 3949 | { |
| 3950 | struct tcp_sock *tp = tcp_sk(sk); |
| 3951 | |
| 3952 | if (tp->packets_out < tp->snd_cwnd && |
| 3953 | !(sk->sk_userlocks & SOCK_SNDBUF_LOCK) && |
| 3954 | !tcp_memory_pressure && |
| 3955 | atomic_read(&tcp_memory_allocated) < sysctl_tcp_mem[0]) { |
| 3956 | int sndmem = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache_std) + |
| 3957 | MAX_TCP_HEADER + 16 + sizeof(struct sk_buff), |
| 3958 | demanded = max_t(unsigned int, tp->snd_cwnd, |
| 3959 | tp->reordering + 1); |
| 3960 | sndmem *= 2*demanded; |
| 3961 | if (sndmem > sk->sk_sndbuf) |
| 3962 | sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]); |
| 3963 | tp->snd_cwnd_stamp = tcp_time_stamp; |
| 3964 | } |
| 3965 | |
| 3966 | sk->sk_write_space(sk); |
| 3967 | } |
| 3968 | |
| 3969 | static inline void tcp_check_space(struct sock *sk) |
| 3970 | { |
| 3971 | if (sock_flag(sk, SOCK_QUEUE_SHRUNK)) { |
| 3972 | sock_reset_flag(sk, SOCK_QUEUE_SHRUNK); |
| 3973 | if (sk->sk_socket && |
| 3974 | test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) |
| 3975 | tcp_new_space(sk); |
| 3976 | } |
| 3977 | } |
| 3978 | |
| 3979 | static void __tcp_data_snd_check(struct sock *sk, struct sk_buff *skb) |
| 3980 | { |
| 3981 | struct tcp_sock *tp = tcp_sk(sk); |
| 3982 | |
| 3983 | if (after(TCP_SKB_CB(skb)->end_seq, tp->snd_una + tp->snd_wnd) || |
| 3984 | tcp_packets_in_flight(tp) >= tp->snd_cwnd || |
| 3985 | tcp_write_xmit(sk, tp->nonagle)) |
| 3986 | tcp_check_probe_timer(sk, tp); |
| 3987 | } |
| 3988 | |
| 3989 | static __inline__ void tcp_data_snd_check(struct sock *sk) |
| 3990 | { |
| 3991 | struct sk_buff *skb = sk->sk_send_head; |
| 3992 | |
| 3993 | if (skb != NULL) |
| 3994 | __tcp_data_snd_check(sk, skb); |
| 3995 | tcp_check_space(sk); |
| 3996 | } |
| 3997 | |
| 3998 | /* |
| 3999 | * Check if sending an ack is needed. |
| 4000 | */ |
| 4001 | static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible) |
| 4002 | { |
| 4003 | struct tcp_sock *tp = tcp_sk(sk); |
| 4004 | |
| 4005 | /* More than one full frame received... */ |
| 4006 | if (((tp->rcv_nxt - tp->rcv_wup) > tp->ack.rcv_mss |
| 4007 | /* ... and right edge of window advances far enough. |
| 4008 | * (tcp_recvmsg() will send ACK otherwise). Or... |
| 4009 | */ |
| 4010 | && __tcp_select_window(sk) >= tp->rcv_wnd) || |
| 4011 | /* We ACK each frame or... */ |
| 4012 | tcp_in_quickack_mode(tp) || |
| 4013 | /* We have out of order data. */ |
| 4014 | (ofo_possible && |
| 4015 | skb_peek(&tp->out_of_order_queue))) { |
| 4016 | /* Then ack it now */ |
| 4017 | tcp_send_ack(sk); |
| 4018 | } else { |
| 4019 | /* Else, send delayed ack. */ |
| 4020 | tcp_send_delayed_ack(sk); |
| 4021 | } |
| 4022 | } |
| 4023 | |
| 4024 | static __inline__ void tcp_ack_snd_check(struct sock *sk) |
| 4025 | { |
| 4026 | struct tcp_sock *tp = tcp_sk(sk); |
| 4027 | if (!tcp_ack_scheduled(tp)) { |
| 4028 | /* We sent a data segment already. */ |
| 4029 | return; |
| 4030 | } |
| 4031 | __tcp_ack_snd_check(sk, 1); |
| 4032 | } |
| 4033 | |
| 4034 | /* |
| 4035 | * This routine is only called when we have urgent data |
| 4036 | * signalled. Its the 'slow' part of tcp_urg. It could be |
| 4037 | * moved inline now as tcp_urg is only called from one |
| 4038 | * place. We handle URGent data wrong. We have to - as |
| 4039 | * BSD still doesn't use the correction from RFC961. |
| 4040 | * For 1003.1g we should support a new option TCP_STDURG to permit |
| 4041 | * either form (or just set the sysctl tcp_stdurg). |
| 4042 | */ |
| 4043 | |
| 4044 | static void tcp_check_urg(struct sock * sk, struct tcphdr * th) |
| 4045 | { |
| 4046 | struct tcp_sock *tp = tcp_sk(sk); |
| 4047 | u32 ptr = ntohs(th->urg_ptr); |
| 4048 | |
| 4049 | if (ptr && !sysctl_tcp_stdurg) |
| 4050 | ptr--; |
| 4051 | ptr += ntohl(th->seq); |
| 4052 | |
| 4053 | /* Ignore urgent data that we've already seen and read. */ |
| 4054 | if (after(tp->copied_seq, ptr)) |
| 4055 | return; |
| 4056 | |
| 4057 | /* Do not replay urg ptr. |
| 4058 | * |
| 4059 | * NOTE: interesting situation not covered by specs. |
| 4060 | * Misbehaving sender may send urg ptr, pointing to segment, |
| 4061 | * which we already have in ofo queue. We are not able to fetch |
| 4062 | * such data and will stay in TCP_URG_NOTYET until will be eaten |
| 4063 | * by recvmsg(). Seems, we are not obliged to handle such wicked |
| 4064 | * situations. But it is worth to think about possibility of some |
| 4065 | * DoSes using some hypothetical application level deadlock. |
| 4066 | */ |
| 4067 | if (before(ptr, tp->rcv_nxt)) |
| 4068 | return; |
| 4069 | |
| 4070 | /* Do we already have a newer (or duplicate) urgent pointer? */ |
| 4071 | if (tp->urg_data && !after(ptr, tp->urg_seq)) |
| 4072 | return; |
| 4073 | |
| 4074 | /* Tell the world about our new urgent pointer. */ |
| 4075 | sk_send_sigurg(sk); |
| 4076 | |
| 4077 | /* We may be adding urgent data when the last byte read was |
| 4078 | * urgent. To do this requires some care. We cannot just ignore |
| 4079 | * tp->copied_seq since we would read the last urgent byte again |
| 4080 | * as data, nor can we alter copied_seq until this data arrives |
| 4081 | * or we break the sematics of SIOCATMARK (and thus sockatmark()) |
| 4082 | * |
| 4083 | * NOTE. Double Dutch. Rendering to plain English: author of comment |
| 4084 | * above did something sort of send("A", MSG_OOB); send("B", MSG_OOB); |
| 4085 | * and expect that both A and B disappear from stream. This is _wrong_. |
| 4086 | * Though this happens in BSD with high probability, this is occasional. |
| 4087 | * Any application relying on this is buggy. Note also, that fix "works" |
| 4088 | * only in this artificial test. Insert some normal data between A and B and we will |
| 4089 | * decline of BSD again. Verdict: it is better to remove to trap |
| 4090 | * buggy users. |
| 4091 | */ |
| 4092 | if (tp->urg_seq == tp->copied_seq && tp->urg_data && |
| 4093 | !sock_flag(sk, SOCK_URGINLINE) && |
| 4094 | tp->copied_seq != tp->rcv_nxt) { |
| 4095 | struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); |
| 4096 | tp->copied_seq++; |
| 4097 | if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) { |
| 4098 | __skb_unlink(skb, skb->list); |
| 4099 | __kfree_skb(skb); |
| 4100 | } |
| 4101 | } |
| 4102 | |
| 4103 | tp->urg_data = TCP_URG_NOTYET; |
| 4104 | tp->urg_seq = ptr; |
| 4105 | |
| 4106 | /* Disable header prediction. */ |
| 4107 | tp->pred_flags = 0; |
| 4108 | } |
| 4109 | |
| 4110 | /* This is the 'fast' part of urgent handling. */ |
| 4111 | static void tcp_urg(struct sock *sk, struct sk_buff *skb, struct tcphdr *th) |
| 4112 | { |
| 4113 | struct tcp_sock *tp = tcp_sk(sk); |
| 4114 | |
| 4115 | /* Check if we get a new urgent pointer - normally not. */ |
| 4116 | if (th->urg) |
| 4117 | tcp_check_urg(sk,th); |
| 4118 | |
| 4119 | /* Do we wait for any urgent data? - normally not... */ |
| 4120 | if (tp->urg_data == TCP_URG_NOTYET) { |
| 4121 | u32 ptr = tp->urg_seq - ntohl(th->seq) + (th->doff * 4) - |
| 4122 | th->syn; |
| 4123 | |
| 4124 | /* Is the urgent pointer pointing into this packet? */ |
| 4125 | if (ptr < skb->len) { |
| 4126 | u8 tmp; |
| 4127 | if (skb_copy_bits(skb, ptr, &tmp, 1)) |
| 4128 | BUG(); |
| 4129 | tp->urg_data = TCP_URG_VALID | tmp; |
| 4130 | if (!sock_flag(sk, SOCK_DEAD)) |
| 4131 | sk->sk_data_ready(sk, 0); |
| 4132 | } |
| 4133 | } |
| 4134 | } |
| 4135 | |
| 4136 | static int tcp_copy_to_iovec(struct sock *sk, struct sk_buff *skb, int hlen) |
| 4137 | { |
| 4138 | struct tcp_sock *tp = tcp_sk(sk); |
| 4139 | int chunk = skb->len - hlen; |
| 4140 | int err; |
| 4141 | |
| 4142 | local_bh_enable(); |
| 4143 | if (skb->ip_summed==CHECKSUM_UNNECESSARY) |
| 4144 | err = skb_copy_datagram_iovec(skb, hlen, tp->ucopy.iov, chunk); |
| 4145 | else |
| 4146 | err = skb_copy_and_csum_datagram_iovec(skb, hlen, |
| 4147 | tp->ucopy.iov); |
| 4148 | |
| 4149 | if (!err) { |
| 4150 | tp->ucopy.len -= chunk; |
| 4151 | tp->copied_seq += chunk; |
| 4152 | tcp_rcv_space_adjust(sk); |
| 4153 | } |
| 4154 | |
| 4155 | local_bh_disable(); |
| 4156 | return err; |
| 4157 | } |
| 4158 | |
| 4159 | static int __tcp_checksum_complete_user(struct sock *sk, struct sk_buff *skb) |
| 4160 | { |
| 4161 | int result; |
| 4162 | |
| 4163 | if (sock_owned_by_user(sk)) { |
| 4164 | local_bh_enable(); |
| 4165 | result = __tcp_checksum_complete(skb); |
| 4166 | local_bh_disable(); |
| 4167 | } else { |
| 4168 | result = __tcp_checksum_complete(skb); |
| 4169 | } |
| 4170 | return result; |
| 4171 | } |
| 4172 | |
| 4173 | static __inline__ int |
| 4174 | tcp_checksum_complete_user(struct sock *sk, struct sk_buff *skb) |
| 4175 | { |
| 4176 | return skb->ip_summed != CHECKSUM_UNNECESSARY && |
| 4177 | __tcp_checksum_complete_user(sk, skb); |
| 4178 | } |
| 4179 | |
| 4180 | /* |
| 4181 | * TCP receive function for the ESTABLISHED state. |
| 4182 | * |
| 4183 | * It is split into a fast path and a slow path. The fast path is |
| 4184 | * disabled when: |
| 4185 | * - A zero window was announced from us - zero window probing |
| 4186 | * is only handled properly in the slow path. |
| 4187 | * - Out of order segments arrived. |
| 4188 | * - Urgent data is expected. |
| 4189 | * - There is no buffer space left |
| 4190 | * - Unexpected TCP flags/window values/header lengths are received |
| 4191 | * (detected by checking the TCP header against pred_flags) |
| 4192 | * - Data is sent in both directions. Fast path only supports pure senders |
| 4193 | * or pure receivers (this means either the sequence number or the ack |
| 4194 | * value must stay constant) |
| 4195 | * - Unexpected TCP option. |
| 4196 | * |
| 4197 | * When these conditions are not satisfied it drops into a standard |
| 4198 | * receive procedure patterned after RFC793 to handle all cases. |
| 4199 | * The first three cases are guaranteed by proper pred_flags setting, |
| 4200 | * the rest is checked inline. Fast processing is turned on in |
| 4201 | * tcp_data_queue when everything is OK. |
| 4202 | */ |
| 4203 | int tcp_rcv_established(struct sock *sk, struct sk_buff *skb, |
| 4204 | struct tcphdr *th, unsigned len) |
| 4205 | { |
| 4206 | struct tcp_sock *tp = tcp_sk(sk); |
| 4207 | |
| 4208 | /* |
| 4209 | * Header prediction. |
| 4210 | * The code loosely follows the one in the famous |
| 4211 | * "30 instruction TCP receive" Van Jacobson mail. |
| 4212 | * |
| 4213 | * Van's trick is to deposit buffers into socket queue |
| 4214 | * on a device interrupt, to call tcp_recv function |
| 4215 | * on the receive process context and checksum and copy |
| 4216 | * the buffer to user space. smart... |
| 4217 | * |
| 4218 | * Our current scheme is not silly either but we take the |
| 4219 | * extra cost of the net_bh soft interrupt processing... |
| 4220 | * We do checksum and copy also but from device to kernel. |
| 4221 | */ |
| 4222 | |
| 4223 | tp->rx_opt.saw_tstamp = 0; |
| 4224 | |
| 4225 | /* pred_flags is 0xS?10 << 16 + snd_wnd |
| 4226 | * if header_predition is to be made |
| 4227 | * 'S' will always be tp->tcp_header_len >> 2 |
| 4228 | * '?' will be 0 for the fast path, otherwise pred_flags is 0 to |
| 4229 | * turn it off (when there are holes in the receive |
| 4230 | * space for instance) |
| 4231 | * PSH flag is ignored. |
| 4232 | */ |
| 4233 | |
| 4234 | if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags && |
| 4235 | TCP_SKB_CB(skb)->seq == tp->rcv_nxt) { |
| 4236 | int tcp_header_len = tp->tcp_header_len; |
| 4237 | |
| 4238 | /* Timestamp header prediction: tcp_header_len |
| 4239 | * is automatically equal to th->doff*4 due to pred_flags |
| 4240 | * match. |
| 4241 | */ |
| 4242 | |
| 4243 | /* Check timestamp */ |
| 4244 | if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) { |
| 4245 | __u32 *ptr = (__u32 *)(th + 1); |
| 4246 | |
| 4247 | /* No? Slow path! */ |
| 4248 | if (*ptr != ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
| 4249 | | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) |
| 4250 | goto slow_path; |
| 4251 | |
| 4252 | tp->rx_opt.saw_tstamp = 1; |
| 4253 | ++ptr; |
| 4254 | tp->rx_opt.rcv_tsval = ntohl(*ptr); |
| 4255 | ++ptr; |
| 4256 | tp->rx_opt.rcv_tsecr = ntohl(*ptr); |
| 4257 | |
| 4258 | /* If PAWS failed, check it more carefully in slow path */ |
| 4259 | if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) < 0) |
| 4260 | goto slow_path; |
| 4261 | |
| 4262 | /* DO NOT update ts_recent here, if checksum fails |
| 4263 | * and timestamp was corrupted part, it will result |
| 4264 | * in a hung connection since we will drop all |
| 4265 | * future packets due to the PAWS test. |
| 4266 | */ |
| 4267 | } |
| 4268 | |
| 4269 | if (len <= tcp_header_len) { |
| 4270 | /* Bulk data transfer: sender */ |
| 4271 | if (len == tcp_header_len) { |
| 4272 | /* Predicted packet is in window by definition. |
| 4273 | * seq == rcv_nxt and rcv_wup <= rcv_nxt. |
| 4274 | * Hence, check seq<=rcv_wup reduces to: |
| 4275 | */ |
| 4276 | if (tcp_header_len == |
| 4277 | (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) && |
| 4278 | tp->rcv_nxt == tp->rcv_wup) |
| 4279 | tcp_store_ts_recent(tp); |
| 4280 | |
| 4281 | tcp_rcv_rtt_measure_ts(tp, skb); |
| 4282 | |
| 4283 | /* We know that such packets are checksummed |
| 4284 | * on entry. |
| 4285 | */ |
| 4286 | tcp_ack(sk, skb, 0); |
| 4287 | __kfree_skb(skb); |
| 4288 | tcp_data_snd_check(sk); |
| 4289 | return 0; |
| 4290 | } else { /* Header too small */ |
| 4291 | TCP_INC_STATS_BH(TCP_MIB_INERRS); |
| 4292 | goto discard; |
| 4293 | } |
| 4294 | } else { |
| 4295 | int eaten = 0; |
| 4296 | |
| 4297 | if (tp->ucopy.task == current && |
| 4298 | tp->copied_seq == tp->rcv_nxt && |
| 4299 | len - tcp_header_len <= tp->ucopy.len && |
| 4300 | sock_owned_by_user(sk)) { |
| 4301 | __set_current_state(TASK_RUNNING); |
| 4302 | |
| 4303 | if (!tcp_copy_to_iovec(sk, skb, tcp_header_len)) { |
| 4304 | /* Predicted packet is in window by definition. |
| 4305 | * seq == rcv_nxt and rcv_wup <= rcv_nxt. |
| 4306 | * Hence, check seq<=rcv_wup reduces to: |
| 4307 | */ |
| 4308 | if (tcp_header_len == |
| 4309 | (sizeof(struct tcphdr) + |
| 4310 | TCPOLEN_TSTAMP_ALIGNED) && |
| 4311 | tp->rcv_nxt == tp->rcv_wup) |
| 4312 | tcp_store_ts_recent(tp); |
| 4313 | |
| 4314 | tcp_rcv_rtt_measure_ts(tp, skb); |
| 4315 | |
| 4316 | __skb_pull(skb, tcp_header_len); |
| 4317 | tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; |
| 4318 | NET_INC_STATS_BH(LINUX_MIB_TCPHPHITSTOUSER); |
| 4319 | eaten = 1; |
| 4320 | } |
| 4321 | } |
| 4322 | if (!eaten) { |
| 4323 | if (tcp_checksum_complete_user(sk, skb)) |
| 4324 | goto csum_error; |
| 4325 | |
| 4326 | /* Predicted packet is in window by definition. |
| 4327 | * seq == rcv_nxt and rcv_wup <= rcv_nxt. |
| 4328 | * Hence, check seq<=rcv_wup reduces to: |
| 4329 | */ |
| 4330 | if (tcp_header_len == |
| 4331 | (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) && |
| 4332 | tp->rcv_nxt == tp->rcv_wup) |
| 4333 | tcp_store_ts_recent(tp); |
| 4334 | |
| 4335 | tcp_rcv_rtt_measure_ts(tp, skb); |
| 4336 | |
| 4337 | if ((int)skb->truesize > sk->sk_forward_alloc) |
| 4338 | goto step5; |
| 4339 | |
| 4340 | NET_INC_STATS_BH(LINUX_MIB_TCPHPHITS); |
| 4341 | |
| 4342 | /* Bulk data transfer: receiver */ |
| 4343 | __skb_pull(skb,tcp_header_len); |
| 4344 | __skb_queue_tail(&sk->sk_receive_queue, skb); |
| 4345 | sk_stream_set_owner_r(skb, sk); |
| 4346 | tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; |
| 4347 | } |
| 4348 | |
| 4349 | tcp_event_data_recv(sk, tp, skb); |
| 4350 | |
| 4351 | if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) { |
| 4352 | /* Well, only one small jumplet in fast path... */ |
| 4353 | tcp_ack(sk, skb, FLAG_DATA); |
| 4354 | tcp_data_snd_check(sk); |
| 4355 | if (!tcp_ack_scheduled(tp)) |
| 4356 | goto no_ack; |
| 4357 | } |
| 4358 | |
| 4359 | if (eaten) { |
| 4360 | if (tcp_in_quickack_mode(tp)) { |
| 4361 | tcp_send_ack(sk); |
| 4362 | } else { |
| 4363 | tcp_send_delayed_ack(sk); |
| 4364 | } |
| 4365 | } else { |
| 4366 | __tcp_ack_snd_check(sk, 0); |
| 4367 | } |
| 4368 | |
| 4369 | no_ack: |
| 4370 | if (eaten) |
| 4371 | __kfree_skb(skb); |
| 4372 | else |
| 4373 | sk->sk_data_ready(sk, 0); |
| 4374 | return 0; |
| 4375 | } |
| 4376 | } |
| 4377 | |
| 4378 | slow_path: |
| 4379 | if (len < (th->doff<<2) || tcp_checksum_complete_user(sk, skb)) |
| 4380 | goto csum_error; |
| 4381 | |
| 4382 | /* |
| 4383 | * RFC1323: H1. Apply PAWS check first. |
| 4384 | */ |
| 4385 | if (tcp_fast_parse_options(skb, th, tp) && tp->rx_opt.saw_tstamp && |
| 4386 | tcp_paws_discard(tp, skb)) { |
| 4387 | if (!th->rst) { |
| 4388 | NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED); |
| 4389 | tcp_send_dupack(sk, skb); |
| 4390 | goto discard; |
| 4391 | } |
| 4392 | /* Resets are accepted even if PAWS failed. |
| 4393 | |
| 4394 | ts_recent update must be made after we are sure |
| 4395 | that the packet is in window. |
| 4396 | */ |
| 4397 | } |
| 4398 | |
| 4399 | /* |
| 4400 | * Standard slow path. |
| 4401 | */ |
| 4402 | |
| 4403 | if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) { |
| 4404 | /* RFC793, page 37: "In all states except SYN-SENT, all reset |
| 4405 | * (RST) segments are validated by checking their SEQ-fields." |
| 4406 | * And page 69: "If an incoming segment is not acceptable, |
| 4407 | * an acknowledgment should be sent in reply (unless the RST bit |
| 4408 | * is set, if so drop the segment and return)". |
| 4409 | */ |
| 4410 | if (!th->rst) |
| 4411 | tcp_send_dupack(sk, skb); |
| 4412 | goto discard; |
| 4413 | } |
| 4414 | |
| 4415 | if(th->rst) { |
| 4416 | tcp_reset(sk); |
| 4417 | goto discard; |
| 4418 | } |
| 4419 | |
| 4420 | tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq); |
| 4421 | |
| 4422 | if (th->syn && !before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) { |
| 4423 | TCP_INC_STATS_BH(TCP_MIB_INERRS); |
| 4424 | NET_INC_STATS_BH(LINUX_MIB_TCPABORTONSYN); |
| 4425 | tcp_reset(sk); |
| 4426 | return 1; |
| 4427 | } |
| 4428 | |
| 4429 | step5: |
| 4430 | if(th->ack) |
| 4431 | tcp_ack(sk, skb, FLAG_SLOWPATH); |
| 4432 | |
| 4433 | tcp_rcv_rtt_measure_ts(tp, skb); |
| 4434 | |
| 4435 | /* Process urgent data. */ |
| 4436 | tcp_urg(sk, skb, th); |
| 4437 | |
| 4438 | /* step 7: process the segment text */ |
| 4439 | tcp_data_queue(sk, skb); |
| 4440 | |
| 4441 | tcp_data_snd_check(sk); |
| 4442 | tcp_ack_snd_check(sk); |
| 4443 | return 0; |
| 4444 | |
| 4445 | csum_error: |
| 4446 | TCP_INC_STATS_BH(TCP_MIB_INERRS); |
| 4447 | |
| 4448 | discard: |
| 4449 | __kfree_skb(skb); |
| 4450 | return 0; |
| 4451 | } |
| 4452 | |
| 4453 | static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb, |
| 4454 | struct tcphdr *th, unsigned len) |
| 4455 | { |
| 4456 | struct tcp_sock *tp = tcp_sk(sk); |
| 4457 | int saved_clamp = tp->rx_opt.mss_clamp; |
| 4458 | |
| 4459 | tcp_parse_options(skb, &tp->rx_opt, 0); |
| 4460 | |
| 4461 | if (th->ack) { |
| 4462 | /* rfc793: |
| 4463 | * "If the state is SYN-SENT then |
| 4464 | * first check the ACK bit |
| 4465 | * If the ACK bit is set |
| 4466 | * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send |
| 4467 | * a reset (unless the RST bit is set, if so drop |
| 4468 | * the segment and return)" |
| 4469 | * |
| 4470 | * We do not send data with SYN, so that RFC-correct |
| 4471 | * test reduces to: |
| 4472 | */ |
| 4473 | if (TCP_SKB_CB(skb)->ack_seq != tp->snd_nxt) |
| 4474 | goto reset_and_undo; |
| 4475 | |
| 4476 | if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && |
| 4477 | !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp, |
| 4478 | tcp_time_stamp)) { |
| 4479 | NET_INC_STATS_BH(LINUX_MIB_PAWSACTIVEREJECTED); |
| 4480 | goto reset_and_undo; |
| 4481 | } |
| 4482 | |
| 4483 | /* Now ACK is acceptable. |
| 4484 | * |
| 4485 | * "If the RST bit is set |
| 4486 | * If the ACK was acceptable then signal the user "error: |
| 4487 | * connection reset", drop the segment, enter CLOSED state, |
| 4488 | * delete TCB, and return." |
| 4489 | */ |
| 4490 | |
| 4491 | if (th->rst) { |
| 4492 | tcp_reset(sk); |
| 4493 | goto discard; |
| 4494 | } |
| 4495 | |
| 4496 | /* rfc793: |
| 4497 | * "fifth, if neither of the SYN or RST bits is set then |
| 4498 | * drop the segment and return." |
| 4499 | * |
| 4500 | * See note below! |
| 4501 | * --ANK(990513) |
| 4502 | */ |
| 4503 | if (!th->syn) |
| 4504 | goto discard_and_undo; |
| 4505 | |
| 4506 | /* rfc793: |
| 4507 | * "If the SYN bit is on ... |
| 4508 | * are acceptable then ... |
| 4509 | * (our SYN has been ACKed), change the connection |
| 4510 | * state to ESTABLISHED..." |
| 4511 | */ |
| 4512 | |
| 4513 | TCP_ECN_rcv_synack(tp, th); |
| 4514 | if (tp->ecn_flags&TCP_ECN_OK) |
| 4515 | sock_set_flag(sk, SOCK_NO_LARGESEND); |
| 4516 | |
| 4517 | tp->snd_wl1 = TCP_SKB_CB(skb)->seq; |
| 4518 | tcp_ack(sk, skb, FLAG_SLOWPATH); |
| 4519 | |
| 4520 | /* Ok.. it's good. Set up sequence numbers and |
| 4521 | * move to established. |
| 4522 | */ |
| 4523 | tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1; |
| 4524 | tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1; |
| 4525 | |
| 4526 | /* RFC1323: The window in SYN & SYN/ACK segments is |
| 4527 | * never scaled. |
| 4528 | */ |
| 4529 | tp->snd_wnd = ntohs(th->window); |
| 4530 | tcp_init_wl(tp, TCP_SKB_CB(skb)->ack_seq, TCP_SKB_CB(skb)->seq); |
| 4531 | |
| 4532 | if (!tp->rx_opt.wscale_ok) { |
| 4533 | tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0; |
| 4534 | tp->window_clamp = min(tp->window_clamp, 65535U); |
| 4535 | } |
| 4536 | |
| 4537 | if (tp->rx_opt.saw_tstamp) { |
| 4538 | tp->rx_opt.tstamp_ok = 1; |
| 4539 | tp->tcp_header_len = |
| 4540 | sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED; |
| 4541 | tp->advmss -= TCPOLEN_TSTAMP_ALIGNED; |
| 4542 | tcp_store_ts_recent(tp); |
| 4543 | } else { |
| 4544 | tp->tcp_header_len = sizeof(struct tcphdr); |
| 4545 | } |
| 4546 | |
| 4547 | if (tp->rx_opt.sack_ok && sysctl_tcp_fack) |
| 4548 | tp->rx_opt.sack_ok |= 2; |
| 4549 | |
| 4550 | tcp_sync_mss(sk, tp->pmtu_cookie); |
| 4551 | tcp_initialize_rcv_mss(sk); |
| 4552 | |
| 4553 | /* Remember, tcp_poll() does not lock socket! |
| 4554 | * Change state from SYN-SENT only after copied_seq |
| 4555 | * is initialized. */ |
| 4556 | tp->copied_seq = tp->rcv_nxt; |
| 4557 | mb(); |
| 4558 | tcp_set_state(sk, TCP_ESTABLISHED); |
| 4559 | |
| 4560 | /* Make sure socket is routed, for correct metrics. */ |
| 4561 | tp->af_specific->rebuild_header(sk); |
| 4562 | |
| 4563 | tcp_init_metrics(sk); |
| 4564 | |
| 4565 | /* Prevent spurious tcp_cwnd_restart() on first data |
| 4566 | * packet. |
| 4567 | */ |
| 4568 | tp->lsndtime = tcp_time_stamp; |
| 4569 | |
| 4570 | tcp_init_buffer_space(sk); |
| 4571 | |
| 4572 | if (sock_flag(sk, SOCK_KEEPOPEN)) |
| 4573 | tcp_reset_keepalive_timer(sk, keepalive_time_when(tp)); |
| 4574 | |
| 4575 | if (!tp->rx_opt.snd_wscale) |
| 4576 | __tcp_fast_path_on(tp, tp->snd_wnd); |
| 4577 | else |
| 4578 | tp->pred_flags = 0; |
| 4579 | |
| 4580 | if (!sock_flag(sk, SOCK_DEAD)) { |
| 4581 | sk->sk_state_change(sk); |
| 4582 | sk_wake_async(sk, 0, POLL_OUT); |
| 4583 | } |
| 4584 | |
| 4585 | if (sk->sk_write_pending || tp->defer_accept || tp->ack.pingpong) { |
| 4586 | /* Save one ACK. Data will be ready after |
| 4587 | * several ticks, if write_pending is set. |
| 4588 | * |
| 4589 | * It may be deleted, but with this feature tcpdumps |
| 4590 | * look so _wonderfully_ clever, that I was not able |
| 4591 | * to stand against the temptation 8) --ANK |
| 4592 | */ |
| 4593 | tcp_schedule_ack(tp); |
| 4594 | tp->ack.lrcvtime = tcp_time_stamp; |
| 4595 | tp->ack.ato = TCP_ATO_MIN; |
| 4596 | tcp_incr_quickack(tp); |
| 4597 | tcp_enter_quickack_mode(tp); |
| 4598 | tcp_reset_xmit_timer(sk, TCP_TIME_DACK, TCP_DELACK_MAX); |
| 4599 | |
| 4600 | discard: |
| 4601 | __kfree_skb(skb); |
| 4602 | return 0; |
| 4603 | } else { |
| 4604 | tcp_send_ack(sk); |
| 4605 | } |
| 4606 | return -1; |
| 4607 | } |
| 4608 | |
| 4609 | /* No ACK in the segment */ |
| 4610 | |
| 4611 | if (th->rst) { |
| 4612 | /* rfc793: |
| 4613 | * "If the RST bit is set |
| 4614 | * |
| 4615 | * Otherwise (no ACK) drop the segment and return." |
| 4616 | */ |
| 4617 | |
| 4618 | goto discard_and_undo; |
| 4619 | } |
| 4620 | |
| 4621 | /* PAWS check. */ |
| 4622 | if (tp->rx_opt.ts_recent_stamp && tp->rx_opt.saw_tstamp && tcp_paws_check(&tp->rx_opt, 0)) |
| 4623 | goto discard_and_undo; |
| 4624 | |
| 4625 | if (th->syn) { |
| 4626 | /* We see SYN without ACK. It is attempt of |
| 4627 | * simultaneous connect with crossed SYNs. |
| 4628 | * Particularly, it can be connect to self. |
| 4629 | */ |
| 4630 | tcp_set_state(sk, TCP_SYN_RECV); |
| 4631 | |
| 4632 | if (tp->rx_opt.saw_tstamp) { |
| 4633 | tp->rx_opt.tstamp_ok = 1; |
| 4634 | tcp_store_ts_recent(tp); |
| 4635 | tp->tcp_header_len = |
| 4636 | sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED; |
| 4637 | } else { |
| 4638 | tp->tcp_header_len = sizeof(struct tcphdr); |
| 4639 | } |
| 4640 | |
| 4641 | tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1; |
| 4642 | tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1; |
| 4643 | |
| 4644 | /* RFC1323: The window in SYN & SYN/ACK segments is |
| 4645 | * never scaled. |
| 4646 | */ |
| 4647 | tp->snd_wnd = ntohs(th->window); |
| 4648 | tp->snd_wl1 = TCP_SKB_CB(skb)->seq; |
| 4649 | tp->max_window = tp->snd_wnd; |
| 4650 | |
| 4651 | TCP_ECN_rcv_syn(tp, th); |
| 4652 | if (tp->ecn_flags&TCP_ECN_OK) |
| 4653 | sock_set_flag(sk, SOCK_NO_LARGESEND); |
| 4654 | |
| 4655 | tcp_sync_mss(sk, tp->pmtu_cookie); |
| 4656 | tcp_initialize_rcv_mss(sk); |
| 4657 | |
| 4658 | |
| 4659 | tcp_send_synack(sk); |
| 4660 | #if 0 |
| 4661 | /* Note, we could accept data and URG from this segment. |
| 4662 | * There are no obstacles to make this. |
| 4663 | * |
| 4664 | * However, if we ignore data in ACKless segments sometimes, |
| 4665 | * we have no reasons to accept it sometimes. |
| 4666 | * Also, seems the code doing it in step6 of tcp_rcv_state_process |
| 4667 | * is not flawless. So, discard packet for sanity. |
| 4668 | * Uncomment this return to process the data. |
| 4669 | */ |
| 4670 | return -1; |
| 4671 | #else |
| 4672 | goto discard; |
| 4673 | #endif |
| 4674 | } |
| 4675 | /* "fifth, if neither of the SYN or RST bits is set then |
| 4676 | * drop the segment and return." |
| 4677 | */ |
| 4678 | |
| 4679 | discard_and_undo: |
| 4680 | tcp_clear_options(&tp->rx_opt); |
| 4681 | tp->rx_opt.mss_clamp = saved_clamp; |
| 4682 | goto discard; |
| 4683 | |
| 4684 | reset_and_undo: |
| 4685 | tcp_clear_options(&tp->rx_opt); |
| 4686 | tp->rx_opt.mss_clamp = saved_clamp; |
| 4687 | return 1; |
| 4688 | } |
| 4689 | |
| 4690 | |
| 4691 | /* |
| 4692 | * This function implements the receiving procedure of RFC 793 for |
| 4693 | * all states except ESTABLISHED and TIME_WAIT. |
| 4694 | * It's called from both tcp_v4_rcv and tcp_v6_rcv and should be |
| 4695 | * address independent. |
| 4696 | */ |
| 4697 | |
| 4698 | int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb, |
| 4699 | struct tcphdr *th, unsigned len) |
| 4700 | { |
| 4701 | struct tcp_sock *tp = tcp_sk(sk); |
| 4702 | int queued = 0; |
| 4703 | |
| 4704 | tp->rx_opt.saw_tstamp = 0; |
| 4705 | |
| 4706 | switch (sk->sk_state) { |
| 4707 | case TCP_CLOSE: |
| 4708 | goto discard; |
| 4709 | |
| 4710 | case TCP_LISTEN: |
| 4711 | if(th->ack) |
| 4712 | return 1; |
| 4713 | |
| 4714 | if(th->rst) |
| 4715 | goto discard; |
| 4716 | |
| 4717 | if(th->syn) { |
| 4718 | if(tp->af_specific->conn_request(sk, skb) < 0) |
| 4719 | return 1; |
| 4720 | |
| 4721 | init_westwood(sk); |
| 4722 | init_bictcp(tp); |
| 4723 | |
| 4724 | /* Now we have several options: In theory there is |
| 4725 | * nothing else in the frame. KA9Q has an option to |
| 4726 | * send data with the syn, BSD accepts data with the |
| 4727 | * syn up to the [to be] advertised window and |
| 4728 | * Solaris 2.1 gives you a protocol error. For now |
| 4729 | * we just ignore it, that fits the spec precisely |
| 4730 | * and avoids incompatibilities. It would be nice in |
| 4731 | * future to drop through and process the data. |
| 4732 | * |
| 4733 | * Now that TTCP is starting to be used we ought to |
| 4734 | * queue this data. |
| 4735 | * But, this leaves one open to an easy denial of |
| 4736 | * service attack, and SYN cookies can't defend |
| 4737 | * against this problem. So, we drop the data |
| 4738 | * in the interest of security over speed. |
| 4739 | */ |
| 4740 | goto discard; |
| 4741 | } |
| 4742 | goto discard; |
| 4743 | |
| 4744 | case TCP_SYN_SENT: |
| 4745 | init_westwood(sk); |
| 4746 | init_bictcp(tp); |
| 4747 | |
| 4748 | queued = tcp_rcv_synsent_state_process(sk, skb, th, len); |
| 4749 | if (queued >= 0) |
| 4750 | return queued; |
| 4751 | |
| 4752 | /* Do step6 onward by hand. */ |
| 4753 | tcp_urg(sk, skb, th); |
| 4754 | __kfree_skb(skb); |
| 4755 | tcp_data_snd_check(sk); |
| 4756 | return 0; |
| 4757 | } |
| 4758 | |
| 4759 | if (tcp_fast_parse_options(skb, th, tp) && tp->rx_opt.saw_tstamp && |
| 4760 | tcp_paws_discard(tp, skb)) { |
| 4761 | if (!th->rst) { |
| 4762 | NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED); |
| 4763 | tcp_send_dupack(sk, skb); |
| 4764 | goto discard; |
| 4765 | } |
| 4766 | /* Reset is accepted even if it did not pass PAWS. */ |
| 4767 | } |
| 4768 | |
| 4769 | /* step 1: check sequence number */ |
| 4770 | if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) { |
| 4771 | if (!th->rst) |
| 4772 | tcp_send_dupack(sk, skb); |
| 4773 | goto discard; |
| 4774 | } |
| 4775 | |
| 4776 | /* step 2: check RST bit */ |
| 4777 | if(th->rst) { |
| 4778 | tcp_reset(sk); |
| 4779 | goto discard; |
| 4780 | } |
| 4781 | |
| 4782 | tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq); |
| 4783 | |
| 4784 | /* step 3: check security and precedence [ignored] */ |
| 4785 | |
| 4786 | /* step 4: |
| 4787 | * |
| 4788 | * Check for a SYN in window. |
| 4789 | */ |
| 4790 | if (th->syn && !before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) { |
| 4791 | NET_INC_STATS_BH(LINUX_MIB_TCPABORTONSYN); |
| 4792 | tcp_reset(sk); |
| 4793 | return 1; |
| 4794 | } |
| 4795 | |
| 4796 | /* step 5: check the ACK field */ |
| 4797 | if (th->ack) { |
| 4798 | int acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH); |
| 4799 | |
| 4800 | switch(sk->sk_state) { |
| 4801 | case TCP_SYN_RECV: |
| 4802 | if (acceptable) { |
| 4803 | tp->copied_seq = tp->rcv_nxt; |
| 4804 | mb(); |
| 4805 | tcp_set_state(sk, TCP_ESTABLISHED); |
| 4806 | sk->sk_state_change(sk); |
| 4807 | |
| 4808 | /* Note, that this wakeup is only for marginal |
| 4809 | * crossed SYN case. Passively open sockets |
| 4810 | * are not waked up, because sk->sk_sleep == |
| 4811 | * NULL and sk->sk_socket == NULL. |
| 4812 | */ |
| 4813 | if (sk->sk_socket) { |
| 4814 | sk_wake_async(sk,0,POLL_OUT); |
| 4815 | } |
| 4816 | |
| 4817 | tp->snd_una = TCP_SKB_CB(skb)->ack_seq; |
| 4818 | tp->snd_wnd = ntohs(th->window) << |
| 4819 | tp->rx_opt.snd_wscale; |
| 4820 | tcp_init_wl(tp, TCP_SKB_CB(skb)->ack_seq, |
| 4821 | TCP_SKB_CB(skb)->seq); |
| 4822 | |
| 4823 | /* tcp_ack considers this ACK as duplicate |
| 4824 | * and does not calculate rtt. |
| 4825 | * Fix it at least with timestamps. |
| 4826 | */ |
| 4827 | if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && |
| 4828 | !tp->srtt) |
| 4829 | tcp_ack_saw_tstamp(tp, 0); |
| 4830 | |
| 4831 | if (tp->rx_opt.tstamp_ok) |
| 4832 | tp->advmss -= TCPOLEN_TSTAMP_ALIGNED; |
| 4833 | |
| 4834 | /* Make sure socket is routed, for |
| 4835 | * correct metrics. |
| 4836 | */ |
| 4837 | tp->af_specific->rebuild_header(sk); |
| 4838 | |
| 4839 | tcp_init_metrics(sk); |
| 4840 | |
| 4841 | /* Prevent spurious tcp_cwnd_restart() on |
| 4842 | * first data packet. |
| 4843 | */ |
| 4844 | tp->lsndtime = tcp_time_stamp; |
| 4845 | |
| 4846 | tcp_initialize_rcv_mss(sk); |
| 4847 | tcp_init_buffer_space(sk); |
| 4848 | tcp_fast_path_on(tp); |
| 4849 | } else { |
| 4850 | return 1; |
| 4851 | } |
| 4852 | break; |
| 4853 | |
| 4854 | case TCP_FIN_WAIT1: |
| 4855 | if (tp->snd_una == tp->write_seq) { |
| 4856 | tcp_set_state(sk, TCP_FIN_WAIT2); |
| 4857 | sk->sk_shutdown |= SEND_SHUTDOWN; |
| 4858 | dst_confirm(sk->sk_dst_cache); |
| 4859 | |
| 4860 | if (!sock_flag(sk, SOCK_DEAD)) |
| 4861 | /* Wake up lingering close() */ |
| 4862 | sk->sk_state_change(sk); |
| 4863 | else { |
| 4864 | int tmo; |
| 4865 | |
| 4866 | if (tp->linger2 < 0 || |
| 4867 | (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && |
| 4868 | after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt))) { |
| 4869 | tcp_done(sk); |
| 4870 | NET_INC_STATS_BH(LINUX_MIB_TCPABORTONDATA); |
| 4871 | return 1; |
| 4872 | } |
| 4873 | |
| 4874 | tmo = tcp_fin_time(tp); |
| 4875 | if (tmo > TCP_TIMEWAIT_LEN) { |
| 4876 | tcp_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN); |
| 4877 | } else if (th->fin || sock_owned_by_user(sk)) { |
| 4878 | /* Bad case. We could lose such FIN otherwise. |
| 4879 | * It is not a big problem, but it looks confusing |
| 4880 | * and not so rare event. We still can lose it now, |
| 4881 | * if it spins in bh_lock_sock(), but it is really |
| 4882 | * marginal case. |
| 4883 | */ |
| 4884 | tcp_reset_keepalive_timer(sk, tmo); |
| 4885 | } else { |
| 4886 | tcp_time_wait(sk, TCP_FIN_WAIT2, tmo); |
| 4887 | goto discard; |
| 4888 | } |
| 4889 | } |
| 4890 | } |
| 4891 | break; |
| 4892 | |
| 4893 | case TCP_CLOSING: |
| 4894 | if (tp->snd_una == tp->write_seq) { |
| 4895 | tcp_time_wait(sk, TCP_TIME_WAIT, 0); |
| 4896 | goto discard; |
| 4897 | } |
| 4898 | break; |
| 4899 | |
| 4900 | case TCP_LAST_ACK: |
| 4901 | if (tp->snd_una == tp->write_seq) { |
| 4902 | tcp_update_metrics(sk); |
| 4903 | tcp_done(sk); |
| 4904 | goto discard; |
| 4905 | } |
| 4906 | break; |
| 4907 | } |
| 4908 | } else |
| 4909 | goto discard; |
| 4910 | |
| 4911 | /* step 6: check the URG bit */ |
| 4912 | tcp_urg(sk, skb, th); |
| 4913 | |
| 4914 | /* step 7: process the segment text */ |
| 4915 | switch (sk->sk_state) { |
| 4916 | case TCP_CLOSE_WAIT: |
| 4917 | case TCP_CLOSING: |
| 4918 | case TCP_LAST_ACK: |
| 4919 | if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) |
| 4920 | break; |
| 4921 | case TCP_FIN_WAIT1: |
| 4922 | case TCP_FIN_WAIT2: |
| 4923 | /* RFC 793 says to queue data in these states, |
| 4924 | * RFC 1122 says we MUST send a reset. |
| 4925 | * BSD 4.4 also does reset. |
| 4926 | */ |
| 4927 | if (sk->sk_shutdown & RCV_SHUTDOWN) { |
| 4928 | if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && |
| 4929 | after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) { |
| 4930 | NET_INC_STATS_BH(LINUX_MIB_TCPABORTONDATA); |
| 4931 | tcp_reset(sk); |
| 4932 | return 1; |
| 4933 | } |
| 4934 | } |
| 4935 | /* Fall through */ |
| 4936 | case TCP_ESTABLISHED: |
| 4937 | tcp_data_queue(sk, skb); |
| 4938 | queued = 1; |
| 4939 | break; |
| 4940 | } |
| 4941 | |
| 4942 | /* tcp_data could move socket to TIME-WAIT */ |
| 4943 | if (sk->sk_state != TCP_CLOSE) { |
| 4944 | tcp_data_snd_check(sk); |
| 4945 | tcp_ack_snd_check(sk); |
| 4946 | } |
| 4947 | |
| 4948 | if (!queued) { |
| 4949 | discard: |
| 4950 | __kfree_skb(skb); |
| 4951 | } |
| 4952 | return 0; |
| 4953 | } |
| 4954 | |
| 4955 | EXPORT_SYMBOL(sysctl_tcp_ecn); |
| 4956 | EXPORT_SYMBOL(sysctl_tcp_reordering); |
| 4957 | EXPORT_SYMBOL(tcp_parse_options); |
| 4958 | EXPORT_SYMBOL(tcp_rcv_established); |
| 4959 | EXPORT_SYMBOL(tcp_rcv_state_process); |