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