xref: /freebsd/sys/netinet/tcp_hostcache.c (revision 7fb179ba7e6d3d82bddcd292bad1e1b7b4aef95e)
1 /*-
2  * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * The tcp_hostcache moves the tcp-specific cached metrics from the routing
32  * table to a dedicated structure indexed by the remote IP address.  It keeps
33  * information on the measured TCP parameters of past TCP sessions to allow
34  * better initial start values to be used with later connections to/from the
35  * same source.  Depending on the network parameters (delay, bandwidth, max
36  * MTU, congestion window) between local and remote sites, this can lead to
37  * significant speed-ups for new TCP connections after the first one.
38  *
39  * Due to the tcp_hostcache, all TCP-specific metrics information in the
40  * routing table have been removed.  The inpcb no longer keeps a pointer to
41  * the routing entry, and protocol-initiated route cloning has been removed
42  * as well.  With these changes, the routing table has gone back to being
43  * more lightwight and only carries information related to packet forwarding.
44  *
45  * tcp_hostcache is designed for multiple concurrent access in SMP
46  * environments and high contention.  All bucket rows have their own lock and
47  * thus multiple lookups and modifies can be done at the same time as long as
48  * they are in different bucket rows.  If a request for insertion of a new
49  * record can't be satisfied, it simply returns an empty structure.  Nobody
50  * and nothing outside of tcp_hostcache.c will ever point directly to any
51  * entry in the tcp_hostcache.  All communication is done in an
52  * object-oriented way and only functions of tcp_hostcache will manipulate
53  * hostcache entries.  Otherwise, we are unable to achieve good behaviour in
54  * concurrent access situations.  Since tcp_hostcache is only caching
55  * information, there are no fatal consequences if we either can't satisfy
56  * any particular request or have to drop/overwrite an existing entry because
57  * of bucket limit memory constrains.
58  */
59 
60 /*
61  * Many thanks to jlemon for basic structure of tcp_syncache which is being
62  * followed here.
63  */
64 
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
67 
68 #include "opt_inet6.h"
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/lock.h>
74 #include <sys/mutex.h>
75 #include <sys/malloc.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/sysctl.h>
79 #include <sys/vimage.h>
80 
81 #include <net/if.h>
82 
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/in_var.h>
87 #include <netinet/in_pcb.h>
88 #include <netinet/ip_var.h>
89 #ifdef INET6
90 #include <netinet/ip6.h>
91 #include <netinet6/ip6_var.h>
92 #endif
93 #include <netinet/tcp.h>
94 #include <netinet/tcp_var.h>
95 #ifdef INET6
96 #include <netinet6/tcp6_var.h>
97 #endif
98 
99 #include <vm/uma.h>
100 
101 
102 TAILQ_HEAD(hc_qhead, hc_metrics);
103 
104 struct hc_head {
105 	struct hc_qhead	hch_bucket;
106 	u_int		hch_length;
107 	struct mtx	hch_mtx;
108 };
109 
110 struct hc_metrics {
111 	/* housekeeping */
112 	TAILQ_ENTRY(hc_metrics) rmx_q;
113 	struct	hc_head *rmx_head; /* head of bucket tail queue */
114 	struct	in_addr ip4;	/* IP address */
115 	struct	in6_addr ip6;	/* IP6 address */
116 	/* endpoint specific values for TCP */
117 	u_long	rmx_mtu;	/* MTU for this path */
118 	u_long	rmx_ssthresh;	/* outbound gateway buffer limit */
119 	u_long	rmx_rtt;	/* estimated round trip time */
120 	u_long	rmx_rttvar;	/* estimated rtt variance */
121 	u_long	rmx_bandwidth;	/* estimated bandwidth */
122 	u_long	rmx_cwnd;	/* congestion window */
123 	u_long	rmx_sendpipe;	/* outbound delay-bandwidth product */
124 	u_long	rmx_recvpipe;	/* inbound delay-bandwidth product */
125 	/* TCP hostcache internal data */
126 	int	rmx_expire;	/* lifetime for object */
127 	u_long	rmx_hits;	/* number of hits */
128 	u_long	rmx_updates;	/* number of updates */
129 };
130 
131 /* Arbitrary values */
132 #define TCP_HOSTCACHE_HASHSIZE		512
133 #define TCP_HOSTCACHE_BUCKETLIMIT	30
134 #define TCP_HOSTCACHE_EXPIRE		60*60	/* one hour */
135 #define TCP_HOSTCACHE_PRUNE		5*60	/* every 5 minutes */
136 
137 struct tcp_hostcache {
138 	struct	hc_head *hashbase;
139 	uma_zone_t zone;
140 	u_int	hashsize;
141 	u_int	hashmask;
142 	u_int	bucket_limit;
143 	u_int	cache_count;
144 	u_int	cache_limit;
145 	int	expire;
146 	int	prune;
147 	int	purgeall;
148 };
149 static struct tcp_hostcache tcp_hostcache;
150 
151 static struct callout tcp_hc_callout;
152 
153 static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
154 static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
155 static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
156 static void tcp_hc_purge(void *);
157 
158 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0,
159     "TCP Host cache");
160 
161 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, cachelimit,
162     CTLFLAG_RDTUN, tcp_hostcache.cache_limit, 0,
163     "Overall entry limit for hostcache");
164 
165 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, hashsize,
166     CTLFLAG_RDTUN, tcp_hostcache.hashsize, 0,
167     "Size of TCP hostcache hashtable");
168 
169 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
170     CTLFLAG_RDTUN, tcp_hostcache.bucket_limit, 0,
171     "Per-bucket hash limit for hostcache");
172 
173 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, count,
174     CTLFLAG_RD, tcp_hostcache.cache_count, 0,
175     "Current number of entries in hostcache");
176 
177 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, expire,
178     CTLFLAG_RW, tcp_hostcache.expire, 0,
179     "Expire time of TCP hostcache entries");
180 
181 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, prune,
182      CTLFLAG_RW, tcp_hostcache.prune, 0, "Time between purge runs");
183 
184 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, purge,
185     CTLFLAG_RW, tcp_hostcache.purgeall, 0,
186     "Expire all entires on next purge run");
187 
188 SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
189     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
190     sysctl_tcp_hc_list, "A", "List of all hostcache entries");
191 
192 
193 static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
194 
195 #define HOSTCACHE_HASH(ip) \
196 	(((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) &	\
197 	  V_tcp_hostcache.hashmask)
198 
199 /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
200 #define HOSTCACHE_HASH6(ip6)				\
201 	(((ip6)->s6_addr32[0] ^				\
202 	  (ip6)->s6_addr32[1] ^				\
203 	  (ip6)->s6_addr32[2] ^				\
204 	  (ip6)->s6_addr32[3]) &			\
205 	 V_tcp_hostcache.hashmask)
206 
207 #define THC_LOCK(lp)		mtx_lock(lp)
208 #define THC_UNLOCK(lp)		mtx_unlock(lp)
209 
210 void
211 tcp_hc_init(void)
212 {
213 	INIT_VNET_INET(curvnet);
214 	int i;
215 
216 	/*
217 	 * Initialize hostcache structures.
218 	 */
219 	V_tcp_hostcache.cache_count = 0;
220 	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
221 	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
222 	V_tcp_hostcache.cache_limit =
223 	    V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
224 	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
225 	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
226 
227 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
228 	    &V_tcp_hostcache.hashsize);
229 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
230 	    &V_tcp_hostcache.cache_limit);
231 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
232 	    &V_tcp_hostcache.bucket_limit);
233 	if (!powerof2(V_tcp_hostcache.hashsize)) {
234 		printf("WARNING: hostcache hash size is not a power of 2.\n");
235 		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
236 	}
237 	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
238 
239 	/*
240 	 * Allocate the hash table.
241 	 */
242 	V_tcp_hostcache.hashbase = (struct hc_head *)
243 	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
244 		   M_HOSTCACHE, M_WAITOK | M_ZERO);
245 
246 	/*
247 	 * Initialize the hash buckets.
248 	 */
249 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
250 		TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
251 		V_tcp_hostcache.hashbase[i].hch_length = 0;
252 		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
253 			  NULL, MTX_DEF);
254 	}
255 
256 	/*
257 	 * Allocate the hostcache entries.
258 	 */
259 	V_tcp_hostcache.zone =
260 	    uma_zcreate("hostcache", sizeof(struct hc_metrics),
261 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
262 	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
263 
264 	/*
265 	 * Set up periodic cache cleanup.
266 	 */
267 	callout_init(&V_tcp_hc_callout, CALLOUT_MPSAFE);
268 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
269 	    tcp_hc_purge, 0);
270 }
271 
272 /*
273  * Internal function: look up an entry in the hostcache or return NULL.
274  *
275  * If an entry has been returned, the caller becomes responsible for
276  * unlocking the bucket row after he is done reading/modifying the entry.
277  */
278 static struct hc_metrics *
279 tcp_hc_lookup(struct in_conninfo *inc)
280 {
281 	INIT_VNET_INET(curvnet);
282 	int hash;
283 	struct hc_head *hc_head;
284 	struct hc_metrics *hc_entry;
285 
286 	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
287 
288 	/*
289 	 * Hash the foreign ip address.
290 	 */
291 	if (inc->inc_isipv6)
292 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
293 	else
294 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
295 
296 	hc_head = &V_tcp_hostcache.hashbase[hash];
297 
298 	/*
299 	 * Acquire lock for this bucket row; we release the lock if we don't
300 	 * find an entry, otherwise the caller has to unlock after he is
301 	 * done.
302 	 */
303 	THC_LOCK(&hc_head->hch_mtx);
304 
305 	/*
306 	 * Iterate through entries in bucket row looking for a match.
307 	 */
308 	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
309 		if (inc->inc_isipv6) {
310 			if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
311 			    sizeof(inc->inc6_faddr)) == 0)
312 				return hc_entry;
313 		} else {
314 			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
315 			    sizeof(inc->inc_faddr)) == 0)
316 				return hc_entry;
317 		}
318 	}
319 
320 	/*
321 	 * We were unsuccessful and didn't find anything.
322 	 */
323 	THC_UNLOCK(&hc_head->hch_mtx);
324 	return NULL;
325 }
326 
327 /*
328  * Internal function: insert an entry into the hostcache or return NULL if
329  * unable to allocate a new one.
330  *
331  * If an entry has been returned, the caller becomes responsible for
332  * unlocking the bucket row after he is done reading/modifying the entry.
333  */
334 static struct hc_metrics *
335 tcp_hc_insert(struct in_conninfo *inc)
336 {
337 	INIT_VNET_INET(curvnet);
338 	int hash;
339 	struct hc_head *hc_head;
340 	struct hc_metrics *hc_entry;
341 
342 	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
343 
344 	/*
345 	 * Hash the foreign ip address.
346 	 */
347 	if (inc->inc_isipv6)
348 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
349 	else
350 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
351 
352 	hc_head = &V_tcp_hostcache.hashbase[hash];
353 
354 	/*
355 	 * Acquire lock for this bucket row; we release the lock if we don't
356 	 * find an entry, otherwise the caller has to unlock after he is
357 	 * done.
358 	 */
359 	THC_LOCK(&hc_head->hch_mtx);
360 
361 	/*
362 	 * If the bucket limit is reached, reuse the least-used element.
363 	 */
364 	if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
365 	    V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) {
366 		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
367 		/*
368 		 * At first we were dropping the last element, just to
369 		 * reacquire it in the next two lines again, which isn't very
370 		 * efficient.  Instead just reuse the least used element.
371 		 * We may drop something that is still "in-use" but we can be
372 		 * "lossy".
373 		 * Just give up if this bucket row is empty and we don't have
374 		 * anything to replace.
375 		 */
376 		if (hc_entry == NULL) {
377 			THC_UNLOCK(&hc_head->hch_mtx);
378 			return NULL;
379 		}
380 		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
381 		V_tcp_hostcache.hashbase[hash].hch_length--;
382 		V_tcp_hostcache.cache_count--;
383 		V_tcpstat.tcps_hc_bucketoverflow++;
384 #if 0
385 		uma_zfree(V_tcp_hostcache.zone, hc_entry);
386 #endif
387 	} else {
388 		/*
389 		 * Allocate a new entry, or balk if not possible.
390 		 */
391 		hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
392 		if (hc_entry == NULL) {
393 			THC_UNLOCK(&hc_head->hch_mtx);
394 			return NULL;
395 		}
396 	}
397 
398 	/*
399 	 * Initialize basic information of hostcache entry.
400 	 */
401 	bzero(hc_entry, sizeof(*hc_entry));
402 	if (inc->inc_isipv6)
403 		bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6));
404 	else
405 		hc_entry->ip4 = inc->inc_faddr;
406 	hc_entry->rmx_head = hc_head;
407 	hc_entry->rmx_expire = V_tcp_hostcache.expire;
408 
409 	/*
410 	 * Put it upfront.
411 	 */
412 	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
413 	V_tcp_hostcache.hashbase[hash].hch_length++;
414 	V_tcp_hostcache.cache_count++;
415 	V_tcpstat.tcps_hc_added++;
416 
417 	return hc_entry;
418 }
419 
420 /*
421  * External function: look up an entry in the hostcache and fill out the
422  * supplied TCP metrics structure.  Fills in NULL when no entry was found or
423  * a value is not set.
424  */
425 void
426 tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
427 {
428 	INIT_VNET_INET(curvnet);
429 	struct hc_metrics *hc_entry;
430 
431 	/*
432 	 * Find the right bucket.
433 	 */
434 	hc_entry = tcp_hc_lookup(inc);
435 
436 	/*
437 	 * If we don't have an existing object.
438 	 */
439 	if (hc_entry == NULL) {
440 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
441 		return;
442 	}
443 	hc_entry->rmx_hits++;
444 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
445 
446 	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
447 	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
448 	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
449 	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
450 	hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth;
451 	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
452 	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
453 	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
454 
455 	/*
456 	 * Unlock bucket row.
457 	 */
458 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
459 }
460 
461 /*
462  * External function: look up an entry in the hostcache and return the
463  * discovered path MTU.  Returns NULL if no entry is found or value is not
464  * set.
465  */
466 u_long
467 tcp_hc_getmtu(struct in_conninfo *inc)
468 {
469 	INIT_VNET_INET(curvnet);
470 	struct hc_metrics *hc_entry;
471 	u_long mtu;
472 
473 	hc_entry = tcp_hc_lookup(inc);
474 	if (hc_entry == NULL) {
475 		return 0;
476 	}
477 	hc_entry->rmx_hits++;
478 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
479 
480 	mtu = hc_entry->rmx_mtu;
481 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
482 	return mtu;
483 }
484 
485 /*
486  * External function: update the MTU value of an entry in the hostcache.
487  * Creates a new entry if none was found.
488  */
489 void
490 tcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
491 {
492 	INIT_VNET_INET(curvnet);
493 	struct hc_metrics *hc_entry;
494 
495 	/*
496 	 * Find the right bucket.
497 	 */
498 	hc_entry = tcp_hc_lookup(inc);
499 
500 	/*
501 	 * If we don't have an existing object, try to insert a new one.
502 	 */
503 	if (hc_entry == NULL) {
504 		hc_entry = tcp_hc_insert(inc);
505 		if (hc_entry == NULL)
506 			return;
507 	}
508 	hc_entry->rmx_updates++;
509 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
510 
511 	hc_entry->rmx_mtu = mtu;
512 
513 	/*
514 	 * Put it upfront so we find it faster next time.
515 	 */
516 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
517 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
518 
519 	/*
520 	 * Unlock bucket row.
521 	 */
522 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
523 }
524 
525 /*
526  * External function: update the TCP metrics of an entry in the hostcache.
527  * Creates a new entry if none was found.
528  */
529 void
530 tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
531 {
532 	INIT_VNET_INET(curvnet);
533 	struct hc_metrics *hc_entry;
534 
535 	hc_entry = tcp_hc_lookup(inc);
536 	if (hc_entry == NULL) {
537 		hc_entry = tcp_hc_insert(inc);
538 		if (hc_entry == NULL)
539 			return;
540 	}
541 	hc_entry->rmx_updates++;
542 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
543 
544 	if (hcml->rmx_rtt != 0) {
545 		if (hc_entry->rmx_rtt == 0)
546 			hc_entry->rmx_rtt = hcml->rmx_rtt;
547 		else
548 			hc_entry->rmx_rtt =
549 			    (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
550 		V_tcpstat.tcps_cachedrtt++;
551 	}
552 	if (hcml->rmx_rttvar != 0) {
553 	        if (hc_entry->rmx_rttvar == 0)
554 			hc_entry->rmx_rttvar = hcml->rmx_rttvar;
555 		else
556 			hc_entry->rmx_rttvar =
557 			    (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
558 		V_tcpstat.tcps_cachedrttvar++;
559 	}
560 	if (hcml->rmx_ssthresh != 0) {
561 		if (hc_entry->rmx_ssthresh == 0)
562 			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
563 		else
564 			hc_entry->rmx_ssthresh =
565 			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
566 		V_tcpstat.tcps_cachedssthresh++;
567 	}
568 	if (hcml->rmx_bandwidth != 0) {
569 		if (hc_entry->rmx_bandwidth == 0)
570 			hc_entry->rmx_bandwidth = hcml->rmx_bandwidth;
571 		else
572 			hc_entry->rmx_bandwidth =
573 			    (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2;
574 		/* V_tcpstat.tcps_cachedbandwidth++; */
575 	}
576 	if (hcml->rmx_cwnd != 0) {
577 		if (hc_entry->rmx_cwnd == 0)
578 			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
579 		else
580 			hc_entry->rmx_cwnd =
581 			    (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
582 		/* V_tcpstat.tcps_cachedcwnd++; */
583 	}
584 	if (hcml->rmx_sendpipe != 0) {
585 		if (hc_entry->rmx_sendpipe == 0)
586 			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
587 		else
588 			hc_entry->rmx_sendpipe =
589 			    (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
590 		/* V_tcpstat.tcps_cachedsendpipe++; */
591 	}
592 	if (hcml->rmx_recvpipe != 0) {
593 		if (hc_entry->rmx_recvpipe == 0)
594 			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
595 		else
596 			hc_entry->rmx_recvpipe =
597 			    (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
598 		/* V_tcpstat.tcps_cachedrecvpipe++; */
599 	}
600 
601 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
602 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
603 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
604 }
605 
606 /*
607  * Sysctl function: prints the list and values of all hostcache entries in
608  * unsorted order.
609  */
610 static int
611 sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
612 {
613 	INIT_VNET_INET(curvnet);
614 	int bufsize;
615 	int linesize = 128;
616 	char *p, *buf;
617 	int len, i, error;
618 	struct hc_metrics *hc_entry;
619 #ifdef INET6
620 	char ip6buf[INET6_ADDRSTRLEN];
621 #endif
622 
623 	bufsize = linesize * (V_tcp_hostcache.cache_count + 1);
624 
625 	p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
626 
627 	len = snprintf(p, linesize,
628 		"\nIP address        MTU  SSTRESH      RTT   RTTVAR BANDWIDTH "
629 		"    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
630 	p += len;
631 
632 #define msec(u) (((u) + 500) / 1000)
633 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
634 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
635 		TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
636 			      rmx_q) {
637 			len = snprintf(p, linesize,
638 			    "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu "
639 			    "%4lu %4lu %4i\n",
640 			    hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
641 #ifdef INET6
642 				ip6_sprintf(ip6buf, &hc_entry->ip6),
643 #else
644 				"IPv6?",
645 #endif
646 			    hc_entry->rmx_mtu,
647 			    hc_entry->rmx_ssthresh,
648 			    msec(hc_entry->rmx_rtt *
649 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
650 			    msec(hc_entry->rmx_rttvar *
651 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
652 			    hc_entry->rmx_bandwidth * 8,
653 			    hc_entry->rmx_cwnd,
654 			    hc_entry->rmx_sendpipe,
655 			    hc_entry->rmx_recvpipe,
656 			    hc_entry->rmx_hits,
657 			    hc_entry->rmx_updates,
658 			    hc_entry->rmx_expire);
659 			p += len;
660 		}
661 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
662 	}
663 #undef msec
664 	error = SYSCTL_OUT(req, buf, p - buf);
665 	free(buf, M_TEMP);
666 	return(error);
667 }
668 
669 /*
670  * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
671  * periodically from the callout.
672  */
673 static void
674 tcp_hc_purge(void *arg)
675 {
676 	INIT_VNET_INET(curvnet);
677 	struct hc_metrics *hc_entry, *hc_next;
678 	int all = (intptr_t)arg;
679 	int i;
680 
681 	if (V_tcp_hostcache.purgeall) {
682 		all = 1;
683 		V_tcp_hostcache.purgeall = 0;
684 	}
685 
686 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
687 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
688 		TAILQ_FOREACH_SAFE(hc_entry,
689 		    &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
690 			if (all || hc_entry->rmx_expire <= 0) {
691 				TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
692 					      hc_entry, rmx_q);
693 				uma_zfree(V_tcp_hostcache.zone, hc_entry);
694 				V_tcp_hostcache.hashbase[i].hch_length--;
695 				V_tcp_hostcache.cache_count--;
696 			} else
697 				hc_entry->rmx_expire -= V_tcp_hostcache.prune;
698 		}
699 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
700 	}
701 
702 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
703 	    tcp_hc_purge, arg);
704 }
705