blob: 745cfa2b142882f0556b76f480f7631f84138f64 [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,
54 ndn::nfd::Controller& nfdController, Dispatcher& dispatcher,
55 ndn::util::Scheduler& scheduler);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070056
Junxiao Shif4cfed12018-08-22 23:26:29 +000057 /**
58 * @brief Apply localhost_security configuration.
59 */
60 void
61 applyLocalhostConfig(const ConfigSection& section, const std::string& filename);
Vince Lehman26b215c2014-08-17 15:00:41 -050062
Junxiao Shif4cfed12018-08-22 23:26:29 +000063 /**
64 * @brief Apply localhop_security configuration and allow accepting commands on
65 * /localhop/nfd/rib prefix.
66 */
67 void
68 enableLocalhop(const ConfigSection& section, const std::string& filename);
69
70 /**
71 * @brief Disallow accepting commands on /localhop/nfd/rib prefix.
72 */
73 void
74 disableLocalhop();
75
76 /**
77 * @brief Start accepting commands and dataset requests.
78 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070079 void
80 registerWithNfd();
81
Junxiao Shif4cfed12018-08-22 23:26:29 +000082 /**
83 * @brief Enable NDNLP IncomingFaceId field in order to support self-registration commands.
84 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070085 void
Eric Newberryecc45cb2016-11-08 19:57:12 +000086 enableLocalFields();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070087
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000088public: // self-learning support
89 enum class SlAnnounceResult {
90 OK, ///< RIB and FIB have been updated
91 ERROR, ///< unspecified error
92 VALIDATION_FAILURE, ///< the announcement cannot be verified against the trust schema
93 EXPIRED, ///< the announcement has expired
94 NOT_FOUND, ///< route does not exist (slRenew only)
95 };
96
97 using SlAnnounceCallback = std::function<void(SlAnnounceResult res)>;
98 using SlFindAnnCallback = std::function<void(optional<ndn::PrefixAnnouncement>)>;
99
100 /** \brief Insert a route by prefix announcement from self-learning strategy.
101 * \param pa A prefix announcement. It must contain the Data.
102 * \param faceId Face on which the announcement arrives.
103 * \param maxLifetime Maximum route lifetime as imposed by self-learning strategy.
104 * \param cb Callback to receive the operation result.
105 *
106 * If \p pa passes validation and is unexpired, inserts or replaces a route for the announced
107 * name and faceId whose lifetime is set to the earlier of now+maxLifetime or prefix
108 * announcement expiration time, updates FIB, and invokes \p cb with SlAnnounceResult::OK.
109 * In case \p pa expires when validation completes, invokes \p cb with SlAnnounceResult::EXPIRED.
110 * If \p pa cannot be verified by the trust schema given in rib.localhop_security config key,
111 * or the relevant config has not been loaded via \c enableLocalHop, invokes \p cb with
112 * SlAnnounceResult::VALIDATION_FAILURE.
113 *
114 * Self-learning strategy invokes this method after receiving a Data carrying a prefix
115 * announcement.
116 */
117 void
118 slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId, time::milliseconds maxLifetime,
119 const SlAnnounceCallback& cb);
120
121 /** \brief Renew a route created by prefix announcement from self-learning strategy.
122 * \param name Data name, for finding RIB entry by longest-prefix-match.
123 * \param faceId Nexthop face.
124 * \param maxLifetime Maximum route lifetime as imposed by self-learning strategy.
125 * \param cb Callback to receive the operation result.
126 *
127 * If the specified route exists, prolongs its lifetime to the earlier of now+maxLifetime or
128 * prefix announcement expiration time, and invokes \p cb with SlAnnounceResult::OK.
129 * If the prefix announcement has expired, invokes \p cb with SlAnnounceResult::EXPIRED.
130 * If the route is not found, invokes \p cb with SlAnnounceResult::NOT_FOUND.
131 *
132 * Self-learning strategy invokes this method after an Interest forwarded via a learned route
133 * is satisfied.
134 *
135 * \bug In current implementation, if an slAnnounce operation is in progress to create a Route
136 * or replace a prefix announcement, slRenew could fail because Route does not exist in
137 * existing RIB, or overwrite the new prefix announcement with an old one.
138 */
139 void
140 slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
141 const SlAnnounceCallback& cb);
142
143 /** \brief Retrieve an outgoing prefix announcement for self-learning strategy.
144 * \param name Data name.
145 * \param cb Callback to receive a prefix announcement that announces a prefix of \p name, or
146 * nullopt if no RIB entry is found by longest-prefix-match of \p name.
147 *
148 * Self-learning strategy invokes this method before sending a Data in reply to a discovery
149 * Interest, so as to attach a prefix announcement onto that Data.
150 *
151 * \bug In current implementation, if an slAnnounce operation is in progress, slFindAnn does not
152 * wait for that operation to complete and its result reflects the prior RIB state.
153 */
154 void
155 slFindAnn(const Name& name, const SlFindAnnCallback& cb) const;
156
Junxiao Shi52009042018-09-10 12:33:56 +0000157private: // RIB and FibUpdater actions
158 enum class RibUpdateResult
159 {
160 OK,
161 ERROR,
162 EXPIRED,
163 };
164
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000165 static SlAnnounceResult
166 getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r);
167
Junxiao Shi52009042018-09-10 12:33:56 +0000168 /** \brief Start adding a route to RIB and FIB.
169 * \param name route name
170 * \param route route parameters; may contain absolute expiration time
171 * \param expires relative expiration time; if specified, overwrites \c route.expires
172 * \param done completion callback
173 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600174 void
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500175 beginAddRoute(const Name& name, rib::Route route, optional<time::nanoseconds> expires,
Junxiao Shi52009042018-09-10 12:33:56 +0000176 const std::function<void(RibUpdateResult)>& done);
177
178 /** \brief Start removing a route from RIB and FIB.
179 * \param name route name
180 * \param route route parameters
181 * \param done completion callback
182 */
183 void
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500184 beginRemoveRoute(const Name& name, const rib::Route& route,
Junxiao Shi52009042018-09-10 12:33:56 +0000185 const std::function<void(RibUpdateResult)>& done);
Vince Lehman4387e782014-06-19 16:57:45 -0500186
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700187 void
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500188 beginRibUpdate(const rib::RibUpdate& update,
189 const std::function<void(RibUpdateResult)>& done);
Vince Lehman76c751c2014-11-18 17:36:38 -0600190
Junxiao Shi52009042018-09-10 12:33:56 +0000191private: // management Dispatcher related
Vince Lehman76c751c2014-11-18 17:36:38 -0600192 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800193 registerTopPrefix(const Name& topPrefix);
194
Junxiao Shi52009042018-09-10 12:33:56 +0000195 /** \brief Serve rib/register command.
196 */
Yanbiao Licf0db022016-01-29 00:54:25 -0800197 void
198 registerEntry(const Name& topPrefix, const Interest& interest,
199 ControlParameters parameters,
200 const ndn::mgmt::CommandContinuation& done);
Junxiao Shia3295742014-05-16 22:40:10 -0700201
Junxiao Shi52009042018-09-10 12:33:56 +0000202 /** \brief Serve rib/unregister command.
203 */
Junxiao Shia3295742014-05-16 22:40:10 -0700204 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800205 unregisterEntry(const Name& topPrefix, const Interest& interest,
206 ControlParameters parameters,
207 const ndn::mgmt::CommandContinuation& done);
Yingdi Yue5224e92014-04-29 18:04:02 -0700208
Junxiao Shi52009042018-09-10 12:33:56 +0000209 /** \brief Serve rib/list dataset.
210 */
Yingdi Yue5224e92014-04-29 18:04:02 -0700211 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800212 listEntries(const Name& topPrefix, const Interest& interest,
213 ndn::mgmt::StatusDatasetContext& context);
Yingdi Yue5224e92014-04-29 18:04:02 -0700214
215 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800216 setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700217
Nick Gordon9fcf1232017-03-10 22:30:20 +0000218 ndn::mgmt::Authorization
Junxiao Shi21738402016-08-19 19:48:00 +0000219 makeAuthorization(const std::string& verb) override;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700220
Yanbiao Licf0db022016-01-29 00:54:25 -0800221private: // Face monitor
Vince Lehman26b215c2014-08-17 15:00:41 -0500222 void
Vince Lehmancd613c52014-07-30 14:34:49 -0500223 fetchActiveFaces();
224
225 void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700226 onFetchActiveFacesFailure(uint32_t code, const std::string& reason);
Vince Lehmancd613c52014-07-30 14:34:49 -0500227
Yanbiao Licf0db022016-01-29 00:54:25 -0800228 void
229 onFaceDestroyedEvent(uint64_t faceId);
230
Vince Lehman281ded72014-08-21 12:17:08 -0500231PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yanbiao Licf0db022016-01-29 00:54:25 -0800232 void
233 scheduleActiveFaceFetch(const time::seconds& timeToWait);
234
Vince Lehman26b215c2014-08-17 15:00:41 -0500235 void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700236 removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces);
Vince Lehman26b215c2014-08-17 15:00:41 -0500237
Yanbiao Licf0db022016-01-29 00:54:25 -0800238 void
Nick Gordon9fcf1232017-03-10 22:30:20 +0000239 onNotification(const ndn::nfd::FaceEventNotification& notification);
Yanbiao Licf0db022016-01-29 00:54:25 -0800240
Yanbiao Lif48d0802018-06-01 03:00:02 -0700241public:
242 static const Name LOCALHOP_TOP_PREFIX;
243
Yanbiao Licf0db022016-01-29 00:54:25 -0800244private:
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500245 rib::Rib& m_rib;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000246 ndn::KeyChain& m_keyChain;
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000247 ndn::nfd::Controller& m_nfdController;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000248 Dispatcher& m_dispatcher;
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400249 ndn::util::Scheduler& m_scheduler;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000250
Yanbiao Licf0db022016-01-29 00:54:25 -0800251 ndn::nfd::FaceMonitor m_faceMonitor;
Yingdi Yue5224e92014-04-29 18:04:02 -0700252 ndn::ValidatorConfig m_localhostValidator;
253 ndn::ValidatorConfig m_localhopValidator;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000254 bool m_isLocalhopEnabled;
Vince Lehman4387e782014-06-19 16:57:45 -0500255
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400256 ndn::util::scheduler::ScopedEventId m_activeFaceFetchEvent;
257 using FaceIdSet = std::set<uint64_t>;
258 FaceIdSet m_registeredFaces; ///< contains FaceIds with one or more Routes in the RIB
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700259};
260
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000261std::ostream&
262operator<<(std::ostream& os, RibManager::SlAnnounceResult res);
263
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700264} // namespace nfd
265
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500266#endif // NFD_DAEMON_MGMT_RIB_MANAGER_HPP