tcp_hostcache.c revision 170434
1139823Simp/*-
2122922Sandre * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG
3122922Sandre * All rights reserved.
4122922Sandre *
5122922Sandre * Redistribution and use in source and binary forms, with or without
6122922Sandre * modification, are permitted provided that the following conditions
7122922Sandre * are met:
8122922Sandre * 1. Redistributions of source code must retain the above copyright
9122922Sandre *    notice, this list of conditions and the following disclaimer.
10122922Sandre * 2. Redistributions in binary form must reproduce the above copyright
11122922Sandre *    notice, this list of conditions and the following disclaimer in the
12122922Sandre *    documentation and/or other materials provided with the distribution.
13122922Sandre * 3. The name of the author may not be used to endorse or promote
14122922Sandre *    products derived from this software without specific prior written
15122922Sandre *    permission.
16122922Sandre *
17122922Sandre * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18122922Sandre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19122922Sandre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20122922Sandre * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21122922Sandre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22122922Sandre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23122922Sandre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24122922Sandre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25122922Sandre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26122922Sandre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27122922Sandre * SUCH DAMAGE.
28122922Sandre *
29122922Sandre * $FreeBSD: head/sys/netinet/tcp_hostcache.c 170434 2007-06-08 13:35:51Z yar $
30122922Sandre */
31122922Sandre
32122922Sandre/*
33170030Srwatson * The tcp_hostcache moves the tcp-specific cached metrics from the routing
34170030Srwatson * table to a dedicated structure indexed by the remote IP address.  It keeps
35170030Srwatson * information on the measured TCP parameters of past TCP sessions to allow
36170030Srwatson * better initial start values to be used with later connections to/from the
37170030Srwatson * same source.  Depending on the network parameters (delay, bandwidth, max
38170030Srwatson * MTU, congestion window) between local and remote sites, this can lead to
39170030Srwatson * significant speed-ups for new TCP connections after the first one.
40122922Sandre *
41170030Srwatson * Due to the tcp_hostcache, all TCP-specific metrics information in the
42170030Srwatson * routing table has been removed.  The inpcb no longer keeps a pointer to
43170030Srwatson * the routing entry, and protocol-initiated route cloning has been removed
44170030Srwatson * as well.  With these changes, the routing table has gone back to being
45170030Srwatson * more lightwight and only carries information related to packet forwarding.
46122922Sandre *
47170030Srwatson * tcp_hostcache is designed for multiple concurrent access in SMP
48170030Srwatson * environments and high contention.  All bucket rows have their own lock and
49170030Srwatson * thus multiple lookups and modifies can be done at the same time as long as
50170030Srwatson * they are in different bucket rows.  If a request for insertion of a new
51170030Srwatson * record can't be satisfied, it simply returns an empty structure.  Nobody
52170030Srwatson * and nothing outside of tcp_hostcache.c will ever point directly to any
53170030Srwatson * entry in the tcp_hostcache.  All communication is done in an
54170030Srwatson * object-oriented way and only functions of tcp_hostcache will manipulate
55170030Srwatson * hostcache entries.  Otherwise, we are unable to achieve good behaviour in
56170030Srwatson * concurrent access situations.  Since tcp_hostcache is only caching
57170030Srwatson * information, there are no fatal consequences if we either can't satisfy
58170030Srwatson * any particular request or have to drop/overwrite an existing entry because
59170030Srwatson * of bucket limit memory constrains.
60122922Sandre */
61122922Sandre
62122922Sandre/*
63122922Sandre * Many thanks to jlemon for basic structure of tcp_syncache which is being
64122922Sandre * followed here.
65122922Sandre */
66122922Sandre
67122922Sandre#include "opt_inet6.h"
68122922Sandre
69122922Sandre#include <sys/param.h>
70122922Sandre#include <sys/systm.h>
71122922Sandre#include <sys/kernel.h>
72122922Sandre#include <sys/lock.h>
73122922Sandre#include <sys/mutex.h>
74122922Sandre#include <sys/malloc.h>
75122922Sandre#include <sys/socket.h>
76122922Sandre#include <sys/socketvar.h>
77122922Sandre#include <sys/sysctl.h>
78122922Sandre
79122922Sandre#include <net/if.h>
80122922Sandre
81122922Sandre#include <netinet/in.h>
82122922Sandre#include <netinet/in_systm.h>
83122922Sandre#include <netinet/ip.h>
84122922Sandre#include <netinet/in_var.h>
85122922Sandre#include <netinet/in_pcb.h>
86122922Sandre#include <netinet/ip_var.h>
87122922Sandre#ifdef INET6
88122922Sandre#include <netinet/ip6.h>
89122922Sandre#include <netinet6/ip6_var.h>
90122922Sandre#endif
91122922Sandre#include <netinet/tcp.h>
92122922Sandre#include <netinet/tcp_var.h>
93122922Sandre#ifdef INET6
94122922Sandre#include <netinet6/tcp6_var.h>
95122922Sandre#endif
96122922Sandre
97122922Sandre#include <vm/uma.h>
98122922Sandre
99122922Sandre
100122922SandreTAILQ_HEAD(hc_qhead, hc_metrics);
101122922Sandre
102122922Sandrestruct hc_head {
103122922Sandre	struct hc_qhead	hch_bucket;
104122922Sandre	u_int		hch_length;
105122922Sandre	struct mtx	hch_mtx;
106122922Sandre};
107122922Sandre
108122922Sandrestruct hc_metrics {
109122922Sandre	/* housekeeping */
110122922Sandre	TAILQ_ENTRY(hc_metrics) rmx_q;
111122922Sandre	struct	hc_head *rmx_head; /* head of bucket tail queue */
112122922Sandre	struct	in_addr ip4;	/* IP address */
113122922Sandre	struct	in6_addr ip6;	/* IP6 address */
114170030Srwatson	/* endpoint specific values for TCP */
115122922Sandre	u_long	rmx_mtu;	/* MTU for this path */
116122922Sandre	u_long	rmx_ssthresh;	/* outbound gateway buffer limit */
117122922Sandre	u_long	rmx_rtt;	/* estimated round trip time */
118122922Sandre	u_long	rmx_rttvar;	/* estimated rtt variance */
119122922Sandre	u_long	rmx_bandwidth;	/* estimated bandwidth */
120122922Sandre	u_long	rmx_cwnd;	/* congestion window */
121122922Sandre	u_long	rmx_sendpipe;	/* outbound delay-bandwidth product */
122122922Sandre	u_long	rmx_recvpipe;	/* inbound delay-bandwidth product */
123170030Srwatson	/* TCP hostcache internal data */
124122922Sandre	int	rmx_expire;	/* lifetime for object */
125122922Sandre	u_long	rmx_hits;	/* number of hits */
126122922Sandre	u_long	rmx_updates;	/* number of updates */
127122922Sandre};
128122922Sandre
129122922Sandre/* Arbitrary values */
130122922Sandre#define TCP_HOSTCACHE_HASHSIZE		512
131122922Sandre#define TCP_HOSTCACHE_BUCKETLIMIT	30
132122922Sandre#define TCP_HOSTCACHE_EXPIRE		60*60	/* one hour */
133122922Sandre#define TCP_HOSTCACHE_PRUNE		5*60	/* every 5 minutes */
134122922Sandre
135122922Sandrestruct tcp_hostcache {
136122922Sandre	struct	hc_head *hashbase;
137122922Sandre	uma_zone_t zone;
138122922Sandre	u_int	hashsize;
139122922Sandre	u_int	hashmask;
140122922Sandre	u_int	bucket_limit;
141122922Sandre	u_int	cache_count;
142122922Sandre	u_int	cache_limit;
143122922Sandre	int	expire;
144170434Syar	int	prune;
145122922Sandre	int	purgeall;
146122922Sandre};
147122922Sandrestatic struct tcp_hostcache tcp_hostcache;
148122922Sandre
149122922Sandrestatic struct callout tcp_hc_callout;
150122922Sandre
151122922Sandrestatic struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
152122922Sandrestatic struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
153122922Sandrestatic int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
154122922Sandrestatic void tcp_hc_purge(void *);
155122922Sandre
156122922SandreSYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0, "TCP Host cache");
157122922Sandre
158122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_RDTUN,
159167784Sandre    &tcp_hostcache.cache_limit, 0, "Overall entry limit for hostcache");
160122922Sandre
161122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_RDTUN,
162167784Sandre    &tcp_hostcache.hashsize, 0, "Size of TCP hostcache hashtable");
163122922Sandre
164122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, CTLFLAG_RDTUN,
165167784Sandre    &tcp_hostcache.bucket_limit, 0, "Per-bucket hash limit for hostcache");
166122922Sandre
167122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_RD,
168167784Sandre    &tcp_hostcache.cache_count, 0, "Current number of entries in hostcache");
169122922Sandre
170122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_RW,
171167784Sandre    &tcp_hostcache.expire, 0, "Expire time of TCP hostcache entries");
172122922Sandre
173170434SyarSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_RW,
174170434Syar     &tcp_hostcache.prune, 0, "Time between purge runs");
175170434Syar
176122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_RW,
177167784Sandre    &tcp_hostcache.purgeall, 0, "Expire all entires on next purge run");
178122922Sandre
179122922SandreSYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
180167784Sandre    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
181167784Sandre    sysctl_tcp_hc_list, "A", "List of all hostcache entries");
182122922Sandre
183122922Sandre
184122922Sandrestatic MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
185122922Sandre
186122922Sandre#define HOSTCACHE_HASH(ip) \
187133874Srwatson	(((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) &	\
188122922Sandre	  tcp_hostcache.hashmask)
189122922Sandre
190122922Sandre/* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
191133874Srwatson#define HOSTCACHE_HASH6(ip6)				\
192122922Sandre	(((ip6)->s6_addr32[0] ^				\
193122922Sandre	  (ip6)->s6_addr32[1] ^				\
194122922Sandre	  (ip6)->s6_addr32[2] ^				\
195122922Sandre	  (ip6)->s6_addr32[3]) &			\
196122922Sandre	 tcp_hostcache.hashmask)
197122922Sandre
198122922Sandre#define THC_LOCK(lp)		mtx_lock(lp)
199122922Sandre#define THC_UNLOCK(lp)		mtx_unlock(lp)
200122922Sandre
201122922Sandrevoid
202122922Sandretcp_hc_init(void)
203122922Sandre{
204122922Sandre	int i;
205122922Sandre
206122922Sandre	/*
207170030Srwatson	 * Initialize hostcache structures.
208122922Sandre	 */
209122922Sandre	tcp_hostcache.cache_count = 0;
210122922Sandre	tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
211122922Sandre	tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
212122922Sandre	tcp_hostcache.cache_limit =
213122922Sandre	    tcp_hostcache.hashsize * tcp_hostcache.bucket_limit;
214122922Sandre	tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
215170434Syar	tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
216122922Sandre
217133874Srwatson	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
218122922Sandre	    &tcp_hostcache.hashsize);
219133874Srwatson	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
220122922Sandre	    &tcp_hostcache.cache_limit);
221133874Srwatson	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
222122922Sandre	    &tcp_hostcache.bucket_limit);
223122922Sandre	if (!powerof2(tcp_hostcache.hashsize)) {
224133874Srwatson		printf("WARNING: hostcache hash size is not a power of 2.\n");
225122922Sandre		tcp_hostcache.hashsize = 512;	/* safe default */
226133874Srwatson	}
227122922Sandre	tcp_hostcache.hashmask = tcp_hostcache.hashsize - 1;
228122922Sandre
229122922Sandre	/*
230170030Srwatson	 * Allocate the hash table.
231122922Sandre	 */
232122922Sandre	tcp_hostcache.hashbase = (struct hc_head *)
233122922Sandre	    malloc(tcp_hostcache.hashsize * sizeof(struct hc_head),
234122922Sandre		   M_HOSTCACHE, M_WAITOK | M_ZERO);
235122922Sandre
236122922Sandre	/*
237170030Srwatson	 * Initialize the hash buckets.
238122922Sandre	 */
239122922Sandre	for (i = 0; i < tcp_hostcache.hashsize; i++) {
240122922Sandre		TAILQ_INIT(&tcp_hostcache.hashbase[i].hch_bucket);
241122922Sandre		tcp_hostcache.hashbase[i].hch_length = 0;
242122922Sandre		mtx_init(&tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
243122922Sandre			  NULL, MTX_DEF);
244122922Sandre	}
245122922Sandre
246122922Sandre	/*
247122922Sandre	 * Allocate the hostcache entries.
248122922Sandre	 */
249122922Sandre	tcp_hostcache.zone = uma_zcreate("hostcache", sizeof(struct hc_metrics),
250133509Sandre	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
251122922Sandre	uma_zone_set_max(tcp_hostcache.zone, tcp_hostcache.cache_limit);
252122922Sandre
253122922Sandre	/*
254122922Sandre	 * Set up periodic cache cleanup.
255122922Sandre	 */
256122922Sandre	callout_init(&tcp_hc_callout, CALLOUT_MPSAFE);
257170434Syar	callout_reset(&tcp_hc_callout, tcp_hostcache.prune * hz, tcp_hc_purge, 0);
258122922Sandre}
259122922Sandre
260122922Sandre/*
261170030Srwatson * Internal function: look up an entry in the hostcache or return NULL.
262122922Sandre *
263122922Sandre * If an entry has been returned, the caller becomes responsible for
264122922Sandre * unlocking the bucket row after he is done reading/modifying the entry.
265122922Sandre */
266122922Sandrestatic struct hc_metrics *
267122922Sandretcp_hc_lookup(struct in_conninfo *inc)
268122922Sandre{
269122922Sandre	int hash;
270122922Sandre	struct hc_head *hc_head;
271122922Sandre	struct hc_metrics *hc_entry;
272122922Sandre
273122922Sandre	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
274122922Sandre
275122922Sandre	/*
276122922Sandre	 * Hash the foreign ip address.
277122922Sandre	 */
278122922Sandre	if (inc->inc_isipv6)
279122922Sandre		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
280122922Sandre	else
281122922Sandre		hash = HOSTCACHE_HASH(&inc->inc_faddr);
282122922Sandre
283122922Sandre	hc_head = &tcp_hostcache.hashbase[hash];
284122922Sandre
285122922Sandre	/*
286170030Srwatson	 * Acquire lock for this bucket row; we release the lock if we don't
287170030Srwatson	 * find an entry, otherwise the caller has to unlock after he is
288170030Srwatson	 * done.
289122922Sandre	 */
290122922Sandre	THC_LOCK(&hc_head->hch_mtx);
291122922Sandre
292122922Sandre	/*
293170030Srwatson	 * Iterate through entries in bucket row looking for a match.
294122922Sandre	 */
295122922Sandre	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
296122922Sandre		if (inc->inc_isipv6) {
297122922Sandre			if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
298122922Sandre			    sizeof(inc->inc6_faddr)) == 0)
299122922Sandre				return hc_entry;
300122922Sandre		} else {
301122922Sandre			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
302122922Sandre			    sizeof(inc->inc_faddr)) == 0)
303122922Sandre				return hc_entry;
304122922Sandre		}
305122922Sandre	}
306122922Sandre
307122922Sandre	/*
308170030Srwatson	 * We were unsuccessful and didn't find anything.
309122922Sandre	 */
310122922Sandre	THC_UNLOCK(&hc_head->hch_mtx);
311122922Sandre	return NULL;
312122922Sandre}
313122922Sandre
314122922Sandre/*
315170030Srwatson * Internal function: insert an entry into the hostcache or return NULL if
316170030Srwatson * unable to allocate a new one.
317133874Srwatson *
318122922Sandre * If an entry has been returned, the caller becomes responsible for
319122922Sandre * unlocking the bucket row after he is done reading/modifying the entry.
320122922Sandre */
321122922Sandrestatic struct hc_metrics *
322122922Sandretcp_hc_insert(struct in_conninfo *inc)
323122922Sandre{
324122922Sandre	int hash;
325122922Sandre	struct hc_head *hc_head;
326122922Sandre	struct hc_metrics *hc_entry;
327122922Sandre
328122922Sandre	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
329122922Sandre
330122922Sandre	/*
331170030Srwatson	 * Hash the foreign ip address.
332122922Sandre	 */
333122922Sandre	if (inc->inc_isipv6)
334122922Sandre		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
335122922Sandre	else
336122922Sandre		hash = HOSTCACHE_HASH(&inc->inc_faddr);
337122922Sandre
338122922Sandre	hc_head = &tcp_hostcache.hashbase[hash];
339122922Sandre
340122922Sandre	/*
341170030Srwatson	 * Acquire lock for this bucket row; we release the lock if we don't
342170030Srwatson	 * find an entry, otherwise the caller has to unlock after he is
343170030Srwatson	 * done.
344122922Sandre	 */
345122922Sandre	THC_LOCK(&hc_head->hch_mtx);
346122922Sandre
347122922Sandre	/*
348170030Srwatson	 * If the bucket limit is reached, reuse the least-used element.
349122922Sandre	 */
350122922Sandre	if (hc_head->hch_length >= tcp_hostcache.bucket_limit ||
351122922Sandre	    tcp_hostcache.cache_count >= tcp_hostcache.cache_limit) {
352122922Sandre		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
353122922Sandre		/*
354122922Sandre		 * At first we were dropping the last element, just to
355170030Srwatson		 * reacquire it in the next two lines again, which isn't very
356170030Srwatson		 * efficient.  Instead just reuse the least used element.
357170030Srwatson		 * We may drop something that is still "in-use" but we can be
358170030Srwatson		 * "lossy".
359170405Sandre		 * Just give up if this bucket row is empty and we don't have
360170405Sandre		 * anything to replace.
361122922Sandre		 */
362170405Sandre		if (hc_entry == NULL) {
363170405Sandre			THC_UNLOCK(&hc_head->hch_mtx);
364170405Sandre			return NULL;
365170405Sandre		}
366122922Sandre		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
367122922Sandre		tcp_hostcache.hashbase[hash].hch_length--;
368122922Sandre		tcp_hostcache.cache_count--;
369123028Sandre		tcpstat.tcps_hc_bucketoverflow++;
370123028Sandre#if 0
371123028Sandre		uma_zfree(tcp_hostcache.zone, hc_entry);
372122922Sandre#endif
373122922Sandre	} else {
374122922Sandre		/*
375170030Srwatson		 * Allocate a new entry, or balk if not possible.
376122922Sandre		 */
377122922Sandre		hc_entry = uma_zalloc(tcp_hostcache.zone, M_NOWAIT);
378122922Sandre		if (hc_entry == NULL) {
379122922Sandre			THC_UNLOCK(&hc_head->hch_mtx);
380122922Sandre			return NULL;
381122922Sandre		}
382122922Sandre	}
383122922Sandre
384122922Sandre	/*
385170030Srwatson	 * Initialize basic information of hostcache entry.
386122922Sandre	 */
387122922Sandre	bzero(hc_entry, sizeof(*hc_entry));
388122922Sandre	if (inc->inc_isipv6)
389123113Sandre		bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6));
390122922Sandre	else
391122922Sandre		hc_entry->ip4 = inc->inc_faddr;
392122922Sandre	hc_entry->rmx_head = hc_head;
393122922Sandre	hc_entry->rmx_expire = tcp_hostcache.expire;
394122922Sandre
395122922Sandre	/*
396170030Srwatson	 * Put it upfront.
397122922Sandre	 */
398122922Sandre	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
399122922Sandre	tcp_hostcache.hashbase[hash].hch_length++;
400122922Sandre	tcp_hostcache.cache_count++;
401122922Sandre	tcpstat.tcps_hc_added++;
402122922Sandre
403122922Sandre	return hc_entry;
404122922Sandre}
405122922Sandre
406122922Sandre/*
407170030Srwatson * External function: look up an entry in the hostcache and fill out the
408170030Srwatson * supplied TCP metrics structure.  Fills in NULL when no entry was found or
409170030Srwatson * a value is not set.
410122922Sandre */
411122922Sandrevoid
412122922Sandretcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
413122922Sandre{
414122922Sandre	struct hc_metrics *hc_entry;
415122922Sandre
416122922Sandre	/*
417170030Srwatson	 * Find the right bucket.
418122922Sandre	 */
419122922Sandre	hc_entry = tcp_hc_lookup(inc);
420122922Sandre
421122922Sandre	/*
422170030Srwatson	 * If we don't have an existing object.
423122922Sandre	 */
424122922Sandre	if (hc_entry == NULL) {
425122922Sandre		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
426122922Sandre		return;
427122922Sandre	}
428122922Sandre	hc_entry->rmx_hits++;
429122922Sandre	hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
430122922Sandre
431122922Sandre	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
432122922Sandre	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
433122922Sandre	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
434122922Sandre	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
435122922Sandre	hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth;
436122922Sandre	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
437122922Sandre	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
438122922Sandre	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
439122922Sandre
440122922Sandre	/*
441170030Srwatson	 * Unlock bucket row.
442122922Sandre	 */
443122922Sandre	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
444122922Sandre}
445122922Sandre
446122922Sandre/*
447170030Srwatson * External function: look up an entry in the hostcache and return the
448170030Srwatson * discovered path MTU.  Returns NULL if no entry is found or value is not
449138409Srwatson * set.
450122922Sandre */
451122922Sandreu_long
452122922Sandretcp_hc_getmtu(struct in_conninfo *inc)
453122922Sandre{
454122922Sandre	struct hc_metrics *hc_entry;
455122922Sandre	u_long mtu;
456122922Sandre
457122922Sandre	hc_entry = tcp_hc_lookup(inc);
458122922Sandre	if (hc_entry == NULL) {
459122922Sandre		return 0;
460122922Sandre	}
461122922Sandre	hc_entry->rmx_hits++;
462122922Sandre	hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
463122922Sandre
464122922Sandre	mtu = hc_entry->rmx_mtu;
465122922Sandre	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
466122922Sandre	return mtu;
467122922Sandre}
468122922Sandre
469122922Sandre/*
470170030Srwatson * External function: update the MTU value of an entry in the hostcache.
471122922Sandre * Creates a new entry if none was found.
472122922Sandre */
473122922Sandrevoid
474122922Sandretcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
475122922Sandre{
476122922Sandre	struct hc_metrics *hc_entry;
477122922Sandre
478122922Sandre	/*
479170030Srwatson	 * Find the right bucket.
480122922Sandre	 */
481122922Sandre	hc_entry = tcp_hc_lookup(inc);
482122922Sandre
483122922Sandre	/*
484170030Srwatson	 * If we don't have an existing object, try to insert a new one.
485122922Sandre	 */
486122922Sandre	if (hc_entry == NULL) {
487122922Sandre		hc_entry = tcp_hc_insert(inc);
488122922Sandre		if (hc_entry == NULL)
489122922Sandre			return;
490122922Sandre	}
491122922Sandre	hc_entry->rmx_updates++;
492122922Sandre	hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
493122922Sandre
494122922Sandre	hc_entry->rmx_mtu = mtu;
495122922Sandre
496122922Sandre	/*
497170030Srwatson	 * Put it upfront so we find it faster next time.
498122922Sandre	 */
499122922Sandre	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
500122922Sandre	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
501122922Sandre
502122922Sandre	/*
503170030Srwatson	 * Unlock bucket row.
504122922Sandre	 */
505122922Sandre	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
506122922Sandre}
507122922Sandre
508122922Sandre/*
509170030Srwatson * External function: update the TCP metrics of an entry in the hostcache.
510122922Sandre * Creates a new entry if none was found.
511122922Sandre */
512122922Sandrevoid
513122922Sandretcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
514122922Sandre{
515122922Sandre	struct hc_metrics *hc_entry;
516122922Sandre
517122922Sandre	hc_entry = tcp_hc_lookup(inc);
518122922Sandre	if (hc_entry == NULL) {
519122922Sandre		hc_entry = tcp_hc_insert(inc);
520122922Sandre		if (hc_entry == NULL)
521122922Sandre			return;
522122922Sandre	}
523122922Sandre	hc_entry->rmx_updates++;
524122922Sandre	hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
525122922Sandre
526122922Sandre	if (hcml->rmx_rtt != 0) {
527122922Sandre		if (hc_entry->rmx_rtt == 0)
528122922Sandre			hc_entry->rmx_rtt = hcml->rmx_rtt;
529122922Sandre		else
530122922Sandre			hc_entry->rmx_rtt =
531122922Sandre			    (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
532122922Sandre		tcpstat.tcps_cachedrtt++;
533122922Sandre	}
534122922Sandre	if (hcml->rmx_rttvar != 0) {
535122922Sandre	        if (hc_entry->rmx_rttvar == 0)
536133874Srwatson			hc_entry->rmx_rttvar = hcml->rmx_rttvar;
537122922Sandre		else
538122922Sandre			hc_entry->rmx_rttvar =
539122922Sandre			    (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
540122922Sandre		tcpstat.tcps_cachedrttvar++;
541122922Sandre	}
542122922Sandre	if (hcml->rmx_ssthresh != 0) {
543122922Sandre		if (hc_entry->rmx_ssthresh == 0)
544122922Sandre			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
545122922Sandre		else
546122922Sandre			hc_entry->rmx_ssthresh =
547122922Sandre			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
548122922Sandre		tcpstat.tcps_cachedssthresh++;
549122922Sandre	}
550122922Sandre	if (hcml->rmx_bandwidth != 0) {
551122922Sandre		if (hc_entry->rmx_bandwidth == 0)
552122922Sandre			hc_entry->rmx_bandwidth = hcml->rmx_bandwidth;
553122922Sandre		else
554122922Sandre			hc_entry->rmx_bandwidth =
555122922Sandre			    (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2;
556122922Sandre		/* tcpstat.tcps_cachedbandwidth++; */
557122922Sandre	}
558122922Sandre	if (hcml->rmx_cwnd != 0) {
559122922Sandre		if (hc_entry->rmx_cwnd == 0)
560122922Sandre			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
561122922Sandre		else
562122922Sandre			hc_entry->rmx_cwnd =
563122922Sandre			    (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
564122922Sandre		/* tcpstat.tcps_cachedcwnd++; */
565122922Sandre	}
566122922Sandre	if (hcml->rmx_sendpipe != 0) {
567122922Sandre		if (hc_entry->rmx_sendpipe == 0)
568122922Sandre			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
569122922Sandre		else
570122922Sandre			hc_entry->rmx_sendpipe =
571122922Sandre			    (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
572133874Srwatson		/* tcpstat.tcps_cachedsendpipe++; */
573133874Srwatson	}
574122922Sandre	if (hcml->rmx_recvpipe != 0) {
575122922Sandre		if (hc_entry->rmx_recvpipe == 0)
576122922Sandre			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
577122922Sandre		else
578122922Sandre			hc_entry->rmx_recvpipe =
579122922Sandre			    (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
580122922Sandre		/* tcpstat.tcps_cachedrecvpipe++; */
581122922Sandre	}
582122922Sandre
583122922Sandre	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
584122922Sandre	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
585122922Sandre	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
586122922Sandre}
587122922Sandre
588122922Sandre/*
589122922Sandre * Sysctl function: prints the list and values of all hostcache entries in
590122922Sandre * unsorted order.
591122922Sandre */
592122922Sandrestatic int
593122922Sandresysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
594122922Sandre{
595122922Sandre	int bufsize;
596122922Sandre	int linesize = 128;
597122922Sandre	char *p, *buf;
598122922Sandre	int len, i, error;
599122922Sandre	struct hc_metrics *hc_entry;
600165118Sbz#ifdef INET6
601165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
602165118Sbz#endif
603122922Sandre
604122922Sandre	bufsize = linesize * (tcp_hostcache.cache_count + 1);
605122922Sandre
606122922Sandre	p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
607122922Sandre
608122922Sandre	len = snprintf(p, linesize,
609122922Sandre		"\nIP address        MTU  SSTRESH      RTT   RTTVAR BANDWIDTH "
610122922Sandre		"    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
611122922Sandre	p += len;
612122922Sandre
613122922Sandre#define msec(u) (((u) + 500) / 1000)
614122922Sandre	for (i = 0; i < tcp_hostcache.hashsize; i++) {
615122922Sandre		THC_LOCK(&tcp_hostcache.hashbase[i].hch_mtx);
616122922Sandre		TAILQ_FOREACH(hc_entry, &tcp_hostcache.hashbase[i].hch_bucket,
617122922Sandre			      rmx_q) {
618122922Sandre			len = snprintf(p, linesize,
619122922Sandre			    "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu "
620122922Sandre			    "%4lu %4lu %4i\n",
621122922Sandre			    hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
622122922Sandre#ifdef INET6
623165118Sbz				ip6_sprintf(ip6buf, &hc_entry->ip6),
624122922Sandre#else
625122922Sandre				"IPv6?",
626122922Sandre#endif
627122922Sandre			    hc_entry->rmx_mtu,
628122922Sandre			    hc_entry->rmx_ssthresh,
629122922Sandre			    msec(hc_entry->rmx_rtt *
630122922Sandre				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
631122922Sandre			    msec(hc_entry->rmx_rttvar *
632122922Sandre				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
633133477Sandre			    hc_entry->rmx_bandwidth * 8,
634122922Sandre			    hc_entry->rmx_cwnd,
635122922Sandre			    hc_entry->rmx_sendpipe,
636122922Sandre			    hc_entry->rmx_recvpipe,
637122922Sandre			    hc_entry->rmx_hits,
638122922Sandre			    hc_entry->rmx_updates,
639122922Sandre			    hc_entry->rmx_expire);
640122922Sandre			p += len;
641122922Sandre		}
642122922Sandre		THC_UNLOCK(&tcp_hostcache.hashbase[i].hch_mtx);
643122922Sandre	}
644122922Sandre#undef msec
645122922Sandre	error = SYSCTL_OUT(req, buf, p - buf);
646122922Sandre	free(buf, M_TEMP);
647122922Sandre	return(error);
648122922Sandre}
649122922Sandre
650122922Sandre/*
651170030Srwatson * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
652170030Srwatson * periodically from the callout.
653122922Sandre */
654122922Sandrestatic void
655122922Sandretcp_hc_purge(void *arg)
656122922Sandre{
657128574Sandre	struct hc_metrics *hc_entry, *hc_next;
658122922Sandre	int all = (intptr_t)arg;
659122922Sandre	int i;
660122922Sandre
661122922Sandre	if (tcp_hostcache.purgeall) {
662122922Sandre		all = 1;
663122922Sandre		tcp_hostcache.purgeall = 0;
664122922Sandre	}
665122922Sandre
666122922Sandre	for (i = 0; i < tcp_hostcache.hashsize; i++) {
667122922Sandre		THC_LOCK(&tcp_hostcache.hashbase[i].hch_mtx);
668128574Sandre		TAILQ_FOREACH_SAFE(hc_entry, &tcp_hostcache.hashbase[i].hch_bucket,
669128574Sandre			      rmx_q, hc_next) {
670122922Sandre			if (all || hc_entry->rmx_expire <= 0) {
671122922Sandre				TAILQ_REMOVE(&tcp_hostcache.hashbase[i].hch_bucket,
672122922Sandre					      hc_entry, rmx_q);
673122922Sandre				uma_zfree(tcp_hostcache.zone, hc_entry);
674122922Sandre				tcp_hostcache.hashbase[i].hch_length--;
675122922Sandre				tcp_hostcache.cache_count--;
676122922Sandre			} else
677170434Syar				hc_entry->rmx_expire -= tcp_hostcache.prune;
678122922Sandre		}
679122922Sandre		THC_UNLOCK(&tcp_hostcache.hashbase[i].hch_mtx);
680122922Sandre	}
681170434Syar	callout_reset(&tcp_hc_callout, tcp_hostcache.prune * hz, tcp_hc_purge, 0);
682122922Sandre}
683