blob: 4c03f3b050ec73e521b6bbe48299a7f37398c7a4 [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi1e46be32015-01-08 20:18:05 -07003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Vince12e49462014-06-09 13:29:32 -050024 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070025
26#include "rib-manager.hpp"
Alexander Afanasyev03ea3eb2014-04-17 18:19:06 -070027#include "core/global-io.hpp"
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070028#include "core/logger.hpp"
Alexander Afanasyev63108c42014-07-07 19:10:47 -070029#include "core/scheduler.hpp"
Vince Lehmancd613c52014-07-30 14:34:49 -050030#include <ndn-cxx/management/nfd-face-status.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070031
32namespace nfd {
33namespace rib {
34
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070035NFD_LOG_INIT("RibManager");
36
Alexander Afanasyev20d31442014-04-19 17:00:53 -070037const Name RibManager::COMMAND_PREFIX = "/localhost/nfd/rib";
38const Name RibManager::REMOTE_COMMAND_PREFIX = "/localhop/nfd/rib";
Vince Lehmancd613c52014-07-30 14:34:49 -050039const Name RibManager::FACES_LIST_DATASET_PREFIX = "/localhost/nfd/faces/list";
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070040
41const size_t RibManager::COMMAND_UNSIGNED_NCOMPS =
42 RibManager::COMMAND_PREFIX.size() +
43 1 + // verb
44 1; // verb options
45
46const size_t RibManager::COMMAND_SIGNED_NCOMPS =
47 RibManager::COMMAND_UNSIGNED_NCOMPS +
48 4; // (timestamp, nonce, signed info tlv, signature tlv)
49
Vince Lehmancd16c832014-07-23 15:14:55 -070050const RibManager::SignedVerbAndProcessor RibManager::SIGNED_COMMAND_VERBS[] =
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070051 {
Vince Lehmancd16c832014-07-23 15:14:55 -070052 SignedVerbAndProcessor(
53 Name::Component("register"),
54 &RibManager::registerEntry
55 ),
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070056
Vince Lehmancd16c832014-07-23 15:14:55 -070057 SignedVerbAndProcessor(
58 Name::Component("unregister"),
59 &RibManager::unregisterEntry
60 ),
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070061 };
62
Vince Lehmancd16c832014-07-23 15:14:55 -070063const RibManager::UnsignedVerbAndProcessor RibManager::UNSIGNED_COMMAND_VERBS[] =
64 {
65 UnsignedVerbAndProcessor(
66 Name::Component("list"),
67 &RibManager::listEntries
68 ),
69 };
70
71const Name RibManager::LIST_COMMAND_PREFIX("/localhost/nfd/rib/list");
72const size_t RibManager::LIST_COMMAND_NCOMPS = LIST_COMMAND_PREFIX.size();
73
Vince Lehman26b215c2014-08-17 15:00:41 -050074const time::seconds RibManager::ACTIVE_FACE_FETCH_INTERVAL = time::seconds(300);
75
Vince Lehmanc1dfdb42015-07-16 12:17:36 -050076RibManager::RibManager(ndn::Face& face, ndn::KeyChain& keyChain)
Vince Lehman72446ec2014-07-09 10:50:02 -050077 : m_face(face)
Vince Lehmanc1dfdb42015-07-16 12:17:36 -050078 , m_keyChain(keyChain)
Junxiao Shi8e273ca2014-11-12 00:42:29 -070079 , m_nfdController(m_face, m_keyChain)
Yingdi Yue5224e92014-04-29 18:04:02 -070080 , m_localhostValidator(m_face)
81 , m_localhopValidator(m_face)
Yingdi Yuf4db0b52014-04-17 13:17:39 -070082 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070083 , m_isLocalhopEnabled(false)
Yanbiao Lic17de832014-11-21 17:51:45 -080084 , m_remoteRegistrator(m_nfdController, m_keyChain, m_managedRib)
Vince Lehmancd16c832014-07-23 15:14:55 -070085 , m_ribStatusPublisher(m_managedRib, face, LIST_COMMAND_PREFIX, m_keyChain)
Vince Lehman76c751c2014-11-18 17:36:38 -060086 , m_fibUpdater(m_managedRib, m_nfdController)
Vince Lehmancd16c832014-07-23 15:14:55 -070087 , m_signedVerbDispatch(SIGNED_COMMAND_VERBS,
88 SIGNED_COMMAND_VERBS +
89 (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor)))
90 , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS,
91 UNSIGNED_COMMAND_VERBS +
92 (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor)))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070093{
94}
95
Vince Lehman26b215c2014-08-17 15:00:41 -050096RibManager::~RibManager()
97{
98 scheduler::cancel(m_activeFaceFetchEvent);
99}
100
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700101void
Junxiao Shia3295742014-05-16 22:40:10 -0700102RibManager::startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest)
103{
104 NFD_LOG_INFO("Listening on: " << commandPrefix);
105
106 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
107 ControlParameters()
108 .setName(commandPrefix)
109 .setFaceId(0),
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600110 bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(commandPrefix), _1),
Junxiao Shia3295742014-05-16 22:40:10 -0700111 bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(commandPrefix), _2));
112
113 m_face.setInterestFilter(commandPrefix, onRequest);
114}
115
116void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700117RibManager::registerWithNfd()
118{
119 //check whether the components of localhop and localhost prefixes are same
120 BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size());
121
Junxiao Shia3295742014-05-16 22:40:10 -0700122 this->startListening(COMMAND_PREFIX, bind(&RibManager::onLocalhostRequest, this, _2));
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700123
Junxiao Shia3295742014-05-16 22:40:10 -0700124 if (m_isLocalhopEnabled) {
125 this->startListening(REMOTE_COMMAND_PREFIX,
126 bind(&RibManager::onLocalhopRequest, this, _2));
127 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700128
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700129 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shifbf78342015-01-23 14:46:41 -0700130 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -0700131 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500132
Vince Lehman26b215c2014-08-17 15:00:41 -0500133 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700134}
135
136void
137RibManager::setConfigFile(ConfigFile& configFile)
138{
Yingdi Yue5224e92014-04-29 18:04:02 -0700139 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700140 bind(&RibManager::onConfig, this, _1, _2, _3));
141}
142
143void
144RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700145 bool isDryRun,
146 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700147{
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800148 bool isRemoteRegisterEnabled = false;
149
Vince Lehman76c751c2014-11-18 17:36:38 -0600150 for (const auto& item : configSection) {
151 if (item.first == "localhost_security") {
152 m_localhostValidator.load(item.second, filename);
Yingdi Yue5224e92014-04-29 18:04:02 -0700153 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600154 else if (item.first == "localhop_security") {
155 m_localhopValidator.load(item.second, filename);
156 m_isLocalhopEnabled = true;
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800157 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600158 else if (item.first == "remote_register") {
159 m_remoteRegistrator.loadConfig(item.second);
160 isRemoteRegisterEnabled = true;
161
162 // Avoid other actions when isDryRun == true
163 if (isDryRun) {
164 continue;
165 }
166
167 m_remoteRegistrator.enable();
168 }
169 else {
170 throw Error("Unrecognized rib property: " + item.first);
171 }
172 }
173
174 if (!isRemoteRegisterEnabled) {
175 m_remoteRegistrator.disable();
176 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700177}
178
179void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700180RibManager::sendResponse(const Name& name,
181 const ControlResponse& response)
182{
183 const Block& encodedControl = response.wireEncode();
184
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700185 shared_ptr<Data> responseData = make_shared<Data>(name);
186 responseData->setContent(encodedControl);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700187
Alexander Afanasyev97a9c2c2014-07-18 16:57:57 -0700188 m_keyChain.sign(*responseData);
189 m_face.put(*responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700190}
191
192void
193RibManager::sendResponse(const Name& name,
194 uint32_t code,
195 const std::string& text)
196{
197 ControlResponse response(code, text);
198 sendResponse(name, response);
199}
200
201void
Yingdi Yue5224e92014-04-29 18:04:02 -0700202RibManager::onLocalhostRequest(const Interest& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700203{
Vince Lehmancd16c832014-07-23 15:14:55 -0700204 const Name& command = request.getName();
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800205 const Name::Component& verb = command.get(COMMAND_PREFIX.size());
Vince Lehmancd16c832014-07-23 15:14:55 -0700206
207 UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
208
Vince Lehman76c751c2014-11-18 17:36:38 -0600209 if (unsignedVerbProcessor != m_unsignedVerbDispatch.end()) {
210 NFD_LOG_DEBUG("command result: processing unsigned verb: " << verb);
211 (unsignedVerbProcessor->second)(this, request);
212 }
213 else {
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);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700239
Vince Lehman76c751c2014-11-18 17:36:38 -0600240 if (verbProcessor != m_signedVerbDispatch.end()) {
241
242 ControlParameters parameters;
243 if (!extractParameters(parameterComponent, parameters)) {
244 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
245
246 if (static_cast<bool>(request)) {
247 sendResponse(command, 400, "Malformed command");
248 }
249
250 return;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700251 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600252
253 NFD_LOG_DEBUG("command result: processing verb: " << verb);
254 (verbProcessor->second)(this, request, parameters);
255 }
256 else {
257 NFD_LOG_DEBUG("Unsupported command: " << verb);
258
259 if (static_cast<bool>(request)) {
260 sendResponse(request->getName(), 501, "Unsupported command");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700261 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600262 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700263}
264
265void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700266RibManager::registerEntry(const shared_ptr<const Interest>& request,
267 ControlParameters& parameters)
268{
269 ndn::nfd::RibRegisterCommand command;
270
Vince Lehman76c751c2014-11-18 17:36:38 -0600271 if (!validateParameters(command, parameters)) {
272 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
Vince Lehmancd613c52014-07-30 14:34:49 -0500273
Vince Lehman76c751c2014-11-18 17:36:38 -0600274 if (static_cast<bool>(request)) {
275 sendResponse(request->getName(), 400, "Malformed command");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700276 }
277
Vince Lehman76c751c2014-11-18 17:36:38 -0600278 return;
279 }
280
Alexander Afanasyev483efd12014-08-14 10:51:18 -0700281 bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600282 if (isSelfRegistration) {
283 parameters.setFaceId(request->getIncomingFaceId());
284 }
285
286 // Respond since command is valid and authorized
287 sendSuccessResponse(request, parameters);
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700288
Vince Lehman218be0a2015-01-15 17:25:20 -0600289 Route route;
290 route.faceId = parameters.getFaceId();
291 route.origin = parameters.getOrigin();
292 route.cost = parameters.getCost();
293 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500294
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700295 if (parameters.hasExpirationPeriod() &&
296 parameters.getExpirationPeriod() != time::milliseconds::max())
Vince Lehman76c751c2014-11-18 17:36:38 -0600297 {
298 route.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
Syed Obaid3313a372014-07-01 01:31:33 -0500299
Vince Lehman76c751c2014-11-18 17:36:38 -0600300 // Schedule a new event, the old one will be cancelled during rib insertion.
301 scheduler::EventId eventId = scheduler::schedule(parameters.getExpirationPeriod(),
302 bind(&Rib::onRouteExpiration, &m_managedRib, parameters.getName(), route));
Syed Obaid3313a372014-07-01 01:31:33 -0500303
Vince Lehman76c751c2014-11-18 17:36:38 -0600304 NFD_LOG_TRACE("Scheduled unregistration at: " << route.expires <<
305 " with EventId: " << eventId);
306
307 // Set the NewEventId of this entry
308 route.setExpirationEvent(eventId);
309 }
310 else {
311 route.expires = time::steady_clock::TimePoint::max();
312 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700313
Vince Lehmanff8b3972015-02-20 16:51:21 -0600314 NFD_LOG_INFO("Adding route " << parameters.getName() << " nexthop=" << route.faceId
315 << " origin=" << route.origin
316 << " cost=" << route.cost);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700317
Vince Lehman76c751c2014-11-18 17:36:38 -0600318 RibUpdate update;
319 update.setAction(RibUpdate::REGISTER)
320 .setName(parameters.getName())
321 .setRoute(route);
322
323 m_managedRib.beginApplyUpdate(update,
324 bind(&RibManager::onRibUpdateSuccess, this, update),
325 bind(&RibManager::onRibUpdateFailure, this, update, _1, _2));
326
Vince Lehman218be0a2015-01-15 17:25:20 -0600327 m_registeredFaces.insert(route.faceId);
Vince Lehman281ded72014-08-21 12:17:08 -0500328}
329
330void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700331RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
Syed Obaid3313a372014-07-01 01:31:33 -0500332 ControlParameters& params)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700333{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700334 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700335
Vince Lehman76c751c2014-11-18 17:36:38 -0600336 // Passing all parameters gives error in validation,
337 // so passing only the required arguments.
Syed Obaid3313a372014-07-01 01:31:33 -0500338 ControlParameters parameters;
339 parameters.setName(params.getName());
Syed Obaid3313a372014-07-01 01:31:33 -0500340
Vince Lehman76c751c2014-11-18 17:36:38 -0600341 if (params.hasFaceId()) {
342 parameters.setFaceId(params.getFaceId());
343 }
344
345 if (params.hasOrigin()) {
346 parameters.setOrigin(params.getOrigin());
347 }
348
349 if (!validateParameters(command, parameters)) {
350 NFD_LOG_DEBUG("unregister result: FAIL reason: malformed");
351
352 if (static_cast<bool>(request)) {
353 sendResponse(request->getName(), 400, "Malformed command");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700354 }
355
Vince Lehman76c751c2014-11-18 17:36:38 -0600356 return;
357 }
358
Alexander Afanasyev483efd12014-08-14 10:51:18 -0700359 bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600360 if (isSelfRegistration) {
361 parameters.setFaceId(request->getIncomingFaceId());
362 }
363
364 // Respond since command is valid and authorized
365 sendSuccessResponse(request, parameters);
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700366
Vince Lehman218be0a2015-01-15 17:25:20 -0600367 Route route;
368 route.faceId = parameters.getFaceId();
369 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700370
Vince Lehmanff8b3972015-02-20 16:51:21 -0600371 NFD_LOG_INFO("Removing route " << parameters.getName() << " nexthop=" << route.faceId
372 << " origin=" << route.origin);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700373
Vince Lehman76c751c2014-11-18 17:36:38 -0600374 RibUpdate update;
375 update.setAction(RibUpdate::UNREGISTER)
376 .setName(parameters.getName())
377 .setRoute(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500378
Vince Lehman76c751c2014-11-18 17:36:38 -0600379 m_managedRib.beginApplyUpdate(update,
380 bind(&RibManager::onRibUpdateSuccess, this, update),
381 bind(&RibManager::onRibUpdateFailure, this, update, _1, _2));
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700382}
383
384void
385RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
386 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700387{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700388 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Vince Lehman76c751c2014-11-18 17:36:38 -0600389
390 if (static_cast<bool>(request)) {
Syed Obaid3313a372014-07-01 01:31:33 -0500391 sendResponse(request->getName(), 403, failureInfo);
Vince Lehman76c751c2014-11-18 17:36:38 -0600392 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700393}
394
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700395
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700396bool
397RibManager::extractParameters(const Name::Component& parameterComponent,
398 ControlParameters& extractedParameters)
399{
Vince Lehman76c751c2014-11-18 17:36:38 -0600400 try {
401 Block rawParameters = parameterComponent.blockFromValue();
402 extractedParameters.wireDecode(rawParameters);
403 }
404 catch (const tlv::Error&) {
405 return false;
406 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700407
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700408 NFD_LOG_DEBUG("Parameters parsed OK");
409 return true;
410}
411
412bool
413RibManager::validateParameters(const ControlCommand& command,
414 ControlParameters& parameters)
415{
Vince Lehman76c751c2014-11-18 17:36:38 -0600416 try {
417 command.validateRequest(parameters);
418 }
419 catch (const ControlCommand::ArgumentError&) {
420 return false;
421 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700422
423 command.applyDefaultsToRequest(parameters);
424
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700425 return true;
426}
427
428void
429RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700430 const shared_ptr<const Interest>& request,
Vince Lehman218be0a2015-01-15 17:25:20 -0600431 const Route& route)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700432{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700433 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700434
435 ControlResponse response;
436
Vince Lehman76c751c2014-11-18 17:36:38 -0600437 if (code == 404) {
438 response.setCode(code);
439 response.setText(error);
440 }
441 else {
442 response.setCode(533);
443 std::ostringstream os;
444 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
445 response.setText(os.str());
446 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700447
Vince Lehman76c751c2014-11-18 17:36:38 -0600448 if (static_cast<bool>(request)) {
Syed Obaid3313a372014-07-01 01:31:33 -0500449 sendResponse(request->getName(), response);
Vince Lehman76c751c2014-11-18 17:36:38 -0600450 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700451}
452
453void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700454RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
455 const ControlParameters& parameters,
Vince Lehman218be0a2015-01-15 17:25:20 -0600456 const Route& route)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700457{
458 ControlResponse response;
459
460 response.setCode(200);
461 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700462 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700463
Vince Lehman218be0a2015-01-15 17:25:20 -0600464 NFD_LOG_TRACE("onRegSuccess: registered " << route);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700465
Vince Lehman76c751c2014-11-18 17:36:38 -0600466 if (static_cast<bool>(request)) {
Syed Obaid3313a372014-07-01 01:31:33 -0500467 sendResponse(request->getName(), response);
Vince Lehman76c751c2014-11-18 17:36:38 -0600468 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700469}
470
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700471
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700472void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700473RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
474 const ControlParameters& parameters,
Vince Lehman218be0a2015-01-15 17:25:20 -0600475 const Route& route)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700476{
477 ControlResponse response;
478
479 response.setCode(200);
480 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700481 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700482
Vince Lehman218be0a2015-01-15 17:25:20 -0600483 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << route);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700484
Vince Lehman76c751c2014-11-18 17:36:38 -0600485 if (static_cast<bool>(request)) {
Syed Obaid3313a372014-07-01 01:31:33 -0500486 sendResponse(request->getName(), response);
Vince Lehman76c751c2014-11-18 17:36:38 -0600487 }
Vince Lehman4387e782014-06-19 16:57:45 -0500488}
489
490void
491RibManager::sendSuccessResponse(const shared_ptr<const Interest>& request,
492 const ControlParameters& parameters)
493{
Vince Lehman76c751c2014-11-18 17:36:38 -0600494 if (!static_cast<bool>(request)) {
495 return;
496 }
Vince Lehman4387e782014-06-19 16:57:45 -0500497
498 ControlResponse response;
499
500 response.setCode(200);
501 response.setText("Success");
502 response.setBody(parameters.wireEncode());
503
Vince Lehman76c751c2014-11-18 17:36:38 -0600504 if (static_cast<bool>(request)) {
Syed Obaid3313a372014-07-01 01:31:33 -0500505 sendResponse(request->getName(), response);
Vince Lehman76c751c2014-11-18 17:36:38 -0600506 }
Vince Lehman4387e782014-06-19 16:57:45 -0500507}
508
509void
510RibManager::sendErrorResponse(uint32_t code, const std::string& error,
511 const shared_ptr<const Interest>& request)
512{
513 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
514
Vince Lehman76c751c2014-11-18 17:36:38 -0600515 if (!static_cast<bool>(request)) {
516 return;
517 }
Vince Lehman4387e782014-06-19 16:57:45 -0500518
519 ControlResponse response;
520
Vince Lehman76c751c2014-11-18 17:36:38 -0600521 if (code == 404) {
522 response.setCode(code);
523 response.setText(error);
524 }
525 else {
526 response.setCode(533);
527 std::ostringstream os;
528 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
529 response.setText(os.str());
530 }
Vince Lehman4387e782014-06-19 16:57:45 -0500531
Vince Lehman76c751c2014-11-18 17:36:38 -0600532 if (static_cast<bool>(request)) {
Syed Obaid3313a372014-07-01 01:31:33 -0500533 sendResponse(request->getName(), response);
Vince Lehman76c751c2014-11-18 17:36:38 -0600534 }
535}
536
537void
538RibManager::onRibUpdateSuccess(const RibUpdate& update)
539{
540 NFD_LOG_DEBUG("RIB update succeeded for " << update);
541}
542
543void
544RibManager::onRibUpdateFailure(const RibUpdate& update, uint32_t code, const std::string& error)
545{
546 NFD_LOG_DEBUG("RIB update failed for " << update << " (code: " << code
547 << ", error: " << error << ")");
548
549 // Since the FIB rejected the update, clean up invalid routes
550 scheduleActiveFaceFetch(time::seconds(1));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700551}
552
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700553void
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600554RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix,
555 const ndn::nfd::ControlParameters& result)
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700556{
557 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600558
559 // Routes must be inserted into the RIB so route flags can be applied
560 Route route;
561 route.faceId = result.getFaceId();
562 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
563 route.expires = time::steady_clock::TimePoint::max();
564 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
565
566 m_managedRib.insert(prefix, route);
567
568 m_registeredFaces.insert(route.faceId);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700569}
570
571void
572RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg)
573{
574 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
575}
576
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700577void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700578RibManager::onControlHeaderSuccess()
579{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700580 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700581}
582
583void
584RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
585{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700586 std::ostringstream os;
587 os << "Couldn't enable local control header "
588 << "(code: " << code << ", info: " << reason << ")";
589 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700590}
591
592void
593RibManager::enableLocalControlHeader()
594{
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700595 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700596 ControlParameters()
597 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
598 bind(&RibManager::onControlHeaderSuccess, this),
599 bind(&RibManager::onControlHeaderError, this, _1, _2));
600}
601
602void
603RibManager::onNotification(const FaceEventNotification& notification)
604{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700605 NFD_LOG_TRACE("onNotification: " << notification);
Vince Lehmancd613c52014-07-30 14:34:49 -0500606
Vince Lehman76c751c2014-11-18 17:36:38 -0600607 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
608 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
Vince Lehmancd613c52014-07-30 14:34:49 -0500609
Vince Lehman76c751c2014-11-18 17:36:38 -0600610 scheduler::schedule(time::seconds(0),
611 bind(&RibManager::onFaceDestroyedEvent, this, notification.getFaceId()));
612 }
Vince Lehman4387e782014-06-19 16:57:45 -0500613}
614
615void
Vince Lehman76c751c2014-11-18 17:36:38 -0600616RibManager::onFaceDestroyedEvent(uint64_t faceId)
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700617{
Vince Lehman76c751c2014-11-18 17:36:38 -0600618 m_managedRib.beginRemoveFace(faceId);
Vince Lehman26b215c2014-08-17 15:00:41 -0500619 m_registeredFaces.erase(faceId);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700620}
621
Vince Lehmancd16c832014-07-23 15:14:55 -0700622void
623RibManager::listEntries(const Interest& request)
624{
625 const Name& command = request.getName();
626 const size_t commandNComps = command.size();
627
Vince Lehman76c751c2014-11-18 17:36:38 -0600628 if (commandNComps < LIST_COMMAND_NCOMPS || !LIST_COMMAND_PREFIX.isPrefixOf(command)) {
629 NFD_LOG_DEBUG("command result: malformed");
630
631 sendResponse(command, 400, "Malformed command");
632 return;
633 }
Vince Lehmancd16c832014-07-23 15:14:55 -0700634
635 m_ribStatusPublisher.publish();
636}
637
Vince Lehmancd613c52014-07-30 14:34:49 -0500638void
Vince Lehman26b215c2014-08-17 15:00:41 -0500639RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
640{
641 scheduler::cancel(m_activeFaceFetchEvent);
642
643 m_activeFaceFetchEvent = scheduler::schedule(timeToWait,
644 bind(&RibManager::fetchActiveFaces, this));
645}
646
647void
Vince Lehmancd613c52014-07-30 14:34:49 -0500648RibManager::fetchActiveFaces()
649{
650 NFD_LOG_DEBUG("Fetching active faces");
651
652 Interest interest(FACES_LIST_DATASET_PREFIX);
653 interest.setChildSelector(1);
654 interest.setMustBeFresh(true);
655
656 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
657
658 m_face.expressInterest(interest,
659 bind(&RibManager::fetchSegments, this, _2, buffer),
660 bind(&RibManager::onFetchFaceStatusTimeout, this));
661}
662
663void
664RibManager::fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer)
665{
666 buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
667 data.getContent().value_size());
668
669 uint64_t currentSegment = data.getName().get(-1).toSegment();
670
671 const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
Vince Lehman76c751c2014-11-18 17:36:38 -0600672
673 if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment) {
674 m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
675 bind(&RibManager::fetchSegments, this, _2, buffer),
676 bind(&RibManager::onFetchFaceStatusTimeout, this));
677 }
678 else {
679 removeInvalidFaces(buffer);
680 }
Vince Lehmancd613c52014-07-30 14:34:49 -0500681}
682
683void
Vince Lehman26b215c2014-08-17 15:00:41 -0500684RibManager::removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer)
Vince Lehmancd613c52014-07-30 14:34:49 -0500685{
Vince Lehman26b215c2014-08-17 15:00:41 -0500686 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500687
688 ndn::ConstBufferPtr buf = buffer->buf();
689
690 Block block;
691 size_t offset = 0;
Vince Lehman26b215c2014-08-17 15:00:41 -0500692 FaceIdSet activeFaces;
Vince Lehmancd613c52014-07-30 14:34:49 -0500693
Junxiao Shi78926c92015-02-28 22:56:06 -0700694 while (offset < buf->size()) {
695 bool isOk = false;
696 std::tie(isOk, block) = Block::fromBuffer(buf, offset);
697 if (!isOk) {
698 std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
699 break;
Vince Lehmancd613c52014-07-30 14:34:49 -0500700 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500701
Junxiao Shi78926c92015-02-28 22:56:06 -0700702 offset += block.size();
703
704 ndn::nfd::FaceStatus status(block);
705 activeFaces.insert(status.getFaceId());
706 }
707
Vince Lehman26b215c2014-08-17 15:00:41 -0500708 // Look for face IDs that were registered but not active to find missed
709 // face destroyed events
Vince Lehman76c751c2014-11-18 17:36:38 -0600710 for (uint64_t faceId : m_registeredFaces) {
711 if (activeFaces.find(faceId) == activeFaces.end()) {
712 NFD_LOG_DEBUG("Removing invalid face ID: " << faceId);
713
714 scheduler::schedule(time::seconds(0),
715 bind(&RibManager::onFaceDestroyedEvent, this, faceId));
Vince Lehman26b215c2014-08-17 15:00:41 -0500716 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600717 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500718
719 // Reschedule the check for future clean up
720 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500721}
722
723void
724RibManager::onFetchFaceStatusTimeout()
725{
726 std::cerr << "Face Status Dataset request timed out" << std::endl;
Vince Lehman26b215c2014-08-17 15:00:41 -0500727 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500728}
729
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700730} // namespace rib
731} // namespace nfd