blob: e61d4d0b08b11f23d256886f094b391f5b5b2520 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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/>.
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);
Nick Gordone8e03ac2016-07-07 14:24:38 -050052
53 bool isHrEnabled = pnlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF;
54
55 if ((!isHrEnabled
56 &&
57 pnlsr.getLsdb()
58 .doesLsaExist(pnlsr.getConfParameter().getRouterPrefix().toUri()
59 + "/" + "adjacency", std::string("adjacency")))
60 ||
61 (isHrEnabled
62 &&
63 pnlsr.getLsdb()
64 .doesLsaExist(pnlsr.getConfParameter().getRouterPrefix().toUri()
65 + "/" + "coordinate", std::string("coordinate")))) {
akmhoque157b0a42014-05-13 00:26:37 -050066 if (pnlsr.getIsBuildAdjLsaSheduled() != 1) {
Vince Lehman50df6b72015-03-03 12:06:40 -060067 _LOG_TRACE("Clearing old routing table");
akmhoque53353462014-04-22 08:43:45 -050068 clearRoutingTable();
akmhoque157b0a42014-05-13 00:26:37 -050069 // for dry run options
70 clearDryRoutingTable();
Vince Lehman50df6b72015-03-03 12:06:40 -060071
72 _LOG_DEBUG("Calculating routing table");
73
akmhoque53353462014-04-22 08:43:45 -050074 // calculate Link State routing
akmhoque157b0a42014-05-13 00:26:37 -050075 if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF)
76 || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
akmhoque53353462014-04-22 08:43:45 -050077 calculateLsRoutingTable(pnlsr);
78 }
79 //calculate hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050080 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque53353462014-04-22 08:43:45 -050081 calculateHypRoutingTable(pnlsr);
82 }
83 //calculate dry hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050084 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
akmhoque53353462014-04-22 08:43:45 -050085 calculateHypDryRoutingTable(pnlsr);
86 }
Nick G97e34942016-07-11 14:46:27 -050087 // Inform the NPT that updates have been made
akmhoque2f423352014-06-03 11:49:35 -050088 _LOG_DEBUG("Calling Update NPT With new Route");
akmhoque31d1d4b2014-05-05 22:08:14 -050089 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoquedcee9362014-08-05 22:58:01 -050090 writeLog(pnlsr.getConfParameter().getHyperbolicState());
akmhoque674b0b12014-05-20 14:33:28 -050091 pnlsr.getNamePrefixTable().writeLog();
92 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -050093 }
akmhoque157b0a42014-05-13 00:26:37 -050094 else {
akmhoque674b0b12014-05-20 14:33:28 -050095 _LOG_DEBUG("Adjacency building is scheduled, so"
96 " routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -050097 }
98 }
akmhoque157b0a42014-05-13 00:26:37 -050099 else {
akmhoque674b0b12014-05-20 14:33:28 -0500100 _LOG_DEBUG("No Adj LSA of router itself,"
101 " 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
akmhoque2f423352014-06-03 11:49:35 -0500105 _LOG_DEBUG("Calling Update NPT With new Route");
akmhoque31d1d4b2014-05-05 22:08:14 -0500106 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoquedcee9362014-08-05 22:58:01 -0500107 writeLog(pnlsr.getConfParameter().getHyperbolicState());
akmhoque674b0b12014-05-20 14:33:28 -0500108 pnlsr.getNamePrefixTable().writeLog();
109 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -0500110 //debugging purpose end
111 }
akmhoque157b0a42014-05-13 00:26:37 -0500112 pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag
113 pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation
akmhoque53353462014-04-22 08:43:45 -0500114 }
akmhoque157b0a42014-05-13 00:26:37 -0500115 else {
akmhoque53353462014-04-22 08:43:45 -0500116 scheduleRoutingTableCalculation(pnlsr);
117 }
118}
119
akmhoque53353462014-04-22 08:43:45 -0500120void
Vince Lehman9a709032014-09-13 16:28:07 -0500121RoutingTable::calculateLsRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500122{
akmhoque2f423352014-06-03 11:49:35 -0500123 _LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called");
Vince Lehman9a709032014-09-13 16:28:07 -0500124
125 Map map;
126 map.createFromAdjLsdb(nlsr);
127 map.writeLog();
128
129 size_t nRouters = map.getMapSize();
130
131 LinkStateRoutingTableCalculator calculator(nRouters);
132
133 calculator.calculatePath(map, ndn::ref(*this), nlsr);
akmhoque53353462014-04-22 08:43:45 -0500134}
135
136void
Vince Lehman9a709032014-09-13 16:28:07 -0500137RoutingTable::calculateHypRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500138{
Vince Lehman9a709032014-09-13 16:28:07 -0500139 Map map;
Nick Gordone8e03ac2016-07-07 14:24:38 -0500140 map.createFromCoordinateLsdb(nlsr);
Vince Lehman9a709032014-09-13 16:28:07 -0500141 map.writeLog();
142
143 size_t nRouters = map.getMapSize();
144
145 HyperbolicRoutingCalculator calculator(nRouters, false,
146 nlsr.getConfParameter().getRouterPrefix());
147
148 calculator.calculatePaths(map, ndn::ref(*this),
149 nlsr.getLsdb(), nlsr.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500150}
151
152void
Vince Lehman9a709032014-09-13 16:28:07 -0500153RoutingTable::calculateHypDryRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500154{
Vince Lehman9a709032014-09-13 16:28:07 -0500155 Map map;
156 map.createFromAdjLsdb(nlsr);
157 map.writeLog();
158
159 size_t nRouters = map.getMapSize();
160
161 HyperbolicRoutingCalculator calculator(nRouters, true,
162 nlsr.getConfParameter().getRouterPrefix());
163
164 calculator.calculatePaths(map, ndn::ref(*this),
165 nlsr.getLsdb(), nlsr.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500166}
167
168void
169RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
170{
akmhoque157b0a42014-05-13 00:26:37 -0500171 if (pnlsr.getIsRouteCalculationScheduled() != true) {
Vince Lehman50df6b72015-03-03 12:06:40 -0600172 _LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
173
Vince Lehman7b616582014-10-17 16:25:39 -0500174 m_scheduler.scheduleEvent(m_routingCalcInterval,
Vince Lehman7c603292014-09-11 17:48:16 -0500175 ndn::bind(&RoutingTable::calculate, this, ndn::ref(pnlsr)));
176
akmhoque157b0a42014-05-13 00:26:37 -0500177 pnlsr.setIsRouteCalculationScheduled(true);
akmhoque53353462014-04-22 08:43:45 -0500178 }
179}
180
181static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500182routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500183{
184 return rte.getDestination() == destRouter;
185}
186
akmhoque53353462014-04-22 08:43:45 -0500187void
akmhoque31d1d4b2014-05-05 22:08:14 -0500188RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500189{
Vince Lehman9a709032014-09-13 16:28:07 -0500190 _LOG_DEBUG("Adding " << nh << " for destination: " << destRouter);
191
akmhoqueb6450b12014-04-24 00:01:03 -0500192 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500193 if (rteChk == 0) {
akmhoque53353462014-04-22 08:43:45 -0500194 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500195 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500196 m_rTable.push_back(rte);
197 }
akmhoque157b0a42014-05-13 00:26:37 -0500198 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500199 rteChk->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500200 }
201}
202
akmhoqueb6450b12014-04-24 00:01:03 -0500203RoutingTableEntry*
akmhoque31d1d4b2014-05-05 22:08:14 -0500204RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500205{
206 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
207 m_rTable.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500208 ndn::bind(&routingTableEntryCompare,
209 _1, destRouter));
210 if (it != m_rTable.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500211 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500212 }
akmhoqueb6450b12014-04-24 00:01:03 -0500213 return 0;
akmhoque53353462014-04-22 08:43:45 -0500214}
215
216void
akmhoquedcee9362014-08-05 22:58:01 -0500217RoutingTable::writeLog(int hyperbolicState)
akmhoque674b0b12014-05-20 14:33:28 -0500218{
219 _LOG_DEBUG("---------------Routing Table------------------");
220 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
221 it != m_rTable.end(); ++it) {
222 _LOG_DEBUG("Destination: " << (*it).getDestination());
223 _LOG_DEBUG("Nexthops: ");
224 (*it).getNexthopList().writeLog();
225 }
akmhoquedcee9362014-08-05 22:58:01 -0500226
227 if (hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
228 _LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
229 for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ;
230 it != m_dryTable.end(); ++it) {
231 _LOG_DEBUG("Destination: " << (*it).getDestination());
232 _LOG_DEBUG("Nexthops: ");
233 (*it).getNexthopList().writeLog();
234 }
235 }
akmhoque674b0b12014-05-20 14:33:28 -0500236}
237
akmhoque53353462014-04-22 08:43:45 -0500238void
akmhoque31d1d4b2014-05-05 22:08:14 -0500239RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500240{
Vince Lehman9a709032014-09-13 16:28:07 -0500241 _LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
242
akmhoque53353462014-04-22 08:43:45 -0500243 std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
244 m_dryTable.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500245 ndn::bind(&routingTableEntryCompare,
246 _1, destRouter));
247 if (it == m_dryTable.end()) {
akmhoque53353462014-04-22 08:43:45 -0500248 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500249 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500250 m_dryTable.push_back(rte);
251 }
akmhoque157b0a42014-05-13 00:26:37 -0500252 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500253 (*it).getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500254 }
255}
256
257void
akmhoque53353462014-04-22 08:43:45 -0500258RoutingTable::clearRoutingTable()
259{
akmhoque157b0a42014-05-13 00:26:37 -0500260 if (m_rTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500261 m_rTable.clear();
262 }
263}
264
265void
266RoutingTable::clearDryRoutingTable()
267{
akmhoque157b0a42014-05-13 00:26:37 -0500268 if (m_dryTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500269 m_dryTable.clear();
270 }
271}
272
Nick Gordonfad8e252016-08-11 14:21:38 -0500273} // namespace nlsr