blob: d4be75daa5228171cc930d6eeedac451a2fb2f8c [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince12e49462014-06-09 13:29:32 -05003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Vince12e49462014-06-09 13:29:32 -050024 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070025
26#include "rib-manager.hpp"
Alexander Afanasyev03ea3eb2014-04-17 18:19:06 -070027#include "core/global-io.hpp"
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070028#include "core/logger.hpp"
Alexander Afanasyev63108c42014-07-07 19:10:47 -070029#include "core/scheduler.hpp"
Vince Lehmancd613c52014-07-30 14:34:49 -050030#include <ndn-cxx/management/nfd-face-status.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070031
32namespace nfd {
33namespace rib {
34
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070035NFD_LOG_INIT("RibManager");
36
Alexander Afanasyev20d31442014-04-19 17:00:53 -070037const Name RibManager::COMMAND_PREFIX = "/localhost/nfd/rib";
38const Name RibManager::REMOTE_COMMAND_PREFIX = "/localhop/nfd/rib";
Vince Lehmancd613c52014-07-30 14:34:49 -050039const Name RibManager::FACES_LIST_DATASET_PREFIX = "/localhost/nfd/faces/list";
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070040
41const size_t RibManager::COMMAND_UNSIGNED_NCOMPS =
42 RibManager::COMMAND_PREFIX.size() +
43 1 + // verb
44 1; // verb options
45
46const size_t RibManager::COMMAND_SIGNED_NCOMPS =
47 RibManager::COMMAND_UNSIGNED_NCOMPS +
48 4; // (timestamp, nonce, signed info tlv, signature tlv)
49
Vince Lehmancd16c832014-07-23 15:14:55 -070050const RibManager::SignedVerbAndProcessor RibManager::SIGNED_COMMAND_VERBS[] =
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070051 {
Vince Lehmancd16c832014-07-23 15:14:55 -070052 SignedVerbAndProcessor(
53 Name::Component("register"),
54 &RibManager::registerEntry
55 ),
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070056
Vince Lehmancd16c832014-07-23 15:14:55 -070057 SignedVerbAndProcessor(
58 Name::Component("unregister"),
59 &RibManager::unregisterEntry
60 ),
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070061 };
62
Vince Lehmancd16c832014-07-23 15:14:55 -070063const RibManager::UnsignedVerbAndProcessor RibManager::UNSIGNED_COMMAND_VERBS[] =
64 {
65 UnsignedVerbAndProcessor(
66 Name::Component("list"),
67 &RibManager::listEntries
68 ),
69 };
70
71const Name RibManager::LIST_COMMAND_PREFIX("/localhost/nfd/rib/list");
72const size_t RibManager::LIST_COMMAND_NCOMPS = LIST_COMMAND_PREFIX.size();
73
Vince Lehman72446ec2014-07-09 10:50:02 -050074RibManager::RibManager(ndn::Face& face)
75 : m_face(face)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070076 , m_nfdController(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070077 , m_localhostValidator(m_face)
78 , m_localhopValidator(m_face)
Yingdi Yuf4db0b52014-04-17 13:17:39 -070079 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070080 , m_isLocalhopEnabled(false)
Vince Lehmancd16c832014-07-23 15:14:55 -070081 , m_ribStatusPublisher(m_managedRib, face, LIST_COMMAND_PREFIX, m_keyChain)
Vince Lehman4387e782014-06-19 16:57:45 -050082 , m_lastTransactionId(0)
Vince Lehmancd16c832014-07-23 15:14:55 -070083 , m_signedVerbDispatch(SIGNED_COMMAND_VERBS,
84 SIGNED_COMMAND_VERBS +
85 (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor)))
86 , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS,
87 UNSIGNED_COMMAND_VERBS +
88 (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor)))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070089{
90}
91
92void
Junxiao Shia3295742014-05-16 22:40:10 -070093RibManager::startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest)
94{
95 NFD_LOG_INFO("Listening on: " << commandPrefix);
96
97 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
98 ControlParameters()
99 .setName(commandPrefix)
100 .setFaceId(0),
101 bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(commandPrefix)),
102 bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(commandPrefix), _2));
103
104 m_face.setInterestFilter(commandPrefix, onRequest);
105}
106
107void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700108RibManager::registerWithNfd()
109{
110 //check whether the components of localhop and localhost prefixes are same
111 BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size());
112
Junxiao Shia3295742014-05-16 22:40:10 -0700113 this->startListening(COMMAND_PREFIX, bind(&RibManager::onLocalhostRequest, this, _2));
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700114
Junxiao Shia3295742014-05-16 22:40:10 -0700115 if (m_isLocalhopEnabled) {
116 this->startListening(REMOTE_COMMAND_PREFIX,
117 bind(&RibManager::onLocalhopRequest, this, _2));
118 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700119
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700120 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shi15b12e72014-08-09 19:56:24 -0700121 m_faceMonitor.onNotification += bind(&RibManager::onNotification, this, _1);
122 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500123
124 fetchActiveFaces();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700125}
126
127void
128RibManager::setConfigFile(ConfigFile& configFile)
129{
Yingdi Yue5224e92014-04-29 18:04:02 -0700130 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700131 bind(&RibManager::onConfig, this, _1, _2, _3));
132}
133
134void
135RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700136 bool isDryRun,
137 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700138{
Yingdi Yue5224e92014-04-29 18:04:02 -0700139 for (ConfigSection::const_iterator i = configSection.begin();
140 i != configSection.end(); ++i)
141 {
142 if (i->first == "localhost_security")
143 m_localhostValidator.load(i->second, filename);
144 else if (i->first == "localhop_security")
145 {
146 m_localhopValidator.load(i->second, filename);
147 m_isLocalhopEnabled = true;
148 }
149 else
150 throw Error("Unrecognized rib property: " + i->first);
151 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700152}
153
154void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700155RibManager::sendResponse(const Name& name,
156 const ControlResponse& response)
157{
158 const Block& encodedControl = response.wireEncode();
159
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700160 shared_ptr<Data> responseData = make_shared<Data>(name);
161 responseData->setContent(encodedControl);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700162
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700163 m_keyChain.sign(*responseData);
164 m_face.put(*responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700165}
166
167void
168RibManager::sendResponse(const Name& name,
169 uint32_t code,
170 const std::string& text)
171{
172 ControlResponse response(code, text);
173 sendResponse(name, response);
174}
175
176void
Yingdi Yue5224e92014-04-29 18:04:02 -0700177RibManager::onLocalhostRequest(const Interest& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700178{
Vince Lehmancd16c832014-07-23 15:14:55 -0700179 const Name& command = request.getName();
180 const Name::Component& verb = command.get(COMMAND_PREFIX.size());
181
182 UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
183
184 if (unsignedVerbProcessor != m_unsignedVerbDispatch.end())
185 {
186 NFD_LOG_DEBUG("command result: processing unsigned verb: " << verb);
187 (unsignedVerbProcessor->second)(this, request);
188 }
189 else
190 {
191 m_localhostValidator.validate(request,
192 bind(&RibManager::onCommandValidated, this, _1),
193 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
194 }
Yingdi Yue5224e92014-04-29 18:04:02 -0700195}
196
197void
198RibManager::onLocalhopRequest(const Interest& request)
199{
200 m_localhopValidator.validate(request,
201 bind(&RibManager::onCommandValidated, this, _1),
202 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700203}
204
205void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700206RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700207{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700208 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700209 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700210
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700211 const Name& command = request->getName();
212 const Name::Component& verb = command[COMMAND_PREFIX.size()];
213 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
214
Vince Lehmancd16c832014-07-23 15:14:55 -0700215 SignedVerbDispatchTable::const_iterator verbProcessor = m_signedVerbDispatch.find(verb);
216 if (verbProcessor != m_signedVerbDispatch.end())
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700217 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700218 ControlParameters parameters;
219 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700220 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700221 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500222 if (static_cast<bool>(request))
223 sendResponse(command, 400, "Malformed command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700224 return;
225 }
226
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700227 NFD_LOG_DEBUG("command result: processing verb: " << verb);
228 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700229 }
230 else
231 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700232 NFD_LOG_DEBUG("Unsupported command: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500233 if (static_cast<bool>(request))
234 sendResponse(request->getName(), 501, "Unsupported command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700235 }
236}
237
238void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700239RibManager::registerEntry(const shared_ptr<const Interest>& request,
240 ControlParameters& parameters)
241{
242 ndn::nfd::RibRegisterCommand command;
243
244 if (!validateParameters(command, parameters))
245 {
246 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
Vince Lehmancd613c52014-07-30 14:34:49 -0500247
Syed Obaid3313a372014-07-01 01:31:33 -0500248 if (static_cast<bool>(request))
Vince Lehmancd613c52014-07-30 14:34:49 -0500249 {
250 sendResponse(request->getName(), 400, "Malformed command");
251 }
252
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700253 return;
254 }
255
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700256 if (!parameters.hasFaceId() || parameters.getFaceId() == 0)
257 {
258 parameters.setFaceId(request->getIncomingFaceId());
259 }
260
Vince Lehmancd613c52014-07-30 14:34:49 -0500261 // Is the face valid?
262 if (activeFaces.find(parameters.getFaceId()) == activeFaces.end())
263 {
264 NFD_LOG_DEBUG("register result: FAIL reason: unknown faceId");
265
266 if (static_cast<bool>(request))
267 {
268 sendResponse(request->getName(), 410, "Face not found");
269 }
270
271 return;
272 }
273
Vince12e49462014-06-09 13:29:32 -0500274 FaceEntry faceEntry;
275 faceEntry.faceId = parameters.getFaceId();
276 faceEntry.origin = parameters.getOrigin();
277 faceEntry.cost = parameters.getCost();
278 faceEntry.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500279
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700280 if (parameters.hasExpirationPeriod() &&
281 parameters.getExpirationPeriod() != time::milliseconds::max())
Syed Obaid3313a372014-07-01 01:31:33 -0500282 {
283 faceEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
284
285 // Schedule a new event, the old one will be cancelled during rib insertion.
286 EventId eventId;
287 NFD_LOG_TRACE("scheduling unregistration at: " << faceEntry.expires);
288 eventId = scheduler::schedule(parameters.getExpirationPeriod(),
289 bind(&RibManager::unregisterEntry,
290 this, shared_ptr<Interest>(), parameters));
291
292 //set the NewEventId of this entry
293 faceEntry.setExpirationEvent(eventId);
294 }
295 else
296 {
297 faceEntry.expires = time::steady_clock::TimePoint::max();
298 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700299
Vince12e49462014-06-09 13:29:32 -0500300 NFD_LOG_TRACE("register prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700301
Vince12e49462014-06-09 13:29:32 -0500302 m_managedRib.insert(parameters.getName(), faceEntry);
Vince Lehman4387e782014-06-19 16:57:45 -0500303
304 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700305}
306
307void
308RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
Syed Obaid3313a372014-07-01 01:31:33 -0500309 ControlParameters& params)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700310{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700311 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700312
Syed Obaid3313a372014-07-01 01:31:33 -0500313 //passing all parameters gives error in validation.
314 //so passing only the required arguments.
315 ControlParameters parameters;
316 parameters.setName(params.getName());
Alexander Afanasyevb609f002014-07-18 16:56:34 -0700317 if (params.hasFaceId())
318 parameters.setFaceId(params.getFaceId());
319 if (params.hasOrigin())
320 parameters.setOrigin(params.getOrigin());
Syed Obaid3313a372014-07-01 01:31:33 -0500321
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700322 if (!validateParameters(command, parameters))
323 {
Syed Obaid3313a372014-07-01 01:31:33 -0500324 NFD_LOG_DEBUG("unregister result: FAIL reason: malformed");
325 if (static_cast<bool>(request))
326 sendResponse(request->getName(), 400, "Malformed command");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700327 return;
328 }
329
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700330 if (!parameters.hasFaceId() || parameters.getFaceId() == 0)
331 {
332 parameters.setFaceId(request->getIncomingFaceId());
333 }
334
Vince Lehmancd613c52014-07-30 14:34:49 -0500335 // Is the face valid?
336 if (activeFaces.find(parameters.getFaceId()) == activeFaces.end())
337 {
338 NFD_LOG_DEBUG("register result: FAIL reason: unknown faceId");
339
340 if (static_cast<bool>(request))
341 {
342 sendResponse(request->getName(), 410, "Face not found");
343 }
344
345 return;
346 }
347
Vince12e49462014-06-09 13:29:32 -0500348 FaceEntry faceEntry;
349 faceEntry.faceId = parameters.getFaceId();
350 faceEntry.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700351
Vince12e49462014-06-09 13:29:32 -0500352 NFD_LOG_TRACE("unregister prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700353
Vince Lehman4387e782014-06-19 16:57:45 -0500354 m_managedRib.erase(parameters.getName(), faceEntry);
355
356 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700357}
358
359void
360RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
361 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700362{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700363 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Syed Obaid3313a372014-07-01 01:31:33 -0500364 if (static_cast<bool>(request))
365 sendResponse(request->getName(), 403, failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700366}
367
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700368
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700369bool
370RibManager::extractParameters(const Name::Component& parameterComponent,
371 ControlParameters& extractedParameters)
372{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700373 try
374 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700375 Block rawParameters = parameterComponent.blockFromValue();
376 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700377 }
378 catch (const ndn::Tlv::Error& e)
379 {
380 return false;
381 }
382
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700383 NFD_LOG_DEBUG("Parameters parsed OK");
384 return true;
385}
386
387bool
388RibManager::validateParameters(const ControlCommand& command,
389 ControlParameters& parameters)
390{
391 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700392 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700393 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700394 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700395 catch (const ControlCommand::ArgumentError&)
396 {
397 return false;
398 }
399
400 command.applyDefaultsToRequest(parameters);
401
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700402 return true;
403}
404
405void
406RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700407 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500408 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700409{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700410 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700411
412 ControlResponse response;
413
414 if (code == 404)
415 {
416 response.setCode(code);
417 response.setText(error);
418 }
419 else
420 {
421 response.setCode(533);
422 std::ostringstream os;
423 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
424 response.setText(os.str());
425 }
426
Syed Obaid3313a372014-07-01 01:31:33 -0500427 if (static_cast<bool>(request))
428 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700429}
430
431void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700432RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
433 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500434 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700435{
436 ControlResponse response;
437
438 response.setCode(200);
439 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700440 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700441
Vince12e49462014-06-09 13:29:32 -0500442 NFD_LOG_TRACE("onRegSuccess: registered " << faceEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700443
Syed Obaid3313a372014-07-01 01:31:33 -0500444 if (static_cast<bool>(request))
445 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700446}
447
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700448
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700449void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700450RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
451 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500452 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700453{
454 ControlResponse response;
455
456 response.setCode(200);
457 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700458 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700459
Vince12e49462014-06-09 13:29:32 -0500460 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700461
Syed Obaid3313a372014-07-01 01:31:33 -0500462 if (static_cast<bool>(request))
463 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500464}
465
466void
467RibManager::sendSuccessResponse(const shared_ptr<const Interest>& request,
468 const ControlParameters& parameters)
469{
470 if (!static_cast<bool>(request))
471 {
472 return;
473 }
474
475 ControlResponse response;
476
477 response.setCode(200);
478 response.setText("Success");
479 response.setBody(parameters.wireEncode());
480
Syed Obaid3313a372014-07-01 01:31:33 -0500481 if (static_cast<bool>(request))
482 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500483}
484
485void
486RibManager::sendErrorResponse(uint32_t code, const std::string& error,
487 const shared_ptr<const Interest>& request)
488{
489 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
490
491 if (!static_cast<bool>(request))
492 {
493 return;
494 }
495
496 ControlResponse response;
497
498 if (code == 404)
499 {
500 response.setCode(code);
501 response.setText(error);
502 }
503 else
504 {
505 response.setCode(533);
506 std::ostringstream os;
507 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
508 response.setText(os.str());
509 }
510
Syed Obaid3313a372014-07-01 01:31:33 -0500511 if (static_cast<bool>(request))
512 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700513}
514
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700515void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700516RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix)
517{
518 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
519}
520
521void
522RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg)
523{
524 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
525}
526
Vince Lehman4387e782014-06-19 16:57:45 -0500527bool
528RibManager::isTransactionComplete(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700529{
Vince Lehman4387e782014-06-19 16:57:45 -0500530 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
531
532 if (it != m_pendingFibTransactions.end())
533 {
534 int& updatesLeft = it->second;
535
536 updatesLeft--;
537
538 // All of the updates have been applied successfully
539 if (updatesLeft == 0)
540 {
541 m_pendingFibTransactions.erase(it);
542 return true;
543 }
544 }
545
546 return false;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700547}
548
549void
Vince Lehman4387e782014-06-19 16:57:45 -0500550RibManager::invalidateTransaction(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700551{
Vince Lehman4387e782014-06-19 16:57:45 -0500552 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
553
554 if (it != m_pendingFibTransactions.end())
555 {
556 m_pendingFibTransactions.erase(it);
557 }
558}
559
560void
561RibManager::onAddNextHopSuccess(const shared_ptr<const Interest>& request,
562 const ControlParameters& parameters,
563 const TransactionId transactionId,
564 const bool shouldSendResponse)
565{
566 if (isTransactionComplete(transactionId) && shouldSendResponse)
567 {
568 sendSuccessResponse(request, parameters);
569 }
570}
571
572void
573RibManager::onAddNextHopError(uint32_t code, const std::string& error,
574 const shared_ptr<const Interest>& request,
575 const TransactionId transactionId, const bool shouldSendResponse)
576{
577 invalidateTransaction(transactionId);
578
579 if (shouldSendResponse)
580 {
581 sendErrorResponse(code, error, request);
582 }
583}
584
585void
586RibManager::onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
587 const ControlParameters& parameters,
588 const TransactionId transactionId,
589 const bool shouldSendResponse)
590{
591 if (isTransactionComplete(transactionId) && shouldSendResponse)
592 {
593 sendSuccessResponse(request, parameters);
594 }
595}
596
597void
598RibManager::onRemoveNextHopError(uint32_t code, const std::string& error,
599 const shared_ptr<const Interest>& request,
600 const TransactionId transactionId, const bool shouldSendResponse)
601{
602 invalidateTransaction(transactionId);
603
604 if (shouldSendResponse)
605 {
606 sendErrorResponse(code, error, request);
607 }
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700608}
609
610void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700611RibManager::onControlHeaderSuccess()
612{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700613 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700614}
615
616void
617RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
618{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700619 std::ostringstream os;
620 os << "Couldn't enable local control header "
621 << "(code: " << code << ", info: " << reason << ")";
622 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700623}
624
625void
626RibManager::enableLocalControlHeader()
627{
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700628 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700629 ControlParameters()
630 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
631 bind(&RibManager::onControlHeaderSuccess, this),
632 bind(&RibManager::onControlHeaderError, this, _1, _2));
633}
634
635void
636RibManager::onNotification(const FaceEventNotification& notification)
637{
638 /// \todo A notification can be missed, in this case check Facelist
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700639 NFD_LOG_TRACE("onNotification: " << notification);
Vince Lehmancd613c52014-07-30 14:34:49 -0500640
641 if (notification.getKind() == ndn::nfd::FACE_EVENT_CREATED)
Vince Lehman4387e782014-06-19 16:57:45 -0500642 {
Vince Lehmancd613c52014-07-30 14:34:49 -0500643 NFD_LOG_DEBUG("Received notification for created faceId: " << notification.getFaceId());
644 activeFaces.insert(notification.getFaceId());
645 }
646 else if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED)
647 {
648 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
649 activeFaces.erase(notification.getFaceId());
650
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700651 scheduler::schedule(time::seconds(0),
652 bind(&RibManager::processErasureAfterNotification, this,
653 notification.getFaceId()));
Vince Lehman4387e782014-06-19 16:57:45 -0500654 }
655}
656
657void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700658RibManager::processErasureAfterNotification(uint64_t faceId)
659{
660 m_managedRib.erase(faceId);
661
662 sendUpdatesToFibAfterFaceDestroyEvent();
663}
664
665void
Vince Lehman4387e782014-06-19 16:57:45 -0500666RibManager::sendUpdatesToFib(const shared_ptr<const Interest>& request,
667 const ControlParameters& parameters)
668{
669 const Rib::FibUpdateList& updates = m_managedRib.getFibUpdates();
670
671 // If no updates were generated, consider the operation a success
672 if (updates.empty())
673 {
674 sendSuccessResponse(request, parameters);
675 return;
676 }
677
678 bool shouldWaitToRespond = false;
679
680 // An application request should wait for all FIB updates to be applied
681 // successfully before sending a response
682 if (parameters.getOrigin() == ndn::nfd::ROUTE_ORIGIN_APP)
683 {
684 shouldWaitToRespond = true;
685 }
686 else // Respond immediately
687 {
688 sendSuccessResponse(request, parameters);
689 }
690
691 NFD_LOG_DEBUG("Applying " << updates.size() << " updates to FIB");
692
693 // Assign an ID to this FIB transaction
694 TransactionId currentTransactionId = ++m_lastTransactionId;
695
696 // Add this transaction to the transaction table
697 m_pendingFibTransactions[currentTransactionId] = updates.size();
698
699 for (Rib::FibUpdateList::const_iterator it = updates.begin(); it != updates.end(); ++it)
700 {
701 shared_ptr<const FibUpdate> update(*it);
702
703 if (update->action == FibUpdate::ADD_NEXTHOP)
704 {
705 FaceEntry faceEntry;
706 faceEntry.faceId = update->faceId;
707 faceEntry.cost = update->cost;
708
709 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
710 ControlParameters()
711 .setName(update->name)
712 .setFaceId(faceEntry.faceId)
713 .setCost(faceEntry.cost),
714 bind(&RibManager::onAddNextHopSuccess, this, request,
715 parameters,
716 currentTransactionId,
717 shouldWaitToRespond),
718 bind(&RibManager::onAddNextHopError, this, _1, _2, request, currentTransactionId,
719 shouldWaitToRespond));
720 }
721 else if (update->action == FibUpdate::REMOVE_NEXTHOP)
722 {
723 FaceEntry faceEntry;
724 faceEntry.faceId = update->faceId;
725
726 m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>(
727 ControlParameters()
728 .setName(update->name)
729 .setFaceId(faceEntry.faceId),
730 bind(&RibManager::onRemoveNextHopSuccess, this, request,
731 parameters,
732 currentTransactionId,
733 shouldWaitToRespond),
734 bind(&RibManager::onRemoveNextHopError, this, _1, _2, request, currentTransactionId,
735 shouldWaitToRespond));
736 }
737 }
738
739 m_managedRib.clearFibUpdates();
740}
741
742void
743RibManager::sendUpdatesToFibAfterFaceDestroyEvent()
744{
745 ControlParameters parameters;
746 parameters.setOrigin(ndn::nfd::ROUTE_ORIGIN_STATIC);
747
748 sendUpdatesToFib(shared_ptr<const Interest>(), parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700749}
750
Vince Lehmancd16c832014-07-23 15:14:55 -0700751void
752RibManager::listEntries(const Interest& request)
753{
754 const Name& command = request.getName();
755 const size_t commandNComps = command.size();
756
757 if (commandNComps < LIST_COMMAND_NCOMPS ||
758 !LIST_COMMAND_PREFIX.isPrefixOf(command))
759 {
760 NFD_LOG_DEBUG("command result: malformed");
761 sendResponse(command, 400, "Malformed command");
762 return;
763 }
764
765 m_ribStatusPublisher.publish();
766}
767
Vince Lehmancd613c52014-07-30 14:34:49 -0500768void
769RibManager::fetchActiveFaces()
770{
771 NFD_LOG_DEBUG("Fetching active faces");
772
773 Interest interest(FACES_LIST_DATASET_PREFIX);
774 interest.setChildSelector(1);
775 interest.setMustBeFresh(true);
776
777 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
778
779 m_face.expressInterest(interest,
780 bind(&RibManager::fetchSegments, this, _2, buffer),
781 bind(&RibManager::onFetchFaceStatusTimeout, this));
782}
783
784void
785RibManager::fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer)
786{
787 buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
788 data.getContent().value_size());
789
790 uint64_t currentSegment = data.getName().get(-1).toSegment();
791
792 const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
793 if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment)
794 {
795 m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
796 bind(&RibManager::fetchSegments, this, _2, buffer),
797 bind(&RibManager::onFetchFaceStatusTimeout, this));
798 }
799 else
800 {
801 updateActiveFaces(buffer);
802 }
803}
804
805void
806RibManager::updateActiveFaces(shared_ptr<ndn::OBufferStream> buffer)
807{
808 NFD_LOG_DEBUG("Updating active faces");
809
810 ndn::ConstBufferPtr buf = buffer->buf();
811
812 Block block;
813 size_t offset = 0;
814
815 while (offset < buf->size())
816 {
817 if (!Block::fromBuffer(buf, offset, block))
818 {
819 std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
820 break;
821 }
822
823 offset += block.size();
824
825 ndn::nfd::FaceStatus status(block);
826
827 NFD_LOG_DEBUG("Adding faceId: " << status.getFaceId() << " to activeFaces");
828 activeFaces.insert(status.getFaceId());
829 }
830}
831
832void
833RibManager::onFetchFaceStatusTimeout()
834{
835 std::cerr << "Face Status Dataset request timed out" << std::endl;
836}
837
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700838} // namespace rib
839} // namespace nfd