blob: 9c4ea7722fbc7bfaa09d7483a0af5508ead725c3 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
dmcoomescf8d0ed2017-02-21 11:39:01 -06003 * Copyright (c) 2014-2018, 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
Nick Gordonb7b58392017-08-17 16:29:21 -050038RoutingTable::RoutingTable(ndn::Scheduler& scheduler)
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040039 : afterRoutingChange{std::make_unique<AfterRoutingChange>()}
Nick Gordonb7b58392017-08-17 16:29:21 -050040 , m_scheduler(scheduler)
41 , m_NO_NEXT_HOP{-12345}
42 , m_routingCalcInterval{static_cast<uint32_t>(ROUTING_CALC_INTERVAL_DEFAULT)}
43{
44}
45
akmhoque53353462014-04-22 08:43:45 -050046void
47RoutingTable::calculate(Nlsr& pnlsr)
48{
akmhoque2f423352014-06-03 11:49:35 -050049 pnlsr.getLsdb().writeCorLsdbLog();
50 pnlsr.getLsdb().writeNameLsdbLog();
51 pnlsr.getLsdb().writeAdjLsdbLog();
akmhoque674b0b12014-05-20 14:33:28 -050052 pnlsr.getNamePrefixTable().writeLog();
akmhoque157b0a42014-05-13 00:26:37 -050053 if (pnlsr.getIsRoutingTableCalculating() == false) {
54 //setting routing table calculation
55 pnlsr.setIsRoutingTableCalculating(true);
Nick Gordone8e03ac2016-07-07 14:24:38 -050056
57 bool isHrEnabled = pnlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF;
58
59 if ((!isHrEnabled
60 &&
61 pnlsr.getLsdb()
Nick Gordon727d4832017-10-13 18:04:25 -050062 .doesLsaExist(ndn::Name{pnlsr.getConfParameter().getRouterPrefix()}
63 .append(std::to_string(Lsa::Type::ADJACENCY)), Lsa::Type::ADJACENCY))
Nick Gordone8e03ac2016-07-07 14:24:38 -050064 ||
65 (isHrEnabled
66 &&
67 pnlsr.getLsdb()
Nick Gordon727d4832017-10-13 18:04:25 -050068 .doesLsaExist(ndn::Name{pnlsr.getConfParameter().getRouterPrefix()}
69 .append(std::to_string(Lsa::Type::COORDINATE)), Lsa::Type::COORDINATE))) {
akmhoque157b0a42014-05-13 00:26:37 -050070 if (pnlsr.getIsBuildAdjLsaSheduled() != 1) {
dmcoomes5bcb39e2017-10-31 15:07:55 -050071 NLSR_LOG_TRACE("Clearing old routing table");
akmhoque53353462014-04-22 08:43:45 -050072 clearRoutingTable();
akmhoque157b0a42014-05-13 00:26:37 -050073 // for dry run options
74 clearDryRoutingTable();
Vince Lehman50df6b72015-03-03 12:06:40 -060075
dmcoomes5bcb39e2017-10-31 15:07:55 -050076 NLSR_LOG_DEBUG("Calculating routing table");
Vince Lehman50df6b72015-03-03 12:06:40 -060077
akmhoque53353462014-04-22 08:43:45 -050078 // calculate Link State routing
akmhoque157b0a42014-05-13 00:26:37 -050079 if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF)
80 || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
akmhoque53353462014-04-22 08:43:45 -050081 calculateLsRoutingTable(pnlsr);
82 }
83 //calculate hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050084 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque53353462014-04-22 08:43:45 -050085 calculateHypRoutingTable(pnlsr);
86 }
87 //calculate dry hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050088 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
akmhoque53353462014-04-22 08:43:45 -050089 calculateHypDryRoutingTable(pnlsr);
90 }
Nick G97e34942016-07-11 14:46:27 -050091 // Inform the NPT that updates have been made
dmcoomes5bcb39e2017-10-31 15:07:55 -050092 NLSR_LOG_DEBUG("Calling Update NPT With new Route");
Nick Gordonb7b58392017-08-17 16:29:21 -050093 (*afterRoutingChange)(m_rTable);
akmhoquedcee9362014-08-05 22:58:01 -050094 writeLog(pnlsr.getConfParameter().getHyperbolicState());
akmhoque674b0b12014-05-20 14:33:28 -050095 pnlsr.getNamePrefixTable().writeLog();
96 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -050097 }
akmhoque157b0a42014-05-13 00:26:37 -050098 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -050099 NLSR_LOG_DEBUG("Adjacency building is scheduled, so"
akmhoque674b0b12014-05-20 14:33:28 -0500100 " routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -0500101 }
102 }
akmhoque157b0a42014-05-13 00:26:37 -0500103 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500104 NLSR_LOG_DEBUG("No Adj LSA of router itself,"
akmhoque674b0b12014-05-20 14:33:28 -0500105 " so Routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -0500106 clearRoutingTable();
107 clearDryRoutingTable(); // for dry run options
108 // need to update NPT here
dmcoomes5bcb39e2017-10-31 15:07:55 -0500109 NLSR_LOG_DEBUG("Calling Update NPT With new Route");
Nick Gordonb7b58392017-08-17 16:29:21 -0500110 (*afterRoutingChange)(m_rTable);
akmhoquedcee9362014-08-05 22:58:01 -0500111 writeLog(pnlsr.getConfParameter().getHyperbolicState());
akmhoque674b0b12014-05-20 14:33:28 -0500112 pnlsr.getNamePrefixTable().writeLog();
113 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -0500114 //debugging purpose end
115 }
akmhoque157b0a42014-05-13 00:26:37 -0500116 pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag
117 pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation
akmhoque53353462014-04-22 08:43:45 -0500118 }
akmhoque157b0a42014-05-13 00:26:37 -0500119 else {
akmhoque53353462014-04-22 08:43:45 -0500120 scheduleRoutingTableCalculation(pnlsr);
121 }
122}
123
akmhoque53353462014-04-22 08:43:45 -0500124void
Vince Lehman9a709032014-09-13 16:28:07 -0500125RoutingTable::calculateLsRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500126{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500127 NLSR_LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called");
Vince Lehman9a709032014-09-13 16:28:07 -0500128
129 Map map;
Nick Gordon22b5c952017-08-10 17:48:15 -0500130 map.createFromAdjLsdb(nlsr.getLsdb().getAdjLsdb().begin(), nlsr.getLsdb().getAdjLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500131 map.writeLog();
132
133 size_t nRouters = map.getMapSize();
134
135 LinkStateRoutingTableCalculator calculator(nRouters);
136
dmcoomes9f936662017-03-02 10:33:09 -0600137 calculator.calculatePath(map, std::ref(*this), nlsr);
akmhoque53353462014-04-22 08:43:45 -0500138}
139
140void
Vince Lehman9a709032014-09-13 16:28:07 -0500141RoutingTable::calculateHypRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500142{
Vince Lehman9a709032014-09-13 16:28:07 -0500143 Map map;
Nick Gordon22b5c952017-08-10 17:48:15 -0500144 map.createFromCoordinateLsdb(nlsr.getLsdb().getCoordinateLsdb().begin(),
145 nlsr.getLsdb().getCoordinateLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500146 map.writeLog();
147
148 size_t nRouters = map.getMapSize();
149
150 HyperbolicRoutingCalculator calculator(nRouters, false,
151 nlsr.getConfParameter().getRouterPrefix());
152
dmcoomes9f936662017-03-02 10:33:09 -0600153 calculator.calculatePaths(map, std::ref(*this),
Vince Lehman9a709032014-09-13 16:28:07 -0500154 nlsr.getLsdb(), nlsr.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500155}
156
157void
Vince Lehman9a709032014-09-13 16:28:07 -0500158RoutingTable::calculateHypDryRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500159{
Vince Lehman9a709032014-09-13 16:28:07 -0500160 Map map;
Nick Gordon22b5c952017-08-10 17:48:15 -0500161 map.createFromAdjLsdb(nlsr.getLsdb().getAdjLsdb().begin(), nlsr.getLsdb().getAdjLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500162 map.writeLog();
163
164 size_t nRouters = map.getMapSize();
165
166 HyperbolicRoutingCalculator calculator(nRouters, true,
167 nlsr.getConfParameter().getRouterPrefix());
168
dmcoomes9f936662017-03-02 10:33:09 -0600169 calculator.calculatePaths(map, std::ref(*this),
Vince Lehman9a709032014-09-13 16:28:07 -0500170 nlsr.getLsdb(), nlsr.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500171}
172
173void
174RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
175{
akmhoque157b0a42014-05-13 00:26:37 -0500176 if (pnlsr.getIsRouteCalculationScheduled() != true) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500177 NLSR_LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
Vince Lehman50df6b72015-03-03 12:06:40 -0600178
Vince Lehman7b616582014-10-17 16:25:39 -0500179 m_scheduler.scheduleEvent(m_routingCalcInterval,
dmcoomes9f936662017-03-02 10:33:09 -0600180 std::bind(&RoutingTable::calculate, this, std::ref(pnlsr)));
Vince Lehman7c603292014-09-11 17:48:16 -0500181
akmhoque157b0a42014-05-13 00:26:37 -0500182 pnlsr.setIsRouteCalculationScheduled(true);
akmhoque53353462014-04-22 08:43:45 -0500183 }
184}
185
186static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500187routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500188{
189 return rte.getDestination() == destRouter;
190}
191
akmhoque53353462014-04-22 08:43:45 -0500192void
akmhoque31d1d4b2014-05-05 22:08:14 -0500193RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500194{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500195 NLSR_LOG_DEBUG("Adding " << nh << " for destination: " << destRouter);
Vince Lehman9a709032014-09-13 16:28:07 -0500196
akmhoqueb6450b12014-04-24 00:01:03 -0500197 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500198 if (rteChk == 0) {
akmhoque53353462014-04-22 08:43:45 -0500199 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500200 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500201 m_rTable.push_back(rte);
202 }
akmhoque157b0a42014-05-13 00:26:37 -0500203 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500204 rteChk->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500205 }
206}
207
akmhoqueb6450b12014-04-24 00:01:03 -0500208RoutingTableEntry*
akmhoque31d1d4b2014-05-05 22:08:14 -0500209RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500210{
211 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
212 m_rTable.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600213 std::bind(&routingTableEntryCompare,
akmhoque157b0a42014-05-13 00:26:37 -0500214 _1, destRouter));
215 if (it != m_rTable.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500216 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500217 }
akmhoqueb6450b12014-04-24 00:01:03 -0500218 return 0;
akmhoque53353462014-04-22 08:43:45 -0500219}
220
221void
akmhoquedcee9362014-08-05 22:58:01 -0500222RoutingTable::writeLog(int hyperbolicState)
akmhoque674b0b12014-05-20 14:33:28 -0500223{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500224 NLSR_LOG_DEBUG("---------------Routing Table------------------");
akmhoque674b0b12014-05-20 14:33:28 -0500225 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
226 it != m_rTable.end(); ++it) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500227 NLSR_LOG_DEBUG("Destination: " << (*it).getDestination());
228 NLSR_LOG_DEBUG("Nexthops: ");
akmhoque674b0b12014-05-20 14:33:28 -0500229 (*it).getNexthopList().writeLog();
230 }
akmhoquedcee9362014-08-05 22:58:01 -0500231
232 if (hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500233 NLSR_LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
akmhoquedcee9362014-08-05 22:58:01 -0500234 for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ;
235 it != m_dryTable.end(); ++it) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500236 NLSR_LOG_DEBUG("Destination: " << (*it).getDestination());
237 NLSR_LOG_DEBUG("Nexthops: ");
akmhoquedcee9362014-08-05 22:58:01 -0500238 (*it).getNexthopList().writeLog();
239 }
240 }
akmhoque674b0b12014-05-20 14:33:28 -0500241}
242
akmhoque53353462014-04-22 08:43:45 -0500243void
akmhoque31d1d4b2014-05-05 22:08:14 -0500244RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500245{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500246 NLSR_LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
Vince Lehman9a709032014-09-13 16:28:07 -0500247
akmhoque53353462014-04-22 08:43:45 -0500248 std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
249 m_dryTable.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600250 std::bind(&routingTableEntryCompare,
akmhoque157b0a42014-05-13 00:26:37 -0500251 _1, destRouter));
252 if (it == m_dryTable.end()) {
akmhoque53353462014-04-22 08:43:45 -0500253 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500254 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500255 m_dryTable.push_back(rte);
256 }
akmhoque157b0a42014-05-13 00:26:37 -0500257 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500258 (*it).getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500259 }
260}
261
262void
akmhoque53353462014-04-22 08:43:45 -0500263RoutingTable::clearRoutingTable()
264{
akmhoque157b0a42014-05-13 00:26:37 -0500265 if (m_rTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500266 m_rTable.clear();
267 }
268}
269
270void
271RoutingTable::clearDryRoutingTable()
272{
akmhoque157b0a42014-05-13 00:26:37 -0500273 if (m_dryTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500274 m_dryTable.clear();
275 }
276}
277
Nick Gordonfad8e252016-08-11 14:21:38 -0500278} // namespace nlsr