blob: 4e2fa86f83972111f155bdd1f1636e2329cc0648 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Saurab Dulal72b2b252019-01-22 16:58:08 -06003 * Copyright (c) 2014-2019, 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 <iostream>
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)
Nick Gordonb7b58392017-08-17 16:29:21 -050045 , m_NO_NEXT_HOP{-12345}
Ashlesh Gawande85998a12017-12-07 22:22:13 -060046 , m_routingCalcInterval{confParam.getRoutingCalcInterval()}
47 , m_isRoutingTableCalculating(false)
48 , m_isRouteCalculationScheduled(false)
49 , m_confParam(confParam)
Nick Gordonb7b58392017-08-17 16:29:21 -050050{
51}
52
akmhoque53353462014-04-22 08:43:45 -050053void
Ashlesh Gawande85998a12017-12-07 22:22:13 -060054RoutingTable::calculate()
akmhoque53353462014-04-22 08:43:45 -050055{
Ashlesh Gawande85998a12017-12-07 22:22:13 -060056 m_lsdb.writeCorLsdbLog();
57 m_lsdb.writeNameLsdbLog();
58 m_lsdb.writeAdjLsdbLog();
59 m_namePrefixTable.writeLog();
60 if (m_isRoutingTableCalculating == false) {
61 // setting routing table calculation
62 m_isRoutingTableCalculating = true;
Nick Gordone8e03ac2016-07-07 14:24:38 -050063
Ashlesh Gawande85998a12017-12-07 22:22:13 -060064 bool isHrEnabled = m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF;
Nick Gordone8e03ac2016-07-07 14:24:38 -050065
Ashlesh Gawande85998a12017-12-07 22:22:13 -060066 if ((!isHrEnabled &&
67 m_lsdb
68 .doesLsaExist(ndn::Name{m_confParam.getRouterPrefix()}
Nick Gordon727d4832017-10-13 18:04:25 -050069 .append(std::to_string(Lsa::Type::ADJACENCY)), Lsa::Type::ADJACENCY))
Nick Gordone8e03ac2016-07-07 14:24:38 -050070 ||
Ashlesh Gawande85998a12017-12-07 22:22:13 -060071 (isHrEnabled &&
72 m_lsdb
73 .doesLsaExist(ndn::Name{m_confParam.getRouterPrefix()}
Nick Gordon727d4832017-10-13 18:04:25 -050074 .append(std::to_string(Lsa::Type::COORDINATE)), Lsa::Type::COORDINATE))) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060075 if (m_lsdb.getIsBuildAdjLsaSheduled() != 1) {
dmcoomes5bcb39e2017-10-31 15:07:55 -050076 NLSR_LOG_TRACE("Clearing old routing table");
akmhoque53353462014-04-22 08:43:45 -050077 clearRoutingTable();
akmhoque157b0a42014-05-13 00:26:37 -050078 // for dry run options
79 clearDryRoutingTable();
Vince Lehman50df6b72015-03-03 12:06:40 -060080
dmcoomes5bcb39e2017-10-31 15:07:55 -050081 NLSR_LOG_DEBUG("Calculating routing table");
Vince Lehman50df6b72015-03-03 12:06:40 -060082
akmhoque53353462014-04-22 08:43:45 -050083 // calculate Link State routing
Ashlesh Gawande85998a12017-12-07 22:22:13 -060084 if ((m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF)
85 || (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
86 calculateLsRoutingTable();
akmhoque53353462014-04-22 08:43:45 -050087 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -060088 // calculate hyperbolic routing
89 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
90 calculateHypRoutingTable(false);
akmhoque53353462014-04-22 08:43:45 -050091 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -060092 // calculate dry hyperbolic routing
93 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
94 calculateHypRoutingTable(true);
akmhoque53353462014-04-22 08:43:45 -050095 }
Nick G97e34942016-07-11 14:46:27 -050096 // Inform the NPT that updates have been made
dmcoomes5bcb39e2017-10-31 15:07:55 -050097 NLSR_LOG_DEBUG("Calling Update NPT With new Route");
Nick Gordonb7b58392017-08-17 16:29:21 -050098 (*afterRoutingChange)(m_rTable);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060099 writeLog();
100 m_namePrefixTable.writeLog();
101 m_fib.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500102 }
akmhoque157b0a42014-05-13 00:26:37 -0500103 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500104 NLSR_LOG_DEBUG("Adjacency building is scheduled, so"
akmhoque674b0b12014-05-20 14:33:28 -0500105 " routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -0500106 }
107 }
akmhoque157b0a42014-05-13 00:26:37 -0500108 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500109 NLSR_LOG_DEBUG("No Adj LSA of router itself,"
akmhoque674b0b12014-05-20 14:33:28 -0500110 " so Routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -0500111 clearRoutingTable();
112 clearDryRoutingTable(); // for dry run options
113 // need to update NPT here
dmcoomes5bcb39e2017-10-31 15:07:55 -0500114 NLSR_LOG_DEBUG("Calling Update NPT With new Route");
Nick Gordonb7b58392017-08-17 16:29:21 -0500115 (*afterRoutingChange)(m_rTable);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600116 writeLog();
117 m_namePrefixTable.writeLog();
118 m_fib.writeLog();
119 // debugging purpose end
akmhoque53353462014-04-22 08:43:45 -0500120 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600121 m_isRouteCalculationScheduled = false; // clear scheduled flag
122 m_isRoutingTableCalculating = false; // unsetting routing table calculation
akmhoque53353462014-04-22 08:43:45 -0500123 }
akmhoque157b0a42014-05-13 00:26:37 -0500124 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600125 scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500126 }
127}
128
akmhoque53353462014-04-22 08:43:45 -0500129void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600130RoutingTable::calculateLsRoutingTable()
akmhoque53353462014-04-22 08:43:45 -0500131{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500132 NLSR_LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called");
Vince Lehman9a709032014-09-13 16:28:07 -0500133
134 Map map;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600135 map.createFromAdjLsdb(m_lsdb.getAdjLsdb().begin(), m_lsdb.getAdjLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500136 map.writeLog();
137
138 size_t nRouters = map.getMapSize();
139
140 LinkStateRoutingTableCalculator calculator(nRouters);
141
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600142 calculator.calculatePath(map, *this, m_confParam, m_lsdb.getAdjLsdb());
akmhoque53353462014-04-22 08:43:45 -0500143}
144
145void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600146RoutingTable::calculateHypRoutingTable(bool isDryRun)
akmhoque53353462014-04-22 08:43:45 -0500147{
Vince Lehman9a709032014-09-13 16:28:07 -0500148 Map map;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600149 map.createFromCoordinateLsdb(m_lsdb.getCoordinateLsdb().begin(),
150 m_lsdb.getCoordinateLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500151 map.writeLog();
152
153 size_t nRouters = map.getMapSize();
154
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600155 HyperbolicRoutingCalculator calculator(nRouters, isDryRun, m_confParam.getRouterPrefix());
Vince Lehman9a709032014-09-13 16:28:07 -0500156
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600157 calculator.calculatePath(map, *this, m_lsdb, m_confParam.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500158}
159
160void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600161RoutingTable::scheduleRoutingTableCalculation()
akmhoque53353462014-04-22 08:43:45 -0500162{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600163 if (m_isRouteCalculationScheduled != true) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500164 NLSR_LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
Vince Lehman50df6b72015-03-03 12:06:40 -0600165
Vince Lehman7b616582014-10-17 16:25:39 -0500166 m_scheduler.scheduleEvent(m_routingCalcInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600167 std::bind(&RoutingTable::calculate, this));
Vince Lehman7c603292014-09-11 17:48:16 -0500168
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600169 m_isRouteCalculationScheduled = true;
akmhoque53353462014-04-22 08:43:45 -0500170 }
171}
172
173static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500174routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500175{
176 return rte.getDestination() == destRouter;
177}
178
akmhoque53353462014-04-22 08:43:45 -0500179void
akmhoque31d1d4b2014-05-05 22:08:14 -0500180RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500181{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500182 NLSR_LOG_DEBUG("Adding " << nh << " for destination: " << destRouter);
Vince Lehman9a709032014-09-13 16:28:07 -0500183
akmhoqueb6450b12014-04-24 00:01:03 -0500184 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500185 if (rteChk == 0) {
akmhoque53353462014-04-22 08:43:45 -0500186 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500187 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500188 m_rTable.push_back(rte);
189 }
akmhoque157b0a42014-05-13 00:26:37 -0500190 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500191 rteChk->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500192 }
193}
194
akmhoqueb6450b12014-04-24 00:01:03 -0500195RoutingTableEntry*
akmhoque31d1d4b2014-05-05 22:08:14 -0500196RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500197{
198 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
199 m_rTable.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600200 std::bind(&routingTableEntryCompare,
akmhoque157b0a42014-05-13 00:26:37 -0500201 _1, destRouter));
202 if (it != m_rTable.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500203 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500204 }
akmhoqueb6450b12014-04-24 00:01:03 -0500205 return 0;
akmhoque53353462014-04-22 08:43:45 -0500206}
207
208void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600209RoutingTable::writeLog()
akmhoque674b0b12014-05-20 14:33:28 -0500210{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500211 NLSR_LOG_DEBUG("---------------Routing Table------------------");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600212 for (const auto& rte : m_rTable) {
213 NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500214 NLSR_LOG_DEBUG("Nexthops: ");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600215 rte.getNexthopList().writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500216 }
akmhoquedcee9362014-08-05 22:58:01 -0500217
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600218 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500219 NLSR_LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600220 for (const auto& rte : m_dryTable) {
221 NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500222 NLSR_LOG_DEBUG("Nexthops: ");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600223 rte.getNexthopList().writeLog();
akmhoquedcee9362014-08-05 22:58:01 -0500224 }
225 }
akmhoque674b0b12014-05-20 14:33:28 -0500226}
227
akmhoque53353462014-04-22 08:43:45 -0500228void
akmhoque31d1d4b2014-05-05 22:08:14 -0500229RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500230{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500231 NLSR_LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
Vince Lehman9a709032014-09-13 16:28:07 -0500232
akmhoque53353462014-04-22 08:43:45 -0500233 std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
234 m_dryTable.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600235 std::bind(&routingTableEntryCompare,
akmhoque157b0a42014-05-13 00:26:37 -0500236 _1, destRouter));
237 if (it == m_dryTable.end()) {
akmhoque53353462014-04-22 08:43:45 -0500238 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500239 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500240 m_dryTable.push_back(rte);
241 }
akmhoque157b0a42014-05-13 00:26:37 -0500242 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500243 (*it).getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500244 }
245}
246
247void
akmhoque53353462014-04-22 08:43:45 -0500248RoutingTable::clearRoutingTable()
249{
akmhoque157b0a42014-05-13 00:26:37 -0500250 if (m_rTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500251 m_rTable.clear();
252 }
253}
254
255void
256RoutingTable::clearDryRoutingTable()
257{
akmhoque157b0a42014-05-13 00:26:37 -0500258 if (m_dryTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500259 m_dryTable.clear();
260 }
261}
262
Nick Gordonfad8e252016-08-11 14:21:38 -0500263} // namespace nlsr