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 | /* |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
Muktadir R Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 21 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 22 | #include "nlsr.hpp" |
| 23 | #include "adjacent.hpp" |
| 24 | #include "logger.hpp" |
| 25 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 26 | #include <cstdlib> |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame] | 27 | #include <string> |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 28 | #include <sstream> |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 29 | #include <cstdio> |
akmhoque | 0494c25 | 2014-07-23 23:46:44 -0500 | [diff] [blame] | 30 | #include <unistd.h> |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 31 | #include <vector> |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 32 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 33 | #include <ndn-cxx/net/face-uri.hpp> |
| 34 | #include <ndn-cxx/signature.hpp> |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 35 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | namespace nlsr { |
| 37 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 38 | INIT_LOGGER(Nlsr); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 39 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 40 | const ndn::Name Nlsr::LOCALHOST_PREFIX = ndn::Name("/localhost/nlsr"); |
| 41 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 42 | Nlsr::Nlsr(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam) |
| 43 | : m_face(face) |
| 44 | , m_scheduler(face.getIoService()) |
Laqin Fan | a4cf402 | 2017-01-03 18:57:35 +0000 | [diff] [blame] | 45 | , m_keyChain(keyChain) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 46 | , m_confParam(confParam) |
| 47 | , m_adjacencyList(confParam.getAdjacencyList()) |
| 48 | , m_namePrefixList(confParam.getNamePrefixList()) |
| 49 | , m_validator(m_confParam.getValidator()) |
| 50 | , m_fib(m_face, m_scheduler, m_adjacencyList, m_confParam, m_keyChain) |
| 51 | , m_routingTable(m_scheduler, m_fib, m_lsdb, m_namePrefixTable, m_confParam) |
| 52 | , m_namePrefixTable(m_fib, m_routingTable, m_routingTable.afterRoutingChange) |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 53 | , m_lsdb(m_face, m_keyChain, m_confParam, m_namePrefixTable, m_routingTable) |
| 54 | , m_helloProtocol(m_face, m_keyChain, confParam, m_routingTable, m_lsdb) |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 55 | , m_onNewLsaConnection(m_lsdb.getSync().onNewLsa->connect( |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 56 | [this] (const ndn::Name& updateName, uint64_t sequenceNumber, |
| 57 | const ndn::Name& originRouter) { |
| 58 | registerStrategyForCerts(originRouter); |
| 59 | })) |
| 60 | , m_onPrefixRegistrationSuccess(m_fib.onPrefixRegistrationSuccess.connect( |
| 61 | [this] (const ndn::Name& name) { |
| 62 | m_helloProtocol.sendHelloInterest(name); |
| 63 | })) |
| 64 | , m_onHelloDataValidated(m_helloProtocol.onHelloDataValidated.connect( |
| 65 | [this] (const ndn::Name& neighbor) { |
| 66 | auto it = m_adjacencyList.findAdjacent(neighbor); |
| 67 | if (it != m_adjacencyList.end()) { |
| 68 | m_fib.registerPrefix(m_confParam.getSyncPrefix(), it->getFaceUri(), it->getLinkCost(), |
| 69 | ndn::time::milliseconds::max(), ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
| 70 | } |
| 71 | })) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 72 | , m_dispatcher(m_face, m_keyChain) |
| 73 | , m_datasetHandler(m_dispatcher, m_lsdb, m_routingTable) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 74 | , m_controller(m_face, m_keyChain) |
| 75 | , m_faceDatasetController(m_face, m_keyChain) |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 76 | , m_prefixUpdateProcessor(m_dispatcher, |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 77 | m_confParam.getPrefixUpdateValidator(), |
| 78 | m_namePrefixList, |
| 79 | m_lsdb, |
| 80 | m_confParam.getConfFileNameDynamic()) |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 81 | , m_nfdRibCommandProcessor(m_dispatcher, |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 82 | m_namePrefixList, |
| 83 | m_lsdb) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 84 | , m_statsCollector(m_lsdb, m_helloProtocol) |
| 85 | , m_faceMonitor(m_face) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 86 | { |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 87 | NLSR_LOG_DEBUG("Initializing Nlsr"); |
| 88 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 89 | m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification, this, _1)); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 90 | m_faceMonitor.start(); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 91 | |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 92 | // Do key initialization and registrations first, then initialize faces |
| 93 | // which will create routes to neighbor |
| 94 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 95 | setStrategies(); |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 96 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 97 | NLSR_LOG_DEBUG("Default NLSR identity: " << m_confParam.getSigningInfo().getSignerName()); |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 98 | |
| 99 | // Can be moved to HelloProtocol and Lsdb ctor if initializeKey is set |
| 100 | // earlier in the Nlsr constructor so as to set m_signingInfo |
| 101 | setInfoInterestFilter(); |
| 102 | setLsaInterestFilter(); |
| 103 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 104 | // Add top-level prefixes: router and localhost prefix |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 105 | addDispatcherTopPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr")); |
| 106 | addDispatcherTopPrefix(LOCALHOST_PREFIX); |
| 107 | |
| 108 | enableIncomingFaceIdIndication(); |
| 109 | |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 110 | registerLocalhostPrefix(); |
| 111 | registerRouterPrefix(); |
| 112 | |
| 113 | initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1), |
| 114 | std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0)); |
| 115 | |
| 116 | // Could be moved into ctor, but unit tests need to be modified |
| 117 | initialize(); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 118 | } |
| 119 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 120 | void |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 121 | Nlsr::registerStrategyForCerts(const ndn::Name& originRouter) |
| 122 | { |
| 123 | for (const ndn::Name& router : m_strategySetOnRouters) { |
| 124 | if (router == originRouter) { |
| 125 | // Have already set strategy for this router's certs once |
| 126 | return; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | m_strategySetOnRouters.push_back(originRouter); |
| 131 | |
| 132 | ndn::Name routerKey(originRouter); |
| 133 | routerKey.append("KEY"); |
| 134 | ndn::Name instanceKey(originRouter); |
| 135 | instanceKey.append("nlsr").append("KEY"); |
| 136 | |
| 137 | m_fib.setStrategy(routerKey, Fib::BEST_ROUTE_V2_STRATEGY, 0); |
| 138 | m_fib.setStrategy(instanceKey, Fib::BEST_ROUTE_V2_STRATEGY, 0); |
| 139 | |
| 140 | ndn::Name siteKey; |
| 141 | for (size_t i = 0; i < originRouter.size(); ++i) { |
| 142 | if (originRouter[i].toUri() == "%C1.Router") { |
| 143 | break; |
| 144 | } |
| 145 | siteKey.append(originRouter[i]); |
| 146 | } |
| 147 | ndn::Name opPrefix(siteKey); |
| 148 | siteKey.append("KEY"); |
| 149 | m_fib.setStrategy(siteKey, Fib::BEST_ROUTE_V2_STRATEGY, 0); |
| 150 | |
| 151 | opPrefix.append(std::string("%C1.Operator")); |
| 152 | m_fib.setStrategy(opPrefix, Fib::BEST_ROUTE_V2_STRATEGY, 0); |
| 153 | } |
| 154 | |
| 155 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 156 | Nlsr::registrationFailed(const ndn::Name& name) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 157 | { |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 158 | NLSR_LOG_ERROR("ERROR: Failed to register prefix " << name << " in local hub's daemon"); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 159 | BOOST_THROW_EXCEPTION(Error("Error: Prefix registration failed")); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 160 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 161 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 162 | void |
| 163 | Nlsr::onRegistrationSuccess(const ndn::Name& name) |
| 164 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 165 | NLSR_LOG_DEBUG("Successfully registered prefix: " << name); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 169 | Nlsr::setInfoInterestFilter() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 170 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 171 | ndn::Name name(m_confParam.getRouterPrefix()); |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 172 | name.append("nlsr"); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 173 | name.append("INFO"); |
| 174 | |
| 175 | NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name); |
| 176 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 177 | m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false), |
| 178 | std::bind(&HelloProtocol::processInterest, &m_helloProtocol, _1, _2), |
| 179 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
| 180 | std::bind(&Nlsr::registrationFailed, this, _1), |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 181 | m_confParam.getSigningInfo(), ndn::nfd::ROUTE_FLAG_CAPTURE); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void |
| 185 | Nlsr::setLsaInterestFilter() |
| 186 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 187 | ndn::Name name = m_confParam.getLsaPrefix(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 188 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 189 | NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 190 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 191 | m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false), |
| 192 | std::bind(&Lsdb::processInterest, &m_lsdb, _1, _2), |
| 193 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
| 194 | std::bind(&Nlsr::registrationFailed, this, _1), |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 195 | m_confParam.getSigningInfo(), ndn::nfd::ROUTE_FLAG_CAPTURE); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 196 | } |
| 197 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 198 | void |
| 199 | Nlsr::addDispatcherTopPrefix(const ndn::Name& topPrefix) |
| 200 | { |
| 201 | try { |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 202 | // false since we want to have control over the registration process |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 203 | m_dispatcher.addTopPrefix(topPrefix, false, m_confParam.getSigningInfo()); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 204 | } |
| 205 | catch (const std::exception& e) { |
| 206 | NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n"); |
| 207 | } |
| 208 | } |
| 209 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 210 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 211 | Nlsr::setStrategies() |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 212 | { |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 213 | m_fib.setStrategy(m_confParam.getLsaPrefix(), Fib::MULTICAST_STRATEGY, 0); |
| 214 | m_fib.setStrategy(m_confParam.getSyncPrefix(), Fib::MULTICAST_STRATEGY, 0); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 218 | Nlsr::initialize() |
| 219 | { |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 220 | // Logging start |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 221 | m_adjacencyList.writeLog(); |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 222 | NLSR_LOG_DEBUG(m_namePrefixList); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 223 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 224 | m_lsdb.buildAndInstallOwnNameLsa(); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 225 | |
| 226 | // Install coordinate LSAs if using HR or dry-run HR. |
| 227 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 228 | m_lsdb.buildAndInstallOwnCoordinateLsa(); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 229 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 230 | |
Vince Lehman | 0913112 | 2014-09-09 17:10:11 -0500 | [diff] [blame] | 231 | // Need to set direct neighbors' costs to 0 for hyperbolic routing |
| 232 | if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 233 | for (auto&& neighbor : m_adjacencyList.getAdjList()) { |
| 234 | neighbor.setLinkCost(0); |
Vince Lehman | 0913112 | 2014-09-09 17:10:11 -0500 | [diff] [blame] | 235 | } |
| 236 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 240 | Nlsr::registerLocalhostPrefix() |
| 241 | { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 242 | m_face.registerPrefix(LOCALHOST_PREFIX, |
| 243 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
| 244 | std::bind(&Nlsr::registrationFailed, this, _1)); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 248 | Nlsr::registerRouterPrefix() |
| 249 | { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 250 | m_face.registerPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"), |
| 251 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
| 252 | std::bind(&Nlsr::registrationFailed, this, _1)); |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 256 | Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification) |
akmhoque | e176515 | 2014-06-30 11:32:01 -0500 | [diff] [blame] | 257 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 258 | NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called"); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 259 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 260 | switch (faceEventNotification.getKind()) { |
| 261 | case ndn::nfd::FACE_EVENT_DESTROYED: { |
| 262 | uint64_t faceId = faceEventNotification.getFaceId(); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 263 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 264 | auto adjacent = m_adjacencyList.findAdjacent(faceId); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 265 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 266 | if (adjacent != m_adjacencyList.end()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 267 | NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed"); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 268 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 269 | adjacent->setFaceId(0); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 270 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 271 | // Only trigger an Adjacency LSA build if this node is changing |
| 272 | // from ACTIVE to INACTIVE since this rebuild will effectively |
| 273 | // cancel the previous Adjacency LSA refresh event and schedule |
| 274 | // a new one further in the future. |
| 275 | // |
| 276 | // Continuously scheduling the refresh in the future will block |
| 277 | // the router from refreshing its Adjacency LSA. Since other |
| 278 | // routers' Name prefixes' expiration times are updated when |
| 279 | // this router refreshes its Adjacency LSA, the other routers' |
| 280 | // prefixes will expire and be removed from the RIB. |
| 281 | // |
| 282 | // This check is required to fix Bug #2733 for now. This check |
| 283 | // would be unnecessary to fix Bug #2733 when Issue #2732 is |
| 284 | // completed, but the check also helps with optimization so it |
| 285 | // can remain even when Issue #2732 is implemented. |
| 286 | if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) { |
| 287 | adjacent->setStatus(Adjacent::STATUS_INACTIVE); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 288 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 289 | // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and |
| 290 | // has met the HELLO retry threshold |
| 291 | adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber()); |
Vince Lehman | 199e9cf | 2015-04-07 13:22:16 -0500 | [diff] [blame] | 292 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 293 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 294 | m_routingTable.scheduleRoutingTableCalculation(); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 295 | } |
| 296 | else { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 297 | m_lsdb.scheduleAdjLsaBuild(); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 298 | } |
Nick Gordon | e8e03ac | 2016-07-07 14:24:38 -0500 | [diff] [blame] | 299 | } |
Vince Lehman | 199e9cf | 2015-04-07 13:22:16 -0500 | [diff] [blame] | 300 | } |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 301 | break; |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 302 | } |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 303 | case ndn::nfd::FACE_EVENT_CREATED: { |
| 304 | // Find the neighbor in our adjacency list |
Ashlesh Gawande | 0d2c382 | 2018-01-24 17:17:15 -0600 | [diff] [blame] | 305 | ndn::FaceUri faceUri; |
| 306 | try { |
| 307 | faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri()); |
| 308 | } |
| 309 | catch (const std::exception& e) { |
| 310 | NLSR_LOG_WARN(e.what()); |
| 311 | return; |
| 312 | } |
| 313 | auto adjacent = m_adjacencyList.findAdjacent(faceUri); |
Ashlesh Gawande | 4187857 | 2019-09-29 00:16:02 -0500 | [diff] [blame] | 314 | uint64_t faceId = faceEventNotification.getFaceId(); |
Ashlesh Gawande | 0d2c382 | 2018-01-24 17:17:15 -0600 | [diff] [blame] | 315 | |
Ashlesh Gawande | 4187857 | 2019-09-29 00:16:02 -0500 | [diff] [blame] | 316 | // If we have a neighbor by that FaceUri and it has no FaceId or |
| 317 | // the FaceId is different from ours, we have a match. |
| 318 | if (adjacent != m_adjacencyList.end() && |
| 319 | (adjacent->getFaceId() == 0 || adjacent->getFaceId() != faceId)) |
| 320 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 321 | NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName() |
Ashlesh Gawande | 4187857 | 2019-09-29 00:16:02 -0500 | [diff] [blame] | 322 | << ". New Face ID: " << faceId << ". Registering prefixes."); |
| 323 | adjacent->setFaceId(faceId); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 324 | |
| 325 | registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max()); |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 326 | |
| 327 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 328 | m_routingTable.scheduleRoutingTableCalculation(); |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 329 | } |
| 330 | else { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 331 | m_lsdb.scheduleAdjLsaBuild(); |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 332 | } |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 333 | } |
| 334 | break; |
| 335 | } |
| 336 | default: |
| 337 | break; |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 338 | } |
akmhoque | e176515 | 2014-06-30 11:32:01 -0500 | [diff] [blame] | 339 | } |
| 340 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 341 | void |
| 342 | Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess, |
| 343 | const FetchDatasetTimeoutCallback& onFetchFailure) |
| 344 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 345 | NLSR_LOG_TRACE("Initializing Faces..."); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 346 | |
| 347 | m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure); |
| 348 | |
| 349 | } |
| 350 | |
| 351 | void |
| 352 | Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces) |
| 353 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 354 | NLSR_LOG_DEBUG("Processing face dataset"); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 355 | |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 356 | // Iterate over each neighbor listed in nlsr.conf |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 357 | for (auto&& adjacent : m_adjacencyList.getAdjList()) { |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 358 | |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 359 | const std::string& faceUriString = adjacent.getFaceUri().toString(); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 360 | // Check the list of FaceStatus objects we got for a match |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 361 | for (const auto& faceStatus : faces) { |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 362 | // Set the adjacency FaceID if we find a URI match and it was |
| 363 | // previously unset. Change the boolean to true. |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 364 | if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 365 | NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() << |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 366 | " FaceId: "<< faceStatus.getFaceId()); |
| 367 | adjacent.setFaceId(faceStatus.getFaceId()); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 368 | // Register the prefixes for each neighbor |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 369 | this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max()); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | // If this adjacency has no information in this dataset, then one |
| 373 | // of two things is happening: 1. NFD is starting slowly and this |
| 374 | // Face wasn't ready yet, or 2. NFD is configured |
| 375 | // incorrectly and this Face isn't available. |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 376 | if (adjacent.getFaceId() == 0) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 377 | NLSR_LOG_WARN("The adjacency " << adjacent.getName() << |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 378 | " has no Face information in this dataset."); |
| 379 | } |
| 380 | } |
| 381 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 382 | scheduleDatasetFetch(); |
| 383 | } |
| 384 | |
| 385 | void |
| 386 | Nlsr::registerAdjacencyPrefixes(const Adjacent& adj, |
| 387 | const ndn::time::milliseconds& timeout) |
| 388 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 389 | ndn::FaceUri faceUri = adj.getFaceUri(); |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 390 | double linkCost = adj.getLinkCost(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 391 | const ndn::Name& adjName = adj.getName(); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 392 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 393 | m_fib.registerPrefix(adjName, faceUri, linkCost, |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 394 | timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 395 | |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 396 | m_fib.registerPrefix(m_confParam.getLsaPrefix(), |
| 397 | faceUri, linkCost, timeout, |
| 398 | ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void |
| 402 | Nlsr::onFaceDatasetFetchTimeout(uint32_t code, |
| 403 | const std::string& msg, |
| 404 | uint32_t nRetriesSoFar) |
| 405 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 406 | NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout"); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 407 | // If we have exceeded the maximum attempt count, do not try again. |
| 408 | if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 409 | NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 410 | m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset, |
| 411 | this, _1), |
| 412 | std::bind(&Nlsr::onFaceDatasetFetchTimeout, |
| 413 | this, _1, _2, nRetriesSoFar)); |
| 414 | } |
| 415 | else { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 416 | NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " << |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 417 | m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time."); |
| 418 | // If we fail to fetch it, just do nothing until the next |
| 419 | // interval. Since this is a backup mechanism, we aren't as |
| 420 | // concerned with retrying. |
| 421 | scheduleDatasetFetch(); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | void |
| 426 | Nlsr::scheduleDatasetFetch() |
| 427 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 428 | NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval()); |
| 429 | |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 430 | m_scheduler.schedule(m_confParam.getFaceDatasetFetchInterval(), |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 431 | [this] { |
| 432 | this->initializeFaces( |
| 433 | [this] (const std::vector<ndn::nfd::FaceStatus>& faces) { |
| 434 | this->processFaceDataset(faces); |
| 435 | }, |
| 436 | [this] (uint32_t code, const std::string& msg) { |
| 437 | this->onFaceDatasetFetchTimeout(code, msg, 0); |
| 438 | }); |
| 439 | }); |
| 440 | } |
akmhoque | e176515 | 2014-06-30 11:32:01 -0500 | [diff] [blame] | 441 | |
| 442 | void |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 443 | Nlsr::enableIncomingFaceIdIndication() |
| 444 | { |
| 445 | NLSR_LOG_DEBUG("Enabling incoming face id indication for local face."); |
| 446 | |
| 447 | m_controller.start<ndn::nfd::FaceUpdateCommand>( |
| 448 | ndn::nfd::ControlParameters() |
| 449 | .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true), |
| 450 | bind(&Nlsr::onFaceIdIndicationSuccess, this, _1), |
| 451 | bind(&Nlsr::onFaceIdIndicationFailure, this, _1)); |
| 452 | } |
| 453 | |
| 454 | void |
| 455 | Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp) |
| 456 | { |
| 457 | NLSR_LOG_DEBUG("Successfully enabled incoming face id indication" |
| 458 | << "for face id " << cp.getFaceId()); |
| 459 | } |
| 460 | |
| 461 | void |
| 462 | Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr) |
| 463 | { |
| 464 | std::ostringstream os; |
| 465 | os << "Failed to enable incoming face id indication feature: " << |
| 466 | "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")"; |
| 467 | |
| 468 | NLSR_LOG_DEBUG(os.str()); |
| 469 | } |
| 470 | |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 471 | } // namespace nlsr |