blob: 71a7cf3d44cb17f3b8910815152577a46845b1e9 [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 Afanasyev89cf5e02014-04-17 12:08:57 -070031#include "core/logger.hpp"
Alexander Afanasyev63108c42014-07-07 19:10:47 -070032#include "core/scheduler.hpp"
Nick Gordon9fcf1232017-03-10 22:30:20 +000033
Junxiao Shicbc8e942016-09-06 03:17:45 +000034#include <ndn-cxx/lp/tags.hpp>
Nick Gordon9fcf1232017-03-10 22:30:20 +000035#include <ndn-cxx/mgmt/nfd/control-command.hpp>
Nick Gordon9fcf1232017-03-10 22:30:20 +000036#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
Junxiao Shi3f21e582017-05-29 15:26:32 +000037#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000038#include <ndn-cxx/mgmt/nfd/face-status.hpp>
39#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070040
41namespace nfd {
42namespace rib {
43
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070044NFD_LOG_INIT("RibManager");
45
Yanbiao Licf0db022016-01-29 00:54:25 -080046const Name RibManager::LOCAL_HOST_TOP_PREFIX = "/localhost/nfd";
47const Name RibManager::LOCAL_HOP_TOP_PREFIX = "/localhop/nfd";
Junxiao Shifde3f542016-07-10 19:54:53 +000048const std::string RibManager::MGMT_MODULE_NAME = "rib";
Vince Lehmancd613c52014-07-30 14:34:49 -050049const Name RibManager::FACES_LIST_DATASET_PREFIX = "/localhost/nfd/faces/list";
Vince Lehman26b215c2014-08-17 15:00:41 -050050const time::seconds RibManager::ACTIVE_FACE_FETCH_INTERVAL = time::seconds(300);
Nick Gordon9fcf1232017-03-10 22:30:20 +000051const Name RibManager::READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
Vince Lehman26b215c2014-08-17 15:00:41 -050052
Yanbiao Licf0db022016-01-29 00:54:25 -080053RibManager::RibManager(Dispatcher& dispatcher,
54 ndn::Face& face,
55 ndn::KeyChain& keyChain)
Junxiao Shifde3f542016-07-10 19:54:53 +000056 : ManagerBase(dispatcher, MGMT_MODULE_NAME)
Yanbiao Licf0db022016-01-29 00:54:25 -080057 , m_face(face)
Vince Lehmanc1dfdb42015-07-16 12:17:36 -050058 , m_keyChain(keyChain)
Junxiao Shi8e273ca2014-11-12 00:42:29 -070059 , m_nfdController(m_face, m_keyChain)
Yanbiao Licf0db022016-01-29 00:54:25 -080060 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070061 , m_localhostValidator(m_face)
62 , m_localhopValidator(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070063 , m_isLocalhopEnabled(false)
Yanbiao Licf0db022016-01-29 00:54:25 -080064 , m_prefixPropagator(m_nfdController, m_keyChain, m_rib)
65 , m_fibUpdater(m_rib, m_nfdController)
66 , m_addTopPrefix([&dispatcher] (const Name& topPrefix) {
67 dispatcher.addTopPrefix(topPrefix, false);
68 })
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070069{
Yanbiao Licf0db022016-01-29 00:54:25 -080070 registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
71 bind(&RibManager::registerEntry, this, _2, _3, _4, _5));
72 registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister",
73 bind(&RibManager::unregisterEntry, this, _2, _3, _4, _5));
74
75 registerStatusDatasetHandler("list", bind(&RibManager::listEntries, this, _1, _2, _3));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070076}
77
Davide Pesaventod396b612017-02-20 22:11:50 -050078RibManager::~RibManager() = default;
Vince Lehman26b215c2014-08-17 15:00:41 -050079
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070080void
81RibManager::registerWithNfd()
82{
Yanbiao Licf0db022016-01-29 00:54:25 -080083 registerTopPrefix(LOCAL_HOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070084
Junxiao Shia3295742014-05-16 22:40:10 -070085 if (m_isLocalhopEnabled) {
Yanbiao Licf0db022016-01-29 00:54:25 -080086 registerTopPrefix(LOCAL_HOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -070087 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070088
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070089 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shifbf78342015-01-23 14:46:41 -070090 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -070091 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -050092
Vince Lehman26b215c2014-08-17 15:00:41 -050093 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070094}
95
96void
Eric Newberryecc45cb2016-11-08 19:57:12 +000097RibManager::enableLocalFields()
Yanbiao Licf0db022016-01-29 00:54:25 -080098{
Eric Newberryecc45cb2016-11-08 19:57:12 +000099 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
Yanbiao Licf0db022016-01-29 00:54:25 -0800100 ControlParameters()
Eric Newberryecc45cb2016-11-08 19:57:12 +0000101 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true),
102 bind(&RibManager::onEnableLocalFieldsSuccess, this),
103 bind(&RibManager::onEnableLocalFieldsError, this, _1));
Yanbiao Licf0db022016-01-29 00:54:25 -0800104}
105
106void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700107RibManager::setConfigFile(ConfigFile& configFile)
108{
Yingdi Yue5224e92014-04-29 18:04:02 -0700109 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700110 bind(&RibManager::onConfig, this, _1, _2, _3));
111}
112
113void
Yanbiao Licf0db022016-01-29 00:54:25 -0800114RibManager::onRibUpdateSuccess(const RibUpdate& update)
115{
116 NFD_LOG_DEBUG("RIB update succeeded for " << update);
117}
118
119void
120RibManager::onRibUpdateFailure(const RibUpdate& update, uint32_t code, const std::string& error)
121{
122 NFD_LOG_DEBUG("RIB update failed for " << update << " (code: " << code
123 << ", error: " << error << ")");
124
125 // Since the FIB rejected the update, clean up invalid routes
126 scheduleActiveFaceFetch(time::seconds(1));
127}
128
129void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700130RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700131 bool isDryRun,
132 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700133{
Yanbiao Lid7c96362015-01-30 23:58:24 -0800134 bool isAutoPrefixPropagatorEnabled = false;
Nick Gordon9fcf1232017-03-10 22:30:20 +0000135 bool wantReadvertiseToNlsr = false;
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800136
Vince Lehman76c751c2014-11-18 17:36:38 -0600137 for (const auto& item : configSection) {
138 if (item.first == "localhost_security") {
139 m_localhostValidator.load(item.second, filename);
Yingdi Yue5224e92014-04-29 18:04:02 -0700140 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600141 else if (item.first == "localhop_security") {
142 m_localhopValidator.load(item.second, filename);
143 m_isLocalhopEnabled = true;
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800144 }
Yanbiao Lid7c96362015-01-30 23:58:24 -0800145 else if (item.first == "auto_prefix_propagate") {
146 m_prefixPropagator.loadConfig(item.second);
147 isAutoPrefixPropagatorEnabled = true;
Vince Lehman76c751c2014-11-18 17:36:38 -0600148
149 // Avoid other actions when isDryRun == true
150 if (isDryRun) {
151 continue;
152 }
153
Yanbiao Lid7c96362015-01-30 23:58:24 -0800154 m_prefixPropagator.enable();
Vince Lehman76c751c2014-11-18 17:36:38 -0600155 }
Nick Gordon9fcf1232017-03-10 22:30:20 +0000156 else if (item.first == "readvertise_nlsr") {
157 wantReadvertiseToNlsr = ConfigFile::parseYesNo(item, "rib.readvertise_nlsr");
158 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600159 else {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700160 BOOST_THROW_EXCEPTION(Error("Unrecognized rib property: " + item.first));
Vince Lehman76c751c2014-11-18 17:36:38 -0600161 }
162 }
163
Yanbiao Lid7c96362015-01-30 23:58:24 -0800164 if (!isAutoPrefixPropagatorEnabled) {
165 m_prefixPropagator.disable();
Vince Lehman76c751c2014-11-18 17:36:38 -0600166 }
Nick Gordon9fcf1232017-03-10 22:30:20 +0000167
168 if (wantReadvertiseToNlsr && m_readvertiseNlsr == nullptr) {
169 NFD_LOG_DEBUG("Enabling readvertise-to-nlsr.");
170 m_readvertiseNlsr.reset(new Readvertise(
171 m_rib,
172 make_unique<ClientToNlsrReadvertisePolicy>(),
173 make_unique<NfdRibReadvertiseDestination>(m_nfdController, READVERTISE_NLSR_PREFIX, m_rib)));
174 }
175 else if (!wantReadvertiseToNlsr && m_readvertiseNlsr != nullptr) {
176 NFD_LOG_DEBUG("Disabling readvertise-to-nlsr.");
177 m_readvertiseNlsr.reset();
178 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700179}
180
181void
Yanbiao Licf0db022016-01-29 00:54:25 -0800182RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700183{
Yanbiao Licf0db022016-01-29 00:54:25 -0800184 // register entry to the FIB
185 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
186 ControlParameters()
Junxiao Shifde3f542016-07-10 19:54:53 +0000187 .setName(Name(topPrefix).append(MGMT_MODULE_NAME))
Yanbiao Licf0db022016-01-29 00:54:25 -0800188 .setFaceId(0),
Junxiao Shib2600172016-07-11 08:53:53 +0000189 bind(&RibManager::onCommandPrefixAddNextHopSuccess, this, cref(topPrefix), _1),
Junxiao Shi29b41282016-08-22 03:47:02 +0000190 bind(&RibManager::onCommandPrefixAddNextHopError, this, cref(topPrefix), _1));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700191
Yanbiao Licf0db022016-01-29 00:54:25 -0800192 // add top prefix to the dispatcher
193 m_addTopPrefix(topPrefix);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700194}
195
196void
Yanbiao Licf0db022016-01-29 00:54:25 -0800197RibManager::registerEntry(const Name& topPrefix, const Interest& interest,
198 ControlParameters parameters,
199 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700200{
Yanbiao Licf0db022016-01-29 00:54:25 -0800201 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600202
203 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800204 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700205
Vince Lehman218be0a2015-01-15 17:25:20 -0600206 Route route;
207 route.faceId = parameters.getFaceId();
208 route.origin = parameters.getOrigin();
209 route.cost = parameters.getCost();
210 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500211
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700212 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000213 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Vince Lehman76c751c2014-11-18 17:36:38 -0600214 route.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
Syed Obaid3313a372014-07-01 01:31:33 -0500215
Vince Lehman76c751c2014-11-18 17:36:38 -0600216 // Schedule a new event, the old one will be cancelled during rib insertion.
217 scheduler::EventId eventId = scheduler::schedule(parameters.getExpirationPeriod(),
Yanbiao Licf0db022016-01-29 00:54:25 -0800218 bind(&Rib::onRouteExpiration, &m_rib, parameters.getName(), route));
Syed Obaid3313a372014-07-01 01:31:33 -0500219
Junxiao Shi3f21e582017-05-29 15:26:32 +0000220 NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires <<
Vince Lehman76c751c2014-11-18 17:36:38 -0600221 " with EventId: " << eventId);
222
223 // Set the NewEventId of this entry
224 route.setExpirationEvent(eventId);
225 }
226 else {
Junxiao Shi3f21e582017-05-29 15:26:32 +0000227 route.expires = ndn::nullopt;
Vince Lehman76c751c2014-11-18 17:36:38 -0600228 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700229
Vince Lehmanff8b3972015-02-20 16:51:21 -0600230 NFD_LOG_INFO("Adding route " << parameters.getName() << " nexthop=" << route.faceId
231 << " origin=" << route.origin
232 << " cost=" << route.cost);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700233
Vince Lehman76c751c2014-11-18 17:36:38 -0600234 RibUpdate update;
235 update.setAction(RibUpdate::REGISTER)
236 .setName(parameters.getName())
237 .setRoute(route);
238
Yanbiao Licf0db022016-01-29 00:54:25 -0800239 m_rib.beginApplyUpdate(update,
Junxiao Shib2600172016-07-11 08:53:53 +0000240 bind(&RibManager::onRibUpdateSuccess, this, update),
241 bind(&RibManager::onRibUpdateFailure, this, update, _1, _2));
Vince Lehman76c751c2014-11-18 17:36:38 -0600242
Vince Lehman218be0a2015-01-15 17:25:20 -0600243 m_registeredFaces.insert(route.faceId);
Vince Lehman281ded72014-08-21 12:17:08 -0500244}
245
246void
Yanbiao Licf0db022016-01-29 00:54:25 -0800247RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest,
248 ControlParameters parameters,
249 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700250{
Yanbiao Licf0db022016-01-29 00:54:25 -0800251 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600252
253 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800254 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700255
Vince Lehman218be0a2015-01-15 17:25:20 -0600256 Route route;
257 route.faceId = parameters.getFaceId();
258 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700259
Vince Lehmanff8b3972015-02-20 16:51:21 -0600260 NFD_LOG_INFO("Removing route " << parameters.getName() << " nexthop=" << route.faceId
261 << " origin=" << route.origin);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700262
Vince Lehman76c751c2014-11-18 17:36:38 -0600263 RibUpdate update;
264 update.setAction(RibUpdate::UNREGISTER)
265 .setName(parameters.getName())
266 .setRoute(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500267
Yanbiao Licf0db022016-01-29 00:54:25 -0800268 m_rib.beginApplyUpdate(update,
Junxiao Shib2600172016-07-11 08:53:53 +0000269 bind(&RibManager::onRibUpdateSuccess, this, update),
270 bind(&RibManager::onRibUpdateFailure, this, update, _1, _2));
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700271}
272
273void
Yanbiao Licf0db022016-01-29 00:54:25 -0800274RibManager::listEntries(const Name& topPrefix, const Interest& interest,
275 ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700276{
Junxiao Shi3f21e582017-05-29 15:26:32 +0000277 auto now = time::steady_clock::now();
278 for (const auto& kv : m_rib) {
279 const RibEntry& entry = *kv.second;
280 ndn::nfd::RibEntry item;
281 item.setName(entry.getName());
282 for (const Route& route : entry.getRoutes()) {
283 ndn::nfd::Route r;
284 r.setFaceId(route.faceId);
285 r.setOrigin(route.origin);
286 r.setCost(route.cost);
287 r.setFlags(route.flags);
288 if (route.expires) {
289 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now));
290 }
291 item.addRoute(r);
292 }
293 context.append(item.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600294 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800295 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700296}
297
298void
Yanbiao Licf0db022016-01-29 00:54:25 -0800299RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700300{
Yanbiao Licf0db022016-01-29 00:54:25 -0800301 bool isSelfRegistration = (parameters.getFaceId() == 0);
302 if (isSelfRegistration) {
303 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
304 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
305 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
306 // and is initialized synchronously with IncomingFaceId field enabled.
307 BOOST_ASSERT(incomingFaceIdTag != nullptr);
308 parameters.setFaceId(*incomingFaceIdTag);
Vince Lehman76c751c2014-11-18 17:36:38 -0600309 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700310}
311
Junxiao Shi21738402016-08-19 19:48:00 +0000312ndn::mgmt::Authorization
313RibManager::makeAuthorization(const std::string& verb)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700314{
Junxiao Shi21738402016-08-19 19:48:00 +0000315 return [this] (const Name& prefix, const Interest& interest,
316 const ndn::mgmt::ControlParameters* params,
317 const ndn::mgmt::AcceptContinuation& accept,
318 const ndn::mgmt::RejectContinuation& reject) {
319 BOOST_ASSERT(params != nullptr);
320 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters));
321 BOOST_ASSERT(prefix == LOCAL_HOST_TOP_PREFIX || prefix == LOCAL_HOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700322
Junxiao Shi21738402016-08-19 19:48:00 +0000323 ndn::ValidatorConfig& validator = prefix == LOCAL_HOST_TOP_PREFIX ?
324 m_localhostValidator : m_localhopValidator;
325 validator.validate(interest,
326 bind([&interest, this, accept] { extractRequester(interest, accept); }),
327 bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); }));
328 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500329}
330
331void
Vince Lehmancd613c52014-07-30 14:34:49 -0500332RibManager::fetchActiveFaces()
333{
334 NFD_LOG_DEBUG("Fetching active faces");
335
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700336 m_nfdController.fetch<ndn::nfd::FaceDataset>(
337 bind(&RibManager::removeInvalidFaces, this, _1),
338 bind(&RibManager::onFetchActiveFacesFailure, this, _1, _2),
339 ndn::nfd::CommandOptions());
Vince Lehmancd613c52014-07-30 14:34:49 -0500340}
341
342void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700343RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason)
Vince Lehmancd613c52014-07-30 14:34:49 -0500344{
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700345 NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason);
Yanbiao Licf0db022016-01-29 00:54:25 -0800346 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
347}
348
349void
350RibManager::onFaceDestroyedEvent(uint64_t faceId)
351{
352 m_rib.beginRemoveFace(faceId);
353 m_registeredFaces.erase(faceId);
354}
355
356void
357RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
358{
Davide Pesaventod396b612017-02-20 22:11:50 -0500359 m_activeFaceFetchEvent = scheduler::schedule(timeToWait, [this] { this->fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800360}
361
362void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700363RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500364{
Vince Lehman26b215c2014-08-17 15:00:41 -0500365 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500366
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700367 FaceIdSet activeFaceIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500368 for (const auto& faceStatus : activeFaces) {
369 activeFaceIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700370 }
371
Vince Lehman26b215c2014-08-17 15:00:41 -0500372 // Look for face IDs that were registered but not active to find missed
373 // face destroyed events
Davide Pesaventod396b612017-02-20 22:11:50 -0500374 for (auto faceId : m_registeredFaces) {
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700375 if (activeFaceIds.count(faceId) == 0) {
Vince Lehman76c751c2014-11-18 17:36:38 -0600376 NFD_LOG_DEBUG("Removing invalid face ID: " << faceId);
Davide Pesaventod396b612017-02-20 22:11:50 -0500377 scheduler::schedule(time::seconds(0), [this, faceId] { this->onFaceDestroyedEvent(faceId); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500378 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600379 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500380
381 // Reschedule the check for future clean up
382 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500383}
384
385void
Davide Pesaventod396b612017-02-20 22:11:50 -0500386RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500387{
Yanbiao Licf0db022016-01-29 00:54:25 -0800388 NFD_LOG_TRACE("onNotification: " << notification);
389
390 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
391 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
392
393 scheduler::schedule(time::seconds(0),
394 bind(&RibManager::onFaceDestroyedEvent, this, notification.getFaceId()));
395 }
396}
397
398void
Junxiao Shib2600172016-07-11 08:53:53 +0000399RibManager::onCommandPrefixAddNextHopSuccess(const Name& prefix,
400 const ndn::nfd::ControlParameters& result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800401{
402 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
403
404 // Routes must be inserted into the RIB so route flags can be applied
405 Route route;
406 route.faceId = result.getFaceId();
407 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000408 route.expires = ndn::nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800409 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
410
411 m_rib.insert(prefix, route);
412
413 m_registeredFaces.insert(route.faceId);
414}
415
416void
Junxiao Shi29b41282016-08-22 03:47:02 +0000417RibManager::onCommandPrefixAddNextHopError(const Name& name,
418 const ndn::nfd::ControlResponse& response)
Yanbiao Licf0db022016-01-29 00:54:25 -0800419{
Junxiao Shi29b41282016-08-22 03:47:02 +0000420 BOOST_THROW_EXCEPTION(Error("Error in setting interest filter (" + name.toUri() +
421 "): " + response.getText()));
Yanbiao Licf0db022016-01-29 00:54:25 -0800422}
423
424void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000425RibManager::onEnableLocalFieldsSuccess()
Yanbiao Licf0db022016-01-29 00:54:25 -0800426{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000427 NFD_LOG_DEBUG("Local fields enabled");
Yanbiao Licf0db022016-01-29 00:54:25 -0800428}
429
430void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000431RibManager::onEnableLocalFieldsError(const ndn::nfd::ControlResponse& response)
Yanbiao Licf0db022016-01-29 00:54:25 -0800432{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000433 BOOST_THROW_EXCEPTION(Error("Couldn't enable local fields (code: " +
434 to_string(response.getCode()) + ", info: " + response.getText() +
435 ")"));
Vince Lehmancd613c52014-07-30 14:34:49 -0500436}
437
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700438} // namespace rib
439} // namespace nfd