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