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
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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>
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>
69 TAILQ_ENTRY(sackblock) sblk_list;
72 #define MAXSAVEDBLOCKS 8 /* per connection limit */
74 static int insert_block(struct scoreboard *scb,
75 const struct raw_sackblock *raw_sb, boolean_t *update);
76 static void update_lostseq(struct scoreboard *scb, tcp_seq snd_una,
77 u_int maxseg, int rxtthresh);
79 static MALLOC_DEFINE(M_SACKBLOCK, "sblk", "sackblock struct");
82 * Per-tcpcb initialization.
85 tcp_sack_tcpcb_init(struct tcpcb *tp)
87 struct scoreboard *scb = &tp->scb;
90 TAILQ_INIT(&scb->sackblocks);
91 scb->lastfound = NULL;
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.
100 sack_block_lookup(struct scoreboard *scb, tcp_seq seq, struct sackblock **sb)
102 struct sackblock *hint = scb->lastfound;
103 struct sackblock *cur, *last, *prev;
105 if (TAILQ_EMPTY(&scb->sackblocks)) {
111 /* No hint. Search from start to end. */
112 cur = TAILQ_FIRST(&scb->sackblocks);
114 prev = TAILQ_LAST(&scb->sackblocks, sackblock_list);
116 if (SEQ_GEQ(seq, hint->sblk_start)) {
117 /* Search from hint to end of list. */
120 prev = TAILQ_LAST(&scb->sackblocks, sackblock_list);
122 /* Search from front of list to hint. */
123 cur = TAILQ_FIRST(&scb->sackblocks);
125 prev = TAILQ_PREV(hint, sackblock_list, sblk_list);
130 if (SEQ_GT(cur->sblk_end, seq)) {
131 if (SEQ_GEQ(seq, cur->sblk_start)) {
132 *sb = scb->lastfound = cur;
135 *sb = scb->lastfound =
136 TAILQ_PREV(cur, sackblock_list, sblk_list);
140 cur = TAILQ_NEXT(cur, sblk_list);
141 } while (cur != last);
143 *sb = scb->lastfound = prev;
148 * Allocate a SACK block.
150 static __inline struct sackblock *
151 alloc_sackblock(struct scoreboard *scb, const struct raw_sackblock *raw_sb)
153 struct sackblock *sb;
155 if (scb->freecache != NULL) {
157 scb->freecache = NULL;
158 tcpstat.tcps_sacksbfast++;
160 sb = kmalloc(sizeof(struct sackblock), M_SACKBLOCK, M_NOWAIT);
162 tcpstat.tcps_sacksbfailed++;
166 sb->sblk_start = raw_sb->rblk_start;
167 sb->sblk_end = raw_sb->rblk_end;
171 static __inline struct sackblock *
172 alloc_sackblock_limit(struct scoreboard *scb,
173 const struct raw_sackblock *raw_sb)
175 if (scb->nblocks == MAXSAVEDBLOCKS) {
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
182 tcpstat.tcps_sacksboverflow++;
185 return alloc_sackblock(scb, raw_sb);
192 free_sackblock(struct scoreboard *scb, struct sackblock *s)
194 if (scb->freecache == NULL) {
195 /* YYY Maybe use the latest freed block? */
199 kfree(s, M_SACKBLOCK);
203 * Free up SACK blocks for data that's been acked.
206 tcp_sack_ack_blocks(struct scoreboard *scb, tcp_seq th_ack)
208 struct sackblock *sb, *nb;
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);
218 KASSERT(scb->nblocks >= 0,
219 ("SACK block count underflow: %d < 0", scb->nblocks));
222 if (sb && SEQ_GT(th_ack, sb->sblk_start))
223 sb->sblk_start = th_ack; /* other side reneged? XXX */
227 * Delete and free SACK blocks saved in scoreboard.
230 tcp_sack_cleanup(struct scoreboard *scb)
232 struct sackblock *sb, *nb;
234 TAILQ_FOREACH_MUTABLE(sb, &scb->sackblocks, sblk_list, nb) {
235 free_sackblock(scb, sb);
238 KASSERT(scb->nblocks == 0,
239 ("SACK block %d count not zero", scb->nblocks));
240 TAILQ_INIT(&scb->sackblocks);
241 scb->lastfound = NULL;
245 * Delete and free SACK blocks saved in scoreboard.
246 * Delete the one slot block cache.
249 tcp_sack_destroy(struct scoreboard *scb)
251 tcp_sack_cleanup(scb);
252 if (scb->freecache != NULL) {
253 kfree(scb->freecache, M_SACKBLOCK);
254 scb->freecache = NULL;
259 * Cleanup the reported SACK block information
262 tcp_sack_report_cleanup(struct tcpcb *tp)
264 tp->t_flags &= ~(TF_DUPSEG | TF_ENCLOSESEG | TF_SACKLEFT);
265 tp->reportblk.rblk_start = tp->reportblk.rblk_end;
269 * Returns 0 if not D-SACK block,
271 * 2 if duplicate of out-of-order D-SACK block.
274 tcp_sack_ndsack_blocks(struct raw_sackblock *blocks, const int numblocks,
280 if (SEQ_LT(blocks[0].rblk_start, snd_una))
283 /* block 0 inside block 1 */
285 SEQ_GEQ(blocks[0].rblk_start, blocks[1].rblk_start) &&
286 SEQ_LEQ(blocks[0].rblk_end, blocks[1].rblk_end))
293 * Update scoreboard on new incoming ACK.
296 tcp_sack_add_blocks(struct tcpcb *tp, struct tcpopt *to)
298 const int numblocks = to->to_nsackblocks;
299 struct raw_sackblock *blocks = to->to_sackblocks;
300 struct scoreboard *scb = &tp->scb;
303 if (tcp_sack_ndsack_blocks(blocks, numblocks, tp->snd_una) > 0)
308 to->to_flags |= TOF_SACK_REDUNDANT;
309 for (i = startblock; i < numblocks; i++) {
310 struct raw_sackblock *newsackblock = &blocks[i];
314 /* Guard against ACK reordering */
315 if (SEQ_LT(newsackblock->rblk_start, tp->snd_una))
318 /* Don't accept bad SACK blocks */
319 if (SEQ_GT(newsackblock->rblk_end, tp->snd_max)) {
320 tcpstat.tcps_rcvbadsackopt++;
321 break; /* skip all other blocks */
323 tcpstat.tcps_sacksbupdate++;
325 error = insert_block(scb, newsackblock, &update);
327 to->to_flags &= ~TOF_SACK_REDUNDANT;
334 tcp_sack_update_scoreboard(struct tcpcb *tp, struct tcpopt *to)
336 struct scoreboard *scb = &tp->scb;
337 int rexmt_high_update = 0;
339 tcp_sack_ack_blocks(scb, tp->snd_una);
340 tcp_sack_add_blocks(tp, to);
341 update_lostseq(scb, tp->snd_una, tp->t_maxseg, tp->t_rxtthresh);
342 if (SEQ_LT(tp->rexmt_high, tp->snd_una)) {
343 tp->rexmt_high = tp->snd_una;
344 rexmt_high_update = 1;
346 if (tp->t_flags & TF_SACKRESCUED) {
347 if (SEQ_LT(tp->rexmt_rescue, tp->snd_una)) {
348 tp->t_flags &= ~TF_SACKRESCUED;
349 } else if (tcp_aggressive_rescuesack && rexmt_high_update &&
350 SEQ_LT(tp->rexmt_rescue, tp->rexmt_high)) {
351 /* Drag RescueRxt along with HighRxt */
352 tp->rexmt_rescue = tp->rexmt_high;
358 * Insert SACK block into sender's scoreboard.
361 insert_block(struct scoreboard *scb, const struct raw_sackblock *raw_sb,
364 struct sackblock *sb, *workingblock;
365 boolean_t overlap_front;
368 if (TAILQ_EMPTY(&scb->sackblocks)) {
369 struct sackblock *newblock;
371 KASSERT(scb->nblocks == 0, ("emply scb w/ blocks"));
373 newblock = alloc_sackblock(scb, raw_sb);
374 if (newblock == NULL)
376 TAILQ_INSERT_HEAD(&scb->sackblocks, newblock, sblk_list);
381 KASSERT(scb->nblocks > 0, ("insert_block() called w/ no blocks"));
382 KASSERT(scb->nblocks <= MAXSAVEDBLOCKS,
383 ("too many SACK blocks %d", scb->nblocks));
385 overlap_front = sack_block_lookup(scb, raw_sb->rblk_start, &sb);
388 workingblock = alloc_sackblock_limit(scb, raw_sb);
389 if (workingblock == NULL)
391 TAILQ_INSERT_HEAD(&scb->sackblocks, workingblock, sblk_list);
394 if (overlap_front || sb->sblk_end == raw_sb->rblk_start) {
395 /* Extend old block */
397 if (SEQ_GT(raw_sb->rblk_end, sb->sblk_end))
398 sb->sblk_end = raw_sb->rblk_end;
401 tcpstat.tcps_sacksbreused++;
403 workingblock = alloc_sackblock_limit(scb, raw_sb);
404 if (workingblock == NULL)
406 TAILQ_INSERT_AFTER(&scb->sackblocks, sb, workingblock,
412 /* Consolidate right-hand side. */
413 sb = TAILQ_NEXT(workingblock, sblk_list);
415 SEQ_GEQ(workingblock->sblk_end, sb->sblk_end)) {
416 struct sackblock *nextblock;
418 nextblock = TAILQ_NEXT(sb, sblk_list);
419 if (scb->lastfound == sb)
420 scb->lastfound = NULL;
421 /* Remove completely overlapped block */
422 TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list);
423 free_sackblock(scb, sb);
425 KASSERT(scb->nblocks > 0,
426 ("removed overlapped block: %d blocks left", scb->nblocks));
430 SEQ_GEQ(workingblock->sblk_end, sb->sblk_start)) {
431 /* Extend new block to cover partially overlapped old block. */
432 workingblock->sblk_end = sb->sblk_end;
433 if (scb->lastfound == sb)
434 scb->lastfound = NULL;
435 TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list);
436 free_sackblock(scb, sb);
438 KASSERT(scb->nblocks > 0,
439 ("removed partial right: %d blocks left", scb->nblocks));
444 #ifdef DEBUG_SACK_BLOCKS
446 tcp_sack_dump_blocks(struct scoreboard *scb)
448 struct sackblock *sb;
450 kprintf("%d blocks:", scb->nblocks);
451 TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list)
452 kprintf(" [%u, %u)", sb->sblk_start, sb->sblk_end);
457 tcp_sack_dump_blocks(struct scoreboard *scb)
463 * Optimization to quickly determine which packets are lost.
466 update_lostseq(struct scoreboard *scb, tcp_seq snd_una, u_int maxseg,
469 struct sackblock *sb;
471 int bytes_sacked = 0;
476 * The RFC3517bis recommends to reduce the byte threshold.
477 * However, it will cause extra spurious retransmit if
478 * segments are reordered. Before certain DupThresh adaptive
479 * algorithm is implemented, we don't reduce the byte
480 * threshold (tcp_rfc3517bis_rxt is off by default).
482 if (tcp_do_rfc3517bis && tcp_rfc3517bis_rxt)
483 rxtthresh_bytes = (rxtthresh - 1) * maxseg;
485 rxtthresh_bytes = rxtthresh * maxseg;
487 sb = TAILQ_LAST(&scb->sackblocks, sackblock_list);
490 bytes_sacked += sb->sblk_end - sb->sblk_start;
491 if (nsackblocks == rxtthresh ||
492 bytes_sacked >= rxtthresh_bytes) {
493 scb->lostseq = sb->sblk_start;
496 sb = TAILQ_PREV(sb, sackblock_list, sblk_list);
498 scb->lostseq = snd_una;
502 * Return whether the given sequence number is considered lost.
505 tcp_sack_islost(struct scoreboard *scb, tcp_seq seqnum)
507 return SEQ_LT(seqnum, scb->lostseq);
511 * True if at least "amount" has been SACKed. Used by Early Retransmit.
514 tcp_sack_has_sacked(struct scoreboard *scb, u_int amount)
516 struct sackblock *sb;
517 int bytes_sacked = 0;
519 TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) {
520 bytes_sacked += sb->sblk_end - sb->sblk_start;
521 if (bytes_sacked >= amount)
528 * Number of bytes SACKed below seq.
531 tcp_sack_bytes_below(struct scoreboard *scb, tcp_seq seq)
533 struct sackblock *sb;
534 int bytes_sacked = 0;
536 sb = TAILQ_FIRST(&scb->sackblocks);
537 while (sb && SEQ_GT(seq, sb->sblk_start)) {
538 bytes_sacked += seq_min(seq, sb->sblk_end) - sb->sblk_start;
539 sb = TAILQ_NEXT(sb, sblk_list);
545 * Return estimate of the number of bytes outstanding in the network.
548 tcp_sack_compute_pipe(struct tcpcb *tp)
550 struct scoreboard *scb = &tp->scb;
551 struct sackblock *sb;
552 int nlost, nretransmitted;
555 nlost = tp->snd_max - scb->lostseq;
556 nretransmitted = tp->rexmt_high - tp->snd_una;
558 TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) {
559 if (SEQ_LT(sb->sblk_start, tp->rexmt_high)) {
560 end = seq_min(sb->sblk_end, tp->rexmt_high);
561 nretransmitted -= end - sb->sblk_start;
563 if (SEQ_GEQ(sb->sblk_start, scb->lostseq))
564 nlost -= sb->sblk_end - sb->sblk_start;
567 return (nlost + nretransmitted);
571 * Return the sequence number and length of the next segment to transmit
572 * when in Fast Recovery.
575 tcp_sack_nextseg(struct tcpcb *tp, tcp_seq *nextrexmt, uint32_t *plen,
578 struct scoreboard *scb = &tp->scb;
579 struct socket *so = tp->t_inpcb->inp_socket;
580 struct sackblock *sb;
581 const struct sackblock *lastblock =
582 TAILQ_LAST(&scb->sackblocks, sackblock_list);
586 /* skip SACKed data */
587 tcp_sack_skip_sacked(scb, &tp->rexmt_high);
589 /* Look for lost data. */
590 torexmt = tp->rexmt_high;
592 if (lastblock != NULL) {
593 if (SEQ_LT(torexmt, lastblock->sblk_end) &&
594 tcp_sack_islost(scb, torexmt)) {
596 *nextrexmt = torexmt;
597 /* If the left-hand edge has been SACKed, pull it in. */
598 if (sack_block_lookup(scb, torexmt + tp->t_maxseg, &sb))
599 *plen = sb->sblk_start - torexmt;
601 *plen = tp->t_maxseg;
606 /* See if unsent data available within send window. */
607 off = tp->snd_max - tp->snd_una;
608 len = (long) ulmin(so->so_snd.ssb_cc, tp->snd_wnd) - off;
610 *nextrexmt = tp->snd_max; /* Send new data. */
611 *plen = tp->t_maxseg;
615 /* We're less certain this data has been lost. */
616 if (lastblock != NULL && SEQ_LT(torexmt, lastblock->sblk_end))
619 /* Rescue retransmission */
620 if (tcp_do_rescuesack || tcp_do_rfc3517bis) {
621 tcpstat.tcps_sackrescue_try++;
622 if (tp->t_flags & TF_SACKRESCUED) {
623 if (!tcp_aggressive_rescuesack)
627 * Aggressive variant of the rescue retransmission.
629 * The idea of the rescue retransmission is to sustain
630 * the ACK clock thus to avoid timeout retransmission.
632 * Under some situations, the conservative approach
633 * suggested in the draft
634 * http://tools.ietf.org/html/
635 * draft-nishida-tcpm-rescue-retransmission-00
636 * could not sustain ACK clock, since it only allows
637 * one rescue retransmission before a cumulative ACK
638 * covers the segement transmitted by rescue
641 * We try to locate the next unSACKed segment which
642 * follows the previously sent rescue segment. If
643 * there is no such segment, we loop back to the first
644 * unacknowledged segment.
648 * Skip SACKed data, but here we follow
649 * the last transmitted rescue segment.
651 torexmt = tp->rexmt_rescue;
652 tcp_sack_skip_sacked(scb, &torexmt);
653 if (torexmt == tp->snd_max) {
654 /* Nothing left to retransmit; restart */
655 torexmt = tp->snd_una;
660 } else if (tcp_do_smartsack && lastblock == NULL) {
661 tcpstat.tcps_sackrescue_try++;
670 * Return the next sequence number higher than "*prexmt" that has
674 tcp_sack_skip_sacked(struct scoreboard *scb, tcp_seq *prexmt)
676 struct sackblock *sb;
678 /* skip SACKed data */
679 if (sack_block_lookup(scb, *prexmt, &sb))
680 *prexmt = sb->sblk_end;
685 tcp_sack_save_scoreboard(struct scoreboard *scb)
687 struct scoreboard *scb = &tp->scb;
689 scb->sackblocks_prev = scb->sackblocks;
690 TAILQ_INIT(&scb->sackblocks);
694 tcp_sack_revert_scoreboard(struct scoreboard *scb, tcp_seq snd_una,
697 struct sackblock *sb;
699 scb->sackblocks = scb->sackblocks_prev;
701 TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list)
703 tcp_sack_ack_blocks(scb, snd_una);
704 scb->lastfound = NULL;
708 #ifdef DEBUG_SACK_HISTORY
710 tcp_sack_dump_history(char *msg, struct tcpcb *tp)
715 /* only need a couple of these to debug most problems */
719 kprintf("%s:\tnsackhistory %d: ", msg, tp->nsackhistory);
720 for (i = 0; i < tp->nsackhistory; ++i)
721 kprintf("[%u, %u) ", tp->sackhistory[i].rblk_start,
722 tp->sackhistory[i].rblk_end);
727 tcp_sack_dump_history(char *msg, struct tcpcb *tp)
733 * Remove old SACK blocks from the SACK history that have already been ACKed.
736 tcp_sack_ack_history(struct tcpcb *tp)
738 int i, nblocks, openslot;
740 tcp_sack_dump_history("before tcp_sack_ack_history", tp);
741 nblocks = tp->nsackhistory;
742 for (i = openslot = 0; i < nblocks; ++i) {
743 if (SEQ_LEQ(tp->sackhistory[i].rblk_end, tp->rcv_nxt)) {
747 if (SEQ_LT(tp->sackhistory[i].rblk_start, tp->rcv_nxt))
748 tp->sackhistory[i].rblk_start = tp->rcv_nxt;
752 tp->sackhistory[openslot++] = tp->sackhistory[i];
754 tcp_sack_dump_history("after tcp_sack_ack_history", tp);
755 KASSERT(openslot == tp->nsackhistory,
756 ("tcp_sack_ack_history miscounted: %d != %d",
757 openslot, tp->nsackhistory));
761 * Add or merge newblock into reported history.
762 * Also remove or update SACK blocks that will be acked.
765 tcp_sack_update_reported_history(struct tcpcb *tp, tcp_seq start, tcp_seq end)
767 struct raw_sackblock copy[MAX_SACK_REPORT_BLOCKS];
770 tcp_sack_dump_history("before tcp_sack_update_reported_history", tp);
774 * 1) newblock == oldblock
775 * 2) oldblock contains newblock
776 * 3) newblock contains oldblock
777 * 4) tail of oldblock overlaps or abuts start of newblock
778 * 5) tail of newblock overlaps or abuts head of oldblock
780 for (i = cindex = 0; i < tp->nsackhistory; ++i) {
781 struct raw_sackblock *oldblock = &tp->sackhistory[i];
782 tcp_seq old_start = oldblock->rblk_start;
783 tcp_seq old_end = oldblock->rblk_end;
785 if (SEQ_LT(end, old_start) || SEQ_GT(start, old_end)) {
786 /* Case 0: no overlap. Copy old block. */
787 copy[cindex++] = *oldblock;
791 if (SEQ_GEQ(start, old_start) && SEQ_LEQ(end, old_end)) {
792 /* Cases 1 & 2. Move block to front of history. */
797 /* no need to check rest of blocks */
798 for (j = i + 1; j < tp->nsackhistory; ++j)
799 copy[cindex++] = tp->sackhistory[j];
803 if (SEQ_GEQ(old_end, start) && SEQ_LT(old_start, start)) {
804 /* Case 4: extend start of new block. */
806 } else if (SEQ_GEQ(end, old_start) && SEQ_GT(old_end, end)) {
807 /* Case 5: extend end of new block */
810 /* Case 3. Delete old block by not copying it. */
811 KASSERT(SEQ_LEQ(start, old_start) &&
812 SEQ_GEQ(end, old_end),
813 ("bad logic: old [%u, %u), new [%u, %u)",
814 old_start, old_end, start, end));
818 /* insert new block */
819 tp->sackhistory[0].rblk_start = start;
820 tp->sackhistory[0].rblk_end = end;
821 cindex = min(cindex, MAX_SACK_REPORT_BLOCKS - 1);
822 for (i = 0; i < cindex; ++i)
823 tp->sackhistory[i + 1] = copy[i];
824 tp->nsackhistory = cindex + 1;
825 tcp_sack_dump_history("after tcp_sack_update_reported_history", tp);
829 * Fill in SACK report to return to data sender.
832 tcp_sack_fill_report(struct tcpcb *tp, u_char *opt, u_int *plen)
834 u_int optlen = *plen;
835 uint32_t *lp = (uint32_t *)(opt + optlen);
837 tcp_seq hstart = tp->rcv_nxt, hend;
840 KASSERT(TCP_MAXOLEN - optlen >=
841 TCPOLEN_SACK_ALIGNED + TCPOLEN_SACK_BLOCK,
842 ("no room for SACK header and one block: optlen %d", optlen));
844 if (tp->t_flags & TF_DUPSEG)
845 tcpstat.tcps_snddsackopt++;
847 tcpstat.tcps_sndsackopt++;
850 optlen += TCPOLEN_SACK_ALIGNED;
852 tcp_sack_ack_history(tp);
853 if (tp->reportblk.rblk_start != tp->reportblk.rblk_end) {
854 *lp++ = htonl(tp->reportblk.rblk_start);
855 *lp++ = htonl(tp->reportblk.rblk_end);
856 optlen += TCPOLEN_SACK_BLOCK;
857 hstart = tp->reportblk.rblk_start;
858 hend = tp->reportblk.rblk_end;
859 if (tp->t_flags & TF_ENCLOSESEG) {
860 KASSERT(TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK,
861 ("no room for enclosing SACK block: oplen %d",
863 *lp++ = htonl(tp->encloseblk.rblk_start);
864 *lp++ = htonl(tp->encloseblk.rblk_end);
865 optlen += TCPOLEN_SACK_BLOCK;
866 hstart = tp->encloseblk.rblk_start;
867 hend = tp->encloseblk.rblk_end;
869 if (SEQ_GT(hstart, tp->rcv_nxt))
870 tcp_sack_update_reported_history(tp, hstart, hend);
872 if (tcp_do_smartsack && (tp->t_flags & TF_SACKLEFT)) {
873 /* Fill in from left! Walk re-assembly queue. */
876 q = LIST_FIRST(&tp->t_segq);
878 TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK) {
879 *lp++ = htonl(q->tqe_th->th_seq);
880 *lp++ = htonl(TCP_SACK_BLKEND(
881 q->tqe_th->th_seq + q->tqe_len,
882 q->tqe_th->th_flags));
883 optlen += TCPOLEN_SACK_BLOCK;
884 q = LIST_NEXT(q, tqe_q);
889 /* Fill in SACK blocks from right side. */
890 while (n < tp->nsackhistory &&
891 TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK) {
892 if (tp->sackhistory[n].rblk_start != hstart) {
893 *lp++ = htonl(tp->sackhistory[n].rblk_start);
894 *lp++ = htonl(tp->sackhistory[n].rblk_end);
895 optlen += TCPOLEN_SACK_BLOCK;
900 tp->reportblk.rblk_start = tp->reportblk.rblk_end;
901 tp->t_flags &= ~(TF_DUPSEG | TF_ENCLOSESEG | TF_SACKLEFT);
902 nblocks = (lp - olp - 1) / 2;
903 *olp = htonl(TCPOPT_SACK_ALIGNED |
904 (TCPOLEN_SACK + nblocks * TCPOLEN_SACK_BLOCK));