akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 2 | /* |
Junxiao Shi | b875293 | 2024-01-07 15:18:46 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, The University of Memphis, |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 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 Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 20 | */ |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 21 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 22 | #include "lsdb.hpp" |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 23 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 24 | #include "logger.hpp" |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 25 | #include "nlsr.hpp" |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 26 | #include "utility/name-helper.hpp" |
| 27 | |
Alexander Afanasyev | 135288c | 2022-04-23 23:06:56 -0400 | [diff] [blame] | 28 | #include <ndn-cxx/lp/tags.hpp> |
| 29 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 30 | namespace nlsr { |
| 31 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 32 | INIT_LOGGER(Lsdb); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 33 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 34 | Lsdb::Lsdb(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 35 | : m_face(face) |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame] | 36 | , m_scheduler(face.getIoContext()) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 37 | , m_confParam(confParam) |
Davide Pesavento | 1954a0c | 2022-09-30 15:56:04 -0400 | [diff] [blame] | 38 | , m_sync(m_face, keyChain, |
Junxiao Shi | f467467 | 2024-01-06 02:27:36 +0000 | [diff] [blame^] | 39 | [this] (const auto& routerName, Lsa::Type lsaType, uint64_t seqNo, uint64_t) { |
| 40 | return isLsaNew(routerName, lsaType, seqNo); |
| 41 | }, |
| 42 | SyncLogicOptions{ |
| 43 | confParam.getSyncProtocol(), |
| 44 | confParam.getSyncPrefix(), |
| 45 | confParam.getSyncUserPrefix(), |
| 46 | confParam.getSyncInterestLifetime(), |
| 47 | confParam.getRouterPrefix(), |
| 48 | confParam.getHyperbolicState() |
| 49 | }) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 50 | , m_lsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime())) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 51 | , m_adjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval()) |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 52 | , m_thisRouterPrefix(m_confParam.getRouterPrefix()) |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 53 | , m_sequencingManager(m_confParam.getStateFileDir(), m_confParam.getHyperbolicState()) |
Davide Pesavento | 1954a0c | 2022-09-30 15:56:04 -0400 | [diff] [blame] | 54 | , m_onNewLsaConnection(m_sync.onNewLsa.connect( |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 55 | [this] (const ndn::Name& updateName, uint64_t sequenceNumber, |
Alexander Afanasyev | 135288c | 2022-04-23 23:06:56 -0400 | [diff] [blame] | 56 | const ndn::Name& originRouter, uint64_t incomingFaceId) { |
Nick Gordon | 9eac4d9 | 2017-08-29 17:31:29 -0500 | [diff] [blame] | 57 | ndn::Name lsaInterest{updateName}; |
| 58 | lsaInterest.appendNumber(sequenceNumber); |
Alexander Afanasyev | 135288c | 2022-04-23 23:06:56 -0400 | [diff] [blame] | 59 | expressInterest(lsaInterest, 0, incomingFaceId); |
Nick Gordon | 9eac4d9 | 2017-08-29 17:31:29 -0500 | [diff] [blame] | 60 | })) |
Davide Pesavento | 65ee992 | 2022-11-16 00:21:43 -0500 | [diff] [blame] | 61 | , m_segmenter(keyChain, m_confParam.getSigningInfo()) |
| 62 | , m_segmentFifo(100) |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 63 | , m_isBuildAdjLsaScheduled(false) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 64 | , m_adjBuildCount(0) |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 65 | { |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 66 | ndn::Name name = m_confParam.getLsaPrefix(); |
| 67 | NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name); |
| 68 | |
| 69 | m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false), |
| 70 | [this] (const auto& name, const auto& interest) { processInterest(name, interest); }, |
| 71 | [] (const auto& name) { NLSR_LOG_DEBUG("Successfully registered prefix: " << name); }, |
| 72 | [] (const auto& name, const auto& reason) { |
| 73 | NLSR_LOG_ERROR("Failed to register prefix " << name); |
| 74 | NDN_THROW(std::runtime_error("Register prefix failed: " + reason)); |
| 75 | }, |
| 76 | m_confParam.getSigningInfo(), ndn::nfd::ROUTE_FLAG_CAPTURE); |
| 77 | |
| 78 | buildAndInstallOwnNameLsa(); |
| 79 | // Install coordinate LSAs if using HR or dry-run HR. |
| 80 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) { |
| 81 | buildAndInstallOwnCoordinateLsa(); |
| 82 | } |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 83 | } |
| 84 | |
Davide Pesavento | 65ee992 | 2022-11-16 00:21:43 -0500 | [diff] [blame] | 85 | Lsdb::~Lsdb() |
| 86 | { |
| 87 | for (const auto& fetcher : m_fetchers) { |
| 88 | fetcher->stop(); |
| 89 | } |
| 90 | } |
| 91 | |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 92 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 93 | Lsdb::buildAndInstallOwnNameLsa() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 94 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 95 | NameLsa nameLsa(m_thisRouterPrefix, m_sequencingManager.getNameLsaSeq() + 1, |
| 96 | getLsaExpirationTimePoint(), m_confParam.getNamePrefixList()); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 97 | m_sequencingManager.increaseNameLsaSeq(); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 98 | m_sequencingManager.writeSeqNoToFile(); |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 99 | m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq()); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 100 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 101 | installLsa(std::make_shared<NameLsa>(nameLsa)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 105 | Lsdb::buildAndInstallOwnCoordinateLsa() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 106 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 107 | CoordinateLsa corLsa(m_thisRouterPrefix, m_sequencingManager.getCorLsaSeq() + 1, |
| 108 | getLsaExpirationTimePoint(), m_confParam.getCorR(), |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 109 | m_confParam.getCorTheta()); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 110 | m_sequencingManager.increaseCorLsaSeq(); |
| 111 | m_sequencingManager.writeSeqNoToFile(); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 112 | |
| 113 | // Sync coordinate LSAs if using HR or HR dry run. |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 114 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) { |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 115 | m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq()); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 118 | installLsa(std::make_shared<CoordinateLsa>(corLsa)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 119 | } |
| 120 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 121 | void |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 122 | Lsdb::scheduleAdjLsaBuild() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 123 | { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 124 | m_adjBuildCount++; |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 125 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 126 | if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 127 | // Don't build adjacency LSAs in hyperbolic routing |
Davide Pesavento | 65ee992 | 2022-11-16 00:21:43 -0500 | [diff] [blame] | 128 | NLSR_LOG_DEBUG("Adjacency LSA not built while in hyperbolic routing state"); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 132 | if (m_isBuildAdjLsaScheduled) { |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 133 | NLSR_LOG_DEBUG("Rescheduling Adjacency LSA build in " << m_adjLsaBuildInterval); |
| 134 | } |
| 135 | else { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 136 | NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 137 | m_isBuildAdjLsaScheduled = true; |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 138 | } |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 139 | m_scheduledAdjLsaBuild = m_scheduler.schedule(m_adjLsaBuildInterval, [this] { buildAdjLsa(); }); |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 140 | } |
| 141 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 142 | void |
| 143 | Lsdb::writeLog() const |
| 144 | { |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 145 | static const Lsa::Type types[] = {Lsa::Type::COORDINATE, Lsa::Type::NAME, Lsa::Type::ADJACENCY}; |
| 146 | for (const auto& type : types) { |
| 147 | if ((type == Lsa::Type::COORDINATE && |
| 148 | m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) || |
| 149 | (type == Lsa::Type::ADJACENCY && |
| 150 | m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON)) { |
| 151 | continue; |
| 152 | } |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 153 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 154 | NLSR_LOG_DEBUG("---------------" << type << " LSDB-------------------"); |
| 155 | auto lsaRange = m_lsdb.get<byType>().equal_range(type); |
| 156 | for (auto lsaIt = lsaRange.first; lsaIt != lsaRange.second; ++lsaIt) { |
| 157 | NLSR_LOG_DEBUG((*lsaIt)->toString()); |
| 158 | } |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | void |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 163 | Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest) |
| 164 | { |
| 165 | ndn::Name interestName(interest.getName()); |
| 166 | NLSR_LOG_DEBUG("Interest received for LSA: " << interestName); |
| 167 | |
| 168 | if (interestName[-2].isVersion()) { |
| 169 | // Interest for particular segment |
Davide Pesavento | 65ee992 | 2022-11-16 00:21:43 -0500 | [diff] [blame] | 170 | auto data = m_segmentFifo.find(interestName); |
| 171 | if (data) { |
| 172 | NLSR_LOG_TRACE("Replying from FIFO buffer"); |
| 173 | m_face.put(*data); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 174 | return; |
| 175 | } |
Davide Pesavento | 65ee992 | 2022-11-16 00:21:43 -0500 | [diff] [blame] | 176 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 177 | // Remove version and segment |
Davide Pesavento | 65ee992 | 2022-11-16 00:21:43 -0500 | [diff] [blame] | 178 | interestName = interestName.getPrefix(-2); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 179 | NLSR_LOG_TRACE("Interest w/o segment and version: " << interestName); |
| 180 | } |
| 181 | |
| 182 | // increment RCV_LSA_INTEREST |
| 183 | lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST); |
| 184 | |
| 185 | std::string chkString("LSA"); |
| 186 | int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString); |
| 187 | |
| 188 | // Forms the name of the router that the Interest packet came from. |
| 189 | ndn::Name originRouter = m_confParam.getNetwork(); |
| 190 | originRouter.append(interestName.getSubName(lsaPosition + 1, |
| 191 | interestName.size() - lsaPosition - 3)); |
| 192 | |
| 193 | // if the interest is for this router's LSA |
| 194 | if (originRouter == m_thisRouterPrefix && lsaPosition >= 0) { |
| 195 | uint64_t seqNo = interestName[-1].toNumber(); |
| 196 | NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo); |
| 197 | |
| 198 | std::string lsaType = interestName[-2].toUri(); |
| 199 | Lsa::Type interestedLsType; |
| 200 | std::istringstream(lsaType) >> interestedLsType; |
| 201 | if (interestedLsType == Lsa::Type::BASE) { |
| 202 | NLSR_LOG_WARN("Received unrecognized LSA type: " << lsaType); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | incrementInterestRcvdStats(interestedLsType); |
| 207 | if (processInterestForLsa(interest, originRouter, interestedLsType, seqNo)) { |
| 208 | lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA); |
| 209 | } |
| 210 | } |
| 211 | // else the interest is for other router's LSA, serve signed data from LsaSegmentStorage |
Davide Pesavento | 65ee992 | 2022-11-16 00:21:43 -0500 | [diff] [blame] | 212 | else if (auto lsaSegment = m_lsaStorage.find(interest); lsaSegment) { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame] | 213 | NLSR_LOG_TRACE("Found data in LSA storage. Sending data for " << interest.getName()); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 214 | m_face.put(*lsaSegment); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | bool |
| 219 | Lsdb::processInterestForLsa(const ndn::Interest& interest, const ndn::Name& originRouter, |
| 220 | Lsa::Type lsaType, uint64_t seqNo) |
| 221 | { |
| 222 | NLSR_LOG_DEBUG(interest << " received for " << lsaType); |
Davide Pesavento | 65ee992 | 2022-11-16 00:21:43 -0500 | [diff] [blame] | 223 | |
| 224 | if (auto lsaPtr = findLsa(originRouter, lsaType); lsaPtr) { |
| 225 | NLSR_LOG_TRACE("Verifying SeqNo for " << lsaType << " is same as requested"); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 226 | if (lsaPtr->getSeqNo() == seqNo) { |
Davide Pesavento | 65ee992 | 2022-11-16 00:21:43 -0500 | [diff] [blame] | 227 | auto segments = m_segmenter.segment(lsaPtr->wireEncode(), |
| 228 | ndn::Name(interest.getName()).appendVersion(), |
| 229 | ndn::MAX_NDN_PACKET_SIZE / 2, m_lsaRefreshTime); |
| 230 | for (const auto& data : segments) { |
| 231 | m_segmentFifo.insert(*data, m_lsaRefreshTime); |
| 232 | m_scheduler.schedule(m_lsaRefreshTime, |
| 233 | [this, name = data->getName()] { m_segmentFifo.erase(name); }); |
| 234 | } |
| 235 | |
| 236 | uint64_t segNum = 0; |
| 237 | if (interest.getName()[-1].isSegment()) { |
| 238 | segNum = interest.getName()[-1].toSegment(); |
| 239 | } |
| 240 | if (segNum < segments.size()) { |
| 241 | m_face.put(*segments[segNum]); |
| 242 | } |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 243 | incrementDataSentStats(lsaType); |
| 244 | return true; |
| 245 | } |
| 246 | } |
| 247 | else { |
Davide Pesavento | 65ee992 | 2022-11-16 00:21:43 -0500 | [diff] [blame] | 248 | NLSR_LOG_TRACE(interest << " was not found in our LSDB"); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 249 | } |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | void |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 254 | Lsdb::installLsa(std::shared_ptr<Lsa> lsa) |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 255 | { |
| 256 | auto timeToExpire = m_lsaRefreshTime; |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 257 | if (lsa->getOriginRouter() != m_thisRouterPrefix) { |
| 258 | auto duration = lsa->getExpirationTimePoint() - ndn::time::system_clock::now(); |
| 259 | if (duration > ndn::time::seconds(0)) { |
| 260 | timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration); |
| 261 | } |
| 262 | } |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 263 | |
| 264 | auto chkLsa = findLsa(lsa->getOriginRouter(), lsa->getType()); |
| 265 | if (chkLsa == nullptr) { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame] | 266 | NLSR_LOG_DEBUG("Adding LSA:\n" << lsa->toString()); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 267 | |
| 268 | m_lsdb.emplace(lsa); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 269 | onLsdbModified(lsa, LsdbUpdate::INSTALLED, {}, {}); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 270 | |
| 271 | lsa->setExpiringEventId(scheduleLsaExpiration(lsa, timeToExpire)); |
| 272 | } |
| 273 | // Else this is a known name LSA, so we are updating it. |
| 274 | else if (chkLsa->getSeqNo() < lsa->getSeqNo()) { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame] | 275 | NLSR_LOG_DEBUG("Updating LSA:\n" << chkLsa->toString()); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 276 | chkLsa->setSeqNo(lsa->getSeqNo()); |
| 277 | chkLsa->setExpirationTimePoint(lsa->getExpirationTimePoint()); |
| 278 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 279 | auto [updated, namesToAdd, namesToRemove] = chkLsa->update(lsa); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 280 | if (updated) { |
| 281 | onLsdbModified(lsa, LsdbUpdate::UPDATED, namesToAdd, namesToRemove); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 284 | chkLsa->setExpiringEventId(scheduleLsaExpiration(chkLsa, timeToExpire)); |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame] | 285 | NLSR_LOG_DEBUG("Updated LSA:\n" << chkLsa->toString()); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 289 | void |
| 290 | Lsdb::removeLsa(const LsaContainer::index<Lsdb::byName>::type::iterator& lsaIt) |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 291 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 292 | if (lsaIt != m_lsdb.end()) { |
| 293 | auto lsaPtr = *lsaIt; |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame] | 294 | NLSR_LOG_DEBUG("Removing LSA:\n" << lsaPtr->toString()); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 295 | m_lsdb.erase(lsaIt); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 296 | onLsdbModified(lsaPtr, LsdbUpdate::REMOVED, {}, {}); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 297 | } |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void |
| 301 | Lsdb::removeLsa(const ndn::Name& router, Lsa::Type lsaType) |
| 302 | { |
| 303 | removeLsa(m_lsdb.get<byName>().find(std::make_tuple(router, lsaType))); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 306 | void |
| 307 | Lsdb::buildAdjLsa() |
| 308 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 309 | NLSR_LOG_TRACE("buildAdjLsa called"); |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 310 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 311 | m_isBuildAdjLsaScheduled = false; |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 312 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 313 | if (m_confParam.getAdjacencyList().isAdjLsaBuildable(m_confParam.getInterestRetryNumber())) { |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 314 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 315 | int adjBuildCount = m_adjBuildCount; |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 316 | // Only do the adjLsa build if there's one scheduled |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 317 | if (adjBuildCount > 0) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 318 | // It only makes sense to do the adjLsa build if we have neighbors |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 319 | if (m_confParam.getAdjacencyList().getNumOfActiveNeighbor() > 0) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 320 | NLSR_LOG_DEBUG("Building and installing own Adj LSA"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 321 | buildAndInstallOwnAdjLsa(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 322 | } |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 323 | // We have no active neighbors, meaning no one can route through |
| 324 | // us. So delete our entry in the LSDB. This prevents this |
| 325 | // router from refreshing the LSA, eventually causing other |
| 326 | // routers to delete it, too. |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 327 | else { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 328 | NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors"); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 329 | removeLsa(m_thisRouterPrefix, Lsa::Type::ADJACENCY); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 330 | } |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 331 | // In the case that during building the adj LSA, the FIB has to |
| 332 | // wait on an Interest response, the number of scheduled adj LSA |
| 333 | // builds could change, so we shouldn't just set it to 0. |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 334 | m_adjBuildCount = m_adjBuildCount - adjBuildCount; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 335 | } |
| 336 | } |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 337 | // We are still waiting to know the adjacency status of some |
| 338 | // neighbor, so schedule a build for later (when all that has |
| 339 | // hopefully finished) |
| 340 | else { |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 341 | m_isBuildAdjLsaScheduled = true; |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 342 | auto schedulingTime = ndn::time::seconds(m_confParam.getInterestRetryNumber() * |
| 343 | m_confParam.getInterestResendTime()); |
| 344 | m_scheduledAdjLsaBuild = m_scheduler.schedule(schedulingTime, [this] { buildAdjLsa(); }); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 345 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 346 | } |
| 347 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 348 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 349 | Lsdb::buildAndInstallOwnAdjLsa() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 350 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 351 | AdjLsa adjLsa(m_thisRouterPrefix, m_sequencingManager.getAdjLsaSeq() + 1, |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 352 | getLsaExpirationTimePoint(), |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 353 | m_confParam.getAdjacencyList()); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 354 | m_sequencingManager.increaseAdjLsaSeq(); |
| 355 | m_sequencingManager.writeSeqNoToFile(); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 356 | |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 357 | //Sync adjacency LSAs if link-state or dry-run HR is enabled. |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 358 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_ON) { |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 359 | m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq()); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 360 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 361 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 362 | installLsa(std::make_shared<AdjLsa>(adjLsa)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 363 | } |
| 364 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 365 | ndn::scheduler::EventId |
| 366 | Lsdb::scheduleLsaExpiration(std::shared_ptr<Lsa> lsa, ndn::time::seconds expTime) |
| 367 | { |
| 368 | NLSR_LOG_DEBUG("Scheduling expiration in: " << expTime + GRACE_PERIOD << " for " << lsa->getOriginRouter()); |
| 369 | return m_scheduler.schedule(expTime + GRACE_PERIOD, [this, lsa] { expireOrRefreshLsa(lsa); }); |
| 370 | } |
| 371 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 372 | void |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 373 | Lsdb::expireOrRefreshLsa(std::shared_ptr<Lsa> lsa) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 374 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 375 | NLSR_LOG_DEBUG("ExpireOrRefreshLsa called for " << lsa->getType()); |
| 376 | NLSR_LOG_DEBUG("OriginRouter: " << lsa->getOriginRouter() << " Seq No: " << lsa->getSeqNo()); |
| 377 | |
| 378 | auto lsaIt = m_lsdb.get<byName>().find(std::make_tuple(lsa->getOriginRouter(), lsa->getType())); |
| 379 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 380 | // If this name LSA exists in the LSDB |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 381 | if (lsaIt != m_lsdb.end()) { |
| 382 | auto lsaPtr = *lsaIt; |
| 383 | NLSR_LOG_DEBUG(lsaPtr->toString()); |
| 384 | NLSR_LOG_DEBUG("LSA Exists with seq no: " << lsaPtr->getSeqNo()); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 385 | // If its seq no is the one we are expecting. |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 386 | if (lsaPtr->getSeqNo() == lsa->getSeqNo()) { |
| 387 | if (lsaPtr->getOriginRouter() == m_thisRouterPrefix) { |
Davide Pesavento | 65ee992 | 2022-11-16 00:21:43 -0500 | [diff] [blame] | 388 | NLSR_LOG_DEBUG("Own " << lsaPtr->getType() << " LSA, so refreshing it"); |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame] | 389 | NLSR_LOG_DEBUG("Current LSA:\n" << lsaPtr->toString()); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 390 | lsaPtr->setSeqNo(lsaPtr->getSeqNo() + 1); |
| 391 | m_sequencingManager.setLsaSeq(lsaPtr->getSeqNo(), lsaPtr->getType()); |
| 392 | lsaPtr->setExpirationTimePoint(getLsaExpirationTimePoint()); |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame] | 393 | NLSR_LOG_DEBUG("Updated LSA:\n" << lsaPtr->toString()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 394 | // schedule refreshing event again |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 395 | lsaPtr->setExpiringEventId(scheduleLsaExpiration(lsaPtr, m_lsaRefreshTime)); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 396 | m_sequencingManager.writeSeqNoToFile(); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 397 | m_sync.publishRoutingUpdate(lsaPtr->getType(), m_sequencingManager.getLsaSeq(lsaPtr->getType())); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 398 | } |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 399 | // Since we cannot refresh other router's LSAs, our only choice is to expire. |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 400 | else { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 401 | NLSR_LOG_DEBUG("Other's " << lsaPtr->getType() << " LSA, so removing from LSDB"); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 402 | removeLsa(lsaIt); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 408 | void |
Alexander Afanasyev | 135288c | 2022-04-23 23:06:56 -0400 | [diff] [blame] | 409 | Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount, uint64_t incomingFaceId, |
Davide Pesavento | 658fd85 | 2023-05-10 22:15:03 -0400 | [diff] [blame] | 410 | ndn::time::steady_clock::time_point deadline) |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 411 | { |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 412 | // increment SENT_LSA_INTEREST |
| 413 | lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST); |
| 414 | |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 415 | if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) { |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 416 | deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX)); |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 417 | } |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 418 | // The first component of the interest is the name. |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 419 | ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 420 | // The seq no is the last |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 421 | uint64_t seqNo = interestName[-1].toNumber(); |
| 422 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 423 | // If the LSA is not found in the list currently. |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 424 | if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) { |
| 425 | m_highestSeqNo[lsaName] = seqNo; |
| 426 | } |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 427 | // If the new seq no is higher, that means the LSA is valid |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 428 | else if (seqNo > m_highestSeqNo[lsaName]) { |
| 429 | m_highestSeqNo[lsaName] = seqNo; |
| 430 | } |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 431 | // Otherwise, its an old/invalid LSA |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 432 | else if (seqNo < m_highestSeqNo[lsaName]) { |
| 433 | return; |
| 434 | } |
| 435 | |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 436 | ndn::Interest interest(interestName); |
Alexander Afanasyev | 135288c | 2022-04-23 23:06:56 -0400 | [diff] [blame] | 437 | if (incomingFaceId != 0) { |
| 438 | interest.setTag(std::make_shared<ndn::lp::NextHopFaceIdTag>(incomingFaceId)); |
| 439 | } |
Junxiao Shi | 43f37a0 | 2023-08-09 00:09:00 +0000 | [diff] [blame] | 440 | ndn::SegmentFetcher::Options options; |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 441 | options.interestLifetime = m_confParam.getLsaInterestLifetime(); |
awlane | 0c87b12 | 2022-06-17 15:53:10 -0500 | [diff] [blame] | 442 | options.maxTimeout = m_confParam.getLsaInterestLifetime(); |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 443 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 444 | NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo); |
Junxiao Shi | 43f37a0 | 2023-08-09 00:09:00 +0000 | [diff] [blame] | 445 | auto fetcher = ndn::SegmentFetcher::start(m_face, interest, m_confParam.getValidator(), options); |
Ashlesh Gawande | 05cb728 | 2018-08-30 14:39:41 -0500 | [diff] [blame] | 446 | |
Ashlesh Gawande | 744e481 | 2018-08-22 16:26:24 -0500 | [diff] [blame] | 447 | auto it = m_fetchers.insert(fetcher).first; |
| 448 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 449 | fetcher->afterSegmentValidated.connect([this] (const ndn::Data& data) { |
Ashlesh Gawande | 1505240 | 2018-12-12 20:20:00 -0600 | [diff] [blame] | 450 | // Nlsr class subscribes to this to fetch certificates |
| 451 | afterSegmentValidatedSignal(data); |
| 452 | |
| 453 | // If we don't do this IMS throws: std::bad_weak_ptr: bad_weak_ptr |
| 454 | auto lsaSegment = std::make_shared<const ndn::Data>(data); |
| 455 | m_lsaStorage.insert(*lsaSegment); |
Ashlesh Gawande | 1505240 | 2018-12-12 20:20:00 -0600 | [diff] [blame] | 456 | // Schedule deletion of the segment |
| 457 | m_scheduler.schedule(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT), |
Davide Pesavento | 65ee992 | 2022-11-16 00:21:43 -0500 | [diff] [blame] | 458 | [this, name = lsaSegment->getName()] { m_lsaStorage.erase(name); }); |
Ashlesh Gawande | 1505240 | 2018-12-12 20:20:00 -0600 | [diff] [blame] | 459 | }); |
| 460 | |
| 461 | fetcher->onComplete.connect([=] (const ndn::ConstBufferPtr& bufferPtr) { |
| 462 | m_lsaStorage.erase(ndn::Name(lsaName).appendNumber(seqNo - 1)); |
| 463 | afterFetchLsa(bufferPtr, interestName); |
| 464 | m_fetchers.erase(it); |
| 465 | }); |
| 466 | |
| 467 | fetcher->onError.connect([=] (uint32_t errorCode, const std::string& msg) { |
| 468 | onFetchLsaError(errorCode, msg, interestName, timeoutCount, deadline, lsaName, seqNo); |
| 469 | m_fetchers.erase(it); |
| 470 | }); |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 471 | |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 472 | Lsa::Type lsaType; |
| 473 | std::istringstream(interestName[-2].toUri()) >> lsaType; |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 474 | incrementInterestSentStats(lsaType); |
| 475 | } |
| 476 | |
| 477 | void |
| 478 | Lsdb::onFetchLsaError(uint32_t errorCode, const std::string& msg, const ndn::Name& interestName, |
Davide Pesavento | 658fd85 | 2023-05-10 22:15:03 -0400 | [diff] [blame] | 479 | uint32_t retransmitNo, const ndn::time::steady_clock::time_point& deadline, |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 480 | ndn::Name lsaName, uint64_t seqNo) |
| 481 | { |
| 482 | NLSR_LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode |
| 483 | << ", Message: " << msg); |
| 484 | |
| 485 | if (ndn::time::steady_clock::now() < deadline) { |
| 486 | auto it = m_highestSeqNo.find(lsaName); |
| 487 | if (it != m_highestSeqNo.end() && it->second == seqNo) { |
| 488 | // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express |
| 489 | // immediately since at the least the LSA Interest lifetime has elapsed. |
| 490 | // Otherwise, it is necessary to delay the Interest re-expression to prevent |
| 491 | // the potential for constant Interest flooding. |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame] | 492 | auto delay = m_confParam.getLsaInterestLifetime(); |
Junxiao Shi | 43f37a0 | 2023-08-09 00:09:00 +0000 | [diff] [blame] | 493 | if (errorCode == ndn::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame] | 494 | delay = 0_s; |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 495 | } |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame] | 496 | m_scheduler.schedule(delay, std::bind(&Lsdb::expressInterest, this, interestName, |
| 497 | retransmitNo + 1, /*Multicast FaceID*/0, deadline)); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 498 | } |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 499 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | void |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 503 | Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& interestName) |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 504 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 505 | NLSR_LOG_DEBUG("Received data for LSA interest: " << interestName); |
| 506 | lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA); |
Ashlesh Gawande | 939b6f8 | 2018-12-09 16:51:09 -0600 | [diff] [blame] | 507 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 508 | ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1); |
| 509 | uint64_t seqNo = interestName[-1].toNumber(); |
| 510 | |
| 511 | if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) { |
| 512 | m_highestSeqNo[lsaName] = seqNo; |
Ashlesh Gawande | 939b6f8 | 2018-12-09 16:51:09 -0600 | [diff] [blame] | 513 | } |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 514 | else if (seqNo > m_highestSeqNo[lsaName]) { |
| 515 | m_highestSeqNo[lsaName] = seqNo; |
Davide Pesavento | 65ee992 | 2022-11-16 00:21:43 -0500 | [diff] [blame] | 516 | NLSR_LOG_TRACE("SeqNo for LSA(name): " << interestName << " updated"); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 517 | } |
| 518 | else if (seqNo < m_highestSeqNo[lsaName]) { |
| 519 | return; |
| 520 | } |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 521 | |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 522 | std::string chkString("LSA"); |
Ashlesh Gawande | 939b6f8 | 2018-12-09 16:51:09 -0600 | [diff] [blame] | 523 | int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString); |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 524 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 525 | if (lsaPosition >= 0) { |
| 526 | // Extracts the prefix of the originating router from the data. |
| 527 | ndn::Name originRouter = m_confParam.getNetwork(); |
| 528 | originRouter.append(interestName.getSubName(lsaPosition + 1, |
| 529 | interestName.size() - lsaPosition - 3)); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 530 | try { |
| 531 | Lsa::Type interestedLsType; |
| 532 | std::istringstream(interestName[-2].toUri()) >> interestedLsType; |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 533 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 534 | if (interestedLsType == Lsa::Type::BASE) { |
| 535 | NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestName[-2].toUri()); |
| 536 | return; |
| 537 | } |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 538 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 539 | ndn::Block block(bufferPtr); |
| 540 | if (interestedLsType == Lsa::Type::NAME) { |
| 541 | lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA); |
| 542 | if (isLsaNew(originRouter, interestedLsType, seqNo)) { |
| 543 | installLsa(std::make_shared<NameLsa>(block)); |
| 544 | } |
| 545 | } |
| 546 | else if (interestedLsType == Lsa::Type::ADJACENCY) { |
| 547 | lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA); |
| 548 | if (isLsaNew(originRouter, interestedLsType, seqNo)) { |
| 549 | installLsa(std::make_shared<AdjLsa>(block)); |
| 550 | } |
| 551 | } |
| 552 | else if (interestedLsType == Lsa::Type::COORDINATE) { |
| 553 | lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA); |
| 554 | if (isLsaNew(originRouter, interestedLsType, seqNo)) { |
| 555 | installLsa(std::make_shared<CoordinateLsa>(block)); |
| 556 | } |
| 557 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 558 | } |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 559 | catch (const std::exception& e) { |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame] | 560 | NLSR_LOG_TRACE("LSA data decoding error: " << e.what()); |
Ashlesh Gawande | 939b6f8 | 2018-12-09 16:51:09 -0600 | [diff] [blame] | 561 | } |
Nick Gordon | 8f23b5d | 2017-08-31 17:53:07 -0500 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 565 | } // namespace nlsr |