]>
Commit | Line | Data |
---|---|---|
8ad349bb | 1 | /* |
39037602 | 2 | * Copyright (c) 2004-2016 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
39037602 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
39037602 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
39037602 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
39037602 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
8ad349bb A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | */ | |
61 | ||
62 | #define _IP_VHL | |
63 | ||
64 | ||
65 | #include <sys/param.h> | |
66 | #include <sys/systm.h> | |
67 | #include <sys/kernel.h> | |
68 | #include <sys/sysctl.h> | |
69 | #include <sys/mbuf.h> | |
70 | #include <sys/domain.h> | |
71 | #include <sys/protosw.h> | |
72 | #include <sys/socket.h> | |
73 | #include <sys/socketvar.h> | |
74 | ||
2d21ac55 A |
75 | #include <kern/zalloc.h> |
76 | ||
8ad349bb A |
77 | #include <net/route.h> |
78 | ||
79 | #include <netinet/in.h> | |
80 | #include <netinet/in_systm.h> | |
81 | #include <netinet/ip.h> | |
82 | #include <netinet/in_pcb.h> | |
83 | #include <netinet/ip_var.h> | |
84 | #if INET6 | |
85 | #include <netinet6/in6_pcb.h> | |
86 | #include <netinet/ip6.h> | |
87 | #include <netinet6/ip6_var.h> | |
88 | #endif | |
89 | #include <netinet/tcp.h> | |
90 | //#define TCPOUTFLAGS | |
91 | #include <netinet/tcp_fsm.h> | |
92 | #include <netinet/tcp_seq.h> | |
93 | #include <netinet/tcp_timer.h> | |
94 | #include <netinet/tcp_var.h> | |
95 | #include <netinet/tcpip.h> | |
39037602 | 96 | #include <netinet/tcp_cache.h> |
8ad349bb A |
97 | #if TCPDEBUG |
98 | #include <netinet/tcp_debug.h> | |
99 | #endif | |
100 | #include <sys/kdebug.h> | |
101 | ||
102 | #if IPSEC | |
103 | #include <netinet6/ipsec.h> | |
104 | #endif /*IPSEC*/ | |
105 | ||
fe8ab488 A |
106 | #include <libkern/OSAtomic.h> |
107 | ||
8ad349bb | 108 | int tcp_do_sack = 1; |
6d2010ae | 109 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_do_sack, 0, |
8ad349bb A |
110 | "Enable/Disable TCP SACK support"); |
111 | static int tcp_sack_maxholes = 128; | |
6d2010ae | 112 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack_maxholes, CTLFLAG_RW | CTLFLAG_LOCKED, |
8ad349bb A |
113 | &tcp_sack_maxholes, 0, |
114 | "Maximum number of TCP SACK holes allowed per connection"); | |
115 | ||
116 | static int tcp_sack_globalmaxholes = 65536; | |
6d2010ae | 117 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack_globalmaxholes, CTLFLAG_RW | CTLFLAG_LOCKED, |
8ad349bb A |
118 | &tcp_sack_globalmaxholes, 0, |
119 | "Global maximum number of TCP SACK holes"); | |
120 | ||
fe8ab488 | 121 | static SInt32 tcp_sack_globalholes = 0; |
6d2010ae | 122 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack_globalholes, CTLFLAG_RD | CTLFLAG_LOCKED, |
8ad349bb A |
123 | &tcp_sack_globalholes, 0, |
124 | "Global number of TCP SACK holes currently allocated"); | |
125 | ||
3e170ce0 A |
126 | static int tcp_detect_reordering = 1; |
127 | static int tcp_dsack_ignore_hw_duplicates = 0; | |
128 | ||
129 | #if (DEVELOPMENT || DEBUG) | |
130 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, detect_reordering, | |
131 | CTLFLAG_RW | CTLFLAG_LOCKED, | |
132 | &tcp_detect_reordering, 0, ""); | |
133 | ||
134 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, ignore_hw_duplicates, | |
135 | CTLFLAG_RW | CTLFLAG_LOCKED, | |
136 | &tcp_dsack_ignore_hw_duplicates, 0, ""); | |
137 | #endif /* (DEVELOPMENT || DEBUG) */ | |
138 | ||
8ad349bb A |
139 | extern struct zone *sack_hole_zone; |
140 | ||
3e170ce0 A |
141 | #define TCP_VALIDATE_SACK_SEQ_NUMBERS(_tp_, _sb_, _ack_) \ |
142 | (SEQ_GT((_sb_)->end, (_sb_)->start) && \ | |
143 | SEQ_GT((_sb_)->start, (_tp_)->snd_una) && \ | |
144 | SEQ_GT((_sb_)->start, (_ack_)) && \ | |
145 | SEQ_LT((_sb_)->start, (_tp_)->snd_max) && \ | |
146 | SEQ_GT((_sb_)->end, (_tp_)->snd_una) && \ | |
147 | SEQ_LEQ((_sb_)->end, (_tp_)->snd_max)) | |
148 | ||
8ad349bb A |
149 | /* |
150 | * This function is called upon receipt of new valid data (while not in header | |
151 | * prediction mode), and it updates the ordered list of sacks. | |
152 | */ | |
153 | void | |
154 | tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end) | |
155 | { | |
156 | /* | |
157 | * First reported block MUST be the most recent one. Subsequent | |
158 | * blocks SHOULD be in the order in which they arrived at the | |
159 | * receiver. These two conditions make the implementation fully | |
160 | * compliant with RFC 2018. | |
161 | */ | |
162 | struct sackblk head_blk, saved_blks[MAX_SACK_BLKS]; | |
163 | int num_head, num_saved, i; | |
164 | ||
165 | /* SACK block for the received segment. */ | |
166 | head_blk.start = rcv_start; | |
167 | head_blk.end = rcv_end; | |
168 | ||
169 | /* | |
170 | * Merge updated SACK blocks into head_blk, and | |
171 | * save unchanged SACK blocks into saved_blks[]. | |
172 | * num_saved will have the number of the saved SACK blocks. | |
173 | */ | |
174 | num_saved = 0; | |
175 | for (i = 0; i < tp->rcv_numsacks; i++) { | |
176 | tcp_seq start = tp->sackblks[i].start; | |
177 | tcp_seq end = tp->sackblks[i].end; | |
178 | if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) { | |
179 | /* | |
180 | * Discard this SACK block. | |
181 | */ | |
182 | } else if (SEQ_LEQ(head_blk.start, end) && | |
183 | SEQ_GEQ(head_blk.end, start)) { | |
184 | /* | |
185 | * Merge this SACK block into head_blk. | |
186 | * This SACK block itself will be discarded. | |
187 | */ | |
188 | if (SEQ_GT(head_blk.start, start)) | |
189 | head_blk.start = start; | |
190 | if (SEQ_LT(head_blk.end, end)) | |
191 | head_blk.end = end; | |
192 | } else { | |
193 | /* | |
194 | * Save this SACK block. | |
195 | */ | |
196 | saved_blks[num_saved].start = start; | |
197 | saved_blks[num_saved].end = end; | |
198 | num_saved++; | |
199 | } | |
200 | } | |
201 | ||
202 | /* | |
203 | * Update SACK list in tp->sackblks[]. | |
204 | */ | |
205 | num_head = 0; | |
206 | if (SEQ_GT(head_blk.start, tp->rcv_nxt)) { | |
207 | /* | |
208 | * The received data segment is an out-of-order segment. | |
209 | * Put head_blk at the top of SACK list. | |
210 | */ | |
211 | tp->sackblks[0] = head_blk; | |
212 | num_head = 1; | |
213 | /* | |
214 | * If the number of saved SACK blocks exceeds its limit, | |
215 | * discard the last SACK block. | |
216 | */ | |
217 | if (num_saved >= MAX_SACK_BLKS) | |
218 | num_saved--; | |
219 | } | |
220 | if (num_saved > 0) { | |
221 | /* | |
222 | * Copy the saved SACK blocks back. | |
223 | */ | |
224 | bcopy(saved_blks, &tp->sackblks[num_head], | |
225 | sizeof(struct sackblk) * num_saved); | |
226 | } | |
227 | ||
228 | /* Save the number of SACK blocks. */ | |
229 | tp->rcv_numsacks = num_head + num_saved; | |
6d2010ae A |
230 | |
231 | /* If we are requesting SACK recovery, reset the stretch-ack state | |
232 | * so that connection will generate more acks after recovery and | |
233 | * sender's cwnd will open. | |
234 | */ | |
235 | if ((tp->t_flags & TF_STRETCHACK) != 0 && tp->rcv_numsacks > 0) | |
236 | tcp_reset_stretch_ack(tp); | |
237 | ||
238 | #if TRAFFIC_MGT | |
239 | if (tp->acc_iaj > 0 && tp->rcv_numsacks > 0) | |
240 | reset_acc_iaj(tp); | |
241 | #endif /* TRAFFIC_MGT */ | |
8ad349bb A |
242 | } |
243 | ||
244 | /* | |
245 | * Delete all receiver-side SACK information. | |
246 | */ | |
247 | void | |
248 | tcp_clean_sackreport( struct tcpcb *tp) | |
249 | { | |
8ad349bb A |
250 | |
251 | tp->rcv_numsacks = 0; | |
8ad349bb A |
252 | bzero(&tp->sackblks[0], sizeof (struct sackblk) * MAX_SACK_BLKS); |
253 | } | |
254 | ||
255 | /* | |
256 | * Allocate struct sackhole. | |
257 | */ | |
258 | static struct sackhole * | |
259 | tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end) | |
260 | { | |
261 | struct sackhole *hole; | |
262 | ||
263 | if (tp->snd_numholes >= tcp_sack_maxholes || | |
264 | tcp_sack_globalholes >= tcp_sack_globalmaxholes) { | |
265 | tcpstat.tcps_sack_sboverflow++; | |
266 | return NULL; | |
267 | } | |
268 | ||
fe8ab488 | 269 | hole = (struct sackhole *)zalloc(sack_hole_zone); |
8ad349bb A |
270 | if (hole == NULL) |
271 | return NULL; | |
272 | ||
273 | hole->start = start; | |
274 | hole->end = end; | |
275 | hole->rxmit = start; | |
276 | ||
277 | tp->snd_numholes++; | |
fe8ab488 | 278 | OSIncrementAtomic(&tcp_sack_globalholes); |
8ad349bb A |
279 | |
280 | return hole; | |
281 | } | |
282 | ||
283 | /* | |
284 | * Free struct sackhole. | |
285 | */ | |
286 | static void | |
287 | tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole) | |
288 | { | |
289 | zfree(sack_hole_zone, hole); | |
290 | ||
291 | tp->snd_numholes--; | |
fe8ab488 | 292 | OSDecrementAtomic(&tcp_sack_globalholes); |
8ad349bb A |
293 | } |
294 | ||
295 | /* | |
296 | * Insert new SACK hole into scoreboard. | |
297 | */ | |
298 | static struct sackhole * | |
299 | tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end, | |
300 | struct sackhole *after) | |
301 | { | |
302 | struct sackhole *hole; | |
303 | ||
304 | /* Allocate a new SACK hole. */ | |
305 | hole = tcp_sackhole_alloc(tp, start, end); | |
306 | if (hole == NULL) | |
307 | return NULL; | |
fe8ab488 | 308 | hole->rxmit_start = tcp_now; |
8ad349bb A |
309 | /* Insert the new SACK hole into scoreboard */ |
310 | if (after != NULL) | |
311 | TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink); | |
312 | else | |
313 | TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink); | |
314 | ||
315 | /* Update SACK hint. */ | |
316 | if (tp->sackhint.nexthole == NULL) | |
317 | tp->sackhint.nexthole = hole; | |
318 | ||
3e170ce0 | 319 | return(hole); |
8ad349bb A |
320 | } |
321 | ||
322 | /* | |
323 | * Remove SACK hole from scoreboard. | |
324 | */ | |
325 | static void | |
326 | tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole) | |
327 | { | |
328 | /* Update SACK hint. */ | |
329 | if (tp->sackhint.nexthole == hole) | |
330 | tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink); | |
331 | ||
332 | /* Remove this SACK hole. */ | |
333 | TAILQ_REMOVE(&tp->snd_holes, hole, scblink); | |
334 | ||
335 | /* Free this SACK hole. */ | |
336 | tcp_sackhole_free(tp, hole); | |
337 | } | |
fe8ab488 A |
338 | /* |
339 | * When a new ack with SACK is received, check if it indicates packet | |
340 | * reordering. If there is packet reordering, the socket is marked and | |
341 | * the late time offset by which the packet was reordered with | |
342 | * respect to its closest neighboring packets is computed. | |
343 | */ | |
344 | static void | |
345 | tcp_sack_detect_reordering(struct tcpcb *tp, struct sackhole *s, | |
346 | tcp_seq sacked_seq, tcp_seq snd_fack) | |
347 | { | |
348 | int32_t rext = 0, reordered = 0; | |
349 | ||
350 | /* | |
351 | * If the SACK hole is past snd_fack, this is from new SACK | |
352 | * information, so we can ignore it. | |
353 | */ | |
354 | if (SEQ_GT(s->end, snd_fack)) | |
355 | return; | |
356 | /* | |
357 | * If there has been a retransmit timeout, then the timestamp on | |
358 | * the SACK segment will be newer. This might lead to a | |
359 | * false-positive. Avoid re-ordering detection in this case. | |
360 | */ | |
361 | if (tp->t_rxtshift > 0) | |
362 | return; | |
363 | ||
364 | /* | |
365 | * Detect reordering from SACK information by checking | |
366 | * if recently sacked data was never retransmitted from this hole. | |
367 | */ | |
368 | if (SEQ_LT(s->rxmit, sacked_seq)) { | |
369 | reordered = 1; | |
370 | tcpstat.tcps_avoid_rxmt++; | |
371 | } | |
372 | ||
373 | if (reordered) { | |
3e170ce0 A |
374 | if (tcp_detect_reordering == 1 && |
375 | !(tp->t_flagsext & TF_PKTS_REORDERED)) { | |
fe8ab488 A |
376 | tp->t_flagsext |= TF_PKTS_REORDERED; |
377 | tcpstat.tcps_detect_reordering++; | |
378 | } | |
379 | ||
380 | tcpstat.tcps_reordered_pkts++; | |
4bd07ac2 | 381 | tp->t_reordered_pkts++; |
fe8ab488 | 382 | |
39037602 A |
383 | /* |
384 | * If reordering is seen on a connection wth ECN enabled, | |
385 | * increment the heuristic | |
386 | */ | |
387 | if (TCP_ECN_ENABLED(tp)) { | |
388 | INP_INC_IFNET_STAT(tp->t_inpcb, ecn_fallback_reorder); | |
389 | tcpstat.tcps_ecn_fallback_reorder++; | |
390 | tcp_heuristic_ecn_aggressive(tp); | |
391 | } | |
392 | ||
fe8ab488 A |
393 | VERIFY(SEQ_GEQ(snd_fack, s->rxmit)); |
394 | ||
395 | if (s->rxmit_start > 0) { | |
396 | rext = timer_diff(tcp_now, 0, s->rxmit_start, 0); | |
397 | if (rext < 0) | |
398 | return; | |
399 | ||
400 | /* | |
401 | * We take the maximum reorder window to schedule | |
402 | * DELAYFR timer as that will take care of jitter | |
403 | * on the network path. | |
404 | * | |
405 | * Computing average and standard deviation seems | |
406 | * to cause unnecessary retransmissions when there | |
407 | * is high jitter. | |
408 | * | |
409 | * We set a maximum of SRTT/2 and a minimum of | |
410 | * 10 ms on the reorder window. | |
411 | */ | |
412 | tp->t_reorderwin = max(tp->t_reorderwin, rext); | |
413 | tp->t_reorderwin = min(tp->t_reorderwin, | |
414 | (tp->t_srtt >> (TCP_RTT_SHIFT - 1))); | |
415 | tp->t_reorderwin = max(tp->t_reorderwin, 10); | |
416 | } | |
417 | } | |
418 | } | |
8ad349bb A |
419 | |
420 | /* | |
421 | * Process cumulative ACK and the TCP SACK option to update the scoreboard. | |
422 | * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of | |
423 | * the sequence space). | |
424 | */ | |
425 | void | |
fe8ab488 | 426 | tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th, |
39236c6e | 427 | u_int32_t *newbytes_acked) |
8ad349bb A |
428 | { |
429 | struct sackhole *cur, *temp; | |
430 | struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp; | |
431 | int i, j, num_sack_blks; | |
fe8ab488 | 432 | tcp_seq old_snd_fack = 0, th_ack = th->th_ack; |
8ad349bb A |
433 | |
434 | num_sack_blks = 0; | |
435 | /* | |
436 | * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist, | |
437 | * treat [SND.UNA, SEG.ACK) as if it is a SACK block. | |
438 | */ | |
439 | if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) { | |
440 | sack_blocks[num_sack_blks].start = tp->snd_una; | |
441 | sack_blocks[num_sack_blks++].end = th_ack; | |
442 | } | |
443 | /* | |
444 | * Append received valid SACK blocks to sack_blocks[]. | |
b0d623f7 | 445 | * Check that the SACK block range is valid. |
8ad349bb | 446 | */ |
39236c6e A |
447 | for (i = 0; i < to->to_nsacks; i++) { |
448 | bcopy((to->to_sacks + i * TCPOLEN_SACK), | |
449 | &sack, sizeof(sack)); | |
450 | sack.start = ntohl(sack.start); | |
451 | sack.end = ntohl(sack.end); | |
3e170ce0 | 452 | if (TCP_VALIDATE_SACK_SEQ_NUMBERS(tp, &sack, th_ack)) |
39236c6e | 453 | sack_blocks[num_sack_blks++] = sack; |
8ad349bb A |
454 | } |
455 | ||
456 | /* | |
457 | * Return if SND.UNA is not advanced and no valid SACK block | |
458 | * is received. | |
459 | */ | |
460 | if (num_sack_blks == 0) | |
461 | return; | |
462 | ||
fe8ab488 | 463 | VERIFY(num_sack_blks <= (TCP_MAX_SACK + 1)); |
8ad349bb A |
464 | /* |
465 | * Sort the SACK blocks so we can update the scoreboard | |
466 | * with just one pass. The overhead of sorting upto 4+1 elements | |
467 | * is less than making upto 4+1 passes over the scoreboard. | |
468 | */ | |
469 | for (i = 0; i < num_sack_blks; i++) { | |
470 | for (j = i + 1; j < num_sack_blks; j++) { | |
471 | if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) { | |
472 | sack = sack_blocks[i]; | |
473 | sack_blocks[i] = sack_blocks[j]; | |
474 | sack_blocks[j] = sack; | |
475 | } | |
476 | } | |
477 | } | |
39236c6e | 478 | if (TAILQ_EMPTY(&tp->snd_holes)) { |
8ad349bb A |
479 | /* |
480 | * Empty scoreboard. Need to initialize snd_fack (it may be | |
481 | * uninitialized or have a bogus value). Scoreboard holes | |
482 | * (from the sack blocks received) are created later below (in | |
483 | * the logic that adds holes to the tail of the scoreboard). | |
484 | */ | |
485 | tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack); | |
39236c6e A |
486 | *newbytes_acked += (tp->snd_fack - tp->snd_una); |
487 | } | |
488 | ||
fe8ab488 | 489 | old_snd_fack = tp->snd_fack; |
8ad349bb A |
490 | /* |
491 | * In the while-loop below, incoming SACK blocks (sack_blocks[]) | |
492 | * and SACK holes (snd_holes) are traversed from their tails with | |
493 | * just one pass in order to reduce the number of compares especially | |
494 | * when the bandwidth-delay product is large. | |
495 | * Note: Typically, in the first RTT of SACK recovery, the highest | |
496 | * three or four SACK blocks with the same ack number are received. | |
497 | * In the second RTT, if retransmitted data segments are not lost, | |
498 | * the highest three or four SACK blocks with ack number advancing | |
499 | * are received. | |
500 | */ | |
501 | sblkp = &sack_blocks[num_sack_blks - 1]; /* Last SACK block */ | |
502 | if (SEQ_LT(tp->snd_fack, sblkp->start)) { | |
503 | /* | |
504 | * The highest SACK block is beyond fack. | |
505 | * Append new SACK hole at the tail. | |
506 | * If the second or later highest SACK blocks are also | |
507 | * beyond the current fack, they will be inserted by | |
508 | * way of hole splitting in the while-loop below. | |
509 | */ | |
510 | temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL); | |
511 | if (temp != NULL) { | |
512 | tp->snd_fack = sblkp->end; | |
39236c6e A |
513 | *newbytes_acked += (sblkp->end - sblkp->start); |
514 | ||
8ad349bb A |
515 | /* Go to the previous sack block. */ |
516 | sblkp--; | |
517 | } else { | |
518 | /* | |
519 | * We failed to add a new hole based on the current | |
520 | * sack block. Skip over all the sack blocks that | |
521 | * fall completely to the right of snd_fack and proceed | |
522 | * to trim the scoreboard based on the remaining sack | |
523 | * blocks. This also trims the scoreboard for th_ack | |
524 | * (which is sack_blocks[0]). | |
525 | */ | |
526 | while (sblkp >= sack_blocks && | |
527 | SEQ_LT(tp->snd_fack, sblkp->start)) | |
528 | sblkp--; | |
529 | if (sblkp >= sack_blocks && | |
39236c6e A |
530 | SEQ_LT(tp->snd_fack, sblkp->end)) { |
531 | *newbytes_acked += (sblkp->end - tp->snd_fack); | |
8ad349bb | 532 | tp->snd_fack = sblkp->end; |
39236c6e | 533 | } |
8ad349bb | 534 | } |
39236c6e | 535 | } else if (SEQ_LT(tp->snd_fack, sblkp->end)) { |
8ad349bb | 536 | /* fack is advanced. */ |
39236c6e | 537 | *newbytes_acked += (sblkp->end - tp->snd_fack); |
8ad349bb | 538 | tp->snd_fack = sblkp->end; |
39236c6e | 539 | } |
8ad349bb A |
540 | /* We must have at least one SACK hole in scoreboard */ |
541 | cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole */ | |
542 | /* | |
543 | * Since the incoming sack blocks are sorted, we can process them | |
544 | * making one sweep of the scoreboard. | |
545 | */ | |
546 | while (sblkp >= sack_blocks && cur != NULL) { | |
547 | if (SEQ_GEQ(sblkp->start, cur->end)) { | |
548 | /* | |
549 | * SACKs data beyond the current hole. | |
550 | * Go to the previous sack block. | |
551 | */ | |
552 | sblkp--; | |
553 | continue; | |
554 | } | |
555 | if (SEQ_LEQ(sblkp->end, cur->start)) { | |
556 | /* | |
557 | * SACKs data before the current hole. | |
558 | * Go to the previous hole. | |
559 | */ | |
560 | cur = TAILQ_PREV(cur, sackhole_head, scblink); | |
561 | continue; | |
562 | } | |
563 | tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start); | |
564 | if (SEQ_LEQ(sblkp->start, cur->start)) { | |
565 | /* Data acks at least the beginning of hole */ | |
566 | if (SEQ_GEQ(sblkp->end, cur->end)) { | |
567 | /* Acks entire hole, so delete hole */ | |
39236c6e | 568 | *newbytes_acked += (cur->end - cur->start); |
fe8ab488 A |
569 | |
570 | tcp_sack_detect_reordering(tp, cur, | |
571 | cur->end, old_snd_fack); | |
8ad349bb A |
572 | temp = cur; |
573 | cur = TAILQ_PREV(cur, sackhole_head, scblink); | |
574 | tcp_sackhole_remove(tp, temp); | |
575 | /* | |
576 | * The sack block may ack all or part of the next | |
577 | * hole too, so continue onto the next hole. | |
578 | */ | |
579 | continue; | |
580 | } else { | |
581 | /* Move start of hole forward */ | |
39236c6e | 582 | *newbytes_acked += (sblkp->end - cur->start); |
fe8ab488 A |
583 | tcp_sack_detect_reordering(tp, cur, |
584 | sblkp->end, old_snd_fack); | |
8ad349bb A |
585 | cur->start = sblkp->end; |
586 | cur->rxmit = SEQ_MAX(cur->rxmit, cur->start); | |
587 | } | |
588 | } else { | |
589 | /* Data acks at least the end of hole */ | |
590 | if (SEQ_GEQ(sblkp->end, cur->end)) { | |
591 | /* Move end of hole backward */ | |
39236c6e | 592 | *newbytes_acked += (cur->end - sblkp->start); |
fe8ab488 A |
593 | tcp_sack_detect_reordering(tp, cur, |
594 | cur->end, old_snd_fack); | |
8ad349bb A |
595 | cur->end = sblkp->start; |
596 | cur->rxmit = SEQ_MIN(cur->rxmit, cur->end); | |
597 | } else { | |
598 | /* | |
fe8ab488 A |
599 | * ACKs some data in the middle of a hole; |
600 | * need to split current hole | |
8ad349bb | 601 | */ |
39236c6e | 602 | *newbytes_acked += (sblkp->end - sblkp->start); |
fe8ab488 A |
603 | tcp_sack_detect_reordering(tp, cur, |
604 | sblkp->end, old_snd_fack); | |
8ad349bb | 605 | temp = tcp_sackhole_insert(tp, sblkp->end, |
fe8ab488 | 606 | cur->end, cur); |
8ad349bb A |
607 | if (temp != NULL) { |
608 | if (SEQ_GT(cur->rxmit, temp->rxmit)) { | |
609 | temp->rxmit = cur->rxmit; | |
610 | tp->sackhint.sack_bytes_rexmit | |
611 | += (temp->rxmit | |
612 | - temp->start); | |
613 | } | |
614 | cur->end = sblkp->start; | |
615 | cur->rxmit = SEQ_MIN(cur->rxmit, | |
616 | cur->end); | |
fe8ab488 A |
617 | /* |
618 | * Reset the rxmit_start to that of | |
619 | * the current hole as that will | |
620 | * help to compute the reorder | |
621 | * window correctly | |
622 | */ | |
623 | temp->rxmit_start = cur->rxmit_start; | |
8ad349bb A |
624 | } |
625 | } | |
626 | } | |
627 | tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start); | |
628 | /* | |
629 | * Testing sblkp->start against cur->start tells us whether | |
630 | * we're done with the sack block or the sack hole. | |
631 | * Accordingly, we advance one or the other. | |
632 | */ | |
633 | if (SEQ_LEQ(sblkp->start, cur->start)) | |
634 | cur = TAILQ_PREV(cur, sackhole_head, scblink); | |
635 | else | |
636 | sblkp--; | |
637 | } | |
638 | } | |
639 | ||
640 | /* | |
641 | * Free all SACK holes to clear the scoreboard. | |
642 | */ | |
643 | void | |
644 | tcp_free_sackholes(struct tcpcb *tp) | |
645 | { | |
646 | struct sackhole *q; | |
647 | ||
648 | while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL) | |
649 | tcp_sackhole_remove(tp, q); | |
650 | tp->sackhint.sack_bytes_rexmit = 0; | |
b0d623f7 A |
651 | tp->sackhint.nexthole = NULL; |
652 | tp->sack_newdata = 0; | |
8ad349bb A |
653 | |
654 | } | |
655 | ||
656 | /* | |
657 | * Partial ack handling within a sack recovery episode. | |
658 | * Keeping this very simple for now. When a partial ack | |
659 | * is received, force snd_cwnd to a value that will allow | |
660 | * the sender to transmit no more than 2 segments. | |
661 | * If necessary, a better scheme can be adopted at a | |
662 | * later point, but for now, the goal is to prevent the | |
663 | * sender from bursting a large amount of data in the midst | |
664 | * of sack recovery. | |
665 | */ | |
666 | void | |
39037602 | 667 | tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th) |
8ad349bb A |
668 | { |
669 | int num_segs = 1; | |
670 | ||
671 | tp->t_timer[TCPT_REXMT] = 0; | |
672 | tp->t_rtttime = 0; | |
673 | /* send one or 2 segments based on how much new data was acked */ | |
39236c6e | 674 | if (((BYTES_ACKED(th, tp)) / tp->t_maxseg) > 2) |
8ad349bb A |
675 | num_segs = 2; |
676 | tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit + | |
677 | (tp->snd_nxt - tp->sack_newdata) + | |
678 | num_segs * tp->t_maxseg); | |
679 | if (tp->snd_cwnd > tp->snd_ssthresh) | |
680 | tp->snd_cwnd = tp->snd_ssthresh; | |
3e170ce0 A |
681 | if (SEQ_LT(tp->snd_fack, tp->snd_recover) && |
682 | tp->snd_fack == th->th_ack && TAILQ_EMPTY(&tp->snd_holes)) { | |
683 | struct sackhole *temp; | |
684 | /* | |
685 | * we received a partial ack but there is no sack_hole | |
686 | * that will cover the remaining seq space. In this case, | |
687 | * create a hole from snd_fack to snd_recover so that | |
688 | * the sack recovery will continue. | |
689 | */ | |
690 | temp = tcp_sackhole_insert(tp, tp->snd_fack, | |
691 | tp->snd_recover, NULL); | |
692 | if (temp != NULL) | |
693 | tp->snd_fack = tp->snd_recover; | |
694 | } | |
8ad349bb A |
695 | (void) tcp_output(tp); |
696 | } | |
697 | ||
698 | /* | |
699 | * Debug version of tcp_sack_output() that walks the scoreboard. Used for | |
700 | * now to sanity check the hint. | |
701 | */ | |
702 | static struct sackhole * | |
703 | tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt) | |
704 | { | |
705 | struct sackhole *p; | |
706 | ||
707 | *sack_bytes_rexmt = 0; | |
708 | TAILQ_FOREACH(p, &tp->snd_holes, scblink) { | |
709 | if (SEQ_LT(p->rxmit, p->end)) { | |
710 | if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */ | |
711 | continue; | |
712 | } | |
713 | *sack_bytes_rexmt += (p->rxmit - p->start); | |
714 | break; | |
715 | } | |
716 | *sack_bytes_rexmt += (p->rxmit - p->start); | |
717 | } | |
718 | return (p); | |
719 | } | |
720 | ||
721 | /* | |
722 | * Returns the next hole to retransmit and the number of retransmitted bytes | |
723 | * from the scoreboard. We store both the next hole and the number of | |
724 | * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK | |
725 | * reception). This avoids scoreboard traversals completely. | |
726 | * | |
727 | * The loop here will traverse *at most* one link. Here's the argument. | |
728 | * For the loop to traverse more than 1 link before finding the next hole to | |
729 | * retransmit, we would need to have at least 1 node following the current hint | |
730 | * with (rxmit == end). But, for all holes following the current hint, | |
731 | * (start == rxmit), since we have not yet retransmitted from them. Therefore, | |
732 | * in order to traverse more 1 link in the loop below, we need to have at least | |
733 | * one node following the current hint with (start == rxmit == end). | |
734 | * But that can't happen, (start == end) means that all the data in that hole | |
735 | * has been sacked, in which case, the hole would have been removed from the | |
736 | * scoreboard. | |
737 | */ | |
738 | struct sackhole * | |
739 | tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt) | |
740 | { | |
741 | struct sackhole *hole = NULL, *dbg_hole = NULL; | |
742 | int dbg_bytes_rexmt; | |
743 | ||
744 | dbg_hole = tcp_sack_output_debug(tp, &dbg_bytes_rexmt); | |
745 | *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit; | |
746 | hole = tp->sackhint.nexthole; | |
747 | if (hole == NULL || SEQ_LT(hole->rxmit, hole->end)) | |
748 | goto out; | |
749 | while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) { | |
750 | if (SEQ_LT(hole->rxmit, hole->end)) { | |
751 | tp->sackhint.nexthole = hole; | |
752 | break; | |
753 | } | |
754 | } | |
755 | out: | |
756 | if (dbg_hole != hole) { | |
757 | printf("%s: Computed sack hole not the same as cached value\n", __func__); | |
758 | hole = dbg_hole; | |
759 | } | |
760 | if (*sack_bytes_rexmt != dbg_bytes_rexmt) { | |
761 | printf("%s: Computed sack_bytes_retransmitted (%d) not " | |
762 | "the same as cached value (%d)\n", | |
763 | __func__, dbg_bytes_rexmt, *sack_bytes_rexmt); | |
764 | *sack_bytes_rexmt = dbg_bytes_rexmt; | |
765 | } | |
766 | return (hole); | |
767 | } | |
768 | ||
769 | /* | |
770 | * After a timeout, the SACK list may be rebuilt. This SACK information | |
771 | * should be used to avoid retransmitting SACKed data. This function | |
772 | * traverses the SACK list to see if snd_nxt should be moved forward. | |
773 | */ | |
774 | void | |
775 | tcp_sack_adjust(struct tcpcb *tp) | |
776 | { | |
777 | struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes); | |
778 | ||
779 | if (cur == NULL) | |
780 | return; /* No holes */ | |
781 | if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack)) | |
782 | return; /* We're already beyond any SACKed blocks */ | |
783 | /* | |
784 | * Two cases for which we want to advance snd_nxt: | |
785 | * i) snd_nxt lies between end of one hole and beginning of another | |
786 | * ii) snd_nxt lies between end of last hole and snd_fack | |
787 | */ | |
788 | while ((p = TAILQ_NEXT(cur, scblink)) != NULL) { | |
789 | if (SEQ_LT(tp->snd_nxt, cur->end)) | |
790 | return; | |
791 | if (SEQ_GEQ(tp->snd_nxt, p->start)) | |
792 | cur = p; | |
793 | else { | |
794 | tp->snd_nxt = p->start; | |
795 | return; | |
796 | } | |
797 | } | |
798 | if (SEQ_LT(tp->snd_nxt, cur->end)) | |
799 | return; | |
800 | tp->snd_nxt = tp->snd_fack; | |
801 | return; | |
802 | } | |
fe8ab488 A |
803 | |
804 | /* | |
3e170ce0 | 805 | * This function returns TRUE if more than (tcprexmtthresh - 1) * SMSS |
fe8ab488 A |
806 | * bytes with sequence numbers greater than snd_una have been SACKed. |
807 | */ | |
808 | boolean_t | |
809 | tcp_sack_byte_islost(struct tcpcb *tp) | |
810 | { | |
811 | u_int32_t unacked_bytes, sndhole_bytes = 0; | |
812 | struct sackhole *sndhole; | |
813 | if (!SACK_ENABLED(tp) || IN_FASTRECOVERY(tp) || | |
814 | TAILQ_EMPTY(&tp->snd_holes) || | |
815 | (tp->t_flagsext & TF_PKTS_REORDERED)) | |
816 | return (FALSE); | |
817 | ||
818 | unacked_bytes = tp->snd_max - tp->snd_una; | |
819 | ||
820 | TAILQ_FOREACH(sndhole, &tp->snd_holes, scblink) { | |
821 | sndhole_bytes += (sndhole->end - sndhole->start); | |
822 | } | |
823 | ||
824 | VERIFY(unacked_bytes >= sndhole_bytes); | |
825 | return ((unacked_bytes - sndhole_bytes) > | |
826 | ((tcprexmtthresh - 1) * tp->t_maxseg)); | |
827 | } | |
3e170ce0 A |
828 | |
829 | /* | |
830 | * Process any DSACK options that might be present on an input packet | |
831 | */ | |
832 | ||
833 | boolean_t | |
834 | tcp_sack_process_dsack(struct tcpcb *tp, struct tcpopt *to, | |
835 | struct tcphdr *th) | |
836 | { | |
837 | struct sackblk first_sack, second_sack; | |
838 | struct tcp_rxt_seg *rxseg; | |
839 | ||
840 | bcopy(to->to_sacks, &first_sack, sizeof(first_sack)); | |
841 | first_sack.start = ntohl(first_sack.start); | |
842 | first_sack.end = ntohl(first_sack.end); | |
843 | ||
844 | if (to->to_nsacks > 1) { | |
845 | bcopy((to->to_sacks + TCPOLEN_SACK), &second_sack, | |
846 | sizeof(second_sack)); | |
847 | second_sack.start = ntohl(second_sack.start); | |
848 | second_sack.end = ntohl(second_sack.end); | |
849 | } | |
850 | ||
851 | if (SEQ_LT(first_sack.start, th->th_ack) && | |
852 | SEQ_LEQ(first_sack.end, th->th_ack)) { | |
853 | /* | |
854 | * There is a dsack option reporting a duplicate segment | |
855 | * also covered by cumulative acknowledgement. | |
856 | * | |
857 | * Validate the sequence numbers before looking at dsack | |
858 | * option. The duplicate notification can come after | |
859 | * snd_una moves forward. In order to set a window of valid | |
860 | * sequence numbers to look for, we set a maximum send | |
861 | * window within which the DSACK option will be processed. | |
862 | */ | |
863 | if (!(TCP_DSACK_SEQ_IN_WINDOW(tp, first_sack.start, th->th_ack) && | |
864 | TCP_DSACK_SEQ_IN_WINDOW(tp, first_sack.end, th->th_ack))) { | |
865 | to->to_nsacks--; | |
866 | to->to_sacks += TCPOLEN_SACK; | |
867 | tcpstat.tcps_dsack_recvd_old++; | |
868 | ||
869 | /* | |
870 | * returning true here so that the ack will not be | |
871 | * treated as duplicate ack. | |
872 | */ | |
873 | return (TRUE); | |
874 | } | |
875 | } else if (to->to_nsacks > 1 && | |
876 | SEQ_LEQ(second_sack.start, first_sack.start) && | |
877 | SEQ_GEQ(second_sack.end, first_sack.end)) { | |
878 | /* | |
879 | * there is a dsack option in the first block not | |
880 | * covered by the cumulative acknowledgement but covered | |
881 | * by the second sack block. | |
882 | * | |
883 | * verify the sequence numbes on the second sack block | |
884 | * before processing the DSACK option. Returning false | |
885 | * here will treat the ack as a duplicate ack. | |
886 | */ | |
887 | if (!TCP_VALIDATE_SACK_SEQ_NUMBERS(tp, &second_sack, | |
888 | th->th_ack)) { | |
889 | to->to_nsacks--; | |
890 | to->to_sacks += TCPOLEN_SACK; | |
891 | tcpstat.tcps_dsack_recvd_old++; | |
892 | return (TRUE); | |
893 | } | |
894 | } else { | |
895 | /* no dsack options, proceed with processing the sack */ | |
896 | return (FALSE); | |
897 | } | |
898 | ||
899 | /* Update the tcpopt pointer to exclude dsack block */ | |
900 | to->to_nsacks--; | |
901 | to->to_sacks += TCPOLEN_SACK; | |
902 | tcpstat.tcps_dsack_recvd++; | |
4bd07ac2 | 903 | tp->t_dsack_recvd++; |
3e170ce0 A |
904 | |
905 | /* ignore DSACK option, if DSACK is disabled */ | |
906 | if (tp->t_flagsext & TF_DISABLE_DSACK) | |
907 | return (TRUE); | |
908 | ||
909 | /* If the DSACK is for TLP mark it as such */ | |
910 | if ((tp->t_flagsext & TF_SENT_TLPROBE) && | |
911 | first_sack.end == tp->t_tlphighrxt) { | |
912 | if ((rxseg = tcp_rxtseg_find(tp, first_sack.start, | |
913 | (first_sack.end - 1))) != NULL) | |
914 | rxseg->rx_flags |= TCP_RXT_DSACK_FOR_TLP; | |
915 | } | |
916 | /* Update the sender's retransmit segment state */ | |
917 | if (((tp->t_rxtshift == 1 && first_sack.start == tp->snd_una) || | |
918 | ((tp->t_flagsext & TF_SENT_TLPROBE) && | |
919 | first_sack.end == tp->t_tlphighrxt)) && | |
920 | TAILQ_EMPTY(&tp->snd_holes) && | |
921 | SEQ_GT(th->th_ack, tp->snd_una)) { | |
922 | /* | |
923 | * If the dsack is for a retransmitted packet and one of | |
924 | * the two cases is true, it indicates ack loss: | |
925 | * - retransmit timeout and first_sack.start == snd_una | |
926 | * - TLP probe and first_sack.end == tlphighrxt | |
927 | * | |
928 | * Ignore dsack and do not update state when there is | |
929 | * ack loss | |
930 | */ | |
931 | tcpstat.tcps_dsack_ackloss++; | |
932 | ||
933 | return (TRUE); | |
934 | } else if ((rxseg = tcp_rxtseg_find(tp, first_sack.start, | |
935 | (first_sack.end - 1))) == NULL) { | |
936 | /* | |
937 | * Duplicate notification was not triggered by a | |
938 | * retransmission. This might be due to network duplication, | |
939 | * disable further DSACK processing. | |
940 | */ | |
941 | if (!tcp_dsack_ignore_hw_duplicates) { | |
942 | tp->t_flagsext |= TF_DISABLE_DSACK; | |
943 | tcpstat.tcps_dsack_disable++; | |
944 | } | |
945 | } else { | |
946 | /* | |
947 | * If the segment was retransmitted only once, mark it as | |
948 | * spurious. Otherwise ignore the duplicate notification. | |
949 | */ | |
950 | if (rxseg->rx_count == 1) | |
951 | rxseg->rx_flags |= TCP_RXT_SPURIOUS; | |
952 | else | |
953 | rxseg->rx_flags &= ~TCP_RXT_SPURIOUS; | |
954 | } | |
955 | return (TRUE); | |
956 | } |