blob: 59e623053242d2f784389676f80d21ca9c8a607f [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"
Vince Lehmancd613c52014-07-30 14:34:49 -050030#include <ndn-cxx/management/nfd-face-status.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070031
32namespace nfd {
33namespace rib {
34
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070035NFD_LOG_INIT("RibManager");
36
Alexander Afanasyev20d31442014-04-19 17:00:53 -070037const Name RibManager::COMMAND_PREFIX = "/localhost/nfd/rib";
38const Name RibManager::REMOTE_COMMAND_PREFIX = "/localhop/nfd/rib";
Vince Lehmancd613c52014-07-30 14:34:49 -050039const Name RibManager::FACES_LIST_DATASET_PREFIX = "/localhost/nfd/faces/list";
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070040
41const size_t RibManager::COMMAND_UNSIGNED_NCOMPS =
42 RibManager::COMMAND_PREFIX.size() +
43 1 + // verb
44 1; // verb options
45
46const size_t RibManager::COMMAND_SIGNED_NCOMPS =
47 RibManager::COMMAND_UNSIGNED_NCOMPS +
48 4; // (timestamp, nonce, signed info tlv, signature tlv)
49
Vince Lehmancd16c832014-07-23 15:14:55 -070050const RibManager::SignedVerbAndProcessor RibManager::SIGNED_COMMAND_VERBS[] =
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070051 {
Vince Lehmancd16c832014-07-23 15:14:55 -070052 SignedVerbAndProcessor(
53 Name::Component("register"),
54 &RibManager::registerEntry
55 ),
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070056
Vince Lehmancd16c832014-07-23 15:14:55 -070057 SignedVerbAndProcessor(
58 Name::Component("unregister"),
59 &RibManager::unregisterEntry
60 ),
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070061 };
62
Vince Lehmancd16c832014-07-23 15:14:55 -070063const RibManager::UnsignedVerbAndProcessor RibManager::UNSIGNED_COMMAND_VERBS[] =
64 {
65 UnsignedVerbAndProcessor(
66 Name::Component("list"),
67 &RibManager::listEntries
68 ),
69 };
70
71const Name RibManager::LIST_COMMAND_PREFIX("/localhost/nfd/rib/list");
72const size_t RibManager::LIST_COMMAND_NCOMPS = LIST_COMMAND_PREFIX.size();
73
Vince Lehman72446ec2014-07-09 10:50:02 -050074RibManager::RibManager(ndn::Face& face)
75 : m_face(face)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070076 , m_nfdController(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070077 , m_localhostValidator(m_face)
78 , m_localhopValidator(m_face)
Yingdi Yuf4db0b52014-04-17 13:17:39 -070079 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070080 , m_isLocalhopEnabled(false)
Vince Lehmancd16c832014-07-23 15:14:55 -070081 , m_ribStatusPublisher(m_managedRib, face, LIST_COMMAND_PREFIX, m_keyChain)
Vince Lehman4387e782014-06-19 16:57:45 -050082 , m_lastTransactionId(0)
Vince Lehmancd16c832014-07-23 15:14:55 -070083 , m_signedVerbDispatch(SIGNED_COMMAND_VERBS,
84 SIGNED_COMMAND_VERBS +
85 (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor)))
86 , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS,
87 UNSIGNED_COMMAND_VERBS +
88 (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor)))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070089{
90}
91
92void
Junxiao Shia3295742014-05-16 22:40:10 -070093RibManager::startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest)
94{
95 NFD_LOG_INFO("Listening on: " << commandPrefix);
96
97 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
98 ControlParameters()
99 .setName(commandPrefix)
100 .setFaceId(0),
101 bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(commandPrefix)),
102 bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(commandPrefix), _2));
103
104 m_face.setInterestFilter(commandPrefix, onRequest);
105}
106
107void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700108RibManager::registerWithNfd()
109{
110 //check whether the components of localhop and localhost prefixes are same
111 BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size());
112
Junxiao Shia3295742014-05-16 22:40:10 -0700113 this->startListening(COMMAND_PREFIX, bind(&RibManager::onLocalhostRequest, this, _2));
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700114
Junxiao Shia3295742014-05-16 22:40:10 -0700115 if (m_isLocalhopEnabled) {
116 this->startListening(REMOTE_COMMAND_PREFIX,
117 bind(&RibManager::onLocalhopRequest, this, _2));
118 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700119
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700120 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shi15b12e72014-08-09 19:56:24 -0700121 m_faceMonitor.onNotification += bind(&RibManager::onNotification, this, _1);
122 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500123
124 fetchActiveFaces();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700125}
126
127void
128RibManager::setConfigFile(ConfigFile& configFile)
129{
Yingdi Yue5224e92014-04-29 18:04:02 -0700130 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700131 bind(&RibManager::onConfig, this, _1, _2, _3));
132}
133
134void
135RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700136 bool isDryRun,
137 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700138{
Yingdi Yue5224e92014-04-29 18:04:02 -0700139 for (ConfigSection::const_iterator i = configSection.begin();
140 i != configSection.end(); ++i)
141 {
142 if (i->first == "localhost_security")
143 m_localhostValidator.load(i->second, filename);
144 else if (i->first == "localhop_security")
145 {
146 m_localhopValidator.load(i->second, filename);
147 m_isLocalhopEnabled = true;
148 }
149 else
150 throw Error("Unrecognized rib property: " + i->first);
151 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700152}
153
154void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700155RibManager::sendResponse(const Name& name,
156 const ControlResponse& response)
157{
158 const Block& encodedControl = response.wireEncode();
159
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700160 shared_ptr<Data> responseData = make_shared<Data>(name);
161 responseData->setContent(encodedControl);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700162
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700163 m_keyChain.sign(*responseData);
164 m_face.put(*responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700165}
166
167void
168RibManager::sendResponse(const Name& name,
169 uint32_t code,
170 const std::string& text)
171{
172 ControlResponse response(code, text);
173 sendResponse(name, response);
174}
175
176void
Yingdi Yue5224e92014-04-29 18:04:02 -0700177RibManager::onLocalhostRequest(const Interest& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700178{
Vince Lehmancd16c832014-07-23 15:14:55 -0700179 const Name& command = request.getName();
180 const Name::Component& verb = command.get(COMMAND_PREFIX.size());
181
182 UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
183
184 if (unsignedVerbProcessor != m_unsignedVerbDispatch.end())
185 {
186 NFD_LOG_DEBUG("command result: processing unsigned verb: " << verb);
187 (unsignedVerbProcessor->second)(this, request);
188 }
189 else
190 {
191 m_localhostValidator.validate(request,
192 bind(&RibManager::onCommandValidated, this, _1),
193 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
194 }
Yingdi Yue5224e92014-04-29 18:04:02 -0700195}
196
197void
198RibManager::onLocalhopRequest(const Interest& request)
199{
200 m_localhopValidator.validate(request,
201 bind(&RibManager::onCommandValidated, this, _1),
202 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700203}
204
205void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700206RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700207{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700208 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700209 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700210
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700211 const Name& command = request->getName();
212 const Name::Component& verb = command[COMMAND_PREFIX.size()];
213 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
214
Vince Lehmancd16c832014-07-23 15:14:55 -0700215 SignedVerbDispatchTable::const_iterator verbProcessor = m_signedVerbDispatch.find(verb);
216 if (verbProcessor != m_signedVerbDispatch.end())
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700217 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700218 ControlParameters parameters;
219 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700220 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700221 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500222 if (static_cast<bool>(request))
223 sendResponse(command, 400, "Malformed command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700224 return;
225 }
226
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700227 NFD_LOG_DEBUG("command result: processing verb: " << verb);
228 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700229 }
230 else
231 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700232 NFD_LOG_DEBUG("Unsupported command: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500233 if (static_cast<bool>(request))
234 sendResponse(request->getName(), 501, "Unsupported command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700235 }
236}
237
238void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700239RibManager::registerEntry(const shared_ptr<const Interest>& request,
240 ControlParameters& parameters)
241{
242 ndn::nfd::RibRegisterCommand command;
243
244 if (!validateParameters(command, parameters))
245 {
246 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
Vince Lehmancd613c52014-07-30 14:34:49 -0500247
Syed Obaid3313a372014-07-01 01:31:33 -0500248 if (static_cast<bool>(request))
Vince Lehmancd613c52014-07-30 14:34:49 -0500249 {
250 sendResponse(request->getName(), 400, "Malformed command");
251 }
252
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700253 return;
254 }
255
Alexander Afanasyev483efd12014-08-14 10:51:18 -0700256 bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0);
257 if (isSelfRegistration)
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700258 {
259 parameters.setFaceId(request->getIncomingFaceId());
260 }
261
Vince Lehmancd613c52014-07-30 14:34:49 -0500262 // Is the face valid?
Alexander Afanasyev483efd12014-08-14 10:51:18 -0700263 // Issue 1852: There is no need to check (and it can easily fail) for self-registrations
264 if (!isSelfRegistration && activeFaces.find(parameters.getFaceId()) == activeFaces.end())
Vince Lehmancd613c52014-07-30 14:34:49 -0500265 {
266 NFD_LOG_DEBUG("register result: FAIL reason: unknown faceId");
267
268 if (static_cast<bool>(request))
269 {
270 sendResponse(request->getName(), 410, "Face not found");
271 }
272
273 return;
274 }
275
Vince12e49462014-06-09 13:29:32 -0500276 FaceEntry faceEntry;
277 faceEntry.faceId = parameters.getFaceId();
278 faceEntry.origin = parameters.getOrigin();
279 faceEntry.cost = parameters.getCost();
280 faceEntry.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500281
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700282 if (parameters.hasExpirationPeriod() &&
283 parameters.getExpirationPeriod() != time::milliseconds::max())
Syed Obaid3313a372014-07-01 01:31:33 -0500284 {
285 faceEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
286
287 // Schedule a new event, the old one will be cancelled during rib insertion.
288 EventId eventId;
Syed Obaid3313a372014-07-01 01:31:33 -0500289 eventId = scheduler::schedule(parameters.getExpirationPeriod(),
Vince Lehman281ded72014-08-21 12:17:08 -0500290 bind(&RibManager::expireEntry,
Syed Obaid3313a372014-07-01 01:31:33 -0500291 this, shared_ptr<Interest>(), parameters));
Vince Lehman281ded72014-08-21 12:17:08 -0500292 NFD_LOG_TRACE("Scheduled unregistration at: " << faceEntry.expires <<
293 " with EventId: " << eventId);
Syed Obaid3313a372014-07-01 01:31:33 -0500294
295 //set the NewEventId of this entry
296 faceEntry.setExpirationEvent(eventId);
297 }
298 else
299 {
300 faceEntry.expires = time::steady_clock::TimePoint::max();
301 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700302
Vince12e49462014-06-09 13:29:32 -0500303 NFD_LOG_TRACE("register prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700304
Vince12e49462014-06-09 13:29:32 -0500305 m_managedRib.insert(parameters.getName(), faceEntry);
Vince Lehman4387e782014-06-19 16:57:45 -0500306
307 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700308}
309
310void
Vince Lehman281ded72014-08-21 12:17:08 -0500311RibManager::expireEntry(const shared_ptr<const Interest>& request, ControlParameters& params)
312{
313 FaceEntry face;
314 face.faceId = params.getFaceId();
315 face.origin = params.getOrigin();
316 face.cost = params.getCost();
317 face.flags = params.getFlags();
318
319 NFD_LOG_DEBUG(face << " for " << params.getName() << " has expired");
320 unregisterEntry(request, params);
321}
322
323void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700324RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
Syed Obaid3313a372014-07-01 01:31:33 -0500325 ControlParameters& params)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700326{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700327 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700328
Syed Obaid3313a372014-07-01 01:31:33 -0500329 //passing all parameters gives error in validation.
330 //so passing only the required arguments.
331 ControlParameters parameters;
332 parameters.setName(params.getName());
Alexander Afanasyevb609f002014-07-18 16:56:34 -0700333 if (params.hasFaceId())
334 parameters.setFaceId(params.getFaceId());
335 if (params.hasOrigin())
336 parameters.setOrigin(params.getOrigin());
Syed Obaid3313a372014-07-01 01:31:33 -0500337
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700338 if (!validateParameters(command, parameters))
339 {
Syed Obaid3313a372014-07-01 01:31:33 -0500340 NFD_LOG_DEBUG("unregister result: FAIL reason: malformed");
341 if (static_cast<bool>(request))
342 sendResponse(request->getName(), 400, "Malformed command");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700343 return;
344 }
345
Alexander Afanasyev483efd12014-08-14 10:51:18 -0700346 bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0);
347 if (isSelfRegistration)
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700348 {
349 parameters.setFaceId(request->getIncomingFaceId());
350 }
351
Vince Lehmancd613c52014-07-30 14:34:49 -0500352 // Is the face valid?
Alexander Afanasyev483efd12014-08-14 10:51:18 -0700353 // Issue 1852: There is no need to check (and it can easily fail) for self-registrations
354 if (!isSelfRegistration && activeFaces.find(parameters.getFaceId()) == activeFaces.end())
Vince Lehmancd613c52014-07-30 14:34:49 -0500355 {
356 NFD_LOG_DEBUG("register result: FAIL reason: unknown faceId");
357
358 if (static_cast<bool>(request))
359 {
360 sendResponse(request->getName(), 410, "Face not found");
361 }
362
363 return;
364 }
365
Vince12e49462014-06-09 13:29:32 -0500366 FaceEntry faceEntry;
367 faceEntry.faceId = parameters.getFaceId();
368 faceEntry.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700369
Vince12e49462014-06-09 13:29:32 -0500370 NFD_LOG_TRACE("unregister prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700371
Vince Lehman4387e782014-06-19 16:57:45 -0500372 m_managedRib.erase(parameters.getName(), faceEntry);
373
374 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700375}
376
377void
378RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
379 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700380{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700381 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Syed Obaid3313a372014-07-01 01:31:33 -0500382 if (static_cast<bool>(request))
383 sendResponse(request->getName(), 403, failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700384}
385
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700386
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700387bool
388RibManager::extractParameters(const Name::Component& parameterComponent,
389 ControlParameters& extractedParameters)
390{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700391 try
392 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700393 Block rawParameters = parameterComponent.blockFromValue();
394 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700395 }
396 catch (const ndn::Tlv::Error& e)
397 {
398 return false;
399 }
400
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700401 NFD_LOG_DEBUG("Parameters parsed OK");
402 return true;
403}
404
405bool
406RibManager::validateParameters(const ControlCommand& command,
407 ControlParameters& parameters)
408{
409 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700410 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700411 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700412 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700413 catch (const ControlCommand::ArgumentError&)
414 {
415 return false;
416 }
417
418 command.applyDefaultsToRequest(parameters);
419
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700420 return true;
421}
422
423void
424RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700425 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500426 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700427{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700428 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700429
430 ControlResponse response;
431
432 if (code == 404)
433 {
434 response.setCode(code);
435 response.setText(error);
436 }
437 else
438 {
439 response.setCode(533);
440 std::ostringstream os;
441 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
442 response.setText(os.str());
443 }
444
Syed Obaid3313a372014-07-01 01:31:33 -0500445 if (static_cast<bool>(request))
446 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700447}
448
449void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700450RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
451 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500452 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700453{
454 ControlResponse response;
455
456 response.setCode(200);
457 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700458 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700459
Vince12e49462014-06-09 13:29:32 -0500460 NFD_LOG_TRACE("onRegSuccess: registered " << faceEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700461
Syed Obaid3313a372014-07-01 01:31:33 -0500462 if (static_cast<bool>(request))
463 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700464}
465
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700466
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700467void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700468RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
469 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500470 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700471{
472 ControlResponse response;
473
474 response.setCode(200);
475 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700476 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700477
Vince12e49462014-06-09 13:29:32 -0500478 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700479
Syed Obaid3313a372014-07-01 01:31:33 -0500480 if (static_cast<bool>(request))
481 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500482}
483
484void
485RibManager::sendSuccessResponse(const shared_ptr<const Interest>& request,
486 const ControlParameters& parameters)
487{
488 if (!static_cast<bool>(request))
489 {
490 return;
491 }
492
493 ControlResponse response;
494
495 response.setCode(200);
496 response.setText("Success");
497 response.setBody(parameters.wireEncode());
498
Syed Obaid3313a372014-07-01 01:31:33 -0500499 if (static_cast<bool>(request))
500 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500501}
502
503void
504RibManager::sendErrorResponse(uint32_t code, const std::string& error,
505 const shared_ptr<const Interest>& request)
506{
507 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
508
509 if (!static_cast<bool>(request))
510 {
511 return;
512 }
513
514 ControlResponse response;
515
516 if (code == 404)
517 {
518 response.setCode(code);
519 response.setText(error);
520 }
521 else
522 {
523 response.setCode(533);
524 std::ostringstream os;
525 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
526 response.setText(os.str());
527 }
528
Syed Obaid3313a372014-07-01 01:31:33 -0500529 if (static_cast<bool>(request))
530 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700531}
532
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700533void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700534RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix)
535{
536 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
537}
538
539void
540RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg)
541{
542 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
543}
544
Vince Lehman4387e782014-06-19 16:57:45 -0500545bool
546RibManager::isTransactionComplete(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700547{
Vince Lehman4387e782014-06-19 16:57:45 -0500548 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
549
550 if (it != m_pendingFibTransactions.end())
551 {
552 int& updatesLeft = it->second;
553
554 updatesLeft--;
555
556 // All of the updates have been applied successfully
557 if (updatesLeft == 0)
558 {
559 m_pendingFibTransactions.erase(it);
560 return true;
561 }
562 }
563
564 return false;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700565}
566
567void
Vince Lehman4387e782014-06-19 16:57:45 -0500568RibManager::invalidateTransaction(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700569{
Vince Lehman4387e782014-06-19 16:57:45 -0500570 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
571
572 if (it != m_pendingFibTransactions.end())
573 {
574 m_pendingFibTransactions.erase(it);
575 }
576}
577
578void
579RibManager::onAddNextHopSuccess(const shared_ptr<const Interest>& request,
580 const ControlParameters& parameters,
581 const TransactionId transactionId,
582 const bool shouldSendResponse)
583{
584 if (isTransactionComplete(transactionId) && shouldSendResponse)
585 {
586 sendSuccessResponse(request, parameters);
587 }
588}
589
590void
591RibManager::onAddNextHopError(uint32_t code, const std::string& error,
592 const shared_ptr<const Interest>& request,
593 const TransactionId transactionId, const bool shouldSendResponse)
594{
595 invalidateTransaction(transactionId);
596
597 if (shouldSendResponse)
598 {
599 sendErrorResponse(code, error, request);
600 }
601}
602
603void
604RibManager::onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
605 const ControlParameters& parameters,
606 const TransactionId transactionId,
607 const bool shouldSendResponse)
608{
609 if (isTransactionComplete(transactionId) && shouldSendResponse)
610 {
611 sendSuccessResponse(request, parameters);
612 }
613}
614
615void
616RibManager::onRemoveNextHopError(uint32_t code, const std::string& error,
617 const shared_ptr<const Interest>& request,
618 const TransactionId transactionId, const bool shouldSendResponse)
619{
620 invalidateTransaction(transactionId);
621
622 if (shouldSendResponse)
623 {
624 sendErrorResponse(code, error, request);
625 }
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700626}
627
628void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700629RibManager::onControlHeaderSuccess()
630{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700631 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700632}
633
634void
635RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
636{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700637 std::ostringstream os;
638 os << "Couldn't enable local control header "
639 << "(code: " << code << ", info: " << reason << ")";
640 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700641}
642
643void
644RibManager::enableLocalControlHeader()
645{
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700646 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700647 ControlParameters()
648 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
649 bind(&RibManager::onControlHeaderSuccess, this),
650 bind(&RibManager::onControlHeaderError, this, _1, _2));
651}
652
653void
654RibManager::onNotification(const FaceEventNotification& notification)
655{
656 /// \todo A notification can be missed, in this case check Facelist
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700657 NFD_LOG_TRACE("onNotification: " << notification);
Vince Lehmancd613c52014-07-30 14:34:49 -0500658
659 if (notification.getKind() == ndn::nfd::FACE_EVENT_CREATED)
Vince Lehman4387e782014-06-19 16:57:45 -0500660 {
Vince Lehmancd613c52014-07-30 14:34:49 -0500661 NFD_LOG_DEBUG("Received notification for created faceId: " << notification.getFaceId());
662 activeFaces.insert(notification.getFaceId());
663 }
664 else if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED)
665 {
666 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
667 activeFaces.erase(notification.getFaceId());
668
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700669 scheduler::schedule(time::seconds(0),
670 bind(&RibManager::processErasureAfterNotification, this,
671 notification.getFaceId()));
Vince Lehman4387e782014-06-19 16:57:45 -0500672 }
673}
674
675void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700676RibManager::processErasureAfterNotification(uint64_t faceId)
677{
678 m_managedRib.erase(faceId);
679
680 sendUpdatesToFibAfterFaceDestroyEvent();
681}
682
683void
Vince Lehman4387e782014-06-19 16:57:45 -0500684RibManager::sendUpdatesToFib(const shared_ptr<const Interest>& request,
685 const ControlParameters& parameters)
686{
687 const Rib::FibUpdateList& updates = m_managedRib.getFibUpdates();
688
689 // If no updates were generated, consider the operation a success
690 if (updates.empty())
691 {
692 sendSuccessResponse(request, parameters);
693 return;
694 }
695
696 bool shouldWaitToRespond = false;
697
698 // An application request should wait for all FIB updates to be applied
699 // successfully before sending a response
700 if (parameters.getOrigin() == ndn::nfd::ROUTE_ORIGIN_APP)
701 {
702 shouldWaitToRespond = true;
703 }
704 else // Respond immediately
705 {
706 sendSuccessResponse(request, parameters);
707 }
708
Vince Lehmanbc05b762014-08-15 15:37:02 -0500709 std::string updateString = (updates.size() == 1) ? " update" : " updates";
710 NFD_LOG_DEBUG("Applying " << updates.size() << updateString << " to FIB");
Vince Lehman4387e782014-06-19 16:57:45 -0500711
712 // Assign an ID to this FIB transaction
713 TransactionId currentTransactionId = ++m_lastTransactionId;
714
715 // Add this transaction to the transaction table
716 m_pendingFibTransactions[currentTransactionId] = updates.size();
717
718 for (Rib::FibUpdateList::const_iterator it = updates.begin(); it != updates.end(); ++it)
719 {
720 shared_ptr<const FibUpdate> update(*it);
Vince Lehmanbc05b762014-08-15 15:37:02 -0500721 NFD_LOG_DEBUG("Sending FIB update: " << *update);
Vince Lehman4387e782014-06-19 16:57:45 -0500722
723 if (update->action == FibUpdate::ADD_NEXTHOP)
724 {
725 FaceEntry faceEntry;
726 faceEntry.faceId = update->faceId;
727 faceEntry.cost = update->cost;
728
729 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
730 ControlParameters()
731 .setName(update->name)
732 .setFaceId(faceEntry.faceId)
733 .setCost(faceEntry.cost),
734 bind(&RibManager::onAddNextHopSuccess, this, request,
735 parameters,
736 currentTransactionId,
737 shouldWaitToRespond),
738 bind(&RibManager::onAddNextHopError, this, _1, _2, request, currentTransactionId,
739 shouldWaitToRespond));
740 }
741 else if (update->action == FibUpdate::REMOVE_NEXTHOP)
742 {
743 FaceEntry faceEntry;
744 faceEntry.faceId = update->faceId;
745
746 m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>(
747 ControlParameters()
748 .setName(update->name)
749 .setFaceId(faceEntry.faceId),
750 bind(&RibManager::onRemoveNextHopSuccess, this, request,
751 parameters,
752 currentTransactionId,
753 shouldWaitToRespond),
754 bind(&RibManager::onRemoveNextHopError, this, _1, _2, request, currentTransactionId,
755 shouldWaitToRespond));
756 }
757 }
758
759 m_managedRib.clearFibUpdates();
760}
761
762void
763RibManager::sendUpdatesToFibAfterFaceDestroyEvent()
764{
765 ControlParameters parameters;
766 parameters.setOrigin(ndn::nfd::ROUTE_ORIGIN_STATIC);
767
768 sendUpdatesToFib(shared_ptr<const Interest>(), parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700769}
770
Vince Lehmancd16c832014-07-23 15:14:55 -0700771void
772RibManager::listEntries(const Interest& request)
773{
774 const Name& command = request.getName();
775 const size_t commandNComps = command.size();
776
777 if (commandNComps < LIST_COMMAND_NCOMPS ||
778 !LIST_COMMAND_PREFIX.isPrefixOf(command))
779 {
780 NFD_LOG_DEBUG("command result: malformed");
781 sendResponse(command, 400, "Malformed command");
782 return;
783 }
784
785 m_ribStatusPublisher.publish();
786}
787
Vince Lehmancd613c52014-07-30 14:34:49 -0500788void
789RibManager::fetchActiveFaces()
790{
791 NFD_LOG_DEBUG("Fetching active faces");
792
793 Interest interest(FACES_LIST_DATASET_PREFIX);
794 interest.setChildSelector(1);
795 interest.setMustBeFresh(true);
796
797 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
798
799 m_face.expressInterest(interest,
800 bind(&RibManager::fetchSegments, this, _2, buffer),
801 bind(&RibManager::onFetchFaceStatusTimeout, this));
802}
803
804void
805RibManager::fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer)
806{
807 buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
808 data.getContent().value_size());
809
810 uint64_t currentSegment = data.getName().get(-1).toSegment();
811
812 const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
813 if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment)
814 {
815 m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
816 bind(&RibManager::fetchSegments, this, _2, buffer),
817 bind(&RibManager::onFetchFaceStatusTimeout, this));
818 }
819 else
820 {
821 updateActiveFaces(buffer);
822 }
823}
824
825void
826RibManager::updateActiveFaces(shared_ptr<ndn::OBufferStream> buffer)
827{
828 NFD_LOG_DEBUG("Updating active faces");
829
830 ndn::ConstBufferPtr buf = buffer->buf();
831
832 Block block;
833 size_t offset = 0;
834
835 while (offset < buf->size())
836 {
837 if (!Block::fromBuffer(buf, offset, block))
838 {
839 std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
840 break;
841 }
842
843 offset += block.size();
844
845 ndn::nfd::FaceStatus status(block);
846
847 NFD_LOG_DEBUG("Adding faceId: " << status.getFaceId() << " to activeFaces");
848 activeFaces.insert(status.getFaceId());
849 }
850}
851
852void
853RibManager::onFetchFaceStatusTimeout()
854{
855 std::cerr << "Face Status Dataset request timed out" << std::endl;
856}
857
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700858} // namespace rib
859} // namespace nfd