30 #include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
31 #include <ndn-cxx/net/face-uri.hpp>
39 , m_scheduler(face.getIoContext())
40 , m_confParam(confParam)
41 , m_adjacencyList(confParam.getAdjacencyList())
42 , m_namePrefixList(confParam.getNamePrefixList())
43 , m_fib(m_face, m_scheduler, m_adjacencyList, m_confParam, keyChain)
44 , m_lsdb(m_face, keyChain, m_confParam)
45 , m_routingTable(m_scheduler, m_lsdb, m_confParam)
46 , m_namePrefixTable(confParam.getRouterPrefix(), m_fib, m_routingTable,
47 m_routingTable.afterRoutingChange, m_lsdb.onLsdbModified)
48 , m_helloProtocol(m_face, keyChain, confParam, m_routingTable, m_lsdb)
49 , m_onNewLsaConnection(m_lsdb.getSync().onNewLsa.connect(
50 [this] (const ndn::Name& updateName, uint64_t sequenceNumber,
51 const ndn::Name& originRouter, uint64_t incomingFaceId) {
52 registerStrategyForCerts(originRouter);
54 , m_onPrefixRegistrationSuccess(m_fib.onPrefixRegistrationSuccess.connect(
55 [
this] (
const ndn::Name& name) {
56 m_helloProtocol.sendHelloInterest(name);
58 , m_onInitialHelloDataValidated(m_helloProtocol.onInitialHelloDataValidated.connect(
59 [
this] (
const ndn::Name& neighbor) {
60 auto it = m_adjacencyList.findAdjacent(neighbor);
61 if (it != m_adjacencyList.end()) {
62 m_fib.registerPrefix(m_confParam.getSyncPrefix(), it->getFaceUri(), it->getLinkCost(),
63 ndn::time::milliseconds::max(), ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
66 , m_dispatcher(m_face, keyChain)
67 , m_datasetHandler(m_dispatcher, m_lsdb, m_routingTable)
68 , m_controller(m_face, keyChain)
69 , m_faceDatasetController(m_face, keyChain)
70 , m_prefixUpdateProcessor(m_dispatcher,
71 m_confParam.getPrefixUpdateValidator(),
74 m_confParam.getConfFileNameDynamic())
75 , m_nfdRibCommandProcessor(m_dispatcher,
78 , m_statsCollector(m_lsdb, m_helloProtocol)
79 , m_faceMonitor(m_face)
83 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification,
this, _1));
84 m_faceMonitor.start();
86 m_fib.setStrategy(m_confParam.getLsaPrefix(), Fib::MULTICAST_STRATEGY, 0);
87 m_fib.setStrategy(m_confParam.getSyncPrefix(), Fib::MULTICAST_STRATEGY, 0);
89 NLSR_LOG_DEBUG(
"Default NLSR identity: " << m_confParam.getSigningInfo().getSignerName());
92 addDispatcherTopPrefix(ndn::Name(m_confParam.getRouterPrefix()).append(
"nlsr"));
93 addDispatcherTopPrefix(LOCALHOST_PREFIX);
95 enableIncomingFaceIdIndication();
97 initializeFaces(std::bind(&Nlsr::processFaceDataset,
this, _1),
98 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
this, _1, _2, 0));
100 m_adjacencyList.writeLog();
105 for (
auto&& neighbor : m_adjacencyList.getAdjList()) {
106 neighbor.setLinkCost(0);
112 Nlsr::registerStrategyForCerts(
const ndn::Name& originRouter)
114 for (
const ndn::Name& router : m_strategySetOnRouters) {
115 if (router == originRouter) {
121 m_strategySetOnRouters.push_back(originRouter);
123 ndn::Name routerKey(originRouter);
124 routerKey.append(ndn::security::Certificate::KEY_COMPONENT);
125 ndn::Name instanceKey(originRouter);
126 instanceKey.append(
"nlsr").append(ndn::security::Certificate::KEY_COMPONENT);
128 m_fib.setStrategy(routerKey, Fib::BEST_ROUTE_STRATEGY, 0);
129 m_fib.setStrategy(instanceKey, Fib::BEST_ROUTE_STRATEGY, 0);
132 for (
size_t i = 0; i < originRouter.size(); ++i) {
133 if (originRouter[i].toUri() ==
"%C1.Router") {
136 siteKey.append(originRouter[i]);
138 ndn::Name opPrefix(siteKey);
139 siteKey.append(ndn::security::Certificate::KEY_COMPONENT);
140 m_fib.setStrategy(siteKey, Fib::BEST_ROUTE_STRATEGY, 0);
142 opPrefix.append(std::string(
"%C1.Operator"));
143 m_fib.setStrategy(opPrefix, Fib::BEST_ROUTE_STRATEGY, 0);
147 Nlsr::addDispatcherTopPrefix(
const ndn::Name& topPrefix)
149 registerPrefix(topPrefix);
152 m_dispatcher.addTopPrefix(topPrefix,
false, m_confParam.getSigningInfo());
154 catch (
const std::exception& e) {
155 NLSR_LOG_ERROR(
"Error setting top-level prefix in dispatcher: " << e.what());
160 Nlsr::registerPrefix(
const ndn::Name& prefix)
162 m_face.registerPrefix(prefix,
163 [] (
const ndn::Name& name) {
166 [] (
const ndn::Name& name,
const std::string& reason) {
167 NLSR_LOG_ERROR(
"ERROR: Failed to register prefix " << name <<
" in local hub's daemon");
168 NDN_THROW(Error(
"Error: Prefix registration failed: " + reason));
173 Nlsr::onFaceEventNotification(
const ndn::nfd::FaceEventNotification& faceEventNotification)
177 switch (faceEventNotification.getKind()) {
178 case ndn::nfd::FACE_EVENT_DESTROYED: {
179 uint64_t faceId = faceEventNotification.getFaceId();
181 auto adjacent = m_adjacencyList.findAdjacent(faceId);
183 if (adjacent != m_adjacencyList.end()) {
184 NLSR_LOG_DEBUG(
"Face to " << adjacent->getName() <<
" with face id: " << faceId <<
" destroyed");
186 adjacent->setFaceId(0);
203 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
204 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
208 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
211 m_routingTable.scheduleRoutingTableCalculation();
216 m_lsdb.scheduleAdjLsaBuild();
222 case ndn::nfd::FACE_EVENT_CREATED: {
224 ndn::FaceUri faceUri;
226 faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri());
228 catch (
const std::exception& e) {
232 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
233 uint64_t faceId = faceEventNotification.getFaceId();
237 if (adjacent != m_adjacencyList.end() &&
238 (adjacent->getFaceId() == 0 || adjacent->getFaceId() != faceId))
240 NLSR_LOG_DEBUG(
"Face creation event matches neighbor: " << adjacent->getName()
241 <<
". New Face ID: " << faceId <<
". Registering prefixes.");
242 adjacent->setFaceId(faceId);
244 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
259 Nlsr::initializeFaces(
const FetchDatasetCallback& onFetchSuccess,
260 const FetchDatasetTimeoutCallback& onFetchFailure)
263 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
267 Nlsr::processFaceDataset(
const std::vector<ndn::nfd::FaceStatus>& faces)
272 for (
auto&& adjacent : m_adjacencyList.getAdjList()) {
274 const std::string& faceUriString = adjacent.getFaceUri().toString();
276 for (
const auto& faceStatus : faces) {
279 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
281 " FaceId: "<< faceStatus.getFaceId());
282 adjacent.setFaceId(faceStatus.getFaceId());
284 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
291 if (adjacent.getFaceId() == 0) {
293 " has no Face information in this dataset.");
297 scheduleDatasetFetch();
301 Nlsr::registerAdjacencyPrefixes(
const Adjacent& adj, ndn::time::milliseconds timeout)
303 ndn::FaceUri faceUri = adj.getFaceUri();
304 double linkCost = adj.getLinkCost();
305 const ndn::Name& adjName = adj.getName();
307 m_fib.registerPrefix(adjName, faceUri, linkCost,
308 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
310 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
311 faceUri, linkCost, timeout,
312 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
316 Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
317 const std::string& msg,
318 uint32_t nRetriesSoFar)
321 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
322 NLSR_LOG_DEBUG(
"Failed to fetch dataset: " << msg <<
". Attempting retry #" << nRetriesSoFar);
323 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
this, _1),
324 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
325 this, _1, _2, nRetriesSoFar));
328 NLSR_LOG_ERROR(
"Failed to fetch dataset: " << msg <<
". Exceeded limit of " <<
329 m_confParam.getFaceDatasetFetchTries() <<
", so not trying again this time.");
333 scheduleDatasetFetch();
338 Nlsr::scheduleDatasetFetch()
340 NLSR_LOG_DEBUG(
"Scheduling dataset fetch in " << m_confParam.getFaceDatasetFetchInterval());
342 m_scheduler.schedule(m_confParam.getFaceDatasetFetchInterval(), [
this] {
344 [this] (const auto& faces) { processFaceDataset(faces); },
345 [
this] (uint32_t code,
const std::string& msg) { onFaceDatasetFetchTimeout(code, msg, 0); });
350 Nlsr::enableIncomingFaceIdIndication()
352 NLSR_LOG_DEBUG(
"Enabling incoming face id indication for local face.");
354 m_controller.start<ndn::nfd::FaceUpdateCommand>(
355 ndn::nfd::ControlParameters()
356 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED,
true),
357 [] (
const ndn::nfd::ControlParameters& cp) {
359 <<
"for face id " << cp.getFaceId());
361 [] (
const ndn::nfd::ControlResponse& cr) {
362 NLSR_LOG_WARN(
"Failed to enable incoming face id indication feature: " <<
363 "(code: " << cr.getCode() <<
", reason: " << cr.getText() <<
")");
A class to house all the configuration parameters for NLSR.
Nlsr(ndn::Face &face, ndn::KeyChain &keyChain, ConfParameter &confParam)
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define NLSR_LOG_DEBUG(x)
#define INIT_LOGGER(name)
#define NLSR_LOG_ERROR(x)
#define NLSR_LOG_TRACE(x)
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.