]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/tcp_sack.c
tcp: Rename rfc6675_pipe to sack.revised, and enable by default
[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 | CTLFLAG_MPSAFE, 0,
132     "TCP SACK");
133
134 VNET_DEFINE(int, tcp_do_sack) = 1;
135 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
136     &VNET_NAME(tcp_do_sack), 0,
137     "Enable/Disable TCP SACK support");
138
139 VNET_DEFINE(int, tcp_do_newsack) = 1;
140 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, revised, CTLFLAG_VNET | CTLFLAG_RW,
141     &VNET_NAME(tcp_do_newsack), 0,
142     "Use revised SACK loss recovery per RFC 6675");
143
144 VNET_DEFINE(int, tcp_sack_maxholes) = 128;
145 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_VNET | CTLFLAG_RW,
146     &VNET_NAME(tcp_sack_maxholes), 0,
147     "Maximum number of TCP SACK holes allowed per connection");
148
149 VNET_DEFINE(int, tcp_sack_globalmaxholes) = 65536;
150 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_VNET | CTLFLAG_RW,
151     &VNET_NAME(tcp_sack_globalmaxholes), 0,
152     "Global maximum number of TCP SACK holes");
153
154 VNET_DEFINE(int, tcp_sack_globalholes) = 0;
155 SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_VNET | CTLFLAG_RD,
156     &VNET_NAME(tcp_sack_globalholes), 0,
157     "Global number of TCP SACK holes currently allocated");
158
159 /*
160  * This function will find overlaps with the currently stored sackblocks
161  * and add any overlap as a dsack block upfront
162  */
163 void
164 tcp_update_dsack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
165 {
166         struct sackblk head_blk,mid_blk,saved_blks[MAX_SACK_BLKS];
167         int i, j, n, identical;
168         tcp_seq start, end;
169
170         INP_WLOCK_ASSERT(tp->t_inpcb);
171
172         KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end"));
173
174         if (SEQ_LT(rcv_end, tp->rcv_nxt) ||
175             ((rcv_end == tp->rcv_nxt) &&
176              (tp->rcv_numsacks > 0 ) &&
177              (tp->sackblks[0].end == tp->rcv_nxt))) {
178                 saved_blks[0].start = rcv_start;
179                 saved_blks[0].end = rcv_end;
180         } else {
181                 saved_blks[0].start = saved_blks[0].end = 0;
182         }
183
184         head_blk.start = head_blk.end = 0;
185         mid_blk.start = rcv_start;
186         mid_blk.end = rcv_end;
187         identical = 0;
188
189         for (i = 0; i < tp->rcv_numsacks; i++) {
190                 start = tp->sackblks[i].start;
191                 end = tp->sackblks[i].end;
192                 if (SEQ_LT(rcv_end, start)) {
193                         /* pkt left to sack blk */
194                         continue;
195                 }
196                 if (SEQ_GT(rcv_start, end)) {
197                         /* pkt right to sack blk */
198                         continue;
199                 }
200                 if (SEQ_GT(tp->rcv_nxt, end)) {
201                         if ((SEQ_MAX(rcv_start, start) != SEQ_MIN(rcv_end, end)) &&
202                             (SEQ_GT(head_blk.start, SEQ_MAX(rcv_start, start)) ||
203                             (head_blk.start == head_blk.end))) {
204                                 head_blk.start = SEQ_MAX(rcv_start, start);
205                                 head_blk.end = SEQ_MIN(rcv_end, end);
206                         }
207                         continue;
208                 }
209                 if (((head_blk.start == head_blk.end) ||
210                      SEQ_LT(start, head_blk.start)) &&
211                      (SEQ_GT(end, rcv_start) &&
212                       SEQ_LEQ(start, rcv_end))) {
213                         head_blk.start = start;
214                         head_blk.end = end;
215                 }
216                 mid_blk.start = SEQ_MIN(mid_blk.start, start);
217                 mid_blk.end = SEQ_MAX(mid_blk.end, end);
218                 if ((mid_blk.start == start) &&
219                     (mid_blk.end == end))
220                         identical = 1;
221         }
222         if (SEQ_LT(head_blk.start, head_blk.end)) {
223                 /* store overlapping range */
224                 saved_blks[0].start = SEQ_MAX(rcv_start, head_blk.start);
225                 saved_blks[0].end   = SEQ_MIN(rcv_end, head_blk.end);
226         }
227         n = 1;
228         /*
229          * Second, if not ACKed, store the SACK block that
230          * overlaps with the DSACK block unless it is identical
231          */
232         if ((SEQ_LT(tp->rcv_nxt, mid_blk.end) &&
233             !((mid_blk.start == saved_blks[0].start) &&
234             (mid_blk.end == saved_blks[0].end))) ||
235             identical == 1) {
236                 saved_blks[n].start = mid_blk.start;
237                 saved_blks[n++].end = mid_blk.end;
238         }
239         for (j = 0; (j < tp->rcv_numsacks) && (n < MAX_SACK_BLKS); j++) {
240                 if (((SEQ_LT(tp->sackblks[j].end, mid_blk.start) ||
241                       SEQ_GT(tp->sackblks[j].start, mid_blk.end)) &&
242                     (SEQ_GT(tp->sackblks[j].start, tp->rcv_nxt))))
243                 saved_blks[n++] = tp->sackblks[j];
244         }
245         j = 0;
246         for (i = 0; i < n; i++) {
247                 /* we can end up with a stale initial entry */
248                 if (SEQ_LT(saved_blks[i].start, saved_blks[i].end)) {
249                         tp->sackblks[j++] = saved_blks[i];
250                 }
251         }
252         tp->rcv_numsacks = j;
253 }
254
255 /*
256  * This function is called upon receipt of new valid data (while not in
257  * header prediction mode), and it updates the ordered list of sacks.
258  */
259 void
260 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end)
261 {
262         /*
263          * First reported block MUST be the most recent one.  Subsequent
264          * blocks SHOULD be in the order in which they arrived at the
265          * receiver.  These two conditions make the implementation fully
266          * compliant with RFC 2018.
267          */
268         struct sackblk head_blk, saved_blks[MAX_SACK_BLKS];
269         int num_head, num_saved, i;
270
271         INP_WLOCK_ASSERT(tp->t_inpcb);
272
273         /* Check arguments. */
274         KASSERT(SEQ_LEQ(rcv_start, rcv_end), ("rcv_start <= rcv_end"));
275
276         if ((rcv_start == rcv_end) &&
277             (tp->rcv_numsacks >= 1) &&
278             (rcv_end == tp->sackblks[0].end)) {
279                 /* retaining DSACK block below rcv_nxt (todrop) */
280                 head_blk = tp->sackblks[0];
281         } else {
282                 /* SACK block for the received segment. */
283                 head_blk.start = rcv_start;
284                 head_blk.end = rcv_end;
285         }
286
287         /*
288          * Merge updated SACK blocks into head_blk, and save unchanged SACK
289          * blocks into saved_blks[].  num_saved will have the number of the
290          * saved SACK blocks.
291          */
292         num_saved = 0;
293         for (i = 0; i < tp->rcv_numsacks; i++) {
294                 tcp_seq start = tp->sackblks[i].start;
295                 tcp_seq end = tp->sackblks[i].end;
296                 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
297                         /*
298                          * Discard this SACK block.
299                          */
300                 } else if (SEQ_LEQ(head_blk.start, end) &&
301                            SEQ_GEQ(head_blk.end, start)) {
302                         /*
303                          * Merge this SACK block into head_blk.  This SACK
304                          * block itself will be discarded.
305                          */
306                         /*
307                          * |-|
308                          *   |---|  merge
309                          *
310                          *     |-|
311                          * |---|    merge
312                          *
313                          * |-----|
314                          *   |-|    DSACK smaller
315                          *
316                          *   |-|
317                          * |-----|  DSACK smaller
318                          */
319                         if (head_blk.start == end)
320                                 head_blk.start = start;
321                         else if (head_blk.end == start)
322                                 head_blk.end = end;
323                         else {
324                                 if (SEQ_LT(head_blk.start, start)) {
325                                         tcp_seq temp = start;
326                                         start = head_blk.start;
327                                         head_blk.start = temp;
328                                 }
329                                 if (SEQ_GT(head_blk.end, end)) {
330                                         tcp_seq temp = end;
331                                         end = head_blk.end;
332                                         head_blk.end = temp;
333                                 }
334                                 if ((head_blk.start != start) ||
335                                     (head_blk.end != end)) {
336                                         if ((num_saved >= 1) &&
337                                            SEQ_GEQ(saved_blks[num_saved-1].start, start) &&
338                                            SEQ_LEQ(saved_blks[num_saved-1].end, end))
339                                                 num_saved--;
340                                         saved_blks[num_saved].start = start;
341                                         saved_blks[num_saved].end = end;
342                                         num_saved++;
343                                 }
344                         }
345                 } else {
346                         /*
347                          * This block supercedes the prior block
348                          */
349                         if ((num_saved >= 1) &&
350                            SEQ_GEQ(saved_blks[num_saved-1].start, start) &&
351                            SEQ_LEQ(saved_blks[num_saved-1].end, end))
352                                 num_saved--;
353                         /*
354                          * Save this SACK block.
355                          */
356                         saved_blks[num_saved].start = start;
357                         saved_blks[num_saved].end = end;
358                         num_saved++;
359                 }
360         }
361
362         /*
363          * Update SACK list in tp->sackblks[].
364          */
365         num_head = 0;
366         if (SEQ_LT(rcv_start, rcv_end)) {
367                 /*
368                  * The received data segment is an out-of-order segment.  Put
369                  * head_blk at the top of SACK list.
370                  */
371                 tp->sackblks[0] = head_blk;
372                 num_head = 1;
373                 /*
374                  * If the number of saved SACK blocks exceeds its limit,
375                  * discard the last SACK block.
376                  */
377                 if (num_saved >= MAX_SACK_BLKS)
378                         num_saved--;
379         }
380         if ((rcv_start == rcv_end) &&
381             (rcv_start == tp->sackblks[0].end)) {
382                 num_head = 1;
383         }
384         if (num_saved > 0) {
385                 /*
386                  * Copy the saved SACK blocks back.
387                  */
388                 bcopy(saved_blks, &tp->sackblks[num_head],
389                       sizeof(struct sackblk) * num_saved);
390         }
391
392         /* Save the number of SACK blocks. */
393         tp->rcv_numsacks = num_head + num_saved;
394 }
395
396 void
397 tcp_clean_dsack_blocks(struct tcpcb *tp)
398 {
399         struct sackblk saved_blks[MAX_SACK_BLKS];
400         int num_saved, i;
401
402         INP_WLOCK_ASSERT(tp->t_inpcb);
403         /*
404          * Clean up any DSACK blocks that
405          * are in our queue of sack blocks.
406          *
407          */
408         num_saved = 0;
409         for (i = 0; i < tp->rcv_numsacks; i++) {
410                 tcp_seq start = tp->sackblks[i].start;
411                 tcp_seq end = tp->sackblks[i].end;
412                 if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) {
413                         /*
414                          * Discard this D-SACK block.
415                          */
416                         continue;
417                 }
418                 /*
419                  * Save this SACK block.
420                  */
421                 saved_blks[num_saved].start = start;
422                 saved_blks[num_saved].end = end;
423                 num_saved++;
424         }
425         if (num_saved > 0) {
426                 /*
427                  * Copy the saved SACK blocks back.
428                  */
429                 bcopy(saved_blks, &tp->sackblks[0],
430                       sizeof(struct sackblk) * num_saved);
431         }
432         tp->rcv_numsacks = num_saved;
433 }
434
435 /*
436  * Delete all receiver-side SACK information.
437  */
438 void
439 tcp_clean_sackreport(struct tcpcb *tp)
440 {
441         int i;
442
443         INP_WLOCK_ASSERT(tp->t_inpcb);
444         tp->rcv_numsacks = 0;
445         for (i = 0; i < MAX_SACK_BLKS; i++)
446                 tp->sackblks[i].start = tp->sackblks[i].end=0;
447 }
448
449 /*
450  * Allocate struct sackhole.
451  */
452 static struct sackhole *
453 tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end)
454 {
455         struct sackhole *hole;
456
457         if (tp->snd_numholes >= V_tcp_sack_maxholes ||
458             V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) {
459                 TCPSTAT_INC(tcps_sack_sboverflow);
460                 return NULL;
461         }
462
463         hole = (struct sackhole *)uma_zalloc(V_sack_hole_zone, M_NOWAIT);
464         if (hole == NULL)
465                 return NULL;
466
467         hole->start = start;
468         hole->end = end;
469         hole->rxmit = start;
470
471         tp->snd_numholes++;
472         atomic_add_int(&V_tcp_sack_globalholes, 1);
473
474         return hole;
475 }
476
477 /*
478  * Free struct sackhole.
479  */
480 static void
481 tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole)
482 {
483
484         uma_zfree(V_sack_hole_zone, hole);
485
486         tp->snd_numholes--;
487         atomic_subtract_int(&V_tcp_sack_globalholes, 1);
488
489         KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0"));
490         KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0"));
491 }
492
493 /*
494  * Insert new SACK hole into scoreboard.
495  */
496 static struct sackhole *
497 tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end,
498     struct sackhole *after)
499 {
500         struct sackhole *hole;
501
502         /* Allocate a new SACK hole. */
503         hole = tcp_sackhole_alloc(tp, start, end);
504         if (hole == NULL)
505                 return NULL;
506
507         /* Insert the new SACK hole into scoreboard. */
508         if (after != NULL)
509                 TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink);
510         else
511                 TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink);
512
513         /* Update SACK hint. */
514         if (tp->sackhint.nexthole == NULL)
515                 tp->sackhint.nexthole = hole;
516
517         return hole;
518 }
519
520 /*
521  * Remove SACK hole from scoreboard.
522  */
523 static void
524 tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole)
525 {
526
527         /* Update SACK hint. */
528         if (tp->sackhint.nexthole == hole)
529                 tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink);
530
531         /* Remove this SACK hole. */
532         TAILQ_REMOVE(&tp->snd_holes, hole, scblink);
533
534         /* Free this SACK hole. */
535         tcp_sackhole_free(tp, hole);
536 }
537
538 /*
539  * Process cumulative ACK and the TCP SACK option to update the scoreboard.
540  * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of
541  * the sequence space).
542  * Returns 1 if incoming ACK has previously unknown SACK information,
543  * 0 otherwise.
544  */
545 int
546 tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack)
547 {
548         struct sackhole *cur, *temp;
549         struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp;
550         int i, j, num_sack_blks, sack_changed;
551         int delivered_data, left_edge_delta;
552
553         INP_WLOCK_ASSERT(tp->t_inpcb);
554
555         num_sack_blks = 0;
556         sack_changed = 0;
557         delivered_data = 0;
558         left_edge_delta = 0;
559         /*
560          * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist,
561          * treat [SND.UNA, SEG.ACK) as if it is a SACK block.
562          * Account changes to SND.UNA always in delivered data.
563          */
564         if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) {
565                 left_edge_delta = th_ack - tp->snd_una;
566                 sack_blocks[num_sack_blks].start = tp->snd_una;
567                 sack_blocks[num_sack_blks++].end = th_ack;
568         }
569         /*
570          * Append received valid SACK blocks to sack_blocks[], but only if we
571          * received new blocks from the other side.
572          */
573         if (to->to_flags & TOF_SACK) {
574                 for (i = 0; i < to->to_nsacks; i++) {
575                         bcopy((to->to_sacks + i * TCPOLEN_SACK),
576                             &sack, sizeof(sack));
577                         sack.start = ntohl(sack.start);
578                         sack.end = ntohl(sack.end);
579                         if (SEQ_GT(sack.end, sack.start) &&
580                             SEQ_GT(sack.start, tp->snd_una) &&
581                             SEQ_GT(sack.start, th_ack) &&
582                             SEQ_LT(sack.start, tp->snd_max) &&
583                             SEQ_GT(sack.end, tp->snd_una) &&
584                             SEQ_LEQ(sack.end, tp->snd_max)) {
585                                 sack_blocks[num_sack_blks++] = sack;
586                         }
587                 }
588         }
589         /*
590          * Return if SND.UNA is not advanced and no valid SACK block is
591          * received.
592          */
593         if (num_sack_blks == 0)
594                 return (sack_changed);
595
596         /*
597          * Sort the SACK blocks so we can update the scoreboard with just one
598          * pass. The overhead of sorting up to 4+1 elements is less than
599          * making up to 4+1 passes over the scoreboard.
600          */
601         for (i = 0; i < num_sack_blks; i++) {
602                 for (j = i + 1; j < num_sack_blks; j++) {
603                         if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) {
604                                 sack = sack_blocks[i];
605                                 sack_blocks[i] = sack_blocks[j];
606                                 sack_blocks[j] = sack;
607                         }
608                 }
609         }
610         if (TAILQ_EMPTY(&tp->snd_holes)) {
611                 /*
612                  * Empty scoreboard. Need to initialize snd_fack (it may be
613                  * uninitialized or have a bogus value). Scoreboard holes
614                  * (from the sack blocks received) are created later below
615                  * (in the logic that adds holes to the tail of the
616                  * scoreboard).
617                  */
618                 tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack);
619                 tp->sackhint.sacked_bytes = 0;  /* reset */
620         }
621         /*
622          * In the while-loop below, incoming SACK blocks (sack_blocks[]) and
623          * SACK holes (snd_holes) are traversed from their tails with just
624          * one pass in order to reduce the number of compares especially when
625          * the bandwidth-delay product is large.
626          *
627          * Note: Typically, in the first RTT of SACK recovery, the highest
628          * three or four SACK blocks with the same ack number are received.
629          * In the second RTT, if retransmitted data segments are not lost,
630          * the highest three or four SACK blocks with ack number advancing
631          * are received.
632          */
633         sblkp = &sack_blocks[num_sack_blks - 1];        /* Last SACK block */
634         tp->sackhint.last_sack_ack = sblkp->end;
635         if (SEQ_LT(tp->snd_fack, sblkp->start)) {
636                 /*
637                  * The highest SACK block is beyond fack.  Append new SACK
638                  * hole at the tail.  If the second or later highest SACK
639                  * blocks are also beyond the current fack, they will be
640                  * inserted by way of hole splitting in the while-loop below.
641                  */
642                 temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL);
643                 if (temp != NULL) {
644                         delivered_data += sblkp->end - sblkp->start;
645                         tp->snd_fack = sblkp->end;
646                         /* Go to the previous sack block. */
647                         sblkp--;
648                         sack_changed = 1;
649                 } else {
650                         /*
651                          * We failed to add a new hole based on the current
652                          * sack block.  Skip over all the sack blocks that
653                          * fall completely to the right of snd_fack and
654                          * proceed to trim the scoreboard based on the
655                          * remaining sack blocks.  This also trims the
656                          * scoreboard for th_ack (which is sack_blocks[0]).
657                          */
658                         while (sblkp >= sack_blocks &&
659                                SEQ_LT(tp->snd_fack, sblkp->start))
660                                 sblkp--;
661                         if (sblkp >= sack_blocks &&
662                             SEQ_LT(tp->snd_fack, sblkp->end)) {
663                                 delivered_data += sblkp->end - tp->snd_fack;
664                                 tp->snd_fack = sblkp->end;
665                                 sack_changed = 1;
666                         }
667                 }
668         } else if (SEQ_LT(tp->snd_fack, sblkp->end)) {
669                 /* fack is advanced. */
670                 delivered_data += sblkp->end - tp->snd_fack;
671                 tp->snd_fack = sblkp->end;
672                 sack_changed = 1;
673         }
674         cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */
675         /*
676          * Since the incoming sack blocks are sorted, we can process them
677          * making one sweep of the scoreboard.
678          */
679         while (sblkp >= sack_blocks  && cur != NULL) {
680                 if (SEQ_GEQ(sblkp->start, cur->end)) {
681                         /*
682                          * SACKs data beyond the current hole.  Go to the
683                          * previous sack block.
684                          */
685                         sblkp--;
686                         continue;
687                 }
688                 if (SEQ_LEQ(sblkp->end, cur->start)) {
689                         /*
690                          * SACKs data before the current hole.  Go to the
691                          * previous hole.
692                          */
693                         cur = TAILQ_PREV(cur, sackhole_head, scblink);
694                         continue;
695                 }
696                 tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start);
697                 KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
698                     ("sackhint bytes rtx >= 0"));
699                 sack_changed = 1;
700                 if (SEQ_LEQ(sblkp->start, cur->start)) {
701                         /* Data acks at least the beginning of hole. */
702                         if (SEQ_GEQ(sblkp->end, cur->end)) {
703                                 /* Acks entire hole, so delete hole. */
704                                 delivered_data += (cur->end - cur->start);
705                                 temp = cur;
706                                 cur = TAILQ_PREV(cur, sackhole_head, scblink);
707                                 tcp_sackhole_remove(tp, temp);
708                                 /*
709                                  * The sack block may ack all or part of the
710                                  * next hole too, so continue onto the next
711                                  * hole.
712                                  */
713                                 continue;
714                         } else {
715                                 /* Move start of hole forward. */
716                                 delivered_data += (sblkp->end - cur->start);
717                                 cur->start = sblkp->end;
718                                 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
719                         }
720                 } else {
721                         /* Data acks at least the end of hole. */
722                         if (SEQ_GEQ(sblkp->end, cur->end)) {
723                                 /* Move end of hole backward. */
724                                 delivered_data += (cur->end - sblkp->start);
725                                 cur->end = sblkp->start;
726                                 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
727                         } else {
728                                 /*
729                                  * ACKs some data in middle of a hole; need
730                                  * to split current hole
731                                  */
732                                 temp = tcp_sackhole_insert(tp, sblkp->end,
733                                     cur->end, cur);
734                                 if (temp != NULL) {
735                                         if (SEQ_GT(cur->rxmit, temp->rxmit)) {
736                                                 temp->rxmit = cur->rxmit;
737                                                 tp->sackhint.sack_bytes_rexmit
738                                                     += (temp->rxmit
739                                                     - temp->start);
740                                         }
741                                         cur->end = sblkp->start;
742                                         cur->rxmit = SEQ_MIN(cur->rxmit,
743                                             cur->end);
744                                         delivered_data += (sblkp->end - sblkp->start);
745                                 }
746                         }
747                 }
748                 tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start);
749                 /*
750                  * Testing sblkp->start against cur->start tells us whether
751                  * we're done with the sack block or the sack hole.
752                  * Accordingly, we advance one or the other.
753                  */
754                 if (SEQ_LEQ(sblkp->start, cur->start))
755                         cur = TAILQ_PREV(cur, sackhole_head, scblink);
756                 else
757                         sblkp--;
758         }
759         if (!(to->to_flags & TOF_SACK))
760                 /*
761                  * If this ACK did not contain any
762                  * SACK blocks, any only moved the
763                  * left edge right, it is a pure
764                  * cumulative ACK. Do not count
765                  * DupAck for this. Also required
766                  * for RFC6675 rescue retransmission.
767                  */
768                 sack_changed = 0;
769         tp->sackhint.delivered_data = delivered_data;
770         tp->sackhint.sacked_bytes += delivered_data - left_edge_delta;
771         KASSERT((delivered_data >= 0), ("delivered_data < 0"));
772         KASSERT((tp->sackhint.sacked_bytes >= 0), ("sacked_bytes < 0"));
773         return (sack_changed);
774 }
775
776 /*
777  * Free all SACK holes to clear the scoreboard.
778  */
779 void
780 tcp_free_sackholes(struct tcpcb *tp)
781 {
782         struct sackhole *q;
783
784         INP_WLOCK_ASSERT(tp->t_inpcb);
785         while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL)
786                 tcp_sackhole_remove(tp, q);
787         tp->sackhint.sack_bytes_rexmit = 0;
788
789         KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0"));
790         KASSERT(tp->sackhint.nexthole == NULL,
791                 ("tp->sackhint.nexthole == NULL"));
792 }
793
794 /*
795  * Partial ack handling within a sack recovery episode.  Keeping this very
796  * simple for now.  When a partial ack is received, force snd_cwnd to a value
797  * that will allow the sender to transmit no more than 2 segments.  If
798  * necessary, a better scheme can be adopted at a later point, but for now,
799  * the goal is to prevent the sender from bursting a large amount of data in
800  * the midst of sack recovery.
801  */
802 void
803 tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th)
804 {
805         int num_segs = 1;
806         u_int maxseg = tcp_maxseg(tp);
807
808         INP_WLOCK_ASSERT(tp->t_inpcb);
809         tcp_timer_activate(tp, TT_REXMT, 0);
810         tp->t_rtttime = 0;
811         /* Send one or 2 segments based on how much new data was acked. */
812         if ((BYTES_THIS_ACK(tp, th) / maxseg) >= 2)
813                 num_segs = 2;
814         tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit +
815             (tp->snd_nxt - tp->snd_recover) + num_segs * maxseg);
816         if (tp->snd_cwnd > tp->snd_ssthresh)
817                 tp->snd_cwnd = tp->snd_ssthresh;
818         tp->t_flags |= TF_ACKNOW;
819         /*
820          * RFC6675 rescue retransmission
821          * Add a hole between th_ack (snd_una is not yet set) and snd_max,
822          * if this was a pure cumulative ACK and no data was send beyond
823          * recovery point. Since the data in the socket has not been freed
824          * at this point, we check if the scoreboard is empty, and the ACK
825          * delivered some new data, indicating a full ACK. Also, if the
826          * recovery point is still at snd_max, we are probably application
827          * limited. However, this inference might not always be true. The
828          * rescue retransmission may rarely be slightly premature
829          * compared to RFC6675.
830          * The corresponding ACK+SACK will cause any further outstanding
831          * segments to be retransmitted. This addresses a corner case, when
832          * the trailing packets of a window are lost and no further data
833          * is available for sending.
834          */
835         if ((V_tcp_do_newsack) &&
836             SEQ_LT(th->th_ack, tp->snd_recover) &&
837             (tp->snd_recover == tp->snd_max) &&
838             TAILQ_EMPTY(&tp->snd_holes) &&
839             (tp->sackhint.delivered_data > 0)) {
840                 /*
841                  * Exclude FIN sequence space in
842                  * the hole for the rescue retransmission,
843                  * and also don't create a hole, if only
844                  * the ACK for a FIN is outstanding.
845                  */
846                 tcp_seq highdata = tp->snd_max;
847                 if (tp->t_flags & TF_SENTFIN)
848                         highdata--;
849                 if (th->th_ack != highdata)
850                         (void)tcp_sackhole_insert(tp, SEQ_MAX(th->th_ack,
851                             highdata - maxseg), highdata, NULL);
852         }
853         (void) tp->t_fb->tfb_tcp_output(tp);
854 }
855
856 #if 0
857 /*
858  * Debug version of tcp_sack_output() that walks the scoreboard.  Used for
859  * now to sanity check the hint.
860  */
861 static struct sackhole *
862 tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt)
863 {
864         struct sackhole *p;
865
866         INP_WLOCK_ASSERT(tp->t_inpcb);
867         *sack_bytes_rexmt = 0;
868         TAILQ_FOREACH(p, &tp->snd_holes, scblink) {
869                 if (SEQ_LT(p->rxmit, p->end)) {
870                         if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
871                                 continue;
872                         }
873                         *sack_bytes_rexmt += (p->rxmit - p->start);
874                         break;
875                 }
876                 *sack_bytes_rexmt += (p->rxmit - p->start);
877         }
878         return (p);
879 }
880 #endif
881
882 /*
883  * Returns the next hole to retransmit and the number of retransmitted bytes
884  * from the scoreboard.  We store both the next hole and the number of
885  * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK
886  * reception).  This avoids scoreboard traversals completely.
887  *
888  * The loop here will traverse *at most* one link.  Here's the argument.  For
889  * the loop to traverse more than 1 link before finding the next hole to
890  * retransmit, we would need to have at least 1 node following the current
891  * hint with (rxmit == end).  But, for all holes following the current hint,
892  * (start == rxmit), since we have not yet retransmitted from them.
893  * Therefore, in order to traverse more 1 link in the loop below, we need to
894  * have at least one node following the current hint with (start == rxmit ==
895  * end).  But that can't happen, (start == end) means that all the data in
896  * that hole has been sacked, in which case, the hole would have been removed
897  * from the scoreboard.
898  */
899 struct sackhole *
900 tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt)
901 {
902         struct sackhole *hole = NULL;
903
904         INP_WLOCK_ASSERT(tp->t_inpcb);
905         *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit;
906         hole = tp->sackhint.nexthole;
907         if (hole == NULL || SEQ_LT(hole->rxmit, hole->end))
908                 goto out;
909         while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) {
910                 if (SEQ_LT(hole->rxmit, hole->end)) {
911                         tp->sackhint.nexthole = hole;
912                         break;
913                 }
914         }
915 out:
916         return (hole);
917 }
918
919 /*
920  * After a timeout, the SACK list may be rebuilt.  This SACK information
921  * should be used to avoid retransmitting SACKed data.  This function
922  * traverses the SACK list to see if snd_nxt should be moved forward.
923  */
924 void
925 tcp_sack_adjust(struct tcpcb *tp)
926 {
927         struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes);
928
929         INP_WLOCK_ASSERT(tp->t_inpcb);
930         if (cur == NULL)
931                 return; /* No holes */
932         if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack))
933                 return; /* We're already beyond any SACKed blocks */
934         /*-
935          * Two cases for which we want to advance snd_nxt:
936          * i) snd_nxt lies between end of one hole and beginning of another
937          * ii) snd_nxt lies between end of last hole and snd_fack
938          */
939         while ((p = TAILQ_NEXT(cur, scblink)) != NULL) {
940                 if (SEQ_LT(tp->snd_nxt, cur->end))
941                         return;
942                 if (SEQ_GEQ(tp->snd_nxt, p->start))
943                         cur = p;
944                 else {
945                         tp->snd_nxt = p->start;
946                         return;
947                 }
948         }
949         if (SEQ_LT(tp->snd_nxt, cur->end))
950                 return;
951         tp->snd_nxt = tp->snd_fack;
952 }