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> 926d90faf3SPaul Saab 936d90faf3SPaul Saab #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 946d90faf3SPaul Saab 956d90faf3SPaul Saab #include <vm/uma.h> 966d90faf3SPaul Saab 976d90faf3SPaul Saab #include <net/if.h> 98*76039bc8SGleb Smirnoff #include <net/if_var.h> 996d90faf3SPaul Saab #include <net/route.h> 100530c0060SRobert Watson #include <net/vnet.h> 1016d90faf3SPaul Saab 1026d90faf3SPaul Saab #include <netinet/in.h> 1036d90faf3SPaul Saab #include <netinet/in_systm.h> 1046d90faf3SPaul Saab #include <netinet/ip.h> 1056d90faf3SPaul Saab #include <netinet/in_var.h> 1066d90faf3SPaul Saab #include <netinet/in_pcb.h> 1076d90faf3SPaul Saab #include <netinet/ip_var.h> 1086d90faf3SPaul Saab #include <netinet/ip6.h> 1096d90faf3SPaul Saab #include <netinet/icmp6.h> 1106d90faf3SPaul Saab #include <netinet6/nd6.h> 1116d90faf3SPaul Saab #include <netinet6/ip6_var.h> 1126d90faf3SPaul Saab #include <netinet6/in6_pcb.h> 1136d90faf3SPaul Saab #include <netinet/tcp.h> 1146d90faf3SPaul Saab #include <netinet/tcp_fsm.h> 1156d90faf3SPaul Saab #include <netinet/tcp_seq.h> 1166d90faf3SPaul Saab #include <netinet/tcp_timer.h> 1176d90faf3SPaul Saab #include <netinet/tcp_var.h> 1186d90faf3SPaul Saab #include <netinet6/tcp6_var.h> 1196d90faf3SPaul Saab #include <netinet/tcpip.h> 1206d90faf3SPaul Saab #ifdef TCPDEBUG 1216d90faf3SPaul Saab #include <netinet/tcp_debug.h> 1226d90faf3SPaul Saab #endif /* TCPDEBUG */ 1236d90faf3SPaul Saab 1246d90faf3SPaul Saab #include <machine/in_cksum.h> 1256d90faf3SPaul Saab 126eddfbb76SRobert Watson VNET_DECLARE(struct uma_zone *, sack_hole_zone); 1271e77c105SRobert Watson #define V_sack_hole_zone VNET(sack_hole_zone) 12844e33a07SMarko Zec 129a55db2b6SPaul Saab SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, 0, "TCP SACK"); 13082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_sack) = 1; 13182cea7e6SBjoern A. Zeeb #define V_tcp_do_sack VNET(tcp_do_sack) 132eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_RW, 133eddfbb76SRobert Watson &VNET_NAME(tcp_do_sack), 0, "Enable/Disable TCP SACK support"); 134a55db2b6SPaul Saab 13582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_sack_maxholes) = 128; 13682cea7e6SBjoern A. Zeeb #define V_tcp_sack_maxholes VNET(tcp_sack_maxholes) 137eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_RW, 138eddfbb76SRobert Watson &VNET_NAME(tcp_sack_maxholes), 0, 139e891d82bSPaul Saab "Maximum number of TCP SACK holes allowed per connection"); 140e891d82bSPaul Saab 14182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_sack_globalmaxholes) = 65536; 14282cea7e6SBjoern A. Zeeb #define V_tcp_sack_globalmaxholes VNET(tcp_sack_globalmaxholes) 143eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_RW, 144eddfbb76SRobert Watson &VNET_NAME(tcp_sack_globalmaxholes), 0, 145e891d82bSPaul Saab "Global maximum number of TCP SACK holes"); 146e891d82bSPaul Saab 14782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_sack_globalholes) = 0; 14882cea7e6SBjoern A. Zeeb #define V_tcp_sack_globalholes VNET(tcp_sack_globalholes) 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 */ 429bee9ab2bSLawrence Stewart tp->sackhint.last_sack_ack = sblkp->end; 4309d17a7a6SPaul Saab if (SEQ_LT(tp->snd_fack, sblkp->start)) { 4319d17a7a6SPaul Saab /* 432632bbf0fSRobert Watson * The highest SACK block is beyond fack. Append new SACK 433632bbf0fSRobert Watson * hole at the tail. If the second or later highest SACK 434632bbf0fSRobert Watson * blocks are also beyond the current fack, they will be 435632bbf0fSRobert Watson * inserted by way of hole splitting in the while-loop below. 4369d17a7a6SPaul Saab */ 4378b9bbaaaSPaul Saab temp = tcp_sackhole_insert(tp, tp->snd_fack,sblkp->start,NULL); 438d0a14f55SPaul Saab if (temp != NULL) { 4399d17a7a6SPaul Saab tp->snd_fack = sblkp->end; 4409d17a7a6SPaul Saab /* Go to the previous sack block. */ 4419d17a7a6SPaul Saab sblkp--; 442d0a14f55SPaul Saab } else { 443d0a14f55SPaul Saab /* 444d0a14f55SPaul Saab * We failed to add a new hole based on the current 445d0a14f55SPaul Saab * sack block. Skip over all the sack blocks that 446632bbf0fSRobert Watson * fall completely to the right of snd_fack and 447632bbf0fSRobert Watson * proceed to trim the scoreboard based on the 448632bbf0fSRobert Watson * remaining sack blocks. This also trims the 449632bbf0fSRobert Watson * scoreboard for th_ack (which is sack_blocks[0]). 450d0a14f55SPaul Saab */ 451d0a14f55SPaul Saab while (sblkp >= sack_blocks && 452d0a14f55SPaul Saab SEQ_LT(tp->snd_fack, sblkp->start)) 453d0a14f55SPaul Saab sblkp--; 454d0a14f55SPaul Saab if (sblkp >= sack_blocks && 455d0a14f55SPaul Saab SEQ_LT(tp->snd_fack, sblkp->end)) 456d0a14f55SPaul Saab tp->snd_fack = sblkp->end; 457d0a14f55SPaul Saab } 4589d17a7a6SPaul Saab } else if (SEQ_LT(tp->snd_fack, sblkp->end)) 4599d17a7a6SPaul Saab /* fack is advanced. */ 4609d17a7a6SPaul Saab tp->snd_fack = sblkp->end; 461632bbf0fSRobert Watson /* We must have at least one SACK hole in scoreboard. */ 462632bbf0fSRobert Watson KASSERT(!TAILQ_EMPTY(&tp->snd_holes), 463632bbf0fSRobert Watson ("SACK scoreboard must not be empty")); 464632bbf0fSRobert Watson cur = TAILQ_LAST(&tp->snd_holes, sackhole_head); /* Last SACK hole. */ 46564b5fbaaSPaul Saab /* 46664b5fbaaSPaul Saab * Since the incoming sack blocks are sorted, we can process them 46764b5fbaaSPaul Saab * making one sweep of the scoreboard. 46864b5fbaaSPaul Saab */ 469d0a14f55SPaul Saab while (sblkp >= sack_blocks && cur != NULL) { 4709d17a7a6SPaul Saab if (SEQ_GEQ(sblkp->start, cur->end)) { 47164b5fbaaSPaul Saab /* 472632bbf0fSRobert Watson * SACKs data beyond the current hole. Go to the 473632bbf0fSRobert Watson * previous sack block. 47464b5fbaaSPaul Saab */ 4759d17a7a6SPaul Saab sblkp--; 4769d17a7a6SPaul Saab continue; 4779d17a7a6SPaul Saab } 4789d17a7a6SPaul Saab if (SEQ_LEQ(sblkp->end, cur->start)) { 4799d17a7a6SPaul Saab /* 480632bbf0fSRobert Watson * SACKs data before the current hole. Go to the 481632bbf0fSRobert Watson * previous hole. 4829d17a7a6SPaul Saab */ 4839d17a7a6SPaul Saab cur = TAILQ_PREV(cur, sackhole_head, scblink); 4846d90faf3SPaul Saab continue; 4856d90faf3SPaul Saab } 48664b5fbaaSPaul Saab tp->sackhint.sack_bytes_rexmit -= (cur->rxmit - cur->start); 4870077b016SPaul Saab KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 4880077b016SPaul Saab ("sackhint bytes rtx >= 0")); 4899d17a7a6SPaul Saab if (SEQ_LEQ(sblkp->start, cur->start)) { 490632bbf0fSRobert Watson /* Data acks at least the beginning of hole. */ 4919d17a7a6SPaul Saab if (SEQ_GEQ(sblkp->end, cur->end)) { 492632bbf0fSRobert Watson /* Acks entire hole, so delete hole. */ 493a6235da6SPaul Saab temp = cur; 4949d17a7a6SPaul Saab cur = TAILQ_PREV(cur, sackhole_head, scblink); 4959d17a7a6SPaul Saab tcp_sackhole_remove(tp, temp); 49664b5fbaaSPaul Saab /* 497632bbf0fSRobert Watson * The sack block may ack all or part of the 498632bbf0fSRobert Watson * next hole too, so continue onto the next 499632bbf0fSRobert Watson * hole. 50064b5fbaaSPaul Saab */ 5016d90faf3SPaul Saab continue; 5020077b016SPaul Saab } else { 503632bbf0fSRobert Watson /* Move start of hole forward. */ 5049d17a7a6SPaul Saab cur->start = sblkp->end; 5056d90faf3SPaul Saab cur->rxmit = SEQ_MAX(cur->rxmit, cur->start); 5066d90faf3SPaul Saab } 50764b5fbaaSPaul Saab } else { 508632bbf0fSRobert Watson /* Data acks at least the end of hole. */ 5099d17a7a6SPaul Saab if (SEQ_GEQ(sblkp->end, cur->end)) { 510632bbf0fSRobert Watson /* Move end of hole backward. */ 5119d17a7a6SPaul Saab cur->end = sblkp->start; 5126d90faf3SPaul Saab cur->rxmit = SEQ_MIN(cur->rxmit, cur->end); 5130077b016SPaul Saab } else { 5146d90faf3SPaul Saab /* 515632bbf0fSRobert Watson * ACKs some data in middle of a hole; need 516632bbf0fSRobert Watson * to split current hole 5176d90faf3SPaul Saab */ 5189d17a7a6SPaul Saab temp = tcp_sackhole_insert(tp, sblkp->end, 5199d17a7a6SPaul Saab cur->end, cur); 5200077b016SPaul Saab if (temp != NULL) { 5219d17a7a6SPaul Saab if (SEQ_GT(cur->rxmit, temp->rxmit)) { 5224fc53245SPaul Saab temp->rxmit = cur->rxmit; 5239d17a7a6SPaul Saab tp->sackhint.sack_bytes_rexmit 5249d17a7a6SPaul Saab += (temp->rxmit 5259d17a7a6SPaul Saab - temp->start); 5269d17a7a6SPaul Saab } 5279d17a7a6SPaul Saab cur->end = sblkp->start; 5289d17a7a6SPaul Saab cur->rxmit = SEQ_MIN(cur->rxmit, 5299d17a7a6SPaul Saab cur->end); 5306d90faf3SPaul Saab } 5316d90faf3SPaul Saab } 5320077b016SPaul Saab } 53364b5fbaaSPaul Saab tp->sackhint.sack_bytes_rexmit += (cur->rxmit - cur->start); 534db4b83feSPaul Saab /* 535db4b83feSPaul Saab * Testing sblkp->start against cur->start tells us whether 536db4b83feSPaul Saab * we're done with the sack block or the sack hole. 537db4b83feSPaul Saab * Accordingly, we advance one or the other. 538db4b83feSPaul Saab */ 539db4b83feSPaul Saab if (SEQ_LEQ(sblkp->start, cur->start)) 540db4b83feSPaul Saab cur = TAILQ_PREV(cur, sackhole_head, scblink); 541db4b83feSPaul Saab else 542db4b83feSPaul Saab sblkp--; 5436d90faf3SPaul Saab } 5446d90faf3SPaul Saab } 5456d90faf3SPaul Saab 5466d90faf3SPaul Saab /* 5475a53ca16SPaul Saab * Free all SACK holes to clear the scoreboard. 5486d90faf3SPaul Saab */ 5496d90faf3SPaul Saab void 5506d90faf3SPaul Saab tcp_free_sackholes(struct tcpcb *tp) 5516d90faf3SPaul Saab { 552a6235da6SPaul Saab struct sackhole *q; 5536d90faf3SPaul Saab 5548501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 5559d17a7a6SPaul Saab while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL) 5569d17a7a6SPaul Saab tcp_sackhole_remove(tp, q); 5570077b016SPaul Saab tp->sackhint.sack_bytes_rexmit = 0; 5584fc53245SPaul Saab 5594fc53245SPaul Saab KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0")); 5609d17a7a6SPaul Saab KASSERT(tp->sackhint.nexthole == NULL, 5619d17a7a6SPaul Saab ("tp->sackhint.nexthole == NULL")); 5626d90faf3SPaul Saab } 5636d90faf3SPaul Saab 5646d90faf3SPaul Saab /* 565632bbf0fSRobert Watson * Partial ack handling within a sack recovery episode. Keeping this very 566632bbf0fSRobert Watson * simple for now. When a partial ack is received, force snd_cwnd to a value 567632bbf0fSRobert Watson * that will allow the sender to transmit no more than 2 segments. If 568632bbf0fSRobert Watson * necessary, a better scheme can be adopted at a later point, but for now, 569632bbf0fSRobert Watson * the goal is to prevent the sender from bursting a large amount of data in 570632bbf0fSRobert Watson * the midst of sack recovery. 5716d90faf3SPaul Saab */ 5726d90faf3SPaul Saab void 573ad3f9ab3SAndre Oppermann tcp_sack_partialack(struct tcpcb *tp, struct tcphdr *th) 5746d90faf3SPaul Saab { 5758db456bfSPaul Saab int num_segs = 1; 5766d90faf3SPaul Saab 5778501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 578b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 5796d90faf3SPaul Saab tp->t_rtttime = 0; 580632bbf0fSRobert Watson /* Send one or 2 segments based on how much new data was acked. */ 581c45e1b3cSWeongyo Jeong if ((BYTES_THIS_ACK(tp, th) / tp->t_maxseg) >= 2) 5828db456bfSPaul Saab num_segs = 2; 5830077b016SPaul Saab tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit + 584632bbf0fSRobert Watson (tp->snd_nxt - tp->sack_newdata) + num_segs * tp->t_maxseg); 58525e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 58625e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 5876d90faf3SPaul Saab tp->t_flags |= TF_ACKNOW; 5886d90faf3SPaul Saab (void) tcp_output(tp); 5896d90faf3SPaul Saab } 5906d90faf3SPaul Saab 5911714e18eSMohan Srinivasan #if 0 5926d90faf3SPaul Saab /* 5930077b016SPaul Saab * Debug version of tcp_sack_output() that walks the scoreboard. Used for 5940077b016SPaul Saab * now to sanity check the hint. 5956d90faf3SPaul Saab */ 5960077b016SPaul Saab static struct sackhole * 5970077b016SPaul Saab tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt) 5986d90faf3SPaul Saab { 599a6235da6SPaul Saab struct sackhole *p; 6006d90faf3SPaul Saab 6018501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 602a55db2b6SPaul Saab *sack_bytes_rexmt = 0; 603a6235da6SPaul Saab TAILQ_FOREACH(p, &tp->snd_holes, scblink) { 6046d90faf3SPaul Saab if (SEQ_LT(p->rxmit, p->end)) { 6056d90faf3SPaul Saab if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */ 6066d90faf3SPaul Saab continue; 6076d90faf3SPaul Saab } 608a55db2b6SPaul Saab *sack_bytes_rexmt += (p->rxmit - p->start); 609a55db2b6SPaul Saab break; 610a55db2b6SPaul Saab } 611a55db2b6SPaul Saab *sack_bytes_rexmt += (p->rxmit - p->start); 612a55db2b6SPaul Saab } 6136d90faf3SPaul Saab return (p); 6146d90faf3SPaul Saab } 6151714e18eSMohan Srinivasan #endif 6166d90faf3SPaul Saab 6176d90faf3SPaul Saab /* 6180077b016SPaul Saab * Returns the next hole to retransmit and the number of retransmitted bytes 6190077b016SPaul Saab * from the scoreboard. We store both the next hole and the number of 6200077b016SPaul Saab * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK 6210077b016SPaul Saab * reception). This avoids scoreboard traversals completely. 6220077b016SPaul Saab * 623632bbf0fSRobert Watson * The loop here will traverse *at most* one link. Here's the argument. For 624632bbf0fSRobert Watson * the loop to traverse more than 1 link before finding the next hole to 625632bbf0fSRobert Watson * retransmit, we would need to have at least 1 node following the current 626632bbf0fSRobert Watson * hint with (rxmit == end). But, for all holes following the current hint, 627632bbf0fSRobert Watson * (start == rxmit), since we have not yet retransmitted from them. 628632bbf0fSRobert Watson * Therefore, in order to traverse more 1 link in the loop below, we need to 629632bbf0fSRobert Watson * have at least one node following the current hint with (start == rxmit == 630632bbf0fSRobert Watson * end). But that can't happen, (start == end) means that all the data in 631632bbf0fSRobert Watson * that hole has been sacked, in which case, the hole would have been removed 632632bbf0fSRobert Watson * from the scoreboard. 6330077b016SPaul Saab */ 6340077b016SPaul Saab struct sackhole * 6350077b016SPaul Saab tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt) 6360077b016SPaul Saab { 6371714e18eSMohan Srinivasan struct sackhole *hole = NULL; 6380077b016SPaul Saab 6398501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 6400077b016SPaul Saab *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit; 6410077b016SPaul Saab hole = tp->sackhint.nexthole; 6420077b016SPaul Saab if (hole == NULL || SEQ_LT(hole->rxmit, hole->end)) 6430077b016SPaul Saab goto out; 6440077b016SPaul Saab while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) { 6450077b016SPaul Saab if (SEQ_LT(hole->rxmit, hole->end)) { 6460077b016SPaul Saab tp->sackhint.nexthole = hole; 6470077b016SPaul Saab break; 6480077b016SPaul Saab } 6490077b016SPaul Saab } 6500077b016SPaul Saab out: 6510077b016SPaul Saab return (hole); 6520077b016SPaul Saab } 6530077b016SPaul Saab 6540077b016SPaul Saab /* 6556d90faf3SPaul Saab * After a timeout, the SACK list may be rebuilt. This SACK information 6566d90faf3SPaul Saab * should be used to avoid retransmitting SACKed data. This function 6576d90faf3SPaul Saab * traverses the SACK list to see if snd_nxt should be moved forward. 6586d90faf3SPaul Saab */ 6596d90faf3SPaul Saab void 6606d90faf3SPaul Saab tcp_sack_adjust(struct tcpcb *tp) 6616d90faf3SPaul Saab { 662a6235da6SPaul Saab struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes); 663b7c75571SPaul Saab 6648501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 6656d90faf3SPaul Saab if (cur == NULL) 6666d90faf3SPaul Saab return; /* No holes */ 667808f11b7SPaul Saab if (SEQ_GEQ(tp->snd_nxt, tp->snd_fack)) 6686d90faf3SPaul Saab return; /* We're already beyond any SACKed blocks */ 669632bbf0fSRobert Watson /*- 6706d90faf3SPaul Saab * Two cases for which we want to advance snd_nxt: 6716d90faf3SPaul Saab * i) snd_nxt lies between end of one hole and beginning of another 672808f11b7SPaul Saab * ii) snd_nxt lies between end of last hole and snd_fack 6736d90faf3SPaul Saab */ 674a6235da6SPaul Saab while ((p = TAILQ_NEXT(cur, scblink)) != NULL) { 6756d90faf3SPaul Saab if (SEQ_LT(tp->snd_nxt, cur->end)) 6766d90faf3SPaul Saab return; 677a6235da6SPaul Saab if (SEQ_GEQ(tp->snd_nxt, p->start)) 678a6235da6SPaul Saab cur = p; 6796d90faf3SPaul Saab else { 680a6235da6SPaul Saab tp->snd_nxt = p->start; 6816d90faf3SPaul Saab return; 6826d90faf3SPaul Saab } 6836d90faf3SPaul Saab } 6846d90faf3SPaul Saab if (SEQ_LT(tp->snd_nxt, cur->end)) 6856d90faf3SPaul Saab return; 686808f11b7SPaul Saab tp->snd_nxt = tp->snd_fack; 6876d90faf3SPaul Saab } 688