blob: 41f9925614e0db631b2a57838eda7ad4bc496c04 [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
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050043using 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&) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500125 NFD_LOG_TRACE("Local fields enabled");
Junxiao Shi52009042018-09-10 12:33:56 +0000126 },
127 [] (const ControlResponse& res) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500128 NDN_THROW(Error("Could not enable local fields (error " +
129 std::to_string(res.getCode()) + ": " + 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
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500158 beginRibUpdate({rib::RibUpdate::REGISTER, name, route}, done);
Yanbiao Licf0db022016-01-29 00:54:25 -0800159}
160
161void
Junxiao Shi52009042018-09-10 12:33:56 +0000162RibManager::beginRemoveRoute(const Name& name, const Route& route,
163 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800164{
Junxiao Shi52009042018-09-10 12:33:56 +0000165 NFD_LOG_INFO("Removing route " << name << " nexthop=" << route.faceId <<
166 " origin=" << route.origin);
Yanbiao Licf0db022016-01-29 00:54:25 -0800167
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500168 beginRibUpdate({rib::RibUpdate::UNREGISTER, name, route}, done);
Junxiao Shi52009042018-09-10 12:33:56 +0000169}
170
171void
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500172RibManager::beginRibUpdate(const rib::RibUpdate& update,
Junxiao Shi52009042018-09-10 12:33:56 +0000173 const std::function<void(RibUpdateResult)>& done)
174{
175 m_rib.beginApplyUpdate(update,
176 [=] {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500177 NFD_LOG_DEBUG(update << " -> OK");
Junxiao Shi52009042018-09-10 12:33:56 +0000178 done(RibUpdateResult::OK);
179 },
180 [=] (uint32_t code, const std::string& error) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500181 NFD_LOG_DEBUG(update << " -> error " << code << ": " << error);
Junxiao Shi52009042018-09-10 12:33:56 +0000182
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>(
Davide Pesavento21e24f92025-01-10 22:22:43 -0500195 ControlParameters().setName(Name(topPrefix).append(MGMT_MODULE_NAME)).setFaceId(0),
Junxiao Shi52009042018-09-10 12:33:56 +0000196 [=] (const ControlParameters& res) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500197 NFD_LOG_DEBUG("Successfully registered " << topPrefix << " with the forwarder");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700198
Junxiao Shi52009042018-09-10 12:33:56 +0000199 // Routes must be inserted into the RIB so route flags can be applied
200 Route route;
201 route.faceId = res.getFaceId();
202 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
203 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
204
205 m_rib.insert(topPrefix, route);
Junxiao Shi52009042018-09-10 12:33:56 +0000206 },
207 [=] (const ControlResponse& res) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500208 NDN_THROW(Error("Could not add FIB entry " + topPrefix.toUri() + " (error " +
209 std::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
Davide Pesavento21e24f92025-01-10 22:22:43 -0500216static uint64_t
217getIncomingFaceId(const Interest& request)
218{
219 auto incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
220 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
221 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
222 // and is initialized synchronously with IncomingFaceId field enabled.
223 BOOST_ASSERT(incomingFaceIdTag != nullptr);
224 return incomingFaceIdTag->get();
225}
226
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700227void
Davide Pesaventoae430302023-05-11 01:42:46 -0400228RibManager::registerEntry(const Interest& interest, ControlParameters parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500229 const CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700230{
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400231 if (parameters.getName().size() > Fib::getMaxDepth()) {
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500232 done(ControlResponse(414, "Route prefix cannot exceed " + std::to_string(Fib::getMaxDepth()) +
Junxiao Shi75306352018-02-01 21:59:44 +0000233 " components"));
234 return;
235 }
236
Davide Pesavento21e24f92025-01-10 22:22:43 -0500237 if (parameters.getFaceId() == 0) { // self registration
238 parameters.setFaceId(getIncomingFaceId(interest));
239 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600240
241 // Respond since command is valid and authorized
Davide Pesavento21e24f92025-01-10 22:22:43 -0500242 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700243
Vince Lehman218be0a2015-01-15 17:25:20 -0600244 Route route;
245 route.faceId = parameters.getFaceId();
246 route.origin = parameters.getOrigin();
247 route.cost = parameters.getCost();
248 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500249
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400250 std::optional<time::nanoseconds> expires;
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700251 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000252 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Junxiao Shi52009042018-09-10 12:33:56 +0000253 expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod());
Vince Lehman76c751c2014-11-18 17:36:38 -0600254 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700255
Junxiao Shi52009042018-09-10 12:33:56 +0000256 beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {});
Vince Lehman281ded72014-08-21 12:17:08 -0500257}
258
259void
Davide Pesaventoae430302023-05-11 01:42:46 -0400260RibManager::unregisterEntry(const Interest& interest, ControlParameters parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500261 const CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700262{
Davide Pesavento21e24f92025-01-10 22:22:43 -0500263 if (parameters.getFaceId() == 0) { // self unregistration
264 parameters.setFaceId(getIncomingFaceId(interest));
265 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600266
267 // Respond since command is valid and authorized
Davide Pesavento21e24f92025-01-10 22:22:43 -0500268 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700269
Vince Lehman218be0a2015-01-15 17:25:20 -0600270 Route route;
271 route.faceId = parameters.getFaceId();
272 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700273
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400274 beginRemoveRoute(parameters.getName(), route, [] (auto&&...) {});
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700275}
276
277void
jaczhib0657682025-01-08 23:01:45 -0800278RibManager::announceEntry(const Interest& interest, const ndn::nfd::RibAnnounceParameters& parameters,
279 const CommandContinuation& done)
280{
281 const auto& announcement = parameters.getPrefixAnnouncement();
Davide Pesavento21e24f92025-01-10 22:22:43 -0500282 const auto& name = announcement.getAnnouncedName();
283 if (name.size() > Fib::getMaxDepth()) {
jaczhib0657682025-01-08 23:01:45 -0800284 done(ControlResponse(414, "Route prefix cannot exceed " + std::to_string(Fib::getMaxDepth()) +
285 " components"));
286 return;
287 }
288
Davide Pesavento21e24f92025-01-10 22:22:43 -0500289 Route route(announcement, getIncomingFaceId(interest));
290
jaczhib0657682025-01-08 23:01:45 -0800291 // Prepare parameters for response
292 ControlParameters responseParams;
jaczhib0657682025-01-08 23:01:45 -0800293 responseParams
Davide Pesavento21e24f92025-01-10 22:22:43 -0500294 .setName(name)
295 .setFaceId(route.faceId)
jaczhib0657682025-01-08 23:01:45 -0800296 .setOrigin(route.origin)
297 .setCost(route.cost)
298 .setFlags(route.flags)
299 .setExpirationPeriod(time::duration_cast<time::milliseconds>(route.annExpires - time::steady_clock::now()));
300
Davide Pesavento21e24f92025-01-10 22:22:43 -0500301 NDN_LOG_TRACE("Validating announcement " << announcement);
jaczhib0657682025-01-08 23:01:45 -0800302 BOOST_ASSERT(announcement.getData());
303 m_paValidator.validate(*announcement.getData(),
Davide Pesavento21e24f92025-01-10 22:22:43 -0500304 [=, route = std::move(route)] (const Data&) {
jaczhib0657682025-01-08 23:01:45 -0800305 // Respond since command is valid and authorized
Davide Pesavento21e24f92025-01-10 22:22:43 -0500306 done(ControlResponse(200, "OK").setBody(responseParams.wireEncode()));
jaczhib0657682025-01-08 23:01:45 -0800307 beginAddRoute(name, std::move(route), std::nullopt, [] (RibUpdateResult) {});
308 },
Davide Pesavento21e24f92025-01-10 22:22:43 -0500309 [=] (const Data&, const ndn::security::ValidationError& err) {
310 NDN_LOG_DEBUG("announce " << name << " -> " << err);
311 done(ControlResponse(403, "Prefix announcement rejected: " +
312 boost::lexical_cast<std::string>(err.getCode())));
313 });
jaczhib0657682025-01-08 23:01:45 -0800314}
315
316void
Davide Pesaventoae430302023-05-11 01:42:46 -0400317RibManager::listEntries(ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700318{
Junxiao Shi3f21e582017-05-29 15:26:32 +0000319 auto now = time::steady_clock::now();
320 for (const auto& kv : m_rib) {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500321 const rib::RibEntry& entry = *kv.second;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000322 ndn::nfd::RibEntry item;
323 item.setName(entry.getName());
324 for (const Route& route : entry.getRoutes()) {
325 ndn::nfd::Route r;
326 r.setFaceId(route.faceId);
327 r.setOrigin(route.origin);
328 r.setCost(route.cost);
329 r.setFlags(route.flags);
330 if (route.expires) {
331 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now));
332 }
333 item.addRoute(r);
334 }
335 context.append(item.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600336 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800337 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700338}
339
Junxiao Shi21738402016-08-19 19:48:00 +0000340ndn::mgmt::Authorization
Davide Pesavento412c9822021-07-02 00:21:05 -0400341RibManager::makeAuthorization(const std::string&)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700342{
Junxiao Shi21738402016-08-19 19:48:00 +0000343 return [this] (const Name& prefix, const Interest& interest,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500344 const ndn::mgmt::ControlParametersBase* params,
Junxiao Shi21738402016-08-19 19:48:00 +0000345 const ndn::mgmt::AcceptContinuation& accept,
346 const ndn::mgmt::RejectContinuation& reject) {
347 BOOST_ASSERT(params != nullptr);
jaczhib0657682025-01-08 23:01:45 -0800348 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters) ||
349 typeid(*params) == typeid(ndn::nfd::RibAnnounceParameters));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000350 BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700351
Davide Pesavento5a897692019-10-31 01:28:43 -0400352 auto& validator = prefix == LOCALHOST_TOP_PREFIX ? m_localhostValidator : m_localhopValidator;
Junxiao Shi21738402016-08-19 19:48:00 +0000353 validator.validate(interest,
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400354 [&interest, accept] (auto&&...) { accept(extractSigner(interest)); },
Davide Pesavento412c9822021-07-02 00:21:05 -0400355 [reject] (auto&&...) { reject(ndn::mgmt::RejectReply::STATUS403); });
Junxiao Shi21738402016-08-19 19:48:00 +0000356 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500357}
358
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000359std::ostream&
360operator<<(std::ostream& os, RibManager::SlAnnounceResult res)
361{
362 switch (res) {
Davide Pesavento5a897692019-10-31 01:28:43 -0400363 case RibManager::SlAnnounceResult::OK:
364 return os << "OK";
365 case RibManager::SlAnnounceResult::ERROR:
366 return os << "ERROR";
367 case RibManager::SlAnnounceResult::VALIDATION_FAILURE:
368 return os << "VALIDATION_FAILURE";
369 case RibManager::SlAnnounceResult::EXPIRED:
370 return os << "EXPIRED";
371 case RibManager::SlAnnounceResult::NOT_FOUND:
372 return os << "NOT_FOUND";
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000373 }
Davide Pesavento5a897692019-10-31 01:28:43 -0400374 NDN_THROW(std::invalid_argument("Unknown SlAnnounceResult"));
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000375}
376
377RibManager::SlAnnounceResult
378RibManager::getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r)
379{
380 switch (r) {
381 case RibUpdateResult::OK:
382 return SlAnnounceResult::OK;
383 case RibUpdateResult::ERROR:
384 return SlAnnounceResult::ERROR;
385 case RibUpdateResult::EXPIRED:
386 return SlAnnounceResult::EXPIRED;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000387 }
Davide Pesavento5a897692019-10-31 01:28:43 -0400388 NDN_CXX_UNREACHABLE;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000389}
390
391void
392RibManager::slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId,
393 time::milliseconds maxLifetime, const SlAnnounceCallback& cb)
394{
395 BOOST_ASSERT(pa.getData());
396
Teng Liang18c2b292019-10-18 14:31:04 -0700397 m_paValidator.validate(*pa.getData(),
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000398 [=] (const Data&) {
399 Route route(pa, faceId);
400 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400401 beginAddRoute(pa.getAnnouncedName(), route, std::nullopt,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000402 [=] (RibUpdateResult ribRes) {
403 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500404 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << " -> " << res);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000405 cb(res);
406 });
407 },
Davide Pesavento21e24f92025-01-10 22:22:43 -0500408 [=] (const Data&, const ndn::security::ValidationError& err) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700409 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Davide Pesavento21e24f92025-01-10 22:22:43 -0500410 " -> validation error: " << err);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000411 cb(SlAnnounceResult::VALIDATION_FAILURE);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500412 });
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000413}
414
415void
416RibManager::slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
417 const SlAnnounceCallback& cb)
418{
419 Route routeQuery;
420 routeQuery.faceId = faceId;
421 routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN;
Teng Lianga4e6ec32018-10-21 09:25:00 -0700422 Route* oldRoute = m_rib.findLongestPrefix(name, routeQuery);
423
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000424 if (oldRoute == nullptr || !oldRoute->announcement) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500425 NFD_LOG_DEBUG("slRenew " << name << " " << faceId << " -> not found");
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000426 return cb(SlAnnounceResult::NOT_FOUND);
427 }
Teng Lianga4e6ec32018-10-21 09:25:00 -0700428 Name routeName = oldRoute->announcement->getAnnouncedName();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000429
430 Route route = *oldRoute;
431 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400432 beginAddRoute(routeName, route, std::nullopt,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000433 [=] (RibUpdateResult ribRes) {
434 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500435 NFD_LOG_INFO("slRenew " << name << " " << faceId << " -> " << res << " " << routeName);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000436 cb(res);
437 });
438}
439
440void
441RibManager::slFindAnn(const Name& name, const SlFindAnnCallback& cb) const
442{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500443 shared_ptr<rib::RibEntry> entry;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000444 auto exactMatch = m_rib.find(name);
445 if (exactMatch != m_rib.end()) {
446 entry = exactMatch->second;
447 }
448 else {
449 entry = m_rib.findParent(name);
450 }
451 if (entry == nullptr) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400452 return cb(std::nullopt);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000453 }
454
455 auto pa = entry->getPrefixAnnouncement();
456 pa.toData(m_keyChain);
457 cb(pa);
458}
459
Vince Lehman26b215c2014-08-17 15:00:41 -0500460void
Vince Lehmancd613c52014-07-30 14:34:49 -0500461RibManager::fetchActiveFaces()
462{
463 NFD_LOG_DEBUG("Fetching active faces");
464
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700465 m_nfdController.fetch<ndn::nfd::FaceDataset>(
Davide Pesaventoae430302023-05-11 01:42:46 -0400466 [this] (auto&&... args) { removeInvalidFaces(std::forward<decltype(args)>(args)...); },
Davide Pesavento21e24f92025-01-10 22:22:43 -0500467 [this] (uint32_t code, const std::string& reason) {
468 NFD_LOG_WARN("Failed to fetch face dataset (error " << code << ": " << reason << ")");
469 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
470 });
Vince Lehmancd613c52014-07-30 14:34:49 -0500471}
472
473void
Davide Pesavento21e24f92025-01-10 22:22:43 -0500474RibManager::scheduleActiveFaceFetch(time::seconds timeToWait)
Yanbiao Licf0db022016-01-29 00:54:25 -0800475{
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400476 m_activeFaceFetchEvent = getScheduler().schedule(timeToWait, [this] { fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800477}
478
479void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700480RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500481{
Vince Lehman26b215c2014-08-17 15:00:41 -0500482 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500483
Davide Pesavento5d642632023-10-03 00:36:08 -0400484 std::set<uint64_t> activeIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500485 for (const auto& faceStatus : activeFaces) {
Davide Pesavento5d642632023-10-03 00:36:08 -0400486 activeIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700487 }
Davide Pesavento5d642632023-10-03 00:36:08 -0400488 boost::asio::defer(getGlobalIoService(),
489 [this, active = std::move(activeIds)] { m_rib.beginRemoveFailedFaces(active); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500490
491 // Reschedule the check for future clean up
492 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500493}
494
495void
Davide Pesaventod396b612017-02-20 22:11:50 -0500496RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500497{
Davide Pesavento21e24f92025-01-10 22:22:43 -0500498 NFD_LOG_TRACE("Received notification " << notification);
Yanbiao Licf0db022016-01-29 00:54:25 -0800499
500 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
Davide Pesavento5d642632023-10-03 00:36:08 -0400501 boost::asio::defer(getGlobalIoService(),
502 [this, id = notification.getFaceId()] { m_rib.beginRemoveFace(id); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800503 }
504}
505
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700506} // namespace nfd