예제 #1
0
bool
validateErrorMessage(const std::string& expectedMessage, const ConfigFile::Error& error)
{
  bool gotExpected = error.what() == expectedMessage;
  if (!gotExpected)
    {
      NFD_LOG_WARN("\ncaught exception: " << error.what()
                    << "\n\nexpected exception: " << expectedMessage);
    }
  return gotExpected;
}
void
FaceTable::add(shared_ptr<Face> face)
{
  if (face->getId() != INVALID_FACEID && m_faces.count(face->getId()) > 0) {
    NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table");
    return;
  }

  FaceId faceId = ++m_lastFaceId;
  BOOST_ASSERT(faceId > FACEID_RESERVED_MAX);
  this->addImpl(face, faceId);
}
void
WeightedLoadBalancerStrategy::beforeSatisfyInterest(shared_ptr<pit::Entry> pitEntry,
                                                    const Face& inFace,
                                                    const Data& data)
{
  NFD_LOG_TRACE("Received Data: " << data.getName());
  shared_ptr<MyPitInfo> pitInfo = pitEntry->getStrategyInfo<MyPitInfo>();

  // No start time available, cannot compute delay for this retrieval
  if (!static_cast<bool>(pitInfo))
    {
      NFD_LOG_TRACE("No start time available for Data " << data.getName());
      return;
    }

  const milliseconds delay =
    duration_cast<milliseconds>(system_clock::now() - pitInfo->creationTime);

  NFD_LOG_TRACE("Computed delay of: " << system_clock::now() << " - " << pitInfo->creationTime << " = " << delay);

  MeasurementsAccessor& accessor = this->getMeasurements();

  // Update Face delay measurements and entry lifetimes owned
  // by this strategy while walking up the NameTree
  shared_ptr<measurements::Entry> measurementsEntry = accessor.get(*pitEntry);
  if (static_cast<bool>(measurementsEntry))
    {
      NFD_LOG_TRACE("accessor returned measurements entry " << measurementsEntry->getName()
                   << " for " << pitEntry->getName());
    }
  else
    {
      NFD_LOG_WARN ("accessor returned invalid measurements entry for " << pitEntry->getName());
    }

  while (static_cast<bool>(measurementsEntry))
    {
      shared_ptr<MyMeasurementInfo> measurementsEntryInfo =
        measurementsEntry->getStrategyInfo<MyMeasurementInfo>();

      if (static_cast<bool>(measurementsEntryInfo))
        {
          accessor.extendLifetime(*measurementsEntry, seconds(16));
          measurementsEntryInfo->updateFaceDelay(inFace, delay);
        }

      measurementsEntry = accessor.getParent(*measurementsEntry);
    }
}
예제 #4
0
ClientControlStrategy::ClientControlStrategy(Forwarder& forwarder, const Name& name)
  : BestRouteStrategyBase(forwarder)
{
  ParsedInstanceName parsed = parseInstanceName(name);
  if (!parsed.parameters.empty()) {
    BOOST_THROW_EXCEPTION(std::invalid_argument("ClientControlStrategy does not accept parameters"));
  }
  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
    BOOST_THROW_EXCEPTION(std::invalid_argument(
      "ClientControlStrategy does not support version " + to_string(*parsed.version)));
  }
  this->setInstanceName(makeInstanceName(name, getStrategyName()));

  NFD_LOG_WARN("NextHopFaceId field is honored universally and "
               "it's unnecessary to set client-control strategy.");
}
예제 #5
0
void
UnixStreamFactory::doProcessConfig(OptionalConfigSection configSection,
                                   FaceSystem::ConfigContext& context)
{
  // unix
  // {
  //   path /var/run/nfd.sock
  // }

  m_wantCongestionMarking = context.generalConfig.wantCongestionMarking;

  if (!configSection) {
    if (!context.isDryRun && !m_channels.empty()) {
      NFD_LOG_WARN("Cannot disable Unix channel after initialization");
    }
    return;
  }

  std::string path = "/var/run/nfd.sock";

  for (const auto& pair : *configSection) {
    const std::string& key = pair.first;
    const ConfigSection& value = pair.second;

    if (key == "path") {
      path = value.get_value<std::string>();
    }
    else {
      NDN_THROW(ConfigFile::Error("Unrecognized option face_system.unix." + key));
    }
  }

  if (context.isDryRun) {
    return;
  }

  auto channel = this->createChannel(path);
  if (!channel->isListening()) {
    channel->listen(this->addFace, nullptr);
  }
}
shared_ptr<Face>
WeightedLoadBalancerStrategy::selectOutgoingFace(const Face& inFace,
                                                 const Interest& interest,
                                                 shared_ptr<MyMeasurementInfo>& measurementsEntryInfo,
                                                 shared_ptr<pit::Entry>& pitEntry)
{
  auto& facesById =
    measurementsEntryInfo->weightedFaces->get<MyMeasurementInfo::ByFaceId>();

  std::vector<uint64_t> faceIds;
  std::vector<double> weights;

  faceIds.reserve(facesById.size() + 1);
  weights.reserve(facesById.size());

  for (auto faceWeight : facesById)
    {
      faceIds.push_back(faceWeight.face->getId());
      weights.push_back(faceWeight.weight);
    }

  faceIds.push_back(std::numeric_limits<uint64_t>::max());

  std::piecewise_constant_distribution<> dist(faceIds.begin(),
                                              faceIds.end(),
                                              weights.begin());

  const uint64_t selection = dist(m_randomGenerator);

  NFD_LOG_DEBUG("selected value: " << selection);

  auto faceEntry = facesById.begin();
  auto firstMatchIndex = std::numeric_limits<uint64_t>::max();
  auto firstMatchFaceEntry = facesById.end();
  for (uint64_t i = 0; i < facesById.size() - 1; i++)
    {
      if (faceIds[i] <= selection && selection < faceIds[i + 1])
        {
          if (isEligibleFace(pitEntry, inFace, *faceEntry->face))
            {
              NFD_LOG_DEBUG("selected FaceID: " << faceEntry->face->getId());
              return faceEntry->face->shared_from_this();
            }
          else if (i < firstMatchIndex)
            {
              firstMatchIndex = i;
              firstMatchFaceEntry = faceEntry;
            }
        }
      ++faceEntry;
    }

  if (faceEntry != facesById.end() &&
      isEligibleFace(pitEntry, inFace, *faceEntry->face))
    {
      NFD_LOG_DEBUG("selected FaceID: " << faceEntry->face->getId());
      return faceEntry->face->shared_from_this();
    }


  // wrap around and try faces up to, but not including, first match
  faceEntry = facesById.begin();
  const auto limit = std::min(firstMatchIndex, static_cast<uint64_t>(facesById.size()));
  for (uint64_t i = 0; i < limit; i++)
    {
      if (isEligibleFace(pitEntry, inFace, *faceEntry->face))
        {
          NFD_LOG_DEBUG("selected FaceID: " << faceEntry->face->getId());
          return faceEntry->face->shared_from_this();
        }
      ++faceEntry;
    }


  NFD_LOG_WARN("no face selected for forwarding");
  return nullptr;
}
예제 #7
0
std::list< shared_ptr<NetworkInterfaceInfo> >
listNetworkInterfaces()
{
  typedef std::map< std::string, shared_ptr<NetworkInterfaceInfo> > InterfacesMap;
  InterfacesMap ifmap;

  ifaddrs* ifa_list;
  if (::getifaddrs(&ifa_list) < 0)
    throw std::runtime_error("getifaddrs() failed");

  for (ifaddrs* ifa = ifa_list; ifa != 0; ifa = ifa->ifa_next)
    {
      shared_ptr<NetworkInterfaceInfo> netif;
      std::string ifname(ifa->ifa_name);
      InterfacesMap::iterator i = ifmap.find(ifname);
      if (i == ifmap.end())
        {
          netif = make_shared<NetworkInterfaceInfo>();
          netif->name = ifname;
          netif->flags = ifa->ifa_flags;
          ifmap[ifname] = netif;
        }
      else
        {
          netif = i->second;
        }

      if (!ifa->ifa_addr)
        continue;

      switch (ifa->ifa_addr->sa_family)
        {
        case AF_INET: {
            const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr);
            char address[INET_ADDRSTRLEN];
            if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address)))
              netif->ipv4Addresses.push_back(boost::asio::ip::address_v4::from_string(address));
            else
              NFD_LOG_WARN("inet_ntop() failed on " << ifname);
          }
          break;
        case AF_INET6: {
            const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr);
            char address[INET6_ADDRSTRLEN];
            if (::inet_ntop(AF_INET6, &sin6->sin6_addr, address, sizeof(address)))
              netif->ipv6Addresses.push_back(boost::asio::ip::address_v6::from_string(address));
            else
              NFD_LOG_WARN("inet_ntop() failed on " << ifname);
          }
          break;
