34 #include <ndn-cxx/lp/tags.hpp> 35 #include <ndn-cxx/mgmt/nfd/control-command.hpp> 36 #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> 37 #include <ndn-cxx/mgmt/nfd/control-response.hpp> 38 #include <ndn-cxx/mgmt/nfd/face-status.hpp> 39 #include <ndn-cxx/mgmt/nfd/rib-entry.hpp> 46 const Name RibManager::LOCAL_HOST_TOP_PREFIX =
"/localhost/nfd";
47 const Name RibManager::LOCAL_HOP_TOP_PREFIX =
"/localhop/nfd";
48 const std::string RibManager::MGMT_MODULE_NAME =
"rib";
49 const Name RibManager::FACES_LIST_DATASET_PREFIX =
"/localhost/nfd/faces/list";
50 const time::seconds RibManager::ACTIVE_FACE_FETCH_INTERVAL = time::seconds(300);
51 const Name RibManager::READVERTISE_NLSR_PREFIX =
"/localhost/nlsr";
55 ndn::KeyChain& keyChain)
58 , m_keyChain(keyChain)
59 , m_nfdController(m_face, m_keyChain)
60 , m_faceMonitor(m_face)
61 , m_localhostValidator(m_face)
62 , m_localhopValidator(m_face)
63 , m_isLocalhopEnabled(false)
64 , 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);
70 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));
83 registerTopPrefix(LOCAL_HOST_TOP_PREFIX);
85 if (m_isLocalhopEnabled) {
86 registerTopPrefix(LOCAL_HOP_TOP_PREFIX);
89 NFD_LOG_INFO(
"Start monitoring face create/destroy events");
90 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification,
this, _1));
91 m_faceMonitor.start();
93 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
99 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
101 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
true),
102 bind(&RibManager::onEnableLocalFieldsSuccess,
this),
103 bind(&RibManager::onEnableLocalFieldsError,
this, _1));
110 bind(&RibManager::onConfig,
this, _1, _2, _3));
122 NFD_LOG_DEBUG(
"RIB update failed for " << update <<
" (code: " << code
123 <<
", error: " << error <<
")");
126 scheduleActiveFaceFetch(time::seconds(1));
132 const std::string& filename)
134 bool isAutoPrefixPropagatorEnabled =
false;
135 bool wantReadvertiseToNlsr =
false;
137 for (
const auto& item : configSection) {
138 if (item.first ==
"localhost_security") {
139 m_localhostValidator.load(item.second, filename);
141 else if (item.first ==
"localhop_security") {
142 m_localhopValidator.load(item.second, filename);
143 m_isLocalhopEnabled =
true;
145 else if (item.first ==
"auto_prefix_propagate") {
147 isAutoPrefixPropagatorEnabled =
true;
154 m_prefixPropagator.
enable();
156 else if (item.first ==
"readvertise_nlsr") {
160 BOOST_THROW_EXCEPTION(
Error(
"Unrecognized rib property: " + item.first));
164 if (!isAutoPrefixPropagatorEnabled) {
168 if (wantReadvertiseToNlsr && m_readvertiseNlsr ==
nullptr) {
172 make_unique<ClientToNlsrReadvertisePolicy>(),
173 make_unique<NfdRibReadvertiseDestination>(m_nfdController, READVERTISE_NLSR_PREFIX, m_rib)));
175 else if (!wantReadvertiseToNlsr && m_readvertiseNlsr !=
nullptr) {
177 m_readvertiseNlsr.reset();
182 RibManager::registerTopPrefix(
const Name& topPrefix)
185 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
187 .setName(Name(topPrefix).append(MGMT_MODULE_NAME))
189 bind(&RibManager::onCommandPrefixAddNextHopSuccess,
this, cref(topPrefix), _1),
190 bind(&RibManager::onCommandPrefixAddNextHopError,
this, cref(topPrefix), _1));
193 m_addTopPrefix(topPrefix);
197 RibManager::registerEntry(
const Name& topPrefix,
const Interest& interest,
198 ControlParameters parameters,
199 const ndn::mgmt::CommandContinuation& done)
201 setFaceForSelfRegistration(interest, parameters);
204 done(ControlResponse(200,
"Success").setBody(parameters.wireEncode()));
207 route.
faceId = parameters.getFaceId();
208 route.
origin = parameters.getOrigin();
209 route.
cost = parameters.getCost();
210 route.
flags = parameters.getFlags();
212 if (parameters.hasExpirationPeriod() &&
213 parameters.getExpirationPeriod() != time::milliseconds::max()) {
214 route.
expires = time::steady_clock::now() + parameters.getExpirationPeriod();
221 " with EventId: " << eventId);
231 <<
" origin=" << route.
origin 232 <<
" cost=" << route.
cost);
243 m_registeredFaces.insert(route.
faceId);
247 RibManager::unregisterEntry(
const Name& topPrefix,
const Interest& interest,
248 ControlParameters parameters,
249 const ndn::mgmt::CommandContinuation& done)
251 setFaceForSelfRegistration(interest, parameters);
254 done(ControlResponse(200,
"Success").setBody(parameters.wireEncode()));
257 route.
faceId = parameters.getFaceId();
258 route.
origin = parameters.getOrigin();
261 <<
" origin=" << route.
origin);
274 RibManager::listEntries(
const Name& topPrefix,
const Interest& interest,
275 ndn::mgmt::StatusDatasetContext& context)
277 auto now = time::steady_clock::now();
278 for (
const auto& kv : m_rib) {
280 ndn::nfd::RibEntry item;
284 r.setFaceId(route.faceId);
285 r.setOrigin(route.origin);
286 r.setCost(route.cost);
287 r.setFlags(route.flags);
289 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now));
293 context.append(item.wireEncode());
299 RibManager::setFaceForSelfRegistration(
const Interest& request, ControlParameters& parameters)
301 bool isSelfRegistration = (parameters.getFaceId() == 0);
302 if (isSelfRegistration) {
303 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
307 BOOST_ASSERT(incomingFaceIdTag !=
nullptr);
308 parameters.setFaceId(*incomingFaceIdTag);
312 ndn::mgmt::Authorization
313 RibManager::makeAuthorization(
const std::string& verb)
315 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);
323 ndn::ValidatorConfig& validator = prefix == LOCAL_HOST_TOP_PREFIX ?
324 m_localhostValidator : m_localhopValidator;
325 validator.validate(interest,
327 bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); }));
332 RibManager::fetchActiveFaces()
336 m_nfdController.fetch<ndn::nfd::FaceDataset>(
337 bind(&RibManager::removeInvalidFaces,
this, _1),
338 bind(&RibManager::onFetchActiveFacesFailure,
this, _1, _2),
339 ndn::nfd::CommandOptions());
343 RibManager::onFetchActiveFacesFailure(uint32_t code,
const std::string& reason)
345 NFD_LOG_DEBUG(
"Face Status Dataset request failure " << code <<
" " << reason);
346 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
350 RibManager::onFaceDestroyedEvent(uint64_t faceId)
353 m_registeredFaces.erase(faceId);
357 RibManager::scheduleActiveFaceFetch(
const time::seconds& timeToWait)
359 m_activeFaceFetchEvent =
scheduler::schedule(timeToWait, [
this] { this->fetchActiveFaces(); });
363 RibManager::removeInvalidFaces(
const std::vector<ndn::nfd::FaceStatus>& activeFaces)
367 FaceIdSet activeFaceIds;
368 for (
const auto& faceStatus : activeFaces) {
369 activeFaceIds.insert(faceStatus.getFaceId());
374 for (
auto faceId : m_registeredFaces) {
375 if (activeFaceIds.count(faceId) == 0) {
377 scheduler::schedule(time::seconds(0), [
this, faceId] { this->onFaceDestroyedEvent(faceId); });
382 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
386 RibManager::onNotification(
const ndn::nfd::FaceEventNotification& notification)
390 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
391 NFD_LOG_DEBUG(
"Received notification for destroyed faceId: " << notification.getFaceId());
394 bind(&RibManager::onFaceDestroyedEvent,
this, notification.getFaceId()));
399 RibManager::onCommandPrefixAddNextHopSuccess(
const Name& prefix,
400 const ndn::nfd::ControlParameters& result)
402 NFD_LOG_DEBUG(
"Successfully registered " + prefix.toUri() +
" with NFD");
406 route.
faceId = result.getFaceId();
407 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
408 route.expires = ndn::nullopt;
409 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
411 m_rib.
insert(prefix, route);
413 m_registeredFaces.insert(route.faceId);
417 RibManager::onCommandPrefixAddNextHopError(
const Name& name,
418 const ndn::nfd::ControlResponse& response)
420 BOOST_THROW_EXCEPTION(
Error(
"Error in setting interest filter (" + name.toUri() +
421 "): " + response.getText()));
425 RibManager::onEnableLocalFieldsSuccess()
431 RibManager::onEnableLocalFieldsError(
const ndn::nfd::ControlResponse& response)
433 BOOST_THROW_EXCEPTION(
Error(
"Couldn't enable local fields (code: " +
434 to_string(response.getCode()) +
", info: " + response.getText() +
void registerStatusDatasetHandler(const std::string &verb, const ndn::mgmt::StatusDatasetHandler &handler)
void addSectionHandler(const std::string §ionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
#define NFD_LOG_DEBUG(expression)
configuration file parsing utility
void disable()
disable automatic prefix propagation
void enable()
enable automatic prefix propagation
RibUpdate & setAction(Action action)
void setName(const Name &prefix)
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string §ionName)
parse a config option that can be either "yes" or "no"
RibUpdate & setName(const Name &name)
a collection of common functions shared by all NFD managers and RIB manager, such as communicating wi...
#define NFD_LOG_INFO(expression)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
const Name & getName() const
void onRibUpdateFailure(const RibUpdate &update, uint32_t code, const std::string &error)
const RouteList & getRoutes() const
ndn::nfd::RouteOrigin origin
void loadConfig(const ConfigSection &configSection)
load the "auto_prefix_propagate" section from config file
void insert(const Name &prefix, const Route &route)
void setExpirationEvent(const scheduler::EventId eid)
boost::property_tree::ptree ConfigSection
a config file section
represents a route for a name prefix
void extractRequester(const Interest &interest, ndn::mgmt::AcceptContinuation accept)
extract a requester from a ControlCommand request
void setConfigFile(ConfigFile &configFile)
#define NFD_LOG_INIT(name)
ndn::optional< time::steady_clock::TimePoint > expires
#define NFD_LOG_TRACE(expression)
EventId schedule(time::nanoseconds after, const EventCallback &event)
schedule an event
readvertise a subset of routes to a destination according to a policy
represents a RIB entry, which contains one or more Routes with the same prefix
void onRibUpdateSuccess(const RibUpdate &update)
void beginRemoveFace(uint64_t faceId)
starts the FIB update process when a face has been destroyed
void onRouteExpiration(const Name &prefix, const Route &route)
void beginApplyUpdate(const RibUpdate &update, const UpdateSuccessCallback &onSuccess, const UpdateFailureCallback &onFailure)
passes the provided RibUpdateBatch to FibUpdater to calculate and send FibUpdates.
RibManager(Dispatcher &dispatcher, ndn::Face &face, ndn::KeyChain &keyChain)
std::underlying_type< ndn::nfd::RouteFlags >::type flags