blob: 181d210b661ede940aba09ba2ab2e7e4c0ba6b6c [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince12e49462014-06-09 13:29:32 -05003 * Copyright (c) 2014, Regents of the University of California,
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 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
26#ifndef NFD_RIB_RIB_MANAGER_HPP
27#define NFD_RIB_RIB_MANAGER_HPP
28
29#include "rib.hpp"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070030#include "core/config-file.hpp"
Vince Lehmancd16c832014-07-23 15:14:55 -070031#include "rib-status-publisher.hpp"
Yanbiao Lic17de832014-11-21 17:51:45 -080032#include "remote-registrator.hpp"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070033
Alexander Afanasyev4a771362014-04-24 21:29:33 -070034#include <ndn-cxx/security/validator-config.hpp>
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070035#include <ndn-cxx/management/nfd-face-monitor.hpp>
Alexander Afanasyev4a771362014-04-24 21:29:33 -070036#include <ndn-cxx/management/nfd-controller.hpp>
37#include <ndn-cxx/management/nfd-control-command.hpp>
38#include <ndn-cxx/management/nfd-control-response.hpp>
39#include <ndn-cxx/management/nfd-control-parameters.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070040
41namespace nfd {
42namespace rib {
43
44using ndn::nfd::ControlCommand;
45using ndn::nfd::ControlResponse;
46using ndn::nfd::ControlParameters;
47
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070048using ndn::nfd::FaceEventNotification;
49
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070050class RibManager : noncopyable
51{
52public:
Yingdi Yue5224e92014-04-29 18:04:02 -070053 class Error : public std::runtime_error
54 {
55 public:
56 explicit
57 Error(const std::string& what)
58 : std::runtime_error(what)
59 {
60 }
61 };
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070062
Vince Lehman72446ec2014-07-09 10:50:02 -050063 explicit
64 RibManager(ndn::Face& face);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070065
Vince Lehman26b215c2014-08-17 15:00:41 -050066 ~RibManager();
67
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070068 void
69 registerWithNfd();
70
71 void
72 enableLocalControlHeader();
73
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070074 void
75 setConfigFile(ConfigFile& configFile);
76
77private:
Vince Lehman4387e782014-06-19 16:57:45 -050078 typedef uint32_t TransactionId;
79
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070080 void
81 onConfig(const ConfigSection& configSection,
82 bool isDryRun,
83 const std::string& filename);
84
85 void
Junxiao Shia3295742014-05-16 22:40:10 -070086 startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest);
87
88 void
Yingdi Yue5224e92014-04-29 18:04:02 -070089 onLocalhopRequest(const Interest& request);
90
91 void
92 onLocalhostRequest(const Interest& request);
93
94 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070095 sendResponse(const Name& name,
96 const ControlResponse& response);
97
98 void
99 sendResponse(const Name& name,
100 uint32_t code,
101 const std::string& text);
102
103 void
Vince Lehman4387e782014-06-19 16:57:45 -0500104 sendSuccessResponse(const shared_ptr<const Interest>& request,
105 const ControlParameters& parameters);
106
107 void
108 sendErrorResponse(uint32_t code, const std::string& error,
109 const shared_ptr<const Interest>& request);
110
111 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700112 registerEntry(const shared_ptr<const Interest>& request,
113 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700114
115 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700116 unregisterEntry(const shared_ptr<const Interest>& request,
117 ControlParameters& parameters);
118
119 void
Vince Lehman281ded72014-08-21 12:17:08 -0500120 expireEntry(const shared_ptr<const Interest>& request, ControlParameters& params);
121
122 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700123 onCommandValidated(const shared_ptr<const Interest>& request);
124
125 void
126 onCommandValidationFailed(const shared_ptr<const Interest>& request,
127 const std::string& failureInfo);
128
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700129
130 void
131 onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700132 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500133 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700134
135 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700136 onRegSuccess(const shared_ptr<const Interest>& request,
137 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500138 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700139
140 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700141 onUnRegSuccess(const shared_ptr<const Interest>& request,
142 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500143 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700144
145 void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700146 onNrdCommandPrefixAddNextHopSuccess(const Name& prefix);
147
148 void
149 onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg);
150
151 void
Vince Lehman4387e782014-06-19 16:57:45 -0500152 onAddNextHopSuccess(const shared_ptr<const Interest>& request,
153 const ControlParameters& parameters,
154 const TransactionId transactionId,
155 const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700156
157 void
Vince Lehman4387e782014-06-19 16:57:45 -0500158 onAddNextHopError(uint32_t code, const std::string& error,
159 const shared_ptr<const Interest>& request,
160 const TransactionId transactionId, const bool shouldSendResponse);
161
162 void
163 onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
164 const ControlParameters& parameters,
165 const TransactionId transactionId,
166 const bool shouldSendResponse);
167
168 void
169 onRemoveNextHopError(uint32_t code, const std::string& error,
170 const shared_ptr<const Interest>& request,
171 const TransactionId transactionId, const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700172
173 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700174 onControlHeaderSuccess();
175
176 void
177 onControlHeaderError(uint32_t code, const std::string& reason);
Vince Lehman281ded72014-08-21 12:17:08 -0500178
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700179 static bool
180 extractParameters(const Name::Component& parameterComponent,
181 ControlParameters& extractedParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700182
183 bool
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700184 validateParameters(const ControlCommand& command,
185 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700186
187 void
188 onNotification(const FaceEventNotification& notification);
189
Vince Lehman4387e782014-06-19 16:57:45 -0500190 void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700191 processErasureAfterNotification(uint64_t faceId);
192
193 void
Vince Lehman4387e782014-06-19 16:57:45 -0500194 sendUpdatesToFib(const shared_ptr<const Interest>& request,
195 const ControlParameters& parameters);
196
197 void
198 sendUpdatesToFibAfterFaceDestroyEvent();
199
200 /** \brief Checks if the transaction has received all of the expected responses
201 * from the FIB.
202 * \return{ True if the transaction with the passed transactionId has applied
203 * all of its FIB updates successfully; otherwise, false }
204 */
205 bool
206 isTransactionComplete(const TransactionId transactionId);
207
208 void
209 invalidateTransaction(const TransactionId transactionId);
210
Vince Lehmancd16c832014-07-23 15:14:55 -0700211 void
212 listEntries(const Interest& request);
213
Vince Lehmancd613c52014-07-30 14:34:49 -0500214 void
Vince Lehman26b215c2014-08-17 15:00:41 -0500215 scheduleActiveFaceFetch(const time::seconds& timeToWait);
216
217 void
Vince Lehmancd613c52014-07-30 14:34:49 -0500218 fetchActiveFaces();
219
220 void
221 fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer);
222
223 void
Vince Lehmancd613c52014-07-30 14:34:49 -0500224 onFetchFaceStatusTimeout();
225
Vince Lehman281ded72014-08-21 12:17:08 -0500226PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Vince Lehman26b215c2014-08-17 15:00:41 -0500227 /** \param buffer Face dataset contents
228 */
229 void
230 removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer);
231
232PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700233 Rib m_managedRib;
Vince Lehman281ded72014-08-21 12:17:08 -0500234
235private:
Vince Lehman72446ec2014-07-09 10:50:02 -0500236 ndn::Face& m_face;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700237 ndn::nfd::Controller m_nfdController;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700238 ndn::KeyChain m_keyChain;
Yingdi Yue5224e92014-04-29 18:04:02 -0700239 ndn::ValidatorConfig m_localhostValidator;
240 ndn::ValidatorConfig m_localhopValidator;
Alexander Afanasyev585e5a62014-08-12 11:49:31 -0700241 ndn::nfd::FaceMonitor m_faceMonitor;
Yingdi Yue5224e92014-04-29 18:04:02 -0700242 bool m_isLocalhopEnabled;
Yanbiao Lic17de832014-11-21 17:51:45 -0800243 RemoteRegistrator m_remoteRegistrator;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700244
Vince Lehmancd16c832014-07-23 15:14:55 -0700245 RibStatusPublisher m_ribStatusPublisher;
246
Vince Lehman4387e782014-06-19 16:57:45 -0500247 /** \brief The last transaction ID for FIB update response messages.
248 * Each group of FIB updates applied to the FIB is assigned an incrementing
249 * ID that is used to track the number of successfully applied updates.
250 */
251 TransactionId m_lastTransactionId;
252
253 /// table of FIB update transactions => count of pending FIB updates
254 typedef std::map<TransactionId, int> FibTransactionTable;
255
256 /** \brief Table used to track the number of FIB updates that have not yet received
257 * a response from the FIB.
258 * The table maps a transaction ID to the number of updates remaining for that
259 * specific transaction.
260 */
261 FibTransactionTable m_pendingFibTransactions;
262
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700263 typedef function<void(RibManager*,
264 const shared_ptr<const Interest>& request,
Vince Lehmancd16c832014-07-23 15:14:55 -0700265 ControlParameters& parameters)> SignedVerbProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700266
Vince Lehmancd16c832014-07-23 15:14:55 -0700267 typedef std::map<name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700268
Vince Lehmancd16c832014-07-23 15:14:55 -0700269 typedef std::pair<name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700270
271
Vince Lehmancd16c832014-07-23 15:14:55 -0700272 const SignedVerbDispatchTable m_signedVerbDispatch;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700273
274 static const Name COMMAND_PREFIX; // /localhost/nrd
275 static const Name REMOTE_COMMAND_PREFIX; // /localhop/nrd
276
277 // number of components in an invalid, but not malformed, unsigned command.
278 // (/localhost/nrd + verb + options) = 4
279 static const size_t COMMAND_UNSIGNED_NCOMPS;
280
281 // number of components in a valid signed Interest.
282 // 8 with signed Interest support.
283 static const size_t COMMAND_SIGNED_NCOMPS;
284
Vince Lehmancd16c832014-07-23 15:14:55 -0700285 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
286
287 typedef function<void(RibManager*, const Interest&)> UnsignedVerbProcessor;
288 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
289 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
290
291 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
292 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
293
294 static const Name LIST_COMMAND_PREFIX;
295 static const size_t LIST_COMMAND_NCOMPS;
Vince Lehmancd613c52014-07-30 14:34:49 -0500296
297 static const Name FACES_LIST_DATASET_PREFIX;
298
Vince Lehman26b215c2014-08-17 15:00:41 -0500299 static const time::seconds ACTIVE_FACE_FETCH_INTERVAL;
300 EventId m_activeFaceFetchEvent;
301
302 typedef std::set<uint64_t> FaceIdSet;
303 /** \brief contains FaceIds with one or more Routes in the RIB
304 */
305 FaceIdSet m_registeredFaces;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700306};
307
308} // namespace rib
309} // namespace nfd
310
311#endif // NFD_RIB_RIB_MANAGER_HPP