28 #include <ndn-cxx/lp/tags.hpp>
36 , m_scheduler(face.getIoContext())
37 , m_confParam(confParam)
38 , m_sync(m_face, keyChain,
39 [this] (const auto& routerName,
Lsa::Type lsaType, uint64_t seqNo, uint64_t) {
40 return isLsaNew(routerName, lsaType, seqNo);
43 confParam.getSyncProtocol(),
44 confParam.getSyncPrefix(),
45 confParam.getSyncUserPrefix(),
46 confParam.getSyncInterestLifetime(),
47 confParam.getRouterPrefix(),
48 confParam.getHyperbolicState()
50 , m_lsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()))
51 , m_adjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval())
52 , m_thisRouterPrefix(m_confParam.getRouterPrefix())
53 , m_sequencingManager(m_confParam.getStateFileDir(), m_confParam.getHyperbolicState())
54 , m_onNewLsaConnection(m_sync.onNewLsa.connect(
55 [
this] (
const ndn::Name& updateName, uint64_t sequenceNumber,
56 const ndn::Name& originRouter, uint64_t incomingFaceId) {
57 ndn::Name lsaInterest{updateName};
58 lsaInterest.appendNumber(sequenceNumber);
59 expressInterest(lsaInterest, 0, incomingFaceId);
61 , m_segmenter(keyChain, m_confParam.getSigningInfo())
63 , m_isBuildAdjLsaScheduled(
false)
66 ndn::Name name = m_confParam.getLsaPrefix();
69 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(
false),
70 [
this] (
const auto& name,
const auto& interest) { processInterest(name, interest); },
71 [] (
const auto& name) {
NLSR_LOG_DEBUG(
"Successfully registered prefix: " << name); },
72 [] (
const auto& name,
const auto& reason) {
74 NDN_THROW(std::runtime_error(
"Register prefix failed: " + reason));
76 m_confParam.getSigningInfo(), ndn::nfd::ROUTE_FLAG_CAPTURE);
78 buildAndInstallOwnNameLsa();
81 buildAndInstallOwnCoordinateLsa();
87 for (
const auto& fetcher : m_fetchers) {
93 Lsdb::buildAndInstallOwnNameLsa()
95 NameLsa nameLsa(m_thisRouterPrefix, m_sequencingManager.getNameLsaSeq() + 1,
96 getLsaExpirationTimePoint(), m_confParam.getNamePrefixList());
97 m_sequencingManager.increaseNameLsaSeq();
98 m_sequencingManager.writeSeqNoToFile();
99 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
101 installLsa(std::make_shared<NameLsa>(nameLsa));
105 Lsdb::buildAndInstallOwnCoordinateLsa()
107 CoordinateLsa corLsa(m_thisRouterPrefix, m_sequencingManager.getCorLsaSeq() + 1,
108 getLsaExpirationTimePoint(), m_confParam.getCorR(),
109 m_confParam.getCorTheta());
110 m_sequencingManager.increaseCorLsaSeq();
111 m_sequencingManager.writeSeqNoToFile();
115 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
118 installLsa(std::make_shared<CoordinateLsa>(corLsa));
122 Lsdb::scheduleAdjLsaBuild()
128 NLSR_LOG_DEBUG(
"Adjacency LSA not built while in hyperbolic routing state");
132 if (m_isBuildAdjLsaScheduled) {
133 NLSR_LOG_DEBUG(
"Rescheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
136 NLSR_LOG_DEBUG(
"Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
137 m_isBuildAdjLsaScheduled =
true;
139 m_scheduledAdjLsaBuild = m_scheduler.schedule(m_adjLsaBuildInterval, [
this] { buildAdjLsa(); });
143 Lsdb::writeLog()
const
145 for (
auto type : {Lsa::Type::COORDINATE, Lsa::Type::NAME, Lsa::Type::ADJACENCY}) {
146 if ((type == Lsa::Type::COORDINATE &&
148 (type == Lsa::Type::ADJACENCY &&
153 NLSR_LOG_DEBUG(
"---------------" << type <<
" LSDB-------------------");
154 auto lsaRange = m_lsdb.get<
byType>().equal_range(type);
155 for (
auto lsaIt = lsaRange.first; lsaIt != lsaRange.second; ++lsaIt) {
162 Lsdb::processInterest(
const ndn::Name& name,
const ndn::Interest& interest)
164 ndn::Name interestName(interest.getName());
167 if (interestName[-2].isVersion()) {
169 auto data = m_segmentFifo.find(interestName);
177 interestName = interestName.getPrefix(-2);
178 NLSR_LOG_TRACE(
"Interest w/o segment and version: " << interestName);
182 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
184 std::string chkString(
"LSA");
188 ndn::Name originRouter = m_confParam.getNetwork();
189 originRouter.append(interestName.getSubName(lsaPosition + 1,
190 interestName.size() - lsaPosition - 3));
193 if (originRouter == m_thisRouterPrefix && lsaPosition >= 0) {
194 uint64_t seqNo = interestName[-1].toNumber();
197 std::string lsaType = interestName[-2].toUri();
199 std::istringstream(lsaType) >> interestedLsType;
200 if (interestedLsType == Lsa::Type::BASE) {
201 NLSR_LOG_WARN(
"Received unrecognized LSA type: " << lsaType);
205 incrementInterestRcvdStats(interestedLsType);
206 if (processInterestForLsa(interest, originRouter, interestedLsType, seqNo)) {
207 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
211 else if (
auto lsaSegment = m_lsaStorage.find(interest); lsaSegment) {
212 NLSR_LOG_TRACE(
"Found data in LSA storage. Sending data for " << interest.getName());
213 m_face.put(*lsaSegment);
218 Lsdb::processInterestForLsa(
const ndn::Interest& interest,
const ndn::Name& originRouter,
223 if (
auto lsaPtr = findLsa(originRouter, lsaType); lsaPtr) {
224 NLSR_LOG_TRACE(
"Verifying SeqNo for " << lsaType <<
" is same as requested");
225 if (lsaPtr->getSeqNo() == seqNo) {
226 auto segments = m_segmenter.segment(lsaPtr->wireEncode(),
227 ndn::Name(interest.getName()).appendVersion(),
228 ndn::MAX_NDN_PACKET_SIZE / 2, m_lsaRefreshTime);
229 for (
const auto& data : segments) {
230 m_segmentFifo.insert(*data, m_lsaRefreshTime);
231 m_scheduler.schedule(m_lsaRefreshTime,
232 [
this, name = data->getName()] { m_segmentFifo.erase(name); });
236 if (interest.getName()[-1].isSegment()) {
237 segNum = interest.getName()[-1].toSegment();
239 if (segNum < segments.size()) {
240 m_face.put(*segments[segNum]);
242 incrementDataSentStats(lsaType);
253 Lsdb::installLsa(std::shared_ptr<Lsa> lsa)
255 auto timeToExpire = m_lsaRefreshTime;
256 if (lsa->getOriginRouter() != m_thisRouterPrefix) {
257 auto duration = lsa->getExpirationTimePoint() - ndn::time::system_clock::now();
258 if (duration > ndn::time::seconds(0)) {
259 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
263 auto chkLsa = findLsa(lsa->getOriginRouter(), lsa->getType());
264 if (chkLsa ==
nullptr) {
268 onLsdbModified(lsa, LsdbUpdate::INSTALLED, {}, {});
270 lsa->setExpiringEventId(scheduleLsaExpiration(lsa, timeToExpire));
273 else if (chkLsa->getSeqNo() < lsa->getSeqNo()) {
275 chkLsa->setSeqNo(lsa->getSeqNo());
276 chkLsa->setExpirationTimePoint(lsa->getExpirationTimePoint());
278 auto [updated, namesToAdd, namesToRemove] = chkLsa->update(lsa);
280 onLsdbModified(lsa, LsdbUpdate::UPDATED, namesToAdd, namesToRemove);
283 chkLsa->setExpiringEventId(scheduleLsaExpiration(chkLsa, timeToExpire));
289 Lsdb::removeLsa(
const LsaContainer::index<Lsdb::byName>::type::iterator& lsaIt)
291 if (lsaIt != m_lsdb.end()) {
292 auto lsaPtr = *lsaIt;
295 onLsdbModified(lsaPtr, LsdbUpdate::REMOVED, {}, {});
300 Lsdb::removeLsa(
const ndn::Name& router, Lsa::Type lsaType)
302 removeLsa(m_lsdb.get<byName>().find(std::make_tuple(router, lsaType)));
310 m_isBuildAdjLsaScheduled =
false;
312 if (m_confParam.getAdjacencyList().isAdjLsaBuildable(m_confParam.getInterestRetryNumber())) {
314 int adjBuildCount = m_adjBuildCount;
316 if (adjBuildCount > 0) {
318 if (m_confParam.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
320 buildAndInstallOwnAdjLsa();
328 removeLsa(m_thisRouterPrefix, Lsa::Type::ADJACENCY);
333 m_adjBuildCount = m_adjBuildCount - adjBuildCount;
340 m_isBuildAdjLsaScheduled =
true;
341 auto schedulingTime = ndn::time::seconds(m_confParam.getInterestRetryNumber() *
342 m_confParam.getInterestResendTime());
343 m_scheduledAdjLsaBuild = m_scheduler.schedule(schedulingTime, [
this] { buildAdjLsa(); });
348 Lsdb::buildAndInstallOwnAdjLsa()
350 AdjLsa adjLsa(m_thisRouterPrefix, m_sequencingManager.getAdjLsaSeq() + 1,
351 getLsaExpirationTimePoint(),
352 m_confParam.getAdjacencyList());
353 m_sequencingManager.increaseAdjLsaSeq();
354 m_sequencingManager.writeSeqNoToFile();
358 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
361 installLsa(std::make_shared<AdjLsa>(adjLsa));
364 ndn::scheduler::EventId
365 Lsdb::scheduleLsaExpiration(std::shared_ptr<Lsa> lsa, ndn::time::seconds expTime)
368 return m_scheduler.schedule(expTime +
GRACE_PERIOD, [
this, lsa] { expireOrRefreshLsa(lsa); });
372 Lsdb::expireOrRefreshLsa(std::shared_ptr<Lsa> lsa)
374 NLSR_LOG_DEBUG(
"ExpireOrRefreshLsa called for " << lsa->getType());
375 NLSR_LOG_DEBUG(
"OriginRouter: " << lsa->getOriginRouter() <<
" Seq No: " << lsa->getSeqNo());
377 auto lsaIt = m_lsdb.get<byName>().find(std::make_tuple(lsa->getOriginRouter(), lsa->getType()));
380 if (lsaIt != m_lsdb.end()) {
381 auto lsaPtr = *lsaIt;
385 if (lsaPtr->getSeqNo() == lsa->getSeqNo()) {
386 if (lsaPtr->getOriginRouter() == m_thisRouterPrefix) {
387 NLSR_LOG_DEBUG(
"Own " << lsaPtr->getType() <<
" LSA, so refreshing it");
389 lsaPtr->setSeqNo(lsaPtr->getSeqNo() + 1);
390 m_sequencingManager.setLsaSeq(lsaPtr->getSeqNo(), lsaPtr->getType());
391 lsaPtr->setExpirationTimePoint(getLsaExpirationTimePoint());
394 lsaPtr->setExpiringEventId(scheduleLsaExpiration(lsaPtr, m_lsaRefreshTime));
395 m_sequencingManager.writeSeqNoToFile();
396 m_sync.publishRoutingUpdate(lsaPtr->getType(), m_sequencingManager.getLsaSeq(lsaPtr->getType()));
400 NLSR_LOG_DEBUG(
"Other's " << lsaPtr->getType() <<
" LSA, so removing from LSDB");
408 Lsdb::expressInterest(
const ndn::Name& interestName, uint32_t timeoutCount, uint64_t incomingFaceId,
409 ndn::time::steady_clock::time_point deadline)
412 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
414 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
415 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(
static_cast<int>(
LSA_REFRESH_TIME_MAX));
418 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
420 uint64_t seqNo = interestName[-1].toNumber();
423 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
424 m_highestSeqNo[lsaName] = seqNo;
427 else if (seqNo > m_highestSeqNo[lsaName]) {
428 m_highestSeqNo[lsaName] = seqNo;
431 else if (seqNo < m_highestSeqNo[lsaName]) {
435 ndn::Interest interest(interestName);
436 if (incomingFaceId != 0) {
437 interest.setTag(std::make_shared<ndn::lp::NextHopFaceIdTag>(incomingFaceId));
439 ndn::SegmentFetcher::Options options;
440 options.interestLifetime = m_confParam.getLsaInterestLifetime();
441 options.maxTimeout = m_confParam.getLsaInterestLifetime();
443 NLSR_LOG_DEBUG(
"Fetching Data for LSA: " << interestName <<
" Seq number: " << seqNo);
444 auto fetcher = ndn::SegmentFetcher::start(m_face, interest, m_confParam.getValidator(), options);
446 auto it = m_fetchers.insert(fetcher).first;
448 fetcher->afterSegmentValidated.connect([
this] (
const ndn::Data& data) {
450 afterSegmentValidatedSignal(data);
453 auto lsaSegment = std::make_shared<const ndn::Data>(data);
454 m_lsaStorage.insert(*lsaSegment);
457 [
this, name = lsaSegment->getName()] { m_lsaStorage.erase(name); });
460 fetcher->onComplete.connect([=] (
const ndn::ConstBufferPtr& bufferPtr) {
461 m_lsaStorage.erase(ndn::Name(lsaName).appendNumber(seqNo - 1));
462 afterFetchLsa(bufferPtr, interestName);
463 m_fetchers.erase(it);
466 fetcher->onError.connect([=] (uint32_t errorCode,
const std::string& msg) {
467 onFetchLsaError(errorCode, msg, interestName, timeoutCount, deadline, lsaName, seqNo);
468 m_fetchers.erase(it);
472 std::istringstream(interestName[-2].toUri()) >> lsaType;
473 incrementInterestSentStats(lsaType);
477 Lsdb::onFetchLsaError(uint32_t errorCode,
const std::string& msg,
const ndn::Name& interestName,
478 uint32_t retransmitNo,
const ndn::time::steady_clock::time_point& deadline,
479 ndn::Name lsaName, uint64_t seqNo)
481 NLSR_LOG_DEBUG(
"Failed to fetch LSA: " << lsaName <<
", Error code: " << errorCode
482 <<
", Message: " << msg);
484 if (ndn::time::steady_clock::now() < deadline) {
485 auto it = m_highestSeqNo.find(lsaName);
486 if (it != m_highestSeqNo.end() && it->second == seqNo) {
491 auto delay = m_confParam.getLsaInterestLifetime();
492 if (errorCode == ndn::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
495 m_scheduler.schedule(delay, std::bind(&Lsdb::expressInterest,
this, interestName,
496 retransmitNo + 1, 0, deadline));
502 Lsdb::afterFetchLsa(
const ndn::ConstBufferPtr& bufferPtr,
const ndn::Name& interestName)
504 NLSR_LOG_DEBUG(
"Received data for LSA interest: " << interestName);
505 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
507 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
508 uint64_t seqNo = interestName[-1].toNumber();
510 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
511 m_highestSeqNo[lsaName] = seqNo;
513 else if (seqNo > m_highestSeqNo[lsaName]) {
514 m_highestSeqNo[lsaName] = seqNo;
515 NLSR_LOG_TRACE(
"SeqNo for LSA(name): " << interestName <<
" updated");
517 else if (seqNo < m_highestSeqNo[lsaName]) {
521 std::string chkString(
"LSA");
524 if (lsaPosition >= 0) {
526 ndn::Name originRouter = m_confParam.getNetwork();
527 originRouter.append(interestName.getSubName(lsaPosition + 1,
528 interestName.size() - lsaPosition - 3));
530 Lsa::Type interestedLsType;
531 std::istringstream(interestName[-2].toUri()) >> interestedLsType;
533 if (interestedLsType == Lsa::Type::BASE) {
534 NLSR_LOG_WARN(
"Received unrecognized LSA Type: " << interestName[-2].toUri());
538 ndn::Block block(bufferPtr);
539 if (interestedLsType == Lsa::Type::NAME) {
540 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
541 if (isLsaNew(originRouter, interestedLsType, seqNo)) {
542 installLsa(std::make_shared<NameLsa>(block));
545 else if (interestedLsType == Lsa::Type::ADJACENCY) {
546 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
547 if (isLsaNew(originRouter, interestedLsType, seqNo)) {
548 installLsa(std::make_shared<AdjLsa>(block));
551 else if (interestedLsType == Lsa::Type::COORDINATE) {
552 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
553 if (isLsaNew(originRouter, interestedLsType, seqNo)) {
554 installLsa(std::make_shared<CoordinateLsa>(block));
558 catch (
const std::exception& e) {
A class to house all the configuration parameters for NLSR.
Represents an LSA of hyperbolic coordinates of the origin router.
Represents a Link State Announcement (LSA).
Lsdb(ndn::Face &face, ndn::KeyChain &keyChain, ConfParameter &confParam)
Represents an LSA of name prefixes announced by the origin router.
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)
int32_t getNameComponentPosition(const ndn::Name &name, const std::string &searchString)
search a name component in ndn::Name and return the position of the component
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
@ LSA_REFRESH_TIME_DEFAULT
constexpr ndn::time::seconds GRACE_PERIOD