blob: e7ea3e992fe580e40b16d78c622aaa44e50d393e [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();
203 const Name::Component& verb = command.get(COMMAND_PREFIX.size());
204
205 UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
206
207 if (unsignedVerbProcessor != m_unsignedVerbDispatch.end())
208 {
209 NFD_LOG_DEBUG("command result: processing unsigned verb: " << verb);
210 (unsignedVerbProcessor->second)(this, request);
211 }
212 else
213 {
214 m_localhostValidator.validate(request,
215 bind(&RibManager::onCommandValidated, this, _1),
216 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
217 }
Yingdi Yue5224e92014-04-29 18:04:02 -0700218}
219
220void
221RibManager::onLocalhopRequest(const Interest& request)
222{
223 m_localhopValidator.validate(request,
224 bind(&RibManager::onCommandValidated, this, _1),
225 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700226}
227
228void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700229RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700230{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700231 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700232 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700233
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700234 const Name& command = request->getName();
235 const Name::Component& verb = command[COMMAND_PREFIX.size()];
236 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
237
Vince Lehmancd16c832014-07-23 15:14:55 -0700238 SignedVerbDispatchTable::const_iterator verbProcessor = m_signedVerbDispatch.find(verb);
239 if (verbProcessor != m_signedVerbDispatch.end())
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700240 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700241 ControlParameters parameters;
242 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700243 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700244 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500245 if (static_cast<bool>(request))
246 sendResponse(command, 400, "Malformed command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700247 return;
248 }
249
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700250 NFD_LOG_DEBUG("command result: processing verb: " << verb);
251 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700252 }
253 else
254 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700255 NFD_LOG_DEBUG("Unsupported command: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500256 if (static_cast<bool>(request))
257 sendResponse(request->getName(), 501, "Unsupported command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700258 }
259}
260
261void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700262RibManager::registerEntry(const shared_ptr<const Interest>& request,
263 ControlParameters& parameters)
264{
265 ndn::nfd::RibRegisterCommand command;
266
267 if (!validateParameters(command, parameters))
268 {
269 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
Vince Lehmancd613c52014-07-30 14:34:49 -0500270
Syed Obaid3313a372014-07-01 01:31:33 -0500271 if (static_cast<bool>(request))
Vince Lehmancd613c52014-07-30 14:34:49 -0500272 {
273 sendResponse(request->getName(), 400, "Malformed command");
274 }
275
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700276 return;
277 }
278
Alexander Afanasyev483efd12014-08-14 10:51:18 -0700279 bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0);
280 if (isSelfRegistration)
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700281 {
282 parameters.setFaceId(request->getIncomingFaceId());
283 }
284
Vince12e49462014-06-09 13:29:32 -0500285 FaceEntry faceEntry;
286 faceEntry.faceId = parameters.getFaceId();
287 faceEntry.origin = parameters.getOrigin();
288 faceEntry.cost = parameters.getCost();
289 faceEntry.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500290
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700291 if (parameters.hasExpirationPeriod() &&
292 parameters.getExpirationPeriod() != time::milliseconds::max())
Syed Obaid3313a372014-07-01 01:31:33 -0500293 {
294 faceEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
295
296 // Schedule a new event, the old one will be cancelled during rib insertion.
297 EventId eventId;
Syed Obaid3313a372014-07-01 01:31:33 -0500298 eventId = scheduler::schedule(parameters.getExpirationPeriod(),
Vince Lehman281ded72014-08-21 12:17:08 -0500299 bind(&RibManager::expireEntry,
Syed Obaid3313a372014-07-01 01:31:33 -0500300 this, shared_ptr<Interest>(), parameters));
Vince Lehman281ded72014-08-21 12:17:08 -0500301 NFD_LOG_TRACE("Scheduled unregistration at: " << faceEntry.expires <<
302 " with EventId: " << eventId);
Syed Obaid3313a372014-07-01 01:31:33 -0500303
304 //set the NewEventId of this entry
305 faceEntry.setExpirationEvent(eventId);
306 }
307 else
308 {
309 faceEntry.expires = time::steady_clock::TimePoint::max();
310 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700311
Vince12e49462014-06-09 13:29:32 -0500312 NFD_LOG_TRACE("register prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700313
Vince12e49462014-06-09 13:29:32 -0500314 m_managedRib.insert(parameters.getName(), faceEntry);
Vince Lehman26b215c2014-08-17 15:00:41 -0500315 m_registeredFaces.insert(faceEntry.faceId);
Vince Lehman4387e782014-06-19 16:57:45 -0500316
317 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700318}
319
320void
Vince Lehman281ded72014-08-21 12:17:08 -0500321RibManager::expireEntry(const shared_ptr<const Interest>& request, ControlParameters& params)
322{
323 FaceEntry face;
324 face.faceId = params.getFaceId();
325 face.origin = params.getOrigin();
326 face.cost = params.getCost();
327 face.flags = params.getFlags();
328
329 NFD_LOG_DEBUG(face << " for " << params.getName() << " has expired");
330 unregisterEntry(request, params);
331}
332
333void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700334RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
Syed Obaid3313a372014-07-01 01:31:33 -0500335 ControlParameters& params)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700336{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700337 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700338
Syed Obaid3313a372014-07-01 01:31:33 -0500339 //passing all parameters gives error in validation.
340 //so passing only the required arguments.
341 ControlParameters parameters;
342 parameters.setName(params.getName());
Alexander Afanasyevb609f002014-07-18 16:56:34 -0700343 if (params.hasFaceId())
344 parameters.setFaceId(params.getFaceId());
345 if (params.hasOrigin())
346 parameters.setOrigin(params.getOrigin());
Syed Obaid3313a372014-07-01 01:31:33 -0500347
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700348 if (!validateParameters(command, parameters))
349 {
Syed Obaid3313a372014-07-01 01:31:33 -0500350 NFD_LOG_DEBUG("unregister result: FAIL reason: malformed");
351 if (static_cast<bool>(request))
352 sendResponse(request->getName(), 400, "Malformed command");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700353 return;
354 }
355
Alexander Afanasyev483efd12014-08-14 10:51:18 -0700356 bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0);
357 if (isSelfRegistration)
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700358 {
359 parameters.setFaceId(request->getIncomingFaceId());
360 }
361
Vince12e49462014-06-09 13:29:32 -0500362 FaceEntry faceEntry;
363 faceEntry.faceId = parameters.getFaceId();
364 faceEntry.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700365
Vince12e49462014-06-09 13:29:32 -0500366 NFD_LOG_TRACE("unregister prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700367
Vince Lehman4387e782014-06-19 16:57:45 -0500368 m_managedRib.erase(parameters.getName(), faceEntry);
369
370 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700371}
372
373void
374RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
375 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700376{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700377 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Syed Obaid3313a372014-07-01 01:31:33 -0500378 if (static_cast<bool>(request))
379 sendResponse(request->getName(), 403, failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700380}
381
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700382
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700383bool
384RibManager::extractParameters(const Name::Component& parameterComponent,
385 ControlParameters& extractedParameters)
386{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700387 try
388 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700389 Block rawParameters = parameterComponent.blockFromValue();
390 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700391 }
Junxiao Shi67f11ac2014-10-19 09:29:13 -0700392 catch (const tlv::Error&)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700393 {
394 return false;
395 }
396
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700397 NFD_LOG_DEBUG("Parameters parsed OK");
398 return true;
399}
400
401bool
402RibManager::validateParameters(const ControlCommand& command,
403 ControlParameters& parameters)
404{
405 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700406 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700407 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700408 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700409 catch (const ControlCommand::ArgumentError&)
410 {
411 return false;
412 }
413
414 command.applyDefaultsToRequest(parameters);
415
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700416 return true;
417}
418
419void
420RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700421 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500422 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700423{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700424 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700425
426 ControlResponse response;
427
428 if (code == 404)
429 {
430 response.setCode(code);
431 response.setText(error);
432 }
433 else
434 {
435 response.setCode(533);
436 std::ostringstream os;
437 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
438 response.setText(os.str());
439 }
440
Syed Obaid3313a372014-07-01 01:31:33 -0500441 if (static_cast<bool>(request))
442 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700443}
444
445void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700446RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
447 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500448 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700449{
450 ControlResponse response;
451
452 response.setCode(200);
453 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700454 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700455
Vince12e49462014-06-09 13:29:32 -0500456 NFD_LOG_TRACE("onRegSuccess: registered " << faceEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700457
Syed Obaid3313a372014-07-01 01:31:33 -0500458 if (static_cast<bool>(request))
459 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700460}
461
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700462
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700463void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700464RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
465 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500466 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700467{
468 ControlResponse response;
469
470 response.setCode(200);
471 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700472 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700473
Vince12e49462014-06-09 13:29:32 -0500474 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700475
Syed Obaid3313a372014-07-01 01:31:33 -0500476 if (static_cast<bool>(request))
477 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500478}
479
480void
481RibManager::sendSuccessResponse(const shared_ptr<const Interest>& request,
482 const ControlParameters& parameters)
483{
484 if (!static_cast<bool>(request))
485 {
486 return;
487 }
488
489 ControlResponse response;
490
491 response.setCode(200);
492 response.setText("Success");
493 response.setBody(parameters.wireEncode());
494
Syed Obaid3313a372014-07-01 01:31:33 -0500495 if (static_cast<bool>(request))
496 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500497}
498
499void
500RibManager::sendErrorResponse(uint32_t code, const std::string& error,
501 const shared_ptr<const Interest>& request)
502{
503 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
504
505 if (!static_cast<bool>(request))
506 {
507 return;
508 }
509
510 ControlResponse response;
511
512 if (code == 404)
513 {
514 response.setCode(code);
515 response.setText(error);
516 }
517 else
518 {
519 response.setCode(533);
520 std::ostringstream os;
521 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
522 response.setText(os.str());
523 }
524
Syed Obaid3313a372014-07-01 01:31:33 -0500525 if (static_cast<bool>(request))
526 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700527}
528
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700529void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700530RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix)
531{
532 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
533}
534
535void
536RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg)
537{
538 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
539}
540
Vince Lehman4387e782014-06-19 16:57:45 -0500541bool
542RibManager::isTransactionComplete(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700543{
Vince Lehman4387e782014-06-19 16:57:45 -0500544 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
545
546 if (it != m_pendingFibTransactions.end())
547 {
548 int& updatesLeft = it->second;
549
550 updatesLeft--;
551
552 // All of the updates have been applied successfully
553 if (updatesLeft == 0)
554 {
555 m_pendingFibTransactions.erase(it);
556 return true;
557 }
558 }
559
560 return false;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700561}
562
563void
Vince Lehman4387e782014-06-19 16:57:45 -0500564RibManager::invalidateTransaction(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700565{
Vince Lehman4387e782014-06-19 16:57:45 -0500566 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
567
568 if (it != m_pendingFibTransactions.end())
569 {
570 m_pendingFibTransactions.erase(it);
571 }
572}
573
574void
575RibManager::onAddNextHopSuccess(const shared_ptr<const Interest>& request,
576 const ControlParameters& parameters,
577 const TransactionId transactionId,
578 const bool shouldSendResponse)
579{
580 if (isTransactionComplete(transactionId) && shouldSendResponse)
581 {
582 sendSuccessResponse(request, parameters);
583 }
584}
585
586void
587RibManager::onAddNextHopError(uint32_t code, const std::string& error,
588 const shared_ptr<const Interest>& request,
589 const TransactionId transactionId, const bool shouldSendResponse)
590{
591 invalidateTransaction(transactionId);
592
593 if (shouldSendResponse)
594 {
595 sendErrorResponse(code, error, request);
596 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500597
598 // Since the FIB rejected the update, clean up the invalid face
599 scheduleActiveFaceFetch(time::seconds(1));
Vince Lehman4387e782014-06-19 16:57:45 -0500600}
601
602void
603RibManager::onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
604 const ControlParameters& parameters,
605 const TransactionId transactionId,
606 const bool shouldSendResponse)
607{
608 if (isTransactionComplete(transactionId) && shouldSendResponse)
609 {
610 sendSuccessResponse(request, parameters);
611 }
612}
613
614void
615RibManager::onRemoveNextHopError(uint32_t code, const std::string& error,
616 const shared_ptr<const Interest>& request,
617 const TransactionId transactionId, const bool shouldSendResponse)
618{
619 invalidateTransaction(transactionId);
620
621 if (shouldSendResponse)
622 {
623 sendErrorResponse(code, error, request);
624 }
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700625}
626
627void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700628RibManager::onControlHeaderSuccess()
629{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700630 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700631}
632
633void
634RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
635{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700636 std::ostringstream os;
637 os << "Couldn't enable local control header "
638 << "(code: " << code << ", info: " << reason << ")";
639 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700640}
641
642void
643RibManager::enableLocalControlHeader()
644{
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700645 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700646 ControlParameters()
647 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
648 bind(&RibManager::onControlHeaderSuccess, this),
649 bind(&RibManager::onControlHeaderError, this, _1, _2));
650}
651
652void
653RibManager::onNotification(const FaceEventNotification& notification)
654{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700655 NFD_LOG_TRACE("onNotification: " << notification);
Vince Lehmancd613c52014-07-30 14:34:49 -0500656
Vince Lehman26b215c2014-08-17 15:00:41 -0500657 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED)
Vince Lehmancd613c52014-07-30 14:34:49 -0500658 {
659 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
Vince Lehmancd613c52014-07-30 14:34:49 -0500660
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700661 scheduler::schedule(time::seconds(0),
662 bind(&RibManager::processErasureAfterNotification, this,
663 notification.getFaceId()));
Vince Lehman4387e782014-06-19 16:57:45 -0500664 }
665}
666
667void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700668RibManager::processErasureAfterNotification(uint64_t faceId)
669{
670 m_managedRib.erase(faceId);
Vince Lehman26b215c2014-08-17 15:00:41 -0500671 m_registeredFaces.erase(faceId);
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700672
673 sendUpdatesToFibAfterFaceDestroyEvent();
674}
675
676void
Vince Lehman4387e782014-06-19 16:57:45 -0500677RibManager::sendUpdatesToFib(const shared_ptr<const Interest>& request,
678 const ControlParameters& parameters)
679{
680 const Rib::FibUpdateList& updates = m_managedRib.getFibUpdates();
681
682 // If no updates were generated, consider the operation a success
683 if (updates.empty())
684 {
685 sendSuccessResponse(request, parameters);
686 return;
687 }
688
689 bool shouldWaitToRespond = false;
690
691 // An application request should wait for all FIB updates to be applied
692 // successfully before sending a response
693 if (parameters.getOrigin() == ndn::nfd::ROUTE_ORIGIN_APP)
694 {
695 shouldWaitToRespond = true;
696 }
697 else // Respond immediately
698 {
699 sendSuccessResponse(request, parameters);
700 }
701
Vince Lehmanbc05b762014-08-15 15:37:02 -0500702 std::string updateString = (updates.size() == 1) ? " update" : " updates";
703 NFD_LOG_DEBUG("Applying " << updates.size() << updateString << " to FIB");
Vince Lehman4387e782014-06-19 16:57:45 -0500704
705 // Assign an ID to this FIB transaction
706 TransactionId currentTransactionId = ++m_lastTransactionId;
707
708 // Add this transaction to the transaction table
709 m_pendingFibTransactions[currentTransactionId] = updates.size();
710
711 for (Rib::FibUpdateList::const_iterator it = updates.begin(); it != updates.end(); ++it)
712 {
713 shared_ptr<const FibUpdate> update(*it);
Vince Lehmanbc05b762014-08-15 15:37:02 -0500714 NFD_LOG_DEBUG("Sending FIB update: " << *update);
Vince Lehman4387e782014-06-19 16:57:45 -0500715
716 if (update->action == FibUpdate::ADD_NEXTHOP)
717 {
718 FaceEntry faceEntry;
719 faceEntry.faceId = update->faceId;
720 faceEntry.cost = update->cost;
721
722 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
723 ControlParameters()
724 .setName(update->name)
725 .setFaceId(faceEntry.faceId)
726 .setCost(faceEntry.cost),
727 bind(&RibManager::onAddNextHopSuccess, this, request,
728 parameters,
729 currentTransactionId,
730 shouldWaitToRespond),
731 bind(&RibManager::onAddNextHopError, this, _1, _2, request, currentTransactionId,
732 shouldWaitToRespond));
733 }
734 else if (update->action == FibUpdate::REMOVE_NEXTHOP)
735 {
736 FaceEntry faceEntry;
737 faceEntry.faceId = update->faceId;
738
739 m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>(
740 ControlParameters()
741 .setName(update->name)
742 .setFaceId(faceEntry.faceId),
743 bind(&RibManager::onRemoveNextHopSuccess, this, request,
744 parameters,
745 currentTransactionId,
746 shouldWaitToRespond),
747 bind(&RibManager::onRemoveNextHopError, this, _1, _2, request, currentTransactionId,
748 shouldWaitToRespond));
749 }
750 }
751
752 m_managedRib.clearFibUpdates();
753}
754
755void
756RibManager::sendUpdatesToFibAfterFaceDestroyEvent()
757{
758 ControlParameters parameters;
759 parameters.setOrigin(ndn::nfd::ROUTE_ORIGIN_STATIC);
760
761 sendUpdatesToFib(shared_ptr<const Interest>(), parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700762}
763
Vince Lehmancd16c832014-07-23 15:14:55 -0700764void
765RibManager::listEntries(const Interest& request)
766{
767 const Name& command = request.getName();
768 const size_t commandNComps = command.size();
769
770 if (commandNComps < LIST_COMMAND_NCOMPS ||
771 !LIST_COMMAND_PREFIX.isPrefixOf(command))
772 {
773 NFD_LOG_DEBUG("command result: malformed");
774 sendResponse(command, 400, "Malformed command");
775 return;
776 }
777
778 m_ribStatusPublisher.publish();
779}
780
Vince Lehmancd613c52014-07-30 14:34:49 -0500781void
Vince Lehman26b215c2014-08-17 15:00:41 -0500782RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
783{
784 scheduler::cancel(m_activeFaceFetchEvent);
785
786 m_activeFaceFetchEvent = scheduler::schedule(timeToWait,
787 bind(&RibManager::fetchActiveFaces, this));
788}
789
790void
Vince Lehmancd613c52014-07-30 14:34:49 -0500791RibManager::fetchActiveFaces()
792{
793 NFD_LOG_DEBUG("Fetching active faces");
794
795 Interest interest(FACES_LIST_DATASET_PREFIX);
796 interest.setChildSelector(1);
797 interest.setMustBeFresh(true);
798
799 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
800
801 m_face.expressInterest(interest,
802 bind(&RibManager::fetchSegments, this, _2, buffer),
803 bind(&RibManager::onFetchFaceStatusTimeout, this));
804}
805
806void
807RibManager::fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer)
808{
809 buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
810 data.getContent().value_size());
811
812 uint64_t currentSegment = data.getName().get(-1).toSegment();
813
814 const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
815 if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment)
816 {
817 m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
818 bind(&RibManager::fetchSegments, this, _2, buffer),
819 bind(&RibManager::onFetchFaceStatusTimeout, this));
820 }
821 else
822 {
Vince Lehman26b215c2014-08-17 15:00:41 -0500823 removeInvalidFaces(buffer);
Vince Lehmancd613c52014-07-30 14:34:49 -0500824 }
825}
826
827void
Vince Lehman26b215c2014-08-17 15:00:41 -0500828RibManager::removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer)
Vince Lehmancd613c52014-07-30 14:34:49 -0500829{
Vince Lehman26b215c2014-08-17 15:00:41 -0500830 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500831
832 ndn::ConstBufferPtr buf = buffer->buf();
833
834 Block block;
835 size_t offset = 0;
Vince Lehman26b215c2014-08-17 15:00:41 -0500836 FaceIdSet activeFaces;
Vince Lehmancd613c52014-07-30 14:34:49 -0500837
838 while (offset < buf->size())
839 {
840 if (!Block::fromBuffer(buf, offset, block))
841 {
842 std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
843 break;
844 }
845
846 offset += block.size();
847
848 ndn::nfd::FaceStatus status(block);
Vince Lehmancd613c52014-07-30 14:34:49 -0500849 activeFaces.insert(status.getFaceId());
850 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500851
852 // Look for face IDs that were registered but not active to find missed
853 // face destroyed events
854 for (FaceIdSet::iterator it = m_registeredFaces.begin(); it != m_registeredFaces.end(); ++it)
855 {
856 if (activeFaces.find(*it) == activeFaces.end())
857 {
858 NFD_LOG_DEBUG("Removing invalid face ID: " << *it);
859 scheduler::schedule(time::seconds(0),
860 bind(&RibManager::processErasureAfterNotification, this, *it));
861 }
862 }
863
864 // Reschedule the check for future clean up
865 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500866}
867
868void
869RibManager::onFetchFaceStatusTimeout()
870{
871 std::cerr << "Face Status Dataset request timed out" << std::endl;
Vince Lehman26b215c2014-08-17 15:00:41 -0500872 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500873}
874
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700875} // namespace rib
876} // namespace nfd