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