blob: 81196dd39dc3f29d2ee6fd374ef2295b8e9177b1 [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
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/global.hpp"
29#include "common/logger.hpp"
Davide Pesavento78ddcab2019-02-28 22:00:03 -050030#include "rib/rib.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040031#include "table/fib.hpp"
Nick Gordon9fcf1232017-03-10 22:30:20 +000032
Junxiao Shicbc8e942016-09-06 03:17:45 +000033#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000034#include <ndn-cxx/mgmt/nfd/face-status.hpp>
35#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Zhiyi Zhanga499aa22019-09-24 15:00:40 -070036#include <ndn-cxx/security/v2/certificate-fetcher-direct-fetch.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 Pesavento0a71dd32019-03-17 20:36:18 -040052 ndn::nfd::Controller& nfdController, Dispatcher& dispatcher)
Davide Pesavento78ddcab2019-02-28 22:00:03 -050053 : ManagerBase(MGMT_MODULE_NAME, dispatcher)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000054 , m_rib(rib)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000055 , m_keyChain(keyChain)
Junxiao Shif4cfed12018-08-22 23:26:29 +000056 , m_nfdController(nfdController)
57 , m_dispatcher(dispatcher)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000058 , m_faceMonitor(face)
59 , m_localhostValidator(face)
Zhiyi Zhanga499aa22019-09-24 15:00:40 -070060 , m_localhopValidator(make_unique<ndn::security::v2::CertificateFetcherDirectFetch>(face))
Junxiao Shif4cfed12018-08-22 23:26:29 +000061 , m_isLocalhopEnabled(false)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070062{
Yanbiao Licf0db022016-01-29 00:54:25 -080063 registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
64 bind(&RibManager::registerEntry, this, _2, _3, _4, _5));
65 registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister",
66 bind(&RibManager::unregisterEntry, this, _2, _3, _4, _5));
67
68 registerStatusDatasetHandler("list", bind(&RibManager::listEntries, this, _1, _2, _3));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070069}
70
Junxiao Shif4cfed12018-08-22 23:26:29 +000071void
72RibManager::applyLocalhostConfig(const ConfigSection& section, const std::string& filename)
73{
74 m_localhostValidator.load(section, filename);
75}
76
77void
78RibManager::enableLocalhop(const ConfigSection& section, const std::string& filename)
79{
80 m_localhopValidator.load(section, filename);
81 m_isLocalhopEnabled = true;
82}
83
84void
85RibManager::disableLocalhop()
86{
87 m_isLocalhopEnabled = false;
88}
Vince Lehman26b215c2014-08-17 15:00:41 -050089
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070090void
91RibManager::registerWithNfd()
92{
Junxiao Shif4cfed12018-08-22 23:26:29 +000093 registerTopPrefix(LOCALHOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070094
Junxiao Shia3295742014-05-16 22:40:10 -070095 if (m_isLocalhopEnabled) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000096 registerTopPrefix(LOCALHOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -070097 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070098
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070099 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shifbf78342015-01-23 14:46:41 -0700100 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -0700101 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500102
Vince Lehman26b215c2014-08-17 15:00:41 -0500103 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700104}
105
106void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000107RibManager::enableLocalFields()
Yanbiao Licf0db022016-01-29 00:54:25 -0800108{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000109 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000110 ControlParameters().setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true),
Davide Pesavento19779d82019-02-14 13:40:04 -0500111 [] (const ControlParameters&) {
Junxiao Shi52009042018-09-10 12:33:56 +0000112 NFD_LOG_DEBUG("Local fields enabled");
113 },
114 [] (const ControlResponse& res) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500115 NDN_THROW(Error("Couldn't enable local fields (" + to_string(res.getCode()) +
116 " " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000117 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800118}
119
120void
Junxiao Shi52009042018-09-10 12:33:56 +0000121RibManager::beginAddRoute(const Name& name, Route route, optional<time::nanoseconds> expires,
122 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800123{
Junxiao Shi52009042018-09-10 12:33:56 +0000124 if (expires) {
Junxiao Shi52009042018-09-10 12:33:56 +0000125 route.expires = time::steady_clock::now() + *expires;
126 }
127 else if (route.expires) {
128 expires = *route.expires - time::steady_clock::now();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000129 }
130
131 if (expires && *expires <= 0_s) {
132 m_rib.onRouteExpiration(name, route);
133 return done(RibUpdateResult::EXPIRED);
Junxiao Shi52009042018-09-10 12:33:56 +0000134 }
135
136 NFD_LOG_INFO("Adding route " << name << " nexthop=" << route.faceId <<
137 " origin=" << route.origin << " cost=" << route.cost);
138
139 if (expires) {
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400140 auto event = getScheduler().schedule(*expires, [=] { m_rib.onRouteExpiration(name, route); });
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000141 route.setExpirationEvent(event);
Junxiao Shi52009042018-09-10 12:33:56 +0000142 NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires);
143 }
144
Junxiao Shi52009042018-09-10 12:33:56 +0000145 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);
Junxiao Shi52009042018-09-10 12:33:56 +0000202 },
203 [=] (const ControlResponse& res) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500204 NDN_THROW(Error("Cannot add FIB entry " + topPrefix.toUri() + " (" +
205 to_string(res.getCode()) + " " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000206 });
207
208 // add top prefix to the dispatcher without prefix registration
Junxiao Shif4cfed12018-08-22 23:26:29 +0000209 m_dispatcher.addTopPrefix(topPrefix, false);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700210}
211
212void
Yanbiao Licf0db022016-01-29 00:54:25 -0800213RibManager::registerEntry(const Name& topPrefix, const Interest& interest,
214 ControlParameters parameters,
215 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700216{
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400217 if (parameters.getName().size() > Fib::getMaxDepth()) {
218 done(ControlResponse(414, "Route prefix cannot exceed " + to_string(Fib::getMaxDepth()) +
Junxiao Shi75306352018-02-01 21:59:44 +0000219 " components"));
220 return;
221 }
222
Yanbiao Licf0db022016-01-29 00:54:25 -0800223 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600224
225 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800226 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700227
Vince Lehman218be0a2015-01-15 17:25:20 -0600228 Route route;
229 route.faceId = parameters.getFaceId();
230 route.origin = parameters.getOrigin();
231 route.cost = parameters.getCost();
232 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500233
Junxiao Shi52009042018-09-10 12:33:56 +0000234 optional<time::nanoseconds> expires;
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700235 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000236 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Junxiao Shi52009042018-09-10 12:33:56 +0000237 expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod());
Vince Lehman76c751c2014-11-18 17:36:38 -0600238 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700239
Junxiao Shi52009042018-09-10 12:33:56 +0000240 beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {});
Vince Lehman281ded72014-08-21 12:17:08 -0500241}
242
243void
Yanbiao Licf0db022016-01-29 00:54:25 -0800244RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest,
245 ControlParameters parameters,
246 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700247{
Yanbiao Licf0db022016-01-29 00:54:25 -0800248 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600249
250 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800251 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700252
Vince Lehman218be0a2015-01-15 17:25:20 -0600253 Route route;
254 route.faceId = parameters.getFaceId();
255 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700256
Junxiao Shi52009042018-09-10 12:33:56 +0000257 beginRemoveRoute(parameters.getName(), route, [] (RibUpdateResult) {});
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700258}
259
260void
Yanbiao Licf0db022016-01-29 00:54:25 -0800261RibManager::listEntries(const Name& topPrefix, const Interest& interest,
262 ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700263{
Junxiao Shi3f21e582017-05-29 15:26:32 +0000264 auto now = time::steady_clock::now();
265 for (const auto& kv : m_rib) {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500266 const rib::RibEntry& entry = *kv.second;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000267 ndn::nfd::RibEntry item;
268 item.setName(entry.getName());
269 for (const Route& route : entry.getRoutes()) {
270 ndn::nfd::Route r;
271 r.setFaceId(route.faceId);
272 r.setOrigin(route.origin);
273 r.setCost(route.cost);
274 r.setFlags(route.flags);
275 if (route.expires) {
276 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now));
277 }
278 item.addRoute(r);
279 }
280 context.append(item.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600281 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800282 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700283}
284
285void
Yanbiao Licf0db022016-01-29 00:54:25 -0800286RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700287{
Yanbiao Licf0db022016-01-29 00:54:25 -0800288 bool isSelfRegistration = (parameters.getFaceId() == 0);
289 if (isSelfRegistration) {
290 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
291 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
292 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
293 // and is initialized synchronously with IncomingFaceId field enabled.
294 BOOST_ASSERT(incomingFaceIdTag != nullptr);
295 parameters.setFaceId(*incomingFaceIdTag);
Vince Lehman76c751c2014-11-18 17:36:38 -0600296 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700297}
298
Junxiao Shi21738402016-08-19 19:48:00 +0000299ndn::mgmt::Authorization
300RibManager::makeAuthorization(const std::string& verb)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700301{
Junxiao Shi21738402016-08-19 19:48:00 +0000302 return [this] (const Name& prefix, const Interest& interest,
303 const ndn::mgmt::ControlParameters* params,
304 const ndn::mgmt::AcceptContinuation& accept,
305 const ndn::mgmt::RejectContinuation& reject) {
306 BOOST_ASSERT(params != nullptr);
307 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000308 BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700309
Junxiao Shif4cfed12018-08-22 23:26:29 +0000310 ndn::ValidatorConfig& validator = prefix == LOCALHOST_TOP_PREFIX ?
Junxiao Shi21738402016-08-19 19:48:00 +0000311 m_localhostValidator : m_localhopValidator;
312 validator.validate(interest,
313 bind([&interest, this, accept] { extractRequester(interest, accept); }),
314 bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); }));
315 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500316}
317
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000318std::ostream&
319operator<<(std::ostream& os, RibManager::SlAnnounceResult res)
320{
321 switch (res) {
322 case RibManager::SlAnnounceResult::OK:
323 return os << "OK";
324 case RibManager::SlAnnounceResult::ERROR:
325 return os << "ERROR";
326 case RibManager::SlAnnounceResult::VALIDATION_FAILURE:
327 return os << "VALIDATION_FAILURE";
328 case RibManager::SlAnnounceResult::EXPIRED:
329 return os << "EXPIRED";
330 case RibManager::SlAnnounceResult::NOT_FOUND:
331 return os << "NOT_FOUND";
332 }
333 BOOST_ASSERT_MSG(false, "bad SlAnnounceResult");
334 return os;
335}
336
337RibManager::SlAnnounceResult
338RibManager::getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r)
339{
340 switch (r) {
341 case RibUpdateResult::OK:
342 return SlAnnounceResult::OK;
343 case RibUpdateResult::ERROR:
344 return SlAnnounceResult::ERROR;
345 case RibUpdateResult::EXPIRED:
346 return SlAnnounceResult::EXPIRED;
347 default:
348 BOOST_ASSERT(false);
349 return SlAnnounceResult::ERROR;
350 }
351}
352
353void
354RibManager::slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId,
355 time::milliseconds maxLifetime, const SlAnnounceCallback& cb)
356{
357 BOOST_ASSERT(pa.getData());
358
359 if (!m_isLocalhopEnabled) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700360 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000361 ": localhop_security unconfigured");
362 cb(SlAnnounceResult::VALIDATION_FAILURE);
363 return;
364 }
365
366 m_localhopValidator.validate(*pa.getData(),
367 [=] (const Data&) {
368 Route route(pa, faceId);
369 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
370 beginAddRoute(pa.getAnnouncedName(), route, nullopt,
371 [=] (RibUpdateResult ribRes) {
372 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700373 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << ": " << res);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000374 cb(res);
375 });
376 },
377 [=] (const Data&, ndn::security::v2::ValidationError err) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700378 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000379 " validation error: " << err);
380 cb(SlAnnounceResult::VALIDATION_FAILURE);
381 }
382 );
383}
384
385void
386RibManager::slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
387 const SlAnnounceCallback& cb)
388{
389 Route routeQuery;
390 routeQuery.faceId = faceId;
391 routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN;
Teng Lianga4e6ec32018-10-21 09:25:00 -0700392 Route* oldRoute = m_rib.findLongestPrefix(name, routeQuery);
393
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000394 if (oldRoute == nullptr || !oldRoute->announcement) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700395 NFD_LOG_DEBUG("slRenew " << name << " " << faceId << ": not found");
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000396 return cb(SlAnnounceResult::NOT_FOUND);
397 }
Teng Lianga4e6ec32018-10-21 09:25:00 -0700398 Name routeName = oldRoute->announcement->getAnnouncedName();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000399
400 Route route = *oldRoute;
401 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700402 beginAddRoute(routeName, route, nullopt,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000403 [=] (RibUpdateResult ribRes) {
404 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700405 NFD_LOG_INFO("slRenew " << name << " " << faceId << ": " << res << " " << routeName);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000406 cb(res);
407 });
408}
409
410void
411RibManager::slFindAnn(const Name& name, const SlFindAnnCallback& cb) const
412{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500413 shared_ptr<rib::RibEntry> entry;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000414 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
Yanbiao Licf0db022016-01-29 00:54:25 -0800449RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
450{
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400451 m_activeFaceFetchEvent = getScheduler().schedule(timeToWait, [this] { fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800452}
453
454void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700455RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500456{
Vince Lehman26b215c2014-08-17 15:00:41 -0500457 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500458
Junxiao Shi17a70012019-06-25 10:50:32 +0000459 std::set<uint64_t> activeFaceIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500460 for (const auto& faceStatus : activeFaces) {
461 activeFaceIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700462 }
Junxiao Shi17a70012019-06-25 10:50:32 +0000463 getGlobalIoService().post([=] { m_rib.beginRemoveFailedFaces(activeFaceIds); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500464
465 // Reschedule the check for future clean up
466 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500467}
468
469void
Davide Pesaventod396b612017-02-20 22:11:50 -0500470RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500471{
Yanbiao Licf0db022016-01-29 00:54:25 -0800472 NFD_LOG_TRACE("onNotification: " << notification);
473
474 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400475 NFD_LOG_DEBUG("Received notification for destroyed FaceId " << notification.getFaceId());
Junxiao Shi17a70012019-06-25 10:50:32 +0000476 getGlobalIoService().post([this, id = notification.getFaceId()] { m_rib.beginRemoveFace(id); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800477 }
478}
479
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700480} // namespace nfd