xref: /freebsd/sys/netinet/tcp_hostcache.c (revision f8b329f0ed4e6d60645e4a0715ecb1a49fd95b00)
1 /*-
2  * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * The tcp_hostcache moves the tcp-specific cached metrics from the routing
32  * table to a dedicated structure indexed by the remote IP address.  It keeps
33  * information on the measured TCP parameters of past TCP sessions to allow
34  * better initial start values to be used with later connections to/from the
35  * same source.  Depending on the network parameters (delay, bandwidth, max
36  * MTU, congestion window) between local and remote sites, this can lead to
37  * significant speed-ups for new TCP connections after the first one.
38  *
39  * Due to the tcp_hostcache, all TCP-specific metrics information in the
40  * routing table have been removed.  The inpcb no longer keeps a pointer to
41  * the routing entry, and protocol-initiated route cloning has been removed
42  * as well.  With these changes, the routing table has gone back to being
43  * more lightwight and only carries information related to packet forwarding.
44  *
45  * tcp_hostcache is designed for multiple concurrent access in SMP
46  * environments and high contention.  All bucket rows have their own lock and
47  * thus multiple lookups and modifies can be done at the same time as long as
48  * they are in different bucket rows.  If a request for insertion of a new
49  * record can't be satisfied, it simply returns an empty structure.  Nobody
50  * and nothing outside of tcp_hostcache.c will ever point directly to any
51  * entry in the tcp_hostcache.  All communication is done in an
52  * object-oriented way and only functions of tcp_hostcache will manipulate
53  * hostcache entries.  Otherwise, we are unable to achieve good behaviour in
54  * concurrent access situations.  Since tcp_hostcache is only caching
55  * information, there are no fatal consequences if we either can't satisfy
56  * any particular request or have to drop/overwrite an existing entry because
57  * of bucket limit memory constrains.
58  */
59 
60 /*
61  * Many thanks to jlemon for basic structure of tcp_syncache which is being
62  * followed here.
63  */
64 
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
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 
102 TAILQ_HEAD(hc_qhead, hc_metrics);
103 
104 struct hc_head {
105 	struct hc_qhead	hch_bucket;
106 	u_int		hch_length;
107 	struct mtx	hch_mtx;
108 };
109 
110 struct 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 
137 struct 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 };
149 static struct tcp_hostcache tcp_hostcache;
150 
151 static struct callout tcp_hc_callout;
152 
153 static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
154 static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
155 static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
156 static void tcp_hc_purge(void *);
157 
158 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0,
159     "TCP Host cache");
160 
161 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_RDTUN,
162     &tcp_hostcache.cache_limit, 0, "Overall entry limit for hostcache");
163 
164 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_RDTUN,
165     &tcp_hostcache.hashsize, 0, "Size of TCP hostcache hashtable");
166 
167 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, CTLFLAG_RDTUN,
168     &tcp_hostcache.bucket_limit, 0, "Per-bucket hash limit for hostcache");
169 
170 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_RD,
171     &tcp_hostcache.cache_count, 0, "Current number of entries in hostcache");
172 
173 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_RW,
174     &tcp_hostcache.expire, 0, "Expire time of TCP hostcache entries");
175 
176 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_RW,
177      &tcp_hostcache.prune, 0, "Time between purge runs");
178 
179 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_RW,
180     &tcp_hostcache.purgeall, 0, "Expire all entires on next purge run");
181 
182 SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
183     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
184     sysctl_tcp_hc_list, "A", "List of all hostcache entries");
185 
186 
187 static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
188 
189 #define HOSTCACHE_HASH(ip) \
190 	(((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) &	\
191 	  V_tcp_hostcache.hashmask)
192 
193 /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
194 #define HOSTCACHE_HASH6(ip6)				\
195 	(((ip6)->s6_addr32[0] ^				\
196 	  (ip6)->s6_addr32[1] ^				\
197 	  (ip6)->s6_addr32[2] ^				\
198 	  (ip6)->s6_addr32[3]) &			\
199 	 V_tcp_hostcache.hashmask)
200 
201 #define THC_LOCK(lp)		mtx_lock(lp)
202 #define THC_UNLOCK(lp)		mtx_unlock(lp)
203 
204 void
205 tcp_hc_init(void)
206 {
207 	int i;
208 
209 	/*
210 	 * Initialize hostcache structures.
211 	 */
212 	V_tcp_hostcache.cache_count = 0;
213 	V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
214 	V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
215 	V_tcp_hostcache.cache_limit =
216 	    V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
217 	V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
218 	V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
219 
220 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
221 	    &V_tcp_hostcache.hashsize);
222 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
223 	    &V_tcp_hostcache.cache_limit);
224 	TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
225 	    &V_tcp_hostcache.bucket_limit);
226 	if (!powerof2(V_tcp_hostcache.hashsize)) {
227 		printf("WARNING: hostcache hash size is not a power of 2.\n");
228 		V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
229 	}
230 	V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
231 
232 	/*
233 	 * Allocate the hash table.
234 	 */
235 	V_tcp_hostcache.hashbase = (struct hc_head *)
236 	    malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
237 		   M_HOSTCACHE, M_WAITOK | M_ZERO);
238 
239 	/*
240 	 * Initialize the hash buckets.
241 	 */
242 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
243 		TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
244 		V_tcp_hostcache.hashbase[i].hch_length = 0;
245 		mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
246 			  NULL, MTX_DEF);
247 	}
248 
249 	/*
250 	 * Allocate the hostcache entries.
251 	 */
252 	V_tcp_hostcache.zone =
253 	    uma_zcreate("hostcache", sizeof(struct hc_metrics),
254 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
255 	uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
256 
257 	/*
258 	 * Set up periodic cache cleanup.
259 	 */
260 	callout_init(&V_tcp_hc_callout, CALLOUT_MPSAFE);
261 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
262 	    tcp_hc_purge, 0);
263 }
264 
265 /*
266  * Internal function: look up an entry in the hostcache or return NULL.
267  *
268  * If an entry has been returned, the caller becomes responsible for
269  * unlocking the bucket row after he is done reading/modifying the entry.
270  */
271 static struct hc_metrics *
272 tcp_hc_lookup(struct in_conninfo *inc)
273 {
274 	int hash;
275 	struct hc_head *hc_head;
276 	struct hc_metrics *hc_entry;
277 
278 	KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
279 
280 	/*
281 	 * Hash the foreign ip address.
282 	 */
283 	if (inc->inc_isipv6)
284 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
285 	else
286 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
287 
288 	hc_head = &V_tcp_hostcache.hashbase[hash];
289 
290 	/*
291 	 * Acquire lock for this bucket row; we release the lock if we don't
292 	 * find an entry, otherwise the caller has to unlock after he is
293 	 * done.
294 	 */
295 	THC_LOCK(&hc_head->hch_mtx);
296 
297 	/*
298 	 * Iterate through entries in bucket row looking for a match.
299 	 */
300 	TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
301 		if (inc->inc_isipv6) {
302 			if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
303 			    sizeof(inc->inc6_faddr)) == 0)
304 				return hc_entry;
305 		} else {
306 			if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
307 			    sizeof(inc->inc_faddr)) == 0)
308 				return hc_entry;
309 		}
310 	}
311 
312 	/*
313 	 * We were unsuccessful and didn't find anything.
314 	 */
315 	THC_UNLOCK(&hc_head->hch_mtx);
316 	return NULL;
317 }
318 
319 /*
320  * Internal function: insert an entry into the hostcache or return NULL if
321  * unable to allocate a new one.
322  *
323  * If an entry has been returned, the caller becomes responsible for
324  * unlocking the bucket row after he is done reading/modifying the entry.
325  */
326 static struct hc_metrics *
327 tcp_hc_insert(struct in_conninfo *inc)
328 {
329 	int hash;
330 	struct hc_head *hc_head;
331 	struct hc_metrics *hc_entry;
332 
333 	KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
334 
335 	/*
336 	 * Hash the foreign ip address.
337 	 */
338 	if (inc->inc_isipv6)
339 		hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
340 	else
341 		hash = HOSTCACHE_HASH(&inc->inc_faddr);
342 
343 	hc_head = &V_tcp_hostcache.hashbase[hash];
344 
345 	/*
346 	 * Acquire lock for this bucket row; we release the lock if we don't
347 	 * find an entry, otherwise the caller has to unlock after he is
348 	 * done.
349 	 */
350 	THC_LOCK(&hc_head->hch_mtx);
351 
352 	/*
353 	 * If the bucket limit is reached, reuse the least-used element.
354 	 */
355 	if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
356 	    V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) {
357 		hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
358 		/*
359 		 * At first we were dropping the last element, just to
360 		 * reacquire it in the next two lines again, which isn't very
361 		 * efficient.  Instead just reuse the least used element.
362 		 * We may drop something that is still "in-use" but we can be
363 		 * "lossy".
364 		 * Just give up if this bucket row is empty and we don't have
365 		 * anything to replace.
366 		 */
367 		if (hc_entry == NULL) {
368 			THC_UNLOCK(&hc_head->hch_mtx);
369 			return NULL;
370 		}
371 		TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
372 		V_tcp_hostcache.hashbase[hash].hch_length--;
373 		V_tcp_hostcache.cache_count--;
374 		V_tcpstat.tcps_hc_bucketoverflow++;
375 #if 0
376 		uma_zfree(V_tcp_hostcache.zone, hc_entry);
377 #endif
378 	} else {
379 		/*
380 		 * Allocate a new entry, or balk if not possible.
381 		 */
382 		hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
383 		if (hc_entry == NULL) {
384 			THC_UNLOCK(&hc_head->hch_mtx);
385 			return NULL;
386 		}
387 	}
388 
389 	/*
390 	 * Initialize basic information of hostcache entry.
391 	 */
392 	bzero(hc_entry, sizeof(*hc_entry));
393 	if (inc->inc_isipv6)
394 		bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6));
395 	else
396 		hc_entry->ip4 = inc->inc_faddr;
397 	hc_entry->rmx_head = hc_head;
398 	hc_entry->rmx_expire = V_tcp_hostcache.expire;
399 
400 	/*
401 	 * Put it upfront.
402 	 */
403 	TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
404 	V_tcp_hostcache.hashbase[hash].hch_length++;
405 	V_tcp_hostcache.cache_count++;
406 	V_tcpstat.tcps_hc_added++;
407 
408 	return hc_entry;
409 }
410 
411 /*
412  * External function: look up an entry in the hostcache and fill out the
413  * supplied TCP metrics structure.  Fills in NULL when no entry was found or
414  * a value is not set.
415  */
416 void
417 tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
418 {
419 	struct hc_metrics *hc_entry;
420 
421 	/*
422 	 * Find the right bucket.
423 	 */
424 	hc_entry = tcp_hc_lookup(inc);
425 
426 	/*
427 	 * If we don't have an existing object.
428 	 */
429 	if (hc_entry == NULL) {
430 		bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
431 		return;
432 	}
433 	hc_entry->rmx_hits++;
434 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
435 
436 	hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
437 	hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
438 	hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
439 	hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
440 	hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth;
441 	hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
442 	hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
443 	hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
444 
445 	/*
446 	 * Unlock bucket row.
447 	 */
448 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
449 }
450 
451 /*
452  * External function: look up an entry in the hostcache and return the
453  * discovered path MTU.  Returns NULL if no entry is found or value is not
454  * set.
455  */
456 u_long
457 tcp_hc_getmtu(struct in_conninfo *inc)
458 {
459 	struct hc_metrics *hc_entry;
460 	u_long mtu;
461 
462 	hc_entry = tcp_hc_lookup(inc);
463 	if (hc_entry == NULL) {
464 		return 0;
465 	}
466 	hc_entry->rmx_hits++;
467 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
468 
469 	mtu = hc_entry->rmx_mtu;
470 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
471 	return mtu;
472 }
473 
474 /*
475  * External function: update the MTU value of an entry in the hostcache.
476  * Creates a new entry if none was found.
477  */
478 void
479 tcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
480 {
481 	struct hc_metrics *hc_entry;
482 
483 	/*
484 	 * Find the right bucket.
485 	 */
486 	hc_entry = tcp_hc_lookup(inc);
487 
488 	/*
489 	 * If we don't have an existing object, try to insert a new one.
490 	 */
491 	if (hc_entry == NULL) {
492 		hc_entry = tcp_hc_insert(inc);
493 		if (hc_entry == NULL)
494 			return;
495 	}
496 	hc_entry->rmx_updates++;
497 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
498 
499 	hc_entry->rmx_mtu = mtu;
500 
501 	/*
502 	 * Put it upfront so we find it faster next time.
503 	 */
504 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
505 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
506 
507 	/*
508 	 * Unlock bucket row.
509 	 */
510 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
511 }
512 
513 /*
514  * External function: update the TCP metrics of an entry in the hostcache.
515  * Creates a new entry if none was found.
516  */
517 void
518 tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
519 {
520 	struct hc_metrics *hc_entry;
521 
522 	hc_entry = tcp_hc_lookup(inc);
523 	if (hc_entry == NULL) {
524 		hc_entry = tcp_hc_insert(inc);
525 		if (hc_entry == NULL)
526 			return;
527 	}
528 	hc_entry->rmx_updates++;
529 	hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
530 
531 	if (hcml->rmx_rtt != 0) {
532 		if (hc_entry->rmx_rtt == 0)
533 			hc_entry->rmx_rtt = hcml->rmx_rtt;
534 		else
535 			hc_entry->rmx_rtt =
536 			    (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
537 		V_tcpstat.tcps_cachedrtt++;
538 	}
539 	if (hcml->rmx_rttvar != 0) {
540 	        if (hc_entry->rmx_rttvar == 0)
541 			hc_entry->rmx_rttvar = hcml->rmx_rttvar;
542 		else
543 			hc_entry->rmx_rttvar =
544 			    (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
545 		V_tcpstat.tcps_cachedrttvar++;
546 	}
547 	if (hcml->rmx_ssthresh != 0) {
548 		if (hc_entry->rmx_ssthresh == 0)
549 			hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
550 		else
551 			hc_entry->rmx_ssthresh =
552 			    (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
553 		V_tcpstat.tcps_cachedssthresh++;
554 	}
555 	if (hcml->rmx_bandwidth != 0) {
556 		if (hc_entry->rmx_bandwidth == 0)
557 			hc_entry->rmx_bandwidth = hcml->rmx_bandwidth;
558 		else
559 			hc_entry->rmx_bandwidth =
560 			    (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2;
561 		/* V_tcpstat.tcps_cachedbandwidth++; */
562 	}
563 	if (hcml->rmx_cwnd != 0) {
564 		if (hc_entry->rmx_cwnd == 0)
565 			hc_entry->rmx_cwnd = hcml->rmx_cwnd;
566 		else
567 			hc_entry->rmx_cwnd =
568 			    (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
569 		/* V_tcpstat.tcps_cachedcwnd++; */
570 	}
571 	if (hcml->rmx_sendpipe != 0) {
572 		if (hc_entry->rmx_sendpipe == 0)
573 			hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
574 		else
575 			hc_entry->rmx_sendpipe =
576 			    (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
577 		/* V_tcpstat.tcps_cachedsendpipe++; */
578 	}
579 	if (hcml->rmx_recvpipe != 0) {
580 		if (hc_entry->rmx_recvpipe == 0)
581 			hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
582 		else
583 			hc_entry->rmx_recvpipe =
584 			    (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
585 		/* V_tcpstat.tcps_cachedrecvpipe++; */
586 	}
587 
588 	TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
589 	TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
590 	THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
591 }
592 
593 /*
594  * Sysctl function: prints the list and values of all hostcache entries in
595  * unsorted order.
596  */
597 static int
598 sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
599 {
600 	int bufsize;
601 	int linesize = 128;
602 	char *p, *buf;
603 	int len, i, error;
604 	struct hc_metrics *hc_entry;
605 #ifdef INET6
606 	char ip6buf[INET6_ADDRSTRLEN];
607 #endif
608 
609 	bufsize = linesize * (V_tcp_hostcache.cache_count + 1);
610 
611 	p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
612 
613 	len = snprintf(p, linesize,
614 		"\nIP address        MTU  SSTRESH      RTT   RTTVAR BANDWIDTH "
615 		"    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
616 	p += len;
617 
618 #define msec(u) (((u) + 500) / 1000)
619 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
620 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
621 		TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
622 			      rmx_q) {
623 			len = snprintf(p, linesize,
624 			    "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu "
625 			    "%4lu %4lu %4i\n",
626 			    hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
627 #ifdef INET6
628 				ip6_sprintf(ip6buf, &hc_entry->ip6),
629 #else
630 				"IPv6?",
631 #endif
632 			    hc_entry->rmx_mtu,
633 			    hc_entry->rmx_ssthresh,
634 			    msec(hc_entry->rmx_rtt *
635 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
636 			    msec(hc_entry->rmx_rttvar *
637 				(RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
638 			    hc_entry->rmx_bandwidth * 8,
639 			    hc_entry->rmx_cwnd,
640 			    hc_entry->rmx_sendpipe,
641 			    hc_entry->rmx_recvpipe,
642 			    hc_entry->rmx_hits,
643 			    hc_entry->rmx_updates,
644 			    hc_entry->rmx_expire);
645 			p += len;
646 		}
647 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
648 	}
649 #undef msec
650 	error = SYSCTL_OUT(req, buf, p - buf);
651 	free(buf, M_TEMP);
652 	return(error);
653 }
654 
655 /*
656  * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
657  * periodically from the callout.
658  */
659 static void
660 tcp_hc_purge(void *arg)
661 {
662 	struct hc_metrics *hc_entry, *hc_next;
663 	int all = (intptr_t)arg;
664 	int i;
665 
666 	if (V_tcp_hostcache.purgeall) {
667 		all = 1;
668 		V_tcp_hostcache.purgeall = 0;
669 	}
670 
671 	for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
672 		THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
673 		TAILQ_FOREACH_SAFE(hc_entry,
674 		    &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
675 			if (all || hc_entry->rmx_expire <= 0) {
676 				TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
677 					      hc_entry, rmx_q);
678 				uma_zfree(V_tcp_hostcache.zone, hc_entry);
679 				V_tcp_hostcache.hashbase[i].hch_length--;
680 				V_tcp_hostcache.cache_count--;
681 			} else
682 				hc_entry->rmx_expire -= V_tcp_hostcache.prune;
683 		}
684 		THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
685 	}
686 
687 	callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
688 	    tcp_hc_purge, arg);
689 }
690