1c398230bSWarner Losh /*- 26d90faf3SPaul Saab * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 3632bbf0fSRobert Watson * The Regents of the University of California. 4632bbf0fSRobert Watson * All rights reserved. 56d90faf3SPaul Saab * 66d90faf3SPaul Saab * Redistribution and use in source and binary forms, with or without 76d90faf3SPaul Saab * modification, are permitted provided that the following conditions 86d90faf3SPaul Saab * are met: 96d90faf3SPaul Saab * 1. Redistributions of source code must retain the above copyright 106d90faf3SPaul Saab * notice, this list of conditions and the following disclaimer. 116d90faf3SPaul Saab * 2. Redistributions in binary form must reproduce the above copyright 126d90faf3SPaul Saab * notice, this list of conditions and the following disclaimer in the 136d90faf3SPaul Saab * documentation and/or other materials provided with the distribution. 146d90faf3SPaul Saab * 4. Neither the name of the University nor the names of its contributors 156d90faf3SPaul Saab * may be used to endorse or promote products derived from this software 166d90faf3SPaul Saab * without specific prior written permission. 176d90faf3SPaul Saab * 186d90faf3SPaul Saab * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 196d90faf3SPaul Saab * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 206d90faf3SPaul Saab * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 216d90faf3SPaul Saab * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 226d90faf3SPaul Saab * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 236d90faf3SPaul Saab * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 246d90faf3SPaul Saab * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 256d90faf3SPaul Saab * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 266d90faf3SPaul Saab * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 276d90faf3SPaul Saab * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 286d90faf3SPaul Saab * SUCH DAMAGE. 296d90faf3SPaul Saab * 306d90faf3SPaul Saab * @(#)tcp_sack.c 8.12 (Berkeley) 5/24/95 316d90faf3SPaul Saab */ 326d90faf3SPaul Saab 33c398230bSWarner Losh /*- 346d90faf3SPaul Saab * @@(#)COPYRIGHT 1.1 (NRL) 17 January 1995 356d90faf3SPaul Saab * 366d90faf3SPaul Saab * NRL grants permission for redistribution and use in source and binary 376d90faf3SPaul Saab * forms, with or without modification, of the software and documentation 386d90faf3SPaul Saab * created at NRL provided that the following conditions are met: 396d90faf3SPaul Saab * 406d90faf3SPaul Saab * 1. Redistributions of source code must retain the above copyright 416d90faf3SPaul Saab * notice, this list of conditions and the following disclaimer. 426d90faf3SPaul Saab * 2. Redistributions in binary form must reproduce the above copyright 436d90faf3SPaul Saab * notice, this list of conditions and the following disclaimer in the 446d90faf3SPaul Saab * documentation and/or other materials provided with the distribution. 456d90faf3SPaul Saab * 3. All advertising materials mentioning features or use of this software 466d90faf3SPaul Saab * must display the following acknowledgements: 476d90faf3SPaul Saab * This product includes software developed by the University of 486d90faf3SPaul Saab * California, Berkeley and its contributors. 496d90faf3SPaul Saab * This product includes software developed at the Information 506d90faf3SPaul Saab * Technology Division, US Naval Research Laboratory. 516d90faf3SPaul Saab * 4. Neither the name of the NRL nor the names of its contributors 526d90faf3SPaul Saab * may be used to endorse or promote products derived from this software 536d90faf3SPaul Saab * without specific prior written permission. 546d90faf3SPaul Saab * 556d90faf3SPaul Saab * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 566d90faf3SPaul Saab * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 576d90faf3SPaul Saab * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 586d90faf3SPaul Saab * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 596d90faf3SPaul Saab * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 606d90faf3SPaul Saab * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 616d90faf3SPaul Saab * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 626d90faf3SPaul Saab * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 636d90faf3SPaul Saab * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 646d90faf3SPaul Saab * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 656d90faf3SPaul Saab * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 666d90faf3SPaul Saab * 676d90faf3SPaul Saab * The views and conclusions contained in the software and documentation 686d90faf3SPaul Saab * are those of the authors and should not be interpreted as representing 696d90faf3SPaul Saab * official policies, either expressed or implied, of the US Naval 706d90faf3SPaul Saab * Research Laboratory (NRL). 716d90faf3SPaul Saab */ 72632bbf0fSRobert Watson 73632bbf0fSRobert Watson #include <sys/cdefs.h> 74632bbf0fSRobert Watson __FBSDID("$FreeBSD$"); 75632bbf0fSRobert Watson 766d90faf3SPaul Saab #include "opt_inet.h" 776d90faf3SPaul Saab #include "opt_inet6.h" 786d90faf3SPaul Saab #include "opt_tcpdebug.h" 796d90faf3SPaul Saab 806d90faf3SPaul Saab #include <sys/param.h> 816d90faf3SPaul Saab #include <sys/systm.h> 826d90faf3SPaul Saab #include <sys/kernel.h> 836d90faf3SPaul Saab #include <sys/sysctl.h> 846d90faf3SPaul Saab #include <sys/malloc.h> 856d90faf3SPaul Saab #include <sys/mbuf.h> 866d90faf3SPaul Saab #include <sys/proc.h> /* for proc0 declaration */ 876d90faf3SPaul Saab #include <sys/protosw.h> 886d90faf3SPaul Saab #include <sys/socket.h> 896d90faf3SPaul Saab #include <sys/socketvar.h> 906d90faf3SPaul Saab #include <sys/syslog.h> 916d90faf3SPaul Saab #include <sys/systm.h> 92603724d3SBjoern A. Zeeb #include <sys/vimage.h> 936d90faf3SPaul Saab 946d90faf3SPaul Saab #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 956d90faf3SPaul Saab 966d90faf3SPaul Saab #include <vm/uma.h> 976d90faf3SPaul Saab 986d90faf3SPaul Saab #include <net/if.h> 996d90faf3SPaul Saab #include <net/route.h> 1006d90faf3SPaul Saab 1016d90faf3SPaul Saab #include <netinet/in.h> 1026d90faf3SPaul Saab #include <netinet/in_systm.h> 1036d90faf3SPaul Saab #include <netinet/ip.h> 1046d90faf3SPaul Saab #include <netinet/in_var.h> 1056d90faf3SPaul Saab #include <netinet/in_pcb.h> 1066d90faf3SPaul Saab #include <netinet/ip_var.h> 1076d90faf3SPaul Saab #include <netinet/ip6.h> 1086d90faf3SPaul Saab #include <netinet/icmp6.h> 1096d90faf3SPaul Saab #include <netinet6/nd6.h> 1106d90faf3SPaul Saab #include <netinet6/ip6_var.h> 1116d90faf3SPaul Saab #include <netinet6/in6_pcb.h> 1126d90faf3SPaul Saab #include <netinet/tcp.h> 1136d90faf3SPaul Saab #include <netinet/tcp_fsm.h> 1146d90faf3SPaul Saab #include <netinet/tcp_seq.h> 1156d90faf3SPaul Saab #include <netinet/tcp_timer.h> 1166d90faf3SPaul Saab #include <netinet/tcp_var.h> 1176d90faf3SPaul Saab #include <netinet6/tcp6_var.h> 1186d90faf3SPaul Saab #include <netinet/tcpip.h> 1196d90faf3SPaul Saab #ifdef TCPDEBUG 1206d90faf3SPaul Saab #include <netinet/tcp_debug.h> 1216d90faf3SPaul Saab #endif /* TCPDEBUG */ 1226d90faf3SPaul Saab 1236d90faf3SPaul Saab #include <machine/in_cksum.h> 1246d90faf3SPaul Saab 125eddfbb76SRobert Watson VNET_DECLARE(struct uma_zone *, sack_hole_zone); 126eddfbb76SRobert Watson VNET_DEFINE(int, tcp_do_sack); 127eddfbb76SRobert Watson VNET_DEFINE(int, tcp_sack_maxholes); 128eddfbb76SRobert Watson VNET_DEFINE(int, tcp_sack_globalmaxholes); 129eddfbb76SRobert Watson VNET_DEFINE(int, tcp_sack_globalholes); 130eddfbb76SRobert Watson 131eddfbb76SRobert Watson #define V_sack_hole_zone VNET_GET(sack_hole_zone) 132eddfbb76SRobert Watson #define V_tcp_do_sack VNET_GET(tcp_do_sack) 133eddfbb76SRobert Watson #define V_tcp_sack_maxholes VNET_GET(tcp_sack_maxholes) 134eddfbb76SRobert Watson #define V_tcp_sack_globalmaxholes VNET_GET(tcp_sack_globalmaxholes) 135eddfbb76SRobert Watson #define V_tcp_sack_globalholes VNET_GET(tcp_sack_globalholes) 13644e33a07SMarko Zec 137a55db2b6SPaul Saab SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, 0, "TCP SACK"); 138eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_RW, 139eddfbb76SRobert Watson &VNET_NAME(tcp_do_sack), 0, "Enable/Disable TCP SACK support"); 140a55db2b6SPaul Saab 141eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_RW, 142eddfbb76SRobert Watson &VNET_NAME(tcp_sack_maxholes), 0, 143e891d82bSPaul Saab "Maximum number of TCP SACK holes allowed per connection"); 144e891d82bSPaul Saab 145eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_RW, 146eddfbb76SRobert Watson &VNET_NAME(tcp_sack_globalmaxholes), 0, 147e891d82bSPaul Saab "Global maximum number of TCP SACK holes"); 148e891d82bSPaul Saab 149eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_RD, 150eddfbb76SRobert Watson &VNET_NAME(tcp_sack_globalholes), 0, 151e891d82bSPaul Saab "Global number of TCP SACK holes currently allocated"); 152b7c75571SPaul Saab 1536d90faf3SPaul Saab /* 154632bbf0fSRobert Watson * This function is called upon receipt of new valid data (while not in 155632bbf0fSRobert Watson * header prediction mode), and it updates the ordered list of sacks. 1566d90faf3SPaul Saab */ 1576d90faf3SPaul Saab void 158b7c75571SPaul Saab tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end) 1596d90faf3SPaul Saab { 1606d90faf3SPaul Saab /* 1616d90faf3SPaul Saab * First reported block MUST be the most recent one. Subsequent 1626d90faf3SPaul Saab * blocks SHOULD be in the order in which they arrived at the 1636d90faf3SPaul Saab * receiver. These two conditions make the implementation fully 1646d90faf3SPaul Saab * compliant with RFC 2018. 1656d90faf3SPaul Saab */ 166b7c75571SPaul Saab struct sackblk head_blk, saved_blks[MAX_SACK_BLKS]; 167b7c75571SPaul Saab int num_head, num_saved, i; 1686d90faf3SPaul Saab 1698501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 170b7c75571SPaul Saab 171632bbf0fSRobert Watson /* Check arguments. */ 172b7c75571SPaul Saab KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end")); 173b7c75571SPaul Saab 174b7c75571SPaul Saab /* SACK block for the received segment. */ 175b7c75571SPaul Saab head_blk.start = rcv_start; 176b7c75571SPaul Saab head_blk.end = rcv_end; 177b7c75571SPaul Saab 178b7c75571SPaul Saab /* 179632bbf0fSRobert Watson * Merge updated SACK blocks into head_blk, and save unchanged SACK 180632bbf0fSRobert Watson * blocks into saved_blks[]. num_saved will have the number of the 181632bbf0fSRobert Watson * saved SACK blocks. 182b7c75571SPaul Saab */ 183b7c75571SPaul Saab num_saved = 0; 1846d90faf3SPaul Saab for (i = 0; i < tp->rcv_numsacks; i++) { 185b7c75571SPaul Saab tcp_seq start = tp->sackblks[i].start; 186b7c75571SPaul Saab tcp_seq end = tp->sackblks[i].end; 187b7c75571SPaul Saab if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) { 188b7c75571SPaul Saab /* 189b7c75571SPaul Saab * Discard this SACK block. 190b7c75571SPaul Saab */ 191b7c75571SPaul Saab } else if (SEQ_LEQ(head_blk.start, end) && 192b7c75571SPaul Saab SEQ_GEQ(head_blk.end, start)) { 193b7c75571SPaul Saab /* 194632bbf0fSRobert Watson * Merge this SACK block into head_blk. This SACK 195632bbf0fSRobert Watson * block itself will be discarded. 196b7c75571SPaul Saab */ 197b7c75571SPaul Saab if (SEQ_GT(head_blk.start, start)) 198b7c75571SPaul Saab head_blk.start = start; 199b7c75571SPaul Saab if (SEQ_LT(head_blk.end, end)) 200b7c75571SPaul Saab head_blk.end = end; 2016d90faf3SPaul Saab } else { 2026d90faf3SPaul Saab /* 203b7c75571SPaul Saab * Save this SACK block. 2046d90faf3SPaul Saab */ 205b7c75571SPaul Saab saved_blks[num_saved].start = start; 206b7c75571SPaul Saab saved_blks[num_saved].end = end; 207b7c75571SPaul Saab num_saved++; 208b7c75571SPaul Saab } 209b7c75571SPaul Saab } 210b7c75571SPaul Saab 2116d90faf3SPaul Saab /* 212b7c75571SPaul Saab * Update SACK list in tp->sackblks[]. 2136d90faf3SPaul Saab */ 214b7c75571SPaul Saab num_head = 0; 215b7c75571SPaul Saab if (SEQ_GT(head_blk.start, tp->rcv_nxt)) { 216b7c75571SPaul Saab /* 217632bbf0fSRobert Watson * The received data segment is an out-of-order segment. Put 218632bbf0fSRobert Watson * head_blk at the top of SACK list. 219b7c75571SPaul Saab */ 220b7c75571SPaul Saab tp->sackblks[0] = head_blk; 221b7c75571SPaul Saab num_head = 1; 222b7c75571SPaul Saab /* 223b7c75571SPaul Saab * If the number of saved SACK blocks exceeds its limit, 224b7c75571SPaul Saab * discard the last SACK block. 225b7c75571SPaul Saab */ 226b7c75571SPaul Saab if (num_saved >= MAX_SACK_BLKS) 227b7c75571SPaul Saab num_saved--; 2286d90faf3SPaul Saab } 229b7c75571SPaul Saab if (num_saved > 0) { 230b7c75571SPaul Saab /* 231b7c75571SPaul Saab * Copy the saved SACK blocks back. 232b7c75571SPaul Saab */ 233b7c75571SPaul Saab bcopy(saved_blks, &tp->sackblks[num_head], 234b7c75571SPaul Saab sizeof(struct sackblk) * num_saved); 2356d90faf3SPaul Saab } 236b7c75571SPaul Saab 237b7c75571SPaul Saab /* Save the number of SACK blocks. */ 238b7c75571SPaul Saab tp->rcv_numsacks = num_head + num_saved; 2396d90faf3SPaul Saab } 2406d90faf3SPaul Saab 2416d90faf3SPaul Saab /* 2426d90faf3SPaul Saab * Delete all receiver-side SACK information. 2436d90faf3SPaul Saab */ 2446d90faf3SPaul Saab void 245f2565d68SRobert Watson tcp_clean_sackreport(struct tcpcb *tp) 2466d90faf3SPaul Saab { 2476d90faf3SPaul Saab int i; 2486d90faf3SPaul Saab 2498501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2506d90faf3SPaul Saab tp->rcv_numsacks = 0; 2516d90faf3SPaul Saab for (i = 0; i < MAX_SACK_BLKS; i++) 2526d90faf3SPaul Saab tp->sackblks[i].start = tp->sackblks[i].end=0; 2536d90faf3SPaul Saab } 2546d90faf3SPaul Saab 2556d90faf3SPaul Saab /* 2564fc53245SPaul Saab * Allocate struct sackhole. 2574fc53245SPaul Saab */ 2584fc53245SPaul Saab static struct sackhole * 2594fc53245SPaul Saab tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end) 2604fc53245SPaul Saab { 2614fc53245SPaul Saab struct sackhole *hole; 2624fc53245SPaul Saab 263603724d3SBjoern A. Zeeb if (tp->snd_numholes >= V_tcp_sack_maxholes || 264603724d3SBjoern A. Zeeb V_tcp_sack_globalholes >= V_tcp_sack_globalmaxholes) { 26578b50714SRobert Watson TCPSTAT_INC(tcps_sack_sboverflow); 2664fc53245SPaul Saab return NULL; 2674fc53245SPaul Saab } 2684fc53245SPaul Saab 2691ed81b73SMarko Zec hole = (struct sackhole *)uma_zalloc(V_sack_hole_zone, M_NOWAIT); 2704fc53245SPaul Saab if (hole == NULL) 2714fc53245SPaul Saab return NULL; 2724fc53245SPaul Saab 2734fc53245SPaul Saab hole->start = start; 2744fc53245SPaul Saab hole->end = end; 2754fc53245SPaul Saab hole->rxmit = start; 2764fc53245SPaul Saab 2774fc53245SPaul Saab tp->snd_numholes++; 27891a5ebdeSLawrence Stewart atomic_add_int(&V_tcp_sack_globalholes, 1); 2794fc53245SPaul Saab 2804fc53245SPaul Saab return hole; 2814fc53245SPaul Saab } 2824fc53245SPaul Saab 2834fc53245SPaul Saab /* 2844fc53245SPaul Saab * Free struct sackhole. 2854fc53245SPaul Saab */ 2864fc53245SPaul Saab static void 2874fc53245SPaul Saab tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole) 2884fc53245SPaul Saab { 289632bbf0fSRobert Watson 2901ed81b73SMarko Zec uma_zfree(V_sack_hole_zone, hole); 2914fc53245SPaul Saab 2924fc53245SPaul Saab tp->snd_numholes--; 29391a5ebdeSLawrence Stewart atomic_subtract_int(&V_tcp_sack_globalholes, 1); 2944fc53245SPaul Saab 2954fc53245SPaul Saab KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0")); 296603724d3SBjoern A. Zeeb KASSERT(V_tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0")); 2974fc53245SPaul Saab } 2984fc53245SPaul Saab 2994fc53245SPaul Saab /* 3009d17a7a6SPaul Saab * Insert new SACK hole into scoreboard. 3019d17a7a6SPaul Saab */ 3029d17a7a6SPaul Saab static struct sackhole * 3039d17a7a6SPaul Saab tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end, 3049d17a7a6SPaul Saab struct sackhole *after) 3059d17a7a6SPaul Saab { 3069d17a7a6SPaul Saab struct sackhole *hole; 3079d17a7a6SPaul Saab 3089d17a7a6SPaul Saab /* Allocate a new SACK hole. */ 3099d17a7a6SPaul Saab hole = tcp_sackhole_alloc(tp, start, end); 3109d17a7a6SPaul Saab if (hole == NULL) 3119d17a7a6SPaul Saab return NULL; 3129d17a7a6SPaul Saab 313632bbf0fSRobert Watson /* Insert the new SACK hole into scoreboard. */ 3149d17a7a6SPaul Saab if (after != NULL) 3159d17a7a6SPaul Saab TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink); 3169d17a7a6SPaul Saab else 3179d17a7a6SPaul Saab TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink); 3189d17a7a6SPaul Saab 3199d17a7a6SPaul Saab /* Update SACK hint. */ 3209d17a7a6SPaul Saab if (tp->sackhint.nexthole == NULL) 3219d17a7a6SPaul Saab tp->sackhint.nexthole = hole; 3229d17a7a6SPaul Saab 3239d17a7a6SPaul Saab return hole; 3249d17a7a6SPaul Saab } 3259d17a7a6SPaul Saab 3269d17a7a6SPaul Saab /* 3279d17a7a6SPaul Saab * Remove SACK hole from scoreboard. 3289d17a7a6SPaul Saab */ 3299d17a7a6SPaul Saab static void 3309d17a7a6SPaul Saab tcp_sackhole_remove(struct tcpcb *tp, struct sackhole *hole) 3319d17a7a6SPaul Saab { 332632bbf0fSRobert Watson 3339d17a7a6SPaul Saab /* Update SACK hint. */ 3349d17a7a6SPaul Saab if (tp->sackhint.nexthole == hole) 3359d17a7a6SPaul Saab tp->sackhint.nexthole = TAILQ_NEXT(hole, scblink); 3369d17a7a6SPaul Saab 3379d17a7a6SPaul Saab /* Remove this SACK hole. */ 3389d17a7a6SPaul Saab TAILQ_REMOVE(&tp->snd_holes, hole, scblink); 3399d17a7a6SPaul Saab 3409d17a7a6SPaul Saab /* Free this SACK hole. */ 3419d17a7a6SPaul Saab tcp_sackhole_free(tp, hole); 3429d17a7a6SPaul Saab } 3439d17a7a6SPaul Saab 3449d17a7a6SPaul Saab /* 3455a53ca16SPaul Saab * Process cumulative ACK and the TCP SACK option to update the scoreboard. 3465a53ca16SPaul Saab * tp->snd_holes is an ordered list of holes (oldest to newest, in terms of 3475a53ca16SPaul Saab * the sequence space). 3486d90faf3SPaul Saab */ 3495a53ca16SPaul Saab void 3505a53ca16SPaul Saab tcp_sack_doack(struct tcpcb *tp, struct tcpopt *to, tcp_seq th_ack) 3516d90faf3SPaul Saab { 352a6235da6SPaul Saab struct sackhole *cur, *temp; 3535a53ca16SPaul Saab struct sackblk sack, sack_blocks[TCP_MAX_SACK + 1], *sblkp; 3549d17a7a6SPaul Saab int i, j, num_sack_blks; 3556d90faf3SPaul Saab 3568501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 3575a53ca16SPaul Saab 35864b5fbaaSPaul Saab num_sack_blks = 0; 3595a53ca16SPaul Saab /* 3605a53ca16SPaul Saab * If SND.UNA will be advanced by SEG.ACK, and if SACK holes exist, 3615a53ca16SPaul Saab * treat [SND.UNA, SEG.ACK) as if it is a SACK block. 3625a53ca16SPaul Saab */ 3635a53ca16SPaul Saab if (SEQ_LT(tp->snd_una, th_ack) && !TAILQ_EMPTY(&tp->snd_holes)) { 3645a53ca16SPaul Saab sack_blocks[num_sack_blks].start = tp->snd_una; 3655a53ca16SPaul Saab sack_blocks[num_sack_blks++].end = th_ack; 3665a53ca16SPaul Saab } 3675a53ca16SPaul Saab /* 368632bbf0fSRobert Watson * Append received valid SACK blocks to sack_blocks[], but only if we 369632bbf0fSRobert Watson * received new blocks from the other side. 3705a53ca16SPaul Saab */ 37107b64b90SAndre Oppermann if (to->to_flags & TOF_SACK) { 3725a53ca16SPaul Saab for (i = 0; i < to->to_nsacks; i++) { 37307b64b90SAndre Oppermann bcopy((to->to_sacks + i * TCPOLEN_SACK), 37407b64b90SAndre Oppermann &sack, sizeof(sack)); 3756d90faf3SPaul Saab sack.start = ntohl(sack.start); 3766d90faf3SPaul Saab sack.end = ntohl(sack.end); 37764b5fbaaSPaul Saab if (SEQ_GT(sack.end, sack.start) && 37864b5fbaaSPaul Saab SEQ_GT(sack.start, tp->snd_una) && 3795a53ca16SPaul Saab SEQ_GT(sack.start, th_ack) && 3801f65c2cdSMohan Srinivasan SEQ_LT(sack.start, tp->snd_max) && 3811f65c2cdSMohan Srinivasan SEQ_GT(sack.end, tp->snd_una) && 38264b5fbaaSPaul Saab SEQ_LEQ(sack.end, tp->snd_max)) 38364b5fbaaSPaul Saab sack_blocks[num_sack_blks++] = sack; 3846d90faf3SPaul Saab } 38507b64b90SAndre Oppermann } 3865a53ca16SPaul Saab /* 387632bbf0fSRobert Watson * Return if SND.UNA is not advanced and no valid SACK block is 388632bbf0fSRobert Watson * received. 3895a53ca16SPaul Saab */ 39064b5fbaaSPaul Saab if (num_sack_blks == 0) 3915a53ca16SPaul Saab return; 3925a53ca16SPaul Saab 3935a53ca16SPaul Saab /* 394632bbf0fSRobert Watson * Sort the SACK blocks so we can update the scoreboard with just one 395632bbf0fSRobert Watson * pass. The overhead of sorting upto 4+1 elements is less than 396632bbf0fSRobert Watson * making upto 4+1 passes over the scoreboard. 3975a53ca16SPaul Saab */ 39864b5fbaaSPaul Saab for (i = 0; i < num_sack_blks; i++) { 39964b5fbaaSPaul Saab for (j = i + 1; j < num_sack_blks; j++) { 4009d17a7a6SPaul Saab if (SEQ_GT(sack_blocks[i].end, sack_blocks[j].end)) { 40164b5fbaaSPaul Saab sack = sack_blocks[i]; 40264b5fbaaSPaul Saab sack_blocks[i] = sack_blocks[j]; 40364b5fbaaSPaul Saab sack_blocks[j] = sack; 4046d90faf3SPaul Saab } 4056d90faf3SPaul Saab } 40664b5fbaaSPaul Saab } 40764b5fbaaSPaul Saab if (TAILQ_EMPTY(&tp->snd_holes)) 40864b5fbaaSPaul Saab /* 409808f11b7SPaul Saab * Empty scoreboard. Need to initialize snd_fack (it may be 41064b5fbaaSPaul Saab * uninitialized or have a bogus value). Scoreboard holes 411632bbf0fSRobert Watson * (from the sack blocks received) are created later below 412632bbf0fSRobert Watson * (in the logic that adds holes to the tail of the 413632bbf0fSRobert Watson * scoreboard). 41464b5fbaaSPaul Saab */ 4155a53ca16SPaul Saab tp->snd_fack = SEQ_MAX(tp->snd_una, th_ack); 4169d17a7a6SPaul Saab /* 417632bbf0fSRobert Watson * In the while-loop below, incoming SACK blocks (sack_blocks[]) and 418632bbf0fSRobert Watson * SACK holes (snd_holes) are traversed from their tails with just 419632bbf0fSRobert Watson * one pass in order to reduce the number of compares especially when 420632bbf0fSRobert Watson * the bandwidth-delay product is large. 421632bbf0fSRobert Watson * 4229d17a7a6SPaul Saab * Note: Typically, in the first RTT of SACK recovery, the highest 4239d17a7a6SPaul Saab * three or four SACK blocks with the same ack number are received. 4249d17a7a6SPaul Saab * In the second RTT, if retransmitted data segments are not lost, 4259d17a7a6SPaul Saab * the highest three or four SACK blocks with ack number advancing 4269d17a7a6SPaul Saab * are received. 4279d17a7a6SPaul Saab */ 4289d17a7a6SPaul Saab sblkp = &sack_blocks[num_sack_blks - 1]; /* Last SACK block */ 4299d17a7a6SPaul Saab if (SEQ_LT(tp->snd_fack, sblkp->start)) { 4309d17a7a6SPaul Saab /* 431632bbf0fSRobert Watson * The highest SACK block is beyond fack. Append new SACK 432632bbf0fSRobert Watson * hole at the tail. If the second or later highest SACK 433632bbf0fSRobert Watson * blocks are also beyond the current fack, they will be 434632bbf0fSRobert Watson * inserted by way of hole splitting in the while-loop below. 4359d17a7a6SPaul Saab */ 4368b9bbaaaSPaul Saab temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL); 437d0a14f55SPaul Saab if (temp != NULL) { 4389d17a7a6SPaul Saab tp->snd_fack = sblkp->end; 4399d17a7a6SPaul Saab /* Go to the previous sack block. */ 4409d17a7a6SPaul Saab sblkp--; 441d0a14f55SPaul Saab } else { 442d0a14f55SPaul Saab /* 443d0a14f55SPaul Saab * We failed to add a new hole based on the current 444d0a14f55SPaul Saab * sack block. Skip over all the sack blocks that 445632bbf0fSRobert Watson * fall completely to the right of snd_fack and 446632bbf0fSRobert Watson * proceed to trim the scoreboard based on the 447632bbf0fSRobert Watson * remaining sack blocks. This also trims the 448632bbf0fSRobert Watson * scoreboard for th_ack (which is sack_blocks[0]). 449d0a14f55SPaul Saab */ 450d0a14f55SPaul Saab while (sblkp >= sack_blocks && 451d0a14f55SPaul Saab SEQ_LT(tp->snd_fack, sblkp->start)) 452d0a14f55SPaul Saab sblkp--; 453d0a14f55SPaul Saab if (sblkp >= sack_blocks && 454d0a14f55SPaul Saab SEQ_LT(tp->snd_fack, sblkp->end)) 455d0a14f55SPaul Saab tp->snd_fack = sblkp->end; 456d0a14f55SPaul Saab } 4579d17a7a6SPaul Saab } else if (SEQ_LT(tp->snd_fack, sblkp->end)) 4589d17a7a6SPaul Saab /* fack is advanced. */ 4599d17a7a6SPaul Saab tp->snd_fack = sblkp->end; 460632bbf0fSRobert Watson /* We must have at least one SACK hole in scoreboard. */ 461632bbf0fSRobert Watson KASSERT(!TAILQ_EMPTY(&tp->snd_holes), 462632bbf0fSRobert Watson ("SACK scoreboard must not be empty")); 463632bbf0fSRobert Watson cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */ 46464b5fbaaSPaul Saab /* 46564b5fbaaSPaul Saab * Since the incoming sack blocks are sorted, we can process them 46664b5fbaaSPaul Saab * making one sweep of the scoreboard. 46764b5fbaaSPaul Saab */ 468d0a14f55SPaul Saab while (sblkp >= sack_blocks && cur != NULL) { 4699d17a7a6SPaul Saab if (SEQ_GEQ(sblkp->start, cur->end)) { 47064b5fbaaSPaul Saab /* 471632bbf0fSRobert Watson * SACKs data beyond the current hole. Go to the 472632bbf0fSRobert Watson * previous sack block. 47364b5fbaaSPaul Saab */ 4749d17a7a6SPaul Saab sblkp--; 4759d17a7a6SPaul Saab continue; 4769d17a7a6SPaul Saab } 4779d17a7a6SPaul Saab if (SEQ_LEQ(sblkp->end, cur->start)) { 4789d17a7a6SPaul Saab /* 479632bbf0fSRobert Watson * SACKs data before the current hole. Go to the 480632bbf0fSRobert Watson * previous hole. 4819d17a7a6SPaul Saab */ 4829d17a7a6SPaul Saab cur = TAILQ_PREV(cur, sackhole_head, scblink); 4836d90faf3SPaul Saab continue; 4846d90faf3SPaul Saab } 48564b5fbaaSPaul Saab tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start); 4860077b016SPaul Saab KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 4870077b016SPaul Saab ("sackhint bytes rtx >= 0")); 4889d17a7a6SPaul Saab if (SEQ_LEQ(sblkp->start, cur->start)) { 489632bbf0fSRobert Watson /* Data acks at least the beginning of hole. */ 4909d17a7a6SPaul Saab if (SEQ_GEQ(sblkp->end, cur->end)) { 491632bbf0fSRobert Watson /* Acks entire hole, so delete hole. */ 492a6235da6SPaul Saab temp = cur; 4939d17a7a6SPaul Saab cur = TAILQ_PREV(cur, sackhole_head, scblink); 4949d17a7a6SPaul Saab tcp_sackhole_remove(tp, temp); 49564b5fbaaSPaul Saab /* 496632bbf0fSRobert Watson * The sack block may ack all or part of the 497632bbf0fSRobert Watson * next hole too, so continue onto the next 498632bbf0fSRobert Watson * hole. 49964b5fbaaSPaul Saab */ 5006d90faf3SPaul Saab continue; 5010077b016SPaul Saab } else { 502632bbf0fSRobert Watson /* Move start of hole forward. */ 5039d17a7a6SPaul Saab cur->start = sblkp->end; 5046d90faf3SPaul Saab cur->rxmit = SEQ_MAX(cur->rxmit, cur->start); 5056d90faf3SPaul Saab } 50664b5fbaaSPaul Saab } else { 507632bbf0fSRobert Watson /* Data acks at least the end of hole. */ 5089d17a7a6SPaul Saab if (SEQ_GEQ(sblkp->end, cur->end)) { 509632bbf0fSRobert Watson /* Move end of hole backward. */ 5109d17a7a6SPaul Saab cur->end = sblkp->start; 5116d90faf3SPaul Saab cur->rxmit = SEQ_MIN(cur->rxmit, cur->end); 5120077b016SPaul Saab } else { 5136d90faf3SPaul Saab /* 514632bbf0fSRobert Watson * ACKs some data in middle of a hole; need 515632bbf0fSRobert Watson * to split current hole 5166d90faf3SPaul Saab */ 5179d17a7a6SPaul Saab temp = tcp_sackhole_insert(tp, sblkp->end, 5189d17a7a6SPaul Saab cur->end, cur); 5190077b016SPaul Saab if (temp != NULL) { 5209d17a7a6SPaul Saab if (SEQ_GT(cur->rxmit, temp->rxmit)) { 5214fc53245SPaul Saab temp->rxmit = cur->rxmit; 5229d17a7a6SPaul Saab tp->sackhint.sack_bytes_rexmit 5239d17a7a6SPaul Saab += (temp->rxmit 5249d17a7a6SPaul Saab - temp->start); 5259d17a7a6SPaul Saab } 5269d17a7a6SPaul Saab cur->end = sblkp->start; 5279d17a7a6SPaul Saab cur->rxmit = SEQ_MIN(cur->rxmit, 5289d17a7a6SPaul Saab cur->end); 5296d90faf3SPaul Saab } 5306d90faf3SPaul Saab } 5310077b016SPaul Saab } 53264b5fbaaSPaul Saab tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start); 533db4b83feSPaul Saab /* 534db4b83feSPaul Saab * Testing sblkp->start against cur->start tells us whether 535db4b83feSPaul Saab * we're done with the sack block or the sack hole. 536db4b83feSPaul Saab * Accordingly, we advance one or the other. 537db4b83feSPaul Saab */ 538db4b83feSPaul Saab if (SEQ_LEQ(sblkp->start, cur->start)) 539db4b83feSPaul Saab cur = TAILQ_PREV(cur, sackhole_head, scblink); 540db4b83feSPaul Saab else 541db4b83feSPaul Saab sblkp--; 5426d90faf3SPaul Saab } 5436d90faf3SPaul Saab } 5446d90faf3SPaul Saab 5456d90faf3SPaul Saab /* 5465a53ca16SPaul Saab * Free all SACK holes to clear the scoreboard. 5476d90faf3SPaul Saab */ 5486d90faf3SPaul Saab void 5496d90faf3SPaul Saab tcp_free_sackholes(struct tcpcb *tp) 5506d90faf3SPaul Saab { 551a6235da6SPaul Saab struct sackhole *q; 5526d90faf3SPaul Saab 5538501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 5549d17a7a6SPaul Saab while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL) 5559d17a7a6SPaul Saab tcp_sackhole_remove(tp, q); 5560077b016SPaul Saab tp->sackhint.sack_bytes_rexmit = 0; 5574fc53245SPaul Saab 5584fc53245SPaul Saab KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0")); 5599d17a7a6SPaul Saab KASSERT(tp->sackhint.nexthole == NULL, 5609d17a7a6SPaul Saab ("tp->sackhint.nexthole == NULL")); 5616d90faf3SPaul Saab } 5626d90faf3SPaul Saab 5636d90faf3SPaul Saab /* 564632bbf0fSRobert Watson * Partial ack handling within a sack recovery episode. Keeping this very 565632bbf0fSRobert Watson * simple for now. When a partial ack is received, force snd_cwnd to a value 566632bbf0fSRobert Watson * that will allow the sender to transmit no more than 2 segments. If 567632bbf0fSRobert Watson * necessary, a better scheme can be adopted at a later point, but for now, 568632bbf0fSRobert Watson * the goal is to prevent the sender from bursting a large amount of data in 569632bbf0fSRobert Watson * the midst of sack recovery. 5706d90faf3SPaul Saab */ 5716d90faf3SPaul Saab void 572ad3f9ab3SAndre Oppermann tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th) 5736d90faf3SPaul Saab { 5748db456bfSPaul Saab int num_segs = 1; 5756d90faf3SPaul Saab 5768501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 577b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 5786d90faf3SPaul Saab tp->t_rtttime = 0; 579632bbf0fSRobert Watson /* Send one or 2 segments based on how much new data was acked. */ 5808db456bfSPaul Saab if (((th->th_ack - tp->snd_una) / tp->t_maxseg) > 2) 5818db456bfSPaul Saab num_segs = 2; 5820077b016SPaul Saab tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit + 583632bbf0fSRobert Watson (tp->snd_nxt - tp->sack_newdata) + num_segs * tp->t_maxseg); 58425e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 58525e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 5866d90faf3SPaul Saab tp->t_flags |= TF_ACKNOW; 5876d90faf3SPaul Saab (void) tcp_output(tp); 5886d90faf3SPaul Saab } 5896d90faf3SPaul Saab 5901714e18eSMohan Srinivasan #if 0 5916d90faf3SPaul Saab /* 5920077b016SPaul Saab * Debug version of tcp_sack_output() that walks the scoreboard. Used for 5930077b016SPaul Saab * now to sanity check the hint. 5946d90faf3SPaul Saab */ 5950077b016SPaul Saab static struct sackhole * 5960077b016SPaul Saab tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt) 5976d90faf3SPaul Saab { 598a6235da6SPaul Saab struct sackhole *p; 5996d90faf3SPaul Saab 6008501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 601a55db2b6SPaul Saab *sack_bytes_rexmt = 0; 602a6235da6SPaul Saab TAILQ_FOREACH(p, &tp->snd_holes, scblink) { 6036d90faf3SPaul Saab if (SEQ_LT(p->rxmit, p->end)) { 6046d90faf3SPaul Saab if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */ 6056d90faf3SPaul Saab continue; 6066d90faf3SPaul Saab } 607a55db2b6SPaul Saab *sack_bytes_rexmt += (p->rxmit - p->start); 608a55db2b6SPaul Saab break; 609a55db2b6SPaul Saab } 610a55db2b6SPaul Saab *sack_bytes_rexmt += (p->rxmit - p->start); 611a55db2b6SPaul Saab } 6126d90faf3SPaul Saab return (p); 6136d90faf3SPaul Saab } 6141714e18eSMohan Srinivasan #endif 6156d90faf3SPaul Saab 6166d90faf3SPaul Saab /* 6170077b016SPaul Saab * Returns the next hole to retransmit and the number of retransmitted bytes 6180077b016SPaul Saab * from the scoreboard. We store both the next hole and the number of 6190077b016SPaul Saab * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK 6200077b016SPaul Saab * reception). This avoids scoreboard traversals completely. 6210077b016SPaul Saab * 622632bbf0fSRobert Watson * The loop here will traverse *at most* one link. Here's the argument. For 623632bbf0fSRobert Watson * the loop to traverse more than 1 link before finding the next hole to 624632bbf0fSRobert Watson * retransmit, we would need to have at least 1 node following the current 625632bbf0fSRobert Watson * hint with (rxmit == end). But, for all holes following the current hint, 626632bbf0fSRobert Watson * (start == rxmit), since we have not yet retransmitted from them. 627632bbf0fSRobert Watson * Therefore, in order to traverse more 1 link in the loop below, we need to 628632bbf0fSRobert Watson * have at least one node following the current hint with (start == rxmit == 629632bbf0fSRobert Watson * end). But that can't happen, (start == end) means that all the data in 630632bbf0fSRobert Watson * that hole has been sacked, in which case, the hole would have been removed 631632bbf0fSRobert Watson * from the scoreboard. 6320077b016SPaul Saab */ 6330077b016SPaul Saab struct sackhole * 6340077b016SPaul Saab tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt) 6350077b016SPaul Saab { 6361714e18eSMohan Srinivasan struct sackhole *hole = NULL; 6370077b016SPaul Saab 6388501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 6390077b016SPaul Saab *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit; 6400077b016SPaul Saab hole = tp->sackhint.nexthole; 6410077b016SPaul Saab if (hole == NULL || SEQ_LT(hole->rxmit, hole->end)) 6420077b016SPaul Saab goto out; 6430077b016SPaul Saab while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) { 6440077b016SPaul Saab if (SEQ_LT(hole->rxmit, hole->end)) { 6450077b016SPaul Saab tp->sackhint.nexthole = hole; 6460077b016SPaul Saab break; 6470077b016SPaul Saab } 6480077b016SPaul Saab } 6490077b016SPaul Saab out: 6500077b016SPaul Saab return (hole); 6510077b016SPaul Saab } 6520077b016SPaul Saab 6530077b016SPaul Saab /* 6546d90faf3SPaul Saab * After a timeout, the SACK list may be rebuilt. This SACK information 6556d90faf3SPaul Saab * should be used to avoid retransmitting SACKed data. This function 6566d90faf3SPaul Saab * traverses the SACK list to see if snd_nxt should be moved forward. 6576d90faf3SPaul Saab */ 6586d90faf3SPaul Saab void 6596d90faf3SPaul Saab tcp_sack_adjust(struct tcpcb *tp) 6606d90faf3SPaul Saab { 661a6235da6SPaul Saab struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes); 662b7c75571SPaul Saab 6638501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 6646d90faf3SPaul Saab if (cur == NULL) 6656d90faf3SPaul Saab return; /* No holes */ 666808f11b7SPaul Saab if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack)) 6676d90faf3SPaul Saab return; /* We're already beyond any SACKed blocks */ 668632bbf0fSRobert Watson /*- 6696d90faf3SPaul Saab * Two cases for which we want to advance snd_nxt: 6706d90faf3SPaul Saab * i) snd_nxt lies between end of one hole and beginning of another 671808f11b7SPaul Saab * ii) snd_nxt lies between end of last hole and snd_fack 6726d90faf3SPaul Saab */ 673a6235da6SPaul Saab while ((p = TAILQ_NEXT(cur, scblink)) != NULL) { 6746d90faf3SPaul Saab if (SEQ_LT(tp->snd_nxt, cur->end)) 6756d90faf3SPaul Saab return; 676a6235da6SPaul Saab if (SEQ_GEQ(tp->snd_nxt, p->start)) 677a6235da6SPaul Saab cur = p; 6786d90faf3SPaul Saab else { 679a6235da6SPaul Saab tp->snd_nxt = p->start; 6806d90faf3SPaul Saab return; 6816d90faf3SPaul Saab } 6826d90faf3SPaul Saab } 6836d90faf3SPaul Saab if (SEQ_LT(tp->snd_nxt, cur->end)) 6846d90faf3SPaul Saab return; 685808f11b7SPaul Saab tp->snd_nxt = tp->snd_fack; 6866d90faf3SPaul Saab } 687