blob: 1ca86cf9cb62cbc2b3ad6e14013b408b09bfc653 [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"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070032
Alexander Afanasyev4a771362014-04-24 21:29:33 -070033#include <ndn-cxx/security/validator-config.hpp>
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070034#include <ndn-cxx/management/nfd-face-monitor.hpp>
Alexander Afanasyev4a771362014-04-24 21:29:33 -070035#include <ndn-cxx/management/nfd-controller.hpp>
36#include <ndn-cxx/management/nfd-control-command.hpp>
37#include <ndn-cxx/management/nfd-control-response.hpp>
38#include <ndn-cxx/management/nfd-control-parameters.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070039
40namespace nfd {
41namespace rib {
42
43using ndn::nfd::ControlCommand;
44using ndn::nfd::ControlResponse;
45using ndn::nfd::ControlParameters;
46
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070047using ndn::nfd::FaceEventNotification;
48
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070049class RibManager : noncopyable
50{
51public:
Yingdi Yue5224e92014-04-29 18:04:02 -070052 class Error : public std::runtime_error
53 {
54 public:
55 explicit
56 Error(const std::string& what)
57 : std::runtime_error(what)
58 {
59 }
60 };
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070061
Vince Lehman72446ec2014-07-09 10:50:02 -050062 explicit
63 RibManager(ndn::Face& face);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070064
Vince Lehman26b215c2014-08-17 15:00:41 -050065 ~RibManager();
66
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070067 void
68 registerWithNfd();
69
70 void
71 enableLocalControlHeader();
72
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070073 void
74 setConfigFile(ConfigFile& configFile);
75
76private:
Vince Lehman4387e782014-06-19 16:57:45 -050077 typedef uint32_t TransactionId;
78
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070079 void
80 onConfig(const ConfigSection& configSection,
81 bool isDryRun,
82 const std::string& filename);
83
84 void
Junxiao Shia3295742014-05-16 22:40:10 -070085 startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest);
86
87 void
Yingdi Yue5224e92014-04-29 18:04:02 -070088 onLocalhopRequest(const Interest& request);
89
90 void
91 onLocalhostRequest(const Interest& request);
92
93 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070094 sendResponse(const Name& name,
95 const ControlResponse& response);
96
97 void
98 sendResponse(const Name& name,
99 uint32_t code,
100 const std::string& text);
101
102 void
Vince Lehman4387e782014-06-19 16:57:45 -0500103 sendSuccessResponse(const shared_ptr<const Interest>& request,
104 const ControlParameters& parameters);
105
106 void
107 sendErrorResponse(uint32_t code, const std::string& error,
108 const shared_ptr<const Interest>& request);
109
110 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700111 registerEntry(const shared_ptr<const Interest>& request,
112 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700113
114 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700115 unregisterEntry(const shared_ptr<const Interest>& request,
116 ControlParameters& parameters);
117
118 void
Vince Lehman281ded72014-08-21 12:17:08 -0500119 expireEntry(const shared_ptr<const Interest>& request, ControlParameters& params);
120
121 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700122 onCommandValidated(const shared_ptr<const Interest>& request);
123
124 void
125 onCommandValidationFailed(const shared_ptr<const Interest>& request,
126 const std::string& failureInfo);
127
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700128
129 void
130 onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700131 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500132 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700133
134 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700135 onRegSuccess(const shared_ptr<const Interest>& request,
136 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500137 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700138
139 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700140 onUnRegSuccess(const shared_ptr<const Interest>& request,
141 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500142 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700143
144 void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700145 onNrdCommandPrefixAddNextHopSuccess(const Name& prefix);
146
147 void
148 onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg);
149
150 void
Vince Lehman4387e782014-06-19 16:57:45 -0500151 onAddNextHopSuccess(const shared_ptr<const Interest>& request,
152 const ControlParameters& parameters,
153 const TransactionId transactionId,
154 const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700155
156 void
Vince Lehman4387e782014-06-19 16:57:45 -0500157 onAddNextHopError(uint32_t code, const std::string& error,
158 const shared_ptr<const Interest>& request,
159 const TransactionId transactionId, const bool shouldSendResponse);
160
161 void
162 onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
163 const ControlParameters& parameters,
164 const TransactionId transactionId,
165 const bool shouldSendResponse);
166
167 void
168 onRemoveNextHopError(uint32_t code, const std::string& error,
169 const shared_ptr<const Interest>& request,
170 const TransactionId transactionId, const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700171
172 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700173 onControlHeaderSuccess();
174
175 void
176 onControlHeaderError(uint32_t code, const std::string& reason);
Vince Lehman281ded72014-08-21 12:17:08 -0500177
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700178 static bool
179 extractParameters(const Name::Component& parameterComponent,
180 ControlParameters& extractedParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700181
182 bool
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700183 validateParameters(const ControlCommand& command,
184 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700185
186 void
187 onNotification(const FaceEventNotification& notification);
188
Vince Lehman4387e782014-06-19 16:57:45 -0500189 void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700190 processErasureAfterNotification(uint64_t faceId);
191
192 void
Vince Lehman4387e782014-06-19 16:57:45 -0500193 sendUpdatesToFib(const shared_ptr<const Interest>& request,
194 const ControlParameters& parameters);
195
196 void
197 sendUpdatesToFibAfterFaceDestroyEvent();
198
199 /** \brief Checks if the transaction has received all of the expected responses
200 * from the FIB.
201 * \return{ True if the transaction with the passed transactionId has applied
202 * all of its FIB updates successfully; otherwise, false }
203 */
204 bool
205 isTransactionComplete(const TransactionId transactionId);
206
207 void
208 invalidateTransaction(const TransactionId transactionId);
209
Vince Lehmancd16c832014-07-23 15:14:55 -0700210 void
211 listEntries(const Interest& request);
212
Vince Lehmancd613c52014-07-30 14:34:49 -0500213 void
Vince Lehman26b215c2014-08-17 15:00:41 -0500214 scheduleActiveFaceFetch(const time::seconds& timeToWait);
215
216 void
Vince Lehmancd613c52014-07-30 14:34:49 -0500217 fetchActiveFaces();
218
219 void
220 fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer);
221
222 void
Vince Lehmancd613c52014-07-30 14:34:49 -0500223 onFetchFaceStatusTimeout();
224
Vince Lehman281ded72014-08-21 12:17:08 -0500225PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Vince Lehman26b215c2014-08-17 15:00:41 -0500226 /** \param buffer Face dataset contents
227 */
228 void
229 removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer);
230
231PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700232 Rib m_managedRib;
Vince Lehman281ded72014-08-21 12:17:08 -0500233
234private:
Vince Lehman72446ec2014-07-09 10:50:02 -0500235 ndn::Face& m_face;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700236 ndn::nfd::Controller m_nfdController;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700237 ndn::KeyChain m_keyChain;
Yingdi Yue5224e92014-04-29 18:04:02 -0700238 ndn::ValidatorConfig m_localhostValidator;
239 ndn::ValidatorConfig m_localhopValidator;
Alexander Afanasyev585e5a62014-08-12 11:49:31 -0700240 ndn::nfd::FaceMonitor m_faceMonitor;
Yingdi Yue5224e92014-04-29 18:04:02 -0700241 bool m_isLocalhopEnabled;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700242
Vince Lehmancd16c832014-07-23 15:14:55 -0700243 RibStatusPublisher m_ribStatusPublisher;
244
Vince Lehman4387e782014-06-19 16:57:45 -0500245 /** \brief The last transaction ID for FIB update response messages.
246 * Each group of FIB updates applied to the FIB is assigned an incrementing
247 * ID that is used to track the number of successfully applied updates.
248 */
249 TransactionId m_lastTransactionId;
250
251 /// table of FIB update transactions => count of pending FIB updates
252 typedef std::map<TransactionId, int> FibTransactionTable;
253
254 /** \brief Table used to track the number of FIB updates that have not yet received
255 * a response from the FIB.
256 * The table maps a transaction ID to the number of updates remaining for that
257 * specific transaction.
258 */
259 FibTransactionTable m_pendingFibTransactions;
260
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700261 typedef function<void(RibManager*,
262 const shared_ptr<const Interest>& request,
Vince Lehmancd16c832014-07-23 15:14:55 -0700263 ControlParameters& parameters)> SignedVerbProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700264
Vince Lehmancd16c832014-07-23 15:14:55 -0700265 typedef std::map<name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700266
Vince Lehmancd16c832014-07-23 15:14:55 -0700267 typedef std::pair<name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700268
269
Vince Lehmancd16c832014-07-23 15:14:55 -0700270 const SignedVerbDispatchTable m_signedVerbDispatch;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700271
272 static const Name COMMAND_PREFIX; // /localhost/nrd
273 static const Name REMOTE_COMMAND_PREFIX; // /localhop/nrd
274
275 // number of components in an invalid, but not malformed, unsigned command.
276 // (/localhost/nrd + verb + options) = 4
277 static const size_t COMMAND_UNSIGNED_NCOMPS;
278
279 // number of components in a valid signed Interest.
280 // 8 with signed Interest support.
281 static const size_t COMMAND_SIGNED_NCOMPS;
282
Vince Lehmancd16c832014-07-23 15:14:55 -0700283 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
284
285 typedef function<void(RibManager*, const Interest&)> UnsignedVerbProcessor;
286 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
287 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
288
289 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
290 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
291
292 static const Name LIST_COMMAND_PREFIX;
293 static const size_t LIST_COMMAND_NCOMPS;
Vince Lehmancd613c52014-07-30 14:34:49 -0500294
295 static const Name FACES_LIST_DATASET_PREFIX;
296
Vince Lehman26b215c2014-08-17 15:00:41 -0500297 static const time::seconds ACTIVE_FACE_FETCH_INTERVAL;
298 EventId m_activeFaceFetchEvent;
299
300 typedef std::set<uint64_t> FaceIdSet;
301 /** \brief contains FaceIds with one or more Routes in the RIB
302 */
303 FaceIdSet m_registeredFaces;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700304};
305
306} // namespace rib
307} // namespace nfd
308
309#endif // NFD_RIB_RIB_MANAGER_HPP