blob: ded568d632aa9874a7da6329b253c6cecc04964e [file] [log] [blame]
Junxiao Shi89c0ea02017-03-06 19:52:05 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento87fc0f82018-04-11 23:43:51 -04002/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi89c0ea02017-03-06 19:52:05 +00004 * 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 Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/global.hpp"
28#include "common/logger.hpp"
Davide Pesaventob8bd5ee2019-02-03 22:53:40 -050029
30#include <ndn-cxx/util/random.hpp>
Junxiao Shi89c0ea02017-03-06 19:52:05 +000031
32namespace nfd {
33namespace rib {
34
Davide Pesaventoa3148082018-04-12 18:21:54 -040035NFD_LOG_INIT(Readvertise);
Junxiao Shi89c0ea02017-03-06 19:52:05 +000036
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040037constexpr time::milliseconds RETRY_DELAY_MIN = 50_s;
38constexpr time::milliseconds RETRY_DELAY_MAX = 1_h;
Junxiao Shi89c0ea02017-03-06 19:52:05 +000039
40static time::milliseconds
41randomizeTimer(time::milliseconds baseTimer)
42{
Davide Pesaventob8bd5ee2019-02-03 22:53:40 -050043 std::uniform_int_distribution<> dist(-5, 5);
44 auto newTime = baseTimer + time::milliseconds(dist(ndn::random::getRandomNumberEngine()));
Davide Pesaventoe4b22382018-06-10 14:37:24 -040045 return std::max(newTime, 0_ms);
Junxiao Shi89c0ea02017-03-06 19:52:05 +000046}
47
Davide Pesavento0a71dd32019-03-17 20:36:18 -040048Readvertise::Readvertise(Rib& rib,
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040049 unique_ptr<ReadvertisePolicy> policy,
Junxiao Shi89c0ea02017-03-06 19:52:05 +000050 unique_ptr<ReadvertiseDestination> destination)
Davide Pesavento0a71dd32019-03-17 20:36:18 -040051 : m_policy(std::move(policy))
Junxiao Shi89c0ea02017-03-06 19:52:05 +000052 , m_destination(std::move(destination))
53{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040054 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 Shi89c0ea02017-03-06 19:52:05 +000056
57 m_destination->afterAvailabilityChange.connect([this] (bool isAvailable) {
58 if (isAvailable) {
59 this->afterDestinationAvailable();
60 }
61 else {
62 this->afterDestinationUnavailable();
63 }
64 });
65}
66
67void
68Readvertise::afterAddRoute(const RibRouteRef& ribRoute)
69{
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040070 std::optional<ReadvertiseAction> action = m_policy->handleNewRoute(ribRoute);
Junxiao Shi89c0ea02017-03-06 19:52:05 +000071 if (!action) {
72 NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
73 ',' << ribRoute.route->origin << ") not-readvertising");
74 return;
75 }
76
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040077 auto [rrIt, isNewRr] = m_rrs.emplace(action->prefix);
78 if (!isNewRr && rrIt->signer != action->signer) {
Junxiao Shi89c0ea02017-03-06 19:52:05 +000079 NFD_LOG_WARN("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040080 ',' << ribRoute.route->origin << ") readvertising-as " << action->prefix <<
Junxiao Shi89c0ea02017-03-06 19:52:05 +000081 " old-signer " << rrIt->signer << " new-signer " << action->signer);
82 }
83 rrIt->signer = action->signer;
84
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040085 bool isNewInMap = m_routeToRr.try_emplace(ribRoute, rrIt).second;
86 BOOST_VERIFY(isNewInMap);
Junxiao Shi89c0ea02017-03-06 19:52:05 +000087
88 if (rrIt->nRibRoutes++ > 0) {
89 NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
90 ',' << ribRoute.route->origin << ") already-readvertised-as " << action->prefix);
91 return;
92 }
93
94 NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
95 ',' << ribRoute.route->origin << ") readvertising-as " << action->prefix <<
96 " signer " << action->signer);
97 rrIt->retryDelay = RETRY_DELAY_MIN;
98 this->advertise(rrIt);
99}
100
101void
102Readvertise::beforeRemoveRoute(const RibRouteRef& ribRoute)
103{
104 auto indexIt = m_routeToRr.find(ribRoute);
105 if (indexIt == m_routeToRr.end()) {
106 NFD_LOG_DEBUG("remove-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
107 ',' << ribRoute.route->origin << ") not-readvertised");
108 return;
109 }
110
111 auto rrIt = indexIt->second;
112 m_routeToRr.erase(indexIt);
113
114 if (--rrIt->nRibRoutes > 0) {
115 NFD_LOG_DEBUG("remove-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
116 ',' << ribRoute.route->origin << ") needed-by " << rrIt->nRibRoutes);
117 return;
118 }
119
120 rrIt->retryDelay = RETRY_DELAY_MIN;
121 this->withdraw(rrIt);
122}
123
124void
125Readvertise::afterDestinationAvailable()
126{
127 for (auto rrIt = m_rrs.begin(); rrIt != m_rrs.end(); ++rrIt) {
128 rrIt->retryDelay = RETRY_DELAY_MIN;
129 this->advertise(rrIt);
130 }
131}
132
133void
134Readvertise::afterDestinationUnavailable()
135{
136 for (auto rrIt = m_rrs.begin(); rrIt != m_rrs.end();) {
137 if (rrIt->nRibRoutes > 0) {
138 rrIt->retryEvt.cancel(); // stop retrying or refreshing
139 ++rrIt;
140 }
141 else {
142 rrIt = m_rrs.erase(rrIt); // assume withdraw has completed
143 }
144 }
145}
146
147void
148Readvertise::advertise(ReadvertisedRouteContainer::iterator rrIt)
149{
150 BOOST_ASSERT(rrIt->nRibRoutes > 0);
151
152 if (!m_destination->isAvailable()) {
153 NFD_LOG_DEBUG("advertise " << rrIt->prefix << " destination-unavailable");
154 return;
155 }
156
157 m_destination->advertise(*rrIt,
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400158 [=] {
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000159 NFD_LOG_DEBUG("advertise " << rrIt->prefix << " success");
160 rrIt->retryDelay = RETRY_DELAY_MIN;
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400161 rrIt->retryEvt = getScheduler().schedule(randomizeTimer(m_policy->getRefreshInterval()),
162 [=] { advertise(rrIt); });
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000163 },
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400164 [=] (const std::string& msg) {
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000165 NFD_LOG_DEBUG("advertise " << rrIt->prefix << " failure " << msg);
166 rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2);
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400167 rrIt->retryEvt = getScheduler().schedule(randomizeTimer(rrIt->retryDelay),
168 [=] { advertise(rrIt); });
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000169 });
170}
171
172void
173Readvertise::withdraw(ReadvertisedRouteContainer::iterator rrIt)
174{
175 BOOST_ASSERT(rrIt->nRibRoutes == 0);
176
177 if (!m_destination->isAvailable()) {
178 NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " destination-unavailable");
179 m_rrs.erase(rrIt);
180 return;
181 }
182
183 m_destination->withdraw(*rrIt,
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400184 [=] {
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000185 NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " success");
186 m_rrs.erase(rrIt);
187 },
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400188 [=] (const std::string& msg) {
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000189 NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " failure " << msg);
190 rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2);
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400191 rrIt->retryEvt = getScheduler().schedule(randomizeTimer(rrIt->retryDelay),
192 [=] { withdraw(rrIt); });
Junxiao Shi89c0ea02017-03-06 19:52:05 +0000193 });
194}
195
196} // namespace rib
197} // namespace nfd