blob: 6888024fead1ef282c2ba4627964b8bd7a62e7e6 [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento9f8b10e2018-08-22 08:45:37 +00002/*
Junxiao Shifeddc3c2019-01-17 19:06:00 +00003 * Copyright (c) 2014-2019, 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
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050026#ifndef NFD_DAEMON_MGMT_RIB_MANAGER_HPP
27#define NFD_DAEMON_MGMT_RIB_MANAGER_HPP
Nick Gordon9fcf1232017-03-10 22:30:20 +000028
Davide Pesavento78ddcab2019-02-28 22:00:03 -050029#include "manager-base.hpp"
Nick Gordon9fcf1232017-03-10 22:30:20 +000030#include "core/config-file.hpp"
Davide Pesavento78ddcab2019-02-28 22:00:03 -050031#include "rib/route.hpp"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070032
Junxiao Shi25c6ce42016-09-09 13:49:59 +000033#include <ndn-cxx/mgmt/nfd/controller.hpp>
Nick Gordon9fcf1232017-03-10 22:30:20 +000034#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
35#include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050036#include <ndn-cxx/security/validator-config.hpp>
Junxiao Shifeddc3c2019-01-17 19:06:00 +000037#include <ndn-cxx/util/scheduler.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070038
39namespace nfd {
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070040
Davide Pesavento78ddcab2019-02-28 22:00:03 -050041namespace rib {
42class Rib;
43class RibUpdate;
44} // namespace rib
45
Junxiao Shif4cfed12018-08-22 23:26:29 +000046/**
Davide Pesavento78ddcab2019-02-28 22:00:03 -050047 * @brief Implements the RIB Management of NFD Management Protocol.
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050048 * @sa https://redmine.named-data.net/projects/nfd/wiki/RibMgmt
Junxiao Shif4cfed12018-08-22 23:26:29 +000049 */
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050050class RibManager : public ManagerBase
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070051{
52public:
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050053 RibManager(rib::Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
Davide Pesavento0a71dd32019-03-17 20:36:18 -040054 ndn::nfd::Controller& nfdController, Dispatcher& dispatcher);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070055
Junxiao Shif4cfed12018-08-22 23:26:29 +000056 /**
57 * @brief Apply localhost_security configuration.
58 */
59 void
60 applyLocalhostConfig(const ConfigSection& section, const std::string& filename);
Vince Lehman26b215c2014-08-17 15:00:41 -050061
Junxiao Shif4cfed12018-08-22 23:26:29 +000062 /**
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 /**
76 * @brief Start accepting commands and dataset requests.
77 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070078 void
79 registerWithNfd();
80
Junxiao Shif4cfed12018-08-22 23:26:29 +000081 /**
82 * @brief Enable NDNLP IncomingFaceId field in order to support self-registration commands.
83 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070084 void
Eric Newberryecc45cb2016-11-08 19:57:12 +000085 enableLocalFields();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070086
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000087public: // self-learning support
88 enum class SlAnnounceResult {
89 OK, ///< RIB and FIB have been updated
90 ERROR, ///< unspecified error
91 VALIDATION_FAILURE, ///< the announcement cannot be verified against the trust schema
92 EXPIRED, ///< the announcement has expired
93 NOT_FOUND, ///< route does not exist (slRenew only)
94 };
95
96 using SlAnnounceCallback = std::function<void(SlAnnounceResult res)>;
97 using SlFindAnnCallback = std::function<void(optional<ndn::PrefixAnnouncement>)>;
98
99 /** \brief Insert a route by prefix announcement from self-learning strategy.
100 * \param pa A prefix announcement. It must contain the Data.
101 * \param faceId Face on which the announcement arrives.
102 * \param maxLifetime Maximum route lifetime as imposed by self-learning strategy.
103 * \param cb Callback to receive the operation result.
104 *
105 * If \p pa passes validation and is unexpired, inserts or replaces a route for the announced
106 * name and faceId whose lifetime is set to the earlier of now+maxLifetime or prefix
107 * announcement expiration time, updates FIB, and invokes \p cb with SlAnnounceResult::OK.
108 * In case \p pa expires when validation completes, invokes \p cb with SlAnnounceResult::EXPIRED.
109 * If \p pa cannot be verified by the trust schema given in rib.localhop_security config key,
110 * or the relevant config has not been loaded via \c enableLocalHop, invokes \p cb with
111 * SlAnnounceResult::VALIDATION_FAILURE.
112 *
113 * Self-learning strategy invokes this method after receiving a Data carrying a prefix
114 * announcement.
115 */
116 void
117 slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId, time::milliseconds maxLifetime,
118 const SlAnnounceCallback& cb);
119
120 /** \brief Renew a route created by prefix announcement from self-learning strategy.
121 * \param name Data name, for finding RIB entry by longest-prefix-match.
122 * \param faceId Nexthop face.
123 * \param maxLifetime Maximum route lifetime as imposed by self-learning strategy.
124 * \param cb Callback to receive the operation result.
125 *
126 * If the specified route exists, prolongs its lifetime to the earlier of now+maxLifetime or
127 * prefix announcement expiration time, and invokes \p cb with SlAnnounceResult::OK.
128 * If the prefix announcement has expired, invokes \p cb with SlAnnounceResult::EXPIRED.
129 * If the route is not found, invokes \p cb with SlAnnounceResult::NOT_FOUND.
130 *
131 * Self-learning strategy invokes this method after an Interest forwarded via a learned route
132 * is satisfied.
133 *
134 * \bug In current implementation, if an slAnnounce operation is in progress to create a Route
135 * or replace a prefix announcement, slRenew could fail because Route does not exist in
136 * existing RIB, or overwrite the new prefix announcement with an old one.
137 */
138 void
139 slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
140 const SlAnnounceCallback& cb);
141
142 /** \brief Retrieve an outgoing prefix announcement for self-learning strategy.
143 * \param name Data name.
144 * \param cb Callback to receive a prefix announcement that announces a prefix of \p name, or
145 * nullopt if no RIB entry is found by longest-prefix-match of \p name.
146 *
147 * Self-learning strategy invokes this method before sending a Data in reply to a discovery
148 * Interest, so as to attach a prefix announcement onto that Data.
149 *
150 * \bug In current implementation, if an slAnnounce operation is in progress, slFindAnn does not
151 * wait for that operation to complete and its result reflects the prior RIB state.
152 */
153 void
154 slFindAnn(const Name& name, const SlFindAnnCallback& cb) const;
155
Junxiao Shi52009042018-09-10 12:33:56 +0000156private: // RIB and FibUpdater actions
157 enum class RibUpdateResult
158 {
159 OK,
160 ERROR,
161 EXPIRED,
162 };
163
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000164 static SlAnnounceResult
165 getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r);
166
Junxiao Shi52009042018-09-10 12:33:56 +0000167 /** \brief Start adding a route to RIB and FIB.
168 * \param name route name
169 * \param route route parameters; may contain absolute expiration time
170 * \param expires relative expiration time; if specified, overwrites \c route.expires
171 * \param done completion callback
172 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600173 void
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500174 beginAddRoute(const Name& name, rib::Route route, optional<time::nanoseconds> expires,
Junxiao Shi52009042018-09-10 12:33:56 +0000175 const std::function<void(RibUpdateResult)>& done);
176
177 /** \brief Start removing a route from RIB and FIB.
178 * \param name route name
179 * \param route route parameters
180 * \param done completion callback
181 */
182 void
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500183 beginRemoveRoute(const Name& name, const rib::Route& route,
Junxiao Shi52009042018-09-10 12:33:56 +0000184 const std::function<void(RibUpdateResult)>& done);
Vince Lehman4387e782014-06-19 16:57:45 -0500185
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700186 void
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500187 beginRibUpdate(const rib::RibUpdate& update,
188 const std::function<void(RibUpdateResult)>& done);
Vince Lehman76c751c2014-11-18 17:36:38 -0600189
Junxiao Shi52009042018-09-10 12:33:56 +0000190private: // management Dispatcher related
Vince Lehman76c751c2014-11-18 17:36:38 -0600191 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800192 registerTopPrefix(const Name& topPrefix);
193
Junxiao Shi52009042018-09-10 12:33:56 +0000194 /** \brief Serve rib/register command.
195 */
Yanbiao Licf0db022016-01-29 00:54:25 -0800196 void
197 registerEntry(const Name& topPrefix, const Interest& interest,
198 ControlParameters parameters,
199 const ndn::mgmt::CommandContinuation& done);
Junxiao Shia3295742014-05-16 22:40:10 -0700200
Junxiao Shi52009042018-09-10 12:33:56 +0000201 /** \brief Serve rib/unregister command.
202 */
Junxiao Shia3295742014-05-16 22:40:10 -0700203 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800204 unregisterEntry(const Name& topPrefix, const Interest& interest,
205 ControlParameters parameters,
206 const ndn::mgmt::CommandContinuation& done);
Yingdi Yue5224e92014-04-29 18:04:02 -0700207
Junxiao Shi52009042018-09-10 12:33:56 +0000208 /** \brief Serve rib/list dataset.
209 */
Yingdi Yue5224e92014-04-29 18:04:02 -0700210 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800211 listEntries(const Name& topPrefix, const Interest& interest,
212 ndn::mgmt::StatusDatasetContext& context);
Yingdi Yue5224e92014-04-29 18:04:02 -0700213
214 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800215 setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700216
Nick Gordon9fcf1232017-03-10 22:30:20 +0000217 ndn::mgmt::Authorization
Junxiao Shi21738402016-08-19 19:48:00 +0000218 makeAuthorization(const std::string& verb) override;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700219
Yanbiao Licf0db022016-01-29 00:54:25 -0800220private: // Face monitor
Vince Lehman26b215c2014-08-17 15:00:41 -0500221 void
Vince Lehmancd613c52014-07-30 14:34:49 -0500222 fetchActiveFaces();
223
224 void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700225 onFetchActiveFacesFailure(uint32_t code, const std::string& reason);
Vince Lehmancd613c52014-07-30 14:34:49 -0500226
Yanbiao Licf0db022016-01-29 00:54:25 -0800227 void
228 onFaceDestroyedEvent(uint64_t faceId);
229
Vince Lehman281ded72014-08-21 12:17:08 -0500230PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yanbiao Licf0db022016-01-29 00:54:25 -0800231 void
232 scheduleActiveFaceFetch(const time::seconds& timeToWait);
233
Vince Lehman26b215c2014-08-17 15:00:41 -0500234 void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700235 removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces);
Vince Lehman26b215c2014-08-17 15:00:41 -0500236
Yanbiao Licf0db022016-01-29 00:54:25 -0800237 void
Nick Gordon9fcf1232017-03-10 22:30:20 +0000238 onNotification(const ndn::nfd::FaceEventNotification& notification);
Yanbiao Licf0db022016-01-29 00:54:25 -0800239
Yanbiao Lif48d0802018-06-01 03:00:02 -0700240public:
241 static const Name LOCALHOP_TOP_PREFIX;
242
Yanbiao Licf0db022016-01-29 00:54:25 -0800243private:
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500244 rib::Rib& m_rib;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000245 ndn::KeyChain& m_keyChain;
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000246 ndn::nfd::Controller& m_nfdController;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000247 Dispatcher& m_dispatcher;
248
Yanbiao Licf0db022016-01-29 00:54:25 -0800249 ndn::nfd::FaceMonitor m_faceMonitor;
Yingdi Yue5224e92014-04-29 18:04:02 -0700250 ndn::ValidatorConfig m_localhostValidator;
251 ndn::ValidatorConfig m_localhopValidator;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000252 bool m_isLocalhopEnabled;
Vince Lehman4387e782014-06-19 16:57:45 -0500253
Davide Pesavento3dade002019-03-19 11:29:56 -0600254 scheduler::ScopedEventId m_activeFaceFetchEvent;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400255 using FaceIdSet = std::set<uint64_t>;
256 FaceIdSet m_registeredFaces; ///< contains FaceIds with one or more Routes in the RIB
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700257};
258
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000259std::ostream&
260operator<<(std::ostream& os, RibManager::SlAnnounceResult res);
261
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700262} // namespace nfd
263
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500264#endif // NFD_DAEMON_MGMT_RIB_MANAGER_HPP