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