blob: dd0abcacf0894512a2c886b9827c6b33eb22f5e9 [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Licf0db022016-01-29 00:54:25 -08003 * Copyright (c) 2014-2016, 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"
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>
Yanbiao Licf0db022016-01-29 00:54:25 -080031#include <ndn-cxx/management/nfd-rib-entry.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070032
33namespace nfd {
34namespace rib {
35
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070036NFD_LOG_INIT("RibManager");
37
Yanbiao Licf0db022016-01-29 00:54:25 -080038const Name RibManager::LOCAL_HOST_TOP_PREFIX = "/localhost/nfd";
39const Name RibManager::LOCAL_HOP_TOP_PREFIX = "/localhop/nfd";
Junxiao Shifde3f542016-07-10 19:54:53 +000040const std::string RibManager::MGMT_MODULE_NAME = "rib";
Vince Lehmancd613c52014-07-30 14:34:49 -050041const Name RibManager::FACES_LIST_DATASET_PREFIX = "/localhost/nfd/faces/list";
Vince Lehman26b215c2014-08-17 15:00:41 -050042const time::seconds RibManager::ACTIVE_FACE_FETCH_INTERVAL = time::seconds(300);
43
Yanbiao Licf0db022016-01-29 00:54:25 -080044RibManager::RibManager(Dispatcher& dispatcher,
45 ndn::Face& face,
46 ndn::KeyChain& keyChain)
Junxiao Shifde3f542016-07-10 19:54:53 +000047 : ManagerBase(dispatcher, MGMT_MODULE_NAME)
Yanbiao Licf0db022016-01-29 00:54:25 -080048 , m_face(face)
Vince Lehmanc1dfdb42015-07-16 12:17:36 -050049 , m_keyChain(keyChain)
Junxiao Shi8e273ca2014-11-12 00:42:29 -070050 , m_nfdController(m_face, m_keyChain)
Yanbiao Licf0db022016-01-29 00:54:25 -080051 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070052 , m_localhostValidator(m_face)
53 , m_localhopValidator(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070054 , m_isLocalhopEnabled(false)
Yanbiao Licf0db022016-01-29 00:54:25 -080055 , m_prefixPropagator(m_nfdController, m_keyChain, m_rib)
56 , m_fibUpdater(m_rib, m_nfdController)
57 , m_addTopPrefix([&dispatcher] (const Name& topPrefix) {
58 dispatcher.addTopPrefix(topPrefix, false);
59 })
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070060{
Yanbiao Licf0db022016-01-29 00:54:25 -080061 registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
62 bind(&RibManager::registerEntry, this, _2, _3, _4, _5));
63 registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister",
64 bind(&RibManager::unregisterEntry, this, _2, _3, _4, _5));
65
66 registerStatusDatasetHandler("list", bind(&RibManager::listEntries, this, _1, _2, _3));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070067}
68
Vince Lehman26b215c2014-08-17 15:00:41 -050069RibManager::~RibManager()
70{
71 scheduler::cancel(m_activeFaceFetchEvent);
72}
73
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070074void
75RibManager::registerWithNfd()
76{
Yanbiao Licf0db022016-01-29 00:54:25 -080077 registerTopPrefix(LOCAL_HOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070078
Junxiao Shia3295742014-05-16 22:40:10 -070079 if (m_isLocalhopEnabled) {
Yanbiao Licf0db022016-01-29 00:54:25 -080080 registerTopPrefix(LOCAL_HOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -070081 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070082
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070083 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shifbf78342015-01-23 14:46:41 -070084 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -070085 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -050086
Vince Lehman26b215c2014-08-17 15:00:41 -050087 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070088}
89
90void
Yanbiao Licf0db022016-01-29 00:54:25 -080091RibManager::enableLocalControlHeader()
92{
93 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
94 ControlParameters()
95 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
96 bind(&RibManager::onControlHeaderSuccess, this),
Junxiao Shi29b41282016-08-22 03:47:02 +000097 bind(&RibManager::onControlHeaderError, this, _1));
Yanbiao Licf0db022016-01-29 00:54:25 -080098}
99
100void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700101RibManager::setConfigFile(ConfigFile& configFile)
102{
Yingdi Yue5224e92014-04-29 18:04:02 -0700103 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700104 bind(&RibManager::onConfig, this, _1, _2, _3));
105}
106
107void
Yanbiao Licf0db022016-01-29 00:54:25 -0800108RibManager::onRibUpdateSuccess(const RibUpdate& update)
109{
110 NFD_LOG_DEBUG("RIB update succeeded for " << update);
111}
112
113void
114RibManager::onRibUpdateFailure(const RibUpdate& update, uint32_t code, const std::string& error)
115{
116 NFD_LOG_DEBUG("RIB update failed for " << update << " (code: " << code
117 << ", error: " << error << ")");
118
119 // Since the FIB rejected the update, clean up invalid routes
120 scheduleActiveFaceFetch(time::seconds(1));
121}
122
123void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700124RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700125 bool isDryRun,
126 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700127{
Yanbiao Lid7c96362015-01-30 23:58:24 -0800128 bool isAutoPrefixPropagatorEnabled = false;
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800129
Vince Lehman76c751c2014-11-18 17:36:38 -0600130 for (const auto& item : configSection) {
131 if (item.first == "localhost_security") {
132 m_localhostValidator.load(item.second, filename);
Yingdi Yue5224e92014-04-29 18:04:02 -0700133 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600134 else if (item.first == "localhop_security") {
135 m_localhopValidator.load(item.second, filename);
136 m_isLocalhopEnabled = true;
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800137 }
Yanbiao Lid7c96362015-01-30 23:58:24 -0800138 else if (item.first == "auto_prefix_propagate") {
139 m_prefixPropagator.loadConfig(item.second);
140 isAutoPrefixPropagatorEnabled = true;
Vince Lehman76c751c2014-11-18 17:36:38 -0600141
142 // Avoid other actions when isDryRun == true
143 if (isDryRun) {
144 continue;
145 }
146
Yanbiao Lid7c96362015-01-30 23:58:24 -0800147 m_prefixPropagator.enable();
Vince Lehman76c751c2014-11-18 17:36:38 -0600148 }
149 else {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700150 BOOST_THROW_EXCEPTION(Error("Unrecognized rib property: " + item.first));
Vince Lehman76c751c2014-11-18 17:36:38 -0600151 }
152 }
153
Yanbiao Lid7c96362015-01-30 23:58:24 -0800154 if (!isAutoPrefixPropagatorEnabled) {
155 m_prefixPropagator.disable();
Vince Lehman76c751c2014-11-18 17:36:38 -0600156 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700157}
158
159void
Yanbiao Licf0db022016-01-29 00:54:25 -0800160RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700161{
Yanbiao Licf0db022016-01-29 00:54:25 -0800162 // register entry to the FIB
163 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
164 ControlParameters()
Junxiao Shifde3f542016-07-10 19:54:53 +0000165 .setName(Name(topPrefix).append(MGMT_MODULE_NAME))
Yanbiao Licf0db022016-01-29 00:54:25 -0800166 .setFaceId(0),
Junxiao Shib2600172016-07-11 08:53:53 +0000167 bind(&RibManager::onCommandPrefixAddNextHopSuccess, this, cref(topPrefix), _1),
Junxiao Shi29b41282016-08-22 03:47:02 +0000168 bind(&RibManager::onCommandPrefixAddNextHopError, this, cref(topPrefix), _1));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700169
Yanbiao Licf0db022016-01-29 00:54:25 -0800170 // add top prefix to the dispatcher
171 m_addTopPrefix(topPrefix);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700172}
173
174void
Yanbiao Licf0db022016-01-29 00:54:25 -0800175RibManager::registerEntry(const Name& topPrefix, const Interest& interest,
176 ControlParameters parameters,
177 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700178{
Yanbiao Licf0db022016-01-29 00:54:25 -0800179 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600180
181 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800182 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700183
Vince Lehman218be0a2015-01-15 17:25:20 -0600184 Route route;
185 route.faceId = parameters.getFaceId();
186 route.origin = parameters.getOrigin();
187 route.cost = parameters.getCost();
188 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500189
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700190 if (parameters.hasExpirationPeriod() &&
191 parameters.getExpirationPeriod() != time::milliseconds::max())
Vince Lehman76c751c2014-11-18 17:36:38 -0600192 {
193 route.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
Syed Obaid3313a372014-07-01 01:31:33 -0500194
Vince Lehman76c751c2014-11-18 17:36:38 -0600195 // Schedule a new event, the old one will be cancelled during rib insertion.
196 scheduler::EventId eventId = scheduler::schedule(parameters.getExpirationPeriod(),
Yanbiao Licf0db022016-01-29 00:54:25 -0800197 bind(&Rib::onRouteExpiration, &m_rib, parameters.getName(), route));
Syed Obaid3313a372014-07-01 01:31:33 -0500198
Vince Lehman76c751c2014-11-18 17:36:38 -0600199 NFD_LOG_TRACE("Scheduled unregistration at: " << route.expires <<
200 " with EventId: " << eventId);
201
202 // Set the NewEventId of this entry
203 route.setExpirationEvent(eventId);
204 }
205 else {
206 route.expires = time::steady_clock::TimePoint::max();
207 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700208
Vince Lehmanff8b3972015-02-20 16:51:21 -0600209 NFD_LOG_INFO("Adding route " << parameters.getName() << " nexthop=" << route.faceId
210 << " origin=" << route.origin
211 << " cost=" << route.cost);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700212
Vince Lehman76c751c2014-11-18 17:36:38 -0600213 RibUpdate update;
214 update.setAction(RibUpdate::REGISTER)
215 .setName(parameters.getName())
216 .setRoute(route);
217
Yanbiao Licf0db022016-01-29 00:54:25 -0800218 m_rib.beginApplyUpdate(update,
Junxiao Shib2600172016-07-11 08:53:53 +0000219 bind(&RibManager::onRibUpdateSuccess, this, update),
220 bind(&RibManager::onRibUpdateFailure, this, update, _1, _2));
Vince Lehman76c751c2014-11-18 17:36:38 -0600221
Vince Lehman218be0a2015-01-15 17:25:20 -0600222 m_registeredFaces.insert(route.faceId);
Vince Lehman281ded72014-08-21 12:17:08 -0500223}
224
225void
Yanbiao Licf0db022016-01-29 00:54:25 -0800226RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest,
227 ControlParameters parameters,
228 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700229{
Yanbiao Licf0db022016-01-29 00:54:25 -0800230 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600231
232 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800233 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700234
Vince Lehman218be0a2015-01-15 17:25:20 -0600235 Route route;
236 route.faceId = parameters.getFaceId();
237 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700238
Vince Lehmanff8b3972015-02-20 16:51:21 -0600239 NFD_LOG_INFO("Removing route " << parameters.getName() << " nexthop=" << route.faceId
240 << " origin=" << route.origin);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700241
Vince Lehman76c751c2014-11-18 17:36:38 -0600242 RibUpdate update;
243 update.setAction(RibUpdate::UNREGISTER)
244 .setName(parameters.getName())
245 .setRoute(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500246
Yanbiao Licf0db022016-01-29 00:54:25 -0800247 m_rib.beginApplyUpdate(update,
Junxiao Shib2600172016-07-11 08:53:53 +0000248 bind(&RibManager::onRibUpdateSuccess, this, update),
249 bind(&RibManager::onRibUpdateFailure, this, update, _1, _2));
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700250}
251
252void
Yanbiao Licf0db022016-01-29 00:54:25 -0800253RibManager::listEntries(const Name& topPrefix, const Interest& interest,
254 ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700255{
Yanbiao Licf0db022016-01-29 00:54:25 -0800256 for (auto&& ribTableEntry : m_rib) {
257 const auto& ribEntry = *ribTableEntry.second;
258 ndn::nfd::RibEntry record;
Vince Lehman76c751c2014-11-18 17:36:38 -0600259
Yanbiao Licf0db022016-01-29 00:54:25 -0800260 for (auto&& route : ribEntry) {
261 ndn::nfd::Route routeElement;
262 routeElement.setFaceId(route.faceId)
263 .setOrigin(route.origin)
264 .setCost(route.cost)
265 .setFlags(route.flags);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700266
Yanbiao Licf0db022016-01-29 00:54:25 -0800267 if (route.expires < time::steady_clock::TimePoint::max()) {
268 routeElement.setExpirationPeriod(time::duration_cast<time::milliseconds>(
269 route.expires - time::steady_clock::now()));
270 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700271
Yanbiao Licf0db022016-01-29 00:54:25 -0800272 record.addRoute(routeElement);
273 }
274
275 record.setName(ribEntry.getName());
276 context.append(record.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600277 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700278
Yanbiao Licf0db022016-01-29 00:54:25 -0800279 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700280}
281
282void
Yanbiao Licf0db022016-01-29 00:54:25 -0800283RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700284{
Yanbiao Licf0db022016-01-29 00:54:25 -0800285 bool isSelfRegistration = (parameters.getFaceId() == 0);
286 if (isSelfRegistration) {
287 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
288 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
289 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
290 // and is initialized synchronously with IncomingFaceId field enabled.
291 BOOST_ASSERT(incomingFaceIdTag != nullptr);
292 parameters.setFaceId(*incomingFaceIdTag);
Vince Lehman76c751c2014-11-18 17:36:38 -0600293 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700294}
295
296void
Yanbiao Licf0db022016-01-29 00:54:25 -0800297RibManager::authorize(const Name& prefix, const Interest& interest,
298 const ndn::mgmt::ControlParameters* params,
299 ndn::mgmt::AcceptContinuation accept,
300 ndn::mgmt::RejectContinuation reject)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700301{
Yanbiao Licf0db022016-01-29 00:54:25 -0800302 BOOST_ASSERT(params != nullptr);
303 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters));
304 BOOST_ASSERT(prefix == LOCAL_HOST_TOP_PREFIX || prefix == LOCAL_HOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700305
Yanbiao Licf0db022016-01-29 00:54:25 -0800306 auto& validator = [this, &prefix] () -> ndn::ValidatorConfig & {
307 return prefix == LOCAL_HOST_TOP_PREFIX ? m_localhostValidator : m_localhopValidator;
308 }();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700309
Yanbiao Licf0db022016-01-29 00:54:25 -0800310 validator.validate(interest,
311 bind([&interest, this, accept] { extractRequester(interest, accept); }),
312 bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); }));
Vince Lehman26b215c2014-08-17 15:00:41 -0500313}
314
315void
Vince Lehmancd613c52014-07-30 14:34:49 -0500316RibManager::fetchActiveFaces()
317{
318 NFD_LOG_DEBUG("Fetching active faces");
319
320 Interest interest(FACES_LIST_DATASET_PREFIX);
321 interest.setChildSelector(1);
322 interest.setMustBeFresh(true);
323
324 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
325
326 m_face.expressInterest(interest,
327 bind(&RibManager::fetchSegments, this, _2, buffer),
328 bind(&RibManager::onFetchFaceStatusTimeout, this));
329}
330
331void
332RibManager::fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer)
333{
334 buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
335 data.getContent().value_size());
336
337 uint64_t currentSegment = data.getName().get(-1).toSegment();
338
339 const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
Vince Lehman76c751c2014-11-18 17:36:38 -0600340
341 if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment) {
342 m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
343 bind(&RibManager::fetchSegments, this, _2, buffer),
344 bind(&RibManager::onFetchFaceStatusTimeout, this));
345 }
346 else {
347 removeInvalidFaces(buffer);
348 }
Vince Lehmancd613c52014-07-30 14:34:49 -0500349}
350
351void
Yanbiao Licf0db022016-01-29 00:54:25 -0800352RibManager::onFetchFaceStatusTimeout()
353{
354 std::cerr << "Face Status Dataset request timed out" << std::endl;
355 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
356}
357
358void
359RibManager::onFaceDestroyedEvent(uint64_t faceId)
360{
361 m_rib.beginRemoveFace(faceId);
362 m_registeredFaces.erase(faceId);
363}
364
365void
366RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
367{
368 scheduler::cancel(m_activeFaceFetchEvent);
369
370 m_activeFaceFetchEvent = scheduler::schedule(timeToWait,
371 bind(&RibManager::fetchActiveFaces, this));
372}
373
374void
Vince Lehman26b215c2014-08-17 15:00:41 -0500375RibManager::removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer)
Vince Lehmancd613c52014-07-30 14:34:49 -0500376{
Vince Lehman26b215c2014-08-17 15:00:41 -0500377 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500378
379 ndn::ConstBufferPtr buf = buffer->buf();
380
381 Block block;
382 size_t offset = 0;
Vince Lehman26b215c2014-08-17 15:00:41 -0500383 FaceIdSet activeFaces;
Vince Lehmancd613c52014-07-30 14:34:49 -0500384
Junxiao Shi78926c92015-02-28 22:56:06 -0700385 while (offset < buf->size()) {
386 bool isOk = false;
387 std::tie(isOk, block) = Block::fromBuffer(buf, offset);
388 if (!isOk) {
389 std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
390 break;
Vince Lehmancd613c52014-07-30 14:34:49 -0500391 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500392
Junxiao Shi78926c92015-02-28 22:56:06 -0700393 offset += block.size();
394
395 ndn::nfd::FaceStatus status(block);
396 activeFaces.insert(status.getFaceId());
397 }
398
Vince Lehman26b215c2014-08-17 15:00:41 -0500399 // Look for face IDs that were registered but not active to find missed
400 // face destroyed events
Yanbiao Licf0db022016-01-29 00:54:25 -0800401 for (auto&& faceId : m_registeredFaces) {
Vince Lehman76c751c2014-11-18 17:36:38 -0600402 if (activeFaces.find(faceId) == activeFaces.end()) {
403 NFD_LOG_DEBUG("Removing invalid face ID: " << faceId);
404
405 scheduler::schedule(time::seconds(0),
406 bind(&RibManager::onFaceDestroyedEvent, this, faceId));
Vince Lehman26b215c2014-08-17 15:00:41 -0500407 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600408 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500409
410 // Reschedule the check for future clean up
411 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500412}
413
414void
Yanbiao Licf0db022016-01-29 00:54:25 -0800415RibManager::onNotification(const FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500416{
Yanbiao Licf0db022016-01-29 00:54:25 -0800417 NFD_LOG_TRACE("onNotification: " << notification);
418
419 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
420 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
421
422 scheduler::schedule(time::seconds(0),
423 bind(&RibManager::onFaceDestroyedEvent, this, notification.getFaceId()));
424 }
425}
426
427void
Junxiao Shib2600172016-07-11 08:53:53 +0000428RibManager::onCommandPrefixAddNextHopSuccess(const Name& prefix,
429 const ndn::nfd::ControlParameters& result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800430{
431 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
432
433 // Routes must be inserted into the RIB so route flags can be applied
434 Route route;
435 route.faceId = result.getFaceId();
436 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
437 route.expires = time::steady_clock::TimePoint::max();
438 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
439
440 m_rib.insert(prefix, route);
441
442 m_registeredFaces.insert(route.faceId);
443}
444
445void
Junxiao Shi29b41282016-08-22 03:47:02 +0000446RibManager::onCommandPrefixAddNextHopError(const Name& name,
447 const ndn::nfd::ControlResponse& response)
Yanbiao Licf0db022016-01-29 00:54:25 -0800448{
Junxiao Shi29b41282016-08-22 03:47:02 +0000449 BOOST_THROW_EXCEPTION(Error("Error in setting interest filter (" + name.toUri() +
450 "): " + response.getText()));
Yanbiao Licf0db022016-01-29 00:54:25 -0800451}
452
453void
454RibManager::onControlHeaderSuccess()
455{
456 NFD_LOG_DEBUG("Local control header enabled");
457}
458
459void
Junxiao Shi29b41282016-08-22 03:47:02 +0000460RibManager::onControlHeaderError(const ndn::nfd::ControlResponse& response)
Yanbiao Licf0db022016-01-29 00:54:25 -0800461{
462 std::ostringstream os;
463 os << "Couldn't enable local control header "
Junxiao Shi29b41282016-08-22 03:47:02 +0000464 << "(code: " << response.getCode() << ", info: " << response.getText() << ")";
Yanbiao Licf0db022016-01-29 00:54:25 -0800465 BOOST_THROW_EXCEPTION(Error(os.str()));
Vince Lehmancd613c52014-07-30 14:34:49 -0500466}
467
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700468} // namespace rib
469} // namespace nfd