blob: edd6c5dd0d86ccdd94fb8e9b35c4a0bc542c4790 [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";
Vince Lehmancd613c52014-07-30 14:34:49 -050040const Name RibManager::FACES_LIST_DATASET_PREFIX = "/localhost/nfd/faces/list";
Vince Lehman26b215c2014-08-17 15:00:41 -050041const time::seconds RibManager::ACTIVE_FACE_FETCH_INTERVAL = time::seconds(300);
42
Yanbiao Licf0db022016-01-29 00:54:25 -080043RibManager::RibManager(Dispatcher& dispatcher,
44 ndn::Face& face,
45 ndn::KeyChain& keyChain)
46 : ManagerBase(dispatcher, "rib")
47 , m_face(face)
Vince Lehmanc1dfdb42015-07-16 12:17:36 -050048 , m_keyChain(keyChain)
Junxiao Shi8e273ca2014-11-12 00:42:29 -070049 , m_nfdController(m_face, m_keyChain)
Yanbiao Licf0db022016-01-29 00:54:25 -080050 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070051 , m_localhostValidator(m_face)
52 , m_localhopValidator(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070053 , m_isLocalhopEnabled(false)
Yanbiao Licf0db022016-01-29 00:54:25 -080054 , m_prefixPropagator(m_nfdController, m_keyChain, m_rib)
55 , m_fibUpdater(m_rib, m_nfdController)
56 , m_addTopPrefix([&dispatcher] (const Name& topPrefix) {
57 dispatcher.addTopPrefix(topPrefix, false);
58 })
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070059{
Yanbiao Licf0db022016-01-29 00:54:25 -080060 registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
61 bind(&RibManager::registerEntry, this, _2, _3, _4, _5));
62 registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister",
63 bind(&RibManager::unregisterEntry, this, _2, _3, _4, _5));
64
65 registerStatusDatasetHandler("list", bind(&RibManager::listEntries, this, _1, _2, _3));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070066}
67
Vince Lehman26b215c2014-08-17 15:00:41 -050068RibManager::~RibManager()
69{
70 scheduler::cancel(m_activeFaceFetchEvent);
71}
72
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070073void
74RibManager::registerWithNfd()
75{
Yanbiao Licf0db022016-01-29 00:54:25 -080076 registerTopPrefix(LOCAL_HOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070077
Junxiao Shia3295742014-05-16 22:40:10 -070078 if (m_isLocalhopEnabled) {
Yanbiao Licf0db022016-01-29 00:54:25 -080079 registerTopPrefix(LOCAL_HOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -070080 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070081
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070082 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shifbf78342015-01-23 14:46:41 -070083 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -070084 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -050085
Vince Lehman26b215c2014-08-17 15:00:41 -050086 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070087}
88
89void
Yanbiao Licf0db022016-01-29 00:54:25 -080090RibManager::enableLocalControlHeader()
91{
92 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
93 ControlParameters()
94 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
95 bind(&RibManager::onControlHeaderSuccess, this),
96 bind(&RibManager::onControlHeaderError, this, _1, _2));
97}
98
99void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700100RibManager::setConfigFile(ConfigFile& configFile)
101{
Yingdi Yue5224e92014-04-29 18:04:02 -0700102 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700103 bind(&RibManager::onConfig, this, _1, _2, _3));
104}
105
106void
Yanbiao Licf0db022016-01-29 00:54:25 -0800107RibManager::onRibUpdateSuccess(const RibUpdate& update)
108{
109 NFD_LOG_DEBUG("RIB update succeeded for " << update);
110}
111
112void
113RibManager::onRibUpdateFailure(const RibUpdate& update, uint32_t code, const std::string& error)
114{
115 NFD_LOG_DEBUG("RIB update failed for " << update << " (code: " << code
116 << ", error: " << error << ")");
117
118 // Since the FIB rejected the update, clean up invalid routes
119 scheduleActiveFaceFetch(time::seconds(1));
120}
121
122void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700123RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700124 bool isDryRun,
125 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700126{
Yanbiao Lid7c96362015-01-30 23:58:24 -0800127 bool isAutoPrefixPropagatorEnabled = false;
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800128
Vince Lehman76c751c2014-11-18 17:36:38 -0600129 for (const auto& item : configSection) {
130 if (item.first == "localhost_security") {
131 m_localhostValidator.load(item.second, filename);
Yingdi Yue5224e92014-04-29 18:04:02 -0700132 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600133 else if (item.first == "localhop_security") {
134 m_localhopValidator.load(item.second, filename);
135 m_isLocalhopEnabled = true;
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800136 }
Yanbiao Lid7c96362015-01-30 23:58:24 -0800137 else if (item.first == "auto_prefix_propagate") {
138 m_prefixPropagator.loadConfig(item.second);
139 isAutoPrefixPropagatorEnabled = true;
Vince Lehman76c751c2014-11-18 17:36:38 -0600140
141 // Avoid other actions when isDryRun == true
142 if (isDryRun) {
143 continue;
144 }
145
Yanbiao Lid7c96362015-01-30 23:58:24 -0800146 m_prefixPropagator.enable();
Vince Lehman76c751c2014-11-18 17:36:38 -0600147 }
148 else {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700149 BOOST_THROW_EXCEPTION(Error("Unrecognized rib property: " + item.first));
Vince Lehman76c751c2014-11-18 17:36:38 -0600150 }
151 }
152
Yanbiao Lid7c96362015-01-30 23:58:24 -0800153 if (!isAutoPrefixPropagatorEnabled) {
154 m_prefixPropagator.disable();
Vince Lehman76c751c2014-11-18 17:36:38 -0600155 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700156}
157
158void
Yanbiao Licf0db022016-01-29 00:54:25 -0800159RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700160{
Yanbiao Licf0db022016-01-29 00:54:25 -0800161 // register entry to the FIB
162 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
163 ControlParameters()
164 .setName(topPrefix)
165 .setFaceId(0),
166 bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(topPrefix), _1),
167 bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(topPrefix), _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700168
Yanbiao Licf0db022016-01-29 00:54:25 -0800169 // add top prefix to the dispatcher
170 m_addTopPrefix(topPrefix);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700171}
172
173void
Yanbiao Licf0db022016-01-29 00:54:25 -0800174RibManager::registerEntry(const Name& topPrefix, const Interest& interest,
175 ControlParameters parameters,
176 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700177{
Yanbiao Licf0db022016-01-29 00:54:25 -0800178 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600179
180 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800181 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700182
Vince Lehman218be0a2015-01-15 17:25:20 -0600183 Route route;
184 route.faceId = parameters.getFaceId();
185 route.origin = parameters.getOrigin();
186 route.cost = parameters.getCost();
187 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500188
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700189 if (parameters.hasExpirationPeriod() &&
190 parameters.getExpirationPeriod() != time::milliseconds::max())
Vince Lehman76c751c2014-11-18 17:36:38 -0600191 {
192 route.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
Syed Obaid3313a372014-07-01 01:31:33 -0500193
Vince Lehman76c751c2014-11-18 17:36:38 -0600194 // Schedule a new event, the old one will be cancelled during rib insertion.
195 scheduler::EventId eventId = scheduler::schedule(parameters.getExpirationPeriod(),
Yanbiao Licf0db022016-01-29 00:54:25 -0800196 bind(&Rib::onRouteExpiration, &m_rib, parameters.getName(), route));
Syed Obaid3313a372014-07-01 01:31:33 -0500197
Vince Lehman76c751c2014-11-18 17:36:38 -0600198 NFD_LOG_TRACE("Scheduled unregistration at: " << route.expires <<
199 " with EventId: " << eventId);
200
201 // Set the NewEventId of this entry
202 route.setExpirationEvent(eventId);
203 }
204 else {
205 route.expires = time::steady_clock::TimePoint::max();
206 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700207
Vince Lehmanff8b3972015-02-20 16:51:21 -0600208 NFD_LOG_INFO("Adding route " << parameters.getName() << " nexthop=" << route.faceId
209 << " origin=" << route.origin
210 << " cost=" << route.cost);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700211
Vince Lehman76c751c2014-11-18 17:36:38 -0600212 RibUpdate update;
213 update.setAction(RibUpdate::REGISTER)
214 .setName(parameters.getName())
215 .setRoute(route);
216
Yanbiao Licf0db022016-01-29 00:54:25 -0800217 m_rib.beginApplyUpdate(update,
Vince Lehman76c751c2014-11-18 17:36:38 -0600218 bind(&RibManager::onRibUpdateSuccess, this, update),
219 bind(&RibManager::onRibUpdateFailure, this, update, _1, _2));
220
Vince Lehman218be0a2015-01-15 17:25:20 -0600221 m_registeredFaces.insert(route.faceId);
Vince Lehman281ded72014-08-21 12:17:08 -0500222}
223
224void
Yanbiao Licf0db022016-01-29 00:54:25 -0800225RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest,
226 ControlParameters parameters,
227 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700228{
Yanbiao Licf0db022016-01-29 00:54:25 -0800229 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600230
231 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800232 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700233
Vince Lehman218be0a2015-01-15 17:25:20 -0600234 Route route;
235 route.faceId = parameters.getFaceId();
236 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700237
Vince Lehmanff8b3972015-02-20 16:51:21 -0600238 NFD_LOG_INFO("Removing route " << parameters.getName() << " nexthop=" << route.faceId
239 << " origin=" << route.origin);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700240
Vince Lehman76c751c2014-11-18 17:36:38 -0600241 RibUpdate update;
242 update.setAction(RibUpdate::UNREGISTER)
243 .setName(parameters.getName())
244 .setRoute(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500245
Yanbiao Licf0db022016-01-29 00:54:25 -0800246 m_rib.beginApplyUpdate(update,
Vince Lehman76c751c2014-11-18 17:36:38 -0600247 bind(&RibManager::onRibUpdateSuccess, this, update),
248 bind(&RibManager::onRibUpdateFailure, this, update, _1, _2));
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700249}
250
251void
Yanbiao Licf0db022016-01-29 00:54:25 -0800252RibManager::listEntries(const Name& topPrefix, const Interest& interest,
253 ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700254{
Yanbiao Licf0db022016-01-29 00:54:25 -0800255 for (auto&& ribTableEntry : m_rib) {
256 const auto& ribEntry = *ribTableEntry.second;
257 ndn::nfd::RibEntry record;
Vince Lehman76c751c2014-11-18 17:36:38 -0600258
Yanbiao Licf0db022016-01-29 00:54:25 -0800259 for (auto&& route : ribEntry) {
260 ndn::nfd::Route routeElement;
261 routeElement.setFaceId(route.faceId)
262 .setOrigin(route.origin)
263 .setCost(route.cost)
264 .setFlags(route.flags);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700265
Yanbiao Licf0db022016-01-29 00:54:25 -0800266 if (route.expires < time::steady_clock::TimePoint::max()) {
267 routeElement.setExpirationPeriod(time::duration_cast<time::milliseconds>(
268 route.expires - time::steady_clock::now()));
269 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700270
Yanbiao Licf0db022016-01-29 00:54:25 -0800271 record.addRoute(routeElement);
272 }
273
274 record.setName(ribEntry.getName());
275 context.append(record.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600276 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700277
Yanbiao Licf0db022016-01-29 00:54:25 -0800278 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700279}
280
281void
Yanbiao Licf0db022016-01-29 00:54:25 -0800282RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700283{
Yanbiao Licf0db022016-01-29 00:54:25 -0800284 bool isSelfRegistration = (parameters.getFaceId() == 0);
285 if (isSelfRegistration) {
286 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
287 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
288 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
289 // and is initialized synchronously with IncomingFaceId field enabled.
290 BOOST_ASSERT(incomingFaceIdTag != nullptr);
291 parameters.setFaceId(*incomingFaceIdTag);
Vince Lehman76c751c2014-11-18 17:36:38 -0600292 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700293}
294
295void
Yanbiao Licf0db022016-01-29 00:54:25 -0800296RibManager::authorize(const Name& prefix, const Interest& interest,
297 const ndn::mgmt::ControlParameters* params,
298 ndn::mgmt::AcceptContinuation accept,
299 ndn::mgmt::RejectContinuation reject)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700300{
Yanbiao Licf0db022016-01-29 00:54:25 -0800301 BOOST_ASSERT(params != nullptr);
302 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters));
303 BOOST_ASSERT(prefix == LOCAL_HOST_TOP_PREFIX || prefix == LOCAL_HOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700304
Yanbiao Licf0db022016-01-29 00:54:25 -0800305 auto& validator = [this, &prefix] () -> ndn::ValidatorConfig & {
306 return prefix == LOCAL_HOST_TOP_PREFIX ? m_localhostValidator : m_localhopValidator;
307 }();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700308
Yanbiao Licf0db022016-01-29 00:54:25 -0800309 validator.validate(interest,
310 bind([&interest, this, accept] { extractRequester(interest, accept); }),
311 bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); }));
Vince Lehman26b215c2014-08-17 15:00:41 -0500312}
313
314void
Vince Lehmancd613c52014-07-30 14:34:49 -0500315RibManager::fetchActiveFaces()
316{
317 NFD_LOG_DEBUG("Fetching active faces");
318
319 Interest interest(FACES_LIST_DATASET_PREFIX);
320 interest.setChildSelector(1);
321 interest.setMustBeFresh(true);
322
323 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
324
325 m_face.expressInterest(interest,
326 bind(&RibManager::fetchSegments, this, _2, buffer),
327 bind(&RibManager::onFetchFaceStatusTimeout, this));
328}
329
330void
331RibManager::fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer)
332{
333 buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
334 data.getContent().value_size());
335
336 uint64_t currentSegment = data.getName().get(-1).toSegment();
337
338 const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
Vince Lehman76c751c2014-11-18 17:36:38 -0600339
340 if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment) {
341 m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
342 bind(&RibManager::fetchSegments, this, _2, buffer),
343 bind(&RibManager::onFetchFaceStatusTimeout, this));
344 }
345 else {
346 removeInvalidFaces(buffer);
347 }
Vince Lehmancd613c52014-07-30 14:34:49 -0500348}
349
350void
Yanbiao Licf0db022016-01-29 00:54:25 -0800351RibManager::onFetchFaceStatusTimeout()
352{
353 std::cerr << "Face Status Dataset request timed out" << std::endl;
354 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
355}
356
357void
358RibManager::onFaceDestroyedEvent(uint64_t faceId)
359{
360 m_rib.beginRemoveFace(faceId);
361 m_registeredFaces.erase(faceId);
362}
363
364void
365RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
366{
367 scheduler::cancel(m_activeFaceFetchEvent);
368
369 m_activeFaceFetchEvent = scheduler::schedule(timeToWait,
370 bind(&RibManager::fetchActiveFaces, this));
371}
372
373void
Vince Lehman26b215c2014-08-17 15:00:41 -0500374RibManager::removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer)
Vince Lehmancd613c52014-07-30 14:34:49 -0500375{
Vince Lehman26b215c2014-08-17 15:00:41 -0500376 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500377
378 ndn::ConstBufferPtr buf = buffer->buf();
379
380 Block block;
381 size_t offset = 0;
Vince Lehman26b215c2014-08-17 15:00:41 -0500382 FaceIdSet activeFaces;
Vince Lehmancd613c52014-07-30 14:34:49 -0500383
Junxiao Shi78926c92015-02-28 22:56:06 -0700384 while (offset < buf->size()) {
385 bool isOk = false;
386 std::tie(isOk, block) = Block::fromBuffer(buf, offset);
387 if (!isOk) {
388 std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
389 break;
Vince Lehmancd613c52014-07-30 14:34:49 -0500390 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500391
Junxiao Shi78926c92015-02-28 22:56:06 -0700392 offset += block.size();
393
394 ndn::nfd::FaceStatus status(block);
395 activeFaces.insert(status.getFaceId());
396 }
397
Vince Lehman26b215c2014-08-17 15:00:41 -0500398 // Look for face IDs that were registered but not active to find missed
399 // face destroyed events
Yanbiao Licf0db022016-01-29 00:54:25 -0800400 for (auto&& faceId : m_registeredFaces) {
Vince Lehman76c751c2014-11-18 17:36:38 -0600401 if (activeFaces.find(faceId) == activeFaces.end()) {
402 NFD_LOG_DEBUG("Removing invalid face ID: " << faceId);
403
404 scheduler::schedule(time::seconds(0),
405 bind(&RibManager::onFaceDestroyedEvent, this, faceId));
Vince Lehman26b215c2014-08-17 15:00:41 -0500406 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600407 }
Vince Lehman26b215c2014-08-17 15:00:41 -0500408
409 // Reschedule the check for future clean up
410 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500411}
412
413void
Yanbiao Licf0db022016-01-29 00:54:25 -0800414RibManager::onNotification(const FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500415{
Yanbiao Licf0db022016-01-29 00:54:25 -0800416 NFD_LOG_TRACE("onNotification: " << notification);
417
418 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
419 NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId());
420
421 scheduler::schedule(time::seconds(0),
422 bind(&RibManager::onFaceDestroyedEvent, this, notification.getFaceId()));
423 }
424}
425
426void
427RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix,
428 const ndn::nfd::ControlParameters& result)
429{
430 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
431
432 // Routes must be inserted into the RIB so route flags can be applied
433 Route route;
434 route.faceId = result.getFaceId();
435 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
436 route.expires = time::steady_clock::TimePoint::max();
437 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
438
439 m_rib.insert(prefix, route);
440
441 m_registeredFaces.insert(route.faceId);
442}
443
444void
445RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg)
446{
447 BOOST_THROW_EXCEPTION(Error("Error in setting interest filter (" + name.toUri() + "): " + msg));
448}
449
450void
451RibManager::onControlHeaderSuccess()
452{
453 NFD_LOG_DEBUG("Local control header enabled");
454}
455
456void
457RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
458{
459 std::ostringstream os;
460 os << "Couldn't enable local control header "
461 << "(code: " << code << ", info: " << reason << ")";
462 BOOST_THROW_EXCEPTION(Error(os.str()));
Vince Lehmancd613c52014-07-30 14:34:49 -0500463}
464
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700465} // namespace rib
466} // namespace nfd