blob: 8db60ab4be861a6e48edbe4cb9ef9385b237ddc5 [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi75306352018-02-01 21:59:44 +00002/*
Junxiao Shifeddc3c2019-01-17 19:06:00 +00003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi1e46be32015-01-08 20:18:05 -07004 * 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.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070010 *
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/>.
Vince12e49462014-06-09 13:29:32 -050024 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070025
26#include "rib-manager.hpp"
Nick Gordon9fcf1232017-03-10 22:30:20 +000027
Junxiao Shi75306352018-02-01 21:59:44 +000028#include "core/fib-max-depth.hpp"
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070029#include "core/logger.hpp"
Davide Pesavento78ddcab2019-02-28 22:00:03 -050030#include "rib/rib.hpp"
Nick Gordon9fcf1232017-03-10 22:30:20 +000031
Junxiao Shicbc8e942016-09-06 03:17:45 +000032#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000033#include <ndn-cxx/mgmt/nfd/face-status.hpp>
34#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070035
36namespace nfd {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050037
38using rib::RibUpdate;
39using rib::Route;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070040
Davide Pesaventoa3148082018-04-12 18:21:54 -040041NFD_LOG_INIT(RibManager);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070042
Junxiao Shif4cfed12018-08-22 23:26:29 +000043static const std::string MGMT_MODULE_NAME = "rib";
44static const Name LOCALHOST_TOP_PREFIX = "/localhost/nfd";
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040045static const time::seconds ACTIVE_FACE_FETCH_INTERVAL = 5_min;
Vince Lehman26b215c2014-08-17 15:00:41 -050046
Yanbiao Lif48d0802018-06-01 03:00:02 -070047const Name RibManager::LOCALHOP_TOP_PREFIX = "/localhop/nfd";
48
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050049RibManager::RibManager(rib::Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
Davide Pesavento3dade002019-03-19 11:29:56 -060050 ndn::nfd::Controller& nfdController, Dispatcher& dispatcher, Scheduler& scheduler)
Davide Pesavento78ddcab2019-02-28 22:00:03 -050051 : ManagerBase(MGMT_MODULE_NAME, dispatcher)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000052 , m_rib(rib)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000053 , m_keyChain(keyChain)
Junxiao Shif4cfed12018-08-22 23:26:29 +000054 , m_nfdController(nfdController)
55 , m_dispatcher(dispatcher)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040056 , m_scheduler(scheduler)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000057 , m_faceMonitor(face)
58 , m_localhostValidator(face)
59 , m_localhopValidator(face)
Junxiao Shif4cfed12018-08-22 23:26:29 +000060 , m_isLocalhopEnabled(false)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070061{
Yanbiao Licf0db022016-01-29 00:54:25 -080062 registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
63 bind(&RibManager::registerEntry, this, _2, _3, _4, _5));
64 registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister",
65 bind(&RibManager::unregisterEntry, this, _2, _3, _4, _5));
66
67 registerStatusDatasetHandler("list", bind(&RibManager::listEntries, this, _1, _2, _3));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070068}
69
Junxiao Shif4cfed12018-08-22 23:26:29 +000070void
71RibManager::applyLocalhostConfig(const ConfigSection& section, const std::string& filename)
72{
73 m_localhostValidator.load(section, filename);
74}
75
76void
77RibManager::enableLocalhop(const ConfigSection& section, const std::string& filename)
78{
79 m_localhopValidator.load(section, filename);
80 m_isLocalhopEnabled = true;
81}
82
83void
84RibManager::disableLocalhop()
85{
86 m_isLocalhopEnabled = false;
87}
Vince Lehman26b215c2014-08-17 15:00:41 -050088
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070089void
90RibManager::registerWithNfd()
91{
Junxiao Shif4cfed12018-08-22 23:26:29 +000092 registerTopPrefix(LOCALHOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070093
Junxiao Shia3295742014-05-16 22:40:10 -070094 if (m_isLocalhopEnabled) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000095 registerTopPrefix(LOCALHOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -070096 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070097
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070098 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shifbf78342015-01-23 14:46:41 -070099 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -0700100 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500101
Vince Lehman26b215c2014-08-17 15:00:41 -0500102 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700103}
104
105void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000106RibManager::enableLocalFields()
Yanbiao Licf0db022016-01-29 00:54:25 -0800107{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000108 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000109 ControlParameters().setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true),
Davide Pesavento19779d82019-02-14 13:40:04 -0500110 [] (const ControlParameters&) {
Junxiao Shi52009042018-09-10 12:33:56 +0000111 NFD_LOG_DEBUG("Local fields enabled");
112 },
113 [] (const ControlResponse& res) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500114 NDN_THROW(Error("Couldn't enable local fields (" + to_string(res.getCode()) +
115 " " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000116 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800117}
118
119void
Junxiao Shi52009042018-09-10 12:33:56 +0000120RibManager::beginAddRoute(const Name& name, Route route, optional<time::nanoseconds> expires,
121 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800122{
Junxiao Shi52009042018-09-10 12:33:56 +0000123 if (expires) {
Junxiao Shi52009042018-09-10 12:33:56 +0000124 route.expires = time::steady_clock::now() + *expires;
125 }
126 else if (route.expires) {
127 expires = *route.expires - time::steady_clock::now();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000128 }
129
130 if (expires && *expires <= 0_s) {
131 m_rib.onRouteExpiration(name, route);
132 return done(RibUpdateResult::EXPIRED);
Junxiao Shi52009042018-09-10 12:33:56 +0000133 }
134
135 NFD_LOG_INFO("Adding route " << name << " nexthop=" << route.faceId <<
136 " origin=" << route.origin << " cost=" << route.cost);
137
138 if (expires) {
Davide Pesavento3dade002019-03-19 11:29:56 -0600139 auto event = m_scheduler.schedule(*expires, [=] { m_rib.onRouteExpiration(name, route); });
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000140 route.setExpirationEvent(event);
Junxiao Shi52009042018-09-10 12:33:56 +0000141 NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires);
142 }
143
144 m_registeredFaces.insert(route.faceId);
145
146 RibUpdate update;
147 update.setAction(RibUpdate::REGISTER)
148 .setName(name)
149 .setRoute(route);
150 beginRibUpdate(update, done);
Yanbiao Licf0db022016-01-29 00:54:25 -0800151}
152
153void
Junxiao Shi52009042018-09-10 12:33:56 +0000154RibManager::beginRemoveRoute(const Name& name, const Route& route,
155 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800156{
Junxiao Shi52009042018-09-10 12:33:56 +0000157 NFD_LOG_INFO("Removing route " << name << " nexthop=" << route.faceId <<
158 " origin=" << route.origin);
Yanbiao Licf0db022016-01-29 00:54:25 -0800159
Junxiao Shi52009042018-09-10 12:33:56 +0000160 RibUpdate update;
161 update.setAction(RibUpdate::UNREGISTER)
162 .setName(name)
163 .setRoute(route);
164 beginRibUpdate(update, done);
165}
166
167void
168RibManager::beginRibUpdate(const RibUpdate& update,
169 const std::function<void(RibUpdateResult)>& done)
170{
171 m_rib.beginApplyUpdate(update,
172 [=] {
173 NFD_LOG_DEBUG("RIB update succeeded for " << update);
174 done(RibUpdateResult::OK);
175 },
176 [=] (uint32_t code, const std::string& error) {
177 NFD_LOG_DEBUG("RIB update failed for " << update << " (" << code << " " << error << ")");
178
179 // Since the FIB rejected the update, clean up invalid routes
180 scheduleActiveFaceFetch(1_s);
181
182 done(RibUpdateResult::ERROR);
183 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800184}
185
186void
Yanbiao Licf0db022016-01-29 00:54:25 -0800187RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700188{
Junxiao Shi52009042018-09-10 12:33:56 +0000189 // add FIB nexthop
Yanbiao Licf0db022016-01-29 00:54:25 -0800190 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000191 ControlParameters().setName(Name(topPrefix).append(MGMT_MODULE_NAME))
192 .setFaceId(0),
193 [=] (const ControlParameters& res) {
194 NFD_LOG_DEBUG("Successfully registered " << topPrefix << " with NFD");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700195
Junxiao Shi52009042018-09-10 12:33:56 +0000196 // Routes must be inserted into the RIB so route flags can be applied
197 Route route;
198 route.faceId = res.getFaceId();
199 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
200 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
201
202 m_rib.insert(topPrefix, route);
203
204 m_registeredFaces.insert(route.faceId);
205 },
206 [=] (const ControlResponse& res) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500207 NDN_THROW(Error("Cannot add FIB entry " + topPrefix.toUri() + " (" +
208 to_string(res.getCode()) + " " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000209 });
210
211 // add top prefix to the dispatcher without prefix registration
Junxiao Shif4cfed12018-08-22 23:26:29 +0000212 m_dispatcher.addTopPrefix(topPrefix, false);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700213}
214
215void
Yanbiao Licf0db022016-01-29 00:54:25 -0800216RibManager::registerEntry(const Name& topPrefix, const Interest& interest,
217 ControlParameters parameters,
218 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700219{
Junxiao Shi75306352018-02-01 21:59:44 +0000220 if (parameters.getName().size() > FIB_MAX_DEPTH) {
221 done(ControlResponse(414, "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) +
222 " components"));
223 return;
224 }
225
Yanbiao Licf0db022016-01-29 00:54:25 -0800226 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600227
228 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800229 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700230
Vince Lehman218be0a2015-01-15 17:25:20 -0600231 Route route;
232 route.faceId = parameters.getFaceId();
233 route.origin = parameters.getOrigin();
234 route.cost = parameters.getCost();
235 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500236
Junxiao Shi52009042018-09-10 12:33:56 +0000237 optional<time::nanoseconds> expires;
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700238 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000239 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Junxiao Shi52009042018-09-10 12:33:56 +0000240 expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod());
Vince Lehman76c751c2014-11-18 17:36:38 -0600241 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700242
Junxiao Shi52009042018-09-10 12:33:56 +0000243 beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {});
Vince Lehman281ded72014-08-21 12:17:08 -0500244}
245
246void
Yanbiao Licf0db022016-01-29 00:54:25 -0800247RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest,
248 ControlParameters parameters,
249 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700250{
Yanbiao Licf0db022016-01-29 00:54:25 -0800251 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600252
253 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800254 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700255
Vince Lehman218be0a2015-01-15 17:25:20 -0600256 Route route;
257 route.faceId = parameters.getFaceId();
258 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700259
Junxiao Shi52009042018-09-10 12:33:56 +0000260 beginRemoveRoute(parameters.getName(), route, [] (RibUpdateResult) {});
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700261}
262
263void
Yanbiao Licf0db022016-01-29 00:54:25 -0800264RibManager::listEntries(const Name& topPrefix, const Interest& interest,
265 ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700266{
Junxiao Shi3f21e582017-05-29 15:26:32 +0000267 auto now = time::steady_clock::now();
268 for (const auto& kv : m_rib) {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500269 const rib::RibEntry& entry = *kv.second;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000270 ndn::nfd::RibEntry item;
271 item.setName(entry.getName());
272 for (const Route& route : entry.getRoutes()) {
273 ndn::nfd::Route r;
274 r.setFaceId(route.faceId);
275 r.setOrigin(route.origin);
276 r.setCost(route.cost);
277 r.setFlags(route.flags);
278 if (route.expires) {
279 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now));
280 }
281 item.addRoute(r);
282 }
283 context.append(item.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600284 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800285 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700286}
287
288void
Yanbiao Licf0db022016-01-29 00:54:25 -0800289RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700290{
Yanbiao Licf0db022016-01-29 00:54:25 -0800291 bool isSelfRegistration = (parameters.getFaceId() == 0);
292 if (isSelfRegistration) {
293 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
294 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
295 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
296 // and is initialized synchronously with IncomingFaceId field enabled.
297 BOOST_ASSERT(incomingFaceIdTag != nullptr);
298 parameters.setFaceId(*incomingFaceIdTag);
Vince Lehman76c751c2014-11-18 17:36:38 -0600299 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700300}
301
Junxiao Shi21738402016-08-19 19:48:00 +0000302ndn::mgmt::Authorization
303RibManager::makeAuthorization(const std::string& verb)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700304{
Junxiao Shi21738402016-08-19 19:48:00 +0000305 return [this] (const Name& prefix, const Interest& interest,
306 const ndn::mgmt::ControlParameters* params,
307 const ndn::mgmt::AcceptContinuation& accept,
308 const ndn::mgmt::RejectContinuation& reject) {
309 BOOST_ASSERT(params != nullptr);
310 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000311 BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700312
Junxiao Shif4cfed12018-08-22 23:26:29 +0000313 ndn::ValidatorConfig& validator = prefix == LOCALHOST_TOP_PREFIX ?
Junxiao Shi21738402016-08-19 19:48:00 +0000314 m_localhostValidator : m_localhopValidator;
315 validator.validate(interest,
316 bind([&interest, this, accept] { extractRequester(interest, accept); }),
317 bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); }));
318 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500319}
320
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000321std::ostream&
322operator<<(std::ostream& os, RibManager::SlAnnounceResult res)
323{
324 switch (res) {
325 case RibManager::SlAnnounceResult::OK:
326 return os << "OK";
327 case RibManager::SlAnnounceResult::ERROR:
328 return os << "ERROR";
329 case RibManager::SlAnnounceResult::VALIDATION_FAILURE:
330 return os << "VALIDATION_FAILURE";
331 case RibManager::SlAnnounceResult::EXPIRED:
332 return os << "EXPIRED";
333 case RibManager::SlAnnounceResult::NOT_FOUND:
334 return os << "NOT_FOUND";
335 }
336 BOOST_ASSERT_MSG(false, "bad SlAnnounceResult");
337 return os;
338}
339
340RibManager::SlAnnounceResult
341RibManager::getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r)
342{
343 switch (r) {
344 case RibUpdateResult::OK:
345 return SlAnnounceResult::OK;
346 case RibUpdateResult::ERROR:
347 return SlAnnounceResult::ERROR;
348 case RibUpdateResult::EXPIRED:
349 return SlAnnounceResult::EXPIRED;
350 default:
351 BOOST_ASSERT(false);
352 return SlAnnounceResult::ERROR;
353 }
354}
355
356void
357RibManager::slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId,
358 time::milliseconds maxLifetime, const SlAnnounceCallback& cb)
359{
360 BOOST_ASSERT(pa.getData());
361
362 if (!m_isLocalhopEnabled) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700363 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000364 ": localhop_security unconfigured");
365 cb(SlAnnounceResult::VALIDATION_FAILURE);
366 return;
367 }
368
369 m_localhopValidator.validate(*pa.getData(),
370 [=] (const Data&) {
371 Route route(pa, faceId);
372 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
373 beginAddRoute(pa.getAnnouncedName(), route, nullopt,
374 [=] (RibUpdateResult ribRes) {
375 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700376 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << ": " << res);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000377 cb(res);
378 });
379 },
380 [=] (const Data&, ndn::security::v2::ValidationError err) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700381 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000382 " validation error: " << err);
383 cb(SlAnnounceResult::VALIDATION_FAILURE);
384 }
385 );
386}
387
388void
389RibManager::slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
390 const SlAnnounceCallback& cb)
391{
392 Route routeQuery;
393 routeQuery.faceId = faceId;
394 routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN;
Teng Lianga4e6ec32018-10-21 09:25:00 -0700395 Route* oldRoute = m_rib.findLongestPrefix(name, routeQuery);
396
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000397 if (oldRoute == nullptr || !oldRoute->announcement) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700398 NFD_LOG_DEBUG("slRenew " << name << " " << faceId << ": not found");
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000399 return cb(SlAnnounceResult::NOT_FOUND);
400 }
Teng Lianga4e6ec32018-10-21 09:25:00 -0700401 Name routeName = oldRoute->announcement->getAnnouncedName();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000402
403 Route route = *oldRoute;
404 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700405 beginAddRoute(routeName, route, nullopt,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000406 [=] (RibUpdateResult ribRes) {
407 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700408 NFD_LOG_INFO("slRenew " << name << " " << faceId << ": " << res << " " << routeName);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000409 cb(res);
410 });
411}
412
413void
414RibManager::slFindAnn(const Name& name, const SlFindAnnCallback& cb) const
415{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500416 shared_ptr<rib::RibEntry> entry;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000417 auto exactMatch = m_rib.find(name);
418 if (exactMatch != m_rib.end()) {
419 entry = exactMatch->second;
420 }
421 else {
422 entry = m_rib.findParent(name);
423 }
424 if (entry == nullptr) {
425 return cb(nullopt);
426 }
427
428 auto pa = entry->getPrefixAnnouncement();
429 pa.toData(m_keyChain);
430 cb(pa);
431}
432
Vince Lehman26b215c2014-08-17 15:00:41 -0500433void
Vince Lehmancd613c52014-07-30 14:34:49 -0500434RibManager::fetchActiveFaces()
435{
436 NFD_LOG_DEBUG("Fetching active faces");
437
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700438 m_nfdController.fetch<ndn::nfd::FaceDataset>(
439 bind(&RibManager::removeInvalidFaces, this, _1),
440 bind(&RibManager::onFetchActiveFacesFailure, this, _1, _2),
441 ndn::nfd::CommandOptions());
Vince Lehmancd613c52014-07-30 14:34:49 -0500442}
443
444void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700445RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason)
Vince Lehmancd613c52014-07-30 14:34:49 -0500446{
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700447 NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason);
Yanbiao Licf0db022016-01-29 00:54:25 -0800448 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
449}
450
451void
452RibManager::onFaceDestroyedEvent(uint64_t faceId)
453{
454 m_rib.beginRemoveFace(faceId);
455 m_registeredFaces.erase(faceId);
456}
457
458void
459RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
460{
Davide Pesavento3dade002019-03-19 11:29:56 -0600461 m_activeFaceFetchEvent = m_scheduler.schedule(timeToWait, [this] { fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800462}
463
464void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700465RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500466{
Vince Lehman26b215c2014-08-17 15:00:41 -0500467 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500468
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700469 FaceIdSet activeFaceIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500470 for (const auto& faceStatus : activeFaces) {
471 activeFaceIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700472 }
473
Vince Lehman26b215c2014-08-17 15:00:41 -0500474 // Look for face IDs that were registered but not active to find missed
475 // face destroyed events
Davide Pesaventod396b612017-02-20 22:11:50 -0500476 for (auto faceId : m_registeredFaces) {
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700477 if (activeFaceIds.count(faceId) == 0) {
Vince Lehman76c751c2014-11-18 17:36:38 -0600478 NFD_LOG_DEBUG("Removing invalid face ID: " << faceId);
Davide Pesavento3dade002019-03-19 11:29:56 -0600479 m_scheduler.schedule(0_ns, [this, faceId] { this->onFaceDestroyedEvent(faceId); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500480 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600481 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500482
483 // Reschedule the check for future clean up
484 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500485}
486
487void
Davide Pesaventod396b612017-02-20 22:11:50 -0500488RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500489{
Yanbiao Licf0db022016-01-29 00:54:25 -0800490 NFD_LOG_TRACE("onNotification: " << notification);
491
492 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
493 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
Davide Pesavento3dade002019-03-19 11:29:56 -0600494 m_scheduler.schedule(0_ns, [this, id = notification.getFaceId()] { onFaceDestroyedEvent(id); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800495 }
496}
497
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700498} // namespace nfd