Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +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 | |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_MGMT_RIB_MANAGER_HPP |
| 27 | #define NFD_DAEMON_MGMT_RIB_MANAGER_HPP |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame] | 28 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 29 | #include "manager-base.hpp" |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 30 | #include "rib/route.hpp" |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 31 | |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/mgmt/nfd/controller.hpp> |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/mgmt/nfd/face-event-notification.hpp> |
| 34 | #include <ndn-cxx/mgmt/nfd/face-monitor.hpp> |
Davide Pesavento | 4064127 | 2023-03-16 13:31:12 -0400 | [diff] [blame^] | 35 | #include <ndn-cxx/mgmt/nfd/face-status.hpp> |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 36 | #include <ndn-cxx/security/validator-config.hpp> |
Junxiao Shi | feddc3c | 2019-01-17 19:06:00 +0000 | [diff] [blame] | 37 | #include <ndn-cxx/util/scheduler.hpp> |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 38 | |
| 39 | namespace nfd { |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 40 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 41 | namespace rib { |
| 42 | class Rib; |
| 43 | class RibUpdate; |
| 44 | } // namespace rib |
| 45 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 46 | /** |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 47 | * @brief Implements the RIB Management of NFD Management Protocol. |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 48 | * @sa https://redmine.named-data.net/projects/nfd/wiki/RibMgmt |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 49 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 50 | class RibManager final : public ManagerBase |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 51 | { |
| 52 | public: |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 53 | RibManager(rib::Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain, |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 54 | ndn::nfd::Controller& nfdController, Dispatcher& dispatcher); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 56 | /** |
| 57 | * @brief Apply localhost_security configuration. |
| 58 | */ |
| 59 | void |
| 60 | applyLocalhostConfig(const ConfigSection& section, const std::string& filename); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 61 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 62 | /** |
| 63 | * @brief Apply localhop_security configuration and allow accepting commands on |
| 64 | * /localhop/nfd/rib prefix. |
| 65 | */ |
| 66 | void |
| 67 | enableLocalhop(const ConfigSection& section, const std::string& filename); |
| 68 | |
| 69 | /** |
| 70 | * @brief Disallow accepting commands on /localhop/nfd/rib prefix. |
| 71 | */ |
| 72 | void |
| 73 | disableLocalhop(); |
| 74 | |
| 75 | /** |
Teng Liang | 18c2b29 | 2019-10-18 14:31:04 -0700 | [diff] [blame] | 76 | * @brief Apply prefix_announcement_validation configuration. |
| 77 | */ |
| 78 | void |
| 79 | applyPaConfig(const ConfigSection& section, const std::string& filename); |
| 80 | |
| 81 | /** |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 82 | * @brief Start accepting commands and dataset requests. |
| 83 | */ |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 84 | void |
| 85 | registerWithNfd(); |
| 86 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 87 | /** |
| 88 | * @brief Enable NDNLP IncomingFaceId field in order to support self-registration commands. |
| 89 | */ |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 90 | void |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 91 | enableLocalFields(); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 92 | |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 93 | public: // self-learning support |
| 94 | enum class SlAnnounceResult { |
| 95 | OK, ///< RIB and FIB have been updated |
| 96 | ERROR, ///< unspecified error |
| 97 | VALIDATION_FAILURE, ///< the announcement cannot be verified against the trust schema |
| 98 | EXPIRED, ///< the announcement has expired |
| 99 | NOT_FOUND, ///< route does not exist (slRenew only) |
| 100 | }; |
| 101 | |
| 102 | using SlAnnounceCallback = std::function<void(SlAnnounceResult res)>; |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 103 | using SlFindAnnCallback = std::function<void(std::optional<ndn::PrefixAnnouncement>)>; |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 104 | |
| 105 | /** \brief Insert a route by prefix announcement from self-learning strategy. |
| 106 | * \param pa A prefix announcement. It must contain the Data. |
| 107 | * \param faceId Face on which the announcement arrives. |
| 108 | * \param maxLifetime Maximum route lifetime as imposed by self-learning strategy. |
| 109 | * \param cb Callback to receive the operation result. |
| 110 | * |
| 111 | * If \p pa passes validation and is unexpired, inserts or replaces a route for the announced |
| 112 | * name and faceId whose lifetime is set to the earlier of now+maxLifetime or prefix |
| 113 | * announcement expiration time, updates FIB, and invokes \p cb with SlAnnounceResult::OK. |
| 114 | * In case \p pa expires when validation completes, invokes \p cb with SlAnnounceResult::EXPIRED. |
| 115 | * If \p pa cannot be verified by the trust schema given in rib.localhop_security config key, |
| 116 | * or the relevant config has not been loaded via \c enableLocalHop, invokes \p cb with |
| 117 | * SlAnnounceResult::VALIDATION_FAILURE. |
| 118 | * |
| 119 | * Self-learning strategy invokes this method after receiving a Data carrying a prefix |
| 120 | * announcement. |
| 121 | */ |
| 122 | void |
| 123 | slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId, time::milliseconds maxLifetime, |
| 124 | const SlAnnounceCallback& cb); |
| 125 | |
| 126 | /** \brief Renew a route created by prefix announcement from self-learning strategy. |
| 127 | * \param name Data name, for finding RIB entry by longest-prefix-match. |
| 128 | * \param faceId Nexthop face. |
| 129 | * \param maxLifetime Maximum route lifetime as imposed by self-learning strategy. |
| 130 | * \param cb Callback to receive the operation result. |
| 131 | * |
| 132 | * If the specified route exists, prolongs its lifetime to the earlier of now+maxLifetime or |
| 133 | * prefix announcement expiration time, and invokes \p cb with SlAnnounceResult::OK. |
| 134 | * If the prefix announcement has expired, invokes \p cb with SlAnnounceResult::EXPIRED. |
| 135 | * If the route is not found, invokes \p cb with SlAnnounceResult::NOT_FOUND. |
| 136 | * |
| 137 | * Self-learning strategy invokes this method after an Interest forwarded via a learned route |
| 138 | * is satisfied. |
| 139 | * |
| 140 | * \bug In current implementation, if an slAnnounce operation is in progress to create a Route |
| 141 | * or replace a prefix announcement, slRenew could fail because Route does not exist in |
| 142 | * existing RIB, or overwrite the new prefix announcement with an old one. |
| 143 | */ |
| 144 | void |
| 145 | slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime, |
| 146 | const SlAnnounceCallback& cb); |
| 147 | |
| 148 | /** \brief Retrieve an outgoing prefix announcement for self-learning strategy. |
| 149 | * \param name Data name. |
| 150 | * \param cb Callback to receive a prefix announcement that announces a prefix of \p name, or |
| 151 | * nullopt if no RIB entry is found by longest-prefix-match of \p name. |
| 152 | * |
| 153 | * Self-learning strategy invokes this method before sending a Data in reply to a discovery |
| 154 | * Interest, so as to attach a prefix announcement onto that Data. |
| 155 | * |
| 156 | * \bug In current implementation, if an slAnnounce operation is in progress, slFindAnn does not |
| 157 | * wait for that operation to complete and its result reflects the prior RIB state. |
| 158 | */ |
| 159 | void |
| 160 | slFindAnn(const Name& name, const SlFindAnnCallback& cb) const; |
| 161 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 162 | private: // RIB and FibUpdater actions |
| 163 | enum class RibUpdateResult |
| 164 | { |
| 165 | OK, |
| 166 | ERROR, |
| 167 | EXPIRED, |
| 168 | }; |
| 169 | |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 170 | static SlAnnounceResult |
| 171 | getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r); |
| 172 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 173 | /** \brief Start adding a route to RIB and FIB. |
| 174 | * \param name route name |
| 175 | * \param route route parameters; may contain absolute expiration time |
| 176 | * \param expires relative expiration time; if specified, overwrites \c route.expires |
| 177 | * \param done completion callback |
| 178 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 179 | void |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 180 | beginAddRoute(const Name& name, rib::Route route, std::optional<time::nanoseconds> expires, |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 181 | const std::function<void(RibUpdateResult)>& done); |
| 182 | |
| 183 | /** \brief Start removing a route from RIB and FIB. |
| 184 | * \param name route name |
| 185 | * \param route route parameters |
| 186 | * \param done completion callback |
| 187 | */ |
| 188 | void |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 189 | beginRemoveRoute(const Name& name, const rib::Route& route, |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 190 | const std::function<void(RibUpdateResult)>& done); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 191 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 192 | void |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 193 | beginRibUpdate(const rib::RibUpdate& update, |
| 194 | const std::function<void(RibUpdateResult)>& done); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 195 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 196 | private: // management Dispatcher related |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 197 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 198 | registerTopPrefix(const Name& topPrefix); |
| 199 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 200 | /** \brief Serve rib/register command. |
| 201 | */ |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 202 | void |
| 203 | registerEntry(const Name& topPrefix, const Interest& interest, |
| 204 | ControlParameters parameters, |
| 205 | const ndn::mgmt::CommandContinuation& done); |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 206 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 207 | /** \brief Serve rib/unregister command. |
| 208 | */ |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 209 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 210 | unregisterEntry(const Name& topPrefix, const Interest& interest, |
| 211 | ControlParameters parameters, |
| 212 | const ndn::mgmt::CommandContinuation& done); |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 213 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 214 | /** \brief Serve rib/list dataset. |
| 215 | */ |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 216 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 217 | listEntries(const Name& topPrefix, const Interest& interest, |
| 218 | ndn::mgmt::StatusDatasetContext& context); |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 219 | |
| 220 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 221 | setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 222 | |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame] | 223 | ndn::mgmt::Authorization |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 224 | makeAuthorization(const std::string& verb) final; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 225 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 226 | private: // Face monitor |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 227 | void |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 228 | fetchActiveFaces(); |
| 229 | |
| 230 | void |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 231 | onFetchActiveFacesFailure(uint32_t code, const std::string& reason); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 232 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 233 | NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 234 | void |
| 235 | scheduleActiveFaceFetch(const time::seconds& timeToWait); |
| 236 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 237 | void |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 238 | removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 239 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 240 | void |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame] | 241 | onNotification(const ndn::nfd::FaceEventNotification& notification); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 242 | |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 243 | public: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 244 | static inline const Name LOCALHOP_TOP_PREFIX{"/localhop/nfd"}; |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 245 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 246 | private: |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 247 | rib::Rib& m_rib; |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 248 | ndn::KeyChain& m_keyChain; |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 249 | ndn::nfd::Controller& m_nfdController; |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 250 | Dispatcher& m_dispatcher; |
| 251 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 252 | ndn::nfd::FaceMonitor m_faceMonitor; |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 253 | ndn::ValidatorConfig m_localhostValidator; |
| 254 | ndn::ValidatorConfig m_localhopValidator; |
Teng Liang | 18c2b29 | 2019-10-18 14:31:04 -0700 | [diff] [blame] | 255 | ndn::ValidatorConfig m_paValidator; |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 256 | bool m_isLocalhopEnabled; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 257 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 258 | scheduler::ScopedEventId m_activeFaceFetchEvent; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 259 | }; |
| 260 | |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 261 | std::ostream& |
| 262 | operator<<(std::ostream& os, RibManager::SlAnnounceResult res); |
| 263 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 264 | } // namespace nfd |
| 265 | |
Davide Pesavento | 8a05c7f | 2019-02-28 02:26:19 -0500 | [diff] [blame] | 266 | #endif // NFD_DAEMON_MGMT_RIB_MANAGER_HPP |