765b2e509f3935ec91d5ef6313378b6789dd06fe
[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.8 2008/08/15 21:37:16 nth 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 /*
59  * Implemented:
60  *
61  * RFC 2018
62  * RFC 2883
63  * RFC 3517
64  */
65
66 struct 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
74 static void insert_block(struct scoreboard *scb, struct sackblock *newblock);
75 static void update_lostseq(struct scoreboard *scb, tcp_seq snd_una,
76                            u_int maxseg);
77
78 static MALLOC_DEFINE(M_SACKBLOCK, "sblk", "sackblock struct");
79
80 /*
81  * Per-tcpcb initialization.
82  */
83 void
84 tcp_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  */
98 static boolean_t
99 sack_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  */
149 static __inline struct sackblock *
150 alloc_sackblock(void)
151 {
152         return (kmalloc(sizeof(struct sackblock), M_SACKBLOCK, M_NOWAIT));
153 }
154
155 /*
156  * Free a SACK block.
157  */
158 static __inline void
159 free_sackblock(struct sackblock *s)
160 {
161         kfree(s, M_SACKBLOCK);
162 }
163
164 /*
165  * Free up SACK blocks for data that's been acked.
166  */
167 static void
168 tcp_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);
175                 if (scb->lastfound == sb)
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  */
191 void
192 tcp_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  */
211 int
212 tcp_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  */
233 static void
234 tcp_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                         tcpstat.tcps_rcvbadsackopt++;
254                         break;          /* skip all other blocks */
255                 }
256                 tcpstat.tcps_sacksbupdate++;
257
258                 sb = alloc_sackblock();
259                 if (sb == NULL) {       /* do some sort of cleanup? XXX */
260                         tcpstat.tcps_sacksbfailed++;
261                         break;          /* just skip rest of blocks */
262                 }
263                 sb->sblk_start = newsackblock->rblk_start;
264                 sb->sblk_end = newsackblock->rblk_end;
265                 if (TAILQ_EMPTY(&scb->sackblocks)) {
266                         KASSERT(scb->nblocks == 0, ("emply scb w/ blocks"));
267                         scb->nblocks = 1;
268                         TAILQ_INSERT_HEAD(&scb->sackblocks, sb, sblk_list);
269                 } else {
270                         insert_block(scb, sb);
271                 }
272         }
273 }
274
275 void
276 tcp_sack_update_scoreboard(struct tcpcb *tp, struct tcpopt *to)
277 {
278         struct scoreboard *scb = &tp->scb;
279
280         tcp_sack_ack_blocks(scb, tp->snd_una);
281         tcp_sack_add_blocks(tp, to);
282         update_lostseq(scb, tp->snd_una, tp->t_maxseg);
283         if (SEQ_LT(tp->rexmt_high, tp->snd_una))
284                 tp->rexmt_high = tp->snd_una;
285 }
286
287 /*
288  * Insert SACK block into sender's scoreboard.
289  */
290 static void
291 insert_block(struct scoreboard *scb, struct sackblock *newblock)
292 {
293         struct sackblock *sb, *workingblock;
294         boolean_t overlap_front;
295
296         KASSERT(scb->nblocks > 0, ("insert_block() called w/ no blocks"));
297
298         if (scb->nblocks == MAXSAVEDBLOCKS) {
299                 /*
300                  * Should try to kick out older blocks XXX JH
301                  * May be able to coalesce with existing block.
302                  * Or, go other way and free all blocks if we hit this limit.
303                  */
304                 free_sackblock(newblock);
305                 tcpstat.tcps_sacksboverflow++;
306                 return;
307         }
308         KASSERT(scb->nblocks < MAXSAVEDBLOCKS,
309             ("too many SACK blocks %d", scb->nblocks));
310
311         overlap_front = sack_block_lookup(scb, newblock->sblk_start,  &sb);
312
313         if (sb == NULL) {
314                 workingblock = newblock;
315                 TAILQ_INSERT_HEAD(&scb->sackblocks, newblock, sblk_list);
316                 ++scb->nblocks;
317         } else {
318                 if (overlap_front || sb->sblk_end == newblock->sblk_start) {
319                         /* extend old block and discard new one */
320                         workingblock = sb;
321                         if (SEQ_GT(newblock->sblk_end, sb->sblk_end))
322                                 sb->sblk_end = newblock->sblk_end;
323                         free_sackblock(newblock);
324                         tcpstat.tcps_sacksbreused++;
325                 } else {
326                         workingblock = newblock;
327                         TAILQ_INSERT_AFTER(&scb->sackblocks, sb, newblock,
328                                            sblk_list);
329                         ++scb->nblocks;
330                 }
331         }
332
333         /* Consolidate right-hand side. */
334         sb = TAILQ_NEXT(workingblock, sblk_list);
335         while (sb != NULL &&
336             SEQ_GEQ(workingblock->sblk_end, sb->sblk_end)) {
337                 struct sackblock *nextblock;
338
339                 nextblock = TAILQ_NEXT(sb, sblk_list);
340                 if (scb->lastfound == sb)
341                         scb->lastfound = NULL;
342                 /* Remove completely overlapped block */
343                 TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list);
344                 free_sackblock(sb);
345                 --scb->nblocks;
346                 KASSERT(scb->nblocks > 0,
347                     ("removed overlapped block: %d blocks left", scb->nblocks));
348                 sb = nextblock;
349         }
350         if (sb != NULL &&
351             SEQ_GEQ(workingblock->sblk_end, sb->sblk_start)) {
352                 /* Extend new block to cover partially overlapped old block. */
353                 workingblock->sblk_end = sb->sblk_end;
354                 if (scb->lastfound == sb)
355                         scb->lastfound = NULL;
356                 TAILQ_REMOVE(&scb->sackblocks, sb, sblk_list);
357                 free_sackblock(sb);
358                 --scb->nblocks;
359                 KASSERT(scb->nblocks > 0,
360                     ("removed partial right: %d blocks left", scb->nblocks));
361         }
362 }
363
364 #ifdef DEBUG_SACK_BLOCKS
365 static void
366 tcp_sack_dump_blocks(struct scoreboard *scb)
367 {
368         struct sackblock *sb;
369
370         kprintf("%d blocks:", scb->nblocks);
371         TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list)
372                 kprintf(" [%u, %u)", sb->sblk_start, sb->sblk_end);
373         kprintf("\n");
374 }
375 #else
376 static __inline void
377 tcp_sack_dump_blocks(struct scoreboard *scb)
378 {
379 }
380 #endif
381
382 /*
383  * Optimization to quickly determine which packets are lost.
384  */
385 static void
386 update_lostseq(struct scoreboard *scb, tcp_seq snd_una, u_int maxseg)
387 {
388         struct sackblock *sb;
389         int nsackblocks = 0;
390         int bytes_sacked = 0;
391
392         sb = TAILQ_LAST(&scb->sackblocks, sackblock_list);
393         while (sb != NULL) {
394                 ++nsackblocks;
395                 bytes_sacked += sb->sblk_end - sb->sblk_start;
396                 if (nsackblocks == tcprexmtthresh ||
397                     bytes_sacked >= tcprexmtthresh * maxseg) {
398                         scb->lostseq = sb->sblk_start;
399                         return;
400                 }
401                 sb = TAILQ_PREV(sb, sackblock_list, sblk_list);
402         }
403         scb->lostseq = snd_una;
404 }
405
406 /*
407  * Return whether the given sequence number is considered lost.
408  */
409 static boolean_t
410 scb_islost(struct scoreboard *scb, tcp_seq seqnum)
411 {
412         return SEQ_LT(seqnum, scb->lostseq);
413 }
414
415 /*
416  * True if at least "amount" has been SACKed.  Used by Early Retransmit.
417  */
418 boolean_t
419 tcp_sack_has_sacked(struct scoreboard *scb, u_int amount)
420 {
421         struct sackblock *sb;
422         int bytes_sacked = 0;
423
424         TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) {
425                 bytes_sacked += sb->sblk_end - sb->sblk_start;
426                 if (bytes_sacked >= amount)
427                         return TRUE;
428         }
429         return FALSE;
430 }
431
432 /*
433  * Number of bytes SACKed below seq.
434  */
435 int
436 tcp_sack_bytes_below(struct scoreboard *scb, tcp_seq seq)
437 {
438         struct sackblock *sb;
439         int bytes_sacked = 0;
440
441         sb = TAILQ_FIRST(&scb->sackblocks);
442         while (sb && SEQ_GT(seq, sb->sblk_start)) {
443                 bytes_sacked += seq_min(seq, sb->sblk_end) - sb->sblk_start;
444                 sb = TAILQ_NEXT(sb, sblk_list);
445         }
446         return bytes_sacked;
447 }
448
449 /*
450  * Return estimate of the number of bytes outstanding in the network.
451  */
452 uint32_t
453 tcp_sack_compute_pipe(struct tcpcb *tp)
454 {
455         struct scoreboard *scb = &tp->scb;
456         struct sackblock *sb;
457         int nlost, nretransmitted;
458         tcp_seq end;
459
460         nlost = tp->snd_max - scb->lostseq;
461         nretransmitted = tp->rexmt_high - tp->snd_una;
462
463         TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list) {
464                 if (SEQ_LT(sb->sblk_start, tp->rexmt_high)) {
465                         end = seq_min(sb->sblk_end, tp->rexmt_high);
466                         nretransmitted -= end - sb->sblk_start;
467                 }
468                 if (SEQ_GEQ(sb->sblk_start, scb->lostseq))
469                         nlost -= sb->sblk_end - sb->sblk_start;
470         }
471
472         return (nlost + nretransmitted);
473 }
474
475 /*
476  * Return the sequence number and length of the next segment to transmit
477  * when in Fast Recovery.
478  */
479 boolean_t
480 tcp_sack_nextseg(struct tcpcb *tp, tcp_seq *nextrexmt, uint32_t *plen,
481                  boolean_t *lostdup)
482 {
483         struct scoreboard *scb = &tp->scb;
484         struct socket *so = tp->t_inpcb->inp_socket;
485         struct sackblock *sb;
486         const struct sackblock *lastblock =
487             TAILQ_LAST(&scb->sackblocks, sackblock_list);
488         tcp_seq torexmt;
489         long len, off;
490
491         /* skip SACKed data */
492         tcp_sack_skip_sacked(scb, &tp->rexmt_high);
493
494         /* Look for lost data. */
495         torexmt = tp->rexmt_high;
496         *lostdup = FALSE;
497         if (lastblock != NULL) {
498                 if (SEQ_LT(torexmt, lastblock->sblk_end) &&
499                     scb_islost(scb, torexmt)) {
500 sendunsacked:
501                         *nextrexmt = torexmt;
502                         /* If the left-hand edge has been SACKed, pull it in. */
503                         if (sack_block_lookup(scb, torexmt + tp->t_maxseg, &sb))
504                                 *plen = sb->sblk_start - torexmt;
505                         else
506                                 *plen = tp->t_maxseg;
507                         return TRUE;
508                 }
509         }
510
511         /* See if unsent data available within send window. */
512         off = tp->snd_max - tp->snd_una;
513         len = (long) ulmin(so->so_snd.ssb_cc, tp->snd_wnd) - off;
514         if (len > 0) {
515                 *nextrexmt = tp->snd_max;       /* Send new data. */
516                 *plen = tp->t_maxseg;
517                 return TRUE;
518         }
519
520         /* We're less certain this data has been lost. */
521         if (lastblock == NULL || SEQ_LT(torexmt, lastblock->sblk_end))
522                 goto sendunsacked;
523
524         return FALSE;
525 }
526
527 /*
528  * Return the next sequence number higher than "*prexmt" that has
529  * not been SACKed.
530  */
531 void
532 tcp_sack_skip_sacked(struct scoreboard *scb, tcp_seq *prexmt)
533 {
534         struct sackblock *sb;
535
536         /* skip SACKed data */
537         if (sack_block_lookup(scb, *prexmt, &sb))
538                 *prexmt = sb->sblk_end;
539 }
540
541 #ifdef later
542 void
543 tcp_sack_save_scoreboard(struct scoreboard *scb)
544 {
545         struct scoreboard *scb = &tp->scb;
546
547         scb->sackblocks_prev = scb->sackblocks;
548         TAILQ_INIT(&scb->sackblocks);
549 }
550
551 void
552 tcp_sack_revert_scoreboard(struct scoreboard *scb, tcp_seq snd_una,
553                            u_int maxseg)
554 {
555         struct sackblock *sb;
556
557         scb->sackblocks = scb->sackblocks_prev;
558         scb->nblocks = 0;
559         TAILQ_FOREACH(sb, &scb->sackblocks, sblk_list)
560                 ++scb->nblocks;
561         tcp_sack_ack_blocks(scb, snd_una);
562         scb->lastfound = NULL;
563 }
564 #endif
565
566 #ifdef DEBUG_SACK_HISTORY
567 static void
568 tcp_sack_dump_history(char *msg, struct tcpcb *tp)
569 {
570         int i;
571         static int ndumped;
572
573         /* only need a couple of these to debug most problems */
574         if (++ndumped > 900)
575                 return;
576
577         kprintf("%s:\tnsackhistory %d: ", msg, tp->nsackhistory);
578         for (i = 0; i < tp->nsackhistory; ++i)
579                 kprintf("[%u, %u) ", tp->sackhistory[i].rblk_start,
580                     tp->sackhistory[i].rblk_end);
581         kprintf("\n");
582 }
583 #else
584 static __inline void
585 tcp_sack_dump_history(char *msg, struct tcpcb *tp)
586 {
587 }
588 #endif
589
590 /*
591  * Remove old SACK blocks from the SACK history that have already been ACKed.
592  */
593 static void
594 tcp_sack_ack_history(struct tcpcb *tp)
595 {
596         int i, nblocks, openslot;
597
598         tcp_sack_dump_history("before tcp_sack_ack_history", tp);
599         nblocks = tp->nsackhistory;
600         for (i = openslot = 0; i < nblocks; ++i) {
601                 if (SEQ_LEQ(tp->sackhistory[i].rblk_end, tp->rcv_nxt)) {
602                         --tp->nsackhistory;
603                         continue;
604                 }
605                 if (SEQ_LT(tp->sackhistory[i].rblk_start, tp->rcv_nxt))
606                         tp->sackhistory[i].rblk_start = tp->rcv_nxt;
607                 if (i == openslot)
608                         ++openslot;
609                 else
610                         tp->sackhistory[openslot++] = tp->sackhistory[i];
611         }
612         tcp_sack_dump_history("after tcp_sack_ack_history", tp);
613         KASSERT(openslot == tp->nsackhistory,
614             ("tcp_sack_ack_history miscounted: %d != %d",
615             openslot, tp->nsackhistory));
616 }
617
618 /*
619  * Add or merge newblock into reported history.
620  * Also remove or update SACK blocks that will be acked.
621  */
622 static void
623 tcp_sack_update_reported_history(struct tcpcb *tp, tcp_seq start, tcp_seq end)
624 {
625         struct raw_sackblock copy[MAX_SACK_REPORT_BLOCKS];
626         int i, cindex;
627
628         tcp_sack_dump_history("before tcp_sack_update_reported_history", tp);
629         /*
630          * Six cases:
631          *      0) no overlap
632          *      1) newblock == oldblock
633          *      2) oldblock contains newblock
634          *      3) newblock contains oldblock
635          *      4) tail of oldblock overlaps or abuts start of newblock
636          *      5) tail of newblock overlaps or abuts head of oldblock
637          */
638         for (i = cindex = 0; i < tp->nsackhistory; ++i) {
639                 struct raw_sackblock *oldblock = &tp->sackhistory[i];
640                 tcp_seq old_start = oldblock->rblk_start;
641                 tcp_seq old_end = oldblock->rblk_end;
642
643                 if (SEQ_LT(end, old_start) || SEQ_GT(start, old_end)) {
644                         /* Case 0:  no overlap.  Copy old block. */
645                         copy[cindex++] = *oldblock;
646                         continue;
647                 }
648
649                 if (SEQ_GEQ(start, old_start) && SEQ_LEQ(end, old_end)) {
650                         /* Cases 1 & 2.  Move block to front of history. */
651                         int j;
652
653                         start = old_start;
654                         end = old_end;
655                         /* no need to check rest of blocks */
656                         for (j = i + 1; j < tp->nsackhistory; ++j)
657                                 copy[cindex++] = tp->sackhistory[j];
658                         break;
659                 }
660
661                 if (SEQ_GEQ(old_end, start) && SEQ_LT(old_start, start)) {
662                         /* Case 4:  extend start of new block. */
663                         start = old_start;
664                 } else if (SEQ_GEQ(end, old_start) && SEQ_GT(old_end, end)) {
665                         /* Case 5: extend end of new block */
666                         end = old_end;
667                 } else {
668                         /* Case 3.  Delete old block by not copying it. */
669                         KASSERT(SEQ_LEQ(start, old_start) &&
670                                 SEQ_GEQ(end, old_end),
671                             ("bad logic: old [%u, %u), new [%u, %u)",
672                              old_start, old_end, start, end));
673                 }
674         }
675
676         /* insert new block */
677         tp->sackhistory[0].rblk_start = start;
678         tp->sackhistory[0].rblk_end = end;
679         cindex = min(cindex, MAX_SACK_REPORT_BLOCKS - 1);
680         for (i = 0; i < cindex; ++i)
681                 tp->sackhistory[i + 1] = copy[i];
682         tp->nsackhistory = cindex + 1;
683         tcp_sack_dump_history("after tcp_sack_update_reported_history", tp);
684 }
685
686 /*
687  * Fill in SACK report to return to data sender.
688  */
689 void
690 tcp_sack_fill_report(struct tcpcb *tp, u_char *opt, u_int *plen)
691 {
692         u_int optlen = *plen;
693         uint32_t *lp = (uint32_t *)(opt + optlen);
694         uint32_t *olp;
695         tcp_seq hstart = tp->rcv_nxt, hend;
696         int nblocks;
697
698         KASSERT(TCP_MAXOLEN - optlen >=
699             TCPOLEN_SACK_ALIGNED + TCPOLEN_SACK_BLOCK,
700             ("no room for SACK header and one block: optlen %d", optlen));
701
702         if (tp->t_flags & TF_DUPSEG)
703                 tcpstat.tcps_snddsackopt++;
704         else
705                 tcpstat.tcps_sndsackopt++;
706
707         olp = lp++;
708         optlen += TCPOLEN_SACK_ALIGNED;
709
710         tcp_sack_ack_history(tp);
711         if (tp->reportblk.rblk_start != tp->reportblk.rblk_end) {
712                 *lp++ = htonl(tp->reportblk.rblk_start);
713                 *lp++ = htonl(tp->reportblk.rblk_end);
714                 optlen += TCPOLEN_SACK_BLOCK;
715                 hstart = tp->reportblk.rblk_start;
716                 hend = tp->reportblk.rblk_end;
717                 if (tp->t_flags & TF_ENCLOSESEG) {
718                         KASSERT(TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK,
719                             ("no room for enclosing SACK block: oplen %d",
720                             optlen));
721                         *lp++ = htonl(tp->encloseblk.rblk_start);
722                         *lp++ = htonl(tp->encloseblk.rblk_end);
723                         optlen += TCPOLEN_SACK_BLOCK;
724                         hstart = tp->encloseblk.rblk_start;
725                         hend = tp->encloseblk.rblk_end;
726                 }
727                 if (SEQ_GT(hstart, tp->rcv_nxt))
728                         tcp_sack_update_reported_history(tp, hstart, hend);
729         }
730         if (tcp_do_smartsack && (tp->t_flags & TF_SACKLEFT)) {
731                 /* Fill in from left!  Walk re-assembly queue. */
732                 struct tseg_qent *q;
733
734                 q = LIST_FIRST(&tp->t_segq);
735                 while (q != NULL &&
736                     TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK) {
737                         *lp++ = htonl(q->tqe_th->th_seq);
738                         *lp++ = htonl(TCP_SACK_BLKEND(
739                             q->tqe_th->th_seq + q->tqe_len,
740                             q->tqe_th->th_flags));
741                         optlen += TCPOLEN_SACK_BLOCK;
742                         q = LIST_NEXT(q, tqe_q);
743                 }
744         } else {
745                 int n = 0;
746
747                 /* Fill in SACK blocks from right side. */
748                 while (n < tp->nsackhistory &&
749                     TCP_MAXOLEN - optlen >= TCPOLEN_SACK_BLOCK) {
750                         if (tp->sackhistory[n].rblk_start != hstart) {
751                                 *lp++ = htonl(tp->sackhistory[n].rblk_start);
752                                 *lp++ = htonl(tp->sackhistory[n].rblk_end);
753                                 optlen += TCPOLEN_SACK_BLOCK;
754                         }
755                         ++n;
756                 }
757         }
758         tp->reportblk.rblk_start = tp->reportblk.rblk_end;
759         tp->t_flags &= ~(TF_DUPSEG | TF_ENCLOSESEG | TF_SACKLEFT);
760         nblocks = (lp - olp - 1) / 2;
761         *olp = htonl(TCPOPT_SACK_ALIGNED |
762                      (TCPOLEN_SACK + nblocks * TCPOLEN_SACK_BLOCK));
763         *plen = optlen;
764 }