blob: 993d84b6ea62efa02da6e53a8dd21628eb5b884b [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#include "rib-manager.hpp"
Alexander Afanasyev03ea3eb2014-04-17 18:19:06 -070027#include "core/global-io.hpp"
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070028#include "core/logger.hpp"
Alexander Afanasyev63108c42014-07-07 19:10:47 -070029#include "core/scheduler.hpp"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070030
31namespace nfd {
32namespace rib {
33
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070034NFD_LOG_INIT("RibManager");
35
Alexander Afanasyev20d31442014-04-19 17:00:53 -070036const Name RibManager::COMMAND_PREFIX = "/localhost/nfd/rib";
37const Name RibManager::REMOTE_COMMAND_PREFIX = "/localhop/nfd/rib";
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070038
39const size_t RibManager::COMMAND_UNSIGNED_NCOMPS =
40 RibManager::COMMAND_PREFIX.size() +
41 1 + // verb
42 1; // verb options
43
44const size_t RibManager::COMMAND_SIGNED_NCOMPS =
45 RibManager::COMMAND_UNSIGNED_NCOMPS +
46 4; // (timestamp, nonce, signed info tlv, signature tlv)
47
Vince Lehmancd16c832014-07-23 15:14:55 -070048const RibManager::SignedVerbAndProcessor RibManager::SIGNED_COMMAND_VERBS[] =
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070049 {
Vince Lehmancd16c832014-07-23 15:14:55 -070050 SignedVerbAndProcessor(
51 Name::Component("register"),
52 &RibManager::registerEntry
53 ),
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070054
Vince Lehmancd16c832014-07-23 15:14:55 -070055 SignedVerbAndProcessor(
56 Name::Component("unregister"),
57 &RibManager::unregisterEntry
58 ),
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070059 };
60
Vince Lehmancd16c832014-07-23 15:14:55 -070061const RibManager::UnsignedVerbAndProcessor RibManager::UNSIGNED_COMMAND_VERBS[] =
62 {
63 UnsignedVerbAndProcessor(
64 Name::Component("list"),
65 &RibManager::listEntries
66 ),
67 };
68
69const Name RibManager::LIST_COMMAND_PREFIX("/localhost/nfd/rib/list");
70const size_t RibManager::LIST_COMMAND_NCOMPS = LIST_COMMAND_PREFIX.size();
71
Vince Lehman72446ec2014-07-09 10:50:02 -050072RibManager::RibManager(ndn::Face& face)
73 : m_face(face)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070074 , m_nfdController(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070075 , m_localhostValidator(m_face)
76 , m_localhopValidator(m_face)
Yingdi Yuf4db0b52014-04-17 13:17:39 -070077 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070078 , m_isLocalhopEnabled(false)
Vince Lehmancd16c832014-07-23 15:14:55 -070079 , m_ribStatusPublisher(m_managedRib, face, LIST_COMMAND_PREFIX, m_keyChain)
Vince Lehman4387e782014-06-19 16:57:45 -050080 , m_lastTransactionId(0)
Vince Lehmancd16c832014-07-23 15:14:55 -070081 , m_signedVerbDispatch(SIGNED_COMMAND_VERBS,
82 SIGNED_COMMAND_VERBS +
83 (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor)))
84 , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS,
85 UNSIGNED_COMMAND_VERBS +
86 (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor)))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070087{
88}
89
90void
Junxiao Shia3295742014-05-16 22:40:10 -070091RibManager::startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest)
92{
93 NFD_LOG_INFO("Listening on: " << commandPrefix);
94
95 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
96 ControlParameters()
97 .setName(commandPrefix)
98 .setFaceId(0),
99 bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(commandPrefix)),
100 bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(commandPrefix), _2));
101
102 m_face.setInterestFilter(commandPrefix, onRequest);
103}
104
105void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700106RibManager::registerWithNfd()
107{
108 //check whether the components of localhop and localhost prefixes are same
109 BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size());
110
Junxiao Shia3295742014-05-16 22:40:10 -0700111 this->startListening(COMMAND_PREFIX, bind(&RibManager::onLocalhostRequest, this, _2));
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700112
Junxiao Shia3295742014-05-16 22:40:10 -0700113 if (m_isLocalhopEnabled) {
114 this->startListening(REMOTE_COMMAND_PREFIX,
115 bind(&RibManager::onLocalhopRequest, this, _2));
116 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700117
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700118 NFD_LOG_INFO("Start monitoring face create/destroy events");
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700119 m_faceMonitor.addSubscriber(bind(&RibManager::onNotification, this, _1));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700120 m_faceMonitor.startNotifications();
121}
122
123void
124RibManager::setConfigFile(ConfigFile& configFile)
125{
Yingdi Yue5224e92014-04-29 18:04:02 -0700126 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700127 bind(&RibManager::onConfig, this, _1, _2, _3));
128}
129
130void
131RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700132 bool isDryRun,
133 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700134{
Yingdi Yue5224e92014-04-29 18:04:02 -0700135 for (ConfigSection::const_iterator i = configSection.begin();
136 i != configSection.end(); ++i)
137 {
138 if (i->first == "localhost_security")
139 m_localhostValidator.load(i->second, filename);
140 else if (i->first == "localhop_security")
141 {
142 m_localhopValidator.load(i->second, filename);
143 m_isLocalhopEnabled = true;
144 }
145 else
146 throw Error("Unrecognized rib property: " + i->first);
147 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700148}
149
150void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700151RibManager::sendResponse(const Name& name,
152 const ControlResponse& response)
153{
154 const Block& encodedControl = response.wireEncode();
155
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700156 shared_ptr<Data> responseData = make_shared<Data>(name);
157 responseData->setContent(encodedControl);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700158
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700159 m_keyChain.sign(*responseData);
160 m_face.put(*responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700161}
162
163void
164RibManager::sendResponse(const Name& name,
165 uint32_t code,
166 const std::string& text)
167{
168 ControlResponse response(code, text);
169 sendResponse(name, response);
170}
171
172void
Yingdi Yue5224e92014-04-29 18:04:02 -0700173RibManager::onLocalhostRequest(const Interest& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700174{
Vince Lehmancd16c832014-07-23 15:14:55 -0700175 const Name& command = request.getName();
176 const Name::Component& verb = command.get(COMMAND_PREFIX.size());
177
178 UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
179
180 if (unsignedVerbProcessor != m_unsignedVerbDispatch.end())
181 {
182 NFD_LOG_DEBUG("command result: processing unsigned verb: " << verb);
183 (unsignedVerbProcessor->second)(this, request);
184 }
185 else
186 {
187 m_localhostValidator.validate(request,
188 bind(&RibManager::onCommandValidated, this, _1),
189 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
190 }
Yingdi Yue5224e92014-04-29 18:04:02 -0700191}
192
193void
194RibManager::onLocalhopRequest(const Interest& request)
195{
196 m_localhopValidator.validate(request,
197 bind(&RibManager::onCommandValidated, this, _1),
198 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700199}
200
201void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700202RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700203{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700204 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700205 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700206
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700207 const Name& command = request->getName();
208 const Name::Component& verb = command[COMMAND_PREFIX.size()];
209 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
210
Vince Lehmancd16c832014-07-23 15:14:55 -0700211 SignedVerbDispatchTable::const_iterator verbProcessor = m_signedVerbDispatch.find(verb);
212 if (verbProcessor != m_signedVerbDispatch.end())
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700213 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700214 ControlParameters parameters;
215 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700216 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700217 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500218 if (static_cast<bool>(request))
219 sendResponse(command, 400, "Malformed command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700220 return;
221 }
222
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700223 NFD_LOG_DEBUG("command result: processing verb: " << verb);
224 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700225 }
226 else
227 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700228 NFD_LOG_DEBUG("Unsupported command: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500229 if (static_cast<bool>(request))
230 sendResponse(request->getName(), 501, "Unsupported command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700231 }
232}
233
234void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700235RibManager::registerEntry(const shared_ptr<const Interest>& request,
236 ControlParameters& parameters)
237{
238 ndn::nfd::RibRegisterCommand command;
239
240 if (!validateParameters(command, parameters))
241 {
242 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
Syed Obaid3313a372014-07-01 01:31:33 -0500243 if (static_cast<bool>(request))
244 sendResponse(request->getName(), 400, "Malformed command");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700245 return;
246 }
247
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700248 if (!parameters.hasFaceId() || parameters.getFaceId() == 0)
249 {
250 parameters.setFaceId(request->getIncomingFaceId());
251 }
252
Vince12e49462014-06-09 13:29:32 -0500253 FaceEntry faceEntry;
254 faceEntry.faceId = parameters.getFaceId();
255 faceEntry.origin = parameters.getOrigin();
256 faceEntry.cost = parameters.getCost();
257 faceEntry.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500258
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700259 if (parameters.hasExpirationPeriod() &&
260 parameters.getExpirationPeriod() != time::milliseconds::max())
Syed Obaid3313a372014-07-01 01:31:33 -0500261 {
262 faceEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
263
264 // Schedule a new event, the old one will be cancelled during rib insertion.
265 EventId eventId;
266 NFD_LOG_TRACE("scheduling unregistration at: " << faceEntry.expires);
267 eventId = scheduler::schedule(parameters.getExpirationPeriod(),
268 bind(&RibManager::unregisterEntry,
269 this, shared_ptr<Interest>(), parameters));
270
271 //set the NewEventId of this entry
272 faceEntry.setExpirationEvent(eventId);
273 }
274 else
275 {
276 faceEntry.expires = time::steady_clock::TimePoint::max();
277 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700278
Vince12e49462014-06-09 13:29:32 -0500279 NFD_LOG_TRACE("register prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700280
Vince12e49462014-06-09 13:29:32 -0500281 m_managedRib.insert(parameters.getName(), faceEntry);
Vince Lehman4387e782014-06-19 16:57:45 -0500282
283 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700284}
285
286void
287RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
Syed Obaid3313a372014-07-01 01:31:33 -0500288 ControlParameters& params)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700289{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700290 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700291
Syed Obaid3313a372014-07-01 01:31:33 -0500292 //passing all parameters gives error in validation.
293 //so passing only the required arguments.
294 ControlParameters parameters;
295 parameters.setName(params.getName());
Alexander Afanasyevb609f002014-07-18 16:56:34 -0700296 if (params.hasFaceId())
297 parameters.setFaceId(params.getFaceId());
298 if (params.hasOrigin())
299 parameters.setOrigin(params.getOrigin());
Syed Obaid3313a372014-07-01 01:31:33 -0500300
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700301 if (!validateParameters(command, parameters))
302 {
Syed Obaid3313a372014-07-01 01:31:33 -0500303 NFD_LOG_DEBUG("unregister result: FAIL reason: malformed");
304 if (static_cast<bool>(request))
305 sendResponse(request->getName(), 400, "Malformed command");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700306 return;
307 }
308
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700309 if (!parameters.hasFaceId() || parameters.getFaceId() == 0)
310 {
311 parameters.setFaceId(request->getIncomingFaceId());
312 }
313
Vince12e49462014-06-09 13:29:32 -0500314 FaceEntry faceEntry;
315 faceEntry.faceId = parameters.getFaceId();
316 faceEntry.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700317
Vince12e49462014-06-09 13:29:32 -0500318 NFD_LOG_TRACE("unregister prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700319
Vince Lehman4387e782014-06-19 16:57:45 -0500320 m_managedRib.erase(parameters.getName(), faceEntry);
321
322 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700323}
324
325void
326RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
327 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700328{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700329 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Syed Obaid3313a372014-07-01 01:31:33 -0500330 if (static_cast<bool>(request))
331 sendResponse(request->getName(), 403, failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700332}
333
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700334
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700335bool
336RibManager::extractParameters(const Name::Component& parameterComponent,
337 ControlParameters& extractedParameters)
338{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700339 try
340 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700341 Block rawParameters = parameterComponent.blockFromValue();
342 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700343 }
344 catch (const ndn::Tlv::Error& e)
345 {
346 return false;
347 }
348
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700349 NFD_LOG_DEBUG("Parameters parsed OK");
350 return true;
351}
352
353bool
354RibManager::validateParameters(const ControlCommand& command,
355 ControlParameters& parameters)
356{
357 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700358 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700359 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700360 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700361 catch (const ControlCommand::ArgumentError&)
362 {
363 return false;
364 }
365
366 command.applyDefaultsToRequest(parameters);
367
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700368 return true;
369}
370
371void
372RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700373 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500374 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700375{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700376 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700377
378 ControlResponse response;
379
380 if (code == 404)
381 {
382 response.setCode(code);
383 response.setText(error);
384 }
385 else
386 {
387 response.setCode(533);
388 std::ostringstream os;
389 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
390 response.setText(os.str());
391 }
392
Syed Obaid3313a372014-07-01 01:31:33 -0500393 if (static_cast<bool>(request))
394 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700395}
396
397void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700398RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
399 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500400 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700401{
402 ControlResponse response;
403
404 response.setCode(200);
405 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700406 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700407
Vince12e49462014-06-09 13:29:32 -0500408 NFD_LOG_TRACE("onRegSuccess: registered " << faceEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700409
Syed Obaid3313a372014-07-01 01:31:33 -0500410 if (static_cast<bool>(request))
411 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700412}
413
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700414
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700415void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700416RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
417 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500418 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700419{
420 ControlResponse response;
421
422 response.setCode(200);
423 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700424 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700425
Vince12e49462014-06-09 13:29:32 -0500426 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700427
Syed Obaid3313a372014-07-01 01:31:33 -0500428 if (static_cast<bool>(request))
429 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500430}
431
432void
433RibManager::sendSuccessResponse(const shared_ptr<const Interest>& request,
434 const ControlParameters& parameters)
435{
436 if (!static_cast<bool>(request))
437 {
438 return;
439 }
440
441 ControlResponse response;
442
443 response.setCode(200);
444 response.setText("Success");
445 response.setBody(parameters.wireEncode());
446
Syed Obaid3313a372014-07-01 01:31:33 -0500447 if (static_cast<bool>(request))
448 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500449}
450
451void
452RibManager::sendErrorResponse(uint32_t code, const std::string& error,
453 const shared_ptr<const Interest>& request)
454{
455 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
456
457 if (!static_cast<bool>(request))
458 {
459 return;
460 }
461
462 ControlResponse response;
463
464 if (code == 404)
465 {
466 response.setCode(code);
467 response.setText(error);
468 }
469 else
470 {
471 response.setCode(533);
472 std::ostringstream os;
473 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
474 response.setText(os.str());
475 }
476
Syed Obaid3313a372014-07-01 01:31:33 -0500477 if (static_cast<bool>(request))
478 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700479}
480
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700481void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700482RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix)
483{
484 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
485}
486
487void
488RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg)
489{
490 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
491}
492
Vince Lehman4387e782014-06-19 16:57:45 -0500493bool
494RibManager::isTransactionComplete(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700495{
Vince Lehman4387e782014-06-19 16:57:45 -0500496 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
497
498 if (it != m_pendingFibTransactions.end())
499 {
500 int& updatesLeft = it->second;
501
502 updatesLeft--;
503
504 // All of the updates have been applied successfully
505 if (updatesLeft == 0)
506 {
507 m_pendingFibTransactions.erase(it);
508 return true;
509 }
510 }
511
512 return false;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700513}
514
515void
Vince Lehman4387e782014-06-19 16:57:45 -0500516RibManager::invalidateTransaction(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700517{
Vince Lehman4387e782014-06-19 16:57:45 -0500518 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
519
520 if (it != m_pendingFibTransactions.end())
521 {
522 m_pendingFibTransactions.erase(it);
523 }
524}
525
526void
527RibManager::onAddNextHopSuccess(const shared_ptr<const Interest>& request,
528 const ControlParameters& parameters,
529 const TransactionId transactionId,
530 const bool shouldSendResponse)
531{
532 if (isTransactionComplete(transactionId) && shouldSendResponse)
533 {
534 sendSuccessResponse(request, parameters);
535 }
536}
537
538void
539RibManager::onAddNextHopError(uint32_t code, const std::string& error,
540 const shared_ptr<const Interest>& request,
541 const TransactionId transactionId, const bool shouldSendResponse)
542{
543 invalidateTransaction(transactionId);
544
545 if (shouldSendResponse)
546 {
547 sendErrorResponse(code, error, request);
548 }
549}
550
551void
552RibManager::onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
553 const ControlParameters& parameters,
554 const TransactionId transactionId,
555 const bool shouldSendResponse)
556{
557 if (isTransactionComplete(transactionId) && shouldSendResponse)
558 {
559 sendSuccessResponse(request, parameters);
560 }
561}
562
563void
564RibManager::onRemoveNextHopError(uint32_t code, const std::string& error,
565 const shared_ptr<const Interest>& request,
566 const TransactionId transactionId, const bool shouldSendResponse)
567{
568 invalidateTransaction(transactionId);
569
570 if (shouldSendResponse)
571 {
572 sendErrorResponse(code, error, request);
573 }
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700574}
575
576void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700577RibManager::onControlHeaderSuccess()
578{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700579 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700580}
581
582void
583RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
584{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700585 std::ostringstream os;
586 os << "Couldn't enable local control header "
587 << "(code: " << code << ", info: " << reason << ")";
588 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700589}
590
591void
592RibManager::enableLocalControlHeader()
593{
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700594 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700595 ControlParameters()
596 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
597 bind(&RibManager::onControlHeaderSuccess, this),
598 bind(&RibManager::onControlHeaderError, this, _1, _2));
599}
600
601void
602RibManager::onNotification(const FaceEventNotification& notification)
603{
604 /// \todo A notification can be missed, in this case check Facelist
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700605 NFD_LOG_TRACE("onNotification: " << notification);
Vince Lehman4387e782014-06-19 16:57:45 -0500606 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) //face destroyed
607 {
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700608 scheduler::schedule(time::seconds(0),
609 bind(&RibManager::processErasureAfterNotification, this,
610 notification.getFaceId()));
Vince Lehman4387e782014-06-19 16:57:45 -0500611 }
612}
613
614void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700615RibManager::processErasureAfterNotification(uint64_t faceId)
616{
617 m_managedRib.erase(faceId);
618
619 sendUpdatesToFibAfterFaceDestroyEvent();
620}
621
622void
Vince Lehman4387e782014-06-19 16:57:45 -0500623RibManager::sendUpdatesToFib(const shared_ptr<const Interest>& request,
624 const ControlParameters& parameters)
625{
626 const Rib::FibUpdateList& updates = m_managedRib.getFibUpdates();
627
628 // If no updates were generated, consider the operation a success
629 if (updates.empty())
630 {
631 sendSuccessResponse(request, parameters);
632 return;
633 }
634
635 bool shouldWaitToRespond = false;
636
637 // An application request should wait for all FIB updates to be applied
638 // successfully before sending a response
639 if (parameters.getOrigin() == ndn::nfd::ROUTE_ORIGIN_APP)
640 {
641 shouldWaitToRespond = true;
642 }
643 else // Respond immediately
644 {
645 sendSuccessResponse(request, parameters);
646 }
647
648 NFD_LOG_DEBUG("Applying " << updates.size() << " updates to FIB");
649
650 // Assign an ID to this FIB transaction
651 TransactionId currentTransactionId = ++m_lastTransactionId;
652
653 // Add this transaction to the transaction table
654 m_pendingFibTransactions[currentTransactionId] = updates.size();
655
656 for (Rib::FibUpdateList::const_iterator it = updates.begin(); it != updates.end(); ++it)
657 {
658 shared_ptr<const FibUpdate> update(*it);
659
660 if (update->action == FibUpdate::ADD_NEXTHOP)
661 {
662 FaceEntry faceEntry;
663 faceEntry.faceId = update->faceId;
664 faceEntry.cost = update->cost;
665
666 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
667 ControlParameters()
668 .setName(update->name)
669 .setFaceId(faceEntry.faceId)
670 .setCost(faceEntry.cost),
671 bind(&RibManager::onAddNextHopSuccess, this, request,
672 parameters,
673 currentTransactionId,
674 shouldWaitToRespond),
675 bind(&RibManager::onAddNextHopError, this, _1, _2, request, currentTransactionId,
676 shouldWaitToRespond));
677 }
678 else if (update->action == FibUpdate::REMOVE_NEXTHOP)
679 {
680 FaceEntry faceEntry;
681 faceEntry.faceId = update->faceId;
682
683 m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>(
684 ControlParameters()
685 .setName(update->name)
686 .setFaceId(faceEntry.faceId),
687 bind(&RibManager::onRemoveNextHopSuccess, this, request,
688 parameters,
689 currentTransactionId,
690 shouldWaitToRespond),
691 bind(&RibManager::onRemoveNextHopError, this, _1, _2, request, currentTransactionId,
692 shouldWaitToRespond));
693 }
694 }
695
696 m_managedRib.clearFibUpdates();
697}
698
699void
700RibManager::sendUpdatesToFibAfterFaceDestroyEvent()
701{
702 ControlParameters parameters;
703 parameters.setOrigin(ndn::nfd::ROUTE_ORIGIN_STATIC);
704
705 sendUpdatesToFib(shared_ptr<const Interest>(), parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700706}
707
Vince Lehmancd16c832014-07-23 15:14:55 -0700708void
709RibManager::listEntries(const Interest& request)
710{
711 const Name& command = request.getName();
712 const size_t commandNComps = command.size();
713
714 if (commandNComps < LIST_COMMAND_NCOMPS ||
715 !LIST_COMMAND_PREFIX.isPrefixOf(command))
716 {
717 NFD_LOG_DEBUG("command result: malformed");
718 sendResponse(command, 400, "Malformed command");
719 return;
720 }
721
722 m_ribStatusPublisher.publish();
723}
724
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700725} // namespace rib
726} // namespace nfd