]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/tcp_sack.c
Remove spurious newline
[FreeBSD/FreeBSD.git] / sys / netinet / tcp_sack.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
5  *      The Regents of the University of California.
6  * All rights reserved.
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 University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      @(#)tcp_sack.c  8.12 (Berkeley) 5/24/95
33  */
34
35 /*-
36  *      @@(#)COPYRIGHT  1.1 (NRL) 17 January 1995
37  *
38  * NRL grants permission for redistribution and use in source and binary
39  * forms, with or without modification, of the software and documentation
40  * created at NRL provided that the following conditions are met:
41  *
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgements:
49  *      This product includes software developed by the University of
50  *      California, Berkeley and its contributors.
51  *      This product includes software developed at the Information
52  *      Technology Division, US Naval Research Laboratory.
53  * 4. Neither the name of the NRL nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
58  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
59  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
60  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
61  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
62  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
63  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
64  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
65  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
66  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
67  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68  *
69  * The views and conclusions contained in the software and documentation
70  * are those of the authors and should not be interpreted as representing
71  * official policies, either expressed or implied, of the US Naval
72  * Research Laboratory (NRL).
73  */
74
75 #include <sys/cdefs.h>
76 __FBSDID("$FreeBSD$");
77
78 #include "opt_inet.h"
79 #include "opt_inet6.h"
80 #include "opt_tcpdebug.h"
81
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.h>
85 #include <sys/sysctl.h>
86 #include <sys/malloc.h>
87 #include <sys/mbuf.h>
88 #include <sys/proc.h>           /* for proc0 declaration */
89 #include <sys/protosw.h>
90 #include <sys/socket.h>
91 #include <sys/socketvar.h>
92 #include <sys/syslog.h>
93 #include <sys/systm.h>
94
95 #include <machine/cpu.h>        /* before tcp_seq.h, for tcp_random18() */
96
97 #include <vm/uma.h>
98
99 #include <net/if.h>
100 #include <net/if_var.h>
101 #include <net/route.h>
102 #include <net/vnet.h>
103
104 #include <netinet/in.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/ip.h>
107 #include <netinet/in_var.h>
108 #include <netinet/in_pcb.h>
109 #include <netinet/ip_var.h>
110 #include <netinet/ip6.h>
111 #include <netinet/icmp6.h>
112 #include <netinet6/nd6.h>
113 #include <netinet6/ip6_var.h>
114 #include <netinet6/in6_pcb.h>
115 #include <netinet/tcp.h>
116 #include <netinet/tcp_fsm.h>
117 #include <netinet/tcp_seq.h>
118 #include <netinet/tcp_timer.h>
119 #include <netinet/tcp_var.h>
120 #include <netinet6/tcp6_var.h>
121 #include <netinet/tcpip.h>
122 #ifdef TCPDEBUG
123 #include <netinet/tcp_debug.h>
124 #endif /* TCPDEBUG */
125
126 #include <machine/in_cksum.h>
127
128 VNET_DECLARE(struct uma_zone *, sack_hole_zone);
129 #define V_sack_hole_zone                VNET(sack_hole_zone)
130
131 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, 0, "TCP SACK");
132 VNET_DEFINE(int, tcp_do_sack) = 1;
133 #define V_tcp_do_sack                   VNET(tcp_do_sack)
134 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
135     &VNET_NAME(tcp_do_sack), 0, "Enable/Disable TCP SACK support");
136
137 VNET_DEFINE(int, tcp_sack_maxholes) = 128;
138 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_VNET | CTLFLAG_RW,
139     &VNET_NAME(tcp_sack_maxholes), 0,
140     "Maximum number of TCP SACK holes allowed per connection");
141
142 VNET_DEFINE(int, tcp_sack_globalmaxholes) = 65536;
143 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_VNET | CTLFLAG_RW,
144     &VNET_NAME(tcp_sack_globalmaxholes), 0, 
145     "Global maximum number of TCP SACK holes");
146
147 VNET_DEFINE(int, tcp_sack_globalholes) = 0;
148 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_VNET | CTLFLAG_RD,
149     &VNET_NAME(tcp_sack_globalholes), 0,
150     "Global number of TCP SACK holes currently allocated");
151
152 /*
153  * This function is called upon receipt of new valid data (while not in
154  * header prediction mode), and it updates the ordered list of sacks.
155  */
156 void
157 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
158 {
159         /*
160          * First reported block MUST be the most recent one.  Subsequent
161          * blocks SHOULD be in the order in which they arrived at the
162          * receiver.  These two conditions make the implementation fully
163          * compliant with RFC 2018.
164          */
165         struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
166         int num_head, num_saved, i;
167
168         INP_WLOCK_ASSERT(tp->t_inpcb);
169
170         /* Check arguments. */
171         KASSERT(SEQ_LEQ(rcv_start, rcv_end), ("rcv_start <= rcv_end"));
172
173         /* SACK block for the received segment. */
174         head_blk.start = rcv_start;
175         head_blk.end = rcv_end;
176
177         /*
178          * Merge updated SACK blocks into head_blk, and save unchanged SACK
179          * blocks into saved_blks[].  num_saved will have the number of the
180          * saved SACK blocks.
181          */
182         num_saved = 0;
183         for (i = 0; i < tp->rcv_numsacks; i++) {
184                 tcp_seq start = tp->sackblks[i].start;
185                 tcp_seq end = tp->sackblks[i].end;
186                 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
187                         /*
188                          * Discard this SACK block.
189                          */
190                 } else if (SEQ_LEQ(head_blk.start, end) &&
191                            SEQ_GEQ(head_blk.end, start)) {
192                         /*
193                          * Merge this SACK block into head_blk.  This SACK
194                          * block itself will be discarded.
195                          */
196                         /*
197                          * |-|
198                          *   |---|  merge
199                          *
200                          *     |-|
201                          * |---|    merge
202                          *
203                          * |-----|
204                          *   |-|    DSACK smaller
205                          *
206                          *   |-|
207                          * |-----|  DSACK smaller
208                          */
209                         if (head_blk.start == end)
210                                 head_blk.start = start;
211                         else if (head_blk.end == start)
212                                 head_blk.end = end;
213                         else {
214                                 if (SEQ_LT(head_blk.start, start)) {
215                                         tcp_seq temp = start;
216                                         start = head_blk.start;
217                                         head_blk.start = temp;
218                                 }
219                                 if (SEQ_GT(head_blk.end, end)) {
220                                         tcp_seq temp = end;
221                                         end = head_blk.end;
222                                         head_blk.end = temp;
223                                 }
224                                 if ((head_blk.start != start) ||
225                                     (head_blk.end != end)) {
226                                         if ((num_saved >= 1) &&
227                                            SEQ_GEQ(saved_blks[num_saved-1].start, start) &&
228                                            SEQ_LEQ(saved_blks[num_saved-1].end, end))
229                                                 num_saved--;
230                                         saved_blks[num_saved].start = start;
231                                         saved_blks[num_saved].end = end;
232                                         num_saved++;
233                                 }
234                         }
235                 } else {
236                         /*
237                          * This block supercedes the prior block
238                          */
239                         if ((num_saved >= 1) &&
240                            SEQ_GEQ(saved_blks[num_saved-1].start, start) &&
241                            SEQ_LEQ(saved_blks[num_saved-1].end, end))
242                                 num_saved--;
243                         /*
244                          * Save this SACK block.
245                          */
246                         saved_blks[num_saved].start = start;
247                         saved_blks[num_saved].end = end;
248                         num_saved++;
249                 }
250         }
251
252         /*
253          * Update SACK list in tp->sackblks[].
254          */
255         num_head = 0;
256         if (SEQ_LT(rcv_start, rcv_end)) {
257                 /*
258                  * The received data segment is an out-of-order segment.  Put
259                  * head_blk at the top of SACK list.
260                  */
261                 tp->sackblks[0] = head_blk;
262                 num_head = 1;
263                 /*
264                  * If the number of saved SACK blocks exceeds its limit,
265                  * discard the last SACK block.
266                  */
267                 if (num_saved >= MAX_SACK_BLKS)
268                         num_saved--;
269         }
270         if (num_saved > 0) {
271                 /*
272                  * Copy the saved SACK blocks back.
273                  */
274                 bcopy(saved_blks, &tp->sackblks[num_head],
275                       sizeof(struct sackblk) * num_saved);
276         }
277
278         /* Save the number of SACK blocks. */
279         tp->rcv_numsacks = num_head + num_saved;
280 }
281
282 /*
283  * Delete all receiver-side SACK information.
284  */
285 void
286 tcp_clean_sackreport(struct tcpcb *tp)
287 {
288         int i;
289
290         INP_WLOCK_ASSERT(tp->t_inpcb);
291         tp->rcv_numsacks = 0;
292         for (i = 0; i < MAX_SACK_BLKS; i++)
293                 tp->sackblks[i].start = tp->sackblks[i].end=0;
294 }
295
296 /*
297  * Allocate struct sackhole.
298  */
299 static struct sackhole *
300 tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
301 {
302         struct sackhole *hole;
303
304         if (tp->snd_numholes >= V_tcp_sack_maxholes ||
305             V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) {
306                 TCPSTAT_INC(tcps_sack_sboverflow);
307                 return NULL;
308         }
309
310         hole = (struct sackhole *)uma_zalloc(V_sack_hole_zone, M_NOWAIT);
311         if (hole == NULL)
312                 return NULL;
313
314         hole->start = start;
315         hole->end = end;
316         hole->rxmit = start;
317
318         tp->snd_numholes++;
319         atomic_add_int(&V_tcp_sack_globalholes, 1);
320
321         return hole;
322 }
323
324 /*
325  * Free struct sackhole.
326  */
327 static void
328 tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
329 {
330
331         uma_zfree(V_sack_hole_zone, hole);
332
333         tp->snd_numholes--;
334         atomic_subtract_int(&V_tcp_sack_globalholes, 1);
335
336         KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
337         KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
338 }
339
340 /*
341  * Insert new SACK hole into scoreboard.
342  */
343 static struct sackhole *
344 tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
345     struct sackhole *after)
346 {
347         struct sackhole *hole;
348
349         /* Allocate a new SACK hole. */
350         hole = tcp_sackhole_alloc(tp, start, end);
351         if (hole == NULL)
352                 return NULL;
353
354         /* Insert the new SACK hole into scoreboard. */
355         if (after != NULL)
356                 TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
357         else
358                 TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
359
360         /* Update SACK hint. */
361         if (tp->sackhint.nexthole == NULL)
362                 tp->sackhint.nexthole = hole;
363
364         return hole;
365 }
366
367 /*
368  * Remove SACK hole from scoreboard.
369  */
370 static void
371 tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
372 {
373
374         /* Update SACK hint. */
375         if (tp->sackhint.nexthole == hole)
376                 tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
377
378         /* Remove this SACK hole. */
379         TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
380
381         /* Free this SACK hole. */
382         tcp_sackhole_free(tp, hole);
383 }
384
385 /*
386  * Process cumulative ACK and the TCP SACK option to update the scoreboard.
387  * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
388  * the sequence space).
389  * Returns 1 if incoming ACK has previously unknown SACK information,
390  * 0 otherwise. Note: We treat (snd_una, th_ack) as a sack block so any changes
391  * to that (i.e. left edge moving) would also be considered a change in SACK
392  * information which is slightly different than rfc6675.
393  */
394 int
395 tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack)
396 {
397         struct sackhole *cur, *temp;
398         struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp;
399         int i, j, num_sack_blks, sack_changed;
400
401         INP_WLOCK_ASSERT(tp->t_inpcb);
402
403         num_sack_blks = 0;
404         sack_changed = 0;
405         /*
406          * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
407          * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
408          */
409         if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
410                 sack_blocks[num_sack_blks].start = tp->snd_una;
411                 sack_blocks[num_sack_blks++].end = th_ack;
412         }
413         /*
414          * Append received valid SACK blocks to sack_blocks[], but only if we
415          * received new blocks from the other side.
416          */
417         if (to->to_flags & TOF_SACK) {
418                 tp->sackhint.sacked_bytes = 0;  /* reset */
419                 for (i = 0; i < to->to_nsacks; i++) {
420                         bcopy((to->to_sacks + i * TCPOLEN_SACK),
421                             &sack, sizeof(sack));
422                         sack.start = ntohl(sack.start);
423                         sack.end = ntohl(sack.end);
424                         if (SEQ_GT(sack.end, sack.start) &&
425                             SEQ_GT(sack.start, tp->snd_una) &&
426                             SEQ_GT(sack.start, th_ack) &&
427                             SEQ_LT(sack.start, tp->snd_max) &&
428                             SEQ_GT(sack.end, tp->snd_una) &&
429                             SEQ_LEQ(sack.end, tp->snd_max)) {
430                                 sack_blocks[num_sack_blks++] = sack;
431                                 tp->sackhint.sacked_bytes +=
432                                     (sack.end-sack.start);
433                         }
434                 }
435         }
436         /*
437          * Return if SND.UNA is not advanced and no valid SACK block is
438          * received.
439          */
440         if (num_sack_blks == 0)
441                 return (sack_changed);
442
443         /*
444          * Sort the SACK blocks so we can update the scoreboard with just one
445          * pass. The overhead of sorting up to 4+1 elements is less than
446          * making up to 4+1 passes over the scoreboard.
447          */
448         for (i = 0; i < num_sack_blks; i++) {
449                 for (j = i + 1; j < num_sack_blks; j++) {
450                         if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
451                                 sack = sack_blocks[i];
452                                 sack_blocks[i] = sack_blocks[j];
453                                 sack_blocks[j] = sack;
454                         }
455                 }
456         }
457         if (TAILQ_EMPTY(&tp->snd_holes))
458                 /*
459                  * Empty scoreboard. Need to initialize snd_fack (it may be
460                  * uninitialized or have a bogus value). Scoreboard holes
461                  * (from the sack blocks received) are created later below
462                  * (in the logic that adds holes to the tail of the
463                  * scoreboard).
464                  */
465                 tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
466         /*
467          * In the while-loop below, incoming SACK blocks (sack_blocks[]) and
468          * SACK holes (snd_holes) are traversed from their tails with just
469          * one pass in order to reduce the number of compares especially when
470          * the bandwidth-delay product is large.
471          *
472          * Note: Typically, in the first RTT of SACK recovery, the highest
473          * three or four SACK blocks with the same ack number are received.
474          * In the second RTT, if retransmitted data segments are not lost,
475          * the highest three or four SACK blocks with ack number advancing
476          * are received.
477          */
478         sblkp = &sack_blocks[num_sack_blks - 1];        /* Last SACK block */
479         tp->sackhint.last_sack_ack = sblkp->end;
480         if (SEQ_LT(tp->snd_fack, sblkp->start)) {
481                 /*
482                  * The highest SACK block is beyond fack.  Append new SACK
483                  * hole at the tail.  If the second or later highest SACK
484                  * blocks are also beyond the current fack, they will be
485                  * inserted by way of hole splitting in the while-loop below.
486                  */
487                 temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL);
488                 if (temp != NULL) {
489                         tp->snd_fack = sblkp->end;
490                         /* Go to the previous sack block. */
491                         sblkp--;
492                         sack_changed = 1;
493                 } else {
494                         /* 
495                          * We failed to add a new hole based on the current 
496                          * sack block.  Skip over all the sack blocks that 
497                          * fall completely to the right of snd_fack and
498                          * proceed to trim the scoreboard based on the
499                          * remaining sack blocks.  This also trims the
500                          * scoreboard for th_ack (which is sack_blocks[0]).
501                          */
502                         while (sblkp >= sack_blocks && 
503                                SEQ_LT(tp->snd_fack, sblkp->start))
504                                 sblkp--;
505                         if (sblkp >= sack_blocks && 
506                             SEQ_LT(tp->snd_fack, sblkp->end))
507                                 tp->snd_fack = sblkp->end;
508                 }
509         } else if (SEQ_LT(tp->snd_fack, sblkp->end)) {
510                 /* fack is advanced. */
511                 tp->snd_fack = sblkp->end;
512                 sack_changed = 1;
513         }
514         cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */
515         /*
516          * Since the incoming sack blocks are sorted, we can process them
517          * making one sweep of the scoreboard.
518          */
519         while (sblkp >= sack_blocks  && cur != NULL) {
520                 if (SEQ_GEQ(sblkp->start, cur->end)) {
521                         /*
522                          * SACKs data beyond the current hole.  Go to the
523                          * previous sack block.
524                          */
525                         sblkp--;
526                         continue;
527                 }
528                 if (SEQ_LEQ(sblkp->end, cur->start)) {
529                         /*
530                          * SACKs data before the current hole.  Go to the
531                          * previous hole.
532                          */
533                         cur = TAILQ_PREV(cur, sackhole_head, scblink);
534                         continue;
535                 }
536                 tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
537                 KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
538                     ("sackhint bytes rtx >= 0"));
539                 sack_changed = 1;
540                 if (SEQ_LEQ(sblkp->start, cur->start)) {
541                         /* Data acks at least the beginning of hole. */
542                         if (SEQ_GEQ(sblkp->end, cur->end)) {
543                                 /* Acks entire hole, so delete hole. */
544                                 temp = cur;
545                                 cur = TAILQ_PREV(cur, sackhole_head, scblink);
546                                 tcp_sackhole_remove(tp, temp);
547                                 /*
548                                  * The sack block may ack all or part of the
549                                  * next hole too, so continue onto the next
550                                  * hole.
551                                  */
552                                 continue;
553                         } else {
554                                 /* Move start of hole forward. */
555                                 cur->start = sblkp->end;
556                                 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
557                         }
558                 } else {
559                         /* Data acks at least the end of hole. */
560                         if (SEQ_GEQ(sblkp->end, cur->end)) {
561                                 /* Move end of hole backward. */
562                                 cur->end = sblkp->start;
563                                 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
564                         } else {
565                                 /*
566                                  * ACKs some data in middle of a hole; need
567                                  * to split current hole
568                                  */
569                                 temp = tcp_sackhole_insert(tp, sblkp->end,
570                                     cur->end, cur);
571                                 if (temp != NULL) {
572                                         if (SEQ_GT(cur->rxmit, temp->rxmit)) {
573                                                 temp->rxmit = cur->rxmit;
574                                                 tp->sackhint.sack_bytes_rexmit
575                                                     += (temp->rxmit
576                                                     - temp->start);
577                                         }
578                                         cur->end = sblkp->start;
579                                         cur->rxmit = SEQ_MIN(cur->rxmit,
580                                             cur->end);
581                                 }
582                         }
583                 }
584                 tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
585                 /*
586                  * Testing sblkp->start against cur->start tells us whether
587                  * we're done with the sack block or the sack hole.
588                  * Accordingly, we advance one or the other.
589                  */
590                 if (SEQ_LEQ(sblkp->start, cur->start))
591                         cur = TAILQ_PREV(cur, sackhole_head, scblink);
592                 else
593                         sblkp--;
594         }
595         return (sack_changed);
596 }
597
598 /*
599  * Free all SACK holes to clear the scoreboard.
600  */
601 void
602 tcp_free_sackholes(struct tcpcb *tp)
603 {
604         struct sackhole *q;
605
606         INP_WLOCK_ASSERT(tp->t_inpcb);
607         while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
608                 tcp_sackhole_remove(tp, q);
609         tp->sackhint.sack_bytes_rexmit = 0;
610
611         KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
612         KASSERT(tp->sackhint.nexthole == NULL,
613                 ("tp->sackhint.nexthole == NULL"));
614 }
615
616 /*
617  * Partial ack handling within a sack recovery episode.  Keeping this very
618  * simple for now.  When a partial ack is received, force snd_cwnd to a value
619  * that will allow the sender to transmit no more than 2 segments.  If
620  * necessary, a better scheme can be adopted at a later point, but for now,
621  * the goal is to prevent the sender from bursting a large amount of data in
622  * the midst of sack recovery.
623  */
624 void
625 tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th)
626 {
627         int num_segs = 1;
628
629         INP_WLOCK_ASSERT(tp->t_inpcb);
630         tcp_timer_activate(tp, TT_REXMT, 0);
631         tp->t_rtttime = 0;
632         /* Send one or 2 segments based on how much new data was acked. */
633         if ((BYTES_THIS_ACK(tp, th) / tp->t_maxseg) >= 2)
634                 num_segs = 2;
635         tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
636             (tp->snd_nxt - tp->sack_newdata) + num_segs * tp->t_maxseg);
637         if (tp->snd_cwnd > tp->snd_ssthresh)
638                 tp->snd_cwnd = tp->snd_ssthresh;
639         tp->t_flags |= TF_ACKNOW;
640         (void) tp->t_fb->tfb_tcp_output(tp);
641 }
642
643 #if 0
644 /*
645  * Debug version of tcp_sack_output() that walks the scoreboard.  Used for
646  * now to sanity check the hint.
647  */
648 static struct sackhole *
649 tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
650 {
651         struct sackhole *p;
652
653         INP_WLOCK_ASSERT(tp->t_inpcb);
654         *sack_bytes_rexmt = 0;
655         TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
656                 if (SEQ_LT(p->rxmit, p->end)) {
657                         if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
658                                 continue;
659                         }
660                         *sack_bytes_rexmt += (p->rxmit - p->start);
661                         break;
662                 }
663                 *sack_bytes_rexmt += (p->rxmit - p->start);
664         }
665         return (p);
666 }
667 #endif
668
669 /*
670  * Returns the next hole to retransmit and the number of retransmitted bytes
671  * from the scoreboard.  We store both the next hole and the number of
672  * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
673  * reception).  This avoids scoreboard traversals completely.
674  *
675  * The loop here will traverse *at most* one link.  Here's the argument.  For
676  * the loop to traverse more than 1 link before finding the next hole to
677  * retransmit, we would need to have at least 1 node following the current
678  * hint with (rxmit == end).  But, for all holes following the current hint,
679  * (start == rxmit), since we have not yet retransmitted from them.
680  * Therefore, in order to traverse more 1 link in the loop below, we need to
681  * have at least one node following the current hint with (start == rxmit ==
682  * end).  But that can't happen, (start == end) means that all the data in
683  * that hole has been sacked, in which case, the hole would have been removed
684  * from the scoreboard.
685  */
686 struct sackhole *
687 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
688 {
689         struct sackhole *hole = NULL;
690
691         INP_WLOCK_ASSERT(tp->t_inpcb);
692         *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
693         hole = tp->sackhint.nexthole;
694         if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
695                 goto out;
696         while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
697                 if (SEQ_LT(hole->rxmit, hole->end)) {
698                         tp->sackhint.nexthole = hole;
699                         break;
700                 }
701         }
702 out:
703         return (hole);
704 }
705
706 /*
707  * After a timeout, the SACK list may be rebuilt.  This SACK information
708  * should be used to avoid retransmitting SACKed data.  This function
709  * traverses the SACK list to see if snd_nxt should be moved forward.
710  */
711 void
712 tcp_sack_adjust(struct tcpcb *tp)
713 {
714         struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
715
716         INP_WLOCK_ASSERT(tp->t_inpcb);
717         if (cur == NULL)
718                 return; /* No holes */
719         if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack))
720                 return; /* We're already beyond any SACKed blocks */
721         /*-
722          * Two cases for which we want to advance snd_nxt:
723          * i) snd_nxt lies between end of one hole and beginning of another
724          * ii) snd_nxt lies between end of last hole and snd_fack
725          */
726         while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
727                 if (SEQ_LT(tp->snd_nxt, cur->end))
728                         return;
729                 if (SEQ_GEQ(tp->snd_nxt, p->start))
730                         cur = p;
731                 else {
732                         tp->snd_nxt = p->start;
733                         return;
734                 }
735         }
736         if (SEQ_LT(tp->snd_nxt, cur->end))
737                 return;
738         tp->snd_nxt = tp->snd_fack;
739 }