xref: /freebsd/sys/netinet/tcp_hostcache.c (revision b8a2fb91f631370ae32a28fe663936de0139358d)
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
354d163382SHiren Panchasara  * same source.  Depending on the network parameters (delay, max MTU,
364d163382SHiren Panchasara  * 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>
72*b8a2fb91SHiren Panchasara #include <sys/jail.h>
7397d8d152SAndre Oppermann #include <sys/kernel.h>
7497d8d152SAndre Oppermann #include <sys/lock.h>
7597d8d152SAndre Oppermann #include <sys/mutex.h>
7697d8d152SAndre Oppermann #include <sys/malloc.h>
77*b8a2fb91SHiren Panchasara #include <sys/proc.h>
78002d4558SJohn Baldwin #include <sys/sbuf.h>
7997d8d152SAndre Oppermann #include <sys/socket.h>
8097d8d152SAndre Oppermann #include <sys/socketvar.h>
8197d8d152SAndre Oppermann #include <sys/sysctl.h>
8297d8d152SAndre Oppermann 
8397d8d152SAndre Oppermann #include <net/if.h>
8476039bc8SGleb Smirnoff #include <net/if_var.h>
855736e6fbSBjoern A. Zeeb #include <net/route.h>
86530c0060SRobert Watson #include <net/vnet.h>
8797d8d152SAndre Oppermann 
8897d8d152SAndre Oppermann #include <netinet/in.h>
8997d8d152SAndre Oppermann #include <netinet/in_systm.h>
9097d8d152SAndre Oppermann #include <netinet/ip.h>
9197d8d152SAndre Oppermann #include <netinet/in_var.h>
9297d8d152SAndre Oppermann #include <netinet/in_pcb.h>
9397d8d152SAndre Oppermann #include <netinet/ip_var.h>
9497d8d152SAndre Oppermann #ifdef INET6
9597d8d152SAndre Oppermann #include <netinet/ip6.h>
9697d8d152SAndre Oppermann #include <netinet6/ip6_var.h>
9797d8d152SAndre Oppermann #endif
9897d8d152SAndre Oppermann #include <netinet/tcp.h>
9997d8d152SAndre Oppermann #include <netinet/tcp_var.h>
1004b79449eSBjoern A. Zeeb #include <netinet/tcp_hostcache.h>
10197d8d152SAndre Oppermann #ifdef INET6
10297d8d152SAndre Oppermann #include <netinet6/tcp6_var.h>
10397d8d152SAndre Oppermann #endif
10497d8d152SAndre Oppermann 
10597d8d152SAndre Oppermann #include <vm/uma.h>
10697d8d152SAndre Oppermann 
10797d8d152SAndre Oppermann /* Arbitrary values */
10897d8d152SAndre Oppermann #define TCP_HOSTCACHE_HASHSIZE		512
10997d8d152SAndre Oppermann #define TCP_HOSTCACHE_BUCKETLIMIT	30
11097d8d152SAndre Oppermann #define TCP_HOSTCACHE_EXPIRE		60*60	/* one hour */
11197d8d152SAndre Oppermann #define TCP_HOSTCACHE_PRUNE		5*60	/* every 5 minutes */
11297d8d152SAndre Oppermann 
1133e288e62SDimitry Andric static VNET_DEFINE(struct tcp_hostcache, tcp_hostcache);
1141e77c105SRobert Watson #define	V_tcp_hostcache		VNET(tcp_hostcache)
11582cea7e6SBjoern A. Zeeb 
1163e288e62SDimitry Andric static VNET_DEFINE(struct callout, tcp_hc_callout);
1171e77c105SRobert Watson #define	V_tcp_hc_callout	VNET(tcp_hc_callout)
11897d8d152SAndre Oppermann 
11997d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
12097d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
12197d8d152SAndre Oppermann static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
12226456380SHiren Panchasara static int sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS);
123fffb9f1dSBjoern A. Zeeb static void tcp_hc_purge_internal(int);
12497d8d152SAndre Oppermann static void tcp_hc_purge(void *);
12597d8d152SAndre Oppermann 
1266472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0,
1270eb7cf4dSBrooks Davis     "TCP Host cache");
12897d8d152SAndre Oppermann 
1298a56c645SHiren Panchasara VNET_DEFINE(int, tcp_use_hostcache) = 1;
1308a56c645SHiren Panchasara #define V_tcp_use_hostcache  VNET(tcp_use_hostcache)
1318a56c645SHiren Panchasara SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
1328a56c645SHiren Panchasara     &VNET_NAME(tcp_use_hostcache), 0,
1338a56c645SHiren Panchasara     "Enable the TCP hostcache");
1348a56c645SHiren Panchasara 
1356df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN,
136eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.cache_limit), 0,
1378b615593SMarko Zec     "Overall entry limit for hostcache");
13897d8d152SAndre Oppermann 
1396df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN,
140eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.hashsize), 0,
1418b615593SMarko Zec     "Size of TCP hostcache hashtable");
14297d8d152SAndre Oppermann 
1436df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
1446df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0,
1458b615593SMarko Zec     "Per-bucket hash limit for hostcache");
14697d8d152SAndre Oppermann 
1476df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD,
148eddfbb76SRobert Watson      &VNET_NAME(tcp_hostcache.cache_count), 0,
1498b615593SMarko Zec     "Current number of entries in hostcache");
15097d8d152SAndre Oppermann 
1516df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW,
152eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.expire), 0,
1538b615593SMarko Zec     "Expire time of TCP hostcache entries");
15497d8d152SAndre Oppermann 
1556df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW,
156eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.prune), 0,
157eddfbb76SRobert Watson     "Time between purge runs");
158dba3c508SYaroslav Tykhiy 
1596df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW,
160eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.purgeall), 0,
1618b615593SMarko Zec     "Expire all entires on next purge run");
16297d8d152SAndre Oppermann 
16397d8d152SAndre Oppermann SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
16497d8d152SAndre Oppermann     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
16597d8d152SAndre Oppermann     sysctl_tcp_hc_list, "A", "List of all hostcache entries");
16697d8d152SAndre Oppermann 
16726456380SHiren Panchasara SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, purgenow,
16826456380SHiren Panchasara     CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
16926456380SHiren Panchasara     sysctl_tcp_hc_purgenow, "I", "Immediately purge all entries");
17097d8d152SAndre Oppermann 
17197d8d152SAndre Oppermann static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
17297d8d152SAndre Oppermann 
17397d8d152SAndre Oppermann #define HOSTCACHE_HASH(ip) \
17497d8d152SAndre Oppermann 	(((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) &	\
175603724d3SBjoern A. Zeeb 	  V_tcp_hostcache.hashmask)
17697d8d152SAndre Oppermann 
17797d8d152SAndre Oppermann /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
17897d8d152SAndre Oppermann #define HOSTCACHE_HASH6(ip6)				\
17997d8d152SAndre Oppermann 	(((ip6)->s6_addr32[0] ^				\
18097d8d152SAndre Oppermann 	  (ip6)->s6_addr32[1] ^				\
18197d8d152SAndre Oppermann 	  (ip6)->s6_addr32[2] ^				\
18297d8d152SAndre Oppermann 	  (ip6)->s6_addr32[3]) &			\
183603724d3SBjoern A. Zeeb 	 V_tcp_hostcache.hashmask)
18497d8d152SAndre Oppermann 
18597d8d152SAndre Oppermann #define THC_LOCK(lp)		mtx_lock(lp)
18697d8d152SAndre Oppermann #define THC_UNLOCK(lp)		mtx_unlock(lp)
18797d8d152SAndre Oppermann 
18897d8d152SAndre Oppermann void
18997d8d152SAndre Oppermann tcp_hc_init(void)
19097d8d152SAndre Oppermann {
19132fe38f1SAndrey Zonov 	u_int cache_limit;
19297d8d152SAndre Oppermann 	int i;
19397d8d152SAndre Oppermann 
19497d8d152SAndre Oppermann 	/*
195e487a5e2SRobert Watson 	 * Initialize hostcache structures.
19697d8d152SAndre Oppermann 	 */
197603724d3SBjoern A. Zeeb 	V_tcp_hostcache.cache_count = 0;
198603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
199603724d3SBjoern A. Zeeb 	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
200603724d3SBjoern A. Zeeb 	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
201603724d3SBjoern A. Zeeb 	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
20297d8d152SAndre Oppermann 
20397d8d152SAndre Oppermann 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
204603724d3SBjoern A. Zeeb 	    &V_tcp_hostcache.hashsize);
205603724d3SBjoern A. Zeeb 	if (!powerof2(V_tcp_hostcache.hashsize)) {
20697d8d152SAndre Oppermann 		printf("WARNING: hostcache hash size is not a power of 2.\n");
207603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
20897d8d152SAndre Oppermann 	}
209603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
21097d8d152SAndre Oppermann 
21132fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
21232fe38f1SAndrey Zonov 	    &V_tcp_hostcache.bucket_limit);
21332fe38f1SAndrey Zonov 
21432fe38f1SAndrey Zonov 	cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
21532fe38f1SAndrey Zonov 	V_tcp_hostcache.cache_limit = cache_limit;
21632fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
21732fe38f1SAndrey Zonov 	    &V_tcp_hostcache.cache_limit);
21832fe38f1SAndrey Zonov 	if (V_tcp_hostcache.cache_limit > cache_limit)
21932fe38f1SAndrey Zonov 		V_tcp_hostcache.cache_limit = cache_limit;
22032fe38f1SAndrey Zonov 
22197d8d152SAndre Oppermann 	/*
222e487a5e2SRobert Watson 	 * Allocate the hash table.
22397d8d152SAndre Oppermann 	 */
224603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase = (struct hc_head *)
225603724d3SBjoern A. Zeeb 	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
22697d8d152SAndre Oppermann 		   M_HOSTCACHE, M_WAITOK | M_ZERO);
22797d8d152SAndre Oppermann 
22897d8d152SAndre Oppermann 	/*
229e487a5e2SRobert Watson 	 * Initialize the hash buckets.
23097d8d152SAndre Oppermann 	 */
231603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
232603724d3SBjoern A. Zeeb 		TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
233603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[i].hch_length = 0;
234603724d3SBjoern A. Zeeb 		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
23597d8d152SAndre Oppermann 			  NULL, MTX_DEF);
23697d8d152SAndre Oppermann 	}
23797d8d152SAndre Oppermann 
23897d8d152SAndre Oppermann 	/*
23997d8d152SAndre Oppermann 	 * Allocate the hostcache entries.
24097d8d152SAndre Oppermann 	 */
241ac957cd2SJulian Elischer 	V_tcp_hostcache.zone =
242ac957cd2SJulian Elischer 	    uma_zcreate("hostcache", sizeof(struct hc_metrics),
2434efb805cSAndre Oppermann 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
244603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
24597d8d152SAndre Oppermann 
24697d8d152SAndre Oppermann 	/*
24797d8d152SAndre Oppermann 	 * Set up periodic cache cleanup.
24897d8d152SAndre Oppermann 	 */
249fd90e2edSJung-uk Kim 	callout_init(&V_tcp_hc_callout, 1);
250ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
25121ca7b57SMarko Zec 	    tcp_hc_purge, curvnet);
25297d8d152SAndre Oppermann }
25397d8d152SAndre Oppermann 
254bc29160dSMarko Zec #ifdef VIMAGE
255bc29160dSMarko Zec void
256bc29160dSMarko Zec tcp_hc_destroy(void)
257bc29160dSMarko Zec {
258fffb9f1dSBjoern A. Zeeb 	int i;
259bc29160dSMarko Zec 
260bc29160dSMarko Zec 	callout_drain(&V_tcp_hc_callout);
261fffb9f1dSBjoern A. Zeeb 
262fffb9f1dSBjoern A. Zeeb 	/* Purge all hc entries. */
263fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(1);
264fffb9f1dSBjoern A. Zeeb 
265fffb9f1dSBjoern A. Zeeb 	/* Free the uma zone and the allocated hash table. */
266fffb9f1dSBjoern A. Zeeb 	uma_zdestroy(V_tcp_hostcache.zone);
267fffb9f1dSBjoern A. Zeeb 
268fffb9f1dSBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++)
269fffb9f1dSBjoern A. Zeeb 		mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx);
270fffb9f1dSBjoern A. Zeeb 	free(V_tcp_hostcache.hashbase, M_HOSTCACHE);
271bc29160dSMarko Zec }
272bc29160dSMarko Zec #endif
273bc29160dSMarko Zec 
27497d8d152SAndre Oppermann /*
27597d8d152SAndre Oppermann  * Internal function: look up an entry in the hostcache or return NULL.
27697d8d152SAndre Oppermann  *
27797d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
27897d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
27997d8d152SAndre Oppermann  */
28097d8d152SAndre Oppermann static struct hc_metrics *
28197d8d152SAndre Oppermann tcp_hc_lookup(struct in_conninfo *inc)
28297d8d152SAndre Oppermann {
28397d8d152SAndre Oppermann 	int hash;
28497d8d152SAndre Oppermann 	struct hc_head *hc_head;
28597d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
28697d8d152SAndre Oppermann 
2878a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
2888a56c645SHiren Panchasara 		return NULL;
2898a56c645SHiren Panchasara 
29097d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
29197d8d152SAndre Oppermann 
29297d8d152SAndre Oppermann 	/*
29397d8d152SAndre Oppermann 	 * Hash the foreign ip address.
29497d8d152SAndre Oppermann 	 */
295dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
29697d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
29797d8d152SAndre Oppermann 	else
29897d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
29997d8d152SAndre Oppermann 
300603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
30197d8d152SAndre Oppermann 
30297d8d152SAndre Oppermann 	/*
303e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
304e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
305e487a5e2SRobert Watson 	 * done.
30697d8d152SAndre Oppermann 	 */
30797d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
30897d8d152SAndre Oppermann 
30997d8d152SAndre Oppermann 	/*
310e487a5e2SRobert Watson 	 * Iterate through entries in bucket row looking for a match.
31197d8d152SAndre Oppermann 	 */
31297d8d152SAndre Oppermann 	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
313dcdb4371SBjoern A. Zeeb 		if (inc->inc_flags & INC_ISIPV6) {
314028bdf28SAndrey V. Elsukov 			/* XXX: check ip6_zoneid */
31597d8d152SAndre Oppermann 			if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
31697d8d152SAndre Oppermann 			    sizeof(inc->inc6_faddr)) == 0)
31797d8d152SAndre Oppermann 				return hc_entry;
31897d8d152SAndre Oppermann 		} else {
31997d8d152SAndre Oppermann 			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
32097d8d152SAndre Oppermann 			    sizeof(inc->inc_faddr)) == 0)
32197d8d152SAndre Oppermann 				return hc_entry;
32297d8d152SAndre Oppermann 		}
32397d8d152SAndre Oppermann 	}
32497d8d152SAndre Oppermann 
32597d8d152SAndre Oppermann 	/*
326e487a5e2SRobert Watson 	 * We were unsuccessful and didn't find anything.
32797d8d152SAndre Oppermann 	 */
32897d8d152SAndre Oppermann 	THC_UNLOCK(&hc_head->hch_mtx);
32997d8d152SAndre Oppermann 	return NULL;
33097d8d152SAndre Oppermann }
33197d8d152SAndre Oppermann 
33297d8d152SAndre Oppermann /*
333e487a5e2SRobert Watson  * Internal function: insert an entry into the hostcache or return NULL if
334e487a5e2SRobert Watson  * unable to allocate a new one.
33597d8d152SAndre Oppermann  *
33697d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
33797d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
33897d8d152SAndre Oppermann  */
33997d8d152SAndre Oppermann static struct hc_metrics *
34097d8d152SAndre Oppermann tcp_hc_insert(struct in_conninfo *inc)
34197d8d152SAndre Oppermann {
34297d8d152SAndre Oppermann 	int hash;
34397d8d152SAndre Oppermann 	struct hc_head *hc_head;
34497d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
34597d8d152SAndre Oppermann 
3468a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
3478a56c645SHiren Panchasara 		return NULL;
3488a56c645SHiren Panchasara 
34997d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
35097d8d152SAndre Oppermann 
35197d8d152SAndre Oppermann 	/*
352e487a5e2SRobert Watson 	 * Hash the foreign ip address.
35397d8d152SAndre Oppermann 	 */
354dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
35597d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
35697d8d152SAndre Oppermann 	else
35797d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
35897d8d152SAndre Oppermann 
359603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
36097d8d152SAndre Oppermann 
36197d8d152SAndre Oppermann 	/*
362e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
363e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
364e487a5e2SRobert Watson 	 * done.
36597d8d152SAndre Oppermann 	 */
36697d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
36797d8d152SAndre Oppermann 
36897d8d152SAndre Oppermann 	/*
369e487a5e2SRobert Watson 	 * If the bucket limit is reached, reuse the least-used element.
37097d8d152SAndre Oppermann 	 */
371603724d3SBjoern A. Zeeb 	if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
372603724d3SBjoern A. Zeeb 	    V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) {
37397d8d152SAndre Oppermann 		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
37497d8d152SAndre Oppermann 		/*
37597d8d152SAndre Oppermann 		 * At first we were dropping the last element, just to
376e487a5e2SRobert Watson 		 * reacquire it in the next two lines again, which isn't very
377e487a5e2SRobert Watson 		 * efficient.  Instead just reuse the least used element.
378e487a5e2SRobert Watson 		 * We may drop something that is still "in-use" but we can be
379e487a5e2SRobert Watson 		 * "lossy".
38045024be0SAndre Oppermann 		 * Just give up if this bucket row is empty and we don't have
38145024be0SAndre Oppermann 		 * anything to replace.
38297d8d152SAndre Oppermann 		 */
38345024be0SAndre Oppermann 		if (hc_entry == NULL) {
38445024be0SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
38545024be0SAndre Oppermann 			return NULL;
38645024be0SAndre Oppermann 		}
38797d8d152SAndre Oppermann 		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
388603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[hash].hch_length--;
389603724d3SBjoern A. Zeeb 		V_tcp_hostcache.cache_count--;
39078b50714SRobert Watson 		TCPSTAT_INC(tcps_hc_bucketoverflow);
391cd6c4060SAndre Oppermann #if 0
392603724d3SBjoern A. Zeeb 		uma_zfree(V_tcp_hostcache.zone, hc_entry);
393cd6c4060SAndre Oppermann #endif
39497d8d152SAndre Oppermann 	} else {
39597d8d152SAndre Oppermann 		/*
396e487a5e2SRobert Watson 		 * Allocate a new entry, or balk if not possible.
39797d8d152SAndre Oppermann 		 */
398603724d3SBjoern A. Zeeb 		hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
39997d8d152SAndre Oppermann 		if (hc_entry == NULL) {
40097d8d152SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
40197d8d152SAndre Oppermann 			return NULL;
40297d8d152SAndre Oppermann 		}
40397d8d152SAndre Oppermann 	}
40497d8d152SAndre Oppermann 
40597d8d152SAndre Oppermann 	/*
406e487a5e2SRobert Watson 	 * Initialize basic information of hostcache entry.
40797d8d152SAndre Oppermann 	 */
40897d8d152SAndre Oppermann 	bzero(hc_entry, sizeof(*hc_entry));
409028bdf28SAndrey V. Elsukov 	if (inc->inc_flags & INC_ISIPV6) {
410028bdf28SAndrey V. Elsukov 		hc_entry->ip6 = inc->inc6_faddr;
411028bdf28SAndrey V. Elsukov 		hc_entry->ip6_zoneid = inc->inc6_zoneid;
412028bdf28SAndrey V. Elsukov 	} else
41397d8d152SAndre Oppermann 		hc_entry->ip4 = inc->inc_faddr;
41497d8d152SAndre Oppermann 	hc_entry->rmx_head = hc_head;
415603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire;
41697d8d152SAndre Oppermann 
41797d8d152SAndre Oppermann 	/*
418e487a5e2SRobert Watson 	 * Put it upfront.
41997d8d152SAndre Oppermann 	 */
42097d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
421603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase[hash].hch_length++;
422603724d3SBjoern A. Zeeb 	V_tcp_hostcache.cache_count++;
42378b50714SRobert Watson 	TCPSTAT_INC(tcps_hc_added);
42497d8d152SAndre Oppermann 
42597d8d152SAndre Oppermann 	return hc_entry;
42697d8d152SAndre Oppermann }
42797d8d152SAndre Oppermann 
42897d8d152SAndre Oppermann /*
42997d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and fill out the
430e487a5e2SRobert Watson  * supplied TCP metrics structure.  Fills in NULL when no entry was found or
431e487a5e2SRobert Watson  * a value is not set.
43297d8d152SAndre Oppermann  */
43397d8d152SAndre Oppermann void
43497d8d152SAndre Oppermann tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
43597d8d152SAndre Oppermann {
43697d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
43797d8d152SAndre Oppermann 
4388a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
4398a56c645SHiren Panchasara 		return;
4408a56c645SHiren Panchasara 
44197d8d152SAndre Oppermann 	/*
442e487a5e2SRobert Watson 	 * Find the right bucket.
44397d8d152SAndre Oppermann 	 */
44497d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
44597d8d152SAndre Oppermann 
44697d8d152SAndre Oppermann 	/*
447e487a5e2SRobert Watson 	 * If we don't have an existing object.
44897d8d152SAndre Oppermann 	 */
44997d8d152SAndre Oppermann 	if (hc_entry == NULL) {
45097d8d152SAndre Oppermann 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
45197d8d152SAndre Oppermann 		return;
45297d8d152SAndre Oppermann 	}
45397d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
454603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
45597d8d152SAndre Oppermann 
45697d8d152SAndre Oppermann 	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
45797d8d152SAndre Oppermann 	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
45897d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
45997d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
46097d8d152SAndre Oppermann 	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
46197d8d152SAndre Oppermann 	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
46297d8d152SAndre Oppermann 	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
46397d8d152SAndre Oppermann 
46497d8d152SAndre Oppermann 	/*
465e487a5e2SRobert Watson 	 * Unlock bucket row.
46697d8d152SAndre Oppermann 	 */
46797d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
46897d8d152SAndre Oppermann }
46997d8d152SAndre Oppermann 
47097d8d152SAndre Oppermann /*
47197d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and return the
4728a56c645SHiren Panchasara  * discovered path MTU.  Returns 0 if no entry is found or value is not
4736fbed4afSRobert Watson  * set.
47497d8d152SAndre Oppermann  */
4753ac12506SJonathan T. Looney uint32_t
47697d8d152SAndre Oppermann tcp_hc_getmtu(struct in_conninfo *inc)
47797d8d152SAndre Oppermann {
47897d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
4793ac12506SJonathan T. Looney 	uint32_t mtu;
48097d8d152SAndre Oppermann 
4818a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
4828a56c645SHiren Panchasara 		return 0;
4838a56c645SHiren Panchasara 
48497d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
48597d8d152SAndre Oppermann 	if (hc_entry == NULL) {
48697d8d152SAndre Oppermann 		return 0;
48797d8d152SAndre Oppermann 	}
48897d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
489603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
49097d8d152SAndre Oppermann 
49197d8d152SAndre Oppermann 	mtu = hc_entry->rmx_mtu;
49297d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
49397d8d152SAndre Oppermann 	return mtu;
49497d8d152SAndre Oppermann }
49597d8d152SAndre Oppermann 
49697d8d152SAndre Oppermann /*
497e487a5e2SRobert Watson  * External function: update the MTU value of an entry in the hostcache.
49897d8d152SAndre Oppermann  * Creates a new entry if none was found.
49997d8d152SAndre Oppermann  */
50097d8d152SAndre Oppermann void
5013ac12506SJonathan T. Looney tcp_hc_updatemtu(struct in_conninfo *inc, uint32_t mtu)
50297d8d152SAndre Oppermann {
50397d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
50497d8d152SAndre Oppermann 
5058a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
5068a56c645SHiren Panchasara 		return;
5078a56c645SHiren Panchasara 
50897d8d152SAndre Oppermann 	/*
509e487a5e2SRobert Watson 	 * Find the right bucket.
51097d8d152SAndre Oppermann 	 */
51197d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
51297d8d152SAndre Oppermann 
51397d8d152SAndre Oppermann 	/*
514e487a5e2SRobert Watson 	 * If we don't have an existing object, try to insert a new one.
51597d8d152SAndre Oppermann 	 */
51697d8d152SAndre Oppermann 	if (hc_entry == NULL) {
51797d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
51897d8d152SAndre Oppermann 		if (hc_entry == NULL)
51997d8d152SAndre Oppermann 			return;
52097d8d152SAndre Oppermann 	}
52197d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
522603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
52397d8d152SAndre Oppermann 
52497d8d152SAndre Oppermann 	hc_entry->rmx_mtu = mtu;
52597d8d152SAndre Oppermann 
52697d8d152SAndre Oppermann 	/*
527e487a5e2SRobert Watson 	 * Put it upfront so we find it faster next time.
52897d8d152SAndre Oppermann 	 */
52997d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
53097d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
53197d8d152SAndre Oppermann 
53297d8d152SAndre Oppermann 	/*
533e487a5e2SRobert Watson 	 * Unlock bucket row.
53497d8d152SAndre Oppermann 	 */
53597d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
53697d8d152SAndre Oppermann }
53797d8d152SAndre Oppermann 
53897d8d152SAndre Oppermann /*
539e487a5e2SRobert Watson  * External function: update the TCP metrics of an entry in the hostcache.
54097d8d152SAndre Oppermann  * Creates a new entry if none was found.
54197d8d152SAndre Oppermann  */
54297d8d152SAndre Oppermann void
54397d8d152SAndre Oppermann tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
54497d8d152SAndre Oppermann {
54597d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
54697d8d152SAndre Oppermann 
5478a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
5488a56c645SHiren Panchasara 		return;
5498a56c645SHiren Panchasara 
55097d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
55197d8d152SAndre Oppermann 	if (hc_entry == NULL) {
55297d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
55397d8d152SAndre Oppermann 		if (hc_entry == NULL)
55497d8d152SAndre Oppermann 			return;
55597d8d152SAndre Oppermann 	}
55697d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
557603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
55897d8d152SAndre Oppermann 
55997d8d152SAndre Oppermann 	if (hcml->rmx_rtt != 0) {
56097d8d152SAndre Oppermann 		if (hc_entry->rmx_rtt == 0)
56197d8d152SAndre Oppermann 			hc_entry->rmx_rtt = hcml->rmx_rtt;
56297d8d152SAndre Oppermann 		else
5633ac12506SJonathan T. Looney 			hc_entry->rmx_rtt = ((uint64_t)hc_entry->rmx_rtt +
5643ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_rtt) / 2;
56578b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrtt);
56697d8d152SAndre Oppermann 	}
56797d8d152SAndre Oppermann 	if (hcml->rmx_rttvar != 0) {
56897d8d152SAndre Oppermann 	        if (hc_entry->rmx_rttvar == 0)
56997d8d152SAndre Oppermann 			hc_entry->rmx_rttvar = hcml->rmx_rttvar;
57097d8d152SAndre Oppermann 		else
5713ac12506SJonathan T. Looney 			hc_entry->rmx_rttvar = ((uint64_t)hc_entry->rmx_rttvar +
5723ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_rttvar) / 2;
57378b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrttvar);
57497d8d152SAndre Oppermann 	}
57597d8d152SAndre Oppermann 	if (hcml->rmx_ssthresh != 0) {
57697d8d152SAndre Oppermann 		if (hc_entry->rmx_ssthresh == 0)
57797d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
57897d8d152SAndre Oppermann 		else
57997d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh =
58097d8d152SAndre Oppermann 			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
58178b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedssthresh);
58297d8d152SAndre Oppermann 	}
58397d8d152SAndre Oppermann 	if (hcml->rmx_cwnd != 0) {
58497d8d152SAndre Oppermann 		if (hc_entry->rmx_cwnd == 0)
58597d8d152SAndre Oppermann 			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
58697d8d152SAndre Oppermann 		else
5873ac12506SJonathan T. Looney 			hc_entry->rmx_cwnd = ((uint64_t)hc_entry->rmx_cwnd +
5883ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_cwnd) / 2;
58978b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedcwnd); */
59097d8d152SAndre Oppermann 	}
59197d8d152SAndre Oppermann 	if (hcml->rmx_sendpipe != 0) {
59297d8d152SAndre Oppermann 		if (hc_entry->rmx_sendpipe == 0)
59397d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
59497d8d152SAndre Oppermann 		else
59597d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe =
5963ac12506SJonathan T. Looney 			    ((uint64_t)hc_entry->rmx_sendpipe +
5973ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_sendpipe) /2;
59878b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedsendpipe); */
59997d8d152SAndre Oppermann 	}
60097d8d152SAndre Oppermann 	if (hcml->rmx_recvpipe != 0) {
60197d8d152SAndre Oppermann 		if (hc_entry->rmx_recvpipe == 0)
60297d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
60397d8d152SAndre Oppermann 		else
60497d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe =
6053ac12506SJonathan T. Looney 			    ((uint64_t)hc_entry->rmx_recvpipe +
6063ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_recvpipe) /2;
60778b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedrecvpipe); */
60897d8d152SAndre Oppermann 	}
60997d8d152SAndre Oppermann 
61097d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
61197d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
61297d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
61397d8d152SAndre Oppermann }
61497d8d152SAndre Oppermann 
61597d8d152SAndre Oppermann /*
61697d8d152SAndre Oppermann  * Sysctl function: prints the list and values of all hostcache entries in
61797d8d152SAndre Oppermann  * unsorted order.
61897d8d152SAndre Oppermann  */
61997d8d152SAndre Oppermann static int
62097d8d152SAndre Oppermann sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
62197d8d152SAndre Oppermann {
622751ccc42SIan Lepore 	const int linesize = 128;
623002d4558SJohn Baldwin 	struct sbuf sb;
624002d4558SJohn Baldwin 	int i, error;
62597d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
6261d54aa3bSBjoern A. Zeeb #ifdef INET6
6271d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
6281d54aa3bSBjoern A. Zeeb #endif
62997d8d152SAndre Oppermann 
630*b8a2fb91SHiren Panchasara 	if (jailed_without_vnet(curthread->td_ucred) != 0)
631*b8a2fb91SHiren Panchasara 		return (EPERM);
632*b8a2fb91SHiren Panchasara 
633dcdeb95fSIan Lepore 	sbuf_new(&sb, NULL, linesize * (V_tcp_hostcache.cache_count + 1),
634dcdeb95fSIan Lepore 		SBUF_INCLUDENUL);
63597d8d152SAndre Oppermann 
636002d4558SJohn Baldwin 	sbuf_printf(&sb,
6374d163382SHiren Panchasara 	        "\nIP address        MTU  SSTRESH      RTT   RTTVAR "
63897d8d152SAndre Oppermann 		"    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
63997d8d152SAndre Oppermann 
64097d8d152SAndre Oppermann #define msec(u) (((u) + 500) / 1000)
641603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
642603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
643603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
64497d8d152SAndre Oppermann 			      rmx_q) {
645002d4558SJohn Baldwin 			sbuf_printf(&sb,
6463ac12506SJonathan T. Looney 			    "%-15s %5u %8u %6lums %6lums %8u %8u %8u %4lu "
6474d163382SHiren Panchasara 			    "%4lu %4i\n",
64897d8d152SAndre Oppermann 			    hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
64997d8d152SAndre Oppermann #ifdef INET6
6501d54aa3bSBjoern A. Zeeb 				ip6_sprintf(ip6buf, &hc_entry->ip6),
65197d8d152SAndre Oppermann #else
65297d8d152SAndre Oppermann 				"IPv6?",
65397d8d152SAndre Oppermann #endif
65497d8d152SAndre Oppermann 			    hc_entry->rmx_mtu,
65597d8d152SAndre Oppermann 			    hc_entry->rmx_ssthresh,
6563ac12506SJonathan T. Looney 			    msec((u_long)hc_entry->rmx_rtt *
65797d8d152SAndre Oppermann 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
6583ac12506SJonathan T. Looney 			    msec((u_long)hc_entry->rmx_rttvar *
6593a288e90SMikolaj Golub 				(RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))),
66097d8d152SAndre Oppermann 			    hc_entry->rmx_cwnd,
66197d8d152SAndre Oppermann 			    hc_entry->rmx_sendpipe,
66297d8d152SAndre Oppermann 			    hc_entry->rmx_recvpipe,
66397d8d152SAndre Oppermann 			    hc_entry->rmx_hits,
66497d8d152SAndre Oppermann 			    hc_entry->rmx_updates,
66597d8d152SAndre Oppermann 			    hc_entry->rmx_expire);
66697d8d152SAndre Oppermann 		}
667603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
66897d8d152SAndre Oppermann 	}
66997d8d152SAndre Oppermann #undef msec
670751ccc42SIan Lepore 	error = sbuf_finish(&sb);
671dcdeb95fSIan Lepore 	if (error == 0)
672dcdeb95fSIan Lepore 		error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
673002d4558SJohn Baldwin 	sbuf_delete(&sb);
67497d8d152SAndre Oppermann 	return(error);
67597d8d152SAndre Oppermann }
67697d8d152SAndre Oppermann 
67797d8d152SAndre Oppermann /*
678fffb9f1dSBjoern A. Zeeb  * Caller has to make sure the curvnet is set properly.
67997d8d152SAndre Oppermann  */
68097d8d152SAndre Oppermann static void
681fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(int all)
68297d8d152SAndre Oppermann {
683b62dccc7SAndre Oppermann 	struct hc_metrics *hc_entry, *hc_next;
68497d8d152SAndre Oppermann 	int i;
68597d8d152SAndre Oppermann 
686603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
687603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
688ac957cd2SJulian Elischer 		TAILQ_FOREACH_SAFE(hc_entry,
6895ed3800eSJulian Elischer 		    &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
69097d8d152SAndre Oppermann 			if (all || hc_entry->rmx_expire <= 0) {
691603724d3SBjoern A. Zeeb 				TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
69297d8d152SAndre Oppermann 					      hc_entry, rmx_q);
693603724d3SBjoern A. Zeeb 				uma_zfree(V_tcp_hostcache.zone, hc_entry);
694603724d3SBjoern A. Zeeb 				V_tcp_hostcache.hashbase[i].hch_length--;
695603724d3SBjoern A. Zeeb 				V_tcp_hostcache.cache_count--;
69697d8d152SAndre Oppermann 			} else
697603724d3SBjoern A. Zeeb 				hc_entry->rmx_expire -= V_tcp_hostcache.prune;
69897d8d152SAndre Oppermann 		}
699603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
70097d8d152SAndre Oppermann 	}
701fffb9f1dSBjoern A. Zeeb }
702fffb9f1dSBjoern A. Zeeb 
703fffb9f1dSBjoern A. Zeeb /*
704fffb9f1dSBjoern A. Zeeb  * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
705fffb9f1dSBjoern A. Zeeb  * periodically from the callout.
706fffb9f1dSBjoern A. Zeeb  */
707fffb9f1dSBjoern A. Zeeb static void
708fffb9f1dSBjoern A. Zeeb tcp_hc_purge(void *arg)
709fffb9f1dSBjoern A. Zeeb {
710fffb9f1dSBjoern A. Zeeb 	CURVNET_SET((struct vnet *) arg);
711fffb9f1dSBjoern A. Zeeb 	int all = 0;
712fffb9f1dSBjoern A. Zeeb 
713fffb9f1dSBjoern A. Zeeb 	if (V_tcp_hostcache.purgeall) {
714fffb9f1dSBjoern A. Zeeb 		all = 1;
715fffb9f1dSBjoern A. Zeeb 		V_tcp_hostcache.purgeall = 0;
716fffb9f1dSBjoern A. Zeeb 	}
717fffb9f1dSBjoern A. Zeeb 
718fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(all);
719ac957cd2SJulian Elischer 
720ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
721ac957cd2SJulian Elischer 	    tcp_hc_purge, arg);
72221ca7b57SMarko Zec 	CURVNET_RESTORE();
72397d8d152SAndre Oppermann }
72426456380SHiren Panchasara 
72526456380SHiren Panchasara /*
72626456380SHiren Panchasara  * Expire and purge all entries in hostcache immediately.
72726456380SHiren Panchasara  */
72826456380SHiren Panchasara static int
72926456380SHiren Panchasara sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS)
73026456380SHiren Panchasara {
73126456380SHiren Panchasara 	int error, val;
73226456380SHiren Panchasara 
73326456380SHiren Panchasara 	val = 0;
73426456380SHiren Panchasara 	error = sysctl_handle_int(oidp, &val, 0, req);
73526456380SHiren Panchasara 	if (error || !req->newptr)
73626456380SHiren Panchasara 		return (error);
73726456380SHiren Panchasara 
73826456380SHiren Panchasara 	tcp_hc_purge_internal(1);
73926456380SHiren Panchasara 
74026456380SHiren Panchasara 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
74126456380SHiren Panchasara 	    tcp_hc_purge, curvnet);
74226456380SHiren Panchasara 
74326456380SHiren Panchasara 	return (0);
74426456380SHiren Panchasara }
745