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