blob: f05ca46d0aeec914f70a5639cd8f50869762076d [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 Pesaventoa7450252025-01-12 00:20:01 -0500134RibManager::addRoute(const Name& name, Route route, const time::steady_clock::time_point& now,
135 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800136{
Davide Pesaventoa7450252025-01-12 00:20:01 -0500137 if (route.expires && *route.expires <= now) {
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000138 m_rib.onRouteExpiration(name, route);
Davide Pesaventoa7450252025-01-12 00:20:01 -0500139 if (done) {
140 done(RibUpdateResult::EXPIRED);
141 }
142 return;
Junxiao Shi52009042018-09-10 12:33:56 +0000143 }
144
145 NFD_LOG_INFO("Adding route " << name << " nexthop=" << route.faceId <<
146 " origin=" << route.origin << " cost=" << route.cost);
147
Davide Pesaventoa7450252025-01-12 00:20:01 -0500148 if (route.expires) {
149 auto delay = *route.expires - now;
150 auto event = getScheduler().schedule(delay, [=] { m_rib.onRouteExpiration(name, route); });
151 route.setExpirationEvent(std::move(event));
152 // cast to milliseconds to make the logs easier to read
153 NFD_LOG_TRACE("Route will expire in " << time::duration_cast<time::milliseconds>(delay));
Junxiao Shi52009042018-09-10 12:33:56 +0000154 }
155
Davide Pesaventoa7450252025-01-12 00:20:01 -0500156 beginRibUpdate({rib::RibUpdate::REGISTER, name, std::move(route)}, done);
Junxiao Shi52009042018-09-10 12:33:56 +0000157}
158
159void
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500160RibManager::beginRibUpdate(const rib::RibUpdate& update,
Junxiao Shi52009042018-09-10 12:33:56 +0000161 const std::function<void(RibUpdateResult)>& done)
162{
163 m_rib.beginApplyUpdate(update,
164 [=] {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500165 NFD_LOG_DEBUG(update << " -> OK");
Davide Pesaventoa7450252025-01-12 00:20:01 -0500166 if (done) {
167 done(RibUpdateResult::OK);
168 }
Junxiao Shi52009042018-09-10 12:33:56 +0000169 },
170 [=] (uint32_t code, const std::string& error) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500171 NFD_LOG_DEBUG(update << " -> error " << code << ": " << error);
Junxiao Shi52009042018-09-10 12:33:56 +0000172
173 // Since the FIB rejected the update, clean up invalid routes
174 scheduleActiveFaceFetch(1_s);
175
Davide Pesaventoa7450252025-01-12 00:20:01 -0500176 if (done) {
177 done(RibUpdateResult::ERROR);
178 }
Junxiao Shi52009042018-09-10 12:33:56 +0000179 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800180}
181
182void
Yanbiao Licf0db022016-01-29 00:54:25 -0800183RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700184{
Junxiao Shi52009042018-09-10 12:33:56 +0000185 // add FIB nexthop
Yanbiao Licf0db022016-01-29 00:54:25 -0800186 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Davide Pesavento21e24f92025-01-10 22:22:43 -0500187 ControlParameters().setName(Name(topPrefix).append(MGMT_MODULE_NAME)).setFaceId(0),
Junxiao Shi52009042018-09-10 12:33:56 +0000188 [=] (const ControlParameters& res) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500189 NFD_LOG_DEBUG("Successfully registered " << topPrefix << " with the forwarder");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700190
Junxiao Shi52009042018-09-10 12:33:56 +0000191 // Routes must be inserted into the RIB so route flags can be applied
192 Route route;
193 route.faceId = res.getFaceId();
194 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
195 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
196
197 m_rib.insert(topPrefix, route);
Junxiao Shi52009042018-09-10 12:33:56 +0000198 },
199 [=] (const ControlResponse& res) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500200 NDN_THROW(Error("Could not add FIB entry " + topPrefix.toUri() + " (error " +
201 std::to_string(res.getCode()) + ": " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000202 });
203
204 // add top prefix to the dispatcher without prefix registration
Junxiao Shif4cfed12018-08-22 23:26:29 +0000205 m_dispatcher.addTopPrefix(topPrefix, false);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700206}
207
Davide Pesavento21e24f92025-01-10 22:22:43 -0500208static uint64_t
209getIncomingFaceId(const Interest& request)
210{
211 auto incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
212 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
213 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
214 // and is initialized synchronously with IncomingFaceId field enabled.
215 BOOST_ASSERT(incomingFaceIdTag != nullptr);
216 return incomingFaceIdTag->get();
217}
218
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700219void
Davide Pesaventoae430302023-05-11 01:42:46 -0400220RibManager::registerEntry(const Interest& interest, ControlParameters parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500221 const CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700222{
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400223 if (parameters.getName().size() > Fib::getMaxDepth()) {
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500224 done(ControlResponse(414, "Route prefix cannot exceed " + std::to_string(Fib::getMaxDepth()) +
Junxiao Shi75306352018-02-01 21:59:44 +0000225 " components"));
226 return;
227 }
228
Davide Pesavento21e24f92025-01-10 22:22:43 -0500229 if (parameters.getFaceId() == 0) { // self registration
230 parameters.setFaceId(getIncomingFaceId(interest));
231 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600232
233 // Respond since command is valid and authorized
Davide Pesavento21e24f92025-01-10 22:22:43 -0500234 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700235
Vince Lehman218be0a2015-01-15 17:25:20 -0600236 Route route;
237 route.faceId = parameters.getFaceId();
238 route.origin = parameters.getOrigin();
239 route.cost = parameters.getCost();
240 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500241
Davide Pesaventoa7450252025-01-12 00:20:01 -0500242 auto now = time::steady_clock::now();
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700243 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000244 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Davide Pesaventoa7450252025-01-12 00:20:01 -0500245 route.expires = now + parameters.getExpirationPeriod();
Vince Lehman76c751c2014-11-18 17:36:38 -0600246 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700247
Davide Pesaventoa7450252025-01-12 00:20:01 -0500248 addRoute(parameters.getName(), std::move(route), now);
Vince Lehman281ded72014-08-21 12:17:08 -0500249}
250
251void
Davide Pesaventoae430302023-05-11 01:42:46 -0400252RibManager::unregisterEntry(const Interest& interest, ControlParameters parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500253 const CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700254{
Davide Pesavento21e24f92025-01-10 22:22:43 -0500255 if (parameters.getFaceId() == 0) { // self unregistration
256 parameters.setFaceId(getIncomingFaceId(interest));
257 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600258
259 // Respond since command is valid and authorized
Davide Pesavento21e24f92025-01-10 22:22:43 -0500260 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700261
Vince Lehman218be0a2015-01-15 17:25:20 -0600262 Route route;
263 route.faceId = parameters.getFaceId();
264 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700265
Davide Pesaventoa7450252025-01-12 00:20:01 -0500266 NFD_LOG_INFO("Removing route " << parameters.getName() <<
267 " nexthop=" << route.faceId << " origin=" << route.origin);
268
269 beginRibUpdate({rib::RibUpdate::UNREGISTER, parameters.getName(), route}, nullptr);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700270}
271
272void
jaczhib0657682025-01-08 23:01:45 -0800273RibManager::announceEntry(const Interest& interest, const ndn::nfd::RibAnnounceParameters& parameters,
274 const CommandContinuation& done)
275{
276 const auto& announcement = parameters.getPrefixAnnouncement();
Davide Pesavento21e24f92025-01-10 22:22:43 -0500277 const auto& name = announcement.getAnnouncedName();
278 if (name.size() > Fib::getMaxDepth()) {
jaczhib0657682025-01-08 23:01:45 -0800279 done(ControlResponse(414, "Route prefix cannot exceed " + std::to_string(Fib::getMaxDepth()) +
280 " components"));
281 return;
282 }
283
Davide Pesaventoa7450252025-01-12 00:20:01 -0500284 auto inFaceId = getIncomingFaceId(interest);
jaczhib0657682025-01-08 23:01:45 -0800285
Davide Pesavento21e24f92025-01-10 22:22:43 -0500286 NDN_LOG_TRACE("Validating announcement " << announcement);
jaczhib0657682025-01-08 23:01:45 -0800287 BOOST_ASSERT(announcement.getData());
288 m_paValidator.validate(*announcement.getData(),
Davide Pesaventoa7450252025-01-12 00:20:01 -0500289 [=] (const Data&) {
290 auto now = time::steady_clock::now();
291 Route route(announcement, inFaceId);
292
293 // Prepare parameters for response
294 ControlParameters responseParams;
295 responseParams
296 .setName(announcement.getAnnouncedName())
297 .setFaceId(route.faceId)
298 .setOrigin(route.origin)
299 .setCost(route.cost)
300 .setFlags(route.flags)
301 .setExpirationPeriod(time::duration_cast<time::milliseconds>(route.annExpires - now));
302
jaczhib0657682025-01-08 23:01:45 -0800303 // Respond since command is valid and authorized
Davide Pesavento21e24f92025-01-10 22:22:43 -0500304 done(ControlResponse(200, "OK").setBody(responseParams.wireEncode()));
Davide Pesaventoa7450252025-01-12 00:20:01 -0500305 addRoute(announcement.getAnnouncedName(), std::move(route), now);
jaczhib0657682025-01-08 23:01:45 -0800306 },
Davide Pesavento21e24f92025-01-10 22:22:43 -0500307 [=] (const Data&, const ndn::security::ValidationError& err) {
308 NDN_LOG_DEBUG("announce " << name << " -> " << err);
309 done(ControlResponse(403, "Prefix announcement rejected: " +
310 boost::lexical_cast<std::string>(err.getCode())));
311 });
jaczhib0657682025-01-08 23:01:45 -0800312}
313
314void
Davide Pesaventoa7450252025-01-12 00:20:01 -0500315RibManager::listEntries(ndn::mgmt::StatusDatasetContext& context) const
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700316{
Junxiao Shi3f21e582017-05-29 15:26:32 +0000317 auto now = time::steady_clock::now();
318 for (const auto& kv : m_rib) {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500319 const rib::RibEntry& entry = *kv.second;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000320 ndn::nfd::RibEntry item;
321 item.setName(entry.getName());
322 for (const Route& route : entry.getRoutes()) {
323 ndn::nfd::Route r;
324 r.setFaceId(route.faceId);
325 r.setOrigin(route.origin);
326 r.setCost(route.cost);
327 r.setFlags(route.flags);
328 if (route.expires) {
329 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now));
330 }
331 item.addRoute(r);
332 }
333 context.append(item.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600334 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800335 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700336}
337
Junxiao Shi21738402016-08-19 19:48:00 +0000338ndn::mgmt::Authorization
Davide Pesavento412c9822021-07-02 00:21:05 -0400339RibManager::makeAuthorization(const std::string&)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700340{
Junxiao Shi21738402016-08-19 19:48:00 +0000341 return [this] (const Name& prefix, const Interest& interest,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500342 const ndn::mgmt::ControlParametersBase* params,
Junxiao Shi21738402016-08-19 19:48:00 +0000343 const ndn::mgmt::AcceptContinuation& accept,
344 const ndn::mgmt::RejectContinuation& reject) {
345 BOOST_ASSERT(params != nullptr);
jaczhib0657682025-01-08 23:01:45 -0800346 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters) ||
347 typeid(*params) == typeid(ndn::nfd::RibAnnounceParameters));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000348 BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700349
Davide Pesavento5a897692019-10-31 01:28:43 -0400350 auto& validator = prefix == LOCALHOST_TOP_PREFIX ? m_localhostValidator : m_localhopValidator;
Junxiao Shi21738402016-08-19 19:48:00 +0000351 validator.validate(interest,
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400352 [&interest, accept] (auto&&...) { accept(extractSigner(interest)); },
Davide Pesavento412c9822021-07-02 00:21:05 -0400353 [reject] (auto&&...) { reject(ndn::mgmt::RejectReply::STATUS403); });
Junxiao Shi21738402016-08-19 19:48:00 +0000354 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500355}
356
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000357std::ostream&
358operator<<(std::ostream& os, RibManager::SlAnnounceResult res)
359{
360 switch (res) {
Davide Pesavento5a897692019-10-31 01:28:43 -0400361 case RibManager::SlAnnounceResult::OK:
362 return os << "OK";
363 case RibManager::SlAnnounceResult::ERROR:
364 return os << "ERROR";
365 case RibManager::SlAnnounceResult::VALIDATION_FAILURE:
366 return os << "VALIDATION_FAILURE";
367 case RibManager::SlAnnounceResult::EXPIRED:
368 return os << "EXPIRED";
369 case RibManager::SlAnnounceResult::NOT_FOUND:
370 return os << "NOT_FOUND";
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000371 }
Davide Pesavento5a897692019-10-31 01:28:43 -0400372 NDN_THROW(std::invalid_argument("Unknown SlAnnounceResult"));
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000373}
374
375RibManager::SlAnnounceResult
376RibManager::getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r)
377{
378 switch (r) {
379 case RibUpdateResult::OK:
380 return SlAnnounceResult::OK;
381 case RibUpdateResult::ERROR:
382 return SlAnnounceResult::ERROR;
383 case RibUpdateResult::EXPIRED:
384 return SlAnnounceResult::EXPIRED;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000385 }
Davide Pesavento5a897692019-10-31 01:28:43 -0400386 NDN_CXX_UNREACHABLE;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000387}
388
389void
390RibManager::slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId,
391 time::milliseconds maxLifetime, const SlAnnounceCallback& cb)
392{
393 BOOST_ASSERT(pa.getData());
394
Teng Liang18c2b292019-10-18 14:31:04 -0700395 m_paValidator.validate(*pa.getData(),
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000396 [=] (const Data&) {
Davide Pesaventoa7450252025-01-12 00:20:01 -0500397 auto now = time::steady_clock::now();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000398 Route route(pa, faceId);
Davide Pesaventoa7450252025-01-12 00:20:01 -0500399 route.expires = std::min(route.annExpires, now + maxLifetime);
400 addRoute(pa.getAnnouncedName(), std::move(route), now, [=] (RibUpdateResult ribRes) {
401 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
402 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << " -> " << res);
403 cb(res);
404 });
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000405 },
Davide Pesavento21e24f92025-01-10 22:22:43 -0500406 [=] (const Data&, const ndn::security::ValidationError& err) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700407 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Davide Pesavento21e24f92025-01-10 22:22:43 -0500408 " -> validation error: " << err);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000409 cb(SlAnnounceResult::VALIDATION_FAILURE);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500410 });
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000411}
412
413void
414RibManager::slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
415 const SlAnnounceCallback& cb)
416{
417 Route routeQuery;
418 routeQuery.faceId = faceId;
419 routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN;
Teng Lianga4e6ec32018-10-21 09:25:00 -0700420 Route* oldRoute = m_rib.findLongestPrefix(name, routeQuery);
421
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000422 if (oldRoute == nullptr || !oldRoute->announcement) {
Davide Pesavento21e24f92025-01-10 22:22:43 -0500423 NFD_LOG_DEBUG("slRenew " << name << " " << faceId << " -> not found");
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000424 return cb(SlAnnounceResult::NOT_FOUND);
425 }
426
Davide Pesaventoa7450252025-01-12 00:20:01 -0500427 auto now = time::steady_clock::now();
428 Name routeName = oldRoute->announcement->getAnnouncedName();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000429 Route route = *oldRoute;
Davide Pesaventoa7450252025-01-12 00:20:01 -0500430 route.expires = std::min(route.annExpires, now + maxLifetime);
431
432 addRoute(routeName, std::move(route), now, [=] (RibUpdateResult ribRes) {
433 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
434 NFD_LOG_INFO("slRenew " << name << " " << faceId << " -> " << res << " " << routeName);
435 cb(res);
436 });
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000437}
438
439void
440RibManager::slFindAnn(const Name& name, const SlFindAnnCallback& cb) const
441{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500442 shared_ptr<rib::RibEntry> entry;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000443 auto exactMatch = m_rib.find(name);
444 if (exactMatch != m_rib.end()) {
445 entry = exactMatch->second;
446 }
447 else {
448 entry = m_rib.findParent(name);
449 }
450 if (entry == nullptr) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400451 return cb(std::nullopt);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000452 }
453
454 auto pa = entry->getPrefixAnnouncement();
455 pa.toData(m_keyChain);
456 cb(pa);
457}
458
Vince Lehman26b215c2014-08-17 15:00:41 -0500459void
Vince Lehmancd613c52014-07-30 14:34:49 -0500460RibManager::fetchActiveFaces()
461{
462 NFD_LOG_DEBUG("Fetching active faces");
463
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700464 m_nfdController.fetch<ndn::nfd::FaceDataset>(
Davide Pesaventoae430302023-05-11 01:42:46 -0400465 [this] (auto&&... args) { removeInvalidFaces(std::forward<decltype(args)>(args)...); },
Davide Pesavento21e24f92025-01-10 22:22:43 -0500466 [this] (uint32_t code, const std::string& reason) {
467 NFD_LOG_WARN("Failed to fetch face dataset (error " << code << ": " << reason << ")");
468 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
469 });
Vince Lehmancd613c52014-07-30 14:34:49 -0500470}
471
472void
Davide Pesavento21e24f92025-01-10 22:22:43 -0500473RibManager::scheduleActiveFaceFetch(time::seconds timeToWait)
Yanbiao Licf0db022016-01-29 00:54:25 -0800474{
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400475 m_activeFaceFetchEvent = getScheduler().schedule(timeToWait, [this] { fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800476}
477
478void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700479RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500480{
Vince Lehman26b215c2014-08-17 15:00:41 -0500481 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500482
Davide Pesavento5d642632023-10-03 00:36:08 -0400483 std::set<uint64_t> activeIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500484 for (const auto& faceStatus : activeFaces) {
Davide Pesavento5d642632023-10-03 00:36:08 -0400485 activeIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700486 }
Davide Pesavento5d642632023-10-03 00:36:08 -0400487 boost::asio::defer(getGlobalIoService(),
488 [this, active = std::move(activeIds)] { m_rib.beginRemoveFailedFaces(active); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500489
490 // Reschedule the check for future clean up
491 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500492}
493
494void
Davide Pesaventod396b612017-02-20 22:11:50 -0500495RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500496{
Davide Pesavento21e24f92025-01-10 22:22:43 -0500497 NFD_LOG_TRACE("Received notification " << notification);
Yanbiao Licf0db022016-01-29 00:54:25 -0800498
499 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
Davide Pesavento5d642632023-10-03 00:36:08 -0400500 boost::asio::defer(getGlobalIoService(),
501 [this, id = notification.getFaceId()] { m_rib.beginRemoveFace(id); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800502 }
503}
504
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700505} // namespace nfd