blob: 74d3c119f9ce827b1f88b93b3e0602aae282909e [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/*
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070019 */
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040020
akmhoque53353462014-04-22 08:43:45 -050021#include "routing-table.hpp"
22#include "nlsr.hpp"
23#include "map.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050024#include "conf-parameter.hpp"
akmhoque53353462014-04-22 08:43:45 -050025#include "routing-table-calculator.hpp"
26#include "routing-table-entry.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050027#include "name-prefix-table.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050028#include "logger.hpp"
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070029#include "tlv-nlsr.hpp"
akmhoque53353462014-04-22 08:43:45 -050030
Nick Gordon22b5c952017-08-10 17:48:15 -050031#include <list>
Nick Gordone98480b2017-05-24 11:23:03 -050032#include <string>
Nick Gordon22b5c952017-08-10 17:48:15 -050033
akmhoque53353462014-04-22 08:43:45 -050034namespace nlsr {
35
dmcoomescf8d0ed2017-02-21 11:39:01 -060036INIT_LOGGER(route.RoutingTable);
akmhoque674b0b12014-05-20 14:33:28 -050037
Ashlesh Gawande85998a12017-12-07 22:22:13 -060038RoutingTable::RoutingTable(ndn::Scheduler& scheduler, Fib& fib, Lsdb& lsdb,
39 NamePrefixTable& namePrefixTable, ConfParameter& confParam)
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040040 : afterRoutingChange{std::make_unique<AfterRoutingChange>()}
Nick Gordonb7b58392017-08-17 16:29:21 -050041 , m_scheduler(scheduler)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060042 , m_fib(fib)
43 , m_lsdb(lsdb)
44 , m_namePrefixTable(namePrefixTable)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060045 , m_routingCalcInterval{confParam.getRoutingCalcInterval()}
46 , m_isRoutingTableCalculating(false)
47 , m_isRouteCalculationScheduled(false)
48 , m_confParam(confParam)
Nick Gordonb7b58392017-08-17 16:29:21 -050049{
50}
51
akmhoque53353462014-04-22 08:43:45 -050052void
Ashlesh Gawande85998a12017-12-07 22:22:13 -060053RoutingTable::calculate()
akmhoque53353462014-04-22 08:43:45 -050054{
Ashlesh Gawande57a87172020-05-09 19:47:06 -070055 m_lsdb.writeLog();
Ashlesh Gawande85998a12017-12-07 22:22:13 -060056 m_namePrefixTable.writeLog();
57 if (m_isRoutingTableCalculating == false) {
58 // setting routing table calculation
59 m_isRoutingTableCalculating = true;
Nick Gordone8e03ac2016-07-07 14:24:38 -050060
Ashlesh Gawande85998a12017-12-07 22:22:13 -060061 bool isHrEnabled = m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF;
Nick Gordone8e03ac2016-07-07 14:24:38 -050062
Ashlesh Gawande85998a12017-12-07 22:22:13 -060063 if ((!isHrEnabled &&
Ashlesh Gawande57a87172020-05-09 19:47:06 -070064 m_lsdb.doesLsaExist(m_confParam.getRouterPrefix(), Lsa::Type::ADJACENCY))
Nick Gordone8e03ac2016-07-07 14:24:38 -050065 ||
Ashlesh Gawande85998a12017-12-07 22:22:13 -060066 (isHrEnabled &&
Ashlesh Gawande57a87172020-05-09 19:47:06 -070067 m_lsdb.doesLsaExist(m_confParam.getRouterPrefix(), Lsa::Type::COORDINATE))) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060068 if (m_lsdb.getIsBuildAdjLsaSheduled() != 1) {
dmcoomes5bcb39e2017-10-31 15:07:55 -050069 NLSR_LOG_TRACE("Clearing old routing table");
akmhoque53353462014-04-22 08:43:45 -050070 clearRoutingTable();
akmhoque157b0a42014-05-13 00:26:37 -050071 // for dry run options
72 clearDryRoutingTable();
Vince Lehman50df6b72015-03-03 12:06:40 -060073
dmcoomes5bcb39e2017-10-31 15:07:55 -050074 NLSR_LOG_DEBUG("Calculating routing table");
Vince Lehman50df6b72015-03-03 12:06:40 -060075
akmhoque53353462014-04-22 08:43:45 -050076 // calculate Link State routing
Ashlesh Gawande57a87172020-05-09 19:47:06 -070077 if ((m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) ||
78 (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060079 calculateLsRoutingTable();
akmhoque53353462014-04-22 08:43:45 -050080 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -060081 // calculate hyperbolic routing
82 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
83 calculateHypRoutingTable(false);
akmhoque53353462014-04-22 08:43:45 -050084 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -060085 // calculate dry hyperbolic routing
86 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
87 calculateHypRoutingTable(true);
akmhoque53353462014-04-22 08:43:45 -050088 }
Nick G97e34942016-07-11 14:46:27 -050089 // Inform the NPT that updates have been made
dmcoomes5bcb39e2017-10-31 15:07:55 -050090 NLSR_LOG_DEBUG("Calling Update NPT With new Route");
Nick Gordonb7b58392017-08-17 16:29:21 -050091 (*afterRoutingChange)(m_rTable);
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070092 NLSR_LOG_DEBUG(*this);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060093 m_namePrefixTable.writeLog();
94 m_fib.writeLog();
akmhoque53353462014-04-22 08:43:45 -050095 }
akmhoque157b0a42014-05-13 00:26:37 -050096 else {
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070097 NLSR_LOG_DEBUG("Adjacency building is scheduled, so routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -050098 }
99 }
akmhoque157b0a42014-05-13 00:26:37 -0500100 else {
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700101 NLSR_LOG_DEBUG("No Adj LSA of router itself, so Routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -0500102 clearRoutingTable();
103 clearDryRoutingTable(); // for dry run options
104 // need to update NPT here
dmcoomes5bcb39e2017-10-31 15:07:55 -0500105 NLSR_LOG_DEBUG("Calling Update NPT With new Route");
Nick Gordonb7b58392017-08-17 16:29:21 -0500106 (*afterRoutingChange)(m_rTable);
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700107 NLSR_LOG_DEBUG(*this);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600108 m_namePrefixTable.writeLog();
109 m_fib.writeLog();
110 // debugging purpose end
akmhoque53353462014-04-22 08:43:45 -0500111 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600112 m_isRouteCalculationScheduled = false; // clear scheduled flag
113 m_isRoutingTableCalculating = false; // unsetting routing table calculation
akmhoque53353462014-04-22 08:43:45 -0500114 }
akmhoque157b0a42014-05-13 00:26:37 -0500115 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600116 scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500117 }
118}
119
akmhoque53353462014-04-22 08:43:45 -0500120void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600121RoutingTable::calculateLsRoutingTable()
akmhoque53353462014-04-22 08:43:45 -0500122{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700123 NLSR_LOG_TRACE("CalculateLsRoutingTable Called");
Vince Lehman9a709032014-09-13 16:28:07 -0500124
125 Map map;
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700126 auto lsaRange = m_lsdb.getLsdbIterator<AdjLsa>();
127 map.createFromAdjLsdb(lsaRange.first, lsaRange.second);
Vince Lehman9a709032014-09-13 16:28:07 -0500128 map.writeLog();
129
130 size_t nRouters = map.getMapSize();
131
132 LinkStateRoutingTableCalculator calculator(nRouters);
133
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700134 calculator.calculatePath(map, *this, m_confParam, m_lsdb);
akmhoque53353462014-04-22 08:43:45 -0500135}
136
137void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600138RoutingTable::calculateHypRoutingTable(bool isDryRun)
akmhoque53353462014-04-22 08:43:45 -0500139{
Vince Lehman9a709032014-09-13 16:28:07 -0500140 Map map;
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700141 auto lsaRange = m_lsdb.getLsdbIterator<CoordinateLsa>();
142 map.createFromCoordinateLsdb(lsaRange.first, lsaRange.second);
Vince Lehman9a709032014-09-13 16:28:07 -0500143 map.writeLog();
144
145 size_t nRouters = map.getMapSize();
146
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600147 HyperbolicRoutingCalculator calculator(nRouters, isDryRun, m_confParam.getRouterPrefix());
Vince Lehman9a709032014-09-13 16:28:07 -0500148
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600149 calculator.calculatePath(map, *this, m_lsdb, m_confParam.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500150}
151
152void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600153RoutingTable::scheduleRoutingTableCalculation()
akmhoque53353462014-04-22 08:43:45 -0500154{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400155 if (!m_isRouteCalculationScheduled) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500156 NLSR_LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400157 m_scheduler.schedule(m_routingCalcInterval, [this] { calculate(); });
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600158 m_isRouteCalculationScheduled = true;
akmhoque53353462014-04-22 08:43:45 -0500159 }
160}
161
162static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500163routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500164{
165 return rte.getDestination() == destRouter;
166}
167
akmhoque53353462014-04-22 08:43:45 -0500168void
akmhoque31d1d4b2014-05-05 22:08:14 -0500169RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500170{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500171 NLSR_LOG_DEBUG("Adding " << nh << " for destination: " << destRouter);
Vince Lehman9a709032014-09-13 16:28:07 -0500172
akmhoqueb6450b12014-04-24 00:01:03 -0500173 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400174 if (rteChk == nullptr) {
akmhoque53353462014-04-22 08:43:45 -0500175 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500176 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500177 m_rTable.push_back(rte);
178 }
akmhoque157b0a42014-05-13 00:26:37 -0500179 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500180 rteChk->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500181 }
182}
183
akmhoqueb6450b12014-04-24 00:01:03 -0500184RoutingTableEntry*
akmhoque31d1d4b2014-05-05 22:08:14 -0500185RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500186{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400187 auto it = std::find_if(m_rTable.begin(), m_rTable.end(),
188 std::bind(&routingTableEntryCompare, _1, destRouter));
akmhoque157b0a42014-05-13 00:26:37 -0500189 if (it != m_rTable.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500190 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500191 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400192 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500193}
194
195void
akmhoque31d1d4b2014-05-05 22:08:14 -0500196RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500197{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500198 NLSR_LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
Vince Lehman9a709032014-09-13 16:28:07 -0500199
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400200 auto it = std::find_if(m_dryTable.begin(), m_dryTable.end(),
201 std::bind(&routingTableEntryCompare, _1, destRouter));
akmhoque157b0a42014-05-13 00:26:37 -0500202 if (it == m_dryTable.end()) {
akmhoque53353462014-04-22 08:43:45 -0500203 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500204 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500205 m_dryTable.push_back(rte);
206 }
akmhoque157b0a42014-05-13 00:26:37 -0500207 else {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400208 it->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500209 }
210}
211
212void
akmhoque53353462014-04-22 08:43:45 -0500213RoutingTable::clearRoutingTable()
214{
akmhoque157b0a42014-05-13 00:26:37 -0500215 if (m_rTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500216 m_rTable.clear();
217 }
218}
219
220void
221RoutingTable::clearDryRoutingTable()
222{
akmhoque157b0a42014-05-13 00:26:37 -0500223 if (m_dryTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500224 m_dryTable.clear();
225 }
226}
227
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700228template<ndn::encoding::Tag TAG>
229size_t
230RoutingTableStatus::wireEncode(ndn::EncodingImpl<TAG>& block) const
231{
232 size_t totalLength = 0;
233
234 for (auto it = m_dryTable.rbegin(); it != m_dryTable.rend(); ++it) {
235 totalLength += it->wireEncode(block);
236 }
237
238 for (auto it = m_rTable.rbegin(); it != m_rTable.rend(); ++it) {
239 totalLength += it->wireEncode(block);
240 }
241
242 totalLength += block.prependVarNumber(totalLength);
243 totalLength += block.prependVarNumber(ndn::tlv::nlsr::RoutingTable);
244
245 return totalLength;
246}
247
248NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RoutingTableStatus);
249
250const ndn::Block&
251RoutingTableStatus::wireEncode() const
252{
253 if (m_wire.hasWire()) {
254 return m_wire;
255 }
256
257 ndn::EncodingEstimator estimator;
258 size_t estimatedSize = wireEncode(estimator);
259
260 ndn::EncodingBuffer buffer(estimatedSize, 0);
261 wireEncode(buffer);
262
263 m_wire = buffer.block();
264
265 return m_wire;
266}
267
268void
269RoutingTableStatus::wireDecode(const ndn::Block& wire)
270{
271 m_rTable.clear();
272
273 m_wire = wire;
274
275 if (m_wire.type() != ndn::tlv::nlsr::RoutingTable) {
276 std::stringstream error;
277 error << "Expected RoutingTableStatus Block, but Block is of a different type: #"
278 << m_wire.type();
279 BOOST_THROW_EXCEPTION(Error(error.str()));
280 }
281
282 m_wire.parse();
283
284 auto val = m_wire.elements_begin();
285
286 std::set<ndn::Name> destinations;
287 for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::RoutingTableEntry; ++val) {
288 auto entry = RoutingTableEntry(*val);
289
290 if (destinations.emplace(entry.getDestination()).second) {
291 m_rTable.push_back(entry);
292 }
293 else {
294 // If destination already exists then this is the start of dry HR table
295 m_dryTable.push_back(entry);
296 }
297 }
298
299 if (val != m_wire.elements_end()) {
300 std::stringstream error;
301 error << "Expected the end of elements, but Block is of a different type: #"
302 << val->type();
303 BOOST_THROW_EXCEPTION(Error(error.str()));
304 }
305}
306
307std::ostream&
308operator<<(std::ostream& os, const RoutingTableStatus& rts)
309{
310 os << "Routing Table:\n";
311 for (const auto& rte : rts.getRoutingTableEntry()) {
312 os << rte;
313 }
314
315 if (!rts.getDryRoutingTableEntry().empty()) {
316 os << "Dry-Run Hyperbolic Routing Table:\n";
317 for (const auto& rte : rts.getDryRoutingTableEntry()) {
318 os << rte;
319 }
320 }
321 return os;
322}
323
Nick Gordonfad8e252016-08-11 14:21:38 -0500324} // namespace nlsr