blob: 238852ac21ee810e98ffb83857af0f50589496a9 [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/>.
akmhoque3d06e792014-05-27 16:23:20 -050019 **/
akmhoque53353462014-04-22 08:43:45 -050020#include "routing-table.hpp"
21#include "nlsr.hpp"
22#include "map.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050023#include "conf-parameter.hpp"
akmhoque53353462014-04-22 08:43:45 -050024#include "routing-table-calculator.hpp"
25#include "routing-table-entry.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050026#include "name-prefix-table.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050027#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050028
Nick Gordon22b5c952017-08-10 17:48:15 -050029#include <iostream>
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
akmhoque674b0b12014-05-20 14:33:28 -050035INIT_LOGGER("RoutingTable");
36
akmhoque53353462014-04-22 08:43:45 -050037void
38RoutingTable::calculate(Nlsr& pnlsr)
39{
akmhoque2f423352014-06-03 11:49:35 -050040 pnlsr.getLsdb().writeCorLsdbLog();
41 pnlsr.getLsdb().writeNameLsdbLog();
42 pnlsr.getLsdb().writeAdjLsdbLog();
akmhoque674b0b12014-05-20 14:33:28 -050043 pnlsr.getNamePrefixTable().writeLog();
akmhoque157b0a42014-05-13 00:26:37 -050044 if (pnlsr.getIsRoutingTableCalculating() == false) {
45 //setting routing table calculation
46 pnlsr.setIsRoutingTableCalculating(true);
Nick Gordone8e03ac2016-07-07 14:24:38 -050047
48 bool isHrEnabled = pnlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF;
49
50 if ((!isHrEnabled
51 &&
52 pnlsr.getLsdb()
53 .doesLsaExist(pnlsr.getConfParameter().getRouterPrefix().toUri()
54 + "/" + "adjacency", std::string("adjacency")))
55 ||
56 (isHrEnabled
57 &&
58 pnlsr.getLsdb()
59 .doesLsaExist(pnlsr.getConfParameter().getRouterPrefix().toUri()
60 + "/" + "coordinate", std::string("coordinate")))) {
akmhoque157b0a42014-05-13 00:26:37 -050061 if (pnlsr.getIsBuildAdjLsaSheduled() != 1) {
Vince Lehman50df6b72015-03-03 12:06:40 -060062 _LOG_TRACE("Clearing old routing table");
akmhoque53353462014-04-22 08:43:45 -050063 clearRoutingTable();
akmhoque157b0a42014-05-13 00:26:37 -050064 // for dry run options
65 clearDryRoutingTable();
Vince Lehman50df6b72015-03-03 12:06:40 -060066
67 _LOG_DEBUG("Calculating routing table");
68
akmhoque53353462014-04-22 08:43:45 -050069 // calculate Link State routing
akmhoque157b0a42014-05-13 00:26:37 -050070 if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF)
71 || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
akmhoque53353462014-04-22 08:43:45 -050072 calculateLsRoutingTable(pnlsr);
73 }
74 //calculate hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050075 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque53353462014-04-22 08:43:45 -050076 calculateHypRoutingTable(pnlsr);
77 }
78 //calculate dry hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050079 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
akmhoque53353462014-04-22 08:43:45 -050080 calculateHypDryRoutingTable(pnlsr);
81 }
Nick G97e34942016-07-11 14:46:27 -050082 // Inform the NPT that updates have been made
akmhoque2f423352014-06-03 11:49:35 -050083 _LOG_DEBUG("Calling Update NPT With new Route");
akmhoque31d1d4b2014-05-05 22:08:14 -050084 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoquedcee9362014-08-05 22:58:01 -050085 writeLog(pnlsr.getConfParameter().getHyperbolicState());
akmhoque674b0b12014-05-20 14:33:28 -050086 pnlsr.getNamePrefixTable().writeLog();
87 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -050088 }
akmhoque157b0a42014-05-13 00:26:37 -050089 else {
akmhoque674b0b12014-05-20 14:33:28 -050090 _LOG_DEBUG("Adjacency building is scheduled, so"
91 " routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -050092 }
93 }
akmhoque157b0a42014-05-13 00:26:37 -050094 else {
akmhoque674b0b12014-05-20 14:33:28 -050095 _LOG_DEBUG("No Adj LSA of router itself,"
96 " so Routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -050097 clearRoutingTable();
98 clearDryRoutingTable(); // for dry run options
99 // need to update NPT here
akmhoque2f423352014-06-03 11:49:35 -0500100 _LOG_DEBUG("Calling Update NPT With new Route");
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoquedcee9362014-08-05 22:58:01 -0500102 writeLog(pnlsr.getConfParameter().getHyperbolicState());
akmhoque674b0b12014-05-20 14:33:28 -0500103 pnlsr.getNamePrefixTable().writeLog();
104 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -0500105 //debugging purpose end
106 }
akmhoque157b0a42014-05-13 00:26:37 -0500107 pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag
108 pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation
akmhoque53353462014-04-22 08:43:45 -0500109 }
akmhoque157b0a42014-05-13 00:26:37 -0500110 else {
akmhoque53353462014-04-22 08:43:45 -0500111 scheduleRoutingTableCalculation(pnlsr);
112 }
113}
114
akmhoque53353462014-04-22 08:43:45 -0500115void
Vince Lehman9a709032014-09-13 16:28:07 -0500116RoutingTable::calculateLsRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500117{
akmhoque2f423352014-06-03 11:49:35 -0500118 _LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called");
Vince Lehman9a709032014-09-13 16:28:07 -0500119
120 Map map;
Nick Gordon22b5c952017-08-10 17:48:15 -0500121 map.createFromAdjLsdb(nlsr.getLsdb().getAdjLsdb().begin(), nlsr.getLsdb().getAdjLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500122 map.writeLog();
123
124 size_t nRouters = map.getMapSize();
125
126 LinkStateRoutingTableCalculator calculator(nRouters);
127
dmcoomes9f936662017-03-02 10:33:09 -0600128 calculator.calculatePath(map, std::ref(*this), nlsr);
akmhoque53353462014-04-22 08:43:45 -0500129}
130
131void
Vince Lehman9a709032014-09-13 16:28:07 -0500132RoutingTable::calculateHypRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500133{
Vince Lehman9a709032014-09-13 16:28:07 -0500134 Map map;
Nick Gordon22b5c952017-08-10 17:48:15 -0500135 map.createFromCoordinateLsdb(nlsr.getLsdb().getCoordinateLsdb().begin(),
136 nlsr.getLsdb().getCoordinateLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500137 map.writeLog();
138
139 size_t nRouters = map.getMapSize();
140
141 HyperbolicRoutingCalculator calculator(nRouters, false,
142 nlsr.getConfParameter().getRouterPrefix());
143
dmcoomes9f936662017-03-02 10:33:09 -0600144 calculator.calculatePaths(map, std::ref(*this),
Vince Lehman9a709032014-09-13 16:28:07 -0500145 nlsr.getLsdb(), nlsr.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500146}
147
148void
Vince Lehman9a709032014-09-13 16:28:07 -0500149RoutingTable::calculateHypDryRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500150{
Vince Lehman9a709032014-09-13 16:28:07 -0500151 Map map;
Nick Gordon22b5c952017-08-10 17:48:15 -0500152 map.createFromAdjLsdb(nlsr.getLsdb().getAdjLsdb().begin(), nlsr.getLsdb().getAdjLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500153 map.writeLog();
154
155 size_t nRouters = map.getMapSize();
156
157 HyperbolicRoutingCalculator calculator(nRouters, true,
158 nlsr.getConfParameter().getRouterPrefix());
159
dmcoomes9f936662017-03-02 10:33:09 -0600160 calculator.calculatePaths(map, std::ref(*this),
Vince Lehman9a709032014-09-13 16:28:07 -0500161 nlsr.getLsdb(), nlsr.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500162}
163
164void
165RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
166{
akmhoque157b0a42014-05-13 00:26:37 -0500167 if (pnlsr.getIsRouteCalculationScheduled() != true) {
Vince Lehman50df6b72015-03-03 12:06:40 -0600168 _LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
169
Vince Lehman7b616582014-10-17 16:25:39 -0500170 m_scheduler.scheduleEvent(m_routingCalcInterval,
dmcoomes9f936662017-03-02 10:33:09 -0600171 std::bind(&RoutingTable::calculate, this, std::ref(pnlsr)));
Vince Lehman7c603292014-09-11 17:48:16 -0500172
akmhoque157b0a42014-05-13 00:26:37 -0500173 pnlsr.setIsRouteCalculationScheduled(true);
akmhoque53353462014-04-22 08:43:45 -0500174 }
175}
176
177static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500178routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500179{
180 return rte.getDestination() == destRouter;
181}
182
akmhoque53353462014-04-22 08:43:45 -0500183void
akmhoque31d1d4b2014-05-05 22:08:14 -0500184RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500185{
Vince Lehman9a709032014-09-13 16:28:07 -0500186 _LOG_DEBUG("Adding " << nh << " for destination: " << destRouter);
187
akmhoqueb6450b12014-04-24 00:01:03 -0500188 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500189 if (rteChk == 0) {
akmhoque53353462014-04-22 08:43:45 -0500190 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500191 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500192 m_rTable.push_back(rte);
193 }
akmhoque157b0a42014-05-13 00:26:37 -0500194 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500195 rteChk->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500196 }
197}
198
akmhoqueb6450b12014-04-24 00:01:03 -0500199RoutingTableEntry*
akmhoque31d1d4b2014-05-05 22:08:14 -0500200RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500201{
202 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
203 m_rTable.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600204 std::bind(&routingTableEntryCompare,
akmhoque157b0a42014-05-13 00:26:37 -0500205 _1, destRouter));
206 if (it != m_rTable.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500207 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500208 }
akmhoqueb6450b12014-04-24 00:01:03 -0500209 return 0;
akmhoque53353462014-04-22 08:43:45 -0500210}
211
212void
akmhoquedcee9362014-08-05 22:58:01 -0500213RoutingTable::writeLog(int hyperbolicState)
akmhoque674b0b12014-05-20 14:33:28 -0500214{
215 _LOG_DEBUG("---------------Routing Table------------------");
216 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
217 it != m_rTable.end(); ++it) {
218 _LOG_DEBUG("Destination: " << (*it).getDestination());
219 _LOG_DEBUG("Nexthops: ");
220 (*it).getNexthopList().writeLog();
221 }
akmhoquedcee9362014-08-05 22:58:01 -0500222
223 if (hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
224 _LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
225 for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ;
226 it != m_dryTable.end(); ++it) {
227 _LOG_DEBUG("Destination: " << (*it).getDestination());
228 _LOG_DEBUG("Nexthops: ");
229 (*it).getNexthopList().writeLog();
230 }
231 }
akmhoque674b0b12014-05-20 14:33:28 -0500232}
233
akmhoque53353462014-04-22 08:43:45 -0500234void
akmhoque31d1d4b2014-05-05 22:08:14 -0500235RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500236{
Vince Lehman9a709032014-09-13 16:28:07 -0500237 _LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
238
akmhoque53353462014-04-22 08:43:45 -0500239 std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
240 m_dryTable.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600241 std::bind(&routingTableEntryCompare,
akmhoque157b0a42014-05-13 00:26:37 -0500242 _1, destRouter));
243 if (it == m_dryTable.end()) {
akmhoque53353462014-04-22 08:43:45 -0500244 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500245 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500246 m_dryTable.push_back(rte);
247 }
akmhoque157b0a42014-05-13 00:26:37 -0500248 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500249 (*it).getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500250 }
251}
252
253void
akmhoque53353462014-04-22 08:43:45 -0500254RoutingTable::clearRoutingTable()
255{
akmhoque157b0a42014-05-13 00:26:37 -0500256 if (m_rTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500257 m_rTable.clear();
258 }
259}
260
261void
262RoutingTable::clearDryRoutingTable()
263{
akmhoque157b0a42014-05-13 00:26:37 -0500264 if (m_dryTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500265 m_dryTable.clear();
266 }
267}
268
Nick Gordonfad8e252016-08-11 14:21:38 -0500269} // namespace nlsr