blob: 809b301d990874916acc65ae521b0b45ec245435 [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi1e46be32015-01-08 20:18:05 -07003 * Copyright (c) 2014-2015, 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{
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800147 bool isRemoteRegisterEnabled = false;
148
Yingdi Yue5224e92014-04-29 18:04:02 -0700149 for (ConfigSection::const_iterator i = configSection.begin();
150 i != configSection.end(); ++i)
151 {
152 if (i->first == "localhost_security")
153 m_localhostValidator.load(i->second, filename);
154 else if (i->first == "localhop_security")
155 {
156 m_localhopValidator.load(i->second, filename);
157 m_isLocalhopEnabled = true;
158 }
Yanbiao Lic17de832014-11-21 17:51:45 -0800159 else if (i->first == "remote_register")
160 {
161 m_remoteRegistrator.loadConfig(i->second);
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800162 isRemoteRegisterEnabled = true;
163 // avoid other actions when isDryRun == true
164 if (isDryRun)
165 {
166 continue;
167 }
Yanbiao Lic17de832014-11-21 17:51:45 -0800168
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800169 m_remoteRegistrator.enable();
Yanbiao Lic17de832014-11-21 17:51:45 -0800170 }
Yingdi Yue5224e92014-04-29 18:04:02 -0700171 else
172 throw Error("Unrecognized rib property: " + i->first);
173 }
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800174
175 if (!isRemoteRegisterEnabled)
176 {
177 m_remoteRegistrator.disable();
178 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700179}
180
181void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700182RibManager::sendResponse(const Name& name,
183 const ControlResponse& response)
184{
185 const Block& encodedControl = response.wireEncode();
186
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700187 shared_ptr<Data> responseData = make_shared<Data>(name);
188 responseData->setContent(encodedControl);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700189
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700190 m_keyChain.sign(*responseData);
191 m_face.put(*responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700192}
193
194void
195RibManager::sendResponse(const Name& name,
196 uint32_t code,
197 const std::string& text)
198{
199 ControlResponse response(code, text);
200 sendResponse(name, response);
201}
202
203void
Yingdi Yue5224e92014-04-29 18:04:02 -0700204RibManager::onLocalhostRequest(const Interest& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700205{
Vince Lehmancd16c832014-07-23 15:14:55 -0700206 const Name& command = request.getName();
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800207 const Name::Component& verb = command.get(COMMAND_PREFIX.size());
Vince Lehmancd16c832014-07-23 15:14:55 -0700208
209 UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
210
211 if (unsignedVerbProcessor != m_unsignedVerbDispatch.end())
212 {
213 NFD_LOG_DEBUG("command result: processing unsigned verb: " << verb);
214 (unsignedVerbProcessor->second)(this, request);
215 }
216 else
217 {
218 m_localhostValidator.validate(request,
219 bind(&RibManager::onCommandValidated, this, _1),
220 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
221 }
Yingdi Yue5224e92014-04-29 18:04:02 -0700222}
223
224void
225RibManager::onLocalhopRequest(const Interest& request)
226{
227 m_localhopValidator.validate(request,
228 bind(&RibManager::onCommandValidated, this, _1),
229 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700230}
231
232void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700233RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700234{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700235 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700236 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700237
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700238 const Name& command = request->getName();
239 const Name::Component& verb = command[COMMAND_PREFIX.size()];
240 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
241
Vince Lehmancd16c832014-07-23 15:14:55 -0700242 SignedVerbDispatchTable::const_iterator verbProcessor = m_signedVerbDispatch.find(verb);
243 if (verbProcessor != m_signedVerbDispatch.end())
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700244 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700245 ControlParameters parameters;
246 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700247 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700248 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500249 if (static_cast<bool>(request))
250 sendResponse(command, 400, "Malformed command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700251 return;
252 }
253
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700254 NFD_LOG_DEBUG("command result: processing verb: " << verb);
255 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700256 }
257 else
258 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700259 NFD_LOG_DEBUG("Unsupported command: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500260 if (static_cast<bool>(request))
261 sendResponse(request->getName(), 501, "Unsupported command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700262 }
263}
264
265void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700266RibManager::registerEntry(const shared_ptr<const Interest>& request,
267 ControlParameters& parameters)
268{
269 ndn::nfd::RibRegisterCommand command;
270
271 if (!validateParameters(command, parameters))
272 {
273 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
Vince Lehmancd613c52014-07-30 14:34:49 -0500274
Syed Obaid3313a372014-07-01 01:31:33 -0500275 if (static_cast<bool>(request))
Vince Lehmancd613c52014-07-30 14:34:49 -0500276 {
277 sendResponse(request->getName(), 400, "Malformed command");
278 }
279
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700280 return;
281 }
282
Alexander Afanasyev483efd12014-08-14 10:51:18 -0700283 bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0);
284 if (isSelfRegistration)
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700285 {
286 parameters.setFaceId(request->getIncomingFaceId());
287 }
288
Vince12e49462014-06-09 13:29:32 -0500289 FaceEntry faceEntry;
290 faceEntry.faceId = parameters.getFaceId();
291 faceEntry.origin = parameters.getOrigin();
292 faceEntry.cost = parameters.getCost();
293 faceEntry.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500294
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700295 if (parameters.hasExpirationPeriod() &&
296 parameters.getExpirationPeriod() != time::milliseconds::max())
Syed Obaid3313a372014-07-01 01:31:33 -0500297 {
298 faceEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
299
300 // Schedule a new event, the old one will be cancelled during rib insertion.
Junxiao Shi1e46be32015-01-08 20:18:05 -0700301 scheduler::EventId eventId = scheduler::schedule(parameters.getExpirationPeriod(),
302 bind(&RibManager::expireEntry, this, shared_ptr<Interest>(), parameters));
Vince Lehman281ded72014-08-21 12:17:08 -0500303 NFD_LOG_TRACE("Scheduled unregistration at: " << faceEntry.expires <<
304 " with EventId: " << eventId);
Syed Obaid3313a372014-07-01 01:31:33 -0500305
306 //set the NewEventId of this entry
307 faceEntry.setExpirationEvent(eventId);
308 }
309 else
310 {
311 faceEntry.expires = time::steady_clock::TimePoint::max();
312 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700313
Vince12e49462014-06-09 13:29:32 -0500314 NFD_LOG_TRACE("register prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700315
Vince12e49462014-06-09 13:29:32 -0500316 m_managedRib.insert(parameters.getName(), faceEntry);
Vince Lehman26b215c2014-08-17 15:00:41 -0500317 m_registeredFaces.insert(faceEntry.faceId);
Vince Lehman4387e782014-06-19 16:57:45 -0500318
319 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700320}
321
322void
Vince Lehman281ded72014-08-21 12:17:08 -0500323RibManager::expireEntry(const shared_ptr<const Interest>& request, ControlParameters& params)
324{
325 FaceEntry face;
326 face.faceId = params.getFaceId();
327 face.origin = params.getOrigin();
328 face.cost = params.getCost();
329 face.flags = params.getFlags();
330
331 NFD_LOG_DEBUG(face << " for " << params.getName() << " has expired");
332 unregisterEntry(request, params);
333}
334
335void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700336RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
Syed Obaid3313a372014-07-01 01:31:33 -0500337 ControlParameters& params)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700338{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700339 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700340
Syed Obaid3313a372014-07-01 01:31:33 -0500341 //passing all parameters gives error in validation.
342 //so passing only the required arguments.
343 ControlParameters parameters;
344 parameters.setName(params.getName());
Alexander Afanasyevb609f002014-07-18 16:56:34 -0700345 if (params.hasFaceId())
346 parameters.setFaceId(params.getFaceId());
347 if (params.hasOrigin())
348 parameters.setOrigin(params.getOrigin());
Syed Obaid3313a372014-07-01 01:31:33 -0500349
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700350 if (!validateParameters(command, parameters))
351 {
Syed Obaid3313a372014-07-01 01:31:33 -0500352 NFD_LOG_DEBUG("unregister result: FAIL reason: malformed");
353 if (static_cast<bool>(request))
354 sendResponse(request->getName(), 400, "Malformed command");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700355 return;
356 }
357
Alexander Afanasyev483efd12014-08-14 10:51:18 -0700358 bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0);
359 if (isSelfRegistration)
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700360 {
361 parameters.setFaceId(request->getIncomingFaceId());
362 }
363
Vince12e49462014-06-09 13:29:32 -0500364 FaceEntry faceEntry;
365 faceEntry.faceId = parameters.getFaceId();
366 faceEntry.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700367
Vince12e49462014-06-09 13:29:32 -0500368 NFD_LOG_TRACE("unregister prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700369
Vince Lehman4387e782014-06-19 16:57:45 -0500370 m_managedRib.erase(parameters.getName(), faceEntry);
371
372 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700373}
374
375void
376RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
377 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700378{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700379 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Syed Obaid3313a372014-07-01 01:31:33 -0500380 if (static_cast<bool>(request))
381 sendResponse(request->getName(), 403, failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700382}
383
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700384
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700385bool
386RibManager::extractParameters(const Name::Component& parameterComponent,
387 ControlParameters& extractedParameters)
388{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700389 try
390 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700391 Block rawParameters = parameterComponent.blockFromValue();
392 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700393 }
Junxiao Shi67f11ac2014-10-19 09:29:13 -0700394 catch (const tlv::Error&)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700395 {
396 return false;
397 }
398
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700399 NFD_LOG_DEBUG("Parameters parsed OK");
400 return true;
401}
402
403bool
404RibManager::validateParameters(const ControlCommand& command,
405 ControlParameters& parameters)
406{
407 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700408 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700409 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700410 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700411 catch (const ControlCommand::ArgumentError&)
412 {
413 return false;
414 }
415
416 command.applyDefaultsToRequest(parameters);
417
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700418 return true;
419}
420
421void
422RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700423 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500424 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700425{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700426 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700427
428 ControlResponse response;
429
430 if (code == 404)
431 {
432 response.setCode(code);
433 response.setText(error);
434 }
435 else
436 {
437 response.setCode(533);
438 std::ostringstream os;
439 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
440 response.setText(os.str());
441 }
442
Syed Obaid3313a372014-07-01 01:31:33 -0500443 if (static_cast<bool>(request))
444 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700445}
446
447void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700448RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
449 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500450 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700451{
452 ControlResponse response;
453
454 response.setCode(200);
455 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700456 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700457
Vince12e49462014-06-09 13:29:32 -0500458 NFD_LOG_TRACE("onRegSuccess: registered " << faceEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700459
Syed Obaid3313a372014-07-01 01:31:33 -0500460 if (static_cast<bool>(request))
461 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700462}
463
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700464
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700465void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700466RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
467 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500468 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700469{
470 ControlResponse response;
471
472 response.setCode(200);
473 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700474 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700475
Vince12e49462014-06-09 13:29:32 -0500476 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700477
Syed Obaid3313a372014-07-01 01:31:33 -0500478 if (static_cast<bool>(request))
479 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500480}
481
482void
483RibManager::sendSuccessResponse(const shared_ptr<const Interest>& request,
484 const ControlParameters& parameters)
485{
486 if (!static_cast<bool>(request))
487 {
488 return;
489 }
490
491 ControlResponse response;
492
493 response.setCode(200);
494 response.setText("Success");
495 response.setBody(parameters.wireEncode());
496
Syed Obaid3313a372014-07-01 01:31:33 -0500497 if (static_cast<bool>(request))
498 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500499}
500
501void
502RibManager::sendErrorResponse(uint32_t code, const std::string& error,
503 const shared_ptr<const Interest>& request)
504{
505 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
506
507 if (!static_cast<bool>(request))
508 {
509 return;
510 }
511
512 ControlResponse response;
513
514 if (code == 404)
515 {
516 response.setCode(code);
517 response.setText(error);
518 }
519 else
520 {
521 response.setCode(533);
522 std::ostringstream os;
523 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
524 response.setText(os.str());
525 }
526
Syed Obaid3313a372014-07-01 01:31:33 -0500527 if (static_cast<bool>(request))
528 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700529}
530
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700531void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700532RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix)
533{
534 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
535}
536
537void
538RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg)
539{
540 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
541}
542
Vince Lehman4387e782014-06-19 16:57:45 -0500543bool
544RibManager::isTransactionComplete(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700545{
Vince Lehman4387e782014-06-19 16:57:45 -0500546 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
547
548 if (it != m_pendingFibTransactions.end())
549 {
550 int& updatesLeft = it->second;
551
552 updatesLeft--;
553
554 // All of the updates have been applied successfully
555 if (updatesLeft == 0)
556 {
557 m_pendingFibTransactions.erase(it);
558 return true;
559 }
560 }
561
562 return false;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700563}
564
565void
Vince Lehman4387e782014-06-19 16:57:45 -0500566RibManager::invalidateTransaction(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700567{
Vince Lehman4387e782014-06-19 16:57:45 -0500568 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
569
570 if (it != m_pendingFibTransactions.end())
571 {
572 m_pendingFibTransactions.erase(it);
573 }
574}
575
576void
577RibManager::onAddNextHopSuccess(const shared_ptr<const Interest>& request,
578 const ControlParameters& parameters,
579 const TransactionId transactionId,
580 const bool shouldSendResponse)
581{
582 if (isTransactionComplete(transactionId) && shouldSendResponse)
583 {
584 sendSuccessResponse(request, parameters);
585 }
586}
587
588void
589RibManager::onAddNextHopError(uint32_t code, const std::string& error,
590 const shared_ptr<const Interest>& request,
591 const TransactionId transactionId, const bool shouldSendResponse)
592{
593 invalidateTransaction(transactionId);
594
595 if (shouldSendResponse)
596 {
597 sendErrorResponse(code, error, request);
598 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500599
600 // Since the FIB rejected the update, clean up the invalid face
601 scheduleActiveFaceFetch(time::seconds(1));
Vince Lehman4387e782014-06-19 16:57:45 -0500602}
603
604void
605RibManager::onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
606 const ControlParameters& parameters,
607 const TransactionId transactionId,
608 const bool shouldSendResponse)
609{
610 if (isTransactionComplete(transactionId) && shouldSendResponse)
611 {
612 sendSuccessResponse(request, parameters);
613 }
614}
615
616void
617RibManager::onRemoveNextHopError(uint32_t code, const std::string& error,
618 const shared_ptr<const Interest>& request,
619 const TransactionId transactionId, const bool shouldSendResponse)
620{
621 invalidateTransaction(transactionId);
622
623 if (shouldSendResponse)
624 {
625 sendErrorResponse(code, error, request);
626 }
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700627}
628
629void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700630RibManager::onControlHeaderSuccess()
631{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700632 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700633}
634
635void
636RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
637{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700638 std::ostringstream os;
639 os << "Couldn't enable local control header "
640 << "(code: " << code << ", info: " << reason << ")";
641 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700642}
643
644void
645RibManager::enableLocalControlHeader()
646{
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700647 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700648 ControlParameters()
649 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
650 bind(&RibManager::onControlHeaderSuccess, this),
651 bind(&RibManager::onControlHeaderError, this, _1, _2));
652}
653
654void
655RibManager::onNotification(const FaceEventNotification& notification)
656{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700657 NFD_LOG_TRACE("onNotification: " << notification);
Vince Lehmancd613c52014-07-30 14:34:49 -0500658
Vince Lehman26b215c2014-08-17 15:00:41 -0500659 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED)
Vince Lehmancd613c52014-07-30 14:34:49 -0500660 {
661 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
Vince Lehmancd613c52014-07-30 14:34:49 -0500662
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700663 scheduler::schedule(time::seconds(0),
664 bind(&RibManager::processErasureAfterNotification, this,
665 notification.getFaceId()));
Vince Lehman4387e782014-06-19 16:57:45 -0500666 }
667}
668
669void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700670RibManager::processErasureAfterNotification(uint64_t faceId)
671{
672 m_managedRib.erase(faceId);
Vince Lehman26b215c2014-08-17 15:00:41 -0500673 m_registeredFaces.erase(faceId);
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700674
675 sendUpdatesToFibAfterFaceDestroyEvent();
676}
677
678void
Vince Lehman4387e782014-06-19 16:57:45 -0500679RibManager::sendUpdatesToFib(const shared_ptr<const Interest>& request,
680 const ControlParameters& parameters)
681{
682 const Rib::FibUpdateList& updates = m_managedRib.getFibUpdates();
683
684 // If no updates were generated, consider the operation a success
685 if (updates.empty())
686 {
687 sendSuccessResponse(request, parameters);
688 return;
689 }
690
691 bool shouldWaitToRespond = false;
692
693 // An application request should wait for all FIB updates to be applied
694 // successfully before sending a response
695 if (parameters.getOrigin() == ndn::nfd::ROUTE_ORIGIN_APP)
696 {
697 shouldWaitToRespond = true;
698 }
699 else // Respond immediately
700 {
701 sendSuccessResponse(request, parameters);
702 }
703
Vince Lehmanbc05b762014-08-15 15:37:02 -0500704 std::string updateString = (updates.size() == 1) ? " update" : " updates";
705 NFD_LOG_DEBUG("Applying " << updates.size() << updateString << " to FIB");
Vince Lehman4387e782014-06-19 16:57:45 -0500706
707 // Assign an ID to this FIB transaction
708 TransactionId currentTransactionId = ++m_lastTransactionId;
709
710 // Add this transaction to the transaction table
711 m_pendingFibTransactions[currentTransactionId] = updates.size();
712
713 for (Rib::FibUpdateList::const_iterator it = updates.begin(); it != updates.end(); ++it)
714 {
715 shared_ptr<const FibUpdate> update(*it);
Vince Lehmanbc05b762014-08-15 15:37:02 -0500716 NFD_LOG_DEBUG("Sending FIB update: " << *update);
Vince Lehman4387e782014-06-19 16:57:45 -0500717
718 if (update->action == FibUpdate::ADD_NEXTHOP)
719 {
720 FaceEntry faceEntry;
721 faceEntry.faceId = update->faceId;
722 faceEntry.cost = update->cost;
723
724 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
725 ControlParameters()
726 .setName(update->name)
727 .setFaceId(faceEntry.faceId)
728 .setCost(faceEntry.cost),
729 bind(&RibManager::onAddNextHopSuccess, this, request,
730 parameters,
731 currentTransactionId,
732 shouldWaitToRespond),
733 bind(&RibManager::onAddNextHopError, this, _1, _2, request, currentTransactionId,
734 shouldWaitToRespond));
735 }
736 else if (update->action == FibUpdate::REMOVE_NEXTHOP)
737 {
738 FaceEntry faceEntry;
739 faceEntry.faceId = update->faceId;
740
741 m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>(
742 ControlParameters()
743 .setName(update->name)
744 .setFaceId(faceEntry.faceId),
745 bind(&RibManager::onRemoveNextHopSuccess, this, request,
746 parameters,
747 currentTransactionId,
748 shouldWaitToRespond),
749 bind(&RibManager::onRemoveNextHopError, this, _1, _2, request, currentTransactionId,
750 shouldWaitToRespond));
751 }
752 }
753
754 m_managedRib.clearFibUpdates();
755}
756
757void
758RibManager::sendUpdatesToFibAfterFaceDestroyEvent()
759{
760 ControlParameters parameters;
761 parameters.setOrigin(ndn::nfd::ROUTE_ORIGIN_STATIC);
762
763 sendUpdatesToFib(shared_ptr<const Interest>(), parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700764}
765
Vince Lehmancd16c832014-07-23 15:14:55 -0700766void
767RibManager::listEntries(const Interest& request)
768{
769 const Name& command = request.getName();
770 const size_t commandNComps = command.size();
771
772 if (commandNComps < LIST_COMMAND_NCOMPS ||
773 !LIST_COMMAND_PREFIX.isPrefixOf(command))
774 {
775 NFD_LOG_DEBUG("command result: malformed");
776 sendResponse(command, 400, "Malformed command");
777 return;
778 }
779
780 m_ribStatusPublisher.publish();
781}
782
Vince Lehmancd613c52014-07-30 14:34:49 -0500783void
Vince Lehman26b215c2014-08-17 15:00:41 -0500784RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
785{
786 scheduler::cancel(m_activeFaceFetchEvent);
787
788 m_activeFaceFetchEvent = scheduler::schedule(timeToWait,
789 bind(&RibManager::fetchActiveFaces, this));
790}
791
792void
Vince Lehmancd613c52014-07-30 14:34:49 -0500793RibManager::fetchActiveFaces()
794{
795 NFD_LOG_DEBUG("Fetching active faces");
796
797 Interest interest(FACES_LIST_DATASET_PREFIX);
798 interest.setChildSelector(1);
799 interest.setMustBeFresh(true);
800
801 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
802
803 m_face.expressInterest(interest,
804 bind(&RibManager::fetchSegments, this, _2, buffer),
805 bind(&RibManager::onFetchFaceStatusTimeout, this));
806}
807
808void
809RibManager::fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer)
810{
811 buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
812 data.getContent().value_size());
813
814 uint64_t currentSegment = data.getName().get(-1).toSegment();
815
816 const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
817 if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment)
818 {
819 m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
820 bind(&RibManager::fetchSegments, this, _2, buffer),
821 bind(&RibManager::onFetchFaceStatusTimeout, this));
822 }
823 else
824 {
Vince Lehman26b215c2014-08-17 15:00:41 -0500825 removeInvalidFaces(buffer);
Vince Lehmancd613c52014-07-30 14:34:49 -0500826 }
827}
828
829void
Vince Lehman26b215c2014-08-17 15:00:41 -0500830RibManager::removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer)
Vince Lehmancd613c52014-07-30 14:34:49 -0500831{
Vince Lehman26b215c2014-08-17 15:00:41 -0500832 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500833
834 ndn::ConstBufferPtr buf = buffer->buf();
835
836 Block block;
837 size_t offset = 0;
Vince Lehman26b215c2014-08-17 15:00:41 -0500838 FaceIdSet activeFaces;
Vince Lehmancd613c52014-07-30 14:34:49 -0500839
840 while (offset < buf->size())
841 {
842 if (!Block::fromBuffer(buf, offset, block))
843 {
844 std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
845 break;
846 }
847
848 offset += block.size();
849
850 ndn::nfd::FaceStatus status(block);
Vince Lehmancd613c52014-07-30 14:34:49 -0500851 activeFaces.insert(status.getFaceId());
852 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500853
854 // Look for face IDs that were registered but not active to find missed
855 // face destroyed events
856 for (FaceIdSet::iterator it = m_registeredFaces.begin(); it != m_registeredFaces.end(); ++it)
857 {
858 if (activeFaces.find(*it) == activeFaces.end())
859 {
860 NFD_LOG_DEBUG("Removing invalid face ID: " << *it);
861 scheduler::schedule(time::seconds(0),
862 bind(&RibManager::processErasureAfterNotification, this, *it));
863 }
864 }
865
866 // Reschedule the check for future clean up
867 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500868}
869
870void
871RibManager::onFetchFaceStatusTimeout()
872{
873 std::cerr << "Face Status Dataset request timed out" << std::endl;
Vince Lehman26b215c2014-08-17 15:00:41 -0500874 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500875}
876
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700877} // namespace rib
878} // namespace nfd