xref: /freebsd/sys/netinet/tcp_sack.c (revision 1c05a6ea6b849ff95e539c31adea887c644a6a01)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3  *	The Regents of the University of California.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	@(#)tcp_sack.c	8.12 (Berkeley) 5/24/95
31  */
32 
33 /*-
34  *	@@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
35  *
36  * NRL grants permission for redistribution and use in source and binary
37  * forms, with or without modification, of the software and documentation
38  * created at NRL provided that the following conditions are met:
39  *
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgements:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  *	This product includes software developed at the Information
50  *	Technology Division, US Naval Research Laboratory.
51  * 4. Neither the name of the NRL nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
56  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
58  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
59  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
60  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
61  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
62  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
63  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
64  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
65  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66  *
67  * The views and conclusions contained in the software and documentation
68  * are those of the authors and should not be interpreted as representing
69  * official policies, either expressed or implied, of the US Naval
70  * Research Laboratory (NRL).
71  */
72 
73 #include <sys/cdefs.h>
74 __FBSDID("$FreeBSD$");
75 
76 #include "opt_inet.h"
77 #include "opt_inet6.h"
78 #include "opt_tcpdebug.h"
79 
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
83 #include <sys/sysctl.h>
84 #include <sys/malloc.h>
85 #include <sys/mbuf.h>
86 #include <sys/proc.h>		/* for proc0 declaration */
87 #include <sys/protosw.h>
88 #include <sys/socket.h>
89 #include <sys/socketvar.h>
90 #include <sys/syslog.h>
91 #include <sys/systm.h>
92 
93 #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
94 
95 #include <vm/uma.h>
96 
97 #include <net/if.h>
98 #include <net/if_var.h>
99 #include <net/route.h>
100 #include <net/vnet.h>
101 
102 #include <netinet/in.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/ip.h>
105 #include <netinet/in_var.h>
106 #include <netinet/in_pcb.h>
107 #include <netinet/ip_var.h>
108 #include <netinet/ip6.h>
109 #include <netinet/icmp6.h>
110 #include <netinet6/nd6.h>
111 #include <netinet6/ip6_var.h>
112 #include <netinet6/in6_pcb.h>
113 #include <netinet/tcp.h>
114 #include <netinet/tcp_fsm.h>
115 #include <netinet/tcp_seq.h>
116 #include <netinet/tcp_timer.h>
117 #include <netinet/tcp_var.h>
118 #include <netinet6/tcp6_var.h>
119 #include <netinet/tcpip.h>
120 #ifdef TCPDEBUG
121 #include <netinet/tcp_debug.h>
122 #endif /* TCPDEBUG */
123 
124 #include <machine/in_cksum.h>
125 
126 VNET_DECLARE(struct uma_zone *, sack_hole_zone);
127 #define	V_sack_hole_zone		VNET(sack_hole_zone)
128 
129 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, 0, "TCP SACK");
130 VNET_DEFINE(int, tcp_do_sack) = 1;
131 #define	V_tcp_do_sack			VNET(tcp_do_sack)
132 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
133     &VNET_NAME(tcp_do_sack), 0, "Enable/Disable TCP SACK support");
134 
135 VNET_DEFINE(int, tcp_sack_maxholes) = 128;
136 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_VNET | CTLFLAG_RW,
137     &VNET_NAME(tcp_sack_maxholes), 0,
138     "Maximum number of TCP SACK holes allowed per connection");
139 
140 VNET_DEFINE(int, tcp_sack_globalmaxholes) = 65536;
141 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_VNET | CTLFLAG_RW,
142     &VNET_NAME(tcp_sack_globalmaxholes), 0,
143     "Global maximum number of TCP SACK holes");
144 
145 VNET_DEFINE(int, tcp_sack_globalholes) = 0;
146 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_VNET | CTLFLAG_RD,
147     &VNET_NAME(tcp_sack_globalholes), 0,
148     "Global number of TCP SACK holes currently allocated");
149 
150 /*
151  * This function is called upon receipt of new valid data (while not in
152  * header prediction mode), and it updates the ordered list of sacks.
153  */
154 void
155 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
156 {
157 	/*
158 	 * First reported block MUST be the most recent one.  Subsequent
159 	 * blocks SHOULD be in the order in which they arrived at the
160 	 * receiver.  These two conditions make the implementation fully
161 	 * compliant with RFC 2018.
162 	 */
163 	struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
164 	int num_head, num_saved, i;
165 
166 	INP_WLOCK_ASSERT(tp->t_inpcb);
167 
168 	/* Check arguments. */
169 	KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end"));
170 
171 	/* SACK block for the received segment. */
172 	head_blk.start = rcv_start;
173 	head_blk.end = rcv_end;
174 
175 	/*
176 	 * Merge updated SACK blocks into head_blk, and save unchanged SACK
177 	 * blocks into saved_blks[].  num_saved will have the number of the
178 	 * saved SACK blocks.
179 	 */
180 	num_saved = 0;
181 	for (i = 0; i < tp->rcv_numsacks; i++) {
182 		tcp_seq start = tp->sackblks[i].start;
183 		tcp_seq end = tp->sackblks[i].end;
184 		if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
185 			/*
186 			 * Discard this SACK block.
187 			 */
188 		} else if (SEQ_LEQ(head_blk.start, end) &&
189 			   SEQ_GEQ(head_blk.end, start)) {
190 			/*
191 			 * Merge this SACK block into head_blk.  This SACK
192 			 * block itself will be discarded.
193 			 */
194 			if (SEQ_GT(head_blk.start, start))
195 				head_blk.start = start;
196 			if (SEQ_LT(head_blk.end, end))
197 				head_blk.end = end;
198 		} else {
199 			/*
200 			 * Save this SACK block.
201 			 */
202 			saved_blks[num_saved].start = start;
203 			saved_blks[num_saved].end = end;
204 			num_saved++;
205 		}
206 	}
207 
208 	/*
209 	 * Update SACK list in tp->sackblks[].
210 	 */
211 	num_head = 0;
212 	if (SEQ_GT(head_blk.start, tp->rcv_nxt)) {
213 		/*
214 		 * The received data segment is an out-of-order segment.  Put
215 		 * head_blk at the top of SACK list.
216 		 */
217 		tp->sackblks[0] = head_blk;
218 		num_head = 1;
219 		/*
220 		 * If the number of saved SACK blocks exceeds its limit,
221 		 * discard the last SACK block.
222 		 */
223 		if (num_saved >= MAX_SACK_BLKS)
224 			num_saved--;
225 	}
226 	if (num_saved > 0) {
227 		/*
228 		 * Copy the saved SACK blocks back.
229 		 */
230 		bcopy(saved_blks, &tp->sackblks[num_head],
231 		      sizeof(struct sackblk) * num_saved);
232 	}
233 
234 	/* Save the number of SACK blocks. */
235 	tp->rcv_numsacks = num_head + num_saved;
236 }
237 
238 /*
239  * Delete all receiver-side SACK information.
240  */
241 void
242 tcp_clean_sackreport(struct tcpcb *tp)
243 {
244 	int i;
245 
246 	INP_WLOCK_ASSERT(tp->t_inpcb);
247 	tp->rcv_numsacks = 0;
248 	for (i = 0; i < MAX_SACK_BLKS; i++)
249 		tp->sackblks[i].start = tp->sackblks[i].end=0;
250 }
251 
252 /*
253  * Allocate struct sackhole.
254  */
255 static struct sackhole *
256 tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
257 {
258 	struct sackhole *hole;
259 
260 	if (tp->snd_numholes >= V_tcp_sack_maxholes ||
261 	    V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) {
262 		TCPSTAT_INC(tcps_sack_sboverflow);
263 		return NULL;
264 	}
265 
266 	hole = (struct sackhole *)uma_zalloc(V_sack_hole_zone, M_NOWAIT);
267 	if (hole == NULL)
268 		return NULL;
269 
270 	hole->start = start;
271 	hole->end = end;
272 	hole->rxmit = start;
273 
274 	tp->snd_numholes++;
275 	atomic_add_int(&V_tcp_sack_globalholes, 1);
276 
277 	return hole;
278 }
279 
280 /*
281  * Free struct sackhole.
282  */
283 static void
284 tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
285 {
286 
287 	uma_zfree(V_sack_hole_zone, hole);
288 
289 	tp->snd_numholes--;
290 	atomic_subtract_int(&V_tcp_sack_globalholes, 1);
291 
292 	KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
293 	KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
294 }
295 
296 /*
297  * Insert new SACK hole into scoreboard.
298  */
299 static struct sackhole *
300 tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
301     struct sackhole *after)
302 {
303 	struct sackhole *hole;
304 
305 	/* Allocate a new SACK hole. */
306 	hole = tcp_sackhole_alloc(tp, start, end);
307 	if (hole == NULL)
308 		return NULL;
309 
310 	/* Insert the new SACK hole into scoreboard. */
311 	if (after != NULL)
312 		TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
313 	else
314 		TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
315 
316 	/* Update SACK hint. */
317 	if (tp->sackhint.nexthole == NULL)
318 		tp->sackhint.nexthole = hole;
319 
320 	return hole;
321 }
322 
323 /*
324  * Remove SACK hole from scoreboard.
325  */
326 static void
327 tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
328 {
329 
330 	/* Update SACK hint. */
331 	if (tp->sackhint.nexthole == hole)
332 		tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
333 
334 	/* Remove this SACK hole. */
335 	TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
336 
337 	/* Free this SACK hole. */
338 	tcp_sackhole_free(tp, hole);
339 }
340 
341 /*
342  * Process cumulative ACK and the TCP SACK option to update the scoreboard.
343  * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
344  * the sequence space).
345  * Returns 1 if incoming ACK has previously unknown SACK information,
346  * 0 otherwise. Note: We treat (snd_una, th_ack) as a sack block so any changes
347  * to that (i.e. left edge moving) would also be considered a change in SACK
348  * information which is slightly different than rfc6675.
349  */
350 int
351 tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack)
352 {
353 	struct sackhole *cur, *temp;
354 	struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp;
355 	int i, j, num_sack_blks, sack_changed;
356 
357 	INP_WLOCK_ASSERT(tp->t_inpcb);
358 
359 	num_sack_blks = 0;
360 	sack_changed = 0;
361 	/*
362 	 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
363 	 * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
364 	 */
365 	if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
366 		sack_blocks[num_sack_blks].start = tp->snd_una;
367 		sack_blocks[num_sack_blks++].end = th_ack;
368 	}
369 	/*
370 	 * Append received valid SACK blocks to sack_blocks[], but only if we
371 	 * received new blocks from the other side.
372 	 */
373 	if (to->to_flags & TOF_SACK) {
374 		tp->sackhint.sacked_bytes = 0;	/* reset */
375 		for (i = 0; i < to->to_nsacks; i++) {
376 			bcopy((to->to_sacks + i * TCPOLEN_SACK),
377 			    &sack, sizeof(sack));
378 			sack.start = ntohl(sack.start);
379 			sack.end = ntohl(sack.end);
380 			if (SEQ_GT(sack.end, sack.start) &&
381 			    SEQ_GT(sack.start, tp->snd_una) &&
382 			    SEQ_GT(sack.start, th_ack) &&
383 			    SEQ_LT(sack.start, tp->snd_max) &&
384 			    SEQ_GT(sack.end, tp->snd_una) &&
385 			    SEQ_LEQ(sack.end, tp->snd_max)) {
386 				sack_blocks[num_sack_blks++] = sack;
387 				tp->sackhint.sacked_bytes +=
388 				    (sack.end-sack.start);
389 			}
390 		}
391 	}
392 	/*
393 	 * Return if SND.UNA is not advanced and no valid SACK block is
394 	 * received.
395 	 */
396 	if (num_sack_blks == 0)
397 		return (sack_changed);
398 
399 	/*
400 	 * Sort the SACK blocks so we can update the scoreboard with just one
401 	 * pass. The overhead of sorting up to 4+1 elements is less than
402 	 * making up to 4+1 passes over the scoreboard.
403 	 */
404 	for (i = 0; i < num_sack_blks; i++) {
405 		for (j = i + 1; j < num_sack_blks; j++) {
406 			if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
407 				sack = sack_blocks[i];
408 				sack_blocks[i] = sack_blocks[j];
409 				sack_blocks[j] = sack;
410 			}
411 		}
412 	}
413 	if (TAILQ_EMPTY(&tp->snd_holes))
414 		/*
415 		 * Empty scoreboard. Need to initialize snd_fack (it may be
416 		 * uninitialized or have a bogus value). Scoreboard holes
417 		 * (from the sack blocks received) are created later below
418 		 * (in the logic that adds holes to the tail of the
419 		 * scoreboard).
420 		 */
421 		tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
422 	/*
423 	 * In the while-loop below, incoming SACK blocks (sack_blocks[]) and
424 	 * SACK holes (snd_holes) are traversed from their tails with just
425 	 * one pass in order to reduce the number of compares especially when
426 	 * the bandwidth-delay product is large.
427 	 *
428 	 * Note: Typically, in the first RTT of SACK recovery, the highest
429 	 * three or four SACK blocks with the same ack number are received.
430 	 * In the second RTT, if retransmitted data segments are not lost,
431 	 * the highest three or four SACK blocks with ack number advancing
432 	 * are received.
433 	 */
434 	sblkp = &sack_blocks[num_sack_blks - 1];	/* Last SACK block */
435 	tp->sackhint.last_sack_ack = sblkp->end;
436 	if (SEQ_LT(tp->snd_fack, sblkp->start)) {
437 		/*
438 		 * The highest SACK block is beyond fack.  Append new SACK
439 		 * hole at the tail.  If the second or later highest SACK
440 		 * blocks are also beyond the current fack, they will be
441 		 * inserted by way of hole splitting in the while-loop below.
442 		 */
443 		temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL);
444 		if (temp != NULL) {
445 			tp->snd_fack = sblkp->end;
446 			/* Go to the previous sack block. */
447 			sblkp--;
448 			sack_changed = 1;
449 		} else {
450 			/*
451 			 * We failed to add a new hole based on the current
452 			 * sack block.  Skip over all the sack blocks that
453 			 * fall completely to the right of snd_fack and
454 			 * proceed to trim the scoreboard based on the
455 			 * remaining sack blocks.  This also trims the
456 			 * scoreboard for th_ack (which is sack_blocks[0]).
457 			 */
458 			while (sblkp >= sack_blocks &&
459 			       SEQ_LT(tp->snd_fack, sblkp->start))
460 				sblkp--;
461 			if (sblkp >= sack_blocks &&
462 			    SEQ_LT(tp->snd_fack, sblkp->end))
463 				tp->snd_fack = sblkp->end;
464 		}
465 	} else if (SEQ_LT(tp->snd_fack, sblkp->end)) {
466 		/* fack is advanced. */
467 		tp->snd_fack = sblkp->end;
468 		sack_changed = 1;
469 	}
470 	cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */
471 	/*
472 	 * Since the incoming sack blocks are sorted, we can process them
473 	 * making one sweep of the scoreboard.
474 	 */
475 	while (sblkp >= sack_blocks  && cur != NULL) {
476 		if (SEQ_GEQ(sblkp->start, cur->end)) {
477 			/*
478 			 * SACKs data beyond the current hole.  Go to the
479 			 * previous sack block.
480 			 */
481 			sblkp--;
482 			continue;
483 		}
484 		if (SEQ_LEQ(sblkp->end, cur->start)) {
485 			/*
486 			 * SACKs data before the current hole.  Go to the
487 			 * previous hole.
488 			 */
489 			cur = TAILQ_PREV(cur, sackhole_head, scblink);
490 			continue;
491 		}
492 		tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
493 		KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
494 		    ("sackhint bytes rtx >= 0"));
495 		sack_changed = 1;
496 		if (SEQ_LEQ(sblkp->start, cur->start)) {
497 			/* Data acks at least the beginning of hole. */
498 			if (SEQ_GEQ(sblkp->end, cur->end)) {
499 				/* Acks entire hole, so delete hole. */
500 				temp = cur;
501 				cur = TAILQ_PREV(cur, sackhole_head, scblink);
502 				tcp_sackhole_remove(tp, temp);
503 				/*
504 				 * The sack block may ack all or part of the
505 				 * next hole too, so continue onto the next
506 				 * hole.
507 				 */
508 				continue;
509 			} else {
510 				/* Move start of hole forward. */
511 				cur->start = sblkp->end;
512 				cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
513 			}
514 		} else {
515 			/* Data acks at least the end of hole. */
516 			if (SEQ_GEQ(sblkp->end, cur->end)) {
517 				/* Move end of hole backward. */
518 				cur->end = sblkp->start;
519 				cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
520 			} else {
521 				/*
522 				 * ACKs some data in middle of a hole; need
523 				 * to split current hole
524 				 */
525 				temp = tcp_sackhole_insert(tp, sblkp->end,
526 				    cur->end, cur);
527 				if (temp != NULL) {
528 					if (SEQ_GT(cur->rxmit, temp->rxmit)) {
529 						temp->rxmit = cur->rxmit;
530 						tp->sackhint.sack_bytes_rexmit
531 						    += (temp->rxmit
532 						    - temp->start);
533 					}
534 					cur->end = sblkp->start;
535 					cur->rxmit = SEQ_MIN(cur->rxmit,
536 					    cur->end);
537 				}
538 			}
539 		}
540 		tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
541 		/*
542 		 * Testing sblkp->start against cur->start tells us whether
543 		 * we're done with the sack block or the sack hole.
544 		 * Accordingly, we advance one or the other.
545 		 */
546 		if (SEQ_LEQ(sblkp->start, cur->start))
547 			cur = TAILQ_PREV(cur, sackhole_head, scblink);
548 		else
549 			sblkp--;
550 	}
551 	return (sack_changed);
552 }
553 
554 /*
555  * Free all SACK holes to clear the scoreboard.
556  */
557 void
558 tcp_free_sackholes(struct tcpcb *tp)
559 {
560 	struct sackhole *q;
561 
562 	INP_WLOCK_ASSERT(tp->t_inpcb);
563 	while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
564 		tcp_sackhole_remove(tp, q);
565 	tp->sackhint.sack_bytes_rexmit = 0;
566 
567 	KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
568 	KASSERT(tp->sackhint.nexthole == NULL,
569 		("tp->sackhint.nexthole == NULL"));
570 }
571 
572 /*
573  * Partial ack handling within a sack recovery episode.  Keeping this very
574  * simple for now.  When a partial ack is received, force snd_cwnd to a value
575  * that will allow the sender to transmit no more than 2 segments.  If
576  * necessary, a better scheme can be adopted at a later point, but for now,
577  * the goal is to prevent the sender from bursting a large amount of data in
578  * the midst of sack recovery.
579  */
580 void
581 tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th)
582 {
583 	int num_segs = 1;
584 
585 	INP_WLOCK_ASSERT(tp->t_inpcb);
586 	tcp_timer_activate(tp, TT_REXMT, 0);
587 	tp->t_rtttime = 0;
588 	/* Send one or 2 segments based on how much new data was acked. */
589 	if ((BYTES_THIS_ACK(tp, th) / tp->t_maxseg) >= 2)
590 		num_segs = 2;
591 	tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
592 	    (tp->snd_nxt - tp->sack_newdata) + num_segs * tp->t_maxseg);
593 	if (tp->snd_cwnd > tp->snd_ssthresh)
594 		tp->snd_cwnd = tp->snd_ssthresh;
595 	tp->t_flags |= TF_ACKNOW;
596 	(void) tp->t_fb->tfb_tcp_output(tp);
597 }
598 
599 #if 0
600 /*
601  * Debug version of tcp_sack_output() that walks the scoreboard.  Used for
602  * now to sanity check the hint.
603  */
604 static struct sackhole *
605 tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
606 {
607 	struct sackhole *p;
608 
609 	INP_WLOCK_ASSERT(tp->t_inpcb);
610 	*sack_bytes_rexmt = 0;
611 	TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
612 		if (SEQ_LT(p->rxmit, p->end)) {
613 			if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
614 				continue;
615 			}
616 			*sack_bytes_rexmt += (p->rxmit - p->start);
617 			break;
618 		}
619 		*sack_bytes_rexmt += (p->rxmit - p->start);
620 	}
621 	return (p);
622 }
623 #endif
624 
625 /*
626  * Returns the next hole to retransmit and the number of retransmitted bytes
627  * from the scoreboard.  We store both the next hole and the number of
628  * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
629  * reception).  This avoids scoreboard traversals completely.
630  *
631  * The loop here will traverse *at most* one link.  Here's the argument.  For
632  * the loop to traverse more than 1 link before finding the next hole to
633  * retransmit, we would need to have at least 1 node following the current
634  * hint with (rxmit == end).  But, for all holes following the current hint,
635  * (start == rxmit), since we have not yet retransmitted from them.
636  * Therefore, in order to traverse more 1 link in the loop below, we need to
637  * have at least one node following the current hint with (start == rxmit ==
638  * end).  But that can't happen, (start == end) means that all the data in
639  * that hole has been sacked, in which case, the hole would have been removed
640  * from the scoreboard.
641  */
642 struct sackhole *
643 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
644 {
645 	struct sackhole *hole = NULL;
646 
647 	INP_WLOCK_ASSERT(tp->t_inpcb);
648 	*sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
649 	hole = tp->sackhint.nexthole;
650 	if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
651 		goto out;
652 	while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
653 		if (SEQ_LT(hole->rxmit, hole->end)) {
654 			tp->sackhint.nexthole = hole;
655 			break;
656 		}
657 	}
658 out:
659 	return (hole);
660 }
661 
662 /*
663  * After a timeout, the SACK list may be rebuilt.  This SACK information
664  * should be used to avoid retransmitting SACKed data.  This function
665  * traverses the SACK list to see if snd_nxt should be moved forward.
666  */
667 void
668 tcp_sack_adjust(struct tcpcb *tp)
669 {
670 	struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
671 
672 	INP_WLOCK_ASSERT(tp->t_inpcb);
673 	if (cur == NULL)
674 		return; /* No holes */
675 	if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack))
676 		return; /* We're already beyond any SACKed blocks */
677 	/*-
678 	 * Two cases for which we want to advance snd_nxt:
679 	 * i) snd_nxt lies between end of one hole and beginning of another
680 	 * ii) snd_nxt lies between end of last hole and snd_fack
681 	 */
682 	while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
683 		if (SEQ_LT(tp->snd_nxt, cur->end))
684 			return;
685 		if (SEQ_GEQ(tp->snd_nxt, p->start))
686 			cur = p;
687 		else {
688 			tp->snd_nxt = p->start;
689 			return;
690 		}
691 	}
692 	if (SEQ_LT(tp->snd_nxt, cur->end))
693 		return;
694 	tp->snd_nxt = tp->snd_fack;
695 }
696