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 1082cca4c0eSGleb Smirnoff TAILQ_HEAD(hc_qhead, hc_metrics); 1092cca4c0eSGleb Smirnoff 1102cca4c0eSGleb Smirnoff struct hc_head { 1112cca4c0eSGleb Smirnoff struct hc_qhead hch_bucket; 1122cca4c0eSGleb Smirnoff u_int hch_length; 1132cca4c0eSGleb Smirnoff struct mtx hch_mtx; 1142cca4c0eSGleb Smirnoff }; 1152cca4c0eSGleb Smirnoff 1162cca4c0eSGleb Smirnoff struct hc_metrics { 1172cca4c0eSGleb Smirnoff /* housekeeping */ 1182cca4c0eSGleb Smirnoff TAILQ_ENTRY(hc_metrics) rmx_q; 1192cca4c0eSGleb Smirnoff struct hc_head *rmx_head; /* head of bucket tail queue */ 1202cca4c0eSGleb Smirnoff struct in_addr ip4; /* IP address */ 1212cca4c0eSGleb Smirnoff struct in6_addr ip6; /* IP6 address */ 1222cca4c0eSGleb Smirnoff uint32_t ip6_zoneid; /* IPv6 scope zone id */ 1232cca4c0eSGleb Smirnoff /* endpoint specific values for tcp */ 1242cca4c0eSGleb Smirnoff uint32_t rmx_mtu; /* MTU for this path */ 1252cca4c0eSGleb Smirnoff uint32_t rmx_ssthresh; /* outbound gateway buffer limit */ 1262cca4c0eSGleb Smirnoff uint32_t rmx_rtt; /* estimated round trip time */ 1272cca4c0eSGleb Smirnoff uint32_t rmx_rttvar; /* estimated rtt variance */ 1282cca4c0eSGleb Smirnoff uint32_t rmx_cwnd; /* congestion window */ 1292cca4c0eSGleb Smirnoff uint32_t rmx_sendpipe; /* outbound delay-bandwidth product */ 1302cca4c0eSGleb Smirnoff uint32_t rmx_recvpipe; /* inbound delay-bandwidth product */ 1312cca4c0eSGleb Smirnoff /* TCP hostcache internal data */ 1322cca4c0eSGleb Smirnoff int rmx_expire; /* lifetime for object */ 133*489bde57SGleb Smirnoff #ifdef TCP_HC_COUNTERS 1342cca4c0eSGleb Smirnoff u_long rmx_hits; /* number of hits */ 1352cca4c0eSGleb Smirnoff u_long rmx_updates; /* number of updates */ 136*489bde57SGleb Smirnoff #endif 1372cca4c0eSGleb Smirnoff }; 1382cca4c0eSGleb Smirnoff 1392cca4c0eSGleb Smirnoff struct tcp_hostcache { 1402cca4c0eSGleb Smirnoff struct hc_head *hashbase; 1412cca4c0eSGleb Smirnoff uma_zone_t zone; 1422cca4c0eSGleb Smirnoff u_int hashsize; 1432cca4c0eSGleb Smirnoff u_int hashmask; 1442cca4c0eSGleb Smirnoff u_int bucket_limit; 1452cca4c0eSGleb Smirnoff u_int cache_count; 1462cca4c0eSGleb Smirnoff u_int cache_limit; 1472cca4c0eSGleb Smirnoff int expire; 1482cca4c0eSGleb Smirnoff int prune; 1492cca4c0eSGleb Smirnoff int purgeall; 1502cca4c0eSGleb Smirnoff }; 1512cca4c0eSGleb Smirnoff 15297d8d152SAndre Oppermann /* Arbitrary values */ 15397d8d152SAndre Oppermann #define TCP_HOSTCACHE_HASHSIZE 512 15497d8d152SAndre Oppermann #define TCP_HOSTCACHE_BUCKETLIMIT 30 15597d8d152SAndre Oppermann #define TCP_HOSTCACHE_EXPIRE 60*60 /* one hour */ 15697d8d152SAndre Oppermann #define TCP_HOSTCACHE_PRUNE 5*60 /* every 5 minutes */ 15797d8d152SAndre Oppermann 1585f901c92SAndrew Turner VNET_DEFINE_STATIC(struct tcp_hostcache, tcp_hostcache); 1591e77c105SRobert Watson #define V_tcp_hostcache VNET(tcp_hostcache) 16082cea7e6SBjoern A. Zeeb 1615f901c92SAndrew Turner VNET_DEFINE_STATIC(struct callout, tcp_hc_callout); 1621e77c105SRobert Watson #define V_tcp_hc_callout VNET(tcp_hc_callout) 16397d8d152SAndre Oppermann 16497d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *); 16597d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_insert(struct in_conninfo *); 16697d8d152SAndre Oppermann static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS); 16702f26e98SRichard Scheffenegger static int sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS); 16826456380SHiren Panchasara static int sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS); 169fffb9f1dSBjoern A. Zeeb static void tcp_hc_purge_internal(int); 17097d8d152SAndre Oppermann static void tcp_hc_purge(void *); 17197d8d152SAndre Oppermann 1727029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, 1737029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1740eb7cf4dSBrooks Davis "TCP Host cache"); 17597d8d152SAndre Oppermann 1768a56c645SHiren Panchasara VNET_DEFINE(int, tcp_use_hostcache) = 1; 1778a56c645SHiren Panchasara #define V_tcp_use_hostcache VNET(tcp_use_hostcache) 1788a56c645SHiren Panchasara SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 1798a56c645SHiren Panchasara &VNET_NAME(tcp_use_hostcache), 0, 1808a56c645SHiren Panchasara "Enable the TCP hostcache"); 1818a56c645SHiren Panchasara 1826df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN, 183eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.cache_limit), 0, 1848b615593SMarko Zec "Overall entry limit for hostcache"); 18597d8d152SAndre Oppermann 1866df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN, 187eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.hashsize), 0, 1888b615593SMarko Zec "Size of TCP hostcache hashtable"); 18997d8d152SAndre Oppermann 1906df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, 1916df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0, 1928b615593SMarko Zec "Per-bucket hash limit for hostcache"); 19397d8d152SAndre Oppermann 194529a2a0fSRichard Scheffenegger SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD, 195529a2a0fSRichard Scheffenegger &VNET_NAME(tcp_hostcache.cache_count), 0, 1968b615593SMarko Zec "Current number of entries in hostcache"); 19797d8d152SAndre Oppermann 1986df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW, 199eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.expire), 0, 2008b615593SMarko Zec "Expire time of TCP hostcache entries"); 20197d8d152SAndre Oppermann 2026df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW, 203eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.prune), 0, 204eddfbb76SRobert Watson "Time between purge runs"); 205dba3c508SYaroslav Tykhiy 2066df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW, 207eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.purgeall), 0, 2088b615593SMarko Zec "Expire all entires on next purge run"); 20997d8d152SAndre Oppermann 21097d8d152SAndre Oppermann SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list, 2117029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, 2127029da5cSPawel Biernacki 0, 0, sysctl_tcp_hc_list, "A", 2137029da5cSPawel Biernacki "List of all hostcache entries"); 21497d8d152SAndre Oppermann 21502f26e98SRichard Scheffenegger SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, histo, 21602f26e98SRichard Scheffenegger CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, 21702f26e98SRichard Scheffenegger 0, 0, sysctl_tcp_hc_histo, "A", 21802f26e98SRichard Scheffenegger "Print a histogram of hostcache hashbucket utilization"); 21902f26e98SRichard Scheffenegger 22026456380SHiren Panchasara SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, purgenow, 2217029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 2227029da5cSPawel Biernacki NULL, 0, sysctl_tcp_hc_purgenow, "I", 2237029da5cSPawel Biernacki "Immediately purge all entries"); 22497d8d152SAndre Oppermann 22597d8d152SAndre Oppermann static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache"); 22697d8d152SAndre Oppermann 22797d8d152SAndre Oppermann #define HOSTCACHE_HASH(ip) \ 22897d8d152SAndre Oppermann (((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) & \ 229603724d3SBjoern A. Zeeb V_tcp_hostcache.hashmask) 23097d8d152SAndre Oppermann 23197d8d152SAndre Oppermann /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */ 23297d8d152SAndre Oppermann #define HOSTCACHE_HASH6(ip6) \ 23397d8d152SAndre Oppermann (((ip6)->s6_addr32[0] ^ \ 23497d8d152SAndre Oppermann (ip6)->s6_addr32[1] ^ \ 23597d8d152SAndre Oppermann (ip6)->s6_addr32[2] ^ \ 23697d8d152SAndre Oppermann (ip6)->s6_addr32[3]) & \ 237603724d3SBjoern A. Zeeb V_tcp_hostcache.hashmask) 23897d8d152SAndre Oppermann 23997d8d152SAndre Oppermann #define THC_LOCK(lp) mtx_lock(lp) 24097d8d152SAndre Oppermann #define THC_UNLOCK(lp) mtx_unlock(lp) 24197d8d152SAndre Oppermann 24297d8d152SAndre Oppermann void 24397d8d152SAndre Oppermann tcp_hc_init(void) 24497d8d152SAndre Oppermann { 24532fe38f1SAndrey Zonov u_int cache_limit; 24697d8d152SAndre Oppermann int i; 24797d8d152SAndre Oppermann 24897d8d152SAndre Oppermann /* 249e487a5e2SRobert Watson * Initialize hostcache structures. 25097d8d152SAndre Oppermann */ 251529a2a0fSRichard Scheffenegger atomic_store_int(&V_tcp_hostcache.cache_count, 0); 252603724d3SBjoern A. Zeeb V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; 253603724d3SBjoern A. Zeeb V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT; 254603724d3SBjoern A. Zeeb V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE; 255603724d3SBjoern A. Zeeb V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE; 25697d8d152SAndre Oppermann 25797d8d152SAndre Oppermann TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize", 258603724d3SBjoern A. Zeeb &V_tcp_hostcache.hashsize); 259603724d3SBjoern A. Zeeb if (!powerof2(V_tcp_hostcache.hashsize)) { 26097d8d152SAndre Oppermann printf("WARNING: hostcache hash size is not a power of 2.\n"); 261603724d3SBjoern A. Zeeb V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */ 26297d8d152SAndre Oppermann } 263603724d3SBjoern A. Zeeb V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1; 26497d8d152SAndre Oppermann 26532fe38f1SAndrey Zonov TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit", 26632fe38f1SAndrey Zonov &V_tcp_hostcache.bucket_limit); 26732fe38f1SAndrey Zonov 26832fe38f1SAndrey Zonov cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit; 26932fe38f1SAndrey Zonov V_tcp_hostcache.cache_limit = cache_limit; 27032fe38f1SAndrey Zonov TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit", 27132fe38f1SAndrey Zonov &V_tcp_hostcache.cache_limit); 27232fe38f1SAndrey Zonov if (V_tcp_hostcache.cache_limit > cache_limit) 27332fe38f1SAndrey Zonov V_tcp_hostcache.cache_limit = cache_limit; 27432fe38f1SAndrey Zonov 27597d8d152SAndre Oppermann /* 276e487a5e2SRobert Watson * Allocate the hash table. 27797d8d152SAndre Oppermann */ 278603724d3SBjoern A. Zeeb V_tcp_hostcache.hashbase = (struct hc_head *) 279603724d3SBjoern A. Zeeb malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head), 28097d8d152SAndre Oppermann M_HOSTCACHE, M_WAITOK | M_ZERO); 28197d8d152SAndre Oppermann 28297d8d152SAndre Oppermann /* 283e487a5e2SRobert Watson * Initialize the hash buckets. 28497d8d152SAndre Oppermann */ 285603724d3SBjoern A. Zeeb for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 286603724d3SBjoern A. Zeeb TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket); 287603724d3SBjoern A. Zeeb V_tcp_hostcache.hashbase[i].hch_length = 0; 288603724d3SBjoern A. Zeeb mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry", 28997d8d152SAndre Oppermann NULL, MTX_DEF); 29097d8d152SAndre Oppermann } 29197d8d152SAndre Oppermann 29297d8d152SAndre Oppermann /* 29397d8d152SAndre Oppermann * Allocate the hostcache entries. 29497d8d152SAndre Oppermann */ 295ac957cd2SJulian Elischer V_tcp_hostcache.zone = 296ac957cd2SJulian Elischer uma_zcreate("hostcache", sizeof(struct hc_metrics), 2974efb805cSAndre Oppermann NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 298603724d3SBjoern A. Zeeb uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit); 29997d8d152SAndre Oppermann 30097d8d152SAndre Oppermann /* 30197d8d152SAndre Oppermann * Set up periodic cache cleanup. 30297d8d152SAndre Oppermann */ 303fd90e2edSJung-uk Kim callout_init(&V_tcp_hc_callout, 1); 304ac957cd2SJulian Elischer callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 30521ca7b57SMarko Zec tcp_hc_purge, curvnet); 30697d8d152SAndre Oppermann } 30797d8d152SAndre Oppermann 308bc29160dSMarko Zec #ifdef VIMAGE 309bc29160dSMarko Zec void 310bc29160dSMarko Zec tcp_hc_destroy(void) 311bc29160dSMarko Zec { 312fffb9f1dSBjoern A. Zeeb int i; 313bc29160dSMarko Zec 314bc29160dSMarko Zec callout_drain(&V_tcp_hc_callout); 315fffb9f1dSBjoern A. Zeeb 316fffb9f1dSBjoern A. Zeeb /* Purge all hc entries. */ 317fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(1); 318fffb9f1dSBjoern A. Zeeb 319fffb9f1dSBjoern A. Zeeb /* Free the uma zone and the allocated hash table. */ 320fffb9f1dSBjoern A. Zeeb uma_zdestroy(V_tcp_hostcache.zone); 321fffb9f1dSBjoern A. Zeeb 322fffb9f1dSBjoern A. Zeeb for (i = 0; i < V_tcp_hostcache.hashsize; i++) 323fffb9f1dSBjoern A. Zeeb mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx); 324fffb9f1dSBjoern A. Zeeb free(V_tcp_hostcache.hashbase, M_HOSTCACHE); 325bc29160dSMarko Zec } 326bc29160dSMarko Zec #endif 327bc29160dSMarko Zec 32897d8d152SAndre Oppermann /* 32997d8d152SAndre Oppermann * Internal function: look up an entry in the hostcache or return NULL. 33097d8d152SAndre Oppermann * 33197d8d152SAndre Oppermann * If an entry has been returned, the caller becomes responsible for 33297d8d152SAndre Oppermann * unlocking the bucket row after he is done reading/modifying the entry. 33397d8d152SAndre Oppermann */ 33497d8d152SAndre Oppermann static struct hc_metrics * 33597d8d152SAndre Oppermann tcp_hc_lookup(struct in_conninfo *inc) 33697d8d152SAndre Oppermann { 33797d8d152SAndre Oppermann int hash; 33897d8d152SAndre Oppermann struct hc_head *hc_head; 33997d8d152SAndre Oppermann struct hc_metrics *hc_entry; 34097d8d152SAndre Oppermann 3418a56c645SHiren Panchasara if (!V_tcp_use_hostcache) 3428a56c645SHiren Panchasara return NULL; 3438a56c645SHiren Panchasara 34497d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer")); 34597d8d152SAndre Oppermann 34697d8d152SAndre Oppermann /* 34797d8d152SAndre Oppermann * Hash the foreign ip address. 34897d8d152SAndre Oppermann */ 349dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) 35097d8d152SAndre Oppermann hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 35197d8d152SAndre Oppermann else 35297d8d152SAndre Oppermann hash = HOSTCACHE_HASH(&inc->inc_faddr); 35397d8d152SAndre Oppermann 354603724d3SBjoern A. Zeeb hc_head = &V_tcp_hostcache.hashbase[hash]; 35597d8d152SAndre Oppermann 35697d8d152SAndre Oppermann /* 357e487a5e2SRobert Watson * Acquire lock for this bucket row; we release the lock if we don't 358e487a5e2SRobert Watson * find an entry, otherwise the caller has to unlock after he is 359e487a5e2SRobert Watson * done. 36097d8d152SAndre Oppermann */ 36197d8d152SAndre Oppermann THC_LOCK(&hc_head->hch_mtx); 36297d8d152SAndre Oppermann 36397d8d152SAndre Oppermann /* 364e487a5e2SRobert Watson * Iterate through entries in bucket row looking for a match. 36597d8d152SAndre Oppermann */ 36697d8d152SAndre Oppermann TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) { 367dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 368028bdf28SAndrey V. Elsukov /* XXX: check ip6_zoneid */ 36997d8d152SAndre Oppermann if (memcmp(&inc->inc6_faddr, &hc_entry->ip6, 37097d8d152SAndre Oppermann sizeof(inc->inc6_faddr)) == 0) 37197d8d152SAndre Oppermann return hc_entry; 37297d8d152SAndre Oppermann } else { 37397d8d152SAndre Oppermann if (memcmp(&inc->inc_faddr, &hc_entry->ip4, 37497d8d152SAndre Oppermann sizeof(inc->inc_faddr)) == 0) 37597d8d152SAndre Oppermann return hc_entry; 37697d8d152SAndre Oppermann } 37797d8d152SAndre Oppermann } 37897d8d152SAndre Oppermann 37997d8d152SAndre Oppermann /* 380e487a5e2SRobert Watson * We were unsuccessful and didn't find anything. 38197d8d152SAndre Oppermann */ 38297d8d152SAndre Oppermann THC_UNLOCK(&hc_head->hch_mtx); 38397d8d152SAndre Oppermann return NULL; 38497d8d152SAndre Oppermann } 38597d8d152SAndre Oppermann 38697d8d152SAndre Oppermann /* 387e487a5e2SRobert Watson * Internal function: insert an entry into the hostcache or return NULL if 388e487a5e2SRobert Watson * unable to allocate a new one. 38997d8d152SAndre Oppermann * 39097d8d152SAndre Oppermann * If an entry has been returned, the caller becomes responsible for 39197d8d152SAndre Oppermann * unlocking the bucket row after he is done reading/modifying the entry. 39297d8d152SAndre Oppermann */ 39397d8d152SAndre Oppermann static struct hc_metrics * 39497d8d152SAndre Oppermann tcp_hc_insert(struct in_conninfo *inc) 39597d8d152SAndre Oppermann { 39697d8d152SAndre Oppermann int hash; 39797d8d152SAndre Oppermann struct hc_head *hc_head; 39897d8d152SAndre Oppermann struct hc_metrics *hc_entry; 39997d8d152SAndre Oppermann 4008a56c645SHiren Panchasara if (!V_tcp_use_hostcache) 4018a56c645SHiren Panchasara return NULL; 4028a56c645SHiren Panchasara 40397d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer")); 40497d8d152SAndre Oppermann 40597d8d152SAndre Oppermann /* 406e487a5e2SRobert Watson * Hash the foreign ip address. 40797d8d152SAndre Oppermann */ 408dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) 40997d8d152SAndre Oppermann hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 41097d8d152SAndre Oppermann else 41197d8d152SAndre Oppermann hash = HOSTCACHE_HASH(&inc->inc_faddr); 41297d8d152SAndre Oppermann 413603724d3SBjoern A. Zeeb hc_head = &V_tcp_hostcache.hashbase[hash]; 41497d8d152SAndre Oppermann 41597d8d152SAndre Oppermann /* 416e487a5e2SRobert Watson * Acquire lock for this bucket row; we release the lock if we don't 417e487a5e2SRobert Watson * find an entry, otherwise the caller has to unlock after he is 418e487a5e2SRobert Watson * done. 41997d8d152SAndre Oppermann */ 42097d8d152SAndre Oppermann THC_LOCK(&hc_head->hch_mtx); 42197d8d152SAndre Oppermann 42297d8d152SAndre Oppermann /* 423e487a5e2SRobert Watson * If the bucket limit is reached, reuse the least-used element. 42497d8d152SAndre Oppermann */ 425603724d3SBjoern A. Zeeb if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit || 426529a2a0fSRichard Scheffenegger atomic_load_int(&V_tcp_hostcache.cache_count) >= V_tcp_hostcache.cache_limit) { 42797d8d152SAndre Oppermann hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead); 42897d8d152SAndre Oppermann /* 42997d8d152SAndre Oppermann * At first we were dropping the last element, just to 430e487a5e2SRobert Watson * reacquire it in the next two lines again, which isn't very 431e487a5e2SRobert Watson * efficient. Instead just reuse the least used element. 432e487a5e2SRobert Watson * We may drop something that is still "in-use" but we can be 433e487a5e2SRobert Watson * "lossy". 43445024be0SAndre Oppermann * Just give up if this bucket row is empty and we don't have 43545024be0SAndre Oppermann * anything to replace. 43697d8d152SAndre Oppermann */ 43745024be0SAndre Oppermann if (hc_entry == NULL) { 43845024be0SAndre Oppermann THC_UNLOCK(&hc_head->hch_mtx); 43945024be0SAndre Oppermann return NULL; 44045024be0SAndre Oppermann } 44197d8d152SAndre Oppermann TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q); 44202f26e98SRichard Scheffenegger KASSERT(V_tcp_hostcache.hashbase[hash].hch_length > 0 && 44302f26e98SRichard Scheffenegger V_tcp_hostcache.hashbase[hash].hch_length <= 44402f26e98SRichard Scheffenegger V_tcp_hostcache.bucket_limit, 44502f26e98SRichard Scheffenegger ("tcp_hostcache: bucket length range violated at %u: %u", 44602f26e98SRichard Scheffenegger hash, V_tcp_hostcache.hashbase[hash].hch_length)); 447603724d3SBjoern A. Zeeb V_tcp_hostcache.hashbase[hash].hch_length--; 448529a2a0fSRichard Scheffenegger atomic_subtract_int(&V_tcp_hostcache.cache_count, 1); 44978b50714SRobert Watson TCPSTAT_INC(tcps_hc_bucketoverflow); 450cd6c4060SAndre Oppermann #if 0 451603724d3SBjoern A. Zeeb uma_zfree(V_tcp_hostcache.zone, hc_entry); 452cd6c4060SAndre Oppermann #endif 45397d8d152SAndre Oppermann } else { 45497d8d152SAndre Oppermann /* 455e487a5e2SRobert Watson * Allocate a new entry, or balk if not possible. 45697d8d152SAndre Oppermann */ 457603724d3SBjoern A. Zeeb hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT); 45897d8d152SAndre Oppermann if (hc_entry == NULL) { 45997d8d152SAndre Oppermann THC_UNLOCK(&hc_head->hch_mtx); 46097d8d152SAndre Oppermann return NULL; 46197d8d152SAndre Oppermann } 46297d8d152SAndre Oppermann } 46397d8d152SAndre Oppermann 46497d8d152SAndre Oppermann /* 465e487a5e2SRobert Watson * Initialize basic information of hostcache entry. 46697d8d152SAndre Oppermann */ 46797d8d152SAndre Oppermann bzero(hc_entry, sizeof(*hc_entry)); 468028bdf28SAndrey V. Elsukov if (inc->inc_flags & INC_ISIPV6) { 469028bdf28SAndrey V. Elsukov hc_entry->ip6 = inc->inc6_faddr; 470028bdf28SAndrey V. Elsukov hc_entry->ip6_zoneid = inc->inc6_zoneid; 471028bdf28SAndrey V. Elsukov } else 47297d8d152SAndre Oppermann hc_entry->ip4 = inc->inc_faddr; 47397d8d152SAndre Oppermann hc_entry->rmx_head = hc_head; 474603724d3SBjoern A. Zeeb hc_entry->rmx_expire = V_tcp_hostcache.expire; 47597d8d152SAndre Oppermann 47697d8d152SAndre Oppermann /* 477e487a5e2SRobert Watson * Put it upfront. 47897d8d152SAndre Oppermann */ 47997d8d152SAndre Oppermann TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q); 480603724d3SBjoern A. Zeeb V_tcp_hostcache.hashbase[hash].hch_length++; 48102f26e98SRichard Scheffenegger KASSERT(V_tcp_hostcache.hashbase[hash].hch_length < 48202f26e98SRichard Scheffenegger V_tcp_hostcache.bucket_limit, 48302f26e98SRichard Scheffenegger ("tcp_hostcache: bucket length too high at %u: %u", 48402f26e98SRichard Scheffenegger hash, V_tcp_hostcache.hashbase[hash].hch_length)); 485529a2a0fSRichard Scheffenegger atomic_add_int(&V_tcp_hostcache.cache_count, 1); 48678b50714SRobert Watson TCPSTAT_INC(tcps_hc_added); 48797d8d152SAndre Oppermann 48897d8d152SAndre Oppermann return hc_entry; 48997d8d152SAndre Oppermann } 49097d8d152SAndre Oppermann 49197d8d152SAndre Oppermann /* 49297d8d152SAndre Oppermann * External function: look up an entry in the hostcache and fill out the 493e487a5e2SRobert Watson * supplied TCP metrics structure. Fills in NULL when no entry was found or 494e487a5e2SRobert Watson * a value is not set. 49597d8d152SAndre Oppermann */ 49697d8d152SAndre Oppermann void 49797d8d152SAndre Oppermann tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite) 49897d8d152SAndre Oppermann { 49997d8d152SAndre Oppermann struct hc_metrics *hc_entry; 50097d8d152SAndre Oppermann 5016b8fba3cSMichael Tuexen if (!V_tcp_use_hostcache) { 5026b8fba3cSMichael Tuexen bzero(hc_metrics_lite, sizeof(*hc_metrics_lite)); 5038a56c645SHiren Panchasara return; 5046b8fba3cSMichael Tuexen } 5058a56c645SHiren Panchasara 50697d8d152SAndre Oppermann /* 507e487a5e2SRobert Watson * Find the right bucket. 50897d8d152SAndre Oppermann */ 50997d8d152SAndre Oppermann hc_entry = tcp_hc_lookup(inc); 51097d8d152SAndre Oppermann 51197d8d152SAndre Oppermann /* 512e487a5e2SRobert Watson * If we don't have an existing object. 51397d8d152SAndre Oppermann */ 51497d8d152SAndre Oppermann if (hc_entry == NULL) { 51597d8d152SAndre Oppermann bzero(hc_metrics_lite, sizeof(*hc_metrics_lite)); 51697d8d152SAndre Oppermann return; 51797d8d152SAndre Oppermann } 518*489bde57SGleb Smirnoff #ifdef TCP_HC_COUNTERS 51997d8d152SAndre Oppermann hc_entry->rmx_hits++; 520*489bde57SGleb Smirnoff #endif 521603724d3SBjoern A. Zeeb hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 52297d8d152SAndre Oppermann 52397d8d152SAndre Oppermann hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu; 52497d8d152SAndre Oppermann hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh; 52597d8d152SAndre Oppermann hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt; 52697d8d152SAndre Oppermann hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar; 52797d8d152SAndre Oppermann hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd; 52897d8d152SAndre Oppermann hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe; 52997d8d152SAndre Oppermann hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe; 53097d8d152SAndre Oppermann 53197d8d152SAndre Oppermann /* 532e487a5e2SRobert Watson * Unlock bucket row. 53397d8d152SAndre Oppermann */ 53497d8d152SAndre Oppermann THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 53597d8d152SAndre Oppermann } 53697d8d152SAndre Oppermann 53797d8d152SAndre Oppermann /* 53897d8d152SAndre Oppermann * External function: look up an entry in the hostcache and return the 5398a56c645SHiren Panchasara * discovered path MTU. Returns 0 if no entry is found or value is not 5406fbed4afSRobert Watson * set. 54197d8d152SAndre Oppermann */ 5423ac12506SJonathan T. Looney uint32_t 54397d8d152SAndre Oppermann tcp_hc_getmtu(struct in_conninfo *inc) 54497d8d152SAndre Oppermann { 54597d8d152SAndre Oppermann struct hc_metrics *hc_entry; 5463ac12506SJonathan T. Looney uint32_t mtu; 54797d8d152SAndre Oppermann 5488a56c645SHiren Panchasara if (!V_tcp_use_hostcache) 5498a56c645SHiren Panchasara return 0; 5508a56c645SHiren Panchasara 55197d8d152SAndre Oppermann hc_entry = tcp_hc_lookup(inc); 55297d8d152SAndre Oppermann if (hc_entry == NULL) { 55397d8d152SAndre Oppermann return 0; 55497d8d152SAndre Oppermann } 555*489bde57SGleb Smirnoff #ifdef TCP_HC_COUNTERS 55697d8d152SAndre Oppermann hc_entry->rmx_hits++; 557*489bde57SGleb Smirnoff #endif 558603724d3SBjoern A. Zeeb hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 55997d8d152SAndre Oppermann 56097d8d152SAndre Oppermann mtu = hc_entry->rmx_mtu; 56197d8d152SAndre Oppermann THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 56297d8d152SAndre Oppermann return mtu; 56397d8d152SAndre Oppermann } 56497d8d152SAndre Oppermann 56597d8d152SAndre Oppermann /* 566e487a5e2SRobert Watson * External function: update the MTU value of an entry in the hostcache. 56797d8d152SAndre Oppermann * Creates a new entry if none was found. 56897d8d152SAndre Oppermann */ 56997d8d152SAndre Oppermann void 5703ac12506SJonathan T. Looney tcp_hc_updatemtu(struct in_conninfo *inc, uint32_t mtu) 57197d8d152SAndre Oppermann { 57297d8d152SAndre Oppermann struct hc_metrics *hc_entry; 57397d8d152SAndre Oppermann 5748a56c645SHiren Panchasara if (!V_tcp_use_hostcache) 5758a56c645SHiren Panchasara return; 5768a56c645SHiren Panchasara 57797d8d152SAndre Oppermann /* 578e487a5e2SRobert Watson * Find the right bucket. 57997d8d152SAndre Oppermann */ 58097d8d152SAndre Oppermann hc_entry = tcp_hc_lookup(inc); 58197d8d152SAndre Oppermann 58297d8d152SAndre Oppermann /* 583e487a5e2SRobert Watson * If we don't have an existing object, try to insert a new one. 58497d8d152SAndre Oppermann */ 58597d8d152SAndre Oppermann if (hc_entry == NULL) { 58697d8d152SAndre Oppermann hc_entry = tcp_hc_insert(inc); 58797d8d152SAndre Oppermann if (hc_entry == NULL) 58897d8d152SAndre Oppermann return; 58997d8d152SAndre Oppermann } 590*489bde57SGleb Smirnoff #ifdef TCP_HC_COUNTERS 59197d8d152SAndre Oppermann hc_entry->rmx_updates++; 592*489bde57SGleb Smirnoff #endif 593603724d3SBjoern A. Zeeb hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 59497d8d152SAndre Oppermann 59597d8d152SAndre Oppermann hc_entry->rmx_mtu = mtu; 59697d8d152SAndre Oppermann 59797d8d152SAndre Oppermann /* 598e487a5e2SRobert Watson * Put it upfront so we find it faster next time. 59997d8d152SAndre Oppermann */ 60097d8d152SAndre Oppermann TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 60197d8d152SAndre Oppermann TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 60297d8d152SAndre Oppermann 60397d8d152SAndre Oppermann /* 604e487a5e2SRobert Watson * Unlock bucket row. 60597d8d152SAndre Oppermann */ 60697d8d152SAndre Oppermann THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 60797d8d152SAndre Oppermann } 60897d8d152SAndre Oppermann 60997d8d152SAndre Oppermann /* 610e487a5e2SRobert Watson * External function: update the TCP metrics of an entry in the hostcache. 61197d8d152SAndre Oppermann * Creates a new entry if none was found. 61297d8d152SAndre Oppermann */ 61397d8d152SAndre Oppermann void 61497d8d152SAndre Oppermann tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml) 61597d8d152SAndre Oppermann { 61697d8d152SAndre Oppermann struct hc_metrics *hc_entry; 61797d8d152SAndre Oppermann 6188a56c645SHiren Panchasara if (!V_tcp_use_hostcache) 6198a56c645SHiren Panchasara return; 6208a56c645SHiren Panchasara 62197d8d152SAndre Oppermann hc_entry = tcp_hc_lookup(inc); 62297d8d152SAndre Oppermann if (hc_entry == NULL) { 62397d8d152SAndre Oppermann hc_entry = tcp_hc_insert(inc); 62497d8d152SAndre Oppermann if (hc_entry == NULL) 62597d8d152SAndre Oppermann return; 62697d8d152SAndre Oppermann } 627*489bde57SGleb Smirnoff #ifdef TCP_HC_COUNTERS 62897d8d152SAndre Oppermann hc_entry->rmx_updates++; 629*489bde57SGleb Smirnoff #endif 630603724d3SBjoern A. Zeeb hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 63197d8d152SAndre Oppermann 63297d8d152SAndre Oppermann if (hcml->rmx_rtt != 0) { 63397d8d152SAndre Oppermann if (hc_entry->rmx_rtt == 0) 63497d8d152SAndre Oppermann hc_entry->rmx_rtt = hcml->rmx_rtt; 63597d8d152SAndre Oppermann else 6363ac12506SJonathan T. Looney hc_entry->rmx_rtt = ((uint64_t)hc_entry->rmx_rtt + 6373ac12506SJonathan T. Looney (uint64_t)hcml->rmx_rtt) / 2; 63878b50714SRobert Watson TCPSTAT_INC(tcps_cachedrtt); 63997d8d152SAndre Oppermann } 64097d8d152SAndre Oppermann if (hcml->rmx_rttvar != 0) { 64197d8d152SAndre Oppermann if (hc_entry->rmx_rttvar == 0) 64297d8d152SAndre Oppermann hc_entry->rmx_rttvar = hcml->rmx_rttvar; 64397d8d152SAndre Oppermann else 6443ac12506SJonathan T. Looney hc_entry->rmx_rttvar = ((uint64_t)hc_entry->rmx_rttvar + 6453ac12506SJonathan T. Looney (uint64_t)hcml->rmx_rttvar) / 2; 64678b50714SRobert Watson TCPSTAT_INC(tcps_cachedrttvar); 64797d8d152SAndre Oppermann } 64897d8d152SAndre Oppermann if (hcml->rmx_ssthresh != 0) { 64997d8d152SAndre Oppermann if (hc_entry->rmx_ssthresh == 0) 65097d8d152SAndre Oppermann hc_entry->rmx_ssthresh = hcml->rmx_ssthresh; 65197d8d152SAndre Oppermann else 65297d8d152SAndre Oppermann hc_entry->rmx_ssthresh = 65397d8d152SAndre Oppermann (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2; 65478b50714SRobert Watson TCPSTAT_INC(tcps_cachedssthresh); 65597d8d152SAndre Oppermann } 65697d8d152SAndre Oppermann if (hcml->rmx_cwnd != 0) { 65797d8d152SAndre Oppermann if (hc_entry->rmx_cwnd == 0) 65897d8d152SAndre Oppermann hc_entry->rmx_cwnd = hcml->rmx_cwnd; 65997d8d152SAndre Oppermann else 6603ac12506SJonathan T. Looney hc_entry->rmx_cwnd = ((uint64_t)hc_entry->rmx_cwnd + 6613ac12506SJonathan T. Looney (uint64_t)hcml->rmx_cwnd) / 2; 66278b50714SRobert Watson /* TCPSTAT_INC(tcps_cachedcwnd); */ 66397d8d152SAndre Oppermann } 66497d8d152SAndre Oppermann if (hcml->rmx_sendpipe != 0) { 66597d8d152SAndre Oppermann if (hc_entry->rmx_sendpipe == 0) 66697d8d152SAndre Oppermann hc_entry->rmx_sendpipe = hcml->rmx_sendpipe; 66797d8d152SAndre Oppermann else 66897d8d152SAndre Oppermann hc_entry->rmx_sendpipe = 6693ac12506SJonathan T. Looney ((uint64_t)hc_entry->rmx_sendpipe + 6703ac12506SJonathan T. Looney (uint64_t)hcml->rmx_sendpipe) /2; 67178b50714SRobert Watson /* TCPSTAT_INC(tcps_cachedsendpipe); */ 67297d8d152SAndre Oppermann } 67397d8d152SAndre Oppermann if (hcml->rmx_recvpipe != 0) { 67497d8d152SAndre Oppermann if (hc_entry->rmx_recvpipe == 0) 67597d8d152SAndre Oppermann hc_entry->rmx_recvpipe = hcml->rmx_recvpipe; 67697d8d152SAndre Oppermann else 67797d8d152SAndre Oppermann hc_entry->rmx_recvpipe = 6783ac12506SJonathan T. Looney ((uint64_t)hc_entry->rmx_recvpipe + 6793ac12506SJonathan T. Looney (uint64_t)hcml->rmx_recvpipe) /2; 68078b50714SRobert Watson /* TCPSTAT_INC(tcps_cachedrecvpipe); */ 68197d8d152SAndre Oppermann } 68297d8d152SAndre Oppermann 68397d8d152SAndre Oppermann TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 68497d8d152SAndre Oppermann TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 68597d8d152SAndre Oppermann THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 68697d8d152SAndre Oppermann } 68797d8d152SAndre Oppermann 68897d8d152SAndre Oppermann /* 68997d8d152SAndre Oppermann * Sysctl function: prints the list and values of all hostcache entries in 69097d8d152SAndre Oppermann * unsorted order. 69197d8d152SAndre Oppermann */ 69297d8d152SAndre Oppermann static int 69397d8d152SAndre Oppermann sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS) 69497d8d152SAndre Oppermann { 695751ccc42SIan Lepore const int linesize = 128; 696002d4558SJohn Baldwin struct sbuf sb; 697cb0dd7e1SRichard Scheffenegger int i, error, len; 69897d8d152SAndre Oppermann struct hc_metrics *hc_entry; 6998144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 7001d54aa3bSBjoern A. Zeeb #ifdef INET6 7011d54aa3bSBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 7021d54aa3bSBjoern A. Zeeb #endif 70397d8d152SAndre Oppermann 704b8a2fb91SHiren Panchasara if (jailed_without_vnet(curthread->td_ucred) != 0) 705b8a2fb91SHiren Panchasara return (EPERM); 706b8a2fb91SHiren Panchasara 707cb0dd7e1SRichard Scheffenegger /* Optimize Buffer length query by sbin/sysctl */ 708cb0dd7e1SRichard Scheffenegger if (req->oldptr == NULL) { 709529a2a0fSRichard Scheffenegger len = (atomic_load_int(&V_tcp_hostcache.cache_count) + 1) * 710529a2a0fSRichard Scheffenegger linesize; 711cb0dd7e1SRichard Scheffenegger return (SYSCTL_OUT(req, NULL, len)); 712cb0dd7e1SRichard Scheffenegger } 713cb0dd7e1SRichard Scheffenegger 714cb0dd7e1SRichard Scheffenegger error = sysctl_wire_old_buffer(req, 0); 715cb0dd7e1SRichard Scheffenegger if (error != 0) { 716cb0dd7e1SRichard Scheffenegger return(error); 717cb0dd7e1SRichard Scheffenegger } 718cb0dd7e1SRichard Scheffenegger 71986988046SRichard Scheffenegger /* Use a buffer sized for one full bucket */ 720529a2a0fSRichard Scheffenegger sbuf_new_for_sysctl(&sb, NULL, V_tcp_hostcache.bucket_limit * 721529a2a0fSRichard Scheffenegger linesize, req); 72297d8d152SAndre Oppermann 723002d4558SJohn Baldwin sbuf_printf(&sb, 7244d163382SHiren Panchasara "\nIP address MTU SSTRESH RTT RTTVAR " 725*489bde57SGleb Smirnoff " CWND SENDPIPE RECVPIPE " 726*489bde57SGleb Smirnoff #ifdef TCP_HC_COUNTERS 727*489bde57SGleb Smirnoff "HITS UPD " 728*489bde57SGleb Smirnoff #endif 729*489bde57SGleb Smirnoff "EXP\n"); 73086988046SRichard Scheffenegger sbuf_drain(&sb); 73197d8d152SAndre Oppermann 73297d8d152SAndre Oppermann #define msec(u) (((u) + 500) / 1000) 733603724d3SBjoern A. Zeeb for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 734603724d3SBjoern A. Zeeb THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 735603724d3SBjoern A. Zeeb TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket, 73697d8d152SAndre Oppermann rmx_q) { 737002d4558SJohn Baldwin sbuf_printf(&sb, 738*489bde57SGleb Smirnoff "%-15s %5u %8u %6lums %6lums %8u %8u %8u " 739*489bde57SGleb Smirnoff #ifdef TCP_HC_COUNTERS 740*489bde57SGleb Smirnoff "%4lu %4lu " 741*489bde57SGleb Smirnoff #endif 742*489bde57SGleb Smirnoff "%4i\n", 7438144690aSEric van Gyzen hc_entry->ip4.s_addr ? 7448144690aSEric van Gyzen inet_ntoa_r(hc_entry->ip4, ip4buf) : 74597d8d152SAndre Oppermann #ifdef INET6 7461d54aa3bSBjoern A. Zeeb ip6_sprintf(ip6buf, &hc_entry->ip6), 74797d8d152SAndre Oppermann #else 74897d8d152SAndre Oppermann "IPv6?", 74997d8d152SAndre Oppermann #endif 75097d8d152SAndre Oppermann hc_entry->rmx_mtu, 75197d8d152SAndre Oppermann hc_entry->rmx_ssthresh, 7523ac12506SJonathan T. Looney msec((u_long)hc_entry->rmx_rtt * 75397d8d152SAndre Oppermann (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))), 7543ac12506SJonathan T. Looney msec((u_long)hc_entry->rmx_rttvar * 7553a288e90SMikolaj Golub (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))), 75697d8d152SAndre Oppermann hc_entry->rmx_cwnd, 75797d8d152SAndre Oppermann hc_entry->rmx_sendpipe, 75897d8d152SAndre Oppermann hc_entry->rmx_recvpipe, 759*489bde57SGleb Smirnoff #ifdef TCP_HC_COUNTERS 76097d8d152SAndre Oppermann hc_entry->rmx_hits, 76197d8d152SAndre Oppermann hc_entry->rmx_updates, 762*489bde57SGleb Smirnoff #endif 76397d8d152SAndre Oppermann hc_entry->rmx_expire); 76497d8d152SAndre Oppermann } 765603724d3SBjoern A. Zeeb THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 76686988046SRichard Scheffenegger sbuf_drain(&sb); 7679aef4e7cSRichard Scheffenegger } 76897d8d152SAndre Oppermann #undef msec 769751ccc42SIan Lepore error = sbuf_finish(&sb); 770002d4558SJohn Baldwin sbuf_delete(&sb); 77197d8d152SAndre Oppermann return(error); 77297d8d152SAndre Oppermann } 77397d8d152SAndre Oppermann 77497d8d152SAndre Oppermann /* 77502f26e98SRichard Scheffenegger * Sysctl function: prints a histogram of the hostcache hashbucket 77602f26e98SRichard Scheffenegger * utilization. 77702f26e98SRichard Scheffenegger */ 77802f26e98SRichard Scheffenegger static int 77902f26e98SRichard Scheffenegger sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS) 78002f26e98SRichard Scheffenegger { 78102f26e98SRichard Scheffenegger const int linesize = 50; 78202f26e98SRichard Scheffenegger struct sbuf sb; 78302f26e98SRichard Scheffenegger int i, error; 78402f26e98SRichard Scheffenegger int *histo; 78502f26e98SRichard Scheffenegger u_int hch_length; 78602f26e98SRichard Scheffenegger 78702f26e98SRichard Scheffenegger if (jailed_without_vnet(curthread->td_ucred) != 0) 78802f26e98SRichard Scheffenegger return (EPERM); 78902f26e98SRichard Scheffenegger 79002f26e98SRichard Scheffenegger histo = (int *)malloc(sizeof(int) * (V_tcp_hostcache.bucket_limit + 1), 79102f26e98SRichard Scheffenegger M_TEMP, M_NOWAIT|M_ZERO); 79202f26e98SRichard Scheffenegger if (histo == NULL) 79302f26e98SRichard Scheffenegger return(ENOMEM); 79402f26e98SRichard Scheffenegger 79502f26e98SRichard Scheffenegger for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 79602f26e98SRichard Scheffenegger hch_length = V_tcp_hostcache.hashbase[i].hch_length; 79702f26e98SRichard Scheffenegger KASSERT(hch_length <= V_tcp_hostcache.bucket_limit, 79802f26e98SRichard Scheffenegger ("tcp_hostcache: bucket limit exceeded at %u: %u", 79902f26e98SRichard Scheffenegger i, hch_length)); 80002f26e98SRichard Scheffenegger histo[hch_length]++; 80102f26e98SRichard Scheffenegger } 80202f26e98SRichard Scheffenegger 80302f26e98SRichard Scheffenegger /* Use a buffer for 16 lines */ 80402f26e98SRichard Scheffenegger sbuf_new_for_sysctl(&sb, NULL, 16 * linesize, req); 80502f26e98SRichard Scheffenegger 80602f26e98SRichard Scheffenegger sbuf_printf(&sb, "\nLength\tCount\n"); 80702f26e98SRichard Scheffenegger for (i = 0; i <= V_tcp_hostcache.bucket_limit; i++) { 80802f26e98SRichard Scheffenegger sbuf_printf(&sb, "%u\t%u\n", i, histo[i]); 80902f26e98SRichard Scheffenegger } 81002f26e98SRichard Scheffenegger error = sbuf_finish(&sb); 81102f26e98SRichard Scheffenegger sbuf_delete(&sb); 81202f26e98SRichard Scheffenegger free(histo, M_TEMP); 81302f26e98SRichard Scheffenegger return(error); 81402f26e98SRichard Scheffenegger } 81502f26e98SRichard Scheffenegger 81602f26e98SRichard Scheffenegger /* 817fffb9f1dSBjoern A. Zeeb * Caller has to make sure the curvnet is set properly. 81897d8d152SAndre Oppermann */ 81997d8d152SAndre Oppermann static void 820fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(int all) 82197d8d152SAndre Oppermann { 822b62dccc7SAndre Oppermann struct hc_metrics *hc_entry, *hc_next; 82397d8d152SAndre Oppermann int i; 82497d8d152SAndre Oppermann 825603724d3SBjoern A. Zeeb for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 826603724d3SBjoern A. Zeeb THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 827ac957cd2SJulian Elischer TAILQ_FOREACH_SAFE(hc_entry, 8285ed3800eSJulian Elischer &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) { 82902f26e98SRichard Scheffenegger KASSERT(V_tcp_hostcache.hashbase[i].hch_length > 0 && 83002f26e98SRichard Scheffenegger V_tcp_hostcache.hashbase[i].hch_length <= 83102f26e98SRichard Scheffenegger V_tcp_hostcache.bucket_limit, 8329aef4e7cSRichard Scheffenegger ("tcp_hostcache: bucket length out of range at %u: %u", 83302f26e98SRichard Scheffenegger i, V_tcp_hostcache.hashbase[i].hch_length)); 83497d8d152SAndre Oppermann if (all || hc_entry->rmx_expire <= 0) { 835603724d3SBjoern A. Zeeb TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket, 83697d8d152SAndre Oppermann hc_entry, rmx_q); 837603724d3SBjoern A. Zeeb uma_zfree(V_tcp_hostcache.zone, hc_entry); 838603724d3SBjoern A. Zeeb V_tcp_hostcache.hashbase[i].hch_length--; 839529a2a0fSRichard Scheffenegger atomic_subtract_int(&V_tcp_hostcache.cache_count, 1); 84097d8d152SAndre Oppermann } else 841603724d3SBjoern A. Zeeb hc_entry->rmx_expire -= V_tcp_hostcache.prune; 84297d8d152SAndre Oppermann } 843603724d3SBjoern A. Zeeb THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 84497d8d152SAndre Oppermann } 845fffb9f1dSBjoern A. Zeeb } 846fffb9f1dSBjoern A. Zeeb 847fffb9f1dSBjoern A. Zeeb /* 848fffb9f1dSBjoern A. Zeeb * Expire and purge (old|all) entries in the tcp_hostcache. Runs 849fffb9f1dSBjoern A. Zeeb * periodically from the callout. 850fffb9f1dSBjoern A. Zeeb */ 851fffb9f1dSBjoern A. Zeeb static void 852fffb9f1dSBjoern A. Zeeb tcp_hc_purge(void *arg) 853fffb9f1dSBjoern A. Zeeb { 854fffb9f1dSBjoern A. Zeeb CURVNET_SET((struct vnet *) arg); 855fffb9f1dSBjoern A. Zeeb int all = 0; 856fffb9f1dSBjoern A. Zeeb 857fffb9f1dSBjoern A. Zeeb if (V_tcp_hostcache.purgeall) { 858fffb9f1dSBjoern A. Zeeb all = 1; 859fffb9f1dSBjoern A. Zeeb V_tcp_hostcache.purgeall = 0; 860fffb9f1dSBjoern A. Zeeb } 861fffb9f1dSBjoern A. Zeeb 862fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(all); 863ac957cd2SJulian Elischer 864ac957cd2SJulian Elischer callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 865ac957cd2SJulian Elischer tcp_hc_purge, arg); 86621ca7b57SMarko Zec CURVNET_RESTORE(); 86797d8d152SAndre Oppermann } 86826456380SHiren Panchasara 86926456380SHiren Panchasara /* 87026456380SHiren Panchasara * Expire and purge all entries in hostcache immediately. 87126456380SHiren Panchasara */ 87226456380SHiren Panchasara static int 87326456380SHiren Panchasara sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS) 87426456380SHiren Panchasara { 87526456380SHiren Panchasara int error, val; 87626456380SHiren Panchasara 87726456380SHiren Panchasara val = 0; 87826456380SHiren Panchasara error = sysctl_handle_int(oidp, &val, 0, req); 87926456380SHiren Panchasara if (error || !req->newptr) 88026456380SHiren Panchasara return (error); 88126456380SHiren Panchasara 88226456380SHiren Panchasara tcp_hc_purge_internal(1); 88326456380SHiren Panchasara 88426456380SHiren Panchasara callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 88526456380SHiren Panchasara tcp_hc_purge, curvnet); 88626456380SHiren Panchasara 88726456380SHiren Panchasara return (0); 88826456380SHiren Panchasara } 889