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