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