xref: /freebsd/sys/netinet/tcp_hostcache.c (revision 8a56c645335e50e7e63cc47cf00ea631030533c8)
1c398230bSWarner Losh /*-
297d8d152SAndre Oppermann  * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG
397d8d152SAndre Oppermann  * All rights reserved.
497d8d152SAndre Oppermann  *
597d8d152SAndre Oppermann  * Redistribution and use in source and binary forms, with or without
697d8d152SAndre Oppermann  * modification, are permitted provided that the following conditions
797d8d152SAndre Oppermann  * are met:
897d8d152SAndre Oppermann  * 1. Redistributions of source code must retain the above copyright
997d8d152SAndre Oppermann  *    notice, this list of conditions and the following disclaimer.
1097d8d152SAndre Oppermann  * 2. Redistributions in binary form must reproduce the above copyright
1197d8d152SAndre Oppermann  *    notice, this list of conditions and the following disclaimer in the
1297d8d152SAndre Oppermann  *    documentation and/or other materials provided with the distribution.
1397d8d152SAndre Oppermann  * 3. The name of the author may not be used to endorse or promote
1497d8d152SAndre Oppermann  *    products derived from this software without specific prior written
1597d8d152SAndre Oppermann  *    permission.
1697d8d152SAndre Oppermann  *
1797d8d152SAndre Oppermann  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1897d8d152SAndre Oppermann  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1997d8d152SAndre Oppermann  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2097d8d152SAndre Oppermann  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2197d8d152SAndre Oppermann  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2297d8d152SAndre Oppermann  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2397d8d152SAndre Oppermann  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2497d8d152SAndre Oppermann  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2597d8d152SAndre Oppermann  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2697d8d152SAndre Oppermann  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2797d8d152SAndre Oppermann  * SUCH DAMAGE.
2897d8d152SAndre Oppermann  */
2997d8d152SAndre Oppermann 
3097d8d152SAndre Oppermann /*
31e487a5e2SRobert Watson  * The tcp_hostcache moves the tcp-specific cached metrics from the routing
32e487a5e2SRobert Watson  * table to a dedicated structure indexed by the remote IP address.  It keeps
33e487a5e2SRobert Watson  * information on the measured TCP parameters of past TCP sessions to allow
34e487a5e2SRobert Watson  * better initial start values to be used with later connections to/from the
354d163382SHiren Panchasara  * same source.  Depending on the network parameters (delay, max MTU,
364d163382SHiren Panchasara  * congestion window) between local and remote sites, this can lead to
37e487a5e2SRobert Watson  * significant speed-ups for new TCP connections after the first one.
3897d8d152SAndre Oppermann  *
39e487a5e2SRobert Watson  * Due to the tcp_hostcache, all TCP-specific metrics information in the
40003c7e36SRui Paulo  * routing table have been removed.  The inpcb no longer keeps a pointer to
41e487a5e2SRobert Watson  * the routing entry, and protocol-initiated route cloning has been removed
42e487a5e2SRobert Watson  * as well.  With these changes, the routing table has gone back to being
43e487a5e2SRobert Watson  * more lightwight and only carries information related to packet forwarding.
4497d8d152SAndre Oppermann  *
45e487a5e2SRobert Watson  * tcp_hostcache is designed for multiple concurrent access in SMP
46e487a5e2SRobert Watson  * environments and high contention.  All bucket rows have their own lock and
47e487a5e2SRobert Watson  * thus multiple lookups and modifies can be done at the same time as long as
48e487a5e2SRobert Watson  * they are in different bucket rows.  If a request for insertion of a new
49e487a5e2SRobert Watson  * record can't be satisfied, it simply returns an empty structure.  Nobody
50e487a5e2SRobert Watson  * and nothing outside of tcp_hostcache.c will ever point directly to any
51e487a5e2SRobert Watson  * entry in the tcp_hostcache.  All communication is done in an
52e487a5e2SRobert Watson  * object-oriented way and only functions of tcp_hostcache will manipulate
53e487a5e2SRobert Watson  * hostcache entries.  Otherwise, we are unable to achieve good behaviour in
54e487a5e2SRobert Watson  * concurrent access situations.  Since tcp_hostcache is only caching
55e487a5e2SRobert Watson  * information, there are no fatal consequences if we either can't satisfy
56e487a5e2SRobert Watson  * any particular request or have to drop/overwrite an existing entry because
57e487a5e2SRobert Watson  * of bucket limit memory constrains.
5897d8d152SAndre Oppermann  */
5997d8d152SAndre Oppermann 
6097d8d152SAndre Oppermann /*
6197d8d152SAndre Oppermann  * Many thanks to jlemon for basic structure of tcp_syncache which is being
6297d8d152SAndre Oppermann  * followed here.
6397d8d152SAndre Oppermann  */
6497d8d152SAndre Oppermann 
654b421e2dSMike Silbersack #include <sys/cdefs.h>
664b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
674b421e2dSMike Silbersack 
6897d8d152SAndre Oppermann #include "opt_inet6.h"
6997d8d152SAndre Oppermann 
7097d8d152SAndre Oppermann #include <sys/param.h>
7197d8d152SAndre Oppermann #include <sys/systm.h>
7297d8d152SAndre Oppermann #include <sys/kernel.h>
7397d8d152SAndre Oppermann #include <sys/lock.h>
7497d8d152SAndre Oppermann #include <sys/mutex.h>
7597d8d152SAndre Oppermann #include <sys/malloc.h>
76002d4558SJohn Baldwin #include <sys/sbuf.h>
7797d8d152SAndre Oppermann #include <sys/socket.h>
7897d8d152SAndre Oppermann #include <sys/socketvar.h>
7997d8d152SAndre Oppermann #include <sys/sysctl.h>
8097d8d152SAndre Oppermann 
8197d8d152SAndre Oppermann #include <net/if.h>
8276039bc8SGleb Smirnoff #include <net/if_var.h>
835736e6fbSBjoern A. Zeeb #include <net/route.h>
84530c0060SRobert Watson #include <net/vnet.h>
8597d8d152SAndre Oppermann 
8697d8d152SAndre Oppermann #include <netinet/in.h>
8797d8d152SAndre Oppermann #include <netinet/in_systm.h>
8897d8d152SAndre Oppermann #include <netinet/ip.h>
8997d8d152SAndre Oppermann #include <netinet/in_var.h>
9097d8d152SAndre Oppermann #include <netinet/in_pcb.h>
9197d8d152SAndre Oppermann #include <netinet/ip_var.h>
9297d8d152SAndre Oppermann #ifdef INET6
9397d8d152SAndre Oppermann #include <netinet/ip6.h>
9497d8d152SAndre Oppermann #include <netinet6/ip6_var.h>
9597d8d152SAndre Oppermann #endif
9697d8d152SAndre Oppermann #include <netinet/tcp.h>
9797d8d152SAndre Oppermann #include <netinet/tcp_var.h>
984b79449eSBjoern A. Zeeb #include <netinet/tcp_hostcache.h>
9997d8d152SAndre Oppermann #ifdef INET6
10097d8d152SAndre Oppermann #include <netinet6/tcp6_var.h>
10197d8d152SAndre Oppermann #endif
10297d8d152SAndre Oppermann 
10397d8d152SAndre Oppermann #include <vm/uma.h>
10497d8d152SAndre Oppermann 
10597d8d152SAndre Oppermann /* Arbitrary values */
10697d8d152SAndre Oppermann #define TCP_HOSTCACHE_HASHSIZE		512
10797d8d152SAndre Oppermann #define TCP_HOSTCACHE_BUCKETLIMIT	30
10897d8d152SAndre Oppermann #define TCP_HOSTCACHE_EXPIRE		60*60	/* one hour */
10997d8d152SAndre Oppermann #define TCP_HOSTCACHE_PRUNE		5*60	/* every 5 minutes */
11097d8d152SAndre Oppermann 
1113e288e62SDimitry Andric static VNET_DEFINE(struct tcp_hostcache, tcp_hostcache);
1121e77c105SRobert Watson #define	V_tcp_hostcache		VNET(tcp_hostcache)
11382cea7e6SBjoern A. Zeeb 
1143e288e62SDimitry Andric static VNET_DEFINE(struct callout, tcp_hc_callout);
1151e77c105SRobert Watson #define	V_tcp_hc_callout	VNET(tcp_hc_callout)
11697d8d152SAndre Oppermann 
11797d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
11897d8d152SAndre Oppermann static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
11997d8d152SAndre Oppermann static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
12026456380SHiren Panchasara static int sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS);
121fffb9f1dSBjoern A. Zeeb static void tcp_hc_purge_internal(int);
12297d8d152SAndre Oppermann static void tcp_hc_purge(void *);
12397d8d152SAndre Oppermann 
1246472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0,
1250eb7cf4dSBrooks Davis     "TCP Host cache");
12697d8d152SAndre Oppermann 
127*8a56c645SHiren Panchasara VNET_DEFINE(int, tcp_use_hostcache) = 1;
128*8a56c645SHiren Panchasara #define V_tcp_use_hostcache  VNET(tcp_use_hostcache)
129*8a56c645SHiren Panchasara SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
130*8a56c645SHiren Panchasara     &VNET_NAME(tcp_use_hostcache), 0,
131*8a56c645SHiren Panchasara     "Enable the TCP hostcache");
132*8a56c645SHiren Panchasara 
1336df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN,
134eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.cache_limit), 0,
1358b615593SMarko Zec     "Overall entry limit for hostcache");
13697d8d152SAndre Oppermann 
1376df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN,
138eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.hashsize), 0,
1398b615593SMarko Zec     "Size of TCP hostcache hashtable");
14097d8d152SAndre Oppermann 
1416df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
1426df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0,
1438b615593SMarko Zec     "Per-bucket hash limit for hostcache");
14497d8d152SAndre Oppermann 
1456df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD,
146eddfbb76SRobert Watson      &VNET_NAME(tcp_hostcache.cache_count), 0,
1478b615593SMarko Zec     "Current number of entries in hostcache");
14897d8d152SAndre Oppermann 
1496df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW,
150eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.expire), 0,
1518b615593SMarko Zec     "Expire time of TCP hostcache entries");
15297d8d152SAndre Oppermann 
1536df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW,
154eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.prune), 0,
155eddfbb76SRobert Watson     "Time between purge runs");
156dba3c508SYaroslav Tykhiy 
1576df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW,
158eddfbb76SRobert Watson     &VNET_NAME(tcp_hostcache.purgeall), 0,
1598b615593SMarko Zec     "Expire all entires on next purge run");
16097d8d152SAndre Oppermann 
16197d8d152SAndre Oppermann SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
16297d8d152SAndre Oppermann     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
16397d8d152SAndre Oppermann     sysctl_tcp_hc_list, "A", "List of all hostcache entries");
16497d8d152SAndre Oppermann 
16526456380SHiren Panchasara SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, purgenow,
16626456380SHiren Panchasara     CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
16726456380SHiren Panchasara     sysctl_tcp_hc_purgenow, "I", "Immediately purge all entries");
16897d8d152SAndre Oppermann 
16997d8d152SAndre Oppermann static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
17097d8d152SAndre Oppermann 
17197d8d152SAndre Oppermann #define HOSTCACHE_HASH(ip) \
17297d8d152SAndre Oppermann 	(((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) &	\
173603724d3SBjoern A. Zeeb 	  V_tcp_hostcache.hashmask)
17497d8d152SAndre Oppermann 
17597d8d152SAndre Oppermann /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
17697d8d152SAndre Oppermann #define HOSTCACHE_HASH6(ip6)				\
17797d8d152SAndre Oppermann 	(((ip6)->s6_addr32[0] ^				\
17897d8d152SAndre Oppermann 	  (ip6)->s6_addr32[1] ^				\
17997d8d152SAndre Oppermann 	  (ip6)->s6_addr32[2] ^				\
18097d8d152SAndre Oppermann 	  (ip6)->s6_addr32[3]) &			\
181603724d3SBjoern A. Zeeb 	 V_tcp_hostcache.hashmask)
18297d8d152SAndre Oppermann 
18397d8d152SAndre Oppermann #define THC_LOCK(lp)		mtx_lock(lp)
18497d8d152SAndre Oppermann #define THC_UNLOCK(lp)		mtx_unlock(lp)
18597d8d152SAndre Oppermann 
18697d8d152SAndre Oppermann void
18797d8d152SAndre Oppermann tcp_hc_init(void)
18897d8d152SAndre Oppermann {
18932fe38f1SAndrey Zonov 	u_int cache_limit;
19097d8d152SAndre Oppermann 	int i;
19197d8d152SAndre Oppermann 
19297d8d152SAndre Oppermann 	/*
193e487a5e2SRobert Watson 	 * Initialize hostcache structures.
19497d8d152SAndre Oppermann 	 */
195603724d3SBjoern A. Zeeb 	V_tcp_hostcache.cache_count = 0;
196603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
197603724d3SBjoern A. Zeeb 	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
198603724d3SBjoern A. Zeeb 	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
199603724d3SBjoern A. Zeeb 	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
20097d8d152SAndre Oppermann 
20197d8d152SAndre Oppermann 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
202603724d3SBjoern A. Zeeb 	    &V_tcp_hostcache.hashsize);
203603724d3SBjoern A. Zeeb 	if (!powerof2(V_tcp_hostcache.hashsize)) {
20497d8d152SAndre Oppermann 		printf("WARNING: hostcache hash size is not a power of 2.\n");
205603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
20697d8d152SAndre Oppermann 	}
207603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
20897d8d152SAndre Oppermann 
20932fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
21032fe38f1SAndrey Zonov 	    &V_tcp_hostcache.bucket_limit);
21132fe38f1SAndrey Zonov 
21232fe38f1SAndrey Zonov 	cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
21332fe38f1SAndrey Zonov 	V_tcp_hostcache.cache_limit = cache_limit;
21432fe38f1SAndrey Zonov 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
21532fe38f1SAndrey Zonov 	    &V_tcp_hostcache.cache_limit);
21632fe38f1SAndrey Zonov 	if (V_tcp_hostcache.cache_limit > cache_limit)
21732fe38f1SAndrey Zonov 		V_tcp_hostcache.cache_limit = cache_limit;
21832fe38f1SAndrey Zonov 
21997d8d152SAndre Oppermann 	/*
220e487a5e2SRobert Watson 	 * Allocate the hash table.
22197d8d152SAndre Oppermann 	 */
222603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase = (struct hc_head *)
223603724d3SBjoern A. Zeeb 	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
22497d8d152SAndre Oppermann 		   M_HOSTCACHE, M_WAITOK | M_ZERO);
22597d8d152SAndre Oppermann 
22697d8d152SAndre Oppermann 	/*
227e487a5e2SRobert Watson 	 * Initialize the hash buckets.
22897d8d152SAndre Oppermann 	 */
229603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
230603724d3SBjoern A. Zeeb 		TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
231603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[i].hch_length = 0;
232603724d3SBjoern A. Zeeb 		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
23397d8d152SAndre Oppermann 			  NULL, MTX_DEF);
23497d8d152SAndre Oppermann 	}
23597d8d152SAndre Oppermann 
23697d8d152SAndre Oppermann 	/*
23797d8d152SAndre Oppermann 	 * Allocate the hostcache entries.
23897d8d152SAndre Oppermann 	 */
239ac957cd2SJulian Elischer 	V_tcp_hostcache.zone =
240ac957cd2SJulian Elischer 	    uma_zcreate("hostcache", sizeof(struct hc_metrics),
2414efb805cSAndre Oppermann 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
242603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
24397d8d152SAndre Oppermann 
24497d8d152SAndre Oppermann 	/*
24597d8d152SAndre Oppermann 	 * Set up periodic cache cleanup.
24697d8d152SAndre Oppermann 	 */
247fd90e2edSJung-uk Kim 	callout_init(&V_tcp_hc_callout, 1);
248ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
24921ca7b57SMarko Zec 	    tcp_hc_purge, curvnet);
25097d8d152SAndre Oppermann }
25197d8d152SAndre Oppermann 
252bc29160dSMarko Zec #ifdef VIMAGE
253bc29160dSMarko Zec void
254bc29160dSMarko Zec tcp_hc_destroy(void)
255bc29160dSMarko Zec {
256fffb9f1dSBjoern A. Zeeb 	int i;
257bc29160dSMarko Zec 
258bc29160dSMarko Zec 	callout_drain(&V_tcp_hc_callout);
259fffb9f1dSBjoern A. Zeeb 
260fffb9f1dSBjoern A. Zeeb 	/* Purge all hc entries. */
261fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(1);
262fffb9f1dSBjoern A. Zeeb 
263fffb9f1dSBjoern A. Zeeb 	/* Free the uma zone and the allocated hash table. */
264fffb9f1dSBjoern A. Zeeb 	uma_zdestroy(V_tcp_hostcache.zone);
265fffb9f1dSBjoern A. Zeeb 
266fffb9f1dSBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++)
267fffb9f1dSBjoern A. Zeeb 		mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx);
268fffb9f1dSBjoern A. Zeeb 	free(V_tcp_hostcache.hashbase, M_HOSTCACHE);
269bc29160dSMarko Zec }
270bc29160dSMarko Zec #endif
271bc29160dSMarko Zec 
27297d8d152SAndre Oppermann /*
27397d8d152SAndre Oppermann  * Internal function: look up an entry in the hostcache or return NULL.
27497d8d152SAndre Oppermann  *
27597d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
27697d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
27797d8d152SAndre Oppermann  */
27897d8d152SAndre Oppermann static struct hc_metrics *
27997d8d152SAndre Oppermann tcp_hc_lookup(struct in_conninfo *inc)
28097d8d152SAndre Oppermann {
28197d8d152SAndre Oppermann 	int hash;
28297d8d152SAndre Oppermann 	struct hc_head *hc_head;
28397d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
28497d8d152SAndre Oppermann 
285*8a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
286*8a56c645SHiren Panchasara 		return NULL;
287*8a56c645SHiren Panchasara 
28897d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
28997d8d152SAndre Oppermann 
29097d8d152SAndre Oppermann 	/*
29197d8d152SAndre Oppermann 	 * Hash the foreign ip address.
29297d8d152SAndre Oppermann 	 */
293dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
29497d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
29597d8d152SAndre Oppermann 	else
29697d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
29797d8d152SAndre Oppermann 
298603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
29997d8d152SAndre Oppermann 
30097d8d152SAndre Oppermann 	/*
301e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
302e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
303e487a5e2SRobert Watson 	 * done.
30497d8d152SAndre Oppermann 	 */
30597d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
30697d8d152SAndre Oppermann 
30797d8d152SAndre Oppermann 	/*
308e487a5e2SRobert Watson 	 * Iterate through entries in bucket row looking for a match.
30997d8d152SAndre Oppermann 	 */
31097d8d152SAndre Oppermann 	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
311dcdb4371SBjoern A. Zeeb 		if (inc->inc_flags & INC_ISIPV6) {
312028bdf28SAndrey V. Elsukov 			/* XXX: check ip6_zoneid */
31397d8d152SAndre Oppermann 			if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
31497d8d152SAndre Oppermann 			    sizeof(inc->inc6_faddr)) == 0)
31597d8d152SAndre Oppermann 				return hc_entry;
31697d8d152SAndre Oppermann 		} else {
31797d8d152SAndre Oppermann 			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
31897d8d152SAndre Oppermann 			    sizeof(inc->inc_faddr)) == 0)
31997d8d152SAndre Oppermann 				return hc_entry;
32097d8d152SAndre Oppermann 		}
32197d8d152SAndre Oppermann 	}
32297d8d152SAndre Oppermann 
32397d8d152SAndre Oppermann 	/*
324e487a5e2SRobert Watson 	 * We were unsuccessful and didn't find anything.
32597d8d152SAndre Oppermann 	 */
32697d8d152SAndre Oppermann 	THC_UNLOCK(&hc_head->hch_mtx);
32797d8d152SAndre Oppermann 	return NULL;
32897d8d152SAndre Oppermann }
32997d8d152SAndre Oppermann 
33097d8d152SAndre Oppermann /*
331e487a5e2SRobert Watson  * Internal function: insert an entry into the hostcache or return NULL if
332e487a5e2SRobert Watson  * unable to allocate a new one.
33397d8d152SAndre Oppermann  *
33497d8d152SAndre Oppermann  * If an entry has been returned, the caller becomes responsible for
33597d8d152SAndre Oppermann  * unlocking the bucket row after he is done reading/modifying the entry.
33697d8d152SAndre Oppermann  */
33797d8d152SAndre Oppermann static struct hc_metrics *
33897d8d152SAndre Oppermann tcp_hc_insert(struct in_conninfo *inc)
33997d8d152SAndre Oppermann {
34097d8d152SAndre Oppermann 	int hash;
34197d8d152SAndre Oppermann 	struct hc_head *hc_head;
34297d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
34397d8d152SAndre Oppermann 
344*8a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
345*8a56c645SHiren Panchasara 		return NULL;
346*8a56c645SHiren Panchasara 
34797d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
34897d8d152SAndre Oppermann 
34997d8d152SAndre Oppermann 	/*
350e487a5e2SRobert Watson 	 * Hash the foreign ip address.
35197d8d152SAndre Oppermann 	 */
352dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6)
35397d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
35497d8d152SAndre Oppermann 	else
35597d8d152SAndre Oppermann 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
35697d8d152SAndre Oppermann 
357603724d3SBjoern A. Zeeb 	hc_head = &V_tcp_hostcache.hashbase[hash];
35897d8d152SAndre Oppermann 
35997d8d152SAndre Oppermann 	/*
360e487a5e2SRobert Watson 	 * Acquire lock for this bucket row; we release the lock if we don't
361e487a5e2SRobert Watson 	 * find an entry, otherwise the caller has to unlock after he is
362e487a5e2SRobert Watson 	 * done.
36397d8d152SAndre Oppermann 	 */
36497d8d152SAndre Oppermann 	THC_LOCK(&hc_head->hch_mtx);
36597d8d152SAndre Oppermann 
36697d8d152SAndre Oppermann 	/*
367e487a5e2SRobert Watson 	 * If the bucket limit is reached, reuse the least-used element.
36897d8d152SAndre Oppermann 	 */
369603724d3SBjoern A. Zeeb 	if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
370603724d3SBjoern A. Zeeb 	    V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) {
37197d8d152SAndre Oppermann 		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
37297d8d152SAndre Oppermann 		/*
37397d8d152SAndre Oppermann 		 * At first we were dropping the last element, just to
374e487a5e2SRobert Watson 		 * reacquire it in the next two lines again, which isn't very
375e487a5e2SRobert Watson 		 * efficient.  Instead just reuse the least used element.
376e487a5e2SRobert Watson 		 * We may drop something that is still "in-use" but we can be
377e487a5e2SRobert Watson 		 * "lossy".
37845024be0SAndre Oppermann 		 * Just give up if this bucket row is empty and we don't have
37945024be0SAndre Oppermann 		 * anything to replace.
38097d8d152SAndre Oppermann 		 */
38145024be0SAndre Oppermann 		if (hc_entry == NULL) {
38245024be0SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
38345024be0SAndre Oppermann 			return NULL;
38445024be0SAndre Oppermann 		}
38597d8d152SAndre Oppermann 		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
386603724d3SBjoern A. Zeeb 		V_tcp_hostcache.hashbase[hash].hch_length--;
387603724d3SBjoern A. Zeeb 		V_tcp_hostcache.cache_count--;
38878b50714SRobert Watson 		TCPSTAT_INC(tcps_hc_bucketoverflow);
389cd6c4060SAndre Oppermann #if 0
390603724d3SBjoern A. Zeeb 		uma_zfree(V_tcp_hostcache.zone, hc_entry);
391cd6c4060SAndre Oppermann #endif
39297d8d152SAndre Oppermann 	} else {
39397d8d152SAndre Oppermann 		/*
394e487a5e2SRobert Watson 		 * Allocate a new entry, or balk if not possible.
39597d8d152SAndre Oppermann 		 */
396603724d3SBjoern A. Zeeb 		hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
39797d8d152SAndre Oppermann 		if (hc_entry == NULL) {
39897d8d152SAndre Oppermann 			THC_UNLOCK(&hc_head->hch_mtx);
39997d8d152SAndre Oppermann 			return NULL;
40097d8d152SAndre Oppermann 		}
40197d8d152SAndre Oppermann 	}
40297d8d152SAndre Oppermann 
40397d8d152SAndre Oppermann 	/*
404e487a5e2SRobert Watson 	 * Initialize basic information of hostcache entry.
40597d8d152SAndre Oppermann 	 */
40697d8d152SAndre Oppermann 	bzero(hc_entry, sizeof(*hc_entry));
407028bdf28SAndrey V. Elsukov 	if (inc->inc_flags & INC_ISIPV6) {
408028bdf28SAndrey V. Elsukov 		hc_entry->ip6 = inc->inc6_faddr;
409028bdf28SAndrey V. Elsukov 		hc_entry->ip6_zoneid = inc->inc6_zoneid;
410028bdf28SAndrey V. Elsukov 	} else
41197d8d152SAndre Oppermann 		hc_entry->ip4 = inc->inc_faddr;
41297d8d152SAndre Oppermann 	hc_entry->rmx_head = hc_head;
413603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire;
41497d8d152SAndre Oppermann 
41597d8d152SAndre Oppermann 	/*
416e487a5e2SRobert Watson 	 * Put it upfront.
41797d8d152SAndre Oppermann 	 */
41897d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
419603724d3SBjoern A. Zeeb 	V_tcp_hostcache.hashbase[hash].hch_length++;
420603724d3SBjoern A. Zeeb 	V_tcp_hostcache.cache_count++;
42178b50714SRobert Watson 	TCPSTAT_INC(tcps_hc_added);
42297d8d152SAndre Oppermann 
42397d8d152SAndre Oppermann 	return hc_entry;
42497d8d152SAndre Oppermann }
42597d8d152SAndre Oppermann 
42697d8d152SAndre Oppermann /*
42797d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and fill out the
428e487a5e2SRobert Watson  * supplied TCP metrics structure.  Fills in NULL when no entry was found or
429e487a5e2SRobert Watson  * a value is not set.
43097d8d152SAndre Oppermann  */
43197d8d152SAndre Oppermann void
43297d8d152SAndre Oppermann tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
43397d8d152SAndre Oppermann {
43497d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
43597d8d152SAndre Oppermann 
436*8a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
437*8a56c645SHiren Panchasara 		return;
438*8a56c645SHiren Panchasara 
43997d8d152SAndre Oppermann 	/*
440e487a5e2SRobert Watson 	 * Find the right bucket.
44197d8d152SAndre Oppermann 	 */
44297d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
44397d8d152SAndre Oppermann 
44497d8d152SAndre Oppermann 	/*
445e487a5e2SRobert Watson 	 * If we don't have an existing object.
44697d8d152SAndre Oppermann 	 */
44797d8d152SAndre Oppermann 	if (hc_entry == NULL) {
44897d8d152SAndre Oppermann 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
44997d8d152SAndre Oppermann 		return;
45097d8d152SAndre Oppermann 	}
45197d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
452603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
45397d8d152SAndre Oppermann 
45497d8d152SAndre Oppermann 	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
45597d8d152SAndre Oppermann 	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
45697d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
45797d8d152SAndre Oppermann 	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
45897d8d152SAndre Oppermann 	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
45997d8d152SAndre Oppermann 	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
46097d8d152SAndre Oppermann 	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
46197d8d152SAndre Oppermann 
46297d8d152SAndre Oppermann 	/*
463e487a5e2SRobert Watson 	 * Unlock bucket row.
46497d8d152SAndre Oppermann 	 */
46597d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
46697d8d152SAndre Oppermann }
46797d8d152SAndre Oppermann 
46897d8d152SAndre Oppermann /*
46997d8d152SAndre Oppermann  * External function: look up an entry in the hostcache and return the
470*8a56c645SHiren Panchasara  * discovered path MTU.  Returns 0 if no entry is found or value is not
4716fbed4afSRobert Watson  * set.
47297d8d152SAndre Oppermann  */
47397d8d152SAndre Oppermann u_long
47497d8d152SAndre Oppermann tcp_hc_getmtu(struct in_conninfo *inc)
47597d8d152SAndre Oppermann {
47697d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
47797d8d152SAndre Oppermann 	u_long mtu;
47897d8d152SAndre Oppermann 
479*8a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
480*8a56c645SHiren Panchasara 		return 0;
481*8a56c645SHiren Panchasara 
48297d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
48397d8d152SAndre Oppermann 	if (hc_entry == NULL) {
48497d8d152SAndre Oppermann 		return 0;
48597d8d152SAndre Oppermann 	}
48697d8d152SAndre Oppermann 	hc_entry->rmx_hits++;
487603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
48897d8d152SAndre Oppermann 
48997d8d152SAndre Oppermann 	mtu = hc_entry->rmx_mtu;
49097d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
49197d8d152SAndre Oppermann 	return mtu;
49297d8d152SAndre Oppermann }
49397d8d152SAndre Oppermann 
49497d8d152SAndre Oppermann /*
495e487a5e2SRobert Watson  * External function: update the MTU value of an entry in the hostcache.
49697d8d152SAndre Oppermann  * Creates a new entry if none was found.
49797d8d152SAndre Oppermann  */
49897d8d152SAndre Oppermann void
49997d8d152SAndre Oppermann tcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
50097d8d152SAndre Oppermann {
50197d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
50297d8d152SAndre Oppermann 
503*8a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
504*8a56c645SHiren Panchasara 		return;
505*8a56c645SHiren Panchasara 
50697d8d152SAndre Oppermann 	/*
507e487a5e2SRobert Watson 	 * Find the right bucket.
50897d8d152SAndre Oppermann 	 */
50997d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
51097d8d152SAndre Oppermann 
51197d8d152SAndre Oppermann 	/*
512e487a5e2SRobert Watson 	 * If we don't have an existing object, try to insert a new one.
51397d8d152SAndre Oppermann 	 */
51497d8d152SAndre Oppermann 	if (hc_entry == NULL) {
51597d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
51697d8d152SAndre Oppermann 		if (hc_entry == NULL)
51797d8d152SAndre Oppermann 			return;
51897d8d152SAndre Oppermann 	}
51997d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
520603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
52197d8d152SAndre Oppermann 
52297d8d152SAndre Oppermann 	hc_entry->rmx_mtu = mtu;
52397d8d152SAndre Oppermann 
52497d8d152SAndre Oppermann 	/*
525e487a5e2SRobert Watson 	 * Put it upfront so we find it faster next time.
52697d8d152SAndre Oppermann 	 */
52797d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
52897d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
52997d8d152SAndre Oppermann 
53097d8d152SAndre Oppermann 	/*
531e487a5e2SRobert Watson 	 * Unlock bucket row.
53297d8d152SAndre Oppermann 	 */
53397d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
53497d8d152SAndre Oppermann }
53597d8d152SAndre Oppermann 
53697d8d152SAndre Oppermann /*
537e487a5e2SRobert Watson  * External function: update the TCP metrics of an entry in the hostcache.
53897d8d152SAndre Oppermann  * Creates a new entry if none was found.
53997d8d152SAndre Oppermann  */
54097d8d152SAndre Oppermann void
54197d8d152SAndre Oppermann tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
54297d8d152SAndre Oppermann {
54397d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
54497d8d152SAndre Oppermann 
545*8a56c645SHiren Panchasara 	if (!V_tcp_use_hostcache)
546*8a56c645SHiren Panchasara 		return;
547*8a56c645SHiren Panchasara 
54897d8d152SAndre Oppermann 	hc_entry = tcp_hc_lookup(inc);
54997d8d152SAndre Oppermann 	if (hc_entry == NULL) {
55097d8d152SAndre Oppermann 		hc_entry = tcp_hc_insert(inc);
55197d8d152SAndre Oppermann 		if (hc_entry == NULL)
55297d8d152SAndre Oppermann 			return;
55397d8d152SAndre Oppermann 	}
55497d8d152SAndre Oppermann 	hc_entry->rmx_updates++;
555603724d3SBjoern A. Zeeb 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
55697d8d152SAndre Oppermann 
55797d8d152SAndre Oppermann 	if (hcml->rmx_rtt != 0) {
55897d8d152SAndre Oppermann 		if (hc_entry->rmx_rtt == 0)
55997d8d152SAndre Oppermann 			hc_entry->rmx_rtt = hcml->rmx_rtt;
56097d8d152SAndre Oppermann 		else
56197d8d152SAndre Oppermann 			hc_entry->rmx_rtt =
56297d8d152SAndre Oppermann 			    (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
56378b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrtt);
56497d8d152SAndre Oppermann 	}
56597d8d152SAndre Oppermann 	if (hcml->rmx_rttvar != 0) {
56697d8d152SAndre Oppermann 	        if (hc_entry->rmx_rttvar == 0)
56797d8d152SAndre Oppermann 			hc_entry->rmx_rttvar = hcml->rmx_rttvar;
56897d8d152SAndre Oppermann 		else
56997d8d152SAndre Oppermann 			hc_entry->rmx_rttvar =
57097d8d152SAndre Oppermann 			    (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
57178b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedrttvar);
57297d8d152SAndre Oppermann 	}
57397d8d152SAndre Oppermann 	if (hcml->rmx_ssthresh != 0) {
57497d8d152SAndre Oppermann 		if (hc_entry->rmx_ssthresh == 0)
57597d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
57697d8d152SAndre Oppermann 		else
57797d8d152SAndre Oppermann 			hc_entry->rmx_ssthresh =
57897d8d152SAndre Oppermann 			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
57978b50714SRobert Watson 		TCPSTAT_INC(tcps_cachedssthresh);
58097d8d152SAndre Oppermann 	}
58197d8d152SAndre Oppermann 	if (hcml->rmx_cwnd != 0) {
58297d8d152SAndre Oppermann 		if (hc_entry->rmx_cwnd == 0)
58397d8d152SAndre Oppermann 			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
58497d8d152SAndre Oppermann 		else
58597d8d152SAndre Oppermann 			hc_entry->rmx_cwnd =
58697d8d152SAndre Oppermann 			    (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
58778b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedcwnd); */
58897d8d152SAndre Oppermann 	}
58997d8d152SAndre Oppermann 	if (hcml->rmx_sendpipe != 0) {
59097d8d152SAndre Oppermann 		if (hc_entry->rmx_sendpipe == 0)
59197d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
59297d8d152SAndre Oppermann 		else
59397d8d152SAndre Oppermann 			hc_entry->rmx_sendpipe =
59497d8d152SAndre Oppermann 			    (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
59578b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedsendpipe); */
59697d8d152SAndre Oppermann 	}
59797d8d152SAndre Oppermann 	if (hcml->rmx_recvpipe != 0) {
59897d8d152SAndre Oppermann 		if (hc_entry->rmx_recvpipe == 0)
59997d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
60097d8d152SAndre Oppermann 		else
60197d8d152SAndre Oppermann 			hc_entry->rmx_recvpipe =
60297d8d152SAndre Oppermann 			    (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
60378b50714SRobert Watson 		/* TCPSTAT_INC(tcps_cachedrecvpipe); */
60497d8d152SAndre Oppermann 	}
60597d8d152SAndre Oppermann 
60697d8d152SAndre Oppermann 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
60797d8d152SAndre Oppermann 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
60897d8d152SAndre Oppermann 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
60997d8d152SAndre Oppermann }
61097d8d152SAndre Oppermann 
61197d8d152SAndre Oppermann /*
61297d8d152SAndre Oppermann  * Sysctl function: prints the list and values of all hostcache entries in
61397d8d152SAndre Oppermann  * unsorted order.
61497d8d152SAndre Oppermann  */
61597d8d152SAndre Oppermann static int
61697d8d152SAndre Oppermann sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
61797d8d152SAndre Oppermann {
618751ccc42SIan Lepore 	const int linesize = 128;
619002d4558SJohn Baldwin 	struct sbuf sb;
620002d4558SJohn Baldwin 	int i, error;
62197d8d152SAndre Oppermann 	struct hc_metrics *hc_entry;
6221d54aa3bSBjoern A. Zeeb #ifdef INET6
6231d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
6241d54aa3bSBjoern A. Zeeb #endif
62597d8d152SAndre Oppermann 
626dcdeb95fSIan Lepore 	sbuf_new(&sb, NULL, linesize * (V_tcp_hostcache.cache_count + 1),
627dcdeb95fSIan Lepore 		SBUF_INCLUDENUL);
62897d8d152SAndre Oppermann 
629002d4558SJohn Baldwin 	sbuf_printf(&sb,
6304d163382SHiren Panchasara 	        "\nIP address        MTU  SSTRESH      RTT   RTTVAR "
63197d8d152SAndre Oppermann 		"    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
63297d8d152SAndre Oppermann 
63397d8d152SAndre Oppermann #define msec(u) (((u) + 500) / 1000)
634603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
635603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
636603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
63797d8d152SAndre Oppermann 			      rmx_q) {
638002d4558SJohn Baldwin 			sbuf_printf(&sb,
6394d163382SHiren Panchasara 			    "%-15s %5lu %8lu %6lums %6lums %8lu %8lu %8lu %4lu "
6404d163382SHiren Panchasara 			    "%4lu %4i\n",
64197d8d152SAndre Oppermann 			    hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
64297d8d152SAndre Oppermann #ifdef INET6
6431d54aa3bSBjoern A. Zeeb 				ip6_sprintf(ip6buf, &hc_entry->ip6),
64497d8d152SAndre Oppermann #else
64597d8d152SAndre Oppermann 				"IPv6?",
64697d8d152SAndre Oppermann #endif
64797d8d152SAndre Oppermann 			    hc_entry->rmx_mtu,
64897d8d152SAndre Oppermann 			    hc_entry->rmx_ssthresh,
64997d8d152SAndre Oppermann 			    msec(hc_entry->rmx_rtt *
65097d8d152SAndre Oppermann 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
65197d8d152SAndre Oppermann 			    msec(hc_entry->rmx_rttvar *
6523a288e90SMikolaj Golub 				(RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))),
65397d8d152SAndre Oppermann 			    hc_entry->rmx_cwnd,
65497d8d152SAndre Oppermann 			    hc_entry->rmx_sendpipe,
65597d8d152SAndre Oppermann 			    hc_entry->rmx_recvpipe,
65697d8d152SAndre Oppermann 			    hc_entry->rmx_hits,
65797d8d152SAndre Oppermann 			    hc_entry->rmx_updates,
65897d8d152SAndre Oppermann 			    hc_entry->rmx_expire);
65997d8d152SAndre Oppermann 		}
660603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
66197d8d152SAndre Oppermann 	}
66297d8d152SAndre Oppermann #undef msec
663751ccc42SIan Lepore 	error = sbuf_finish(&sb);
664dcdeb95fSIan Lepore 	if (error == 0)
665dcdeb95fSIan Lepore 		error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
666002d4558SJohn Baldwin 	sbuf_delete(&sb);
66797d8d152SAndre Oppermann 	return(error);
66897d8d152SAndre Oppermann }
66997d8d152SAndre Oppermann 
67097d8d152SAndre Oppermann /*
671fffb9f1dSBjoern A. Zeeb  * Caller has to make sure the curvnet is set properly.
67297d8d152SAndre Oppermann  */
67397d8d152SAndre Oppermann static void
674fffb9f1dSBjoern A. Zeeb tcp_hc_purge_internal(int all)
67597d8d152SAndre Oppermann {
676b62dccc7SAndre Oppermann 	struct hc_metrics *hc_entry, *hc_next;
67797d8d152SAndre Oppermann 	int i;
67897d8d152SAndre Oppermann 
679603724d3SBjoern A. Zeeb 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
680603724d3SBjoern A. Zeeb 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
681ac957cd2SJulian Elischer 		TAILQ_FOREACH_SAFE(hc_entry,
6825ed3800eSJulian Elischer 		    &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
68397d8d152SAndre Oppermann 			if (all || hc_entry->rmx_expire <= 0) {
684603724d3SBjoern A. Zeeb 				TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
68597d8d152SAndre Oppermann 					      hc_entry, rmx_q);
686603724d3SBjoern A. Zeeb 				uma_zfree(V_tcp_hostcache.zone, hc_entry);
687603724d3SBjoern A. Zeeb 				V_tcp_hostcache.hashbase[i].hch_length--;
688603724d3SBjoern A. Zeeb 				V_tcp_hostcache.cache_count--;
68997d8d152SAndre Oppermann 			} else
690603724d3SBjoern A. Zeeb 				hc_entry->rmx_expire -= V_tcp_hostcache.prune;
69197d8d152SAndre Oppermann 		}
692603724d3SBjoern A. Zeeb 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
69397d8d152SAndre Oppermann 	}
694fffb9f1dSBjoern A. Zeeb }
695fffb9f1dSBjoern A. Zeeb 
696fffb9f1dSBjoern A. Zeeb /*
697fffb9f1dSBjoern A. Zeeb  * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
698fffb9f1dSBjoern A. Zeeb  * periodically from the callout.
699fffb9f1dSBjoern A. Zeeb  */
700fffb9f1dSBjoern A. Zeeb static void
701fffb9f1dSBjoern A. Zeeb tcp_hc_purge(void *arg)
702fffb9f1dSBjoern A. Zeeb {
703fffb9f1dSBjoern A. Zeeb 	CURVNET_SET((struct vnet *) arg);
704fffb9f1dSBjoern A. Zeeb 	int all = 0;
705fffb9f1dSBjoern A. Zeeb 
706fffb9f1dSBjoern A. Zeeb 	if (V_tcp_hostcache.purgeall) {
707fffb9f1dSBjoern A. Zeeb 		all = 1;
708fffb9f1dSBjoern A. Zeeb 		V_tcp_hostcache.purgeall = 0;
709fffb9f1dSBjoern A. Zeeb 	}
710fffb9f1dSBjoern A. Zeeb 
711fffb9f1dSBjoern A. Zeeb 	tcp_hc_purge_internal(all);
712ac957cd2SJulian Elischer 
713ac957cd2SJulian Elischer 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
714ac957cd2SJulian Elischer 	    tcp_hc_purge, arg);
71521ca7b57SMarko Zec 	CURVNET_RESTORE();
71697d8d152SAndre Oppermann }
71726456380SHiren Panchasara 
71826456380SHiren Panchasara /*
71926456380SHiren Panchasara  * Expire and purge all entries in hostcache immediately.
72026456380SHiren Panchasara  */
72126456380SHiren Panchasara static int
72226456380SHiren Panchasara sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS)
72326456380SHiren Panchasara {
72426456380SHiren Panchasara 	int error, val;
72526456380SHiren Panchasara 
72626456380SHiren Panchasara 	val = 0;
72726456380SHiren Panchasara 	error = sysctl_handle_int(oidp, &val, 0, req);
72826456380SHiren Panchasara 	if (error || !req->newptr)
72926456380SHiren Panchasara 		return (error);
73026456380SHiren Panchasara 
73126456380SHiren Panchasara 	tcp_hc_purge_internal(1);
73226456380SHiren Panchasara 
73326456380SHiren Panchasara 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
73426456380SHiren Panchasara 	    tcp_hc_purge, curvnet);
73526456380SHiren Panchasara 
73626456380SHiren Panchasara 	return (0);
73726456380SHiren Panchasara }
738