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