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>
122 #include <netinet/cc/cc.h>
124 #include <netinet/tcp_debug.h>
125 #endif /* TCPDEBUG */
127 #include <machine/in_cksum.h>
129 VNET_DECLARE(struct uma_zone *, sack_hole_zone);
130 #define V_sack_hole_zone VNET(sack_hole_zone)
132 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
135 VNET_DEFINE(int, tcp_do_sack) = 1;
136 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
137 &VNET_NAME(tcp_do_sack), 0,
138 "Enable/Disable TCP SACK support");
140 VNET_DEFINE(int, tcp_do_newsack) = 1;
141 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, revised, CTLFLAG_VNET | CTLFLAG_RW,
142 &VNET_NAME(tcp_do_newsack), 0,
143 "Use revised SACK loss recovery per RFC 6675");
145 VNET_DEFINE(int, tcp_sack_maxholes) = 128;
146 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_VNET | CTLFLAG_RW,
147 &VNET_NAME(tcp_sack_maxholes), 0,
148 "Maximum number of TCP SACK holes allowed per connection");
150 VNET_DEFINE(int, tcp_sack_globalmaxholes) = 65536;
151 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_VNET | CTLFLAG_RW,
152 &VNET_NAME(tcp_sack_globalmaxholes), 0,
153 "Global maximum number of TCP SACK holes");
155 VNET_DEFINE(int, tcp_sack_globalholes) = 0;
156 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_VNET | CTLFLAG_RD,
157 &VNET_NAME(tcp_sack_globalholes), 0,
158 "Global number of TCP SACK holes currently allocated");
161 tcp_dsack_block_exists(struct tcpcb *tp)
163 /* Return true if a DSACK block exists */
164 if (tp->rcv_numsacks == 0)
166 if (SEQ_LEQ(tp->sackblks[0].end, tp->rcv_nxt))
172 * This function will find overlaps with the currently stored sackblocks
173 * and add any overlap as a dsack block upfront
176 tcp_update_dsack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
178 struct sackblk head_blk,mid_blk,saved_blks[MAX_SACK_BLKS];
179 int i, j, n, identical;
182 INP_WLOCK_ASSERT(tp->t_inpcb);
184 KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end"));
186 if (SEQ_LT(rcv_end, tp->rcv_nxt) ||
187 ((rcv_end == tp->rcv_nxt) &&
188 (tp->rcv_numsacks > 0 ) &&
189 (tp->sackblks[0].end == tp->rcv_nxt))) {
190 saved_blks[0].start = rcv_start;
191 saved_blks[0].end = rcv_end;
193 saved_blks[0].start = saved_blks[0].end = 0;
196 head_blk.start = head_blk.end = 0;
197 mid_blk.start = rcv_start;
198 mid_blk.end = rcv_end;
201 for (i = 0; i < tp->rcv_numsacks; i++) {
202 start = tp->sackblks[i].start;
203 end = tp->sackblks[i].end;
204 if (SEQ_LT(rcv_end, start)) {
205 /* pkt left to sack blk */
208 if (SEQ_GT(rcv_start, end)) {
209 /* pkt right to sack blk */
212 if (SEQ_GT(tp->rcv_nxt, end)) {
213 if ((SEQ_MAX(rcv_start, start) != SEQ_MIN(rcv_end, end)) &&
214 (SEQ_GT(head_blk.start, SEQ_MAX(rcv_start, start)) ||
215 (head_blk.start == head_blk.end))) {
216 head_blk.start = SEQ_MAX(rcv_start, start);
217 head_blk.end = SEQ_MIN(rcv_end, end);
221 if (((head_blk.start == head_blk.end) ||
222 SEQ_LT(start, head_blk.start)) &&
223 (SEQ_GT(end, rcv_start) &&
224 SEQ_LEQ(start, rcv_end))) {
225 head_blk.start = start;
228 mid_blk.start = SEQ_MIN(mid_blk.start, start);
229 mid_blk.end = SEQ_MAX(mid_blk.end, end);
230 if ((mid_blk.start == start) &&
231 (mid_blk.end == end))
234 if (SEQ_LT(head_blk.start, head_blk.end)) {
235 /* store overlapping range */
236 saved_blks[0].start = SEQ_MAX(rcv_start, head_blk.start);
237 saved_blks[0].end = SEQ_MIN(rcv_end, head_blk.end);
241 * Second, if not ACKed, store the SACK block that
242 * overlaps with the DSACK block unless it is identical
244 if ((SEQ_LT(tp->rcv_nxt, mid_blk.end) &&
245 !((mid_blk.start == saved_blks[0].start) &&
246 (mid_blk.end == saved_blks[0].end))) ||
248 saved_blks[n].start = mid_blk.start;
249 saved_blks[n++].end = mid_blk.end;
251 for (j = 0; (j < tp->rcv_numsacks) && (n < MAX_SACK_BLKS); j++) {
252 if (((SEQ_LT(tp->sackblks[j].end, mid_blk.start) ||
253 SEQ_GT(tp->sackblks[j].start, mid_blk.end)) &&
254 (SEQ_GT(tp->sackblks[j].start, tp->rcv_nxt))))
255 saved_blks[n++] = tp->sackblks[j];
258 for (i = 0; i < n; i++) {
259 /* we can end up with a stale initial entry */
260 if (SEQ_LT(saved_blks[i].start, saved_blks[i].end)) {
261 tp->sackblks[j++] = saved_blks[i];
264 tp->rcv_numsacks = j;
268 * This function is called upon receipt of new valid data (while not in
269 * header prediction mode), and it updates the ordered list of sacks.
272 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
275 * First reported block MUST be the most recent one. Subsequent
276 * blocks SHOULD be in the order in which they arrived at the
277 * receiver. These two conditions make the implementation fully
278 * compliant with RFC 2018.
280 struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
281 int num_head, num_saved, i;
283 INP_WLOCK_ASSERT(tp->t_inpcb);
285 /* Check arguments. */
286 KASSERT(SEQ_LEQ(rcv_start, rcv_end), ("rcv_start <= rcv_end"));
288 if ((rcv_start == rcv_end) &&
289 (tp->rcv_numsacks >= 1) &&
290 (rcv_end == tp->sackblks[0].end)) {
291 /* retaining DSACK block below rcv_nxt (todrop) */
292 head_blk = tp->sackblks[0];
294 /* SACK block for the received segment. */
295 head_blk.start = rcv_start;
296 head_blk.end = rcv_end;
300 * Merge updated SACK blocks into head_blk, and save unchanged SACK
301 * blocks into saved_blks[]. num_saved will have the number of the
305 for (i = 0; i < tp->rcv_numsacks; i++) {
306 tcp_seq start = tp->sackblks[i].start;
307 tcp_seq end = tp->sackblks[i].end;
308 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
310 * Discard this SACK block.
312 } else if (SEQ_LEQ(head_blk.start, end) &&
313 SEQ_GEQ(head_blk.end, start)) {
315 * Merge this SACK block into head_blk. This SACK
316 * block itself will be discarded.
329 * |-----| DSACK smaller
331 if (head_blk.start == end)
332 head_blk.start = start;
333 else if (head_blk.end == start)
336 if (SEQ_LT(head_blk.start, start)) {
337 tcp_seq temp = start;
338 start = head_blk.start;
339 head_blk.start = temp;
341 if (SEQ_GT(head_blk.end, end)) {
346 if ((head_blk.start != start) ||
347 (head_blk.end != end)) {
348 if ((num_saved >= 1) &&
349 SEQ_GEQ(saved_blks[num_saved-1].start, start) &&
350 SEQ_LEQ(saved_blks[num_saved-1].end, end))
352 saved_blks[num_saved].start = start;
353 saved_blks[num_saved].end = end;
359 * This block supercedes the prior block
361 if ((num_saved >= 1) &&
362 SEQ_GEQ(saved_blks[num_saved-1].start, start) &&
363 SEQ_LEQ(saved_blks[num_saved-1].end, end))
366 * Save this SACK block.
368 saved_blks[num_saved].start = start;
369 saved_blks[num_saved].end = end;
375 * Update SACK list in tp->sackblks[].
378 if (SEQ_LT(rcv_start, rcv_end)) {
380 * The received data segment is an out-of-order segment. Put
381 * head_blk at the top of SACK list.
383 tp->sackblks[0] = head_blk;
386 * If the number of saved SACK blocks exceeds its limit,
387 * discard the last SACK block.
389 if (num_saved >= MAX_SACK_BLKS)
392 if ((rcv_start == rcv_end) &&
393 (rcv_start == tp->sackblks[0].end)) {
398 * Copy the saved SACK blocks back.
400 bcopy(saved_blks, &tp->sackblks[num_head],
401 sizeof(struct sackblk) * num_saved);
404 /* Save the number of SACK blocks. */
405 tp->rcv_numsacks = num_head + num_saved;
409 tcp_clean_dsack_blocks(struct tcpcb *tp)
411 struct sackblk saved_blks[MAX_SACK_BLKS];
414 INP_WLOCK_ASSERT(tp->t_inpcb);
416 * Clean up any DSACK blocks that
417 * are in our queue of sack blocks.
421 for (i = 0; i < tp->rcv_numsacks; i++) {
422 tcp_seq start = tp->sackblks[i].start;
423 tcp_seq end = tp->sackblks[i].end;
424 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
426 * Discard this D-SACK block.
431 * Save this SACK block.
433 saved_blks[num_saved].start = start;
434 saved_blks[num_saved].end = end;
439 * Copy the saved SACK blocks back.
441 bcopy(saved_blks, &tp->sackblks[0],
442 sizeof(struct sackblk) * num_saved);
444 tp->rcv_numsacks = num_saved;
448 * Delete all receiver-side SACK information.
451 tcp_clean_sackreport(struct tcpcb *tp)
455 INP_WLOCK_ASSERT(tp->t_inpcb);
456 tp->rcv_numsacks = 0;
457 for (i = 0; i < MAX_SACK_BLKS; i++)
458 tp->sackblks[i].start = tp->sackblks[i].end=0;
462 * Allocate struct sackhole.
464 static struct sackhole *
465 tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
467 struct sackhole *hole;
469 if (tp->snd_numholes >= V_tcp_sack_maxholes ||
470 V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) {
471 TCPSTAT_INC(tcps_sack_sboverflow);
475 hole = (struct sackhole *)uma_zalloc(V_sack_hole_zone, M_NOWAIT);
484 atomic_add_int(&V_tcp_sack_globalholes, 1);
490 * Free struct sackhole.
493 tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
496 uma_zfree(V_sack_hole_zone, hole);
499 atomic_subtract_int(&V_tcp_sack_globalholes, 1);
501 KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
502 KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
506 * Insert new SACK hole into scoreboard.
508 static struct sackhole *
509 tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
510 struct sackhole *after)
512 struct sackhole *hole;
514 /* Allocate a new SACK hole. */
515 hole = tcp_sackhole_alloc(tp, start, end);
519 /* Insert the new SACK hole into scoreboard. */
521 TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
523 TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
525 /* Update SACK hint. */
526 if (tp->sackhint.nexthole == NULL)
527 tp->sackhint.nexthole = hole;
533 * Remove SACK hole from scoreboard.
536 tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
539 /* Update SACK hint. */
540 if (tp->sackhint.nexthole == hole)
541 tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
543 /* Remove this SACK hole. */
544 TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
546 /* Free this SACK hole. */
547 tcp_sackhole_free(tp, hole);
551 * Process cumulative ACK and the TCP SACK option to update the scoreboard.
552 * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
553 * the sequence space).
554 * Returns 1 if incoming ACK has previously unknown SACK information,
558 tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack)
560 struct sackhole *cur, *temp;
561 struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp;
562 int i, j, num_sack_blks, sack_changed;
563 int delivered_data, left_edge_delta;
565 INP_WLOCK_ASSERT(tp->t_inpcb);
572 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
573 * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
574 * Account changes to SND.UNA always in delivered data.
576 if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
577 left_edge_delta = th_ack - tp->snd_una;
578 sack_blocks[num_sack_blks].start = tp->snd_una;
579 sack_blocks[num_sack_blks++].end = th_ack;
581 * Pulling snd_fack forward if we got here
582 * due to DSACK blocks
584 if (SEQ_LT(tp->snd_fack, th_ack)) {
585 delivered_data += th_ack - tp->snd_una;
586 tp->snd_fack = th_ack;
591 * Append received valid SACK blocks to sack_blocks[], but only if we
592 * received new blocks from the other side.
594 if (to->to_flags & TOF_SACK) {
595 for (i = 0; i < to->to_nsacks; i++) {
596 bcopy((to->to_sacks + i * TCPOLEN_SACK),
597 &sack, sizeof(sack));
598 sack.start = ntohl(sack.start);
599 sack.end = ntohl(sack.end);
600 if (SEQ_GT(sack.end, sack.start) &&
601 SEQ_GT(sack.start, tp->snd_una) &&
602 SEQ_GT(sack.start, th_ack) &&
603 SEQ_LT(sack.start, tp->snd_max) &&
604 SEQ_GT(sack.end, tp->snd_una) &&
605 SEQ_LEQ(sack.end, tp->snd_max)) {
606 sack_blocks[num_sack_blks++] = sack;
611 * Return if SND.UNA is not advanced and no valid SACK block is
614 if (num_sack_blks == 0)
615 return (sack_changed);
618 * Sort the SACK blocks so we can update the scoreboard with just one
619 * pass. The overhead of sorting up to 4+1 elements is less than
620 * making up to 4+1 passes over the scoreboard.
622 for (i = 0; i < num_sack_blks; i++) {
623 for (j = i + 1; j < num_sack_blks; j++) {
624 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
625 sack = sack_blocks[i];
626 sack_blocks[i] = sack_blocks[j];
627 sack_blocks[j] = sack;
631 if (TAILQ_EMPTY(&tp->snd_holes)) {
633 * Empty scoreboard. Need to initialize snd_fack (it may be
634 * uninitialized or have a bogus value). Scoreboard holes
635 * (from the sack blocks received) are created later below
636 * (in the logic that adds holes to the tail of the
639 tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
640 tp->sackhint.sacked_bytes = 0; /* reset */
643 * In the while-loop below, incoming SACK blocks (sack_blocks[]) and
644 * SACK holes (snd_holes) are traversed from their tails with just
645 * one pass in order to reduce the number of compares especially when
646 * the bandwidth-delay product is large.
648 * Note: Typically, in the first RTT of SACK recovery, the highest
649 * three or four SACK blocks with the same ack number are received.
650 * In the second RTT, if retransmitted data segments are not lost,
651 * the highest three or four SACK blocks with ack number advancing
654 sblkp = &sack_blocks[num_sack_blks - 1]; /* Last SACK block */
655 tp->sackhint.last_sack_ack = sblkp->end;
656 if (SEQ_LT(tp->snd_fack, sblkp->start)) {
658 * The highest SACK block is beyond fack. First,
659 * check if there was a successful Rescue Retransmission,
660 * and move this hole left. With normal holes, snd_fack
661 * is always to the right of the end.
663 if (((temp = TAILQ_LAST(&tp->snd_holes, sackhole_head)) != NULL) &&
664 SEQ_LEQ(tp->snd_fack,temp->end)) {
665 temp->start = SEQ_MAX(tp->snd_fack, SEQ_MAX(tp->snd_una, th_ack));
666 temp->end = sblkp->start;
667 temp->rxmit = temp->start;
668 delivered_data += sblkp->end - sblkp->start;
669 tp->snd_fack = sblkp->end;
674 * Append a new SACK hole at the tail. If the
675 * second or later highest SACK blocks are also
676 * beyond the current fack, they will be inserted
677 * by way of hole splitting in the while-loop below.
679 temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL);
681 delivered_data += sblkp->end - sblkp->start;
682 tp->snd_fack = sblkp->end;
683 /* Go to the previous sack block. */
688 * We failed to add a new hole based on the current
689 * sack block. Skip over all the sack blocks that
690 * fall completely to the right of snd_fack and
691 * proceed to trim the scoreboard based on the
692 * remaining sack blocks. This also trims the
693 * scoreboard for th_ack (which is sack_blocks[0]).
695 while (sblkp >= sack_blocks &&
696 SEQ_LT(tp->snd_fack, sblkp->start))
698 if (sblkp >= sack_blocks &&
699 SEQ_LT(tp->snd_fack, sblkp->end)) {
700 delivered_data += sblkp->end - tp->snd_fack;
701 tp->snd_fack = sblkp->end;
706 } else if (SEQ_LT(tp->snd_fack, sblkp->end)) {
707 /* fack is advanced. */
708 delivered_data += sblkp->end - tp->snd_fack;
709 tp->snd_fack = sblkp->end;
712 cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */
714 * Since the incoming sack blocks are sorted, we can process them
715 * making one sweep of the scoreboard.
717 while (sblkp >= sack_blocks && cur != NULL) {
718 if (SEQ_GEQ(sblkp->start, cur->end)) {
720 * SACKs data beyond the current hole. Go to the
721 * previous sack block.
726 if (SEQ_LEQ(sblkp->end, cur->start)) {
728 * SACKs data before the current hole. Go to the
731 cur = TAILQ_PREV(cur, sackhole_head, scblink);
734 tp->sackhint.sack_bytes_rexmit -=
735 (SEQ_MIN(cur->rxmit, cur->end) - cur->start);
736 KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
737 ("sackhint bytes rtx >= 0"));
739 if (SEQ_LEQ(sblkp->start, cur->start)) {
740 /* Data acks at least the beginning of hole. */
741 if (SEQ_GEQ(sblkp->end, cur->end)) {
742 /* Acks entire hole, so delete hole. */
743 delivered_data += (cur->end - cur->start);
745 cur = TAILQ_PREV(cur, sackhole_head, scblink);
746 tcp_sackhole_remove(tp, temp);
748 * The sack block may ack all or part of the
749 * next hole too, so continue onto the next
754 /* Move start of hole forward. */
755 delivered_data += (sblkp->end - cur->start);
756 cur->start = sblkp->end;
757 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
760 /* Data acks at least the end of hole. */
761 if (SEQ_GEQ(sblkp->end, cur->end)) {
762 /* Move end of hole backward. */
763 delivered_data += (cur->end - sblkp->start);
764 cur->end = sblkp->start;
765 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
766 if ((tp->t_flags & TF_LRD) && SEQ_GEQ(cur->rxmit, cur->end))
767 cur->rxmit = tp->snd_recover;
770 * ACKs some data in middle of a hole; need
771 * to split current hole
773 temp = tcp_sackhole_insert(tp, sblkp->end,
776 if (SEQ_GT(cur->rxmit, temp->rxmit)) {
777 temp->rxmit = cur->rxmit;
778 tp->sackhint.sack_bytes_rexmit +=
779 (SEQ_MIN(temp->rxmit,
780 temp->end) - temp->start);
782 cur->end = sblkp->start;
783 cur->rxmit = SEQ_MIN(cur->rxmit,
785 if ((tp->t_flags & TF_LRD) && SEQ_GEQ(cur->rxmit, cur->end))
786 cur->rxmit = tp->snd_recover;
787 delivered_data += (sblkp->end - sblkp->start);
791 tp->sackhint.sack_bytes_rexmit +=
792 (SEQ_MIN(cur->rxmit, cur->end) - cur->start);
794 * Testing sblkp->start against cur->start tells us whether
795 * we're done with the sack block or the sack hole.
796 * Accordingly, we advance one or the other.
798 if (SEQ_LEQ(sblkp->start, cur->start))
799 cur = TAILQ_PREV(cur, sackhole_head, scblink);
803 if (!(to->to_flags & TOF_SACK))
805 * If this ACK did not contain any
806 * SACK blocks, any only moved the
807 * left edge right, it is a pure
808 * cumulative ACK. Do not count
809 * DupAck for this. Also required
810 * for RFC6675 rescue retransmission.
813 tp->sackhint.delivered_data = delivered_data;
814 tp->sackhint.sacked_bytes += delivered_data - left_edge_delta;
815 KASSERT((delivered_data >= 0), ("delivered_data < 0"));
816 KASSERT((tp->sackhint.sacked_bytes >= 0), ("sacked_bytes < 0"));
817 return (sack_changed);
821 * Free all SACK holes to clear the scoreboard.
824 tcp_free_sackholes(struct tcpcb *tp)
828 INP_WLOCK_ASSERT(tp->t_inpcb);
829 while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
830 tcp_sackhole_remove(tp, q);
831 tp->sackhint.sack_bytes_rexmit = 0;
833 KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
834 KASSERT(tp->sackhint.nexthole == NULL,
835 ("tp->sackhint.nexthole == NULL"));
839 * Partial ack handling within a sack recovery episode. Keeping this very
840 * simple for now. When a partial ack is received, force snd_cwnd to a value
841 * that will allow the sender to transmit no more than 2 segments. If
842 * necessary, a better scheme can be adopted at a later point, but for now,
843 * the goal is to prevent the sender from bursting a large amount of data in
844 * the midst of sack recovery.
847 tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th)
850 u_int maxseg = tcp_maxseg(tp);
852 INP_WLOCK_ASSERT(tp->t_inpcb);
853 tcp_timer_activate(tp, TT_REXMT, 0);
855 /* Send one or 2 segments based on how much new data was acked. */
856 if ((BYTES_THIS_ACK(tp, th) / maxseg) >= 2)
858 tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
859 (tp->snd_nxt - tp->snd_recover) + num_segs * maxseg);
860 if (tp->snd_cwnd > tp->snd_ssthresh)
861 tp->snd_cwnd = tp->snd_ssthresh;
862 tp->t_flags |= TF_ACKNOW;
864 * RFC6675 rescue retransmission
865 * Add a hole between th_ack (snd_una is not yet set) and snd_max,
866 * if this was a pure cumulative ACK and no data was send beyond
867 * recovery point. Since the data in the socket has not been freed
868 * at this point, we check if the scoreboard is empty, and the ACK
869 * delivered some new data, indicating a full ACK. Also, if the
870 * recovery point is still at snd_max, we are probably application
871 * limited. However, this inference might not always be true. The
872 * rescue retransmission may rarely be slightly premature
873 * compared to RFC6675.
874 * The corresponding ACK+SACK will cause any further outstanding
875 * segments to be retransmitted. This addresses a corner case, when
876 * the trailing packets of a window are lost and no further data
877 * is available for sending.
879 if ((V_tcp_do_newsack) &&
880 SEQ_LT(th->th_ack, tp->snd_recover) &&
881 (tp->snd_recover == tp->snd_max) &&
882 TAILQ_EMPTY(&tp->snd_holes) &&
883 (tp->sackhint.delivered_data > 0)) {
885 * Exclude FIN sequence space in
886 * the hole for the rescue retransmission,
887 * and also don't create a hole, if only
888 * the ACK for a FIN is outstanding.
890 tcp_seq highdata = tp->snd_max;
891 if (tp->t_flags & TF_SENTFIN)
893 if (th->th_ack != highdata) {
894 tp->snd_fack = th->th_ack;
895 (void)tcp_sackhole_insert(tp, SEQ_MAX(th->th_ack,
896 highdata - maxseg), highdata, NULL);
899 (void) tp->t_fb->tfb_tcp_output(tp);
904 * Debug version of tcp_sack_output() that walks the scoreboard. Used for
905 * now to sanity check the hint.
907 static struct sackhole *
908 tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
912 INP_WLOCK_ASSERT(tp->t_inpcb);
913 *sack_bytes_rexmt = 0;
914 TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
915 if (SEQ_LT(p->rxmit, p->end)) {
916 if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
919 *sack_bytes_rexmt += (p->rxmit - p->start);
922 *sack_bytes_rexmt += (SEQ_MIN(p->rxmit, p->end) - p->start);
929 * Returns the next hole to retransmit and the number of retransmitted bytes
930 * from the scoreboard. We store both the next hole and the number of
931 * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
932 * reception). This avoids scoreboard traversals completely.
934 * The loop here will traverse *at most* one link. Here's the argument. For
935 * the loop to traverse more than 1 link before finding the next hole to
936 * retransmit, we would need to have at least 1 node following the current
937 * hint with (rxmit == end). But, for all holes following the current hint,
938 * (start == rxmit), since we have not yet retransmitted from them.
939 * Therefore, in order to traverse more 1 link in the loop below, we need to
940 * have at least one node following the current hint with (start == rxmit ==
941 * end). But that can't happen, (start == end) means that all the data in
942 * that hole has been sacked, in which case, the hole would have been removed
943 * from the scoreboard.
946 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
948 struct sackhole *hole = NULL;
950 INP_WLOCK_ASSERT(tp->t_inpcb);
951 *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
952 hole = tp->sackhint.nexthole;
953 if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
955 while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
956 if (SEQ_LT(hole->rxmit, hole->end)) {
957 tp->sackhint.nexthole = hole;
966 * After a timeout, the SACK list may be rebuilt. This SACK information
967 * should be used to avoid retransmitting SACKed data. This function
968 * traverses the SACK list to see if snd_nxt should be moved forward.
971 tcp_sack_adjust(struct tcpcb *tp)
973 struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
975 INP_WLOCK_ASSERT(tp->t_inpcb);
977 return; /* No holes */
978 if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack))
979 return; /* We're already beyond any SACKed blocks */
981 * Two cases for which we want to advance snd_nxt:
982 * i) snd_nxt lies between end of one hole and beginning of another
983 * ii) snd_nxt lies between end of last hole and snd_fack
985 while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
986 if (SEQ_LT(tp->snd_nxt, cur->end))
988 if (SEQ_GEQ(tp->snd_nxt, p->start))
991 tp->snd_nxt = p->start;
995 if (SEQ_LT(tp->snd_nxt, cur->end))
997 tp->snd_nxt = tp->snd_fack;
1001 * Lost Retransmission Detection
1002 * Check is FACK is beyond the rexmit of the leftmost hole.
1003 * If yes, we restart sending from still existing holes,
1004 * and adjust cwnd via the congestion control module.
1007 tcp_sack_lost_retransmission(struct tcpcb *tp, struct tcphdr *th)
1009 struct sackhole *temp;
1011 if (IN_RECOVERY(tp->t_flags) &&
1012 SEQ_GT(tp->snd_fack, tp->snd_recover) &&
1013 ((temp = TAILQ_FIRST(&tp->snd_holes)) != NULL) &&
1014 SEQ_GEQ(temp->rxmit, temp->end) &&
1015 SEQ_GEQ(tp->snd_fack, temp->rxmit)) {
1016 TCPSTAT_INC(tcps_sack_lostrexmt);
1018 * Start retransmissions from the first hole, and
1019 * subsequently all other remaining holes, including
1020 * those, which had been sent completely before.
1022 tp->sackhint.nexthole = temp;
1023 TAILQ_FOREACH(temp, &tp->snd_holes, scblink) {
1024 if (SEQ_GEQ(tp->snd_fack, temp->rxmit) &&
1025 SEQ_GEQ(temp->rxmit, temp->end))
1026 temp->rxmit = temp->start;
1029 * Remember the old ssthresh, to deduct the beta factor used
1030 * by the CC module. Finally, set cwnd to ssthresh just
1031 * prior to invoking another cwnd reduction by the CC
1032 * module, to not shrink it excessively.
1034 prev_cwnd = tp->snd_cwnd;
1035 tp->snd_cwnd = tp->snd_ssthresh;
1037 * Formally exit recovery, and let the CC module adjust
1038 * ssthresh as intended.
1040 EXIT_RECOVERY(tp->t_flags);
1041 cc_cong_signal(tp, th, CC_NDUPACK);
1043 * For PRR, adjust recover_fs as if this new reduction
1044 * initialized this variable.
1045 * cwnd will be adjusted by SACK or PRR processing
1046 * subsequently, only set it to a safe value here.
1048 tp->snd_cwnd = tcp_maxseg(tp);
1049 tp->sackhint.recover_fs = (tp->snd_max - tp->snd_una) -
1050 tp->sackhint.recover_fs;