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