blob: d9331e56ab368f1ecb87cd63bbc96c50b52fe571 [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>
Davide Pesavento21e24f92025-01-10 22:22:43 -050034#include <boost/lexical_cast.hpp>
Davide Pesavento5d642632023-10-03 00:36:08 -040035
Junxiao Shicbc8e942016-09-06 03:17:45 +000036#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000037#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Davide Pesavento40641272023-03-16 13:31:12 -040038#include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
Alexander Afanasyeva1583702020-06-03 13:55:45 -040039#include <ndn-cxx/security/certificate-fetcher-direct-fetch.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070040
41namespace nfd {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050042
43using rib::RibUpdate;
44using rib::Route;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070045
Davide Pesaventoa3148082018-04-12 18:21:54 -040046NFD_LOG_INIT(RibManager);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070047
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040048const std::string MGMT_MODULE_NAME = "rib";
49const Name LOCALHOST_TOP_PREFIX = "/localhost/nfd";
Davide Pesavento20d94f62022-09-26 03:30:53 -040050constexpr time::seconds ACTIVE_FACE_FETCH_INTERVAL = 5_min;
Yanbiao Lif48d0802018-06-01 03:00:02 -070051
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050052RibManager::RibManager(rib::Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
Davide Pesavento0a71dd32019-03-17 20:36:18 -040053 ndn::nfd::Controller& nfdController, Dispatcher& dispatcher)
Davide Pesavento78ddcab2019-02-28 22:00:03 -050054 : ManagerBase(MGMT_MODULE_NAME, dispatcher)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000055 , m_rib(rib)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000056 , m_keyChain(keyChain)
Junxiao Shif4cfed12018-08-22 23:26:29 +000057 , m_nfdController(nfdController)
58 , m_dispatcher(dispatcher)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000059 , m_faceMonitor(face)
60 , m_localhostValidator(face)
Alexander Afanasyeva1583702020-06-03 13:55:45 -040061 , m_localhopValidator(make_unique<ndn::security::CertificateFetcherDirectFetch>(face))
62 , m_paValidator(make_unique<ndn::security::CertificateFetcherDirectFetch>(face))
Junxiao Shif4cfed12018-08-22 23:26:29 +000063 , m_isLocalhopEnabled(false)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070064{
Davide Pesavento1db1bb62025-01-06 01:23:41 -050065 registerCommandHandler<ndn::nfd::RibRegisterCommand>([this] (auto&&, auto&&... args) {
66 registerEntry(std::forward<decltype(args)>(args)...);
67 });
68 registerCommandHandler<ndn::nfd::RibUnregisterCommand>([this] (auto&&, auto&&... args) {
69 unregisterEntry(std::forward<decltype(args)>(args)...);
70 });
jaczhib0657682025-01-08 23:01:45 -080071 registerCommandHandler<ndn::nfd::RibAnnounceCommand>([this] (auto&&, auto&&... args) {
72 announceEntry(std::forward<decltype(args)>(args)...);
73 });
Davide Pesavento1db1bb62025-01-06 01:23:41 -050074 registerStatusDatasetHandler("list", [this] (auto&&, auto&&, auto&&... args) {
75 listEntries(std::forward<decltype(args)>(args)...);
76 });
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070077}
78
Junxiao Shif4cfed12018-08-22 23:26:29 +000079void
80RibManager::applyLocalhostConfig(const ConfigSection& section, const std::string& filename)
81{
82 m_localhostValidator.load(section, filename);
83}
84
85void
86RibManager::enableLocalhop(const ConfigSection& section, const std::string& filename)
87{
88 m_localhopValidator.load(section, filename);
89 m_isLocalhopEnabled = true;
90}
91
92void
93RibManager::disableLocalhop()
94{
95 m_isLocalhopEnabled = false;
96}
Vince Lehman26b215c2014-08-17 15:00:41 -050097
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070098void
Teng Liang18c2b292019-10-18 14:31:04 -070099RibManager::applyPaConfig(const ConfigSection& section, const std::string& filename)
100{
101 m_paValidator.load(section, filename);
102}
103
104void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700105RibManager::registerWithNfd()
106{
Junxiao Shif4cfed12018-08-22 23:26:29 +0000107 registerTopPrefix(LOCALHOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700108
Junxiao Shia3295742014-05-16 22:40:10 -0700109 if (m_isLocalhopEnabled) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000110 registerTopPrefix(LOCALHOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -0700111 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700112
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700113 NFD_LOG_INFO("Start monitoring face create/destroy events");
Davide Pesavento412c9822021-07-02 00:21:05 -0400114 m_faceMonitor.onNotification.connect([this] (const auto& notif) { onNotification(notif); });
Junxiao Shi15b12e72014-08-09 19:56:24 -0700115 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500116
Vince Lehman26b215c2014-08-17 15:00:41 -0500117 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700118}
119
120void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000121RibManager::enableLocalFields()
Yanbiao Licf0db022016-01-29 00:54:25 -0800122{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000123 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000124 ControlParameters().setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true),
Davide Pesavento19779d82019-02-14 13:40:04 -0500125 [] (const ControlParameters&) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500126 NFD_LOG_TRACE("Local fields enabled");
Junxiao Shi52009042018-09-10 12:33:56 +0000127 },
128 [] (const ControlResponse& res) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500129 NDN_THROW(Error("Could not enable local fields (error " +
130 std::to_string(res.getCode()) + ": " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000131 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800132}
133
134void
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400135RibManager::beginAddRoute(const Name& name, Route route, std::optional<time::nanoseconds> expires,
Junxiao Shi52009042018-09-10 12:33:56 +0000136 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800137{
Junxiao Shi52009042018-09-10 12:33:56 +0000138 if (expires) {
Junxiao Shi52009042018-09-10 12:33:56 +0000139 route.expires = time::steady_clock::now() + *expires;
140 }
141 else if (route.expires) {
142 expires = *route.expires - time::steady_clock::now();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000143 }
144
145 if (expires && *expires <= 0_s) {
146 m_rib.onRouteExpiration(name, route);
147 return done(RibUpdateResult::EXPIRED);
Junxiao Shi52009042018-09-10 12:33:56 +0000148 }
149
150 NFD_LOG_INFO("Adding route " << name << " nexthop=" << route.faceId <<
151 " origin=" << route.origin << " cost=" << route.cost);
152
153 if (expires) {
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400154 auto event = getScheduler().schedule(*expires, [=] { m_rib.onRouteExpiration(name, route); });
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000155 route.setExpirationEvent(event);
Junxiao Shi52009042018-09-10 12:33:56 +0000156 NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires);
157 }
158
Junxiao Shi52009042018-09-10 12:33:56 +0000159 RibUpdate update;
160 update.setAction(RibUpdate::REGISTER)
161 .setName(name)
162 .setRoute(route);
163 beginRibUpdate(update, done);
Yanbiao Licf0db022016-01-29 00:54:25 -0800164}
165
166void
Junxiao Shi52009042018-09-10 12:33:56 +0000167RibManager::beginRemoveRoute(const Name& name, const Route& route,
168 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800169{
Junxiao Shi52009042018-09-10 12:33:56 +0000170 NFD_LOG_INFO("Removing route " << name << " nexthop=" << route.faceId <<
171 " origin=" << route.origin);
Yanbiao Licf0db022016-01-29 00:54:25 -0800172
Junxiao Shi52009042018-09-10 12:33:56 +0000173 RibUpdate update;
174 update.setAction(RibUpdate::UNREGISTER)
175 .setName(name)
176 .setRoute(route);
177 beginRibUpdate(update, done);
178}
179
180void
181RibManager::beginRibUpdate(const RibUpdate& update,
182 const std::function<void(RibUpdateResult)>& done)
183{
184 m_rib.beginApplyUpdate(update,
185 [=] {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500186 NFD_LOG_DEBUG(update << " -> OK");
Junxiao Shi52009042018-09-10 12:33:56 +0000187 done(RibUpdateResult::OK);
188 },
189 [=] (uint32_t code, const std::string& error) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500190 NFD_LOG_DEBUG(update << " -> error " << code << ": " << error);
Junxiao Shi52009042018-09-10 12:33:56 +0000191
192 // Since the FIB rejected the update, clean up invalid routes
193 scheduleActiveFaceFetch(1_s);
194
195 done(RibUpdateResult::ERROR);
196 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800197}
198
199void
Yanbiao Licf0db022016-01-29 00:54:25 -0800200RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700201{
Junxiao Shi52009042018-09-10 12:33:56 +0000202 // add FIB nexthop
Yanbiao Licf0db022016-01-29 00:54:25 -0800203 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Davide Pesavento21e24f92025-01-10 22:22:43 -0500204 ControlParameters().setName(Name(topPrefix).append(MGMT_MODULE_NAME)).setFaceId(0),
Junxiao Shi52009042018-09-10 12:33:56 +0000205 [=] (const ControlParameters& res) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500206 NFD_LOG_DEBUG("Successfully registered " << topPrefix << " with the forwarder");
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 Pesavento21e24f92025-01-10 22:22:43 -0500217 NDN_THROW(Error("Could not add FIB entry " + topPrefix.toUri() + " (error " +
218 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
Davide Pesavento21e24f92025-01-10 22:22:43 -0500225static uint64_t
226getIncomingFaceId(const Interest& request)
227{
228 auto incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
229 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
230 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
231 // and is initialized synchronously with IncomingFaceId field enabled.
232 BOOST_ASSERT(incomingFaceIdTag != nullptr);
233 return incomingFaceIdTag->get();
234}
235
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700236void
Davide Pesaventoae430302023-05-11 01:42:46 -0400237RibManager::registerEntry(const Interest& interest, ControlParameters parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500238 const CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700239{
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400240 if (parameters.getName().size() > Fib::getMaxDepth()) {
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500241 done(ControlResponse(414, "Route prefix cannot exceed " + std::to_string(Fib::getMaxDepth()) +
Junxiao Shi75306352018-02-01 21:59:44 +0000242 " components"));
243 return;
244 }
245
Davide Pesavento21e24f92025-01-10 22:22:43 -0500246 if (parameters.getFaceId() == 0) { // self registration
247 parameters.setFaceId(getIncomingFaceId(interest));
248 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600249
250 // Respond since command is valid and authorized
Davide Pesavento21e24f92025-01-10 22:22:43 -0500251 done(ControlResponse(200, "OK").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();
256 route.cost = parameters.getCost();
257 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500258
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400259 std::optional<time::nanoseconds> expires;
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700260 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000261 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Junxiao Shi52009042018-09-10 12:33:56 +0000262 expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod());
Vince Lehman76c751c2014-11-18 17:36:38 -0600263 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700264
Junxiao Shi52009042018-09-10 12:33:56 +0000265 beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {});
Vince Lehman281ded72014-08-21 12:17:08 -0500266}
267
268void
Davide Pesaventoae430302023-05-11 01:42:46 -0400269RibManager::unregisterEntry(const Interest& interest, ControlParameters parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500270 const CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700271{
Davide Pesavento21e24f92025-01-10 22:22:43 -0500272 if (parameters.getFaceId() == 0) { // self unregistration
273 parameters.setFaceId(getIncomingFaceId(interest));
274 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600275
276 // Respond since command is valid and authorized
Davide Pesavento21e24f92025-01-10 22:22:43 -0500277 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700278
Vince Lehman218be0a2015-01-15 17:25:20 -0600279 Route route;
280 route.faceId = parameters.getFaceId();
281 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700282
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400283 beginRemoveRoute(parameters.getName(), route, [] (auto&&...) {});
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700284}
285
286void
jaczhib0657682025-01-08 23:01:45 -0800287RibManager::announceEntry(const Interest& interest, const ndn::nfd::RibAnnounceParameters& parameters,
288 const CommandContinuation& done)
289{
290 const auto& announcement = parameters.getPrefixAnnouncement();
Davide Pesavento21e24f92025-01-10 22:22:43 -0500291 const auto& name = announcement.getAnnouncedName();
292 if (name.size() > Fib::getMaxDepth()) {
jaczhib0657682025-01-08 23:01:45 -0800293 done(ControlResponse(414, "Route prefix cannot exceed " + std::to_string(Fib::getMaxDepth()) +
294 " components"));
295 return;
296 }
297
Davide Pesavento21e24f92025-01-10 22:22:43 -0500298 Route route(announcement, getIncomingFaceId(interest));
299
jaczhib0657682025-01-08 23:01:45 -0800300 // Prepare parameters for response
301 ControlParameters responseParams;
jaczhib0657682025-01-08 23:01:45 -0800302 responseParams
Davide Pesavento21e24f92025-01-10 22:22:43 -0500303 .setName(name)
304 .setFaceId(route.faceId)
jaczhib0657682025-01-08 23:01:45 -0800305 .setOrigin(route.origin)
306 .setCost(route.cost)
307 .setFlags(route.flags)
308 .setExpirationPeriod(time::duration_cast<time::milliseconds>(route.annExpires - time::steady_clock::now()));
309
Davide Pesavento21e24f92025-01-10 22:22:43 -0500310 NDN_LOG_TRACE("Validating announcement " << announcement);
jaczhib0657682025-01-08 23:01:45 -0800311 BOOST_ASSERT(announcement.getData());
312 m_paValidator.validate(*announcement.getData(),
Davide Pesavento21e24f92025-01-10 22:22:43 -0500313 [=, route = std::move(route)] (const Data&) {
jaczhib0657682025-01-08 23:01:45 -0800314 // Respond since command is valid and authorized
Davide Pesavento21e24f92025-01-10 22:22:43 -0500315 done(ControlResponse(200, "OK").setBody(responseParams.wireEncode()));
jaczhib0657682025-01-08 23:01:45 -0800316 beginAddRoute(name, std::move(route), std::nullopt, [] (RibUpdateResult) {});
317 },
Davide Pesavento21e24f92025-01-10 22:22:43 -0500318 [=] (const Data&, const ndn::security::ValidationError& err) {
319 NDN_LOG_DEBUG("announce " << name << " -> " << err);
320 done(ControlResponse(403, "Prefix announcement rejected: " +
321 boost::lexical_cast<std::string>(err.getCode())));
322 });
jaczhib0657682025-01-08 23:01:45 -0800323}
324
325void
Davide Pesaventoae430302023-05-11 01:42:46 -0400326RibManager::listEntries(ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700327{
Junxiao Shi3f21e582017-05-29 15:26:32 +0000328 auto now = time::steady_clock::now();
329 for (const auto& kv : m_rib) {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500330 const rib::RibEntry& entry = *kv.second;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000331 ndn::nfd::RibEntry item;
332 item.setName(entry.getName());
333 for (const Route& route : entry.getRoutes()) {
334 ndn::nfd::Route r;
335 r.setFaceId(route.faceId);
336 r.setOrigin(route.origin);
337 r.setCost(route.cost);
338 r.setFlags(route.flags);
339 if (route.expires) {
340 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now));
341 }
342 item.addRoute(r);
343 }
344 context.append(item.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600345 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800346 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700347}
348
Junxiao Shi21738402016-08-19 19:48:00 +0000349ndn::mgmt::Authorization
Davide Pesavento412c9822021-07-02 00:21:05 -0400350RibManager::makeAuthorization(const std::string&)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700351{
Junxiao Shi21738402016-08-19 19:48:00 +0000352 return [this] (const Name& prefix, const Interest& interest,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500353 const ndn::mgmt::ControlParametersBase* params,
Junxiao Shi21738402016-08-19 19:48:00 +0000354 const ndn::mgmt::AcceptContinuation& accept,
355 const ndn::mgmt::RejectContinuation& reject) {
356 BOOST_ASSERT(params != nullptr);
jaczhib0657682025-01-08 23:01:45 -0800357 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters) ||
358 typeid(*params) == typeid(ndn::nfd::RibAnnounceParameters));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000359 BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700360
Davide Pesavento5a897692019-10-31 01:28:43 -0400361 auto& validator = prefix == LOCALHOST_TOP_PREFIX ? m_localhostValidator : m_localhopValidator;
Junxiao Shi21738402016-08-19 19:48:00 +0000362 validator.validate(interest,
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400363 [&interest, accept] (auto&&...) { accept(extractSigner(interest)); },
Davide Pesavento412c9822021-07-02 00:21:05 -0400364 [reject] (auto&&...) { reject(ndn::mgmt::RejectReply::STATUS403); });
Junxiao Shi21738402016-08-19 19:48:00 +0000365 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500366}
367
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000368std::ostream&
369operator<<(std::ostream& os, RibManager::SlAnnounceResult res)
370{
371 switch (res) {
Davide Pesavento5a897692019-10-31 01:28:43 -0400372 case RibManager::SlAnnounceResult::OK:
373 return os << "OK";
374 case RibManager::SlAnnounceResult::ERROR:
375 return os << "ERROR";
376 case RibManager::SlAnnounceResult::VALIDATION_FAILURE:
377 return os << "VALIDATION_FAILURE";
378 case RibManager::SlAnnounceResult::EXPIRED:
379 return os << "EXPIRED";
380 case RibManager::SlAnnounceResult::NOT_FOUND:
381 return os << "NOT_FOUND";
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000382 }
Davide Pesavento5a897692019-10-31 01:28:43 -0400383 NDN_THROW(std::invalid_argument("Unknown SlAnnounceResult"));
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000384}
385
386RibManager::SlAnnounceResult
387RibManager::getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r)
388{
389 switch (r) {
390 case RibUpdateResult::OK:
391 return SlAnnounceResult::OK;
392 case RibUpdateResult::ERROR:
393 return SlAnnounceResult::ERROR;
394 case RibUpdateResult::EXPIRED:
395 return SlAnnounceResult::EXPIRED;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000396 }
Davide Pesavento5a897692019-10-31 01:28:43 -0400397 NDN_CXX_UNREACHABLE;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000398}
399
400void
401RibManager::slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId,
402 time::milliseconds maxLifetime, const SlAnnounceCallback& cb)
403{
404 BOOST_ASSERT(pa.getData());
405
Teng Liang18c2b292019-10-18 14:31:04 -0700406 m_paValidator.validate(*pa.getData(),
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000407 [=] (const Data&) {
408 Route route(pa, faceId);
409 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400410 beginAddRoute(pa.getAnnouncedName(), route, std::nullopt,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000411 [=] (RibUpdateResult ribRes) {
412 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500413 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << " -> " << res);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000414 cb(res);
415 });
416 },
Davide Pesavento21e24f92025-01-10 22:22:43 -0500417 [=] (const Data&, const ndn::security::ValidationError& err) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700418 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Davide Pesavento21e24f92025-01-10 22:22:43 -0500419 " -> validation error: " << err);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000420 cb(SlAnnounceResult::VALIDATION_FAILURE);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500421 });
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000422}
423
424void
425RibManager::slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
426 const SlAnnounceCallback& cb)
427{
428 Route routeQuery;
429 routeQuery.faceId = faceId;
430 routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN;
Teng Lianga4e6ec32018-10-21 09:25:00 -0700431 Route* oldRoute = m_rib.findLongestPrefix(name, routeQuery);
432
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000433 if (oldRoute == nullptr || !oldRoute->announcement) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500434 NFD_LOG_DEBUG("slRenew " << name << " " << faceId << " -> not found");
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000435 return cb(SlAnnounceResult::NOT_FOUND);
436 }
Teng Lianga4e6ec32018-10-21 09:25:00 -0700437 Name routeName = oldRoute->announcement->getAnnouncedName();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000438
439 Route route = *oldRoute;
440 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400441 beginAddRoute(routeName, route, std::nullopt,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000442 [=] (RibUpdateResult ribRes) {
443 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500444 NFD_LOG_INFO("slRenew " << name << " " << faceId << " -> " << res << " " << routeName);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000445 cb(res);
446 });
447}
448
449void
450RibManager::slFindAnn(const Name& name, const SlFindAnnCallback& cb) const
451{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500452 shared_ptr<rib::RibEntry> entry;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000453 auto exactMatch = m_rib.find(name);
454 if (exactMatch != m_rib.end()) {
455 entry = exactMatch->second;
456 }
457 else {
458 entry = m_rib.findParent(name);
459 }
460 if (entry == nullptr) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400461 return cb(std::nullopt);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000462 }
463
464 auto pa = entry->getPrefixAnnouncement();
465 pa.toData(m_keyChain);
466 cb(pa);
467}
468
Vince Lehman26b215c2014-08-17 15:00:41 -0500469void
Vince Lehmancd613c52014-07-30 14:34:49 -0500470RibManager::fetchActiveFaces()
471{
472 NFD_LOG_DEBUG("Fetching active faces");
473
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700474 m_nfdController.fetch<ndn::nfd::FaceDataset>(
Davide Pesaventoae430302023-05-11 01:42:46 -0400475 [this] (auto&&... args) { removeInvalidFaces(std::forward<decltype(args)>(args)...); },
Davide Pesavento21e24f92025-01-10 22:22:43 -0500476 [this] (uint32_t code, const std::string& reason) {
477 NFD_LOG_WARN("Failed to fetch face dataset (error " << code << ": " << reason << ")");
478 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
479 });
Vince Lehmancd613c52014-07-30 14:34:49 -0500480}
481
482void
Davide Pesavento21e24f92025-01-10 22:22:43 -0500483RibManager::scheduleActiveFaceFetch(time::seconds timeToWait)
Yanbiao Licf0db022016-01-29 00:54:25 -0800484{
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400485 m_activeFaceFetchEvent = getScheduler().schedule(timeToWait, [this] { fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800486}
487
488void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700489RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500490{
Vince Lehman26b215c2014-08-17 15:00:41 -0500491 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500492
Davide Pesavento5d642632023-10-03 00:36:08 -0400493 std::set<uint64_t> activeIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500494 for (const auto& faceStatus : activeFaces) {
Davide Pesavento5d642632023-10-03 00:36:08 -0400495 activeIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700496 }
Davide Pesavento5d642632023-10-03 00:36:08 -0400497 boost::asio::defer(getGlobalIoService(),
498 [this, active = std::move(activeIds)] { m_rib.beginRemoveFailedFaces(active); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500499
500 // Reschedule the check for future clean up
501 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500502}
503
504void
Davide Pesaventod396b612017-02-20 22:11:50 -0500505RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500506{
Davide Pesavento21e24f92025-01-10 22:22:43 -0500507 NFD_LOG_TRACE("Received notification " << notification);
Yanbiao Licf0db022016-01-29 00:54:25 -0800508
509 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
Davide Pesavento5d642632023-10-03 00:36:08 -0400510 boost::asio::defer(getGlobalIoService(),
511 [this, id = notification.getFaceId()] { m_rib.beginRemoveFace(id); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800512 }
513}
514
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700515} // namespace nfd