]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_sack.c
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/sysctl.h>
70 #include <sys/domain.h>
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
75 #include <net/route.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
80 #include <netinet/in_pcb.h>
81 #include <netinet/ip_var.h>
83 #include <netinet6/in6_pcb.h>
84 #include <netinet/ip6.h>
85 #include <netinet6/ip6_var.h>
87 #include <netinet/tcp.h>
89 #include <netinet/tcp_fsm.h>
90 #include <netinet/tcp_seq.h>
91 #include <netinet/tcp_timer.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/tcpip.h>
95 #include <netinet/tcp_debug.h>
97 #include <sys/kdebug.h>
100 #include <netinet6/ipsec.h>
104 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack
, CTLFLAG_RW
, &tcp_do_sack
, 0,
105 "Enable/Disable TCP SACK support");
106 static int tcp_sack_maxholes
= 128;
107 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack_maxholes
, CTLFLAG_RW
,
108 &tcp_sack_maxholes
, 0,
109 "Maximum number of TCP SACK holes allowed per connection");
111 static int tcp_sack_globalmaxholes
= 65536;
112 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack_globalmaxholes
, CTLFLAG_RW
,
113 &tcp_sack_globalmaxholes
, 0,
114 "Global maximum number of TCP SACK holes");
116 static int tcp_sack_globalholes
= 0;
117 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack_globalholes
, CTLFLAG_RD
,
118 &tcp_sack_globalholes
, 0,
119 "Global number of TCP SACK holes currently allocated");
121 extern struct zone
*sack_hole_zone
;
124 * This function is called upon receipt of new valid data (while not in header
125 * prediction mode), and it updates the ordered list of sacks.
128 tcp_update_sack_list(struct tcpcb
*tp
, tcp_seq rcv_start
, tcp_seq rcv_end
)
131 * First reported block MUST be the most recent one. Subsequent
132 * blocks SHOULD be in the order in which they arrived at the
133 * receiver. These two conditions make the implementation fully
134 * compliant with RFC 2018.
136 struct sackblk head_blk
, saved_blks
[MAX_SACK_BLKS
];
137 int num_head
, num_saved
, i
;
139 /* SACK block for the received segment. */
140 head_blk
.start
= rcv_start
;
141 head_blk
.end
= rcv_end
;
144 * Merge updated SACK blocks into head_blk, and
145 * save unchanged SACK blocks into saved_blks[].
146 * num_saved will have the number of the saved SACK blocks.
149 for (i
= 0; i
< tp
->rcv_numsacks
; i
++) {
150 tcp_seq start
= tp
->sackblks
[i
].start
;
151 tcp_seq end
= tp
->sackblks
[i
].end
;
152 if (SEQ_GEQ(start
, end
) || SEQ_LEQ(start
, tp
->rcv_nxt
)) {
154 * Discard this SACK block.
156 } else if (SEQ_LEQ(head_blk
.start
, end
) &&
157 SEQ_GEQ(head_blk
.end
, start
)) {
159 * Merge this SACK block into head_blk.
160 * This SACK block itself will be discarded.
162 if (SEQ_GT(head_blk
.start
, start
))
163 head_blk
.start
= start
;
164 if (SEQ_LT(head_blk
.end
, end
))
168 * Save this SACK block.
170 saved_blks
[num_saved
].start
= start
;
171 saved_blks
[num_saved
].end
= end
;
177 * Update SACK list in tp->sackblks[].
180 if (SEQ_GT(head_blk
.start
, tp
->rcv_nxt
)) {
182 * The received data segment is an out-of-order segment.
183 * Put head_blk at the top of SACK list.
185 tp
->sackblks
[0] = head_blk
;
188 * If the number of saved SACK blocks exceeds its limit,
189 * discard the last SACK block.
191 if (num_saved
>= MAX_SACK_BLKS
)
196 * Copy the saved SACK blocks back.
198 bcopy(saved_blks
, &tp
->sackblks
[num_head
],
199 sizeof(struct sackblk
) * num_saved
);
202 /* Save the number of SACK blocks. */
203 tp
->rcv_numsacks
= num_head
+ num_saved
;
207 * Delete all receiver-side SACK information.
210 tcp_clean_sackreport( struct tcpcb
*tp
)
215 tp->rcv_numsacks = 0;
216 for (i = 0; i < MAX_SACK_BLKS; i++)
217 tp->sackblks[i].start = tp->sackblks[i].end=0;
219 bzero(&tp
->sackblks
[0], sizeof (struct sackblk
) * MAX_SACK_BLKS
);
223 * Allocate struct sackhole.
225 static struct sackhole
*
226 tcp_sackhole_alloc(struct tcpcb
*tp
, tcp_seq start
, tcp_seq end
)
228 struct sackhole
*hole
;
230 if (tp
->snd_numholes
>= tcp_sack_maxholes
||
231 tcp_sack_globalholes
>= tcp_sack_globalmaxholes
) {
232 tcpstat
.tcps_sack_sboverflow
++;
236 hole
= (struct sackhole
*)zalloc_noblock(sack_hole_zone
);
245 tcp_sack_globalholes
++;
251 * Free struct sackhole.
254 tcp_sackhole_free(struct tcpcb
*tp
, struct sackhole
*hole
)
256 zfree(sack_hole_zone
, hole
);
259 tcp_sack_globalholes
--;
263 * Insert new SACK hole into scoreboard.
265 static struct sackhole
*
266 tcp_sackhole_insert(struct tcpcb
*tp
, tcp_seq start
, tcp_seq end
,
267 struct sackhole
*after
)
269 struct sackhole
*hole
;
271 /* Allocate a new SACK hole. */
272 hole
= tcp_sackhole_alloc(tp
, start
, end
);
276 /* Insert the new SACK hole into scoreboard */
278 TAILQ_INSERT_AFTER(&tp
->snd_holes
, after
, hole
, scblink
);
280 TAILQ_INSERT_TAIL(&tp
->snd_holes
, hole
, scblink
);
282 /* Update SACK hint. */
283 if (tp
->sackhint
.nexthole
== NULL
)
284 tp
->sackhint
.nexthole
= hole
;
290 * Remove SACK hole from scoreboard.
293 tcp_sackhole_remove(struct tcpcb
*tp
, struct sackhole
*hole
)
295 /* Update SACK hint. */
296 if (tp
->sackhint
.nexthole
== hole
)
297 tp
->sackhint
.nexthole
= TAILQ_NEXT(hole
, scblink
);
299 /* Remove this SACK hole. */
300 TAILQ_REMOVE(&tp
->snd_holes
, hole
, scblink
);
302 /* Free this SACK hole. */
303 tcp_sackhole_free(tp
, hole
);
307 * Process cumulative ACK and the TCP SACK option to update the scoreboard.
308 * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
309 * the sequence space).
312 tcp_sack_doack(struct tcpcb
*tp
, struct tcpopt
*to
, tcp_seq th_ack
)
314 struct sackhole
*cur
, *temp
;
315 struct sackblk sack
, sack_blocks
[TCP_MAX_SACK
+ 1], *sblkp
;
316 int i
, j
, num_sack_blks
;
320 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
321 * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
323 if (SEQ_LT(tp
->snd_una
, th_ack
) && !TAILQ_EMPTY(&tp
->snd_holes
)) {
324 sack_blocks
[num_sack_blks
].start
= tp
->snd_una
;
325 sack_blocks
[num_sack_blks
++].end
= th_ack
;
328 * Append received valid SACK blocks to sack_blocks[].
330 for (i
= 0; i
< to
->to_nsacks
; i
++) {
331 bcopy((to
->to_sacks
+ i
* TCPOLEN_SACK
), &sack
, sizeof(sack
));
332 sack
.start
= ntohl(sack
.start
);
333 sack
.end
= ntohl(sack
.end
);
334 if (SEQ_GT(sack
.end
, sack
.start
) &&
335 SEQ_GT(sack
.start
, tp
->snd_una
) &&
336 SEQ_GT(sack
.start
, th_ack
) &&
337 SEQ_LEQ(sack
.end
, tp
->snd_max
))
338 sack_blocks
[num_sack_blks
++] = sack
;
342 * Return if SND.UNA is not advanced and no valid SACK block
345 if (num_sack_blks
== 0)
349 * Sort the SACK blocks so we can update the scoreboard
350 * with just one pass. The overhead of sorting upto 4+1 elements
351 * is less than making upto 4+1 passes over the scoreboard.
353 for (i
= 0; i
< num_sack_blks
; i
++) {
354 for (j
= i
+ 1; j
< num_sack_blks
; j
++) {
355 if (SEQ_GT(sack_blocks
[i
].end
, sack_blocks
[j
].end
)) {
356 sack
= sack_blocks
[i
];
357 sack_blocks
[i
] = sack_blocks
[j
];
358 sack_blocks
[j
] = sack
;
362 if (TAILQ_EMPTY(&tp
->snd_holes
))
364 * Empty scoreboard. Need to initialize snd_fack (it may be
365 * uninitialized or have a bogus value). Scoreboard holes
366 * (from the sack blocks received) are created later below (in
367 * the logic that adds holes to the tail of the scoreboard).
369 tp
->snd_fack
= SEQ_MAX(tp
->snd_una
, th_ack
);
371 * In the while-loop below, incoming SACK blocks (sack_blocks[])
372 * and SACK holes (snd_holes) are traversed from their tails with
373 * just one pass in order to reduce the number of compares especially
374 * when the bandwidth-delay product is large.
375 * Note: Typically, in the first RTT of SACK recovery, the highest
376 * three or four SACK blocks with the same ack number are received.
377 * In the second RTT, if retransmitted data segments are not lost,
378 * the highest three or four SACK blocks with ack number advancing
381 sblkp
= &sack_blocks
[num_sack_blks
- 1]; /* Last SACK block */
382 if (SEQ_LT(tp
->snd_fack
, sblkp
->start
)) {
384 * The highest SACK block is beyond fack.
385 * Append new SACK hole at the tail.
386 * If the second or later highest SACK blocks are also
387 * beyond the current fack, they will be inserted by
388 * way of hole splitting in the while-loop below.
390 temp
= tcp_sackhole_insert(tp
, tp
->snd_fack
,sblkp
->start
,NULL
);
392 tp
->snd_fack
= sblkp
->end
;
393 /* Go to the previous sack block. */
397 * We failed to add a new hole based on the current
398 * sack block. Skip over all the sack blocks that
399 * fall completely to the right of snd_fack and proceed
400 * to trim the scoreboard based on the remaining sack
401 * blocks. This also trims the scoreboard for th_ack
402 * (which is sack_blocks[0]).
404 while (sblkp
>= sack_blocks
&&
405 SEQ_LT(tp
->snd_fack
, sblkp
->start
))
407 if (sblkp
>= sack_blocks
&&
408 SEQ_LT(tp
->snd_fack
, sblkp
->end
))
409 tp
->snd_fack
= sblkp
->end
;
411 } else if (SEQ_LT(tp
->snd_fack
, sblkp
->end
))
412 /* fack is advanced. */
413 tp
->snd_fack
= sblkp
->end
;
414 /* We must have at least one SACK hole in scoreboard */
415 cur
= TAILQ_LAST(&tp
->snd_holes
, sackhole_head
); /* Last SACK hole */
417 * Since the incoming sack blocks are sorted, we can process them
418 * making one sweep of the scoreboard.
420 while (sblkp
>= sack_blocks
&& cur
!= NULL
) {
421 if (SEQ_GEQ(sblkp
->start
, cur
->end
)) {
423 * SACKs data beyond the current hole.
424 * Go to the previous sack block.
429 if (SEQ_LEQ(sblkp
->end
, cur
->start
)) {
431 * SACKs data before the current hole.
432 * Go to the previous hole.
434 cur
= TAILQ_PREV(cur
, sackhole_head
, scblink
);
437 tp
->sackhint
.sack_bytes_rexmit
-= (cur
->rxmit
- cur
->start
);
438 if (SEQ_LEQ(sblkp
->start
, cur
->start
)) {
439 /* Data acks at least the beginning of hole */
440 if (SEQ_GEQ(sblkp
->end
, cur
->end
)) {
441 /* Acks entire hole, so delete hole */
443 cur
= TAILQ_PREV(cur
, sackhole_head
, scblink
);
444 tcp_sackhole_remove(tp
, temp
);
446 * The sack block may ack all or part of the next
447 * hole too, so continue onto the next hole.
451 /* Move start of hole forward */
452 cur
->start
= sblkp
->end
;
453 cur
->rxmit
= SEQ_MAX(cur
->rxmit
, cur
->start
);
456 /* Data acks at least the end of hole */
457 if (SEQ_GEQ(sblkp
->end
, cur
->end
)) {
458 /* Move end of hole backward */
459 cur
->end
= sblkp
->start
;
460 cur
->rxmit
= SEQ_MIN(cur
->rxmit
, cur
->end
);
463 * ACKs some data in middle of a hole; need to
466 temp
= tcp_sackhole_insert(tp
, sblkp
->end
,
469 if (SEQ_GT(cur
->rxmit
, temp
->rxmit
)) {
470 temp
->rxmit
= cur
->rxmit
;
471 tp
->sackhint
.sack_bytes_rexmit
475 cur
->end
= sblkp
->start
;
476 cur
->rxmit
= SEQ_MIN(cur
->rxmit
,
481 tp
->sackhint
.sack_bytes_rexmit
+= (cur
->rxmit
- cur
->start
);
483 * Testing sblkp->start against cur->start tells us whether
484 * we're done with the sack block or the sack hole.
485 * Accordingly, we advance one or the other.
487 if (SEQ_LEQ(sblkp
->start
, cur
->start
))
488 cur
= TAILQ_PREV(cur
, sackhole_head
, scblink
);
495 * Free all SACK holes to clear the scoreboard.
498 tcp_free_sackholes(struct tcpcb
*tp
)
502 while ((q
= TAILQ_FIRST(&tp
->snd_holes
)) != NULL
)
503 tcp_sackhole_remove(tp
, q
);
504 tp
->sackhint
.sack_bytes_rexmit
= 0;
509 * Partial ack handling within a sack recovery episode.
510 * Keeping this very simple for now. When a partial ack
511 * is received, force snd_cwnd to a value that will allow
512 * the sender to transmit no more than 2 segments.
513 * If necessary, a better scheme can be adopted at a
514 * later point, but for now, the goal is to prevent the
515 * sender from bursting a large amount of data in the midst
519 tcp_sack_partialack(tp
, th
)
525 tp
->t_timer
[TCPT_REXMT
] = 0;
527 /* send one or 2 segments based on how much new data was acked */
528 if (((th
->th_ack
- tp
->snd_una
) / tp
->t_maxseg
) > 2)
530 tp
->snd_cwnd
= (tp
->sackhint
.sack_bytes_rexmit
+
531 (tp
->snd_nxt
- tp
->sack_newdata
) +
532 num_segs
* tp
->t_maxseg
);
533 if (tp
->snd_cwnd
> tp
->snd_ssthresh
)
534 tp
->snd_cwnd
= tp
->snd_ssthresh
;
535 tp
->t_flags
|= TF_ACKNOW
;
536 (void) tcp_output(tp
);
540 * Debug version of tcp_sack_output() that walks the scoreboard. Used for
541 * now to sanity check the hint.
543 static struct sackhole
*
544 tcp_sack_output_debug(struct tcpcb
*tp
, int *sack_bytes_rexmt
)
548 *sack_bytes_rexmt
= 0;
549 TAILQ_FOREACH(p
, &tp
->snd_holes
, scblink
) {
550 if (SEQ_LT(p
->rxmit
, p
->end
)) {
551 if (SEQ_LT(p
->rxmit
, tp
->snd_una
)) {/* old SACK hole */
554 *sack_bytes_rexmt
+= (p
->rxmit
- p
->start
);
557 *sack_bytes_rexmt
+= (p
->rxmit
- p
->start
);
563 * Returns the next hole to retransmit and the number of retransmitted bytes
564 * from the scoreboard. We store both the next hole and the number of
565 * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
566 * reception). This avoids scoreboard traversals completely.
568 * The loop here will traverse *at most* one link. Here's the argument.
569 * For the loop to traverse more than 1 link before finding the next hole to
570 * retransmit, we would need to have at least 1 node following the current hint
571 * with (rxmit == end). But, for all holes following the current hint,
572 * (start == rxmit), since we have not yet retransmitted from them. Therefore,
573 * in order to traverse more 1 link in the loop below, we need to have at least
574 * one node following the current hint with (start == rxmit == end).
575 * But that can't happen, (start == end) means that all the data in that hole
576 * has been sacked, in which case, the hole would have been removed from the
580 tcp_sack_output(struct tcpcb
*tp
, int *sack_bytes_rexmt
)
582 struct sackhole
*hole
= NULL
, *dbg_hole
= NULL
;
585 dbg_hole
= tcp_sack_output_debug(tp
, &dbg_bytes_rexmt
);
586 *sack_bytes_rexmt
= tp
->sackhint
.sack_bytes_rexmit
;
587 hole
= tp
->sackhint
.nexthole
;
588 if (hole
== NULL
|| SEQ_LT(hole
->rxmit
, hole
->end
))
590 while ((hole
= TAILQ_NEXT(hole
, scblink
)) != NULL
) {
591 if (SEQ_LT(hole
->rxmit
, hole
->end
)) {
592 tp
->sackhint
.nexthole
= hole
;
597 if (dbg_hole
!= hole
) {
598 printf("%s: Computed sack hole not the same as cached value\n", __func__
);
601 if (*sack_bytes_rexmt
!= dbg_bytes_rexmt
) {
602 printf("%s: Computed sack_bytes_retransmitted (%d) not "
603 "the same as cached value (%d)\n",
604 __func__
, dbg_bytes_rexmt
, *sack_bytes_rexmt
);
605 *sack_bytes_rexmt
= dbg_bytes_rexmt
;
611 * After a timeout, the SACK list may be rebuilt. This SACK information
612 * should be used to avoid retransmitting SACKed data. This function
613 * traverses the SACK list to see if snd_nxt should be moved forward.
616 tcp_sack_adjust(struct tcpcb
*tp
)
618 struct sackhole
*p
, *cur
= TAILQ_FIRST(&tp
->snd_holes
);
621 return; /* No holes */
622 if (SEQ_GEQ(tp
->snd_nxt
, tp
->snd_fack
))
623 return; /* We're already beyond any SACKed blocks */
625 * Two cases for which we want to advance snd_nxt:
626 * i) snd_nxt lies between end of one hole and beginning of another
627 * ii) snd_nxt lies between end of last hole and snd_fack
629 while ((p
= TAILQ_NEXT(cur
, scblink
)) != NULL
) {
630 if (SEQ_LT(tp
->snd_nxt
, cur
->end
))
632 if (SEQ_GEQ(tp
->snd_nxt
, p
->start
))
635 tp
->snd_nxt
= p
->start
;
639 if (SEQ_LT(tp
->snd_nxt
, cur
->end
))
641 tp
->snd_nxt
= tp
->snd_fack
;