blob: 491739a8ce50fc959610c07cfc70e46d4e39beaf [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 Lehman26b215c2014-08-17 15:00:41 -050074const time::seconds RibManager::ACTIVE_FACE_FETCH_INTERVAL = time::seconds(300);
75
Vince Lehman72446ec2014-07-09 10:50:02 -050076RibManager::RibManager(ndn::Face& face)
77 : m_face(face)
Junxiao Shi8e273ca2014-11-12 00:42:29 -070078 , m_nfdController(m_face, m_keyChain)
Yingdi Yue5224e92014-04-29 18:04:02 -070079 , m_localhostValidator(m_face)
80 , m_localhopValidator(m_face)
Yingdi Yuf4db0b52014-04-17 13:17:39 -070081 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070082 , m_isLocalhopEnabled(false)
Yanbiao Lic17de832014-11-21 17:51:45 -080083 , m_remoteRegistrator(m_nfdController, m_keyChain, m_managedRib)
Vince Lehmancd16c832014-07-23 15:14:55 -070084 , m_ribStatusPublisher(m_managedRib, face, LIST_COMMAND_PREFIX, m_keyChain)
Vince Lehman4387e782014-06-19 16:57:45 -050085 , m_lastTransactionId(0)
Vince Lehmancd16c832014-07-23 15:14:55 -070086 , m_signedVerbDispatch(SIGNED_COMMAND_VERBS,
87 SIGNED_COMMAND_VERBS +
88 (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor)))
89 , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS,
90 UNSIGNED_COMMAND_VERBS +
91 (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor)))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070092{
93}
94
Vince Lehman26b215c2014-08-17 15:00:41 -050095RibManager::~RibManager()
96{
97 scheduler::cancel(m_activeFaceFetchEvent);
98}
99
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700100void
Junxiao Shia3295742014-05-16 22:40:10 -0700101RibManager::startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest)
102{
103 NFD_LOG_INFO("Listening on: " << commandPrefix);
104
105 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
106 ControlParameters()
107 .setName(commandPrefix)
108 .setFaceId(0),
109 bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(commandPrefix)),
110 bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(commandPrefix), _2));
111
112 m_face.setInterestFilter(commandPrefix, onRequest);
113}
114
115void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700116RibManager::registerWithNfd()
117{
118 //check whether the components of localhop and localhost prefixes are same
119 BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size());
120
Junxiao Shia3295742014-05-16 22:40:10 -0700121 this->startListening(COMMAND_PREFIX, bind(&RibManager::onLocalhostRequest, this, _2));
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700122
Junxiao Shia3295742014-05-16 22:40:10 -0700123 if (m_isLocalhopEnabled) {
124 this->startListening(REMOTE_COMMAND_PREFIX,
125 bind(&RibManager::onLocalhopRequest, this, _2));
126 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700127
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700128 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shi15b12e72014-08-09 19:56:24 -0700129 m_faceMonitor.onNotification += bind(&RibManager::onNotification, this, _1);
130 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500131
Vince Lehman26b215c2014-08-17 15:00:41 -0500132 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700133}
134
135void
136RibManager::setConfigFile(ConfigFile& configFile)
137{
Yingdi Yue5224e92014-04-29 18:04:02 -0700138 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700139 bind(&RibManager::onConfig, this, _1, _2, _3));
140}
141
142void
143RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700144 bool isDryRun,
145 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700146{
Yingdi Yue5224e92014-04-29 18:04:02 -0700147 for (ConfigSection::const_iterator i = configSection.begin();
148 i != configSection.end(); ++i)
149 {
150 if (i->first == "localhost_security")
151 m_localhostValidator.load(i->second, filename);
152 else if (i->first == "localhop_security")
153 {
154 m_localhopValidator.load(i->second, filename);
155 m_isLocalhopEnabled = true;
156 }
Yanbiao Lic17de832014-11-21 17:51:45 -0800157 else if (i->first == "remote_register")
158 {
159 m_remoteRegistrator.loadConfig(i->second);
160
161 // register callback to the RIB.
162 // do remote registration after an entry is inserted into the RIB.
163 // do remote unregistration after an entry is erased from the RIB.
164 m_managedRib.afterInsertEntry += [this] (const Name& prefix) {
165 m_remoteRegistrator.registerPrefix(prefix);
166 };
167
168 m_managedRib.afterEraseEntry += [this] (const Name& prefix) {
169 m_remoteRegistrator.unregisterPrefix(prefix);
170 };
171 }
Yingdi Yue5224e92014-04-29 18:04:02 -0700172 else
173 throw Error("Unrecognized rib property: " + i->first);
174 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700175}
176
177void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700178RibManager::sendResponse(const Name& name,
179 const ControlResponse& response)
180{
181 const Block& encodedControl = response.wireEncode();
182
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700183 shared_ptr<Data> responseData = make_shared<Data>(name);
184 responseData->setContent(encodedControl);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700185
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700186 m_keyChain.sign(*responseData);
187 m_face.put(*responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700188}
189
190void
191RibManager::sendResponse(const Name& name,
192 uint32_t code,
193 const std::string& text)
194{
195 ControlResponse response(code, text);
196 sendResponse(name, response);
197}
198
199void
Yingdi Yue5224e92014-04-29 18:04:02 -0700200RibManager::onLocalhostRequest(const Interest& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700201{
Vince Lehmancd16c832014-07-23 15:14:55 -0700202 const Name& command = request.getName();
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700203
204 if (command.size() <= COMMAND_PREFIX.size())
205 {
206 // command is too short to have a verb
207 NFD_LOG_DEBUG("command result: malformed");
208 sendResponse(command, 400, "Malformed command");
209 return;
210 }
211
212 const Name::Component& verb = command.at(COMMAND_PREFIX.size());
Vince Lehmancd16c832014-07-23 15:14:55 -0700213
214 UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
215
216 if (unsignedVerbProcessor != m_unsignedVerbDispatch.end())
217 {
218 NFD_LOG_DEBUG("command result: processing unsigned verb: " << verb);
219 (unsignedVerbProcessor->second)(this, request);
220 }
221 else
222 {
223 m_localhostValidator.validate(request,
224 bind(&RibManager::onCommandValidated, this, _1),
225 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
226 }
Yingdi Yue5224e92014-04-29 18:04:02 -0700227}
228
229void
230RibManager::onLocalhopRequest(const Interest& request)
231{
232 m_localhopValidator.validate(request,
233 bind(&RibManager::onCommandValidated, this, _1),
234 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700235}
236
237void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700238RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700239{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700240 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700241 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700242
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700243 const Name& command = request->getName();
244 const Name::Component& verb = command[COMMAND_PREFIX.size()];
245 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
246
Vince Lehmancd16c832014-07-23 15:14:55 -0700247 SignedVerbDispatchTable::const_iterator verbProcessor = m_signedVerbDispatch.find(verb);
248 if (verbProcessor != m_signedVerbDispatch.end())
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700249 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700250 ControlParameters parameters;
251 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700252 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700253 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500254 if (static_cast<bool>(request))
255 sendResponse(command, 400, "Malformed command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700256 return;
257 }
258
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700259 NFD_LOG_DEBUG("command result: processing verb: " << verb);
260 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700261 }
262 else
263 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700264 NFD_LOG_DEBUG("Unsupported command: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500265 if (static_cast<bool>(request))
266 sendResponse(request->getName(), 501, "Unsupported command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700267 }
268}
269
270void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700271RibManager::registerEntry(const shared_ptr<const Interest>& request,
272 ControlParameters& parameters)
273{
274 ndn::nfd::RibRegisterCommand command;
275
276 if (!validateParameters(command, parameters))
277 {
278 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
Vince Lehmancd613c52014-07-30 14:34:49 -0500279
Syed Obaid3313a372014-07-01 01:31:33 -0500280 if (static_cast<bool>(request))
Vince Lehmancd613c52014-07-30 14:34:49 -0500281 {
282 sendResponse(request->getName(), 400, "Malformed command");
283 }
284
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700285 return;
286 }
287
Alexander Afanasyev483efd12014-08-14 10:51:18 -0700288 bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0);
289 if (isSelfRegistration)
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700290 {
291 parameters.setFaceId(request->getIncomingFaceId());
292 }
293
Vince12e49462014-06-09 13:29:32 -0500294 FaceEntry faceEntry;
295 faceEntry.faceId = parameters.getFaceId();
296 faceEntry.origin = parameters.getOrigin();
297 faceEntry.cost = parameters.getCost();
298 faceEntry.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500299
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700300 if (parameters.hasExpirationPeriod() &&
301 parameters.getExpirationPeriod() != time::milliseconds::max())
Syed Obaid3313a372014-07-01 01:31:33 -0500302 {
303 faceEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
304
305 // Schedule a new event, the old one will be cancelled during rib insertion.
306 EventId eventId;
Syed Obaid3313a372014-07-01 01:31:33 -0500307 eventId = scheduler::schedule(parameters.getExpirationPeriod(),
Vince Lehman281ded72014-08-21 12:17:08 -0500308 bind(&RibManager::expireEntry,
Syed Obaid3313a372014-07-01 01:31:33 -0500309 this, shared_ptr<Interest>(), parameters));
Vince Lehman281ded72014-08-21 12:17:08 -0500310 NFD_LOG_TRACE("Scheduled unregistration at: " << faceEntry.expires <<
311 " with EventId: " << eventId);
Syed Obaid3313a372014-07-01 01:31:33 -0500312
313 //set the NewEventId of this entry
314 faceEntry.setExpirationEvent(eventId);
315 }
316 else
317 {
318 faceEntry.expires = time::steady_clock::TimePoint::max();
319 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700320
Vince12e49462014-06-09 13:29:32 -0500321 NFD_LOG_TRACE("register prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700322
Vince12e49462014-06-09 13:29:32 -0500323 m_managedRib.insert(parameters.getName(), faceEntry);
Vince Lehman26b215c2014-08-17 15:00:41 -0500324 m_registeredFaces.insert(faceEntry.faceId);
Vince Lehman4387e782014-06-19 16:57:45 -0500325
326 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700327}
328
329void
Vince Lehman281ded72014-08-21 12:17:08 -0500330RibManager::expireEntry(const shared_ptr<const Interest>& request, ControlParameters& params)
331{
332 FaceEntry face;
333 face.faceId = params.getFaceId();
334 face.origin = params.getOrigin();
335 face.cost = params.getCost();
336 face.flags = params.getFlags();
337
338 NFD_LOG_DEBUG(face << " for " << params.getName() << " has expired");
339 unregisterEntry(request, params);
340}
341
342void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700343RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
Syed Obaid3313a372014-07-01 01:31:33 -0500344 ControlParameters& params)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700345{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700346 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700347
Syed Obaid3313a372014-07-01 01:31:33 -0500348 //passing all parameters gives error in validation.
349 //so passing only the required arguments.
350 ControlParameters parameters;
351 parameters.setName(params.getName());
Alexander Afanasyevb609f002014-07-18 16:56:34 -0700352 if (params.hasFaceId())
353 parameters.setFaceId(params.getFaceId());
354 if (params.hasOrigin())
355 parameters.setOrigin(params.getOrigin());
Syed Obaid3313a372014-07-01 01:31:33 -0500356
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700357 if (!validateParameters(command, parameters))
358 {
Syed Obaid3313a372014-07-01 01:31:33 -0500359 NFD_LOG_DEBUG("unregister result: FAIL reason: malformed");
360 if (static_cast<bool>(request))
361 sendResponse(request->getName(), 400, "Malformed command");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700362 return;
363 }
364
Alexander Afanasyev483efd12014-08-14 10:51:18 -0700365 bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0);
366 if (isSelfRegistration)
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700367 {
368 parameters.setFaceId(request->getIncomingFaceId());
369 }
370
Vince12e49462014-06-09 13:29:32 -0500371 FaceEntry faceEntry;
372 faceEntry.faceId = parameters.getFaceId();
373 faceEntry.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700374
Vince12e49462014-06-09 13:29:32 -0500375 NFD_LOG_TRACE("unregister prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700376
Vince Lehman4387e782014-06-19 16:57:45 -0500377 m_managedRib.erase(parameters.getName(), faceEntry);
378
379 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700380}
381
382void
383RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
384 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700385{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700386 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Syed Obaid3313a372014-07-01 01:31:33 -0500387 if (static_cast<bool>(request))
388 sendResponse(request->getName(), 403, failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700389}
390
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700391
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700392bool
393RibManager::extractParameters(const Name::Component& parameterComponent,
394 ControlParameters& extractedParameters)
395{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700396 try
397 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700398 Block rawParameters = parameterComponent.blockFromValue();
399 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700400 }
Junxiao Shi67f11ac2014-10-19 09:29:13 -0700401 catch (const tlv::Error&)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700402 {
403 return false;
404 }
405
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700406 NFD_LOG_DEBUG("Parameters parsed OK");
407 return true;
408}
409
410bool
411RibManager::validateParameters(const ControlCommand& command,
412 ControlParameters& parameters)
413{
414 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700415 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700416 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700417 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700418 catch (const ControlCommand::ArgumentError&)
419 {
420 return false;
421 }
422
423 command.applyDefaultsToRequest(parameters);
424
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700425 return true;
426}
427
428void
429RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700430 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500431 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700432{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700433 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700434
435 ControlResponse response;
436
437 if (code == 404)
438 {
439 response.setCode(code);
440 response.setText(error);
441 }
442 else
443 {
444 response.setCode(533);
445 std::ostringstream os;
446 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
447 response.setText(os.str());
448 }
449
Syed Obaid3313a372014-07-01 01:31:33 -0500450 if (static_cast<bool>(request))
451 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700452}
453
454void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700455RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
456 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500457 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700458{
459 ControlResponse response;
460
461 response.setCode(200);
462 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700463 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700464
Vince12e49462014-06-09 13:29:32 -0500465 NFD_LOG_TRACE("onRegSuccess: registered " << faceEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700466
Syed Obaid3313a372014-07-01 01:31:33 -0500467 if (static_cast<bool>(request))
468 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700469}
470
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700471
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700472void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700473RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
474 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500475 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700476{
477 ControlResponse response;
478
479 response.setCode(200);
480 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700481 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700482
Vince12e49462014-06-09 13:29:32 -0500483 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700484
Syed Obaid3313a372014-07-01 01:31:33 -0500485 if (static_cast<bool>(request))
486 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500487}
488
489void
490RibManager::sendSuccessResponse(const shared_ptr<const Interest>& request,
491 const ControlParameters& parameters)
492{
493 if (!static_cast<bool>(request))
494 {
495 return;
496 }
497
498 ControlResponse response;
499
500 response.setCode(200);
501 response.setText("Success");
502 response.setBody(parameters.wireEncode());
503
Syed Obaid3313a372014-07-01 01:31:33 -0500504 if (static_cast<bool>(request))
505 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500506}
507
508void
509RibManager::sendErrorResponse(uint32_t code, const std::string& error,
510 const shared_ptr<const Interest>& request)
511{
512 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
513
514 if (!static_cast<bool>(request))
515 {
516 return;
517 }
518
519 ControlResponse response;
520
521 if (code == 404)
522 {
523 response.setCode(code);
524 response.setText(error);
525 }
526 else
527 {
528 response.setCode(533);
529 std::ostringstream os;
530 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
531 response.setText(os.str());
532 }
533
Syed Obaid3313a372014-07-01 01:31:33 -0500534 if (static_cast<bool>(request))
535 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700536}
537
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700538void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700539RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix)
540{
541 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
542}
543
544void
545RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg)
546{
547 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
548}
549
Vince Lehman4387e782014-06-19 16:57:45 -0500550bool
551RibManager::isTransactionComplete(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700552{
Vince Lehman4387e782014-06-19 16:57:45 -0500553 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
554
555 if (it != m_pendingFibTransactions.end())
556 {
557 int& updatesLeft = it->second;
558
559 updatesLeft--;
560
561 // All of the updates have been applied successfully
562 if (updatesLeft == 0)
563 {
564 m_pendingFibTransactions.erase(it);
565 return true;
566 }
567 }
568
569 return false;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700570}
571
572void
Vince Lehman4387e782014-06-19 16:57:45 -0500573RibManager::invalidateTransaction(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700574{
Vince Lehman4387e782014-06-19 16:57:45 -0500575 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
576
577 if (it != m_pendingFibTransactions.end())
578 {
579 m_pendingFibTransactions.erase(it);
580 }
581}
582
583void
584RibManager::onAddNextHopSuccess(const shared_ptr<const Interest>& request,
585 const ControlParameters& parameters,
586 const TransactionId transactionId,
587 const bool shouldSendResponse)
588{
589 if (isTransactionComplete(transactionId) && shouldSendResponse)
590 {
591 sendSuccessResponse(request, parameters);
592 }
593}
594
595void
596RibManager::onAddNextHopError(uint32_t code, const std::string& error,
597 const shared_ptr<const Interest>& request,
598 const TransactionId transactionId, const bool shouldSendResponse)
599{
600 invalidateTransaction(transactionId);
601
602 if (shouldSendResponse)
603 {
604 sendErrorResponse(code, error, request);
605 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500606
607 // Since the FIB rejected the update, clean up the invalid face
608 scheduleActiveFaceFetch(time::seconds(1));
Vince Lehman4387e782014-06-19 16:57:45 -0500609}
610
611void
612RibManager::onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
613 const ControlParameters& parameters,
614 const TransactionId transactionId,
615 const bool shouldSendResponse)
616{
617 if (isTransactionComplete(transactionId) && shouldSendResponse)
618 {
619 sendSuccessResponse(request, parameters);
620 }
621}
622
623void
624RibManager::onRemoveNextHopError(uint32_t code, const std::string& error,
625 const shared_ptr<const Interest>& request,
626 const TransactionId transactionId, const bool shouldSendResponse)
627{
628 invalidateTransaction(transactionId);
629
630 if (shouldSendResponse)
631 {
632 sendErrorResponse(code, error, request);
633 }
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700634}
635
636void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700637RibManager::onControlHeaderSuccess()
638{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700639 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700640}
641
642void
643RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
644{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700645 std::ostringstream os;
646 os << "Couldn't enable local control header "
647 << "(code: " << code << ", info: " << reason << ")";
648 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700649}
650
651void
652RibManager::enableLocalControlHeader()
653{
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700654 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700655 ControlParameters()
656 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
657 bind(&RibManager::onControlHeaderSuccess, this),
658 bind(&RibManager::onControlHeaderError, this, _1, _2));
659}
660
661void
662RibManager::onNotification(const FaceEventNotification& notification)
663{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700664 NFD_LOG_TRACE("onNotification: " << notification);
Vince Lehmancd613c52014-07-30 14:34:49 -0500665
Vince Lehman26b215c2014-08-17 15:00:41 -0500666 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED)
Vince Lehmancd613c52014-07-30 14:34:49 -0500667 {
668 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
Vince Lehmancd613c52014-07-30 14:34:49 -0500669
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700670 scheduler::schedule(time::seconds(0),
671 bind(&RibManager::processErasureAfterNotification, this,
672 notification.getFaceId()));
Vince Lehman4387e782014-06-19 16:57:45 -0500673 }
674}
675
676void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700677RibManager::processErasureAfterNotification(uint64_t faceId)
678{
679 m_managedRib.erase(faceId);
Vince Lehman26b215c2014-08-17 15:00:41 -0500680 m_registeredFaces.erase(faceId);
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700681
682 sendUpdatesToFibAfterFaceDestroyEvent();
683}
684
685void
Vince Lehman4387e782014-06-19 16:57:45 -0500686RibManager::sendUpdatesToFib(const shared_ptr<const Interest>& request,
687 const ControlParameters& parameters)
688{
689 const Rib::FibUpdateList& updates = m_managedRib.getFibUpdates();
690
691 // If no updates were generated, consider the operation a success
692 if (updates.empty())
693 {
694 sendSuccessResponse(request, parameters);
695 return;
696 }
697
698 bool shouldWaitToRespond = false;
699
700 // An application request should wait for all FIB updates to be applied
701 // successfully before sending a response
702 if (parameters.getOrigin() == ndn::nfd::ROUTE_ORIGIN_APP)
703 {
704 shouldWaitToRespond = true;
705 }
706 else // Respond immediately
707 {
708 sendSuccessResponse(request, parameters);
709 }
710
Vince Lehmanbc05b762014-08-15 15:37:02 -0500711 std::string updateString = (updates.size() == 1) ? " update" : " updates";
712 NFD_LOG_DEBUG("Applying " << updates.size() << updateString << " to FIB");
Vince Lehman4387e782014-06-19 16:57:45 -0500713
714 // Assign an ID to this FIB transaction
715 TransactionId currentTransactionId = ++m_lastTransactionId;
716
717 // Add this transaction to the transaction table
718 m_pendingFibTransactions[currentTransactionId] = updates.size();
719
720 for (Rib::FibUpdateList::const_iterator it = updates.begin(); it != updates.end(); ++it)
721 {
722 shared_ptr<const FibUpdate> update(*it);
Vince Lehmanbc05b762014-08-15 15:37:02 -0500723 NFD_LOG_DEBUG("Sending FIB update: " << *update);
Vince Lehman4387e782014-06-19 16:57:45 -0500724
725 if (update->action == FibUpdate::ADD_NEXTHOP)
726 {
727 FaceEntry faceEntry;
728 faceEntry.faceId = update->faceId;
729 faceEntry.cost = update->cost;
730
731 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
732 ControlParameters()
733 .setName(update->name)
734 .setFaceId(faceEntry.faceId)
735 .setCost(faceEntry.cost),
736 bind(&RibManager::onAddNextHopSuccess, this, request,
737 parameters,
738 currentTransactionId,
739 shouldWaitToRespond),
740 bind(&RibManager::onAddNextHopError, this, _1, _2, request, currentTransactionId,
741 shouldWaitToRespond));
742 }
743 else if (update->action == FibUpdate::REMOVE_NEXTHOP)
744 {
745 FaceEntry faceEntry;
746 faceEntry.faceId = update->faceId;
747
748 m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>(
749 ControlParameters()
750 .setName(update->name)
751 .setFaceId(faceEntry.faceId),
752 bind(&RibManager::onRemoveNextHopSuccess, this, request,
753 parameters,
754 currentTransactionId,
755 shouldWaitToRespond),
756 bind(&RibManager::onRemoveNextHopError, this, _1, _2, request, currentTransactionId,
757 shouldWaitToRespond));
758 }
759 }
760
761 m_managedRib.clearFibUpdates();
762}
763
764void
765RibManager::sendUpdatesToFibAfterFaceDestroyEvent()
766{
767 ControlParameters parameters;
768 parameters.setOrigin(ndn::nfd::ROUTE_ORIGIN_STATIC);
769
770 sendUpdatesToFib(shared_ptr<const Interest>(), parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700771}
772
Vince Lehmancd16c832014-07-23 15:14:55 -0700773void
774RibManager::listEntries(const Interest& request)
775{
776 const Name& command = request.getName();
777 const size_t commandNComps = command.size();
778
779 if (commandNComps < LIST_COMMAND_NCOMPS ||
780 !LIST_COMMAND_PREFIX.isPrefixOf(command))
781 {
782 NFD_LOG_DEBUG("command result: malformed");
783 sendResponse(command, 400, "Malformed command");
784 return;
785 }
786
787 m_ribStatusPublisher.publish();
788}
789
Vince Lehmancd613c52014-07-30 14:34:49 -0500790void
Vince Lehman26b215c2014-08-17 15:00:41 -0500791RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
792{
793 scheduler::cancel(m_activeFaceFetchEvent);
794
795 m_activeFaceFetchEvent = scheduler::schedule(timeToWait,
796 bind(&RibManager::fetchActiveFaces, this));
797}
798
799void
Vince Lehmancd613c52014-07-30 14:34:49 -0500800RibManager::fetchActiveFaces()
801{
802 NFD_LOG_DEBUG("Fetching active faces");
803
804 Interest interest(FACES_LIST_DATASET_PREFIX);
805 interest.setChildSelector(1);
806 interest.setMustBeFresh(true);
807
808 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
809
810 m_face.expressInterest(interest,
811 bind(&RibManager::fetchSegments, this, _2, buffer),
812 bind(&RibManager::onFetchFaceStatusTimeout, this));
813}
814
815void
816RibManager::fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer)
817{
818 buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
819 data.getContent().value_size());
820
821 uint64_t currentSegment = data.getName().get(-1).toSegment();
822
823 const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
824 if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment)
825 {
826 m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
827 bind(&RibManager::fetchSegments, this, _2, buffer),
828 bind(&RibManager::onFetchFaceStatusTimeout, this));
829 }
830 else
831 {
Vince Lehman26b215c2014-08-17 15:00:41 -0500832 removeInvalidFaces(buffer);
Vince Lehmancd613c52014-07-30 14:34:49 -0500833 }
834}
835
836void
Vince Lehman26b215c2014-08-17 15:00:41 -0500837RibManager::removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer)
Vince Lehmancd613c52014-07-30 14:34:49 -0500838{
Vince Lehman26b215c2014-08-17 15:00:41 -0500839 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500840
841 ndn::ConstBufferPtr buf = buffer->buf();
842
843 Block block;
844 size_t offset = 0;
Vince Lehman26b215c2014-08-17 15:00:41 -0500845 FaceIdSet activeFaces;
Vince Lehmancd613c52014-07-30 14:34:49 -0500846
847 while (offset < buf->size())
848 {
849 if (!Block::fromBuffer(buf, offset, block))
850 {
851 std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
852 break;
853 }
854
855 offset += block.size();
856
857 ndn::nfd::FaceStatus status(block);
Vince Lehmancd613c52014-07-30 14:34:49 -0500858 activeFaces.insert(status.getFaceId());
859 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500860
861 // Look for face IDs that were registered but not active to find missed
862 // face destroyed events
863 for (FaceIdSet::iterator it = m_registeredFaces.begin(); it != m_registeredFaces.end(); ++it)
864 {
865 if (activeFaces.find(*it) == activeFaces.end())
866 {
867 NFD_LOG_DEBUG("Removing invalid face ID: " << *it);
868 scheduler::schedule(time::seconds(0),
869 bind(&RibManager::processErasureAfterNotification, this, *it));
870 }
871 }
872
873 // Reschedule the check for future clean up
874 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500875}
876
877void
878RibManager::onFetchFaceStatusTimeout()
879{
880 std::cerr << "Face Status Dataset request timed out" << std::endl;
Vince Lehman26b215c2014-08-17 15:00:41 -0500881 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500882}
883
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700884} // namespace rib
885} // namespace nfd