blob: ae76a811877d6f8369318b15974c5a54a4c4cda0 [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordon9fcf1232017-03-10 22:30:20 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shi1e46be32015-01-08 20:18:05 -07004 * 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"
Nick Gordon9fcf1232017-03-10 22:30:20 +000027#include "readvertise/readvertise.hpp"
28#include "readvertise/client-to-nlsr-readvertise-policy.hpp"
29#include "readvertise/nfd-rib-readvertise-destination.hpp"
30
Alexander Afanasyev03ea3eb2014-04-17 18:19:06 -070031#include "core/global-io.hpp"
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070032#include "core/logger.hpp"
Alexander Afanasyev63108c42014-07-07 19:10:47 -070033#include "core/scheduler.hpp"
Nick Gordon9fcf1232017-03-10 22:30:20 +000034
Junxiao Shicbc8e942016-09-06 03:17:45 +000035#include <ndn-cxx/lp/tags.hpp>
Nick Gordon9fcf1232017-03-10 22:30:20 +000036#include <ndn-cxx/mgmt/nfd/control-command.hpp>
37#include <ndn-cxx/mgmt/nfd/control-response.hpp>
38#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000039#include <ndn-cxx/mgmt/nfd/face-status.hpp>
40#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070041
42namespace nfd {
43namespace rib {
44
Nick Gordon9fcf1232017-03-10 22:30:20 +000045using ndn::nfd::ControlCommand;
46using ndn::nfd::ControlResponse;
47using ndn::nfd::ControlParameters;
48using ndn::nfd::FaceEventNotification;
49
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070050NFD_LOG_INIT("RibManager");
51
Yanbiao Licf0db022016-01-29 00:54:25 -080052const Name RibManager::LOCAL_HOST_TOP_PREFIX = "/localhost/nfd";
53const Name RibManager::LOCAL_HOP_TOP_PREFIX = "/localhop/nfd";
Junxiao Shifde3f542016-07-10 19:54:53 +000054const std::string RibManager::MGMT_MODULE_NAME = "rib";
Vince Lehmancd613c52014-07-30 14:34:49 -050055const Name RibManager::FACES_LIST_DATASET_PREFIX = "/localhost/nfd/faces/list";
Vince Lehman26b215c2014-08-17 15:00:41 -050056const time::seconds RibManager::ACTIVE_FACE_FETCH_INTERVAL = time::seconds(300);
Nick Gordon9fcf1232017-03-10 22:30:20 +000057const Name RibManager::READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
Vince Lehman26b215c2014-08-17 15:00:41 -050058
Yanbiao Licf0db022016-01-29 00:54:25 -080059RibManager::RibManager(Dispatcher& dispatcher,
60 ndn::Face& face,
61 ndn::KeyChain& keyChain)
Junxiao Shifde3f542016-07-10 19:54:53 +000062 : ManagerBase(dispatcher, MGMT_MODULE_NAME)
Yanbiao Licf0db022016-01-29 00:54:25 -080063 , m_face(face)
Vince Lehmanc1dfdb42015-07-16 12:17:36 -050064 , m_keyChain(keyChain)
Junxiao Shi8e273ca2014-11-12 00:42:29 -070065 , m_nfdController(m_face, m_keyChain)
Yanbiao Licf0db022016-01-29 00:54:25 -080066 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070067 , m_localhostValidator(m_face)
68 , m_localhopValidator(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070069 , m_isLocalhopEnabled(false)
Yanbiao Licf0db022016-01-29 00:54:25 -080070 , m_prefixPropagator(m_nfdController, m_keyChain, m_rib)
71 , m_fibUpdater(m_rib, m_nfdController)
72 , m_addTopPrefix([&dispatcher] (const Name& topPrefix) {
73 dispatcher.addTopPrefix(topPrefix, false);
74 })
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070075{
Yanbiao Licf0db022016-01-29 00:54:25 -080076 registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
77 bind(&RibManager::registerEntry, this, _2, _3, _4, _5));
78 registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister",
79 bind(&RibManager::unregisterEntry, this, _2, _3, _4, _5));
80
81 registerStatusDatasetHandler("list", bind(&RibManager::listEntries, this, _1, _2, _3));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070082}
83
Vince Lehman26b215c2014-08-17 15:00:41 -050084RibManager::~RibManager()
85{
86 scheduler::cancel(m_activeFaceFetchEvent);
87}
88
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070089void
90RibManager::registerWithNfd()
91{
Yanbiao Licf0db022016-01-29 00:54:25 -080092 registerTopPrefix(LOCAL_HOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070093
Junxiao Shia3295742014-05-16 22:40:10 -070094 if (m_isLocalhopEnabled) {
Yanbiao Licf0db022016-01-29 00:54:25 -080095 registerTopPrefix(LOCAL_HOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -070096 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070097
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070098 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shifbf78342015-01-23 14:46:41 -070099 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -0700100 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500101
Vince Lehman26b215c2014-08-17 15:00:41 -0500102 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700103}
104
105void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000106RibManager::enableLocalFields()
Yanbiao Licf0db022016-01-29 00:54:25 -0800107{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000108 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
Yanbiao Licf0db022016-01-29 00:54:25 -0800109 ControlParameters()
Eric Newberryecc45cb2016-11-08 19:57:12 +0000110 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true),
111 bind(&RibManager::onEnableLocalFieldsSuccess, this),
112 bind(&RibManager::onEnableLocalFieldsError, this, _1));
Yanbiao Licf0db022016-01-29 00:54:25 -0800113}
114
115void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700116RibManager::setConfigFile(ConfigFile& configFile)
117{
Yingdi Yue5224e92014-04-29 18:04:02 -0700118 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700119 bind(&RibManager::onConfig, this, _1, _2, _3));
120}
121
122void
Yanbiao Licf0db022016-01-29 00:54:25 -0800123RibManager::onRibUpdateSuccess(const RibUpdate& update)
124{
125 NFD_LOG_DEBUG("RIB update succeeded for " << update);
126}
127
128void
129RibManager::onRibUpdateFailure(const RibUpdate& update, uint32_t code, const std::string& error)
130{
131 NFD_LOG_DEBUG("RIB update failed for " << update << " (code: " << code
132 << ", error: " << error << ")");
133
134 // Since the FIB rejected the update, clean up invalid routes
135 scheduleActiveFaceFetch(time::seconds(1));
136}
137
138void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700139RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700140 bool isDryRun,
141 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700142{
Yanbiao Lid7c96362015-01-30 23:58:24 -0800143 bool isAutoPrefixPropagatorEnabled = false;
Nick Gordon9fcf1232017-03-10 22:30:20 +0000144 bool wantReadvertiseToNlsr = false;
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800145
Vince Lehman76c751c2014-11-18 17:36:38 -0600146 for (const auto& item : configSection) {
147 if (item.first == "localhost_security") {
148 m_localhostValidator.load(item.second, filename);
Yingdi Yue5224e92014-04-29 18:04:02 -0700149 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600150 else if (item.first == "localhop_security") {
151 m_localhopValidator.load(item.second, filename);
152 m_isLocalhopEnabled = true;
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800153 }
Yanbiao Lid7c96362015-01-30 23:58:24 -0800154 else if (item.first == "auto_prefix_propagate") {
155 m_prefixPropagator.loadConfig(item.second);
156 isAutoPrefixPropagatorEnabled = true;
Vince Lehman76c751c2014-11-18 17:36:38 -0600157
158 // Avoid other actions when isDryRun == true
159 if (isDryRun) {
160 continue;
161 }
162
Yanbiao Lid7c96362015-01-30 23:58:24 -0800163 m_prefixPropagator.enable();
Vince Lehman76c751c2014-11-18 17:36:38 -0600164 }
Nick Gordon9fcf1232017-03-10 22:30:20 +0000165 else if (item.first == "readvertise_nlsr") {
166 wantReadvertiseToNlsr = ConfigFile::parseYesNo(item, "rib.readvertise_nlsr");
167 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600168 else {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700169 BOOST_THROW_EXCEPTION(Error("Unrecognized rib property: " + item.first));
Vince Lehman76c751c2014-11-18 17:36:38 -0600170 }
171 }
172
Yanbiao Lid7c96362015-01-30 23:58:24 -0800173 if (!isAutoPrefixPropagatorEnabled) {
174 m_prefixPropagator.disable();
Vince Lehman76c751c2014-11-18 17:36:38 -0600175 }
Nick Gordon9fcf1232017-03-10 22:30:20 +0000176
177 if (wantReadvertiseToNlsr && m_readvertiseNlsr == nullptr) {
178 NFD_LOG_DEBUG("Enabling readvertise-to-nlsr.");
179 m_readvertiseNlsr.reset(new Readvertise(
180 m_rib,
181 make_unique<ClientToNlsrReadvertisePolicy>(),
182 make_unique<NfdRibReadvertiseDestination>(m_nfdController, READVERTISE_NLSR_PREFIX, m_rib)));
183 }
184 else if (!wantReadvertiseToNlsr && m_readvertiseNlsr != nullptr) {
185 NFD_LOG_DEBUG("Disabling readvertise-to-nlsr.");
186 m_readvertiseNlsr.reset();
187 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700188}
189
190void
Yanbiao Licf0db022016-01-29 00:54:25 -0800191RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700192{
Yanbiao Licf0db022016-01-29 00:54:25 -0800193 // register entry to the FIB
194 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
195 ControlParameters()
Junxiao Shifde3f542016-07-10 19:54:53 +0000196 .setName(Name(topPrefix).append(MGMT_MODULE_NAME))
Yanbiao Licf0db022016-01-29 00:54:25 -0800197 .setFaceId(0),
Junxiao Shib2600172016-07-11 08:53:53 +0000198 bind(&RibManager::onCommandPrefixAddNextHopSuccess, this, cref(topPrefix), _1),
Junxiao Shi29b41282016-08-22 03:47:02 +0000199 bind(&RibManager::onCommandPrefixAddNextHopError, this, cref(topPrefix), _1));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700200
Yanbiao Licf0db022016-01-29 00:54:25 -0800201 // add top prefix to the dispatcher
202 m_addTopPrefix(topPrefix);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700203}
204
205void
Yanbiao Licf0db022016-01-29 00:54:25 -0800206RibManager::registerEntry(const Name& topPrefix, const Interest& interest,
207 ControlParameters parameters,
208 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700209{
Yanbiao Licf0db022016-01-29 00:54:25 -0800210 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600211
212 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800213 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700214
Vince Lehman218be0a2015-01-15 17:25:20 -0600215 Route route;
216 route.faceId = parameters.getFaceId();
217 route.origin = parameters.getOrigin();
218 route.cost = parameters.getCost();
219 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500220
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700221 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000222 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Vince Lehman76c751c2014-11-18 17:36:38 -0600223 route.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
Syed Obaid3313a372014-07-01 01:31:33 -0500224
Vince Lehman76c751c2014-11-18 17:36:38 -0600225 // Schedule a new event, the old one will be cancelled during rib insertion.
226 scheduler::EventId eventId = scheduler::schedule(parameters.getExpirationPeriod(),
Yanbiao Licf0db022016-01-29 00:54:25 -0800227 bind(&Rib::onRouteExpiration, &m_rib, parameters.getName(), route));
Syed Obaid3313a372014-07-01 01:31:33 -0500228
Vince Lehman76c751c2014-11-18 17:36:38 -0600229 NFD_LOG_TRACE("Scheduled unregistration at: " << route.expires <<
230 " with EventId: " << eventId);
231
232 // Set the NewEventId of this entry
233 route.setExpirationEvent(eventId);
234 }
235 else {
236 route.expires = time::steady_clock::TimePoint::max();
237 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700238
Vince Lehmanff8b3972015-02-20 16:51:21 -0600239 NFD_LOG_INFO("Adding route " << parameters.getName() << " nexthop=" << route.faceId
240 << " origin=" << route.origin
241 << " cost=" << route.cost);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700242
Vince Lehman76c751c2014-11-18 17:36:38 -0600243 RibUpdate update;
244 update.setAction(RibUpdate::REGISTER)
245 .setName(parameters.getName())
246 .setRoute(route);
247
Yanbiao Licf0db022016-01-29 00:54:25 -0800248 m_rib.beginApplyUpdate(update,
Junxiao Shib2600172016-07-11 08:53:53 +0000249 bind(&RibManager::onRibUpdateSuccess, this, update),
250 bind(&RibManager::onRibUpdateFailure, this, update, _1, _2));
Vince Lehman76c751c2014-11-18 17:36:38 -0600251
Vince Lehman218be0a2015-01-15 17:25:20 -0600252 m_registeredFaces.insert(route.faceId);
Vince Lehman281ded72014-08-21 12:17:08 -0500253}
254
255void
Yanbiao Licf0db022016-01-29 00:54:25 -0800256RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest,
257 ControlParameters parameters,
258 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700259{
Yanbiao Licf0db022016-01-29 00:54:25 -0800260 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600261
262 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800263 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700264
Vince Lehman218be0a2015-01-15 17:25:20 -0600265 Route route;
266 route.faceId = parameters.getFaceId();
267 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700268
Vince Lehmanff8b3972015-02-20 16:51:21 -0600269 NFD_LOG_INFO("Removing route " << parameters.getName() << " nexthop=" << route.faceId
270 << " origin=" << route.origin);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700271
Vince Lehman76c751c2014-11-18 17:36:38 -0600272 RibUpdate update;
273 update.setAction(RibUpdate::UNREGISTER)
274 .setName(parameters.getName())
275 .setRoute(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500276
Yanbiao Licf0db022016-01-29 00:54:25 -0800277 m_rib.beginApplyUpdate(update,
Junxiao Shib2600172016-07-11 08:53:53 +0000278 bind(&RibManager::onRibUpdateSuccess, this, update),
279 bind(&RibManager::onRibUpdateFailure, this, update, _1, _2));
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700280}
281
282void
Yanbiao Licf0db022016-01-29 00:54:25 -0800283RibManager::listEntries(const Name& topPrefix, const Interest& interest,
284 ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700285{
Yanbiao Licf0db022016-01-29 00:54:25 -0800286 for (auto&& ribTableEntry : m_rib) {
287 const auto& ribEntry = *ribTableEntry.second;
288 ndn::nfd::RibEntry record;
Vince Lehman76c751c2014-11-18 17:36:38 -0600289
Yanbiao Licf0db022016-01-29 00:54:25 -0800290 for (auto&& route : ribEntry) {
291 ndn::nfd::Route routeElement;
292 routeElement.setFaceId(route.faceId)
293 .setOrigin(route.origin)
294 .setCost(route.cost)
295 .setFlags(route.flags);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700296
Yanbiao Licf0db022016-01-29 00:54:25 -0800297 if (route.expires < time::steady_clock::TimePoint::max()) {
298 routeElement.setExpirationPeriod(time::duration_cast<time::milliseconds>(
299 route.expires - time::steady_clock::now()));
300 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700301
Yanbiao Licf0db022016-01-29 00:54:25 -0800302 record.addRoute(routeElement);
303 }
304
305 record.setName(ribEntry.getName());
306 context.append(record.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600307 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700308
Yanbiao Licf0db022016-01-29 00:54:25 -0800309 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700310}
311
312void
Yanbiao Licf0db022016-01-29 00:54:25 -0800313RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700314{
Yanbiao Licf0db022016-01-29 00:54:25 -0800315 bool isSelfRegistration = (parameters.getFaceId() == 0);
316 if (isSelfRegistration) {
317 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
318 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
319 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
320 // and is initialized synchronously with IncomingFaceId field enabled.
321 BOOST_ASSERT(incomingFaceIdTag != nullptr);
322 parameters.setFaceId(*incomingFaceIdTag);
Vince Lehman76c751c2014-11-18 17:36:38 -0600323 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700324}
325
Junxiao Shi21738402016-08-19 19:48:00 +0000326ndn::mgmt::Authorization
327RibManager::makeAuthorization(const std::string& verb)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700328{
Junxiao Shi21738402016-08-19 19:48:00 +0000329 return [this] (const Name& prefix, const Interest& interest,
330 const ndn::mgmt::ControlParameters* params,
331 const ndn::mgmt::AcceptContinuation& accept,
332 const ndn::mgmt::RejectContinuation& reject) {
333 BOOST_ASSERT(params != nullptr);
334 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters));
335 BOOST_ASSERT(prefix == LOCAL_HOST_TOP_PREFIX || prefix == LOCAL_HOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700336
Junxiao Shi21738402016-08-19 19:48:00 +0000337 ndn::ValidatorConfig& validator = prefix == LOCAL_HOST_TOP_PREFIX ?
338 m_localhostValidator : m_localhopValidator;
339 validator.validate(interest,
340 bind([&interest, this, accept] { extractRequester(interest, accept); }),
341 bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); }));
342 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500343}
344
345void
Vince Lehmancd613c52014-07-30 14:34:49 -0500346RibManager::fetchActiveFaces()
347{
348 NFD_LOG_DEBUG("Fetching active faces");
349
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700350 m_nfdController.fetch<ndn::nfd::FaceDataset>(
351 bind(&RibManager::removeInvalidFaces, this, _1),
352 bind(&RibManager::onFetchActiveFacesFailure, this, _1, _2),
353 ndn::nfd::CommandOptions());
Vince Lehmancd613c52014-07-30 14:34:49 -0500354}
355
356void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700357RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason)
Vince Lehmancd613c52014-07-30 14:34:49 -0500358{
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700359 NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason);
Yanbiao Licf0db022016-01-29 00:54:25 -0800360 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
361}
362
363void
364RibManager::onFaceDestroyedEvent(uint64_t faceId)
365{
366 m_rib.beginRemoveFace(faceId);
367 m_registeredFaces.erase(faceId);
368}
369
370void
371RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
372{
373 scheduler::cancel(m_activeFaceFetchEvent);
374
375 m_activeFaceFetchEvent = scheduler::schedule(timeToWait,
376 bind(&RibManager::fetchActiveFaces, this));
377}
378
379void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700380RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500381{
Vince Lehman26b215c2014-08-17 15:00:41 -0500382 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500383
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700384 FaceIdSet activeFaceIds;
385 for (const ndn::nfd::FaceStatus& item : activeFaces) {
386 activeFaceIds.insert(item.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700387 }
388
Vince Lehman26b215c2014-08-17 15:00:41 -0500389 // Look for face IDs that were registered but not active to find missed
390 // face destroyed events
Yanbiao Licf0db022016-01-29 00:54:25 -0800391 for (auto&& faceId : m_registeredFaces) {
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700392 if (activeFaceIds.count(faceId) == 0) {
Vince Lehman76c751c2014-11-18 17:36:38 -0600393 NFD_LOG_DEBUG("Removing invalid face ID: " << faceId);
394
395 scheduler::schedule(time::seconds(0),
396 bind(&RibManager::onFaceDestroyedEvent, this, faceId));
Vince Lehman26b215c2014-08-17 15:00:41 -0500397 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600398 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500399
400 // Reschedule the check for future clean up
401 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500402}
403
404void
Yanbiao Licf0db022016-01-29 00:54:25 -0800405RibManager::onNotification(const FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500406{
Yanbiao Licf0db022016-01-29 00:54:25 -0800407 NFD_LOG_TRACE("onNotification: " << notification);
408
409 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
410 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
411
412 scheduler::schedule(time::seconds(0),
413 bind(&RibManager::onFaceDestroyedEvent, this, notification.getFaceId()));
414 }
415}
416
417void
Junxiao Shib2600172016-07-11 08:53:53 +0000418RibManager::onCommandPrefixAddNextHopSuccess(const Name& prefix,
419 const ndn::nfd::ControlParameters& result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800420{
421 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
422
423 // Routes must be inserted into the RIB so route flags can be applied
424 Route route;
425 route.faceId = result.getFaceId();
426 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
427 route.expires = time::steady_clock::TimePoint::max();
428 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
429
430 m_rib.insert(prefix, route);
431
432 m_registeredFaces.insert(route.faceId);
433}
434
435void
Junxiao Shi29b41282016-08-22 03:47:02 +0000436RibManager::onCommandPrefixAddNextHopError(const Name& name,
437 const ndn::nfd::ControlResponse& response)
Yanbiao Licf0db022016-01-29 00:54:25 -0800438{
Junxiao Shi29b41282016-08-22 03:47:02 +0000439 BOOST_THROW_EXCEPTION(Error("Error in setting interest filter (" + name.toUri() +
440 "): " + response.getText()));
Yanbiao Licf0db022016-01-29 00:54:25 -0800441}
442
443void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000444RibManager::onEnableLocalFieldsSuccess()
Yanbiao Licf0db022016-01-29 00:54:25 -0800445{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000446 NFD_LOG_DEBUG("Local fields enabled");
Yanbiao Licf0db022016-01-29 00:54:25 -0800447}
448
449void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000450RibManager::onEnableLocalFieldsError(const ndn::nfd::ControlResponse& response)
Yanbiao Licf0db022016-01-29 00:54:25 -0800451{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000452 BOOST_THROW_EXCEPTION(Error("Couldn't enable local fields (code: " +
453 to_string(response.getCode()) + ", info: " + response.getText() +
454 ")"));
Vince Lehmancd613c52014-07-30 14:34:49 -0500455}
456
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700457} // namespace rib
458} // namespace nfd