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