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