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 5d554522fSGleb Smirnoff * Copyright (c) 2021 Gleb Smirnoff <glebius@FreeBSD.org> 697d8d152SAndre Oppermann * All rights reserved. 797d8d152SAndre Oppermann * 897d8d152SAndre Oppermann * Redistribution and use in source and binary forms, with or without 997d8d152SAndre Oppermann * modification, are permitted provided that the following conditions 1097d8d152SAndre Oppermann * are met: 1197d8d152SAndre Oppermann * 1. Redistributions of source code must retain the above copyright 1297d8d152SAndre Oppermann * notice, this list of conditions and the following disclaimer. 1397d8d152SAndre Oppermann * 2. Redistributions in binary form must reproduce the above copyright 1497d8d152SAndre Oppermann * notice, this list of conditions and the following disclaimer in the 1597d8d152SAndre Oppermann * documentation and/or other materials provided with the distribution. 1697d8d152SAndre Oppermann * 3. The name of the author may not be used to endorse or promote 1797d8d152SAndre Oppermann * products derived from this software without specific prior written 1897d8d152SAndre Oppermann * permission. 1997d8d152SAndre Oppermann * 2097d8d152SAndre Oppermann * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2197d8d152SAndre Oppermann * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2297d8d152SAndre Oppermann * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2397d8d152SAndre Oppermann * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2497d8d152SAndre Oppermann * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2597d8d152SAndre Oppermann * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2697d8d152SAndre Oppermann * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2797d8d152SAndre Oppermann * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2897d8d152SAndre Oppermann * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2997d8d152SAndre Oppermann * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3097d8d152SAndre Oppermann * SUCH DAMAGE. 3197d8d152SAndre Oppermann */ 3297d8d152SAndre Oppermann 3397d8d152SAndre Oppermann /* 34e487a5e2SRobert Watson * The tcp_hostcache moves the tcp-specific cached metrics from the routing 35e487a5e2SRobert Watson * table to a dedicated structure indexed by the remote IP address. It keeps 36e487a5e2SRobert Watson * information on the measured TCP parameters of past TCP sessions to allow 37e487a5e2SRobert Watson * better initial start values to be used with later connections to/from the 384d163382SHiren Panchasara * same source. Depending on the network parameters (delay, max MTU, 394d163382SHiren Panchasara * congestion window) between local and remote sites, this can lead to 40e487a5e2SRobert Watson * significant speed-ups for new TCP connections after the first one. 4197d8d152SAndre Oppermann * 42e487a5e2SRobert Watson * Due to the tcp_hostcache, all TCP-specific metrics information in the 43003c7e36SRui Paulo * routing table have been removed. The inpcb no longer keeps a pointer to 44e487a5e2SRobert Watson * the routing entry, and protocol-initiated route cloning has been removed 45e487a5e2SRobert Watson * as well. With these changes, the routing table has gone back to being 46e487a5e2SRobert Watson * more lightwight and only carries information related to packet forwarding. 4797d8d152SAndre Oppermann * 48e487a5e2SRobert Watson * tcp_hostcache is designed for multiple concurrent access in SMP 49d554522fSGleb Smirnoff * environments and high contention. It is a straight hash. Each bucket row 50d554522fSGleb Smirnoff * is protected by its own lock for modification. Readers are protected by 51d554522fSGleb Smirnoff * SMR. This puts certain restrictions on writers, e.g. a writer shall only 52d554522fSGleb Smirnoff * insert a fully populated entry into a row. Writer can't reuse least used 53d554522fSGleb Smirnoff * entry if a hash is full. Value updates for an entry shall be atomic. 54d554522fSGleb Smirnoff * 55d554522fSGleb Smirnoff * TCP stack(s) communication with tcp_hostcache() is done via KBI functions 56d554522fSGleb Smirnoff * tcp_hc_*() and the hc_metrics_lite structure. 57d554522fSGleb Smirnoff * 58d554522fSGleb Smirnoff * Since tcp_hostcache is only caching information, there are no fatal 59d554522fSGleb Smirnoff * consequences if we either can't allocate a new entry or have to drop 60d554522fSGleb Smirnoff * an existing entry, or return somewhat stale information. 6197d8d152SAndre Oppermann */ 6297d8d152SAndre Oppermann 6397d8d152SAndre Oppermann /* 6497d8d152SAndre Oppermann * Many thanks to jlemon for basic structure of tcp_syncache which is being 6597d8d152SAndre Oppermann * followed here. 6697d8d152SAndre Oppermann */ 6797d8d152SAndre Oppermann 684b421e2dSMike Silbersack #include <sys/cdefs.h> 6997d8d152SAndre Oppermann #include "opt_inet6.h" 7097d8d152SAndre Oppermann 7197d8d152SAndre Oppermann #include <sys/param.h> 7297d8d152SAndre Oppermann #include <sys/systm.h> 73b878ec02SRichard Scheffenegger #include <sys/hash.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> 81d554522fSGleb Smirnoff #include <sys/smr.h> 8297d8d152SAndre Oppermann #include <sys/socket.h> 8397d8d152SAndre Oppermann #include <sys/sysctl.h> 8497d8d152SAndre Oppermann 85530c0060SRobert Watson #include <net/vnet.h> 8697d8d152SAndre Oppermann 8797d8d152SAndre Oppermann #include <netinet/in.h> 8897d8d152SAndre Oppermann #include <netinet/in_pcb.h> 8997d8d152SAndre Oppermann #include <netinet/tcp.h> 9097d8d152SAndre Oppermann #include <netinet/tcp_var.h> 9197d8d152SAndre Oppermann 9297d8d152SAndre Oppermann #include <vm/uma.h> 9397d8d152SAndre Oppermann 942cca4c0eSGleb Smirnoff struct hc_head { 95d554522fSGleb Smirnoff CK_SLIST_HEAD(hc_qhead, hc_metrics) hch_bucket; 962cca4c0eSGleb Smirnoff u_int hch_length; 972cca4c0eSGleb Smirnoff struct mtx hch_mtx; 982cca4c0eSGleb Smirnoff }; 992cca4c0eSGleb Smirnoff 1002cca4c0eSGleb Smirnoff struct hc_metrics { 1012cca4c0eSGleb Smirnoff /* housekeeping */ 102*09000cc1SGleb Smirnoff CK_SLIST_ENTRY(hc_metrics) hc_q; 1032cca4c0eSGleb Smirnoff struct in_addr ip4; /* IP address */ 1042cca4c0eSGleb Smirnoff struct in6_addr ip6; /* IP6 address */ 1052cca4c0eSGleb Smirnoff uint32_t ip6_zoneid; /* IPv6 scope zone id */ 1062cca4c0eSGleb Smirnoff /* endpoint specific values for tcp */ 107*09000cc1SGleb Smirnoff uint32_t hc_mtu; /* MTU for this path */ 108*09000cc1SGleb Smirnoff uint32_t hc_ssthresh; /* outbound gateway buffer limit */ 109*09000cc1SGleb Smirnoff uint32_t hc_rtt; /* estimated round trip time */ 110*09000cc1SGleb Smirnoff uint32_t hc_rttvar; /* estimated rtt variance */ 111*09000cc1SGleb Smirnoff uint32_t hc_cwnd; /* congestion window */ 112*09000cc1SGleb Smirnoff uint32_t hc_sendpipe; /* outbound delay-bandwidth product */ 113*09000cc1SGleb Smirnoff uint32_t hc_recvpipe; /* inbound delay-bandwidth product */ 1142cca4c0eSGleb Smirnoff /* TCP hostcache internal data */ 115*09000cc1SGleb Smirnoff int hc_expire; /* lifetime for object */ 116489bde57SGleb Smirnoff #ifdef TCP_HC_COUNTERS 117*09000cc1SGleb Smirnoff u_long hc_hits; /* number of hits */ 118*09000cc1SGleb Smirnoff u_long hc_updates; /* number of updates */ 119489bde57SGleb Smirnoff #endif 1202cca4c0eSGleb Smirnoff }; 1212cca4c0eSGleb Smirnoff 1222cca4c0eSGleb Smirnoff struct tcp_hostcache { 1232cca4c0eSGleb Smirnoff struct hc_head *hashbase; 1242cca4c0eSGleb Smirnoff uma_zone_t zone; 125d554522fSGleb Smirnoff smr_t smr; 1262cca4c0eSGleb Smirnoff u_int hashsize; 1272cca4c0eSGleb Smirnoff u_int hashmask; 128b878ec02SRichard Scheffenegger u_int hashsalt; 1292cca4c0eSGleb Smirnoff u_int bucket_limit; 1302cca4c0eSGleb Smirnoff u_int cache_count; 1312cca4c0eSGleb Smirnoff u_int cache_limit; 1322cca4c0eSGleb Smirnoff int expire; 1332cca4c0eSGleb Smirnoff int prune; 1342cca4c0eSGleb Smirnoff int purgeall; 1352cca4c0eSGleb Smirnoff }; 1362cca4c0eSGleb Smirnoff 13797d8d152SAndre Oppermann /* Arbitrary values */ 13897d8d152SAndre Oppermann #define TCP_HOSTCACHE_HASHSIZE 512 13997d8d152SAndre Oppermann #define TCP_HOSTCACHE_BUCKETLIMIT 30 14097d8d152SAndre Oppermann #define TCP_HOSTCACHE_EXPIRE 60*60 /* one hour */ 14197d8d152SAndre Oppermann #define TCP_HOSTCACHE_PRUNE 5*60 /* every 5 minutes */ 14297d8d152SAndre Oppermann 1435f901c92SAndrew Turner VNET_DEFINE_STATIC(struct tcp_hostcache, tcp_hostcache); 1441e77c105SRobert Watson #define V_tcp_hostcache VNET(tcp_hostcache) 14582cea7e6SBjoern A. Zeeb 1465f901c92SAndrew Turner VNET_DEFINE_STATIC(struct callout, tcp_hc_callout); 1471e77c105SRobert Watson #define V_tcp_hc_callout VNET(tcp_hc_callout) 14897d8d152SAndre Oppermann 149d554522fSGleb Smirnoff static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *); 15097d8d152SAndre Oppermann static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS); 15102f26e98SRichard Scheffenegger static int sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS); 15226456380SHiren Panchasara static int sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS); 153fffb9f1dSBjoern A. Zeeb static void tcp_hc_purge_internal(int); 15497d8d152SAndre Oppermann static void tcp_hc_purge(void *); 15597d8d152SAndre Oppermann 1567029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, 1577029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1580eb7cf4dSBrooks Davis "TCP Host cache"); 15997d8d152SAndre Oppermann 1608a56c645SHiren Panchasara VNET_DEFINE(int, tcp_use_hostcache) = 1; 1618a56c645SHiren Panchasara #define V_tcp_use_hostcache VNET(tcp_use_hostcache) 1628a56c645SHiren Panchasara SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 1638a56c645SHiren Panchasara &VNET_NAME(tcp_use_hostcache), 0, 1648a56c645SHiren Panchasara "Enable the TCP hostcache"); 1658a56c645SHiren Panchasara 1666df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN, 167eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.cache_limit), 0, 1688b615593SMarko Zec "Overall entry limit for hostcache"); 16997d8d152SAndre Oppermann 1706df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN, 171eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.hashsize), 0, 1728b615593SMarko Zec "Size of TCP hostcache hashtable"); 17397d8d152SAndre Oppermann 1746df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, 1756df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0, 1768b615593SMarko Zec "Per-bucket hash limit for hostcache"); 17797d8d152SAndre Oppermann 178529a2a0fSRichard Scheffenegger SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD, 179529a2a0fSRichard Scheffenegger &VNET_NAME(tcp_hostcache.cache_count), 0, 1808b615593SMarko Zec "Current number of entries in hostcache"); 18197d8d152SAndre Oppermann 1826df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW, 183eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.expire), 0, 1848b615593SMarko Zec "Expire time of TCP hostcache entries"); 18597d8d152SAndre Oppermann 1866df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW, 187eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.prune), 0, 188eddfbb76SRobert Watson "Time between purge runs"); 189dba3c508SYaroslav Tykhiy 1906df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW, 191eddfbb76SRobert Watson &VNET_NAME(tcp_hostcache.purgeall), 0, 192b4aa9cb2SGordon Bergling "Expire all entries on next purge run"); 19397d8d152SAndre Oppermann 19497d8d152SAndre Oppermann SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list, 1957029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, 1967029da5cSPawel Biernacki 0, 0, sysctl_tcp_hc_list, "A", 1977029da5cSPawel Biernacki "List of all hostcache entries"); 19897d8d152SAndre Oppermann 19902f26e98SRichard Scheffenegger SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, histo, 20002f26e98SRichard Scheffenegger CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, 20102f26e98SRichard Scheffenegger 0, 0, sysctl_tcp_hc_histo, "A", 20202f26e98SRichard Scheffenegger "Print a histogram of hostcache hashbucket utilization"); 20302f26e98SRichard Scheffenegger 20426456380SHiren Panchasara SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, purgenow, 2057029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 2067029da5cSPawel Biernacki NULL, 0, sysctl_tcp_hc_purgenow, "I", 2077029da5cSPawel Biernacki "Immediately purge all entries"); 20897d8d152SAndre Oppermann 20997d8d152SAndre Oppermann static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache"); 21097d8d152SAndre Oppermann 211b878ec02SRichard Scheffenegger /* Use jenkins_hash32(), as in other parts of the tcp stack */ 212d554522fSGleb Smirnoff #define HOSTCACHE_HASH(inc) \ 213d554522fSGleb Smirnoff ((inc)->inc_flags & INC_ISIPV6) ? \ 214d554522fSGleb Smirnoff (jenkins_hash32((inc)->inc6_faddr.s6_addr32, 4, \ 215d554522fSGleb Smirnoff V_tcp_hostcache.hashsalt) & V_tcp_hostcache.hashmask) \ 216d554522fSGleb Smirnoff : \ 217d554522fSGleb Smirnoff (jenkins_hash32(&(inc)->inc_faddr.s_addr, 1, \ 218d554522fSGleb Smirnoff V_tcp_hostcache.hashsalt) & V_tcp_hostcache.hashmask) 21997d8d152SAndre Oppermann 2201a7fe55aSGleb Smirnoff #define THC_LOCK(h) mtx_lock(&(h)->hch_mtx) 2211a7fe55aSGleb Smirnoff #define THC_UNLOCK(h) mtx_unlock(&(h)->hch_mtx) 22297d8d152SAndre Oppermann 22397d8d152SAndre Oppermann void 22497d8d152SAndre Oppermann tcp_hc_init(void) 22597d8d152SAndre Oppermann { 22632fe38f1SAndrey Zonov u_int cache_limit; 22797d8d152SAndre Oppermann int i; 22897d8d152SAndre Oppermann 22997d8d152SAndre Oppermann /* 230e487a5e2SRobert Watson * Initialize hostcache structures. 23197d8d152SAndre Oppermann */ 232529a2a0fSRichard Scheffenegger atomic_store_int(&V_tcp_hostcache.cache_count, 0); 233603724d3SBjoern A. Zeeb V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; 234603724d3SBjoern A. Zeeb V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT; 235603724d3SBjoern A. Zeeb V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE; 236603724d3SBjoern A. Zeeb V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE; 237b878ec02SRichard Scheffenegger V_tcp_hostcache.hashsalt = arc4random(); 23897d8d152SAndre Oppermann 23997d8d152SAndre Oppermann TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize", 240603724d3SBjoern A. Zeeb &V_tcp_hostcache.hashsize); 241603724d3SBjoern A. Zeeb if (!powerof2(V_tcp_hostcache.hashsize)) { 24297d8d152SAndre Oppermann printf("WARNING: hostcache hash size is not a power of 2.\n"); 243603724d3SBjoern A. Zeeb V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */ 24497d8d152SAndre Oppermann } 245603724d3SBjoern A. Zeeb V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1; 24697d8d152SAndre Oppermann 24732fe38f1SAndrey Zonov TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit", 24832fe38f1SAndrey Zonov &V_tcp_hostcache.bucket_limit); 24932fe38f1SAndrey Zonov 25032fe38f1SAndrey Zonov cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit; 25132fe38f1SAndrey Zonov V_tcp_hostcache.cache_limit = cache_limit; 25232fe38f1SAndrey Zonov TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit", 25332fe38f1SAndrey Zonov &V_tcp_hostcache.cache_limit); 25432fe38f1SAndrey Zonov if (V_tcp_hostcache.cache_limit > cache_limit) 25532fe38f1SAndrey Zonov V_tcp_hostcache.cache_limit = cache_limit; 25632fe38f1SAndrey Zonov 25797d8d152SAndre Oppermann /* 258e487a5e2SRobert Watson * Allocate the hash table. 25997d8d152SAndre Oppermann */ 260603724d3SBjoern A. Zeeb V_tcp_hostcache.hashbase = (struct hc_head *) 261603724d3SBjoern A. Zeeb malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head), 26297d8d152SAndre Oppermann M_HOSTCACHE, M_WAITOK | M_ZERO); 26397d8d152SAndre Oppermann 26497d8d152SAndre Oppermann /* 265e487a5e2SRobert Watson * Initialize the hash buckets. 26697d8d152SAndre Oppermann */ 267603724d3SBjoern A. Zeeb for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 268d554522fSGleb Smirnoff CK_SLIST_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket); 269603724d3SBjoern A. Zeeb V_tcp_hostcache.hashbase[i].hch_length = 0; 270603724d3SBjoern A. Zeeb mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry", 27197d8d152SAndre Oppermann NULL, MTX_DEF); 27297d8d152SAndre Oppermann } 27397d8d152SAndre Oppermann 27497d8d152SAndre Oppermann /* 27597d8d152SAndre Oppermann * Allocate the hostcache entries. 27697d8d152SAndre Oppermann */ 277ac957cd2SJulian Elischer V_tcp_hostcache.zone = 278ac957cd2SJulian Elischer uma_zcreate("hostcache", sizeof(struct hc_metrics), 279d554522fSGleb Smirnoff NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_SMR); 280603724d3SBjoern A. Zeeb uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit); 281d554522fSGleb Smirnoff V_tcp_hostcache.smr = uma_zone_get_smr(V_tcp_hostcache.zone); 28297d8d152SAndre Oppermann 28397d8d152SAndre Oppermann /* 28497d8d152SAndre Oppermann * Set up periodic cache cleanup. 28597d8d152SAndre Oppermann */ 286fd90e2edSJung-uk Kim callout_init(&V_tcp_hc_callout, 1); 287ac957cd2SJulian Elischer callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 28821ca7b57SMarko Zec tcp_hc_purge, curvnet); 28997d8d152SAndre Oppermann } 29097d8d152SAndre Oppermann 291bc29160dSMarko Zec #ifdef VIMAGE 292bc29160dSMarko Zec void 293bc29160dSMarko Zec tcp_hc_destroy(void) 294bc29160dSMarko Zec { 295fffb9f1dSBjoern A. Zeeb int i; 296bc29160dSMarko Zec 297bc29160dSMarko Zec callout_drain(&V_tcp_hc_callout); 298fffb9f1dSBjoern A. Zeeb 299fffb9f1dSBjoern A. Zeeb /* Purge all hc entries. */ 300fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(1); 301fffb9f1dSBjoern A. Zeeb 302fffb9f1dSBjoern A. Zeeb /* Free the uma zone and the allocated hash table. */ 303fffb9f1dSBjoern A. Zeeb uma_zdestroy(V_tcp_hostcache.zone); 304fffb9f1dSBjoern A. Zeeb 305fffb9f1dSBjoern A. Zeeb for (i = 0; i < V_tcp_hostcache.hashsize; i++) 306fffb9f1dSBjoern A. Zeeb mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx); 307fffb9f1dSBjoern A. Zeeb free(V_tcp_hostcache.hashbase, M_HOSTCACHE); 308bc29160dSMarko Zec } 309bc29160dSMarko Zec #endif 310bc29160dSMarko Zec 31197d8d152SAndre Oppermann /* 312d554522fSGleb Smirnoff * Internal function: compare cache entry to a connection. 31397d8d152SAndre Oppermann */ 314d554522fSGleb Smirnoff static bool 315d554522fSGleb Smirnoff tcp_hc_cmp(struct hc_metrics *hc_entry, struct in_conninfo *inc) 31697d8d152SAndre Oppermann { 31797d8d152SAndre Oppermann 318dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 319028bdf28SAndrey V. Elsukov /* XXX: check ip6_zoneid */ 32097d8d152SAndre Oppermann if (memcmp(&inc->inc6_faddr, &hc_entry->ip6, 32197d8d152SAndre Oppermann sizeof(inc->inc6_faddr)) == 0) 322d554522fSGleb Smirnoff return (true); 32397d8d152SAndre Oppermann } else { 32497d8d152SAndre Oppermann if (memcmp(&inc->inc_faddr, &hc_entry->ip4, 32597d8d152SAndre Oppermann sizeof(inc->inc_faddr)) == 0) 326d554522fSGleb Smirnoff return (true); 32797d8d152SAndre Oppermann } 328d554522fSGleb Smirnoff 329d554522fSGleb Smirnoff return (false); 33097d8d152SAndre Oppermann } 33197d8d152SAndre Oppermann 33297d8d152SAndre Oppermann /* 333d554522fSGleb Smirnoff * Internal function: look up an entry in the hostcache for read. 334d554522fSGleb Smirnoff * On success returns in SMR section. 33597d8d152SAndre Oppermann */ 33697d8d152SAndre Oppermann static struct hc_metrics * 337d554522fSGleb Smirnoff tcp_hc_lookup(struct in_conninfo *inc) 33897d8d152SAndre Oppermann { 33997d8d152SAndre Oppermann struct hc_head *hc_head; 34097d8d152SAndre Oppermann struct hc_metrics *hc_entry; 34197d8d152SAndre Oppermann 3424f49e338SGleb Smirnoff KASSERT(inc != NULL, ("%s: NULL in_conninfo", __func__)); 34397d8d152SAndre Oppermann 344d554522fSGleb Smirnoff hc_head = &V_tcp_hostcache.hashbase[HOSTCACHE_HASH(inc)]; 34597d8d152SAndre Oppermann 34697d8d152SAndre Oppermann /* 347d554522fSGleb Smirnoff * Iterate through entries in bucket row looking for a match. 34897d8d152SAndre Oppermann */ 349d554522fSGleb Smirnoff smr_enter(V_tcp_hostcache.smr); 350*09000cc1SGleb Smirnoff CK_SLIST_FOREACH(hc_entry, &hc_head->hch_bucket, hc_q) 351d554522fSGleb Smirnoff if (tcp_hc_cmp(hc_entry, inc)) 352d554522fSGleb Smirnoff break; 35397d8d152SAndre Oppermann 354d554522fSGleb Smirnoff if (hc_entry != NULL) { 355*09000cc1SGleb Smirnoff if (atomic_load_int(&hc_entry->hc_expire) != 356d554522fSGleb Smirnoff V_tcp_hostcache.expire) 357*09000cc1SGleb Smirnoff atomic_store_int(&hc_entry->hc_expire, 358d554522fSGleb Smirnoff V_tcp_hostcache.expire); 359d554522fSGleb Smirnoff #ifdef TCP_HC_COUNTERS 360*09000cc1SGleb Smirnoff hc_entry->hc_hits++; 361cd6c4060SAndre Oppermann #endif 362028bdf28SAndrey V. Elsukov } else 363d554522fSGleb Smirnoff smr_exit(V_tcp_hostcache.smr); 36497d8d152SAndre Oppermann 3654f49e338SGleb Smirnoff return (hc_entry); 36697d8d152SAndre Oppermann } 36797d8d152SAndre Oppermann 36897d8d152SAndre Oppermann /* 36997d8d152SAndre Oppermann * External function: look up an entry in the hostcache and fill out the 370e487a5e2SRobert Watson * supplied TCP metrics structure. Fills in NULL when no entry was found or 371e487a5e2SRobert Watson * a value is not set. 37297d8d152SAndre Oppermann */ 37397d8d152SAndre Oppermann void 37497d8d152SAndre Oppermann tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite) 37597d8d152SAndre Oppermann { 37697d8d152SAndre Oppermann struct hc_metrics *hc_entry; 37797d8d152SAndre Oppermann 3786b8fba3cSMichael Tuexen if (!V_tcp_use_hostcache) { 3796b8fba3cSMichael Tuexen bzero(hc_metrics_lite, sizeof(*hc_metrics_lite)); 3808a56c645SHiren Panchasara return; 3816b8fba3cSMichael Tuexen } 3828a56c645SHiren Panchasara 38397d8d152SAndre Oppermann /* 384e487a5e2SRobert Watson * Find the right bucket. 38597d8d152SAndre Oppermann */ 386d554522fSGleb Smirnoff hc_entry = tcp_hc_lookup(inc); 38797d8d152SAndre Oppermann 38897d8d152SAndre Oppermann /* 389e487a5e2SRobert Watson * If we don't have an existing object. 39097d8d152SAndre Oppermann */ 39197d8d152SAndre Oppermann if (hc_entry == NULL) { 39297d8d152SAndre Oppermann bzero(hc_metrics_lite, sizeof(*hc_metrics_lite)); 39397d8d152SAndre Oppermann return; 39497d8d152SAndre Oppermann } 39597d8d152SAndre Oppermann 396*09000cc1SGleb Smirnoff hc_metrics_lite->hc_mtu = atomic_load_32(&hc_entry->hc_mtu); 397*09000cc1SGleb Smirnoff hc_metrics_lite->hc_ssthresh = atomic_load_32(&hc_entry->hc_ssthresh); 398*09000cc1SGleb Smirnoff hc_metrics_lite->hc_rtt = atomic_load_32(&hc_entry->hc_rtt); 399*09000cc1SGleb Smirnoff hc_metrics_lite->hc_rttvar = atomic_load_32(&hc_entry->hc_rttvar); 400*09000cc1SGleb Smirnoff hc_metrics_lite->hc_cwnd = atomic_load_32(&hc_entry->hc_cwnd); 401*09000cc1SGleb Smirnoff hc_metrics_lite->hc_sendpipe = atomic_load_32(&hc_entry->hc_sendpipe); 402*09000cc1SGleb Smirnoff hc_metrics_lite->hc_recvpipe = atomic_load_32(&hc_entry->hc_recvpipe); 40397d8d152SAndre Oppermann 404d554522fSGleb Smirnoff smr_exit(V_tcp_hostcache.smr); 40597d8d152SAndre Oppermann } 40697d8d152SAndre Oppermann 40797d8d152SAndre Oppermann /* 40897d8d152SAndre Oppermann * External function: look up an entry in the hostcache and return the 4098a56c645SHiren Panchasara * discovered path MTU. Returns 0 if no entry is found or value is not 4106fbed4afSRobert Watson * set. 41197d8d152SAndre Oppermann */ 4123ac12506SJonathan T. Looney uint32_t 41397d8d152SAndre Oppermann tcp_hc_getmtu(struct in_conninfo *inc) 41497d8d152SAndre Oppermann { 41597d8d152SAndre Oppermann struct hc_metrics *hc_entry; 4163ac12506SJonathan T. Looney uint32_t mtu; 41797d8d152SAndre Oppermann 4188a56c645SHiren Panchasara if (!V_tcp_use_hostcache) 4194f49e338SGleb Smirnoff return (0); 4208a56c645SHiren Panchasara 421d554522fSGleb Smirnoff hc_entry = tcp_hc_lookup(inc); 42297d8d152SAndre Oppermann if (hc_entry == NULL) { 4234f49e338SGleb Smirnoff return (0); 42497d8d152SAndre Oppermann } 42597d8d152SAndre Oppermann 426*09000cc1SGleb Smirnoff mtu = atomic_load_32(&hc_entry->hc_mtu); 427d554522fSGleb Smirnoff smr_exit(V_tcp_hostcache.smr); 428d554522fSGleb Smirnoff 4294f49e338SGleb Smirnoff return (mtu); 43097d8d152SAndre Oppermann } 43197d8d152SAndre Oppermann 43297d8d152SAndre Oppermann /* 433e487a5e2SRobert Watson * External function: update the MTU value of an entry in the hostcache. 43497d8d152SAndre Oppermann * Creates a new entry if none was found. 43597d8d152SAndre Oppermann */ 43697d8d152SAndre Oppermann void 4373ac12506SJonathan T. Looney tcp_hc_updatemtu(struct in_conninfo *inc, uint32_t mtu) 43897d8d152SAndre Oppermann { 439*09000cc1SGleb Smirnoff struct hc_metrics_lite hcml = { .hc_mtu = mtu }; 44097d8d152SAndre Oppermann 4410c25bf7eSGleb Smirnoff return (tcp_hc_update(inc, &hcml)); 44297d8d152SAndre Oppermann } 44397d8d152SAndre Oppermann 44497d8d152SAndre Oppermann /* 445e487a5e2SRobert Watson * External function: update the TCP metrics of an entry in the hostcache. 44697d8d152SAndre Oppermann * Creates a new entry if none was found. 44797d8d152SAndre Oppermann */ 44897d8d152SAndre Oppermann void 44997d8d152SAndre Oppermann tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml) 45097d8d152SAndre Oppermann { 451d554522fSGleb Smirnoff struct hc_head *hc_head; 452d554522fSGleb Smirnoff struct hc_metrics *hc_entry, *hc_prev; 453d554522fSGleb Smirnoff uint32_t v; 454d554522fSGleb Smirnoff bool new; 45597d8d152SAndre Oppermann 4568a56c645SHiren Panchasara if (!V_tcp_use_hostcache) 4578a56c645SHiren Panchasara return; 4588a56c645SHiren Panchasara 459d554522fSGleb Smirnoff hc_head = &V_tcp_hostcache.hashbase[HOSTCACHE_HASH(inc)]; 460d554522fSGleb Smirnoff hc_prev = NULL; 461d554522fSGleb Smirnoff 462d554522fSGleb Smirnoff THC_LOCK(hc_head); 463*09000cc1SGleb Smirnoff CK_SLIST_FOREACH(hc_entry, &hc_head->hch_bucket, hc_q) { 464d554522fSGleb Smirnoff if (tcp_hc_cmp(hc_entry, inc)) 465d554522fSGleb Smirnoff break; 466*09000cc1SGleb Smirnoff if (CK_SLIST_NEXT(hc_entry, hc_q) != NULL) 467d554522fSGleb Smirnoff hc_prev = hc_entry; 468d554522fSGleb Smirnoff } 469d554522fSGleb Smirnoff 470d554522fSGleb Smirnoff if (hc_entry != NULL) { 471*09000cc1SGleb Smirnoff if (atomic_load_int(&hc_entry->hc_expire) != 472d554522fSGleb Smirnoff V_tcp_hostcache.expire) 473*09000cc1SGleb Smirnoff atomic_store_int(&hc_entry->hc_expire, 474d554522fSGleb Smirnoff V_tcp_hostcache.expire); 475d554522fSGleb Smirnoff #ifdef TCP_HC_COUNTERS 476*09000cc1SGleb Smirnoff hc_entry->hc_updates++; 477d554522fSGleb Smirnoff #endif 478d554522fSGleb Smirnoff new = false; 479d554522fSGleb Smirnoff } else { 480d554522fSGleb Smirnoff /* 481d554522fSGleb Smirnoff * Try to allocate a new entry. If the bucket limit is 482d554522fSGleb Smirnoff * reached, delete the least-used element, located at the end 483d554522fSGleb Smirnoff * of the CK_SLIST. During lookup we saved the pointer to 484d554522fSGleb Smirnoff * the second to last element, in case if list has at least 2 485d554522fSGleb Smirnoff * elements. This will allow to delete last element without 486d554522fSGleb Smirnoff * extra traversal. 487d554522fSGleb Smirnoff * 488d554522fSGleb Smirnoff * Give up if the row is empty. 489d554522fSGleb Smirnoff */ 490d554522fSGleb Smirnoff if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit || 491d554522fSGleb Smirnoff atomic_load_int(&V_tcp_hostcache.cache_count) >= 492d554522fSGleb Smirnoff V_tcp_hostcache.cache_limit) { 493d554522fSGleb Smirnoff if (hc_prev != NULL) { 494*09000cc1SGleb Smirnoff hc_entry = CK_SLIST_NEXT(hc_prev, hc_q); 495*09000cc1SGleb Smirnoff KASSERT(CK_SLIST_NEXT(hc_entry, hc_q) == NULL, 496d554522fSGleb Smirnoff ("%s: %p is not one to last", 497d554522fSGleb Smirnoff __func__, hc_prev)); 498*09000cc1SGleb Smirnoff CK_SLIST_REMOVE_AFTER(hc_prev, hc_q); 499d554522fSGleb Smirnoff } else if ((hc_entry = 500d554522fSGleb Smirnoff CK_SLIST_FIRST(&hc_head->hch_bucket)) != NULL) { 501*09000cc1SGleb Smirnoff KASSERT(CK_SLIST_NEXT(hc_entry, hc_q) == NULL, 502d554522fSGleb Smirnoff ("%s: %p is not the only element", 503d554522fSGleb Smirnoff __func__, hc_entry)); 504d554522fSGleb Smirnoff CK_SLIST_REMOVE_HEAD(&hc_head->hch_bucket, 505*09000cc1SGleb Smirnoff hc_q); 506d554522fSGleb Smirnoff } else { 507d554522fSGleb Smirnoff THC_UNLOCK(hc_head); 508d554522fSGleb Smirnoff return; 509d554522fSGleb Smirnoff } 510d554522fSGleb Smirnoff KASSERT(hc_head->hch_length > 0 && 511d554522fSGleb Smirnoff hc_head->hch_length <= V_tcp_hostcache.bucket_limit, 512d554522fSGleb Smirnoff ("tcp_hostcache: bucket length violated at %p", 513d554522fSGleb Smirnoff hc_head)); 514d554522fSGleb Smirnoff hc_head->hch_length--; 515d554522fSGleb Smirnoff atomic_subtract_int(&V_tcp_hostcache.cache_count, 1); 516d554522fSGleb Smirnoff TCPSTAT_INC(tcps_hc_bucketoverflow); 517d554522fSGleb Smirnoff uma_zfree_smr(V_tcp_hostcache.zone, hc_entry); 518d554522fSGleb Smirnoff } 519d554522fSGleb Smirnoff 520d554522fSGleb Smirnoff /* 521d554522fSGleb Smirnoff * Allocate a new entry, or balk if not possible. 522d554522fSGleb Smirnoff */ 523d554522fSGleb Smirnoff hc_entry = uma_zalloc_smr(V_tcp_hostcache.zone, M_NOWAIT); 52497d8d152SAndre Oppermann if (hc_entry == NULL) { 525d554522fSGleb Smirnoff THC_UNLOCK(hc_head); 52697d8d152SAndre Oppermann return; 52797d8d152SAndre Oppermann } 52897d8d152SAndre Oppermann 529d554522fSGleb Smirnoff /* 530d554522fSGleb Smirnoff * Initialize basic information of hostcache entry. 531d554522fSGleb Smirnoff */ 532d554522fSGleb Smirnoff bzero(hc_entry, sizeof(*hc_entry)); 533d554522fSGleb Smirnoff if (inc->inc_flags & INC_ISIPV6) { 534d554522fSGleb Smirnoff hc_entry->ip6 = inc->inc6_faddr; 535d554522fSGleb Smirnoff hc_entry->ip6_zoneid = inc->inc6_zoneid; 536d554522fSGleb Smirnoff } else 537d554522fSGleb Smirnoff hc_entry->ip4 = inc->inc_faddr; 538*09000cc1SGleb Smirnoff hc_entry->hc_expire = V_tcp_hostcache.expire; 539d554522fSGleb Smirnoff new = true; 540d554522fSGleb Smirnoff } 541d554522fSGleb Smirnoff 542d554522fSGleb Smirnoff /* 543d554522fSGleb Smirnoff * Fill in data. Use atomics, since an existing entry is 544d554522fSGleb Smirnoff * accessible by readers in SMR section. 545d554522fSGleb Smirnoff */ 546*09000cc1SGleb Smirnoff if (hcml->hc_mtu != 0) { 547*09000cc1SGleb Smirnoff atomic_store_32(&hc_entry->hc_mtu, hcml->hc_mtu); 5480c25bf7eSGleb Smirnoff } 549*09000cc1SGleb Smirnoff if (hcml->hc_rtt != 0) { 550*09000cc1SGleb Smirnoff if (hc_entry->hc_rtt == 0) 551*09000cc1SGleb Smirnoff v = hcml->hc_rtt; 55297d8d152SAndre Oppermann else 553*09000cc1SGleb Smirnoff v = ((uint64_t)hc_entry->hc_rtt + 554*09000cc1SGleb Smirnoff (uint64_t)hcml->hc_rtt) / 2; 555*09000cc1SGleb Smirnoff atomic_store_32(&hc_entry->hc_rtt, v); 55678b50714SRobert Watson TCPSTAT_INC(tcps_cachedrtt); 55797d8d152SAndre Oppermann } 558*09000cc1SGleb Smirnoff if (hcml->hc_rttvar != 0) { 559*09000cc1SGleb Smirnoff if (hc_entry->hc_rttvar == 0) 560*09000cc1SGleb Smirnoff v = hcml->hc_rttvar; 56197d8d152SAndre Oppermann else 562*09000cc1SGleb Smirnoff v = ((uint64_t)hc_entry->hc_rttvar + 563*09000cc1SGleb Smirnoff (uint64_t)hcml->hc_rttvar) / 2; 564*09000cc1SGleb Smirnoff atomic_store_32(&hc_entry->hc_rttvar, v); 56578b50714SRobert Watson TCPSTAT_INC(tcps_cachedrttvar); 56697d8d152SAndre Oppermann } 567*09000cc1SGleb Smirnoff if (hcml->hc_ssthresh != 0) { 568*09000cc1SGleb Smirnoff if (hc_entry->hc_ssthresh == 0) 569*09000cc1SGleb Smirnoff v = hcml->hc_ssthresh; 57097d8d152SAndre Oppermann else 571*09000cc1SGleb Smirnoff v = (hc_entry->hc_ssthresh + hcml->hc_ssthresh) / 2; 572*09000cc1SGleb Smirnoff atomic_store_32(&hc_entry->hc_ssthresh, v); 57378b50714SRobert Watson TCPSTAT_INC(tcps_cachedssthresh); 57497d8d152SAndre Oppermann } 575*09000cc1SGleb Smirnoff if (hcml->hc_cwnd != 0) { 576*09000cc1SGleb Smirnoff if (hc_entry->hc_cwnd == 0) 577*09000cc1SGleb Smirnoff v = hcml->hc_cwnd; 57897d8d152SAndre Oppermann else 579*09000cc1SGleb Smirnoff v = ((uint64_t)hc_entry->hc_cwnd + 580*09000cc1SGleb Smirnoff (uint64_t)hcml->hc_cwnd) / 2; 581*09000cc1SGleb Smirnoff atomic_store_32(&hc_entry->hc_cwnd, v); 58278b50714SRobert Watson /* TCPSTAT_INC(tcps_cachedcwnd); */ 58397d8d152SAndre Oppermann } 584*09000cc1SGleb Smirnoff if (hcml->hc_sendpipe != 0) { 585*09000cc1SGleb Smirnoff if (hc_entry->hc_sendpipe == 0) 586*09000cc1SGleb Smirnoff v = hcml->hc_sendpipe; 58797d8d152SAndre Oppermann else 588*09000cc1SGleb Smirnoff v = ((uint64_t)hc_entry->hc_sendpipe + 589*09000cc1SGleb Smirnoff (uint64_t)hcml->hc_sendpipe) /2; 590*09000cc1SGleb Smirnoff atomic_store_32(&hc_entry->hc_sendpipe, v); 59178b50714SRobert Watson /* TCPSTAT_INC(tcps_cachedsendpipe); */ 59297d8d152SAndre Oppermann } 593*09000cc1SGleb Smirnoff if (hcml->hc_recvpipe != 0) { 594*09000cc1SGleb Smirnoff if (hc_entry->hc_recvpipe == 0) 595*09000cc1SGleb Smirnoff v = hcml->hc_recvpipe; 59697d8d152SAndre Oppermann else 597*09000cc1SGleb Smirnoff v = ((uint64_t)hc_entry->hc_recvpipe + 598*09000cc1SGleb Smirnoff (uint64_t)hcml->hc_recvpipe) /2; 599*09000cc1SGleb Smirnoff atomic_store_32(&hc_entry->hc_recvpipe, v); 60078b50714SRobert Watson /* TCPSTAT_INC(tcps_cachedrecvpipe); */ 60197d8d152SAndre Oppermann } 60297d8d152SAndre Oppermann 603d554522fSGleb Smirnoff /* 604d554522fSGleb Smirnoff * Put it upfront. 605d554522fSGleb Smirnoff */ 606d554522fSGleb Smirnoff if (new) { 607*09000cc1SGleb Smirnoff CK_SLIST_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, hc_q); 608d554522fSGleb Smirnoff hc_head->hch_length++; 609d554522fSGleb Smirnoff KASSERT(hc_head->hch_length <= V_tcp_hostcache.bucket_limit, 610d554522fSGleb Smirnoff ("tcp_hostcache: bucket length too high at %p", hc_head)); 611d554522fSGleb Smirnoff atomic_add_int(&V_tcp_hostcache.cache_count, 1); 612d554522fSGleb Smirnoff TCPSTAT_INC(tcps_hc_added); 613d554522fSGleb Smirnoff } else if (hc_entry != CK_SLIST_FIRST(&hc_head->hch_bucket)) { 614*09000cc1SGleb Smirnoff KASSERT(CK_SLIST_NEXT(hc_prev, hc_q) == hc_entry, 615d554522fSGleb Smirnoff ("%s: %p next is not %p", __func__, hc_prev, hc_entry)); 616*09000cc1SGleb Smirnoff CK_SLIST_REMOVE_AFTER(hc_prev, hc_q); 617*09000cc1SGleb Smirnoff CK_SLIST_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, hc_q); 618d554522fSGleb Smirnoff } 619d554522fSGleb Smirnoff THC_UNLOCK(hc_head); 62097d8d152SAndre Oppermann } 62197d8d152SAndre Oppermann 62297d8d152SAndre Oppermann /* 62397d8d152SAndre Oppermann * Sysctl function: prints the list and values of all hostcache entries in 62497d8d152SAndre Oppermann * unsorted order. 62597d8d152SAndre Oppermann */ 62697d8d152SAndre Oppermann static int 62797d8d152SAndre Oppermann sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS) 62897d8d152SAndre Oppermann { 629751ccc42SIan Lepore const int linesize = 128; 630002d4558SJohn Baldwin struct sbuf sb; 631cb0dd7e1SRichard Scheffenegger int i, error, len; 63297d8d152SAndre Oppermann struct hc_metrics *hc_entry; 6338144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 6341d54aa3bSBjoern A. Zeeb #ifdef INET6 6351d54aa3bSBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 6361d54aa3bSBjoern A. Zeeb #endif 63797d8d152SAndre Oppermann 638b8a2fb91SHiren Panchasara if (jailed_without_vnet(curthread->td_ucred) != 0) 639b8a2fb91SHiren Panchasara return (EPERM); 640b8a2fb91SHiren Panchasara 641cb0dd7e1SRichard Scheffenegger /* Optimize Buffer length query by sbin/sysctl */ 642cb0dd7e1SRichard Scheffenegger if (req->oldptr == NULL) { 643529a2a0fSRichard Scheffenegger len = (atomic_load_int(&V_tcp_hostcache.cache_count) + 1) * 644529a2a0fSRichard Scheffenegger linesize; 645cb0dd7e1SRichard Scheffenegger return (SYSCTL_OUT(req, NULL, len)); 646cb0dd7e1SRichard Scheffenegger } 647cb0dd7e1SRichard Scheffenegger 648cb0dd7e1SRichard Scheffenegger error = sysctl_wire_old_buffer(req, 0); 649cb0dd7e1SRichard Scheffenegger if (error != 0) { 650cb0dd7e1SRichard Scheffenegger return(error); 651cb0dd7e1SRichard Scheffenegger } 652cb0dd7e1SRichard Scheffenegger 65386988046SRichard Scheffenegger /* Use a buffer sized for one full bucket */ 654529a2a0fSRichard Scheffenegger sbuf_new_for_sysctl(&sb, NULL, V_tcp_hostcache.bucket_limit * 655529a2a0fSRichard Scheffenegger linesize, req); 65697d8d152SAndre Oppermann 657002d4558SJohn Baldwin sbuf_printf(&sb, 6584d163382SHiren Panchasara "\nIP address MTU SSTRESH RTT RTTVAR " 659489bde57SGleb Smirnoff " CWND SENDPIPE RECVPIPE " 660489bde57SGleb Smirnoff #ifdef TCP_HC_COUNTERS 661489bde57SGleb Smirnoff "HITS UPD " 662489bde57SGleb Smirnoff #endif 663489bde57SGleb Smirnoff "EXP\n"); 66486988046SRichard Scheffenegger sbuf_drain(&sb); 66597d8d152SAndre Oppermann 66697d8d152SAndre Oppermann #define msec(u) (((u) + 500) / 1000) 667603724d3SBjoern A. Zeeb for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 6681a7fe55aSGleb Smirnoff THC_LOCK(&V_tcp_hostcache.hashbase[i]); 669d554522fSGleb Smirnoff CK_SLIST_FOREACH(hc_entry, 670*09000cc1SGleb Smirnoff &V_tcp_hostcache.hashbase[i].hch_bucket, hc_q) { 671002d4558SJohn Baldwin sbuf_printf(&sb, 672489bde57SGleb Smirnoff "%-15s %5u %8u %6lums %6lums %8u %8u %8u " 673489bde57SGleb Smirnoff #ifdef TCP_HC_COUNTERS 674489bde57SGleb Smirnoff "%4lu %4lu " 675489bde57SGleb Smirnoff #endif 676489bde57SGleb Smirnoff "%4i\n", 6778144690aSEric van Gyzen hc_entry->ip4.s_addr ? 6788144690aSEric van Gyzen inet_ntoa_r(hc_entry->ip4, ip4buf) : 67997d8d152SAndre Oppermann #ifdef INET6 6801d54aa3bSBjoern A. Zeeb ip6_sprintf(ip6buf, &hc_entry->ip6), 68197d8d152SAndre Oppermann #else 68297d8d152SAndre Oppermann "IPv6?", 68397d8d152SAndre Oppermann #endif 684*09000cc1SGleb Smirnoff hc_entry->hc_mtu, 685*09000cc1SGleb Smirnoff hc_entry->hc_ssthresh, 686*09000cc1SGleb Smirnoff msec((u_long)hc_entry->hc_rtt * 68797d8d152SAndre Oppermann (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))), 688*09000cc1SGleb Smirnoff msec((u_long)hc_entry->hc_rttvar * 6893a288e90SMikolaj Golub (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))), 690*09000cc1SGleb Smirnoff hc_entry->hc_cwnd, 691*09000cc1SGleb Smirnoff hc_entry->hc_sendpipe, 692*09000cc1SGleb Smirnoff hc_entry->hc_recvpipe, 693489bde57SGleb Smirnoff #ifdef TCP_HC_COUNTERS 694*09000cc1SGleb Smirnoff hc_entry->hc_hits, 695*09000cc1SGleb Smirnoff hc_entry->hc_updates, 696489bde57SGleb Smirnoff #endif 697*09000cc1SGleb Smirnoff hc_entry->hc_expire); 69897d8d152SAndre Oppermann } 6991a7fe55aSGleb Smirnoff THC_UNLOCK(&V_tcp_hostcache.hashbase[i]); 70086988046SRichard Scheffenegger sbuf_drain(&sb); 7019aef4e7cSRichard Scheffenegger } 70297d8d152SAndre Oppermann #undef msec 703751ccc42SIan Lepore error = sbuf_finish(&sb); 704002d4558SJohn Baldwin sbuf_delete(&sb); 70597d8d152SAndre Oppermann return(error); 70697d8d152SAndre Oppermann } 70797d8d152SAndre Oppermann 70897d8d152SAndre Oppermann /* 70902f26e98SRichard Scheffenegger * Sysctl function: prints a histogram of the hostcache hashbucket 71002f26e98SRichard Scheffenegger * utilization. 71102f26e98SRichard Scheffenegger */ 71202f26e98SRichard Scheffenegger static int 71302f26e98SRichard Scheffenegger sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS) 71402f26e98SRichard Scheffenegger { 71502f26e98SRichard Scheffenegger const int linesize = 50; 71602f26e98SRichard Scheffenegger struct sbuf sb; 71702f26e98SRichard Scheffenegger int i, error; 71802f26e98SRichard Scheffenegger int *histo; 71902f26e98SRichard Scheffenegger u_int hch_length; 72002f26e98SRichard Scheffenegger 72102f26e98SRichard Scheffenegger if (jailed_without_vnet(curthread->td_ucred) != 0) 72202f26e98SRichard Scheffenegger return (EPERM); 72302f26e98SRichard Scheffenegger 72402f26e98SRichard Scheffenegger histo = (int *)malloc(sizeof(int) * (V_tcp_hostcache.bucket_limit + 1), 72502f26e98SRichard Scheffenegger M_TEMP, M_NOWAIT|M_ZERO); 72602f26e98SRichard Scheffenegger if (histo == NULL) 72702f26e98SRichard Scheffenegger return(ENOMEM); 72802f26e98SRichard Scheffenegger 72902f26e98SRichard Scheffenegger for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 73002f26e98SRichard Scheffenegger hch_length = V_tcp_hostcache.hashbase[i].hch_length; 73102f26e98SRichard Scheffenegger KASSERT(hch_length <= V_tcp_hostcache.bucket_limit, 73202f26e98SRichard Scheffenegger ("tcp_hostcache: bucket limit exceeded at %u: %u", 73302f26e98SRichard Scheffenegger i, hch_length)); 73402f26e98SRichard Scheffenegger histo[hch_length]++; 73502f26e98SRichard Scheffenegger } 73602f26e98SRichard Scheffenegger 73702f26e98SRichard Scheffenegger /* Use a buffer for 16 lines */ 73802f26e98SRichard Scheffenegger sbuf_new_for_sysctl(&sb, NULL, 16 * linesize, req); 73902f26e98SRichard Scheffenegger 74002f26e98SRichard Scheffenegger sbuf_printf(&sb, "\nLength\tCount\n"); 74102f26e98SRichard Scheffenegger for (i = 0; i <= V_tcp_hostcache.bucket_limit; i++) { 74202f26e98SRichard Scheffenegger sbuf_printf(&sb, "%u\t%u\n", i, histo[i]); 74302f26e98SRichard Scheffenegger } 74402f26e98SRichard Scheffenegger error = sbuf_finish(&sb); 74502f26e98SRichard Scheffenegger sbuf_delete(&sb); 74602f26e98SRichard Scheffenegger free(histo, M_TEMP); 74702f26e98SRichard Scheffenegger return(error); 74802f26e98SRichard Scheffenegger } 74902f26e98SRichard Scheffenegger 75002f26e98SRichard Scheffenegger /* 751fffb9f1dSBjoern A. Zeeb * Caller has to make sure the curvnet is set properly. 75297d8d152SAndre Oppermann */ 75397d8d152SAndre Oppermann static void 754fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(int all) 75597d8d152SAndre Oppermann { 756d554522fSGleb Smirnoff struct hc_head *head; 757d554522fSGleb Smirnoff struct hc_metrics *hc_entry, *hc_next, *hc_prev; 75897d8d152SAndre Oppermann int i; 75997d8d152SAndre Oppermann 760603724d3SBjoern A. Zeeb for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 761d554522fSGleb Smirnoff head = &V_tcp_hostcache.hashbase[i]; 762d554522fSGleb Smirnoff hc_prev = NULL; 763d554522fSGleb Smirnoff THC_LOCK(head); 764*09000cc1SGleb Smirnoff CK_SLIST_FOREACH_SAFE(hc_entry, &head->hch_bucket, hc_q, 765d554522fSGleb Smirnoff hc_next) { 766d554522fSGleb Smirnoff KASSERT(head->hch_length > 0 && head->hch_length <= 7674f49e338SGleb Smirnoff V_tcp_hostcache.bucket_limit, ("tcp_hostcache: " 768d554522fSGleb Smirnoff "bucket length out of range at %u: %u", i, 769d554522fSGleb Smirnoff head->hch_length)); 770d554522fSGleb Smirnoff if (all || 771*09000cc1SGleb Smirnoff atomic_load_int(&hc_entry->hc_expire) <= 0) { 772d554522fSGleb Smirnoff if (hc_prev != NULL) { 773d554522fSGleb Smirnoff KASSERT(hc_entry == 774*09000cc1SGleb Smirnoff CK_SLIST_NEXT(hc_prev, hc_q), 775d554522fSGleb Smirnoff ("%s: %p is not next to %p", 776d554522fSGleb Smirnoff __func__, hc_entry, hc_prev)); 777*09000cc1SGleb Smirnoff CK_SLIST_REMOVE_AFTER(hc_prev, hc_q); 778d554522fSGleb Smirnoff } else { 779d554522fSGleb Smirnoff KASSERT(hc_entry == 780d554522fSGleb Smirnoff CK_SLIST_FIRST(&head->hch_bucket), 781d554522fSGleb Smirnoff ("%s: %p is not first", 782d554522fSGleb Smirnoff __func__, hc_entry)); 783d554522fSGleb Smirnoff CK_SLIST_REMOVE_HEAD(&head->hch_bucket, 784*09000cc1SGleb Smirnoff hc_q); 78597d8d152SAndre Oppermann } 786d554522fSGleb Smirnoff uma_zfree_smr(V_tcp_hostcache.zone, hc_entry); 787d554522fSGleb Smirnoff head->hch_length--; 788d554522fSGleb Smirnoff atomic_subtract_int(&V_tcp_hostcache.cache_count, 1); 789d554522fSGleb Smirnoff } else { 790*09000cc1SGleb Smirnoff atomic_subtract_int(&hc_entry->hc_expire, 791d554522fSGleb Smirnoff V_tcp_hostcache.prune); 792d554522fSGleb Smirnoff hc_prev = hc_entry; 793d554522fSGleb Smirnoff } 794d554522fSGleb Smirnoff } 795d554522fSGleb Smirnoff THC_UNLOCK(head); 79697d8d152SAndre Oppermann } 797fffb9f1dSBjoern A. Zeeb } 798fffb9f1dSBjoern A. Zeeb 799fffb9f1dSBjoern A. Zeeb /* 800fffb9f1dSBjoern A. Zeeb * Expire and purge (old|all) entries in the tcp_hostcache. Runs 801fffb9f1dSBjoern A. Zeeb * periodically from the callout. 802fffb9f1dSBjoern A. Zeeb */ 803fffb9f1dSBjoern A. Zeeb static void 804fffb9f1dSBjoern A. Zeeb tcp_hc_purge(void *arg) 805fffb9f1dSBjoern A. Zeeb { 806fffb9f1dSBjoern A. Zeeb CURVNET_SET((struct vnet *) arg); 807fffb9f1dSBjoern A. Zeeb int all = 0; 808fffb9f1dSBjoern A. Zeeb 809fffb9f1dSBjoern A. Zeeb if (V_tcp_hostcache.purgeall) { 810b878ec02SRichard Scheffenegger if (V_tcp_hostcache.purgeall == 2) 811b878ec02SRichard Scheffenegger V_tcp_hostcache.hashsalt = arc4random(); 812fffb9f1dSBjoern A. Zeeb all = 1; 813fffb9f1dSBjoern A. Zeeb V_tcp_hostcache.purgeall = 0; 814fffb9f1dSBjoern A. Zeeb } 815fffb9f1dSBjoern A. Zeeb 816fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(all); 817ac957cd2SJulian Elischer 818ac957cd2SJulian Elischer callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 819ac957cd2SJulian Elischer tcp_hc_purge, arg); 82021ca7b57SMarko Zec CURVNET_RESTORE(); 82197d8d152SAndre Oppermann } 82226456380SHiren Panchasara 82326456380SHiren Panchasara /* 82426456380SHiren Panchasara * Expire and purge all entries in hostcache immediately. 82526456380SHiren Panchasara */ 82626456380SHiren Panchasara static int 82726456380SHiren Panchasara sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS) 82826456380SHiren Panchasara { 82926456380SHiren Panchasara int error, val; 83026456380SHiren Panchasara 83126456380SHiren Panchasara val = 0; 83226456380SHiren Panchasara error = sysctl_handle_int(oidp, &val, 0, req); 83326456380SHiren Panchasara if (error || !req->newptr) 83426456380SHiren Panchasara return (error); 83526456380SHiren Panchasara 836b878ec02SRichard Scheffenegger if (val == 2) 837b878ec02SRichard Scheffenegger V_tcp_hostcache.hashsalt = arc4random(); 83826456380SHiren Panchasara tcp_hc_purge_internal(1); 83926456380SHiren Panchasara 84026456380SHiren Panchasara callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 84126456380SHiren Panchasara tcp_hc_purge, curvnet); 84226456380SHiren Panchasara 84326456380SHiren Panchasara return (0); 84426456380SHiren Panchasara } 845