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