xref: /freebsd/sys/netinet/tcp_hostcache.c (revision 529a2a0f2765f6c57c50a5af6be242c03bf714e3)
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);
12426456380SHiren Panchasara static int sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS);
125fffb9f1dSBjoern A. Zeeb static void tcp_hc_purge_internal(int);
12697d8d152SAndre Oppermann static void tcp_hc_purge(void *);
12797d8d152SAndre Oppermann 
1287029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache,
1297029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1300eb7cf4dSBrooks Davis     "TCP Host cache");
13197d8d152SAndre Oppermann 
1328a56c645SHiren Panchasara VNET_DEFINE(int, tcp_use_hostcache) = 1;
1338a56c645SHiren Panchasara #define V_tcp_use_hostcache  VNET(tcp_use_hostcache)
1348a56c645SHiren Panchasara SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
1358a56c645SHiren Panchasara     &VNET_NAME(tcp_use_hostcache), 0,
1368a56c645SHiren Panchasara     "Enable the TCP hostcache");
1378a56c645SHiren Panchasara 
1386df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN,
139eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.cache_limit), 0,
1408b615593SMarko Zec     "Overall entry limit for hostcache");
14197d8d152SAndre Oppermann 
1426df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN,
143eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.hashsize), 0,
1448b615593SMarko Zec     "Size of TCP hostcache hashtable");
14597d8d152SAndre Oppermann 
1466df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
1476df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0,
1488b615593SMarko Zec     "Per-bucket hash limit for hostcache");
14997d8d152SAndre Oppermann 
150*529a2a0fSRichard Scheffenegger SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD,
151*529a2a0fSRichard Scheffenegger      &VNET_NAME(tcp_hostcache.cache_count), 0,
1528b615593SMarko Zec     "Current number of entries in hostcache");
15397d8d152SAndre Oppermann 
1546df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW,
155eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.expire), 0,
1568b615593SMarko Zec     "Expire time of TCP hostcache entries");
15797d8d152SAndre Oppermann 
1586df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW,
159eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.prune), 0,
160eddfbb76SRobert Watson     "Time between purge runs");
161dba3c508SYaroslav Tykhiy 
1626df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW,
163eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.purgeall), 0,
1648b615593SMarko Zec     "Expire all entires on next purge run");
16597d8d152SAndre Oppermann 
16697d8d152SAndre Oppermann SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
1677029da5cSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE,
1687029da5cSPawel Biernacki     0, 0, sysctl_tcp_hc_list, "A",
1697029da5cSPawel Biernacki     "List of all hostcache entries");
17097d8d152SAndre Oppermann 
17126456380SHiren Panchasara SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, purgenow,
1727029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1737029da5cSPawel Biernacki     NULL, 0, sysctl_tcp_hc_purgenow, "I",
1747029da5cSPawel Biernacki     "Immediately purge all entries");
17597d8d152SAndre Oppermann 
17697d8d152SAndre Oppermann static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
17797d8d152SAndre Oppermann 
17897d8d152SAndre Oppermann #define HOSTCACHE_HASH(ip) \
17997d8d152SAndre Oppermann 	(((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) &	\
180603724d3SBjoern A. Zeeb 	  V_tcp_hostcache.hashmask)
18197d8d152SAndre Oppermann 
18297d8d152SAndre Oppermann /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
18397d8d152SAndre Oppermann #define HOSTCACHE_HASH6(ip6)				\
18497d8d152SAndre Oppermann 	(((ip6)->s6_addr32[0] ^				\
18597d8d152SAndre Oppermann 	  (ip6)->s6_addr32[1] ^				\
18697d8d152SAndre Oppermann 	  (ip6)->s6_addr32[2] ^				\
18797d8d152SAndre Oppermann 	  (ip6)->s6_addr32[3]) &			\
188603724d3SBjoern A. Zeeb 	 V_tcp_hostcache.hashmask)
18997d8d152SAndre Oppermann 
19097d8d152SAndre Oppermann #define THC_LOCK(lp)		mtx_lock(lp)
19197d8d152SAndre Oppermann #define THC_UNLOCK(lp)		mtx_unlock(lp)
19297d8d152SAndre Oppermann 
19397d8d152SAndre Oppermann void
19497d8d152SAndre Oppermann tcp_hc_init(void)
19597d8d152SAndre Oppermann {
19632fe38f1SAndrey Zonov 	u_int cache_limit;
19797d8d152SAndre Oppermann 	int i;
19897d8d152SAndre Oppermann 
19997d8d152SAndre Oppermann 	/*
200e487a5e2SRobert Watson 	 * Initialize hostcache structures.
20197d8d152SAndre Oppermann 	 */
202*529a2a0fSRichard Scheffenegger 	atomic_store_int(&V_tcp_hostcache.cache_count, 0);
203603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
204603724d3SBjoern A. Zeeb 	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
205603724d3SBjoern A. Zeeb 	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
206603724d3SBjoern A. Zeeb 	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
20797d8d152SAndre Oppermann 
20897d8d152SAndre Oppermann 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
209603724d3SBjoern A. Zeeb 	    &V_tcp_hostcache.hashsize);
210603724d3SBjoern A. Zeeb 	if (!powerof2(V_tcp_hostcache.hashsize)) {
21197d8d152SAndre Oppermann 		printf("WARNING: hostcache hash size is not a power of 2.\n");
212603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
21397d8d152SAndre Oppermann 	}
214603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
21597d8d152SAndre Oppermann 
21632fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
21732fe38f1SAndrey Zonov 	    &V_tcp_hostcache.bucket_limit);
21832fe38f1SAndrey Zonov 
21932fe38f1SAndrey Zonov 	cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
22032fe38f1SAndrey Zonov 	V_tcp_hostcache.cache_limit = cache_limit;
22132fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
22232fe38f1SAndrey Zonov 	    &V_tcp_hostcache.cache_limit);
22332fe38f1SAndrey Zonov 	if (V_tcp_hostcache.cache_limit > cache_limit)
22432fe38f1SAndrey Zonov 		V_tcp_hostcache.cache_limit = cache_limit;
22532fe38f1SAndrey Zonov 
22697d8d152SAndre Oppermann 	/*
227e487a5e2SRobert Watson 	 * Allocate the hash table.
22897d8d152SAndre Oppermann 	 */
229603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase = (struct hc_head *)
230603724d3SBjoern A. Zeeb 	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
23197d8d152SAndre Oppermann 		   M_HOSTCACHE, M_WAITOK | M_ZERO);
23297d8d152SAndre Oppermann 
23397d8d152SAndre Oppermann 	/*
234e487a5e2SRobert Watson 	 * Initialize the hash buckets.
23597d8d152SAndre Oppermann 	 */
236603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
237603724d3SBjoern A. Zeeb 		TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
238603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[i].hch_length = 0;
239603724d3SBjoern A. Zeeb 		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
24097d8d152SAndre Oppermann 			  NULL, MTX_DEF);
24197d8d152SAndre Oppermann 	}
24297d8d152SAndre Oppermann 
24397d8d152SAndre Oppermann 	/*
24497d8d152SAndre Oppermann 	 * Allocate the hostcache entries.
24597d8d152SAndre Oppermann 	 */
246ac957cd2SJulian Elischer 	V_tcp_hostcache.zone =
247ac957cd2SJulian Elischer 	    uma_zcreate("hostcache", sizeof(struct hc_metrics),
2484efb805cSAndre Oppermann 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
249603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
25097d8d152SAndre Oppermann 
25197d8d152SAndre Oppermann 	/*
25297d8d152SAndre Oppermann 	 * Set up periodic cache cleanup.
25397d8d152SAndre Oppermann 	 */
254fd90e2edSJung-uk Kim 	callout_init(&V_tcp_hc_callout, 1);
255ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
25621ca7b57SMarko Zec 	    tcp_hc_purge, curvnet);
25797d8d152SAndre Oppermann }
25897d8d152SAndre Oppermann 
259bc29160dSMarko Zec #ifdef VIMAGE
260bc29160dSMarko Zec void
261bc29160dSMarko Zec tcp_hc_destroy(void)
262bc29160dSMarko Zec {
263fffb9f1dSBjoern A. Zeeb 	int i;
264bc29160dSMarko Zec 
265bc29160dSMarko Zec 	callout_drain(&V_tcp_hc_callout);
266fffb9f1dSBjoern A. Zeeb 
267fffb9f1dSBjoern A. Zeeb 	/* Purge all hc entries. */
268fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(1);
269fffb9f1dSBjoern A. Zeeb 
270fffb9f1dSBjoern A. Zeeb 	/* Free the uma zone and the allocated hash table. */
271fffb9f1dSBjoern A. Zeeb 	uma_zdestroy(V_tcp_hostcache.zone);
272fffb9f1dSBjoern A. Zeeb 
273fffb9f1dSBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++)
274fffb9f1dSBjoern A. Zeeb 		mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx);
275fffb9f1dSBjoern A. Zeeb 	free(V_tcp_hostcache.hashbase, M_HOSTCACHE);
276bc29160dSMarko Zec }
277bc29160dSMarko Zec #endif
278bc29160dSMarko Zec 
27997d8d152SAndre Oppermann /*
28097d8d152SAndre Oppermann  * Internal function: look up an entry in the hostcache or return NULL.
28197d8d152SAndre Oppermann  *
28297d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
28397d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
28497d8d152SAndre Oppermann  */
28597d8d152SAndre Oppermann static struct hc_metrics *
28697d8d152SAndre Oppermann tcp_hc_lookup(struct in_conninfo *inc)
28797d8d152SAndre Oppermann {
28897d8d152SAndre Oppermann 	int hash;
28997d8d152SAndre Oppermann 	struct hc_head *hc_head;
29097d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
29197d8d152SAndre Oppermann 
2928a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
2938a56c645SHiren Panchasara 		return NULL;
2948a56c645SHiren Panchasara 
29597d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
29697d8d152SAndre Oppermann 
29797d8d152SAndre Oppermann 	/*
29897d8d152SAndre Oppermann 	 * Hash the foreign ip address.
29997d8d152SAndre Oppermann 	 */
300dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
30197d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
30297d8d152SAndre Oppermann 	else
30397d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
30497d8d152SAndre Oppermann 
305603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
30697d8d152SAndre Oppermann 
30797d8d152SAndre Oppermann 	/*
308e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
309e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
310e487a5e2SRobert Watson 	 * done.
31197d8d152SAndre Oppermann 	 */
31297d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
31397d8d152SAndre Oppermann 
31497d8d152SAndre Oppermann 	/*
315e487a5e2SRobert Watson 	 * Iterate through entries in bucket row looking for a match.
31697d8d152SAndre Oppermann 	 */
31797d8d152SAndre Oppermann 	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
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)
32297d8d152SAndre Oppermann 				return hc_entry;
32397d8d152SAndre Oppermann 		} else {
32497d8d152SAndre Oppermann 			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
32597d8d152SAndre Oppermann 			    sizeof(inc->inc_faddr)) == 0)
32697d8d152SAndre Oppermann 				return hc_entry;
32797d8d152SAndre Oppermann 		}
32897d8d152SAndre Oppermann 	}
32997d8d152SAndre Oppermann 
33097d8d152SAndre Oppermann 	/*
331e487a5e2SRobert Watson 	 * We were unsuccessful and didn't find anything.
33297d8d152SAndre Oppermann 	 */
33397d8d152SAndre Oppermann 	THC_UNLOCK(&hc_head->hch_mtx);
33497d8d152SAndre Oppermann 	return NULL;
33597d8d152SAndre Oppermann }
33697d8d152SAndre Oppermann 
33797d8d152SAndre Oppermann /*
338e487a5e2SRobert Watson  * Internal function: insert an entry into the hostcache or return NULL if
339e487a5e2SRobert Watson  * unable to allocate a new one.
34097d8d152SAndre Oppermann  *
34197d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
34297d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
34397d8d152SAndre Oppermann  */
34497d8d152SAndre Oppermann static struct hc_metrics *
34597d8d152SAndre Oppermann tcp_hc_insert(struct in_conninfo *inc)
34697d8d152SAndre Oppermann {
34797d8d152SAndre Oppermann 	int hash;
34897d8d152SAndre Oppermann 	struct hc_head *hc_head;
34997d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
35097d8d152SAndre Oppermann 
3518a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
3528a56c645SHiren Panchasara 		return NULL;
3538a56c645SHiren Panchasara 
35497d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
35597d8d152SAndre Oppermann 
35697d8d152SAndre Oppermann 	/*
357e487a5e2SRobert Watson 	 * Hash the foreign ip address.
35897d8d152SAndre Oppermann 	 */
359dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
36097d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
36197d8d152SAndre Oppermann 	else
36297d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
36397d8d152SAndre Oppermann 
364603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
36597d8d152SAndre Oppermann 
36697d8d152SAndre Oppermann 	/*
367e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
368e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
369e487a5e2SRobert Watson 	 * done.
37097d8d152SAndre Oppermann 	 */
37197d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
37297d8d152SAndre Oppermann 
37397d8d152SAndre Oppermann 	/*
374e487a5e2SRobert Watson 	 * If the bucket limit is reached, reuse the least-used element.
37597d8d152SAndre Oppermann 	 */
376603724d3SBjoern A. Zeeb 	if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
377*529a2a0fSRichard Scheffenegger 	    atomic_load_int(&V_tcp_hostcache.cache_count) >= V_tcp_hostcache.cache_limit) {
37897d8d152SAndre Oppermann 		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
37997d8d152SAndre Oppermann 		/*
38097d8d152SAndre Oppermann 		 * At first we were dropping the last element, just to
381e487a5e2SRobert Watson 		 * reacquire it in the next two lines again, which isn't very
382e487a5e2SRobert Watson 		 * efficient.  Instead just reuse the least used element.
383e487a5e2SRobert Watson 		 * We may drop something that is still "in-use" but we can be
384e487a5e2SRobert Watson 		 * "lossy".
38545024be0SAndre Oppermann 		 * Just give up if this bucket row is empty and we don't have
38645024be0SAndre Oppermann 		 * anything to replace.
38797d8d152SAndre Oppermann 		 */
38845024be0SAndre Oppermann 		if (hc_entry == NULL) {
38945024be0SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
39045024be0SAndre Oppermann 			return NULL;
39145024be0SAndre Oppermann 		}
39297d8d152SAndre Oppermann 		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
393603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[hash].hch_length--;
394*529a2a0fSRichard Scheffenegger 		atomic_subtract_int(&V_tcp_hostcache.cache_count, 1);
39578b50714SRobert Watson 		TCPSTAT_INC(tcps_hc_bucketoverflow);
396cd6c4060SAndre Oppermann #if 0
397603724d3SBjoern A. Zeeb 		uma_zfree(V_tcp_hostcache.zone, hc_entry);
398cd6c4060SAndre Oppermann #endif
39997d8d152SAndre Oppermann 	} else {
40097d8d152SAndre Oppermann 		/*
401e487a5e2SRobert Watson 		 * Allocate a new entry, or balk if not possible.
40297d8d152SAndre Oppermann 		 */
403603724d3SBjoern A. Zeeb 		hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
40497d8d152SAndre Oppermann 		if (hc_entry == NULL) {
40597d8d152SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
40697d8d152SAndre Oppermann 			return NULL;
40797d8d152SAndre Oppermann 		}
40897d8d152SAndre Oppermann 	}
40997d8d152SAndre Oppermann 
41097d8d152SAndre Oppermann 	/*
411e487a5e2SRobert Watson 	 * Initialize basic information of hostcache entry.
41297d8d152SAndre Oppermann 	 */
41397d8d152SAndre Oppermann 	bzero(hc_entry, sizeof(*hc_entry));
414028bdf28SAndrey V. Elsukov 	if (inc->inc_flags & INC_ISIPV6) {
415028bdf28SAndrey V. Elsukov 		hc_entry->ip6 = inc->inc6_faddr;
416028bdf28SAndrey V. Elsukov 		hc_entry->ip6_zoneid = inc->inc6_zoneid;
417028bdf28SAndrey V. Elsukov 	} else
41897d8d152SAndre Oppermann 		hc_entry->ip4 = inc->inc_faddr;
41997d8d152SAndre Oppermann 	hc_entry->rmx_head = hc_head;
420603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire;
42197d8d152SAndre Oppermann 
42297d8d152SAndre Oppermann 	/*
423e487a5e2SRobert Watson 	 * Put it upfront.
42497d8d152SAndre Oppermann 	 */
42597d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
426603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase[hash].hch_length++;
427*529a2a0fSRichard Scheffenegger 	atomic_add_int(&V_tcp_hostcache.cache_count, 1);
42878b50714SRobert Watson 	TCPSTAT_INC(tcps_hc_added);
42997d8d152SAndre Oppermann 
43097d8d152SAndre Oppermann 	return hc_entry;
43197d8d152SAndre Oppermann }
43297d8d152SAndre Oppermann 
43397d8d152SAndre Oppermann /*
43497d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and fill out the
435e487a5e2SRobert Watson  * supplied TCP metrics structure.  Fills in NULL when no entry was found or
436e487a5e2SRobert Watson  * a value is not set.
43797d8d152SAndre Oppermann  */
43897d8d152SAndre Oppermann void
43997d8d152SAndre Oppermann tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
44097d8d152SAndre Oppermann {
44197d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
44297d8d152SAndre Oppermann 
4436b8fba3cSMichael Tuexen 	if (!V_tcp_use_hostcache) {
4446b8fba3cSMichael Tuexen 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
4458a56c645SHiren Panchasara 		return;
4466b8fba3cSMichael Tuexen 	}
4478a56c645SHiren Panchasara 
44897d8d152SAndre Oppermann 	/*
449e487a5e2SRobert Watson 	 * Find the right bucket.
45097d8d152SAndre Oppermann 	 */
45197d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
45297d8d152SAndre Oppermann 
45397d8d152SAndre Oppermann 	/*
454e487a5e2SRobert Watson 	 * If we don't have an existing object.
45597d8d152SAndre Oppermann 	 */
45697d8d152SAndre Oppermann 	if (hc_entry == NULL) {
45797d8d152SAndre Oppermann 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
45897d8d152SAndre Oppermann 		return;
45997d8d152SAndre Oppermann 	}
46097d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
461603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
46297d8d152SAndre Oppermann 
46397d8d152SAndre Oppermann 	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
46497d8d152SAndre Oppermann 	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
46597d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
46697d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
46797d8d152SAndre Oppermann 	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
46897d8d152SAndre Oppermann 	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
46997d8d152SAndre Oppermann 	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
47097d8d152SAndre Oppermann 
47197d8d152SAndre Oppermann 	/*
472e487a5e2SRobert Watson 	 * Unlock bucket row.
47397d8d152SAndre Oppermann 	 */
47497d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
47597d8d152SAndre Oppermann }
47697d8d152SAndre Oppermann 
47797d8d152SAndre Oppermann /*
47897d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and return the
4798a56c645SHiren Panchasara  * discovered path MTU.  Returns 0 if no entry is found or value is not
4806fbed4afSRobert Watson  * set.
48197d8d152SAndre Oppermann  */
4823ac12506SJonathan T. Looney uint32_t
48397d8d152SAndre Oppermann tcp_hc_getmtu(struct in_conninfo *inc)
48497d8d152SAndre Oppermann {
48597d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
4863ac12506SJonathan T. Looney 	uint32_t mtu;
48797d8d152SAndre Oppermann 
4888a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
4898a56c645SHiren Panchasara 		return 0;
4908a56c645SHiren Panchasara 
49197d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
49297d8d152SAndre Oppermann 	if (hc_entry == NULL) {
49397d8d152SAndre Oppermann 		return 0;
49497d8d152SAndre Oppermann 	}
49597d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
496603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
49797d8d152SAndre Oppermann 
49897d8d152SAndre Oppermann 	mtu = hc_entry->rmx_mtu;
49997d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
50097d8d152SAndre Oppermann 	return mtu;
50197d8d152SAndre Oppermann }
50297d8d152SAndre Oppermann 
50397d8d152SAndre Oppermann /*
504e487a5e2SRobert Watson  * External function: update the MTU value of an entry in the hostcache.
50597d8d152SAndre Oppermann  * Creates a new entry if none was found.
50697d8d152SAndre Oppermann  */
50797d8d152SAndre Oppermann void
5083ac12506SJonathan T. Looney tcp_hc_updatemtu(struct in_conninfo *inc, uint32_t mtu)
50997d8d152SAndre Oppermann {
51097d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
51197d8d152SAndre Oppermann 
5128a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
5138a56c645SHiren Panchasara 		return;
5148a56c645SHiren Panchasara 
51597d8d152SAndre Oppermann 	/*
516e487a5e2SRobert Watson 	 * Find the right bucket.
51797d8d152SAndre Oppermann 	 */
51897d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
51997d8d152SAndre Oppermann 
52097d8d152SAndre Oppermann 	/*
521e487a5e2SRobert Watson 	 * If we don't have an existing object, try to insert a new one.
52297d8d152SAndre Oppermann 	 */
52397d8d152SAndre Oppermann 	if (hc_entry == NULL) {
52497d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
52597d8d152SAndre Oppermann 		if (hc_entry == NULL)
52697d8d152SAndre Oppermann 			return;
52797d8d152SAndre Oppermann 	}
52897d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
529603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
53097d8d152SAndre Oppermann 
53197d8d152SAndre Oppermann 	hc_entry->rmx_mtu = mtu;
53297d8d152SAndre Oppermann 
53397d8d152SAndre Oppermann 	/*
534e487a5e2SRobert Watson 	 * Put it upfront so we find it faster next time.
53597d8d152SAndre Oppermann 	 */
53697d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
53797d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
53897d8d152SAndre Oppermann 
53997d8d152SAndre Oppermann 	/*
540e487a5e2SRobert Watson 	 * Unlock bucket row.
54197d8d152SAndre Oppermann 	 */
54297d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
54397d8d152SAndre Oppermann }
54497d8d152SAndre Oppermann 
54597d8d152SAndre Oppermann /*
546e487a5e2SRobert Watson  * External function: update the TCP metrics of an entry in the hostcache.
54797d8d152SAndre Oppermann  * Creates a new entry if none was found.
54897d8d152SAndre Oppermann  */
54997d8d152SAndre Oppermann void
55097d8d152SAndre Oppermann tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
55197d8d152SAndre Oppermann {
55297d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
55397d8d152SAndre Oppermann 
5548a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
5558a56c645SHiren Panchasara 		return;
5568a56c645SHiren Panchasara 
55797d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
55897d8d152SAndre Oppermann 	if (hc_entry == NULL) {
55997d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
56097d8d152SAndre Oppermann 		if (hc_entry == NULL)
56197d8d152SAndre Oppermann 			return;
56297d8d152SAndre Oppermann 	}
56397d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
564603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
56597d8d152SAndre Oppermann 
56697d8d152SAndre Oppermann 	if (hcml->rmx_rtt != 0) {
56797d8d152SAndre Oppermann 		if (hc_entry->rmx_rtt == 0)
56897d8d152SAndre Oppermann 			hc_entry->rmx_rtt = hcml->rmx_rtt;
56997d8d152SAndre Oppermann 		else
5703ac12506SJonathan T. Looney 			hc_entry->rmx_rtt = ((uint64_t)hc_entry->rmx_rtt +
5713ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_rtt) / 2;
57278b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrtt);
57397d8d152SAndre Oppermann 	}
57497d8d152SAndre Oppermann 	if (hcml->rmx_rttvar != 0) {
57597d8d152SAndre Oppermann 	        if (hc_entry->rmx_rttvar == 0)
57697d8d152SAndre Oppermann 			hc_entry->rmx_rttvar = hcml->rmx_rttvar;
57797d8d152SAndre Oppermann 		else
5783ac12506SJonathan T. Looney 			hc_entry->rmx_rttvar = ((uint64_t)hc_entry->rmx_rttvar +
5793ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_rttvar) / 2;
58078b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrttvar);
58197d8d152SAndre Oppermann 	}
58297d8d152SAndre Oppermann 	if (hcml->rmx_ssthresh != 0) {
58397d8d152SAndre Oppermann 		if (hc_entry->rmx_ssthresh == 0)
58497d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
58597d8d152SAndre Oppermann 		else
58697d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh =
58797d8d152SAndre Oppermann 			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
58878b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedssthresh);
58997d8d152SAndre Oppermann 	}
59097d8d152SAndre Oppermann 	if (hcml->rmx_cwnd != 0) {
59197d8d152SAndre Oppermann 		if (hc_entry->rmx_cwnd == 0)
59297d8d152SAndre Oppermann 			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
59397d8d152SAndre Oppermann 		else
5943ac12506SJonathan T. Looney 			hc_entry->rmx_cwnd = ((uint64_t)hc_entry->rmx_cwnd +
5953ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_cwnd) / 2;
59678b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedcwnd); */
59797d8d152SAndre Oppermann 	}
59897d8d152SAndre Oppermann 	if (hcml->rmx_sendpipe != 0) {
59997d8d152SAndre Oppermann 		if (hc_entry->rmx_sendpipe == 0)
60097d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
60197d8d152SAndre Oppermann 		else
60297d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe =
6033ac12506SJonathan T. Looney 			    ((uint64_t)hc_entry->rmx_sendpipe +
6043ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_sendpipe) /2;
60578b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedsendpipe); */
60697d8d152SAndre Oppermann 	}
60797d8d152SAndre Oppermann 	if (hcml->rmx_recvpipe != 0) {
60897d8d152SAndre Oppermann 		if (hc_entry->rmx_recvpipe == 0)
60997d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
61097d8d152SAndre Oppermann 		else
61197d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe =
6123ac12506SJonathan T. Looney 			    ((uint64_t)hc_entry->rmx_recvpipe +
6133ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_recvpipe) /2;
61478b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedrecvpipe); */
61597d8d152SAndre Oppermann 	}
61697d8d152SAndre Oppermann 
61797d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
61897d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
61997d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
62097d8d152SAndre Oppermann }
62197d8d152SAndre Oppermann 
62297d8d152SAndre Oppermann /*
62397d8d152SAndre Oppermann  * Sysctl function: prints the list and values of all hostcache entries in
62497d8d152SAndre Oppermann  * unsorted order.
62597d8d152SAndre Oppermann  */
62697d8d152SAndre Oppermann static int
62797d8d152SAndre Oppermann sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
62897d8d152SAndre Oppermann {
629751ccc42SIan Lepore 	const int linesize = 128;
630002d4558SJohn Baldwin 	struct sbuf sb;
631cb0dd7e1SRichard Scheffenegger 	int i, error, len;
63297d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
6338144690aSEric van Gyzen 	char ip4buf[INET_ADDRSTRLEN];
6341d54aa3bSBjoern A. Zeeb #ifdef INET6
6351d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
6361d54aa3bSBjoern A. Zeeb #endif
63797d8d152SAndre Oppermann 
638b8a2fb91SHiren Panchasara 	if (jailed_without_vnet(curthread->td_ucred) != 0)
639b8a2fb91SHiren Panchasara 		return (EPERM);
640b8a2fb91SHiren Panchasara 
641cb0dd7e1SRichard Scheffenegger 	/* Optimize Buffer length query by sbin/sysctl */
642cb0dd7e1SRichard Scheffenegger 	if (req->oldptr == NULL) {
643*529a2a0fSRichard Scheffenegger 		len = (atomic_load_int(&V_tcp_hostcache.cache_count) + 1) *
644*529a2a0fSRichard Scheffenegger 			linesize;
645cb0dd7e1SRichard Scheffenegger 		return (SYSCTL_OUT(req, NULL, len));
646cb0dd7e1SRichard Scheffenegger 	}
647cb0dd7e1SRichard Scheffenegger 
648cb0dd7e1SRichard Scheffenegger 	error = sysctl_wire_old_buffer(req, 0);
649cb0dd7e1SRichard Scheffenegger 	if (error != 0) {
650cb0dd7e1SRichard Scheffenegger 		return(error);
651cb0dd7e1SRichard Scheffenegger 	}
652cb0dd7e1SRichard Scheffenegger 
65386988046SRichard Scheffenegger 	/* Use a buffer sized for one full bucket */
654*529a2a0fSRichard Scheffenegger 	sbuf_new_for_sysctl(&sb, NULL, V_tcp_hostcache.bucket_limit *
655*529a2a0fSRichard Scheffenegger 		linesize, req);
65697d8d152SAndre Oppermann 
657002d4558SJohn Baldwin 	sbuf_printf(&sb,
6584d163382SHiren Panchasara 		"\nIP address        MTU  SSTRESH      RTT   RTTVAR "
65997d8d152SAndre Oppermann 		"    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
66086988046SRichard Scheffenegger 	sbuf_drain(&sb);
66197d8d152SAndre Oppermann 
66297d8d152SAndre Oppermann #define msec(u) (((u) + 500) / 1000)
663603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
664603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
665603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
66697d8d152SAndre Oppermann 			      rmx_q) {
667002d4558SJohn Baldwin 			sbuf_printf(&sb,
6683ac12506SJonathan T. Looney 			    "%-15s %5u %8u %6lums %6lums %8u %8u %8u %4lu "
6694d163382SHiren Panchasara 			    "%4lu %4i\n",
6708144690aSEric van Gyzen 			    hc_entry->ip4.s_addr ?
6718144690aSEric van Gyzen 			        inet_ntoa_r(hc_entry->ip4, ip4buf) :
67297d8d152SAndre Oppermann #ifdef INET6
6731d54aa3bSBjoern A. Zeeb 				ip6_sprintf(ip6buf, &hc_entry->ip6),
67497d8d152SAndre Oppermann #else
67597d8d152SAndre Oppermann 				"IPv6?",
67697d8d152SAndre Oppermann #endif
67797d8d152SAndre Oppermann 			    hc_entry->rmx_mtu,
67897d8d152SAndre Oppermann 			    hc_entry->rmx_ssthresh,
6793ac12506SJonathan T. Looney 			    msec((u_long)hc_entry->rmx_rtt *
68097d8d152SAndre Oppermann 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
6813ac12506SJonathan T. Looney 			    msec((u_long)hc_entry->rmx_rttvar *
6823a288e90SMikolaj Golub 				(RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))),
68397d8d152SAndre Oppermann 			    hc_entry->rmx_cwnd,
68497d8d152SAndre Oppermann 			    hc_entry->rmx_sendpipe,
68597d8d152SAndre Oppermann 			    hc_entry->rmx_recvpipe,
68697d8d152SAndre Oppermann 			    hc_entry->rmx_hits,
68797d8d152SAndre Oppermann 			    hc_entry->rmx_updates,
68897d8d152SAndre Oppermann 			    hc_entry->rmx_expire);
68997d8d152SAndre Oppermann 		}
690603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
69186988046SRichard Scheffenegger 		sbuf_drain(&sb);
69297d8d152SAndre Oppermann 	}
69397d8d152SAndre Oppermann #undef msec
694751ccc42SIan Lepore 	error = sbuf_finish(&sb);
695002d4558SJohn Baldwin 	sbuf_delete(&sb);
69697d8d152SAndre Oppermann 	return(error);
69797d8d152SAndre Oppermann }
69897d8d152SAndre Oppermann 
69997d8d152SAndre Oppermann /*
700fffb9f1dSBjoern A. Zeeb  * Caller has to make sure the curvnet is set properly.
70197d8d152SAndre Oppermann  */
70297d8d152SAndre Oppermann static void
703fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(int all)
70497d8d152SAndre Oppermann {
705b62dccc7SAndre Oppermann 	struct hc_metrics *hc_entry, *hc_next;
70697d8d152SAndre Oppermann 	int i;
70797d8d152SAndre Oppermann 
708603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
709603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
710ac957cd2SJulian Elischer 		TAILQ_FOREACH_SAFE(hc_entry,
7115ed3800eSJulian Elischer 		    &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
71297d8d152SAndre Oppermann 			if (all || hc_entry->rmx_expire <= 0) {
713603724d3SBjoern A. Zeeb 				TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
71497d8d152SAndre Oppermann 					      hc_entry, rmx_q);
715603724d3SBjoern A. Zeeb 				uma_zfree(V_tcp_hostcache.zone, hc_entry);
716603724d3SBjoern A. Zeeb 				V_tcp_hostcache.hashbase[i].hch_length--;
717*529a2a0fSRichard Scheffenegger 				atomic_subtract_int(&V_tcp_hostcache.cache_count, 1);
71897d8d152SAndre Oppermann 			} else
719603724d3SBjoern A. Zeeb 				hc_entry->rmx_expire -= V_tcp_hostcache.prune;
72097d8d152SAndre Oppermann 		}
721603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
72297d8d152SAndre Oppermann 	}
723fffb9f1dSBjoern A. Zeeb }
724fffb9f1dSBjoern A. Zeeb 
725fffb9f1dSBjoern A. Zeeb /*
726fffb9f1dSBjoern A. Zeeb  * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
727fffb9f1dSBjoern A. Zeeb  * periodically from the callout.
728fffb9f1dSBjoern A. Zeeb  */
729fffb9f1dSBjoern A. Zeeb static void
730fffb9f1dSBjoern A. Zeeb tcp_hc_purge(void *arg)
731fffb9f1dSBjoern A. Zeeb {
732fffb9f1dSBjoern A. Zeeb 	CURVNET_SET((struct vnet *) arg);
733fffb9f1dSBjoern A. Zeeb 	int all = 0;
734fffb9f1dSBjoern A. Zeeb 
735fffb9f1dSBjoern A. Zeeb 	if (V_tcp_hostcache.purgeall) {
736fffb9f1dSBjoern A. Zeeb 		all = 1;
737fffb9f1dSBjoern A. Zeeb 		V_tcp_hostcache.purgeall = 0;
738fffb9f1dSBjoern A. Zeeb 	}
739fffb9f1dSBjoern A. Zeeb 
740fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(all);
741ac957cd2SJulian Elischer 
742ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
743ac957cd2SJulian Elischer 	    tcp_hc_purge, arg);
74421ca7b57SMarko Zec 	CURVNET_RESTORE();
74597d8d152SAndre Oppermann }
74626456380SHiren Panchasara 
74726456380SHiren Panchasara /*
74826456380SHiren Panchasara  * Expire and purge all entries in hostcache immediately.
74926456380SHiren Panchasara  */
75026456380SHiren Panchasara static int
75126456380SHiren Panchasara sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS)
75226456380SHiren Panchasara {
75326456380SHiren Panchasara 	int error, val;
75426456380SHiren Panchasara 
75526456380SHiren Panchasara 	val = 0;
75626456380SHiren Panchasara 	error = sysctl_handle_int(oidp, &val, 0, req);
75726456380SHiren Panchasara 	if (error || !req->newptr)
75826456380SHiren Panchasara 		return (error);
75926456380SHiren Panchasara 
76026456380SHiren Panchasara 	tcp_hc_purge_internal(1);
76126456380SHiren Panchasara 
76226456380SHiren Panchasara 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
76326456380SHiren Panchasara 	    tcp_hc_purge, curvnet);
76426456380SHiren Panchasara 
76526456380SHiren Panchasara 	return (0);
76626456380SHiren Panchasara }
767