]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/tcp_hostcache.c
This commit was generated by cvs2svn to compensate for changes in r170331,
[FreeBSD/FreeBSD.git] / sys / netinet / tcp_hostcache.c
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  * $FreeBSD$
30  */
31
32 /*
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, bandwidth, max
38  * MTU, congestion window) between local and remote sites, this can lead to
39  * significant speed-ups for new TCP connections after the first one.
40  *
41  * Due to the tcp_hostcache, all TCP-specific metrics information in the
42  * routing table has 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.
46  *
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.
60  */
61
62 /*
63  * Many thanks to jlemon for basic structure of tcp_syncache which is being
64  * followed here.
65  */
66
67 #include "opt_inet6.h"
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/lock.h>
73 #include <sys/mutex.h>
74 #include <sys/malloc.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/sysctl.h>
78
79 #include <net/if.h>
80
81 #include <netinet/in.h>
82 #include <netinet/in_systm.h>
83 #include <netinet/ip.h>
84 #include <netinet/in_var.h>
85 #include <netinet/in_pcb.h>
86 #include <netinet/ip_var.h>
87 #ifdef INET6
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6_var.h>
90 #endif
91 #include <netinet/tcp.h>
92 #include <netinet/tcp_var.h>
93 #ifdef INET6
94 #include <netinet6/tcp6_var.h>
95 #endif
96
97 #include <vm/uma.h>
98
99
100 TAILQ_HEAD(hc_qhead, hc_metrics);
101
102 struct hc_head {
103         struct hc_qhead hch_bucket;
104         u_int           hch_length;
105         struct mtx      hch_mtx;
106 };
107
108 struct hc_metrics {
109         /* housekeeping */
110         TAILQ_ENTRY(hc_metrics) rmx_q;
111         struct  hc_head *rmx_head; /* head of bucket tail queue */
112         struct  in_addr ip4;    /* IP address */
113         struct  in6_addr ip6;   /* IP6 address */
114         /* endpoint specific values for TCP */
115         u_long  rmx_mtu;        /* MTU for this path */
116         u_long  rmx_ssthresh;   /* outbound gateway buffer limit */
117         u_long  rmx_rtt;        /* estimated round trip time */
118         u_long  rmx_rttvar;     /* estimated rtt variance */
119         u_long  rmx_bandwidth;  /* estimated bandwidth */
120         u_long  rmx_cwnd;       /* congestion window */
121         u_long  rmx_sendpipe;   /* outbound delay-bandwidth product */
122         u_long  rmx_recvpipe;   /* inbound delay-bandwidth product */
123         /* TCP hostcache internal data */
124         int     rmx_expire;     /* lifetime for object */
125         u_long  rmx_hits;       /* number of hits */
126         u_long  rmx_updates;    /* number of updates */
127 };
128
129 /* Arbitrary values */
130 #define TCP_HOSTCACHE_HASHSIZE          512
131 #define TCP_HOSTCACHE_BUCKETLIMIT       30
132 #define TCP_HOSTCACHE_EXPIRE            60*60   /* one hour */
133 #define TCP_HOSTCACHE_PRUNE             5*60    /* every 5 minutes */
134
135 struct tcp_hostcache {
136         struct  hc_head *hashbase;
137         uma_zone_t zone;
138         u_int   hashsize;
139         u_int   hashmask;
140         u_int   bucket_limit;
141         u_int   cache_count;
142         u_int   cache_limit;
143         int     expire;
144         int     purgeall;
145 };
146 static struct tcp_hostcache tcp_hostcache;
147
148 static struct callout tcp_hc_callout;
149
150 static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
151 static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
152 static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
153 static void tcp_hc_purge(void *);
154
155 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0, "TCP Host cache");
156
157 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_RDTUN,
158     &tcp_hostcache.cache_limit, 0, "Overall entry limit for hostcache");
159
160 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_RDTUN,
161     &tcp_hostcache.hashsize, 0, "Size of TCP hostcache hashtable");
162
163 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, CTLFLAG_RDTUN,
164     &tcp_hostcache.bucket_limit, 0, "Per-bucket hash limit for hostcache");
165
166 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_RD,
167     &tcp_hostcache.cache_count, 0, "Current number of entries in hostcache");
168
169 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_RW,
170     &tcp_hostcache.expire, 0, "Expire time of TCP hostcache entries");
171
172 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_RW,
173     &tcp_hostcache.purgeall, 0, "Expire all entires on next purge run");
174
175 SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
176     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
177     sysctl_tcp_hc_list, "A", "List of all hostcache entries");
178
179
180 static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
181
182 #define HOSTCACHE_HASH(ip) \
183         (((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) &  \
184           tcp_hostcache.hashmask)
185
186 /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
187 #define HOSTCACHE_HASH6(ip6)                            \
188         (((ip6)->s6_addr32[0] ^                         \
189           (ip6)->s6_addr32[1] ^                         \
190           (ip6)->s6_addr32[2] ^                         \
191           (ip6)->s6_addr32[3]) &                        \
192          tcp_hostcache.hashmask)
193
194 #define THC_LOCK(lp)            mtx_lock(lp)
195 #define THC_UNLOCK(lp)          mtx_unlock(lp)
196
197 void
198 tcp_hc_init(void)
199 {
200         int i;
201
202         /*
203          * Initialize hostcache structures.
204          */
205         tcp_hostcache.cache_count = 0;
206         tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
207         tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
208         tcp_hostcache.cache_limit =
209             tcp_hostcache.hashsize * tcp_hostcache.bucket_limit;
210         tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
211
212         TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
213             &tcp_hostcache.hashsize);
214         TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
215             &tcp_hostcache.cache_limit);
216         TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
217             &tcp_hostcache.bucket_limit);
218         if (!powerof2(tcp_hostcache.hashsize)) {
219                 printf("WARNING: hostcache hash size is not a power of 2.\n");
220                 tcp_hostcache.hashsize = 512;   /* safe default */
221         }
222         tcp_hostcache.hashmask = tcp_hostcache.hashsize - 1;
223
224         /*
225          * Allocate the hash table.
226          */
227         tcp_hostcache.hashbase = (struct hc_head *)
228             malloc(tcp_hostcache.hashsize * sizeof(struct hc_head),
229                    M_HOSTCACHE, M_WAITOK | M_ZERO);
230
231         /*
232          * Initialize the hash buckets.
233          */
234         for (i = 0; i < tcp_hostcache.hashsize; i++) {
235                 TAILQ_INIT(&tcp_hostcache.hashbase[i].hch_bucket);
236                 tcp_hostcache.hashbase[i].hch_length = 0;
237                 mtx_init(&tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
238                           NULL, MTX_DEF);
239         }
240
241         /*
242          * Allocate the hostcache entries.
243          */
244         tcp_hostcache.zone = uma_zcreate("hostcache", sizeof(struct hc_metrics),
245             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
246         uma_zone_set_max(tcp_hostcache.zone, tcp_hostcache.cache_limit);
247
248         /*
249          * Set up periodic cache cleanup.
250          */
251         callout_init(&tcp_hc_callout, CALLOUT_MPSAFE);
252         callout_reset(&tcp_hc_callout, TCP_HOSTCACHE_PRUNE * hz, tcp_hc_purge, 0);
253 }
254
255 /*
256  * Internal function: look up an entry in the hostcache or return NULL.
257  *
258  * If an entry has been returned, the caller becomes responsible for
259  * unlocking the bucket row after he is done reading/modifying the entry.
260  */
261 static struct hc_metrics *
262 tcp_hc_lookup(struct in_conninfo *inc)
263 {
264         int hash;
265         struct hc_head *hc_head;
266         struct hc_metrics *hc_entry;
267
268         KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
269
270         /*
271          * Hash the foreign ip address.
272          */
273         if (inc->inc_isipv6)
274                 hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
275         else
276                 hash = HOSTCACHE_HASH(&inc->inc_faddr);
277
278         hc_head = &tcp_hostcache.hashbase[hash];
279
280         /*
281          * Acquire lock for this bucket row; we release the lock if we don't
282          * find an entry, otherwise the caller has to unlock after he is
283          * done.
284          */
285         THC_LOCK(&hc_head->hch_mtx);
286
287         /*
288          * Iterate through entries in bucket row looking for a match.
289          */
290         TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
291                 if (inc->inc_isipv6) {
292                         if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
293                             sizeof(inc->inc6_faddr)) == 0)
294                                 return hc_entry;
295                 } else {
296                         if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
297                             sizeof(inc->inc_faddr)) == 0)
298                                 return hc_entry;
299                 }
300         }
301
302         /*
303          * We were unsuccessful and didn't find anything.
304          */
305         THC_UNLOCK(&hc_head->hch_mtx);
306         return NULL;
307 }
308
309 /*
310  * Internal function: insert an entry into the hostcache or return NULL if
311  * unable to allocate a new one.
312  *
313  * If an entry has been returned, the caller becomes responsible for
314  * unlocking the bucket row after he is done reading/modifying the entry.
315  */
316 static struct hc_metrics *
317 tcp_hc_insert(struct in_conninfo *inc)
318 {
319         int hash;
320         struct hc_head *hc_head;
321         struct hc_metrics *hc_entry;
322
323         KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
324
325         /*
326          * Hash the foreign ip address.
327          */
328         if (inc->inc_isipv6)
329                 hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
330         else
331                 hash = HOSTCACHE_HASH(&inc->inc_faddr);
332
333         hc_head = &tcp_hostcache.hashbase[hash];
334
335         /*
336          * Acquire lock for this bucket row; we release the lock if we don't
337          * find an entry, otherwise the caller has to unlock after he is
338          * done.
339          */
340         THC_LOCK(&hc_head->hch_mtx);
341
342         /*
343          * If the bucket limit is reached, reuse the least-used element.
344          */
345         if (hc_head->hch_length >= tcp_hostcache.bucket_limit ||
346             tcp_hostcache.cache_count >= tcp_hostcache.cache_limit) {
347                 hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
348                 /*
349                  * At first we were dropping the last element, just to
350                  * reacquire it in the next two lines again, which isn't very
351                  * efficient.  Instead just reuse the least used element.
352                  * We may drop something that is still "in-use" but we can be
353                  * "lossy".
354                  */
355                 TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
356                 tcp_hostcache.hashbase[hash].hch_length--;
357                 tcp_hostcache.cache_count--;
358                 tcpstat.tcps_hc_bucketoverflow++;
359 #if 0
360                 uma_zfree(tcp_hostcache.zone, hc_entry);
361 #endif
362         } else {
363                 /*
364                  * Allocate a new entry, or balk if not possible.
365                  */
366                 hc_entry = uma_zalloc(tcp_hostcache.zone, M_NOWAIT);
367                 if (hc_entry == NULL) {
368                         THC_UNLOCK(&hc_head->hch_mtx);
369                         return NULL;
370                 }
371         }
372
373         /*
374          * Initialize basic information of hostcache entry.
375          */
376         bzero(hc_entry, sizeof(*hc_entry));
377         if (inc->inc_isipv6)
378                 bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6));
379         else
380                 hc_entry->ip4 = inc->inc_faddr;
381         hc_entry->rmx_head = hc_head;
382         hc_entry->rmx_expire = tcp_hostcache.expire;
383
384         /*
385          * Put it upfront.
386          */
387         TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
388         tcp_hostcache.hashbase[hash].hch_length++;
389         tcp_hostcache.cache_count++;
390         tcpstat.tcps_hc_added++;
391
392         return hc_entry;
393 }
394
395 /*
396  * External function: look up an entry in the hostcache and fill out the
397  * supplied TCP metrics structure.  Fills in NULL when no entry was found or
398  * a value is not set.
399  */
400 void
401 tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
402 {
403         struct hc_metrics *hc_entry;
404
405         /*
406          * Find the right bucket.
407          */
408         hc_entry = tcp_hc_lookup(inc);
409
410         /*
411          * If we don't have an existing object.
412          */
413         if (hc_entry == NULL) {
414                 bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
415                 return;
416         }
417         hc_entry->rmx_hits++;
418         hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
419
420         hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
421         hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
422         hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
423         hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
424         hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth;
425         hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
426         hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
427         hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
428
429         /*
430          * Unlock bucket row.
431          */
432         THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
433 }
434
435 /*
436  * External function: look up an entry in the hostcache and return the
437  * discovered path MTU.  Returns NULL if no entry is found or value is not
438  * set.
439  */
440 u_long
441 tcp_hc_getmtu(struct in_conninfo *inc)
442 {
443         struct hc_metrics *hc_entry;
444         u_long mtu;
445
446         hc_entry = tcp_hc_lookup(inc);
447         if (hc_entry == NULL) {
448                 return 0;
449         }
450         hc_entry->rmx_hits++;
451         hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
452
453         mtu = hc_entry->rmx_mtu;
454         THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
455         return mtu;
456 }
457
458 /*
459  * External function: update the MTU value of an entry in the hostcache.
460  * Creates a new entry if none was found.
461  */
462 void
463 tcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
464 {
465         struct hc_metrics *hc_entry;
466
467         /*
468          * Find the right bucket.
469          */
470         hc_entry = tcp_hc_lookup(inc);
471
472         /*
473          * If we don't have an existing object, try to insert a new one.
474          */
475         if (hc_entry == NULL) {
476                 hc_entry = tcp_hc_insert(inc);
477                 if (hc_entry == NULL)
478                         return;
479         }
480         hc_entry->rmx_updates++;
481         hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
482
483         hc_entry->rmx_mtu = mtu;
484
485         /*
486          * Put it upfront so we find it faster next time.
487          */
488         TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
489         TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
490
491         /*
492          * Unlock bucket row.
493          */
494         THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
495 }
496
497 /*
498  * External function: update the TCP metrics of an entry in the hostcache.
499  * Creates a new entry if none was found.
500  */
501 void
502 tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
503 {
504         struct hc_metrics *hc_entry;
505
506         hc_entry = tcp_hc_lookup(inc);
507         if (hc_entry == NULL) {
508                 hc_entry = tcp_hc_insert(inc);
509                 if (hc_entry == NULL)
510                         return;
511         }
512         hc_entry->rmx_updates++;
513         hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
514
515         if (hcml->rmx_rtt != 0) {
516                 if (hc_entry->rmx_rtt == 0)
517                         hc_entry->rmx_rtt = hcml->rmx_rtt;
518                 else
519                         hc_entry->rmx_rtt =
520                             (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
521                 tcpstat.tcps_cachedrtt++;
522         }
523         if (hcml->rmx_rttvar != 0) {
524                 if (hc_entry->rmx_rttvar == 0)
525                         hc_entry->rmx_rttvar = hcml->rmx_rttvar;
526                 else
527                         hc_entry->rmx_rttvar =
528                             (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
529                 tcpstat.tcps_cachedrttvar++;
530         }
531         if (hcml->rmx_ssthresh != 0) {
532                 if (hc_entry->rmx_ssthresh == 0)
533                         hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
534                 else
535                         hc_entry->rmx_ssthresh =
536                             (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
537                 tcpstat.tcps_cachedssthresh++;
538         }
539         if (hcml->rmx_bandwidth != 0) {
540                 if (hc_entry->rmx_bandwidth == 0)
541                         hc_entry->rmx_bandwidth = hcml->rmx_bandwidth;
542                 else
543                         hc_entry->rmx_bandwidth =
544                             (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2;
545                 /* tcpstat.tcps_cachedbandwidth++; */
546         }
547         if (hcml->rmx_cwnd != 0) {
548                 if (hc_entry->rmx_cwnd == 0)
549                         hc_entry->rmx_cwnd = hcml->rmx_cwnd;
550                 else
551                         hc_entry->rmx_cwnd =
552                             (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
553                 /* tcpstat.tcps_cachedcwnd++; */
554         }
555         if (hcml->rmx_sendpipe != 0) {
556                 if (hc_entry->rmx_sendpipe == 0)
557                         hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
558                 else
559                         hc_entry->rmx_sendpipe =
560                             (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
561                 /* tcpstat.tcps_cachedsendpipe++; */
562         }
563         if (hcml->rmx_recvpipe != 0) {
564                 if (hc_entry->rmx_recvpipe == 0)
565                         hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
566                 else
567                         hc_entry->rmx_recvpipe =
568                             (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
569                 /* tcpstat.tcps_cachedrecvpipe++; */
570         }
571
572         TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
573         TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
574         THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
575 }
576
577 /*
578  * Sysctl function: prints the list and values of all hostcache entries in
579  * unsorted order.
580  */
581 static int
582 sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
583 {
584         int bufsize;
585         int linesize = 128;
586         char *p, *buf;
587         int len, i, error;
588         struct hc_metrics *hc_entry;
589 #ifdef INET6
590         char ip6buf[INET6_ADDRSTRLEN];
591 #endif
592
593         bufsize = linesize * (tcp_hostcache.cache_count + 1);
594
595         p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
596
597         len = snprintf(p, linesize,
598                 "\nIP address        MTU  SSTRESH      RTT   RTTVAR BANDWIDTH "
599                 "    CWND SENDPIPE RECVPIPE HITS  UPD  EXP\n");
600         p += len;
601
602 #define msec(u) (((u) + 500) / 1000)
603         for (i = 0; i < tcp_hostcache.hashsize; i++) {
604                 THC_LOCK(&tcp_hostcache.hashbase[i].hch_mtx);
605                 TAILQ_FOREACH(hc_entry, &tcp_hostcache.hashbase[i].hch_bucket,
606                               rmx_q) {
607                         len = snprintf(p, linesize,
608                             "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu "
609                             "%4lu %4lu %4i\n",
610                             hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
611 #ifdef INET6
612                                 ip6_sprintf(ip6buf, &hc_entry->ip6),
613 #else
614                                 "IPv6?",
615 #endif
616                             hc_entry->rmx_mtu,
617                             hc_entry->rmx_ssthresh,
618                             msec(hc_entry->rmx_rtt *
619                                 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
620                             msec(hc_entry->rmx_rttvar *
621                                 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
622                             hc_entry->rmx_bandwidth * 8,
623                             hc_entry->rmx_cwnd,
624                             hc_entry->rmx_sendpipe,
625                             hc_entry->rmx_recvpipe,
626                             hc_entry->rmx_hits,
627                             hc_entry->rmx_updates,
628                             hc_entry->rmx_expire);
629                         p += len;
630                 }
631                 THC_UNLOCK(&tcp_hostcache.hashbase[i].hch_mtx);
632         }
633 #undef msec
634         error = SYSCTL_OUT(req, buf, p - buf);
635         free(buf, M_TEMP);
636         return(error);
637 }
638
639 /*
640  * Expire and purge (old|all) entries in the tcp_hostcache.  Runs
641  * periodically from the callout.
642  */
643 static void
644 tcp_hc_purge(void *arg)
645 {
646         struct hc_metrics *hc_entry, *hc_next;
647         int all = (intptr_t)arg;
648         int i;
649
650         if (tcp_hostcache.purgeall) {
651                 all = 1;
652                 tcp_hostcache.purgeall = 0;
653         }
654
655         for (i = 0; i < tcp_hostcache.hashsize; i++) {
656                 THC_LOCK(&tcp_hostcache.hashbase[i].hch_mtx);
657                 TAILQ_FOREACH_SAFE(hc_entry, &tcp_hostcache.hashbase[i].hch_bucket,
658                               rmx_q, hc_next) {
659                         if (all || hc_entry->rmx_expire <= 0) {
660                                 TAILQ_REMOVE(&tcp_hostcache.hashbase[i].hch_bucket,
661                                               hc_entry, rmx_q);
662                                 uma_zfree(tcp_hostcache.zone, hc_entry);
663                                 tcp_hostcache.hashbase[i].hch_length--;
664                                 tcp_hostcache.cache_count--;
665                         } else
666                                 hc_entry->rmx_expire -= TCP_HOSTCACHE_PRUNE;
667                 }
668                 THC_UNLOCK(&tcp_hostcache.hashbase[i].hch_mtx);
669         }
670         callout_reset(&tcp_hc_callout, TCP_HOSTCACHE_PRUNE * hz, tcp_hc_purge, 0);
671 }