Rename printf -> kprintf in sys/ and add some defines where necessary
[dragonfly.git] / sys / netinet / tcp_sack.c
CommitLineData
91489f6b
JH
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 *
f3f70f0d 33 * $DragonFly: src/sys/netinet/tcp_sack.c,v 1.3 2006/01/14 11:33:50 swildner Exp $
91489f6b
JH
34 */
35
36/*
37 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
38 *
39 * License terms: all terms for the DragonFly license above plus the following:
40 *
41 * 4. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 *
44 * This product includes software developed by Jeffrey M. Hsu
45 * for the DragonFly Project.
46 *
47 * This requirement may be waived with permission from Jeffrey Hsu.
48 * This requirement will sunset and may be removed on November 13, 2005,
49 * after which the standard DragonFly license (as shown above) will
50 * apply.
51 */
52
53#include <sys/param.h>
54#include <sys/systm.h>
55#include <sys/kernel.h>
56#include <sys/malloc.h>
57#include <sys/queue.h>
58#include <sys/thread.h>
59#include <sys/types.h>
60#include <sys/socket.h>
61#include <sys/socketvar.h>
62
63#include <net/if.h>
64
65#include <netinet/in.h>
66#include <netinet/in_systm.h>
67#include <netinet/ip.h>
68#include <netinet/in_var.h>
69#include <netinet/in_pcb.h>
70#include <netinet/ip_var.h>
71#include <netinet/tcp.h>
72#include <netinet/tcp_seq.h>
73#include <netinet/tcp_var.h>
74
75#include <vm/vm_zone.h>
76
77struct sackblock {
78 tcp_seq sblk_start;
79 tcp_seq sblk_end;
80 TAILQ_ENTRY(sackblock) sblk_list;
81};
82
83#define MAXSAVEDBLOCKS 8 /* per connection limit */
84
85static void insert_block(struct scoreboard *scb, struct sackblock *newblock);
86static void update_lostseq(struct scoreboard *scb, tcp_seq snd_una,
87 u_int maxseg);
88
89static vm_zone_t sackblock_zone;
90
91/*
92 * Initialize SACK module.
93 */
94void
f3f70f0d 95tcp_sack_init(void)
91489f6b
JH
96{
97 sackblock_zone = zinit("sack blocks", sizeof(struct sackblock),
98 maxsockets, ZONE_INTERRUPT, 0);
99}
100
101/*
102 * Per-tcpcb initialization.
103 */
104void
105tcp_sack_tcpcb_init(struct tcpcb *tp)
106{
107 struct scoreboard *scb = &tp->scb;
108
109 scb->nblocks = 0;
110 TAILQ_INIT(&scb->sackblocks);
111 scb->lastfound = NULL;
112}
113
114/*
115 * Find the SACK block containing or immediately preceding "seq".
116 * The boolean result indicates whether the sequence is actually
117 * contained in the SACK block.
118 */
119static boolean_t
120sack_block_lookup(struct scoreboard *scb, tcp_seq seq, struct sackblock **sb)
121{
122 struct sackblock *hint = scb->lastfound;
123 struct sackblock *cur, *last, *prev;
124
125 if (TAILQ_EMPTY(&scb->sackblocks)) {
126 *sb = NULL;
127 return FALSE;
128 }
129
130 if (hint == NULL) {
131 /* No hint. Search from start to end. */
132 cur = TAILQ_FIRST(&scb->sackblocks);
133 last = NULL;
134 prev = TAILQ_LAST(&scb->sackblocks, sackblock_list);
135 } else {
136 if (SEQ_GEQ(seq, hint->sblk_start)) {
137 /* Search from hint to end of list. */
138 cur = hint;
139 last = NULL;
140 prev = TAILQ_LAST(&scb->sackblocks, sackblock_list);
141 } else {
142 /* Search from front of list to hint. */
143 cur = TAILQ_FIRST(&scb->sackblocks);
144 last = hint;
145 prev = TAILQ_PREV(hint, sackblock_list, sblk_list);
146 }
147 }
148
149 do {
150 if (SEQ_GT(cur->sblk_end, seq)) {
151 if (SEQ_GEQ(seq, cur->sblk_start)) {
152 *sb = scb->lastfound = cur;
153 return TRUE;
154 } else {
155 *sb = scb->lastfound =
156 TAILQ_PREV(cur, sackblock_list, sblk_list);
157 return FALSE;
158 }
159 }
160 cur = TAILQ_NEXT(cur, sblk_list);
161 } while (cur != last);
162
163 *sb = scb->lastfound = prev;
164 return FALSE;
165}
166
167/*
168 * Allocate a SACK block.
169 */
170static __inline struct sackblock *
171alloc_sackblock(void)
172{
173 return zalloc(sackblock_zone);
174}
175
176/*
177 * Free a SACK block.
178 */
179static __inline void
180free_sackblock(struct sackblock *s)
181{
182 zfree(sackblock_zone, s);
183}
184
185/*
186 * Free up SACK blocks for data that's been acked.
187 */
188static void
189tcp_sack_ack_blocks(struct scoreboard *scb, tcp_seq th_ack)
190{
191 struct sackblock *sb, *nb;
192
193 sb = TAILQ_FIRST(&scb->sackblocks);
194 while (sb && SEQ_LEQ(sb->sblk_end, th_ack)) {
195 nb = TAILQ_NEXT(sb, sblk_list);
196 if (sb == scb->lastfound)
197 scb->lastfound = NULL;
198 TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list);
199 free_sackblock(sb);
200 --scb->nblocks;
201 KASSERT(scb->nblocks >= 0,
202 ("SACK block count underflow: %d < 0", scb->nblocks));
203 sb = nb;
204 }
205 if (sb && SEQ_GT(th_ack, sb->sblk_start))
206 sb->sblk_start = th_ack; /* other side reneged? XXX */
207}
208
209/*
210 * Delete and free SACK blocks saved in scoreboard.
211 */
212void
213tcp_sack_cleanup(struct scoreboard *scb)
214{
215 struct sackblock *sb, *nb;
216
217 TAILQ_FOREACH_MUTABLE(sb, &scb->sackblocks, sblk_list, nb) {
218 free_sackblock(sb);
219 --scb->nblocks;
220 }
221 KASSERT(scb->nblocks == 0,
222 ("SACK block %d count not zero", scb->nblocks));
223 TAILQ_INIT(&scb->sackblocks);
224 scb->lastfound = NULL;
225}
226
227/*
228 * Returns 0 if not D-SACK block,
229 * 1 if D-SACK,
230 * 2 if duplicate of out-of-order D-SACK block.
231 */
232int
233tcp_sack_ndsack_blocks(struct raw_sackblock *blocks, const int numblocks,
234 tcp_seq snd_una)
235{
236 if (numblocks == 0)
237 return 0;
238
239 if (SEQ_LT(blocks[0].rblk_start, snd_una))
240 return 1;
241
242 /* block 0 inside block 1 */
243 if (numblocks > 1 &&
244 SEQ_GEQ(blocks[0].rblk_start, blocks[1].rblk_start) &&
245 SEQ_LEQ(blocks[0].rblk_end, blocks[1].rblk_end))
246 return 2;
247
248 return 0;
249}
250
251/*
252 * Update scoreboard on new incoming ACK.
253 */
254static void
255tcp_sack_add_blocks(struct tcpcb *tp, struct tcpopt *to)
256{
257 const int numblocks = to->to_nsackblocks;
258 struct raw_sackblock *blocks = to->to_sackblocks;
259 struct scoreboard *scb = &tp->scb;
260 struct sackblock *sb;
261 int startblock;
262 int i;
263
264 if (tcp_sack_ndsack_blocks(blocks, numblocks, tp->snd_una) > 0)
265 startblock = 1;
266 else
267 startblock = 0;
268
269 for (i = startblock; i < numblocks; i++) {
270 struct raw_sackblock *newsackblock = &blocks[i];
271
272 /* don't accept bad SACK blocks */
273 if (SEQ_GT(newsackblock->rblk_end, tp->snd_max))
274 break; /* skip all other blocks */
275
276 sb = alloc_sackblock();
277 if (sb == NULL) /* do some sort of cleanup? XXX */
278 break; /* just skip rest of blocks */
279 sb->sblk_start = newsackblock->rblk_start;
280 sb->sblk_end = newsackblock->rblk_end;
281 if (TAILQ_EMPTY(&scb->sackblocks)) {
282 KASSERT(scb->nblocks == 0, ("emply scb w/ blocks"));
283 scb->nblocks = 1;
284 TAILQ_INSERT_HEAD(&scb->sackblocks, sb, sblk_list);
285 } else {
286 insert_block(scb, sb);
287 }
288 }
289}
290
291void
292tcp_sack_update_scoreboard(struct tcpcb *tp, struct tcpopt *to)
293{
294 struct scoreboard *scb = &tp->scb;
295
296 tcp_sack_ack_blocks(scb, tp->snd_una);
297 tcp_sack_add_blocks(tp, to);
298 update_lostseq(scb, tp->snd_una, tp->t_maxseg);
299 if (SEQ_LT(tp->rexmt_high, tp->snd_una))
300 tp->rexmt_high = tp->snd_una;
301}
302
303/*
304 * Insert SACK block into sender's scoreboard.
305 */
306static void
307insert_block(struct scoreboard *scb, struct sackblock *newblock)
308{
309 struct sackblock *sb, *workingblock;
310 boolean_t overlap_front;
311
312 KASSERT(scb->nblocks > 0, ("insert_block() called w/ no blocks"));
313
314 if (scb->nblocks == MAXSAVEDBLOCKS) {
315 /*
316 * Should try to kick out older blocks XXX JH
317 * May be able to coalesce with existing block.
318 * Or, go other way and free all blocks if we hit this limit.
319 */
320 free_sackblock(newblock);
321 return;
322 }
323 KASSERT(scb->nblocks < MAXSAVEDBLOCKS,
324 ("too many SACK blocks %d", scb->nblocks));
325
326 overlap_front = sack_block_lookup(scb, newblock->sblk_start, &sb);
327
328 if (sb == NULL) {
329 workingblock = newblock;
330 TAILQ_INSERT_HEAD(&scb->sackblocks, newblock, sblk_list);
331 ++scb->nblocks;
332 } else {
333 if (overlap_front || sb->sblk_end == newblock->sblk_start) {
334 /* extend old block and discard new one */
335 workingblock = sb;
336 if (SEQ_GT(newblock->sblk_end, sb->sblk_end))
337 sb->sblk_end = newblock->sblk_end;
338 free_sackblock(newblock);
339 } else {
340 workingblock = newblock;
341 TAILQ_INSERT_AFTER(&scb->sackblocks, sb, newblock,
342 sblk_list);
343 ++scb->nblocks;
344 }
345 }
346
347 /* Consolidate right-hand side. */
348 sb = TAILQ_NEXT(workingblock, sblk_list);
349 while (sb != NULL &&
350 SEQ_GEQ(workingblock->sblk_end, sb->sblk_end)) {
351 struct sackblock *nextblock;
352
353 nextblock = TAILQ_NEXT(sb, sblk_list);
354 /* Remove completely overlapped block */
355 TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list);
356 free_sackblock(sb);
357 --scb->nblocks;
358 KASSERT(scb->nblocks > 0,
359 ("removed overlapped block: %d blocks left", scb->nblocks));
360 sb = nextblock;
361 }
362 if (sb != NULL &&
363 SEQ_GEQ(workingblock->sblk_end, sb->sblk_start)) {
364 /* Extend new block to cover partially overlapped old block. */
365 workingblock->sblk_end = sb->sblk_end;
366 TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list);
367 free_sackblock(sb);
368 --scb->nblocks;
369 KASSERT(scb->nblocks > 0,
370 ("removed partial right: %d blocks left", scb->nblocks));
371 }
372}
373
374#ifdef DEBUG_SACK_BLOCKS
375static void
376tcp_sack_dump_blocks(struct scoreboard *scb)
377{
378 struct sackblock *sb;
379
380 printf("%d blocks:", scb->nblocks);
381 TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list)
382 printf(" [%u, %u)", sb->sblk_start, sb->sblk_end);
383 printf("\n");
384}
385#else
386static __inline void
387tcp_sack_dump_blocks(struct scoreboard *scb)
388{
389}
390#endif
391
392/*
393 * Optimization to quickly determine which packets are lost.
394 */
395static void
396update_lostseq(struct scoreboard *scb, tcp_seq snd_una, u_int maxseg)
397{
398 struct sackblock *sb;
399 int nsackblocks = 0;
400 int bytes_sacked = 0;
401
402 sb = TAILQ_LAST(&scb->sackblocks, sackblock_list);
403 while (sb != NULL) {
404 ++nsackblocks;
405 bytes_sacked += sb->sblk_end - sb->sblk_start;
406 if (nsackblocks == tcprexmtthresh ||
407 bytes_sacked >= tcprexmtthresh * maxseg) {
408 scb->lostseq = sb->sblk_start;
409 return;
410 }
411 sb = TAILQ_PREV(sb, sackblock_list, sblk_list);
412 }
413 scb->lostseq = snd_una;
414}
415
416/*
417 * Return whether the given sequence number is considered lost.
418 */
419static boolean_t
420scb_islost(struct scoreboard *scb, tcp_seq seqnum)
421{
422 return SEQ_LT(seqnum, scb->lostseq);
423}
424
425/*
426 * True if at least "amount" has been SACKed. Used by Early Retransmit.
427 */
428boolean_t
429tcp_sack_has_sacked(struct scoreboard *scb, u_int amount)
430{
431 struct sackblock *sb;
432 int bytes_sacked = 0;
433
434 TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) {
435 bytes_sacked += sb->sblk_end - sb->sblk_start;
436 if (bytes_sacked >= amount)
437 return TRUE;
438 }
439 return FALSE;
440}
441
9a5b142f
JH
442/*
443 * Number of bytes SACKed below seq.
444 */
445int
446tcp_sack_bytes_below(struct scoreboard *scb, tcp_seq seq)
447{
448 struct sackblock *sb;
449 int bytes_sacked = 0;
450
451 sb = TAILQ_FIRST(&scb->sackblocks);
452 while (sb && SEQ_GT(seq, sb->sblk_start)) {
453 bytes_sacked += seq_min(seq, sb->sblk_end) - sb->sblk_start;
454 sb = TAILQ_NEXT(sb, sblk_list);
455 }
456 return bytes_sacked;
457}
458
91489f6b
JH
459/*
460 * Return estimate of the number of bytes outstanding in the network.
461 */
462uint32_t
463tcp_sack_compute_pipe(struct tcpcb *tp)
464{
465 struct scoreboard *scb = &tp->scb;
466 struct sackblock *sb;
467 int nlost, nretransmitted;
468 tcp_seq end;
469
470 nlost = tp->snd_max - scb->lostseq;
471 nretransmitted = tp->rexmt_high - tp->snd_una;
472
473 TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) {
474 if (SEQ_LT(sb->sblk_start, tp->rexmt_high)) {
475 end = seq_min(sb->sblk_end, tp->rexmt_high);
476 nretransmitted -= end - sb->sblk_start;
477 }
478 if (SEQ_GEQ(sb->sblk_start, scb->lostseq))
479 nlost -= sb->sblk_end - sb->sblk_start;
480 }
481
482 return (nlost + nretransmitted);
483}
484
485/*
486 * Return the sequence number and length of the next segment to transmit
487 * when in Fast Recovery.
488 */
489boolean_t
490tcp_sack_nextseg(struct tcpcb *tp, tcp_seq *nextrexmt, uint32_t *plen,
491 boolean_t *lostdup)
492{
493 struct scoreboard *scb = &tp->scb;
494 struct socket *so = tp->t_inpcb->inp_socket;
495 struct sackblock *sb;
496 const struct sackblock *lastblock =
497 TAILQ_LAST(&scb->sackblocks, sackblock_list);
498 tcp_seq torexmt;
499 long len, off;
500
501 /* skip SACKed data */
502 tcp_sack_skip_sacked(scb, &tp->rexmt_high);
503
504 /* Look for lost data. */
505 torexmt = tp->rexmt_high;
506 *lostdup = FALSE;
507 if (lastblock != NULL) {
508 if (SEQ_LT(torexmt, lastblock->sblk_end) &&
509 scb_islost(scb, torexmt)) {
510sendunsacked:
511 *nextrexmt = torexmt;
512 /* If the left-hand edge has been SACKed, pull it in. */
513 if (sack_block_lookup(scb, torexmt + tp->t_maxseg, &sb))
514 *plen = sb->sblk_start - torexmt;
515 else
516 *plen = tp->t_maxseg;
517 return TRUE;
518 }
519 }
520
521 /* See if unsent data available within send window. */
522 off = tp->snd_max - tp->snd_una;
523 len = (long) ulmin(so->so_snd.sb_cc, tp->snd_wnd) - off;
524 if (len > 0) {
525 *nextrexmt = tp->snd_max; /* Send new data. */
526 *plen = tp->t_maxseg;
527 return TRUE;
528 }
529
530 /* We're less certain this data has been lost. */
531 if (lastblock == NULL || SEQ_LT(torexmt, lastblock->sblk_end))
532 goto sendunsacked;
533
534 return FALSE;
535}
536
537/*
538 * Return the next sequence number higher than "*prexmt" that has
539 * not been SACKed.
540 */
541void
542tcp_sack_skip_sacked(struct scoreboard *scb, tcp_seq *prexmt)
543{
544 struct sackblock *sb;
545
546 /* skip SACKed data */
547 if (sack_block_lookup(scb, *prexmt, &sb))
548 *prexmt = sb->sblk_end;
549}
550
551#ifdef later
552void
553tcp_sack_save_scoreboard(struct scoreboard *scb)
554{
555 struct scoreboard *scb = &tp->scb;
556
557 scb->sackblocks_prev = scb->sackblocks;
558 TAILQ_INIT(&scb->sackblocks);
559}
560
561void
562tcp_sack_revert_scoreboard(struct scoreboard *scb, tcp_seq snd_una,
563 u_int maxseg)
564{
565 struct sackblock *sb;
566
567 scb->sackblocks = scb->sackblocks_prev;
568 scb->nblocks = 0;
569 TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list)
570 ++scb->nblocks;
571 tcp_sack_ack_blocks(scb, snd_una);
572 scb->lastfound = NULL;
573}
574#endif
575
576#ifdef DEBUG_SACK_HISTORY
577static void
578tcp_sack_dump_history(char *msg, struct tcpcb *tp)
579{
580 int i;
581 static int ndumped;
582
583 /* only need a couple of these to debug most problems */
584 if (++ndumped > 900)
585 return;
586
587 printf("%s:\tnsackhistory %d: ", msg, tp->nsackhistory);
588 for (i = 0; i < tp->nsackhistory; ++i)
589 printf("[%u, %u) ", tp->sackhistory[i].rblk_start,
590 tp->sackhistory[i].rblk_end);
591 printf("\n");
592}
593#else
594static __inline void
595tcp_sack_dump_history(char *msg, struct tcpcb *tp)
596{
597}
598#endif
599
600/*
601 * Remove old SACK blocks from the SACK history that have already been ACKed.
602 */
603static void
604tcp_sack_ack_history(struct tcpcb *tp)
605{
606 int i, nblocks, openslot;
607
608 tcp_sack_dump_history("before tcp_sack_ack_history", tp);
609 nblocks = tp->nsackhistory;
610 for (i = openslot = 0; i < nblocks; ++i) {
611 if (SEQ_LEQ(tp->sackhistory[i].rblk_end, tp->rcv_nxt)) {
612 --tp->nsackhistory;
613 continue;
614 }
615 if (SEQ_LT(tp->sackhistory[i].rblk_start, tp->rcv_nxt))
616 tp->sackhistory[i].rblk_start = tp->rcv_nxt;
617 if (i == openslot)
618 ++openslot;
619 else
620 tp->sackhistory[openslot++] = tp->sackhistory[i];
621 }
622 tcp_sack_dump_history("after tcp_sack_ack_history", tp);
623 KASSERT(openslot == tp->nsackhistory,
624 ("tcp_sack_ack_history miscounted: %d != %d",
625 openslot, tp->nsackhistory));
626}
627
628/*
629 * Add or merge newblock into reported history.
630 * Also remove or update SACK blocks that will be acked.
631 */
632static void
633tcp_sack_update_reported_history(struct tcpcb *tp, tcp_seq start, tcp_seq end)
634{
635 struct raw_sackblock copy[MAX_SACK_REPORT_BLOCKS];
636 int i, cindex;
637
638 tcp_sack_dump_history("before tcp_sack_update_reported_history", tp);
639 /*
640 * Six cases:
641 * 0) no overlap
642 * 1) newblock == oldblock
643 * 2) oldblock contains newblock
644 * 3) newblock contains oldblock
645 * 4) tail of oldblock overlaps or abuts start of newblock
646 * 5) tail of newblock overlaps or abuts head of oldblock
647 */
648 for (i = cindex = 0; i < tp->nsackhistory; ++i) {
649 struct raw_sackblock *oldblock = &tp->sackhistory[i];
650 tcp_seq old_start = oldblock->rblk_start;
651 tcp_seq old_end = oldblock->rblk_end;
652
653 if (SEQ_LT(end, old_start) || SEQ_GT(start, old_end)) {
654 /* Case 0: no overlap. Copy old block. */
655 copy[cindex++] = *oldblock;
656 continue;
657 }
658
659 if (SEQ_GEQ(start, old_start) && SEQ_LEQ(end, old_end)) {
660 /* Cases 1 & 2. Move block to front of history. */
661 int j;
662
663 start = old_start;
664 end = old_end;
665 /* no need to check rest of blocks */
666 for (j = i + 1; j < tp->nsackhistory; ++j)
667 copy[cindex++] = tp->sackhistory[j];
668 break;
669 }
670
671 if (SEQ_GEQ(old_end, start) && SEQ_LT(old_start, start)) {
672 /* Case 4: extend start of new block. */
673 start = old_start;
674 } else if (SEQ_GEQ(end, old_start) && SEQ_GT(old_end, end)) {
675 /* Case 5: extend end of new block */
676 end = old_end;
677 } else {
678 /* Case 3. Delete old block by not copying it. */
679 KASSERT(SEQ_LEQ(start, old_start) &&
680 SEQ_GEQ(end, old_end),
681 ("bad logic: old [%u, %u), new [%u, %u)",
682 old_start, old_end, start, end));
683 }
684 }
685
686 /* insert new block */
687 tp->sackhistory[0].rblk_start = start;
688 tp->sackhistory[0].rblk_end = end;
689 cindex = min(cindex, MAX_SACK_REPORT_BLOCKS - 1);
690 for (i = 0; i < cindex; ++i)
691 tp->sackhistory[i + 1] = copy[i];
692 tp->nsackhistory = cindex + 1;
693 tcp_sack_dump_history("after tcp_sack_update_reported_history", tp);
694}
695
696/*
697 * Fill in SACK report to return to data sender.
698 */
699void
700tcp_sack_fill_report(struct tcpcb *tp, u_char *opt, u_int *plen)
701{
702 u_int optlen = *plen;
703 uint32_t *lp = (uint32_t *)(opt + optlen);
704 uint32_t *olp;
705 tcp_seq hstart = tp->rcv_nxt, hend;
706 int nblocks;
707
708 KASSERT(TCP_MAXOLEN - optlen >=
709 TCPOLEN_SACK_ALIGNED + TCPOLEN_SACK_BLOCK,
710 ("no room for SACK header and one block: optlen %d", optlen));
711
712 olp = lp++;
713 optlen += TCPOLEN_SACK_ALIGNED;
714
715 tcp_sack_ack_history(tp);
716 if (tp->reportblk.rblk_start != tp->reportblk.rblk_end) {
717 *lp++ = htonl(tp->reportblk.rblk_start);
718 *lp++ = htonl(tp->reportblk.rblk_end);
719 optlen += TCPOLEN_SACK_BLOCK;
720 hstart = tp->reportblk.rblk_start;
721 hend = tp->reportblk.rblk_end;
722 if (tp->t_flags & TF_ENCLOSESEG) {
723 KASSERT(TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK,
724 ("no room for enclosing SACK block: oplen %d",
725 optlen));
726 *lp++ = htonl(tp->encloseblk.rblk_start);
727 *lp++ = htonl(tp->encloseblk.rblk_end);
728 optlen += TCPOLEN_SACK_BLOCK;
729 hstart = tp->encloseblk.rblk_start;
730 hend = tp->encloseblk.rblk_end;
731 }
732 if (SEQ_GT(hstart, tp->rcv_nxt))
733 tcp_sack_update_reported_history(tp, hstart, hend);
734 }
735 if (tcp_do_smartsack && (tp->t_flags & TF_SACKLEFT)) {
736 /* Fill in from left! Walk re-assembly queue. */
737 struct tseg_qent *q;
738
739 q = LIST_FIRST(&tp->t_segq);
740 while (q != NULL &&
741 TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK) {
742 *lp++ = htonl(q->tqe_th->th_seq);
743 *lp++ = htonl(q->tqe_th->th_seq + q->tqe_len);
744 optlen += TCPOLEN_SACK_BLOCK;
745 q = LIST_NEXT(q, tqe_q);
746 }
747 } else {
748 int n = 0;
749
750 /* Fill in SACK blocks from right side. */
751 while (n < tp->nsackhistory &&
752 TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK) {
753 if (tp->sackhistory[n].rblk_start != hstart) {
754 *lp++ = htonl(tp->sackhistory[n].rblk_start);
755 *lp++ = htonl(tp->sackhistory[n].rblk_end);
756 optlen += TCPOLEN_SACK_BLOCK;
757 }
758 ++n;
759 }
760 }
761 tp->reportblk.rblk_start = tp->reportblk.rblk_end;
762 tp->t_flags &= ~(TF_DUPSEG | TF_ENCLOSESEG | TF_SACKLEFT);
763 nblocks = (lp - olp - 1) / 2;
764 *olp = htonl(TCPOPT_SACK_ALIGNED |
765 (TCPOLEN_SACK + nblocks * TCPOLEN_SACK_BLOCK));
766 *plen = optlen;
767}