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