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