blob: 5b0e0c6222a6b3c0c23290519288f90ec2eddaa2 [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi75306352018-02-01 21:59:44 +00002/*
3 * Copyright (c) 2014-2018, 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
Junxiao Shi75306352018-02-01 21:59:44 +000031#include "core/fib-max-depth.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>
Nick Gordon9fcf1232017-03-10 22:30:20 +000037#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
Junxiao Shi3f21e582017-05-29 15:26:32 +000038#include <ndn-cxx/mgmt/nfd/control-response.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
Davide Pesaventoa3148082018-04-12 18:21:54 -040045NFD_LOG_INIT(RibManager);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070046
Yanbiao Licf0db022016-01-29 00:54:25 -080047const Name RibManager::LOCAL_HOST_TOP_PREFIX = "/localhost/nfd";
48const Name RibManager::LOCAL_HOP_TOP_PREFIX = "/localhop/nfd";
Junxiao Shifde3f542016-07-10 19:54:53 +000049const std::string RibManager::MGMT_MODULE_NAME = "rib";
Vince Lehmancd613c52014-07-30 14:34:49 -050050const Name RibManager::FACES_LIST_DATASET_PREFIX = "/localhost/nfd/faces/list";
Vince Lehman26b215c2014-08-17 15:00:41 -050051const time::seconds RibManager::ACTIVE_FACE_FETCH_INTERVAL = time::seconds(300);
Nick Gordon9fcf1232017-03-10 22:30:20 +000052const Name RibManager::READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
Vince Lehman26b215c2014-08-17 15:00:41 -050053
Yanbiao Licf0db022016-01-29 00:54:25 -080054RibManager::RibManager(Dispatcher& dispatcher,
55 ndn::Face& face,
56 ndn::KeyChain& keyChain)
Junxiao Shifde3f542016-07-10 19:54:53 +000057 : ManagerBase(dispatcher, MGMT_MODULE_NAME)
Yanbiao Licf0db022016-01-29 00:54:25 -080058 , m_face(face)
Vince Lehmanc1dfdb42015-07-16 12:17:36 -050059 , m_keyChain(keyChain)
Junxiao Shi8e273ca2014-11-12 00:42:29 -070060 , m_nfdController(m_face, m_keyChain)
Yanbiao Licf0db022016-01-29 00:54:25 -080061 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070062 , m_localhostValidator(m_face)
63 , m_localhopValidator(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070064 , m_isLocalhopEnabled(false)
Yanbiao Licf0db022016-01-29 00:54:25 -080065 , m_prefixPropagator(m_nfdController, m_keyChain, m_rib)
66 , m_fibUpdater(m_rib, m_nfdController)
67 , m_addTopPrefix([&dispatcher] (const Name& topPrefix) {
68 dispatcher.addTopPrefix(topPrefix, false);
69 })
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070070{
Yanbiao Licf0db022016-01-29 00:54:25 -080071 registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
72 bind(&RibManager::registerEntry, this, _2, _3, _4, _5));
73 registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister",
74 bind(&RibManager::unregisterEntry, this, _2, _3, _4, _5));
75
76 registerStatusDatasetHandler("list", bind(&RibManager::listEntries, this, _1, _2, _3));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070077}
78
Davide Pesaventod396b612017-02-20 22:11:50 -050079RibManager::~RibManager() = default;
Vince Lehman26b215c2014-08-17 15:00:41 -050080
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070081void
82RibManager::registerWithNfd()
83{
Yanbiao Licf0db022016-01-29 00:54:25 -080084 registerTopPrefix(LOCAL_HOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070085
Junxiao Shia3295742014-05-16 22:40:10 -070086 if (m_isLocalhopEnabled) {
Yanbiao Licf0db022016-01-29 00:54:25 -080087 registerTopPrefix(LOCAL_HOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -070088 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070089
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070090 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shifbf78342015-01-23 14:46:41 -070091 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -070092 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -050093
Vince Lehman26b215c2014-08-17 15:00:41 -050094 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070095}
96
97void
Eric Newberryecc45cb2016-11-08 19:57:12 +000098RibManager::enableLocalFields()
Yanbiao Licf0db022016-01-29 00:54:25 -080099{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000100 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
Yanbiao Licf0db022016-01-29 00:54:25 -0800101 ControlParameters()
Eric Newberryecc45cb2016-11-08 19:57:12 +0000102 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true),
103 bind(&RibManager::onEnableLocalFieldsSuccess, this),
104 bind(&RibManager::onEnableLocalFieldsError, this, _1));
Yanbiao Licf0db022016-01-29 00:54:25 -0800105}
106
107void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700108RibManager::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
Yanbiao Licf0db022016-01-29 00:54:25 -0800115RibManager::onRibUpdateSuccess(const RibUpdate& update)
116{
117 NFD_LOG_DEBUG("RIB update succeeded for " << update);
118}
119
120void
121RibManager::onRibUpdateFailure(const RibUpdate& update, uint32_t code, const std::string& error)
122{
123 NFD_LOG_DEBUG("RIB update failed for " << update << " (code: " << code
124 << ", error: " << error << ")");
125
126 // Since the FIB rejected the update, clean up invalid routes
127 scheduleActiveFaceFetch(time::seconds(1));
128}
129
130void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700131RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700132 bool isDryRun,
133 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700134{
Yanbiao Lid7c96362015-01-30 23:58:24 -0800135 bool isAutoPrefixPropagatorEnabled = false;
Nick Gordon9fcf1232017-03-10 22:30:20 +0000136 bool wantReadvertiseToNlsr = false;
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800137
Vince Lehman76c751c2014-11-18 17:36:38 -0600138 for (const auto& item : configSection) {
139 if (item.first == "localhost_security") {
140 m_localhostValidator.load(item.second, filename);
Yingdi Yue5224e92014-04-29 18:04:02 -0700141 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600142 else if (item.first == "localhop_security") {
143 m_localhopValidator.load(item.second, filename);
144 m_isLocalhopEnabled = true;
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800145 }
Yanbiao Lid7c96362015-01-30 23:58:24 -0800146 else if (item.first == "auto_prefix_propagate") {
147 m_prefixPropagator.loadConfig(item.second);
148 isAutoPrefixPropagatorEnabled = true;
Vince Lehman76c751c2014-11-18 17:36:38 -0600149
150 // Avoid other actions when isDryRun == true
151 if (isDryRun) {
152 continue;
153 }
154
Yanbiao Lid7c96362015-01-30 23:58:24 -0800155 m_prefixPropagator.enable();
Vince Lehman76c751c2014-11-18 17:36:38 -0600156 }
Nick Gordon9fcf1232017-03-10 22:30:20 +0000157 else if (item.first == "readvertise_nlsr") {
158 wantReadvertiseToNlsr = ConfigFile::parseYesNo(item, "rib.readvertise_nlsr");
159 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600160 else {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700161 BOOST_THROW_EXCEPTION(Error("Unrecognized rib property: " + item.first));
Vince Lehman76c751c2014-11-18 17:36:38 -0600162 }
163 }
164
Yanbiao Lid7c96362015-01-30 23:58:24 -0800165 if (!isAutoPrefixPropagatorEnabled) {
166 m_prefixPropagator.disable();
Vince Lehman76c751c2014-11-18 17:36:38 -0600167 }
Nick Gordon9fcf1232017-03-10 22:30:20 +0000168
169 if (wantReadvertiseToNlsr && m_readvertiseNlsr == nullptr) {
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400170 NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
171 m_readvertiseNlsr = make_unique<Readvertise>(
Nick Gordon9fcf1232017-03-10 22:30:20 +0000172 m_rib,
173 make_unique<ClientToNlsrReadvertisePolicy>(),
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400174 make_unique<NfdRibReadvertiseDestination>(m_nfdController, READVERTISE_NLSR_PREFIX, m_rib));
Nick Gordon9fcf1232017-03-10 22:30:20 +0000175 }
176 else if (!wantReadvertiseToNlsr && m_readvertiseNlsr != nullptr) {
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400177 NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
Nick Gordon9fcf1232017-03-10 22:30:20 +0000178 m_readvertiseNlsr.reset();
179 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700180}
181
182void
Yanbiao Licf0db022016-01-29 00:54:25 -0800183RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700184{
Yanbiao Licf0db022016-01-29 00:54:25 -0800185 // register entry to the FIB
186 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400187 ControlParameters()
188 .setName(Name(topPrefix).append(MGMT_MODULE_NAME))
189 .setFaceId(0),
190 [=] (const auto& res) { this->onCommandPrefixAddNextHopSuccess(topPrefix, res); },
191 [=] (const auto& res) { this->onCommandPrefixAddNextHopError(topPrefix, res); });
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700192
Yanbiao Licf0db022016-01-29 00:54:25 -0800193 // add top prefix to the dispatcher
194 m_addTopPrefix(topPrefix);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700195}
196
197void
Yanbiao Licf0db022016-01-29 00:54:25 -0800198RibManager::registerEntry(const Name& topPrefix, const Interest& interest,
199 ControlParameters parameters,
200 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700201{
Junxiao Shi75306352018-02-01 21:59:44 +0000202 if (parameters.getName().size() > FIB_MAX_DEPTH) {
203 done(ControlResponse(414, "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) +
204 " components"));
205 return;
206 }
207
Yanbiao Licf0db022016-01-29 00:54:25 -0800208 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600209
210 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800211 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700212
Vince Lehman218be0a2015-01-15 17:25:20 -0600213 Route route;
214 route.faceId = parameters.getFaceId();
215 route.origin = parameters.getOrigin();
216 route.cost = parameters.getCost();
217 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500218
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700219 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000220 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Vince Lehman76c751c2014-11-18 17:36:38 -0600221 route.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
Syed Obaid3313a372014-07-01 01:31:33 -0500222
Vince Lehman76c751c2014-11-18 17:36:38 -0600223 // Schedule a new event, the old one will be cancelled during rib insertion.
224 scheduler::EventId eventId = scheduler::schedule(parameters.getExpirationPeriod(),
Yanbiao Licf0db022016-01-29 00:54:25 -0800225 bind(&Rib::onRouteExpiration, &m_rib, parameters.getName(), route));
Syed Obaid3313a372014-07-01 01:31:33 -0500226
Junxiao Shi3f21e582017-05-29 15:26:32 +0000227 NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires <<
Vince Lehman76c751c2014-11-18 17:36:38 -0600228 " with EventId: " << eventId);
229
230 // Set the NewEventId of this entry
231 route.setExpirationEvent(eventId);
232 }
233 else {
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400234 route.expires = nullopt;
Vince Lehman76c751c2014-11-18 17:36:38 -0600235 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700236
Vince Lehmanff8b3972015-02-20 16:51:21 -0600237 NFD_LOG_INFO("Adding route " << parameters.getName() << " nexthop=" << route.faceId
238 << " origin=" << route.origin
239 << " cost=" << route.cost);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700240
Vince Lehman76c751c2014-11-18 17:36:38 -0600241 RibUpdate update;
242 update.setAction(RibUpdate::REGISTER)
243 .setName(parameters.getName())
244 .setRoute(route);
245
Yanbiao Licf0db022016-01-29 00:54:25 -0800246 m_rib.beginApplyUpdate(update,
Junxiao Shib2600172016-07-11 08:53:53 +0000247 bind(&RibManager::onRibUpdateSuccess, this, update),
248 bind(&RibManager::onRibUpdateFailure, this, update, _1, _2));
Vince Lehman76c751c2014-11-18 17:36:38 -0600249
Vince Lehman218be0a2015-01-15 17:25:20 -0600250 m_registeredFaces.insert(route.faceId);
Vince Lehman281ded72014-08-21 12:17:08 -0500251}
252
253void
Yanbiao Licf0db022016-01-29 00:54:25 -0800254RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest,
255 ControlParameters parameters,
256 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700257{
Yanbiao Licf0db022016-01-29 00:54:25 -0800258 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600259
260 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800261 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700262
Vince Lehman218be0a2015-01-15 17:25:20 -0600263 Route route;
264 route.faceId = parameters.getFaceId();
265 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700266
Vince Lehmanff8b3972015-02-20 16:51:21 -0600267 NFD_LOG_INFO("Removing route " << parameters.getName() << " nexthop=" << route.faceId
268 << " origin=" << route.origin);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700269
Vince Lehman76c751c2014-11-18 17:36:38 -0600270 RibUpdate update;
271 update.setAction(RibUpdate::UNREGISTER)
272 .setName(parameters.getName())
273 .setRoute(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500274
Yanbiao Licf0db022016-01-29 00:54:25 -0800275 m_rib.beginApplyUpdate(update,
Junxiao Shib2600172016-07-11 08:53:53 +0000276 bind(&RibManager::onRibUpdateSuccess, this, update),
277 bind(&RibManager::onRibUpdateFailure, this, update, _1, _2));
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700278}
279
280void
Yanbiao Licf0db022016-01-29 00:54:25 -0800281RibManager::listEntries(const Name& topPrefix, const Interest& interest,
282 ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700283{
Junxiao Shi3f21e582017-05-29 15:26:32 +0000284 auto now = time::steady_clock::now();
285 for (const auto& kv : m_rib) {
286 const RibEntry& entry = *kv.second;
287 ndn::nfd::RibEntry item;
288 item.setName(entry.getName());
289 for (const Route& route : entry.getRoutes()) {
290 ndn::nfd::Route r;
291 r.setFaceId(route.faceId);
292 r.setOrigin(route.origin);
293 r.setCost(route.cost);
294 r.setFlags(route.flags);
295 if (route.expires) {
296 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now));
297 }
298 item.addRoute(r);
299 }
300 context.append(item.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600301 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800302 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700303}
304
305void
Yanbiao Licf0db022016-01-29 00:54:25 -0800306RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700307{
Yanbiao Licf0db022016-01-29 00:54:25 -0800308 bool isSelfRegistration = (parameters.getFaceId() == 0);
309 if (isSelfRegistration) {
310 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
311 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
312 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
313 // and is initialized synchronously with IncomingFaceId field enabled.
314 BOOST_ASSERT(incomingFaceIdTag != nullptr);
315 parameters.setFaceId(*incomingFaceIdTag);
Vince Lehman76c751c2014-11-18 17:36:38 -0600316 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700317}
318
Junxiao Shi21738402016-08-19 19:48:00 +0000319ndn::mgmt::Authorization
320RibManager::makeAuthorization(const std::string& verb)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700321{
Junxiao Shi21738402016-08-19 19:48:00 +0000322 return [this] (const Name& prefix, const Interest& interest,
323 const ndn::mgmt::ControlParameters* params,
324 const ndn::mgmt::AcceptContinuation& accept,
325 const ndn::mgmt::RejectContinuation& reject) {
326 BOOST_ASSERT(params != nullptr);
327 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters));
328 BOOST_ASSERT(prefix == LOCAL_HOST_TOP_PREFIX || prefix == LOCAL_HOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700329
Junxiao Shi21738402016-08-19 19:48:00 +0000330 ndn::ValidatorConfig& validator = prefix == LOCAL_HOST_TOP_PREFIX ?
331 m_localhostValidator : m_localhopValidator;
332 validator.validate(interest,
333 bind([&interest, this, accept] { extractRequester(interest, accept); }),
334 bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); }));
335 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500336}
337
338void
Vince Lehmancd613c52014-07-30 14:34:49 -0500339RibManager::fetchActiveFaces()
340{
341 NFD_LOG_DEBUG("Fetching active faces");
342
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700343 m_nfdController.fetch<ndn::nfd::FaceDataset>(
344 bind(&RibManager::removeInvalidFaces, this, _1),
345 bind(&RibManager::onFetchActiveFacesFailure, this, _1, _2),
346 ndn::nfd::CommandOptions());
Vince Lehmancd613c52014-07-30 14:34:49 -0500347}
348
349void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700350RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason)
Vince Lehmancd613c52014-07-30 14:34:49 -0500351{
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700352 NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason);
Yanbiao Licf0db022016-01-29 00:54:25 -0800353 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
354}
355
356void
357RibManager::onFaceDestroyedEvent(uint64_t faceId)
358{
359 m_rib.beginRemoveFace(faceId);
360 m_registeredFaces.erase(faceId);
361}
362
363void
364RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
365{
Davide Pesaventod396b612017-02-20 22:11:50 -0500366 m_activeFaceFetchEvent = scheduler::schedule(timeToWait, [this] { this->fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800367}
368
369void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700370RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500371{
Vince Lehman26b215c2014-08-17 15:00:41 -0500372 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500373
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700374 FaceIdSet activeFaceIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500375 for (const auto& faceStatus : activeFaces) {
376 activeFaceIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700377 }
378
Vince Lehman26b215c2014-08-17 15:00:41 -0500379 // Look for face IDs that were registered but not active to find missed
380 // face destroyed events
Davide Pesaventod396b612017-02-20 22:11:50 -0500381 for (auto faceId : m_registeredFaces) {
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700382 if (activeFaceIds.count(faceId) == 0) {
Vince Lehman76c751c2014-11-18 17:36:38 -0600383 NFD_LOG_DEBUG("Removing invalid face ID: " << faceId);
Davide Pesaventod396b612017-02-20 22:11:50 -0500384 scheduler::schedule(time::seconds(0), [this, faceId] { this->onFaceDestroyedEvent(faceId); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500385 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600386 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500387
388 // Reschedule the check for future clean up
389 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500390}
391
392void
Davide Pesaventod396b612017-02-20 22:11:50 -0500393RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500394{
Yanbiao Licf0db022016-01-29 00:54:25 -0800395 NFD_LOG_TRACE("onNotification: " << notification);
396
397 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
398 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
399
400 scheduler::schedule(time::seconds(0),
401 bind(&RibManager::onFaceDestroyedEvent, this, notification.getFaceId()));
402 }
403}
404
405void
Junxiao Shib2600172016-07-11 08:53:53 +0000406RibManager::onCommandPrefixAddNextHopSuccess(const Name& prefix,
407 const ndn::nfd::ControlParameters& result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800408{
409 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
410
411 // Routes must be inserted into the RIB so route flags can be applied
412 Route route;
413 route.faceId = result.getFaceId();
414 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400415 route.expires = nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800416 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
417
418 m_rib.insert(prefix, route);
419
420 m_registeredFaces.insert(route.faceId);
421}
422
423void
Junxiao Shi29b41282016-08-22 03:47:02 +0000424RibManager::onCommandPrefixAddNextHopError(const Name& name,
425 const ndn::nfd::ControlResponse& response)
Yanbiao Licf0db022016-01-29 00:54:25 -0800426{
Junxiao Shi29b41282016-08-22 03:47:02 +0000427 BOOST_THROW_EXCEPTION(Error("Error in setting interest filter (" + name.toUri() +
428 "): " + response.getText()));
Yanbiao Licf0db022016-01-29 00:54:25 -0800429}
430
431void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000432RibManager::onEnableLocalFieldsSuccess()
Yanbiao Licf0db022016-01-29 00:54:25 -0800433{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000434 NFD_LOG_DEBUG("Local fields enabled");
Yanbiao Licf0db022016-01-29 00:54:25 -0800435}
436
437void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000438RibManager::onEnableLocalFieldsError(const ndn::nfd::ControlResponse& response)
Yanbiao Licf0db022016-01-29 00:54:25 -0800439{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000440 BOOST_THROW_EXCEPTION(Error("Couldn't enable local fields (code: " +
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400441 to_string(response.getCode()) + ", info: " + response.getText() + ")"));
Vince Lehmancd613c52014-07-30 14:34:49 -0500442}
443
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700444} // namespace rib
445} // namespace nfd