xref: /freebsd/sys/netinet/tcp_hostcache.c (revision 2645638064960797fdfb0a5e80f7fa1af7833861)
1c398230bSWarner Losh /*-
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 
3097d8d152SAndre Oppermann /*
31e487a5e2SRobert Watson  * The tcp_hostcache moves the tcp-specific cached metrics from the routing
32e487a5e2SRobert Watson  * table to a dedicated structure indexed by the remote IP address.  It keeps
33e487a5e2SRobert Watson  * information on the measured TCP parameters of past TCP sessions to allow
34e487a5e2SRobert Watson  * better initial start values to be used with later connections to/from the
3597d8d152SAndre Oppermann  * same source.  Depending on the network parameters (delay, bandwidth, max
36e487a5e2SRobert Watson  * MTU, congestion window) between local and remote sites, this can lead to
37e487a5e2SRobert Watson  * significant speed-ups for new TCP connections after the first one.
3897d8d152SAndre Oppermann  *
39e487a5e2SRobert Watson  * Due to the tcp_hostcache, all TCP-specific metrics information in the
40003c7e36SRui Paulo  * routing table have been removed.  The inpcb no longer keeps a pointer to
41e487a5e2SRobert Watson  * the routing entry, and protocol-initiated route cloning has been removed
42e487a5e2SRobert Watson  * as well.  With these changes, the routing table has gone back to being
43e487a5e2SRobert Watson  * more lightwight and only carries information related to packet forwarding.
4497d8d152SAndre Oppermann  *
45e487a5e2SRobert Watson  * tcp_hostcache is designed for multiple concurrent access in SMP
46e487a5e2SRobert Watson  * environments and high contention.  All bucket rows have their own lock and
47e487a5e2SRobert Watson  * thus multiple lookups and modifies can be done at the same time as long as
48e487a5e2SRobert Watson  * they are in different bucket rows.  If a request for insertion of a new
49e487a5e2SRobert Watson  * record can't be satisfied, it simply returns an empty structure.  Nobody
50e487a5e2SRobert Watson  * and nothing outside of tcp_hostcache.c will ever point directly to any
51e487a5e2SRobert Watson  * entry in the tcp_hostcache.  All communication is done in an
52e487a5e2SRobert Watson  * object-oriented way and only functions of tcp_hostcache will manipulate
53e487a5e2SRobert Watson  * hostcache entries.  Otherwise, we are unable to achieve good behaviour in
54e487a5e2SRobert Watson  * concurrent access situations.  Since tcp_hostcache is only caching
55e487a5e2SRobert Watson  * information, there are no fatal consequences if we either can't satisfy
56e487a5e2SRobert Watson  * any particular request or have to drop/overwrite an existing entry because
57e487a5e2SRobert Watson  * of bucket limit memory constrains.
5897d8d152SAndre Oppermann  */
5997d8d152SAndre Oppermann 
6097d8d152SAndre Oppermann /*
6197d8d152SAndre Oppermann  * Many thanks to jlemon for basic structure of tcp_syncache which is being
6297d8d152SAndre Oppermann  * followed here.
6397d8d152SAndre Oppermann  */
6497d8d152SAndre Oppermann 
654b421e2dSMike Silbersack #include <sys/cdefs.h>
664b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
674b421e2dSMike Silbersack 
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>
76002d4558SJohn Baldwin #include <sys/sbuf.h>
7797d8d152SAndre Oppermann #include <sys/socket.h>
7897d8d152SAndre Oppermann #include <sys/socketvar.h>
7997d8d152SAndre Oppermann #include <sys/sysctl.h>
8097d8d152SAndre Oppermann 
8197d8d152SAndre Oppermann #include <net/if.h>
8276039bc8SGleb Smirnoff #include <net/if_var.h>
835736e6fbSBjoern A. Zeeb #include <net/route.h>
84530c0060SRobert Watson #include <net/vnet.h>
8597d8d152SAndre Oppermann 
8697d8d152SAndre Oppermann #include <netinet/in.h>
8797d8d152SAndre Oppermann #include <netinet/in_systm.h>
8897d8d152SAndre Oppermann #include <netinet/ip.h>
8997d8d152SAndre Oppermann #include <netinet/in_var.h>
9097d8d152SAndre Oppermann #include <netinet/in_pcb.h>
9197d8d152SAndre Oppermann #include <netinet/ip_var.h>
9297d8d152SAndre Oppermann #ifdef INET6
9397d8d152SAndre Oppermann #include <netinet/ip6.h>
9497d8d152SAndre Oppermann #include <netinet6/ip6_var.h>
9597d8d152SAndre Oppermann #endif
9697d8d152SAndre Oppermann #include <netinet/tcp.h>
9797d8d152SAndre Oppermann #include <netinet/tcp_var.h>
984b79449eSBjoern A. Zeeb #include <netinet/tcp_hostcache.h>
9997d8d152SAndre Oppermann #ifdef INET6
10097d8d152SAndre Oppermann #include <netinet6/tcp6_var.h>
10197d8d152SAndre Oppermann #endif
10297d8d152SAndre Oppermann 
10397d8d152SAndre Oppermann #include <vm/uma.h>
10497d8d152SAndre Oppermann 
10597d8d152SAndre Oppermann /* Arbitrary values */
10697d8d152SAndre Oppermann #define TCP_HOSTCACHE_HASHSIZE		512
10797d8d152SAndre Oppermann #define TCP_HOSTCACHE_BUCKETLIMIT	30
10897d8d152SAndre Oppermann #define TCP_HOSTCACHE_EXPIRE		60*60	/* one hour */
10997d8d152SAndre Oppermann #define TCP_HOSTCACHE_PRUNE		5*60	/* every 5 minutes */
11097d8d152SAndre Oppermann 
1113e288e62SDimitry Andric static VNET_DEFINE(struct tcp_hostcache, tcp_hostcache);
1121e77c105SRobert Watson #define	V_tcp_hostcache		VNET(tcp_hostcache)
11382cea7e6SBjoern A. Zeeb 
1143e288e62SDimitry Andric static VNET_DEFINE(struct callout, tcp_hc_callout);
1151e77c105SRobert Watson #define	V_tcp_hc_callout	VNET(tcp_hc_callout)
11697d8d152SAndre Oppermann 
11797d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
11897d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
11997d8d152SAndre Oppermann static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
120*26456380SHiren Panchasara static int sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS);
121fffb9f1dSBjoern A. Zeeb static void tcp_hc_purge_internal(int);
12297d8d152SAndre Oppermann static void tcp_hc_purge(void *);
12397d8d152SAndre Oppermann 
1246472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0,
1250eb7cf4dSBrooks Davis     "TCP Host cache");
12697d8d152SAndre Oppermann 
1276df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN,
128eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.cache_limit), 0,
1298b615593SMarko Zec     "Overall entry limit for hostcache");
13097d8d152SAndre Oppermann 
1316df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN,
132eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.hashsize), 0,
1338b615593SMarko Zec     "Size of TCP hostcache hashtable");
13497d8d152SAndre Oppermann 
1356df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
1366df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0,
1378b615593SMarko Zec     "Per-bucket hash limit for hostcache");
13897d8d152SAndre Oppermann 
1396df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD,
140eddfbb76SRobert Watson      &VNET_NAME(tcp_hostcache.cache_count), 0,
1418b615593SMarko Zec     "Current number of entries in hostcache");
14297d8d152SAndre Oppermann 
1436df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW,
144eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.expire), 0,
1458b615593SMarko Zec     "Expire time of TCP hostcache entries");
14697d8d152SAndre Oppermann 
1476df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW,
148eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.prune), 0,
149eddfbb76SRobert Watson     "Time between purge runs");
150dba3c508SYaroslav Tykhiy 
1516df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW,
152eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.purgeall), 0,
1538b615593SMarko Zec     "Expire all entires on next purge run");
15497d8d152SAndre Oppermann 
15597d8d152SAndre Oppermann SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
15697d8d152SAndre Oppermann     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
15797d8d152SAndre Oppermann     sysctl_tcp_hc_list, "A", "List of all hostcache entries");
15897d8d152SAndre Oppermann 
159*26456380SHiren Panchasara SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, purgenow,
160*26456380SHiren Panchasara     CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
161*26456380SHiren Panchasara     sysctl_tcp_hc_purgenow, "I", "Immediately purge all entries");
16297d8d152SAndre Oppermann 
16397d8d152SAndre Oppermann static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
16497d8d152SAndre Oppermann 
16597d8d152SAndre Oppermann #define HOSTCACHE_HASH(ip) \
16697d8d152SAndre Oppermann 	(((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) &	\
167603724d3SBjoern A. Zeeb 	  V_tcp_hostcache.hashmask)
16897d8d152SAndre Oppermann 
16997d8d152SAndre Oppermann /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
17097d8d152SAndre Oppermann #define HOSTCACHE_HASH6(ip6)				\
17197d8d152SAndre Oppermann 	(((ip6)->s6_addr32[0] ^				\
17297d8d152SAndre Oppermann 	  (ip6)->s6_addr32[1] ^				\
17397d8d152SAndre Oppermann 	  (ip6)->s6_addr32[2] ^				\
17497d8d152SAndre Oppermann 	  (ip6)->s6_addr32[3]) &			\
175603724d3SBjoern A. Zeeb 	 V_tcp_hostcache.hashmask)
17697d8d152SAndre Oppermann 
17797d8d152SAndre Oppermann #define THC_LOCK(lp)		mtx_lock(lp)
17897d8d152SAndre Oppermann #define THC_UNLOCK(lp)		mtx_unlock(lp)
17997d8d152SAndre Oppermann 
18097d8d152SAndre Oppermann void
18197d8d152SAndre Oppermann tcp_hc_init(void)
18297d8d152SAndre Oppermann {
18332fe38f1SAndrey Zonov 	u_int cache_limit;
18497d8d152SAndre Oppermann 	int i;
18597d8d152SAndre Oppermann 
18697d8d152SAndre Oppermann 	/*
187e487a5e2SRobert Watson 	 * Initialize hostcache structures.
18897d8d152SAndre Oppermann 	 */
189603724d3SBjoern A. Zeeb 	V_tcp_hostcache.cache_count = 0;
190603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
191603724d3SBjoern A. Zeeb 	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
192603724d3SBjoern A. Zeeb 	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
193603724d3SBjoern A. Zeeb 	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
19497d8d152SAndre Oppermann 
19597d8d152SAndre Oppermann 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
196603724d3SBjoern A. Zeeb 	    &V_tcp_hostcache.hashsize);
197603724d3SBjoern A. Zeeb 	if (!powerof2(V_tcp_hostcache.hashsize)) {
19897d8d152SAndre Oppermann 		printf("WARNING: hostcache hash size is not a power of 2.\n");
199603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
20097d8d152SAndre Oppermann 	}
201603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
20297d8d152SAndre Oppermann 
20332fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
20432fe38f1SAndrey Zonov 	    &V_tcp_hostcache.bucket_limit);
20532fe38f1SAndrey Zonov 
20632fe38f1SAndrey Zonov 	cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
20732fe38f1SAndrey Zonov 	V_tcp_hostcache.cache_limit = cache_limit;
20832fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
20932fe38f1SAndrey Zonov 	    &V_tcp_hostcache.cache_limit);
21032fe38f1SAndrey Zonov 	if (V_tcp_hostcache.cache_limit > cache_limit)
21132fe38f1SAndrey Zonov 		V_tcp_hostcache.cache_limit = cache_limit;
21232fe38f1SAndrey Zonov 
21397d8d152SAndre Oppermann 	/*
214e487a5e2SRobert Watson 	 * Allocate the hash table.
21597d8d152SAndre Oppermann 	 */
216603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase = (struct hc_head *)
217603724d3SBjoern A. Zeeb 	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
21897d8d152SAndre Oppermann 		   M_HOSTCACHE, M_WAITOK | M_ZERO);
21997d8d152SAndre Oppermann 
22097d8d152SAndre Oppermann 	/*
221e487a5e2SRobert Watson 	 * Initialize the hash buckets.
22297d8d152SAndre Oppermann 	 */
223603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
224603724d3SBjoern A. Zeeb 		TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
225603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[i].hch_length = 0;
226603724d3SBjoern A. Zeeb 		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
22797d8d152SAndre Oppermann 			  NULL, MTX_DEF);
22897d8d152SAndre Oppermann 	}
22997d8d152SAndre Oppermann 
23097d8d152SAndre Oppermann 	/*
23197d8d152SAndre Oppermann 	 * Allocate the hostcache entries.
23297d8d152SAndre Oppermann 	 */
233ac957cd2SJulian Elischer 	V_tcp_hostcache.zone =
234ac957cd2SJulian Elischer 	    uma_zcreate("hostcache", sizeof(struct hc_metrics),
2354efb805cSAndre Oppermann 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
236603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
23797d8d152SAndre Oppermann 
23897d8d152SAndre Oppermann 	/*
23997d8d152SAndre Oppermann 	 * Set up periodic cache cleanup.
24097d8d152SAndre Oppermann 	 */
241603724d3SBjoern A. Zeeb 	callout_init(&V_tcp_hc_callout, CALLOUT_MPSAFE);
242ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
24321ca7b57SMarko Zec 	    tcp_hc_purge, curvnet);
24497d8d152SAndre Oppermann }
24597d8d152SAndre Oppermann 
246bc29160dSMarko Zec #ifdef VIMAGE
247bc29160dSMarko Zec void
248bc29160dSMarko Zec tcp_hc_destroy(void)
249bc29160dSMarko Zec {
250fffb9f1dSBjoern A. Zeeb 	int i;
251bc29160dSMarko Zec 
252bc29160dSMarko Zec 	callout_drain(&V_tcp_hc_callout);
253fffb9f1dSBjoern A. Zeeb 
254fffb9f1dSBjoern A. Zeeb 	/* Purge all hc entries. */
255fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(1);
256fffb9f1dSBjoern A. Zeeb 
257fffb9f1dSBjoern A. Zeeb 	/* Free the uma zone and the allocated hash table. */
258fffb9f1dSBjoern A. Zeeb 	uma_zdestroy(V_tcp_hostcache.zone);
259fffb9f1dSBjoern A. Zeeb 
260fffb9f1dSBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++)
261fffb9f1dSBjoern A. Zeeb 		mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx);
262fffb9f1dSBjoern A. Zeeb 	free(V_tcp_hostcache.hashbase, M_HOSTCACHE);
263bc29160dSMarko Zec }
264bc29160dSMarko Zec #endif
265bc29160dSMarko Zec 
26697d8d152SAndre Oppermann /*
26797d8d152SAndre Oppermann  * Internal function: look up an entry in the hostcache or return NULL.
26897d8d152SAndre Oppermann  *
26997d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
27097d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
27197d8d152SAndre Oppermann  */
27297d8d152SAndre Oppermann static struct hc_metrics *
27397d8d152SAndre Oppermann tcp_hc_lookup(struct in_conninfo *inc)
27497d8d152SAndre Oppermann {
27597d8d152SAndre Oppermann 	int hash;
27697d8d152SAndre Oppermann 	struct hc_head *hc_head;
27797d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
27897d8d152SAndre Oppermann 
27997d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
28097d8d152SAndre Oppermann 
28197d8d152SAndre Oppermann 	/*
28297d8d152SAndre Oppermann 	 * Hash the foreign ip address.
28397d8d152SAndre Oppermann 	 */
284dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
28597d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
28697d8d152SAndre Oppermann 	else
28797d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
28897d8d152SAndre Oppermann 
289603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
29097d8d152SAndre Oppermann 
29197d8d152SAndre Oppermann 	/*
292e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
293e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
294e487a5e2SRobert Watson 	 * done.
29597d8d152SAndre Oppermann 	 */
29697d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
29797d8d152SAndre Oppermann 
29897d8d152SAndre Oppermann 	/*
299e487a5e2SRobert Watson 	 * Iterate through entries in bucket row looking for a match.
30097d8d152SAndre Oppermann 	 */
30197d8d152SAndre Oppermann 	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
302dcdb4371SBjoern A. Zeeb 		if (inc->inc_flags & INC_ISIPV6) {
303028bdf28SAndrey V. Elsukov 			/* XXX: check ip6_zoneid */
30497d8d152SAndre Oppermann 			if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
30597d8d152SAndre Oppermann 			    sizeof(inc->inc6_faddr)) == 0)
30697d8d152SAndre Oppermann 				return hc_entry;
30797d8d152SAndre Oppermann 		} else {
30897d8d152SAndre Oppermann 			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
30997d8d152SAndre Oppermann 			    sizeof(inc->inc_faddr)) == 0)
31097d8d152SAndre Oppermann 				return hc_entry;
31197d8d152SAndre Oppermann 		}
31297d8d152SAndre Oppermann 	}
31397d8d152SAndre Oppermann 
31497d8d152SAndre Oppermann 	/*
315e487a5e2SRobert Watson 	 * We were unsuccessful and didn't find anything.
31697d8d152SAndre Oppermann 	 */
31797d8d152SAndre Oppermann 	THC_UNLOCK(&hc_head->hch_mtx);
31897d8d152SAndre Oppermann 	return NULL;
31997d8d152SAndre Oppermann }
32097d8d152SAndre Oppermann 
32197d8d152SAndre Oppermann /*
322e487a5e2SRobert Watson  * Internal function: insert an entry into the hostcache or return NULL if
323e487a5e2SRobert Watson  * unable to allocate a new one.
32497d8d152SAndre Oppermann  *
32597d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
32697d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
32797d8d152SAndre Oppermann  */
32897d8d152SAndre Oppermann static struct hc_metrics *
32997d8d152SAndre Oppermann tcp_hc_insert(struct in_conninfo *inc)
33097d8d152SAndre Oppermann {
33197d8d152SAndre Oppermann 	int hash;
33297d8d152SAndre Oppermann 	struct hc_head *hc_head;
33397d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
33497d8d152SAndre Oppermann 
33597d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
33697d8d152SAndre Oppermann 
33797d8d152SAndre Oppermann 	/*
338e487a5e2SRobert Watson 	 * Hash the foreign ip address.
33997d8d152SAndre Oppermann 	 */
340dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
34197d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
34297d8d152SAndre Oppermann 	else
34397d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
34497d8d152SAndre Oppermann 
345603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
34697d8d152SAndre Oppermann 
34797d8d152SAndre Oppermann 	/*
348e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
349e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
350e487a5e2SRobert Watson 	 * done.
35197d8d152SAndre Oppermann 	 */
35297d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
35397d8d152SAndre Oppermann 
35497d8d152SAndre Oppermann 	/*
355e487a5e2SRobert Watson 	 * If the bucket limit is reached, reuse the least-used element.
35697d8d152SAndre Oppermann 	 */
357603724d3SBjoern A. Zeeb 	if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
358603724d3SBjoern A. Zeeb 	    V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) {
35997d8d152SAndre Oppermann 		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
36097d8d152SAndre Oppermann 		/*
36197d8d152SAndre Oppermann 		 * At first we were dropping the last element, just to
362e487a5e2SRobert Watson 		 * reacquire it in the next two lines again, which isn't very
363e487a5e2SRobert Watson 		 * efficient.  Instead just reuse the least used element.
364e487a5e2SRobert Watson 		 * We may drop something that is still "in-use" but we can be
365e487a5e2SRobert Watson 		 * "lossy".
36645024be0SAndre Oppermann 		 * Just give up if this bucket row is empty and we don't have
36745024be0SAndre Oppermann 		 * anything to replace.
36897d8d152SAndre Oppermann 		 */
36945024be0SAndre Oppermann 		if (hc_entry == NULL) {
37045024be0SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
37145024be0SAndre Oppermann 			return NULL;
37245024be0SAndre Oppermann 		}
37397d8d152SAndre Oppermann 		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
374603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[hash].hch_length--;
375603724d3SBjoern A. Zeeb 		V_tcp_hostcache.cache_count--;
37678b50714SRobert Watson 		TCPSTAT_INC(tcps_hc_bucketoverflow);
377cd6c4060SAndre Oppermann #if 0
378603724d3SBjoern A. Zeeb 		uma_zfree(V_tcp_hostcache.zone, hc_entry);
379cd6c4060SAndre Oppermann #endif
38097d8d152SAndre Oppermann 	} else {
38197d8d152SAndre Oppermann 		/*
382e487a5e2SRobert Watson 		 * Allocate a new entry, or balk if not possible.
38397d8d152SAndre Oppermann 		 */
384603724d3SBjoern A. Zeeb 		hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
38597d8d152SAndre Oppermann 		if (hc_entry == NULL) {
38697d8d152SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
38797d8d152SAndre Oppermann 			return NULL;
38897d8d152SAndre Oppermann 		}
38997d8d152SAndre Oppermann 	}
39097d8d152SAndre Oppermann 
39197d8d152SAndre Oppermann 	/*
392e487a5e2SRobert Watson 	 * Initialize basic information of hostcache entry.
39397d8d152SAndre Oppermann 	 */
39497d8d152SAndre Oppermann 	bzero(hc_entry, sizeof(*hc_entry));
395028bdf28SAndrey V. Elsukov 	if (inc->inc_flags & INC_ISIPV6) {
396028bdf28SAndrey V. Elsukov 		hc_entry->ip6 = inc->inc6_faddr;
397028bdf28SAndrey V. Elsukov 		hc_entry->ip6_zoneid = inc->inc6_zoneid;
398028bdf28SAndrey V. Elsukov 	} else
39997d8d152SAndre Oppermann 		hc_entry->ip4 = inc->inc_faddr;
40097d8d152SAndre Oppermann 	hc_entry->rmx_head = hc_head;
401603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire;
40297d8d152SAndre Oppermann 
40397d8d152SAndre Oppermann 	/*
404e487a5e2SRobert Watson 	 * Put it upfront.
40597d8d152SAndre Oppermann 	 */
40697d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
407603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase[hash].hch_length++;
408603724d3SBjoern A. Zeeb 	V_tcp_hostcache.cache_count++;
40978b50714SRobert Watson 	TCPSTAT_INC(tcps_hc_added);
41097d8d152SAndre Oppermann 
41197d8d152SAndre Oppermann 	return hc_entry;
41297d8d152SAndre Oppermann }
41397d8d152SAndre Oppermann 
41497d8d152SAndre Oppermann /*
41597d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and fill out the
416e487a5e2SRobert Watson  * supplied TCP metrics structure.  Fills in NULL when no entry was found or
417e487a5e2SRobert Watson  * a value is not set.
41897d8d152SAndre Oppermann  */
41997d8d152SAndre Oppermann void
42097d8d152SAndre Oppermann tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
42197d8d152SAndre Oppermann {
42297d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
42397d8d152SAndre Oppermann 
42497d8d152SAndre Oppermann 	/*
425e487a5e2SRobert Watson 	 * Find the right bucket.
42697d8d152SAndre Oppermann 	 */
42797d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
42897d8d152SAndre Oppermann 
42997d8d152SAndre Oppermann 	/*
430e487a5e2SRobert Watson 	 * If we don't have an existing object.
43197d8d152SAndre Oppermann 	 */
43297d8d152SAndre Oppermann 	if (hc_entry == NULL) {
43397d8d152SAndre Oppermann 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
43497d8d152SAndre Oppermann 		return;
43597d8d152SAndre Oppermann 	}
43697d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
437603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
43897d8d152SAndre Oppermann 
43997d8d152SAndre Oppermann 	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
44097d8d152SAndre Oppermann 	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
44197d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
44297d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
44397d8d152SAndre Oppermann 	hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth;
44497d8d152SAndre Oppermann 	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
44597d8d152SAndre Oppermann 	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
44697d8d152SAndre Oppermann 	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
44797d8d152SAndre Oppermann 
44897d8d152SAndre Oppermann 	/*
449e487a5e2SRobert Watson 	 * Unlock bucket row.
45097d8d152SAndre Oppermann 	 */
45197d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
45297d8d152SAndre Oppermann }
45397d8d152SAndre Oppermann 
45497d8d152SAndre Oppermann /*
45597d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and return the
456e487a5e2SRobert Watson  * discovered path MTU.  Returns NULL if no entry is found or value is not
4576fbed4afSRobert Watson  * set.
45897d8d152SAndre Oppermann  */
45997d8d152SAndre Oppermann u_long
46097d8d152SAndre Oppermann tcp_hc_getmtu(struct in_conninfo *inc)
46197d8d152SAndre Oppermann {
46297d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
46397d8d152SAndre Oppermann 	u_long mtu;
46497d8d152SAndre Oppermann 
46597d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
46697d8d152SAndre Oppermann 	if (hc_entry == NULL) {
46797d8d152SAndre Oppermann 		return 0;
46897d8d152SAndre Oppermann 	}
46997d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
470603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
47197d8d152SAndre Oppermann 
47297d8d152SAndre Oppermann 	mtu = hc_entry->rmx_mtu;
47397d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
47497d8d152SAndre Oppermann 	return mtu;
47597d8d152SAndre Oppermann }
47697d8d152SAndre Oppermann 
47797d8d152SAndre Oppermann /*
478e487a5e2SRobert Watson  * External function: update the MTU value of an entry in the hostcache.
47997d8d152SAndre Oppermann  * Creates a new entry if none was found.
48097d8d152SAndre Oppermann  */
48197d8d152SAndre Oppermann void
48297d8d152SAndre Oppermann tcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
48397d8d152SAndre Oppermann {
48497d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
48597d8d152SAndre Oppermann 
48697d8d152SAndre Oppermann 	/*
487e487a5e2SRobert Watson 	 * Find the right bucket.
48897d8d152SAndre Oppermann 	 */
48997d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
49097d8d152SAndre Oppermann 
49197d8d152SAndre Oppermann 	/*
492e487a5e2SRobert Watson 	 * If we don't have an existing object, try to insert a new one.
49397d8d152SAndre Oppermann 	 */
49497d8d152SAndre Oppermann 	if (hc_entry == NULL) {
49597d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
49697d8d152SAndre Oppermann 		if (hc_entry == NULL)
49797d8d152SAndre Oppermann 			return;
49897d8d152SAndre Oppermann 	}
49997d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
500603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
50197d8d152SAndre Oppermann 
50297d8d152SAndre Oppermann 	hc_entry->rmx_mtu = mtu;
50397d8d152SAndre Oppermann 
50497d8d152SAndre Oppermann 	/*
505e487a5e2SRobert Watson 	 * Put it upfront so we find it faster next time.
50697d8d152SAndre Oppermann 	 */
50797d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
50897d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
50997d8d152SAndre Oppermann 
51097d8d152SAndre Oppermann 	/*
511e487a5e2SRobert Watson 	 * Unlock bucket row.
51297d8d152SAndre Oppermann 	 */
51397d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
51497d8d152SAndre Oppermann }
51597d8d152SAndre Oppermann 
51697d8d152SAndre Oppermann /*
517e487a5e2SRobert Watson  * External function: update the TCP metrics of an entry in the hostcache.
51897d8d152SAndre Oppermann  * Creates a new entry if none was found.
51997d8d152SAndre Oppermann  */
52097d8d152SAndre Oppermann void
52197d8d152SAndre Oppermann tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
52297d8d152SAndre Oppermann {
52397d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
52497d8d152SAndre Oppermann 
52597d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
52697d8d152SAndre Oppermann 	if (hc_entry == NULL) {
52797d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
52897d8d152SAndre Oppermann 		if (hc_entry == NULL)
52997d8d152SAndre Oppermann 			return;
53097d8d152SAndre Oppermann 	}
53197d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
532603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
53397d8d152SAndre Oppermann 
53497d8d152SAndre Oppermann 	if (hcml->rmx_rtt != 0) {
53597d8d152SAndre Oppermann 		if (hc_entry->rmx_rtt == 0)
53697d8d152SAndre Oppermann 			hc_entry->rmx_rtt = hcml->rmx_rtt;
53797d8d152SAndre Oppermann 		else
53897d8d152SAndre Oppermann 			hc_entry->rmx_rtt =
53997d8d152SAndre Oppermann 			    (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
54078b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrtt);
54197d8d152SAndre Oppermann 	}
54297d8d152SAndre Oppermann 	if (hcml->rmx_rttvar != 0) {
54397d8d152SAndre Oppermann 	        if (hc_entry->rmx_rttvar == 0)
54497d8d152SAndre Oppermann 			hc_entry->rmx_rttvar = hcml->rmx_rttvar;
54597d8d152SAndre Oppermann 		else
54697d8d152SAndre Oppermann 			hc_entry->rmx_rttvar =
54797d8d152SAndre Oppermann 			    (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
54878b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrttvar);
54997d8d152SAndre Oppermann 	}
55097d8d152SAndre Oppermann 	if (hcml->rmx_ssthresh != 0) {
55197d8d152SAndre Oppermann 		if (hc_entry->rmx_ssthresh == 0)
55297d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
55397d8d152SAndre Oppermann 		else
55497d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh =
55597d8d152SAndre Oppermann 			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
55678b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedssthresh);
55797d8d152SAndre Oppermann 	}
55897d8d152SAndre Oppermann 	if (hcml->rmx_bandwidth != 0) {
55997d8d152SAndre Oppermann 		if (hc_entry->rmx_bandwidth == 0)
56097d8d152SAndre Oppermann 			hc_entry->rmx_bandwidth = hcml->rmx_bandwidth;
56197d8d152SAndre Oppermann 		else
56297d8d152SAndre Oppermann 			hc_entry->rmx_bandwidth =
56397d8d152SAndre Oppermann 			    (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2;
56478b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedbandwidth); */
56597d8d152SAndre Oppermann 	}
56697d8d152SAndre Oppermann 	if (hcml->rmx_cwnd != 0) {
56797d8d152SAndre Oppermann 		if (hc_entry->rmx_cwnd == 0)
56897d8d152SAndre Oppermann 			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
56997d8d152SAndre Oppermann 		else
57097d8d152SAndre Oppermann 			hc_entry->rmx_cwnd =
57197d8d152SAndre Oppermann 			    (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
57278b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedcwnd); */
57397d8d152SAndre Oppermann 	}
57497d8d152SAndre Oppermann 	if (hcml->rmx_sendpipe != 0) {
57597d8d152SAndre Oppermann 		if (hc_entry->rmx_sendpipe == 0)
57697d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
57797d8d152SAndre Oppermann 		else
57897d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe =
57997d8d152SAndre Oppermann 			    (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
58078b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedsendpipe); */
58197d8d152SAndre Oppermann 	}
58297d8d152SAndre Oppermann 	if (hcml->rmx_recvpipe != 0) {
58397d8d152SAndre Oppermann 		if (hc_entry->rmx_recvpipe == 0)
58497d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
58597d8d152SAndre Oppermann 		else
58697d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe =
58797d8d152SAndre Oppermann 			    (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
58878b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedrecvpipe); */
58997d8d152SAndre Oppermann 	}
59097d8d152SAndre Oppermann 
59197d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
59297d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
59397d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
59497d8d152SAndre Oppermann }
59597d8d152SAndre Oppermann 
59697d8d152SAndre Oppermann /*
59797d8d152SAndre Oppermann  * Sysctl function: prints the list and values of all hostcache entries in
59897d8d152SAndre Oppermann  * unsorted order.
59997d8d152SAndre Oppermann  */
60097d8d152SAndre Oppermann static int
60197d8d152SAndre Oppermann sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
60297d8d152SAndre Oppermann {
603751ccc42SIan Lepore 	const int linesize = 128;
604002d4558SJohn Baldwin 	struct sbuf sb;
605002d4558SJohn Baldwin 	int i, error;
60697d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
6071d54aa3bSBjoern A. Zeeb #ifdef INET6
6081d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
6091d54aa3bSBjoern A. Zeeb #endif
61097d8d152SAndre Oppermann 
611dcdeb95fSIan Lepore 	sbuf_new(&sb, NULL, linesize * (V_tcp_hostcache.cache_count + 1),
612dcdeb95fSIan Lepore 		SBUF_INCLUDENUL);
61397d8d152SAndre Oppermann 
614002d4558SJohn Baldwin 	sbuf_printf(&sb,
61597d8d152SAndre Oppermann 	        "\nIP address        MTU  SSTRESH      RTT   RTTVAR BANDWIDTH "
61697d8d152SAndre Oppermann 		"    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
61797d8d152SAndre Oppermann 
61897d8d152SAndre Oppermann #define msec(u) (((u) + 500) / 1000)
619603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
620603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
621603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
62297d8d152SAndre Oppermann 			      rmx_q) {
623002d4558SJohn Baldwin 			sbuf_printf(&sb,
62497d8d152SAndre Oppermann 			    "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu "
62597d8d152SAndre Oppermann 			    "%4lu %4lu %4i\n",
62697d8d152SAndre Oppermann 			    hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
62797d8d152SAndre Oppermann #ifdef INET6
6281d54aa3bSBjoern A. Zeeb 				ip6_sprintf(ip6buf, &hc_entry->ip6),
62997d8d152SAndre Oppermann #else
63097d8d152SAndre Oppermann 				"IPv6?",
63197d8d152SAndre Oppermann #endif
63297d8d152SAndre Oppermann 			    hc_entry->rmx_mtu,
63397d8d152SAndre Oppermann 			    hc_entry->rmx_ssthresh,
63497d8d152SAndre Oppermann 			    msec(hc_entry->rmx_rtt *
63597d8d152SAndre Oppermann 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
63697d8d152SAndre Oppermann 			    msec(hc_entry->rmx_rttvar *
6373a288e90SMikolaj Golub 				(RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))),
6382f6e6e9bSAndre Oppermann 			    hc_entry->rmx_bandwidth * 8,
63997d8d152SAndre Oppermann 			    hc_entry->rmx_cwnd,
64097d8d152SAndre Oppermann 			    hc_entry->rmx_sendpipe,
64197d8d152SAndre Oppermann 			    hc_entry->rmx_recvpipe,
64297d8d152SAndre Oppermann 			    hc_entry->rmx_hits,
64397d8d152SAndre Oppermann 			    hc_entry->rmx_updates,
64497d8d152SAndre Oppermann 			    hc_entry->rmx_expire);
64597d8d152SAndre Oppermann 		}
646603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
64797d8d152SAndre Oppermann 	}
64897d8d152SAndre Oppermann #undef msec
649751ccc42SIan Lepore 	error = sbuf_finish(&sb);
650dcdeb95fSIan Lepore 	if (error == 0)
651dcdeb95fSIan Lepore 		error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
652002d4558SJohn Baldwin 	sbuf_delete(&sb);
65397d8d152SAndre Oppermann 	return(error);
65497d8d152SAndre Oppermann }
65597d8d152SAndre Oppermann 
65697d8d152SAndre Oppermann /*
657fffb9f1dSBjoern A. Zeeb  * Caller has to make sure the curvnet is set properly.
65897d8d152SAndre Oppermann  */
65997d8d152SAndre Oppermann static void
660fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(int all)
66197d8d152SAndre Oppermann {
662b62dccc7SAndre Oppermann 	struct hc_metrics *hc_entry, *hc_next;
66397d8d152SAndre Oppermann 	int i;
66497d8d152SAndre Oppermann 
665603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
666603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
667ac957cd2SJulian Elischer 		TAILQ_FOREACH_SAFE(hc_entry,
6685ed3800eSJulian Elischer 		    &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
66997d8d152SAndre Oppermann 			if (all || hc_entry->rmx_expire <= 0) {
670603724d3SBjoern A. Zeeb 				TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
67197d8d152SAndre Oppermann 					      hc_entry, rmx_q);
672603724d3SBjoern A. Zeeb 				uma_zfree(V_tcp_hostcache.zone, hc_entry);
673603724d3SBjoern A. Zeeb 				V_tcp_hostcache.hashbase[i].hch_length--;
674603724d3SBjoern A. Zeeb 				V_tcp_hostcache.cache_count--;
67597d8d152SAndre Oppermann 			} else
676603724d3SBjoern A. Zeeb 				hc_entry->rmx_expire -= V_tcp_hostcache.prune;
67797d8d152SAndre Oppermann 		}
678603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
67997d8d152SAndre Oppermann 	}
680fffb9f1dSBjoern A. Zeeb }
681fffb9f1dSBjoern A. Zeeb 
682fffb9f1dSBjoern A. Zeeb /*
683fffb9f1dSBjoern A. Zeeb  * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
684fffb9f1dSBjoern A. Zeeb  * periodically from the callout.
685fffb9f1dSBjoern A. Zeeb  */
686fffb9f1dSBjoern A. Zeeb static void
687fffb9f1dSBjoern A. Zeeb tcp_hc_purge(void *arg)
688fffb9f1dSBjoern A. Zeeb {
689fffb9f1dSBjoern A. Zeeb 	CURVNET_SET((struct vnet *) arg);
690fffb9f1dSBjoern A. Zeeb 	int all = 0;
691fffb9f1dSBjoern A. Zeeb 
692fffb9f1dSBjoern A. Zeeb 	if (V_tcp_hostcache.purgeall) {
693fffb9f1dSBjoern A. Zeeb 		all = 1;
694fffb9f1dSBjoern A. Zeeb 		V_tcp_hostcache.purgeall = 0;
695fffb9f1dSBjoern A. Zeeb 	}
696fffb9f1dSBjoern A. Zeeb 
697fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(all);
698ac957cd2SJulian Elischer 
699ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
700ac957cd2SJulian Elischer 	    tcp_hc_purge, arg);
70121ca7b57SMarko Zec 	CURVNET_RESTORE();
70297d8d152SAndre Oppermann }
703*26456380SHiren Panchasara 
704*26456380SHiren Panchasara /*
705*26456380SHiren Panchasara  * Expire and purge all entries in hostcache immediately.
706*26456380SHiren Panchasara  */
707*26456380SHiren Panchasara static int
708*26456380SHiren Panchasara sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS)
709*26456380SHiren Panchasara {
710*26456380SHiren Panchasara 	int error, val;
711*26456380SHiren Panchasara 
712*26456380SHiren Panchasara 	val = 0;
713*26456380SHiren Panchasara 	error = sysctl_handle_int(oidp, &val, 0, req);
714*26456380SHiren Panchasara 	if (error || !req->newptr)
715*26456380SHiren Panchasara 		return (error);
716*26456380SHiren Panchasara 
717*26456380SHiren Panchasara 	tcp_hc_purge_internal(1);
718*26456380SHiren Panchasara 
719*26456380SHiren Panchasara 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
720*26456380SHiren Panchasara 	    tcp_hc_purge, curvnet);
721*26456380SHiren Panchasara 
722*26456380SHiren Panchasara 	return (0);
723*26456380SHiren Panchasara }
724