blob: 28a1e3eb105ca6dbe9008adf515da2165bc24db7 [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
48const RibManager::VerbAndProcessor RibManager::COMMAND_VERBS[] =
49 {
50 VerbAndProcessor(
51 Name::Component("register"),
Alexander Afanasyev20d31442014-04-19 17:00:53 -070052 &RibManager::registerEntry
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070053 ),
54
55 VerbAndProcessor(
56 Name::Component("unregister"),
Alexander Afanasyev20d31442014-04-19 17:00:53 -070057 &RibManager::unregisterEntry
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070058 ),
59 };
60
Vince Lehman72446ec2014-07-09 10:50:02 -050061RibManager::RibManager(ndn::Face& face)
62 : m_face(face)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070063 , m_nfdController(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070064 , m_localhostValidator(m_face)
65 , m_localhopValidator(m_face)
Yingdi Yuf4db0b52014-04-17 13:17:39 -070066 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070067 , m_isLocalhopEnabled(false)
Vince Lehman4387e782014-06-19 16:57:45 -050068 , m_lastTransactionId(0)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070069 , m_verbDispatch(COMMAND_VERBS,
70 COMMAND_VERBS + (sizeof(COMMAND_VERBS) / sizeof(VerbAndProcessor)))
71{
72}
73
74void
Junxiao Shia3295742014-05-16 22:40:10 -070075RibManager::startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest)
76{
77 NFD_LOG_INFO("Listening on: " << commandPrefix);
78
79 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
80 ControlParameters()
81 .setName(commandPrefix)
82 .setFaceId(0),
83 bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(commandPrefix)),
84 bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(commandPrefix), _2));
85
86 m_face.setInterestFilter(commandPrefix, onRequest);
87}
88
89void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070090RibManager::registerWithNfd()
91{
92 //check whether the components of localhop and localhost prefixes are same
93 BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size());
94
Junxiao Shia3295742014-05-16 22:40:10 -070095 this->startListening(COMMAND_PREFIX, bind(&RibManager::onLocalhostRequest, this, _2));
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070096
Junxiao Shia3295742014-05-16 22:40:10 -070097 if (m_isLocalhopEnabled) {
98 this->startListening(REMOTE_COMMAND_PREFIX,
99 bind(&RibManager::onLocalhopRequest, this, _2));
100 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700101
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700102 NFD_LOG_INFO("Start monitoring face create/destroy events");
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700103 m_faceMonitor.addSubscriber(bind(&RibManager::onNotification, this, _1));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700104 m_faceMonitor.startNotifications();
105}
106
107void
108RibManager::setConfigFile(ConfigFile& configFile)
109{
Yingdi Yue5224e92014-04-29 18:04:02 -0700110 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700111 bind(&RibManager::onConfig, this, _1, _2, _3));
112}
113
114void
115RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700116 bool isDryRun,
117 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700118{
Yingdi Yue5224e92014-04-29 18:04:02 -0700119 for (ConfigSection::const_iterator i = configSection.begin();
120 i != configSection.end(); ++i)
121 {
122 if (i->first == "localhost_security")
123 m_localhostValidator.load(i->second, filename);
124 else if (i->first == "localhop_security")
125 {
126 m_localhopValidator.load(i->second, filename);
127 m_isLocalhopEnabled = true;
128 }
129 else
130 throw Error("Unrecognized rib property: " + i->first);
131 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700132}
133
134void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700135RibManager::sendResponse(const Name& name,
136 const ControlResponse& response)
137{
138 const Block& encodedControl = response.wireEncode();
139
140 Data responseData(name);
141 responseData.setContent(encodedControl);
142
143 m_keyChain.sign(responseData);
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700144 m_face.put(responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700145}
146
147void
148RibManager::sendResponse(const Name& name,
149 uint32_t code,
150 const std::string& text)
151{
152 ControlResponse response(code, text);
153 sendResponse(name, response);
154}
155
156void
Yingdi Yue5224e92014-04-29 18:04:02 -0700157RibManager::onLocalhostRequest(const Interest& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700158{
Yingdi Yue5224e92014-04-29 18:04:02 -0700159 m_localhostValidator.validate(request,
160 bind(&RibManager::onCommandValidated, this, _1),
161 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
162}
163
164void
165RibManager::onLocalhopRequest(const Interest& request)
166{
167 m_localhopValidator.validate(request,
168 bind(&RibManager::onCommandValidated, this, _1),
169 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700170}
171
172void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700173RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700174{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700175 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700176 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700177
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700178 const Name& command = request->getName();
179 const Name::Component& verb = command[COMMAND_PREFIX.size()];
180 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
181
182 VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700183 if (verbProcessor != m_verbDispatch.end())
184 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700185 ControlParameters parameters;
186 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700187 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700188 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500189 if (static_cast<bool>(request))
190 sendResponse(command, 400, "Malformed command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700191 return;
192 }
193
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700194 NFD_LOG_DEBUG("command result: processing verb: " << verb);
195 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700196 }
197 else
198 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700199 NFD_LOG_DEBUG("Unsupported command: " << verb);
Syed Obaid3313a372014-07-01 01:31:33 -0500200 if (static_cast<bool>(request))
201 sendResponse(request->getName(), 501, "Unsupported command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700202 }
203}
204
205void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700206RibManager::registerEntry(const shared_ptr<const Interest>& request,
207 ControlParameters& parameters)
208{
209 ndn::nfd::RibRegisterCommand command;
210
211 if (!validateParameters(command, parameters))
212 {
213 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
Syed Obaid3313a372014-07-01 01:31:33 -0500214 if (static_cast<bool>(request))
215 sendResponse(request->getName(), 400, "Malformed command");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700216 return;
217 }
218
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700219 if (!parameters.hasFaceId() || parameters.getFaceId() == 0)
220 {
221 parameters.setFaceId(request->getIncomingFaceId());
222 }
223
Vince12e49462014-06-09 13:29:32 -0500224 FaceEntry faceEntry;
225 faceEntry.faceId = parameters.getFaceId();
226 faceEntry.origin = parameters.getOrigin();
227 faceEntry.cost = parameters.getCost();
228 faceEntry.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500229
230 if (parameters.getExpirationPeriod() != time::milliseconds::max())
231 {
232 faceEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
233
234 // Schedule a new event, the old one will be cancelled during rib insertion.
235 EventId eventId;
236 NFD_LOG_TRACE("scheduling unregistration at: " << faceEntry.expires);
237 eventId = scheduler::schedule(parameters.getExpirationPeriod(),
238 bind(&RibManager::unregisterEntry,
239 this, shared_ptr<Interest>(), parameters));
240
241 //set the NewEventId of this entry
242 faceEntry.setExpirationEvent(eventId);
243 }
244 else
245 {
246 faceEntry.expires = time::steady_clock::TimePoint::max();
247 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700248
Vince12e49462014-06-09 13:29:32 -0500249 NFD_LOG_TRACE("register prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700250
Vince12e49462014-06-09 13:29:32 -0500251 m_managedRib.insert(parameters.getName(), faceEntry);
Vince Lehman4387e782014-06-19 16:57:45 -0500252
253 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700254}
255
256void
257RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
Syed Obaid3313a372014-07-01 01:31:33 -0500258 ControlParameters& params)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700259{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700260 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700261
Syed Obaid3313a372014-07-01 01:31:33 -0500262 //passing all parameters gives error in validation.
263 //so passing only the required arguments.
264 ControlParameters parameters;
265 parameters.setName(params.getName());
Alexander Afanasyevb609f002014-07-18 16:56:34 -0700266 if (params.hasFaceId())
267 parameters.setFaceId(params.getFaceId());
268 if (params.hasOrigin())
269 parameters.setOrigin(params.getOrigin());
Syed Obaid3313a372014-07-01 01:31:33 -0500270
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700271 if (!validateParameters(command, parameters))
272 {
Syed Obaid3313a372014-07-01 01:31:33 -0500273 NFD_LOG_DEBUG("unregister result: FAIL reason: malformed");
274 if (static_cast<bool>(request))
275 sendResponse(request->getName(), 400, "Malformed command");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700276 return;
277 }
278
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700279 if (!parameters.hasFaceId() || parameters.getFaceId() == 0)
280 {
281 parameters.setFaceId(request->getIncomingFaceId());
282 }
283
Vince12e49462014-06-09 13:29:32 -0500284 FaceEntry faceEntry;
285 faceEntry.faceId = parameters.getFaceId();
286 faceEntry.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700287
Vince12e49462014-06-09 13:29:32 -0500288 NFD_LOG_TRACE("unregister prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700289
Vince Lehman4387e782014-06-19 16:57:45 -0500290 m_managedRib.erase(parameters.getName(), faceEntry);
291
292 sendUpdatesToFib(request, parameters);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700293}
294
295void
296RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
297 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700298{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700299 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Syed Obaid3313a372014-07-01 01:31:33 -0500300 if (static_cast<bool>(request))
301 sendResponse(request->getName(), 403, failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700302}
303
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700304
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700305bool
306RibManager::extractParameters(const Name::Component& parameterComponent,
307 ControlParameters& extractedParameters)
308{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700309 try
310 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700311 Block rawParameters = parameterComponent.blockFromValue();
312 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700313 }
314 catch (const ndn::Tlv::Error& e)
315 {
316 return false;
317 }
318
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700319 NFD_LOG_DEBUG("Parameters parsed OK");
320 return true;
321}
322
323bool
324RibManager::validateParameters(const ControlCommand& command,
325 ControlParameters& parameters)
326{
327 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700328 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700329 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700330 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700331 catch (const ControlCommand::ArgumentError&)
332 {
333 return false;
334 }
335
336 command.applyDefaultsToRequest(parameters);
337
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700338 return true;
339}
340
341void
342RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700343 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500344 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700345{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700346 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700347
348 ControlResponse response;
349
350 if (code == 404)
351 {
352 response.setCode(code);
353 response.setText(error);
354 }
355 else
356 {
357 response.setCode(533);
358 std::ostringstream os;
359 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
360 response.setText(os.str());
361 }
362
Syed Obaid3313a372014-07-01 01:31:33 -0500363 if (static_cast<bool>(request))
364 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700365}
366
367void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700368RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
369 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500370 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700371{
372 ControlResponse response;
373
374 response.setCode(200);
375 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700376 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700377
Vince12e49462014-06-09 13:29:32 -0500378 NFD_LOG_TRACE("onRegSuccess: registered " << faceEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700379
Syed Obaid3313a372014-07-01 01:31:33 -0500380 if (static_cast<bool>(request))
381 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700382}
383
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700384
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700385void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700386RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
387 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500388 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700389{
390 ControlResponse response;
391
392 response.setCode(200);
393 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700394 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700395
Vince12e49462014-06-09 13:29:32 -0500396 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700397
Syed Obaid3313a372014-07-01 01:31:33 -0500398 if (static_cast<bool>(request))
399 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500400}
401
402void
403RibManager::sendSuccessResponse(const shared_ptr<const Interest>& request,
404 const ControlParameters& parameters)
405{
406 if (!static_cast<bool>(request))
407 {
408 return;
409 }
410
411 ControlResponse response;
412
413 response.setCode(200);
414 response.setText("Success");
415 response.setBody(parameters.wireEncode());
416
Syed Obaid3313a372014-07-01 01:31:33 -0500417 if (static_cast<bool>(request))
418 sendResponse(request->getName(), response);
Vince Lehman4387e782014-06-19 16:57:45 -0500419}
420
421void
422RibManager::sendErrorResponse(uint32_t code, const std::string& error,
423 const shared_ptr<const Interest>& request)
424{
425 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
426
427 if (!static_cast<bool>(request))
428 {
429 return;
430 }
431
432 ControlResponse response;
433
434 if (code == 404)
435 {
436 response.setCode(code);
437 response.setText(error);
438 }
439 else
440 {
441 response.setCode(533);
442 std::ostringstream os;
443 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
444 response.setText(os.str());
445 }
446
Syed Obaid3313a372014-07-01 01:31:33 -0500447 if (static_cast<bool>(request))
448 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700449}
450
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700451void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700452RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix)
453{
454 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
455}
456
457void
458RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg)
459{
460 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
461}
462
Vince Lehman4387e782014-06-19 16:57:45 -0500463bool
464RibManager::isTransactionComplete(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700465{
Vince Lehman4387e782014-06-19 16:57:45 -0500466 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
467
468 if (it != m_pendingFibTransactions.end())
469 {
470 int& updatesLeft = it->second;
471
472 updatesLeft--;
473
474 // All of the updates have been applied successfully
475 if (updatesLeft == 0)
476 {
477 m_pendingFibTransactions.erase(it);
478 return true;
479 }
480 }
481
482 return false;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700483}
484
485void
Vince Lehman4387e782014-06-19 16:57:45 -0500486RibManager::invalidateTransaction(const TransactionId transactionId)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700487{
Vince Lehman4387e782014-06-19 16:57:45 -0500488 FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId);
489
490 if (it != m_pendingFibTransactions.end())
491 {
492 m_pendingFibTransactions.erase(it);
493 }
494}
495
496void
497RibManager::onAddNextHopSuccess(const shared_ptr<const Interest>& request,
498 const ControlParameters& parameters,
499 const TransactionId transactionId,
500 const bool shouldSendResponse)
501{
502 if (isTransactionComplete(transactionId) && shouldSendResponse)
503 {
504 sendSuccessResponse(request, parameters);
505 }
506}
507
508void
509RibManager::onAddNextHopError(uint32_t code, const std::string& error,
510 const shared_ptr<const Interest>& request,
511 const TransactionId transactionId, const bool shouldSendResponse)
512{
513 invalidateTransaction(transactionId);
514
515 if (shouldSendResponse)
516 {
517 sendErrorResponse(code, error, request);
518 }
519}
520
521void
522RibManager::onRemoveNextHopSuccess(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::onRemoveNextHopError(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 }
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700544}
545
546void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700547RibManager::onControlHeaderSuccess()
548{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700549 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700550}
551
552void
553RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
554{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700555 std::ostringstream os;
556 os << "Couldn't enable local control header "
557 << "(code: " << code << ", info: " << reason << ")";
558 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700559}
560
561void
562RibManager::enableLocalControlHeader()
563{
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700564 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700565 ControlParameters()
566 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
567 bind(&RibManager::onControlHeaderSuccess, this),
568 bind(&RibManager::onControlHeaderError, this, _1, _2));
569}
570
571void
572RibManager::onNotification(const FaceEventNotification& notification)
573{
574 /// \todo A notification can be missed, in this case check Facelist
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700575 NFD_LOG_TRACE("onNotification: " << notification);
Vince Lehman4387e782014-06-19 16:57:45 -0500576 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) //face destroyed
577 {
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700578 scheduler::schedule(time::seconds(0),
579 bind(&RibManager::processErasureAfterNotification, this,
580 notification.getFaceId()));
Vince Lehman4387e782014-06-19 16:57:45 -0500581 }
582}
583
584void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700585RibManager::processErasureAfterNotification(uint64_t faceId)
586{
587 m_managedRib.erase(faceId);
588
589 sendUpdatesToFibAfterFaceDestroyEvent();
590}
591
592void
Vince Lehman4387e782014-06-19 16:57:45 -0500593RibManager::sendUpdatesToFib(const shared_ptr<const Interest>& request,
594 const ControlParameters& parameters)
595{
596 const Rib::FibUpdateList& updates = m_managedRib.getFibUpdates();
597
598 // If no updates were generated, consider the operation a success
599 if (updates.empty())
600 {
601 sendSuccessResponse(request, parameters);
602 return;
603 }
604
605 bool shouldWaitToRespond = false;
606
607 // An application request should wait for all FIB updates to be applied
608 // successfully before sending a response
609 if (parameters.getOrigin() == ndn::nfd::ROUTE_ORIGIN_APP)
610 {
611 shouldWaitToRespond = true;
612 }
613 else // Respond immediately
614 {
615 sendSuccessResponse(request, parameters);
616 }
617
618 NFD_LOG_DEBUG("Applying " << updates.size() << " updates to FIB");
619
620 // Assign an ID to this FIB transaction
621 TransactionId currentTransactionId = ++m_lastTransactionId;
622
623 // Add this transaction to the transaction table
624 m_pendingFibTransactions[currentTransactionId] = updates.size();
625
626 for (Rib::FibUpdateList::const_iterator it = updates.begin(); it != updates.end(); ++it)
627 {
628 shared_ptr<const FibUpdate> update(*it);
629
630 if (update->action == FibUpdate::ADD_NEXTHOP)
631 {
632 FaceEntry faceEntry;
633 faceEntry.faceId = update->faceId;
634 faceEntry.cost = update->cost;
635
636 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
637 ControlParameters()
638 .setName(update->name)
639 .setFaceId(faceEntry.faceId)
640 .setCost(faceEntry.cost),
641 bind(&RibManager::onAddNextHopSuccess, this, request,
642 parameters,
643 currentTransactionId,
644 shouldWaitToRespond),
645 bind(&RibManager::onAddNextHopError, this, _1, _2, request, currentTransactionId,
646 shouldWaitToRespond));
647 }
648 else if (update->action == FibUpdate::REMOVE_NEXTHOP)
649 {
650 FaceEntry faceEntry;
651 faceEntry.faceId = update->faceId;
652
653 m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>(
654 ControlParameters()
655 .setName(update->name)
656 .setFaceId(faceEntry.faceId),
657 bind(&RibManager::onRemoveNextHopSuccess, this, request,
658 parameters,
659 currentTransactionId,
660 shouldWaitToRespond),
661 bind(&RibManager::onRemoveNextHopError, this, _1, _2, request, currentTransactionId,
662 shouldWaitToRespond));
663 }
664 }
665
666 m_managedRib.clearFibUpdates();
667}
668
669void
670RibManager::sendUpdatesToFibAfterFaceDestroyEvent()
671{
672 ControlParameters parameters;
673 parameters.setOrigin(ndn::nfd::ROUTE_ORIGIN_STATIC);
674
675 sendUpdatesToFib(shared_ptr<const Interest>(), parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700676}
677
678} // namespace rib
679} // namespace nfd