2 * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
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.
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.
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.
61 * Many thanks to jlemon for basic structure of tcp_syncache which is being
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
68 #include "opt_inet6.h"
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
74 #include <sys/mutex.h>
75 #include <sys/malloc.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/sysctl.h>
82 #include <net/if_var.h>
83 #include <net/route.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/in_var.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/ip_var.h>
93 #include <netinet/ip6.h>
94 #include <netinet6/ip6_var.h>
96 #include <netinet/tcp.h>
97 #include <netinet/tcp_var.h>
98 #include <netinet/tcp_hostcache.h>
100 #include <netinet6/tcp6_var.h>
105 /* Arbitrary values */
106 #define TCP_HOSTCACHE_HASHSIZE 512
107 #define TCP_HOSTCACHE_BUCKETLIMIT 30
108 #define TCP_HOSTCACHE_EXPIRE 60*60 /* one hour */
109 #define TCP_HOSTCACHE_PRUNE 5*60 /* every 5 minutes */
111 static VNET_DEFINE(struct tcp_hostcache, tcp_hostcache);
112 #define V_tcp_hostcache VNET(tcp_hostcache)
114 static VNET_DEFINE(struct callout, tcp_hc_callout);
115 #define V_tcp_hc_callout VNET(tcp_hc_callout)
117 static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
118 static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
119 static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
120 static void tcp_hc_purge_internal(int);
121 static void tcp_hc_purge(void *);
123 static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0,
126 SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN,
127 &VNET_NAME(tcp_hostcache.cache_limit), 0,
128 "Overall entry limit for hostcache");
130 SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN,
131 &VNET_NAME(tcp_hostcache.hashsize), 0,
132 "Size of TCP hostcache hashtable");
134 SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
135 CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0,
136 "Per-bucket hash limit for hostcache");
138 SYSCTL_UINT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET | CTLFLAG_RD,
139 &VNET_NAME(tcp_hostcache.cache_count), 0,
140 "Current number of entries in hostcache");
142 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET | CTLFLAG_RW,
143 &VNET_NAME(tcp_hostcache.expire), 0,
144 "Expire time of TCP hostcache entries");
146 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET | CTLFLAG_RW,
147 &VNET_NAME(tcp_hostcache.prune), 0,
148 "Time between purge runs");
150 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET | CTLFLAG_RW,
151 &VNET_NAME(tcp_hostcache.purgeall), 0,
152 "Expire all entires on next purge run");
154 SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
155 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
156 sysctl_tcp_hc_list, "A", "List of all hostcache entries");
159 static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
161 #define HOSTCACHE_HASH(ip) \
162 (((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) & \
163 V_tcp_hostcache.hashmask)
165 /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
166 #define HOSTCACHE_HASH6(ip6) \
167 (((ip6)->s6_addr32[0] ^ \
168 (ip6)->s6_addr32[1] ^ \
169 (ip6)->s6_addr32[2] ^ \
170 (ip6)->s6_addr32[3]) & \
171 V_tcp_hostcache.hashmask)
173 #define THC_LOCK(lp) mtx_lock(lp)
174 #define THC_UNLOCK(lp) mtx_unlock(lp)
183 * Initialize hostcache structures.
185 V_tcp_hostcache.cache_count = 0;
186 V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
187 V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
188 V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
189 V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
191 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
192 &V_tcp_hostcache.hashsize);
193 if (!powerof2(V_tcp_hostcache.hashsize)) {
194 printf("WARNING: hostcache hash size is not a power of 2.\n");
195 V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
197 V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
199 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
200 &V_tcp_hostcache.bucket_limit);
202 cache_limit = V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
203 V_tcp_hostcache.cache_limit = cache_limit;
204 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
205 &V_tcp_hostcache.cache_limit);
206 if (V_tcp_hostcache.cache_limit > cache_limit)
207 V_tcp_hostcache.cache_limit = cache_limit;
210 * Allocate the hash table.
212 V_tcp_hostcache.hashbase = (struct hc_head *)
213 malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
214 M_HOSTCACHE, M_WAITOK | M_ZERO);
217 * Initialize the hash buckets.
219 for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
220 TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
221 V_tcp_hostcache.hashbase[i].hch_length = 0;
222 mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
227 * Allocate the hostcache entries.
229 V_tcp_hostcache.zone =
230 uma_zcreate("hostcache", sizeof(struct hc_metrics),
231 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
232 uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
235 * Set up periodic cache cleanup.
237 callout_init(&V_tcp_hc_callout, CALLOUT_MPSAFE);
238 callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
239 tcp_hc_purge, curvnet);
248 callout_drain(&V_tcp_hc_callout);
250 /* Purge all hc entries. */
251 tcp_hc_purge_internal(1);
253 /* Free the uma zone and the allocated hash table. */
254 uma_zdestroy(V_tcp_hostcache.zone);
256 for (i = 0; i < V_tcp_hostcache.hashsize; i++)
257 mtx_destroy(&V_tcp_hostcache.hashbase[i].hch_mtx);
258 free(V_tcp_hostcache.hashbase, M_HOSTCACHE);
263 * Internal function: look up an entry in the hostcache or return NULL.
265 * If an entry has been returned, the caller becomes responsible for
266 * unlocking the bucket row after he is done reading/modifying the entry.
268 static struct hc_metrics *
269 tcp_hc_lookup(struct in_conninfo *inc)
272 struct hc_head *hc_head;
273 struct hc_metrics *hc_entry;
275 KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
278 * Hash the foreign ip address.
280 if (inc->inc_flags & INC_ISIPV6)
281 hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
283 hash = HOSTCACHE_HASH(&inc->inc_faddr);
285 hc_head = &V_tcp_hostcache.hashbase[hash];
288 * Acquire lock for this bucket row; we release the lock if we don't
289 * find an entry, otherwise the caller has to unlock after he is
292 THC_LOCK(&hc_head->hch_mtx);
295 * Iterate through entries in bucket row looking for a match.
297 TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
298 if (inc->inc_flags & INC_ISIPV6) {
299 /* XXX: check ip6_zoneid */
300 if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
301 sizeof(inc->inc6_faddr)) == 0)
304 if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
305 sizeof(inc->inc_faddr)) == 0)
311 * We were unsuccessful and didn't find anything.
313 THC_UNLOCK(&hc_head->hch_mtx);
318 * Internal function: insert an entry into the hostcache or return NULL if
319 * unable to allocate a new one.
321 * If an entry has been returned, the caller becomes responsible for
322 * unlocking the bucket row after he is done reading/modifying the entry.
324 static struct hc_metrics *
325 tcp_hc_insert(struct in_conninfo *inc)
328 struct hc_head *hc_head;
329 struct hc_metrics *hc_entry;
331 KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
334 * Hash the foreign ip address.
336 if (inc->inc_flags & INC_ISIPV6)
337 hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
339 hash = HOSTCACHE_HASH(&inc->inc_faddr);
341 hc_head = &V_tcp_hostcache.hashbase[hash];
344 * Acquire lock for this bucket row; we release the lock if we don't
345 * find an entry, otherwise the caller has to unlock after he is
348 THC_LOCK(&hc_head->hch_mtx);
351 * If the bucket limit is reached, reuse the least-used element.
353 if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
354 V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) {
355 hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
357 * At first we were dropping the last element, just to
358 * reacquire it in the next two lines again, which isn't very
359 * efficient. Instead just reuse the least used element.
360 * We may drop something that is still "in-use" but we can be
362 * Just give up if this bucket row is empty and we don't have
363 * anything to replace.
365 if (hc_entry == NULL) {
366 THC_UNLOCK(&hc_head->hch_mtx);
369 TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
370 V_tcp_hostcache.hashbase[hash].hch_length--;
371 V_tcp_hostcache.cache_count--;
372 TCPSTAT_INC(tcps_hc_bucketoverflow);
374 uma_zfree(V_tcp_hostcache.zone, hc_entry);
378 * Allocate a new entry, or balk if not possible.
380 hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
381 if (hc_entry == NULL) {
382 THC_UNLOCK(&hc_head->hch_mtx);
388 * Initialize basic information of hostcache entry.
390 bzero(hc_entry, sizeof(*hc_entry));
391 if (inc->inc_flags & INC_ISIPV6) {
392 hc_entry->ip6 = inc->inc6_faddr;
393 hc_entry->ip6_zoneid = inc->inc6_zoneid;
395 hc_entry->ip4 = inc->inc_faddr;
396 hc_entry->rmx_head = hc_head;
397 hc_entry->rmx_expire = V_tcp_hostcache.expire;
402 TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
403 V_tcp_hostcache.hashbase[hash].hch_length++;
404 V_tcp_hostcache.cache_count++;
405 TCPSTAT_INC(tcps_hc_added);
411 * External function: look up an entry in the hostcache and fill out the
412 * supplied TCP metrics structure. Fills in NULL when no entry was found or
413 * a value is not set.
416 tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
418 struct hc_metrics *hc_entry;
421 * Find the right bucket.
423 hc_entry = tcp_hc_lookup(inc);
426 * If we don't have an existing object.
428 if (hc_entry == NULL) {
429 bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
432 hc_entry->rmx_hits++;
433 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
435 hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
436 hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
437 hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
438 hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
439 hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth;
440 hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
441 hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
442 hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
447 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
451 * External function: look up an entry in the hostcache and return the
452 * discovered path MTU. Returns NULL if no entry is found or value is not
456 tcp_hc_getmtu(struct in_conninfo *inc)
458 struct hc_metrics *hc_entry;
461 hc_entry = tcp_hc_lookup(inc);
462 if (hc_entry == NULL) {
465 hc_entry->rmx_hits++;
466 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
468 mtu = hc_entry->rmx_mtu;
469 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
474 * External function: update the MTU value of an entry in the hostcache.
475 * Creates a new entry if none was found.
478 tcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
480 struct hc_metrics *hc_entry;
483 * Find the right bucket.
485 hc_entry = tcp_hc_lookup(inc);
488 * If we don't have an existing object, try to insert a new one.
490 if (hc_entry == NULL) {
491 hc_entry = tcp_hc_insert(inc);
492 if (hc_entry == NULL)
495 hc_entry->rmx_updates++;
496 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
498 hc_entry->rmx_mtu = mtu;
501 * Put it upfront so we find it faster next time.
503 TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
504 TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
509 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
513 * External function: update the TCP metrics of an entry in the hostcache.
514 * Creates a new entry if none was found.
517 tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
519 struct hc_metrics *hc_entry;
521 hc_entry = tcp_hc_lookup(inc);
522 if (hc_entry == NULL) {
523 hc_entry = tcp_hc_insert(inc);
524 if (hc_entry == NULL)
527 hc_entry->rmx_updates++;
528 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
530 if (hcml->rmx_rtt != 0) {
531 if (hc_entry->rmx_rtt == 0)
532 hc_entry->rmx_rtt = hcml->rmx_rtt;
535 (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
536 TCPSTAT_INC(tcps_cachedrtt);
538 if (hcml->rmx_rttvar != 0) {
539 if (hc_entry->rmx_rttvar == 0)
540 hc_entry->rmx_rttvar = hcml->rmx_rttvar;
542 hc_entry->rmx_rttvar =
543 (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
544 TCPSTAT_INC(tcps_cachedrttvar);
546 if (hcml->rmx_ssthresh != 0) {
547 if (hc_entry->rmx_ssthresh == 0)
548 hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
550 hc_entry->rmx_ssthresh =
551 (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
552 TCPSTAT_INC(tcps_cachedssthresh);
554 if (hcml->rmx_bandwidth != 0) {
555 if (hc_entry->rmx_bandwidth == 0)
556 hc_entry->rmx_bandwidth = hcml->rmx_bandwidth;
558 hc_entry->rmx_bandwidth =
559 (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2;
560 /* TCPSTAT_INC(tcps_cachedbandwidth); */
562 if (hcml->rmx_cwnd != 0) {
563 if (hc_entry->rmx_cwnd == 0)
564 hc_entry->rmx_cwnd = hcml->rmx_cwnd;
567 (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
568 /* TCPSTAT_INC(tcps_cachedcwnd); */
570 if (hcml->rmx_sendpipe != 0) {
571 if (hc_entry->rmx_sendpipe == 0)
572 hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
574 hc_entry->rmx_sendpipe =
575 (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
576 /* TCPSTAT_INC(tcps_cachedsendpipe); */
578 if (hcml->rmx_recvpipe != 0) {
579 if (hc_entry->rmx_recvpipe == 0)
580 hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
582 hc_entry->rmx_recvpipe =
583 (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
584 /* TCPSTAT_INC(tcps_cachedrecvpipe); */
587 TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
588 TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
589 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
593 * Sysctl function: prints the list and values of all hostcache entries in
597 sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
602 struct hc_metrics *hc_entry;
604 char ip6buf[INET6_ADDRSTRLEN];
607 sbuf_new(&sb, NULL, linesize * (V_tcp_hostcache.cache_count + 1),
611 "\nIP address MTU SSTRESH RTT RTTVAR BANDWIDTH "
612 " CWND SENDPIPE RECVPIPE HITS UPD EXP\n");
614 #define msec(u) (((u) + 500) / 1000)
615 for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
616 THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
617 TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
620 "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu "
622 hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
624 ip6_sprintf(ip6buf, &hc_entry->ip6),
629 hc_entry->rmx_ssthresh,
630 msec(hc_entry->rmx_rtt *
631 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
632 msec(hc_entry->rmx_rttvar *
633 (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE))),
634 hc_entry->rmx_bandwidth * 8,
636 hc_entry->rmx_sendpipe,
637 hc_entry->rmx_recvpipe,
639 hc_entry->rmx_updates,
640 hc_entry->rmx_expire);
642 THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
646 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
652 * Caller has to make sure the curvnet is set properly.
655 tcp_hc_purge_internal(int all)
657 struct hc_metrics *hc_entry, *hc_next;
660 for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
661 THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
662 TAILQ_FOREACH_SAFE(hc_entry,
663 &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
664 if (all || hc_entry->rmx_expire <= 0) {
665 TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
667 uma_zfree(V_tcp_hostcache.zone, hc_entry);
668 V_tcp_hostcache.hashbase[i].hch_length--;
669 V_tcp_hostcache.cache_count--;
671 hc_entry->rmx_expire -= V_tcp_hostcache.prune;
673 THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
678 * Expire and purge (old|all) entries in the tcp_hostcache. Runs
679 * periodically from the callout.
682 tcp_hc_purge(void *arg)
684 CURVNET_SET((struct vnet *) arg);
687 if (V_tcp_hostcache.purgeall) {
689 V_tcp_hostcache.purgeall = 0;
692 tcp_hc_purge_internal(all);
694 callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,