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