blob: 3fcd654eeeaab14c069233c2e9efc8711df4d561 [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/>.
akmhoque3d06e792014-05-27 16:23:20 -050019 **/
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"
akmhoque53353462014-04-22 08:43:45 -050029
Nick Gordon22b5c952017-08-10 17:48:15 -050030#include <list>
Nick Gordone98480b2017-05-24 11:23:03 -050031#include <string>
Nick Gordon22b5c952017-08-10 17:48:15 -050032
akmhoque53353462014-04-22 08:43:45 -050033namespace nlsr {
34
dmcoomescf8d0ed2017-02-21 11:39:01 -060035INIT_LOGGER(route.RoutingTable);
akmhoque674b0b12014-05-20 14:33:28 -050036
Ashlesh Gawande85998a12017-12-07 22:22:13 -060037RoutingTable::RoutingTable(ndn::Scheduler& scheduler, Fib& fib, Lsdb& lsdb,
38 NamePrefixTable& namePrefixTable, ConfParameter& confParam)
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040039 : afterRoutingChange{std::make_unique<AfterRoutingChange>()}
Nick Gordonb7b58392017-08-17 16:29:21 -050040 , m_scheduler(scheduler)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060041 , m_fib(fib)
42 , m_lsdb(lsdb)
43 , m_namePrefixTable(namePrefixTable)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060044 , m_routingCalcInterval{confParam.getRoutingCalcInterval()}
45 , m_isRoutingTableCalculating(false)
46 , m_isRouteCalculationScheduled(false)
47 , m_confParam(confParam)
Nick Gordonb7b58392017-08-17 16:29:21 -050048{
49}
50
akmhoque53353462014-04-22 08:43:45 -050051void
Ashlesh Gawande85998a12017-12-07 22:22:13 -060052RoutingTable::calculate()
akmhoque53353462014-04-22 08:43:45 -050053{
Ashlesh Gawande85998a12017-12-07 22:22:13 -060054 m_lsdb.writeCorLsdbLog();
55 m_lsdb.writeNameLsdbLog();
56 m_lsdb.writeAdjLsdbLog();
57 m_namePrefixTable.writeLog();
58 if (m_isRoutingTableCalculating == false) {
59 // setting routing table calculation
60 m_isRoutingTableCalculating = true;
Nick Gordone8e03ac2016-07-07 14:24:38 -050061
Ashlesh Gawande85998a12017-12-07 22:22:13 -060062 bool isHrEnabled = m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF;
Nick Gordone8e03ac2016-07-07 14:24:38 -050063
Ashlesh Gawande85998a12017-12-07 22:22:13 -060064 if ((!isHrEnabled &&
65 m_lsdb
66 .doesLsaExist(ndn::Name{m_confParam.getRouterPrefix()}
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080067 .append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY)), Lsa::Type::ADJACENCY))
Nick Gordone8e03ac2016-07-07 14:24:38 -050068 ||
Ashlesh Gawande85998a12017-12-07 22:22:13 -060069 (isHrEnabled &&
70 m_lsdb
71 .doesLsaExist(ndn::Name{m_confParam.getRouterPrefix()}
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080072 .append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE)), Lsa::Type::COORDINATE))) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060073 if (m_lsdb.getIsBuildAdjLsaSheduled() != 1) {
dmcoomes5bcb39e2017-10-31 15:07:55 -050074 NLSR_LOG_TRACE("Clearing old routing table");
akmhoque53353462014-04-22 08:43:45 -050075 clearRoutingTable();
akmhoque157b0a42014-05-13 00:26:37 -050076 // for dry run options
77 clearDryRoutingTable();
Vince Lehman50df6b72015-03-03 12:06:40 -060078
dmcoomes5bcb39e2017-10-31 15:07:55 -050079 NLSR_LOG_DEBUG("Calculating routing table");
Vince Lehman50df6b72015-03-03 12:06:40 -060080
akmhoque53353462014-04-22 08:43:45 -050081 // calculate Link State routing
Ashlesh Gawande85998a12017-12-07 22:22:13 -060082 if ((m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF)
83 || (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
84 calculateLsRoutingTable();
akmhoque53353462014-04-22 08:43:45 -050085 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -060086 // calculate hyperbolic routing
87 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
88 calculateHypRoutingTable(false);
akmhoque53353462014-04-22 08:43:45 -050089 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -060090 // calculate dry hyperbolic routing
91 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
92 calculateHypRoutingTable(true);
akmhoque53353462014-04-22 08:43:45 -050093 }
Nick G97e34942016-07-11 14:46:27 -050094 // Inform the NPT that updates have been made
dmcoomes5bcb39e2017-10-31 15:07:55 -050095 NLSR_LOG_DEBUG("Calling Update NPT With new Route");
Nick Gordonb7b58392017-08-17 16:29:21 -050096 (*afterRoutingChange)(m_rTable);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060097 writeLog();
98 m_namePrefixTable.writeLog();
99 m_fib.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500100 }
akmhoque157b0a42014-05-13 00:26:37 -0500101 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500102 NLSR_LOG_DEBUG("Adjacency building is scheduled, so"
akmhoque674b0b12014-05-20 14:33:28 -0500103 " routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -0500104 }
105 }
akmhoque157b0a42014-05-13 00:26:37 -0500106 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500107 NLSR_LOG_DEBUG("No Adj LSA of router itself,"
akmhoque674b0b12014-05-20 14:33:28 -0500108 " so Routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -0500109 clearRoutingTable();
110 clearDryRoutingTable(); // for dry run options
111 // need to update NPT here
dmcoomes5bcb39e2017-10-31 15:07:55 -0500112 NLSR_LOG_DEBUG("Calling Update NPT With new Route");
Nick Gordonb7b58392017-08-17 16:29:21 -0500113 (*afterRoutingChange)(m_rTable);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600114 writeLog();
115 m_namePrefixTable.writeLog();
116 m_fib.writeLog();
117 // debugging purpose end
akmhoque53353462014-04-22 08:43:45 -0500118 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600119 m_isRouteCalculationScheduled = false; // clear scheduled flag
120 m_isRoutingTableCalculating = false; // unsetting routing table calculation
akmhoque53353462014-04-22 08:43:45 -0500121 }
akmhoque157b0a42014-05-13 00:26:37 -0500122 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600123 scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500124 }
125}
126
akmhoque53353462014-04-22 08:43:45 -0500127void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600128RoutingTable::calculateLsRoutingTable()
akmhoque53353462014-04-22 08:43:45 -0500129{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500130 NLSR_LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called");
Vince Lehman9a709032014-09-13 16:28:07 -0500131
132 Map map;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600133 map.createFromAdjLsdb(m_lsdb.getAdjLsdb().begin(), m_lsdb.getAdjLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500134 map.writeLog();
135
136 size_t nRouters = map.getMapSize();
137
138 LinkStateRoutingTableCalculator calculator(nRouters);
139
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600140 calculator.calculatePath(map, *this, m_confParam, m_lsdb.getAdjLsdb());
akmhoque53353462014-04-22 08:43:45 -0500141}
142
143void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600144RoutingTable::calculateHypRoutingTable(bool isDryRun)
akmhoque53353462014-04-22 08:43:45 -0500145{
Vince Lehman9a709032014-09-13 16:28:07 -0500146 Map map;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600147 map.createFromCoordinateLsdb(m_lsdb.getCoordinateLsdb().begin(),
148 m_lsdb.getCoordinateLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500149 map.writeLog();
150
151 size_t nRouters = map.getMapSize();
152
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600153 HyperbolicRoutingCalculator calculator(nRouters, isDryRun, m_confParam.getRouterPrefix());
Vince Lehman9a709032014-09-13 16:28:07 -0500154
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600155 calculator.calculatePath(map, *this, m_lsdb, m_confParam.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500156}
157
158void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600159RoutingTable::scheduleRoutingTableCalculation()
akmhoque53353462014-04-22 08:43:45 -0500160{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400161 if (!m_isRouteCalculationScheduled) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500162 NLSR_LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400163 m_scheduler.schedule(m_routingCalcInterval, [this] { calculate(); });
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600164 m_isRouteCalculationScheduled = true;
akmhoque53353462014-04-22 08:43:45 -0500165 }
166}
167
168static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500169routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500170{
171 return rte.getDestination() == destRouter;
172}
173
akmhoque53353462014-04-22 08:43:45 -0500174void
akmhoque31d1d4b2014-05-05 22:08:14 -0500175RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500176{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500177 NLSR_LOG_DEBUG("Adding " << nh << " for destination: " << destRouter);
Vince Lehman9a709032014-09-13 16:28:07 -0500178
akmhoqueb6450b12014-04-24 00:01:03 -0500179 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400180 if (rteChk == nullptr) {
akmhoque53353462014-04-22 08:43:45 -0500181 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500182 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500183 m_rTable.push_back(rte);
184 }
akmhoque157b0a42014-05-13 00:26:37 -0500185 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500186 rteChk->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500187 }
188}
189
akmhoqueb6450b12014-04-24 00:01:03 -0500190RoutingTableEntry*
akmhoque31d1d4b2014-05-05 22:08:14 -0500191RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500192{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400193 auto it = std::find_if(m_rTable.begin(), m_rTable.end(),
194 std::bind(&routingTableEntryCompare, _1, destRouter));
akmhoque157b0a42014-05-13 00:26:37 -0500195 if (it != m_rTable.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500196 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500197 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400198 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500199}
200
201void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600202RoutingTable::writeLog()
akmhoque674b0b12014-05-20 14:33:28 -0500203{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500204 NLSR_LOG_DEBUG("---------------Routing Table------------------");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600205 for (const auto& rte : m_rTable) {
206 NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500207 NLSR_LOG_DEBUG("Nexthops: ");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600208 rte.getNexthopList().writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500209 }
akmhoquedcee9362014-08-05 22:58:01 -0500210
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600211 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500212 NLSR_LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600213 for (const auto& rte : m_dryTable) {
214 NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500215 NLSR_LOG_DEBUG("Nexthops: ");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600216 rte.getNexthopList().writeLog();
akmhoquedcee9362014-08-05 22:58:01 -0500217 }
218 }
akmhoque674b0b12014-05-20 14:33:28 -0500219}
220
akmhoque53353462014-04-22 08:43:45 -0500221void
akmhoque31d1d4b2014-05-05 22:08:14 -0500222RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500223{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500224 NLSR_LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
Vince Lehman9a709032014-09-13 16:28:07 -0500225
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400226 auto it = std::find_if(m_dryTable.begin(), m_dryTable.end(),
227 std::bind(&routingTableEntryCompare, _1, destRouter));
akmhoque157b0a42014-05-13 00:26:37 -0500228 if (it == m_dryTable.end()) {
akmhoque53353462014-04-22 08:43:45 -0500229 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500230 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500231 m_dryTable.push_back(rte);
232 }
akmhoque157b0a42014-05-13 00:26:37 -0500233 else {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400234 it->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500235 }
236}
237
238void
akmhoque53353462014-04-22 08:43:45 -0500239RoutingTable::clearRoutingTable()
240{
akmhoque157b0a42014-05-13 00:26:37 -0500241 if (m_rTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500242 m_rTable.clear();
243 }
244}
245
246void
247RoutingTable::clearDryRoutingTable()
248{
akmhoque157b0a42014-05-13 00:26:37 -0500249 if (m_dryTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500250 m_dryTable.clear();
251 }
252}
253
Nick Gordonfad8e252016-08-11 14:21:38 -0500254} // namespace nlsr