tcp_hostcache.c revision 194739
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 194739 2009-06-23 17:03:45Z bz $"); 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> 79181803Sbz#include <sys/vimage.h> 80122922Sandre 81122922Sandre#include <net/if.h> 82194739Sbz#include <net/route.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> 97185571Sbz#include <netinet/vinet.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 110185088Szec#ifdef VIMAGE_GLOBALS 111122922Sandrestatic struct tcp_hostcache tcp_hostcache; 112122922Sandrestatic struct callout tcp_hc_callout; 113185088Szec#endif 114122922Sandre 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); 118122922Sandrestatic void tcp_hc_purge(void *); 119122922Sandre 120182633SbrooksSYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0, 121182633Sbrooks "TCP Host cache"); 122122922Sandre 123183550SzecSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, cachelimit, 124183550Szec CTLFLAG_RDTUN, tcp_hostcache.cache_limit, 0, 125183550Szec "Overall entry limit for hostcache"); 126122922Sandre 127183550SzecSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, hashsize, 128183550Szec CTLFLAG_RDTUN, tcp_hostcache.hashsize, 0, 129183550Szec "Size of TCP hostcache hashtable"); 130122922Sandre 131183550SzecSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, bucketlimit, 132183550Szec CTLFLAG_RDTUN, tcp_hostcache.bucket_limit, 0, 133183550Szec "Per-bucket hash limit for hostcache"); 134122922Sandre 135183550SzecSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, count, 136183550Szec CTLFLAG_RD, tcp_hostcache.cache_count, 0, 137183550Szec "Current number of entries in hostcache"); 138122922Sandre 139183550SzecSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, expire, 140183550Szec CTLFLAG_RW, tcp_hostcache.expire, 0, 141183550Szec "Expire time of TCP hostcache entries"); 142122922Sandre 143183550SzecSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, prune, 144183550Szec CTLFLAG_RW, tcp_hostcache.prune, 0, "Time between purge runs"); 145170434Syar 146183550SzecSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, purge, 147183550Szec CTLFLAG_RW, tcp_hostcache.purgeall, 0, 148183550Szec "Expire all entires on next purge run"); 149122922Sandre 150122922SandreSYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list, 151167784Sandre CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0, 152167784Sandre sysctl_tcp_hc_list, "A", "List of all hostcache entries"); 153122922Sandre 154122922Sandre 155122922Sandrestatic MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache"); 156122922Sandre 157122922Sandre#define HOSTCACHE_HASH(ip) \ 158133874Srwatson (((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) & \ 159181803Sbz V_tcp_hostcache.hashmask) 160122922Sandre 161122922Sandre/* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */ 162133874Srwatson#define HOSTCACHE_HASH6(ip6) \ 163122922Sandre (((ip6)->s6_addr32[0] ^ \ 164122922Sandre (ip6)->s6_addr32[1] ^ \ 165122922Sandre (ip6)->s6_addr32[2] ^ \ 166122922Sandre (ip6)->s6_addr32[3]) & \ 167181803Sbz V_tcp_hostcache.hashmask) 168122922Sandre 169122922Sandre#define THC_LOCK(lp) mtx_lock(lp) 170122922Sandre#define THC_UNLOCK(lp) mtx_unlock(lp) 171122922Sandre 172122922Sandrevoid 173122922Sandretcp_hc_init(void) 174122922Sandre{ 175183550Szec INIT_VNET_INET(curvnet); 176122922Sandre int i; 177122922Sandre 178122922Sandre /* 179170030Srwatson * Initialize hostcache structures. 180122922Sandre */ 181181803Sbz V_tcp_hostcache.cache_count = 0; 182181803Sbz V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; 183181803Sbz V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT; 184181803Sbz V_tcp_hostcache.cache_limit = 185181803Sbz V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit; 186181803Sbz V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE; 187181803Sbz V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE; 188122922Sandre 189133874Srwatson TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize", 190181803Sbz &V_tcp_hostcache.hashsize); 191133874Srwatson TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit", 192181803Sbz &V_tcp_hostcache.cache_limit); 193133874Srwatson TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit", 194181803Sbz &V_tcp_hostcache.bucket_limit); 195181803Sbz if (!powerof2(V_tcp_hostcache.hashsize)) { 196133874Srwatson printf("WARNING: hostcache hash size is not a power of 2.\n"); 197181803Sbz V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */ 198133874Srwatson } 199181803Sbz V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1; 200122922Sandre 201122922Sandre /* 202170030Srwatson * Allocate the hash table. 203122922Sandre */ 204181803Sbz V_tcp_hostcache.hashbase = (struct hc_head *) 205181803Sbz malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head), 206122922Sandre M_HOSTCACHE, M_WAITOK | M_ZERO); 207122922Sandre 208122922Sandre /* 209170030Srwatson * Initialize the hash buckets. 210122922Sandre */ 211181803Sbz for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 212181803Sbz TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket); 213181803Sbz V_tcp_hostcache.hashbase[i].hch_length = 0; 214181803Sbz mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry", 215122922Sandre NULL, MTX_DEF); 216122922Sandre } 217122922Sandre 218122922Sandre /* 219122922Sandre * Allocate the hostcache entries. 220122922Sandre */ 221181887Sjulian V_tcp_hostcache.zone = 222181888Sjulian uma_zcreate("hostcache", sizeof(struct hc_metrics), 223181888Sjulian NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 224181803Sbz uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit); 225122922Sandre 226122922Sandre /* 227122922Sandre * Set up periodic cache cleanup. 228122922Sandre */ 229181803Sbz callout_init(&V_tcp_hc_callout, CALLOUT_MPSAFE); 230181887Sjulian callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 231191816Szec tcp_hc_purge, curvnet); 232122922Sandre} 233122922Sandre 234193731Szec#ifdef VIMAGE 235193731Szecvoid 236193731Szectcp_hc_destroy(void) 237193731Szec{ 238193731Szec INIT_VNET_INET(curvnet); 239193731Szec 240193731Szec /* XXX TODO walk the hashtable and free all entries */ 241193731Szec 242193731Szec callout_drain(&V_tcp_hc_callout); 243193731Szec} 244193731Szec#endif 245193731Szec 246122922Sandre/* 247170030Srwatson * Internal function: look up an entry in the hostcache or return NULL. 248122922Sandre * 249122922Sandre * If an entry has been returned, the caller becomes responsible for 250122922Sandre * unlocking the bucket row after he is done reading/modifying the entry. 251122922Sandre */ 252122922Sandrestatic struct hc_metrics * 253122922Sandretcp_hc_lookup(struct in_conninfo *inc) 254122922Sandre{ 255183550Szec INIT_VNET_INET(curvnet); 256122922Sandre int hash; 257122922Sandre struct hc_head *hc_head; 258122922Sandre struct hc_metrics *hc_entry; 259122922Sandre 260122922Sandre KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer")); 261122922Sandre 262122922Sandre /* 263122922Sandre * Hash the foreign ip address. 264122922Sandre */ 265186222Sbz if (inc->inc_flags & INC_ISIPV6) 266122922Sandre hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 267122922Sandre else 268122922Sandre hash = HOSTCACHE_HASH(&inc->inc_faddr); 269122922Sandre 270181803Sbz hc_head = &V_tcp_hostcache.hashbase[hash]; 271122922Sandre 272122922Sandre /* 273170030Srwatson * Acquire lock for this bucket row; we release the lock if we don't 274170030Srwatson * find an entry, otherwise the caller has to unlock after he is 275170030Srwatson * done. 276122922Sandre */ 277122922Sandre THC_LOCK(&hc_head->hch_mtx); 278122922Sandre 279122922Sandre /* 280170030Srwatson * Iterate through entries in bucket row looking for a match. 281122922Sandre */ 282122922Sandre TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) { 283186222Sbz if (inc->inc_flags & INC_ISIPV6) { 284122922Sandre if (memcmp(&inc->inc6_faddr, &hc_entry->ip6, 285122922Sandre sizeof(inc->inc6_faddr)) == 0) 286122922Sandre return hc_entry; 287122922Sandre } else { 288122922Sandre if (memcmp(&inc->inc_faddr, &hc_entry->ip4, 289122922Sandre sizeof(inc->inc_faddr)) == 0) 290122922Sandre return hc_entry; 291122922Sandre } 292122922Sandre } 293122922Sandre 294122922Sandre /* 295170030Srwatson * We were unsuccessful and didn't find anything. 296122922Sandre */ 297122922Sandre THC_UNLOCK(&hc_head->hch_mtx); 298122922Sandre return NULL; 299122922Sandre} 300122922Sandre 301122922Sandre/* 302170030Srwatson * Internal function: insert an entry into the hostcache or return NULL if 303170030Srwatson * unable to allocate a new one. 304133874Srwatson * 305122922Sandre * If an entry has been returned, the caller becomes responsible for 306122922Sandre * unlocking the bucket row after he is done reading/modifying the entry. 307122922Sandre */ 308122922Sandrestatic struct hc_metrics * 309122922Sandretcp_hc_insert(struct in_conninfo *inc) 310122922Sandre{ 311183550Szec INIT_VNET_INET(curvnet); 312122922Sandre int hash; 313122922Sandre struct hc_head *hc_head; 314122922Sandre struct hc_metrics *hc_entry; 315122922Sandre 316122922Sandre KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer")); 317122922Sandre 318122922Sandre /* 319170030Srwatson * Hash the foreign ip address. 320122922Sandre */ 321186222Sbz if (inc->inc_flags & INC_ISIPV6) 322122922Sandre hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 323122922Sandre else 324122922Sandre hash = HOSTCACHE_HASH(&inc->inc_faddr); 325122922Sandre 326181803Sbz hc_head = &V_tcp_hostcache.hashbase[hash]; 327122922Sandre 328122922Sandre /* 329170030Srwatson * Acquire lock for this bucket row; we release the lock if we don't 330170030Srwatson * find an entry, otherwise the caller has to unlock after he is 331170030Srwatson * done. 332122922Sandre */ 333122922Sandre THC_LOCK(&hc_head->hch_mtx); 334122922Sandre 335122922Sandre /* 336170030Srwatson * If the bucket limit is reached, reuse the least-used element. 337122922Sandre */ 338181803Sbz if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit || 339181803Sbz V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) { 340122922Sandre hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead); 341122922Sandre /* 342122922Sandre * At first we were dropping the last element, just to 343170030Srwatson * reacquire it in the next two lines again, which isn't very 344170030Srwatson * efficient. Instead just reuse the least used element. 345170030Srwatson * We may drop something that is still "in-use" but we can be 346170030Srwatson * "lossy". 347170405Sandre * Just give up if this bucket row is empty and we don't have 348170405Sandre * anything to replace. 349122922Sandre */ 350170405Sandre if (hc_entry == NULL) { 351170405Sandre THC_UNLOCK(&hc_head->hch_mtx); 352170405Sandre return NULL; 353170405Sandre } 354122922Sandre TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q); 355181803Sbz V_tcp_hostcache.hashbase[hash].hch_length--; 356181803Sbz V_tcp_hostcache.cache_count--; 357190948Srwatson TCPSTAT_INC(tcps_hc_bucketoverflow); 358123028Sandre#if 0 359181803Sbz uma_zfree(V_tcp_hostcache.zone, hc_entry); 360122922Sandre#endif 361122922Sandre } else { 362122922Sandre /* 363170030Srwatson * Allocate a new entry, or balk if not possible. 364122922Sandre */ 365181803Sbz hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT); 366122922Sandre if (hc_entry == NULL) { 367122922Sandre THC_UNLOCK(&hc_head->hch_mtx); 368122922Sandre return NULL; 369122922Sandre } 370122922Sandre } 371122922Sandre 372122922Sandre /* 373170030Srwatson * Initialize basic information of hostcache entry. 374122922Sandre */ 375122922Sandre bzero(hc_entry, sizeof(*hc_entry)); 376186222Sbz if (inc->inc_flags & INC_ISIPV6) 377123113Sandre bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6)); 378122922Sandre else 379122922Sandre hc_entry->ip4 = inc->inc_faddr; 380122922Sandre hc_entry->rmx_head = hc_head; 381181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; 382122922Sandre 383122922Sandre /* 384170030Srwatson * Put it upfront. 385122922Sandre */ 386122922Sandre TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q); 387181803Sbz V_tcp_hostcache.hashbase[hash].hch_length++; 388181803Sbz V_tcp_hostcache.cache_count++; 389190948Srwatson TCPSTAT_INC(tcps_hc_added); 390122922Sandre 391122922Sandre return hc_entry; 392122922Sandre} 393122922Sandre 394122922Sandre/* 395170030Srwatson * External function: look up an entry in the hostcache and fill out the 396170030Srwatson * supplied TCP metrics structure. Fills in NULL when no entry was found or 397170030Srwatson * a value is not set. 398122922Sandre */ 399122922Sandrevoid 400122922Sandretcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite) 401122922Sandre{ 402183550Szec INIT_VNET_INET(curvnet); 403122922Sandre struct hc_metrics *hc_entry; 404122922Sandre 405122922Sandre /* 406170030Srwatson * Find the right bucket. 407122922Sandre */ 408122922Sandre hc_entry = tcp_hc_lookup(inc); 409122922Sandre 410122922Sandre /* 411170030Srwatson * If we don't have an existing object. 412122922Sandre */ 413122922Sandre if (hc_entry == NULL) { 414122922Sandre bzero(hc_metrics_lite, sizeof(*hc_metrics_lite)); 415122922Sandre return; 416122922Sandre } 417122922Sandre hc_entry->rmx_hits++; 418181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 419122922Sandre 420122922Sandre hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu; 421122922Sandre hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh; 422122922Sandre hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt; 423122922Sandre hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar; 424122922Sandre hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth; 425122922Sandre hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd; 426122922Sandre hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe; 427122922Sandre hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe; 428122922Sandre 429122922Sandre /* 430170030Srwatson * Unlock bucket row. 431122922Sandre */ 432122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 433122922Sandre} 434122922Sandre 435122922Sandre/* 436170030Srwatson * External function: look up an entry in the hostcache and return the 437170030Srwatson * discovered path MTU. Returns NULL if no entry is found or value is not 438138409Srwatson * set. 439122922Sandre */ 440122922Sandreu_long 441122922Sandretcp_hc_getmtu(struct in_conninfo *inc) 442122922Sandre{ 443183550Szec INIT_VNET_INET(curvnet); 444122922Sandre struct hc_metrics *hc_entry; 445122922Sandre u_long mtu; 446122922Sandre 447122922Sandre hc_entry = tcp_hc_lookup(inc); 448122922Sandre if (hc_entry == NULL) { 449122922Sandre return 0; 450122922Sandre } 451122922Sandre hc_entry->rmx_hits++; 452181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 453122922Sandre 454122922Sandre mtu = hc_entry->rmx_mtu; 455122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 456122922Sandre return mtu; 457122922Sandre} 458122922Sandre 459122922Sandre/* 460170030Srwatson * External function: update the MTU value of an entry in the hostcache. 461122922Sandre * Creates a new entry if none was found. 462122922Sandre */ 463122922Sandrevoid 464122922Sandretcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu) 465122922Sandre{ 466183550Szec INIT_VNET_INET(curvnet); 467122922Sandre struct hc_metrics *hc_entry; 468122922Sandre 469122922Sandre /* 470170030Srwatson * Find the right bucket. 471122922Sandre */ 472122922Sandre hc_entry = tcp_hc_lookup(inc); 473122922Sandre 474122922Sandre /* 475170030Srwatson * If we don't have an existing object, try to insert a new one. 476122922Sandre */ 477122922Sandre if (hc_entry == NULL) { 478122922Sandre hc_entry = tcp_hc_insert(inc); 479122922Sandre if (hc_entry == NULL) 480122922Sandre return; 481122922Sandre } 482122922Sandre hc_entry->rmx_updates++; 483181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 484122922Sandre 485122922Sandre hc_entry->rmx_mtu = mtu; 486122922Sandre 487122922Sandre /* 488170030Srwatson * Put it upfront so we find it faster next time. 489122922Sandre */ 490122922Sandre TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 491122922Sandre TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 492122922Sandre 493122922Sandre /* 494170030Srwatson * Unlock bucket row. 495122922Sandre */ 496122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 497122922Sandre} 498122922Sandre 499122922Sandre/* 500170030Srwatson * External function: update the TCP metrics of an entry in the hostcache. 501122922Sandre * Creates a new entry if none was found. 502122922Sandre */ 503122922Sandrevoid 504122922Sandretcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml) 505122922Sandre{ 506183550Szec INIT_VNET_INET(curvnet); 507122922Sandre struct hc_metrics *hc_entry; 508122922Sandre 509122922Sandre hc_entry = tcp_hc_lookup(inc); 510122922Sandre if (hc_entry == NULL) { 511122922Sandre hc_entry = tcp_hc_insert(inc); 512122922Sandre if (hc_entry == NULL) 513122922Sandre return; 514122922Sandre } 515122922Sandre hc_entry->rmx_updates++; 516181803Sbz hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 517122922Sandre 518122922Sandre if (hcml->rmx_rtt != 0) { 519122922Sandre if (hc_entry->rmx_rtt == 0) 520122922Sandre hc_entry->rmx_rtt = hcml->rmx_rtt; 521122922Sandre else 522122922Sandre hc_entry->rmx_rtt = 523122922Sandre (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2; 524190948Srwatson TCPSTAT_INC(tcps_cachedrtt); 525122922Sandre } 526122922Sandre if (hcml->rmx_rttvar != 0) { 527122922Sandre if (hc_entry->rmx_rttvar == 0) 528133874Srwatson hc_entry->rmx_rttvar = hcml->rmx_rttvar; 529122922Sandre else 530122922Sandre hc_entry->rmx_rttvar = 531122922Sandre (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2; 532190948Srwatson TCPSTAT_INC(tcps_cachedrttvar); 533122922Sandre } 534122922Sandre if (hcml->rmx_ssthresh != 0) { 535122922Sandre if (hc_entry->rmx_ssthresh == 0) 536122922Sandre hc_entry->rmx_ssthresh = hcml->rmx_ssthresh; 537122922Sandre else 538122922Sandre hc_entry->rmx_ssthresh = 539122922Sandre (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2; 540190948Srwatson TCPSTAT_INC(tcps_cachedssthresh); 541122922Sandre } 542122922Sandre if (hcml->rmx_bandwidth != 0) { 543122922Sandre if (hc_entry->rmx_bandwidth == 0) 544122922Sandre hc_entry->rmx_bandwidth = hcml->rmx_bandwidth; 545122922Sandre else 546122922Sandre hc_entry->rmx_bandwidth = 547122922Sandre (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2; 548190948Srwatson /* TCPSTAT_INC(tcps_cachedbandwidth); */ 549122922Sandre } 550122922Sandre if (hcml->rmx_cwnd != 0) { 551122922Sandre if (hc_entry->rmx_cwnd == 0) 552122922Sandre hc_entry->rmx_cwnd = hcml->rmx_cwnd; 553122922Sandre else 554122922Sandre hc_entry->rmx_cwnd = 555122922Sandre (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2; 556190948Srwatson /* TCPSTAT_INC(tcps_cachedcwnd); */ 557122922Sandre } 558122922Sandre if (hcml->rmx_sendpipe != 0) { 559122922Sandre if (hc_entry->rmx_sendpipe == 0) 560122922Sandre hc_entry->rmx_sendpipe = hcml->rmx_sendpipe; 561122922Sandre else 562122922Sandre hc_entry->rmx_sendpipe = 563122922Sandre (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2; 564190948Srwatson /* TCPSTAT_INC(tcps_cachedsendpipe); */ 565133874Srwatson } 566122922Sandre if (hcml->rmx_recvpipe != 0) { 567122922Sandre if (hc_entry->rmx_recvpipe == 0) 568122922Sandre hc_entry->rmx_recvpipe = hcml->rmx_recvpipe; 569122922Sandre else 570122922Sandre hc_entry->rmx_recvpipe = 571122922Sandre (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2; 572190948Srwatson /* TCPSTAT_INC(tcps_cachedrecvpipe); */ 573122922Sandre } 574122922Sandre 575122922Sandre TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 576122922Sandre TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 577122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 578122922Sandre} 579122922Sandre 580122922Sandre/* 581122922Sandre * Sysctl function: prints the list and values of all hostcache entries in 582122922Sandre * unsorted order. 583122922Sandre */ 584122922Sandrestatic int 585122922Sandresysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS) 586122922Sandre{ 587183550Szec INIT_VNET_INET(curvnet); 588122922Sandre int bufsize; 589122922Sandre int linesize = 128; 590122922Sandre char *p, *buf; 591122922Sandre int len, i, error; 592122922Sandre struct hc_metrics *hc_entry; 593165118Sbz#ifdef INET6 594165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 595165118Sbz#endif 596122922Sandre 597181803Sbz bufsize = linesize * (V_tcp_hostcache.cache_count + 1); 598122922Sandre 599122922Sandre p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO); 600122922Sandre 601122922Sandre len = snprintf(p, linesize, 602122922Sandre "\nIP address MTU SSTRESH RTT RTTVAR BANDWIDTH " 603122922Sandre " CWND SENDPIPE RECVPIPE HITS UPD EXP\n"); 604122922Sandre p += len; 605122922Sandre 606122922Sandre#define msec(u) (((u) + 500) / 1000) 607181803Sbz for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 608181803Sbz THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 609181803Sbz TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket, 610122922Sandre rmx_q) { 611122922Sandre len = snprintf(p, linesize, 612122922Sandre "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu " 613122922Sandre "%4lu %4lu %4i\n", 614122922Sandre hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) : 615122922Sandre#ifdef INET6 616165118Sbz ip6_sprintf(ip6buf, &hc_entry->ip6), 617122922Sandre#else 618122922Sandre "IPv6?", 619122922Sandre#endif 620122922Sandre hc_entry->rmx_mtu, 621122922Sandre hc_entry->rmx_ssthresh, 622122922Sandre msec(hc_entry->rmx_rtt * 623122922Sandre (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))), 624122922Sandre msec(hc_entry->rmx_rttvar * 625122922Sandre (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))), 626133477Sandre hc_entry->rmx_bandwidth * 8, 627122922Sandre hc_entry->rmx_cwnd, 628122922Sandre hc_entry->rmx_sendpipe, 629122922Sandre hc_entry->rmx_recvpipe, 630122922Sandre hc_entry->rmx_hits, 631122922Sandre hc_entry->rmx_updates, 632122922Sandre hc_entry->rmx_expire); 633122922Sandre p += len; 634122922Sandre } 635181803Sbz THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 636122922Sandre } 637122922Sandre#undef msec 638122922Sandre error = SYSCTL_OUT(req, buf, p - buf); 639122922Sandre free(buf, M_TEMP); 640122922Sandre return(error); 641122922Sandre} 642122922Sandre 643122922Sandre/* 644170030Srwatson * Expire and purge (old|all) entries in the tcp_hostcache. Runs 645170030Srwatson * periodically from the callout. 646122922Sandre */ 647122922Sandrestatic void 648122922Sandretcp_hc_purge(void *arg) 649122922Sandre{ 650191816Szec CURVNET_SET((struct vnet *) arg); 651183550Szec INIT_VNET_INET(curvnet); 652128574Sandre struct hc_metrics *hc_entry, *hc_next; 653191917Szec int all = 0; 654122922Sandre int i; 655122922Sandre 656181803Sbz if (V_tcp_hostcache.purgeall) { 657122922Sandre all = 1; 658181803Sbz V_tcp_hostcache.purgeall = 0; 659122922Sandre } 660122922Sandre 661181803Sbz for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 662181803Sbz THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 663181887Sjulian TAILQ_FOREACH_SAFE(hc_entry, 664181888Sjulian &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) { 665122922Sandre if (all || hc_entry->rmx_expire <= 0) { 666181803Sbz TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket, 667122922Sandre hc_entry, rmx_q); 668181803Sbz uma_zfree(V_tcp_hostcache.zone, hc_entry); 669181803Sbz V_tcp_hostcache.hashbase[i].hch_length--; 670181803Sbz V_tcp_hostcache.cache_count--; 671122922Sandre } else 672181803Sbz hc_entry->rmx_expire -= V_tcp_hostcache.prune; 673122922Sandre } 674181803Sbz THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 675122922Sandre } 676181887Sjulian 677181887Sjulian callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 678181887Sjulian tcp_hc_purge, arg); 679191816Szec CURVNET_RESTORE(); 680122922Sandre} 681