2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)tcp_sack.c 8.12 (Berkeley) 5/24/95
34 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @@(#)COPYRIGHT 1.1 (NRL) 17 January 1995
63 * NRL grants permission for redistribution and use in source and binary
64 * forms, with or without modification, of the software and documentation
65 * created at NRL provided that the following conditions are met:
67 * 1. Redistributions of source code must retain the above copyright
68 * notice, this list of conditions and the following disclaimer.
69 * 2. Redistributions in binary form must reproduce the above copyright
70 * notice, this list of conditions and the following disclaimer in the
71 * documentation and/or other materials provided with the distribution.
72 * 3. All advertising materials mentioning features or use of this software
73 * must display the following acknowledgements:
74 * This product includes software developed by the University of
75 * California, Berkeley and its contributors.
76 * This product includes software developed at the Information
77 * Technology Division, US Naval Research Laboratory.
78 * 4. Neither the name of the NRL nor the names of its contributors
79 * may be used to endorse or promote products derived from this software
80 * without specific prior written permission.
82 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
83 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
84 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
85 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
86 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
87 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
88 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
89 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
91 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
92 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
94 * The views and conclusions contained in the software and documentation
95 * are those of the authors and should not be interpreted as representing
96 * official policies, either expressed or implied, of the US Naval
97 * Research Laboratory (NRL).
100 #include "opt_inet6.h"
101 #include "opt_ipsec.h"
102 #include "opt_tcpdebug.h"
103 #include "opt_tcp_input.h"
104 #include "opt_tcp_sack.h"
106 #include <sys/param.h>
107 #include <sys/systm.h>
108 #include <sys/kernel.h>
109 #include <sys/sysctl.h>
110 #include <sys/malloc.h>
111 #include <sys/mbuf.h>
112 #include <sys/proc.h> /* for proc0 declaration */
113 #include <sys/protosw.h>
114 #include <sys/socket.h>
115 #include <sys/socketvar.h>
116 #include <sys/syslog.h>
117 #include <sys/systm.h>
119 #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
124 #include <net/route.h>
126 #include <netinet/in.h>
127 #include <netinet/in_systm.h>
128 #include <netinet/ip.h>
129 #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */
130 #include <netinet/in_var.h>
131 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
132 #include <netinet/in_pcb.h>
133 #include <netinet/ip_var.h>
134 #include <netinet/ip6.h>
135 #include <netinet/icmp6.h>
136 #include <netinet6/nd6.h>
137 #include <netinet6/ip6_var.h>
138 #include <netinet6/in6_pcb.h>
139 #include <netinet/tcp.h>
140 #include <netinet/tcp_fsm.h>
141 #include <netinet/tcp_seq.h>
142 #include <netinet/tcp_timer.h>
143 #include <netinet/tcp_var.h>
144 #include <netinet6/tcp6_var.h>
145 #include <netinet/tcpip.h>
147 #include <netinet/tcp_debug.h>
148 #endif /* TCPDEBUG */
151 #include <netipsec/ipsec.h>
152 #include <netipsec/ipsec6.h>
156 #include <netinet6/ipsec.h>
157 #include <netinet6/ipsec6.h>
158 #include <netkey/key.h>
160 #include <machine/in_cksum.h>
162 extern struct uma_zone *sack_hole_zone;
164 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, 0, "TCP SACK");
166 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_RW,
167 &tcp_do_sack, 0, "Enable/Disable TCP SACK support");
168 TUNABLE_INT("net.inet.tcp.sack.enable", &tcp_do_sack);
170 static int tcp_sack_maxholes = 128;
171 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_RW,
172 &tcp_sack_maxholes, 0,
173 "Maximum number of TCP SACK holes allowed per connection");
175 static int tcp_sack_globalmaxholes = 65536;
176 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_RW,
177 &tcp_sack_globalmaxholes, 0,
178 "Global maximum number of TCP SACK holes");
180 static int tcp_sack_globalholes = 0;
181 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_RD,
182 &tcp_sack_globalholes, 0,
183 "Global number of TCP SACK holes currently allocated");
186 * This function is called upon receipt of new valid data (while not in header
187 * prediction mode), and it updates the ordered list of sacks.
190 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
193 * First reported block MUST be the most recent one. Subsequent
194 * blocks SHOULD be in the order in which they arrived at the
195 * receiver. These two conditions make the implementation fully
196 * compliant with RFC 2018.
198 struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
199 int num_head, num_saved, i;
201 INP_LOCK_ASSERT(tp->t_inpcb);
203 /* Check arguments */
204 KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end"));
206 /* SACK block for the received segment. */
207 head_blk.start = rcv_start;
208 head_blk.end = rcv_end;
211 * Merge updated SACK blocks into head_blk, and
212 * save unchanged SACK blocks into saved_blks[].
213 * num_saved will have the number of the saved SACK blocks.
216 for (i = 0; i < tp->rcv_numsacks; i++) {
217 tcp_seq start = tp->sackblks[i].start;
218 tcp_seq end = tp->sackblks[i].end;
219 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
221 * Discard this SACK block.
223 } else if (SEQ_LEQ(head_blk.start, end) &&
224 SEQ_GEQ(head_blk.end, start)) {
226 * Merge this SACK block into head_blk.
227 * This SACK block itself will be discarded.
229 if (SEQ_GT(head_blk.start, start))
230 head_blk.start = start;
231 if (SEQ_LT(head_blk.end, end))
235 * Save this SACK block.
237 saved_blks[num_saved].start = start;
238 saved_blks[num_saved].end = end;
244 * Update SACK list in tp->sackblks[].
247 if (SEQ_GT(head_blk.start, tp->rcv_nxt)) {
249 * The received data segment is an out-of-order segment.
250 * Put head_blk at the top of SACK list.
252 tp->sackblks[0] = head_blk;
255 * If the number of saved SACK blocks exceeds its limit,
256 * discard the last SACK block.
258 if (num_saved >= MAX_SACK_BLKS)
263 * Copy the saved SACK blocks back.
265 bcopy(saved_blks, &tp->sackblks[num_head],
266 sizeof(struct sackblk) * num_saved);
269 /* Save the number of SACK blocks. */
270 tp->rcv_numsacks = num_head + num_saved;
274 * Delete all receiver-side SACK information.
277 tcp_clean_sackreport(tp)
282 INP_LOCK_ASSERT(tp->t_inpcb);
283 tp->rcv_numsacks = 0;
284 for (i = 0; i < MAX_SACK_BLKS; i++)
285 tp->sackblks[i].start = tp->sackblks[i].end=0;
289 * Allocate struct sackhole.
291 static struct sackhole *
292 tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
294 struct sackhole *hole;
296 if (tp->snd_numholes >= tcp_sack_maxholes ||
297 tcp_sack_globalholes >= tcp_sack_globalmaxholes) {
298 tcpstat.tcps_sack_sboverflow++;
302 hole = (struct sackhole *)uma_zalloc(sack_hole_zone, M_NOWAIT);
311 tcp_sack_globalholes++;
317 * Free struct sackhole.
320 tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
322 uma_zfree(sack_hole_zone, hole);
325 tcp_sack_globalholes--;
327 KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
328 KASSERT(tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
332 * Insert new SACK hole into scoreboard.
334 static struct sackhole *
335 tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
336 struct sackhole *after)
338 struct sackhole *hole;
340 /* Allocate a new SACK hole. */
341 hole = tcp_sackhole_alloc(tp, start, end);
345 /* Insert the new SACK hole into scoreboard */
347 TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
349 TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
351 /* Update SACK hint. */
352 if (tp->sackhint.nexthole == NULL)
353 tp->sackhint.nexthole = hole;
359 * Remove SACK hole from scoreboard.
362 tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
364 /* Update SACK hint. */
365 if (tp->sackhint.nexthole == hole)
366 tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
368 /* Remove this SACK hole. */
369 TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
371 /* Free this SACK hole. */
372 tcp_sackhole_free(tp, hole);
376 * Process the TCP SACK option. Returns 1 if tcp_dooptions() should continue,
377 * and 0 otherwise, if the option was fine. tp->snd_holes is an ordered list
378 * of holes (oldest to newest, in terms of the sequence space).
381 tcp_sack_option(struct tcpcb *tp, struct tcphdr *th, u_char *cp, int optlen)
385 struct sackhole *cur, *temp;
386 struct sackblk sack, sack_blocks[TCP_MAX_SACK], *sblkp;
387 int i, j, num_sack_blks;
389 INP_LOCK_ASSERT(tp->t_inpcb);
390 if (!tp->sack_enable)
392 if ((th->th_flags & TH_ACK) == 0)
394 /* Note: TCPOLEN_SACK must be 2*sizeof(tcp_seq) */
395 if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
397 /* If ack is outside [snd_una, snd_max], ignore the SACK options */
398 if (SEQ_LT(th->th_ack, tp->snd_una) || SEQ_GT(th->th_ack, tp->snd_max))
401 tmp_olen = optlen - 2;
402 tcpstat.tcps_sack_rcv_blocks++;
404 * Sort the SACK blocks so we can update the scoreboard
405 * with just one pass. The overhead of sorting upto 4 elements
406 * is less than making upto 4 passes over the scoreboard.
409 while (tmp_olen > 0) {
410 bcopy(tmp_cp, &sack, sizeof(sack));
411 sack.start = ntohl(sack.start);
412 sack.end = ntohl(sack.end);
413 if (SEQ_GT(sack.end, sack.start) &&
414 SEQ_GT(sack.start, tp->snd_una) &&
415 SEQ_GT(sack.start, th->th_ack) &&
416 SEQ_LEQ(sack.end, tp->snd_max))
417 sack_blocks[num_sack_blks++] = sack;
418 tmp_olen -= TCPOLEN_SACK;
419 tmp_cp += TCPOLEN_SACK;
421 if (num_sack_blks == 0)
424 for (i = 0; i < num_sack_blks; i++) {
425 for (j = i + 1; j < num_sack_blks; j++) {
426 if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
427 sack = sack_blocks[i];
428 sack_blocks[i] = sack_blocks[j];
429 sack_blocks[j] = sack;
433 if (TAILQ_EMPTY(&tp->snd_holes))
435 * Empty scoreboard. Need to initialize snd_fack (it may be
436 * uninitialized or have a bogus value). Scoreboard holes
437 * (from the sack blocks received) are created later below (in
438 * the logic that adds holes to the tail of the scoreboard).
440 tp->snd_fack = tp->snd_una;
442 * In the while-loop below, incoming SACK blocks (sack_blocks[])
443 * and SACK holes (snd_holes) are traversed from their tails with
444 * just one pass in order to reduce the number of compares especially
445 * when the bandwidth-delay product is large.
446 * Note: Typically, in the first RTT of SACK recovery, the highest
447 * three or four SACK blocks with the same ack number are received.
448 * In the second RTT, if retransmitted data segments are not lost,
449 * the highest three or four SACK blocks with ack number advancing
452 sblkp = &sack_blocks[num_sack_blks - 1]; /* Last SACK block */
453 if (SEQ_LT(tp->snd_fack, sblkp->start)) {
455 * The highest SACK block is beyond fack.
456 * Append new SACK hole at the tail.
457 * If the second or later highest SACK blocks are also
458 * beyond the current fack, they will be inserted by
459 * way of hole splitting in the while-loop below.
461 tcp_sackhole_insert(tp, tp->snd_fack, sblkp->start, NULL);
462 tp->snd_fack = sblkp->end;
463 /* Go to the previous sack block. */
465 } else if (SEQ_LT(tp->snd_fack, sblkp->end))
466 /* fack is advanced. */
467 tp->snd_fack = sblkp->end;
468 cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole */
470 * Since the incoming sack blocks are sorted, we can process them
471 * making one sweep of the scoreboard.
473 while (sblkp - sack_blocks >= 0) {
474 KASSERT(cur != NULL, ("cur != NULL"));
475 if (SEQ_GEQ(sblkp->start, cur->end)) {
477 * SACKs data beyond the current hole.
478 * Go to the previous sack block.
483 if (SEQ_LEQ(sblkp->end, cur->start)) {
485 * SACKs data before the current hole.
486 * Go to the previous hole.
488 cur = TAILQ_PREV(cur, sackhole_head, scblink);
491 tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
492 KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
493 ("sackhint bytes rtx >= 0"));
494 if (SEQ_LEQ(sblkp->start, cur->start)) {
495 /* Data acks at least the beginning of hole */
496 if (SEQ_GEQ(sblkp->end, cur->end)) {
497 /* Acks entire hole, so delete hole */
499 cur = TAILQ_PREV(cur, sackhole_head, scblink);
500 tcp_sackhole_remove(tp, temp);
502 * The sack block may ack all or part of the next
503 * hole too, so continue onto the next hole.
507 /* Move start of hole forward */
508 cur->start = sblkp->end;
509 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
511 /* Go to the previous hole. */
512 cur = TAILQ_PREV(cur, sackhole_head, scblink);
514 /* Data acks at least the end of hole */
515 if (SEQ_GEQ(sblkp->end, cur->end)) {
516 /* Move end of hole backward */
517 cur->end = sblkp->start;
518 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
521 * ACKs some data in middle of a hole; need to
524 temp = tcp_sackhole_insert(tp, sblkp->end,
527 if (SEQ_GT(cur->rxmit, temp->rxmit)) {
528 temp->rxmit = cur->rxmit;
529 tp->sackhint.sack_bytes_rexmit
533 cur->end = sblkp->start;
534 cur->rxmit = SEQ_MIN(cur->rxmit,
538 /* Go to the previous sack block. */
541 tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
547 * Delete stale (i.e, cumulatively ack'd) holes. Hole is deleted only if
548 * it is completely acked; otherwise, tcp_sack_option(), called from
549 * tcp_dooptions(), will fix up the hole.
552 tcp_del_sackholes(tp, th)
556 INP_LOCK_ASSERT(tp->t_inpcb);
557 if (tp->sack_enable && tp->t_state != TCPS_LISTEN) {
558 /* max because this could be an older ack just arrived */
559 tcp_seq lastack = SEQ_GT(th->th_ack, tp->snd_una) ?
560 th->th_ack : tp->snd_una;
561 struct sackhole *cur = TAILQ_FIRST(&tp->snd_holes);
562 struct sackhole *prev;
564 if (SEQ_LEQ(cur->end, lastack)) {
566 cur = TAILQ_NEXT(cur, scblink);
567 tp->sackhint.sack_bytes_rexmit -=
568 (prev->rxmit - prev->start);
569 tcp_sackhole_remove(tp, prev);
570 } else if (SEQ_LT(cur->start, lastack)) {
571 if (SEQ_LT(cur->rxmit, lastack)) {
572 tp->sackhint.sack_bytes_rexmit -=
573 (cur->rxmit - cur->start);
574 cur->rxmit = lastack;
576 tp->sackhint.sack_bytes_rexmit -=
577 (lastack - cur->start);
578 cur->start = lastack;
586 tcp_free_sackholes(struct tcpcb *tp)
590 INP_LOCK_ASSERT(tp->t_inpcb);
591 while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
592 tcp_sackhole_remove(tp, q);
593 tp->sackhint.sack_bytes_rexmit = 0;
595 KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
596 KASSERT(tp->sackhint.nexthole == NULL,
597 ("tp->sackhint.nexthole == NULL"));
601 * Partial ack handling within a sack recovery episode.
602 * Keeping this very simple for now. When a partial ack
603 * is received, force snd_cwnd to a value that will allow
604 * the sender to transmit no more than 2 segments.
605 * If necessary, a better scheme can be adopted at a
606 * later point, but for now, the goal is to prevent the
607 * sender from bursting a large amount of data in the midst
611 tcp_sack_partialack(tp, th)
617 INP_LOCK_ASSERT(tp->t_inpcb);
618 callout_stop(tp->tt_rexmt);
620 /* send one or 2 segments based on how much new data was acked */
621 if (((th->th_ack - tp->snd_una) / tp->t_maxseg) > 2)
623 tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
624 (tp->snd_nxt - tp->sack_newdata) +
625 num_segs * tp->t_maxseg);
626 if (tp->snd_cwnd > tp->snd_ssthresh)
627 tp->snd_cwnd = tp->snd_ssthresh;
628 tp->t_flags |= TF_ACKNOW;
629 (void) tcp_output(tp);
633 * Debug version of tcp_sack_output() that walks the scoreboard. Used for
634 * now to sanity check the hint.
636 static struct sackhole *
637 tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
641 INP_LOCK_ASSERT(tp->t_inpcb);
642 *sack_bytes_rexmt = 0;
643 TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
644 if (SEQ_LT(p->rxmit, p->end)) {
645 if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
648 *sack_bytes_rexmt += (p->rxmit - p->start);
651 *sack_bytes_rexmt += (p->rxmit - p->start);
657 * Returns the next hole to retransmit and the number of retransmitted bytes
658 * from the scoreboard. We store both the next hole and the number of
659 * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
660 * reception). This avoids scoreboard traversals completely.
662 * The loop here will traverse *at most* one link. Here's the argument.
663 * For the loop to traverse more than 1 link before finding the next hole to
664 * retransmit, we would need to have at least 1 node following the current hint
665 * with (rxmit == end). But, for all holes following the current hint,
666 * (start == rxmit), since we have not yet retransmitted from them. Therefore,
667 * in order to traverse more 1 link in the loop below, we need to have at least
668 * one node following the current hint with (start == rxmit == end).
669 * But that can't happen, (start == end) means that all the data in that hole
670 * has been sacked, in which case, the hole would have been removed from the
674 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
676 struct sackhole *hole = NULL, *dbg_hole = NULL;
679 INP_LOCK_ASSERT(tp->t_inpcb);
680 dbg_hole = tcp_sack_output_debug(tp, &dbg_bytes_rexmt);
681 *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
682 hole = tp->sackhint.nexthole;
683 if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
685 while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
686 if (SEQ_LT(hole->rxmit, hole->end)) {
687 tp->sackhint.nexthole = hole;
692 if (dbg_hole != hole) {
693 printf("%s: Computed sack hole not the same as cached value\n", __func__);
696 if (*sack_bytes_rexmt != dbg_bytes_rexmt) {
697 printf("%s: Computed sack_bytes_retransmitted (%d) not "
698 "the same as cached value (%d)\n",
699 __func__, dbg_bytes_rexmt, *sack_bytes_rexmt);
700 *sack_bytes_rexmt = dbg_bytes_rexmt;
706 * After a timeout, the SACK list may be rebuilt. This SACK information
707 * should be used to avoid retransmitting SACKed data. This function
708 * traverses the SACK list to see if snd_nxt should be moved forward.
711 tcp_sack_adjust(struct tcpcb *tp)
713 struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
715 INP_LOCK_ASSERT(tp->t_inpcb);
717 return; /* No holes */
718 if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack))
719 return; /* We're already beyond any SACKed blocks */
721 * Two cases for which we want to advance snd_nxt:
722 * i) snd_nxt lies between end of one hole and beginning of another
723 * ii) snd_nxt lies between end of last hole and snd_fack
725 while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
726 if (SEQ_LT(tp->snd_nxt, cur->end))
728 if (SEQ_GEQ(tp->snd_nxt, p->start))
731 tp->snd_nxt = p->start;
735 if (SEQ_LT(tp->snd_nxt, cur->end))
737 tp->snd_nxt = tp->snd_fack;