Home | History | Annotate | Download | only in dns
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "net/dns/host_resolver.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/metrics/field_trial.h"
      9 #include "base/strings/string_number_conversions.h"
     10 #include "base/strings/string_split.h"
     11 #include "net/dns/dns_client.h"
     12 #include "net/dns/dns_config_service.h"
     13 #include "net/dns/host_cache.h"
     14 #include "net/dns/host_resolver_impl.h"
     15 
     16 namespace net {
     17 
     18 namespace {
     19 
     20 // Maximum of 6 concurrent resolver threads (excluding retries).
     21 // Some routers (or resolvers) appear to start to provide host-not-found if
     22 // too many simultaneous resolutions are pending.  This number needs to be
     23 // further optimized, but 8 is what FF currently does. We found some routers
     24 // that limit this to 6, so we're temporarily holding it at that level.
     25 const size_t kDefaultMaxProcTasks = 6u;
     26 
     27 PrioritizedDispatcher::Limits GetDispatcherLimits(
     28     const HostResolver::Options& options) {
     29   PrioritizedDispatcher::Limits limits(NUM_PRIORITIES,
     30                                        options.max_concurrent_resolves);
     31 
     32   // If not using default, do not use the field trial.
     33   if (limits.total_jobs != HostResolver::kDefaultParallelism)
     34     return limits;
     35 
     36   // Default, without trial is no reserved slots.
     37   limits.total_jobs = kDefaultMaxProcTasks;
     38 
     39   // Parallelism is determined by the field trial.
     40   std::string group = base::FieldTrialList::FindFullName(
     41       "HostResolverDispatch");
     42 
     43   if (group.empty())
     44     return limits;
     45 
     46   // The format of the group name is a list of non-negative integers separated
     47   // by ':'. Each of the elements in the list corresponds to an element in
     48   // |reserved_slots|, except the last one which is the |total_jobs|.
     49 
     50   std::vector<std::string> group_parts;
     51   base::SplitString(group, ':', &group_parts);
     52   if (group_parts.size() != NUM_PRIORITIES + 1) {
     53     NOTREACHED();
     54     return limits;
     55   }
     56 
     57   std::vector<size_t> parsed(group_parts.size());
     58   size_t total_reserved_slots = 0;
     59 
     60   for (size_t i = 0; i < group_parts.size(); ++i) {
     61     if (!base::StringToSizeT(group_parts[i], &parsed[i])) {
     62       NOTREACHED();
     63       return limits;
     64     }
     65   }
     66 
     67   size_t total_jobs = parsed.back();
     68   parsed.pop_back();
     69   for (size_t i = 0; i < parsed.size(); ++i) {
     70     total_reserved_slots += parsed[i];
     71   }
     72 
     73   // There must be some unreserved slots available for the all priorities.
     74   if (total_reserved_slots > total_jobs ||
     75       (total_reserved_slots == total_jobs && parsed[MINIMUM_PRIORITY] == 0)) {
     76     NOTREACHED();
     77     return limits;
     78   }
     79 
     80   limits.total_jobs = total_jobs;
     81   limits.reserved_slots = parsed;
     82   return limits;
     83 }
     84 
     85 }  // namespace
     86 
     87 HostResolver::Options::Options()
     88     : max_concurrent_resolves(kDefaultParallelism),
     89       max_retry_attempts(kDefaultRetryAttempts),
     90       enable_caching(true) {
     91 }
     92 
     93 HostResolver::RequestInfo::RequestInfo(const HostPortPair& host_port_pair)
     94     : host_port_pair_(host_port_pair),
     95       address_family_(ADDRESS_FAMILY_UNSPECIFIED),
     96       host_resolver_flags_(0),
     97       allow_cached_response_(true),
     98       is_speculative_(false) {}
     99 
    100 HostResolver::~HostResolver() {
    101 }
    102 
    103 AddressFamily HostResolver::GetDefaultAddressFamily() const {
    104   return ADDRESS_FAMILY_UNSPECIFIED;
    105 }
    106 
    107 void HostResolver::SetDnsClientEnabled(bool enabled) {
    108 }
    109 
    110 HostCache* HostResolver::GetHostCache() {
    111   return NULL;
    112 }
    113 
    114 base::Value* HostResolver::GetDnsConfigAsValue() const {
    115   return NULL;
    116 }
    117 
    118 // static
    119 scoped_ptr<HostResolver>
    120 HostResolver::CreateSystemResolver(const Options& options, NetLog* net_log) {
    121   scoped_ptr<HostCache> cache;
    122   if (options.enable_caching)
    123     cache = HostCache::CreateDefaultCache();
    124   return scoped_ptr<HostResolver>(new HostResolverImpl(
    125       cache.Pass(),
    126       GetDispatcherLimits(options),
    127       HostResolverImpl::ProcTaskParams(NULL, options.max_retry_attempts),
    128       net_log));
    129 }
    130 
    131 // static
    132 scoped_ptr<HostResolver>
    133 HostResolver::CreateDefaultResolver(NetLog* net_log) {
    134   return CreateSystemResolver(Options(), net_log);
    135 }
    136 
    137 HostResolver::HostResolver() {
    138 }
    139 
    140 }  // namespace net
    141