xref: /netbsd-src/sys/netinet/tcp_sack.c (revision 88fcb00c0357f2d7c1774f86a352637bfda96184)
1 /* $NetBSD: tcp_sack.c,v 1.26 2011/04/14 15:54:31 yamt Exp $ */
2 
3 /*
4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Kentaro A. Kurahone.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 4. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)tcp_sack.c	8.12 (Berkeley) 5/24/95
61  * $FreeBSD: src/sys/netinet/tcp_sack.c,v 1.3.2.2 2004/12/25 23:02:57 rwatson Exp $
62  */
63 
64 /*
65  *	@@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
66  *
67  * NRL grants permission for redistribution and use in source and binary
68  * forms, with or without modification, of the software and documentation
69  * created at NRL provided that the following conditions are met:
70  *
71  * 1. Redistributions of source code must retain the above copyright
72  *    notice, this list of conditions and the following disclaimer.
73  * 2. Redistributions in binary form must reproduce the above copyright
74  *    notice, this list of conditions and the following disclaimer in the
75  *    documentation and/or other materials provided with the distribution.
76  * 3. All advertising materials mentioning features or use of this software
77  *    must display the following acknowledgements:
78  *	This product includes software developed by the University of
79  *	California, Berkeley and its contributors.
80  *	This product includes software developed at the Information
81  *	Technology Division, US Naval Research Laboratory.
82  * 4. Neither the name of the NRL nor the names of its contributors
83  *    may be used to endorse or promote products derived from this software
84  *    without specific prior written permission.
85  *
86  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
87  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
88  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
89  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
90  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
91  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
92  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
93  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
94  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
95  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
96  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97  *
98  * The views and conclusions contained in the software and documentation
99  * are those of the authors and should not be interpreted as representing
100  * official policies, either expressed or implied, of the US Naval
101  * Research Laboratory (NRL).
102  */
103 
104 #include <sys/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: tcp_sack.c,v 1.26 2011/04/14 15:54:31 yamt Exp $");
106 
107 #include "opt_inet.h"
108 #include "opt_ipsec.h"
109 #include "opt_inet_csum.h"
110 #include "opt_tcp_debug.h"
111 #include "opt_ddb.h"
112 
113 #include <sys/param.h>
114 #include <sys/systm.h>
115 #include <sys/malloc.h>
116 #include <sys/mbuf.h>
117 #include <sys/protosw.h>
118 #include <sys/socket.h>
119 #include <sys/socketvar.h>
120 #include <sys/errno.h>
121 #include <sys/syslog.h>
122 #include <sys/pool.h>
123 #include <sys/domain.h>
124 #include <sys/kernel.h>
125 
126 #include <net/if.h>
127 #include <net/route.h>
128 #include <net/if_types.h>
129 
130 #include <netinet/in.h>
131 #include <netinet/in_systm.h>
132 #include <netinet/ip.h>
133 #include <netinet/in_pcb.h>
134 #include <netinet/in_var.h>
135 #include <netinet/ip_var.h>
136 
137 #ifdef INET6
138 #ifndef INET
139 #include <netinet/in.h>
140 #endif
141 #include <netinet/ip6.h>
142 #include <netinet6/ip6_var.h>
143 #include <netinet6/in6_pcb.h>
144 #include <netinet6/ip6_var.h>
145 #include <netinet6/in6_var.h>
146 #include <netinet/icmp6.h>
147 #include <netinet6/nd6.h>
148 #endif
149 
150 #ifndef INET6
151 /* always need ip6.h for IP6_EXTHDR_GET */
152 #include <netinet/ip6.h>
153 #endif
154 
155 #include <netinet/tcp.h>
156 #include <netinet/tcp_fsm.h>
157 #include <netinet/tcp_seq.h>
158 #include <netinet/tcp_timer.h>
159 #include <netinet/tcp_var.h>
160 #include <netinet/tcpip.h>
161 #include <netinet/tcp_debug.h>
162 
163 #include <machine/stdarg.h>
164 
165 /* SACK block pool. */
166 static struct pool sackhole_pool;
167 
168 void
169 tcp_sack_init()
170 {
171 
172 	pool_init(&sackhole_pool, sizeof(struct sackhole), 0, 0, 0,
173 	    "sackholepl", NULL, IPL_SOFTNET);
174 }
175 
176 static struct sackhole *
177 sack_allochole(struct tcpcb *tp)
178 {
179 	struct sackhole *hole;
180 
181 	if (tp->snd_numholes >= tcp_sack_tp_maxholes ||
182 	    tcp_sack_globalholes >= tcp_sack_globalmaxholes) {
183 		return NULL;
184 	}
185 	hole = pool_get(&sackhole_pool, PR_NOWAIT);
186 	if (hole == NULL) {
187 		return NULL;
188 	}
189 	tp->snd_numholes++;
190 	tcp_sack_globalholes++;
191 
192 	return hole;
193 }
194 
195 static struct sackhole *
196 sack_inserthole(struct tcpcb *tp, tcp_seq start, tcp_seq end,
197     struct sackhole *prev)
198 {
199 	struct sackhole *hole;
200 
201 	hole = sack_allochole(tp);
202 	if (hole == NULL) {
203 		return NULL;
204 	}
205 	hole->start = hole->rxmit = start;
206 	hole->end = end;
207 	if (prev != NULL) {
208 		TAILQ_INSERT_AFTER(&tp->snd_holes, prev, hole, sackhole_q);
209 	} else {
210 		TAILQ_INSERT_TAIL(&tp->snd_holes, hole, sackhole_q);
211 	}
212 	return hole;
213 }
214 
215 static struct sackhole *
216 sack_removehole(struct tcpcb *tp, struct sackhole *hole)
217 {
218 	struct sackhole *next;
219 
220 	next = TAILQ_NEXT(hole, sackhole_q);
221 	tp->snd_numholes--;
222 	tcp_sack_globalholes--;
223 	TAILQ_REMOVE(&tp->snd_holes, hole, sackhole_q);
224 	pool_put(&sackhole_pool, hole);
225 
226 	return next;
227 }
228 
229 /*
230  * tcp_new_dsack: record the reception of a duplicated segment.
231  */
232 
233 void
234 tcp_new_dsack(struct tcpcb *tp, tcp_seq seq, u_int32_t len)
235 {
236 
237 	if (TCP_SACK_ENABLED(tp)) {
238 		tp->rcv_dsack_block.left = seq;
239 		tp->rcv_dsack_block.right = seq + len;
240 		tp->rcv_sack_flags |= TCPSACK_HAVED;
241 	}
242 }
243 
244 /*
245  * tcp_sack_option: parse the given SACK option and update the scoreboard.
246  */
247 
248 void
249 tcp_sack_option(struct tcpcb *tp, const struct tcphdr *th, const u_char *cp,
250     int optlen)
251 {
252 	struct sackblk
253 	    t_sack_block[(MAX_TCPOPTLEN - 2) / (sizeof(u_int32_t) * 2)];
254 	struct sackblk *sack = NULL;
255 	struct sackhole *cur = NULL;
256 	struct sackhole *tmp = NULL;
257 	const char *lp = cp + 2;
258 	int i, j, num_sack_blks;
259 	tcp_seq left, right, acked;
260 
261 	/*
262 	 * If we aren't processing SACK responses, this is not an ACK
263 	 * or the peer sends us a sack option with invalid length, don't
264 	 * update the scoreboard.
265 	 */
266 	if (!TCP_SACK_ENABLED(tp) || ((th->th_flags & TH_ACK) == 0) ||
267 			(optlen % 8 != 2 || optlen < 10)) {
268 		return;
269 	}
270 
271 	/*
272 	 * If we don't want any SACK holes to be allocated, just return.
273 	 */
274 	if (tcp_sack_globalmaxholes == 0 || tcp_sack_tp_maxholes == 0) {
275 		return;
276 	}
277 
278 	/* If the ACK is outside [snd_una, snd_max], ignore the SACK options. */
279 	if (SEQ_LT(th->th_ack, tp->snd_una) || SEQ_GT(th->th_ack, tp->snd_max))
280 		return;
281 
282 	/*
283 	 * Extract SACK blocks.
284 	 *
285 	 * Note that t_sack_block is sorted so that we only need to do
286 	 * one pass over the sequence number space. (SACK "fast-path")
287 	 */
288 	num_sack_blks = optlen / 8;
289 	acked = (SEQ_GT(th->th_ack, tp->snd_una)) ? th->th_ack : tp->snd_una;
290 	for (i = 0; i < num_sack_blks; i++, lp += sizeof(uint32_t) * 2) {
291 		memcpy(&left, lp, sizeof(uint32_t));
292 		memcpy(&right, lp + sizeof(uint32_t), sizeof(uint32_t));
293 		left = ntohl(left);
294 		right = ntohl(right);
295 
296 		if (SEQ_LEQ(right, acked) || SEQ_GT(right, tp->snd_max) ||
297 		    SEQ_GEQ(left, right)) {
298 			/* SACK entry that's old, or invalid. */
299 			i--;
300 			num_sack_blks--;
301 			continue;
302 		}
303 
304 		/* Insertion sort. */
305 		for (j = i; (j > 0) && SEQ_LT(left, t_sack_block[j - 1].left);
306 		    j--) {
307 			t_sack_block[j].left = t_sack_block[j - 1].left;
308 			t_sack_block[j].right = t_sack_block[j - 1].right;
309 		}
310 		t_sack_block[j].left = left;
311 		t_sack_block[j].right = right;
312 	}
313 
314 	/* Update the scoreboard. */
315 	cur = TAILQ_FIRST(&tp->snd_holes);
316 	for (i = 0; i < num_sack_blks; i++) {
317 		sack = &t_sack_block[i];
318 		/*
319 		 * FACK TCP.  Update snd_fack so we can enter Fast
320 		 * Recovery early.
321 		 */
322 		if (SEQ_GEQ(sack->right, tp->snd_fack))
323 			tp->snd_fack = sack->right;
324 
325 		if (TAILQ_EMPTY(&tp->snd_holes)) {
326 			/* First hole. */
327 			cur = sack_inserthole(tp, th->th_ack, sack->left, NULL);
328 			if (cur == NULL) {
329 				/* ENOBUFS, bail out*/
330 				return;
331 			}
332 			tp->rcv_lastsack = sack->right;
333 			continue; /* With next sack block */
334 		}
335 
336 		/* Go through the list of holes. */
337 		while (cur) {
338 			if (SEQ_LEQ(sack->right, cur->start))
339 				/* SACKs data before the current hole */
340 				break; /* No use going through more holes */
341 
342 			if (SEQ_GEQ(sack->left, cur->end)) {
343 				/* SACKs data beyond the current hole */
344 				cur = TAILQ_NEXT(cur, sackhole_q);
345 				continue;
346 			}
347 
348 			if (SEQ_LEQ(sack->left, cur->start)) {
349 				/* Data acks at least the beginning of hole */
350 				if (SEQ_GEQ(sack->right, cur->end)) {
351 					/* Acks entire hole, so delete hole */
352 					cur = sack_removehole(tp, cur);
353 					break;
354 				}
355 
356 				/* Otherwise, move start of hole forward */
357 				cur->start = sack->right;
358 				cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
359 				break;
360 			}
361 
362 			if (SEQ_GEQ(sack->right, cur->end)) {
363 				/* Move end of hole backward. */
364 				cur->end = sack->left;
365 				cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
366 				cur = TAILQ_NEXT(cur, sackhole_q);
367 				break;
368 			}
369 
370 			if (SEQ_LT(cur->start, sack->left) &&
371 			    SEQ_GT(cur->end, sack->right)) {
372 				/*
373 				 * ACKs some data in middle of a hole; need to
374 				 * split current hole
375 				 */
376 				tmp = sack_inserthole(tp, sack->right, cur->end,
377 				    cur);
378 				if (tmp == NULL) {
379 					return;
380 				}
381 				tmp->rxmit = SEQ_MAX(cur->rxmit, tmp->start);
382 				cur->end = sack->left;
383 				cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
384 				cur = tmp;
385 				break;
386 			}
387 		}
388 
389 		/* At this point, we have reached the tail of the list. */
390 		if (SEQ_LT(tp->rcv_lastsack, sack->left)) {
391 			/*
392 			 * Need to append new hole at end.
393 			 */
394 			cur = sack_inserthole(tp, tp->rcv_lastsack, sack->left,
395 			    NULL);
396 			if (cur == NULL) {
397 				return;
398 			}
399 		}
400 		if (SEQ_LT(tp->rcv_lastsack, sack->right)) {
401 			tp->rcv_lastsack = sack->right;
402 		}
403 	}
404 }
405 
406 /*
407  * tcp_del_sackholes: remove holes covered by a cumulative ACK.
408  */
409 
410 void
411 tcp_del_sackholes(struct tcpcb *tp, const struct tcphdr *th)
412 {
413 	/* Max because this could be an older ack that just arrived. */
414 	tcp_seq lastack = SEQ_GT(th->th_ack, tp->snd_una) ?
415 		th->th_ack : tp->snd_una;
416 	struct sackhole *cur = TAILQ_FIRST(&tp->snd_holes);
417 
418 	while (cur) {
419 		if (SEQ_LEQ(cur->end, lastack)) {
420 			cur = sack_removehole(tp, cur);
421 		} else if (SEQ_LT(cur->start, lastack)) {
422 			cur->start = lastack;
423 			if (SEQ_LT(cur->rxmit, cur->start))
424 				cur->rxmit = cur->start;
425 			break;
426 		} else
427 			break;
428 	}
429 }
430 
431 /*
432  * tcp_free_sackholes: clear the scoreboard.
433  */
434 
435 void
436 tcp_free_sackholes(struct tcpcb *tp)
437 {
438 	struct sackhole *sack;
439 
440 	/* Free up the SACK hole list. */
441 	while ((sack = TAILQ_FIRST(&tp->snd_holes)) != NULL) {
442 		sack_removehole(tp, sack);
443 	}
444 	KASSERT(tp->snd_numholes == 0);
445 }
446 
447 /*
448  * Implements the SACK response to a new ack, checking for partial acks
449  * in fast recovery.
450  */
451 void
452 tcp_sack_newack(struct tcpcb *tp, const struct tcphdr *th)
453 {
454 	if (tp->t_partialacks < 0) {
455 		/*
456 		 * Not in fast recovery.  Reset the duplicate ack
457 		 * counter.
458 		 */
459 		tp->t_dupacks = 0;
460 	} else if (SEQ_LT(th->th_ack, tp->snd_recover)) {
461 		/*
462 		 * Partial ack handling within a sack recovery episode.
463 		 * Keeping this very simple for now. When a partial ack
464 		 * is received, force snd_cwnd to a value that will allow
465 		 * the sender to transmit no more than 2 segments.
466 		 * If necessary, a fancier scheme can be adopted at a
467 		 * later point, but for now, the goal is to prevent the
468 		 * sender from bursting a large amount of data in the midst
469 		 * of sack recovery.
470 		 */
471 		int num_segs = 1;
472 		int sack_bytes_rxmt = 0;
473 
474 		tp->t_partialacks++;
475 		TCP_TIMER_DISARM(tp, TCPT_REXMT);
476 		tp->t_rtttime = 0;
477 
478 	 	/*
479 		 * send one or 2 segments based on how much new data was acked
480 		 */
481  		if (((th->th_ack - tp->snd_una) / tp->t_segsz) > 2)
482  			num_segs = 2;
483 	 	(void)tcp_sack_output(tp, &sack_bytes_rxmt);
484  		tp->snd_cwnd = sack_bytes_rxmt +
485 		    (tp->snd_nxt - tp->sack_newdata) + num_segs * tp->t_segsz;
486   		tp->t_flags |= TF_ACKNOW;
487 	  	(void) tcp_output(tp);
488 	} else {
489 		/*
490 		 * Complete ack, inflate the congestion window to
491                  * ssthresh and exit fast recovery.
492 		 *
493 		 * Window inflation should have left us with approx.
494 		 * snd_ssthresh outstanding data.  But in case we
495 		 * would be inclined to send a burst, better to do
496 		 * it via the slow start mechanism.
497 		 */
498 		if (SEQ_SUB(tp->snd_max, th->th_ack) < tp->snd_ssthresh)
499 			tp->snd_cwnd = SEQ_SUB(tp->snd_max, th->th_ack)
500 			    + tp->t_segsz;
501 		else
502 			tp->snd_cwnd = tp->snd_ssthresh;
503 		tp->t_partialacks = -1;
504 		tp->t_dupacks = 0;
505 		if (SEQ_GT(th->th_ack, tp->snd_fack))
506 			tp->snd_fack = th->th_ack;
507 	}
508 }
509 
510 /*
511  * Returns pointer to a sackhole if there are any pending retransmissions;
512  * NULL otherwise.
513  */
514 struct sackhole *
515 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
516 {
517 	struct sackhole *cur = NULL;
518 
519 	if (!TCP_SACK_ENABLED(tp))
520 		return (NULL);
521 
522 	*sack_bytes_rexmt = 0;
523 	TAILQ_FOREACH(cur, &tp->snd_holes, sackhole_q) {
524 		if (SEQ_LT(cur->rxmit, cur->end)) {
525 			if (SEQ_LT(cur->rxmit, tp->snd_una)) {
526 				/* old SACK hole */
527 				continue;
528 			}
529 			*sack_bytes_rexmt += (cur->rxmit - cur->start);
530 			break;
531 		}
532 		*sack_bytes_rexmt += (cur->rxmit - cur->start);
533 	}
534 
535 	return (cur);
536 }
537 
538 /*
539  * After a timeout, the SACK list may be rebuilt.  This SACK information
540  * should be used to avoid retransmitting SACKed data.  This function
541  * traverses the SACK list to see if snd_nxt should be moved forward.
542  */
543 void
544 tcp_sack_adjust(struct tcpcb *tp)
545 {
546 	struct sackhole *cur = TAILQ_FIRST(&tp->snd_holes);
547 	struct sackhole *n = NULL;
548 
549 	if (TAILQ_EMPTY(&tp->snd_holes))
550 		return; /* No holes */
551 	if (SEQ_GEQ(tp->snd_nxt, tp->rcv_lastsack))
552 		return; /* We're already beyond any SACKed blocks */
553 
554 	/*
555 	 * Two cases for which we want to advance snd_nxt:
556 	 * i) snd_nxt lies between end of one hole and beginning of another
557 	 * ii) snd_nxt lies between end of last hole and rcv_lastsack
558 	 */
559 	while ((n = TAILQ_NEXT(cur, sackhole_q)) != NULL) {
560 		if (SEQ_LT(tp->snd_nxt, cur->end))
561 			return;
562 		if (SEQ_GEQ(tp->snd_nxt, n->start))
563 			cur = n;
564 		else {
565 			tp->snd_nxt = n->start;
566 			return;
567 		}
568 	}
569 	if (SEQ_LT(tp->snd_nxt, cur->end))
570 		return;
571 	tp->snd_nxt = tp->rcv_lastsack;
572 
573 	return;
574 }
575 
576 /*
577  * tcp_sack_numblks: return the number of SACK blocks to send.
578  */
579 
580 int
581 tcp_sack_numblks(const struct tcpcb *tp)
582 {
583 	int numblks;
584 
585 	if (!TCP_SACK_ENABLED(tp)) {
586 		return 0;
587 	}
588 
589 	numblks = (((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) ? 1 : 0) +
590 	    tp->t_segqlen;
591 
592 	if (numblks == 0) {
593 		return 0;
594 	}
595 
596 	if (numblks > TCP_SACK_MAX) {
597 		numblks = TCP_SACK_MAX;
598 	}
599 
600 	return numblks;
601 }
602 
603 #if defined(DDB)
604 void sack_dump(const struct tcpcb *);
605 
606 void
607 sack_dump(const struct tcpcb *tp)
608 {
609 	const struct sackhole *cur;
610 
611 	printf("snd_una=%" PRIu32 ", snd_max=%" PRIu32 "\n",
612 	    tp->snd_una, tp->snd_max);
613 	printf("rcv_lastsack=%" PRIu32 ", snd_fack=%" PRIu32 "\n",
614 	    tp->rcv_lastsack, tp->snd_fack);
615 	printf("numholes=%d\n", tp->snd_numholes);
616 	TAILQ_FOREACH(cur, &tp->snd_holes, sackhole_q) {
617 		printf("\t%" PRIu32 "-%" PRIu32 ", rxmit=%" PRIu32 "\n",
618 		    cur->start, cur->end, cur->rxmit);
619 	}
620 }
621 #endif /* defined(DDB) */
622