The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/tcp_hostcache.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: f1f8df8240885819b779ee02d152a2c9


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.