xref: /freebsd/sys/netinet/tcp_hostcache.c (revision cd6c4060c8801b7b1c1e74ffbc7aedc37c603fe8)
197d8d152SAndre Oppermann /*
297d8d152SAndre Oppermann  * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG
397d8d152SAndre Oppermann  * All rights reserved.
497d8d152SAndre Oppermann  *
597d8d152SAndre Oppermann  * Redistribution and use in source and binary forms, with or without
697d8d152SAndre Oppermann  * modification, are permitted provided that the following conditions
797d8d152SAndre Oppermann  * are met:
897d8d152SAndre Oppermann  * 1. Redistributions of source code must retain the above copyright
997d8d152SAndre Oppermann  *    notice, this list of conditions and the following disclaimer.
1097d8d152SAndre Oppermann  * 2. Redistributions in binary form must reproduce the above copyright
1197d8d152SAndre Oppermann  *    notice, this list of conditions and the following disclaimer in the
1297d8d152SAndre Oppermann  *    documentation and/or other materials provided with the distribution.
1397d8d152SAndre Oppermann  * 3. The name of the author may not be used to endorse or promote
1497d8d152SAndre Oppermann  *    products derived from this software without specific prior written
1597d8d152SAndre Oppermann  *    permission.
1697d8d152SAndre Oppermann  *
1797d8d152SAndre Oppermann  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1897d8d152SAndre Oppermann  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1997d8d152SAndre Oppermann  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2097d8d152SAndre Oppermann  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2197d8d152SAndre Oppermann  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2297d8d152SAndre Oppermann  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2397d8d152SAndre Oppermann  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2497d8d152SAndre Oppermann  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2597d8d152SAndre Oppermann  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2697d8d152SAndre Oppermann  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2797d8d152SAndre Oppermann  * SUCH DAMAGE.
2897d8d152SAndre Oppermann  *
2997d8d152SAndre Oppermann  * $FreeBSD$
3097d8d152SAndre Oppermann  */
3197d8d152SAndre Oppermann 
3297d8d152SAndre Oppermann /*
3397d8d152SAndre Oppermann  * The tcp_hostcache moves the tcp specific cached metrics from the routing
3497d8d152SAndre Oppermann  * table into a dedicated structure indexed by the remote IP address. It
3597d8d152SAndre Oppermann  * keeps information on the measured tcp parameters of past tcp sessions
3697d8d152SAndre Oppermann  * to have better initial start values for following connections from the
3797d8d152SAndre Oppermann  * same source. Depending on the network parameters (delay, bandwidth, max
3897d8d152SAndre Oppermann  * MTU, congestion window) between local and remote site this can lead to
3997d8d152SAndre Oppermann  * significant speedups for new tcp connections after the first one.
4097d8d152SAndre Oppermann  *
4197d8d152SAndre Oppermann  * Due to this new tcp_hostcache all tcp specific metrics information in
4297d8d152SAndre Oppermann  * the routing table has been removed. The INPCB no longer keeps a pointer
4397d8d152SAndre Oppermann  * to the routing entry and protocol initiated route cloning has been
4497d8d152SAndre Oppermann  * removed as well. With these changes the routing table has gone back
4597d8d152SAndre Oppermann  * to being more lightwight and only carries information related to packet
4697d8d152SAndre Oppermann  * forwarding.
4797d8d152SAndre Oppermann  *
4897d8d152SAndre Oppermann  * Tcp_hostcache is designed for multiple concurrent access in SMP
4997d8d152SAndre Oppermann  * environments and high contention. All bucket rows have their own
5097d8d152SAndre Oppermann  * lock and thus multiple lookups and modifies can be done at the same
5197d8d152SAndre Oppermann  * time as long as they are in different bucket rows. If a request for
5297d8d152SAndre Oppermann  * insertion of a new record can't be satisfied it simply returns an
5397d8d152SAndre Oppermann  * empty structure. Nobody and nothing shall ever point directly to
5497d8d152SAndre Oppermann  * any entry in tcp_hostcache. All communication is done in an object
5597d8d152SAndre Oppermann  * oriented way and only funtions of tcp_hostcache will manipulate hostcache
5697d8d152SAndre Oppermann  * entries. Otherwise we are unable to achieve good behaviour in concurrent
5797d8d152SAndre Oppermann  * access situations. Since tcp_hostcache is only caching information there
5897d8d152SAndre Oppermann  * are no fatal consequences if we either can't satisfy any particular request
5997d8d152SAndre Oppermann  * or have to drop/overwrite an existing entry because of bucket limit
6097d8d152SAndre Oppermann  * memory constrains.
6197d8d152SAndre Oppermann  */
6297d8d152SAndre Oppermann 
6397d8d152SAndre Oppermann /*
6497d8d152SAndre Oppermann  * Many thanks to jlemon for basic structure of tcp_syncache which is being
6597d8d152SAndre Oppermann  * followed here.
6697d8d152SAndre Oppermann  */
6797d8d152SAndre Oppermann 
6897d8d152SAndre Oppermann #include "opt_inet6.h"
6997d8d152SAndre Oppermann 
7097d8d152SAndre Oppermann #include <sys/param.h>
7197d8d152SAndre Oppermann #include <sys/systm.h>
7297d8d152SAndre Oppermann #include <sys/kernel.h>
7397d8d152SAndre Oppermann #include <sys/lock.h>
7497d8d152SAndre Oppermann #include <sys/mutex.h>
7597d8d152SAndre Oppermann #include <sys/malloc.h>
7697d8d152SAndre Oppermann #include <sys/socket.h>
7797d8d152SAndre Oppermann #include <sys/socketvar.h>
7897d8d152SAndre Oppermann #include <sys/sysctl.h>
7997d8d152SAndre Oppermann 
8097d8d152SAndre Oppermann #include <net/if.h>
8197d8d152SAndre Oppermann 
8297d8d152SAndre Oppermann #include <netinet/in.h>
8397d8d152SAndre Oppermann #include <netinet/in_systm.h>
8497d8d152SAndre Oppermann #include <netinet/ip.h>
8597d8d152SAndre Oppermann #include <netinet/in_var.h>
8697d8d152SAndre Oppermann #include <netinet/in_pcb.h>
8797d8d152SAndre Oppermann #include <netinet/ip_var.h>
8897d8d152SAndre Oppermann #ifdef INET6
8997d8d152SAndre Oppermann #include <netinet/ip6.h>
9097d8d152SAndre Oppermann #include <netinet6/ip6_var.h>
9197d8d152SAndre Oppermann #endif
9297d8d152SAndre Oppermann #include <netinet/tcp.h>
9397d8d152SAndre Oppermann #include <netinet/tcp_var.h>
9497d8d152SAndre Oppermann #ifdef INET6
9597d8d152SAndre Oppermann #include <netinet6/tcp6_var.h>
9697d8d152SAndre Oppermann #endif
9797d8d152SAndre Oppermann 
9897d8d152SAndre Oppermann #include <vm/uma.h>
9997d8d152SAndre Oppermann 
10097d8d152SAndre Oppermann 
10197d8d152SAndre Oppermann TAILQ_HEAD(hc_qhead, hc_metrics);
10297d8d152SAndre Oppermann 
10397d8d152SAndre Oppermann struct hc_head {
10497d8d152SAndre Oppermann 	struct hc_qhead	hch_bucket;
10597d8d152SAndre Oppermann 	u_int		hch_length;
10697d8d152SAndre Oppermann 	struct mtx	hch_mtx;
10797d8d152SAndre Oppermann };
10897d8d152SAndre Oppermann 
10997d8d152SAndre Oppermann struct hc_metrics {
11097d8d152SAndre Oppermann 	/* housekeeping */
11197d8d152SAndre Oppermann 	TAILQ_ENTRY(hc_metrics) rmx_q;
11297d8d152SAndre Oppermann 	struct	hc_head *rmx_head; /* head of bucket tail queue */
11397d8d152SAndre Oppermann 	struct	in_addr ip4;	/* IP address */
11497d8d152SAndre Oppermann 	struct	in6_addr ip6;	/* IP6 address */
11597d8d152SAndre Oppermann 	/* endpoint specific values for tcp */
11697d8d152SAndre Oppermann 	u_long	rmx_mtu;	/* MTU for this path */
11797d8d152SAndre Oppermann 	u_long	rmx_ssthresh;	/* outbound gateway buffer limit */
11897d8d152SAndre Oppermann 	u_long	rmx_rtt;	/* estimated round trip time */
11997d8d152SAndre Oppermann 	u_long	rmx_rttvar;	/* estimated rtt variance */
12097d8d152SAndre Oppermann 	u_long	rmx_bandwidth;	/* estimated bandwidth */
12197d8d152SAndre Oppermann 	u_long	rmx_cwnd;	/* congestion window */
12297d8d152SAndre Oppermann 	u_long	rmx_sendpipe;	/* outbound delay-bandwidth product */
12397d8d152SAndre Oppermann 	u_long	rmx_recvpipe;	/* inbound delay-bandwidth product */
12497d8d152SAndre Oppermann 	struct	rmxp_tao rmx_tao; /* TAO cache for T/TCP */
12597d8d152SAndre Oppermann 	/* tcp hostcache internal data */
12697d8d152SAndre Oppermann 	int	rmx_expire;	/* lifetime for object */
12797d8d152SAndre Oppermann 	u_long	rmx_hits;	/* number of hits */
12897d8d152SAndre Oppermann 	u_long	rmx_updates;	/* number of updates */
12997d8d152SAndre Oppermann };
13097d8d152SAndre Oppermann 
13197d8d152SAndre Oppermann /* Arbitrary values */
13297d8d152SAndre Oppermann #define TCP_HOSTCACHE_HASHSIZE		512
13397d8d152SAndre Oppermann #define TCP_HOSTCACHE_BUCKETLIMIT	30
13497d8d152SAndre Oppermann #define TCP_HOSTCACHE_EXPIRE		60*60	/* one hour */
13597d8d152SAndre Oppermann #define TCP_HOSTCACHE_PRUNE		5*60	/* every 5 minutes */
13697d8d152SAndre Oppermann 
13797d8d152SAndre Oppermann struct tcp_hostcache {
13897d8d152SAndre Oppermann 	struct	hc_head *hashbase;
13997d8d152SAndre Oppermann 	uma_zone_t zone;
14097d8d152SAndre Oppermann 	u_int	hashsize;
14197d8d152SAndre Oppermann 	u_int	hashmask;
14297d8d152SAndre Oppermann 	u_int	bucket_limit;
14397d8d152SAndre Oppermann 	u_int	cache_count;
14497d8d152SAndre Oppermann 	u_int	cache_limit;
14597d8d152SAndre Oppermann 	int	expire;
14697d8d152SAndre Oppermann 	int	purgeall;
14797d8d152SAndre Oppermann };
14897d8d152SAndre Oppermann static struct tcp_hostcache tcp_hostcache;
14997d8d152SAndre Oppermann 
15097d8d152SAndre Oppermann static struct callout tcp_hc_callout;
15197d8d152SAndre Oppermann 
15297d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
15397d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
15497d8d152SAndre Oppermann static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
15597d8d152SAndre Oppermann static void tcp_hc_purge(void *);
15697d8d152SAndre Oppermann 
15797d8d152SAndre Oppermann SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0, "TCP Host cache");
15897d8d152SAndre Oppermann 
15997d8d152SAndre Oppermann SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_RDTUN,
16097d8d152SAndre Oppermann      &tcp_hostcache.cache_limit, 0, "Overall entry limit for hostcache");
16197d8d152SAndre Oppermann 
16297d8d152SAndre Oppermann SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_RDTUN,
16397d8d152SAndre Oppermann      &tcp_hostcache.hashsize, 0, "Size of TCP hostcache hashtable");
16497d8d152SAndre Oppermann 
16597d8d152SAndre Oppermann SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, CTLFLAG_RDTUN,
16697d8d152SAndre Oppermann      &tcp_hostcache.bucket_limit, 0, "Per-bucket hash limit for hostcache");
16797d8d152SAndre Oppermann 
16897d8d152SAndre Oppermann SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_RD,
16997d8d152SAndre Oppermann      &tcp_hostcache.cache_count, 0, "Current number of entries in hostcache");
17097d8d152SAndre Oppermann 
17197d8d152SAndre Oppermann SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_RW,
17297d8d152SAndre Oppermann      &tcp_hostcache.expire, 0, "Expire time of TCP hostcache entries");
17397d8d152SAndre Oppermann 
17497d8d152SAndre Oppermann SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_RW,
17597d8d152SAndre Oppermann      &tcp_hostcache.purgeall, 0, "Expire all entires on next purge run");
17697d8d152SAndre Oppermann 
17797d8d152SAndre Oppermann SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
17897d8d152SAndre Oppermann 	CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
17997d8d152SAndre Oppermann 	sysctl_tcp_hc_list, "A", "List of all hostcache entries");
18097d8d152SAndre Oppermann 
18197d8d152SAndre Oppermann 
18297d8d152SAndre Oppermann static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
18397d8d152SAndre Oppermann 
18497d8d152SAndre Oppermann #define HOSTCACHE_HASH(ip) \
18597d8d152SAndre Oppermann 	(((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) & 	\
18697d8d152SAndre Oppermann 	  tcp_hostcache.hashmask)
18797d8d152SAndre Oppermann 
18897d8d152SAndre Oppermann /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
18997d8d152SAndre Oppermann #define HOSTCACHE_HASH6(ip6)	 			\
19097d8d152SAndre Oppermann 	(((ip6)->s6_addr32[0] ^				\
19197d8d152SAndre Oppermann 	  (ip6)->s6_addr32[1] ^				\
19297d8d152SAndre Oppermann 	  (ip6)->s6_addr32[2] ^				\
19397d8d152SAndre Oppermann 	  (ip6)->s6_addr32[3]) &			\
19497d8d152SAndre Oppermann 	 tcp_hostcache.hashmask)
19597d8d152SAndre Oppermann 
19697d8d152SAndre Oppermann #define THC_LOCK(lp)		mtx_lock(lp)
19797d8d152SAndre Oppermann #define THC_UNLOCK(lp)		mtx_unlock(lp)
19897d8d152SAndre Oppermann 
19997d8d152SAndre Oppermann void
20097d8d152SAndre Oppermann tcp_hc_init(void)
20197d8d152SAndre Oppermann {
20297d8d152SAndre Oppermann 	int i;
20397d8d152SAndre Oppermann 
20497d8d152SAndre Oppermann 	/*
20597d8d152SAndre Oppermann 	 * Initialize hostcache structures
20697d8d152SAndre Oppermann 	 */
20797d8d152SAndre Oppermann 	tcp_hostcache.cache_count = 0;
20897d8d152SAndre Oppermann 	tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
20997d8d152SAndre Oppermann 	tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
21097d8d152SAndre Oppermann 	tcp_hostcache.cache_limit =
21197d8d152SAndre Oppermann 	    tcp_hostcache.hashsize * tcp_hostcache.bucket_limit;
21297d8d152SAndre Oppermann 	tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
21397d8d152SAndre Oppermann 
21497d8d152SAndre Oppermann         TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
21597d8d152SAndre Oppermann 	    &tcp_hostcache.hashsize);
21697d8d152SAndre Oppermann         TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
21797d8d152SAndre Oppermann 	    &tcp_hostcache.cache_limit);
21897d8d152SAndre Oppermann         TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
21997d8d152SAndre Oppermann 	    &tcp_hostcache.bucket_limit);
22097d8d152SAndre Oppermann 	if (!powerof2(tcp_hostcache.hashsize)) {
22197d8d152SAndre Oppermann                 printf("WARNING: hostcache hash size is not a power of 2.\n");
22297d8d152SAndre Oppermann 		tcp_hostcache.hashsize = 512;	/* safe default */
22397d8d152SAndre Oppermann         }
22497d8d152SAndre Oppermann 	tcp_hostcache.hashmask = tcp_hostcache.hashsize - 1;
22597d8d152SAndre Oppermann 
22697d8d152SAndre Oppermann 	/*
22797d8d152SAndre Oppermann 	 * Allocate the hash table
22897d8d152SAndre Oppermann 	 */
22997d8d152SAndre Oppermann 	tcp_hostcache.hashbase = (struct hc_head *)
23097d8d152SAndre Oppermann 	    malloc(tcp_hostcache.hashsize * sizeof(struct hc_head),
23197d8d152SAndre Oppermann 		   M_HOSTCACHE, M_WAITOK | M_ZERO);
23297d8d152SAndre Oppermann 
23397d8d152SAndre Oppermann 	/*
23497d8d152SAndre Oppermann 	 * Initialize the hash buckets
23597d8d152SAndre Oppermann 	 */
23697d8d152SAndre Oppermann 	for (i = 0; i < tcp_hostcache.hashsize; i++) {
23797d8d152SAndre Oppermann 		TAILQ_INIT(&tcp_hostcache.hashbase[i].hch_bucket);
23897d8d152SAndre Oppermann 		tcp_hostcache.hashbase[i].hch_length = 0;
23997d8d152SAndre Oppermann 		mtx_init(&tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
24097d8d152SAndre Oppermann 			  NULL, MTX_DEF);
24197d8d152SAndre Oppermann 	}
24297d8d152SAndre Oppermann 
24397d8d152SAndre Oppermann 	/*
24497d8d152SAndre Oppermann 	 * Allocate the hostcache entries.
24597d8d152SAndre Oppermann 	 */
24697d8d152SAndre Oppermann 	tcp_hostcache.zone = uma_zcreate("hostcache", sizeof(struct hc_metrics),
24797d8d152SAndre Oppermann 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
24897d8d152SAndre Oppermann 	uma_zone_set_max(tcp_hostcache.zone, tcp_hostcache.cache_limit);
24997d8d152SAndre Oppermann 
25097d8d152SAndre Oppermann 	/*
25197d8d152SAndre Oppermann 	 * Set up periodic cache cleanup.
25297d8d152SAndre Oppermann 	 */
25397d8d152SAndre Oppermann 	callout_init(&tcp_hc_callout, CALLOUT_MPSAFE);
25497d8d152SAndre Oppermann 	callout_reset(&tcp_hc_callout, TCP_HOSTCACHE_PRUNE * hz, tcp_hc_purge, 0);
25597d8d152SAndre Oppermann }
25697d8d152SAndre Oppermann 
25797d8d152SAndre Oppermann /*
25897d8d152SAndre Oppermann  * Internal function: lookup an entry in the hostcache or return NULL.
25997d8d152SAndre Oppermann  *
26097d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
26197d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
26297d8d152SAndre Oppermann  */
26397d8d152SAndre Oppermann static struct hc_metrics *
26497d8d152SAndre Oppermann tcp_hc_lookup(struct in_conninfo *inc)
26597d8d152SAndre Oppermann {
26697d8d152SAndre Oppermann 	int hash;
26797d8d152SAndre Oppermann 	struct hc_head *hc_head;
26897d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
26997d8d152SAndre Oppermann 
27097d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
27197d8d152SAndre Oppermann 
27297d8d152SAndre Oppermann 	/*
27397d8d152SAndre Oppermann 	 * Hash the foreign ip address.
27497d8d152SAndre Oppermann 	 */
27597d8d152SAndre Oppermann 	if (inc->inc_isipv6)
27697d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
27797d8d152SAndre Oppermann 	else
27897d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
27997d8d152SAndre Oppermann 
28097d8d152SAndre Oppermann 	hc_head = &tcp_hostcache.hashbase[hash];
28197d8d152SAndre Oppermann 
28297d8d152SAndre Oppermann 	/*
28397d8d152SAndre Oppermann 	 * aquire lock for this bucket row
28497d8d152SAndre Oppermann 	 * we release the lock if we don't find an entry,
28597d8d152SAndre Oppermann 	 * otherwise the caller has to unlock after he is done
28697d8d152SAndre Oppermann 	 */
28797d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
28897d8d152SAndre Oppermann 
28997d8d152SAndre Oppermann 	/*
29097d8d152SAndre Oppermann 	 * circle through entries in bucket row looking for a match
29197d8d152SAndre Oppermann 	 */
29297d8d152SAndre Oppermann 	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
29397d8d152SAndre Oppermann 		if (inc->inc_isipv6) {
29497d8d152SAndre Oppermann 			if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
29597d8d152SAndre Oppermann 			    sizeof(inc->inc6_faddr)) == 0)
29697d8d152SAndre Oppermann 				return hc_entry;
29797d8d152SAndre Oppermann 		} else {
29897d8d152SAndre Oppermann 			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
29997d8d152SAndre Oppermann 			    sizeof(inc->inc_faddr)) == 0)
30097d8d152SAndre Oppermann 				return hc_entry;
30197d8d152SAndre Oppermann 		}
30297d8d152SAndre Oppermann 	}
30397d8d152SAndre Oppermann 
30497d8d152SAndre Oppermann 	/*
30597d8d152SAndre Oppermann 	 * We were unsuccessful and didn't find anything
30697d8d152SAndre Oppermann 	 */
30797d8d152SAndre Oppermann 	THC_UNLOCK(&hc_head->hch_mtx);
30897d8d152SAndre Oppermann 	return NULL;
30997d8d152SAndre Oppermann }
31097d8d152SAndre Oppermann 
31197d8d152SAndre Oppermann /*
31297d8d152SAndre Oppermann  * Internal function: insert an entry into the hostcache or return NULL
31397d8d152SAndre Oppermann  * if unable to allocate a new one.
31497d8d152SAndre Oppermann  *
31597d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
31697d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
31797d8d152SAndre Oppermann  */
31897d8d152SAndre Oppermann static struct hc_metrics *
31997d8d152SAndre Oppermann tcp_hc_insert(struct in_conninfo *inc)
32097d8d152SAndre Oppermann {
32197d8d152SAndre Oppermann 	int hash;
32297d8d152SAndre Oppermann 	struct hc_head *hc_head;
32397d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
32497d8d152SAndre Oppermann 
32597d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
32697d8d152SAndre Oppermann 
32797d8d152SAndre Oppermann 	/*
32897d8d152SAndre Oppermann 	 * Hash the foreign ip address
32997d8d152SAndre Oppermann 	 */
33097d8d152SAndre Oppermann 	if (inc->inc_isipv6)
33197d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
33297d8d152SAndre Oppermann 	else
33397d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
33497d8d152SAndre Oppermann 
33597d8d152SAndre Oppermann 	hc_head = &tcp_hostcache.hashbase[hash];
33697d8d152SAndre Oppermann 
33797d8d152SAndre Oppermann 	/*
33897d8d152SAndre Oppermann 	 * aquire lock for this bucket row
33997d8d152SAndre Oppermann 	 * we release the lock if we don't find an entry,
34097d8d152SAndre Oppermann 	 * otherwise the caller has to unlock after he is done
34197d8d152SAndre Oppermann 	 */
34297d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
34397d8d152SAndre Oppermann 
34497d8d152SAndre Oppermann 	/*
34597d8d152SAndre Oppermann 	 * If the bucket limit is reached reuse the least used element
34697d8d152SAndre Oppermann 	 */
34797d8d152SAndre Oppermann 	if (hc_head->hch_length >= tcp_hostcache.bucket_limit ||
34897d8d152SAndre Oppermann 	    tcp_hostcache.cache_count >= tcp_hostcache.cache_limit) {
34997d8d152SAndre Oppermann 		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
35097d8d152SAndre Oppermann 		/*
35197d8d152SAndre Oppermann 		 * At first we were dropping the last element, just to
35297d8d152SAndre Oppermann 		 * reaquire it in the next two lines again which ain't
35397d8d152SAndre Oppermann 		 * very efficient. Instead just reuse the least used element.
354cd6c4060SAndre Oppermann 		 * Maybe we drop something that is still "in-use" but we can
35597d8d152SAndre Oppermann 		 * be "lossy".
35697d8d152SAndre Oppermann 		 */
35797d8d152SAndre Oppermann 		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
35897d8d152SAndre Oppermann 		tcp_hostcache.hashbase[hash].hch_length--;
35997d8d152SAndre Oppermann 		tcp_hostcache.cache_count--;
36097d8d152SAndre Oppermann 		tcpstat.tcps_hc_bucketoverflow++;
361cd6c4060SAndre Oppermann #if 0
362cd6c4060SAndre Oppermann 		uma_zfree(tcp_hostcache.zone, hc_entry);
363cd6c4060SAndre Oppermann #endif
36497d8d152SAndre Oppermann 	} else {
36597d8d152SAndre Oppermann 		/*
36697d8d152SAndre Oppermann 		 * Allocate a new entry, or balk if not possible
36797d8d152SAndre Oppermann 		 */
36897d8d152SAndre Oppermann 		hc_entry = uma_zalloc(tcp_hostcache.zone, M_NOWAIT);
36997d8d152SAndre Oppermann 		if (hc_entry == NULL) {
37097d8d152SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
37197d8d152SAndre Oppermann 			return NULL;
37297d8d152SAndre Oppermann 		}
37397d8d152SAndre Oppermann 	}
37497d8d152SAndre Oppermann 
37597d8d152SAndre Oppermann 	/*
37697d8d152SAndre Oppermann 	 * Initialize basic information of hostcache entry
37797d8d152SAndre Oppermann 	 */
37897d8d152SAndre Oppermann 	bzero(hc_entry, sizeof(*hc_entry));
37997d8d152SAndre Oppermann 	if (inc->inc_isipv6)
38097d8d152SAndre Oppermann 		bcopy(&hc_entry->ip6, &inc->inc6_faddr, sizeof(hc_entry->ip6));
38197d8d152SAndre Oppermann 	else
38297d8d152SAndre Oppermann 		hc_entry->ip4 = inc->inc_faddr;
38397d8d152SAndre Oppermann 	hc_entry->rmx_head = hc_head;
38497d8d152SAndre Oppermann 	hc_entry->rmx_expire = tcp_hostcache.expire;
38597d8d152SAndre Oppermann 
38697d8d152SAndre Oppermann 	/*
38797d8d152SAndre Oppermann 	 * Put it upfront
38897d8d152SAndre Oppermann 	 */
38997d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
39097d8d152SAndre Oppermann 	tcp_hostcache.hashbase[hash].hch_length++;
39197d8d152SAndre Oppermann 	tcp_hostcache.cache_count++;
39297d8d152SAndre Oppermann 	tcpstat.tcps_hc_added++;
39397d8d152SAndre Oppermann 
39497d8d152SAndre Oppermann 	return hc_entry;
39597d8d152SAndre Oppermann }
39697d8d152SAndre Oppermann 
39797d8d152SAndre Oppermann /*
39897d8d152SAndre Oppermann  * External function: lookup an entry in the hostcache and fill out the
39997d8d152SAndre Oppermann  * supplied tcp metrics structure.  Fills in null when no entry was found
40097d8d152SAndre Oppermann  * or a value is not set.
40197d8d152SAndre Oppermann  */
40297d8d152SAndre Oppermann void
40397d8d152SAndre Oppermann tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
40497d8d152SAndre Oppermann {
40597d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
40697d8d152SAndre Oppermann 
40797d8d152SAndre Oppermann 	/*
40897d8d152SAndre Oppermann 	 * Find the right bucket
40997d8d152SAndre Oppermann 	 */
41097d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
41197d8d152SAndre Oppermann 
41297d8d152SAndre Oppermann 	/*
41397d8d152SAndre Oppermann 	 * If we don't have an existing object
41497d8d152SAndre Oppermann 	 */
41597d8d152SAndre Oppermann 	if (hc_entry == NULL) {
41697d8d152SAndre Oppermann 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
41797d8d152SAndre Oppermann 		return;
41897d8d152SAndre Oppermann 	}
41997d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
42097d8d152SAndre Oppermann 	hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
42197d8d152SAndre Oppermann 
42297d8d152SAndre Oppermann 	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
42397d8d152SAndre Oppermann 	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
42497d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
42597d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
42697d8d152SAndre Oppermann 	hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth;
42797d8d152SAndre Oppermann 	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
42897d8d152SAndre Oppermann 	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
42997d8d152SAndre Oppermann 	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
43097d8d152SAndre Oppermann 
43197d8d152SAndre Oppermann 	/*
43297d8d152SAndre Oppermann 	 * unlock bucket row
43397d8d152SAndre Oppermann 	 */
43497d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
43597d8d152SAndre Oppermann }
43697d8d152SAndre Oppermann 
43797d8d152SAndre Oppermann /*
43897d8d152SAndre Oppermann  * External function: lookup an entry in the hostcache and return the
43997d8d152SAndre Oppermann  * discovered path mtu.  Returns null if no entry found or value not is set.
44097d8d152SAndre Oppermann  */
44197d8d152SAndre Oppermann u_long
44297d8d152SAndre Oppermann tcp_hc_getmtu(struct in_conninfo *inc)
44397d8d152SAndre Oppermann {
44497d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
44597d8d152SAndre Oppermann 	u_long mtu;
44697d8d152SAndre Oppermann 
44797d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
44897d8d152SAndre Oppermann 	if (hc_entry == NULL) {
44997d8d152SAndre Oppermann 		return 0;
45097d8d152SAndre Oppermann 	}
45197d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
45297d8d152SAndre Oppermann 	hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
45397d8d152SAndre Oppermann 
45497d8d152SAndre Oppermann 	mtu = hc_entry->rmx_mtu;
45597d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
45697d8d152SAndre Oppermann 	return mtu;
45797d8d152SAndre Oppermann }
45897d8d152SAndre Oppermann 
45997d8d152SAndre Oppermann /*
46097d8d152SAndre Oppermann  * External function: lookup an entry in the hostcache and fill out the
46197d8d152SAndre Oppermann  * supplied t/tcp tao structure.  Fills in null when no entry was found
46297d8d152SAndre Oppermann  * or a value is not set.
46397d8d152SAndre Oppermann  */
46497d8d152SAndre Oppermann void
46597d8d152SAndre Oppermann tcp_hc_gettao(struct in_conninfo *inc, struct rmxp_tao *tao)
46697d8d152SAndre Oppermann {
46797d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
46897d8d152SAndre Oppermann 
46997d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
47097d8d152SAndre Oppermann 	if (hc_entry == NULL) {
47197d8d152SAndre Oppermann 		bzero(tao, sizeof(*tao));
47297d8d152SAndre Oppermann 		return;
47397d8d152SAndre Oppermann 	}
47497d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
47597d8d152SAndre Oppermann 	hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
47697d8d152SAndre Oppermann 
47797d8d152SAndre Oppermann 	bcopy(tao, &hc_entry->rmx_tao, sizeof(*tao));
47897d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
47997d8d152SAndre Oppermann }
48097d8d152SAndre Oppermann 
48197d8d152SAndre Oppermann /*
48297d8d152SAndre Oppermann  * External function: update the mtu value of an entry in the hostcache.
48397d8d152SAndre Oppermann  * Creates a new entry if none was found.
48497d8d152SAndre Oppermann  */
48597d8d152SAndre Oppermann void
48697d8d152SAndre Oppermann tcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
48797d8d152SAndre Oppermann {
48897d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
48997d8d152SAndre Oppermann 
49097d8d152SAndre Oppermann 	/*
49197d8d152SAndre Oppermann 	 * Find the right bucket
49297d8d152SAndre Oppermann 	 */
49397d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
49497d8d152SAndre Oppermann 
49597d8d152SAndre Oppermann 	/*
49697d8d152SAndre Oppermann 	 * If we don't have an existing object try to insert a new one
49797d8d152SAndre Oppermann 	 */
49897d8d152SAndre Oppermann 	if (hc_entry == NULL) {
49997d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
50097d8d152SAndre Oppermann 		if (hc_entry == NULL)
50197d8d152SAndre Oppermann 			return;
50297d8d152SAndre Oppermann 	}
50397d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
50497d8d152SAndre Oppermann 	hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
50597d8d152SAndre Oppermann 
50697d8d152SAndre Oppermann 	hc_entry->rmx_mtu = mtu;
50797d8d152SAndre Oppermann 
50897d8d152SAndre Oppermann 	/*
50997d8d152SAndre Oppermann 	 * put it upfront so we find it faster next time
51097d8d152SAndre Oppermann 	 */
51197d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
51297d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
51397d8d152SAndre Oppermann 
51497d8d152SAndre Oppermann 	/*
51597d8d152SAndre Oppermann 	 * unlock bucket row
51697d8d152SAndre Oppermann 	 */
51797d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
51897d8d152SAndre Oppermann }
51997d8d152SAndre Oppermann 
52097d8d152SAndre Oppermann /*
52197d8d152SAndre Oppermann  * External function: update the tcp metrics of an entry in the hostcache.
52297d8d152SAndre Oppermann  * Creates a new entry if none was found.
52397d8d152SAndre Oppermann  */
52497d8d152SAndre Oppermann void
52597d8d152SAndre Oppermann tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
52697d8d152SAndre Oppermann {
52797d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
52897d8d152SAndre Oppermann 
52997d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
53097d8d152SAndre Oppermann 	if (hc_entry == NULL) {
53197d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
53297d8d152SAndre Oppermann 		if (hc_entry == NULL)
53397d8d152SAndre Oppermann 			return;
53497d8d152SAndre Oppermann 	}
53597d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
53697d8d152SAndre Oppermann 	hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
53797d8d152SAndre Oppermann 
53897d8d152SAndre Oppermann 	if (hcml->rmx_rtt != 0) {
53997d8d152SAndre Oppermann 		if (hc_entry->rmx_rtt == 0)
54097d8d152SAndre Oppermann 			hc_entry->rmx_rtt = hcml->rmx_rtt;
54197d8d152SAndre Oppermann 		else
54297d8d152SAndre Oppermann 			hc_entry->rmx_rtt =
54397d8d152SAndre Oppermann 			    (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
54497d8d152SAndre Oppermann 		tcpstat.tcps_cachedrtt++;
54597d8d152SAndre Oppermann 	}
54697d8d152SAndre Oppermann 	if (hcml->rmx_rttvar != 0) {
54797d8d152SAndre Oppermann 	        if (hc_entry->rmx_rttvar == 0)
54897d8d152SAndre Oppermann         	        hc_entry->rmx_rttvar = hcml->rmx_rttvar;
54997d8d152SAndre Oppermann 		else
55097d8d152SAndre Oppermann 			hc_entry->rmx_rttvar =
55197d8d152SAndre Oppermann 			    (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
55297d8d152SAndre Oppermann 		tcpstat.tcps_cachedrttvar++;
55397d8d152SAndre Oppermann 	}
55497d8d152SAndre Oppermann 	if (hcml->rmx_ssthresh != 0) {
55597d8d152SAndre Oppermann 		if (hc_entry->rmx_ssthresh == 0)
55697d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
55797d8d152SAndre Oppermann 		else
55897d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh =
55997d8d152SAndre Oppermann 			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
56097d8d152SAndre Oppermann 		tcpstat.tcps_cachedssthresh++;
56197d8d152SAndre Oppermann 	}
56297d8d152SAndre Oppermann 	if (hcml->rmx_bandwidth != 0) {
56397d8d152SAndre Oppermann 		if (hc_entry->rmx_bandwidth == 0)
56497d8d152SAndre Oppermann 			hc_entry->rmx_bandwidth = hcml->rmx_bandwidth;
56597d8d152SAndre Oppermann 		else
56697d8d152SAndre Oppermann 			hc_entry->rmx_bandwidth =
56797d8d152SAndre Oppermann 			    (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2;
56897d8d152SAndre Oppermann 		/* tcpstat.tcps_cachedbandwidth++; */
56997d8d152SAndre Oppermann 	}
57097d8d152SAndre Oppermann 	if (hcml->rmx_cwnd != 0) {
57197d8d152SAndre Oppermann 		if (hc_entry->rmx_cwnd == 0)
57297d8d152SAndre Oppermann 			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
57397d8d152SAndre Oppermann 		else
57497d8d152SAndre Oppermann 			hc_entry->rmx_cwnd =
57597d8d152SAndre Oppermann 			    (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
57697d8d152SAndre Oppermann 		/* tcpstat.tcps_cachedcwnd++; */
57797d8d152SAndre Oppermann 	}
57897d8d152SAndre Oppermann 	if (hcml->rmx_sendpipe != 0) {
57997d8d152SAndre Oppermann 		if (hc_entry->rmx_sendpipe == 0)
58097d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
58197d8d152SAndre Oppermann 		else
58297d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe =
58397d8d152SAndre Oppermann 			    (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
58497d8d152SAndre Oppermann                 /* tcpstat.tcps_cachedsendpipe++; */
58597d8d152SAndre Oppermann         }
58697d8d152SAndre Oppermann 	if (hcml->rmx_recvpipe != 0) {
58797d8d152SAndre Oppermann 		if (hc_entry->rmx_recvpipe == 0)
58897d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
58997d8d152SAndre Oppermann 		else
59097d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe =
59197d8d152SAndre Oppermann 			    (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
59297d8d152SAndre Oppermann 		/* tcpstat.tcps_cachedrecvpipe++; */
59397d8d152SAndre Oppermann 	}
59497d8d152SAndre Oppermann 
59597d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
59697d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
59797d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
59897d8d152SAndre Oppermann }
59997d8d152SAndre Oppermann 
60097d8d152SAndre Oppermann /*
60197d8d152SAndre Oppermann  * External function: update the t/tcp tao of an entry in the hostcache.
60297d8d152SAndre Oppermann  * Creates a new entry if none was found.
60397d8d152SAndre Oppermann  */
60497d8d152SAndre Oppermann void
60597d8d152SAndre Oppermann tcp_hc_updatetao(struct in_conninfo *inc, int field, tcp_cc ccount, u_short mss)
60697d8d152SAndre Oppermann {
60797d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
60897d8d152SAndre Oppermann 
60997d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
61097d8d152SAndre Oppermann 	if (hc_entry == NULL) {
61197d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
61297d8d152SAndre Oppermann 		if (hc_entry == NULL)
61397d8d152SAndre Oppermann 			return;
61497d8d152SAndre Oppermann 	}
61597d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
61697d8d152SAndre Oppermann 	hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
61797d8d152SAndre Oppermann 
61897d8d152SAndre Oppermann 	switch(field) {
61997d8d152SAndre Oppermann 		case TCP_HC_TAO_CC:
62097d8d152SAndre Oppermann 			hc_entry->rmx_tao.tao_cc = ccount;
62197d8d152SAndre Oppermann 			break;
62297d8d152SAndre Oppermann 
62397d8d152SAndre Oppermann 		case TCP_HC_TAO_CCSENT:
62497d8d152SAndre Oppermann 			hc_entry->rmx_tao.tao_ccsent = ccount;
62597d8d152SAndre Oppermann 			break;
62697d8d152SAndre Oppermann 
62797d8d152SAndre Oppermann 		case TCP_HC_TAO_MSSOPT:
62897d8d152SAndre Oppermann 			hc_entry->rmx_tao.tao_mssopt = mss;
62997d8d152SAndre Oppermann 			break;
63097d8d152SAndre Oppermann 	}
63197d8d152SAndre Oppermann 
63297d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
63397d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
63497d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
63597d8d152SAndre Oppermann }
63697d8d152SAndre Oppermann 
63797d8d152SAndre Oppermann /*
63897d8d152SAndre Oppermann  * Sysctl function: prints the list and values of all hostcache entries in
63997d8d152SAndre Oppermann  * unsorted order.
64097d8d152SAndre Oppermann  */
64197d8d152SAndre Oppermann static int
64297d8d152SAndre Oppermann sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
64397d8d152SAndre Oppermann {
64497d8d152SAndre Oppermann 	int bufsize;
64597d8d152SAndre Oppermann 	int linesize = 128;
64697d8d152SAndre Oppermann 	char *p, *buf;
64797d8d152SAndre Oppermann 	int len, i, error;
64897d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
64997d8d152SAndre Oppermann 
65097d8d152SAndre Oppermann 	bufsize = linesize * (tcp_hostcache.cache_count + 1);
65197d8d152SAndre Oppermann 
65297d8d152SAndre Oppermann 	p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
65397d8d152SAndre Oppermann 
65497d8d152SAndre Oppermann 	len = snprintf(p, linesize,
65597d8d152SAndre Oppermann 		"\nIP address        MTU  SSTRESH      RTT   RTTVAR BANDWIDTH "
65697d8d152SAndre Oppermann 		"    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
65797d8d152SAndre Oppermann 	p += len;
65897d8d152SAndre Oppermann 
65997d8d152SAndre Oppermann #define msec(u) (((u) + 500) / 1000)
66097d8d152SAndre Oppermann 	for (i = 0; i < tcp_hostcache.hashsize; i++) {
66197d8d152SAndre Oppermann 		THC_LOCK(&tcp_hostcache.hashbase[i].hch_mtx);
66297d8d152SAndre Oppermann 		TAILQ_FOREACH(hc_entry, &tcp_hostcache.hashbase[i].hch_bucket,
66397d8d152SAndre Oppermann 			      rmx_q) {
66497d8d152SAndre Oppermann 			len = snprintf(p, linesize,
66597d8d152SAndre Oppermann 			    "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu "
66697d8d152SAndre Oppermann 			    "%4lu %4lu %4i\n",
66797d8d152SAndre Oppermann 			    hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
66897d8d152SAndre Oppermann #ifdef INET6
66997d8d152SAndre Oppermann 				ip6_sprintf(&hc_entry->ip6),
67097d8d152SAndre Oppermann #else
67197d8d152SAndre Oppermann 				"IPv6?",
67297d8d152SAndre Oppermann #endif
67397d8d152SAndre Oppermann 			    hc_entry->rmx_mtu,
67497d8d152SAndre Oppermann 			    hc_entry->rmx_ssthresh,
67597d8d152SAndre Oppermann 			    msec(hc_entry->rmx_rtt *
67697d8d152SAndre Oppermann 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
67797d8d152SAndre Oppermann 			    msec(hc_entry->rmx_rttvar *
67897d8d152SAndre Oppermann 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
67997d8d152SAndre Oppermann 			    hc_entry->rmx_bandwidth * hz * 8,
68097d8d152SAndre Oppermann 			    hc_entry->rmx_cwnd,
68197d8d152SAndre Oppermann 			    hc_entry->rmx_sendpipe,
68297d8d152SAndre Oppermann 			    hc_entry->rmx_recvpipe,
68397d8d152SAndre Oppermann 			    hc_entry->rmx_hits,
68497d8d152SAndre Oppermann 			    hc_entry->rmx_updates,
68597d8d152SAndre Oppermann 			    hc_entry->rmx_expire);
68697d8d152SAndre Oppermann 			p += len;
68797d8d152SAndre Oppermann 		}
68897d8d152SAndre Oppermann 		THC_UNLOCK(&tcp_hostcache.hashbase[i].hch_mtx);
68997d8d152SAndre Oppermann 	}
69097d8d152SAndre Oppermann #undef msec
69197d8d152SAndre Oppermann 	error = SYSCTL_OUT(req, buf, p - buf);
69297d8d152SAndre Oppermann 	free(buf, M_TEMP);
69397d8d152SAndre Oppermann 	return(error);
69497d8d152SAndre Oppermann }
69597d8d152SAndre Oppermann 
69697d8d152SAndre Oppermann /*
69797d8d152SAndre Oppermann  * Expire and purge (old|all) entries in the tcp_hostcache.  Runs periodically
69897d8d152SAndre Oppermann  * from the callout.
69997d8d152SAndre Oppermann  */
70097d8d152SAndre Oppermann static void
70197d8d152SAndre Oppermann tcp_hc_purge(void *arg)
70297d8d152SAndre Oppermann {
70397d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
70497d8d152SAndre Oppermann 	int all = (intptr_t)arg;
70597d8d152SAndre Oppermann 	int i;
70697d8d152SAndre Oppermann 
70797d8d152SAndre Oppermann 	if (tcp_hostcache.purgeall) {
70897d8d152SAndre Oppermann 		all = 1;
70997d8d152SAndre Oppermann 		tcp_hostcache.purgeall = 0;
71097d8d152SAndre Oppermann 	}
71197d8d152SAndre Oppermann 
71297d8d152SAndre Oppermann 	for (i = 0; i < tcp_hostcache.hashsize; i++) {
71397d8d152SAndre Oppermann 		THC_LOCK(&tcp_hostcache.hashbase[i].hch_mtx);
71497d8d152SAndre Oppermann 		TAILQ_FOREACH(hc_entry, &tcp_hostcache.hashbase[i].hch_bucket,
71597d8d152SAndre Oppermann 			      rmx_q) {
71697d8d152SAndre Oppermann 			if (all || hc_entry->rmx_expire <= 0) {
71797d8d152SAndre Oppermann 				TAILQ_REMOVE(&tcp_hostcache.hashbase[i].hch_bucket,
71897d8d152SAndre Oppermann 					      hc_entry, rmx_q);
71997d8d152SAndre Oppermann 				uma_zfree(tcp_hostcache.zone, hc_entry);
72097d8d152SAndre Oppermann 				tcp_hostcache.hashbase[i].hch_length--;
72197d8d152SAndre Oppermann 				tcp_hostcache.cache_count--;
72297d8d152SAndre Oppermann 			} else
72397d8d152SAndre Oppermann 				hc_entry->rmx_expire -= TCP_HOSTCACHE_PRUNE;
72497d8d152SAndre Oppermann 		}
72597d8d152SAndre Oppermann 		THC_UNLOCK(&tcp_hostcache.hashbase[i].hch_mtx);
72697d8d152SAndre Oppermann 	}
72797d8d152SAndre Oppermann 	callout_reset(&tcp_hc_callout, TCP_HOSTCACHE_PRUNE * hz, tcp_hc_purge, 0);
72897d8d152SAndre Oppermann }
729