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 | /* |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/global.hpp" |
| 28 | #include "common/logger.hpp" |
Davide Pesavento | b8bd5ee | 2019-02-03 22:53:40 -0500 | [diff] [blame] | 29 | |
| 30 | #include <ndn-cxx/util/random.hpp> |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace nfd::rib { |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 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 | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 36 | constexpr time::milliseconds RETRY_DELAY_MIN = 50_s; |
| 37 | constexpr time::milliseconds RETRY_DELAY_MAX = 1_h; |
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 | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 47 | Readvertise::Readvertise(Rib& rib, |
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 | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 50 | : m_policy(std::move(policy)) |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 51 | , m_destination(std::move(destination)) |
| 52 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 53 | m_addRouteConn = rib.afterAddRoute.connect([this] (const auto& r) { this->afterAddRoute(r); }); |
| 54 | m_removeRouteConn = rib.beforeRemoveRoute.connect([this] (const auto& r) { this->beforeRemoveRoute(r); }); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 55 | |
| 56 | m_destination->afterAvailabilityChange.connect([this] (bool isAvailable) { |
| 57 | if (isAvailable) { |
| 58 | this->afterDestinationAvailable(); |
| 59 | } |
| 60 | else { |
| 61 | this->afterDestinationUnavailable(); |
| 62 | } |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | Readvertise::afterAddRoute(const RibRouteRef& ribRoute) |
| 68 | { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 69 | std::optional<ReadvertiseAction> action = m_policy->handleNewRoute(ribRoute); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 70 | if (!action) { |
| 71 | NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << |
| 72 | ',' << ribRoute.route->origin << ") not-readvertising"); |
| 73 | return; |
| 74 | } |
| 75 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 76 | auto [rrIt, isNewRr] = m_rrs.emplace(action->prefix); |
| 77 | if (!isNewRr && rrIt->signer != action->signer) { |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 78 | NFD_LOG_WARN("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 79 | ',' << ribRoute.route->origin << ") readvertising-as " << action->prefix << |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 80 | " old-signer " << rrIt->signer << " new-signer " << action->signer); |
| 81 | } |
| 82 | rrIt->signer = action->signer; |
| 83 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 84 | bool isNewInMap = m_routeToRr.try_emplace(ribRoute, rrIt).second; |
| 85 | BOOST_VERIFY(isNewInMap); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 86 | |
| 87 | if (rrIt->nRibRoutes++ > 0) { |
| 88 | NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << |
| 89 | ',' << ribRoute.route->origin << ") already-readvertised-as " << action->prefix); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << |
| 94 | ',' << ribRoute.route->origin << ") readvertising-as " << action->prefix << |
| 95 | " signer " << action->signer); |
| 96 | rrIt->retryDelay = RETRY_DELAY_MIN; |
| 97 | this->advertise(rrIt); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | Readvertise::beforeRemoveRoute(const RibRouteRef& ribRoute) |
| 102 | { |
| 103 | auto indexIt = m_routeToRr.find(ribRoute); |
| 104 | if (indexIt == m_routeToRr.end()) { |
| 105 | NFD_LOG_DEBUG("remove-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << |
| 106 | ',' << ribRoute.route->origin << ") not-readvertised"); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | auto rrIt = indexIt->second; |
| 111 | m_routeToRr.erase(indexIt); |
| 112 | |
| 113 | if (--rrIt->nRibRoutes > 0) { |
| 114 | NFD_LOG_DEBUG("remove-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << |
| 115 | ',' << ribRoute.route->origin << ") needed-by " << rrIt->nRibRoutes); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | rrIt->retryDelay = RETRY_DELAY_MIN; |
| 120 | this->withdraw(rrIt); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | Readvertise::afterDestinationAvailable() |
| 125 | { |
| 126 | for (auto rrIt = m_rrs.begin(); rrIt != m_rrs.end(); ++rrIt) { |
| 127 | rrIt->retryDelay = RETRY_DELAY_MIN; |
| 128 | this->advertise(rrIt); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | Readvertise::afterDestinationUnavailable() |
| 134 | { |
| 135 | for (auto rrIt = m_rrs.begin(); rrIt != m_rrs.end();) { |
| 136 | if (rrIt->nRibRoutes > 0) { |
| 137 | rrIt->retryEvt.cancel(); // stop retrying or refreshing |
| 138 | ++rrIt; |
| 139 | } |
| 140 | else { |
| 141 | rrIt = m_rrs.erase(rrIt); // assume withdraw has completed |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | Readvertise::advertise(ReadvertisedRouteContainer::iterator rrIt) |
| 148 | { |
| 149 | BOOST_ASSERT(rrIt->nRibRoutes > 0); |
| 150 | |
| 151 | if (!m_destination->isAvailable()) { |
| 152 | NFD_LOG_DEBUG("advertise " << rrIt->prefix << " destination-unavailable"); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | m_destination->advertise(*rrIt, |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 157 | [=] { |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 158 | NFD_LOG_DEBUG("advertise " << rrIt->prefix << " success"); |
| 159 | rrIt->retryDelay = RETRY_DELAY_MIN; |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 160 | rrIt->retryEvt = getScheduler().schedule(randomizeTimer(m_policy->getRefreshInterval()), |
| 161 | [=] { advertise(rrIt); }); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 162 | }, |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 163 | [=] (const std::string& msg) { |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 164 | NFD_LOG_DEBUG("advertise " << rrIt->prefix << " failure " << msg); |
| 165 | rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2); |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 166 | rrIt->retryEvt = getScheduler().schedule(randomizeTimer(rrIt->retryDelay), |
| 167 | [=] { advertise(rrIt); }); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 168 | }); |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | Readvertise::withdraw(ReadvertisedRouteContainer::iterator rrIt) |
| 173 | { |
| 174 | BOOST_ASSERT(rrIt->nRibRoutes == 0); |
| 175 | |
| 176 | if (!m_destination->isAvailable()) { |
| 177 | NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " destination-unavailable"); |
| 178 | m_rrs.erase(rrIt); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | m_destination->withdraw(*rrIt, |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 183 | [=] { |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 184 | NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " success"); |
| 185 | m_rrs.erase(rrIt); |
| 186 | }, |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 187 | [=] (const std::string& msg) { |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 188 | NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " failure " << msg); |
| 189 | rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2); |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 190 | rrIt->retryEvt = getScheduler().schedule(randomizeTimer(rrIt->retryDelay), |
| 191 | [=] { withdraw(rrIt); }); |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 192 | }); |
| 193 | } |
| 194 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 195 | } // namespace nfd::rib |