xref: /xnu-10002.81.5/bsd/netinet/tcp_sack.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2004-2016 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions /*
29*5e3eaea3SApple OSS Distributions  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30*5e3eaea3SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
31*5e3eaea3SApple OSS Distributions  *
32*5e3eaea3SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
33*5e3eaea3SApple OSS Distributions  * modification, are permitted provided that the following conditions
34*5e3eaea3SApple OSS Distributions  * are met:
35*5e3eaea3SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
36*5e3eaea3SApple OSS Distributions  *	notice, this list of conditions and the following disclaimer.
37*5e3eaea3SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
38*5e3eaea3SApple OSS Distributions  *	notice, this list of conditions and the following disclaimer in the
39*5e3eaea3SApple OSS Distributions  *	documentation and/or other materials provided with the distribution.
40*5e3eaea3SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
41*5e3eaea3SApple OSS Distributions  *	must display the following acknowledgement:
42*5e3eaea3SApple OSS Distributions  *	This product includes software developed by the University of
43*5e3eaea3SApple OSS Distributions  *	California, Berkeley and its contributors.
44*5e3eaea3SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
45*5e3eaea3SApple OSS Distributions  *	may be used to endorse or promote products derived from this software
46*5e3eaea3SApple OSS Distributions  *	without specific prior written permission.
47*5e3eaea3SApple OSS Distributions  *
48*5e3eaea3SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49*5e3eaea3SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50*5e3eaea3SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51*5e3eaea3SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52*5e3eaea3SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53*5e3eaea3SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54*5e3eaea3SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55*5e3eaea3SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56*5e3eaea3SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57*5e3eaea3SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58*5e3eaea3SApple OSS Distributions  * SUCH DAMAGE.
59*5e3eaea3SApple OSS Distributions  *
60*5e3eaea3SApple OSS Distributions  */
61*5e3eaea3SApple OSS Distributions 
62*5e3eaea3SApple OSS Distributions #define _IP_VHL
63*5e3eaea3SApple OSS Distributions 
64*5e3eaea3SApple OSS Distributions 
65*5e3eaea3SApple OSS Distributions #include <sys/param.h>
66*5e3eaea3SApple OSS Distributions #include <sys/systm.h>
67*5e3eaea3SApple OSS Distributions #include <sys/kernel.h>
68*5e3eaea3SApple OSS Distributions #include <sys/sysctl.h>
69*5e3eaea3SApple OSS Distributions #include <sys/mbuf.h>
70*5e3eaea3SApple OSS Distributions #include <sys/domain.h>
71*5e3eaea3SApple OSS Distributions #include <sys/protosw.h>
72*5e3eaea3SApple OSS Distributions #include <sys/socket.h>
73*5e3eaea3SApple OSS Distributions #include <sys/socketvar.h>
74*5e3eaea3SApple OSS Distributions 
75*5e3eaea3SApple OSS Distributions #include <kern/zalloc.h>
76*5e3eaea3SApple OSS Distributions 
77*5e3eaea3SApple OSS Distributions #include <net/route.h>
78*5e3eaea3SApple OSS Distributions 
79*5e3eaea3SApple OSS Distributions #include <netinet/in.h>
80*5e3eaea3SApple OSS Distributions #include <netinet/in_systm.h>
81*5e3eaea3SApple OSS Distributions #include <netinet/ip.h>
82*5e3eaea3SApple OSS Distributions #include <netinet/in_pcb.h>
83*5e3eaea3SApple OSS Distributions #include <netinet/ip_var.h>
84*5e3eaea3SApple OSS Distributions #include <netinet6/in6_pcb.h>
85*5e3eaea3SApple OSS Distributions #include <netinet/ip6.h>
86*5e3eaea3SApple OSS Distributions #include <netinet6/ip6_var.h>
87*5e3eaea3SApple OSS Distributions #include <netinet/tcp.h>
88*5e3eaea3SApple OSS Distributions #include <netinet/tcp_fsm.h>
89*5e3eaea3SApple OSS Distributions #include <netinet/tcp_seq.h>
90*5e3eaea3SApple OSS Distributions #include <netinet/tcp_timer.h>
91*5e3eaea3SApple OSS Distributions #include <netinet/tcp_var.h>
92*5e3eaea3SApple OSS Distributions #include <netinet/tcpip.h>
93*5e3eaea3SApple OSS Distributions #include <netinet/tcp_cache.h>
94*5e3eaea3SApple OSS Distributions #if TCPDEBUG
95*5e3eaea3SApple OSS Distributions #include <netinet/tcp_debug.h>
96*5e3eaea3SApple OSS Distributions #endif
97*5e3eaea3SApple OSS Distributions #include <sys/kdebug.h>
98*5e3eaea3SApple OSS Distributions 
99*5e3eaea3SApple OSS Distributions #if IPSEC
100*5e3eaea3SApple OSS Distributions #include <netinet6/ipsec.h>
101*5e3eaea3SApple OSS Distributions #endif /*IPSEC*/
102*5e3eaea3SApple OSS Distributions 
103*5e3eaea3SApple OSS Distributions #include <libkern/OSAtomic.h>
104*5e3eaea3SApple OSS Distributions 
105*5e3eaea3SApple OSS Distributions SYSCTL_SKMEM_TCP_INT(OID_AUTO, sack, CTLFLAG_RW | CTLFLAG_LOCKED,
106*5e3eaea3SApple OSS Distributions     int, tcp_do_sack, 1, "Enable/Disable TCP SACK support");
107*5e3eaea3SApple OSS Distributions SYSCTL_SKMEM_TCP_INT(OID_AUTO, sack_maxholes, CTLFLAG_RW | CTLFLAG_LOCKED,
108*5e3eaea3SApple OSS Distributions     static int, tcp_sack_maxholes, 128,
109*5e3eaea3SApple OSS Distributions     "Maximum number of TCP SACK holes allowed per connection");
110*5e3eaea3SApple OSS Distributions 
111*5e3eaea3SApple OSS Distributions SYSCTL_SKMEM_TCP_INT(OID_AUTO, sack_globalmaxholes,
112*5e3eaea3SApple OSS Distributions     CTLFLAG_RW | CTLFLAG_LOCKED, static int, tcp_sack_globalmaxholes, 65536,
113*5e3eaea3SApple OSS Distributions     "Global maximum number of TCP SACK holes");
114*5e3eaea3SApple OSS Distributions 
115*5e3eaea3SApple OSS Distributions static SInt32 tcp_sack_globalholes = 0;
116*5e3eaea3SApple OSS Distributions SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack_globalholes, CTLFLAG_RD | CTLFLAG_LOCKED,
117*5e3eaea3SApple OSS Distributions     &tcp_sack_globalholes, 0,
118*5e3eaea3SApple OSS Distributions     "Global number of TCP SACK holes currently allocated");
119*5e3eaea3SApple OSS Distributions 
120*5e3eaea3SApple OSS Distributions static KALLOC_TYPE_DEFINE(sack_hole_zone, struct sackhole, NET_KT_DEFAULT);
121*5e3eaea3SApple OSS Distributions 
122*5e3eaea3SApple OSS Distributions #define TCP_VALIDATE_SACK_SEQ_NUMBERS(_tp_, _sb_, _ack_) \
123*5e3eaea3SApple OSS Distributions     (SEQ_GT((_sb_)->end, (_sb_)->start) && \
124*5e3eaea3SApple OSS Distributions     SEQ_GT((_sb_)->start, (_tp_)->snd_una) && \
125*5e3eaea3SApple OSS Distributions     SEQ_GT((_sb_)->start, (_ack_)) && \
126*5e3eaea3SApple OSS Distributions     SEQ_LT((_sb_)->start, (_tp_)->snd_max) && \
127*5e3eaea3SApple OSS Distributions     SEQ_GT((_sb_)->end, (_tp_)->snd_una) && \
128*5e3eaea3SApple OSS Distributions     SEQ_LEQ((_sb_)->end, (_tp_)->snd_max))
129*5e3eaea3SApple OSS Distributions 
130*5e3eaea3SApple OSS Distributions /*
131*5e3eaea3SApple OSS Distributions  * This function is called upon receipt of new valid data (while not in header
132*5e3eaea3SApple OSS Distributions  * prediction mode), and it updates the ordered list of sacks.
133*5e3eaea3SApple OSS Distributions  */
134*5e3eaea3SApple OSS Distributions void
tcp_update_sack_list(struct tcpcb * tp,tcp_seq rcv_start,tcp_seq rcv_end)135*5e3eaea3SApple OSS Distributions tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
136*5e3eaea3SApple OSS Distributions {
137*5e3eaea3SApple OSS Distributions 	/*
138*5e3eaea3SApple OSS Distributions 	 * First reported block MUST be the most recent one.  Subsequent
139*5e3eaea3SApple OSS Distributions 	 * blocks SHOULD be in the order in which they arrived at the
140*5e3eaea3SApple OSS Distributions 	 * receiver.  These two conditions make the implementation fully
141*5e3eaea3SApple OSS Distributions 	 * compliant with RFC 2018.
142*5e3eaea3SApple OSS Distributions 	 */
143*5e3eaea3SApple OSS Distributions 	struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
144*5e3eaea3SApple OSS Distributions 	int num_head, num_saved, i;
145*5e3eaea3SApple OSS Distributions 
146*5e3eaea3SApple OSS Distributions 	/* SACK block for the received segment. */
147*5e3eaea3SApple OSS Distributions 	head_blk.start = rcv_start;
148*5e3eaea3SApple OSS Distributions 	head_blk.end = rcv_end;
149*5e3eaea3SApple OSS Distributions 
150*5e3eaea3SApple OSS Distributions 	/*
151*5e3eaea3SApple OSS Distributions 	 * Merge updated SACK blocks into head_blk, and
152*5e3eaea3SApple OSS Distributions 	 * save unchanged SACK blocks into saved_blks[].
153*5e3eaea3SApple OSS Distributions 	 * num_saved will have the number of the saved SACK blocks.
154*5e3eaea3SApple OSS Distributions 	 */
155*5e3eaea3SApple OSS Distributions 	num_saved = 0;
156*5e3eaea3SApple OSS Distributions 	for (i = 0; i < tp->rcv_numsacks; i++) {
157*5e3eaea3SApple OSS Distributions 		tcp_seq start = tp->sackblks[i].start;
158*5e3eaea3SApple OSS Distributions 		tcp_seq end = tp->sackblks[i].end;
159*5e3eaea3SApple OSS Distributions 		if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
160*5e3eaea3SApple OSS Distributions 			/*
161*5e3eaea3SApple OSS Distributions 			 * Discard this SACK block.
162*5e3eaea3SApple OSS Distributions 			 */
163*5e3eaea3SApple OSS Distributions 		} else if (SEQ_LEQ(head_blk.start, end) &&
164*5e3eaea3SApple OSS Distributions 		    SEQ_GEQ(head_blk.end, start)) {
165*5e3eaea3SApple OSS Distributions 			/*
166*5e3eaea3SApple OSS Distributions 			 * Merge this SACK block into head_blk.
167*5e3eaea3SApple OSS Distributions 			 * This SACK block itself will be discarded.
168*5e3eaea3SApple OSS Distributions 			 */
169*5e3eaea3SApple OSS Distributions 			if (SEQ_GT(head_blk.start, start)) {
170*5e3eaea3SApple OSS Distributions 				head_blk.start = start;
171*5e3eaea3SApple OSS Distributions 			}
172*5e3eaea3SApple OSS Distributions 			if (SEQ_LT(head_blk.end, end)) {
173*5e3eaea3SApple OSS Distributions 				head_blk.end = end;
174*5e3eaea3SApple OSS Distributions 			}
175*5e3eaea3SApple OSS Distributions 		} else {
176*5e3eaea3SApple OSS Distributions 			/*
177*5e3eaea3SApple OSS Distributions 			 * Save this SACK block.
178*5e3eaea3SApple OSS Distributions 			 */
179*5e3eaea3SApple OSS Distributions 			saved_blks[num_saved].start = start;
180*5e3eaea3SApple OSS Distributions 			saved_blks[num_saved].end = end;
181*5e3eaea3SApple OSS Distributions 			num_saved++;
182*5e3eaea3SApple OSS Distributions 		}
183*5e3eaea3SApple OSS Distributions 	}
184*5e3eaea3SApple OSS Distributions 
185*5e3eaea3SApple OSS Distributions 	/*
186*5e3eaea3SApple OSS Distributions 	 * Update SACK list in tp->sackblks[].
187*5e3eaea3SApple OSS Distributions 	 */
188*5e3eaea3SApple OSS Distributions 	num_head = 0;
189*5e3eaea3SApple OSS Distributions 	if (SEQ_GT(head_blk.start, tp->rcv_nxt)) {
190*5e3eaea3SApple OSS Distributions 		/*
191*5e3eaea3SApple OSS Distributions 		 * The received data segment is an out-of-order segment.
192*5e3eaea3SApple OSS Distributions 		 * Put head_blk at the top of SACK list.
193*5e3eaea3SApple OSS Distributions 		 */
194*5e3eaea3SApple OSS Distributions 		tp->sackblks[0] = head_blk;
195*5e3eaea3SApple OSS Distributions 		num_head = 1;
196*5e3eaea3SApple OSS Distributions 		/*
197*5e3eaea3SApple OSS Distributions 		 * If the number of saved SACK blocks exceeds its limit,
198*5e3eaea3SApple OSS Distributions 		 * discard the last SACK block.
199*5e3eaea3SApple OSS Distributions 		 */
200*5e3eaea3SApple OSS Distributions 		if (num_saved >= MAX_SACK_BLKS) {
201*5e3eaea3SApple OSS Distributions 			num_saved--;
202*5e3eaea3SApple OSS Distributions 		}
203*5e3eaea3SApple OSS Distributions 	}
204*5e3eaea3SApple OSS Distributions 	if (num_saved > 0) {
205*5e3eaea3SApple OSS Distributions 		/*
206*5e3eaea3SApple OSS Distributions 		 * Copy the saved SACK blocks back.
207*5e3eaea3SApple OSS Distributions 		 */
208*5e3eaea3SApple OSS Distributions 		bcopy(saved_blks, &tp->sackblks[num_head], sizeof(struct sackblk) * num_saved);
209*5e3eaea3SApple OSS Distributions 	}
210*5e3eaea3SApple OSS Distributions 
211*5e3eaea3SApple OSS Distributions 	/* Save the number of SACK blocks. */
212*5e3eaea3SApple OSS Distributions 	tp->rcv_numsacks = num_head + num_saved;
213*5e3eaea3SApple OSS Distributions 
214*5e3eaea3SApple OSS Distributions 	/* If we are requesting SACK recovery, reset the stretch-ack state
215*5e3eaea3SApple OSS Distributions 	 * so that connection will generate more acks after recovery and
216*5e3eaea3SApple OSS Distributions 	 * sender's cwnd will open.
217*5e3eaea3SApple OSS Distributions 	 */
218*5e3eaea3SApple OSS Distributions 	if ((tp->t_flags & TF_STRETCHACK) != 0 && tp->rcv_numsacks > 0) {
219*5e3eaea3SApple OSS Distributions 		tcp_reset_stretch_ack(tp);
220*5e3eaea3SApple OSS Distributions 	}
221*5e3eaea3SApple OSS Distributions 	if (tp->rcv_numsacks > 0) {
222*5e3eaea3SApple OSS Distributions 		tp->t_forced_acks = TCP_FORCED_ACKS_COUNT;
223*5e3eaea3SApple OSS Distributions 	}
224*5e3eaea3SApple OSS Distributions 
225*5e3eaea3SApple OSS Distributions #if TRAFFIC_MGT
226*5e3eaea3SApple OSS Distributions 	if (tp->acc_iaj > 0 && tp->rcv_numsacks > 0) {
227*5e3eaea3SApple OSS Distributions 		reset_acc_iaj(tp);
228*5e3eaea3SApple OSS Distributions 	}
229*5e3eaea3SApple OSS Distributions #endif /* TRAFFIC_MGT */
230*5e3eaea3SApple OSS Distributions }
231*5e3eaea3SApple OSS Distributions 
232*5e3eaea3SApple OSS Distributions /*
233*5e3eaea3SApple OSS Distributions  * Delete all receiver-side SACK information.
234*5e3eaea3SApple OSS Distributions  */
235*5e3eaea3SApple OSS Distributions void
tcp_clean_sackreport(struct tcpcb * tp)236*5e3eaea3SApple OSS Distributions tcp_clean_sackreport( struct tcpcb *tp)
237*5e3eaea3SApple OSS Distributions {
238*5e3eaea3SApple OSS Distributions 	tp->rcv_numsacks = 0;
239*5e3eaea3SApple OSS Distributions 	bzero(&tp->sackblks[0], sizeof(struct sackblk) * MAX_SACK_BLKS);
240*5e3eaea3SApple OSS Distributions }
241*5e3eaea3SApple OSS Distributions 
242*5e3eaea3SApple OSS Distributions /*
243*5e3eaea3SApple OSS Distributions  * Allocate struct sackhole.
244*5e3eaea3SApple OSS Distributions  */
245*5e3eaea3SApple OSS Distributions static struct sackhole *
tcp_sackhole_alloc(struct tcpcb * tp,tcp_seq start,tcp_seq end)246*5e3eaea3SApple OSS Distributions tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
247*5e3eaea3SApple OSS Distributions {
248*5e3eaea3SApple OSS Distributions 	struct sackhole *hole;
249*5e3eaea3SApple OSS Distributions 
250*5e3eaea3SApple OSS Distributions 	if (tp->snd_numholes >= tcp_sack_maxholes ||
251*5e3eaea3SApple OSS Distributions 	    tcp_sack_globalholes >= tcp_sack_globalmaxholes) {
252*5e3eaea3SApple OSS Distributions 		tcpstat.tcps_sack_sboverflow++;
253*5e3eaea3SApple OSS Distributions 		return NULL;
254*5e3eaea3SApple OSS Distributions 	}
255*5e3eaea3SApple OSS Distributions 
256*5e3eaea3SApple OSS Distributions 	hole = zalloc_flags(sack_hole_zone, Z_WAITOK | Z_NOFAIL);
257*5e3eaea3SApple OSS Distributions 
258*5e3eaea3SApple OSS Distributions 	hole->start = start;
259*5e3eaea3SApple OSS Distributions 	hole->end = end;
260*5e3eaea3SApple OSS Distributions 	hole->rxmit = start;
261*5e3eaea3SApple OSS Distributions 
262*5e3eaea3SApple OSS Distributions 	tp->snd_numholes++;
263*5e3eaea3SApple OSS Distributions 	OSIncrementAtomic(&tcp_sack_globalholes);
264*5e3eaea3SApple OSS Distributions 
265*5e3eaea3SApple OSS Distributions 	return hole;
266*5e3eaea3SApple OSS Distributions }
267*5e3eaea3SApple OSS Distributions 
268*5e3eaea3SApple OSS Distributions /*
269*5e3eaea3SApple OSS Distributions  * Free struct sackhole.
270*5e3eaea3SApple OSS Distributions  */
271*5e3eaea3SApple OSS Distributions static void
tcp_sackhole_free(struct tcpcb * tp,struct sackhole * hole)272*5e3eaea3SApple OSS Distributions tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
273*5e3eaea3SApple OSS Distributions {
274*5e3eaea3SApple OSS Distributions 	zfree(sack_hole_zone, hole);
275*5e3eaea3SApple OSS Distributions 
276*5e3eaea3SApple OSS Distributions 	tp->snd_numholes--;
277*5e3eaea3SApple OSS Distributions 	OSDecrementAtomic(&tcp_sack_globalholes);
278*5e3eaea3SApple OSS Distributions }
279*5e3eaea3SApple OSS Distributions 
280*5e3eaea3SApple OSS Distributions /*
281*5e3eaea3SApple OSS Distributions  * Insert new SACK hole into scoreboard.
282*5e3eaea3SApple OSS Distributions  */
283*5e3eaea3SApple OSS Distributions static struct sackhole *
tcp_sackhole_insert(struct tcpcb * tp,tcp_seq start,tcp_seq end,struct sackhole * after)284*5e3eaea3SApple OSS Distributions tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
285*5e3eaea3SApple OSS Distributions     struct sackhole *after)
286*5e3eaea3SApple OSS Distributions {
287*5e3eaea3SApple OSS Distributions 	struct sackhole *hole;
288*5e3eaea3SApple OSS Distributions 
289*5e3eaea3SApple OSS Distributions 	/* Allocate a new SACK hole. */
290*5e3eaea3SApple OSS Distributions 	hole = tcp_sackhole_alloc(tp, start, end);
291*5e3eaea3SApple OSS Distributions 	if (hole == NULL) {
292*5e3eaea3SApple OSS Distributions 		return NULL;
293*5e3eaea3SApple OSS Distributions 	}
294*5e3eaea3SApple OSS Distributions 	hole->rxmit_start = tcp_now;
295*5e3eaea3SApple OSS Distributions 	/* Insert the new SACK hole into scoreboard */
296*5e3eaea3SApple OSS Distributions 	if (after != NULL) {
297*5e3eaea3SApple OSS Distributions 		TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
298*5e3eaea3SApple OSS Distributions 	} else {
299*5e3eaea3SApple OSS Distributions 		TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
300*5e3eaea3SApple OSS Distributions 	}
301*5e3eaea3SApple OSS Distributions 
302*5e3eaea3SApple OSS Distributions 	/* Update SACK hint. */
303*5e3eaea3SApple OSS Distributions 	if (tp->sackhint.nexthole == NULL) {
304*5e3eaea3SApple OSS Distributions 		tp->sackhint.nexthole = hole;
305*5e3eaea3SApple OSS Distributions 	}
306*5e3eaea3SApple OSS Distributions 
307*5e3eaea3SApple OSS Distributions 	return hole;
308*5e3eaea3SApple OSS Distributions }
309*5e3eaea3SApple OSS Distributions 
310*5e3eaea3SApple OSS Distributions /*
311*5e3eaea3SApple OSS Distributions  * Remove SACK hole from scoreboard.
312*5e3eaea3SApple OSS Distributions  */
313*5e3eaea3SApple OSS Distributions static void
tcp_sackhole_remove(struct tcpcb * tp,struct sackhole * hole)314*5e3eaea3SApple OSS Distributions tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
315*5e3eaea3SApple OSS Distributions {
316*5e3eaea3SApple OSS Distributions 	/* Update SACK hint. */
317*5e3eaea3SApple OSS Distributions 	if (tp->sackhint.nexthole == hole) {
318*5e3eaea3SApple OSS Distributions 		tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
319*5e3eaea3SApple OSS Distributions 	}
320*5e3eaea3SApple OSS Distributions 
321*5e3eaea3SApple OSS Distributions 	/* Remove this SACK hole. */
322*5e3eaea3SApple OSS Distributions 	TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
323*5e3eaea3SApple OSS Distributions 
324*5e3eaea3SApple OSS Distributions 	/* Free this SACK hole. */
325*5e3eaea3SApple OSS Distributions 	tcp_sackhole_free(tp, hole);
326*5e3eaea3SApple OSS Distributions }
327*5e3eaea3SApple OSS Distributions /*
328*5e3eaea3SApple OSS Distributions  * When a new ack with SACK is received, check if it indicates packet
329*5e3eaea3SApple OSS Distributions  * reordering. If there is packet reordering, the socket is marked and
330*5e3eaea3SApple OSS Distributions  * the late time offset by which the packet was reordered with
331*5e3eaea3SApple OSS Distributions  * respect to its closest neighboring packets is computed.
332*5e3eaea3SApple OSS Distributions  */
333*5e3eaea3SApple OSS Distributions static void
tcp_sack_detect_reordering(struct tcpcb * tp,struct sackhole * s,tcp_seq sacked_seq,tcp_seq snd_fack)334*5e3eaea3SApple OSS Distributions tcp_sack_detect_reordering(struct tcpcb *tp, struct sackhole *s,
335*5e3eaea3SApple OSS Distributions     tcp_seq sacked_seq, tcp_seq snd_fack)
336*5e3eaea3SApple OSS Distributions {
337*5e3eaea3SApple OSS Distributions 	int32_t rext = 0, reordered = 0;
338*5e3eaea3SApple OSS Distributions 
339*5e3eaea3SApple OSS Distributions 	/*
340*5e3eaea3SApple OSS Distributions 	 * If the SACK hole is past snd_fack, this is from new SACK
341*5e3eaea3SApple OSS Distributions 	 * information, so we can ignore it.
342*5e3eaea3SApple OSS Distributions 	 */
343*5e3eaea3SApple OSS Distributions 	if (SEQ_GT(s->end, snd_fack)) {
344*5e3eaea3SApple OSS Distributions 		return;
345*5e3eaea3SApple OSS Distributions 	}
346*5e3eaea3SApple OSS Distributions 	/*
347*5e3eaea3SApple OSS Distributions 	 * If there has been a retransmit timeout, then the timestamp on
348*5e3eaea3SApple OSS Distributions 	 * the SACK segment will be newer. This might lead to a
349*5e3eaea3SApple OSS Distributions 	 * false-positive. Avoid re-ordering detection in this case.
350*5e3eaea3SApple OSS Distributions 	 */
351*5e3eaea3SApple OSS Distributions 	if (tp->t_rxtshift > 0) {
352*5e3eaea3SApple OSS Distributions 		return;
353*5e3eaea3SApple OSS Distributions 	}
354*5e3eaea3SApple OSS Distributions 
355*5e3eaea3SApple OSS Distributions 	/*
356*5e3eaea3SApple OSS Distributions 	 * Detect reordering from SACK information by checking
357*5e3eaea3SApple OSS Distributions 	 * if recently sacked data was never retransmitted from this hole.
358*5e3eaea3SApple OSS Distributions 	 *
359*5e3eaea3SApple OSS Distributions 	 * First, we look for the byte in the list of retransmitted segments. This one
360*5e3eaea3SApple OSS Distributions 	 * will contain even the segments that are retransmitted thanks to RTO/TLP.
361*5e3eaea3SApple OSS Distributions 	 *
362*5e3eaea3SApple OSS Distributions 	 * Then, we check the sackhole which indicates whether or not the sackhole
363*5e3eaea3SApple OSS Distributions 	 * was subject to retransmission.
364*5e3eaea3SApple OSS Distributions 	 */
365*5e3eaea3SApple OSS Distributions 	if (SEQ_LT(s->rxmit, sacked_seq) &&
366*5e3eaea3SApple OSS Distributions 	    (!tcp_do_better_lr || tcp_rxtseg_find(tp, sacked_seq - 1, sacked_seq - 1) == NULL)) {
367*5e3eaea3SApple OSS Distributions 		reordered = 1;
368*5e3eaea3SApple OSS Distributions 		tcpstat.tcps_avoid_rxmt++;
369*5e3eaea3SApple OSS Distributions 	}
370*5e3eaea3SApple OSS Distributions 
371*5e3eaea3SApple OSS Distributions 	if (reordered) {
372*5e3eaea3SApple OSS Distributions 		if (!(tp->t_flagsext & TF_PKTS_REORDERED)) {
373*5e3eaea3SApple OSS Distributions 			tp->t_flagsext |= TF_PKTS_REORDERED;
374*5e3eaea3SApple OSS Distributions 			tcpstat.tcps_detect_reordering++;
375*5e3eaea3SApple OSS Distributions 		}
376*5e3eaea3SApple OSS Distributions 
377*5e3eaea3SApple OSS Distributions 		tcpstat.tcps_reordered_pkts++;
378*5e3eaea3SApple OSS Distributions 		tp->t_reordered_pkts++;
379*5e3eaea3SApple OSS Distributions 
380*5e3eaea3SApple OSS Distributions 		/*
381*5e3eaea3SApple OSS Distributions 		 * If reordering is seen on a connection wth ECN enabled,
382*5e3eaea3SApple OSS Distributions 		 * increment the heuristic
383*5e3eaea3SApple OSS Distributions 		 */
384*5e3eaea3SApple OSS Distributions 		if (TCP_ECN_ENABLED(tp)) {
385*5e3eaea3SApple OSS Distributions 			INP_INC_IFNET_STAT(tp->t_inpcb, ecn_fallback_reorder);
386*5e3eaea3SApple OSS Distributions 			tcpstat.tcps_ecn_fallback_reorder++;
387*5e3eaea3SApple OSS Distributions 			tcp_heuristic_ecn_aggressive(tp);
388*5e3eaea3SApple OSS Distributions 		}
389*5e3eaea3SApple OSS Distributions 
390*5e3eaea3SApple OSS Distributions 		VERIFY(SEQ_GEQ(snd_fack, s->rxmit));
391*5e3eaea3SApple OSS Distributions 
392*5e3eaea3SApple OSS Distributions 		if (s->rxmit_start > 0) {
393*5e3eaea3SApple OSS Distributions 			rext = timer_diff(tcp_now, 0, s->rxmit_start, 0);
394*5e3eaea3SApple OSS Distributions 			if (rext < 0) {
395*5e3eaea3SApple OSS Distributions 				return;
396*5e3eaea3SApple OSS Distributions 			}
397*5e3eaea3SApple OSS Distributions 
398*5e3eaea3SApple OSS Distributions 			/*
399*5e3eaea3SApple OSS Distributions 			 * We take the maximum reorder window to schedule
400*5e3eaea3SApple OSS Distributions 			 * DELAYFR timer as that will take care of jitter
401*5e3eaea3SApple OSS Distributions 			 * on the network path.
402*5e3eaea3SApple OSS Distributions 			 *
403*5e3eaea3SApple OSS Distributions 			 * Computing average and standard deviation seems
404*5e3eaea3SApple OSS Distributions 			 * to cause unnecessary retransmissions when there
405*5e3eaea3SApple OSS Distributions 			 * is high jitter.
406*5e3eaea3SApple OSS Distributions 			 *
407*5e3eaea3SApple OSS Distributions 			 * We set a maximum of SRTT/2 and a minimum of
408*5e3eaea3SApple OSS Distributions 			 * 10 ms on the reorder window.
409*5e3eaea3SApple OSS Distributions 			 */
410*5e3eaea3SApple OSS Distributions 			tp->t_reorderwin = max(tp->t_reorderwin, rext);
411*5e3eaea3SApple OSS Distributions 			tp->t_reorderwin = min(tp->t_reorderwin,
412*5e3eaea3SApple OSS Distributions 			    (tp->t_srtt >> (TCP_RTT_SHIFT - 1)));
413*5e3eaea3SApple OSS Distributions 			tp->t_reorderwin = max(tp->t_reorderwin, 10);
414*5e3eaea3SApple OSS Distributions 		}
415*5e3eaea3SApple OSS Distributions 	}
416*5e3eaea3SApple OSS Distributions }
417*5e3eaea3SApple OSS Distributions 
418*5e3eaea3SApple OSS Distributions static void
tcp_sack_update_byte_counter(struct tcpcb * tp,uint32_t start,uint32_t end,uint32_t * newbytes_acked,uint32_t * towards_fr_acked)419*5e3eaea3SApple OSS Distributions tcp_sack_update_byte_counter(struct tcpcb *tp, uint32_t start, uint32_t end,
420*5e3eaea3SApple OSS Distributions     uint32_t *newbytes_acked, uint32_t *towards_fr_acked)
421*5e3eaea3SApple OSS Distributions {
422*5e3eaea3SApple OSS Distributions 	*newbytes_acked += (end - start);
423*5e3eaea3SApple OSS Distributions 	if (SEQ_GEQ(start, tp->send_highest_sack)) {
424*5e3eaea3SApple OSS Distributions 		*towards_fr_acked += (end - start);
425*5e3eaea3SApple OSS Distributions 	}
426*5e3eaea3SApple OSS Distributions }
427*5e3eaea3SApple OSS Distributions 
428*5e3eaea3SApple OSS Distributions /*
429*5e3eaea3SApple OSS Distributions  * Process cumulative ACK and the TCP SACK option to update the scoreboard.
430*5e3eaea3SApple OSS Distributions  * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
431*5e3eaea3SApple OSS Distributions  * the sequence space).
432*5e3eaea3SApple OSS Distributions  */
433*5e3eaea3SApple OSS Distributions void
tcp_sack_doack(struct tcpcb * tp,struct tcpopt * to,struct tcphdr * th,u_int32_t * newbytes_acked,uint32_t * after_rexmit_acked)434*5e3eaea3SApple OSS Distributions tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th,
435*5e3eaea3SApple OSS Distributions     u_int32_t *newbytes_acked, uint32_t *after_rexmit_acked)
436*5e3eaea3SApple OSS Distributions {
437*5e3eaea3SApple OSS Distributions 	struct sackhole *cur, *temp;
438*5e3eaea3SApple OSS Distributions 	struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp;
439*5e3eaea3SApple OSS Distributions 	int i, j, num_sack_blks;
440*5e3eaea3SApple OSS Distributions 	tcp_seq old_snd_fack = 0, th_ack = th->th_ack;
441*5e3eaea3SApple OSS Distributions 
442*5e3eaea3SApple OSS Distributions 	num_sack_blks = 0;
443*5e3eaea3SApple OSS Distributions 	/*
444*5e3eaea3SApple OSS Distributions 	 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
445*5e3eaea3SApple OSS Distributions 	 * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
446*5e3eaea3SApple OSS Distributions 	 */
447*5e3eaea3SApple OSS Distributions 	if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
448*5e3eaea3SApple OSS Distributions 		sack_blocks[num_sack_blks].start = tp->snd_una;
449*5e3eaea3SApple OSS Distributions 		sack_blocks[num_sack_blks++].end = th_ack;
450*5e3eaea3SApple OSS Distributions 	}
451*5e3eaea3SApple OSS Distributions 	/*
452*5e3eaea3SApple OSS Distributions 	 * Append received valid SACK blocks to sack_blocks[].
453*5e3eaea3SApple OSS Distributions 	 * Check that the SACK block range is valid.
454*5e3eaea3SApple OSS Distributions 	 */
455*5e3eaea3SApple OSS Distributions 	for (i = 0; i < to->to_nsacks; i++) {
456*5e3eaea3SApple OSS Distributions 		bcopy((to->to_sacks + i * TCPOLEN_SACK),
457*5e3eaea3SApple OSS Distributions 		    &sack, sizeof(sack));
458*5e3eaea3SApple OSS Distributions 		sack.start = ntohl(sack.start);
459*5e3eaea3SApple OSS Distributions 		sack.end = ntohl(sack.end);
460*5e3eaea3SApple OSS Distributions 		if (TCP_VALIDATE_SACK_SEQ_NUMBERS(tp, &sack, th_ack)) {
461*5e3eaea3SApple OSS Distributions 			sack_blocks[num_sack_blks++] = sack;
462*5e3eaea3SApple OSS Distributions 		}
463*5e3eaea3SApple OSS Distributions 	}
464*5e3eaea3SApple OSS Distributions 
465*5e3eaea3SApple OSS Distributions 	/*
466*5e3eaea3SApple OSS Distributions 	 * Return if SND.UNA is not advanced and no valid SACK block
467*5e3eaea3SApple OSS Distributions 	 * is received.
468*5e3eaea3SApple OSS Distributions 	 */
469*5e3eaea3SApple OSS Distributions 	if (num_sack_blks == 0) {
470*5e3eaea3SApple OSS Distributions 		return;
471*5e3eaea3SApple OSS Distributions 	}
472*5e3eaea3SApple OSS Distributions 
473*5e3eaea3SApple OSS Distributions 	VERIFY(num_sack_blks <= (TCP_MAX_SACK + 1));
474*5e3eaea3SApple OSS Distributions 	/*
475*5e3eaea3SApple OSS Distributions 	 * Sort the SACK blocks so we can update the scoreboard
476*5e3eaea3SApple OSS Distributions 	 * with just one pass. The overhead of sorting upto 4+1 elements
477*5e3eaea3SApple OSS Distributions 	 * is less than making upto 4+1 passes over the scoreboard.
478*5e3eaea3SApple OSS Distributions 	 */
479*5e3eaea3SApple OSS Distributions 	for (i = 0; i < num_sack_blks; i++) {
480*5e3eaea3SApple OSS Distributions 		for (j = i + 1; j < num_sack_blks; j++) {
481*5e3eaea3SApple OSS Distributions 			if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
482*5e3eaea3SApple OSS Distributions 				sack = sack_blocks[i];
483*5e3eaea3SApple OSS Distributions 				sack_blocks[i] = sack_blocks[j];
484*5e3eaea3SApple OSS Distributions 				sack_blocks[j] = sack;
485*5e3eaea3SApple OSS Distributions 			}
486*5e3eaea3SApple OSS Distributions 		}
487*5e3eaea3SApple OSS Distributions 	}
488*5e3eaea3SApple OSS Distributions 	if (TAILQ_EMPTY(&tp->snd_holes)) {
489*5e3eaea3SApple OSS Distributions 		/*
490*5e3eaea3SApple OSS Distributions 		 * Empty scoreboard. Need to initialize snd_fack (it may be
491*5e3eaea3SApple OSS Distributions 		 * uninitialized or have a bogus value). Scoreboard holes
492*5e3eaea3SApple OSS Distributions 		 * (from the sack blocks received) are created later below (in
493*5e3eaea3SApple OSS Distributions 		 * the logic that adds holes to the tail of the scoreboard).
494*5e3eaea3SApple OSS Distributions 		 */
495*5e3eaea3SApple OSS Distributions 		tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
496*5e3eaea3SApple OSS Distributions 	}
497*5e3eaea3SApple OSS Distributions 
498*5e3eaea3SApple OSS Distributions 	old_snd_fack = tp->snd_fack;
499*5e3eaea3SApple OSS Distributions 	/*
500*5e3eaea3SApple OSS Distributions 	 * In the while-loop below, incoming SACK blocks (sack_blocks[])
501*5e3eaea3SApple OSS Distributions 	 * and SACK holes (snd_holes) are traversed from their tails with
502*5e3eaea3SApple OSS Distributions 	 * just one pass in order to reduce the number of compares especially
503*5e3eaea3SApple OSS Distributions 	 * when the bandwidth-delay product is large.
504*5e3eaea3SApple OSS Distributions 	 * Note: Typically, in the first RTT of SACK recovery, the highest
505*5e3eaea3SApple OSS Distributions 	 * three or four SACK blocks with the same ack number are received.
506*5e3eaea3SApple OSS Distributions 	 * In the second RTT, if retransmitted data segments are not lost,
507*5e3eaea3SApple OSS Distributions 	 * the highest three or four SACK blocks with ack number advancing
508*5e3eaea3SApple OSS Distributions 	 * are received.
509*5e3eaea3SApple OSS Distributions 	 */
510*5e3eaea3SApple OSS Distributions 	sblkp = &sack_blocks[num_sack_blks - 1];        /* Last SACK block */
511*5e3eaea3SApple OSS Distributions 	if (SEQ_LT(tp->snd_fack, sblkp->start)) {
512*5e3eaea3SApple OSS Distributions 		/*
513*5e3eaea3SApple OSS Distributions 		 * The highest SACK block is beyond fack.
514*5e3eaea3SApple OSS Distributions 		 * Append new SACK hole at the tail.
515*5e3eaea3SApple OSS Distributions 		 * If the second or later highest SACK blocks are also
516*5e3eaea3SApple OSS Distributions 		 * beyond the current fack, they will be inserted by
517*5e3eaea3SApple OSS Distributions 		 * way of hole splitting in the while-loop below.
518*5e3eaea3SApple OSS Distributions 		 */
519*5e3eaea3SApple OSS Distributions 		temp = tcp_sackhole_insert(tp, tp->snd_fack, sblkp->start, NULL);
520*5e3eaea3SApple OSS Distributions 		if (temp != NULL) {
521*5e3eaea3SApple OSS Distributions 			tp->snd_fack = sblkp->end;
522*5e3eaea3SApple OSS Distributions 			tcp_sack_update_byte_counter(tp, sblkp->start, sblkp->end, newbytes_acked, after_rexmit_acked);
523*5e3eaea3SApple OSS Distributions 
524*5e3eaea3SApple OSS Distributions 			/* Go to the previous sack block. */
525*5e3eaea3SApple OSS Distributions 			sblkp--;
526*5e3eaea3SApple OSS Distributions 		} else {
527*5e3eaea3SApple OSS Distributions 			/*
528*5e3eaea3SApple OSS Distributions 			 * We failed to add a new hole based on the current
529*5e3eaea3SApple OSS Distributions 			 * sack block.  Skip over all the sack blocks that
530*5e3eaea3SApple OSS Distributions 			 * fall completely to the right of snd_fack and proceed
531*5e3eaea3SApple OSS Distributions 			 * to trim the scoreboard based on the remaining sack
532*5e3eaea3SApple OSS Distributions 			 * blocks. This also trims the scoreboard for th_ack
533*5e3eaea3SApple OSS Distributions 			 * (which is sack_blocks[0]).
534*5e3eaea3SApple OSS Distributions 			 */
535*5e3eaea3SApple OSS Distributions 			while (sblkp >= sack_blocks &&
536*5e3eaea3SApple OSS Distributions 			    SEQ_LT(tp->snd_fack, sblkp->start)) {
537*5e3eaea3SApple OSS Distributions 				sblkp--;
538*5e3eaea3SApple OSS Distributions 			}
539*5e3eaea3SApple OSS Distributions 			if (sblkp >= sack_blocks &&
540*5e3eaea3SApple OSS Distributions 			    SEQ_LT(tp->snd_fack, sblkp->end)) {
541*5e3eaea3SApple OSS Distributions 				tcp_sack_update_byte_counter(tp, tp->snd_fack, sblkp->end, newbytes_acked, after_rexmit_acked);
542*5e3eaea3SApple OSS Distributions 				tp->snd_fack = sblkp->end;
543*5e3eaea3SApple OSS Distributions 			}
544*5e3eaea3SApple OSS Distributions 		}
545*5e3eaea3SApple OSS Distributions 	} else if (SEQ_LT(tp->snd_fack, sblkp->end)) {
546*5e3eaea3SApple OSS Distributions 		/* fack is advanced. */
547*5e3eaea3SApple OSS Distributions 		tcp_sack_update_byte_counter(tp, tp->snd_fack, sblkp->end, newbytes_acked, after_rexmit_acked);
548*5e3eaea3SApple OSS Distributions 		tp->snd_fack = sblkp->end;
549*5e3eaea3SApple OSS Distributions 	}
550*5e3eaea3SApple OSS Distributions 	/* We must have at least one SACK hole in scoreboard */
551*5e3eaea3SApple OSS Distributions 	cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole */
552*5e3eaea3SApple OSS Distributions 	/*
553*5e3eaea3SApple OSS Distributions 	 * Since the incoming sack blocks are sorted, we can process them
554*5e3eaea3SApple OSS Distributions 	 * making one sweep of the scoreboard.
555*5e3eaea3SApple OSS Distributions 	 */
556*5e3eaea3SApple OSS Distributions 	while (sblkp >= sack_blocks && cur != NULL) {
557*5e3eaea3SApple OSS Distributions 		if (SEQ_GEQ(sblkp->start, cur->end)) {
558*5e3eaea3SApple OSS Distributions 			/*
559*5e3eaea3SApple OSS Distributions 			 * SACKs data beyond the current hole.
560*5e3eaea3SApple OSS Distributions 			 * Go to the previous sack block.
561*5e3eaea3SApple OSS Distributions 			 */
562*5e3eaea3SApple OSS Distributions 			sblkp--;
563*5e3eaea3SApple OSS Distributions 			continue;
564*5e3eaea3SApple OSS Distributions 		}
565*5e3eaea3SApple OSS Distributions 		if (SEQ_LEQ(sblkp->end, cur->start)) {
566*5e3eaea3SApple OSS Distributions 			/*
567*5e3eaea3SApple OSS Distributions 			 * SACKs data before the current hole.
568*5e3eaea3SApple OSS Distributions 			 * Go to the previous hole.
569*5e3eaea3SApple OSS Distributions 			 */
570*5e3eaea3SApple OSS Distributions 			cur = TAILQ_PREV(cur, sackhole_head, scblink);
571*5e3eaea3SApple OSS Distributions 			continue;
572*5e3eaea3SApple OSS Distributions 		}
573*5e3eaea3SApple OSS Distributions 		tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
574*5e3eaea3SApple OSS Distributions 		if (tp->sackhint.sack_bytes_rexmit < 0) {
575*5e3eaea3SApple OSS Distributions 			tp->sackhint.sack_bytes_rexmit = 0;
576*5e3eaea3SApple OSS Distributions 		}
577*5e3eaea3SApple OSS Distributions 
578*5e3eaea3SApple OSS Distributions 		if (SEQ_LEQ(sblkp->start, cur->start)) {
579*5e3eaea3SApple OSS Distributions 			/* Data acks at least the beginning of hole */
580*5e3eaea3SApple OSS Distributions 			if (SEQ_GEQ(sblkp->end, cur->end)) {
581*5e3eaea3SApple OSS Distributions 				/* Acks entire hole, so delete hole */
582*5e3eaea3SApple OSS Distributions 				tcp_sack_update_byte_counter(tp, cur->start, cur->end, newbytes_acked, after_rexmit_acked);
583*5e3eaea3SApple OSS Distributions 
584*5e3eaea3SApple OSS Distributions 				tcp_sack_detect_reordering(tp, cur,
585*5e3eaea3SApple OSS Distributions 				    cur->end, old_snd_fack);
586*5e3eaea3SApple OSS Distributions 				temp = cur;
587*5e3eaea3SApple OSS Distributions 				cur = TAILQ_PREV(cur, sackhole_head, scblink);
588*5e3eaea3SApple OSS Distributions 				tcp_sackhole_remove(tp, temp);
589*5e3eaea3SApple OSS Distributions 				/*
590*5e3eaea3SApple OSS Distributions 				 * The sack block may ack all or part of the next
591*5e3eaea3SApple OSS Distributions 				 * hole too, so continue onto the next hole.
592*5e3eaea3SApple OSS Distributions 				 */
593*5e3eaea3SApple OSS Distributions 				continue;
594*5e3eaea3SApple OSS Distributions 			} else {
595*5e3eaea3SApple OSS Distributions 				/* Move start of hole forward */
596*5e3eaea3SApple OSS Distributions 				tcp_sack_update_byte_counter(tp, cur->start, sblkp->end, newbytes_acked, after_rexmit_acked);
597*5e3eaea3SApple OSS Distributions 				tcp_sack_detect_reordering(tp, cur,
598*5e3eaea3SApple OSS Distributions 				    sblkp->end, old_snd_fack);
599*5e3eaea3SApple OSS Distributions 				cur->start = sblkp->end;
600*5e3eaea3SApple OSS Distributions 				cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
601*5e3eaea3SApple OSS Distributions 			}
602*5e3eaea3SApple OSS Distributions 		} else {
603*5e3eaea3SApple OSS Distributions 			/* Data acks at least the end of hole */
604*5e3eaea3SApple OSS Distributions 			if (SEQ_GEQ(sblkp->end, cur->end)) {
605*5e3eaea3SApple OSS Distributions 				/* Move end of hole backward */
606*5e3eaea3SApple OSS Distributions 				tcp_sack_update_byte_counter(tp, sblkp->start, cur->end, newbytes_acked, after_rexmit_acked);
607*5e3eaea3SApple OSS Distributions 				tcp_sack_detect_reordering(tp, cur,
608*5e3eaea3SApple OSS Distributions 				    cur->end, old_snd_fack);
609*5e3eaea3SApple OSS Distributions 				cur->end = sblkp->start;
610*5e3eaea3SApple OSS Distributions 				cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
611*5e3eaea3SApple OSS Distributions 			} else {
612*5e3eaea3SApple OSS Distributions 				/*
613*5e3eaea3SApple OSS Distributions 				 * ACKs some data in the middle of a hole;
614*5e3eaea3SApple OSS Distributions 				 * need to split current hole
615*5e3eaea3SApple OSS Distributions 				 */
616*5e3eaea3SApple OSS Distributions 				tcp_sack_detect_reordering(tp, cur,
617*5e3eaea3SApple OSS Distributions 				    sblkp->end, old_snd_fack);
618*5e3eaea3SApple OSS Distributions 				temp = tcp_sackhole_insert(tp, sblkp->end,
619*5e3eaea3SApple OSS Distributions 				    cur->end, cur);
620*5e3eaea3SApple OSS Distributions 				if (temp != NULL) {
621*5e3eaea3SApple OSS Distributions 					tcp_sack_update_byte_counter(tp, sblkp->start, sblkp->end, newbytes_acked, after_rexmit_acked);
622*5e3eaea3SApple OSS Distributions 					if (SEQ_GT(cur->rxmit, temp->rxmit)) {
623*5e3eaea3SApple OSS Distributions 						temp->rxmit = cur->rxmit;
624*5e3eaea3SApple OSS Distributions 						tp->sackhint.sack_bytes_rexmit
625*5e3eaea3SApple OSS Distributions 						        += (temp->rxmit
626*5e3eaea3SApple OSS Distributions 						    - temp->start);
627*5e3eaea3SApple OSS Distributions 					}
628*5e3eaea3SApple OSS Distributions 					cur->end = sblkp->start;
629*5e3eaea3SApple OSS Distributions 					cur->rxmit = SEQ_MIN(cur->rxmit,
630*5e3eaea3SApple OSS Distributions 					    cur->end);
631*5e3eaea3SApple OSS Distributions 					/*
632*5e3eaea3SApple OSS Distributions 					 * Reset the rxmit_start to that of
633*5e3eaea3SApple OSS Distributions 					 * the current hole as that will
634*5e3eaea3SApple OSS Distributions 					 * help to compute the reorder
635*5e3eaea3SApple OSS Distributions 					 * window correctly
636*5e3eaea3SApple OSS Distributions 					 */
637*5e3eaea3SApple OSS Distributions 					temp->rxmit_start = cur->rxmit_start;
638*5e3eaea3SApple OSS Distributions 				}
639*5e3eaea3SApple OSS Distributions 			}
640*5e3eaea3SApple OSS Distributions 		}
641*5e3eaea3SApple OSS Distributions 		tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
642*5e3eaea3SApple OSS Distributions 		/*
643*5e3eaea3SApple OSS Distributions 		 * Testing sblkp->start against cur->start tells us whether
644*5e3eaea3SApple OSS Distributions 		 * we're done with the sack block or the sack hole.
645*5e3eaea3SApple OSS Distributions 		 * Accordingly, we advance one or the other.
646*5e3eaea3SApple OSS Distributions 		 */
647*5e3eaea3SApple OSS Distributions 		if (SEQ_LEQ(sblkp->start, cur->start)) {
648*5e3eaea3SApple OSS Distributions 			cur = TAILQ_PREV(cur, sackhole_head, scblink);
649*5e3eaea3SApple OSS Distributions 		} else {
650*5e3eaea3SApple OSS Distributions 			sblkp--;
651*5e3eaea3SApple OSS Distributions 		}
652*5e3eaea3SApple OSS Distributions 	}
653*5e3eaea3SApple OSS Distributions }
654*5e3eaea3SApple OSS Distributions 
655*5e3eaea3SApple OSS Distributions /*
656*5e3eaea3SApple OSS Distributions  * Free all SACK holes to clear the scoreboard.
657*5e3eaea3SApple OSS Distributions  */
658*5e3eaea3SApple OSS Distributions void
tcp_free_sackholes(struct tcpcb * tp)659*5e3eaea3SApple OSS Distributions tcp_free_sackholes(struct tcpcb *tp)
660*5e3eaea3SApple OSS Distributions {
661*5e3eaea3SApple OSS Distributions 	struct sackhole *q;
662*5e3eaea3SApple OSS Distributions 
663*5e3eaea3SApple OSS Distributions 	while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL) {
664*5e3eaea3SApple OSS Distributions 		tcp_sackhole_remove(tp, q);
665*5e3eaea3SApple OSS Distributions 	}
666*5e3eaea3SApple OSS Distributions 	tp->sackhint.sack_bytes_rexmit = 0;
667*5e3eaea3SApple OSS Distributions 	tp->sackhint.sack_bytes_acked = 0;
668*5e3eaea3SApple OSS Distributions 	tp->t_new_dupacks = 0;
669*5e3eaea3SApple OSS Distributions 	tp->sackhint.nexthole = NULL;
670*5e3eaea3SApple OSS Distributions 	tp->sack_newdata = 0;
671*5e3eaea3SApple OSS Distributions }
672*5e3eaea3SApple OSS Distributions 
673*5e3eaea3SApple OSS Distributions /*
674*5e3eaea3SApple OSS Distributions  * Partial ack handling within a sack recovery episode.
675*5e3eaea3SApple OSS Distributions  * Keeping this very simple for now. When a partial ack
676*5e3eaea3SApple OSS Distributions  * is received, force snd_cwnd to a value that will allow
677*5e3eaea3SApple OSS Distributions  * the sender to transmit no more than 2 segments.
678*5e3eaea3SApple OSS Distributions  * If necessary, a better scheme can be adopted at a
679*5e3eaea3SApple OSS Distributions  * later point, but for now, the goal is to prevent the
680*5e3eaea3SApple OSS Distributions  * sender from bursting a large amount of data in the midst
681*5e3eaea3SApple OSS Distributions  * of sack recovery.
682*5e3eaea3SApple OSS Distributions  */
683*5e3eaea3SApple OSS Distributions void
tcp_sack_partialack(struct tcpcb * tp,struct tcphdr * th)684*5e3eaea3SApple OSS Distributions tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th)
685*5e3eaea3SApple OSS Distributions {
686*5e3eaea3SApple OSS Distributions 	int num_segs = 1;
687*5e3eaea3SApple OSS Distributions 
688*5e3eaea3SApple OSS Distributions 	tp->t_timer[TCPT_REXMT] = 0;
689*5e3eaea3SApple OSS Distributions 	tp->t_rtttime = 0;
690*5e3eaea3SApple OSS Distributions 	/* send one or 2 segments based on how much new data was acked */
691*5e3eaea3SApple OSS Distributions 	if (((BYTES_ACKED(th, tp)) / tp->t_maxseg) > 2) {
692*5e3eaea3SApple OSS Distributions 		num_segs = 2;
693*5e3eaea3SApple OSS Distributions 	}
694*5e3eaea3SApple OSS Distributions 	if (tcp_do_better_lr) {
695*5e3eaea3SApple OSS Distributions 		tp->snd_cwnd = tcp_flight_size(tp) + num_segs * tp->t_maxseg;
696*5e3eaea3SApple OSS Distributions 	} else {
697*5e3eaea3SApple OSS Distributions 		tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
698*5e3eaea3SApple OSS Distributions 		    (tp->snd_nxt - tp->sack_newdata) +
699*5e3eaea3SApple OSS Distributions 		    num_segs * tp->t_maxseg);
700*5e3eaea3SApple OSS Distributions 	}
701*5e3eaea3SApple OSS Distributions 	if (tp->snd_cwnd > tp->snd_ssthresh) {
702*5e3eaea3SApple OSS Distributions 		tp->snd_cwnd = tp->snd_ssthresh;
703*5e3eaea3SApple OSS Distributions 	}
704*5e3eaea3SApple OSS Distributions 	if (SEQ_LT(tp->snd_fack, tp->snd_recover) &&
705*5e3eaea3SApple OSS Distributions 	    tp->snd_fack == th->th_ack && TAILQ_EMPTY(&tp->snd_holes)) {
706*5e3eaea3SApple OSS Distributions 		struct sackhole *temp;
707*5e3eaea3SApple OSS Distributions 		/*
708*5e3eaea3SApple OSS Distributions 		 * we received a partial ack but there is no sack_hole
709*5e3eaea3SApple OSS Distributions 		 * that will cover the remaining seq space. In this case,
710*5e3eaea3SApple OSS Distributions 		 * create a hole from snd_fack to snd_recover so that
711*5e3eaea3SApple OSS Distributions 		 * the sack recovery will continue.
712*5e3eaea3SApple OSS Distributions 		 */
713*5e3eaea3SApple OSS Distributions 		temp = tcp_sackhole_insert(tp, tp->snd_fack,
714*5e3eaea3SApple OSS Distributions 		    tp->snd_recover, NULL);
715*5e3eaea3SApple OSS Distributions 		if (temp != NULL) {
716*5e3eaea3SApple OSS Distributions 			tp->snd_fack = tp->snd_recover;
717*5e3eaea3SApple OSS Distributions 		}
718*5e3eaea3SApple OSS Distributions 	}
719*5e3eaea3SApple OSS Distributions 	(void) tcp_output(tp);
720*5e3eaea3SApple OSS Distributions }
721*5e3eaea3SApple OSS Distributions 
722*5e3eaea3SApple OSS Distributions /*
723*5e3eaea3SApple OSS Distributions  * Debug version of tcp_sack_output() that walks the scoreboard. Used for
724*5e3eaea3SApple OSS Distributions  * now to sanity check the hint.
725*5e3eaea3SApple OSS Distributions  */
726*5e3eaea3SApple OSS Distributions static struct sackhole *
tcp_sack_output_debug(struct tcpcb * tp,int * sack_bytes_rexmt)727*5e3eaea3SApple OSS Distributions tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
728*5e3eaea3SApple OSS Distributions {
729*5e3eaea3SApple OSS Distributions 	struct sackhole *p;
730*5e3eaea3SApple OSS Distributions 
731*5e3eaea3SApple OSS Distributions 	*sack_bytes_rexmt = 0;
732*5e3eaea3SApple OSS Distributions 	TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
733*5e3eaea3SApple OSS Distributions 		if (SEQ_LT(p->rxmit, p->end)) {
734*5e3eaea3SApple OSS Distributions 			if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
735*5e3eaea3SApple OSS Distributions 				continue;
736*5e3eaea3SApple OSS Distributions 			}
737*5e3eaea3SApple OSS Distributions 			*sack_bytes_rexmt += (p->rxmit - p->start);
738*5e3eaea3SApple OSS Distributions 			break;
739*5e3eaea3SApple OSS Distributions 		}
740*5e3eaea3SApple OSS Distributions 		*sack_bytes_rexmt += (p->rxmit - p->start);
741*5e3eaea3SApple OSS Distributions 	}
742*5e3eaea3SApple OSS Distributions 	return p;
743*5e3eaea3SApple OSS Distributions }
744*5e3eaea3SApple OSS Distributions 
745*5e3eaea3SApple OSS Distributions /*
746*5e3eaea3SApple OSS Distributions  * Returns the next hole to retransmit and the number of retransmitted bytes
747*5e3eaea3SApple OSS Distributions  * from the scoreboard. We store both the next hole and the number of
748*5e3eaea3SApple OSS Distributions  * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
749*5e3eaea3SApple OSS Distributions  * reception). This avoids scoreboard traversals completely.
750*5e3eaea3SApple OSS Distributions  *
751*5e3eaea3SApple OSS Distributions  * The loop here will traverse *at most* one link. Here's the argument.
752*5e3eaea3SApple OSS Distributions  * For the loop to traverse more than 1 link before finding the next hole to
753*5e3eaea3SApple OSS Distributions  * retransmit, we would need to have at least 1 node following the current hint
754*5e3eaea3SApple OSS Distributions  * with (rxmit == end). But, for all holes following the current hint,
755*5e3eaea3SApple OSS Distributions  * (start == rxmit), since we have not yet retransmitted from them. Therefore,
756*5e3eaea3SApple OSS Distributions  * in order to traverse more 1 link in the loop below, we need to have at least
757*5e3eaea3SApple OSS Distributions  * one node following the current hint with (start == rxmit == end).
758*5e3eaea3SApple OSS Distributions  * But that can't happen, (start == end) means that all the data in that hole
759*5e3eaea3SApple OSS Distributions  * has been sacked, in which case, the hole would have been removed from the
760*5e3eaea3SApple OSS Distributions  * scoreboard.
761*5e3eaea3SApple OSS Distributions  */
762*5e3eaea3SApple OSS Distributions struct sackhole *
tcp_sack_output(struct tcpcb * tp,int * sack_bytes_rexmt)763*5e3eaea3SApple OSS Distributions tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
764*5e3eaea3SApple OSS Distributions {
765*5e3eaea3SApple OSS Distributions 	struct sackhole *hole = NULL, *dbg_hole = NULL;
766*5e3eaea3SApple OSS Distributions 	int dbg_bytes_rexmt;
767*5e3eaea3SApple OSS Distributions 
768*5e3eaea3SApple OSS Distributions 	dbg_hole = tcp_sack_output_debug(tp, &dbg_bytes_rexmt);
769*5e3eaea3SApple OSS Distributions 	*sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
770*5e3eaea3SApple OSS Distributions 	hole = tp->sackhint.nexthole;
771*5e3eaea3SApple OSS Distributions 	if (hole == NULL || SEQ_LT(hole->rxmit, hole->end)) {
772*5e3eaea3SApple OSS Distributions 		goto out;
773*5e3eaea3SApple OSS Distributions 	}
774*5e3eaea3SApple OSS Distributions 	while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
775*5e3eaea3SApple OSS Distributions 		if (SEQ_LT(hole->rxmit, hole->end)) {
776*5e3eaea3SApple OSS Distributions 			tp->sackhint.nexthole = hole;
777*5e3eaea3SApple OSS Distributions 			break;
778*5e3eaea3SApple OSS Distributions 		}
779*5e3eaea3SApple OSS Distributions 	}
780*5e3eaea3SApple OSS Distributions out:
781*5e3eaea3SApple OSS Distributions 	if (dbg_hole != hole) {
782*5e3eaea3SApple OSS Distributions 		printf("%s: Computed sack hole not the same as cached value\n", __func__);
783*5e3eaea3SApple OSS Distributions 		hole = dbg_hole;
784*5e3eaea3SApple OSS Distributions 	}
785*5e3eaea3SApple OSS Distributions 	if (*sack_bytes_rexmt != dbg_bytes_rexmt) {
786*5e3eaea3SApple OSS Distributions 		printf("%s: Computed sack_bytes_retransmitted (%d) not "
787*5e3eaea3SApple OSS Distributions 		    "the same as cached value (%d)\n",
788*5e3eaea3SApple OSS Distributions 		    __func__, dbg_bytes_rexmt, *sack_bytes_rexmt);
789*5e3eaea3SApple OSS Distributions 		*sack_bytes_rexmt = dbg_bytes_rexmt;
790*5e3eaea3SApple OSS Distributions 	}
791*5e3eaea3SApple OSS Distributions 	return hole;
792*5e3eaea3SApple OSS Distributions }
793*5e3eaea3SApple OSS Distributions 
794*5e3eaea3SApple OSS Distributions void
tcp_sack_lost_rexmit(struct tcpcb * tp)795*5e3eaea3SApple OSS Distributions tcp_sack_lost_rexmit(struct tcpcb *tp)
796*5e3eaea3SApple OSS Distributions {
797*5e3eaea3SApple OSS Distributions 	struct sackhole *hole = TAILQ_FIRST(&tp->snd_holes);
798*5e3eaea3SApple OSS Distributions 
799*5e3eaea3SApple OSS Distributions 	while (hole) {
800*5e3eaea3SApple OSS Distributions 		hole->rxmit = hole->start;
801*5e3eaea3SApple OSS Distributions 		hole->rxmit_start = tcp_now;
802*5e3eaea3SApple OSS Distributions 
803*5e3eaea3SApple OSS Distributions 		hole = TAILQ_NEXT(hole, scblink);
804*5e3eaea3SApple OSS Distributions 	}
805*5e3eaea3SApple OSS Distributions 
806*5e3eaea3SApple OSS Distributions 	tp->sackhint.nexthole = TAILQ_FIRST(&tp->snd_holes);
807*5e3eaea3SApple OSS Distributions 	tp->sackhint.sack_bytes_rexmit = 0;
808*5e3eaea3SApple OSS Distributions 	tp->sack_newdata = tp->snd_nxt;
809*5e3eaea3SApple OSS Distributions }
810*5e3eaea3SApple OSS Distributions 
811*5e3eaea3SApple OSS Distributions /*
812*5e3eaea3SApple OSS Distributions  * After a timeout, the SACK list may be rebuilt.  This SACK information
813*5e3eaea3SApple OSS Distributions  * should be used to avoid retransmitting SACKed data.  This function
814*5e3eaea3SApple OSS Distributions  * traverses the SACK list to see if snd_nxt should be moved forward.
815*5e3eaea3SApple OSS Distributions  */
816*5e3eaea3SApple OSS Distributions uint32_t
tcp_sack_adjust(struct tcpcb * tp)817*5e3eaea3SApple OSS Distributions tcp_sack_adjust(struct tcpcb *tp)
818*5e3eaea3SApple OSS Distributions {
819*5e3eaea3SApple OSS Distributions 	struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
820*5e3eaea3SApple OSS Distributions 
821*5e3eaea3SApple OSS Distributions 	if (cur == NULL) {
822*5e3eaea3SApple OSS Distributions 		return 0; /* No holes */
823*5e3eaea3SApple OSS Distributions 	}
824*5e3eaea3SApple OSS Distributions 	if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack)) {
825*5e3eaea3SApple OSS Distributions 		return 0; /* We're already beyond any SACKed blocks */
826*5e3eaea3SApple OSS Distributions 	}
827*5e3eaea3SApple OSS Distributions 	/*
828*5e3eaea3SApple OSS Distributions 	 * Two cases for which we want to advance snd_nxt:
829*5e3eaea3SApple OSS Distributions 	 * i) snd_nxt lies between end of one hole and beginning of another
830*5e3eaea3SApple OSS Distributions 	 * ii) snd_nxt lies between end of last hole and snd_fack
831*5e3eaea3SApple OSS Distributions 	 */
832*5e3eaea3SApple OSS Distributions 	while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
833*5e3eaea3SApple OSS Distributions 		if (SEQ_LT(tp->snd_nxt, cur->end)) {
834*5e3eaea3SApple OSS Distributions 			return cur->end - tp->snd_nxt;
835*5e3eaea3SApple OSS Distributions 		}
836*5e3eaea3SApple OSS Distributions 		if (SEQ_GEQ(tp->snd_nxt, p->start)) {
837*5e3eaea3SApple OSS Distributions 			cur = p;
838*5e3eaea3SApple OSS Distributions 		} else {
839*5e3eaea3SApple OSS Distributions 			tp->snd_nxt = p->start;
840*5e3eaea3SApple OSS Distributions 			return p->end - tp->snd_nxt;
841*5e3eaea3SApple OSS Distributions 		}
842*5e3eaea3SApple OSS Distributions 	}
843*5e3eaea3SApple OSS Distributions 	if (SEQ_LT(tp->snd_nxt, cur->end)) {
844*5e3eaea3SApple OSS Distributions 		return cur->end - tp->snd_nxt;
845*5e3eaea3SApple OSS Distributions 	}
846*5e3eaea3SApple OSS Distributions 	tp->snd_nxt = tp->snd_fack;
847*5e3eaea3SApple OSS Distributions 	return 0;
848*5e3eaea3SApple OSS Distributions }
849*5e3eaea3SApple OSS Distributions 
850*5e3eaea3SApple OSS Distributions /*
851*5e3eaea3SApple OSS Distributions  * This function returns TRUE if more than (tcprexmtthresh - 1) * SMSS
852*5e3eaea3SApple OSS Distributions  * bytes with sequence numbers greater than snd_una have been SACKed.
853*5e3eaea3SApple OSS Distributions  */
854*5e3eaea3SApple OSS Distributions boolean_t
tcp_sack_byte_islost(struct tcpcb * tp)855*5e3eaea3SApple OSS Distributions tcp_sack_byte_islost(struct tcpcb *tp)
856*5e3eaea3SApple OSS Distributions {
857*5e3eaea3SApple OSS Distributions 	u_int32_t unacked_bytes, sndhole_bytes = 0;
858*5e3eaea3SApple OSS Distributions 	struct sackhole *sndhole;
859*5e3eaea3SApple OSS Distributions 	if (!SACK_ENABLED(tp) || IN_FASTRECOVERY(tp) ||
860*5e3eaea3SApple OSS Distributions 	    TAILQ_EMPTY(&tp->snd_holes) ||
861*5e3eaea3SApple OSS Distributions 	    (tp->t_flagsext & TF_PKTS_REORDERED)) {
862*5e3eaea3SApple OSS Distributions 		return FALSE;
863*5e3eaea3SApple OSS Distributions 	}
864*5e3eaea3SApple OSS Distributions 
865*5e3eaea3SApple OSS Distributions 	unacked_bytes = tp->snd_max - tp->snd_una;
866*5e3eaea3SApple OSS Distributions 
867*5e3eaea3SApple OSS Distributions 	TAILQ_FOREACH(sndhole, &tp->snd_holes, scblink) {
868*5e3eaea3SApple OSS Distributions 		sndhole_bytes += (sndhole->end - sndhole->start);
869*5e3eaea3SApple OSS Distributions 	}
870*5e3eaea3SApple OSS Distributions 
871*5e3eaea3SApple OSS Distributions 	VERIFY(unacked_bytes >= sndhole_bytes);
872*5e3eaea3SApple OSS Distributions 	return (unacked_bytes - sndhole_bytes) >
873*5e3eaea3SApple OSS Distributions 	       ((tcprexmtthresh - 1) * tp->t_maxseg);
874*5e3eaea3SApple OSS Distributions }
875*5e3eaea3SApple OSS Distributions 
876*5e3eaea3SApple OSS Distributions /*
877*5e3eaea3SApple OSS Distributions  * Process any DSACK options that might be present on an input packet
878*5e3eaea3SApple OSS Distributions  */
879*5e3eaea3SApple OSS Distributions 
880*5e3eaea3SApple OSS Distributions boolean_t
tcp_sack_process_dsack(struct tcpcb * tp,struct tcpopt * to,struct tcphdr * th)881*5e3eaea3SApple OSS Distributions tcp_sack_process_dsack(struct tcpcb *tp, struct tcpopt *to,
882*5e3eaea3SApple OSS Distributions     struct tcphdr *th)
883*5e3eaea3SApple OSS Distributions {
884*5e3eaea3SApple OSS Distributions 	struct sackblk first_sack, second_sack;
885*5e3eaea3SApple OSS Distributions 
886*5e3eaea3SApple OSS Distributions 	bcopy(to->to_sacks, &first_sack, sizeof(first_sack));
887*5e3eaea3SApple OSS Distributions 	first_sack.start = ntohl(first_sack.start);
888*5e3eaea3SApple OSS Distributions 	first_sack.end = ntohl(first_sack.end);
889*5e3eaea3SApple OSS Distributions 
890*5e3eaea3SApple OSS Distributions 	if (to->to_nsacks > 1) {
891*5e3eaea3SApple OSS Distributions 		bcopy((to->to_sacks + TCPOLEN_SACK), &second_sack,
892*5e3eaea3SApple OSS Distributions 		    sizeof(second_sack));
893*5e3eaea3SApple OSS Distributions 		second_sack.start = ntohl(second_sack.start);
894*5e3eaea3SApple OSS Distributions 		second_sack.end = ntohl(second_sack.end);
895*5e3eaea3SApple OSS Distributions 	}
896*5e3eaea3SApple OSS Distributions 
897*5e3eaea3SApple OSS Distributions 	if (SEQ_LT(first_sack.start, th->th_ack) &&
898*5e3eaea3SApple OSS Distributions 	    SEQ_LEQ(first_sack.end, th->th_ack)) {
899*5e3eaea3SApple OSS Distributions 		/*
900*5e3eaea3SApple OSS Distributions 		 * There is a dsack option reporting a duplicate segment
901*5e3eaea3SApple OSS Distributions 		 * also covered by cumulative acknowledgement.
902*5e3eaea3SApple OSS Distributions 		 *
903*5e3eaea3SApple OSS Distributions 		 * Validate the sequence numbers before looking at dsack
904*5e3eaea3SApple OSS Distributions 		 * option. The duplicate notification can come after
905*5e3eaea3SApple OSS Distributions 		 * snd_una moves forward. In order to set a window of valid
906*5e3eaea3SApple OSS Distributions 		 * sequence numbers to look for, we set a maximum send
907*5e3eaea3SApple OSS Distributions 		 * window within which the DSACK option will be processed.
908*5e3eaea3SApple OSS Distributions 		 */
909*5e3eaea3SApple OSS Distributions 		if (!(TCP_DSACK_SEQ_IN_WINDOW(tp, first_sack.start, th->th_ack) &&
910*5e3eaea3SApple OSS Distributions 		    TCP_DSACK_SEQ_IN_WINDOW(tp, first_sack.end, th->th_ack))) {
911*5e3eaea3SApple OSS Distributions 			to->to_nsacks--;
912*5e3eaea3SApple OSS Distributions 			to->to_sacks += TCPOLEN_SACK;
913*5e3eaea3SApple OSS Distributions 			tcpstat.tcps_dsack_recvd_old++;
914*5e3eaea3SApple OSS Distributions 
915*5e3eaea3SApple OSS Distributions 			/*
916*5e3eaea3SApple OSS Distributions 			 * returning true here so that the ack will not be
917*5e3eaea3SApple OSS Distributions 			 * treated as duplicate ack.
918*5e3eaea3SApple OSS Distributions 			 */
919*5e3eaea3SApple OSS Distributions 			return TRUE;
920*5e3eaea3SApple OSS Distributions 		}
921*5e3eaea3SApple OSS Distributions 	} else if (to->to_nsacks > 1 &&
922*5e3eaea3SApple OSS Distributions 	    SEQ_LEQ(second_sack.start, first_sack.start) &&
923*5e3eaea3SApple OSS Distributions 	    SEQ_GEQ(second_sack.end, first_sack.end)) {
924*5e3eaea3SApple OSS Distributions 		/*
925*5e3eaea3SApple OSS Distributions 		 * there is a dsack option in the first block not
926*5e3eaea3SApple OSS Distributions 		 * covered by the cumulative acknowledgement but covered
927*5e3eaea3SApple OSS Distributions 		 * by the second sack block.
928*5e3eaea3SApple OSS Distributions 		 *
929*5e3eaea3SApple OSS Distributions 		 * verify the sequence numbes on the second sack block
930*5e3eaea3SApple OSS Distributions 		 * before processing the DSACK option. Returning false
931*5e3eaea3SApple OSS Distributions 		 * here will treat the ack as a duplicate ack.
932*5e3eaea3SApple OSS Distributions 		 */
933*5e3eaea3SApple OSS Distributions 		if (!TCP_VALIDATE_SACK_SEQ_NUMBERS(tp, &second_sack,
934*5e3eaea3SApple OSS Distributions 		    th->th_ack)) {
935*5e3eaea3SApple OSS Distributions 			to->to_nsacks--;
936*5e3eaea3SApple OSS Distributions 			to->to_sacks += TCPOLEN_SACK;
937*5e3eaea3SApple OSS Distributions 			tcpstat.tcps_dsack_recvd_old++;
938*5e3eaea3SApple OSS Distributions 			return TRUE;
939*5e3eaea3SApple OSS Distributions 		}
940*5e3eaea3SApple OSS Distributions 	} else {
941*5e3eaea3SApple OSS Distributions 		/* no dsack options, proceed with processing the sack */
942*5e3eaea3SApple OSS Distributions 		return FALSE;
943*5e3eaea3SApple OSS Distributions 	}
944*5e3eaea3SApple OSS Distributions 
945*5e3eaea3SApple OSS Distributions 	/* Update the tcpopt pointer to exclude dsack block */
946*5e3eaea3SApple OSS Distributions 	to->to_nsacks--;
947*5e3eaea3SApple OSS Distributions 	to->to_sacks += TCPOLEN_SACK;
948*5e3eaea3SApple OSS Distributions 	tcpstat.tcps_dsack_recvd++;
949*5e3eaea3SApple OSS Distributions 	tp->t_dsack_recvd++;
950*5e3eaea3SApple OSS Distributions 
951*5e3eaea3SApple OSS Distributions 	/* Update the sender's retransmit segment state */
952*5e3eaea3SApple OSS Distributions 	if (((tp->t_rxtshift == 1 && first_sack.start == tp->snd_una) ||
953*5e3eaea3SApple OSS Distributions 	    ((tp->t_flagsext & TF_SENT_TLPROBE) &&
954*5e3eaea3SApple OSS Distributions 	    first_sack.end == tp->t_tlphighrxt)) &&
955*5e3eaea3SApple OSS Distributions 	    TAILQ_EMPTY(&tp->snd_holes) &&
956*5e3eaea3SApple OSS Distributions 	    SEQ_GT(th->th_ack, tp->snd_una)) {
957*5e3eaea3SApple OSS Distributions 		/*
958*5e3eaea3SApple OSS Distributions 		 * If the dsack is for a retransmitted packet and one of
959*5e3eaea3SApple OSS Distributions 		 * the two cases is true, it indicates ack loss:
960*5e3eaea3SApple OSS Distributions 		 * - retransmit timeout and first_sack.start == snd_una
961*5e3eaea3SApple OSS Distributions 		 * - TLP probe and first_sack.end == tlphighrxt
962*5e3eaea3SApple OSS Distributions 		 *
963*5e3eaea3SApple OSS Distributions 		 * Ignore dsack and do not update state when there is
964*5e3eaea3SApple OSS Distributions 		 * ack loss
965*5e3eaea3SApple OSS Distributions 		 */
966*5e3eaea3SApple OSS Distributions 		tcpstat.tcps_dsack_ackloss++;
967*5e3eaea3SApple OSS Distributions 
968*5e3eaea3SApple OSS Distributions 		return TRUE;
969*5e3eaea3SApple OSS Distributions 	} else {
970*5e3eaea3SApple OSS Distributions 		tcp_rxtseg_set_spurious(tp, first_sack.start, (first_sack.end - 1));
971*5e3eaea3SApple OSS Distributions 	}
972*5e3eaea3SApple OSS Distributions 	return TRUE;
973*5e3eaea3SApple OSS Distributions }
974