1139823Simp/*- 2122922Sandre * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG 3122922Sandre * All rights reserved. 4122922Sandre * 5122922Sandre * Redistribution and use in source and binary forms, with or without 6122922Sandre * modification, are permitted provided that the following conditions 7122922Sandre * are met: 8122922Sandre * 1. Redistributions of source code must retain the above copyright 9122922Sandre * notice, this list of conditions and the following disclaimer. 10122922Sandre * 2. Redistributions in binary form must reproduce the above copyright 11122922Sandre * notice, this list of conditions and the following disclaimer in the 12122922Sandre * documentation and/or other materials provided with the distribution. 13122922Sandre * 3. The name of the author may not be used to endorse or promote 14122922Sandre * products derived from this software without specific prior written 15122922Sandre * permission. 16122922Sandre * 17122922Sandre * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18122922Sandre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19122922Sandre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20122922Sandre * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21122922Sandre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22122922Sandre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23122922Sandre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24122922Sandre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25122922Sandre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26122922Sandre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27122922Sandre * SUCH DAMAGE. 28122922Sandre */ 29122922Sandre 30122922Sandre/* 31170030Srwatson * The tcp_hostcache moves the tcp-specific cached metrics from the routing 32170030Srwatson * table to a dedicated structure indexed by the remote IP address. It keeps 33170030Srwatson * information on the measured TCP parameters of past TCP sessions to allow 34170030Srwatson * better initial start values to be used with later connections to/from the 35170030Srwatson * same source. Depending on the network parameters (delay, bandwidth, max 36170030Srwatson * MTU, congestion window) between local and remote sites, this can lead to 37170030Srwatson * significant speed-ups for new TCP connections after the first one. 38122922Sandre * 39170030Srwatson * Due to the tcp_hostcache, all TCP-specific metrics information in the 40182411Srpaulo * routing table have been removed. The inpcb no longer keeps a pointer to 41170030Srwatson * the routing entry, and protocol-initiated route cloning has been removed 42170030Srwatson * as well. With these changes, the routing table has gone back to being 43170030Srwatson * more lightwight and only carries information related to packet forwarding. 44122922Sandre * 45170030Srwatson * tcp_hostcache is designed for multiple concurrent access in SMP 46170030Srwatson * environments and high contention. All bucket rows have their own lock and 47170030Srwatson * thus multiple lookups and modifies can be done at the same time as long as 48170030Srwatson * they are in different bucket rows. If a request for insertion of a new 49170030Srwatson * record can't be satisfied, it simply returns an empty structure. Nobody 50170030Srwatson * and nothing outside of tcp_hostcache.c will ever point directly to any 51170030Srwatson * entry in the tcp_hostcache. All communication is done in an 52170030Srwatson * object-oriented way and only functions of tcp_hostcache will manipulate 53170030Srwatson * hostcache entries. Otherwise, we are unable to achieve good behaviour in 54170030Srwatson * concurrent access situations. Since tcp_hostcache is only caching 55170030Srwatson * information, there are no fatal consequences if we either can't satisfy 56170030Srwatson * any particular request or have to drop/overwrite an existing entry because 57170030Srwatson * of bucket limit memory constrains. 58122922Sandre */ 59122922Sandre 60122922Sandre/* 61122922Sandre * Many thanks to jlemon for basic structure of tcp_syncache which is being 62122922Sandre * followed here. 63122922Sandre */ 64122922Sandre 65172467Ssilby#include <sys/cdefs.h> 66172467Ssilby__FBSDID("$FreeBSD: stable/10/sys/netinet/tcp_hostcache.c 314667 2017-03-04 13:03:31Z avg $"); 67172467Ssilby 68122922Sandre#include "opt_inet6.h" 69122922Sandre 70122922Sandre#include <sys/param.h> 71122922Sandre#include <sys/systm.h> 72122922Sandre#include <sys/kernel.h> 73122922Sandre#include <sys/lock.h> 74122922Sandre#include <sys/mutex.h> 75122922Sandre#include <sys/malloc.h> 76278534Sjhb#include <sys/sbuf.h> 77122922Sandre#include <sys/socket.h> 78122922Sandre#include <sys/socketvar.h> 79122922Sandre#include <sys/sysctl.h> 80122922Sandre 81122922Sandre#include <net/if.h> 82194739Sbz#include <net/route.h> 83196019Srwatson#include <net/vnet.h> 84122922Sandre 85122922Sandre#include <netinet/in.h> 86122922Sandre#include <netinet/in_systm.h> 87122922Sandre#include <netinet/ip.h> 88122922Sandre#include <netinet/in_var.h> 89122922Sandre#include <netinet/in_pcb.h> 90122922Sandre#include <netinet/ip_var.h> 91122922Sandre#ifdef INET6 92122922Sandre#include <netinet/ip6.h> 93122922Sandre#include <netinet6/ip6_var.h> 94122922Sandre#endif 95122922Sandre#include <netinet/tcp.h> 96122922Sandre#include <netinet/tcp_var.h> 97185571Sbz#include <netinet/tcp_hostcache.h> 98122922Sandre#ifdef INET6 99122922Sandre#include <netinet6/tcp6_var.h> 100122922Sandre#endif 101122922Sandre 102122922Sandre#include <vm/uma.h> 103122922Sandre 104122922Sandre/* Arbitrary values */ 105122922Sandre#define TCP_HOSTCACHE_HASHSIZE 512 106122922Sandre#define TCP_HOSTCACHE_BUCKETLIMIT 30 107122922Sandre#define TCP_HOSTCACHE_EXPIRE 60*60 /* one hour */ 108122922Sandre#define TCP_HOSTCACHE_PRUNE 5*60 /* every 5 minutes */ 109122922Sandre 110215701Sdimstatic VNET_DEFINE(struct tcp_hostcache, tcp_hostcache); 111207369Sbz#define V_tcp_hostcache VNET(tcp_hostcache) 112207369Sbz 113215701Sdimstatic VNET_DEFINE(struct callout, tcp_hc_callout); 114195727Srwatson#define V_tcp_hc_callout VNET(tcp_hc_callout) 115195699Srwatson 116122922Sandrestatic struct hc_metrics *tcp_hc_lookup(struct in_conninfo *); 117122922Sandrestatic struct hc_metrics *tcp_hc_insert(struct in_conninfo *); 118122922Sandrestatic int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS); 119203724Sbzstatic void tcp_hc_purge_internal(int); 120122922Sandrestatic void tcp_hc_purge(void *); 121122922Sandre 122227309Sedstatic SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0, 123182633Sbrooks "TCP Host cache"); 124122922Sandre 125217322SmdfSYSCTL_VNET_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_RDTUN, 126195699Srwatson &VNET_NAME(tcp_hostcache.cache_limit), 0, 127183550Szec "Overall entry limit for hostcache"); 128122922Sandre 129217322SmdfSYSCTL_VNET_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_RDTUN, 130195699Srwatson &VNET_NAME(tcp_hostcache.hashsize), 0, 131183550Szec "Size of TCP hostcache hashtable"); 132122922Sandre 133217322SmdfSYSCTL_VNET_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, 134195699Srwatson CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0, 135183550Szec "Per-bucket hash limit for hostcache"); 136122922Sandre 137217322SmdfSYSCTL_VNET_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_RD, 138195699Srwatson &VNET_NAME(tcp_hostcache.cache_count), 0, 139183550Szec "Current number of entries in hostcache"); 140122922Sandre 141195699SrwatsonSYSCTL_VNET_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_RW, 142195699Srwatson &VNET_NAME(tcp_hostcache.expire), 0, 143183550Szec "Expire time of TCP hostcache entries"); 144122922Sandre 145195699SrwatsonSYSCTL_VNET_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_RW, 146195699Srwatson &VNET_NAME(tcp_hostcache.prune), 0, 147195699Srwatson "Time between purge runs"); 148170434Syar 149195699SrwatsonSYSCTL_VNET_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_RW, 150195699Srwatson &VNET_NAME(tcp_hostcache.purgeall), 0, 151183550Szec "Expire all entires on next purge run"); 152122922Sandre 153122922SandreSYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list, 154167784Sandre CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0, 155167784Sandre sysctl_tcp_hc_list, "A", "List of all hostcache entries"); 156122922Sandre 157122922Sandre 158122922Sandrestatic MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache"); 159122922Sandre 160122922Sandre#define HOSTCACHE_HASH(ip) \ 161133874Srwatson (((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) & \ 162181803Sbz V_tcp_hostcache.hashmask) 163122922Sandre 164122922Sandre/* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */ 165133874Srwatson#define HOSTCACHE_HASH6(ip6) \ 166122922Sandre (((ip6)->s6_addr32[0] ^ \ 167122922Sandre (ip6)->s6_addr32[1] ^ \ 168122922Sandre (ip6)->s6_addr32[2] ^ \ 169122922Sandre (ip6)->s6_addr32[3]) & \ 170181803Sbz V_tcp_hostcache.hashmask) 171122922Sandre 172122922Sandre#define THC_LOCK(lp) mtx_lock(lp) 173122922Sandre#define THC_UNLOCK(lp) mtx_unlock(lp) 174122922Sandre 175122922Sandrevoid 176122922Sandretcp_hc_init(void) 177122922Sandre{ 178241735Szont u_int cache_limit; 179122922Sandre int i; 180122922Sandre 181122922Sandre /* 182170030Srwatson * Initialize hostcache structures. 183122922Sandre */ 184181803Sbz V_tcp_hostcache.cache_count = 0; 185181803Sbz V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; 186181803Sbz V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT; 187181803Sbz V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE; 188181803Sbz V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE; 189122922Sandre 190133874Srwatson TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize", 191181803Sbz &V_tcp_hostcache.hashsize); 192181803Sbz if (!powerof2(V_tcp_hostcache.hashsize)) { 193133874Srwatson printf("WARNING: hostcache hash size is not a power of 2.\n"); 194181803Sbz V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */ 195133874Srwatson } 196181803Sbz V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1; 197122922Sandre 198241735Szont TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit", 199241735Szont &V_tcp_hostcache.bucket_limit); 200241735Szont 201241735Szont cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit; 202241735Szont V_tcp_hostcache.cache_limit = cache_limit; 203241735Szont TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit", 204241735Szont &V_tcp_hostcache.cache_limit); 205241735Szont if (V_tcp_hostcache.cache_limit > cache_limit) 206241735Szont V_tcp_hostcache.cache_limit = cache_limit; 207241735Szont 208122922Sandre /* 209170030Srwatson * Allocate the hash table. 210122922Sandre */ 211181803Sbz V_tcp_hostcache.hashbase = (struct hc_head *) 212181803Sbz malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head), 213122922Sandre M_HOSTCACHE, M_WAITOK | M_ZERO); 214122922Sandre 215122922Sandre /* 216170030Srwatson * Initialize the hash buckets. 217122922Sandre */ 218181803Sbz for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 219181803Sbz TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket); 220181803Sbz V_tcp_hostcache.hashbase[i].hch_length = 0; 221181803Sbz mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry", 222122922Sandre NULL, MTX_DEF); 223122922Sandre } 224122922Sandre 225122922Sandre /* 226122922Sandre * Allocate the hostcache entries. 227122922Sandre */ 228181887Sjulian V_tcp_hostcache.zone = 229181888Sjulian uma_zcreate("hostcache", sizeof(struct hc_metrics), 230181888Sjulian NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 231181803Sbz uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit); 232122922Sandre 233122922Sandre /* 234122922Sandre * Set up periodic cache cleanup. 235122922Sandre */ 236314667Savg callout_init(&V_tcp_hc_callout, 1); 237181887Sjulian callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 238191816Szec tcp_hc_purge, curvnet); 239122922Sandre} 240122922Sandre 241193731Szec#ifdef VIMAGE 242193731Szecvoid 243193731Szectcp_hc_destroy(void) 244193731Szec{ 245203724Sbz int i; 246193731Szec 247203724Sbz callout_drain(&V_tcp_hc_callout); 248193731Szec 249203724Sbz /* Purge all hc entries. */ 250203724Sbz tcp_hc_purge_internal(1); 251203724Sbz 252203724Sbz /* Free the uma zone and the allocated hash table. */ 253203724Sbz uma_zdestroy(V_tcp_hostcache.zone); 254203724Sbz 255203724Sbz for (i = 0; i < V_tcp_hostcache.hashsize; i++) 256203724Sbz mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx); 257203724Sbz free(V_tcp_hostcache.hashbase, M_HOSTCACHE); 258193731Szec} 259193731Szec#endif 260193731Szec 261122922Sandre/* 262170030Srwatson * Internal function: look up an entry in the hostcache or return NULL. 263122922Sandre * 264122922Sandre * If an entry has been returned, the caller becomes responsible for 265122922Sandre * unlocking the bucket row after he is done reading/modifying the entry. 266122922Sandre */ 267122922Sandrestatic struct hc_metrics * 268122922Sandretcp_hc_lookup(struct in_conninfo *inc) 269122922Sandre{ 270122922Sandre int hash; 271122922Sandre struct hc_head *hc_head; 272122922Sandre struct hc_metrics *hc_entry; 273122922Sandre 274122922Sandre KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer")); 275122922Sandre 276122922Sandre /* 277122922Sandre * Hash the foreign ip address. 278122922Sandre */ 279186222Sbz if (inc->inc_flags & INC_ISIPV6) 280122922Sandre hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 281122922Sandre else 282122922Sandre hash = HOSTCACHE_HASH(&inc->inc_faddr); 283122922Sandre 284181803Sbz hc_head = &V_tcp_hostcache.hashbase[hash]; 285122922Sandre 286122922Sandre /* 287170030Srwatson * Acquire lock for this bucket row; we release the lock if we don't 288170030Srwatson * find an entry, otherwise the caller has to unlock after he is 289170030Srwatson * done. 290122922Sandre */ 291122922Sandre THC_LOCK(&hc_head->hch_mtx); 292122922Sandre 293122922Sandre /* 294170030Srwatson * Iterate through entries in bucket row looking for a match. 295122922Sandre */ 296122922Sandre TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) { 297186222Sbz if (inc->inc_flags & INC_ISIPV6) { 298122922Sandre if (memcmp(&inc->inc6_faddr, &hc_entry->ip6, 299122922Sandre sizeof(inc->inc6_faddr)) == 0) 300122922Sandre return hc_entry; 301122922Sandre } else { 302122922Sandre if (memcmp(&inc->inc_faddr, &hc_entry->ip4, 303122922Sandre sizeof(inc->inc_faddr)) == 0) 304122922Sandre return hc_entry; 305122922Sandre } 306122922Sandre } 307122922Sandre 308122922Sandre /* 309170030Srwatson * We were unsuccessful and didn't find anything. 310122922Sandre */ 311122922Sandre THC_UNLOCK(&hc_head->hch_mtx); 312122922Sandre return NULL; 313122922Sandre} 314122922Sandre 315122922Sandre/* 316170030Srwatson * Internal function: insert an entry into the hostcache or return NULL if 317170030Srwatson * unable to allocate a new one. 318133874Srwatson * 319122922Sandre * If an entry has been returned, the caller becomes responsible for 320122922Sandre * unlocking the bucket row after he is done reading/modifying the entry. 321122922Sandre */ 322122922Sandrestatic struct hc_metrics * 323122922Sandretcp_hc_insert(struct in_conninfo *inc) 324122922Sandre{ 325122922Sandre int hash; 326122922Sandre struct hc_head *hc_head; 327122922Sandre struct hc_metrics *hc_entry; 328122922Sandre 329122922Sandre KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer")); 330122922Sandre 331122922Sandre /* 332170030Srwatson * Hash the foreign ip address. 333122922Sandre */ 334186222Sbz if (inc->inc_flags & INC_ISIPV6) 335122922Sandre hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 336122922Sandre else 337122922Sandre hash = HOSTCACHE_HASH(&inc->inc_faddr); 338122922Sandre 339181803Sbz hc_head = &V_tcp_hostcache.hashbase[hash]; 340122922Sandre 341122922Sandre /* 342170030Srwatson * Acquire lock for this bucket row; we release the lock if we don't 343170030Srwatson * find an entry, otherwise the caller has to unlock after he is 344170030Srwatson * done. 345122922Sandre */ 346122922Sandre THC_LOCK(&hc_head->hch_mtx); 347122922Sandre 348122922Sandre /* 349170030Srwatson * If the bucket limit is reached, reuse the least-used element. 350122922Sandre */ 351181803Sbz if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit || 352181803Sbz V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) { 353122922Sandre hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead); 354122922Sandre /* 355122922Sandre * At first we were dropping the last element, just to 356170030Srwatson * reacquire it in the next two lines again, which isn't very 357170030Srwatson * efficient. Instead just reuse the least used element. 358170030Srwatson * We may drop something that is still "in-use" but we can be 359170030Srwatson * "lossy". 360170405Sandre * Just give up if this bucket row is empty and we don't have 361170405Sandre * anything to replace. 362122922Sandre */ 363170405Sandre if (hc_entry == NULL) { 364170405Sandre THC_UNLOCK(&hc_head->hch_mtx); 365170405Sandre return NULL; 366170405Sandre } 367122922Sandre TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q); 368181803Sbz V_tcp_hostcache.hashbase[hash].hch_length--; 369181803Sbz V_tcp_hostcache.cache_count--; 370190948Srwatson TCPSTAT_INC(tcps_hc_bucketoverflow); 371123028Sandre#if 0 372181803Sbz uma_zfree(V_tcp_hostcache.zone, hc_entry); 373122922Sandre#endif 374122922Sandre } else { 375122922Sandre /* 376170030Srwatson * Allocate a new entry, or balk if not possible. 377122922Sandre */ 378181803Sbz hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT); 379122922Sandre if (hc_entry == NULL) { 380122922Sandre THC_UNLOCK(&hc_head->hch_mtx); 381122922Sandre return NULL; 382122922Sandre } 383122922Sandre } 384122922Sandre 385122922Sandre /* 386170030Srwatson * Initialize basic information of hostcache entry. 387122922Sandre */ 388122922Sandre bzero(hc_entry, sizeof(*hc_entry)); 389186222Sbz if (inc->inc_flags & INC_ISIPV6) 390123113Sandre bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6)); 391122922Sandre else 392122922Sandre hc_entry->ip4 = inc->inc_faddr; 393122922Sandre hc_entry->rmx_head = hc_head; 394181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; 395122922Sandre 396122922Sandre /* 397170030Srwatson * Put it upfront. 398122922Sandre */ 399122922Sandre TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q); 400181803Sbz V_tcp_hostcache.hashbase[hash].hch_length++; 401181803Sbz V_tcp_hostcache.cache_count++; 402190948Srwatson TCPSTAT_INC(tcps_hc_added); 403122922Sandre 404122922Sandre return hc_entry; 405122922Sandre} 406122922Sandre 407122922Sandre/* 408170030Srwatson * External function: look up an entry in the hostcache and fill out the 409170030Srwatson * supplied TCP metrics structure. Fills in NULL when no entry was found or 410170030Srwatson * a value is not set. 411122922Sandre */ 412122922Sandrevoid 413122922Sandretcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite) 414122922Sandre{ 415122922Sandre struct hc_metrics *hc_entry; 416122922Sandre 417122922Sandre /* 418170030Srwatson * Find the right bucket. 419122922Sandre */ 420122922Sandre hc_entry = tcp_hc_lookup(inc); 421122922Sandre 422122922Sandre /* 423170030Srwatson * If we don't have an existing object. 424122922Sandre */ 425122922Sandre if (hc_entry == NULL) { 426122922Sandre bzero(hc_metrics_lite, sizeof(*hc_metrics_lite)); 427122922Sandre return; 428122922Sandre } 429122922Sandre hc_entry->rmx_hits++; 430181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 431122922Sandre 432122922Sandre hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu; 433122922Sandre hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh; 434122922Sandre hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt; 435122922Sandre hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar; 436122922Sandre hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth; 437122922Sandre hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd; 438122922Sandre hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe; 439122922Sandre hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe; 440122922Sandre 441122922Sandre /* 442170030Srwatson * Unlock bucket row. 443122922Sandre */ 444122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 445122922Sandre} 446122922Sandre 447122922Sandre/* 448170030Srwatson * External function: look up an entry in the hostcache and return the 449170030Srwatson * discovered path MTU. Returns NULL if no entry is found or value is not 450138409Srwatson * set. 451122922Sandre */ 452122922Sandreu_long 453122922Sandretcp_hc_getmtu(struct in_conninfo *inc) 454122922Sandre{ 455122922Sandre struct hc_metrics *hc_entry; 456122922Sandre u_long mtu; 457122922Sandre 458122922Sandre hc_entry = tcp_hc_lookup(inc); 459122922Sandre if (hc_entry == NULL) { 460122922Sandre return 0; 461122922Sandre } 462122922Sandre hc_entry->rmx_hits++; 463181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 464122922Sandre 465122922Sandre mtu = hc_entry->rmx_mtu; 466122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 467122922Sandre return mtu; 468122922Sandre} 469122922Sandre 470122922Sandre/* 471170030Srwatson * External function: update the MTU value of an entry in the hostcache. 472122922Sandre * Creates a new entry if none was found. 473122922Sandre */ 474122922Sandrevoid 475122922Sandretcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu) 476122922Sandre{ 477122922Sandre struct hc_metrics *hc_entry; 478122922Sandre 479122922Sandre /* 480170030Srwatson * Find the right bucket. 481122922Sandre */ 482122922Sandre hc_entry = tcp_hc_lookup(inc); 483122922Sandre 484122922Sandre /* 485170030Srwatson * If we don't have an existing object, try to insert a new one. 486122922Sandre */ 487122922Sandre if (hc_entry == NULL) { 488122922Sandre hc_entry = tcp_hc_insert(inc); 489122922Sandre if (hc_entry == NULL) 490122922Sandre return; 491122922Sandre } 492122922Sandre hc_entry->rmx_updates++; 493181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 494122922Sandre 495122922Sandre hc_entry->rmx_mtu = mtu; 496122922Sandre 497122922Sandre /* 498170030Srwatson * Put it upfront so we find it faster next time. 499122922Sandre */ 500122922Sandre TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 501122922Sandre TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 502122922Sandre 503122922Sandre /* 504170030Srwatson * Unlock bucket row. 505122922Sandre */ 506122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 507122922Sandre} 508122922Sandre 509122922Sandre/* 510170030Srwatson * External function: update the TCP metrics of an entry in the hostcache. 511122922Sandre * Creates a new entry if none was found. 512122922Sandre */ 513122922Sandrevoid 514122922Sandretcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml) 515122922Sandre{ 516122922Sandre struct hc_metrics *hc_entry; 517122922Sandre 518122922Sandre hc_entry = tcp_hc_lookup(inc); 519122922Sandre if (hc_entry == NULL) { 520122922Sandre hc_entry = tcp_hc_insert(inc); 521122922Sandre if (hc_entry == NULL) 522122922Sandre return; 523122922Sandre } 524122922Sandre hc_entry->rmx_updates++; 525181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 526122922Sandre 527122922Sandre if (hcml->rmx_rtt != 0) { 528122922Sandre if (hc_entry->rmx_rtt == 0) 529122922Sandre hc_entry->rmx_rtt = hcml->rmx_rtt; 530122922Sandre else 531122922Sandre hc_entry->rmx_rtt = 532122922Sandre (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2; 533190948Srwatson TCPSTAT_INC(tcps_cachedrtt); 534122922Sandre } 535122922Sandre if (hcml->rmx_rttvar != 0) { 536122922Sandre if (hc_entry->rmx_rttvar == 0) 537133874Srwatson hc_entry->rmx_rttvar = hcml->rmx_rttvar; 538122922Sandre else 539122922Sandre hc_entry->rmx_rttvar = 540122922Sandre (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2; 541190948Srwatson TCPSTAT_INC(tcps_cachedrttvar); 542122922Sandre } 543122922Sandre if (hcml->rmx_ssthresh != 0) { 544122922Sandre if (hc_entry->rmx_ssthresh == 0) 545122922Sandre hc_entry->rmx_ssthresh = hcml->rmx_ssthresh; 546122922Sandre else 547122922Sandre hc_entry->rmx_ssthresh = 548122922Sandre (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2; 549190948Srwatson TCPSTAT_INC(tcps_cachedssthresh); 550122922Sandre } 551122922Sandre if (hcml->rmx_bandwidth != 0) { 552122922Sandre if (hc_entry->rmx_bandwidth == 0) 553122922Sandre hc_entry->rmx_bandwidth = hcml->rmx_bandwidth; 554122922Sandre else 555122922Sandre hc_entry->rmx_bandwidth = 556122922Sandre (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2; 557190948Srwatson /* TCPSTAT_INC(tcps_cachedbandwidth); */ 558122922Sandre } 559122922Sandre if (hcml->rmx_cwnd != 0) { 560122922Sandre if (hc_entry->rmx_cwnd == 0) 561122922Sandre hc_entry->rmx_cwnd = hcml->rmx_cwnd; 562122922Sandre else 563122922Sandre hc_entry->rmx_cwnd = 564122922Sandre (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2; 565190948Srwatson /* TCPSTAT_INC(tcps_cachedcwnd); */ 566122922Sandre } 567122922Sandre if (hcml->rmx_sendpipe != 0) { 568122922Sandre if (hc_entry->rmx_sendpipe == 0) 569122922Sandre hc_entry->rmx_sendpipe = hcml->rmx_sendpipe; 570122922Sandre else 571122922Sandre hc_entry->rmx_sendpipe = 572122922Sandre (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2; 573190948Srwatson /* TCPSTAT_INC(tcps_cachedsendpipe); */ 574133874Srwatson } 575122922Sandre if (hcml->rmx_recvpipe != 0) { 576122922Sandre if (hc_entry->rmx_recvpipe == 0) 577122922Sandre hc_entry->rmx_recvpipe = hcml->rmx_recvpipe; 578122922Sandre else 579122922Sandre hc_entry->rmx_recvpipe = 580122922Sandre (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2; 581190948Srwatson /* TCPSTAT_INC(tcps_cachedrecvpipe); */ 582122922Sandre } 583122922Sandre 584122922Sandre TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 585122922Sandre TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 586122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 587122922Sandre} 588122922Sandre 589122922Sandre/* 590122922Sandre * Sysctl function: prints the list and values of all hostcache entries in 591122922Sandre * unsorted order. 592122922Sandre */ 593122922Sandrestatic int 594122922Sandresysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS) 595122922Sandre{ 596122922Sandre int linesize = 128; 597278534Sjhb struct sbuf sb; 598278534Sjhb int i, error; 599122922Sandre struct hc_metrics *hc_entry; 600165118Sbz#ifdef INET6 601165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 602165118Sbz#endif 603122922Sandre 604278534Sjhb sbuf_new(&sb, NULL, linesize * (V_tcp_hostcache.cache_count + 1), 605278534Sjhb SBUF_FIXEDLEN); 606122922Sandre 607278534Sjhb sbuf_printf(&sb, 608278534Sjhb "\nIP address MTU SSTRESH RTT RTTVAR BANDWIDTH " 609122922Sandre " CWND SENDPIPE RECVPIPE HITS UPD EXP\n"); 610122922Sandre 611122922Sandre#define msec(u) (((u) + 500) / 1000) 612181803Sbz for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 613181803Sbz THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 614181803Sbz TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket, 615122922Sandre rmx_q) { 616278534Sjhb sbuf_printf(&sb, 617122922Sandre "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu " 618122922Sandre "%4lu %4lu %4i\n", 619122922Sandre hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) : 620122922Sandre#ifdef INET6 621165118Sbz ip6_sprintf(ip6buf, &hc_entry->ip6), 622122922Sandre#else 623122922Sandre "IPv6?", 624122922Sandre#endif 625122922Sandre hc_entry->rmx_mtu, 626122922Sandre hc_entry->rmx_ssthresh, 627122922Sandre msec(hc_entry->rmx_rtt * 628122922Sandre (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))), 629122922Sandre msec(hc_entry->rmx_rttvar * 630238083Strociny (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))), 631133477Sandre hc_entry->rmx_bandwidth * 8, 632122922Sandre hc_entry->rmx_cwnd, 633122922Sandre hc_entry->rmx_sendpipe, 634122922Sandre hc_entry->rmx_recvpipe, 635122922Sandre hc_entry->rmx_hits, 636122922Sandre hc_entry->rmx_updates, 637122922Sandre hc_entry->rmx_expire); 638122922Sandre } 639181803Sbz THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 640122922Sandre } 641122922Sandre#undef msec 642278534Sjhb sbuf_finish(&sb); 643278534Sjhb error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 644278534Sjhb sbuf_delete(&sb); 645122922Sandre return(error); 646122922Sandre} 647122922Sandre 648122922Sandre/* 649203724Sbz * Caller has to make sure the curvnet is set properly. 650122922Sandre */ 651122922Sandrestatic void 652203724Sbztcp_hc_purge_internal(int all) 653122922Sandre{ 654128574Sandre struct hc_metrics *hc_entry, *hc_next; 655122922Sandre int i; 656122922Sandre 657181803Sbz for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 658181803Sbz THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 659181887Sjulian TAILQ_FOREACH_SAFE(hc_entry, 660181888Sjulian &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) { 661122922Sandre if (all || hc_entry->rmx_expire <= 0) { 662181803Sbz TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket, 663122922Sandre hc_entry, rmx_q); 664181803Sbz uma_zfree(V_tcp_hostcache.zone, hc_entry); 665181803Sbz V_tcp_hostcache.hashbase[i].hch_length--; 666181803Sbz V_tcp_hostcache.cache_count--; 667122922Sandre } else 668181803Sbz hc_entry->rmx_expire -= V_tcp_hostcache.prune; 669122922Sandre } 670181803Sbz THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 671122922Sandre } 672203724Sbz} 673181887Sjulian 674203724Sbz/* 675203724Sbz * Expire and purge (old|all) entries in the tcp_hostcache. Runs 676203724Sbz * periodically from the callout. 677203724Sbz */ 678203724Sbzstatic void 679203724Sbztcp_hc_purge(void *arg) 680203724Sbz{ 681203724Sbz CURVNET_SET((struct vnet *) arg); 682203724Sbz int all = 0; 683203724Sbz 684203724Sbz if (V_tcp_hostcache.purgeall) { 685203724Sbz all = 1; 686203724Sbz V_tcp_hostcache.purgeall = 0; 687203724Sbz } 688203724Sbz 689203724Sbz tcp_hc_purge_internal(all); 690203724Sbz 691181887Sjulian callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 692181887Sjulian tcp_hc_purge, arg); 693191816Szec CURVNET_RESTORE(); 694122922Sandre} 695