blob: 608b575f8753ac4ddc5c74b1b1958be7f7881c0b [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/*
Junxiao Shib8752932024-01-07 15:18:46 +00003 * Copyright (c) 2014-2024, 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/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070020 */
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
Alexander Afanasyev135288c2022-04-23 23:06:56 -040028#include <ndn-cxx/lp/tags.hpp>
29
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
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070034Lsdb::Lsdb(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060035 : m_face(face)
Davide Pesaventofd1e9402023-11-13 15:40:41 -050036 , m_scheduler(face.getIoContext())
Ashlesh Gawande85998a12017-12-07 22:22:13 -060037 , m_confParam(confParam)
Davide Pesavento1954a0c2022-09-30 15:56:04 -040038 , m_sync(m_face, keyChain,
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040039 [this] (const auto& routerName, const Lsa::Type& lsaType,
40 uint64_t sequenceNumber, uint64_t incomingFaceId) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -050041 return isLsaNew(routerName, lsaType, sequenceNumber);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060042 }, m_confParam)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060043 , m_lsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()))
Ashlesh Gawande85998a12017-12-07 22:22:13 -060044 , m_adjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval())
Ashlesh Gawande57a87172020-05-09 19:47:06 -070045 , m_thisRouterPrefix(m_confParam.getRouterPrefix())
dulalsaurab82a34c22019-02-04 17:31:21 +000046 , m_sequencingManager(m_confParam.getStateFileDir(), m_confParam.getHyperbolicState())
Davide Pesavento1954a0c2022-09-30 15:56:04 -040047 , m_onNewLsaConnection(m_sync.onNewLsa.connect(
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -050048 [this] (const ndn::Name& updateName, uint64_t sequenceNumber,
Alexander Afanasyev135288c2022-04-23 23:06:56 -040049 const ndn::Name& originRouter, uint64_t incomingFaceId) {
Nick Gordon9eac4d92017-08-29 17:31:29 -050050 ndn::Name lsaInterest{updateName};
51 lsaInterest.appendNumber(sequenceNumber);
Alexander Afanasyev135288c2022-04-23 23:06:56 -040052 expressInterest(lsaInterest, 0, incomingFaceId);
Nick Gordon9eac4d92017-08-29 17:31:29 -050053 }))
Davide Pesavento65ee9922022-11-16 00:21:43 -050054 , m_segmenter(keyChain, m_confParam.getSigningInfo())
55 , m_segmentFifo(100)
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070056 , m_isBuildAdjLsaScheduled(false)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060057 , m_adjBuildCount(0)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050058{
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070059 ndn::Name name = m_confParam.getLsaPrefix();
60 NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name);
61
62 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
63 [this] (const auto& name, const auto& interest) { processInterest(name, interest); },
64 [] (const auto& name) { NLSR_LOG_DEBUG("Successfully registered prefix: " << name); },
65 [] (const auto& name, const auto& reason) {
66 NLSR_LOG_ERROR("Failed to register prefix " << name);
67 NDN_THROW(std::runtime_error("Register prefix failed: " + reason));
68 },
69 m_confParam.getSigningInfo(), ndn::nfd::ROUTE_FLAG_CAPTURE);
70
71 buildAndInstallOwnNameLsa();
72 // Install coordinate LSAs if using HR or dry-run HR.
73 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
74 buildAndInstallOwnCoordinateLsa();
75 }
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050076}
77
Davide Pesavento65ee9922022-11-16 00:21:43 -050078Lsdb::~Lsdb()
79{
80 for (const auto& fetcher : m_fetchers) {
81 fetcher->stop();
82 }
83}
84
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050085void
akmhoque31d1d4b2014-05-05 22:08:14 -050086Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -050087{
Ashlesh Gawande57a87172020-05-09 19:47:06 -070088 NameLsa nameLsa(m_thisRouterPrefix, m_sequencingManager.getNameLsaSeq() + 1,
89 getLsaExpirationTimePoint(), m_confParam.getNamePrefixList());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050090 m_sequencingManager.increaseNameLsaSeq();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050091 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -050092 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050093
Ashlesh Gawande57a87172020-05-09 19:47:06 -070094 installLsa(std::make_shared<NameLsa>(nameLsa));
akmhoque53353462014-04-22 08:43:45 -050095}
96
97void
akmhoque31d1d4b2014-05-05 22:08:14 -050098Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -050099{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700100 CoordinateLsa corLsa(m_thisRouterPrefix, m_sequencingManager.getCorLsaSeq() + 1,
101 getLsaExpirationTimePoint(), m_confParam.getCorR(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600102 m_confParam.getCorTheta());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700103 m_sequencingManager.increaseCorLsaSeq();
104 m_sequencingManager.writeSeqNoToFile();
Nick Gordon5c467f02016-07-13 13:40:10 -0500105
106 // Sync coordinate LSAs if using HR or HR dry run.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600107 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Nick Gordon727d4832017-10-13 18:04:25 -0500108 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500109 }
110
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700111 installLsa(std::make_shared<CoordinateLsa>(corLsa));
akmhoque53353462014-04-22 08:43:45 -0500112}
113
akmhoque53353462014-04-22 08:43:45 -0500114void
Vince Lehman50df6b72015-03-03 12:06:40 -0600115Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500116{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600117 m_adjBuildCount++;
Vince Lehman50df6b72015-03-03 12:06:40 -0600118
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600119 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Nick Gordon5c467f02016-07-13 13:40:10 -0500120 // Don't build adjacency LSAs in hyperbolic routing
Davide Pesavento65ee9922022-11-16 00:21:43 -0500121 NLSR_LOG_DEBUG("Adjacency LSA not built while in hyperbolic routing state");
Nick Gordon5c467f02016-07-13 13:40:10 -0500122 return;
123 }
124
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700125 if (m_isBuildAdjLsaScheduled) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500126 NLSR_LOG_DEBUG("Rescheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
127 }
128 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500129 NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700130 m_isBuildAdjLsaScheduled = true;
Vince Lehman50df6b72015-03-03 12:06:40 -0600131 }
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500132 m_scheduledAdjLsaBuild = m_scheduler.schedule(m_adjLsaBuildInterval, [this] { buildAdjLsa(); });
Vince Lehman50df6b72015-03-03 12:06:40 -0600133}
134
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700135void
136Lsdb::writeLog() const
137{
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700138 static const Lsa::Type types[] = {Lsa::Type::COORDINATE, Lsa::Type::NAME, Lsa::Type::ADJACENCY};
139 for (const auto& type : types) {
140 if ((type == Lsa::Type::COORDINATE &&
141 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) ||
142 (type == Lsa::Type::ADJACENCY &&
143 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON)) {
144 continue;
145 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700146
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700147 NLSR_LOG_DEBUG("---------------" << type << " LSDB-------------------");
148 auto lsaRange = m_lsdb.get<byType>().equal_range(type);
149 for (auto lsaIt = lsaRange.first; lsaIt != lsaRange.second; ++lsaIt) {
150 NLSR_LOG_DEBUG((*lsaIt)->toString());
151 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700152 }
153}
154
155void
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700156Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
157{
158 ndn::Name interestName(interest.getName());
159 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
160
161 if (interestName[-2].isVersion()) {
162 // Interest for particular segment
Davide Pesavento65ee9922022-11-16 00:21:43 -0500163 auto data = m_segmentFifo.find(interestName);
164 if (data) {
165 NLSR_LOG_TRACE("Replying from FIFO buffer");
166 m_face.put(*data);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700167 return;
168 }
Davide Pesavento65ee9922022-11-16 00:21:43 -0500169
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700170 // Remove version and segment
Davide Pesavento65ee9922022-11-16 00:21:43 -0500171 interestName = interestName.getPrefix(-2);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700172 NLSR_LOG_TRACE("Interest w/o segment and version: " << interestName);
173 }
174
175 // increment RCV_LSA_INTEREST
176 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
177
178 std::string chkString("LSA");
179 int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString);
180
181 // Forms the name of the router that the Interest packet came from.
182 ndn::Name originRouter = m_confParam.getNetwork();
183 originRouter.append(interestName.getSubName(lsaPosition + 1,
184 interestName.size() - lsaPosition - 3));
185
186 // if the interest is for this router's LSA
187 if (originRouter == m_thisRouterPrefix && lsaPosition >= 0) {
188 uint64_t seqNo = interestName[-1].toNumber();
189 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
190
191 std::string lsaType = interestName[-2].toUri();
192 Lsa::Type interestedLsType;
193 std::istringstream(lsaType) >> interestedLsType;
194 if (interestedLsType == Lsa::Type::BASE) {
195 NLSR_LOG_WARN("Received unrecognized LSA type: " << lsaType);
196 return;
197 }
198
199 incrementInterestRcvdStats(interestedLsType);
200 if (processInterestForLsa(interest, originRouter, interestedLsType, seqNo)) {
201 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
202 }
203 }
204 // else the interest is for other router's LSA, serve signed data from LsaSegmentStorage
Davide Pesavento65ee9922022-11-16 00:21:43 -0500205 else if (auto lsaSegment = m_lsaStorage.find(interest); lsaSegment) {
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500206 NLSR_LOG_TRACE("Found data in LSA storage. Sending data for " << interest.getName());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700207 m_face.put(*lsaSegment);
208 }
209}
210
211bool
212Lsdb::processInterestForLsa(const ndn::Interest& interest, const ndn::Name& originRouter,
213 Lsa::Type lsaType, uint64_t seqNo)
214{
215 NLSR_LOG_DEBUG(interest << " received for " << lsaType);
Davide Pesavento65ee9922022-11-16 00:21:43 -0500216
217 if (auto lsaPtr = findLsa(originRouter, lsaType); lsaPtr) {
218 NLSR_LOG_TRACE("Verifying SeqNo for " << lsaType << " is same as requested");
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700219 if (lsaPtr->getSeqNo() == seqNo) {
Davide Pesavento65ee9922022-11-16 00:21:43 -0500220 auto segments = m_segmenter.segment(lsaPtr->wireEncode(),
221 ndn::Name(interest.getName()).appendVersion(),
222 ndn::MAX_NDN_PACKET_SIZE / 2, m_lsaRefreshTime);
223 for (const auto& data : segments) {
224 m_segmentFifo.insert(*data, m_lsaRefreshTime);
225 m_scheduler.schedule(m_lsaRefreshTime,
226 [this, name = data->getName()] { m_segmentFifo.erase(name); });
227 }
228
229 uint64_t segNum = 0;
230 if (interest.getName()[-1].isSegment()) {
231 segNum = interest.getName()[-1].toSegment();
232 }
233 if (segNum < segments.size()) {
234 m_face.put(*segments[segNum]);
235 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700236 incrementDataSentStats(lsaType);
237 return true;
238 }
239 }
240 else {
Davide Pesavento65ee9922022-11-16 00:21:43 -0500241 NLSR_LOG_TRACE(interest << " was not found in our LSDB");
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700242 }
243 return false;
244}
245
246void
Davide Pesaventod90338d2021-01-07 17:50:05 -0500247Lsdb::installLsa(std::shared_ptr<Lsa> lsa)
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700248{
249 auto timeToExpire = m_lsaRefreshTime;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700250 if (lsa->getOriginRouter() != m_thisRouterPrefix) {
251 auto duration = lsa->getExpirationTimePoint() - ndn::time::system_clock::now();
252 if (duration > ndn::time::seconds(0)) {
253 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
254 }
255 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700256
257 auto chkLsa = findLsa(lsa->getOriginRouter(), lsa->getType());
258 if (chkLsa == nullptr) {
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500259 NLSR_LOG_DEBUG("Adding LSA:\n" << lsa->toString());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700260
261 m_lsdb.emplace(lsa);
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700262 onLsdbModified(lsa, LsdbUpdate::INSTALLED, {}, {});
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700263
264 lsa->setExpiringEventId(scheduleLsaExpiration(lsa, timeToExpire));
265 }
266 // Else this is a known name LSA, so we are updating it.
267 else if (chkLsa->getSeqNo() < lsa->getSeqNo()) {
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500268 NLSR_LOG_DEBUG("Updating LSA:\n" << chkLsa->toString());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700269 chkLsa->setSeqNo(lsa->getSeqNo());
270 chkLsa->setExpirationTimePoint(lsa->getExpirationTimePoint());
271
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400272 auto [updated, namesToAdd, namesToRemove] = chkLsa->update(lsa);
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700273 if (updated) {
274 onLsdbModified(lsa, LsdbUpdate::UPDATED, namesToAdd, namesToRemove);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700275 }
276
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700277 chkLsa->setExpiringEventId(scheduleLsaExpiration(chkLsa, timeToExpire));
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500278 NLSR_LOG_DEBUG("Updated LSA:\n" << chkLsa->toString());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700279 }
280}
281
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700282void
283Lsdb::removeLsa(const LsaContainer::index<Lsdb::byName>::type::iterator& lsaIt)
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700284{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700285 if (lsaIt != m_lsdb.end()) {
286 auto lsaPtr = *lsaIt;
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500287 NLSR_LOG_DEBUG("Removing LSA:\n" << lsaPtr->toString());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700288 m_lsdb.erase(lsaIt);
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700289 onLsdbModified(lsaPtr, LsdbUpdate::REMOVED, {}, {});
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700290 }
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700291}
292
293void
294Lsdb::removeLsa(const ndn::Name& router, Lsa::Type lsaType)
295{
296 removeLsa(m_lsdb.get<byName>().find(std::make_tuple(router, lsaType)));
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700297}
298
Vince Lehman50df6b72015-03-03 12:06:40 -0600299void
300Lsdb::buildAdjLsa()
301{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700302 NLSR_LOG_TRACE("buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600303
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700304 m_isBuildAdjLsaScheduled = false;
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500305
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600306 if (m_confParam.getAdjacencyList().isAdjLsaBuildable(m_confParam.getInterestRetryNumber())) {
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500307
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600308 int adjBuildCount = m_adjBuildCount;
Nick G97e34942016-07-11 14:46:27 -0500309 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500310 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500311 // It only makes sense to do the adjLsa build if we have neighbors
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600312 if (m_confParam.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500313 NLSR_LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500314 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500315 }
Nick G97e34942016-07-11 14:46:27 -0500316 // We have no active neighbors, meaning no one can route through
317 // us. So delete our entry in the LSDB. This prevents this
318 // router from refreshing the LSA, eventually causing other
319 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500320 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500321 NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700322 removeLsa(m_thisRouterPrefix, Lsa::Type::ADJACENCY);
akmhoque53353462014-04-22 08:43:45 -0500323 }
Nick G97e34942016-07-11 14:46:27 -0500324 // In the case that during building the adj LSA, the FIB has to
325 // wait on an Interest response, the number of scheduled adj LSA
326 // builds could change, so we shouldn't just set it to 0.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600327 m_adjBuildCount = m_adjBuildCount - adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500328 }
329 }
Nick G97e34942016-07-11 14:46:27 -0500330 // We are still waiting to know the adjacency status of some
331 // neighbor, so schedule a build for later (when all that has
332 // hopefully finished)
333 else {
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700334 m_isBuildAdjLsaScheduled = true;
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500335 auto schedulingTime = ndn::time::seconds(m_confParam.getInterestRetryNumber() *
336 m_confParam.getInterestResendTime());
337 m_scheduledAdjLsaBuild = m_scheduler.schedule(schedulingTime, [this] { buildAdjLsa(); });
Nick G97e34942016-07-11 14:46:27 -0500338 }
akmhoque53353462014-04-22 08:43:45 -0500339}
340
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700341void
akmhoque31d1d4b2014-05-05 22:08:14 -0500342Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500343{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700344 AdjLsa adjLsa(m_thisRouterPrefix, m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500345 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600346 m_confParam.getAdjacencyList());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700347 m_sequencingManager.increaseAdjLsaSeq();
348 m_sequencingManager.writeSeqNoToFile();
Vince Lehman904c2412014-09-23 19:36:11 -0500349
Nick Gordon5c467f02016-07-13 13:40:10 -0500350 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600351 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Nick Gordon727d4832017-10-13 18:04:25 -0500352 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500353 }
Vince Lehman904c2412014-09-23 19:36:11 -0500354
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700355 installLsa(std::make_shared<AdjLsa>(adjLsa));
akmhoque53353462014-04-22 08:43:45 -0500356}
357
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700358ndn::scheduler::EventId
359Lsdb::scheduleLsaExpiration(std::shared_ptr<Lsa> lsa, ndn::time::seconds expTime)
360{
361 NLSR_LOG_DEBUG("Scheduling expiration in: " << expTime + GRACE_PERIOD << " for " << lsa->getOriginRouter());
362 return m_scheduler.schedule(expTime + GRACE_PERIOD, [this, lsa] { expireOrRefreshLsa(lsa); });
363}
364
akmhoque53353462014-04-22 08:43:45 -0500365void
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700366Lsdb::expireOrRefreshLsa(std::shared_ptr<Lsa> lsa)
akmhoque53353462014-04-22 08:43:45 -0500367{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700368 NLSR_LOG_DEBUG("ExpireOrRefreshLsa called for " << lsa->getType());
369 NLSR_LOG_DEBUG("OriginRouter: " << lsa->getOriginRouter() << " Seq No: " << lsa->getSeqNo());
370
371 auto lsaIt = m_lsdb.get<byName>().find(std::make_tuple(lsa->getOriginRouter(), lsa->getType()));
372
Nick G97e34942016-07-11 14:46:27 -0500373 // If this name LSA exists in the LSDB
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700374 if (lsaIt != m_lsdb.end()) {
375 auto lsaPtr = *lsaIt;
376 NLSR_LOG_DEBUG(lsaPtr->toString());
377 NLSR_LOG_DEBUG("LSA Exists with seq no: " << lsaPtr->getSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500378 // If its seq no is the one we are expecting.
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700379 if (lsaPtr->getSeqNo() == lsa->getSeqNo()) {
380 if (lsaPtr->getOriginRouter() == m_thisRouterPrefix) {
Davide Pesavento65ee9922022-11-16 00:21:43 -0500381 NLSR_LOG_DEBUG("Own " << lsaPtr->getType() << " LSA, so refreshing it");
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500382 NLSR_LOG_DEBUG("Current LSA:\n" << lsaPtr->toString());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700383 lsaPtr->setSeqNo(lsaPtr->getSeqNo() + 1);
384 m_sequencingManager.setLsaSeq(lsaPtr->getSeqNo(), lsaPtr->getType());
385 lsaPtr->setExpirationTimePoint(getLsaExpirationTimePoint());
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500386 NLSR_LOG_DEBUG("Updated LSA:\n" << lsaPtr->toString());
akmhoquefdbddb12014-05-02 18:35:19 -0500387 // schedule refreshing event again
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700388 lsaPtr->setExpiringEventId(scheduleLsaExpiration(lsaPtr, m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500389 m_sequencingManager.writeSeqNoToFile();
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700390 m_sync.publishRoutingUpdate(lsaPtr->getType(), m_sequencingManager.getLsaSeq(lsaPtr->getType()));
akmhoque53353462014-04-22 08:43:45 -0500391 }
Nick G97e34942016-07-11 14:46:27 -0500392 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500393 else {
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700394 NLSR_LOG_DEBUG("Other's " << lsaPtr->getType() << " LSA, so removing from LSDB");
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700395 removeLsa(lsaIt);
akmhoque53353462014-04-22 08:43:45 -0500396 }
397 }
398 }
399}
400
akmhoque53353462014-04-22 08:43:45 -0500401void
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400402Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount, uint64_t incomingFaceId,
Davide Pesavento658fd852023-05-10 22:15:03 -0400403 ndn::time::steady_clock::time_point deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500404{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600405 // increment SENT_LSA_INTEREST
406 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
407
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500408 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500409 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700410 }
Nick G97e34942016-07-11 14:46:27 -0500411 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500412 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500413 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500414 uint64_t seqNo = interestName[-1].toNumber();
415
Nick G97e34942016-07-11 14:46:27 -0500416 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500417 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
418 m_highestSeqNo[lsaName] = seqNo;
419 }
Nick G97e34942016-07-11 14:46:27 -0500420 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500421 else if (seqNo > m_highestSeqNo[lsaName]) {
422 m_highestSeqNo[lsaName] = seqNo;
423 }
Nick G97e34942016-07-11 14:46:27 -0500424 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500425 else if (seqNo < m_highestSeqNo[lsaName]) {
426 return;
427 }
428
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500429 ndn::Interest interest(interestName);
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400430 if (incomingFaceId != 0) {
431 interest.setTag(std::make_shared<ndn::lp::NextHopFaceIdTag>(incomingFaceId));
432 }
Junxiao Shi43f37a02023-08-09 00:09:00 +0000433 ndn::SegmentFetcher::Options options;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600434 options.interestLifetime = m_confParam.getLsaInterestLifetime();
awlane0c87b122022-06-17 15:53:10 -0500435 options.maxTimeout = m_confParam.getLsaInterestLifetime();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500436
dmcoomes5bcb39e2017-10-31 15:07:55 -0500437 NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
Junxiao Shi43f37a02023-08-09 00:09:00 +0000438 auto fetcher = ndn::SegmentFetcher::start(m_face, interest, m_confParam.getValidator(), options);
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500439
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500440 auto it = m_fetchers.insert(fetcher).first;
441
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600442 fetcher->afterSegmentValidated.connect([this] (const ndn::Data& data) {
Ashlesh Gawande15052402018-12-12 20:20:00 -0600443 // Nlsr class subscribes to this to fetch certificates
444 afterSegmentValidatedSignal(data);
445
446 // If we don't do this IMS throws: std::bad_weak_ptr: bad_weak_ptr
447 auto lsaSegment = std::make_shared<const ndn::Data>(data);
448 m_lsaStorage.insert(*lsaSegment);
Ashlesh Gawande15052402018-12-12 20:20:00 -0600449 // Schedule deletion of the segment
450 m_scheduler.schedule(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT),
Davide Pesavento65ee9922022-11-16 00:21:43 -0500451 [this, name = lsaSegment->getName()] { m_lsaStorage.erase(name); });
Ashlesh Gawande15052402018-12-12 20:20:00 -0600452 });
453
454 fetcher->onComplete.connect([=] (const ndn::ConstBufferPtr& bufferPtr) {
455 m_lsaStorage.erase(ndn::Name(lsaName).appendNumber(seqNo - 1));
456 afterFetchLsa(bufferPtr, interestName);
457 m_fetchers.erase(it);
458 });
459
460 fetcher->onError.connect([=] (uint32_t errorCode, const std::string& msg) {
461 onFetchLsaError(errorCode, msg, interestName, timeoutCount, deadline, lsaName, seqNo);
462 m_fetchers.erase(it);
463 });
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000464
Nick Gordon727d4832017-10-13 18:04:25 -0500465 Lsa::Type lsaType;
466 std::istringstream(interestName[-2].toUri()) >> lsaType;
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700467 incrementInterestSentStats(lsaType);
468}
469
470void
471Lsdb::onFetchLsaError(uint32_t errorCode, const std::string& msg, const ndn::Name& interestName,
Davide Pesavento658fd852023-05-10 22:15:03 -0400472 uint32_t retransmitNo, const ndn::time::steady_clock::time_point& deadline,
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700473 ndn::Name lsaName, uint64_t seqNo)
474{
475 NLSR_LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
476 << ", Message: " << msg);
477
478 if (ndn::time::steady_clock::now() < deadline) {
479 auto it = m_highestSeqNo.find(lsaName);
480 if (it != m_highestSeqNo.end() && it->second == seqNo) {
481 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
482 // immediately since at the least the LSA Interest lifetime has elapsed.
483 // Otherwise, it is necessary to delay the Interest re-expression to prevent
484 // the potential for constant Interest flooding.
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500485 auto delay = m_confParam.getLsaInterestLifetime();
Junxiao Shi43f37a02023-08-09 00:09:00 +0000486 if (errorCode == ndn::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500487 delay = 0_s;
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700488 }
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500489 m_scheduler.schedule(delay, std::bind(&Lsdb::expressInterest, this, interestName,
490 retransmitNo + 1, /*Multicast FaceID*/0, deadline));
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700491 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600492 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500493}
494
495void
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700496Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& interestName)
akmhoque31d1d4b2014-05-05 22:08:14 -0500497{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700498 NLSR_LOG_DEBUG("Received data for LSA interest: " << interestName);
499 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600500
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700501 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
502 uint64_t seqNo = interestName[-1].toNumber();
503
504 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
505 m_highestSeqNo[lsaName] = seqNo;
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600506 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700507 else if (seqNo > m_highestSeqNo[lsaName]) {
508 m_highestSeqNo[lsaName] = seqNo;
Davide Pesavento65ee9922022-11-16 00:21:43 -0500509 NLSR_LOG_TRACE("SeqNo for LSA(name): " << interestName << " updated");
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700510 }
511 else if (seqNo < m_highestSeqNo[lsaName]) {
512 return;
513 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600514
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500515 std::string chkString("LSA");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600516 int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500517
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700518 if (lsaPosition >= 0) {
519 // Extracts the prefix of the originating router from the data.
520 ndn::Name originRouter = m_confParam.getNetwork();
521 originRouter.append(interestName.getSubName(lsaPosition + 1,
522 interestName.size() - lsaPosition - 3));
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700523 try {
524 Lsa::Type interestedLsType;
525 std::istringstream(interestName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500526
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700527 if (interestedLsType == Lsa::Type::BASE) {
528 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestName[-2].toUri());
529 return;
530 }
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500531
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700532 ndn::Block block(bufferPtr);
533 if (interestedLsType == Lsa::Type::NAME) {
534 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
535 if (isLsaNew(originRouter, interestedLsType, seqNo)) {
536 installLsa(std::make_shared<NameLsa>(block));
537 }
538 }
539 else if (interestedLsType == Lsa::Type::ADJACENCY) {
540 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
541 if (isLsaNew(originRouter, interestedLsType, seqNo)) {
542 installLsa(std::make_shared<AdjLsa>(block));
543 }
544 }
545 else if (interestedLsType == Lsa::Type::COORDINATE) {
546 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
547 if (isLsaNew(originRouter, interestedLsType, seqNo)) {
548 installLsa(std::make_shared<CoordinateLsa>(block));
549 }
550 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500551 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700552 catch (const std::exception& e) {
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500553 NLSR_LOG_TRACE("LSA data decoding error: " << e.what());
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600554 }
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500555 }
556}
557
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700558} // namespace nlsr