tcp_sack.c revision 185571
1247835Skib/*-
2247835Skib * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3247835Skib *	The Regents of the University of California.
4247835Skib * All rights reserved.
5247835Skib *
6247835Skib * Redistribution and use in source and binary forms, with or without
7247835Skib * modification, are permitted provided that the following conditions
8247835Skib * are met:
9247835Skib * 1. Redistributions of source code must retain the above copyright
10247835Skib *    notice, this list of conditions and the following disclaimer.
11247835Skib * 2. Redistributions in binary form must reproduce the above copyright
12247835Skib *    notice, this list of conditions and the following disclaimer in the
13247835Skib *    documentation and/or other materials provided with the distribution.
14247835Skib * 4. Neither the name of the University nor the names of its contributors
15247835Skib *    may be used to endorse or promote products derived from this software
16247835Skib *    without specific prior written permission.
17247835Skib *
18247835Skib * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19247835Skib * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20247835Skib * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21247835Skib * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22247835Skib * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23247835Skib * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24247835Skib * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25247835Skib * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26247835Skib * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27247835Skib * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28247835Skib * SUCH DAMAGE.
29247835Skib *
30247835Skib *	@(#)tcp_sack.c	8.12 (Berkeley) 5/24/95
31247835Skib */
32247835Skib
33247835Skib/*-
34247835Skib *	@@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
35247835Skib *
36247835Skib * NRL grants permission for redistribution and use in source and binary
37247835Skib * forms, with or without modification, of the software and documentation
38247835Skib * created at NRL provided that the following conditions are met:
39247835Skib *
40247835Skib * 1. Redistributions of source code must retain the above copyright
41247835Skib *    notice, this list of conditions and the following disclaimer.
42247835Skib * 2. Redistributions in binary form must reproduce the above copyright
43247835Skib *    notice, this list of conditions and the following disclaimer in the
44247835Skib *    documentation and/or other materials provided with the distribution.
45247835Skib * 3. All advertising materials mentioning features or use of this software
46247835Skib *    must display the following acknowledgements:
47247835Skib *	This product includes software developed by the University of
48247835Skib *	California, Berkeley and its contributors.
49247835Skib *	This product includes software developed at the Information
50247835Skib *	Technology Division, US Naval Research Laboratory.
51247835Skib * 4. Neither the name of the NRL nor the names of its contributors
52247835Skib *    may be used to endorse or promote products derived from this software
53247835Skib *    without specific prior written permission.
54247835Skib *
55247835Skib * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
56247835Skib * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57247835Skib * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
58247835Skib * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
59247835Skib * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
60247835Skib * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
61247835Skib * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
62247835Skib * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
63247835Skib * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
64247835Skib * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
65247835Skib * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66247835Skib *
67247835Skib * The views and conclusions contained in the software and documentation
68247835Skib * are those of the authors and should not be interpreted as representing
69247835Skib * official policies, either expressed or implied, of the US Naval
70247835Skib * Research Laboratory (NRL).
71247835Skib */
72247835Skib
73247835Skib#include <sys/cdefs.h>
74247835Skib__FBSDID("$FreeBSD: head/sys/netinet/tcp_sack.c 185571 2008-12-02 21:37:28Z bz $");
75247835Skib
76247835Skib#include "opt_inet.h"
77247835Skib#include "opt_inet6.h"
78247835Skib#include "opt_tcpdebug.h"
79247835Skib
80247835Skib#include <sys/param.h>
81247835Skib#include <sys/systm.h>
82247835Skib#include <sys/kernel.h>
83247835Skib#include <sys/sysctl.h>
84247835Skib#include <sys/malloc.h>
85247835Skib#include <sys/mbuf.h>
86247835Skib#include <sys/proc.h>		/* for proc0 declaration */
87247835Skib#include <sys/protosw.h>
88247835Skib#include <sys/socket.h>
89247835Skib#include <sys/socketvar.h>
90247835Skib#include <sys/syslog.h>
91247835Skib#include <sys/systm.h>
92247835Skib#include <sys/vimage.h>
93247835Skib
94247835Skib#include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
95247835Skib
96247835Skib#include <vm/uma.h>
97247835Skib
98247835Skib#include <net/if.h>
99247835Skib#include <net/route.h>
100247835Skib
101247835Skib#include <netinet/in.h>
102247835Skib#include <netinet/in_systm.h>
103247835Skib#include <netinet/ip.h>
104247835Skib#include <netinet/in_var.h>
105247835Skib#include <netinet/in_pcb.h>
106247835Skib#include <netinet/ip_var.h>
107247835Skib#include <netinet/ip6.h>
108247835Skib#include <netinet/icmp6.h>
109247835Skib#include <netinet6/nd6.h>
110247835Skib#include <netinet6/ip6_var.h>
111247835Skib#include <netinet6/in6_pcb.h>
112247835Skib#include <netinet/tcp.h>
113247835Skib#include <netinet/tcp_fsm.h>
114247835Skib#include <netinet/tcp_seq.h>
115247835Skib#include <netinet/tcp_timer.h>
116247835Skib#include <netinet/tcp_var.h>
117247835Skib#include <netinet6/tcp6_var.h>
118247835Skib#include <netinet/tcpip.h>
119247835Skib#ifdef TCPDEBUG
120247835Skib#include <netinet/tcp_debug.h>
121247835Skib#endif /* TCPDEBUG */
122247835Skib#include <netinet/vinet.h>
123247835Skib
124247835Skib#include <machine/in_cksum.h>
125247835Skib
126247835Skibextern struct uma_zone *sack_hole_zone;
127247835Skib
128247835Skib#ifdef VIMAGE_GLOBALS
129247835Skibint tcp_do_sack;
130247835Skibint tcp_sack_maxholes;
131247835Skibint tcp_sack_globalmaxholes;
132247835Skibint tcp_sack_globalholes;
133247835Skib#endif
134247835Skib
135247835SkibSYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, 0, "TCP SACK");
136247835SkibSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_sack, OID_AUTO, enable,
137247835Skib    CTLFLAG_RW, tcp_do_sack, 0, "Enable/Disable TCP SACK support");
138247835SkibTUNABLE_INT("net.inet.tcp.sack.enable", &tcp_do_sack);
139247835Skib
140247835SkibSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_sack, OID_AUTO, maxholes,
141247835Skib    CTLFLAG_RW, tcp_sack_maxholes, 0,
142247835Skib    "Maximum number of TCP SACK holes allowed per connection");
143247835Skib
144247835SkibSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_sack, OID_AUTO, globalmaxholes,
145247835Skib    CTLFLAG_RW, tcp_sack_globalmaxholes, 0,
146247835Skib    "Global maximum number of TCP SACK holes");
147247835Skib
148247835SkibSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_sack, OID_AUTO, globalholes,
149247835Skib    CTLFLAG_RD, tcp_sack_globalholes, 0,
150    "Global number of TCP SACK holes currently allocated");
151
152/*
153 * This function is called upon receipt of new valid data (while not in
154 * header prediction mode), and it updates the ordered list of sacks.
155 */
156void
157tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
158{
159	/*
160	 * First reported block MUST be the most recent one.  Subsequent
161	 * blocks SHOULD be in the order in which they arrived at the
162	 * receiver.  These two conditions make the implementation fully
163	 * compliant with RFC 2018.
164	 */
165	struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
166	int num_head, num_saved, i;
167
168	INP_WLOCK_ASSERT(tp->t_inpcb);
169
170	/* Check arguments. */
171	KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end"));
172
173	/* SACK block for the received segment. */
174	head_blk.start = rcv_start;
175	head_blk.end = rcv_end;
176
177	/*
178	 * Merge updated SACK blocks into head_blk, and save unchanged SACK
179	 * blocks into saved_blks[].  num_saved will have the number of the
180	 * saved SACK blocks.
181	 */
182	num_saved = 0;
183	for (i = 0; i < tp->rcv_numsacks; i++) {
184		tcp_seq start = tp->sackblks[i].start;
185		tcp_seq end = tp->sackblks[i].end;
186		if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
187			/*
188			 * Discard this SACK block.
189			 */
190		} else if (SEQ_LEQ(head_blk.start, end) &&
191			   SEQ_GEQ(head_blk.end, start)) {
192			/*
193			 * Merge this SACK block into head_blk.  This SACK
194			 * block itself will be discarded.
195			 */
196			if (SEQ_GT(head_blk.start, start))
197				head_blk.start = start;
198			if (SEQ_LT(head_blk.end, end))
199				head_blk.end = end;
200		} else {
201			/*
202			 * Save this SACK block.
203			 */
204			saved_blks[num_saved].start = start;
205			saved_blks[num_saved].end = end;
206			num_saved++;
207		}
208	}
209
210	/*
211	 * Update SACK list in tp->sackblks[].
212	 */
213	num_head = 0;
214	if (SEQ_GT(head_blk.start, tp->rcv_nxt)) {
215		/*
216		 * The received data segment is an out-of-order segment.  Put
217		 * head_blk at the top of SACK list.
218		 */
219		tp->sackblks[0] = head_blk;
220		num_head = 1;
221		/*
222		 * If the number of saved SACK blocks exceeds its limit,
223		 * discard the last SACK block.
224		 */
225		if (num_saved >= MAX_SACK_BLKS)
226			num_saved--;
227	}
228	if (num_saved > 0) {
229		/*
230		 * Copy the saved SACK blocks back.
231		 */
232		bcopy(saved_blks, &tp->sackblks[num_head],
233		      sizeof(struct sackblk) * num_saved);
234	}
235
236	/* Save the number of SACK blocks. */
237	tp->rcv_numsacks = num_head + num_saved;
238}
239
240/*
241 * Delete all receiver-side SACK information.
242 */
243void
244tcp_clean_sackreport(struct tcpcb *tp)
245{
246	int i;
247
248	INP_WLOCK_ASSERT(tp->t_inpcb);
249	tp->rcv_numsacks = 0;
250	for (i = 0; i < MAX_SACK_BLKS; i++)
251		tp->sackblks[i].start = tp->sackblks[i].end=0;
252}
253
254/*
255 * Allocate struct sackhole.
256 */
257static struct sackhole *
258tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
259{
260	INIT_VNET_INET(tp->t_inpcb->inp_vnet);
261	struct sackhole *hole;
262
263	if (tp->snd_numholes >= V_tcp_sack_maxholes ||
264	    V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) {
265		V_tcpstat.tcps_sack_sboverflow++;
266		return NULL;
267	}
268
269	hole = (struct sackhole *)uma_zalloc(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	V_tcp_sack_globalholes++;
279
280	return hole;
281}
282
283/*
284 * Free struct sackhole.
285 */
286static void
287tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
288{
289	INIT_VNET_INET(tp->t_vnet);
290
291	uma_zfree(sack_hole_zone, hole);
292
293	tp->snd_numholes--;
294	V_tcp_sack_globalholes--;
295
296	KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
297	KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
298}
299
300/*
301 * Insert new SACK hole into scoreboard.
302 */
303static struct sackhole *
304tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
305    struct sackhole *after)
306{
307	struct sackhole *hole;
308
309	/* Allocate a new SACK hole. */
310	hole = tcp_sackhole_alloc(tp, start, end);
311	if (hole == NULL)
312		return NULL;
313
314	/* Insert the new SACK hole into scoreboard. */
315	if (after != NULL)
316		TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
317	else
318		TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
319
320	/* Update SACK hint. */
321	if (tp->sackhint.nexthole == NULL)
322		tp->sackhint.nexthole = hole;
323
324	return hole;
325}
326
327/*
328 * Remove SACK hole from scoreboard.
329 */
330static void
331tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
332{
333
334	/* Update SACK hint. */
335	if (tp->sackhint.nexthole == hole)
336		tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
337
338	/* Remove this SACK hole. */
339	TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
340
341	/* Free this SACK hole. */
342	tcp_sackhole_free(tp, hole);
343}
344
345/*
346 * Process cumulative ACK and the TCP SACK option to update the scoreboard.
347 * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
348 * the sequence space).
349 */
350void
351tcp_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;
356
357	INP_WLOCK_ASSERT(tp->t_inpcb);
358
359	num_sack_blks = 0;
360	/*
361	 * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
362	 * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
363	 */
364	if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
365		sack_blocks[num_sack_blks].start = tp->snd_una;
366		sack_blocks[num_sack_blks++].end = th_ack;
367	}
368	/*
369	 * Append received valid SACK blocks to sack_blocks[], but only if we
370	 * received new blocks from the other side.
371	 */
372	if (to->to_flags & TOF_SACK) {
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		}
386	}
387	/*
388	 * Return if SND.UNA is not advanced and no valid SACK block is
389	 * received.
390	 */
391	if (num_sack_blks == 0)
392		return;
393
394	/*
395	 * Sort the SACK blocks so we can update the scoreboard with just one
396	 * pass. The overhead of sorting upto 4+1 elements is less than
397	 * making upto 4+1 passes over the scoreboard.
398	 */
399	for (i = 0; i < num_sack_blks; i++) {
400		for (j = i + 1; j < num_sack_blks; j++) {
401			if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
402				sack = sack_blocks[i];
403				sack_blocks[i] = sack_blocks[j];
404				sack_blocks[j] = sack;
405			}
406		}
407	}
408	if (TAILQ_EMPTY(&tp->snd_holes))
409		/*
410		 * Empty scoreboard. Need to initialize snd_fack (it may be
411		 * uninitialized or have a bogus value). Scoreboard holes
412		 * (from the sack blocks received) are created later below
413		 * (in the logic that adds holes to the tail of the
414		 * scoreboard).
415		 */
416		tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
417	/*
418	 * In the while-loop below, incoming SACK blocks (sack_blocks[]) and
419	 * SACK holes (snd_holes) are traversed from their tails with just
420	 * one pass in order to reduce the number of compares especially when
421	 * the bandwidth-delay product is large.
422	 *
423	 * Note: Typically, in the first RTT of SACK recovery, the highest
424	 * three or four SACK blocks with the same ack number are received.
425	 * In the second RTT, if retransmitted data segments are not lost,
426	 * the highest three or four SACK blocks with ack number advancing
427	 * are received.
428	 */
429	sblkp = &sack_blocks[num_sack_blks - 1];	/* Last SACK block */
430	if (SEQ_LT(tp->snd_fack, sblkp->start)) {
431		/*
432		 * The highest SACK block is beyond fack.  Append new SACK
433		 * hole at the tail.  If the second or later highest SACK
434		 * blocks are also beyond the current fack, they will be
435		 * inserted by way of hole splitting in the while-loop below.
436		 */
437		temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL);
438		if (temp != NULL) {
439			tp->snd_fack = sblkp->end;
440			/* Go to the previous sack block. */
441			sblkp--;
442		} else {
443			/*
444			 * We failed to add a new hole based on the current
445			 * sack block.  Skip over all the sack blocks that
446			 * fall completely to the right of snd_fack and
447			 * proceed to trim the scoreboard based on the
448			 * remaining sack blocks.  This also trims the
449			 * scoreboard for th_ack (which is sack_blocks[0]).
450			 */
451			while (sblkp >= sack_blocks &&
452			       SEQ_LT(tp->snd_fack, sblkp->start))
453				sblkp--;
454			if (sblkp >= sack_blocks &&
455			    SEQ_LT(tp->snd_fack, sblkp->end))
456				tp->snd_fack = sblkp->end;
457		}
458	} else if (SEQ_LT(tp->snd_fack, sblkp->end))
459		/* fack is advanced. */
460		tp->snd_fack = sblkp->end;
461	/* We must have at least one SACK hole in scoreboard. */
462	KASSERT(!TAILQ_EMPTY(&tp->snd_holes),
463	    ("SACK scoreboard must not be empty"));
464	cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */
465	/*
466	 * Since the incoming sack blocks are sorted, we can process them
467	 * making one sweep of the scoreboard.
468	 */
469	while (sblkp >= sack_blocks  && cur != NULL) {
470		if (SEQ_GEQ(sblkp->start, cur->end)) {
471			/*
472			 * SACKs data beyond the current hole.  Go to the
473			 * previous sack block.
474			 */
475			sblkp--;
476			continue;
477		}
478		if (SEQ_LEQ(sblkp->end, cur->start)) {
479			/*
480			 * SACKs data before the current hole.  Go to the
481			 * previous hole.
482			 */
483			cur = TAILQ_PREV(cur, sackhole_head, scblink);
484			continue;
485		}
486		tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
487		KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
488		    ("sackhint bytes rtx >= 0"));
489		if (SEQ_LEQ(sblkp->start, cur->start)) {
490			/* Data acks at least the beginning of hole. */
491			if (SEQ_GEQ(sblkp->end, cur->end)) {
492				/* Acks entire hole, so delete hole. */
493				temp = cur;
494				cur = TAILQ_PREV(cur, sackhole_head, scblink);
495				tcp_sackhole_remove(tp, temp);
496				/*
497				 * The sack block may ack all or part of the
498				 * next hole too, so continue onto the next
499				 * hole.
500				 */
501				continue;
502			} else {
503				/* Move start of hole forward. */
504				cur->start = sblkp->end;
505				cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
506			}
507		} else {
508			/* Data acks at least the end of hole. */
509			if (SEQ_GEQ(sblkp->end, cur->end)) {
510				/* Move end of hole backward. */
511				cur->end = sblkp->start;
512				cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
513			} else {
514				/*
515				 * ACKs some data in middle of a hole; need
516				 * to split current hole
517				 */
518				temp = tcp_sackhole_insert(tp, sblkp->end,
519				    cur->end, cur);
520				if (temp != NULL) {
521					if (SEQ_GT(cur->rxmit, temp->rxmit)) {
522						temp->rxmit = cur->rxmit;
523						tp->sackhint.sack_bytes_rexmit
524						    += (temp->rxmit
525						    - temp->start);
526					}
527					cur->end = sblkp->start;
528					cur->rxmit = SEQ_MIN(cur->rxmit,
529					    cur->end);
530				}
531			}
532		}
533		tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
534		/*
535		 * Testing sblkp->start against cur->start tells us whether
536		 * we're done with the sack block or the sack hole.
537		 * Accordingly, we advance one or the other.
538		 */
539		if (SEQ_LEQ(sblkp->start, cur->start))
540			cur = TAILQ_PREV(cur, sackhole_head, scblink);
541		else
542			sblkp--;
543	}
544}
545
546/*
547 * Free all SACK holes to clear the scoreboard.
548 */
549void
550tcp_free_sackholes(struct tcpcb *tp)
551{
552	struct sackhole *q;
553
554	INP_WLOCK_ASSERT(tp->t_inpcb);
555	while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
556		tcp_sackhole_remove(tp, q);
557	tp->sackhint.sack_bytes_rexmit = 0;
558
559	KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
560	KASSERT(tp->sackhint.nexthole == NULL,
561		("tp->sackhint.nexthole == NULL"));
562}
563
564/*
565 * Partial ack handling within a sack recovery episode.  Keeping this very
566 * simple for now.  When a partial ack is received, force snd_cwnd to a value
567 * that will allow the sender to transmit no more than 2 segments.  If
568 * necessary, a better scheme can be adopted at a later point, but for now,
569 * the goal is to prevent the sender from bursting a large amount of data in
570 * the midst of sack recovery.
571 */
572void
573tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th)
574{
575	int num_segs = 1;
576
577	INP_WLOCK_ASSERT(tp->t_inpcb);
578	tcp_timer_activate(tp, TT_REXMT, 0);
579	tp->t_rtttime = 0;
580	/* Send one or 2 segments based on how much new data was acked. */
581	if (((th->th_ack - tp->snd_una) / tp->t_maxseg) > 2)
582		num_segs = 2;
583	tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
584	    (tp->snd_nxt - tp->sack_newdata) + num_segs * tp->t_maxseg);
585	if (tp->snd_cwnd > tp->snd_ssthresh)
586		tp->snd_cwnd = tp->snd_ssthresh;
587	tp->t_flags |= TF_ACKNOW;
588	(void) tcp_output(tp);
589}
590
591#if 0
592/*
593 * Debug version of tcp_sack_output() that walks the scoreboard.  Used for
594 * now to sanity check the hint.
595 */
596static struct sackhole *
597tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
598{
599	struct sackhole *p;
600
601	INP_WLOCK_ASSERT(tp->t_inpcb);
602	*sack_bytes_rexmt = 0;
603	TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
604		if (SEQ_LT(p->rxmit, p->end)) {
605			if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
606				continue;
607			}
608			*sack_bytes_rexmt += (p->rxmit - p->start);
609			break;
610		}
611		*sack_bytes_rexmt += (p->rxmit - p->start);
612	}
613	return (p);
614}
615#endif
616
617/*
618 * Returns the next hole to retransmit and the number of retransmitted bytes
619 * from the scoreboard.  We store both the next hole and the number of
620 * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
621 * reception).  This avoids scoreboard traversals completely.
622 *
623 * The loop here will traverse *at most* one link.  Here's the argument.  For
624 * the loop to traverse more than 1 link before finding the next hole to
625 * retransmit, we would need to have at least 1 node following the current
626 * hint with (rxmit == end).  But, for all holes following the current hint,
627 * (start == rxmit), since we have not yet retransmitted from them.
628 * Therefore, in order to traverse more 1 link in the loop below, we need to
629 * have at least one node following the current hint with (start == rxmit ==
630 * end).  But that can't happen, (start == end) means that all the data in
631 * that hole has been sacked, in which case, the hole would have been removed
632 * from the scoreboard.
633 */
634struct sackhole *
635tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
636{
637	struct sackhole *hole = NULL;
638
639	INP_WLOCK_ASSERT(tp->t_inpcb);
640	*sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
641	hole = tp->sackhint.nexthole;
642	if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
643		goto out;
644	while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
645		if (SEQ_LT(hole->rxmit, hole->end)) {
646			tp->sackhint.nexthole = hole;
647			break;
648		}
649	}
650out:
651	return (hole);
652}
653
654/*
655 * After a timeout, the SACK list may be rebuilt.  This SACK information
656 * should be used to avoid retransmitting SACKed data.  This function
657 * traverses the SACK list to see if snd_nxt should be moved forward.
658 */
659void
660tcp_sack_adjust(struct tcpcb *tp)
661{
662	struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
663
664	INP_WLOCK_ASSERT(tp->t_inpcb);
665	if (cur == NULL)
666		return; /* No holes */
667	if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack))
668		return; /* We're already beyond any SACKed blocks */
669	/*-
670	 * Two cases for which we want to advance snd_nxt:
671	 * i) snd_nxt lies between end of one hole and beginning of another
672	 * ii) snd_nxt lies between end of last hole and snd_fack
673	 */
674	while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
675		if (SEQ_LT(tp->snd_nxt, cur->end))
676			return;
677		if (SEQ_GEQ(tp->snd_nxt, p->start))
678			cur = p;
679		else {
680			tp->snd_nxt = p->start;
681			return;
682		}
683	}
684	if (SEQ_LT(tp->snd_nxt, cur->end))
685		return;
686	tp->snd_nxt = tp->snd_fack;
687}
688