blob: b2c61183bb582cc96dbc913b712dab2e3d28a3b9 [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/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
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>
34#include <ndn-cxx/signature.hpp>
akmhoque298385a2014-02-13 14:13:09 -060035
akmhoque53353462014-04-22 08:43:45 -050036namespace nlsr {
37
dmcoomescf8d0ed2017-02-21 11:39:01 -060038INIT_LOGGER(Nlsr);
akmhoque674b0b12014-05-20 14:33:28 -050039
alvy297f4162015-03-03 17:15:33 -060040const ndn::Name Nlsr::LOCALHOST_PREFIX = ndn::Name("/localhost/nlsr");
41
Ashlesh Gawande85998a12017-12-07 22:22:13 -060042Nlsr::Nlsr(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam)
43 : m_face(face)
44 , m_scheduler(face.getIoService())
Ashlesh Gawande85998a12017-12-07 22:22:13 -060045 , m_confParam(confParam)
46 , m_adjacencyList(confParam.getAdjacencyList())
47 , m_namePrefixList(confParam.getNamePrefixList())
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070048 , m_fib(m_face, m_scheduler, m_adjacencyList, m_confParam, keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060049 , m_routingTable(m_scheduler, m_fib, m_lsdb, m_namePrefixTable, m_confParam)
50 , m_namePrefixTable(m_fib, m_routingTable, m_routingTable.afterRoutingChange)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070051 , m_lsdb(m_face, keyChain, m_confParam, m_namePrefixTable, m_routingTable)
52 , m_helloProtocol(m_face, keyChain, confParam, m_routingTable, m_lsdb)
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -050053 , m_onNewLsaConnection(m_lsdb.getSync().onNewLsa->connect(
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050054 [this] (const ndn::Name& updateName, uint64_t sequenceNumber,
55 const ndn::Name& originRouter) {
56 registerStrategyForCerts(originRouter);
57 }))
58 , m_onPrefixRegistrationSuccess(m_fib.onPrefixRegistrationSuccess.connect(
59 [this] (const ndn::Name& name) {
60 m_helloProtocol.sendHelloInterest(name);
61 }))
62 , m_onHelloDataValidated(m_helloProtocol.onHelloDataValidated.connect(
63 [this] (const ndn::Name& neighbor) {
64 auto it = m_adjacencyList.findAdjacent(neighbor);
65 if (it != m_adjacencyList.end()) {
66 m_fib.registerPrefix(m_confParam.getSyncPrefix(), it->getFaceUri(), it->getLinkCost(),
67 ndn::time::milliseconds::max(), ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
68 }
69 }))
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070070 , m_dispatcher(m_face, keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060071 , m_datasetHandler(m_dispatcher, m_lsdb, m_routingTable)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070072 , m_controller(m_face, keyChain)
73 , m_faceDatasetController(m_face, keyChain)
laqinfan35731852017-08-08 06:17:39 -050074 , m_prefixUpdateProcessor(m_dispatcher,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050075 m_confParam.getPrefixUpdateValidator(),
76 m_namePrefixList,
77 m_lsdb,
78 m_confParam.getConfFileNameDynamic())
laqinfan35731852017-08-08 06:17:39 -050079 , m_nfdRibCommandProcessor(m_dispatcher,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050080 m_namePrefixList,
81 m_lsdb)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060082 , m_statsCollector(m_lsdb, m_helloProtocol)
83 , m_faceMonitor(m_face)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050084{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050085 NLSR_LOG_DEBUG("Initializing Nlsr");
86
dmcoomes9f936662017-03-02 10:33:09 -060087 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification, this, _1));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050088 m_faceMonitor.start();
Ashlesh Gawande85998a12017-12-07 22:22:13 -060089
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050090 // Do key initialization and registrations first, then initialize faces
91 // which will create routes to neighbor
92
Ashlesh Gawande85998a12017-12-07 22:22:13 -060093 setStrategies();
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050094
Saurab Dulal427e0122019-11-28 11:58:02 -060095 NLSR_LOG_DEBUG("Default NLSR identity: " << m_confParam.getSigningInfo().getSignerName());
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050096
97 // Can be moved to HelloProtocol and Lsdb ctor if initializeKey is set
98 // earlier in the Nlsr constructor so as to set m_signingInfo
99 setInfoInterestFilter();
100 setLsaInterestFilter();
101
Saurab Dulal427e0122019-11-28 11:58:02 -0600102 // Add top-level prefixes: router and localhost prefix
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500103 addDispatcherTopPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"));
104 addDispatcherTopPrefix(LOCALHOST_PREFIX);
105
106 enableIncomingFaceIdIndication();
107
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500108 registerLocalhostPrefix();
109 registerRouterPrefix();
110
111 initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1),
112 std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0));
113
114 // Could be moved into ctor, but unit tests need to be modified
115 initialize();
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500116}
117
akmhoque53353462014-04-22 08:43:45 -0500118void
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500119Nlsr::registerStrategyForCerts(const ndn::Name& originRouter)
120{
121 for (const ndn::Name& router : m_strategySetOnRouters) {
122 if (router == originRouter) {
123 // Have already set strategy for this router's certs once
124 return;
125 }
126 }
127
128 m_strategySetOnRouters.push_back(originRouter);
129
130 ndn::Name routerKey(originRouter);
131 routerKey.append("KEY");
132 ndn::Name instanceKey(originRouter);
133 instanceKey.append("nlsr").append("KEY");
134
135 m_fib.setStrategy(routerKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
136 m_fib.setStrategy(instanceKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
137
138 ndn::Name siteKey;
139 for (size_t i = 0; i < originRouter.size(); ++i) {
140 if (originRouter[i].toUri() == "%C1.Router") {
141 break;
142 }
143 siteKey.append(originRouter[i]);
144 }
145 ndn::Name opPrefix(siteKey);
146 siteKey.append("KEY");
147 m_fib.setStrategy(siteKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
148
149 opPrefix.append(std::string("%C1.Operator"));
150 m_fib.setStrategy(opPrefix, Fib::BEST_ROUTE_V2_STRATEGY, 0);
151}
152
153void
akmhoque53353462014-04-22 08:43:45 -0500154Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -0600155{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500156 NLSR_LOG_ERROR("ERROR: Failed to register prefix " << name << " in local hub's daemon");
dmcoomes9f936662017-03-02 10:33:09 -0600157 BOOST_THROW_EXCEPTION(Error("Error: Prefix registration failed"));
akmhoque53353462014-04-22 08:43:45 -0500158}
akmhoque1fd8c1e2014-02-19 19:41:49 -0600159
akmhoque157b0a42014-05-13 00:26:37 -0500160void
161Nlsr::onRegistrationSuccess(const ndn::Name& name)
162{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500163 NLSR_LOG_DEBUG("Successfully registered prefix: " << name);
alvy297f4162015-03-03 17:15:33 -0600164}
165
166void
akmhoque31d1d4b2014-05-05 22:08:14 -0500167Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -0500168{
akmhoque31d1d4b2014-05-05 22:08:14 -0500169 ndn::Name name(m_confParam.getRouterPrefix());
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500170 name.append("nlsr");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500171 name.append("INFO");
172
173 NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name);
174
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600175 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
176 std::bind(&HelloProtocol::processInterest, &m_helloProtocol, _1, _2),
177 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
178 std::bind(&Nlsr::registrationFailed, this, _1),
Saurab Dulal427e0122019-11-28 11:58:02 -0600179 m_confParam.getSigningInfo(), ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500180}
181
182void
183Nlsr::setLsaInterestFilter()
184{
akmhoque157b0a42014-05-13 00:26:37 -0500185 ndn::Name name = m_confParam.getLsaPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500186
dmcoomes5bcb39e2017-10-31 15:07:55 -0500187 NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500188
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600189 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
190 std::bind(&Lsdb::processInterest, &m_lsdb, _1, _2),
191 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
192 std::bind(&Nlsr::registrationFailed, this, _1),
Saurab Dulal427e0122019-11-28 11:58:02 -0600193 m_confParam.getSigningInfo(), ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -0500194}
195
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500196void
197Nlsr::addDispatcherTopPrefix(const ndn::Name& topPrefix)
198{
199 try {
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500200 // false since we want to have control over the registration process
Saurab Dulal427e0122019-11-28 11:58:02 -0600201 m_dispatcher.addTopPrefix(topPrefix, false, m_confParam.getSigningInfo());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500202 }
203 catch (const std::exception& e) {
204 NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
205 }
206}
207
akmhoque53353462014-04-22 08:43:45 -0500208void
akmhoquec04e7272014-07-02 11:00:14 -0500209Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -0500210{
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500211 m_fib.setStrategy(m_confParam.getLsaPrefix(), Fib::MULTICAST_STRATEGY, 0);
212 m_fib.setStrategy(m_confParam.getSyncPrefix(), Fib::MULTICAST_STRATEGY, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500213}
214
215void
akmhoque53353462014-04-22 08:43:45 -0500216Nlsr::initialize()
217{
dmcoomes9f936662017-03-02 10:33:09 -0600218 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500219 m_adjacencyList.writeLog();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500220 NLSR_LOG_DEBUG(m_namePrefixList);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500221
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600222 m_lsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500223
224 // Install coordinate LSAs if using HR or dry-run HR.
225 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600226 m_lsdb.buildAndInstallOwnCoordinateLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500227 }
Vince Lehman904c2412014-09-23 19:36:11 -0500228
Vince Lehman09131122014-09-09 17:10:11 -0500229 // Need to set direct neighbors' costs to 0 for hyperbolic routing
230 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500231 for (auto&& neighbor : m_adjacencyList.getAdjList()) {
232 neighbor.setLinkCost(0);
Vince Lehman09131122014-09-09 17:10:11 -0500233 }
234 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700235}
236
237void
alvy297f4162015-03-03 17:15:33 -0600238Nlsr::registerLocalhostPrefix()
239{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600240 m_face.registerPrefix(LOCALHOST_PREFIX,
241 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
242 std::bind(&Nlsr::registrationFailed, this, _1));
alvy297f4162015-03-03 17:15:33 -0600243}
244
245void
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500246Nlsr::registerRouterPrefix()
247{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600248 m_face.registerPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"),
249 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
250 std::bind(&Nlsr::registrationFailed, this, _1));
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500251}
252
253void
akmhoquec04e7272014-07-02 11:00:14 -0500254Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500255{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500256 NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called");
Vince Lehman02e32992015-03-11 12:31:20 -0500257
Nick Gordond5c1a372016-10-31 13:56:23 -0500258 switch (faceEventNotification.getKind()) {
259 case ndn::nfd::FACE_EVENT_DESTROYED: {
260 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500261
Nick Gordond5c1a372016-10-31 13:56:23 -0500262 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500263
Nick Gordond5c1a372016-10-31 13:56:23 -0500264 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500265 NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
Vince Lehman02e32992015-03-11 12:31:20 -0500266
Nick Gordond5c1a372016-10-31 13:56:23 -0500267 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500268
Nick Gordond5c1a372016-10-31 13:56:23 -0500269 // Only trigger an Adjacency LSA build if this node is changing
270 // from ACTIVE to INACTIVE since this rebuild will effectively
271 // cancel the previous Adjacency LSA refresh event and schedule
272 // a new one further in the future.
273 //
274 // Continuously scheduling the refresh in the future will block
275 // the router from refreshing its Adjacency LSA. Since other
276 // routers' Name prefixes' expiration times are updated when
277 // this router refreshes its Adjacency LSA, the other routers'
278 // prefixes will expire and be removed from the RIB.
279 //
280 // This check is required to fix Bug #2733 for now. This check
281 // would be unnecessary to fix Bug #2733 when Issue #2732 is
282 // completed, but the check also helps with optimization so it
283 // can remain even when Issue #2732 is implemented.
284 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
285 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500286
Nick Gordond5c1a372016-10-31 13:56:23 -0500287 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
288 // has met the HELLO retry threshold
289 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
Vince Lehman199e9cf2015-04-07 13:22:16 -0500290
Nick Gordond5c1a372016-10-31 13:56:23 -0500291 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600292 m_routingTable.scheduleRoutingTableCalculation();
Nick Gordond5c1a372016-10-31 13:56:23 -0500293 }
294 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600295 m_lsdb.scheduleAdjLsaBuild();
Nick Gordond5c1a372016-10-31 13:56:23 -0500296 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500297 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500298 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500299 break;
akmhoquec04e7272014-07-02 11:00:14 -0500300 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500301 case ndn::nfd::FACE_EVENT_CREATED: {
302 // Find the neighbor in our adjacency list
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600303 ndn::FaceUri faceUri;
304 try {
305 faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri());
306 }
307 catch (const std::exception& e) {
308 NLSR_LOG_WARN(e.what());
309 return;
310 }
311 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
Ashlesh Gawande41878572019-09-29 00:16:02 -0500312 uint64_t faceId = faceEventNotification.getFaceId();
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600313
Ashlesh Gawande41878572019-09-29 00:16:02 -0500314 // If we have a neighbor by that FaceUri and it has no FaceId or
315 // the FaceId is different from ours, we have a match.
316 if (adjacent != m_adjacencyList.end() &&
317 (adjacent->getFaceId() == 0 || adjacent->getFaceId() != faceId))
318 {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500319 NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName()
Ashlesh Gawande41878572019-09-29 00:16:02 -0500320 << ". New Face ID: " << faceId << ". Registering prefixes.");
321 adjacent->setFaceId(faceId);
Nick Gordond5c1a372016-10-31 13:56:23 -0500322
323 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500324
325 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600326 m_routingTable.scheduleRoutingTableCalculation();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500327 }
328 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600329 m_lsdb.scheduleAdjLsaBuild();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500330 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500331 }
332 break;
333 }
334 default:
335 break;
akmhoquec04e7272014-07-02 11:00:14 -0500336 }
akmhoquee1765152014-06-30 11:32:01 -0500337}
338
Nick Gordond5c1a372016-10-31 13:56:23 -0500339void
340Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess,
341 const FetchDatasetTimeoutCallback& onFetchFailure)
342{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500343 NLSR_LOG_TRACE("Initializing Faces...");
Nick Gordond5c1a372016-10-31 13:56:23 -0500344
345 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
346
347}
348
349void
350Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces)
351{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500352 NLSR_LOG_DEBUG("Processing face dataset");
Nick Gordond5c1a372016-10-31 13:56:23 -0500353
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500354 // Iterate over each neighbor listed in nlsr.conf
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500355 for (auto&& adjacent : m_adjacencyList.getAdjList()) {
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500356
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500357 const std::string& faceUriString = adjacent.getFaceUri().toString();
Nick Gordond5c1a372016-10-31 13:56:23 -0500358 // Check the list of FaceStatus objects we got for a match
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500359 for (const auto& faceStatus : faces) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500360 // Set the adjacency FaceID if we find a URI match and it was
361 // previously unset. Change the boolean to true.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500362 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500363 NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() <<
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500364 " FaceId: "<< faceStatus.getFaceId());
365 adjacent.setFaceId(faceStatus.getFaceId());
Nick Gordond5c1a372016-10-31 13:56:23 -0500366 // Register the prefixes for each neighbor
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500367 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
Nick Gordond5c1a372016-10-31 13:56:23 -0500368 }
369 }
370 // If this adjacency has no information in this dataset, then one
371 // of two things is happening: 1. NFD is starting slowly and this
372 // Face wasn't ready yet, or 2. NFD is configured
373 // incorrectly and this Face isn't available.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500374 if (adjacent.getFaceId() == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500375 NLSR_LOG_WARN("The adjacency " << adjacent.getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500376 " has no Face information in this dataset.");
377 }
378 }
379
Nick Gordond5c1a372016-10-31 13:56:23 -0500380 scheduleDatasetFetch();
381}
382
383void
384Nlsr::registerAdjacencyPrefixes(const Adjacent& adj,
385 const ndn::time::milliseconds& timeout)
386{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500387 ndn::FaceUri faceUri = adj.getFaceUri();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500388 double linkCost = adj.getLinkCost();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500389 const ndn::Name& adjName = adj.getName();
Nick Gordond5c1a372016-10-31 13:56:23 -0500390
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500391 m_fib.registerPrefix(adjName, faceUri, linkCost,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500392 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500393
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500394 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
395 faceUri, linkCost, timeout,
396 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500397}
398
399void
400Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
401 const std::string& msg,
402 uint32_t nRetriesSoFar)
403{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500404 NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout");
Nick Gordond5c1a372016-10-31 13:56:23 -0500405 // If we have exceeded the maximum attempt count, do not try again.
406 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500407 NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar);
Nick Gordond5c1a372016-10-31 13:56:23 -0500408 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
409 this, _1),
410 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
411 this, _1, _2, nRetriesSoFar));
412 }
413 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500414 NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500415 m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time.");
416 // If we fail to fetch it, just do nothing until the next
417 // interval. Since this is a backup mechanism, we aren't as
418 // concerned with retrying.
419 scheduleDatasetFetch();
420 }
421}
422
423void
424Nlsr::scheduleDatasetFetch()
425{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500426 NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval());
427
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400428 m_scheduler.schedule(m_confParam.getFaceDatasetFetchInterval(),
Nick Gordond5c1a372016-10-31 13:56:23 -0500429 [this] {
430 this->initializeFaces(
431 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
432 this->processFaceDataset(faces);
433 },
434 [this] (uint32_t code, const std::string& msg) {
435 this->onFaceDatasetFetchTimeout(code, msg, 0);
436 });
437 });
438}
akmhoquee1765152014-06-30 11:32:01 -0500439
440void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500441Nlsr::enableIncomingFaceIdIndication()
442{
443 NLSR_LOG_DEBUG("Enabling incoming face id indication for local face.");
444
445 m_controller.start<ndn::nfd::FaceUpdateCommand>(
446 ndn::nfd::ControlParameters()
447 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true),
448 bind(&Nlsr::onFaceIdIndicationSuccess, this, _1),
449 bind(&Nlsr::onFaceIdIndicationFailure, this, _1));
450}
451
452void
453Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp)
454{
455 NLSR_LOG_DEBUG("Successfully enabled incoming face id indication"
456 << "for face id " << cp.getFaceId());
457}
458
459void
460Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr)
461{
462 std::ostringstream os;
463 os << "Failed to enable incoming face id indication feature: " <<
464 "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")";
465
466 NLSR_LOG_DEBUG(os.str());
467}
468
akmhoqueb1710aa2014-02-19 17:13:36 -0600469} // namespace nlsr