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