blob: 09d5c27c576ac4fb51cf98b91ac001274221603f [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
akmhoque53353462014-04-22 08:43:45 -050022#include "sync-logic-handler.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050023
Vince Lehman0a7da612014-10-29 14:39:29 -050024#include "common.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050025#include "conf-parameter.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050026#include "logger.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050027#include "lsa.hpp"
28#include "lsdb.hpp"
29#include "sequencing-manager.hpp"
30#include "utility/name-helper.hpp"
31
akmhoque53353462014-04-22 08:43:45 -050032namespace nlsr {
33
akmhoque674b0b12014-05-20 14:33:28 -050034INIT_LOGGER("SyncLogicHandler");
35
akmhoquefdbddb12014-05-02 18:35:19 -050036using namespace ndn;
37using namespace std;
38
Vince Lehman904c2412014-09-23 19:36:11 -050039class SyncUpdate
40{
41public:
42 class Error : public std::runtime_error
43 {
44 public:
45 explicit
46 Error(const std::string& what)
47 : std::runtime_error(what)
48 {
49 }
50 };
51
52public:
53 SyncUpdate(const ndn::Name& name, uint64_t seqNo)
54 : m_name(name)
55 , m_seqManager(seqNo)
56 {
57 }
58
59 const ndn::Name&
60 getName() const
61 {
62 return m_name;
63 }
64
65 const ndn::Name
66 getOriginRouter() const
67 {
68 int32_t nlsrPosition = util::getNameComponentPosition(m_name, NLSR_COMPONENT);
69 int32_t lsaPosition = util::getNameComponentPosition(m_name, LSA_COMPONENT);
70
71 if (nlsrPosition < 0 || lsaPosition < 0) {
dmcoomes9f936662017-03-02 10:33:09 -060072 BOOST_THROW_EXCEPTION(Error("Cannot parse update name because expected components are missing"));
Vince Lehman904c2412014-09-23 19:36:11 -050073 }
74
Ashlesh Gawande6077ea92017-01-19 11:48:29 -060075 ndn::Name networkName = m_name.getSubName(1, nlsrPosition-1);
Vince Lehman904c2412014-09-23 19:36:11 -050076 ndn::Name routerName = m_name.getSubName(lsaPosition + 1);
77
78 ndn::Name originRouter = networkName;
79 originRouter.append(routerName);
80
81 return originRouter;
82 }
83
84 uint64_t
85 getNameLsaSeqNo() const
86 {
87 return m_seqManager.getNameLsaSeq();
88 }
89
90 uint64_t
91 getAdjLsaSeqNo() const
92 {
93 return m_seqManager.getAdjLsaSeq();
94 }
95
96 uint64_t
97 getCorLsaSeqNo() const
98 {
99 return m_seqManager.getCorLsaSeq();
100 }
101
102 const SequencingManager&
103 getSequencingManager() const
104 {
105 return m_seqManager;
106 }
107
108private:
109 const ndn::Name m_name;
110 SequencingManager m_seqManager;
111
112 static const std::string NLSR_COMPONENT;
113 static const std::string LSA_COMPONENT;
114};
115
116const std::string SyncUpdate::NLSR_COMPONENT = "NLSR";
117const std::string SyncUpdate::LSA_COMPONENT = "LSA";
118
Alexander Afanasyev7c8882f2014-10-28 12:12:15 -0700119template<class T>
120class NullDeleter
121{
122public:
123 void
124 operator()(T*)
125 {
126 }
127};
128
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600129SyncLogicHandler::SyncLogicHandler(ndn::Face& face,
130 Lsdb& lsdb, ConfParameter& conf,
131 SequencingManager& seqManager)
132 : m_validator(new ndn::ValidatorNull())
133 , m_syncFace(face)
134 , m_lsdb(lsdb)
135 , m_confParam(conf)
136 , m_sequencingManager(seqManager)
137{
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600138}
139
akmhoque53353462014-04-22 08:43:45 -0500140void
Vince Lehman9d097802015-03-16 17:55:59 -0500141SyncLogicHandler::createSyncSocket(const ndn::Name& syncPrefix)
akmhoque53353462014-04-22 08:43:45 -0500142{
Vince Lehman9d097802015-03-16 17:55:59 -0500143 if (m_syncSocket != nullptr) {
144 _LOG_WARN("Trying to create Sync socket, but Sync socket already exists");
145 return;
146 }
147
148 m_syncPrefix = syncPrefix;
149
150 // Build LSA sync update prefix
151 buildUpdatePrefix();
152
akmhoque674b0b12014-05-20 14:33:28 -0500153 _LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix);
Vince Lehman904c2412014-09-23 19:36:11 -0500154
155 // The face's lifetime is managed in main.cpp; SyncSocket should not manage the memory
156 // of the object
dmcoomes9f936662017-03-02 10:33:09 -0600157 std::shared_ptr<ndn::Face> facePtr(&m_syncFace, NullDeleter<ndn::Face>());
Vince Lehman904c2412014-09-23 19:36:11 -0500158
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600159 m_syncSocket = std::make_shared<chronosync::Socket>(m_syncPrefix, m_updatePrefix, *facePtr,
160 bind(&SyncLogicHandler::onNsyncUpdate, this, _1));
akmhoque53353462014-04-22 08:43:45 -0500161}
162
163void
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600164SyncLogicHandler::onNsyncUpdate(const vector<chronosync::MissingDataInfo>& v)
akmhoque53353462014-04-22 08:43:45 -0500165{
Vince Lehman904c2412014-09-23 19:36:11 -0500166 _LOG_DEBUG("Received Nsync update event");
167
168 for (size_t i = 0; i < v.size(); i++){
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600169 _LOG_DEBUG("Update Name: " << v[i].session.getPrefix(-1).toUri() << " Seq no: " << v[i].high);
Vince Lehman904c2412014-09-23 19:36:11 -0500170
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600171 SyncUpdate update(v[i].session.getPrefix(-1), v[i].high);
Vince Lehman904c2412014-09-23 19:36:11 -0500172
173 processUpdateFromSync(update);
akmhoque53353462014-04-22 08:43:45 -0500174 }
175}
176
177void
Vince Lehman904c2412014-09-23 19:36:11 -0500178SyncLogicHandler::onNsyncRemoval(const string& prefix)
akmhoque53353462014-04-22 08:43:45 -0500179{
Vince Lehman904c2412014-09-23 19:36:11 -0500180 _LOG_DEBUG("Received Nsync removal event");
akmhoque53353462014-04-22 08:43:45 -0500181}
182
183void
Vince Lehman904c2412014-09-23 19:36:11 -0500184SyncLogicHandler::processUpdateFromSync(const SyncUpdate& update)
akmhoque53353462014-04-22 08:43:45 -0500185{
Vince Lehman904c2412014-09-23 19:36:11 -0500186 ndn::Name originRouter;
akmhoque53353462014-04-22 08:43:45 -0500187
Vince Lehman904c2412014-09-23 19:36:11 -0500188 try {
189 originRouter = update.getOriginRouter();
190 }
dmcoomes9f936662017-03-02 10:33:09 -0600191 catch (const std::exception& e) {
Vince Lehman904c2412014-09-23 19:36:11 -0500192 _LOG_WARN("Received malformed sync update");
akmhoque31d1d4b2014-05-05 22:08:14 -0500193 return;
akmhoque53353462014-04-22 08:43:45 -0500194 }
akmhoque53353462014-04-22 08:43:45 -0500195
Vince Lehman904c2412014-09-23 19:36:11 -0500196 // A router should not try to fetch its own LSA
197 if (originRouter != m_confParam.getRouterPrefix()) {
198
199 update.getSequencingManager().writeLog();
200
Nick Gordon5c467f02016-07-13 13:40:10 -0500201 if (isLsaNew(originRouter, NameLsa::TYPE_STRING, update.getNameLsaSeqNo())) {
Vince Lehman904c2412014-09-23 19:36:11 -0500202 _LOG_DEBUG("Received sync update with higher Name LSA sequence number than entry in LSDB");
203
alvy49b1c0c2014-12-19 13:57:46 -0600204 expressInterestForLsa(update, NameLsa::TYPE_STRING, update.getNameLsaSeqNo());
akmhoque31d1d4b2014-05-05 22:08:14 -0500205 }
Vince Lehman904c2412014-09-23 19:36:11 -0500206
alvy49b1c0c2014-12-19 13:57:46 -0600207 if (isLsaNew(originRouter, AdjLsa::TYPE_STRING, update.getAdjLsaSeqNo())) {
Vince Lehman904c2412014-09-23 19:36:11 -0500208 _LOG_DEBUG("Received sync update with higher Adj LSA sequence number than entry in LSDB");
Nick Gordon5c467f02016-07-13 13:40:10 -0500209 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
210 if (update.getAdjLsaSeqNo() != 0) {
211 _LOG_ERROR("Tried to fetch an adjacency LSA when hyperbolic routing"
212 << " is enabled.");
213 }
214 }
215 else {
216 expressInterestForLsa(update, AdjLsa::TYPE_STRING, update.getAdjLsaSeqNo());
217 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500218 }
Vince Lehman904c2412014-09-23 19:36:11 -0500219
alvy49b1c0c2014-12-19 13:57:46 -0600220 if (isLsaNew(originRouter, CoordinateLsa::TYPE_STRING, update.getCorLsaSeqNo())) {
Vince Lehman904c2412014-09-23 19:36:11 -0500221 _LOG_DEBUG("Received sync update with higher Cor LSA sequence number than entry in LSDB");
Nick Gordon5c467f02016-07-13 13:40:10 -0500222 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
223 if (update.getCorLsaSeqNo() != 0) {
224 _LOG_ERROR("Tried to fetch a coordinate LSA when link-state"
225 << " is enabled.");
226 }
227 }
228 else {
229 expressInterestForLsa(update, CoordinateLsa::TYPE_STRING, update.getCorLsaSeqNo());
230 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500231 }
akmhoque53353462014-04-22 08:43:45 -0500232 }
233}
234
Vince Lehman904c2412014-09-23 19:36:11 -0500235bool
236SyncLogicHandler::isLsaNew(const ndn::Name& originRouter, const std::string& lsaType,
237 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500238{
Vince Lehman904c2412014-09-23 19:36:11 -0500239 ndn::Name lsaKey = originRouter;
240 lsaKey.append(lsaType);
241
alvy49b1c0c2014-12-19 13:57:46 -0600242 if (lsaType == NameLsa::TYPE_STRING)
Vince Lehman904c2412014-09-23 19:36:11 -0500243 {
244 return m_lsdb.isNameLsaNew(lsaKey, seqNo);
245 }
alvy49b1c0c2014-12-19 13:57:46 -0600246 else if (lsaType == AdjLsa::TYPE_STRING)
Vince Lehman904c2412014-09-23 19:36:11 -0500247 {
248 return m_lsdb.isAdjLsaNew(lsaKey, seqNo);
249 }
alvy49b1c0c2014-12-19 13:57:46 -0600250 else if (lsaType == CoordinateLsa::TYPE_STRING)
Vince Lehman904c2412014-09-23 19:36:11 -0500251 {
252 return m_lsdb.isCoordinateLsaNew(lsaKey, seqNo);
253 }
254
255 return false;
akmhoque53353462014-04-22 08:43:45 -0500256}
257
akmhoque53353462014-04-22 08:43:45 -0500258void
Vince Lehman904c2412014-09-23 19:36:11 -0500259SyncLogicHandler::expressInterestForLsa(const SyncUpdate& update, std::string lsaType,
260 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500261{
Vince Lehman904c2412014-09-23 19:36:11 -0500262 ndn::Name interest(update.getName());
263 interest.append(lsaType);
264 interest.appendNumber(seqNo);
265
266 m_lsdb.expressInterest(interest, 0);
267}
268
269void
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600270SyncLogicHandler::publishRoutingUpdate()
Vince Lehman904c2412014-09-23 19:36:11 -0500271{
Vince Lehman9d097802015-03-16 17:55:59 -0500272 if (m_syncSocket == nullptr) {
273 _LOG_FATAL("Cannot publish routing update; SyncSocket does not exist");
274
dmcoomes9f936662017-03-02 10:33:09 -0600275 BOOST_THROW_EXCEPTION(SyncLogicHandler::Error("Cannot publish routing update; SyncSocket does not exist"));
Vince Lehman9d097802015-03-16 17:55:59 -0500276 }
277
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600278 m_sequencingManager.writeSeqNoToFile();
279
280 publishSyncUpdate(m_updatePrefix, m_sequencingManager.getCombinedSeqNo());
Vince Lehman904c2412014-09-23 19:36:11 -0500281}
282
283void
Vince Lehmanc11cc202015-01-20 11:41:33 -0600284SyncLogicHandler::buildUpdatePrefix()
285{
286 m_updatePrefix = m_confParam.getLsaPrefix();
287 m_updatePrefix.append(m_confParam.getSiteName());
288 m_updatePrefix.append(m_confParam.getRouterName());
289}
290
291void
Vince Lehman904c2412014-09-23 19:36:11 -0500292SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix, uint64_t seqNo)
293{
294 _LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << " Seq No: " << seqNo);
295
akmhoque53353462014-04-22 08:43:45 -0500296 ndn::Name updateName(updatePrefix);
297 string data("NoData");
Vince Lehman904c2412014-09-23 19:36:11 -0500298
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600299 m_syncSocket->publishData(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(),
300 ndn::time::milliseconds(1000), seqNo, updateName);
akmhoque53353462014-04-22 08:43:45 -0500301}
302
Nick Gordonfad8e252016-08-11 14:21:38 -0500303} // namespace nlsr