blob: dffb24a81172f918db07ea2d98f92bd5df281e9b [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
65 void
66 registerWithNfd();
67
68 void
69 enableLocalControlHeader();
70
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070071 void
72 setConfigFile(ConfigFile& configFile);
73
74private:
Vince Lehman4387e782014-06-19 16:57:45 -050075 typedef uint32_t TransactionId;
76
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070077 void
78 onConfig(const ConfigSection& configSection,
79 bool isDryRun,
80 const std::string& filename);
81
82 void
Junxiao Shia3295742014-05-16 22:40:10 -070083 startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest);
84
85 void
Yingdi Yue5224e92014-04-29 18:04:02 -070086 onLocalhopRequest(const Interest& request);
87
88 void
89 onLocalhostRequest(const Interest& request);
90
91 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070092 sendResponse(const Name& name,
93 const ControlResponse& response);
94
95 void
96 sendResponse(const Name& name,
97 uint32_t code,
98 const std::string& text);
99
100 void
Vince Lehman4387e782014-06-19 16:57:45 -0500101 sendSuccessResponse(const shared_ptr<const Interest>& request,
102 const ControlParameters& parameters);
103
104 void
105 sendErrorResponse(uint32_t code, const std::string& error,
106 const shared_ptr<const Interest>& request);
107
108 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700109 registerEntry(const shared_ptr<const Interest>& request,
110 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700111
112 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700113 unregisterEntry(const shared_ptr<const Interest>& request,
114 ControlParameters& parameters);
115
116 void
Vince Lehman281ded72014-08-21 12:17:08 -0500117 expireEntry(const shared_ptr<const Interest>& request, ControlParameters& params);
118
119 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700120 onCommandValidated(const shared_ptr<const Interest>& request);
121
122 void
123 onCommandValidationFailed(const shared_ptr<const Interest>& request,
124 const std::string& failureInfo);
125
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700126
127 void
128 onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700129 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500130 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700131
132 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700133 onRegSuccess(const shared_ptr<const Interest>& request,
134 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500135 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700136
137 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700138 onUnRegSuccess(const shared_ptr<const Interest>& request,
139 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500140 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700141
142 void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700143 onNrdCommandPrefixAddNextHopSuccess(const Name& prefix);
144
145 void
146 onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg);
147
148 void
Vince Lehman4387e782014-06-19 16:57:45 -0500149 onAddNextHopSuccess(const shared_ptr<const Interest>& request,
150 const ControlParameters& parameters,
151 const TransactionId transactionId,
152 const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700153
154 void
Vince Lehman4387e782014-06-19 16:57:45 -0500155 onAddNextHopError(uint32_t code, const std::string& error,
156 const shared_ptr<const Interest>& request,
157 const TransactionId transactionId, const bool shouldSendResponse);
158
159 void
160 onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
161 const ControlParameters& parameters,
162 const TransactionId transactionId,
163 const bool shouldSendResponse);
164
165 void
166 onRemoveNextHopError(uint32_t code, const std::string& error,
167 const shared_ptr<const Interest>& request,
168 const TransactionId transactionId, const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700169
170 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700171 onControlHeaderSuccess();
172
173 void
174 onControlHeaderError(uint32_t code, const std::string& reason);
Vince Lehman281ded72014-08-21 12:17:08 -0500175
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700176 static bool
177 extractParameters(const Name::Component& parameterComponent,
178 ControlParameters& extractedParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700179
180 bool
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700181 validateParameters(const ControlCommand& command,
182 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700183
184 void
185 onNotification(const FaceEventNotification& notification);
186
Vince Lehman4387e782014-06-19 16:57:45 -0500187 void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700188 processErasureAfterNotification(uint64_t faceId);
189
190 void
Vince Lehman4387e782014-06-19 16:57:45 -0500191 sendUpdatesToFib(const shared_ptr<const Interest>& request,
192 const ControlParameters& parameters);
193
194 void
195 sendUpdatesToFibAfterFaceDestroyEvent();
196
197 /** \brief Checks if the transaction has received all of the expected responses
198 * from the FIB.
199 * \return{ True if the transaction with the passed transactionId has applied
200 * all of its FIB updates successfully; otherwise, false }
201 */
202 bool
203 isTransactionComplete(const TransactionId transactionId);
204
205 void
206 invalidateTransaction(const TransactionId transactionId);
207
Vince Lehmancd16c832014-07-23 15:14:55 -0700208 void
209 listEntries(const Interest& request);
210
Vince Lehmancd613c52014-07-30 14:34:49 -0500211 void
212 fetchActiveFaces();
213
214 void
215 fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer);
216
217 void
218 updateActiveFaces(shared_ptr<ndn::OBufferStream> buffer);
219
220 void
221 onFetchFaceStatusTimeout();
222
Vince Lehman281ded72014-08-21 12:17:08 -0500223PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700224 Rib m_managedRib;
Vince Lehman281ded72014-08-21 12:17:08 -0500225
226private:
Vince Lehman72446ec2014-07-09 10:50:02 -0500227 ndn::Face& m_face;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700228 ndn::nfd::Controller m_nfdController;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700229 ndn::KeyChain m_keyChain;
Yingdi Yue5224e92014-04-29 18:04:02 -0700230 ndn::ValidatorConfig m_localhostValidator;
231 ndn::ValidatorConfig m_localhopValidator;
Alexander Afanasyev585e5a62014-08-12 11:49:31 -0700232 ndn::nfd::FaceMonitor m_faceMonitor;
Yingdi Yue5224e92014-04-29 18:04:02 -0700233 bool m_isLocalhopEnabled;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700234
Vince Lehmancd16c832014-07-23 15:14:55 -0700235 RibStatusPublisher m_ribStatusPublisher;
236
Vince Lehman4387e782014-06-19 16:57:45 -0500237 /** \brief The last transaction ID for FIB update response messages.
238 * Each group of FIB updates applied to the FIB is assigned an incrementing
239 * ID that is used to track the number of successfully applied updates.
240 */
241 TransactionId m_lastTransactionId;
242
243 /// table of FIB update transactions => count of pending FIB updates
244 typedef std::map<TransactionId, int> FibTransactionTable;
245
246 /** \brief Table used to track the number of FIB updates that have not yet received
247 * a response from the FIB.
248 * The table maps a transaction ID to the number of updates remaining for that
249 * specific transaction.
250 */
251 FibTransactionTable m_pendingFibTransactions;
252
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700253 typedef function<void(RibManager*,
254 const shared_ptr<const Interest>& request,
Vince Lehmancd16c832014-07-23 15:14:55 -0700255 ControlParameters& parameters)> SignedVerbProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700256
Vince Lehmancd16c832014-07-23 15:14:55 -0700257 typedef std::map<name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700258
Vince Lehmancd16c832014-07-23 15:14:55 -0700259 typedef std::pair<name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700260
261
Vince Lehmancd16c832014-07-23 15:14:55 -0700262 const SignedVerbDispatchTable m_signedVerbDispatch;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700263
264 static const Name COMMAND_PREFIX; // /localhost/nrd
265 static const Name REMOTE_COMMAND_PREFIX; // /localhop/nrd
266
267 // number of components in an invalid, but not malformed, unsigned command.
268 // (/localhost/nrd + verb + options) = 4
269 static const size_t COMMAND_UNSIGNED_NCOMPS;
270
271 // number of components in a valid signed Interest.
272 // 8 with signed Interest support.
273 static const size_t COMMAND_SIGNED_NCOMPS;
274
Vince Lehmancd16c832014-07-23 15:14:55 -0700275 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
276
277 typedef function<void(RibManager*, const Interest&)> UnsignedVerbProcessor;
278 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
279 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
280
281 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
282 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
283
284 static const Name LIST_COMMAND_PREFIX;
285 static const size_t LIST_COMMAND_NCOMPS;
Vince Lehmancd613c52014-07-30 14:34:49 -0500286
287 static const Name FACES_LIST_DATASET_PREFIX;
288
289PUBLIC_WITH_TESTS_ELSE_PRIVATE:
290 std::set<int> activeFaces;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700291};
292
293} // namespace rib
294} // namespace nfd
295
296#endif // NFD_RIB_RIB_MANAGER_HPP