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