xref: /freebsd/sys/netinet/tcp_sack.c (revision 64b5fbaa04a76f488c9a98b1c38b34292c99623c)
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_ipsec.h"
1026d90faf3SPaul Saab #include "opt_tcpdebug.h"
1036d90faf3SPaul Saab #include "opt_tcp_input.h"
1046d90faf3SPaul Saab #include "opt_tcp_sack.h"
1056d90faf3SPaul Saab 
1066d90faf3SPaul Saab #include <sys/param.h>
1076d90faf3SPaul Saab #include <sys/systm.h>
1086d90faf3SPaul Saab #include <sys/kernel.h>
1096d90faf3SPaul Saab #include <sys/sysctl.h>
1106d90faf3SPaul Saab #include <sys/malloc.h>
1116d90faf3SPaul Saab #include <sys/mbuf.h>
1126d90faf3SPaul Saab #include <sys/proc.h>		/* for proc0 declaration */
1136d90faf3SPaul Saab #include <sys/protosw.h>
1146d90faf3SPaul Saab #include <sys/socket.h>
1156d90faf3SPaul Saab #include <sys/socketvar.h>
1166d90faf3SPaul Saab #include <sys/syslog.h>
1176d90faf3SPaul Saab #include <sys/systm.h>
1186d90faf3SPaul Saab 
1196d90faf3SPaul Saab #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
1206d90faf3SPaul Saab 
1216d90faf3SPaul Saab #include <vm/uma.h>
1226d90faf3SPaul Saab 
1236d90faf3SPaul Saab #include <net/if.h>
1246d90faf3SPaul Saab #include <net/route.h>
1256d90faf3SPaul Saab 
1266d90faf3SPaul Saab #include <netinet/in.h>
1276d90faf3SPaul Saab #include <netinet/in_systm.h>
1286d90faf3SPaul Saab #include <netinet/ip.h>
1296d90faf3SPaul Saab #include <netinet/ip_icmp.h>	/* for ICMP_BANDLIM		*/
1306d90faf3SPaul Saab #include <netinet/in_var.h>
1316d90faf3SPaul Saab #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM		*/
1326d90faf3SPaul Saab #include <netinet/in_pcb.h>
1336d90faf3SPaul Saab #include <netinet/ip_var.h>
1346d90faf3SPaul Saab #include <netinet/ip6.h>
1356d90faf3SPaul Saab #include <netinet/icmp6.h>
1366d90faf3SPaul Saab #include <netinet6/nd6.h>
1376d90faf3SPaul Saab #include <netinet6/ip6_var.h>
1386d90faf3SPaul Saab #include <netinet6/in6_pcb.h>
1396d90faf3SPaul Saab #include <netinet/tcp.h>
1406d90faf3SPaul Saab #include <netinet/tcp_fsm.h>
1416d90faf3SPaul Saab #include <netinet/tcp_seq.h>
1426d90faf3SPaul Saab #include <netinet/tcp_timer.h>
1436d90faf3SPaul Saab #include <netinet/tcp_var.h>
1446d90faf3SPaul Saab #include <netinet6/tcp6_var.h>
1456d90faf3SPaul Saab #include <netinet/tcpip.h>
1466d90faf3SPaul Saab #ifdef TCPDEBUG
1476d90faf3SPaul Saab #include <netinet/tcp_debug.h>
1486d90faf3SPaul Saab #endif /* TCPDEBUG */
1496d90faf3SPaul Saab 
1506d90faf3SPaul Saab #ifdef FAST_IPSEC
1516d90faf3SPaul Saab #include <netipsec/ipsec.h>
1526d90faf3SPaul Saab #include <netipsec/ipsec6.h>
1536d90faf3SPaul Saab #endif
1546d90faf3SPaul Saab 
1556d90faf3SPaul Saab #ifdef IPSEC
1566d90faf3SPaul Saab #include <netinet6/ipsec.h>
1576d90faf3SPaul Saab #include <netinet6/ipsec6.h>
1586d90faf3SPaul Saab #include <netkey/key.h>
1596d90faf3SPaul Saab #endif /*IPSEC*/
1606d90faf3SPaul Saab #include <machine/in_cksum.h>
1616d90faf3SPaul Saab 
1626d90faf3SPaul Saab extern struct uma_zone *sack_hole_zone;
1636d90faf3SPaul Saab 
164a55db2b6SPaul Saab SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, 0, "TCP SACK");
165a55db2b6SPaul Saab int tcp_do_sack = 1;
166a55db2b6SPaul Saab SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_RW,
167a55db2b6SPaul Saab 	&tcp_do_sack, 0, "Enable/Disable TCP SACK support");
168c4274833SRobert Watson TUNABLE_INT("net.inet.tcp.sack.enable", &tcp_do_sack);
169a55db2b6SPaul Saab 
170e891d82bSPaul Saab static int tcp_sack_maxholes = 128;
171e891d82bSPaul Saab SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_RW,
172e891d82bSPaul Saab 	&tcp_sack_maxholes, 0,
173e891d82bSPaul Saab     "Maximum number of TCP SACK holes allowed per connection");
174e891d82bSPaul Saab 
175e891d82bSPaul Saab static int tcp_sack_globalmaxholes = 65536;
176e891d82bSPaul Saab SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_RW,
177e891d82bSPaul Saab 	&tcp_sack_globalmaxholes, 0,
178e891d82bSPaul Saab     "Global maximum number of TCP SACK holes");
179e891d82bSPaul Saab 
180e891d82bSPaul Saab static int tcp_sack_globalholes = 0;
181e891d82bSPaul Saab SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_RD,
182e891d82bSPaul Saab     &tcp_sack_globalholes, 0,
183e891d82bSPaul Saab     "Global number of TCP SACK holes currently allocated");
184b7c75571SPaul Saab 
1856d90faf3SPaul Saab /*
1866d90faf3SPaul Saab  * This function is called upon receipt of new valid data (while not in header
1876d90faf3SPaul Saab  * prediction mode), and it updates the ordered list of sacks.
1886d90faf3SPaul Saab  */
1896d90faf3SPaul Saab void
190b7c75571SPaul Saab tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
1916d90faf3SPaul Saab {
1926d90faf3SPaul Saab 	/*
1936d90faf3SPaul Saab 	 * First reported block MUST be the most recent one.  Subsequent
1946d90faf3SPaul Saab 	 * blocks SHOULD be in the order in which they arrived at the
1956d90faf3SPaul Saab 	 * receiver.  These two conditions make the implementation fully
1966d90faf3SPaul Saab 	 * compliant with RFC 2018.
1976d90faf3SPaul Saab 	 */
198b7c75571SPaul Saab 	struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
199b7c75571SPaul Saab 	int num_head, num_saved, i;
2006d90faf3SPaul Saab 
2016d90faf3SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
202b7c75571SPaul Saab 
203b7c75571SPaul Saab 	/* Check arguments */
204b7c75571SPaul Saab 	KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end"));
205b7c75571SPaul Saab 
206b7c75571SPaul Saab 	/* SACK block for the received segment. */
207b7c75571SPaul Saab 	head_blk.start = rcv_start;
208b7c75571SPaul Saab 	head_blk.end = rcv_end;
209b7c75571SPaul Saab 
210b7c75571SPaul Saab 	/*
211b7c75571SPaul Saab 	 * Merge updated SACK blocks into head_blk, and
212b7c75571SPaul Saab 	 * save unchanged SACK blocks into saved_blks[].
213b7c75571SPaul Saab 	 * num_saved will have the number of the saved SACK blocks.
214b7c75571SPaul Saab 	 */
215b7c75571SPaul Saab 	num_saved = 0;
2166d90faf3SPaul Saab 	for (i = 0; i < tp->rcv_numsacks; i++) {
217b7c75571SPaul Saab 		tcp_seq start = tp->sackblks[i].start;
218b7c75571SPaul Saab 		tcp_seq end = tp->sackblks[i].end;
219b7c75571SPaul Saab 		if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
220b7c75571SPaul Saab 			/*
221b7c75571SPaul Saab 			 * Discard this SACK block.
222b7c75571SPaul Saab 			 */
223b7c75571SPaul Saab 		} else if (SEQ_LEQ(head_blk.start, end) &&
224b7c75571SPaul Saab 			   SEQ_GEQ(head_blk.end, start)) {
225b7c75571SPaul Saab 			/*
226b7c75571SPaul Saab 			 * Merge this SACK block into head_blk.
227b7c75571SPaul Saab 			 * This SACK block itself will be discarded.
228b7c75571SPaul Saab 			 */
229b7c75571SPaul Saab 			if (SEQ_GT(head_blk.start, start))
230b7c75571SPaul Saab 				head_blk.start = start;
231b7c75571SPaul Saab 			if (SEQ_LT(head_blk.end, end))
232b7c75571SPaul Saab 				head_blk.end = end;
2336d90faf3SPaul Saab 		} else {
2346d90faf3SPaul Saab 			/*
235b7c75571SPaul Saab 			 * Save this SACK block.
2366d90faf3SPaul Saab 			 */
237b7c75571SPaul Saab 			saved_blks[num_saved].start = start;
238b7c75571SPaul Saab 			saved_blks[num_saved].end = end;
239b7c75571SPaul Saab 			num_saved++;
240b7c75571SPaul Saab 		}
241b7c75571SPaul Saab 	}
242b7c75571SPaul Saab 
2436d90faf3SPaul Saab 	/*
244b7c75571SPaul Saab 	 * Update SACK list in tp->sackblks[].
2456d90faf3SPaul Saab 	 */
246b7c75571SPaul Saab 	num_head = 0;
247b7c75571SPaul Saab 	if (SEQ_GT(head_blk.start, tp->rcv_nxt)) {
248b7c75571SPaul Saab 		/*
249b7c75571SPaul Saab 		 * The received data segment is an out-of-order segment.
250b7c75571SPaul Saab 		 * Put head_blk at the top of SACK list.
251b7c75571SPaul Saab 		 */
252b7c75571SPaul Saab 		tp->sackblks[0] = head_blk;
253b7c75571SPaul Saab 		num_head = 1;
254b7c75571SPaul Saab 		/*
255b7c75571SPaul Saab 		 * If the number of saved SACK blocks exceeds its limit,
256b7c75571SPaul Saab 		 * discard the last SACK block.
257b7c75571SPaul Saab 		 */
258b7c75571SPaul Saab 		if (num_saved >= MAX_SACK_BLKS)
259b7c75571SPaul Saab 			num_saved--;
2606d90faf3SPaul Saab 	}
261b7c75571SPaul Saab 	if (num_saved > 0) {
262b7c75571SPaul Saab 		/*
263b7c75571SPaul Saab 		 * Copy the saved SACK blocks back.
264b7c75571SPaul Saab 		 */
265b7c75571SPaul Saab 		bcopy(saved_blks, &tp->sackblks[num_head],
266b7c75571SPaul Saab 		      sizeof(struct sackblk) * num_saved);
2676d90faf3SPaul Saab 	}
268b7c75571SPaul Saab 
269b7c75571SPaul Saab 	/* Save the number of SACK blocks. */
270b7c75571SPaul Saab 	tp->rcv_numsacks = num_head + num_saved;
2716d90faf3SPaul Saab }
2726d90faf3SPaul Saab 
2736d90faf3SPaul Saab /*
2746d90faf3SPaul Saab  * Delete all receiver-side SACK information.
2756d90faf3SPaul Saab  */
2766d90faf3SPaul Saab void
2776d90faf3SPaul Saab tcp_clean_sackreport(tp)
2786d90faf3SPaul Saab 	struct tcpcb *tp;
2796d90faf3SPaul Saab {
2806d90faf3SPaul Saab 	int i;
2816d90faf3SPaul Saab 
2826d90faf3SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
2836d90faf3SPaul Saab 	tp->rcv_numsacks = 0;
2846d90faf3SPaul Saab 	for (i = 0; i < MAX_SACK_BLKS; i++)
2856d90faf3SPaul Saab 		tp->sackblks[i].start = tp->sackblks[i].end=0;
2866d90faf3SPaul Saab }
2876d90faf3SPaul Saab 
2886d90faf3SPaul Saab /*
2894fc53245SPaul Saab  * Allocate struct sackhole.
2904fc53245SPaul Saab  */
2914fc53245SPaul Saab static struct sackhole *
2924fc53245SPaul Saab tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
2934fc53245SPaul Saab {
2944fc53245SPaul Saab 	struct sackhole *hole;
2954fc53245SPaul Saab 
2964fc53245SPaul Saab 	if (tp->snd_numholes >= tcp_sack_maxholes ||
2974fc53245SPaul Saab 	    tcp_sack_globalholes >= tcp_sack_globalmaxholes) {
2984fc53245SPaul Saab 		tcpstat.tcps_sack_sboverflow++;
2994fc53245SPaul Saab 		return NULL;
3004fc53245SPaul Saab 	}
3014fc53245SPaul Saab 
3024fc53245SPaul Saab 	hole = (struct sackhole *)uma_zalloc(sack_hole_zone, M_NOWAIT);
3034fc53245SPaul Saab 	if (hole == NULL)
3044fc53245SPaul Saab 		return NULL;
3054fc53245SPaul Saab 
3064fc53245SPaul Saab 	hole->start = start;
3074fc53245SPaul Saab 	hole->end = end;
3084fc53245SPaul Saab 	hole->rxmit = start;
3094fc53245SPaul Saab 
3104fc53245SPaul Saab 	tp->snd_numholes++;
3114fc53245SPaul Saab 	tcp_sack_globalholes++;
3124fc53245SPaul Saab 
3134fc53245SPaul Saab 	return hole;
3144fc53245SPaul Saab }
3154fc53245SPaul Saab 
3164fc53245SPaul Saab /*
3174fc53245SPaul Saab  * Free struct sackhole.
3184fc53245SPaul Saab  */
3194fc53245SPaul Saab static void
3204fc53245SPaul Saab tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
3214fc53245SPaul Saab {
3224fc53245SPaul Saab 	uma_zfree(sack_hole_zone, hole);
3234fc53245SPaul Saab 
3244fc53245SPaul Saab 	tp->snd_numholes--;
3254fc53245SPaul Saab 	tcp_sack_globalholes--;
3264fc53245SPaul Saab 
3274fc53245SPaul Saab 	KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
3284fc53245SPaul Saab 	KASSERT(tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
3294fc53245SPaul Saab }
3304fc53245SPaul Saab 
3314fc53245SPaul Saab /*
3326d90faf3SPaul Saab  * Process the TCP SACK option.  Returns 1 if tcp_dooptions() should continue,
3336d90faf3SPaul Saab  * and 0 otherwise, if the option was fine.  tp->snd_holes is an ordered list
3346d90faf3SPaul Saab  * of holes (oldest to newest, in terms of the sequence space).
3356d90faf3SPaul Saab  */
3366d90faf3SPaul Saab int
3376d90faf3SPaul Saab tcp_sack_option(struct tcpcb *tp, struct tcphdr *th, u_char *cp, int optlen)
3386d90faf3SPaul Saab {
3396d90faf3SPaul Saab 	int tmp_olen;
3406d90faf3SPaul Saab 	u_char *tmp_cp;
341a6235da6SPaul Saab 	struct sackhole *cur, *temp;
34264b5fbaaSPaul Saab 	struct sackblk sack, sack_blocks[TCP_MAX_SACK];
34364b5fbaaSPaul Saab 	int i, j, next_sack_blk, num_sack_blks;
3446d90faf3SPaul Saab 
3456d90faf3SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
3466d90faf3SPaul Saab 	if (!tp->sack_enable)
3476d90faf3SPaul Saab 		return (1);
348e891d82bSPaul Saab 	if ((th->th_flags & TH_ACK) == 0)
349e891d82bSPaul Saab 		return (1);
3506d90faf3SPaul Saab 	/* Note: TCPOLEN_SACK must be 2*sizeof(tcp_seq) */
3516d90faf3SPaul Saab 	if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
3526d90faf3SPaul Saab 		return (1);
353e891d82bSPaul Saab 	/* If ack is outside [snd_una, snd_max], ignore the SACK options */
35482912940SPaul Saab 	if (SEQ_LT(th->th_ack, tp->snd_una) || SEQ_GT(th->th_ack, tp->snd_max))
35582912940SPaul Saab 		return (1);
3566d90faf3SPaul Saab 	tmp_cp = cp + 2;
3576d90faf3SPaul Saab 	tmp_olen = optlen - 2;
3586d90faf3SPaul Saab 	tcpstat.tcps_sack_rcv_blocks++;
35964b5fbaaSPaul Saab 	/*
36064b5fbaaSPaul Saab 	 * Sort the SACK blocks so we can update the scoreboard
36164b5fbaaSPaul Saab 	 * with just one pass. The overhead of sorting upto 4 elements
36264b5fbaaSPaul Saab 	 * is less than making 3 passes over the scoreboard.
36364b5fbaaSPaul Saab 	 */
36464b5fbaaSPaul Saab 	num_sack_blks = 0;
3656d90faf3SPaul Saab 	while (tmp_olen > 0) {
36664b5fbaaSPaul Saab 		bcopy(tmp_cp, &sack, sizeof(sack));
3676d90faf3SPaul Saab 		sack.start = ntohl(sack.start);
3686d90faf3SPaul Saab 		sack.end = ntohl(sack.end);
36964b5fbaaSPaul Saab 		if (SEQ_GT(sack.end, sack.start) &&
37064b5fbaaSPaul Saab 		    SEQ_GT(sack.start, tp->snd_una) &&
37164b5fbaaSPaul Saab 		    SEQ_GT(sack.start, th->th_ack) &&
37264b5fbaaSPaul Saab 		    SEQ_LEQ(sack.end, tp->snd_max))
37364b5fbaaSPaul Saab 			sack_blocks[num_sack_blks++] = sack;
3746d90faf3SPaul Saab 		tmp_olen -= TCPOLEN_SACK;
3756d90faf3SPaul Saab 		tmp_cp += TCPOLEN_SACK;
3766d90faf3SPaul Saab 	}
37764b5fbaaSPaul Saab 	if (num_sack_blks == 0)
37864b5fbaaSPaul Saab 		return 0;
37964b5fbaaSPaul Saab 	/* Bubble sort */
38064b5fbaaSPaul Saab 	for (i = 0; i < num_sack_blks; i++) {
38164b5fbaaSPaul Saab 		for (j = i + 1; j < num_sack_blks; j++) {
38264b5fbaaSPaul Saab 			if (SEQ_GT(sack_blocks[i].start,
38364b5fbaaSPaul Saab 				   sack_blocks[j].start)){
38464b5fbaaSPaul Saab 				sack = sack_blocks[i];
38564b5fbaaSPaul Saab 				sack_blocks[i] = sack_blocks[j];
38664b5fbaaSPaul Saab 				sack_blocks[j] = sack;
3876d90faf3SPaul Saab 			}
3886d90faf3SPaul Saab 		}
38964b5fbaaSPaul Saab 	}
39064b5fbaaSPaul Saab 	if (TAILQ_EMPTY(&tp->snd_holes))
39164b5fbaaSPaul Saab 		/*
39264b5fbaaSPaul Saab 		 * Empty scoreboard. Need to initialize rcv_lastsack (it may be
39364b5fbaaSPaul Saab 		 * uninitialized or have a bogus value). Scoreboard holes
39464b5fbaaSPaul Saab 		 * (from the sack blocks received) are created later below (in
39564b5fbaaSPaul Saab 		 * the logic that adds holes to the tail of the scoreboard).
39664b5fbaaSPaul Saab 		 */
39764b5fbaaSPaul Saab 		tp->rcv_lastsack = tp->snd_una;
39864b5fbaaSPaul Saab 	next_sack_blk = 0;
399a6235da6SPaul Saab 	cur = TAILQ_FIRST(&tp->snd_holes);
40064b5fbaaSPaul Saab 	/*
40164b5fbaaSPaul Saab 	 * Since the incoming sack blocks are sorted, we can process them
40264b5fbaaSPaul Saab 	 * making one sweep of the scoreboard.
40364b5fbaaSPaul Saab 	 */
40464b5fbaaSPaul Saab 	while ((next_sack_blk < num_sack_blks) && (cur != NULL)) {
40564b5fbaaSPaul Saab 		sack = sack_blocks[next_sack_blk];
40664b5fbaaSPaul Saab 		if (SEQ_LT(tp->rcv_lastsack, sack.start))
40764b5fbaaSPaul Saab 			/*
40864b5fbaaSPaul Saab 			 * The sack block acks data to the right of all the holes
40964b5fbaaSPaul Saab 			 * in the scoreboard. No need to iterate over the
41064b5fbaaSPaul Saab 			 * scoreboard anymore.
41164b5fbaaSPaul Saab 			 */
41264b5fbaaSPaul Saab 			break;
41364b5fbaaSPaul Saab 		if (SEQ_LEQ(sack.end, cur->start)) {
41464b5fbaaSPaul Saab 			/*
41564b5fbaaSPaul Saab 			 * SACKs data before the current hole.
41664b5fbaaSPaul Saab 			 * Ignore the sack block. Go to the next sack
41764b5fbaaSPaul Saab 			 * block.
41864b5fbaaSPaul Saab 			 */
41964b5fbaaSPaul Saab 			next_sack_blk++;
42064b5fbaaSPaul Saab 			continue;
42164b5fbaaSPaul Saab 		}
4226d90faf3SPaul Saab 		if (SEQ_GEQ(sack.start, cur->end)) {
42364b5fbaaSPaul Saab 			/*
42464b5fbaaSPaul Saab 			 * SACKs data beyond the current hole.
42564b5fbaaSPaul Saab 			 * Go to the next hole.
42664b5fbaaSPaul Saab 			 */
427a6235da6SPaul Saab 			cur = TAILQ_NEXT(cur, scblink);
4286d90faf3SPaul Saab 			continue;
4296d90faf3SPaul Saab 		}
43064b5fbaaSPaul Saab 		tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
4310077b016SPaul Saab 		KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
4320077b016SPaul Saab 			("sackhint bytes rtx >= 0"));
4336d90faf3SPaul Saab 		if (SEQ_LEQ(sack.start, cur->start)) {
4346d90faf3SPaul Saab 			/* Data acks at least the beginning of hole */
4356d90faf3SPaul Saab 			if (SEQ_GEQ(sack.end, cur->end)) {
4366d90faf3SPaul Saab 				/* Acks entire hole, so delete hole */
4370077b016SPaul Saab 				if (tp->sackhint.nexthole == cur)
4380077b016SPaul Saab 					tp->sackhint.nexthole =
4390077b016SPaul Saab 						TAILQ_NEXT(cur, scblink);
440a6235da6SPaul Saab 				temp = cur;
441a6235da6SPaul Saab 				cur = TAILQ_NEXT(cur, scblink);
44264b5fbaaSPaul Saab 				TAILQ_REMOVE(&tp->snd_holes, temp, scblink);
4434fc53245SPaul Saab 				tcp_sackhole_free(tp, temp);
44464b5fbaaSPaul Saab 				/*
44564b5fbaaSPaul Saab 				 * The sack block may ack all or part of the next
44664b5fbaaSPaul Saab 				 * hole too, so continue onto the next hole.
44764b5fbaaSPaul Saab 				 */
4486d90faf3SPaul Saab 				continue;
4490077b016SPaul Saab 			} else {
4500077b016SPaul Saab 				/* Move start of hole forward */
4516d90faf3SPaul Saab 				cur->start = sack.end;
4526d90faf3SPaul Saab 				cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
4536d90faf3SPaul Saab 			}
45464b5fbaaSPaul Saab 		} else {
45564b5fbaaSPaul Saab 			if (SEQ_GEQ(sack.end, cur->end)) {
4560077b016SPaul Saab 				/* Move end of hole backward */
4576d90faf3SPaul Saab 				cur->end = sack.start;
4586d90faf3SPaul Saab 				cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
4590077b016SPaul Saab 			} else {
4606d90faf3SPaul Saab 				/*
4616d90faf3SPaul Saab 				 * ACKs some data in middle of a hole; need to
4626d90faf3SPaul Saab 				 * split current hole
4636d90faf3SPaul Saab 				 */
4644fc53245SPaul Saab 				temp = tcp_sackhole_alloc(tp, sack.end,
4654fc53245SPaul Saab 							  cur->end);
4660077b016SPaul Saab 				if (temp != NULL) {
4674fc53245SPaul Saab 					if (SEQ_GT(cur->rxmit, temp->rxmit))
4684fc53245SPaul Saab 						temp->rxmit = cur->rxmit;
4694fc53245SPaul Saab 					TAILQ_INSERT_AFTER(&tp->snd_holes,
4704fc53245SPaul Saab 							   cur, temp, scblink);
4716d90faf3SPaul Saab 					cur->end = sack.start;
47264b5fbaaSPaul Saab 					cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
4730077b016SPaul Saab 					tp->sackhint.sack_bytes_rexmit +=
4740077b016SPaul Saab 						(cur->rxmit - cur->start);
4750077b016SPaul Saab 					cur = temp;
4766d90faf3SPaul Saab 				}
4776d90faf3SPaul Saab 			}
4780077b016SPaul Saab 		}
47964b5fbaaSPaul Saab 		tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
48064b5fbaaSPaul Saab 		/*
48164b5fbaaSPaul Saab 		 * Testing sack.end against cur->end tells us whether we're done
48264b5fbaaSPaul Saab 		 * with the sack block or the sack hole. Accordingly, we advance
48364b5fbaaSPaul Saab 		 * one or the other.
48464b5fbaaSPaul Saab 		 */
48564b5fbaaSPaul Saab 		if (SEQ_GEQ(sack.end, cur->end))
48664b5fbaaSPaul Saab 			cur = TAILQ_NEXT(cur, scblink);
48764b5fbaaSPaul Saab 		else
48864b5fbaaSPaul Saab 			next_sack_blk++;
48964b5fbaaSPaul Saab 	}
49064b5fbaaSPaul Saab 	/* Iterated all the holes in the scoreboard. Add new holes. */
49164b5fbaaSPaul Saab 	for ( ; next_sack_blk < num_sack_blks ; next_sack_blk++) {
49264b5fbaaSPaul Saab 		sack = sack_blocks[next_sack_blk];
49364b5fbaaSPaul Saab 		/*
49464b5fbaaSPaul Saab 		 * The two SEQ_LT() checks here that test rcv_laststart against
49564b5fbaaSPaul Saab 		 * sack.start and sack.end seem redundant, but they're necessary
49664b5fbaaSPaul Saab 		 * to deal with overlapping sack blocks.
49764b5fbaaSPaul Saab 		 */
4986d90faf3SPaul Saab 		if (SEQ_LT(tp->rcv_lastsack, sack.start)) {
499a6235da6SPaul Saab 			/* Need to append new hole at end. */
5004fc53245SPaul Saab 			temp = tcp_sackhole_alloc(tp, tp->rcv_lastsack,
5014fc53245SPaul Saab 						  sack.start);
5026d90faf3SPaul Saab 			if (temp == NULL)
5036d90faf3SPaul Saab 				continue; /* ENOBUFS */
504a6235da6SPaul Saab 			TAILQ_INSERT_TAIL(&tp->snd_holes, temp, scblink);
5054fc53245SPaul Saab 			tp->rcv_lastsack = sack.end;
506fdace17fSPaul Saab 			if (tp->sackhint.nexthole == NULL)
507fdace17fSPaul Saab 				tp->sackhint.nexthole = temp;
5086d90faf3SPaul Saab 		}
509b962fa74SPaul Saab 		if (SEQ_LT(tp->rcv_lastsack, sack.end))
510b962fa74SPaul Saab 			tp->rcv_lastsack = sack.end;
5116d90faf3SPaul Saab 	}
5126d90faf3SPaul Saab 	return (0);
5136d90faf3SPaul Saab }
5146d90faf3SPaul Saab 
5156d90faf3SPaul Saab /*
5166d90faf3SPaul Saab  * Delete stale (i.e, cumulatively ack'd) holes.  Hole is deleted only if
5176d90faf3SPaul Saab  * it is completely acked; otherwise, tcp_sack_option(), called from
5186d90faf3SPaul Saab  * tcp_dooptions(), will fix up the hole.
5196d90faf3SPaul Saab  */
5206d90faf3SPaul Saab void
5216d90faf3SPaul Saab tcp_del_sackholes(tp, th)
5226d90faf3SPaul Saab 	struct tcpcb *tp;
5236d90faf3SPaul Saab 	struct tcphdr *th;
5246d90faf3SPaul Saab {
5256d90faf3SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
5266d90faf3SPaul Saab 	if (tp->sack_enable && tp->t_state != TCPS_LISTEN) {
5276d90faf3SPaul Saab 		/* max because this could be an older ack just arrived */
5286d90faf3SPaul Saab 		tcp_seq lastack = SEQ_GT(th->th_ack, tp->snd_una) ?
5296d90faf3SPaul Saab 			th->th_ack : tp->snd_una;
530a6235da6SPaul Saab 		struct sackhole *cur = TAILQ_FIRST(&tp->snd_holes);
5316d90faf3SPaul Saab 		struct sackhole *prev;
5326d90faf3SPaul Saab 		while (cur)
5336d90faf3SPaul Saab 			if (SEQ_LEQ(cur->end, lastack)) {
5346d90faf3SPaul Saab 				prev = cur;
5350077b016SPaul Saab 				tp->sackhint.sack_bytes_rexmit -=
5360077b016SPaul Saab 					(cur->rxmit - cur->start);
5370077b016SPaul Saab 				if (tp->sackhint.nexthole == cur)
5380077b016SPaul Saab 					tp->sackhint.nexthole =
5390077b016SPaul Saab 					    TAILQ_NEXT(cur, scblink);
540a6235da6SPaul Saab 				cur = TAILQ_NEXT(cur, scblink);
541a6235da6SPaul Saab 				TAILQ_REMOVE(&tp->snd_holes, prev, scblink);
5424fc53245SPaul Saab 				tcp_sackhole_free(tp, prev);
5436d90faf3SPaul Saab 			} else if (SEQ_LT(cur->start, lastack)) {
5440077b016SPaul Saab 				if (SEQ_LT(cur->rxmit, lastack)) {
5450077b016SPaul Saab 					tp->sackhint.sack_bytes_rexmit -=
5460077b016SPaul Saab 					    (cur->rxmit - cur->start);
5470077b016SPaul Saab 					cur->rxmit = lastack;
5480077b016SPaul Saab 				} else
5490077b016SPaul Saab 					tp->sackhint.sack_bytes_rexmit -=
5500077b016SPaul Saab 					    (lastack - cur->start);
5516d90faf3SPaul Saab 				cur->start = lastack;
5526d90faf3SPaul Saab 				break;
5536d90faf3SPaul Saab 			} else
5546d90faf3SPaul Saab 				break;
5556d90faf3SPaul Saab 	}
5566d90faf3SPaul Saab }
5576d90faf3SPaul Saab 
5586d90faf3SPaul Saab void
5596d90faf3SPaul Saab tcp_free_sackholes(struct tcpcb *tp)
5606d90faf3SPaul Saab {
561a6235da6SPaul Saab 	struct sackhole *q;
5626d90faf3SPaul Saab 
5636d90faf3SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
564a6235da6SPaul Saab 	while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL) {
565a6235da6SPaul Saab 		TAILQ_REMOVE(&tp->snd_holes, q, scblink);
5664fc53245SPaul Saab 		tcp_sackhole_free(tp, q);
5676d90faf3SPaul Saab 	}
5680077b016SPaul Saab 	tp->sackhint.nexthole = NULL;
5690077b016SPaul Saab 	tp->sackhint.sack_bytes_rexmit = 0;
5704fc53245SPaul Saab 
5714fc53245SPaul Saab 	KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
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
5856d90faf3SPaul Saab tcp_sack_partialack(tp, th)
5866d90faf3SPaul Saab 	struct tcpcb *tp;
5876d90faf3SPaul Saab 	struct tcphdr *th;
5886d90faf3SPaul Saab {
5898db456bfSPaul Saab 	int num_segs = 1;
5906d90faf3SPaul Saab 
59125e6f9edSPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
5926d90faf3SPaul Saab 	callout_stop(tp->tt_rexmt);
5936d90faf3SPaul Saab 	tp->t_rtttime = 0;
5948db456bfSPaul Saab 	/* send one or 2 segments based on how much new data was acked */
5958db456bfSPaul Saab 	if (((th->th_ack - tp->snd_una) / tp->t_maxseg) > 2)
5968db456bfSPaul Saab 		num_segs = 2;
5970077b016SPaul Saab 	tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
5980077b016SPaul Saab 		(tp->snd_nxt - tp->sack_newdata) +
5990077b016SPaul Saab 		num_segs * tp->t_maxseg);
60025e6f9edSPaul Saab 	if (tp->snd_cwnd > tp->snd_ssthresh)
60125e6f9edSPaul Saab 		tp->snd_cwnd = tp->snd_ssthresh;
6026d90faf3SPaul Saab 	tp->t_flags |= TF_ACKNOW;
6036d90faf3SPaul Saab 	(void) tcp_output(tp);
6046d90faf3SPaul Saab }
6056d90faf3SPaul Saab 
6066d90faf3SPaul Saab /*
6070077b016SPaul Saab  * Debug version of tcp_sack_output() that walks the scoreboard. Used for
6080077b016SPaul Saab  * now to sanity check the hint.
6096d90faf3SPaul Saab  */
6100077b016SPaul Saab static struct sackhole *
6110077b016SPaul Saab tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
6126d90faf3SPaul Saab {
613a6235da6SPaul Saab 	struct sackhole *p;
6146d90faf3SPaul Saab 
6156d90faf3SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
616a55db2b6SPaul Saab 	*sack_bytes_rexmt = 0;
617a6235da6SPaul Saab 	TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
6186d90faf3SPaul Saab 		if (SEQ_LT(p->rxmit, p->end)) {
6196d90faf3SPaul Saab 			if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
6206d90faf3SPaul Saab 				continue;
6216d90faf3SPaul Saab 			}
622a55db2b6SPaul Saab 			*sack_bytes_rexmt += (p->rxmit - p->start);
623a55db2b6SPaul Saab 			break;
624a55db2b6SPaul Saab 		}
625a55db2b6SPaul Saab 		*sack_bytes_rexmt += (p->rxmit - p->start);
626a55db2b6SPaul Saab 	}
6276d90faf3SPaul Saab 	return (p);
6286d90faf3SPaul Saab }
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 {
6500077b016SPaul Saab 	struct sackhole *hole = NULL, *dbg_hole = NULL;
6510077b016SPaul Saab 	int dbg_bytes_rexmt;
6520077b016SPaul Saab 
6530077b016SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
6540077b016SPaul Saab 	dbg_hole = tcp_sack_output_debug(tp, &dbg_bytes_rexmt);
6550077b016SPaul Saab 	*sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
6560077b016SPaul Saab 	hole = tp->sackhint.nexthole;
6570077b016SPaul Saab 	if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
6580077b016SPaul Saab 		goto out;
6590077b016SPaul Saab 	while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
6600077b016SPaul Saab 		if (SEQ_LT(hole->rxmit, hole->end)) {
6610077b016SPaul Saab 			tp->sackhint.nexthole = hole;
6620077b016SPaul Saab 			break;
6630077b016SPaul Saab 		}
6640077b016SPaul Saab 	}
6650077b016SPaul Saab out:
6660077b016SPaul Saab 	if (dbg_hole != hole) {
6670077b016SPaul Saab 		printf("%s: Computed sack hole not the same as cached value\n", __func__);
6680077b016SPaul Saab 		hole = dbg_hole;
6690077b016SPaul Saab 	}
6700077b016SPaul Saab 	if (*sack_bytes_rexmt != dbg_bytes_rexmt) {
6710077b016SPaul Saab 		printf("%s: Computed sack_bytes_retransmitted (%d) not"
6720077b016SPaul Saab 		       "the same as cached value (%d)\n",
6730077b016SPaul Saab 		       __func__, dbg_bytes_rexmt, *sack_bytes_rexmt);
6740077b016SPaul Saab 		*sack_bytes_rexmt = dbg_bytes_rexmt;
6750077b016SPaul Saab 	}
6760077b016SPaul Saab 	return (hole);
6770077b016SPaul Saab }
6780077b016SPaul Saab 
6790077b016SPaul Saab /*
6806d90faf3SPaul Saab  * After a timeout, the SACK list may be rebuilt.  This SACK information
6816d90faf3SPaul Saab  * should be used to avoid retransmitting SACKed data.  This function
6826d90faf3SPaul Saab  * traverses the SACK list to see if snd_nxt should be moved forward.
6836d90faf3SPaul Saab  */
6846d90faf3SPaul Saab void
6856d90faf3SPaul Saab tcp_sack_adjust(struct tcpcb *tp)
6866d90faf3SPaul Saab {
687a6235da6SPaul Saab 	struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
688b7c75571SPaul Saab 
689b7c75571SPaul Saab 	INP_LOCK_ASSERT(tp->t_inpcb);
6906d90faf3SPaul Saab 	if (cur == NULL)
6916d90faf3SPaul Saab 		return; /* No holes */
6926d90faf3SPaul Saab 	if (SEQ_GEQ(tp->snd_nxt, tp->rcv_lastsack))
6936d90faf3SPaul Saab 		return; /* We're already beyond any SACKed blocks */
6946d90faf3SPaul Saab 	/*
6956d90faf3SPaul Saab 	 * Two cases for which we want to advance snd_nxt:
6966d90faf3SPaul Saab 	 * i) snd_nxt lies between end of one hole and beginning of another
6976d90faf3SPaul Saab 	 * ii) snd_nxt lies between end of last hole and rcv_lastsack
6986d90faf3SPaul Saab 	 */
699a6235da6SPaul Saab 	while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
7006d90faf3SPaul Saab 		if (SEQ_LT(tp->snd_nxt, cur->end))
7016d90faf3SPaul Saab 			return;
702a6235da6SPaul Saab 		if (SEQ_GEQ(tp->snd_nxt, p->start))
703a6235da6SPaul Saab 			cur = p;
7046d90faf3SPaul Saab 		else {
705a6235da6SPaul Saab 			tp->snd_nxt = p->start;
7066d90faf3SPaul Saab 			return;
7076d90faf3SPaul Saab 		}
7086d90faf3SPaul Saab 	}
7096d90faf3SPaul Saab 	if (SEQ_LT(tp->snd_nxt, cur->end))
7106d90faf3SPaul Saab 		return;
7116d90faf3SPaul Saab 	tp->snd_nxt = tp->rcv_lastsack;
7126d90faf3SPaul Saab 	return;
7136d90faf3SPaul Saab }
714