blob: 343767a02425f45977f7b993bde848b2d3598621 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
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/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque53353462014-04-22 08:43:45 -050023#include <iostream>
24#include <string>
25#include <list>
26
27#include "routing-table.hpp"
28#include "nlsr.hpp"
29#include "map.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050030#include "conf-parameter.hpp"
akmhoque53353462014-04-22 08:43:45 -050031#include "routing-table-calculator.hpp"
32#include "routing-table-entry.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050033#include "name-prefix-table.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050034#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050035
36namespace nlsr {
37
akmhoque674b0b12014-05-20 14:33:28 -050038INIT_LOGGER("RoutingTable");
39
akmhoque53353462014-04-22 08:43:45 -050040using namespace std;
41
42void
43RoutingTable::calculate(Nlsr& pnlsr)
44{
akmhoque2f423352014-06-03 11:49:35 -050045 pnlsr.getLsdb().writeCorLsdbLog();
46 pnlsr.getLsdb().writeNameLsdbLog();
47 pnlsr.getLsdb().writeAdjLsdbLog();
akmhoque674b0b12014-05-20 14:33:28 -050048 pnlsr.getNamePrefixTable().writeLog();
akmhoque157b0a42014-05-13 00:26:37 -050049 if (pnlsr.getIsRoutingTableCalculating() == false) {
50 //setting routing table calculation
51 pnlsr.setIsRoutingTableCalculating(true);
akmhoque53353462014-04-22 08:43:45 -050052 if (pnlsr.getLsdb().doesLsaExist(
akmhoque31d1d4b2014-05-05 22:08:14 -050053 pnlsr.getConfParameter().getRouterPrefix().toUri() + "/" + "adjacency",
akmhoque157b0a42014-05-13 00:26:37 -050054 std::string("adjacency"))) {
55 if (pnlsr.getIsBuildAdjLsaSheduled() != 1) {
Vince Lehman50df6b72015-03-03 12:06:40 -060056 _LOG_TRACE("Clearing old routing table");
akmhoque53353462014-04-22 08:43:45 -050057 clearRoutingTable();
akmhoque157b0a42014-05-13 00:26:37 -050058 // for dry run options
59 clearDryRoutingTable();
Vince Lehman50df6b72015-03-03 12:06:40 -060060
61 _LOG_DEBUG("Calculating routing table");
62
akmhoque53353462014-04-22 08:43:45 -050063 // calculate Link State routing
akmhoque157b0a42014-05-13 00:26:37 -050064 if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF)
65 || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
akmhoque53353462014-04-22 08:43:45 -050066 calculateLsRoutingTable(pnlsr);
67 }
68 //calculate hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050069 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque53353462014-04-22 08:43:45 -050070 calculateHypRoutingTable(pnlsr);
71 }
72 //calculate dry hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050073 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
akmhoque53353462014-04-22 08:43:45 -050074 calculateHypDryRoutingTable(pnlsr);
75 }
76 //need to update NPT here
akmhoque2f423352014-06-03 11:49:35 -050077 _LOG_DEBUG("Calling Update NPT With new Route");
akmhoque31d1d4b2014-05-05 22:08:14 -050078 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoquedcee9362014-08-05 22:58:01 -050079 writeLog(pnlsr.getConfParameter().getHyperbolicState());
akmhoque674b0b12014-05-20 14:33:28 -050080 pnlsr.getNamePrefixTable().writeLog();
81 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -050082 }
akmhoque157b0a42014-05-13 00:26:37 -050083 else {
akmhoque674b0b12014-05-20 14:33:28 -050084 _LOG_DEBUG("Adjacency building is scheduled, so"
85 " routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -050086 }
87 }
akmhoque157b0a42014-05-13 00:26:37 -050088 else {
akmhoque674b0b12014-05-20 14:33:28 -050089 _LOG_DEBUG("No Adj LSA of router itself,"
90 " so Routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -050091 clearRoutingTable();
92 clearDryRoutingTable(); // for dry run options
93 // need to update NPT here
akmhoque2f423352014-06-03 11:49:35 -050094 _LOG_DEBUG("Calling Update NPT With new Route");
akmhoque31d1d4b2014-05-05 22:08:14 -050095 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoquedcee9362014-08-05 22:58:01 -050096 writeLog(pnlsr.getConfParameter().getHyperbolicState());
akmhoque674b0b12014-05-20 14:33:28 -050097 pnlsr.getNamePrefixTable().writeLog();
98 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -050099 //debugging purpose end
100 }
akmhoque157b0a42014-05-13 00:26:37 -0500101 pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag
102 pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation
akmhoque53353462014-04-22 08:43:45 -0500103 }
akmhoque157b0a42014-05-13 00:26:37 -0500104 else {
akmhoque53353462014-04-22 08:43:45 -0500105 scheduleRoutingTableCalculation(pnlsr);
106 }
107}
108
109
110void
Vince Lehman9a709032014-09-13 16:28:07 -0500111RoutingTable::calculateLsRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500112{
akmhoque2f423352014-06-03 11:49:35 -0500113 _LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called");
Vince Lehman9a709032014-09-13 16:28:07 -0500114
115 Map map;
116 map.createFromAdjLsdb(nlsr);
117 map.writeLog();
118
119 size_t nRouters = map.getMapSize();
120
121 LinkStateRoutingTableCalculator calculator(nRouters);
122
123 calculator.calculatePath(map, ndn::ref(*this), nlsr);
akmhoque53353462014-04-22 08:43:45 -0500124}
125
126void
Vince Lehman9a709032014-09-13 16:28:07 -0500127RoutingTable::calculateHypRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500128{
Vince Lehman9a709032014-09-13 16:28:07 -0500129 Map map;
130 map.createFromAdjLsdb(nlsr);
131 map.writeLog();
132
133 size_t nRouters = map.getMapSize();
134
135 HyperbolicRoutingCalculator calculator(nRouters, false,
136 nlsr.getConfParameter().getRouterPrefix());
137
138 calculator.calculatePaths(map, ndn::ref(*this),
139 nlsr.getLsdb(), nlsr.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500140}
141
142void
Vince Lehman9a709032014-09-13 16:28:07 -0500143RoutingTable::calculateHypDryRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500144{
Vince Lehman9a709032014-09-13 16:28:07 -0500145 Map map;
146 map.createFromAdjLsdb(nlsr);
147 map.writeLog();
148
149 size_t nRouters = map.getMapSize();
150
151 HyperbolicRoutingCalculator calculator(nRouters, true,
152 nlsr.getConfParameter().getRouterPrefix());
153
154 calculator.calculatePaths(map, ndn::ref(*this),
155 nlsr.getLsdb(), nlsr.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500156}
157
158void
159RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
160{
akmhoque157b0a42014-05-13 00:26:37 -0500161 if (pnlsr.getIsRouteCalculationScheduled() != true) {
Vince Lehman50df6b72015-03-03 12:06:40 -0600162 _LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
163
Vince Lehman7b616582014-10-17 16:25:39 -0500164 m_scheduler.scheduleEvent(m_routingCalcInterval,
Vince Lehman7c603292014-09-11 17:48:16 -0500165 ndn::bind(&RoutingTable::calculate, this, ndn::ref(pnlsr)));
166
akmhoque157b0a42014-05-13 00:26:37 -0500167 pnlsr.setIsRouteCalculationScheduled(true);
akmhoque53353462014-04-22 08:43:45 -0500168 }
169}
170
171static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500172routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500173{
174 return rte.getDestination() == destRouter;
175}
176
177// function related to manipulation of routing table
178void
akmhoque31d1d4b2014-05-05 22:08:14 -0500179RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500180{
Vince Lehman9a709032014-09-13 16:28:07 -0500181 _LOG_DEBUG("Adding " << nh << " for destination: " << destRouter);
182
akmhoqueb6450b12014-04-24 00:01:03 -0500183 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500184 if (rteChk == 0) {
akmhoque53353462014-04-22 08:43:45 -0500185 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500186 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500187 m_rTable.push_back(rte);
188 }
akmhoque157b0a42014-05-13 00:26:37 -0500189 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500190 rteChk->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500191 }
192}
193
akmhoqueb6450b12014-04-24 00:01:03 -0500194RoutingTableEntry*
akmhoque31d1d4b2014-05-05 22:08:14 -0500195RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500196{
197 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
198 m_rTable.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500199 ndn::bind(&routingTableEntryCompare,
200 _1, destRouter));
201 if (it != m_rTable.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500202 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500203 }
akmhoqueb6450b12014-04-24 00:01:03 -0500204 return 0;
akmhoque53353462014-04-22 08:43:45 -0500205}
206
207void
akmhoquedcee9362014-08-05 22:58:01 -0500208RoutingTable::writeLog(int hyperbolicState)
akmhoque674b0b12014-05-20 14:33:28 -0500209{
210 _LOG_DEBUG("---------------Routing Table------------------");
211 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
212 it != m_rTable.end(); ++it) {
213 _LOG_DEBUG("Destination: " << (*it).getDestination());
214 _LOG_DEBUG("Nexthops: ");
215 (*it).getNexthopList().writeLog();
216 }
akmhoquedcee9362014-08-05 22:58:01 -0500217
218 if (hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
219 _LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
220 for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ;
221 it != m_dryTable.end(); ++it) {
222 _LOG_DEBUG("Destination: " << (*it).getDestination());
223 _LOG_DEBUG("Nexthops: ");
224 (*it).getNexthopList().writeLog();
225 }
226 }
akmhoque674b0b12014-05-20 14:33:28 -0500227}
228
akmhoque53353462014-04-22 08:43:45 -0500229//function related to manipulation of dry routing table
230void
akmhoque31d1d4b2014-05-05 22:08:14 -0500231RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500232{
Vince Lehman9a709032014-09-13 16:28:07 -0500233 _LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
234
akmhoque53353462014-04-22 08:43:45 -0500235 std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
236 m_dryTable.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500237 ndn::bind(&routingTableEntryCompare,
238 _1, destRouter));
239 if (it == m_dryTable.end()) {
akmhoque53353462014-04-22 08:43:45 -0500240 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500241 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500242 m_dryTable.push_back(rte);
243 }
akmhoque157b0a42014-05-13 00:26:37 -0500244 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500245 (*it).getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500246 }
247}
248
249void
akmhoque53353462014-04-22 08:43:45 -0500250RoutingTable::clearRoutingTable()
251{
akmhoque157b0a42014-05-13 00:26:37 -0500252 if (m_rTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500253 m_rTable.clear();
254 }
255}
256
257void
258RoutingTable::clearDryRoutingTable()
259{
akmhoque157b0a42014-05-13 00:26:37 -0500260 if (m_dryTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500261 m_dryTable.clear();
262 }
263}
264
265}//namespace nlsr
266