32 #include <ndn-cxx/lp/tags.hpp> 33 #include <ndn-cxx/mgmt/nfd/control-command.hpp> 34 #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> 35 #include <ndn-cxx/mgmt/nfd/control-response.hpp> 36 #include <ndn-cxx/mgmt/nfd/face-status.hpp> 37 #include <ndn-cxx/mgmt/nfd/rib-entry.hpp> 52 , m_nfdController(nfdController)
53 , m_dispatcher(dispatcher)
55 , m_localhostValidator(face)
56 , m_localhopValidator(face)
57 , m_isLocalhopEnabled(false)
59 registerCommandHandler<ndn::nfd::RibRegisterCommand>(
"register",
60 bind(&RibManager::registerEntry,
this, _2, _3, _4, _5));
61 registerCommandHandler<ndn::nfd::RibUnregisterCommand>(
"unregister",
62 bind(&RibManager::unregisterEntry,
this, _2, _3, _4, _5));
70 m_localhostValidator.load(section, filename);
76 m_localhopValidator.load(section, filename);
77 m_isLocalhopEnabled =
true;
83 m_isLocalhopEnabled =
false;
89 registerTopPrefix(LOCALHOST_TOP_PREFIX);
91 if (m_isLocalhopEnabled) {
92 registerTopPrefix(LOCALHOP_TOP_PREFIX);
95 NFD_LOG_INFO(
"Start monitoring face create/destroy events");
96 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification,
this, _1));
97 m_faceMonitor.start();
99 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
105 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
106 ControlParameters().setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED,
true),
107 [] (
const ControlParameters& res) {
110 [] (
const ControlResponse& res) {
111 BOOST_THROW_EXCEPTION(
Error(
"Couldn't enable local fields (" + to_string(res.getCode()) +
112 " " + res.getText() +
")"));
117 RibManager::beginAddRoute(
const Name& name,
Route route, optional<time::nanoseconds> expires,
118 const std::function<
void(RibUpdateResult)>& done)
121 if (*expires <= 0_ns) {
122 done(RibUpdateResult::EXPIRED);
125 route.
expires = time::steady_clock::now() + *expires;
128 expires = *route.
expires - time::steady_clock::now();
129 if (*expires <= 0_ns) {
130 done(RibUpdateResult::EXPIRED);
136 " origin=" << route.
origin <<
" cost=" << route.
cost);
144 m_registeredFaces.insert(route.
faceId);
150 beginRibUpdate(update, done);
154 RibManager::beginRemoveRoute(
const Name& name,
const Route& route,
155 const std::function<
void(RibUpdateResult)>& done)
158 " origin=" << route.
origin);
164 beginRibUpdate(update, done);
168 RibManager::beginRibUpdate(
const RibUpdate& update,
169 const std::function<
void(RibUpdateResult)>& done)
174 done(RibUpdateResult::OK);
176 [=] (uint32_t code,
const std::string& error) {
177 NFD_LOG_DEBUG(
"RIB update failed for " << update <<
" (" << code <<
" " << error <<
")");
180 scheduleActiveFaceFetch(1_s);
182 done(RibUpdateResult::ERROR);
187 RibManager::registerTopPrefix(
const Name& topPrefix)
190 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
191 ControlParameters().setName(Name(topPrefix).append(MGMT_MODULE_NAME))
193 [=] (
const ControlParameters& res) {
194 NFD_LOG_DEBUG(
"Successfully registered " << topPrefix <<
" with NFD");
198 route.
faceId = res.getFaceId();
199 route.
origin = ndn::nfd::ROUTE_ORIGIN_APP;
200 route.
flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
202 m_rib.
insert(topPrefix, route);
204 m_registeredFaces.insert(route.
faceId);
206 [=] (
const ControlResponse& res) {
207 BOOST_THROW_EXCEPTION(
Error(
"Cannot add FIB entry " + topPrefix.toUri() +
" (" +
208 to_string(res.getCode()) +
" " + res.getText() +
")"));
212 m_dispatcher.addTopPrefix(topPrefix,
false);
216 RibManager::registerEntry(
const Name& topPrefix,
const Interest& interest,
217 ControlParameters parameters,
218 const ndn::mgmt::CommandContinuation& done)
221 done(ControlResponse(414,
"Route prefix cannot exceed " + ndn::to_string(
FIB_MAX_DEPTH) +
226 setFaceForSelfRegistration(interest, parameters);
229 done(ControlResponse(200,
"Success").setBody(parameters.wireEncode()));
232 route.
faceId = parameters.getFaceId();
233 route.
origin = parameters.getOrigin();
234 route.
cost = parameters.getCost();
235 route.
flags = parameters.getFlags();
237 optional<time::nanoseconds> expires;
238 if (parameters.hasExpirationPeriod() &&
239 parameters.getExpirationPeriod() != time::milliseconds::max()) {
240 expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod());
243 beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {});
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();
260 beginRemoveRoute(parameters.getName(), route, [] (RibUpdateResult) {});
264 RibManager::listEntries(
const Name& topPrefix,
const Interest& interest,
265 ndn::mgmt::StatusDatasetContext& context)
267 auto now = time::steady_clock::now();
268 for (
const auto& kv : m_rib) {
270 ndn::nfd::RibEntry item;
274 r.setFaceId(route.
faceId);
275 r.setOrigin(route.
origin);
276 r.setCost(route.
cost);
277 r.setFlags(route.
flags);
279 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.
expires - now));
283 context.append(item.wireEncode());
289 RibManager::setFaceForSelfRegistration(
const Interest& request, ControlParameters& parameters)
291 bool isSelfRegistration = (parameters.getFaceId() == 0);
292 if (isSelfRegistration) {
293 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
297 BOOST_ASSERT(incomingFaceIdTag !=
nullptr);
298 parameters.setFaceId(*incomingFaceIdTag);
302 ndn::mgmt::Authorization
303 RibManager::makeAuthorization(
const std::string& verb)
305 return [
this] (
const Name& prefix,
const Interest& interest,
306 const ndn::mgmt::ControlParameters* params,
307 const ndn::mgmt::AcceptContinuation& accept,
308 const ndn::mgmt::RejectContinuation& reject) {
309 BOOST_ASSERT(params !=
nullptr);
310 BOOST_ASSERT(
typeid(*params) ==
typeid(ndn::nfd::ControlParameters));
311 BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX);
313 ndn::ValidatorConfig& validator = prefix == LOCALHOST_TOP_PREFIX ?
314 m_localhostValidator : m_localhopValidator;
315 validator.validate(interest,
317 bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); }));
322 RibManager::fetchActiveFaces()
326 m_nfdController.fetch<ndn::nfd::FaceDataset>(
327 bind(&RibManager::removeInvalidFaces,
this, _1),
328 bind(&RibManager::onFetchActiveFacesFailure,
this, _1, _2),
329 ndn::nfd::CommandOptions());
333 RibManager::onFetchActiveFacesFailure(uint32_t code,
const std::string& reason)
335 NFD_LOG_DEBUG(
"Face Status Dataset request failure " << code <<
" " << reason);
336 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
340 RibManager::onFaceDestroyedEvent(uint64_t faceId)
343 m_registeredFaces.erase(faceId);
347 RibManager::scheduleActiveFaceFetch(
const time::seconds& timeToWait)
349 m_activeFaceFetchEvent =
scheduler::schedule(timeToWait, [
this] { this->fetchActiveFaces(); });
353 RibManager::removeInvalidFaces(
const std::vector<ndn::nfd::FaceStatus>& activeFaces)
357 FaceIdSet activeFaceIds;
358 for (
const auto& faceStatus : activeFaces) {
359 activeFaceIds.insert(faceStatus.getFaceId());
364 for (
auto faceId : m_registeredFaces) {
365 if (activeFaceIds.count(faceId) == 0) {
367 scheduler::schedule(time::seconds(0), [
this, faceId] { this->onFaceDestroyedEvent(faceId); });
372 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
376 RibManager::onNotification(
const ndn::nfd::FaceEventNotification& notification)
380 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
381 NFD_LOG_DEBUG(
"Received notification for destroyed faceId: " << notification.getFaceId());
384 bind(&RibManager::onFaceDestroyedEvent,
this, notification.getFaceId()));
void registerStatusDatasetHandler(const std::string &verb, const ndn::mgmt::StatusDatasetHandler &handler)
RibUpdate & setRoute(const Route &route)
represents the Routing Information Base
static const Name LOCALHOP_TOP_PREFIX
RibUpdate & setAction(Action action)
static const time::seconds ACTIVE_FACE_FETCH_INTERVAL
void setName(const Name &prefix)
RibManager(Rib &rib, ndn::Face &face, ndn::nfd::Controller &nfdController, Dispatcher &dispatcher)
void enableLocalhop(const ConfigSection §ion, const std::string &filename)
Apply localhop_security configuration and allow accepting commands on /localhop/nfd/rib prefix...
void registerWithNfd()
Start accepting commands and dataset requests.
void applyLocalhostConfig(const ConfigSection §ion, const std::string &filename)
Apply localhost_security configuration.
void enableLocalFields()
Enable NDNLP IncomingFaceId field in order to support self-registration commands. ...
RibUpdate & setName(const Name &name)
a collection of common functions shared by all NFD managers and RIB manager, such as communicating wi...
boost::property_tree::ptree ConfigSection
a config file section
optional< time::steady_clock::TimePoint > expires
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
const Name & getName() const
const RouteList & getRoutes() const
ndn::nfd::RouteOrigin origin
void insert(const Name &prefix, const Route &route)
void setExpirationEvent(const scheduler::EventId eid)
represents a route for a name prefix
static const std::string MGMT_MODULE_NAME
void extractRequester(const Interest &interest, ndn::mgmt::AcceptContinuation accept)
extract a requester from a ControlCommand request
static const int FIB_MAX_DEPTH
Maximum number of components in a FIB entry prefix.
void disableLocalhop()
Disallow accepting commands on /localhop/nfd/rib prefix.
#define NFD_LOG_INIT(name)
EventId schedule(time::nanoseconds after, const EventCallback &event)
Schedule an event.
static const Name LOCALHOST_TOP_PREFIX
Represents a RIB entry, which contains one or more Routes with the same prefix.
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.
std::underlying_type< ndn::nfd::RouteFlags >::type flags