blob: 0a73db2b706a9c3cc8ad0979c1c77b7486f249c6 [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"
Nick Gordon9fcf1232017-03-10 22:30:20 +000030
Junxiao Shicbc8e942016-09-06 03:17:45 +000031#include <ndn-cxx/lp/tags.hpp>
Nick Gordon9fcf1232017-03-10 22:30:20 +000032#include <ndn-cxx/mgmt/nfd/control-command.hpp>
Nick Gordon9fcf1232017-03-10 22:30:20 +000033#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
Junxiao Shi3f21e582017-05-29 15:26:32 +000034#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000035#include <ndn-cxx/mgmt/nfd/face-status.hpp>
36#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070037
38namespace nfd {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050039
40using rib::RibUpdate;
41using rib::Route;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070042
Davide Pesaventoa3148082018-04-12 18:21:54 -040043NFD_LOG_INIT(RibManager);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070044
Junxiao Shif4cfed12018-08-22 23:26:29 +000045static const std::string MGMT_MODULE_NAME = "rib";
46static const Name LOCALHOST_TOP_PREFIX = "/localhost/nfd";
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040047static const time::seconds ACTIVE_FACE_FETCH_INTERVAL = 5_min;
Vince Lehman26b215c2014-08-17 15:00:41 -050048
Yanbiao Lif48d0802018-06-01 03:00:02 -070049const Name RibManager::LOCALHOP_TOP_PREFIX = "/localhop/nfd";
50
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050051RibManager::RibManager(rib::Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040052 ndn::nfd::Controller& nfdController, Dispatcher& dispatcher,
53 ndn::util::Scheduler& scheduler)
Junxiao Shifde3f542016-07-10 19:54:53 +000054 : ManagerBase(dispatcher, MGMT_MODULE_NAME)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000055 , m_rib(rib)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000056 , m_keyChain(keyChain)
Junxiao Shif4cfed12018-08-22 23:26:29 +000057 , m_nfdController(nfdController)
58 , m_dispatcher(dispatcher)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040059 , m_scheduler(scheduler)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000060 , m_faceMonitor(face)
61 , m_localhostValidator(face)
62 , m_localhopValidator(face)
Junxiao Shif4cfed12018-08-22 23:26:29 +000063 , m_isLocalhopEnabled(false)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070064{
Yanbiao Licf0db022016-01-29 00:54:25 -080065 registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
66 bind(&RibManager::registerEntry, this, _2, _3, _4, _5));
67 registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister",
68 bind(&RibManager::unregisterEntry, this, _2, _3, _4, _5));
69
70 registerStatusDatasetHandler("list", bind(&RibManager::listEntries, this, _1, _2, _3));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070071}
72
Junxiao Shif4cfed12018-08-22 23:26:29 +000073void
74RibManager::applyLocalhostConfig(const ConfigSection& section, const std::string& filename)
75{
76 m_localhostValidator.load(section, filename);
77}
78
79void
80RibManager::enableLocalhop(const ConfigSection& section, const std::string& filename)
81{
82 m_localhopValidator.load(section, filename);
83 m_isLocalhopEnabled = true;
84}
85
86void
87RibManager::disableLocalhop()
88{
89 m_isLocalhopEnabled = false;
90}
Vince Lehman26b215c2014-08-17 15:00:41 -050091
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070092void
93RibManager::registerWithNfd()
94{
Junxiao Shif4cfed12018-08-22 23:26:29 +000095 registerTopPrefix(LOCALHOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070096
Junxiao Shia3295742014-05-16 22:40:10 -070097 if (m_isLocalhopEnabled) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000098 registerTopPrefix(LOCALHOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -070099 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700100
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700101 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shifbf78342015-01-23 14:46:41 -0700102 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -0700103 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500104
Vince Lehman26b215c2014-08-17 15:00:41 -0500105 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700106}
107
108void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000109RibManager::enableLocalFields()
Yanbiao Licf0db022016-01-29 00:54:25 -0800110{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000111 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000112 ControlParameters().setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true),
Davide Pesavento19779d82019-02-14 13:40:04 -0500113 [] (const ControlParameters&) {
Junxiao Shi52009042018-09-10 12:33:56 +0000114 NFD_LOG_DEBUG("Local fields enabled");
115 },
116 [] (const ControlResponse& res) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500117 NDN_THROW(Error("Couldn't enable local fields (" + to_string(res.getCode()) +
118 " " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000119 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800120}
121
122void
Junxiao Shi52009042018-09-10 12:33:56 +0000123RibManager::beginAddRoute(const Name& name, Route route, optional<time::nanoseconds> expires,
124 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800125{
Junxiao Shi52009042018-09-10 12:33:56 +0000126 if (expires) {
Junxiao Shi52009042018-09-10 12:33:56 +0000127 route.expires = time::steady_clock::now() + *expires;
128 }
129 else if (route.expires) {
130 expires = *route.expires - time::steady_clock::now();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000131 }
132
133 if (expires && *expires <= 0_s) {
134 m_rib.onRouteExpiration(name, route);
135 return done(RibUpdateResult::EXPIRED);
Junxiao Shi52009042018-09-10 12:33:56 +0000136 }
137
138 NFD_LOG_INFO("Adding route " << name << " nexthop=" << route.faceId <<
139 " origin=" << route.origin << " cost=" << route.cost);
140
141 if (expires) {
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400142 auto event = m_scheduler.scheduleEvent(*expires, [=] { m_rib.onRouteExpiration(name, route); });
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000143 route.setExpirationEvent(event);
Junxiao Shi52009042018-09-10 12:33:56 +0000144 NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires);
145 }
146
147 m_registeredFaces.insert(route.faceId);
148
149 RibUpdate update;
150 update.setAction(RibUpdate::REGISTER)
151 .setName(name)
152 .setRoute(route);
153 beginRibUpdate(update, done);
Yanbiao Licf0db022016-01-29 00:54:25 -0800154}
155
156void
Junxiao Shi52009042018-09-10 12:33:56 +0000157RibManager::beginRemoveRoute(const Name& name, const Route& route,
158 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800159{
Junxiao Shi52009042018-09-10 12:33:56 +0000160 NFD_LOG_INFO("Removing route " << name << " nexthop=" << route.faceId <<
161 " origin=" << route.origin);
Yanbiao Licf0db022016-01-29 00:54:25 -0800162
Junxiao Shi52009042018-09-10 12:33:56 +0000163 RibUpdate update;
164 update.setAction(RibUpdate::UNREGISTER)
165 .setName(name)
166 .setRoute(route);
167 beginRibUpdate(update, done);
168}
169
170void
171RibManager::beginRibUpdate(const RibUpdate& update,
172 const std::function<void(RibUpdateResult)>& done)
173{
174 m_rib.beginApplyUpdate(update,
175 [=] {
176 NFD_LOG_DEBUG("RIB update succeeded for " << update);
177 done(RibUpdateResult::OK);
178 },
179 [=] (uint32_t code, const std::string& error) {
180 NFD_LOG_DEBUG("RIB update failed for " << update << " (" << code << " " << error << ")");
181
182 // Since the FIB rejected the update, clean up invalid routes
183 scheduleActiveFaceFetch(1_s);
184
185 done(RibUpdateResult::ERROR);
186 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800187}
188
189void
Yanbiao Licf0db022016-01-29 00:54:25 -0800190RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700191{
Junxiao Shi52009042018-09-10 12:33:56 +0000192 // add FIB nexthop
Yanbiao Licf0db022016-01-29 00:54:25 -0800193 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000194 ControlParameters().setName(Name(topPrefix).append(MGMT_MODULE_NAME))
195 .setFaceId(0),
196 [=] (const ControlParameters& res) {
197 NFD_LOG_DEBUG("Successfully registered " << topPrefix << " with NFD");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700198
Junxiao Shi52009042018-09-10 12:33:56 +0000199 // Routes must be inserted into the RIB so route flags can be applied
200 Route route;
201 route.faceId = res.getFaceId();
202 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
203 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
204
205 m_rib.insert(topPrefix, route);
206
207 m_registeredFaces.insert(route.faceId);
208 },
209 [=] (const ControlResponse& res) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500210 NDN_THROW(Error("Cannot add FIB entry " + topPrefix.toUri() + " (" +
211 to_string(res.getCode()) + " " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000212 });
213
214 // add top prefix to the dispatcher without prefix registration
Junxiao Shif4cfed12018-08-22 23:26:29 +0000215 m_dispatcher.addTopPrefix(topPrefix, false);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700216}
217
218void
Yanbiao Licf0db022016-01-29 00:54:25 -0800219RibManager::registerEntry(const Name& topPrefix, const Interest& interest,
220 ControlParameters parameters,
221 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700222{
Junxiao Shi75306352018-02-01 21:59:44 +0000223 if (parameters.getName().size() > FIB_MAX_DEPTH) {
224 done(ControlResponse(414, "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) +
225 " components"));
226 return;
227 }
228
Yanbiao Licf0db022016-01-29 00:54:25 -0800229 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600230
231 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800232 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700233
Vince Lehman218be0a2015-01-15 17:25:20 -0600234 Route route;
235 route.faceId = parameters.getFaceId();
236 route.origin = parameters.getOrigin();
237 route.cost = parameters.getCost();
238 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500239
Junxiao Shi52009042018-09-10 12:33:56 +0000240 optional<time::nanoseconds> expires;
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700241 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000242 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Junxiao Shi52009042018-09-10 12:33:56 +0000243 expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod());
Vince Lehman76c751c2014-11-18 17:36:38 -0600244 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700245
Junxiao Shi52009042018-09-10 12:33:56 +0000246 beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {});
Vince Lehman281ded72014-08-21 12:17:08 -0500247}
248
249void
Yanbiao Licf0db022016-01-29 00:54:25 -0800250RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest,
251 ControlParameters parameters,
252 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700253{
Yanbiao Licf0db022016-01-29 00:54:25 -0800254 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600255
256 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800257 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700258
Vince Lehman218be0a2015-01-15 17:25:20 -0600259 Route route;
260 route.faceId = parameters.getFaceId();
261 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700262
Junxiao Shi52009042018-09-10 12:33:56 +0000263 beginRemoveRoute(parameters.getName(), route, [] (RibUpdateResult) {});
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700264}
265
266void
Yanbiao Licf0db022016-01-29 00:54:25 -0800267RibManager::listEntries(const Name& topPrefix, const Interest& interest,
268 ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700269{
Junxiao Shi3f21e582017-05-29 15:26:32 +0000270 auto now = time::steady_clock::now();
271 for (const auto& kv : m_rib) {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500272 const rib::RibEntry& entry = *kv.second;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000273 ndn::nfd::RibEntry item;
274 item.setName(entry.getName());
275 for (const Route& route : entry.getRoutes()) {
276 ndn::nfd::Route r;
277 r.setFaceId(route.faceId);
278 r.setOrigin(route.origin);
279 r.setCost(route.cost);
280 r.setFlags(route.flags);
281 if (route.expires) {
282 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now));
283 }
284 item.addRoute(r);
285 }
286 context.append(item.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600287 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800288 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700289}
290
291void
Yanbiao Licf0db022016-01-29 00:54:25 -0800292RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700293{
Yanbiao Licf0db022016-01-29 00:54:25 -0800294 bool isSelfRegistration = (parameters.getFaceId() == 0);
295 if (isSelfRegistration) {
296 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
297 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
298 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
299 // and is initialized synchronously with IncomingFaceId field enabled.
300 BOOST_ASSERT(incomingFaceIdTag != nullptr);
301 parameters.setFaceId(*incomingFaceIdTag);
Vince Lehman76c751c2014-11-18 17:36:38 -0600302 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700303}
304
Junxiao Shi21738402016-08-19 19:48:00 +0000305ndn::mgmt::Authorization
306RibManager::makeAuthorization(const std::string& verb)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700307{
Junxiao Shi21738402016-08-19 19:48:00 +0000308 return [this] (const Name& prefix, const Interest& interest,
309 const ndn::mgmt::ControlParameters* params,
310 const ndn::mgmt::AcceptContinuation& accept,
311 const ndn::mgmt::RejectContinuation& reject) {
312 BOOST_ASSERT(params != nullptr);
313 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000314 BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700315
Junxiao Shif4cfed12018-08-22 23:26:29 +0000316 ndn::ValidatorConfig& validator = prefix == LOCALHOST_TOP_PREFIX ?
Junxiao Shi21738402016-08-19 19:48:00 +0000317 m_localhostValidator : m_localhopValidator;
318 validator.validate(interest,
319 bind([&interest, this, accept] { extractRequester(interest, accept); }),
320 bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); }));
321 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500322}
323
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000324std::ostream&
325operator<<(std::ostream& os, RibManager::SlAnnounceResult res)
326{
327 switch (res) {
328 case RibManager::SlAnnounceResult::OK:
329 return os << "OK";
330 case RibManager::SlAnnounceResult::ERROR:
331 return os << "ERROR";
332 case RibManager::SlAnnounceResult::VALIDATION_FAILURE:
333 return os << "VALIDATION_FAILURE";
334 case RibManager::SlAnnounceResult::EXPIRED:
335 return os << "EXPIRED";
336 case RibManager::SlAnnounceResult::NOT_FOUND:
337 return os << "NOT_FOUND";
338 }
339 BOOST_ASSERT_MSG(false, "bad SlAnnounceResult");
340 return os;
341}
342
343RibManager::SlAnnounceResult
344RibManager::getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r)
345{
346 switch (r) {
347 case RibUpdateResult::OK:
348 return SlAnnounceResult::OK;
349 case RibUpdateResult::ERROR:
350 return SlAnnounceResult::ERROR;
351 case RibUpdateResult::EXPIRED:
352 return SlAnnounceResult::EXPIRED;
353 default:
354 BOOST_ASSERT(false);
355 return SlAnnounceResult::ERROR;
356 }
357}
358
359void
360RibManager::slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId,
361 time::milliseconds maxLifetime, const SlAnnounceCallback& cb)
362{
363 BOOST_ASSERT(pa.getData());
364
365 if (!m_isLocalhopEnabled) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700366 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000367 ": localhop_security unconfigured");
368 cb(SlAnnounceResult::VALIDATION_FAILURE);
369 return;
370 }
371
372 m_localhopValidator.validate(*pa.getData(),
373 [=] (const Data&) {
374 Route route(pa, faceId);
375 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
376 beginAddRoute(pa.getAnnouncedName(), route, nullopt,
377 [=] (RibUpdateResult ribRes) {
378 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700379 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << ": " << res);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000380 cb(res);
381 });
382 },
383 [=] (const Data&, ndn::security::v2::ValidationError err) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700384 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000385 " validation error: " << err);
386 cb(SlAnnounceResult::VALIDATION_FAILURE);
387 }
388 );
389}
390
391void
392RibManager::slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
393 const SlAnnounceCallback& cb)
394{
395 Route routeQuery;
396 routeQuery.faceId = faceId;
397 routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN;
Teng Lianga4e6ec32018-10-21 09:25:00 -0700398 Route* oldRoute = m_rib.findLongestPrefix(name, routeQuery);
399
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000400 if (oldRoute == nullptr || !oldRoute->announcement) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700401 NFD_LOG_DEBUG("slRenew " << name << " " << faceId << ": not found");
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000402 return cb(SlAnnounceResult::NOT_FOUND);
403 }
Teng Lianga4e6ec32018-10-21 09:25:00 -0700404 Name routeName = oldRoute->announcement->getAnnouncedName();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000405
406 Route route = *oldRoute;
407 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700408 beginAddRoute(routeName, route, nullopt,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000409 [=] (RibUpdateResult ribRes) {
410 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700411 NFD_LOG_INFO("slRenew " << name << " " << faceId << ": " << res << " " << routeName);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000412 cb(res);
413 });
414}
415
416void
417RibManager::slFindAnn(const Name& name, const SlFindAnnCallback& cb) const
418{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500419 shared_ptr<rib::RibEntry> entry;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000420 auto exactMatch = m_rib.find(name);
421 if (exactMatch != m_rib.end()) {
422 entry = exactMatch->second;
423 }
424 else {
425 entry = m_rib.findParent(name);
426 }
427 if (entry == nullptr) {
428 return cb(nullopt);
429 }
430
431 auto pa = entry->getPrefixAnnouncement();
432 pa.toData(m_keyChain);
433 cb(pa);
434}
435
Vince Lehman26b215c2014-08-17 15:00:41 -0500436void
Vince Lehmancd613c52014-07-30 14:34:49 -0500437RibManager::fetchActiveFaces()
438{
439 NFD_LOG_DEBUG("Fetching active faces");
440
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700441 m_nfdController.fetch<ndn::nfd::FaceDataset>(
442 bind(&RibManager::removeInvalidFaces, this, _1),
443 bind(&RibManager::onFetchActiveFacesFailure, this, _1, _2),
444 ndn::nfd::CommandOptions());
Vince Lehmancd613c52014-07-30 14:34:49 -0500445}
446
447void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700448RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason)
Vince Lehmancd613c52014-07-30 14:34:49 -0500449{
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700450 NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason);
Yanbiao Licf0db022016-01-29 00:54:25 -0800451 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
452}
453
454void
455RibManager::onFaceDestroyedEvent(uint64_t faceId)
456{
457 m_rib.beginRemoveFace(faceId);
458 m_registeredFaces.erase(faceId);
459}
460
461void
462RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
463{
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400464 m_activeFaceFetchEvent = m_scheduler.scheduleEvent(timeToWait, [this] { fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800465}
466
467void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700468RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500469{
Vince Lehman26b215c2014-08-17 15:00:41 -0500470 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500471
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700472 FaceIdSet activeFaceIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500473 for (const auto& faceStatus : activeFaces) {
474 activeFaceIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700475 }
476
Vince Lehman26b215c2014-08-17 15:00:41 -0500477 // Look for face IDs that were registered but not active to find missed
478 // face destroyed events
Davide Pesaventod396b612017-02-20 22:11:50 -0500479 for (auto faceId : m_registeredFaces) {
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700480 if (activeFaceIds.count(faceId) == 0) {
Vince Lehman76c751c2014-11-18 17:36:38 -0600481 NFD_LOG_DEBUG("Removing invalid face ID: " << faceId);
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400482 m_scheduler.scheduleEvent(0_ns, [this, faceId] { this->onFaceDestroyedEvent(faceId); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500483 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600484 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500485
486 // Reschedule the check for future clean up
487 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500488}
489
490void
Davide Pesaventod396b612017-02-20 22:11:50 -0500491RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500492{
Yanbiao Licf0db022016-01-29 00:54:25 -0800493 NFD_LOG_TRACE("onNotification: " << notification);
494
495 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
496 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400497 m_scheduler.scheduleEvent(0_ns, [this, id = notification.getFaceId()] { onFaceDestroyedEvent(id); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800498 }
499}
500
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700501} // namespace nfd