]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/ipv4/tcp_input.c
[TCP]: Convert highest_sack to sk_buff to allow direct access
[mirror_ubuntu-zesty-kernel.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 *
8 * Version: $Id: tcp_input.c,v 1.243 2002/02/01 22:01:04 davem Exp $
9 *
02c30a84 10 * Authors: Ross Biro
1da177e4
LT
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
13 * Corey Minyard <wf-rch!minyard@relay.EU.net>
14 * Florian La Roche, <flla@stud.uni-sb.de>
15 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
16 * Linus Torvalds, <torvalds@cs.helsinki.fi>
17 * Alan Cox, <gw4pts@gw4pts.ampr.org>
18 * Matthew Dillon, <dillon@apollo.west.oic.com>
19 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
20 * Jorge Cwik, <jorge@laser.satlink.net>
21 */
22
23/*
24 * Changes:
25 * Pedro Roque : Fast Retransmit/Recovery.
26 * Two receive queues.
27 * Retransmit queue handled by TCP.
28 * Better retransmit timer handling.
29 * New congestion avoidance.
30 * Header prediction.
31 * Variable renaming.
32 *
33 * Eric : Fast Retransmit.
34 * Randy Scott : MSS option defines.
35 * Eric Schenk : Fixes to slow start algorithm.
36 * Eric Schenk : Yet another double ACK bug.
37 * Eric Schenk : Delayed ACK bug fixes.
38 * Eric Schenk : Floyd style fast retrans war avoidance.
39 * David S. Miller : Don't allow zero congestion window.
40 * Eric Schenk : Fix retransmitter so that it sends
41 * next packet on ack of previous packet.
42 * Andi Kleen : Moved open_request checking here
43 * and process RSTs for open_requests.
44 * Andi Kleen : Better prune_queue, and other fixes.
caa20d9a 45 * Andrey Savochkin: Fix RTT measurements in the presence of
1da177e4
LT
46 * timestamps.
47 * Andrey Savochkin: Check sequence numbers correctly when
48 * removing SACKs due to in sequence incoming
49 * data segments.
50 * Andi Kleen: Make sure we never ack data there is not
51 * enough room for. Also make this condition
52 * a fatal error if it might still happen.
e905a9ed 53 * Andi Kleen: Add tcp_measure_rcv_mss to make
1da177e4 54 * connections with MSS<min(MTU,ann. MSS)
e905a9ed 55 * work without delayed acks.
1da177e4
LT
56 * Andi Kleen: Process packets with PSH set in the
57 * fast path.
58 * J Hadi Salim: ECN support
59 * Andrei Gurtov,
60 * Pasi Sarolahti,
61 * Panu Kuhlberg: Experimental audit of TCP (re)transmission
62 * engine. Lots of bugs are found.
63 * Pasi Sarolahti: F-RTO for dealing with spurious RTOs
1da177e4
LT
64 */
65
1da177e4
LT
66#include <linux/mm.h>
67#include <linux/module.h>
68#include <linux/sysctl.h>
69#include <net/tcp.h>
70#include <net/inet_common.h>
71#include <linux/ipsec.h>
72#include <asm/unaligned.h>
1a2449a8 73#include <net/netdma.h>
1da177e4 74
ab32ea5d
BH
75int sysctl_tcp_timestamps __read_mostly = 1;
76int sysctl_tcp_window_scaling __read_mostly = 1;
77int sysctl_tcp_sack __read_mostly = 1;
78int sysctl_tcp_fack __read_mostly = 1;
79int sysctl_tcp_reordering __read_mostly = TCP_FASTRETRANS_THRESH;
80int sysctl_tcp_ecn __read_mostly;
81int sysctl_tcp_dsack __read_mostly = 1;
82int sysctl_tcp_app_win __read_mostly = 31;
83int sysctl_tcp_adv_win_scale __read_mostly = 2;
1da177e4 84
ab32ea5d
BH
85int sysctl_tcp_stdurg __read_mostly;
86int sysctl_tcp_rfc1337 __read_mostly;
87int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
c96fd3d4 88int sysctl_tcp_frto __read_mostly = 2;
3cfe3baa 89int sysctl_tcp_frto_response __read_mostly;
ab32ea5d 90int sysctl_tcp_nometrics_save __read_mostly;
1da177e4 91
ab32ea5d
BH
92int sysctl_tcp_moderate_rcvbuf __read_mostly = 1;
93int sysctl_tcp_abc __read_mostly;
1da177e4 94
1da177e4
LT
95#define FLAG_DATA 0x01 /* Incoming frame contained data. */
96#define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */
97#define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */
98#define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted. */
99#define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */
100#define FLAG_DATA_SACKED 0x20 /* New SACK. */
101#define FLAG_ECE 0x40 /* ECE in this ACK */
102#define FLAG_DATA_LOST 0x80 /* SACK detected data lossage. */
103#define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/
4dc2665e 104#define FLAG_ONLY_ORIG_SACKED 0x200 /* SACKs only non-rexmit sent before RTO */
2e605294 105#define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */
564262c1 106#define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */
009a2e3e 107#define FLAG_NONHEAD_RETRANS_ACKED 0x1000 /* Non-head rexmitted data was ACKed */
1da177e4
LT
108
109#define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED)
110#define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
111#define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE)
112#define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED)
2e605294 113#define FLAG_ANY_PROGRESS (FLAG_FORWARD_PROGRESS|FLAG_SND_UNA_ADVANCED)
1da177e4 114
4dc2665e
IJ
115#define IsSackFrto() (sysctl_tcp_frto == 0x2)
116
1da177e4 117#define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
bdf1ee5d 118#define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
1da177e4 119
e905a9ed 120/* Adapt the MSS value used to make delayed ack decision to the
1da177e4 121 * real world.
e905a9ed 122 */
40efc6fa
SH
123static void tcp_measure_rcv_mss(struct sock *sk,
124 const struct sk_buff *skb)
1da177e4 125{
463c84b9 126 struct inet_connection_sock *icsk = inet_csk(sk);
e905a9ed 127 const unsigned int lss = icsk->icsk_ack.last_seg_size;
463c84b9 128 unsigned int len;
1da177e4 129
e905a9ed 130 icsk->icsk_ack.last_seg_size = 0;
1da177e4
LT
131
132 /* skb->len may jitter because of SACKs, even if peer
133 * sends good full-sized frames.
134 */
ff9b5e0f 135 len = skb_shinfo(skb)->gso_size ?: skb->len;
463c84b9
ACM
136 if (len >= icsk->icsk_ack.rcv_mss) {
137 icsk->icsk_ack.rcv_mss = len;
1da177e4
LT
138 } else {
139 /* Otherwise, we make more careful check taking into account,
140 * that SACKs block is variable.
141 *
142 * "len" is invariant segment length, including TCP header.
143 */
9c70220b 144 len += skb->data - skb_transport_header(skb);
1da177e4
LT
145 if (len >= TCP_MIN_RCVMSS + sizeof(struct tcphdr) ||
146 /* If PSH is not set, packet should be
147 * full sized, provided peer TCP is not badly broken.
148 * This observation (if it is correct 8)) allows
149 * to handle super-low mtu links fairly.
150 */
151 (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
aa8223c7 152 !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {
1da177e4
LT
153 /* Subtract also invariant (if peer is RFC compliant),
154 * tcp header plus fixed timestamp option length.
155 * Resulting "len" is MSS free of SACK jitter.
156 */
463c84b9
ACM
157 len -= tcp_sk(sk)->tcp_header_len;
158 icsk->icsk_ack.last_seg_size = len;
1da177e4 159 if (len == lss) {
463c84b9 160 icsk->icsk_ack.rcv_mss = len;
1da177e4
LT
161 return;
162 }
163 }
1ef9696c
AK
164 if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)
165 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;
463c84b9 166 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
1da177e4
LT
167 }
168}
169
463c84b9 170static void tcp_incr_quickack(struct sock *sk)
1da177e4 171{
463c84b9
ACM
172 struct inet_connection_sock *icsk = inet_csk(sk);
173 unsigned quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);
1da177e4
LT
174
175 if (quickacks==0)
176 quickacks=2;
463c84b9
ACM
177 if (quickacks > icsk->icsk_ack.quick)
178 icsk->icsk_ack.quick = min(quickacks, TCP_MAX_QUICKACKS);
1da177e4
LT
179}
180
463c84b9 181void tcp_enter_quickack_mode(struct sock *sk)
1da177e4 182{
463c84b9
ACM
183 struct inet_connection_sock *icsk = inet_csk(sk);
184 tcp_incr_quickack(sk);
185 icsk->icsk_ack.pingpong = 0;
186 icsk->icsk_ack.ato = TCP_ATO_MIN;
1da177e4
LT
187}
188
189/* Send ACKs quickly, if "quick" count is not exhausted
190 * and the session is not interactive.
191 */
192
463c84b9 193static inline int tcp_in_quickack_mode(const struct sock *sk)
1da177e4 194{
463c84b9
ACM
195 const struct inet_connection_sock *icsk = inet_csk(sk);
196 return icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong;
1da177e4
LT
197}
198
bdf1ee5d
IJ
199static inline void TCP_ECN_queue_cwr(struct tcp_sock *tp)
200{
201 if (tp->ecn_flags&TCP_ECN_OK)
202 tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
203}
204
205static inline void TCP_ECN_accept_cwr(struct tcp_sock *tp, struct sk_buff *skb)
206{
207 if (tcp_hdr(skb)->cwr)
208 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
209}
210
211static inline void TCP_ECN_withdraw_cwr(struct tcp_sock *tp)
212{
213 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
214}
215
216static inline void TCP_ECN_check_ce(struct tcp_sock *tp, struct sk_buff *skb)
217{
218 if (tp->ecn_flags&TCP_ECN_OK) {
219 if (INET_ECN_is_ce(TCP_SKB_CB(skb)->flags))
220 tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
221 /* Funny extension: if ECT is not set on a segment,
222 * it is surely retransmit. It is not in ECN RFC,
223 * but Linux follows this rule. */
224 else if (INET_ECN_is_not_ect((TCP_SKB_CB(skb)->flags)))
225 tcp_enter_quickack_mode((struct sock *)tp);
226 }
227}
228
229static inline void TCP_ECN_rcv_synack(struct tcp_sock *tp, struct tcphdr *th)
230{
231 if ((tp->ecn_flags&TCP_ECN_OK) && (!th->ece || th->cwr))
232 tp->ecn_flags &= ~TCP_ECN_OK;
233}
234
235static inline void TCP_ECN_rcv_syn(struct tcp_sock *tp, struct tcphdr *th)
236{
237 if ((tp->ecn_flags&TCP_ECN_OK) && (!th->ece || !th->cwr))
238 tp->ecn_flags &= ~TCP_ECN_OK;
239}
240
241static inline int TCP_ECN_rcv_ecn_echo(struct tcp_sock *tp, struct tcphdr *th)
242{
243 if (th->ece && !th->syn && (tp->ecn_flags&TCP_ECN_OK))
244 return 1;
245 return 0;
246}
247
1da177e4
LT
248/* Buffer size and advertised window tuning.
249 *
250 * 1. Tuning sk->sk_sndbuf, when connection enters established state.
251 */
252
253static void tcp_fixup_sndbuf(struct sock *sk)
254{
255 int sndmem = tcp_sk(sk)->rx_opt.mss_clamp + MAX_TCP_HEADER + 16 +
256 sizeof(struct sk_buff);
257
258 if (sk->sk_sndbuf < 3 * sndmem)
259 sk->sk_sndbuf = min(3 * sndmem, sysctl_tcp_wmem[2]);
260}
261
262/* 2. Tuning advertised window (window_clamp, rcv_ssthresh)
263 *
264 * All tcp_full_space() is split to two parts: "network" buffer, allocated
265 * forward and advertised in receiver window (tp->rcv_wnd) and
266 * "application buffer", required to isolate scheduling/application
267 * latencies from network.
268 * window_clamp is maximal advertised window. It can be less than
269 * tcp_full_space(), in this case tcp_full_space() - window_clamp
270 * is reserved for "application" buffer. The less window_clamp is
271 * the smoother our behaviour from viewpoint of network, but the lower
272 * throughput and the higher sensitivity of the connection to losses. 8)
273 *
274 * rcv_ssthresh is more strict window_clamp used at "slow start"
275 * phase to predict further behaviour of this connection.
276 * It is used for two goals:
277 * - to enforce header prediction at sender, even when application
278 * requires some significant "application buffer". It is check #1.
279 * - to prevent pruning of receive queue because of misprediction
280 * of receiver window. Check #2.
281 *
282 * The scheme does not work when sender sends good segments opening
caa20d9a 283 * window and then starts to feed us spaghetti. But it should work
1da177e4
LT
284 * in common situations. Otherwise, we have to rely on queue collapsing.
285 */
286
287/* Slow part of check#2. */
9e412ba7 288static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb)
1da177e4 289{
9e412ba7 290 struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
291 /* Optimize this! */
292 int truesize = tcp_win_from_space(skb->truesize)/2;
326f36e9 293 int window = tcp_win_from_space(sysctl_tcp_rmem[2])/2;
1da177e4
LT
294
295 while (tp->rcv_ssthresh <= window) {
296 if (truesize <= skb->len)
463c84b9 297 return 2 * inet_csk(sk)->icsk_ack.rcv_mss;
1da177e4
LT
298
299 truesize >>= 1;
300 window >>= 1;
301 }
302 return 0;
303}
304
9e412ba7 305static void tcp_grow_window(struct sock *sk,
40efc6fa 306 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)
320 incr = 2*tp->advmss;
321 else
9e412ba7 322 incr = __tcp_grow_window(sk, skb);
1da177e4
LT
323
324 if (incr) {
325 tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr, tp->window_clamp);
463c84b9 326 inet_csk(sk)->icsk_ack.quick |= 1;
1da177e4
LT
327 }
328 }
329}
330
331/* 3. Tuning rcvbuf, when connection enters established state. */
332
333static void tcp_fixup_rcvbuf(struct sock *sk)
334{
335 struct tcp_sock *tp = tcp_sk(sk);
336 int rcvmem = tp->advmss + MAX_TCP_HEADER + 16 + sizeof(struct sk_buff);
337
338 /* Try to select rcvbuf so that 4 mss-sized segments
caa20d9a 339 * will fit to window and corresponding skbs will fit to our rcvbuf.
1da177e4
LT
340 * (was 3; 4 is minimum to allow fast retransmit to work.)
341 */
342 while (tcp_win_from_space(rcvmem) < tp->advmss)
343 rcvmem += 128;
344 if (sk->sk_rcvbuf < 4 * rcvmem)
345 sk->sk_rcvbuf = min(4 * rcvmem, sysctl_tcp_rmem[2]);
346}
347
caa20d9a 348/* 4. Try to fixup all. It is made immediately after connection enters
1da177e4
LT
349 * established state.
350 */
351static void tcp_init_buffer_space(struct sock *sk)
352{
353 struct tcp_sock *tp = tcp_sk(sk);
354 int maxwin;
355
356 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
357 tcp_fixup_rcvbuf(sk);
358 if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
359 tcp_fixup_sndbuf(sk);
360
361 tp->rcvq_space.space = tp->rcv_wnd;
362
363 maxwin = tcp_full_space(sk);
364
365 if (tp->window_clamp >= maxwin) {
366 tp->window_clamp = maxwin;
367
368 if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss)
369 tp->window_clamp = max(maxwin -
370 (maxwin >> sysctl_tcp_app_win),
371 4 * tp->advmss);
372 }
373
374 /* Force reservation of one segment. */
375 if (sysctl_tcp_app_win &&
376 tp->window_clamp > 2 * tp->advmss &&
377 tp->window_clamp + tp->advmss > maxwin)
378 tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss);
379
380 tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
381 tp->snd_cwnd_stamp = tcp_time_stamp;
382}
383
1da177e4 384/* 5. Recalculate window clamp after socket hit its memory bounds. */
9e412ba7 385static void tcp_clamp_window(struct sock *sk)
1da177e4 386{
9e412ba7 387 struct tcp_sock *tp = tcp_sk(sk);
6687e988 388 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 389
6687e988 390 icsk->icsk_ack.quick = 0;
1da177e4 391
326f36e9
JH
392 if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] &&
393 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
394 !tcp_memory_pressure &&
395 atomic_read(&tcp_memory_allocated) < sysctl_tcp_mem[0]) {
396 sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),
397 sysctl_tcp_rmem[2]);
1da177e4 398 }
326f36e9 399 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
1da177e4 400 tp->rcv_ssthresh = min(tp->window_clamp, 2U*tp->advmss);
1da177e4
LT
401}
402
40efc6fa
SH
403
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
416 hint = min(hint, tp->rcv_wnd/2);
417 hint = min(hint, TCP_MIN_RCVMSS);
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;
473 tcp_rcv_rtt_update(tp,
474 jiffies - tp->rcv_rtt_est.time,
475 1);
476
477new_measure:
478 tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
479 tp->rcv_rtt_est.time = tcp_time_stamp;
480}
481
463c84b9 482static inline void tcp_rcv_rtt_measure_ts(struct sock *sk, const struct sk_buff *skb)
1da177e4 483{
463c84b9 484 struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
485 if (tp->rx_opt.rcv_tsecr &&
486 (TCP_SKB_CB(skb)->end_seq -
463c84b9 487 TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss))
1da177e4
LT
488 tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rx_opt.rcv_tsecr, 0);
489}
490
491/*
492 * This function should be called every time data is copied to user space.
493 * It calculates the appropriate TCP receive buffer space.
494 */
495void tcp_rcv_space_adjust(struct sock *sk)
496{
497 struct tcp_sock *tp = tcp_sk(sk);
498 int time;
499 int space;
e905a9ed 500
1da177e4
LT
501 if (tp->rcvq_space.time == 0)
502 goto new_measure;
e905a9ed 503
1da177e4
LT
504 time = tcp_time_stamp - tp->rcvq_space.time;
505 if (time < (tp->rcv_rtt_est.rtt >> 3) ||
506 tp->rcv_rtt_est.rtt == 0)
507 return;
e905a9ed 508
1da177e4
LT
509 space = 2 * (tp->copied_seq - tp->rcvq_space.seq);
510
511 space = max(tp->rcvq_space.space, space);
512
513 if (tp->rcvq_space.space != space) {
514 int rcvmem;
515
516 tp->rcvq_space.space = space;
517
6fcf9412
JH
518 if (sysctl_tcp_moderate_rcvbuf &&
519 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1da177e4
LT
520 int new_clamp = space;
521
522 /* Receive space grows, normalize in order to
523 * take into account packet headers and sk_buff
524 * structure overhead.
525 */
526 space /= tp->advmss;
527 if (!space)
528 space = 1;
529 rcvmem = (tp->advmss + MAX_TCP_HEADER +
530 16 + sizeof(struct sk_buff));
531 while (tcp_win_from_space(rcvmem) < tp->advmss)
532 rcvmem += 128;
533 space *= rcvmem;
534 space = min(space, sysctl_tcp_rmem[2]);
535 if (space > sk->sk_rcvbuf) {
536 sk->sk_rcvbuf = space;
537
538 /* Make the window clamp follow along. */
539 tp->window_clamp = new_clamp;
540 }
541 }
542 }
e905a9ed 543
1da177e4
LT
544new_measure:
545 tp->rcvq_space.seq = tp->copied_seq;
546 tp->rcvq_space.time = tcp_time_stamp;
547}
548
549/* There is something which you must keep in mind when you analyze the
550 * behavior of the tp->ato delayed ack timeout interval. When a
551 * connection starts up, we want to ack as quickly as possible. The
552 * problem is that "good" TCP's do slow start at the beginning of data
553 * transmission. The means that until we send the first few ACK's the
554 * sender will sit on his end and only queue most of his data, because
555 * he can only send snd_cwnd unacked packets at any given time. For
556 * each ACK we send, he increments snd_cwnd and transmits more of his
557 * queue. -DaveM
558 */
9e412ba7 559static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
1da177e4 560{
9e412ba7 561 struct tcp_sock *tp = tcp_sk(sk);
463c84b9 562 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4
LT
563 u32 now;
564
463c84b9 565 inet_csk_schedule_ack(sk);
1da177e4 566
463c84b9 567 tcp_measure_rcv_mss(sk, skb);
1da177e4
LT
568
569 tcp_rcv_rtt_measure(tp);
e905a9ed 570
1da177e4
LT
571 now = tcp_time_stamp;
572
463c84b9 573 if (!icsk->icsk_ack.ato) {
1da177e4
LT
574 /* The _first_ data packet received, initialize
575 * delayed ACK engine.
576 */
463c84b9
ACM
577 tcp_incr_quickack(sk);
578 icsk->icsk_ack.ato = TCP_ATO_MIN;
1da177e4 579 } else {
463c84b9 580 int m = now - icsk->icsk_ack.lrcvtime;
1da177e4
LT
581
582 if (m <= TCP_ATO_MIN/2) {
583 /* The fastest case is the first. */
463c84b9
ACM
584 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
585 } else if (m < icsk->icsk_ack.ato) {
586 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m;
587 if (icsk->icsk_ack.ato > icsk->icsk_rto)
588 icsk->icsk_ack.ato = icsk->icsk_rto;
589 } else if (m > icsk->icsk_rto) {
caa20d9a 590 /* Too long gap. Apparently sender failed to
1da177e4
LT
591 * restart window, so that we send ACKs quickly.
592 */
463c84b9 593 tcp_incr_quickack(sk);
1da177e4
LT
594 sk_stream_mem_reclaim(sk);
595 }
596 }
463c84b9 597 icsk->icsk_ack.lrcvtime = now;
1da177e4
LT
598
599 TCP_ECN_check_ce(tp, skb);
600
601 if (skb->len >= 128)
9e412ba7 602 tcp_grow_window(sk, skb);
1da177e4
LT
603}
604
05bb1fad
DM
605static u32 tcp_rto_min(struct sock *sk)
606{
607 struct dst_entry *dst = __sk_dst_get(sk);
608 u32 rto_min = TCP_RTO_MIN;
609
5c127c58 610 if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
05bb1fad
DM
611 rto_min = dst->metrics[RTAX_RTO_MIN-1];
612 return rto_min;
613}
614
1da177e4
LT
615/* Called to compute a smoothed rtt estimate. The data fed to this
616 * routine either comes from timestamps, or from segments that were
617 * known _not_ to have been retransmitted [see Karn/Partridge
618 * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88
619 * piece by Van Jacobson.
620 * NOTE: the next three routines used to be one big routine.
621 * To save cycles in the RFC 1323 implementation it was better to break
622 * it up into three procedures. -- erics
623 */
2d2abbab 624static void tcp_rtt_estimator(struct sock *sk, const __u32 mrtt)
1da177e4 625{
6687e988 626 struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
627 long m = mrtt; /* RTT */
628
1da177e4
LT
629 /* The following amusing code comes from Jacobson's
630 * article in SIGCOMM '88. Note that rtt and mdev
631 * are scaled versions of rtt and mean deviation.
e905a9ed 632 * This is designed to be as fast as possible
1da177e4
LT
633 * m stands for "measurement".
634 *
635 * On a 1990 paper the rto value is changed to:
636 * RTO = rtt + 4 * mdev
637 *
638 * Funny. This algorithm seems to be very broken.
639 * These formulae increase RTO, when it should be decreased, increase
31f34269 640 * too slowly, when it should be increased quickly, decrease too quickly
1da177e4
LT
641 * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
642 * does not matter how to _calculate_ it. Seems, it was trap
643 * that VJ failed to avoid. 8)
644 */
2de979bd 645 if (m == 0)
1da177e4
LT
646 m = 1;
647 if (tp->srtt != 0) {
648 m -= (tp->srtt >> 3); /* m is now error in rtt est */
649 tp->srtt += m; /* rtt = 7/8 rtt + 1/8 new */
650 if (m < 0) {
651 m = -m; /* m is now abs(error) */
652 m -= (tp->mdev >> 2); /* similar update on mdev */
653 /* This is similar to one of Eifel findings.
654 * Eifel blocks mdev updates when rtt decreases.
655 * This solution is a bit different: we use finer gain
656 * for mdev in this case (alpha*beta).
657 * Like Eifel it also prevents growth of rto,
658 * but also it limits too fast rto decreases,
659 * happening in pure Eifel.
660 */
661 if (m > 0)
662 m >>= 3;
663 } else {
664 m -= (tp->mdev >> 2); /* similar update on mdev */
665 }
666 tp->mdev += m; /* mdev = 3/4 mdev + 1/4 new */
667 if (tp->mdev > tp->mdev_max) {
668 tp->mdev_max = tp->mdev;
669 if (tp->mdev_max > tp->rttvar)
670 tp->rttvar = tp->mdev_max;
671 }
672 if (after(tp->snd_una, tp->rtt_seq)) {
673 if (tp->mdev_max < tp->rttvar)
674 tp->rttvar -= (tp->rttvar-tp->mdev_max)>>2;
675 tp->rtt_seq = tp->snd_nxt;
05bb1fad 676 tp->mdev_max = tcp_rto_min(sk);
1da177e4
LT
677 }
678 } else {
679 /* no previous measure. */
680 tp->srtt = m<<3; /* take the measured time to be rtt */
681 tp->mdev = m<<1; /* make sure rto = 3*rtt */
05bb1fad 682 tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
1da177e4
LT
683 tp->rtt_seq = tp->snd_nxt;
684 }
1da177e4
LT
685}
686
687/* Calculate rto without backoff. This is the second half of Van Jacobson's
688 * routine referred to above.
689 */
463c84b9 690static inline void tcp_set_rto(struct sock *sk)
1da177e4 691{
463c84b9 692 const struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
693 /* Old crap is replaced with new one. 8)
694 *
695 * More seriously:
696 * 1. If rtt variance happened to be less 50msec, it is hallucination.
697 * It cannot be less due to utterly erratic ACK generation made
698 * at least by solaris and freebsd. "Erratic ACKs" has _nothing_
699 * to do with delayed acks, because at cwnd>2 true delack timeout
700 * is invisible. Actually, Linux-2.4 also generates erratic
caa20d9a 701 * ACKs in some circumstances.
1da177e4 702 */
463c84b9 703 inet_csk(sk)->icsk_rto = (tp->srtt >> 3) + tp->rttvar;
1da177e4
LT
704
705 /* 2. Fixups made earlier cannot be right.
706 * If we do not estimate RTO correctly without them,
707 * all the algo is pure shit and should be replaced
caa20d9a 708 * with correct one. It is exactly, which we pretend to do.
1da177e4
LT
709 */
710}
711
712/* NOTE: clamping at TCP_RTO_MIN is not required, current algo
713 * guarantees that rto is higher.
714 */
463c84b9 715static inline void tcp_bound_rto(struct sock *sk)
1da177e4 716{
463c84b9
ACM
717 if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
718 inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
1da177e4
LT
719}
720
721/* Save metrics learned by this TCP session.
722 This function is called only, when TCP finishes successfully
723 i.e. when it enters TIME-WAIT or goes from LAST-ACK to CLOSE.
724 */
725void tcp_update_metrics(struct sock *sk)
726{
727 struct tcp_sock *tp = tcp_sk(sk);
728 struct dst_entry *dst = __sk_dst_get(sk);
729
730 if (sysctl_tcp_nometrics_save)
731 return;
732
733 dst_confirm(dst);
734
735 if (dst && (dst->flags&DST_HOST)) {
6687e988 736 const struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4
LT
737 int m;
738
6687e988 739 if (icsk->icsk_backoff || !tp->srtt) {
1da177e4
LT
740 /* This session failed to estimate rtt. Why?
741 * Probably, no packets returned in time.
742 * Reset our results.
743 */
744 if (!(dst_metric_locked(dst, RTAX_RTT)))
745 dst->metrics[RTAX_RTT-1] = 0;
746 return;
747 }
748
749 m = dst_metric(dst, RTAX_RTT) - tp->srtt;
750
751 /* If newly calculated rtt larger than stored one,
752 * store new one. Otherwise, use EWMA. Remember,
753 * rtt overestimation is always better than underestimation.
754 */
755 if (!(dst_metric_locked(dst, RTAX_RTT))) {
756 if (m <= 0)
757 dst->metrics[RTAX_RTT-1] = tp->srtt;
758 else
759 dst->metrics[RTAX_RTT-1] -= (m>>3);
760 }
761
762 if (!(dst_metric_locked(dst, RTAX_RTTVAR))) {
763 if (m < 0)
764 m = -m;
765
766 /* Scale deviation to rttvar fixed point */
767 m >>= 1;
768 if (m < tp->mdev)
769 m = tp->mdev;
770
771 if (m >= dst_metric(dst, RTAX_RTTVAR))
772 dst->metrics[RTAX_RTTVAR-1] = m;
773 else
774 dst->metrics[RTAX_RTTVAR-1] -=
775 (dst->metrics[RTAX_RTTVAR-1] - m)>>2;
776 }
777
778 if (tp->snd_ssthresh >= 0xFFFF) {
779 /* Slow start still did not finish. */
780 if (dst_metric(dst, RTAX_SSTHRESH) &&
781 !dst_metric_locked(dst, RTAX_SSTHRESH) &&
782 (tp->snd_cwnd >> 1) > dst_metric(dst, RTAX_SSTHRESH))
783 dst->metrics[RTAX_SSTHRESH-1] = tp->snd_cwnd >> 1;
784 if (!dst_metric_locked(dst, RTAX_CWND) &&
785 tp->snd_cwnd > dst_metric(dst, RTAX_CWND))
786 dst->metrics[RTAX_CWND-1] = tp->snd_cwnd;
787 } else if (tp->snd_cwnd > tp->snd_ssthresh &&
6687e988 788 icsk->icsk_ca_state == TCP_CA_Open) {
1da177e4
LT
789 /* Cong. avoidance phase, cwnd is reliable. */
790 if (!dst_metric_locked(dst, RTAX_SSTHRESH))
791 dst->metrics[RTAX_SSTHRESH-1] =
792 max(tp->snd_cwnd >> 1, tp->snd_ssthresh);
793 if (!dst_metric_locked(dst, RTAX_CWND))
794 dst->metrics[RTAX_CWND-1] = (dst->metrics[RTAX_CWND-1] + tp->snd_cwnd) >> 1;
795 } else {
796 /* Else slow start did not finish, cwnd is non-sense,
797 ssthresh may be also invalid.
798 */
799 if (!dst_metric_locked(dst, RTAX_CWND))
800 dst->metrics[RTAX_CWND-1] = (dst->metrics[RTAX_CWND-1] + tp->snd_ssthresh) >> 1;
801 if (dst->metrics[RTAX_SSTHRESH-1] &&
802 !dst_metric_locked(dst, RTAX_SSTHRESH) &&
803 tp->snd_ssthresh > dst->metrics[RTAX_SSTHRESH-1])
804 dst->metrics[RTAX_SSTHRESH-1] = tp->snd_ssthresh;
805 }
806
807 if (!dst_metric_locked(dst, RTAX_REORDERING)) {
808 if (dst->metrics[RTAX_REORDERING-1] < tp->reordering &&
809 tp->reordering != sysctl_tcp_reordering)
810 dst->metrics[RTAX_REORDERING-1] = tp->reordering;
811 }
812 }
813}
814
26722873
DM
815/* Numbers are taken from RFC3390.
816 *
817 * John Heffner states:
818 *
819 * The RFC specifies a window of no more than 4380 bytes
820 * unless 2*MSS > 4380. Reading the pseudocode in the RFC
821 * is a bit misleading because they use a clamp at 4380 bytes
822 * rather than use a multiplier in the relevant range.
823 */
1da177e4
LT
824__u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst)
825{
826 __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
827
828 if (!cwnd) {
c1b4a7e6 829 if (tp->mss_cache > 1460)
1da177e4
LT
830 cwnd = 2;
831 else
c1b4a7e6 832 cwnd = (tp->mss_cache > 1095) ? 3 : 4;
1da177e4
LT
833 }
834 return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
835}
836
40efc6fa 837/* Set slow start threshold and cwnd not falling to slow start */
3cfe3baa 838void tcp_enter_cwr(struct sock *sk, const int set_ssthresh)
40efc6fa
SH
839{
840 struct tcp_sock *tp = tcp_sk(sk);
3cfe3baa 841 const struct inet_connection_sock *icsk = inet_csk(sk);
40efc6fa
SH
842
843 tp->prior_ssthresh = 0;
844 tp->bytes_acked = 0;
e01f9d77 845 if (icsk->icsk_ca_state < TCP_CA_CWR) {
40efc6fa 846 tp->undo_marker = 0;
3cfe3baa
IJ
847 if (set_ssthresh)
848 tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
40efc6fa
SH
849 tp->snd_cwnd = min(tp->snd_cwnd,
850 tcp_packets_in_flight(tp) + 1U);
851 tp->snd_cwnd_cnt = 0;
852 tp->high_seq = tp->snd_nxt;
853 tp->snd_cwnd_stamp = tcp_time_stamp;
854 TCP_ECN_queue_cwr(tp);
855
856 tcp_set_ca_state(sk, TCP_CA_CWR);
857 }
858}
859
e60402d0
IJ
860/*
861 * Packet counting of FACK is based on in-order assumptions, therefore TCP
862 * disables it when reordering is detected
863 */
864static void tcp_disable_fack(struct tcp_sock *tp)
865{
85cc391c
IJ
866 /* RFC3517 uses different metric in lost marker => reset on change */
867 if (tcp_is_fack(tp))
868 tp->lost_skb_hint = NULL;
e60402d0
IJ
869 tp->rx_opt.sack_ok &= ~2;
870}
871
564262c1 872/* Take a notice that peer is sending D-SACKs */
e60402d0
IJ
873static void tcp_dsack_seen(struct tcp_sock *tp)
874{
875 tp->rx_opt.sack_ok |= 4;
876}
877
1da177e4
LT
878/* Initialize metrics on socket. */
879
880static void tcp_init_metrics(struct sock *sk)
881{
882 struct tcp_sock *tp = tcp_sk(sk);
883 struct dst_entry *dst = __sk_dst_get(sk);
884
885 if (dst == NULL)
886 goto reset;
887
888 dst_confirm(dst);
889
890 if (dst_metric_locked(dst, RTAX_CWND))
891 tp->snd_cwnd_clamp = dst_metric(dst, RTAX_CWND);
892 if (dst_metric(dst, RTAX_SSTHRESH)) {
893 tp->snd_ssthresh = dst_metric(dst, RTAX_SSTHRESH);
894 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
895 tp->snd_ssthresh = tp->snd_cwnd_clamp;
896 }
897 if (dst_metric(dst, RTAX_REORDERING) &&
898 tp->reordering != dst_metric(dst, RTAX_REORDERING)) {
e60402d0 899 tcp_disable_fack(tp);
1da177e4
LT
900 tp->reordering = dst_metric(dst, RTAX_REORDERING);
901 }
902
903 if (dst_metric(dst, RTAX_RTT) == 0)
904 goto reset;
905
906 if (!tp->srtt && dst_metric(dst, RTAX_RTT) < (TCP_TIMEOUT_INIT << 3))
907 goto reset;
908
909 /* Initial rtt is determined from SYN,SYN-ACK.
910 * The segment is small and rtt may appear much
911 * less than real one. Use per-dst memory
912 * to make it more realistic.
913 *
914 * A bit of theory. RTT is time passed after "normal" sized packet
caa20d9a 915 * is sent until it is ACKed. In normal circumstances sending small
1da177e4
LT
916 * packets force peer to delay ACKs and calculation is correct too.
917 * The algorithm is adaptive and, provided we follow specs, it
918 * NEVER underestimate RTT. BUT! If peer tries to make some clever
919 * tricks sort of "quick acks" for time long enough to decrease RTT
920 * to low value, and then abruptly stops to do it and starts to delay
921 * ACKs, wait for troubles.
922 */
923 if (dst_metric(dst, RTAX_RTT) > tp->srtt) {
924 tp->srtt = dst_metric(dst, RTAX_RTT);
925 tp->rtt_seq = tp->snd_nxt;
926 }
927 if (dst_metric(dst, RTAX_RTTVAR) > tp->mdev) {
928 tp->mdev = dst_metric(dst, RTAX_RTTVAR);
488faa2a 929 tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
1da177e4 930 }
463c84b9
ACM
931 tcp_set_rto(sk);
932 tcp_bound_rto(sk);
933 if (inet_csk(sk)->icsk_rto < TCP_TIMEOUT_INIT && !tp->rx_opt.saw_tstamp)
1da177e4
LT
934 goto reset;
935 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
936 tp->snd_cwnd_stamp = tcp_time_stamp;
937 return;
938
939reset:
940 /* Play conservative. If timestamps are not
941 * supported, TCP will fail to recalculate correct
942 * rtt, if initial rto is too small. FORGET ALL AND RESET!
943 */
944 if (!tp->rx_opt.saw_tstamp && tp->srtt) {
945 tp->srtt = 0;
946 tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_INIT;
463c84b9 947 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_INIT;
1da177e4
LT
948 }
949}
950
6687e988
ACM
951static void tcp_update_reordering(struct sock *sk, const int metric,
952 const int ts)
1da177e4 953{
6687e988 954 struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
955 if (metric > tp->reordering) {
956 tp->reordering = min(TCP_MAX_REORDERING, metric);
957
958 /* This exciting event is worth to be remembered. 8) */
959 if (ts)
960 NET_INC_STATS_BH(LINUX_MIB_TCPTSREORDER);
e60402d0 961 else if (tcp_is_reno(tp))
1da177e4 962 NET_INC_STATS_BH(LINUX_MIB_TCPRENOREORDER);
e60402d0 963 else if (tcp_is_fack(tp))
1da177e4
LT
964 NET_INC_STATS_BH(LINUX_MIB_TCPFACKREORDER);
965 else
966 NET_INC_STATS_BH(LINUX_MIB_TCPSACKREORDER);
967#if FASTRETRANS_DEBUG > 1
968 printk(KERN_DEBUG "Disorder%d %d %u f%u s%u rr%d\n",
6687e988 969 tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
1da177e4
LT
970 tp->reordering,
971 tp->fackets_out,
972 tp->sacked_out,
973 tp->undo_marker ? tp->undo_retrans : 0);
974#endif
e60402d0 975 tcp_disable_fack(tp);
1da177e4
LT
976 }
977}
978
979/* This procedure tags the retransmission queue when SACKs arrive.
980 *
981 * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
982 * Packets in queue with these bits set are counted in variables
983 * sacked_out, retrans_out and lost_out, correspondingly.
984 *
985 * Valid combinations are:
986 * Tag InFlight Description
987 * 0 1 - orig segment is in flight.
988 * S 0 - nothing flies, orig reached receiver.
989 * L 0 - nothing flies, orig lost by net.
990 * R 2 - both orig and retransmit are in flight.
991 * L|R 1 - orig is lost, retransmit is in flight.
992 * S|R 1 - orig reached receiver, retrans is still in flight.
993 * (L|S|R is logically valid, it could occur when L|R is sacked,
994 * but it is equivalent to plain S and code short-curcuits it to S.
995 * L|S is logically invalid, it would mean -1 packet in flight 8))
996 *
997 * These 6 states form finite state machine, controlled by the following events:
998 * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
999 * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
1000 * 3. Loss detection event of one of three flavors:
1001 * A. Scoreboard estimator decided the packet is lost.
1002 * A'. Reno "three dupacks" marks head of queue lost.
1003 * A''. Its FACK modfication, head until snd.fack is lost.
1004 * B. SACK arrives sacking data transmitted after never retransmitted
1005 * hole was sent out.
1006 * C. SACK arrives sacking SND.NXT at the moment, when the
1007 * segment was retransmitted.
1008 * 4. D-SACK added new rule: D-SACK changes any tag to S.
1009 *
1010 * It is pleasant to note, that state diagram turns out to be commutative,
1011 * so that we are allowed not to be bothered by order of our actions,
1012 * when multiple events arrive simultaneously. (see the function below).
1013 *
1014 * Reordering detection.
1015 * --------------------
1016 * Reordering metric is maximal distance, which a packet can be displaced
1017 * in packet stream. With SACKs we can estimate it:
1018 *
1019 * 1. SACK fills old hole and the corresponding segment was not
1020 * ever retransmitted -> reordering. Alas, we cannot use it
1021 * when segment was retransmitted.
1022 * 2. The last flaw is solved with D-SACK. D-SACK arrives
1023 * for retransmitted and already SACKed segment -> reordering..
1024 * Both of these heuristics are not used in Loss state, when we cannot
1025 * account for retransmits accurately.
5b3c9882
IJ
1026 *
1027 * SACK block validation.
1028 * ----------------------
1029 *
1030 * SACK block range validation checks that the received SACK block fits to
1031 * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT.
1032 * Note that SND.UNA is not included to the range though being valid because
0e835331
IJ
1033 * it means that the receiver is rather inconsistent with itself reporting
1034 * SACK reneging when it should advance SND.UNA. Such SACK block this is
1035 * perfectly valid, however, in light of RFC2018 which explicitly states
1036 * that "SACK block MUST reflect the newest segment. Even if the newest
1037 * segment is going to be discarded ...", not that it looks very clever
1038 * in case of head skb. Due to potentional receiver driven attacks, we
1039 * choose to avoid immediate execution of a walk in write queue due to
1040 * reneging and defer head skb's loss recovery to standard loss recovery
1041 * procedure that will eventually trigger (nothing forbids us doing this).
5b3c9882
IJ
1042 *
1043 * Implements also blockage to start_seq wrap-around. Problem lies in the
1044 * fact that though start_seq (s) is before end_seq (i.e., not reversed),
1045 * there's no guarantee that it will be before snd_nxt (n). The problem
1046 * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt
1047 * wrap (s_w):
1048 *
1049 * <- outs wnd -> <- wrapzone ->
1050 * u e n u_w e_w s n_w
1051 * | | | | | | |
1052 * |<------------+------+----- TCP seqno space --------------+---------->|
1053 * ...-- <2^31 ->| |<--------...
1054 * ...---- >2^31 ------>| |<--------...
1055 *
1056 * Current code wouldn't be vulnerable but it's better still to discard such
1057 * crazy SACK blocks. Doing this check for start_seq alone closes somewhat
1058 * similar case (end_seq after snd_nxt wrap) as earlier reversed check in
1059 * snd_nxt wrap -> snd_una region will then become "well defined", i.e.,
1060 * equal to the ideal case (infinite seqno space without wrap caused issues).
1061 *
1062 * With D-SACK the lower bound is extended to cover sequence space below
1063 * SND.UNA down to undo_marker, which is the last point of interest. Yet
564262c1 1064 * again, D-SACK block must not to go across snd_una (for the same reason as
5b3c9882
IJ
1065 * for the normal SACK blocks, explained above). But there all simplicity
1066 * ends, TCP might receive valid D-SACKs below that. As long as they reside
1067 * fully below undo_marker they do not affect behavior in anyway and can
1068 * therefore be safely ignored. In rare cases (which are more or less
1069 * theoretical ones), the D-SACK will nicely cross that boundary due to skb
1070 * fragmentation and packet reordering past skb's retransmission. To consider
1071 * them correctly, the acceptable range must be extended even more though
1072 * the exact amount is rather hard to quantify. However, tp->max_window can
1073 * be used as an exaggerated estimate.
1da177e4 1074 */
5b3c9882
IJ
1075static int tcp_is_sackblock_valid(struct tcp_sock *tp, int is_dsack,
1076 u32 start_seq, u32 end_seq)
1077{
1078 /* Too far in future, or reversed (interpretation is ambiguous) */
1079 if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq))
1080 return 0;
1081
1082 /* Nasty start_seq wrap-around check (see comments above) */
1083 if (!before(start_seq, tp->snd_nxt))
1084 return 0;
1085
564262c1 1086 /* In outstanding window? ...This is valid exit for D-SACKs too.
5b3c9882
IJ
1087 * start_seq == snd_una is non-sensical (see comments above)
1088 */
1089 if (after(start_seq, tp->snd_una))
1090 return 1;
1091
1092 if (!is_dsack || !tp->undo_marker)
1093 return 0;
1094
1095 /* ...Then it's D-SACK, and must reside below snd_una completely */
1096 if (!after(end_seq, tp->snd_una))
1097 return 0;
1098
1099 if (!before(start_seq, tp->undo_marker))
1100 return 1;
1101
1102 /* Too old */
1103 if (!after(end_seq, tp->undo_marker))
1104 return 0;
1105
1106 /* Undo_marker boundary crossing (overestimates a lot). Known already:
1107 * start_seq < undo_marker and end_seq >= undo_marker.
1108 */
1109 return !before(start_seq, end_seq - tp->max_window);
1110}
1111
1c1e87ed
IJ
1112/* Check for lost retransmit. This superb idea is borrowed from "ratehalving".
1113 * Event "C". Later note: FACK people cheated me again 8), we have to account
1114 * for reordering! Ugly, but should help.
f785a8e2
IJ
1115 *
1116 * Search retransmitted skbs from write_queue that were sent when snd_nxt was
1117 * less than what is now known to be received by the other end (derived from
b08d6cb2
IJ
1118 * SACK blocks by the caller). Also calculate the lowest snd_nxt among the
1119 * remaining retransmitted skbs to avoid some costly processing per ACKs.
1c1e87ed 1120 */
f785a8e2 1121static int tcp_mark_lost_retrans(struct sock *sk, u32 received_upto)
1c1e87ed
IJ
1122{
1123 struct tcp_sock *tp = tcp_sk(sk);
1124 struct sk_buff *skb;
1125 int flag = 0;
f785a8e2 1126 int cnt = 0;
df2e014b 1127 u32 new_low_seq = tp->snd_nxt;
1c1e87ed
IJ
1128
1129 tcp_for_write_queue(skb, sk) {
1130 u32 ack_seq = TCP_SKB_CB(skb)->ack_seq;
1131
1132 if (skb == tcp_send_head(sk))
1133 break;
f785a8e2 1134 if (cnt == tp->retrans_out)
1c1e87ed
IJ
1135 break;
1136 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1137 continue;
1138
f785a8e2
IJ
1139 if (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS))
1140 continue;
1141
1142 if (after(received_upto, ack_seq) &&
1c1e87ed 1143 (tcp_is_fack(tp) ||
f785a8e2 1144 !before(received_upto,
1c1e87ed
IJ
1145 ack_seq + tp->reordering * tp->mss_cache))) {
1146 TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
1147 tp->retrans_out -= tcp_skb_pcount(skb);
1148
1149 /* clear lost hint */
1150 tp->retransmit_skb_hint = NULL;
1151
1152 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
1153 tp->lost_out += tcp_skb_pcount(skb);
1154 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
1155 flag |= FLAG_DATA_SACKED;
1156 NET_INC_STATS_BH(LINUX_MIB_TCPLOSTRETRANSMIT);
1157 }
f785a8e2 1158 } else {
df2e014b 1159 if (before(ack_seq, new_low_seq))
b08d6cb2 1160 new_low_seq = ack_seq;
f785a8e2 1161 cnt += tcp_skb_pcount(skb);
1c1e87ed
IJ
1162 }
1163 }
b08d6cb2
IJ
1164
1165 if (tp->retrans_out)
1166 tp->lost_retrans_low = new_low_seq;
1167
1c1e87ed
IJ
1168 return flag;
1169}
5b3c9882 1170
d06e021d
DM
1171static int tcp_check_dsack(struct tcp_sock *tp, struct sk_buff *ack_skb,
1172 struct tcp_sack_block_wire *sp, int num_sacks,
1173 u32 prior_snd_una)
1174{
1175 u32 start_seq_0 = ntohl(get_unaligned(&sp[0].start_seq));
1176 u32 end_seq_0 = ntohl(get_unaligned(&sp[0].end_seq));
1177 int dup_sack = 0;
1178
1179 if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) {
1180 dup_sack = 1;
e60402d0 1181 tcp_dsack_seen(tp);
d06e021d
DM
1182 NET_INC_STATS_BH(LINUX_MIB_TCPDSACKRECV);
1183 } else if (num_sacks > 1) {
1184 u32 end_seq_1 = ntohl(get_unaligned(&sp[1].end_seq));
1185 u32 start_seq_1 = ntohl(get_unaligned(&sp[1].start_seq));
1186
1187 if (!after(end_seq_0, end_seq_1) &&
1188 !before(start_seq_0, start_seq_1)) {
1189 dup_sack = 1;
e60402d0 1190 tcp_dsack_seen(tp);
d06e021d
DM
1191 NET_INC_STATS_BH(LINUX_MIB_TCPDSACKOFORECV);
1192 }
1193 }
1194
1195 /* D-SACK for already forgotten data... Do dumb counting. */
1196 if (dup_sack &&
1197 !after(end_seq_0, prior_snd_una) &&
1198 after(end_seq_0, tp->undo_marker))
1199 tp->undo_retrans--;
1200
1201 return dup_sack;
1202}
1203
d1935942
IJ
1204/* Check if skb is fully within the SACK block. In presence of GSO skbs,
1205 * the incoming SACK may not exactly match but we can find smaller MSS
1206 * aligned portion of it that matches. Therefore we might need to fragment
1207 * which may fail and creates some hassle (caller must handle error case
1208 * returns).
1209 */
0f79efdc
AB
1210static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
1211 u32 start_seq, u32 end_seq)
d1935942
IJ
1212{
1213 int in_sack, err;
1214 unsigned int pkt_len;
1215
1216 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1217 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1218
1219 if (tcp_skb_pcount(skb) > 1 && !in_sack &&
1220 after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
1221
1222 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1223
1224 if (!in_sack)
1225 pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
1226 else
1227 pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
1228 err = tcp_fragment(sk, skb, pkt_len, skb_shinfo(skb)->gso_size);
1229 if (err < 0)
1230 return err;
1231 }
1232
1233 return in_sack;
1234}
1235
1da177e4
LT
1236static int
1237tcp_sacktag_write_queue(struct sock *sk, struct sk_buff *ack_skb, u32 prior_snd_una)
1238{
6687e988 1239 const struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 1240 struct tcp_sock *tp = tcp_sk(sk);
9c70220b
ACM
1241 unsigned char *ptr = (skb_transport_header(ack_skb) +
1242 TCP_SKB_CB(ack_skb)->sacked);
269bd27e 1243 struct tcp_sack_block_wire *sp = (struct tcp_sack_block_wire *)(ptr+2);
fda03fbb 1244 struct sk_buff *cached_skb;
1da177e4
LT
1245 int num_sacks = (ptr[1] - TCPOLEN_SACK_BASE)>>3;
1246 int reord = tp->packets_out;
1247 int prior_fackets;
a47e5a98 1248 u32 highest_sack_end_seq;
1da177e4 1249 int flag = 0;
7769f406 1250 int found_dup_sack = 0;
fda03fbb 1251 int cached_fack_count;
1da177e4 1252 int i;
fda03fbb 1253 int first_sack_index;
94d3b1e5 1254 int force_one_sack;
1da177e4 1255
d738cd8f 1256 if (!tp->sacked_out) {
de83c058
IJ
1257 if (WARN_ON(tp->fackets_out))
1258 tp->fackets_out = 0;
a47e5a98 1259 tp->highest_sack = tcp_write_queue_head(sk);
d738cd8f 1260 }
1da177e4
LT
1261 prior_fackets = tp->fackets_out;
1262
d06e021d
DM
1263 found_dup_sack = tcp_check_dsack(tp, ack_skb, sp,
1264 num_sacks, prior_snd_una);
1265 if (found_dup_sack)
49ff4bb4 1266 flag |= FLAG_DSACKING_ACK;
6f74651a
BE
1267
1268 /* Eliminate too old ACKs, but take into
1269 * account more or less fresh ones, they can
1270 * contain valid SACK info.
1271 */
1272 if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
1273 return 0;
1274