]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/ipv4/tcp_input.c
mm: add a "struct page_frag" type containing a page, offset and length
[mirror_ubuntu-artful-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 *
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
209static inline void TCP_ECN_accept_cwr(struct tcp_sock *tp, struct sk_buff *skb)
210{
211 if (tcp_hdr(skb)->cwr)
212 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
213}
214
215static inline void TCP_ECN_withdraw_cwr(struct tcp_sock *tp)
216{
217 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
218}
219
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
242static inline void TCP_ECN_rcv_synack(struct tcp_sock *tp, struct tcphdr *th)
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
248static inline void TCP_ECN_rcv_syn(struct tcp_sock *tp, struct tcphdr *th)
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
254static inline int TCP_ECN_rcv_ecn_echo(struct tcp_sock *tp, struct tcphdr *th)
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
056834d9 318static void tcp_grow_window(struct sock *sk, 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{
348 struct tcp_sock *tp = tcp_sk(sk);
87fb4b7b 349 int rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
1da177e4
LT
350
351 /* Try to select rcvbuf so that 4 mss-sized segments
caa20d9a 352 * will fit to window and corresponding skbs will fit to our rcvbuf.
1da177e4
LT
353 * (was 3; 4 is minimum to allow fast retransmit to work.)
354 */
355 while (tcp_win_from_space(rcvmem) < tp->advmss)
356 rcvmem += 128;
357 if (sk->sk_rcvbuf < 4 * rcvmem)
358 sk->sk_rcvbuf = min(4 * rcvmem, sysctl_tcp_rmem[2]);
359}
360
caa20d9a 361/* 4. Try to fixup all. It is made immediately after connection enters
1da177e4
LT
362 * established state.
363 */
364static void tcp_init_buffer_space(struct sock *sk)
365{
366 struct tcp_sock *tp = tcp_sk(sk);
367 int maxwin;
368
369 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
370 tcp_fixup_rcvbuf(sk);
371 if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
372 tcp_fixup_sndbuf(sk);
373
374 tp->rcvq_space.space = tp->rcv_wnd;
375
376 maxwin = tcp_full_space(sk);
377
378 if (tp->window_clamp >= maxwin) {
379 tp->window_clamp = maxwin;
380
381 if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss)
382 tp->window_clamp = max(maxwin -
383 (maxwin >> sysctl_tcp_app_win),
384 4 * tp->advmss);
385 }
386
387 /* Force reservation of one segment. */
388 if (sysctl_tcp_app_win &&
389 tp->window_clamp > 2 * tp->advmss &&
390 tp->window_clamp + tp->advmss > maxwin)
391 tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss);
392
393 tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
394 tp->snd_cwnd_stamp = tcp_time_stamp;
395}
396
1da177e4 397/* 5. Recalculate window clamp after socket hit its memory bounds. */
9e412ba7 398static void tcp_clamp_window(struct sock *sk)
1da177e4 399{
9e412ba7 400 struct tcp_sock *tp = tcp_sk(sk);
6687e988 401 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 402
6687e988 403 icsk->icsk_ack.quick = 0;
1da177e4 404
326f36e9
JH
405 if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] &&
406 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
407 !tcp_memory_pressure &&
8d987e5c 408 atomic_long_read(&tcp_memory_allocated) < sysctl_tcp_mem[0]) {
326f36e9
JH
409 sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),
410 sysctl_tcp_rmem[2]);
1da177e4 411 }
326f36e9 412 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
056834d9 413 tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss);
1da177e4
LT
414}
415
40efc6fa
SH
416/* Initialize RCV_MSS value.
417 * RCV_MSS is an our guess about MSS used by the peer.
418 * We haven't any direct information about the MSS.
419 * It's better to underestimate the RCV_MSS rather than overestimate.
420 * Overestimations make us ACKing less frequently than needed.
421 * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
422 */
423void tcp_initialize_rcv_mss(struct sock *sk)
424{
425 struct tcp_sock *tp = tcp_sk(sk);
426 unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
427
056834d9 428 hint = min(hint, tp->rcv_wnd / 2);
bee7ca9e 429 hint = min(hint, TCP_MSS_DEFAULT);
40efc6fa
SH
430 hint = max(hint, TCP_MIN_MSS);
431
432 inet_csk(sk)->icsk_ack.rcv_mss = hint;
433}
4bc2f18b 434EXPORT_SYMBOL(tcp_initialize_rcv_mss);
40efc6fa 435
1da177e4
LT
436/* Receiver "autotuning" code.
437 *
438 * The algorithm for RTT estimation w/o timestamps is based on
439 * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL.
631dd1a8 440 * <http://public.lanl.gov/radiant/pubs.html#DRS>
1da177e4
LT
441 *
442 * More detail on this code can be found at
631dd1a8 443 * <http://staff.psc.edu/jheffner/>,
1da177e4
LT
444 * though this reference is out of date. A new paper
445 * is pending.
446 */
447static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
448{
449 u32 new_sample = tp->rcv_rtt_est.rtt;
450 long m = sample;
451
452 if (m == 0)
453 m = 1;
454
455 if (new_sample != 0) {
456 /* If we sample in larger samples in the non-timestamp
457 * case, we could grossly overestimate the RTT especially
458 * with chatty applications or bulk transfer apps which
459 * are stalled on filesystem I/O.
460 *
461 * Also, since we are only going for a minimum in the
31f34269 462 * non-timestamp case, we do not smooth things out
caa20d9a 463 * else with timestamps disabled convergence takes too
1da177e4
LT
464 * long.
465 */
466 if (!win_dep) {
467 m -= (new_sample >> 3);
468 new_sample += m;
469 } else if (m < new_sample)
470 new_sample = m << 3;
471 } else {
caa20d9a 472 /* No previous measure. */
1da177e4
LT
473 new_sample = m << 3;
474 }
475
476 if (tp->rcv_rtt_est.rtt != new_sample)
477 tp->rcv_rtt_est.rtt = new_sample;
478}
479
480static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
481{
482 if (tp->rcv_rtt_est.time == 0)
483 goto new_measure;
484 if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))
485 return;
056834d9 486 tcp_rcv_rtt_update(tp, jiffies - tp->rcv_rtt_est.time, 1);
1da177e4
LT
487
488new_measure:
489 tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
490 tp->rcv_rtt_est.time = tcp_time_stamp;
491}
492
056834d9
IJ
493static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
494 const struct sk_buff *skb)
1da177e4 495{
463c84b9 496 struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
497 if (tp->rx_opt.rcv_tsecr &&
498 (TCP_SKB_CB(skb)->end_seq -
463c84b9 499 TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss))
1da177e4
LT
500 tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rx_opt.rcv_tsecr, 0);
501}
502
503/*
504 * This function should be called every time data is copied to user space.
505 * It calculates the appropriate TCP receive buffer space.
506 */
507void tcp_rcv_space_adjust(struct sock *sk)
508{
509 struct tcp_sock *tp = tcp_sk(sk);
510 int time;
511 int space;
e905a9ed 512
1da177e4
LT
513 if (tp->rcvq_space.time == 0)
514 goto new_measure;
e905a9ed 515
1da177e4 516 time = tcp_time_stamp - tp->rcvq_space.time;
056834d9 517 if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0)
1da177e4 518 return;
e905a9ed 519
1da177e4
LT
520 space = 2 * (tp->copied_seq - tp->rcvq_space.seq);
521
522 space = max(tp->rcvq_space.space, space);
523
524 if (tp->rcvq_space.space != space) {
525 int rcvmem;
526
527 tp->rcvq_space.space = space;
528
6fcf9412
JH
529 if (sysctl_tcp_moderate_rcvbuf &&
530 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1da177e4
LT
531 int new_clamp = space;
532
533 /* Receive space grows, normalize in order to
534 * take into account packet headers and sk_buff
535 * structure overhead.
536 */
537 space /= tp->advmss;
538 if (!space)
539 space = 1;
87fb4b7b 540 rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
1da177e4
LT
541 while (tcp_win_from_space(rcvmem) < tp->advmss)
542 rcvmem += 128;
543 space *= rcvmem;
544 space = min(space, sysctl_tcp_rmem[2]);
545 if (space > sk->sk_rcvbuf) {
546 sk->sk_rcvbuf = space;
547
548 /* Make the window clamp follow along. */
549 tp->window_clamp = new_clamp;
550 }
551 }
552 }
e905a9ed 553
1da177e4
LT
554new_measure:
555 tp->rcvq_space.seq = tp->copied_seq;
556 tp->rcvq_space.time = tcp_time_stamp;
557}
558
559/* There is something which you must keep in mind when you analyze the
560 * behavior of the tp->ato delayed ack timeout interval. When a
561 * connection starts up, we want to ack as quickly as possible. The
562 * problem is that "good" TCP's do slow start at the beginning of data
563 * transmission. The means that until we send the first few ACK's the
564 * sender will sit on his end and only queue most of his data, because
565 * he can only send snd_cwnd unacked packets at any given time. For
566 * each ACK we send, he increments snd_cwnd and transmits more of his
567 * queue. -DaveM
568 */
9e412ba7 569static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
1da177e4 570{
9e412ba7 571 struct tcp_sock *tp = tcp_sk(sk);
463c84b9 572 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4
LT
573 u32 now;
574
463c84b9 575 inet_csk_schedule_ack(sk);
1da177e4 576
463c84b9 577 tcp_measure_rcv_mss(sk, skb);
1da177e4
LT
578
579 tcp_rcv_rtt_measure(tp);
e905a9ed 580
1da177e4
LT
581 now = tcp_time_stamp;
582
463c84b9 583 if (!icsk->icsk_ack.ato) {
1da177e4
LT
584 /* The _first_ data packet received, initialize
585 * delayed ACK engine.
586 */
463c84b9
ACM
587 tcp_incr_quickack(sk);
588 icsk->icsk_ack.ato = TCP_ATO_MIN;
1da177e4 589 } else {
463c84b9 590 int m = now - icsk->icsk_ack.lrcvtime;
1da177e4 591
056834d9 592 if (m <= TCP_ATO_MIN / 2) {
1da177e4 593 /* The fastest case is the first. */
463c84b9
ACM
594 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
595 } else if (m < icsk->icsk_ack.ato) {
596 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m;
597 if (icsk->icsk_ack.ato > icsk->icsk_rto)
598 icsk->icsk_ack.ato = icsk->icsk_rto;
599 } else if (m > icsk->icsk_rto) {
caa20d9a 600 /* Too long gap. Apparently sender failed to
1da177e4
LT
601 * restart window, so that we send ACKs quickly.
602 */
463c84b9 603 tcp_incr_quickack(sk);
3ab224be 604 sk_mem_reclaim(sk);
1da177e4
LT
605 }
606 }
463c84b9 607 icsk->icsk_ack.lrcvtime = now;
1da177e4
LT
608
609 TCP_ECN_check_ce(tp, skb);
610
611 if (skb->len >= 128)
9e412ba7 612 tcp_grow_window(sk, skb);
1da177e4
LT
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)
056834d9 674 tp->rttvar -= (tp->rttvar - tp->mdev_max) >> 2;
1da177e4 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. */
056834d9
IJ
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 */
f1ecd5d9 703 inet_csk(sk)->icsk_rto = __tcp_set_rto(tp);
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 709 */
1da177e4 710
ee6aac59
IJ
711 /* NOTE: clamping at TCP_RTO_MIN is not required, current algo
712 * guarantees that rto is higher.
713 */
f1ecd5d9 714 tcp_bound_rto(sk);
1da177e4
LT
715}
716
717/* Save metrics learned by this TCP session.
718 This function is called only, when TCP finishes successfully
719 i.e. when it enters TIME-WAIT or goes from LAST-ACK to CLOSE.
720 */
721void tcp_update_metrics(struct sock *sk)
722{
723 struct tcp_sock *tp = tcp_sk(sk);
724 struct dst_entry *dst = __sk_dst_get(sk);
725
726 if (sysctl_tcp_nometrics_save)
727 return;
728
729 dst_confirm(dst);
730
056834d9 731 if (dst && (dst->flags & DST_HOST)) {
6687e988 732 const struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 733 int m;
c1e20f7c 734 unsigned long rtt;
1da177e4 735
6687e988 736 if (icsk->icsk_backoff || !tp->srtt) {
1da177e4
LT
737 /* This session failed to estimate rtt. Why?
738 * Probably, no packets returned in time.
739 * Reset our results.
740 */
741 if (!(dst_metric_locked(dst, RTAX_RTT)))
defb3519 742 dst_metric_set(dst, RTAX_RTT, 0);
1da177e4
LT
743 return;
744 }
745
c1e20f7c
SH
746 rtt = dst_metric_rtt(dst, RTAX_RTT);
747 m = rtt - tp->srtt;
1da177e4
LT
748
749 /* If newly calculated rtt larger than stored one,
750 * store new one. Otherwise, use EWMA. Remember,
751 * rtt overestimation is always better than underestimation.
752 */
753 if (!(dst_metric_locked(dst, RTAX_RTT))) {
754 if (m <= 0)
c1e20f7c 755 set_dst_metric_rtt(dst, RTAX_RTT, tp->srtt);
1da177e4 756 else
c1e20f7c 757 set_dst_metric_rtt(dst, RTAX_RTT, rtt - (m >> 3));
1da177e4
LT
758 }
759
760 if (!(dst_metric_locked(dst, RTAX_RTTVAR))) {
c1e20f7c 761 unsigned long var;
1da177e4
LT
762 if (m < 0)
763 m = -m;
764
765 /* Scale deviation to rttvar fixed point */
766 m >>= 1;
767 if (m < tp->mdev)
768 m = tp->mdev;
769
c1e20f7c
SH
770 var = dst_metric_rtt(dst, RTAX_RTTVAR);
771 if (m >= var)
772 var = m;
1da177e4 773 else
c1e20f7c
SH
774 var -= (var - m) >> 2;
775
776 set_dst_metric_rtt(dst, RTAX_RTTVAR, var);
1da177e4
LT
777 }
778
0b6a05c1 779 if (tcp_in_initial_slowstart(tp)) {
1da177e4
LT
780 /* Slow start still did not finish. */
781 if (dst_metric(dst, RTAX_SSTHRESH) &&
782 !dst_metric_locked(dst, RTAX_SSTHRESH) &&
783 (tp->snd_cwnd >> 1) > dst_metric(dst, RTAX_SSTHRESH))
defb3519 784 dst_metric_set(dst, RTAX_SSTHRESH, tp->snd_cwnd >> 1);
1da177e4
LT
785 if (!dst_metric_locked(dst, RTAX_CWND) &&
786 tp->snd_cwnd > dst_metric(dst, RTAX_CWND))
defb3519 787 dst_metric_set(dst, RTAX_CWND, tp->snd_cwnd);
1da177e4 788 } else if (tp->snd_cwnd > tp->snd_ssthresh &&
6687e988 789 icsk->icsk_ca_state == TCP_CA_Open) {
1da177e4
LT
790 /* Cong. avoidance phase, cwnd is reliable. */
791 if (!dst_metric_locked(dst, RTAX_SSTHRESH))
defb3519
DM
792 dst_metric_set(dst, RTAX_SSTHRESH,
793 max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
1da177e4 794 if (!dst_metric_locked(dst, RTAX_CWND))
defb3519
DM
795 dst_metric_set(dst, RTAX_CWND,
796 (dst_metric(dst, RTAX_CWND) +
797 tp->snd_cwnd) >> 1);
1da177e4
LT
798 } else {
799 /* Else slow start did not finish, cwnd is non-sense,
800 ssthresh may be also invalid.
801 */
802 if (!dst_metric_locked(dst, RTAX_CWND))
defb3519
DM
803 dst_metric_set(dst, RTAX_CWND,
804 (dst_metric(dst, RTAX_CWND) +
805 tp->snd_ssthresh) >> 1);
5ffc02a1 806 if (dst_metric(dst, RTAX_SSTHRESH) &&
1da177e4 807 !dst_metric_locked(dst, RTAX_SSTHRESH) &&
5ffc02a1 808 tp->snd_ssthresh > dst_metric(dst, RTAX_SSTHRESH))
defb3519 809 dst_metric_set(dst, RTAX_SSTHRESH, tp->snd_ssthresh);
1da177e4
LT
810 }
811
812 if (!dst_metric_locked(dst, RTAX_REORDERING)) {
5ffc02a1 813 if (dst_metric(dst, RTAX_REORDERING) < tp->reordering &&
1da177e4 814 tp->reordering != sysctl_tcp_reordering)
defb3519 815 dst_metric_set(dst, RTAX_REORDERING, tp->reordering);
1da177e4
LT
816 }
817 }
818}
819
1da177e4
LT
820__u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst)
821{
822 __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
823
22b71c8f 824 if (!cwnd)
442b9635 825 cwnd = TCP_INIT_CWND;
1da177e4
LT
826 return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
827}
828
40efc6fa 829/* Set slow start threshold and cwnd not falling to slow start */
3cfe3baa 830void tcp_enter_cwr(struct sock *sk, const int set_ssthresh)
40efc6fa
SH
831{
832 struct tcp_sock *tp = tcp_sk(sk);
3cfe3baa 833 const struct inet_connection_sock *icsk = inet_csk(sk);
40efc6fa
SH
834
835 tp->prior_ssthresh = 0;
836 tp->bytes_acked = 0;
e01f9d77 837 if (icsk->icsk_ca_state < TCP_CA_CWR) {
40efc6fa 838 tp->undo_marker = 0;
3cfe3baa
IJ
839 if (set_ssthresh)
840 tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
40efc6fa
SH
841 tp->snd_cwnd = min(tp->snd_cwnd,
842 tcp_packets_in_flight(tp) + 1U);
843 tp->snd_cwnd_cnt = 0;
844 tp->high_seq = tp->snd_nxt;
845 tp->snd_cwnd_stamp = tcp_time_stamp;
846 TCP_ECN_queue_cwr(tp);
847
848 tcp_set_ca_state(sk, TCP_CA_CWR);
849 }
850}
851
e60402d0
IJ
852/*
853 * Packet counting of FACK is based on in-order assumptions, therefore TCP
854 * disables it when reordering is detected
855 */
856static void tcp_disable_fack(struct tcp_sock *tp)
857{
85cc391c
IJ
858 /* RFC3517 uses different metric in lost marker => reset on change */
859 if (tcp_is_fack(tp))
860 tp->lost_skb_hint = NULL;
e60402d0
IJ
861 tp->rx_opt.sack_ok &= ~2;
862}
863
564262c1 864/* Take a notice that peer is sending D-SACKs */
e60402d0
IJ
865static void tcp_dsack_seen(struct tcp_sock *tp)
866{
867 tp->rx_opt.sack_ok |= 4;
868}
869
1da177e4
LT
870/* Initialize metrics on socket. */
871
872static void tcp_init_metrics(struct sock *sk)
873{
874 struct tcp_sock *tp = tcp_sk(sk);
875 struct dst_entry *dst = __sk_dst_get(sk);
876
877 if (dst == NULL)
878 goto reset;
879
880 dst_confirm(dst);
881
882 if (dst_metric_locked(dst, RTAX_CWND))
883 tp->snd_cwnd_clamp = dst_metric(dst, RTAX_CWND);
884 if (dst_metric(dst, RTAX_SSTHRESH)) {
885 tp->snd_ssthresh = dst_metric(dst, RTAX_SSTHRESH);
886 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
887 tp->snd_ssthresh = tp->snd_cwnd_clamp;
9ad7c049
JC
888 } else {
889 /* ssthresh may have been reduced unnecessarily during.
890 * 3WHS. Restore it back to its initial default.
891 */
892 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
1da177e4
LT
893 }
894 if (dst_metric(dst, RTAX_REORDERING) &&
895 tp->reordering != dst_metric(dst, RTAX_REORDERING)) {
e60402d0 896 tcp_disable_fack(tp);
1da177e4
LT
897 tp->reordering = dst_metric(dst, RTAX_REORDERING);
898 }
899
9ad7c049 900 if (dst_metric(dst, RTAX_RTT) == 0 || tp->srtt == 0)
1da177e4
LT
901 goto reset;
902
903 /* Initial rtt is determined from SYN,SYN-ACK.
904 * The segment is small and rtt may appear much
905 * less than real one. Use per-dst memory
906 * to make it more realistic.
907 *
908 * A bit of theory. RTT is time passed after "normal" sized packet
caa20d9a 909 * is sent until it is ACKed. In normal circumstances sending small
1da177e4
LT
910 * packets force peer to delay ACKs and calculation is correct too.
911 * The algorithm is adaptive and, provided we follow specs, it
912 * NEVER underestimate RTT. BUT! If peer tries to make some clever
913 * tricks sort of "quick acks" for time long enough to decrease RTT
914 * to low value, and then abruptly stops to do it and starts to delay
915 * ACKs, wait for troubles.
916 */
c1e20f7c
SH
917 if (dst_metric_rtt(dst, RTAX_RTT) > tp->srtt) {
918 tp->srtt = dst_metric_rtt(dst, RTAX_RTT);
1da177e4
LT
919 tp->rtt_seq = tp->snd_nxt;
920 }
c1e20f7c
SH
921 if (dst_metric_rtt(dst, RTAX_RTTVAR) > tp->mdev) {
922 tp->mdev = dst_metric_rtt(dst, RTAX_RTTVAR);
488faa2a 923 tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
1da177e4 924 }
463c84b9 925 tcp_set_rto(sk);
1da177e4 926reset:
9ad7c049
JC
927 if (tp->srtt == 0) {
928 /* RFC2988bis: We've failed to get a valid RTT sample from
929 * 3WHS. This is most likely due to retransmission,
930 * including spurious one. Reset the RTO back to 3secs
931 * from the more aggressive 1sec to avoid more spurious
932 * retransmission.
d9f4fbaf 933 */
9ad7c049
JC
934 tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_FALLBACK;
935 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
1da177e4 936 }
9ad7c049
JC
937 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
938 * retransmitted. In light of RFC2988bis' more aggressive 1sec
939 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
940 * retransmission has occurred.
941 */
942 if (tp->total_retrans > 1)
943 tp->snd_cwnd = 1;
944 else
945 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
d9f4fbaf 946 tp->snd_cwnd_stamp = tcp_time_stamp;
1da177e4
LT
947}
948
6687e988
ACM
949static void tcp_update_reordering(struct sock *sk, const int metric,
950 const int ts)
1da177e4 951{
6687e988 952 struct tcp_sock *tp = tcp_sk(sk);
1da177e4 953 if (metric > tp->reordering) {
40b215e5
PE
954 int mib_idx;
955
1da177e4
LT
956 tp->reordering = min(TCP_MAX_REORDERING, metric);
957
958 /* This exciting event is worth to be remembered. 8) */
959 if (ts)
40b215e5 960 mib_idx = LINUX_MIB_TCPTSREORDER;
e60402d0 961 else if (tcp_is_reno(tp))
40b215e5 962 mib_idx = LINUX_MIB_TCPRENOREORDER;
e60402d0 963 else if (tcp_is_fack(tp))
40b215e5 964 mib_idx = LINUX_MIB_TCPFACKREORDER;
1da177e4 965 else
40b215e5
PE
966 mib_idx = LINUX_MIB_TCPSACKREORDER;
967
de0744af 968 NET_INC_STATS_BH(sock_net(sk), mib_idx);
1da177e4
LT
969#if FASTRETRANS_DEBUG > 1
970 printk(KERN_DEBUG "Disorder%d %d %u f%u s%u rr%d\n",
6687e988 971 tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
1da177e4
LT
972 tp->reordering,
973 tp->fackets_out,
974 tp->sacked_out,
975 tp->undo_marker ? tp->undo_retrans : 0);
976#endif
e60402d0 977 tcp_disable_fack(tp);
1da177e4
LT
978 }
979}
980
006f582c 981/* This must be called before lost_out is incremented */
c8c213f2
IJ
982static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
983{
006f582c 984 if ((tp->retransmit_skb_hint == NULL) ||
c8c213f2
IJ
985 before(TCP_SKB_CB(skb)->seq,
986 TCP_SKB_CB(tp->retransmit_skb_hint)->seq))
006f582c
IJ
987 tp->retransmit_skb_hint = skb;
988
989 if (!tp->lost_out ||
990 after(TCP_SKB_CB(skb)->end_seq, tp->retransmit_high))
991 tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
c8c213f2
IJ
992}
993
41ea36e3
IJ
994static void tcp_skb_mark_lost(struct tcp_sock *tp, struct sk_buff *skb)
995{
996 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
997 tcp_verify_retransmit_hint(tp, skb);
998
999 tp->lost_out += tcp_skb_pcount(skb);
1000 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
1001 }
1002}
1003
e1aa680f
IJ
1004static void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp,
1005 struct sk_buff *skb)
006f582c
IJ
1006{
1007 tcp_verify_retransmit_hint(tp, skb);
1008
1009 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
1010 tp->lost_out += tcp_skb_pcount(skb);
1011 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
1012 }
1013}
1014
1da177e4
LT
1015/* This procedure tags the retransmission queue when SACKs arrive.
1016 *
1017 * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
1018 * Packets in queue with these bits set are counted in variables
1019 * sacked_out, retrans_out and lost_out, correspondingly.
1020 *
1021 * Valid combinations are:
1022 * Tag InFlight Description
1023 * 0 1 - orig segment is in flight.
1024 * S 0 - nothing flies, orig reached receiver.
1025 * L 0 - nothing flies, orig lost by net.
1026 * R 2 - both orig and retransmit are in flight.
1027 * L|R 1 - orig is lost, retransmit is in flight.
1028 * S|R 1 - orig reached receiver, retrans is still in flight.
1029 * (L|S|R is logically valid, it could occur when L|R is sacked,
1030 * but it is equivalent to plain S and code short-curcuits it to S.
1031 * L|S is logically invalid, it would mean -1 packet in flight 8))
1032 *
1033 * These 6 states form finite state machine, controlled by the following events:
1034 * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
1035 * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
1036 * 3. Loss detection event of one of three flavors:
1037 * A. Scoreboard estimator decided the packet is lost.
1038 * A'. Reno "three dupacks" marks head of queue lost.
1039 * A''. Its FACK modfication, head until snd.fack is lost.
1040 * B. SACK arrives sacking data transmitted after never retransmitted
1041 * hole was sent out.
1042 * C. SACK arrives sacking SND.NXT at the moment, when the
1043 * segment was retransmitted.
1044 * 4. D-SACK added new rule: D-SACK changes any tag to S.
1045 *
1046 * It is pleasant to note, that state diagram turns out to be commutative,
1047 * so that we are allowed not to be bothered by order of our actions,
1048 * when multiple events arrive simultaneously. (see the function below).
1049 *
1050 * Reordering detection.
1051 * --------------------
1052 * Reordering metric is maximal distance, which a packet can be displaced
1053 * in packet stream. With SACKs we can estimate it:
1054 *
1055 * 1. SACK fills old hole and the corresponding segment was not
1056 * ever retransmitted -> reordering. Alas, we cannot use it
1057 * when segment was retransmitted.
1058 * 2. The last flaw is solved with D-SACK. D-SACK arrives
1059 * for retransmitted and already SACKed segment -> reordering..
1060 * Both of these heuristics are not used in Loss state, when we cannot
1061 * account for retransmits accurately.
5b3c9882
IJ
1062 *
1063 * SACK block validation.
1064 * ----------------------
1065 *
1066 * SACK block range validation checks that the received SACK block fits to
1067 * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT.
1068 * Note that SND.UNA is not included to the range though being valid because
0e835331
IJ
1069 * it means that the receiver is rather inconsistent with itself reporting
1070 * SACK reneging when it should advance SND.UNA. Such SACK block this is
1071 * perfectly valid, however, in light of RFC2018 which explicitly states
1072 * that "SACK block MUST reflect the newest segment. Even if the newest
1073 * segment is going to be discarded ...", not that it looks very clever
1074 * in case of head skb. Due to potentional receiver driven attacks, we
1075 * choose to avoid immediate execution of a walk in write queue due to
1076 * reneging and defer head skb's loss recovery to standard loss recovery
1077 * procedure that will eventually trigger (nothing forbids us doing this).
5b3c9882
IJ
1078 *
1079 * Implements also blockage to start_seq wrap-around. Problem lies in the
1080 * fact that though start_seq (s) is before end_seq (i.e., not reversed),
1081 * there's no guarantee that it will be before snd_nxt (n). The problem
1082 * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt
1083 * wrap (s_w):
1084 *
1085 * <- outs wnd -> <- wrapzone ->
1086 * u e n u_w e_w s n_w
1087 * | | | | | | |
1088 * |<------------+------+----- TCP seqno space --------------+---------->|
1089 * ...-- <2^31 ->| |<--------...
1090 * ...---- >2^31 ------>| |<--------...
1091 *
1092 * Current code wouldn't be vulnerable but it's better still to discard such
1093 * crazy SACK blocks. Doing this check for start_seq alone closes somewhat
1094 * similar case (end_seq after snd_nxt wrap) as earlier reversed check in
1095 * snd_nxt wrap -> snd_una region will then become "well defined", i.e.,
1096 * equal to the ideal case (infinite seqno space without wrap caused issues).
1097 *
1098 * With D-SACK the lower bound is extended to cover sequence space below
1099 * SND.UNA down to undo_marker, which is the last point of interest. Yet
564262c1 1100 * again, D-SACK block must not to go across snd_una (for the same reason as
5b3c9882
IJ
1101 * for the normal SACK blocks, explained above). But there all simplicity
1102 * ends, TCP might receive valid D-SACKs below that. As long as they reside
1103 * fully below undo_marker they do not affect behavior in anyway and can
1104 * therefore be safely ignored. In rare cases (which are more or less
1105 * theoretical ones), the D-SACK will nicely cross that boundary due to skb
1106 * fragmentation and packet reordering past skb's retransmission. To consider
1107 * them correctly, the acceptable range must be extended even more though
1108 * the exact amount is rather hard to quantify. However, tp->max_window can
1109 * be used as an exaggerated estimate.
1da177e4 1110 */
5b3c9882
IJ
1111static int tcp_is_sackblock_valid(struct tcp_sock *tp, int is_dsack,
1112 u32 start_seq, u32 end_seq)
1113{
1114 /* Too far in future, or reversed (interpretation is ambiguous) */
1115 if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq))
1116 return 0;
1117
1118 /* Nasty start_seq wrap-around check (see comments above) */
1119 if (!before(start_seq, tp->snd_nxt))
1120 return 0;
1121
564262c1 1122 /* In outstanding window? ...This is valid exit for D-SACKs too.
5b3c9882
IJ
1123 * start_seq == snd_una is non-sensical (see comments above)
1124 */
1125 if (after(start_seq, tp->snd_una))
1126 return 1;
1127
1128 if (!is_dsack || !tp->undo_marker)
1129 return 0;
1130
1131 /* ...Then it's D-SACK, and must reside below snd_una completely */
f779b2d6 1132 if (after(end_seq, tp->snd_una))
5b3c9882
IJ
1133 return 0;
1134
1135 if (!before(start_seq, tp->undo_marker))
1136 return 1;
1137
1138 /* Too old */
1139 if (!after(end_seq, tp->undo_marker))
1140 return 0;
1141
1142 /* Undo_marker boundary crossing (overestimates a lot). Known already:
1143 * start_seq < undo_marker and end_seq >= undo_marker.
1144 */
1145 return !before(start_seq, end_seq - tp->max_window);
1146}
1147
1c1e87ed
IJ
1148/* Check for lost retransmit. This superb idea is borrowed from "ratehalving".
1149 * Event "C". Later note: FACK people cheated me again 8), we have to account
1150 * for reordering! Ugly, but should help.
f785a8e2
IJ
1151 *
1152 * Search retransmitted skbs from write_queue that were sent when snd_nxt was
1153 * less than what is now known to be received by the other end (derived from
9f58f3b7
IJ
1154 * highest SACK block). Also calculate the lowest snd_nxt among the remaining
1155 * retransmitted skbs to avoid some costly processing per ACKs.
1c1e87ed 1156 */
407ef1de 1157static void tcp_mark_lost_retrans(struct sock *sk)
1c1e87ed 1158{
9f58f3b7 1159 const struct inet_connection_sock *icsk = inet_csk(sk);
1c1e87ed
IJ
1160 struct tcp_sock *tp = tcp_sk(sk);
1161 struct sk_buff *skb;
f785a8e2 1162 int cnt = 0;
df2e014b 1163 u32 new_low_seq = tp->snd_nxt;
6859d494 1164 u32 received_upto = tcp_highest_sack_seq(tp);
9f58f3b7
IJ
1165
1166 if (!tcp_is_fack(tp) || !tp->retrans_out ||
1167 !after(received_upto, tp->lost_retrans_low) ||
1168 icsk->icsk_ca_state != TCP_CA_Recovery)
407ef1de 1169 return;
1c1e87ed
IJ
1170
1171 tcp_for_write_queue(skb, sk) {
1172 u32 ack_seq = TCP_SKB_CB(skb)->ack_seq;
1173
1174 if (skb == tcp_send_head(sk))
1175 break;
f785a8e2 1176 if (cnt == tp->retrans_out)
1c1e87ed
IJ
1177 break;
1178 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1179 continue;
1180
f785a8e2
IJ
1181 if (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS))
1182 continue;
1183
d0af4160
IJ
1184 /* TODO: We would like to get rid of tcp_is_fack(tp) only
1185 * constraint here (see above) but figuring out that at
1186 * least tp->reordering SACK blocks reside between ack_seq
1187 * and received_upto is not easy task to do cheaply with
1188 * the available datastructures.
1189 *
1190 * Whether FACK should check here for tp->reordering segs
1191 * in-between one could argue for either way (it would be
1192 * rather simple to implement as we could count fack_count
1193 * during the walk and do tp->fackets_out - fack_count).
1194 */
1195 if (after(received_upto, ack_seq)) {
1c1e87ed
IJ
1196 TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
1197 tp->retrans_out -= tcp_skb_pcount(skb);
1198
006f582c 1199 tcp_skb_mark_lost_uncond_verify(tp, skb);
de0744af 1200 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSTRETRANSMIT);
f785a8e2 1201 } else {
df2e014b 1202 if (before(ack_seq, new_low_seq))
b08d6cb2 1203 new_low_seq = ack_seq;
f785a8e2 1204 cnt += tcp_skb_pcount(skb);
1c1e87ed
IJ
1205 }
1206 }
b08d6cb2
IJ
1207
1208 if (tp->retrans_out)
1209 tp->lost_retrans_low = new_low_seq;
1c1e87ed 1210}
5b3c9882 1211
1ed83465 1212static int tcp_check_dsack(struct sock *sk, struct sk_buff *ack_skb,
d06e021d
DM
1213 struct tcp_sack_block_wire *sp, int num_sacks,
1214 u32 prior_snd_una)
1215{
1ed83465 1216 struct tcp_sock *tp = tcp_sk(sk);
d3e2ce3b
HH
1217 u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq);
1218 u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq);
d06e021d
DM
1219 int dup_sack = 0;
1220
1221 if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) {
1222 dup_sack = 1;
e60402d0 1223 tcp_dsack_seen(tp);
de0744af 1224 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKRECV);
d06e021d 1225 } else if (num_sacks > 1) {
d3e2ce3b
HH
1226 u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq);
1227 u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq);
d06e021d
DM
1228
1229 if (!after(end_seq_0, end_seq_1) &&
1230 !before(start_seq_0, start_seq_1)) {
1231 dup_sack = 1;
e60402d0 1232 tcp_dsack_seen(tp);
de0744af
PE
1233 NET_INC_STATS_BH(sock_net(sk),
1234 LINUX_MIB_TCPDSACKOFORECV);
d06e021d
DM
1235 }
1236 }
1237
1238 /* D-SACK for already forgotten data... Do dumb counting. */
c24f691b 1239 if (dup_sack && tp->undo_marker && tp->undo_retrans &&
d06e021d
DM
1240 !after(end_seq_0, prior_snd_una) &&
1241 after(end_seq_0, tp->undo_marker))
1242 tp->undo_retrans--;
1243
1244 return dup_sack;
1245}
1246
a1197f5a
IJ
1247struct tcp_sacktag_state {
1248 int reord;
1249 int fack_count;
1250 int flag;
1251};
1252
d1935942
IJ
1253/* Check if skb is fully within the SACK block. In presence of GSO skbs,
1254 * the incoming SACK may not exactly match but we can find smaller MSS
1255 * aligned portion of it that matches. Therefore we might need to fragment
1256 * which may fail and creates some hassle (caller must handle error case
1257 * returns).
832d11c5
IJ
1258 *
1259 * FIXME: this could be merged to shift decision code
d1935942 1260 */
0f79efdc
AB
1261static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
1262 u32 start_seq, u32 end_seq)
d1935942
IJ
1263{
1264 int in_sack, err;
1265 unsigned int pkt_len;
adb92db8 1266 unsigned int mss;
d1935942
IJ
1267
1268 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1269 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1270
1271 if (tcp_skb_pcount(skb) > 1 && !in_sack &&
1272 after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
adb92db8 1273 mss = tcp_skb_mss(skb);
d1935942
IJ
1274 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1275
adb92db8 1276 if (!in_sack) {
d1935942 1277 pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
adb92db8
IJ
1278 if (pkt_len < mss)
1279 pkt_len = mss;
1280 } else {
d1935942 1281 pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
adb92db8
IJ
1282 if (pkt_len < mss)
1283 return -EINVAL;
1284 }
1285
1286 /* Round if necessary so that SACKs cover only full MSSes
1287 * and/or the remaining small portion (if present)
1288 */
1289 if (pkt_len > mss) {
1290 unsigned int new_len = (pkt_len / mss) * mss;
1291 if (!in_sack && new_len < pkt_len) {
1292 new_len += mss;
1293 if (new_len > skb->len)
1294 return 0;
1295 }
1296 pkt_len = new_len;
1297 }
1298 err = tcp_fragment(sk, skb, pkt_len, mss);
d1935942
IJ
1299 if (err < 0)
1300 return err;
1301 }
1302
1303 return in_sack;
1304}
1305
a1197f5a
IJ
1306static u8 tcp_sacktag_one(struct sk_buff *skb, struct sock *sk,
1307 struct tcp_sacktag_state *state,
1308 int dup_sack, int pcount)
9e10c47c 1309{
6859d494 1310 struct tcp_sock *tp = tcp_sk(sk);
9e10c47c 1311 u8 sacked = TCP_SKB_CB(skb)->sacked;
a1197f5a 1312 int fack_count = state->fack_count;
9e10c47c
IJ
1313
1314 /* Account D-SACK for retransmitted packet. */
1315 if (dup_sack && (sacked & TCPCB_RETRANS)) {
c24f691b
YC
1316 if (tp->undo_marker && tp->undo_retrans &&
1317 after(TCP_SKB_CB(skb)->end_seq, tp->undo_marker))
9e10c47c 1318 tp->undo_retrans--;
ede9f3b1 1319 if (sacked & TCPCB_SACKED_ACKED)
a1197f5a 1320 state->reord = min(fack_count, state->reord);
9e10c47c
IJ
1321 }
1322
1323 /* Nothing to do; acked frame is about to be dropped (was ACKed). */
1324 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
a1197f5a 1325 return sacked;
9e10c47c
IJ
1326
1327 if (!(sacked & TCPCB_SACKED_ACKED)) {
1328 if (sacked & TCPCB_SACKED_RETRANS) {
1329 /* If the segment is not tagged as lost,
1330 * we do not clear RETRANS, believing
1331 * that retransmission is still in flight.
1332 */
1333 if (sacked & TCPCB_LOST) {
a1197f5a 1334 sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
f58b22fd
IJ
1335 tp->lost_out -= pcount;
1336 tp->retrans_out -= pcount;
9e10c47c
IJ
1337 }
1338 } else {
1339 if (!(sacked & TCPCB_RETRANS)) {
1340 /* New sack for not retransmitted frame,
1341 * which was in hole. It is reordering.
1342 */
1343 if (before(TCP_SKB_CB(skb)->seq,
1344 tcp_highest_sack_seq(tp)))
a1197f5a
IJ
1345 state->reord = min(fack_count,
1346 state->reord);
9e10c47c
IJ
1347
1348 /* SACK enhanced F-RTO (RFC4138; Appendix B) */
1349 if (!after(TCP_SKB_CB(skb)->end_seq, tp->frto_highmark))
a1197f5a 1350 state->flag |= FLAG_ONLY_ORIG_SACKED;
9e10c47c
IJ
1351 }
1352
1353 if (sacked & TCPCB_LOST) {
a1197f5a 1354 sacked &= ~TCPCB_LOST;
f58b22fd 1355 tp->lost_out -= pcount;
9e10c47c
IJ
1356 }
1357 }
1358
a1197f5a
IJ
1359 sacked |= TCPCB_SACKED_ACKED;
1360 state->flag |= FLAG_DATA_SACKED;
f58b22fd 1361 tp->sacked_out += pcount;
9e10c47c 1362
f58b22fd 1363 fack_count += pcount;
9e10c47c
IJ
1364
1365 /* Lost marker hint past SACKed? Tweak RFC3517 cnt */
1366 if (!tcp_is_fack(tp) && (tp->lost_skb_hint != NULL) &&
1367 before(TCP_SKB_CB(skb)->seq,
1368 TCP_SKB_CB(tp->lost_skb_hint)->seq))
f58b22fd 1369 tp->lost_cnt_hint += pcount;
9e10c47c
IJ
1370
1371 if (fack_count > tp->fackets_out)
1372 tp->fackets_out = fack_count;
9e10c47c
IJ
1373 }
1374
1375 /* D-SACK. We can detect redundant retransmission in S|R and plain R
1376 * frames and clear it. undo_retrans is decreased above, L|R frames
1377 * are accounted above as well.
1378 */
a1197f5a
IJ
1379 if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
1380 sacked &= ~TCPCB_SACKED_RETRANS;
f58b22fd 1381 tp->retrans_out -= pcount;
9e10c47c
IJ
1382 }
1383
a1197f5a 1384 return sacked;
9e10c47c
IJ
1385}
1386
50133161 1387static int tcp_shifted_skb(struct sock *sk, struct sk_buff *skb,
a1197f5a 1388 struct tcp_sacktag_state *state,
9ec06ff5
IJ
1389 unsigned int pcount, int shifted, int mss,
1390 int dup_sack)
832d11c5
IJ
1391{
1392 struct tcp_sock *tp = tcp_sk(sk);
50133161 1393 struct sk_buff *prev = tcp_write_queue_prev(sk, skb);
832d11c5
IJ
1394
1395 BUG_ON(!pcount);
1396
1e5289e1 1397 if (skb == tp->lost_skb_hint)
92ee76b6
IJ
1398 tp->lost_cnt_hint += pcount;
1399
832d11c5
IJ
1400 TCP_SKB_CB(prev)->end_seq += shifted;
1401 TCP_SKB_CB(skb)->seq += shifted;
1402
1403 skb_shinfo(prev)->gso_segs += pcount;
1404 BUG_ON(skb_shinfo(skb)->gso_segs < pcount);
1405 skb_shinfo(skb)->gso_segs -= pcount;
1406
1407 /* When we're adding to gso_segs == 1, gso_size will be zero,
1408 * in theory this shouldn't be necessary but as long as DSACK
1409 * code can come after this skb later on it's better to keep
1410 * setting gso_size to something.
1411 */
1412 if (!skb_shinfo(prev)->gso_size) {
1413 skb_shinfo(prev)->gso_size = mss;
1414 skb_shinfo(prev)->gso_type = sk->sk_gso_type;
1415 }
1416
1417 /* CHECKME: To clear or not to clear? Mimics normal skb currently */
1418 if (skb_shinfo(skb)->gso_segs <= 1) {
1419 skb_shinfo(skb)->gso_size = 0;
1420 skb_shinfo(skb)->gso_type = 0;
1421 }
1422
a1197f5a 1423 /* We discard results */
9ec06ff5 1424 tcp_sacktag_one(skb, sk, state, dup_sack, pcount);
832d11c5
IJ
1425
1426 /* Difference in this won't matter, both ACKed by the same cumul. ACK */
1427 TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
1428
832d11c5
IJ
1429 if (skb->len > 0) {
1430 BUG_ON(!tcp_skb_pcount(skb));
111cc8b9 1431 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTED);
832d11c5
IJ
1432 return 0;
1433 }
1434
1435 /* Whole SKB was eaten :-) */
1436
92ee76b6
IJ
1437 if (skb == tp->retransmit_skb_hint)
1438 tp->retransmit_skb_hint = prev;
1439 if (skb == tp->scoreboard_skb_hint)
1440 tp->scoreboard_skb_hint = prev;
1441 if (skb == tp->lost_skb_hint) {
1442 tp->lost_skb_hint = prev;
1443 tp->lost_cnt_hint -= tcp_skb_pcount(prev);
1444 }
1445
4de075e0 1446 TCP_SKB_CB(skb)->tcp_flags |= TCP_SKB_CB(prev)->tcp_flags;
832d11c5
IJ
1447 if (skb == tcp_highest_sack(sk))
1448 tcp_advance_highest_sack(sk, skb);
1449
1450 tcp_unlink_write_queue(skb, sk);
1451 sk_wmem_free_skb(sk, skb);
1452
111cc8b9
IJ
1453 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKMERGED);
1454
832d11c5
IJ
1455 return 1;
1456}
1457
1458/* I wish gso_size would have a bit more sane initialization than
1459 * something-or-zero which complicates things
1460 */
775ffabf 1461static int tcp_skb_seglen(struct sk_buff *skb)
832d11c5 1462{
775ffabf 1463 return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
832d11c5
IJ
1464}
1465
1466/* Shifting pages past head area doesn't work */
1467static int skb_can_shift(struct sk_buff *skb)
1468{
1469 return !skb_headlen(skb) && skb_is_nonlinear(skb);
1470}
1471
1472/* Try collapsing SACK blocks spanning across multiple skbs to a single
1473 * skb.
1474 */
1475static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
a1197f5a 1476 struct tcp_sacktag_state *state,
832d11c5 1477 u32 start_seq, u32 end_seq,
a1197f5a 1478 int dup_sack)
832d11c5
IJ
1479{
1480 struct tcp_sock *tp = tcp_sk(sk);
1481 struct sk_buff *prev;
1482 int mss;
1483 int pcount = 0;
1484 int len;
1485 int in_sack;
1486
1487 if (!sk_can_gso(sk))
1488 goto fallback;
1489
1490 /* Normally R but no L won't result in plain S */
1491 if (!dup_sack &&
9969ca5f 1492 (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
832d11c5
IJ
1493 goto fallback;
1494 if (!skb_can_shift(skb))
1495 goto fallback;
1496 /* This frame is about to be dropped (was ACKed). */
1497 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1498 goto fallback;
1499
1500 /* Can only happen with delayed DSACK + discard craziness */
1501 if (unlikely(skb == tcp_write_queue_head(sk)))
1502 goto fallback;
1503 prev = tcp_write_queue_prev(sk, skb);
1504
1505 if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
1506 goto fallback;
1507
1508 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1509 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1510
1511 if (in_sack) {
1512 len = skb->len;
1513 pcount = tcp_skb_pcount(skb);
775ffabf 1514 mss = tcp_skb_seglen(skb);
832d11c5
IJ
1515
1516 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1517 * drop this restriction as unnecessary
1518 */
775ffabf 1519 if (mss != tcp_skb_seglen(prev))
832d11c5
IJ
1520 goto fallback;
1521 } else {
1522 if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
1523 goto noop;
1524 /* CHECKME: This is non-MSS split case only?, this will
1525 * cause skipped skbs due to advancing loop btw, original
1526 * has that feature too
1527 */
1528 if (tcp_skb_pcount(skb) <= 1)
1529 goto noop;
1530
1531 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1532 if (!in_sack) {
1533 /* TODO: head merge to next could be attempted here
1534 * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
1535 * though it might not be worth of the additional hassle
1536 *
1537 * ...we can probably just fallback to what was done
1538 * previously. We could try merging non-SACKed ones
1539 * as well but it probably isn't going to buy off
1540 * because later SACKs might again split them, and
1541 * it would make skb timestamp tracking considerably
1542 * harder problem.
1543 */
1544 goto fallback;
1545 }
1546
1547 len = end_seq - TCP_SKB_CB(skb)->seq;
1548 BUG_ON(len < 0);
1549 BUG_ON(len > skb->len);
1550
1551 /* MSS boundaries should be honoured or else pcount will
1552 * severely break even though it makes things bit trickier.
1553 * Optimize common case to avoid most of the divides
1554 */
1555 mss = tcp_skb_mss(skb);
1556
1557 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1558 * drop this restriction as unnecessary
1559 */
775ffabf 1560 if (mss != tcp_skb_seglen(prev))
832d11c5
IJ
1561 goto fallback;
1562
1563 if (len == mss) {
1564 pcount = 1;
1565 } else if (len < mss) {
1566 goto noop;
1567 } else {
1568 pcount = len / mss;
1569 len = pcount * mss;
1570 }
1571 }
1572
1573 if (!skb_shift(prev, skb, len))
1574 goto fallback;
9ec06ff5 1575 if (!tcp_shifted_skb(sk, skb, state, pcount, len, mss, dup_sack))
832d11c5
IJ
1576 goto out;
1577
1578 /* Hole filled allows collapsing with the next as well, this is very
1579 * useful when hole on every nth skb pattern happens
1580 */
1581 if (prev == tcp_write_queue_tail(sk))
1582 goto out;
1583 skb = tcp_write_queue_next(sk, prev);
1584
f0bc52f3
IJ
1585 if (!skb_can_shift(skb) ||
1586 (skb == tcp_send_head(sk)) ||
1587 ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
775ffabf 1588 (mss != tcp_skb_seglen(skb)))
832d11c5
IJ
1589 goto out;
1590
1591 len = skb->len;
1592 if (skb_shift(prev, skb, len)) {
1593 pcount += tcp_skb_pcount(skb);
9ec06ff5 1594 tcp_shifted_skb(sk, skb, state, tcp_skb_pcount(skb), len, mss, 0);
832d11c5
IJ
1595 }
1596
1597out:
a1197f5a 1598 state->fack_count += pcount;
832d11c5
IJ
1599 return prev;
1600
1601noop:
1602 return skb;
1603
1604fallback:
111cc8b9 1605 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
832d11c5
IJ
1606 return NULL;
1607}
1608
68f8353b
IJ
1609static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
1610 struct tcp_sack_block *next_dup,
a1197f5a 1611 struct tcp_sacktag_state *state,
68f8353b 1612 u32 start_seq, u32 end_seq,
a1197f5a 1613 int dup_sack_in)
68f8353b 1614{
832d11c5
IJ
1615 struct tcp_sock *tp = tcp_sk(sk);
1616 struct sk_buff *tmp;
1617
68f8353b
IJ
1618 tcp_for_write_queue_from(skb, sk) {
1619 int in_sack = 0;
1620 int dup_sack = dup_sack_in;
1621
1622 if (skb == tcp_send_head(sk))
1623 break;
1624
1625 /* queue is in-order => we can short-circuit the walk early */
1626 if (!before(TCP_SKB_CB(skb)->seq, end_seq))
1627 break;
1628
1629 if ((next_dup != NULL) &&
1630 before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
1631 in_sack = tcp_match_skb_to_sack(sk, skb,
1632 next_dup->start_seq,
1633 next_dup->end_seq);
1634 if (in_sack > 0)
1635 dup_sack = 1;
1636 }
1637
832d11c5
IJ
1638 /* skb reference here is a bit tricky to get right, since
1639 * shifting can eat and free both this skb and the next,
1640 * so not even _safe variant of the loop is enough.
1641 */
1642 if (in_sack <= 0) {
a1197f5a
IJ
1643 tmp = tcp_shift_skb_data(sk, skb, state,
1644 start_seq, end_seq, dup_sack);
832d11c5
IJ
1645 if (tmp != NULL) {
1646 if (tmp != skb) {
1647 skb = tmp;
1648 continue;
1649 }
1650
1651 in_sack = 0;
1652 } else {
1653 in_sack = tcp_match_skb_to_sack(sk, skb,
1654 start_seq,
1655 end_seq);
1656 }
1657 }
1658
68f8353b
IJ
1659 if (unlikely(in_sack < 0))
1660 break;
1661
832d11c5 1662 if (in_sack) {
a1197f5a
IJ
1663 TCP_SKB_CB(skb)->sacked = tcp_sacktag_one(skb, sk,
1664 state,
1665 dup_sack,
1666 tcp_skb_pcount(skb));
68f8353b 1667
832d11c5
IJ
1668 if (!before(TCP_SKB_CB(skb)->seq,
1669 tcp_highest_sack_seq(tp)))
1670 tcp_advance_highest_sack(sk, skb);
1671 }
1672
a1197f5a 1673 state->fack_count += tcp_skb_pcount(skb);
68f8353b
IJ
1674 }
1675 return skb;
1676}
1677
1678/* Avoid all extra work that is being done by sacktag while walking in
1679 * a normal way
1680 */
1681static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk,
a1197f5a
IJ
1682 struct tcp_sacktag_state *state,
1683 u32 skip_to_seq)
68f8353b
IJ
1684{
1685 tcp_for_write_queue_from(skb, sk) {
1686 if (skb == tcp_send_head(sk))
1687 break;
1688
e8bae275 1689 if (after(TCP_SKB_CB(skb)->end_seq, skip_to_seq))
68f8353b 1690 break;
d152a7d8 1691
a1197f5a 1692 state->fack_count += tcp_skb_pcount(skb);
68f8353b
IJ
1693 }
1694 return skb;
1695}
1696
1697static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
1698 struct sock *sk,
1699 struct tcp_sack_block *next_dup,
a1197f5a
IJ
1700 struct tcp_sacktag_state *state,
1701 u32 skip_to_seq)
68f8353b
IJ
1702{
1703 if (next_dup == NULL)
1704 return skb;
1705
1706 if (before(next_dup->start_seq, skip_to_seq)) {
a1197f5a
IJ
1707 skb = tcp_sacktag_skip(skb, sk, state, next_dup->start_seq);
1708 skb = tcp_sacktag_walk(skb, sk, NULL, state,
1709 next_dup->start_seq, next_dup->end_seq,
1710 1);
68f8353b
IJ
1711 }
1712
1713 return skb;
1714}
1715
1716static int tcp_sack_cache_ok(struct tcp_sock *tp, struct tcp_sack_block *cache)
1717{
1718 return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
1719}
1720
1da177e4 1721static int
056834d9
IJ
1722tcp_sacktag_write_queue(struct sock *sk, struct sk_buff *ack_skb,
1723 u32 prior_snd_una)
1da177e4 1724{
6687e988 1725 const struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 1726 struct tcp_sock *tp = tcp_sk(sk);
9c70220b
ACM
1727 unsigned char *ptr = (skb_transport_header(ack_skb) +
1728 TCP_SKB_CB(ack_skb)->sacked);
fd6dad61 1729 struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
4389dded 1730 struct tcp_sack_block sp[TCP_NUM_SACKS];
68f8353b 1731 struct tcp_sack_block *cache;
a1197f5a 1732 struct tcp_sacktag_state state;
68f8353b 1733 struct sk_buff *skb;
4389dded 1734 int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
fd6dad61 1735 int used_sacks;
7769f406 1736 int found_dup_sack = 0;
68f8353b 1737 int i, j;
fda03fbb 1738 int first_sack_index;
1da177e4 1739
a1197f5a
IJ
1740 state.flag = 0;
1741 state.reord = tp->packets_out;
1742
d738cd8f 1743 if (!tp->sacked_out) {
de83c058
IJ
1744 if (WARN_ON(tp->fackets_out))
1745 tp->fackets_out = 0;
6859d494 1746 tcp_highest_sack_reset(sk);
d738cd8f 1747 }
1da177e4 1748
1ed83465 1749 found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
d06e021d
DM
1750 num_sacks, prior_snd_una);
1751 if (found_dup_sack)
a1197f5a 1752 state.flag |= FLAG_DSACKING_ACK;
6f74651a
BE
1753
1754 /* Eliminate too old ACKs, but take into
1755 * account more or less fresh ones, they can
1756 * contain valid SACK info.
1757 */
1758 if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
1759 return 0;
1760