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