blob: 24d52b71d8f25de7b06d36f93a5124103872f59c [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/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, 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>
Alexander Afanasyeva1583702020-06-03 13:55:45 -040036#include <ndn-cxx/security/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
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040045const std::string MGMT_MODULE_NAME = "rib";
46const Name LOCALHOST_TOP_PREFIX = "/localhost/nfd";
47const time::seconds ACTIVE_FACE_FETCH_INTERVAL = 5_min;
Yanbiao Lif48d0802018-06-01 03:00:02 -070048
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050049RibManager::RibManager(rib::Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
Davide Pesavento0a71dd32019-03-17 20:36:18 -040050 ndn::nfd::Controller& nfdController, Dispatcher& dispatcher)
Davide Pesavento78ddcab2019-02-28 22:00:03 -050051 : ManagerBase(MGMT_MODULE_NAME, dispatcher)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000052 , m_rib(rib)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000053 , m_keyChain(keyChain)
Junxiao Shif4cfed12018-08-22 23:26:29 +000054 , m_nfdController(nfdController)
55 , m_dispatcher(dispatcher)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000056 , m_faceMonitor(face)
57 , m_localhostValidator(face)
Alexander Afanasyeva1583702020-06-03 13:55:45 -040058 , m_localhopValidator(make_unique<ndn::security::CertificateFetcherDirectFetch>(face))
59 , m_paValidator(make_unique<ndn::security::CertificateFetcherDirectFetch>(face))
Junxiao Shif4cfed12018-08-22 23:26:29 +000060 , m_isLocalhopEnabled(false)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070061{
Yanbiao Licf0db022016-01-29 00:54:25 -080062 registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
Davide Pesavento412c9822021-07-02 00:21:05 -040063 std::bind(&RibManager::registerEntry, this, _2, _3, _4, _5));
Yanbiao Licf0db022016-01-29 00:54:25 -080064 registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister",
Davide Pesavento412c9822021-07-02 00:21:05 -040065 std::bind(&RibManager::unregisterEntry, this, _2, _3, _4, _5));
Yanbiao Licf0db022016-01-29 00:54:25 -080066
Davide Pesavento412c9822021-07-02 00:21:05 -040067 registerStatusDatasetHandler("list", std::bind(&RibManager::listEntries, this, _1, _2, _3));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070068}
69
Junxiao Shif4cfed12018-08-22 23:26:29 +000070void
71RibManager::applyLocalhostConfig(const ConfigSection& section, const std::string& filename)
72{
73 m_localhostValidator.load(section, filename);
74}
75
76void
77RibManager::enableLocalhop(const ConfigSection& section, const std::string& filename)
78{
79 m_localhopValidator.load(section, filename);
80 m_isLocalhopEnabled = true;
81}
82
83void
84RibManager::disableLocalhop()
85{
86 m_isLocalhopEnabled = false;
87}
Vince Lehman26b215c2014-08-17 15:00:41 -050088
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070089void
Teng Liang18c2b292019-10-18 14:31:04 -070090RibManager::applyPaConfig(const ConfigSection& section, const std::string& filename)
91{
92 m_paValidator.load(section, filename);
93}
94
95void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070096RibManager::registerWithNfd()
97{
Junxiao Shif4cfed12018-08-22 23:26:29 +000098 registerTopPrefix(LOCALHOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070099
Junxiao Shia3295742014-05-16 22:40:10 -0700100 if (m_isLocalhopEnabled) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000101 registerTopPrefix(LOCALHOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -0700102 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700103
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700104 NFD_LOG_INFO("Start monitoring face create/destroy events");
Davide Pesavento412c9822021-07-02 00:21:05 -0400105 m_faceMonitor.onNotification.connect([this] (const auto& notif) { onNotification(notif); });
Junxiao Shi15b12e72014-08-09 19:56:24 -0700106 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500107
Vince Lehman26b215c2014-08-17 15:00:41 -0500108 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700109}
110
111void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000112RibManager::enableLocalFields()
Yanbiao Licf0db022016-01-29 00:54:25 -0800113{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000114 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000115 ControlParameters().setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true),
Davide Pesavento19779d82019-02-14 13:40:04 -0500116 [] (const ControlParameters&) {
Junxiao Shi52009042018-09-10 12:33:56 +0000117 NFD_LOG_DEBUG("Local fields enabled");
118 },
119 [] (const ControlResponse& res) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500120 NDN_THROW(Error("Couldn't enable local fields (" + to_string(res.getCode()) +
121 " " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000122 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800123}
124
125void
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400126RibManager::beginAddRoute(const Name& name, Route route, std::optional<time::nanoseconds> expires,
Junxiao Shi52009042018-09-10 12:33:56 +0000127 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800128{
Junxiao Shi52009042018-09-10 12:33:56 +0000129 if (expires) {
Junxiao Shi52009042018-09-10 12:33:56 +0000130 route.expires = time::steady_clock::now() + *expires;
131 }
132 else if (route.expires) {
133 expires = *route.expires - time::steady_clock::now();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000134 }
135
136 if (expires && *expires <= 0_s) {
137 m_rib.onRouteExpiration(name, route);
138 return done(RibUpdateResult::EXPIRED);
Junxiao Shi52009042018-09-10 12:33:56 +0000139 }
140
141 NFD_LOG_INFO("Adding route " << name << " nexthop=" << route.faceId <<
142 " origin=" << route.origin << " cost=" << route.cost);
143
144 if (expires) {
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400145 auto event = getScheduler().schedule(*expires, [=] { m_rib.onRouteExpiration(name, route); });
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000146 route.setExpirationEvent(event);
Junxiao Shi52009042018-09-10 12:33:56 +0000147 NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires);
148 }
149
Junxiao Shi52009042018-09-10 12:33:56 +0000150 RibUpdate update;
151 update.setAction(RibUpdate::REGISTER)
152 .setName(name)
153 .setRoute(route);
154 beginRibUpdate(update, done);
Yanbiao Licf0db022016-01-29 00:54:25 -0800155}
156
157void
Junxiao Shi52009042018-09-10 12:33:56 +0000158RibManager::beginRemoveRoute(const Name& name, const Route& route,
159 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800160{
Junxiao Shi52009042018-09-10 12:33:56 +0000161 NFD_LOG_INFO("Removing route " << name << " nexthop=" << route.faceId <<
162 " origin=" << route.origin);
Yanbiao Licf0db022016-01-29 00:54:25 -0800163
Junxiao Shi52009042018-09-10 12:33:56 +0000164 RibUpdate update;
165 update.setAction(RibUpdate::UNREGISTER)
166 .setName(name)
167 .setRoute(route);
168 beginRibUpdate(update, done);
169}
170
171void
172RibManager::beginRibUpdate(const RibUpdate& update,
173 const std::function<void(RibUpdateResult)>& done)
174{
175 m_rib.beginApplyUpdate(update,
176 [=] {
177 NFD_LOG_DEBUG("RIB update succeeded for " << update);
178 done(RibUpdateResult::OK);
179 },
180 [=] (uint32_t code, const std::string& error) {
181 NFD_LOG_DEBUG("RIB update failed for " << update << " (" << code << " " << error << ")");
182
183 // Since the FIB rejected the update, clean up invalid routes
184 scheduleActiveFaceFetch(1_s);
185
186 done(RibUpdateResult::ERROR);
187 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800188}
189
190void
Yanbiao Licf0db022016-01-29 00:54:25 -0800191RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700192{
Junxiao Shi52009042018-09-10 12:33:56 +0000193 // add FIB nexthop
Yanbiao Licf0db022016-01-29 00:54:25 -0800194 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000195 ControlParameters().setName(Name(topPrefix).append(MGMT_MODULE_NAME))
196 .setFaceId(0),
197 [=] (const ControlParameters& res) {
198 NFD_LOG_DEBUG("Successfully registered " << topPrefix << " with NFD");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700199
Junxiao Shi52009042018-09-10 12:33:56 +0000200 // Routes must be inserted into the RIB so route flags can be applied
201 Route route;
202 route.faceId = res.getFaceId();
203 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
204 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
205
206 m_rib.insert(topPrefix, route);
Junxiao Shi52009042018-09-10 12:33:56 +0000207 },
208 [=] (const ControlResponse& res) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500209 NDN_THROW(Error("Cannot add FIB entry " + topPrefix.toUri() + " (" +
210 to_string(res.getCode()) + " " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000211 });
212
213 // add top prefix to the dispatcher without prefix registration
Junxiao Shif4cfed12018-08-22 23:26:29 +0000214 m_dispatcher.addTopPrefix(topPrefix, false);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700215}
216
217void
Yanbiao Licf0db022016-01-29 00:54:25 -0800218RibManager::registerEntry(const Name& topPrefix, const Interest& interest,
219 ControlParameters parameters,
220 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700221{
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400222 if (parameters.getName().size() > Fib::getMaxDepth()) {
223 done(ControlResponse(414, "Route prefix cannot exceed " + to_string(Fib::getMaxDepth()) +
Junxiao Shi75306352018-02-01 21:59:44 +0000224 " components"));
225 return;
226 }
227
Yanbiao Licf0db022016-01-29 00:54:25 -0800228 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600229
230 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800231 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700232
Vince Lehman218be0a2015-01-15 17:25:20 -0600233 Route route;
234 route.faceId = parameters.getFaceId();
235 route.origin = parameters.getOrigin();
236 route.cost = parameters.getCost();
237 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500238
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400239 std::optional<time::nanoseconds> expires;
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700240 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000241 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Junxiao Shi52009042018-09-10 12:33:56 +0000242 expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod());
Vince Lehman76c751c2014-11-18 17:36:38 -0600243 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700244
Junxiao Shi52009042018-09-10 12:33:56 +0000245 beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {});
Vince Lehman281ded72014-08-21 12:17:08 -0500246}
247
248void
Davide Pesavento412c9822021-07-02 00:21:05 -0400249RibManager::unregisterEntry(const Name&, const Interest& interest,
Yanbiao Licf0db022016-01-29 00:54:25 -0800250 ControlParameters parameters,
251 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700252{
Yanbiao Licf0db022016-01-29 00:54:25 -0800253 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600254
255 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800256 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700257
Vince Lehman218be0a2015-01-15 17:25:20 -0600258 Route route;
259 route.faceId = parameters.getFaceId();
260 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700261
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400262 beginRemoveRoute(parameters.getName(), route, [] (auto&&...) {});
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700263}
264
265void
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400266RibManager::listEntries(const Name&, const Interest&, 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) {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500270 const rib::RibEntry& entry = *kv.second;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000271 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
Davide Pesavento412c9822021-07-02 00:21:05 -0400304RibManager::makeAuthorization(const std::string&)
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
Davide Pesavento5a897692019-10-31 01:28:43 -0400314 auto& validator = prefix == LOCALHOST_TOP_PREFIX ? m_localhostValidator : m_localhopValidator;
Junxiao Shi21738402016-08-19 19:48:00 +0000315 validator.validate(interest,
Davide Pesavento412c9822021-07-02 00:21:05 -0400316 [&interest, accept] (auto&&...) { extractRequester(interest, accept); },
317 [reject] (auto&&...) { reject(ndn::mgmt::RejectReply::STATUS403); });
Junxiao Shi21738402016-08-19 19:48:00 +0000318 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500319}
320
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000321std::ostream&
322operator<<(std::ostream& os, RibManager::SlAnnounceResult res)
323{
324 switch (res) {
Davide Pesavento5a897692019-10-31 01:28:43 -0400325 case RibManager::SlAnnounceResult::OK:
326 return os << "OK";
327 case RibManager::SlAnnounceResult::ERROR:
328 return os << "ERROR";
329 case RibManager::SlAnnounceResult::VALIDATION_FAILURE:
330 return os << "VALIDATION_FAILURE";
331 case RibManager::SlAnnounceResult::EXPIRED:
332 return os << "EXPIRED";
333 case RibManager::SlAnnounceResult::NOT_FOUND:
334 return os << "NOT_FOUND";
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000335 }
Davide Pesavento5a897692019-10-31 01:28:43 -0400336 NDN_THROW(std::invalid_argument("Unknown SlAnnounceResult"));
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000337}
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;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000349 }
Davide Pesavento5a897692019-10-31 01:28:43 -0400350 NDN_CXX_UNREACHABLE;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000351}
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
Teng Liang18c2b292019-10-18 14:31:04 -0700359 m_paValidator.validate(*pa.getData(),
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000360 [=] (const Data&) {
361 Route route(pa, faceId);
362 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400363 beginAddRoute(pa.getAnnouncedName(), route, std::nullopt,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000364 [=] (RibUpdateResult ribRes) {
365 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700366 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << ": " << res);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000367 cb(res);
368 });
369 },
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400370 [=] (const Data&, ndn::security::ValidationError err) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700371 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000372 " validation error: " << err);
373 cb(SlAnnounceResult::VALIDATION_FAILURE);
374 }
375 );
376}
377
378void
379RibManager::slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
380 const SlAnnounceCallback& cb)
381{
382 Route routeQuery;
383 routeQuery.faceId = faceId;
384 routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN;
Teng Lianga4e6ec32018-10-21 09:25:00 -0700385 Route* oldRoute = m_rib.findLongestPrefix(name, routeQuery);
386
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000387 if (oldRoute == nullptr || !oldRoute->announcement) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700388 NFD_LOG_DEBUG("slRenew " << name << " " << faceId << ": not found");
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000389 return cb(SlAnnounceResult::NOT_FOUND);
390 }
Teng Lianga4e6ec32018-10-21 09:25:00 -0700391 Name routeName = oldRoute->announcement->getAnnouncedName();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000392
393 Route route = *oldRoute;
394 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400395 beginAddRoute(routeName, route, std::nullopt,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000396 [=] (RibUpdateResult ribRes) {
397 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700398 NFD_LOG_INFO("slRenew " << name << " " << faceId << ": " << res << " " << routeName);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000399 cb(res);
400 });
401}
402
403void
404RibManager::slFindAnn(const Name& name, const SlFindAnnCallback& cb) const
405{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500406 shared_ptr<rib::RibEntry> entry;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000407 auto exactMatch = m_rib.find(name);
408 if (exactMatch != m_rib.end()) {
409 entry = exactMatch->second;
410 }
411 else {
412 entry = m_rib.findParent(name);
413 }
414 if (entry == nullptr) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400415 return cb(std::nullopt);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000416 }
417
418 auto pa = entry->getPrefixAnnouncement();
419 pa.toData(m_keyChain);
420 cb(pa);
421}
422
Vince Lehman26b215c2014-08-17 15:00:41 -0500423void
Vince Lehmancd613c52014-07-30 14:34:49 -0500424RibManager::fetchActiveFaces()
425{
426 NFD_LOG_DEBUG("Fetching active faces");
427
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700428 m_nfdController.fetch<ndn::nfd::FaceDataset>(
Davide Pesavento412c9822021-07-02 00:21:05 -0400429 std::bind(&RibManager::removeInvalidFaces, this, _1),
430 std::bind(&RibManager::onFetchActiveFacesFailure, this, _1, _2),
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700431 ndn::nfd::CommandOptions());
Vince Lehmancd613c52014-07-30 14:34:49 -0500432}
433
434void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700435RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason)
Vince Lehmancd613c52014-07-30 14:34:49 -0500436{
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700437 NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason);
Yanbiao Licf0db022016-01-29 00:54:25 -0800438 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
439}
440
441void
Yanbiao Licf0db022016-01-29 00:54:25 -0800442RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
443{
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400444 m_activeFaceFetchEvent = getScheduler().schedule(timeToWait, [this] { fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800445}
446
447void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700448RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500449{
Vince Lehman26b215c2014-08-17 15:00:41 -0500450 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500451
Junxiao Shi17a70012019-06-25 10:50:32 +0000452 std::set<uint64_t> activeFaceIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500453 for (const auto& faceStatus : activeFaces) {
454 activeFaceIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700455 }
Junxiao Shi17a70012019-06-25 10:50:32 +0000456 getGlobalIoService().post([=] { m_rib.beginRemoveFailedFaces(activeFaceIds); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500457
458 // Reschedule the check for future clean up
459 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500460}
461
462void
Davide Pesaventod396b612017-02-20 22:11:50 -0500463RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500464{
Yanbiao Licf0db022016-01-29 00:54:25 -0800465 NFD_LOG_TRACE("onNotification: " << notification);
466
467 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400468 NFD_LOG_DEBUG("Received notification for destroyed FaceId " << notification.getFaceId());
Junxiao Shi17a70012019-06-25 10:50:32 +0000469 getGlobalIoService().post([this, id = notification.getFaceId()] { m_rib.beginRemoveFace(id); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800470 }
471}
472
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700473} // namespace nfd