xref: /freebsd/sys/netinet/tcp_sack.c (revision 7dc78150c730e90fa2afdaba3aa645932b30c429)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
46d90faf3SPaul Saab  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
5632bbf0fSRobert Watson  *	The Regents of the University of California.
6632bbf0fSRobert Watson  * All rights reserved.
76d90faf3SPaul Saab  *
86d90faf3SPaul Saab  * Redistribution and use in source and binary forms, with or without
96d90faf3SPaul Saab  * modification, are permitted provided that the following conditions
106d90faf3SPaul Saab  * are met:
116d90faf3SPaul Saab  * 1. Redistributions of source code must retain the above copyright
126d90faf3SPaul Saab  *    notice, this list of conditions and the following disclaimer.
136d90faf3SPaul Saab  * 2. Redistributions in binary form must reproduce the above copyright
146d90faf3SPaul Saab  *    notice, this list of conditions and the following disclaimer in the
156d90faf3SPaul Saab  *    documentation and/or other materials provided with the distribution.
1651369649SPedro F. Giffuni  * 3. Neither the name of the University nor the names of its contributors
176d90faf3SPaul Saab  *    may be used to endorse or promote products derived from this software
186d90faf3SPaul Saab  *    without specific prior written permission.
196d90faf3SPaul Saab  *
206d90faf3SPaul Saab  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
216d90faf3SPaul Saab  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
226d90faf3SPaul Saab  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236d90faf3SPaul Saab  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
246d90faf3SPaul Saab  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
256d90faf3SPaul Saab  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
266d90faf3SPaul Saab  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
276d90faf3SPaul Saab  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
286d90faf3SPaul Saab  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
296d90faf3SPaul Saab  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
306d90faf3SPaul Saab  * SUCH DAMAGE.
316d90faf3SPaul Saab  */
326d90faf3SPaul Saab 
33c398230bSWarner Losh /*-
346d90faf3SPaul Saab  *
356d90faf3SPaul Saab  * NRL grants permission for redistribution and use in source and binary
366d90faf3SPaul Saab  * forms, with or without modification, of the software and documentation
376d90faf3SPaul Saab  * created at NRL provided that the following conditions are met:
386d90faf3SPaul Saab  *
396d90faf3SPaul Saab  * 1. Redistributions of source code must retain the above copyright
406d90faf3SPaul Saab  *    notice, this list of conditions and the following disclaimer.
416d90faf3SPaul Saab  * 2. Redistributions in binary form must reproduce the above copyright
426d90faf3SPaul Saab  *    notice, this list of conditions and the following disclaimer in the
436d90faf3SPaul Saab  *    documentation and/or other materials provided with the distribution.
446d90faf3SPaul Saab  * 3. All advertising materials mentioning features or use of this software
456d90faf3SPaul Saab  *    must display the following acknowledgements:
466d90faf3SPaul Saab  *	This product includes software developed by the University of
476d90faf3SPaul Saab  *	California, Berkeley and its contributors.
486d90faf3SPaul Saab  *	This product includes software developed at the Information
496d90faf3SPaul Saab  *	Technology Division, US Naval Research Laboratory.
506d90faf3SPaul Saab  * 4. Neither the name of the NRL nor the names of its contributors
516d90faf3SPaul Saab  *    may be used to endorse or promote products derived from this software
526d90faf3SPaul Saab  *    without specific prior written permission.
536d90faf3SPaul Saab  *
546d90faf3SPaul Saab  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
556d90faf3SPaul Saab  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
566d90faf3SPaul Saab  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
576d90faf3SPaul Saab  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
586d90faf3SPaul Saab  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
596d90faf3SPaul Saab  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
606d90faf3SPaul Saab  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
616d90faf3SPaul Saab  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
626d90faf3SPaul Saab  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
636d90faf3SPaul Saab  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
646d90faf3SPaul Saab  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
656d90faf3SPaul Saab  *
666d90faf3SPaul Saab  * The views and conclusions contained in the software and documentation
676d90faf3SPaul Saab  * are those of the authors and should not be interpreted as representing
686d90faf3SPaul Saab  * official policies, either expressed or implied, of the US Naval
696d90faf3SPaul Saab  * Research Laboratory (NRL).
706d90faf3SPaul Saab  */
71632bbf0fSRobert Watson 
72632bbf0fSRobert Watson #include <sys/cdefs.h>
736d90faf3SPaul Saab #include "opt_inet.h"
746d90faf3SPaul Saab #include "opt_inet6.h"
756d90faf3SPaul Saab 
766d90faf3SPaul Saab #include <sys/param.h>
776d90faf3SPaul Saab #include <sys/systm.h>
786d90faf3SPaul Saab #include <sys/kernel.h>
796d90faf3SPaul Saab #include <sys/sysctl.h>
806d90faf3SPaul Saab #include <sys/malloc.h>
816d90faf3SPaul Saab #include <sys/mbuf.h>
826d90faf3SPaul Saab #include <sys/proc.h>		/* for proc0 declaration */
836d90faf3SPaul Saab #include <sys/protosw.h>
846d90faf3SPaul Saab #include <sys/socket.h>
856d90faf3SPaul Saab #include <sys/socketvar.h>
866d90faf3SPaul Saab #include <sys/syslog.h>
876d90faf3SPaul Saab #include <sys/systm.h>
886d90faf3SPaul Saab 
896d90faf3SPaul Saab #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
906d90faf3SPaul Saab 
916d90faf3SPaul Saab #include <vm/uma.h>
926d90faf3SPaul Saab 
936d90faf3SPaul Saab #include <net/if.h>
9476039bc8SGleb Smirnoff #include <net/if_var.h>
956d90faf3SPaul Saab #include <net/route.h>
96530c0060SRobert Watson #include <net/vnet.h>
976d90faf3SPaul Saab 
986d90faf3SPaul Saab #include <netinet/in.h>
996d90faf3SPaul Saab #include <netinet/in_systm.h>
1006d90faf3SPaul Saab #include <netinet/ip.h>
1016d90faf3SPaul Saab #include <netinet/in_var.h>
1026d90faf3SPaul Saab #include <netinet/in_pcb.h>
1036d90faf3SPaul Saab #include <netinet/ip_var.h>
1046d90faf3SPaul Saab #include <netinet/ip6.h>
1056d90faf3SPaul Saab #include <netinet/icmp6.h>
1066d90faf3SPaul Saab #include <netinet6/nd6.h>
1076d90faf3SPaul Saab #include <netinet6/ip6_var.h>
1086d90faf3SPaul Saab #include <netinet6/in6_pcb.h>
1096d90faf3SPaul Saab #include <netinet/tcp.h>
1106d90faf3SPaul Saab #include <netinet/tcp_fsm.h>
1116d90faf3SPaul Saab #include <netinet/tcp_seq.h>
1126d90faf3SPaul Saab #include <netinet/tcp_timer.h>
1136d90faf3SPaul Saab #include <netinet/tcp_var.h>
1146d90faf3SPaul Saab #include <netinet/tcpip.h>
1150471a8c7SRichard Scheffenegger #include <netinet/cc/cc.h>
1166d90faf3SPaul Saab 
1176d90faf3SPaul Saab #include <machine/in_cksum.h>
1186d90faf3SPaul Saab 
119eddfbb76SRobert Watson VNET_DECLARE(struct uma_zone *, sack_hole_zone);
1201e77c105SRobert Watson #define	V_sack_hole_zone		VNET(sack_hole_zone)
12144e33a07SMarko Zec 
1227029da5cSPawel Biernacki SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1237029da5cSPawel Biernacki     "TCP SACK");
124d1de2b05SRichard Scheffenegger 
12582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_sack) = 1;
1266df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
127d1de2b05SRichard Scheffenegger     &VNET_NAME(tcp_do_sack), 0,
128d1de2b05SRichard Scheffenegger     "Enable/Disable TCP SACK support");
129d1de2b05SRichard Scheffenegger 
130d1de2b05SRichard Scheffenegger VNET_DEFINE(int, tcp_do_newsack) = 1;
131d1de2b05SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, revised, CTLFLAG_VNET | CTLFLAG_RW,
132d1de2b05SRichard Scheffenegger     &VNET_NAME(tcp_do_newsack), 0,
133d1de2b05SRichard Scheffenegger     "Use revised SACK loss recovery per RFC 6675");
134a55db2b6SPaul Saab 
135f42518ffSRichard Scheffenegger VNET_DEFINE(int, tcp_do_lrd) = 1;
136f42518ffSRichard Scheffenegger SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, lrd, CTLFLAG_VNET | CTLFLAG_RW,
137f42518ffSRichard Scheffenegger     &VNET_NAME(tcp_do_lrd), 1,
138f42518ffSRichard Scheffenegger     "Perform Lost Retransmission Detection");
139f42518ffSRichard Scheffenegger 
140dcdfe449SRichard Scheffenegger VNET_DEFINE(int, tcp_sack_tso) = 0;
141dcdfe449SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, tso, CTLFLAG_VNET | CTLFLAG_RW,
142dcdfe449SRichard Scheffenegger     &VNET_NAME(tcp_sack_tso), 0,
143dcdfe449SRichard Scheffenegger     "Allow TSO during SACK loss recovery");
144dcdfe449SRichard Scheffenegger 
14582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_sack_maxholes) = 128;
1466df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_VNET | CTLFLAG_RW,
147eddfbb76SRobert Watson     &VNET_NAME(tcp_sack_maxholes), 0,
148e891d82bSPaul Saab     "Maximum number of TCP SACK holes allowed per connection");
149e891d82bSPaul Saab 
15082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_sack_globalmaxholes) = 65536;
1516df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_VNET | CTLFLAG_RW,
152eddfbb76SRobert Watson     &VNET_NAME(tcp_sack_globalmaxholes), 0,
153e891d82bSPaul Saab     "Global maximum number of TCP SACK holes");
154e891d82bSPaul Saab 
15582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_sack_globalholes) = 0;
1566df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_VNET | CTLFLAG_RD,
157eddfbb76SRobert Watson     &VNET_NAME(tcp_sack_globalholes), 0,
158e891d82bSPaul Saab     "Global number of TCP SACK holes currently allocated");
159b7c75571SPaul Saab 
1605d8fd932SRandall Stewart int
tcp_dsack_block_exists(struct tcpcb * tp)1615d8fd932SRandall Stewart tcp_dsack_block_exists(struct tcpcb *tp)
1625d8fd932SRandall Stewart {
1635d8fd932SRandall Stewart 	/* Return true if a DSACK block exists */
1645d8fd932SRandall Stewart 	if (tp->rcv_numsacks == 0)
1655d8fd932SRandall Stewart 		return (0);
1665d8fd932SRandall Stewart 	if (SEQ_LEQ(tp->sackblks[0].end, tp->rcv_nxt))
1675d8fd932SRandall Stewart 		return(1);
1685d8fd932SRandall Stewart 	return (0);
1695d8fd932SRandall Stewart }
1705d8fd932SRandall Stewart 
171fe5dee73SMichael Tuexen /*
172fe5dee73SMichael Tuexen  * This function will find overlaps with the currently stored sackblocks
173fe5dee73SMichael Tuexen  * and add any overlap as a dsack block upfront
174fe5dee73SMichael Tuexen  */
175fe5dee73SMichael Tuexen void
tcp_update_dsack_list(struct tcpcb * tp,tcp_seq rcv_start,tcp_seq rcv_end)176fe5dee73SMichael Tuexen tcp_update_dsack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
177fe5dee73SMichael Tuexen {
178fe5dee73SMichael Tuexen 	struct sackblk head_blk,mid_blk,saved_blks[MAX_SACK_BLKS];
179fe5dee73SMichael Tuexen 	int i, j, n, identical;
180fe5dee73SMichael Tuexen 	tcp_seq start, end;
181fe5dee73SMichael Tuexen 
1829eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
183fe5dee73SMichael Tuexen 
184fe5dee73SMichael Tuexen 	KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end"));
185fe5dee73SMichael Tuexen 
186fe5dee73SMichael Tuexen 	if (SEQ_LT(rcv_end, tp->rcv_nxt) ||
187fe5dee73SMichael Tuexen 	    ((rcv_end == tp->rcv_nxt) &&
188fe5dee73SMichael Tuexen 	     (tp->rcv_numsacks > 0 ) &&
189fe5dee73SMichael Tuexen 	     (tp->sackblks[0].end == tp->rcv_nxt))) {
190fe5dee73SMichael Tuexen 		saved_blks[0].start = rcv_start;
191fe5dee73SMichael Tuexen 		saved_blks[0].end = rcv_end;
192fe5dee73SMichael Tuexen 	} else {
193fe5dee73SMichael Tuexen 		saved_blks[0].start = saved_blks[0].end = 0;
194fe5dee73SMichael Tuexen 	}
195fe5dee73SMichael Tuexen 
196fe5dee73SMichael Tuexen 	head_blk.start = head_blk.end = 0;
197fe5dee73SMichael Tuexen 	mid_blk.start = rcv_start;
198fe5dee73SMichael Tuexen 	mid_blk.end = rcv_end;
199fe5dee73SMichael Tuexen 	identical = 0;
200fe5dee73SMichael Tuexen 
201fe5dee73SMichael Tuexen 	for (i = 0; i < tp->rcv_numsacks; i++) {
202fe5dee73SMichael Tuexen 		start = tp->sackblks[i].start;
203fe5dee73SMichael Tuexen 		end = tp->sackblks[i].end;
204fe5dee73SMichael Tuexen 		if (SEQ_LT(rcv_end, start)) {
205fe5dee73SMichael Tuexen 			/* pkt left to sack blk */
206fe5dee73SMichael Tuexen 			continue;
207fe5dee73SMichael Tuexen 		}
208fe5dee73SMichael Tuexen 		if (SEQ_GT(rcv_start, end)) {
209fe5dee73SMichael Tuexen 			/* pkt right to sack blk */
210fe5dee73SMichael Tuexen 			continue;
211fe5dee73SMichael Tuexen 		}
212fe5dee73SMichael Tuexen 		if (SEQ_GT(tp->rcv_nxt, end)) {
213fe5dee73SMichael Tuexen 			if ((SEQ_MAX(rcv_start, start) != SEQ_MIN(rcv_end, end)) &&
214fe5dee73SMichael Tuexen 			    (SEQ_GT(head_blk.start, SEQ_MAX(rcv_start, start)) ||
215fe5dee73SMichael Tuexen 			    (head_blk.start == head_blk.end))) {
216fe5dee73SMichael Tuexen 				head_blk.start = SEQ_MAX(rcv_start, start);
217fe5dee73SMichael Tuexen 				head_blk.end = SEQ_MIN(rcv_end, end);
218fe5dee73SMichael Tuexen 			}
219fe5dee73SMichael Tuexen 			continue;
220fe5dee73SMichael Tuexen 		}
221fe5dee73SMichael Tuexen 		if (((head_blk.start == head_blk.end) ||
222fe5dee73SMichael Tuexen 		     SEQ_LT(start, head_blk.start)) &&
223fe5dee73SMichael Tuexen 		     (SEQ_GT(end, rcv_start) &&
224fe5dee73SMichael Tuexen 		      SEQ_LEQ(start, rcv_end))) {
225fe5dee73SMichael Tuexen 			head_blk.start = start;
226fe5dee73SMichael Tuexen 			head_blk.end = end;
227fe5dee73SMichael Tuexen 		}
228fe5dee73SMichael Tuexen 		mid_blk.start = SEQ_MIN(mid_blk.start, start);
229fe5dee73SMichael Tuexen 		mid_blk.end = SEQ_MAX(mid_blk.end, end);
230fe5dee73SMichael Tuexen 		if ((mid_blk.start == start) &&
231fe5dee73SMichael Tuexen 		    (mid_blk.end == end))
232fe5dee73SMichael Tuexen 			identical = 1;
233fe5dee73SMichael Tuexen 	}
234fe5dee73SMichael Tuexen 	if (SEQ_LT(head_blk.start, head_blk.end)) {
235fe5dee73SMichael Tuexen 		/* store overlapping range */
236fe5dee73SMichael Tuexen 		saved_blks[0].start = SEQ_MAX(rcv_start, head_blk.start);
237fe5dee73SMichael Tuexen 		saved_blks[0].end   = SEQ_MIN(rcv_end, head_blk.end);
238fe5dee73SMichael Tuexen 	}
239fe5dee73SMichael Tuexen 	n = 1;
240fe5dee73SMichael Tuexen 	/*
241fe5dee73SMichael Tuexen 	 * Second, if not ACKed, store the SACK block that
242fe5dee73SMichael Tuexen 	 * overlaps with the DSACK block unless it is identical
243fe5dee73SMichael Tuexen 	 */
244fe5dee73SMichael Tuexen 	if ((SEQ_LT(tp->rcv_nxt, mid_blk.end) &&
245fe5dee73SMichael Tuexen 	    !((mid_blk.start == saved_blks[0].start) &&
246fe5dee73SMichael Tuexen 	    (mid_blk.end == saved_blks[0].end))) ||
247fe5dee73SMichael Tuexen 	    identical == 1) {
248fe5dee73SMichael Tuexen 		saved_blks[n].start = mid_blk.start;
249fe5dee73SMichael Tuexen 		saved_blks[n++].end = mid_blk.end;
250fe5dee73SMichael Tuexen 	}
2515b66b7f1SMichael Tuexen 	for (j = 0; (j < tp->rcv_numsacks) && (n < MAX_SACK_BLKS); j++) {
252fe5dee73SMichael Tuexen 		if (((SEQ_LT(tp->sackblks[j].end, mid_blk.start) ||
253fe5dee73SMichael Tuexen 		      SEQ_GT(tp->sackblks[j].start, mid_blk.end)) &&
254fe5dee73SMichael Tuexen 		    (SEQ_GT(tp->sackblks[j].start, tp->rcv_nxt))))
255fe5dee73SMichael Tuexen 		saved_blks[n++] = tp->sackblks[j];
256fe5dee73SMichael Tuexen 	}
257fe5dee73SMichael Tuexen 	j = 0;
258fe5dee73SMichael Tuexen 	for (i = 0; i < n; i++) {
2595aa0576bSEd Maste 		/* we can end up with a stale initial entry */
260fe5dee73SMichael Tuexen 		if (SEQ_LT(saved_blks[i].start, saved_blks[i].end)) {
261fe5dee73SMichael Tuexen 			tp->sackblks[j++] = saved_blks[i];
262fe5dee73SMichael Tuexen 		}
263fe5dee73SMichael Tuexen 	}
264fe5dee73SMichael Tuexen 	tp->rcv_numsacks = j;
265fe5dee73SMichael Tuexen }
266fe5dee73SMichael Tuexen 
2676d90faf3SPaul Saab /*
268632bbf0fSRobert Watson  * This function is called upon receipt of new valid data (while not in
269632bbf0fSRobert Watson  * header prediction mode), and it updates the ordered list of sacks.
2706d90faf3SPaul Saab  */
2716d90faf3SPaul Saab void
tcp_update_sack_list(struct tcpcb * tp,tcp_seq rcv_start,tcp_seq rcv_end)272b7c75571SPaul Saab tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
2736d90faf3SPaul Saab {
2746d90faf3SPaul Saab 	/*
2756d90faf3SPaul Saab 	 * First reported block MUST be the most recent one.  Subsequent
2766d90faf3SPaul Saab 	 * blocks SHOULD be in the order in which they arrived at the
2776d90faf3SPaul Saab 	 * receiver.  These two conditions make the implementation fully
2786d90faf3SPaul Saab 	 * compliant with RFC 2018.
2796d90faf3SPaul Saab 	 */
280b7c75571SPaul Saab 	struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
281b7c75571SPaul Saab 	int num_head, num_saved, i;
2826d90faf3SPaul Saab 
2839eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
284b7c75571SPaul Saab 
285632bbf0fSRobert Watson 	/* Check arguments. */
2865acfd95cSMichael Tuexen 	KASSERT(SEQ_LEQ(rcv_start, rcv_end), ("rcv_start <= rcv_end"));
287b7c75571SPaul Saab 
288fe5dee73SMichael Tuexen 	if ((rcv_start == rcv_end) &&
289fe5dee73SMichael Tuexen 	    (tp->rcv_numsacks >= 1) &&
290fe5dee73SMichael Tuexen 	    (rcv_end == tp->sackblks[0].end)) {
291fe5dee73SMichael Tuexen 		/* retaining DSACK block below rcv_nxt (todrop) */
292fe5dee73SMichael Tuexen 		head_blk = tp->sackblks[0];
293fe5dee73SMichael Tuexen 	} else {
294b7c75571SPaul Saab 		/* SACK block for the received segment. */
295b7c75571SPaul Saab 		head_blk.start = rcv_start;
296b7c75571SPaul Saab 		head_blk.end = rcv_end;
297fe5dee73SMichael Tuexen 	}
298b7c75571SPaul Saab 
299b7c75571SPaul Saab 	/*
300632bbf0fSRobert Watson 	 * Merge updated SACK blocks into head_blk, and save unchanged SACK
301632bbf0fSRobert Watson 	 * blocks into saved_blks[].  num_saved will have the number of the
302632bbf0fSRobert Watson 	 * saved SACK blocks.
303b7c75571SPaul Saab 	 */
304b7c75571SPaul Saab 	num_saved = 0;
3056d90faf3SPaul Saab 	for (i = 0; i < tp->rcv_numsacks; i++) {
306b7c75571SPaul Saab 		tcp_seq start = tp->sackblks[i].start;
307b7c75571SPaul Saab 		tcp_seq end = tp->sackblks[i].end;
308b7c75571SPaul Saab 		if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
309b7c75571SPaul Saab 			/*
310b7c75571SPaul Saab 			 * Discard this SACK block.
311b7c75571SPaul Saab 			 */
312b7c75571SPaul Saab 		} else if (SEQ_LEQ(head_blk.start, end) &&
313b7c75571SPaul Saab 			   SEQ_GEQ(head_blk.end, start)) {
314b7c75571SPaul Saab 			/*
315632bbf0fSRobert Watson 			 * Merge this SACK block into head_blk.  This SACK
316632bbf0fSRobert Watson 			 * block itself will be discarded.
317b7c75571SPaul Saab 			 */
3185acfd95cSMichael Tuexen 			/*
3195acfd95cSMichael Tuexen 			 * |-|
3205acfd95cSMichael Tuexen 			 *   |---|  merge
3215acfd95cSMichael Tuexen 			 *
3225acfd95cSMichael Tuexen 			 *     |-|
3235acfd95cSMichael Tuexen 			 * |---|    merge
3245acfd95cSMichael Tuexen 			 *
3255acfd95cSMichael Tuexen 			 * |-----|
3265acfd95cSMichael Tuexen 			 *   |-|    DSACK smaller
3275acfd95cSMichael Tuexen 			 *
3285acfd95cSMichael Tuexen 			 *   |-|
3295acfd95cSMichael Tuexen 			 * |-----|  DSACK smaller
3305acfd95cSMichael Tuexen 			 */
3315acfd95cSMichael Tuexen 			if (head_blk.start == end)
332b7c75571SPaul Saab 				head_blk.start = start;
3335acfd95cSMichael Tuexen 			else if (head_blk.end == start)
334b7c75571SPaul Saab 				head_blk.end = end;
3355acfd95cSMichael Tuexen 			else {
3365acfd95cSMichael Tuexen 				if (SEQ_LT(head_blk.start, start)) {
3375acfd95cSMichael Tuexen 					tcp_seq temp = start;
3385acfd95cSMichael Tuexen 					start = head_blk.start;
3395acfd95cSMichael Tuexen 					head_blk.start = temp;
3405acfd95cSMichael Tuexen 				}
3415acfd95cSMichael Tuexen 				if (SEQ_GT(head_blk.end, end)) {
3425acfd95cSMichael Tuexen 					tcp_seq temp = end;
3435acfd95cSMichael Tuexen 					end = head_blk.end;
3445acfd95cSMichael Tuexen 					head_blk.end = temp;
3455acfd95cSMichael Tuexen 				}
3465acfd95cSMichael Tuexen 				if ((head_blk.start != start) ||
3475acfd95cSMichael Tuexen 				    (head_blk.end != end)) {
3485acfd95cSMichael Tuexen 					if ((num_saved >= 1) &&
3495acfd95cSMichael Tuexen 					   SEQ_GEQ(saved_blks[num_saved-1].start, start) &&
3505acfd95cSMichael Tuexen 					   SEQ_LEQ(saved_blks[num_saved-1].end, end))
3515acfd95cSMichael Tuexen 						num_saved--;
3525acfd95cSMichael Tuexen 					saved_blks[num_saved].start = start;
3535acfd95cSMichael Tuexen 					saved_blks[num_saved].end = end;
3545acfd95cSMichael Tuexen 					num_saved++;
3555acfd95cSMichael Tuexen 				}
3565acfd95cSMichael Tuexen 			}
3576d90faf3SPaul Saab 		} else {
3586d90faf3SPaul Saab 			/*
3595acfd95cSMichael Tuexen 			 * This block supercedes the prior block
3605acfd95cSMichael Tuexen 			 */
3615acfd95cSMichael Tuexen 			if ((num_saved >= 1) &&
3625acfd95cSMichael Tuexen 			   SEQ_GEQ(saved_blks[num_saved-1].start, start) &&
3635acfd95cSMichael Tuexen 			   SEQ_LEQ(saved_blks[num_saved-1].end, end))
3645acfd95cSMichael Tuexen 				num_saved--;
3655acfd95cSMichael Tuexen 			/*
366b7c75571SPaul Saab 			 * Save this SACK block.
3676d90faf3SPaul Saab 			 */
368b7c75571SPaul Saab 			saved_blks[num_saved].start = start;
369b7c75571SPaul Saab 			saved_blks[num_saved].end = end;
370b7c75571SPaul Saab 			num_saved++;
371b7c75571SPaul Saab 		}
372b7c75571SPaul Saab 	}
373b7c75571SPaul Saab 
3746d90faf3SPaul Saab 	/*
375b7c75571SPaul Saab 	 * Update SACK list in tp->sackblks[].
3766d90faf3SPaul Saab 	 */
377b7c75571SPaul Saab 	num_head = 0;
3785acfd95cSMichael Tuexen 	if (SEQ_LT(rcv_start, rcv_end)) {
379b7c75571SPaul Saab 		/*
380632bbf0fSRobert Watson 		 * The received data segment is an out-of-order segment.  Put
381632bbf0fSRobert Watson 		 * head_blk at the top of SACK list.
382b7c75571SPaul Saab 		 */
383b7c75571SPaul Saab 		tp->sackblks[0] = head_blk;
384b7c75571SPaul Saab 		num_head = 1;
385b7c75571SPaul Saab 		/*
386b7c75571SPaul Saab 		 * If the number of saved SACK blocks exceeds its limit,
387b7c75571SPaul Saab 		 * discard the last SACK block.
388b7c75571SPaul Saab 		 */
389b7c75571SPaul Saab 		if (num_saved >= MAX_SACK_BLKS)
390b7c75571SPaul Saab 			num_saved--;
3916d90faf3SPaul Saab 	}
392fe5dee73SMichael Tuexen 	if ((rcv_start == rcv_end) &&
393fe5dee73SMichael Tuexen 	    (rcv_start == tp->sackblks[0].end)) {
394fe5dee73SMichael Tuexen 		num_head = 1;
395fe5dee73SMichael Tuexen 	}
396b7c75571SPaul Saab 	if (num_saved > 0) {
397b7c75571SPaul Saab 		/*
398b7c75571SPaul Saab 		 * Copy the saved SACK blocks back.
399b7c75571SPaul Saab 		 */
400b7c75571SPaul Saab 		bcopy(saved_blks, &tp->sackblks[num_head],
401b7c75571SPaul Saab 		      sizeof(struct sackblk) * num_saved);
4026d90faf3SPaul Saab 	}
403b7c75571SPaul Saab 
404b7c75571SPaul Saab 	/* Save the number of SACK blocks. */
405b7c75571SPaul Saab 	tp->rcv_numsacks = num_head + num_saved;
4066d90faf3SPaul Saab }
4076d90faf3SPaul Saab 
408e5926fd3SRandall Stewart void
tcp_clean_dsack_blocks(struct tcpcb * tp)409e5926fd3SRandall Stewart tcp_clean_dsack_blocks(struct tcpcb *tp)
410e5926fd3SRandall Stewart {
411e5926fd3SRandall Stewart 	struct sackblk saved_blks[MAX_SACK_BLKS];
412e5926fd3SRandall Stewart 	int num_saved, i;
413e5926fd3SRandall Stewart 
4149eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
415e5926fd3SRandall Stewart 	/*
416e5926fd3SRandall Stewart 	 * Clean up any DSACK blocks that
417e5926fd3SRandall Stewart 	 * are in our queue of sack blocks.
418e5926fd3SRandall Stewart 	 *
419e5926fd3SRandall Stewart 	 */
420e5926fd3SRandall Stewart 	num_saved = 0;
421e5926fd3SRandall Stewart 	for (i = 0; i < tp->rcv_numsacks; i++) {
422e5926fd3SRandall Stewart 		tcp_seq start = tp->sackblks[i].start;
423e5926fd3SRandall Stewart 		tcp_seq end = tp->sackblks[i].end;
424e5926fd3SRandall Stewart 		if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
425e5926fd3SRandall Stewart 			/*
426e5926fd3SRandall Stewart 			 * Discard this D-SACK block.
427e5926fd3SRandall Stewart 			 */
428e5926fd3SRandall Stewart 			continue;
429e5926fd3SRandall Stewart 		}
430e5926fd3SRandall Stewart 		/*
431e5926fd3SRandall Stewart 		 * Save this SACK block.
432e5926fd3SRandall Stewart 		 */
433e5926fd3SRandall Stewart 		saved_blks[num_saved].start = start;
434e5926fd3SRandall Stewart 		saved_blks[num_saved].end = end;
435e5926fd3SRandall Stewart 		num_saved++;
436e5926fd3SRandall Stewart 	}
437e5926fd3SRandall Stewart 	if (num_saved > 0) {
438e5926fd3SRandall Stewart 		/*
439e5926fd3SRandall Stewart 		 * Copy the saved SACK blocks back.
440e5926fd3SRandall Stewart 		 */
441e5926fd3SRandall Stewart 		bcopy(saved_blks, &tp->sackblks[0],
442e5926fd3SRandall Stewart 		      sizeof(struct sackblk) * num_saved);
443e5926fd3SRandall Stewart 	}
444e5926fd3SRandall Stewart 	tp->rcv_numsacks = num_saved;
445e5926fd3SRandall Stewart }
446e5926fd3SRandall Stewart 
4476d90faf3SPaul Saab /*
4486d90faf3SPaul Saab  * Delete all receiver-side SACK information.
4496d90faf3SPaul Saab  */
4506d90faf3SPaul Saab void
tcp_clean_sackreport(struct tcpcb * tp)451f2565d68SRobert Watson tcp_clean_sackreport(struct tcpcb *tp)
4526d90faf3SPaul Saab {
4536d90faf3SPaul Saab 	int i;
4546d90faf3SPaul Saab 
4559eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
4566d90faf3SPaul Saab 	tp->rcv_numsacks = 0;
4576d90faf3SPaul Saab 	for (i = 0; i < MAX_SACK_BLKS; i++)
4586d90faf3SPaul Saab 		tp->sackblks[i].start = tp->sackblks[i].end=0;
4596d90faf3SPaul Saab }
4606d90faf3SPaul Saab 
4616d90faf3SPaul Saab /*
4624fc53245SPaul Saab  * Allocate struct sackhole.
4634fc53245SPaul Saab  */
4644fc53245SPaul Saab static struct sackhole *
tcp_sackhole_alloc(struct tcpcb * tp,tcp_seq start,tcp_seq end)4654fc53245SPaul Saab tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
4664fc53245SPaul Saab {
4674fc53245SPaul Saab 	struct sackhole *hole;
4684fc53245SPaul Saab 
469603724d3SBjoern A. Zeeb 	if (tp->snd_numholes >= V_tcp_sack_maxholes ||
470603724d3SBjoern A. Zeeb 	    V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) {
47178b50714SRobert Watson 		TCPSTAT_INC(tcps_sack_sboverflow);
4724fc53245SPaul Saab 		return NULL;
4734fc53245SPaul Saab 	}
4744fc53245SPaul Saab 
4751ed81b73SMarko Zec 	hole = (struct sackhole *)uma_zalloc(V_sack_hole_zone, M_NOWAIT);
4764fc53245SPaul Saab 	if (hole == NULL)
4774fc53245SPaul Saab 		return NULL;
4784fc53245SPaul Saab 
4794fc53245SPaul Saab 	hole->start = start;
4804fc53245SPaul Saab 	hole->end = end;
4814fc53245SPaul Saab 	hole->rxmit = start;
4824fc53245SPaul Saab 
4834fc53245SPaul Saab 	tp->snd_numholes++;
48491a5ebdeSLawrence Stewart 	atomic_add_int(&V_tcp_sack_globalholes, 1);
4854fc53245SPaul Saab 
4864fc53245SPaul Saab 	return hole;
4874fc53245SPaul Saab }
4884fc53245SPaul Saab 
4894fc53245SPaul Saab /*
4904fc53245SPaul Saab  * Free struct sackhole.
4914fc53245SPaul Saab  */
4924fc53245SPaul Saab static void
tcp_sackhole_free(struct tcpcb * tp,struct sackhole * hole)4934fc53245SPaul Saab tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
4944fc53245SPaul Saab {
495632bbf0fSRobert Watson 
4961ed81b73SMarko Zec 	uma_zfree(V_sack_hole_zone, hole);
4974fc53245SPaul Saab 
4984fc53245SPaul Saab 	tp->snd_numholes--;
49991a5ebdeSLawrence Stewart 	atomic_subtract_int(&V_tcp_sack_globalholes, 1);
5004fc53245SPaul Saab 
5014fc53245SPaul Saab 	KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
502603724d3SBjoern A. Zeeb 	KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
5034fc53245SPaul Saab }
5044fc53245SPaul Saab 
5054fc53245SPaul Saab /*
5069d17a7a6SPaul Saab  * Insert new SACK hole into scoreboard.
5079d17a7a6SPaul Saab  */
5089d17a7a6SPaul Saab static struct sackhole *
tcp_sackhole_insert(struct tcpcb * tp,tcp_seq start,tcp_seq end,struct sackhole * after)5099d17a7a6SPaul Saab tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
5109d17a7a6SPaul Saab     struct sackhole *after)
5119d17a7a6SPaul Saab {
512a649f1f6SRichard Scheffenegger 	struct sackhole *hole;
5139d17a7a6SPaul Saab 
5149d17a7a6SPaul Saab 	/* Allocate a new SACK hole. */
5159d17a7a6SPaul Saab 	hole = tcp_sackhole_alloc(tp, start, end);
5169d17a7a6SPaul Saab 	if (hole == NULL)
5179d17a7a6SPaul Saab 		return NULL;
5189d17a7a6SPaul Saab 
519632bbf0fSRobert Watson 	/* Insert the new SACK hole into scoreboard. */
5209d17a7a6SPaul Saab 	if (after != NULL)
5219d17a7a6SPaul Saab 		TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
5229d17a7a6SPaul Saab 	else
5239d17a7a6SPaul Saab 		TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
5249d17a7a6SPaul Saab 
5259d17a7a6SPaul Saab 	/* Update SACK hint. */
5269d17a7a6SPaul Saab 	if (tp->sackhint.nexthole == NULL)
5279d17a7a6SPaul Saab 		tp->sackhint.nexthole = hole;
5289d17a7a6SPaul Saab 
5299d17a7a6SPaul Saab 	return hole;
5309d17a7a6SPaul Saab }
5319d17a7a6SPaul Saab 
5329d17a7a6SPaul Saab /*
5339d17a7a6SPaul Saab  * Remove SACK hole from scoreboard.
5349d17a7a6SPaul Saab  */
5359d17a7a6SPaul Saab static void
tcp_sackhole_remove(struct tcpcb * tp,struct sackhole * hole)5369d17a7a6SPaul Saab tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
5379d17a7a6SPaul Saab {
538632bbf0fSRobert Watson 
5399d17a7a6SPaul Saab 	/* Update SACK hint. */
5409d17a7a6SPaul Saab 	if (tp->sackhint.nexthole == hole)
5419d17a7a6SPaul Saab 		tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
5429d17a7a6SPaul Saab 
5439d17a7a6SPaul Saab 	/* Remove this SACK hole. */
5449d17a7a6SPaul Saab 	TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
5459d17a7a6SPaul Saab 
5469d17a7a6SPaul Saab 	/* Free this SACK hole. */
5479d17a7a6SPaul Saab 	tcp_sackhole_free(tp, hole);
5489d17a7a6SPaul Saab }
5499d17a7a6SPaul Saab 
5509d17a7a6SPaul Saab /*
5515a53ca16SPaul Saab  * Process cumulative ACK and the TCP SACK option to update the scoreboard.
5525a53ca16SPaul Saab  * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
5535a53ca16SPaul Saab  * the sequence space).
55449a6fbe3SRichard Scheffenegger  * Returns SACK_NEWLOSS if incoming ACK indicates ongoing loss (hole split, new hole),
55549a6fbe3SRichard Scheffenegger  * SACK_CHANGE if incoming ACK has previously unknown SACK information,
55649a6fbe3SRichard Scheffenegger  * SACK_NOCHANGE otherwise.
5576d90faf3SPaul Saab  */
55849a6fbe3SRichard Scheffenegger sackstatus_t
tcp_sack_doack(struct tcpcb * tp,struct tcpopt * to,tcp_seq th_ack)5595a53ca16SPaul Saab tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack)
5606d90faf3SPaul Saab {
561a6235da6SPaul Saab 	struct sackhole *cur, *temp;
5625a53ca16SPaul Saab 	struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp;
56349a6fbe3SRichard Scheffenegger 	int i, j, num_sack_blks;
56449a6fbe3SRichard Scheffenegger 	sackstatus_t sack_changed;
565f359d6ebSRichard Scheffenegger 	int delivered_data, left_edge_delta;
566cbf3575aSRichard Scheffenegger 	int maxseg = tp->t_maxseg - MAX_TCPOPTLEN;
5676d90faf3SPaul Saab 
568e2c6a6d2SRichard Scheffenegger 	tcp_seq loss_hiack = 0;
569e2c6a6d2SRichard Scheffenegger 	int loss_thresh = 0;
570e2c6a6d2SRichard Scheffenegger 	int loss_sblks = 0;
571e2c6a6d2SRichard Scheffenegger 	int notlost_bytes = 0;
572e2c6a6d2SRichard Scheffenegger 
5739eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
5745a53ca16SPaul Saab 
57564b5fbaaSPaul Saab 	num_sack_blks = 0;
57649a6fbe3SRichard Scheffenegger 	sack_changed = SACK_NOCHANGE;
577f359d6ebSRichard Scheffenegger 	delivered_data = 0;
578f359d6ebSRichard Scheffenegger 	left_edge_delta = 0;
5795a53ca16SPaul Saab 	/*
5805a53ca16SPaul Saab 	 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
5815a53ca16SPaul Saab 	 * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
582f359d6ebSRichard Scheffenegger 	 * Account changes to SND.UNA always in delivered data.
5835a53ca16SPaul Saab 	 */
5845a53ca16SPaul Saab 	if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
585f359d6ebSRichard Scheffenegger 		left_edge_delta = th_ack - tp->snd_una;
5865a53ca16SPaul Saab 		sack_blocks[num_sack_blks].start = tp->snd_una;
5875a53ca16SPaul Saab 		sack_blocks[num_sack_blks++].end = th_ack;
588a649f1f6SRichard Scheffenegger 		/*
589a649f1f6SRichard Scheffenegger 		 * Pulling snd_fack forward if we got here
590a649f1f6SRichard Scheffenegger 		 * due to DSACK blocks
591a649f1f6SRichard Scheffenegger 		 */
592a649f1f6SRichard Scheffenegger 		if (SEQ_LT(tp->snd_fack, th_ack)) {
593a649f1f6SRichard Scheffenegger 			delivered_data += th_ack - tp->snd_una;
594a649f1f6SRichard Scheffenegger 			tp->snd_fack = th_ack;
59549a6fbe3SRichard Scheffenegger 			sack_changed = SACK_CHANGE;
596a649f1f6SRichard Scheffenegger 		}
5975a53ca16SPaul Saab 	}
5985a53ca16SPaul Saab 	/*
599632bbf0fSRobert Watson 	 * Append received valid SACK blocks to sack_blocks[], but only if we
600632bbf0fSRobert Watson 	 * received new blocks from the other side.
6015a53ca16SPaul Saab 	 */
60207b64b90SAndre Oppermann 	if (to->to_flags & TOF_SACK) {
6035a53ca16SPaul Saab 		for (i = 0; i < to->to_nsacks; i++) {
60407b64b90SAndre Oppermann 			bcopy((to->to_sacks + i * TCPOLEN_SACK),
60507b64b90SAndre Oppermann 			    &sack, sizeof(sack));
6066d90faf3SPaul Saab 			sack.start = ntohl(sack.start);
6076d90faf3SPaul Saab 			sack.end = ntohl(sack.end);
60864b5fbaaSPaul Saab 			if (SEQ_GT(sack.end, sack.start) &&
60964b5fbaaSPaul Saab 			    SEQ_GT(sack.start, tp->snd_una) &&
6105a53ca16SPaul Saab 			    SEQ_GT(sack.start, th_ack) &&
6111f65c2cdSMohan Srinivasan 			    SEQ_LT(sack.start, tp->snd_max) &&
6121f65c2cdSMohan Srinivasan 			    SEQ_GT(sack.end, tp->snd_una) &&
613cbf3575aSRichard Scheffenegger 			    SEQ_LEQ(sack.end, tp->snd_max) &&
614cbf3575aSRichard Scheffenegger 			    ((sack.end - sack.start) >= maxseg ||
615cbf3575aSRichard Scheffenegger 			     SEQ_GEQ(sack.end, tp->snd_max))) {
61664b5fbaaSPaul Saab 				sack_blocks[num_sack_blks++] = sack;
617a36230f7SRandall Stewart 			} else if (SEQ_LEQ(sack.start, th_ack) &&
618a36230f7SRandall Stewart 			    SEQ_LEQ(sack.end, th_ack)) {
619a36230f7SRandall Stewart 				/*
620a36230f7SRandall Stewart 				 * Its a D-SACK block.
621a36230f7SRandall Stewart 				 */
622a36230f7SRandall Stewart 				tcp_record_dsack(tp, sack.start, sack.end, 0);
62312eeb81fSHiren Panchasara 			}
6246d90faf3SPaul Saab 		}
62507b64b90SAndre Oppermann 	}
6265a53ca16SPaul Saab 	/*
627632bbf0fSRobert Watson 	 * Return if SND.UNA is not advanced and no valid SACK block is
628632bbf0fSRobert Watson 	 * received.
6295a53ca16SPaul Saab 	 */
63064b5fbaaSPaul Saab 	if (num_sack_blks == 0)
631021eaf79SHiren Panchasara 		return (sack_changed);
6325a53ca16SPaul Saab 
6335a53ca16SPaul Saab 	/*
634632bbf0fSRobert Watson 	 * Sort the SACK blocks so we can update the scoreboard with just one
635632bbf0fSRobert Watson 	 * pass. The overhead of sorting up to 4+1 elements is less than
636632bbf0fSRobert Watson 	 * making up to 4+1 passes over the scoreboard.
6375a53ca16SPaul Saab 	 */
63864b5fbaaSPaul Saab 	for (i = 0; i < num_sack_blks; i++) {
63964b5fbaaSPaul Saab 		for (j = i + 1; j < num_sack_blks; j++) {
6409d17a7a6SPaul Saab 			if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
64164b5fbaaSPaul Saab 				sack = sack_blocks[i];
64264b5fbaaSPaul Saab 				sack_blocks[i] = sack_blocks[j];
64364b5fbaaSPaul Saab 				sack_blocks[j] = sack;
6446d90faf3SPaul Saab 			}
6456d90faf3SPaul Saab 		}
64664b5fbaaSPaul Saab 	}
647f359d6ebSRichard Scheffenegger 	if (TAILQ_EMPTY(&tp->snd_holes)) {
64864b5fbaaSPaul Saab 		/*
649808f11b7SPaul Saab 		 * Empty scoreboard. Need to initialize snd_fack (it may be
65064b5fbaaSPaul Saab 		 * uninitialized or have a bogus value). Scoreboard holes
651632bbf0fSRobert Watson 		 * (from the sack blocks received) are created later below
652632bbf0fSRobert Watson 		 * (in the logic that adds holes to the tail of the
653632bbf0fSRobert Watson 		 * scoreboard).
65464b5fbaaSPaul Saab 		 */
6555a53ca16SPaul Saab 		tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
656f359d6ebSRichard Scheffenegger 		tp->sackhint.sacked_bytes = 0;	/* reset */
657e2c6a6d2SRichard Scheffenegger 		tp->sackhint.hole_bytes = 0;
658f359d6ebSRichard Scheffenegger 	}
6599d17a7a6SPaul Saab 	/*
660632bbf0fSRobert Watson 	 * In the while-loop below, incoming SACK blocks (sack_blocks[]) and
661632bbf0fSRobert Watson 	 * SACK holes (snd_holes) are traversed from their tails with just
662632bbf0fSRobert Watson 	 * one pass in order to reduce the number of compares especially when
663632bbf0fSRobert Watson 	 * the bandwidth-delay product is large.
664632bbf0fSRobert Watson 	 *
6659d17a7a6SPaul Saab 	 * Note: Typically, in the first RTT of SACK recovery, the highest
6669d17a7a6SPaul Saab 	 * three or four SACK blocks with the same ack number are received.
6679d17a7a6SPaul Saab 	 * In the second RTT, if retransmitted data segments are not lost,
6689d17a7a6SPaul Saab 	 * the highest three or four SACK blocks with ack number advancing
6699d17a7a6SPaul Saab 	 * are received.
6709d17a7a6SPaul Saab 	 */
6719d17a7a6SPaul Saab 	sblkp = &sack_blocks[num_sack_blks - 1];	/* Last SACK block */
672bee9ab2bSLawrence Stewart 	tp->sackhint.last_sack_ack = sblkp->end;
6739d17a7a6SPaul Saab 	if (SEQ_LT(tp->snd_fack, sblkp->start)) {
6749d17a7a6SPaul Saab 		/*
675a649f1f6SRichard Scheffenegger 		 * The highest SACK block is beyond fack.  First,
676a649f1f6SRichard Scheffenegger 		 * check if there was a successful Rescue Retransmission,
677a649f1f6SRichard Scheffenegger 		 * and move this hole left. With normal holes, snd_fack
678a649f1f6SRichard Scheffenegger 		 * is always to the right of the end.
679a649f1f6SRichard Scheffenegger 		 */
680a649f1f6SRichard Scheffenegger 		if (((temp = TAILQ_LAST(&tp->snd_holes, sackhole_head)) != NULL) &&
681a649f1f6SRichard Scheffenegger 		    SEQ_LEQ(tp->snd_fack,temp->end)) {
682e2c6a6d2SRichard Scheffenegger 			tp->sackhint.hole_bytes -= temp->end - temp->start;
683a649f1f6SRichard Scheffenegger 			temp->start = SEQ_MAX(tp->snd_fack, SEQ_MAX(tp->snd_una, th_ack));
684a649f1f6SRichard Scheffenegger 			temp->end = sblkp->start;
685a649f1f6SRichard Scheffenegger 			temp->rxmit = temp->start;
686a649f1f6SRichard Scheffenegger 			delivered_data += sblkp->end - sblkp->start;
687e2c6a6d2SRichard Scheffenegger 			tp->sackhint.hole_bytes += temp->end - temp->start;
688e2c6a6d2SRichard Scheffenegger 			KASSERT(tp->sackhint.hole_bytes >= 0,
689e2c6a6d2SRichard Scheffenegger 			    ("sackhint hole bytes >= 0"));
690a649f1f6SRichard Scheffenegger 			tp->snd_fack = sblkp->end;
691e2c6a6d2SRichard Scheffenegger 			sblkp--;
69249a6fbe3SRichard Scheffenegger 			sack_changed = SACK_NEWLOSS;
693a649f1f6SRichard Scheffenegger 		} else {
694a649f1f6SRichard Scheffenegger 			/*
695a649f1f6SRichard Scheffenegger 			 * Append a new SACK hole at the tail.  If the
696a649f1f6SRichard Scheffenegger 			 * second or later highest SACK blocks are also
697a649f1f6SRichard Scheffenegger 			 * beyond the current fack, they will be inserted
698a649f1f6SRichard Scheffenegger 			 * by way of hole splitting in the while-loop below.
6999d17a7a6SPaul Saab 			 */
7008b9bbaaaSPaul Saab 			temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL);
701d0a14f55SPaul Saab 			if (temp != NULL) {
702f359d6ebSRichard Scheffenegger 				delivered_data += sblkp->end - sblkp->start;
703e2c6a6d2SRichard Scheffenegger 				tp->sackhint.hole_bytes += temp->end - temp->start;
7049d17a7a6SPaul Saab 				tp->snd_fack = sblkp->end;
7059d17a7a6SPaul Saab 				/* Go to the previous sack block. */
7069d17a7a6SPaul Saab 				sblkp--;
70749a6fbe3SRichard Scheffenegger 				sack_changed = SACK_CHANGE;
708d0a14f55SPaul Saab 			} else {
709d0a14f55SPaul Saab 				/*
710d0a14f55SPaul Saab 				 * We failed to add a new hole based on the current
711d0a14f55SPaul Saab 				 * sack block.  Skip over all the sack blocks that
712632bbf0fSRobert Watson 				 * fall completely to the right of snd_fack and
713632bbf0fSRobert Watson 				 * proceed to trim the scoreboard based on the
714632bbf0fSRobert Watson 				 * remaining sack blocks.  This also trims the
715632bbf0fSRobert Watson 				 * scoreboard for th_ack (which is sack_blocks[0]).
716d0a14f55SPaul Saab 				 */
717d0a14f55SPaul Saab 				while (sblkp >= sack_blocks &&
718d0a14f55SPaul Saab 				       SEQ_LT(tp->snd_fack, sblkp->start))
719d0a14f55SPaul Saab 					sblkp--;
720d0a14f55SPaul Saab 				if (sblkp >= sack_blocks &&
721f359d6ebSRichard Scheffenegger 				    SEQ_LT(tp->snd_fack, sblkp->end)) {
722f359d6ebSRichard Scheffenegger 					delivered_data += sblkp->end - tp->snd_fack;
723d0a14f55SPaul Saab 					tp->snd_fack = sblkp->end;
72449a6fbe3SRichard Scheffenegger 					/*
72549a6fbe3SRichard Scheffenegger 					 * While the Scoreboard didn't change in
72649a6fbe3SRichard Scheffenegger 					 * size, we only ended up here because
72749a6fbe3SRichard Scheffenegger 					 * some SACK data had to be dismissed.
72849a6fbe3SRichard Scheffenegger 					 */
72949a6fbe3SRichard Scheffenegger 					sack_changed = SACK_NEWLOSS;
730f359d6ebSRichard Scheffenegger 				}
731d0a14f55SPaul Saab 			}
732a649f1f6SRichard Scheffenegger 		}
733021eaf79SHiren Panchasara 	} else if (SEQ_LT(tp->snd_fack, sblkp->end)) {
7349d17a7a6SPaul Saab 		/* fack is advanced. */
735f359d6ebSRichard Scheffenegger 		delivered_data += sblkp->end - tp->snd_fack;
7369d17a7a6SPaul Saab 		tp->snd_fack = sblkp->end;
73749a6fbe3SRichard Scheffenegger 		sack_changed = SACK_CHANGE;
738021eaf79SHiren Panchasara 	}
739632bbf0fSRobert Watson 	cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */
740e2c6a6d2SRichard Scheffenegger 	loss_hiack = tp->snd_fack;
741e2c6a6d2SRichard Scheffenegger 
74264b5fbaaSPaul Saab 	/*
74364b5fbaaSPaul Saab 	 * Since the incoming sack blocks are sorted, we can process them
74464b5fbaaSPaul Saab 	 * making one sweep of the scoreboard.
74564b5fbaaSPaul Saab 	 */
746e2c6a6d2SRichard Scheffenegger 	while (cur != NULL) {
747e2c6a6d2SRichard Scheffenegger 		if (!(sblkp >= sack_blocks)) {
748e2c6a6d2SRichard Scheffenegger 			if (((loss_sblks >= tcprexmtthresh) ||
749e2c6a6d2SRichard Scheffenegger 			    (loss_thresh > (tcprexmtthresh-1)*tp->t_maxseg)))
750e2c6a6d2SRichard Scheffenegger 				break;
751e2c6a6d2SRichard Scheffenegger 			loss_thresh += loss_hiack - cur->end;
752e2c6a6d2SRichard Scheffenegger 			loss_hiack = cur->start;
753e2c6a6d2SRichard Scheffenegger 			loss_sblks++;
754e2c6a6d2SRichard Scheffenegger 			if (!((loss_sblks >= tcprexmtthresh) ||
755e2c6a6d2SRichard Scheffenegger 			    (loss_thresh > (tcprexmtthresh-1)*tp->t_maxseg))) {
756e2c6a6d2SRichard Scheffenegger 				notlost_bytes += cur->end - cur->start;
757e2c6a6d2SRichard Scheffenegger 			} else {
758e2c6a6d2SRichard Scheffenegger 				break;
759e2c6a6d2SRichard Scheffenegger 			}
760e2c6a6d2SRichard Scheffenegger 			cur = TAILQ_PREV(cur, sackhole_head, scblink);
761e2c6a6d2SRichard Scheffenegger 			continue;
762e2c6a6d2SRichard Scheffenegger 		}
7639d17a7a6SPaul Saab 		if (SEQ_GEQ(sblkp->start, cur->end)) {
76464b5fbaaSPaul Saab 			/*
765632bbf0fSRobert Watson 			 * SACKs data beyond the current hole.  Go to the
766632bbf0fSRobert Watson 			 * previous sack block.
76764b5fbaaSPaul Saab 			 */
7689d17a7a6SPaul Saab 			sblkp--;
7699d17a7a6SPaul Saab 			continue;
7709d17a7a6SPaul Saab 		}
7719d17a7a6SPaul Saab 		if (SEQ_LEQ(sblkp->end, cur->start)) {
7729d17a7a6SPaul Saab 			/*
773632bbf0fSRobert Watson 			 * SACKs data before the current hole.  Go to the
774632bbf0fSRobert Watson 			 * previous hole.
7759d17a7a6SPaul Saab 			 */
776e2c6a6d2SRichard Scheffenegger 			loss_thresh += loss_hiack - cur->end;
777e2c6a6d2SRichard Scheffenegger 			loss_hiack = cur->start;
778e2c6a6d2SRichard Scheffenegger 			loss_sblks++;
779e2c6a6d2SRichard Scheffenegger 			if (!((loss_sblks >= tcprexmtthresh) ||
780e2c6a6d2SRichard Scheffenegger 			    (loss_thresh > (tcprexmtthresh-1)*tp->t_maxseg)))
781e2c6a6d2SRichard Scheffenegger 				notlost_bytes += cur->end - cur->start;
7829d17a7a6SPaul Saab 			cur = TAILQ_PREV(cur, sackhole_head, scblink);
7836d90faf3SPaul Saab 			continue;
7846d90faf3SPaul Saab 		}
7850471a8c7SRichard Scheffenegger 		tp->sackhint.sack_bytes_rexmit -=
7860471a8c7SRichard Scheffenegger 		    (SEQ_MIN(cur->rxmit, cur->end) - cur->start);
7870077b016SPaul Saab 		KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
7880077b016SPaul Saab 		    ("sackhint bytes rtx >= 0"));
78949a6fbe3SRichard Scheffenegger 		sack_changed = SACK_CHANGE;
7909d17a7a6SPaul Saab 		if (SEQ_LEQ(sblkp->start, cur->start)) {
791632bbf0fSRobert Watson 			/* Data acks at least the beginning of hole. */
7929d17a7a6SPaul Saab 			if (SEQ_GEQ(sblkp->end, cur->end)) {
793632bbf0fSRobert Watson 				/* Acks entire hole, so delete hole. */
794f359d6ebSRichard Scheffenegger 				delivered_data += (cur->end - cur->start);
795a6235da6SPaul Saab 				temp = cur;
7969d17a7a6SPaul Saab 				cur = TAILQ_PREV(cur, sackhole_head, scblink);
797e2c6a6d2SRichard Scheffenegger 				tp->sackhint.hole_bytes -= temp->end - temp->start;
7989d17a7a6SPaul Saab 				tcp_sackhole_remove(tp, temp);
79964b5fbaaSPaul Saab 				/*
800632bbf0fSRobert Watson 				 * The sack block may ack all or part of the
801632bbf0fSRobert Watson 				 * next hole too, so continue onto the next
802632bbf0fSRobert Watson 				 * hole.
80364b5fbaaSPaul Saab 				 */
8046d90faf3SPaul Saab 				continue;
8050077b016SPaul Saab 			} else {
806632bbf0fSRobert Watson 				/* Move start of hole forward. */
807f359d6ebSRichard Scheffenegger 				delivered_data += (sblkp->end - cur->start);
808e2c6a6d2SRichard Scheffenegger 				tp->sackhint.hole_bytes -= sblkp->end - cur->start;
8099d17a7a6SPaul Saab 				cur->start = sblkp->end;
8106d90faf3SPaul Saab 				cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
8116d90faf3SPaul Saab 			}
81264b5fbaaSPaul Saab 		} else {
813632bbf0fSRobert Watson 			/* Data acks at least the end of hole. */
8149d17a7a6SPaul Saab 			if (SEQ_GEQ(sblkp->end, cur->end)) {
815632bbf0fSRobert Watson 				/* Move end of hole backward. */
816f359d6ebSRichard Scheffenegger 				delivered_data += (cur->end - sblkp->start);
817e2c6a6d2SRichard Scheffenegger 				tp->sackhint.hole_bytes -= cur->end - sblkp->start;
8189d17a7a6SPaul Saab 				cur->end = sblkp->start;
8196d90faf3SPaul Saab 				cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
8200471a8c7SRichard Scheffenegger 				if ((tp->t_flags & TF_LRD) && SEQ_GEQ(cur->rxmit, cur->end))
8210471a8c7SRichard Scheffenegger 					cur->rxmit = tp->snd_recover;
8220077b016SPaul Saab 			} else {
8236d90faf3SPaul Saab 				/*
824632bbf0fSRobert Watson 				 * ACKs some data in middle of a hole; need
825632bbf0fSRobert Watson 				 * to split current hole
8266d90faf3SPaul Saab 				 */
8279d17a7a6SPaul Saab 				temp = tcp_sackhole_insert(tp, sblkp->end,
8289d17a7a6SPaul Saab 				    cur->end, cur);
82949a6fbe3SRichard Scheffenegger 				sack_changed = SACK_NEWLOSS;
8300077b016SPaul Saab 				if (temp != NULL) {
8319d17a7a6SPaul Saab 					if (SEQ_GT(cur->rxmit, temp->rxmit)) {
8324fc53245SPaul Saab 						temp->rxmit = cur->rxmit;
8330471a8c7SRichard Scheffenegger 						tp->sackhint.sack_bytes_rexmit +=
8340471a8c7SRichard Scheffenegger 						    (SEQ_MIN(temp->rxmit,
8350471a8c7SRichard Scheffenegger 						    temp->end) - temp->start);
8369d17a7a6SPaul Saab 					}
837e2c6a6d2SRichard Scheffenegger 					tp->sackhint.hole_bytes -= sblkp->end - sblkp->start;
838e2c6a6d2SRichard Scheffenegger 					loss_thresh += loss_hiack - temp->end;
839e2c6a6d2SRichard Scheffenegger 					loss_hiack = temp->start;
840e2c6a6d2SRichard Scheffenegger 					loss_sblks++;
841e2c6a6d2SRichard Scheffenegger 					if (!((loss_sblks >= tcprexmtthresh) ||
842e2c6a6d2SRichard Scheffenegger 					    (loss_thresh > (tcprexmtthresh-1)*tp->t_maxseg)))
843e2c6a6d2SRichard Scheffenegger 						notlost_bytes += temp->end - temp->start;
8449d17a7a6SPaul Saab 					cur->end = sblkp->start;
8459d17a7a6SPaul Saab 					cur->rxmit = SEQ_MIN(cur->rxmit,
8469d17a7a6SPaul Saab 					    cur->end);
8470471a8c7SRichard Scheffenegger 					if ((tp->t_flags & TF_LRD) && SEQ_GEQ(cur->rxmit, cur->end))
8480471a8c7SRichard Scheffenegger 						cur->rxmit = tp->snd_recover;
849f359d6ebSRichard Scheffenegger 					delivered_data += (sblkp->end - sblkp->start);
8506d90faf3SPaul Saab 				}
8516d90faf3SPaul Saab 			}
8520077b016SPaul Saab 		}
8530471a8c7SRichard Scheffenegger 		tp->sackhint.sack_bytes_rexmit +=
8540471a8c7SRichard Scheffenegger 		    (SEQ_MIN(cur->rxmit, cur->end) - cur->start);
855db4b83feSPaul Saab 		/*
856db4b83feSPaul Saab 		 * Testing sblkp->start against cur->start tells us whether
857db4b83feSPaul Saab 		 * we're done with the sack block or the sack hole.
858db4b83feSPaul Saab 		 * Accordingly, we advance one or the other.
859db4b83feSPaul Saab 		 */
860e2c6a6d2SRichard Scheffenegger 		if (SEQ_LEQ(sblkp->start, cur->start)) {
861e2c6a6d2SRichard Scheffenegger 			loss_thresh += loss_hiack - cur->end;
862e2c6a6d2SRichard Scheffenegger 			loss_hiack = cur->start;
863e2c6a6d2SRichard Scheffenegger 			loss_sblks++;
864e2c6a6d2SRichard Scheffenegger 			if (!((loss_sblks >= tcprexmtthresh) ||
865e2c6a6d2SRichard Scheffenegger 			    (loss_thresh > (tcprexmtthresh-1)*tp->t_maxseg)))
866e2c6a6d2SRichard Scheffenegger 				notlost_bytes += cur->end - cur->start;
867db4b83feSPaul Saab 			cur = TAILQ_PREV(cur, sackhole_head, scblink);
868e2c6a6d2SRichard Scheffenegger 		} else {
869db4b83feSPaul Saab 			sblkp--;
8706d90faf3SPaul Saab 		}
871e2c6a6d2SRichard Scheffenegger 	}
872e2c6a6d2SRichard Scheffenegger 
873e2c6a6d2SRichard Scheffenegger 	KASSERT(!(TAILQ_EMPTY(&tp->snd_holes) && (tp->sackhint.hole_bytes != 0)),
874e2c6a6d2SRichard Scheffenegger 	    ("SACK scoreboard empty, but accounting non-zero\n"));
875e2c6a6d2SRichard Scheffenegger 
876e2c6a6d2SRichard Scheffenegger 	KASSERT(notlost_bytes <= tp->sackhint.hole_bytes,
877e2c6a6d2SRichard Scheffenegger 	    ("SACK: more bytes marked notlost than in scoreboard holes"));
878e2c6a6d2SRichard Scheffenegger 
8793c40e1d5SRichard Scheffenegger 	if (!(to->to_flags & TOF_SACK))
8803c40e1d5SRichard Scheffenegger 		/*
8813c40e1d5SRichard Scheffenegger 		 * If this ACK did not contain any
8823c40e1d5SRichard Scheffenegger 		 * SACK blocks, any only moved the
8833c40e1d5SRichard Scheffenegger 		 * left edge right, it is a pure
8843c40e1d5SRichard Scheffenegger 		 * cumulative ACK. Do not count
8853c40e1d5SRichard Scheffenegger 		 * DupAck for this. Also required
8863c40e1d5SRichard Scheffenegger 		 * for RFC6675 rescue retransmission.
8873c40e1d5SRichard Scheffenegger 		 */
88849a6fbe3SRichard Scheffenegger 		sack_changed = SACK_NOCHANGE;
889f359d6ebSRichard Scheffenegger 	tp->sackhint.delivered_data = delivered_data;
890f359d6ebSRichard Scheffenegger 	tp->sackhint.sacked_bytes += delivered_data - left_edge_delta;
891e2c6a6d2SRichard Scheffenegger 	tp->sackhint.lost_bytes = tp->sackhint.hole_bytes - notlost_bytes;
892f359d6ebSRichard Scheffenegger 	KASSERT((delivered_data >= 0), ("delivered_data < 0"));
893f359d6ebSRichard Scheffenegger 	KASSERT((tp->sackhint.sacked_bytes >= 0), ("sacked_bytes < 0"));
894021eaf79SHiren Panchasara 	return (sack_changed);
8956d90faf3SPaul Saab }
8966d90faf3SPaul Saab 
8976d90faf3SPaul Saab /*
8985a53ca16SPaul Saab  * Free all SACK holes to clear the scoreboard.
8996d90faf3SPaul Saab  */
9006d90faf3SPaul Saab void
tcp_free_sackholes(struct tcpcb * tp)9016d90faf3SPaul Saab tcp_free_sackholes(struct tcpcb *tp)
9026d90faf3SPaul Saab {
903a6235da6SPaul Saab 	struct sackhole *q;
9046d90faf3SPaul Saab 
9059eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
9069d17a7a6SPaul Saab 	while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
9079d17a7a6SPaul Saab 		tcp_sackhole_remove(tp, q);
9080077b016SPaul Saab 	tp->sackhint.sack_bytes_rexmit = 0;
90991ee2d8dSRichard Scheffenegger 	tp->sackhint.delivered_data = 0;
91022dc8609SRichard Scheffenegger 	tp->sackhint.sacked_bytes = 0;
91191ee2d8dSRichard Scheffenegger 	tp->sackhint.hole_bytes = 0;
91222dc8609SRichard Scheffenegger 	tp->sackhint.lost_bytes = 0;
9134fc53245SPaul Saab 
9144fc53245SPaul Saab 	KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
9159d17a7a6SPaul Saab 	KASSERT(tp->sackhint.nexthole == NULL,
9169d17a7a6SPaul Saab 		("tp->sackhint.nexthole == NULL"));
9176d90faf3SPaul Saab }
9186d90faf3SPaul Saab 
9196d90faf3SPaul Saab /*
92030409ecdSRichard Scheffenegger  * Resend all the currently existing SACK holes of
92130409ecdSRichard Scheffenegger  * the scoreboard. This is in line with the Errata to
92230409ecdSRichard Scheffenegger  * RFC 2018, which allows the use of SACK data past
92330409ecdSRichard Scheffenegger  * an RTO to good effect typically.
92430409ecdSRichard Scheffenegger  */
92530409ecdSRichard Scheffenegger void
tcp_resend_sackholes(struct tcpcb * tp)92630409ecdSRichard Scheffenegger tcp_resend_sackholes(struct tcpcb *tp)
92730409ecdSRichard Scheffenegger {
92830409ecdSRichard Scheffenegger 	struct sackhole *p;
92930409ecdSRichard Scheffenegger 
93030409ecdSRichard Scheffenegger 	INP_WLOCK_ASSERT(tptoinpcb(tp));
93130409ecdSRichard Scheffenegger 	TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
93230409ecdSRichard Scheffenegger 		p->rxmit = p->start;
93330409ecdSRichard Scheffenegger 	}
93430409ecdSRichard Scheffenegger 	tp->sackhint.nexthole = TAILQ_FIRST(&tp->snd_holes);
93530409ecdSRichard Scheffenegger 	tp->sackhint.sack_bytes_rexmit = 0;
93630409ecdSRichard Scheffenegger }
93730409ecdSRichard Scheffenegger 
93830409ecdSRichard Scheffenegger /*
939632bbf0fSRobert Watson  * Partial ack handling within a sack recovery episode.  Keeping this very
940632bbf0fSRobert Watson  * simple for now.  When a partial ack is received, force snd_cwnd to a value
941632bbf0fSRobert Watson  * that will allow the sender to transmit no more than 2 segments.  If
942632bbf0fSRobert Watson  * necessary, a better scheme can be adopted at a later point, but for now,
943632bbf0fSRobert Watson  * the goal is to prevent the sender from bursting a large amount of data in
944632bbf0fSRobert Watson  * the midst of sack recovery.
9456d90faf3SPaul Saab  */
9466d90faf3SPaul Saab void
tcp_sack_partialack(struct tcpcb * tp,struct tcphdr * th,u_int * maxsegp)947c7c325d0SRichard Scheffenegger tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th, u_int *maxsegp)
9486d90faf3SPaul Saab {
949e2c6a6d2SRichard Scheffenegger 	struct sackhole *temp;
9508db456bfSPaul Saab 	int num_segs = 1;
951c7c325d0SRichard Scheffenegger 	u_int maxseg;
9526d90faf3SPaul Saab 
9539eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
954c7c325d0SRichard Scheffenegger 
955c7c325d0SRichard Scheffenegger 	if (*maxsegp == 0) {
956c7c325d0SRichard Scheffenegger 		*maxsegp = tcp_maxseg(tp);
957c7c325d0SRichard Scheffenegger 	}
958c7c325d0SRichard Scheffenegger 	maxseg = *maxsegp;
959b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
9606d90faf3SPaul Saab 	tp->t_rtttime = 0;
961632bbf0fSRobert Watson 	/* Send one or 2 segments based on how much new data was acked. */
9624b72ae16SRichard Scheffenegger 	if ((BYTES_THIS_ACK(tp, th) / maxseg) >= 2)
9638db456bfSPaul Saab 		num_segs = 2;
964*7dc78150SRichard Scheffenegger 	if (tp->snd_nxt == tp->snd_max) {
9650077b016SPaul Saab 		tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
966*7dc78150SRichard Scheffenegger 		    (tp->snd_nxt - tp->snd_recover) + num_segs * maxseg);
967*7dc78150SRichard Scheffenegger 	} else {
968*7dc78150SRichard Scheffenegger 		/*
969*7dc78150SRichard Scheffenegger 		 * Since cwnd is not the expected flightsize during
970*7dc78150SRichard Scheffenegger 		 * SACK LR, not deflating cwnd allows the partial
971*7dc78150SRichard Scheffenegger 		 * ACKed amount to be sent.
972*7dc78150SRichard Scheffenegger 		 */
97323c4f232SRichard Scheffenegger 	}
97425e6f9edSPaul Saab 	if (tp->snd_cwnd > tp->snd_ssthresh)
97525e6f9edSPaul Saab 		tp->snd_cwnd = tp->snd_ssthresh;
9766d90faf3SPaul Saab 	tp->t_flags |= TF_ACKNOW;
9773c40e1d5SRichard Scheffenegger 	/*
9783c40e1d5SRichard Scheffenegger 	 * RFC6675 rescue retransmission
9793c40e1d5SRichard Scheffenegger 	 * Add a hole between th_ack (snd_una is not yet set) and snd_max,
9803c40e1d5SRichard Scheffenegger 	 * if this was a pure cumulative ACK and no data was send beyond
9813c40e1d5SRichard Scheffenegger 	 * recovery point. Since the data in the socket has not been freed
9823c40e1d5SRichard Scheffenegger 	 * at this point, we check if the scoreboard is empty, and the ACK
9833c40e1d5SRichard Scheffenegger 	 * delivered some new data, indicating a full ACK. Also, if the
9843c40e1d5SRichard Scheffenegger 	 * recovery point is still at snd_max, we are probably application
9853c40e1d5SRichard Scheffenegger 	 * limited. However, this inference might not always be true. The
9863c40e1d5SRichard Scheffenegger 	 * rescue retransmission may rarely be slightly premature
9873c40e1d5SRichard Scheffenegger 	 * compared to RFC6675.
9883c40e1d5SRichard Scheffenegger 	 * The corresponding ACK+SACK will cause any further outstanding
9893c40e1d5SRichard Scheffenegger 	 * segments to be retransmitted. This addresses a corner case, when
9903c40e1d5SRichard Scheffenegger 	 * the trailing packets of a window are lost and no further data
9913c40e1d5SRichard Scheffenegger 	 * is available for sending.
9923c40e1d5SRichard Scheffenegger 	 */
993d1de2b05SRichard Scheffenegger 	if ((V_tcp_do_newsack) &&
9943c40e1d5SRichard Scheffenegger 	    SEQ_LT(th->th_ack, tp->snd_recover) &&
9953c40e1d5SRichard Scheffenegger 	    TAILQ_EMPTY(&tp->snd_holes) &&
9963c40e1d5SRichard Scheffenegger 	    (tp->sackhint.delivered_data > 0)) {
997e9f02983SRichard Scheffenegger 		/*
998e9f02983SRichard Scheffenegger 		 * Exclude FIN sequence space in
999e9f02983SRichard Scheffenegger 		 * the hole for the rescue retransmission,
1000e9f02983SRichard Scheffenegger 		 * and also don't create a hole, if only
1001e9f02983SRichard Scheffenegger 		 * the ACK for a FIN is outstanding.
1002e9f02983SRichard Scheffenegger 		 */
1003e9f02983SRichard Scheffenegger 		tcp_seq highdata = tp->snd_max;
1004e9f02983SRichard Scheffenegger 		if (tp->t_flags & TF_SENTFIN)
1005e9f02983SRichard Scheffenegger 			highdata--;
1006f858eb91SRichard Scheffenegger 		highdata = SEQ_MIN(highdata, tp->snd_recover);
10072d05a1c8SRichard Scheffenegger 		if (SEQ_LT(th->th_ack, highdata)) {
1008440f4ba1SRichard Scheffenegger 			tp->snd_fack = SEQ_MAX(th->th_ack, tp->snd_fack);
1009e2c6a6d2SRichard Scheffenegger 			if ((temp = tcp_sackhole_insert(tp, SEQ_MAX(th->th_ack,
10102d05a1c8SRichard Scheffenegger 			    highdata - maxseg), highdata, NULL)) != NULL) {
10112d05a1c8SRichard Scheffenegger 				tp->sackhint.hole_bytes +=
10122d05a1c8SRichard Scheffenegger 					temp->end - temp->start;
10132d05a1c8SRichard Scheffenegger 			}
10143c40e1d5SRichard Scheffenegger 		}
101548be5b97SRichard Scheffenegger 	}
101640fa3e40SGleb Smirnoff 	(void) tcp_output(tp);
10176d90faf3SPaul Saab }
10186d90faf3SPaul Saab 
10196d90faf3SPaul Saab /*
10200077b016SPaul Saab  * Returns the next hole to retransmit and the number of retransmitted bytes
10210077b016SPaul Saab  * from the scoreboard.  We store both the next hole and the number of
10220077b016SPaul Saab  * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
10230077b016SPaul Saab  * reception).  This avoids scoreboard traversals completely.
10240077b016SPaul Saab  *
1025632bbf0fSRobert Watson  * The loop here will traverse *at most* one link.  Here's the argument.  For
1026632bbf0fSRobert Watson  * the loop to traverse more than 1 link before finding the next hole to
1027632bbf0fSRobert Watson  * retransmit, we would need to have at least 1 node following the current
1028632bbf0fSRobert Watson  * hint with (rxmit == end).  But, for all holes following the current hint,
1029632bbf0fSRobert Watson  * (start == rxmit), since we have not yet retransmitted from them.
1030632bbf0fSRobert Watson  * Therefore, in order to traverse more 1 link in the loop below, we need to
1031632bbf0fSRobert Watson  * have at least one node following the current hint with (start == rxmit ==
1032632bbf0fSRobert Watson  * end).  But that can't happen, (start == end) means that all the data in
1033632bbf0fSRobert Watson  * that hole has been sacked, in which case, the hole would have been removed
1034632bbf0fSRobert Watson  * from the scoreboard.
10350077b016SPaul Saab  */
10360077b016SPaul Saab struct sackhole *
tcp_sack_output(struct tcpcb * tp,int * sack_bytes_rexmt)10370077b016SPaul Saab tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
10380077b016SPaul Saab {
10391714e18eSMohan Srinivasan 	struct sackhole *hole = NULL;
10400077b016SPaul Saab 
10419eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
10420077b016SPaul Saab 	*sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
10430077b016SPaul Saab 	hole = tp->sackhint.nexthole;
1044231e0dd5SRichard Scheffenegger 	if (hole == NULL)
1045231e0dd5SRichard Scheffenegger 		return (hole);
1046ce2525c8SRichard Scheffenegger 	if (SEQ_GEQ(hole->rxmit, hole->end)) {
1047ce2525c8SRichard Scheffenegger 		for (;;) {
1048ce2525c8SRichard Scheffenegger 			hole = TAILQ_NEXT(hole, scblink);
1049ce2525c8SRichard Scheffenegger 			if (hole == NULL)
1050ce2525c8SRichard Scheffenegger 				return (hole);
10510077b016SPaul Saab 			if (SEQ_LT(hole->rxmit, hole->end)) {
10520077b016SPaul Saab 				tp->sackhint.nexthole = hole;
10530077b016SPaul Saab 				break;
10540077b016SPaul Saab 			}
10550077b016SPaul Saab 		}
1056ce2525c8SRichard Scheffenegger 	}
105791d6afe6SRichard Scheffenegger 	KASSERT(SEQ_LT(hole->start, hole->end), ("%s: hole.start >= hole.end", __func__));
105857317c89SRichard Scheffenegger 	if (!(V_tcp_do_newsack)) {
105991d6afe6SRichard Scheffenegger 		KASSERT(SEQ_LT(hole->start, tp->snd_fack), ("%s: hole.start >= snd.fack", __func__));
106091d6afe6SRichard Scheffenegger 		KASSERT(SEQ_LT(hole->end, tp->snd_fack), ("%s: hole.end >= snd.fack", __func__));
106191d6afe6SRichard Scheffenegger 		KASSERT(SEQ_LT(hole->rxmit, tp->snd_fack), ("%s: hole.rxmit >= snd.fack", __func__));
106291d6afe6SRichard Scheffenegger 		if (SEQ_GEQ(hole->start, hole->end) ||
106391d6afe6SRichard Scheffenegger 		    SEQ_GEQ(hole->start, tp->snd_fack) ||
106491d6afe6SRichard Scheffenegger 		    SEQ_GEQ(hole->end, tp->snd_fack) ||
106591d6afe6SRichard Scheffenegger 		    SEQ_GEQ(hole->rxmit, tp->snd_fack)) {
106691d6afe6SRichard Scheffenegger 			log(LOG_CRIT,"tcp: invalid SACK hole (%u-%u,%u) vs fwd ack %u, ignoring.\n",
106791d6afe6SRichard Scheffenegger 					hole->start, hole->end, hole->rxmit, tp->snd_fack);
106891d6afe6SRichard Scheffenegger 			return (NULL);
106991d6afe6SRichard Scheffenegger 		}
107057317c89SRichard Scheffenegger 	}
10710077b016SPaul Saab 	return (hole);
10720077b016SPaul Saab }
10730077b016SPaul Saab 
10740077b016SPaul Saab /*
10756d90faf3SPaul Saab  * After a timeout, the SACK list may be rebuilt.  This SACK information
10766d90faf3SPaul Saab  * should be used to avoid retransmitting SACKed data.  This function
10776d90faf3SPaul Saab  * traverses the SACK list to see if snd_nxt should be moved forward.
1078440f4ba1SRichard Scheffenegger  * In addition, cwnd will be inflated by the sacked bytes traversed when
1079440f4ba1SRichard Scheffenegger  * moving snd_nxt forward. This prevents a traffic burst after the final
1080440f4ba1SRichard Scheffenegger  * full ACK, and also keeps ACKs coming back.
10816d90faf3SPaul Saab  */
1082440f4ba1SRichard Scheffenegger int
tcp_sack_adjust(struct tcpcb * tp)10836d90faf3SPaul Saab tcp_sack_adjust(struct tcpcb *tp)
10846d90faf3SPaul Saab {
1085440f4ba1SRichard Scheffenegger 	int sacked = 0;
1086a6235da6SPaul Saab 	struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
1087b7c75571SPaul Saab 
10889eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
10892d05a1c8SRichard Scheffenegger 	if (cur == NULL) {
10902d05a1c8SRichard Scheffenegger 		/* No holes */
1091440f4ba1SRichard Scheffenegger 		return (0);
10922d05a1c8SRichard Scheffenegger 	}
10932d05a1c8SRichard Scheffenegger 	if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack)) {
10942d05a1c8SRichard Scheffenegger 		/* We're already beyond any SACKed blocks */
1095440f4ba1SRichard Scheffenegger 		return (tp->sackhint.sacked_bytes);
10962d05a1c8SRichard Scheffenegger 	}
1097440f4ba1SRichard Scheffenegger 	/*
10986d90faf3SPaul Saab 	 * Two cases for which we want to advance snd_nxt:
10996d90faf3SPaul Saab 	 * i) snd_nxt lies between end of one hole and beginning of another
1100808f11b7SPaul Saab 	 * ii) snd_nxt lies between end of last hole and snd_fack
11016d90faf3SPaul Saab 	 */
1102a6235da6SPaul Saab 	while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
11032d05a1c8SRichard Scheffenegger 		if (SEQ_LT(tp->snd_nxt, cur->end)) {
1104440f4ba1SRichard Scheffenegger 			return (sacked);
11052d05a1c8SRichard Scheffenegger 		}
1106440f4ba1SRichard Scheffenegger 		sacked += p->start - cur->end;
11072d05a1c8SRichard Scheffenegger 		if (SEQ_GEQ(tp->snd_nxt, p->start)) {
1108a6235da6SPaul Saab 			cur = p;
11092d05a1c8SRichard Scheffenegger 		} else {
1110a6235da6SPaul Saab 			tp->snd_nxt = p->start;
1111440f4ba1SRichard Scheffenegger 			return (sacked);
11126d90faf3SPaul Saab 		}
11136d90faf3SPaul Saab 	}
11142d05a1c8SRichard Scheffenegger 	if (SEQ_LT(tp->snd_nxt, cur->end)) {
1115440f4ba1SRichard Scheffenegger 		return (sacked);
11162d05a1c8SRichard Scheffenegger 	}
1117808f11b7SPaul Saab 	tp->snd_nxt = tp->snd_fack;
1118440f4ba1SRichard Scheffenegger 	return (tp->sackhint.sacked_bytes);
11196d90faf3SPaul Saab }
11200471a8c7SRichard Scheffenegger 
11210471a8c7SRichard Scheffenegger /*
11220471a8c7SRichard Scheffenegger  * Lost Retransmission Detection
11230471a8c7SRichard Scheffenegger  * Check is FACK is beyond the rexmit of the leftmost hole.
11240471a8c7SRichard Scheffenegger  * If yes, we restart sending from still existing holes,
11250471a8c7SRichard Scheffenegger  * and adjust cwnd via the congestion control module.
11260471a8c7SRichard Scheffenegger  */
11270471a8c7SRichard Scheffenegger void
tcp_sack_lost_retransmission(struct tcpcb * tp,struct tcphdr * th)11280471a8c7SRichard Scheffenegger tcp_sack_lost_retransmission(struct tcpcb *tp, struct tcphdr *th)
11290471a8c7SRichard Scheffenegger {
11300471a8c7SRichard Scheffenegger 	struct sackhole *temp;
1131aac52f94SRandall Stewart 
11320471a8c7SRichard Scheffenegger 	if (IN_RECOVERY(tp->t_flags) &&
11330471a8c7SRichard Scheffenegger 	    SEQ_GT(tp->snd_fack, tp->snd_recover) &&
11340471a8c7SRichard Scheffenegger 	    ((temp = TAILQ_FIRST(&tp->snd_holes)) != NULL) &&
11350471a8c7SRichard Scheffenegger 	    SEQ_GEQ(temp->rxmit, temp->end) &&
11360471a8c7SRichard Scheffenegger 	    SEQ_GEQ(tp->snd_fack, temp->rxmit)) {
11370471a8c7SRichard Scheffenegger 		TCPSTAT_INC(tcps_sack_lostrexmt);
11380471a8c7SRichard Scheffenegger 		/*
11390471a8c7SRichard Scheffenegger 		 * Start retransmissions from the first hole, and
11400471a8c7SRichard Scheffenegger 		 * subsequently all other remaining holes, including
11410471a8c7SRichard Scheffenegger 		 * those, which had been sent completely before.
11420471a8c7SRichard Scheffenegger 		 */
11430471a8c7SRichard Scheffenegger 		tp->sackhint.nexthole = temp;
11440471a8c7SRichard Scheffenegger 		TAILQ_FOREACH(temp, &tp->snd_holes, scblink) {
11450471a8c7SRichard Scheffenegger 			if (SEQ_GEQ(tp->snd_fack, temp->rxmit) &&
11460471a8c7SRichard Scheffenegger 			    SEQ_GEQ(temp->rxmit, temp->end))
11470471a8c7SRichard Scheffenegger 				temp->rxmit = temp->start;
11480471a8c7SRichard Scheffenegger 		}
11490471a8c7SRichard Scheffenegger 		/*
11500471a8c7SRichard Scheffenegger 		 * Remember the old ssthresh, to deduct the beta factor used
11510471a8c7SRichard Scheffenegger 		 * by the CC module. Finally, set cwnd to ssthresh just
11520471a8c7SRichard Scheffenegger 		 * prior to invoking another cwnd reduction by the CC
11530471a8c7SRichard Scheffenegger 		 * module, to not shrink it excessively.
11540471a8c7SRichard Scheffenegger 		 */
11550471a8c7SRichard Scheffenegger 		tp->snd_cwnd = tp->snd_ssthresh;
11560471a8c7SRichard Scheffenegger 		/*
11570471a8c7SRichard Scheffenegger 		 * Formally exit recovery, and let the CC module adjust
11580471a8c7SRichard Scheffenegger 		 * ssthresh as intended.
11590471a8c7SRichard Scheffenegger 		 */
11600471a8c7SRichard Scheffenegger 		EXIT_RECOVERY(tp->t_flags);
11610471a8c7SRichard Scheffenegger 		cc_cong_signal(tp, th, CC_NDUPACK);
11620471a8c7SRichard Scheffenegger 		/*
11630471a8c7SRichard Scheffenegger 		 * For PRR, adjust recover_fs as if this new reduction
11640471a8c7SRichard Scheffenegger 		 * initialized this variable.
11650471a8c7SRichard Scheffenegger 		 * cwnd will be adjusted by SACK or PRR processing
11660471a8c7SRichard Scheffenegger 		 * subsequently, only set it to a safe value here.
11670471a8c7SRichard Scheffenegger 		 */
11680471a8c7SRichard Scheffenegger 		tp->snd_cwnd = tcp_maxseg(tp);
11690471a8c7SRichard Scheffenegger 		tp->sackhint.recover_fs = (tp->snd_max - tp->snd_una) -
11700471a8c7SRichard Scheffenegger 					    tp->sackhint.recover_fs;
11710471a8c7SRichard Scheffenegger 	}
11720471a8c7SRichard Scheffenegger }
1173