]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/ipv4/tcp_input.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[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 */
1da177e4 108#define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/
4dc2665e 109#define FLAG_ONLY_ORIG_SACKED 0x200 /* SACKs only non-rexmit sent before RTO */
2e605294 110#define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */
564262c1 111#define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */
009a2e3e 112#define FLAG_NONHEAD_RETRANS_ACKED 0x1000 /* Non-head rexmitted data was ACKed */
cadbd031 113#define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */
1da177e4
LT
114
115#define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED)
116#define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
117#define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE)
118#define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED)
2e605294 119#define FLAG_ANY_PROGRESS (FLAG_FORWARD_PROGRESS|FLAG_SND_UNA_ADVANCED)
1da177e4 120
1da177e4 121#define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
bdf1ee5d 122#define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
1da177e4 123
e905a9ed 124/* Adapt the MSS value used to make delayed ack decision to the
1da177e4 125 * real world.
e905a9ed 126 */
056834d9 127static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb)
1da177e4 128{
463c84b9 129 struct inet_connection_sock *icsk = inet_csk(sk);
e905a9ed 130 const unsigned int lss = icsk->icsk_ack.last_seg_size;
463c84b9 131 unsigned int len;
1da177e4 132
e905a9ed 133 icsk->icsk_ack.last_seg_size = 0;
1da177e4
LT
134
135 /* skb->len may jitter because of SACKs, even if peer
136 * sends good full-sized frames.
137 */
056834d9 138 len = skb_shinfo(skb)->gso_size ? : skb->len;
463c84b9
ACM
139 if (len >= icsk->icsk_ack.rcv_mss) {
140 icsk->icsk_ack.rcv_mss = len;
1da177e4
LT
141 } else {
142 /* Otherwise, we make more careful check taking into account,
143 * that SACKs block is variable.
144 *
145 * "len" is invariant segment length, including TCP header.
146 */
9c70220b 147 len += skb->data - skb_transport_header(skb);
bee7ca9e 148 if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) ||
1da177e4
LT
149 /* If PSH is not set, packet should be
150 * full sized, provided peer TCP is not badly broken.
151 * This observation (if it is correct 8)) allows
152 * to handle super-low mtu links fairly.
153 */
154 (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
aa8223c7 155 !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {
1da177e4
LT
156 /* Subtract also invariant (if peer is RFC compliant),
157 * tcp header plus fixed timestamp option length.
158 * Resulting "len" is MSS free of SACK jitter.
159 */
463c84b9
ACM
160 len -= tcp_sk(sk)->tcp_header_len;
161 icsk->icsk_ack.last_seg_size = len;
1da177e4 162 if (len == lss) {
463c84b9 163 icsk->icsk_ack.rcv_mss = len;
1da177e4
LT
164 return;
165 }
166 }
1ef9696c
AK
167 if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)
168 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;
463c84b9 169 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
1da177e4
LT
170 }
171}
172
463c84b9 173static void tcp_incr_quickack(struct sock *sk)
1da177e4 174{
463c84b9
ACM
175 struct inet_connection_sock *icsk = inet_csk(sk);
176 unsigned quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);
1da177e4 177
056834d9
IJ
178 if (quickacks == 0)
179 quickacks = 2;
463c84b9
ACM
180 if (quickacks > icsk->icsk_ack.quick)
181 icsk->icsk_ack.quick = min(quickacks, TCP_MAX_QUICKACKS);
1da177e4
LT
182}
183
1b9f4092 184static void tcp_enter_quickack_mode(struct sock *sk)
1da177e4 185{
463c84b9
ACM
186 struct inet_connection_sock *icsk = inet_csk(sk);
187 tcp_incr_quickack(sk);
188 icsk->icsk_ack.pingpong = 0;
189 icsk->icsk_ack.ato = TCP_ATO_MIN;
1da177e4
LT
190}
191
192/* Send ACKs quickly, if "quick" count is not exhausted
193 * and the session is not interactive.
194 */
195
463c84b9 196static inline int tcp_in_quickack_mode(const struct sock *sk)
1da177e4 197{
463c84b9
ACM
198 const struct inet_connection_sock *icsk = inet_csk(sk);
199 return icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong;
1da177e4
LT
200}
201
bdf1ee5d
IJ
202static inline void TCP_ECN_queue_cwr(struct tcp_sock *tp)
203{
056834d9 204 if (tp->ecn_flags & TCP_ECN_OK)
bdf1ee5d
IJ
205 tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
206}
207
cf533ea5 208static inline void TCP_ECN_accept_cwr(struct tcp_sock *tp, const struct sk_buff *skb)
bdf1ee5d
IJ
209{
210 if (tcp_hdr(skb)->cwr)
211 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
212}
213
214static inline void TCP_ECN_withdraw_cwr(struct tcp_sock *tp)
215{
216 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
217}
218
7a269ffa 219static inline void TCP_ECN_check_ce(struct tcp_sock *tp, const struct sk_buff *skb)
bdf1ee5d 220{
7a269ffa
ED
221 if (!(tp->ecn_flags & TCP_ECN_OK))
222 return;
223
b82d1bb4 224 switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) {
7a269ffa 225 case INET_ECN_NOT_ECT:
bdf1ee5d 226 /* Funny extension: if ECT is not set on a segment,
7a269ffa
ED
227 * and we already seen ECT on a previous segment,
228 * it is probably a retransmit.
229 */
230 if (tp->ecn_flags & TCP_ECN_SEEN)
bdf1ee5d 231 tcp_enter_quickack_mode((struct sock *)tp);
7a269ffa
ED
232 break;
233 case INET_ECN_CE:
234 tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
235 /* fallinto */
236 default:
237 tp->ecn_flags |= TCP_ECN_SEEN;
bdf1ee5d
IJ
238 }
239}
240
cf533ea5 241static inline void TCP_ECN_rcv_synack(struct tcp_sock *tp, const struct tcphdr *th)
bdf1ee5d 242{
056834d9 243 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr))
bdf1ee5d
IJ
244 tp->ecn_flags &= ~TCP_ECN_OK;
245}
246
cf533ea5 247static inline void TCP_ECN_rcv_syn(struct tcp_sock *tp, const struct tcphdr *th)
bdf1ee5d 248{
056834d9 249 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr))
bdf1ee5d
IJ
250 tp->ecn_flags &= ~TCP_ECN_OK;
251}
252
cf533ea5 253static inline int TCP_ECN_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr *th)
bdf1ee5d 254{
056834d9 255 if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK))
bdf1ee5d
IJ
256 return 1;
257 return 0;
258}
259
1da177e4
LT
260/* Buffer size and advertised window tuning.
261 *
262 * 1. Tuning sk->sk_sndbuf, when connection enters established state.
263 */
264
265static void tcp_fixup_sndbuf(struct sock *sk)
266{
87fb4b7b 267 int sndmem = SKB_TRUESIZE(tcp_sk(sk)->rx_opt.mss_clamp + MAX_TCP_HEADER);
1da177e4 268
06a59ecb
ED
269 sndmem *= TCP_INIT_CWND;
270 if (sk->sk_sndbuf < sndmem)
271 sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);
1da177e4
LT
272}
273
274/* 2. Tuning advertised window (window_clamp, rcv_ssthresh)
275 *
276 * All tcp_full_space() is split to two parts: "network" buffer, allocated
277 * forward and advertised in receiver window (tp->rcv_wnd) and
278 * "application buffer", required to isolate scheduling/application
279 * latencies from network.
280 * window_clamp is maximal advertised window. It can be less than
281 * tcp_full_space(), in this case tcp_full_space() - window_clamp
282 * is reserved for "application" buffer. The less window_clamp is
283 * the smoother our behaviour from viewpoint of network, but the lower
284 * throughput and the higher sensitivity of the connection to losses. 8)
285 *
286 * rcv_ssthresh is more strict window_clamp used at "slow start"
287 * phase to predict further behaviour of this connection.
288 * It is used for two goals:
289 * - to enforce header prediction at sender, even when application
290 * requires some significant "application buffer". It is check #1.
291 * - to prevent pruning of receive queue because of misprediction
292 * of receiver window. Check #2.
293 *
294 * The scheme does not work when sender sends good segments opening
caa20d9a 295 * window and then starts to feed us spaghetti. But it should work
1da177e4
LT
296 * in common situations. Otherwise, we have to rely on queue collapsing.
297 */
298
299/* Slow part of check#2. */
9e412ba7 300static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb)
1da177e4 301{
9e412ba7 302 struct tcp_sock *tp = tcp_sk(sk);
1da177e4 303 /* Optimize this! */
dfd4f0ae
ED
304 int truesize = tcp_win_from_space(skb->truesize) >> 1;
305 int window = tcp_win_from_space(sysctl_tcp_rmem[2]) >> 1;
1da177e4
LT
306
307 while (tp->rcv_ssthresh <= window) {
308 if (truesize <= skb->len)
463c84b9 309 return 2 * inet_csk(sk)->icsk_ack.rcv_mss;
1da177e4
LT
310
311 truesize >>= 1;
312 window >>= 1;
313 }
314 return 0;
315}
316
cf533ea5 317static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb)
1da177e4 318{
9e412ba7
IJ
319 struct tcp_sock *tp = tcp_sk(sk);
320
1da177e4
LT
321 /* Check #1 */
322 if (tp->rcv_ssthresh < tp->window_clamp &&
323 (int)tp->rcv_ssthresh < tcp_space(sk) &&
180d8cd9 324 !sk_under_memory_pressure(sk)) {
1da177e4
LT
325 int incr;
326
327 /* Check #2. Increase window, if skb with such overhead
328 * will fit to rcvbuf in future.
329 */
330 if (tcp_win_from_space(skb->truesize) <= skb->len)
056834d9 331 incr = 2 * tp->advmss;
1da177e4 332 else
9e412ba7 333 incr = __tcp_grow_window(sk, skb);
1da177e4
LT
334
335 if (incr) {
056834d9
IJ
336 tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr,
337 tp->window_clamp);
463c84b9 338 inet_csk(sk)->icsk_ack.quick |= 1;
1da177e4
LT
339 }
340 }
341}
342
343/* 3. Tuning rcvbuf, when connection enters established state. */
344
345static void tcp_fixup_rcvbuf(struct sock *sk)
346{
e9266a02
ED
347 u32 mss = tcp_sk(sk)->advmss;
348 u32 icwnd = TCP_DEFAULT_INIT_RCVWND;
349 int rcvmem;
1da177e4 350
e9266a02
ED
351 /* Limit to 10 segments if mss <= 1460,
352 * or 14600/mss segments, with a minimum of two segments.
1da177e4 353 */
e9266a02
ED
354 if (mss > 1460)
355 icwnd = max_t(u32, (1460 * TCP_DEFAULT_INIT_RCVWND) / mss, 2);
356
357 rcvmem = SKB_TRUESIZE(mss + MAX_TCP_HEADER);
358 while (tcp_win_from_space(rcvmem) < mss)
1da177e4 359 rcvmem += 128;
e9266a02
ED
360
361 rcvmem *= icwnd;
362
363 if (sk->sk_rcvbuf < rcvmem)
364 sk->sk_rcvbuf = min(rcvmem, sysctl_tcp_rmem[2]);
1da177e4
LT
365}
366
caa20d9a 367/* 4. Try to fixup all. It is made immediately after connection enters
1da177e4
LT
368 * established state.
369 */
370static void tcp_init_buffer_space(struct sock *sk)
371{
372 struct tcp_sock *tp = tcp_sk(sk);
373 int maxwin;
374
375 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
376 tcp_fixup_rcvbuf(sk);
377 if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
378 tcp_fixup_sndbuf(sk);
379
380 tp->rcvq_space.space = tp->rcv_wnd;
381
382 maxwin = tcp_full_space(sk);
383
384 if (tp->window_clamp >= maxwin) {
385 tp->window_clamp = maxwin;
386
387 if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss)
388 tp->window_clamp = max(maxwin -
389 (maxwin >> sysctl_tcp_app_win),
390 4 * tp->advmss);
391 }
392
393 /* Force reservation of one segment. */
394 if (sysctl_tcp_app_win &&
395 tp->window_clamp > 2 * tp->advmss &&
396 tp->window_clamp + tp->advmss > maxwin)
397 tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss);
398
399 tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
400 tp->snd_cwnd_stamp = tcp_time_stamp;
401}
402
1da177e4 403/* 5. Recalculate window clamp after socket hit its memory bounds. */
9e412ba7 404static void tcp_clamp_window(struct sock *sk)
1da177e4 405{
9e412ba7 406 struct tcp_sock *tp = tcp_sk(sk);
6687e988 407 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 408
6687e988 409 icsk->icsk_ack.quick = 0;
1da177e4 410
326f36e9
JH
411 if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] &&
412 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
180d8cd9
GC
413 !sk_under_memory_pressure(sk) &&
414 sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)) {
326f36e9
JH
415 sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),
416 sysctl_tcp_rmem[2]);
1da177e4 417 }
326f36e9 418 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
056834d9 419 tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss);
1da177e4
LT
420}
421
40efc6fa
SH
422/* Initialize RCV_MSS value.
423 * RCV_MSS is an our guess about MSS used by the peer.
424 * We haven't any direct information about the MSS.
425 * It's better to underestimate the RCV_MSS rather than overestimate.
426 * Overestimations make us ACKing less frequently than needed.
427 * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
428 */
429void tcp_initialize_rcv_mss(struct sock *sk)
430{
cf533ea5 431 const struct tcp_sock *tp = tcp_sk(sk);
40efc6fa
SH
432 unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
433
056834d9 434 hint = min(hint, tp->rcv_wnd / 2);
bee7ca9e 435 hint = min(hint, TCP_MSS_DEFAULT);
40efc6fa
SH
436 hint = max(hint, TCP_MIN_MSS);
437
438 inet_csk(sk)->icsk_ack.rcv_mss = hint;
439}
4bc2f18b 440EXPORT_SYMBOL(tcp_initialize_rcv_mss);
40efc6fa 441
1da177e4
LT
442/* Receiver "autotuning" code.
443 *
444 * The algorithm for RTT estimation w/o timestamps is based on
445 * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL.
631dd1a8 446 * <http://public.lanl.gov/radiant/pubs.html#DRS>
1da177e4
LT
447 *
448 * More detail on this code can be found at
631dd1a8 449 * <http://staff.psc.edu/jheffner/>,
1da177e4
LT
450 * though this reference is out of date. A new paper
451 * is pending.
452 */
453static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
454{
455 u32 new_sample = tp->rcv_rtt_est.rtt;
456 long m = sample;
457
458 if (m == 0)
459 m = 1;
460
461 if (new_sample != 0) {
462 /* If we sample in larger samples in the non-timestamp
463 * case, we could grossly overestimate the RTT especially
464 * with chatty applications or bulk transfer apps which
465 * are stalled on filesystem I/O.
466 *
467 * Also, since we are only going for a minimum in the
31f34269 468 * non-timestamp case, we do not smooth things out
caa20d9a 469 * else with timestamps disabled convergence takes too
1da177e4
LT
470 * long.
471 */
472 if (!win_dep) {
473 m -= (new_sample >> 3);
474 new_sample += m;
475 } else if (m < new_sample)
476 new_sample = m << 3;
477 } else {
caa20d9a 478 /* No previous measure. */
1da177e4
LT
479 new_sample = m << 3;
480 }
481
482 if (tp->rcv_rtt_est.rtt != new_sample)
483 tp->rcv_rtt_est.rtt = new_sample;
484}
485
486static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
487{
488 if (tp->rcv_rtt_est.time == 0)
489 goto new_measure;
490 if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))
491 return;
056834d9 492 tcp_rcv_rtt_update(tp, jiffies - tp->rcv_rtt_est.time, 1);
1da177e4
LT
493
494new_measure:
495 tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
496 tp->rcv_rtt_est.time = tcp_time_stamp;
497}
498
056834d9
IJ
499static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
500 const struct sk_buff *skb)
1da177e4 501{
463c84b9 502 struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
503 if (tp->rx_opt.rcv_tsecr &&
504 (TCP_SKB_CB(skb)->end_seq -
463c84b9 505 TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss))
1da177e4
LT
506 tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rx_opt.rcv_tsecr, 0);
507}
508
509/*
510 * This function should be called every time data is copied to user space.
511 * It calculates the appropriate TCP receive buffer space.
512 */
513void tcp_rcv_space_adjust(struct sock *sk)
514{
515 struct tcp_sock *tp = tcp_sk(sk);
516 int time;
517 int space;
e905a9ed 518
1da177e4
LT
519 if (tp->rcvq_space.time == 0)
520 goto new_measure;
e905a9ed 521
1da177e4 522 time = tcp_time_stamp - tp->rcvq_space.time;
056834d9 523 if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0)
1da177e4 524 return;
e905a9ed 525
1da177e4
LT
526 space = 2 * (tp->copied_seq - tp->rcvq_space.seq);
527
528 space = max(tp->rcvq_space.space, space);
529
530 if (tp->rcvq_space.space != space) {
531 int rcvmem;
532
533 tp->rcvq_space.space = space;
534
6fcf9412
JH
535 if (sysctl_tcp_moderate_rcvbuf &&
536 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
1da177e4
LT
537 int new_clamp = space;
538
539 /* Receive space grows, normalize in order to
540 * take into account packet headers and sk_buff
541 * structure overhead.
542 */
543 space /= tp->advmss;
544 if (!space)
545 space = 1;
87fb4b7b 546 rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
1da177e4
LT
547 while (tcp_win_from_space(rcvmem) < tp->advmss)
548 rcvmem += 128;
549 space *= rcvmem;
550 space = min(space, sysctl_tcp_rmem[2]);
551 if (space > sk->sk_rcvbuf) {
552 sk->sk_rcvbuf = space;
553
554 /* Make the window clamp follow along. */
555 tp->window_clamp = new_clamp;
556 }
557 }
558 }
e905a9ed 559
1da177e4
LT
560new_measure:
561 tp->rcvq_space.seq = tp->copied_seq;
562 tp->rcvq_space.time = tcp_time_stamp;
563}
564
565/* There is something which you must keep in mind when you analyze the
566 * behavior of the tp->ato delayed ack timeout interval. When a
567 * connection starts up, we want to ack as quickly as possible. The
568 * problem is that "good" TCP's do slow start at the beginning of data
569 * transmission. The means that until we send the first few ACK's the
570 * sender will sit on his end and only queue most of his data, because
571 * he can only send snd_cwnd unacked packets at any given time. For
572 * each ACK we send, he increments snd_cwnd and transmits more of his
573 * queue. -DaveM
574 */
9e412ba7 575static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
1da177e4 576{
9e412ba7 577 struct tcp_sock *tp = tcp_sk(sk);
463c84b9 578 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4
LT
579 u32 now;
580
463c84b9 581 inet_csk_schedule_ack(sk);
1da177e4 582
463c84b9 583 tcp_measure_rcv_mss(sk, skb);
1da177e4
LT
584
585 tcp_rcv_rtt_measure(tp);
e905a9ed 586
1da177e4
LT
587 now = tcp_time_stamp;
588
463c84b9 589 if (!icsk->icsk_ack.ato) {
1da177e4
LT
590 /* The _first_ data packet received, initialize
591 * delayed ACK engine.
592 */
463c84b9
ACM
593 tcp_incr_quickack(sk);
594 icsk->icsk_ack.ato = TCP_ATO_MIN;
1da177e4 595 } else {
463c84b9 596 int m = now - icsk->icsk_ack.lrcvtime;
1da177e4 597
056834d9 598 if (m <= TCP_ATO_MIN / 2) {
1da177e4 599 /* The fastest case is the first. */
463c84b9
ACM
600 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
601 } else if (m < icsk->icsk_ack.ato) {
602 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m;
603 if (icsk->icsk_ack.ato > icsk->icsk_rto)
604 icsk->icsk_ack.ato = icsk->icsk_rto;
605 } else if (m > icsk->icsk_rto) {
caa20d9a 606 /* Too long gap. Apparently sender failed to
1da177e4
LT
607 * restart window, so that we send ACKs quickly.
608 */
463c84b9 609 tcp_incr_quickack(sk);
3ab224be 610 sk_mem_reclaim(sk);
1da177e4
LT
611 }
612 }
463c84b9 613 icsk->icsk_ack.lrcvtime = now;
1da177e4
LT
614
615 TCP_ECN_check_ce(tp, skb);
616
617 if (skb->len >= 128)
9e412ba7 618 tcp_grow_window(sk, skb);
1da177e4
LT
619}
620
1da177e4
LT
621/* Called to compute a smoothed rtt estimate. The data fed to this
622 * routine either comes from timestamps, or from segments that were
623 * known _not_ to have been retransmitted [see Karn/Partridge
624 * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88
625 * piece by Van Jacobson.
626 * NOTE: the next three routines used to be one big routine.
627 * To save cycles in the RFC 1323 implementation it was better to break
628 * it up into three procedures. -- erics
629 */
2d2abbab 630static void tcp_rtt_estimator(struct sock *sk, const __u32 mrtt)
1da177e4 631{
6687e988 632 struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
633 long m = mrtt; /* RTT */
634
1da177e4
LT
635 /* The following amusing code comes from Jacobson's
636 * article in SIGCOMM '88. Note that rtt and mdev
637 * are scaled versions of rtt and mean deviation.
e905a9ed 638 * This is designed to be as fast as possible
1da177e4
LT
639 * m stands for "measurement".
640 *
641 * On a 1990 paper the rto value is changed to:
642 * RTO = rtt + 4 * mdev
643 *
644 * Funny. This algorithm seems to be very broken.
645 * These formulae increase RTO, when it should be decreased, increase
31f34269 646 * too slowly, when it should be increased quickly, decrease too quickly
1da177e4
LT
647 * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
648 * does not matter how to _calculate_ it. Seems, it was trap
649 * that VJ failed to avoid. 8)
650 */
2de979bd 651 if (m == 0)
1da177e4
LT
652 m = 1;
653 if (tp->srtt != 0) {
654 m -= (tp->srtt >> 3); /* m is now error in rtt est */
655 tp->srtt += m; /* rtt = 7/8 rtt + 1/8 new */
656 if (m < 0) {
657 m = -m; /* m is now abs(error) */
658 m -= (tp->mdev >> 2); /* similar update on mdev */
659 /* This is similar to one of Eifel findings.
660 * Eifel blocks mdev updates when rtt decreases.
661 * This solution is a bit different: we use finer gain
662 * for mdev in this case (alpha*beta).
663 * Like Eifel it also prevents growth of rto,
664 * but also it limits too fast rto decreases,
665 * happening in pure Eifel.
666 */
667 if (m > 0)
668 m >>= 3;
669 } else {
670 m -= (tp->mdev >> 2); /* similar update on mdev */
671 }
672 tp->mdev += m; /* mdev = 3/4 mdev + 1/4 new */
673 if (tp->mdev > tp->mdev_max) {
674 tp->mdev_max = tp->mdev;
675 if (tp->mdev_max > tp->rttvar)
676 tp->rttvar = tp->mdev_max;
677 }
678 if (after(tp->snd_una, tp->rtt_seq)) {
679 if (tp->mdev_max < tp->rttvar)
056834d9 680 tp->rttvar -= (tp->rttvar - tp->mdev_max) >> 2;
1da177e4 681 tp->rtt_seq = tp->snd_nxt;
05bb1fad 682 tp->mdev_max = tcp_rto_min(sk);
1da177e4
LT
683 }
684 } else {
685 /* no previous measure. */
056834d9
IJ
686 tp->srtt = m << 3; /* take the measured time to be rtt */
687 tp->mdev = m << 1; /* make sure rto = 3*rtt */
05bb1fad 688 tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
1da177e4
LT
689 tp->rtt_seq = tp->snd_nxt;
690 }
1da177e4
LT
691}
692
693/* Calculate rto without backoff. This is the second half of Van Jacobson's
694 * routine referred to above.
695 */
463c84b9 696static inline void tcp_set_rto(struct sock *sk)
1da177e4 697{
463c84b9 698 const struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
699 /* Old crap is replaced with new one. 8)
700 *
701 * More seriously:
702 * 1. If rtt variance happened to be less 50msec, it is hallucination.
703 * It cannot be less due to utterly erratic ACK generation made
704 * at least by solaris and freebsd. "Erratic ACKs" has _nothing_
705 * to do with delayed acks, because at cwnd>2 true delack timeout
706 * is invisible. Actually, Linux-2.4 also generates erratic
caa20d9a 707 * ACKs in some circumstances.
1da177e4 708 */
f1ecd5d9 709 inet_csk(sk)->icsk_rto = __tcp_set_rto(tp);
1da177e4
LT
710
711 /* 2. Fixups made earlier cannot be right.
712 * If we do not estimate RTO correctly without them,
713 * all the algo is pure shit and should be replaced
caa20d9a 714 * with correct one. It is exactly, which we pretend to do.
1da177e4 715 */
1da177e4 716
ee6aac59
IJ
717 /* NOTE: clamping at TCP_RTO_MIN is not required, current algo
718 * guarantees that rto is higher.
719 */
f1ecd5d9 720 tcp_bound_rto(sk);
1da177e4
LT
721}
722
723/* Save metrics learned by this TCP session.
724 This function is called only, when TCP finishes successfully
725 i.e. when it enters TIME-WAIT or goes from LAST-ACK to CLOSE.
726 */
727void tcp_update_metrics(struct sock *sk)
728{
729 struct tcp_sock *tp = tcp_sk(sk);
730 struct dst_entry *dst = __sk_dst_get(sk);
731
732 if (sysctl_tcp_nometrics_save)
733 return;
734
735 dst_confirm(dst);
736
056834d9 737 if (dst && (dst->flags & DST_HOST)) {
6687e988 738 const struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 739 int m;
c1e20f7c 740 unsigned long rtt;
1da177e4 741
6687e988 742 if (icsk->icsk_backoff || !tp->srtt) {
1da177e4
LT
743 /* This session failed to estimate rtt. Why?
744 * Probably, no packets returned in time.
745 * Reset our results.
746 */
747 if (!(dst_metric_locked(dst, RTAX_RTT)))
defb3519 748 dst_metric_set(dst, RTAX_RTT, 0);
1da177e4
LT
749 return;
750 }
751
c1e20f7c
SH
752 rtt = dst_metric_rtt(dst, RTAX_RTT);
753 m = rtt - tp->srtt;
1da177e4
LT
754
755 /* If newly calculated rtt larger than stored one,
756 * store new one. Otherwise, use EWMA. Remember,
757 * rtt overestimation is always better than underestimation.
758 */
759 if (!(dst_metric_locked(dst, RTAX_RTT))) {
760 if (m <= 0)
c1e20f7c 761 set_dst_metric_rtt(dst, RTAX_RTT, tp->srtt);
1da177e4 762 else
c1e20f7c 763 set_dst_metric_rtt(dst, RTAX_RTT, rtt - (m >> 3));
1da177e4
LT
764 }
765
766 if (!(dst_metric_locked(dst, RTAX_RTTVAR))) {
c1e20f7c 767 unsigned long var;
1da177e4
LT
768 if (m < 0)
769 m = -m;
770
771 /* Scale deviation to rttvar fixed point */
772 m >>= 1;
773 if (m < tp->mdev)
774 m = tp->mdev;
775
c1e20f7c
SH
776 var = dst_metric_rtt(dst, RTAX_RTTVAR);
777 if (m >= var)
778 var = m;
1da177e4 779 else
c1e20f7c
SH
780 var -= (var - m) >> 2;
781
782 set_dst_metric_rtt(dst, RTAX_RTTVAR, var);
1da177e4
LT
783 }
784
0b6a05c1 785 if (tcp_in_initial_slowstart(tp)) {
1da177e4
LT
786 /* Slow start still did not finish. */
787 if (dst_metric(dst, RTAX_SSTHRESH) &&
788 !dst_metric_locked(dst, RTAX_SSTHRESH) &&
789 (tp->snd_cwnd >> 1) > dst_metric(dst, RTAX_SSTHRESH))
defb3519 790 dst_metric_set(dst, RTAX_SSTHRESH, tp->snd_cwnd >> 1);
1da177e4
LT
791 if (!dst_metric_locked(dst, RTAX_CWND) &&
792 tp->snd_cwnd > dst_metric(dst, RTAX_CWND))
defb3519 793 dst_metric_set(dst, RTAX_CWND, tp->snd_cwnd);
1da177e4 794 } else if (tp->snd_cwnd > tp->snd_ssthresh &&
6687e988 795 icsk->icsk_ca_state == TCP_CA_Open) {
1da177e4
LT
796 /* Cong. avoidance phase, cwnd is reliable. */
797 if (!dst_metric_locked(dst, RTAX_SSTHRESH))
defb3519
DM
798 dst_metric_set(dst, RTAX_SSTHRESH,
799 max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
1da177e4 800 if (!dst_metric_locked(dst, RTAX_CWND))
defb3519
DM
801 dst_metric_set(dst, RTAX_CWND,
802 (dst_metric(dst, RTAX_CWND) +
803 tp->snd_cwnd) >> 1);
1da177e4
LT
804 } else {
805 /* Else slow start did not finish, cwnd is non-sense,
806 ssthresh may be also invalid.
807 */
808 if (!dst_metric_locked(dst, RTAX_CWND))
defb3519
DM
809 dst_metric_set(dst, RTAX_CWND,
810 (dst_metric(dst, RTAX_CWND) +
811 tp->snd_ssthresh) >> 1);
5ffc02a1 812 if (dst_metric(dst, RTAX_SSTHRESH) &&
1da177e4 813 !dst_metric_locked(dst, RTAX_SSTHRESH) &&
5ffc02a1 814 tp->snd_ssthresh > dst_metric(dst, RTAX_SSTHRESH))
defb3519 815 dst_metric_set(dst, RTAX_SSTHRESH, tp->snd_ssthresh);
1da177e4
LT
816 }
817
818 if (!dst_metric_locked(dst, RTAX_REORDERING)) {
5ffc02a1 819 if (dst_metric(dst, RTAX_REORDERING) < tp->reordering &&
1da177e4 820 tp->reordering != sysctl_tcp_reordering)
defb3519 821 dst_metric_set(dst, RTAX_REORDERING, tp->reordering);
1da177e4
LT
822 }
823 }
824}
825
cf533ea5 826__u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst)
1da177e4
LT
827{
828 __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
829
22b71c8f 830 if (!cwnd)
442b9635 831 cwnd = TCP_INIT_CWND;
1da177e4
LT
832 return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
833}
834
40efc6fa 835/* Set slow start threshold and cwnd not falling to slow start */
3cfe3baa 836void tcp_enter_cwr(struct sock *sk, const int set_ssthresh)
40efc6fa
SH
837{
838 struct tcp_sock *tp = tcp_sk(sk);
3cfe3baa 839 const struct inet_connection_sock *icsk = inet_csk(sk);
40efc6fa
SH
840
841 tp->prior_ssthresh = 0;
842 tp->bytes_acked = 0;
e01f9d77 843 if (icsk->icsk_ca_state < TCP_CA_CWR) {
40efc6fa 844 tp->undo_marker = 0;
3cfe3baa
IJ
845 if (set_ssthresh)
846 tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
40efc6fa
SH
847 tp->snd_cwnd = min(tp->snd_cwnd,
848 tcp_packets_in_flight(tp) + 1U);
849 tp->snd_cwnd_cnt = 0;
850 tp->high_seq = tp->snd_nxt;
851 tp->snd_cwnd_stamp = tcp_time_stamp;
852 TCP_ECN_queue_cwr(tp);
853
854 tcp_set_ca_state(sk, TCP_CA_CWR);
855 }
856}
857
e60402d0
IJ
858/*
859 * Packet counting of FACK is based on in-order assumptions, therefore TCP
860 * disables it when reordering is detected
861 */
862static void tcp_disable_fack(struct tcp_sock *tp)
863{
85cc391c
IJ
864 /* RFC3517 uses different metric in lost marker => reset on change */
865 if (tcp_is_fack(tp))
866 tp->lost_skb_hint = NULL;
ab56222a 867 tp->rx_opt.sack_ok &= ~TCP_FACK_ENABLED;
e60402d0
IJ
868}
869
564262c1 870/* Take a notice that peer is sending D-SACKs */
e60402d0
IJ
871static void tcp_dsack_seen(struct tcp_sock *tp)
872{
ab56222a 873 tp->rx_opt.sack_ok |= TCP_DSACK_SEEN;
e60402d0
IJ
874}
875
1da177e4
LT
876/* Initialize metrics on socket. */
877
878static void tcp_init_metrics(struct sock *sk)
879{
880 struct tcp_sock *tp = tcp_sk(sk);
881 struct dst_entry *dst = __sk_dst_get(sk);
882
883 if (dst == NULL)
884 goto reset;
885
886 dst_confirm(dst);
887
888 if (dst_metric_locked(dst, RTAX_CWND))
889 tp->snd_cwnd_clamp = dst_metric(dst, RTAX_CWND);
890 if (dst_metric(dst, RTAX_SSTHRESH)) {
891 tp->snd_ssthresh = dst_metric(dst, RTAX_SSTHRESH);
892 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
893 tp->snd_ssthresh = tp->snd_cwnd_clamp;
9ad7c049
JC
894 } else {
895 /* ssthresh may have been reduced unnecessarily during.
896 * 3WHS. Restore it back to its initial default.
897 */
898 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
1da177e4
LT
899 }
900 if (dst_metric(dst, RTAX_REORDERING) &&
901 tp->reordering != dst_metric(dst, RTAX_REORDERING)) {
e60402d0 902 tcp_disable_fack(tp);
1da177e4
LT
903 tp->reordering = dst_metric(dst, RTAX_REORDERING);
904 }
905
9ad7c049 906 if (dst_metric(dst, RTAX_RTT) == 0 || tp->srtt == 0)
1da177e4
LT
907 goto reset;
908
909 /* Initial rtt is determined from SYN,SYN-ACK.
910 * The segment is small and rtt may appear much
911 * less than real one. Use per-dst memory
912 * to make it more realistic.
913 *
914 * A bit of theory. RTT is time passed after "normal" sized packet
caa20d9a 915 * is sent until it is ACKed. In normal circumstances sending small
1da177e4
LT
916 * packets force peer to delay ACKs and calculation is correct too.
917 * The algorithm is adaptive and, provided we follow specs, it
918 * NEVER underestimate RTT. BUT! If peer tries to make some clever
919 * tricks sort of "quick acks" for time long enough to decrease RTT
920 * to low value, and then abruptly stops to do it and starts to delay
921 * ACKs, wait for troubles.
922 */
c1e20f7c
SH
923 if (dst_metric_rtt(dst, RTAX_RTT) > tp->srtt) {
924 tp->srtt = dst_metric_rtt(dst, RTAX_RTT);
1da177e4
LT
925 tp->rtt_seq = tp->snd_nxt;
926 }
c1e20f7c
SH
927 if (dst_metric_rtt(dst, RTAX_RTTVAR) > tp->mdev) {
928 tp->mdev = dst_metric_rtt(dst, RTAX_RTTVAR);
488faa2a 929 tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
1da177e4 930 }
463c84b9 931 tcp_set_rto(sk);
1da177e4 932reset:
9ad7c049
JC
933 if (tp->srtt == 0) {
934 /* RFC2988bis: We've failed to get a valid RTT sample from
935 * 3WHS. This is most likely due to retransmission,
936 * including spurious one. Reset the RTO back to 3secs
937 * from the more aggressive 1sec to avoid more spurious
938 * retransmission.
d9f4fbaf 939 */
9ad7c049
JC
940 tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_FALLBACK;
941 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
1da177e4 942 }
9ad7c049
JC
943 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
944 * retransmitted. In light of RFC2988bis' more aggressive 1sec
945 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
946 * retransmission has occurred.
947 */
948 if (tp->total_retrans > 1)
949 tp->snd_cwnd = 1;
950 else
951 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
d9f4fbaf 952 tp->snd_cwnd_stamp = tcp_time_stamp;
1da177e4
LT
953}
954
6687e988
ACM
955static void tcp_update_reordering(struct sock *sk, const int metric,
956 const int ts)
1da177e4 957{
6687e988 958 struct tcp_sock *tp = tcp_sk(sk);
1da177e4 959 if (metric > tp->reordering) {
40b215e5
PE
960 int mib_idx;
961
1da177e4
LT
962 tp->reordering = min(TCP_MAX_REORDERING, metric);
963
964 /* This exciting event is worth to be remembered. 8) */
965 if (ts)
40b215e5 966 mib_idx = LINUX_MIB_TCPTSREORDER;
e60402d0 967 else if (tcp_is_reno(tp))
40b215e5 968 mib_idx = LINUX_MIB_TCPRENOREORDER;
e60402d0 969 else if (tcp_is_fack(tp))
40b215e5 970 mib_idx = LINUX_MIB_TCPFACKREORDER;
1da177e4 971 else
40b215e5
PE
972 mib_idx = LINUX_MIB_TCPSACKREORDER;
973
de0744af 974 NET_INC_STATS_BH(sock_net(sk), mib_idx);
1da177e4
LT
975#if FASTRETRANS_DEBUG > 1
976 printk(KERN_DEBUG "Disorder%d %d %u f%u s%u rr%d\n",
6687e988 977 tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
1da177e4
LT
978 tp->reordering,
979 tp->fackets_out,
980 tp->sacked_out,
981 tp->undo_marker ? tp->undo_retrans : 0);
982#endif
e60402d0 983 tcp_disable_fack(tp);
1da177e4
LT
984 }
985}
986
006f582c 987/* This must be called before lost_out is incremented */
c8c213f2
IJ
988static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
989{
006f582c 990 if ((tp->retransmit_skb_hint == NULL) ||
c8c213f2
IJ
991 before(TCP_SKB_CB(skb)->seq,
992 TCP_SKB_CB(tp->retransmit_skb_hint)->seq))
006f582c
IJ
993 tp->retransmit_skb_hint = skb;
994
995 if (!tp->lost_out ||
996 after(TCP_SKB_CB(skb)->end_seq, tp->retransmit_high))
997 tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
c8c213f2
IJ
998}
999
41ea36e3
IJ
1000static void tcp_skb_mark_lost(struct tcp_sock *tp, struct sk_buff *skb)
1001{
1002 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
1003 tcp_verify_retransmit_hint(tp, skb);
1004
1005 tp->lost_out += tcp_skb_pcount(skb);
1006 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
1007 }
1008}
1009
e1aa680f
IJ
1010static void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp,
1011 struct sk_buff *skb)
006f582c
IJ
1012{
1013 tcp_verify_retransmit_hint(tp, skb);
1014
1015 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
1016 tp->lost_out += tcp_skb_pcount(skb);
1017 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
1018 }
1019}
1020
1da177e4
LT
1021/* This procedure tags the retransmission queue when SACKs arrive.
1022 *
1023 * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
1024 * Packets in queue with these bits set are counted in variables
1025 * sacked_out, retrans_out and lost_out, correspondingly.
1026 *
1027 * Valid combinations are:
1028 * Tag InFlight Description
1029 * 0 1 - orig segment is in flight.
1030 * S 0 - nothing flies, orig reached receiver.
1031 * L 0 - nothing flies, orig lost by net.
1032 * R 2 - both orig and retransmit are in flight.
1033 * L|R 1 - orig is lost, retransmit is in flight.
1034 * S|R 1 - orig reached receiver, retrans is still in flight.
1035 * (L|S|R is logically valid, it could occur when L|R is sacked,
1036 * but it is equivalent to plain S and code short-curcuits it to S.
1037 * L|S is logically invalid, it would mean -1 packet in flight 8))
1038 *
1039 * These 6 states form finite state machine, controlled by the following events:
1040 * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
1041 * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
974c1236 1042 * 3. Loss detection event of two flavors:
1da177e4
LT
1043 * A. Scoreboard estimator decided the packet is lost.
1044 * A'. Reno "three dupacks" marks head of queue lost.
974c1236
YC
1045 * A''. Its FACK modification, head until snd.fack is lost.
1046 * B. SACK arrives sacking SND.NXT at the moment, when the
1da177e4
LT
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 1152/* Check for lost retransmit. This superb idea is borrowed from "ratehalving".
974c1236 1153 * Event "B". Later note: FACK people cheated me again 8), we have to account
1c1e87ed 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
cf533ea5 1216static int tcp_check_dsack(struct sock *sk, const 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
cf533ea5 1310static u8 tcp_sacktag_one(const struct sk_buff *skb, struct sock *sk,
a1197f5a
IJ
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
1e5289e1 1401 if (skb == tp->lost_skb_hint)
92ee76b6
IJ
1402 tp->lost_cnt_hint += pcount;
1403
832d11c5
IJ
1404 TCP_SKB_CB(prev)->end_seq += shifted;
1405 TCP_SKB_CB(skb)->seq += shifted;
1406
1407 skb_shinfo(prev)->gso_segs += pcount;
1408 BUG_ON(skb_shinfo(skb)->gso_segs < pcount);
1409 skb_shinfo(skb)->gso_segs -= pcount;
1410
1411 /* When we're adding to gso_segs == 1, gso_size will be zero,
1412 * in theory this shouldn't be necessary but as long as DSACK
1413 * code can come after this skb later on it's better to keep
1414 * setting gso_size to something.
1415 */
1416 if (!skb_shinfo(prev)->gso_size) {
1417 skb_shinfo(prev)->gso_size = mss;
1418 skb_shinfo(prev)->gso_type = sk->sk_gso_type;
1419 }
1420
1421 /* CHECKME: To clear or not to clear? Mimics normal skb currently */
1422 if (skb_shinfo(skb)->gso_segs <= 1) {
1423 skb_shinfo(skb)->gso_size = 0;
1424 skb_shinfo(skb)->gso_type = 0;
1425 }
1426
a1197f5a 1427 /* We discard results */
9ec06ff5 1428 tcp_sacktag_one(skb, sk, state, dup_sack, pcount);
832d11c5
IJ
1429
1430 /* Difference in this won't matter, both ACKed by the same cumul. ACK */
1431 TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
1432
832d11c5
IJ
1433 if (skb->len > 0) {
1434 BUG_ON(!tcp_skb_pcount(skb));
111cc8b9 1435 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTED);
832d11c5
IJ
1436 return 0;
1437 }
1438
1439 /* Whole SKB was eaten :-) */
1440
92ee76b6
IJ
1441 if (skb == tp->retransmit_skb_hint)
1442 tp->retransmit_skb_hint = prev;
1443 if (skb == tp->scoreboard_skb_hint)
1444 tp->scoreboard_skb_hint = prev;
1445 if (skb == tp->lost_skb_hint) {
1446 tp->lost_skb_hint = prev;
1447 tp->lost_cnt_hint -= tcp_skb_pcount(prev);
1448 }
1449
4de075e0 1450 TCP_SKB_CB(skb)->tcp_flags |= TCP_SKB_CB(prev)->tcp_flags;
832d11c5
IJ
1451 if (skb == tcp_highest_sack(sk))
1452 tcp_advance_highest_sack(sk, skb);
1453
1454 tcp_unlink_write_queue(skb, sk);
1455 sk_wmem_free_skb(sk, skb);
1456
111cc8b9
IJ
1457 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKMERGED);
1458
832d11c5
IJ
1459 return 1;
1460}
1461
1462/* I wish gso_size would have a bit more sane initialization than
1463 * something-or-zero which complicates things
1464 */
cf533ea5 1465static int tcp_skb_seglen(const struct sk_buff *skb)
832d11c5 1466{
775ffabf 1467 return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
832d11c5
IJ
1468}
1469
1470/* Shifting pages past head area doesn't work */
cf533ea5 1471static int skb_can_shift(const struct sk_buff *skb)
832d11c5
IJ
1472{
1473 return !skb_headlen(skb) && skb_is_nonlinear(skb);
1474}
1475
1476/* Try collapsing SACK blocks spanning across multiple skbs to a single
1477 * skb.
1478 */
1479static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
a1197f5a 1480 struct tcp_sacktag_state *state,
832d11c5 1481 u32 start_seq, u32 end_seq,
a1197f5a 1482 int dup_sack)
832d11c5
IJ
1483{
1484 struct tcp_sock *tp = tcp_sk(sk);
1485 struct sk_buff *prev;
1486 int mss;
1487 int pcount = 0;
1488 int len;
1489 int in_sack;
1490
1491 if (!sk_can_gso(sk))
1492 goto fallback;
1493
1494 /* Normally R but no L won't result in plain S */
1495 if (!dup_sack &&
9969ca5f 1496 (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
832d11c5
IJ
1497 goto fallback;
1498 if (!skb_can_shift(skb))
1499 goto fallback;
1500 /* This frame is about to be dropped (was ACKed). */
1501 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1502 goto fallback;
1503
1504 /* Can only happen with delayed DSACK + discard craziness */
1505 if (unlikely(skb == tcp_write_queue_head(sk)))
1506 goto fallback;
1507 prev = tcp_write_queue_prev(sk, skb);
1508
1509 if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
1510 goto fallback;
1511
1512 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1513 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1514
1515 if (in_sack) {
1516 len = skb->len;
1517 pcount = tcp_skb_pcount(skb);
775ffabf 1518 mss = tcp_skb_seglen(skb);
832d11c5
IJ
1519
1520 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1521 * drop this restriction as unnecessary
1522 */
775ffabf 1523 if (mss != tcp_skb_seglen(prev))
832d11c5
IJ
1524 goto fallback;
1525 } else {
1526 if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
1527 goto noop;
1528 /* CHECKME: This is non-MSS split case only?, this will
1529 * cause skipped skbs due to advancing loop btw, original
1530 * has that feature too
1531 */
1532 if (tcp_skb_pcount(skb) <= 1)
1533 goto noop;
1534
1535 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1536 if (!in_sack) {
1537 /* TODO: head merge to next could be attempted here
1538 * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
1539 * though it might not be worth of the additional hassle
1540 *
1541 * ...we can probably just fallback to what was done
1542 * previously. We could try merging non-SACKed ones
1543 * as well but it probably isn't going to buy off
1544 * because later SACKs might again split them, and
1545 * it would make skb timestamp tracking considerably
1546 * harder problem.
1547 */
1548 goto fallback;
1549 }
1550
1551 len = end_seq - TCP_SKB_CB(skb)->seq;
1552 BUG_ON(len < 0);
1553 BUG_ON(len > skb->len);
1554
1555 /* MSS boundaries should be honoured or else pcount will
1556 * severely break even though it makes things bit trickier.
1557 * Optimize common case to avoid most of the divides
1558 */
1559 mss = tcp_skb_mss(skb);
1560
1561 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1562 * drop this restriction as unnecessary
1563 */
775ffabf 1564 if (mss != tcp_skb_seglen(prev))
832d11c5
IJ
1565 goto fallback;
1566
1567 if (len == mss) {
1568 pcount = 1;
1569 } else if (len < mss) {
1570 goto noop;
1571 } else {
1572 pcount = len / mss;
1573 len = pcount * mss;
1574 }
1575 }
1576
1577 if (!skb_shift(prev, skb, len))
1578 goto fallback;
9ec06ff5 1579 if (!tcp_shifted_skb(sk, skb, state, pcount, len, mss, dup_sack))
832d11c5
IJ
1580 goto out;
1581
1582 /* Hole filled allows collapsing with the next as well, this is very
1583 * useful when hole on every nth skb pattern happens
1584 */
1585 if (prev == tcp_write_queue_tail(sk))
1586 goto out;
1587 skb = tcp_write_queue_next(sk, prev);
1588
f0bc52f3
IJ
1589 if (!skb_can_shift(skb) ||
1590 (skb == tcp_send_head(sk)) ||
1591 ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
775ffabf 1592 (mss != tcp_skb_seglen(skb)))
832d11c5
IJ
1593 goto out;
1594
1595 len = skb->len;
1596 if (skb_shift(prev, skb, len)) {
1597 pcount += tcp_skb_pcount(skb);
9ec06ff5 1598 tcp_shifted_skb(sk, skb, state, tcp_skb_pcount(skb), len, mss, 0);
832d11c5
IJ
1599 }
1600
1601out:
a1197f5a 1602 state->fack_count += pcount;
832d11c5
IJ
1603 return prev;
1604
1605noop:
1606 return skb;
1607
1608fallback:
111cc8b9 1609 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
832d11c5
IJ
1610 return NULL;
1611}
1612
68f8353b
IJ
1613static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
1614 struct tcp_sack_block *next_dup,
a1197f5a 1615 struct tcp_sacktag_state *state,
68f8353b 1616 u32 start_seq, u32 end_seq,
a1197f5a 1617 int dup_sack_in)
68f8353b 1618{
832d11c5
IJ
1619 struct tcp_sock *tp = tcp_sk(sk);
1620 struct sk_buff *tmp;
1621
68f8353b
IJ
1622 tcp_for_write_queue_from(skb, sk) {
1623 int in_sack = 0;
1624 int dup_sack = dup_sack_in;
1625
1626 if (skb == tcp_send_head(sk))
1627 break;
1628
1629 /* queue is in-order => we can short-circuit the walk early */
1630 if (!before(TCP_SKB_CB(skb)->seq, end_seq))
1631 break;
1632
1633 if ((next_dup != NULL) &&
1634 before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
1635 in_sack = tcp_match_skb_to_sack(sk, skb,
1636 next_dup->start_seq,
1637 next_dup->end_seq);
1638 if (in_sack > 0)
1639 dup_sack = 1;
1640 }
1641
832d11c5
IJ
1642 /* skb reference here is a bit tricky to get right, since
1643 * shifting can eat and free both this skb and the next,
1644 * so not even _safe variant of the loop is enough.
1645 */
1646 if (in_sack <= 0) {
a1197f5a
IJ
1647 tmp = tcp_shift_skb_data(sk, skb, state,
1648 start_seq, end_seq, dup_sack);
832d11c5
IJ
1649 if (tmp != NULL) {
1650 if (tmp != skb) {
1651 skb = tmp;
1652 continue;
1653 }
1654
1655 in_sack = 0;
1656 } else {
1657 in_sack = tcp_match_skb_to_sack(sk, skb,
1658 start_seq,
1659 end_seq);
1660 }
1661 }
1662
68f8353b
IJ
1663 if (unlikely(in_sack < 0))
1664 break;
1665
832d11c5 1666 if (in_sack) {
a1197f5a
IJ
1667 TCP_SKB_CB(skb)->sacked = tcp_sacktag_one(skb, sk,
1668 state,
1669 dup_sack,
1670 tcp_skb_pcount(skb));
68f8353b 1671
832d11c5
IJ
1672 if (!before(TCP_SKB_CB(skb)->seq,
1673 tcp_highest_sack_seq(tp)))
1674 tcp_advance_highest_sack(sk, skb);
1675 }
1676
a1197f5a 1677 state->fack_count += tcp_skb_pcount(skb);
68f8353b
IJ
1678 }
1679 return skb;
1680}
1681
1682/* Avoid all extra work that is being done by sacktag while walking in
1683 * a normal way
1684 */
1685static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk,
a1197f5a
IJ
1686 struct tcp_sacktag_state *state,
1687 u32 skip_to_seq)
68f8353b
IJ
1688{
1689 tcp_for_write_queue_from(skb, sk) {
1690 if (skb == tcp_send_head(sk))
1691 break;
1692
e8bae275 1693 if (after(TCP_SKB_CB(skb)->end_seq, skip_to_seq))
68f8353b 1694 break;
d152a7d8 1695
a1197f5a 1696 state->fack_count += tcp_skb_pcount(skb);
68f8353b
IJ
1697 }
1698 return skb;
1699}
1700
1701static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
1702 struct sock *sk,
1703 struct tcp_sack_block *next_dup,
a1197f5a
IJ
1704 struct tcp_sacktag_state *state,
1705 u32 skip_to_seq)
68f8353b
IJ
1706{
1707 if (next_dup == NULL)
1708 return skb;
1709
1710 if (before(next_dup->start_seq, skip_to_seq)) {
a1197f5a
IJ
1711 skb = tcp_sacktag_skip(skb, sk, state, next_dup->start_seq);
1712 skb = tcp_sacktag_walk(skb, sk, NULL, state,
1713 next_dup->start_seq, next_dup->end_seq,
1714 1);
68f8353b
IJ
1715 }
1716
1717 return skb;
1718}
1719
cf533ea5 1720static int tcp_sack_cache_ok(const struct tcp_sock *tp, const struct tcp_sack_block *cache)
68f8353b
IJ
1721{
1722 return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
1723}
1724
1da177e4 1725static int
cf533ea5 1726tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb,
056834d9 1727 u32 prior_snd_una)
1da177e4 1728{
6687e988 1729 const struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 1730 struct tcp_sock *tp = tcp_sk(sk);
cf533ea5
ED
1731 const unsigned char *ptr = (skb_transport_header(ack_skb) +
1732 TCP_SKB_CB(ack_skb)->sacked);
fd6dad61 1733 struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
4389dded 1734 struct tcp_sack_block sp[TCP_NUM_SACKS];
68f8353b 1735 struct tcp_sack_block *cache;
a1197f5a 1736 struct tcp_sacktag_state state;
68f8353b 1737 struct sk_buff *skb;
4389dded 1738 int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
fd6dad61 1739 int used_sacks;
7769f406 1740 int found_dup_sack = 0;
68f8353b 1741 int i, j;
fda03fbb 1742 int first_sack_index;
1da177e4 1743
a1197f5a
IJ
1744 state.flag = 0;
1745 state.reord = tp->packets_out;
1746
d738cd8f 1747 if (!tp->sacked_out) {
de83c058
IJ
1748 if (WARN_ON(tp->fackets_out))
1749 tp->fackets_out = 0;
6859d494 1750 tcp_highest_sack_reset(sk);
d738cd8f 1751 }
1da177e4 1752
1ed83465 1753 found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
d06e021d
DM
1754 num_sacks, prior_snd_una);
1755 if (found_dup_sack)
a1197f5a 1756 state.flag |= FLAG_DSACKING_ACK;
6f74651a
BE
1757
1758 /* Eliminate too old ACKs, but take into
1759 * account more or less fresh ones, they can
1760 * contain valid SACK info.
1761 */
1762 if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
1763 return 0;
1764