Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 2 | /* |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "readvertise.hpp" |
| 27 | #include "core/logger.hpp" |
Davide Pesavento | b8bd5ee | 2019-02-03 22:53:40 -0500 | [diff] [blame] | 28 | |
| 29 | #include <ndn-cxx/util/random.hpp> |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | namespace rib { |
| 33 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 34 | NFD_LOG_INIT(Readvertise); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 35 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 36 | const time::milliseconds Readvertise::RETRY_DELAY_MIN = 50_s; |
| 37 | const time::milliseconds Readvertise::RETRY_DELAY_MAX = 3600_s; |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 38 | |
| 39 | static time::milliseconds |
| 40 | randomizeTimer(time::milliseconds baseTimer) |
| 41 | { |
Davide Pesavento | b8bd5ee | 2019-02-03 22:53:40 -0500 | [diff] [blame] | 42 | std::uniform_int_distribution<> dist(-5, 5); |
| 43 | auto newTime = baseTimer + time::milliseconds(dist(ndn::random::getRandomNumberEngine())); |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 44 | return std::max(newTime, 0_ms); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame^] | 47 | Readvertise::Readvertise(Rib& rib, Scheduler& scheduler, |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 48 | unique_ptr<ReadvertisePolicy> policy, |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 49 | unique_ptr<ReadvertiseDestination> destination) |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 50 | : m_scheduler(scheduler) |
| 51 | , m_policy(std::move(policy)) |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 52 | , m_destination(std::move(destination)) |
| 53 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 54 | m_addRouteConn = rib.afterAddRoute.connect([this] (const auto& r) { this->afterAddRoute(r); }); |
| 55 | m_removeRouteConn = rib.beforeRemoveRoute.connect([this] (const auto& r) { this->beforeRemoveRoute(r); }); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 56 | |
| 57 | m_destination->afterAvailabilityChange.connect([this] (bool isAvailable) { |
| 58 | if (isAvailable) { |
| 59 | this->afterDestinationAvailable(); |
| 60 | } |
| 61 | else { |
| 62 | this->afterDestinationUnavailable(); |
| 63 | } |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | Readvertise::afterAddRoute(const RibRouteRef& ribRoute) |
| 69 | { |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 70 | optional<ReadvertiseAction> action = m_policy->handleNewRoute(ribRoute); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 71 | if (!action) { |
| 72 | NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << |
| 73 | ',' << ribRoute.route->origin << ") not-readvertising"); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | ReadvertisedRouteContainer::iterator rrIt; |
| 78 | bool isNew = false; |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame] | 79 | std::tie(rrIt, isNew) = m_rrs.emplace(action->prefix); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 80 | |
| 81 | if (!isNew && rrIt->signer != action->signer) { |
| 82 | NFD_LOG_WARN("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << |
| 83 | ',' << ribRoute.route->origin << ") readvertising-as " << action->prefix << |
| 84 | " old-signer " << rrIt->signer << " new-signer " << action->signer); |
| 85 | } |
| 86 | rrIt->signer = action->signer; |
| 87 | |
| 88 | RouteRrIndex::iterator indexIt; |
| 89 | std::tie(indexIt, isNew) = m_routeToRr.emplace(ribRoute, rrIt); |
| 90 | BOOST_ASSERT(isNew); |
| 91 | |
| 92 | if (rrIt->nRibRoutes++ > 0) { |
| 93 | NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << |
| 94 | ',' << ribRoute.route->origin << ") already-readvertised-as " << action->prefix); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << |
| 99 | ',' << ribRoute.route->origin << ") readvertising-as " << action->prefix << |
| 100 | " signer " << action->signer); |
| 101 | rrIt->retryDelay = RETRY_DELAY_MIN; |
| 102 | this->advertise(rrIt); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | Readvertise::beforeRemoveRoute(const RibRouteRef& ribRoute) |
| 107 | { |
| 108 | auto indexIt = m_routeToRr.find(ribRoute); |
| 109 | if (indexIt == m_routeToRr.end()) { |
| 110 | NFD_LOG_DEBUG("remove-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << |
| 111 | ',' << ribRoute.route->origin << ") not-readvertised"); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | auto rrIt = indexIt->second; |
| 116 | m_routeToRr.erase(indexIt); |
| 117 | |
| 118 | if (--rrIt->nRibRoutes > 0) { |
| 119 | NFD_LOG_DEBUG("remove-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << |
| 120 | ',' << ribRoute.route->origin << ") needed-by " << rrIt->nRibRoutes); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | rrIt->retryDelay = RETRY_DELAY_MIN; |
| 125 | this->withdraw(rrIt); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | Readvertise::afterDestinationAvailable() |
| 130 | { |
| 131 | for (auto rrIt = m_rrs.begin(); rrIt != m_rrs.end(); ++rrIt) { |
| 132 | rrIt->retryDelay = RETRY_DELAY_MIN; |
| 133 | this->advertise(rrIt); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | Readvertise::afterDestinationUnavailable() |
| 139 | { |
| 140 | for (auto rrIt = m_rrs.begin(); rrIt != m_rrs.end();) { |
| 141 | if (rrIt->nRibRoutes > 0) { |
| 142 | rrIt->retryEvt.cancel(); // stop retrying or refreshing |
| 143 | ++rrIt; |
| 144 | } |
| 145 | else { |
| 146 | rrIt = m_rrs.erase(rrIt); // assume withdraw has completed |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | Readvertise::advertise(ReadvertisedRouteContainer::iterator rrIt) |
| 153 | { |
| 154 | BOOST_ASSERT(rrIt->nRibRoutes > 0); |
| 155 | |
| 156 | if (!m_destination->isAvailable()) { |
| 157 | NFD_LOG_DEBUG("advertise " << rrIt->prefix << " destination-unavailable"); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | m_destination->advertise(*rrIt, |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 162 | [=] { |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 163 | NFD_LOG_DEBUG("advertise " << rrIt->prefix << " success"); |
| 164 | rrIt->retryDelay = RETRY_DELAY_MIN; |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame^] | 165 | rrIt->retryEvt = m_scheduler.schedule(randomizeTimer(m_policy->getRefreshInterval()), |
| 166 | [=] { advertise(rrIt); }); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 167 | }, |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 168 | [=] (const std::string& msg) { |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 169 | NFD_LOG_DEBUG("advertise " << rrIt->prefix << " failure " << msg); |
| 170 | rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2); |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame^] | 171 | rrIt->retryEvt = m_scheduler.schedule(randomizeTimer(rrIt->retryDelay), |
| 172 | [=] { advertise(rrIt); }); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 173 | }); |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | Readvertise::withdraw(ReadvertisedRouteContainer::iterator rrIt) |
| 178 | { |
| 179 | BOOST_ASSERT(rrIt->nRibRoutes == 0); |
| 180 | |
| 181 | if (!m_destination->isAvailable()) { |
| 182 | NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " destination-unavailable"); |
| 183 | m_rrs.erase(rrIt); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | m_destination->withdraw(*rrIt, |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 188 | [=] { |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 189 | NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " success"); |
| 190 | m_rrs.erase(rrIt); |
| 191 | }, |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 192 | [=] (const std::string& msg) { |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 193 | NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " failure " << msg); |
| 194 | rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2); |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame^] | 195 | rrIt->retryEvt = m_scheduler.schedule(randomizeTimer(rrIt->retryDelay), |
| 196 | [=] { withdraw(rrIt); }); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 197 | }); |
| 198 | } |
| 199 | |
| 200 | } // namespace rib |
| 201 | } // namespace nfd |