NLSR 24.08-21-g73a96d2c
Loading...
Searching...
No Matches
lsdb.hpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2026, The University of Memphis,
4 * Regents of the University of California,
5 * Arizona Board of Regents.
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/>.
20 */
21
22#ifndef NLSR_LSDB_HPP
23#define NLSR_LSDB_HPP
24
26#include "conf-parameter.hpp"
27#include "lsa/lsa.hpp"
28#include "lsa/name-lsa.hpp"
30#include "lsa/adj-lsa.hpp"
32#include "signals.hpp"
33#include "statistics.hpp"
35
36#include <ndn-cxx/ims/in-memory-storage-fifo.hpp>
37#include <ndn-cxx/ims/in-memory-storage-persistent.hpp>
38#include <ndn-cxx/security/key-chain.hpp>
39#include <ndn-cxx/util/segmenter.hpp>
40#include <ndn-cxx/util/segment-fetcher.hpp>
41#include <ndn-cxx/util/signal.hpp>
42#include <ndn-cxx/util/time.hpp>
43
44#include <boost/multi_index_container.hpp>
45#include <boost/multi_index/composite_key.hpp>
46#include <boost/multi_index/hashed_index.hpp>
47
48namespace nlsr {
49
50namespace bmi = boost::multi_index;
51
52inline constexpr ndn::time::seconds GRACE_PERIOD = 10_s;
53
54enum class LsdbUpdate {
56 UPDATED,
58};
59
60class Lsdb
61{
62public:
63 Lsdb(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam);
64
65 ~Lsdb();
66
69 bool
70 doesLsaExist(const ndn::Name& router, Lsa::Type lsaType)
71 {
72 return m_lsdb.get<byName>().find(std::make_tuple(router, lsaType)) != m_lsdb.end();
73 }
74
78 void
80
83 void
84 buildAndInstallOwnCoordinateLsa();
85
97 void
98 processUpdateFromSync(const ndn::Name& updateName, uint64_t seqNo,
99 const ndn::Name& originRouter, uint64_t incomingFaceId);
100
101public:
103 void
105
106 void
107 writeLog() const;
108
109 /* \brief Process interest which can be either:
110 * 1) Discovery interest from segment fetcher:
111 * /localhop/<network>/nlsr/LSA/<site>/<router>/<lsaType>/<seqNo>
112 * 2) Interest containing segment number:
113 * /localhop/<network>/nlsr/LSA/<site>/<router>/<lsaType>/<seqNo>/<version>/<segmentNo>
114 */
115 void
116 processInterest(const ndn::Name& name, const ndn::Interest& interest);
117
118 bool
120 {
121 return m_isBuildAdjLsaScheduled;
122 }
123
126 {
127 return m_sync;
128 }
129
130 template<typename T>
131 std::shared_ptr<T>
132 findLsa(const ndn::Name& router) const
133 {
134 return std::static_pointer_cast<T>(findLsa(router, T::type()));
135 }
136
138 {
139 using result_type = ndn::Name;
140
141 ndn::Name
142 operator()(const Lsa& lsa) const
143 {
144 return lsa.getOriginRouter();
145 }
146 };
147
148 struct name_hash {
149 int
150 operator()(const ndn::Name& name) const {
151 return std::hash<ndn::Name>{}(name);
152 }
153 };
154
156 template<typename T>
157 int
158 operator()(T t) const {
159 return static_cast<int>(t);
160 }
161 };
162
163 struct byName{};
164 struct byType{};
165
166 using LsaContainer = boost::multi_index_container<
167 std::shared_ptr<Lsa>,
168 bmi::indexed_by<
169 bmi::hashed_unique<
170 bmi::tag<byName>,
171 bmi::composite_key<
172 Lsa,
174 bmi::const_mem_fun<Lsa, Lsa::Type, &Lsa::getType>
175 >,
176 bmi::composite_key_hash<name_hash, enum_class_hash>
177 >,
178 bmi::hashed_non_unique<
179 bmi::tag<byType>,
180 bmi::const_mem_fun<Lsa, Lsa::Type, &Lsa::getType>,
182 >
183 >
184 >;
185
186 template<typename T>
187 std::pair<LsaContainer::index<Lsdb::byType>::type::iterator,
188 LsaContainer::index<Lsdb::byType>::type::iterator>
190 {
191 return m_lsdb.get<byType>().equal_range(T::type());
192 }
193
195 std::shared_ptr<Lsa>
196 findLsa(const ndn::Name& router, Lsa::Type lsaType) const
197 {
198 auto it = m_lsdb.get<byName>().find(std::make_tuple(router, lsaType));
199 return it != m_lsdb.end() ? *it : nullptr;
200 }
201
202 void
203 incrementDataSentStats(Lsa::Type lsaType)
204 {
205 if (lsaType == Lsa::Type::NAME) {
207 }
208 else if (lsaType == Lsa::Type::ADJACENCY) {
210 }
211 else if (lsaType == Lsa::Type::COORDINATE) {
213 }
214 }
215
216 void
217 incrementInterestRcvdStats(Lsa::Type lsaType)
218 {
219 if (lsaType == Lsa::Type::NAME) {
221 }
222 else if (lsaType == Lsa::Type::ADJACENCY) {
224 }
225 else if (lsaType == Lsa::Type::COORDINATE) {
227 }
228 }
229
230 void
231 incrementInterestSentStats(Lsa::Type lsaType)
232 {
233 if (lsaType == Lsa::Type::NAME) {
235 }
236 else if (lsaType == Lsa::Type::ADJACENCY) {
238 }
239 else if (lsaType == Lsa::Type::COORDINATE) {
241 }
242 }
243
249 bool
250 isLsaNew(const ndn::Name& originRouter, Lsa::Type lsaType, uint64_t seqNo) const
251 {
252 // Is the name in the LSDB and the supplied seq no is the highest so far
253 auto lsaPtr = findLsa(originRouter, lsaType);
254 return lsaPtr ? lsaPtr->getSeqNo() < seqNo : true;
255 }
256
257 void
258 installLsa(std::shared_ptr<Lsa> lsa);
259
268 void
269 removeLsa(const ndn::Name& router, Lsa::Type lsaType);
270
271 void
272 removeLsa(const LsaContainer::index<Lsdb::byName>::type::iterator& lsaIt);
273
281 void
282 buildAdjLsa();
283
285 void
286 buildAndInstallOwnAdjLsa();
287
292 ndn::scheduler::EventId
293 scheduleLsaExpiration(std::shared_ptr<Lsa> lsa, ndn::time::seconds expTime);
294
298 void
299 expireOrRefreshLsa(std::shared_ptr<Lsa> lsa);
300
301 bool
302 processInterestForLsa(const ndn::Interest& interest, const ndn::Name& originRouter,
303 Lsa::Type lsaType, uint64_t seqNo);
304
305 void
306 expressInterest(const ndn::Name& interestName, uint32_t timeoutCount, uint64_t incomingFaceId,
307 ndn::time::steady_clock::time_point deadline = DEFAULT_LSA_RETRIEVAL_DEADLINE);
308
323 void
324 onFetchLsaError(uint32_t errorCode, const std::string& msg,
325 const ndn::Name& interestName, uint32_t retransmitNo,
326 const ndn::time::steady_clock::time_point& deadline,
327 ndn::Name lsaName, uint64_t seqNo);
328
335 void
336 afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& interestName);
337
338 void
339 emitSegmentValidatedSignal(const ndn::Data& data)
340 {
342 }
343
344 ndn::time::system_clock::time_point
345 getLsaExpirationTimePoint() const
346 {
347 return ndn::time::system_clock::now() + ndn::time::seconds(m_confParam.getRouterDeadInterval());
348 }
349
350public:
351 ndn::signal::Signal<Lsdb, Statistics::PacketType> lsaIncrementSignal;
352 ndn::signal::Signal<Lsdb, ndn::Data> afterSegmentValidatedSignal;
353 using AfterLsdbModified = ndn::signal::Signal<Lsdb, std::shared_ptr<Lsa>, LsdbUpdate,
354 std::list<nlsr::PrefixInfo>, std::list<nlsr::PrefixInfo>>;
357
359 ndn::Face& m_face;
360 ndn::Scheduler m_scheduler;
361 ConfParameter& m_confParam;
362
363 SyncLogicHandler m_sync;
364
365 LsaContainer m_lsdb;
366
367 ndn::time::seconds m_lsaRefreshTime;
368 ndn::time::seconds m_adjLsaBuildInterval;
369 const ndn::Name& m_thisRouterPrefix;
370
371 // Maps the name of an LSA to its highest known sequence number from sync;
372 // Used to stop NLSR from trying to fetch outdated LSAs
373 std::map<ndn::Name, uint64_t> m_highestSeqNo;
374
375 SequencingManager m_sequencingManager;
376
377 ndn::signal::ScopedConnection m_onSyncUpdate;
378
379 std::set<std::shared_ptr<ndn::SegmentFetcher>> m_fetchers;
380 ndn::Segmenter m_segmenter;
381 ndn::InMemoryStorageFifo m_segmentFifo;
382
383 bool m_isBuildAdjLsaScheduled;
384 int64_t m_adjBuildCount;
385 ndn::scheduler::ScopedEventId m_scheduledAdjLsaBuild;
386
387 ndn::InMemoryStoragePersistent m_lsaStorage;
388
389 static inline const ndn::time::steady_clock::time_point DEFAULT_LSA_RETRIEVAL_DEADLINE =
390 ndn::time::steady_clock::time_point::min();
391};
392
393} // namespace nlsr
394
395#endif // NLSR_LSDB_HPP
A class to house all the configuration parameters for NLSR.
uint32_t getRouterDeadInterval() const
Represents a Link State Announcement (LSA).
Definition lsa.hpp:48
const ndn::Name & getOriginRouter() const
Definition lsa.hpp:93
ndn::signal::Signal< Lsdb, Statistics::PacketType > lsaIncrementSignal
Definition lsdb.hpp:351
bool doesLsaExist(const ndn::Name &router, Lsa::Type lsaType)
Returns whether the LSDB contains some LSA.
Definition lsdb.hpp:70
ndn::signal::Signal< Lsdb, std::shared_ptr< Lsa >, LsdbUpdate, std::list< nlsr::PrefixInfo >, std::list< nlsr::PrefixInfo > > AfterLsdbModified
Definition lsdb.hpp:354
void buildAndInstallOwnNameLsa()
Builds a name LSA for this router and then installs it into the LSDB.
Definition lsdb.cpp:145
boost::multi_index_container< std::shared_ptr< Lsa >, bmi::indexed_by< bmi::hashed_unique< bmi::tag< byName >, bmi::composite_key< Lsa, ExtractOriginRouter, bmi::const_mem_fun< Lsa, Lsa::Type, &Lsa::getType > >, bmi::composite_key_hash< name_hash, enum_class_hash > >, bmi::hashed_non_unique< bmi::tag< byType >, bmi::const_mem_fun< Lsa, Lsa::Type, &Lsa::getType >, enum_class_hash > > > LsaContainer
Definition lsdb.hpp:184
std::shared_ptr< T > findLsa(const ndn::Name &router) const
Definition lsdb.hpp:132
AfterLsdbModified onLsdbModified
Definition lsdb.hpp:355
void writeLog() const
Definition lsdb.cpp:195
std::pair< LsaContainer::index< Lsdb::byType >::type::iterator, LsaContainer::index< Lsdb::byType >::type::iterator > getLsdbIterator() const
Definition lsdb.hpp:189
void processInterest(const ndn::Name &name, const ndn::Interest &interest)
Definition lsdb.cpp:214
SyncLogicHandler & getSync()
Definition lsdb.hpp:125
ndn::signal::Signal< Lsdb, ndn::Data > afterSegmentValidatedSignal
Definition lsdb.hpp:352
bool getIsBuildAdjLsaScheduled() const
Definition lsdb.hpp:119
void scheduleAdjLsaBuild()
Schedules a build of this router's LSA.
Definition lsdb.cpp:174
OnLsaUpdate onNewLsa
Definition lsdb.hpp:356
NLSR-to-sync interaction point.
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
ndn::signal::Signal< Lsdb, ndn::Name, uint64_t, ndn::Name, uint64_t > OnLsaUpdate
Definition signals.hpp:38
LsdbUpdate
Definition lsdb.hpp:54
constexpr ndn::time::seconds GRACE_PERIOD
Definition lsdb.hpp:52
ndn::Name operator()(const Lsa &lsa) const
Definition lsdb.hpp:142
int operator()(T t) const
Definition lsdb.hpp:158
int operator()(const ndn::Name &name) const
Definition lsdb.hpp:150
#define PUBLIC_WITH_TESTS_ELSE_PRIVATE