xref: /freebsd/sys/netinet/tcp_hostcache.c (revision 5f901c92a8d99227901f548f0b755b31d9dd8525)
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 
115*5f901c92SAndrew Turner VNET_DEFINE_STATIC(struct tcp_hostcache, tcp_hostcache);
1161e77c105SRobert Watson #define	V_tcp_hostcache		VNET(tcp_hostcache)
11782cea7e6SBjoern A. Zeeb 
118*5f901c92SAndrew 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 
1286472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0,
1290eb7cf4dSBrooks Davis     "TCP Host cache");
13097d8d152SAndre Oppermann 
1318a56c645SHiren Panchasara VNET_DEFINE(int, tcp_use_hostcache) = 1;
1328a56c645SHiren Panchasara #define V_tcp_use_hostcache  VNET(tcp_use_hostcache)
1338a56c645SHiren Panchasara SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
1348a56c645SHiren Panchasara     &VNET_NAME(tcp_use_hostcache), 0,
1358a56c645SHiren Panchasara     "Enable the TCP hostcache");
1368a56c645SHiren Panchasara 
1376df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN,
138eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.cache_limit), 0,
1398b615593SMarko Zec     "Overall entry limit for hostcache");
14097d8d152SAndre Oppermann 
1416df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN,
142eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.hashsize), 0,
1438b615593SMarko Zec     "Size of TCP hostcache hashtable");
14497d8d152SAndre Oppermann 
1456df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
1466df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0,
1478b615593SMarko Zec     "Per-bucket hash limit for hostcache");
14897d8d152SAndre Oppermann 
1496df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD,
150eddfbb76SRobert Watson      &VNET_NAME(tcp_hostcache.cache_count), 0,
1518b615593SMarko Zec     "Current number of entries in hostcache");
15297d8d152SAndre Oppermann 
1536df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW,
154eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.expire), 0,
1558b615593SMarko Zec     "Expire time of TCP hostcache entries");
15697d8d152SAndre Oppermann 
1576df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW,
158eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.prune), 0,
159eddfbb76SRobert Watson     "Time between purge runs");
160dba3c508SYaroslav Tykhiy 
1616df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW,
162eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.purgeall), 0,
1638b615593SMarko Zec     "Expire all entires on next purge run");
16497d8d152SAndre Oppermann 
16597d8d152SAndre Oppermann SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
16697d8d152SAndre Oppermann     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
16797d8d152SAndre Oppermann     sysctl_tcp_hc_list, "A", "List of all hostcache entries");
16897d8d152SAndre Oppermann 
16926456380SHiren Panchasara SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, purgenow,
17026456380SHiren Panchasara     CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
17126456380SHiren Panchasara     sysctl_tcp_hc_purgenow, "I", "Immediately purge all entries");
17297d8d152SAndre Oppermann 
17397d8d152SAndre Oppermann static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
17497d8d152SAndre Oppermann 
17597d8d152SAndre Oppermann #define HOSTCACHE_HASH(ip) \
17697d8d152SAndre Oppermann 	(((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) &	\
177603724d3SBjoern A. Zeeb 	  V_tcp_hostcache.hashmask)
17897d8d152SAndre Oppermann 
17997d8d152SAndre Oppermann /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
18097d8d152SAndre Oppermann #define HOSTCACHE_HASH6(ip6)				\
18197d8d152SAndre Oppermann 	(((ip6)->s6_addr32[0] ^				\
18297d8d152SAndre Oppermann 	  (ip6)->s6_addr32[1] ^				\
18397d8d152SAndre Oppermann 	  (ip6)->s6_addr32[2] ^				\
18497d8d152SAndre Oppermann 	  (ip6)->s6_addr32[3]) &			\
185603724d3SBjoern A. Zeeb 	 V_tcp_hostcache.hashmask)
18697d8d152SAndre Oppermann 
18797d8d152SAndre Oppermann #define THC_LOCK(lp)		mtx_lock(lp)
18897d8d152SAndre Oppermann #define THC_UNLOCK(lp)		mtx_unlock(lp)
18997d8d152SAndre Oppermann 
19097d8d152SAndre Oppermann void
19197d8d152SAndre Oppermann tcp_hc_init(void)
19297d8d152SAndre Oppermann {
19332fe38f1SAndrey Zonov 	u_int cache_limit;
19497d8d152SAndre Oppermann 	int i;
19597d8d152SAndre Oppermann 
19697d8d152SAndre Oppermann 	/*
197e487a5e2SRobert Watson 	 * Initialize hostcache structures.
19897d8d152SAndre Oppermann 	 */
199603724d3SBjoern A. Zeeb 	V_tcp_hostcache.cache_count = 0;
200603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
201603724d3SBjoern A. Zeeb 	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
202603724d3SBjoern A. Zeeb 	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
203603724d3SBjoern A. Zeeb 	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
20497d8d152SAndre Oppermann 
20597d8d152SAndre Oppermann 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
206603724d3SBjoern A. Zeeb 	    &V_tcp_hostcache.hashsize);
207603724d3SBjoern A. Zeeb 	if (!powerof2(V_tcp_hostcache.hashsize)) {
20897d8d152SAndre Oppermann 		printf("WARNING: hostcache hash size is not a power of 2.\n");
209603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
21097d8d152SAndre Oppermann 	}
211603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
21297d8d152SAndre Oppermann 
21332fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
21432fe38f1SAndrey Zonov 	    &V_tcp_hostcache.bucket_limit);
21532fe38f1SAndrey Zonov 
21632fe38f1SAndrey Zonov 	cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
21732fe38f1SAndrey Zonov 	V_tcp_hostcache.cache_limit = cache_limit;
21832fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
21932fe38f1SAndrey Zonov 	    &V_tcp_hostcache.cache_limit);
22032fe38f1SAndrey Zonov 	if (V_tcp_hostcache.cache_limit > cache_limit)
22132fe38f1SAndrey Zonov 		V_tcp_hostcache.cache_limit = cache_limit;
22232fe38f1SAndrey Zonov 
22397d8d152SAndre Oppermann 	/*
224e487a5e2SRobert Watson 	 * Allocate the hash table.
22597d8d152SAndre Oppermann 	 */
226603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase = (struct hc_head *)
227603724d3SBjoern A. Zeeb 	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
22897d8d152SAndre Oppermann 		   M_HOSTCACHE, M_WAITOK | M_ZERO);
22997d8d152SAndre Oppermann 
23097d8d152SAndre Oppermann 	/*
231e487a5e2SRobert Watson 	 * Initialize the hash buckets.
23297d8d152SAndre Oppermann 	 */
233603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
234603724d3SBjoern A. Zeeb 		TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
235603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[i].hch_length = 0;
236603724d3SBjoern A. Zeeb 		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
23797d8d152SAndre Oppermann 			  NULL, MTX_DEF);
23897d8d152SAndre Oppermann 	}
23997d8d152SAndre Oppermann 
24097d8d152SAndre Oppermann 	/*
24197d8d152SAndre Oppermann 	 * Allocate the hostcache entries.
24297d8d152SAndre Oppermann 	 */
243ac957cd2SJulian Elischer 	V_tcp_hostcache.zone =
244ac957cd2SJulian Elischer 	    uma_zcreate("hostcache", sizeof(struct hc_metrics),
2454efb805cSAndre Oppermann 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
246603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
24797d8d152SAndre Oppermann 
24897d8d152SAndre Oppermann 	/*
24997d8d152SAndre Oppermann 	 * Set up periodic cache cleanup.
25097d8d152SAndre Oppermann 	 */
251fd90e2edSJung-uk Kim 	callout_init(&V_tcp_hc_callout, 1);
252ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
25321ca7b57SMarko Zec 	    tcp_hc_purge, curvnet);
25497d8d152SAndre Oppermann }
25597d8d152SAndre Oppermann 
256bc29160dSMarko Zec #ifdef VIMAGE
257bc29160dSMarko Zec void
258bc29160dSMarko Zec tcp_hc_destroy(void)
259bc29160dSMarko Zec {
260fffb9f1dSBjoern A. Zeeb 	int i;
261bc29160dSMarko Zec 
262bc29160dSMarko Zec 	callout_drain(&V_tcp_hc_callout);
263fffb9f1dSBjoern A. Zeeb 
264fffb9f1dSBjoern A. Zeeb 	/* Purge all hc entries. */
265fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(1);
266fffb9f1dSBjoern A. Zeeb 
267fffb9f1dSBjoern A. Zeeb 	/* Free the uma zone and the allocated hash table. */
268fffb9f1dSBjoern A. Zeeb 	uma_zdestroy(V_tcp_hostcache.zone);
269fffb9f1dSBjoern A. Zeeb 
270fffb9f1dSBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++)
271fffb9f1dSBjoern A. Zeeb 		mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx);
272fffb9f1dSBjoern A. Zeeb 	free(V_tcp_hostcache.hashbase, M_HOSTCACHE);
273bc29160dSMarko Zec }
274bc29160dSMarko Zec #endif
275bc29160dSMarko Zec 
27697d8d152SAndre Oppermann /*
27797d8d152SAndre Oppermann  * Internal function: look up an entry in the hostcache or return NULL.
27897d8d152SAndre Oppermann  *
27997d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
28097d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
28197d8d152SAndre Oppermann  */
28297d8d152SAndre Oppermann static struct hc_metrics *
28397d8d152SAndre Oppermann tcp_hc_lookup(struct in_conninfo *inc)
28497d8d152SAndre Oppermann {
28597d8d152SAndre Oppermann 	int hash;
28697d8d152SAndre Oppermann 	struct hc_head *hc_head;
28797d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
28897d8d152SAndre Oppermann 
2898a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
2908a56c645SHiren Panchasara 		return NULL;
2918a56c645SHiren Panchasara 
29297d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
29397d8d152SAndre Oppermann 
29497d8d152SAndre Oppermann 	/*
29597d8d152SAndre Oppermann 	 * Hash the foreign ip address.
29697d8d152SAndre Oppermann 	 */
297dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
29897d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
29997d8d152SAndre Oppermann 	else
30097d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
30197d8d152SAndre Oppermann 
302603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
30397d8d152SAndre Oppermann 
30497d8d152SAndre Oppermann 	/*
305e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
306e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
307e487a5e2SRobert Watson 	 * done.
30897d8d152SAndre Oppermann 	 */
30997d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
31097d8d152SAndre Oppermann 
31197d8d152SAndre Oppermann 	/*
312e487a5e2SRobert Watson 	 * Iterate through entries in bucket row looking for a match.
31397d8d152SAndre Oppermann 	 */
31497d8d152SAndre Oppermann 	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
315dcdb4371SBjoern A. Zeeb 		if (inc->inc_flags & INC_ISIPV6) {
316028bdf28SAndrey V. Elsukov 			/* XXX: check ip6_zoneid */
31797d8d152SAndre Oppermann 			if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
31897d8d152SAndre Oppermann 			    sizeof(inc->inc6_faddr)) == 0)
31997d8d152SAndre Oppermann 				return hc_entry;
32097d8d152SAndre Oppermann 		} else {
32197d8d152SAndre Oppermann 			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
32297d8d152SAndre Oppermann 			    sizeof(inc->inc_faddr)) == 0)
32397d8d152SAndre Oppermann 				return hc_entry;
32497d8d152SAndre Oppermann 		}
32597d8d152SAndre Oppermann 	}
32697d8d152SAndre Oppermann 
32797d8d152SAndre Oppermann 	/*
328e487a5e2SRobert Watson 	 * We were unsuccessful and didn't find anything.
32997d8d152SAndre Oppermann 	 */
33097d8d152SAndre Oppermann 	THC_UNLOCK(&hc_head->hch_mtx);
33197d8d152SAndre Oppermann 	return NULL;
33297d8d152SAndre Oppermann }
33397d8d152SAndre Oppermann 
33497d8d152SAndre Oppermann /*
335e487a5e2SRobert Watson  * Internal function: insert an entry into the hostcache or return NULL if
336e487a5e2SRobert Watson  * unable to allocate a new one.
33797d8d152SAndre Oppermann  *
33897d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
33997d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
34097d8d152SAndre Oppermann  */
34197d8d152SAndre Oppermann static struct hc_metrics *
34297d8d152SAndre Oppermann tcp_hc_insert(struct in_conninfo *inc)
34397d8d152SAndre Oppermann {
34497d8d152SAndre Oppermann 	int hash;
34597d8d152SAndre Oppermann 	struct hc_head *hc_head;
34697d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
34797d8d152SAndre Oppermann 
3488a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
3498a56c645SHiren Panchasara 		return NULL;
3508a56c645SHiren Panchasara 
35197d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
35297d8d152SAndre Oppermann 
35397d8d152SAndre Oppermann 	/*
354e487a5e2SRobert Watson 	 * Hash the foreign ip address.
35597d8d152SAndre Oppermann 	 */
356dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
35797d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
35897d8d152SAndre Oppermann 	else
35997d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
36097d8d152SAndre Oppermann 
361603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
36297d8d152SAndre Oppermann 
36397d8d152SAndre Oppermann 	/*
364e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
365e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
366e487a5e2SRobert Watson 	 * done.
36797d8d152SAndre Oppermann 	 */
36897d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
36997d8d152SAndre Oppermann 
37097d8d152SAndre Oppermann 	/*
371e487a5e2SRobert Watson 	 * If the bucket limit is reached, reuse the least-used element.
37297d8d152SAndre Oppermann 	 */
373603724d3SBjoern A. Zeeb 	if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
374603724d3SBjoern A. Zeeb 	    V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) {
37597d8d152SAndre Oppermann 		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
37697d8d152SAndre Oppermann 		/*
37797d8d152SAndre Oppermann 		 * At first we were dropping the last element, just to
378e487a5e2SRobert Watson 		 * reacquire it in the next two lines again, which isn't very
379e487a5e2SRobert Watson 		 * efficient.  Instead just reuse the least used element.
380e487a5e2SRobert Watson 		 * We may drop something that is still "in-use" but we can be
381e487a5e2SRobert Watson 		 * "lossy".
38245024be0SAndre Oppermann 		 * Just give up if this bucket row is empty and we don't have
38345024be0SAndre Oppermann 		 * anything to replace.
38497d8d152SAndre Oppermann 		 */
38545024be0SAndre Oppermann 		if (hc_entry == NULL) {
38645024be0SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
38745024be0SAndre Oppermann 			return NULL;
38845024be0SAndre Oppermann 		}
38997d8d152SAndre Oppermann 		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
390603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[hash].hch_length--;
391603724d3SBjoern A. Zeeb 		V_tcp_hostcache.cache_count--;
39278b50714SRobert Watson 		TCPSTAT_INC(tcps_hc_bucketoverflow);
393cd6c4060SAndre Oppermann #if 0
394603724d3SBjoern A. Zeeb 		uma_zfree(V_tcp_hostcache.zone, hc_entry);
395cd6c4060SAndre Oppermann #endif
39697d8d152SAndre Oppermann 	} else {
39797d8d152SAndre Oppermann 		/*
398e487a5e2SRobert Watson 		 * Allocate a new entry, or balk if not possible.
39997d8d152SAndre Oppermann 		 */
400603724d3SBjoern A. Zeeb 		hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
40197d8d152SAndre Oppermann 		if (hc_entry == NULL) {
40297d8d152SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
40397d8d152SAndre Oppermann 			return NULL;
40497d8d152SAndre Oppermann 		}
40597d8d152SAndre Oppermann 	}
40697d8d152SAndre Oppermann 
40797d8d152SAndre Oppermann 	/*
408e487a5e2SRobert Watson 	 * Initialize basic information of hostcache entry.
40997d8d152SAndre Oppermann 	 */
41097d8d152SAndre Oppermann 	bzero(hc_entry, sizeof(*hc_entry));
411028bdf28SAndrey V. Elsukov 	if (inc->inc_flags & INC_ISIPV6) {
412028bdf28SAndrey V. Elsukov 		hc_entry->ip6 = inc->inc6_faddr;
413028bdf28SAndrey V. Elsukov 		hc_entry->ip6_zoneid = inc->inc6_zoneid;
414028bdf28SAndrey V. Elsukov 	} else
41597d8d152SAndre Oppermann 		hc_entry->ip4 = inc->inc_faddr;
41697d8d152SAndre Oppermann 	hc_entry->rmx_head = hc_head;
417603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire;
41897d8d152SAndre Oppermann 
41997d8d152SAndre Oppermann 	/*
420e487a5e2SRobert Watson 	 * Put it upfront.
42197d8d152SAndre Oppermann 	 */
42297d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
423603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase[hash].hch_length++;
424603724d3SBjoern A. Zeeb 	V_tcp_hostcache.cache_count++;
42578b50714SRobert Watson 	TCPSTAT_INC(tcps_hc_added);
42697d8d152SAndre Oppermann 
42797d8d152SAndre Oppermann 	return hc_entry;
42897d8d152SAndre Oppermann }
42997d8d152SAndre Oppermann 
43097d8d152SAndre Oppermann /*
43197d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and fill out the
432e487a5e2SRobert Watson  * supplied TCP metrics structure.  Fills in NULL when no entry was found or
433e487a5e2SRobert Watson  * a value is not set.
43497d8d152SAndre Oppermann  */
43597d8d152SAndre Oppermann void
43697d8d152SAndre Oppermann tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
43797d8d152SAndre Oppermann {
43897d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
43997d8d152SAndre Oppermann 
4408a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
4418a56c645SHiren Panchasara 		return;
4428a56c645SHiren Panchasara 
44397d8d152SAndre Oppermann 	/*
444e487a5e2SRobert Watson 	 * Find the right bucket.
44597d8d152SAndre Oppermann 	 */
44697d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
44797d8d152SAndre Oppermann 
44897d8d152SAndre Oppermann 	/*
449e487a5e2SRobert Watson 	 * If we don't have an existing object.
45097d8d152SAndre Oppermann 	 */
45197d8d152SAndre Oppermann 	if (hc_entry == NULL) {
45297d8d152SAndre Oppermann 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
45397d8d152SAndre Oppermann 		return;
45497d8d152SAndre Oppermann 	}
45597d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
456603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
45797d8d152SAndre Oppermann 
45897d8d152SAndre Oppermann 	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
45997d8d152SAndre Oppermann 	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
46097d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
46197d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
46297d8d152SAndre Oppermann 	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
46397d8d152SAndre Oppermann 	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
46497d8d152SAndre Oppermann 	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
46597d8d152SAndre Oppermann 
46697d8d152SAndre Oppermann 	/*
467e487a5e2SRobert Watson 	 * Unlock bucket row.
46897d8d152SAndre Oppermann 	 */
46997d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
47097d8d152SAndre Oppermann }
47197d8d152SAndre Oppermann 
47297d8d152SAndre Oppermann /*
47397d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and return the
4748a56c645SHiren Panchasara  * discovered path MTU.  Returns 0 if no entry is found or value is not
4756fbed4afSRobert Watson  * set.
47697d8d152SAndre Oppermann  */
4773ac12506SJonathan T. Looney uint32_t
47897d8d152SAndre Oppermann tcp_hc_getmtu(struct in_conninfo *inc)
47997d8d152SAndre Oppermann {
48097d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
4813ac12506SJonathan T. Looney 	uint32_t mtu;
48297d8d152SAndre Oppermann 
4838a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
4848a56c645SHiren Panchasara 		return 0;
4858a56c645SHiren Panchasara 
48697d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
48797d8d152SAndre Oppermann 	if (hc_entry == NULL) {
48897d8d152SAndre Oppermann 		return 0;
48997d8d152SAndre Oppermann 	}
49097d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
491603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
49297d8d152SAndre Oppermann 
49397d8d152SAndre Oppermann 	mtu = hc_entry->rmx_mtu;
49497d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
49597d8d152SAndre Oppermann 	return mtu;
49697d8d152SAndre Oppermann }
49797d8d152SAndre Oppermann 
49897d8d152SAndre Oppermann /*
499e487a5e2SRobert Watson  * External function: update the MTU value of an entry in the hostcache.
50097d8d152SAndre Oppermann  * Creates a new entry if none was found.
50197d8d152SAndre Oppermann  */
50297d8d152SAndre Oppermann void
5033ac12506SJonathan T. Looney tcp_hc_updatemtu(struct in_conninfo *inc, uint32_t mtu)
50497d8d152SAndre Oppermann {
50597d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
50697d8d152SAndre Oppermann 
5078a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
5088a56c645SHiren Panchasara 		return;
5098a56c645SHiren Panchasara 
51097d8d152SAndre Oppermann 	/*
511e487a5e2SRobert Watson 	 * Find the right bucket.
51297d8d152SAndre Oppermann 	 */
51397d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
51497d8d152SAndre Oppermann 
51597d8d152SAndre Oppermann 	/*
516e487a5e2SRobert Watson 	 * If we don't have an existing object, try to insert a new one.
51797d8d152SAndre Oppermann 	 */
51897d8d152SAndre Oppermann 	if (hc_entry == NULL) {
51997d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
52097d8d152SAndre Oppermann 		if (hc_entry == NULL)
52197d8d152SAndre Oppermann 			return;
52297d8d152SAndre Oppermann 	}
52397d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
524603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
52597d8d152SAndre Oppermann 
52697d8d152SAndre Oppermann 	hc_entry->rmx_mtu = mtu;
52797d8d152SAndre Oppermann 
52897d8d152SAndre Oppermann 	/*
529e487a5e2SRobert Watson 	 * Put it upfront so we find it faster next time.
53097d8d152SAndre Oppermann 	 */
53197d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
53297d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
53397d8d152SAndre Oppermann 
53497d8d152SAndre Oppermann 	/*
535e487a5e2SRobert Watson 	 * Unlock bucket row.
53697d8d152SAndre Oppermann 	 */
53797d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
53897d8d152SAndre Oppermann }
53997d8d152SAndre Oppermann 
54097d8d152SAndre Oppermann /*
541e487a5e2SRobert Watson  * External function: update the TCP metrics of an entry in the hostcache.
54297d8d152SAndre Oppermann  * Creates a new entry if none was found.
54397d8d152SAndre Oppermann  */
54497d8d152SAndre Oppermann void
54597d8d152SAndre Oppermann tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
54697d8d152SAndre Oppermann {
54797d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
54897d8d152SAndre Oppermann 
5498a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
5508a56c645SHiren Panchasara 		return;
5518a56c645SHiren Panchasara 
55297d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
55397d8d152SAndre Oppermann 	if (hc_entry == NULL) {
55497d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
55597d8d152SAndre Oppermann 		if (hc_entry == NULL)
55697d8d152SAndre Oppermann 			return;
55797d8d152SAndre Oppermann 	}
55897d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
559603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
56097d8d152SAndre Oppermann 
56197d8d152SAndre Oppermann 	if (hcml->rmx_rtt != 0) {
56297d8d152SAndre Oppermann 		if (hc_entry->rmx_rtt == 0)
56397d8d152SAndre Oppermann 			hc_entry->rmx_rtt = hcml->rmx_rtt;
56497d8d152SAndre Oppermann 		else
5653ac12506SJonathan T. Looney 			hc_entry->rmx_rtt = ((uint64_t)hc_entry->rmx_rtt +
5663ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_rtt) / 2;
56778b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrtt);
56897d8d152SAndre Oppermann 	}
56997d8d152SAndre Oppermann 	if (hcml->rmx_rttvar != 0) {
57097d8d152SAndre Oppermann 	        if (hc_entry->rmx_rttvar == 0)
57197d8d152SAndre Oppermann 			hc_entry->rmx_rttvar = hcml->rmx_rttvar;
57297d8d152SAndre Oppermann 		else
5733ac12506SJonathan T. Looney 			hc_entry->rmx_rttvar = ((uint64_t)hc_entry->rmx_rttvar +
5743ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_rttvar) / 2;
57578b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrttvar);
57697d8d152SAndre Oppermann 	}
57797d8d152SAndre Oppermann 	if (hcml->rmx_ssthresh != 0) {
57897d8d152SAndre Oppermann 		if (hc_entry->rmx_ssthresh == 0)
57997d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
58097d8d152SAndre Oppermann 		else
58197d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh =
58297d8d152SAndre Oppermann 			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
58378b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedssthresh);
58497d8d152SAndre Oppermann 	}
58597d8d152SAndre Oppermann 	if (hcml->rmx_cwnd != 0) {
58697d8d152SAndre Oppermann 		if (hc_entry->rmx_cwnd == 0)
58797d8d152SAndre Oppermann 			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
58897d8d152SAndre Oppermann 		else
5893ac12506SJonathan T. Looney 			hc_entry->rmx_cwnd = ((uint64_t)hc_entry->rmx_cwnd +
5903ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_cwnd) / 2;
59178b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedcwnd); */
59297d8d152SAndre Oppermann 	}
59397d8d152SAndre Oppermann 	if (hcml->rmx_sendpipe != 0) {
59497d8d152SAndre Oppermann 		if (hc_entry->rmx_sendpipe == 0)
59597d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
59697d8d152SAndre Oppermann 		else
59797d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe =
5983ac12506SJonathan T. Looney 			    ((uint64_t)hc_entry->rmx_sendpipe +
5993ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_sendpipe) /2;
60078b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedsendpipe); */
60197d8d152SAndre Oppermann 	}
60297d8d152SAndre Oppermann 	if (hcml->rmx_recvpipe != 0) {
60397d8d152SAndre Oppermann 		if (hc_entry->rmx_recvpipe == 0)
60497d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
60597d8d152SAndre Oppermann 		else
60697d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe =
6073ac12506SJonathan T. Looney 			    ((uint64_t)hc_entry->rmx_recvpipe +
6083ac12506SJonathan T. Looney 			    (uint64_t)hcml->rmx_recvpipe) /2;
60978b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedrecvpipe); */
61097d8d152SAndre Oppermann 	}
61197d8d152SAndre Oppermann 
61297d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
61397d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
61497d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
61597d8d152SAndre Oppermann }
61697d8d152SAndre Oppermann 
61797d8d152SAndre Oppermann /*
61897d8d152SAndre Oppermann  * Sysctl function: prints the list and values of all hostcache entries in
61997d8d152SAndre Oppermann  * unsorted order.
62097d8d152SAndre Oppermann  */
62197d8d152SAndre Oppermann static int
62297d8d152SAndre Oppermann sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
62397d8d152SAndre Oppermann {
624751ccc42SIan Lepore 	const int linesize = 128;
625002d4558SJohn Baldwin 	struct sbuf sb;
626002d4558SJohn Baldwin 	int i, error;
62797d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
6288144690aSEric van Gyzen 	char ip4buf[INET_ADDRSTRLEN];
6291d54aa3bSBjoern A. Zeeb #ifdef INET6
6301d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
6311d54aa3bSBjoern A. Zeeb #endif
63297d8d152SAndre Oppermann 
633b8a2fb91SHiren Panchasara 	if (jailed_without_vnet(curthread->td_ucred) != 0)
634b8a2fb91SHiren Panchasara 		return (EPERM);
635b8a2fb91SHiren Panchasara 
636dcdeb95fSIan Lepore 	sbuf_new(&sb, NULL, linesize * (V_tcp_hostcache.cache_count + 1),
637dcdeb95fSIan Lepore 		SBUF_INCLUDENUL);
63897d8d152SAndre Oppermann 
639002d4558SJohn Baldwin 	sbuf_printf(&sb,
6404d163382SHiren Panchasara 	        "\nIP address        MTU  SSTRESH      RTT   RTTVAR "
64197d8d152SAndre Oppermann 		"    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
64297d8d152SAndre Oppermann 
64397d8d152SAndre Oppermann #define msec(u) (((u) + 500) / 1000)
644603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
645603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
646603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
64797d8d152SAndre Oppermann 			      rmx_q) {
648002d4558SJohn Baldwin 			sbuf_printf(&sb,
6493ac12506SJonathan T. Looney 			    "%-15s %5u %8u %6lums %6lums %8u %8u %8u %4lu "
6504d163382SHiren Panchasara 			    "%4lu %4i\n",
6518144690aSEric van Gyzen 			    hc_entry->ip4.s_addr ?
6528144690aSEric van Gyzen 			        inet_ntoa_r(hc_entry->ip4, ip4buf) :
65397d8d152SAndre Oppermann #ifdef INET6
6541d54aa3bSBjoern A. Zeeb 				ip6_sprintf(ip6buf, &hc_entry->ip6),
65597d8d152SAndre Oppermann #else
65697d8d152SAndre Oppermann 				"IPv6?",
65797d8d152SAndre Oppermann #endif
65897d8d152SAndre Oppermann 			    hc_entry->rmx_mtu,
65997d8d152SAndre Oppermann 			    hc_entry->rmx_ssthresh,
6603ac12506SJonathan T. Looney 			    msec((u_long)hc_entry->rmx_rtt *
66197d8d152SAndre Oppermann 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
6623ac12506SJonathan T. Looney 			    msec((u_long)hc_entry->rmx_rttvar *
6633a288e90SMikolaj Golub 				(RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))),
66497d8d152SAndre Oppermann 			    hc_entry->rmx_cwnd,
66597d8d152SAndre Oppermann 			    hc_entry->rmx_sendpipe,
66697d8d152SAndre Oppermann 			    hc_entry->rmx_recvpipe,
66797d8d152SAndre Oppermann 			    hc_entry->rmx_hits,
66897d8d152SAndre Oppermann 			    hc_entry->rmx_updates,
66997d8d152SAndre Oppermann 			    hc_entry->rmx_expire);
67097d8d152SAndre Oppermann 		}
671603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
67297d8d152SAndre Oppermann 	}
67397d8d152SAndre Oppermann #undef msec
674751ccc42SIan Lepore 	error = sbuf_finish(&sb);
675dcdeb95fSIan Lepore 	if (error == 0)
676dcdeb95fSIan Lepore 		error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
677002d4558SJohn Baldwin 	sbuf_delete(&sb);
67897d8d152SAndre Oppermann 	return(error);
67997d8d152SAndre Oppermann }
68097d8d152SAndre Oppermann 
68197d8d152SAndre Oppermann /*
682fffb9f1dSBjoern A. Zeeb  * Caller has to make sure the curvnet is set properly.
68397d8d152SAndre Oppermann  */
68497d8d152SAndre Oppermann static void
685fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(int all)
68697d8d152SAndre Oppermann {
687b62dccc7SAndre Oppermann 	struct hc_metrics *hc_entry, *hc_next;
68897d8d152SAndre Oppermann 	int i;
68997d8d152SAndre Oppermann 
690603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
691603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
692ac957cd2SJulian Elischer 		TAILQ_FOREACH_SAFE(hc_entry,
6935ed3800eSJulian Elischer 		    &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
69497d8d152SAndre Oppermann 			if (all || hc_entry->rmx_expire <= 0) {
695603724d3SBjoern A. Zeeb 				TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
69697d8d152SAndre Oppermann 					      hc_entry, rmx_q);
697603724d3SBjoern A. Zeeb 				uma_zfree(V_tcp_hostcache.zone, hc_entry);
698603724d3SBjoern A. Zeeb 				V_tcp_hostcache.hashbase[i].hch_length--;
699603724d3SBjoern A. Zeeb 				V_tcp_hostcache.cache_count--;
70097d8d152SAndre Oppermann 			} else
701603724d3SBjoern A. Zeeb 				hc_entry->rmx_expire -= V_tcp_hostcache.prune;
70297d8d152SAndre Oppermann 		}
703603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
70497d8d152SAndre Oppermann 	}
705fffb9f1dSBjoern A. Zeeb }
706fffb9f1dSBjoern A. Zeeb 
707fffb9f1dSBjoern A. Zeeb /*
708fffb9f1dSBjoern A. Zeeb  * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
709fffb9f1dSBjoern A. Zeeb  * periodically from the callout.
710fffb9f1dSBjoern A. Zeeb  */
711fffb9f1dSBjoern A. Zeeb static void
712fffb9f1dSBjoern A. Zeeb tcp_hc_purge(void *arg)
713fffb9f1dSBjoern A. Zeeb {
714fffb9f1dSBjoern A. Zeeb 	CURVNET_SET((struct vnet *) arg);
715fffb9f1dSBjoern A. Zeeb 	int all = 0;
716fffb9f1dSBjoern A. Zeeb 
717fffb9f1dSBjoern A. Zeeb 	if (V_tcp_hostcache.purgeall) {
718fffb9f1dSBjoern A. Zeeb 		all = 1;
719fffb9f1dSBjoern A. Zeeb 		V_tcp_hostcache.purgeall = 0;
720fffb9f1dSBjoern A. Zeeb 	}
721fffb9f1dSBjoern A. Zeeb 
722fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(all);
723ac957cd2SJulian Elischer 
724ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
725ac957cd2SJulian Elischer 	    tcp_hc_purge, arg);
72621ca7b57SMarko Zec 	CURVNET_RESTORE();
72797d8d152SAndre Oppermann }
72826456380SHiren Panchasara 
72926456380SHiren Panchasara /*
73026456380SHiren Panchasara  * Expire and purge all entries in hostcache immediately.
73126456380SHiren Panchasara  */
73226456380SHiren Panchasara static int
73326456380SHiren Panchasara sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS)
73426456380SHiren Panchasara {
73526456380SHiren Panchasara 	int error, val;
73626456380SHiren Panchasara 
73726456380SHiren Panchasara 	val = 0;
73826456380SHiren Panchasara 	error = sysctl_handle_int(oidp, &val, 0, req);
73926456380SHiren Panchasara 	if (error || !req->newptr)
74026456380SHiren Panchasara 		return (error);
74126456380SHiren Panchasara 
74226456380SHiren Panchasara 	tcp_hc_purge_internal(1);
74326456380SHiren Panchasara 
74426456380SHiren Panchasara 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
74526456380SHiren Panchasara 	    tcp_hc_purge, curvnet);
74626456380SHiren Panchasara 
74726456380SHiren Panchasara 	return (0);
74826456380SHiren Panchasara }
749