xref: /freebsd/sys/netinet/tcp_hostcache.c (revision b80c06cc0af4a913778d6014eae6ce30e1ec2c68)
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>
6997d8d152SAndre Oppermann #include "opt_inet6.h"
7097d8d152SAndre Oppermann 
7197d8d152SAndre Oppermann #include <sys/param.h>
7297d8d152SAndre Oppermann #include <sys/systm.h>
73b878ec02SRichard Scheffenegger #include <sys/hash.h>
74b8a2fb91SHiren Panchasara #include <sys/jail.h>
7597d8d152SAndre Oppermann #include <sys/kernel.h>
7697d8d152SAndre Oppermann #include <sys/lock.h>
7797d8d152SAndre Oppermann #include <sys/mutex.h>
7897d8d152SAndre Oppermann #include <sys/malloc.h>
79b8a2fb91SHiren Panchasara #include <sys/proc.h>
80002d4558SJohn Baldwin #include <sys/sbuf.h>
81d554522fSGleb Smirnoff #include <sys/smr.h>
8297d8d152SAndre Oppermann #include <sys/socket.h>
8397d8d152SAndre Oppermann #include <sys/sysctl.h>
8497d8d152SAndre Oppermann 
85530c0060SRobert Watson #include <net/vnet.h>
8697d8d152SAndre Oppermann 
8797d8d152SAndre Oppermann #include <netinet/in.h>
8897d8d152SAndre Oppermann #include <netinet/in_pcb.h>
8997d8d152SAndre Oppermann #include <netinet/tcp.h>
9097d8d152SAndre Oppermann #include <netinet/tcp_var.h>
9197d8d152SAndre Oppermann 
9297d8d152SAndre Oppermann #include <vm/uma.h>
9397d8d152SAndre Oppermann 
942cca4c0eSGleb Smirnoff struct hc_head {
95d554522fSGleb Smirnoff 	CK_SLIST_HEAD(hc_qhead, hc_metrics) hch_bucket;
962cca4c0eSGleb Smirnoff 	u_int		hch_length;
972cca4c0eSGleb Smirnoff 	struct mtx	hch_mtx;
982cca4c0eSGleb Smirnoff };
992cca4c0eSGleb Smirnoff 
1002cca4c0eSGleb Smirnoff struct hc_metrics {
1012cca4c0eSGleb Smirnoff 	/* housekeeping */
10209000cc1SGleb Smirnoff 	CK_SLIST_ENTRY(hc_metrics) hc_q;
1032cca4c0eSGleb Smirnoff 	struct		in_addr ip4;	/* IP address */
1042cca4c0eSGleb Smirnoff 	struct		in6_addr ip6;	/* IP6 address */
1052cca4c0eSGleb Smirnoff 	uint32_t	ip6_zoneid;	/* IPv6 scope zone id */
1062cca4c0eSGleb Smirnoff 	/* endpoint specific values for tcp */
10709000cc1SGleb Smirnoff 	uint32_t	hc_mtu;		/* MTU for this path */
10809000cc1SGleb Smirnoff 	uint32_t	hc_ssthresh;	/* outbound gateway buffer limit */
10909000cc1SGleb Smirnoff 	uint32_t	hc_rtt;		/* estimated round trip time */
11009000cc1SGleb Smirnoff 	uint32_t	hc_rttvar;	/* estimated rtt variance */
11109000cc1SGleb Smirnoff 	uint32_t	hc_cwnd;	/* congestion window */
11209000cc1SGleb Smirnoff 	uint32_t	hc_sendpipe;	/* outbound delay-bandwidth product */
11309000cc1SGleb Smirnoff 	uint32_t	hc_recvpipe;	/* inbound delay-bandwidth product */
1142cca4c0eSGleb Smirnoff 	/* TCP hostcache internal data */
11509000cc1SGleb Smirnoff 	int		hc_expire;	/* lifetime for object */
116489bde57SGleb Smirnoff #ifdef	TCP_HC_COUNTERS
11709000cc1SGleb Smirnoff 	u_long		hc_hits;	/* number of hits */
11809000cc1SGleb Smirnoff 	u_long		hc_updates;	/* number of updates */
119489bde57SGleb Smirnoff #endif
1202cca4c0eSGleb Smirnoff };
1212cca4c0eSGleb Smirnoff 
1222cca4c0eSGleb Smirnoff struct tcp_hostcache {
1232cca4c0eSGleb Smirnoff 	struct hc_head	*hashbase;
1242cca4c0eSGleb Smirnoff 	uma_zone_t	zone;
125d554522fSGleb Smirnoff 	smr_t		smr;
1262cca4c0eSGleb Smirnoff 	u_int		hashsize;
1272cca4c0eSGleb Smirnoff 	u_int		hashmask;
128b878ec02SRichard Scheffenegger 	u_int		hashsalt;
1292cca4c0eSGleb Smirnoff 	u_int		bucket_limit;
1302cca4c0eSGleb Smirnoff 	u_int		cache_count;
1312cca4c0eSGleb Smirnoff 	u_int		cache_limit;
1322cca4c0eSGleb Smirnoff 	int		expire;
1332cca4c0eSGleb Smirnoff 	int		prune;
1342cca4c0eSGleb Smirnoff 	int		purgeall;
1352cca4c0eSGleb Smirnoff };
1362cca4c0eSGleb Smirnoff 
13797d8d152SAndre Oppermann /* Arbitrary values */
13897d8d152SAndre Oppermann #define TCP_HOSTCACHE_HASHSIZE		512
13997d8d152SAndre Oppermann #define TCP_HOSTCACHE_BUCKETLIMIT	30
14097d8d152SAndre Oppermann #define TCP_HOSTCACHE_EXPIRE		60*60	/* one hour */
14197d8d152SAndre Oppermann #define TCP_HOSTCACHE_PRUNE		5*60	/* every 5 minutes */
14297d8d152SAndre Oppermann 
1435f901c92SAndrew Turner VNET_DEFINE_STATIC(struct tcp_hostcache, tcp_hostcache);
1441e77c105SRobert Watson #define	V_tcp_hostcache		VNET(tcp_hostcache)
14582cea7e6SBjoern A. Zeeb 
1465f901c92SAndrew Turner VNET_DEFINE_STATIC(struct callout, tcp_hc_callout);
1471e77c105SRobert Watson #define	V_tcp_hc_callout	VNET(tcp_hc_callout)
14897d8d152SAndre Oppermann 
149*b80c06ccSGleb Smirnoff static struct hc_metrics *tcp_hc_lookup(const struct in_conninfo *);
15097d8d152SAndre Oppermann static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
15102f26e98SRichard Scheffenegger static int sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS);
15226456380SHiren Panchasara static int sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS);
153fffb9f1dSBjoern A. Zeeb static void tcp_hc_purge_internal(int);
15497d8d152SAndre Oppermann static void tcp_hc_purge(void *);
15597d8d152SAndre Oppermann 
1567029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache,
1577029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1580eb7cf4dSBrooks Davis     "TCP Host cache");
15997d8d152SAndre Oppermann 
1608a56c645SHiren Panchasara VNET_DEFINE(int, tcp_use_hostcache) = 1;
1618a56c645SHiren Panchasara #define V_tcp_use_hostcache  VNET(tcp_use_hostcache)
1628a56c645SHiren Panchasara SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
1638a56c645SHiren Panchasara     &VNET_NAME(tcp_use_hostcache), 0,
1648a56c645SHiren Panchasara     "Enable the TCP hostcache");
1658a56c645SHiren Panchasara 
1666df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN,
167eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.cache_limit), 0,
1688b615593SMarko Zec     "Overall entry limit for hostcache");
16997d8d152SAndre Oppermann 
1706df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN,
171eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.hashsize), 0,
1728b615593SMarko Zec     "Size of TCP hostcache hashtable");
17397d8d152SAndre Oppermann 
1746df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
1756df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0,
1768b615593SMarko Zec     "Per-bucket hash limit for hostcache");
17797d8d152SAndre Oppermann 
178529a2a0fSRichard Scheffenegger SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD,
179529a2a0fSRichard Scheffenegger     &VNET_NAME(tcp_hostcache.cache_count), 0,
1808b615593SMarko Zec     "Current number of entries in hostcache");
18197d8d152SAndre Oppermann 
1826df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW,
183eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.expire), 0,
1848b615593SMarko Zec     "Expire time of TCP hostcache entries");
18597d8d152SAndre Oppermann 
1866df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW,
187eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.prune), 0,
188eddfbb76SRobert Watson     "Time between purge runs");
189dba3c508SYaroslav Tykhiy 
1906df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW,
191eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.purgeall), 0,
192b4aa9cb2SGordon Bergling     "Expire all entries on next purge run");
19397d8d152SAndre Oppermann 
19497d8d152SAndre Oppermann SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
1957029da5cSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE,
1967029da5cSPawel Biernacki     0, 0, sysctl_tcp_hc_list, "A",
1977029da5cSPawel Biernacki     "List of all hostcache entries");
19897d8d152SAndre Oppermann 
19902f26e98SRichard Scheffenegger SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, histo,
20002f26e98SRichard Scheffenegger     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE,
20102f26e98SRichard Scheffenegger     0, 0, sysctl_tcp_hc_histo, "A",
20202f26e98SRichard Scheffenegger     "Print a histogram of hostcache hashbucket utilization");
20302f26e98SRichard Scheffenegger 
20426456380SHiren Panchasara SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, purgenow,
2057029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2067029da5cSPawel Biernacki     NULL, 0, sysctl_tcp_hc_purgenow, "I",
2077029da5cSPawel Biernacki     "Immediately purge all entries");
20897d8d152SAndre Oppermann 
20997d8d152SAndre Oppermann static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
21097d8d152SAndre Oppermann 
211b878ec02SRichard Scheffenegger /* Use jenkins_hash32(), as in other parts of the tcp stack */
212d554522fSGleb Smirnoff #define	HOSTCACHE_HASH(inc)						\
213d554522fSGleb Smirnoff 	((inc)->inc_flags & INC_ISIPV6) ?				\
214d554522fSGleb Smirnoff 		(jenkins_hash32((inc)->inc6_faddr.s6_addr32, 4,		\
215d554522fSGleb Smirnoff 		V_tcp_hostcache.hashsalt) & V_tcp_hostcache.hashmask)	\
216d554522fSGleb Smirnoff 	:								\
217d554522fSGleb Smirnoff 		(jenkins_hash32(&(inc)->inc_faddr.s_addr, 1,		\
218d554522fSGleb Smirnoff 		V_tcp_hostcache.hashsalt) & V_tcp_hostcache.hashmask)
21997d8d152SAndre Oppermann 
2201a7fe55aSGleb Smirnoff #define THC_LOCK(h)		mtx_lock(&(h)->hch_mtx)
2211a7fe55aSGleb Smirnoff #define THC_UNLOCK(h)		mtx_unlock(&(h)->hch_mtx)
22297d8d152SAndre Oppermann 
22397d8d152SAndre Oppermann void
tcp_hc_init(void)22497d8d152SAndre Oppermann tcp_hc_init(void)
22597d8d152SAndre Oppermann {
22632fe38f1SAndrey Zonov 	u_int cache_limit;
22797d8d152SAndre Oppermann 	int i;
22897d8d152SAndre Oppermann 
22997d8d152SAndre Oppermann 	/*
230e487a5e2SRobert Watson 	 * Initialize hostcache structures.
23197d8d152SAndre Oppermann 	 */
232529a2a0fSRichard Scheffenegger 	atomic_store_int(&V_tcp_hostcache.cache_count, 0);
233603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
234603724d3SBjoern A. Zeeb 	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
235603724d3SBjoern A. Zeeb 	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
236603724d3SBjoern A. Zeeb 	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
237b878ec02SRichard Scheffenegger 	V_tcp_hostcache.hashsalt = arc4random();
23897d8d152SAndre Oppermann 
23997d8d152SAndre Oppermann 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
240603724d3SBjoern A. Zeeb 	    &V_tcp_hostcache.hashsize);
241603724d3SBjoern A. Zeeb 	if (!powerof2(V_tcp_hostcache.hashsize)) {
24297d8d152SAndre Oppermann 		printf("WARNING: hostcache hash size is not a power of 2.\n");
243603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
24497d8d152SAndre Oppermann 	}
245603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
24697d8d152SAndre Oppermann 
24732fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
24832fe38f1SAndrey Zonov 	    &V_tcp_hostcache.bucket_limit);
24932fe38f1SAndrey Zonov 
25032fe38f1SAndrey Zonov 	cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
25132fe38f1SAndrey Zonov 	V_tcp_hostcache.cache_limit = cache_limit;
25232fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
25332fe38f1SAndrey Zonov 	    &V_tcp_hostcache.cache_limit);
25432fe38f1SAndrey Zonov 	if (V_tcp_hostcache.cache_limit > cache_limit)
25532fe38f1SAndrey Zonov 		V_tcp_hostcache.cache_limit = cache_limit;
25632fe38f1SAndrey Zonov 
25797d8d152SAndre Oppermann 	/*
258e487a5e2SRobert Watson 	 * Allocate the hash table.
25997d8d152SAndre Oppermann 	 */
260603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase = (struct hc_head *)
261603724d3SBjoern A. Zeeb 	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
26297d8d152SAndre Oppermann 		   M_HOSTCACHE, M_WAITOK | M_ZERO);
26397d8d152SAndre Oppermann 
26497d8d152SAndre Oppermann 	/*
265e487a5e2SRobert Watson 	 * Initialize the hash buckets.
26697d8d152SAndre Oppermann 	 */
267603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
268d554522fSGleb Smirnoff 		CK_SLIST_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
269603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[i].hch_length = 0;
270603724d3SBjoern A. Zeeb 		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
27197d8d152SAndre Oppermann 			  NULL, MTX_DEF);
27297d8d152SAndre Oppermann 	}
27397d8d152SAndre Oppermann 
27497d8d152SAndre Oppermann 	/*
27597d8d152SAndre Oppermann 	 * Allocate the hostcache entries.
27697d8d152SAndre Oppermann 	 */
277ac957cd2SJulian Elischer 	V_tcp_hostcache.zone =
278ac957cd2SJulian Elischer 	    uma_zcreate("hostcache", sizeof(struct hc_metrics),
279d554522fSGleb Smirnoff 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_SMR);
280603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
281d554522fSGleb Smirnoff 	V_tcp_hostcache.smr = uma_zone_get_smr(V_tcp_hostcache.zone);
28297d8d152SAndre Oppermann 
28397d8d152SAndre Oppermann 	/*
28497d8d152SAndre Oppermann 	 * Set up periodic cache cleanup.
28597d8d152SAndre Oppermann 	 */
286fd90e2edSJung-uk Kim 	callout_init(&V_tcp_hc_callout, 1);
287ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
28821ca7b57SMarko Zec 	    tcp_hc_purge, curvnet);
28997d8d152SAndre Oppermann }
29097d8d152SAndre Oppermann 
291bc29160dSMarko Zec #ifdef VIMAGE
292bc29160dSMarko Zec void
tcp_hc_destroy(void)293bc29160dSMarko Zec tcp_hc_destroy(void)
294bc29160dSMarko Zec {
295fffb9f1dSBjoern A. Zeeb 	int i;
296bc29160dSMarko Zec 
297bc29160dSMarko Zec 	callout_drain(&V_tcp_hc_callout);
298fffb9f1dSBjoern A. Zeeb 
299fffb9f1dSBjoern A. Zeeb 	/* Purge all hc entries. */
300fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(1);
301fffb9f1dSBjoern A. Zeeb 
302fffb9f1dSBjoern A. Zeeb 	/* Free the uma zone and the allocated hash table. */
303fffb9f1dSBjoern A. Zeeb 	uma_zdestroy(V_tcp_hostcache.zone);
304fffb9f1dSBjoern A. Zeeb 
305fffb9f1dSBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++)
306fffb9f1dSBjoern A. Zeeb 		mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx);
307fffb9f1dSBjoern A. Zeeb 	free(V_tcp_hostcache.hashbase, M_HOSTCACHE);
308bc29160dSMarko Zec }
309bc29160dSMarko Zec #endif
310bc29160dSMarko Zec 
31197d8d152SAndre Oppermann /*
312d554522fSGleb Smirnoff  * Internal function: compare cache entry to a connection.
31397d8d152SAndre Oppermann  */
314d554522fSGleb Smirnoff static bool
tcp_hc_cmp(struct hc_metrics * hc_entry,const struct in_conninfo * inc)315*b80c06ccSGleb Smirnoff tcp_hc_cmp(struct hc_metrics *hc_entry, const struct in_conninfo *inc)
31697d8d152SAndre Oppermann {
31797d8d152SAndre Oppermann 
318dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
319028bdf28SAndrey V. Elsukov 		/* XXX: check ip6_zoneid */
32097d8d152SAndre Oppermann 		if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
32197d8d152SAndre Oppermann 		    sizeof(inc->inc6_faddr)) == 0)
322d554522fSGleb Smirnoff 			return (true);
32397d8d152SAndre Oppermann 	} else {
32497d8d152SAndre Oppermann 		if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
32597d8d152SAndre Oppermann 		    sizeof(inc->inc_faddr)) == 0)
326d554522fSGleb Smirnoff 			return (true);
32797d8d152SAndre Oppermann 	}
328d554522fSGleb Smirnoff 
329d554522fSGleb Smirnoff 	return (false);
33097d8d152SAndre Oppermann }
33197d8d152SAndre Oppermann 
33297d8d152SAndre Oppermann /*
333d554522fSGleb Smirnoff  * Internal function: look up an entry in the hostcache for read.
334d554522fSGleb Smirnoff  * On success returns in SMR section.
33597d8d152SAndre Oppermann  */
33697d8d152SAndre Oppermann static struct hc_metrics *
tcp_hc_lookup(const struct in_conninfo * inc)337*b80c06ccSGleb Smirnoff tcp_hc_lookup(const struct in_conninfo *inc)
33897d8d152SAndre Oppermann {
33997d8d152SAndre Oppermann 	struct hc_head *hc_head;
34097d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
34197d8d152SAndre Oppermann 
3424f49e338SGleb Smirnoff 	KASSERT(inc != NULL, ("%s: NULL in_conninfo", __func__));
34397d8d152SAndre Oppermann 
344d554522fSGleb Smirnoff 	hc_head = &V_tcp_hostcache.hashbase[HOSTCACHE_HASH(inc)];
34597d8d152SAndre Oppermann 
34697d8d152SAndre Oppermann 	/*
347d554522fSGleb Smirnoff 	 * Iterate through entries in bucket row looking for a match.
34897d8d152SAndre Oppermann 	 */
349d554522fSGleb Smirnoff 	smr_enter(V_tcp_hostcache.smr);
35009000cc1SGleb Smirnoff 	CK_SLIST_FOREACH(hc_entry, &hc_head->hch_bucket, hc_q)
351d554522fSGleb Smirnoff 		if (tcp_hc_cmp(hc_entry, inc))
352d554522fSGleb Smirnoff 			break;
35397d8d152SAndre Oppermann 
354d554522fSGleb Smirnoff 	if (hc_entry != NULL) {
35509000cc1SGleb Smirnoff 		if (atomic_load_int(&hc_entry->hc_expire) !=
356d554522fSGleb Smirnoff 		    V_tcp_hostcache.expire)
35709000cc1SGleb Smirnoff 			atomic_store_int(&hc_entry->hc_expire,
358d554522fSGleb Smirnoff 			    V_tcp_hostcache.expire);
359d554522fSGleb Smirnoff #ifdef	TCP_HC_COUNTERS
36009000cc1SGleb Smirnoff 		hc_entry->hc_hits++;
361cd6c4060SAndre Oppermann #endif
362028bdf28SAndrey V. Elsukov 	} else
363d554522fSGleb Smirnoff 		smr_exit(V_tcp_hostcache.smr);
36497d8d152SAndre Oppermann 
3654f49e338SGleb Smirnoff 	return (hc_entry);
36697d8d152SAndre Oppermann }
36797d8d152SAndre Oppermann 
36897d8d152SAndre Oppermann /*
36997d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and fill out the
370e487a5e2SRobert Watson  * supplied TCP metrics structure.  Fills in NULL when no entry was found or
371e487a5e2SRobert Watson  * a value is not set.
37297d8d152SAndre Oppermann  */
37397d8d152SAndre Oppermann void
tcp_hc_get(const struct in_conninfo * inc,struct hc_metrics_lite * hc_metrics_lite)374*b80c06ccSGleb Smirnoff tcp_hc_get(const struct in_conninfo *inc,
375*b80c06ccSGleb Smirnoff     struct hc_metrics_lite *hc_metrics_lite)
37697d8d152SAndre Oppermann {
37797d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
37897d8d152SAndre Oppermann 
3796b8fba3cSMichael Tuexen 	if (!V_tcp_use_hostcache) {
3806b8fba3cSMichael Tuexen 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
3818a56c645SHiren Panchasara 		return;
3826b8fba3cSMichael Tuexen 	}
3838a56c645SHiren Panchasara 
38497d8d152SAndre Oppermann 	/*
385e487a5e2SRobert Watson 	 * Find the right bucket.
38697d8d152SAndre Oppermann 	 */
387d554522fSGleb Smirnoff 	hc_entry = tcp_hc_lookup(inc);
38897d8d152SAndre Oppermann 
38997d8d152SAndre Oppermann 	/*
390e487a5e2SRobert Watson 	 * If we don't have an existing object.
39197d8d152SAndre Oppermann 	 */
39297d8d152SAndre Oppermann 	if (hc_entry == NULL) {
39397d8d152SAndre Oppermann 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
39497d8d152SAndre Oppermann 		return;
39597d8d152SAndre Oppermann 	}
39697d8d152SAndre Oppermann 
39709000cc1SGleb Smirnoff 	hc_metrics_lite->hc_mtu = atomic_load_32(&hc_entry->hc_mtu);
39809000cc1SGleb Smirnoff 	hc_metrics_lite->hc_ssthresh = atomic_load_32(&hc_entry->hc_ssthresh);
39909000cc1SGleb Smirnoff 	hc_metrics_lite->hc_rtt = atomic_load_32(&hc_entry->hc_rtt);
40009000cc1SGleb Smirnoff 	hc_metrics_lite->hc_rttvar = atomic_load_32(&hc_entry->hc_rttvar);
40109000cc1SGleb Smirnoff 	hc_metrics_lite->hc_cwnd = atomic_load_32(&hc_entry->hc_cwnd);
40209000cc1SGleb Smirnoff 	hc_metrics_lite->hc_sendpipe = atomic_load_32(&hc_entry->hc_sendpipe);
40309000cc1SGleb Smirnoff 	hc_metrics_lite->hc_recvpipe = atomic_load_32(&hc_entry->hc_recvpipe);
40497d8d152SAndre Oppermann 
405d554522fSGleb Smirnoff 	smr_exit(V_tcp_hostcache.smr);
40697d8d152SAndre Oppermann }
40797d8d152SAndre Oppermann 
40897d8d152SAndre Oppermann /*
40997d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and return the
4108a56c645SHiren Panchasara  * discovered path MTU.  Returns 0 if no entry is found or value is not
4116fbed4afSRobert Watson  * set.
41297d8d152SAndre Oppermann  */
4133ac12506SJonathan T. Looney uint32_t
tcp_hc_getmtu(const struct in_conninfo * inc)414*b80c06ccSGleb Smirnoff tcp_hc_getmtu(const struct in_conninfo *inc)
41597d8d152SAndre Oppermann {
41697d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
4173ac12506SJonathan T. Looney 	uint32_t mtu;
41897d8d152SAndre Oppermann 
4198a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
4204f49e338SGleb Smirnoff 		return (0);
4218a56c645SHiren Panchasara 
422d554522fSGleb Smirnoff 	hc_entry = tcp_hc_lookup(inc);
42397d8d152SAndre Oppermann 	if (hc_entry == NULL) {
4244f49e338SGleb Smirnoff 		return (0);
42597d8d152SAndre Oppermann 	}
42697d8d152SAndre Oppermann 
42709000cc1SGleb Smirnoff 	mtu = atomic_load_32(&hc_entry->hc_mtu);
428d554522fSGleb Smirnoff 	smr_exit(V_tcp_hostcache.smr);
429d554522fSGleb Smirnoff 
4304f49e338SGleb Smirnoff 	return (mtu);
43197d8d152SAndre Oppermann }
43297d8d152SAndre Oppermann 
43397d8d152SAndre Oppermann /*
434e487a5e2SRobert Watson  * External function: update the MTU value of an entry in the hostcache.
43597d8d152SAndre Oppermann  * Creates a new entry if none was found.
43697d8d152SAndre Oppermann  */
43797d8d152SAndre Oppermann void
tcp_hc_updatemtu(const struct in_conninfo * inc,uint32_t mtu)438*b80c06ccSGleb Smirnoff tcp_hc_updatemtu(const struct in_conninfo *inc, uint32_t mtu)
43997d8d152SAndre Oppermann {
44009000cc1SGleb Smirnoff 	struct hc_metrics_lite hcml = { .hc_mtu = mtu };
44197d8d152SAndre Oppermann 
4420c25bf7eSGleb Smirnoff 	return (tcp_hc_update(inc, &hcml));
44397d8d152SAndre Oppermann }
44497d8d152SAndre Oppermann 
44597d8d152SAndre Oppermann /*
446e487a5e2SRobert Watson  * External function: update the TCP metrics of an entry in the hostcache.
44797d8d152SAndre Oppermann  * Creates a new entry if none was found.
44897d8d152SAndre Oppermann  */
44997d8d152SAndre Oppermann void
tcp_hc_update(const struct in_conninfo * inc,struct hc_metrics_lite * hcml)450*b80c06ccSGleb Smirnoff tcp_hc_update(const struct in_conninfo *inc, struct hc_metrics_lite *hcml)
45197d8d152SAndre Oppermann {
452d554522fSGleb Smirnoff 	struct hc_head *hc_head;
453d554522fSGleb Smirnoff 	struct hc_metrics *hc_entry, *hc_prev;
454d554522fSGleb Smirnoff 	uint32_t v;
455d554522fSGleb Smirnoff 	bool new;
45697d8d152SAndre Oppermann 
4578a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
4588a56c645SHiren Panchasara 		return;
4598a56c645SHiren Panchasara 
460d554522fSGleb Smirnoff 	hc_head = &V_tcp_hostcache.hashbase[HOSTCACHE_HASH(inc)];
461d554522fSGleb Smirnoff 	hc_prev = NULL;
462d554522fSGleb Smirnoff 
463d554522fSGleb Smirnoff 	THC_LOCK(hc_head);
46409000cc1SGleb Smirnoff 	CK_SLIST_FOREACH(hc_entry, &hc_head->hch_bucket, hc_q) {
465d554522fSGleb Smirnoff 		if (tcp_hc_cmp(hc_entry, inc))
466d554522fSGleb Smirnoff 			break;
46709000cc1SGleb Smirnoff 		if (CK_SLIST_NEXT(hc_entry, hc_q) != NULL)
468d554522fSGleb Smirnoff 			hc_prev = hc_entry;
469d554522fSGleb Smirnoff 	}
470d554522fSGleb Smirnoff 
471d554522fSGleb Smirnoff 	if (hc_entry != NULL) {
47209000cc1SGleb Smirnoff 		if (atomic_load_int(&hc_entry->hc_expire) !=
473d554522fSGleb Smirnoff 		    V_tcp_hostcache.expire)
47409000cc1SGleb Smirnoff 			atomic_store_int(&hc_entry->hc_expire,
475d554522fSGleb Smirnoff 			    V_tcp_hostcache.expire);
476d554522fSGleb Smirnoff #ifdef	TCP_HC_COUNTERS
47709000cc1SGleb Smirnoff 		hc_entry->hc_updates++;
478d554522fSGleb Smirnoff #endif
479d554522fSGleb Smirnoff 		new = false;
480d554522fSGleb Smirnoff 	} else {
481d554522fSGleb Smirnoff 		/*
482d554522fSGleb Smirnoff 		 * Try to allocate a new entry.  If the bucket limit is
483d554522fSGleb Smirnoff 		 * reached, delete the least-used element, located at the end
484d554522fSGleb Smirnoff 		 * of the CK_SLIST.  During lookup we saved the pointer to
485d554522fSGleb Smirnoff 		 * the second to last element, in case if list has at least 2
486d554522fSGleb Smirnoff 		 * elements.  This will allow to delete last element without
487d554522fSGleb Smirnoff 		 * extra traversal.
488d554522fSGleb Smirnoff 		 *
489d554522fSGleb Smirnoff 		 * Give up if the row is empty.
490d554522fSGleb Smirnoff 		 */
491d554522fSGleb Smirnoff 		if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
492d554522fSGleb Smirnoff 		    atomic_load_int(&V_tcp_hostcache.cache_count) >=
493d554522fSGleb Smirnoff 		    V_tcp_hostcache.cache_limit) {
494d554522fSGleb Smirnoff 			if (hc_prev != NULL) {
49509000cc1SGleb Smirnoff 				hc_entry = CK_SLIST_NEXT(hc_prev, hc_q);
49609000cc1SGleb Smirnoff 				KASSERT(CK_SLIST_NEXT(hc_entry, hc_q) == NULL,
497d554522fSGleb Smirnoff 				    ("%s: %p is not one to last",
498d554522fSGleb Smirnoff 				    __func__, hc_prev));
49909000cc1SGleb Smirnoff 				CK_SLIST_REMOVE_AFTER(hc_prev, hc_q);
500d554522fSGleb Smirnoff 			} else if ((hc_entry =
501d554522fSGleb Smirnoff 			    CK_SLIST_FIRST(&hc_head->hch_bucket)) != NULL) {
50209000cc1SGleb Smirnoff 				KASSERT(CK_SLIST_NEXT(hc_entry, hc_q) == NULL,
503d554522fSGleb Smirnoff 				    ("%s: %p is not the only element",
504d554522fSGleb Smirnoff 				    __func__, hc_entry));
505d554522fSGleb Smirnoff 				CK_SLIST_REMOVE_HEAD(&hc_head->hch_bucket,
50609000cc1SGleb Smirnoff 				    hc_q);
507d554522fSGleb Smirnoff 			} else {
508d554522fSGleb Smirnoff 				THC_UNLOCK(hc_head);
509d554522fSGleb Smirnoff 				return;
510d554522fSGleb Smirnoff 			}
511d554522fSGleb Smirnoff 			KASSERT(hc_head->hch_length > 0 &&
512d554522fSGleb Smirnoff 			    hc_head->hch_length <= V_tcp_hostcache.bucket_limit,
513d554522fSGleb Smirnoff 			    ("tcp_hostcache: bucket length violated at %p",
514d554522fSGleb Smirnoff 			    hc_head));
515d554522fSGleb Smirnoff 			hc_head->hch_length--;
516d554522fSGleb Smirnoff 			atomic_subtract_int(&V_tcp_hostcache.cache_count, 1);
517d554522fSGleb Smirnoff 			TCPSTAT_INC(tcps_hc_bucketoverflow);
518d554522fSGleb Smirnoff 			uma_zfree_smr(V_tcp_hostcache.zone, hc_entry);
519d554522fSGleb Smirnoff 		}
520d554522fSGleb Smirnoff 
521d554522fSGleb Smirnoff 		/*
522d554522fSGleb Smirnoff 		 * Allocate a new entry, or balk if not possible.
523d554522fSGleb Smirnoff 		 */
524d554522fSGleb Smirnoff 		hc_entry = uma_zalloc_smr(V_tcp_hostcache.zone, M_NOWAIT);
52597d8d152SAndre Oppermann 		if (hc_entry == NULL) {
526d554522fSGleb Smirnoff 			THC_UNLOCK(hc_head);
52797d8d152SAndre Oppermann 			return;
52897d8d152SAndre Oppermann 		}
52997d8d152SAndre Oppermann 
530d554522fSGleb Smirnoff 		/*
531d554522fSGleb Smirnoff 		 * Initialize basic information of hostcache entry.
532d554522fSGleb Smirnoff 		 */
533d554522fSGleb Smirnoff 		bzero(hc_entry, sizeof(*hc_entry));
534d554522fSGleb Smirnoff 		if (inc->inc_flags & INC_ISIPV6) {
535d554522fSGleb Smirnoff 			hc_entry->ip6 = inc->inc6_faddr;
536d554522fSGleb Smirnoff 			hc_entry->ip6_zoneid = inc->inc6_zoneid;
537d554522fSGleb Smirnoff 		} else
538d554522fSGleb Smirnoff 			hc_entry->ip4 = inc->inc_faddr;
53909000cc1SGleb Smirnoff 		hc_entry->hc_expire = V_tcp_hostcache.expire;
540d554522fSGleb Smirnoff 		new = true;
541d554522fSGleb Smirnoff 	}
542d554522fSGleb Smirnoff 
543d554522fSGleb Smirnoff 	/*
544d554522fSGleb Smirnoff 	 * Fill in data.  Use atomics, since an existing entry is
545d554522fSGleb Smirnoff 	 * accessible by readers in SMR section.
546d554522fSGleb Smirnoff 	 */
54709000cc1SGleb Smirnoff 	if (hcml->hc_mtu != 0) {
54809000cc1SGleb Smirnoff 		atomic_store_32(&hc_entry->hc_mtu, hcml->hc_mtu);
5490c25bf7eSGleb Smirnoff 	}
55009000cc1SGleb Smirnoff 	if (hcml->hc_rtt != 0) {
55109000cc1SGleb Smirnoff 		if (hc_entry->hc_rtt == 0)
55209000cc1SGleb Smirnoff 			v = hcml->hc_rtt;
55397d8d152SAndre Oppermann 		else
55409000cc1SGleb Smirnoff 			v = ((uint64_t)hc_entry->hc_rtt +
55509000cc1SGleb Smirnoff 			    (uint64_t)hcml->hc_rtt) / 2;
55609000cc1SGleb Smirnoff 		atomic_store_32(&hc_entry->hc_rtt, v);
55778b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrtt);
55897d8d152SAndre Oppermann 	}
55909000cc1SGleb Smirnoff 	if (hcml->hc_rttvar != 0) {
56009000cc1SGleb Smirnoff 	        if (hc_entry->hc_rttvar == 0)
56109000cc1SGleb Smirnoff 			v = hcml->hc_rttvar;
56297d8d152SAndre Oppermann 		else
56309000cc1SGleb Smirnoff 			v = ((uint64_t)hc_entry->hc_rttvar +
56409000cc1SGleb Smirnoff 			    (uint64_t)hcml->hc_rttvar) / 2;
56509000cc1SGleb Smirnoff 		atomic_store_32(&hc_entry->hc_rttvar, v);
56678b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrttvar);
56797d8d152SAndre Oppermann 	}
56809000cc1SGleb Smirnoff 	if (hcml->hc_ssthresh != 0) {
56909000cc1SGleb Smirnoff 		if (hc_entry->hc_ssthresh == 0)
57009000cc1SGleb Smirnoff 			v = hcml->hc_ssthresh;
57197d8d152SAndre Oppermann 		else
57209000cc1SGleb Smirnoff 			v = (hc_entry->hc_ssthresh + hcml->hc_ssthresh) / 2;
57309000cc1SGleb Smirnoff 		atomic_store_32(&hc_entry->hc_ssthresh, v);
57478b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedssthresh);
57597d8d152SAndre Oppermann 	}
57609000cc1SGleb Smirnoff 	if (hcml->hc_cwnd != 0) {
57709000cc1SGleb Smirnoff 		if (hc_entry->hc_cwnd == 0)
57809000cc1SGleb Smirnoff 			v = hcml->hc_cwnd;
57997d8d152SAndre Oppermann 		else
58009000cc1SGleb Smirnoff 			v = ((uint64_t)hc_entry->hc_cwnd +
58109000cc1SGleb Smirnoff 			    (uint64_t)hcml->hc_cwnd) / 2;
58209000cc1SGleb Smirnoff 		atomic_store_32(&hc_entry->hc_cwnd, v);
58378b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedcwnd); */
58497d8d152SAndre Oppermann 	}
58509000cc1SGleb Smirnoff 	if (hcml->hc_sendpipe != 0) {
58609000cc1SGleb Smirnoff 		if (hc_entry->hc_sendpipe == 0)
58709000cc1SGleb Smirnoff 			v = hcml->hc_sendpipe;
58897d8d152SAndre Oppermann 		else
58909000cc1SGleb Smirnoff 			v = ((uint64_t)hc_entry->hc_sendpipe +
59009000cc1SGleb Smirnoff 			    (uint64_t)hcml->hc_sendpipe) /2;
59109000cc1SGleb Smirnoff 		atomic_store_32(&hc_entry->hc_sendpipe, v);
59278b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedsendpipe); */
59397d8d152SAndre Oppermann 	}
59409000cc1SGleb Smirnoff 	if (hcml->hc_recvpipe != 0) {
59509000cc1SGleb Smirnoff 		if (hc_entry->hc_recvpipe == 0)
59609000cc1SGleb Smirnoff 			v = hcml->hc_recvpipe;
59797d8d152SAndre Oppermann 		else
59809000cc1SGleb Smirnoff 			v = ((uint64_t)hc_entry->hc_recvpipe +
59909000cc1SGleb Smirnoff 			    (uint64_t)hcml->hc_recvpipe) /2;
60009000cc1SGleb Smirnoff 		atomic_store_32(&hc_entry->hc_recvpipe, v);
60178b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedrecvpipe); */
60297d8d152SAndre Oppermann 	}
60397d8d152SAndre Oppermann 
604d554522fSGleb Smirnoff 	/*
605d554522fSGleb Smirnoff 	 * Put it upfront.
606d554522fSGleb Smirnoff 	 */
607d554522fSGleb Smirnoff 	if (new) {
60809000cc1SGleb Smirnoff 		CK_SLIST_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, hc_q);
609d554522fSGleb Smirnoff 		hc_head->hch_length++;
610d554522fSGleb Smirnoff 		KASSERT(hc_head->hch_length <= V_tcp_hostcache.bucket_limit,
611d554522fSGleb Smirnoff 		    ("tcp_hostcache: bucket length too high at %p", hc_head));
612d554522fSGleb Smirnoff 		atomic_add_int(&V_tcp_hostcache.cache_count, 1);
613d554522fSGleb Smirnoff 		TCPSTAT_INC(tcps_hc_added);
614d554522fSGleb Smirnoff 	} else if (hc_entry != CK_SLIST_FIRST(&hc_head->hch_bucket)) {
61509000cc1SGleb Smirnoff 		KASSERT(CK_SLIST_NEXT(hc_prev, hc_q) == hc_entry,
616d554522fSGleb Smirnoff 		    ("%s: %p next is not %p", __func__, hc_prev, hc_entry));
61709000cc1SGleb Smirnoff 		CK_SLIST_REMOVE_AFTER(hc_prev, hc_q);
61809000cc1SGleb Smirnoff 		CK_SLIST_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, hc_q);
619d554522fSGleb Smirnoff 	}
620d554522fSGleb Smirnoff 	THC_UNLOCK(hc_head);
62197d8d152SAndre Oppermann }
62297d8d152SAndre Oppermann 
62397d8d152SAndre Oppermann /*
62497d8d152SAndre Oppermann  * Sysctl function: prints the list and values of all hostcache entries in
62597d8d152SAndre Oppermann  * unsorted order.
62697d8d152SAndre Oppermann  */
62797d8d152SAndre Oppermann static int
sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)62897d8d152SAndre Oppermann sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
62997d8d152SAndre Oppermann {
630751ccc42SIan Lepore 	const int linesize = 128;
631002d4558SJohn Baldwin 	struct sbuf sb;
632cb0dd7e1SRichard Scheffenegger 	int i, error, len;
63397d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
6348144690aSEric van Gyzen 	char ip4buf[INET_ADDRSTRLEN];
6351d54aa3bSBjoern A. Zeeb #ifdef INET6
6361d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
6371d54aa3bSBjoern A. Zeeb #endif
63897d8d152SAndre Oppermann 
639b8a2fb91SHiren Panchasara 	if (jailed_without_vnet(curthread->td_ucred) != 0)
640b8a2fb91SHiren Panchasara 		return (EPERM);
641b8a2fb91SHiren Panchasara 
642cb0dd7e1SRichard Scheffenegger 	/* Optimize Buffer length query by sbin/sysctl */
643cb0dd7e1SRichard Scheffenegger 	if (req->oldptr == NULL) {
644529a2a0fSRichard Scheffenegger 		len = (atomic_load_int(&V_tcp_hostcache.cache_count) + 1) *
645529a2a0fSRichard Scheffenegger 			linesize;
646cb0dd7e1SRichard Scheffenegger 		return (SYSCTL_OUT(req, NULL, len));
647cb0dd7e1SRichard Scheffenegger 	}
648cb0dd7e1SRichard Scheffenegger 
649cb0dd7e1SRichard Scheffenegger 	error = sysctl_wire_old_buffer(req, 0);
650cb0dd7e1SRichard Scheffenegger 	if (error != 0) {
651cb0dd7e1SRichard Scheffenegger 		return(error);
652cb0dd7e1SRichard Scheffenegger 	}
653cb0dd7e1SRichard Scheffenegger 
65486988046SRichard Scheffenegger 	/* Use a buffer sized for one full bucket */
655529a2a0fSRichard Scheffenegger 	sbuf_new_for_sysctl(&sb, NULL, V_tcp_hostcache.bucket_limit *
656529a2a0fSRichard Scheffenegger 		linesize, req);
65797d8d152SAndre Oppermann 
658002d4558SJohn Baldwin 	sbuf_printf(&sb,
6594d163382SHiren Panchasara 		"\nIP address        MTU  SSTRESH      RTT   RTTVAR "
660489bde57SGleb Smirnoff 		"    CWND SENDPIPE RECVPIPE "
661489bde57SGleb Smirnoff #ifdef	TCP_HC_COUNTERS
662489bde57SGleb Smirnoff 		"HITS  UPD  "
663489bde57SGleb Smirnoff #endif
664489bde57SGleb Smirnoff 		"EXP\n");
66586988046SRichard Scheffenegger 	sbuf_drain(&sb);
66697d8d152SAndre Oppermann 
66797d8d152SAndre Oppermann #define msec(u) (((u) + 500) / 1000)
668603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
6691a7fe55aSGleb Smirnoff 		THC_LOCK(&V_tcp_hostcache.hashbase[i]);
670d554522fSGleb Smirnoff 		CK_SLIST_FOREACH(hc_entry,
67109000cc1SGleb Smirnoff 		    &V_tcp_hostcache.hashbase[i].hch_bucket, hc_q) {
672002d4558SJohn Baldwin 			sbuf_printf(&sb,
673489bde57SGleb Smirnoff 			    "%-15s %5u %8u %6lums %6lums %8u %8u %8u "
674489bde57SGleb Smirnoff #ifdef	TCP_HC_COUNTERS
675489bde57SGleb Smirnoff 			    "%4lu %4lu "
676489bde57SGleb Smirnoff #endif
677489bde57SGleb Smirnoff 			    "%4i\n",
6788144690aSEric van Gyzen 			    hc_entry->ip4.s_addr ?
6798144690aSEric van Gyzen 			        inet_ntoa_r(hc_entry->ip4, ip4buf) :
68097d8d152SAndre Oppermann #ifdef INET6
6811d54aa3bSBjoern A. Zeeb 				ip6_sprintf(ip6buf, &hc_entry->ip6),
68297d8d152SAndre Oppermann #else
68397d8d152SAndre Oppermann 				"IPv6?",
68497d8d152SAndre Oppermann #endif
68509000cc1SGleb Smirnoff 			    hc_entry->hc_mtu,
68609000cc1SGleb Smirnoff 			    hc_entry->hc_ssthresh,
68709000cc1SGleb Smirnoff 			    msec((u_long)hc_entry->hc_rtt *
68897d8d152SAndre Oppermann 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
68909000cc1SGleb Smirnoff 			    msec((u_long)hc_entry->hc_rttvar *
6903a288e90SMikolaj Golub 				(RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))),
69109000cc1SGleb Smirnoff 			    hc_entry->hc_cwnd,
69209000cc1SGleb Smirnoff 			    hc_entry->hc_sendpipe,
69309000cc1SGleb Smirnoff 			    hc_entry->hc_recvpipe,
694489bde57SGleb Smirnoff #ifdef	TCP_HC_COUNTERS
69509000cc1SGleb Smirnoff 			    hc_entry->hc_hits,
69609000cc1SGleb Smirnoff 			    hc_entry->hc_updates,
697489bde57SGleb Smirnoff #endif
69809000cc1SGleb Smirnoff 			    hc_entry->hc_expire);
69997d8d152SAndre Oppermann 		}
7001a7fe55aSGleb Smirnoff 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i]);
70186988046SRichard Scheffenegger 		sbuf_drain(&sb);
7029aef4e7cSRichard Scheffenegger 	}
70397d8d152SAndre Oppermann #undef msec
704751ccc42SIan Lepore 	error = sbuf_finish(&sb);
705002d4558SJohn Baldwin 	sbuf_delete(&sb);
70697d8d152SAndre Oppermann 	return(error);
70797d8d152SAndre Oppermann }
70897d8d152SAndre Oppermann 
70997d8d152SAndre Oppermann /*
71002f26e98SRichard Scheffenegger  * Sysctl function: prints a histogram of the hostcache hashbucket
71102f26e98SRichard Scheffenegger  * utilization.
71202f26e98SRichard Scheffenegger  */
71302f26e98SRichard Scheffenegger static int
sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS)71402f26e98SRichard Scheffenegger sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS)
71502f26e98SRichard Scheffenegger {
71602f26e98SRichard Scheffenegger 	const int linesize = 50;
71702f26e98SRichard Scheffenegger 	struct sbuf sb;
71802f26e98SRichard Scheffenegger 	int i, error;
71902f26e98SRichard Scheffenegger 	int *histo;
72002f26e98SRichard Scheffenegger 	u_int hch_length;
72102f26e98SRichard Scheffenegger 
72202f26e98SRichard Scheffenegger 	if (jailed_without_vnet(curthread->td_ucred) != 0)
72302f26e98SRichard Scheffenegger 		return (EPERM);
72402f26e98SRichard Scheffenegger 
72502f26e98SRichard Scheffenegger 	histo = (int *)malloc(sizeof(int) * (V_tcp_hostcache.bucket_limit + 1),
72602f26e98SRichard Scheffenegger 			M_TEMP, M_NOWAIT|M_ZERO);
72702f26e98SRichard Scheffenegger 	if (histo == NULL)
72802f26e98SRichard Scheffenegger 		return(ENOMEM);
72902f26e98SRichard Scheffenegger 
73002f26e98SRichard Scheffenegger 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
73102f26e98SRichard Scheffenegger 		hch_length = V_tcp_hostcache.hashbase[i].hch_length;
73202f26e98SRichard Scheffenegger 		KASSERT(hch_length <= V_tcp_hostcache.bucket_limit,
73302f26e98SRichard Scheffenegger 		    ("tcp_hostcache: bucket limit exceeded at %u: %u",
73402f26e98SRichard Scheffenegger 		    i, hch_length));
73502f26e98SRichard Scheffenegger 		histo[hch_length]++;
73602f26e98SRichard Scheffenegger 	}
73702f26e98SRichard Scheffenegger 
73802f26e98SRichard Scheffenegger 	/* Use a buffer for 16 lines */
73902f26e98SRichard Scheffenegger 	sbuf_new_for_sysctl(&sb, NULL, 16 * linesize, req);
74002f26e98SRichard Scheffenegger 
74102f26e98SRichard Scheffenegger 	sbuf_printf(&sb, "\nLength\tCount\n");
74202f26e98SRichard Scheffenegger 	for (i = 0; i <= V_tcp_hostcache.bucket_limit; i++) {
74302f26e98SRichard Scheffenegger 		sbuf_printf(&sb, "%u\t%u\n", i, histo[i]);
74402f26e98SRichard Scheffenegger 	}
74502f26e98SRichard Scheffenegger 	error = sbuf_finish(&sb);
74602f26e98SRichard Scheffenegger 	sbuf_delete(&sb);
74702f26e98SRichard Scheffenegger 	free(histo, M_TEMP);
74802f26e98SRichard Scheffenegger 	return(error);
74902f26e98SRichard Scheffenegger }
75002f26e98SRichard Scheffenegger 
75102f26e98SRichard Scheffenegger /*
752fffb9f1dSBjoern A. Zeeb  * Caller has to make sure the curvnet is set properly.
75397d8d152SAndre Oppermann  */
75497d8d152SAndre Oppermann static void
tcp_hc_purge_internal(int all)755fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(int all)
75697d8d152SAndre Oppermann {
757d554522fSGleb Smirnoff 	struct hc_head *head;
758d554522fSGleb Smirnoff 	struct hc_metrics *hc_entry, *hc_next, *hc_prev;
75997d8d152SAndre Oppermann 	int i;
76097d8d152SAndre Oppermann 
761603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
762d554522fSGleb Smirnoff 		head = &V_tcp_hostcache.hashbase[i];
763d554522fSGleb Smirnoff 		hc_prev = NULL;
764d554522fSGleb Smirnoff 		THC_LOCK(head);
76509000cc1SGleb Smirnoff 		CK_SLIST_FOREACH_SAFE(hc_entry, &head->hch_bucket, hc_q,
766d554522fSGleb Smirnoff 		    hc_next) {
767d554522fSGleb Smirnoff 			KASSERT(head->hch_length > 0 && head->hch_length <=
7684f49e338SGleb Smirnoff 			    V_tcp_hostcache.bucket_limit, ("tcp_hostcache: "
769d554522fSGleb Smirnoff 			    "bucket length out of range at %u: %u", i,
770d554522fSGleb Smirnoff 			    head->hch_length));
771d554522fSGleb Smirnoff 			if (all ||
77209000cc1SGleb Smirnoff 			    atomic_load_int(&hc_entry->hc_expire) <= 0) {
773d554522fSGleb Smirnoff 				if (hc_prev != NULL) {
774d554522fSGleb Smirnoff 					KASSERT(hc_entry ==
77509000cc1SGleb Smirnoff 					    CK_SLIST_NEXT(hc_prev, hc_q),
776d554522fSGleb Smirnoff 					    ("%s: %p is not next to %p",
777d554522fSGleb Smirnoff 					    __func__, hc_entry, hc_prev));
77809000cc1SGleb Smirnoff 					CK_SLIST_REMOVE_AFTER(hc_prev, hc_q);
779d554522fSGleb Smirnoff 				} else {
780d554522fSGleb Smirnoff 					KASSERT(hc_entry ==
781d554522fSGleb Smirnoff 					    CK_SLIST_FIRST(&head->hch_bucket),
782d554522fSGleb Smirnoff 					    ("%s: %p is not first",
783d554522fSGleb Smirnoff 					    __func__, hc_entry));
784d554522fSGleb Smirnoff 					CK_SLIST_REMOVE_HEAD(&head->hch_bucket,
78509000cc1SGleb Smirnoff 					    hc_q);
78697d8d152SAndre Oppermann 				}
787d554522fSGleb Smirnoff 				uma_zfree_smr(V_tcp_hostcache.zone, hc_entry);
788d554522fSGleb Smirnoff 				head->hch_length--;
789d554522fSGleb Smirnoff 				atomic_subtract_int(&V_tcp_hostcache.cache_count, 1);
790d554522fSGleb Smirnoff 			} else {
79109000cc1SGleb Smirnoff 				atomic_subtract_int(&hc_entry->hc_expire,
792d554522fSGleb Smirnoff 				    V_tcp_hostcache.prune);
793d554522fSGleb Smirnoff 				hc_prev = hc_entry;
794d554522fSGleb Smirnoff 			}
795d554522fSGleb Smirnoff 		}
796d554522fSGleb Smirnoff 		THC_UNLOCK(head);
79797d8d152SAndre Oppermann 	}
798fffb9f1dSBjoern A. Zeeb }
799fffb9f1dSBjoern A. Zeeb 
800fffb9f1dSBjoern A. Zeeb /*
801fffb9f1dSBjoern A. Zeeb  * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
802fffb9f1dSBjoern A. Zeeb  * periodically from the callout.
803fffb9f1dSBjoern A. Zeeb  */
804fffb9f1dSBjoern A. Zeeb static void
tcp_hc_purge(void * arg)805fffb9f1dSBjoern A. Zeeb tcp_hc_purge(void *arg)
806fffb9f1dSBjoern A. Zeeb {
807fffb9f1dSBjoern A. Zeeb 	CURVNET_SET((struct vnet *) arg);
808fffb9f1dSBjoern A. Zeeb 	int all = 0;
809fffb9f1dSBjoern A. Zeeb 
810fffb9f1dSBjoern A. Zeeb 	if (V_tcp_hostcache.purgeall) {
811b878ec02SRichard Scheffenegger 		if (V_tcp_hostcache.purgeall == 2)
812b878ec02SRichard Scheffenegger 			V_tcp_hostcache.hashsalt = arc4random();
813fffb9f1dSBjoern A. Zeeb 		all = 1;
814fffb9f1dSBjoern A. Zeeb 		V_tcp_hostcache.purgeall = 0;
815fffb9f1dSBjoern A. Zeeb 	}
816fffb9f1dSBjoern A. Zeeb 
817fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(all);
818ac957cd2SJulian Elischer 
819ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
820ac957cd2SJulian Elischer 	    tcp_hc_purge, arg);
82121ca7b57SMarko Zec 	CURVNET_RESTORE();
82297d8d152SAndre Oppermann }
82326456380SHiren Panchasara 
82426456380SHiren Panchasara /*
82526456380SHiren Panchasara  * Expire and purge all entries in hostcache immediately.
82626456380SHiren Panchasara  */
82726456380SHiren Panchasara static int
sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS)82826456380SHiren Panchasara sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS)
82926456380SHiren Panchasara {
83026456380SHiren Panchasara 	int error, val;
83126456380SHiren Panchasara 
83226456380SHiren Panchasara 	val = 0;
83326456380SHiren Panchasara 	error = sysctl_handle_int(oidp, &val, 0, req);
83426456380SHiren Panchasara 	if (error || !req->newptr)
83526456380SHiren Panchasara 		return (error);
83626456380SHiren Panchasara 
837b878ec02SRichard Scheffenegger 	if (val == 2)
838b878ec02SRichard Scheffenegger 		V_tcp_hostcache.hashsalt = arc4random();
83926456380SHiren Panchasara 	tcp_hc_purge_internal(1);
84026456380SHiren Panchasara 
84126456380SHiren Panchasara 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
84226456380SHiren Panchasara 	    tcp_hc_purge, curvnet);
84326456380SHiren Panchasara 
84426456380SHiren Panchasara 	return (0);
84526456380SHiren Panchasara }
846