blob: 490c5405eb5b9df05f5e8cc0b055122a3609333f [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/*
Alexander Afanasyev135288c2022-04-23 23:06:56 -04003 * Copyright (c) 2014-2022, 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
Nick Gordone98480b2017-05-24 11:23:03 -050034const ndn::time::steady_clock::TimePoint Lsdb::DEFAULT_LSA_RETRIEVAL_DEADLINE =
35 ndn::time::steady_clock::TimePoint::min();
Vince Lehman18841082014-08-19 17:15:24 -050036
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070037Lsdb::Lsdb(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060038 : m_face(face)
39 , m_scheduler(face.getIoService())
Ashlesh Gawande85998a12017-12-07 22:22:13 -060040 , m_confParam(confParam)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060041 , m_sync(m_face,
Nick Gordon727d4832017-10-13 18:04:25 -050042 [this] (const ndn::Name& routerName, const Lsa::Type& lsaType,
Alexander Afanasyev135288c2022-04-23 23:06:56 -040043 const uint64_t& sequenceNumber, uint64_t incomingFaceId) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -050044 return isLsaNew(routerName, lsaType, sequenceNumber);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060045 }, m_confParam)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060046 , m_lsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()))
Ashlesh Gawande85998a12017-12-07 22:22:13 -060047 , m_adjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval())
Ashlesh Gawande57a87172020-05-09 19:47:06 -070048 , m_thisRouterPrefix(m_confParam.getRouterPrefix())
dulalsaurab82a34c22019-02-04 17:31:21 +000049 , m_sequencingManager(m_confParam.getStateFileDir(), m_confParam.getHyperbolicState())
Nick Gordon9eac4d92017-08-29 17:31:29 -050050 , m_onNewLsaConnection(m_sync.onNewLsa->connect(
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -050051 [this] (const ndn::Name& updateName, uint64_t sequenceNumber,
Alexander Afanasyev135288c2022-04-23 23:06:56 -040052 const ndn::Name& originRouter, uint64_t incomingFaceId) {
Nick Gordon9eac4d92017-08-29 17:31:29 -050053 ndn::Name lsaInterest{updateName};
54 lsaInterest.appendNumber(sequenceNumber);
Alexander Afanasyev135288c2022-04-23 23:06:56 -040055 expressInterest(lsaInterest, 0, incomingFaceId);
Nick Gordon9eac4d92017-08-29 17:31:29 -050056 }))
Ashlesh Gawande85998a12017-12-07 22:22:13 -060057 , m_segmentPublisher(m_face, keyChain)
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070058 , m_isBuildAdjLsaScheduled(false)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060059 , m_adjBuildCount(0)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050060{
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070061 ndn::Name name = m_confParam.getLsaPrefix();
62 NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name);
63
64 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
65 [this] (const auto& name, const auto& interest) { processInterest(name, interest); },
66 [] (const auto& name) { NLSR_LOG_DEBUG("Successfully registered prefix: " << name); },
67 [] (const auto& name, const auto& reason) {
68 NLSR_LOG_ERROR("Failed to register prefix " << name);
69 NDN_THROW(std::runtime_error("Register prefix failed: " + reason));
70 },
71 m_confParam.getSigningInfo(), ndn::nfd::ROUTE_FLAG_CAPTURE);
72
73 buildAndInstallOwnNameLsa();
74 // Install coordinate LSAs if using HR or dry-run HR.
75 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
76 buildAndInstallOwnCoordinateLsa();
77 }
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050078}
79
80void
akmhoque31d1d4b2014-05-05 22:08:14 -050081Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -050082{
Ashlesh Gawande57a87172020-05-09 19:47:06 -070083 NameLsa nameLsa(m_thisRouterPrefix, m_sequencingManager.getNameLsaSeq() + 1,
84 getLsaExpirationTimePoint(), m_confParam.getNamePrefixList());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050085 m_sequencingManager.increaseNameLsaSeq();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050086 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -050087 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050088
Ashlesh Gawande57a87172020-05-09 19:47:06 -070089 installLsa(std::make_shared<NameLsa>(nameLsa));
akmhoque53353462014-04-22 08:43:45 -050090}
91
92void
akmhoque31d1d4b2014-05-05 22:08:14 -050093Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -050094{
Ashlesh Gawande57a87172020-05-09 19:47:06 -070095 CoordinateLsa corLsa(m_thisRouterPrefix, m_sequencingManager.getCorLsaSeq() + 1,
96 getLsaExpirationTimePoint(), m_confParam.getCorR(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -060097 m_confParam.getCorTheta());
Ashlesh Gawande57a87172020-05-09 19:47:06 -070098 m_sequencingManager.increaseCorLsaSeq();
99 m_sequencingManager.writeSeqNoToFile();
Nick Gordon5c467f02016-07-13 13:40:10 -0500100
101 // Sync coordinate LSAs if using HR or HR dry run.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600102 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Nick Gordon727d4832017-10-13 18:04:25 -0500103 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500104 }
105
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700106 installLsa(std::make_shared<CoordinateLsa>(corLsa));
akmhoque53353462014-04-22 08:43:45 -0500107}
108
akmhoque53353462014-04-22 08:43:45 -0500109void
Vince Lehman50df6b72015-03-03 12:06:40 -0600110Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500111{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600112 m_adjBuildCount++;
Vince Lehman50df6b72015-03-03 12:06:40 -0600113
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600114 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Nick Gordon5c467f02016-07-13 13:40:10 -0500115 // Don't build adjacency LSAs in hyperbolic routing
dmcoomes5bcb39e2017-10-31 15:07:55 -0500116 NLSR_LOG_DEBUG("Adjacency LSA not built. Currently in hyperbolic routing state.");
Nick Gordon5c467f02016-07-13 13:40:10 -0500117 return;
118 }
119
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700120 if (m_isBuildAdjLsaScheduled) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500121 NLSR_LOG_DEBUG("Rescheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
122 }
123 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500124 NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700125 m_isBuildAdjLsaScheduled = true;
Vince Lehman50df6b72015-03-03 12:06:40 -0600126 }
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500127 m_scheduledAdjLsaBuild = m_scheduler.schedule(m_adjLsaBuildInterval, [this] { buildAdjLsa(); });
Vince Lehman50df6b72015-03-03 12:06:40 -0600128}
129
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700130void
131Lsdb::writeLog() const
132{
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700133 static const Lsa::Type types[] = {Lsa::Type::COORDINATE, Lsa::Type::NAME, Lsa::Type::ADJACENCY};
134 for (const auto& type : types) {
135 if ((type == Lsa::Type::COORDINATE &&
136 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) ||
137 (type == Lsa::Type::ADJACENCY &&
138 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON)) {
139 continue;
140 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700141
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700142 NLSR_LOG_DEBUG("---------------" << type << " LSDB-------------------");
143 auto lsaRange = m_lsdb.get<byType>().equal_range(type);
144 for (auto lsaIt = lsaRange.first; lsaIt != lsaRange.second; ++lsaIt) {
145 NLSR_LOG_DEBUG((*lsaIt)->toString());
146 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700147 }
148}
149
150void
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700151Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
152{
153 ndn::Name interestName(interest.getName());
154 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
155
156 if (interestName[-2].isVersion()) {
157 // Interest for particular segment
158 if (m_segmentPublisher.replyFromStore(interestName)) {
159 NLSR_LOG_TRACE("Reply from SegmentPublisher storage");
160 return;
161 }
162 // Remove version and segment
163 interestName = interestName.getSubName(0, interestName.size() - 2);
164 NLSR_LOG_TRACE("Interest w/o segment and version: " << interestName);
165 }
166
167 // increment RCV_LSA_INTEREST
168 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
169
170 std::string chkString("LSA");
171 int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString);
172
173 // Forms the name of the router that the Interest packet came from.
174 ndn::Name originRouter = m_confParam.getNetwork();
175 originRouter.append(interestName.getSubName(lsaPosition + 1,
176 interestName.size() - lsaPosition - 3));
177
178 // if the interest is for this router's LSA
179 if (originRouter == m_thisRouterPrefix && lsaPosition >= 0) {
180 uint64_t seqNo = interestName[-1].toNumber();
181 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
182
183 std::string lsaType = interestName[-2].toUri();
184 Lsa::Type interestedLsType;
185 std::istringstream(lsaType) >> interestedLsType;
186 if (interestedLsType == Lsa::Type::BASE) {
187 NLSR_LOG_WARN("Received unrecognized LSA type: " << lsaType);
188 return;
189 }
190
191 incrementInterestRcvdStats(interestedLsType);
192 if (processInterestForLsa(interest, originRouter, interestedLsType, seqNo)) {
193 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
194 }
195 }
196 // else the interest is for other router's LSA, serve signed data from LsaSegmentStorage
197 else if (auto lsaSegment = m_lsaStorage.find(interest)) {
198 NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
199 m_face.put(*lsaSegment);
200 }
201}
202
203bool
204Lsdb::processInterestForLsa(const ndn::Interest& interest, const ndn::Name& originRouter,
205 Lsa::Type lsaType, uint64_t seqNo)
206{
207 NLSR_LOG_DEBUG(interest << " received for " << lsaType);
208 if (auto lsaPtr = findLsa(originRouter, lsaType)) {
209 NLSR_LOG_TRACE("Verifying SeqNo for " << lsaType << " is same as requested.");
210 if (lsaPtr->getSeqNo() == seqNo) {
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700211 m_segmentPublisher.publish(interest.getName(), interest.getName(), lsaPtr->wireEncode(),
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700212 m_lsaRefreshTime, m_confParam.getSigningInfo());
213 incrementDataSentStats(lsaType);
214 return true;
215 }
216 }
217 else {
218 NLSR_LOG_TRACE(interest << " was not found in our LSDB");
219 }
220 return false;
221}
222
223void
Davide Pesaventod90338d2021-01-07 17:50:05 -0500224Lsdb::installLsa(std::shared_ptr<Lsa> lsa)
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700225{
226 auto timeToExpire = m_lsaRefreshTime;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700227 if (lsa->getOriginRouter() != m_thisRouterPrefix) {
228 auto duration = lsa->getExpirationTimePoint() - ndn::time::system_clock::now();
229 if (duration > ndn::time::seconds(0)) {
230 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
231 }
232 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700233
234 auto chkLsa = findLsa(lsa->getOriginRouter(), lsa->getType());
235 if (chkLsa == nullptr) {
236 NLSR_LOG_DEBUG("Adding " << lsa->getType() << " LSA");
237 NLSR_LOG_DEBUG(lsa->toString());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700238
239 m_lsdb.emplace(lsa);
240
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700241 onLsdbModified(lsa, LsdbUpdate::INSTALLED, {}, {});
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700242
243 lsa->setExpiringEventId(scheduleLsaExpiration(lsa, timeToExpire));
244 }
245 // Else this is a known name LSA, so we are updating it.
246 else if (chkLsa->getSeqNo() < lsa->getSeqNo()) {
247 NLSR_LOG_DEBUG("Updating " << lsa->getType() << " LSA:");
248 NLSR_LOG_DEBUG(chkLsa->toString());
249 chkLsa->setSeqNo(lsa->getSeqNo());
250 chkLsa->setExpirationTimePoint(lsa->getExpirationTimePoint());
251
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700252 bool updated;
253 std::list<ndn::Name> namesToAdd, namesToRemove;
254 std::tie(updated, namesToAdd, namesToRemove) = chkLsa->update(lsa);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700255
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700256 if (updated) {
257 onLsdbModified(lsa, LsdbUpdate::UPDATED, namesToAdd, namesToRemove);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700258 }
259
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700260 chkLsa->setExpiringEventId(scheduleLsaExpiration(chkLsa, timeToExpire));
261 NLSR_LOG_DEBUG("Updated " << lsa->getType() << " LSA:");
262 NLSR_LOG_DEBUG(chkLsa->toString());
263 }
264}
265
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700266void
267Lsdb::removeLsa(const LsaContainer::index<Lsdb::byName>::type::iterator& lsaIt)
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700268{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700269 if (lsaIt != m_lsdb.end()) {
270 auto lsaPtr = *lsaIt;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700271 NLSR_LOG_DEBUG("Removing " << lsaPtr->getType() << " LSA:");
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700272 NLSR_LOG_DEBUG(lsaPtr->toString());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700273 m_lsdb.erase(lsaIt);
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700274 onLsdbModified(lsaPtr, LsdbUpdate::REMOVED, {}, {});
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700275 }
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700276}
277
278void
279Lsdb::removeLsa(const ndn::Name& router, Lsa::Type lsaType)
280{
281 removeLsa(m_lsdb.get<byName>().find(std::make_tuple(router, lsaType)));
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700282}
283
Vince Lehman50df6b72015-03-03 12:06:40 -0600284void
285Lsdb::buildAdjLsa()
286{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700287 NLSR_LOG_TRACE("buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600288
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700289 m_isBuildAdjLsaScheduled = false;
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500290
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600291 if (m_confParam.getAdjacencyList().isAdjLsaBuildable(m_confParam.getInterestRetryNumber())) {
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500292
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600293 int adjBuildCount = m_adjBuildCount;
Nick G97e34942016-07-11 14:46:27 -0500294 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500295 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500296 // It only makes sense to do the adjLsa build if we have neighbors
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600297 if (m_confParam.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500298 NLSR_LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500299 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500300 }
Nick G97e34942016-07-11 14:46:27 -0500301 // We have no active neighbors, meaning no one can route through
302 // us. So delete our entry in the LSDB. This prevents this
303 // router from refreshing the LSA, eventually causing other
304 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500305 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500306 NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700307 removeLsa(m_thisRouterPrefix, Lsa::Type::ADJACENCY);
akmhoque53353462014-04-22 08:43:45 -0500308 }
Nick G97e34942016-07-11 14:46:27 -0500309 // In the case that during building the adj LSA, the FIB has to
310 // wait on an Interest response, the number of scheduled adj LSA
311 // builds could change, so we shouldn't just set it to 0.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600312 m_adjBuildCount = m_adjBuildCount - adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500313 }
314 }
Nick G97e34942016-07-11 14:46:27 -0500315 // We are still waiting to know the adjacency status of some
316 // neighbor, so schedule a build for later (when all that has
317 // hopefully finished)
318 else {
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700319 m_isBuildAdjLsaScheduled = true;
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500320 auto schedulingTime = ndn::time::seconds(m_confParam.getInterestRetryNumber() *
321 m_confParam.getInterestResendTime());
322 m_scheduledAdjLsaBuild = m_scheduler.schedule(schedulingTime, [this] { buildAdjLsa(); });
Nick G97e34942016-07-11 14:46:27 -0500323 }
akmhoque53353462014-04-22 08:43:45 -0500324}
325
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700326void
akmhoque31d1d4b2014-05-05 22:08:14 -0500327Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500328{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700329 AdjLsa adjLsa(m_thisRouterPrefix, m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500330 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600331 m_confParam.getAdjacencyList().getNumOfActiveNeighbor(),
332 m_confParam.getAdjacencyList());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700333 m_sequencingManager.increaseAdjLsaSeq();
334 m_sequencingManager.writeSeqNoToFile();
Vince Lehman904c2412014-09-23 19:36:11 -0500335
Nick Gordon5c467f02016-07-13 13:40:10 -0500336 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600337 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Nick Gordon727d4832017-10-13 18:04:25 -0500338 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500339 }
Vince Lehman904c2412014-09-23 19:36:11 -0500340
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700341 installLsa(std::make_shared<AdjLsa>(adjLsa));
akmhoque53353462014-04-22 08:43:45 -0500342}
343
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700344ndn::scheduler::EventId
345Lsdb::scheduleLsaExpiration(std::shared_ptr<Lsa> lsa, ndn::time::seconds expTime)
346{
347 NLSR_LOG_DEBUG("Scheduling expiration in: " << expTime + GRACE_PERIOD << " for " << lsa->getOriginRouter());
348 return m_scheduler.schedule(expTime + GRACE_PERIOD, [this, lsa] { expireOrRefreshLsa(lsa); });
349}
350
akmhoque53353462014-04-22 08:43:45 -0500351void
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700352Lsdb::expireOrRefreshLsa(std::shared_ptr<Lsa> lsa)
akmhoque53353462014-04-22 08:43:45 -0500353{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700354 NLSR_LOG_DEBUG("ExpireOrRefreshLsa called for " << lsa->getType());
355 NLSR_LOG_DEBUG("OriginRouter: " << lsa->getOriginRouter() << " Seq No: " << lsa->getSeqNo());
356
357 auto lsaIt = m_lsdb.get<byName>().find(std::make_tuple(lsa->getOriginRouter(), lsa->getType()));
358
Nick G97e34942016-07-11 14:46:27 -0500359 // If this name LSA exists in the LSDB
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700360 if (lsaIt != m_lsdb.end()) {
361 auto lsaPtr = *lsaIt;
362 NLSR_LOG_DEBUG(lsaPtr->toString());
363 NLSR_LOG_DEBUG("LSA Exists with seq no: " << lsaPtr->getSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500364 // If its seq no is the one we are expecting.
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700365 if (lsaPtr->getSeqNo() == lsa->getSeqNo()) {
366 if (lsaPtr->getOriginRouter() == m_thisRouterPrefix) {
367 NLSR_LOG_DEBUG("Own " << lsaPtr->getType() << " LSA, so refreshing it.");
368 NLSR_LOG_DEBUG("Current LSA:");
369 NLSR_LOG_DEBUG(lsaPtr->toString());
370 lsaPtr->setSeqNo(lsaPtr->getSeqNo() + 1);
371 m_sequencingManager.setLsaSeq(lsaPtr->getSeqNo(), lsaPtr->getType());
372 lsaPtr->setExpirationTimePoint(getLsaExpirationTimePoint());
373 NLSR_LOG_DEBUG("Updated LSA:");
374 NLSR_LOG_DEBUG(lsaPtr->toString());
akmhoquefdbddb12014-05-02 18:35:19 -0500375 // schedule refreshing event again
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700376 lsaPtr->setExpiringEventId(scheduleLsaExpiration(lsaPtr, m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500377 m_sequencingManager.writeSeqNoToFile();
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700378 m_sync.publishRoutingUpdate(lsaPtr->getType(), m_sequencingManager.getLsaSeq(lsaPtr->getType()));
akmhoque53353462014-04-22 08:43:45 -0500379 }
Nick G97e34942016-07-11 14:46:27 -0500380 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500381 else {
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700382 NLSR_LOG_DEBUG("Other's " << lsaPtr->getType() << " LSA, so removing from LSDB");
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700383 removeLsa(lsaIt);
akmhoque53353462014-04-22 08:43:45 -0500384 }
385 }
386 }
387}
388
akmhoque53353462014-04-22 08:43:45 -0500389void
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400390Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount, uint64_t incomingFaceId,
Nick Gordone98480b2017-05-24 11:23:03 -0500391 ndn::time::steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500392{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600393 // increment SENT_LSA_INTEREST
394 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
395
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500396 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500397 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700398 }
Nick G97e34942016-07-11 14:46:27 -0500399 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500400 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500401 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500402 uint64_t seqNo = interestName[-1].toNumber();
403
Nick G97e34942016-07-11 14:46:27 -0500404 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500405 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
406 m_highestSeqNo[lsaName] = seqNo;
407 }
Nick G97e34942016-07-11 14:46:27 -0500408 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500409 else if (seqNo > m_highestSeqNo[lsaName]) {
410 m_highestSeqNo[lsaName] = seqNo;
411 }
Nick G97e34942016-07-11 14:46:27 -0500412 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500413 else if (seqNo < m_highestSeqNo[lsaName]) {
414 return;
415 }
416
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500417 ndn::Interest interest(interestName);
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400418 if (incomingFaceId != 0) {
419 interest.setTag(std::make_shared<ndn::lp::NextHopFaceIdTag>(incomingFaceId));
420 }
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500421 ndn::util::SegmentFetcher::Options options;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600422 options.interestLifetime = m_confParam.getLsaInterestLifetime();
awlane0c87b122022-06-17 15:53:10 -0500423 options.maxTimeout = m_confParam.getLsaInterestLifetime();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500424
dmcoomes5bcb39e2017-10-31 15:07:55 -0500425 NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600426 auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest,
427 m_confParam.getValidator(), options);
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500428
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500429 auto it = m_fetchers.insert(fetcher).first;
430
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600431 fetcher->afterSegmentValidated.connect([this] (const ndn::Data& data) {
Ashlesh Gawande15052402018-12-12 20:20:00 -0600432 // Nlsr class subscribes to this to fetch certificates
433 afterSegmentValidatedSignal(data);
434
435 // If we don't do this IMS throws: std::bad_weak_ptr: bad_weak_ptr
436 auto lsaSegment = std::make_shared<const ndn::Data>(data);
437 m_lsaStorage.insert(*lsaSegment);
438 const ndn::Name& segmentName = lsaSegment->getName();
439 // Schedule deletion of the segment
440 m_scheduler.schedule(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT),
441 [this, segmentName] { m_lsaStorage.erase(segmentName); });
442 });
443
444 fetcher->onComplete.connect([=] (const ndn::ConstBufferPtr& bufferPtr) {
445 m_lsaStorage.erase(ndn::Name(lsaName).appendNumber(seqNo - 1));
446 afterFetchLsa(bufferPtr, interestName);
447 m_fetchers.erase(it);
448 });
449
450 fetcher->onError.connect([=] (uint32_t errorCode, const std::string& msg) {
451 onFetchLsaError(errorCode, msg, interestName, timeoutCount, deadline, lsaName, seqNo);
452 m_fetchers.erase(it);
453 });
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000454
Nick Gordon727d4832017-10-13 18:04:25 -0500455 Lsa::Type lsaType;
456 std::istringstream(interestName[-2].toUri()) >> lsaType;
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700457 incrementInterestSentStats(lsaType);
458}
459
460void
461Lsdb::onFetchLsaError(uint32_t errorCode, const std::string& msg, const ndn::Name& interestName,
462 uint32_t retransmitNo, const ndn::time::steady_clock::TimePoint& deadline,
463 ndn::Name lsaName, uint64_t seqNo)
464{
465 NLSR_LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
466 << ", Message: " << msg);
467
468 if (ndn::time::steady_clock::now() < deadline) {
469 auto it = m_highestSeqNo.find(lsaName);
470 if (it != m_highestSeqNo.end() && it->second == seqNo) {
471 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
472 // immediately since at the least the LSA Interest lifetime has elapsed.
473 // Otherwise, it is necessary to delay the Interest re-expression to prevent
474 // the potential for constant Interest flooding.
475 ndn::time::seconds delay = m_confParam.getLsaInterestLifetime();
476
477 if (errorCode == ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
478 delay = ndn::time::seconds(0);
479 }
480 m_scheduler.schedule(delay, std::bind(&Lsdb::expressInterest, this,
awlane0c87b122022-06-17 15:53:10 -0500481 interestName, retransmitNo + 1, /*Multicast FaceID*/0, deadline));
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700482 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600483 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500484}
485
486void
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700487Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& interestName)
akmhoque31d1d4b2014-05-05 22:08:14 -0500488{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700489 NLSR_LOG_DEBUG("Received data for LSA interest: " << interestName);
490 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600491
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700492 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
493 uint64_t seqNo = interestName[-1].toNumber();
494
495 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
496 m_highestSeqNo[lsaName] = seqNo;
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600497 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700498 else if (seqNo > m_highestSeqNo[lsaName]) {
499 m_highestSeqNo[lsaName] = seqNo;
500 NLSR_LOG_TRACE("SeqNo for LSA(name): " << interestName << " updated");
501 }
502 else if (seqNo < m_highestSeqNo[lsaName]) {
503 return;
504 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600505
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500506 std::string chkString("LSA");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600507 int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500508
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700509 if (lsaPosition >= 0) {
510 // Extracts the prefix of the originating router from the data.
511 ndn::Name originRouter = m_confParam.getNetwork();
512 originRouter.append(interestName.getSubName(lsaPosition + 1,
513 interestName.size() - lsaPosition - 3));
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700514 try {
515 Lsa::Type interestedLsType;
516 std::istringstream(interestName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500517
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700518 if (interestedLsType == Lsa::Type::BASE) {
519 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestName[-2].toUri());
520 return;
521 }
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500522
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700523 ndn::Block block(bufferPtr);
524 if (interestedLsType == Lsa::Type::NAME) {
525 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
526 if (isLsaNew(originRouter, interestedLsType, seqNo)) {
527 installLsa(std::make_shared<NameLsa>(block));
528 }
529 }
530 else if (interestedLsType == Lsa::Type::ADJACENCY) {
531 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
532 if (isLsaNew(originRouter, interestedLsType, seqNo)) {
533 installLsa(std::make_shared<AdjLsa>(block));
534 }
535 }
536 else if (interestedLsType == Lsa::Type::COORDINATE) {
537 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
538 if (isLsaNew(originRouter, interestedLsType, seqNo)) {
539 installLsa(std::make_shared<CoordinateLsa>(block));
540 }
541 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500542 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700543 catch (const std::exception& e) {
544 NLSR_LOG_TRACE("LSA data decoding error :( " << e.what());
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600545 }
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500546 }
547}
548
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700549} // namespace nlsr