blob: 590c8a46c30db84e1d15fa471a906403856acd91 [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/*
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, 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 Gawande57a87172020-05-09 19:47:06 -070020 */
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050021
Laqin Fan54a43f02017-03-08 12:31:30 -060022#include "nlsr.hpp"
23#include "adjacent.hpp"
24#include "logger.hpp"
25
akmhoque298385a2014-02-13 14:13:09 -060026#include <cstdlib>
akmhoque92afde42014-02-18 14:04:07 -060027#include <string>
akmhoque298385a2014-02-13 14:13:09 -060028#include <sstream>
akmhoque05d5fcf2014-04-15 14:58:45 -050029#include <cstdio>
akmhoque0494c252014-07-23 23:46:44 -050030#include <unistd.h>
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000031#include <vector>
akmhoque298385a2014-02-13 14:13:09 -060032
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050033#include <ndn-cxx/net/face-uri.hpp>
akmhoque298385a2014-02-13 14:13:09 -060034
akmhoque53353462014-04-22 08:43:45 -050035namespace nlsr {
36
dmcoomescf8d0ed2017-02-21 11:39:01 -060037INIT_LOGGER(Nlsr);
akmhoque674b0b12014-05-20 14:33:28 -050038
alvy297f4162015-03-03 17:15:33 -060039const ndn::Name Nlsr::LOCALHOST_PREFIX = ndn::Name("/localhost/nlsr");
40
Ashlesh Gawande85998a12017-12-07 22:22:13 -060041Nlsr::Nlsr(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam)
42 : m_face(face)
43 , m_scheduler(face.getIoService())
Ashlesh Gawande85998a12017-12-07 22:22:13 -060044 , m_confParam(confParam)
45 , m_adjacencyList(confParam.getAdjacencyList())
46 , m_namePrefixList(confParam.getNamePrefixList())
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070047 , m_fib(m_face, m_scheduler, m_adjacencyList, m_confParam, keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060048 , m_routingTable(m_scheduler, m_fib, m_lsdb, m_namePrefixTable, m_confParam)
49 , m_namePrefixTable(m_fib, m_routingTable, m_routingTable.afterRoutingChange)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070050 , m_lsdb(m_face, keyChain, m_confParam, m_namePrefixTable, m_routingTable)
51 , m_helloProtocol(m_face, keyChain, confParam, m_routingTable, m_lsdb)
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -050052 , m_onNewLsaConnection(m_lsdb.getSync().onNewLsa->connect(
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050053 [this] (const ndn::Name& updateName, uint64_t sequenceNumber,
54 const ndn::Name& originRouter) {
55 registerStrategyForCerts(originRouter);
56 }))
57 , m_onPrefixRegistrationSuccess(m_fib.onPrefixRegistrationSuccess.connect(
58 [this] (const ndn::Name& name) {
59 m_helloProtocol.sendHelloInterest(name);
60 }))
61 , m_onHelloDataValidated(m_helloProtocol.onHelloDataValidated.connect(
62 [this] (const ndn::Name& neighbor) {
63 auto it = m_adjacencyList.findAdjacent(neighbor);
64 if (it != m_adjacencyList.end()) {
65 m_fib.registerPrefix(m_confParam.getSyncPrefix(), it->getFaceUri(), it->getLinkCost(),
66 ndn::time::milliseconds::max(), ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
67 }
68 }))
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070069 , m_dispatcher(m_face, keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060070 , m_datasetHandler(m_dispatcher, m_lsdb, m_routingTable)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070071 , m_controller(m_face, keyChain)
72 , m_faceDatasetController(m_face, keyChain)
laqinfan35731852017-08-08 06:17:39 -050073 , m_prefixUpdateProcessor(m_dispatcher,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050074 m_confParam.getPrefixUpdateValidator(),
75 m_namePrefixList,
76 m_lsdb,
77 m_confParam.getConfFileNameDynamic())
laqinfan35731852017-08-08 06:17:39 -050078 , m_nfdRibCommandProcessor(m_dispatcher,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050079 m_namePrefixList,
80 m_lsdb)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060081 , m_statsCollector(m_lsdb, m_helloProtocol)
82 , m_faceMonitor(m_face)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050083{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050084 NLSR_LOG_DEBUG("Initializing Nlsr");
85
dmcoomes9f936662017-03-02 10:33:09 -060086 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification, this, _1));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050087 m_faceMonitor.start();
Ashlesh Gawande85998a12017-12-07 22:22:13 -060088
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050089 // Do key initialization and registrations first, then initialize faces
90 // which will create routes to neighbor
91
Ashlesh Gawande85998a12017-12-07 22:22:13 -060092 setStrategies();
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050093
Saurab Dulal427e0122019-11-28 11:58:02 -060094 NLSR_LOG_DEBUG("Default NLSR identity: " << m_confParam.getSigningInfo().getSignerName());
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050095
Ashlesh Gawande214032e2020-12-22 17:20:14 -050096 // Can be moved to Lsdb ctor
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050097 setLsaInterestFilter();
98
Saurab Dulal427e0122019-11-28 11:58:02 -060099 // Add top-level prefixes: router and localhost prefix
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500100 addDispatcherTopPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"));
101 addDispatcherTopPrefix(LOCALHOST_PREFIX);
102
103 enableIncomingFaceIdIndication();
104
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500105 registerLocalhostPrefix();
106 registerRouterPrefix();
107
108 initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1),
109 std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0));
110
111 // Could be moved into ctor, but unit tests need to be modified
112 initialize();
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500113}
114
akmhoque53353462014-04-22 08:43:45 -0500115void
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500116Nlsr::registerStrategyForCerts(const ndn::Name& originRouter)
117{
118 for (const ndn::Name& router : m_strategySetOnRouters) {
119 if (router == originRouter) {
120 // Have already set strategy for this router's certs once
121 return;
122 }
123 }
124
125 m_strategySetOnRouters.push_back(originRouter);
126
127 ndn::Name routerKey(originRouter);
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700128 routerKey.append(ndn::security::Certificate::KEY_COMPONENT);
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500129 ndn::Name instanceKey(originRouter);
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700130 instanceKey.append("nlsr").append(ndn::security::Certificate::KEY_COMPONENT);
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500131
132 m_fib.setStrategy(routerKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
133 m_fib.setStrategy(instanceKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
134
135 ndn::Name siteKey;
136 for (size_t i = 0; i < originRouter.size(); ++i) {
137 if (originRouter[i].toUri() == "%C1.Router") {
138 break;
139 }
140 siteKey.append(originRouter[i]);
141 }
142 ndn::Name opPrefix(siteKey);
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700143 siteKey.append(ndn::security::Certificate::KEY_COMPONENT);
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500144 m_fib.setStrategy(siteKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
145
146 opPrefix.append(std::string("%C1.Operator"));
147 m_fib.setStrategy(opPrefix, Fib::BEST_ROUTE_V2_STRATEGY, 0);
148}
149
150void
akmhoque53353462014-04-22 08:43:45 -0500151Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -0600152{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500153 NLSR_LOG_ERROR("ERROR: Failed to register prefix " << name << " in local hub's daemon");
Ashlesh Gawande214032e2020-12-22 17:20:14 -0500154 NDN_THROW(Error("Error: Prefix registration failed"));
akmhoque53353462014-04-22 08:43:45 -0500155}
akmhoque1fd8c1e2014-02-19 19:41:49 -0600156
akmhoque157b0a42014-05-13 00:26:37 -0500157void
158Nlsr::onRegistrationSuccess(const ndn::Name& name)
159{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500160 NLSR_LOG_DEBUG("Successfully registered prefix: " << name);
alvy297f4162015-03-03 17:15:33 -0600161}
162
163void
akmhoque31d1d4b2014-05-05 22:08:14 -0500164Nlsr::setLsaInterestFilter()
165{
akmhoque157b0a42014-05-13 00:26:37 -0500166 ndn::Name name = m_confParam.getLsaPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500167
dmcoomes5bcb39e2017-10-31 15:07:55 -0500168 NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500169
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600170 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
171 std::bind(&Lsdb::processInterest, &m_lsdb, _1, _2),
172 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
173 std::bind(&Nlsr::registrationFailed, this, _1),
Saurab Dulal427e0122019-11-28 11:58:02 -0600174 m_confParam.getSigningInfo(), ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -0500175}
176
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500177void
178Nlsr::addDispatcherTopPrefix(const ndn::Name& topPrefix)
179{
180 try {
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500181 // false since we want to have control over the registration process
Saurab Dulal427e0122019-11-28 11:58:02 -0600182 m_dispatcher.addTopPrefix(topPrefix, false, m_confParam.getSigningInfo());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500183 }
184 catch (const std::exception& e) {
185 NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
186 }
187}
188
akmhoque53353462014-04-22 08:43:45 -0500189void
akmhoquec04e7272014-07-02 11:00:14 -0500190Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -0500191{
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500192 m_fib.setStrategy(m_confParam.getLsaPrefix(), Fib::MULTICAST_STRATEGY, 0);
193 m_fib.setStrategy(m_confParam.getSyncPrefix(), Fib::MULTICAST_STRATEGY, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500194}
195
196void
akmhoque53353462014-04-22 08:43:45 -0500197Nlsr::initialize()
198{
dmcoomes9f936662017-03-02 10:33:09 -0600199 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500200 m_adjacencyList.writeLog();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500201 NLSR_LOG_DEBUG(m_namePrefixList);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500202
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600203 m_lsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500204
205 // Install coordinate LSAs if using HR or dry-run HR.
206 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600207 m_lsdb.buildAndInstallOwnCoordinateLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500208 }
Vince Lehman904c2412014-09-23 19:36:11 -0500209
Vince Lehman09131122014-09-09 17:10:11 -0500210 // Need to set direct neighbors' costs to 0 for hyperbolic routing
211 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500212 for (auto&& neighbor : m_adjacencyList.getAdjList()) {
213 neighbor.setLinkCost(0);
Vince Lehman09131122014-09-09 17:10:11 -0500214 }
215 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700216}
217
218void
alvy297f4162015-03-03 17:15:33 -0600219Nlsr::registerLocalhostPrefix()
220{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600221 m_face.registerPrefix(LOCALHOST_PREFIX,
222 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
223 std::bind(&Nlsr::registrationFailed, this, _1));
alvy297f4162015-03-03 17:15:33 -0600224}
225
226void
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500227Nlsr::registerRouterPrefix()
228{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600229 m_face.registerPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"),
230 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
231 std::bind(&Nlsr::registrationFailed, this, _1));
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500232}
233
234void
akmhoquec04e7272014-07-02 11:00:14 -0500235Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500236{
Ashlesh Gawande214032e2020-12-22 17:20:14 -0500237 NLSR_LOG_TRACE("onFaceEventNotification called");
Vince Lehman02e32992015-03-11 12:31:20 -0500238
Nick Gordond5c1a372016-10-31 13:56:23 -0500239 switch (faceEventNotification.getKind()) {
240 case ndn::nfd::FACE_EVENT_DESTROYED: {
241 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500242
Nick Gordond5c1a372016-10-31 13:56:23 -0500243 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500244
Nick Gordond5c1a372016-10-31 13:56:23 -0500245 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500246 NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
Vince Lehman02e32992015-03-11 12:31:20 -0500247
Nick Gordond5c1a372016-10-31 13:56:23 -0500248 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500249
Nick Gordond5c1a372016-10-31 13:56:23 -0500250 // Only trigger an Adjacency LSA build if this node is changing
251 // from ACTIVE to INACTIVE since this rebuild will effectively
252 // cancel the previous Adjacency LSA refresh event and schedule
253 // a new one further in the future.
254 //
255 // Continuously scheduling the refresh in the future will block
256 // the router from refreshing its Adjacency LSA. Since other
257 // routers' Name prefixes' expiration times are updated when
258 // this router refreshes its Adjacency LSA, the other routers'
259 // prefixes will expire and be removed from the RIB.
260 //
261 // This check is required to fix Bug #2733 for now. This check
262 // would be unnecessary to fix Bug #2733 when Issue #2732 is
263 // completed, but the check also helps with optimization so it
264 // can remain even when Issue #2732 is implemented.
265 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
266 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500267
Nick Gordond5c1a372016-10-31 13:56:23 -0500268 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
269 // has met the HELLO retry threshold
270 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
Vince Lehman199e9cf2015-04-07 13:22:16 -0500271
Ashlesh Gawande214032e2020-12-22 17:20:14 -0500272 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600273 m_routingTable.scheduleRoutingTableCalculation();
Nick Gordond5c1a372016-10-31 13:56:23 -0500274 }
275 else {
Ashlesh Gawande214032e2020-12-22 17:20:14 -0500276 // Will call scheduleRoutingTableCalculation internally
277 // if needed in case of LS or DRY_RUN
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600278 m_lsdb.scheduleAdjLsaBuild();
Nick Gordond5c1a372016-10-31 13:56:23 -0500279 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500280 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500281 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500282 break;
akmhoquec04e7272014-07-02 11:00:14 -0500283 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500284 case ndn::nfd::FACE_EVENT_CREATED: {
285 // Find the neighbor in our adjacency list
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600286 ndn::FaceUri faceUri;
287 try {
288 faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri());
289 }
290 catch (const std::exception& e) {
291 NLSR_LOG_WARN(e.what());
292 return;
293 }
294 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
Ashlesh Gawande41878572019-09-29 00:16:02 -0500295 uint64_t faceId = faceEventNotification.getFaceId();
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600296
Ashlesh Gawande41878572019-09-29 00:16:02 -0500297 // If we have a neighbor by that FaceUri and it has no FaceId or
298 // the FaceId is different from ours, we have a match.
299 if (adjacent != m_adjacencyList.end() &&
300 (adjacent->getFaceId() == 0 || adjacent->getFaceId() != faceId))
301 {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500302 NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName()
Ashlesh Gawande41878572019-09-29 00:16:02 -0500303 << ". New Face ID: " << faceId << ". Registering prefixes.");
304 adjacent->setFaceId(faceId);
Nick Gordond5c1a372016-10-31 13:56:23 -0500305
306 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500307
Ashlesh Gawande214032e2020-12-22 17:20:14 -0500308 // We should not do scheduleRoutingTableCalculation or scheduleAdjLsaBuild here
309 // because once the prefixes are registered, we send a HelloInterest
310 // to the prefix (see NLSR ctor). HelloProtocol will call these functions
311 // once HelloData is received and validated.
Nick Gordond5c1a372016-10-31 13:56:23 -0500312 }
313 break;
314 }
315 default:
316 break;
akmhoquec04e7272014-07-02 11:00:14 -0500317 }
akmhoquee1765152014-06-30 11:32:01 -0500318}
319
Nick Gordond5c1a372016-10-31 13:56:23 -0500320void
321Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess,
322 const FetchDatasetTimeoutCallback& onFetchFailure)
323{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500324 NLSR_LOG_TRACE("Initializing Faces...");
Nick Gordond5c1a372016-10-31 13:56:23 -0500325
326 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
327
328}
329
330void
331Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces)
332{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500333 NLSR_LOG_DEBUG("Processing face dataset");
Nick Gordond5c1a372016-10-31 13:56:23 -0500334
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500335 // Iterate over each neighbor listed in nlsr.conf
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500336 for (auto&& adjacent : m_adjacencyList.getAdjList()) {
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500337
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500338 const std::string& faceUriString = adjacent.getFaceUri().toString();
Nick Gordond5c1a372016-10-31 13:56:23 -0500339 // Check the list of FaceStatus objects we got for a match
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500340 for (const auto& faceStatus : faces) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500341 // Set the adjacency FaceID if we find a URI match and it was
342 // previously unset. Change the boolean to true.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500343 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500344 NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() <<
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500345 " FaceId: "<< faceStatus.getFaceId());
346 adjacent.setFaceId(faceStatus.getFaceId());
Nick Gordond5c1a372016-10-31 13:56:23 -0500347 // Register the prefixes for each neighbor
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500348 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
Nick Gordond5c1a372016-10-31 13:56:23 -0500349 }
350 }
351 // If this adjacency has no information in this dataset, then one
352 // of two things is happening: 1. NFD is starting slowly and this
353 // Face wasn't ready yet, or 2. NFD is configured
354 // incorrectly and this Face isn't available.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500355 if (adjacent.getFaceId() == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500356 NLSR_LOG_WARN("The adjacency " << adjacent.getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500357 " has no Face information in this dataset.");
358 }
359 }
360
Nick Gordond5c1a372016-10-31 13:56:23 -0500361 scheduleDatasetFetch();
362}
363
364void
365Nlsr::registerAdjacencyPrefixes(const Adjacent& adj,
366 const ndn::time::milliseconds& timeout)
367{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500368 ndn::FaceUri faceUri = adj.getFaceUri();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500369 double linkCost = adj.getLinkCost();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500370 const ndn::Name& adjName = adj.getName();
Nick Gordond5c1a372016-10-31 13:56:23 -0500371
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500372 m_fib.registerPrefix(adjName, faceUri, linkCost,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500373 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500374
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500375 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
376 faceUri, linkCost, timeout,
377 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500378}
379
380void
381Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
382 const std::string& msg,
383 uint32_t nRetriesSoFar)
384{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500385 NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout");
Nick Gordond5c1a372016-10-31 13:56:23 -0500386 // If we have exceeded the maximum attempt count, do not try again.
387 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500388 NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar);
Nick Gordond5c1a372016-10-31 13:56:23 -0500389 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
390 this, _1),
391 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
392 this, _1, _2, nRetriesSoFar));
393 }
394 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500395 NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500396 m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time.");
397 // If we fail to fetch it, just do nothing until the next
398 // interval. Since this is a backup mechanism, we aren't as
399 // concerned with retrying.
400 scheduleDatasetFetch();
401 }
402}
403
404void
405Nlsr::scheduleDatasetFetch()
406{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500407 NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval());
408
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400409 m_scheduler.schedule(m_confParam.getFaceDatasetFetchInterval(),
Nick Gordond5c1a372016-10-31 13:56:23 -0500410 [this] {
411 this->initializeFaces(
412 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
413 this->processFaceDataset(faces);
414 },
415 [this] (uint32_t code, const std::string& msg) {
416 this->onFaceDatasetFetchTimeout(code, msg, 0);
417 });
418 });
419}
akmhoquee1765152014-06-30 11:32:01 -0500420
421void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500422Nlsr::enableIncomingFaceIdIndication()
423{
424 NLSR_LOG_DEBUG("Enabling incoming face id indication for local face.");
425
426 m_controller.start<ndn::nfd::FaceUpdateCommand>(
427 ndn::nfd::ControlParameters()
428 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true),
429 bind(&Nlsr::onFaceIdIndicationSuccess, this, _1),
430 bind(&Nlsr::onFaceIdIndicationFailure, this, _1));
431}
432
433void
434Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp)
435{
436 NLSR_LOG_DEBUG("Successfully enabled incoming face id indication"
437 << "for face id " << cp.getFaceId());
438}
439
440void
441Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr)
442{
443 std::ostringstream os;
444 os << "Failed to enable incoming face id indication feature: " <<
445 "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")";
446
447 NLSR_LOG_DEBUG(os.str());
448}
449
akmhoqueb1710aa2014-02-19 17:13:36 -0600450} // namespace nlsr