]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/tcp_sack.c
This commit was generated by cvs2svn to compensate for changes in r150765,
[FreeBSD/FreeBSD.git] / sys / netinet / tcp_sack.c
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  *      @(#)tcp_sack.c  8.12 (Berkeley) 5/24/95
30  * $FreeBSD$
31  */
32
33 /*-
34  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
35  *      The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
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.
48  *
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
59  * SUCH DAMAGE.
60  *
61  *      @@(#)COPYRIGHT  1.1 (NRL) 17 January 1995
62  *
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:
66  *
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.
81  *
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.
93  *
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).
98  */
99 #include "opt_inet.h"
100 #include "opt_inet6.h"
101 #include "opt_tcpdebug.h"
102 #include "opt_tcp_input.h"
103 #include "opt_tcp_sack.h"
104
105 #include <sys/param.h>
106 #include <sys/systm.h>
107 #include <sys/kernel.h>
108 #include <sys/sysctl.h>
109 #include <sys/malloc.h>
110 #include <sys/mbuf.h>
111 #include <sys/proc.h>           /* for proc0 declaration */
112 #include <sys/protosw.h>
113 #include <sys/socket.h>
114 #include <sys/socketvar.h>
115 #include <sys/syslog.h>
116 #include <sys/systm.h>
117
118 #include <machine/cpu.h>        /* before tcp_seq.h, for tcp_random18() */
119
120 #include <vm/uma.h>
121
122 #include <net/if.h>
123 #include <net/route.h>
124
125 #include <netinet/in.h>
126 #include <netinet/in_systm.h>
127 #include <netinet/ip.h>
128 #include <netinet/ip_icmp.h>    /* for ICMP_BANDLIM             */
129 #include <netinet/in_var.h>
130 #include <netinet/icmp_var.h>   /* for ICMP_BANDLIM             */
131 #include <netinet/in_pcb.h>
132 #include <netinet/ip_var.h>
133 #include <netinet/ip6.h>
134 #include <netinet/icmp6.h>
135 #include <netinet6/nd6.h>
136 #include <netinet6/ip6_var.h>
137 #include <netinet6/in6_pcb.h>
138 #include <netinet/tcp.h>
139 #include <netinet/tcp_fsm.h>
140 #include <netinet/tcp_seq.h>
141 #include <netinet/tcp_timer.h>
142 #include <netinet/tcp_var.h>
143 #include <netinet6/tcp6_var.h>
144 #include <netinet/tcpip.h>
145 #ifdef TCPDEBUG
146 #include <netinet/tcp_debug.h>
147 #endif /* TCPDEBUG */
148
149 #include <machine/in_cksum.h>
150
151 extern struct uma_zone *sack_hole_zone;
152
153 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, 0, "TCP SACK");
154 int tcp_do_sack = 1;
155 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_RW,
156         &tcp_do_sack, 0, "Enable/Disable TCP SACK support");
157 TUNABLE_INT("net.inet.tcp.sack.enable", &tcp_do_sack);
158
159 static int tcp_sack_maxholes = 128;
160 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_RW,
161         &tcp_sack_maxholes, 0, 
162     "Maximum number of TCP SACK holes allowed per connection");
163
164 static int tcp_sack_globalmaxholes = 65536;
165 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_RW,
166         &tcp_sack_globalmaxholes, 0, 
167     "Global maximum number of TCP SACK holes");
168
169 static int tcp_sack_globalholes = 0;
170 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_RD,
171     &tcp_sack_globalholes, 0,
172     "Global number of TCP SACK holes currently allocated");
173
174 /*
175  * This function is called upon receipt of new valid data (while not in header
176  * prediction mode), and it updates the ordered list of sacks.
177  */
178 void
179 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
180 {
181         /*
182          * First reported block MUST be the most recent one.  Subsequent
183          * blocks SHOULD be in the order in which they arrived at the
184          * receiver.  These two conditions make the implementation fully
185          * compliant with RFC 2018.
186          */
187         struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
188         int num_head, num_saved, i;
189
190         INP_LOCK_ASSERT(tp->t_inpcb);
191
192         /* Check arguments */
193         KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end"));
194
195         /* SACK block for the received segment. */
196         head_blk.start = rcv_start;
197         head_blk.end = rcv_end;
198
199         /*
200          * Merge updated SACK blocks into head_blk, and
201          * save unchanged SACK blocks into saved_blks[].
202          * num_saved will have the number of the saved SACK blocks.
203          */
204         num_saved = 0;
205         for (i = 0; i < tp->rcv_numsacks; i++) {
206                 tcp_seq start = tp->sackblks[i].start;
207                 tcp_seq end = tp->sackblks[i].end;
208                 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
209                         /*
210                          * Discard this SACK block.
211                          */
212                 } else if (SEQ_LEQ(head_blk.start, end) &&
213                            SEQ_GEQ(head_blk.end, start)) {
214                         /*
215                          * Merge this SACK block into head_blk.
216                          * This SACK block itself will be discarded.
217                          */
218                         if (SEQ_GT(head_blk.start, start))
219                                 head_blk.start = start;
220                         if (SEQ_LT(head_blk.end, end))
221                                 head_blk.end = end;
222                 } else {
223                         /*
224                          * Save this SACK block.
225                          */
226                         saved_blks[num_saved].start = start;
227                         saved_blks[num_saved].end = end;
228                         num_saved++;
229                 }
230         }
231
232         /*
233          * Update SACK list in tp->sackblks[].
234          */
235         num_head = 0;
236         if (SEQ_GT(head_blk.start, tp->rcv_nxt)) {
237                 /*
238                  * The received data segment is an out-of-order segment.
239                  * Put head_blk at the top of SACK list.
240                  */
241                 tp->sackblks[0] = head_blk;
242                 num_head = 1;
243                 /*
244                  * If the number of saved SACK blocks exceeds its limit,
245                  * discard the last SACK block.
246                  */
247                 if (num_saved >= MAX_SACK_BLKS)
248                         num_saved--;
249         }
250         if (num_saved > 0) {
251                 /*
252                  * Copy the saved SACK blocks back.
253                  */
254                 bcopy(saved_blks, &tp->sackblks[num_head],
255                       sizeof(struct sackblk) * num_saved);
256         }
257
258         /* Save the number of SACK blocks. */
259         tp->rcv_numsacks = num_head + num_saved;
260 }
261
262 /*
263  * Delete all receiver-side SACK information.
264  */
265 void
266 tcp_clean_sackreport(tp)
267         struct tcpcb *tp;
268 {
269         int i;
270
271         INP_LOCK_ASSERT(tp->t_inpcb);
272         tp->rcv_numsacks = 0;
273         for (i = 0; i < MAX_SACK_BLKS; i++)
274                 tp->sackblks[i].start = tp->sackblks[i].end=0;
275 }
276
277 /*
278  * Allocate struct sackhole.
279  */
280 static struct sackhole *
281 tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
282 {
283         struct sackhole *hole;
284
285         if (tp->snd_numholes >= tcp_sack_maxholes ||
286             tcp_sack_globalholes >= tcp_sack_globalmaxholes) {
287                 tcpstat.tcps_sack_sboverflow++;
288                 return NULL;
289         }
290
291         hole = (struct sackhole *)uma_zalloc(sack_hole_zone, M_NOWAIT);
292         if (hole == NULL)
293                 return NULL;
294
295         hole->start = start;
296         hole->end = end;
297         hole->rxmit = start;
298
299         tp->snd_numholes++;
300         tcp_sack_globalholes++;
301
302         return hole;
303 }
304
305 /*
306  * Free struct sackhole.
307  */
308 static void
309 tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
310 {
311         uma_zfree(sack_hole_zone, hole);
312
313         tp->snd_numholes--;
314         tcp_sack_globalholes--;
315
316         KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
317         KASSERT(tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
318 }
319
320 /*
321  * Insert new SACK hole into scoreboard.
322  */
323 static struct sackhole *
324 tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
325                     struct sackhole *after)
326 {
327         struct sackhole *hole;
328
329         /* Allocate a new SACK hole. */
330         hole = tcp_sackhole_alloc(tp, start, end);
331         if (hole == NULL)
332                 return NULL;
333
334         /* Insert the new SACK hole into scoreboard */
335         if (after != NULL)
336                 TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
337         else
338                 TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
339
340         /* Update SACK hint. */
341         if (tp->sackhint.nexthole == NULL)
342                 tp->sackhint.nexthole = hole;
343
344         return hole;
345 }
346
347 /*
348  * Remove SACK hole from scoreboard.
349  */
350 static void
351 tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
352 {
353         /* Update SACK hint. */
354         if (tp->sackhint.nexthole == hole)
355                 tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
356
357         /* Remove this SACK hole. */
358         TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
359
360         /* Free this SACK hole. */
361         tcp_sackhole_free(tp, hole);
362 }
363
364 /*
365  * Process cumulative ACK and the TCP SACK option to update the scoreboard.
366  * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
367  * the sequence space).
368  */
369 void
370 tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack)
371 {
372         struct sackhole *cur, *temp;
373         struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp;
374         int i, j, num_sack_blks;
375
376         INP_LOCK_ASSERT(tp->t_inpcb);
377
378         num_sack_blks = 0;
379         /*
380          * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
381          * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
382          */
383         if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
384                 sack_blocks[num_sack_blks].start = tp->snd_una;
385                 sack_blocks[num_sack_blks++].end = th_ack;
386         }
387         /*
388          * Append received valid SACK blocks to sack_blocks[].
389          */
390         for (i = 0; i < to->to_nsacks; i++) {
391                 bcopy((to->to_sacks + i * TCPOLEN_SACK), &sack, sizeof(sack));
392                 sack.start = ntohl(sack.start);
393                 sack.end = ntohl(sack.end);
394                 if (SEQ_GT(sack.end, sack.start) &&
395                     SEQ_GT(sack.start, tp->snd_una) &&
396                     SEQ_GT(sack.start, th_ack) &&
397                     SEQ_LEQ(sack.end, tp->snd_max))
398                         sack_blocks[num_sack_blks++] = sack;
399         }
400
401         /*
402          * Return if SND.UNA is not advanced and no valid SACK block
403          * is received.
404          */
405         if (num_sack_blks == 0)
406                 return;
407
408         /*
409          * Sort the SACK blocks so we can update the scoreboard
410          * with just one pass. The overhead of sorting upto 4+1 elements
411          * is less than making upto 4+1 passes over the scoreboard.
412          */
413         for (i = 0; i < num_sack_blks; i++) {
414                 for (j = i + 1; j < num_sack_blks; j++) {
415                         if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
416                                 sack = sack_blocks[i];
417                                 sack_blocks[i] = sack_blocks[j];
418                                 sack_blocks[j] = sack;
419                         }
420                 }
421         }
422         if (TAILQ_EMPTY(&tp->snd_holes))
423                 /*
424                  * Empty scoreboard. Need to initialize snd_fack (it may be
425                  * uninitialized or have a bogus value). Scoreboard holes
426                  * (from the sack blocks received) are created later below (in
427                  * the logic that adds holes to the tail of the scoreboard).
428                  */
429                 tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
430         /*
431          * In the while-loop below, incoming SACK blocks (sack_blocks[])
432          * and SACK holes (snd_holes) are traversed from their tails with
433          * just one pass in order to reduce the number of compares especially
434          * when the bandwidth-delay product is large.
435          * Note: Typically, in the first RTT of SACK recovery, the highest
436          * three or four SACK blocks with the same ack number are received.
437          * In the second RTT, if retransmitted data segments are not lost,
438          * the highest three or four SACK blocks with ack number advancing
439          * are received.
440          */
441         sblkp = &sack_blocks[num_sack_blks - 1];        /* Last SACK block */
442         if (SEQ_LT(tp->snd_fack, sblkp->start)) {
443                 /*
444                  * The highest SACK block is beyond fack.
445                  * Append new SACK hole at the tail.
446                  * If the second or later highest SACK blocks are also
447                  * beyond the current fack, they will be inserted by
448                  * way of hole splitting in the while-loop below.
449                  */
450                 temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL);
451                 if (temp == NULL)
452                         return;
453                 tp->snd_fack = sblkp->end;
454                 /* Go to the previous sack block. */
455                 sblkp--;
456         } else if (SEQ_LT(tp->snd_fack, sblkp->end))
457                 /* fack is advanced. */
458                 tp->snd_fack = sblkp->end;
459         /* We must have at least one SACK hole in scoreboard */
460         KASSERT(!TAILQ_EMPTY(&tp->snd_holes), ("SACK scoreboard must not be empty"));
461         cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole */
462         /*
463          * Since the incoming sack blocks are sorted, we can process them
464          * making one sweep of the scoreboard.
465          */
466         while (sblkp - sack_blocks >= 0 && cur != NULL) {
467                 if (SEQ_GEQ(sblkp->start, cur->end)) {
468                         /*
469                          * SACKs data beyond the current hole.
470                          * Go to the previous sack block.
471                          */
472                         sblkp--;
473                         continue;
474                 }
475                 if (SEQ_LEQ(sblkp->end, cur->start)) {
476                         /*
477                          * SACKs data before the current hole.
478                          * Go to the previous hole.
479                          */
480                         cur = TAILQ_PREV(cur, sackhole_head, scblink);
481                         continue;
482                 }
483                 tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
484                 KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
485                         ("sackhint bytes rtx >= 0"));
486                 if (SEQ_LEQ(sblkp->start, cur->start)) {
487                         /* Data acks at least the beginning of hole */
488                         if (SEQ_GEQ(sblkp->end, cur->end)) {
489                                 /* Acks entire hole, so delete hole */
490                                 temp = cur;
491                                 cur = TAILQ_PREV(cur, sackhole_head, scblink);
492                                 tcp_sackhole_remove(tp, temp);
493                                 /*
494                                  * The sack block may ack all or part of the next
495                                  * hole too, so continue onto the next hole.
496                                  */
497                                 continue;
498                         } else {
499                                 /* Move start of hole forward */
500                                 cur->start = sblkp->end;
501                                 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
502                         }
503                 } else {
504                         /* Data acks at least the end of hole */
505                         if (SEQ_GEQ(sblkp->end, cur->end)) {
506                                 /* Move end of hole backward */
507                                 cur->end = sblkp->start;
508                                 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
509                         } else {
510                                 /*
511                                  * ACKs some data in middle of a hole; need to
512                                  * split current hole
513                                  */
514                                 temp = tcp_sackhole_insert(tp, sblkp->end,
515                                                            cur->end, cur);
516                                 if (temp != NULL) {
517                                         if (SEQ_GT(cur->rxmit, temp->rxmit)) {
518                                                 temp->rxmit = cur->rxmit;
519                                                 tp->sackhint.sack_bytes_rexmit
520                                                         += (temp->rxmit
521                                                             - temp->start);
522                                         }
523                                         cur->end = sblkp->start;
524                                         cur->rxmit = SEQ_MIN(cur->rxmit,
525                                                              cur->end);
526                                 }
527                         }
528                 }
529                 tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
530                 /*
531                  * Testing sblkp->start against cur->start tells us whether
532                  * we're done with the sack block or the sack hole.
533                  * Accordingly, we advance one or the other.
534                  */
535                 if (SEQ_LEQ(sblkp->start, cur->start))
536                         cur = TAILQ_PREV(cur, sackhole_head, scblink);
537                 else
538                         sblkp--;
539         }
540 }
541
542 /*
543  * Free all SACK holes to clear the scoreboard.
544  */
545 void
546 tcp_free_sackholes(struct tcpcb *tp)
547 {
548         struct sackhole *q;
549
550         INP_LOCK_ASSERT(tp->t_inpcb);
551         while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
552                 tcp_sackhole_remove(tp, q);
553         tp->sackhint.sack_bytes_rexmit = 0;
554
555         KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
556         KASSERT(tp->sackhint.nexthole == NULL,
557                 ("tp->sackhint.nexthole == NULL"));
558 }
559
560 /*
561  * Partial ack handling within a sack recovery episode. 
562  * Keeping this very simple for now. When a partial ack
563  * is received, force snd_cwnd to a value that will allow
564  * the sender to transmit no more than 2 segments.
565  * If necessary, a better scheme can be adopted at a 
566  * later point, but for now, the goal is to prevent the
567  * sender from bursting a large amount of data in the midst
568  * of sack recovery.
569  */
570 void
571 tcp_sack_partialack(tp, th)
572         struct tcpcb *tp;
573         struct tcphdr *th;
574 {
575         int num_segs = 1;
576
577         INP_LOCK_ASSERT(tp->t_inpcb);
578         callout_stop(tp->tt_rexmt);
579         tp->t_rtttime = 0;
580         /* send one or 2 segments based on how much new data was acked */
581         if (((th->th_ack - tp->snd_una) / tp->t_maxseg) > 2)
582                 num_segs = 2;
583         tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
584                 (tp->snd_nxt - tp->sack_newdata) +
585                 num_segs * tp->t_maxseg);
586         if (tp->snd_cwnd > tp->snd_ssthresh)
587                 tp->snd_cwnd = tp->snd_ssthresh;
588         tp->t_flags |= TF_ACKNOW;
589         (void) tcp_output(tp);
590 }
591
592 /*
593  * Debug version of tcp_sack_output() that walks the scoreboard. Used for
594  * now to sanity check the hint.
595  */
596 static struct sackhole *
597 tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
598 {
599         struct sackhole *p;
600
601         INP_LOCK_ASSERT(tp->t_inpcb);
602         *sack_bytes_rexmt = 0;
603         TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
604                 if (SEQ_LT(p->rxmit, p->end)) {
605                         if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
606                                 continue;
607                         }
608                         *sack_bytes_rexmt += (p->rxmit - p->start);
609                         break;
610                 }
611                 *sack_bytes_rexmt += (p->rxmit - p->start);
612         }
613         return (p);
614 }
615
616 /*
617  * Returns the next hole to retransmit and the number of retransmitted bytes
618  * from the scoreboard. We store both the next hole and the number of
619  * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
620  * reception). This avoids scoreboard traversals completely.
621  *
622  * The loop here will traverse *at most* one link. Here's the argument.
623  * For the loop to traverse more than 1 link before finding the next hole to
624  * retransmit, we would need to have at least 1 node following the current hint
625  * with (rxmit == end). But, for all holes following the current hint,
626  * (start == rxmit), since we have not yet retransmitted from them. Therefore,
627  * in order to traverse more 1 link in the loop below, we need to have at least
628  * one node following the current hint with (start == rxmit == end).
629  * But that can't happen, (start == end) means that all the data in that hole
630  * has been sacked, in which case, the hole would have been removed from the
631  * scoreboard.
632  */
633 struct sackhole *
634 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
635 {
636         struct sackhole *hole = NULL, *dbg_hole = NULL;
637         int dbg_bytes_rexmt;
638
639         INP_LOCK_ASSERT(tp->t_inpcb);
640         dbg_hole = tcp_sack_output_debug(tp, &dbg_bytes_rexmt);
641         *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
642         hole = tp->sackhint.nexthole;
643         if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
644                 goto out;
645         while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
646                 if (SEQ_LT(hole->rxmit, hole->end)) {
647                         tp->sackhint.nexthole = hole;
648                         break;
649                 }
650         }
651 out:
652         if (dbg_hole != hole) {
653                 printf("%s: Computed sack hole not the same as cached value\n", __func__);
654                 hole = dbg_hole;
655         }
656         if (*sack_bytes_rexmt != dbg_bytes_rexmt) {
657                 printf("%s: Computed sack_bytes_retransmitted (%d) not "
658                        "the same as cached value (%d)\n",
659                        __func__, dbg_bytes_rexmt, *sack_bytes_rexmt);
660                 *sack_bytes_rexmt = dbg_bytes_rexmt;
661         }
662         return (hole);
663 }
664
665 /*
666  * After a timeout, the SACK list may be rebuilt.  This SACK information
667  * should be used to avoid retransmitting SACKed data.  This function
668  * traverses the SACK list to see if snd_nxt should be moved forward.
669  */
670 void
671 tcp_sack_adjust(struct tcpcb *tp)
672 {
673         struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
674
675         INP_LOCK_ASSERT(tp->t_inpcb);
676         if (cur == NULL)
677                 return; /* No holes */
678         if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack))
679                 return; /* We're already beyond any SACKed blocks */
680         /*
681          * Two cases for which we want to advance snd_nxt:
682          * i) snd_nxt lies between end of one hole and beginning of another
683          * ii) snd_nxt lies between end of last hole and snd_fack
684          */
685         while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
686                 if (SEQ_LT(tp->snd_nxt, cur->end))
687                         return;
688                 if (SEQ_GEQ(tp->snd_nxt, p->start))
689                         cur = p;
690                 else {
691                         tp->snd_nxt = p->start;
692                         return;
693                 }
694         }
695         if (SEQ_LT(tp->snd_nxt, cur->end))
696                 return;
697         tp->snd_nxt = tp->snd_fack;
698         return;
699 }