blob: e3fe9d3ef898815f513b62d4cc3760b588d16422 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoaf7a2112019-03-19 14:55:20 -04002/*
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
akmhoque53353462014-04-22 08:43:45 -050022#include "lsdb.hpp"
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050023
akmhoque674b0b12014-05-20 14:33:28 -050024#include "logger.hpp"
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050025#include "nlsr.hpp"
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050026#include "utility/name-helper.hpp"
27
28#include <ndn-cxx/security/signing-helpers.hpp>
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050029
akmhoque53353462014-04-22 08:43:45 -050030namespace nlsr {
31
dmcoomescf8d0ed2017-02-21 11:39:01 -060032INIT_LOGGER(Lsdb);
akmhoque674b0b12014-05-20 14:33:28 -050033
Jiewen Tana0497d82015-02-02 21:59:18 -080034const ndn::Name::Component Lsdb::NAME_COMPONENT = ndn::Name::Component("lsdb");
Vince Lehman18841082014-08-19 17:15:24 -050035const ndn::time::seconds Lsdb::GRACE_PERIOD = ndn::time::seconds(10);
Nick Gordone98480b2017-05-24 11:23:03 -050036const ndn::time::steady_clock::TimePoint Lsdb::DEFAULT_LSA_RETRIEVAL_DEADLINE =
37 ndn::time::steady_clock::TimePoint::min();
Vince Lehman18841082014-08-19 17:15:24 -050038
Saurab Dulal427e0122019-11-28 11:58:02 -060039Lsdb::Lsdb(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060040 NamePrefixTable& namePrefixTable, RoutingTable& routingTable)
41 : m_face(face)
42 , m_scheduler(face.getIoService())
Ashlesh Gawande85998a12017-12-07 22:22:13 -060043 , m_confParam(confParam)
44 , m_namePrefixTable(namePrefixTable)
45 , m_routingTable(routingTable)
46 , m_sync(m_face,
Nick Gordon727d4832017-10-13 18:04:25 -050047 [this] (const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon9eac4d92017-08-29 17:31:29 -050048 const uint64_t& sequenceNumber) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -050049 return isLsaNew(routerName, lsaType, sequenceNumber);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060050 }, m_confParam)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060051 , m_lsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()))
52 , m_thisRouterPrefix(m_confParam.getRouterPrefix().toUri())
53 , m_adjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval())
dulalsaurab82a34c22019-02-04 17:31:21 +000054 , m_sequencingManager(m_confParam.getStateFileDir(), m_confParam.getHyperbolicState())
Nick Gordon9eac4d92017-08-29 17:31:29 -050055 , m_onNewLsaConnection(m_sync.onNewLsa->connect(
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -050056 [this] (const ndn::Name& updateName, uint64_t sequenceNumber,
57 const ndn::Name& originRouter) {
Nick Gordon9eac4d92017-08-29 17:31:29 -050058 ndn::Name lsaInterest{updateName};
59 lsaInterest.appendNumber(sequenceNumber);
60 expressInterest(lsaInterest, 0);
61 }))
Ashlesh Gawande85998a12017-12-07 22:22:13 -060062 , m_segmentPublisher(m_face, keyChain)
63 , m_isBuildAdjLsaSheduled(false)
64 , m_adjBuildCount(0)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050065{
66}
67
Ashlesh Gawande744e4812018-08-22 16:26:24 -050068Lsdb::~Lsdb()
69{
70 for (const auto& sp : m_fetchers) {
71 sp->stop();
72 }
73}
74
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050075void
76Lsdb::onFetchLsaError(uint32_t errorCode,
77 const std::string& msg,
Ashlesh Gawande744e4812018-08-22 16:26:24 -050078 const ndn::Name& interestName,
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050079 uint32_t retransmitNo,
80 const ndn::time::steady_clock::TimePoint& deadline,
81 ndn::Name lsaName,
82 uint64_t seqNo)
83{
dmcoomes5bcb39e2017-10-31 15:07:55 -050084 NLSR_LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040085 << ", Message: " << msg);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050086
87 if (ndn::time::steady_clock::now() < deadline) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040088 auto it = m_highestSeqNo.find(lsaName);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050089 if (it != m_highestSeqNo.end() && it->second == seqNo) {
90 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
91 // immediately since at the least the LSA Interest lifetime has elapsed.
92 // Otherwise, it is necessary to delay the Interest re-expression to prevent
93 // the potential for constant Interest flooding.
Ashlesh Gawande85998a12017-12-07 22:22:13 -060094 ndn::time::seconds delay = m_confParam.getLsaInterestLifetime();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050095
96 if (errorCode == ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
97 delay = ndn::time::seconds(0);
98 }
99
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400100 m_scheduler.schedule(delay, std::bind(&Lsdb::expressInterest, this,
101 interestName, retransmitNo + 1, deadline));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500102 }
103 }
104}
105
106void
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500107Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& interestName)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500108{
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800109 NLSR_LOG_DEBUG("Received data for LSA interest: " << interestName);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500110
111 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
112 uint64_t seqNo = interestName[-1].toNumber();
113
114 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
115 m_highestSeqNo[lsaName] = seqNo;
116 }
117 else if (seqNo > m_highestSeqNo[lsaName]) {
118 m_highestSeqNo[lsaName] = seqNo;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800119 NLSR_LOG_TRACE("SeqNo for LSA(name): " << interestName << " updated");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500120 }
121 else if (seqNo < m_highestSeqNo[lsaName]) {
122 return;
123 }
124
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800125 std::string chkString("LSA");
126 int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString);
127
128 if (lsaPosition >= 0) {
129 // Extracts the prefix of the originating router from the data.
130 ndn::Name originRouter = m_confParam.getNetwork();
131 originRouter.append(interestName.getSubName(lsaPosition + 1,
132 interestName.size() - lsaPosition - 3));
133
134 try {
135 ndn::Block block(bufferPtr);
136 Lsa::Type interestedLsType;
137 std::istringstream(interestName[-2].toUri()) >> interestedLsType;
138
139 if (interestedLsType == Lsa::Type::NAME) {
140 processContentNameLsa(originRouter.append(boost::lexical_cast<std::string>(interestedLsType)),
141 seqNo,
142 block);
143 }
144 else if (interestedLsType == Lsa::Type::ADJACENCY) {
145 processContentAdjacencyLsa(originRouter.append(boost::lexical_cast<std::string>(interestedLsType)),
146 seqNo,
147 block);
148 }
149 else if (interestedLsType == Lsa::Type::COORDINATE) {
150 processContentCoordinateLsa(originRouter.append(boost::lexical_cast<std::string>(interestedLsType)),
151 seqNo,
152 block);
153 }
154 else {
155 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
156 }
157 }
158 catch (const std::exception& e) {
159 NLSR_LOG_TRACE("LSA data decoding error :( " << e.what());
160 return;
161 }
162
163 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
164 }
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500165}
akmhoque53353462014-04-22 08:43:45 -0500166
Nick G97e34942016-07-11 14:46:27 -0500167 /*! \brief Compares if a name LSA is the same as the one specified by key
168
169 \param nlsa1 A name LSA object
170 \param key A key of an originating router to compare to nlsa1
171 */
akmhoque53353462014-04-22 08:43:45 -0500172static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500173nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500174{
175 return nlsa1.getKey() == key;
176}
177
akmhoque53353462014-04-22 08:43:45 -0500178bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500179Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -0500180{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600181 NameLsa nameLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500182 m_sequencingManager.getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500183 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600184 m_confParam.getNamePrefixList());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500185 m_sequencingManager.increaseNameLsaSeq();
186
187 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500188 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500189
akmhoque31d1d4b2014-05-05 22:08:14 -0500190 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500191}
192
akmhoqueb6450b12014-04-24 00:01:03 -0500193NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500194Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500195{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400196 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
197 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500198 if (it != m_nameLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400199 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500200 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400201 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500202}
203
204bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500205Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500206{
akmhoqueb6450b12014-04-24 00:01:03 -0500207 NameLsa* nameLsaCheck = findNameLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500208 // Is the name in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400209 if (nameLsaCheck != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500210 // And the supplied seq no is the highest so far
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800211 if (nameLsaCheck->getSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500212 return true;
213 }
akmhoque157b0a42014-05-13 00:26:37 -0500214 else {
akmhoque53353462014-04-22 08:43:45 -0500215 return false;
216 }
217 }
218 return true;
219}
220
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400221ndn::scheduler::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500222Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
223 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500224{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400225 return m_scheduler.schedule(expTime + GRACE_PERIOD,
226 std::bind(&Lsdb::expireOrRefreshNameLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500227}
228
229bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500230Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500231{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000232 NLSR_LOG_TRACE("installNameLsa");
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700233 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500234 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500235 // Determines if the name LSA is new or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400236 if (chkNameLsa == nullptr) {
akmhoque53353462014-04-22 08:43:45 -0500237 addNameLsa(nlsa);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500238 NLSR_LOG_DEBUG("New Name LSA");
239 NLSR_LOG_DEBUG("Adding Name Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800240 NLSR_LOG_DEBUG(nlsa);
akmhoque674b0b12014-05-20 14:33:28 -0500241
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800242 NLSR_LOG_TRACE("nlsa.getOriginRouter(): " << nlsa.getOriginRouter());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600243 NLSR_LOG_TRACE("m_confParam.getRouterPrefix(): " << m_confParam.getRouterPrefix());
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000244
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800245 if (nlsa.getOriginRouter() != m_confParam.getRouterPrefix()) {
Nick G97e34942016-07-11 14:46:27 -0500246 // If this name LSA is from another router, add the advertised
247 // prefixes to the NPT.
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800248 m_namePrefixTable.addEntry(nlsa.getOriginRouter(), nlsa.getOriginRouter());
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500249
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500250 for (const auto& name : nlsa.getNpl().getNames()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600251 if (name != m_confParam.getRouterPrefix()) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800252 m_namePrefixTable.addEntry(name, nlsa.getOriginRouter());
akmhoque53353462014-04-22 08:43:45 -0500253 }
254 }
dulalsaurabd0816a32019-07-26 13:11:24 +0000255 auto duration = nlsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500256 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500257 }
dulalsaurabd0816a32019-07-26 13:11:24 +0000258
akmhoque31d1d4b2014-05-05 22:08:14 -0500259 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800260 nlsa.getSeqNo(),
akmhoque53353462014-04-22 08:43:45 -0500261 timeToExpire));
262 }
Nick G97e34942016-07-11 14:46:27 -0500263 // Else this is a known name LSA, so we are updating it.
akmhoque157b0a42014-05-13 00:26:37 -0500264 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000265 NLSR_LOG_TRACE("Known name lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800266 NLSR_LOG_TRACE("chkNameLsa->getSeqNo(): " << chkNameLsa->getSeqNo());
267 NLSR_LOG_TRACE("nlsa.getSeqNo(): " << nlsa.getSeqNo());
268 if (chkNameLsa->getSeqNo() < nlsa.getSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500269 NLSR_LOG_DEBUG("Updated Name LSA. Updating LSDB");
270 NLSR_LOG_DEBUG("Deleting Name Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800271 NLSR_LOG_DEBUG(chkNameLsa);
272 chkNameLsa->setSeqNo(nlsa.getSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500273 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500274 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500275 nlsa.getNpl().sort();
Nick G97e34942016-07-11 14:46:27 -0500276 // Obtain the set difference of the current and the incoming
277 // name prefix sets, and add those.
Nick Gordonf14ec352017-07-24 16:09:58 -0500278 std::list<ndn::Name> newNames = nlsa.getNpl().getNames();
279 std::list<ndn::Name> oldNames = chkNameLsa->getNpl().getNames();
280 std::list<ndn::Name> namesToAdd;
281 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
282 std::inserter(namesToAdd, namesToAdd.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500283 for (const auto& name : namesToAdd) {
284 chkNameLsa->addName(name);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800285 if (nlsa.getOriginRouter() != m_confParam.getRouterPrefix()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600286 if (name != m_confParam.getRouterPrefix()) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800287 m_namePrefixTable.addEntry(name, nlsa.getOriginRouter());
akmhoque53353462014-04-22 08:43:45 -0500288 }
289 }
290 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500291
292 chkNameLsa->getNpl().sort();
293
Nick G97e34942016-07-11 14:46:27 -0500294 // Also remove any names that are no longer being advertised.
Nick Gordonf14ec352017-07-24 16:09:58 -0500295 std::list<ndn::Name> namesToRemove;
296 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
297 std::inserter(namesToRemove, namesToRemove.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500298 for (const auto& name : namesToRemove) {
299 NLSR_LOG_DEBUG("Removing name LSA no longer advertised: " << name);
300 chkNameLsa->removeName(name);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800301 if (nlsa.getOriginRouter() != m_confParam.getRouterPrefix()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600302 if (name != m_confParam.getRouterPrefix()) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800303 m_namePrefixTable.removeEntry(name, nlsa.getOriginRouter());
akmhoque53353462014-04-22 08:43:45 -0500304 }
305 }
306 }
dmcoomes9eaf3f42017-02-21 11:39:01 -0600307
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800308 if (nlsa.getOriginRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400309 auto duration = nlsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500310 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500311 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400312 chkNameLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500313 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800314 nlsa.getSeqNo(),
akmhoqueb6450b12014-04-24 00:01:03 -0500315 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500316 NLSR_LOG_DEBUG("Adding Name Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800317 NLSR_LOG_DEBUG(chkNameLsa);
akmhoque53353462014-04-22 08:43:45 -0500318 }
319 }
320 return true;
321}
322
323bool
324Lsdb::addNameLsa(NameLsa& nlsa)
325{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400326 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
327 std::bind(nameLsaCompareByKey, _1, nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500328 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500329 m_nameLsdb.push_back(nlsa);
330 return true;
331 }
332 return false;
333}
334
335bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500336Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500337{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400338 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
339 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500340 if (it != m_nameLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500341 NLSR_LOG_DEBUG("Deleting Name Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800342 NLSR_LOG_DEBUG(*it);
Nick G97e34942016-07-11 14:46:27 -0500343 // If the requested name LSA is not ours, we also need to remove
344 // its entries from the NPT.
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800345 if (it->getOriginRouter() != m_confParam.getRouterPrefix()) {
346 m_namePrefixTable.removeEntry(it->getOriginRouter(), it->getOriginRouter());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600347
Nick Gordonf14ec352017-07-24 16:09:58 -0500348 for (const auto& name : it->getNpl().getNames()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600349 if (name != m_confParam.getRouterPrefix()) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800350 m_namePrefixTable.removeEntry(name, it->getOriginRouter());
akmhoque53353462014-04-22 08:43:45 -0500351 }
352 }
353 }
354 m_nameLsdb.erase(it);
355 return true;
356 }
357 return false;
358}
359
360bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500361Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500362{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400363 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
364 std::bind(nameLsaCompareByKey, _1, key));
365 return it != m_nameLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500366}
367
368void
akmhoque2f423352014-06-03 11:49:35 -0500369Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500370{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500371 NLSR_LOG_DEBUG("---------------Name LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500372 for (const auto& nlsa : m_nameLsdb) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800373 NLSR_LOG_DEBUG(nlsa);
akmhoque53353462014-04-22 08:43:45 -0500374 }
375}
376
Jiewen Tana0497d82015-02-02 21:59:18 -0800377const std::list<NameLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500378Lsdb::getNameLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800379{
380 return m_nameLsdb;
381}
382
akmhoque53353462014-04-22 08:43:45 -0500383// Cor LSA and LSDB related Functions start here
384
Nick G97e34942016-07-11 14:46:27 -0500385/*! \brief Compares whether an LSA object is the same as a key.
386 \param clsa The cor. LSA to check the identity of.
387 \param key The key of the publishing router to check against.
388*/
akmhoque53353462014-04-22 08:43:45 -0500389static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500390corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500391{
392 return clsa.getKey() == key;
393}
394
395bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500396Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500397{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600398 CoordinateLsa corLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500399 m_sequencingManager.getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500400 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600401 m_confParam.getCorR(),
402 m_confParam.getCorTheta());
Nick Gordon5c467f02016-07-13 13:40:10 -0500403
404 // Sync coordinate LSAs if using HR or HR dry run.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600405 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500406 m_sequencingManager.increaseCorLsaSeq();
407 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500408 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500409 }
410
akmhoque31d1d4b2014-05-05 22:08:14 -0500411 installCoordinateLsa(corLsa);
Nick Gordon5c467f02016-07-13 13:40:10 -0500412
akmhoque53353462014-04-22 08:43:45 -0500413 return true;
414}
415
akmhoqueb6450b12014-04-24 00:01:03 -0500416CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500417Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500418{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400419 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
420 std::bind(corLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500421 if (it != m_corLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400422 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500423 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400424 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500425}
426
427bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500428Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500429{
akmhoqueb6450b12014-04-24 00:01:03 -0500430 CoordinateLsa* clsa = findCoordinateLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500431 // Is the coordinate LSA in the LSDB already
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400432 if (clsa != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500433 // And the seq no is newer (higher) than the current one
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800434 if (clsa->getSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500435 return true;
436 }
akmhoque157b0a42014-05-13 00:26:37 -0500437 else {
akmhoque53353462014-04-22 08:43:45 -0500438 return false;
439 }
440 }
441 return true;
442}
443
Nick G97e34942016-07-11 14:46:27 -0500444 // Schedules a refresh/expire event in the scheduler.
445 // \param key The name of the router that published the LSA.
446 // \param seqNo the seq. no. associated with the LSA to check.
447 // \param expTime How long to wait before triggering the event.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400448ndn::scheduler::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500449Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500450 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500451{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400452 return m_scheduler.schedule(expTime + GRACE_PERIOD,
453 std::bind(&Lsdb::expireOrRefreshCoordinateLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500454}
455
456bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500457Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500458{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700459 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500460 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500461 // Checking whether the LSA is new or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400462 if (chkCorLsa == nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500463 NLSR_LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
464 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800465 NLSR_LOG_DEBUG(clsa);
akmhoqueb6450b12014-04-24 00:01:03 -0500466 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500467
Nick Gordon5c467f02016-07-13 13:40:10 -0500468 // Register the LSA's origin router prefix
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800469 if (clsa.getOriginRouter() != m_confParam.getRouterPrefix()) {
470 m_namePrefixTable.addEntry(clsa.getOriginRouter(), clsa.getOriginRouter());
akmhoque53353462014-04-22 08:43:45 -0500471 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600472 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
473 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500474 }
Nick G97e34942016-07-11 14:46:27 -0500475 // Set the expiration time for the new LSA.
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800476 if (clsa.getOriginRouter() != m_confParam.getRouterPrefix()) {
477 auto duration = clsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500478 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500479 }
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800480 scheduleCoordinateLsaExpiration(clsa.getKey(), clsa.getSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500481 }
Nick G97e34942016-07-11 14:46:27 -0500482 // We are just updating this LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500483 else {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800484 if (chkCorLsa->getSeqNo() < clsa.getSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500485 NLSR_LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
486 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800487 NLSR_LOG_DEBUG(chkCorLsa);
488 chkCorLsa->setSeqNo(clsa.getSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500489 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500490 // If the new LSA contains new routing information, update the LSDB with it.
akmhoque157b0a42014-05-13 00:26:37 -0500491 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500492 chkCorLsa->setCorRadius(clsa.getCorRadius());
493 chkCorLsa->setCorTheta(clsa.getCorTheta());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600494 if (m_confParam.getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
495 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500496 }
497 }
Nick G97e34942016-07-11 14:46:27 -0500498 // If this is an LSA from another router, refresh its expiration time.
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800499 if (clsa.getOriginRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400500 auto duration = clsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500501 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500502 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400503 chkCorLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500504 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800505 clsa.getSeqNo(),
akmhoqueb6450b12014-04-24 00:01:03 -0500506 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500507 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800508 NLSR_LOG_DEBUG(chkCorLsa);
akmhoque53353462014-04-22 08:43:45 -0500509 }
510 }
511 return true;
512}
513
514bool
akmhoqueb6450b12014-04-24 00:01:03 -0500515Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500516{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400517 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
518 std::bind(corLsaCompareByKey, _1, clsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500519 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500520 m_corLsdb.push_back(clsa);
521 return true;
522 }
523 return false;
524}
525
526bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500527Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500528{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400529 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
530 std::bind(corLsaCompareByKey, _1, key));
531
akmhoque157b0a42014-05-13 00:26:37 -0500532 if (it != m_corLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500533 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800534 NLSR_LOG_DEBUG(*it);
Nick Gordon5c467f02016-07-13 13:40:10 -0500535
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800536 if (it->getOriginRouter() != m_confParam.getRouterPrefix()) {
537 m_namePrefixTable.removeEntry(it->getOriginRouter(), it->getOriginRouter());
akmhoque53353462014-04-22 08:43:45 -0500538 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500539
akmhoque53353462014-04-22 08:43:45 -0500540 m_corLsdb.erase(it);
541 return true;
542 }
543 return false;
544}
545
546bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500547Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500548{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400549 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
550 std::bind(corLsaCompareByKey, _1, key));
551 return it != m_corLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500552}
553
554void
akmhoque2f423352014-06-03 11:49:35 -0500555Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500556{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500557 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
558 return;
559 }
560
dmcoomes5bcb39e2017-10-31 15:07:55 -0500561 NLSR_LOG_DEBUG("---------------Cor LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500562 for (const auto& corLsa : m_corLsdb) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800563 NLSR_LOG_DEBUG(corLsa);
akmhoque53353462014-04-22 08:43:45 -0500564 }
565}
566
Jiewen Tana0497d82015-02-02 21:59:18 -0800567const std::list<CoordinateLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500568Lsdb::getCoordinateLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800569{
570 return m_corLsdb;
571}
572
akmhoque53353462014-04-22 08:43:45 -0500573// Adj LSA and LSDB related function starts here
574
Nick G97e34942016-07-11 14:46:27 -0500575 /*! \brief Returns whether an adj. LSA object is from some router.
576 \param alsa The adj. LSA object.
577 \param key The router name that you want to compare the LSA with.
578 */
akmhoque53353462014-04-22 08:43:45 -0500579static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500580adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500581{
582 return alsa.getKey() == key;
583}
584
akmhoque53353462014-04-22 08:43:45 -0500585void
Vince Lehman50df6b72015-03-03 12:06:40 -0600586Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500587{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600588 m_adjBuildCount++;
Vince Lehman50df6b72015-03-03 12:06:40 -0600589
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600590 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Nick Gordon5c467f02016-07-13 13:40:10 -0500591 // Don't build adjacency LSAs in hyperbolic routing
dmcoomes5bcb39e2017-10-31 15:07:55 -0500592 NLSR_LOG_DEBUG("Adjacency LSA not built. Currently in hyperbolic routing state.");
Nick Gordon5c467f02016-07-13 13:40:10 -0500593 return;
594 }
595
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500596 if (m_isBuildAdjLsaSheduled) {
597 NLSR_LOG_DEBUG("Rescheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
598 }
599 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500600 NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600601 m_isBuildAdjLsaSheduled = true;
Vince Lehman50df6b72015-03-03 12:06:40 -0600602 }
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500603 m_scheduledAdjLsaBuild = m_scheduler.schedule(m_adjLsaBuildInterval, [this] { buildAdjLsa(); });
Vince Lehman50df6b72015-03-03 12:06:40 -0600604}
605
606void
607Lsdb::buildAdjLsa()
608{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500609 NLSR_LOG_TRACE("Lsdb::buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600610
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600611 m_isBuildAdjLsaSheduled = false;
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500612
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600613 if (m_confParam.getAdjacencyList().isAdjLsaBuildable(m_confParam.getInterestRetryNumber())) {
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500614
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600615 int adjBuildCount = m_adjBuildCount;
Nick G97e34942016-07-11 14:46:27 -0500616 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500617 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500618 // It only makes sense to do the adjLsa build if we have neighbors
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600619 if (m_confParam.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500620 NLSR_LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500621 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500622 }
Nick G97e34942016-07-11 14:46:27 -0500623 // We have no active neighbors, meaning no one can route through
624 // us. So delete our entry in the LSDB. This prevents this
625 // router from refreshing the LSA, eventually causing other
626 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500627 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500628 NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Nick G97e34942016-07-11 14:46:27 -0500629 // Get this router's key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600630 ndn::Name key = m_confParam.getRouterPrefix();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800631 key.append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500632
akmhoque31d1d4b2014-05-05 22:08:14 -0500633 removeAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500634 // Recompute routing table after removal
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600635 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500636 }
Nick G97e34942016-07-11 14:46:27 -0500637 // In the case that during building the adj LSA, the FIB has to
638 // wait on an Interest response, the number of scheduled adj LSA
639 // builds could change, so we shouldn't just set it to 0.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600640 m_adjBuildCount = m_adjBuildCount - adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500641 }
642 }
Nick G97e34942016-07-11 14:46:27 -0500643 // We are still waiting to know the adjacency status of some
644 // neighbor, so schedule a build for later (when all that has
645 // hopefully finished)
646 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600647 m_isBuildAdjLsaSheduled = true;
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500648 auto schedulingTime = ndn::time::seconds(m_confParam.getInterestRetryNumber() *
649 m_confParam.getInterestResendTime());
650 m_scheduledAdjLsaBuild = m_scheduler.schedule(schedulingTime, [this] { buildAdjLsa(); });
Nick G97e34942016-07-11 14:46:27 -0500651 }
akmhoque53353462014-04-22 08:43:45 -0500652}
653
akmhoque53353462014-04-22 08:43:45 -0500654bool
655Lsdb::addAdjLsa(AdjLsa& alsa)
656{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400657 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
658 std::bind(adjLsaCompareByKey, _1, alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500659 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500660 m_adjLsdb.push_back(alsa);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600661 // Add any new name prefixes to the NPT
662 // Only add NPT entries if this is an adj LSA from another router.
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800663 if (alsa.getOriginRouter() != m_confParam.getRouterPrefix()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600664 // Pass the originating router as both the name to register and
665 // where it came from.
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800666 m_namePrefixTable.addEntry(alsa.getOriginRouter(), alsa.getOriginRouter());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600667 }
akmhoque53353462014-04-22 08:43:45 -0500668 return true;
669 }
670 return false;
671}
672
akmhoqueb6450b12014-04-24 00:01:03 -0500673AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500674Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500675{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400676 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
677 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500678 if (it != m_adjLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400679 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500680 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400681 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500682}
683
akmhoque53353462014-04-22 08:43:45 -0500684bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500685Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500686{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400687 AdjLsa* adjLsaCheck = findAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500688 // If it is in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400689 if (adjLsaCheck != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500690 // And the supplied seq no is newer (higher) than the current one.
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800691 if (adjLsaCheck->getSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500692 return true;
693 }
akmhoque157b0a42014-05-13 00:26:37 -0500694 else {
akmhoque53353462014-04-22 08:43:45 -0500695 return false;
696 }
697 }
698 return true;
699}
700
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400701ndn::scheduler::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500702Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
703 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500704{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400705 return m_scheduler.schedule(expTime + GRACE_PERIOD,
706 std::bind(&Lsdb::expireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500707}
708
709bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500710Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500711{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700712 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500713 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500714 // If this adj. LSA is not in the LSDB already
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400715 if (chkAdjLsa == nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500716 NLSR_LOG_DEBUG("New Adj LSA. Adding to LSDB");
717 NLSR_LOG_DEBUG("Adding Adj Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800718 NLSR_LOG_DEBUG(alsa);
akmhoque53353462014-04-22 08:43:45 -0500719 addAdjLsa(alsa);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600720
721 m_routingTable.scheduleRoutingTableCalculation();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800722 if (alsa.getOriginRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500723 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
724 ndn::time::system_clock::now();
725 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500726 }
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800727 scheduleAdjLsaExpiration(alsa.getKey(), alsa.getSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500728 }
akmhoque157b0a42014-05-13 00:26:37 -0500729 else {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800730 if (chkAdjLsa->getSeqNo() < alsa.getSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500731 NLSR_LOG_DEBUG("Updated Adj LSA. Updating LSDB");
732 NLSR_LOG_DEBUG("Deleting Adj Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800733 NLSR_LOG_DEBUG(chkAdjLsa);
734 chkAdjLsa->setSeqNo(alsa.getSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500735 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500736 // If the new adj LSA has new content, update the contents of
737 // the LSDB entry. Additionally, since we've changed the
738 // contents of the LSDB, we have to schedule a routing
739 // calculation.
akmhoque157b0a42014-05-13 00:26:37 -0500740 if (!chkAdjLsa->isEqualContent(alsa)) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800741 chkAdjLsa->resetAdl();
742 for (const auto& adjacent : alsa.getAdl()) {
743 chkAdjLsa->addAdjacent(adjacent);
744 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600745 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500746 }
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800747 if (alsa.getOriginRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400748 auto duration = alsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500749 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500750 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400751 chkAdjLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500752 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800753 alsa.getSeqNo(),
akmhoqueb6450b12014-04-24 00:01:03 -0500754 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500755 NLSR_LOG_DEBUG("Adding Adj Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800756 NLSR_LOG_DEBUG(chkAdjLsa);
akmhoque53353462014-04-22 08:43:45 -0500757 }
758 }
759 return true;
760}
761
762bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500763Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500764{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600765 AdjLsa adjLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500766 m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500767 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600768 m_confParam.getAdjacencyList().getNumOfActiveNeighbor(),
769 m_confParam.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500770
Nick Gordon5c467f02016-07-13 13:40:10 -0500771 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600772 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500773 m_sequencingManager.increaseAdjLsaSeq();
774 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500775 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500776 }
Vince Lehman904c2412014-09-23 19:36:11 -0500777
Vince Lehman9d097802015-03-16 17:55:59 -0500778 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500779}
780
781bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500782Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500783{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400784 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
785 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500786 if (it != m_adjLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500787 NLSR_LOG_DEBUG("Deleting Adj Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800788 NLSR_LOG_DEBUG(*it);
789 if (it->getOriginRouter() != m_confParam.getRouterPrefix()) {
790 m_namePrefixTable.removeEntry(it->getOriginRouter(), it->getOriginRouter());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600791 }
akmhoque53353462014-04-22 08:43:45 -0500792 m_adjLsdb.erase(it);
793 return true;
794 }
795 return false;
796}
797
798bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500799Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500800{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400801 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
802 std::bind(adjLsaCompareByKey, _1, key));
803 return it != m_adjLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500804}
805
Jiewen Tana0497d82015-02-02 21:59:18 -0800806const std::list<AdjLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500807Lsdb::getAdjLsdb() const
akmhoque53353462014-04-22 08:43:45 -0500808{
809 return m_adjLsdb;
810}
811
Nick G97e34942016-07-11 14:46:27 -0500812 // This function determines whether a name LSA should be refreshed
813 // or expired. The conditions for getting refreshed are: it is still
814 // in the LSDB, it hasn't been updated by something else already (as
815 // evidenced by its seq. no.), and this is the originating router for
816 // the LSA. Is it let expire in all other cases.
817 // lsaKey is the key of the LSA's publishing router.
818 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500819void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500820Lsdb::expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500821{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500822 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshNameLsa Called");
823 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500824 NameLsa* chkNameLsa = findNameLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500825 // If this name LSA exists in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400826 if (chkNameLsa != nullptr) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800827 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500828 // If its seq no is the one we are expecting.
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800829 if (chkNameLsa->getSeqNo() == seqNo) {
830 if (chkNameLsa->getOriginRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500831 NLSR_LOG_DEBUG("Own Name LSA, so refreshing it");
832 NLSR_LOG_DEBUG("Deleting Name Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800833 NLSR_LOG_DEBUG(chkNameLsa);
834 chkNameLsa->setSeqNo(chkNameLsa->getSeqNo() + 1);
835 m_sequencingManager.setNameLsaSeq(chkNameLsa->getSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500836 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500837 NLSR_LOG_DEBUG("Adding Name Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800838 NLSR_LOG_DEBUG(chkNameLsa);
akmhoquefdbddb12014-05-02 18:35:19 -0500839 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500840 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800841 chkNameLsa->getSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700842 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500843 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500844 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500845 }
Nick G97e34942016-07-11 14:46:27 -0500846 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500847 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500848 NLSR_LOG_DEBUG("Other's Name LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500849 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500850 }
851 }
852 }
853}
854
Nick G97e34942016-07-11 14:46:27 -0500855 // This function determines whether an adj. LSA should be refreshed
856 // or expired. The conditions for getting refreshed are: it is still
857 // in the LSDB, it hasn't been updated by something else already (as
858 // evidenced by its seq. no.), and this is the originating router for
859 // the LSA. Is it let expire in all other cases.
860 // lsaKey is the key of the LSA's publishing router.
861 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500862void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500863Lsdb::expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500864{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500865 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshAdjLsa Called");
866 NLSR_LOG_DEBUG("LSA Key: " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500867 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500868 // If this is a valid LSA
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400869 if (chkAdjLsa != nullptr) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800870 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500871 // And if it hasn't been updated for some other reason
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800872 if (chkAdjLsa->getSeqNo() == seqNo) {
Nick G97e34942016-07-11 14:46:27 -0500873 // If it is our own LSA
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800874 if (chkAdjLsa->getOriginRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500875 NLSR_LOG_DEBUG("Own Adj LSA, so refreshing it");
876 NLSR_LOG_DEBUG("Deleting Adj Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800877 NLSR_LOG_DEBUG(chkAdjLsa);
878 chkAdjLsa->setSeqNo(chkAdjLsa->getSeqNo() + 1);
879 m_sequencingManager.setAdjLsaSeq(chkAdjLsa->getSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500880 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500881 NLSR_LOG_DEBUG("Adding Adj Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800882 NLSR_LOG_DEBUG(chkAdjLsa);
akmhoquefdbddb12014-05-02 18:35:19 -0500883 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500884 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800885 chkAdjLsa->getSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700886 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500887 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500888 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500889 }
Nick G97e34942016-07-11 14:46:27 -0500890 // An LSA from another router is expiring
akmhoque157b0a42014-05-13 00:26:37 -0500891 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500892 NLSR_LOG_DEBUG("Other's Adj LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500893 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500894 }
Nick G97e34942016-07-11 14:46:27 -0500895 // We have changed the contents of the LSDB, so we have to
896 // schedule a routing calculation
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600897 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500898 }
899 }
900}
901
Nick G97e34942016-07-11 14:46:27 -0500902 // This function determines whether an adj. LSA should be refreshed
903 // or expired. The conditions for getting refreshed are: it is still
904 // in the LSDB, it hasn't been updated by something else already (as
905 // evidenced by its seq. no.), and this is the originating router for
906 // the LSA. It is let expire in all other cases.
907 // lsaKey is the key of the LSA's publishing router.
908 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500909void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500910Lsdb::expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500911 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500912{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500913 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshCorLsa Called ");
914 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500915 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500916 // Whether the LSA is in the LSDB or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400917 if (chkCorLsa != nullptr) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800918 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500919 // Whether the LSA has been updated without our knowledge.
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800920 if (chkCorLsa->getSeqNo() == seqNo) {
921 if (chkCorLsa->getOriginRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500922 NLSR_LOG_DEBUG("Own Cor LSA, so refreshing it");
923 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800924 NLSR_LOG_DEBUG(chkCorLsa);
925 chkCorLsa->setSeqNo(chkCorLsa->getSeqNo() + 1);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600926 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800927 m_sequencingManager.setCorLsaSeq(chkCorLsa->getSeqNo());
Nick Gordon5c467f02016-07-13 13:40:10 -0500928 }
929
akmhoquec7a79b22014-05-26 08:06:19 -0500930 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500931 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800932 NLSR_LOG_DEBUG(chkCorLsa);
akmhoquefdbddb12014-05-02 18:35:19 -0500933 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500934 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
935 chkCorLsa->getKey(),
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800936 chkCorLsa->getSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700937 m_lsaRefreshTime));
Nick Gordon5c467f02016-07-13 13:40:10 -0500938 // Only sync coordinate LSAs if link-state routing is disabled
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600939 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500940 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500941 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500942 }
akmhoque53353462014-04-22 08:43:45 -0500943 }
Nick G97e34942016-07-11 14:46:27 -0500944 // We can't refresh other router's LSAs, so we remove it.
akmhoque157b0a42014-05-13 00:26:37 -0500945 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500946 NLSR_LOG_DEBUG("Other's Cor LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500947 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500948 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600949 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
950 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500951 }
952 }
953 }
954}
955
akmhoque53353462014-04-22 08:43:45 -0500956void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700957Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Nick Gordone98480b2017-05-24 11:23:03 -0500958 ndn::time::steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500959{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600960 // increment SENT_LSA_INTEREST
961 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
962
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500963 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500964 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700965 }
Nick G97e34942016-07-11 14:46:27 -0500966 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500967 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500968 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500969 uint64_t seqNo = interestName[-1].toNumber();
970
Nick G97e34942016-07-11 14:46:27 -0500971 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500972 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
973 m_highestSeqNo[lsaName] = seqNo;
974 }
Nick G97e34942016-07-11 14:46:27 -0500975 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500976 else if (seqNo > m_highestSeqNo[lsaName]) {
977 m_highestSeqNo[lsaName] = seqNo;
978 }
Nick G97e34942016-07-11 14:46:27 -0500979 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500980 else if (seqNo < m_highestSeqNo[lsaName]) {
981 return;
982 }
983
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500984 ndn::Interest interest(interestName);
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500985 ndn::util::SegmentFetcher::Options options;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600986 options.interestLifetime = m_confParam.getLsaInterestLifetime();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500987
dmcoomes5bcb39e2017-10-31 15:07:55 -0500988 NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600989 auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest,
990 m_confParam.getValidator(), options);
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500991
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500992 auto it = m_fetchers.insert(fetcher).first;
993
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600994 fetcher->afterSegmentValidated.connect([this] (const ndn::Data& data) {
Ashlesh Gawande15052402018-12-12 20:20:00 -0600995 // Nlsr class subscribes to this to fetch certificates
996 afterSegmentValidatedSignal(data);
997
998 // If we don't do this IMS throws: std::bad_weak_ptr: bad_weak_ptr
999 auto lsaSegment = std::make_shared<const ndn::Data>(data);
1000 m_lsaStorage.insert(*lsaSegment);
1001 const ndn::Name& segmentName = lsaSegment->getName();
1002 // Schedule deletion of the segment
1003 m_scheduler.schedule(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT),
1004 [this, segmentName] { m_lsaStorage.erase(segmentName); });
1005 });
1006
1007 fetcher->onComplete.connect([=] (const ndn::ConstBufferPtr& bufferPtr) {
1008 m_lsaStorage.erase(ndn::Name(lsaName).appendNumber(seqNo - 1));
1009 afterFetchLsa(bufferPtr, interestName);
1010 m_fetchers.erase(it);
1011 });
1012
1013 fetcher->onError.connect([=] (uint32_t errorCode, const std::string& msg) {
1014 onFetchLsaError(errorCode, msg, interestName, timeoutCount, deadline, lsaName, seqNo);
1015 m_fetchers.erase(it);
1016 });
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001017
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001018 // increment a specific SENT_LSA_INTEREST
Nick Gordon727d4832017-10-13 18:04:25 -05001019 Lsa::Type lsaType;
1020 std::istringstream(interestName[-2].toUri()) >> lsaType;
1021 switch (lsaType) {
1022 case Lsa::Type::ADJACENCY:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001023 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001024 break;
1025 case Lsa::Type::COORDINATE:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001026 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001027 break;
1028 case Lsa::Type::NAME:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001029 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001030 break;
1031 default:
dmcoomes5bcb39e2017-10-31 15:07:55 -05001032 NLSR_LOG_ERROR("lsaType " << lsaType << " not recognized; failed Statistics::PacketType conversion");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001033 }
akmhoque31d1d4b2014-05-05 22:08:14 -05001034}
1035
1036void
1037Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
1038{
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001039 ndn::Name interestName(interest.getName());
1040 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
1041
1042 if (interestName[-2].isVersion()) {
1043 // Interest for particular segment
1044 if (m_segmentPublisher.replyFromStore(interestName)) {
1045 NLSR_LOG_TRACE("Reply from SegmentPublisher storage");
1046 return;
1047 }
1048 // Remove version and segment
1049 interestName = interestName.getSubName(0, interestName.size() - 2);
1050 NLSR_LOG_TRACE("Interest w/o segment and version: " << interestName);
1051 }
1052
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001053 // increment RCV_LSA_INTEREST
1054 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
1055
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001056 std::string chkString("LSA");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001057 int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001058
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001059 // Forms the name of the router that the Interest packet came from.
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001060 ndn::Name originRouter = m_confParam.getNetwork();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001061 originRouter.append(interestName.getSubName(lsaPosition + 1,
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001062 interestName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001063
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001064 // if the interest is for this router's LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001065 if (originRouter == m_confParam.getRouterPrefix() && lsaPosition >= 0) {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001066 uint64_t seqNo = interestName[-1].toNumber();
1067 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001068
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001069 std::string lsaType = interestName[-2].toUri();
1070 Lsa::Type interestedLsType;
1071 std::istringstream(lsaType) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001072
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001073 if (interestedLsType == Lsa::Type::NAME) {
1074 processInterestForNameLsa(interest, originRouter.append(lsaType), seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -05001075 }
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001076 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1077 processInterestForAdjacencyLsa(interest, originRouter.append(lsaType), seqNo);
1078 }
1079 else if (interestedLsType == Lsa::Type::COORDINATE) {
1080 processInterestForCoordinateLsa(interest, originRouter.append(lsaType), seqNo);
1081 }
1082 else {
1083 NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
1084 }
1085 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001086 }
1087 else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
Ashlesh Gawande15052402018-12-12 20:20:00 -06001088 std::shared_ptr<const ndn::Data> lsaSegment = m_lsaStorage.find(interest);
1089 if (lsaSegment) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001090 NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001091 m_face.put(*lsaSegment);
akmhoque31d1d4b2014-05-05 22:08:14 -05001092 }
akmhoque157b0a42014-05-13 00:26:37 -05001093 else {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001094 NLSR_LOG_TRACE(interest << " was not found in this lsa storage.");
akmhoque31d1d4b2014-05-05 22:08:14 -05001095 }
1096 }
1097}
1098
Nick G97e34942016-07-11 14:46:27 -05001099 // \brief Finds and sends a requested name LSA.
1100 // \param interest The interest that seeks the name LSA.
1101 // \param lsaKey The LSA that the Interest is seeking.
1102 // \param seqNo A sequence number to ensure that we are sending the
1103 // version that was requested.
akmhoque69c9aa92014-07-23 15:15:05 -05001104void
akmhoque31d1d4b2014-05-05 22:08:14 -05001105Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
1106 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001107 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001108{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001109 // increment RCV_NAME_LSA_INTEREST
1110 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001111 NLSR_LOG_DEBUG("nameLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001112 NameLsa* nameLsa = findNameLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001113 if (nameLsa != nullptr) {
1114 NLSR_LOG_TRACE("Verifying SeqNo for NameLsa is same as requested.");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001115 if (nameLsa->getSeqNo() == seqNo) {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001116 m_segmentPublisher.publish(interest.getName(), interest.getName(),
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001117 nameLsa->wireEncode(),
Saurab Dulal427e0122019-11-28 11:58:02 -06001118 m_lsaRefreshTime, m_confParam.getSigningInfo());
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001119
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001120 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001121 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001122 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001123 NLSR_LOG_TRACE("SeqNo for nameLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001124 }
1125 }
1126 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001127 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001128 }
1129}
1130
Nick G97e34942016-07-11 14:46:27 -05001131 // \brief Finds and sends a requested adj. LSA.
1132 // \param interest The interest that seeks the adj. LSA.
1133 // \param lsaKey The LSA that the Interest is seeking.
1134 // \param seqNo A sequence number to ensure that we are sending the
1135 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001136void
1137Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
1138 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001139 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001140{
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001141 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001142 NLSR_LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001143 }
1144
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001145 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001146 NLSR_LOG_DEBUG("AdjLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001147 AdjLsa* adjLsa = findAdjLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001148 if (adjLsa != nullptr) {
1149 NLSR_LOG_TRACE("Verifying SeqNo for AdjLsa is same as requested.");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001150 if (adjLsa->getSeqNo() == seqNo) {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001151 m_segmentPublisher.publish(interest.getName(), interest.getName(),
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001152 adjLsa->wireEncode(),
Saurab Dulal427e0122019-11-28 11:58:02 -06001153 m_lsaRefreshTime, m_confParam.getSigningInfo());
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001154
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001155 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001156 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001157 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001158 NLSR_LOG_TRACE("SeqNo for AdjLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001159 }
1160 }
1161 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001162 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001163 }
1164}
1165
Nick G97e34942016-07-11 14:46:27 -05001166 // \brief Finds and sends a requested cor. LSA.
1167 // \param interest The interest that seeks the cor. LSA.
1168 // \param lsaKey The LSA that the Interest is seeking.
1169 // \param seqNo A sequence number to ensure that we are sending the
1170 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001171void
1172Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
1173 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001174 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001175{
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001176 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001177 NLSR_LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001178 }
1179
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001180 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001181 NLSR_LOG_DEBUG("CoordinateLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001182 CoordinateLsa* corLsa = findCoordinateLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001183 if (corLsa != nullptr) {
1184 NLSR_LOG_TRACE("Verifying SeqNo for CoordinateLsa is same as requested.");
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001185 if (corLsa->getSeqNo() == seqNo) {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001186 m_segmentPublisher.publish(interest.getName(), interest.getName(),
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001187 corLsa->wireEncode(),
Saurab Dulal427e0122019-11-28 11:58:02 -06001188 m_lsaRefreshTime, m_confParam.getSigningInfo());
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001189
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001190 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001191 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001192 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001193 NLSR_LOG_TRACE("SeqNo for CoordinateLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001194 }
1195 }
1196 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001197 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001198 }
1199}
1200
1201void
akmhoque31d1d4b2014-05-05 22:08:14 -05001202Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001203 uint64_t lsSeqNo, const ndn::Block& block)
akmhoque31d1d4b2014-05-05 22:08:14 -05001204{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001205 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001206 if (isNameLsaNew(lsaKey, lsSeqNo)) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001207 NameLsa nameLsa(block);
1208 installNameLsa(nameLsa);
akmhoque31d1d4b2014-05-05 22:08:14 -05001209 }
1210}
1211
1212void
1213Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001214 uint64_t lsSeqNo, const ndn::Block& block)
akmhoque31d1d4b2014-05-05 22:08:14 -05001215{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001216 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001217 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001218 AdjLsa adjLsa(block);
1219 installAdjLsa(adjLsa);
akmhoque31d1d4b2014-05-05 22:08:14 -05001220 }
1221}
1222
1223void
1224Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001225 uint64_t lsSeqNo, const ndn::Block& block)
akmhoque31d1d4b2014-05-05 22:08:14 -05001226{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001227 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001228 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001229 CoordinateLsa corLsa(block);
1230 installCoordinateLsa(corLsa);
akmhoque31d1d4b2014-05-05 22:08:14 -05001231 }
1232}
1233
akmhoquec7a79b22014-05-26 08:06:19 -05001234ndn::time::system_clock::TimePoint
1235Lsdb::getLsaExpirationTimePoint()
1236{
1237 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1238 expirationTimePoint = expirationTimePoint +
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001239 ndn::time::seconds(m_confParam.getRouterDeadInterval());
akmhoquec7a79b22014-05-26 08:06:19 -05001240 return expirationTimePoint;
1241}
akmhoque31d1d4b2014-05-05 22:08:14 -05001242
1243void
akmhoque2f423352014-06-03 11:49:35 -05001244Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001245{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -05001246 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
1247 return;
1248 }
1249
dmcoomes5bcb39e2017-10-31 15:07:55 -05001250 NLSR_LOG_DEBUG("---------------Adj LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -05001251 for (const auto& adj : m_adjLsdb) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001252 NLSR_LOG_DEBUG(adj);
akmhoque53353462014-04-22 08:43:45 -05001253 }
1254}
1255
1256//-----utility function -----
1257bool
Nick Gordon727d4832017-10-13 18:04:25 -05001258Lsdb::doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType)
akmhoque53353462014-04-22 08:43:45 -05001259{
Nick Gordon727d4832017-10-13 18:04:25 -05001260 switch (lsType) {
1261 case Lsa::Type::ADJACENCY:
akmhoque53353462014-04-22 08:43:45 -05001262 return doesAdjLsaExist(key);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001263
Nick Gordon727d4832017-10-13 18:04:25 -05001264 case Lsa::Type::COORDINATE:
akmhoqueb6450b12014-04-24 00:01:03 -05001265 return doesCoordinateLsaExist(key);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001266
Nick Gordon727d4832017-10-13 18:04:25 -05001267 case Lsa::Type::NAME:
1268 return doesNameLsaExist(key);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001269
Nick Gordon727d4832017-10-13 18:04:25 -05001270 default:
1271 return false;
akmhoque53353462014-04-22 08:43:45 -05001272 }
akmhoque53353462014-04-22 08:43:45 -05001273}
1274
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001275bool
Nick Gordon727d4832017-10-13 18:04:25 -05001276Lsdb::isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001277 const uint64_t& sequenceNumber) {
1278 ndn::Name lsaKey = routerName;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001279 lsaKey.append(boost::lexical_cast<std::string>(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001280
Nick Gordon727d4832017-10-13 18:04:25 -05001281 switch (lsaType) {
1282 case Lsa::Type::ADJACENCY:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001283 return isAdjLsaNew(lsaKey, sequenceNumber);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001284
Nick Gordon727d4832017-10-13 18:04:25 -05001285 case Lsa::Type::COORDINATE:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001286 return isCoordinateLsaNew(lsaKey, sequenceNumber);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001287
Nick Gordon727d4832017-10-13 18:04:25 -05001288 case Lsa::Type::NAME:
1289 return isNameLsaNew(lsaKey, sequenceNumber);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001290
Nick Gordon727d4832017-10-13 18:04:25 -05001291 default:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001292 return false;
1293 }
1294}
1295
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001296} // namespace nlsr