utilities - Temporary map out libevtr, ktrdump, and evtranalyze from x86_64
[dragonfly.git] / sys / netinet / tcp_sack.c
blob71865d8d342ba705ba528eca244b33e5f051c33e
1 /*
2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
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.
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.
33 * $DragonFly: src/sys/netinet/tcp_sack.c,v 1.8 2008/08/15 21:37:16 nth Exp $
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>
46 #include <net/if.h>
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>
58 struct sackblock {
59 tcp_seq sblk_start;
60 tcp_seq sblk_end;
61 TAILQ_ENTRY(sackblock) sblk_list;
64 #define MAXSAVEDBLOCKS 8 /* per connection limit */
66 static void insert_block(struct scoreboard *scb, struct sackblock *newblock);
67 static void update_lostseq(struct scoreboard *scb, tcp_seq snd_una,
68 u_int maxseg);
70 static MALLOC_DEFINE(M_SACKBLOCK, "sblk", "sackblock struct");
73 * Per-tcpcb initialization.
75 void
76 tcp_sack_tcpcb_init(struct tcpcb *tp)
78 struct scoreboard *scb = &tp->scb;
80 scb->nblocks = 0;
81 TAILQ_INIT(&scb->sackblocks);
82 scb->lastfound = NULL;
86 * Find the SACK block containing or immediately preceding "seq".
87 * The boolean result indicates whether the sequence is actually
88 * contained in the SACK block.
90 static boolean_t
91 sack_block_lookup(struct scoreboard *scb, tcp_seq seq, struct sackblock **sb)
93 struct sackblock *hint = scb->lastfound;
94 struct sackblock *cur, *last, *prev;
96 if (TAILQ_EMPTY(&scb->sackblocks)) {
97 *sb = NULL;
98 return FALSE;
101 if (hint == NULL) {
102 /* No hint. Search from start to end. */
103 cur = TAILQ_FIRST(&scb->sackblocks);
104 last = NULL;
105 prev = TAILQ_LAST(&scb->sackblocks, sackblock_list);
106 } else {
107 if (SEQ_GEQ(seq, hint->sblk_start)) {
108 /* Search from hint to end of list. */
109 cur = hint;
110 last = NULL;
111 prev = TAILQ_LAST(&scb->sackblocks, sackblock_list);
112 } else {
113 /* Search from front of list to hint. */
114 cur = TAILQ_FIRST(&scb->sackblocks);
115 last = hint;
116 prev = TAILQ_PREV(hint, sackblock_list, sblk_list);
120 do {
121 if (SEQ_GT(cur->sblk_end, seq)) {
122 if (SEQ_GEQ(seq, cur->sblk_start)) {
123 *sb = scb->lastfound = cur;
124 return TRUE;
125 } else {
126 *sb = scb->lastfound =
127 TAILQ_PREV(cur, sackblock_list, sblk_list);
128 return FALSE;
131 cur = TAILQ_NEXT(cur, sblk_list);
132 } while (cur != last);
134 *sb = scb->lastfound = prev;
135 return FALSE;
139 * Allocate a SACK block.
141 static __inline struct sackblock *
142 alloc_sackblock(void)
144 return (kmalloc(sizeof(struct sackblock), M_SACKBLOCK, M_NOWAIT));
148 * Free a SACK block.
150 static __inline void
151 free_sackblock(struct sackblock *s)
153 kfree(s, M_SACKBLOCK);
157 * Free up SACK blocks for data that's been acked.
159 static void
160 tcp_sack_ack_blocks(struct scoreboard *scb, tcp_seq th_ack)
162 struct sackblock *sb, *nb;
164 sb = TAILQ_FIRST(&scb->sackblocks);
165 while (sb && SEQ_LEQ(sb->sblk_end, th_ack)) {
166 nb = TAILQ_NEXT(sb, sblk_list);
167 if (scb->lastfound == sb)
168 scb->lastfound = NULL;
169 TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list);
170 free_sackblock(sb);
171 --scb->nblocks;
172 KASSERT(scb->nblocks >= 0,
173 ("SACK block count underflow: %d < 0", scb->nblocks));
174 sb = nb;
176 if (sb && SEQ_GT(th_ack, sb->sblk_start))
177 sb->sblk_start = th_ack; /* other side reneged? XXX */
181 * Delete and free SACK blocks saved in scoreboard.
183 void
184 tcp_sack_cleanup(struct scoreboard *scb)
186 struct sackblock *sb, *nb;
188 TAILQ_FOREACH_MUTABLE(sb, &scb->sackblocks, sblk_list, nb) {
189 free_sackblock(sb);
190 --scb->nblocks;
192 KASSERT(scb->nblocks == 0,
193 ("SACK block %d count not zero", scb->nblocks));
194 TAILQ_INIT(&scb->sackblocks);
195 scb->lastfound = NULL;
199 * Returns 0 if not D-SACK block,
200 * 1 if D-SACK,
201 * 2 if duplicate of out-of-order D-SACK block.
204 tcp_sack_ndsack_blocks(struct raw_sackblock *blocks, const int numblocks,
205 tcp_seq snd_una)
207 if (numblocks == 0)
208 return 0;
210 if (SEQ_LT(blocks[0].rblk_start, snd_una))
211 return 1;
213 /* block 0 inside block 1 */
214 if (numblocks > 1 &&
215 SEQ_GEQ(blocks[0].rblk_start, blocks[1].rblk_start) &&
216 SEQ_LEQ(blocks[0].rblk_end, blocks[1].rblk_end))
217 return 2;
219 return 0;
223 * Update scoreboard on new incoming ACK.
225 static void
226 tcp_sack_add_blocks(struct tcpcb *tp, struct tcpopt *to)
228 const int numblocks = to->to_nsackblocks;
229 struct raw_sackblock *blocks = to->to_sackblocks;
230 struct scoreboard *scb = &tp->scb;
231 struct sackblock *sb;
232 int startblock;
233 int i;
235 if (tcp_sack_ndsack_blocks(blocks, numblocks, tp->snd_una) > 0)
236 startblock = 1;
237 else
238 startblock = 0;
240 for (i = startblock; i < numblocks; i++) {
241 struct raw_sackblock *newsackblock = &blocks[i];
243 /* don't accept bad SACK blocks */
244 if (SEQ_GT(newsackblock->rblk_end, tp->snd_max))
245 break; /* skip all other blocks */
247 sb = alloc_sackblock();
248 if (sb == NULL) /* do some sort of cleanup? XXX */
249 break; /* just skip rest of blocks */
250 sb->sblk_start = newsackblock->rblk_start;
251 sb->sblk_end = newsackblock->rblk_end;
252 if (TAILQ_EMPTY(&scb->sackblocks)) {
253 KASSERT(scb->nblocks == 0, ("emply scb w/ blocks"));
254 scb->nblocks = 1;
255 TAILQ_INSERT_HEAD(&scb->sackblocks, sb, sblk_list);
256 } else {
257 insert_block(scb, sb);
262 void
263 tcp_sack_update_scoreboard(struct tcpcb *tp, struct tcpopt *to)
265 struct scoreboard *scb = &tp->scb;
267 tcp_sack_ack_blocks(scb, tp->snd_una);
268 tcp_sack_add_blocks(tp, to);
269 update_lostseq(scb, tp->snd_una, tp->t_maxseg);
270 if (SEQ_LT(tp->rexmt_high, tp->snd_una))
271 tp->rexmt_high = tp->snd_una;
275 * Insert SACK block into sender's scoreboard.
277 static void
278 insert_block(struct scoreboard *scb, struct sackblock *newblock)
280 struct sackblock *sb, *workingblock;
281 boolean_t overlap_front;
283 KASSERT(scb->nblocks > 0, ("insert_block() called w/ no blocks"));
285 if (scb->nblocks == MAXSAVEDBLOCKS) {
287 * Should try to kick out older blocks XXX JH
288 * May be able to coalesce with existing block.
289 * Or, go other way and free all blocks if we hit this limit.
291 free_sackblock(newblock);
292 return;
294 KASSERT(scb->nblocks < MAXSAVEDBLOCKS,
295 ("too many SACK blocks %d", scb->nblocks));
297 overlap_front = sack_block_lookup(scb, newblock->sblk_start, &sb);
299 if (sb == NULL) {
300 workingblock = newblock;
301 TAILQ_INSERT_HEAD(&scb->sackblocks, newblock, sblk_list);
302 ++scb->nblocks;
303 } else {
304 if (overlap_front || sb->sblk_end == newblock->sblk_start) {
305 /* extend old block and discard new one */
306 workingblock = sb;
307 if (SEQ_GT(newblock->sblk_end, sb->sblk_end))
308 sb->sblk_end = newblock->sblk_end;
309 free_sackblock(newblock);
310 } else {
311 workingblock = newblock;
312 TAILQ_INSERT_AFTER(&scb->sackblocks, sb, newblock,
313 sblk_list);
314 ++scb->nblocks;
318 /* Consolidate right-hand side. */
319 sb = TAILQ_NEXT(workingblock, sblk_list);
320 while (sb != NULL &&
321 SEQ_GEQ(workingblock->sblk_end, sb->sblk_end)) {
322 struct sackblock *nextblock;
324 nextblock = TAILQ_NEXT(sb, sblk_list);
325 if (scb->lastfound == sb)
326 scb->lastfound = NULL;
327 /* Remove completely overlapped block */
328 TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list);
329 free_sackblock(sb);
330 --scb->nblocks;
331 KASSERT(scb->nblocks > 0,
332 ("removed overlapped block: %d blocks left", scb->nblocks));
333 sb = nextblock;
335 if (sb != NULL &&
336 SEQ_GEQ(workingblock->sblk_end, sb->sblk_start)) {
337 /* Extend new block to cover partially overlapped old block. */
338 workingblock->sblk_end = sb->sblk_end;
339 if (scb->lastfound == sb)
340 scb->lastfound = NULL;
341 TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list);
342 free_sackblock(sb);
343 --scb->nblocks;
344 KASSERT(scb->nblocks > 0,
345 ("removed partial right: %d blocks left", scb->nblocks));
349 #ifdef DEBUG_SACK_BLOCKS
350 static void
351 tcp_sack_dump_blocks(struct scoreboard *scb)
353 struct sackblock *sb;
355 kprintf("%d blocks:", scb->nblocks);
356 TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list)
357 kprintf(" [%u, %u)", sb->sblk_start, sb->sblk_end);
358 kprintf("\n");
360 #else
361 static __inline void
362 tcp_sack_dump_blocks(struct scoreboard *scb)
365 #endif
368 * Optimization to quickly determine which packets are lost.
370 static void
371 update_lostseq(struct scoreboard *scb, tcp_seq snd_una, u_int maxseg)
373 struct sackblock *sb;
374 int nsackblocks = 0;
375 int bytes_sacked = 0;
377 sb = TAILQ_LAST(&scb->sackblocks, sackblock_list);
378 while (sb != NULL) {
379 ++nsackblocks;
380 bytes_sacked += sb->sblk_end - sb->sblk_start;
381 if (nsackblocks == tcprexmtthresh ||
382 bytes_sacked >= tcprexmtthresh * maxseg) {
383 scb->lostseq = sb->sblk_start;
384 return;
386 sb = TAILQ_PREV(sb, sackblock_list, sblk_list);
388 scb->lostseq = snd_una;
392 * Return whether the given sequence number is considered lost.
394 static boolean_t
395 scb_islost(struct scoreboard *scb, tcp_seq seqnum)
397 return SEQ_LT(seqnum, scb->lostseq);
401 * True if at least "amount" has been SACKed. Used by Early Retransmit.
403 boolean_t
404 tcp_sack_has_sacked(struct scoreboard *scb, u_int amount)
406 struct sackblock *sb;
407 int bytes_sacked = 0;
409 TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) {
410 bytes_sacked += sb->sblk_end - sb->sblk_start;
411 if (bytes_sacked >= amount)
412 return TRUE;
414 return FALSE;
418 * Number of bytes SACKed below seq.
421 tcp_sack_bytes_below(struct scoreboard *scb, tcp_seq seq)
423 struct sackblock *sb;
424 int bytes_sacked = 0;
426 sb = TAILQ_FIRST(&scb->sackblocks);
427 while (sb && SEQ_GT(seq, sb->sblk_start)) {
428 bytes_sacked += seq_min(seq, sb->sblk_end) - sb->sblk_start;
429 sb = TAILQ_NEXT(sb, sblk_list);
431 return bytes_sacked;
435 * Return estimate of the number of bytes outstanding in the network.
437 uint32_t
438 tcp_sack_compute_pipe(struct tcpcb *tp)
440 struct scoreboard *scb = &tp->scb;
441 struct sackblock *sb;
442 int nlost, nretransmitted;
443 tcp_seq end;
445 nlost = tp->snd_max - scb->lostseq;
446 nretransmitted = tp->rexmt_high - tp->snd_una;
448 TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) {
449 if (SEQ_LT(sb->sblk_start, tp->rexmt_high)) {
450 end = seq_min(sb->sblk_end, tp->rexmt_high);
451 nretransmitted -= end - sb->sblk_start;
453 if (SEQ_GEQ(sb->sblk_start, scb->lostseq))
454 nlost -= sb->sblk_end - sb->sblk_start;
457 return (nlost + nretransmitted);
461 * Return the sequence number and length of the next segment to transmit
462 * when in Fast Recovery.
464 boolean_t
465 tcp_sack_nextseg(struct tcpcb *tp, tcp_seq *nextrexmt, uint32_t *plen,
466 boolean_t *lostdup)
468 struct scoreboard *scb = &tp->scb;
469 struct socket *so = tp->t_inpcb->inp_socket;
470 struct sackblock *sb;
471 const struct sackblock *lastblock =
472 TAILQ_LAST(&scb->sackblocks, sackblock_list);
473 tcp_seq torexmt;
474 long len, off;
476 /* skip SACKed data */
477 tcp_sack_skip_sacked(scb, &tp->rexmt_high);
479 /* Look for lost data. */
480 torexmt = tp->rexmt_high;
481 *lostdup = FALSE;
482 if (lastblock != NULL) {
483 if (SEQ_LT(torexmt, lastblock->sblk_end) &&
484 scb_islost(scb, torexmt)) {
485 sendunsacked:
486 *nextrexmt = torexmt;
487 /* If the left-hand edge has been SACKed, pull it in. */
488 if (sack_block_lookup(scb, torexmt + tp->t_maxseg, &sb))
489 *plen = sb->sblk_start - torexmt;
490 else
491 *plen = tp->t_maxseg;
492 return TRUE;
496 /* See if unsent data available within send window. */
497 off = tp->snd_max - tp->snd_una;
498 len = (long) ulmin(so->so_snd.ssb_cc, tp->snd_wnd) - off;
499 if (len > 0) {
500 *nextrexmt = tp->snd_max; /* Send new data. */
501 *plen = tp->t_maxseg;
502 return TRUE;
505 /* We're less certain this data has been lost. */
506 if (lastblock == NULL || SEQ_LT(torexmt, lastblock->sblk_end))
507 goto sendunsacked;
509 return FALSE;
513 * Return the next sequence number higher than "*prexmt" that has
514 * not been SACKed.
516 void
517 tcp_sack_skip_sacked(struct scoreboard *scb, tcp_seq *prexmt)
519 struct sackblock *sb;
521 /* skip SACKed data */
522 if (sack_block_lookup(scb, *prexmt, &sb))
523 *prexmt = sb->sblk_end;
526 #ifdef later
527 void
528 tcp_sack_save_scoreboard(struct scoreboard *scb)
530 struct scoreboard *scb = &tp->scb;
532 scb->sackblocks_prev = scb->sackblocks;
533 TAILQ_INIT(&scb->sackblocks);
536 void
537 tcp_sack_revert_scoreboard(struct scoreboard *scb, tcp_seq snd_una,
538 u_int maxseg)
540 struct sackblock *sb;
542 scb->sackblocks = scb->sackblocks_prev;
543 scb->nblocks = 0;
544 TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list)
545 ++scb->nblocks;
546 tcp_sack_ack_blocks(scb, snd_una);
547 scb->lastfound = NULL;
549 #endif
551 #ifdef DEBUG_SACK_HISTORY
552 static void
553 tcp_sack_dump_history(char *msg, struct tcpcb *tp)
555 int i;
556 static int ndumped;
558 /* only need a couple of these to debug most problems */
559 if (++ndumped > 900)
560 return;
562 kprintf("%s:\tnsackhistory %d: ", msg, tp->nsackhistory);
563 for (i = 0; i < tp->nsackhistory; ++i)
564 kprintf("[%u, %u) ", tp->sackhistory[i].rblk_start,
565 tp->sackhistory[i].rblk_end);
566 kprintf("\n");
568 #else
569 static __inline void
570 tcp_sack_dump_history(char *msg, struct tcpcb *tp)
573 #endif
576 * Remove old SACK blocks from the SACK history that have already been ACKed.
578 static void
579 tcp_sack_ack_history(struct tcpcb *tp)
581 int i, nblocks, openslot;
583 tcp_sack_dump_history("before tcp_sack_ack_history", tp);
584 nblocks = tp->nsackhistory;
585 for (i = openslot = 0; i < nblocks; ++i) {
586 if (SEQ_LEQ(tp->sackhistory[i].rblk_end, tp->rcv_nxt)) {
587 --tp->nsackhistory;
588 continue;
590 if (SEQ_LT(tp->sackhistory[i].rblk_start, tp->rcv_nxt))
591 tp->sackhistory[i].rblk_start = tp->rcv_nxt;
592 if (i == openslot)
593 ++openslot;
594 else
595 tp->sackhistory[openslot++] = tp->sackhistory[i];
597 tcp_sack_dump_history("after tcp_sack_ack_history", tp);
598 KASSERT(openslot == tp->nsackhistory,
599 ("tcp_sack_ack_history miscounted: %d != %d",
600 openslot, tp->nsackhistory));
604 * Add or merge newblock into reported history.
605 * Also remove or update SACK blocks that will be acked.
607 static void
608 tcp_sack_update_reported_history(struct tcpcb *tp, tcp_seq start, tcp_seq end)
610 struct raw_sackblock copy[MAX_SACK_REPORT_BLOCKS];
611 int i, cindex;
613 tcp_sack_dump_history("before tcp_sack_update_reported_history", tp);
615 * Six cases:
616 * 0) no overlap
617 * 1) newblock == oldblock
618 * 2) oldblock contains newblock
619 * 3) newblock contains oldblock
620 * 4) tail of oldblock overlaps or abuts start of newblock
621 * 5) tail of newblock overlaps or abuts head of oldblock
623 for (i = cindex = 0; i < tp->nsackhistory; ++i) {
624 struct raw_sackblock *oldblock = &tp->sackhistory[i];
625 tcp_seq old_start = oldblock->rblk_start;
626 tcp_seq old_end = oldblock->rblk_end;
628 if (SEQ_LT(end, old_start) || SEQ_GT(start, old_end)) {
629 /* Case 0: no overlap. Copy old block. */
630 copy[cindex++] = *oldblock;
631 continue;
634 if (SEQ_GEQ(start, old_start) && SEQ_LEQ(end, old_end)) {
635 /* Cases 1 & 2. Move block to front of history. */
636 int j;
638 start = old_start;
639 end = old_end;
640 /* no need to check rest of blocks */
641 for (j = i + 1; j < tp->nsackhistory; ++j)
642 copy[cindex++] = tp->sackhistory[j];
643 break;
646 if (SEQ_GEQ(old_end, start) && SEQ_LT(old_start, start)) {
647 /* Case 4: extend start of new block. */
648 start = old_start;
649 } else if (SEQ_GEQ(end, old_start) && SEQ_GT(old_end, end)) {
650 /* Case 5: extend end of new block */
651 end = old_end;
652 } else {
653 /* Case 3. Delete old block by not copying it. */
654 KASSERT(SEQ_LEQ(start, old_start) &&
655 SEQ_GEQ(end, old_end),
656 ("bad logic: old [%u, %u), new [%u, %u)",
657 old_start, old_end, start, end));
661 /* insert new block */
662 tp->sackhistory[0].rblk_start = start;
663 tp->sackhistory[0].rblk_end = end;
664 cindex = min(cindex, MAX_SACK_REPORT_BLOCKS - 1);
665 for (i = 0; i < cindex; ++i)
666 tp->sackhistory[i + 1] = copy[i];
667 tp->nsackhistory = cindex + 1;
668 tcp_sack_dump_history("after tcp_sack_update_reported_history", tp);
672 * Fill in SACK report to return to data sender.
674 void
675 tcp_sack_fill_report(struct tcpcb *tp, u_char *opt, u_int *plen)
677 u_int optlen = *plen;
678 uint32_t *lp = (uint32_t *)(opt + optlen);
679 uint32_t *olp;
680 tcp_seq hstart = tp->rcv_nxt, hend;
681 int nblocks;
683 KASSERT(TCP_MAXOLEN - optlen >=
684 TCPOLEN_SACK_ALIGNED + TCPOLEN_SACK_BLOCK,
685 ("no room for SACK header and one block: optlen %d", optlen));
687 olp = lp++;
688 optlen += TCPOLEN_SACK_ALIGNED;
690 tcp_sack_ack_history(tp);
691 if (tp->reportblk.rblk_start != tp->reportblk.rblk_end) {
692 *lp++ = htonl(tp->reportblk.rblk_start);
693 *lp++ = htonl(tp->reportblk.rblk_end);
694 optlen += TCPOLEN_SACK_BLOCK;
695 hstart = tp->reportblk.rblk_start;
696 hend = tp->reportblk.rblk_end;
697 if (tp->t_flags & TF_ENCLOSESEG) {
698 KASSERT(TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK,
699 ("no room for enclosing SACK block: oplen %d",
700 optlen));
701 *lp++ = htonl(tp->encloseblk.rblk_start);
702 *lp++ = htonl(tp->encloseblk.rblk_end);
703 optlen += TCPOLEN_SACK_BLOCK;
704 hstart = tp->encloseblk.rblk_start;
705 hend = tp->encloseblk.rblk_end;
707 if (SEQ_GT(hstart, tp->rcv_nxt))
708 tcp_sack_update_reported_history(tp, hstart, hend);
710 if (tcp_do_smartsack && (tp->t_flags & TF_SACKLEFT)) {
711 /* Fill in from left! Walk re-assembly queue. */
712 struct tseg_qent *q;
714 q = LIST_FIRST(&tp->t_segq);
715 while (q != NULL &&
716 TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK) {
717 *lp++ = htonl(q->tqe_th->th_seq);
718 *lp++ = htonl(q->tqe_th->th_seq + q->tqe_len);
719 optlen += TCPOLEN_SACK_BLOCK;
720 q = LIST_NEXT(q, tqe_q);
722 } else {
723 int n = 0;
725 /* Fill in SACK blocks from right side. */
726 while (n < tp->nsackhistory &&
727 TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK) {
728 if (tp->sackhistory[n].rblk_start != hstart) {
729 *lp++ = htonl(tp->sackhistory[n].rblk_start);
730 *lp++ = htonl(tp->sackhistory[n].rblk_end);
731 optlen += TCPOLEN_SACK_BLOCK;
733 ++n;
736 tp->reportblk.rblk_start = tp->reportblk.rblk_end;
737 tp->t_flags &= ~(TF_DUPSEG | TF_ENCLOSESEG | TF_SACKLEFT);
738 nblocks = (lp - olp - 1) / 2;
739 *olp = htonl(TCPOPT_SACK_ALIGNED |
740 (TCPOLEN_SACK + nblocks * TCPOLEN_SACK_BLOCK));
741 *plen = optlen;