blob: d08ab5004066f3f7ddb73fa5554adccd5301a904 [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
117 onCommandValidated(const shared_ptr<const Interest>& request);
118
119 void
120 onCommandValidationFailed(const shared_ptr<const Interest>& request,
121 const std::string& failureInfo);
122
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700123
124 void
125 onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700126 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500127 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700128
129 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700130 onRegSuccess(const shared_ptr<const Interest>& request,
131 const ControlParameters& parameters,
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 onUnRegSuccess(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 Afanasyevb3893c92014-05-15 01:49:54 -0700140 onNrdCommandPrefixAddNextHopSuccess(const Name& prefix);
141
142 void
143 onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg);
144
145 void
Vince Lehman4387e782014-06-19 16:57:45 -0500146 onAddNextHopSuccess(const shared_ptr<const Interest>& request,
147 const ControlParameters& parameters,
148 const TransactionId transactionId,
149 const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700150
151 void
Vince Lehman4387e782014-06-19 16:57:45 -0500152 onAddNextHopError(uint32_t code, const std::string& error,
153 const shared_ptr<const Interest>& request,
154 const TransactionId transactionId, const bool shouldSendResponse);
155
156 void
157 onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
158 const ControlParameters& parameters,
159 const TransactionId transactionId,
160 const bool shouldSendResponse);
161
162 void
163 onRemoveNextHopError(uint32_t code, const std::string& error,
164 const shared_ptr<const Interest>& request,
165 const TransactionId transactionId, const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700166
167 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700168 onControlHeaderSuccess();
169
170 void
171 onControlHeaderError(uint32_t code, const std::string& reason);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700172 static bool
173 extractParameters(const Name::Component& parameterComponent,
174 ControlParameters& extractedParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700175
176 bool
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700177 validateParameters(const ControlCommand& command,
178 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700179
180 void
181 onNotification(const FaceEventNotification& notification);
182
Vince Lehman4387e782014-06-19 16:57:45 -0500183 void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700184 processErasureAfterNotification(uint64_t faceId);
185
186 void
Vince Lehman4387e782014-06-19 16:57:45 -0500187 sendUpdatesToFib(const shared_ptr<const Interest>& request,
188 const ControlParameters& parameters);
189
190 void
191 sendUpdatesToFibAfterFaceDestroyEvent();
192
193 /** \brief Checks if the transaction has received all of the expected responses
194 * from the FIB.
195 * \return{ True if the transaction with the passed transactionId has applied
196 * all of its FIB updates successfully; otherwise, false }
197 */
198 bool
199 isTransactionComplete(const TransactionId transactionId);
200
201 void
202 invalidateTransaction(const TransactionId transactionId);
203
Vince Lehmancd16c832014-07-23 15:14:55 -0700204 void
205 listEntries(const Interest& request);
206
Vince Lehmancd613c52014-07-30 14:34:49 -0500207 void
208 fetchActiveFaces();
209
210 void
211 fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer);
212
213 void
214 updateActiveFaces(shared_ptr<ndn::OBufferStream> buffer);
215
216 void
217 onFetchFaceStatusTimeout();
218
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700219private:
220 Rib m_managedRib;
Vince Lehman72446ec2014-07-09 10:50:02 -0500221 ndn::Face& m_face;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700222 ndn::nfd::Controller m_nfdController;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700223 ndn::KeyChain m_keyChain;
Yingdi Yue5224e92014-04-29 18:04:02 -0700224 ndn::ValidatorConfig m_localhostValidator;
225 ndn::ValidatorConfig m_localhopValidator;
Alexander Afanasyev585e5a62014-08-12 11:49:31 -0700226 ndn::nfd::FaceMonitor m_faceMonitor;
Yingdi Yue5224e92014-04-29 18:04:02 -0700227 bool m_isLocalhopEnabled;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700228
Vince Lehmancd16c832014-07-23 15:14:55 -0700229 RibStatusPublisher m_ribStatusPublisher;
230
Vince Lehman4387e782014-06-19 16:57:45 -0500231 /** \brief The last transaction ID for FIB update response messages.
232 * Each group of FIB updates applied to the FIB is assigned an incrementing
233 * ID that is used to track the number of successfully applied updates.
234 */
235 TransactionId m_lastTransactionId;
236
237 /// table of FIB update transactions => count of pending FIB updates
238 typedef std::map<TransactionId, int> FibTransactionTable;
239
240 /** \brief Table used to track the number of FIB updates that have not yet received
241 * a response from the FIB.
242 * The table maps a transaction ID to the number of updates remaining for that
243 * specific transaction.
244 */
245 FibTransactionTable m_pendingFibTransactions;
246
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700247 typedef function<void(RibManager*,
248 const shared_ptr<const Interest>& request,
Vince Lehmancd16c832014-07-23 15:14:55 -0700249 ControlParameters& parameters)> SignedVerbProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700250
Vince Lehmancd16c832014-07-23 15:14:55 -0700251 typedef std::map<name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700252
Vince Lehmancd16c832014-07-23 15:14:55 -0700253 typedef std::pair<name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700254
255
Vince Lehmancd16c832014-07-23 15:14:55 -0700256 const SignedVerbDispatchTable m_signedVerbDispatch;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700257
258 static const Name COMMAND_PREFIX; // /localhost/nrd
259 static const Name REMOTE_COMMAND_PREFIX; // /localhop/nrd
260
261 // number of components in an invalid, but not malformed, unsigned command.
262 // (/localhost/nrd + verb + options) = 4
263 static const size_t COMMAND_UNSIGNED_NCOMPS;
264
265 // number of components in a valid signed Interest.
266 // 8 with signed Interest support.
267 static const size_t COMMAND_SIGNED_NCOMPS;
268
Vince Lehmancd16c832014-07-23 15:14:55 -0700269 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
270
271 typedef function<void(RibManager*, const Interest&)> UnsignedVerbProcessor;
272 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
273 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
274
275 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
276 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
277
278 static const Name LIST_COMMAND_PREFIX;
279 static const size_t LIST_COMMAND_NCOMPS;
Vince Lehmancd613c52014-07-30 14:34:49 -0500280
281 static const Name FACES_LIST_DATASET_PREFIX;
282
283PUBLIC_WITH_TESTS_ELSE_PRIVATE:
284 std::set<int> activeFaces;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700285};
286
287} // namespace rib
288} // namespace nfd
289
290#endif // NFD_RIB_RIB_MANAGER_HPP