1c398230bSWarner Losh /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3fe267a55SPedro F. Giffuni * 497d8d152SAndre Oppermann * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG 597d8d152SAndre Oppermann * All rights reserved. 697d8d152SAndre Oppermann * 797d8d152SAndre Oppermann * Redistribution and use in source and binary forms, with or without 897d8d152SAndre Oppermann * modification, are permitted provided that the following conditions 997d8d152SAndre Oppermann * are met: 1097d8d152SAndre Oppermann * 1. Redistributions of source code must retain the above copyright 1197d8d152SAndre Oppermann * notice, this list of conditions and the following disclaimer. 1297d8d152SAndre Oppermann * 2. Redistributions in binary form must reproduce the above copyright 1397d8d152SAndre Oppermann * notice, this list of conditions and the following disclaimer in the 1497d8d152SAndre Oppermann * documentation and/or other materials provided with the distribution. 1597d8d152SAndre Oppermann * 3. The name of the author may not be used to endorse or promote 1697d8d152SAndre Oppermann * products derived from this software without specific prior written 1797d8d152SAndre Oppermann * permission. 1897d8d152SAndre Oppermann * 1997d8d152SAndre Oppermann * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2097d8d152SAndre Oppermann * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2197d8d152SAndre Oppermann * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2297d8d152SAndre Oppermann * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2397d8d152SAndre Oppermann * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2497d8d152SAndre Oppermann * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2597d8d152SAndre Oppermann * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2697d8d152SAndre Oppermann * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2797d8d152SAndre Oppermann * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2897d8d152SAndre Oppermann * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2997d8d152SAndre Oppermann * SUCH DAMAGE. 3097d8d152SAndre Oppermann */ 3197d8d152SAndre Oppermann 3297d8d152SAndre Oppermann /* 33e487a5e2SRobert Watson * The tcp_hostcache moves the tcp-specific cached metrics from the routing 34e487a5e2SRobert Watson * table to a dedicated structure indexed by the remote IP address. It keeps 35e487a5e2SRobert Watson * information on the measured TCP parameters of past TCP sessions to allow 36e487a5e2SRobert Watson * better initial start values to be used with later connections to/from the 374d163382SHiren Panchasara * same source. Depending on the network parameters (delay, max MTU, 384d163382SHiren Panchasara * congestion window) between local and remote sites, this can lead to 39e487a5e2SRobert Watson * significant speed-ups for new TCP connections after the first one. 4097d8d152SAndre Oppermann * 41e487a5e2SRobert Watson * Due to the tcp_hostcache, all TCP-specific metrics information in the 42003c7e36SRui Paulo * routing table have been removed. The inpcb no longer keeps a pointer to 43e487a5e2SRobert Watson * the routing entry, and protocol-initiated route cloning has been removed 44e487a5e2SRobert Watson * as well. With these changes, the routing table has gone back to being 45e487a5e2SRobert Watson * more lightwight and only carries information related to packet forwarding. 4697d8d152SAndre Oppermann * 47e487a5e2SRobert Watson * tcp_hostcache is designed for multiple concurrent access in SMP 48e487a5e2SRobert Watson * environments and high contention. All bucket rows have their own lock and 49e487a5e2SRobert Watson * thus multiple lookups and modifies can be done at the same time as long as 50e487a5e2SRobert Watson * they are in different bucket rows. If a request for insertion of a new 51e487a5e2SRobert Watson * record can't be satisfied, it simply returns an empty structure. Nobody 52e487a5e2SRobert Watson * and nothing outside of tcp_hostcache.c will ever point directly to any 53e487a5e2SRobert Watson * entry in the tcp_hostcache. All communication is done in an 54e487a5e2SRobert Watson * object-oriented way and only functions of tcp_hostcache will manipulate 55e487a5e2SRobert Watson * hostcache entries. Otherwise, we are unable to achieve good behaviour in 56e487a5e2SRobert Watson * concurrent access situations. Since tcp_hostcache is only caching 57e487a5e2SRobert Watson * information, there are no fatal consequences if we either can't satisfy 58e487a5e2SRobert Watson * any particular request or have to drop/overwrite an existing entry because 59e487a5e2SRobert Watson * of bucket limit memory constrains. 6097d8d152SAndre Oppermann */ 6197d8d152SAndre Oppermann 6297d8d152SAndre Oppermann /* 6397d8d152SAndre Oppermann * Many thanks to jlemon for basic structure of tcp_syncache which is being 6497d8d152SAndre Oppermann * followed here. 6597d8d152SAndre Oppermann */ 6697d8d152SAndre Oppermann 674b421e2dSMike Silbersack #include <sys/cdefs.h> 684b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 694b421e2dSMike Silbersack 7097d8d152SAndre Oppermann #include "opt_inet6.h" 7197d8d152SAndre Oppermann 7297d8d152SAndre Oppermann #include <sys/param.h> 7397d8d152SAndre Oppermann #include <sys/systm.h> 74b8a2fb91SHiren Panchasara #include <sys/jail.h> 7597d8d152SAndre Oppermann #include <sys/kernel.h> 7697d8d152SAndre Oppermann #include <sys/lock.h> 7797d8d152SAndre Oppermann #include <sys/mutex.h> 7897d8d152SAndre Oppermann #include <sys/malloc.h> 79b8a2fb91SHiren Panchasara #include <sys/proc.h> 80002d4558SJohn Baldwin #include <sys/sbuf.h> 8197d8d152SAndre Oppermann #include <sys/socket.h> 8297d8d152SAndre Oppermann #include <sys/socketvar.h> 8397d8d152SAndre Oppermann #include <sys/sysctl.h> 8497d8d152SAndre Oppermann 8597d8d152SAndre Oppermann #include <net/if.h> 8676039bc8SGleb Smirnoff #include <net/if_var.h> 875736e6fbSBjoern A. Zeeb #include <net/route.h> 88530c0060SRobert Watson #include <net/vnet.h> 8997d8d152SAndre Oppermann 9097d8d152SAndre Oppermann #include <netinet/in.h> 9197d8d152SAndre Oppermann #include <netinet/in_systm.h> 9297d8d152SAndre Oppermann #include <netinet/ip.h> 9397d8d152SAndre Oppermann #include <netinet/in_var.h> 9497d8d152SAndre Oppermann #include <netinet/in_pcb.h> 9597d8d152SAndre Oppermann #include <netinet/ip_var.h> 9697d8d152SAndre Oppermann #ifdef INET6 9797d8d152SAndre Oppermann #include <netinet/ip6.h> 9897d8d152SAndre Oppermann #include <netinet6/ip6_var.h> 9997d8d152SAndre Oppermann #endif 10097d8d152SAndre Oppermann #include <netinet/tcp.h> 10197d8d152SAndre Oppermann #include <netinet/tcp_var.h> 10297d8d152SAndre Oppermann #ifdef INET6 10397d8d152SAndre Oppermann #include <netinet6/tcp6_var.h> 10497d8d152SAndre Oppermann #endif 10597d8d152SAndre Oppermann 10697d8d152SAndre Oppermann #include <vm/uma.h> 10797d8d152SAndre Oppermann 108*2cca4c0eSGleb Smirnoff TAILQ_HEAD(hc_qhead, hc_metrics); 109*2cca4c0eSGleb Smirnoff 110*2cca4c0eSGleb Smirnoff struct hc_head { 111*2cca4c0eSGleb Smirnoff struct hc_qhead hch_bucket; 112*2cca4c0eSGleb Smirnoff u_int hch_length; 113*2cca4c0eSGleb Smirnoff struct mtx hch_mtx; 114*2cca4c0eSGleb Smirnoff }; 115*2cca4c0eSGleb Smirnoff 116*2cca4c0eSGleb Smirnoff struct hc_metrics { 117*2cca4c0eSGleb Smirnoff /* housekeeping */ 118*2cca4c0eSGleb Smirnoff TAILQ_ENTRY(hc_metrics) rmx_q; 119*2cca4c0eSGleb Smirnoff struct hc_head *rmx_head; /* head of bucket tail queue */ 120*2cca4c0eSGleb Smirnoff struct in_addr ip4; /* IP address */ 121*2cca4c0eSGleb Smirnoff struct in6_addr ip6; /* IP6 address */ 122*2cca4c0eSGleb Smirnoff uint32_t ip6_zoneid; /* IPv6 scope zone id */ 123*2cca4c0eSGleb Smirnoff /* endpoint specific values for tcp */ 124*2cca4c0eSGleb Smirnoff uint32_t rmx_mtu; /* MTU for this path */ 125*2cca4c0eSGleb Smirnoff uint32_t rmx_ssthresh; /* outbound gateway buffer limit */ 126*2cca4c0eSGleb Smirnoff uint32_t rmx_rtt; /* estimated round trip time */ 127*2cca4c0eSGleb Smirnoff uint32_t rmx_rttvar; /* estimated rtt variance */ 128*2cca4c0eSGleb Smirnoff uint32_t rmx_cwnd; /* congestion window */ 129*2cca4c0eSGleb Smirnoff uint32_t rmx_sendpipe; /* outbound delay-bandwidth product */ 130*2cca4c0eSGleb Smirnoff uint32_t rmx_recvpipe; /* inbound delay-bandwidth product */ 131*2cca4c0eSGleb Smirnoff /* TCP hostcache internal data */ 132*2cca4c0eSGleb Smirnoff int rmx_expire; /* lifetime for object */ 133*2cca4c0eSGleb Smirnoff u_long rmx_hits; /* number of hits */ 134*2cca4c0eSGleb Smirnoff u_long rmx_updates; /* number of updates */ 135*2cca4c0eSGleb Smirnoff }; 136*2cca4c0eSGleb Smirnoff 137*2cca4c0eSGleb Smirnoff struct tcp_hostcache { 138*2cca4c0eSGleb Smirnoff struct hc_head *hashbase; 139*2cca4c0eSGleb Smirnoff uma_zone_t zone; 140*2cca4c0eSGleb Smirnoff u_int hashsize; 141*2cca4c0eSGleb Smirnoff u_int hashmask; 142*2cca4c0eSGleb Smirnoff u_int bucket_limit; 143*2cca4c0eSGleb Smirnoff u_int cache_count; 144*2cca4c0eSGleb Smirnoff u_int cache_limit; 145*2cca4c0eSGleb Smirnoff int expire; 146*2cca4c0eSGleb Smirnoff int prune; 147*2cca4c0eSGleb Smirnoff int purgeall; 148*2cca4c0eSGleb Smirnoff }; 149*2cca4c0eSGleb Smirnoff 15097d8d152SAndre Oppermann /* Arbitrary values */ 15197d8d152SAndre Oppermann #define TCP_HOSTCACHE_HASHSIZE 512 15297d8d152SAndre Oppermann #define TCP_HOSTCACHE_BUCKETLIMIT 30 15397d8d152SAndre Oppermann #define TCP_HOSTCACHE_EXPIRE 60*60 /* one hour */ 15497d8d152SAndre Oppermann #define TCP_HOSTCACHE_PRUNE 5*60 /* every 5 minutes */ 15597d8d152SAndre Oppermann 1565f901c92SAndrew Turner VNET_DEFINE_STATIC(struct tcp_hostcache, tcp_hostcache); 1571e77c105SRobert Watson #define V_tcp_hostcache VNET(tcp_hostcache) 15882cea7e6SBjoern A. Zeeb 1595f901c92SAndrew Turner VNET_DEFINE_STATIC(struct callout, tcp_hc_callout); 1601e77c105SRobert Watson #define V_tcp_hc_callout VNET(tcp_hc_callout) 16197d8d152SAndre Oppermann 16297d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *); 16397d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_insert(struct in_conninfo *); 16497d8d152SAndre Oppermann static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS); 16502f26e98SRichard Scheffenegger static int sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS); 16626456380SHiren Panchasara static int sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS); 167fffb9f1dSBjoern A. Zeeb static void tcp_hc_purge_internal(int); 16897d8d152SAndre Oppermann static void tcp_hc_purge(void *); 16997d8d152SAndre Oppermann 1707029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, 1717029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1720eb7cf4dSBrooks Davis "TCP Host cache"); 17397d8d152SAndre Oppermann 1748a56c645SHiren Panchasara VNET_DEFINE(int, tcp_use_hostcache) = 1; 1758a56c645SHiren Panchasara #define V_tcp_use_hostcache VNET(tcp_use_hostcache) 1768a56c645SHiren Panchasara SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 1778a56c645SHiren Panchasara &VNET_NAME(tcp_use_hostcache), 0, 1788a56c645SHiren Panchasara "Enable the TCP hostcache"); 1798a56c645SHiren Panchasara 1806df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN, 181eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.cache_limit), 0, 1828b615593SMarko Zec "Overall entry limit for hostcache"); 18397d8d152SAndre Oppermann 1846df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN, 185eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.hashsize), 0, 1868b615593SMarko Zec "Size of TCP hostcache hashtable"); 18797d8d152SAndre Oppermann 1886df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, 1896df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0, 1908b615593SMarko Zec "Per-bucket hash limit for hostcache"); 19197d8d152SAndre Oppermann 192529a2a0fSRichard Scheffenegger SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD, 193529a2a0fSRichard Scheffenegger &VNET_NAME(tcp_hostcache.cache_count), 0, 1948b615593SMarko Zec "Current number of entries in hostcache"); 19597d8d152SAndre Oppermann 1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW, 197eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.expire), 0, 1988b615593SMarko Zec "Expire time of TCP hostcache entries"); 19997d8d152SAndre Oppermann 2006df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW, 201eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.prune), 0, 202eddfbb76SRobert Watson "Time between purge runs"); 203dba3c508SYaroslav Tykhiy 2046df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW, 205eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.purgeall), 0, 2068b615593SMarko Zec "Expire all entires on next purge run"); 20797d8d152SAndre Oppermann 20897d8d152SAndre Oppermann SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list, 2097029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, 2107029da5cSPawel Biernacki 0, 0, sysctl_tcp_hc_list, "A", 2117029da5cSPawel Biernacki "List of all hostcache entries"); 21297d8d152SAndre Oppermann 21302f26e98SRichard Scheffenegger SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, histo, 21402f26e98SRichard Scheffenegger CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, 21502f26e98SRichard Scheffenegger 0, 0, sysctl_tcp_hc_histo, "A", 21602f26e98SRichard Scheffenegger "Print a histogram of hostcache hashbucket utilization"); 21702f26e98SRichard Scheffenegger 21826456380SHiren Panchasara SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, purgenow, 2197029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 2207029da5cSPawel Biernacki NULL, 0, sysctl_tcp_hc_purgenow, "I", 2217029da5cSPawel Biernacki "Immediately purge all entries"); 22297d8d152SAndre Oppermann 22397d8d152SAndre Oppermann static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache"); 22497d8d152SAndre Oppermann 22597d8d152SAndre Oppermann #define HOSTCACHE_HASH(ip) \ 22697d8d152SAndre Oppermann (((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) & \ 227603724d3SBjoern A. Zeeb V_tcp_hostcache.hashmask) 22897d8d152SAndre Oppermann 22997d8d152SAndre Oppermann /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */ 23097d8d152SAndre Oppermann #define HOSTCACHE_HASH6(ip6) \ 23197d8d152SAndre Oppermann (((ip6)->s6_addr32[0] ^ \ 23297d8d152SAndre Oppermann (ip6)->s6_addr32[1] ^ \ 23397d8d152SAndre Oppermann (ip6)->s6_addr32[2] ^ \ 23497d8d152SAndre Oppermann (ip6)->s6_addr32[3]) & \ 235603724d3SBjoern A. Zeeb V_tcp_hostcache.hashmask) 23697d8d152SAndre Oppermann 23797d8d152SAndre Oppermann #define THC_LOCK(lp) mtx_lock(lp) 23897d8d152SAndre Oppermann #define THC_UNLOCK(lp) mtx_unlock(lp) 23997d8d152SAndre Oppermann 24097d8d152SAndre Oppermann void 24197d8d152SAndre Oppermann tcp_hc_init(void) 24297d8d152SAndre Oppermann { 24332fe38f1SAndrey Zonov u_int cache_limit; 24497d8d152SAndre Oppermann int i; 24597d8d152SAndre Oppermann 24697d8d152SAndre Oppermann /* 247e487a5e2SRobert Watson * Initialize hostcache structures. 24897d8d152SAndre Oppermann */ 249529a2a0fSRichard Scheffenegger atomic_store_int(&V_tcp_hostcache.cache_count, 0); 250603724d3SBjoern A. Zeeb V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; 251603724d3SBjoern A. Zeeb V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT; 252603724d3SBjoern A. Zeeb V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE; 253603724d3SBjoern A. Zeeb V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE; 25497d8d152SAndre Oppermann 25597d8d152SAndre Oppermann TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize", 256603724d3SBjoern A. Zeeb &V_tcp_hostcache.hashsize); 257603724d3SBjoern A. Zeeb if (!powerof2(V_tcp_hostcache.hashsize)) { 25897d8d152SAndre Oppermann printf("WARNING: hostcache hash size is not a power of 2.\n"); 259603724d3SBjoern A. Zeeb V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */ 26097d8d152SAndre Oppermann } 261603724d3SBjoern A. Zeeb V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1; 26297d8d152SAndre Oppermann 26332fe38f1SAndrey Zonov TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit", 26432fe38f1SAndrey Zonov &V_tcp_hostcache.bucket_limit); 26532fe38f1SAndrey Zonov 26632fe38f1SAndrey Zonov cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit; 26732fe38f1SAndrey Zonov V_tcp_hostcache.cache_limit = cache_limit; 26832fe38f1SAndrey Zonov TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit", 26932fe38f1SAndrey Zonov &V_tcp_hostcache.cache_limit); 27032fe38f1SAndrey Zonov if (V_tcp_hostcache.cache_limit > cache_limit) 27132fe38f1SAndrey Zonov V_tcp_hostcache.cache_limit = cache_limit; 27232fe38f1SAndrey Zonov 27397d8d152SAndre Oppermann /* 274e487a5e2SRobert Watson * Allocate the hash table. 27597d8d152SAndre Oppermann */ 276603724d3SBjoern A. Zeeb V_tcp_hostcache.hashbase = (struct hc_head *) 277603724d3SBjoern A. Zeeb malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head), 27897d8d152SAndre Oppermann M_HOSTCACHE, M_WAITOK | M_ZERO); 27997d8d152SAndre Oppermann 28097d8d152SAndre Oppermann /* 281e487a5e2SRobert Watson * Initialize the hash buckets. 28297d8d152SAndre Oppermann */ 283603724d3SBjoern A. Zeeb for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 284603724d3SBjoern A. Zeeb TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket); 285603724d3SBjoern A. Zeeb V_tcp_hostcache.hashbase[i].hch_length = 0; 286603724d3SBjoern A. Zeeb mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry", 28797d8d152SAndre Oppermann NULL, MTX_DEF); 28897d8d152SAndre Oppermann } 28997d8d152SAndre Oppermann 29097d8d152SAndre Oppermann /* 29197d8d152SAndre Oppermann * Allocate the hostcache entries. 29297d8d152SAndre Oppermann */ 293ac957cd2SJulian Elischer V_tcp_hostcache.zone = 294ac957cd2SJulian Elischer uma_zcreate("hostcache", sizeof(struct hc_metrics), 2954efb805cSAndre Oppermann NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 296603724d3SBjoern A. Zeeb uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit); 29797d8d152SAndre Oppermann 29897d8d152SAndre Oppermann /* 29997d8d152SAndre Oppermann * Set up periodic cache cleanup. 30097d8d152SAndre Oppermann */ 301fd90e2edSJung-uk Kim callout_init(&V_tcp_hc_callout, 1); 302ac957cd2SJulian Elischer callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 30321ca7b57SMarko Zec tcp_hc_purge, curvnet); 30497d8d152SAndre Oppermann } 30597d8d152SAndre Oppermann 306bc29160dSMarko Zec #ifdef VIMAGE 307bc29160dSMarko Zec void 308bc29160dSMarko Zec tcp_hc_destroy(void) 309bc29160dSMarko Zec { 310fffb9f1dSBjoern A. Zeeb int i; 311bc29160dSMarko Zec 312bc29160dSMarko Zec callout_drain(&V_tcp_hc_callout); 313fffb9f1dSBjoern A. Zeeb 314fffb9f1dSBjoern A. Zeeb /* Purge all hc entries. */ 315fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(1); 316fffb9f1dSBjoern A. Zeeb 317fffb9f1dSBjoern A. Zeeb /* Free the uma zone and the allocated hash table. */ 318fffb9f1dSBjoern A. Zeeb uma_zdestroy(V_tcp_hostcache.zone); 319fffb9f1dSBjoern A. Zeeb 320fffb9f1dSBjoern A. Zeeb for (i = 0; i < V_tcp_hostcache.hashsize; i++) 321fffb9f1dSBjoern A. Zeeb mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx); 322fffb9f1dSBjoern A. Zeeb free(V_tcp_hostcache.hashbase, M_HOSTCACHE); 323bc29160dSMarko Zec } 324bc29160dSMarko Zec #endif 325bc29160dSMarko Zec 32697d8d152SAndre Oppermann /* 32797d8d152SAndre Oppermann * Internal function: look up an entry in the hostcache or return NULL. 32897d8d152SAndre Oppermann * 32997d8d152SAndre Oppermann * If an entry has been returned, the caller becomes responsible for 33097d8d152SAndre Oppermann * unlocking the bucket row after he is done reading/modifying the entry. 33197d8d152SAndre Oppermann */ 33297d8d152SAndre Oppermann static struct hc_metrics * 33397d8d152SAndre Oppermann tcp_hc_lookup(struct in_conninfo *inc) 33497d8d152SAndre Oppermann { 33597d8d152SAndre Oppermann int hash; 33697d8d152SAndre Oppermann struct hc_head *hc_head; 33797d8d152SAndre Oppermann struct hc_metrics *hc_entry; 33897d8d152SAndre Oppermann 3398a56c645SHiren Panchasara if (!V_tcp_use_hostcache) 3408a56c645SHiren Panchasara return NULL; 3418a56c645SHiren Panchasara 34297d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer")); 34397d8d152SAndre Oppermann 34497d8d152SAndre Oppermann /* 34597d8d152SAndre Oppermann * Hash the foreign ip address. 34697d8d152SAndre Oppermann */ 347dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) 34897d8d152SAndre Oppermann hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 34997d8d152SAndre Oppermann else 35097d8d152SAndre Oppermann hash = HOSTCACHE_HASH(&inc->inc_faddr); 35197d8d152SAndre Oppermann 352603724d3SBjoern A. Zeeb hc_head = &V_tcp_hostcache.hashbase[hash]; 35397d8d152SAndre Oppermann 35497d8d152SAndre Oppermann /* 355e487a5e2SRobert Watson * Acquire lock for this bucket row; we release the lock if we don't 356e487a5e2SRobert Watson * find an entry, otherwise the caller has to unlock after he is 357e487a5e2SRobert Watson * done. 35897d8d152SAndre Oppermann */ 35997d8d152SAndre Oppermann THC_LOCK(&hc_head->hch_mtx); 36097d8d152SAndre Oppermann 36197d8d152SAndre Oppermann /* 362e487a5e2SRobert Watson * Iterate through entries in bucket row looking for a match. 36397d8d152SAndre Oppermann */ 36497d8d152SAndre Oppermann TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) { 365dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 366028bdf28SAndrey V. Elsukov /* XXX: check ip6_zoneid */ 36797d8d152SAndre Oppermann if (memcmp(&inc->inc6_faddr, &hc_entry->ip6, 36897d8d152SAndre Oppermann sizeof(inc->inc6_faddr)) == 0) 36997d8d152SAndre Oppermann return hc_entry; 37097d8d152SAndre Oppermann } else { 37197d8d152SAndre Oppermann if (memcmp(&inc->inc_faddr, &hc_entry->ip4, 37297d8d152SAndre Oppermann sizeof(inc->inc_faddr)) == 0) 37397d8d152SAndre Oppermann return hc_entry; 37497d8d152SAndre Oppermann } 37597d8d152SAndre Oppermann } 37697d8d152SAndre Oppermann 37797d8d152SAndre Oppermann /* 378e487a5e2SRobert Watson * We were unsuccessful and didn't find anything. 37997d8d152SAndre Oppermann */ 38097d8d152SAndre Oppermann THC_UNLOCK(&hc_head->hch_mtx); 38197d8d152SAndre Oppermann return NULL; 38297d8d152SAndre Oppermann } 38397d8d152SAndre Oppermann 38497d8d152SAndre Oppermann /* 385e487a5e2SRobert Watson * Internal function: insert an entry into the hostcache or return NULL if 386e487a5e2SRobert Watson * unable to allocate a new one. 38797d8d152SAndre Oppermann * 38897d8d152SAndre Oppermann * If an entry has been returned, the caller becomes responsible for 38997d8d152SAndre Oppermann * unlocking the bucket row after he is done reading/modifying the entry. 39097d8d152SAndre Oppermann */ 39197d8d152SAndre Oppermann static struct hc_metrics * 39297d8d152SAndre Oppermann tcp_hc_insert(struct in_conninfo *inc) 39397d8d152SAndre Oppermann { 39497d8d152SAndre Oppermann int hash; 39597d8d152SAndre Oppermann struct hc_head *hc_head; 39697d8d152SAndre Oppermann struct hc_metrics *hc_entry; 39797d8d152SAndre Oppermann 3988a56c645SHiren Panchasara if (!V_tcp_use_hostcache) 3998a56c645SHiren Panchasara return NULL; 4008a56c645SHiren Panchasara 40197d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer")); 40297d8d152SAndre Oppermann 40397d8d152SAndre Oppermann /* 404e487a5e2SRobert Watson * Hash the foreign ip address. 40597d8d152SAndre Oppermann */ 406dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) 40797d8d152SAndre Oppermann hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 40897d8d152SAndre Oppermann else 40997d8d152SAndre Oppermann hash = HOSTCACHE_HASH(&inc->inc_faddr); 41097d8d152SAndre Oppermann 411603724d3SBjoern A. Zeeb hc_head = &V_tcp_hostcache.hashbase[hash]; 41297d8d152SAndre Oppermann 41397d8d152SAndre Oppermann /* 414e487a5e2SRobert Watson * Acquire lock for this bucket row; we release the lock if we don't 415e487a5e2SRobert Watson * find an entry, otherwise the caller has to unlock after he is 416e487a5e2SRobert Watson * done. 41797d8d152SAndre Oppermann */ 41897d8d152SAndre Oppermann THC_LOCK(&hc_head->hch_mtx); 41997d8d152SAndre Oppermann 42097d8d152SAndre Oppermann /* 421e487a5e2SRobert Watson * If the bucket limit is reached, reuse the least-used element. 42297d8d152SAndre Oppermann */ 423603724d3SBjoern A. Zeeb if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit || 424529a2a0fSRichard Scheffenegger atomic_load_int(&V_tcp_hostcache.cache_count) >= V_tcp_hostcache.cache_limit) { 42597d8d152SAndre Oppermann hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead); 42697d8d152SAndre Oppermann /* 42797d8d152SAndre Oppermann * At first we were dropping the last element, just to 428e487a5e2SRobert Watson * reacquire it in the next two lines again, which isn't very 429e487a5e2SRobert Watson * efficient. Instead just reuse the least used element. 430e487a5e2SRobert Watson * We may drop something that is still "in-use" but we can be 431e487a5e2SRobert Watson * "lossy". 43245024be0SAndre Oppermann * Just give up if this bucket row is empty and we don't have 43345024be0SAndre Oppermann * anything to replace. 43497d8d152SAndre Oppermann */ 43545024be0SAndre Oppermann if (hc_entry == NULL) { 43645024be0SAndre Oppermann THC_UNLOCK(&hc_head->hch_mtx); 43745024be0SAndre Oppermann return NULL; 43845024be0SAndre Oppermann } 43997d8d152SAndre Oppermann TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q); 44002f26e98SRichard Scheffenegger KASSERT(V_tcp_hostcache.hashbase[hash].hch_length > 0 && 44102f26e98SRichard Scheffenegger V_tcp_hostcache.hashbase[hash].hch_length <= 44202f26e98SRichard Scheffenegger V_tcp_hostcache.bucket_limit, 44302f26e98SRichard Scheffenegger ("tcp_hostcache: bucket length range violated at %u: %u", 44402f26e98SRichard Scheffenegger hash, V_tcp_hostcache.hashbase[hash].hch_length)); 445603724d3SBjoern A. Zeeb V_tcp_hostcache.hashbase[hash].hch_length--; 446529a2a0fSRichard Scheffenegger atomic_subtract_int(&V_tcp_hostcache.cache_count, 1); 44778b50714SRobert Watson TCPSTAT_INC(tcps_hc_bucketoverflow); 448cd6c4060SAndre Oppermann #if 0 449603724d3SBjoern A. Zeeb uma_zfree(V_tcp_hostcache.zone, hc_entry); 450cd6c4060SAndre Oppermann #endif 45197d8d152SAndre Oppermann } else { 45297d8d152SAndre Oppermann /* 453e487a5e2SRobert Watson * Allocate a new entry, or balk if not possible. 45497d8d152SAndre Oppermann */ 455603724d3SBjoern A. Zeeb hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT); 45697d8d152SAndre Oppermann if (hc_entry == NULL) { 45797d8d152SAndre Oppermann THC_UNLOCK(&hc_head->hch_mtx); 45897d8d152SAndre Oppermann return NULL; 45997d8d152SAndre Oppermann } 46097d8d152SAndre Oppermann } 46197d8d152SAndre Oppermann 46297d8d152SAndre Oppermann /* 463e487a5e2SRobert Watson * Initialize basic information of hostcache entry. 46497d8d152SAndre Oppermann */ 46597d8d152SAndre Oppermann bzero(hc_entry, sizeof(*hc_entry)); 466028bdf28SAndrey V. Elsukov if (inc->inc_flags & INC_ISIPV6) { 467028bdf28SAndrey V. Elsukov hc_entry->ip6 = inc->inc6_faddr; 468028bdf28SAndrey V. Elsukov hc_entry->ip6_zoneid = inc->inc6_zoneid; 469028bdf28SAndrey V. Elsukov } else 47097d8d152SAndre Oppermann hc_entry->ip4 = inc->inc_faddr; 47197d8d152SAndre Oppermann hc_entry->rmx_head = hc_head; 472603724d3SBjoern A. Zeeb hc_entry->rmx_expire = V_tcp_hostcache.expire; 47397d8d152SAndre Oppermann 47497d8d152SAndre Oppermann /* 475e487a5e2SRobert Watson * Put it upfront. 47697d8d152SAndre Oppermann */ 47797d8d152SAndre Oppermann TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q); 478603724d3SBjoern A. Zeeb V_tcp_hostcache.hashbase[hash].hch_length++; 47902f26e98SRichard Scheffenegger KASSERT(V_tcp_hostcache.hashbase[hash].hch_length < 48002f26e98SRichard Scheffenegger V_tcp_hostcache.bucket_limit, 48102f26e98SRichard Scheffenegger ("tcp_hostcache: bucket length too high at %u: %u", 48202f26e98SRichard Scheffenegger hash, V_tcp_hostcache.hashbase[hash].hch_length)); 483529a2a0fSRichard Scheffenegger atomic_add_int(&V_tcp_hostcache.cache_count, 1); 48478b50714SRobert Watson TCPSTAT_INC(tcps_hc_added); 48597d8d152SAndre Oppermann 48697d8d152SAndre Oppermann return hc_entry; 48797d8d152SAndre Oppermann } 48897d8d152SAndre Oppermann 48997d8d152SAndre Oppermann /* 49097d8d152SAndre Oppermann * External function: look up an entry in the hostcache and fill out the 491e487a5e2SRobert Watson * supplied TCP metrics structure. Fills in NULL when no entry was found or 492e487a5e2SRobert Watson * a value is not set. 49397d8d152SAndre Oppermann */ 49497d8d152SAndre Oppermann void 49597d8d152SAndre Oppermann tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite) 49697d8d152SAndre Oppermann { 49797d8d152SAndre Oppermann struct hc_metrics *hc_entry; 49897d8d152SAndre Oppermann 4996b8fba3cSMichael Tuexen if (!V_tcp_use_hostcache) { 5006b8fba3cSMichael Tuexen bzero(hc_metrics_lite, sizeof(*hc_metrics_lite)); 5018a56c645SHiren Panchasara return; 5026b8fba3cSMichael Tuexen } 5038a56c645SHiren Panchasara 50497d8d152SAndre Oppermann /* 505e487a5e2SRobert Watson * Find the right bucket. 50697d8d152SAndre Oppermann */ 50797d8d152SAndre Oppermann hc_entry = tcp_hc_lookup(inc); 50897d8d152SAndre Oppermann 50997d8d152SAndre Oppermann /* 510e487a5e2SRobert Watson * If we don't have an existing object. 51197d8d152SAndre Oppermann */ 51297d8d152SAndre Oppermann if (hc_entry == NULL) { 51397d8d152SAndre Oppermann bzero(hc_metrics_lite, sizeof(*hc_metrics_lite)); 51497d8d152SAndre Oppermann return; 51597d8d152SAndre Oppermann } 51697d8d152SAndre Oppermann hc_entry->rmx_hits++; 517603724d3SBjoern A. Zeeb hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 51897d8d152SAndre Oppermann 51997d8d152SAndre Oppermann hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu; 52097d8d152SAndre Oppermann hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh; 52197d8d152SAndre Oppermann hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt; 52297d8d152SAndre Oppermann hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar; 52397d8d152SAndre Oppermann hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd; 52497d8d152SAndre Oppermann hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe; 52597d8d152SAndre Oppermann hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe; 52697d8d152SAndre Oppermann 52797d8d152SAndre Oppermann /* 528e487a5e2SRobert Watson * Unlock bucket row. 52997d8d152SAndre Oppermann */ 53097d8d152SAndre Oppermann THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 53197d8d152SAndre Oppermann } 53297d8d152SAndre Oppermann 53397d8d152SAndre Oppermann /* 53497d8d152SAndre Oppermann * External function: look up an entry in the hostcache and return the 5358a56c645SHiren Panchasara * discovered path MTU. Returns 0 if no entry is found or value is not 5366fbed4afSRobert Watson * set. 53797d8d152SAndre Oppermann */ 5383ac12506SJonathan T. Looney uint32_t 53997d8d152SAndre Oppermann tcp_hc_getmtu(struct in_conninfo *inc) 54097d8d152SAndre Oppermann { 54197d8d152SAndre Oppermann struct hc_metrics *hc_entry; 5423ac12506SJonathan T. Looney uint32_t mtu; 54397d8d152SAndre Oppermann 5448a56c645SHiren Panchasara if (!V_tcp_use_hostcache) 5458a56c645SHiren Panchasara return 0; 5468a56c645SHiren Panchasara 54797d8d152SAndre Oppermann hc_entry = tcp_hc_lookup(inc); 54897d8d152SAndre Oppermann if (hc_entry == NULL) { 54997d8d152SAndre Oppermann return 0; 55097d8d152SAndre Oppermann } 55197d8d152SAndre Oppermann hc_entry->rmx_hits++; 552603724d3SBjoern A. Zeeb hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 55397d8d152SAndre Oppermann 55497d8d152SAndre Oppermann mtu = hc_entry->rmx_mtu; 55597d8d152SAndre Oppermann THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 55697d8d152SAndre Oppermann return mtu; 55797d8d152SAndre Oppermann } 55897d8d152SAndre Oppermann 55997d8d152SAndre Oppermann /* 560e487a5e2SRobert Watson * External function: update the MTU value of an entry in the hostcache. 56197d8d152SAndre Oppermann * Creates a new entry if none was found. 56297d8d152SAndre Oppermann */ 56397d8d152SAndre Oppermann void 5643ac12506SJonathan T. Looney tcp_hc_updatemtu(struct in_conninfo *inc, uint32_t mtu) 56597d8d152SAndre Oppermann { 56697d8d152SAndre Oppermann struct hc_metrics *hc_entry; 56797d8d152SAndre Oppermann 5688a56c645SHiren Panchasara if (!V_tcp_use_hostcache) 5698a56c645SHiren Panchasara return; 5708a56c645SHiren Panchasara 57197d8d152SAndre Oppermann /* 572e487a5e2SRobert Watson * Find the right bucket. 57397d8d152SAndre Oppermann */ 57497d8d152SAndre Oppermann hc_entry = tcp_hc_lookup(inc); 57597d8d152SAndre Oppermann 57697d8d152SAndre Oppermann /* 577e487a5e2SRobert Watson * If we don't have an existing object, try to insert a new one. 57897d8d152SAndre Oppermann */ 57997d8d152SAndre Oppermann if (hc_entry == NULL) { 58097d8d152SAndre Oppermann hc_entry = tcp_hc_insert(inc); 58197d8d152SAndre Oppermann if (hc_entry == NULL) 58297d8d152SAndre Oppermann return; 58397d8d152SAndre Oppermann } 58497d8d152SAndre Oppermann hc_entry->rmx_updates++; 585603724d3SBjoern A. Zeeb hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 58697d8d152SAndre Oppermann 58797d8d152SAndre Oppermann hc_entry->rmx_mtu = mtu; 58897d8d152SAndre Oppermann 58997d8d152SAndre Oppermann /* 590e487a5e2SRobert Watson * Put it upfront so we find it faster next time. 59197d8d152SAndre Oppermann */ 59297d8d152SAndre Oppermann TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 59397d8d152SAndre Oppermann TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 59497d8d152SAndre Oppermann 59597d8d152SAndre Oppermann /* 596e487a5e2SRobert Watson * Unlock bucket row. 59797d8d152SAndre Oppermann */ 59897d8d152SAndre Oppermann THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 59997d8d152SAndre Oppermann } 60097d8d152SAndre Oppermann 60197d8d152SAndre Oppermann /* 602e487a5e2SRobert Watson * External function: update the TCP metrics of an entry in the hostcache. 60397d8d152SAndre Oppermann * Creates a new entry if none was found. 60497d8d152SAndre Oppermann */ 60597d8d152SAndre Oppermann void 60697d8d152SAndre Oppermann tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml) 60797d8d152SAndre Oppermann { 60897d8d152SAndre Oppermann struct hc_metrics *hc_entry; 60997d8d152SAndre Oppermann 6108a56c645SHiren Panchasara if (!V_tcp_use_hostcache) 6118a56c645SHiren Panchasara return; 6128a56c645SHiren Panchasara 61397d8d152SAndre Oppermann hc_entry = tcp_hc_lookup(inc); 61497d8d152SAndre Oppermann if (hc_entry == NULL) { 61597d8d152SAndre Oppermann hc_entry = tcp_hc_insert(inc); 61697d8d152SAndre Oppermann if (hc_entry == NULL) 61797d8d152SAndre Oppermann return; 61897d8d152SAndre Oppermann } 61997d8d152SAndre Oppermann hc_entry->rmx_updates++; 620603724d3SBjoern A. Zeeb hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 62197d8d152SAndre Oppermann 62297d8d152SAndre Oppermann if (hcml->rmx_rtt != 0) { 62397d8d152SAndre Oppermann if (hc_entry->rmx_rtt == 0) 62497d8d152SAndre Oppermann hc_entry->rmx_rtt = hcml->rmx_rtt; 62597d8d152SAndre Oppermann else 6263ac12506SJonathan T. Looney hc_entry->rmx_rtt = ((uint64_t)hc_entry->rmx_rtt + 6273ac12506SJonathan T. Looney (uint64_t)hcml->rmx_rtt) / 2; 62878b50714SRobert Watson TCPSTAT_INC(tcps_cachedrtt); 62997d8d152SAndre Oppermann } 63097d8d152SAndre Oppermann if (hcml->rmx_rttvar != 0) { 63197d8d152SAndre Oppermann if (hc_entry->rmx_rttvar == 0) 63297d8d152SAndre Oppermann hc_entry->rmx_rttvar = hcml->rmx_rttvar; 63397d8d152SAndre Oppermann else 6343ac12506SJonathan T. Looney hc_entry->rmx_rttvar = ((uint64_t)hc_entry->rmx_rttvar + 6353ac12506SJonathan T. Looney (uint64_t)hcml->rmx_rttvar) / 2; 63678b50714SRobert Watson TCPSTAT_INC(tcps_cachedrttvar); 63797d8d152SAndre Oppermann } 63897d8d152SAndre Oppermann if (hcml->rmx_ssthresh != 0) { 63997d8d152SAndre Oppermann if (hc_entry->rmx_ssthresh == 0) 64097d8d152SAndre Oppermann hc_entry->rmx_ssthresh = hcml->rmx_ssthresh; 64197d8d152SAndre Oppermann else 64297d8d152SAndre Oppermann hc_entry->rmx_ssthresh = 64397d8d152SAndre Oppermann (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2; 64478b50714SRobert Watson TCPSTAT_INC(tcps_cachedssthresh); 64597d8d152SAndre Oppermann } 64697d8d152SAndre Oppermann if (hcml->rmx_cwnd != 0) { 64797d8d152SAndre Oppermann if (hc_entry->rmx_cwnd == 0) 64897d8d152SAndre Oppermann hc_entry->rmx_cwnd = hcml->rmx_cwnd; 64997d8d152SAndre Oppermann else 6503ac12506SJonathan T. Looney hc_entry->rmx_cwnd = ((uint64_t)hc_entry->rmx_cwnd + 6513ac12506SJonathan T. Looney (uint64_t)hcml->rmx_cwnd) / 2; 65278b50714SRobert Watson /* TCPSTAT_INC(tcps_cachedcwnd); */ 65397d8d152SAndre Oppermann } 65497d8d152SAndre Oppermann if (hcml->rmx_sendpipe != 0) { 65597d8d152SAndre Oppermann if (hc_entry->rmx_sendpipe == 0) 65697d8d152SAndre Oppermann hc_entry->rmx_sendpipe = hcml->rmx_sendpipe; 65797d8d152SAndre Oppermann else 65897d8d152SAndre Oppermann hc_entry->rmx_sendpipe = 6593ac12506SJonathan T. Looney ((uint64_t)hc_entry->rmx_sendpipe + 6603ac12506SJonathan T. Looney (uint64_t)hcml->rmx_sendpipe) /2; 66178b50714SRobert Watson /* TCPSTAT_INC(tcps_cachedsendpipe); */ 66297d8d152SAndre Oppermann } 66397d8d152SAndre Oppermann if (hcml->rmx_recvpipe != 0) { 66497d8d152SAndre Oppermann if (hc_entry->rmx_recvpipe == 0) 66597d8d152SAndre Oppermann hc_entry->rmx_recvpipe = hcml->rmx_recvpipe; 66697d8d152SAndre Oppermann else 66797d8d152SAndre Oppermann hc_entry->rmx_recvpipe = 6683ac12506SJonathan T. Looney ((uint64_t)hc_entry->rmx_recvpipe + 6693ac12506SJonathan T. Looney (uint64_t)hcml->rmx_recvpipe) /2; 67078b50714SRobert Watson /* TCPSTAT_INC(tcps_cachedrecvpipe); */ 67197d8d152SAndre Oppermann } 67297d8d152SAndre Oppermann 67397d8d152SAndre Oppermann TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 67497d8d152SAndre Oppermann TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 67597d8d152SAndre Oppermann THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 67697d8d152SAndre Oppermann } 67797d8d152SAndre Oppermann 67897d8d152SAndre Oppermann /* 67997d8d152SAndre Oppermann * Sysctl function: prints the list and values of all hostcache entries in 68097d8d152SAndre Oppermann * unsorted order. 68197d8d152SAndre Oppermann */ 68297d8d152SAndre Oppermann static int 68397d8d152SAndre Oppermann sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS) 68497d8d152SAndre Oppermann { 685751ccc42SIan Lepore const int linesize = 128; 686002d4558SJohn Baldwin struct sbuf sb; 687cb0dd7e1SRichard Scheffenegger int i, error, len; 68897d8d152SAndre Oppermann struct hc_metrics *hc_entry; 6898144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 6901d54aa3bSBjoern A. Zeeb #ifdef INET6 6911d54aa3bSBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 6921d54aa3bSBjoern A. Zeeb #endif 69397d8d152SAndre Oppermann 694b8a2fb91SHiren Panchasara if (jailed_without_vnet(curthread->td_ucred) != 0) 695b8a2fb91SHiren Panchasara return (EPERM); 696b8a2fb91SHiren Panchasara 697cb0dd7e1SRichard Scheffenegger /* Optimize Buffer length query by sbin/sysctl */ 698cb0dd7e1SRichard Scheffenegger if (req->oldptr == NULL) { 699529a2a0fSRichard Scheffenegger len = (atomic_load_int(&V_tcp_hostcache.cache_count) + 1) * 700529a2a0fSRichard Scheffenegger linesize; 701cb0dd7e1SRichard Scheffenegger return (SYSCTL_OUT(req, NULL, len)); 702cb0dd7e1SRichard Scheffenegger } 703cb0dd7e1SRichard Scheffenegger 704cb0dd7e1SRichard Scheffenegger error = sysctl_wire_old_buffer(req, 0); 705cb0dd7e1SRichard Scheffenegger if (error != 0) { 706cb0dd7e1SRichard Scheffenegger return(error); 707cb0dd7e1SRichard Scheffenegger } 708cb0dd7e1SRichard Scheffenegger 70986988046SRichard Scheffenegger /* Use a buffer sized for one full bucket */ 710529a2a0fSRichard Scheffenegger sbuf_new_for_sysctl(&sb, NULL, V_tcp_hostcache.bucket_limit * 711529a2a0fSRichard Scheffenegger linesize, req); 71297d8d152SAndre Oppermann 713002d4558SJohn Baldwin sbuf_printf(&sb, 7144d163382SHiren Panchasara "\nIP address MTU SSTRESH RTT RTTVAR " 71597d8d152SAndre Oppermann " CWND SENDPIPE RECVPIPE HITS UPD EXP\n"); 71686988046SRichard Scheffenegger sbuf_drain(&sb); 71797d8d152SAndre Oppermann 71897d8d152SAndre Oppermann #define msec(u) (((u) + 500) / 1000) 719603724d3SBjoern A. Zeeb for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 720603724d3SBjoern A. Zeeb THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 721603724d3SBjoern A. Zeeb TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket, 72297d8d152SAndre Oppermann rmx_q) { 723002d4558SJohn Baldwin sbuf_printf(&sb, 7243ac12506SJonathan T. Looney "%-15s %5u %8u %6lums %6lums %8u %8u %8u %4lu " 7254d163382SHiren Panchasara "%4lu %4i\n", 7268144690aSEric van Gyzen hc_entry->ip4.s_addr ? 7278144690aSEric van Gyzen inet_ntoa_r(hc_entry->ip4, ip4buf) : 72897d8d152SAndre Oppermann #ifdef INET6 7291d54aa3bSBjoern A. Zeeb ip6_sprintf(ip6buf, &hc_entry->ip6), 73097d8d152SAndre Oppermann #else 73197d8d152SAndre Oppermann "IPv6?", 73297d8d152SAndre Oppermann #endif 73397d8d152SAndre Oppermann hc_entry->rmx_mtu, 73497d8d152SAndre Oppermann hc_entry->rmx_ssthresh, 7353ac12506SJonathan T. Looney msec((u_long)hc_entry->rmx_rtt * 73697d8d152SAndre Oppermann (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))), 7373ac12506SJonathan T. Looney msec((u_long)hc_entry->rmx_rttvar * 7383a288e90SMikolaj Golub (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))), 73997d8d152SAndre Oppermann hc_entry->rmx_cwnd, 74097d8d152SAndre Oppermann hc_entry->rmx_sendpipe, 74197d8d152SAndre Oppermann hc_entry->rmx_recvpipe, 74297d8d152SAndre Oppermann hc_entry->rmx_hits, 74397d8d152SAndre Oppermann hc_entry->rmx_updates, 74497d8d152SAndre Oppermann hc_entry->rmx_expire); 74597d8d152SAndre Oppermann } 746603724d3SBjoern A. Zeeb THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 74786988046SRichard Scheffenegger sbuf_drain(&sb); 7489aef4e7cSRichard Scheffenegger } 74997d8d152SAndre Oppermann #undef msec 750751ccc42SIan Lepore error = sbuf_finish(&sb); 751002d4558SJohn Baldwin sbuf_delete(&sb); 75297d8d152SAndre Oppermann return(error); 75397d8d152SAndre Oppermann } 75497d8d152SAndre Oppermann 75597d8d152SAndre Oppermann /* 75602f26e98SRichard Scheffenegger * Sysctl function: prints a histogram of the hostcache hashbucket 75702f26e98SRichard Scheffenegger * utilization. 75802f26e98SRichard Scheffenegger */ 75902f26e98SRichard Scheffenegger static int 76002f26e98SRichard Scheffenegger sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS) 76102f26e98SRichard Scheffenegger { 76202f26e98SRichard Scheffenegger const int linesize = 50; 76302f26e98SRichard Scheffenegger struct sbuf sb; 76402f26e98SRichard Scheffenegger int i, error; 76502f26e98SRichard Scheffenegger int *histo; 76602f26e98SRichard Scheffenegger u_int hch_length; 76702f26e98SRichard Scheffenegger 76802f26e98SRichard Scheffenegger if (jailed_without_vnet(curthread->td_ucred) != 0) 76902f26e98SRichard Scheffenegger return (EPERM); 77002f26e98SRichard Scheffenegger 77102f26e98SRichard Scheffenegger histo = (int *)malloc(sizeof(int) * (V_tcp_hostcache.bucket_limit + 1), 77202f26e98SRichard Scheffenegger M_TEMP, M_NOWAIT|M_ZERO); 77302f26e98SRichard Scheffenegger if (histo == NULL) 77402f26e98SRichard Scheffenegger return(ENOMEM); 77502f26e98SRichard Scheffenegger 77602f26e98SRichard Scheffenegger for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 77702f26e98SRichard Scheffenegger hch_length = V_tcp_hostcache.hashbase[i].hch_length; 77802f26e98SRichard Scheffenegger KASSERT(hch_length <= V_tcp_hostcache.bucket_limit, 77902f26e98SRichard Scheffenegger ("tcp_hostcache: bucket limit exceeded at %u: %u", 78002f26e98SRichard Scheffenegger i, hch_length)); 78102f26e98SRichard Scheffenegger histo[hch_length]++; 78202f26e98SRichard Scheffenegger } 78302f26e98SRichard Scheffenegger 78402f26e98SRichard Scheffenegger /* Use a buffer for 16 lines */ 78502f26e98SRichard Scheffenegger sbuf_new_for_sysctl(&sb, NULL, 16 * linesize, req); 78602f26e98SRichard Scheffenegger 78702f26e98SRichard Scheffenegger sbuf_printf(&sb, "\nLength\tCount\n"); 78802f26e98SRichard Scheffenegger for (i = 0; i <= V_tcp_hostcache.bucket_limit; i++) { 78902f26e98SRichard Scheffenegger sbuf_printf(&sb, "%u\t%u\n", i, histo[i]); 79002f26e98SRichard Scheffenegger } 79102f26e98SRichard Scheffenegger error = sbuf_finish(&sb); 79202f26e98SRichard Scheffenegger sbuf_delete(&sb); 79302f26e98SRichard Scheffenegger free(histo, M_TEMP); 79402f26e98SRichard Scheffenegger return(error); 79502f26e98SRichard Scheffenegger } 79602f26e98SRichard Scheffenegger 79702f26e98SRichard Scheffenegger /* 798fffb9f1dSBjoern A. Zeeb * Caller has to make sure the curvnet is set properly. 79997d8d152SAndre Oppermann */ 80097d8d152SAndre Oppermann static void 801fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(int all) 80297d8d152SAndre Oppermann { 803b62dccc7SAndre Oppermann struct hc_metrics *hc_entry, *hc_next; 80497d8d152SAndre Oppermann int i; 80597d8d152SAndre Oppermann 806603724d3SBjoern A. Zeeb for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 807603724d3SBjoern A. Zeeb THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 808ac957cd2SJulian Elischer TAILQ_FOREACH_SAFE(hc_entry, 8095ed3800eSJulian Elischer &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) { 81002f26e98SRichard Scheffenegger KASSERT(V_tcp_hostcache.hashbase[i].hch_length > 0 && 81102f26e98SRichard Scheffenegger V_tcp_hostcache.hashbase[i].hch_length <= 81202f26e98SRichard Scheffenegger V_tcp_hostcache.bucket_limit, 8139aef4e7cSRichard Scheffenegger ("tcp_hostcache: bucket length out of range at %u: %u", 81402f26e98SRichard Scheffenegger i, V_tcp_hostcache.hashbase[i].hch_length)); 81597d8d152SAndre Oppermann if (all || hc_entry->rmx_expire <= 0) { 816603724d3SBjoern A. Zeeb TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket, 81797d8d152SAndre Oppermann hc_entry, rmx_q); 818603724d3SBjoern A. Zeeb uma_zfree(V_tcp_hostcache.zone, hc_entry); 819603724d3SBjoern A. Zeeb V_tcp_hostcache.hashbase[i].hch_length--; 820529a2a0fSRichard Scheffenegger atomic_subtract_int(&V_tcp_hostcache.cache_count, 1); 82197d8d152SAndre Oppermann } else 822603724d3SBjoern A. Zeeb hc_entry->rmx_expire -= V_tcp_hostcache.prune; 82397d8d152SAndre Oppermann } 824603724d3SBjoern A. Zeeb THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 82597d8d152SAndre Oppermann } 826fffb9f1dSBjoern A. Zeeb } 827fffb9f1dSBjoern A. Zeeb 828fffb9f1dSBjoern A. Zeeb /* 829fffb9f1dSBjoern A. Zeeb * Expire and purge (old|all) entries in the tcp_hostcache. Runs 830fffb9f1dSBjoern A. Zeeb * periodically from the callout. 831fffb9f1dSBjoern A. Zeeb */ 832fffb9f1dSBjoern A. Zeeb static void 833fffb9f1dSBjoern A. Zeeb tcp_hc_purge(void *arg) 834fffb9f1dSBjoern A. Zeeb { 835fffb9f1dSBjoern A. Zeeb CURVNET_SET((struct vnet *) arg); 836fffb9f1dSBjoern A. Zeeb int all = 0; 837fffb9f1dSBjoern A. Zeeb 838fffb9f1dSBjoern A. Zeeb if (V_tcp_hostcache.purgeall) { 839fffb9f1dSBjoern A. Zeeb all = 1; 840fffb9f1dSBjoern A. Zeeb V_tcp_hostcache.purgeall = 0; 841fffb9f1dSBjoern A. Zeeb } 842fffb9f1dSBjoern A. Zeeb 843fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(all); 844ac957cd2SJulian Elischer 845ac957cd2SJulian Elischer callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 846ac957cd2SJulian Elischer tcp_hc_purge, arg); 84721ca7b57SMarko Zec CURVNET_RESTORE(); 84897d8d152SAndre Oppermann } 84926456380SHiren Panchasara 85026456380SHiren Panchasara /* 85126456380SHiren Panchasara * Expire and purge all entries in hostcache immediately. 85226456380SHiren Panchasara */ 85326456380SHiren Panchasara static int 85426456380SHiren Panchasara sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS) 85526456380SHiren Panchasara { 85626456380SHiren Panchasara int error, val; 85726456380SHiren Panchasara 85826456380SHiren Panchasara val = 0; 85926456380SHiren Panchasara error = sysctl_handle_int(oidp, &val, 0, req); 86026456380SHiren Panchasara if (error || !req->newptr) 86126456380SHiren Panchasara return (error); 86226456380SHiren Panchasara 86326456380SHiren Panchasara tcp_hc_purge_internal(1); 86426456380SHiren Panchasara 86526456380SHiren Panchasara callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 86626456380SHiren Panchasara tcp_hc_purge, curvnet); 86726456380SHiren Panchasara 86826456380SHiren Panchasara return (0); 86926456380SHiren Panchasara } 870