blob: d2e80c3f790dd22c55bc118bb18e7b3e7a348b6d [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"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070030
31namespace nfd {
32namespace rib {
33
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070034NFD_LOG_INIT("RibManager");
35
Alexander Afanasyev20d31442014-04-19 17:00:53 -070036const Name RibManager::COMMAND_PREFIX = "/localhost/nfd/rib";
37const Name RibManager::REMOTE_COMMAND_PREFIX = "/localhop/nfd/rib";
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070038
39const size_t RibManager::COMMAND_UNSIGNED_NCOMPS =
40 RibManager::COMMAND_PREFIX.size() +
41 1 + // verb
42 1; // verb options
43
44const size_t RibManager::COMMAND_SIGNED_NCOMPS =
45 RibManager::COMMAND_UNSIGNED_NCOMPS +
46 4; // (timestamp, nonce, signed info tlv, signature tlv)
47
Vince Lehmancd16c832014-07-23 15:14:55 -070048const RibManager::SignedVerbAndProcessor RibManager::SIGNED_COMMAND_VERBS[] =
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070049 {
Vince Lehmancd16c832014-07-23 15:14:55 -070050 SignedVerbAndProcessor(
51 Name::Component("register"),
52 &RibManager::registerEntry
53 ),
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070054
Vince Lehmancd16c832014-07-23 15:14:55 -070055 SignedVerbAndProcessor(
56 Name::Component("unregister"),
57 &RibManager::unregisterEntry
58 ),
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070059 };
60
Vince Lehmancd16c832014-07-23 15:14:55 -070061const RibManager::UnsignedVerbAndProcessor RibManager::UNSIGNED_COMMAND_VERBS[] =
62 {
63 UnsignedVerbAndProcessor(
64 Name::Component("list"),
65 &RibManager::listEntries
66 ),
67 };
68
69const Name RibManager::LIST_COMMAND_PREFIX("/localhost/nfd/rib/list");
70const size_t RibManager::LIST_COMMAND_NCOMPS = LIST_COMMAND_PREFIX.size();
71
Vince Lehman72446ec2014-07-09 10:50:02 -050072RibManager::RibManager(ndn::Face& face)
73 : m_face(face)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070074 , m_nfdController(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070075 , m_localhostValidator(m_face)
76 , m_localhopValidator(m_face)
Yingdi Yuf4db0b52014-04-17 13:17:39 -070077 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070078 , m_isLocalhopEnabled(false)
Vince Lehmancd16c832014-07-23 15:14:55 -070079 , m_ribStatusPublisher(m_managedRib, face, LIST_COMMAND_PREFIX, m_keyChain)
Vince Lehman4387e782014-06-19 16:57:45 -050080 , m_lastTransactionId(0)
Vince Lehmancd16c832014-07-23 15:14:55 -070081 , m_signedVerbDispatch(SIGNED_COMMAND_VERBS,
82 SIGNED_COMMAND_VERBS +
83 (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor)))
84 , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS,
85 UNSIGNED_COMMAND_VERBS +
86 (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor)))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070087{
88}
89
90void
Junxiao Shia3295742014-05-16 22:40:10 -070091RibManager::startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest)
92{
93 NFD_LOG_INFO("Listening on: " << commandPrefix);
94
95 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
96 ControlParameters()
97 .setName(commandPrefix)
98 .setFaceId(0),
99 bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(commandPrefix)),
100 bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(commandPrefix), _2));
101
102 m_face.setInterestFilter(commandPrefix, onRequest);
103}
104
105void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700106RibManager::registerWithNfd()
107{
108 //check whether the components of localhop and localhost prefixes are same
109 BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size());
110
Junxiao Shia3295742014-05-16 22:40:10 -0700111 this->startListening(COMMAND_PREFIX, bind(&RibManager::onLocalhostRequest, this, _2));
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700112
Junxiao Shia3295742014-05-16 22:40:10 -0700113 if (m_isLocalhopEnabled) {
114 this->startListening(REMOTE_COMMAND_PREFIX,
115 bind(&RibManager::onLocalhopRequest, this, _2));
116 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700117
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700118 NFD_LOG_INFO("Start monitoring face create/destroy events");
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700119 m_faceMonitor.addSubscriber(bind(&RibManager::onNotification, this, _1));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700120 m_faceMonitor.startNotifications();
121}
122
123void
124RibManager::setConfigFile(ConfigFile& configFile)
125{
Yingdi Yue5224e92014-04-29 18:04:02 -0700126 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700127 bind(&RibManager::onConfig, this, _1, _2, _3));
128}
129
130void
131RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700132 bool isDryRun,
133 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700134{
Yingdi Yue5224e92014-04-29 18:04:02 -0700135 for (ConfigSection::const_iterator i = configSection.begin();
136 i != configSection.end(); ++i)
137 {
138 if (i->first == "localhost_security")
139 m_localhostValidator.load(i->second, filename);
140 else if (i->first == "localhop_security")
141 {
142 m_localhopValidator.load(i->second, filename);
143 m_isLocalhopEnabled = true;
144 }
145 else
146 throw Error("Unrecognized rib property: " + i->first);
147 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700148}
149
150void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700151RibManager::sendResponse(const Name& name,
152 const ControlResponse& response)
153{
154 const Block& encodedControl = response.wireEncode();
155
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700156 shared_ptr<Data> responseData = make_shared<Data>(name);
157 responseData->setContent(encodedControl);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700158
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700159 m_keyChain.sign(*responseData);
160 m_face.put(*responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700161}
162
163void
164RibManager::sendResponse(const Name& name,
165 uint32_t code,
166 const std::string& text)
167{
168 ControlResponse response(code, text);
169 sendResponse(name, response);
170}
171
172void
Yingdi Yue5224e92014-04-29 18:04:02 -0700173RibManager::onLocalhostRequest(const Interest& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700174{
Vince Lehmancd16c832014-07-23 15:14:55 -0700175 const Name& command = request.getName();
176 const Name::Component& verb = command.get(COMMAND_PREFIX.size());
177
178 UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
179
180 if (unsignedVerbProcessor != m_unsignedVerbDispatch.end())
181 {
182 NFD_LOG_DEBUG("command result: processing unsigned verb: " << verb);
183 (unsignedVerbProcessor->second)(this, request);
184 }
185 else
186 {
187 m_localhostValidator.validate(request,
188 bind(&RibManager::onCommandValidated, this, _1),
189 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
190 }
Yingdi Yue5224e92014-04-29 18:04:02 -0700191}
192
193void
194RibManager::onLocalhopRequest(const Interest& request)
195{
196 m_localhopValidator.validate(request,
197 bind(&RibManager::onCommandValidated, this, _1),
198 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700199}
200
201void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700202RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700203{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700204 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700205 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700206
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700207 const Name& command = request->getName();
208 const Name::Component& verb = command[COMMAND_PREFIX.size()];
209 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
210
Vince Lehmancd16c832014-07-23 15:14:55 -0700211 SignedVerbDispatchTable::const_iterator verbProcessor = m_signedVerbDispatch.find(verb);
212 if (verbProcessor != m_signedVerbDispatch.end())
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700213 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700214 ControlParameters parameters;
215 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700216 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700217 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500218 if (static_cast<bool>(request))
219 sendResponse(command, 400, "Malformed command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700220 return;
221 }
222
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700223 if (!parameters.hasFaceId() || parameters.getFaceId() == 0)
224 {
225 parameters.setFaceId(request->getIncomingFaceId());
226 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700227
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700228 NFD_LOG_DEBUG("command result: processing verb: " << verb);
229 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700230 }
231 else
232 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700233 NFD_LOG_DEBUG("Unsupported command: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500234 if (static_cast<bool>(request))
235 sendResponse(request->getName(), 501, "Unsupported command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700236 }
237}
238
239void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700240RibManager::registerEntry(const shared_ptr<const Interest>& request,
241 ControlParameters& parameters)
242{
243 ndn::nfd::RibRegisterCommand command;
244
245 if (!validateParameters(command, parameters))
246 {
247 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
Syed Obaid3313a372014-07-01 01:31:33 -0500248 if (static_cast<bool>(request))
249 sendResponse(request->getName(), 400, "Malformed command");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700250 return;
251 }
252
Vince12e49462014-06-09 13:29:32 -0500253 FaceEntry faceEntry;
254 faceEntry.faceId = parameters.getFaceId();
255 faceEntry.origin = parameters.getOrigin();
256 faceEntry.cost = parameters.getCost();
257 faceEntry.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500258
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700259 if (parameters.hasExpirationPeriod() &&
260 parameters.getExpirationPeriod() != time::milliseconds::max())
Syed Obaid3313a372014-07-01 01:31:33 -0500261 {
262 faceEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
263
264 // Schedule a new event, the old one will be cancelled during rib insertion.
265 EventId eventId;
266 NFD_LOG_TRACE("scheduling unregistration at: " << faceEntry.expires);
267 eventId = scheduler::schedule(parameters.getExpirationPeriod(),
268 bind(&RibManager::unregisterEntry,
269 this, shared_ptr<Interest>(), parameters));
270
271 //set the NewEventId of this entry
272 faceEntry.setExpirationEvent(eventId);
273 }
274 else
275 {
276 faceEntry.expires = time::steady_clock::TimePoint::max();
277 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700278
Vince12e49462014-06-09 13:29:32 -0500279 NFD_LOG_TRACE("register prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700280
Vince12e49462014-06-09 13:29:32 -0500281 m_managedRib.insert(parameters.getName(), faceEntry);
Vince Lehman4387e782014-06-19 16:57:45 -0500282
283 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700284}
285
286void
287RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
Syed Obaid3313a372014-07-01 01:31:33 -0500288 ControlParameters& params)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700289{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700290 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700291
Syed Obaid3313a372014-07-01 01:31:33 -0500292 //passing all parameters gives error in validation.
293 //so passing only the required arguments.
294 ControlParameters parameters;
295 parameters.setName(params.getName());
Alexander Afanasyev56356b22014-07-18 16:56:34 -0700296 if (params.hasFaceId())
297 parameters.setFaceId(params.getFaceId());
298 if (params.hasOrigin())
299 parameters.setOrigin(params.getOrigin());
Syed Obaid3313a372014-07-01 01:31:33 -0500300
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700301 if (!validateParameters(command, parameters))
302 {
Syed Obaid3313a372014-07-01 01:31:33 -0500303 NFD_LOG_DEBUG("unregister result: FAIL reason: malformed");
304 if (static_cast<bool>(request))
305 sendResponse(request->getName(), 400, "Malformed command");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700306 return;
307 }
308
Vince12e49462014-06-09 13:29:32 -0500309 FaceEntry faceEntry;
310 faceEntry.faceId = parameters.getFaceId();
311 faceEntry.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700312
Vince12e49462014-06-09 13:29:32 -0500313 NFD_LOG_TRACE("unregister prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700314
Vince Lehman4387e782014-06-19 16:57:45 -0500315 m_managedRib.erase(parameters.getName(), faceEntry);
316
317 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700318}
319
320void
321RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
322 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700323{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700324 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Syed Obaid3313a372014-07-01 01:31:33 -0500325 if (static_cast<bool>(request))
326 sendResponse(request->getName(), 403, failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700327}
328
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700329
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700330bool
331RibManager::extractParameters(const Name::Component& parameterComponent,
332 ControlParameters& extractedParameters)
333{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700334 try
335 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700336 Block rawParameters = parameterComponent.blockFromValue();
337 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700338 }
339 catch (const ndn::Tlv::Error& e)
340 {
341 return false;
342 }
343
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700344 NFD_LOG_DEBUG("Parameters parsed OK");
345 return true;
346}
347
348bool
349RibManager::validateParameters(const ControlCommand& command,
350 ControlParameters& parameters)
351{
352 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700353 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700354 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700355 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700356 catch (const ControlCommand::ArgumentError&)
357 {
358 return false;
359 }
360
361 command.applyDefaultsToRequest(parameters);
362
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700363 return true;
364}
365
366void
367RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700368 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500369 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700370{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700371 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700372
373 ControlResponse response;
374
375 if (code == 404)
376 {
377 response.setCode(code);
378 response.setText(error);
379 }
380 else
381 {
382 response.setCode(533);
383 std::ostringstream os;
384 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
385 response.setText(os.str());
386 }
387
Syed Obaid3313a372014-07-01 01:31:33 -0500388 if (static_cast<bool>(request))
389 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700390}
391
392void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700393RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
394 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500395 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700396{
397 ControlResponse response;
398
399 response.setCode(200);
400 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700401 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700402
Vince12e49462014-06-09 13:29:32 -0500403 NFD_LOG_TRACE("onRegSuccess: registered " << faceEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700404
Syed Obaid3313a372014-07-01 01:31:33 -0500405 if (static_cast<bool>(request))
406 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700407}
408
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700409
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700410void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700411RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
412 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500413 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700414{
415 ControlResponse response;
416
417 response.setCode(200);
418 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700419 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700420
Vince12e49462014-06-09 13:29:32 -0500421 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700422
Syed Obaid3313a372014-07-01 01:31:33 -0500423 if (static_cast<bool>(request))
424 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500425}
426
427void
428RibManager::sendSuccessResponse(const shared_ptr<const Interest>& request,
429 const ControlParameters& parameters)
430{
431 if (!static_cast<bool>(request))
432 {
433 return;
434 }
435
436 ControlResponse response;
437
438 response.setCode(200);
439 response.setText("Success");
440 response.setBody(parameters.wireEncode());
441
Syed Obaid3313a372014-07-01 01:31:33 -0500442 if (static_cast<bool>(request))
443 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500444}
445
446void
447RibManager::sendErrorResponse(uint32_t code, const std::string& error,
448 const shared_ptr<const Interest>& request)
449{
450 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
451
452 if (!static_cast<bool>(request))
453 {
454 return;
455 }
456
457 ControlResponse response;
458
459 if (code == 404)
460 {
461 response.setCode(code);
462 response.setText(error);
463 }
464 else
465 {
466 response.setCode(533);
467 std::ostringstream os;
468 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
469 response.setText(os.str());
470 }
471
Syed Obaid3313a372014-07-01 01:31:33 -0500472 if (static_cast<bool>(request))
473 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700474}
475
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700476void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700477RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix)
478{
479 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
480}
481
482void
483RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg)
484{
485 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
486}
487
Vince Lehman4387e782014-06-19 16:57:45 -0500488bool
489RibManager::isTransactionComplete(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700490{
Vince Lehman4387e782014-06-19 16:57:45 -0500491 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
492
493 if (it != m_pendingFibTransactions.end())
494 {
495 int& updatesLeft = it->second;
496
497 updatesLeft--;
498
499 // All of the updates have been applied successfully
500 if (updatesLeft == 0)
501 {
502 m_pendingFibTransactions.erase(it);
503 return true;
504 }
505 }
506
507 return false;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700508}
509
510void
Vince Lehman4387e782014-06-19 16:57:45 -0500511RibManager::invalidateTransaction(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700512{
Vince Lehman4387e782014-06-19 16:57:45 -0500513 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
514
515 if (it != m_pendingFibTransactions.end())
516 {
517 m_pendingFibTransactions.erase(it);
518 }
519}
520
521void
522RibManager::onAddNextHopSuccess(const shared_ptr<const Interest>& request,
523 const ControlParameters& parameters,
524 const TransactionId transactionId,
525 const bool shouldSendResponse)
526{
527 if (isTransactionComplete(transactionId) && shouldSendResponse)
528 {
529 sendSuccessResponse(request, parameters);
530 }
531}
532
533void
534RibManager::onAddNextHopError(uint32_t code, const std::string& error,
535 const shared_ptr<const Interest>& request,
536 const TransactionId transactionId, const bool shouldSendResponse)
537{
538 invalidateTransaction(transactionId);
539
540 if (shouldSendResponse)
541 {
542 sendErrorResponse(code, error, request);
543 }
544}
545
546void
547RibManager::onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
548 const ControlParameters& parameters,
549 const TransactionId transactionId,
550 const bool shouldSendResponse)
551{
552 if (isTransactionComplete(transactionId) && shouldSendResponse)
553 {
554 sendSuccessResponse(request, parameters);
555 }
556}
557
558void
559RibManager::onRemoveNextHopError(uint32_t code, const std::string& error,
560 const shared_ptr<const Interest>& request,
561 const TransactionId transactionId, const bool shouldSendResponse)
562{
563 invalidateTransaction(transactionId);
564
565 if (shouldSendResponse)
566 {
567 sendErrorResponse(code, error, request);
568 }
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700569}
570
571void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700572RibManager::onControlHeaderSuccess()
573{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700574 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700575}
576
577void
578RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
579{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700580 std::ostringstream os;
581 os << "Couldn't enable local control header "
582 << "(code: " << code << ", info: " << reason << ")";
583 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700584}
585
586void
587RibManager::enableLocalControlHeader()
588{
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700589 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700590 ControlParameters()
591 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
592 bind(&RibManager::onControlHeaderSuccess, this),
593 bind(&RibManager::onControlHeaderError, this, _1, _2));
594}
595
596void
597RibManager::onNotification(const FaceEventNotification& notification)
598{
599 /// \todo A notification can be missed, in this case check Facelist
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700600 NFD_LOG_TRACE("onNotification: " << notification);
Vince Lehman4387e782014-06-19 16:57:45 -0500601 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) //face destroyed
602 {
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700603 scheduler::schedule(time::seconds(0),
604 bind(&RibManager::processErasureAfterNotification, this,
605 notification.getFaceId()));
Vince Lehman4387e782014-06-19 16:57:45 -0500606 }
607}
608
609void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700610RibManager::processErasureAfterNotification(uint64_t faceId)
611{
612 m_managedRib.erase(faceId);
613
614 sendUpdatesToFibAfterFaceDestroyEvent();
615}
616
617void
Vince Lehman4387e782014-06-19 16:57:45 -0500618RibManager::sendUpdatesToFib(const shared_ptr<const Interest>& request,
619 const ControlParameters& parameters)
620{
621 const Rib::FibUpdateList& updates = m_managedRib.getFibUpdates();
622
623 // If no updates were generated, consider the operation a success
624 if (updates.empty())
625 {
626 sendSuccessResponse(request, parameters);
627 return;
628 }
629
630 bool shouldWaitToRespond = false;
631
632 // An application request should wait for all FIB updates to be applied
633 // successfully before sending a response
634 if (parameters.getOrigin() == ndn::nfd::ROUTE_ORIGIN_APP)
635 {
636 shouldWaitToRespond = true;
637 }
638 else // Respond immediately
639 {
640 sendSuccessResponse(request, parameters);
641 }
642
643 NFD_LOG_DEBUG("Applying " << updates.size() << " updates to FIB");
644
645 // Assign an ID to this FIB transaction
646 TransactionId currentTransactionId = ++m_lastTransactionId;
647
648 // Add this transaction to the transaction table
649 m_pendingFibTransactions[currentTransactionId] = updates.size();
650
651 for (Rib::FibUpdateList::const_iterator it = updates.begin(); it != updates.end(); ++it)
652 {
653 shared_ptr<const FibUpdate> update(*it);
654
655 if (update->action == FibUpdate::ADD_NEXTHOP)
656 {
657 FaceEntry faceEntry;
658 faceEntry.faceId = update->faceId;
659 faceEntry.cost = update->cost;
660
661 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
662 ControlParameters()
663 .setName(update->name)
664 .setFaceId(faceEntry.faceId)
665 .setCost(faceEntry.cost),
666 bind(&RibManager::onAddNextHopSuccess, this, request,
667 parameters,
668 currentTransactionId,
669 shouldWaitToRespond),
670 bind(&RibManager::onAddNextHopError, this, _1, _2, request, currentTransactionId,
671 shouldWaitToRespond));
672 }
673 else if (update->action == FibUpdate::REMOVE_NEXTHOP)
674 {
675 FaceEntry faceEntry;
676 faceEntry.faceId = update->faceId;
677
678 m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>(
679 ControlParameters()
680 .setName(update->name)
681 .setFaceId(faceEntry.faceId),
682 bind(&RibManager::onRemoveNextHopSuccess, this, request,
683 parameters,
684 currentTransactionId,
685 shouldWaitToRespond),
686 bind(&RibManager::onRemoveNextHopError, this, _1, _2, request, currentTransactionId,
687 shouldWaitToRespond));
688 }
689 }
690
691 m_managedRib.clearFibUpdates();
692}
693
694void
695RibManager::sendUpdatesToFibAfterFaceDestroyEvent()
696{
697 ControlParameters parameters;
698 parameters.setOrigin(ndn::nfd::ROUTE_ORIGIN_STATIC);
699
700 sendUpdatesToFib(shared_ptr<const Interest>(), parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700701}
702
Vince Lehmancd16c832014-07-23 15:14:55 -0700703void
704RibManager::listEntries(const Interest& request)
705{
706 const Name& command = request.getName();
707 const size_t commandNComps = command.size();
708
709 if (commandNComps < LIST_COMMAND_NCOMPS ||
710 !LIST_COMMAND_PREFIX.isPrefixOf(command))
711 {
712 NFD_LOG_DEBUG("command result: malformed");
713 sendResponse(command, 400, "Malformed command");
714 return;
715 }
716
717 m_ribStatusPublisher.publish();
718}
719
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700720} // namespace rib
721} // namespace nfd