1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 46d90faf3SPaul Saab * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 5632bbf0fSRobert Watson * The Regents of the University of California. 6632bbf0fSRobert Watson * All rights reserved. 76d90faf3SPaul Saab * 86d90faf3SPaul Saab * Redistribution and use in source and binary forms, with or without 96d90faf3SPaul Saab * modification, are permitted provided that the following conditions 106d90faf3SPaul Saab * are met: 116d90faf3SPaul Saab * 1. Redistributions of source code must retain the above copyright 126d90faf3SPaul Saab * notice, this list of conditions and the following disclaimer. 136d90faf3SPaul Saab * 2. Redistributions in binary form must reproduce the above copyright 146d90faf3SPaul Saab * notice, this list of conditions and the following disclaimer in the 156d90faf3SPaul Saab * documentation and/or other materials provided with the distribution. 1651369649SPedro F. Giffuni * 3. Neither the name of the University nor the names of its contributors 176d90faf3SPaul Saab * may be used to endorse or promote products derived from this software 186d90faf3SPaul Saab * without specific prior written permission. 196d90faf3SPaul Saab * 206d90faf3SPaul Saab * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 216d90faf3SPaul Saab * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 226d90faf3SPaul Saab * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 236d90faf3SPaul Saab * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 246d90faf3SPaul Saab * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 256d90faf3SPaul Saab * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 266d90faf3SPaul Saab * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 276d90faf3SPaul Saab * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 286d90faf3SPaul Saab * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 296d90faf3SPaul Saab * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 306d90faf3SPaul Saab * SUCH DAMAGE. 316d90faf3SPaul Saab * 326d90faf3SPaul Saab * @(#)tcp_sack.c 8.12 (Berkeley) 5/24/95 336d90faf3SPaul Saab */ 346d90faf3SPaul Saab 35c398230bSWarner Losh /*- 366d90faf3SPaul Saab * @@(#)COPYRIGHT 1.1 (NRL) 17 January 1995 376d90faf3SPaul Saab * 386d90faf3SPaul Saab * NRL grants permission for redistribution and use in source and binary 396d90faf3SPaul Saab * forms, with or without modification, of the software and documentation 406d90faf3SPaul Saab * created at NRL provided that the following conditions are met: 416d90faf3SPaul Saab * 426d90faf3SPaul Saab * 1. Redistributions of source code must retain the above copyright 436d90faf3SPaul Saab * notice, this list of conditions and the following disclaimer. 446d90faf3SPaul Saab * 2. Redistributions in binary form must reproduce the above copyright 456d90faf3SPaul Saab * notice, this list of conditions and the following disclaimer in the 466d90faf3SPaul Saab * documentation and/or other materials provided with the distribution. 476d90faf3SPaul Saab * 3. All advertising materials mentioning features or use of this software 486d90faf3SPaul Saab * must display the following acknowledgements: 496d90faf3SPaul Saab * This product includes software developed by the University of 506d90faf3SPaul Saab * California, Berkeley and its contributors. 516d90faf3SPaul Saab * This product includes software developed at the Information 526d90faf3SPaul Saab * Technology Division, US Naval Research Laboratory. 536d90faf3SPaul Saab * 4. Neither the name of the NRL nor the names of its contributors 546d90faf3SPaul Saab * may be used to endorse or promote products derived from this software 556d90faf3SPaul Saab * without specific prior written permission. 566d90faf3SPaul Saab * 576d90faf3SPaul Saab * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 586d90faf3SPaul Saab * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 596d90faf3SPaul Saab * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 606d90faf3SPaul Saab * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 616d90faf3SPaul Saab * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 626d90faf3SPaul Saab * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 636d90faf3SPaul Saab * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 646d90faf3SPaul Saab * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 656d90faf3SPaul Saab * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 666d90faf3SPaul Saab * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 676d90faf3SPaul Saab * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 686d90faf3SPaul Saab * 696d90faf3SPaul Saab * The views and conclusions contained in the software and documentation 706d90faf3SPaul Saab * are those of the authors and should not be interpreted as representing 716d90faf3SPaul Saab * official policies, either expressed or implied, of the US Naval 726d90faf3SPaul Saab * Research Laboratory (NRL). 736d90faf3SPaul Saab */ 74632bbf0fSRobert Watson 75632bbf0fSRobert Watson #include <sys/cdefs.h> 76632bbf0fSRobert Watson __FBSDID("$FreeBSD$"); 77632bbf0fSRobert Watson 786d90faf3SPaul Saab #include "opt_inet.h" 796d90faf3SPaul Saab #include "opt_inet6.h" 806d90faf3SPaul Saab #include "opt_tcpdebug.h" 816d90faf3SPaul Saab 826d90faf3SPaul Saab #include <sys/param.h> 836d90faf3SPaul Saab #include <sys/systm.h> 846d90faf3SPaul Saab #include <sys/kernel.h> 856d90faf3SPaul Saab #include <sys/sysctl.h> 866d90faf3SPaul Saab #include <sys/malloc.h> 876d90faf3SPaul Saab #include <sys/mbuf.h> 886d90faf3SPaul Saab #include <sys/proc.h> /* for proc0 declaration */ 896d90faf3SPaul Saab #include <sys/protosw.h> 906d90faf3SPaul Saab #include <sys/socket.h> 916d90faf3SPaul Saab #include <sys/socketvar.h> 926d90faf3SPaul Saab #include <sys/syslog.h> 936d90faf3SPaul Saab #include <sys/systm.h> 946d90faf3SPaul Saab 956d90faf3SPaul Saab #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 966d90faf3SPaul Saab 976d90faf3SPaul Saab #include <vm/uma.h> 986d90faf3SPaul Saab 996d90faf3SPaul Saab #include <net/if.h> 10076039bc8SGleb Smirnoff #include <net/if_var.h> 1016d90faf3SPaul Saab #include <net/route.h> 102530c0060SRobert Watson #include <net/vnet.h> 1036d90faf3SPaul Saab 1046d90faf3SPaul Saab #include <netinet/in.h> 1056d90faf3SPaul Saab #include <netinet/in_systm.h> 1066d90faf3SPaul Saab #include <netinet/ip.h> 1076d90faf3SPaul Saab #include <netinet/in_var.h> 1086d90faf3SPaul Saab #include <netinet/in_pcb.h> 1096d90faf3SPaul Saab #include <netinet/ip_var.h> 1106d90faf3SPaul Saab #include <netinet/ip6.h> 1116d90faf3SPaul Saab #include <netinet/icmp6.h> 1126d90faf3SPaul Saab #include <netinet6/nd6.h> 1136d90faf3SPaul Saab #include <netinet6/ip6_var.h> 1146d90faf3SPaul Saab #include <netinet6/in6_pcb.h> 1156d90faf3SPaul Saab #include <netinet/tcp.h> 1166d90faf3SPaul Saab #include <netinet/tcp_fsm.h> 1176d90faf3SPaul Saab #include <netinet/tcp_seq.h> 1186d90faf3SPaul Saab #include <netinet/tcp_timer.h> 1196d90faf3SPaul Saab #include <netinet/tcp_var.h> 1206d90faf3SPaul Saab #include <netinet6/tcp6_var.h> 1216d90faf3SPaul Saab #include <netinet/tcpip.h> 1226d90faf3SPaul Saab #ifdef TCPDEBUG 1236d90faf3SPaul Saab #include <netinet/tcp_debug.h> 1246d90faf3SPaul Saab #endif /* TCPDEBUG */ 1256d90faf3SPaul Saab 1266d90faf3SPaul Saab #include <machine/in_cksum.h> 1276d90faf3SPaul Saab 128eddfbb76SRobert Watson VNET_DECLARE(struct uma_zone *, sack_hole_zone); 1291e77c105SRobert Watson #define V_sack_hole_zone VNET(sack_hole_zone) 13044e33a07SMarko Zec 1317029da5cSPawel Biernacki SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1327029da5cSPawel Biernacki "TCP SACK"); 13382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_sack) = 1; 13482cea7e6SBjoern A. Zeeb #define V_tcp_do_sack VNET(tcp_do_sack) 1356df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 136eddfbb76SRobert Watson &VNET_NAME(tcp_do_sack), 0, "Enable/Disable TCP SACK support"); 137a55db2b6SPaul Saab 13882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_sack_maxholes) = 128; 1396df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_VNET | CTLFLAG_RW, 140eddfbb76SRobert Watson &VNET_NAME(tcp_sack_maxholes), 0, 141e891d82bSPaul Saab "Maximum number of TCP SACK holes allowed per connection"); 142e891d82bSPaul Saab 14382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_sack_globalmaxholes) = 65536; 1446df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_VNET | CTLFLAG_RW, 145eddfbb76SRobert Watson &VNET_NAME(tcp_sack_globalmaxholes), 0, 146e891d82bSPaul Saab "Global maximum number of TCP SACK holes"); 147e891d82bSPaul Saab 14882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_sack_globalholes) = 0; 1496df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_VNET | CTLFLAG_RD, 150eddfbb76SRobert Watson &VNET_NAME(tcp_sack_globalholes), 0, 151e891d82bSPaul Saab "Global number of TCP SACK holes currently allocated"); 152b7c75571SPaul Saab 153fe5dee73SMichael Tuexen /* 154fe5dee73SMichael Tuexen * This function will find overlaps with the currently stored sackblocks 155fe5dee73SMichael Tuexen * and add any overlap as a dsack block upfront 156fe5dee73SMichael Tuexen */ 157fe5dee73SMichael Tuexen void 158fe5dee73SMichael Tuexen tcp_update_dsack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end) 159fe5dee73SMichael Tuexen { 160fe5dee73SMichael Tuexen struct sackblk head_blk,mid_blk,saved_blks[MAX_SACK_BLKS]; 161fe5dee73SMichael Tuexen int i, j, n, identical; 162fe5dee73SMichael Tuexen tcp_seq start, end; 163fe5dee73SMichael Tuexen 164fe5dee73SMichael Tuexen INP_WLOCK_ASSERT(tp->t_inpcb); 165fe5dee73SMichael Tuexen 166fe5dee73SMichael Tuexen KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end")); 167fe5dee73SMichael Tuexen 168fe5dee73SMichael Tuexen if (SEQ_LT(rcv_end, tp->rcv_nxt) || 169fe5dee73SMichael Tuexen ((rcv_end == tp->rcv_nxt) && 170fe5dee73SMichael Tuexen (tp->rcv_numsacks > 0 ) && 171fe5dee73SMichael Tuexen (tp->sackblks[0].end == tp->rcv_nxt))) { 172fe5dee73SMichael Tuexen saved_blks[0].start = rcv_start; 173fe5dee73SMichael Tuexen saved_blks[0].end = rcv_end; 174fe5dee73SMichael Tuexen } else { 175fe5dee73SMichael Tuexen saved_blks[0].start = saved_blks[0].end = 0; 176fe5dee73SMichael Tuexen } 177fe5dee73SMichael Tuexen 178fe5dee73SMichael Tuexen head_blk.start = head_blk.end = 0; 179fe5dee73SMichael Tuexen mid_blk.start = rcv_start; 180fe5dee73SMichael Tuexen mid_blk.end = rcv_end; 181fe5dee73SMichael Tuexen identical = 0; 182fe5dee73SMichael Tuexen 183fe5dee73SMichael Tuexen for (i = 0; i < tp->rcv_numsacks; i++) { 184fe5dee73SMichael Tuexen start = tp->sackblks[i].start; 185fe5dee73SMichael Tuexen end = tp->sackblks[i].end; 186fe5dee73SMichael Tuexen if (SEQ_LT(rcv_end, start)) { 187fe5dee73SMichael Tuexen /* pkt left to sack blk */ 188fe5dee73SMichael Tuexen continue; 189fe5dee73SMichael Tuexen } 190fe5dee73SMichael Tuexen if (SEQ_GT(rcv_start, end)) { 191fe5dee73SMichael Tuexen /* pkt right to sack blk */ 192fe5dee73SMichael Tuexen continue; 193fe5dee73SMichael Tuexen } 194fe5dee73SMichael Tuexen if (SEQ_GT(tp->rcv_nxt, end)) { 195fe5dee73SMichael Tuexen if ((SEQ_MAX(rcv_start, start) != SEQ_MIN(rcv_end, end)) && 196fe5dee73SMichael Tuexen (SEQ_GT(head_blk.start, SEQ_MAX(rcv_start, start)) || 197fe5dee73SMichael Tuexen (head_blk.start == head_blk.end))) { 198fe5dee73SMichael Tuexen head_blk.start = SEQ_MAX(rcv_start, start); 199fe5dee73SMichael Tuexen head_blk.end = SEQ_MIN(rcv_end, end); 200fe5dee73SMichael Tuexen } 201fe5dee73SMichael Tuexen continue; 202fe5dee73SMichael Tuexen } 203fe5dee73SMichael Tuexen if (((head_blk.start == head_blk.end) || 204fe5dee73SMichael Tuexen SEQ_LT(start, head_blk.start)) && 205fe5dee73SMichael Tuexen (SEQ_GT(end, rcv_start) && 206fe5dee73SMichael Tuexen SEQ_LEQ(start, rcv_end))) { 207fe5dee73SMichael Tuexen head_blk.start = start; 208fe5dee73SMichael Tuexen head_blk.end = end; 209fe5dee73SMichael Tuexen } 210fe5dee73SMichael Tuexen mid_blk.start = SEQ_MIN(mid_blk.start, start); 211fe5dee73SMichael Tuexen mid_blk.end = SEQ_MAX(mid_blk.end, end); 212fe5dee73SMichael Tuexen if ((mid_blk.start == start) && 213fe5dee73SMichael Tuexen (mid_blk.end == end)) 214fe5dee73SMichael Tuexen identical = 1; 215fe5dee73SMichael Tuexen } 216fe5dee73SMichael Tuexen if (SEQ_LT(head_blk.start, head_blk.end)) { 217fe5dee73SMichael Tuexen /* store overlapping range */ 218fe5dee73SMichael Tuexen saved_blks[0].start = SEQ_MAX(rcv_start, head_blk.start); 219fe5dee73SMichael Tuexen saved_blks[0].end = SEQ_MIN(rcv_end, head_blk.end); 220fe5dee73SMichael Tuexen } 221fe5dee73SMichael Tuexen n = 1; 222fe5dee73SMichael Tuexen /* 223fe5dee73SMichael Tuexen * Second, if not ACKed, store the SACK block that 224fe5dee73SMichael Tuexen * overlaps with the DSACK block unless it is identical 225fe5dee73SMichael Tuexen */ 226fe5dee73SMichael Tuexen if ((SEQ_LT(tp->rcv_nxt, mid_blk.end) && 227fe5dee73SMichael Tuexen !((mid_blk.start == saved_blks[0].start) && 228fe5dee73SMichael Tuexen (mid_blk.end == saved_blks[0].end))) || 229fe5dee73SMichael Tuexen identical == 1) { 230fe5dee73SMichael Tuexen saved_blks[n].start = mid_blk.start; 231fe5dee73SMichael Tuexen saved_blks[n++].end = mid_blk.end; 232fe5dee73SMichael Tuexen } 2335b66b7f1SMichael Tuexen for (j = 0; (j < tp->rcv_numsacks) && (n < MAX_SACK_BLKS); j++) { 234fe5dee73SMichael Tuexen if (((SEQ_LT(tp->sackblks[j].end, mid_blk.start) || 235fe5dee73SMichael Tuexen SEQ_GT(tp->sackblks[j].start, mid_blk.end)) && 236fe5dee73SMichael Tuexen (SEQ_GT(tp->sackblks[j].start, tp->rcv_nxt)))) 237fe5dee73SMichael Tuexen saved_blks[n++] = tp->sackblks[j]; 238fe5dee73SMichael Tuexen } 239fe5dee73SMichael Tuexen j = 0; 240fe5dee73SMichael Tuexen for (i = 0; i < n; i++) { 2415aa0576bSEd Maste /* we can end up with a stale initial entry */ 242fe5dee73SMichael Tuexen if (SEQ_LT(saved_blks[i].start, saved_blks[i].end)) { 243fe5dee73SMichael Tuexen tp->sackblks[j++] = saved_blks[i]; 244fe5dee73SMichael Tuexen } 245fe5dee73SMichael Tuexen } 246fe5dee73SMichael Tuexen tp->rcv_numsacks = j; 247fe5dee73SMichael Tuexen } 248fe5dee73SMichael Tuexen 2496d90faf3SPaul Saab /* 250632bbf0fSRobert Watson * This function is called upon receipt of new valid data (while not in 251632bbf0fSRobert Watson * header prediction mode), and it updates the ordered list of sacks. 2526d90faf3SPaul Saab */ 2536d90faf3SPaul Saab void 254b7c75571SPaul Saab tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end) 2556d90faf3SPaul Saab { 2566d90faf3SPaul Saab /* 2576d90faf3SPaul Saab * First reported block MUST be the most recent one. Subsequent 2586d90faf3SPaul Saab * blocks SHOULD be in the order in which they arrived at the 2596d90faf3SPaul Saab * receiver. These two conditions make the implementation fully 2606d90faf3SPaul Saab * compliant with RFC 2018. 2616d90faf3SPaul Saab */ 262b7c75571SPaul Saab struct sackblk head_blk, saved_blks[MAX_SACK_BLKS]; 263b7c75571SPaul Saab int num_head, num_saved, i; 2646d90faf3SPaul Saab 2658501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 266b7c75571SPaul Saab 267632bbf0fSRobert Watson /* Check arguments. */ 2685acfd95cSMichael Tuexen KASSERT(SEQ_LEQ(rcv_start, rcv_end), ("rcv_start <= rcv_end")); 269b7c75571SPaul Saab 270fe5dee73SMichael Tuexen if ((rcv_start == rcv_end) && 271fe5dee73SMichael Tuexen (tp->rcv_numsacks >= 1) && 272fe5dee73SMichael Tuexen (rcv_end == tp->sackblks[0].end)) { 273fe5dee73SMichael Tuexen /* retaining DSACK block below rcv_nxt (todrop) */ 274fe5dee73SMichael Tuexen head_blk = tp->sackblks[0]; 275fe5dee73SMichael Tuexen } else { 276b7c75571SPaul Saab /* SACK block for the received segment. */ 277b7c75571SPaul Saab head_blk.start = rcv_start; 278b7c75571SPaul Saab head_blk.end = rcv_end; 279fe5dee73SMichael Tuexen } 280b7c75571SPaul Saab 281b7c75571SPaul Saab /* 282632bbf0fSRobert Watson * Merge updated SACK blocks into head_blk, and save unchanged SACK 283632bbf0fSRobert Watson * blocks into saved_blks[]. num_saved will have the number of the 284632bbf0fSRobert Watson * saved SACK blocks. 285b7c75571SPaul Saab */ 286b7c75571SPaul Saab num_saved = 0; 2876d90faf3SPaul Saab for (i = 0; i < tp->rcv_numsacks; i++) { 288b7c75571SPaul Saab tcp_seq start = tp->sackblks[i].start; 289b7c75571SPaul Saab tcp_seq end = tp->sackblks[i].end; 290b7c75571SPaul Saab if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) { 291b7c75571SPaul Saab /* 292b7c75571SPaul Saab * Discard this SACK block. 293b7c75571SPaul Saab */ 294b7c75571SPaul Saab } else if (SEQ_LEQ(head_blk.start, end) && 295b7c75571SPaul Saab SEQ_GEQ(head_blk.end, start)) { 296b7c75571SPaul Saab /* 297632bbf0fSRobert Watson * Merge this SACK block into head_blk. This SACK 298632bbf0fSRobert Watson * block itself will be discarded. 299b7c75571SPaul Saab */ 3005acfd95cSMichael Tuexen /* 3015acfd95cSMichael Tuexen * |-| 3025acfd95cSMichael Tuexen * |---| merge 3035acfd95cSMichael Tuexen * 3045acfd95cSMichael Tuexen * |-| 3055acfd95cSMichael Tuexen * |---| merge 3065acfd95cSMichael Tuexen * 3075acfd95cSMichael Tuexen * |-----| 3085acfd95cSMichael Tuexen * |-| DSACK smaller 3095acfd95cSMichael Tuexen * 3105acfd95cSMichael Tuexen * |-| 3115acfd95cSMichael Tuexen * |-----| DSACK smaller 3125acfd95cSMichael Tuexen */ 3135acfd95cSMichael Tuexen if (head_blk.start == end) 314b7c75571SPaul Saab head_blk.start = start; 3155acfd95cSMichael Tuexen else if (head_blk.end == start) 316b7c75571SPaul Saab head_blk.end = end; 3175acfd95cSMichael Tuexen else { 3185acfd95cSMichael Tuexen if (SEQ_LT(head_blk.start, start)) { 3195acfd95cSMichael Tuexen tcp_seq temp = start; 3205acfd95cSMichael Tuexen start = head_blk.start; 3215acfd95cSMichael Tuexen head_blk.start = temp; 3225acfd95cSMichael Tuexen } 3235acfd95cSMichael Tuexen if (SEQ_GT(head_blk.end, end)) { 3245acfd95cSMichael Tuexen tcp_seq temp = end; 3255acfd95cSMichael Tuexen end = head_blk.end; 3265acfd95cSMichael Tuexen head_blk.end = temp; 3275acfd95cSMichael Tuexen } 3285acfd95cSMichael Tuexen if ((head_blk.start != start) || 3295acfd95cSMichael Tuexen (head_blk.end != end)) { 3305acfd95cSMichael Tuexen if ((num_saved >= 1) && 3315acfd95cSMichael Tuexen SEQ_GEQ(saved_blks[num_saved-1].start, start) && 3325acfd95cSMichael Tuexen SEQ_LEQ(saved_blks[num_saved-1].end, end)) 3335acfd95cSMichael Tuexen num_saved--; 3345acfd95cSMichael Tuexen saved_blks[num_saved].start = start; 3355acfd95cSMichael Tuexen saved_blks[num_saved].end = end; 3365acfd95cSMichael Tuexen num_saved++; 3375acfd95cSMichael Tuexen } 3385acfd95cSMichael Tuexen } 3396d90faf3SPaul Saab } else { 3406d90faf3SPaul Saab /* 3415acfd95cSMichael Tuexen * This block supercedes the prior block 3425acfd95cSMichael Tuexen */ 3435acfd95cSMichael Tuexen if ((num_saved >= 1) && 3445acfd95cSMichael Tuexen SEQ_GEQ(saved_blks[num_saved-1].start, start) && 3455acfd95cSMichael Tuexen SEQ_LEQ(saved_blks[num_saved-1].end, end)) 3465acfd95cSMichael Tuexen num_saved--; 3475acfd95cSMichael Tuexen /* 348b7c75571SPaul Saab * Save this SACK block. 3496d90faf3SPaul Saab */ 350b7c75571SPaul Saab saved_blks[num_saved].start = start; 351b7c75571SPaul Saab saved_blks[num_saved].end = end; 352b7c75571SPaul Saab num_saved++; 353b7c75571SPaul Saab } 354b7c75571SPaul Saab } 355b7c75571SPaul Saab 3566d90faf3SPaul Saab /* 357b7c75571SPaul Saab * Update SACK list in tp->sackblks[]. 3586d90faf3SPaul Saab */ 359b7c75571SPaul Saab num_head = 0; 3605acfd95cSMichael Tuexen if (SEQ_LT(rcv_start, rcv_end)) { 361b7c75571SPaul Saab /* 362632bbf0fSRobert Watson * The received data segment is an out-of-order segment. Put 363632bbf0fSRobert Watson * head_blk at the top of SACK list. 364b7c75571SPaul Saab */ 365b7c75571SPaul Saab tp->sackblks[0] = head_blk; 366b7c75571SPaul Saab num_head = 1; 367b7c75571SPaul Saab /* 368b7c75571SPaul Saab * If the number of saved SACK blocks exceeds its limit, 369b7c75571SPaul Saab * discard the last SACK block. 370b7c75571SPaul Saab */ 371b7c75571SPaul Saab if (num_saved >= MAX_SACK_BLKS) 372b7c75571SPaul Saab num_saved--; 3736d90faf3SPaul Saab } 374fe5dee73SMichael Tuexen if ((rcv_start == rcv_end) && 375fe5dee73SMichael Tuexen (rcv_start == tp->sackblks[0].end)) { 376fe5dee73SMichael Tuexen num_head = 1; 377fe5dee73SMichael Tuexen } 378b7c75571SPaul Saab if (num_saved > 0) { 379b7c75571SPaul Saab /* 380b7c75571SPaul Saab * Copy the saved SACK blocks back. 381b7c75571SPaul Saab */ 382b7c75571SPaul Saab bcopy(saved_blks, &tp->sackblks[num_head], 383b7c75571SPaul Saab sizeof(struct sackblk) * num_saved); 3846d90faf3SPaul Saab } 385b7c75571SPaul Saab 386b7c75571SPaul Saab /* Save the number of SACK blocks. */ 387b7c75571SPaul Saab tp->rcv_numsacks = num_head + num_saved; 3886d90faf3SPaul Saab } 3896d90faf3SPaul Saab 390e5926fd3SRandall Stewart void 391e5926fd3SRandall Stewart tcp_clean_dsack_blocks(struct tcpcb *tp) 392e5926fd3SRandall Stewart { 393e5926fd3SRandall Stewart struct sackblk saved_blks[MAX_SACK_BLKS]; 394e5926fd3SRandall Stewart int num_saved, i; 395e5926fd3SRandall Stewart 396e5926fd3SRandall Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 397e5926fd3SRandall Stewart /* 398e5926fd3SRandall Stewart * Clean up any DSACK blocks that 399e5926fd3SRandall Stewart * are in our queue of sack blocks. 400e5926fd3SRandall Stewart * 401e5926fd3SRandall Stewart */ 402e5926fd3SRandall Stewart num_saved = 0; 403e5926fd3SRandall Stewart for (i = 0; i < tp->rcv_numsacks; i++) { 404e5926fd3SRandall Stewart tcp_seq start = tp->sackblks[i].start; 405e5926fd3SRandall Stewart tcp_seq end = tp->sackblks[i].end; 406e5926fd3SRandall Stewart if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) { 407e5926fd3SRandall Stewart /* 408e5926fd3SRandall Stewart * Discard this D-SACK block. 409e5926fd3SRandall Stewart */ 410e5926fd3SRandall Stewart continue; 411e5926fd3SRandall Stewart } 412e5926fd3SRandall Stewart /* 413e5926fd3SRandall Stewart * Save this SACK block. 414e5926fd3SRandall Stewart */ 415e5926fd3SRandall Stewart saved_blks[num_saved].start = start; 416e5926fd3SRandall Stewart saved_blks[num_saved].end = end; 417e5926fd3SRandall Stewart num_saved++; 418e5926fd3SRandall Stewart } 419e5926fd3SRandall Stewart if (num_saved > 0) { 420e5926fd3SRandall Stewart /* 421e5926fd3SRandall Stewart * Copy the saved SACK blocks back. 422e5926fd3SRandall Stewart */ 423e5926fd3SRandall Stewart bcopy(saved_blks, &tp->sackblks[0], 424e5926fd3SRandall Stewart sizeof(struct sackblk) * num_saved); 425e5926fd3SRandall Stewart } 426e5926fd3SRandall Stewart tp->rcv_numsacks = num_saved; 427e5926fd3SRandall Stewart } 428e5926fd3SRandall Stewart 4296d90faf3SPaul Saab /* 4306d90faf3SPaul Saab * Delete all receiver-side SACK information. 4316d90faf3SPaul Saab */ 4326d90faf3SPaul Saab void 433f2565d68SRobert Watson tcp_clean_sackreport(struct tcpcb *tp) 4346d90faf3SPaul Saab { 4356d90faf3SPaul Saab int i; 4366d90faf3SPaul Saab 4378501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 4386d90faf3SPaul Saab tp->rcv_numsacks = 0; 4396d90faf3SPaul Saab for (i = 0; i < MAX_SACK_BLKS; i++) 4406d90faf3SPaul Saab tp->sackblks[i].start = tp->sackblks[i].end=0; 4416d90faf3SPaul Saab } 4426d90faf3SPaul Saab 4436d90faf3SPaul Saab /* 4444fc53245SPaul Saab * Allocate struct sackhole. 4454fc53245SPaul Saab */ 4464fc53245SPaul Saab static struct sackhole * 4474fc53245SPaul Saab tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end) 4484fc53245SPaul Saab { 4494fc53245SPaul Saab struct sackhole *hole; 4504fc53245SPaul Saab 451603724d3SBjoern A. Zeeb if (tp->snd_numholes >= V_tcp_sack_maxholes || 452603724d3SBjoern A. Zeeb V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) { 45378b50714SRobert Watson TCPSTAT_INC(tcps_sack_sboverflow); 4544fc53245SPaul Saab return NULL; 4554fc53245SPaul Saab } 4564fc53245SPaul Saab 4571ed81b73SMarko Zec hole = (struct sackhole *)uma_zalloc(V_sack_hole_zone, M_NOWAIT); 4584fc53245SPaul Saab if (hole == NULL) 4594fc53245SPaul Saab return NULL; 4604fc53245SPaul Saab 4614fc53245SPaul Saab hole->start = start; 4624fc53245SPaul Saab hole->end = end; 4634fc53245SPaul Saab hole->rxmit = start; 4644fc53245SPaul Saab 4654fc53245SPaul Saab tp->snd_numholes++; 46691a5ebdeSLawrence Stewart atomic_add_int(&V_tcp_sack_globalholes, 1); 4674fc53245SPaul Saab 4684fc53245SPaul Saab return hole; 4694fc53245SPaul Saab } 4704fc53245SPaul Saab 4714fc53245SPaul Saab /* 4724fc53245SPaul Saab * Free struct sackhole. 4734fc53245SPaul Saab */ 4744fc53245SPaul Saab static void 4754fc53245SPaul Saab tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole) 4764fc53245SPaul Saab { 477632bbf0fSRobert Watson 4781ed81b73SMarko Zec uma_zfree(V_sack_hole_zone, hole); 4794fc53245SPaul Saab 4804fc53245SPaul Saab tp->snd_numholes--; 48191a5ebdeSLawrence Stewart atomic_subtract_int(&V_tcp_sack_globalholes, 1); 4824fc53245SPaul Saab 4834fc53245SPaul Saab KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0")); 484603724d3SBjoern A. Zeeb KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0")); 4854fc53245SPaul Saab } 4864fc53245SPaul Saab 4874fc53245SPaul Saab /* 4889d17a7a6SPaul Saab * Insert new SACK hole into scoreboard. 4899d17a7a6SPaul Saab */ 4909d17a7a6SPaul Saab static struct sackhole * 4919d17a7a6SPaul Saab tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end, 4929d17a7a6SPaul Saab struct sackhole *after) 4939d17a7a6SPaul Saab { 4949d17a7a6SPaul Saab struct sackhole *hole; 4959d17a7a6SPaul Saab 4969d17a7a6SPaul Saab /* Allocate a new SACK hole. */ 4979d17a7a6SPaul Saab hole = tcp_sackhole_alloc(tp, start, end); 4989d17a7a6SPaul Saab if (hole == NULL) 4999d17a7a6SPaul Saab return NULL; 5009d17a7a6SPaul Saab 501632bbf0fSRobert Watson /* Insert the new SACK hole into scoreboard. */ 5029d17a7a6SPaul Saab if (after != NULL) 5039d17a7a6SPaul Saab TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink); 5049d17a7a6SPaul Saab else 5059d17a7a6SPaul Saab TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink); 5069d17a7a6SPaul Saab 5079d17a7a6SPaul Saab /* Update SACK hint. */ 5089d17a7a6SPaul Saab if (tp->sackhint.nexthole == NULL) 5099d17a7a6SPaul Saab tp->sackhint.nexthole = hole; 5109d17a7a6SPaul Saab 5119d17a7a6SPaul Saab return hole; 5129d17a7a6SPaul Saab } 5139d17a7a6SPaul Saab 5149d17a7a6SPaul Saab /* 5159d17a7a6SPaul Saab * Remove SACK hole from scoreboard. 5169d17a7a6SPaul Saab */ 5179d17a7a6SPaul Saab static void 5189d17a7a6SPaul Saab tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole) 5199d17a7a6SPaul Saab { 520632bbf0fSRobert Watson 5219d17a7a6SPaul Saab /* Update SACK hint. */ 5229d17a7a6SPaul Saab if (tp->sackhint.nexthole == hole) 5239d17a7a6SPaul Saab tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink); 5249d17a7a6SPaul Saab 5259d17a7a6SPaul Saab /* Remove this SACK hole. */ 5269d17a7a6SPaul Saab TAILQ_REMOVE(&tp->snd_holes, hole, scblink); 5279d17a7a6SPaul Saab 5289d17a7a6SPaul Saab /* Free this SACK hole. */ 5299d17a7a6SPaul Saab tcp_sackhole_free(tp, hole); 5309d17a7a6SPaul Saab } 5319d17a7a6SPaul Saab 5329d17a7a6SPaul Saab /* 5335a53ca16SPaul Saab * Process cumulative ACK and the TCP SACK option to update the scoreboard. 5345a53ca16SPaul Saab * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of 5355a53ca16SPaul Saab * the sequence space). 536021eaf79SHiren Panchasara * Returns 1 if incoming ACK has previously unknown SACK information, 537f359d6ebSRichard Scheffenegger * 0 otherwise. 5386d90faf3SPaul Saab */ 539021eaf79SHiren Panchasara int 5405a53ca16SPaul Saab tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack) 5416d90faf3SPaul Saab { 542a6235da6SPaul Saab struct sackhole *cur, *temp; 5435a53ca16SPaul Saab struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp; 544021eaf79SHiren Panchasara int i, j, num_sack_blks, sack_changed; 545f359d6ebSRichard Scheffenegger int delivered_data, left_edge_delta; 5466d90faf3SPaul Saab 5478501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 5485a53ca16SPaul Saab 54964b5fbaaSPaul Saab num_sack_blks = 0; 550021eaf79SHiren Panchasara sack_changed = 0; 551f359d6ebSRichard Scheffenegger delivered_data = 0; 552f359d6ebSRichard Scheffenegger left_edge_delta = 0; 5535a53ca16SPaul Saab /* 5545a53ca16SPaul Saab * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist, 5555a53ca16SPaul Saab * treat [SND.UNA, SEG.ACK) as if it is a SACK block. 556f359d6ebSRichard Scheffenegger * Account changes to SND.UNA always in delivered data. 5575a53ca16SPaul Saab */ 5585a53ca16SPaul Saab if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) { 559f359d6ebSRichard Scheffenegger left_edge_delta = th_ack - tp->snd_una; 5605a53ca16SPaul Saab sack_blocks[num_sack_blks].start = tp->snd_una; 5615a53ca16SPaul Saab sack_blocks[num_sack_blks++].end = th_ack; 5625a53ca16SPaul Saab } 5635a53ca16SPaul Saab /* 564632bbf0fSRobert Watson * Append received valid SACK blocks to sack_blocks[], but only if we 565632bbf0fSRobert Watson * received new blocks from the other side. 5665a53ca16SPaul Saab */ 56707b64b90SAndre Oppermann if (to->to_flags & TOF_SACK) { 5685a53ca16SPaul Saab for (i = 0; i < to->to_nsacks; i++) { 56907b64b90SAndre Oppermann bcopy((to->to_sacks + i * TCPOLEN_SACK), 57007b64b90SAndre Oppermann &sack, sizeof(sack)); 5716d90faf3SPaul Saab sack.start = ntohl(sack.start); 5726d90faf3SPaul Saab sack.end = ntohl(sack.end); 57364b5fbaaSPaul Saab if (SEQ_GT(sack.end, sack.start) && 57464b5fbaaSPaul Saab SEQ_GT(sack.start, tp->snd_una) && 5755a53ca16SPaul Saab SEQ_GT(sack.start, th_ack) && 5761f65c2cdSMohan Srinivasan SEQ_LT(sack.start, tp->snd_max) && 5771f65c2cdSMohan Srinivasan SEQ_GT(sack.end, tp->snd_una) && 57812eeb81fSHiren Panchasara SEQ_LEQ(sack.end, tp->snd_max)) { 57964b5fbaaSPaul Saab sack_blocks[num_sack_blks++] = sack; 58012eeb81fSHiren Panchasara } 5816d90faf3SPaul Saab } 58207b64b90SAndre Oppermann } 5835a53ca16SPaul Saab /* 584632bbf0fSRobert Watson * Return if SND.UNA is not advanced and no valid SACK block is 585632bbf0fSRobert Watson * received. 5865a53ca16SPaul Saab */ 58764b5fbaaSPaul Saab if (num_sack_blks == 0) 588021eaf79SHiren Panchasara return (sack_changed); 5895a53ca16SPaul Saab 5905a53ca16SPaul Saab /* 591632bbf0fSRobert Watson * Sort the SACK blocks so we can update the scoreboard with just one 592632bbf0fSRobert Watson * pass. The overhead of sorting up to 4+1 elements is less than 593632bbf0fSRobert Watson * making up to 4+1 passes over the scoreboard. 5945a53ca16SPaul Saab */ 59564b5fbaaSPaul Saab for (i = 0; i < num_sack_blks; i++) { 59664b5fbaaSPaul Saab for (j = i + 1; j < num_sack_blks; j++) { 5979d17a7a6SPaul Saab if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) { 59864b5fbaaSPaul Saab sack = sack_blocks[i]; 59964b5fbaaSPaul Saab sack_blocks[i] = sack_blocks[j]; 60064b5fbaaSPaul Saab sack_blocks[j] = sack; 6016d90faf3SPaul Saab } 6026d90faf3SPaul Saab } 60364b5fbaaSPaul Saab } 604f359d6ebSRichard Scheffenegger if (TAILQ_EMPTY(&tp->snd_holes)) { 60564b5fbaaSPaul Saab /* 606808f11b7SPaul Saab * Empty scoreboard. Need to initialize snd_fack (it may be 60764b5fbaaSPaul Saab * uninitialized or have a bogus value). Scoreboard holes 608632bbf0fSRobert Watson * (from the sack blocks received) are created later below 609632bbf0fSRobert Watson * (in the logic that adds holes to the tail of the 610632bbf0fSRobert Watson * scoreboard). 61164b5fbaaSPaul Saab */ 6125a53ca16SPaul Saab tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack); 613f359d6ebSRichard Scheffenegger tp->sackhint.sacked_bytes = 0; /* reset */ 614f359d6ebSRichard Scheffenegger } 6159d17a7a6SPaul Saab /* 616632bbf0fSRobert Watson * In the while-loop below, incoming SACK blocks (sack_blocks[]) and 617632bbf0fSRobert Watson * SACK holes (snd_holes) are traversed from their tails with just 618632bbf0fSRobert Watson * one pass in order to reduce the number of compares especially when 619632bbf0fSRobert Watson * the bandwidth-delay product is large. 620632bbf0fSRobert Watson * 6219d17a7a6SPaul Saab * Note: Typically, in the first RTT of SACK recovery, the highest 6229d17a7a6SPaul Saab * three or four SACK blocks with the same ack number are received. 6239d17a7a6SPaul Saab * In the second RTT, if retransmitted data segments are not lost, 6249d17a7a6SPaul Saab * the highest three or four SACK blocks with ack number advancing 6259d17a7a6SPaul Saab * are received. 6269d17a7a6SPaul Saab */ 6279d17a7a6SPaul Saab sblkp = &sack_blocks[num_sack_blks - 1]; /* Last SACK block */ 628bee9ab2bSLawrence Stewart tp->sackhint.last_sack_ack = sblkp->end; 6299d17a7a6SPaul Saab if (SEQ_LT(tp->snd_fack, sblkp->start)) { 6309d17a7a6SPaul Saab /* 631632bbf0fSRobert Watson * The highest SACK block is beyond fack. Append new SACK 632632bbf0fSRobert Watson * hole at the tail. If the second or later highest SACK 633632bbf0fSRobert Watson * blocks are also beyond the current fack, they will be 634632bbf0fSRobert Watson * inserted by way of hole splitting in the while-loop below. 6359d17a7a6SPaul Saab */ 6368b9bbaaaSPaul Saab temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL); 637d0a14f55SPaul Saab if (temp != NULL) { 638f359d6ebSRichard Scheffenegger delivered_data += sblkp->end - sblkp->start; 6399d17a7a6SPaul Saab tp->snd_fack = sblkp->end; 6409d17a7a6SPaul Saab /* Go to the previous sack block. */ 6419d17a7a6SPaul Saab sblkp--; 642021eaf79SHiren Panchasara sack_changed = 1; 643d0a14f55SPaul Saab } else { 644d0a14f55SPaul Saab /* 645d0a14f55SPaul Saab * We failed to add a new hole based on the current 646d0a14f55SPaul Saab * sack block. Skip over all the sack blocks that 647632bbf0fSRobert Watson * fall completely to the right of snd_fack and 648632bbf0fSRobert Watson * proceed to trim the scoreboard based on the 649632bbf0fSRobert Watson * remaining sack blocks. This also trims the 650632bbf0fSRobert Watson * scoreboard for th_ack (which is sack_blocks[0]). 651d0a14f55SPaul Saab */ 652d0a14f55SPaul Saab while (sblkp >= sack_blocks && 653d0a14f55SPaul Saab SEQ_LT(tp->snd_fack, sblkp->start)) 654d0a14f55SPaul Saab sblkp--; 655d0a14f55SPaul Saab if (sblkp >= sack_blocks && 656f359d6ebSRichard Scheffenegger SEQ_LT(tp->snd_fack, sblkp->end)) { 657f359d6ebSRichard Scheffenegger delivered_data += sblkp->end - tp->snd_fack; 658d0a14f55SPaul Saab tp->snd_fack = sblkp->end; 659f359d6ebSRichard Scheffenegger sack_changed = 1; 660f359d6ebSRichard Scheffenegger } 661d0a14f55SPaul Saab } 662021eaf79SHiren Panchasara } else if (SEQ_LT(tp->snd_fack, sblkp->end)) { 6639d17a7a6SPaul Saab /* fack is advanced. */ 664f359d6ebSRichard Scheffenegger delivered_data += sblkp->end - tp->snd_fack; 6659d17a7a6SPaul Saab tp->snd_fack = sblkp->end; 666021eaf79SHiren Panchasara sack_changed = 1; 667021eaf79SHiren Panchasara } 668632bbf0fSRobert Watson cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */ 66964b5fbaaSPaul Saab /* 67064b5fbaaSPaul Saab * Since the incoming sack blocks are sorted, we can process them 67164b5fbaaSPaul Saab * making one sweep of the scoreboard. 67264b5fbaaSPaul Saab */ 673d0a14f55SPaul Saab while (sblkp >= sack_blocks && cur != NULL) { 6749d17a7a6SPaul Saab if (SEQ_GEQ(sblkp->start, cur->end)) { 67564b5fbaaSPaul Saab /* 676632bbf0fSRobert Watson * SACKs data beyond the current hole. Go to the 677632bbf0fSRobert Watson * previous sack block. 67864b5fbaaSPaul Saab */ 6799d17a7a6SPaul Saab sblkp--; 6809d17a7a6SPaul Saab continue; 6819d17a7a6SPaul Saab } 6829d17a7a6SPaul Saab if (SEQ_LEQ(sblkp->end, cur->start)) { 6839d17a7a6SPaul Saab /* 684632bbf0fSRobert Watson * SACKs data before the current hole. Go to the 685632bbf0fSRobert Watson * previous hole. 6869d17a7a6SPaul Saab */ 6879d17a7a6SPaul Saab cur = TAILQ_PREV(cur, sackhole_head, scblink); 6886d90faf3SPaul Saab continue; 6896d90faf3SPaul Saab } 69064b5fbaaSPaul Saab tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start); 6910077b016SPaul Saab KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 6920077b016SPaul Saab ("sackhint bytes rtx >= 0")); 693021eaf79SHiren Panchasara sack_changed = 1; 6949d17a7a6SPaul Saab if (SEQ_LEQ(sblkp->start, cur->start)) { 695632bbf0fSRobert Watson /* Data acks at least the beginning of hole. */ 6969d17a7a6SPaul Saab if (SEQ_GEQ(sblkp->end, cur->end)) { 697632bbf0fSRobert Watson /* Acks entire hole, so delete hole. */ 698f359d6ebSRichard Scheffenegger delivered_data += (cur->end - cur->start); 699a6235da6SPaul Saab temp = cur; 7009d17a7a6SPaul Saab cur = TAILQ_PREV(cur, sackhole_head, scblink); 7019d17a7a6SPaul Saab tcp_sackhole_remove(tp, temp); 70264b5fbaaSPaul Saab /* 703632bbf0fSRobert Watson * The sack block may ack all or part of the 704632bbf0fSRobert Watson * next hole too, so continue onto the next 705632bbf0fSRobert Watson * hole. 70664b5fbaaSPaul Saab */ 7076d90faf3SPaul Saab continue; 7080077b016SPaul Saab } else { 709632bbf0fSRobert Watson /* Move start of hole forward. */ 710f359d6ebSRichard Scheffenegger delivered_data += (sblkp->end - cur->start); 7119d17a7a6SPaul Saab cur->start = sblkp->end; 7126d90faf3SPaul Saab cur->rxmit = SEQ_MAX(cur->rxmit, cur->start); 7136d90faf3SPaul Saab } 71464b5fbaaSPaul Saab } else { 715632bbf0fSRobert Watson /* Data acks at least the end of hole. */ 7169d17a7a6SPaul Saab if (SEQ_GEQ(sblkp->end, cur->end)) { 717632bbf0fSRobert Watson /* Move end of hole backward. */ 718f359d6ebSRichard Scheffenegger delivered_data += (cur->end - sblkp->start); 7199d17a7a6SPaul Saab cur->end = sblkp->start; 7206d90faf3SPaul Saab cur->rxmit = SEQ_MIN(cur->rxmit, cur->end); 7210077b016SPaul Saab } else { 7226d90faf3SPaul Saab /* 723632bbf0fSRobert Watson * ACKs some data in middle of a hole; need 724632bbf0fSRobert Watson * to split current hole 7256d90faf3SPaul Saab */ 7269d17a7a6SPaul Saab temp = tcp_sackhole_insert(tp, sblkp->end, 7279d17a7a6SPaul Saab cur->end, cur); 7280077b016SPaul Saab if (temp != NULL) { 7299d17a7a6SPaul Saab if (SEQ_GT(cur->rxmit, temp->rxmit)) { 7304fc53245SPaul Saab temp->rxmit = cur->rxmit; 7319d17a7a6SPaul Saab tp->sackhint.sack_bytes_rexmit 7329d17a7a6SPaul Saab += (temp->rxmit 7339d17a7a6SPaul Saab - temp->start); 7349d17a7a6SPaul Saab } 7359d17a7a6SPaul Saab cur->end = sblkp->start; 7369d17a7a6SPaul Saab cur->rxmit = SEQ_MIN(cur->rxmit, 7379d17a7a6SPaul Saab cur->end); 738f359d6ebSRichard Scheffenegger delivered_data += (sblkp->end - sblkp->start); 7396d90faf3SPaul Saab } 7406d90faf3SPaul Saab } 7410077b016SPaul Saab } 74264b5fbaaSPaul Saab tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start); 743db4b83feSPaul Saab /* 744db4b83feSPaul Saab * Testing sblkp->start against cur->start tells us whether 745db4b83feSPaul Saab * we're done with the sack block or the sack hole. 746db4b83feSPaul Saab * Accordingly, we advance one or the other. 747db4b83feSPaul Saab */ 748db4b83feSPaul Saab if (SEQ_LEQ(sblkp->start, cur->start)) 749db4b83feSPaul Saab cur = TAILQ_PREV(cur, sackhole_head, scblink); 750db4b83feSPaul Saab else 751db4b83feSPaul Saab sblkp--; 7526d90faf3SPaul Saab } 7533c40e1d5SRichard Scheffenegger if (!(to->to_flags & TOF_SACK)) 7543c40e1d5SRichard Scheffenegger /* 7553c40e1d5SRichard Scheffenegger * If this ACK did not contain any 7563c40e1d5SRichard Scheffenegger * SACK blocks, any only moved the 7573c40e1d5SRichard Scheffenegger * left edge right, it is a pure 7583c40e1d5SRichard Scheffenegger * cumulative ACK. Do not count 7593c40e1d5SRichard Scheffenegger * DupAck for this. Also required 7603c40e1d5SRichard Scheffenegger * for RFC6675 rescue retransmission. 7613c40e1d5SRichard Scheffenegger */ 7623c40e1d5SRichard Scheffenegger sack_changed = 0; 763f359d6ebSRichard Scheffenegger tp->sackhint.delivered_data = delivered_data; 764f359d6ebSRichard Scheffenegger tp->sackhint.sacked_bytes += delivered_data - left_edge_delta; 765f359d6ebSRichard Scheffenegger KASSERT((delivered_data >= 0), ("delivered_data < 0")); 766f359d6ebSRichard Scheffenegger KASSERT((tp->sackhint.sacked_bytes >= 0), ("sacked_bytes < 0")); 767021eaf79SHiren Panchasara return (sack_changed); 7686d90faf3SPaul Saab } 7696d90faf3SPaul Saab 7706d90faf3SPaul Saab /* 7715a53ca16SPaul Saab * Free all SACK holes to clear the scoreboard. 7726d90faf3SPaul Saab */ 7736d90faf3SPaul Saab void 7746d90faf3SPaul Saab tcp_free_sackholes(struct tcpcb *tp) 7756d90faf3SPaul Saab { 776a6235da6SPaul Saab struct sackhole *q; 7776d90faf3SPaul Saab 7788501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 7799d17a7a6SPaul Saab while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL) 7809d17a7a6SPaul Saab tcp_sackhole_remove(tp, q); 7810077b016SPaul Saab tp->sackhint.sack_bytes_rexmit = 0; 7824fc53245SPaul Saab 7834fc53245SPaul Saab KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0")); 7849d17a7a6SPaul Saab KASSERT(tp->sackhint.nexthole == NULL, 7859d17a7a6SPaul Saab ("tp->sackhint.nexthole == NULL")); 7866d90faf3SPaul Saab } 7876d90faf3SPaul Saab 7886d90faf3SPaul Saab /* 789632bbf0fSRobert Watson * Partial ack handling within a sack recovery episode. Keeping this very 790632bbf0fSRobert Watson * simple for now. When a partial ack is received, force snd_cwnd to a value 791632bbf0fSRobert Watson * that will allow the sender to transmit no more than 2 segments. If 792632bbf0fSRobert Watson * necessary, a better scheme can be adopted at a later point, but for now, 793632bbf0fSRobert Watson * the goal is to prevent the sender from bursting a large amount of data in 794632bbf0fSRobert Watson * the midst of sack recovery. 7956d90faf3SPaul Saab */ 7966d90faf3SPaul Saab void 797ad3f9ab3SAndre Oppermann tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th) 7986d90faf3SPaul Saab { 7998db456bfSPaul Saab int num_segs = 1; 8004b72ae16SRichard Scheffenegger u_int maxseg = tcp_maxseg(tp); 8016d90faf3SPaul Saab 8028501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 803b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 8046d90faf3SPaul Saab tp->t_rtttime = 0; 805632bbf0fSRobert Watson /* Send one or 2 segments based on how much new data was acked. */ 8064b72ae16SRichard Scheffenegger if ((BYTES_THIS_ACK(tp, th) / maxseg) >= 2) 8078db456bfSPaul Saab num_segs = 2; 8080077b016SPaul Saab tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit + 8094b72ae16SRichard Scheffenegger (tp->snd_nxt - tp->snd_recover) + num_segs * maxseg); 81025e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 81125e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 8126d90faf3SPaul Saab tp->t_flags |= TF_ACKNOW; 8133c40e1d5SRichard Scheffenegger /* 8143c40e1d5SRichard Scheffenegger * RFC6675 rescue retransmission 8153c40e1d5SRichard Scheffenegger * Add a hole between th_ack (snd_una is not yet set) and snd_max, 8163c40e1d5SRichard Scheffenegger * if this was a pure cumulative ACK and no data was send beyond 8173c40e1d5SRichard Scheffenegger * recovery point. Since the data in the socket has not been freed 8183c40e1d5SRichard Scheffenegger * at this point, we check if the scoreboard is empty, and the ACK 8193c40e1d5SRichard Scheffenegger * delivered some new data, indicating a full ACK. Also, if the 8203c40e1d5SRichard Scheffenegger * recovery point is still at snd_max, we are probably application 8213c40e1d5SRichard Scheffenegger * limited. However, this inference might not always be true. The 8223c40e1d5SRichard Scheffenegger * rescue retransmission may rarely be slightly premature 8233c40e1d5SRichard Scheffenegger * compared to RFC6675. 8243c40e1d5SRichard Scheffenegger * The corresponding ACK+SACK will cause any further outstanding 8253c40e1d5SRichard Scheffenegger * segments to be retransmitted. This addresses a corner case, when 8263c40e1d5SRichard Scheffenegger * the trailing packets of a window are lost and no further data 8273c40e1d5SRichard Scheffenegger * is available for sending. 8283c40e1d5SRichard Scheffenegger */ 8293c40e1d5SRichard Scheffenegger if ((V_tcp_do_rfc6675_pipe) && 8303c40e1d5SRichard Scheffenegger SEQ_LT(th->th_ack, tp->snd_recover) && 8313c40e1d5SRichard Scheffenegger (tp->snd_recover == tp->snd_max) && 8323c40e1d5SRichard Scheffenegger TAILQ_EMPTY(&tp->snd_holes) && 8333c40e1d5SRichard Scheffenegger (tp->sackhint.delivered_data > 0)) { 834*e9f02983SRichard Scheffenegger /* 835*e9f02983SRichard Scheffenegger * Exclude FIN sequence space in 836*e9f02983SRichard Scheffenegger * the hole for the rescue retransmission, 837*e9f02983SRichard Scheffenegger * and also don't create a hole, if only 838*e9f02983SRichard Scheffenegger * the ACK for a FIN is outstanding. 839*e9f02983SRichard Scheffenegger */ 840*e9f02983SRichard Scheffenegger tcp_seq highdata = tp->snd_max; 841*e9f02983SRichard Scheffenegger if (tp->t_flags & TF_SENTFIN) 842*e9f02983SRichard Scheffenegger highdata--; 843*e9f02983SRichard Scheffenegger if (th->th_ack != highdata) 844*e9f02983SRichard Scheffenegger (void)tcp_sackhole_insert(tp, SEQ_MAX(th->th_ack, 845*e9f02983SRichard Scheffenegger highdata - maxseg), highdata, NULL); 8463c40e1d5SRichard Scheffenegger } 84755bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 8486d90faf3SPaul Saab } 8496d90faf3SPaul Saab 8501714e18eSMohan Srinivasan #if 0 8516d90faf3SPaul Saab /* 8520077b016SPaul Saab * Debug version of tcp_sack_output() that walks the scoreboard. Used for 8530077b016SPaul Saab * now to sanity check the hint. 8546d90faf3SPaul Saab */ 8550077b016SPaul Saab static struct sackhole * 8560077b016SPaul Saab tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt) 8576d90faf3SPaul Saab { 858a6235da6SPaul Saab struct sackhole *p; 8596d90faf3SPaul Saab 8608501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 861a55db2b6SPaul Saab *sack_bytes_rexmt = 0; 862a6235da6SPaul Saab TAILQ_FOREACH(p, &tp->snd_holes, scblink) { 8636d90faf3SPaul Saab if (SEQ_LT(p->rxmit, p->end)) { 8646d90faf3SPaul Saab if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */ 8656d90faf3SPaul Saab continue; 8666d90faf3SPaul Saab } 867a55db2b6SPaul Saab *sack_bytes_rexmt += (p->rxmit - p->start); 868a55db2b6SPaul Saab break; 869a55db2b6SPaul Saab } 870a55db2b6SPaul Saab *sack_bytes_rexmt += (p->rxmit - p->start); 871a55db2b6SPaul Saab } 8726d90faf3SPaul Saab return (p); 8736d90faf3SPaul Saab } 8741714e18eSMohan Srinivasan #endif 8756d90faf3SPaul Saab 8766d90faf3SPaul Saab /* 8770077b016SPaul Saab * Returns the next hole to retransmit and the number of retransmitted bytes 8780077b016SPaul Saab * from the scoreboard. We store both the next hole and the number of 8790077b016SPaul Saab * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK 8800077b016SPaul Saab * reception). This avoids scoreboard traversals completely. 8810077b016SPaul Saab * 882632bbf0fSRobert Watson * The loop here will traverse *at most* one link. Here's the argument. For 883632bbf0fSRobert Watson * the loop to traverse more than 1 link before finding the next hole to 884632bbf0fSRobert Watson * retransmit, we would need to have at least 1 node following the current 885632bbf0fSRobert Watson * hint with (rxmit == end). But, for all holes following the current hint, 886632bbf0fSRobert Watson * (start == rxmit), since we have not yet retransmitted from them. 887632bbf0fSRobert Watson * Therefore, in order to traverse more 1 link in the loop below, we need to 888632bbf0fSRobert Watson * have at least one node following the current hint with (start == rxmit == 889632bbf0fSRobert Watson * end). But that can't happen, (start == end) means that all the data in 890632bbf0fSRobert Watson * that hole has been sacked, in which case, the hole would have been removed 891632bbf0fSRobert Watson * from the scoreboard. 8920077b016SPaul Saab */ 8930077b016SPaul Saab struct sackhole * 8940077b016SPaul Saab tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt) 8950077b016SPaul Saab { 8961714e18eSMohan Srinivasan struct sackhole *hole = NULL; 8970077b016SPaul Saab 8988501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 8990077b016SPaul Saab *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit; 9000077b016SPaul Saab hole = tp->sackhint.nexthole; 9010077b016SPaul Saab if (hole == NULL || SEQ_LT(hole->rxmit, hole->end)) 9020077b016SPaul Saab goto out; 9030077b016SPaul Saab while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) { 9040077b016SPaul Saab if (SEQ_LT(hole->rxmit, hole->end)) { 9050077b016SPaul Saab tp->sackhint.nexthole = hole; 9060077b016SPaul Saab break; 9070077b016SPaul Saab } 9080077b016SPaul Saab } 9090077b016SPaul Saab out: 9100077b016SPaul Saab return (hole); 9110077b016SPaul Saab } 9120077b016SPaul Saab 9130077b016SPaul Saab /* 9146d90faf3SPaul Saab * After a timeout, the SACK list may be rebuilt. This SACK information 9156d90faf3SPaul Saab * should be used to avoid retransmitting SACKed data. This function 9166d90faf3SPaul Saab * traverses the SACK list to see if snd_nxt should be moved forward. 9176d90faf3SPaul Saab */ 9186d90faf3SPaul Saab void 9196d90faf3SPaul Saab tcp_sack_adjust(struct tcpcb *tp) 9206d90faf3SPaul Saab { 921a6235da6SPaul Saab struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes); 922b7c75571SPaul Saab 9238501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 9246d90faf3SPaul Saab if (cur == NULL) 9256d90faf3SPaul Saab return; /* No holes */ 926808f11b7SPaul Saab if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack)) 9276d90faf3SPaul Saab return; /* We're already beyond any SACKed blocks */ 928632bbf0fSRobert Watson /*- 9296d90faf3SPaul Saab * Two cases for which we want to advance snd_nxt: 9306d90faf3SPaul Saab * i) snd_nxt lies between end of one hole and beginning of another 931808f11b7SPaul Saab * ii) snd_nxt lies between end of last hole and snd_fack 9326d90faf3SPaul Saab */ 933a6235da6SPaul Saab while ((p = TAILQ_NEXT(cur, scblink)) != NULL) { 9346d90faf3SPaul Saab if (SEQ_LT(tp->snd_nxt, cur->end)) 9356d90faf3SPaul Saab return; 936a6235da6SPaul Saab if (SEQ_GEQ(tp->snd_nxt, p->start)) 937a6235da6SPaul Saab cur = p; 9386d90faf3SPaul Saab else { 939a6235da6SPaul Saab tp->snd_nxt = p->start; 9406d90faf3SPaul Saab return; 9416d90faf3SPaul Saab } 9426d90faf3SPaul Saab } 9436d90faf3SPaul Saab if (SEQ_LT(tp->snd_nxt, cur->end)) 9446d90faf3SPaul Saab return; 945808f11b7SPaul Saab tp->snd_nxt = tp->snd_fack; 9466d90faf3SPaul Saab } 947