blob: c31256ef65c133c22c71262612a5c345ad14d83e [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"
30#include "face-monitor.hpp"
31#include "core/config-file.hpp"
Vince Lehmancd16c832014-07-23 15:14:55 -070032#include "rib-status-publisher.hpp"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070033
Alexander Afanasyev4a771362014-04-24 21:29:33 -070034#include <ndn-cxx/security/validator-config.hpp>
35#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
47class RibManager : noncopyable
48{
49public:
Yingdi Yue5224e92014-04-29 18:04:02 -070050 class Error : public std::runtime_error
51 {
52 public:
53 explicit
54 Error(const std::string& what)
55 : std::runtime_error(what)
56 {
57 }
58 };
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070059
Vince Lehman72446ec2014-07-09 10:50:02 -050060 explicit
61 RibManager(ndn::Face& face);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070062
63 void
64 registerWithNfd();
65
66 void
67 enableLocalControlHeader();
68
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070069 void
70 setConfigFile(ConfigFile& configFile);
71
72private:
Vince Lehman4387e782014-06-19 16:57:45 -050073 typedef uint32_t TransactionId;
74
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070075 void
76 onConfig(const ConfigSection& configSection,
77 bool isDryRun,
78 const std::string& filename);
79
80 void
Junxiao Shia3295742014-05-16 22:40:10 -070081 startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest);
82
83 void
Yingdi Yue5224e92014-04-29 18:04:02 -070084 onLocalhopRequest(const Interest& request);
85
86 void
87 onLocalhostRequest(const Interest& request);
88
89 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070090 sendResponse(const Name& name,
91 const ControlResponse& response);
92
93 void
94 sendResponse(const Name& name,
95 uint32_t code,
96 const std::string& text);
97
98 void
Vince Lehman4387e782014-06-19 16:57:45 -050099 sendSuccessResponse(const shared_ptr<const Interest>& request,
100 const ControlParameters& parameters);
101
102 void
103 sendErrorResponse(uint32_t code, const std::string& error,
104 const shared_ptr<const Interest>& request);
105
106 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700107 registerEntry(const shared_ptr<const Interest>& request,
108 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700109
110 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700111 unregisterEntry(const shared_ptr<const Interest>& request,
112 ControlParameters& parameters);
113
114 void
115 onCommandValidated(const shared_ptr<const Interest>& request);
116
117 void
118 onCommandValidationFailed(const shared_ptr<const Interest>& request,
119 const std::string& failureInfo);
120
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700121
122 void
123 onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700124 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500125 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700126
127 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700128 onRegSuccess(const shared_ptr<const Interest>& request,
129 const ControlParameters& parameters,
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 onUnRegSuccess(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 Afanasyevb3893c92014-05-15 01:49:54 -0700138 onNrdCommandPrefixAddNextHopSuccess(const Name& prefix);
139
140 void
141 onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg);
142
143 void
Vince Lehman4387e782014-06-19 16:57:45 -0500144 onAddNextHopSuccess(const shared_ptr<const Interest>& request,
145 const ControlParameters& parameters,
146 const TransactionId transactionId,
147 const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700148
149 void
Vince Lehman4387e782014-06-19 16:57:45 -0500150 onAddNextHopError(uint32_t code, const std::string& error,
151 const shared_ptr<const Interest>& request,
152 const TransactionId transactionId, const bool shouldSendResponse);
153
154 void
155 onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
156 const ControlParameters& parameters,
157 const TransactionId transactionId,
158 const bool shouldSendResponse);
159
160 void
161 onRemoveNextHopError(uint32_t code, const std::string& error,
162 const shared_ptr<const Interest>& request,
163 const TransactionId transactionId, const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700164
165 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700166 onControlHeaderSuccess();
167
168 void
169 onControlHeaderError(uint32_t code, const std::string& reason);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700170 static bool
171 extractParameters(const Name::Component& parameterComponent,
172 ControlParameters& extractedParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700173
174 bool
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700175 validateParameters(const ControlCommand& command,
176 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700177
178 void
179 onNotification(const FaceEventNotification& notification);
180
Vince Lehman4387e782014-06-19 16:57:45 -0500181 void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700182 processErasureAfterNotification(uint64_t faceId);
183
184 void
Vince Lehman4387e782014-06-19 16:57:45 -0500185 sendUpdatesToFib(const shared_ptr<const Interest>& request,
186 const ControlParameters& parameters);
187
188 void
189 sendUpdatesToFibAfterFaceDestroyEvent();
190
191 /** \brief Checks if the transaction has received all of the expected responses
192 * from the FIB.
193 * \return{ True if the transaction with the passed transactionId has applied
194 * all of its FIB updates successfully; otherwise, false }
195 */
196 bool
197 isTransactionComplete(const TransactionId transactionId);
198
199 void
200 invalidateTransaction(const TransactionId transactionId);
201
Vince Lehmancd16c832014-07-23 15:14:55 -0700202 void
203 listEntries(const Interest& request);
204
Vince Lehmancd613c52014-07-30 14:34:49 -0500205 void
206 fetchActiveFaces();
207
208 void
209 fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer);
210
211 void
212 updateActiveFaces(shared_ptr<ndn::OBufferStream> buffer);
213
214 void
215 onFetchFaceStatusTimeout();
216
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700217private:
218 Rib m_managedRib;
Vince Lehman72446ec2014-07-09 10:50:02 -0500219 ndn::Face& m_face;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700220 ndn::nfd::Controller m_nfdController;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700221 ndn::KeyChain m_keyChain;
Yingdi Yue5224e92014-04-29 18:04:02 -0700222 ndn::ValidatorConfig m_localhostValidator;
223 ndn::ValidatorConfig m_localhopValidator;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700224 FaceMonitor m_faceMonitor;
Yingdi Yue5224e92014-04-29 18:04:02 -0700225 bool m_isLocalhopEnabled;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700226
Vince Lehmancd16c832014-07-23 15:14:55 -0700227 RibStatusPublisher m_ribStatusPublisher;
228
Vince Lehman4387e782014-06-19 16:57:45 -0500229 /** \brief The last transaction ID for FIB update response messages.
230 * Each group of FIB updates applied to the FIB is assigned an incrementing
231 * ID that is used to track the number of successfully applied updates.
232 */
233 TransactionId m_lastTransactionId;
234
235 /// table of FIB update transactions => count of pending FIB updates
236 typedef std::map<TransactionId, int> FibTransactionTable;
237
238 /** \brief Table used to track the number of FIB updates that have not yet received
239 * a response from the FIB.
240 * The table maps a transaction ID to the number of updates remaining for that
241 * specific transaction.
242 */
243 FibTransactionTable m_pendingFibTransactions;
244
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700245 typedef function<void(RibManager*,
246 const shared_ptr<const Interest>& request,
Vince Lehmancd16c832014-07-23 15:14:55 -0700247 ControlParameters& parameters)> SignedVerbProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700248
Vince Lehmancd16c832014-07-23 15:14:55 -0700249 typedef std::map<name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700250
Vince Lehmancd16c832014-07-23 15:14:55 -0700251 typedef std::pair<name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700252
253
Vince Lehmancd16c832014-07-23 15:14:55 -0700254 const SignedVerbDispatchTable m_signedVerbDispatch;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700255
256 static const Name COMMAND_PREFIX; // /localhost/nrd
257 static const Name REMOTE_COMMAND_PREFIX; // /localhop/nrd
258
259 // number of components in an invalid, but not malformed, unsigned command.
260 // (/localhost/nrd + verb + options) = 4
261 static const size_t COMMAND_UNSIGNED_NCOMPS;
262
263 // number of components in a valid signed Interest.
264 // 8 with signed Interest support.
265 static const size_t COMMAND_SIGNED_NCOMPS;
266
Vince Lehmancd16c832014-07-23 15:14:55 -0700267 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
268
269 typedef function<void(RibManager*, const Interest&)> UnsignedVerbProcessor;
270 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
271 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
272
273 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
274 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
275
276 static const Name LIST_COMMAND_PREFIX;
277 static const size_t LIST_COMMAND_NCOMPS;
Vince Lehmancd613c52014-07-30 14:34:49 -0500278
279 static const Name FACES_LIST_DATASET_PREFIX;
280
281PUBLIC_WITH_TESTS_ELSE_PRIVATE:
282 std::set<int> activeFaces;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700283};
284
285} // namespace rib
286} // namespace nfd
287
288#endif // NFD_RIB_RIB_MANAGER_HPP