diff --git a/rib/readvertise/readvertise.cpp b/rib/readvertise/readvertise.cpp new file mode 100644 index 00000000..c585bf55 --- /dev/null +++ b/rib/readvertise/readvertise.cpp @@ -0,0 +1,198 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (c) 2014-2017, Regents of the University of California, + * Arizona Board of Regents, + * Colorado State University, + * University Pierre & Marie Curie, Sorbonne University, + * Washington University in St. Louis, + * Beijing Institute of Technology, + * The University of Memphis. + * + * This file is part of NFD (Named Data Networking Forwarding Daemon). + * See AUTHORS.md for complete list of NFD authors and contributors. + * + * NFD is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * NFD, e.g., in COPYING.md file. If not, see . + */ + +#include "readvertise.hpp" +#include "core/logger.hpp" +#include "core/random.hpp" + +namespace nfd { +namespace rib { + +NFD_LOG_INIT("Readvertise"); + +const time::milliseconds Readvertise::RETRY_DELAY_MIN = time::seconds(50); +const time::milliseconds Readvertise::RETRY_DELAY_MAX = time::seconds(3600); + +static time::milliseconds +randomizeTimer(time::milliseconds baseTimer) +{ + std::uniform_int_distribution dist(-5, 5); + time::milliseconds newTime = baseTimer + time::milliseconds(dist(getGlobalRng())); + return std::max(newTime, time::milliseconds(0)); +} + +Readvertise::Readvertise(Rib& rib, unique_ptr policy, + unique_ptr destination) + : m_policy(std::move(policy)) + , m_destination(std::move(destination)) +{ + m_addRouteConn = rib.afterAddRoute.connect(bind(&Readvertise::afterAddRoute, this, _1)); + m_removeRouteConn = rib.beforeRemoveRoute.connect(bind(&Readvertise::beforeRemoveRoute, this, _1)); + + m_destination->afterAvailabilityChange.connect([this] (bool isAvailable) { + if (isAvailable) { + this->afterDestinationAvailable(); + } + else { + this->afterDestinationUnavailable(); + } + }); +} + +void +Readvertise::afterAddRoute(const RibRouteRef& ribRoute) +{ + ndn::optional action = m_policy->handleNewRoute(ribRoute); + if (!action) { + NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << + ',' << ribRoute.route->origin << ") not-readvertising"); + return; + } + + ReadvertisedRouteContainer::iterator rrIt; + bool isNew = false; + std::tie(rrIt, isNew) = m_rrs.emplace(action->prefix); + + if (!isNew && rrIt->signer != action->signer) { + NFD_LOG_WARN("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << + ',' << ribRoute.route->origin << ") readvertising-as " << action->prefix << + " old-signer " << rrIt->signer << " new-signer " << action->signer); + } + rrIt->signer = action->signer; + + RouteRrIndex::iterator indexIt; + std::tie(indexIt, isNew) = m_routeToRr.emplace(ribRoute, rrIt); + BOOST_ASSERT(isNew); + + if (rrIt->nRibRoutes++ > 0) { + NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << + ',' << ribRoute.route->origin << ") already-readvertised-as " << action->prefix); + return; + } + + NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << + ',' << ribRoute.route->origin << ") readvertising-as " << action->prefix << + " signer " << action->signer); + rrIt->retryDelay = RETRY_DELAY_MIN; + this->advertise(rrIt); +} + +void +Readvertise::beforeRemoveRoute(const RibRouteRef& ribRoute) +{ + auto indexIt = m_routeToRr.find(ribRoute); + if (indexIt == m_routeToRr.end()) { + NFD_LOG_DEBUG("remove-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << + ',' << ribRoute.route->origin << ") not-readvertised"); + return; + } + + auto rrIt = indexIt->second; + m_routeToRr.erase(indexIt); + + if (--rrIt->nRibRoutes > 0) { + NFD_LOG_DEBUG("remove-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << + ',' << ribRoute.route->origin << ") needed-by " << rrIt->nRibRoutes); + return; + } + + rrIt->retryDelay = RETRY_DELAY_MIN; + this->withdraw(rrIt); +} + +void +Readvertise::afterDestinationAvailable() +{ + for (auto rrIt = m_rrs.begin(); rrIt != m_rrs.end(); ++rrIt) { + rrIt->retryDelay = RETRY_DELAY_MIN; + this->advertise(rrIt); + } +} + +void +Readvertise::afterDestinationUnavailable() +{ + for (auto rrIt = m_rrs.begin(); rrIt != m_rrs.end();) { + if (rrIt->nRibRoutes > 0) { + rrIt->retryEvt.cancel(); // stop retrying or refreshing + ++rrIt; + } + else { + rrIt = m_rrs.erase(rrIt); // assume withdraw has completed + } + } +} + +void +Readvertise::advertise(ReadvertisedRouteContainer::iterator rrIt) +{ + BOOST_ASSERT(rrIt->nRibRoutes > 0); + + if (!m_destination->isAvailable()) { + NFD_LOG_DEBUG("advertise " << rrIt->prefix << " destination-unavailable"); + return; + } + + m_destination->advertise(*rrIt, + [this, rrIt] { + NFD_LOG_DEBUG("advertise " << rrIt->prefix << " success"); + rrIt->retryDelay = RETRY_DELAY_MIN; + rrIt->retryEvt = scheduler::schedule(randomizeTimer(m_policy->getRefreshInterval()), + bind(&Readvertise::advertise, this, rrIt)); + }, + [this, rrIt] (const std::string& msg) { + NFD_LOG_DEBUG("advertise " << rrIt->prefix << " failure " << msg); + rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2); + rrIt->retryEvt = scheduler::schedule(randomizeTimer(rrIt->retryDelay), + bind(&Readvertise::advertise, this, rrIt)); + }); +} + +void +Readvertise::withdraw(ReadvertisedRouteContainer::iterator rrIt) +{ + BOOST_ASSERT(rrIt->nRibRoutes == 0); + + if (!m_destination->isAvailable()) { + NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " destination-unavailable"); + m_rrs.erase(rrIt); + return; + } + + m_destination->withdraw(*rrIt, + [this, rrIt] { + NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " success"); + m_rrs.erase(rrIt); + }, + [this, rrIt] (const std::string& msg) { + NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " failure " << msg); + rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2); + rrIt->retryEvt = scheduler::schedule(randomizeTimer(rrIt->retryDelay), + bind(&Readvertise::withdraw, this, rrIt)); + }); +} + +} // namespace rib +} // namespace nfd diff --git a/rib/readvertise/readvertise.hpp b/rib/readvertise/readvertise.hpp new file mode 100644 index 00000000..13a6e593 --- /dev/null +++ b/rib/readvertise/readvertise.hpp @@ -0,0 +1,93 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (c) 2014-2017, Regents of the University of California, + * Arizona Board of Regents, + * Colorado State University, + * University Pierre & Marie Curie, Sorbonne University, + * Washington University in St. Louis, + * Beijing Institute of Technology, + * The University of Memphis. + * + * This file is part of NFD (Named Data Networking Forwarding Daemon). + * See AUTHORS.md for complete list of NFD authors and contributors. + * + * NFD is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * NFD, e.g., in COPYING.md file. If not, see . + */ + +#ifndef NFD_RIB_READVERTISE_READVERTISE_HPP +#define NFD_RIB_READVERTISE_READVERTISE_HPP + +#include "readvertise-destination.hpp" +#include "readvertise-policy.hpp" +#include "readvertised-route.hpp" +#include "../rib.hpp" +#include "core/scheduler.hpp" + +namespace nfd { +namespace rib { + +/** \brief readvertise a subset of routes to a destination according to a policy + * + * The Readvertise class allows RIB routes to be readvertised to a destination such as a routing + * protocol daemon or another NFD-RIB. It monitors the RIB for route additions and removals, + * asks the ReadvertisePolicy to make decision on whether to readvertise each new route and what + * prefix to readvertise as, and invokes a ReadvertiseDestination to send the commands. + */ +class Readvertise : noncopyable +{ + +public: + Readvertise(Rib& rib, + unique_ptr policy, + unique_ptr destination); + +private: + void + afterAddRoute(const RibRouteRef& ribRoute); + + void + beforeRemoveRoute(const RibRouteRef& ribRoute); + + void + afterDestinationAvailable(); + + void + afterDestinationUnavailable(); + + void + advertise(ReadvertisedRouteContainer::iterator rrIt); + + void + withdraw(ReadvertisedRouteContainer::iterator rrIt); + +private: + /** \brief maps from RIB route to readvertised route derived from RIB route(s) + */ + using RouteRrIndex = std::map; + + static const time::milliseconds RETRY_DELAY_MIN; + static const time::milliseconds RETRY_DELAY_MAX; + + unique_ptr m_policy; + unique_ptr m_destination; + + ReadvertisedRouteContainer m_rrs; + RouteRrIndex m_routeToRr; + + signal::ScopedConnection m_addRouteConn; + signal::ScopedConnection m_removeRouteConn; +}; + +} // namespace rib +} // namespace nfd + +#endif // NFD_RIB_READVERTISE_READVERTISE_HPP diff --git a/rib/rib.cpp b/rib/rib.cpp index 422de9f5..a2a27fb7 100644 --- a/rib/rib.cpp +++ b/rib/rib.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /** - * Copyright (c) 2014-2016, Regents of the University of California, + * Copyright (c) 2014-2017, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -33,6 +33,13 @@ NFD_LOG_INIT("Rib"); namespace nfd { namespace rib { +bool +operator<(const RibRouteRef& lhs, const RibRouteRef& rhs) +{ + return std::tie(lhs.entry->getName(), lhs.route->faceId, lhs.route->origin) < + std::tie(rhs.entry->getName(), rhs.route->faceId, rhs.route->origin); +} + static inline bool sortRoutes(const Route& lhs, const Route& rhs) { diff --git a/rib/rib.hpp b/rib/rib.hpp index 70554e68..78ea7668 100644 --- a/rib/rib.hpp +++ b/rib/rib.hpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /** - * Copyright (c) 2014-2016, Regents of the University of California, + * Copyright (c) 2014-2017, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -46,6 +46,9 @@ struct RibRouteRef RibEntry::const_iterator route; }; +bool +operator<(const RibRouteRef& lhs, const RibRouteRef& rhs); + /** \brief represents the Routing Information Base The Routing Information Base contains a collection of Routes, each diff --git a/tests/rib/readvertise/readvertise.t.cpp b/tests/rib/readvertise/readvertise.t.cpp new file mode 100644 index 00000000..14949b80 --- /dev/null +++ b/tests/rib/readvertise/readvertise.t.cpp @@ -0,0 +1,305 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (c) 2014-2017, Regents of the University of California, + * Arizona Board of Regents, + * Colorado State University, + * University Pierre & Marie Curie, Sorbonne University, + * Washington University in St. Louis, + * Beijing Institute of Technology, + * The University of Memphis. + * + * This file is part of NFD (Named Data Networking Forwarding Daemon). + * See AUTHORS.md for complete list of NFD authors and contributors. + * + * NFD is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * NFD, e.g., in COPYING.md file. If not, see . + */ + +#include "rib/readvertise/readvertise.hpp" + +#include "tests/test-common.hpp" +#include "tests/identity-management-fixture.hpp" +#include +#include +#include +#include + +namespace nfd { +namespace rib { +namespace tests { + +using namespace nfd::tests; + +class DummyReadvertisePolicy : public ReadvertisePolicy +{ +public: + ndn::optional + handleNewRoute(const RibRouteRef& route) const override + { + return this->decision; + } + + time::milliseconds + getRefreshInterval() const override + { + return time::seconds(60); + } + +public: + ndn::optional decision; +}; + +class DummyReadvertiseDestination : public ReadvertiseDestination +{ +public: + DummyReadvertiseDestination() + { + this->setAvailability(true); + } + + void + advertise(const ReadvertisedRoute& rr, + std::function successCb, + std::function failureCb) override + { + advertiseHistory.push_back({time::steady_clock::now(), rr.prefix}); + if (shouldSucceed) { + successCb(); + } + else { + failureCb("FAKE-FAILURE"); + } + } + + void + withdraw(const ReadvertisedRoute& rr, + std::function successCb, + std::function failureCb) override + { + withdrawHistory.push_back({time::steady_clock::now(), rr.prefix}); + if (shouldSucceed) { + successCb(); + } + else { + failureCb("FAKE-FAILURE"); + } + } + + void + setAvailability(bool isAvailable) + { + this->ReadvertiseDestination::setAvailability(isAvailable); + } + +public: + struct HistoryEntry + { + time::steady_clock::TimePoint timestamp; + Name prefix; + }; + + bool shouldSucceed = true; + std::vector advertiseHistory; + std::vector withdrawHistory; +}; + +class ReadvertiseFixture : public IdentityManagementTimeFixture +{ +public: + ReadvertiseFixture() + : face(getGlobalIoService(), m_keyChain, {false, false}) + , controller(face, m_keyChain) + { + auto policyUnique = make_unique(); + policy = policyUnique.get(); + auto destinationUnique = make_unique(); + destination = destinationUnique.get(); + readvertise.reset(new Readvertise(rib, std::move(policyUnique), std::move(destinationUnique))); + } + + void + insertRoute(const Name& prefix, uint64_t faceId, ndn::nfd::RouteOrigin origin) + { + Route route; + route.faceId = faceId; + route.origin = origin; + rib.insert(prefix, route); + this->advanceClocks(time::milliseconds(6)); + } + + void + eraseRoute(const Name& prefix, uint64_t faceId, ndn::nfd::RouteOrigin origin) + { + Route route; + route.faceId = faceId; + route.origin = origin; + rib.erase(prefix, route); + this->advanceClocks(time::milliseconds(6)); + } + + void + setDestinationAvailability(bool isAvailable) + { + destination->setAvailability(isAvailable); + this->advanceClocks(time::milliseconds(6)); + } + +public: + ndn::KeyChain m_keyChain; + ndn::util::DummyClientFace face; + ndn::nfd::Controller controller; + DummyReadvertisePolicy* policy; + DummyReadvertiseDestination* destination; + Rib rib; + unique_ptr readvertise; +}; + +BOOST_AUTO_TEST_SUITE(Readvertise) +BOOST_FIXTURE_TEST_SUITE(TestReadvertise, ReadvertiseFixture) + +BOOST_AUTO_TEST_CASE(AddRemoveRoute) +{ + policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()}; + + // advertising /A + this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1); + BOOST_CHECK_EQUAL(destination->advertiseHistory.at(0).prefix, "/A"); + + // /A is already advertised + this->insertRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1); + + // refresh every 60 seconds + destination->advertiseHistory.clear(); + this->advanceClocks(time::seconds(61), 5); + BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 5); + + // /A is still needed by /A/2 route + this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); + + // withdrawing /A + this->eraseRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 1); + BOOST_CHECK_EQUAL(destination->withdrawHistory.at(0).prefix, "/A"); +} + +BOOST_AUTO_TEST_CASE(NoAdvertise) +{ + policy->decision = ndn::nullopt; + + this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + this->insertRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + this->eraseRoute("/A/2", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + + BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0); + BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); +} + +BOOST_AUTO_TEST_CASE(DestinationAvailability) +{ + this->setDestinationAvailability(false); + + policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()}; + this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + policy->decision = ReadvertiseAction{"/B", ndn::security::SigningInfo()}; + this->insertRoute("/B/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0); + + this->setDestinationAvailability(true); + std::set advertisedPrefixes; + boost::copy(destination->advertiseHistory | boost::adaptors::transformed( + [] (const DummyReadvertiseDestination::HistoryEntry& he) { return he.prefix; }), + std::inserter(advertisedPrefixes, advertisedPrefixes.end())); + std::set expectedPrefixes{"/A", "/B"}; + BOOST_CHECK_EQUAL_COLLECTIONS(advertisedPrefixes.begin(), advertisedPrefixes.end(), + expectedPrefixes.begin(), expectedPrefixes.end()); + destination->advertiseHistory.clear(); + + this->setDestinationAvailability(false); + this->eraseRoute("/B/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); + + this->setDestinationAvailability(true); + BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); + BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 1); + BOOST_CHECK_EQUAL(destination->advertiseHistory.at(0).prefix, "/A"); +} + +BOOST_AUTO_TEST_CASE(AdvertiseRetryInterval) +{ + destination->shouldSucceed = false; + + policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()}; + this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + + this->advanceClocks(time::seconds(10), time::seconds(3600)); + BOOST_REQUIRE_GT(destination->advertiseHistory.size(), 2); + + // destination->advertise keeps failing, so interval should increase + using FloatInterval = time::duration; + FloatInterval initialInterval = destination->advertiseHistory[1].timestamp - + destination->advertiseHistory[0].timestamp; + FloatInterval lastInterval = initialInterval; + for (size_t i = 2; i < destination->advertiseHistory.size(); ++i) { + FloatInterval interval = destination->advertiseHistory[i].timestamp - + destination->advertiseHistory[i - 1].timestamp; + BOOST_CHECK_CLOSE(interval.count(), (lastInterval * 2.0).count(), 10.0); + lastInterval = interval; + } + + destination->shouldSucceed = true; + this->advanceClocks(time::seconds(3600)); + destination->advertiseHistory.clear(); + + // destination->advertise has succeeded, retry interval should reset to initial + destination->shouldSucceed = false; + this->advanceClocks(time::seconds(10), time::seconds(300)); + BOOST_REQUIRE_GE(destination->advertiseHistory.size(), 2); + FloatInterval restartInterval = destination->advertiseHistory[1].timestamp - + destination->advertiseHistory[0].timestamp; + BOOST_CHECK_CLOSE(restartInterval.count(), initialInterval.count(), 10.0); +} + +BOOST_AUTO_TEST_CASE(ChangeDuringRetry) +{ + destination->shouldSucceed = false; + policy->decision = ReadvertiseAction{"/A", ndn::security::SigningInfo()}; + this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + this->advanceClocks(time::seconds(10), time::seconds(300)); + BOOST_CHECK_GT(destination->advertiseHistory.size(), 0); + BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); + + // destination->advertise has been failing, but we want to withdraw + destination->advertiseHistory.clear(); + destination->withdrawHistory.clear(); + this->eraseRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + this->advanceClocks(time::seconds(10), time::seconds(300)); + BOOST_CHECK_EQUAL(destination->advertiseHistory.size(), 0); // don't try to advertise + BOOST_CHECK_GT(destination->withdrawHistory.size(), 0); // try to withdraw + + // destination->withdraw has been failing, but we want to advertise + destination->advertiseHistory.clear(); + destination->withdrawHistory.clear(); + this->insertRoute("/A/1", 1, ndn::nfd::ROUTE_ORIGIN_CLIENT); + this->advanceClocks(time::seconds(10), time::seconds(300)); + BOOST_CHECK_GT(destination->advertiseHistory.size(), 0); // try to advertise + BOOST_CHECK_EQUAL(destination->withdrawHistory.size(), 0); // don't try to withdraw +} + +BOOST_AUTO_TEST_SUITE_END() // TestReadvertise +BOOST_AUTO_TEST_SUITE_END() // Readvertise + +} // namespace tests +} // namespace rib +} // namespace nfd