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 has 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, "TCP Host cache"); 159 160 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_RDTUN, 161 &tcp_hostcache.cache_limit, 0, "Overall entry limit for hostcache"); 162 163 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_RDTUN, 164 &tcp_hostcache.hashsize, 0, "Size of TCP hostcache hashtable"); 165 166 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, CTLFLAG_RDTUN, 167 &tcp_hostcache.bucket_limit, 0, "Per-bucket hash limit for hostcache"); 168 169 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_RD, 170 &tcp_hostcache.cache_count, 0, "Current number of entries in hostcache"); 171 172 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_RW, 173 &tcp_hostcache.expire, 0, "Expire time of TCP hostcache entries"); 174 175 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_RW, 176 &tcp_hostcache.prune, 0, "Time between purge runs"); 177 178 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_RW, 179 &tcp_hostcache.purgeall, 0, "Expire all entires on next purge run"); 180 181 SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list, 182 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0, 183 sysctl_tcp_hc_list, "A", "List of all hostcache entries"); 184 185 186 static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache"); 187 188 #define HOSTCACHE_HASH(ip) \ 189 (((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) & \ 190 V_tcp_hostcache.hashmask) 191 192 /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */ 193 #define HOSTCACHE_HASH6(ip6) \ 194 (((ip6)->s6_addr32[0] ^ \ 195 (ip6)->s6_addr32[1] ^ \ 196 (ip6)->s6_addr32[2] ^ \ 197 (ip6)->s6_addr32[3]) & \ 198 V_tcp_hostcache.hashmask) 199 200 #define THC_LOCK(lp) mtx_lock(lp) 201 #define THC_UNLOCK(lp) mtx_unlock(lp) 202 203 void 204 tcp_hc_init(void) 205 { 206 int i; 207 208 /* 209 * Initialize hostcache structures. 210 */ 211 V_tcp_hostcache.cache_count = 0; 212 V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; 213 V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT; 214 V_tcp_hostcache.cache_limit = 215 V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit; 216 V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE; 217 V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE; 218 219 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize", 220 &V_tcp_hostcache.hashsize); 221 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit", 222 &V_tcp_hostcache.cache_limit); 223 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit", 224 &V_tcp_hostcache.bucket_limit); 225 if (!powerof2(V_tcp_hostcache.hashsize)) { 226 printf("WARNING: hostcache hash size is not a power of 2.\n"); 227 V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */ 228 } 229 V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1; 230 231 /* 232 * Allocate the hash table. 233 */ 234 V_tcp_hostcache.hashbase = (struct hc_head *) 235 malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head), 236 M_HOSTCACHE, M_WAITOK | M_ZERO); 237 238 /* 239 * Initialize the hash buckets. 240 */ 241 for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 242 TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket); 243 V_tcp_hostcache.hashbase[i].hch_length = 0; 244 mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry", 245 NULL, MTX_DEF); 246 } 247 248 /* 249 * Allocate the hostcache entries. 250 */ 251 V_tcp_hostcache.zone = 252 uma_zcreate("hostcache", sizeof(struct hc_metrics), 253 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 254 uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit); 255 256 /* 257 * Set up periodic cache cleanup. 258 */ 259 callout_init(&V_tcp_hc_callout, CALLOUT_MPSAFE); 260 callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 261 tcp_hc_purge, 0); 262 } 263 264 /* 265 * Internal function: look up an entry in the hostcache or return NULL. 266 * 267 * If an entry has been returned, the caller becomes responsible for 268 * unlocking the bucket row after he is done reading/modifying the entry. 269 */ 270 static struct hc_metrics * 271 tcp_hc_lookup(struct in_conninfo *inc) 272 { 273 int hash; 274 struct hc_head *hc_head; 275 struct hc_metrics *hc_entry; 276 277 KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer")); 278 279 /* 280 * Hash the foreign ip address. 281 */ 282 if (inc->inc_isipv6) 283 hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 284 else 285 hash = HOSTCACHE_HASH(&inc->inc_faddr); 286 287 hc_head = &V_tcp_hostcache.hashbase[hash]; 288 289 /* 290 * Acquire lock for this bucket row; we release the lock if we don't 291 * find an entry, otherwise the caller has to unlock after he is 292 * done. 293 */ 294 THC_LOCK(&hc_head->hch_mtx); 295 296 /* 297 * Iterate through entries in bucket row looking for a match. 298 */ 299 TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) { 300 if (inc->inc_isipv6) { 301 if (memcmp(&inc->inc6_faddr, &hc_entry->ip6, 302 sizeof(inc->inc6_faddr)) == 0) 303 return hc_entry; 304 } else { 305 if (memcmp(&inc->inc_faddr, &hc_entry->ip4, 306 sizeof(inc->inc_faddr)) == 0) 307 return hc_entry; 308 } 309 } 310 311 /* 312 * We were unsuccessful and didn't find anything. 313 */ 314 THC_UNLOCK(&hc_head->hch_mtx); 315 return NULL; 316 } 317 318 /* 319 * Internal function: insert an entry into the hostcache or return NULL if 320 * unable to allocate a new one. 321 * 322 * If an entry has been returned, the caller becomes responsible for 323 * unlocking the bucket row after he is done reading/modifying the entry. 324 */ 325 static struct hc_metrics * 326 tcp_hc_insert(struct in_conninfo *inc) 327 { 328 int hash; 329 struct hc_head *hc_head; 330 struct hc_metrics *hc_entry; 331 332 KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer")); 333 334 /* 335 * Hash the foreign ip address. 336 */ 337 if (inc->inc_isipv6) 338 hash = HOSTCACHE_HASH6(&inc->inc6_faddr); 339 else 340 hash = HOSTCACHE_HASH(&inc->inc_faddr); 341 342 hc_head = &V_tcp_hostcache.hashbase[hash]; 343 344 /* 345 * Acquire lock for this bucket row; we release the lock if we don't 346 * find an entry, otherwise the caller has to unlock after he is 347 * done. 348 */ 349 THC_LOCK(&hc_head->hch_mtx); 350 351 /* 352 * If the bucket limit is reached, reuse the least-used element. 353 */ 354 if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit || 355 V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) { 356 hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead); 357 /* 358 * At first we were dropping the last element, just to 359 * reacquire it in the next two lines again, which isn't very 360 * efficient. Instead just reuse the least used element. 361 * We may drop something that is still "in-use" but we can be 362 * "lossy". 363 * Just give up if this bucket row is empty and we don't have 364 * anything to replace. 365 */ 366 if (hc_entry == NULL) { 367 THC_UNLOCK(&hc_head->hch_mtx); 368 return NULL; 369 } 370 TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q); 371 V_tcp_hostcache.hashbase[hash].hch_length--; 372 V_tcp_hostcache.cache_count--; 373 V_tcpstat.tcps_hc_bucketoverflow++; 374 #if 0 375 uma_zfree(V_tcp_hostcache.zone, hc_entry); 376 #endif 377 } else { 378 /* 379 * Allocate a new entry, or balk if not possible. 380 */ 381 hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT); 382 if (hc_entry == NULL) { 383 THC_UNLOCK(&hc_head->hch_mtx); 384 return NULL; 385 } 386 } 387 388 /* 389 * Initialize basic information of hostcache entry. 390 */ 391 bzero(hc_entry, sizeof(*hc_entry)); 392 if (inc->inc_isipv6) 393 bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6)); 394 else 395 hc_entry->ip4 = inc->inc_faddr; 396 hc_entry->rmx_head = hc_head; 397 hc_entry->rmx_expire = V_tcp_hostcache.expire; 398 399 /* 400 * Put it upfront. 401 */ 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 V_tcpstat.tcps_hc_added++; 406 407 return hc_entry; 408 } 409 410 /* 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. 414 */ 415 void 416 tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite) 417 { 418 struct hc_metrics *hc_entry; 419 420 /* 421 * Find the right bucket. 422 */ 423 hc_entry = tcp_hc_lookup(inc); 424 425 /* 426 * If we don't have an existing object. 427 */ 428 if (hc_entry == NULL) { 429 bzero(hc_metrics_lite, sizeof(*hc_metrics_lite)); 430 return; 431 } 432 hc_entry->rmx_hits++; 433 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 434 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; 443 444 /* 445 * Unlock bucket row. 446 */ 447 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 448 } 449 450 /* 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 453 * set. 454 */ 455 u_long 456 tcp_hc_getmtu(struct in_conninfo *inc) 457 { 458 struct hc_metrics *hc_entry; 459 u_long mtu; 460 461 hc_entry = tcp_hc_lookup(inc); 462 if (hc_entry == NULL) { 463 return 0; 464 } 465 hc_entry->rmx_hits++; 466 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 467 468 mtu = hc_entry->rmx_mtu; 469 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 470 return mtu; 471 } 472 473 /* 474 * External function: update the MTU value of an entry in the hostcache. 475 * Creates a new entry if none was found. 476 */ 477 void 478 tcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu) 479 { 480 struct hc_metrics *hc_entry; 481 482 /* 483 * Find the right bucket. 484 */ 485 hc_entry = tcp_hc_lookup(inc); 486 487 /* 488 * If we don't have an existing object, try to insert a new one. 489 */ 490 if (hc_entry == NULL) { 491 hc_entry = tcp_hc_insert(inc); 492 if (hc_entry == NULL) 493 return; 494 } 495 hc_entry->rmx_updates++; 496 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 497 498 hc_entry->rmx_mtu = mtu; 499 500 /* 501 * Put it upfront so we find it faster next time. 502 */ 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); 505 506 /* 507 * Unlock bucket row. 508 */ 509 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx); 510 } 511 512 /* 513 * External function: update the TCP metrics of an entry in the hostcache. 514 * Creates a new entry if none was found. 515 */ 516 void 517 tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml) 518 { 519 struct hc_metrics *hc_entry; 520 521 hc_entry = tcp_hc_lookup(inc); 522 if (hc_entry == NULL) { 523 hc_entry = tcp_hc_insert(inc); 524 if (hc_entry == NULL) 525 return; 526 } 527 hc_entry->rmx_updates++; 528 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */ 529 530 if (hcml->rmx_rtt != 0) { 531 if (hc_entry->rmx_rtt == 0) 532 hc_entry->rmx_rtt = hcml->rmx_rtt; 533 else 534 hc_entry->rmx_rtt = 535 (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2; 536 V_tcpstat.tcps_cachedrtt++; 537 } 538 if (hcml->rmx_rttvar != 0) { 539 if (hc_entry->rmx_rttvar == 0) 540 hc_entry->rmx_rttvar = hcml->rmx_rttvar; 541 else 542 hc_entry->rmx_rttvar = 543 (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2; 544 V_tcpstat.tcps_cachedrttvar++; 545 } 546 if (hcml->rmx_ssthresh != 0) { 547 if (hc_entry->rmx_ssthresh == 0) 548 hc_entry->rmx_ssthresh = hcml->rmx_ssthresh; 549 else 550 hc_entry->rmx_ssthresh = 551 (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2; 552 V_tcpstat.tcps_cachedssthresh++; 553 } 554 if (hcml->rmx_bandwidth != 0) { 555 if (hc_entry->rmx_bandwidth == 0) 556 hc_entry->rmx_bandwidth = hcml->rmx_bandwidth; 557 else 558 hc_entry->rmx_bandwidth = 559 (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2; 560 /* V_tcpstat.tcps_cachedbandwidth++; */ 561 } 562 if (hcml->rmx_cwnd != 0) { 563 if (hc_entry->rmx_cwnd == 0) 564 hc_entry->rmx_cwnd = hcml->rmx_cwnd; 565 else 566 hc_entry->rmx_cwnd = 567 (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2; 568 /* V_tcpstat.tcps_cachedcwnd++; */ 569 } 570 if (hcml->rmx_sendpipe != 0) { 571 if (hc_entry->rmx_sendpipe == 0) 572 hc_entry->rmx_sendpipe = hcml->rmx_sendpipe; 573 else 574 hc_entry->rmx_sendpipe = 575 (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2; 576 /* V_tcpstat.tcps_cachedsendpipe++; */ 577 } 578 if (hcml->rmx_recvpipe != 0) { 579 if (hc_entry->rmx_recvpipe == 0) 580 hc_entry->rmx_recvpipe = hcml->rmx_recvpipe; 581 else 582 hc_entry->rmx_recvpipe = 583 (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2; 584 /* V_tcpstat.tcps_cachedrecvpipe++; */ 585 } 586 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); 590 } 591 592 /* 593 * Sysctl function: prints the list and values of all hostcache entries in 594 * unsorted order. 595 */ 596 static int 597 sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS) 598 { 599 int bufsize; 600 int linesize = 128; 601 char *p, *buf; 602 int len, i, error; 603 struct hc_metrics *hc_entry; 604 #ifdef INET6 605 char ip6buf[INET6_ADDRSTRLEN]; 606 #endif 607 608 bufsize = linesize * (V_tcp_hostcache.cache_count + 1); 609 610 p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO); 611 612 len = snprintf(p, linesize, 613 "\nIP address MTU SSTRESH RTT RTTVAR BANDWIDTH " 614 " CWND SENDPIPE RECVPIPE HITS UPD EXP\n"); 615 p += len; 616 617 #define msec(u) (((u) + 500) / 1000) 618 for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 619 THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 620 TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket, 621 rmx_q) { 622 len = snprintf(p, linesize, 623 "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu " 624 "%4lu %4lu %4i\n", 625 hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) : 626 #ifdef INET6 627 ip6_sprintf(ip6buf, &hc_entry->ip6), 628 #else 629 "IPv6?", 630 #endif 631 hc_entry->rmx_mtu, 632 hc_entry->rmx_ssthresh, 633 msec(hc_entry->rmx_rtt * 634 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))), 635 msec(hc_entry->rmx_rttvar * 636 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))), 637 hc_entry->rmx_bandwidth * 8, 638 hc_entry->rmx_cwnd, 639 hc_entry->rmx_sendpipe, 640 hc_entry->rmx_recvpipe, 641 hc_entry->rmx_hits, 642 hc_entry->rmx_updates, 643 hc_entry->rmx_expire); 644 p += len; 645 } 646 THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 647 } 648 #undef msec 649 error = SYSCTL_OUT(req, buf, p - buf); 650 free(buf, M_TEMP); 651 return(error); 652 } 653 654 /* 655 * Expire and purge (old|all) entries in the tcp_hostcache. Runs 656 * periodically from the callout. 657 */ 658 static void 659 tcp_hc_purge(void *arg) 660 { 661 struct hc_metrics *hc_entry, *hc_next; 662 int all = (intptr_t)arg; 663 int i; 664 665 if (V_tcp_hostcache.purgeall) { 666 all = 1; 667 V_tcp_hostcache.purgeall = 0; 668 } 669 670 for (i = 0; i < V_tcp_hostcache.hashsize; i++) { 671 THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 672 TAILQ_FOREACH_SAFE(hc_entry, 673 &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) { 674 if (all || hc_entry->rmx_expire <= 0) { 675 TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket, 676 hc_entry, rmx_q); 677 uma_zfree(V_tcp_hostcache.zone, hc_entry); 678 V_tcp_hostcache.hashbase[i].hch_length--; 679 V_tcp_hostcache.cache_count--; 680 } else 681 hc_entry->rmx_expire -= V_tcp_hostcache.prune; 682 } 683 THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx); 684 } 685 686 callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz, 687 tcp_hc_purge, arg); 688 } 689