blob: 009b9ad9f92d91082e833f1ac065ba8da7736097 [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/*
3 * Copyright (c) 2014-2018, 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"
Alexander Afanasyev63108c42014-07-07 19:10:47 -070030#include "core/scheduler.hpp"
Nick Gordon9fcf1232017-03-10 22:30:20 +000031
Junxiao Shicbc8e942016-09-06 03:17:45 +000032#include <ndn-cxx/lp/tags.hpp>
Nick Gordon9fcf1232017-03-10 22:30:20 +000033#include <ndn-cxx/mgmt/nfd/control-command.hpp>
Nick Gordon9fcf1232017-03-10 22:30:20 +000034#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
Junxiao Shi3f21e582017-05-29 15:26:32 +000035#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000036#include <ndn-cxx/mgmt/nfd/face-status.hpp>
37#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070038
39namespace nfd {
40namespace rib {
41
Davide Pesaventoa3148082018-04-12 18:21:54 -040042NFD_LOG_INIT(RibManager);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070043
Junxiao Shif4cfed12018-08-22 23:26:29 +000044static const std::string MGMT_MODULE_NAME = "rib";
45static const Name LOCALHOST_TOP_PREFIX = "/localhost/nfd";
46static const Name LOCALHOP_TOP_PREFIX = "/localhop/nfd";
47static const time::seconds ACTIVE_FACE_FETCH_INTERVAL = time::seconds(300);
Vince Lehman26b215c2014-08-17 15:00:41 -050048
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000049RibManager::RibManager(Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
50 ndn::nfd::Controller& nfdController, Dispatcher& dispatcher)
Junxiao Shifde3f542016-07-10 19:54:53 +000051 : ManagerBase(dispatcher, MGMT_MODULE_NAME)
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 Pesavento9f8b10e2018-08-22 08:45:37 +000056 , m_faceMonitor(face)
57 , m_localhostValidator(face)
58 , m_localhopValidator(face)
Junxiao Shif4cfed12018-08-22 23:26:29 +000059 , m_isLocalhopEnabled(false)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070060{
Yanbiao Licf0db022016-01-29 00:54:25 -080061 registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
62 bind(&RibManager::registerEntry, this, _2, _3, _4, _5));
63 registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister",
64 bind(&RibManager::unregisterEntry, this, _2, _3, _4, _5));
65
66 registerStatusDatasetHandler("list", bind(&RibManager::listEntries, this, _1, _2, _3));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070067}
68
Junxiao Shif4cfed12018-08-22 23:26:29 +000069void
70RibManager::applyLocalhostConfig(const ConfigSection& section, const std::string& filename)
71{
72 m_localhostValidator.load(section, filename);
73}
74
75void
76RibManager::enableLocalhop(const ConfigSection& section, const std::string& filename)
77{
78 m_localhopValidator.load(section, filename);
79 m_isLocalhopEnabled = true;
80}
81
82void
83RibManager::disableLocalhop()
84{
85 m_isLocalhopEnabled = false;
86}
Vince Lehman26b215c2014-08-17 15:00:41 -050087
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070088void
89RibManager::registerWithNfd()
90{
Junxiao Shif4cfed12018-08-22 23:26:29 +000091 registerTopPrefix(LOCALHOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070092
Junxiao Shia3295742014-05-16 22:40:10 -070093 if (m_isLocalhopEnabled) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000094 registerTopPrefix(LOCALHOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -070095 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070096
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070097 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shifbf78342015-01-23 14:46:41 -070098 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -070099 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500100
Vince Lehman26b215c2014-08-17 15:00:41 -0500101 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700102}
103
104void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000105RibManager::enableLocalFields()
Yanbiao Licf0db022016-01-29 00:54:25 -0800106{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000107 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000108 ControlParameters().setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true),
109 [] (const ControlParameters& res) {
110 NFD_LOG_DEBUG("Local fields enabled");
111 },
112 [] (const ControlResponse& res) {
113 BOOST_THROW_EXCEPTION(Error("Couldn't enable local fields (" + to_string(res.getCode()) +
114 " " + res.getText() + ")"));
115 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800116}
117
118void
Junxiao Shi52009042018-09-10 12:33:56 +0000119RibManager::beginAddRoute(const Name& name, Route route, optional<time::nanoseconds> expires,
120 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800121{
Junxiao Shi52009042018-09-10 12:33:56 +0000122 if (expires) {
Junxiao Shi52009042018-09-10 12:33:56 +0000123 route.expires = time::steady_clock::now() + *expires;
124 }
125 else if (route.expires) {
126 expires = *route.expires - time::steady_clock::now();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000127 }
128
129 if (expires && *expires <= 0_s) {
130 m_rib.onRouteExpiration(name, route);
131 return done(RibUpdateResult::EXPIRED);
Junxiao Shi52009042018-09-10 12:33:56 +0000132 }
133
134 NFD_LOG_INFO("Adding route " << name << " nexthop=" << route.faceId <<
135 " origin=" << route.origin << " cost=" << route.cost);
136
137 if (expires) {
138 route.setExpirationEvent(scheduler::schedule(
139 *expires, [=] { m_rib.onRouteExpiration(name, route); }));
140 NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires);
141 }
142
143 m_registeredFaces.insert(route.faceId);
144
145 RibUpdate update;
146 update.setAction(RibUpdate::REGISTER)
147 .setName(name)
148 .setRoute(route);
149 beginRibUpdate(update, done);
Yanbiao Licf0db022016-01-29 00:54:25 -0800150}
151
152void
Junxiao Shi52009042018-09-10 12:33:56 +0000153RibManager::beginRemoveRoute(const Name& name, const Route& route,
154 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800155{
Junxiao Shi52009042018-09-10 12:33:56 +0000156 NFD_LOG_INFO("Removing route " << name << " nexthop=" << route.faceId <<
157 " origin=" << route.origin);
Yanbiao Licf0db022016-01-29 00:54:25 -0800158
Junxiao Shi52009042018-09-10 12:33:56 +0000159 RibUpdate update;
160 update.setAction(RibUpdate::UNREGISTER)
161 .setName(name)
162 .setRoute(route);
163 beginRibUpdate(update, done);
164}
165
166void
167RibManager::beginRibUpdate(const RibUpdate& update,
168 const std::function<void(RibUpdateResult)>& done)
169{
170 m_rib.beginApplyUpdate(update,
171 [=] {
172 NFD_LOG_DEBUG("RIB update succeeded for " << update);
173 done(RibUpdateResult::OK);
174 },
175 [=] (uint32_t code, const std::string& error) {
176 NFD_LOG_DEBUG("RIB update failed for " << update << " (" << code << " " << error << ")");
177
178 // Since the FIB rejected the update, clean up invalid routes
179 scheduleActiveFaceFetch(1_s);
180
181 done(RibUpdateResult::ERROR);
182 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800183}
184
185void
Yanbiao Licf0db022016-01-29 00:54:25 -0800186RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700187{
Junxiao Shi52009042018-09-10 12:33:56 +0000188 // add FIB nexthop
Yanbiao Licf0db022016-01-29 00:54:25 -0800189 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000190 ControlParameters().setName(Name(topPrefix).append(MGMT_MODULE_NAME))
191 .setFaceId(0),
192 [=] (const ControlParameters& res) {
193 NFD_LOG_DEBUG("Successfully registered " << topPrefix << " with NFD");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700194
Junxiao Shi52009042018-09-10 12:33:56 +0000195 // Routes must be inserted into the RIB so route flags can be applied
196 Route route;
197 route.faceId = res.getFaceId();
198 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
199 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
200
201 m_rib.insert(topPrefix, route);
202
203 m_registeredFaces.insert(route.faceId);
204 },
205 [=] (const ControlResponse& res) {
206 BOOST_THROW_EXCEPTION(Error("Cannot add FIB entry " + topPrefix.toUri() + " (" +
207 to_string(res.getCode()) + " " + res.getText() + ")"));
208 });
209
210 // add top prefix to the dispatcher without prefix registration
Junxiao Shif4cfed12018-08-22 23:26:29 +0000211 m_dispatcher.addTopPrefix(topPrefix, false);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700212}
213
214void
Yanbiao Licf0db022016-01-29 00:54:25 -0800215RibManager::registerEntry(const Name& topPrefix, const Interest& interest,
216 ControlParameters parameters,
217 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700218{
Junxiao Shi75306352018-02-01 21:59:44 +0000219 if (parameters.getName().size() > FIB_MAX_DEPTH) {
220 done(ControlResponse(414, "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) +
221 " components"));
222 return;
223 }
224
Yanbiao Licf0db022016-01-29 00:54:25 -0800225 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600226
227 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800228 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700229
Vince Lehman218be0a2015-01-15 17:25:20 -0600230 Route route;
231 route.faceId = parameters.getFaceId();
232 route.origin = parameters.getOrigin();
233 route.cost = parameters.getCost();
234 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500235
Junxiao Shi52009042018-09-10 12:33:56 +0000236 optional<time::nanoseconds> expires;
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700237 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000238 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Junxiao Shi52009042018-09-10 12:33:56 +0000239 expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod());
Vince Lehman76c751c2014-11-18 17:36:38 -0600240 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700241
Junxiao Shi52009042018-09-10 12:33:56 +0000242 beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {});
Vince Lehman281ded72014-08-21 12:17:08 -0500243}
244
245void
Yanbiao Licf0db022016-01-29 00:54:25 -0800246RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest,
247 ControlParameters parameters,
248 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700249{
Yanbiao Licf0db022016-01-29 00:54:25 -0800250 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600251
252 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800253 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700254
Vince Lehman218be0a2015-01-15 17:25:20 -0600255 Route route;
256 route.faceId = parameters.getFaceId();
257 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700258
Junxiao Shi52009042018-09-10 12:33:56 +0000259 beginRemoveRoute(parameters.getName(), route, [] (RibUpdateResult) {});
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700260}
261
262void
Yanbiao Licf0db022016-01-29 00:54:25 -0800263RibManager::listEntries(const Name& topPrefix, const Interest& interest,
264 ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700265{
Junxiao Shi3f21e582017-05-29 15:26:32 +0000266 auto now = time::steady_clock::now();
267 for (const auto& kv : m_rib) {
268 const RibEntry& entry = *kv.second;
269 ndn::nfd::RibEntry item;
270 item.setName(entry.getName());
271 for (const Route& route : entry.getRoutes()) {
272 ndn::nfd::Route r;
273 r.setFaceId(route.faceId);
274 r.setOrigin(route.origin);
275 r.setCost(route.cost);
276 r.setFlags(route.flags);
277 if (route.expires) {
278 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now));
279 }
280 item.addRoute(r);
281 }
282 context.append(item.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600283 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800284 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700285}
286
287void
Yanbiao Licf0db022016-01-29 00:54:25 -0800288RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700289{
Yanbiao Licf0db022016-01-29 00:54:25 -0800290 bool isSelfRegistration = (parameters.getFaceId() == 0);
291 if (isSelfRegistration) {
292 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
293 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
294 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
295 // and is initialized synchronously with IncomingFaceId field enabled.
296 BOOST_ASSERT(incomingFaceIdTag != nullptr);
297 parameters.setFaceId(*incomingFaceIdTag);
Vince Lehman76c751c2014-11-18 17:36:38 -0600298 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700299}
300
Junxiao Shi21738402016-08-19 19:48:00 +0000301ndn::mgmt::Authorization
302RibManager::makeAuthorization(const std::string& verb)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700303{
Junxiao Shi21738402016-08-19 19:48:00 +0000304 return [this] (const Name& prefix, const Interest& interest,
305 const ndn::mgmt::ControlParameters* params,
306 const ndn::mgmt::AcceptContinuation& accept,
307 const ndn::mgmt::RejectContinuation& reject) {
308 BOOST_ASSERT(params != nullptr);
309 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000310 BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700311
Junxiao Shif4cfed12018-08-22 23:26:29 +0000312 ndn::ValidatorConfig& validator = prefix == LOCALHOST_TOP_PREFIX ?
Junxiao Shi21738402016-08-19 19:48:00 +0000313 m_localhostValidator : m_localhopValidator;
314 validator.validate(interest,
315 bind([&interest, this, accept] { extractRequester(interest, accept); }),
316 bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); }));
317 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500318}
319
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000320std::ostream&
321operator<<(std::ostream& os, RibManager::SlAnnounceResult res)
322{
323 switch (res) {
324 case RibManager::SlAnnounceResult::OK:
325 return os << "OK";
326 case RibManager::SlAnnounceResult::ERROR:
327 return os << "ERROR";
328 case RibManager::SlAnnounceResult::VALIDATION_FAILURE:
329 return os << "VALIDATION_FAILURE";
330 case RibManager::SlAnnounceResult::EXPIRED:
331 return os << "EXPIRED";
332 case RibManager::SlAnnounceResult::NOT_FOUND:
333 return os << "NOT_FOUND";
334 }
335 BOOST_ASSERT_MSG(false, "bad SlAnnounceResult");
336 return os;
337}
338
339RibManager::SlAnnounceResult
340RibManager::getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r)
341{
342 switch (r) {
343 case RibUpdateResult::OK:
344 return SlAnnounceResult::OK;
345 case RibUpdateResult::ERROR:
346 return SlAnnounceResult::ERROR;
347 case RibUpdateResult::EXPIRED:
348 return SlAnnounceResult::EXPIRED;
349 default:
350 BOOST_ASSERT(false);
351 return SlAnnounceResult::ERROR;
352 }
353}
354
355void
356RibManager::slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId,
357 time::milliseconds maxLifetime, const SlAnnounceCallback& cb)
358{
359 BOOST_ASSERT(pa.getData());
360
361 if (!m_isLocalhopEnabled) {
362 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << ' ' << faceId <<
363 ": localhop_security unconfigured");
364 cb(SlAnnounceResult::VALIDATION_FAILURE);
365 return;
366 }
367
368 m_localhopValidator.validate(*pa.getData(),
369 [=] (const Data&) {
370 Route route(pa, faceId);
371 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
372 beginAddRoute(pa.getAnnouncedName(), route, nullopt,
373 [=] (RibUpdateResult ribRes) {
374 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
375 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << ' ' << faceId << ": " << res);
376 cb(res);
377 });
378 },
379 [=] (const Data&, ndn::security::v2::ValidationError err) {
380 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << ' ' << faceId <<
381 " validation error: " << err);
382 cb(SlAnnounceResult::VALIDATION_FAILURE);
383 }
384 );
385}
386
387void
388RibManager::slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
389 const SlAnnounceCallback& cb)
390{
391 Route routeQuery;
392 routeQuery.faceId = faceId;
393 routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN;
394 Route* oldRoute = m_rib.find(name, routeQuery);
395 if (oldRoute == nullptr || !oldRoute->announcement) {
396 NFD_LOG_DEBUG("slRenew " << name << ' ' << faceId << ": not found");
397 return cb(SlAnnounceResult::NOT_FOUND);
398 }
399
400 Route route = *oldRoute;
401 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
402 beginAddRoute(name, route, nullopt,
403 [=] (RibUpdateResult ribRes) {
404 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
405 NFD_LOG_INFO("slRenew " << name << ' ' << faceId << ": " << res);
406 cb(res);
407 });
408}
409
410void
411RibManager::slFindAnn(const Name& name, const SlFindAnnCallback& cb) const
412{
413 shared_ptr<RibEntry> entry;
414 auto exactMatch = m_rib.find(name);
415 if (exactMatch != m_rib.end()) {
416 entry = exactMatch->second;
417 }
418 else {
419 entry = m_rib.findParent(name);
420 }
421 if (entry == nullptr) {
422 return cb(nullopt);
423 }
424
425 auto pa = entry->getPrefixAnnouncement();
426 pa.toData(m_keyChain);
427 cb(pa);
428}
429
Vince Lehman26b215c2014-08-17 15:00:41 -0500430void
Vince Lehmancd613c52014-07-30 14:34:49 -0500431RibManager::fetchActiveFaces()
432{
433 NFD_LOG_DEBUG("Fetching active faces");
434
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700435 m_nfdController.fetch<ndn::nfd::FaceDataset>(
436 bind(&RibManager::removeInvalidFaces, this, _1),
437 bind(&RibManager::onFetchActiveFacesFailure, this, _1, _2),
438 ndn::nfd::CommandOptions());
Vince Lehmancd613c52014-07-30 14:34:49 -0500439}
440
441void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700442RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason)
Vince Lehmancd613c52014-07-30 14:34:49 -0500443{
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700444 NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason);
Yanbiao Licf0db022016-01-29 00:54:25 -0800445 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
446}
447
448void
449RibManager::onFaceDestroyedEvent(uint64_t faceId)
450{
451 m_rib.beginRemoveFace(faceId);
452 m_registeredFaces.erase(faceId);
453}
454
455void
456RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
457{
Davide Pesaventod396b612017-02-20 22:11:50 -0500458 m_activeFaceFetchEvent = scheduler::schedule(timeToWait, [this] { this->fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800459}
460
461void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700462RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500463{
Vince Lehman26b215c2014-08-17 15:00:41 -0500464 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500465
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700466 FaceIdSet activeFaceIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500467 for (const auto& faceStatus : activeFaces) {
468 activeFaceIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700469 }
470
Vince Lehman26b215c2014-08-17 15:00:41 -0500471 // Look for face IDs that were registered but not active to find missed
472 // face destroyed events
Davide Pesaventod396b612017-02-20 22:11:50 -0500473 for (auto faceId : m_registeredFaces) {
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700474 if (activeFaceIds.count(faceId) == 0) {
Vince Lehman76c751c2014-11-18 17:36:38 -0600475 NFD_LOG_DEBUG("Removing invalid face ID: " << faceId);
Davide Pesaventod396b612017-02-20 22:11:50 -0500476 scheduler::schedule(time::seconds(0), [this, faceId] { this->onFaceDestroyedEvent(faceId); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500477 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600478 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500479
480 // Reschedule the check for future clean up
481 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500482}
483
484void
Davide Pesaventod396b612017-02-20 22:11:50 -0500485RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500486{
Yanbiao Licf0db022016-01-29 00:54:25 -0800487 NFD_LOG_TRACE("onNotification: " << notification);
488
489 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
490 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
491
492 scheduler::schedule(time::seconds(0),
493 bind(&RibManager::onFaceDestroyedEvent, this, notification.getFaceId()));
494 }
495}
496
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700497} // namespace rib
498} // namespace nfd