xref: /freebsd/sys/netinet/tcp_sack.c (revision 93e779a26c651610ac6e7986d67ecc9ed2cadcbf)
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 #define	V_tcp_sack_maxholes		VNET(tcp_sack_maxholes)
137 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_VNET | CTLFLAG_RW,
138     &VNET_NAME(tcp_sack_maxholes), 0,
139     "Maximum number of TCP SACK holes allowed per connection");
140 
141 VNET_DEFINE(int, tcp_sack_globalmaxholes) = 65536;
142 #define	V_tcp_sack_globalmaxholes	VNET(tcp_sack_globalmaxholes)
143 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_VNET | CTLFLAG_RW,
144     &VNET_NAME(tcp_sack_globalmaxholes), 0,
145     "Global maximum number of TCP SACK holes");
146 
147 VNET_DEFINE(int, tcp_sack_globalholes) = 0;
148 #define	V_tcp_sack_globalholes		VNET(tcp_sack_globalholes)
149 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_VNET | CTLFLAG_RD,
150     &VNET_NAME(tcp_sack_globalholes), 0,
151     "Global number of TCP SACK holes currently allocated");
152 
153 /*
154  * This function is called upon receipt of new valid data (while not in
155  * header prediction mode), and it updates the ordered list of sacks.
156  */
157 void
158 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
159 {
160 	/*
161 	 * First reported block MUST be the most recent one.  Subsequent
162 	 * blocks SHOULD be in the order in which they arrived at the
163 	 * receiver.  These two conditions make the implementation fully
164 	 * compliant with RFC 2018.
165 	 */
166 	struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
167 	int num_head, num_saved, i;
168 
169 	INP_WLOCK_ASSERT(tp->t_inpcb);
170 
171 	/* Check arguments. */
172 	KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end"));
173 
174 	/* SACK block for the received segment. */
175 	head_blk.start = rcv_start;
176 	head_blk.end = rcv_end;
177 
178 	/*
179 	 * Merge updated SACK blocks into head_blk, and save unchanged SACK
180 	 * blocks into saved_blks[].  num_saved will have the number of the
181 	 * saved SACK blocks.
182 	 */
183 	num_saved = 0;
184 	for (i = 0; i < tp->rcv_numsacks; i++) {
185 		tcp_seq start = tp->sackblks[i].start;
186 		tcp_seq end = tp->sackblks[i].end;
187 		if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
188 			/*
189 			 * Discard this SACK block.
190 			 */
191 		} else if (SEQ_LEQ(head_blk.start, end) &&
192 			   SEQ_GEQ(head_blk.end, start)) {
193 			/*
194 			 * Merge this SACK block into head_blk.  This SACK
195 			 * block itself will be discarded.
196 			 */
197 			if (SEQ_GT(head_blk.start, start))
198 				head_blk.start = start;
199 			if (SEQ_LT(head_blk.end, end))
200 				head_blk.end = end;
201 		} else {
202 			/*
203 			 * Save this SACK block.
204 			 */
205 			saved_blks[num_saved].start = start;
206 			saved_blks[num_saved].end = end;
207 			num_saved++;
208 		}
209 	}
210 
211 	/*
212 	 * Update SACK list in tp->sackblks[].
213 	 */
214 	num_head = 0;
215 	if (SEQ_GT(head_blk.start, tp->rcv_nxt)) {
216 		/*
217 		 * The received data segment is an out-of-order segment.  Put
218 		 * head_blk at the top of SACK list.
219 		 */
220 		tp->sackblks[0] = head_blk;
221 		num_head = 1;
222 		/*
223 		 * If the number of saved SACK blocks exceeds its limit,
224 		 * discard the last SACK block.
225 		 */
226 		if (num_saved >= MAX_SACK_BLKS)
227 			num_saved--;
228 	}
229 	if (num_saved > 0) {
230 		/*
231 		 * Copy the saved SACK blocks back.
232 		 */
233 		bcopy(saved_blks, &tp->sackblks[num_head],
234 		      sizeof(struct sackblk) * num_saved);
235 	}
236 
237 	/* Save the number of SACK blocks. */
238 	tp->rcv_numsacks = num_head + num_saved;
239 }
240 
241 /*
242  * Delete all receiver-side SACK information.
243  */
244 void
245 tcp_clean_sackreport(struct tcpcb *tp)
246 {
247 	int i;
248 
249 	INP_WLOCK_ASSERT(tp->t_inpcb);
250 	tp->rcv_numsacks = 0;
251 	for (i = 0; i < MAX_SACK_BLKS; i++)
252 		tp->sackblks[i].start = tp->sackblks[i].end=0;
253 }
254 
255 /*
256  * Allocate struct sackhole.
257  */
258 static struct sackhole *
259 tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
260 {
261 	struct sackhole *hole;
262 
263 	if (tp->snd_numholes >= V_tcp_sack_maxholes ||
264 	    V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) {
265 		TCPSTAT_INC(tcps_sack_sboverflow);
266 		return NULL;
267 	}
268 
269 	hole = (struct sackhole *)uma_zalloc(V_sack_hole_zone, M_NOWAIT);
270 	if (hole == NULL)
271 		return NULL;
272 
273 	hole->start = start;
274 	hole->end = end;
275 	hole->rxmit = start;
276 
277 	tp->snd_numholes++;
278 	atomic_add_int(&V_tcp_sack_globalholes, 1);
279 
280 	return hole;
281 }
282 
283 /*
284  * Free struct sackhole.
285  */
286 static void
287 tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
288 {
289 
290 	uma_zfree(V_sack_hole_zone, hole);
291 
292 	tp->snd_numholes--;
293 	atomic_subtract_int(&V_tcp_sack_globalholes, 1);
294 
295 	KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
296 	KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
297 }
298 
299 /*
300  * Insert new SACK hole into scoreboard.
301  */
302 static struct sackhole *
303 tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
304     struct sackhole *after)
305 {
306 	struct sackhole *hole;
307 
308 	/* Allocate a new SACK hole. */
309 	hole = tcp_sackhole_alloc(tp, start, end);
310 	if (hole == NULL)
311 		return NULL;
312 
313 	/* Insert the new SACK hole into scoreboard. */
314 	if (after != NULL)
315 		TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
316 	else
317 		TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
318 
319 	/* Update SACK hint. */
320 	if (tp->sackhint.nexthole == NULL)
321 		tp->sackhint.nexthole = hole;
322 
323 	return hole;
324 }
325 
326 /*
327  * Remove SACK hole from scoreboard.
328  */
329 static void
330 tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
331 {
332 
333 	/* Update SACK hint. */
334 	if (tp->sackhint.nexthole == hole)
335 		tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
336 
337 	/* Remove this SACK hole. */
338 	TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
339 
340 	/* Free this SACK hole. */
341 	tcp_sackhole_free(tp, hole);
342 }
343 
344 /*
345  * Process cumulative ACK and the TCP SACK option to update the scoreboard.
346  * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
347  * the sequence space).
348  */
349 void
350 tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack)
351 {
352 	struct sackhole *cur, *temp;
353 	struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp;
354 	int i, j, num_sack_blks;
355 
356 	INP_WLOCK_ASSERT(tp->t_inpcb);
357 
358 	num_sack_blks = 0;
359 	/*
360 	 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
361 	 * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
362 	 */
363 	if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
364 		sack_blocks[num_sack_blks].start = tp->snd_una;
365 		sack_blocks[num_sack_blks++].end = th_ack;
366 	}
367 	/*
368 	 * Append received valid SACK blocks to sack_blocks[], but only if we
369 	 * received new blocks from the other side.
370 	 */
371 	if (to->to_flags & TOF_SACK) {
372 		tp->sackhint.sacked_bytes = 0;	/* reset */
373 		for (i = 0; i < to->to_nsacks; i++) {
374 			bcopy((to->to_sacks + i * TCPOLEN_SACK),
375 			    &sack, sizeof(sack));
376 			sack.start = ntohl(sack.start);
377 			sack.end = ntohl(sack.end);
378 			if (SEQ_GT(sack.end, sack.start) &&
379 			    SEQ_GT(sack.start, tp->snd_una) &&
380 			    SEQ_GT(sack.start, th_ack) &&
381 			    SEQ_LT(sack.start, tp->snd_max) &&
382 			    SEQ_GT(sack.end, tp->snd_una) &&
383 			    SEQ_LEQ(sack.end, tp->snd_max)) {
384 				sack_blocks[num_sack_blks++] = sack;
385 				tp->sackhint.sacked_bytes +=
386 				    (sack.end-sack.start);
387 			}
388 		}
389 	}
390 	/*
391 	 * Return if SND.UNA is not advanced and no valid SACK block is
392 	 * received.
393 	 */
394 	if (num_sack_blks == 0)
395 		return;
396 
397 	/*
398 	 * Sort the SACK blocks so we can update the scoreboard with just one
399 	 * pass. The overhead of sorting upto 4+1 elements is less than
400 	 * making upto 4+1 passes over the scoreboard.
401 	 */
402 	for (i = 0; i < num_sack_blks; i++) {
403 		for (j = i + 1; j < num_sack_blks; j++) {
404 			if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
405 				sack = sack_blocks[i];
406 				sack_blocks[i] = sack_blocks[j];
407 				sack_blocks[j] = sack;
408 			}
409 		}
410 	}
411 	if (TAILQ_EMPTY(&tp->snd_holes))
412 		/*
413 		 * Empty scoreboard. Need to initialize snd_fack (it may be
414 		 * uninitialized or have a bogus value). Scoreboard holes
415 		 * (from the sack blocks received) are created later below
416 		 * (in the logic that adds holes to the tail of the
417 		 * scoreboard).
418 		 */
419 		tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
420 	/*
421 	 * In the while-loop below, incoming SACK blocks (sack_blocks[]) and
422 	 * SACK holes (snd_holes) are traversed from their tails with just
423 	 * one pass in order to reduce the number of compares especially when
424 	 * the bandwidth-delay product is large.
425 	 *
426 	 * Note: Typically, in the first RTT of SACK recovery, the highest
427 	 * three or four SACK blocks with the same ack number are received.
428 	 * In the second RTT, if retransmitted data segments are not lost,
429 	 * the highest three or four SACK blocks with ack number advancing
430 	 * are received.
431 	 */
432 	sblkp = &sack_blocks[num_sack_blks - 1];	/* Last SACK block */
433 	tp->sackhint.last_sack_ack = sblkp->end;
434 	if (SEQ_LT(tp->snd_fack, sblkp->start)) {
435 		/*
436 		 * The highest SACK block is beyond fack.  Append new SACK
437 		 * hole at the tail.  If the second or later highest SACK
438 		 * blocks are also beyond the current fack, they will be
439 		 * inserted by way of hole splitting in the while-loop below.
440 		 */
441 		temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL);
442 		if (temp != NULL) {
443 			tp->snd_fack = sblkp->end;
444 			/* Go to the previous sack block. */
445 			sblkp--;
446 		} else {
447 			/*
448 			 * We failed to add a new hole based on the current
449 			 * sack block.  Skip over all the sack blocks that
450 			 * fall completely to the right of snd_fack and
451 			 * proceed to trim the scoreboard based on the
452 			 * remaining sack blocks.  This also trims the
453 			 * scoreboard for th_ack (which is sack_blocks[0]).
454 			 */
455 			while (sblkp >= sack_blocks &&
456 			       SEQ_LT(tp->snd_fack, sblkp->start))
457 				sblkp--;
458 			if (sblkp >= sack_blocks &&
459 			    SEQ_LT(tp->snd_fack, sblkp->end))
460 				tp->snd_fack = sblkp->end;
461 		}
462 	} else if (SEQ_LT(tp->snd_fack, sblkp->end))
463 		/* fack is advanced. */
464 		tp->snd_fack = sblkp->end;
465 	/* We must have at least one SACK hole in scoreboard. */
466 	KASSERT(!TAILQ_EMPTY(&tp->snd_holes),
467 	    ("SACK scoreboard must not be empty"));
468 	cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */
469 	/*
470 	 * Since the incoming sack blocks are sorted, we can process them
471 	 * making one sweep of the scoreboard.
472 	 */
473 	while (sblkp >= sack_blocks  && cur != NULL) {
474 		if (SEQ_GEQ(sblkp->start, cur->end)) {
475 			/*
476 			 * SACKs data beyond the current hole.  Go to the
477 			 * previous sack block.
478 			 */
479 			sblkp--;
480 			continue;
481 		}
482 		if (SEQ_LEQ(sblkp->end, cur->start)) {
483 			/*
484 			 * SACKs data before the current hole.  Go to the
485 			 * previous hole.
486 			 */
487 			cur = TAILQ_PREV(cur, sackhole_head, scblink);
488 			continue;
489 		}
490 		tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
491 		KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
492 		    ("sackhint bytes rtx >= 0"));
493 		if (SEQ_LEQ(sblkp->start, cur->start)) {
494 			/* Data acks at least the beginning of hole. */
495 			if (SEQ_GEQ(sblkp->end, cur->end)) {
496 				/* Acks entire hole, so delete hole. */
497 				temp = cur;
498 				cur = TAILQ_PREV(cur, sackhole_head, scblink);
499 				tcp_sackhole_remove(tp, temp);
500 				/*
501 				 * The sack block may ack all or part of the
502 				 * next hole too, so continue onto the next
503 				 * hole.
504 				 */
505 				continue;
506 			} else {
507 				/* Move start of hole forward. */
508 				cur->start = sblkp->end;
509 				cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
510 			}
511 		} else {
512 			/* Data acks at least the end of hole. */
513 			if (SEQ_GEQ(sblkp->end, cur->end)) {
514 				/* Move end of hole backward. */
515 				cur->end = sblkp->start;
516 				cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
517 			} else {
518 				/*
519 				 * ACKs some data in middle of a hole; need
520 				 * to split current hole
521 				 */
522 				temp = tcp_sackhole_insert(tp, sblkp->end,
523 				    cur->end, cur);
524 				if (temp != NULL) {
525 					if (SEQ_GT(cur->rxmit, temp->rxmit)) {
526 						temp->rxmit = cur->rxmit;
527 						tp->sackhint.sack_bytes_rexmit
528 						    += (temp->rxmit
529 						    - temp->start);
530 					}
531 					cur->end = sblkp->start;
532 					cur->rxmit = SEQ_MIN(cur->rxmit,
533 					    cur->end);
534 				}
535 			}
536 		}
537 		tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
538 		/*
539 		 * Testing sblkp->start against cur->start tells us whether
540 		 * we're done with the sack block or the sack hole.
541 		 * Accordingly, we advance one or the other.
542 		 */
543 		if (SEQ_LEQ(sblkp->start, cur->start))
544 			cur = TAILQ_PREV(cur, sackhole_head, scblink);
545 		else
546 			sblkp--;
547 	}
548 }
549 
550 /*
551  * Free all SACK holes to clear the scoreboard.
552  */
553 void
554 tcp_free_sackholes(struct tcpcb *tp)
555 {
556 	struct sackhole *q;
557 
558 	INP_WLOCK_ASSERT(tp->t_inpcb);
559 	while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
560 		tcp_sackhole_remove(tp, q);
561 	tp->sackhint.sack_bytes_rexmit = 0;
562 
563 	KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
564 	KASSERT(tp->sackhint.nexthole == NULL,
565 		("tp->sackhint.nexthole == NULL"));
566 }
567 
568 /*
569  * Partial ack handling within a sack recovery episode.  Keeping this very
570  * simple for now.  When a partial ack is received, force snd_cwnd to a value
571  * that will allow the sender to transmit no more than 2 segments.  If
572  * necessary, a better scheme can be adopted at a later point, but for now,
573  * the goal is to prevent the sender from bursting a large amount of data in
574  * the midst of sack recovery.
575  */
576 void
577 tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th)
578 {
579 	int num_segs = 1;
580 
581 	INP_WLOCK_ASSERT(tp->t_inpcb);
582 	tcp_timer_activate(tp, TT_REXMT, 0);
583 	tp->t_rtttime = 0;
584 	/* Send one or 2 segments based on how much new data was acked. */
585 	if ((BYTES_THIS_ACK(tp, th) / tp->t_maxseg) >= 2)
586 		num_segs = 2;
587 	tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
588 	    (tp->snd_nxt - tp->sack_newdata) + num_segs * tp->t_maxseg);
589 	if (tp->snd_cwnd > tp->snd_ssthresh)
590 		tp->snd_cwnd = tp->snd_ssthresh;
591 	tp->t_flags |= TF_ACKNOW;
592 	(void) tcp_output(tp);
593 }
594 
595 #if 0
596 /*
597  * Debug version of tcp_sack_output() that walks the scoreboard.  Used for
598  * now to sanity check the hint.
599  */
600 static struct sackhole *
601 tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
602 {
603 	struct sackhole *p;
604 
605 	INP_WLOCK_ASSERT(tp->t_inpcb);
606 	*sack_bytes_rexmt = 0;
607 	TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
608 		if (SEQ_LT(p->rxmit, p->end)) {
609 			if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
610 				continue;
611 			}
612 			*sack_bytes_rexmt += (p->rxmit - p->start);
613 			break;
614 		}
615 		*sack_bytes_rexmt += (p->rxmit - p->start);
616 	}
617 	return (p);
618 }
619 #endif
620 
621 /*
622  * Returns the next hole to retransmit and the number of retransmitted bytes
623  * from the scoreboard.  We store both the next hole and the number of
624  * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
625  * reception).  This avoids scoreboard traversals completely.
626  *
627  * The loop here will traverse *at most* one link.  Here's the argument.  For
628  * the loop to traverse more than 1 link before finding the next hole to
629  * retransmit, we would need to have at least 1 node following the current
630  * hint with (rxmit == end).  But, for all holes following the current hint,
631  * (start == rxmit), since we have not yet retransmitted from them.
632  * Therefore, in order to traverse more 1 link in the loop below, we need to
633  * have at least one node following the current hint with (start == rxmit ==
634  * end).  But that can't happen, (start == end) means that all the data in
635  * that hole has been sacked, in which case, the hole would have been removed
636  * from the scoreboard.
637  */
638 struct sackhole *
639 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
640 {
641 	struct sackhole *hole = NULL;
642 
643 	INP_WLOCK_ASSERT(tp->t_inpcb);
644 	*sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
645 	hole = tp->sackhint.nexthole;
646 	if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
647 		goto out;
648 	while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
649 		if (SEQ_LT(hole->rxmit, hole->end)) {
650 			tp->sackhint.nexthole = hole;
651 			break;
652 		}
653 	}
654 out:
655 	return (hole);
656 }
657 
658 /*
659  * After a timeout, the SACK list may be rebuilt.  This SACK information
660  * should be used to avoid retransmitting SACKed data.  This function
661  * traverses the SACK list to see if snd_nxt should be moved forward.
662  */
663 void
664 tcp_sack_adjust(struct tcpcb *tp)
665 {
666 	struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
667 
668 	INP_WLOCK_ASSERT(tp->t_inpcb);
669 	if (cur == NULL)
670 		return; /* No holes */
671 	if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack))
672 		return; /* We're already beyond any SACKed blocks */
673 	/*-
674 	 * Two cases for which we want to advance snd_nxt:
675 	 * i) snd_nxt lies between end of one hole and beginning of another
676 	 * ii) snd_nxt lies between end of last hole and snd_fack
677 	 */
678 	while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
679 		if (SEQ_LT(tp->snd_nxt, cur->end))
680 			return;
681 		if (SEQ_GEQ(tp->snd_nxt, p->start))
682 			cur = p;
683 		else {
684 			tp->snd_nxt = p->start;
685 			return;
686 		}
687 	}
688 	if (SEQ_LT(tp->snd_nxt, cur->end))
689 		return;
690 	tp->snd_nxt = tp->snd_fack;
691 }
692