]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * INET An implementation of the TCP/IP protocol suite for the LINUX | |
4 | * operating system. INET is implemented using the BSD Socket | |
5 | * interface as the means of communication with the user level. | |
6 | * | |
7 | * Implementation of the Transmission Control Protocol(TCP). | |
8 | * | |
02c30a84 | 9 | * Authors: Ross Biro |
1da177e4 LT |
10 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
11 | * Mark Evans, <evansmp@uhura.aston.ac.uk> | |
12 | * Corey Minyard <wf-rch!minyard@relay.EU.net> | |
13 | * Florian La Roche, <flla@stud.uni-sb.de> | |
14 | * Charles Hedrick, <hedrick@klinzhai.rutgers.edu> | |
15 | * Linus Torvalds, <torvalds@cs.helsinki.fi> | |
16 | * Alan Cox, <gw4pts@gw4pts.ampr.org> | |
17 | * Matthew Dillon, <dillon@apollo.west.oic.com> | |
18 | * Arnt Gulbrandsen, <agulbra@nvg.unit.no> | |
19 | * Jorge Cwik, <jorge@laser.satlink.net> | |
20 | */ | |
21 | ||
22 | /* | |
23 | * Changes: | |
24 | * Pedro Roque : Fast Retransmit/Recovery. | |
25 | * Two receive queues. | |
26 | * Retransmit queue handled by TCP. | |
27 | * Better retransmit timer handling. | |
28 | * New congestion avoidance. | |
29 | * Header prediction. | |
30 | * Variable renaming. | |
31 | * | |
32 | * Eric : Fast Retransmit. | |
33 | * Randy Scott : MSS option defines. | |
34 | * Eric Schenk : Fixes to slow start algorithm. | |
35 | * Eric Schenk : Yet another double ACK bug. | |
36 | * Eric Schenk : Delayed ACK bug fixes. | |
37 | * Eric Schenk : Floyd style fast retrans war avoidance. | |
38 | * David S. Miller : Don't allow zero congestion window. | |
39 | * Eric Schenk : Fix retransmitter so that it sends | |
40 | * next packet on ack of previous packet. | |
41 | * Andi Kleen : Moved open_request checking here | |
42 | * and process RSTs for open_requests. | |
43 | * Andi Kleen : Better prune_queue, and other fixes. | |
caa20d9a | 44 | * Andrey Savochkin: Fix RTT measurements in the presence of |
1da177e4 LT |
45 | * timestamps. |
46 | * Andrey Savochkin: Check sequence numbers correctly when | |
47 | * removing SACKs due to in sequence incoming | |
48 | * data segments. | |
49 | * Andi Kleen: Make sure we never ack data there is not | |
50 | * enough room for. Also make this condition | |
51 | * a fatal error if it might still happen. | |
e905a9ed | 52 | * Andi Kleen: Add tcp_measure_rcv_mss to make |
1da177e4 | 53 | * connections with MSS<min(MTU,ann. MSS) |
e905a9ed | 54 | * work without delayed acks. |
1da177e4 LT |
55 | * Andi Kleen: Process packets with PSH set in the |
56 | * fast path. | |
57 | * J Hadi Salim: ECN support | |
58 | * Andrei Gurtov, | |
59 | * Pasi Sarolahti, | |
60 | * Panu Kuhlberg: Experimental audit of TCP (re)transmission | |
61 | * engine. Lots of bugs are found. | |
62 | * Pasi Sarolahti: F-RTO for dealing with spurious RTOs | |
1da177e4 LT |
63 | */ |
64 | ||
afd46503 JP |
65 | #define pr_fmt(fmt) "TCP: " fmt |
66 | ||
1da177e4 | 67 | #include <linux/mm.h> |
5a0e3ad6 | 68 | #include <linux/slab.h> |
1da177e4 LT |
69 | #include <linux/module.h> |
70 | #include <linux/sysctl.h> | |
a0bffffc | 71 | #include <linux/kernel.h> |
ad971f61 | 72 | #include <linux/prefetch.h> |
5ffc02a1 | 73 | #include <net/dst.h> |
1da177e4 LT |
74 | #include <net/tcp.h> |
75 | #include <net/inet_common.h> | |
76 | #include <linux/ipsec.h> | |
77 | #include <asm/unaligned.h> | |
e1c8a607 | 78 | #include <linux/errqueue.h> |
5941521c | 79 | #include <trace/events/tcp.h> |
494bc1d2 | 80 | #include <linux/jump_label_ratelimit.h> |
c6345ce7 | 81 | #include <net/busy_poll.h> |
eda7acdd | 82 | #include <net/mptcp.h> |
1da177e4 | 83 | |
ab32ea5d | 84 | int sysctl_tcp_max_orphans __read_mostly = NR_FILE; |
1da177e4 | 85 | |
1da177e4 LT |
86 | #define FLAG_DATA 0x01 /* Incoming frame contained data. */ |
87 | #define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */ | |
88 | #define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */ | |
89 | #define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted. */ | |
90 | #define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */ | |
91 | #define FLAG_DATA_SACKED 0x20 /* New SACK. */ | |
92 | #define FLAG_ECE 0x40 /* ECE in this ACK */ | |
291a00d1 | 93 | #define FLAG_LOST_RETRANS 0x80 /* This ACK marks some retransmission lost */ |
31770e34 | 94 | #define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/ |
e33099f9 | 95 | #define FLAG_ORIG_SACK_ACKED 0x200 /* Never retransmitted data are (s)acked */ |
2e605294 | 96 | #define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */ |
564262c1 | 97 | #define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */ |
df92c839 | 98 | #define FLAG_SET_XMIT_TIMER 0x1000 /* Set TLP or RTO timer */ |
cadbd031 | 99 | #define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */ |
12fb3dd9 | 100 | #define FLAG_UPDATE_TS_RECENT 0x4000 /* tcp_replace_ts_recent() */ |
d0e1a1b5 | 101 | #define FLAG_NO_CHALLENGE_ACK 0x8000 /* do not call tcp_send_challenge_ack() */ |
eb36be0f | 102 | #define FLAG_ACK_MAYBE_DELAYED 0x10000 /* Likely a delayed ACK */ |
63f367d9 | 103 | #define FLAG_DSACK_TLP 0x20000 /* DSACK for tail loss probe */ |
1da177e4 LT |
104 | |
105 | #define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED) | |
106 | #define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED) | |
d09b9e60 | 107 | #define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE|FLAG_DSACKING_ACK) |
1da177e4 LT |
108 | #define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED) |
109 | ||
1da177e4 | 110 | #define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH) |
bdf1ee5d | 111 | #define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH)) |
1da177e4 | 112 | |
e662ca40 YC |
113 | #define REXMIT_NONE 0 /* no loss recovery to do */ |
114 | #define REXMIT_LOST 1 /* retransmit packets marked lost */ | |
115 | #define REXMIT_NEW 2 /* FRTO-style transmit of unsent/new packets */ | |
116 | ||
6dac1523 | 117 | #if IS_ENABLED(CONFIG_TLS_DEVICE) |
494bc1d2 | 118 | static DEFINE_STATIC_KEY_DEFERRED_FALSE(clean_acked_data_enabled, HZ); |
6dac1523 IL |
119 | |
120 | void clean_acked_data_enable(struct inet_connection_sock *icsk, | |
121 | void (*cad)(struct sock *sk, u32 ack_seq)) | |
122 | { | |
123 | icsk->icsk_clean_acked = cad; | |
7b58139f | 124 | static_branch_deferred_inc(&clean_acked_data_enabled); |
6dac1523 IL |
125 | } |
126 | EXPORT_SYMBOL_GPL(clean_acked_data_enable); | |
127 | ||
128 | void clean_acked_data_disable(struct inet_connection_sock *icsk) | |
129 | { | |
494bc1d2 | 130 | static_branch_slow_dec_deferred(&clean_acked_data_enabled); |
6dac1523 IL |
131 | icsk->icsk_clean_acked = NULL; |
132 | } | |
133 | EXPORT_SYMBOL_GPL(clean_acked_data_disable); | |
494bc1d2 JK |
134 | |
135 | void clean_acked_data_flush(void) | |
136 | { | |
137 | static_key_deferred_flush(&clean_acked_data_enabled); | |
138 | } | |
139 | EXPORT_SYMBOL_GPL(clean_acked_data_flush); | |
6dac1523 IL |
140 | #endif |
141 | ||
72be0fe6 | 142 | #ifdef CONFIG_CGROUP_BPF |
00d211a4 MKL |
143 | static void bpf_skops_parse_hdr(struct sock *sk, struct sk_buff *skb) |
144 | { | |
145 | bool unknown_opt = tcp_sk(sk)->rx_opt.saw_unknown && | |
146 | BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), | |
147 | BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG); | |
148 | bool parse_all_opt = BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), | |
149 | BPF_SOCK_OPS_PARSE_ALL_HDR_OPT_CB_FLAG); | |
0813a841 | 150 | struct bpf_sock_ops_kern sock_ops; |
00d211a4 MKL |
151 | |
152 | if (likely(!unknown_opt && !parse_all_opt)) | |
153 | return; | |
154 | ||
155 | /* The skb will be handled in the | |
156 | * bpf_skops_established() or | |
157 | * bpf_skops_write_hdr_opt(). | |
158 | */ | |
159 | switch (sk->sk_state) { | |
160 | case TCP_SYN_RECV: | |
161 | case TCP_SYN_SENT: | |
162 | case TCP_LISTEN: | |
163 | return; | |
164 | } | |
165 | ||
0813a841 MKL |
166 | sock_owned_by_me(sk); |
167 | ||
168 | memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); | |
169 | sock_ops.op = BPF_SOCK_OPS_PARSE_HDR_OPT_CB; | |
170 | sock_ops.is_fullsock = 1; | |
171 | sock_ops.sk = sk; | |
172 | bpf_skops_init_skb(&sock_ops, skb, tcp_hdrlen(skb)); | |
173 | ||
174 | BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops); | |
00d211a4 MKL |
175 | } |
176 | ||
72be0fe6 MKL |
177 | static void bpf_skops_established(struct sock *sk, int bpf_op, |
178 | struct sk_buff *skb) | |
179 | { | |
180 | struct bpf_sock_ops_kern sock_ops; | |
181 | ||
182 | sock_owned_by_me(sk); | |
183 | ||
184 | memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); | |
185 | sock_ops.op = bpf_op; | |
186 | sock_ops.is_fullsock = 1; | |
187 | sock_ops.sk = sk; | |
0813a841 MKL |
188 | /* sk with TCP_REPAIR_ON does not have skb in tcp_finish_connect */ |
189 | if (skb) | |
190 | bpf_skops_init_skb(&sock_ops, skb, tcp_hdrlen(skb)); | |
72be0fe6 MKL |
191 | |
192 | BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops); | |
193 | } | |
194 | #else | |
00d211a4 MKL |
195 | static void bpf_skops_parse_hdr(struct sock *sk, struct sk_buff *skb) |
196 | { | |
197 | } | |
198 | ||
72be0fe6 MKL |
199 | static void bpf_skops_established(struct sock *sk, int bpf_op, |
200 | struct sk_buff *skb) | |
201 | { | |
202 | } | |
203 | #endif | |
204 | ||
0b9aefea MRL |
205 | static void tcp_gro_dev_warn(struct sock *sk, const struct sk_buff *skb, |
206 | unsigned int len) | |
dcb17d22 MRL |
207 | { |
208 | static bool __once __read_mostly; | |
209 | ||
210 | if (!__once) { | |
211 | struct net_device *dev; | |
212 | ||
213 | __once = true; | |
214 | ||
215 | rcu_read_lock(); | |
216 | dev = dev_get_by_index_rcu(sock_net(sk), skb->skb_iif); | |
0b9aefea MRL |
217 | if (!dev || len >= dev->mtu) |
218 | pr_warn("%s: Driver has suspect GRO implementation, TCP performance may be compromised.\n", | |
219 | dev ? dev->name : "Unknown driver"); | |
dcb17d22 MRL |
220 | rcu_read_unlock(); |
221 | } | |
222 | } | |
223 | ||
e905a9ed | 224 | /* Adapt the MSS value used to make delayed ack decision to the |
1da177e4 | 225 | * real world. |
e905a9ed | 226 | */ |
056834d9 | 227 | static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb) |
1da177e4 | 228 | { |
463c84b9 | 229 | struct inet_connection_sock *icsk = inet_csk(sk); |
e905a9ed | 230 | const unsigned int lss = icsk->icsk_ack.last_seg_size; |
463c84b9 | 231 | unsigned int len; |
1da177e4 | 232 | |
e905a9ed | 233 | icsk->icsk_ack.last_seg_size = 0; |
1da177e4 LT |
234 | |
235 | /* skb->len may jitter because of SACKs, even if peer | |
236 | * sends good full-sized frames. | |
237 | */ | |
056834d9 | 238 | len = skb_shinfo(skb)->gso_size ? : skb->len; |
463c84b9 | 239 | if (len >= icsk->icsk_ack.rcv_mss) { |
dfa2f048 ED |
240 | /* Note: divides are still a bit expensive. |
241 | * For the moment, only adjust scaling_ratio | |
242 | * when we update icsk_ack.rcv_mss. | |
243 | */ | |
244 | if (unlikely(len != icsk->icsk_ack.rcv_mss)) { | |
245 | u64 val = (u64)skb->len << TCP_RMEM_TO_WIN_SCALE; | |
246 | ||
247 | do_div(val, skb->truesize); | |
248 | tcp_sk(sk)->scaling_ratio = val ? val : 1; | |
249 | } | |
dcb17d22 MRL |
250 | icsk->icsk_ack.rcv_mss = min_t(unsigned int, len, |
251 | tcp_sk(sk)->advmss); | |
0b9aefea MRL |
252 | /* Account for possibly-removed options */ |
253 | if (unlikely(len > icsk->icsk_ack.rcv_mss + | |
254 | MAX_TCP_OPTION_SPACE)) | |
255 | tcp_gro_dev_warn(sk, skb, len); | |
4720852e NC |
256 | /* If the skb has a len of exactly 1*MSS and has the PSH bit |
257 | * set then it is likely the end of an application write. So | |
258 | * more data may not be arriving soon, and yet the data sender | |
259 | * may be waiting for an ACK if cwnd-bound or using TX zero | |
260 | * copy. So we set ICSK_ACK_PUSHED here so that | |
261 | * tcp_cleanup_rbuf() will send an ACK immediately if the app | |
262 | * reads all of the data and is not ping-pong. If len > MSS | |
263 | * then this logic does not matter (and does not hurt) because | |
264 | * tcp_cleanup_rbuf() will always ACK immediately if the app | |
265 | * reads data and there is more than an MSS of unACKed data. | |
266 | */ | |
267 | if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_PSH) | |
268 | icsk->icsk_ack.pending |= ICSK_ACK_PUSHED; | |
1da177e4 LT |
269 | } else { |
270 | /* Otherwise, we make more careful check taking into account, | |
271 | * that SACKs block is variable. | |
272 | * | |
273 | * "len" is invariant segment length, including TCP header. | |
274 | */ | |
9c70220b | 275 | len += skb->data - skb_transport_header(skb); |
bee7ca9e | 276 | if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) || |
1da177e4 LT |
277 | /* If PSH is not set, packet should be |
278 | * full sized, provided peer TCP is not badly broken. | |
279 | * This observation (if it is correct 8)) allows | |
280 | * to handle super-low mtu links fairly. | |
281 | */ | |
282 | (len >= TCP_MIN_MSS + sizeof(struct tcphdr) && | |
aa8223c7 | 283 | !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) { |
1da177e4 LT |
284 | /* Subtract also invariant (if peer is RFC compliant), |
285 | * tcp header plus fixed timestamp option length. | |
286 | * Resulting "len" is MSS free of SACK jitter. | |
287 | */ | |
463c84b9 ACM |
288 | len -= tcp_sk(sk)->tcp_header_len; |
289 | icsk->icsk_ack.last_seg_size = len; | |
1da177e4 | 290 | if (len == lss) { |
463c84b9 | 291 | icsk->icsk_ack.rcv_mss = len; |
1da177e4 LT |
292 | return; |
293 | } | |
294 | } | |
1ef9696c AK |
295 | if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED) |
296 | icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2; | |
463c84b9 | 297 | icsk->icsk_ack.pending |= ICSK_ACK_PUSHED; |
1da177e4 LT |
298 | } |
299 | } | |
300 | ||
9a9c9b51 | 301 | static void tcp_incr_quickack(struct sock *sk, unsigned int max_quickacks) |
1da177e4 | 302 | { |
463c84b9 | 303 | struct inet_connection_sock *icsk = inet_csk(sk); |
95c96174 | 304 | unsigned int quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss); |
1da177e4 | 305 | |
056834d9 IJ |
306 | if (quickacks == 0) |
307 | quickacks = 2; | |
9a9c9b51 | 308 | quickacks = min(quickacks, max_quickacks); |
463c84b9 | 309 | if (quickacks > icsk->icsk_ack.quick) |
9a9c9b51 | 310 | icsk->icsk_ack.quick = quickacks; |
1da177e4 LT |
311 | } |
312 | ||
03b123de | 313 | static void tcp_enter_quickack_mode(struct sock *sk, unsigned int max_quickacks) |
1da177e4 | 314 | { |
463c84b9 | 315 | struct inet_connection_sock *icsk = inet_csk(sk); |
9a9c9b51 ED |
316 | |
317 | tcp_incr_quickack(sk, max_quickacks); | |
31954cd8 | 318 | inet_csk_exit_pingpong_mode(sk); |
463c84b9 | 319 | icsk->icsk_ack.ato = TCP_ATO_MIN; |
1da177e4 LT |
320 | } |
321 | ||
322 | /* Send ACKs quickly, if "quick" count is not exhausted | |
323 | * and the session is not interactive. | |
324 | */ | |
325 | ||
2251ae46 | 326 | static bool tcp_in_quickack_mode(struct sock *sk) |
1da177e4 | 327 | { |
463c84b9 | 328 | const struct inet_connection_sock *icsk = inet_csk(sk); |
2251ae46 | 329 | const struct dst_entry *dst = __sk_dst_get(sk); |
a2a385d6 | 330 | |
2251ae46 | 331 | return (dst && dst_metric(dst, RTAX_QUICKACK)) || |
31954cd8 | 332 | (icsk->icsk_ack.quick && !inet_csk_in_pingpong_mode(sk)); |
1da177e4 LT |
333 | } |
334 | ||
735d3831 | 335 | static void tcp_ecn_queue_cwr(struct tcp_sock *tp) |
bdf1ee5d | 336 | { |
056834d9 | 337 | if (tp->ecn_flags & TCP_ECN_OK) |
bdf1ee5d IJ |
338 | tp->ecn_flags |= TCP_ECN_QUEUE_CWR; |
339 | } | |
340 | ||
fd2123a3 | 341 | static void tcp_ecn_accept_cwr(struct sock *sk, const struct sk_buff *skb) |
bdf1ee5d | 342 | { |
9aee4000 | 343 | if (tcp_hdr(skb)->cwr) { |
fd2123a3 | 344 | tcp_sk(sk)->ecn_flags &= ~TCP_ECN_DEMAND_CWR; |
9aee4000 LB |
345 | |
346 | /* If the sender is telling us it has entered CWR, then its | |
347 | * cwnd may be very low (even just 1 packet), so we should ACK | |
348 | * immediately. | |
349 | */ | |
25702840 DK |
350 | if (TCP_SKB_CB(skb)->seq != TCP_SKB_CB(skb)->end_seq) |
351 | inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_NOW; | |
9aee4000 | 352 | } |
bdf1ee5d IJ |
353 | } |
354 | ||
735d3831 | 355 | static void tcp_ecn_withdraw_cwr(struct tcp_sock *tp) |
bdf1ee5d | 356 | { |
af38d07e | 357 | tp->ecn_flags &= ~TCP_ECN_QUEUE_CWR; |
bdf1ee5d IJ |
358 | } |
359 | ||
f4c9f85f | 360 | static void __tcp_ecn_check_ce(struct sock *sk, const struct sk_buff *skb) |
bdf1ee5d | 361 | { |
f4c9f85f YS |
362 | struct tcp_sock *tp = tcp_sk(sk); |
363 | ||
b82d1bb4 | 364 | switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) { |
7a269ffa | 365 | case INET_ECN_NOT_ECT: |
bdf1ee5d | 366 | /* Funny extension: if ECT is not set on a segment, |
7a269ffa ED |
367 | * and we already seen ECT on a previous segment, |
368 | * it is probably a retransmit. | |
369 | */ | |
370 | if (tp->ecn_flags & TCP_ECN_SEEN) | |
15ecbe94 | 371 | tcp_enter_quickack_mode(sk, 2); |
7a269ffa ED |
372 | break; |
373 | case INET_ECN_CE: | |
f4c9f85f YS |
374 | if (tcp_ca_needs_ecn(sk)) |
375 | tcp_ca_event(sk, CA_EVENT_ECN_IS_CE); | |
9890092e | 376 | |
aae06bf5 ED |
377 | if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR)) { |
378 | /* Better not delay acks, sender can have a very low cwnd */ | |
15ecbe94 | 379 | tcp_enter_quickack_mode(sk, 2); |
aae06bf5 ED |
380 | tp->ecn_flags |= TCP_ECN_DEMAND_CWR; |
381 | } | |
9890092e FW |
382 | tp->ecn_flags |= TCP_ECN_SEEN; |
383 | break; | |
7a269ffa | 384 | default: |
f4c9f85f YS |
385 | if (tcp_ca_needs_ecn(sk)) |
386 | tcp_ca_event(sk, CA_EVENT_ECN_NO_CE); | |
7a269ffa | 387 | tp->ecn_flags |= TCP_ECN_SEEN; |
9890092e | 388 | break; |
bdf1ee5d IJ |
389 | } |
390 | } | |
391 | ||
f4c9f85f | 392 | static void tcp_ecn_check_ce(struct sock *sk, const struct sk_buff *skb) |
735d3831 | 393 | { |
f4c9f85f YS |
394 | if (tcp_sk(sk)->ecn_flags & TCP_ECN_OK) |
395 | __tcp_ecn_check_ce(sk, skb); | |
735d3831 FW |
396 | } |
397 | ||
398 | static void tcp_ecn_rcv_synack(struct tcp_sock *tp, const struct tcphdr *th) | |
bdf1ee5d | 399 | { |
056834d9 | 400 | if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr)) |
bdf1ee5d IJ |
401 | tp->ecn_flags &= ~TCP_ECN_OK; |
402 | } | |
403 | ||
735d3831 | 404 | static void tcp_ecn_rcv_syn(struct tcp_sock *tp, const struct tcphdr *th) |
bdf1ee5d | 405 | { |
056834d9 | 406 | if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr)) |
bdf1ee5d IJ |
407 | tp->ecn_flags &= ~TCP_ECN_OK; |
408 | } | |
409 | ||
735d3831 | 410 | static bool tcp_ecn_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr *th) |
bdf1ee5d | 411 | { |
056834d9 | 412 | if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK)) |
a2a385d6 ED |
413 | return true; |
414 | return false; | |
bdf1ee5d IJ |
415 | } |
416 | ||
1da177e4 LT |
417 | /* Buffer size and advertised window tuning. |
418 | * | |
419 | * 1. Tuning sk->sk_sndbuf, when connection enters established state. | |
420 | */ | |
421 | ||
6ae70532 | 422 | static void tcp_sndbuf_expand(struct sock *sk) |
1da177e4 | 423 | { |
6ae70532 | 424 | const struct tcp_sock *tp = tcp_sk(sk); |
77bfc174 | 425 | const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops; |
6ae70532 ED |
426 | int sndmem, per_mss; |
427 | u32 nr_segs; | |
428 | ||
429 | /* Worst case is non GSO/TSO : each frame consumes one skb | |
430 | * and skb->head is kmalloced using power of two area of memory | |
431 | */ | |
432 | per_mss = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache) + | |
433 | MAX_TCP_HEADER + | |
434 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); | |
435 | ||
436 | per_mss = roundup_pow_of_two(per_mss) + | |
437 | SKB_DATA_ALIGN(sizeof(struct sk_buff)); | |
438 | ||
40570375 | 439 | nr_segs = max_t(u32, TCP_INIT_CWND, tcp_snd_cwnd(tp)); |
6ae70532 ED |
440 | nr_segs = max_t(u32, nr_segs, tp->reordering + 1); |
441 | ||
442 | /* Fast Recovery (RFC 5681 3.2) : | |
443 | * Cubic needs 1.7 factor, rounded to 2 to include | |
a9a08845 | 444 | * extra cushion (application might react slowly to EPOLLOUT) |
6ae70532 | 445 | */ |
77bfc174 YC |
446 | sndmem = ca_ops->sndbuf_expand ? ca_ops->sndbuf_expand(sk) : 2; |
447 | sndmem *= nr_segs * per_mss; | |
1da177e4 | 448 | |
06a59ecb | 449 | if (sk->sk_sndbuf < sndmem) |
e292f05e | 450 | WRITE_ONCE(sk->sk_sndbuf, |
02739545 | 451 | min(sndmem, READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_wmem[2]))); |
1da177e4 LT |
452 | } |
453 | ||
454 | /* 2. Tuning advertised window (window_clamp, rcv_ssthresh) | |
455 | * | |
456 | * All tcp_full_space() is split to two parts: "network" buffer, allocated | |
457 | * forward and advertised in receiver window (tp->rcv_wnd) and | |
458 | * "application buffer", required to isolate scheduling/application | |
459 | * latencies from network. | |
460 | * window_clamp is maximal advertised window. It can be less than | |
461 | * tcp_full_space(), in this case tcp_full_space() - window_clamp | |
462 | * is reserved for "application" buffer. The less window_clamp is | |
463 | * the smoother our behaviour from viewpoint of network, but the lower | |
464 | * throughput and the higher sensitivity of the connection to losses. 8) | |
465 | * | |
466 | * rcv_ssthresh is more strict window_clamp used at "slow start" | |
467 | * phase to predict further behaviour of this connection. | |
468 | * It is used for two goals: | |
469 | * - to enforce header prediction at sender, even when application | |
470 | * requires some significant "application buffer". It is check #1. | |
471 | * - to prevent pruning of receive queue because of misprediction | |
472 | * of receiver window. Check #2. | |
473 | * | |
474 | * The scheme does not work when sender sends good segments opening | |
caa20d9a | 475 | * window and then starts to feed us spaghetti. But it should work |
1da177e4 LT |
476 | * in common situations. Otherwise, we have to rely on queue collapsing. |
477 | */ | |
478 | ||
479 | /* Slow part of check#2. */ | |
240bfd13 ED |
480 | static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb, |
481 | unsigned int skbtruesize) | |
1da177e4 | 482 | { |
e9d9da91 | 483 | const struct tcp_sock *tp = tcp_sk(sk); |
1da177e4 | 484 | /* Optimize this! */ |
240bfd13 | 485 | int truesize = tcp_win_from_space(sk, skbtruesize) >> 1; |
02739545 | 486 | int window = tcp_win_from_space(sk, READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])) >> 1; |
1da177e4 LT |
487 | |
488 | while (tp->rcv_ssthresh <= window) { | |
489 | if (truesize <= skb->len) | |
463c84b9 | 490 | return 2 * inet_csk(sk)->icsk_ack.rcv_mss; |
1da177e4 LT |
491 | |
492 | truesize >>= 1; | |
493 | window >>= 1; | |
494 | } | |
495 | return 0; | |
496 | } | |
497 | ||
240bfd13 ED |
498 | /* Even if skb appears to have a bad len/truesize ratio, TCP coalescing |
499 | * can play nice with us, as sk_buff and skb->head might be either | |
500 | * freed or shared with up to MAX_SKB_FRAGS segments. | |
501 | * Only give a boost to drivers using page frag(s) to hold the frame(s), | |
502 | * and if no payload was pulled in skb->head before reaching us. | |
503 | */ | |
504 | static u32 truesize_adjust(bool adjust, const struct sk_buff *skb) | |
505 | { | |
506 | u32 truesize = skb->truesize; | |
507 | ||
508 | if (adjust && !skb_headlen(skb)) { | |
509 | truesize -= SKB_TRUESIZE(skb_end_offset(skb)); | |
510 | /* paranoid check, some drivers might be buggy */ | |
511 | if (unlikely((int)truesize < (int)skb->len)) | |
512 | truesize = skb->truesize; | |
513 | } | |
514 | return truesize; | |
515 | } | |
516 | ||
517 | static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb, | |
518 | bool adjust) | |
1da177e4 | 519 | { |
9e412ba7 | 520 | struct tcp_sock *tp = tcp_sk(sk); |
50ce163a ED |
521 | int room; |
522 | ||
523 | room = min_t(int, tp->window_clamp, tcp_space(sk)) - tp->rcv_ssthresh; | |
9e412ba7 | 524 | |
053f3684 WW |
525 | if (room <= 0) |
526 | return; | |
527 | ||
1da177e4 | 528 | /* Check #1 */ |
053f3684 | 529 | if (!tcp_under_memory_pressure(sk)) { |
240bfd13 | 530 | unsigned int truesize = truesize_adjust(adjust, skb); |
1da177e4 LT |
531 | int incr; |
532 | ||
533 | /* Check #2. Increase window, if skb with such overhead | |
534 | * will fit to rcvbuf in future. | |
535 | */ | |
240bfd13 | 536 | if (tcp_win_from_space(sk, truesize) <= skb->len) |
056834d9 | 537 | incr = 2 * tp->advmss; |
1da177e4 | 538 | else |
240bfd13 | 539 | incr = __tcp_grow_window(sk, skb, truesize); |
1da177e4 LT |
540 | |
541 | if (incr) { | |
4d846f02 | 542 | incr = max_t(int, incr, 2 * skb->len); |
50ce163a | 543 | tp->rcv_ssthresh += min(room, incr); |
463c84b9 | 544 | inet_csk(sk)->icsk_ack.quick |= 1; |
1da177e4 | 545 | } |
053f3684 WW |
546 | } else { |
547 | /* Under pressure: | |
548 | * Adjust rcv_ssthresh according to reserved mem | |
549 | */ | |
550 | tcp_adjust_rcv_ssthresh(sk); | |
1da177e4 LT |
551 | } |
552 | } | |
553 | ||
a337531b | 554 | /* 3. Try to fixup all. It is made immediately after connection enters |
1da177e4 LT |
555 | * established state. |
556 | */ | |
bc183dec | 557 | static void tcp_init_buffer_space(struct sock *sk) |
1da177e4 | 558 | { |
02ca527a | 559 | int tcp_app_win = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_app_win); |
1da177e4 LT |
560 | struct tcp_sock *tp = tcp_sk(sk); |
561 | int maxwin; | |
562 | ||
1da177e4 | 563 | if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK)) |
6ae70532 | 564 | tcp_sndbuf_expand(sk); |
1da177e4 | 565 | |
9a568de4 | 566 | tcp_mstamp_refresh(tp); |
645f4c6f | 567 | tp->rcvq_space.time = tp->tcp_mstamp; |
b0983d3c | 568 | tp->rcvq_space.seq = tp->copied_seq; |
1da177e4 LT |
569 | |
570 | maxwin = tcp_full_space(sk); | |
571 | ||
572 | if (tp->window_clamp >= maxwin) { | |
573 | tp->window_clamp = maxwin; | |
574 | ||
0c12654a | 575 | if (tcp_app_win && maxwin > 4 * tp->advmss) |
1da177e4 | 576 | tp->window_clamp = max(maxwin - |
0c12654a | 577 | (maxwin >> tcp_app_win), |
1da177e4 LT |
578 | 4 * tp->advmss); |
579 | } | |
580 | ||
581 | /* Force reservation of one segment. */ | |
0c12654a | 582 | if (tcp_app_win && |
1da177e4 LT |
583 | tp->window_clamp > 2 * tp->advmss && |
584 | tp->window_clamp + tp->advmss > maxwin) | |
585 | tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss); | |
586 | ||
587 | tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp); | |
c2203cf7 | 588 | tp->snd_cwnd_stamp = tcp_jiffies32; |
72d05c00 ED |
589 | tp->rcvq_space.space = min3(tp->rcv_ssthresh, tp->rcv_wnd, |
590 | (u32)TCP_INIT_CWND * tp->advmss); | |
1da177e4 LT |
591 | } |
592 | ||
a337531b | 593 | /* 4. Recalculate window clamp after socket hit its memory bounds. */ |
9e412ba7 | 594 | static void tcp_clamp_window(struct sock *sk) |
1da177e4 | 595 | { |
9e412ba7 | 596 | struct tcp_sock *tp = tcp_sk(sk); |
6687e988 | 597 | struct inet_connection_sock *icsk = inet_csk(sk); |
356d1833 | 598 | struct net *net = sock_net(sk); |
02739545 | 599 | int rmem2; |
1da177e4 | 600 | |
6687e988 | 601 | icsk->icsk_ack.quick = 0; |
02739545 | 602 | rmem2 = READ_ONCE(net->ipv4.sysctl_tcp_rmem[2]); |
1da177e4 | 603 | |
02739545 | 604 | if (sk->sk_rcvbuf < rmem2 && |
326f36e9 | 605 | !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) && |
b8da51eb | 606 | !tcp_under_memory_pressure(sk) && |
180d8cd9 | 607 | sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)) { |
ebb3b78d | 608 | WRITE_ONCE(sk->sk_rcvbuf, |
02739545 | 609 | min(atomic_read(&sk->sk_rmem_alloc), rmem2)); |
1da177e4 | 610 | } |
326f36e9 | 611 | if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf) |
056834d9 | 612 | tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss); |
1da177e4 LT |
613 | } |
614 | ||
40efc6fa SH |
615 | /* Initialize RCV_MSS value. |
616 | * RCV_MSS is an our guess about MSS used by the peer. | |
617 | * We haven't any direct information about the MSS. | |
618 | * It's better to underestimate the RCV_MSS rather than overestimate. | |
619 | * Overestimations make us ACKing less frequently than needed. | |
620 | * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss(). | |
621 | */ | |
622 | void tcp_initialize_rcv_mss(struct sock *sk) | |
623 | { | |
cf533ea5 | 624 | const struct tcp_sock *tp = tcp_sk(sk); |
40efc6fa SH |
625 | unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache); |
626 | ||
056834d9 | 627 | hint = min(hint, tp->rcv_wnd / 2); |
bee7ca9e | 628 | hint = min(hint, TCP_MSS_DEFAULT); |
40efc6fa SH |
629 | hint = max(hint, TCP_MIN_MSS); |
630 | ||
631 | inet_csk(sk)->icsk_ack.rcv_mss = hint; | |
632 | } | |
4bc2f18b | 633 | EXPORT_SYMBOL(tcp_initialize_rcv_mss); |
40efc6fa | 634 | |
1da177e4 LT |
635 | /* Receiver "autotuning" code. |
636 | * | |
637 | * The algorithm for RTT estimation w/o timestamps is based on | |
638 | * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL. | |
7a6498eb | 639 | * <https://public.lanl.gov/radiant/pubs.html#DRS> |
1da177e4 LT |
640 | * |
641 | * More detail on this code can be found at | |
631dd1a8 | 642 | * <http://staff.psc.edu/jheffner/>, |
1da177e4 LT |
643 | * though this reference is out of date. A new paper |
644 | * is pending. | |
645 | */ | |
646 | static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep) | |
647 | { | |
645f4c6f | 648 | u32 new_sample = tp->rcv_rtt_est.rtt_us; |
1da177e4 LT |
649 | long m = sample; |
650 | ||
1da177e4 LT |
651 | if (new_sample != 0) { |
652 | /* If we sample in larger samples in the non-timestamp | |
653 | * case, we could grossly overestimate the RTT especially | |
654 | * with chatty applications or bulk transfer apps which | |
655 | * are stalled on filesystem I/O. | |
656 | * | |
657 | * Also, since we are only going for a minimum in the | |
31f34269 | 658 | * non-timestamp case, we do not smooth things out |
caa20d9a | 659 | * else with timestamps disabled convergence takes too |
1da177e4 LT |
660 | * long. |
661 | */ | |
662 | if (!win_dep) { | |
663 | m -= (new_sample >> 3); | |
664 | new_sample += m; | |
18a223e0 NC |
665 | } else { |
666 | m <<= 3; | |
667 | if (m < new_sample) | |
668 | new_sample = m; | |
669 | } | |
1da177e4 | 670 | } else { |
caa20d9a | 671 | /* No previous measure. */ |
1da177e4 LT |
672 | new_sample = m << 3; |
673 | } | |
674 | ||
645f4c6f | 675 | tp->rcv_rtt_est.rtt_us = new_sample; |
1da177e4 LT |
676 | } |
677 | ||
678 | static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp) | |
679 | { | |
645f4c6f ED |
680 | u32 delta_us; |
681 | ||
9a568de4 | 682 | if (tp->rcv_rtt_est.time == 0) |
1da177e4 LT |
683 | goto new_measure; |
684 | if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq)) | |
685 | return; | |
9a568de4 | 686 | delta_us = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcv_rtt_est.time); |
9ee11bd0 WW |
687 | if (!delta_us) |
688 | delta_us = 1; | |
645f4c6f | 689 | tcp_rcv_rtt_update(tp, delta_us, 1); |
1da177e4 LT |
690 | |
691 | new_measure: | |
692 | tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd; | |
645f4c6f | 693 | tp->rcv_rtt_est.time = tp->tcp_mstamp; |
1da177e4 LT |
694 | } |
695 | ||
056834d9 IJ |
696 | static inline void tcp_rcv_rtt_measure_ts(struct sock *sk, |
697 | const struct sk_buff *skb) | |
1da177e4 | 698 | { |
463c84b9 | 699 | struct tcp_sock *tp = tcp_sk(sk); |
9a568de4 | 700 | |
3f6c65d6 WW |
701 | if (tp->rx_opt.rcv_tsecr == tp->rcv_rtt_last_tsecr) |
702 | return; | |
703 | tp->rcv_rtt_last_tsecr = tp->rx_opt.rcv_tsecr; | |
704 | ||
705 | if (TCP_SKB_CB(skb)->end_seq - | |
706 | TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss) { | |
9a568de4 | 707 | u32 delta = tcp_time_stamp(tp) - tp->rx_opt.rcv_tsecr; |
9ee11bd0 | 708 | u32 delta_us; |
9a568de4 | 709 | |
9efdda4e ED |
710 | if (likely(delta < INT_MAX / (USEC_PER_SEC / TCP_TS_HZ))) { |
711 | if (!delta) | |
712 | delta = 1; | |
713 | delta_us = delta * (USEC_PER_SEC / TCP_TS_HZ); | |
714 | tcp_rcv_rtt_update(tp, delta_us, 0); | |
715 | } | |
9a568de4 | 716 | } |
1da177e4 LT |
717 | } |
718 | ||
719 | /* | |
720 | * This function should be called every time data is copied to user space. | |
721 | * It calculates the appropriate TCP receive buffer space. | |
722 | */ | |
723 | void tcp_rcv_space_adjust(struct sock *sk) | |
724 | { | |
725 | struct tcp_sock *tp = tcp_sk(sk); | |
607065ba | 726 | u32 copied; |
1da177e4 | 727 | int time; |
e905a9ed | 728 | |
6163849d YS |
729 | trace_tcp_rcv_space_adjust(sk); |
730 | ||
86323850 | 731 | tcp_mstamp_refresh(tp); |
9a568de4 | 732 | time = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcvq_space.time); |
645f4c6f | 733 | if (time < (tp->rcv_rtt_est.rtt_us >> 3) || tp->rcv_rtt_est.rtt_us == 0) |
1da177e4 | 734 | return; |
e905a9ed | 735 | |
b0983d3c ED |
736 | /* Number of bytes copied to user in last RTT */ |
737 | copied = tp->copied_seq - tp->rcvq_space.seq; | |
738 | if (copied <= tp->rcvq_space.space) | |
739 | goto new_measure; | |
740 | ||
741 | /* A bit of theory : | |
742 | * copied = bytes received in previous RTT, our base window | |
743 | * To cope with packet losses, we need a 2x factor | |
744 | * To cope with slow start, and sender growing its cwin by 100 % | |
745 | * every RTT, we need a 4x factor, because the ACK we are sending | |
746 | * now is for the next RTT, not the current one : | |
747 | * <prev RTT . ><current RTT .. ><next RTT .... > | |
748 | */ | |
749 | ||
78047648 | 750 | if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) && |
b0983d3c | 751 | !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { |
c3916ad9 | 752 | u64 rcvwin, grow; |
dfa2f048 | 753 | int rcvbuf; |
1da177e4 | 754 | |
b0983d3c ED |
755 | /* minimal window to cope with packet losses, assuming |
756 | * steady state. Add some cushion because of small variations. | |
757 | */ | |
607065ba | 758 | rcvwin = ((u64)copied << 1) + 16 * tp->advmss; |
1da177e4 | 759 | |
c3916ad9 ED |
760 | /* Accommodate for sender rate increase (eg. slow start) */ |
761 | grow = rcvwin * (copied - tp->rcvq_space.space); | |
762 | do_div(grow, tp->rcvq_space.space); | |
763 | rcvwin += (grow << 1); | |
1da177e4 | 764 | |
dfa2f048 | 765 | rcvbuf = min_t(u64, tcp_space_from_win(sk, rcvwin), |
02739545 | 766 | READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); |
b0983d3c | 767 | if (rcvbuf > sk->sk_rcvbuf) { |
ebb3b78d | 768 | WRITE_ONCE(sk->sk_rcvbuf, rcvbuf); |
1da177e4 | 769 | |
b0983d3c | 770 | /* Make the window clamp follow along. */ |
02db5571 | 771 | tp->window_clamp = tcp_win_from_space(sk, rcvbuf); |
1da177e4 LT |
772 | } |
773 | } | |
b0983d3c | 774 | tp->rcvq_space.space = copied; |
e905a9ed | 775 | |
1da177e4 LT |
776 | new_measure: |
777 | tp->rcvq_space.seq = tp->copied_seq; | |
645f4c6f | 778 | tp->rcvq_space.time = tp->tcp_mstamp; |
1da177e4 LT |
779 | } |
780 | ||
781 | /* There is something which you must keep in mind when you analyze the | |
782 | * behavior of the tp->ato delayed ack timeout interval. When a | |
783 | * connection starts up, we want to ack as quickly as possible. The | |
784 | * problem is that "good" TCP's do slow start at the beginning of data | |
785 | * transmission. The means that until we send the first few ACK's the | |
786 | * sender will sit on his end and only queue most of his data, because | |
787 | * he can only send snd_cwnd unacked packets at any given time. For | |
788 | * each ACK we send, he increments snd_cwnd and transmits more of his | |
789 | * queue. -DaveM | |
790 | */ | |
9e412ba7 | 791 | static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb) |
1da177e4 | 792 | { |
9e412ba7 | 793 | struct tcp_sock *tp = tcp_sk(sk); |
463c84b9 | 794 | struct inet_connection_sock *icsk = inet_csk(sk); |
1da177e4 LT |
795 | u32 now; |
796 | ||
463c84b9 | 797 | inet_csk_schedule_ack(sk); |
1da177e4 | 798 | |
463c84b9 | 799 | tcp_measure_rcv_mss(sk, skb); |
1da177e4 LT |
800 | |
801 | tcp_rcv_rtt_measure(tp); | |
e905a9ed | 802 | |
70eabf0e | 803 | now = tcp_jiffies32; |
1da177e4 | 804 | |
463c84b9 | 805 | if (!icsk->icsk_ack.ato) { |
1da177e4 LT |
806 | /* The _first_ data packet received, initialize |
807 | * delayed ACK engine. | |
808 | */ | |
9a9c9b51 | 809 | tcp_incr_quickack(sk, TCP_MAX_QUICKACKS); |
463c84b9 | 810 | icsk->icsk_ack.ato = TCP_ATO_MIN; |
1da177e4 | 811 | } else { |
463c84b9 | 812 | int m = now - icsk->icsk_ack.lrcvtime; |
1da177e4 | 813 | |
056834d9 | 814 | if (m <= TCP_ATO_MIN / 2) { |
1da177e4 | 815 | /* The fastest case is the first. */ |
463c84b9 ACM |
816 | icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2; |
817 | } else if (m < icsk->icsk_ack.ato) { | |
818 | icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m; | |
819 | if (icsk->icsk_ack.ato > icsk->icsk_rto) | |
820 | icsk->icsk_ack.ato = icsk->icsk_rto; | |
821 | } else if (m > icsk->icsk_rto) { | |
caa20d9a | 822 | /* Too long gap. Apparently sender failed to |
1da177e4 LT |
823 | * restart window, so that we send ACKs quickly. |
824 | */ | |
9a9c9b51 | 825 | tcp_incr_quickack(sk, TCP_MAX_QUICKACKS); |
1da177e4 LT |
826 | } |
827 | } | |
463c84b9 | 828 | icsk->icsk_ack.lrcvtime = now; |
1da177e4 | 829 | |
f4c9f85f | 830 | tcp_ecn_check_ce(sk, skb); |
1da177e4 LT |
831 | |
832 | if (skb->len >= 128) | |
240bfd13 | 833 | tcp_grow_window(sk, skb, true); |
1da177e4 LT |
834 | } |
835 | ||
1da177e4 LT |
836 | /* Called to compute a smoothed rtt estimate. The data fed to this |
837 | * routine either comes from timestamps, or from segments that were | |
838 | * known _not_ to have been retransmitted [see Karn/Partridge | |
839 | * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88 | |
840 | * piece by Van Jacobson. | |
841 | * NOTE: the next three routines used to be one big routine. | |
842 | * To save cycles in the RFC 1323 implementation it was better to break | |
843 | * it up into three procedures. -- erics | |
844 | */ | |
740b0f18 | 845 | static void tcp_rtt_estimator(struct sock *sk, long mrtt_us) |
1da177e4 | 846 | { |
6687e988 | 847 | struct tcp_sock *tp = tcp_sk(sk); |
740b0f18 ED |
848 | long m = mrtt_us; /* RTT */ |
849 | u32 srtt = tp->srtt_us; | |
1da177e4 | 850 | |
1da177e4 LT |
851 | /* The following amusing code comes from Jacobson's |
852 | * article in SIGCOMM '88. Note that rtt and mdev | |
853 | * are scaled versions of rtt and mean deviation. | |
e905a9ed | 854 | * This is designed to be as fast as possible |
1da177e4 LT |
855 | * m stands for "measurement". |
856 | * | |
857 | * On a 1990 paper the rto value is changed to: | |
858 | * RTO = rtt + 4 * mdev | |
859 | * | |
860 | * Funny. This algorithm seems to be very broken. | |
861 | * These formulae increase RTO, when it should be decreased, increase | |
31f34269 | 862 | * too slowly, when it should be increased quickly, decrease too quickly |
1da177e4 LT |
863 | * etc. I guess in BSD RTO takes ONE value, so that it is absolutely |
864 | * does not matter how to _calculate_ it. Seems, it was trap | |
865 | * that VJ failed to avoid. 8) | |
866 | */ | |
4a5ab4e2 ED |
867 | if (srtt != 0) { |
868 | m -= (srtt >> 3); /* m is now error in rtt est */ | |
869 | srtt += m; /* rtt = 7/8 rtt + 1/8 new */ | |
1da177e4 LT |
870 | if (m < 0) { |
871 | m = -m; /* m is now abs(error) */ | |
740b0f18 | 872 | m -= (tp->mdev_us >> 2); /* similar update on mdev */ |
1da177e4 LT |
873 | /* This is similar to one of Eifel findings. |
874 | * Eifel blocks mdev updates when rtt decreases. | |
875 | * This solution is a bit different: we use finer gain | |
876 | * for mdev in this case (alpha*beta). | |
877 | * Like Eifel it also prevents growth of rto, | |
878 | * but also it limits too fast rto decreases, | |
879 | * happening in pure Eifel. | |
880 | */ | |
881 | if (m > 0) | |
882 | m >>= 3; | |
883 | } else { | |
740b0f18 | 884 | m -= (tp->mdev_us >> 2); /* similar update on mdev */ |
1da177e4 | 885 | } |
740b0f18 ED |
886 | tp->mdev_us += m; /* mdev = 3/4 mdev + 1/4 new */ |
887 | if (tp->mdev_us > tp->mdev_max_us) { | |
888 | tp->mdev_max_us = tp->mdev_us; | |
889 | if (tp->mdev_max_us > tp->rttvar_us) | |
890 | tp->rttvar_us = tp->mdev_max_us; | |
1da177e4 LT |
891 | } |
892 | if (after(tp->snd_una, tp->rtt_seq)) { | |
740b0f18 ED |
893 | if (tp->mdev_max_us < tp->rttvar_us) |
894 | tp->rttvar_us -= (tp->rttvar_us - tp->mdev_max_us) >> 2; | |
1da177e4 | 895 | tp->rtt_seq = tp->snd_nxt; |
740b0f18 | 896 | tp->mdev_max_us = tcp_rto_min_us(sk); |
23729ff2 SF |
897 | |
898 | tcp_bpf_rtt(sk); | |
1da177e4 LT |
899 | } |
900 | } else { | |
901 | /* no previous measure. */ | |
4a5ab4e2 | 902 | srtt = m << 3; /* take the measured time to be rtt */ |
740b0f18 ED |
903 | tp->mdev_us = m << 1; /* make sure rto = 3*rtt */ |
904 | tp->rttvar_us = max(tp->mdev_us, tcp_rto_min_us(sk)); | |
905 | tp->mdev_max_us = tp->rttvar_us; | |
1da177e4 | 906 | tp->rtt_seq = tp->snd_nxt; |
23729ff2 SF |
907 | |
908 | tcp_bpf_rtt(sk); | |
1da177e4 | 909 | } |
740b0f18 | 910 | tp->srtt_us = max(1U, srtt); |
1da177e4 LT |
911 | } |
912 | ||
95bd09eb ED |
913 | static void tcp_update_pacing_rate(struct sock *sk) |
914 | { | |
915 | const struct tcp_sock *tp = tcp_sk(sk); | |
916 | u64 rate; | |
917 | ||
918 | /* set sk_pacing_rate to 200 % of current rate (mss * cwnd / srtt) */ | |
43e122b0 ED |
919 | rate = (u64)tp->mss_cache * ((USEC_PER_SEC / 100) << 3); |
920 | ||
921 | /* current rate is (cwnd * mss) / srtt | |
922 | * In Slow Start [1], set sk_pacing_rate to 200 % the current rate. | |
923 | * In Congestion Avoidance phase, set it to 120 % the current rate. | |
924 | * | |
925 | * [1] : Normal Slow Start condition is (tp->snd_cwnd < tp->snd_ssthresh) | |
926 | * If snd_cwnd >= (tp->snd_ssthresh / 2), we are approaching | |
927 | * end of slow start and should slow down. | |
928 | */ | |
40570375 | 929 | if (tcp_snd_cwnd(tp) < tp->snd_ssthresh / 2) |
59bf6c65 | 930 | rate *= READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_pacing_ss_ratio); |
43e122b0 | 931 | else |
59bf6c65 | 932 | rate *= READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_pacing_ca_ratio); |
95bd09eb | 933 | |
40570375 | 934 | rate *= max(tcp_snd_cwnd(tp), tp->packets_out); |
95bd09eb | 935 | |
740b0f18 ED |
936 | if (likely(tp->srtt_us)) |
937 | do_div(rate, tp->srtt_us); | |
95bd09eb | 938 | |
a9da6f29 | 939 | /* WRITE_ONCE() is needed because sch_fq fetches sk_pacing_rate |
ba537427 ED |
940 | * without any lock. We want to make sure compiler wont store |
941 | * intermediate values in this location. | |
942 | */ | |
a9da6f29 MR |
943 | WRITE_ONCE(sk->sk_pacing_rate, min_t(u64, rate, |
944 | sk->sk_max_pacing_rate)); | |
95bd09eb ED |
945 | } |
946 | ||
1da177e4 LT |
947 | /* Calculate rto without backoff. This is the second half of Van Jacobson's |
948 | * routine referred to above. | |
949 | */ | |
f7e56a76 | 950 | static void tcp_set_rto(struct sock *sk) |
1da177e4 | 951 | { |
463c84b9 | 952 | const struct tcp_sock *tp = tcp_sk(sk); |
1da177e4 LT |
953 | /* Old crap is replaced with new one. 8) |
954 | * | |
955 | * More seriously: | |
956 | * 1. If rtt variance happened to be less 50msec, it is hallucination. | |
957 | * It cannot be less due to utterly erratic ACK generation made | |
958 | * at least by solaris and freebsd. "Erratic ACKs" has _nothing_ | |
959 | * to do with delayed acks, because at cwnd>2 true delack timeout | |
960 | * is invisible. Actually, Linux-2.4 also generates erratic | |
caa20d9a | 961 | * ACKs in some circumstances. |
1da177e4 | 962 | */ |
f1ecd5d9 | 963 | inet_csk(sk)->icsk_rto = __tcp_set_rto(tp); |
1da177e4 LT |
964 | |
965 | /* 2. Fixups made earlier cannot be right. | |
966 | * If we do not estimate RTO correctly without them, | |
967 | * all the algo is pure shit and should be replaced | |
caa20d9a | 968 | * with correct one. It is exactly, which we pretend to do. |
1da177e4 | 969 | */ |
1da177e4 | 970 | |
ee6aac59 IJ |
971 | /* NOTE: clamping at TCP_RTO_MIN is not required, current algo |
972 | * guarantees that rto is higher. | |
973 | */ | |
f1ecd5d9 | 974 | tcp_bound_rto(sk); |
1da177e4 LT |
975 | } |
976 | ||
cf533ea5 | 977 | __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst) |
1da177e4 LT |
978 | { |
979 | __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0); | |
980 | ||
22b71c8f | 981 | if (!cwnd) |
442b9635 | 982 | cwnd = TCP_INIT_CWND; |
1da177e4 LT |
983 | return min_t(__u32, cwnd, tp->snd_cwnd_clamp); |
984 | } | |
985 | ||
a71d77e6 PJ |
986 | struct tcp_sacktag_state { |
987 | /* Timestamps for earliest and latest never-retransmitted segment | |
988 | * that was SACKed. RTO needs the earliest RTT to stay conservative, | |
989 | * but congestion control should still get an accurate delay signal. | |
990 | */ | |
991 | u64 first_sackt; | |
992 | u64 last_sackt; | |
993 | u32 reord; | |
994 | u32 sack_delivered; | |
995 | int flag; | |
996 | unsigned int mss_now; | |
997 | struct rate_sample *rate; | |
998 | }; | |
999 | ||
ad2b9b0f PJ |
1000 | /* Take a notice that peer is sending D-SACKs. Skip update of data delivery |
1001 | * and spurious retransmission information if this DSACK is unlikely caused by | |
1002 | * sender's action: | |
1003 | * - DSACKed sequence range is larger than maximum receiver's window. | |
1004 | * - Total no. of DSACKed segments exceed the total no. of retransmitted segs. | |
1005 | */ | |
a71d77e6 PJ |
1006 | static u32 tcp_dsack_seen(struct tcp_sock *tp, u32 start_seq, |
1007 | u32 end_seq, struct tcp_sacktag_state *state) | |
e60402d0 | 1008 | { |
a71d77e6 PJ |
1009 | u32 seq_len, dup_segs = 1; |
1010 | ||
ad2b9b0f PJ |
1011 | if (!before(start_seq, end_seq)) |
1012 | return 0; | |
1013 | ||
1014 | seq_len = end_seq - start_seq; | |
1015 | /* Dubious DSACK: DSACKed range greater than maximum advertised rwnd */ | |
1016 | if (seq_len > tp->max_window) | |
1017 | return 0; | |
1018 | if (seq_len > tp->mss_cache) | |
1019 | dup_segs = DIV_ROUND_UP(seq_len, tp->mss_cache); | |
63f367d9 YC |
1020 | else if (tp->tlp_high_seq && tp->tlp_high_seq == end_seq) |
1021 | state->flag |= FLAG_DSACK_TLP; | |
ad2b9b0f PJ |
1022 | |
1023 | tp->dsack_dups += dup_segs; | |
1024 | /* Skip the DSACK if dup segs weren't retransmitted by sender */ | |
1025 | if (tp->dsack_dups > tp->total_retrans) | |
1026 | return 0; | |
a71d77e6 | 1027 | |
ab56222a | 1028 | tp->rx_opt.sack_ok |= TCP_DSACK_SEEN; |
a657db03 NC |
1029 | /* We increase the RACK ordering window in rounds where we receive |
1030 | * DSACKs that may have been due to reordering causing RACK to trigger | |
1031 | * a spurious fast recovery. Thus RACK ignores DSACKs that happen | |
1032 | * without having seen reordering, or that match TLP probes (TLP | |
1033 | * is timer-driven, not triggered by RACK). | |
1034 | */ | |
1035 | if (tp->reord_seen && !(state->flag & FLAG_DSACK_TLP)) | |
1036 | tp->rack.dsack_seen = 1; | |
a71d77e6 PJ |
1037 | |
1038 | state->flag |= FLAG_DSACKING_ACK; | |
1039 | /* A spurious retransmission is delivered */ | |
1040 | state->sack_delivered += dup_segs; | |
1041 | ||
1042 | return dup_segs; | |
e60402d0 IJ |
1043 | } |
1044 | ||
737ff314 YC |
1045 | /* It's reordering when higher sequence was delivered (i.e. sacked) before |
1046 | * some lower never-retransmitted sequence ("low_seq"). The maximum reordering | |
1047 | * distance is approximated in full-mss packet distance ("reordering"). | |
1048 | */ | |
1049 | static void tcp_check_sack_reordering(struct sock *sk, const u32 low_seq, | |
1050 | const int ts) | |
1da177e4 | 1051 | { |
6687e988 | 1052 | struct tcp_sock *tp = tcp_sk(sk); |
737ff314 YC |
1053 | const u32 mss = tp->mss_cache; |
1054 | u32 fack, metric; | |
40b215e5 | 1055 | |
737ff314 YC |
1056 | fack = tcp_highest_sack_seq(tp); |
1057 | if (!before(low_seq, fack)) | |
6f5b24ee SHY |
1058 | return; |
1059 | ||
737ff314 YC |
1060 | metric = fack - low_seq; |
1061 | if ((metric > tp->reordering * mss) && mss) { | |
1da177e4 | 1062 | #if FASTRETRANS_DEBUG > 1 |
91df42be JP |
1063 | pr_debug("Disorder%d %d %u f%u s%u rr%d\n", |
1064 | tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state, | |
1065 | tp->reordering, | |
737ff314 | 1066 | 0, |
91df42be JP |
1067 | tp->sacked_out, |
1068 | tp->undo_marker ? tp->undo_retrans : 0); | |
1da177e4 | 1069 | #endif |
737ff314 | 1070 | tp->reordering = min_t(u32, (metric + mss - 1) / mss, |
a11e5b3e | 1071 | READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_max_reordering)); |
1da177e4 | 1072 | } |
eed530b6 | 1073 | |
2d2517ee | 1074 | /* This exciting event is worth to be remembered. 8) */ |
7ec65372 | 1075 | tp->reord_seen++; |
737ff314 YC |
1076 | NET_INC_STATS(sock_net(sk), |
1077 | ts ? LINUX_MIB_TCPTSREORDER : LINUX_MIB_TCPSACKREORDER); | |
1da177e4 LT |
1078 | } |
1079 | ||
68698970 YC |
1080 | /* This must be called before lost_out or retrans_out are updated |
1081 | * on a new loss, because we want to know if all skbs previously | |
1082 | * known to be lost have already been retransmitted, indicating | |
1083 | * that this newly lost skb is our next skb to retransmit. | |
1084 | */ | |
c8c213f2 IJ |
1085 | static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb) |
1086 | { | |
e176b1ba PY |
1087 | if ((!tp->retransmit_skb_hint && tp->retrans_out >= tp->lost_out) || |
1088 | (tp->retransmit_skb_hint && | |
1089 | before(TCP_SKB_CB(skb)->seq, | |
1090 | TCP_SKB_CB(tp->retransmit_skb_hint)->seq))) | |
006f582c | 1091 | tp->retransmit_skb_hint = skb; |
c8c213f2 IJ |
1092 | } |
1093 | ||
9cd8b6c9 YC |
1094 | /* Sum the number of packets on the wire we have marked as lost, and |
1095 | * notify the congestion control module that the given skb was marked lost. | |
1096 | */ | |
1097 | static void tcp_notify_skb_loss_event(struct tcp_sock *tp, const struct sk_buff *skb) | |
1098 | { | |
1099 | tp->lost += tcp_skb_pcount(skb); | |
1100 | } | |
1101 | ||
fd214674 YC |
1102 | void tcp_mark_skb_lost(struct sock *sk, struct sk_buff *skb) |
1103 | { | |
68698970 | 1104 | __u8 sacked = TCP_SKB_CB(skb)->sacked; |
fd214674 YC |
1105 | struct tcp_sock *tp = tcp_sk(sk); |
1106 | ||
68698970 YC |
1107 | if (sacked & TCPCB_SACKED_ACKED) |
1108 | return; | |
0682e690 | 1109 | |
68698970 YC |
1110 | tcp_verify_retransmit_hint(tp, skb); |
1111 | if (sacked & TCPCB_LOST) { | |
1112 | if (sacked & TCPCB_SACKED_RETRANS) { | |
1113 | /* Account for retransmits that are lost again */ | |
1114 | TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS; | |
1115 | tp->retrans_out -= tcp_skb_pcount(skb); | |
1116 | NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPLOSTRETRANSMIT, | |
1117 | tcp_skb_pcount(skb)); | |
9cd8b6c9 | 1118 | tcp_notify_skb_loss_event(tp, skb); |
68698970 YC |
1119 | } |
1120 | } else { | |
1121 | tp->lost_out += tcp_skb_pcount(skb); | |
1122 | TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; | |
9cd8b6c9 | 1123 | tcp_notify_skb_loss_event(tp, skb); |
68698970 | 1124 | } |
0682e690 NC |
1125 | } |
1126 | ||
082d4fa9 YS |
1127 | /* Updates the delivered and delivered_ce counts */ |
1128 | static void tcp_count_delivered(struct tcp_sock *tp, u32 delivered, | |
1129 | bool ece_ack) | |
1130 | { | |
1131 | tp->delivered += delivered; | |
1132 | if (ece_ack) | |
1133 | tp->delivered_ce += delivered; | |
1134 | } | |
1135 | ||
1da177e4 LT |
1136 | /* This procedure tags the retransmission queue when SACKs arrive. |
1137 | * | |
1138 | * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L). | |
1139 | * Packets in queue with these bits set are counted in variables | |
1140 | * sacked_out, retrans_out and lost_out, correspondingly. | |
1141 | * | |
1142 | * Valid combinations are: | |
1143 | * Tag InFlight Description | |
1144 | * 0 1 - orig segment is in flight. | |
1145 | * S 0 - nothing flies, orig reached receiver. | |
1146 | * L 0 - nothing flies, orig lost by net. | |
1147 | * R 2 - both orig and retransmit are in flight. | |
1148 | * L|R 1 - orig is lost, retransmit is in flight. | |
1149 | * S|R 1 - orig reached receiver, retrans is still in flight. | |
1150 | * (L|S|R is logically valid, it could occur when L|R is sacked, | |
1151 | * but it is equivalent to plain S and code short-curcuits it to S. | |
1152 | * L|S is logically invalid, it would mean -1 packet in flight 8)) | |
1153 | * | |
1154 | * These 6 states form finite state machine, controlled by the following events: | |
1155 | * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue()) | |
1156 | * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue()) | |
974c1236 | 1157 | * 3. Loss detection event of two flavors: |
1da177e4 LT |
1158 | * A. Scoreboard estimator decided the packet is lost. |
1159 | * A'. Reno "three dupacks" marks head of queue lost. | |
974c1236 | 1160 | * B. SACK arrives sacking SND.NXT at the moment, when the |
1da177e4 LT |
1161 | * segment was retransmitted. |
1162 | * 4. D-SACK added new rule: D-SACK changes any tag to S. | |
1163 | * | |
1164 | * It is pleasant to note, that state diagram turns out to be commutative, | |
1165 | * so that we are allowed not to be bothered by order of our actions, | |
1166 | * when multiple events arrive simultaneously. (see the function below). | |
1167 | * | |
1168 | * Reordering detection. | |
1169 | * -------------------- | |
1170 | * Reordering metric is maximal distance, which a packet can be displaced | |
1171 | * in packet stream. With SACKs we can estimate it: | |
1172 | * | |
1173 | * 1. SACK fills old hole and the corresponding segment was not | |
1174 | * ever retransmitted -> reordering. Alas, we cannot use it | |
1175 | * when segment was retransmitted. | |
1176 | * 2. The last flaw is solved with D-SACK. D-SACK arrives | |
1177 | * for retransmitted and already SACKed segment -> reordering.. | |
1178 | * Both of these heuristics are not used in Loss state, when we cannot | |
1179 | * account for retransmits accurately. | |
5b3c9882 IJ |
1180 | * |
1181 | * SACK block validation. | |
1182 | * ---------------------- | |
1183 | * | |
1184 | * SACK block range validation checks that the received SACK block fits to | |
1185 | * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT. | |
1186 | * Note that SND.UNA is not included to the range though being valid because | |
0e835331 IJ |
1187 | * it means that the receiver is rather inconsistent with itself reporting |
1188 | * SACK reneging when it should advance SND.UNA. Such SACK block this is | |
1189 | * perfectly valid, however, in light of RFC2018 which explicitly states | |
1190 | * that "SACK block MUST reflect the newest segment. Even if the newest | |
1191 | * segment is going to be discarded ...", not that it looks very clever | |
1192 | * in case of head skb. Due to potentional receiver driven attacks, we | |
1193 | * choose to avoid immediate execution of a walk in write queue due to | |
1194 | * reneging and defer head skb's loss recovery to standard loss recovery | |
1195 | * procedure that will eventually trigger (nothing forbids us doing this). | |
5b3c9882 IJ |
1196 | * |
1197 | * Implements also blockage to start_seq wrap-around. Problem lies in the | |
1198 | * fact that though start_seq (s) is before end_seq (i.e., not reversed), | |
1199 | * there's no guarantee that it will be before snd_nxt (n). The problem | |
1200 | * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt | |
1201 | * wrap (s_w): | |
1202 | * | |
1203 | * <- outs wnd -> <- wrapzone -> | |
1204 | * u e n u_w e_w s n_w | |
1205 | * | | | | | | | | |
1206 | * |<------------+------+----- TCP seqno space --------------+---------->| | |
1207 | * ...-- <2^31 ->| |<--------... | |
1208 | * ...---- >2^31 ------>| |<--------... | |
1209 | * | |
1210 | * Current code wouldn't be vulnerable but it's better still to discard such | |
1211 | * crazy SACK blocks. Doing this check for start_seq alone closes somewhat | |
1212 | * similar case (end_seq after snd_nxt wrap) as earlier reversed check in | |
1213 | * snd_nxt wrap -> snd_una region will then become "well defined", i.e., | |
1214 | * equal to the ideal case (infinite seqno space without wrap caused issues). | |
1215 | * | |
1216 | * With D-SACK the lower bound is extended to cover sequence space below | |
1217 | * SND.UNA down to undo_marker, which is the last point of interest. Yet | |
564262c1 | 1218 | * again, D-SACK block must not to go across snd_una (for the same reason as |
5b3c9882 IJ |
1219 | * for the normal SACK blocks, explained above). But there all simplicity |
1220 | * ends, TCP might receive valid D-SACKs below that. As long as they reside | |
1221 | * fully below undo_marker they do not affect behavior in anyway and can | |
1222 | * therefore be safely ignored. In rare cases (which are more or less | |
1223 | * theoretical ones), the D-SACK will nicely cross that boundary due to skb | |
1224 | * fragmentation and packet reordering past skb's retransmission. To consider | |
1225 | * them correctly, the acceptable range must be extended even more though | |
1226 | * the exact amount is rather hard to quantify. However, tp->max_window can | |
1227 | * be used as an exaggerated estimate. | |
1da177e4 | 1228 | */ |
a2a385d6 ED |
1229 | static bool tcp_is_sackblock_valid(struct tcp_sock *tp, bool is_dsack, |
1230 | u32 start_seq, u32 end_seq) | |
5b3c9882 IJ |
1231 | { |
1232 | /* Too far in future, or reversed (interpretation is ambiguous) */ | |
1233 | if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq)) | |
a2a385d6 | 1234 | return false; |
5b3c9882 IJ |
1235 | |
1236 | /* Nasty start_seq wrap-around check (see comments above) */ | |
1237 | if (!before(start_seq, tp->snd_nxt)) | |
a2a385d6 | 1238 | return false; |
5b3c9882 | 1239 | |
564262c1 | 1240 | /* In outstanding window? ...This is valid exit for D-SACKs too. |
5b3c9882 IJ |
1241 | * start_seq == snd_una is non-sensical (see comments above) |
1242 | */ | |
1243 | if (after(start_seq, tp->snd_una)) | |
a2a385d6 | 1244 | return true; |
5b3c9882 IJ |
1245 | |
1246 | if (!is_dsack || !tp->undo_marker) | |
a2a385d6 | 1247 | return false; |
5b3c9882 IJ |
1248 | |
1249 | /* ...Then it's D-SACK, and must reside below snd_una completely */ | |
f779b2d6 | 1250 | if (after(end_seq, tp->snd_una)) |
a2a385d6 | 1251 | return false; |
5b3c9882 IJ |
1252 | |
1253 | if (!before(start_seq, tp->undo_marker)) | |
a2a385d6 | 1254 | return true; |
5b3c9882 IJ |
1255 | |
1256 | /* Too old */ | |
1257 | if (!after(end_seq, tp->undo_marker)) | |
a2a385d6 | 1258 | return false; |
5b3c9882 IJ |
1259 | |
1260 | /* Undo_marker boundary crossing (overestimates a lot). Known already: | |
1261 | * start_seq < undo_marker and end_seq >= undo_marker. | |
1262 | */ | |
1263 | return !before(start_seq, end_seq - tp->max_window); | |
1264 | } | |
1265 | ||
a2a385d6 ED |
1266 | static bool tcp_check_dsack(struct sock *sk, const struct sk_buff *ack_skb, |
1267 | struct tcp_sack_block_wire *sp, int num_sacks, | |
a71d77e6 | 1268 | u32 prior_snd_una, struct tcp_sacktag_state *state) |
d06e021d | 1269 | { |
1ed83465 | 1270 | struct tcp_sock *tp = tcp_sk(sk); |
d3e2ce3b HH |
1271 | u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq); |
1272 | u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq); | |
a71d77e6 | 1273 | u32 dup_segs; |
d06e021d DM |
1274 | |
1275 | if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) { | |
c10d9310 | 1276 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKRECV); |
d06e021d | 1277 | } else if (num_sacks > 1) { |
d3e2ce3b HH |
1278 | u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq); |
1279 | u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq); | |
d06e021d | 1280 | |
a71d77e6 PJ |
1281 | if (after(end_seq_0, end_seq_1) || before(start_seq_0, start_seq_1)) |
1282 | return false; | |
1283 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKOFORECV); | |
1284 | } else { | |
1285 | return false; | |
d06e021d DM |
1286 | } |
1287 | ||
a71d77e6 | 1288 | dup_segs = tcp_dsack_seen(tp, start_seq_0, end_seq_0, state); |
ad2b9b0f PJ |
1289 | if (!dup_segs) { /* Skip dubious DSACK */ |
1290 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKIGNOREDDUBIOUS); | |
1291 | return false; | |
1292 | } | |
1293 | ||
e3a5a1e8 | 1294 | NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPDSACKRECVSEGS, dup_segs); |
a71d77e6 | 1295 | |
d06e021d | 1296 | /* D-SACK for already forgotten data... Do dumb counting. */ |
a71d77e6 | 1297 | if (tp->undo_marker && tp->undo_retrans > 0 && |
d06e021d DM |
1298 | !after(end_seq_0, prior_snd_una) && |
1299 | after(end_seq_0, tp->undo_marker)) | |
a71d77e6 | 1300 | tp->undo_retrans = max_t(int, 0, tp->undo_retrans - dup_segs); |
d06e021d | 1301 | |
a71d77e6 | 1302 | return true; |
d06e021d DM |
1303 | } |
1304 | ||
d1935942 IJ |
1305 | /* Check if skb is fully within the SACK block. In presence of GSO skbs, |
1306 | * the incoming SACK may not exactly match but we can find smaller MSS | |
1307 | * aligned portion of it that matches. Therefore we might need to fragment | |
1308 | * which may fail and creates some hassle (caller must handle error case | |
1309 | * returns). | |
832d11c5 IJ |
1310 | * |
1311 | * FIXME: this could be merged to shift decision code | |
d1935942 | 1312 | */ |
0f79efdc | 1313 | static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb, |
a2a385d6 | 1314 | u32 start_seq, u32 end_seq) |
d1935942 | 1315 | { |
a2a385d6 ED |
1316 | int err; |
1317 | bool in_sack; | |
d1935942 | 1318 | unsigned int pkt_len; |
adb92db8 | 1319 | unsigned int mss; |
d1935942 IJ |
1320 | |
1321 | in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) && | |
1322 | !before(end_seq, TCP_SKB_CB(skb)->end_seq); | |
1323 | ||
1324 | if (tcp_skb_pcount(skb) > 1 && !in_sack && | |
1325 | after(TCP_SKB_CB(skb)->end_seq, start_seq)) { | |
adb92db8 | 1326 | mss = tcp_skb_mss(skb); |
d1935942 IJ |
1327 | in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq); |
1328 | ||
adb92db8 | 1329 | if (!in_sack) { |
d1935942 | 1330 | pkt_len = start_seq - TCP_SKB_CB(skb)->seq; |
adb92db8 IJ |
1331 | if (pkt_len < mss) |
1332 | pkt_len = mss; | |
1333 | } else { | |
d1935942 | 1334 | pkt_len = end_seq - TCP_SKB_CB(skb)->seq; |
adb92db8 IJ |
1335 | if (pkt_len < mss) |
1336 | return -EINVAL; | |
1337 | } | |
1338 | ||
1339 | /* Round if necessary so that SACKs cover only full MSSes | |
1340 | * and/or the remaining small portion (if present) | |
1341 | */ | |
1342 | if (pkt_len > mss) { | |
1343 | unsigned int new_len = (pkt_len / mss) * mss; | |
b451e5d2 | 1344 | if (!in_sack && new_len < pkt_len) |
adb92db8 | 1345 | new_len += mss; |
adb92db8 IJ |
1346 | pkt_len = new_len; |
1347 | } | |
b451e5d2 YC |
1348 | |
1349 | if (pkt_len >= skb->len && !in_sack) | |
1350 | return 0; | |
1351 | ||
75c119af ED |
1352 | err = tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb, |
1353 | pkt_len, mss, GFP_ATOMIC); | |
d1935942 IJ |
1354 | if (err < 0) |
1355 | return err; | |
1356 | } | |
1357 | ||
1358 | return in_sack; | |
1359 | } | |
1360 | ||
cc9a672e NC |
1361 | /* Mark the given newly-SACKed range as such, adjusting counters and hints. */ |
1362 | static u8 tcp_sacktag_one(struct sock *sk, | |
1363 | struct tcp_sacktag_state *state, u8 sacked, | |
1364 | u32 start_seq, u32 end_seq, | |
740b0f18 | 1365 | int dup_sack, int pcount, |
9a568de4 | 1366 | u64 xmit_time) |
9e10c47c | 1367 | { |
6859d494 | 1368 | struct tcp_sock *tp = tcp_sk(sk); |
9e10c47c IJ |
1369 | |
1370 | /* Account D-SACK for retransmitted packet. */ | |
1371 | if (dup_sack && (sacked & TCPCB_RETRANS)) { | |
6e08d5e3 | 1372 | if (tp->undo_marker && tp->undo_retrans > 0 && |
cc9a672e | 1373 | after(end_seq, tp->undo_marker)) |
4f884f39 | 1374 | tp->undo_retrans = max_t(int, 0, tp->undo_retrans - pcount); |
737ff314 YC |
1375 | if ((sacked & TCPCB_SACKED_ACKED) && |
1376 | before(start_seq, state->reord)) | |
1377 | state->reord = start_seq; | |
9e10c47c IJ |
1378 | } |
1379 | ||
1380 | /* Nothing to do; acked frame is about to be dropped (was ACKed). */ | |
cc9a672e | 1381 | if (!after(end_seq, tp->snd_una)) |
a1197f5a | 1382 | return sacked; |
9e10c47c IJ |
1383 | |
1384 | if (!(sacked & TCPCB_SACKED_ACKED)) { | |
d2329f10 | 1385 | tcp_rack_advance(tp, sacked, end_seq, xmit_time); |
659a8ad5 | 1386 | |
9e10c47c IJ |
1387 | if (sacked & TCPCB_SACKED_RETRANS) { |
1388 | /* If the segment is not tagged as lost, | |
1389 | * we do not clear RETRANS, believing | |
1390 | * that retransmission is still in flight. | |
1391 | */ | |
1392 | if (sacked & TCPCB_LOST) { | |
a1197f5a | 1393 | sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS); |
f58b22fd IJ |
1394 | tp->lost_out -= pcount; |
1395 | tp->retrans_out -= pcount; | |
9e10c47c IJ |
1396 | } |
1397 | } else { | |
1398 | if (!(sacked & TCPCB_RETRANS)) { | |
1399 | /* New sack for not retransmitted frame, | |
1400 | * which was in hole. It is reordering. | |
1401 | */ | |
cc9a672e | 1402 | if (before(start_seq, |
737ff314 YC |
1403 | tcp_highest_sack_seq(tp)) && |
1404 | before(start_seq, state->reord)) | |
1405 | state->reord = start_seq; | |
1406 | ||
e33099f9 YC |
1407 | if (!after(end_seq, tp->high_seq)) |
1408 | state->flag |= FLAG_ORIG_SACK_ACKED; | |
9a568de4 ED |
1409 | if (state->first_sackt == 0) |
1410 | state->first_sackt = xmit_time; | |
1411 | state->last_sackt = xmit_time; | |
9e10c47c IJ |
1412 | } |
1413 | ||
1414 | if (sacked & TCPCB_LOST) { | |
a1197f5a | 1415 | sacked &= ~TCPCB_LOST; |
f58b22fd | 1416 | tp->lost_out -= pcount; |
9e10c47c IJ |
1417 | } |
1418 | } | |
1419 | ||
a1197f5a IJ |
1420 | sacked |= TCPCB_SACKED_ACKED; |
1421 | state->flag |= FLAG_DATA_SACKED; | |
f58b22fd | 1422 | tp->sacked_out += pcount; |
082d4fa9 | 1423 | /* Out-of-order packets delivered */ |
f00394ce | 1424 | state->sack_delivered += pcount; |
9e10c47c | 1425 | |
9e10c47c | 1426 | /* Lost marker hint past SACKed? Tweak RFC3517 cnt */ |
713bafea | 1427 | if (tp->lost_skb_hint && |
cc9a672e | 1428 | before(start_seq, TCP_SKB_CB(tp->lost_skb_hint)->seq)) |
f58b22fd | 1429 | tp->lost_cnt_hint += pcount; |
9e10c47c IJ |
1430 | } |
1431 | ||
1432 | /* D-SACK. We can detect redundant retransmission in S|R and plain R | |
1433 | * frames and clear it. undo_retrans is decreased above, L|R frames | |
1434 | * are accounted above as well. | |
1435 | */ | |
a1197f5a IJ |
1436 | if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) { |
1437 | sacked &= ~TCPCB_SACKED_RETRANS; | |
f58b22fd | 1438 | tp->retrans_out -= pcount; |
9e10c47c IJ |
1439 | } |
1440 | ||
a1197f5a | 1441 | return sacked; |
9e10c47c IJ |
1442 | } |
1443 | ||
daef52ba NC |
1444 | /* Shift newly-SACKed bytes from this skb to the immediately previous |
1445 | * already-SACKed sk_buff. Mark the newly-SACKed bytes as such. | |
1446 | */ | |
f3319816 ED |
1447 | static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *prev, |
1448 | struct sk_buff *skb, | |
a2a385d6 ED |
1449 | struct tcp_sacktag_state *state, |
1450 | unsigned int pcount, int shifted, int mss, | |
1451 | bool dup_sack) | |
832d11c5 IJ |
1452 | { |
1453 | struct tcp_sock *tp = tcp_sk(sk); | |
daef52ba NC |
1454 | u32 start_seq = TCP_SKB_CB(skb)->seq; /* start of newly-SACKed */ |
1455 | u32 end_seq = start_seq + shifted; /* end of newly-SACKed */ | |
832d11c5 IJ |
1456 | |
1457 | BUG_ON(!pcount); | |
1458 | ||
4c90d3b3 NC |
1459 | /* Adjust counters and hints for the newly sacked sequence |
1460 | * range but discard the return value since prev is already | |
1461 | * marked. We must tag the range first because the seq | |
1462 | * advancement below implicitly advances | |
1463 | * tcp_highest_sack_seq() when skb is highest_sack. | |
1464 | */ | |
1465 | tcp_sacktag_one(sk, state, TCP_SKB_CB(skb)->sacked, | |
59c9af42 | 1466 | start_seq, end_seq, dup_sack, pcount, |
2fd66ffb | 1467 | tcp_skb_timestamp_us(skb)); |
b9f64820 | 1468 | tcp_rate_skb_delivered(sk, skb, state->rate); |
4c90d3b3 NC |
1469 | |
1470 | if (skb == tp->lost_skb_hint) | |
0af2a0d0 NC |
1471 | tp->lost_cnt_hint += pcount; |
1472 | ||
832d11c5 IJ |
1473 | TCP_SKB_CB(prev)->end_seq += shifted; |
1474 | TCP_SKB_CB(skb)->seq += shifted; | |
1475 | ||
cd7d8498 | 1476 | tcp_skb_pcount_add(prev, pcount); |
3b4929f6 | 1477 | WARN_ON_ONCE(tcp_skb_pcount(skb) < pcount); |
cd7d8498 | 1478 | tcp_skb_pcount_add(skb, -pcount); |
832d11c5 IJ |
1479 | |
1480 | /* When we're adding to gso_segs == 1, gso_size will be zero, | |
1481 | * in theory this shouldn't be necessary but as long as DSACK | |
1482 | * code can come after this skb later on it's better to keep | |
1483 | * setting gso_size to something. | |
1484 | */ | |
f69ad292 ED |
1485 | if (!TCP_SKB_CB(prev)->tcp_gso_size) |
1486 | TCP_SKB_CB(prev)->tcp_gso_size = mss; | |
832d11c5 IJ |
1487 | |
1488 | /* CHECKME: To clear or not to clear? Mimics normal skb currently */ | |
51466a75 | 1489 | if (tcp_skb_pcount(skb) <= 1) |
f69ad292 | 1490 | TCP_SKB_CB(skb)->tcp_gso_size = 0; |
832d11c5 | 1491 | |
832d11c5 IJ |
1492 | /* Difference in this won't matter, both ACKed by the same cumul. ACK */ |
1493 | TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS); | |
1494 | ||
832d11c5 IJ |
1495 | if (skb->len > 0) { |
1496 | BUG_ON(!tcp_skb_pcount(skb)); | |
c10d9310 | 1497 | NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTED); |
a2a385d6 | 1498 | return false; |
832d11c5 IJ |
1499 | } |
1500 | ||
1501 | /* Whole SKB was eaten :-) */ | |
1502 | ||
92ee76b6 IJ |
1503 | if (skb == tp->retransmit_skb_hint) |
1504 | tp->retransmit_skb_hint = prev; | |
92ee76b6 IJ |
1505 | if (skb == tp->lost_skb_hint) { |
1506 | tp->lost_skb_hint = prev; | |
1507 | tp->lost_cnt_hint -= tcp_skb_pcount(prev); | |
1508 | } | |
1509 | ||
5e8a402f | 1510 | TCP_SKB_CB(prev)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags; |
a643b5d4 | 1511 | TCP_SKB_CB(prev)->eor = TCP_SKB_CB(skb)->eor; |
5e8a402f ED |
1512 | if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) |
1513 | TCP_SKB_CB(prev)->end_seq++; | |
1514 | ||
832d11c5 IJ |
1515 | if (skb == tcp_highest_sack(sk)) |
1516 | tcp_advance_highest_sack(sk, skb); | |
1517 | ||
cfea5a68 | 1518 | tcp_skb_collapse_tstamp(prev, skb); |
9a568de4 ED |
1519 | if (unlikely(TCP_SKB_CB(prev)->tx.delivered_mstamp)) |
1520 | TCP_SKB_CB(prev)->tx.delivered_mstamp = 0; | |
b9f64820 | 1521 | |
75c119af | 1522 | tcp_rtx_queue_unlink_and_free(skb, sk); |
832d11c5 | 1523 | |
c10d9310 | 1524 | NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKMERGED); |
111cc8b9 | 1525 | |
a2a385d6 | 1526 | return true; |
832d11c5 IJ |
1527 | } |
1528 | ||
1529 | /* I wish gso_size would have a bit more sane initialization than | |
1530 | * something-or-zero which complicates things | |
1531 | */ | |
cf533ea5 | 1532 | static int tcp_skb_seglen(const struct sk_buff *skb) |
832d11c5 | 1533 | { |
775ffabf | 1534 | return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb); |
832d11c5 IJ |
1535 | } |
1536 | ||
1537 | /* Shifting pages past head area doesn't work */ | |
cf533ea5 | 1538 | static int skb_can_shift(const struct sk_buff *skb) |
832d11c5 IJ |
1539 | { |
1540 | return !skb_headlen(skb) && skb_is_nonlinear(skb); | |
1541 | } | |
1542 | ||
3b4929f6 ED |
1543 | int tcp_skb_shift(struct sk_buff *to, struct sk_buff *from, |
1544 | int pcount, int shiftlen) | |
1545 | { | |
1546 | /* TCP min gso_size is 8 bytes (TCP_MIN_GSO_SIZE) | |
1547 | * Since TCP_SKB_CB(skb)->tcp_gso_segs is 16 bits, we need | |
1548 | * to make sure not storing more than 65535 * 8 bytes per skb, | |
1549 | * even if current MSS is bigger. | |
1550 | */ | |
1551 | if (unlikely(to->len + shiftlen >= 65535 * TCP_MIN_GSO_SIZE)) | |
1552 | return 0; | |
1553 | if (unlikely(tcp_skb_pcount(to) + pcount > 65535)) | |
1554 | return 0; | |
1555 | return skb_shift(to, from, shiftlen); | |
1556 | } | |
1557 | ||
832d11c5 IJ |
1558 | /* Try collapsing SACK blocks spanning across multiple skbs to a single |
1559 | * skb. | |
1560 | */ | |
1561 | static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb, | |
a1197f5a | 1562 | struct tcp_sacktag_state *state, |
832d11c5 | 1563 | u32 start_seq, u32 end_seq, |
a2a385d6 | 1564 | bool dup_sack) |
832d11c5 IJ |
1565 | { |
1566 | struct tcp_sock *tp = tcp_sk(sk); | |
1567 | struct sk_buff *prev; | |
1568 | int mss; | |
1569 | int pcount = 0; | |
1570 | int len; | |
1571 | int in_sack; | |
1572 | ||
832d11c5 IJ |
1573 | /* Normally R but no L won't result in plain S */ |
1574 | if (!dup_sack && | |
9969ca5f | 1575 | (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS) |
832d11c5 IJ |
1576 | goto fallback; |
1577 | if (!skb_can_shift(skb)) | |
1578 | goto fallback; | |
1579 | /* This frame is about to be dropped (was ACKed). */ | |
1580 | if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una)) | |
1581 | goto fallback; | |
1582 | ||
1583 | /* Can only happen with delayed DSACK + discard craziness */ | |
75c119af ED |
1584 | prev = skb_rb_prev(skb); |
1585 | if (!prev) | |
832d11c5 | 1586 | goto fallback; |
832d11c5 IJ |
1587 | |
1588 | if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) | |
1589 | goto fallback; | |
1590 | ||
85712484 | 1591 | if (!tcp_skb_can_collapse(prev, skb)) |
a643b5d4 MKL |
1592 | goto fallback; |
1593 | ||
832d11c5 IJ |
1594 | in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) && |
1595 | !before(end_seq, TCP_SKB_CB(skb)->end_seq); | |
1596 | ||
1597 | if (in_sack) { | |
1598 | len = skb->len; | |
1599 | pcount = tcp_skb_pcount(skb); | |
775ffabf | 1600 | mss = tcp_skb_seglen(skb); |
832d11c5 IJ |
1601 | |
1602 | /* TODO: Fix DSACKs to not fragment already SACKed and we can | |
1603 | * drop this restriction as unnecessary | |
1604 | */ | |
775ffabf | 1605 | if (mss != tcp_skb_seglen(prev)) |
832d11c5 IJ |
1606 | goto fallback; |
1607 | } else { | |
1608 | if (!after(TCP_SKB_CB(skb)->end_seq, start_seq)) | |
1609 | goto noop; | |
1610 | /* CHECKME: This is non-MSS split case only?, this will | |
1611 | * cause skipped skbs due to advancing loop btw, original | |
1612 | * has that feature too | |
1613 | */ | |
1614 | if (tcp_skb_pcount(skb) <= 1) | |
1615 | goto noop; | |
1616 | ||
1617 | in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq); | |
1618 | if (!in_sack) { | |
1619 | /* TODO: head merge to next could be attempted here | |
1620 | * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)), | |
1621 | * though it might not be worth of the additional hassle | |
1622 | * | |
1623 | * ...we can probably just fallback to what was done | |
1624 | * previously. We could try merging non-SACKed ones | |
1625 | * as well but it probably isn't going to buy off | |
1626 | * because later SACKs might again split them, and | |
1627 | * it would make skb timestamp tracking considerably | |
1628 | * harder problem. | |
1629 | */ | |
1630 | goto fallback; | |
1631 | } | |
1632 | ||
1633 | len = end_seq - TCP_SKB_CB(skb)->seq; | |
1634 | BUG_ON(len < 0); | |
1635 | BUG_ON(len > skb->len); | |
1636 | ||
1637 | /* MSS boundaries should be honoured or else pcount will | |
1638 | * severely break even though it makes things bit trickier. | |
1639 | * Optimize common case to avoid most of the divides | |
1640 | */ | |
1641 | mss = tcp_skb_mss(skb); | |
1642 | ||
1643 | /* TODO: Fix DSACKs to not fragment already SACKed and we can | |
1644 | * drop this restriction as unnecessary | |
1645 | */ | |
775ffabf | 1646 | if (mss != tcp_skb_seglen(prev)) |
832d11c5 IJ |
1647 | goto fallback; |
1648 | ||
1649 | if (len == mss) { | |
1650 | pcount = 1; | |
1651 | } else if (len < mss) { | |
1652 | goto noop; | |
1653 | } else { | |
1654 | pcount = len / mss; | |
1655 | len = pcount * mss; | |
1656 | } | |
1657 | } | |
1658 | ||
4648dc97 NC |
1659 | /* tcp_sacktag_one() won't SACK-tag ranges below snd_una */ |
1660 | if (!after(TCP_SKB_CB(skb)->seq + len, tp->snd_una)) | |
1661 | goto fallback; | |
1662 | ||
3b4929f6 | 1663 | if (!tcp_skb_shift(prev, skb, pcount, len)) |
832d11c5 | 1664 | goto fallback; |
f3319816 | 1665 | if (!tcp_shifted_skb(sk, prev, skb, state, pcount, len, mss, dup_sack)) |
832d11c5 IJ |
1666 | goto out; |
1667 | ||
1668 | /* Hole filled allows collapsing with the next as well, this is very | |
1669 | * useful when hole on every nth skb pattern happens | |
1670 | */ | |
75c119af ED |
1671 | skb = skb_rb_next(prev); |
1672 | if (!skb) | |
832d11c5 | 1673 | goto out; |
832d11c5 | 1674 | |
f0bc52f3 | 1675 | if (!skb_can_shift(skb) || |
f0bc52f3 | 1676 | ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) || |
775ffabf | 1677 | (mss != tcp_skb_seglen(skb))) |
832d11c5 IJ |
1678 | goto out; |
1679 | ||
b67985be ED |
1680 | if (!tcp_skb_can_collapse(prev, skb)) |
1681 | goto out; | |
832d11c5 | 1682 | len = skb->len; |
3b4929f6 ED |
1683 | pcount = tcp_skb_pcount(skb); |
1684 | if (tcp_skb_shift(prev, skb, pcount, len)) | |
1685 | tcp_shifted_skb(sk, prev, skb, state, pcount, | |
f3319816 | 1686 | len, mss, 0); |
832d11c5 IJ |
1687 | |
1688 | out: | |
832d11c5 IJ |
1689 | return prev; |
1690 | ||
1691 | noop: | |
1692 | return skb; | |
1693 | ||
1694 | fallback: | |
c10d9310 | 1695 | NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK); |
832d11c5 IJ |
1696 | return NULL; |
1697 | } | |
1698 | ||
68f8353b IJ |
1699 | static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk, |
1700 | struct tcp_sack_block *next_dup, | |
a1197f5a | 1701 | struct tcp_sacktag_state *state, |
68f8353b | 1702 | u32 start_seq, u32 end_seq, |
a2a385d6 | 1703 | bool dup_sack_in) |
68f8353b | 1704 | { |
832d11c5 IJ |
1705 | struct tcp_sock *tp = tcp_sk(sk); |
1706 | struct sk_buff *tmp; | |
1707 | ||
75c119af | 1708 | skb_rbtree_walk_from(skb) { |
68f8353b | 1709 | int in_sack = 0; |
a2a385d6 | 1710 | bool dup_sack = dup_sack_in; |
68f8353b | 1711 | |
68f8353b IJ |
1712 | /* queue is in-order => we can short-circuit the walk early */ |
1713 | if (!before(TCP_SKB_CB(skb)->seq, end_seq)) | |
1714 | break; | |
1715 | ||
00db4124 | 1716 | if (next_dup && |
68f8353b IJ |
1717 | before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) { |
1718 | in_sack = tcp_match_skb_to_sack(sk, skb, | |
1719 | next_dup->start_seq, | |
1720 | next_dup->end_seq); | |
1721 | if (in_sack > 0) | |
a2a385d6 | 1722 | dup_sack = true; |
68f8353b IJ |
1723 | } |
1724 | ||
832d11c5 IJ |
1725 | /* skb reference here is a bit tricky to get right, since |
1726 | * shifting can eat and free both this skb and the next, | |
1727 | * so not even _safe variant of the loop is enough. | |
1728 | */ | |
1729 | if (in_sack <= 0) { | |
a1197f5a IJ |
1730 | tmp = tcp_shift_skb_data(sk, skb, state, |
1731 | start_seq, end_seq, dup_sack); | |
00db4124 | 1732 | if (tmp) { |
832d11c5 IJ |
1733 | if (tmp != skb) { |
1734 | skb = tmp; | |
1735 | continue; | |
1736 | } | |
1737 | ||
1738 | in_sack = 0; | |
1739 | } else { | |
1740 | in_sack = tcp_match_skb_to_sack(sk, skb, | |
1741 | start_seq, | |
1742 | end_seq); | |
1743 | } | |
1744 | } | |
1745 | ||
68f8353b IJ |
1746 | if (unlikely(in_sack < 0)) |
1747 | break; | |
1748 | ||
832d11c5 | 1749 | if (in_sack) { |
cc9a672e NC |
1750 | TCP_SKB_CB(skb)->sacked = |
1751 | tcp_sacktag_one(sk, | |
1752 | state, | |
1753 | TCP_SKB_CB(skb)->sacked, | |
1754 | TCP_SKB_CB(skb)->seq, | |
1755 | TCP_SKB_CB(skb)->end_seq, | |
1756 | dup_sack, | |
59c9af42 | 1757 | tcp_skb_pcount(skb), |
2fd66ffb | 1758 | tcp_skb_timestamp_us(skb)); |
b9f64820 | 1759 | tcp_rate_skb_delivered(sk, skb, state->rate); |
e2080072 ED |
1760 | if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) |
1761 | list_del_init(&skb->tcp_tsorted_anchor); | |
68f8353b | 1762 | |
832d11c5 IJ |
1763 | if (!before(TCP_SKB_CB(skb)->seq, |
1764 | tcp_highest_sack_seq(tp))) | |
1765 | tcp_advance_highest_sack(sk, skb); | |
1766 | } | |
68f8353b IJ |
1767 | } |
1768 | return skb; | |
1769 | } | |
1770 | ||
4bfabc46 | 1771 | static struct sk_buff *tcp_sacktag_bsearch(struct sock *sk, u32 seq) |
75c119af ED |
1772 | { |
1773 | struct rb_node *parent, **p = &sk->tcp_rtx_queue.rb_node; | |
1774 | struct sk_buff *skb; | |
75c119af ED |
1775 | |
1776 | while (*p) { | |
1777 | parent = *p; | |
1778 | skb = rb_to_skb(parent); | |
1779 | if (before(seq, TCP_SKB_CB(skb)->seq)) { | |
1780 | p = &parent->rb_left; | |
1781 | continue; | |
1782 | } | |
1783 | if (!before(seq, TCP_SKB_CB(skb)->end_seq)) { | |
1784 | p = &parent->rb_right; | |
1785 | continue; | |
1786 | } | |
75c119af ED |
1787 | return skb; |
1788 | } | |
1789 | return NULL; | |
1790 | } | |
1791 | ||
68f8353b | 1792 | static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk, |
a1197f5a | 1793 | u32 skip_to_seq) |
68f8353b | 1794 | { |
75c119af ED |
1795 | if (skb && after(TCP_SKB_CB(skb)->seq, skip_to_seq)) |
1796 | return skb; | |
d152a7d8 | 1797 | |
4bfabc46 | 1798 | return tcp_sacktag_bsearch(sk, skip_to_seq); |
68f8353b IJ |
1799 | } |
1800 | ||
1801 | static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb, | |
1802 | struct sock *sk, | |
1803 | struct tcp_sack_block *next_dup, | |
a1197f5a IJ |
1804 | struct tcp_sacktag_state *state, |
1805 | u32 skip_to_seq) | |
68f8353b | 1806 | { |
51456b29 | 1807 | if (!next_dup) |
68f8353b IJ |
1808 | return skb; |
1809 | ||
1810 | if (before(next_dup->start_seq, skip_to_seq)) { | |
4bfabc46 | 1811 | skb = tcp_sacktag_skip(skb, sk, next_dup->start_seq); |
a1197f5a IJ |
1812 | skb = tcp_sacktag_walk(skb, sk, NULL, state, |
1813 | next_dup->start_seq, next_dup->end_seq, | |
1814 | 1); | |
68f8353b IJ |
1815 | } |
1816 | ||
1817 | return skb; | |
1818 | } | |
1819 | ||
cf533ea5 | 1820 | static int tcp_sack_cache_ok(const struct tcp_sock *tp, const struct tcp_sack_block *cache) |
68f8353b IJ |
1821 | { |
1822 | return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache); | |
1823 | } | |
1824 | ||
1da177e4 | 1825 | static int |
cf533ea5 | 1826 | tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb, |
196da974 | 1827 | u32 prior_snd_una, struct tcp_sacktag_state *state) |
1da177e4 LT |
1828 | { |
1829 | struct tcp_sock *tp = tcp_sk(sk); | |
cf533ea5 ED |
1830 | const unsigned char *ptr = (skb_transport_header(ack_skb) + |
1831 | TCP_SKB_CB(ack_skb)->sacked); | |
fd6dad61 | 1832 | struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2); |
4389dded | 1833 | struct tcp_sack_block sp[TCP_NUM_SACKS]; |
68f8353b IJ |
1834 | struct tcp_sack_block *cache; |
1835 | struct sk_buff *skb; | |
4389dded | 1836 | int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3); |
fd6dad61 | 1837 | int used_sacks; |
a2a385d6 | 1838 | bool found_dup_sack = false; |
68f8353b | 1839 | int i, j; |
fda03fbb | 1840 | int first_sack_index; |
1da177e4 | 1841 | |
196da974 | 1842 | state->flag = 0; |
737ff314 | 1843 | state->reord = tp->snd_nxt; |
a1197f5a | 1844 | |
737ff314 | 1845 | if (!tp->sacked_out) |
6859d494 | 1846 | tcp_highest_sack_reset(sk); |
1da177e4 | 1847 | |
1ed83465 | 1848 | found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire, |
a71d77e6 | 1849 | num_sacks, prior_snd_una, state); |
6f74651a BE |
1850 | |
1851 | /* Eliminate too old ACKs, but take into | |
1852 | * account more or less fresh ones, they can | |
1853 | * contain valid SACK info. | |
1854 | */ | |
1855 | if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window)) | |
1856 | return 0; | |
1857 | ||