2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
5 * The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)tcp_sack.c 8.12 (Berkeley) 5/24/95
36 * @@(#)COPYRIGHT 1.1 (NRL) 17 January 1995
38 * NRL grants permission for redistribution and use in source and binary
39 * forms, with or without modification, of the software and documentation
40 * created at NRL provided that the following conditions are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgements:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * This product includes software developed at the Information
52 * Technology Division, US Naval Research Laboratory.
53 * 4. Neither the name of the NRL nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
58 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
59 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
60 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
61 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
62 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
63 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
64 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
65 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
66 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
67 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 * The views and conclusions contained in the software and documentation
70 * are those of the authors and should not be interpreted as representing
71 * official policies, either expressed or implied, of the US Naval
72 * Research Laboratory (NRL).
75 #include <sys/cdefs.h>
76 __FBSDID("$FreeBSD$");
79 #include "opt_inet6.h"
80 #include "opt_tcpdebug.h"
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.h>
85 #include <sys/sysctl.h>
86 #include <sys/malloc.h>
88 #include <sys/proc.h> /* for proc0 declaration */
89 #include <sys/protosw.h>
90 #include <sys/socket.h>
91 #include <sys/socketvar.h>
92 #include <sys/syslog.h>
93 #include <sys/systm.h>
95 #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
100 #include <net/if_var.h>
101 #include <net/route.h>
102 #include <net/vnet.h>
104 #include <netinet/in.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/ip.h>
107 #include <netinet/in_var.h>
108 #include <netinet/in_pcb.h>
109 #include <netinet/ip_var.h>
110 #include <netinet/ip6.h>
111 #include <netinet/icmp6.h>
112 #include <netinet6/nd6.h>
113 #include <netinet6/ip6_var.h>
114 #include <netinet6/in6_pcb.h>
115 #include <netinet/tcp.h>
116 #include <netinet/tcp_fsm.h>
117 #include <netinet/tcp_seq.h>
118 #include <netinet/tcp_timer.h>
119 #include <netinet/tcp_var.h>
120 #include <netinet6/tcp6_var.h>
121 #include <netinet/tcpip.h>
123 #include <netinet/tcp_debug.h>
124 #endif /* TCPDEBUG */
126 #include <machine/in_cksum.h>
128 VNET_DECLARE(struct uma_zone *, sack_hole_zone);
129 #define V_sack_hole_zone VNET(sack_hole_zone)
131 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
133 VNET_DEFINE(int, tcp_do_sack) = 1;
134 #define V_tcp_do_sack VNET(tcp_do_sack)
135 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
136 &VNET_NAME(tcp_do_sack), 0, "Enable/Disable TCP SACK support");
138 VNET_DEFINE(int, tcp_sack_maxholes) = 128;
139 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_VNET | CTLFLAG_RW,
140 &VNET_NAME(tcp_sack_maxholes), 0,
141 "Maximum number of TCP SACK holes allowed per connection");
143 VNET_DEFINE(int, tcp_sack_globalmaxholes) = 65536;
144 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_VNET | CTLFLAG_RW,
145 &VNET_NAME(tcp_sack_globalmaxholes), 0,
146 "Global maximum number of TCP SACK holes");
148 VNET_DEFINE(int, tcp_sack_globalholes) = 0;
149 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_VNET | CTLFLAG_RD,
150 &VNET_NAME(tcp_sack_globalholes), 0,
151 "Global number of TCP SACK holes currently allocated");
154 tcp_dsack_block_exists(struct tcpcb *tp)
156 /* Return true if a DSACK block exists */
157 if (tp->rcv_numsacks == 0)
159 if (SEQ_LEQ(tp->sackblks[0].end, tp->rcv_nxt))
165 * This function will find overlaps with the currently stored sackblocks
166 * and add any overlap as a dsack block upfront
169 tcp_update_dsack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
171 struct sackblk head_blk,mid_blk,saved_blks[MAX_SACK_BLKS];
172 int i, j, n, identical;
175 INP_WLOCK_ASSERT(tp->t_inpcb);
177 KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end"));
179 if (SEQ_LT(rcv_end, tp->rcv_nxt) ||
180 ((rcv_end == tp->rcv_nxt) &&
181 (tp->rcv_numsacks > 0 ) &&
182 (tp->sackblks[0].end == tp->rcv_nxt))) {
183 saved_blks[0].start = rcv_start;
184 saved_blks[0].end = rcv_end;
186 saved_blks[0].start = saved_blks[0].end = 0;
189 head_blk.start = head_blk.end = 0;
190 mid_blk.start = rcv_start;
191 mid_blk.end = rcv_end;
194 for (i = 0; i < tp->rcv_numsacks; i++) {
195 start = tp->sackblks[i].start;
196 end = tp->sackblks[i].end;
197 if (SEQ_LT(rcv_end, start)) {
198 /* pkt left to sack blk */
201 if (SEQ_GT(rcv_start, end)) {
202 /* pkt right to sack blk */
205 if (SEQ_GT(tp->rcv_nxt, end)) {
206 if ((SEQ_MAX(rcv_start, start) != SEQ_MIN(rcv_end, end)) &&
207 (SEQ_GT(head_blk.start, SEQ_MAX(rcv_start, start)) ||
208 (head_blk.start == head_blk.end))) {
209 head_blk.start = SEQ_MAX(rcv_start, start);
210 head_blk.end = SEQ_MIN(rcv_end, end);
214 if (((head_blk.start == head_blk.end) ||
215 SEQ_LT(start, head_blk.start)) &&
216 (SEQ_GT(end, rcv_start) &&
217 SEQ_LEQ(start, rcv_end))) {
218 head_blk.start = start;
221 mid_blk.start = SEQ_MIN(mid_blk.start, start);
222 mid_blk.end = SEQ_MAX(mid_blk.end, end);
223 if ((mid_blk.start == start) &&
224 (mid_blk.end == end))
227 if (SEQ_LT(head_blk.start, head_blk.end)) {
228 /* store overlapping range */
229 saved_blks[0].start = SEQ_MAX(rcv_start, head_blk.start);
230 saved_blks[0].end = SEQ_MIN(rcv_end, head_blk.end);
234 * Second, if not ACKed, store the SACK block that
235 * overlaps with the DSACK block unless it is identical
237 if ((SEQ_LT(tp->rcv_nxt, mid_blk.end) &&
238 !((mid_blk.start == saved_blks[0].start) &&
239 (mid_blk.end == saved_blks[0].end))) ||
241 saved_blks[n].start = mid_blk.start;
242 saved_blks[n++].end = mid_blk.end;
244 for (j = 0; (j < tp->rcv_numsacks) && (n < MAX_SACK_BLKS); j++) {
245 if (((SEQ_LT(tp->sackblks[j].end, mid_blk.start) ||
246 SEQ_GT(tp->sackblks[j].start, mid_blk.end)) &&
247 (SEQ_GT(tp->sackblks[j].start, tp->rcv_nxt))))
248 saved_blks[n++] = tp->sackblks[j];
251 for (i = 0; i < n; i++) {
252 /* we can end up with a stale initial entry */
253 if (SEQ_LT(saved_blks[i].start, saved_blks[i].end)) {
254 tp->sackblks[j++] = saved_blks[i];
257 tp->rcv_numsacks = j;
261 * This function is called upon receipt of new valid data (while not in
262 * header prediction mode), and it updates the ordered list of sacks.
265 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
268 * First reported block MUST be the most recent one. Subsequent
269 * blocks SHOULD be in the order in which they arrived at the
270 * receiver. These two conditions make the implementation fully
271 * compliant with RFC 2018.
273 struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
274 int num_head, num_saved, i;
276 INP_WLOCK_ASSERT(tp->t_inpcb);
278 /* Check arguments. */
279 KASSERT(SEQ_LEQ(rcv_start, rcv_end), ("rcv_start <= rcv_end"));
281 if ((rcv_start == rcv_end) &&
282 (tp->rcv_numsacks >= 1) &&
283 (rcv_end == tp->sackblks[0].end)) {
284 /* retaining DSACK block below rcv_nxt (todrop) */
285 head_blk = tp->sackblks[0];
287 /* SACK block for the received segment. */
288 head_blk.start = rcv_start;
289 head_blk.end = rcv_end;
293 * Merge updated SACK blocks into head_blk, and save unchanged SACK
294 * blocks into saved_blks[]. num_saved will have the number of the
298 for (i = 0; i < tp->rcv_numsacks; i++) {
299 tcp_seq start = tp->sackblks[i].start;
300 tcp_seq end = tp->sackblks[i].end;
301 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
303 * Discard this SACK block.
305 } else if (SEQ_LEQ(head_blk.start, end) &&
306 SEQ_GEQ(head_blk.end, start)) {
308 * Merge this SACK block into head_blk. This SACK
309 * block itself will be discarded.
322 * |-----| DSACK smaller
324 if (head_blk.start == end)
325 head_blk.start = start;
326 else if (head_blk.end == start)
329 if (SEQ_LT(head_blk.start, start)) {
330 tcp_seq temp = start;
331 start = head_blk.start;
332 head_blk.start = temp;
334 if (SEQ_GT(head_blk.end, end)) {
339 if ((head_blk.start != start) ||
340 (head_blk.end != end)) {
341 if ((num_saved >= 1) &&
342 SEQ_GEQ(saved_blks[num_saved-1].start, start) &&
343 SEQ_LEQ(saved_blks[num_saved-1].end, end))
345 saved_blks[num_saved].start = start;
346 saved_blks[num_saved].end = end;
352 * This block supercedes the prior block
354 if ((num_saved >= 1) &&
355 SEQ_GEQ(saved_blks[num_saved-1].start, start) &&
356 SEQ_LEQ(saved_blks[num_saved-1].end, end))
359 * Save this SACK block.
361 saved_blks[num_saved].start = start;
362 saved_blks[num_saved].end = end;
368 * Update SACK list in tp->sackblks[].
371 if (SEQ_LT(rcv_start, rcv_end)) {
373 * The received data segment is an out-of-order segment. Put
374 * head_blk at the top of SACK list.
376 tp->sackblks[0] = head_blk;
379 * If the number of saved SACK blocks exceeds its limit,
380 * discard the last SACK block.
382 if (num_saved >= MAX_SACK_BLKS)
385 if ((rcv_start == rcv_end) &&
386 (rcv_start == tp->sackblks[0].end)) {
391 * Copy the saved SACK blocks back.
393 bcopy(saved_blks, &tp->sackblks[num_head],
394 sizeof(struct sackblk) * num_saved);
397 /* Save the number of SACK blocks. */
398 tp->rcv_numsacks = num_head + num_saved;
402 tcp_clean_dsack_blocks(struct tcpcb *tp)
404 struct sackblk saved_blks[MAX_SACK_BLKS];
407 INP_WLOCK_ASSERT(tp->t_inpcb);
409 * Clean up any DSACK blocks that
410 * are in our queue of sack blocks.
414 for (i = 0; i < tp->rcv_numsacks; i++) {
415 tcp_seq start = tp->sackblks[i].start;
416 tcp_seq end = tp->sackblks[i].end;
417 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
419 * Discard this D-SACK block.
424 * Save this SACK block.
426 saved_blks[num_saved].start = start;
427 saved_blks[num_saved].end = end;
432 * Copy the saved SACK blocks back.
434 bcopy(saved_blks, &tp->sackblks[0],
435 sizeof(struct sackblk) * num_saved);
437 tp->rcv_numsacks = num_saved;
441 * Delete all receiver-side SACK information.
444 tcp_clean_sackreport(struct tcpcb *tp)
448 INP_WLOCK_ASSERT(tp->t_inpcb);
449 tp->rcv_numsacks = 0;
450 for (i = 0; i < MAX_SACK_BLKS; i++)
451 tp->sackblks[i].start = tp->sackblks[i].end=0;
455 * Allocate struct sackhole.
457 static struct sackhole *
458 tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
460 struct sackhole *hole;
462 if (tp->snd_numholes >= V_tcp_sack_maxholes ||
463 V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) {
464 TCPSTAT_INC(tcps_sack_sboverflow);
468 hole = (struct sackhole *)uma_zalloc(V_sack_hole_zone, M_NOWAIT);
477 atomic_add_int(&V_tcp_sack_globalholes, 1);
483 * Free struct sackhole.
486 tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
489 uma_zfree(V_sack_hole_zone, hole);
492 atomic_subtract_int(&V_tcp_sack_globalholes, 1);
494 KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
495 KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
499 * Insert new SACK hole into scoreboard.
501 static struct sackhole *
502 tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
503 struct sackhole *after)
505 struct sackhole *hole;
507 /* Allocate a new SACK hole. */
508 hole = tcp_sackhole_alloc(tp, start, end);
512 /* Insert the new SACK hole into scoreboard. */
514 TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
516 TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
518 /* Update SACK hint. */
519 if (tp->sackhint.nexthole == NULL)
520 tp->sackhint.nexthole = hole;
526 * Remove SACK hole from scoreboard.
529 tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
532 /* Update SACK hint. */
533 if (tp->sackhint.nexthole == hole)
534 tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
536 /* Remove this SACK hole. */
537 TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
539 /* Free this SACK hole. */
540 tcp_sackhole_free(tp, hole);
544 * Process cumulative ACK and the TCP SACK option to update the scoreboard.
545 * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
546 * the sequence space).
547 * Returns 1 if incoming ACK has previously unknown SACK information,
551 tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack)
553 struct sackhole *cur, *temp;
554 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp;
555 int i, j, num_sack_blks, sack_changed;
556 int delivered_data, left_edge_delta;
558 INP_WLOCK_ASSERT(tp->t_inpcb);
565 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
566 * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
567 * Account changes to SND.UNA always in delivered data.
569 if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
570 left_edge_delta = th_ack - tp->snd_una;
571 sack_blocks[num_sack_blks].start = tp->snd_una;
572 sack_blocks[num_sack_blks++].end = th_ack;
574 * Pulling snd_fack forward if we got here
575 * due to DSACK blocks
577 if (SEQ_LT(tp->snd_fack, th_ack)) {
578 delivered_data += th_ack - tp->snd_una;
579 tp->snd_fack = th_ack;
584 * Append received valid SACK blocks to sack_blocks[], but only if we
585 * received new blocks from the other side.
587 if (to->to_flags & TOF_SACK) {
588 for (i = 0; i < to->to_nsacks; i++) {
589 bcopy((to->to_sacks + i * TCPOLEN_SACK),
590 &sack, sizeof(sack));
591 sack.start = ntohl(sack.start);
592 sack.end = ntohl(sack.end);
593 if (SEQ_GT(sack.end, sack.start) &&
594 SEQ_GT(sack.start, tp->snd_una) &&
595 SEQ_GT(sack.start, th_ack) &&
596 SEQ_LT(sack.start, tp->snd_max) &&
597 SEQ_GT(sack.end, tp->snd_una) &&
598 SEQ_LEQ(sack.end, tp->snd_max)) {
599 sack_blocks[num_sack_blks++] = sack;
604 * Return if SND.UNA is not advanced and no valid SACK block is
607 if (num_sack_blks == 0)
608 return (sack_changed);
611 * Sort the SACK blocks so we can update the scoreboard with just one
612 * pass. The overhead of sorting up to 4+1 elements is less than
613 * making up to 4+1 passes over the scoreboard.
615 for (i = 0; i < num_sack_blks; i++) {
616 for (j = i + 1; j < num_sack_blks; j++) {
617 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
618 sack = sack_blocks[i];
619 sack_blocks[i] = sack_blocks[j];
620 sack_blocks[j] = sack;
624 if (TAILQ_EMPTY(&tp->snd_holes)) {
626 * Empty scoreboard. Need to initialize snd_fack (it may be
627 * uninitialized or have a bogus value). Scoreboard holes
628 * (from the sack blocks received) are created later below
629 * (in the logic that adds holes to the tail of the
632 tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
633 tp->sackhint.sacked_bytes = 0; /* reset */
636 * In the while-loop below, incoming SACK blocks (sack_blocks[]) and
637 * SACK holes (snd_holes) are traversed from their tails with just
638 * one pass in order to reduce the number of compares especially when
639 * the bandwidth-delay product is large.
641 * Note: Typically, in the first RTT of SACK recovery, the highest
642 * three or four SACK blocks with the same ack number are received.
643 * In the second RTT, if retransmitted data segments are not lost,
644 * the highest three or four SACK blocks with ack number advancing
647 sblkp = &sack_blocks[num_sack_blks - 1]; /* Last SACK block */
648 tp->sackhint.last_sack_ack = sblkp->end;
649 if (SEQ_LT(tp->snd_fack, sblkp->start)) {
651 * The highest SACK block is beyond fack. First,
652 * check if there was a successful Rescue Retransmission,
653 * and move this hole left. With normal holes, snd_fack
654 * is always to the right of the end.
656 if (((temp = TAILQ_LAST(&tp->snd_holes, sackhole_head)) != NULL) &&
657 SEQ_LEQ(tp->snd_fack,temp->end)) {
658 temp->start = SEQ_MAX(tp->snd_fack, SEQ_MAX(tp->snd_una, th_ack));
659 temp->end = sblkp->start;
660 temp->rxmit = temp->start;
661 delivered_data += sblkp->end - sblkp->start;
662 tp->snd_fack = sblkp->end;
667 * Append a new SACK hole at the tail. If the
668 * second or later highest SACK blocks are also
669 * beyond the current fack, they will be inserted
670 * by way of hole splitting in the while-loop below.
672 temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL);
674 delivered_data += sblkp->end - sblkp->start;
675 tp->snd_fack = sblkp->end;
676 /* Go to the previous sack block. */
681 * We failed to add a new hole based on the current
682 * sack block. Skip over all the sack blocks that
683 * fall completely to the right of snd_fack and
684 * proceed to trim the scoreboard based on the
685 * remaining sack blocks. This also trims the
686 * scoreboard for th_ack (which is sack_blocks[0]).
688 while (sblkp >= sack_blocks &&
689 SEQ_LT(tp->snd_fack, sblkp->start))
691 if (sblkp >= sack_blocks &&
692 SEQ_LT(tp->snd_fack, sblkp->end)) {
693 delivered_data += sblkp->end - tp->snd_fack;
694 tp->snd_fack = sblkp->end;
699 } else if (SEQ_LT(tp->snd_fack, sblkp->end)) {
700 /* fack is advanced. */
701 delivered_data += sblkp->end - tp->snd_fack;
702 tp->snd_fack = sblkp->end;
705 cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */
707 * Since the incoming sack blocks are sorted, we can process them
708 * making one sweep of the scoreboard.
710 while (sblkp >= sack_blocks && cur != NULL) {
711 if (SEQ_GEQ(sblkp->start, cur->end)) {
713 * SACKs data beyond the current hole. Go to the
714 * previous sack block.
719 if (SEQ_LEQ(sblkp->end, cur->start)) {
721 * SACKs data before the current hole. Go to the
724 cur = TAILQ_PREV(cur, sackhole_head, scblink);
727 tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
728 KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
729 ("sackhint bytes rtx >= 0"));
731 if (SEQ_LEQ(sblkp->start, cur->start)) {
732 /* Data acks at least the beginning of hole. */
733 if (SEQ_GEQ(sblkp->end, cur->end)) {
734 /* Acks entire hole, so delete hole. */
735 delivered_data += (cur->end - cur->start);
737 cur = TAILQ_PREV(cur, sackhole_head, scblink);
738 tcp_sackhole_remove(tp, temp);
740 * The sack block may ack all or part of the
741 * next hole too, so continue onto the next
746 /* Move start of hole forward. */
747 delivered_data += (sblkp->end - cur->start);
748 cur->start = sblkp->end;
749 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
752 /* Data acks at least the end of hole. */
753 if (SEQ_GEQ(sblkp->end, cur->end)) {
754 /* Move end of hole backward. */
755 delivered_data += (cur->end - sblkp->start);
756 cur->end = sblkp->start;
757 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
760 * ACKs some data in middle of a hole; need
761 * to split current hole
763 temp = tcp_sackhole_insert(tp, sblkp->end,
766 if (SEQ_GT(cur->rxmit, temp->rxmit)) {
767 temp->rxmit = cur->rxmit;
768 tp->sackhint.sack_bytes_rexmit
772 cur->end = sblkp->start;
773 cur->rxmit = SEQ_MIN(cur->rxmit,
775 delivered_data += (sblkp->end - sblkp->start);
779 tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
781 * Testing sblkp->start against cur->start tells us whether
782 * we're done with the sack block or the sack hole.
783 * Accordingly, we advance one or the other.
785 if (SEQ_LEQ(sblkp->start, cur->start))
786 cur = TAILQ_PREV(cur, sackhole_head, scblink);
790 if (!(to->to_flags & TOF_SACK))
792 * If this ACK did not contain any
793 * SACK blocks, any only moved the
794 * left edge right, it is a pure
795 * cumulative ACK. Do not count
796 * DupAck for this. Also required
797 * for RFC6675 rescue retransmission.
800 tp->sackhint.delivered_data = delivered_data;
801 tp->sackhint.sacked_bytes += delivered_data - left_edge_delta;
802 KASSERT((delivered_data >= 0), ("delivered_data < 0"));
803 KASSERT((tp->sackhint.sacked_bytes >= 0), ("sacked_bytes < 0"));
804 return (sack_changed);
808 * Free all SACK holes to clear the scoreboard.
811 tcp_free_sackholes(struct tcpcb *tp)
815 INP_WLOCK_ASSERT(tp->t_inpcb);
816 while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
817 tcp_sackhole_remove(tp, q);
818 tp->sackhint.sack_bytes_rexmit = 0;
820 KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
821 KASSERT(tp->sackhint.nexthole == NULL,
822 ("tp->sackhint.nexthole == NULL"));
826 * Partial ack handling within a sack recovery episode. Keeping this very
827 * simple for now. When a partial ack is received, force snd_cwnd to a value
828 * that will allow the sender to transmit no more than 2 segments. If
829 * necessary, a better scheme can be adopted at a later point, but for now,
830 * the goal is to prevent the sender from bursting a large amount of data in
831 * the midst of sack recovery.
834 tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th)
837 u_int maxseg = tcp_maxseg(tp);
839 INP_WLOCK_ASSERT(tp->t_inpcb);
840 tcp_timer_activate(tp, TT_REXMT, 0);
842 /* Send one or 2 segments based on how much new data was acked. */
843 if ((BYTES_THIS_ACK(tp, th) / maxseg) >= 2)
845 tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
846 (tp->snd_nxt - tp->snd_recover) + num_segs * maxseg);
847 if (tp->snd_cwnd > tp->snd_ssthresh)
848 tp->snd_cwnd = tp->snd_ssthresh;
849 tp->t_flags |= TF_ACKNOW;
851 * RFC6675 rescue retransmission
852 * Add a hole between th_ack (snd_una is not yet set) and snd_max,
853 * if this was a pure cumulative ACK and no data was send beyond
854 * recovery point. Since the data in the socket has not been freed
855 * at this point, we check if the scoreboard is empty, and the ACK
856 * delivered some new data, indicating a full ACK. Also, if the
857 * recovery point is still at snd_max, we are probably application
858 * limited. However, this inference might not always be true. The
859 * rescue retransmission may rarely be slightly premature
860 * compared to RFC6675.
861 * The corresponding ACK+SACK will cause any further outstanding
862 * segments to be retransmitted. This addresses a corner case, when
863 * the trailing packets of a window are lost and no further data
864 * is available for sending.
866 if ((V_tcp_do_rfc6675_pipe) &&
867 SEQ_LT(th->th_ack, tp->snd_recover) &&
868 (tp->snd_recover == tp->snd_max) &&
869 TAILQ_EMPTY(&tp->snd_holes) &&
870 (tp->sackhint.delivered_data > 0)) {
872 * Exclude FIN sequence space in
873 * the hole for the rescue retransmission,
874 * and also don't create a hole, if only
875 * the ACK for a FIN is outstanding.
877 tcp_seq highdata = tp->snd_max;
878 if (tp->t_flags & TF_SENTFIN)
880 if (th->th_ack != highdata) {
881 tp->snd_fack = th->th_ack;
882 (void)tcp_sackhole_insert(tp, SEQ_MAX(th->th_ack,
883 highdata - maxseg), highdata, NULL);
886 (void) tp->t_fb->tfb_tcp_output(tp);
891 * Debug version of tcp_sack_output() that walks the scoreboard. Used for
892 * now to sanity check the hint.
894 static struct sackhole *
895 tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
899 INP_WLOCK_ASSERT(tp->t_inpcb);
900 *sack_bytes_rexmt = 0;
901 TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
902 if (SEQ_LT(p->rxmit, p->end)) {
903 if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
906 *sack_bytes_rexmt += (p->rxmit - p->start);
909 *sack_bytes_rexmt += (p->rxmit - p->start);
916 * Returns the next hole to retransmit and the number of retransmitted bytes
917 * from the scoreboard. We store both the next hole and the number of
918 * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
919 * reception). This avoids scoreboard traversals completely.
921 * The loop here will traverse *at most* one link. Here's the argument. For
922 * the loop to traverse more than 1 link before finding the next hole to
923 * retransmit, we would need to have at least 1 node following the current
924 * hint with (rxmit == end). But, for all holes following the current hint,
925 * (start == rxmit), since we have not yet retransmitted from them.
926 * Therefore, in order to traverse more 1 link in the loop below, we need to
927 * have at least one node following the current hint with (start == rxmit ==
928 * end). But that can't happen, (start == end) means that all the data in
929 * that hole has been sacked, in which case, the hole would have been removed
930 * from the scoreboard.
933 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
935 struct sackhole *hole = NULL;
937 INP_WLOCK_ASSERT(tp->t_inpcb);
938 *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
939 hole = tp->sackhint.nexthole;
940 if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
942 while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
943 if (SEQ_LT(hole->rxmit, hole->end)) {
944 tp->sackhint.nexthole = hole;
953 * After a timeout, the SACK list may be rebuilt. This SACK information
954 * should be used to avoid retransmitting SACKed data. This function
955 * traverses the SACK list to see if snd_nxt should be moved forward.
958 tcp_sack_adjust(struct tcpcb *tp)
960 struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
962 INP_WLOCK_ASSERT(tp->t_inpcb);
964 return; /* No holes */
965 if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack))
966 return; /* We're already beyond any SACKed blocks */
968 * Two cases for which we want to advance snd_nxt:
969 * i) snd_nxt lies between end of one hole and beginning of another
970 * ii) snd_nxt lies between end of last hole and snd_fack
972 while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
973 if (SEQ_LT(tp->snd_nxt, cur->end))
975 if (SEQ_GEQ(tp->snd_nxt, p->start))
978 tp->snd_nxt = p->start;
982 if (SEQ_LT(tp->snd_nxt, cur->end))
984 tp->snd_nxt = tp->snd_fack;