tcp_hostcache.c revision 181888
1226586Sdim/*-
2226586Sdim * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG
3226586Sdim * All rights reserved.
4226586Sdim *
5226586Sdim * Redistribution and use in source and binary forms, with or without
6226586Sdim * modification, are permitted provided that the following conditions
7226586Sdim * are met:
8226586Sdim * 1. Redistributions of source code must retain the above copyright
9226586Sdim *    notice, this list of conditions and the following disclaimer.
10226586Sdim * 2. Redistributions in binary form must reproduce the above copyright
11226586Sdim *    notice, this list of conditions and the following disclaimer in the
12234353Sdim *    documentation and/or other materials provided with the distribution.
13234353Sdim * 3. The name of the author may not be used to endorse or promote
14226586Sdim *    products derived from this software without specific prior written
15226586Sdim *    permission.
16226586Sdim *
17226586Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18226586Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19226586Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20226586Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21249423Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22249423Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23226586Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24226586Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25226586Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26226586Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27226586Sdim * SUCH DAMAGE.
28226586Sdim */
29226586Sdim
30226586Sdim/*
31226586Sdim * The tcp_hostcache moves the tcp-specific cached metrics from the routing
32226586Sdim * table to a dedicated structure indexed by the remote IP address.  It keeps
33226586Sdim * information on the measured TCP parameters of past TCP sessions to allow
34226586Sdim * better initial start values to be used with later connections to/from the
35226586Sdim * same source.  Depending on the network parameters (delay, bandwidth, max
36243830Sdim * MTU, congestion window) between local and remote sites, this can lead to
37243830Sdim * significant speed-ups for new TCP connections after the first one.
38243830Sdim *
39243830Sdim * Due to the tcp_hostcache, all TCP-specific metrics information in the
40243830Sdim * routing table has been removed.  The inpcb no longer keeps a pointer to
41243830Sdim * the routing entry, and protocol-initiated route cloning has been removed
42243830Sdim * as well.  With these changes, the routing table has gone back to being
43243830Sdim * more lightwight and only carries information related to packet forwarding.
44243830Sdim *
45243830Sdim * tcp_hostcache is designed for multiple concurrent access in SMP
46243830Sdim * environments and high contention.  All bucket rows have their own lock and
47243830Sdim * thus multiple lookups and modifies can be done at the same time as long as
48243830Sdim * they are in different bucket rows.  If a request for insertion of a new
49243830Sdim * record can't be satisfied, it simply returns an empty structure.  Nobody
50243830Sdim * and nothing outside of tcp_hostcache.c will ever point directly to any
51243830Sdim * entry in the tcp_hostcache.  All communication is done in an
52243830Sdim * object-oriented way and only functions of tcp_hostcache will manipulate
53243830Sdim * hostcache entries.  Otherwise, we are unable to achieve good behaviour in
54243830Sdim * concurrent access situations.  Since tcp_hostcache is only caching
55243830Sdim * information, there are no fatal consequences if we either can't satisfy
56226586Sdim * any particular request or have to drop/overwrite an existing entry because
57226586Sdim * of bucket limit memory constrains.
58226586Sdim */
59226586Sdim
60226586Sdim/*
61226586Sdim * Many thanks to jlemon for basic structure of tcp_syncache which is being
62226586Sdim * followed here.
63226586Sdim */
64226586Sdim
65226586Sdim#include <sys/cdefs.h>
66226586Sdim__FBSDID("$FreeBSD: head/sys/netinet/tcp_hostcache.c 181888 2008-08-20 01:24:55Z julian $");
67226586Sdim
68226586Sdim#include "opt_inet6.h"
69226586Sdim
70226586Sdim#include <sys/param.h>
71226586Sdim#include <sys/systm.h>
72226586Sdim#include <sys/kernel.h>
73226586Sdim#include <sys/lock.h>
74226586Sdim#include <sys/mutex.h>
75226586Sdim#include <sys/malloc.h>
76226586Sdim#include <sys/socket.h>
77226586Sdim#include <sys/socketvar.h>
78226586Sdim#include <sys/sysctl.h>
79226586Sdim#include <sys/vimage.h>
80226586Sdim
81226586Sdim#include <net/if.h>
82226586Sdim
83226586Sdim#include <netinet/in.h>
84226586Sdim#include <netinet/in_systm.h>
85226586Sdim#include <netinet/ip.h>
86226586Sdim#include <netinet/in_var.h>
87226586Sdim#include <netinet/in_pcb.h>
88226586Sdim#include <netinet/ip_var.h>
89226586Sdim#ifdef INET6
90226586Sdim#include <netinet/ip6.h>
91226586Sdim#include <netinet6/ip6_var.h>
92226586Sdim#endif
93226586Sdim#include <netinet/tcp.h>
94226586Sdim#include <netinet/tcp_var.h>
95226586Sdim#ifdef INET6
96243830Sdim#include <netinet6/tcp6_var.h>
97243830Sdim#endif
98243830Sdim
99243830Sdim#include <vm/uma.h>
100243830Sdim
101243830Sdim
102243830SdimTAILQ_HEAD(hc_qhead, hc_metrics);
103243830Sdim
104226586Sdimstruct hc_head {
105243830Sdim	struct hc_qhead	hch_bucket;
106226586Sdim	u_int		hch_length;
107226586Sdim	struct mtx	hch_mtx;
108226586Sdim};
109226586Sdim
110226586Sdimstruct hc_metrics {
111226586Sdim	/* housekeeping */
112226586Sdim	TAILQ_ENTRY(hc_metrics) rmx_q;
113226586Sdim	struct	hc_head *rmx_head; /* head of bucket tail queue */
114226586Sdim	struct	in_addr ip4;	/* IP address */
115226586Sdim	struct	in6_addr ip6;	/* IP6 address */
116226586Sdim	/* endpoint specific values for TCP */
117226586Sdim	u_long	rmx_mtu;	/* MTU for this path */
118226586Sdim	u_long	rmx_ssthresh;	/* outbound gateway buffer limit */
119226586Sdim	u_long	rmx_rtt;	/* estimated round trip time */
120226586Sdim	u_long	rmx_rttvar;	/* estimated rtt variance */
121226586Sdim	u_long	rmx_bandwidth;	/* estimated bandwidth */
122226586Sdim	u_long	rmx_cwnd;	/* congestion window */
123226586Sdim	u_long	rmx_sendpipe;	/* outbound delay-bandwidth product */
124226586Sdim	u_long	rmx_recvpipe;	/* inbound delay-bandwidth product */
125226586Sdim	/* TCP hostcache internal data */
126226586Sdim	int	rmx_expire;	/* lifetime for object */
127226586Sdim	u_long	rmx_hits;	/* number of hits */
128226586Sdim	u_long	rmx_updates;	/* number of updates */
129226586Sdim};
130226586Sdim
131226586Sdim/* Arbitrary values */
132226586Sdim#define TCP_HOSTCACHE_HASHSIZE		512
133226586Sdim#define TCP_HOSTCACHE_BUCKETLIMIT	30
134226586Sdim#define TCP_HOSTCACHE_EXPIRE		60*60	/* one hour */
135226586Sdim#define TCP_HOSTCACHE_PRUNE		5*60	/* every 5 minutes */
136226586Sdim
137226586Sdimstruct tcp_hostcache {
138226586Sdim	struct	hc_head *hashbase;
139226586Sdim	uma_zone_t zone;
140226586Sdim	u_int	hashsize;
141226586Sdim	u_int	hashmask;
142226586Sdim	u_int	bucket_limit;
143226586Sdim	u_int	cache_count;
144226586Sdim	u_int	cache_limit;
145226586Sdim	int	expire;
146226586Sdim	int	prune;
147243830Sdim	int	purgeall;
148226586Sdim};
149226586Sdimstatic struct tcp_hostcache tcp_hostcache;
150226586Sdim
151226586Sdimstatic struct callout tcp_hc_callout;
152226586Sdim
153226586Sdimstatic struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
154226586Sdimstatic struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
155226586Sdimstatic int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
156226586Sdimstatic void tcp_hc_purge(void *);
157226586Sdim
158226586SdimSYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0, "TCP Host cache");
159226586Sdim
160226586SdimSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_RDTUN,
161226586Sdim    &tcp_hostcache.cache_limit, 0, "Overall entry limit for hostcache");
162226586Sdim
163226586SdimSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_RDTUN,
164226586Sdim    &tcp_hostcache.hashsize, 0, "Size of TCP hostcache hashtable");
165226586Sdim
166226586SdimSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, CTLFLAG_RDTUN,
167226586Sdim    &tcp_hostcache.bucket_limit, 0, "Per-bucket hash limit for hostcache");
168226586Sdim
169226586SdimSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_RD,
170249423Sdim    &tcp_hostcache.cache_count, 0, "Current number of entries in hostcache");
171226586Sdim
172226586SdimSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_RW,
173226586Sdim    &tcp_hostcache.expire, 0, "Expire time of TCP hostcache entries");
174226586Sdim
175226586SdimSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_RW,
176226586Sdim     &tcp_hostcache.prune, 0, "Time between purge runs");
177226586Sdim
178226586SdimSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_RW,
179226586Sdim    &tcp_hostcache.purgeall, 0, "Expire all entires on next purge run");
180226586Sdim
181226586SdimSYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
182226586Sdim    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
183226586Sdim    sysctl_tcp_hc_list, "A", "List of all hostcache entries");
184226586Sdim
185243830Sdim
186226586Sdimstatic MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
187226586Sdim
188226586Sdim#define HOSTCACHE_HASH(ip) \
189226586Sdim	(((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) &	\
190226586Sdim	  V_tcp_hostcache.hashmask)
191226586Sdim
192226586Sdim/* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
193226586Sdim#define HOSTCACHE_HASH6(ip6)				\
194226586Sdim	(((ip6)->s6_addr32[0] ^				\
195226586Sdim	  (ip6)->s6_addr32[1] ^				\
196226586Sdim	  (ip6)->s6_addr32[2] ^				\
197226586Sdim	  (ip6)->s6_addr32[3]) &			\
198226586Sdim	 V_tcp_hostcache.hashmask)
199226586Sdim
200226586Sdim#define THC_LOCK(lp)		mtx_lock(lp)
201226586Sdim#define THC_UNLOCK(lp)		mtx_unlock(lp)
202226586Sdim
203226586Sdimvoid
204226586Sdimtcp_hc_init(void)
205226586Sdim{
206226586Sdim	int i;
207226586Sdim
208226586Sdim	/*
209226586Sdim	 * Initialize hostcache structures.
210226586Sdim	 */
211226586Sdim	V_tcp_hostcache.cache_count = 0;
212234353Sdim	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
213234353Sdim	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
214234353Sdim	V_tcp_hostcache.cache_limit =
215234353Sdim	    V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
216234353Sdim	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
217234353Sdim	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
218234353Sdim
219234353Sdim	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
220234353Sdim	    &V_tcp_hostcache.hashsize);
221234353Sdim	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
222234353Sdim	    &V_tcp_hostcache.cache_limit);
223234353Sdim	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
224234353Sdim	    &V_tcp_hostcache.bucket_limit);
225234353Sdim	if (!powerof2(V_tcp_hostcache.hashsize)) {
226249423Sdim		printf("WARNING: hostcache hash size is not a power of 2.\n");
227249423Sdim		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
228249423Sdim	}
229226586Sdim	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
230249423Sdim
231249423Sdim	/*
232249423Sdim	 * Allocate the hash table.
233249423Sdim	 */
234249423Sdim	V_tcp_hostcache.hashbase = (struct hc_head *)
235249423Sdim	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
236249423Sdim		   M_HOSTCACHE, M_WAITOK | M_ZERO);
237249423Sdim
238249423Sdim	/*
239249423Sdim	 * Initialize the hash buckets.
240249423Sdim	 */
241226586Sdim	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
242226586Sdim		TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
243226586Sdim		V_tcp_hostcache.hashbase[i].hch_length = 0;
244		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
245			  NULL, MTX_DEF);
246	}
247
248	/*
249	 * Allocate the hostcache entries.
250	 */
251	V_tcp_hostcache.zone =
252	    uma_zcreate("hostcache", sizeof(struct hc_metrics),
253	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
254	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
255
256	/*
257	 * Set up periodic cache cleanup.
258	 */
259	callout_init(&V_tcp_hc_callout, CALLOUT_MPSAFE);
260	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
261	    tcp_hc_purge, 0);
262}
263
264/*
265 * Internal function: look up an entry in the hostcache or return NULL.
266 *
267 * If an entry has been returned, the caller becomes responsible for
268 * unlocking the bucket row after he is done reading/modifying the entry.
269 */
270static struct hc_metrics *
271tcp_hc_lookup(struct in_conninfo *inc)
272{
273	int hash;
274	struct hc_head *hc_head;
275	struct hc_metrics *hc_entry;
276
277	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
278
279	/*
280	 * Hash the foreign ip address.
281	 */
282	if (inc->inc_isipv6)
283		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
284	else
285		hash = HOSTCACHE_HASH(&inc->inc_faddr);
286
287	hc_head = &V_tcp_hostcache.hashbase[hash];
288
289	/*
290	 * Acquire lock for this bucket row; we release the lock if we don't
291	 * find an entry, otherwise the caller has to unlock after he is
292	 * done.
293	 */
294	THC_LOCK(&hc_head->hch_mtx);
295
296	/*
297	 * Iterate through entries in bucket row looking for a match.
298	 */
299	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
300		if (inc->inc_isipv6) {
301			if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
302			    sizeof(inc->inc6_faddr)) == 0)
303				return hc_entry;
304		} else {
305			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
306			    sizeof(inc->inc_faddr)) == 0)
307				return hc_entry;
308		}
309	}
310
311	/*
312	 * We were unsuccessful and didn't find anything.
313	 */
314	THC_UNLOCK(&hc_head->hch_mtx);
315	return NULL;
316}
317
318/*
319 * Internal function: insert an entry into the hostcache or return NULL if
320 * unable to allocate a new one.
321 *
322 * If an entry has been returned, the caller becomes responsible for
323 * unlocking the bucket row after he is done reading/modifying the entry.
324 */
325static struct hc_metrics *
326tcp_hc_insert(struct in_conninfo *inc)
327{
328	int hash;
329	struct hc_head *hc_head;
330	struct hc_metrics *hc_entry;
331
332	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
333
334	/*
335	 * Hash the foreign ip address.
336	 */
337	if (inc->inc_isipv6)
338		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
339	else
340		hash = HOSTCACHE_HASH(&inc->inc_faddr);
341
342	hc_head = &V_tcp_hostcache.hashbase[hash];
343
344	/*
345	 * Acquire lock for this bucket row; we release the lock if we don't
346	 * find an entry, otherwise the caller has to unlock after he is
347	 * done.
348	 */
349	THC_LOCK(&hc_head->hch_mtx);
350
351	/*
352	 * If the bucket limit is reached, reuse the least-used element.
353	 */
354	if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
355	    V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) {
356		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
357		/*
358		 * At first we were dropping the last element, just to
359		 * reacquire it in the next two lines again, which isn't very
360		 * efficient.  Instead just reuse the least used element.
361		 * We may drop something that is still "in-use" but we can be
362		 * "lossy".
363		 * Just give up if this bucket row is empty and we don't have
364		 * anything to replace.
365		 */
366		if (hc_entry == NULL) {
367			THC_UNLOCK(&hc_head->hch_mtx);
368			return NULL;
369		}
370		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
371		V_tcp_hostcache.hashbase[hash].hch_length--;
372		V_tcp_hostcache.cache_count--;
373		V_tcpstat.tcps_hc_bucketoverflow++;
374#if 0
375		uma_zfree(V_tcp_hostcache.zone, hc_entry);
376#endif
377	} else {
378		/*
379		 * Allocate a new entry, or balk if not possible.
380		 */
381		hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
382		if (hc_entry == NULL) {
383			THC_UNLOCK(&hc_head->hch_mtx);
384			return NULL;
385		}
386	}
387
388	/*
389	 * Initialize basic information of hostcache entry.
390	 */
391	bzero(hc_entry, sizeof(*hc_entry));
392	if (inc->inc_isipv6)
393		bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6));
394	else
395		hc_entry->ip4 = inc->inc_faddr;
396	hc_entry->rmx_head = hc_head;
397	hc_entry->rmx_expire = V_tcp_hostcache.expire;
398
399	/*
400	 * Put it upfront.
401	 */
402	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
403	V_tcp_hostcache.hashbase[hash].hch_length++;
404	V_tcp_hostcache.cache_count++;
405	V_tcpstat.tcps_hc_added++;
406
407	return hc_entry;
408}
409
410/*
411 * External function: look up an entry in the hostcache and fill out the
412 * supplied TCP metrics structure.  Fills in NULL when no entry was found or
413 * a value is not set.
414 */
415void
416tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
417{
418	struct hc_metrics *hc_entry;
419
420	/*
421	 * Find the right bucket.
422	 */
423	hc_entry = tcp_hc_lookup(inc);
424
425	/*
426	 * If we don't have an existing object.
427	 */
428	if (hc_entry == NULL) {
429		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
430		return;
431	}
432	hc_entry->rmx_hits++;
433	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
434
435	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
436	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
437	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
438	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
439	hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth;
440	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
441	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
442	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
443
444	/*
445	 * Unlock bucket row.
446	 */
447	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
448}
449
450/*
451 * External function: look up an entry in the hostcache and return the
452 * discovered path MTU.  Returns NULL if no entry is found or value is not
453 * set.
454 */
455u_long
456tcp_hc_getmtu(struct in_conninfo *inc)
457{
458	struct hc_metrics *hc_entry;
459	u_long mtu;
460
461	hc_entry = tcp_hc_lookup(inc);
462	if (hc_entry == NULL) {
463		return 0;
464	}
465	hc_entry->rmx_hits++;
466	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
467
468	mtu = hc_entry->rmx_mtu;
469	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
470	return mtu;
471}
472
473/*
474 * External function: update the MTU value of an entry in the hostcache.
475 * Creates a new entry if none was found.
476 */
477void
478tcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
479{
480	struct hc_metrics *hc_entry;
481
482	/*
483	 * Find the right bucket.
484	 */
485	hc_entry = tcp_hc_lookup(inc);
486
487	/*
488	 * If we don't have an existing object, try to insert a new one.
489	 */
490	if (hc_entry == NULL) {
491		hc_entry = tcp_hc_insert(inc);
492		if (hc_entry == NULL)
493			return;
494	}
495	hc_entry->rmx_updates++;
496	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
497
498	hc_entry->rmx_mtu = mtu;
499
500	/*
501	 * Put it upfront so we find it faster next time.
502	 */
503	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
504	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
505
506	/*
507	 * Unlock bucket row.
508	 */
509	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
510}
511
512/*
513 * External function: update the TCP metrics of an entry in the hostcache.
514 * Creates a new entry if none was found.
515 */
516void
517tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
518{
519	struct hc_metrics *hc_entry;
520
521	hc_entry = tcp_hc_lookup(inc);
522	if (hc_entry == NULL) {
523		hc_entry = tcp_hc_insert(inc);
524		if (hc_entry == NULL)
525			return;
526	}
527	hc_entry->rmx_updates++;
528	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
529
530	if (hcml->rmx_rtt != 0) {
531		if (hc_entry->rmx_rtt == 0)
532			hc_entry->rmx_rtt = hcml->rmx_rtt;
533		else
534			hc_entry->rmx_rtt =
535			    (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
536		V_tcpstat.tcps_cachedrtt++;
537	}
538	if (hcml->rmx_rttvar != 0) {
539	        if (hc_entry->rmx_rttvar == 0)
540			hc_entry->rmx_rttvar = hcml->rmx_rttvar;
541		else
542			hc_entry->rmx_rttvar =
543			    (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
544		V_tcpstat.tcps_cachedrttvar++;
545	}
546	if (hcml->rmx_ssthresh != 0) {
547		if (hc_entry->rmx_ssthresh == 0)
548			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
549		else
550			hc_entry->rmx_ssthresh =
551			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
552		V_tcpstat.tcps_cachedssthresh++;
553	}
554	if (hcml->rmx_bandwidth != 0) {
555		if (hc_entry->rmx_bandwidth == 0)
556			hc_entry->rmx_bandwidth = hcml->rmx_bandwidth;
557		else
558			hc_entry->rmx_bandwidth =
559			    (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2;
560		/* V_tcpstat.tcps_cachedbandwidth++; */
561	}
562	if (hcml->rmx_cwnd != 0) {
563		if (hc_entry->rmx_cwnd == 0)
564			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
565		else
566			hc_entry->rmx_cwnd =
567			    (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
568		/* V_tcpstat.tcps_cachedcwnd++; */
569	}
570	if (hcml->rmx_sendpipe != 0) {
571		if (hc_entry->rmx_sendpipe == 0)
572			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
573		else
574			hc_entry->rmx_sendpipe =
575			    (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
576		/* V_tcpstat.tcps_cachedsendpipe++; */
577	}
578	if (hcml->rmx_recvpipe != 0) {
579		if (hc_entry->rmx_recvpipe == 0)
580			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
581		else
582			hc_entry->rmx_recvpipe =
583			    (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
584		/* V_tcpstat.tcps_cachedrecvpipe++; */
585	}
586
587	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
588	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
589	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
590}
591
592/*
593 * Sysctl function: prints the list and values of all hostcache entries in
594 * unsorted order.
595 */
596static int
597sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
598{
599	int bufsize;
600	int linesize = 128;
601	char *p, *buf;
602	int len, i, error;
603	struct hc_metrics *hc_entry;
604#ifdef INET6
605	char ip6buf[INET6_ADDRSTRLEN];
606#endif
607
608	bufsize = linesize * (V_tcp_hostcache.cache_count + 1);
609
610	p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
611
612	len = snprintf(p, linesize,
613		"\nIP address        MTU  SSTRESH      RTT   RTTVAR BANDWIDTH "
614		"    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
615	p += len;
616
617#define msec(u) (((u) + 500) / 1000)
618	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
619		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
620		TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
621			      rmx_q) {
622			len = snprintf(p, linesize,
623			    "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu "
624			    "%4lu %4lu %4i\n",
625			    hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
626#ifdef INET6
627				ip6_sprintf(ip6buf, &hc_entry->ip6),
628#else
629				"IPv6?",
630#endif
631			    hc_entry->rmx_mtu,
632			    hc_entry->rmx_ssthresh,
633			    msec(hc_entry->rmx_rtt *
634				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
635			    msec(hc_entry->rmx_rttvar *
636				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
637			    hc_entry->rmx_bandwidth * 8,
638			    hc_entry->rmx_cwnd,
639			    hc_entry->rmx_sendpipe,
640			    hc_entry->rmx_recvpipe,
641			    hc_entry->rmx_hits,
642			    hc_entry->rmx_updates,
643			    hc_entry->rmx_expire);
644			p += len;
645		}
646		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
647	}
648#undef msec
649	error = SYSCTL_OUT(req, buf, p - buf);
650	free(buf, M_TEMP);
651	return(error);
652}
653
654/*
655 * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
656 * periodically from the callout.
657 */
658static void
659tcp_hc_purge(void *arg)
660{
661	struct hc_metrics *hc_entry, *hc_next;
662	int all = (intptr_t)arg;
663	int i;
664
665	if (V_tcp_hostcache.purgeall) {
666		all = 1;
667		V_tcp_hostcache.purgeall = 0;
668	}
669
670	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
671		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
672		TAILQ_FOREACH_SAFE(hc_entry,
673		    &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
674			if (all || hc_entry->rmx_expire <= 0) {
675				TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
676					      hc_entry, rmx_q);
677				uma_zfree(V_tcp_hostcache.zone, hc_entry);
678				V_tcp_hostcache.hashbase[i].hch_length--;
679				V_tcp_hostcache.cache_count--;
680			} else
681				hc_entry->rmx_expire -= V_tcp_hostcache.prune;
682		}
683		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
684	}
685
686	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
687	    tcp_hc_purge, arg);
688}
689