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