xref: /freebsd/sys/netinet/tcp_hostcache.c (revision 9aef4e7c2bd4371e526e4e3feb26064d361ad8ce)
1c398230bSWarner Losh /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3fe267a55SPedro F. Giffuni  *
497d8d152SAndre Oppermann  * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG
597d8d152SAndre Oppermann  * All rights reserved.
697d8d152SAndre Oppermann  *
797d8d152SAndre Oppermann  * Redistribution and use in source and binary forms, with or without
897d8d152SAndre Oppermann  * modification, are permitted provided that the following conditions
997d8d152SAndre Oppermann  * are met:
1097d8d152SAndre Oppermann  * 1. Redistributions of source code must retain the above copyright
1197d8d152SAndre Oppermann  *    notice, this list of conditions and the following disclaimer.
1297d8d152SAndre Oppermann  * 2. Redistributions in binary form must reproduce the above copyright
1397d8d152SAndre Oppermann  *    notice, this list of conditions and the following disclaimer in the
1497d8d152SAndre Oppermann  *    documentation and/or other materials provided with the distribution.
1597d8d152SAndre Oppermann  * 3. The name of the author may not be used to endorse or promote
1697d8d152SAndre Oppermann  *    products derived from this software without specific prior written
1797d8d152SAndre Oppermann  *    permission.
1897d8d152SAndre Oppermann  *
1997d8d152SAndre Oppermann  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2097d8d152SAndre Oppermann  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2197d8d152SAndre Oppermann  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2297d8d152SAndre Oppermann  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2397d8d152SAndre Oppermann  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2497d8d152SAndre Oppermann  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2597d8d152SAndre Oppermann  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2697d8d152SAndre Oppermann  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2797d8d152SAndre Oppermann  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2897d8d152SAndre Oppermann  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2997d8d152SAndre Oppermann  * SUCH DAMAGE.
3097d8d152SAndre Oppermann  */
3197d8d152SAndre Oppermann 
3297d8d152SAndre Oppermann /*
33e487a5e2SRobert Watson  * The tcp_hostcache moves the tcp-specific cached metrics from the routing
34e487a5e2SRobert Watson  * table to a dedicated structure indexed by the remote IP address.  It keeps
35e487a5e2SRobert Watson  * information on the measured TCP parameters of past TCP sessions to allow
36e487a5e2SRobert Watson  * better initial start values to be used with later connections to/from the
374d163382SHiren Panchasara  * same source.  Depending on the network parameters (delay, max MTU,
384d163382SHiren Panchasara  * congestion window) between local and remote sites, this can lead to
39e487a5e2SRobert Watson  * significant speed-ups for new TCP connections after the first one.
4097d8d152SAndre Oppermann  *
41e487a5e2SRobert Watson  * Due to the tcp_hostcache, all TCP-specific metrics information in the
42003c7e36SRui Paulo  * routing table have been removed.  The inpcb no longer keeps a pointer to
43e487a5e2SRobert Watson  * the routing entry, and protocol-initiated route cloning has been removed
44e487a5e2SRobert Watson  * as well.  With these changes, the routing table has gone back to being
45e487a5e2SRobert Watson  * more lightwight and only carries information related to packet forwarding.
4697d8d152SAndre Oppermann  *
47e487a5e2SRobert Watson  * tcp_hostcache is designed for multiple concurrent access in SMP
48e487a5e2SRobert Watson  * environments and high contention.  All bucket rows have their own lock and
49e487a5e2SRobert Watson  * thus multiple lookups and modifies can be done at the same time as long as
50e487a5e2SRobert Watson  * they are in different bucket rows.  If a request for insertion of a new
51e487a5e2SRobert Watson  * record can't be satisfied, it simply returns an empty structure.  Nobody
52e487a5e2SRobert Watson  * and nothing outside of tcp_hostcache.c will ever point directly to any
53e487a5e2SRobert Watson  * entry in the tcp_hostcache.  All communication is done in an
54e487a5e2SRobert Watson  * object-oriented way and only functions of tcp_hostcache will manipulate
55e487a5e2SRobert Watson  * hostcache entries.  Otherwise, we are unable to achieve good behaviour in
56e487a5e2SRobert Watson  * concurrent access situations.  Since tcp_hostcache is only caching
57e487a5e2SRobert Watson  * information, there are no fatal consequences if we either can't satisfy
58e487a5e2SRobert Watson  * any particular request or have to drop/overwrite an existing entry because
59e487a5e2SRobert Watson  * of bucket limit memory constrains.
6097d8d152SAndre Oppermann  */
6197d8d152SAndre Oppermann 
6297d8d152SAndre Oppermann /*
6397d8d152SAndre Oppermann  * Many thanks to jlemon for basic structure of tcp_syncache which is being
6497d8d152SAndre Oppermann  * followed here.
6597d8d152SAndre Oppermann  */
6697d8d152SAndre Oppermann 
674b421e2dSMike Silbersack #include <sys/cdefs.h>
684b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
694b421e2dSMike Silbersack 
7097d8d152SAndre Oppermann #include "opt_inet6.h"
7197d8d152SAndre Oppermann 
7297d8d152SAndre Oppermann #include <sys/param.h>
7397d8d152SAndre Oppermann #include <sys/systm.h>
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>
8197d8d152SAndre Oppermann #include <sys/socket.h>
8297d8d152SAndre Oppermann #include <sys/socketvar.h>
8397d8d152SAndre Oppermann #include <sys/sysctl.h>
8497d8d152SAndre Oppermann 
8597d8d152SAndre Oppermann #include <net/if.h>
8676039bc8SGleb Smirnoff #include <net/if_var.h>
875736e6fbSBjoern A. Zeeb #include <net/route.h>
88530c0060SRobert Watson #include <net/vnet.h>
8997d8d152SAndre Oppermann 
9097d8d152SAndre Oppermann #include <netinet/in.h>
9197d8d152SAndre Oppermann #include <netinet/in_systm.h>
9297d8d152SAndre Oppermann #include <netinet/ip.h>
9397d8d152SAndre Oppermann #include <netinet/in_var.h>
9497d8d152SAndre Oppermann #include <netinet/in_pcb.h>
9597d8d152SAndre Oppermann #include <netinet/ip_var.h>
9697d8d152SAndre Oppermann #ifdef INET6
9797d8d152SAndre Oppermann #include <netinet/ip6.h>
9897d8d152SAndre Oppermann #include <netinet6/ip6_var.h>
9997d8d152SAndre Oppermann #endif
10097d8d152SAndre Oppermann #include <netinet/tcp.h>
10197d8d152SAndre Oppermann #include <netinet/tcp_var.h>
1024b79449eSBjoern A. Zeeb #include <netinet/tcp_hostcache.h>
10397d8d152SAndre Oppermann #ifdef INET6
10497d8d152SAndre Oppermann #include <netinet6/tcp6_var.h>
10597d8d152SAndre Oppermann #endif
10697d8d152SAndre Oppermann 
10797d8d152SAndre Oppermann #include <vm/uma.h>
10897d8d152SAndre Oppermann 
10997d8d152SAndre Oppermann /* Arbitrary values */
11097d8d152SAndre Oppermann #define TCP_HOSTCACHE_HASHSIZE		512
11197d8d152SAndre Oppermann #define TCP_HOSTCACHE_BUCKETLIMIT	30
11297d8d152SAndre Oppermann #define TCP_HOSTCACHE_EXPIRE		60*60	/* one hour */
11397d8d152SAndre Oppermann #define TCP_HOSTCACHE_PRUNE		5*60	/* every 5 minutes */
11497d8d152SAndre Oppermann 
1155f901c92SAndrew Turner VNET_DEFINE_STATIC(struct tcp_hostcache, tcp_hostcache);
1161e77c105SRobert Watson #define	V_tcp_hostcache		VNET(tcp_hostcache)
11782cea7e6SBjoern A. Zeeb 
1185f901c92SAndrew Turner VNET_DEFINE_STATIC(struct callout, tcp_hc_callout);
1191e77c105SRobert Watson #define	V_tcp_hc_callout	VNET(tcp_hc_callout)
12097d8d152SAndre Oppermann 
12197d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
12297d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
12397d8d152SAndre Oppermann static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
12402f26e98SRichard Scheffenegger static int sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS);
12526456380SHiren Panchasara static int sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS);
126fffb9f1dSBjoern A. Zeeb static void tcp_hc_purge_internal(int);
12797d8d152SAndre Oppermann static void tcp_hc_purge(void *);
12897d8d152SAndre Oppermann 
1297029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache,
1307029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1310eb7cf4dSBrooks Davis     "TCP Host cache");
13297d8d152SAndre Oppermann 
1338a56c645SHiren Panchasara VNET_DEFINE(int, tcp_use_hostcache) = 1;
1348a56c645SHiren Panchasara #define V_tcp_use_hostcache  VNET(tcp_use_hostcache)
1358a56c645SHiren Panchasara SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
1368a56c645SHiren Panchasara     &VNET_NAME(tcp_use_hostcache), 0,
1378a56c645SHiren Panchasara     "Enable the TCP hostcache");
1388a56c645SHiren Panchasara 
1396df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN,
140eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.cache_limit), 0,
1418b615593SMarko Zec     "Overall entry limit for hostcache");
14297d8d152SAndre Oppermann 
1436df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN,
144eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.hashsize), 0,
1458b615593SMarko Zec     "Size of TCP hostcache hashtable");
14697d8d152SAndre Oppermann 
1476df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
1486df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0,
1498b615593SMarko Zec     "Per-bucket hash limit for hostcache");
15097d8d152SAndre Oppermann 
151529a2a0fSRichard Scheffenegger SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD,
152529a2a0fSRichard Scheffenegger      &VNET_NAME(tcp_hostcache.cache_count), 0,
1538b615593SMarko Zec     "Current number of entries in hostcache");
15497d8d152SAndre Oppermann 
1556df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW,
156eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.expire), 0,
1578b615593SMarko Zec     "Expire time of TCP hostcache entries");
15897d8d152SAndre Oppermann 
1596df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW,
160eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.prune), 0,
161eddfbb76SRobert Watson     "Time between purge runs");
162dba3c508SYaroslav Tykhiy 
1636df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW,
164eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.purgeall), 0,
1658b615593SMarko Zec     "Expire all entires on next purge run");
16697d8d152SAndre Oppermann 
16797d8d152SAndre Oppermann SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
1687029da5cSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE,
1697029da5cSPawel Biernacki     0, 0, sysctl_tcp_hc_list, "A",
1707029da5cSPawel Biernacki     "List of all hostcache entries");
17197d8d152SAndre Oppermann 
17202f26e98SRichard Scheffenegger SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, histo,
17302f26e98SRichard Scheffenegger     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE,
17402f26e98SRichard Scheffenegger     0, 0, sysctl_tcp_hc_histo, "A",
17502f26e98SRichard Scheffenegger     "Print a histogram of hostcache hashbucket utilization");
17602f26e98SRichard Scheffenegger 
17726456380SHiren Panchasara SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, purgenow,
1787029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1797029da5cSPawel Biernacki     NULL, 0, sysctl_tcp_hc_purgenow, "I",
1807029da5cSPawel Biernacki     "Immediately purge all entries");
18197d8d152SAndre Oppermann 
18297d8d152SAndre Oppermann static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
18397d8d152SAndre Oppermann 
18497d8d152SAndre Oppermann #define HOSTCACHE_HASH(ip) \
18597d8d152SAndre Oppermann 	(((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) &	\
186603724d3SBjoern A. Zeeb 	  V_tcp_hostcache.hashmask)
18797d8d152SAndre Oppermann 
18897d8d152SAndre Oppermann /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
18997d8d152SAndre Oppermann #define HOSTCACHE_HASH6(ip6)				\
19097d8d152SAndre Oppermann 	(((ip6)->s6_addr32[0] ^				\
19197d8d152SAndre Oppermann 	  (ip6)->s6_addr32[1] ^				\
19297d8d152SAndre Oppermann 	  (ip6)->s6_addr32[2] ^				\
19397d8d152SAndre Oppermann 	  (ip6)->s6_addr32[3]) &			\
194603724d3SBjoern A. Zeeb 	 V_tcp_hostcache.hashmask)
19597d8d152SAndre Oppermann 
19697d8d152SAndre Oppermann #define THC_LOCK(lp)		mtx_lock(lp)
19797d8d152SAndre Oppermann #define THC_UNLOCK(lp)		mtx_unlock(lp)
19897d8d152SAndre Oppermann 
19997d8d152SAndre Oppermann void
20097d8d152SAndre Oppermann tcp_hc_init(void)
20197d8d152SAndre Oppermann {
20232fe38f1SAndrey Zonov 	u_int cache_limit;
20397d8d152SAndre Oppermann 	int i;
20497d8d152SAndre Oppermann 
20597d8d152SAndre Oppermann 	/*
206e487a5e2SRobert Watson 	 * Initialize hostcache structures.
20797d8d152SAndre Oppermann 	 */
208529a2a0fSRichard Scheffenegger 	atomic_store_int(&V_tcp_hostcache.cache_count, 0);
209603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
210603724d3SBjoern A. Zeeb 	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
211603724d3SBjoern A. Zeeb 	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
212603724d3SBjoern A. Zeeb 	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
21397d8d152SAndre Oppermann 
21497d8d152SAndre Oppermann 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
215603724d3SBjoern A. Zeeb 	    &V_tcp_hostcache.hashsize);
216603724d3SBjoern A. Zeeb 	if (!powerof2(V_tcp_hostcache.hashsize)) {
21797d8d152SAndre Oppermann 		printf("WARNING: hostcache hash size is not a power of 2.\n");
218603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
21997d8d152SAndre Oppermann 	}
220603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
22197d8d152SAndre Oppermann 
22232fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
22332fe38f1SAndrey Zonov 	    &V_tcp_hostcache.bucket_limit);
22432fe38f1SAndrey Zonov 
22532fe38f1SAndrey Zonov 	cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
22632fe38f1SAndrey Zonov 	V_tcp_hostcache.cache_limit = cache_limit;
22732fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
22832fe38f1SAndrey Zonov 	    &V_tcp_hostcache.cache_limit);
22932fe38f1SAndrey Zonov 	if (V_tcp_hostcache.cache_limit > cache_limit)
23032fe38f1SAndrey Zonov 		V_tcp_hostcache.cache_limit = cache_limit;
23132fe38f1SAndrey Zonov 
23297d8d152SAndre Oppermann 	/*
233e487a5e2SRobert Watson 	 * Allocate the hash table.
23497d8d152SAndre Oppermann 	 */
235603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase = (struct hc_head *)
236603724d3SBjoern A. Zeeb 	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
23797d8d152SAndre Oppermann 		   M_HOSTCACHE, M_WAITOK | M_ZERO);
23897d8d152SAndre Oppermann 
23997d8d152SAndre Oppermann 	/*
240e487a5e2SRobert Watson 	 * Initialize the hash buckets.
24197d8d152SAndre Oppermann 	 */
242603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
243603724d3SBjoern A. Zeeb 		TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
244603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[i].hch_length = 0;
245603724d3SBjoern A. Zeeb 		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
24697d8d152SAndre Oppermann 			  NULL, MTX_DEF);
24797d8d152SAndre Oppermann 	}
24897d8d152SAndre Oppermann 
24997d8d152SAndre Oppermann 	/*
25097d8d152SAndre Oppermann 	 * Allocate the hostcache entries.
25197d8d152SAndre Oppermann 	 */
252ac957cd2SJulian Elischer 	V_tcp_hostcache.zone =
253ac957cd2SJulian Elischer 	    uma_zcreate("hostcache", sizeof(struct hc_metrics),
2544efb805cSAndre Oppermann 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
255603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
25697d8d152SAndre Oppermann 
25797d8d152SAndre Oppermann 	/*
25897d8d152SAndre Oppermann 	 * Set up periodic cache cleanup.
25997d8d152SAndre Oppermann 	 */
260fd90e2edSJung-uk Kim 	callout_init(&V_tcp_hc_callout, 1);
261ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
26221ca7b57SMarko Zec 	    tcp_hc_purge, curvnet);
26397d8d152SAndre Oppermann }
26497d8d152SAndre Oppermann 
265bc29160dSMarko Zec #ifdef VIMAGE
266bc29160dSMarko Zec void
267bc29160dSMarko Zec tcp_hc_destroy(void)
268bc29160dSMarko Zec {
269fffb9f1dSBjoern A. Zeeb 	int i;
270bc29160dSMarko Zec 
271bc29160dSMarko Zec 	callout_drain(&V_tcp_hc_callout);
272fffb9f1dSBjoern A. Zeeb 
273fffb9f1dSBjoern A. Zeeb 	/* Purge all hc entries. */
274fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(1);
275fffb9f1dSBjoern A. Zeeb 
276fffb9f1dSBjoern A. Zeeb 	/* Free the uma zone and the allocated hash table. */
277fffb9f1dSBjoern A. Zeeb 	uma_zdestroy(V_tcp_hostcache.zone);
278fffb9f1dSBjoern A. Zeeb 
279fffb9f1dSBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++)
280fffb9f1dSBjoern A. Zeeb 		mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx);
281fffb9f1dSBjoern A. Zeeb 	free(V_tcp_hostcache.hashbase, M_HOSTCACHE);
282bc29160dSMarko Zec }
283bc29160dSMarko Zec #endif
284bc29160dSMarko Zec 
28597d8d152SAndre Oppermann /*
28697d8d152SAndre Oppermann  * Internal function: look up an entry in the hostcache or return NULL.
28797d8d152SAndre Oppermann  *
28897d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
28997d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
29097d8d152SAndre Oppermann  */
29197d8d152SAndre Oppermann static struct hc_metrics *
29297d8d152SAndre Oppermann tcp_hc_lookup(struct in_conninfo *inc)
29397d8d152SAndre Oppermann {
29497d8d152SAndre Oppermann 	int hash;
29597d8d152SAndre Oppermann 	struct hc_head *hc_head;
29697d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
29797d8d152SAndre Oppermann 
2988a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
2998a56c645SHiren Panchasara 		return NULL;
3008a56c645SHiren Panchasara 
30197d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
30297d8d152SAndre Oppermann 
30397d8d152SAndre Oppermann 	/*
30497d8d152SAndre Oppermann 	 * Hash the foreign ip address.
30597d8d152SAndre Oppermann 	 */
306dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
30797d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
30897d8d152SAndre Oppermann 	else
30997d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
31097d8d152SAndre Oppermann 
311603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
31297d8d152SAndre Oppermann 
31397d8d152SAndre Oppermann 	/*
314e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
315e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
316e487a5e2SRobert Watson 	 * done.
31797d8d152SAndre Oppermann 	 */
31897d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
31997d8d152SAndre Oppermann 
32097d8d152SAndre Oppermann 	/*
321e487a5e2SRobert Watson 	 * Iterate through entries in bucket row looking for a match.
32297d8d152SAndre Oppermann 	 */
32397d8d152SAndre Oppermann 	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
324dcdb4371SBjoern A. Zeeb 		if (inc->inc_flags & INC_ISIPV6) {
325028bdf28SAndrey V. Elsukov 			/* XXX: check ip6_zoneid */
32697d8d152SAndre Oppermann 			if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
32797d8d152SAndre Oppermann 			    sizeof(inc->inc6_faddr)) == 0)
32897d8d152SAndre Oppermann 				return hc_entry;
32997d8d152SAndre Oppermann 		} else {
33097d8d152SAndre Oppermann 			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
33197d8d152SAndre Oppermann 			    sizeof(inc->inc_faddr)) == 0)
33297d8d152SAndre Oppermann 				return hc_entry;
33397d8d152SAndre Oppermann 		}
33497d8d152SAndre Oppermann 	}
33597d8d152SAndre Oppermann 
33697d8d152SAndre Oppermann 	/*
337e487a5e2SRobert Watson 	 * We were unsuccessful and didn't find anything.
33897d8d152SAndre Oppermann 	 */
33997d8d152SAndre Oppermann 	THC_UNLOCK(&hc_head->hch_mtx);
34097d8d152SAndre Oppermann 	return NULL;
34197d8d152SAndre Oppermann }
34297d8d152SAndre Oppermann 
34397d8d152SAndre Oppermann /*
344e487a5e2SRobert Watson  * Internal function: insert an entry into the hostcache or return NULL if
345e487a5e2SRobert Watson  * unable to allocate a new one.
34697d8d152SAndre Oppermann  *
34797d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
34897d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
34997d8d152SAndre Oppermann  */
35097d8d152SAndre Oppermann static struct hc_metrics *
35197d8d152SAndre Oppermann tcp_hc_insert(struct in_conninfo *inc)
35297d8d152SAndre Oppermann {
35397d8d152SAndre Oppermann 	int hash;
35497d8d152SAndre Oppermann 	struct hc_head *hc_head;
35597d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
35697d8d152SAndre Oppermann 
3578a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
3588a56c645SHiren Panchasara 		return NULL;
3598a56c645SHiren Panchasara 
36097d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
36197d8d152SAndre Oppermann 
36297d8d152SAndre Oppermann 	/*
363e487a5e2SRobert Watson 	 * Hash the foreign ip address.
36497d8d152SAndre Oppermann 	 */
365dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
36697d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
36797d8d152SAndre Oppermann 	else
36897d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
36997d8d152SAndre Oppermann 
370603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
37197d8d152SAndre Oppermann 
37297d8d152SAndre Oppermann 	/*
373e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
374e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
375e487a5e2SRobert Watson 	 * done.
37697d8d152SAndre Oppermann 	 */
37797d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
37897d8d152SAndre Oppermann 
37997d8d152SAndre Oppermann 	/*
380e487a5e2SRobert Watson 	 * If the bucket limit is reached, reuse the least-used element.
38197d8d152SAndre Oppermann 	 */
382603724d3SBjoern A. Zeeb 	if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
383529a2a0fSRichard Scheffenegger 	    atomic_load_int(&V_tcp_hostcache.cache_count) >= V_tcp_hostcache.cache_limit) {
38497d8d152SAndre Oppermann 		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
38597d8d152SAndre Oppermann 		/*
38697d8d152SAndre Oppermann 		 * At first we were dropping the last element, just to
387e487a5e2SRobert Watson 		 * reacquire it in the next two lines again, which isn't very
388e487a5e2SRobert Watson 		 * efficient.  Instead just reuse the least used element.
389e487a5e2SRobert Watson 		 * We may drop something that is still "in-use" but we can be
390e487a5e2SRobert Watson 		 * "lossy".
39145024be0SAndre Oppermann 		 * Just give up if this bucket row is empty and we don't have
39245024be0SAndre Oppermann 		 * anything to replace.
39397d8d152SAndre Oppermann 		 */
39445024be0SAndre Oppermann 		if (hc_entry == NULL) {
39545024be0SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
39645024be0SAndre Oppermann 			return NULL;
39745024be0SAndre Oppermann 		}
39897d8d152SAndre Oppermann 		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
39902f26e98SRichard Scheffenegger 		KASSERT(V_tcp_hostcache.hashbase[hash].hch_length > 0 &&
40002f26e98SRichard Scheffenegger 			V_tcp_hostcache.hashbase[hash].hch_length <=
40102f26e98SRichard Scheffenegger 			V_tcp_hostcache.bucket_limit,
40202f26e98SRichard Scheffenegger 			("tcp_hostcache: bucket length range violated at %u: %u",
40302f26e98SRichard Scheffenegger 			hash, V_tcp_hostcache.hashbase[hash].hch_length));
404603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[hash].hch_length--;
405529a2a0fSRichard Scheffenegger 		atomic_subtract_int(&V_tcp_hostcache.cache_count, 1);
40678b50714SRobert Watson 		TCPSTAT_INC(tcps_hc_bucketoverflow);
407cd6c4060SAndre Oppermann #if 0
408603724d3SBjoern A. Zeeb 		uma_zfree(V_tcp_hostcache.zone, hc_entry);
409cd6c4060SAndre Oppermann #endif
41097d8d152SAndre Oppermann 	} else {
41197d8d152SAndre Oppermann 		/*
412e487a5e2SRobert Watson 		 * Allocate a new entry, or balk if not possible.
41397d8d152SAndre Oppermann 		 */
414603724d3SBjoern A. Zeeb 		hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
41597d8d152SAndre Oppermann 		if (hc_entry == NULL) {
41697d8d152SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
41797d8d152SAndre Oppermann 			return NULL;
41897d8d152SAndre Oppermann 		}
41997d8d152SAndre Oppermann 	}
42097d8d152SAndre Oppermann 
42197d8d152SAndre Oppermann 	/*
422e487a5e2SRobert Watson 	 * Initialize basic information of hostcache entry.
42397d8d152SAndre Oppermann 	 */
42497d8d152SAndre Oppermann 	bzero(hc_entry, sizeof(*hc_entry));
425028bdf28SAndrey V. Elsukov 	if (inc->inc_flags & INC_ISIPV6) {
426028bdf28SAndrey V. Elsukov 		hc_entry->ip6 = inc->inc6_faddr;
427028bdf28SAndrey V. Elsukov 		hc_entry->ip6_zoneid = inc->inc6_zoneid;
428028bdf28SAndrey V. Elsukov 	} else
42997d8d152SAndre Oppermann 		hc_entry->ip4 = inc->inc_faddr;
43097d8d152SAndre Oppermann 	hc_entry->rmx_head = hc_head;
431603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire;
43297d8d152SAndre Oppermann 
43397d8d152SAndre Oppermann 	/*
434e487a5e2SRobert Watson 	 * Put it upfront.
43597d8d152SAndre Oppermann 	 */
43697d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
437603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase[hash].hch_length++;
43802f26e98SRichard Scheffenegger 	KASSERT(V_tcp_hostcache.hashbase[hash].hch_length <
43902f26e98SRichard Scheffenegger 		V_tcp_hostcache.bucket_limit,
44002f26e98SRichard Scheffenegger 		("tcp_hostcache: bucket length too high at %u: %u",
44102f26e98SRichard Scheffenegger 		hash, V_tcp_hostcache.hashbase[hash].hch_length));
442529a2a0fSRichard Scheffenegger 	atomic_add_int(&V_tcp_hostcache.cache_count, 1);
44378b50714SRobert Watson 	TCPSTAT_INC(tcps_hc_added);
44497d8d152SAndre Oppermann 
44597d8d152SAndre Oppermann 	return hc_entry;
44697d8d152SAndre Oppermann }
44797d8d152SAndre Oppermann 
44897d8d152SAndre Oppermann /*
44997d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and fill out the
450e487a5e2SRobert Watson  * supplied TCP metrics structure.  Fills in NULL when no entry was found or
451e487a5e2SRobert Watson  * a value is not set.
45297d8d152SAndre Oppermann  */
45397d8d152SAndre Oppermann void
45497d8d152SAndre Oppermann tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
45597d8d152SAndre Oppermann {
45697d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
45797d8d152SAndre Oppermann 
4586b8fba3cSMichael Tuexen 	if (!V_tcp_use_hostcache) {
4596b8fba3cSMichael Tuexen 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
4608a56c645SHiren Panchasara 		return;
4616b8fba3cSMichael Tuexen 	}
4628a56c645SHiren Panchasara 
46397d8d152SAndre Oppermann 	/*
464e487a5e2SRobert Watson 	 * Find the right bucket.
46597d8d152SAndre Oppermann 	 */
46697d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
46797d8d152SAndre Oppermann 
46897d8d152SAndre Oppermann 	/*
469e487a5e2SRobert Watson 	 * If we don't have an existing object.
47097d8d152SAndre Oppermann 	 */
47197d8d152SAndre Oppermann 	if (hc_entry == NULL) {
47297d8d152SAndre Oppermann 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
47397d8d152SAndre Oppermann 		return;
47497d8d152SAndre Oppermann 	}
47597d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
476603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
47797d8d152SAndre Oppermann 
47897d8d152SAndre Oppermann 	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
47997d8d152SAndre Oppermann 	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
48097d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
48197d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
48297d8d152SAndre Oppermann 	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
48397d8d152SAndre Oppermann 	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
48497d8d152SAndre Oppermann 	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
48597d8d152SAndre Oppermann 
48697d8d152SAndre Oppermann 	/*
487e487a5e2SRobert Watson 	 * Unlock bucket row.
48897d8d152SAndre Oppermann 	 */
48997d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
49097d8d152SAndre Oppermann }
49197d8d152SAndre Oppermann 
49297d8d152SAndre Oppermann /*
49397d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and return the
4948a56c645SHiren Panchasara  * discovered path MTU.  Returns 0 if no entry is found or value is not
4956fbed4afSRobert Watson  * set.
49697d8d152SAndre Oppermann  */
4973ac12506SJonathan T. Looney uint32_t
49897d8d152SAndre Oppermann tcp_hc_getmtu(struct in_conninfo *inc)
49997d8d152SAndre Oppermann {
50097d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
5013ac12506SJonathan T. Looney 	uint32_t mtu;
50297d8d152SAndre Oppermann 
5038a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
5048a56c645SHiren Panchasara 		return 0;
5058a56c645SHiren Panchasara 
50697d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
50797d8d152SAndre Oppermann 	if (hc_entry == NULL) {
50897d8d152SAndre Oppermann 		return 0;
50997d8d152SAndre Oppermann 	}
51097d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
511603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
51297d8d152SAndre Oppermann 
51397d8d152SAndre Oppermann 	mtu = hc_entry->rmx_mtu;
51497d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
51597d8d152SAndre Oppermann 	return mtu;
51697d8d152SAndre Oppermann }
51797d8d152SAndre Oppermann 
51897d8d152SAndre Oppermann /*
519e487a5e2SRobert Watson  * External function: update the MTU value of an entry in the hostcache.
52097d8d152SAndre Oppermann  * Creates a new entry if none was found.
52197d8d152SAndre Oppermann  */
52297d8d152SAndre Oppermann void
5233ac12506SJonathan T. Looney tcp_hc_updatemtu(struct in_conninfo *inc, uint32_t mtu)
52497d8d152SAndre Oppermann {
52597d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
52697d8d152SAndre Oppermann 
5278a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
5288a56c645SHiren Panchasara 		return;
5298a56c645SHiren Panchasara 
53097d8d152SAndre Oppermann 	/*
531e487a5e2SRobert Watson 	 * Find the right bucket.
53297d8d152SAndre Oppermann 	 */
53397d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
53497d8d152SAndre Oppermann 
53597d8d152SAndre Oppermann 	/*
536e487a5e2SRobert Watson 	 * If we don't have an existing object, try to insert a new one.
53797d8d152SAndre Oppermann 	 */
53897d8d152SAndre Oppermann 	if (hc_entry == NULL) {
53997d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
54097d8d152SAndre Oppermann 		if (hc_entry == NULL)
54197d8d152SAndre Oppermann 			return;
54297d8d152SAndre Oppermann 	}
54397d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
544603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
54597d8d152SAndre Oppermann 
54697d8d152SAndre Oppermann 	hc_entry->rmx_mtu = mtu;
54797d8d152SAndre Oppermann 
54897d8d152SAndre Oppermann 	/*
549e487a5e2SRobert Watson 	 * Put it upfront so we find it faster next time.
55097d8d152SAndre Oppermann 	 */
55197d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
55297d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
55397d8d152SAndre Oppermann 
55497d8d152SAndre Oppermann 	/*
555e487a5e2SRobert Watson 	 * Unlock bucket row.
55697d8d152SAndre Oppermann 	 */
55797d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
55897d8d152SAndre Oppermann }
55997d8d152SAndre Oppermann 
56097d8d152SAndre Oppermann /*
561e487a5e2SRobert Watson  * External function: update the TCP metrics of an entry in the hostcache.
56297d8d152SAndre Oppermann  * Creates a new entry if none was found.
56397d8d152SAndre Oppermann  */
56497d8d152SAndre Oppermann void
56597d8d152SAndre Oppermann tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
56697d8d152SAndre Oppermann {
56797d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
56897d8d152SAndre Oppermann 
5698a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
5708a56c645SHiren Panchasara 		return;
5718a56c645SHiren Panchasara 
57297d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
57397d8d152SAndre Oppermann 	if (hc_entry == NULL) {
57497d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
57597d8d152SAndre Oppermann 		if (hc_entry == NULL)
57697d8d152SAndre Oppermann 			return;
57797d8d152SAndre Oppermann 	}
57897d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
579603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
58097d8d152SAndre Oppermann 
58197d8d152SAndre Oppermann 	if (hcml->rmx_rtt != 0) {
58297d8d152SAndre Oppermann 		if (hc_entry->rmx_rtt == 0)
58397d8d152SAndre Oppermann 			hc_entry->rmx_rtt = hcml->rmx_rtt;
58497d8d152SAndre Oppermann 		else
5853ac12506SJonathan T. Looney 			hc_entry->rmx_rtt = ((uint64_t)hc_entry->rmx_rtt +
5863ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_rtt) / 2;
58778b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrtt);
58897d8d152SAndre Oppermann 	}
58997d8d152SAndre Oppermann 	if (hcml->rmx_rttvar != 0) {
59097d8d152SAndre Oppermann 	        if (hc_entry->rmx_rttvar == 0)
59197d8d152SAndre Oppermann 			hc_entry->rmx_rttvar = hcml->rmx_rttvar;
59297d8d152SAndre Oppermann 		else
5933ac12506SJonathan T. Looney 			hc_entry->rmx_rttvar = ((uint64_t)hc_entry->rmx_rttvar +
5943ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_rttvar) / 2;
59578b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrttvar);
59697d8d152SAndre Oppermann 	}
59797d8d152SAndre Oppermann 	if (hcml->rmx_ssthresh != 0) {
59897d8d152SAndre Oppermann 		if (hc_entry->rmx_ssthresh == 0)
59997d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
60097d8d152SAndre Oppermann 		else
60197d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh =
60297d8d152SAndre Oppermann 			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
60378b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedssthresh);
60497d8d152SAndre Oppermann 	}
60597d8d152SAndre Oppermann 	if (hcml->rmx_cwnd != 0) {
60697d8d152SAndre Oppermann 		if (hc_entry->rmx_cwnd == 0)
60797d8d152SAndre Oppermann 			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
60897d8d152SAndre Oppermann 		else
6093ac12506SJonathan T. Looney 			hc_entry->rmx_cwnd = ((uint64_t)hc_entry->rmx_cwnd +
6103ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_cwnd) / 2;
61178b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedcwnd); */
61297d8d152SAndre Oppermann 	}
61397d8d152SAndre Oppermann 	if (hcml->rmx_sendpipe != 0) {
61497d8d152SAndre Oppermann 		if (hc_entry->rmx_sendpipe == 0)
61597d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
61697d8d152SAndre Oppermann 		else
61797d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe =
6183ac12506SJonathan T. Looney 			    ((uint64_t)hc_entry->rmx_sendpipe +
6193ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_sendpipe) /2;
62078b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedsendpipe); */
62197d8d152SAndre Oppermann 	}
62297d8d152SAndre Oppermann 	if (hcml->rmx_recvpipe != 0) {
62397d8d152SAndre Oppermann 		if (hc_entry->rmx_recvpipe == 0)
62497d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
62597d8d152SAndre Oppermann 		else
62697d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe =
6273ac12506SJonathan T. Looney 			    ((uint64_t)hc_entry->rmx_recvpipe +
6283ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_recvpipe) /2;
62978b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedrecvpipe); */
63097d8d152SAndre Oppermann 	}
63197d8d152SAndre Oppermann 
63297d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
63397d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
63497d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
63597d8d152SAndre Oppermann }
63697d8d152SAndre Oppermann 
63797d8d152SAndre Oppermann /*
63897d8d152SAndre Oppermann  * Sysctl function: prints the list and values of all hostcache entries in
63997d8d152SAndre Oppermann  * unsorted order.
64097d8d152SAndre Oppermann  */
64197d8d152SAndre Oppermann static int
64297d8d152SAndre Oppermann sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
64397d8d152SAndre Oppermann {
644751ccc42SIan Lepore 	const int linesize = 128;
645002d4558SJohn Baldwin 	struct sbuf sb;
646cb0dd7e1SRichard Scheffenegger 	int i, error, len;
647*9aef4e7cSRichard Scheffenegger 	bool do_drain = false;
64897d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
6498144690aSEric van Gyzen 	char ip4buf[INET_ADDRSTRLEN];
6501d54aa3bSBjoern A. Zeeb #ifdef INET6
6511d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
6521d54aa3bSBjoern A. Zeeb #endif
65397d8d152SAndre Oppermann 
654b8a2fb91SHiren Panchasara 	if (jailed_without_vnet(curthread->td_ucred) != 0)
655b8a2fb91SHiren Panchasara 		return (EPERM);
656b8a2fb91SHiren Panchasara 
657cb0dd7e1SRichard Scheffenegger 	/* Optimize Buffer length query by sbin/sysctl */
658cb0dd7e1SRichard Scheffenegger 	if (req->oldptr == NULL) {
659529a2a0fSRichard Scheffenegger 		len = (atomic_load_int(&V_tcp_hostcache.cache_count) + 1) *
660529a2a0fSRichard Scheffenegger 			linesize;
661cb0dd7e1SRichard Scheffenegger 		return (SYSCTL_OUT(req, NULL, len));
662cb0dd7e1SRichard Scheffenegger 	}
663cb0dd7e1SRichard Scheffenegger 
664cb0dd7e1SRichard Scheffenegger 	error = sysctl_wire_old_buffer(req, 0);
665cb0dd7e1SRichard Scheffenegger 	if (error != 0) {
666cb0dd7e1SRichard Scheffenegger 		return(error);
667cb0dd7e1SRichard Scheffenegger 	}
668cb0dd7e1SRichard Scheffenegger 
66986988046SRichard Scheffenegger 	/* Use a buffer sized for one full bucket */
670529a2a0fSRichard Scheffenegger 	sbuf_new_for_sysctl(&sb, NULL, V_tcp_hostcache.bucket_limit *
671529a2a0fSRichard Scheffenegger 		linesize, req);
67297d8d152SAndre Oppermann 
673002d4558SJohn Baldwin 	sbuf_printf(&sb,
6744d163382SHiren Panchasara 		"\nIP address        MTU  SSTRESH      RTT   RTTVAR "
67597d8d152SAndre Oppermann 		"    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
67686988046SRichard Scheffenegger 	sbuf_drain(&sb);
67797d8d152SAndre Oppermann 
67897d8d152SAndre Oppermann #define msec(u) (((u) + 500) / 1000)
679603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
680603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
681603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
68297d8d152SAndre Oppermann 			      rmx_q) {
683002d4558SJohn Baldwin 			sbuf_printf(&sb,
6843ac12506SJonathan T. Looney 			    "%-15s %5u %8u %6lums %6lums %8u %8u %8u %4lu "
6854d163382SHiren Panchasara 			    "%4lu %4i\n",
6868144690aSEric van Gyzen 			    hc_entry->ip4.s_addr ?
6878144690aSEric van Gyzen 			        inet_ntoa_r(hc_entry->ip4, ip4buf) :
68897d8d152SAndre Oppermann #ifdef INET6
6891d54aa3bSBjoern A. Zeeb 				ip6_sprintf(ip6buf, &hc_entry->ip6),
69097d8d152SAndre Oppermann #else
69197d8d152SAndre Oppermann 				"IPv6?",
69297d8d152SAndre Oppermann #endif
69397d8d152SAndre Oppermann 			    hc_entry->rmx_mtu,
69497d8d152SAndre Oppermann 			    hc_entry->rmx_ssthresh,
6953ac12506SJonathan T. Looney 			    msec((u_long)hc_entry->rmx_rtt *
69697d8d152SAndre Oppermann 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
6973ac12506SJonathan T. Looney 			    msec((u_long)hc_entry->rmx_rttvar *
6983a288e90SMikolaj Golub 				(RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))),
69997d8d152SAndre Oppermann 			    hc_entry->rmx_cwnd,
70097d8d152SAndre Oppermann 			    hc_entry->rmx_sendpipe,
70197d8d152SAndre Oppermann 			    hc_entry->rmx_recvpipe,
70297d8d152SAndre Oppermann 			    hc_entry->rmx_hits,
70397d8d152SAndre Oppermann 			    hc_entry->rmx_updates,
70497d8d152SAndre Oppermann 			    hc_entry->rmx_expire);
705*9aef4e7cSRichard Scheffenegger 			do_drain = true;
70697d8d152SAndre Oppermann 		}
707603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
708*9aef4e7cSRichard Scheffenegger 		/* Need to track if sbuf has data, to avoid
709*9aef4e7cSRichard Scheffenegger 		 * a KASSERT when calling sbuf_drain.
710*9aef4e7cSRichard Scheffenegger 		 */
711*9aef4e7cSRichard Scheffenegger 		if (do_drain) {
71286988046SRichard Scheffenegger 			sbuf_drain(&sb);
713*9aef4e7cSRichard Scheffenegger 			do_drain = false;
714*9aef4e7cSRichard Scheffenegger 		}
71597d8d152SAndre Oppermann 	}
71697d8d152SAndre Oppermann #undef msec
717751ccc42SIan Lepore 	error = sbuf_finish(&sb);
718002d4558SJohn Baldwin 	sbuf_delete(&sb);
71997d8d152SAndre Oppermann 	return(error);
72097d8d152SAndre Oppermann }
72197d8d152SAndre Oppermann 
72297d8d152SAndre Oppermann /*
72302f26e98SRichard Scheffenegger  * Sysctl function: prints a histogram of the hostcache hashbucket
72402f26e98SRichard Scheffenegger  * utilization.
72502f26e98SRichard Scheffenegger  */
72602f26e98SRichard Scheffenegger static int
72702f26e98SRichard Scheffenegger sysctl_tcp_hc_histo(SYSCTL_HANDLER_ARGS)
72802f26e98SRichard Scheffenegger {
72902f26e98SRichard Scheffenegger 	const int linesize = 50;
73002f26e98SRichard Scheffenegger 	struct sbuf sb;
73102f26e98SRichard Scheffenegger 	int i, error;
73202f26e98SRichard Scheffenegger 	int *histo;
73302f26e98SRichard Scheffenegger 	u_int hch_length;
73402f26e98SRichard Scheffenegger 
73502f26e98SRichard Scheffenegger 	if (jailed_without_vnet(curthread->td_ucred) != 0)
73602f26e98SRichard Scheffenegger 		return (EPERM);
73702f26e98SRichard Scheffenegger 
73802f26e98SRichard Scheffenegger 	histo = (int *)malloc(sizeof(int) * (V_tcp_hostcache.bucket_limit + 1),
73902f26e98SRichard Scheffenegger 			M_TEMP, M_NOWAIT|M_ZERO);
74002f26e98SRichard Scheffenegger 	if (histo == NULL)
74102f26e98SRichard Scheffenegger 		return(ENOMEM);
74202f26e98SRichard Scheffenegger 
74302f26e98SRichard Scheffenegger 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
74402f26e98SRichard Scheffenegger 		hch_length = V_tcp_hostcache.hashbase[i].hch_length;
74502f26e98SRichard Scheffenegger 		KASSERT(hch_length <= V_tcp_hostcache.bucket_limit,
74602f26e98SRichard Scheffenegger 			("tcp_hostcache: bucket limit exceeded at %u: %u",
74702f26e98SRichard Scheffenegger 			i, hch_length));
74802f26e98SRichard Scheffenegger 		histo[hch_length]++;
74902f26e98SRichard Scheffenegger 	}
75002f26e98SRichard Scheffenegger 
75102f26e98SRichard Scheffenegger 	/* Use a buffer for 16 lines */
75202f26e98SRichard Scheffenegger 	sbuf_new_for_sysctl(&sb, NULL, 16 * linesize, req);
75302f26e98SRichard Scheffenegger 
75402f26e98SRichard Scheffenegger 	sbuf_printf(&sb, "\nLength\tCount\n");
75502f26e98SRichard Scheffenegger 	for (i = 0; i <= V_tcp_hostcache.bucket_limit; i++) {
75602f26e98SRichard Scheffenegger 		sbuf_printf(&sb, "%u\t%u\n", i, histo[i]);
75702f26e98SRichard Scheffenegger 	}
75802f26e98SRichard Scheffenegger 	error = sbuf_finish(&sb);
75902f26e98SRichard Scheffenegger 	sbuf_delete(&sb);
76002f26e98SRichard Scheffenegger 	free(histo, M_TEMP);
76102f26e98SRichard Scheffenegger 	return(error);
76202f26e98SRichard Scheffenegger }
76302f26e98SRichard Scheffenegger 
76402f26e98SRichard Scheffenegger /*
765fffb9f1dSBjoern A. Zeeb  * Caller has to make sure the curvnet is set properly.
76697d8d152SAndre Oppermann  */
76797d8d152SAndre Oppermann static void
768fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(int all)
76997d8d152SAndre Oppermann {
770b62dccc7SAndre Oppermann 	struct hc_metrics *hc_entry, *hc_next;
77197d8d152SAndre Oppermann 	int i;
77297d8d152SAndre Oppermann 
773603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
774603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
775ac957cd2SJulian Elischer 		TAILQ_FOREACH_SAFE(hc_entry,
7765ed3800eSJulian Elischer 		    &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
77702f26e98SRichard Scheffenegger 			KASSERT(V_tcp_hostcache.hashbase[i].hch_length > 0 &&
77802f26e98SRichard Scheffenegger 				V_tcp_hostcache.hashbase[i].hch_length <=
77902f26e98SRichard Scheffenegger 				V_tcp_hostcache.bucket_limit,
780*9aef4e7cSRichard Scheffenegger 				("tcp_hostcache: bucket length out of range at %u: %u",
78102f26e98SRichard Scheffenegger 				i, V_tcp_hostcache.hashbase[i].hch_length));
78297d8d152SAndre Oppermann 			if (all || hc_entry->rmx_expire <= 0) {
783603724d3SBjoern A. Zeeb 				TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
78497d8d152SAndre Oppermann 					      hc_entry, rmx_q);
785603724d3SBjoern A. Zeeb 				uma_zfree(V_tcp_hostcache.zone, hc_entry);
786603724d3SBjoern A. Zeeb 				V_tcp_hostcache.hashbase[i].hch_length--;
787529a2a0fSRichard Scheffenegger 				atomic_subtract_int(&V_tcp_hostcache.cache_count, 1);
78897d8d152SAndre Oppermann 			} else
789603724d3SBjoern A. Zeeb 				hc_entry->rmx_expire -= V_tcp_hostcache.prune;
79097d8d152SAndre Oppermann 		}
791603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
79297d8d152SAndre Oppermann 	}
793fffb9f1dSBjoern A. Zeeb }
794fffb9f1dSBjoern A. Zeeb 
795fffb9f1dSBjoern A. Zeeb /*
796fffb9f1dSBjoern A. Zeeb  * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
797fffb9f1dSBjoern A. Zeeb  * periodically from the callout.
798fffb9f1dSBjoern A. Zeeb  */
799fffb9f1dSBjoern A. Zeeb static void
800fffb9f1dSBjoern A. Zeeb tcp_hc_purge(void *arg)
801fffb9f1dSBjoern A. Zeeb {
802fffb9f1dSBjoern A. Zeeb 	CURVNET_SET((struct vnet *) arg);
803fffb9f1dSBjoern A. Zeeb 	int all = 0;
804fffb9f1dSBjoern A. Zeeb 
805fffb9f1dSBjoern A. Zeeb 	if (V_tcp_hostcache.purgeall) {
806fffb9f1dSBjoern A. Zeeb 		all = 1;
807fffb9f1dSBjoern A. Zeeb 		V_tcp_hostcache.purgeall = 0;
808fffb9f1dSBjoern A. Zeeb 	}
809fffb9f1dSBjoern A. Zeeb 
810fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(all);
811ac957cd2SJulian Elischer 
812ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
813ac957cd2SJulian Elischer 	    tcp_hc_purge, arg);
81421ca7b57SMarko Zec 	CURVNET_RESTORE();
81597d8d152SAndre Oppermann }
81626456380SHiren Panchasara 
81726456380SHiren Panchasara /*
81826456380SHiren Panchasara  * Expire and purge all entries in hostcache immediately.
81926456380SHiren Panchasara  */
82026456380SHiren Panchasara static int
82126456380SHiren Panchasara sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS)
82226456380SHiren Panchasara {
82326456380SHiren Panchasara 	int error, val;
82426456380SHiren Panchasara 
82526456380SHiren Panchasara 	val = 0;
82626456380SHiren Panchasara 	error = sysctl_handle_int(oidp, &val, 0, req);
82726456380SHiren Panchasara 	if (error || !req->newptr)
82826456380SHiren Panchasara 		return (error);
82926456380SHiren Panchasara 
83026456380SHiren Panchasara 	tcp_hc_purge_internal(1);
83126456380SHiren Panchasara 
83226456380SHiren Panchasara 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
83326456380SHiren Panchasara 	    tcp_hc_purge, curvnet);
83426456380SHiren Panchasara 
83526456380SHiren Panchasara 	return (0);
83626456380SHiren Panchasara }
837