2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * The tcp_hostcache moves the tcp-specific cached metrics from the routing
34 * table to a dedicated structure indexed by the remote IP address. It keeps
35 * information on the measured TCP parameters of past TCP sessions to allow
36 * better initial start values to be used with later connections to/from the
37 * same source. Depending on the network parameters (delay, max MTU,
38 * congestion window) between local and remote sites, this can lead to
39 * significant speed-ups for new TCP connections after the first one.
41 * Due to the tcp_hostcache, all TCP-specific metrics information in the
42 * routing table have been removed. The inpcb no longer keeps a pointer to
43 * the routing entry, and protocol-initiated route cloning has been removed
44 * as well. With these changes, the routing table has gone back to being
45 * more lightwight and only carries information related to packet forwarding.
47 * tcp_hostcache is designed for multiple concurrent access in SMP
48 * environments and high contention. All bucket rows have their own lock and
49 * thus multiple lookups and modifies can be done at the same time as long as
50 * they are in different bucket rows. If a request for insertion of a new
51 * record can't be satisfied, it simply returns an empty structure. Nobody
52 * and nothing outside of tcp_hostcache.c will ever point directly to any
53 * entry in the tcp_hostcache. All communication is done in an
54 * object-oriented way and only functions of tcp_hostcache will manipulate
55 * hostcache entries. Otherwise, we are unable to achieve good behaviour in
56 * concurrent access situations. Since tcp_hostcache is only caching
57 * information, there are no fatal consequences if we either can't satisfy
58 * any particular request or have to drop/overwrite an existing entry because
59 * of bucket limit memory constrains.
63 * Many thanks to jlemon for basic structure of tcp_syncache which is being
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
70 #include "opt_inet6.h"
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/kernel.h>
77 #include <sys/mutex.h>
78 #include <sys/malloc.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/sysctl.h>
86 #include <net/if_var.h>
87 #include <net/route.h>
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_var.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/ip_var.h>
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
100 #include <netinet/tcp.h>
101 #include <netinet/tcp_var.h>
102 #include <netinet/tcp_hostcache.h>
104 #include <netinet6/tcp6_var.h>
109 /* Arbitrary values */
110 #define TCP_HOSTCACHE_HASHSIZE 512
111 #define TCP_HOSTCACHE_BUCKETLIMIT 30
112 #define TCP_HOSTCACHE_EXPIRE 60*60 /* one hour */
113 #define TCP_HOSTCACHE_PRUNE 5*60 /* every 5 minutes */
115 VNET_DEFINE_STATIC(struct tcp_hostcache, tcp_hostcache);
116 #define V_tcp_hostcache VNET(tcp_hostcache)
118 VNET_DEFINE_STATIC(struct callout, tcp_hc_callout);
119 #define V_tcp_hc_callout VNET(tcp_hc_callout)
121 static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
122 static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
123 static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
124 static int sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS);
125 static void tcp_hc_purge_internal(int);
126 static void tcp_hc_purge(void *);
128 static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0,
131 VNET_DEFINE(int, tcp_use_hostcache) = 1;
132 #define V_tcp_use_hostcache VNET(tcp_use_hostcache)
133 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
134 &VNET_NAME(tcp_use_hostcache), 0,
135 "Enable the TCP hostcache");
137 SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN,
138 &VNET_NAME(tcp_hostcache.cache_limit), 0,
139 "Overall entry limit for hostcache");
141 SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN,
142 &VNET_NAME(tcp_hostcache.hashsize), 0,
143 "Size of TCP hostcache hashtable");
145 SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
146 CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0,
147 "Per-bucket hash limit for hostcache");
149 SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD,
150 &VNET_NAME(tcp_hostcache.cache_count), 0,
151 "Current number of entries in hostcache");
153 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW,
154 &VNET_NAME(tcp_hostcache.expire), 0,
155 "Expire time of TCP hostcache entries");
157 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW,
158 &VNET_NAME(tcp_hostcache.prune), 0,
159 "Time between purge runs");
161 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW,
162 &VNET_NAME(tcp_hostcache.purgeall), 0,
163 "Expire all entires on next purge run");
165 SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
166 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
167 sysctl_tcp_hc_list, "A", "List of all hostcache entries");
169 SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, purgenow,
170 CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
171 sysctl_tcp_hc_purgenow, "I", "Immediately purge all entries");
173 static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
175 #define HOSTCACHE_HASH(ip) \
176 (((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) & \
177 V_tcp_hostcache.hashmask)
179 /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
180 #define HOSTCACHE_HASH6(ip6) \
181 (((ip6)->s6_addr32[0] ^ \
182 (ip6)->s6_addr32[1] ^ \
183 (ip6)->s6_addr32[2] ^ \
184 (ip6)->s6_addr32[3]) & \
185 V_tcp_hostcache.hashmask)
187 #define THC_LOCK(lp) mtx_lock(lp)
188 #define THC_UNLOCK(lp) mtx_unlock(lp)
197 * Initialize hostcache structures.
199 V_tcp_hostcache.cache_count = 0;
200 V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
201 V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
202 V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
203 V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
205 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
206 &V_tcp_hostcache.hashsize);
207 if (!powerof2(V_tcp_hostcache.hashsize)) {
208 printf("WARNING: hostcache hash size is not a power of 2.\n");
209 V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
211 V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
213 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
214 &V_tcp_hostcache.bucket_limit);
216 cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
217 V_tcp_hostcache.cache_limit = cache_limit;
218 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
219 &V_tcp_hostcache.cache_limit);
220 if (V_tcp_hostcache.cache_limit > cache_limit)
221 V_tcp_hostcache.cache_limit = cache_limit;
224 * Allocate the hash table.
226 V_tcp_hostcache.hashbase = (struct hc_head *)
227 malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
228 M_HOSTCACHE, M_WAITOK | M_ZERO);
231 * Initialize the hash buckets.
233 for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
234 TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
235 V_tcp_hostcache.hashbase[i].hch_length = 0;
236 mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
241 * Allocate the hostcache entries.
243 V_tcp_hostcache.zone =
244 uma_zcreate("hostcache", sizeof(struct hc_metrics),
245 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
246 uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
249 * Set up periodic cache cleanup.
251 callout_init(&V_tcp_hc_callout, 1);
252 callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
253 tcp_hc_purge, curvnet);
262 callout_drain(&V_tcp_hc_callout);
264 /* Purge all hc entries. */
265 tcp_hc_purge_internal(1);
267 /* Free the uma zone and the allocated hash table. */
268 uma_zdestroy(V_tcp_hostcache.zone);
270 for (i = 0; i < V_tcp_hostcache.hashsize; i++)
271 mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx);
272 free(V_tcp_hostcache.hashbase, M_HOSTCACHE);
277 * Internal function: look up an entry in the hostcache or return NULL.
279 * If an entry has been returned, the caller becomes responsible for
280 * unlocking the bucket row after he is done reading/modifying the entry.
282 static struct hc_metrics *
283 tcp_hc_lookup(struct in_conninfo *inc)
286 struct hc_head *hc_head;
287 struct hc_metrics *hc_entry;
289 if (!V_tcp_use_hostcache)
292 KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
295 * Hash the foreign ip address.
297 if (inc->inc_flags & INC_ISIPV6)
298 hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
300 hash = HOSTCACHE_HASH(&inc->inc_faddr);
302 hc_head = &V_tcp_hostcache.hashbase[hash];
305 * Acquire lock for this bucket row; we release the lock if we don't
306 * find an entry, otherwise the caller has to unlock after he is
309 THC_LOCK(&hc_head->hch_mtx);
312 * Iterate through entries in bucket row looking for a match.
314 TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
315 if (inc->inc_flags & INC_ISIPV6) {
316 /* XXX: check ip6_zoneid */
317 if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
318 sizeof(inc->inc6_faddr)) == 0)
321 if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
322 sizeof(inc->inc_faddr)) == 0)
328 * We were unsuccessful and didn't find anything.
330 THC_UNLOCK(&hc_head->hch_mtx);
335 * Internal function: insert an entry into the hostcache or return NULL if
336 * unable to allocate a new one.
338 * If an entry has been returned, the caller becomes responsible for
339 * unlocking the bucket row after he is done reading/modifying the entry.
341 static struct hc_metrics *
342 tcp_hc_insert(struct in_conninfo *inc)
345 struct hc_head *hc_head;
346 struct hc_metrics *hc_entry;
348 if (!V_tcp_use_hostcache)
351 KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
354 * Hash the foreign ip address.
356 if (inc->inc_flags & INC_ISIPV6)
357 hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
359 hash = HOSTCACHE_HASH(&inc->inc_faddr);
361 hc_head = &V_tcp_hostcache.hashbase[hash];
364 * Acquire lock for this bucket row; we release the lock if we don't
365 * find an entry, otherwise the caller has to unlock after he is
368 THC_LOCK(&hc_head->hch_mtx);
371 * If the bucket limit is reached, reuse the least-used element.
373 if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
374 V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) {
375 hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
377 * At first we were dropping the last element, just to
378 * reacquire it in the next two lines again, which isn't very
379 * efficient. Instead just reuse the least used element.
380 * We may drop something that is still "in-use" but we can be
382 * Just give up if this bucket row is empty and we don't have
383 * anything to replace.
385 if (hc_entry == NULL) {
386 THC_UNLOCK(&hc_head->hch_mtx);
389 TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
390 V_tcp_hostcache.hashbase[hash].hch_length--;
391 V_tcp_hostcache.cache_count--;
392 TCPSTAT_INC(tcps_hc_bucketoverflow);
394 uma_zfree(V_tcp_hostcache.zone, hc_entry);
398 * Allocate a new entry, or balk if not possible.
400 hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
401 if (hc_entry == NULL) {
402 THC_UNLOCK(&hc_head->hch_mtx);
408 * Initialize basic information of hostcache entry.
410 bzero(hc_entry, sizeof(*hc_entry));
411 if (inc->inc_flags & INC_ISIPV6) {
412 hc_entry->ip6 = inc->inc6_faddr;
413 hc_entry->ip6_zoneid = inc->inc6_zoneid;
415 hc_entry->ip4 = inc->inc_faddr;
416 hc_entry->rmx_head = hc_head;
417 hc_entry->rmx_expire = V_tcp_hostcache.expire;
422 TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
423 V_tcp_hostcache.hashbase[hash].hch_length++;
424 V_tcp_hostcache.cache_count++;
425 TCPSTAT_INC(tcps_hc_added);
431 * External function: look up an entry in the hostcache and fill out the
432 * supplied TCP metrics structure. Fills in NULL when no entry was found or
433 * a value is not set.
436 tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
438 struct hc_metrics *hc_entry;
440 if (!V_tcp_use_hostcache)
444 * Find the right bucket.
446 hc_entry = tcp_hc_lookup(inc);
449 * If we don't have an existing object.
451 if (hc_entry == NULL) {
452 bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
455 hc_entry->rmx_hits++;
456 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
458 hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
459 hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
460 hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
461 hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
462 hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
463 hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
464 hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
469 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
473 * External function: look up an entry in the hostcache and return the
474 * discovered path MTU. Returns 0 if no entry is found or value is not
478 tcp_hc_getmtu(struct in_conninfo *inc)
480 struct hc_metrics *hc_entry;
483 if (!V_tcp_use_hostcache)
486 hc_entry = tcp_hc_lookup(inc);
487 if (hc_entry == NULL) {
490 hc_entry->rmx_hits++;
491 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
493 mtu = hc_entry->rmx_mtu;
494 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
499 * External function: update the MTU value of an entry in the hostcache.
500 * Creates a new entry if none was found.
503 tcp_hc_updatemtu(struct in_conninfo *inc, uint32_t mtu)
505 struct hc_metrics *hc_entry;
507 if (!V_tcp_use_hostcache)
511 * Find the right bucket.
513 hc_entry = tcp_hc_lookup(inc);
516 * If we don't have an existing object, try to insert a new one.
518 if (hc_entry == NULL) {
519 hc_entry = tcp_hc_insert(inc);
520 if (hc_entry == NULL)
523 hc_entry->rmx_updates++;
524 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
526 hc_entry->rmx_mtu = mtu;
529 * Put it upfront so we find it faster next time.
531 TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
532 TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
537 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
541 * External function: update the TCP metrics of an entry in the hostcache.
542 * Creates a new entry if none was found.
545 tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
547 struct hc_metrics *hc_entry;
549 if (!V_tcp_use_hostcache)
552 hc_entry = tcp_hc_lookup(inc);
553 if (hc_entry == NULL) {
554 hc_entry = tcp_hc_insert(inc);
555 if (hc_entry == NULL)
558 hc_entry->rmx_updates++;
559 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
561 if (hcml->rmx_rtt != 0) {
562 if (hc_entry->rmx_rtt == 0)
563 hc_entry->rmx_rtt = hcml->rmx_rtt;
565 hc_entry->rmx_rtt = ((uint64_t)hc_entry->rmx_rtt +
566 (uint64_t)hcml->rmx_rtt) / 2;
567 TCPSTAT_INC(tcps_cachedrtt);
569 if (hcml->rmx_rttvar != 0) {
570 if (hc_entry->rmx_rttvar == 0)
571 hc_entry->rmx_rttvar = hcml->rmx_rttvar;
573 hc_entry->rmx_rttvar = ((uint64_t)hc_entry->rmx_rttvar +
574 (uint64_t)hcml->rmx_rttvar) / 2;
575 TCPSTAT_INC(tcps_cachedrttvar);
577 if (hcml->rmx_ssthresh != 0) {
578 if (hc_entry->rmx_ssthresh == 0)
579 hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
581 hc_entry->rmx_ssthresh =
582 (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
583 TCPSTAT_INC(tcps_cachedssthresh);
585 if (hcml->rmx_cwnd != 0) {
586 if (hc_entry->rmx_cwnd == 0)
587 hc_entry->rmx_cwnd = hcml->rmx_cwnd;
589 hc_entry->rmx_cwnd = ((uint64_t)hc_entry->rmx_cwnd +
590 (uint64_t)hcml->rmx_cwnd) / 2;
591 /* TCPSTAT_INC(tcps_cachedcwnd); */
593 if (hcml->rmx_sendpipe != 0) {
594 if (hc_entry->rmx_sendpipe == 0)
595 hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
597 hc_entry->rmx_sendpipe =
598 ((uint64_t)hc_entry->rmx_sendpipe +
599 (uint64_t)hcml->rmx_sendpipe) /2;
600 /* TCPSTAT_INC(tcps_cachedsendpipe); */
602 if (hcml->rmx_recvpipe != 0) {
603 if (hc_entry->rmx_recvpipe == 0)
604 hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
606 hc_entry->rmx_recvpipe =
607 ((uint64_t)hc_entry->rmx_recvpipe +
608 (uint64_t)hcml->rmx_recvpipe) /2;
609 /* TCPSTAT_INC(tcps_cachedrecvpipe); */
612 TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
613 TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
614 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
618 * Sysctl function: prints the list and values of all hostcache entries in
622 sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
624 const int linesize = 128;
627 struct hc_metrics *hc_entry;
628 char ip4buf[INET_ADDRSTRLEN];
630 char ip6buf[INET6_ADDRSTRLEN];
633 if (jailed_without_vnet(curthread->td_ucred) != 0)
636 sbuf_new(&sb, NULL, linesize * (V_tcp_hostcache.cache_count + 1),
640 "\nIP address MTU SSTRESH RTT RTTVAR "
641 " CWND SENDPIPE RECVPIPE HITS UPD EXP\n");
643 #define msec(u) (((u) + 500) / 1000)
644 for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
645 THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
646 TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
649 "%-15s %5u %8u %6lums %6lums %8u %8u %8u %4lu "
651 hc_entry->ip4.s_addr ?
652 inet_ntoa_r(hc_entry->ip4, ip4buf) :
654 ip6_sprintf(ip6buf, &hc_entry->ip6),
659 hc_entry->rmx_ssthresh,
660 msec((u_long)hc_entry->rmx_rtt *
661 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
662 msec((u_long)hc_entry->rmx_rttvar *
663 (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))),
665 hc_entry->rmx_sendpipe,
666 hc_entry->rmx_recvpipe,
668 hc_entry->rmx_updates,
669 hc_entry->rmx_expire);
671 THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
674 error = sbuf_finish(&sb);
676 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
682 * Caller has to make sure the curvnet is set properly.
685 tcp_hc_purge_internal(int all)
687 struct hc_metrics *hc_entry, *hc_next;
690 for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
691 THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
692 TAILQ_FOREACH_SAFE(hc_entry,
693 &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
694 if (all || hc_entry->rmx_expire <= 0) {
695 TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
697 uma_zfree(V_tcp_hostcache.zone, hc_entry);
698 V_tcp_hostcache.hashbase[i].hch_length--;
699 V_tcp_hostcache.cache_count--;
701 hc_entry->rmx_expire -= V_tcp_hostcache.prune;
703 THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
708 * Expire and purge (old|all) entries in the tcp_hostcache. Runs
709 * periodically from the callout.
712 tcp_hc_purge(void *arg)
714 CURVNET_SET((struct vnet *) arg);
717 if (V_tcp_hostcache.purgeall) {
719 V_tcp_hostcache.purgeall = 0;
722 tcp_hc_purge_internal(all);
724 callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
730 * Expire and purge all entries in hostcache immediately.
733 sysctl_tcp_hc_purgenow(SYSCTL_HANDLER_ARGS)
738 error = sysctl_handle_int(oidp, &val, 0, req);
739 if (error || !req->newptr)
742 tcp_hc_purge_internal(1);
744 callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
745 tcp_hc_purge, curvnet);