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