tcp_hostcache.c revision 238083
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: head/sys/netinet/tcp_hostcache.c 238083 2012-07-03 18:59:13Z trociny $"); 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> 76122922Sandre#include <sys/socket.h> 77122922Sandre#include <sys/socketvar.h> 78122922Sandre#include <sys/sysctl.h> 79122922Sandre 80122922Sandre#include <net/if.h> 81194739Sbz#include <net/route.h> 82196019Srwatson#include <net/vnet.h> 83122922Sandre 84122922Sandre#include <netinet/in.h> 85122922Sandre#include <netinet/in_systm.h> 86122922Sandre#include <netinet/ip.h> 87122922Sandre#include <netinet/in_var.h> 88122922Sandre#include <netinet/in_pcb.h> 89122922Sandre#include <netinet/ip_var.h> 90122922Sandre#ifdef INET6 91122922Sandre#include <netinet/ip6.h> 92122922Sandre#include <netinet6/ip6_var.h> 93122922Sandre#endif 94122922Sandre#include <netinet/tcp.h> 95122922Sandre#include <netinet/tcp_var.h> 96185571Sbz#include <netinet/tcp_hostcache.h> 97122922Sandre#ifdef INET6 98122922Sandre#include <netinet6/tcp6_var.h> 99122922Sandre#endif 100122922Sandre 101122922Sandre#include <vm/uma.h> 102122922Sandre 103122922Sandre/* Arbitrary values */ 104122922Sandre#define TCP_HOSTCACHE_HASHSIZE 512 105122922Sandre#define TCP_HOSTCACHE_BUCKETLIMIT 30 106122922Sandre#define TCP_HOSTCACHE_EXPIRE 60*60 /* one hour */ 107122922Sandre#define TCP_HOSTCACHE_PRUNE 5*60 /* every 5 minutes */ 108122922Sandre 109215701Sdimstatic VNET_DEFINE(struct tcp_hostcache, tcp_hostcache); 110207369Sbz#define V_tcp_hostcache VNET(tcp_hostcache) 111207369Sbz 112215701Sdimstatic VNET_DEFINE(struct callout, tcp_hc_callout); 113195727Srwatson#define V_tcp_hc_callout VNET(tcp_hc_callout) 114195699Srwatson 115122922Sandrestatic struct hc_metrics *tcp_hc_lookup(struct in_conninfo *); 116122922Sandrestatic struct hc_metrics *tcp_hc_insert(struct in_conninfo *); 117122922Sandrestatic int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS); 118203724Sbzstatic void tcp_hc_purge_internal(int); 119122922Sandrestatic void tcp_hc_purge(void *); 120122922Sandre 121227309Sedstatic SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0, 122182633Sbrooks "TCP Host cache"); 123122922Sandre 124217322SmdfSYSCTL_VNET_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_RDTUN, 125195699Srwatson &VNET_NAME(tcp_hostcache.cache_limit), 0, 126183550Szec "Overall entry limit for hostcache"); 127122922Sandre 128217322SmdfSYSCTL_VNET_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_RDTUN, 129195699Srwatson &VNET_NAME(tcp_hostcache.hashsize), 0, 130183550Szec "Size of TCP hostcache hashtable"); 131122922Sandre 132217322SmdfSYSCTL_VNET_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, 133195699Srwatson CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0, 134183550Szec "Per-bucket hash limit for hostcache"); 135122922Sandre 136217322SmdfSYSCTL_VNET_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_RD, 137195699Srwatson &VNET_NAME(tcp_hostcache.cache_count), 0, 138183550Szec "Current number of entries in hostcache"); 139122922Sandre 140195699SrwatsonSYSCTL_VNET_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_RW, 141195699Srwatson &VNET_NAME(tcp_hostcache.expire), 0, 142183550Szec "Expire time of TCP hostcache entries"); 143122922Sandre 144195699SrwatsonSYSCTL_VNET_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_RW, 145195699Srwatson &VNET_NAME(tcp_hostcache.prune), 0, 146195699Srwatson "Time between purge runs"); 147170434Syar 148195699SrwatsonSYSCTL_VNET_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_RW, 149195699Srwatson &VNET_NAME(tcp_hostcache.purgeall), 0, 150183550Szec "Expire all entires on next purge run"); 151122922Sandre 152122922SandreSYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list, 153167784Sandre CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0, 154167784Sandre sysctl_tcp_hc_list, "A", "List of all hostcache entries"); 155122922Sandre 156122922Sandre 157122922Sandrestatic MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache"); 158122922Sandre 159122922Sandre#define HOSTCACHE_HASH(ip) \ 160133874Srwatson (((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) & \ 161181803Sbz V_tcp_hostcache.hashmask) 162122922Sandre 163122922Sandre/* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */ 164133874Srwatson#define HOSTCACHE_HASH6(ip6) \ 165122922Sandre (((ip6)->s6_addr32[0] ^ \ 166122922Sandre (ip6)->s6_addr32[1] ^ \ 167122922Sandre (ip6)->s6_addr32[2] ^ \ 168122922Sandre (ip6)->s6_addr32[3]) & \ 169181803Sbz V_tcp_hostcache.hashmask) 170122922Sandre 171122922Sandre#define THC_LOCK(lp) mtx_lock(lp) 172122922Sandre#define THC_UNLOCK(lp) mtx_unlock(lp) 173122922Sandre 174122922Sandrevoid 175122922Sandretcp_hc_init(void) 176122922Sandre{ 177122922Sandre int i; 178122922Sandre 179122922Sandre /* 180170030Srwatson * Initialize hostcache structures. 181122922Sandre */ 182181803Sbz V_tcp_hostcache.cache_count = 0; 183181803Sbz V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; 184181803Sbz V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT; 185181803Sbz V_tcp_hostcache.cache_limit = 186181803Sbz V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit; 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); 192133874Srwatson TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit", 193181803Sbz &V_tcp_hostcache.cache_limit); 194133874Srwatson TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit", 195181803Sbz &V_tcp_hostcache.bucket_limit); 196181803Sbz if (!powerof2(V_tcp_hostcache.hashsize)) { 197133874Srwatson printf("WARNING: hostcache hash size is not a power of 2.\n"); 198181803Sbz V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */ 199133874Srwatson } 200181803Sbz V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1; 201122922Sandre 202122922Sandre /* 203170030Srwatson * Allocate the hash table. 204122922Sandre */ 205181803Sbz V_tcp_hostcache.hashbase = (struct hc_head *) 206181803Sbz malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head), 207122922Sandre M_HOSTCACHE, M_WAITOK | M_ZERO); 208122922Sandre 209122922Sandre /* 210170030Srwatson * Initialize the hash buckets. 211122922Sandre */ 212181803Sbz for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 213181803Sbz TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket); 214181803Sbz V_tcp_hostcache.hashbase[i].hch_length = 0; 215181803Sbz mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry", 216122922Sandre NULL, MTX_DEF); 217122922Sandre } 218122922Sandre 219122922Sandre /* 220122922Sandre * Allocate the hostcache entries. 221122922Sandre */ 222181887Sjulian V_tcp_hostcache.zone = 223181888Sjulian uma_zcreate("hostcache", sizeof(struct hc_metrics), 224181888Sjulian NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 225181803Sbz uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit); 226122922Sandre 227122922Sandre /* 228122922Sandre * Set up periodic cache cleanup. 229122922Sandre */ 230181803Sbz callout_init(&V_tcp_hc_callout, CALLOUT_MPSAFE); 231181887Sjulian callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 232191816Szec tcp_hc_purge, curvnet); 233122922Sandre} 234122922Sandre 235193731Szec#ifdef VIMAGE 236193731Szecvoid 237193731Szectcp_hc_destroy(void) 238193731Szec{ 239203724Sbz int i; 240193731Szec 241203724Sbz callout_drain(&V_tcp_hc_callout); 242193731Szec 243203724Sbz /* Purge all hc entries. */ 244203724Sbz tcp_hc_purge_internal(1); 245203724Sbz 246203724Sbz /* Free the uma zone and the allocated hash table. */ 247203724Sbz uma_zdestroy(V_tcp_hostcache.zone); 248203724Sbz 249203724Sbz for (i = 0; i < V_tcp_hostcache.hashsize; i++) 250203724Sbz mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx); 251203724Sbz free(V_tcp_hostcache.hashbase, M_HOSTCACHE); 252193731Szec} 253193731Szec#endif 254193731Szec 255122922Sandre/* 256170030Srwatson * Internal function: look up an entry in the hostcache or return NULL. 257122922Sandre * 258122922Sandre * If an entry has been returned, the caller becomes responsible for 259122922Sandre * unlocking the bucket row after he is done reading/modifying the entry. 260122922Sandre */ 261122922Sandrestatic struct hc_metrics * 262122922Sandretcp_hc_lookup(struct in_conninfo *inc) 263122922Sandre{ 264122922Sandre int hash; 265122922Sandre struct hc_head *hc_head; 266122922Sandre struct hc_metrics *hc_entry; 267122922Sandre 268122922Sandre KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer")); 269122922Sandre 270122922Sandre /* 271122922Sandre * Hash the foreign ip address. 272122922Sandre */ 273186222Sbz if (inc->inc_flags & INC_ISIPV6) 274122922Sandre hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 275122922Sandre else 276122922Sandre hash = HOSTCACHE_HASH(&inc->inc_faddr); 277122922Sandre 278181803Sbz hc_head = &V_tcp_hostcache.hashbase[hash]; 279122922Sandre 280122922Sandre /* 281170030Srwatson * Acquire lock for this bucket row; we release the lock if we don't 282170030Srwatson * find an entry, otherwise the caller has to unlock after he is 283170030Srwatson * done. 284122922Sandre */ 285122922Sandre THC_LOCK(&hc_head->hch_mtx); 286122922Sandre 287122922Sandre /* 288170030Srwatson * Iterate through entries in bucket row looking for a match. 289122922Sandre */ 290122922Sandre TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) { 291186222Sbz if (inc->inc_flags & INC_ISIPV6) { 292122922Sandre if (memcmp(&inc->inc6_faddr, &hc_entry->ip6, 293122922Sandre sizeof(inc->inc6_faddr)) == 0) 294122922Sandre return hc_entry; 295122922Sandre } else { 296122922Sandre if (memcmp(&inc->inc_faddr, &hc_entry->ip4, 297122922Sandre sizeof(inc->inc_faddr)) == 0) 298122922Sandre return hc_entry; 299122922Sandre } 300122922Sandre } 301122922Sandre 302122922Sandre /* 303170030Srwatson * We were unsuccessful and didn't find anything. 304122922Sandre */ 305122922Sandre THC_UNLOCK(&hc_head->hch_mtx); 306122922Sandre return NULL; 307122922Sandre} 308122922Sandre 309122922Sandre/* 310170030Srwatson * Internal function: insert an entry into the hostcache or return NULL if 311170030Srwatson * unable to allocate a new one. 312133874Srwatson * 313122922Sandre * If an entry has been returned, the caller becomes responsible for 314122922Sandre * unlocking the bucket row after he is done reading/modifying the entry. 315122922Sandre */ 316122922Sandrestatic struct hc_metrics * 317122922Sandretcp_hc_insert(struct in_conninfo *inc) 318122922Sandre{ 319122922Sandre int hash; 320122922Sandre struct hc_head *hc_head; 321122922Sandre struct hc_metrics *hc_entry; 322122922Sandre 323122922Sandre KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer")); 324122922Sandre 325122922Sandre /* 326170030Srwatson * Hash the foreign ip address. 327122922Sandre */ 328186222Sbz if (inc->inc_flags & INC_ISIPV6) 329122922Sandre hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 330122922Sandre else 331122922Sandre hash = HOSTCACHE_HASH(&inc->inc_faddr); 332122922Sandre 333181803Sbz hc_head = &V_tcp_hostcache.hashbase[hash]; 334122922Sandre 335122922Sandre /* 336170030Srwatson * Acquire lock for this bucket row; we release the lock if we don't 337170030Srwatson * find an entry, otherwise the caller has to unlock after he is 338170030Srwatson * done. 339122922Sandre */ 340122922Sandre THC_LOCK(&hc_head->hch_mtx); 341122922Sandre 342122922Sandre /* 343170030Srwatson * If the bucket limit is reached, reuse the least-used element. 344122922Sandre */ 345181803Sbz if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit || 346181803Sbz V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) { 347122922Sandre hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead); 348122922Sandre /* 349122922Sandre * At first we were dropping the last element, just to 350170030Srwatson * reacquire it in the next two lines again, which isn't very 351170030Srwatson * efficient. Instead just reuse the least used element. 352170030Srwatson * We may drop something that is still "in-use" but we can be 353170030Srwatson * "lossy". 354170405Sandre * Just give up if this bucket row is empty and we don't have 355170405Sandre * anything to replace. 356122922Sandre */ 357170405Sandre if (hc_entry == NULL) { 358170405Sandre THC_UNLOCK(&hc_head->hch_mtx); 359170405Sandre return NULL; 360170405Sandre } 361122922Sandre TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q); 362181803Sbz V_tcp_hostcache.hashbase[hash].hch_length--; 363181803Sbz V_tcp_hostcache.cache_count--; 364190948Srwatson TCPSTAT_INC(tcps_hc_bucketoverflow); 365123028Sandre#if 0 366181803Sbz uma_zfree(V_tcp_hostcache.zone, hc_entry); 367122922Sandre#endif 368122922Sandre } else { 369122922Sandre /* 370170030Srwatson * Allocate a new entry, or balk if not possible. 371122922Sandre */ 372181803Sbz hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT); 373122922Sandre if (hc_entry == NULL) { 374122922Sandre THC_UNLOCK(&hc_head->hch_mtx); 375122922Sandre return NULL; 376122922Sandre } 377122922Sandre } 378122922Sandre 379122922Sandre /* 380170030Srwatson * Initialize basic information of hostcache entry. 381122922Sandre */ 382122922Sandre bzero(hc_entry, sizeof(*hc_entry)); 383186222Sbz if (inc->inc_flags & INC_ISIPV6) 384123113Sandre bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6)); 385122922Sandre else 386122922Sandre hc_entry->ip4 = inc->inc_faddr; 387122922Sandre hc_entry->rmx_head = hc_head; 388181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; 389122922Sandre 390122922Sandre /* 391170030Srwatson * Put it upfront. 392122922Sandre */ 393122922Sandre TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q); 394181803Sbz V_tcp_hostcache.hashbase[hash].hch_length++; 395181803Sbz V_tcp_hostcache.cache_count++; 396190948Srwatson TCPSTAT_INC(tcps_hc_added); 397122922Sandre 398122922Sandre return hc_entry; 399122922Sandre} 400122922Sandre 401122922Sandre/* 402170030Srwatson * External function: look up an entry in the hostcache and fill out the 403170030Srwatson * supplied TCP metrics structure. Fills in NULL when no entry was found or 404170030Srwatson * a value is not set. 405122922Sandre */ 406122922Sandrevoid 407122922Sandretcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite) 408122922Sandre{ 409122922Sandre struct hc_metrics *hc_entry; 410122922Sandre 411122922Sandre /* 412170030Srwatson * Find the right bucket. 413122922Sandre */ 414122922Sandre hc_entry = tcp_hc_lookup(inc); 415122922Sandre 416122922Sandre /* 417170030Srwatson * If we don't have an existing object. 418122922Sandre */ 419122922Sandre if (hc_entry == NULL) { 420122922Sandre bzero(hc_metrics_lite, sizeof(*hc_metrics_lite)); 421122922Sandre return; 422122922Sandre } 423122922Sandre hc_entry->rmx_hits++; 424181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 425122922Sandre 426122922Sandre hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu; 427122922Sandre hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh; 428122922Sandre hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt; 429122922Sandre hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar; 430122922Sandre hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth; 431122922Sandre hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd; 432122922Sandre hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe; 433122922Sandre hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe; 434122922Sandre 435122922Sandre /* 436170030Srwatson * Unlock bucket row. 437122922Sandre */ 438122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 439122922Sandre} 440122922Sandre 441122922Sandre/* 442170030Srwatson * External function: look up an entry in the hostcache and return the 443170030Srwatson * discovered path MTU. Returns NULL if no entry is found or value is not 444138409Srwatson * set. 445122922Sandre */ 446122922Sandreu_long 447122922Sandretcp_hc_getmtu(struct in_conninfo *inc) 448122922Sandre{ 449122922Sandre struct hc_metrics *hc_entry; 450122922Sandre u_long mtu; 451122922Sandre 452122922Sandre hc_entry = tcp_hc_lookup(inc); 453122922Sandre if (hc_entry == NULL) { 454122922Sandre return 0; 455122922Sandre } 456122922Sandre hc_entry->rmx_hits++; 457181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 458122922Sandre 459122922Sandre mtu = hc_entry->rmx_mtu; 460122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 461122922Sandre return mtu; 462122922Sandre} 463122922Sandre 464122922Sandre/* 465170030Srwatson * External function: update the MTU value of an entry in the hostcache. 466122922Sandre * Creates a new entry if none was found. 467122922Sandre */ 468122922Sandrevoid 469122922Sandretcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu) 470122922Sandre{ 471122922Sandre struct hc_metrics *hc_entry; 472122922Sandre 473122922Sandre /* 474170030Srwatson * Find the right bucket. 475122922Sandre */ 476122922Sandre hc_entry = tcp_hc_lookup(inc); 477122922Sandre 478122922Sandre /* 479170030Srwatson * If we don't have an existing object, try to insert a new one. 480122922Sandre */ 481122922Sandre if (hc_entry == NULL) { 482122922Sandre hc_entry = tcp_hc_insert(inc); 483122922Sandre if (hc_entry == NULL) 484122922Sandre return; 485122922Sandre } 486122922Sandre hc_entry->rmx_updates++; 487181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 488122922Sandre 489122922Sandre hc_entry->rmx_mtu = mtu; 490122922Sandre 491122922Sandre /* 492170030Srwatson * Put it upfront so we find it faster next time. 493122922Sandre */ 494122922Sandre TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 495122922Sandre TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 496122922Sandre 497122922Sandre /* 498170030Srwatson * Unlock bucket row. 499122922Sandre */ 500122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 501122922Sandre} 502122922Sandre 503122922Sandre/* 504170030Srwatson * External function: update the TCP metrics of an entry in the hostcache. 505122922Sandre * Creates a new entry if none was found. 506122922Sandre */ 507122922Sandrevoid 508122922Sandretcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml) 509122922Sandre{ 510122922Sandre struct hc_metrics *hc_entry; 511122922Sandre 512122922Sandre hc_entry = tcp_hc_lookup(inc); 513122922Sandre if (hc_entry == NULL) { 514122922Sandre hc_entry = tcp_hc_insert(inc); 515122922Sandre if (hc_entry == NULL) 516122922Sandre return; 517122922Sandre } 518122922Sandre hc_entry->rmx_updates++; 519181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 520122922Sandre 521122922Sandre if (hcml->rmx_rtt != 0) { 522122922Sandre if (hc_entry->rmx_rtt == 0) 523122922Sandre hc_entry->rmx_rtt = hcml->rmx_rtt; 524122922Sandre else 525122922Sandre hc_entry->rmx_rtt = 526122922Sandre (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2; 527190948Srwatson TCPSTAT_INC(tcps_cachedrtt); 528122922Sandre } 529122922Sandre if (hcml->rmx_rttvar != 0) { 530122922Sandre if (hc_entry->rmx_rttvar == 0) 531133874Srwatson hc_entry->rmx_rttvar = hcml->rmx_rttvar; 532122922Sandre else 533122922Sandre hc_entry->rmx_rttvar = 534122922Sandre (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2; 535190948Srwatson TCPSTAT_INC(tcps_cachedrttvar); 536122922Sandre } 537122922Sandre if (hcml->rmx_ssthresh != 0) { 538122922Sandre if (hc_entry->rmx_ssthresh == 0) 539122922Sandre hc_entry->rmx_ssthresh = hcml->rmx_ssthresh; 540122922Sandre else 541122922Sandre hc_entry->rmx_ssthresh = 542122922Sandre (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2; 543190948Srwatson TCPSTAT_INC(tcps_cachedssthresh); 544122922Sandre } 545122922Sandre if (hcml->rmx_bandwidth != 0) { 546122922Sandre if (hc_entry->rmx_bandwidth == 0) 547122922Sandre hc_entry->rmx_bandwidth = hcml->rmx_bandwidth; 548122922Sandre else 549122922Sandre hc_entry->rmx_bandwidth = 550122922Sandre (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2; 551190948Srwatson /* TCPSTAT_INC(tcps_cachedbandwidth); */ 552122922Sandre } 553122922Sandre if (hcml->rmx_cwnd != 0) { 554122922Sandre if (hc_entry->rmx_cwnd == 0) 555122922Sandre hc_entry->rmx_cwnd = hcml->rmx_cwnd; 556122922Sandre else 557122922Sandre hc_entry->rmx_cwnd = 558122922Sandre (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2; 559190948Srwatson /* TCPSTAT_INC(tcps_cachedcwnd); */ 560122922Sandre } 561122922Sandre if (hcml->rmx_sendpipe != 0) { 562122922Sandre if (hc_entry->rmx_sendpipe == 0) 563122922Sandre hc_entry->rmx_sendpipe = hcml->rmx_sendpipe; 564122922Sandre else 565122922Sandre hc_entry->rmx_sendpipe = 566122922Sandre (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2; 567190948Srwatson /* TCPSTAT_INC(tcps_cachedsendpipe); */ 568133874Srwatson } 569122922Sandre if (hcml->rmx_recvpipe != 0) { 570122922Sandre if (hc_entry->rmx_recvpipe == 0) 571122922Sandre hc_entry->rmx_recvpipe = hcml->rmx_recvpipe; 572122922Sandre else 573122922Sandre hc_entry->rmx_recvpipe = 574122922Sandre (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2; 575190948Srwatson /* TCPSTAT_INC(tcps_cachedrecvpipe); */ 576122922Sandre } 577122922Sandre 578122922Sandre TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 579122922Sandre TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 580122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 581122922Sandre} 582122922Sandre 583122922Sandre/* 584122922Sandre * Sysctl function: prints the list and values of all hostcache entries in 585122922Sandre * unsorted order. 586122922Sandre */ 587122922Sandrestatic int 588122922Sandresysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS) 589122922Sandre{ 590122922Sandre int bufsize; 591122922Sandre int linesize = 128; 592122922Sandre char *p, *buf; 593122922Sandre int len, i, error; 594122922Sandre struct hc_metrics *hc_entry; 595165118Sbz#ifdef INET6 596165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 597165118Sbz#endif 598122922Sandre 599181803Sbz bufsize = linesize * (V_tcp_hostcache.cache_count + 1); 600122922Sandre 601122922Sandre p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO); 602122922Sandre 603122922Sandre len = snprintf(p, linesize, 604122922Sandre "\nIP address MTU SSTRESH RTT RTTVAR BANDWIDTH " 605122922Sandre " CWND SENDPIPE RECVPIPE HITS UPD EXP\n"); 606122922Sandre p += len; 607122922Sandre 608122922Sandre#define msec(u) (((u) + 500) / 1000) 609181803Sbz for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 610181803Sbz THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 611181803Sbz TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket, 612122922Sandre rmx_q) { 613122922Sandre len = snprintf(p, linesize, 614122922Sandre "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu " 615122922Sandre "%4lu %4lu %4i\n", 616122922Sandre hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) : 617122922Sandre#ifdef INET6 618165118Sbz ip6_sprintf(ip6buf, &hc_entry->ip6), 619122922Sandre#else 620122922Sandre "IPv6?", 621122922Sandre#endif 622122922Sandre hc_entry->rmx_mtu, 623122922Sandre hc_entry->rmx_ssthresh, 624122922Sandre msec(hc_entry->rmx_rtt * 625122922Sandre (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))), 626122922Sandre msec(hc_entry->rmx_rttvar * 627238083Strociny (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))), 628133477Sandre hc_entry->rmx_bandwidth * 8, 629122922Sandre hc_entry->rmx_cwnd, 630122922Sandre hc_entry->rmx_sendpipe, 631122922Sandre hc_entry->rmx_recvpipe, 632122922Sandre hc_entry->rmx_hits, 633122922Sandre hc_entry->rmx_updates, 634122922Sandre hc_entry->rmx_expire); 635122922Sandre p += len; 636122922Sandre } 637181803Sbz THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 638122922Sandre } 639122922Sandre#undef msec 640122922Sandre error = SYSCTL_OUT(req, buf, p - buf); 641122922Sandre free(buf, M_TEMP); 642122922Sandre return(error); 643122922Sandre} 644122922Sandre 645122922Sandre/* 646203724Sbz * Caller has to make sure the curvnet is set properly. 647122922Sandre */ 648122922Sandrestatic void 649203724Sbztcp_hc_purge_internal(int all) 650122922Sandre{ 651128574Sandre struct hc_metrics *hc_entry, *hc_next; 652122922Sandre int i; 653122922Sandre 654181803Sbz for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 655181803Sbz THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 656181887Sjulian TAILQ_FOREACH_SAFE(hc_entry, 657181888Sjulian &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) { 658122922Sandre if (all || hc_entry->rmx_expire <= 0) { 659181803Sbz TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket, 660122922Sandre hc_entry, rmx_q); 661181803Sbz uma_zfree(V_tcp_hostcache.zone, hc_entry); 662181803Sbz V_tcp_hostcache.hashbase[i].hch_length--; 663181803Sbz V_tcp_hostcache.cache_count--; 664122922Sandre } else 665181803Sbz hc_entry->rmx_expire -= V_tcp_hostcache.prune; 666122922Sandre } 667181803Sbz THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 668122922Sandre } 669203724Sbz} 670181887Sjulian 671203724Sbz/* 672203724Sbz * Expire and purge (old|all) entries in the tcp_hostcache. Runs 673203724Sbz * periodically from the callout. 674203724Sbz */ 675203724Sbzstatic void 676203724Sbztcp_hc_purge(void *arg) 677203724Sbz{ 678203724Sbz CURVNET_SET((struct vnet *) arg); 679203724Sbz int all = 0; 680203724Sbz 681203724Sbz if (V_tcp_hostcache.purgeall) { 682203724Sbz all = 1; 683203724Sbz V_tcp_hostcache.purgeall = 0; 684203724Sbz } 685203724Sbz 686203724Sbz tcp_hc_purge_internal(all); 687203724Sbz 688181887Sjulian callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 689181887Sjulian tcp_hc_purge, arg); 690191816Szec CURVNET_RESTORE(); 691122922Sandre} 692