xref: /freebsd/sys/netinet/tcp_sack.c (revision ad3f9ab320ed8ae3b99e300f7daa1469cb66139d)
1c398230bSWarner Losh /*-
26d90faf3SPaul Saab  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
36d90faf3SPaul Saab  *	The Regents of the University of California.  All rights reserved.
46d90faf3SPaul Saab  *
56d90faf3SPaul Saab  * Redistribution and use in source and binary forms, with or without
66d90faf3SPaul Saab  * modification, are permitted provided that the following conditions
76d90faf3SPaul Saab  * are met:
86d90faf3SPaul Saab  * 1. Redistributions of source code must retain the above copyright
96d90faf3SPaul Saab  *    notice, this list of conditions and the following disclaimer.
106d90faf3SPaul Saab  * 2. Redistributions in binary form must reproduce the above copyright
116d90faf3SPaul Saab  *    notice, this list of conditions and the following disclaimer in the
126d90faf3SPaul Saab  *    documentation and/or other materials provided with the distribution.
136d90faf3SPaul Saab  * 4. Neither the name of the University nor the names of its contributors
146d90faf3SPaul Saab  *    may be used to endorse or promote products derived from this software
156d90faf3SPaul Saab  *    without specific prior written permission.
166d90faf3SPaul Saab  *
176d90faf3SPaul Saab  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
186d90faf3SPaul Saab  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196d90faf3SPaul Saab  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206d90faf3SPaul Saab  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
216d90faf3SPaul Saab  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
226d90faf3SPaul Saab  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
236d90faf3SPaul Saab  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
246d90faf3SPaul Saab  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256d90faf3SPaul Saab  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266d90faf3SPaul Saab  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
276d90faf3SPaul Saab  * SUCH DAMAGE.
286d90faf3SPaul Saab  *
296d90faf3SPaul Saab  *	@(#)tcp_sack.c	8.12 (Berkeley) 5/24/95
306d90faf3SPaul Saab  * $FreeBSD$
316d90faf3SPaul Saab  */
326d90faf3SPaul Saab 
33c398230bSWarner Losh /*-
346d90faf3SPaul Saab  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
356d90faf3SPaul Saab  *	The Regents of the University of California.  All rights reserved.
366d90faf3SPaul Saab  *
376d90faf3SPaul Saab  * Redistribution and use in source and binary forms, with or without
386d90faf3SPaul Saab  * modification, are permitted provided that the following conditions
396d90faf3SPaul Saab  * are met:
406d90faf3SPaul Saab  * 1. Redistributions of source code must retain the above copyright
416d90faf3SPaul Saab  *    notice, this list of conditions and the following disclaimer.
426d90faf3SPaul Saab  * 2. Redistributions in binary form must reproduce the above copyright
436d90faf3SPaul Saab  *    notice, this list of conditions and the following disclaimer in the
446d90faf3SPaul Saab  *    documentation and/or other materials provided with the distribution.
456d90faf3SPaul Saab  * 3. Neither the name of the University nor the names of its contributors
466d90faf3SPaul Saab  *    may be used to endorse or promote products derived from this software
476d90faf3SPaul Saab  *    without specific prior written permission.
486d90faf3SPaul Saab  *
496d90faf3SPaul Saab  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
506d90faf3SPaul Saab  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
516d90faf3SPaul Saab  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
526d90faf3SPaul Saab  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
536d90faf3SPaul Saab  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
546d90faf3SPaul Saab  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
556d90faf3SPaul Saab  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
566d90faf3SPaul Saab  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
576d90faf3SPaul Saab  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
586d90faf3SPaul Saab  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
596d90faf3SPaul Saab  * SUCH DAMAGE.
606d90faf3SPaul Saab  *
616d90faf3SPaul Saab  *	@@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
626d90faf3SPaul Saab  *
636d90faf3SPaul Saab  * NRL grants permission for redistribution and use in source and binary
646d90faf3SPaul Saab  * forms, with or without modification, of the software and documentation
656d90faf3SPaul Saab  * created at NRL provided that the following conditions are met:
666d90faf3SPaul Saab  *
676d90faf3SPaul Saab  * 1. Redistributions of source code must retain the above copyright
686d90faf3SPaul Saab  *    notice, this list of conditions and the following disclaimer.
696d90faf3SPaul Saab  * 2. Redistributions in binary form must reproduce the above copyright
706d90faf3SPaul Saab  *    notice, this list of conditions and the following disclaimer in the
716d90faf3SPaul Saab  *    documentation and/or other materials provided with the distribution.
726d90faf3SPaul Saab  * 3. All advertising materials mentioning features or use of this software
736d90faf3SPaul Saab  *    must display the following acknowledgements:
746d90faf3SPaul Saab  *	This product includes software developed by the University of
756d90faf3SPaul Saab  *	California, Berkeley and its contributors.
766d90faf3SPaul Saab  *	This product includes software developed at the Information
776d90faf3SPaul Saab  *	Technology Division, US Naval Research Laboratory.
786d90faf3SPaul Saab  * 4. Neither the name of the NRL nor the names of its contributors
796d90faf3SPaul Saab  *    may be used to endorse or promote products derived from this software
806d90faf3SPaul Saab  *    without specific prior written permission.
816d90faf3SPaul Saab  *
826d90faf3SPaul Saab  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
836d90faf3SPaul Saab  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
846d90faf3SPaul Saab  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
856d90faf3SPaul Saab  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
866d90faf3SPaul Saab  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
876d90faf3SPaul Saab  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
886d90faf3SPaul Saab  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
896d90faf3SPaul Saab  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
906d90faf3SPaul Saab  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
916d90faf3SPaul Saab  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
926d90faf3SPaul Saab  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
936d90faf3SPaul Saab  *
946d90faf3SPaul Saab  * The views and conclusions contained in the software and documentation
956d90faf3SPaul Saab  * are those of the authors and should not be interpreted as representing
966d90faf3SPaul Saab  * official policies, either expressed or implied, of the US Naval
976d90faf3SPaul Saab  * Research Laboratory (NRL).
986d90faf3SPaul Saab  */
996d90faf3SPaul Saab #include "opt_inet.h"
1006d90faf3SPaul Saab #include "opt_inet6.h"
1016d90faf3SPaul Saab #include "opt_tcpdebug.h"
1026d90faf3SPaul Saab #include "opt_tcp_sack.h"
1036d90faf3SPaul Saab 
1046d90faf3SPaul Saab #include <sys/param.h>
1056d90faf3SPaul Saab #include <sys/systm.h>
1066d90faf3SPaul Saab #include <sys/kernel.h>
1076d90faf3SPaul Saab #include <sys/sysctl.h>
1086d90faf3SPaul Saab #include <sys/malloc.h>
1096d90faf3SPaul Saab #include <sys/mbuf.h>
1106d90faf3SPaul Saab #include <sys/proc.h>		/* for proc0 declaration */
1116d90faf3SPaul Saab #include <sys/protosw.h>
1126d90faf3SPaul Saab #include <sys/socket.h>
1136d90faf3SPaul Saab #include <sys/socketvar.h>
1146d90faf3SPaul Saab #include <sys/syslog.h>
1156d90faf3SPaul Saab #include <sys/systm.h>
1166d90faf3SPaul Saab 
1176d90faf3SPaul Saab #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
1186d90faf3SPaul Saab 
1196d90faf3SPaul Saab #include <vm/uma.h>
1206d90faf3SPaul Saab 
1216d90faf3SPaul Saab #include <net/if.h>
1226d90faf3SPaul Saab #include <net/route.h>
1236d90faf3SPaul Saab 
1246d90faf3SPaul Saab #include <netinet/in.h>
1256d90faf3SPaul Saab #include <netinet/in_systm.h>
1266d90faf3SPaul Saab #include <netinet/ip.h>
1276d90faf3SPaul Saab #include <netinet/in_var.h>
1286d90faf3SPaul Saab #include <netinet/in_pcb.h>
1296d90faf3SPaul Saab #include <netinet/ip_var.h>
1306d90faf3SPaul Saab #include <netinet/ip6.h>
1316d90faf3SPaul Saab #include <netinet/icmp6.h>
1326d90faf3SPaul Saab #include <netinet6/nd6.h>
1336d90faf3SPaul Saab #include <netinet6/ip6_var.h>
1346d90faf3SPaul Saab #include <netinet6/in6_pcb.h>
1356d90faf3SPaul Saab #include <netinet/tcp.h>
1366d90faf3SPaul Saab #include <netinet/tcp_fsm.h>
1376d90faf3SPaul Saab #include <netinet/tcp_seq.h>
1386d90faf3SPaul Saab #include <netinet/tcp_timer.h>
1396d90faf3SPaul Saab #include <netinet/tcp_var.h>
1406d90faf3SPaul Saab #include <netinet6/tcp6_var.h>
1416d90faf3SPaul Saab #include <netinet/tcpip.h>
1426d90faf3SPaul Saab #ifdef TCPDEBUG
1436d90faf3SPaul Saab #include <netinet/tcp_debug.h>
1446d90faf3SPaul Saab #endif /* TCPDEBUG */
1456d90faf3SPaul Saab 
1466d90faf3SPaul Saab #include <machine/in_cksum.h>
1476d90faf3SPaul Saab 
1486d90faf3SPaul Saab extern struct uma_zone *sack_hole_zone;
1496d90faf3SPaul Saab 
150a55db2b6SPaul Saab SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, 0, "TCP SACK");
151a55db2b6SPaul Saab int tcp_do_sack = 1;
152a55db2b6SPaul Saab SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_RW,
153a55db2b6SPaul Saab     &tcp_do_sack, 0, "Enable/Disable TCP SACK support");
154c4274833SRobert Watson TUNABLE_INT("net.inet.tcp.sack.enable", &tcp_do_sack);
155a55db2b6SPaul Saab 
156e891d82bSPaul Saab static int tcp_sack_maxholes = 128;
157e891d82bSPaul Saab SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_RW,
158e891d82bSPaul Saab     &tcp_sack_maxholes, 0,
159e891d82bSPaul Saab     "Maximum number of TCP SACK holes allowed per connection");
160e891d82bSPaul Saab 
161e891d82bSPaul Saab static int tcp_sack_globalmaxholes = 65536;
162e891d82bSPaul Saab SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_RW,
163e891d82bSPaul Saab     &tcp_sack_globalmaxholes, 0,
164e891d82bSPaul Saab     "Global maximum number of TCP SACK holes");
165e891d82bSPaul Saab 
166e891d82bSPaul Saab static int tcp_sack_globalholes = 0;
167e891d82bSPaul Saab SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_RD,
168e891d82bSPaul Saab     &tcp_sack_globalholes, 0,
169e891d82bSPaul Saab     "Global number of TCP SACK holes currently allocated");
170b7c75571SPaul Saab 
1716d90faf3SPaul Saab /*
1726d90faf3SPaul Saab  * This function is called upon receipt of new valid data (while not in header
1736d90faf3SPaul Saab  * prediction mode), and it updates the ordered list of sacks.
1746d90faf3SPaul Saab  */
1756d90faf3SPaul Saab void
176b7c75571SPaul Saab tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
1776d90faf3SPaul Saab {
1786d90faf3SPaul Saab 	/*
1796d90faf3SPaul Saab 	 * First reported block MUST be the most recent one.  Subsequent
1806d90faf3SPaul Saab 	 * blocks SHOULD be in the order in which they arrived at the
1816d90faf3SPaul Saab 	 * receiver.  These two conditions make the implementation fully
1826d90faf3SPaul Saab 	 * compliant with RFC 2018.
1836d90faf3SPaul Saab 	 */
184b7c75571SPaul Saab 	struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
185b7c75571SPaul Saab 	int num_head, num_saved, i;
1866d90faf3SPaul Saab 
1876d90faf3SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
188b7c75571SPaul Saab 
189b7c75571SPaul Saab 	/* Check arguments */
190b7c75571SPaul Saab 	KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end"));
191b7c75571SPaul Saab 
192b7c75571SPaul Saab 	/* SACK block for the received segment. */
193b7c75571SPaul Saab 	head_blk.start = rcv_start;
194b7c75571SPaul Saab 	head_blk.end = rcv_end;
195b7c75571SPaul Saab 
196b7c75571SPaul Saab 	/*
197b7c75571SPaul Saab 	 * Merge updated SACK blocks into head_blk, and
198b7c75571SPaul Saab 	 * save unchanged SACK blocks into saved_blks[].
199b7c75571SPaul Saab 	 * num_saved will have the number of the saved SACK blocks.
200b7c75571SPaul Saab 	 */
201b7c75571SPaul Saab 	num_saved = 0;
2026d90faf3SPaul Saab 	for (i = 0; i < tp->rcv_numsacks; i++) {
203b7c75571SPaul Saab 		tcp_seq start = tp->sackblks[i].start;
204b7c75571SPaul Saab 		tcp_seq end = tp->sackblks[i].end;
205b7c75571SPaul Saab 		if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
206b7c75571SPaul Saab 			/*
207b7c75571SPaul Saab 			 * Discard this SACK block.
208b7c75571SPaul Saab 			 */
209b7c75571SPaul Saab 		} else if (SEQ_LEQ(head_blk.start, end) &&
210b7c75571SPaul Saab 			   SEQ_GEQ(head_blk.end, start)) {
211b7c75571SPaul Saab 			/*
212b7c75571SPaul Saab 			 * Merge this SACK block into head_blk.
213b7c75571SPaul Saab 			 * This SACK block itself will be discarded.
214b7c75571SPaul Saab 			 */
215b7c75571SPaul Saab 			if (SEQ_GT(head_blk.start, start))
216b7c75571SPaul Saab 				head_blk.start = start;
217b7c75571SPaul Saab 			if (SEQ_LT(head_blk.end, end))
218b7c75571SPaul Saab 				head_blk.end = end;
2196d90faf3SPaul Saab 		} else {
2206d90faf3SPaul Saab 			/*
221b7c75571SPaul Saab 			 * Save this SACK block.
2226d90faf3SPaul Saab 			 */
223b7c75571SPaul Saab 			saved_blks[num_saved].start = start;
224b7c75571SPaul Saab 			saved_blks[num_saved].end = end;
225b7c75571SPaul Saab 			num_saved++;
226b7c75571SPaul Saab 		}
227b7c75571SPaul Saab 	}
228b7c75571SPaul Saab 
2296d90faf3SPaul Saab 	/*
230b7c75571SPaul Saab 	 * Update SACK list in tp->sackblks[].
2316d90faf3SPaul Saab 	 */
232b7c75571SPaul Saab 	num_head = 0;
233b7c75571SPaul Saab 	if (SEQ_GT(head_blk.start, tp->rcv_nxt)) {
234b7c75571SPaul Saab 		/*
235b7c75571SPaul Saab 		 * The received data segment is an out-of-order segment.
236b7c75571SPaul Saab 		 * Put head_blk at the top of SACK list.
237b7c75571SPaul Saab 		 */
238b7c75571SPaul Saab 		tp->sackblks[0] = head_blk;
239b7c75571SPaul Saab 		num_head = 1;
240b7c75571SPaul Saab 		/*
241b7c75571SPaul Saab 		 * If the number of saved SACK blocks exceeds its limit,
242b7c75571SPaul Saab 		 * discard the last SACK block.
243b7c75571SPaul Saab 		 */
244b7c75571SPaul Saab 		if (num_saved >= MAX_SACK_BLKS)
245b7c75571SPaul Saab 			num_saved--;
2466d90faf3SPaul Saab 	}
247b7c75571SPaul Saab 	if (num_saved > 0) {
248b7c75571SPaul Saab 		/*
249b7c75571SPaul Saab 		 * Copy the saved SACK blocks back.
250b7c75571SPaul Saab 		 */
251b7c75571SPaul Saab 		bcopy(saved_blks, &tp->sackblks[num_head],
252b7c75571SPaul Saab 		      sizeof(struct sackblk) * num_saved);
2536d90faf3SPaul Saab 	}
254b7c75571SPaul Saab 
255b7c75571SPaul Saab 	/* Save the number of SACK blocks. */
256b7c75571SPaul Saab 	tp->rcv_numsacks = num_head + num_saved;
2576d90faf3SPaul Saab }
2586d90faf3SPaul Saab 
2596d90faf3SPaul Saab /*
2606d90faf3SPaul Saab  * Delete all receiver-side SACK information.
2616d90faf3SPaul Saab  */
2626d90faf3SPaul Saab void
2636d90faf3SPaul Saab tcp_clean_sackreport(tp)
2646d90faf3SPaul Saab 	struct tcpcb *tp;
2656d90faf3SPaul Saab {
2666d90faf3SPaul Saab 	int i;
2676d90faf3SPaul Saab 
2686d90faf3SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
2696d90faf3SPaul Saab 	tp->rcv_numsacks = 0;
2706d90faf3SPaul Saab 	for (i = 0; i < MAX_SACK_BLKS; i++)
2716d90faf3SPaul Saab 		tp->sackblks[i].start = tp->sackblks[i].end=0;
2726d90faf3SPaul Saab }
2736d90faf3SPaul Saab 
2746d90faf3SPaul Saab /*
2754fc53245SPaul Saab  * Allocate struct sackhole.
2764fc53245SPaul Saab  */
2774fc53245SPaul Saab static struct sackhole *
2784fc53245SPaul Saab tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
2794fc53245SPaul Saab {
2804fc53245SPaul Saab 	struct sackhole *hole;
2814fc53245SPaul Saab 
2824fc53245SPaul Saab 	if (tp->snd_numholes >= tcp_sack_maxholes ||
2834fc53245SPaul Saab 	    tcp_sack_globalholes >= tcp_sack_globalmaxholes) {
2844fc53245SPaul Saab 		tcpstat.tcps_sack_sboverflow++;
2854fc53245SPaul Saab 		return NULL;
2864fc53245SPaul Saab 	}
2874fc53245SPaul Saab 
2884fc53245SPaul Saab 	hole = (struct sackhole *)uma_zalloc(sack_hole_zone, M_NOWAIT);
2894fc53245SPaul Saab 	if (hole == NULL)
2904fc53245SPaul Saab 		return NULL;
2914fc53245SPaul Saab 
2924fc53245SPaul Saab 	hole->start = start;
2934fc53245SPaul Saab 	hole->end = end;
2944fc53245SPaul Saab 	hole->rxmit = start;
2954fc53245SPaul Saab 
2964fc53245SPaul Saab 	tp->snd_numholes++;
2974fc53245SPaul Saab 	tcp_sack_globalholes++;
2984fc53245SPaul Saab 
2994fc53245SPaul Saab 	return hole;
3004fc53245SPaul Saab }
3014fc53245SPaul Saab 
3024fc53245SPaul Saab /*
3034fc53245SPaul Saab  * Free struct sackhole.
3044fc53245SPaul Saab  */
3054fc53245SPaul Saab static void
3064fc53245SPaul Saab tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
3074fc53245SPaul Saab {
3084fc53245SPaul Saab 	uma_zfree(sack_hole_zone, hole);
3094fc53245SPaul Saab 
3104fc53245SPaul Saab 	tp->snd_numholes--;
3114fc53245SPaul Saab 	tcp_sack_globalholes--;
3124fc53245SPaul Saab 
3134fc53245SPaul Saab 	KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
3144fc53245SPaul Saab 	KASSERT(tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
3154fc53245SPaul Saab }
3164fc53245SPaul Saab 
3174fc53245SPaul Saab /*
3189d17a7a6SPaul Saab  * Insert new SACK hole into scoreboard.
3199d17a7a6SPaul Saab  */
3209d17a7a6SPaul Saab static struct sackhole *
3219d17a7a6SPaul Saab tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
3229d17a7a6SPaul Saab     struct sackhole *after)
3239d17a7a6SPaul Saab {
3249d17a7a6SPaul Saab 	struct sackhole *hole;
3259d17a7a6SPaul Saab 
3269d17a7a6SPaul Saab 	/* Allocate a new SACK hole. */
3279d17a7a6SPaul Saab 	hole = tcp_sackhole_alloc(tp, start, end);
3289d17a7a6SPaul Saab 	if (hole == NULL)
3299d17a7a6SPaul Saab 		return NULL;
3309d17a7a6SPaul Saab 
3319d17a7a6SPaul Saab 	/* Insert the new SACK hole into scoreboard */
3329d17a7a6SPaul Saab 	if (after != NULL)
3339d17a7a6SPaul Saab 		TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
3349d17a7a6SPaul Saab 	else
3359d17a7a6SPaul Saab 		TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
3369d17a7a6SPaul Saab 
3379d17a7a6SPaul Saab 	/* Update SACK hint. */
3389d17a7a6SPaul Saab 	if (tp->sackhint.nexthole == NULL)
3399d17a7a6SPaul Saab 		tp->sackhint.nexthole = hole;
3409d17a7a6SPaul Saab 
3419d17a7a6SPaul Saab 	return hole;
3429d17a7a6SPaul Saab }
3439d17a7a6SPaul Saab 
3449d17a7a6SPaul Saab /*
3459d17a7a6SPaul Saab  * Remove SACK hole from scoreboard.
3469d17a7a6SPaul Saab  */
3479d17a7a6SPaul Saab static void
3489d17a7a6SPaul Saab tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
3499d17a7a6SPaul Saab {
3509d17a7a6SPaul Saab 	/* Update SACK hint. */
3519d17a7a6SPaul Saab 	if (tp->sackhint.nexthole == hole)
3529d17a7a6SPaul Saab 		tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
3539d17a7a6SPaul Saab 
3549d17a7a6SPaul Saab 	/* Remove this SACK hole. */
3559d17a7a6SPaul Saab 	TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
3569d17a7a6SPaul Saab 
3579d17a7a6SPaul Saab 	/* Free this SACK hole. */
3589d17a7a6SPaul Saab 	tcp_sackhole_free(tp, hole);
3599d17a7a6SPaul Saab }
3609d17a7a6SPaul Saab 
3619d17a7a6SPaul Saab /*
3625a53ca16SPaul Saab  * Process cumulative ACK and the TCP SACK option to update the scoreboard.
3635a53ca16SPaul Saab  * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
3645a53ca16SPaul Saab  * the sequence space).
3656d90faf3SPaul Saab  */
3665a53ca16SPaul Saab void
3675a53ca16SPaul Saab tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack)
3686d90faf3SPaul Saab {
369a6235da6SPaul Saab 	struct sackhole *cur, *temp;
3705a53ca16SPaul Saab 	struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp;
3719d17a7a6SPaul Saab 	int i, j, num_sack_blks;
3726d90faf3SPaul Saab 
3736d90faf3SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
3745a53ca16SPaul Saab 
37564b5fbaaSPaul Saab 	num_sack_blks = 0;
3765a53ca16SPaul Saab 	/*
3775a53ca16SPaul Saab 	 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
3785a53ca16SPaul Saab 	 * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
3795a53ca16SPaul Saab 	 */
3805a53ca16SPaul Saab 	if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
3815a53ca16SPaul Saab 		sack_blocks[num_sack_blks].start = tp->snd_una;
3825a53ca16SPaul Saab 		sack_blocks[num_sack_blks++].end = th_ack;
3835a53ca16SPaul Saab 	}
3845a53ca16SPaul Saab 	/*
3855a53ca16SPaul Saab 	 * Append received valid SACK blocks to sack_blocks[].
3865a53ca16SPaul Saab 	 */
3875a53ca16SPaul Saab 	for (i = 0; i < to->to_nsacks; i++) {
3885a53ca16SPaul Saab 		bcopy((to->to_sacks + i * TCPOLEN_SACK), &sack, sizeof(sack));
3896d90faf3SPaul Saab 		sack.start = ntohl(sack.start);
3906d90faf3SPaul Saab 		sack.end = ntohl(sack.end);
39164b5fbaaSPaul Saab 		if (SEQ_GT(sack.end, sack.start) &&
39264b5fbaaSPaul Saab 		    SEQ_GT(sack.start, tp->snd_una) &&
3935a53ca16SPaul Saab 		    SEQ_GT(sack.start, th_ack) &&
3941f65c2cdSMohan Srinivasan 		    SEQ_LT(sack.start, tp->snd_max) &&
3951f65c2cdSMohan Srinivasan 		    SEQ_GT(sack.end, tp->snd_una) &&
39664b5fbaaSPaul Saab 		    SEQ_LEQ(sack.end, tp->snd_max))
39764b5fbaaSPaul Saab 			sack_blocks[num_sack_blks++] = sack;
3986d90faf3SPaul Saab 	}
3995a53ca16SPaul Saab 
4005a53ca16SPaul Saab 	/*
4015a53ca16SPaul Saab 	 * Return if SND.UNA is not advanced and no valid SACK block
4025a53ca16SPaul Saab 	 * is received.
4035a53ca16SPaul Saab 	 */
40464b5fbaaSPaul Saab 	if (num_sack_blks == 0)
4055a53ca16SPaul Saab 		return;
4065a53ca16SPaul Saab 
4075a53ca16SPaul Saab 	/*
4085a53ca16SPaul Saab 	 * Sort the SACK blocks so we can update the scoreboard
4095a53ca16SPaul Saab 	 * with just one pass. The overhead of sorting upto 4+1 elements
4105a53ca16SPaul Saab 	 * is less than making upto 4+1 passes over the scoreboard.
4115a53ca16SPaul Saab 	 */
41264b5fbaaSPaul Saab 	for (i = 0; i < num_sack_blks; i++) {
41364b5fbaaSPaul Saab 		for (j = i + 1; j < num_sack_blks; j++) {
4149d17a7a6SPaul Saab 			if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
41564b5fbaaSPaul Saab 				sack = sack_blocks[i];
41664b5fbaaSPaul Saab 				sack_blocks[i] = sack_blocks[j];
41764b5fbaaSPaul Saab 				sack_blocks[j] = sack;
4186d90faf3SPaul Saab 			}
4196d90faf3SPaul Saab 		}
42064b5fbaaSPaul Saab 	}
42164b5fbaaSPaul Saab 	if (TAILQ_EMPTY(&tp->snd_holes))
42264b5fbaaSPaul Saab 		/*
423808f11b7SPaul Saab 		 * Empty scoreboard. Need to initialize snd_fack (it may be
42464b5fbaaSPaul Saab 		 * uninitialized or have a bogus value). Scoreboard holes
42564b5fbaaSPaul Saab 		 * (from the sack blocks received) are created later below (in
42664b5fbaaSPaul Saab 		 * the logic that adds holes to the tail of the scoreboard).
42764b5fbaaSPaul Saab 		 */
4285a53ca16SPaul Saab 		tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
4299d17a7a6SPaul Saab 	/*
4309d17a7a6SPaul Saab 	 * In the while-loop below, incoming SACK blocks (sack_blocks[])
4319d17a7a6SPaul Saab 	 * and SACK holes (snd_holes) are traversed from their tails with
4329d17a7a6SPaul Saab 	 * just one pass in order to reduce the number of compares especially
4339d17a7a6SPaul Saab 	 * when the bandwidth-delay product is large.
4349d17a7a6SPaul Saab 	 * Note: Typically, in the first RTT of SACK recovery, the highest
4359d17a7a6SPaul Saab 	 * three or four SACK blocks with the same ack number are received.
4369d17a7a6SPaul Saab 	 * In the second RTT, if retransmitted data segments are not lost,
4379d17a7a6SPaul Saab 	 * the highest three or four SACK blocks with ack number advancing
4389d17a7a6SPaul Saab 	 * are received.
4399d17a7a6SPaul Saab 	 */
4409d17a7a6SPaul Saab 	sblkp = &sack_blocks[num_sack_blks - 1];	/* Last SACK block */
4419d17a7a6SPaul Saab 	if (SEQ_LT(tp->snd_fack, sblkp->start)) {
4429d17a7a6SPaul Saab 		/*
4439d17a7a6SPaul Saab 		 * The highest SACK block is beyond fack.
4449d17a7a6SPaul Saab 		 * Append new SACK hole at the tail.
4459d17a7a6SPaul Saab 		 * If the second or later highest SACK blocks are also
4469d17a7a6SPaul Saab 		 * beyond the current fack, they will be inserted by
4479d17a7a6SPaul Saab 		 * way of hole splitting in the while-loop below.
4489d17a7a6SPaul Saab 		 */
4498b9bbaaaSPaul Saab 		temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL);
450d0a14f55SPaul Saab 		if (temp != NULL) {
4519d17a7a6SPaul Saab 			tp->snd_fack = sblkp->end;
4529d17a7a6SPaul Saab 			/* Go to the previous sack block. */
4539d17a7a6SPaul Saab 			sblkp--;
454d0a14f55SPaul Saab 		} else {
455d0a14f55SPaul Saab 			/*
456d0a14f55SPaul Saab 			 * We failed to add a new hole based on the current
457d0a14f55SPaul Saab 			 * sack block.  Skip over all the sack blocks that
458d0a14f55SPaul Saab 			 * fall completely to the right of snd_fack and proceed
459d0a14f55SPaul Saab 			 * to trim the scoreboard based on the remaining sack
460d0a14f55SPaul Saab 			 * blocks. This also trims the scoreboard for th_ack
461d0a14f55SPaul Saab 			 * (which is sack_blocks[0]).
462d0a14f55SPaul Saab 			 */
463d0a14f55SPaul Saab 			while (sblkp >= sack_blocks &&
464d0a14f55SPaul Saab 			       SEQ_LT(tp->snd_fack, sblkp->start))
465d0a14f55SPaul Saab 				sblkp--;
466d0a14f55SPaul Saab 			if (sblkp >= sack_blocks &&
467d0a14f55SPaul Saab 			    SEQ_LT(tp->snd_fack, sblkp->end))
468d0a14f55SPaul Saab 				tp->snd_fack = sblkp->end;
469d0a14f55SPaul Saab 		}
4709d17a7a6SPaul Saab 	} else if (SEQ_LT(tp->snd_fack, sblkp->end))
4719d17a7a6SPaul Saab 		/* fack is advanced. */
4729d17a7a6SPaul Saab 		tp->snd_fack = sblkp->end;
4738b9bbaaaSPaul Saab 	/* We must have at least one SACK hole in scoreboard */
4748b9bbaaaSPaul Saab 	KASSERT(!TAILQ_EMPTY(&tp->snd_holes), ("SACK scoreboard must not be empty"));
4759d17a7a6SPaul Saab 	cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole */
47664b5fbaaSPaul Saab 	/*
47764b5fbaaSPaul Saab 	 * Since the incoming sack blocks are sorted, we can process them
47864b5fbaaSPaul Saab 	 * making one sweep of the scoreboard.
47964b5fbaaSPaul Saab 	 */
480d0a14f55SPaul Saab 	while (sblkp >= sack_blocks  && cur != NULL) {
4819d17a7a6SPaul Saab 		if (SEQ_GEQ(sblkp->start, cur->end)) {
48264b5fbaaSPaul Saab 			/*
48364b5fbaaSPaul Saab 			 * SACKs data beyond the current hole.
4849d17a7a6SPaul Saab 			 * Go to the previous sack block.
48564b5fbaaSPaul Saab 			 */
4869d17a7a6SPaul Saab 			sblkp--;
4879d17a7a6SPaul Saab 			continue;
4889d17a7a6SPaul Saab 		}
4899d17a7a6SPaul Saab 		if (SEQ_LEQ(sblkp->end, cur->start)) {
4909d17a7a6SPaul Saab 			/*
4919d17a7a6SPaul Saab 			 * SACKs data before the current hole.
4929d17a7a6SPaul Saab 			 * Go to the previous hole.
4939d17a7a6SPaul Saab 			 */
4949d17a7a6SPaul Saab 			cur = TAILQ_PREV(cur, sackhole_head, scblink);
4956d90faf3SPaul Saab 			continue;
4966d90faf3SPaul Saab 		}
49764b5fbaaSPaul Saab 		tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
4980077b016SPaul Saab 		KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
4990077b016SPaul Saab 			("sackhint bytes rtx >= 0"));
5009d17a7a6SPaul Saab 		if (SEQ_LEQ(sblkp->start, cur->start)) {
5016d90faf3SPaul Saab 			/* Data acks at least the beginning of hole */
5029d17a7a6SPaul Saab 			if (SEQ_GEQ(sblkp->end, cur->end)) {
5036d90faf3SPaul Saab 				/* Acks entire hole, so delete hole */
504a6235da6SPaul Saab 				temp = cur;
5059d17a7a6SPaul Saab 				cur = TAILQ_PREV(cur, sackhole_head, scblink);
5069d17a7a6SPaul Saab 				tcp_sackhole_remove(tp, temp);
50764b5fbaaSPaul Saab 				/*
50864b5fbaaSPaul Saab 				 * The sack block may ack all or part of the next
50964b5fbaaSPaul Saab 				 * hole too, so continue onto the next hole.
51064b5fbaaSPaul Saab 				 */
5116d90faf3SPaul Saab 				continue;
5120077b016SPaul Saab 			} else {
5130077b016SPaul Saab 				/* Move start of hole forward */
5149d17a7a6SPaul Saab 				cur->start = sblkp->end;
5156d90faf3SPaul Saab 				cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
5166d90faf3SPaul Saab 			}
51764b5fbaaSPaul Saab 		} else {
5189d17a7a6SPaul Saab 			/* Data acks at least the end of hole */
5199d17a7a6SPaul Saab 			if (SEQ_GEQ(sblkp->end, cur->end)) {
5200077b016SPaul Saab 				/* Move end of hole backward */
5219d17a7a6SPaul Saab 				cur->end = sblkp->start;
5226d90faf3SPaul Saab 				cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
5230077b016SPaul Saab 			} else {
5246d90faf3SPaul Saab 				/*
5256d90faf3SPaul Saab 				 * ACKs some data in middle of a hole; need to
5266d90faf3SPaul Saab 				 * split current hole
5276d90faf3SPaul Saab 				 */
5289d17a7a6SPaul Saab 				temp = tcp_sackhole_insert(tp, sblkp->end,
5299d17a7a6SPaul Saab 							   cur->end, cur);
5300077b016SPaul Saab 				if (temp != NULL) {
5319d17a7a6SPaul Saab 					if (SEQ_GT(cur->rxmit, temp->rxmit)) {
5324fc53245SPaul Saab 						temp->rxmit = cur->rxmit;
5339d17a7a6SPaul Saab 						tp->sackhint.sack_bytes_rexmit
5349d17a7a6SPaul Saab 							+= (temp->rxmit
5359d17a7a6SPaul Saab 							    - temp->start);
5369d17a7a6SPaul Saab 					}
5379d17a7a6SPaul Saab 					cur->end = sblkp->start;
5389d17a7a6SPaul Saab 					cur->rxmit = SEQ_MIN(cur->rxmit,
5399d17a7a6SPaul Saab 							     cur->end);
5406d90faf3SPaul Saab 				}
5416d90faf3SPaul Saab 			}
5420077b016SPaul Saab 		}
54364b5fbaaSPaul Saab 		tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
544db4b83feSPaul Saab 		/*
545db4b83feSPaul Saab 		 * Testing sblkp->start against cur->start tells us whether
546db4b83feSPaul Saab 		 * we're done with the sack block or the sack hole.
547db4b83feSPaul Saab 		 * Accordingly, we advance one or the other.
548db4b83feSPaul Saab 		 */
549db4b83feSPaul Saab 		if (SEQ_LEQ(sblkp->start, cur->start))
550db4b83feSPaul Saab 			cur = TAILQ_PREV(cur, sackhole_head, scblink);
551db4b83feSPaul Saab 		else
552db4b83feSPaul Saab 			sblkp--;
5536d90faf3SPaul Saab 	}
5546d90faf3SPaul Saab }
5556d90faf3SPaul Saab 
5566d90faf3SPaul Saab /*
5575a53ca16SPaul Saab  * Free all SACK holes to clear the scoreboard.
5586d90faf3SPaul Saab  */
5596d90faf3SPaul Saab void
5606d90faf3SPaul Saab tcp_free_sackholes(struct tcpcb *tp)
5616d90faf3SPaul Saab {
562a6235da6SPaul Saab 	struct sackhole *q;
5636d90faf3SPaul Saab 
5646d90faf3SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
5659d17a7a6SPaul Saab 	while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
5669d17a7a6SPaul Saab 		tcp_sackhole_remove(tp, q);
5670077b016SPaul Saab 	tp->sackhint.sack_bytes_rexmit = 0;
5684fc53245SPaul Saab 
5694fc53245SPaul Saab 	KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
5709d17a7a6SPaul Saab 	KASSERT(tp->sackhint.nexthole == NULL,
5719d17a7a6SPaul Saab 		("tp->sackhint.nexthole == NULL"));
5726d90faf3SPaul Saab }
5736d90faf3SPaul Saab 
5746d90faf3SPaul Saab /*
5758db456bfSPaul Saab  * Partial ack handling within a sack recovery episode.
5768db456bfSPaul Saab  * Keeping this very simple for now. When a partial ack
5778db456bfSPaul Saab  * is received, force snd_cwnd to a value that will allow
5788db456bfSPaul Saab  * the sender to transmit no more than 2 segments.
5798db456bfSPaul Saab  * If necessary, a better scheme can be adopted at a
5808db456bfSPaul Saab  * later point, but for now, the goal is to prevent the
5818db456bfSPaul Saab  * sender from bursting a large amount of data in the midst
5828db456bfSPaul Saab  * of sack recovery.
5836d90faf3SPaul Saab  */
5846d90faf3SPaul Saab void
585ad3f9ab3SAndre Oppermann tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th)
5866d90faf3SPaul Saab {
5878db456bfSPaul Saab 	int num_segs = 1;
5886d90faf3SPaul Saab 
58925e6f9edSPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
5906d90faf3SPaul Saab 	callout_stop(tp->tt_rexmt);
5916d90faf3SPaul Saab 	tp->t_rtttime = 0;
5928db456bfSPaul Saab 	/* send one or 2 segments based on how much new data was acked */
5938db456bfSPaul Saab 	if (((th->th_ack - tp->snd_una) / tp->t_maxseg) > 2)
5948db456bfSPaul Saab 		num_segs = 2;
5950077b016SPaul Saab 	tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
5960077b016SPaul Saab 		(tp->snd_nxt - tp->sack_newdata) +
5970077b016SPaul Saab 		num_segs * tp->t_maxseg);
59825e6f9edSPaul Saab 	if (tp->snd_cwnd > tp->snd_ssthresh)
59925e6f9edSPaul Saab 		tp->snd_cwnd = tp->snd_ssthresh;
6006d90faf3SPaul Saab 	tp->t_flags |= TF_ACKNOW;
6016d90faf3SPaul Saab 	(void) tcp_output(tp);
6026d90faf3SPaul Saab }
6036d90faf3SPaul Saab 
6041714e18eSMohan Srinivasan #if 0
6056d90faf3SPaul Saab /*
6060077b016SPaul Saab  * Debug version of tcp_sack_output() that walks the scoreboard. Used for
6070077b016SPaul Saab  * now to sanity check the hint.
6086d90faf3SPaul Saab  */
6090077b016SPaul Saab static struct sackhole *
6100077b016SPaul Saab tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
6116d90faf3SPaul Saab {
612a6235da6SPaul Saab 	struct sackhole *p;
6136d90faf3SPaul Saab 
6146d90faf3SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
615a55db2b6SPaul Saab 	*sack_bytes_rexmt = 0;
616a6235da6SPaul Saab 	TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
6176d90faf3SPaul Saab 		if (SEQ_LT(p->rxmit, p->end)) {
6186d90faf3SPaul Saab 			if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
6196d90faf3SPaul Saab 				continue;
6206d90faf3SPaul Saab 			}
621a55db2b6SPaul Saab 			*sack_bytes_rexmt += (p->rxmit - p->start);
622a55db2b6SPaul Saab 			break;
623a55db2b6SPaul Saab 		}
624a55db2b6SPaul Saab 		*sack_bytes_rexmt += (p->rxmit - p->start);
625a55db2b6SPaul Saab 	}
6266d90faf3SPaul Saab 	return (p);
6276d90faf3SPaul Saab }
6281714e18eSMohan Srinivasan #endif
6296d90faf3SPaul Saab 
6306d90faf3SPaul Saab /*
6310077b016SPaul Saab  * Returns the next hole to retransmit and the number of retransmitted bytes
6320077b016SPaul Saab  * from the scoreboard. We store both the next hole and the number of
6330077b016SPaul Saab  * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
6340077b016SPaul Saab  * reception). This avoids scoreboard traversals completely.
6350077b016SPaul Saab  *
6360077b016SPaul Saab  * The loop here will traverse *at most* one link. Here's the argument.
6370077b016SPaul Saab  * For the loop to traverse more than 1 link before finding the next hole to
6380077b016SPaul Saab  * retransmit, we would need to have at least 1 node following the current hint
6390077b016SPaul Saab  * with (rxmit == end). But, for all holes following the current hint,
6400077b016SPaul Saab  * (start == rxmit), since we have not yet retransmitted from them. Therefore,
6410077b016SPaul Saab  * in order to traverse more 1 link in the loop below, we need to have at least
6420077b016SPaul Saab  * one node following the current hint with (start == rxmit == end).
6430077b016SPaul Saab  * But that can't happen, (start == end) means that all the data in that hole
6440077b016SPaul Saab  * has been sacked, in which case, the hole would have been removed from the
6450077b016SPaul Saab  * scoreboard.
6460077b016SPaul Saab  */
6470077b016SPaul Saab struct sackhole *
6480077b016SPaul Saab tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
6490077b016SPaul Saab {
6501714e18eSMohan Srinivasan 	struct sackhole *hole = NULL;
6510077b016SPaul Saab 
6520077b016SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
6530077b016SPaul Saab 	*sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
6540077b016SPaul Saab 	hole = tp->sackhint.nexthole;
6550077b016SPaul Saab 	if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
6560077b016SPaul Saab 		goto out;
6570077b016SPaul Saab 	while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
6580077b016SPaul Saab 		if (SEQ_LT(hole->rxmit, hole->end)) {
6590077b016SPaul Saab 			tp->sackhint.nexthole = hole;
6600077b016SPaul Saab 			break;
6610077b016SPaul Saab 		}
6620077b016SPaul Saab 	}
6630077b016SPaul Saab out:
6640077b016SPaul Saab 	return (hole);
6650077b016SPaul Saab }
6660077b016SPaul Saab 
6670077b016SPaul Saab /*
6686d90faf3SPaul Saab  * After a timeout, the SACK list may be rebuilt.  This SACK information
6696d90faf3SPaul Saab  * should be used to avoid retransmitting SACKed data.  This function
6706d90faf3SPaul Saab  * traverses the SACK list to see if snd_nxt should be moved forward.
6716d90faf3SPaul Saab  */
6726d90faf3SPaul Saab void
6736d90faf3SPaul Saab tcp_sack_adjust(struct tcpcb *tp)
6746d90faf3SPaul Saab {
675a6235da6SPaul Saab 	struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
676b7c75571SPaul Saab 
677b7c75571SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
6786d90faf3SPaul Saab 	if (cur == NULL)
6796d90faf3SPaul Saab 		return; /* No holes */
680808f11b7SPaul Saab 	if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack))
6816d90faf3SPaul Saab 		return; /* We're already beyond any SACKed blocks */
6826d90faf3SPaul Saab 	/*
6836d90faf3SPaul Saab 	 * Two cases for which we want to advance snd_nxt:
6846d90faf3SPaul Saab 	 * i) snd_nxt lies between end of one hole and beginning of another
685808f11b7SPaul Saab 	 * ii) snd_nxt lies between end of last hole and snd_fack
6866d90faf3SPaul Saab 	 */
687a6235da6SPaul Saab 	while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
6886d90faf3SPaul Saab 		if (SEQ_LT(tp->snd_nxt, cur->end))
6896d90faf3SPaul Saab 			return;
690a6235da6SPaul Saab 		if (SEQ_GEQ(tp->snd_nxt, p->start))
691a6235da6SPaul Saab 			cur = p;
6926d90faf3SPaul Saab 		else {
693a6235da6SPaul Saab 			tp->snd_nxt = p->start;
6946d90faf3SPaul Saab 			return;
6956d90faf3SPaul Saab 		}
6966d90faf3SPaul Saab 	}
6976d90faf3SPaul Saab 	if (SEQ_LT(tp->snd_nxt, cur->end))
6986d90faf3SPaul Saab 		return;
699808f11b7SPaul Saab 	tp->snd_nxt = tp->snd_fack;
7006d90faf3SPaul Saab 	return;
7016d90faf3SPaul Saab }
702