blob: ddc53cf01f0a53e1ae805e4d7483768f2e1d1ea9 [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 Pesavento45c1f6a2025-01-01 19:30:30 -05003 * Copyright (c) 2014-2025, 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
Davide Pesavento5d642632023-10-03 00:36:08 -040033#include <boost/asio/defer.hpp>
34
Junxiao Shicbc8e942016-09-06 03:17:45 +000035#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000036#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Davide Pesavento40641272023-03-16 13:31:12 -040037#include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
Alexander Afanasyeva1583702020-06-03 13:55:45 -040038#include <ndn-cxx/security/certificate-fetcher-direct-fetch.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070039
40namespace nfd {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050041
42using rib::RibUpdate;
43using rib::Route;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070044
Davide Pesaventoa3148082018-04-12 18:21:54 -040045NFD_LOG_INIT(RibManager);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070046
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040047const std::string MGMT_MODULE_NAME = "rib";
48const Name LOCALHOST_TOP_PREFIX = "/localhost/nfd";
Davide Pesavento20d94f62022-09-26 03:30:53 -040049constexpr time::seconds ACTIVE_FACE_FETCH_INTERVAL = 5_min;
Yanbiao Lif48d0802018-06-01 03:00:02 -070050
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)
Alexander Afanasyeva1583702020-06-03 13:55:45 -040060 , m_localhopValidator(make_unique<ndn::security::CertificateFetcherDirectFetch>(face))
61 , m_paValidator(make_unique<ndn::security::CertificateFetcherDirectFetch>(face))
Junxiao Shif4cfed12018-08-22 23:26:29 +000062 , m_isLocalhopEnabled(false)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070063{
Davide Pesavento1db1bb62025-01-06 01:23:41 -050064 registerCommandHandler<ndn::nfd::RibRegisterCommand>([this] (auto&&, auto&&... args) {
65 registerEntry(std::forward<decltype(args)>(args)...);
66 });
67 registerCommandHandler<ndn::nfd::RibUnregisterCommand>([this] (auto&&, auto&&... args) {
68 unregisterEntry(std::forward<decltype(args)>(args)...);
69 });
jaczhib0657682025-01-08 23:01:45 -080070 registerCommandHandler<ndn::nfd::RibAnnounceCommand>([this] (auto&&, auto&&... args) {
71 announceEntry(std::forward<decltype(args)>(args)...);
72 });
Davide Pesavento1db1bb62025-01-06 01:23:41 -050073 registerStatusDatasetHandler("list", [this] (auto&&, auto&&, auto&&... args) {
74 listEntries(std::forward<decltype(args)>(args)...);
75 });
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070076}
77
Junxiao Shif4cfed12018-08-22 23:26:29 +000078void
79RibManager::applyLocalhostConfig(const ConfigSection& section, const std::string& filename)
80{
81 m_localhostValidator.load(section, filename);
82}
83
84void
85RibManager::enableLocalhop(const ConfigSection& section, const std::string& filename)
86{
87 m_localhopValidator.load(section, filename);
88 m_isLocalhopEnabled = true;
89}
90
91void
92RibManager::disableLocalhop()
93{
94 m_isLocalhopEnabled = false;
95}
Vince Lehman26b215c2014-08-17 15:00:41 -050096
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070097void
Teng Liang18c2b292019-10-18 14:31:04 -070098RibManager::applyPaConfig(const ConfigSection& section, const std::string& filename)
99{
100 m_paValidator.load(section, filename);
101}
102
103void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700104RibManager::registerWithNfd()
105{
Junxiao Shif4cfed12018-08-22 23:26:29 +0000106 registerTopPrefix(LOCALHOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700107
Junxiao Shia3295742014-05-16 22:40:10 -0700108 if (m_isLocalhopEnabled) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000109 registerTopPrefix(LOCALHOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -0700110 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700111
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700112 NFD_LOG_INFO("Start monitoring face create/destroy events");
Davide Pesavento412c9822021-07-02 00:21:05 -0400113 m_faceMonitor.onNotification.connect([this] (const auto& notif) { onNotification(notif); });
Junxiao Shi15b12e72014-08-09 19:56:24 -0700114 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500115
Vince Lehman26b215c2014-08-17 15:00:41 -0500116 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700117}
118
119void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000120RibManager::enableLocalFields()
Yanbiao Licf0db022016-01-29 00:54:25 -0800121{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000122 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000123 ControlParameters().setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true),
Davide Pesavento19779d82019-02-14 13:40:04 -0500124 [] (const ControlParameters&) {
Junxiao Shi52009042018-09-10 12:33:56 +0000125 NFD_LOG_DEBUG("Local fields enabled");
126 },
127 [] (const ControlResponse& res) {
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500128 NDN_THROW(Error("Couldn't enable local fields (" + std::to_string(res.getCode()) +
Davide Pesavento19779d82019-02-14 13:40:04 -0500129 " " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000130 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800131}
132
133void
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400134RibManager::beginAddRoute(const Name& name, Route route, std::optional<time::nanoseconds> expires,
Junxiao Shi52009042018-09-10 12:33:56 +0000135 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800136{
Junxiao Shi52009042018-09-10 12:33:56 +0000137 if (expires) {
Junxiao Shi52009042018-09-10 12:33:56 +0000138 route.expires = time::steady_clock::now() + *expires;
139 }
140 else if (route.expires) {
141 expires = *route.expires - time::steady_clock::now();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000142 }
143
144 if (expires && *expires <= 0_s) {
145 m_rib.onRouteExpiration(name, route);
146 return done(RibUpdateResult::EXPIRED);
Junxiao Shi52009042018-09-10 12:33:56 +0000147 }
148
149 NFD_LOG_INFO("Adding route " << name << " nexthop=" << route.faceId <<
150 " origin=" << route.origin << " cost=" << route.cost);
151
152 if (expires) {
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400153 auto event = getScheduler().schedule(*expires, [=] { m_rib.onRouteExpiration(name, route); });
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000154 route.setExpirationEvent(event);
Junxiao Shi52009042018-09-10 12:33:56 +0000155 NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires);
156 }
157
Junxiao Shi52009042018-09-10 12:33:56 +0000158 RibUpdate update;
159 update.setAction(RibUpdate::REGISTER)
160 .setName(name)
161 .setRoute(route);
162 beginRibUpdate(update, done);
Yanbiao Licf0db022016-01-29 00:54:25 -0800163}
164
165void
Junxiao Shi52009042018-09-10 12:33:56 +0000166RibManager::beginRemoveRoute(const Name& name, const Route& route,
167 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800168{
Junxiao Shi52009042018-09-10 12:33:56 +0000169 NFD_LOG_INFO("Removing route " << name << " nexthop=" << route.faceId <<
170 " origin=" << route.origin);
Yanbiao Licf0db022016-01-29 00:54:25 -0800171
Junxiao Shi52009042018-09-10 12:33:56 +0000172 RibUpdate update;
173 update.setAction(RibUpdate::UNREGISTER)
174 .setName(name)
175 .setRoute(route);
176 beginRibUpdate(update, done);
177}
178
179void
180RibManager::beginRibUpdate(const RibUpdate& update,
181 const std::function<void(RibUpdateResult)>& done)
182{
183 m_rib.beginApplyUpdate(update,
184 [=] {
185 NFD_LOG_DEBUG("RIB update succeeded for " << update);
186 done(RibUpdateResult::OK);
187 },
188 [=] (uint32_t code, const std::string& error) {
189 NFD_LOG_DEBUG("RIB update failed for " << update << " (" << code << " " << error << ")");
190
191 // Since the FIB rejected the update, clean up invalid routes
192 scheduleActiveFaceFetch(1_s);
193
194 done(RibUpdateResult::ERROR);
195 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800196}
197
198void
Yanbiao Licf0db022016-01-29 00:54:25 -0800199RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700200{
Junxiao Shi52009042018-09-10 12:33:56 +0000201 // add FIB nexthop
Yanbiao Licf0db022016-01-29 00:54:25 -0800202 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000203 ControlParameters().setName(Name(topPrefix).append(MGMT_MODULE_NAME))
204 .setFaceId(0),
205 [=] (const ControlParameters& res) {
206 NFD_LOG_DEBUG("Successfully registered " << topPrefix << " with NFD");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700207
Junxiao Shi52009042018-09-10 12:33:56 +0000208 // Routes must be inserted into the RIB so route flags can be applied
209 Route route;
210 route.faceId = res.getFaceId();
211 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
212 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
213
214 m_rib.insert(topPrefix, route);
Junxiao Shi52009042018-09-10 12:33:56 +0000215 },
216 [=] (const ControlResponse& res) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500217 NDN_THROW(Error("Cannot add FIB entry " + topPrefix.toUri() + " (" +
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500218 std::to_string(res.getCode()) + " " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000219 });
220
221 // add top prefix to the dispatcher without prefix registration
Junxiao Shif4cfed12018-08-22 23:26:29 +0000222 m_dispatcher.addTopPrefix(topPrefix, false);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700223}
224
225void
Davide Pesaventoae430302023-05-11 01:42:46 -0400226RibManager::registerEntry(const Interest& interest, ControlParameters parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500227 const CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700228{
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400229 if (parameters.getName().size() > Fib::getMaxDepth()) {
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500230 done(ControlResponse(414, "Route prefix cannot exceed " + std::to_string(Fib::getMaxDepth()) +
Junxiao Shi75306352018-02-01 21:59:44 +0000231 " components"));
232 return;
233 }
234
Yanbiao Licf0db022016-01-29 00:54:25 -0800235 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600236
237 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800238 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700239
Vince Lehman218be0a2015-01-15 17:25:20 -0600240 Route route;
241 route.faceId = parameters.getFaceId();
242 route.origin = parameters.getOrigin();
243 route.cost = parameters.getCost();
244 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500245
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400246 std::optional<time::nanoseconds> expires;
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700247 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000248 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Junxiao Shi52009042018-09-10 12:33:56 +0000249 expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod());
Vince Lehman76c751c2014-11-18 17:36:38 -0600250 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700251
Junxiao Shi52009042018-09-10 12:33:56 +0000252 beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {});
Vince Lehman281ded72014-08-21 12:17:08 -0500253}
254
255void
Davide Pesaventoae430302023-05-11 01:42:46 -0400256RibManager::unregisterEntry(const Interest& interest, ControlParameters parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500257 const CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700258{
Yanbiao Licf0db022016-01-29 00:54:25 -0800259 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600260
261 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800262 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700263
Vince Lehman218be0a2015-01-15 17:25:20 -0600264 Route route;
265 route.faceId = parameters.getFaceId();
266 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700267
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400268 beginRemoveRoute(parameters.getName(), route, [] (auto&&...) {});
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700269}
270
271void
jaczhib0657682025-01-08 23:01:45 -0800272RibManager::announceEntry(const Interest& interest, const ndn::nfd::RibAnnounceParameters& parameters,
273 const CommandContinuation& done)
274{
275 const auto& announcement = parameters.getPrefixAnnouncement();
276 if (announcement.getAnnouncedName().size() > Fib::getMaxDepth()) {
277 done(ControlResponse(414, "Route prefix cannot exceed " + std::to_string(Fib::getMaxDepth()) +
278 " components"));
279 return;
280 }
281
282 // Prepare parameters for response
283 ControlParameters responseParams;
284 responseParams.setFaceId(0);
285 setFaceForSelfRegistration(interest, responseParams);
286
287 Route route(announcement, responseParams.getFaceId());
288
289 responseParams
290 .setName(announcement.getAnnouncedName())
291 .setOrigin(route.origin)
292 .setCost(route.cost)
293 .setFlags(route.flags)
294 .setExpirationPeriod(time::duration_cast<time::milliseconds>(route.annExpires - time::steady_clock::now()));
295
296 BOOST_ASSERT(announcement.getData());
297 m_paValidator.validate(*announcement.getData(),
298 [=, name = announcement.getAnnouncedName(), route = std::move(route)] (const Data&) {
299 // Respond since command is valid and authorized
300 done(ControlResponse(200, "Success").setBody(responseParams.wireEncode()));
301 beginAddRoute(name, std::move(route), std::nullopt, [] (RibUpdateResult) {});
302 },
303 [=] (const Data&, ndn::security::ValidationError err) {
304 done(ControlResponse(403, "Validation error: " + err.getInfo()));
305 }
306 );
307}
308
309void
Davide Pesaventoae430302023-05-11 01:42:46 -0400310RibManager::listEntries(ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700311{
Junxiao Shi3f21e582017-05-29 15:26:32 +0000312 auto now = time::steady_clock::now();
313 for (const auto& kv : m_rib) {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500314 const rib::RibEntry& entry = *kv.second;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000315 ndn::nfd::RibEntry item;
316 item.setName(entry.getName());
317 for (const Route& route : entry.getRoutes()) {
318 ndn::nfd::Route r;
319 r.setFaceId(route.faceId);
320 r.setOrigin(route.origin);
321 r.setCost(route.cost);
322 r.setFlags(route.flags);
323 if (route.expires) {
324 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now));
325 }
326 item.addRoute(r);
327 }
328 context.append(item.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600329 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800330 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700331}
332
333void
Yanbiao Licf0db022016-01-29 00:54:25 -0800334RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700335{
Yanbiao Licf0db022016-01-29 00:54:25 -0800336 bool isSelfRegistration = (parameters.getFaceId() == 0);
337 if (isSelfRegistration) {
338 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
339 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
340 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
341 // and is initialized synchronously with IncomingFaceId field enabled.
342 BOOST_ASSERT(incomingFaceIdTag != nullptr);
343 parameters.setFaceId(*incomingFaceIdTag);
Vince Lehman76c751c2014-11-18 17:36:38 -0600344 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700345}
346
Junxiao Shi21738402016-08-19 19:48:00 +0000347ndn::mgmt::Authorization
Davide Pesavento412c9822021-07-02 00:21:05 -0400348RibManager::makeAuthorization(const std::string&)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700349{
Junxiao Shi21738402016-08-19 19:48:00 +0000350 return [this] (const Name& prefix, const Interest& interest,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500351 const ndn::mgmt::ControlParametersBase* params,
Junxiao Shi21738402016-08-19 19:48:00 +0000352 const ndn::mgmt::AcceptContinuation& accept,
353 const ndn::mgmt::RejectContinuation& reject) {
354 BOOST_ASSERT(params != nullptr);
jaczhib0657682025-01-08 23:01:45 -0800355 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters) ||
356 typeid(*params) == typeid(ndn::nfd::RibAnnounceParameters));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000357 BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700358
Davide Pesavento5a897692019-10-31 01:28:43 -0400359 auto& validator = prefix == LOCALHOST_TOP_PREFIX ? m_localhostValidator : m_localhopValidator;
Junxiao Shi21738402016-08-19 19:48:00 +0000360 validator.validate(interest,
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400361 [&interest, accept] (auto&&...) { accept(extractSigner(interest)); },
Davide Pesavento412c9822021-07-02 00:21:05 -0400362 [reject] (auto&&...) { reject(ndn::mgmt::RejectReply::STATUS403); });
Junxiao Shi21738402016-08-19 19:48:00 +0000363 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500364}
365
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000366std::ostream&
367operator<<(std::ostream& os, RibManager::SlAnnounceResult res)
368{
369 switch (res) {
Davide Pesavento5a897692019-10-31 01:28:43 -0400370 case RibManager::SlAnnounceResult::OK:
371 return os << "OK";
372 case RibManager::SlAnnounceResult::ERROR:
373 return os << "ERROR";
374 case RibManager::SlAnnounceResult::VALIDATION_FAILURE:
375 return os << "VALIDATION_FAILURE";
376 case RibManager::SlAnnounceResult::EXPIRED:
377 return os << "EXPIRED";
378 case RibManager::SlAnnounceResult::NOT_FOUND:
379 return os << "NOT_FOUND";
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000380 }
Davide Pesavento5a897692019-10-31 01:28:43 -0400381 NDN_THROW(std::invalid_argument("Unknown SlAnnounceResult"));
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000382}
383
384RibManager::SlAnnounceResult
385RibManager::getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r)
386{
387 switch (r) {
388 case RibUpdateResult::OK:
389 return SlAnnounceResult::OK;
390 case RibUpdateResult::ERROR:
391 return SlAnnounceResult::ERROR;
392 case RibUpdateResult::EXPIRED:
393 return SlAnnounceResult::EXPIRED;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000394 }
Davide Pesavento5a897692019-10-31 01:28:43 -0400395 NDN_CXX_UNREACHABLE;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000396}
397
398void
399RibManager::slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId,
400 time::milliseconds maxLifetime, const SlAnnounceCallback& cb)
401{
402 BOOST_ASSERT(pa.getData());
403
Teng Liang18c2b292019-10-18 14:31:04 -0700404 m_paValidator.validate(*pa.getData(),
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000405 [=] (const Data&) {
406 Route route(pa, faceId);
407 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400408 beginAddRoute(pa.getAnnouncedName(), route, std::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("slAnnounce " << pa.getAnnouncedName() << " " << faceId << ": " << res);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000412 cb(res);
413 });
414 },
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400415 [=] (const Data&, ndn::security::ValidationError err) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700416 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000417 " validation error: " << err);
418 cb(SlAnnounceResult::VALIDATION_FAILURE);
419 }
420 );
421}
422
423void
424RibManager::slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
425 const SlAnnounceCallback& cb)
426{
427 Route routeQuery;
428 routeQuery.faceId = faceId;
429 routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN;
Teng Lianga4e6ec32018-10-21 09:25:00 -0700430 Route* oldRoute = m_rib.findLongestPrefix(name, routeQuery);
431
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000432 if (oldRoute == nullptr || !oldRoute->announcement) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700433 NFD_LOG_DEBUG("slRenew " << name << " " << faceId << ": not found");
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000434 return cb(SlAnnounceResult::NOT_FOUND);
435 }
Teng Lianga4e6ec32018-10-21 09:25:00 -0700436 Name routeName = oldRoute->announcement->getAnnouncedName();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000437
438 Route route = *oldRoute;
439 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400440 beginAddRoute(routeName, route, std::nullopt,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000441 [=] (RibUpdateResult ribRes) {
442 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700443 NFD_LOG_INFO("slRenew " << name << " " << faceId << ": " << res << " " << routeName);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000444 cb(res);
445 });
446}
447
448void
449RibManager::slFindAnn(const Name& name, const SlFindAnnCallback& cb) const
450{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500451 shared_ptr<rib::RibEntry> entry;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000452 auto exactMatch = m_rib.find(name);
453 if (exactMatch != m_rib.end()) {
454 entry = exactMatch->second;
455 }
456 else {
457 entry = m_rib.findParent(name);
458 }
459 if (entry == nullptr) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400460 return cb(std::nullopt);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000461 }
462
463 auto pa = entry->getPrefixAnnouncement();
464 pa.toData(m_keyChain);
465 cb(pa);
466}
467
Vince Lehman26b215c2014-08-17 15:00:41 -0500468void
Vince Lehmancd613c52014-07-30 14:34:49 -0500469RibManager::fetchActiveFaces()
470{
471 NFD_LOG_DEBUG("Fetching active faces");
472
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700473 m_nfdController.fetch<ndn::nfd::FaceDataset>(
Davide Pesaventoae430302023-05-11 01:42:46 -0400474 [this] (auto&&... args) { removeInvalidFaces(std::forward<decltype(args)>(args)...); },
475 [this] (auto&&... args) { onFetchActiveFacesFailure(std::forward<decltype(args)>(args)...); });
Vince Lehmancd613c52014-07-30 14:34:49 -0500476}
477
478void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700479RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason)
Vince Lehmancd613c52014-07-30 14:34:49 -0500480{
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700481 NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason);
Yanbiao Licf0db022016-01-29 00:54:25 -0800482 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
483}
484
485void
Yanbiao Licf0db022016-01-29 00:54:25 -0800486RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
487{
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400488 m_activeFaceFetchEvent = getScheduler().schedule(timeToWait, [this] { fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800489}
490
491void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700492RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500493{
Vince Lehman26b215c2014-08-17 15:00:41 -0500494 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500495
Davide Pesavento5d642632023-10-03 00:36:08 -0400496 std::set<uint64_t> activeIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500497 for (const auto& faceStatus : activeFaces) {
Davide Pesavento5d642632023-10-03 00:36:08 -0400498 activeIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700499 }
Davide Pesavento5d642632023-10-03 00:36:08 -0400500 boost::asio::defer(getGlobalIoService(),
501 [this, active = std::move(activeIds)] { m_rib.beginRemoveFailedFaces(active); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500502
503 // Reschedule the check for future clean up
504 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500505}
506
507void
Davide Pesaventod396b612017-02-20 22:11:50 -0500508RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500509{
Yanbiao Licf0db022016-01-29 00:54:25 -0800510 NFD_LOG_TRACE("onNotification: " << notification);
511
512 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400513 NFD_LOG_DEBUG("Received notification for destroyed FaceId " << notification.getFaceId());
Davide Pesavento5d642632023-10-03 00:36:08 -0400514 boost::asio::defer(getGlobalIoService(),
515 [this, id = notification.getFaceId()] { m_rib.beginRemoveFace(id); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800516 }
517}
518
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700519} // namespace nfd