tcp_hostcache.c revision 181887
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
30122922Sandre/*
31170030Srwatson * The tcp_hostcache moves the tcp-specific cached metrics from the routing
32170030Srwatson * table to a dedicated structure indexed by the remote IP address.  It keeps
33170030Srwatson * information on the measured TCP parameters of past TCP sessions to allow
34170030Srwatson * better initial start values to be used with later connections to/from the
35170030Srwatson * same source.  Depending on the network parameters (delay, bandwidth, max
36170030Srwatson * MTU, congestion window) between local and remote sites, this can lead to
37170030Srwatson * significant speed-ups for new TCP connections after the first one.
38122922Sandre *
39170030Srwatson * Due to the tcp_hostcache, all TCP-specific metrics information in the
40170030Srwatson * routing table has been removed.  The inpcb no longer keeps a pointer to
41170030Srwatson * the routing entry, and protocol-initiated route cloning has been removed
42170030Srwatson * as well.  With these changes, the routing table has gone back to being
43170030Srwatson * more lightwight and only carries information related to packet forwarding.
44122922Sandre *
45170030Srwatson * tcp_hostcache is designed for multiple concurrent access in SMP
46170030Srwatson * environments and high contention.  All bucket rows have their own lock and
47170030Srwatson * thus multiple lookups and modifies can be done at the same time as long as
48170030Srwatson * they are in different bucket rows.  If a request for insertion of a new
49170030Srwatson * record can't be satisfied, it simply returns an empty structure.  Nobody
50170030Srwatson * and nothing outside of tcp_hostcache.c will ever point directly to any
51170030Srwatson * entry in the tcp_hostcache.  All communication is done in an
52170030Srwatson * object-oriented way and only functions of tcp_hostcache will manipulate
53170030Srwatson * hostcache entries.  Otherwise, we are unable to achieve good behaviour in
54170030Srwatson * concurrent access situations.  Since tcp_hostcache is only caching
55170030Srwatson * information, there are no fatal consequences if we either can't satisfy
56170030Srwatson * any particular request or have to drop/overwrite an existing entry because
57170030Srwatson * of bucket limit memory constrains.
58122922Sandre */
59122922Sandre
60122922Sandre/*
61122922Sandre * Many thanks to jlemon for basic structure of tcp_syncache which is being
62122922Sandre * followed here.
63122922Sandre */
64122922Sandre
65172467Ssilby#include <sys/cdefs.h>
66172467Ssilby__FBSDID("$FreeBSD: head/sys/netinet/tcp_hostcache.c 181887 2008-08-20 01:05:56Z julian $");
67172467Ssilby
68122922Sandre#include "opt_inet6.h"
69122922Sandre
70122922Sandre#include <sys/param.h>
71122922Sandre#include <sys/systm.h>
72122922Sandre#include <sys/kernel.h>
73122922Sandre#include <sys/lock.h>
74122922Sandre#include <sys/mutex.h>
75122922Sandre#include <sys/malloc.h>
76122922Sandre#include <sys/socket.h>
77122922Sandre#include <sys/socketvar.h>
78122922Sandre#include <sys/sysctl.h>
79181803Sbz#include <sys/vimage.h>
80122922Sandre
81122922Sandre#include <net/if.h>
82122922Sandre
83122922Sandre#include <netinet/in.h>
84122922Sandre#include <netinet/in_systm.h>
85122922Sandre#include <netinet/ip.h>
86122922Sandre#include <netinet/in_var.h>
87122922Sandre#include <netinet/in_pcb.h>
88122922Sandre#include <netinet/ip_var.h>
89122922Sandre#ifdef INET6
90122922Sandre#include <netinet/ip6.h>
91122922Sandre#include <netinet6/ip6_var.h>
92122922Sandre#endif
93122922Sandre#include <netinet/tcp.h>
94122922Sandre#include <netinet/tcp_var.h>
95122922Sandre#ifdef INET6
96122922Sandre#include <netinet6/tcp6_var.h>
97122922Sandre#endif
98122922Sandre
99122922Sandre#include <vm/uma.h>
100122922Sandre
101122922Sandre
102122922SandreTAILQ_HEAD(hc_qhead, hc_metrics);
103122922Sandre
104122922Sandrestruct hc_head {
105122922Sandre	struct hc_qhead	hch_bucket;
106122922Sandre	u_int		hch_length;
107122922Sandre	struct mtx	hch_mtx;
108122922Sandre};
109122922Sandre
110122922Sandrestruct hc_metrics {
111122922Sandre	/* housekeeping */
112122922Sandre	TAILQ_ENTRY(hc_metrics) rmx_q;
113122922Sandre	struct	hc_head *rmx_head; /* head of bucket tail queue */
114122922Sandre	struct	in_addr ip4;	/* IP address */
115122922Sandre	struct	in6_addr ip6;	/* IP6 address */
116170030Srwatson	/* endpoint specific values for TCP */
117122922Sandre	u_long	rmx_mtu;	/* MTU for this path */
118122922Sandre	u_long	rmx_ssthresh;	/* outbound gateway buffer limit */
119122922Sandre	u_long	rmx_rtt;	/* estimated round trip time */
120122922Sandre	u_long	rmx_rttvar;	/* estimated rtt variance */
121122922Sandre	u_long	rmx_bandwidth;	/* estimated bandwidth */
122122922Sandre	u_long	rmx_cwnd;	/* congestion window */
123122922Sandre	u_long	rmx_sendpipe;	/* outbound delay-bandwidth product */
124122922Sandre	u_long	rmx_recvpipe;	/* inbound delay-bandwidth product */
125170030Srwatson	/* TCP hostcache internal data */
126122922Sandre	int	rmx_expire;	/* lifetime for object */
127122922Sandre	u_long	rmx_hits;	/* number of hits */
128122922Sandre	u_long	rmx_updates;	/* number of updates */
129122922Sandre};
130122922Sandre
131122922Sandre/* Arbitrary values */
132122922Sandre#define TCP_HOSTCACHE_HASHSIZE		512
133122922Sandre#define TCP_HOSTCACHE_BUCKETLIMIT	30
134122922Sandre#define TCP_HOSTCACHE_EXPIRE		60*60	/* one hour */
135122922Sandre#define TCP_HOSTCACHE_PRUNE		5*60	/* every 5 minutes */
136122922Sandre
137122922Sandrestruct tcp_hostcache {
138122922Sandre	struct	hc_head *hashbase;
139122922Sandre	uma_zone_t zone;
140122922Sandre	u_int	hashsize;
141122922Sandre	u_int	hashmask;
142122922Sandre	u_int	bucket_limit;
143122922Sandre	u_int	cache_count;
144122922Sandre	u_int	cache_limit;
145122922Sandre	int	expire;
146170434Syar	int	prune;
147122922Sandre	int	purgeall;
148122922Sandre};
149122922Sandrestatic struct tcp_hostcache tcp_hostcache;
150122922Sandre
151122922Sandrestatic struct callout tcp_hc_callout;
152122922Sandre
153122922Sandrestatic struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
154122922Sandrestatic struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
155122922Sandrestatic int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
156122922Sandrestatic void tcp_hc_purge(void *);
157122922Sandre
158122922SandreSYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0, "TCP Host cache");
159122922Sandre
160122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_RDTUN,
161167784Sandre    &tcp_hostcache.cache_limit, 0, "Overall entry limit for hostcache");
162122922Sandre
163122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_RDTUN,
164167784Sandre    &tcp_hostcache.hashsize, 0, "Size of TCP hostcache hashtable");
165122922Sandre
166122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, CTLFLAG_RDTUN,
167167784Sandre    &tcp_hostcache.bucket_limit, 0, "Per-bucket hash limit for hostcache");
168122922Sandre
169122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_RD,
170167784Sandre    &tcp_hostcache.cache_count, 0, "Current number of entries in hostcache");
171122922Sandre
172122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_RW,
173167784Sandre    &tcp_hostcache.expire, 0, "Expire time of TCP hostcache entries");
174122922Sandre
175170434SyarSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_RW,
176170434Syar     &tcp_hostcache.prune, 0, "Time between purge runs");
177170434Syar
178122922SandreSYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_RW,
179167784Sandre    &tcp_hostcache.purgeall, 0, "Expire all entires on next purge run");
180122922Sandre
181122922SandreSYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
182167784Sandre    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
183167784Sandre    sysctl_tcp_hc_list, "A", "List of all hostcache entries");
184122922Sandre
185122922Sandre
186122922Sandrestatic MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
187122922Sandre
188122922Sandre#define HOSTCACHE_HASH(ip) \
189133874Srwatson	(((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) &	\
190181803Sbz	  V_tcp_hostcache.hashmask)
191122922Sandre
192122922Sandre/* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
193133874Srwatson#define HOSTCACHE_HASH6(ip6)				\
194122922Sandre	(((ip6)->s6_addr32[0] ^				\
195122922Sandre	  (ip6)->s6_addr32[1] ^				\
196122922Sandre	  (ip6)->s6_addr32[2] ^				\
197122922Sandre	  (ip6)->s6_addr32[3]) &			\
198181803Sbz	 V_tcp_hostcache.hashmask)
199122922Sandre
200122922Sandre#define THC_LOCK(lp)		mtx_lock(lp)
201122922Sandre#define THC_UNLOCK(lp)		mtx_unlock(lp)
202122922Sandre
203122922Sandrevoid
204122922Sandretcp_hc_init(void)
205122922Sandre{
206122922Sandre	int i;
207122922Sandre
208122922Sandre	/*
209170030Srwatson	 * Initialize hostcache structures.
210122922Sandre	 */
211181803Sbz	V_tcp_hostcache.cache_count = 0;
212181803Sbz	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
213181803Sbz	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
214181803Sbz	V_tcp_hostcache.cache_limit =
215181803Sbz	    V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
216181803Sbz	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
217181803Sbz	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
218122922Sandre
219133874Srwatson	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
220181803Sbz	    &V_tcp_hostcache.hashsize);
221133874Srwatson	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
222181803Sbz	    &V_tcp_hostcache.cache_limit);
223133874Srwatson	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
224181803Sbz	    &V_tcp_hostcache.bucket_limit);
225181803Sbz	if (!powerof2(V_tcp_hostcache.hashsize)) {
226133874Srwatson		printf("WARNING: hostcache hash size is not a power of 2.\n");
227181803Sbz		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
228133874Srwatson	}
229181803Sbz	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
230122922Sandre
231122922Sandre	/*
232170030Srwatson	 * Allocate the hash table.
233122922Sandre	 */
234181803Sbz	V_tcp_hostcache.hashbase = (struct hc_head *)
235181803Sbz	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
236122922Sandre		   M_HOSTCACHE, M_WAITOK | M_ZERO);
237122922Sandre
238122922Sandre	/*
239170030Srwatson	 * Initialize the hash buckets.
240122922Sandre	 */
241181803Sbz	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
242181803Sbz		TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
243181803Sbz		V_tcp_hostcache.hashbase[i].hch_length = 0;
244181803Sbz		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
245122922Sandre			  NULL, MTX_DEF);
246122922Sandre	}
247122922Sandre
248122922Sandre	/*
249122922Sandre	 * Allocate the hostcache entries.
250122922Sandre	 */
251181887Sjulian	V_tcp_hostcache.zone =
252181887Sjulian		uma_zcreate("hostcache", sizeof(struct hc_metrics),
253181887Sjulian			NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
254181803Sbz	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
255122922Sandre
256122922Sandre	/*
257122922Sandre	 * Set up periodic cache cleanup.
258122922Sandre	 */
259181803Sbz	callout_init(&V_tcp_hc_callout, CALLOUT_MPSAFE);
260181887Sjulian	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
261181887Sjulian	    tcp_hc_purge, 0);
262122922Sandre}
263122922Sandre
264122922Sandre/*
265170030Srwatson * Internal function: look up an entry in the hostcache or return NULL.
266122922Sandre *
267122922Sandre * If an entry has been returned, the caller becomes responsible for
268122922Sandre * unlocking the bucket row after he is done reading/modifying the entry.
269122922Sandre */
270122922Sandrestatic struct hc_metrics *
271122922Sandretcp_hc_lookup(struct in_conninfo *inc)
272122922Sandre{
273122922Sandre	int hash;
274122922Sandre	struct hc_head *hc_head;
275122922Sandre	struct hc_metrics *hc_entry;
276122922Sandre
277122922Sandre	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
278122922Sandre
279122922Sandre	/*
280122922Sandre	 * Hash the foreign ip address.
281122922Sandre	 */
282122922Sandre	if (inc->inc_isipv6)
283122922Sandre		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
284122922Sandre	else
285122922Sandre		hash = HOSTCACHE_HASH(&inc->inc_faddr);
286122922Sandre
287181803Sbz	hc_head = &V_tcp_hostcache.hashbase[hash];
288122922Sandre
289122922Sandre	/*
290170030Srwatson	 * Acquire lock for this bucket row; we release the lock if we don't
291170030Srwatson	 * find an entry, otherwise the caller has to unlock after he is
292170030Srwatson	 * done.
293122922Sandre	 */
294122922Sandre	THC_LOCK(&hc_head->hch_mtx);
295122922Sandre
296122922Sandre	/*
297170030Srwatson	 * Iterate through entries in bucket row looking for a match.
298122922Sandre	 */
299122922Sandre	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
300122922Sandre		if (inc->inc_isipv6) {
301122922Sandre			if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
302122922Sandre			    sizeof(inc->inc6_faddr)) == 0)
303122922Sandre				return hc_entry;
304122922Sandre		} else {
305122922Sandre			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
306122922Sandre			    sizeof(inc->inc_faddr)) == 0)
307122922Sandre				return hc_entry;
308122922Sandre		}
309122922Sandre	}
310122922Sandre
311122922Sandre	/*
312170030Srwatson	 * We were unsuccessful and didn't find anything.
313122922Sandre	 */
314122922Sandre	THC_UNLOCK(&hc_head->hch_mtx);
315122922Sandre	return NULL;
316122922Sandre}
317122922Sandre
318122922Sandre/*
319170030Srwatson * Internal function: insert an entry into the hostcache or return NULL if
320170030Srwatson * unable to allocate a new one.
321133874Srwatson *
322122922Sandre * If an entry has been returned, the caller becomes responsible for
323122922Sandre * unlocking the bucket row after he is done reading/modifying the entry.
324122922Sandre */
325122922Sandrestatic struct hc_metrics *
326122922Sandretcp_hc_insert(struct in_conninfo *inc)
327122922Sandre{
328122922Sandre	int hash;
329122922Sandre	struct hc_head *hc_head;
330122922Sandre	struct hc_metrics *hc_entry;
331122922Sandre
332122922Sandre	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
333122922Sandre
334122922Sandre	/*
335170030Srwatson	 * Hash the foreign ip address.
336122922Sandre	 */
337122922Sandre	if (inc->inc_isipv6)
338122922Sandre		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
339122922Sandre	else
340122922Sandre		hash = HOSTCACHE_HASH(&inc->inc_faddr);
341122922Sandre
342181803Sbz	hc_head = &V_tcp_hostcache.hashbase[hash];
343122922Sandre
344122922Sandre	/*
345170030Srwatson	 * Acquire lock for this bucket row; we release the lock if we don't
346170030Srwatson	 * find an entry, otherwise the caller has to unlock after he is
347170030Srwatson	 * done.
348122922Sandre	 */
349122922Sandre	THC_LOCK(&hc_head->hch_mtx);
350122922Sandre
351122922Sandre	/*
352170030Srwatson	 * If the bucket limit is reached, reuse the least-used element.
353122922Sandre	 */
354181803Sbz	if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
355181803Sbz	    V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) {
356122922Sandre		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
357122922Sandre		/*
358122922Sandre		 * At first we were dropping the last element, just to
359170030Srwatson		 * reacquire it in the next two lines again, which isn't very
360170030Srwatson		 * efficient.  Instead just reuse the least used element.
361170030Srwatson		 * We may drop something that is still "in-use" but we can be
362170030Srwatson		 * "lossy".
363170405Sandre		 * Just give up if this bucket row is empty and we don't have
364170405Sandre		 * anything to replace.
365122922Sandre		 */
366170405Sandre		if (hc_entry == NULL) {
367170405Sandre			THC_UNLOCK(&hc_head->hch_mtx);
368170405Sandre			return NULL;
369170405Sandre		}
370122922Sandre		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
371181803Sbz		V_tcp_hostcache.hashbase[hash].hch_length--;
372181803Sbz		V_tcp_hostcache.cache_count--;
373181803Sbz		V_tcpstat.tcps_hc_bucketoverflow++;
374123028Sandre#if 0
375181803Sbz		uma_zfree(V_tcp_hostcache.zone, hc_entry);
376122922Sandre#endif
377122922Sandre	} else {
378122922Sandre		/*
379170030Srwatson		 * Allocate a new entry, or balk if not possible.
380122922Sandre		 */
381181803Sbz		hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
382122922Sandre		if (hc_entry == NULL) {
383122922Sandre			THC_UNLOCK(&hc_head->hch_mtx);
384122922Sandre			return NULL;
385122922Sandre		}
386122922Sandre	}
387122922Sandre
388122922Sandre	/*
389170030Srwatson	 * Initialize basic information of hostcache entry.
390122922Sandre	 */
391122922Sandre	bzero(hc_entry, sizeof(*hc_entry));
392122922Sandre	if (inc->inc_isipv6)
393123113Sandre		bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6));
394122922Sandre	else
395122922Sandre		hc_entry->ip4 = inc->inc_faddr;
396122922Sandre	hc_entry->rmx_head = hc_head;
397181803Sbz	hc_entry->rmx_expire = V_tcp_hostcache.expire;
398122922Sandre
399122922Sandre	/*
400170030Srwatson	 * Put it upfront.
401122922Sandre	 */
402122922Sandre	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
403181803Sbz	V_tcp_hostcache.hashbase[hash].hch_length++;
404181803Sbz	V_tcp_hostcache.cache_count++;
405181803Sbz	V_tcpstat.tcps_hc_added++;
406122922Sandre
407122922Sandre	return hc_entry;
408122922Sandre}
409122922Sandre
410122922Sandre/*
411170030Srwatson * External function: look up an entry in the hostcache and fill out the
412170030Srwatson * supplied TCP metrics structure.  Fills in NULL when no entry was found or
413170030Srwatson * a value is not set.
414122922Sandre */
415122922Sandrevoid
416122922Sandretcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
417122922Sandre{
418122922Sandre	struct hc_metrics *hc_entry;
419122922Sandre
420122922Sandre	/*
421170030Srwatson	 * Find the right bucket.
422122922Sandre	 */
423122922Sandre	hc_entry = tcp_hc_lookup(inc);
424122922Sandre
425122922Sandre	/*
426170030Srwatson	 * If we don't have an existing object.
427122922Sandre	 */
428122922Sandre	if (hc_entry == NULL) {
429122922Sandre		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
430122922Sandre		return;
431122922Sandre	}
432122922Sandre	hc_entry->rmx_hits++;
433181803Sbz	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
434122922Sandre
435122922Sandre	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
436122922Sandre	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
437122922Sandre	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
438122922Sandre	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
439122922Sandre	hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth;
440122922Sandre	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
441122922Sandre	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
442122922Sandre	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
443122922Sandre
444122922Sandre	/*
445170030Srwatson	 * Unlock bucket row.
446122922Sandre	 */
447122922Sandre	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
448122922Sandre}
449122922Sandre
450122922Sandre/*
451170030Srwatson * External function: look up an entry in the hostcache and return the
452170030Srwatson * discovered path MTU.  Returns NULL if no entry is found or value is not
453138409Srwatson * set.
454122922Sandre */
455122922Sandreu_long
456122922Sandretcp_hc_getmtu(struct in_conninfo *inc)
457122922Sandre{
458122922Sandre	struct hc_metrics *hc_entry;
459122922Sandre	u_long mtu;
460122922Sandre
461122922Sandre	hc_entry = tcp_hc_lookup(inc);
462122922Sandre	if (hc_entry == NULL) {
463122922Sandre		return 0;
464122922Sandre	}
465122922Sandre	hc_entry->rmx_hits++;
466181803Sbz	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
467122922Sandre
468122922Sandre	mtu = hc_entry->rmx_mtu;
469122922Sandre	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
470122922Sandre	return mtu;
471122922Sandre}
472122922Sandre
473122922Sandre/*
474170030Srwatson * External function: update the MTU value of an entry in the hostcache.
475122922Sandre * Creates a new entry if none was found.
476122922Sandre */
477122922Sandrevoid
478122922Sandretcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
479122922Sandre{
480122922Sandre	struct hc_metrics *hc_entry;
481122922Sandre
482122922Sandre	/*
483170030Srwatson	 * Find the right bucket.
484122922Sandre	 */
485122922Sandre	hc_entry = tcp_hc_lookup(inc);
486122922Sandre
487122922Sandre	/*
488170030Srwatson	 * If we don't have an existing object, try to insert a new one.
489122922Sandre	 */
490122922Sandre	if (hc_entry == NULL) {
491122922Sandre		hc_entry = tcp_hc_insert(inc);
492122922Sandre		if (hc_entry == NULL)
493122922Sandre			return;
494122922Sandre	}
495122922Sandre	hc_entry->rmx_updates++;
496181803Sbz	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
497122922Sandre
498122922Sandre	hc_entry->rmx_mtu = mtu;
499122922Sandre
500122922Sandre	/*
501170030Srwatson	 * Put it upfront so we find it faster next time.
502122922Sandre	 */
503122922Sandre	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
504122922Sandre	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
505122922Sandre
506122922Sandre	/*
507170030Srwatson	 * Unlock bucket row.
508122922Sandre	 */
509122922Sandre	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
510122922Sandre}
511122922Sandre
512122922Sandre/*
513170030Srwatson * External function: update the TCP metrics of an entry in the hostcache.
514122922Sandre * Creates a new entry if none was found.
515122922Sandre */
516122922Sandrevoid
517122922Sandretcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
518122922Sandre{
519122922Sandre	struct hc_metrics *hc_entry;
520122922Sandre
521122922Sandre	hc_entry = tcp_hc_lookup(inc);
522122922Sandre	if (hc_entry == NULL) {
523122922Sandre		hc_entry = tcp_hc_insert(inc);
524122922Sandre		if (hc_entry == NULL)
525122922Sandre			return;
526122922Sandre	}
527122922Sandre	hc_entry->rmx_updates++;
528181803Sbz	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
529122922Sandre
530122922Sandre	if (hcml->rmx_rtt != 0) {
531122922Sandre		if (hc_entry->rmx_rtt == 0)
532122922Sandre			hc_entry->rmx_rtt = hcml->rmx_rtt;
533122922Sandre		else
534122922Sandre			hc_entry->rmx_rtt =
535122922Sandre			    (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
536181803Sbz		V_tcpstat.tcps_cachedrtt++;
537122922Sandre	}
538122922Sandre	if (hcml->rmx_rttvar != 0) {
539122922Sandre	        if (hc_entry->rmx_rttvar == 0)
540133874Srwatson			hc_entry->rmx_rttvar = hcml->rmx_rttvar;
541122922Sandre		else
542122922Sandre			hc_entry->rmx_rttvar =
543122922Sandre			    (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
544181803Sbz		V_tcpstat.tcps_cachedrttvar++;
545122922Sandre	}
546122922Sandre	if (hcml->rmx_ssthresh != 0) {
547122922Sandre		if (hc_entry->rmx_ssthresh == 0)
548122922Sandre			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
549122922Sandre		else
550122922Sandre			hc_entry->rmx_ssthresh =
551122922Sandre			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
552181803Sbz		V_tcpstat.tcps_cachedssthresh++;
553122922Sandre	}
554122922Sandre	if (hcml->rmx_bandwidth != 0) {
555122922Sandre		if (hc_entry->rmx_bandwidth == 0)
556122922Sandre			hc_entry->rmx_bandwidth = hcml->rmx_bandwidth;
557122922Sandre		else
558122922Sandre			hc_entry->rmx_bandwidth =
559122922Sandre			    (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2;
560181803Sbz		/* V_tcpstat.tcps_cachedbandwidth++; */
561122922Sandre	}
562122922Sandre	if (hcml->rmx_cwnd != 0) {
563122922Sandre		if (hc_entry->rmx_cwnd == 0)
564122922Sandre			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
565122922Sandre		else
566122922Sandre			hc_entry->rmx_cwnd =
567122922Sandre			    (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
568181803Sbz		/* V_tcpstat.tcps_cachedcwnd++; */
569122922Sandre	}
570122922Sandre	if (hcml->rmx_sendpipe != 0) {
571122922Sandre		if (hc_entry->rmx_sendpipe == 0)
572122922Sandre			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
573122922Sandre		else
574122922Sandre			hc_entry->rmx_sendpipe =
575122922Sandre			    (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
576181803Sbz		/* V_tcpstat.tcps_cachedsendpipe++; */
577133874Srwatson	}
578122922Sandre	if (hcml->rmx_recvpipe != 0) {
579122922Sandre		if (hc_entry->rmx_recvpipe == 0)
580122922Sandre			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
581122922Sandre		else
582122922Sandre			hc_entry->rmx_recvpipe =
583122922Sandre			    (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
584181803Sbz		/* V_tcpstat.tcps_cachedrecvpipe++; */
585122922Sandre	}
586122922Sandre
587122922Sandre	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
588122922Sandre	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
589122922Sandre	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
590122922Sandre}
591122922Sandre
592122922Sandre/*
593122922Sandre * Sysctl function: prints the list and values of all hostcache entries in
594122922Sandre * unsorted order.
595122922Sandre */
596122922Sandrestatic int
597122922Sandresysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
598122922Sandre{
599122922Sandre	int bufsize;
600122922Sandre	int linesize = 128;
601122922Sandre	char *p, *buf;
602122922Sandre	int len, i, error;
603122922Sandre	struct hc_metrics *hc_entry;
604165118Sbz#ifdef INET6
605165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
606165118Sbz#endif
607122922Sandre
608181803Sbz	bufsize = linesize * (V_tcp_hostcache.cache_count + 1);
609122922Sandre
610122922Sandre	p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
611122922Sandre
612122922Sandre	len = snprintf(p, linesize,
613122922Sandre		"\nIP address        MTU  SSTRESH      RTT   RTTVAR BANDWIDTH "
614122922Sandre		"    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
615122922Sandre	p += len;
616122922Sandre
617122922Sandre#define msec(u) (((u) + 500) / 1000)
618181803Sbz	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
619181803Sbz		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
620181803Sbz		TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
621122922Sandre			      rmx_q) {
622122922Sandre			len = snprintf(p, linesize,
623122922Sandre			    "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu "
624122922Sandre			    "%4lu %4lu %4i\n",
625122922Sandre			    hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
626122922Sandre#ifdef INET6
627165118Sbz				ip6_sprintf(ip6buf, &hc_entry->ip6),
628122922Sandre#else
629122922Sandre				"IPv6?",
630122922Sandre#endif
631122922Sandre			    hc_entry->rmx_mtu,
632122922Sandre			    hc_entry->rmx_ssthresh,
633122922Sandre			    msec(hc_entry->rmx_rtt *
634122922Sandre				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
635122922Sandre			    msec(hc_entry->rmx_rttvar *
636122922Sandre				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
637133477Sandre			    hc_entry->rmx_bandwidth * 8,
638122922Sandre			    hc_entry->rmx_cwnd,
639122922Sandre			    hc_entry->rmx_sendpipe,
640122922Sandre			    hc_entry->rmx_recvpipe,
641122922Sandre			    hc_entry->rmx_hits,
642122922Sandre			    hc_entry->rmx_updates,
643122922Sandre			    hc_entry->rmx_expire);
644122922Sandre			p += len;
645122922Sandre		}
646181803Sbz		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
647122922Sandre	}
648122922Sandre#undef msec
649122922Sandre	error = SYSCTL_OUT(req, buf, p - buf);
650122922Sandre	free(buf, M_TEMP);
651122922Sandre	return(error);
652122922Sandre}
653122922Sandre
654122922Sandre/*
655170030Srwatson * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
656170030Srwatson * periodically from the callout.
657122922Sandre */
658122922Sandrestatic void
659122922Sandretcp_hc_purge(void *arg)
660122922Sandre{
661128574Sandre	struct hc_metrics *hc_entry, *hc_next;
662122922Sandre	int all = (intptr_t)arg;
663122922Sandre	int i;
664122922Sandre
665181803Sbz	if (V_tcp_hostcache.purgeall) {
666122922Sandre		all = 1;
667181803Sbz		V_tcp_hostcache.purgeall = 0;
668122922Sandre	}
669122922Sandre
670181803Sbz	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
671181803Sbz		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
672181887Sjulian		TAILQ_FOREACH_SAFE(hc_entry,
673181887Sjulian		    &V_tcp_hostcache.hashbase[i].hch_bucket,
674181887Sjulian		    rmx_q, hc_next) {
675122922Sandre			if (all || hc_entry->rmx_expire <= 0) {
676181803Sbz				TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
677122922Sandre					      hc_entry, rmx_q);
678181803Sbz				uma_zfree(V_tcp_hostcache.zone, hc_entry);
679181803Sbz				V_tcp_hostcache.hashbase[i].hch_length--;
680181803Sbz				V_tcp_hostcache.cache_count--;
681122922Sandre			} else
682181803Sbz				hc_entry->rmx_expire -= V_tcp_hostcache.prune;
683122922Sandre		}
684181803Sbz		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
685122922Sandre	}
686181887Sjulian
687181887Sjulian	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
688181887Sjulian	    tcp_hc_purge, arg);
689122922Sandre}
690