tcp_hostcache.c revision 185088
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: head/sys/netinet/tcp_hostcache.c 185088 2008-11-19 09:39:34Z zec $");
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
102TAILQ_HEAD(hc_qhead, hc_metrics);
103
104struct hc_head {
105	struct hc_qhead	hch_bucket;
106	u_int		hch_length;
107	struct mtx	hch_mtx;
108};
109
110struct 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
137struct 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
150#ifdef VIMAGE_GLOBALS
151static struct tcp_hostcache tcp_hostcache;
152static struct callout tcp_hc_callout;
153#endif
154
155static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
156static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
157static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
158static void tcp_hc_purge(void *);
159
160SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0,
161    "TCP Host cache");
162
163SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, cachelimit,
164    CTLFLAG_RDTUN, tcp_hostcache.cache_limit, 0,
165    "Overall entry limit for hostcache");
166
167SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, hashsize,
168    CTLFLAG_RDTUN, tcp_hostcache.hashsize, 0,
169    "Size of TCP hostcache hashtable");
170
171SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
172    CTLFLAG_RDTUN, tcp_hostcache.bucket_limit, 0,
173    "Per-bucket hash limit for hostcache");
174
175SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, count,
176    CTLFLAG_RD, tcp_hostcache.cache_count, 0,
177    "Current number of entries in hostcache");
178
179SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, expire,
180    CTLFLAG_RW, tcp_hostcache.expire, 0,
181    "Expire time of TCP hostcache entries");
182
183SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, prune,
184     CTLFLAG_RW, tcp_hostcache.prune, 0, "Time between purge runs");
185
186SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, purge,
187    CTLFLAG_RW, tcp_hostcache.purgeall, 0,
188    "Expire all entires on next purge run");
189
190SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
191    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
192    sysctl_tcp_hc_list, "A", "List of all hostcache entries");
193
194
195static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
196
197#define HOSTCACHE_HASH(ip) \
198	(((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) &	\
199	  V_tcp_hostcache.hashmask)
200
201/* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
202#define HOSTCACHE_HASH6(ip6)				\
203	(((ip6)->s6_addr32[0] ^				\
204	  (ip6)->s6_addr32[1] ^				\
205	  (ip6)->s6_addr32[2] ^				\
206	  (ip6)->s6_addr32[3]) &			\
207	 V_tcp_hostcache.hashmask)
208
209#define THC_LOCK(lp)		mtx_lock(lp)
210#define THC_UNLOCK(lp)		mtx_unlock(lp)
211
212void
213tcp_hc_init(void)
214{
215	INIT_VNET_INET(curvnet);
216	int i;
217
218	/*
219	 * Initialize hostcache structures.
220	 */
221	V_tcp_hostcache.cache_count = 0;
222	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
223	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
224	V_tcp_hostcache.cache_limit =
225	    V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
226	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
227	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
228
229	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
230	    &V_tcp_hostcache.hashsize);
231	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
232	    &V_tcp_hostcache.cache_limit);
233	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
234	    &V_tcp_hostcache.bucket_limit);
235	if (!powerof2(V_tcp_hostcache.hashsize)) {
236		printf("WARNING: hostcache hash size is not a power of 2.\n");
237		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
238	}
239	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
240
241	/*
242	 * Allocate the hash table.
243	 */
244	V_tcp_hostcache.hashbase = (struct hc_head *)
245	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
246		   M_HOSTCACHE, M_WAITOK | M_ZERO);
247
248	/*
249	 * Initialize the hash buckets.
250	 */
251	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
252		TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
253		V_tcp_hostcache.hashbase[i].hch_length = 0;
254		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
255			  NULL, MTX_DEF);
256	}
257
258	/*
259	 * Allocate the hostcache entries.
260	 */
261	V_tcp_hostcache.zone =
262	    uma_zcreate("hostcache", sizeof(struct hc_metrics),
263	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
264	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
265
266	/*
267	 * Set up periodic cache cleanup.
268	 */
269	callout_init(&V_tcp_hc_callout, CALLOUT_MPSAFE);
270	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
271	    tcp_hc_purge, 0);
272}
273
274/*
275 * Internal function: look up an entry in the hostcache or return NULL.
276 *
277 * If an entry has been returned, the caller becomes responsible for
278 * unlocking the bucket row after he is done reading/modifying the entry.
279 */
280static struct hc_metrics *
281tcp_hc_lookup(struct in_conninfo *inc)
282{
283	INIT_VNET_INET(curvnet);
284	int hash;
285	struct hc_head *hc_head;
286	struct hc_metrics *hc_entry;
287
288	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
289
290	/*
291	 * Hash the foreign ip address.
292	 */
293	if (inc->inc_isipv6)
294		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
295	else
296		hash = HOSTCACHE_HASH(&inc->inc_faddr);
297
298	hc_head = &V_tcp_hostcache.hashbase[hash];
299
300	/*
301	 * Acquire lock for this bucket row; we release the lock if we don't
302	 * find an entry, otherwise the caller has to unlock after he is
303	 * done.
304	 */
305	THC_LOCK(&hc_head->hch_mtx);
306
307	/*
308	 * Iterate through entries in bucket row looking for a match.
309	 */
310	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
311		if (inc->inc_isipv6) {
312			if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
313			    sizeof(inc->inc6_faddr)) == 0)
314				return hc_entry;
315		} else {
316			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
317			    sizeof(inc->inc_faddr)) == 0)
318				return hc_entry;
319		}
320	}
321
322	/*
323	 * We were unsuccessful and didn't find anything.
324	 */
325	THC_UNLOCK(&hc_head->hch_mtx);
326	return NULL;
327}
328
329/*
330 * Internal function: insert an entry into the hostcache or return NULL if
331 * unable to allocate a new one.
332 *
333 * If an entry has been returned, the caller becomes responsible for
334 * unlocking the bucket row after he is done reading/modifying the entry.
335 */
336static struct hc_metrics *
337tcp_hc_insert(struct in_conninfo *inc)
338{
339	INIT_VNET_INET(curvnet);
340	int hash;
341	struct hc_head *hc_head;
342	struct hc_metrics *hc_entry;
343
344	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
345
346	/*
347	 * Hash the foreign ip address.
348	 */
349	if (inc->inc_isipv6)
350		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
351	else
352		hash = HOSTCACHE_HASH(&inc->inc_faddr);
353
354	hc_head = &V_tcp_hostcache.hashbase[hash];
355
356	/*
357	 * Acquire lock for this bucket row; we release the lock if we don't
358	 * find an entry, otherwise the caller has to unlock after he is
359	 * done.
360	 */
361	THC_LOCK(&hc_head->hch_mtx);
362
363	/*
364	 * If the bucket limit is reached, reuse the least-used element.
365	 */
366	if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
367	    V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) {
368		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
369		/*
370		 * At first we were dropping the last element, just to
371		 * reacquire it in the next two lines again, which isn't very
372		 * efficient.  Instead just reuse the least used element.
373		 * We may drop something that is still "in-use" but we can be
374		 * "lossy".
375		 * Just give up if this bucket row is empty and we don't have
376		 * anything to replace.
377		 */
378		if (hc_entry == NULL) {
379			THC_UNLOCK(&hc_head->hch_mtx);
380			return NULL;
381		}
382		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
383		V_tcp_hostcache.hashbase[hash].hch_length--;
384		V_tcp_hostcache.cache_count--;
385		V_tcpstat.tcps_hc_bucketoverflow++;
386#if 0
387		uma_zfree(V_tcp_hostcache.zone, hc_entry);
388#endif
389	} else {
390		/*
391		 * Allocate a new entry, or balk if not possible.
392		 */
393		hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
394		if (hc_entry == NULL) {
395			THC_UNLOCK(&hc_head->hch_mtx);
396			return NULL;
397		}
398	}
399
400	/*
401	 * Initialize basic information of hostcache entry.
402	 */
403	bzero(hc_entry, sizeof(*hc_entry));
404	if (inc->inc_isipv6)
405		bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6));
406	else
407		hc_entry->ip4 = inc->inc_faddr;
408	hc_entry->rmx_head = hc_head;
409	hc_entry->rmx_expire = V_tcp_hostcache.expire;
410
411	/*
412	 * Put it upfront.
413	 */
414	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
415	V_tcp_hostcache.hashbase[hash].hch_length++;
416	V_tcp_hostcache.cache_count++;
417	V_tcpstat.tcps_hc_added++;
418
419	return hc_entry;
420}
421
422/*
423 * External function: look up an entry in the hostcache and fill out the
424 * supplied TCP metrics structure.  Fills in NULL when no entry was found or
425 * a value is not set.
426 */
427void
428tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
429{
430	INIT_VNET_INET(curvnet);
431	struct hc_metrics *hc_entry;
432
433	/*
434	 * Find the right bucket.
435	 */
436	hc_entry = tcp_hc_lookup(inc);
437
438	/*
439	 * If we don't have an existing object.
440	 */
441	if (hc_entry == NULL) {
442		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
443		return;
444	}
445	hc_entry->rmx_hits++;
446	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
447
448	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
449	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
450	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
451	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
452	hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth;
453	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
454	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
455	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
456
457	/*
458	 * Unlock bucket row.
459	 */
460	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
461}
462
463/*
464 * External function: look up an entry in the hostcache and return the
465 * discovered path MTU.  Returns NULL if no entry is found or value is not
466 * set.
467 */
468u_long
469tcp_hc_getmtu(struct in_conninfo *inc)
470{
471	INIT_VNET_INET(curvnet);
472	struct hc_metrics *hc_entry;
473	u_long mtu;
474
475	hc_entry = tcp_hc_lookup(inc);
476	if (hc_entry == NULL) {
477		return 0;
478	}
479	hc_entry->rmx_hits++;
480	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
481
482	mtu = hc_entry->rmx_mtu;
483	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
484	return mtu;
485}
486
487/*
488 * External function: update the MTU value of an entry in the hostcache.
489 * Creates a new entry if none was found.
490 */
491void
492tcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
493{
494	INIT_VNET_INET(curvnet);
495	struct hc_metrics *hc_entry;
496
497	/*
498	 * Find the right bucket.
499	 */
500	hc_entry = tcp_hc_lookup(inc);
501
502	/*
503	 * If we don't have an existing object, try to insert a new one.
504	 */
505	if (hc_entry == NULL) {
506		hc_entry = tcp_hc_insert(inc);
507		if (hc_entry == NULL)
508			return;
509	}
510	hc_entry->rmx_updates++;
511	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
512
513	hc_entry->rmx_mtu = mtu;
514
515	/*
516	 * Put it upfront so we find it faster next time.
517	 */
518	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
519	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
520
521	/*
522	 * Unlock bucket row.
523	 */
524	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
525}
526
527/*
528 * External function: update the TCP metrics of an entry in the hostcache.
529 * Creates a new entry if none was found.
530 */
531void
532tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
533{
534	INIT_VNET_INET(curvnet);
535	struct hc_metrics *hc_entry;
536
537	hc_entry = tcp_hc_lookup(inc);
538	if (hc_entry == NULL) {
539		hc_entry = tcp_hc_insert(inc);
540		if (hc_entry == NULL)
541			return;
542	}
543	hc_entry->rmx_updates++;
544	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
545
546	if (hcml->rmx_rtt != 0) {
547		if (hc_entry->rmx_rtt == 0)
548			hc_entry->rmx_rtt = hcml->rmx_rtt;
549		else
550			hc_entry->rmx_rtt =
551			    (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
552		V_tcpstat.tcps_cachedrtt++;
553	}
554	if (hcml->rmx_rttvar != 0) {
555	        if (hc_entry->rmx_rttvar == 0)
556			hc_entry->rmx_rttvar = hcml->rmx_rttvar;
557		else
558			hc_entry->rmx_rttvar =
559			    (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
560		V_tcpstat.tcps_cachedrttvar++;
561	}
562	if (hcml->rmx_ssthresh != 0) {
563		if (hc_entry->rmx_ssthresh == 0)
564			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
565		else
566			hc_entry->rmx_ssthresh =
567			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
568		V_tcpstat.tcps_cachedssthresh++;
569	}
570	if (hcml->rmx_bandwidth != 0) {
571		if (hc_entry->rmx_bandwidth == 0)
572			hc_entry->rmx_bandwidth = hcml->rmx_bandwidth;
573		else
574			hc_entry->rmx_bandwidth =
575			    (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2;
576		/* V_tcpstat.tcps_cachedbandwidth++; */
577	}
578	if (hcml->rmx_cwnd != 0) {
579		if (hc_entry->rmx_cwnd == 0)
580			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
581		else
582			hc_entry->rmx_cwnd =
583			    (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
584		/* V_tcpstat.tcps_cachedcwnd++; */
585	}
586	if (hcml->rmx_sendpipe != 0) {
587		if (hc_entry->rmx_sendpipe == 0)
588			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
589		else
590			hc_entry->rmx_sendpipe =
591			    (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
592		/* V_tcpstat.tcps_cachedsendpipe++; */
593	}
594	if (hcml->rmx_recvpipe != 0) {
595		if (hc_entry->rmx_recvpipe == 0)
596			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
597		else
598			hc_entry->rmx_recvpipe =
599			    (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
600		/* V_tcpstat.tcps_cachedrecvpipe++; */
601	}
602
603	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
604	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
605	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
606}
607
608/*
609 * Sysctl function: prints the list and values of all hostcache entries in
610 * unsorted order.
611 */
612static int
613sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
614{
615	INIT_VNET_INET(curvnet);
616	int bufsize;
617	int linesize = 128;
618	char *p, *buf;
619	int len, i, error;
620	struct hc_metrics *hc_entry;
621#ifdef INET6
622	char ip6buf[INET6_ADDRSTRLEN];
623#endif
624
625	bufsize = linesize * (V_tcp_hostcache.cache_count + 1);
626
627	p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
628
629	len = snprintf(p, linesize,
630		"\nIP address        MTU  SSTRESH      RTT   RTTVAR BANDWIDTH "
631		"    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
632	p += len;
633
634#define msec(u) (((u) + 500) / 1000)
635	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
636		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
637		TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
638			      rmx_q) {
639			len = snprintf(p, linesize,
640			    "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu "
641			    "%4lu %4lu %4i\n",
642			    hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
643#ifdef INET6
644				ip6_sprintf(ip6buf, &hc_entry->ip6),
645#else
646				"IPv6?",
647#endif
648			    hc_entry->rmx_mtu,
649			    hc_entry->rmx_ssthresh,
650			    msec(hc_entry->rmx_rtt *
651				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
652			    msec(hc_entry->rmx_rttvar *
653				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
654			    hc_entry->rmx_bandwidth * 8,
655			    hc_entry->rmx_cwnd,
656			    hc_entry->rmx_sendpipe,
657			    hc_entry->rmx_recvpipe,
658			    hc_entry->rmx_hits,
659			    hc_entry->rmx_updates,
660			    hc_entry->rmx_expire);
661			p += len;
662		}
663		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
664	}
665#undef msec
666	error = SYSCTL_OUT(req, buf, p - buf);
667	free(buf, M_TEMP);
668	return(error);
669}
670
671/*
672 * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
673 * periodically from the callout.
674 */
675static void
676tcp_hc_purge(void *arg)
677{
678	INIT_VNET_INET(curvnet);
679	struct hc_metrics *hc_entry, *hc_next;
680	int all = (intptr_t)arg;
681	int i;
682
683	if (V_tcp_hostcache.purgeall) {
684		all = 1;
685		V_tcp_hostcache.purgeall = 0;
686	}
687
688	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
689		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
690		TAILQ_FOREACH_SAFE(hc_entry,
691		    &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
692			if (all || hc_entry->rmx_expire <= 0) {
693				TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
694					      hc_entry, rmx_q);
695				uma_zfree(V_tcp_hostcache.zone, hc_entry);
696				V_tcp_hostcache.hashbase[i].hch_length--;
697				V_tcp_hostcache.cache_count--;
698			} else
699				hc_entry->rmx_expire -= V_tcp_hostcache.prune;
700		}
701		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
702	}
703
704	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
705	    tcp_hc_purge, arg);
706}
707