tcp_hostcache.c revision 133477
1122922Sandre/* 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 * $FreeBSD: head/sys/netinet/tcp_hostcache.c 133477 2004-08-11 10:12:16Z andre $ 30122922Sandre */ 31122922Sandre 32122922Sandre/* 33122922Sandre * The tcp_hostcache moves the tcp specific cached metrics from the routing 34122922Sandre * table into a dedicated structure indexed by the remote IP address. It 35122922Sandre * keeps information on the measured tcp parameters of past tcp sessions 36122922Sandre * to have better initial start values for following connections from the 37122922Sandre * same source. Depending on the network parameters (delay, bandwidth, max 38122922Sandre * MTU, congestion window) between local and remote site this can lead to 39122922Sandre * significant speedups for new tcp connections after the first one. 40122922Sandre * 41122922Sandre * Due to this new tcp_hostcache all tcp specific metrics information in 42122922Sandre * the routing table has been removed. The INPCB no longer keeps a pointer 43122922Sandre * to the routing entry and protocol initiated route cloning has been 44122922Sandre * removed as well. With these changes the routing table has gone back 45122922Sandre * to being more lightwight and only carries information related to packet 46122922Sandre * forwarding. 47122922Sandre * 48122922Sandre * Tcp_hostcache is designed for multiple concurrent access in SMP 49122922Sandre * environments and high contention. All bucket rows have their own 50122922Sandre * lock and thus multiple lookups and modifies can be done at the same 51122922Sandre * time as long as they are in different bucket rows. If a request for 52122922Sandre * insertion of a new record can't be satisfied it simply returns an 53122922Sandre * empty structure. Nobody and nothing shall ever point directly to 54122922Sandre * any entry in tcp_hostcache. All communication is done in an object 55122922Sandre * oriented way and only funtions of tcp_hostcache will manipulate hostcache 56122922Sandre * entries. Otherwise we are unable to achieve good behaviour in concurrent 57122922Sandre * access situations. Since tcp_hostcache is only caching information there 58122922Sandre * are no fatal consequences if we either can't satisfy any particular request 59122922Sandre * or have to drop/overwrite an existing entry because of bucket limit 60122922Sandre * memory constrains. 61122922Sandre */ 62122922Sandre 63122922Sandre/* 64122922Sandre * Many thanks to jlemon for basic structure of tcp_syncache which is being 65122922Sandre * followed here. 66122922Sandre */ 67122922Sandre 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> 81122922Sandre 82122922Sandre#include <netinet/in.h> 83122922Sandre#include <netinet/in_systm.h> 84122922Sandre#include <netinet/ip.h> 85122922Sandre#include <netinet/in_var.h> 86122922Sandre#include <netinet/in_pcb.h> 87122922Sandre#include <netinet/ip_var.h> 88122922Sandre#ifdef INET6 89122922Sandre#include <netinet/ip6.h> 90122922Sandre#include <netinet6/ip6_var.h> 91122922Sandre#endif 92122922Sandre#include <netinet/tcp.h> 93122922Sandre#include <netinet/tcp_var.h> 94122922Sandre#ifdef INET6 95122922Sandre#include <netinet6/tcp6_var.h> 96122922Sandre#endif 97122922Sandre 98122922Sandre#include <vm/uma.h> 99122922Sandre 100122922Sandre 101122922SandreTAILQ_HEAD(hc_qhead, hc_metrics); 102122922Sandre 103122922Sandrestruct hc_head { 104122922Sandre struct hc_qhead hch_bucket; 105122922Sandre u_int hch_length; 106122922Sandre struct mtx hch_mtx; 107122922Sandre}; 108122922Sandre 109122922Sandrestruct hc_metrics { 110122922Sandre /* housekeeping */ 111122922Sandre TAILQ_ENTRY(hc_metrics) rmx_q; 112122922Sandre struct hc_head *rmx_head; /* head of bucket tail queue */ 113122922Sandre struct in_addr ip4; /* IP address */ 114122922Sandre struct in6_addr ip6; /* IP6 address */ 115122922Sandre /* endpoint specific values for tcp */ 116122922Sandre u_long rmx_mtu; /* MTU for this path */ 117122922Sandre u_long rmx_ssthresh; /* outbound gateway buffer limit */ 118122922Sandre u_long rmx_rtt; /* estimated round trip time */ 119122922Sandre u_long rmx_rttvar; /* estimated rtt variance */ 120122922Sandre u_long rmx_bandwidth; /* estimated bandwidth */ 121122922Sandre u_long rmx_cwnd; /* congestion window */ 122122922Sandre u_long rmx_sendpipe; /* outbound delay-bandwidth product */ 123122922Sandre u_long rmx_recvpipe; /* inbound delay-bandwidth product */ 124122922Sandre struct rmxp_tao rmx_tao; /* TAO cache for T/TCP */ 125122922Sandre /* tcp hostcache internal data */ 126122922Sandre int rmx_expire; /* lifetime for object */ 127122922Sandre u_long rmx_hits; /* number of hits */ 128122922Sandre u_long rmx_updates; /* number of updates */ 129122922Sandre}; 130122922Sandre 131122922Sandre/* Arbitrary values */ 132122922Sandre#define TCP_HOSTCACHE_HASHSIZE 512 133122922Sandre#define TCP_HOSTCACHE_BUCKETLIMIT 30 134122922Sandre#define TCP_HOSTCACHE_EXPIRE 60*60 /* one hour */ 135122922Sandre#define TCP_HOSTCACHE_PRUNE 5*60 /* every 5 minutes */ 136122922Sandre 137122922Sandrestruct tcp_hostcache { 138122922Sandre struct hc_head *hashbase; 139122922Sandre uma_zone_t zone; 140122922Sandre u_int hashsize; 141122922Sandre u_int hashmask; 142122922Sandre u_int bucket_limit; 143122922Sandre u_int cache_count; 144122922Sandre u_int cache_limit; 145122922Sandre int expire; 146122922Sandre int purgeall; 147122922Sandre}; 148122922Sandrestatic struct tcp_hostcache tcp_hostcache; 149122922Sandre 150122922Sandrestatic struct callout tcp_hc_callout; 151122922Sandre 152122922Sandrestatic struct hc_metrics *tcp_hc_lookup(struct in_conninfo *); 153122922Sandrestatic struct hc_metrics *tcp_hc_insert(struct in_conninfo *); 154122922Sandrestatic int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS); 155122922Sandrestatic void tcp_hc_purge(void *); 156122922Sandre 157122922SandreSYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0, "TCP Host cache"); 158122922Sandre 159122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_RDTUN, 160122922Sandre &tcp_hostcache.cache_limit, 0, "Overall entry limit for hostcache"); 161122922Sandre 162122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_RDTUN, 163122922Sandre &tcp_hostcache.hashsize, 0, "Size of TCP hostcache hashtable"); 164122922Sandre 165122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, CTLFLAG_RDTUN, 166122922Sandre &tcp_hostcache.bucket_limit, 0, "Per-bucket hash limit for hostcache"); 167122922Sandre 168122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_RD, 169122922Sandre &tcp_hostcache.cache_count, 0, "Current number of entries in hostcache"); 170122922Sandre 171122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_RW, 172122922Sandre &tcp_hostcache.expire, 0, "Expire time of TCP hostcache entries"); 173122922Sandre 174122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_RW, 175122922Sandre &tcp_hostcache.purgeall, 0, "Expire all entires on next purge run"); 176122922Sandre 177122922SandreSYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list, 178122922Sandre CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0, 179122922Sandre sysctl_tcp_hc_list, "A", "List of all hostcache entries"); 180122922Sandre 181122922Sandre 182122922Sandrestatic MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache"); 183122922Sandre 184122922Sandre#define HOSTCACHE_HASH(ip) \ 185122922Sandre (((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) & \ 186122922Sandre tcp_hostcache.hashmask) 187122922Sandre 188122922Sandre/* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */ 189122922Sandre#define HOSTCACHE_HASH6(ip6) \ 190122922Sandre (((ip6)->s6_addr32[0] ^ \ 191122922Sandre (ip6)->s6_addr32[1] ^ \ 192122922Sandre (ip6)->s6_addr32[2] ^ \ 193122922Sandre (ip6)->s6_addr32[3]) & \ 194122922Sandre tcp_hostcache.hashmask) 195122922Sandre 196122922Sandre#define THC_LOCK(lp) mtx_lock(lp) 197122922Sandre#define THC_UNLOCK(lp) mtx_unlock(lp) 198122922Sandre 199122922Sandrevoid 200122922Sandretcp_hc_init(void) 201122922Sandre{ 202122922Sandre int i; 203122922Sandre 204122922Sandre /* 205122922Sandre * Initialize hostcache structures 206122922Sandre */ 207122922Sandre tcp_hostcache.cache_count = 0; 208122922Sandre tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; 209122922Sandre tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT; 210122922Sandre tcp_hostcache.cache_limit = 211122922Sandre tcp_hostcache.hashsize * tcp_hostcache.bucket_limit; 212122922Sandre tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE; 213122922Sandre 214122922Sandre TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize", 215122922Sandre &tcp_hostcache.hashsize); 216122922Sandre TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit", 217122922Sandre &tcp_hostcache.cache_limit); 218122922Sandre TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit", 219122922Sandre &tcp_hostcache.bucket_limit); 220122922Sandre if (!powerof2(tcp_hostcache.hashsize)) { 221122922Sandre printf("WARNING: hostcache hash size is not a power of 2.\n"); 222122922Sandre tcp_hostcache.hashsize = 512; /* safe default */ 223122922Sandre } 224122922Sandre tcp_hostcache.hashmask = tcp_hostcache.hashsize - 1; 225122922Sandre 226122922Sandre /* 227122922Sandre * Allocate the hash table 228122922Sandre */ 229122922Sandre tcp_hostcache.hashbase = (struct hc_head *) 230122922Sandre malloc(tcp_hostcache.hashsize * sizeof(struct hc_head), 231122922Sandre M_HOSTCACHE, M_WAITOK | M_ZERO); 232122922Sandre 233122922Sandre /* 234122922Sandre * Initialize the hash buckets 235122922Sandre */ 236122922Sandre for (i = 0; i < tcp_hostcache.hashsize; i++) { 237122922Sandre TAILQ_INIT(&tcp_hostcache.hashbase[i].hch_bucket); 238122922Sandre tcp_hostcache.hashbase[i].hch_length = 0; 239122922Sandre mtx_init(&tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry", 240122922Sandre NULL, MTX_DEF); 241122922Sandre } 242122922Sandre 243122922Sandre /* 244122922Sandre * Allocate the hostcache entries. 245122922Sandre */ 246122922Sandre tcp_hostcache.zone = uma_zcreate("hostcache", sizeof(struct hc_metrics), 247122922Sandre NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 248122922Sandre uma_zone_set_max(tcp_hostcache.zone, tcp_hostcache.cache_limit); 249122922Sandre 250122922Sandre /* 251122922Sandre * Set up periodic cache cleanup. 252122922Sandre */ 253122922Sandre callout_init(&tcp_hc_callout, CALLOUT_MPSAFE); 254122922Sandre callout_reset(&tcp_hc_callout, TCP_HOSTCACHE_PRUNE * hz, tcp_hc_purge, 0); 255122922Sandre} 256122922Sandre 257122922Sandre/* 258122922Sandre * Internal function: lookup an entry in the hostcache or return NULL. 259122922Sandre * 260122922Sandre * If an entry has been returned, the caller becomes responsible for 261122922Sandre * unlocking the bucket row after he is done reading/modifying the entry. 262122922Sandre */ 263122922Sandrestatic struct hc_metrics * 264122922Sandretcp_hc_lookup(struct in_conninfo *inc) 265122922Sandre{ 266122922Sandre int hash; 267122922Sandre struct hc_head *hc_head; 268122922Sandre struct hc_metrics *hc_entry; 269122922Sandre 270122922Sandre KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer")); 271122922Sandre 272122922Sandre /* 273122922Sandre * Hash the foreign ip address. 274122922Sandre */ 275122922Sandre if (inc->inc_isipv6) 276122922Sandre hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 277122922Sandre else 278122922Sandre hash = HOSTCACHE_HASH(&inc->inc_faddr); 279122922Sandre 280122922Sandre hc_head = &tcp_hostcache.hashbase[hash]; 281122922Sandre 282122922Sandre /* 283122922Sandre * aquire lock for this bucket row 284122922Sandre * we release the lock if we don't find an entry, 285122922Sandre * otherwise the caller has to unlock after he is done 286122922Sandre */ 287122922Sandre THC_LOCK(&hc_head->hch_mtx); 288122922Sandre 289122922Sandre /* 290122922Sandre * circle through entries in bucket row looking for a match 291122922Sandre */ 292122922Sandre TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) { 293122922Sandre if (inc->inc_isipv6) { 294122922Sandre if (memcmp(&inc->inc6_faddr, &hc_entry->ip6, 295122922Sandre sizeof(inc->inc6_faddr)) == 0) 296122922Sandre return hc_entry; 297122922Sandre } else { 298122922Sandre if (memcmp(&inc->inc_faddr, &hc_entry->ip4, 299122922Sandre sizeof(inc->inc_faddr)) == 0) 300122922Sandre return hc_entry; 301122922Sandre } 302122922Sandre } 303122922Sandre 304122922Sandre /* 305122922Sandre * We were unsuccessful and didn't find anything 306122922Sandre */ 307122922Sandre THC_UNLOCK(&hc_head->hch_mtx); 308122922Sandre return NULL; 309122922Sandre} 310122922Sandre 311122922Sandre/* 312122922Sandre * Internal function: insert an entry into the hostcache or return NULL 313122922Sandre * if unable to allocate a new one. 314122922Sandre * 315122922Sandre * If an entry has been returned, the caller becomes responsible for 316122922Sandre * unlocking the bucket row after he is done reading/modifying the entry. 317122922Sandre */ 318122922Sandrestatic struct hc_metrics * 319122922Sandretcp_hc_insert(struct in_conninfo *inc) 320122922Sandre{ 321122922Sandre int hash; 322122922Sandre struct hc_head *hc_head; 323122922Sandre struct hc_metrics *hc_entry; 324122922Sandre 325122922Sandre KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer")); 326122922Sandre 327122922Sandre /* 328122922Sandre * Hash the foreign ip address 329122922Sandre */ 330122922Sandre if (inc->inc_isipv6) 331122922Sandre hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 332122922Sandre else 333122922Sandre hash = HOSTCACHE_HASH(&inc->inc_faddr); 334122922Sandre 335122922Sandre hc_head = &tcp_hostcache.hashbase[hash]; 336122922Sandre 337122922Sandre /* 338122922Sandre * aquire lock for this bucket row 339122922Sandre * we release the lock if we don't find an entry, 340122922Sandre * otherwise the caller has to unlock after he is done 341122922Sandre */ 342122922Sandre THC_LOCK(&hc_head->hch_mtx); 343122922Sandre 344122922Sandre /* 345122922Sandre * If the bucket limit is reached reuse the least used element 346122922Sandre */ 347122922Sandre if (hc_head->hch_length >= tcp_hostcache.bucket_limit || 348122922Sandre tcp_hostcache.cache_count >= tcp_hostcache.cache_limit) { 349122922Sandre hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead); 350122922Sandre /* 351122922Sandre * At first we were dropping the last element, just to 352122922Sandre * reaquire it in the next two lines again which ain't 353122922Sandre * very efficient. Instead just reuse the least used element. 354123028Sandre * Maybe we drop something that is still "in-use" but we can 355122922Sandre * be "lossy". 356122922Sandre */ 357122922Sandre TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q); 358122922Sandre tcp_hostcache.hashbase[hash].hch_length--; 359122922Sandre tcp_hostcache.cache_count--; 360123028Sandre tcpstat.tcps_hc_bucketoverflow++; 361123028Sandre#if 0 362123028Sandre uma_zfree(tcp_hostcache.zone, hc_entry); 363122922Sandre#endif 364122922Sandre } else { 365122922Sandre /* 366122922Sandre * Allocate a new entry, or balk if not possible 367122922Sandre */ 368122922Sandre hc_entry = uma_zalloc(tcp_hostcache.zone, M_NOWAIT); 369122922Sandre if (hc_entry == NULL) { 370122922Sandre THC_UNLOCK(&hc_head->hch_mtx); 371122922Sandre return NULL; 372122922Sandre } 373122922Sandre } 374122922Sandre 375122922Sandre /* 376122922Sandre * Initialize basic information of hostcache entry 377122922Sandre */ 378122922Sandre bzero(hc_entry, sizeof(*hc_entry)); 379122922Sandre if (inc->inc_isipv6) 380123113Sandre bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6)); 381122922Sandre else 382122922Sandre hc_entry->ip4 = inc->inc_faddr; 383122922Sandre hc_entry->rmx_head = hc_head; 384122922Sandre hc_entry->rmx_expire = tcp_hostcache.expire; 385122922Sandre 386122922Sandre /* 387122922Sandre * Put it upfront 388122922Sandre */ 389122922Sandre TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q); 390122922Sandre tcp_hostcache.hashbase[hash].hch_length++; 391122922Sandre tcp_hostcache.cache_count++; 392122922Sandre tcpstat.tcps_hc_added++; 393122922Sandre 394122922Sandre return hc_entry; 395122922Sandre} 396122922Sandre 397122922Sandre/* 398122922Sandre * External function: lookup an entry in the hostcache and fill out the 399122922Sandre * supplied tcp metrics structure. Fills in null when no entry was found 400122922Sandre * or a value is not set. 401122922Sandre */ 402122922Sandrevoid 403122922Sandretcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite) 404122922Sandre{ 405122922Sandre struct hc_metrics *hc_entry; 406122922Sandre 407122922Sandre /* 408122922Sandre * Find the right bucket 409122922Sandre */ 410122922Sandre hc_entry = tcp_hc_lookup(inc); 411122922Sandre 412122922Sandre /* 413122922Sandre * If we don't have an existing object 414122922Sandre */ 415122922Sandre if (hc_entry == NULL) { 416122922Sandre bzero(hc_metrics_lite, sizeof(*hc_metrics_lite)); 417122922Sandre return; 418122922Sandre } 419122922Sandre hc_entry->rmx_hits++; 420122922Sandre hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */ 421122922Sandre 422122922Sandre hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu; 423122922Sandre hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh; 424122922Sandre hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt; 425122922Sandre hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar; 426122922Sandre hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth; 427122922Sandre hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd; 428122922Sandre hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe; 429122922Sandre hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe; 430122922Sandre 431122922Sandre /* 432122922Sandre * unlock bucket row 433122922Sandre */ 434122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 435122922Sandre} 436122922Sandre 437122922Sandre/* 438122922Sandre * External function: lookup an entry in the hostcache and return the 439122922Sandre * discovered path mtu. Returns null if no entry found or value not is set. 440122922Sandre */ 441122922Sandreu_long 442122922Sandretcp_hc_getmtu(struct in_conninfo *inc) 443122922Sandre{ 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++; 452122922Sandre hc_entry->rmx_expire = 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/* 460122922Sandre * External function: lookup an entry in the hostcache and fill out the 461122922Sandre * supplied t/tcp tao structure. Fills in null when no entry was found 462122922Sandre * or a value is not set. 463122922Sandre */ 464122922Sandrevoid 465122922Sandretcp_hc_gettao(struct in_conninfo *inc, struct rmxp_tao *tao) 466122922Sandre{ 467122922Sandre struct hc_metrics *hc_entry; 468122922Sandre 469122922Sandre hc_entry = tcp_hc_lookup(inc); 470122922Sandre if (hc_entry == NULL) { 471122922Sandre bzero(tao, sizeof(*tao)); 472122922Sandre return; 473122922Sandre } 474122922Sandre hc_entry->rmx_hits++; 475122922Sandre hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */ 476122922Sandre 477123113Sandre bcopy(&hc_entry->rmx_tao, tao, sizeof(*tao)); 478122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 479122922Sandre} 480122922Sandre 481122922Sandre/* 482122922Sandre * External function: update the mtu value of an entry in the hostcache. 483122922Sandre * Creates a new entry if none was found. 484122922Sandre */ 485122922Sandrevoid 486122922Sandretcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu) 487122922Sandre{ 488122922Sandre struct hc_metrics *hc_entry; 489122922Sandre 490122922Sandre /* 491122922Sandre * Find the right bucket 492122922Sandre */ 493122922Sandre hc_entry = tcp_hc_lookup(inc); 494122922Sandre 495122922Sandre /* 496122922Sandre * If we don't have an existing object try to insert a new one 497122922Sandre */ 498122922Sandre if (hc_entry == NULL) { 499122922Sandre hc_entry = tcp_hc_insert(inc); 500122922Sandre if (hc_entry == NULL) 501122922Sandre return; 502122922Sandre } 503122922Sandre hc_entry->rmx_updates++; 504122922Sandre hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */ 505122922Sandre 506122922Sandre hc_entry->rmx_mtu = mtu; 507122922Sandre 508122922Sandre /* 509122922Sandre * put it upfront so we find it faster next time 510122922Sandre */ 511122922Sandre TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 512122922Sandre TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 513122922Sandre 514122922Sandre /* 515122922Sandre * unlock bucket row 516122922Sandre */ 517122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 518122922Sandre} 519122922Sandre 520122922Sandre/* 521122922Sandre * External function: update the tcp metrics of an entry in the hostcache. 522122922Sandre * Creates a new entry if none was found. 523122922Sandre */ 524122922Sandrevoid 525122922Sandretcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml) 526122922Sandre{ 527122922Sandre struct hc_metrics *hc_entry; 528122922Sandre 529122922Sandre hc_entry = tcp_hc_lookup(inc); 530122922Sandre if (hc_entry == NULL) { 531122922Sandre hc_entry = tcp_hc_insert(inc); 532122922Sandre if (hc_entry == NULL) 533122922Sandre return; 534122922Sandre } 535122922Sandre hc_entry->rmx_updates++; 536122922Sandre hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */ 537122922Sandre 538122922Sandre if (hcml->rmx_rtt != 0) { 539122922Sandre if (hc_entry->rmx_rtt == 0) 540122922Sandre hc_entry->rmx_rtt = hcml->rmx_rtt; 541122922Sandre else 542122922Sandre hc_entry->rmx_rtt = 543122922Sandre (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2; 544122922Sandre tcpstat.tcps_cachedrtt++; 545122922Sandre } 546122922Sandre if (hcml->rmx_rttvar != 0) { 547122922Sandre if (hc_entry->rmx_rttvar == 0) 548122922Sandre hc_entry->rmx_rttvar = hcml->rmx_rttvar; 549122922Sandre else 550122922Sandre hc_entry->rmx_rttvar = 551122922Sandre (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2; 552122922Sandre tcpstat.tcps_cachedrttvar++; 553122922Sandre } 554122922Sandre if (hcml->rmx_ssthresh != 0) { 555122922Sandre if (hc_entry->rmx_ssthresh == 0) 556122922Sandre hc_entry->rmx_ssthresh = hcml->rmx_ssthresh; 557122922Sandre else 558122922Sandre hc_entry->rmx_ssthresh = 559122922Sandre (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2; 560122922Sandre tcpstat.tcps_cachedssthresh++; 561122922Sandre } 562122922Sandre if (hcml->rmx_bandwidth != 0) { 563122922Sandre if (hc_entry->rmx_bandwidth == 0) 564122922Sandre hc_entry->rmx_bandwidth = hcml->rmx_bandwidth; 565122922Sandre else 566122922Sandre hc_entry->rmx_bandwidth = 567122922Sandre (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2; 568122922Sandre /* tcpstat.tcps_cachedbandwidth++; */ 569122922Sandre } 570122922Sandre if (hcml->rmx_cwnd != 0) { 571122922Sandre if (hc_entry->rmx_cwnd == 0) 572122922Sandre hc_entry->rmx_cwnd = hcml->rmx_cwnd; 573122922Sandre else 574122922Sandre hc_entry->rmx_cwnd = 575122922Sandre (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2; 576122922Sandre /* tcpstat.tcps_cachedcwnd++; */ 577122922Sandre } 578122922Sandre if (hcml->rmx_sendpipe != 0) { 579122922Sandre if (hc_entry->rmx_sendpipe == 0) 580122922Sandre hc_entry->rmx_sendpipe = hcml->rmx_sendpipe; 581122922Sandre else 582122922Sandre hc_entry->rmx_sendpipe = 583122922Sandre (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2; 584122922Sandre /* tcpstat.tcps_cachedsendpipe++; */ 585122922Sandre } 586122922Sandre if (hcml->rmx_recvpipe != 0) { 587122922Sandre if (hc_entry->rmx_recvpipe == 0) 588122922Sandre hc_entry->rmx_recvpipe = hcml->rmx_recvpipe; 589122922Sandre else 590122922Sandre hc_entry->rmx_recvpipe = 591122922Sandre (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2; 592122922Sandre /* tcpstat.tcps_cachedrecvpipe++; */ 593122922Sandre } 594122922Sandre 595122922Sandre TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 596122922Sandre TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 597122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 598122922Sandre} 599122922Sandre 600122922Sandre/* 601122922Sandre * External function: update the t/tcp tao of an entry in the hostcache. 602122922Sandre * Creates a new entry if none was found. 603122922Sandre */ 604122922Sandrevoid 605122922Sandretcp_hc_updatetao(struct in_conninfo *inc, int field, tcp_cc ccount, u_short mss) 606122922Sandre{ 607122922Sandre struct hc_metrics *hc_entry; 608122922Sandre 609122922Sandre hc_entry = tcp_hc_lookup(inc); 610122922Sandre if (hc_entry == NULL) { 611122922Sandre hc_entry = tcp_hc_insert(inc); 612122922Sandre if (hc_entry == NULL) 613122922Sandre return; 614122922Sandre } 615122922Sandre hc_entry->rmx_updates++; 616122922Sandre hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */ 617122922Sandre 618122922Sandre switch(field) { 619122922Sandre case TCP_HC_TAO_CC: 620122922Sandre hc_entry->rmx_tao.tao_cc = ccount; 621122922Sandre break; 622122922Sandre 623122922Sandre case TCP_HC_TAO_CCSENT: 624122922Sandre hc_entry->rmx_tao.tao_ccsent = ccount; 625122922Sandre break; 626122922Sandre 627122922Sandre case TCP_HC_TAO_MSSOPT: 628122922Sandre hc_entry->rmx_tao.tao_mssopt = mss; 629122922Sandre break; 630122922Sandre } 631122922Sandre 632122922Sandre TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 633122922Sandre TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q); 634122922Sandre THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 635122922Sandre} 636122922Sandre 637122922Sandre/* 638122922Sandre * Sysctl function: prints the list and values of all hostcache entries in 639122922Sandre * unsorted order. 640122922Sandre */ 641122922Sandrestatic int 642122922Sandresysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS) 643122922Sandre{ 644122922Sandre int bufsize; 645122922Sandre int linesize = 128; 646122922Sandre char *p, *buf; 647122922Sandre int len, i, error; 648122922Sandre struct hc_metrics *hc_entry; 649122922Sandre 650122922Sandre bufsize = linesize * (tcp_hostcache.cache_count + 1); 651122922Sandre 652122922Sandre p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO); 653122922Sandre 654122922Sandre len = snprintf(p, linesize, 655122922Sandre "\nIP address MTU SSTRESH RTT RTTVAR BANDWIDTH " 656122922Sandre " CWND SENDPIPE RECVPIPE HITS UPD EXP\n"); 657122922Sandre p += len; 658122922Sandre 659122922Sandre#define msec(u) (((u) + 500) / 1000) 660122922Sandre for (i = 0; i < tcp_hostcache.hashsize; i++) { 661122922Sandre THC_LOCK(&tcp_hostcache.hashbase[i].hch_mtx); 662122922Sandre TAILQ_FOREACH(hc_entry, &tcp_hostcache.hashbase[i].hch_bucket, 663122922Sandre rmx_q) { 664122922Sandre len = snprintf(p, linesize, 665122922Sandre "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu " 666122922Sandre "%4lu %4lu %4i\n", 667122922Sandre hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) : 668122922Sandre#ifdef INET6 669122922Sandre ip6_sprintf(&hc_entry->ip6), 670122922Sandre#else 671122922Sandre "IPv6?", 672122922Sandre#endif 673122922Sandre hc_entry->rmx_mtu, 674122922Sandre hc_entry->rmx_ssthresh, 675122922Sandre msec(hc_entry->rmx_rtt * 676122922Sandre (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))), 677122922Sandre msec(hc_entry->rmx_rttvar * 678122922Sandre (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))), 679133477Sandre hc_entry->rmx_bandwidth * 8, 680122922Sandre hc_entry->rmx_cwnd, 681122922Sandre hc_entry->rmx_sendpipe, 682122922Sandre hc_entry->rmx_recvpipe, 683122922Sandre hc_entry->rmx_hits, 684122922Sandre hc_entry->rmx_updates, 685122922Sandre hc_entry->rmx_expire); 686122922Sandre p += len; 687122922Sandre } 688122922Sandre THC_UNLOCK(&tcp_hostcache.hashbase[i].hch_mtx); 689122922Sandre } 690122922Sandre#undef msec 691122922Sandre error = SYSCTL_OUT(req, buf, p - buf); 692122922Sandre free(buf, M_TEMP); 693122922Sandre return(error); 694122922Sandre} 695122922Sandre 696122922Sandre/* 697122922Sandre * Expire and purge (old|all) entries in the tcp_hostcache. Runs periodically 698122922Sandre * from the callout. 699122922Sandre */ 700122922Sandrestatic void 701122922Sandretcp_hc_purge(void *arg) 702122922Sandre{ 703128574Sandre struct hc_metrics *hc_entry, *hc_next; 704122922Sandre int all = (intptr_t)arg; 705122922Sandre int i; 706122922Sandre 707122922Sandre if (tcp_hostcache.purgeall) { 708122922Sandre all = 1; 709122922Sandre tcp_hostcache.purgeall = 0; 710122922Sandre } 711122922Sandre 712122922Sandre for (i = 0; i < tcp_hostcache.hashsize; i++) { 713122922Sandre THC_LOCK(&tcp_hostcache.hashbase[i].hch_mtx); 714128574Sandre TAILQ_FOREACH_SAFE(hc_entry, &tcp_hostcache.hashbase[i].hch_bucket, 715128574Sandre rmx_q, hc_next) { 716122922Sandre if (all || hc_entry->rmx_expire <= 0) { 717122922Sandre TAILQ_REMOVE(&tcp_hostcache.hashbase[i].hch_bucket, 718122922Sandre hc_entry, rmx_q); 719122922Sandre uma_zfree(tcp_hostcache.zone, hc_entry); 720122922Sandre tcp_hostcache.hashbase[i].hch_length--; 721122922Sandre tcp_hostcache.cache_count--; 722122922Sandre } else 723122922Sandre hc_entry->rmx_expire -= TCP_HOSTCACHE_PRUNE; 724122922Sandre } 725122922Sandre THC_UNLOCK(&tcp_hostcache.hashbase[i].hch_mtx); 726122922Sandre } 727122922Sandre callout_reset(&tcp_hc_callout, TCP_HOSTCACHE_PRUNE * hz, tcp_hc_purge, 0); 728122922Sandre} 729