xref: /dflybsd-src/sys/netinet/tcp_sack.c (revision 848a176c3e7fdc1819adc6b852193ed7161610b9)
1 /*
2  * Copyright (c) 2003, 2004 Jeffrey M. Hsu.  All rights reserved.
3  * Copyright (c) 2003, 2004 The DragonFly Project.  All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Jeffrey M. Hsu.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of The DragonFly Project nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific, prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $DragonFly: src/sys/netinet/tcp_sack.c,v 1.8 2008/08/15 21:37:16 nth Exp $
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/queue.h>
41 #include <sys/thread.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 
46 #include <net/if.h>
47 
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/ip.h>
51 #include <netinet/in_var.h>
52 #include <netinet/in_pcb.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/tcp.h>
55 #include <netinet/tcp_seq.h>
56 #include <netinet/tcp_var.h>
57 
58 /*
59  * Implemented:
60  *
61  * RFC 2018
62  * RFC 2883
63  * RFC 3517
64  */
65 
66 struct sackblock {
67 	tcp_seq			sblk_start;
68 	tcp_seq			sblk_end;
69 	TAILQ_ENTRY(sackblock)	sblk_list;
70 };
71 
72 #define	MAXSAVEDBLOCKS	8			/* per connection limit */
73 
74 static int insert_block(struct scoreboard *scb,
75 			const struct raw_sackblock *raw_sb);
76 static void update_lostseq(struct scoreboard *scb, tcp_seq snd_una,
77 			   u_int maxseg);
78 
79 static MALLOC_DEFINE(M_SACKBLOCK, "sblk", "sackblock struct");
80 
81 /*
82  * Per-tcpcb initialization.
83  */
84 void
85 tcp_sack_tcpcb_init(struct tcpcb *tp)
86 {
87 	struct scoreboard *scb = &tp->scb;
88 
89 	scb->nblocks = 0;
90 	TAILQ_INIT(&scb->sackblocks);
91 	scb->lastfound = NULL;
92 }
93 
94 /*
95  * Find the SACK block containing or immediately preceding "seq".
96  * The boolean result indicates whether the sequence is actually
97  * contained in the SACK block.
98  */
99 static boolean_t
100 sack_block_lookup(struct scoreboard *scb, tcp_seq seq, struct sackblock **sb)
101 {
102 	struct sackblock *hint = scb->lastfound;
103 	struct sackblock *cur, *last, *prev;
104 
105 	if (TAILQ_EMPTY(&scb->sackblocks)) {
106 		*sb = NULL;
107 		return FALSE;
108 	}
109 
110 	if (hint == NULL) {
111 		/* No hint.  Search from start to end. */
112 		cur = TAILQ_FIRST(&scb->sackblocks);
113 		last = NULL;
114 		prev = TAILQ_LAST(&scb->sackblocks, sackblock_list);
115 	} else  {
116 		if (SEQ_GEQ(seq, hint->sblk_start)) {
117 			/* Search from hint to end of list. */
118 			cur = hint;
119 			last = NULL;
120 			prev = TAILQ_LAST(&scb->sackblocks, sackblock_list);
121 		} else {
122 			/* Search from front of list to hint. */
123 			cur = TAILQ_FIRST(&scb->sackblocks);
124 			last = hint;
125 			prev = TAILQ_PREV(hint, sackblock_list, sblk_list);
126 		}
127 	}
128 
129 	do {
130 		if (SEQ_GT(cur->sblk_end, seq)) {
131 			if (SEQ_GEQ(seq, cur->sblk_start)) {
132 				*sb = scb->lastfound = cur;
133 				return TRUE;
134 			} else {
135 				*sb = scb->lastfound =
136 				    TAILQ_PREV(cur, sackblock_list, sblk_list);
137 				return FALSE;
138 			}
139 		}
140 		cur = TAILQ_NEXT(cur, sblk_list);
141 	} while (cur != last);
142 
143 	*sb = scb->lastfound = prev;
144 	return FALSE;
145 }
146 
147 /*
148  * Allocate a SACK block.
149  */
150 static __inline struct sackblock *
151 alloc_sackblock(struct scoreboard *scb, const struct raw_sackblock *raw_sb)
152 {
153 	struct sackblock *sb;
154 
155 	if (scb->freecache != NULL) {
156 		sb = scb->freecache;
157 		scb->freecache = NULL;
158 		tcpstat.tcps_sacksbfast++;
159 	} else {
160 		sb = kmalloc(sizeof(struct sackblock), M_SACKBLOCK, M_NOWAIT);
161 		if (sb == NULL) {
162 			tcpstat.tcps_sacksbfailed++;
163 			return NULL;
164 		}
165 	}
166 	sb->sblk_start = raw_sb->rblk_start;
167 	sb->sblk_end = raw_sb->rblk_end;
168 	return sb;
169 }
170 
171 static __inline struct sackblock *
172 alloc_sackblock_limit(struct scoreboard *scb,
173     const struct raw_sackblock *raw_sb)
174 {
175 	if (scb->nblocks == MAXSAVEDBLOCKS) {
176 		/*
177 		 * Should try to kick out older blocks XXX JH
178 		 * May be able to coalesce with existing block.
179 		 * Or, go other way and free all blocks if we hit
180 		 * this limit.
181 		 */
182 		tcpstat.tcps_sacksboverflow++;
183 		return NULL;
184 	}
185 	return alloc_sackblock(scb, raw_sb);
186 }
187 
188 /*
189  * Free a SACK block.
190  */
191 static __inline void
192 free_sackblock(struct scoreboard *scb, struct sackblock *s)
193 {
194 	if (scb->freecache == NULL) {
195 		/* YYY Maybe use the latest freed block? */
196 		scb->freecache = s;
197 		return;
198 	}
199 	kfree(s, M_SACKBLOCK);
200 }
201 
202 /*
203  * Free up SACK blocks for data that's been acked.
204  */
205 static void
206 tcp_sack_ack_blocks(struct scoreboard *scb, tcp_seq th_ack)
207 {
208 	struct sackblock *sb, *nb;
209 
210 	sb = TAILQ_FIRST(&scb->sackblocks);
211 	while (sb && SEQ_LEQ(sb->sblk_end, th_ack)) {
212 		nb = TAILQ_NEXT(sb, sblk_list);
213 		if (scb->lastfound == sb)
214 			scb->lastfound = NULL;
215 		TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list);
216 		free_sackblock(scb, sb);
217 		--scb->nblocks;
218 		KASSERT(scb->nblocks >= 0,
219 		    ("SACK block count underflow: %d < 0", scb->nblocks));
220 		sb = nb;
221 	}
222 	if (sb && SEQ_GT(th_ack, sb->sblk_start))
223 		sb->sblk_start = th_ack;	/* other side reneged? XXX */
224 }
225 
226 /*
227  * Delete and free SACK blocks saved in scoreboard.
228  */
229 void
230 tcp_sack_cleanup(struct scoreboard *scb)
231 {
232 	struct sackblock *sb, *nb;
233 
234 	TAILQ_FOREACH_MUTABLE(sb, &scb->sackblocks, sblk_list, nb) {
235 		free_sackblock(scb, sb);
236 		--scb->nblocks;
237 	}
238 	KASSERT(scb->nblocks == 0,
239 	    ("SACK block %d count not zero", scb->nblocks));
240 	TAILQ_INIT(&scb->sackblocks);
241 	scb->lastfound = NULL;
242 }
243 
244 /*
245  * Delete and free SACK blocks saved in scoreboard.
246  * Delete the one slot block cache.
247  */
248 void
249 tcp_sack_destroy(struct scoreboard *scb)
250 {
251 	tcp_sack_cleanup(scb);
252 	if (scb->freecache != NULL) {
253 		kfree(scb->freecache, M_SACKBLOCK);
254 		scb->freecache = NULL;
255 	}
256 }
257 
258 /*
259  * Cleanup the reported SACK block information
260  */
261 void
262 tcp_sack_report_cleanup(struct tcpcb *tp)
263 {
264 	tp->t_flags &= ~(TF_DUPSEG | TF_ENCLOSESEG | TF_SACKLEFT);
265 	tp->reportblk.rblk_start = tp->reportblk.rblk_end;
266 }
267 
268 /*
269  * Returns	0 if not D-SACK block,
270  *		1 if D-SACK,
271  *		2 if duplicate of out-of-order D-SACK block.
272  */
273 int
274 tcp_sack_ndsack_blocks(struct raw_sackblock *blocks, const int numblocks,
275 		       tcp_seq snd_una)
276 {
277 	if (numblocks == 0)
278 		return 0;
279 
280 	if (SEQ_LT(blocks[0].rblk_start, snd_una))
281 		return 1;
282 
283 	/* block 0 inside block 1 */
284 	if (numblocks > 1 &&
285 	    SEQ_GEQ(blocks[0].rblk_start, blocks[1].rblk_start) &&
286 	    SEQ_LEQ(blocks[0].rblk_end, blocks[1].rblk_end))
287 		return 2;
288 
289 	return 0;
290 }
291 
292 /*
293  * Update scoreboard on new incoming ACK.
294  */
295 static void
296 tcp_sack_add_blocks(struct tcpcb *tp, struct tcpopt *to)
297 {
298 	const int numblocks = to->to_nsackblocks;
299 	struct raw_sackblock *blocks = to->to_sackblocks;
300 	struct scoreboard *scb = &tp->scb;
301 	int startblock;
302 	int i;
303 
304 	if (tcp_sack_ndsack_blocks(blocks, numblocks, tp->snd_una) > 0)
305 		startblock = 1;
306 	else
307 		startblock = 0;
308 
309 	for (i = startblock; i < numblocks; i++) {
310 		struct raw_sackblock *newsackblock = &blocks[i];
311 
312 		/* don't accept bad SACK blocks */
313 		if (SEQ_GT(newsackblock->rblk_end, tp->snd_max)) {
314 			tcpstat.tcps_rcvbadsackopt++;
315 			break;		/* skip all other blocks */
316 		}
317 		tcpstat.tcps_sacksbupdate++;
318 
319 		if (insert_block(scb, newsackblock))
320 			break;
321 	}
322 }
323 
324 void
325 tcp_sack_update_scoreboard(struct tcpcb *tp, struct tcpopt *to)
326 {
327 	struct scoreboard *scb = &tp->scb;
328 	int rexmt_high_update = 0;
329 
330 	tcp_sack_ack_blocks(scb, tp->snd_una);
331 	tcp_sack_add_blocks(tp, to);
332 	update_lostseq(scb, tp->snd_una, tp->t_maxseg);
333 	if (SEQ_LT(tp->rexmt_high, tp->snd_una)) {
334 		tp->rexmt_high = tp->snd_una;
335 		rexmt_high_update = 1;
336 	}
337 	if (tp->t_flags & TF_SACKRESCUED) {
338 		if (SEQ_LT(tp->rexmt_rescue, tp->snd_una)) {
339 			tp->t_flags &= ~TF_SACKRESCUED;
340 		} else if (rexmt_high_update &&
341 		    SEQ_LT(tp->rexmt_rescue, tp->rexmt_high)) {
342 			/* Drag RescueRxt along with HighRxt */
343 			tp->rexmt_rescue = tp->rexmt_high;
344 		}
345 	}
346 }
347 
348 /*
349  * Insert SACK block into sender's scoreboard.
350  */
351 static int
352 insert_block(struct scoreboard *scb, const struct raw_sackblock *raw_sb)
353 {
354 	struct sackblock *sb, *workingblock;
355 	boolean_t overlap_front;
356 
357 	if (TAILQ_EMPTY(&scb->sackblocks)) {
358 		struct sackblock *newblock;
359 
360 		KASSERT(scb->nblocks == 0, ("emply scb w/ blocks"));
361 
362 		newblock = alloc_sackblock(scb, raw_sb);
363 		if (newblock == NULL)
364 			return ENOMEM;
365 		TAILQ_INSERT_HEAD(&scb->sackblocks, newblock, sblk_list);
366 		scb->nblocks = 1;
367 		return 0;
368 	}
369 
370 	KASSERT(scb->nblocks > 0, ("insert_block() called w/ no blocks"));
371 	KASSERT(scb->nblocks <= MAXSAVEDBLOCKS,
372 	    ("too many SACK blocks %d", scb->nblocks));
373 
374 	overlap_front = sack_block_lookup(scb, raw_sb->rblk_start, &sb);
375 
376 	if (sb == NULL) {
377 		workingblock = alloc_sackblock_limit(scb, raw_sb);
378 		if (workingblock == NULL)
379 			return ENOMEM;
380 		TAILQ_INSERT_HEAD(&scb->sackblocks, workingblock, sblk_list);
381 		++scb->nblocks;
382 	} else {
383 		if (overlap_front || sb->sblk_end == raw_sb->rblk_start) {
384 			/* Extend old block */
385 			workingblock = sb;
386 			if (SEQ_GT(raw_sb->rblk_end, sb->sblk_end))
387 				sb->sblk_end = raw_sb->rblk_end;
388 			tcpstat.tcps_sacksbreused++;
389 		} else {
390 			workingblock = alloc_sackblock_limit(scb, raw_sb);
391 			if (workingblock == NULL)
392 				return ENOMEM;
393 			TAILQ_INSERT_AFTER(&scb->sackblocks, sb, workingblock,
394 			    sblk_list);
395 			++scb->nblocks;
396 		}
397 	}
398 
399 	/* Consolidate right-hand side. */
400 	sb = TAILQ_NEXT(workingblock, sblk_list);
401 	while (sb != NULL &&
402 	    SEQ_GEQ(workingblock->sblk_end, sb->sblk_end)) {
403 		struct sackblock *nextblock;
404 
405 		nextblock = TAILQ_NEXT(sb, sblk_list);
406 		if (scb->lastfound == sb)
407 			scb->lastfound = NULL;
408 		/* Remove completely overlapped block */
409 		TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list);
410 		free_sackblock(scb, sb);
411 		--scb->nblocks;
412 		KASSERT(scb->nblocks > 0,
413 		    ("removed overlapped block: %d blocks left", scb->nblocks));
414 		sb = nextblock;
415 	}
416 	if (sb != NULL &&
417 	    SEQ_GEQ(workingblock->sblk_end, sb->sblk_start)) {
418 		/* Extend new block to cover partially overlapped old block. */
419 		workingblock->sblk_end = sb->sblk_end;
420 		if (scb->lastfound == sb)
421 			scb->lastfound = NULL;
422 		TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list);
423 		free_sackblock(scb, sb);
424 		--scb->nblocks;
425 		KASSERT(scb->nblocks > 0,
426 		    ("removed partial right: %d blocks left", scb->nblocks));
427 	}
428 	return 0;
429 }
430 
431 #ifdef DEBUG_SACK_BLOCKS
432 static void
433 tcp_sack_dump_blocks(struct scoreboard *scb)
434 {
435 	struct sackblock *sb;
436 
437 	kprintf("%d blocks:", scb->nblocks);
438 	TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list)
439 		kprintf(" [%u, %u)", sb->sblk_start, sb->sblk_end);
440 	kprintf("\n");
441 }
442 #else
443 static __inline void
444 tcp_sack_dump_blocks(struct scoreboard *scb)
445 {
446 }
447 #endif
448 
449 /*
450  * Optimization to quickly determine which packets are lost.
451  */
452 static void
453 update_lostseq(struct scoreboard *scb, tcp_seq snd_una, u_int maxseg)
454 {
455 	struct sackblock *sb;
456 	int nsackblocks = 0;
457 	int bytes_sacked = 0;
458 
459 	sb = TAILQ_LAST(&scb->sackblocks, sackblock_list);
460 	while (sb != NULL) {
461 		++nsackblocks;
462 		bytes_sacked += sb->sblk_end - sb->sblk_start;
463 		if (nsackblocks == tcprexmtthresh ||
464 		    bytes_sacked >= tcprexmtthresh * maxseg) {
465 			scb->lostseq = sb->sblk_start;
466 			return;
467 		}
468 		sb = TAILQ_PREV(sb, sackblock_list, sblk_list);
469 	}
470 	scb->lostseq = snd_una;
471 }
472 
473 /*
474  * Return whether the given sequence number is considered lost.
475  */
476 static boolean_t
477 scb_islost(struct scoreboard *scb, tcp_seq seqnum)
478 {
479 	return SEQ_LT(seqnum, scb->lostseq);
480 }
481 
482 /*
483  * True if at least "amount" has been SACKed.  Used by Early Retransmit.
484  */
485 boolean_t
486 tcp_sack_has_sacked(struct scoreboard *scb, u_int amount)
487 {
488 	struct sackblock *sb;
489 	int bytes_sacked = 0;
490 
491 	TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) {
492 		bytes_sacked += sb->sblk_end - sb->sblk_start;
493 		if (bytes_sacked >= amount)
494 			return TRUE;
495 	}
496 	return FALSE;
497 }
498 
499 /*
500  * Number of bytes SACKed below seq.
501  */
502 int
503 tcp_sack_bytes_below(struct scoreboard *scb, tcp_seq seq)
504 {
505 	struct sackblock *sb;
506 	int bytes_sacked = 0;
507 
508 	sb = TAILQ_FIRST(&scb->sackblocks);
509 	while (sb && SEQ_GT(seq, sb->sblk_start)) {
510 		bytes_sacked += seq_min(seq, sb->sblk_end) - sb->sblk_start;
511 		sb = TAILQ_NEXT(sb, sblk_list);
512 	}
513 	return bytes_sacked;
514 }
515 
516 /*
517  * Return estimate of the number of bytes outstanding in the network.
518  */
519 uint32_t
520 tcp_sack_compute_pipe(struct tcpcb *tp)
521 {
522 	struct scoreboard *scb = &tp->scb;
523 	struct sackblock *sb;
524 	int nlost, nretransmitted;
525 	tcp_seq end;
526 
527 	nlost = tp->snd_max - scb->lostseq;
528 	nretransmitted = tp->rexmt_high - tp->snd_una;
529 
530 	TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) {
531 		if (SEQ_LT(sb->sblk_start, tp->rexmt_high)) {
532 			end = seq_min(sb->sblk_end, tp->rexmt_high);
533 			nretransmitted -= end - sb->sblk_start;
534 		}
535 		if (SEQ_GEQ(sb->sblk_start, scb->lostseq))
536 			nlost -= sb->sblk_end - sb->sblk_start;
537 	}
538 
539 	return (nlost + nretransmitted);
540 }
541 
542 /*
543  * Return the sequence number and length of the next segment to transmit
544  * when in Fast Recovery.
545  */
546 boolean_t
547 tcp_sack_nextseg(struct tcpcb *tp, tcp_seq *nextrexmt, uint32_t *plen,
548     boolean_t *rescue)
549 {
550 	struct scoreboard *scb = &tp->scb;
551 	struct socket *so = tp->t_inpcb->inp_socket;
552 	struct sackblock *sb;
553 	const struct sackblock *lastblock =
554 	    TAILQ_LAST(&scb->sackblocks, sackblock_list);
555 	tcp_seq torexmt;
556 	long len, off;
557 
558 	/* skip SACKed data */
559 	tcp_sack_skip_sacked(scb, &tp->rexmt_high);
560 
561 	/* Look for lost data. */
562 	torexmt = tp->rexmt_high;
563 	*rescue = FALSE;
564 	if (lastblock != NULL) {
565 		if (SEQ_LT(torexmt, lastblock->sblk_end) &&
566 		    scb_islost(scb, torexmt)) {
567 sendunsacked:
568 			*nextrexmt = torexmt;
569 			/* If the left-hand edge has been SACKed, pull it in. */
570 			if (sack_block_lookup(scb, torexmt + tp->t_maxseg, &sb))
571 				*plen = sb->sblk_start - torexmt;
572 			else
573 				*plen = tp->t_maxseg;
574 			return TRUE;
575 		}
576 	}
577 
578 	/* See if unsent data available within send window. */
579 	off = tp->snd_max - tp->snd_una;
580 	len = (long) ulmin(so->so_snd.ssb_cc, tp->snd_wnd) - off;
581 	if (len > 0) {
582 		*nextrexmt = tp->snd_max;	/* Send new data. */
583 		*plen = tp->t_maxseg;
584 		return TRUE;
585 	}
586 
587 	/* We're less certain this data has been lost. */
588 	if (lastblock != NULL && SEQ_LT(torexmt, lastblock->sblk_end))
589 		goto sendunsacked;
590 
591 	/* Rescue retransmission */
592 	if (tcp_do_rescuesack) {
593 		tcpstat.tcps_sackrescue_try++;
594 		if (tp->t_flags & TF_SACKRESCUED) {
595 			if (!tcp_aggressive_rescuesack)
596 				return FALSE;
597 
598 			/*
599 			 * Aggressive variant of the rescue retransmission.
600 			 *
601 			 * The idea of the rescue retransmission is to sustain
602 			 * the ACK clock thus to avoid timeout retransmission.
603 			 *
604 			 * Under some situations, the conservative approach
605 			 * suggested in the draft
606  			 * http://tools.ietf.org/html/
607 			 * draft-nishida-tcpm-rescue-retransmission-00
608 			 * could not sustain ACK clock, since it only allows
609 			 * one rescue retransmission before a cumulative ACK
610 			 * covers the segement transmitted by rescue
611 			 * retransmission.
612 			 *
613 			 * We try to locate the next unSACKed segment which
614 			 * follows the previously sent rescue segment.  If
615 			 * there is no such segment, we loop back to the first
616 			 * unacknowledged segment.
617 			 */
618 
619 			/*
620 			 * Skip SACKed data, but here we follow
621 			 * the last transmitted rescue segment.
622 			 */
623 			torexmt = tp->rexmt_rescue;
624 			tcp_sack_skip_sacked(scb, &torexmt);
625 			if (torexmt == tp->snd_max) {
626 				/* Nothing left to retransmit; restart */
627 				torexmt = tp->snd_una;
628 			}
629 		}
630 		*rescue = TRUE;
631 		goto sendunsacked;
632 	} else if (tcp_do_smartsack && lastblock == NULL) {
633 		tcpstat.tcps_sackrescue_try++;
634 		*rescue = TRUE;
635 		goto sendunsacked;
636 	}
637 
638 	return FALSE;
639 }
640 
641 /*
642  * Return the next sequence number higher than "*prexmt" that has
643  * not been SACKed.
644  */
645 void
646 tcp_sack_skip_sacked(struct scoreboard *scb, tcp_seq *prexmt)
647 {
648 	struct sackblock *sb;
649 
650 	/* skip SACKed data */
651 	if (sack_block_lookup(scb, *prexmt, &sb))
652 		*prexmt = sb->sblk_end;
653 }
654 
655 #ifdef later
656 void
657 tcp_sack_save_scoreboard(struct scoreboard *scb)
658 {
659 	struct scoreboard *scb = &tp->scb;
660 
661 	scb->sackblocks_prev = scb->sackblocks;
662 	TAILQ_INIT(&scb->sackblocks);
663 }
664 
665 void
666 tcp_sack_revert_scoreboard(struct scoreboard *scb, tcp_seq snd_una,
667 			   u_int maxseg)
668 {
669 	struct sackblock *sb;
670 
671 	scb->sackblocks = scb->sackblocks_prev;
672 	scb->nblocks = 0;
673 	TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list)
674 		++scb->nblocks;
675 	tcp_sack_ack_blocks(scb, snd_una);
676 	scb->lastfound = NULL;
677 }
678 #endif
679 
680 #ifdef DEBUG_SACK_HISTORY
681 static void
682 tcp_sack_dump_history(char *msg, struct tcpcb *tp)
683 {
684 	int i;
685 	static int ndumped;
686 
687 	/* only need a couple of these to debug most problems */
688 	if (++ndumped > 900)
689 		return;
690 
691 	kprintf("%s:\tnsackhistory %d: ", msg, tp->nsackhistory);
692 	for (i = 0; i < tp->nsackhistory; ++i)
693 		kprintf("[%u, %u) ", tp->sackhistory[i].rblk_start,
694 		    tp->sackhistory[i].rblk_end);
695 	kprintf("\n");
696 }
697 #else
698 static __inline void
699 tcp_sack_dump_history(char *msg, struct tcpcb *tp)
700 {
701 }
702 #endif
703 
704 /*
705  * Remove old SACK blocks from the SACK history that have already been ACKed.
706  */
707 static void
708 tcp_sack_ack_history(struct tcpcb *tp)
709 {
710 	int i, nblocks, openslot;
711 
712 	tcp_sack_dump_history("before tcp_sack_ack_history", tp);
713 	nblocks = tp->nsackhistory;
714 	for (i = openslot = 0; i < nblocks; ++i) {
715 		if (SEQ_LEQ(tp->sackhistory[i].rblk_end, tp->rcv_nxt)) {
716 			--tp->nsackhistory;
717 			continue;
718 		}
719 		if (SEQ_LT(tp->sackhistory[i].rblk_start, tp->rcv_nxt))
720 			tp->sackhistory[i].rblk_start = tp->rcv_nxt;
721 		if (i == openslot)
722 			++openslot;
723 		else
724 			tp->sackhistory[openslot++] = tp->sackhistory[i];
725 	}
726 	tcp_sack_dump_history("after tcp_sack_ack_history", tp);
727 	KASSERT(openslot == tp->nsackhistory,
728 	    ("tcp_sack_ack_history miscounted: %d != %d",
729 	    openslot, tp->nsackhistory));
730 }
731 
732 /*
733  * Add or merge newblock into reported history.
734  * Also remove or update SACK blocks that will be acked.
735  */
736 static void
737 tcp_sack_update_reported_history(struct tcpcb *tp, tcp_seq start, tcp_seq end)
738 {
739 	struct raw_sackblock copy[MAX_SACK_REPORT_BLOCKS];
740 	int i, cindex;
741 
742 	tcp_sack_dump_history("before tcp_sack_update_reported_history", tp);
743 	/*
744 	 * Six cases:
745 	 *	0) no overlap
746 	 *	1) newblock == oldblock
747 	 *	2) oldblock contains newblock
748 	 *	3) newblock contains oldblock
749 	 *	4) tail of oldblock overlaps or abuts start of newblock
750 	 *	5) tail of newblock overlaps or abuts head of oldblock
751 	 */
752 	for (i = cindex = 0; i < tp->nsackhistory; ++i) {
753 		struct raw_sackblock *oldblock = &tp->sackhistory[i];
754 		tcp_seq old_start = oldblock->rblk_start;
755 		tcp_seq old_end = oldblock->rblk_end;
756 
757 		if (SEQ_LT(end, old_start) || SEQ_GT(start, old_end)) {
758 			/* Case 0:  no overlap.  Copy old block. */
759 			copy[cindex++] = *oldblock;
760 			continue;
761 		}
762 
763 		if (SEQ_GEQ(start, old_start) && SEQ_LEQ(end, old_end)) {
764 			/* Cases 1 & 2.  Move block to front of history. */
765 			int j;
766 
767 			start = old_start;
768 			end = old_end;
769 			/* no need to check rest of blocks */
770 			for (j = i + 1; j < tp->nsackhistory; ++j)
771 				copy[cindex++] = tp->sackhistory[j];
772 			break;
773 		}
774 
775 		if (SEQ_GEQ(old_end, start) && SEQ_LT(old_start, start)) {
776 			/* Case 4:  extend start of new block. */
777 			start = old_start;
778 		} else if (SEQ_GEQ(end, old_start) && SEQ_GT(old_end, end)) {
779 			/* Case 5: extend end of new block */
780 			end = old_end;
781 		} else {
782 			/* Case 3.  Delete old block by not copying it. */
783 			KASSERT(SEQ_LEQ(start, old_start) &&
784 				SEQ_GEQ(end, old_end),
785 			    ("bad logic: old [%u, %u), new [%u, %u)",
786 			     old_start, old_end, start, end));
787 		}
788 	}
789 
790 	/* insert new block */
791 	tp->sackhistory[0].rblk_start = start;
792 	tp->sackhistory[0].rblk_end = end;
793 	cindex = min(cindex, MAX_SACK_REPORT_BLOCKS - 1);
794 	for (i = 0; i < cindex; ++i)
795 		tp->sackhistory[i + 1] = copy[i];
796 	tp->nsackhistory = cindex + 1;
797 	tcp_sack_dump_history("after tcp_sack_update_reported_history", tp);
798 }
799 
800 /*
801  * Fill in SACK report to return to data sender.
802  */
803 void
804 tcp_sack_fill_report(struct tcpcb *tp, u_char *opt, u_int *plen)
805 {
806 	u_int optlen = *plen;
807 	uint32_t *lp = (uint32_t *)(opt + optlen);
808 	uint32_t *olp;
809 	tcp_seq hstart = tp->rcv_nxt, hend;
810 	int nblocks;
811 
812 	KASSERT(TCP_MAXOLEN - optlen >=
813 	    TCPOLEN_SACK_ALIGNED + TCPOLEN_SACK_BLOCK,
814 	    ("no room for SACK header and one block: optlen %d", optlen));
815 
816 	if (tp->t_flags & TF_DUPSEG)
817 		tcpstat.tcps_snddsackopt++;
818 	else
819 		tcpstat.tcps_sndsackopt++;
820 
821 	olp = lp++;
822 	optlen += TCPOLEN_SACK_ALIGNED;
823 
824 	tcp_sack_ack_history(tp);
825 	if (tp->reportblk.rblk_start != tp->reportblk.rblk_end) {
826 		*lp++ = htonl(tp->reportblk.rblk_start);
827 		*lp++ = htonl(tp->reportblk.rblk_end);
828 		optlen += TCPOLEN_SACK_BLOCK;
829 		hstart = tp->reportblk.rblk_start;
830 		hend = tp->reportblk.rblk_end;
831 		if (tp->t_flags & TF_ENCLOSESEG) {
832 			KASSERT(TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK,
833 			    ("no room for enclosing SACK block: oplen %d",
834 			    optlen));
835 			*lp++ = htonl(tp->encloseblk.rblk_start);
836 			*lp++ = htonl(tp->encloseblk.rblk_end);
837 			optlen += TCPOLEN_SACK_BLOCK;
838 			hstart = tp->encloseblk.rblk_start;
839 			hend = tp->encloseblk.rblk_end;
840 		}
841 		if (SEQ_GT(hstart, tp->rcv_nxt))
842 			tcp_sack_update_reported_history(tp, hstart, hend);
843 	}
844 	if (tcp_do_smartsack && (tp->t_flags & TF_SACKLEFT)) {
845 		/* Fill in from left!  Walk re-assembly queue. */
846 		struct tseg_qent *q;
847 
848 		q = LIST_FIRST(&tp->t_segq);
849 		while (q != NULL &&
850 		    TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK) {
851 			*lp++ = htonl(q->tqe_th->th_seq);
852 			*lp++ = htonl(TCP_SACK_BLKEND(
853 			    q->tqe_th->th_seq + q->tqe_len,
854 			    q->tqe_th->th_flags));
855 			optlen += TCPOLEN_SACK_BLOCK;
856 			q = LIST_NEXT(q, tqe_q);
857 		}
858 	} else {
859 		int n = 0;
860 
861 		/* Fill in SACK blocks from right side. */
862 		while (n < tp->nsackhistory &&
863 		    TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK) {
864 			if (tp->sackhistory[n].rblk_start != hstart) {
865 				*lp++ = htonl(tp->sackhistory[n].rblk_start);
866 				*lp++ = htonl(tp->sackhistory[n].rblk_end);
867 				optlen += TCPOLEN_SACK_BLOCK;
868 			}
869 			++n;
870 		}
871 	}
872 	tp->reportblk.rblk_start = tp->reportblk.rblk_end;
873 	tp->t_flags &= ~(TF_DUPSEG | TF_ENCLOSESEG | TF_SACKLEFT);
874 	nblocks = (lp - olp - 1) / 2;
875 	*olp = htonl(TCPOPT_SACK_ALIGNED |
876 		     (TCPOLEN_SACK + nblocks * TCPOLEN_SACK_BLOCK));
877 	*plen = optlen;
878 }
879