xref: /freebsd/sys/netinet/tcp_hostcache.c (revision b878ec024bbee063f4181c9be08476a864fa6a7b)
1c398230bSWarner Losh /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3fe267a55SPedro F. Giffuni  *
497d8d152SAndre Oppermann  * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG
597d8d152SAndre Oppermann  * All rights reserved.
697d8d152SAndre Oppermann  *
797d8d152SAndre Oppermann  * Redistribution and use in source and binary forms, with or without
897d8d152SAndre Oppermann  * modification, are permitted provided that the following conditions
997d8d152SAndre Oppermann  * are met:
1097d8d152SAndre Oppermann  * 1. Redistributions of source code must retain the above copyright
1197d8d152SAndre Oppermann  *    notice, this list of conditions and the following disclaimer.
1297d8d152SAndre Oppermann  * 2. Redistributions in binary form must reproduce the above copyright
1397d8d152SAndre Oppermann  *    notice, this list of conditions and the following disclaimer in the
1497d8d152SAndre Oppermann  *    documentation and/or other materials provided with the distribution.
1597d8d152SAndre Oppermann  * 3. The name of the author may not be used to endorse or promote
1697d8d152SAndre Oppermann  *    products derived from this software without specific prior written
1797d8d152SAndre Oppermann  *    permission.
1897d8d152SAndre Oppermann  *
1997d8d152SAndre Oppermann  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2097d8d152SAndre Oppermann  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2197d8d152SAndre Oppermann  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2297d8d152SAndre Oppermann  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2397d8d152SAndre Oppermann  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2497d8d152SAndre Oppermann  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2597d8d152SAndre Oppermann  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2697d8d152SAndre Oppermann  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2797d8d152SAndre Oppermann  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2897d8d152SAndre Oppermann  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2997d8d152SAndre Oppermann  * SUCH DAMAGE.
3097d8d152SAndre Oppermann  */
3197d8d152SAndre Oppermann 
3297d8d152SAndre Oppermann /*
33e487a5e2SRobert Watson  * The tcp_hostcache moves the tcp-specific cached metrics from the routing
34e487a5e2SRobert Watson  * table to a dedicated structure indexed by the remote IP address.  It keeps
35e487a5e2SRobert Watson  * information on the measured TCP parameters of past TCP sessions to allow
36e487a5e2SRobert Watson  * better initial start values to be used with later connections to/from the
374d163382SHiren Panchasara  * same source.  Depending on the network parameters (delay, max MTU,
384d163382SHiren Panchasara  * congestion window) between local and remote sites, this can lead to
39e487a5e2SRobert Watson  * significant speed-ups for new TCP connections after the first one.
4097d8d152SAndre Oppermann  *
41e487a5e2SRobert Watson  * Due to the tcp_hostcache, all TCP-specific metrics information in the
42003c7e36SRui Paulo  * routing table have been removed.  The inpcb no longer keeps a pointer to
43e487a5e2SRobert Watson  * the routing entry, and protocol-initiated route cloning has been removed
44e487a5e2SRobert Watson  * as well.  With these changes, the routing table has gone back to being
45e487a5e2SRobert Watson  * more lightwight and only carries information related to packet forwarding.
4697d8d152SAndre Oppermann  *
47e487a5e2SRobert Watson  * tcp_hostcache is designed for multiple concurrent access in SMP
48e487a5e2SRobert Watson  * environments and high contention.  All bucket rows have their own lock and
49e487a5e2SRobert Watson  * thus multiple lookups and modifies can be done at the same time as long as
50e487a5e2SRobert Watson  * they are in different bucket rows.  If a request for insertion of a new
51e487a5e2SRobert Watson  * record can't be satisfied, it simply returns an empty structure.  Nobody
52e487a5e2SRobert Watson  * and nothing outside of tcp_hostcache.c will ever point directly to any
53e487a5e2SRobert Watson  * entry in the tcp_hostcache.  All communication is done in an
54e487a5e2SRobert Watson  * object-oriented way and only functions of tcp_hostcache will manipulate
55e487a5e2SRobert Watson  * hostcache entries.  Otherwise, we are unable to achieve good behaviour in
56e487a5e2SRobert Watson  * concurrent access situations.  Since tcp_hostcache is only caching
57e487a5e2SRobert Watson  * information, there are no fatal consequences if we either can't satisfy
58e487a5e2SRobert Watson  * any particular request or have to drop/overwrite an existing entry because
59e487a5e2SRobert Watson  * of bucket limit memory constrains.
6097d8d152SAndre Oppermann  */
6197d8d152SAndre Oppermann 
6297d8d152SAndre Oppermann /*
6397d8d152SAndre Oppermann  * Many thanks to jlemon for basic structure of tcp_syncache which is being
6497d8d152SAndre Oppermann  * followed here.
6597d8d152SAndre Oppermann  */
6697d8d152SAndre Oppermann 
674b421e2dSMike Silbersack #include <sys/cdefs.h>
684b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
694b421e2dSMike Silbersack 
7097d8d152SAndre Oppermann #include "opt_inet6.h"
7197d8d152SAndre Oppermann 
7297d8d152SAndre Oppermann #include <sys/param.h>
7397d8d152SAndre Oppermann #include <sys/systm.h>
74*b878ec02SRichard Scheffenegger #include <sys/hash.h>
75b8a2fb91SHiren Panchasara #include <sys/jail.h>
7697d8d152SAndre Oppermann #include <sys/kernel.h>
7797d8d152SAndre Oppermann #include <sys/lock.h>
7897d8d152SAndre Oppermann #include <sys/mutex.h>
7997d8d152SAndre Oppermann #include <sys/malloc.h>
80b8a2fb91SHiren Panchasara #include <sys/proc.h>
81002d4558SJohn Baldwin #include <sys/sbuf.h>
8297d8d152SAndre Oppermann #include <sys/socket.h>
8397d8d152SAndre Oppermann #include <sys/socketvar.h>
8497d8d152SAndre Oppermann #include <sys/sysctl.h>
8597d8d152SAndre Oppermann 
86530c0060SRobert Watson #include <net/vnet.h>
8797d8d152SAndre Oppermann 
8897d8d152SAndre Oppermann #include <netinet/in.h>
8997d8d152SAndre Oppermann #include <netinet/in_pcb.h>
9097d8d152SAndre Oppermann #include <netinet/tcp.h>
9197d8d152SAndre Oppermann #include <netinet/tcp_var.h>
9297d8d152SAndre Oppermann 
9397d8d152SAndre Oppermann #include <vm/uma.h>
9497d8d152SAndre Oppermann 
952cca4c0eSGleb Smirnoff TAILQ_HEAD(hc_qhead, hc_metrics);
962cca4c0eSGleb Smirnoff 
972cca4c0eSGleb Smirnoff struct hc_head {
982cca4c0eSGleb Smirnoff 	struct hc_qhead	hch_bucket;
992cca4c0eSGleb Smirnoff 	u_int		hch_length;
1002cca4c0eSGleb Smirnoff 	struct mtx	hch_mtx;
1012cca4c0eSGleb Smirnoff };
1022cca4c0eSGleb Smirnoff 
1032cca4c0eSGleb Smirnoff struct hc_metrics {
1042cca4c0eSGleb Smirnoff 	/* housekeeping */
1052cca4c0eSGleb Smirnoff 	TAILQ_ENTRY(hc_metrics) rmx_q;
1062cca4c0eSGleb Smirnoff 	struct		hc_head *rmx_head; /* head of bucket tail queue */
1072cca4c0eSGleb Smirnoff 	struct		in_addr ip4;	/* IP address */
1082cca4c0eSGleb Smirnoff 	struct		in6_addr ip6;	/* IP6 address */
1092cca4c0eSGleb Smirnoff 	uint32_t	ip6_zoneid;	/* IPv6 scope zone id */
1102cca4c0eSGleb Smirnoff 	/* endpoint specific values for tcp */
1112cca4c0eSGleb Smirnoff 	uint32_t	rmx_mtu;	/* MTU for this path */
1122cca4c0eSGleb Smirnoff 	uint32_t	rmx_ssthresh;	/* outbound gateway buffer limit */
1132cca4c0eSGleb Smirnoff 	uint32_t	rmx_rtt;	/* estimated round trip time */
1142cca4c0eSGleb Smirnoff 	uint32_t	rmx_rttvar;	/* estimated rtt variance */
1152cca4c0eSGleb Smirnoff 	uint32_t	rmx_cwnd;	/* congestion window */
1162cca4c0eSGleb Smirnoff 	uint32_t	rmx_sendpipe;	/* outbound delay-bandwidth product */
1172cca4c0eSGleb Smirnoff 	uint32_t	rmx_recvpipe;	/* inbound delay-bandwidth product */
1182cca4c0eSGleb Smirnoff 	/* TCP hostcache internal data */
1192cca4c0eSGleb Smirnoff 	int		rmx_expire;	/* lifetime for object */
120489bde57SGleb Smirnoff #ifdef	TCP_HC_COUNTERS
1212cca4c0eSGleb Smirnoff 	u_long		rmx_hits;	/* number of hits */
1222cca4c0eSGleb Smirnoff 	u_long		rmx_updates;	/* number of updates */
123489bde57SGleb Smirnoff #endif
1242cca4c0eSGleb Smirnoff };
1252cca4c0eSGleb Smirnoff 
1262cca4c0eSGleb Smirnoff struct tcp_hostcache {
1272cca4c0eSGleb Smirnoff 	struct hc_head	*hashbase;
1282cca4c0eSGleb Smirnoff 	uma_zone_t	zone;
1292cca4c0eSGleb Smirnoff 	u_int		hashsize;
1302cca4c0eSGleb Smirnoff 	u_int		hashmask;
131*b878ec02SRichard Scheffenegger 	u_int		hashsalt;
1322cca4c0eSGleb Smirnoff 	u_int		bucket_limit;
1332cca4c0eSGleb Smirnoff 	u_int		cache_count;
1342cca4c0eSGleb Smirnoff 	u_int		cache_limit;
1352cca4c0eSGleb Smirnoff 	int		expire;
1362cca4c0eSGleb Smirnoff 	int		prune;
1372cca4c0eSGleb Smirnoff 	int		purgeall;
1382cca4c0eSGleb Smirnoff };
1392cca4c0eSGleb Smirnoff 
14097d8d152SAndre Oppermann /* Arbitrary values */
14197d8d152SAndre Oppermann #define TCP_HOSTCACHE_HASHSIZE		512
14297d8d152SAndre Oppermann #define TCP_HOSTCACHE_BUCKETLIMIT	30
14397d8d152SAndre Oppermann #define TCP_HOSTCACHE_EXPIRE		60*60	/* one hour */
14497d8d152SAndre Oppermann #define TCP_HOSTCACHE_PRUNE		5*60	/* every 5 minutes */
14597d8d152SAndre Oppermann 
1465f901c92SAndrew Turner VNET_DEFINE_STATIC(struct tcp_hostcache, tcp_hostcache);
1471e77c105SRobert Watson #define	V_tcp_hostcache		VNET(tcp_hostcache)
14882cea7e6SBjoern A. Zeeb 
1495f901c92SAndrew Turner VNET_DEFINE_STATIC(struct callout, tcp_hc_callout);
1501e77c105SRobert Watson #define	V_tcp_hc_callout	VNET(tcp_hc_callout)
15197d8d152SAndre Oppermann 
15229acb543SGleb Smirnoff static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *, bool);
15397d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
15497d8d152SAndre Oppermann static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
15502f26e98SRichard Scheffenegger static int sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS);
15626456380SHiren Panchasara static int sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS);
157fffb9f1dSBjoern A. Zeeb static void tcp_hc_purge_internal(int);
15897d8d152SAndre Oppermann static void tcp_hc_purge(void *);
15997d8d152SAndre Oppermann 
1607029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache,
1617029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1620eb7cf4dSBrooks Davis     "TCP Host cache");
16397d8d152SAndre Oppermann 
1648a56c645SHiren Panchasara VNET_DEFINE(int, tcp_use_hostcache) = 1;
1658a56c645SHiren Panchasara #define V_tcp_use_hostcache  VNET(tcp_use_hostcache)
1668a56c645SHiren Panchasara SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
1678a56c645SHiren Panchasara     &VNET_NAME(tcp_use_hostcache), 0,
1688a56c645SHiren Panchasara     "Enable the TCP hostcache");
1698a56c645SHiren Panchasara 
1706df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN,
171eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.cache_limit), 0,
1728b615593SMarko Zec     "Overall entry limit for hostcache");
17397d8d152SAndre Oppermann 
1746df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN,
175eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.hashsize), 0,
1768b615593SMarko Zec     "Size of TCP hostcache hashtable");
17797d8d152SAndre Oppermann 
1786df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
1796df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0,
1808b615593SMarko Zec     "Per-bucket hash limit for hostcache");
18197d8d152SAndre Oppermann 
182529a2a0fSRichard Scheffenegger SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD,
183529a2a0fSRichard Scheffenegger      &VNET_NAME(tcp_hostcache.cache_count), 0,
1848b615593SMarko Zec     "Current number of entries in hostcache");
18597d8d152SAndre Oppermann 
1866df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW,
187eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.expire), 0,
1888b615593SMarko Zec     "Expire time of TCP hostcache entries");
18997d8d152SAndre Oppermann 
1906df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW,
191eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.prune), 0,
192eddfbb76SRobert Watson     "Time between purge runs");
193dba3c508SYaroslav Tykhiy 
1946df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW,
195eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.purgeall), 0,
1968b615593SMarko Zec     "Expire all entires on next purge run");
19797d8d152SAndre Oppermann 
19897d8d152SAndre Oppermann SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
1997029da5cSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE,
2007029da5cSPawel Biernacki     0, 0, sysctl_tcp_hc_list, "A",
2017029da5cSPawel Biernacki     "List of all hostcache entries");
20297d8d152SAndre Oppermann 
20302f26e98SRichard Scheffenegger SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, histo,
20402f26e98SRichard Scheffenegger     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE,
20502f26e98SRichard Scheffenegger     0, 0, sysctl_tcp_hc_histo, "A",
20602f26e98SRichard Scheffenegger     "Print a histogram of hostcache hashbucket utilization");
20702f26e98SRichard Scheffenegger 
20826456380SHiren Panchasara SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, purgenow,
2097029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2107029da5cSPawel Biernacki     NULL, 0, sysctl_tcp_hc_purgenow, "I",
2117029da5cSPawel Biernacki     "Immediately purge all entries");
21297d8d152SAndre Oppermann 
21397d8d152SAndre Oppermann static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
21497d8d152SAndre Oppermann 
215*b878ec02SRichard Scheffenegger /* Use jenkins_hash32(), as in other parts of the tcp stack */
21697d8d152SAndre Oppermann #define HOSTCACHE_HASH(ip) \
217*b878ec02SRichard Scheffenegger 	(jenkins_hash32((uint32_t *)(ip), 1, V_tcp_hostcache.hashsalt) & \
218603724d3SBjoern A. Zeeb 	 V_tcp_hostcache.hashmask)
21997d8d152SAndre Oppermann 
22097d8d152SAndre Oppermann #define HOSTCACHE_HASH6(ip6)				\
221*b878ec02SRichard Scheffenegger 	(jenkins_hash32((uint32_t *)&((ip6)->s6_addr32[0]), 4, \
222*b878ec02SRichard Scheffenegger 	 V_tcp_hostcache.hashsalt) & \
223603724d3SBjoern A. Zeeb 	 V_tcp_hostcache.hashmask)
22497d8d152SAndre Oppermann 
22597d8d152SAndre Oppermann #define THC_LOCK(lp)		mtx_lock(lp)
22697d8d152SAndre Oppermann #define THC_UNLOCK(lp)		mtx_unlock(lp)
22797d8d152SAndre Oppermann 
22897d8d152SAndre Oppermann void
22997d8d152SAndre Oppermann tcp_hc_init(void)
23097d8d152SAndre Oppermann {
23132fe38f1SAndrey Zonov 	u_int cache_limit;
23297d8d152SAndre Oppermann 	int i;
23397d8d152SAndre Oppermann 
23497d8d152SAndre Oppermann 	/*
235e487a5e2SRobert Watson 	 * Initialize hostcache structures.
23697d8d152SAndre Oppermann 	 */
237529a2a0fSRichard Scheffenegger 	atomic_store_int(&V_tcp_hostcache.cache_count, 0);
238603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
239603724d3SBjoern A. Zeeb 	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
240603724d3SBjoern A. Zeeb 	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
241603724d3SBjoern A. Zeeb 	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
242*b878ec02SRichard Scheffenegger 	V_tcp_hostcache.hashsalt = arc4random();
24397d8d152SAndre Oppermann 
24497d8d152SAndre Oppermann 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
245603724d3SBjoern A. Zeeb 	    &V_tcp_hostcache.hashsize);
246603724d3SBjoern A. Zeeb 	if (!powerof2(V_tcp_hostcache.hashsize)) {
24797d8d152SAndre Oppermann 		printf("WARNING: hostcache hash size is not a power of 2.\n");
248603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
24997d8d152SAndre Oppermann 	}
250603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
25197d8d152SAndre Oppermann 
25232fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
25332fe38f1SAndrey Zonov 	    &V_tcp_hostcache.bucket_limit);
25432fe38f1SAndrey Zonov 
25532fe38f1SAndrey Zonov 	cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
25632fe38f1SAndrey Zonov 	V_tcp_hostcache.cache_limit = cache_limit;
25732fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
25832fe38f1SAndrey Zonov 	    &V_tcp_hostcache.cache_limit);
25932fe38f1SAndrey Zonov 	if (V_tcp_hostcache.cache_limit > cache_limit)
26032fe38f1SAndrey Zonov 		V_tcp_hostcache.cache_limit = cache_limit;
26132fe38f1SAndrey Zonov 
26297d8d152SAndre Oppermann 	/*
263e487a5e2SRobert Watson 	 * Allocate the hash table.
26497d8d152SAndre Oppermann 	 */
265603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase = (struct hc_head *)
266603724d3SBjoern A. Zeeb 	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
26797d8d152SAndre Oppermann 		   M_HOSTCACHE, M_WAITOK | M_ZERO);
26897d8d152SAndre Oppermann 
26997d8d152SAndre Oppermann 	/*
270e487a5e2SRobert Watson 	 * Initialize the hash buckets.
27197d8d152SAndre Oppermann 	 */
272603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
273603724d3SBjoern A. Zeeb 		TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
274603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[i].hch_length = 0;
275603724d3SBjoern A. Zeeb 		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
27697d8d152SAndre Oppermann 			  NULL, MTX_DEF);
27797d8d152SAndre Oppermann 	}
27897d8d152SAndre Oppermann 
27997d8d152SAndre Oppermann 	/*
28097d8d152SAndre Oppermann 	 * Allocate the hostcache entries.
28197d8d152SAndre Oppermann 	 */
282ac957cd2SJulian Elischer 	V_tcp_hostcache.zone =
283ac957cd2SJulian Elischer 	    uma_zcreate("hostcache", sizeof(struct hc_metrics),
2844efb805cSAndre Oppermann 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
285603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
28697d8d152SAndre Oppermann 
28797d8d152SAndre Oppermann 	/*
28897d8d152SAndre Oppermann 	 * Set up periodic cache cleanup.
28997d8d152SAndre Oppermann 	 */
290fd90e2edSJung-uk Kim 	callout_init(&V_tcp_hc_callout, 1);
291ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
29221ca7b57SMarko Zec 	    tcp_hc_purge, curvnet);
29397d8d152SAndre Oppermann }
29497d8d152SAndre Oppermann 
295bc29160dSMarko Zec #ifdef VIMAGE
296bc29160dSMarko Zec void
297bc29160dSMarko Zec tcp_hc_destroy(void)
298bc29160dSMarko Zec {
299fffb9f1dSBjoern A. Zeeb 	int i;
300bc29160dSMarko Zec 
301bc29160dSMarko Zec 	callout_drain(&V_tcp_hc_callout);
302fffb9f1dSBjoern A. Zeeb 
303fffb9f1dSBjoern A. Zeeb 	/* Purge all hc entries. */
304fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(1);
305fffb9f1dSBjoern A. Zeeb 
306fffb9f1dSBjoern A. Zeeb 	/* Free the uma zone and the allocated hash table. */
307fffb9f1dSBjoern A. Zeeb 	uma_zdestroy(V_tcp_hostcache.zone);
308fffb9f1dSBjoern A. Zeeb 
309fffb9f1dSBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++)
310fffb9f1dSBjoern A. Zeeb 		mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx);
311fffb9f1dSBjoern A. Zeeb 	free(V_tcp_hostcache.hashbase, M_HOSTCACHE);
312bc29160dSMarko Zec }
313bc29160dSMarko Zec #endif
314bc29160dSMarko Zec 
31597d8d152SAndre Oppermann /*
31697d8d152SAndre Oppermann  * Internal function: look up an entry in the hostcache or return NULL.
31797d8d152SAndre Oppermann  *
31897d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
31997d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
32097d8d152SAndre Oppermann  */
32197d8d152SAndre Oppermann static struct hc_metrics *
32229acb543SGleb Smirnoff tcp_hc_lookup(struct in_conninfo *inc, bool update)
32397d8d152SAndre Oppermann {
32497d8d152SAndre Oppermann 	int hash;
32597d8d152SAndre Oppermann 	struct hc_head *hc_head;
32697d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
32797d8d152SAndre Oppermann 
3288a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
3298a56c645SHiren Panchasara 		return NULL;
3308a56c645SHiren Panchasara 
33197d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
33297d8d152SAndre Oppermann 
33397d8d152SAndre Oppermann 	/*
33497d8d152SAndre Oppermann 	 * Hash the foreign ip address.
33597d8d152SAndre Oppermann 	 */
336dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
33797d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
33897d8d152SAndre Oppermann 	else
33997d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
34097d8d152SAndre Oppermann 
341603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
34297d8d152SAndre Oppermann 
34397d8d152SAndre Oppermann 	/*
344e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
345e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
346e487a5e2SRobert Watson 	 * done.
34797d8d152SAndre Oppermann 	 */
34897d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
34997d8d152SAndre Oppermann 
35097d8d152SAndre Oppermann 	/*
351e487a5e2SRobert Watson 	 * Iterate through entries in bucket row looking for a match.
35297d8d152SAndre Oppermann 	 */
35397d8d152SAndre Oppermann 	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
354dcdb4371SBjoern A. Zeeb 		if (inc->inc_flags & INC_ISIPV6) {
355028bdf28SAndrey V. Elsukov 			/* XXX: check ip6_zoneid */
35697d8d152SAndre Oppermann 			if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
35797d8d152SAndre Oppermann 			    sizeof(inc->inc6_faddr)) == 0)
35829acb543SGleb Smirnoff 				goto found;
35997d8d152SAndre Oppermann 		} else {
36097d8d152SAndre Oppermann 			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
36197d8d152SAndre Oppermann 			    sizeof(inc->inc_faddr)) == 0)
36229acb543SGleb Smirnoff 				goto found;
36397d8d152SAndre Oppermann 		}
36497d8d152SAndre Oppermann 	}
36597d8d152SAndre Oppermann 
36697d8d152SAndre Oppermann 	/*
367e487a5e2SRobert Watson 	 * We were unsuccessful and didn't find anything.
36897d8d152SAndre Oppermann 	 */
36997d8d152SAndre Oppermann 	THC_UNLOCK(&hc_head->hch_mtx);
37029acb543SGleb Smirnoff 	return (NULL);
37129acb543SGleb Smirnoff 
37229acb543SGleb Smirnoff found:
37329acb543SGleb Smirnoff #ifdef	TCP_HC_COUNTERS
37429acb543SGleb Smirnoff 	if (update)
37529acb543SGleb Smirnoff 		hc_entry->rmx_updates++;
37629acb543SGleb Smirnoff 	else
37729acb543SGleb Smirnoff 		hc_entry->rmx_hits++;
37829acb543SGleb Smirnoff #endif
37929acb543SGleb Smirnoff 	hc_entry->rmx_expire = V_tcp_hostcache.expire;
38029acb543SGleb Smirnoff 
38129acb543SGleb Smirnoff 	return (hc_entry);
38297d8d152SAndre Oppermann }
38397d8d152SAndre Oppermann 
38497d8d152SAndre Oppermann /*
385e487a5e2SRobert Watson  * Internal function: insert an entry into the hostcache or return NULL if
386e487a5e2SRobert Watson  * unable to allocate a new one.
38797d8d152SAndre Oppermann  *
38897d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
38997d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
39097d8d152SAndre Oppermann  */
39197d8d152SAndre Oppermann static struct hc_metrics *
39297d8d152SAndre Oppermann tcp_hc_insert(struct in_conninfo *inc)
39397d8d152SAndre Oppermann {
39497d8d152SAndre Oppermann 	int hash;
39597d8d152SAndre Oppermann 	struct hc_head *hc_head;
39697d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
39797d8d152SAndre Oppermann 
3988a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
3998a56c645SHiren Panchasara 		return NULL;
4008a56c645SHiren Panchasara 
40197d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
40297d8d152SAndre Oppermann 
40397d8d152SAndre Oppermann 	/*
404e487a5e2SRobert Watson 	 * Hash the foreign ip address.
40597d8d152SAndre Oppermann 	 */
406dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
40797d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
40897d8d152SAndre Oppermann 	else
40997d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
41097d8d152SAndre Oppermann 
411603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
41297d8d152SAndre Oppermann 
41397d8d152SAndre Oppermann 	/*
414e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
415e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
416e487a5e2SRobert Watson 	 * done.
41797d8d152SAndre Oppermann 	 */
41897d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
41997d8d152SAndre Oppermann 
42097d8d152SAndre Oppermann 	/*
421e487a5e2SRobert Watson 	 * If the bucket limit is reached, reuse the least-used element.
42297d8d152SAndre Oppermann 	 */
423603724d3SBjoern A. Zeeb 	if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
424529a2a0fSRichard Scheffenegger 	    atomic_load_int(&V_tcp_hostcache.cache_count) >= V_tcp_hostcache.cache_limit) {
42597d8d152SAndre Oppermann 		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
42697d8d152SAndre Oppermann 		/*
42797d8d152SAndre Oppermann 		 * At first we were dropping the last element, just to
428e487a5e2SRobert Watson 		 * reacquire it in the next two lines again, which isn't very
429e487a5e2SRobert Watson 		 * efficient.  Instead just reuse the least used element.
430e487a5e2SRobert Watson 		 * We may drop something that is still "in-use" but we can be
431e487a5e2SRobert Watson 		 * "lossy".
43245024be0SAndre Oppermann 		 * Just give up if this bucket row is empty and we don't have
43345024be0SAndre Oppermann 		 * anything to replace.
43497d8d152SAndre Oppermann 		 */
43545024be0SAndre Oppermann 		if (hc_entry == NULL) {
43645024be0SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
43745024be0SAndre Oppermann 			return NULL;
43845024be0SAndre Oppermann 		}
43997d8d152SAndre Oppermann 		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
44002f26e98SRichard Scheffenegger 		KASSERT(V_tcp_hostcache.hashbase[hash].hch_length > 0 &&
44102f26e98SRichard Scheffenegger 			V_tcp_hostcache.hashbase[hash].hch_length <=
44202f26e98SRichard Scheffenegger 			V_tcp_hostcache.bucket_limit,
44302f26e98SRichard Scheffenegger 			("tcp_hostcache: bucket length range violated at %u: %u",
44402f26e98SRichard Scheffenegger 			hash, V_tcp_hostcache.hashbase[hash].hch_length));
445603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[hash].hch_length--;
446529a2a0fSRichard Scheffenegger 		atomic_subtract_int(&V_tcp_hostcache.cache_count, 1);
44778b50714SRobert Watson 		TCPSTAT_INC(tcps_hc_bucketoverflow);
448cd6c4060SAndre Oppermann #if 0
449603724d3SBjoern A. Zeeb 		uma_zfree(V_tcp_hostcache.zone, hc_entry);
450cd6c4060SAndre Oppermann #endif
45197d8d152SAndre Oppermann 	} else {
45297d8d152SAndre Oppermann 		/*
453e487a5e2SRobert Watson 		 * Allocate a new entry, or balk if not possible.
45497d8d152SAndre Oppermann 		 */
455603724d3SBjoern A. Zeeb 		hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
45697d8d152SAndre Oppermann 		if (hc_entry == NULL) {
45797d8d152SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
45897d8d152SAndre Oppermann 			return NULL;
45997d8d152SAndre Oppermann 		}
46097d8d152SAndre Oppermann 	}
46197d8d152SAndre Oppermann 
46297d8d152SAndre Oppermann 	/*
463e487a5e2SRobert Watson 	 * Initialize basic information of hostcache entry.
46497d8d152SAndre Oppermann 	 */
46597d8d152SAndre Oppermann 	bzero(hc_entry, sizeof(*hc_entry));
466028bdf28SAndrey V. Elsukov 	if (inc->inc_flags & INC_ISIPV6) {
467028bdf28SAndrey V. Elsukov 		hc_entry->ip6 = inc->inc6_faddr;
468028bdf28SAndrey V. Elsukov 		hc_entry->ip6_zoneid = inc->inc6_zoneid;
469028bdf28SAndrey V. Elsukov 	} else
47097d8d152SAndre Oppermann 		hc_entry->ip4 = inc->inc_faddr;
47197d8d152SAndre Oppermann 	hc_entry->rmx_head = hc_head;
472603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire;
47397d8d152SAndre Oppermann 
47497d8d152SAndre Oppermann 	/*
475e487a5e2SRobert Watson 	 * Put it upfront.
47697d8d152SAndre Oppermann 	 */
47797d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
478603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase[hash].hch_length++;
47902f26e98SRichard Scheffenegger 	KASSERT(V_tcp_hostcache.hashbase[hash].hch_length <
48002f26e98SRichard Scheffenegger 		V_tcp_hostcache.bucket_limit,
48102f26e98SRichard Scheffenegger 		("tcp_hostcache: bucket length too high at %u: %u",
48202f26e98SRichard Scheffenegger 		hash, V_tcp_hostcache.hashbase[hash].hch_length));
483529a2a0fSRichard Scheffenegger 	atomic_add_int(&V_tcp_hostcache.cache_count, 1);
48478b50714SRobert Watson 	TCPSTAT_INC(tcps_hc_added);
48597d8d152SAndre Oppermann 
48697d8d152SAndre Oppermann 	return hc_entry;
48797d8d152SAndre Oppermann }
48897d8d152SAndre Oppermann 
48997d8d152SAndre Oppermann /*
49097d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and fill out the
491e487a5e2SRobert Watson  * supplied TCP metrics structure.  Fills in NULL when no entry was found or
492e487a5e2SRobert Watson  * a value is not set.
49397d8d152SAndre Oppermann  */
49497d8d152SAndre Oppermann void
49597d8d152SAndre Oppermann tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
49697d8d152SAndre Oppermann {
49797d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
49897d8d152SAndre Oppermann 
4996b8fba3cSMichael Tuexen 	if (!V_tcp_use_hostcache) {
5006b8fba3cSMichael Tuexen 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
5018a56c645SHiren Panchasara 		return;
5026b8fba3cSMichael Tuexen 	}
5038a56c645SHiren Panchasara 
50497d8d152SAndre Oppermann 	/*
505e487a5e2SRobert Watson 	 * Find the right bucket.
50697d8d152SAndre Oppermann 	 */
50729acb543SGleb Smirnoff 	hc_entry = tcp_hc_lookup(inc, false);
50897d8d152SAndre Oppermann 
50997d8d152SAndre Oppermann 	/*
510e487a5e2SRobert Watson 	 * If we don't have an existing object.
51197d8d152SAndre Oppermann 	 */
51297d8d152SAndre Oppermann 	if (hc_entry == NULL) {
51397d8d152SAndre Oppermann 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
51497d8d152SAndre Oppermann 		return;
51597d8d152SAndre Oppermann 	}
51697d8d152SAndre Oppermann 
51797d8d152SAndre Oppermann 	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
51897d8d152SAndre Oppermann 	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
51997d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
52097d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
52197d8d152SAndre Oppermann 	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
52297d8d152SAndre Oppermann 	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
52397d8d152SAndre Oppermann 	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
52497d8d152SAndre Oppermann 
52597d8d152SAndre Oppermann 	/*
526e487a5e2SRobert Watson 	 * Unlock bucket row.
52797d8d152SAndre Oppermann 	 */
52897d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
52997d8d152SAndre Oppermann }
53097d8d152SAndre Oppermann 
53197d8d152SAndre Oppermann /*
53297d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and return the
5338a56c645SHiren Panchasara  * discovered path MTU.  Returns 0 if no entry is found or value is not
5346fbed4afSRobert Watson  * set.
53597d8d152SAndre Oppermann  */
5363ac12506SJonathan T. Looney uint32_t
53797d8d152SAndre Oppermann tcp_hc_getmtu(struct in_conninfo *inc)
53897d8d152SAndre Oppermann {
53997d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
5403ac12506SJonathan T. Looney 	uint32_t mtu;
54197d8d152SAndre Oppermann 
5428a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
5438a56c645SHiren Panchasara 		return 0;
5448a56c645SHiren Panchasara 
54529acb543SGleb Smirnoff 	hc_entry = tcp_hc_lookup(inc, false);
54697d8d152SAndre Oppermann 	if (hc_entry == NULL) {
54797d8d152SAndre Oppermann 		return 0;
54897d8d152SAndre Oppermann 	}
54997d8d152SAndre Oppermann 
55097d8d152SAndre Oppermann 	mtu = hc_entry->rmx_mtu;
55197d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
55297d8d152SAndre Oppermann 	return mtu;
55397d8d152SAndre Oppermann }
55497d8d152SAndre Oppermann 
55597d8d152SAndre Oppermann /*
556e487a5e2SRobert Watson  * External function: update the MTU value of an entry in the hostcache.
55797d8d152SAndre Oppermann  * Creates a new entry if none was found.
55897d8d152SAndre Oppermann  */
55997d8d152SAndre Oppermann void
5603ac12506SJonathan T. Looney tcp_hc_updatemtu(struct in_conninfo *inc, uint32_t mtu)
56197d8d152SAndre Oppermann {
56297d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
56397d8d152SAndre Oppermann 
5648a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
5658a56c645SHiren Panchasara 		return;
5668a56c645SHiren Panchasara 
56797d8d152SAndre Oppermann 	/*
568e487a5e2SRobert Watson 	 * Find the right bucket.
56997d8d152SAndre Oppermann 	 */
57029acb543SGleb Smirnoff 	hc_entry = tcp_hc_lookup(inc, true);
57197d8d152SAndre Oppermann 
57297d8d152SAndre Oppermann 	/*
573e487a5e2SRobert Watson 	 * If we don't have an existing object, try to insert a new one.
57497d8d152SAndre Oppermann 	 */
57597d8d152SAndre Oppermann 	if (hc_entry == NULL) {
57697d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
57797d8d152SAndre Oppermann 		if (hc_entry == NULL)
57897d8d152SAndre Oppermann 			return;
57997d8d152SAndre Oppermann 	}
58097d8d152SAndre Oppermann 
58197d8d152SAndre Oppermann 	hc_entry->rmx_mtu = mtu;
58297d8d152SAndre Oppermann 
58397d8d152SAndre Oppermann 	/*
584e487a5e2SRobert Watson 	 * Put it upfront so we find it faster next time.
58597d8d152SAndre Oppermann 	 */
58697d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
58797d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
58897d8d152SAndre Oppermann 
58997d8d152SAndre Oppermann 	/*
590e487a5e2SRobert Watson 	 * Unlock bucket row.
59197d8d152SAndre Oppermann 	 */
59297d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
59397d8d152SAndre Oppermann }
59497d8d152SAndre Oppermann 
59597d8d152SAndre Oppermann /*
596e487a5e2SRobert Watson  * External function: update the TCP metrics of an entry in the hostcache.
59797d8d152SAndre Oppermann  * Creates a new entry if none was found.
59897d8d152SAndre Oppermann  */
59997d8d152SAndre Oppermann void
60097d8d152SAndre Oppermann tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
60197d8d152SAndre Oppermann {
60297d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
60397d8d152SAndre Oppermann 
6048a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
6058a56c645SHiren Panchasara 		return;
6068a56c645SHiren Panchasara 
60729acb543SGleb Smirnoff 	hc_entry = tcp_hc_lookup(inc, true);
60897d8d152SAndre Oppermann 	if (hc_entry == NULL) {
60997d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
61097d8d152SAndre Oppermann 		if (hc_entry == NULL)
61197d8d152SAndre Oppermann 			return;
61297d8d152SAndre Oppermann 	}
61397d8d152SAndre Oppermann 
61497d8d152SAndre Oppermann 	if (hcml->rmx_rtt != 0) {
61597d8d152SAndre Oppermann 		if (hc_entry->rmx_rtt == 0)
61697d8d152SAndre Oppermann 			hc_entry->rmx_rtt = hcml->rmx_rtt;
61797d8d152SAndre Oppermann 		else
6183ac12506SJonathan T. Looney 			hc_entry->rmx_rtt = ((uint64_t)hc_entry->rmx_rtt +
6193ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_rtt) / 2;
62078b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrtt);
62197d8d152SAndre Oppermann 	}
62297d8d152SAndre Oppermann 	if (hcml->rmx_rttvar != 0) {
62397d8d152SAndre Oppermann 	        if (hc_entry->rmx_rttvar == 0)
62497d8d152SAndre Oppermann 			hc_entry->rmx_rttvar = hcml->rmx_rttvar;
62597d8d152SAndre Oppermann 		else
6263ac12506SJonathan T. Looney 			hc_entry->rmx_rttvar = ((uint64_t)hc_entry->rmx_rttvar +
6273ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_rttvar) / 2;
62878b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrttvar);
62997d8d152SAndre Oppermann 	}
63097d8d152SAndre Oppermann 	if (hcml->rmx_ssthresh != 0) {
63197d8d152SAndre Oppermann 		if (hc_entry->rmx_ssthresh == 0)
63297d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
63397d8d152SAndre Oppermann 		else
63497d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh =
63597d8d152SAndre Oppermann 			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
63678b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedssthresh);
63797d8d152SAndre Oppermann 	}
63897d8d152SAndre Oppermann 	if (hcml->rmx_cwnd != 0) {
63997d8d152SAndre Oppermann 		if (hc_entry->rmx_cwnd == 0)
64097d8d152SAndre Oppermann 			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
64197d8d152SAndre Oppermann 		else
6423ac12506SJonathan T. Looney 			hc_entry->rmx_cwnd = ((uint64_t)hc_entry->rmx_cwnd +
6433ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_cwnd) / 2;
64478b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedcwnd); */
64597d8d152SAndre Oppermann 	}
64697d8d152SAndre Oppermann 	if (hcml->rmx_sendpipe != 0) {
64797d8d152SAndre Oppermann 		if (hc_entry->rmx_sendpipe == 0)
64897d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
64997d8d152SAndre Oppermann 		else
65097d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe =
6513ac12506SJonathan T. Looney 			    ((uint64_t)hc_entry->rmx_sendpipe +
6523ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_sendpipe) /2;
65378b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedsendpipe); */
65497d8d152SAndre Oppermann 	}
65597d8d152SAndre Oppermann 	if (hcml->rmx_recvpipe != 0) {
65697d8d152SAndre Oppermann 		if (hc_entry->rmx_recvpipe == 0)
65797d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
65897d8d152SAndre Oppermann 		else
65997d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe =
6603ac12506SJonathan T. Looney 			    ((uint64_t)hc_entry->rmx_recvpipe +
6613ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_recvpipe) /2;
66278b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedrecvpipe); */
66397d8d152SAndre Oppermann 	}
66497d8d152SAndre Oppermann 
66597d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
66697d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
66797d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
66897d8d152SAndre Oppermann }
66997d8d152SAndre Oppermann 
67097d8d152SAndre Oppermann /*
67197d8d152SAndre Oppermann  * Sysctl function: prints the list and values of all hostcache entries in
67297d8d152SAndre Oppermann  * unsorted order.
67397d8d152SAndre Oppermann  */
67497d8d152SAndre Oppermann static int
67597d8d152SAndre Oppermann sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
67697d8d152SAndre Oppermann {
677751ccc42SIan Lepore 	const int linesize = 128;
678002d4558SJohn Baldwin 	struct sbuf sb;
679cb0dd7e1SRichard Scheffenegger 	int i, error, len;
68097d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
6818144690aSEric van Gyzen 	char ip4buf[INET_ADDRSTRLEN];
6821d54aa3bSBjoern A. Zeeb #ifdef INET6
6831d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
6841d54aa3bSBjoern A. Zeeb #endif
68597d8d152SAndre Oppermann 
686b8a2fb91SHiren Panchasara 	if (jailed_without_vnet(curthread->td_ucred) != 0)
687b8a2fb91SHiren Panchasara 		return (EPERM);
688b8a2fb91SHiren Panchasara 
689cb0dd7e1SRichard Scheffenegger 	/* Optimize Buffer length query by sbin/sysctl */
690cb0dd7e1SRichard Scheffenegger 	if (req->oldptr == NULL) {
691529a2a0fSRichard Scheffenegger 		len = (atomic_load_int(&V_tcp_hostcache.cache_count) + 1) *
692529a2a0fSRichard Scheffenegger 			linesize;
693cb0dd7e1SRichard Scheffenegger 		return (SYSCTL_OUT(req, NULL, len));
694cb0dd7e1SRichard Scheffenegger 	}
695cb0dd7e1SRichard Scheffenegger 
696cb0dd7e1SRichard Scheffenegger 	error = sysctl_wire_old_buffer(req, 0);
697cb0dd7e1SRichard Scheffenegger 	if (error != 0) {
698cb0dd7e1SRichard Scheffenegger 		return(error);
699cb0dd7e1SRichard Scheffenegger 	}
700cb0dd7e1SRichard Scheffenegger 
70186988046SRichard Scheffenegger 	/* Use a buffer sized for one full bucket */
702529a2a0fSRichard Scheffenegger 	sbuf_new_for_sysctl(&sb, NULL, V_tcp_hostcache.bucket_limit *
703529a2a0fSRichard Scheffenegger 		linesize, req);
70497d8d152SAndre Oppermann 
705002d4558SJohn Baldwin 	sbuf_printf(&sb,
7064d163382SHiren Panchasara 		"\nIP address        MTU  SSTRESH      RTT   RTTVAR "
707489bde57SGleb Smirnoff 		"    CWND SENDPIPE RECVPIPE "
708489bde57SGleb Smirnoff #ifdef	TCP_HC_COUNTERS
709489bde57SGleb Smirnoff 		"HITS  UPD  "
710489bde57SGleb Smirnoff #endif
711489bde57SGleb Smirnoff 		"EXP\n");
71286988046SRichard Scheffenegger 	sbuf_drain(&sb);
71397d8d152SAndre Oppermann 
71497d8d152SAndre Oppermann #define msec(u) (((u) + 500) / 1000)
715603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
716603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
717603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
71897d8d152SAndre Oppermann 			      rmx_q) {
719002d4558SJohn Baldwin 			sbuf_printf(&sb,
720489bde57SGleb Smirnoff 			    "%-15s %5u %8u %6lums %6lums %8u %8u %8u "
721489bde57SGleb Smirnoff #ifdef	TCP_HC_COUNTERS
722489bde57SGleb Smirnoff 			    "%4lu %4lu "
723489bde57SGleb Smirnoff #endif
724489bde57SGleb Smirnoff 			    "%4i\n",
7258144690aSEric van Gyzen 			    hc_entry->ip4.s_addr ?
7268144690aSEric van Gyzen 			        inet_ntoa_r(hc_entry->ip4, ip4buf) :
72797d8d152SAndre Oppermann #ifdef INET6
7281d54aa3bSBjoern A. Zeeb 				ip6_sprintf(ip6buf, &hc_entry->ip6),
72997d8d152SAndre Oppermann #else
73097d8d152SAndre Oppermann 				"IPv6?",
73197d8d152SAndre Oppermann #endif
73297d8d152SAndre Oppermann 			    hc_entry->rmx_mtu,
73397d8d152SAndre Oppermann 			    hc_entry->rmx_ssthresh,
7343ac12506SJonathan T. Looney 			    msec((u_long)hc_entry->rmx_rtt *
73597d8d152SAndre Oppermann 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
7363ac12506SJonathan T. Looney 			    msec((u_long)hc_entry->rmx_rttvar *
7373a288e90SMikolaj Golub 				(RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))),
73897d8d152SAndre Oppermann 			    hc_entry->rmx_cwnd,
73997d8d152SAndre Oppermann 			    hc_entry->rmx_sendpipe,
74097d8d152SAndre Oppermann 			    hc_entry->rmx_recvpipe,
741489bde57SGleb Smirnoff #ifdef	TCP_HC_COUNTERS
74297d8d152SAndre Oppermann 			    hc_entry->rmx_hits,
74397d8d152SAndre Oppermann 			    hc_entry->rmx_updates,
744489bde57SGleb Smirnoff #endif
74597d8d152SAndre Oppermann 			    hc_entry->rmx_expire);
74697d8d152SAndre Oppermann 		}
747603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
74886988046SRichard Scheffenegger 		sbuf_drain(&sb);
7499aef4e7cSRichard Scheffenegger 	}
75097d8d152SAndre Oppermann #undef msec
751751ccc42SIan Lepore 	error = sbuf_finish(&sb);
752002d4558SJohn Baldwin 	sbuf_delete(&sb);
75397d8d152SAndre Oppermann 	return(error);
75497d8d152SAndre Oppermann }
75597d8d152SAndre Oppermann 
75697d8d152SAndre Oppermann /*
75702f26e98SRichard Scheffenegger  * Sysctl function: prints a histogram of the hostcache hashbucket
75802f26e98SRichard Scheffenegger  * utilization.
75902f26e98SRichard Scheffenegger  */
76002f26e98SRichard Scheffenegger static int
76102f26e98SRichard Scheffenegger sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS)
76202f26e98SRichard Scheffenegger {
76302f26e98SRichard Scheffenegger 	const int linesize = 50;
76402f26e98SRichard Scheffenegger 	struct sbuf sb;
76502f26e98SRichard Scheffenegger 	int i, error;
76602f26e98SRichard Scheffenegger 	int *histo;
76702f26e98SRichard Scheffenegger 	u_int hch_length;
76802f26e98SRichard Scheffenegger 
76902f26e98SRichard Scheffenegger 	if (jailed_without_vnet(curthread->td_ucred) != 0)
77002f26e98SRichard Scheffenegger 		return (EPERM);
77102f26e98SRichard Scheffenegger 
77202f26e98SRichard Scheffenegger 	histo = (int *)malloc(sizeof(int) * (V_tcp_hostcache.bucket_limit + 1),
77302f26e98SRichard Scheffenegger 			M_TEMP, M_NOWAIT|M_ZERO);
77402f26e98SRichard Scheffenegger 	if (histo == NULL)
77502f26e98SRichard Scheffenegger 		return(ENOMEM);
77602f26e98SRichard Scheffenegger 
77702f26e98SRichard Scheffenegger 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
77802f26e98SRichard Scheffenegger 		hch_length = V_tcp_hostcache.hashbase[i].hch_length;
77902f26e98SRichard Scheffenegger 		KASSERT(hch_length <= V_tcp_hostcache.bucket_limit,
78002f26e98SRichard Scheffenegger 			("tcp_hostcache: bucket limit exceeded at %u: %u",
78102f26e98SRichard Scheffenegger 			i, hch_length));
78202f26e98SRichard Scheffenegger 		histo[hch_length]++;
78302f26e98SRichard Scheffenegger 	}
78402f26e98SRichard Scheffenegger 
78502f26e98SRichard Scheffenegger 	/* Use a buffer for 16 lines */
78602f26e98SRichard Scheffenegger 	sbuf_new_for_sysctl(&sb, NULL, 16 * linesize, req);
78702f26e98SRichard Scheffenegger 
78802f26e98SRichard Scheffenegger 	sbuf_printf(&sb, "\nLength\tCount\n");
78902f26e98SRichard Scheffenegger 	for (i = 0; i <= V_tcp_hostcache.bucket_limit; i++) {
79002f26e98SRichard Scheffenegger 		sbuf_printf(&sb, "%u\t%u\n", i, histo[i]);
79102f26e98SRichard Scheffenegger 	}
79202f26e98SRichard Scheffenegger 	error = sbuf_finish(&sb);
79302f26e98SRichard Scheffenegger 	sbuf_delete(&sb);
79402f26e98SRichard Scheffenegger 	free(histo, M_TEMP);
79502f26e98SRichard Scheffenegger 	return(error);
79602f26e98SRichard Scheffenegger }
79702f26e98SRichard Scheffenegger 
79802f26e98SRichard Scheffenegger /*
799fffb9f1dSBjoern A. Zeeb  * Caller has to make sure the curvnet is set properly.
80097d8d152SAndre Oppermann  */
80197d8d152SAndre Oppermann static void
802fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(int all)
80397d8d152SAndre Oppermann {
804b62dccc7SAndre Oppermann 	struct hc_metrics *hc_entry, *hc_next;
80597d8d152SAndre Oppermann 	int i;
80697d8d152SAndre Oppermann 
807603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
808603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
809ac957cd2SJulian Elischer 		TAILQ_FOREACH_SAFE(hc_entry,
8105ed3800eSJulian Elischer 		    &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
81102f26e98SRichard Scheffenegger 			KASSERT(V_tcp_hostcache.hashbase[i].hch_length > 0 &&
81202f26e98SRichard Scheffenegger 				V_tcp_hostcache.hashbase[i].hch_length <=
81302f26e98SRichard Scheffenegger 				V_tcp_hostcache.bucket_limit,
8149aef4e7cSRichard Scheffenegger 				("tcp_hostcache: bucket length out of range at %u: %u",
81502f26e98SRichard Scheffenegger 				i, V_tcp_hostcache.hashbase[i].hch_length));
81697d8d152SAndre Oppermann 			if (all || hc_entry->rmx_expire <= 0) {
817603724d3SBjoern A. Zeeb 				TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
81897d8d152SAndre Oppermann 					      hc_entry, rmx_q);
819603724d3SBjoern A. Zeeb 				uma_zfree(V_tcp_hostcache.zone, hc_entry);
820603724d3SBjoern A. Zeeb 				V_tcp_hostcache.hashbase[i].hch_length--;
821529a2a0fSRichard Scheffenegger 				atomic_subtract_int(&V_tcp_hostcache.cache_count, 1);
82297d8d152SAndre Oppermann 			} else
823603724d3SBjoern A. Zeeb 				hc_entry->rmx_expire -= V_tcp_hostcache.prune;
82497d8d152SAndre Oppermann 		}
825603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
82697d8d152SAndre Oppermann 	}
827fffb9f1dSBjoern A. Zeeb }
828fffb9f1dSBjoern A. Zeeb 
829fffb9f1dSBjoern A. Zeeb /*
830fffb9f1dSBjoern A. Zeeb  * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
831fffb9f1dSBjoern A. Zeeb  * periodically from the callout.
832fffb9f1dSBjoern A. Zeeb  */
833fffb9f1dSBjoern A. Zeeb static void
834fffb9f1dSBjoern A. Zeeb tcp_hc_purge(void *arg)
835fffb9f1dSBjoern A. Zeeb {
836fffb9f1dSBjoern A. Zeeb 	CURVNET_SET((struct vnet *) arg);
837fffb9f1dSBjoern A. Zeeb 	int all = 0;
838fffb9f1dSBjoern A. Zeeb 
839fffb9f1dSBjoern A. Zeeb 	if (V_tcp_hostcache.purgeall) {
840*b878ec02SRichard Scheffenegger 		if (V_tcp_hostcache.purgeall == 2)
841*b878ec02SRichard Scheffenegger 			V_tcp_hostcache.hashsalt = arc4random();
842fffb9f1dSBjoern A. Zeeb 		all = 1;
843fffb9f1dSBjoern A. Zeeb 		V_tcp_hostcache.purgeall = 0;
844fffb9f1dSBjoern A. Zeeb 	}
845fffb9f1dSBjoern A. Zeeb 
846fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(all);
847ac957cd2SJulian Elischer 
848ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
849ac957cd2SJulian Elischer 	    tcp_hc_purge, arg);
85021ca7b57SMarko Zec 	CURVNET_RESTORE();
85197d8d152SAndre Oppermann }
85226456380SHiren Panchasara 
85326456380SHiren Panchasara /*
85426456380SHiren Panchasara  * Expire and purge all entries in hostcache immediately.
85526456380SHiren Panchasara  */
85626456380SHiren Panchasara static int
85726456380SHiren Panchasara sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS)
85826456380SHiren Panchasara {
85926456380SHiren Panchasara 	int error, val;
86026456380SHiren Panchasara 
86126456380SHiren Panchasara 	val = 0;
86226456380SHiren Panchasara 	error = sysctl_handle_int(oidp, &val, 0, req);
86326456380SHiren Panchasara 	if (error || !req->newptr)
86426456380SHiren Panchasara 		return (error);
86526456380SHiren Panchasara 
866*b878ec02SRichard Scheffenegger 	if (val == 2)
867*b878ec02SRichard Scheffenegger 		V_tcp_hostcache.hashsalt = arc4random();
86826456380SHiren Panchasara 	tcp_hc_purge_internal(1);
86926456380SHiren Panchasara 
87026456380SHiren Panchasara 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
87126456380SHiren Panchasara 	    tcp_hc_purge, curvnet);
87226456380SHiren Panchasara 
87326456380SHiren Panchasara 	return (0);
87426456380SHiren Panchasara }
875