1c398230bSWarner Losh /*- 26d90faf3SPaul Saab * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 36d90faf3SPaul Saab * The Regents of the University of California. All rights reserved. 46d90faf3SPaul Saab * 56d90faf3SPaul Saab * Redistribution and use in source and binary forms, with or without 66d90faf3SPaul Saab * modification, are permitted provided that the following conditions 76d90faf3SPaul Saab * are met: 86d90faf3SPaul Saab * 1. Redistributions of source code must retain the above copyright 96d90faf3SPaul Saab * notice, this list of conditions and the following disclaimer. 106d90faf3SPaul Saab * 2. Redistributions in binary form must reproduce the above copyright 116d90faf3SPaul Saab * notice, this list of conditions and the following disclaimer in the 126d90faf3SPaul Saab * documentation and/or other materials provided with the distribution. 136d90faf3SPaul Saab * 4. Neither the name of the University nor the names of its contributors 146d90faf3SPaul Saab * may be used to endorse or promote products derived from this software 156d90faf3SPaul Saab * without specific prior written permission. 166d90faf3SPaul Saab * 176d90faf3SPaul Saab * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 186d90faf3SPaul Saab * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 196d90faf3SPaul Saab * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 206d90faf3SPaul Saab * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 216d90faf3SPaul Saab * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 226d90faf3SPaul Saab * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 236d90faf3SPaul Saab * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 246d90faf3SPaul Saab * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 256d90faf3SPaul Saab * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 266d90faf3SPaul Saab * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 276d90faf3SPaul Saab * SUCH DAMAGE. 286d90faf3SPaul Saab * 296d90faf3SPaul Saab * @(#)tcp_sack.c 8.12 (Berkeley) 5/24/95 306d90faf3SPaul Saab * $FreeBSD$ 316d90faf3SPaul Saab */ 326d90faf3SPaul Saab 33c398230bSWarner Losh /*- 346d90faf3SPaul Saab * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994 356d90faf3SPaul Saab * The Regents of the University of California. All rights reserved. 366d90faf3SPaul Saab * 376d90faf3SPaul Saab * Redistribution and use in source and binary forms, with or without 386d90faf3SPaul Saab * modification, are permitted provided that the following conditions 396d90faf3SPaul Saab * are met: 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. Neither the name of the University nor the names of its contributors 466d90faf3SPaul Saab * may be used to endorse or promote products derived from this software 476d90faf3SPaul Saab * without specific prior written permission. 486d90faf3SPaul Saab * 496d90faf3SPaul Saab * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 506d90faf3SPaul Saab * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 516d90faf3SPaul Saab * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 526d90faf3SPaul Saab * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 536d90faf3SPaul Saab * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 546d90faf3SPaul Saab * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 556d90faf3SPaul Saab * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 566d90faf3SPaul Saab * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 576d90faf3SPaul Saab * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 586d90faf3SPaul Saab * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 596d90faf3SPaul Saab * SUCH DAMAGE. 606d90faf3SPaul Saab * 616d90faf3SPaul Saab * @@(#)COPYRIGHT 1.1 (NRL) 17 January 1995 626d90faf3SPaul Saab * 636d90faf3SPaul Saab * NRL grants permission for redistribution and use in source and binary 646d90faf3SPaul Saab * forms, with or without modification, of the software and documentation 656d90faf3SPaul Saab * created at NRL provided that the following conditions are met: 666d90faf3SPaul Saab * 676d90faf3SPaul Saab * 1. Redistributions of source code must retain the above copyright 686d90faf3SPaul Saab * notice, this list of conditions and the following disclaimer. 696d90faf3SPaul Saab * 2. Redistributions in binary form must reproduce the above copyright 706d90faf3SPaul Saab * notice, this list of conditions and the following disclaimer in the 716d90faf3SPaul Saab * documentation and/or other materials provided with the distribution. 726d90faf3SPaul Saab * 3. All advertising materials mentioning features or use of this software 736d90faf3SPaul Saab * must display the following acknowledgements: 746d90faf3SPaul Saab * This product includes software developed by the University of 756d90faf3SPaul Saab * California, Berkeley and its contributors. 766d90faf3SPaul Saab * This product includes software developed at the Information 776d90faf3SPaul Saab * Technology Division, US Naval Research Laboratory. 786d90faf3SPaul Saab * 4. Neither the name of the NRL nor the names of its contributors 796d90faf3SPaul Saab * may be used to endorse or promote products derived from this software 806d90faf3SPaul Saab * without specific prior written permission. 816d90faf3SPaul Saab * 826d90faf3SPaul Saab * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 836d90faf3SPaul Saab * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 846d90faf3SPaul Saab * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 856d90faf3SPaul Saab * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 866d90faf3SPaul Saab * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 876d90faf3SPaul Saab * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 886d90faf3SPaul Saab * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 896d90faf3SPaul Saab * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 906d90faf3SPaul Saab * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 916d90faf3SPaul Saab * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 926d90faf3SPaul Saab * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 936d90faf3SPaul Saab * 946d90faf3SPaul Saab * The views and conclusions contained in the software and documentation 956d90faf3SPaul Saab * are those of the authors and should not be interpreted as representing 966d90faf3SPaul Saab * official policies, either expressed or implied, of the US Naval 976d90faf3SPaul Saab * Research Laboratory (NRL). 986d90faf3SPaul Saab */ 996d90faf3SPaul Saab #include "opt_inet.h" 1006d90faf3SPaul Saab #include "opt_inet6.h" 1016d90faf3SPaul Saab #include "opt_ipsec.h" 1026d90faf3SPaul Saab #include "opt_tcpdebug.h" 1036d90faf3SPaul Saab #include "opt_tcp_input.h" 1046d90faf3SPaul Saab #include "opt_tcp_sack.h" 1056d90faf3SPaul Saab 1066d90faf3SPaul Saab #include <sys/param.h> 1076d90faf3SPaul Saab #include <sys/systm.h> 1086d90faf3SPaul Saab #include <sys/kernel.h> 1096d90faf3SPaul Saab #include <sys/sysctl.h> 1106d90faf3SPaul Saab #include <sys/malloc.h> 1116d90faf3SPaul Saab #include <sys/mbuf.h> 1126d90faf3SPaul Saab #include <sys/proc.h> /* for proc0 declaration */ 1136d90faf3SPaul Saab #include <sys/protosw.h> 1146d90faf3SPaul Saab #include <sys/socket.h> 1156d90faf3SPaul Saab #include <sys/socketvar.h> 1166d90faf3SPaul Saab #include <sys/syslog.h> 1176d90faf3SPaul Saab #include <sys/systm.h> 1186d90faf3SPaul Saab 1196d90faf3SPaul Saab #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 1206d90faf3SPaul Saab 1216d90faf3SPaul Saab #include <vm/uma.h> 1226d90faf3SPaul Saab 1236d90faf3SPaul Saab #include <net/if.h> 1246d90faf3SPaul Saab #include <net/route.h> 1256d90faf3SPaul Saab 1266d90faf3SPaul Saab #include <netinet/in.h> 1276d90faf3SPaul Saab #include <netinet/in_systm.h> 1286d90faf3SPaul Saab #include <netinet/ip.h> 1296d90faf3SPaul Saab #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 1306d90faf3SPaul Saab #include <netinet/in_var.h> 1316d90faf3SPaul Saab #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 1326d90faf3SPaul Saab #include <netinet/in_pcb.h> 1336d90faf3SPaul Saab #include <netinet/ip_var.h> 1346d90faf3SPaul Saab #include <netinet/ip6.h> 1356d90faf3SPaul Saab #include <netinet/icmp6.h> 1366d90faf3SPaul Saab #include <netinet6/nd6.h> 1376d90faf3SPaul Saab #include <netinet6/ip6_var.h> 1386d90faf3SPaul Saab #include <netinet6/in6_pcb.h> 1396d90faf3SPaul Saab #include <netinet/tcp.h> 1406d90faf3SPaul Saab #include <netinet/tcp_fsm.h> 1416d90faf3SPaul Saab #include <netinet/tcp_seq.h> 1426d90faf3SPaul Saab #include <netinet/tcp_timer.h> 1436d90faf3SPaul Saab #include <netinet/tcp_var.h> 1446d90faf3SPaul Saab #include <netinet6/tcp6_var.h> 1456d90faf3SPaul Saab #include <netinet/tcpip.h> 1466d90faf3SPaul Saab #ifdef TCPDEBUG 1476d90faf3SPaul Saab #include <netinet/tcp_debug.h> 1486d90faf3SPaul Saab #endif /* TCPDEBUG */ 1496d90faf3SPaul Saab 1506d90faf3SPaul Saab #ifdef FAST_IPSEC 1516d90faf3SPaul Saab #include <netipsec/ipsec.h> 1526d90faf3SPaul Saab #include <netipsec/ipsec6.h> 1536d90faf3SPaul Saab #endif 1546d90faf3SPaul Saab 1556d90faf3SPaul Saab #ifdef IPSEC 1566d90faf3SPaul Saab #include <netinet6/ipsec.h> 1576d90faf3SPaul Saab #include <netinet6/ipsec6.h> 1586d90faf3SPaul Saab #include <netkey/key.h> 1596d90faf3SPaul Saab #endif /*IPSEC*/ 1606d90faf3SPaul Saab #include <machine/in_cksum.h> 1616d90faf3SPaul Saab 1626d90faf3SPaul Saab extern struct uma_zone *sack_hole_zone; 1636d90faf3SPaul Saab 164a55db2b6SPaul Saab SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW, 0, "TCP SACK"); 165a55db2b6SPaul Saab int tcp_do_sack = 1; 166a55db2b6SPaul Saab SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, enable, CTLFLAG_RW, 167a55db2b6SPaul Saab &tcp_do_sack, 0, "Enable/Disable TCP SACK support"); 168c4274833SRobert Watson TUNABLE_INT("net.inet.tcp.sack.enable", &tcp_do_sack); 169a55db2b6SPaul Saab 170e891d82bSPaul Saab static int tcp_sack_maxholes = 128; 171e891d82bSPaul Saab SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, maxholes, CTLFLAG_RW, 172e891d82bSPaul Saab &tcp_sack_maxholes, 0, 173e891d82bSPaul Saab "Maximum number of TCP SACK holes allowed per connection"); 174e891d82bSPaul Saab 175e891d82bSPaul Saab static int tcp_sack_globalmaxholes = 65536; 176e891d82bSPaul Saab SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalmaxholes, CTLFLAG_RW, 177e891d82bSPaul Saab &tcp_sack_globalmaxholes, 0, 178e891d82bSPaul Saab "Global maximum number of TCP SACK holes"); 179e891d82bSPaul Saab 180e891d82bSPaul Saab static int tcp_sack_globalholes = 0; 181e891d82bSPaul Saab SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, globalholes, CTLFLAG_RD, 182e891d82bSPaul Saab &tcp_sack_globalholes, 0, 183e891d82bSPaul Saab "Global number of TCP SACK holes currently allocated"); 184b7c75571SPaul Saab 1856d90faf3SPaul Saab /* 1866d90faf3SPaul Saab * This function is called upon receipt of new valid data (while not in header 1876d90faf3SPaul Saab * prediction mode), and it updates the ordered list of sacks. 1886d90faf3SPaul Saab */ 1896d90faf3SPaul Saab void 190b7c75571SPaul Saab tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_start, tcp_seq rcv_end) 1916d90faf3SPaul Saab { 1926d90faf3SPaul Saab /* 1936d90faf3SPaul Saab * First reported block MUST be the most recent one. Subsequent 1946d90faf3SPaul Saab * blocks SHOULD be in the order in which they arrived at the 1956d90faf3SPaul Saab * receiver. These two conditions make the implementation fully 1966d90faf3SPaul Saab * compliant with RFC 2018. 1976d90faf3SPaul Saab */ 198b7c75571SPaul Saab struct sackblk head_blk, saved_blks[MAX_SACK_BLKS]; 199b7c75571SPaul Saab int num_head, num_saved, i; 2006d90faf3SPaul Saab 2016d90faf3SPaul Saab INP_LOCK_ASSERT(tp->t_inpcb); 202b7c75571SPaul Saab 203b7c75571SPaul Saab /* Check arguments */ 204b7c75571SPaul Saab KASSERT(SEQ_LT(rcv_start, rcv_end), ("rcv_start < rcv_end")); 205b7c75571SPaul Saab 206b7c75571SPaul Saab /* SACK block for the received segment. */ 207b7c75571SPaul Saab head_blk.start = rcv_start; 208b7c75571SPaul Saab head_blk.end = rcv_end; 209b7c75571SPaul Saab 210b7c75571SPaul Saab /* 211b7c75571SPaul Saab * Merge updated SACK blocks into head_blk, and 212b7c75571SPaul Saab * save unchanged SACK blocks into saved_blks[]. 213b7c75571SPaul Saab * num_saved will have the number of the saved SACK blocks. 214b7c75571SPaul Saab */ 215b7c75571SPaul Saab num_saved = 0; 2166d90faf3SPaul Saab for (i = 0; i < tp->rcv_numsacks; i++) { 217b7c75571SPaul Saab tcp_seq start = tp->sackblks[i].start; 218b7c75571SPaul Saab tcp_seq end = tp->sackblks[i].end; 219b7c75571SPaul Saab if (SEQ_GEQ(start, end) || SEQ_LEQ(start, tp->rcv_nxt)) { 220b7c75571SPaul Saab /* 221b7c75571SPaul Saab * Discard this SACK block. 222b7c75571SPaul Saab */ 223b7c75571SPaul Saab } else if (SEQ_LEQ(head_blk.start, end) && 224b7c75571SPaul Saab SEQ_GEQ(head_blk.end, start)) { 225b7c75571SPaul Saab /* 226b7c75571SPaul Saab * Merge this SACK block into head_blk. 227b7c75571SPaul Saab * This SACK block itself will be discarded. 228b7c75571SPaul Saab */ 229b7c75571SPaul Saab if (SEQ_GT(head_blk.start, start)) 230b7c75571SPaul Saab head_blk.start = start; 231b7c75571SPaul Saab if (SEQ_LT(head_blk.end, end)) 232b7c75571SPaul Saab head_blk.end = end; 2336d90faf3SPaul Saab } else { 2346d90faf3SPaul Saab /* 235b7c75571SPaul Saab * Save this SACK block. 2366d90faf3SPaul Saab */ 237b7c75571SPaul Saab saved_blks[num_saved].start = start; 238b7c75571SPaul Saab saved_blks[num_saved].end = end; 239b7c75571SPaul Saab num_saved++; 240b7c75571SPaul Saab } 241b7c75571SPaul Saab } 242b7c75571SPaul Saab 2436d90faf3SPaul Saab /* 244b7c75571SPaul Saab * Update SACK list in tp->sackblks[]. 2456d90faf3SPaul Saab */ 246b7c75571SPaul Saab num_head = 0; 247b7c75571SPaul Saab if (SEQ_GT(head_blk.start, tp->rcv_nxt)) { 248b7c75571SPaul Saab /* 249b7c75571SPaul Saab * The received data segment is an out-of-order segment. 250b7c75571SPaul Saab * Put head_blk at the top of SACK list. 251b7c75571SPaul Saab */ 252b7c75571SPaul Saab tp->sackblks[0] = head_blk; 253b7c75571SPaul Saab num_head = 1; 254b7c75571SPaul Saab /* 255b7c75571SPaul Saab * If the number of saved SACK blocks exceeds its limit, 256b7c75571SPaul Saab * discard the last SACK block. 257b7c75571SPaul Saab */ 258b7c75571SPaul Saab if (num_saved >= MAX_SACK_BLKS) 259b7c75571SPaul Saab num_saved--; 2606d90faf3SPaul Saab } 261b7c75571SPaul Saab if (num_saved > 0) { 262b7c75571SPaul Saab /* 263b7c75571SPaul Saab * Copy the saved SACK blocks back. 264b7c75571SPaul Saab */ 265b7c75571SPaul Saab bcopy(saved_blks, &tp->sackblks[num_head], 266b7c75571SPaul Saab sizeof(struct sackblk) * num_saved); 2676d90faf3SPaul Saab } 268b7c75571SPaul Saab 269b7c75571SPaul Saab /* Save the number of SACK blocks. */ 270b7c75571SPaul Saab tp->rcv_numsacks = num_head + num_saved; 2716d90faf3SPaul Saab } 2726d90faf3SPaul Saab 2736d90faf3SPaul Saab /* 2746d90faf3SPaul Saab * Delete all receiver-side SACK information. 2756d90faf3SPaul Saab */ 2766d90faf3SPaul Saab void 2776d90faf3SPaul Saab tcp_clean_sackreport(tp) 2786d90faf3SPaul Saab struct tcpcb *tp; 2796d90faf3SPaul Saab { 2806d90faf3SPaul Saab int i; 2816d90faf3SPaul Saab 2826d90faf3SPaul Saab INP_LOCK_ASSERT(tp->t_inpcb); 2836d90faf3SPaul Saab tp->rcv_numsacks = 0; 2846d90faf3SPaul Saab for (i = 0; i < MAX_SACK_BLKS; i++) 2856d90faf3SPaul Saab tp->sackblks[i].start = tp->sackblks[i].end=0; 2866d90faf3SPaul Saab } 2876d90faf3SPaul Saab 2886d90faf3SPaul Saab /* 2894fc53245SPaul Saab * Allocate struct sackhole. 2904fc53245SPaul Saab */ 2914fc53245SPaul Saab static struct sackhole * 2924fc53245SPaul Saab tcp_sackhole_alloc(struct tcpcb *tp, tcp_seq start, tcp_seq end) 2934fc53245SPaul Saab { 2944fc53245SPaul Saab struct sackhole *hole; 2954fc53245SPaul Saab 2964fc53245SPaul Saab if (tp->snd_numholes >= tcp_sack_maxholes || 2974fc53245SPaul Saab tcp_sack_globalholes >= tcp_sack_globalmaxholes) { 2984fc53245SPaul Saab tcpstat.tcps_sack_sboverflow++; 2994fc53245SPaul Saab return NULL; 3004fc53245SPaul Saab } 3014fc53245SPaul Saab 3024fc53245SPaul Saab hole = (struct sackhole *)uma_zalloc(sack_hole_zone, M_NOWAIT); 3034fc53245SPaul Saab if (hole == NULL) 3044fc53245SPaul Saab return NULL; 3054fc53245SPaul Saab 3064fc53245SPaul Saab hole->start = start; 3074fc53245SPaul Saab hole->end = end; 3084fc53245SPaul Saab hole->rxmit = start; 3094fc53245SPaul Saab 3104fc53245SPaul Saab tp->snd_numholes++; 3114fc53245SPaul Saab tcp_sack_globalholes++; 3124fc53245SPaul Saab 3134fc53245SPaul Saab return hole; 3144fc53245SPaul Saab } 3154fc53245SPaul Saab 3164fc53245SPaul Saab /* 3174fc53245SPaul Saab * Free struct sackhole. 3184fc53245SPaul Saab */ 3194fc53245SPaul Saab static void 3204fc53245SPaul Saab tcp_sackhole_free(struct tcpcb *tp, struct sackhole *hole) 3214fc53245SPaul Saab { 3224fc53245SPaul Saab uma_zfree(sack_hole_zone, hole); 3234fc53245SPaul Saab 3244fc53245SPaul Saab tp->snd_numholes--; 3254fc53245SPaul Saab tcp_sack_globalholes--; 3264fc53245SPaul Saab 3274fc53245SPaul Saab KASSERT(tp->snd_numholes >= 0, ("tp->snd_numholes >= 0")); 3284fc53245SPaul Saab KASSERT(tcp_sack_globalholes >= 0, ("tcp_sack_globalholes >= 0")); 3294fc53245SPaul Saab } 3304fc53245SPaul Saab 3314fc53245SPaul Saab /* 3326d90faf3SPaul Saab * Process the TCP SACK option. Returns 1 if tcp_dooptions() should continue, 3336d90faf3SPaul Saab * and 0 otherwise, if the option was fine. tp->snd_holes is an ordered list 3346d90faf3SPaul Saab * of holes (oldest to newest, in terms of the sequence space). 3356d90faf3SPaul Saab */ 3366d90faf3SPaul Saab int 3376d90faf3SPaul Saab tcp_sack_option(struct tcpcb *tp, struct tcphdr *th, u_char *cp, int optlen) 3386d90faf3SPaul Saab { 3396d90faf3SPaul Saab int tmp_olen; 3406d90faf3SPaul Saab u_char *tmp_cp; 341a6235da6SPaul Saab struct sackhole *cur, *temp; 3426d90faf3SPaul Saab 3436d90faf3SPaul Saab INP_LOCK_ASSERT(tp->t_inpcb); 3446d90faf3SPaul Saab if (!tp->sack_enable) 3456d90faf3SPaul Saab return (1); 346e891d82bSPaul Saab if ((th->th_flags & TH_ACK) == 0) 347e891d82bSPaul Saab return (1); 3486d90faf3SPaul Saab /* Note: TCPOLEN_SACK must be 2*sizeof(tcp_seq) */ 3496d90faf3SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 3506d90faf3SPaul Saab return (1); 351e891d82bSPaul Saab /* If ack is outside [snd_una, snd_max], ignore the SACK options */ 35282912940SPaul Saab if (SEQ_LT(th->th_ack, tp->snd_una) || SEQ_GT(th->th_ack, tp->snd_max)) 35382912940SPaul Saab return (1); 3546d90faf3SPaul Saab tmp_cp = cp + 2; 3556d90faf3SPaul Saab tmp_olen = optlen - 2; 3566d90faf3SPaul Saab tcpstat.tcps_sack_rcv_blocks++; 3576d90faf3SPaul Saab if (tp->t_maxseg == 0) 3586d90faf3SPaul Saab panic("tcp_sack_option"); /* Should never happen */ 3596d90faf3SPaul Saab while (tmp_olen > 0) { 3606d90faf3SPaul Saab struct sackblk sack; 3616d90faf3SPaul Saab 3626d90faf3SPaul Saab bcopy(tmp_cp, (char *) &(sack.start), sizeof(tcp_seq)); 3636d90faf3SPaul Saab sack.start = ntohl(sack.start); 3646d90faf3SPaul Saab bcopy(tmp_cp + sizeof(tcp_seq), 3656d90faf3SPaul Saab (char *) &(sack.end), sizeof(tcp_seq)); 3666d90faf3SPaul Saab sack.end = ntohl(sack.end); 3676d90faf3SPaul Saab tmp_olen -= TCPOLEN_SACK; 3686d90faf3SPaul Saab tmp_cp += TCPOLEN_SACK; 3696d90faf3SPaul Saab if (SEQ_LEQ(sack.end, sack.start)) 3706d90faf3SPaul Saab continue; /* bad SACK fields */ 3716d90faf3SPaul Saab if (SEQ_LEQ(sack.end, tp->snd_una)) 3726d90faf3SPaul Saab continue; /* old block */ 3736d90faf3SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_una)) { 3746d90faf3SPaul Saab if (SEQ_LT(sack.start, th->th_ack)) 3756d90faf3SPaul Saab continue; 3766d90faf3SPaul Saab } 3776d90faf3SPaul Saab if (SEQ_GT(sack.end, tp->snd_max)) 3786d90faf3SPaul Saab continue; 379a6235da6SPaul Saab if (TAILQ_EMPTY(&tp->snd_holes)) { /* first hole */ 3804fc53245SPaul Saab cur = tcp_sackhole_alloc(tp, th->th_ack, sack.start); 381a6235da6SPaul Saab if (cur == NULL) { 3826d90faf3SPaul Saab /* ENOBUFS, so ignore SACKed block for now*/ 3836d90faf3SPaul Saab continue; 3846d90faf3SPaul Saab } 3854fc53245SPaul Saab TAILQ_INSERT_HEAD(&tp->snd_holes, cur, scblink); 3864fc53245SPaul Saab tp->rcv_lastsack = sack.end; 3870077b016SPaul Saab /* Update the sack scoreboard "cache" */ 3880077b016SPaul Saab tp->sackhint.nexthole = cur; 3896d90faf3SPaul Saab continue; /* with next sack block */ 3906d90faf3SPaul Saab } 391a6235da6SPaul Saab /* Go thru list of holes. */ 392a6235da6SPaul Saab cur = TAILQ_FIRST(&tp->snd_holes); 3936d90faf3SPaul Saab while (cur) { 3946d90faf3SPaul Saab if (SEQ_LEQ(sack.end, cur->start)) 3956d90faf3SPaul Saab /* SACKs data before the current hole */ 3966d90faf3SPaul Saab break; /* no use going through more holes */ 3976d90faf3SPaul Saab if (SEQ_GEQ(sack.start, cur->end)) { 3986d90faf3SPaul Saab /* SACKs data beyond the current hole */ 399a6235da6SPaul Saab cur = TAILQ_NEXT(cur, scblink); 4006d90faf3SPaul Saab continue; 4016d90faf3SPaul Saab } 4020077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= 4030077b016SPaul Saab (cur->rxmit - cur->start); 4040077b016SPaul Saab KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 4050077b016SPaul Saab ("sackhint bytes rtx >= 0")); 4066d90faf3SPaul Saab if (SEQ_LEQ(sack.start, cur->start)) { 4076d90faf3SPaul Saab /* Data acks at least the beginning of hole */ 4086d90faf3SPaul Saab if (SEQ_GEQ(sack.end, cur->end)) { 4096d90faf3SPaul Saab /* Acks entire hole, so delete hole */ 4100077b016SPaul Saab if (tp->sackhint.nexthole == cur) 4110077b016SPaul Saab tp->sackhint.nexthole = 4120077b016SPaul Saab TAILQ_NEXT(cur, scblink); 413a6235da6SPaul Saab temp = cur; 414a6235da6SPaul Saab cur = TAILQ_NEXT(cur, scblink); 415a6235da6SPaul Saab TAILQ_REMOVE(&tp->snd_holes, 416a6235da6SPaul Saab temp, scblink); 4174fc53245SPaul Saab tcp_sackhole_free(tp, temp); 4186d90faf3SPaul Saab continue; 4190077b016SPaul Saab } else { 4200077b016SPaul Saab /* Move start of hole forward */ 4216d90faf3SPaul Saab cur->start = sack.end; 4226d90faf3SPaul Saab cur->rxmit = SEQ_MAX(cur->rxmit, cur->start); 4236d90faf3SPaul Saab } 4240077b016SPaul Saab } else if (SEQ_GEQ(sack.end, cur->end)) { 4250077b016SPaul Saab /* Move end of hole backward */ 4266d90faf3SPaul Saab cur->end = sack.start; 4276d90faf3SPaul Saab cur->rxmit = SEQ_MIN(cur->rxmit, cur->end); 4280077b016SPaul Saab } else { 4296d90faf3SPaul Saab /* 4306d90faf3SPaul Saab * ACKs some data in middle of a hole; need to 4316d90faf3SPaul Saab * split current hole 4326d90faf3SPaul Saab */ 4334fc53245SPaul Saab temp = tcp_sackhole_alloc(tp, sack.end, 4344fc53245SPaul Saab cur->end); 4350077b016SPaul Saab if (temp != NULL) { 4364fc53245SPaul Saab if (SEQ_GT(cur->rxmit, temp->rxmit)) 4374fc53245SPaul Saab temp->rxmit = cur->rxmit; 4384fc53245SPaul Saab TAILQ_INSERT_AFTER(&tp->snd_holes, 4394fc53245SPaul Saab cur, temp, scblink); 4406d90faf3SPaul Saab cur->end = sack.start; 4410077b016SPaul Saab cur->rxmit = SEQ_MIN(cur->rxmit, 4420077b016SPaul Saab cur->end); 4430077b016SPaul Saab tp->sackhint.sack_bytes_rexmit += 4440077b016SPaul Saab (cur->rxmit - cur->start); 4450077b016SPaul Saab cur = temp; 4466d90faf3SPaul Saab } 4476d90faf3SPaul Saab } 4480077b016SPaul Saab tp->sackhint.sack_bytes_rexmit += 4490077b016SPaul Saab (cur->rxmit - cur->start); 4500077b016SPaul Saab cur = TAILQ_NEXT(cur, scblink); 4510077b016SPaul Saab } 4520077b016SPaul Saab /* At this point, we have iterated the whole scoreboard. */ 4536d90faf3SPaul Saab if (SEQ_LT(tp->rcv_lastsack, sack.start)) { 454a6235da6SPaul Saab /* Need to append new hole at end. */ 4554fc53245SPaul Saab temp = tcp_sackhole_alloc(tp, tp->rcv_lastsack, 4564fc53245SPaul Saab sack.start); 4576d90faf3SPaul Saab if (temp == NULL) 4586d90faf3SPaul Saab continue; /* ENOBUFS */ 459a6235da6SPaul Saab TAILQ_INSERT_TAIL(&tp->snd_holes, temp, scblink); 4604fc53245SPaul Saab tp->rcv_lastsack = sack.end; 461fdace17fSPaul Saab if (tp->sackhint.nexthole == NULL) 462fdace17fSPaul Saab tp->sackhint.nexthole = temp; 4636d90faf3SPaul Saab } 464b962fa74SPaul Saab if (SEQ_LT(tp->rcv_lastsack, sack.end)) 465b962fa74SPaul Saab tp->rcv_lastsack = sack.end; 4666d90faf3SPaul Saab } 4676d90faf3SPaul Saab return (0); 4686d90faf3SPaul Saab } 4696d90faf3SPaul Saab 4706d90faf3SPaul Saab /* 4716d90faf3SPaul Saab * Delete stale (i.e, cumulatively ack'd) holes. Hole is deleted only if 4726d90faf3SPaul Saab * it is completely acked; otherwise, tcp_sack_option(), called from 4736d90faf3SPaul Saab * tcp_dooptions(), will fix up the hole. 4746d90faf3SPaul Saab */ 4756d90faf3SPaul Saab void 4766d90faf3SPaul Saab tcp_del_sackholes(tp, th) 4776d90faf3SPaul Saab struct tcpcb *tp; 4786d90faf3SPaul Saab struct tcphdr *th; 4796d90faf3SPaul Saab { 4806d90faf3SPaul Saab INP_LOCK_ASSERT(tp->t_inpcb); 4816d90faf3SPaul Saab if (tp->sack_enable && tp->t_state != TCPS_LISTEN) { 4826d90faf3SPaul Saab /* max because this could be an older ack just arrived */ 4836d90faf3SPaul Saab tcp_seq lastack = SEQ_GT(th->th_ack, tp->snd_una) ? 4846d90faf3SPaul Saab th->th_ack : tp->snd_una; 485a6235da6SPaul Saab struct sackhole *cur = TAILQ_FIRST(&tp->snd_holes); 4866d90faf3SPaul Saab struct sackhole *prev; 4876d90faf3SPaul Saab while (cur) 4886d90faf3SPaul Saab if (SEQ_LEQ(cur->end, lastack)) { 4896d90faf3SPaul Saab prev = cur; 4900077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= 4910077b016SPaul Saab (cur->rxmit - cur->start); 4920077b016SPaul Saab if (tp->sackhint.nexthole == cur) 4930077b016SPaul Saab tp->sackhint.nexthole = 4940077b016SPaul Saab TAILQ_NEXT(cur, scblink); 495a6235da6SPaul Saab cur = TAILQ_NEXT(cur, scblink); 496a6235da6SPaul Saab TAILQ_REMOVE(&tp->snd_holes, prev, scblink); 4974fc53245SPaul Saab tcp_sackhole_free(tp, prev); 4986d90faf3SPaul Saab } else if (SEQ_LT(cur->start, lastack)) { 4990077b016SPaul Saab if (SEQ_LT(cur->rxmit, lastack)) { 5000077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= 5010077b016SPaul Saab (cur->rxmit - cur->start); 5020077b016SPaul Saab cur->rxmit = lastack; 5030077b016SPaul Saab } else 5040077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= 5050077b016SPaul Saab (lastack - cur->start); 5066d90faf3SPaul Saab cur->start = lastack; 5076d90faf3SPaul Saab break; 5086d90faf3SPaul Saab } else 5096d90faf3SPaul Saab break; 5106d90faf3SPaul Saab } 5116d90faf3SPaul Saab } 5126d90faf3SPaul Saab 5136d90faf3SPaul Saab void 5146d90faf3SPaul Saab tcp_free_sackholes(struct tcpcb *tp) 5156d90faf3SPaul Saab { 516a6235da6SPaul Saab struct sackhole *q; 5176d90faf3SPaul Saab 5186d90faf3SPaul Saab INP_LOCK_ASSERT(tp->t_inpcb); 519a6235da6SPaul Saab while ((q = TAILQ_FIRST(&tp->snd_holes)) != NULL) { 520a6235da6SPaul Saab TAILQ_REMOVE(&tp->snd_holes, q, scblink); 5214fc53245SPaul Saab tcp_sackhole_free(tp, q); 5226d90faf3SPaul Saab } 5230077b016SPaul Saab tp->sackhint.nexthole = NULL; 5240077b016SPaul Saab tp->sackhint.sack_bytes_rexmit = 0; 5254fc53245SPaul Saab 5264fc53245SPaul Saab KASSERT(tp->snd_numholes == 0, ("tp->snd_numholes == 0")); 5276d90faf3SPaul Saab } 5286d90faf3SPaul Saab 5296d90faf3SPaul Saab /* 5308db456bfSPaul Saab * Partial ack handling within a sack recovery episode. 5318db456bfSPaul Saab * Keeping this very simple for now. When a partial ack 5328db456bfSPaul Saab * is received, force snd_cwnd to a value that will allow 5338db456bfSPaul Saab * the sender to transmit no more than 2 segments. 5348db456bfSPaul Saab * If necessary, a better scheme can be adopted at a 5358db456bfSPaul Saab * later point, but for now, the goal is to prevent the 5368db456bfSPaul Saab * sender from bursting a large amount of data in the midst 5378db456bfSPaul Saab * of sack recovery. 5386d90faf3SPaul Saab */ 5396d90faf3SPaul Saab void 5406d90faf3SPaul Saab tcp_sack_partialack(tp, th) 5416d90faf3SPaul Saab struct tcpcb *tp; 5426d90faf3SPaul Saab struct tcphdr *th; 5436d90faf3SPaul Saab { 5448db456bfSPaul Saab int num_segs = 1; 5456d90faf3SPaul Saab 54625e6f9edSPaul Saab INP_LOCK_ASSERT(tp->t_inpcb); 5476d90faf3SPaul Saab callout_stop(tp->tt_rexmt); 5486d90faf3SPaul Saab tp->t_rtttime = 0; 5498db456bfSPaul Saab /* send one or 2 segments based on how much new data was acked */ 5508db456bfSPaul Saab if (((th->th_ack - tp->snd_una) / tp->t_maxseg) > 2) 5518db456bfSPaul Saab num_segs = 2; 5520077b016SPaul Saab tp->snd_cwnd = (tp->sackhint.sack_bytes_rexmit + 5530077b016SPaul Saab (tp->snd_nxt - tp->sack_newdata) + 5540077b016SPaul Saab num_segs * tp->t_maxseg); 55525e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 55625e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 5576d90faf3SPaul Saab tp->t_flags |= TF_ACKNOW; 5586d90faf3SPaul Saab (void) tcp_output(tp); 5596d90faf3SPaul Saab } 5606d90faf3SPaul Saab 5616d90faf3SPaul Saab /* 5620077b016SPaul Saab * Debug version of tcp_sack_output() that walks the scoreboard. Used for 5630077b016SPaul Saab * now to sanity check the hint. 5646d90faf3SPaul Saab */ 5650077b016SPaul Saab static struct sackhole * 5660077b016SPaul Saab tcp_sack_output_debug(struct tcpcb *tp, int *sack_bytes_rexmt) 5676d90faf3SPaul Saab { 568a6235da6SPaul Saab struct sackhole *p; 5696d90faf3SPaul Saab 5706d90faf3SPaul Saab INP_LOCK_ASSERT(tp->t_inpcb); 571a55db2b6SPaul Saab *sack_bytes_rexmt = 0; 572a6235da6SPaul Saab TAILQ_FOREACH(p, &tp->snd_holes, scblink) { 5736d90faf3SPaul Saab if (SEQ_LT(p->rxmit, p->end)) { 5746d90faf3SPaul Saab if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */ 5756d90faf3SPaul Saab continue; 5766d90faf3SPaul Saab } 577a55db2b6SPaul Saab *sack_bytes_rexmt += (p->rxmit - p->start); 578a55db2b6SPaul Saab break; 579a55db2b6SPaul Saab } 580a55db2b6SPaul Saab *sack_bytes_rexmt += (p->rxmit - p->start); 581a55db2b6SPaul Saab } 5826d90faf3SPaul Saab return (p); 5836d90faf3SPaul Saab } 5846d90faf3SPaul Saab 5856d90faf3SPaul Saab /* 5860077b016SPaul Saab * Returns the next hole to retransmit and the number of retransmitted bytes 5870077b016SPaul Saab * from the scoreboard. We store both the next hole and the number of 5880077b016SPaul Saab * retransmitted bytes as hints (and recompute these on the fly upon SACK/ACK 5890077b016SPaul Saab * reception). This avoids scoreboard traversals completely. 5900077b016SPaul Saab * 5910077b016SPaul Saab * The loop here will traverse *at most* one link. Here's the argument. 5920077b016SPaul Saab * For the loop to traverse more than 1 link before finding the next hole to 5930077b016SPaul Saab * retransmit, we would need to have at least 1 node following the current hint 5940077b016SPaul Saab * with (rxmit == end). But, for all holes following the current hint, 5950077b016SPaul Saab * (start == rxmit), since we have not yet retransmitted from them. Therefore, 5960077b016SPaul Saab * in order to traverse more 1 link in the loop below, we need to have at least 5970077b016SPaul Saab * one node following the current hint with (start == rxmit == end). 5980077b016SPaul Saab * But that can't happen, (start == end) means that all the data in that hole 5990077b016SPaul Saab * has been sacked, in which case, the hole would have been removed from the 6000077b016SPaul Saab * scoreboard. 6010077b016SPaul Saab */ 6020077b016SPaul Saab struct sackhole * 6030077b016SPaul Saab tcp_sack_output(struct tcpcb *tp, int *sack_bytes_rexmt) 6040077b016SPaul Saab { 6050077b016SPaul Saab struct sackhole *hole = NULL, *dbg_hole = NULL; 6060077b016SPaul Saab int dbg_bytes_rexmt; 6070077b016SPaul Saab 6080077b016SPaul Saab INP_LOCK_ASSERT(tp->t_inpcb); 6090077b016SPaul Saab dbg_hole = tcp_sack_output_debug(tp, &dbg_bytes_rexmt); 6100077b016SPaul Saab *sack_bytes_rexmt = tp->sackhint.sack_bytes_rexmit; 6110077b016SPaul Saab hole = tp->sackhint.nexthole; 6120077b016SPaul Saab if (hole == NULL || SEQ_LT(hole->rxmit, hole->end)) 6130077b016SPaul Saab goto out; 6140077b016SPaul Saab while ((hole = TAILQ_NEXT(hole, scblink)) != NULL) { 6150077b016SPaul Saab if (SEQ_LT(hole->rxmit, hole->end)) { 6160077b016SPaul Saab tp->sackhint.nexthole = hole; 6170077b016SPaul Saab break; 6180077b016SPaul Saab } 6190077b016SPaul Saab } 6200077b016SPaul Saab out: 6210077b016SPaul Saab if (dbg_hole != hole) { 6220077b016SPaul Saab printf("%s: Computed sack hole not the same as cached value\n", __func__); 6230077b016SPaul Saab hole = dbg_hole; 6240077b016SPaul Saab } 6250077b016SPaul Saab if (*sack_bytes_rexmt != dbg_bytes_rexmt) { 6260077b016SPaul Saab printf("%s: Computed sack_bytes_retransmitted (%d) not" 6270077b016SPaul Saab "the same as cached value (%d)\n", 6280077b016SPaul Saab __func__, dbg_bytes_rexmt, *sack_bytes_rexmt); 6290077b016SPaul Saab *sack_bytes_rexmt = dbg_bytes_rexmt; 6300077b016SPaul Saab } 6310077b016SPaul Saab return (hole); 6320077b016SPaul Saab } 6330077b016SPaul Saab 6340077b016SPaul Saab /* 6356d90faf3SPaul Saab * After a timeout, the SACK list may be rebuilt. This SACK information 6366d90faf3SPaul Saab * should be used to avoid retransmitting SACKed data. This function 6376d90faf3SPaul Saab * traverses the SACK list to see if snd_nxt should be moved forward. 6386d90faf3SPaul Saab */ 6396d90faf3SPaul Saab void 6406d90faf3SPaul Saab tcp_sack_adjust(struct tcpcb *tp) 6416d90faf3SPaul Saab { 642a6235da6SPaul Saab struct sackhole *p, *cur = TAILQ_FIRST(&tp->snd_holes); 643b7c75571SPaul Saab 644b7c75571SPaul Saab INP_LOCK_ASSERT(tp->t_inpcb); 6456d90faf3SPaul Saab if (cur == NULL) 6466d90faf3SPaul Saab return; /* No holes */ 6476d90faf3SPaul Saab if (SEQ_GEQ(tp->snd_nxt, tp->rcv_lastsack)) 6486d90faf3SPaul Saab return; /* We're already beyond any SACKed blocks */ 6496d90faf3SPaul Saab /* 6506d90faf3SPaul Saab * Two cases for which we want to advance snd_nxt: 6516d90faf3SPaul Saab * i) snd_nxt lies between end of one hole and beginning of another 6526d90faf3SPaul Saab * ii) snd_nxt lies between end of last hole and rcv_lastsack 6536d90faf3SPaul Saab */ 654a6235da6SPaul Saab while ((p = TAILQ_NEXT(cur, scblink)) != NULL) { 6556d90faf3SPaul Saab if (SEQ_LT(tp->snd_nxt, cur->end)) 6566d90faf3SPaul Saab return; 657a6235da6SPaul Saab if (SEQ_GEQ(tp->snd_nxt, p->start)) 658a6235da6SPaul Saab cur = p; 6596d90faf3SPaul Saab else { 660a6235da6SPaul Saab tp->snd_nxt = p->start; 6616d90faf3SPaul Saab return; 6626d90faf3SPaul Saab } 6636d90faf3SPaul Saab } 6646d90faf3SPaul Saab if (SEQ_LT(tp->snd_nxt, cur->end)) 6656d90faf3SPaul Saab return; 6666d90faf3SPaul Saab tp->snd_nxt = tp->rcv_lastsack; 6676d90faf3SPaul Saab return; 6686d90faf3SPaul Saab } 669