blob: 1be0b81fdaea73b9d7ade08291dfd92f62675dfc [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"
Davide Pesavento78ddcab2019-02-28 22:00:03 -050030#include "rib/route.hpp"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070031
Junxiao Shi25c6ce42016-09-09 13:49:59 +000032#include <ndn-cxx/mgmt/nfd/controller.hpp>
Nick Gordon9fcf1232017-03-10 22:30:20 +000033#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
34#include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050035#include <ndn-cxx/security/validator-config.hpp>
Junxiao Shifeddc3c2019-01-17 19:06:00 +000036#include <ndn-cxx/util/scheduler.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070037
38namespace nfd {
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070039
Davide Pesavento78ddcab2019-02-28 22:00:03 -050040namespace rib {
41class Rib;
42class RibUpdate;
43} // namespace rib
44
Junxiao Shif4cfed12018-08-22 23:26:29 +000045/**
Davide Pesavento78ddcab2019-02-28 22:00:03 -050046 * @brief Implements the RIB Management of NFD Management Protocol.
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050047 * @sa https://redmine.named-data.net/projects/nfd/wiki/RibMgmt
Junxiao Shif4cfed12018-08-22 23:26:29 +000048 */
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050049class RibManager : public ManagerBase
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070050{
51public:
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050052 RibManager(rib::Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
Davide Pesavento0a71dd32019-03-17 20:36:18 -040053 ndn::nfd::Controller& nfdController, Dispatcher& dispatcher);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070054
Junxiao Shif4cfed12018-08-22 23:26:29 +000055 /**
56 * @brief Apply localhost_security configuration.
57 */
58 void
59 applyLocalhostConfig(const ConfigSection& section, const std::string& filename);
Vince Lehman26b215c2014-08-17 15:00:41 -050060
Junxiao Shif4cfed12018-08-22 23:26:29 +000061 /**
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 /**
75 * @brief Start accepting commands and dataset requests.
76 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070077 void
78 registerWithNfd();
79
Junxiao Shif4cfed12018-08-22 23:26:29 +000080 /**
81 * @brief Enable NDNLP IncomingFaceId field in order to support self-registration commands.
82 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070083 void
Eric Newberryecc45cb2016-11-08 19:57:12 +000084 enableLocalFields();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070085
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000086public: // self-learning support
87 enum class SlAnnounceResult {
88 OK, ///< RIB and FIB have been updated
89 ERROR, ///< unspecified error
90 VALIDATION_FAILURE, ///< the announcement cannot be verified against the trust schema
91 EXPIRED, ///< the announcement has expired
92 NOT_FOUND, ///< route does not exist (slRenew only)
93 };
94
95 using SlAnnounceCallback = std::function<void(SlAnnounceResult res)>;
96 using SlFindAnnCallback = std::function<void(optional<ndn::PrefixAnnouncement>)>;
97
98 /** \brief Insert a route by prefix announcement from self-learning strategy.
99 * \param pa A prefix announcement. It must contain the Data.
100 * \param faceId Face on which the announcement arrives.
101 * \param maxLifetime Maximum route lifetime as imposed by self-learning strategy.
102 * \param cb Callback to receive the operation result.
103 *
104 * If \p pa passes validation and is unexpired, inserts or replaces a route for the announced
105 * name and faceId whose lifetime is set to the earlier of now+maxLifetime or prefix
106 * announcement expiration time, updates FIB, and invokes \p cb with SlAnnounceResult::OK.
107 * In case \p pa expires when validation completes, invokes \p cb with SlAnnounceResult::EXPIRED.
108 * If \p pa cannot be verified by the trust schema given in rib.localhop_security config key,
109 * or the relevant config has not been loaded via \c enableLocalHop, invokes \p cb with
110 * SlAnnounceResult::VALIDATION_FAILURE.
111 *
112 * Self-learning strategy invokes this method after receiving a Data carrying a prefix
113 * announcement.
114 */
115 void
116 slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId, time::milliseconds maxLifetime,
117 const SlAnnounceCallback& cb);
118
119 /** \brief Renew a route created by prefix announcement from self-learning strategy.
120 * \param name Data name, for finding RIB entry by longest-prefix-match.
121 * \param faceId Nexthop face.
122 * \param maxLifetime Maximum route lifetime as imposed by self-learning strategy.
123 * \param cb Callback to receive the operation result.
124 *
125 * If the specified route exists, prolongs its lifetime to the earlier of now+maxLifetime or
126 * prefix announcement expiration time, and invokes \p cb with SlAnnounceResult::OK.
127 * If the prefix announcement has expired, invokes \p cb with SlAnnounceResult::EXPIRED.
128 * If the route is not found, invokes \p cb with SlAnnounceResult::NOT_FOUND.
129 *
130 * Self-learning strategy invokes this method after an Interest forwarded via a learned route
131 * is satisfied.
132 *
133 * \bug In current implementation, if an slAnnounce operation is in progress to create a Route
134 * or replace a prefix announcement, slRenew could fail because Route does not exist in
135 * existing RIB, or overwrite the new prefix announcement with an old one.
136 */
137 void
138 slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
139 const SlAnnounceCallback& cb);
140
141 /** \brief Retrieve an outgoing prefix announcement for self-learning strategy.
142 * \param name Data name.
143 * \param cb Callback to receive a prefix announcement that announces a prefix of \p name, or
144 * nullopt if no RIB entry is found by longest-prefix-match of \p name.
145 *
146 * Self-learning strategy invokes this method before sending a Data in reply to a discovery
147 * Interest, so as to attach a prefix announcement onto that Data.
148 *
149 * \bug In current implementation, if an slAnnounce operation is in progress, slFindAnn does not
150 * wait for that operation to complete and its result reflects the prior RIB state.
151 */
152 void
153 slFindAnn(const Name& name, const SlFindAnnCallback& cb) const;
154
Junxiao Shi52009042018-09-10 12:33:56 +0000155private: // RIB and FibUpdater actions
156 enum class RibUpdateResult
157 {
158 OK,
159 ERROR,
160 EXPIRED,
161 };
162
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000163 static SlAnnounceResult
164 getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r);
165
Junxiao Shi52009042018-09-10 12:33:56 +0000166 /** \brief Start adding a route to RIB and FIB.
167 * \param name route name
168 * \param route route parameters; may contain absolute expiration time
169 * \param expires relative expiration time; if specified, overwrites \c route.expires
170 * \param done completion callback
171 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600172 void
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500173 beginAddRoute(const Name& name, rib::Route route, optional<time::nanoseconds> expires,
Junxiao Shi52009042018-09-10 12:33:56 +0000174 const std::function<void(RibUpdateResult)>& done);
175
176 /** \brief Start removing a route from RIB and FIB.
177 * \param name route name
178 * \param route route parameters
179 * \param done completion callback
180 */
181 void
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500182 beginRemoveRoute(const Name& name, const rib::Route& route,
Junxiao Shi52009042018-09-10 12:33:56 +0000183 const std::function<void(RibUpdateResult)>& done);
Vince Lehman4387e782014-06-19 16:57:45 -0500184
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700185 void
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500186 beginRibUpdate(const rib::RibUpdate& update,
187 const std::function<void(RibUpdateResult)>& done);
Vince Lehman76c751c2014-11-18 17:36:38 -0600188
Junxiao Shi52009042018-09-10 12:33:56 +0000189private: // management Dispatcher related
Vince Lehman76c751c2014-11-18 17:36:38 -0600190 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800191 registerTopPrefix(const Name& topPrefix);
192
Junxiao Shi52009042018-09-10 12:33:56 +0000193 /** \brief Serve rib/register command.
194 */
Yanbiao Licf0db022016-01-29 00:54:25 -0800195 void
196 registerEntry(const Name& topPrefix, const Interest& interest,
197 ControlParameters parameters,
198 const ndn::mgmt::CommandContinuation& done);
Junxiao Shia3295742014-05-16 22:40:10 -0700199
Junxiao Shi52009042018-09-10 12:33:56 +0000200 /** \brief Serve rib/unregister command.
201 */
Junxiao Shia3295742014-05-16 22:40:10 -0700202 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800203 unregisterEntry(const Name& topPrefix, const Interest& interest,
204 ControlParameters parameters,
205 const ndn::mgmt::CommandContinuation& done);
Yingdi Yue5224e92014-04-29 18:04:02 -0700206
Junxiao Shi52009042018-09-10 12:33:56 +0000207 /** \brief Serve rib/list dataset.
208 */
Yingdi Yue5224e92014-04-29 18:04:02 -0700209 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800210 listEntries(const Name& topPrefix, const Interest& interest,
211 ndn::mgmt::StatusDatasetContext& context);
Yingdi Yue5224e92014-04-29 18:04:02 -0700212
213 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800214 setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700215
Nick Gordon9fcf1232017-03-10 22:30:20 +0000216 ndn::mgmt::Authorization
Junxiao Shi21738402016-08-19 19:48:00 +0000217 makeAuthorization(const std::string& verb) override;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700218
Yanbiao Licf0db022016-01-29 00:54:25 -0800219private: // Face monitor
Vince Lehman26b215c2014-08-17 15:00:41 -0500220 void
Vince Lehmancd613c52014-07-30 14:34:49 -0500221 fetchActiveFaces();
222
223 void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700224 onFetchActiveFacesFailure(uint32_t code, const std::string& reason);
Vince Lehmancd613c52014-07-30 14:34:49 -0500225
Vince Lehman281ded72014-08-21 12:17:08 -0500226PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yanbiao Licf0db022016-01-29 00:54:25 -0800227 void
228 scheduleActiveFaceFetch(const time::seconds& timeToWait);
229
Vince Lehman26b215c2014-08-17 15:00:41 -0500230 void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700231 removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces);
Vince Lehman26b215c2014-08-17 15:00:41 -0500232
Yanbiao Licf0db022016-01-29 00:54:25 -0800233 void
Nick Gordon9fcf1232017-03-10 22:30:20 +0000234 onNotification(const ndn::nfd::FaceEventNotification& notification);
Yanbiao Licf0db022016-01-29 00:54:25 -0800235
Yanbiao Lif48d0802018-06-01 03:00:02 -0700236public:
237 static const Name LOCALHOP_TOP_PREFIX;
238
Yanbiao Licf0db022016-01-29 00:54:25 -0800239private:
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500240 rib::Rib& m_rib;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000241 ndn::KeyChain& m_keyChain;
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000242 ndn::nfd::Controller& m_nfdController;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000243 Dispatcher& m_dispatcher;
244
Yanbiao Licf0db022016-01-29 00:54:25 -0800245 ndn::nfd::FaceMonitor m_faceMonitor;
Yingdi Yue5224e92014-04-29 18:04:02 -0700246 ndn::ValidatorConfig m_localhostValidator;
247 ndn::ValidatorConfig m_localhopValidator;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000248 bool m_isLocalhopEnabled;
Vince Lehman4387e782014-06-19 16:57:45 -0500249
Davide Pesavento3dade002019-03-19 11:29:56 -0600250 scheduler::ScopedEventId m_activeFaceFetchEvent;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700251};
252
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000253std::ostream&
254operator<<(std::ostream& os, RibManager::SlAnnounceResult res);
255
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700256} // namespace nfd
257
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500258#endif // NFD_DAEMON_MGMT_RIB_MANAGER_HPP