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

Cache object: 97d995c7bed092e57df39b56e6203d29


[ 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.