blob: 708e3dd022a22703f9cad8ef41e05cdda31b1f57 [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
21#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>
31#include <string>
32#include <list>
33
akmhoque53353462014-04-22 08:43:45 -050034namespace nlsr {
35
akmhoque674b0b12014-05-20 14:33:28 -050036INIT_LOGGER("RoutingTable");
37
akmhoque53353462014-04-22 08:43:45 -050038using namespace std;
39
40void
41RoutingTable::calculate(Nlsr& pnlsr)
42{
akmhoque2f423352014-06-03 11:49:35 -050043 pnlsr.getLsdb().writeCorLsdbLog();
44 pnlsr.getLsdb().writeNameLsdbLog();
45 pnlsr.getLsdb().writeAdjLsdbLog();
akmhoque674b0b12014-05-20 14:33:28 -050046 pnlsr.getNamePrefixTable().writeLog();
akmhoque157b0a42014-05-13 00:26:37 -050047 if (pnlsr.getIsRoutingTableCalculating() == false) {
48 //setting routing table calculation
49 pnlsr.setIsRoutingTableCalculating(true);
Nick Gordone8e03ac2016-07-07 14:24:38 -050050
51 bool isHrEnabled = pnlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF;
52
53 if ((!isHrEnabled
54 &&
55 pnlsr.getLsdb()
56 .doesLsaExist(pnlsr.getConfParameter().getRouterPrefix().toUri()
57 + "/" + "adjacency", std::string("adjacency")))
58 ||
59 (isHrEnabled
60 &&
61 pnlsr.getLsdb()
62 .doesLsaExist(pnlsr.getConfParameter().getRouterPrefix().toUri()
63 + "/" + "coordinate", std::string("coordinate")))) {
akmhoque157b0a42014-05-13 00:26:37 -050064 if (pnlsr.getIsBuildAdjLsaSheduled() != 1) {
Vince Lehman50df6b72015-03-03 12:06:40 -060065 _LOG_TRACE("Clearing old routing table");
akmhoque53353462014-04-22 08:43:45 -050066 clearRoutingTable();
akmhoque157b0a42014-05-13 00:26:37 -050067 // for dry run options
68 clearDryRoutingTable();
Vince Lehman50df6b72015-03-03 12:06:40 -060069
70 _LOG_DEBUG("Calculating routing table");
71
akmhoque53353462014-04-22 08:43:45 -050072 // calculate Link State routing
akmhoque157b0a42014-05-13 00:26:37 -050073 if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF)
74 || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
akmhoque53353462014-04-22 08:43:45 -050075 calculateLsRoutingTable(pnlsr);
76 }
77 //calculate hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050078 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque53353462014-04-22 08:43:45 -050079 calculateHypRoutingTable(pnlsr);
80 }
81 //calculate dry hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050082 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
akmhoque53353462014-04-22 08:43:45 -050083 calculateHypDryRoutingTable(pnlsr);
84 }
Nick G97e34942016-07-11 14:46:27 -050085 // Inform the NPT that updates have been made
akmhoque2f423352014-06-03 11:49:35 -050086 _LOG_DEBUG("Calling Update NPT With new Route");
akmhoque31d1d4b2014-05-05 22:08:14 -050087 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoquedcee9362014-08-05 22:58:01 -050088 writeLog(pnlsr.getConfParameter().getHyperbolicState());
akmhoque674b0b12014-05-20 14:33:28 -050089 pnlsr.getNamePrefixTable().writeLog();
90 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -050091 }
akmhoque157b0a42014-05-13 00:26:37 -050092 else {
akmhoque674b0b12014-05-20 14:33:28 -050093 _LOG_DEBUG("Adjacency building is scheduled, so"
94 " routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -050095 }
96 }
akmhoque157b0a42014-05-13 00:26:37 -050097 else {
akmhoque674b0b12014-05-20 14:33:28 -050098 _LOG_DEBUG("No Adj LSA of router itself,"
99 " so Routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -0500100 clearRoutingTable();
101 clearDryRoutingTable(); // for dry run options
102 // need to update NPT here
akmhoque2f423352014-06-03 11:49:35 -0500103 _LOG_DEBUG("Calling Update NPT With new Route");
akmhoque31d1d4b2014-05-05 22:08:14 -0500104 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoquedcee9362014-08-05 22:58:01 -0500105 writeLog(pnlsr.getConfParameter().getHyperbolicState());
akmhoque674b0b12014-05-20 14:33:28 -0500106 pnlsr.getNamePrefixTable().writeLog();
107 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -0500108 //debugging purpose end
109 }
akmhoque157b0a42014-05-13 00:26:37 -0500110 pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag
111 pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation
akmhoque53353462014-04-22 08:43:45 -0500112 }
akmhoque157b0a42014-05-13 00:26:37 -0500113 else {
akmhoque53353462014-04-22 08:43:45 -0500114 scheduleRoutingTableCalculation(pnlsr);
115 }
116}
117
akmhoque53353462014-04-22 08:43:45 -0500118void
Vince Lehman9a709032014-09-13 16:28:07 -0500119RoutingTable::calculateLsRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500120{
akmhoque2f423352014-06-03 11:49:35 -0500121 _LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called");
Vince Lehman9a709032014-09-13 16:28:07 -0500122
123 Map map;
Nick Gordon22b5c952017-08-10 17:48:15 -0500124 map.createFromAdjLsdb(nlsr.getLsdb().getAdjLsdb().begin(), nlsr.getLsdb().getAdjLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500125 map.writeLog();
126
127 size_t nRouters = map.getMapSize();
128
129 LinkStateRoutingTableCalculator calculator(nRouters);
130
dmcoomes9f936662017-03-02 10:33:09 -0600131 calculator.calculatePath(map, std::ref(*this), nlsr);
akmhoque53353462014-04-22 08:43:45 -0500132}
133
134void
Vince Lehman9a709032014-09-13 16:28:07 -0500135RoutingTable::calculateHypRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500136{
Vince Lehman9a709032014-09-13 16:28:07 -0500137 Map map;
Nick Gordon22b5c952017-08-10 17:48:15 -0500138 map.createFromCoordinateLsdb(nlsr.getLsdb().getCoordinateLsdb().begin(),
139 nlsr.getLsdb().getCoordinateLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500140 map.writeLog();
141
142 size_t nRouters = map.getMapSize();
143
144 HyperbolicRoutingCalculator calculator(nRouters, false,
145 nlsr.getConfParameter().getRouterPrefix());
146
dmcoomes9f936662017-03-02 10:33:09 -0600147 calculator.calculatePaths(map, std::ref(*this),
Vince Lehman9a709032014-09-13 16:28:07 -0500148 nlsr.getLsdb(), nlsr.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500149}
150
151void
Vince Lehman9a709032014-09-13 16:28:07 -0500152RoutingTable::calculateHypDryRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500153{
Vince Lehman9a709032014-09-13 16:28:07 -0500154 Map map;
Nick Gordon22b5c952017-08-10 17:48:15 -0500155 map.createFromAdjLsdb(nlsr.getLsdb().getAdjLsdb().begin(), nlsr.getLsdb().getAdjLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500156 map.writeLog();
157
158 size_t nRouters = map.getMapSize();
159
160 HyperbolicRoutingCalculator calculator(nRouters, true,
161 nlsr.getConfParameter().getRouterPrefix());
162
dmcoomes9f936662017-03-02 10:33:09 -0600163 calculator.calculatePaths(map, std::ref(*this),
Vince Lehman9a709032014-09-13 16:28:07 -0500164 nlsr.getLsdb(), nlsr.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500165}
166
167void
168RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
169{
akmhoque157b0a42014-05-13 00:26:37 -0500170 if (pnlsr.getIsRouteCalculationScheduled() != true) {
Vince Lehman50df6b72015-03-03 12:06:40 -0600171 _LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
172
Vince Lehman7b616582014-10-17 16:25:39 -0500173 m_scheduler.scheduleEvent(m_routingCalcInterval,
dmcoomes9f936662017-03-02 10:33:09 -0600174 std::bind(&RoutingTable::calculate, this, std::ref(pnlsr)));
Vince Lehman7c603292014-09-11 17:48:16 -0500175
akmhoque157b0a42014-05-13 00:26:37 -0500176 pnlsr.setIsRouteCalculationScheduled(true);
akmhoque53353462014-04-22 08:43:45 -0500177 }
178}
179
180static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500181routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500182{
183 return rte.getDestination() == destRouter;
184}
185
akmhoque53353462014-04-22 08:43:45 -0500186void
akmhoque31d1d4b2014-05-05 22:08:14 -0500187RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500188{
Vince Lehman9a709032014-09-13 16:28:07 -0500189 _LOG_DEBUG("Adding " << nh << " for destination: " << destRouter);
190
akmhoqueb6450b12014-04-24 00:01:03 -0500191 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500192 if (rteChk == 0) {
akmhoque53353462014-04-22 08:43:45 -0500193 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500194 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500195 m_rTable.push_back(rte);
196 }
akmhoque157b0a42014-05-13 00:26:37 -0500197 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500198 rteChk->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500199 }
200}
201
akmhoqueb6450b12014-04-24 00:01:03 -0500202RoutingTableEntry*
akmhoque31d1d4b2014-05-05 22:08:14 -0500203RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500204{
205 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
206 m_rTable.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600207 std::bind(&routingTableEntryCompare,
akmhoque157b0a42014-05-13 00:26:37 -0500208 _1, destRouter));
209 if (it != m_rTable.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500210 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500211 }
akmhoqueb6450b12014-04-24 00:01:03 -0500212 return 0;
akmhoque53353462014-04-22 08:43:45 -0500213}
214
215void
akmhoquedcee9362014-08-05 22:58:01 -0500216RoutingTable::writeLog(int hyperbolicState)
akmhoque674b0b12014-05-20 14:33:28 -0500217{
218 _LOG_DEBUG("---------------Routing Table------------------");
219 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
220 it != m_rTable.end(); ++it) {
221 _LOG_DEBUG("Destination: " << (*it).getDestination());
222 _LOG_DEBUG("Nexthops: ");
223 (*it).getNexthopList().writeLog();
224 }
akmhoquedcee9362014-08-05 22:58:01 -0500225
226 if (hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
227 _LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
228 for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ;
229 it != m_dryTable.end(); ++it) {
230 _LOG_DEBUG("Destination: " << (*it).getDestination());
231 _LOG_DEBUG("Nexthops: ");
232 (*it).getNexthopList().writeLog();
233 }
234 }
akmhoque674b0b12014-05-20 14:33:28 -0500235}
236
akmhoque53353462014-04-22 08:43:45 -0500237void
akmhoque31d1d4b2014-05-05 22:08:14 -0500238RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500239{
Vince Lehman9a709032014-09-13 16:28:07 -0500240 _LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
241
akmhoque53353462014-04-22 08:43:45 -0500242 std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
243 m_dryTable.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600244 std::bind(&routingTableEntryCompare,
akmhoque157b0a42014-05-13 00:26:37 -0500245 _1, destRouter));
246 if (it == m_dryTable.end()) {
akmhoque53353462014-04-22 08:43:45 -0500247 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500248 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500249 m_dryTable.push_back(rte);
250 }
akmhoque157b0a42014-05-13 00:26:37 -0500251 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500252 (*it).getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500253 }
254}
255
256void
akmhoque53353462014-04-22 08:43:45 -0500257RoutingTable::clearRoutingTable()
258{
akmhoque157b0a42014-05-13 00:26:37 -0500259 if (m_rTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500260 m_rTable.clear();
261 }
262}
263
264void
265RoutingTable::clearDryRoutingTable()
266{
akmhoque157b0a42014-05-13 00:26:37 -0500267 if (m_dryTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500268 m_dryTable.clear();
269 }
270}
271
Nick Gordonfad8e252016-08-11 14:21:38 -0500272} // namespace nlsr