]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_sack.c
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/sysctl.h>
72 #include <sys/domain.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
77 #include <net/route.h>
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>
85 #include <netinet6/in6_pcb.h>
86 #include <netinet/ip6.h>
87 #include <netinet6/ip6_var.h>
89 #include <netinet/tcp.h>
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>
97 #include <netinet/tcp_debug.h>
99 #include <sys/kdebug.h>
102 #include <netinet6/ipsec.h>
106 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack
, CTLFLAG_RW
, &tcp_do_sack
, 0,
107 "Enable/Disable TCP SACK support");
108 static int tcp_sack_maxholes
= 128;
109 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack_maxholes
, CTLFLAG_RW
,
110 &tcp_sack_maxholes
, 0,
111 "Maximum number of TCP SACK holes allowed per connection");
113 static int tcp_sack_globalmaxholes
= 65536;
114 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack_globalmaxholes
, CTLFLAG_RW
,
115 &tcp_sack_globalmaxholes
, 0,
116 "Global maximum number of TCP SACK holes");
118 static int tcp_sack_globalholes
= 0;
119 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack_globalholes
, CTLFLAG_RD
,
120 &tcp_sack_globalholes
, 0,
121 "Global number of TCP SACK holes currently allocated");
123 extern struct zone
*sack_hole_zone
;
126 * This function is called upon receipt of new valid data (while not in header
127 * prediction mode), and it updates the ordered list of sacks.
130 tcp_update_sack_list(struct tcpcb
*tp
, tcp_seq rcv_start
, tcp_seq rcv_end
)
133 * First reported block MUST be the most recent one. Subsequent
134 * blocks SHOULD be in the order in which they arrived at the
135 * receiver. These two conditions make the implementation fully
136 * compliant with RFC 2018.
138 struct sackblk head_blk
, saved_blks
[MAX_SACK_BLKS
];
139 int num_head
, num_saved
, i
;
141 /* SACK block for the received segment. */
142 head_blk
.start
= rcv_start
;
143 head_blk
.end
= rcv_end
;
146 * Merge updated SACK blocks into head_blk, and
147 * save unchanged SACK blocks into saved_blks[].
148 * num_saved will have the number of the saved SACK blocks.
151 for (i
= 0; i
< tp
->rcv_numsacks
; i
++) {
152 tcp_seq start
= tp
->sackblks
[i
].start
;
153 tcp_seq end
= tp
->sackblks
[i
].end
;
154 if (SEQ_GEQ(start
, end
) || SEQ_LEQ(start
, tp
->rcv_nxt
)) {
156 * Discard this SACK block.
158 } else if (SEQ_LEQ(head_blk
.start
, end
) &&
159 SEQ_GEQ(head_blk
.end
, start
)) {
161 * Merge this SACK block into head_blk.
162 * This SACK block itself will be discarded.
164 if (SEQ_GT(head_blk
.start
, start
))
165 head_blk
.start
= start
;
166 if (SEQ_LT(head_blk
.end
, end
))
170 * Save this SACK block.
172 saved_blks
[num_saved
].start
= start
;
173 saved_blks
[num_saved
].end
= end
;
179 * Update SACK list in tp->sackblks[].
182 if (SEQ_GT(head_blk
.start
, tp
->rcv_nxt
)) {
184 * The received data segment is an out-of-order segment.
185 * Put head_blk at the top of SACK list.
187 tp
->sackblks
[0] = head_blk
;
190 * If the number of saved SACK blocks exceeds its limit,
191 * discard the last SACK block.
193 if (num_saved
>= MAX_SACK_BLKS
)
198 * Copy the saved SACK blocks back.
200 bcopy(saved_blks
, &tp
->sackblks
[num_head
],
201 sizeof(struct sackblk
) * num_saved
);
204 /* Save the number of SACK blocks. */
205 tp
->rcv_numsacks
= num_head
+ num_saved
;
209 * Delete all receiver-side SACK information.
212 tcp_clean_sackreport( struct tcpcb
*tp
)
217 tp->rcv_numsacks = 0;
218 for (i = 0; i < MAX_SACK_BLKS; i++)
219 tp->sackblks[i].start = tp->sackblks[i].end=0;
221 bzero(&tp
->sackblks
[0], sizeof (struct sackblk
) * MAX_SACK_BLKS
);
225 * Allocate struct sackhole.
227 static struct sackhole
*
228 tcp_sackhole_alloc(struct tcpcb
*tp
, tcp_seq start
, tcp_seq end
)
230 struct sackhole
*hole
;
232 if (tp
->snd_numholes
>= tcp_sack_maxholes
||
233 tcp_sack_globalholes
>= tcp_sack_globalmaxholes
) {
234 tcpstat
.tcps_sack_sboverflow
++;
238 hole
= (struct sackhole
*)zalloc_noblock(sack_hole_zone
);
247 tcp_sack_globalholes
++;
253 * Free struct sackhole.
256 tcp_sackhole_free(struct tcpcb
*tp
, struct sackhole
*hole
)
258 zfree(sack_hole_zone
, hole
);
261 tcp_sack_globalholes
--;
265 * Insert new SACK hole into scoreboard.
267 static struct sackhole
*
268 tcp_sackhole_insert(struct tcpcb
*tp
, tcp_seq start
, tcp_seq end
,
269 struct sackhole
*after
)
271 struct sackhole
*hole
;
273 /* Allocate a new SACK hole. */
274 hole
= tcp_sackhole_alloc(tp
, start
, end
);
278 /* Insert the new SACK hole into scoreboard */
280 TAILQ_INSERT_AFTER(&tp
->snd_holes
, after
, hole
, scblink
);
282 TAILQ_INSERT_TAIL(&tp
->snd_holes
, hole
, scblink
);
284 /* Update SACK hint. */
285 if (tp
->sackhint
.nexthole
== NULL
)
286 tp
->sackhint
.nexthole
= hole
;
292 * Remove SACK hole from scoreboard.
295 tcp_sackhole_remove(struct tcpcb
*tp
, struct sackhole
*hole
)
297 /* Update SACK hint. */
298 if (tp
->sackhint
.nexthole
== hole
)
299 tp
->sackhint
.nexthole
= TAILQ_NEXT(hole
, scblink
);
301 /* Remove this SACK hole. */
302 TAILQ_REMOVE(&tp
->snd_holes
, hole
, scblink
);
304 /* Free this SACK hole. */
305 tcp_sackhole_free(tp
, hole
);
309 * Process cumulative ACK and the TCP SACK option to update the scoreboard.
310 * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
311 * the sequence space).
314 tcp_sack_doack(struct tcpcb
*tp
, struct tcpopt
*to
, tcp_seq th_ack
)
316 struct sackhole
*cur
, *temp
;
317 struct sackblk sack
, sack_blocks
[TCP_MAX_SACK
+ 1], *sblkp
;
318 int i
, j
, num_sack_blks
;
322 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
323 * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
325 if (SEQ_LT(tp
->snd_una
, th_ack
) && !TAILQ_EMPTY(&tp
->snd_holes
)) {
326 sack_blocks
[num_sack_blks
].start
= tp
->snd_una
;
327 sack_blocks
[num_sack_blks
++].end
= th_ack
;
330 * Append received valid SACK blocks to sack_blocks[].
332 for (i
= 0; i
< to
->to_nsacks
; i
++) {
333 bcopy((to
->to_sacks
+ i
* TCPOLEN_SACK
), &sack
, sizeof(sack
));
334 sack
.start
= ntohl(sack
.start
);
335 sack
.end
= ntohl(sack
.end
);
336 if (SEQ_GT(sack
.end
, sack
.start
) &&
337 SEQ_GT(sack
.start
, tp
->snd_una
) &&
338 SEQ_GT(sack
.start
, th_ack
) &&
339 SEQ_LEQ(sack
.end
, tp
->snd_max
))
340 sack_blocks
[num_sack_blks
++] = sack
;
344 * Return if SND.UNA is not advanced and no valid SACK block
347 if (num_sack_blks
== 0)
351 * Sort the SACK blocks so we can update the scoreboard
352 * with just one pass. The overhead of sorting upto 4+1 elements
353 * is less than making upto 4+1 passes over the scoreboard.
355 for (i
= 0; i
< num_sack_blks
; i
++) {
356 for (j
= i
+ 1; j
< num_sack_blks
; j
++) {
357 if (SEQ_GT(sack_blocks
[i
].end
, sack_blocks
[j
].end
)) {
358 sack
= sack_blocks
[i
];
359 sack_blocks
[i
] = sack_blocks
[j
];
360 sack_blocks
[j
] = sack
;
364 if (TAILQ_EMPTY(&tp
->snd_holes
))
366 * Empty scoreboard. Need to initialize snd_fack (it may be
367 * uninitialized or have a bogus value). Scoreboard holes
368 * (from the sack blocks received) are created later below (in
369 * the logic that adds holes to the tail of the scoreboard).
371 tp
->snd_fack
= SEQ_MAX(tp
->snd_una
, th_ack
);
373 * In the while-loop below, incoming SACK blocks (sack_blocks[])
374 * and SACK holes (snd_holes) are traversed from their tails with
375 * just one pass in order to reduce the number of compares especially
376 * when the bandwidth-delay product is large.
377 * Note: Typically, in the first RTT of SACK recovery, the highest
378 * three or four SACK blocks with the same ack number are received.
379 * In the second RTT, if retransmitted data segments are not lost,
380 * the highest three or four SACK blocks with ack number advancing
383 sblkp
= &sack_blocks
[num_sack_blks
- 1]; /* Last SACK block */
384 if (SEQ_LT(tp
->snd_fack
, sblkp
->start
)) {
386 * The highest SACK block is beyond fack.
387 * Append new SACK hole at the tail.
388 * If the second or later highest SACK blocks are also
389 * beyond the current fack, they will be inserted by
390 * way of hole splitting in the while-loop below.
392 temp
= tcp_sackhole_insert(tp
, tp
->snd_fack
,sblkp
->start
,NULL
);
394 tp
->snd_fack
= sblkp
->end
;
395 /* Go to the previous sack block. */
399 * We failed to add a new hole based on the current
400 * sack block. Skip over all the sack blocks that
401 * fall completely to the right of snd_fack and proceed
402 * to trim the scoreboard based on the remaining sack
403 * blocks. This also trims the scoreboard for th_ack
404 * (which is sack_blocks[0]).
406 while (sblkp
>= sack_blocks
&&
407 SEQ_LT(tp
->snd_fack
, sblkp
->start
))
409 if (sblkp
>= sack_blocks
&&
410 SEQ_LT(tp
->snd_fack
, sblkp
->end
))
411 tp
->snd_fack
= sblkp
->end
;
413 } else if (SEQ_LT(tp
->snd_fack
, sblkp
->end
))
414 /* fack is advanced. */
415 tp
->snd_fack
= sblkp
->end
;
416 /* We must have at least one SACK hole in scoreboard */
417 cur
= TAILQ_LAST(&tp
->snd_holes
, sackhole_head
); /* Last SACK hole */
419 * Since the incoming sack blocks are sorted, we can process them
420 * making one sweep of the scoreboard.
422 while (sblkp
>= sack_blocks
&& cur
!= NULL
) {
423 if (SEQ_GEQ(sblkp
->start
, cur
->end
)) {
425 * SACKs data beyond the current hole.
426 * Go to the previous sack block.
431 if (SEQ_LEQ(sblkp
->end
, cur
->start
)) {
433 * SACKs data before the current hole.
434 * Go to the previous hole.
436 cur
= TAILQ_PREV(cur
, sackhole_head
, scblink
);
439 tp
->sackhint
.sack_bytes_rexmit
-= (cur
->rxmit
- cur
->start
);
440 if (SEQ_LEQ(sblkp
->start
, cur
->start
)) {
441 /* Data acks at least the beginning of hole */
442 if (SEQ_GEQ(sblkp
->end
, cur
->end
)) {
443 /* Acks entire hole, so delete hole */
445 cur
= TAILQ_PREV(cur
, sackhole_head
, scblink
);
446 tcp_sackhole_remove(tp
, temp
);
448 * The sack block may ack all or part of the next
449 * hole too, so continue onto the next hole.
453 /* Move start of hole forward */
454 cur
->start
= sblkp
->end
;
455 cur
->rxmit
= SEQ_MAX(cur
->rxmit
, cur
->start
);
458 /* Data acks at least the end of hole */
459 if (SEQ_GEQ(sblkp
->end
, cur
->end
)) {
460 /* Move end of hole backward */
461 cur
->end
= sblkp
->start
;
462 cur
->rxmit
= SEQ_MIN(cur
->rxmit
, cur
->end
);
465 * ACKs some data in middle of a hole; need to
468 temp
= tcp_sackhole_insert(tp
, sblkp
->end
,
471 if (SEQ_GT(cur
->rxmit
, temp
->rxmit
)) {
472 temp
->rxmit
= cur
->rxmit
;
473 tp
->sackhint
.sack_bytes_rexmit
477 cur
->end
= sblkp
->start
;
478 cur
->rxmit
= SEQ_MIN(cur
->rxmit
,
483 tp
->sackhint
.sack_bytes_rexmit
+= (cur
->rxmit
- cur
->start
);
485 * Testing sblkp->start against cur->start tells us whether
486 * we're done with the sack block or the sack hole.
487 * Accordingly, we advance one or the other.
489 if (SEQ_LEQ(sblkp
->start
, cur
->start
))
490 cur
= TAILQ_PREV(cur
, sackhole_head
, scblink
);
497 * Free all SACK holes to clear the scoreboard.
500 tcp_free_sackholes(struct tcpcb
*tp
)
504 while ((q
= TAILQ_FIRST(&tp
->snd_holes
)) != NULL
)
505 tcp_sackhole_remove(tp
, q
);
506 tp
->sackhint
.sack_bytes_rexmit
= 0;
511 * Partial ack handling within a sack recovery episode.
512 * Keeping this very simple for now. When a partial ack
513 * is received, force snd_cwnd to a value that will allow
514 * the sender to transmit no more than 2 segments.
515 * If necessary, a better scheme can be adopted at a
516 * later point, but for now, the goal is to prevent the
517 * sender from bursting a large amount of data in the midst
521 tcp_sack_partialack(tp
, th
)
527 tp
->t_timer
[TCPT_REXMT
] = 0;
529 /* send one or 2 segments based on how much new data was acked */
530 if (((th
->th_ack
- tp
->snd_una
) / tp
->t_maxseg
) > 2)
532 tp
->snd_cwnd
= (tp
->sackhint
.sack_bytes_rexmit
+
533 (tp
->snd_nxt
- tp
->sack_newdata
) +
534 num_segs
* tp
->t_maxseg
);
535 if (tp
->snd_cwnd
> tp
->snd_ssthresh
)
536 tp
->snd_cwnd
= tp
->snd_ssthresh
;
537 tp
->t_flags
|= TF_ACKNOW
;
538 (void) tcp_output(tp
);
542 * Debug version of tcp_sack_output() that walks the scoreboard. Used for
543 * now to sanity check the hint.
545 static struct sackhole
*
546 tcp_sack_output_debug(struct tcpcb
*tp
, int *sack_bytes_rexmt
)
550 *sack_bytes_rexmt
= 0;
551 TAILQ_FOREACH(p
, &tp
->snd_holes
, scblink
) {
552 if (SEQ_LT(p
->rxmit
, p
->end
)) {
553 if (SEQ_LT(p
->rxmit
, tp
->snd_una
)) {/* old SACK hole */
556 *sack_bytes_rexmt
+= (p
->rxmit
- p
->start
);
559 *sack_bytes_rexmt
+= (p
->rxmit
- p
->start
);
565 * Returns the next hole to retransmit and the number of retransmitted bytes
566 * from the scoreboard. We store both the next hole and the number of
567 * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
568 * reception). This avoids scoreboard traversals completely.
570 * The loop here will traverse *at most* one link. Here's the argument.
571 * For the loop to traverse more than 1 link before finding the next hole to
572 * retransmit, we would need to have at least 1 node following the current hint
573 * with (rxmit == end). But, for all holes following the current hint,
574 * (start == rxmit), since we have not yet retransmitted from them. Therefore,
575 * in order to traverse more 1 link in the loop below, we need to have at least
576 * one node following the current hint with (start == rxmit == end).
577 * But that can't happen, (start == end) means that all the data in that hole
578 * has been sacked, in which case, the hole would have been removed from the
582 tcp_sack_output(struct tcpcb
*tp
, int *sack_bytes_rexmt
)
584 struct sackhole
*hole
= NULL
, *dbg_hole
= NULL
;
587 dbg_hole
= tcp_sack_output_debug(tp
, &dbg_bytes_rexmt
);
588 *sack_bytes_rexmt
= tp
->sackhint
.sack_bytes_rexmit
;
589 hole
= tp
->sackhint
.nexthole
;
590 if (hole
== NULL
|| SEQ_LT(hole
->rxmit
, hole
->end
))
592 while ((hole
= TAILQ_NEXT(hole
, scblink
)) != NULL
) {
593 if (SEQ_LT(hole
->rxmit
, hole
->end
)) {
594 tp
->sackhint
.nexthole
= hole
;
599 if (dbg_hole
!= hole
) {
600 printf("%s: Computed sack hole not the same as cached value\n", __func__
);
603 if (*sack_bytes_rexmt
!= dbg_bytes_rexmt
) {
604 printf("%s: Computed sack_bytes_retransmitted (%d) not "
605 "the same as cached value (%d)\n",
606 __func__
, dbg_bytes_rexmt
, *sack_bytes_rexmt
);
607 *sack_bytes_rexmt
= dbg_bytes_rexmt
;
613 * After a timeout, the SACK list may be rebuilt. This SACK information
614 * should be used to avoid retransmitting SACKed data. This function
615 * traverses the SACK list to see if snd_nxt should be moved forward.
618 tcp_sack_adjust(struct tcpcb
*tp
)
620 struct sackhole
*p
, *cur
= TAILQ_FIRST(&tp
->snd_holes
);
623 return; /* No holes */
624 if (SEQ_GEQ(tp
->snd_nxt
, tp
->snd_fack
))
625 return; /* We're already beyond any SACKed blocks */
627 * Two cases for which we want to advance snd_nxt:
628 * i) snd_nxt lies between end of one hole and beginning of another
629 * ii) snd_nxt lies between end of last hole and snd_fack
631 while ((p
= TAILQ_NEXT(cur
, scblink
)) != NULL
) {
632 if (SEQ_LT(tp
->snd_nxt
, cur
->end
))
634 if (SEQ_GEQ(tp
->snd_nxt
, p
->start
))
637 tp
->snd_nxt
= p
->start
;
641 if (SEQ_LT(tp
->snd_nxt
, cur
->end
))
643 tp
->snd_nxt
= tp
->snd_fack
;