Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 7530635 | 2018-02-01 21:59:44 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 4064127 | 2023-03-16 13:31:12 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 4 | * 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 Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 10 | * |
| 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/>. |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 25 | |
| 26 | #include "rib-manager.hpp" |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame] | 27 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 28 | #include "common/global.hpp" |
| 29 | #include "common/logger.hpp" |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 30 | #include "rib/rib.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 31 | #include "table/fib.hpp" |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame] | 32 | |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/lp/tags.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 34 | #include <ndn-cxx/mgmt/nfd/rib-entry.hpp> |
Davide Pesavento | 4064127 | 2023-03-16 13:31:12 -0400 | [diff] [blame] | 35 | #include <ndn-cxx/mgmt/nfd/status-dataset.hpp> |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 36 | #include <ndn-cxx/security/certificate-fetcher-direct-fetch.hpp> |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 37 | |
| 38 | namespace nfd { |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 39 | |
| 40 | using rib::RibUpdate; |
| 41 | using rib::Route; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 42 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 43 | NFD_LOG_INIT(RibManager); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 44 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 45 | const std::string MGMT_MODULE_NAME = "rib"; |
| 46 | const Name LOCALHOST_TOP_PREFIX = "/localhost/nfd"; |
Davide Pesavento | 20d94f6 | 2022-09-26 03:30:53 -0400 | [diff] [blame] | 47 | constexpr time::seconds ACTIVE_FACE_FETCH_INTERVAL = 5_min; |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 48 | |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 49 | RibManager::RibManager(rib::Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain, |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 50 | ndn::nfd::Controller& nfdController, Dispatcher& dispatcher) |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 51 | : ManagerBase(MGMT_MODULE_NAME, dispatcher) |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 52 | , m_rib(rib) |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 53 | , m_keyChain(keyChain) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 54 | , m_nfdController(nfdController) |
| 55 | , m_dispatcher(dispatcher) |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 56 | , m_faceMonitor(face) |
| 57 | , m_localhostValidator(face) |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 58 | , m_localhopValidator(make_unique<ndn::security::CertificateFetcherDirectFetch>(face)) |
| 59 | , m_paValidator(make_unique<ndn::security::CertificateFetcherDirectFetch>(face)) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 60 | , m_isLocalhopEnabled(false) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 61 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 62 | registerCommandHandler<ndn::nfd::RibRegisterCommand>("register", |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 63 | [this] (auto&&, auto&&, auto&&... args) { registerEntry(std::forward<decltype(args)>(args)...); }); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 64 | registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister", |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 65 | [this] (auto&&, auto&&, auto&&... args) { unregisterEntry(std::forward<decltype(args)>(args)...); }); |
| 66 | registerStatusDatasetHandler("list", |
| 67 | [this] (auto&&, auto&&, auto&&... args) { listEntries(std::forward<decltype(args)>(args)...); }); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 70 | void |
| 71 | RibManager::applyLocalhostConfig(const ConfigSection& section, const std::string& filename) |
| 72 | { |
| 73 | m_localhostValidator.load(section, filename); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | RibManager::enableLocalhop(const ConfigSection& section, const std::string& filename) |
| 78 | { |
| 79 | m_localhopValidator.load(section, filename); |
| 80 | m_isLocalhopEnabled = true; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | RibManager::disableLocalhop() |
| 85 | { |
| 86 | m_isLocalhopEnabled = false; |
| 87 | } |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 88 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 89 | void |
Teng Liang | 18c2b29 | 2019-10-18 14:31:04 -0700 | [diff] [blame] | 90 | RibManager::applyPaConfig(const ConfigSection& section, const std::string& filename) |
| 91 | { |
| 92 | m_paValidator.load(section, filename); |
| 93 | } |
| 94 | |
| 95 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 96 | RibManager::registerWithNfd() |
| 97 | { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 98 | registerTopPrefix(LOCALHOST_TOP_PREFIX); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 99 | |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 100 | if (m_isLocalhopEnabled) { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 101 | registerTopPrefix(LOCALHOP_TOP_PREFIX); |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 102 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 103 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 104 | NFD_LOG_INFO("Start monitoring face create/destroy events"); |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 105 | m_faceMonitor.onNotification.connect([this] (const auto& notif) { onNotification(notif); }); |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 106 | m_faceMonitor.start(); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 107 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 108 | scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 112 | RibManager::enableLocalFields() |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 113 | { |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 114 | m_nfdController.start<ndn::nfd::FaceUpdateCommand>( |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 115 | ControlParameters().setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true), |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 116 | [] (const ControlParameters&) { |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 117 | NFD_LOG_DEBUG("Local fields enabled"); |
| 118 | }, |
| 119 | [] (const ControlResponse& res) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 120 | NDN_THROW(Error("Couldn't enable local fields (" + to_string(res.getCode()) + |
| 121 | " " + res.getText() + ")")); |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 122 | }); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 126 | RibManager::beginAddRoute(const Name& name, Route route, std::optional<time::nanoseconds> expires, |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 127 | const std::function<void(RibUpdateResult)>& done) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 128 | { |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 129 | if (expires) { |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 130 | route.expires = time::steady_clock::now() + *expires; |
| 131 | } |
| 132 | else if (route.expires) { |
| 133 | expires = *route.expires - time::steady_clock::now(); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | if (expires && *expires <= 0_s) { |
| 137 | m_rib.onRouteExpiration(name, route); |
| 138 | return done(RibUpdateResult::EXPIRED); |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | NFD_LOG_INFO("Adding route " << name << " nexthop=" << route.faceId << |
| 142 | " origin=" << route.origin << " cost=" << route.cost); |
| 143 | |
| 144 | if (expires) { |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 145 | auto event = getScheduler().schedule(*expires, [=] { m_rib.onRouteExpiration(name, route); }); |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame] | 146 | route.setExpirationEvent(event); |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 147 | NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires); |
| 148 | } |
| 149 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 150 | RibUpdate update; |
| 151 | update.setAction(RibUpdate::REGISTER) |
| 152 | .setName(name) |
| 153 | .setRoute(route); |
| 154 | beginRibUpdate(update, done); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 158 | RibManager::beginRemoveRoute(const Name& name, const Route& route, |
| 159 | const std::function<void(RibUpdateResult)>& done) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 160 | { |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 161 | NFD_LOG_INFO("Removing route " << name << " nexthop=" << route.faceId << |
| 162 | " origin=" << route.origin); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 163 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 164 | RibUpdate update; |
| 165 | update.setAction(RibUpdate::UNREGISTER) |
| 166 | .setName(name) |
| 167 | .setRoute(route); |
| 168 | beginRibUpdate(update, done); |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | RibManager::beginRibUpdate(const RibUpdate& update, |
| 173 | const std::function<void(RibUpdateResult)>& done) |
| 174 | { |
| 175 | m_rib.beginApplyUpdate(update, |
| 176 | [=] { |
| 177 | NFD_LOG_DEBUG("RIB update succeeded for " << update); |
| 178 | done(RibUpdateResult::OK); |
| 179 | }, |
| 180 | [=] (uint32_t code, const std::string& error) { |
| 181 | NFD_LOG_DEBUG("RIB update failed for " << update << " (" << code << " " << error << ")"); |
| 182 | |
| 183 | // Since the FIB rejected the update, clean up invalid routes |
| 184 | scheduleActiveFaceFetch(1_s); |
| 185 | |
| 186 | done(RibUpdateResult::ERROR); |
| 187 | }); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 191 | RibManager::registerTopPrefix(const Name& topPrefix) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 192 | { |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 193 | // add FIB nexthop |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 194 | m_nfdController.start<ndn::nfd::FibAddNextHopCommand>( |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 195 | ControlParameters().setName(Name(topPrefix).append(MGMT_MODULE_NAME)) |
| 196 | .setFaceId(0), |
| 197 | [=] (const ControlParameters& res) { |
| 198 | NFD_LOG_DEBUG("Successfully registered " << topPrefix << " with NFD"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 199 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 200 | // Routes must be inserted into the RIB so route flags can be applied |
| 201 | Route route; |
| 202 | route.faceId = res.getFaceId(); |
| 203 | route.origin = ndn::nfd::ROUTE_ORIGIN_APP; |
| 204 | route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT; |
| 205 | |
| 206 | m_rib.insert(topPrefix, route); |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 207 | }, |
| 208 | [=] (const ControlResponse& res) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 209 | NDN_THROW(Error("Cannot add FIB entry " + topPrefix.toUri() + " (" + |
| 210 | to_string(res.getCode()) + " " + res.getText() + ")")); |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 211 | }); |
| 212 | |
| 213 | // add top prefix to the dispatcher without prefix registration |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 214 | m_dispatcher.addTopPrefix(topPrefix, false); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 218 | RibManager::registerEntry(const Interest& interest, ControlParameters parameters, |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 219 | const ndn::mgmt::CommandContinuation& done) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 220 | { |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 221 | if (parameters.getName().size() > Fib::getMaxDepth()) { |
| 222 | done(ControlResponse(414, "Route prefix cannot exceed " + to_string(Fib::getMaxDepth()) + |
Junxiao Shi | 7530635 | 2018-02-01 21:59:44 +0000 | [diff] [blame] | 223 | " components")); |
| 224 | return; |
| 225 | } |
| 226 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 227 | setFaceForSelfRegistration(interest, parameters); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 228 | |
| 229 | // Respond since command is valid and authorized |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 230 | done(ControlResponse(200, "Success").setBody(parameters.wireEncode())); |
Alexander Afanasyev | fb1c808 | 2014-07-17 15:16:15 -0700 | [diff] [blame] | 231 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 232 | Route route; |
| 233 | route.faceId = parameters.getFaceId(); |
| 234 | route.origin = parameters.getOrigin(); |
| 235 | route.cost = parameters.getCost(); |
| 236 | route.flags = parameters.getFlags(); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 237 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 238 | std::optional<time::nanoseconds> expires; |
Alexander Afanasyev | f67cf08 | 2014-07-18 16:47:29 -0700 | [diff] [blame] | 239 | if (parameters.hasExpirationPeriod() && |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame] | 240 | parameters.getExpirationPeriod() != time::milliseconds::max()) { |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 241 | expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod()); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 242 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 243 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 244 | beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {}); |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 248 | RibManager::unregisterEntry(const Interest& interest, ControlParameters parameters, |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 249 | const ndn::mgmt::CommandContinuation& done) |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 250 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 251 | setFaceForSelfRegistration(interest, parameters); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 252 | |
| 253 | // Respond since command is valid and authorized |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 254 | done(ControlResponse(200, "Success").setBody(parameters.wireEncode())); |
Alexander Afanasyev | fb1c808 | 2014-07-17 15:16:15 -0700 | [diff] [blame] | 255 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 256 | Route route; |
| 257 | route.faceId = parameters.getFaceId(); |
| 258 | route.origin = parameters.getOrigin(); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 259 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 260 | beginRemoveRoute(parameters.getName(), route, [] (auto&&...) {}); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 264 | RibManager::listEntries(ndn::mgmt::StatusDatasetContext& context) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 265 | { |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 266 | auto now = time::steady_clock::now(); |
| 267 | for (const auto& kv : m_rib) { |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 268 | const rib::RibEntry& entry = *kv.second; |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 269 | ndn::nfd::RibEntry item; |
| 270 | item.setName(entry.getName()); |
| 271 | for (const Route& route : entry.getRoutes()) { |
| 272 | ndn::nfd::Route r; |
| 273 | r.setFaceId(route.faceId); |
| 274 | r.setOrigin(route.origin); |
| 275 | r.setCost(route.cost); |
| 276 | r.setFlags(route.flags); |
| 277 | if (route.expires) { |
| 278 | r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now)); |
| 279 | } |
| 280 | item.addRoute(r); |
| 281 | } |
| 282 | context.append(item.wireEncode()); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 283 | } |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 284 | context.end(); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 288 | RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 289 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 290 | bool isSelfRegistration = (parameters.getFaceId() == 0); |
| 291 | if (isSelfRegistration) { |
| 292 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>(); |
| 293 | // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field", |
| 294 | // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD |
| 295 | // and is initialized synchronously with IncomingFaceId field enabled. |
| 296 | BOOST_ASSERT(incomingFaceIdTag != nullptr); |
| 297 | parameters.setFaceId(*incomingFaceIdTag); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 298 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 301 | ndn::mgmt::Authorization |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 302 | RibManager::makeAuthorization(const std::string&) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 303 | { |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 304 | return [this] (const Name& prefix, const Interest& interest, |
| 305 | const ndn::mgmt::ControlParameters* params, |
| 306 | const ndn::mgmt::AcceptContinuation& accept, |
| 307 | const ndn::mgmt::RejectContinuation& reject) { |
| 308 | BOOST_ASSERT(params != nullptr); |
| 309 | BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters)); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 310 | BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 311 | |
Davide Pesavento | 5a89769 | 2019-10-31 01:28:43 -0400 | [diff] [blame] | 312 | auto& validator = prefix == LOCALHOST_TOP_PREFIX ? m_localhostValidator : m_localhopValidator; |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 313 | validator.validate(interest, |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 314 | [&interest, accept] (auto&&...) { accept(extractSigner(interest)); }, |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 315 | [reject] (auto&&...) { reject(ndn::mgmt::RejectReply::STATUS403); }); |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 316 | }; |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 317 | } |
| 318 | |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 319 | std::ostream& |
| 320 | operator<<(std::ostream& os, RibManager::SlAnnounceResult res) |
| 321 | { |
| 322 | switch (res) { |
Davide Pesavento | 5a89769 | 2019-10-31 01:28:43 -0400 | [diff] [blame] | 323 | case RibManager::SlAnnounceResult::OK: |
| 324 | return os << "OK"; |
| 325 | case RibManager::SlAnnounceResult::ERROR: |
| 326 | return os << "ERROR"; |
| 327 | case RibManager::SlAnnounceResult::VALIDATION_FAILURE: |
| 328 | return os << "VALIDATION_FAILURE"; |
| 329 | case RibManager::SlAnnounceResult::EXPIRED: |
| 330 | return os << "EXPIRED"; |
| 331 | case RibManager::SlAnnounceResult::NOT_FOUND: |
| 332 | return os << "NOT_FOUND"; |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 333 | } |
Davide Pesavento | 5a89769 | 2019-10-31 01:28:43 -0400 | [diff] [blame] | 334 | NDN_THROW(std::invalid_argument("Unknown SlAnnounceResult")); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | RibManager::SlAnnounceResult |
| 338 | RibManager::getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r) |
| 339 | { |
| 340 | switch (r) { |
| 341 | case RibUpdateResult::OK: |
| 342 | return SlAnnounceResult::OK; |
| 343 | case RibUpdateResult::ERROR: |
| 344 | return SlAnnounceResult::ERROR; |
| 345 | case RibUpdateResult::EXPIRED: |
| 346 | return SlAnnounceResult::EXPIRED; |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 347 | } |
Davide Pesavento | 5a89769 | 2019-10-31 01:28:43 -0400 | [diff] [blame] | 348 | NDN_CXX_UNREACHABLE; |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void |
| 352 | RibManager::slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId, |
| 353 | time::milliseconds maxLifetime, const SlAnnounceCallback& cb) |
| 354 | { |
| 355 | BOOST_ASSERT(pa.getData()); |
| 356 | |
Teng Liang | 18c2b29 | 2019-10-18 14:31:04 -0700 | [diff] [blame] | 357 | m_paValidator.validate(*pa.getData(), |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 358 | [=] (const Data&) { |
| 359 | Route route(pa, faceId); |
| 360 | route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime); |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 361 | beginAddRoute(pa.getAnnouncedName(), route, std::nullopt, |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 362 | [=] (RibUpdateResult ribRes) { |
| 363 | auto res = getSlAnnounceResultFromRibUpdateResult(ribRes); |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 364 | NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << ": " << res); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 365 | cb(res); |
| 366 | }); |
| 367 | }, |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 368 | [=] (const Data&, ndn::security::ValidationError err) { |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 369 | NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 370 | " validation error: " << err); |
| 371 | cb(SlAnnounceResult::VALIDATION_FAILURE); |
| 372 | } |
| 373 | ); |
| 374 | } |
| 375 | |
| 376 | void |
| 377 | RibManager::slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime, |
| 378 | const SlAnnounceCallback& cb) |
| 379 | { |
| 380 | Route routeQuery; |
| 381 | routeQuery.faceId = faceId; |
| 382 | routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN; |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 383 | Route* oldRoute = m_rib.findLongestPrefix(name, routeQuery); |
| 384 | |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 385 | if (oldRoute == nullptr || !oldRoute->announcement) { |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 386 | NFD_LOG_DEBUG("slRenew " << name << " " << faceId << ": not found"); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 387 | return cb(SlAnnounceResult::NOT_FOUND); |
| 388 | } |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 389 | Name routeName = oldRoute->announcement->getAnnouncedName(); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 390 | |
| 391 | Route route = *oldRoute; |
| 392 | route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime); |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 393 | beginAddRoute(routeName, route, std::nullopt, |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 394 | [=] (RibUpdateResult ribRes) { |
| 395 | auto res = getSlAnnounceResultFromRibUpdateResult(ribRes); |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 396 | NFD_LOG_INFO("slRenew " << name << " " << faceId << ": " << res << " " << routeName); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 397 | cb(res); |
| 398 | }); |
| 399 | } |
| 400 | |
| 401 | void |
| 402 | RibManager::slFindAnn(const Name& name, const SlFindAnnCallback& cb) const |
| 403 | { |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 404 | shared_ptr<rib::RibEntry> entry; |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 405 | auto exactMatch = m_rib.find(name); |
| 406 | if (exactMatch != m_rib.end()) { |
| 407 | entry = exactMatch->second; |
| 408 | } |
| 409 | else { |
| 410 | entry = m_rib.findParent(name); |
| 411 | } |
| 412 | if (entry == nullptr) { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 413 | return cb(std::nullopt); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | auto pa = entry->getPrefixAnnouncement(); |
| 417 | pa.toData(m_keyChain); |
| 418 | cb(pa); |
| 419 | } |
| 420 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 421 | void |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 422 | RibManager::fetchActiveFaces() |
| 423 | { |
| 424 | NFD_LOG_DEBUG("Fetching active faces"); |
| 425 | |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 426 | m_nfdController.fetch<ndn::nfd::FaceDataset>( |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 427 | [this] (auto&&... args) { removeInvalidFaces(std::forward<decltype(args)>(args)...); }, |
| 428 | [this] (auto&&... args) { onFetchActiveFacesFailure(std::forward<decltype(args)>(args)...); }); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | void |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 432 | RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason) |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 433 | { |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 434 | NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 435 | scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL); |
| 436 | } |
| 437 | |
| 438 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 439 | RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait) |
| 440 | { |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 441 | m_activeFaceFetchEvent = getScheduler().schedule(timeToWait, [this] { fetchActiveFaces(); }); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 445 | RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces) |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 446 | { |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 447 | NFD_LOG_DEBUG("Checking for invalid face registrations"); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 448 | |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 449 | std::set<uint64_t> activeFaceIds; |
Davide Pesavento | d396b61 | 2017-02-20 22:11:50 -0500 | [diff] [blame] | 450 | for (const auto& faceStatus : activeFaces) { |
| 451 | activeFaceIds.insert(faceStatus.getFaceId()); |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 452 | } |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 453 | getGlobalIoService().post([=] { m_rib.beginRemoveFailedFaces(activeFaceIds); }); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 454 | |
| 455 | // Reschedule the check for future clean up |
| 456 | scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void |
Davide Pesavento | d396b61 | 2017-02-20 22:11:50 -0500 | [diff] [blame] | 460 | RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification) |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 461 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 462 | NFD_LOG_TRACE("onNotification: " << notification); |
| 463 | |
| 464 | if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 465 | NFD_LOG_DEBUG("Received notification for destroyed FaceId " << notification.getFaceId()); |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 466 | getGlobalIoService().post([this, id = notification.getFaceId()] { m_rib.beginRemoveFace(id); }); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 470 | } // namespace nfd |