xref: /freebsd/sys/netinet/tcp_hostcache.c (revision b4aa9cb2175221d31ce06cf91bd9a6b34ebe868e)
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
5d554522fSGleb Smirnoff  * Copyright (c) 2021 Gleb Smirnoff <glebius@FreeBSD.org>
697d8d152SAndre Oppermann  * All rights reserved.
797d8d152SAndre Oppermann  *
897d8d152SAndre Oppermann  * Redistribution and use in source and binary forms, with or without
997d8d152SAndre Oppermann  * modification, are permitted provided that the following conditions
1097d8d152SAndre Oppermann  * are met:
1197d8d152SAndre Oppermann  * 1. Redistributions of source code must retain the above copyright
1297d8d152SAndre Oppermann  *    notice, this list of conditions and the following disclaimer.
1397d8d152SAndre Oppermann  * 2. Redistributions in binary form must reproduce the above copyright
1497d8d152SAndre Oppermann  *    notice, this list of conditions and the following disclaimer in the
1597d8d152SAndre Oppermann  *    documentation and/or other materials provided with the distribution.
1697d8d152SAndre Oppermann  * 3. The name of the author may not be used to endorse or promote
1797d8d152SAndre Oppermann  *    products derived from this software without specific prior written
1897d8d152SAndre Oppermann  *    permission.
1997d8d152SAndre Oppermann  *
2097d8d152SAndre Oppermann  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2197d8d152SAndre Oppermann  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2297d8d152SAndre Oppermann  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2397d8d152SAndre Oppermann  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2497d8d152SAndre Oppermann  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2597d8d152SAndre Oppermann  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2697d8d152SAndre Oppermann  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2797d8d152SAndre Oppermann  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2897d8d152SAndre Oppermann  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2997d8d152SAndre Oppermann  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3097d8d152SAndre Oppermann  * SUCH DAMAGE.
3197d8d152SAndre Oppermann  */
3297d8d152SAndre Oppermann 
3397d8d152SAndre Oppermann /*
34e487a5e2SRobert Watson  * The tcp_hostcache moves the tcp-specific cached metrics from the routing
35e487a5e2SRobert Watson  * table to a dedicated structure indexed by the remote IP address.  It keeps
36e487a5e2SRobert Watson  * information on the measured TCP parameters of past TCP sessions to allow
37e487a5e2SRobert Watson  * better initial start values to be used with later connections to/from the
384d163382SHiren Panchasara  * same source.  Depending on the network parameters (delay, max MTU,
394d163382SHiren Panchasara  * congestion window) between local and remote sites, this can lead to
40e487a5e2SRobert Watson  * significant speed-ups for new TCP connections after the first one.
4197d8d152SAndre Oppermann  *
42e487a5e2SRobert Watson  * Due to the tcp_hostcache, all TCP-specific metrics information in the
43003c7e36SRui Paulo  * routing table have been removed.  The inpcb no longer keeps a pointer to
44e487a5e2SRobert Watson  * the routing entry, and protocol-initiated route cloning has been removed
45e487a5e2SRobert Watson  * as well.  With these changes, the routing table has gone back to being
46e487a5e2SRobert Watson  * more lightwight and only carries information related to packet forwarding.
4797d8d152SAndre Oppermann  *
48e487a5e2SRobert Watson  * tcp_hostcache is designed for multiple concurrent access in SMP
49d554522fSGleb Smirnoff  * environments and high contention.  It is a straight hash.  Each bucket row
50d554522fSGleb Smirnoff  * is protected by its own lock for modification.  Readers are protected by
51d554522fSGleb Smirnoff  * SMR.  This puts certain restrictions on writers, e.g. a writer shall only
52d554522fSGleb Smirnoff  * insert a fully populated entry into a row.  Writer can't reuse least used
53d554522fSGleb Smirnoff  * entry if a hash is full.  Value updates for an entry shall be atomic.
54d554522fSGleb Smirnoff  *
55d554522fSGleb Smirnoff  * TCP stack(s) communication with tcp_hostcache() is done via KBI functions
56d554522fSGleb Smirnoff  * tcp_hc_*() and the hc_metrics_lite structure.
57d554522fSGleb Smirnoff  *
58d554522fSGleb Smirnoff  * Since tcp_hostcache is only caching information, there are no fatal
59d554522fSGleb Smirnoff  * consequences if we either can't allocate a new entry or have to drop
60d554522fSGleb Smirnoff  * an existing entry, or return somewhat stale information.
6197d8d152SAndre Oppermann  */
6297d8d152SAndre Oppermann 
6397d8d152SAndre Oppermann /*
6497d8d152SAndre Oppermann  * Many thanks to jlemon for basic structure of tcp_syncache which is being
6597d8d152SAndre Oppermann  * followed here.
6697d8d152SAndre Oppermann  */
6797d8d152SAndre Oppermann 
684b421e2dSMike Silbersack #include <sys/cdefs.h>
694b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
704b421e2dSMike Silbersack 
7197d8d152SAndre Oppermann #include "opt_inet6.h"
7297d8d152SAndre Oppermann 
7397d8d152SAndre Oppermann #include <sys/param.h>
7497d8d152SAndre Oppermann #include <sys/systm.h>
75b878ec02SRichard Scheffenegger #include <sys/hash.h>
76b8a2fb91SHiren Panchasara #include <sys/jail.h>
7797d8d152SAndre Oppermann #include <sys/kernel.h>
7897d8d152SAndre Oppermann #include <sys/lock.h>
7997d8d152SAndre Oppermann #include <sys/mutex.h>
8097d8d152SAndre Oppermann #include <sys/malloc.h>
81b8a2fb91SHiren Panchasara #include <sys/proc.h>
82002d4558SJohn Baldwin #include <sys/sbuf.h>
83d554522fSGleb Smirnoff #include <sys/smr.h>
8497d8d152SAndre Oppermann #include <sys/socket.h>
8597d8d152SAndre Oppermann #include <sys/socketvar.h>
8697d8d152SAndre Oppermann #include <sys/sysctl.h>
8797d8d152SAndre Oppermann 
88530c0060SRobert Watson #include <net/vnet.h>
8997d8d152SAndre Oppermann 
9097d8d152SAndre Oppermann #include <netinet/in.h>
9197d8d152SAndre Oppermann #include <netinet/in_pcb.h>
9297d8d152SAndre Oppermann #include <netinet/tcp.h>
9397d8d152SAndre Oppermann #include <netinet/tcp_var.h>
9497d8d152SAndre Oppermann 
9597d8d152SAndre Oppermann #include <vm/uma.h>
9697d8d152SAndre Oppermann 
972cca4c0eSGleb Smirnoff struct hc_head {
98d554522fSGleb Smirnoff 	CK_SLIST_HEAD(hc_qhead, hc_metrics) 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 */
105d554522fSGleb Smirnoff 	CK_SLIST_ENTRY(hc_metrics) rmx_q;
1062cca4c0eSGleb Smirnoff 	struct		in_addr ip4;	/* IP address */
1072cca4c0eSGleb Smirnoff 	struct		in6_addr ip6;	/* IP6 address */
1082cca4c0eSGleb Smirnoff 	uint32_t	ip6_zoneid;	/* IPv6 scope zone id */
1092cca4c0eSGleb Smirnoff 	/* endpoint specific values for tcp */
1102cca4c0eSGleb Smirnoff 	uint32_t	rmx_mtu;	/* MTU for this path */
1112cca4c0eSGleb Smirnoff 	uint32_t	rmx_ssthresh;	/* outbound gateway buffer limit */
1122cca4c0eSGleb Smirnoff 	uint32_t	rmx_rtt;	/* estimated round trip time */
1132cca4c0eSGleb Smirnoff 	uint32_t	rmx_rttvar;	/* estimated rtt variance */
1142cca4c0eSGleb Smirnoff 	uint32_t	rmx_cwnd;	/* congestion window */
1152cca4c0eSGleb Smirnoff 	uint32_t	rmx_sendpipe;	/* outbound delay-bandwidth product */
1162cca4c0eSGleb Smirnoff 	uint32_t	rmx_recvpipe;	/* inbound delay-bandwidth product */
1172cca4c0eSGleb Smirnoff 	/* TCP hostcache internal data */
1182cca4c0eSGleb Smirnoff 	int		rmx_expire;	/* lifetime for object */
119489bde57SGleb Smirnoff #ifdef	TCP_HC_COUNTERS
1202cca4c0eSGleb Smirnoff 	u_long		rmx_hits;	/* number of hits */
1212cca4c0eSGleb Smirnoff 	u_long		rmx_updates;	/* number of updates */
122489bde57SGleb Smirnoff #endif
1232cca4c0eSGleb Smirnoff };
1242cca4c0eSGleb Smirnoff 
1252cca4c0eSGleb Smirnoff struct tcp_hostcache {
1262cca4c0eSGleb Smirnoff 	struct hc_head	*hashbase;
1272cca4c0eSGleb Smirnoff 	uma_zone_t	zone;
128d554522fSGleb Smirnoff 	smr_t		smr;
1292cca4c0eSGleb Smirnoff 	u_int		hashsize;
1302cca4c0eSGleb Smirnoff 	u_int		hashmask;
131b878ec02SRichard 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 
152d554522fSGleb Smirnoff static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
15397d8d152SAndre Oppermann static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
15402f26e98SRichard Scheffenegger static int sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS);
15526456380SHiren Panchasara static int sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS);
156fffb9f1dSBjoern A. Zeeb static void tcp_hc_purge_internal(int);
15797d8d152SAndre Oppermann static void tcp_hc_purge(void *);
15897d8d152SAndre Oppermann 
1597029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache,
1607029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1610eb7cf4dSBrooks Davis     "TCP Host cache");
16297d8d152SAndre Oppermann 
1638a56c645SHiren Panchasara VNET_DEFINE(int, tcp_use_hostcache) = 1;
1648a56c645SHiren Panchasara #define V_tcp_use_hostcache  VNET(tcp_use_hostcache)
1658a56c645SHiren Panchasara SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
1668a56c645SHiren Panchasara     &VNET_NAME(tcp_use_hostcache), 0,
1678a56c645SHiren Panchasara     "Enable the TCP hostcache");
1688a56c645SHiren Panchasara 
1696df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN,
170eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.cache_limit), 0,
1718b615593SMarko Zec     "Overall entry limit for hostcache");
17297d8d152SAndre Oppermann 
1736df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN,
174eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.hashsize), 0,
1758b615593SMarko Zec     "Size of TCP hostcache hashtable");
17697d8d152SAndre Oppermann 
1776df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
1786df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0,
1798b615593SMarko Zec     "Per-bucket hash limit for hostcache");
18097d8d152SAndre Oppermann 
181529a2a0fSRichard Scheffenegger SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD,
182529a2a0fSRichard Scheffenegger     &VNET_NAME(tcp_hostcache.cache_count), 0,
1838b615593SMarko Zec     "Current number of entries in hostcache");
18497d8d152SAndre Oppermann 
1856df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW,
186eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.expire), 0,
1878b615593SMarko Zec     "Expire time of TCP hostcache entries");
18897d8d152SAndre Oppermann 
1896df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW,
190eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.prune), 0,
191eddfbb76SRobert Watson     "Time between purge runs");
192dba3c508SYaroslav Tykhiy 
1936df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW,
194eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.purgeall), 0,
195*b4aa9cb2SGordon Bergling     "Expire all entries on next purge run");
19697d8d152SAndre Oppermann 
19797d8d152SAndre Oppermann SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
1987029da5cSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE,
1997029da5cSPawel Biernacki     0, 0, sysctl_tcp_hc_list, "A",
2007029da5cSPawel Biernacki     "List of all hostcache entries");
20197d8d152SAndre Oppermann 
20202f26e98SRichard Scheffenegger SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, histo,
20302f26e98SRichard Scheffenegger     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE,
20402f26e98SRichard Scheffenegger     0, 0, sysctl_tcp_hc_histo, "A",
20502f26e98SRichard Scheffenegger     "Print a histogram of hostcache hashbucket utilization");
20602f26e98SRichard Scheffenegger 
20726456380SHiren Panchasara SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, purgenow,
2087029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2097029da5cSPawel Biernacki     NULL, 0, sysctl_tcp_hc_purgenow, "I",
2107029da5cSPawel Biernacki     "Immediately purge all entries");
21197d8d152SAndre Oppermann 
21297d8d152SAndre Oppermann static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
21397d8d152SAndre Oppermann 
214b878ec02SRichard Scheffenegger /* Use jenkins_hash32(), as in other parts of the tcp stack */
215d554522fSGleb Smirnoff #define	HOSTCACHE_HASH(inc)						\
216d554522fSGleb Smirnoff 	((inc)->inc_flags & INC_ISIPV6) ?				\
217d554522fSGleb Smirnoff 		(jenkins_hash32((inc)->inc6_faddr.s6_addr32, 4,		\
218d554522fSGleb Smirnoff 		V_tcp_hostcache.hashsalt) & V_tcp_hostcache.hashmask)	\
219d554522fSGleb Smirnoff 	:								\
220d554522fSGleb Smirnoff 		(jenkins_hash32(&(inc)->inc_faddr.s_addr, 1,		\
221d554522fSGleb Smirnoff 		V_tcp_hostcache.hashsalt) & V_tcp_hostcache.hashmask)
22297d8d152SAndre Oppermann 
2231a7fe55aSGleb Smirnoff #define THC_LOCK(h)		mtx_lock(&(h)->hch_mtx)
2241a7fe55aSGleb Smirnoff #define THC_UNLOCK(h)		mtx_unlock(&(h)->hch_mtx)
22597d8d152SAndre Oppermann 
22697d8d152SAndre Oppermann void
22797d8d152SAndre Oppermann tcp_hc_init(void)
22897d8d152SAndre Oppermann {
22932fe38f1SAndrey Zonov 	u_int cache_limit;
23097d8d152SAndre Oppermann 	int i;
23197d8d152SAndre Oppermann 
23297d8d152SAndre Oppermann 	/*
233e487a5e2SRobert Watson 	 * Initialize hostcache structures.
23497d8d152SAndre Oppermann 	 */
235529a2a0fSRichard Scheffenegger 	atomic_store_int(&V_tcp_hostcache.cache_count, 0);
236603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
237603724d3SBjoern A. Zeeb 	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
238603724d3SBjoern A. Zeeb 	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
239603724d3SBjoern A. Zeeb 	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
240b878ec02SRichard Scheffenegger 	V_tcp_hostcache.hashsalt = arc4random();
24197d8d152SAndre Oppermann 
24297d8d152SAndre Oppermann 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
243603724d3SBjoern A. Zeeb 	    &V_tcp_hostcache.hashsize);
244603724d3SBjoern A. Zeeb 	if (!powerof2(V_tcp_hostcache.hashsize)) {
24597d8d152SAndre Oppermann 		printf("WARNING: hostcache hash size is not a power of 2.\n");
246603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
24797d8d152SAndre Oppermann 	}
248603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
24997d8d152SAndre Oppermann 
25032fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
25132fe38f1SAndrey Zonov 	    &V_tcp_hostcache.bucket_limit);
25232fe38f1SAndrey Zonov 
25332fe38f1SAndrey Zonov 	cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
25432fe38f1SAndrey Zonov 	V_tcp_hostcache.cache_limit = cache_limit;
25532fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
25632fe38f1SAndrey Zonov 	    &V_tcp_hostcache.cache_limit);
25732fe38f1SAndrey Zonov 	if (V_tcp_hostcache.cache_limit > cache_limit)
25832fe38f1SAndrey Zonov 		V_tcp_hostcache.cache_limit = cache_limit;
25932fe38f1SAndrey Zonov 
26097d8d152SAndre Oppermann 	/*
261e487a5e2SRobert Watson 	 * Allocate the hash table.
26297d8d152SAndre Oppermann 	 */
263603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase = (struct hc_head *)
264603724d3SBjoern A. Zeeb 	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
26597d8d152SAndre Oppermann 		   M_HOSTCACHE, M_WAITOK | M_ZERO);
26697d8d152SAndre Oppermann 
26797d8d152SAndre Oppermann 	/*
268e487a5e2SRobert Watson 	 * Initialize the hash buckets.
26997d8d152SAndre Oppermann 	 */
270603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
271d554522fSGleb Smirnoff 		CK_SLIST_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
272603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[i].hch_length = 0;
273603724d3SBjoern A. Zeeb 		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
27497d8d152SAndre Oppermann 			  NULL, MTX_DEF);
27597d8d152SAndre Oppermann 	}
27697d8d152SAndre Oppermann 
27797d8d152SAndre Oppermann 	/*
27897d8d152SAndre Oppermann 	 * Allocate the hostcache entries.
27997d8d152SAndre Oppermann 	 */
280ac957cd2SJulian Elischer 	V_tcp_hostcache.zone =
281ac957cd2SJulian Elischer 	    uma_zcreate("hostcache", sizeof(struct hc_metrics),
282d554522fSGleb Smirnoff 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_SMR);
283603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
284d554522fSGleb Smirnoff 	V_tcp_hostcache.smr = uma_zone_get_smr(V_tcp_hostcache.zone);
28597d8d152SAndre Oppermann 
28697d8d152SAndre Oppermann 	/*
28797d8d152SAndre Oppermann 	 * Set up periodic cache cleanup.
28897d8d152SAndre Oppermann 	 */
289fd90e2edSJung-uk Kim 	callout_init(&V_tcp_hc_callout, 1);
290ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
29121ca7b57SMarko Zec 	    tcp_hc_purge, curvnet);
29297d8d152SAndre Oppermann }
29397d8d152SAndre Oppermann 
294bc29160dSMarko Zec #ifdef VIMAGE
295bc29160dSMarko Zec void
296bc29160dSMarko Zec tcp_hc_destroy(void)
297bc29160dSMarko Zec {
298fffb9f1dSBjoern A. Zeeb 	int i;
299bc29160dSMarko Zec 
300bc29160dSMarko Zec 	callout_drain(&V_tcp_hc_callout);
301fffb9f1dSBjoern A. Zeeb 
302fffb9f1dSBjoern A. Zeeb 	/* Purge all hc entries. */
303fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(1);
304fffb9f1dSBjoern A. Zeeb 
305fffb9f1dSBjoern A. Zeeb 	/* Free the uma zone and the allocated hash table. */
306fffb9f1dSBjoern A. Zeeb 	uma_zdestroy(V_tcp_hostcache.zone);
307fffb9f1dSBjoern A. Zeeb 
308fffb9f1dSBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++)
309fffb9f1dSBjoern A. Zeeb 		mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx);
310fffb9f1dSBjoern A. Zeeb 	free(V_tcp_hostcache.hashbase, M_HOSTCACHE);
311bc29160dSMarko Zec }
312bc29160dSMarko Zec #endif
313bc29160dSMarko Zec 
31497d8d152SAndre Oppermann /*
315d554522fSGleb Smirnoff  * Internal function: compare cache entry to a connection.
31697d8d152SAndre Oppermann  */
317d554522fSGleb Smirnoff static bool
318d554522fSGleb Smirnoff tcp_hc_cmp(struct hc_metrics *hc_entry, struct in_conninfo *inc)
31997d8d152SAndre Oppermann {
32097d8d152SAndre Oppermann 
321dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
322028bdf28SAndrey V. Elsukov 		/* XXX: check ip6_zoneid */
32397d8d152SAndre Oppermann 		if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
32497d8d152SAndre Oppermann 		    sizeof(inc->inc6_faddr)) == 0)
325d554522fSGleb Smirnoff 			return (true);
32697d8d152SAndre Oppermann 	} else {
32797d8d152SAndre Oppermann 		if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
32897d8d152SAndre Oppermann 		    sizeof(inc->inc_faddr)) == 0)
329d554522fSGleb Smirnoff 			return (true);
33097d8d152SAndre Oppermann 	}
331d554522fSGleb Smirnoff 
332d554522fSGleb Smirnoff 	return (false);
33397d8d152SAndre Oppermann }
33497d8d152SAndre Oppermann 
33597d8d152SAndre Oppermann /*
336d554522fSGleb Smirnoff  * Internal function: look up an entry in the hostcache for read.
337d554522fSGleb Smirnoff  * On success returns in SMR section.
33897d8d152SAndre Oppermann  */
33997d8d152SAndre Oppermann static struct hc_metrics *
340d554522fSGleb Smirnoff tcp_hc_lookup(struct in_conninfo *inc)
34197d8d152SAndre Oppermann {
34297d8d152SAndre Oppermann 	struct hc_head *hc_head;
34397d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
34497d8d152SAndre Oppermann 
3454f49e338SGleb Smirnoff 	KASSERT(inc != NULL, ("%s: NULL in_conninfo", __func__));
34697d8d152SAndre Oppermann 
347d554522fSGleb Smirnoff 	hc_head = &V_tcp_hostcache.hashbase[HOSTCACHE_HASH(inc)];
34897d8d152SAndre Oppermann 
34997d8d152SAndre Oppermann 	/*
350d554522fSGleb Smirnoff 	 * Iterate through entries in bucket row looking for a match.
35197d8d152SAndre Oppermann 	 */
352d554522fSGleb Smirnoff 	smr_enter(V_tcp_hostcache.smr);
353d554522fSGleb Smirnoff 	CK_SLIST_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q)
354d554522fSGleb Smirnoff 		if (tcp_hc_cmp(hc_entry, inc))
355d554522fSGleb Smirnoff 			break;
35697d8d152SAndre Oppermann 
357d554522fSGleb Smirnoff 	if (hc_entry != NULL) {
358d554522fSGleb Smirnoff 		if (atomic_load_int(&hc_entry->rmx_expire) !=
359d554522fSGleb Smirnoff 		    V_tcp_hostcache.expire)
360d554522fSGleb Smirnoff 			atomic_store_int(&hc_entry->rmx_expire,
361d554522fSGleb Smirnoff 			    V_tcp_hostcache.expire);
362d554522fSGleb Smirnoff #ifdef	TCP_HC_COUNTERS
363d554522fSGleb Smirnoff 		hc_entry->rmx_hits++;
364cd6c4060SAndre Oppermann #endif
365028bdf28SAndrey V. Elsukov 	} else
366d554522fSGleb Smirnoff 		smr_exit(V_tcp_hostcache.smr);
36797d8d152SAndre Oppermann 
3684f49e338SGleb Smirnoff 	return (hc_entry);
36997d8d152SAndre Oppermann }
37097d8d152SAndre Oppermann 
37197d8d152SAndre Oppermann /*
37297d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and fill out the
373e487a5e2SRobert Watson  * supplied TCP metrics structure.  Fills in NULL when no entry was found or
374e487a5e2SRobert Watson  * a value is not set.
37597d8d152SAndre Oppermann  */
37697d8d152SAndre Oppermann void
37797d8d152SAndre Oppermann tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
37897d8d152SAndre Oppermann {
37997d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
38097d8d152SAndre Oppermann 
3816b8fba3cSMichael Tuexen 	if (!V_tcp_use_hostcache) {
3826b8fba3cSMichael Tuexen 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
3838a56c645SHiren Panchasara 		return;
3846b8fba3cSMichael Tuexen 	}
3858a56c645SHiren Panchasara 
38697d8d152SAndre Oppermann 	/*
387e487a5e2SRobert Watson 	 * Find the right bucket.
38897d8d152SAndre Oppermann 	 */
389d554522fSGleb Smirnoff 	hc_entry = tcp_hc_lookup(inc);
39097d8d152SAndre Oppermann 
39197d8d152SAndre Oppermann 	/*
392e487a5e2SRobert Watson 	 * If we don't have an existing object.
39397d8d152SAndre Oppermann 	 */
39497d8d152SAndre Oppermann 	if (hc_entry == NULL) {
39597d8d152SAndre Oppermann 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
39697d8d152SAndre Oppermann 		return;
39797d8d152SAndre Oppermann 	}
39897d8d152SAndre Oppermann 
399d554522fSGleb Smirnoff 	hc_metrics_lite->rmx_mtu = atomic_load_32(&hc_entry->rmx_mtu);
400d554522fSGleb Smirnoff 	hc_metrics_lite->rmx_ssthresh = atomic_load_32(&hc_entry->rmx_ssthresh);
401d554522fSGleb Smirnoff 	hc_metrics_lite->rmx_rtt = atomic_load_32(&hc_entry->rmx_rtt);
402d554522fSGleb Smirnoff 	hc_metrics_lite->rmx_rttvar = atomic_load_32(&hc_entry->rmx_rttvar);
403d554522fSGleb Smirnoff 	hc_metrics_lite->rmx_cwnd = atomic_load_32(&hc_entry->rmx_cwnd);
404d554522fSGleb Smirnoff 	hc_metrics_lite->rmx_sendpipe = atomic_load_32(&hc_entry->rmx_sendpipe);
405d554522fSGleb Smirnoff 	hc_metrics_lite->rmx_recvpipe = atomic_load_32(&hc_entry->rmx_recvpipe);
40697d8d152SAndre Oppermann 
407d554522fSGleb Smirnoff 	smr_exit(V_tcp_hostcache.smr);
40897d8d152SAndre Oppermann }
40997d8d152SAndre Oppermann 
41097d8d152SAndre Oppermann /*
41197d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and return the
4128a56c645SHiren Panchasara  * discovered path MTU.  Returns 0 if no entry is found or value is not
4136fbed4afSRobert Watson  * set.
41497d8d152SAndre Oppermann  */
4153ac12506SJonathan T. Looney uint32_t
41697d8d152SAndre Oppermann tcp_hc_getmtu(struct in_conninfo *inc)
41797d8d152SAndre Oppermann {
41897d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
4193ac12506SJonathan T. Looney 	uint32_t mtu;
42097d8d152SAndre Oppermann 
4218a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
4224f49e338SGleb Smirnoff 		return (0);
4238a56c645SHiren Panchasara 
424d554522fSGleb Smirnoff 	hc_entry = tcp_hc_lookup(inc);
42597d8d152SAndre Oppermann 	if (hc_entry == NULL) {
4264f49e338SGleb Smirnoff 		return (0);
42797d8d152SAndre Oppermann 	}
42897d8d152SAndre Oppermann 
429d554522fSGleb Smirnoff 	mtu = atomic_load_32(&hc_entry->rmx_mtu);
430d554522fSGleb Smirnoff 	smr_exit(V_tcp_hostcache.smr);
431d554522fSGleb Smirnoff 
4324f49e338SGleb Smirnoff 	return (mtu);
43397d8d152SAndre Oppermann }
43497d8d152SAndre Oppermann 
43597d8d152SAndre Oppermann /*
436e487a5e2SRobert Watson  * External function: update the MTU value of an entry in the hostcache.
43797d8d152SAndre Oppermann  * Creates a new entry if none was found.
43897d8d152SAndre Oppermann  */
43997d8d152SAndre Oppermann void
4403ac12506SJonathan T. Looney tcp_hc_updatemtu(struct in_conninfo *inc, uint32_t mtu)
44197d8d152SAndre Oppermann {
4420c25bf7eSGleb Smirnoff 	struct hc_metrics_lite hcml = { .rmx_mtu = mtu };
44397d8d152SAndre Oppermann 
4440c25bf7eSGleb Smirnoff 	return (tcp_hc_update(inc, &hcml));
44597d8d152SAndre Oppermann }
44697d8d152SAndre Oppermann 
44797d8d152SAndre Oppermann /*
448e487a5e2SRobert Watson  * External function: update the TCP metrics of an entry in the hostcache.
44997d8d152SAndre Oppermann  * Creates a new entry if none was found.
45097d8d152SAndre Oppermann  */
45197d8d152SAndre Oppermann void
45297d8d152SAndre Oppermann tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
45397d8d152SAndre Oppermann {
454d554522fSGleb Smirnoff 	struct hc_head *hc_head;
455d554522fSGleb Smirnoff 	struct hc_metrics *hc_entry, *hc_prev;
456d554522fSGleb Smirnoff 	uint32_t v;
457d554522fSGleb Smirnoff 	bool new;
45897d8d152SAndre Oppermann 
4598a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
4608a56c645SHiren Panchasara 		return;
4618a56c645SHiren Panchasara 
462d554522fSGleb Smirnoff 	hc_head = &V_tcp_hostcache.hashbase[HOSTCACHE_HASH(inc)];
463d554522fSGleb Smirnoff 	hc_prev = NULL;
464d554522fSGleb Smirnoff 
465d554522fSGleb Smirnoff 	THC_LOCK(hc_head);
466d554522fSGleb Smirnoff 	CK_SLIST_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
467d554522fSGleb Smirnoff 		if (tcp_hc_cmp(hc_entry, inc))
468d554522fSGleb Smirnoff 			break;
469d554522fSGleb Smirnoff 		if (CK_SLIST_NEXT(hc_entry, rmx_q) != NULL)
470d554522fSGleb Smirnoff 			hc_prev = hc_entry;
471d554522fSGleb Smirnoff 	}
472d554522fSGleb Smirnoff 
473d554522fSGleb Smirnoff 	if (hc_entry != NULL) {
474d554522fSGleb Smirnoff 		if (atomic_load_int(&hc_entry->rmx_expire) !=
475d554522fSGleb Smirnoff 		    V_tcp_hostcache.expire)
476d554522fSGleb Smirnoff 			atomic_store_int(&hc_entry->rmx_expire,
477d554522fSGleb Smirnoff 			    V_tcp_hostcache.expire);
478d554522fSGleb Smirnoff #ifdef	TCP_HC_COUNTERS
479d554522fSGleb Smirnoff 		hc_entry->rmx_updates++;
480d554522fSGleb Smirnoff #endif
481d554522fSGleb Smirnoff 		new = false;
482d554522fSGleb Smirnoff 	} else {
483d554522fSGleb Smirnoff 		/*
484d554522fSGleb Smirnoff 		 * Try to allocate a new entry.  If the bucket limit is
485d554522fSGleb Smirnoff 		 * reached, delete the least-used element, located at the end
486d554522fSGleb Smirnoff 		 * of the CK_SLIST.  During lookup we saved the pointer to
487d554522fSGleb Smirnoff 		 * the second to last element, in case if list has at least 2
488d554522fSGleb Smirnoff 		 * elements.  This will allow to delete last element without
489d554522fSGleb Smirnoff 		 * extra traversal.
490d554522fSGleb Smirnoff 		 *
491d554522fSGleb Smirnoff 		 * Give up if the row is empty.
492d554522fSGleb Smirnoff 		 */
493d554522fSGleb Smirnoff 		if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
494d554522fSGleb Smirnoff 		    atomic_load_int(&V_tcp_hostcache.cache_count) >=
495d554522fSGleb Smirnoff 		    V_tcp_hostcache.cache_limit) {
496d554522fSGleb Smirnoff 			if (hc_prev != NULL) {
497d554522fSGleb Smirnoff 				hc_entry = CK_SLIST_NEXT(hc_prev, rmx_q);
498d554522fSGleb Smirnoff 				KASSERT(CK_SLIST_NEXT(hc_entry, rmx_q) == NULL,
499d554522fSGleb Smirnoff 				    ("%s: %p is not one to last",
500d554522fSGleb Smirnoff 				    __func__, hc_prev));
501d554522fSGleb Smirnoff 				CK_SLIST_REMOVE_AFTER(hc_prev, rmx_q);
502d554522fSGleb Smirnoff 			} else if ((hc_entry =
503d554522fSGleb Smirnoff 			    CK_SLIST_FIRST(&hc_head->hch_bucket)) != NULL) {
504d554522fSGleb Smirnoff 				KASSERT(CK_SLIST_NEXT(hc_entry, rmx_q) == NULL,
505d554522fSGleb Smirnoff 				    ("%s: %p is not the only element",
506d554522fSGleb Smirnoff 				    __func__, hc_entry));
507d554522fSGleb Smirnoff 				CK_SLIST_REMOVE_HEAD(&hc_head->hch_bucket,
508d554522fSGleb Smirnoff 				    rmx_q);
509d554522fSGleb Smirnoff 			} else {
510d554522fSGleb Smirnoff 				THC_UNLOCK(hc_head);
511d554522fSGleb Smirnoff 				return;
512d554522fSGleb Smirnoff 			}
513d554522fSGleb Smirnoff 			KASSERT(hc_head->hch_length > 0 &&
514d554522fSGleb Smirnoff 			    hc_head->hch_length <= V_tcp_hostcache.bucket_limit,
515d554522fSGleb Smirnoff 			    ("tcp_hostcache: bucket length violated at %p",
516d554522fSGleb Smirnoff 			    hc_head));
517d554522fSGleb Smirnoff 			hc_head->hch_length--;
518d554522fSGleb Smirnoff 			atomic_subtract_int(&V_tcp_hostcache.cache_count, 1);
519d554522fSGleb Smirnoff 			TCPSTAT_INC(tcps_hc_bucketoverflow);
520d554522fSGleb Smirnoff 			uma_zfree_smr(V_tcp_hostcache.zone, hc_entry);
521d554522fSGleb Smirnoff 		}
522d554522fSGleb Smirnoff 
523d554522fSGleb Smirnoff 		/*
524d554522fSGleb Smirnoff 		 * Allocate a new entry, or balk if not possible.
525d554522fSGleb Smirnoff 		 */
526d554522fSGleb Smirnoff 		hc_entry = uma_zalloc_smr(V_tcp_hostcache.zone, M_NOWAIT);
52797d8d152SAndre Oppermann 		if (hc_entry == NULL) {
528d554522fSGleb Smirnoff 			THC_UNLOCK(hc_head);
52997d8d152SAndre Oppermann 			return;
53097d8d152SAndre Oppermann 		}
53197d8d152SAndre Oppermann 
532d554522fSGleb Smirnoff 		/*
533d554522fSGleb Smirnoff 		 * Initialize basic information of hostcache entry.
534d554522fSGleb Smirnoff 		 */
535d554522fSGleb Smirnoff 		bzero(hc_entry, sizeof(*hc_entry));
536d554522fSGleb Smirnoff 		if (inc->inc_flags & INC_ISIPV6) {
537d554522fSGleb Smirnoff 			hc_entry->ip6 = inc->inc6_faddr;
538d554522fSGleb Smirnoff 			hc_entry->ip6_zoneid = inc->inc6_zoneid;
539d554522fSGleb Smirnoff 		} else
540d554522fSGleb Smirnoff 			hc_entry->ip4 = inc->inc_faddr;
541d554522fSGleb Smirnoff 		hc_entry->rmx_expire = V_tcp_hostcache.expire;
542d554522fSGleb Smirnoff 		new = true;
543d554522fSGleb Smirnoff 	}
544d554522fSGleb Smirnoff 
545d554522fSGleb Smirnoff 	/*
546d554522fSGleb Smirnoff 	 * Fill in data.  Use atomics, since an existing entry is
547d554522fSGleb Smirnoff 	 * accessible by readers in SMR section.
548d554522fSGleb Smirnoff 	 */
5490c25bf7eSGleb Smirnoff 	if (hcml->rmx_mtu != 0) {
550d554522fSGleb Smirnoff 		atomic_store_32(&hc_entry->rmx_mtu, hcml->rmx_mtu);
5510c25bf7eSGleb Smirnoff 	}
55297d8d152SAndre Oppermann 	if (hcml->rmx_rtt != 0) {
55397d8d152SAndre Oppermann 		if (hc_entry->rmx_rtt == 0)
554d554522fSGleb Smirnoff 			v = hcml->rmx_rtt;
55597d8d152SAndre Oppermann 		else
556d554522fSGleb Smirnoff 			v = ((uint64_t)hc_entry->rmx_rtt +
5573ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_rtt) / 2;
558d554522fSGleb Smirnoff 		atomic_store_32(&hc_entry->rmx_rtt, v);
55978b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrtt);
56097d8d152SAndre Oppermann 	}
56197d8d152SAndre Oppermann 	if (hcml->rmx_rttvar != 0) {
56297d8d152SAndre Oppermann 	        if (hc_entry->rmx_rttvar == 0)
563d554522fSGleb Smirnoff 			v = hcml->rmx_rttvar;
56497d8d152SAndre Oppermann 		else
565d554522fSGleb Smirnoff 			v = ((uint64_t)hc_entry->rmx_rttvar +
5663ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_rttvar) / 2;
567d554522fSGleb Smirnoff 		atomic_store_32(&hc_entry->rmx_rttvar, v);
56878b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrttvar);
56997d8d152SAndre Oppermann 	}
57097d8d152SAndre Oppermann 	if (hcml->rmx_ssthresh != 0) {
57197d8d152SAndre Oppermann 		if (hc_entry->rmx_ssthresh == 0)
572d554522fSGleb Smirnoff 			v = hcml->rmx_ssthresh;
57397d8d152SAndre Oppermann 		else
574d554522fSGleb Smirnoff 			v = (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
575d554522fSGleb Smirnoff 		atomic_store_32(&hc_entry->rmx_ssthresh, v);
57678b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedssthresh);
57797d8d152SAndre Oppermann 	}
57897d8d152SAndre Oppermann 	if (hcml->rmx_cwnd != 0) {
57997d8d152SAndre Oppermann 		if (hc_entry->rmx_cwnd == 0)
580d554522fSGleb Smirnoff 			v = hcml->rmx_cwnd;
58197d8d152SAndre Oppermann 		else
582d554522fSGleb Smirnoff 			v = ((uint64_t)hc_entry->rmx_cwnd +
5833ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_cwnd) / 2;
584d554522fSGleb Smirnoff 		atomic_store_32(&hc_entry->rmx_cwnd, v);
58578b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedcwnd); */
58697d8d152SAndre Oppermann 	}
58797d8d152SAndre Oppermann 	if (hcml->rmx_sendpipe != 0) {
58897d8d152SAndre Oppermann 		if (hc_entry->rmx_sendpipe == 0)
589d554522fSGleb Smirnoff 			v = hcml->rmx_sendpipe;
59097d8d152SAndre Oppermann 		else
591d554522fSGleb Smirnoff 			v = ((uint64_t)hc_entry->rmx_sendpipe +
5923ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_sendpipe) /2;
593d554522fSGleb Smirnoff 		atomic_store_32(&hc_entry->rmx_sendpipe, v);
59478b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedsendpipe); */
59597d8d152SAndre Oppermann 	}
59697d8d152SAndre Oppermann 	if (hcml->rmx_recvpipe != 0) {
59797d8d152SAndre Oppermann 		if (hc_entry->rmx_recvpipe == 0)
598d554522fSGleb Smirnoff 			v = hcml->rmx_recvpipe;
59997d8d152SAndre Oppermann 		else
600d554522fSGleb Smirnoff 			v = ((uint64_t)hc_entry->rmx_recvpipe +
6013ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_recvpipe) /2;
602d554522fSGleb Smirnoff 		atomic_store_32(&hc_entry->rmx_recvpipe, v);
60378b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedrecvpipe); */
60497d8d152SAndre Oppermann 	}
60597d8d152SAndre Oppermann 
606d554522fSGleb Smirnoff 	/*
607d554522fSGleb Smirnoff 	 * Put it upfront.
608d554522fSGleb Smirnoff 	 */
609d554522fSGleb Smirnoff 	if (new) {
610d554522fSGleb Smirnoff 		CK_SLIST_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
611d554522fSGleb Smirnoff 		hc_head->hch_length++;
612d554522fSGleb Smirnoff 		KASSERT(hc_head->hch_length <= V_tcp_hostcache.bucket_limit,
613d554522fSGleb Smirnoff 		    ("tcp_hostcache: bucket length too high at %p", hc_head));
614d554522fSGleb Smirnoff 		atomic_add_int(&V_tcp_hostcache.cache_count, 1);
615d554522fSGleb Smirnoff 		TCPSTAT_INC(tcps_hc_added);
616d554522fSGleb Smirnoff 	} else if (hc_entry != CK_SLIST_FIRST(&hc_head->hch_bucket)) {
617d554522fSGleb Smirnoff 		KASSERT(CK_SLIST_NEXT(hc_prev, rmx_q) == hc_entry,
618d554522fSGleb Smirnoff 		    ("%s: %p next is not %p", __func__, hc_prev, hc_entry));
619d554522fSGleb Smirnoff 		CK_SLIST_REMOVE_AFTER(hc_prev, rmx_q);
620d554522fSGleb Smirnoff 		CK_SLIST_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
621d554522fSGleb Smirnoff 	}
622d554522fSGleb Smirnoff 	THC_UNLOCK(hc_head);
62397d8d152SAndre Oppermann }
62497d8d152SAndre Oppermann 
62597d8d152SAndre Oppermann /*
62697d8d152SAndre Oppermann  * Sysctl function: prints the list and values of all hostcache entries in
62797d8d152SAndre Oppermann  * unsorted order.
62897d8d152SAndre Oppermann  */
62997d8d152SAndre Oppermann static int
63097d8d152SAndre Oppermann sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
63197d8d152SAndre Oppermann {
632751ccc42SIan Lepore 	const int linesize = 128;
633002d4558SJohn Baldwin 	struct sbuf sb;
634cb0dd7e1SRichard Scheffenegger 	int i, error, len;
63597d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
6368144690aSEric van Gyzen 	char ip4buf[INET_ADDRSTRLEN];
6371d54aa3bSBjoern A. Zeeb #ifdef INET6
6381d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
6391d54aa3bSBjoern A. Zeeb #endif
64097d8d152SAndre Oppermann 
641b8a2fb91SHiren Panchasara 	if (jailed_without_vnet(curthread->td_ucred) != 0)
642b8a2fb91SHiren Panchasara 		return (EPERM);
643b8a2fb91SHiren Panchasara 
644cb0dd7e1SRichard Scheffenegger 	/* Optimize Buffer length query by sbin/sysctl */
645cb0dd7e1SRichard Scheffenegger 	if (req->oldptr == NULL) {
646529a2a0fSRichard Scheffenegger 		len = (atomic_load_int(&V_tcp_hostcache.cache_count) + 1) *
647529a2a0fSRichard Scheffenegger 			linesize;
648cb0dd7e1SRichard Scheffenegger 		return (SYSCTL_OUT(req, NULL, len));
649cb0dd7e1SRichard Scheffenegger 	}
650cb0dd7e1SRichard Scheffenegger 
651cb0dd7e1SRichard Scheffenegger 	error = sysctl_wire_old_buffer(req, 0);
652cb0dd7e1SRichard Scheffenegger 	if (error != 0) {
653cb0dd7e1SRichard Scheffenegger 		return(error);
654cb0dd7e1SRichard Scheffenegger 	}
655cb0dd7e1SRichard Scheffenegger 
65686988046SRichard Scheffenegger 	/* Use a buffer sized for one full bucket */
657529a2a0fSRichard Scheffenegger 	sbuf_new_for_sysctl(&sb, NULL, V_tcp_hostcache.bucket_limit *
658529a2a0fSRichard Scheffenegger 		linesize, req);
65997d8d152SAndre Oppermann 
660002d4558SJohn Baldwin 	sbuf_printf(&sb,
6614d163382SHiren Panchasara 		"\nIP address        MTU  SSTRESH      RTT   RTTVAR "
662489bde57SGleb Smirnoff 		"    CWND SENDPIPE RECVPIPE "
663489bde57SGleb Smirnoff #ifdef	TCP_HC_COUNTERS
664489bde57SGleb Smirnoff 		"HITS  UPD  "
665489bde57SGleb Smirnoff #endif
666489bde57SGleb Smirnoff 		"EXP\n");
66786988046SRichard Scheffenegger 	sbuf_drain(&sb);
66897d8d152SAndre Oppermann 
66997d8d152SAndre Oppermann #define msec(u) (((u) + 500) / 1000)
670603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
6711a7fe55aSGleb Smirnoff 		THC_LOCK(&V_tcp_hostcache.hashbase[i]);
672d554522fSGleb Smirnoff 		CK_SLIST_FOREACH(hc_entry,
673d554522fSGleb Smirnoff 		    &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q) {
674002d4558SJohn Baldwin 			sbuf_printf(&sb,
675489bde57SGleb Smirnoff 			    "%-15s %5u %8u %6lums %6lums %8u %8u %8u "
676489bde57SGleb Smirnoff #ifdef	TCP_HC_COUNTERS
677489bde57SGleb Smirnoff 			    "%4lu %4lu "
678489bde57SGleb Smirnoff #endif
679489bde57SGleb Smirnoff 			    "%4i\n",
6808144690aSEric van Gyzen 			    hc_entry->ip4.s_addr ?
6818144690aSEric van Gyzen 			        inet_ntoa_r(hc_entry->ip4, ip4buf) :
68297d8d152SAndre Oppermann #ifdef INET6
6831d54aa3bSBjoern A. Zeeb 				ip6_sprintf(ip6buf, &hc_entry->ip6),
68497d8d152SAndre Oppermann #else
68597d8d152SAndre Oppermann 				"IPv6?",
68697d8d152SAndre Oppermann #endif
68797d8d152SAndre Oppermann 			    hc_entry->rmx_mtu,
68897d8d152SAndre Oppermann 			    hc_entry->rmx_ssthresh,
6893ac12506SJonathan T. Looney 			    msec((u_long)hc_entry->rmx_rtt *
69097d8d152SAndre Oppermann 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
6913ac12506SJonathan T. Looney 			    msec((u_long)hc_entry->rmx_rttvar *
6923a288e90SMikolaj Golub 				(RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))),
69397d8d152SAndre Oppermann 			    hc_entry->rmx_cwnd,
69497d8d152SAndre Oppermann 			    hc_entry->rmx_sendpipe,
69597d8d152SAndre Oppermann 			    hc_entry->rmx_recvpipe,
696489bde57SGleb Smirnoff #ifdef	TCP_HC_COUNTERS
69797d8d152SAndre Oppermann 			    hc_entry->rmx_hits,
69897d8d152SAndre Oppermann 			    hc_entry->rmx_updates,
699489bde57SGleb Smirnoff #endif
70097d8d152SAndre Oppermann 			    hc_entry->rmx_expire);
70197d8d152SAndre Oppermann 		}
7021a7fe55aSGleb Smirnoff 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i]);
70386988046SRichard Scheffenegger 		sbuf_drain(&sb);
7049aef4e7cSRichard Scheffenegger 	}
70597d8d152SAndre Oppermann #undef msec
706751ccc42SIan Lepore 	error = sbuf_finish(&sb);
707002d4558SJohn Baldwin 	sbuf_delete(&sb);
70897d8d152SAndre Oppermann 	return(error);
70997d8d152SAndre Oppermann }
71097d8d152SAndre Oppermann 
71197d8d152SAndre Oppermann /*
71202f26e98SRichard Scheffenegger  * Sysctl function: prints a histogram of the hostcache hashbucket
71302f26e98SRichard Scheffenegger  * utilization.
71402f26e98SRichard Scheffenegger  */
71502f26e98SRichard Scheffenegger static int
71602f26e98SRichard Scheffenegger sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS)
71702f26e98SRichard Scheffenegger {
71802f26e98SRichard Scheffenegger 	const int linesize = 50;
71902f26e98SRichard Scheffenegger 	struct sbuf sb;
72002f26e98SRichard Scheffenegger 	int i, error;
72102f26e98SRichard Scheffenegger 	int *histo;
72202f26e98SRichard Scheffenegger 	u_int hch_length;
72302f26e98SRichard Scheffenegger 
72402f26e98SRichard Scheffenegger 	if (jailed_without_vnet(curthread->td_ucred) != 0)
72502f26e98SRichard Scheffenegger 		return (EPERM);
72602f26e98SRichard Scheffenegger 
72702f26e98SRichard Scheffenegger 	histo = (int *)malloc(sizeof(int) * (V_tcp_hostcache.bucket_limit + 1),
72802f26e98SRichard Scheffenegger 			M_TEMP, M_NOWAIT|M_ZERO);
72902f26e98SRichard Scheffenegger 	if (histo == NULL)
73002f26e98SRichard Scheffenegger 		return(ENOMEM);
73102f26e98SRichard Scheffenegger 
73202f26e98SRichard Scheffenegger 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
73302f26e98SRichard Scheffenegger 		hch_length = V_tcp_hostcache.hashbase[i].hch_length;
73402f26e98SRichard Scheffenegger 		KASSERT(hch_length <= V_tcp_hostcache.bucket_limit,
73502f26e98SRichard Scheffenegger 		    ("tcp_hostcache: bucket limit exceeded at %u: %u",
73602f26e98SRichard Scheffenegger 		    i, hch_length));
73702f26e98SRichard Scheffenegger 		histo[hch_length]++;
73802f26e98SRichard Scheffenegger 	}
73902f26e98SRichard Scheffenegger 
74002f26e98SRichard Scheffenegger 	/* Use a buffer for 16 lines */
74102f26e98SRichard Scheffenegger 	sbuf_new_for_sysctl(&sb, NULL, 16 * linesize, req);
74202f26e98SRichard Scheffenegger 
74302f26e98SRichard Scheffenegger 	sbuf_printf(&sb, "\nLength\tCount\n");
74402f26e98SRichard Scheffenegger 	for (i = 0; i <= V_tcp_hostcache.bucket_limit; i++) {
74502f26e98SRichard Scheffenegger 		sbuf_printf(&sb, "%u\t%u\n", i, histo[i]);
74602f26e98SRichard Scheffenegger 	}
74702f26e98SRichard Scheffenegger 	error = sbuf_finish(&sb);
74802f26e98SRichard Scheffenegger 	sbuf_delete(&sb);
74902f26e98SRichard Scheffenegger 	free(histo, M_TEMP);
75002f26e98SRichard Scheffenegger 	return(error);
75102f26e98SRichard Scheffenegger }
75202f26e98SRichard Scheffenegger 
75302f26e98SRichard Scheffenegger /*
754fffb9f1dSBjoern A. Zeeb  * Caller has to make sure the curvnet is set properly.
75597d8d152SAndre Oppermann  */
75697d8d152SAndre Oppermann static void
757fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(int all)
75897d8d152SAndre Oppermann {
759d554522fSGleb Smirnoff 	struct hc_head *head;
760d554522fSGleb Smirnoff 	struct hc_metrics *hc_entry, *hc_next, *hc_prev;
76197d8d152SAndre Oppermann 	int i;
76297d8d152SAndre Oppermann 
763603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
764d554522fSGleb Smirnoff 		head = &V_tcp_hostcache.hashbase[i];
765d554522fSGleb Smirnoff 		hc_prev = NULL;
766d554522fSGleb Smirnoff 		THC_LOCK(head);
767d554522fSGleb Smirnoff 		CK_SLIST_FOREACH_SAFE(hc_entry, &head->hch_bucket, rmx_q,
768d554522fSGleb Smirnoff 		    hc_next) {
769d554522fSGleb Smirnoff 			KASSERT(head->hch_length > 0 && head->hch_length <=
7704f49e338SGleb Smirnoff 			    V_tcp_hostcache.bucket_limit, ("tcp_hostcache: "
771d554522fSGleb Smirnoff 			    "bucket length out of range at %u: %u", i,
772d554522fSGleb Smirnoff 			    head->hch_length));
773d554522fSGleb Smirnoff 			if (all ||
774d554522fSGleb Smirnoff 			    atomic_load_int(&hc_entry->rmx_expire) <= 0) {
775d554522fSGleb Smirnoff 				if (hc_prev != NULL) {
776d554522fSGleb Smirnoff 					KASSERT(hc_entry ==
777d554522fSGleb Smirnoff 					    CK_SLIST_NEXT(hc_prev, rmx_q),
778d554522fSGleb Smirnoff 					    ("%s: %p is not next to %p",
779d554522fSGleb Smirnoff 					    __func__, hc_entry, hc_prev));
780d554522fSGleb Smirnoff 					CK_SLIST_REMOVE_AFTER(hc_prev, rmx_q);
781d554522fSGleb Smirnoff 				} else {
782d554522fSGleb Smirnoff 					KASSERT(hc_entry ==
783d554522fSGleb Smirnoff 					    CK_SLIST_FIRST(&head->hch_bucket),
784d554522fSGleb Smirnoff 					    ("%s: %p is not first",
785d554522fSGleb Smirnoff 					    __func__, hc_entry));
786d554522fSGleb Smirnoff 					CK_SLIST_REMOVE_HEAD(&head->hch_bucket,
787d554522fSGleb Smirnoff 					    rmx_q);
78897d8d152SAndre Oppermann 				}
789d554522fSGleb Smirnoff 				uma_zfree_smr(V_tcp_hostcache.zone, hc_entry);
790d554522fSGleb Smirnoff 				head->hch_length--;
791d554522fSGleb Smirnoff 				atomic_subtract_int(&V_tcp_hostcache.cache_count, 1);
792d554522fSGleb Smirnoff 			} else {
793d554522fSGleb Smirnoff 				atomic_subtract_int(&hc_entry->rmx_expire,
794d554522fSGleb Smirnoff 				    V_tcp_hostcache.prune);
795d554522fSGleb Smirnoff 				hc_prev = hc_entry;
796d554522fSGleb Smirnoff 			}
797d554522fSGleb Smirnoff 		}
798d554522fSGleb Smirnoff 		THC_UNLOCK(head);
79997d8d152SAndre Oppermann 	}
800fffb9f1dSBjoern A. Zeeb }
801fffb9f1dSBjoern A. Zeeb 
802fffb9f1dSBjoern A. Zeeb /*
803fffb9f1dSBjoern A. Zeeb  * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
804fffb9f1dSBjoern A. Zeeb  * periodically from the callout.
805fffb9f1dSBjoern A. Zeeb  */
806fffb9f1dSBjoern A. Zeeb static void
807fffb9f1dSBjoern A. Zeeb tcp_hc_purge(void *arg)
808fffb9f1dSBjoern A. Zeeb {
809fffb9f1dSBjoern A. Zeeb 	CURVNET_SET((struct vnet *) arg);
810fffb9f1dSBjoern A. Zeeb 	int all = 0;
811fffb9f1dSBjoern A. Zeeb 
812fffb9f1dSBjoern A. Zeeb 	if (V_tcp_hostcache.purgeall) {
813b878ec02SRichard Scheffenegger 		if (V_tcp_hostcache.purgeall == 2)
814b878ec02SRichard Scheffenegger 			V_tcp_hostcache.hashsalt = arc4random();
815fffb9f1dSBjoern A. Zeeb 		all = 1;
816fffb9f1dSBjoern A. Zeeb 		V_tcp_hostcache.purgeall = 0;
817fffb9f1dSBjoern A. Zeeb 	}
818fffb9f1dSBjoern A. Zeeb 
819fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(all);
820ac957cd2SJulian Elischer 
821ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
822ac957cd2SJulian Elischer 	    tcp_hc_purge, arg);
82321ca7b57SMarko Zec 	CURVNET_RESTORE();
82497d8d152SAndre Oppermann }
82526456380SHiren Panchasara 
82626456380SHiren Panchasara /*
82726456380SHiren Panchasara  * Expire and purge all entries in hostcache immediately.
82826456380SHiren Panchasara  */
82926456380SHiren Panchasara static int
83026456380SHiren Panchasara sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS)
83126456380SHiren Panchasara {
83226456380SHiren Panchasara 	int error, val;
83326456380SHiren Panchasara 
83426456380SHiren Panchasara 	val = 0;
83526456380SHiren Panchasara 	error = sysctl_handle_int(oidp, &val, 0, req);
83626456380SHiren Panchasara 	if (error || !req->newptr)
83726456380SHiren Panchasara 		return (error);
83826456380SHiren Panchasara 
839b878ec02SRichard Scheffenegger 	if (val == 2)
840b878ec02SRichard Scheffenegger 		V_tcp_hostcache.hashsalt = arc4random();
84126456380SHiren Panchasara 	tcp_hc_purge_internal(1);
84226456380SHiren Panchasara 
84326456380SHiren Panchasara 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
84426456380SHiren Panchasara 	    tcp_hc_purge, curvnet);
84526456380SHiren Panchasara 
84626456380SHiren Panchasara 	return (0);
84726456380SHiren Panchasara }
848