blob: 4477ebc4d282bd95e724320982e1388693d8a01f [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 {
39namespace rib {
40
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
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000049RibManager::RibManager(Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040050 ndn::nfd::Controller& nfdController, Dispatcher& dispatcher,
51 ndn::util::Scheduler& scheduler)
Junxiao Shifde3f542016-07-10 19:54:53 +000052 : ManagerBase(dispatcher, MGMT_MODULE_NAME)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000053 , m_rib(rib)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000054 , m_keyChain(keyChain)
Junxiao Shif4cfed12018-08-22 23:26:29 +000055 , m_nfdController(nfdController)
56 , m_dispatcher(dispatcher)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040057 , m_scheduler(scheduler)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000058 , m_faceMonitor(face)
59 , m_localhostValidator(face)
60 , m_localhopValidator(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 Pesaventoe1bdc082018-10-11 21:20:23 -0400140 auto event = m_scheduler.scheduleEvent(*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
145 m_registeredFaces.insert(route.faceId);
146
147 RibUpdate update;
148 update.setAction(RibUpdate::REGISTER)
149 .setName(name)
150 .setRoute(route);
151 beginRibUpdate(update, done);
Yanbiao Licf0db022016-01-29 00:54:25 -0800152}
153
154void
Junxiao Shi52009042018-09-10 12:33:56 +0000155RibManager::beginRemoveRoute(const Name& name, const Route& route,
156 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800157{
Junxiao Shi52009042018-09-10 12:33:56 +0000158 NFD_LOG_INFO("Removing route " << name << " nexthop=" << route.faceId <<
159 " origin=" << route.origin);
Yanbiao Licf0db022016-01-29 00:54:25 -0800160
Junxiao Shi52009042018-09-10 12:33:56 +0000161 RibUpdate update;
162 update.setAction(RibUpdate::UNREGISTER)
163 .setName(name)
164 .setRoute(route);
165 beginRibUpdate(update, done);
166}
167
168void
169RibManager::beginRibUpdate(const RibUpdate& update,
170 const std::function<void(RibUpdateResult)>& done)
171{
172 m_rib.beginApplyUpdate(update,
173 [=] {
174 NFD_LOG_DEBUG("RIB update succeeded for " << update);
175 done(RibUpdateResult::OK);
176 },
177 [=] (uint32_t code, const std::string& error) {
178 NFD_LOG_DEBUG("RIB update failed for " << update << " (" << code << " " << error << ")");
179
180 // Since the FIB rejected the update, clean up invalid routes
181 scheduleActiveFaceFetch(1_s);
182
183 done(RibUpdateResult::ERROR);
184 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800185}
186
187void
Yanbiao Licf0db022016-01-29 00:54:25 -0800188RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700189{
Junxiao Shi52009042018-09-10 12:33:56 +0000190 // add FIB nexthop
Yanbiao Licf0db022016-01-29 00:54:25 -0800191 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000192 ControlParameters().setName(Name(topPrefix).append(MGMT_MODULE_NAME))
193 .setFaceId(0),
194 [=] (const ControlParameters& res) {
195 NFD_LOG_DEBUG("Successfully registered " << topPrefix << " with NFD");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700196
Junxiao Shi52009042018-09-10 12:33:56 +0000197 // Routes must be inserted into the RIB so route flags can be applied
198 Route route;
199 route.faceId = res.getFaceId();
200 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
201 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
202
203 m_rib.insert(topPrefix, route);
204
205 m_registeredFaces.insert(route.faceId);
206 },
207 [=] (const ControlResponse& res) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500208 NDN_THROW(Error("Cannot add FIB entry " + topPrefix.toUri() + " (" +
209 to_string(res.getCode()) + " " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000210 });
211
212 // add top prefix to the dispatcher without prefix registration
Junxiao Shif4cfed12018-08-22 23:26:29 +0000213 m_dispatcher.addTopPrefix(topPrefix, false);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700214}
215
216void
Yanbiao Licf0db022016-01-29 00:54:25 -0800217RibManager::registerEntry(const Name& topPrefix, const Interest& interest,
218 ControlParameters parameters,
219 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700220{
Junxiao Shi75306352018-02-01 21:59:44 +0000221 if (parameters.getName().size() > FIB_MAX_DEPTH) {
222 done(ControlResponse(414, "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) +
223 " components"));
224 return;
225 }
226
Yanbiao Licf0db022016-01-29 00:54:25 -0800227 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600228
229 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800230 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700231
Vince Lehman218be0a2015-01-15 17:25:20 -0600232 Route route;
233 route.faceId = parameters.getFaceId();
234 route.origin = parameters.getOrigin();
235 route.cost = parameters.getCost();
236 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500237
Junxiao Shi52009042018-09-10 12:33:56 +0000238 optional<time::nanoseconds> expires;
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700239 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000240 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Junxiao Shi52009042018-09-10 12:33:56 +0000241 expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod());
Vince Lehman76c751c2014-11-18 17:36:38 -0600242 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700243
Junxiao Shi52009042018-09-10 12:33:56 +0000244 beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {});
Vince Lehman281ded72014-08-21 12:17:08 -0500245}
246
247void
Yanbiao Licf0db022016-01-29 00:54:25 -0800248RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest,
249 ControlParameters parameters,
250 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700251{
Yanbiao Licf0db022016-01-29 00:54:25 -0800252 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600253
254 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800255 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700256
Vince Lehman218be0a2015-01-15 17:25:20 -0600257 Route route;
258 route.faceId = parameters.getFaceId();
259 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700260
Junxiao Shi52009042018-09-10 12:33:56 +0000261 beginRemoveRoute(parameters.getName(), route, [] (RibUpdateResult) {});
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700262}
263
264void
Yanbiao Licf0db022016-01-29 00:54:25 -0800265RibManager::listEntries(const Name& topPrefix, const Interest& interest,
266 ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700267{
Junxiao Shi3f21e582017-05-29 15:26:32 +0000268 auto now = time::steady_clock::now();
269 for (const auto& kv : m_rib) {
270 const RibEntry& entry = *kv.second;
271 ndn::nfd::RibEntry item;
272 item.setName(entry.getName());
273 for (const Route& route : entry.getRoutes()) {
274 ndn::nfd::Route r;
275 r.setFaceId(route.faceId);
276 r.setOrigin(route.origin);
277 r.setCost(route.cost);
278 r.setFlags(route.flags);
279 if (route.expires) {
280 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now));
281 }
282 item.addRoute(r);
283 }
284 context.append(item.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600285 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800286 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700287}
288
289void
Yanbiao Licf0db022016-01-29 00:54:25 -0800290RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700291{
Yanbiao Licf0db022016-01-29 00:54:25 -0800292 bool isSelfRegistration = (parameters.getFaceId() == 0);
293 if (isSelfRegistration) {
294 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
295 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
296 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
297 // and is initialized synchronously with IncomingFaceId field enabled.
298 BOOST_ASSERT(incomingFaceIdTag != nullptr);
299 parameters.setFaceId(*incomingFaceIdTag);
Vince Lehman76c751c2014-11-18 17:36:38 -0600300 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700301}
302
Junxiao Shi21738402016-08-19 19:48:00 +0000303ndn::mgmt::Authorization
304RibManager::makeAuthorization(const std::string& verb)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700305{
Junxiao Shi21738402016-08-19 19:48:00 +0000306 return [this] (const Name& prefix, const Interest& interest,
307 const ndn::mgmt::ControlParameters* params,
308 const ndn::mgmt::AcceptContinuation& accept,
309 const ndn::mgmt::RejectContinuation& reject) {
310 BOOST_ASSERT(params != nullptr);
311 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000312 BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700313
Junxiao Shif4cfed12018-08-22 23:26:29 +0000314 ndn::ValidatorConfig& validator = prefix == LOCALHOST_TOP_PREFIX ?
Junxiao Shi21738402016-08-19 19:48:00 +0000315 m_localhostValidator : m_localhopValidator;
316 validator.validate(interest,
317 bind([&interest, this, accept] { extractRequester(interest, accept); }),
318 bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); }));
319 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500320}
321
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000322std::ostream&
323operator<<(std::ostream& os, RibManager::SlAnnounceResult res)
324{
325 switch (res) {
326 case RibManager::SlAnnounceResult::OK:
327 return os << "OK";
328 case RibManager::SlAnnounceResult::ERROR:
329 return os << "ERROR";
330 case RibManager::SlAnnounceResult::VALIDATION_FAILURE:
331 return os << "VALIDATION_FAILURE";
332 case RibManager::SlAnnounceResult::EXPIRED:
333 return os << "EXPIRED";
334 case RibManager::SlAnnounceResult::NOT_FOUND:
335 return os << "NOT_FOUND";
336 }
337 BOOST_ASSERT_MSG(false, "bad SlAnnounceResult");
338 return os;
339}
340
341RibManager::SlAnnounceResult
342RibManager::getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r)
343{
344 switch (r) {
345 case RibUpdateResult::OK:
346 return SlAnnounceResult::OK;
347 case RibUpdateResult::ERROR:
348 return SlAnnounceResult::ERROR;
349 case RibUpdateResult::EXPIRED:
350 return SlAnnounceResult::EXPIRED;
351 default:
352 BOOST_ASSERT(false);
353 return SlAnnounceResult::ERROR;
354 }
355}
356
357void
358RibManager::slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId,
359 time::milliseconds maxLifetime, const SlAnnounceCallback& cb)
360{
361 BOOST_ASSERT(pa.getData());
362
363 if (!m_isLocalhopEnabled) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700364 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000365 ": localhop_security unconfigured");
366 cb(SlAnnounceResult::VALIDATION_FAILURE);
367 return;
368 }
369
370 m_localhopValidator.validate(*pa.getData(),
371 [=] (const Data&) {
372 Route route(pa, faceId);
373 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
374 beginAddRoute(pa.getAnnouncedName(), route, nullopt,
375 [=] (RibUpdateResult ribRes) {
376 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700377 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << ": " << res);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000378 cb(res);
379 });
380 },
381 [=] (const Data&, ndn::security::v2::ValidationError err) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700382 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000383 " validation error: " << err);
384 cb(SlAnnounceResult::VALIDATION_FAILURE);
385 }
386 );
387}
388
389void
390RibManager::slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
391 const SlAnnounceCallback& cb)
392{
393 Route routeQuery;
394 routeQuery.faceId = faceId;
395 routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN;
Teng Lianga4e6ec32018-10-21 09:25:00 -0700396 Route* oldRoute = m_rib.findLongestPrefix(name, routeQuery);
397
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000398 if (oldRoute == nullptr || !oldRoute->announcement) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700399 NFD_LOG_DEBUG("slRenew " << name << " " << faceId << ": not found");
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000400 return cb(SlAnnounceResult::NOT_FOUND);
401 }
Teng Lianga4e6ec32018-10-21 09:25:00 -0700402 Name routeName = oldRoute->announcement->getAnnouncedName();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000403
404 Route route = *oldRoute;
405 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700406 beginAddRoute(routeName, route, nullopt,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000407 [=] (RibUpdateResult ribRes) {
408 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700409 NFD_LOG_INFO("slRenew " << name << " " << faceId << ": " << res << " " << routeName);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000410 cb(res);
411 });
412}
413
414void
415RibManager::slFindAnn(const Name& name, const SlFindAnnCallback& cb) const
416{
417 shared_ptr<RibEntry> entry;
418 auto exactMatch = m_rib.find(name);
419 if (exactMatch != m_rib.end()) {
420 entry = exactMatch->second;
421 }
422 else {
423 entry = m_rib.findParent(name);
424 }
425 if (entry == nullptr) {
426 return cb(nullopt);
427 }
428
429 auto pa = entry->getPrefixAnnouncement();
430 pa.toData(m_keyChain);
431 cb(pa);
432}
433
Vince Lehman26b215c2014-08-17 15:00:41 -0500434void
Vince Lehmancd613c52014-07-30 14:34:49 -0500435RibManager::fetchActiveFaces()
436{
437 NFD_LOG_DEBUG("Fetching active faces");
438
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700439 m_nfdController.fetch<ndn::nfd::FaceDataset>(
440 bind(&RibManager::removeInvalidFaces, this, _1),
441 bind(&RibManager::onFetchActiveFacesFailure, this, _1, _2),
442 ndn::nfd::CommandOptions());
Vince Lehmancd613c52014-07-30 14:34:49 -0500443}
444
445void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700446RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason)
Vince Lehmancd613c52014-07-30 14:34:49 -0500447{
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700448 NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason);
Yanbiao Licf0db022016-01-29 00:54:25 -0800449 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
450}
451
452void
453RibManager::onFaceDestroyedEvent(uint64_t faceId)
454{
455 m_rib.beginRemoveFace(faceId);
456 m_registeredFaces.erase(faceId);
457}
458
459void
460RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
461{
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400462 m_activeFaceFetchEvent = m_scheduler.scheduleEvent(timeToWait, [this] { fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800463}
464
465void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700466RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500467{
Vince Lehman26b215c2014-08-17 15:00:41 -0500468 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500469
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700470 FaceIdSet activeFaceIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500471 for (const auto& faceStatus : activeFaces) {
472 activeFaceIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700473 }
474
Vince Lehman26b215c2014-08-17 15:00:41 -0500475 // Look for face IDs that were registered but not active to find missed
476 // face destroyed events
Davide Pesaventod396b612017-02-20 22:11:50 -0500477 for (auto faceId : m_registeredFaces) {
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700478 if (activeFaceIds.count(faceId) == 0) {
Vince Lehman76c751c2014-11-18 17:36:38 -0600479 NFD_LOG_DEBUG("Removing invalid face ID: " << faceId);
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400480 m_scheduler.scheduleEvent(0_ns, [this, faceId] { this->onFaceDestroyedEvent(faceId); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500481 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600482 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500483
484 // Reschedule the check for future clean up
485 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500486}
487
488void
Davide Pesaventod396b612017-02-20 22:11:50 -0500489RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500490{
Yanbiao Licf0db022016-01-29 00:54:25 -0800491 NFD_LOG_TRACE("onNotification: " << notification);
492
493 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
494 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400495 m_scheduler.scheduleEvent(0_ns, [this, id = notification.getFaceId()] { onFaceDestroyedEvent(id); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800496 }
497}
498
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700499} // namespace rib
500} // namespace nfd