#if defined(__linux__)
        case AF_PACKET: {
            const sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ifa->ifa_addr);
            netif->index = sll->sll_ifindex;
            if (sll->sll_hatype == ARPHRD_ETHER && sll->sll_halen == ethernet::ADDR_LEN)
              netif->etherAddress = ethernet::Address(sll->sll_addr);
            else if (sll->sll_hatype != ARPHRD_LOOPBACK)
              NFD_LOG_WARN("Unrecognized hardware address on " << ifname);
          }
          break;
#elif defined(__APPLE__) || defined(__FreeBSD__)
        case AF_LINK: {
            const sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(ifa->ifa_addr);
            netif->index = sdl->sdl_index;
            netif->etherAddress = ethernet::Address(reinterpret_cast<uint8_t*>(LLADDR(sdl)));
          }
          break;
#endif
        }

      if (netif->isBroadcastCapable() && ifa->ifa_broadaddr != 0)
        {
          const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_broadaddr);

          char address[INET_ADDRSTRLEN];
          if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address)))
            netif->broadcastAddress = boost::asio::ip::address_v4::from_string(address);
          else
            NFD_LOG_WARN("inet_ntop() failed on " << ifname);
        }
    }

  ::freeifaddrs(ifa_list);

  std::list< shared_ptr<NetworkInterfaceInfo> > list;
  BOOST_FOREACH(InterfacesMap::value_type elem, ifmap) {
    list.push_back(elem.second);
  }