Merge branch 'for-33' of git://repo.or.cz/linux-kbuild
[linux-2.6-block.git] / net / ipv4 / tcp_input.c
CommitLineData
1da177e4
LT
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 *
02c30a84 8 * Authors: Ross Biro
1da177e4
LT
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Mark Evans, <evansmp@uhura.aston.ac.uk>
11 * Corey Minyard <wf-rch!minyard@relay.EU.net>
12 * Florian La Roche, <flla@stud.uni-sb.de>
13 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
14 * Linus Torvalds, <torvalds@cs.helsinki.fi>
15 * Alan Cox, <gw4pts@gw4pts.ampr.org>
16 * Matthew Dillon, <dillon@apollo.west.oic.com>
17 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
18 * Jorge Cwik, <jorge@laser.satlink.net>
19 */
20
21/*
22 * Changes:
23 * Pedro Roque : Fast Retransmit/Recovery.
24 * Two receive queues.
25 * Retransmit queue handled by TCP.
26 * Better retransmit timer handling.
27 * New congestion avoidance.
28 * Header prediction.
29 * Variable renaming.
30 *
31 * Eric : Fast Retransmit.
32 * Randy Scott : MSS option defines.
33 * Eric Schenk : Fixes to slow start algorithm.
34 * Eric Schenk : Yet another double ACK bug.
35 * Eric Schenk : Delayed ACK bug fixes.
36 * Eric Schenk : Floyd style fast retrans war avoidance.
37 * David S. Miller : Don't allow zero congestion window.
38 * Eric Schenk : Fix retransmitter so that it sends
39 * next packet on ack of previous packet.
40 * Andi Kleen : Moved open_request checking here
41 * and process RSTs for open_requests.
42 * Andi Kleen : Better prune_queue, and other fixes.
caa20d9a 43 * Andrey Savochkin: Fix RTT measurements in the presence of
1da177e4
LT
44 * timestamps.
45 * Andrey Savochkin: Check sequence numbers correctly when
46 * removing SACKs due to in sequence incoming
47 * data segments.
48 * Andi Kleen: Make sure we never ack data there is not
49 * enough room for. Also make this condition
50 * a fatal error if it might still happen.
e905a9ed 51 * Andi Kleen: Add tcp_measure_rcv_mss to make
1da177e4 52 * connections with MSS<min(MTU,ann. MSS)
e905a9ed 53 * work without delayed acks.
1da177e4
LT
54 * Andi Kleen: Process packets with PSH set in the
55 * fast path.
56 * J Hadi Salim: ECN support
57 * Andrei Gurtov,
58 * Pasi Sarolahti,
59 * Panu Kuhlberg: Experimental audit of TCP (re)transmission
60 * engine. Lots of bugs are found.
61 * Pasi Sarolahti: F-RTO for dealing with spurious RTOs
1da177e4
LT
62 */
63
1da177e4
LT
64#include <linux/mm.h>
65#include <linux/module.h>
66#include <linux/sysctl.h>
a0bffffc 67#include <linux/kernel.h>
5ffc02a1 68#include <net/dst.h>
1da177e4
LT
69#include <net/tcp.h>
70#include <net/inet_common.h>
71#include <linux/ipsec.h>
72#include <asm/unaligned.h>
1a2449a8 73#include <net/netdma.h>
1da177e4 74
ab32ea5d
BH
75int sysctl_tcp_timestamps __read_mostly = 1;
76int sysctl_tcp_window_scaling __read_mostly = 1;
77int sysctl_tcp_sack __read_mostly = 1;
78int sysctl_tcp_fack __read_mostly = 1;
79int sysctl_tcp_reordering __read_mostly = TCP_FASTRETRANS_THRESH;
255cac91 80int sysctl_tcp_ecn __read_mostly = 2;
ab32ea5d
BH
81int sysctl_tcp_dsack __read_mostly = 1;
82int sysctl_tcp_app_win __read_mostly = 31;
83int sysctl_tcp_adv_win_scale __read_mostly = 2;
1da177e4 84
ab32ea5d
BH
85int sysctl_tcp_stdurg __read_mostly;
86int sysctl_tcp_rfc1337 __read_mostly;
87int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
c96fd3d4 88int sysctl_tcp_frto __read_mostly = 2;
3cfe3baa 89int sysctl_tcp_frto_response __read_mostly;
ab32ea5d 90int sysctl_tcp_nometrics_save __read_mostly;
1da177e4 91
ab32ea5d
BH
92int sysctl_tcp_moderate_rcvbuf __read_mostly = 1;
93int sysctl_tcp_abc __read_mostly;
1da177e4 94
1da177e4
LT
95#define FLAG_DATA 0x01 /* Incoming frame contained data. */
96#define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */
97#define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */
98#define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted. */
99#define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */
100#define FLAG_DATA_SACKED 0x20 /* New SACK. */
101#define FLAG_ECE 0x40 /* ECE in this ACK */
102#define FLAG_DATA_LOST 0x80 /* SACK detected data lossage. */
103#define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/
4dc2665e 104#define FLAG_ONLY_ORIG_SACKED 0x200 /* SACKs only non-rexmit sent before RTO */
2e605294 105#define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */
564262c1 106#define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */
009a2e3e 107#define FLAG_NONHEAD_RETRANS_ACKED 0x1000 /* Non-head rexmitted data was ACKed */
cadbd031 108#define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */
1da177e4
LT
109
110#define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED)
111#define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
112#define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE)
113#define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED)
2e605294 114#define FLAG_ANY_PROGRESS (FLAG_FORWARD_PROGRESS|FLAG_SND_UNA_ADVANCED)
1da177e4 115
1da177e4 116#define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
bdf1ee5d 117#define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
1da177e4 118
e905a9ed 119/* Adapt the MSS value used to make delayed ack decision to the
1da177e4 120 * real world.
e905a9ed 121 */
056834d9 122static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb)
1da177e4 123{
463c84b9 124 struct inet_connection_sock *icsk = inet_csk(sk);
e905a9ed 125 const unsigned int lss = icsk->icsk_ack.last_seg_size;
463c84b9 126 unsigned int len;
1da177e4 127
e905a9ed 128 icsk->icsk_ack.last_seg_size = 0;
1da177e4
LT
129
130 /* skb->len may jitter because of SACKs, even if peer
131 * sends good full-sized frames.
132 */
056834d9 133 len = skb_shinfo(skb)->gso_size ? : skb->len;
463c84b9
ACM
134 if (len >= icsk->icsk_ack.rcv_mss) {
135 icsk->icsk_ack.rcv_mss = len;
1da177e4
LT
136 } else {
137 /* Otherwise, we make more careful check taking into account,
138 * that SACKs block is variable.
139 *
140 * "len" is invariant segment length, including TCP header.
141 */
9c70220b 142 len += skb->data - skb_transport_header(skb);
bee7ca9e 143 if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) ||
1da177e4
LT
144 /* If PSH is not set, packet should be
145 * full sized, provided peer TCP is not badly broken.
146 * This observation (if it is correct 8)) allows
147 * to handle super-low mtu links fairly.
148 */
149 (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
aa8223c7 150 !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {
1da177e4
LT
151 /* Subtract also invariant (if peer is RFC compliant),
152 * tcp header plus fixed timestamp option length.
153 * Resulting "len" is MSS free of SACK jitter.
154 */
463c84b9
ACM
155 len -= tcp_sk(sk)->tcp_header_len;
156 icsk->icsk_ack.last_seg_size = len;
1da177e4 157 if (len == lss) {
463c84b9 158 icsk->icsk_ack.rcv_mss = len;
1da177e4
LT
159 return;
160 }
161 }
1ef9696c
AK
162 if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)
163 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;
463c84b9 164 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
1da177e4
LT
165 }
166}
167
463c84b9 168static void tcp_incr_quickack(struct sock *sk)
1da177e4 169{
463c84b9
ACM
170 struct inet_connection_sock *icsk = inet_csk(sk);
171 unsigned quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);
1da177e4 172
056834d9
IJ
173 if (quickacks == 0)
174 quickacks = 2;
463c84b9
ACM
175 if (quickacks > icsk->icsk_ack.quick)
176 icsk->icsk_ack.quick = min(quickacks, TCP_MAX_QUICKACKS);
1da177e4
LT
177}
178
463c84b9 179void tcp_enter_quickack_mode(struct sock *sk)
1da177e4 180{
463c84b9
ACM
181 struct inet_connection_sock *icsk = inet_csk(sk);
182 tcp_incr_quickack(sk);
183 icsk->icsk_ack.pingpong = 0;
184 icsk->icsk_ack.ato = TCP_ATO_MIN;
1da177e4
LT
185}
186
187/* Send ACKs quickly, if "quick" count is not exhausted
188 * and the session is not interactive.
189 */
190
463c84b9 191static inline int tcp_in_quickack_mode(const struct sock *sk)
1da177e4 192{
463c84b9
ACM
193 const struct inet_connection_sock *icsk = inet_csk(sk);
194 return icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong;
1da177e4
LT
195}
196
bdf1ee5d
IJ
197static inline void TCP_ECN_queue_cwr(struct tcp_sock *tp)
198{
056834d9 199 if (tp->ecn_flags & TCP_ECN_OK)
bdf1ee5d
IJ
200 tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
201}
202
203static inline void TCP_ECN_accept_cwr(struct tcp_sock *tp, struct sk_buff *skb)
204{
205 if (tcp_hdr(skb)->cwr)
206 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
207}
208
209static inline void TCP_ECN_withdraw_cwr(struct tcp_sock *tp)
210{
211 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
212}
213
214static inline void TCP_ECN_check_ce(struct tcp_sock *tp, struct sk_buff *skb)
215{
056834d9 216 if (tp->ecn_flags & TCP_ECN_OK) {
bdf1ee5d
IJ
217 if (INET_ECN_is_ce(TCP_SKB_CB(skb)->flags))
218 tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
219 /* Funny extension: if ECT is not set on a segment,
220 * it is surely retransmit. It is not in ECN RFC,
221 * but Linux follows this rule. */
222 else if (INET_ECN_is_not_ect((TCP_SKB_CB(skb)->flags)))
223 tcp_enter_quickack_mode((struct sock *)tp);
224 }
225}
226
227static inline void TCP_ECN_rcv_synack(struct tcp_sock *tp, struct tcphdr *th)
228{
056834d9 229 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr))
bdf1ee5d
IJ
230 tp->ecn_flags &= ~TCP_ECN_OK;
231}
232
233static inline void TCP_ECN_rcv_syn(struct tcp_sock *tp, struct tcphdr *th)
234{
056834d9 235 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr))
bdf1ee5d
IJ
236 tp->ecn_flags &= ~TCP_ECN_OK;
237}
238
239static inline int TCP_ECN_rcv_ecn_echo(struct tcp_sock *tp, struct tcphdr *th)
240{
056834d9 241 if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK))
bdf1ee5d
IJ
242 return 1;
243 return 0;
244}
245
1da177e4
LT
246/* Buffer size and advertised window tuning.
247 *
248 * 1. Tuning sk->sk_sndbuf, when connection enters established state.
249 */
250
251static void tcp_fixup_sndbuf(struct sock *sk)
252{
253 int sndmem = tcp_sk(sk)->rx_opt.mss_clamp + MAX_TCP_HEADER + 16 +
254 sizeof(struct sk_buff);
255
256 if (sk->sk_sndbuf < 3 * sndmem)
257 sk->sk_sndbuf = min(3 * sndmem, sysctl_tcp_wmem[2]);
258}
259
260/* 2. Tuning advertised window (window_clamp, rcv_ssthresh)
261 *
262 * All tcp_full_space() is split to two parts: "network" buffer, allocated
263 * forward and advertised in receiver window (tp->rcv_wnd) and
264 * "application buffer", required to isolate scheduling/application
265 * latencies from network.
266 * window_clamp is maximal advertised window. It can be less than
267 * tcp_full_space(), in this case tcp_full_space() - window_clamp
268 * is reserved for "application" buffer. The less window_clamp is
269 * the smoother our behaviour from viewpoint of network, but the lower
270 * throughput and the higher sensitivity of the connection to losses. 8)
271 *
272 * rcv_ssthresh is more strict window_clamp used at "slow start"
273 * phase to predict further behaviour of this connection.
274 * It is used for two goals:
275 * - to enforce header prediction at sender, even when application
276 * requires some significant "application buffer". It is check #1.
277 * - to prevent pruning of receive queue because of misprediction
278 * of receiver window. Check #2.
279 *
280 * The scheme does not work when sender sends good segments opening
caa20d9a 281 * window and then starts to feed us spaghetti. But it should work
1da177e4
LT
282 * in common situations. Otherwise, we have to rely on queue collapsing.
283 */
284
285/* Slow part of check#2. */
9e412ba7 286static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb)
1da177e4 287{
9e412ba7 288 struct tcp_sock *tp = tcp_sk(sk);
1da177e4 289 /* Optimize this! */
dfd4f0ae
ED
290 int truesize = tcp_win_from_space(skb->truesize) >> 1;
291 int window = tcp_win_from_space(sysctl_tcp_rmem[2]) >> 1;
1da177e4
LT
292
293 while (tp->rcv_ssthresh <= window) {
294 if (truesize <= skb->len)
463c84b9 295 return 2 * inet_csk(sk)->icsk_ack.rcv_mss;
1da177e4
LT
296
297 truesize >>= 1;
298 window >>= 1;
299 }
300 return 0;
301}
302
056834d9 303static void tcp_grow_window(struct sock *sk, struct sk_buff *skb)
1da177e4 304{
9e412ba7
IJ
305 struct tcp_sock *tp = tcp_sk(sk);
306
1da177e4
LT
307 /* Check #1 */
308 if (tp->rcv_ssthresh < tp->window_clamp &&
309 (int)tp->rcv_ssthresh < tcp_space(sk) &&
310 !tcp_memory_pressure) {
311 int incr;
312
313 /* Check #2. Increase window, if skb with such overhead
314 * will fit to rcvbuf in future.
315 */
316 if (tcp_win_from_space(skb->truesize) <= skb->len)
056834d9 317 incr = 2 * tp->advmss;
1da177e4 318 else
9e412ba7 319 incr = __tcp_grow_window(sk, skb);
1da177e4
LT
320
321 if (incr) {
056834d9
IJ
322 tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr,
323 tp->window_clamp);
463c84b9 324 inet_csk(sk)->icsk_ack.quick |= 1;
1da177e4
LT
325 }
326 }
327}
328
329/* 3. Tuning rcvbuf, when connection enters established state. */
330
331static void tcp_fixup_rcvbuf(struct sock *sk)
332{
333 struct tcp_sock *tp = tcp_sk(sk);
334 int rcvmem = tp->advmss + MAX_TCP_HEADER + 16 + sizeof(struct sk_buff);
335
336 /* Try to select rcvbuf so that 4 mss-sized segments
caa20d9a 337 * will fit to window and corresponding skbs will fit to our rcvbuf.
1da177e4
LT
338 * (was 3; 4 is minimum to allow fast retransmit to work.)
339 */
340 while (tcp_win_from_space(rcvmem) < tp->advmss)
341 rcvmem += 128;
342 if (sk->sk_rcvbuf < 4 * rcvmem)
343 sk->sk_rcvbuf = min(4 * rcvmem, sysctl_tcp_rmem[2]);
344}
345
caa20d9a 346/* 4. Try to fixup all. It is made immediately after connection enters
1da177e4
LT
347 * established state.
348 */
349static void tcp_init_buffer_space(struct sock *sk)
350{
351 struct tcp_sock *tp = tcp_sk(sk);
352 int maxwin;
353
354 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
355 tcp_fixup_rcvbuf(sk);
356 if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
357 tcp_fixup_sndbuf(sk);
358
359 tp->rcvq_space.space = tp->rcv_wnd;
360
361 maxwin = tcp_full_space(sk);
362
363 if (tp->window_clamp >= maxwin) {
364 tp->window_clamp = maxwin;
365
366 if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss)
367 tp->window_clamp = max(maxwin -
368 (maxwin >> sysctl_tcp_app_win),
369 4 * tp->advmss);
370 }
371
372 /* Force reservation of one segment. */
373 if (sysctl_tcp_app_win &&
374 tp->window_clamp > 2 * tp->advmss &&
375 tp->window_clamp + tp->advmss > maxwin)
376 tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss);
377
378 tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
379 tp->snd_cwnd_stamp = tcp_time_stamp;
380}
381
1da177e4 382/* 5. Recalculate window clamp after socket hit its memory bounds. */
9e412ba7 383static void tcp_clamp_window(struct sock *sk)
1da177e4 384{
9e412ba7 385 struct tcp_sock *tp = tcp_sk(sk);
6687e988 386 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 387
6687e988 388 icsk->icsk_ack.quick = 0;
1da177e4 389
326f36e9
JH
390 if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] &&
391 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
392 !tcp_memory_pressure &&
393 atomic_read(&tcp_memory_allocated) < sysctl_tcp_mem[0]) {
394 sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),
395 sysctl_tcp_rmem[2]);
1da177e4 396 }
326f36e9 397 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
056834d9 398 tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss);
1da177e4
LT
399}
400
40efc6fa
SH
401/* Initialize RCV_MSS value.
402 * RCV_MSS is an our guess about MSS used by the peer.
403 * We haven't any direct information about the MSS.
404 * It's better to underestimate the RCV_MSS rather than overestimate.
405 * Overestimations make us ACKing less frequently than needed.
406 * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
407 */
408void tcp_initialize_rcv_mss(struct sock *sk)
409{
410 struct tcp_sock *tp = tcp_sk(sk);
411 unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
412
056834d9 413 hint = min(hint, tp->rcv_wnd / 2);
bee7ca9e 414 hint = min(hint, TCP_MSS_DEFAULT);
40efc6fa
SH
415 hint = max(hint, TCP_MIN_MSS);
416
417 inet_csk(sk)->icsk_ack.rcv_mss = hint;
418}
419
1da177e4
LT
420/* Receiver "autotuning" code.
421 *
422 * The algorithm for RTT estimation w/o timestamps is based on
423 * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL.
424 * <http://www.lanl.gov/radiant/website/pubs/drs/lacsi2001.ps>
425 *
426 * More detail on this code can be found at
427 * <http://www.psc.edu/~jheffner/senior_thesis.ps>,
428 * though this reference is out of date. A new paper
429 * is pending.
430 */
431static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
432{
433 u32 new_sample = tp->rcv_rtt_est.rtt;
434 long m = sample;
435
436 if (m == 0)
437 m = 1;
438
439 if (new_sample != 0) {
440 /* If we sample in larger samples in the non-timestamp
441 * case, we could grossly overestimate the RTT especially
442 * with chatty applications or bulk transfer apps which
443 * are stalled on filesystem I/O.
444 *
445 * Also, since we are only going for a minimum in the
31f34269 446 * non-timestamp case, we do not smooth things out
caa20d9a 447 * else with timestamps disabled convergence takes too
1da177e4
LT
448 * long.
449 */
450 if (!win_dep) {
451 m -= (new_sample >> 3);
452 new_sample += m;
453 } else if (m < new_sample)
454 new_sample = m << 3;
455 } else {
caa20d9a 456 /* No previous measure. */
1da177e4
LT
457 new_sample = m << 3;
458 }
459
460 if (tp->rcv_rtt_est.rtt != new_sample)
461 tp->rcv_rtt_est.rtt = new_sample;
462}
463
464static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
465{
466 if (tp->rcv_rtt_est.time == 0)
467 goto new_measure;
468 if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))
469 return;
056834d9 470 tcp_rcv_rtt_update(tp, jiffies - tp->rcv_rtt_est.time, 1);
1da177e4
LT
471
472new_measure:
473 tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
474 tp->rcv_rtt_est.time = tcp_time_stamp;
475}
476
056834d9
IJ
477static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
478 const struct sk_buff *skb)
1da177e4 479{
463c84b9 480 struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
481 if (tp->rx_opt.rcv_tsecr &&
482 (TCP_SKB_CB(skb)->end_seq -
463c84b9 483 TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss))
1da177e4
LT
484 tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rx_opt.rcv_tsecr, 0);
485}
486
487/*
488 * This function should be called every time data is copied to user space.
489 * It calculates the appropriate TCP receive buffer space.
490 */
491void tcp_rcv_space_adjust(struct sock *sk)
492{
493 struct tcp_sock *tp = tcp_sk(sk);
494 int time;
495 int space;
e905a9ed 496
1da177e4
LT
497 if (tp->rcvq_space.time == 0)
498 goto new_measure;
e905a9ed 499
1da177e4 500 time = tcp_time_stamp - tp->rcvq_space.time;
056834d9 501 if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0)
1da177e4 502 return;
e905a9ed 503
1da177e4
LT
504 space = 2 * (tp->copied_seq - tp->rcvq_space.seq);
505
506 space = max(tp->rcvq_space.space, space);
507
508 if (tp->rcvq_space.space != space) {
509 int rcvmem;
510
511 tp->rcvq_space.space = space;
512
6fcf9412
JH
513 if (sysctl_tcp_moderate_rcvbuf &&
514 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1da177e4
LT
515 int new_clamp = space;
516
517 /* Receive space grows, normalize in order to
518 * take into account packet headers and sk_buff
519 * structure overhead.
520 */
521 space /= tp->advmss;
522 if (!space)
523 space = 1;
524 rcvmem = (tp->advmss + MAX_TCP_HEADER +
525 16 + sizeof(struct sk_buff));
526 while (tcp_win_from_space(rcvmem) < tp->advmss)
527 rcvmem += 128;
528 space *= rcvmem;
529 space = min(space, sysctl_tcp_rmem[2]);
530 if (space > sk->sk_rcvbuf) {
531 sk->sk_rcvbuf = space;
532
533 /* Make the window clamp follow along. */
534 tp->window_clamp = new_clamp;
535 }
536 }
537 }
e905a9ed 538
1da177e4
LT
539new_measure:
540 tp->rcvq_space.seq = tp->copied_seq;
541 tp->rcvq_space.time = tcp_time_stamp;
542}
543
544/* There is something which you must keep in mind when you analyze the
545 * behavior of the tp->ato delayed ack timeout interval. When a
546 * connection starts up, we want to ack as quickly as possible. The
547 * problem is that "good" TCP's do slow start at the beginning of data
548 * transmission. The means that until we send the first few ACK's the
549 * sender will sit on his end and only queue most of his data, because
550 * he can only send snd_cwnd unacked packets at any given time. For
551 * each ACK we send, he increments snd_cwnd and transmits more of his
552 * queue. -DaveM
553 */
9e412ba7 554static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
1da177e4 555{
9e412ba7 556 struct tcp_sock *tp = tcp_sk(sk);
463c84b9 557 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4
LT
558 u32 now;
559
463c84b9 560 inet_csk_schedule_ack(sk);
1da177e4 561
463c84b9 562 tcp_measure_rcv_mss(sk, skb);
1da177e4
LT
563
564 tcp_rcv_rtt_measure(tp);
e905a9ed 565
1da177e4
LT
566 now = tcp_time_stamp;
567
463c84b9 568 if (!icsk->icsk_ack.ato) {
1da177e4
LT
569 /* The _first_ data packet received, initialize
570 * delayed ACK engine.
571 */
463c84b9
ACM
572 tcp_incr_quickack(sk);
573 icsk->icsk_ack.ato = TCP_ATO_MIN;
1da177e4 574 } else {
463c84b9 575 int m = now - icsk->icsk_ack.lrcvtime;
1da177e4 576
056834d9 577 if (m <= TCP_ATO_MIN / 2) {
1da177e4 578 /* The fastest case is the first. */
463c84b9
ACM
579 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
580 } else if (m < icsk->icsk_ack.ato) {
581 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m;
582 if (icsk->icsk_ack.ato > icsk->icsk_rto)
583 icsk->icsk_ack.ato = icsk->icsk_rto;
584 } else if (m > icsk->icsk_rto) {
caa20d9a 585 /* Too long gap. Apparently sender failed to
1da177e4
LT
586 * restart window, so that we send ACKs quickly.
587 */
463c84b9 588 tcp_incr_quickack(sk);
3ab224be 589 sk_mem_reclaim(sk);
1da177e4
LT
590 }
591 }
463c84b9 592 icsk->icsk_ack.lrcvtime = now;
1da177e4
LT
593
594 TCP_ECN_check_ce(tp, skb);
595
596 if (skb->len >= 128)
9e412ba7 597 tcp_grow_window(sk, skb);
1da177e4
LT
598}
599
1da177e4
LT
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 */
2d2abbab 609static void tcp_rtt_estimator(struct sock *sk, const __u32 mrtt)
1da177e4 610{
6687e988 611 struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
612 long m = mrtt; /* RTT */
613
1da177e4
LT
614 /* The following amusing code comes from Jacobson's
615 * article in SIGCOMM '88. Note that rtt and mdev
616 * are scaled versions of rtt and mean deviation.
e905a9ed 617 * This is designed to be as fast as possible
1da177e4
LT
618 * m stands for "measurement".
619 *
620 * On a 1990 paper the rto value is changed to:
621 * RTO = rtt + 4 * mdev
622 *
623 * Funny. This algorithm seems to be very broken.
624 * These formulae increase RTO, when it should be decreased, increase
31f34269 625 * too slowly, when it should be increased quickly, decrease too quickly
1da177e4
LT
626 * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
627 * does not matter how to _calculate_ it. Seems, it was trap
628 * that VJ failed to avoid. 8)
629 */
2de979bd 630 if (m == 0)
1da177e4
LT
631 m = 1;
632 if (tp->srtt != 0) {
633 m -= (tp->srtt >> 3); /* m is now error in rtt est */
634 tp->srtt += m; /* rtt = 7/8 rtt + 1/8 new */
635 if (m < 0) {
636 m = -m; /* m is now abs(error) */
637 m -= (tp->mdev >> 2); /* similar update on mdev */
638 /* This is similar to one of Eifel findings.
639 * Eifel blocks mdev updates when rtt decreases.
640 * This solution is a bit different: we use finer gain
641 * for mdev in this case (alpha*beta).
642 * Like Eifel it also prevents growth of rto,
643 * but also it limits too fast rto decreases,
644 * happening in pure Eifel.
645 */
646 if (m > 0)
647 m >>= 3;
648 } else {
649 m -= (tp->mdev >> 2); /* similar update on mdev */
650 }
651 tp->mdev += m; /* mdev = 3/4 mdev + 1/4 new */
652 if (tp->mdev > tp->mdev_max) {
653 tp->mdev_max = tp->mdev;
654 if (tp->mdev_max > tp->rttvar)
655 tp->rttvar = tp->mdev_max;
656 }
657 if (after(tp->snd_una, tp->rtt_seq)) {
658 if (tp->mdev_max < tp->rttvar)
056834d9 659 tp->rttvar -= (tp->rttvar - tp->mdev_max) >> 2;
1da177e4 660 tp->rtt_seq = tp->snd_nxt;
05bb1fad 661 tp->mdev_max = tcp_rto_min(sk);
1da177e4
LT
662 }
663 } else {
664 /* no previous measure. */
056834d9
IJ
665 tp->srtt = m << 3; /* take the measured time to be rtt */
666 tp->mdev = m << 1; /* make sure rto = 3*rtt */
05bb1fad 667 tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
1da177e4
LT
668 tp->rtt_seq = tp->snd_nxt;
669 }
1da177e4
LT
670}
671
672/* Calculate rto without backoff. This is the second half of Van Jacobson's
673 * routine referred to above.
674 */
463c84b9 675static inline void tcp_set_rto(struct sock *sk)
1da177e4 676{
463c84b9 677 const struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
678 /* Old crap is replaced with new one. 8)
679 *
680 * More seriously:
681 * 1. If rtt variance happened to be less 50msec, it is hallucination.
682 * It cannot be less due to utterly erratic ACK generation made
683 * at least by solaris and freebsd. "Erratic ACKs" has _nothing_
684 * to do with delayed acks, because at cwnd>2 true delack timeout
685 * is invisible. Actually, Linux-2.4 also generates erratic
caa20d9a 686 * ACKs in some circumstances.
1da177e4 687 */
f1ecd5d9 688 inet_csk(sk)->icsk_rto = __tcp_set_rto(tp);
1da177e4
LT
689
690 /* 2. Fixups made earlier cannot be right.
691 * If we do not estimate RTO correctly without them,
692 * all the algo is pure shit and should be replaced
caa20d9a 693 * with correct one. It is exactly, which we pretend to do.
1da177e4 694 */
1da177e4 695
ee6aac59
IJ
696 /* NOTE: clamping at TCP_RTO_MIN is not required, current algo
697 * guarantees that rto is higher.
698 */
f1ecd5d9 699 tcp_bound_rto(sk);
1da177e4
LT
700}
701
702/* Save metrics learned by this TCP session.
703 This function is called only, when TCP finishes successfully
704 i.e. when it enters TIME-WAIT or goes from LAST-ACK to CLOSE.
705 */
706void tcp_update_metrics(struct sock *sk)
707{
708 struct tcp_sock *tp = tcp_sk(sk);
709 struct dst_entry *dst = __sk_dst_get(sk);
710
711 if (sysctl_tcp_nometrics_save)
712 return;
713
714 dst_confirm(dst);
715
056834d9 716 if (dst && (dst->flags & DST_HOST)) {
6687e988 717 const struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 718 int m;
c1e20f7c 719 unsigned long rtt;
1da177e4 720
6687e988 721 if (icsk->icsk_backoff || !tp->srtt) {
1da177e4
LT
722 /* This session failed to estimate rtt. Why?
723 * Probably, no packets returned in time.
724 * Reset our results.
725 */
726 if (!(dst_metric_locked(dst, RTAX_RTT)))
056834d9 727 dst->metrics[RTAX_RTT - 1] = 0;
1da177e4
LT
728 return;
729 }
730
c1e20f7c
SH
731 rtt = dst_metric_rtt(dst, RTAX_RTT);
732 m = rtt - tp->srtt;
1da177e4
LT
733
734 /* If newly calculated rtt larger than stored one,
735 * store new one. Otherwise, use EWMA. Remember,
736 * rtt overestimation is always better than underestimation.
737 */
738 if (!(dst_metric_locked(dst, RTAX_RTT))) {
739 if (m <= 0)
c1e20f7c 740 set_dst_metric_rtt(dst, RTAX_RTT, tp->srtt);
1da177e4 741 else
c1e20f7c 742 set_dst_metric_rtt(dst, RTAX_RTT, rtt - (m >> 3));
1da177e4
LT
743 }
744
745 if (!(dst_metric_locked(dst, RTAX_RTTVAR))) {
c1e20f7c 746 unsigned long var;
1da177e4
LT
747 if (m < 0)
748 m = -m;
749
750 /* Scale deviation to rttvar fixed point */
751 m >>= 1;
752 if (m < tp->mdev)
753 m = tp->mdev;
754
c1e20f7c
SH
755 var = dst_metric_rtt(dst, RTAX_RTTVAR);
756 if (m >= var)
757 var = m;
1da177e4 758 else
c1e20f7c
SH
759 var -= (var - m) >> 2;
760
761 set_dst_metric_rtt(dst, RTAX_RTTVAR, var);
1da177e4
LT
762 }
763
0b6a05c1 764 if (tcp_in_initial_slowstart(tp)) {
1da177e4
LT
765 /* Slow start still did not finish. */
766 if (dst_metric(dst, RTAX_SSTHRESH) &&
767 !dst_metric_locked(dst, RTAX_SSTHRESH) &&
768 (tp->snd_cwnd >> 1) > dst_metric(dst, RTAX_SSTHRESH))
769 dst->metrics[RTAX_SSTHRESH-1] = tp->snd_cwnd >> 1;
770 if (!dst_metric_locked(dst, RTAX_CWND) &&
771 tp->snd_cwnd > dst_metric(dst, RTAX_CWND))
056834d9 772 dst->metrics[RTAX_CWND - 1] = tp->snd_cwnd;
1da177e4 773 } else if (tp->snd_cwnd > tp->snd_ssthresh &&
6687e988 774 icsk->icsk_ca_state == TCP_CA_Open) {
1da177e4
LT
775 /* Cong. avoidance phase, cwnd is reliable. */
776 if (!dst_metric_locked(dst, RTAX_SSTHRESH))
777 dst->metrics[RTAX_SSTHRESH-1] =
778 max(tp->snd_cwnd >> 1, tp->snd_ssthresh);
779 if (!dst_metric_locked(dst, RTAX_CWND))
5ffc02a1 780 dst->metrics[RTAX_CWND-1] = (dst_metric(dst, RTAX_CWND) + tp->snd_cwnd) >> 1;
1da177e4
LT
781 } else {
782 /* Else slow start did not finish, cwnd is non-sense,
783 ssthresh may be also invalid.
784 */
785 if (!dst_metric_locked(dst, RTAX_CWND))
5ffc02a1
SS
786 dst->metrics[RTAX_CWND-1] = (dst_metric(dst, RTAX_CWND) + tp->snd_ssthresh) >> 1;
787 if (dst_metric(dst, RTAX_SSTHRESH) &&
1da177e4 788 !dst_metric_locked(dst, RTAX_SSTHRESH) &&
5ffc02a1 789 tp->snd_ssthresh > dst_metric(dst, RTAX_SSTHRESH))
1da177e4
LT
790 dst->metrics[RTAX_SSTHRESH-1] = tp->snd_ssthresh;
791 }
792
793 if (!dst_metric_locked(dst, RTAX_REORDERING)) {
5ffc02a1 794 if (dst_metric(dst, RTAX_REORDERING) < tp->reordering &&
1da177e4
LT
795 tp->reordering != sysctl_tcp_reordering)
796 dst->metrics[RTAX_REORDERING-1] = tp->reordering;
797 }
798 }
799}
800
410e27a4
GR
801/* Numbers are taken from RFC3390.
802 *
803 * John Heffner states:
804 *
805 * The RFC specifies a window of no more than 4380 bytes
806 * unless 2*MSS > 4380. Reading the pseudocode in the RFC
807 * is a bit misleading because they use a clamp at 4380 bytes
808 * rather than use a multiplier in the relevant range.
809 */
1da177e4
LT
810__u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst)
811{
812 __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
813
410e27a4
GR
814 if (!cwnd) {
815 if (tp->mss_cache > 1460)
816 cwnd = 2;
817 else
818 cwnd = (tp->mss_cache > 1095) ? 3 : 4;
819 }
1da177e4
LT
820 return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
821}
822
40efc6fa 823/* Set slow start threshold and cwnd not falling to slow start */
3cfe3baa 824void tcp_enter_cwr(struct sock *sk, const int set_ssthresh)
40efc6fa
SH
825{
826 struct tcp_sock *tp = tcp_sk(sk);
3cfe3baa 827 const struct inet_connection_sock *icsk = inet_csk(sk);
40efc6fa
SH
828
829 tp->prior_ssthresh = 0;
830 tp->bytes_acked = 0;
e01f9d77 831 if (icsk->icsk_ca_state < TCP_CA_CWR) {
40efc6fa 832 tp->undo_marker = 0;
3cfe3baa
IJ
833 if (set_ssthresh)
834 tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
40efc6fa
SH
835 tp->snd_cwnd = min(tp->snd_cwnd,
836 tcp_packets_in_flight(tp) + 1U);
837 tp->snd_cwnd_cnt = 0;
838 tp->high_seq = tp->snd_nxt;
839 tp->snd_cwnd_stamp = tcp_time_stamp;
840 TCP_ECN_queue_cwr(tp);
841
842 tcp_set_ca_state(sk, TCP_CA_CWR);
843 }
844}
845
e60402d0
IJ
846/*
847 * Packet counting of FACK is based on in-order assumptions, therefore TCP
848 * disables it when reordering is detected
849 */
850static void tcp_disable_fack(struct tcp_sock *tp)
851{
85cc391c
IJ
852 /* RFC3517 uses different metric in lost marker => reset on change */
853 if (tcp_is_fack(tp))
854 tp->lost_skb_hint = NULL;
e60402d0
IJ
855 tp->rx_opt.sack_ok &= ~2;
856}
857
564262c1 858/* Take a notice that peer is sending D-SACKs */
e60402d0
IJ
859static void tcp_dsack_seen(struct tcp_sock *tp)
860{
861 tp->rx_opt.sack_ok |= 4;
862}
863
1da177e4
LT
864/* Initialize metrics on socket. */
865
866static void tcp_init_metrics(struct sock *sk)
867{
868 struct tcp_sock *tp = tcp_sk(sk);
869 struct dst_entry *dst = __sk_dst_get(sk);
870
871 if (dst == NULL)
872 goto reset;
873
874 dst_confirm(dst);
875
876 if (dst_metric_locked(dst, RTAX_CWND))
877 tp->snd_cwnd_clamp = dst_metric(dst, RTAX_CWND);
878 if (dst_metric(dst, RTAX_SSTHRESH)) {
879 tp->snd_ssthresh = dst_metric(dst, RTAX_SSTHRESH);
880 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
881 tp->snd_ssthresh = tp->snd_cwnd_clamp;
882 }
883 if (dst_metric(dst, RTAX_REORDERING) &&
884 tp->reordering != dst_metric(dst, RTAX_REORDERING)) {
e60402d0 885 tcp_disable_fack(tp);
1da177e4
LT
886 tp->reordering = dst_metric(dst, RTAX_REORDERING);
887 }
888
889 if (dst_metric(dst, RTAX_RTT) == 0)
890 goto reset;
891
c1e20f7c 892 if (!tp->srtt && dst_metric_rtt(dst, RTAX_RTT) < (TCP_TIMEOUT_INIT << 3))
1da177e4
LT
893 goto reset;
894
895 /* Initial rtt is determined from SYN,SYN-ACK.
896 * The segment is small and rtt may appear much
897 * less than real one. Use per-dst memory
898 * to make it more realistic.
899 *
900 * A bit of theory. RTT is time passed after "normal" sized packet
caa20d9a 901 * is sent until it is ACKed. In normal circumstances sending small
1da177e4
LT
902 * packets force peer to delay ACKs and calculation is correct too.
903 * The algorithm is adaptive and, provided we follow specs, it
904 * NEVER underestimate RTT. BUT! If peer tries to make some clever
905 * tricks sort of "quick acks" for time long enough to decrease RTT
906 * to low value, and then abruptly stops to do it and starts to delay
907 * ACKs, wait for troubles.
908 */
c1e20f7c
SH
909 if (dst_metric_rtt(dst, RTAX_RTT) > tp->srtt) {
910 tp->srtt = dst_metric_rtt(dst, RTAX_RTT);
1da177e4
LT
911 tp->rtt_seq = tp->snd_nxt;
912 }
c1e20f7c
SH
913 if (dst_metric_rtt(dst, RTAX_RTTVAR) > tp->mdev) {
914 tp->mdev = dst_metric_rtt(dst, RTAX_RTTVAR);
488faa2a 915 tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
1da177e4 916 }
463c84b9 917 tcp_set_rto(sk);
463c84b9 918 if (inet_csk(sk)->icsk_rto < TCP_TIMEOUT_INIT && !tp->rx_opt.saw_tstamp)
1da177e4 919 goto reset;
86bcebaf
IJ
920
921cwnd:
1da177e4
LT
922 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
923 tp->snd_cwnd_stamp = tcp_time_stamp;
924 return;
925
926reset:
927 /* Play conservative. If timestamps are not
928 * supported, TCP will fail to recalculate correct
929 * rtt, if initial rto is too small. FORGET ALL AND RESET!
930 */
931 if (!tp->rx_opt.saw_tstamp && tp->srtt) {
932 tp->srtt = 0;
933 tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_INIT;
463c84b9 934 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_INIT;
1da177e4 935 }
86bcebaf 936 goto cwnd;
1da177e4
LT
937}
938
6687e988
ACM
939static void tcp_update_reordering(struct sock *sk, const int metric,
940 const int ts)
1da177e4 941{
6687e988 942 struct tcp_sock *tp = tcp_sk(sk);
1da177e4 943 if (metric > tp->reordering) {
40b215e5
PE
944 int mib_idx;
945
1da177e4
LT
946 tp->reordering = min(TCP_MAX_REORDERING, metric);
947
948 /* This exciting event is worth to be remembered. 8) */
949 if (ts)
40b215e5 950 mib_idx = LINUX_MIB_TCPTSREORDER;
e60402d0 951 else if (tcp_is_reno(tp))
40b215e5 952 mib_idx = LINUX_MIB_TCPRENOREORDER;
e60402d0 953 else if (tcp_is_fack(tp))
40b215e5 954 mib_idx = LINUX_MIB_TCPFACKREORDER;
1da177e4 955 else
40b215e5
PE
956 mib_idx = LINUX_MIB_TCPSACKREORDER;
957
de0744af 958 NET_INC_STATS_BH(sock_net(sk), mib_idx);
1da177e4
LT
959#if FASTRETRANS_DEBUG > 1
960 printk(KERN_DEBUG "Disorder%d %d %u f%u s%u rr%d\n",
6687e988 961 tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
1da177e4
LT
962 tp->reordering,
963 tp->fackets_out,
964 tp->sacked_out,
965 tp->undo_marker ? tp->undo_retrans : 0);
966#endif
e60402d0 967 tcp_disable_fack(tp);
1da177e4
LT
968 }
969}
970
006f582c 971/* This must be called before lost_out is incremented */
c8c213f2
IJ
972static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
973{
006f582c 974 if ((tp->retransmit_skb_hint == NULL) ||
c8c213f2
IJ
975 before(TCP_SKB_CB(skb)->seq,
976 TCP_SKB_CB(tp->retransmit_skb_hint)->seq))
006f582c
IJ
977 tp->retransmit_skb_hint = skb;
978
979 if (!tp->lost_out ||
980 after(TCP_SKB_CB(skb)->end_seq, tp->retransmit_high))
981 tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
c8c213f2
IJ
982}
983
41ea36e3
IJ
984static void tcp_skb_mark_lost(struct tcp_sock *tp, struct sk_buff *skb)
985{
986 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
987 tcp_verify_retransmit_hint(tp, skb);
988
989 tp->lost_out += tcp_skb_pcount(skb);
990 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
991 }
992}
993
e1aa680f
IJ
994static void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp,
995 struct sk_buff *skb)
006f582c
IJ
996{
997 tcp_verify_retransmit_hint(tp, skb);
998
999 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
1000 tp->lost_out += tcp_skb_pcount(skb);
1001 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
1002 }
1003}
1004
1da177e4
LT
1005/* This procedure tags the retransmission queue when SACKs arrive.
1006 *
1007 * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
1008 * Packets in queue with these bits set are counted in variables
1009 * sacked_out, retrans_out and lost_out, correspondingly.
1010 *
1011 * Valid combinations are:
1012 * Tag InFlight Description
1013 * 0 1 - orig segment is in flight.
1014 * S 0 - nothing flies, orig reached receiver.
1015 * L 0 - nothing flies, orig lost by net.
1016 * R 2 - both orig and retransmit are in flight.
1017 * L|R 1 - orig is lost, retransmit is in flight.
1018 * S|R 1 - orig reached receiver, retrans is still in flight.
1019 * (L|S|R is logically valid, it could occur when L|R is sacked,
1020 * but it is equivalent to plain S and code short-curcuits it to S.
1021 * L|S is logically invalid, it would mean -1 packet in flight 8))
1022 *
1023 * These 6 states form finite state machine, controlled by the following events:
1024 * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
1025 * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
1026 * 3. Loss detection event of one of three flavors:
1027 * A. Scoreboard estimator decided the packet is lost.
1028 * A'. Reno "three dupacks" marks head of queue lost.
1029 * A''. Its FACK modfication, head until snd.fack is lost.
1030 * B. SACK arrives sacking data transmitted after never retransmitted
1031 * hole was sent out.
1032 * C. SACK arrives sacking SND.NXT at the moment, when the
1033 * segment was retransmitted.
1034 * 4. D-SACK added new rule: D-SACK changes any tag to S.
1035 *
1036 * It is pleasant to note, that state diagram turns out to be commutative,
1037 * so that we are allowed not to be bothered by order of our actions,
1038 * when multiple events arrive simultaneously. (see the function below).
1039 *
1040 * Reordering detection.
1041 * --------------------
1042 * Reordering metric is maximal distance, which a packet can be displaced
1043 * in packet stream. With SACKs we can estimate it:
1044 *
1045 * 1. SACK fills old hole and the corresponding segment was not
1046 * ever retransmitted -> reordering. Alas, we cannot use it
1047 * when segment was retransmitted.
1048 * 2. The last flaw is solved with D-SACK. D-SACK arrives
1049 * for retransmitted and already SACKed segment -> reordering..
1050 * Both of these heuristics are not used in Loss state, when we cannot
1051 * account for retransmits accurately.
5b3c9882
IJ
1052 *
1053 * SACK block validation.
1054 * ----------------------
1055 *
1056 * SACK block range validation checks that the received SACK block fits to
1057 * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT.
1058 * Note that SND.UNA is not included to the range though being valid because
0e835331
IJ
1059 * it means that the receiver is rather inconsistent with itself reporting
1060 * SACK reneging when it should advance SND.UNA. Such SACK block this is
1061 * perfectly valid, however, in light of RFC2018 which explicitly states
1062 * that "SACK block MUST reflect the newest segment. Even if the newest
1063 * segment is going to be discarded ...", not that it looks very clever
1064 * in case of head skb. Due to potentional receiver driven attacks, we
1065 * choose to avoid immediate execution of a walk in write queue due to
1066 * reneging and defer head skb's loss recovery to standard loss recovery
1067 * procedure that will eventually trigger (nothing forbids us doing this).
5b3c9882
IJ
1068 *
1069 * Implements also blockage to start_seq wrap-around. Problem lies in the
1070 * fact that though start_seq (s) is before end_seq (i.e., not reversed),
1071 * there's no guarantee that it will be before snd_nxt (n). The problem
1072 * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt
1073 * wrap (s_w):
1074 *
1075 * <- outs wnd -> <- wrapzone ->
1076 * u e n u_w e_w s n_w
1077 * | | | | | | |
1078 * |<------------+------+----- TCP seqno space --------------+---------->|
1079 * ...-- <2^31 ->| |<--------...
1080 * ...---- >2^31 ------>| |<--------...
1081 *
1082 * Current code wouldn't be vulnerable but it's better still to discard such
1083 * crazy SACK blocks. Doing this check for start_seq alone closes somewhat
1084 * similar case (end_seq after snd_nxt wrap) as earlier reversed check in
1085 * snd_nxt wrap -> snd_una region will then become "well defined", i.e.,
1086 * equal to the ideal case (infinite seqno space without wrap caused issues).
1087 *
1088 * With D-SACK the lower bound is extended to cover sequence space below
1089 * SND.UNA down to undo_marker, which is the last point of interest. Yet
564262c1 1090 * again, D-SACK block must not to go across snd_una (for the same reason as
5b3c9882
IJ
1091 * for the normal SACK blocks, explained above). But there all simplicity
1092 * ends, TCP might receive valid D-SACKs below that. As long as they reside
1093 * fully below undo_marker they do not affect behavior in anyway and can
1094 * therefore be safely ignored. In rare cases (which are more or less
1095 * theoretical ones), the D-SACK will nicely cross that boundary due to skb
1096 * fragmentation and packet reordering past skb's retransmission. To consider
1097 * them correctly, the acceptable range must be extended even more though
1098 * the exact amount is rather hard to quantify. However, tp->max_window can
1099 * be used as an exaggerated estimate.
1da177e4 1100 */
5b3c9882
IJ
1101static int tcp_is_sackblock_valid(struct tcp_sock *tp, int is_dsack,
1102 u32 start_seq, u32 end_seq)
1103{
1104 /* Too far in future, or reversed (interpretation is ambiguous) */
1105 if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq))
1106 return 0;
1107
1108 /* Nasty start_seq wrap-around check (see comments above) */
1109 if (!before(start_seq, tp->snd_nxt))
1110 return 0;
1111
564262c1 1112 /* In outstanding window? ...This is valid exit for D-SACKs too.
5b3c9882
IJ
1113 * start_seq == snd_una is non-sensical (see comments above)
1114 */
1115 if (after(start_seq, tp->snd_una))
1116 return 1;
1117
1118 if (!is_dsack || !tp->undo_marker)
1119 return 0;
1120
1121 /* ...Then it's D-SACK, and must reside below snd_una completely */
1122 if (!after(end_seq, tp->snd_una))
1123 return 0;
1124
1125 if (!before(start_seq, tp->undo_marker))
1126 return 1;
1127
1128 /* Too old */
1129 if (!after(end_seq, tp->undo_marker))
1130 return 0;
1131
1132 /* Undo_marker boundary crossing (overestimates a lot). Known already:
1133 * start_seq < undo_marker and end_seq >= undo_marker.
1134 */
1135 return !before(start_seq, end_seq - tp->max_window);
1136}
1137
1c1e87ed
IJ
1138/* Check for lost retransmit. This superb idea is borrowed from "ratehalving".
1139 * Event "C". Later note: FACK people cheated me again 8), we have to account
1140 * for reordering! Ugly, but should help.
f785a8e2
IJ
1141 *
1142 * Search retransmitted skbs from write_queue that were sent when snd_nxt was
1143 * less than what is now known to be received by the other end (derived from
9f58f3b7
IJ
1144 * highest SACK block). Also calculate the lowest snd_nxt among the remaining
1145 * retransmitted skbs to avoid some costly processing per ACKs.
1c1e87ed 1146 */
407ef1de 1147static void tcp_mark_lost_retrans(struct sock *sk)
1c1e87ed 1148{
9f58f3b7 1149 const struct inet_connection_sock *icsk = inet_csk(sk);
1c1e87ed
IJ
1150 struct tcp_sock *tp = tcp_sk(sk);
1151 struct sk_buff *skb;
f785a8e2 1152 int cnt = 0;
df2e014b 1153 u32 new_low_seq = tp->snd_nxt;
6859d494 1154 u32 received_upto = tcp_highest_sack_seq(tp);
9f58f3b7
IJ
1155
1156 if (!tcp_is_fack(tp) || !tp->retrans_out ||
1157 !after(received_upto, tp->lost_retrans_low) ||
1158 icsk->icsk_ca_state != TCP_CA_Recovery)
407ef1de 1159 return;
1c1e87ed
IJ
1160
1161 tcp_for_write_queue(skb, sk) {
1162 u32 ack_seq = TCP_SKB_CB(skb)->ack_seq;
1163
1164 if (skb == tcp_send_head(sk))
1165 break;
f785a8e2 1166 if (cnt == tp->retrans_out)
1c1e87ed
IJ
1167 break;
1168 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1169 continue;
1170
f785a8e2
IJ
1171 if (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS))
1172 continue;
1173
d0af4160
IJ
1174 /* TODO: We would like to get rid of tcp_is_fack(tp) only
1175 * constraint here (see above) but figuring out that at
1176 * least tp->reordering SACK blocks reside between ack_seq
1177 * and received_upto is not easy task to do cheaply with
1178 * the available datastructures.
1179 *
1180 * Whether FACK should check here for tp->reordering segs
1181 * in-between one could argue for either way (it would be
1182 * rather simple to implement as we could count fack_count
1183 * during the walk and do tp->fackets_out - fack_count).
1184 */
1185 if (after(received_upto, ack_seq)) {
1c1e87ed
IJ
1186 TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
1187 tp->retrans_out -= tcp_skb_pcount(skb);
1188
006f582c 1189 tcp_skb_mark_lost_uncond_verify(tp, skb);
de0744af 1190 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSTRETRANSMIT);
f785a8e2 1191 } else {
df2e014b 1192 if (before(ack_seq, new_low_seq))
b08d6cb2 1193 new_low_seq = ack_seq;
f785a8e2 1194 cnt += tcp_skb_pcount(skb);
1c1e87ed
IJ
1195 }
1196 }
b08d6cb2
IJ
1197
1198 if (tp->retrans_out)
1199 tp->lost_retrans_low = new_low_seq;
1c1e87ed 1200}
5b3c9882 1201
1ed83465 1202static int tcp_check_dsack(struct sock *sk, struct sk_buff *ack_skb,
d06e021d
DM
1203 struct tcp_sack_block_wire *sp, int num_sacks,
1204 u32 prior_snd_una)
1205{
1ed83465 1206 struct tcp_sock *tp = tcp_sk(sk);
d3e2ce3b
HH
1207 u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq);
1208 u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq);
d06e021d
DM
1209 int dup_sack = 0;
1210
1211 if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) {
1212 dup_sack = 1;
e60402d0 1213 tcp_dsack_seen(tp);
de0744af 1214 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKRECV);
d06e021d 1215 } else if (num_sacks > 1) {
d3e2ce3b
HH
1216 u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq);
1217 u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq);
d06e021d
DM
1218
1219 if (!after(end_seq_0, end_seq_1) &&
1220 !before(start_seq_0, start_seq_1)) {
1221 dup_sack = 1;
e60402d0 1222 tcp_dsack_seen(tp);
de0744af
PE
1223 NET_INC_STATS_BH(sock_net(sk),
1224 LINUX_MIB_TCPDSACKOFORECV);
d06e021d
DM
1225 }
1226 }
1227
1228 /* D-SACK for already forgotten data... Do dumb counting. */
1229 if (dup_sack &&
1230 !after(end_seq_0, prior_snd_una) &&
1231 after(end_seq_0, tp->undo_marker))
1232 tp->undo_retrans--;
1233
1234 return dup_sack;
1235}
1236
a1197f5a
IJ
1237struct tcp_sacktag_state {
1238 int reord;
1239 int fack_count;
1240 int flag;
1241};
1242
d1935942
IJ
1243/* Check if skb is fully within the SACK block. In presence of GSO skbs,
1244 * the incoming SACK may not exactly match but we can find smaller MSS
1245 * aligned portion of it that matches. Therefore we might need to fragment
1246 * which may fail and creates some hassle (caller must handle error case
1247 * returns).
832d11c5
IJ
1248 *
1249 * FIXME: this could be merged to shift decision code
d1935942 1250 */
0f79efdc
AB
1251static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
1252 u32 start_seq, u32 end_seq)
d1935942
IJ
1253{
1254 int in_sack, err;
1255 unsigned int pkt_len;
adb92db8 1256 unsigned int mss;
d1935942
IJ
1257
1258 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1259 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1260
1261 if (tcp_skb_pcount(skb) > 1 && !in_sack &&
1262 after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
adb92db8 1263 mss = tcp_skb_mss(skb);
d1935942
IJ
1264 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1265
adb92db8 1266 if (!in_sack) {
d1935942 1267 pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
adb92db8
IJ
1268 if (pkt_len < mss)
1269 pkt_len = mss;
1270 } else {
d1935942 1271 pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
adb92db8
IJ
1272 if (pkt_len < mss)
1273 return -EINVAL;
1274 }
1275
1276 /* Round if necessary so that SACKs cover only full MSSes
1277 * and/or the remaining small portion (if present)
1278 */
1279 if (pkt_len > mss) {
1280 unsigned int new_len = (pkt_len / mss) * mss;
1281 if (!in_sack && new_len < pkt_len) {
1282 new_len += mss;
1283 if (new_len > skb->len)
1284 return 0;
1285 }
1286 pkt_len = new_len;
1287 }
1288 err = tcp_fragment(sk, skb, pkt_len, mss);
d1935942
IJ
1289 if (err < 0)
1290 return err;
1291 }
1292
1293 return in_sack;
1294}
1295
a1197f5a
IJ
1296static u8 tcp_sacktag_one(struct sk_buff *skb, struct sock *sk,
1297 struct tcp_sacktag_state *state,
1298 int dup_sack, int pcount)
9e10c47c 1299{
6859d494 1300 struct tcp_sock *tp = tcp_sk(sk);
9e10c47c 1301 u8 sacked = TCP_SKB_CB(skb)->sacked;
a1197f5a 1302 int fack_count = state->fack_count;
9e10c47c
IJ
1303
1304 /* Account D-SACK for retransmitted packet. */
1305 if (dup_sack && (sacked & TCPCB_RETRANS)) {
1306 if (after(TCP_SKB_CB(skb)->end_seq, tp->undo_marker))
1307 tp->undo_retrans--;
ede9f3b1 1308 if (sacked & TCPCB_SACKED_ACKED)
a1197f5a 1309 state->reord = min(fack_count, state->reord);
9e10c47c
IJ
1310 }
1311
1312 /* Nothing to do; acked frame is about to be dropped (was ACKed). */
1313 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
a1197f5a 1314 return sacked;
9e10c47c
IJ
1315
1316 if (!(sacked & TCPCB_SACKED_ACKED)) {
1317 if (sacked & TCPCB_SACKED_RETRANS) {
1318 /* If the segment is not tagged as lost,
1319 * we do not clear RETRANS, believing
1320 * that retransmission is still in flight.
1321 */
1322 if (sacked & TCPCB_LOST) {
a1197f5a 1323 sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
f58b22fd
IJ
1324 tp->lost_out -= pcount;
1325 tp->retrans_out -= pcount;
9e10c47c
IJ
1326 }
1327 } else {
1328 if (!(sacked & TCPCB_RETRANS)) {
1329 /* New sack for not retransmitted frame,
1330 * which was in hole. It is reordering.
1331 */
1332 if (before(TCP_SKB_CB(skb)->seq,
1333 tcp_highest_sack_seq(tp)))
a1197f5a
IJ
1334 state->reord = min(fack_count,
1335 state->reord);
9e10c47c
IJ
1336
1337 /* SACK enhanced F-RTO (RFC4138; Appendix B) */
1338 if (!after(TCP_SKB_CB(skb)->end_seq, tp->frto_highmark))
a1197f5a 1339 state->flag |= FLAG_ONLY_ORIG_SACKED;
9e10c47c
IJ
1340 }
1341
1342 if (sacked & TCPCB_LOST) {
a1197f5a 1343 sacked &= ~TCPCB_LOST;
f58b22fd 1344 tp->lost_out -= pcount;
9e10c47c
IJ
1345 }
1346 }
1347
a1197f5a
IJ
1348 sacked |= TCPCB_SACKED_ACKED;
1349 state->flag |= FLAG_DATA_SACKED;
f58b22fd 1350 tp->sacked_out += pcount;
9e10c47c 1351
f58b22fd 1352 fack_count += pcount;
9e10c47c
IJ
1353
1354 /* Lost marker hint past SACKed? Tweak RFC3517 cnt */
1355 if (!tcp_is_fack(tp) && (tp->lost_skb_hint != NULL) &&
1356 before(TCP_SKB_CB(skb)->seq,
1357 TCP_SKB_CB(tp->lost_skb_hint)->seq))
f58b22fd 1358 tp->lost_cnt_hint += pcount;
9e10c47c
IJ
1359
1360 if (fack_count > tp->fackets_out)
1361 tp->fackets_out = fack_count;
9e10c47c
IJ
1362 }
1363
1364 /* D-SACK. We can detect redundant retransmission in S|R and plain R
1365 * frames and clear it. undo_retrans is decreased above, L|R frames
1366 * are accounted above as well.
1367 */
a1197f5a
IJ
1368 if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
1369 sacked &= ~TCPCB_SACKED_RETRANS;
f58b22fd 1370 tp->retrans_out -= pcount;
9e10c47c
IJ
1371 }
1372
a1197f5a 1373 return sacked;
9e10c47c
IJ
1374}
1375
50133161 1376static int tcp_shifted_skb(struct sock *sk, struct sk_buff *skb,
a1197f5a 1377 struct tcp_sacktag_state *state,
9ec06ff5
IJ
1378 unsigned int pcount, int shifted, int mss,
1379 int dup_sack)
832d11c5
IJ
1380{
1381 struct tcp_sock *tp = tcp_sk(sk);
50133161 1382 struct sk_buff *prev = tcp_write_queue_prev(sk, skb);
832d11c5
IJ
1383
1384 BUG_ON(!pcount);
1385
92ee76b6
IJ
1386 /* Tweak before seqno plays */
1387 if (!tcp_is_fack(tp) && tcp_is_sack(tp) && tp->lost_skb_hint &&
1388 !before(TCP_SKB_CB(tp->lost_skb_hint)->seq, TCP_SKB_CB(skb)->seq))
1389 tp->lost_cnt_hint += pcount;
1390
832d11c5
IJ
1391 TCP_SKB_CB(prev)->end_seq += shifted;
1392 TCP_SKB_CB(skb)->seq += shifted;
1393
1394 skb_shinfo(prev)->gso_segs += pcount;
1395 BUG_ON(skb_shinfo(skb)->gso_segs < pcount);
1396 skb_shinfo(skb)->gso_segs -= pcount;
1397
1398 /* When we're adding to gso_segs == 1, gso_size will be zero,
1399 * in theory this shouldn't be necessary but as long as DSACK
1400 * code can come after this skb later on it's better to keep
1401 * setting gso_size to something.
1402 */
1403 if (!skb_shinfo(prev)->gso_size) {
1404 skb_shinfo(prev)->gso_size = mss;
1405 skb_shinfo(prev)->gso_type = sk->sk_gso_type;
1406 }
1407
1408 /* CHECKME: To clear or not to clear? Mimics normal skb currently */
1409 if (skb_shinfo(skb)->gso_segs <= 1) {
1410 skb_shinfo(skb)->gso_size = 0;
1411 skb_shinfo(skb)->gso_type = 0;
1412 }
1413
a1197f5a 1414 /* We discard results */
9ec06ff5 1415 tcp_sacktag_one(skb, sk, state, dup_sack, pcount);
832d11c5
IJ
1416
1417 /* Difference in this won't matter, both ACKed by the same cumul. ACK */
1418 TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
1419
832d11c5
IJ
1420 if (skb->len > 0) {
1421 BUG_ON(!tcp_skb_pcount(skb));
111cc8b9 1422 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTED);
832d11c5
IJ
1423 return 0;
1424 }
1425
1426 /* Whole SKB was eaten :-) */
1427
92ee76b6
IJ
1428 if (skb == tp->retransmit_skb_hint)
1429 tp->retransmit_skb_hint = prev;
1430 if (skb == tp->scoreboard_skb_hint)
1431 tp->scoreboard_skb_hint = prev;
1432 if (skb == tp->lost_skb_hint) {
1433 tp->lost_skb_hint = prev;
1434 tp->lost_cnt_hint -= tcp_skb_pcount(prev);
1435 }
1436
832d11c5
IJ
1437 TCP_SKB_CB(skb)->flags |= TCP_SKB_CB(prev)->flags;
1438 if (skb == tcp_highest_sack(sk))
1439 tcp_advance_highest_sack(sk, skb);
1440
1441 tcp_unlink_write_queue(skb, sk);
1442 sk_wmem_free_skb(sk, skb);
1443
111cc8b9
IJ
1444 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKMERGED);
1445
832d11c5
IJ
1446 return 1;
1447}
1448
1449/* I wish gso_size would have a bit more sane initialization than
1450 * something-or-zero which complicates things
1451 */
775ffabf 1452static int tcp_skb_seglen(struct sk_buff *skb)
832d11c5 1453{
775ffabf 1454 return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
832d11c5
IJ
1455}
1456
1457/* Shifting pages past head area doesn't work */
1458static int skb_can_shift(struct sk_buff *skb)
1459{
1460 return !skb_headlen(skb) && skb_is_nonlinear(skb);
1461}
1462
1463/* Try collapsing SACK blocks spanning across multiple skbs to a single
1464 * skb.
1465 */
1466static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
a1197f5a 1467 struct tcp_sacktag_state *state,
832d11c5 1468 u32 start_seq, u32 end_seq,
a1197f5a 1469 int dup_sack)
832d11c5
IJ
1470{
1471 struct tcp_sock *tp = tcp_sk(sk);
1472 struct sk_buff *prev;
1473 int mss;
1474 int pcount = 0;
1475 int len;
1476 int in_sack;
1477
1478 if (!sk_can_gso(sk))
1479 goto fallback;
1480
1481 /* Normally R but no L won't result in plain S */
1482 if (!dup_sack &&
9969ca5f 1483 (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
832d11c5
IJ
1484 goto fallback;
1485 if (!skb_can_shift(skb))
1486 goto fallback;
1487 /* This frame is about to be dropped (was ACKed). */
1488 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1489 goto fallback;
1490
1491 /* Can only happen with delayed DSACK + discard craziness */
1492 if (unlikely(skb == tcp_write_queue_head(sk)))
1493 goto fallback;
1494 prev = tcp_write_queue_prev(sk, skb);
1495
1496 if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
1497 goto fallback;
1498
1499 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1500 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1501
1502 if (in_sack) {
1503 len = skb->len;
1504 pcount = tcp_skb_pcount(skb);
775ffabf 1505 mss = tcp_skb_seglen(skb);
832d11c5
IJ
1506
1507 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1508 * drop this restriction as unnecessary
1509 */
775ffabf 1510 if (mss != tcp_skb_seglen(prev))
832d11c5
IJ
1511 goto fallback;
1512 } else {
1513 if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
1514 goto noop;
1515 /* CHECKME: This is non-MSS split case only?, this will
1516 * cause skipped skbs due to advancing loop btw, original
1517 * has that feature too
1518 */
1519 if (tcp_skb_pcount(skb) <= 1)
1520 goto noop;
1521
1522 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1523 if (!in_sack) {
1524 /* TODO: head merge to next could be attempted here
1525 * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
1526 * though it might not be worth of the additional hassle
1527 *
1528 * ...we can probably just fallback to what was done
1529 * previously. We could try merging non-SACKed ones
1530 * as well but it probably isn't going to buy off
1531 * because later SACKs might again split them, and
1532 * it would make skb timestamp tracking considerably
1533 * harder problem.
1534 */
1535 goto fallback;
1536 }
1537
1538 len = end_seq - TCP_SKB_CB(skb)->seq;
1539 BUG_ON(len < 0);
1540 BUG_ON(len > skb->len);
1541
1542 /* MSS boundaries should be honoured or else pcount will
1543 * severely break even though it makes things bit trickier.
1544 * Optimize common case to avoid most of the divides
1545 */
1546 mss = tcp_skb_mss(skb);
1547
1548 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1549 * drop this restriction as unnecessary
1550 */
775ffabf 1551 if (mss != tcp_skb_seglen(prev))
832d11c5
IJ
1552 goto fallback;
1553
1554 if (len == mss) {
1555 pcount = 1;
1556 } else if (len < mss) {
1557 goto noop;
1558 } else {
1559 pcount = len / mss;
1560 len = pcount * mss;
1561 }
1562 }
1563
1564 if (!skb_shift(prev, skb, len))
1565 goto fallback;
9ec06ff5 1566 if (!tcp_shifted_skb(sk, skb, state, pcount, len, mss, dup_sack))
832d11c5
IJ
1567 goto out;
1568
1569 /* Hole filled allows collapsing with the next as well, this is very
1570 * useful when hole on every nth skb pattern happens
1571 */
1572 if (prev == tcp_write_queue_tail(sk))
1573 goto out;
1574 skb = tcp_write_queue_next(sk, prev);
1575
f0bc52f3
IJ
1576 if (!skb_can_shift(skb) ||
1577 (skb == tcp_send_head(sk)) ||
1578 ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
775ffabf 1579 (mss != tcp_skb_seglen(skb)))
832d11c5
IJ
1580 goto out;
1581
1582 len = skb->len;
1583 if (skb_shift(prev, skb, len)) {
1584 pcount += tcp_skb_pcount(skb);
9ec06ff5 1585 tcp_shifted_skb(sk, skb, state, tcp_skb_pcount(skb), len, mss, 0);
832d11c5
IJ
1586 }
1587
1588out:
a1197f5a 1589 state->fack_count += pcount;
832d11c5
IJ
1590 return prev;
1591
1592noop:
1593 return skb;
1594
1595fallback:
111cc8b9 1596 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
832d11c5
IJ
1597 return NULL;
1598}
1599
68f8353b
IJ
1600static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
1601 struct tcp_sack_block *next_dup,
a1197f5a 1602 struct tcp_sacktag_state *state,
68f8353b 1603 u32 start_seq, u32 end_seq,
a1197f5a 1604 int dup_sack_in)
68f8353b 1605{
832d11c5
IJ
1606 struct tcp_sock *tp = tcp_sk(sk);
1607 struct sk_buff *tmp;
1608
68f8353b
IJ
1609 tcp_for_write_queue_from(skb, sk) {
1610 int in_sack = 0;
1611 int dup_sack = dup_sack_in;
1612
1613 if (skb == tcp_send_head(sk))
1614 break;
1615
1616 /* queue is in-order => we can short-circuit the walk early */
1617 if (!before(TCP_SKB_CB(skb)->seq, end_seq))
1618 break;
1619
1620 if ((next_dup != NULL) &&
1621 before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
1622 in_sack = tcp_match_skb_to_sack(sk, skb,
1623 next_dup->start_seq,
1624 next_dup->end_seq);
1625 if (in_sack > 0)
1626 dup_sack = 1;
1627 }
1628
832d11c5
IJ
1629 /* skb reference here is a bit tricky to get right, since
1630 * shifting can eat and free both this skb and the next,
1631 * so not even _safe variant of the loop is enough.
1632 */
1633 if (in_sack <= 0) {
a1197f5a
IJ
1634 tmp = tcp_shift_skb_data(sk, skb, state,
1635 start_seq, end_seq, dup_sack);
832d11c5
IJ
1636 if (tmp != NULL) {
1637 if (tmp != skb) {
1638 skb = tmp;
1639 continue;
1640 }
1641
1642 in_sack = 0;
1643 } else {
1644 in_sack = tcp_match_skb_to_sack(sk, skb,
1645 start_seq,
1646 end_seq);
1647 }
1648 }
1649
68f8353b
IJ
1650 if (unlikely(in_sack < 0))
1651 break;
1652
832d11c5 1653 if (in_sack) {
a1197f5a
IJ
1654 TCP_SKB_CB(skb)->sacked = tcp_sacktag_one(skb, sk,
1655 state,
1656 dup_sack,
1657 tcp_skb_pcount(skb));
68f8353b 1658
832d11c5
IJ
1659 if (!before(TCP_SKB_CB(skb)->seq,
1660 tcp_highest_sack_seq(tp)))
1661 tcp_advance_highest_sack(sk, skb);
1662 }
1663
a1197f5a 1664 state->fack_count += tcp_skb_pcount(skb);
68f8353b
IJ
1665 }
1666 return skb;
1667}
1668
1669/* Avoid all extra work that is being done by sacktag while walking in
1670 * a normal way
1671 */
1672static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk,
a1197f5a
IJ
1673 struct tcp_sacktag_state *state,
1674 u32 skip_to_seq)
68f8353b
IJ
1675{
1676 tcp_for_write_queue_from(skb, sk) {
1677 if (skb == tcp_send_head(sk))
1678 break;
1679
e8bae275 1680 if (after(TCP_SKB_CB(skb)->end_seq, skip_to_seq))
68f8353b 1681 break;
d152a7d8 1682
a1197f5a 1683 state->fack_count += tcp_skb_pcount(skb);
68f8353b
IJ
1684 }
1685 return skb;
1686}
1687
1688static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
1689 struct sock *sk,
1690 struct tcp_sack_block *next_dup,
a1197f5a
IJ
1691 struct tcp_sacktag_state *state,
1692 u32 skip_to_seq)
68f8353b
IJ
1693{
1694 if (next_dup == NULL)
1695 return skb;
1696
1697 if (before(next_dup->start_seq, skip_to_seq)) {
a1197f5a
IJ
1698 skb = tcp_sacktag_skip(skb, sk, state, next_dup->start_seq);
1699 skb = tcp_sacktag_walk(skb, sk, NULL, state,
1700 next_dup->start_seq, next_dup->end_seq,
1701 1);
68f8353b
IJ
1702 }
1703
1704 return skb;
1705}
1706
1707static int tcp_sack_cache_ok(struct tcp_sock *tp, struct tcp_sack_block *cache)
1708{
1709 return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
1710}
1711
1da177e4 1712static int
056834d9
IJ
1713tcp_sacktag_write_queue(struct sock *sk, struct sk_buff *ack_skb,
1714 u32 prior_snd_una)
1da177e4 1715{
6687e988 1716 const struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 1717 struct tcp_sock *tp = tcp_sk(sk);
9c70220b
ACM
1718 unsigned char *ptr = (skb_transport_header(ack_skb) +
1719 TCP_SKB_CB(ack_skb)->sacked);
fd6dad61 1720 struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
4389dded 1721 struct tcp_sack_block sp[TCP_NUM_SACKS];
68f8353b 1722 struct tcp_sack_block *cache;
a1197f5a 1723 struct tcp_sacktag_state state;
68f8353b 1724 struct sk_buff *skb;
4389dded 1725 int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
fd6dad61 1726 int used_sacks;
7769f406 1727 int found_dup_sack = 0;
68f8353b 1728 int i, j;
fda03fbb 1729 int first_sack_index;
1da177e4 1730
a1197f5a
IJ
1731 state.flag = 0;
1732 state.reord = tp->packets_out;
1733
d738cd8f 1734 if (!tp->sacked_out) {
de83c058
IJ
1735 if (WARN_ON(tp->fackets_out))
1736 tp->fackets_out = 0;
6859d494 1737 tcp_highest_sack_reset(sk);
d738cd8f 1738 }
1da177e4 1739
1ed83465 1740 found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
d06e021d
DM
1741 num_sacks, prior_snd_una);
1742 if (found_dup_sack)
a1197f5a 1743 state.flag |= FLAG_DSACKING_ACK;
6f74651a
BE
1744
1745 /* Eliminate too old ACKs, but take into
1746 * account more or less fresh ones, they can
1747 * contain valid SACK info.
1748 */
1749 if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
1750 return 0;
1751