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