blob: a414e8ab02216758f1c299f77f4111cc812d56d3 [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
Nick Gordonb7b58392017-08-17 16:29:21 -050037RoutingTable::RoutingTable(ndn::Scheduler& scheduler)
Nick Gordond40a5882017-09-05 15:34:58 -050038 : afterRoutingChange{ndn::make_unique<AfterRoutingChange>()}
Nick Gordonb7b58392017-08-17 16:29:21 -050039 , m_scheduler(scheduler)
40 , m_NO_NEXT_HOP{-12345}
41 , m_routingCalcInterval{static_cast<uint32_t>(ROUTING_CALC_INTERVAL_DEFAULT)}
42{
43}
44
akmhoque53353462014-04-22 08:43:45 -050045void
46RoutingTable::calculate(Nlsr& pnlsr)
47{
akmhoque2f423352014-06-03 11:49:35 -050048 pnlsr.getLsdb().writeCorLsdbLog();
49 pnlsr.getLsdb().writeNameLsdbLog();
50 pnlsr.getLsdb().writeAdjLsdbLog();
akmhoque674b0b12014-05-20 14:33:28 -050051 pnlsr.getNamePrefixTable().writeLog();
akmhoque157b0a42014-05-13 00:26:37 -050052 if (pnlsr.getIsRoutingTableCalculating() == false) {
53 //setting routing table calculation
54 pnlsr.setIsRoutingTableCalculating(true);
Nick Gordone8e03ac2016-07-07 14:24:38 -050055
56 bool isHrEnabled = pnlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF;
57
58 if ((!isHrEnabled
59 &&
60 pnlsr.getLsdb()
Nick Gordon727d4832017-10-13 18:04:25 -050061 .doesLsaExist(ndn::Name{pnlsr.getConfParameter().getRouterPrefix()}
62 .append(std::to_string(Lsa::Type::ADJACENCY)), Lsa::Type::ADJACENCY))
Nick Gordone8e03ac2016-07-07 14:24:38 -050063 ||
64 (isHrEnabled
65 &&
66 pnlsr.getLsdb()
Nick Gordon727d4832017-10-13 18:04:25 -050067 .doesLsaExist(ndn::Name{pnlsr.getConfParameter().getRouterPrefix()}
68 .append(std::to_string(Lsa::Type::COORDINATE)), Lsa::Type::COORDINATE))) {
akmhoque157b0a42014-05-13 00:26:37 -050069 if (pnlsr.getIsBuildAdjLsaSheduled() != 1) {
Vince Lehman50df6b72015-03-03 12:06:40 -060070 _LOG_TRACE("Clearing old routing table");
akmhoque53353462014-04-22 08:43:45 -050071 clearRoutingTable();
akmhoque157b0a42014-05-13 00:26:37 -050072 // for dry run options
73 clearDryRoutingTable();
Vince Lehman50df6b72015-03-03 12:06:40 -060074
75 _LOG_DEBUG("Calculating routing table");
76
akmhoque53353462014-04-22 08:43:45 -050077 // calculate Link State routing
akmhoque157b0a42014-05-13 00:26:37 -050078 if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF)
79 || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
akmhoque53353462014-04-22 08:43:45 -050080 calculateLsRoutingTable(pnlsr);
81 }
82 //calculate hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050083 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque53353462014-04-22 08:43:45 -050084 calculateHypRoutingTable(pnlsr);
85 }
86 //calculate dry hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050087 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
akmhoque53353462014-04-22 08:43:45 -050088 calculateHypDryRoutingTable(pnlsr);
89 }
Nick G97e34942016-07-11 14:46:27 -050090 // Inform the NPT that updates have been made
akmhoque2f423352014-06-03 11:49:35 -050091 _LOG_DEBUG("Calling Update NPT With new Route");
Nick Gordonb7b58392017-08-17 16:29:21 -050092 (*afterRoutingChange)(m_rTable);
akmhoquedcee9362014-08-05 22:58:01 -050093 writeLog(pnlsr.getConfParameter().getHyperbolicState());
akmhoque674b0b12014-05-20 14:33:28 -050094 pnlsr.getNamePrefixTable().writeLog();
95 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -050096 }
akmhoque157b0a42014-05-13 00:26:37 -050097 else {
akmhoque674b0b12014-05-20 14:33:28 -050098 _LOG_DEBUG("Adjacency building is scheduled, so"
99 " routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -0500100 }
101 }
akmhoque157b0a42014-05-13 00:26:37 -0500102 else {
akmhoque674b0b12014-05-20 14:33:28 -0500103 _LOG_DEBUG("No Adj LSA of router itself,"
104 " so Routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -0500105 clearRoutingTable();
106 clearDryRoutingTable(); // for dry run options
107 // need to update NPT here
akmhoque2f423352014-06-03 11:49:35 -0500108 _LOG_DEBUG("Calling Update NPT With new Route");
Nick Gordonb7b58392017-08-17 16:29:21 -0500109 (*afterRoutingChange)(m_rTable);
akmhoquedcee9362014-08-05 22:58:01 -0500110 writeLog(pnlsr.getConfParameter().getHyperbolicState());
akmhoque674b0b12014-05-20 14:33:28 -0500111 pnlsr.getNamePrefixTable().writeLog();
112 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -0500113 //debugging purpose end
114 }
akmhoque157b0a42014-05-13 00:26:37 -0500115 pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag
116 pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation
akmhoque53353462014-04-22 08:43:45 -0500117 }
akmhoque157b0a42014-05-13 00:26:37 -0500118 else {
akmhoque53353462014-04-22 08:43:45 -0500119 scheduleRoutingTableCalculation(pnlsr);
120 }
121}
122
akmhoque53353462014-04-22 08:43:45 -0500123void
Vince Lehman9a709032014-09-13 16:28:07 -0500124RoutingTable::calculateLsRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500125{
akmhoque2f423352014-06-03 11:49:35 -0500126 _LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called");
Vince Lehman9a709032014-09-13 16:28:07 -0500127
128 Map map;
Nick Gordon22b5c952017-08-10 17:48:15 -0500129 map.createFromAdjLsdb(nlsr.getLsdb().getAdjLsdb().begin(), nlsr.getLsdb().getAdjLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500130 map.writeLog();
131
132 size_t nRouters = map.getMapSize();
133
134 LinkStateRoutingTableCalculator calculator(nRouters);
135
dmcoomes9f936662017-03-02 10:33:09 -0600136 calculator.calculatePath(map, std::ref(*this), nlsr);
akmhoque53353462014-04-22 08:43:45 -0500137}
138
139void
Vince Lehman9a709032014-09-13 16:28:07 -0500140RoutingTable::calculateHypRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500141{
Vince Lehman9a709032014-09-13 16:28:07 -0500142 Map map;
Nick Gordon22b5c952017-08-10 17:48:15 -0500143 map.createFromCoordinateLsdb(nlsr.getLsdb().getCoordinateLsdb().begin(),
144 nlsr.getLsdb().getCoordinateLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500145 map.writeLog();
146
147 size_t nRouters = map.getMapSize();
148
149 HyperbolicRoutingCalculator calculator(nRouters, false,
150 nlsr.getConfParameter().getRouterPrefix());
151
dmcoomes9f936662017-03-02 10:33:09 -0600152 calculator.calculatePaths(map, std::ref(*this),
Vince Lehman9a709032014-09-13 16:28:07 -0500153 nlsr.getLsdb(), nlsr.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500154}
155
156void
Vince Lehman9a709032014-09-13 16:28:07 -0500157RoutingTable::calculateHypDryRoutingTable(Nlsr& nlsr)
akmhoque53353462014-04-22 08:43:45 -0500158{
Vince Lehman9a709032014-09-13 16:28:07 -0500159 Map map;
Nick Gordon22b5c952017-08-10 17:48:15 -0500160 map.createFromAdjLsdb(nlsr.getLsdb().getAdjLsdb().begin(), nlsr.getLsdb().getAdjLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500161 map.writeLog();
162
163 size_t nRouters = map.getMapSize();
164
165 HyperbolicRoutingCalculator calculator(nRouters, true,
166 nlsr.getConfParameter().getRouterPrefix());
167
dmcoomes9f936662017-03-02 10:33:09 -0600168 calculator.calculatePaths(map, std::ref(*this),
Vince Lehman9a709032014-09-13 16:28:07 -0500169 nlsr.getLsdb(), nlsr.getAdjacencyList());
akmhoque53353462014-04-22 08:43:45 -0500170}
171
172void
173RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
174{
akmhoque157b0a42014-05-13 00:26:37 -0500175 if (pnlsr.getIsRouteCalculationScheduled() != true) {
Vince Lehman50df6b72015-03-03 12:06:40 -0600176 _LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
177
Vince Lehman7b616582014-10-17 16:25:39 -0500178 m_scheduler.scheduleEvent(m_routingCalcInterval,
dmcoomes9f936662017-03-02 10:33:09 -0600179 std::bind(&RoutingTable::calculate, this, std::ref(pnlsr)));
Vince Lehman7c603292014-09-11 17:48:16 -0500180
akmhoque157b0a42014-05-13 00:26:37 -0500181 pnlsr.setIsRouteCalculationScheduled(true);
akmhoque53353462014-04-22 08:43:45 -0500182 }
183}
184
185static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500186routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500187{
188 return rte.getDestination() == destRouter;
189}
190
akmhoque53353462014-04-22 08:43:45 -0500191void
akmhoque31d1d4b2014-05-05 22:08:14 -0500192RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500193{
Vince Lehman9a709032014-09-13 16:28:07 -0500194 _LOG_DEBUG("Adding " << nh << " for destination: " << destRouter);
195
akmhoqueb6450b12014-04-24 00:01:03 -0500196 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500197 if (rteChk == 0) {
akmhoque53353462014-04-22 08:43:45 -0500198 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500199 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500200 m_rTable.push_back(rte);
201 }
akmhoque157b0a42014-05-13 00:26:37 -0500202 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500203 rteChk->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500204 }
205}
206
akmhoqueb6450b12014-04-24 00:01:03 -0500207RoutingTableEntry*
akmhoque31d1d4b2014-05-05 22:08:14 -0500208RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500209{
210 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
211 m_rTable.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600212 std::bind(&routingTableEntryCompare,
akmhoque157b0a42014-05-13 00:26:37 -0500213 _1, destRouter));
214 if (it != m_rTable.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500215 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500216 }
akmhoqueb6450b12014-04-24 00:01:03 -0500217 return 0;
akmhoque53353462014-04-22 08:43:45 -0500218}
219
220void
akmhoquedcee9362014-08-05 22:58:01 -0500221RoutingTable::writeLog(int hyperbolicState)
akmhoque674b0b12014-05-20 14:33:28 -0500222{
223 _LOG_DEBUG("---------------Routing Table------------------");
224 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
225 it != m_rTable.end(); ++it) {
226 _LOG_DEBUG("Destination: " << (*it).getDestination());
227 _LOG_DEBUG("Nexthops: ");
228 (*it).getNexthopList().writeLog();
229 }
akmhoquedcee9362014-08-05 22:58:01 -0500230
231 if (hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
232 _LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
233 for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ;
234 it != m_dryTable.end(); ++it) {
235 _LOG_DEBUG("Destination: " << (*it).getDestination());
236 _LOG_DEBUG("Nexthops: ");
237 (*it).getNexthopList().writeLog();
238 }
239 }
akmhoque674b0b12014-05-20 14:33:28 -0500240}
241
akmhoque53353462014-04-22 08:43:45 -0500242void
akmhoque31d1d4b2014-05-05 22:08:14 -0500243RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500244{
Vince Lehman9a709032014-09-13 16:28:07 -0500245 _LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
246
akmhoque53353462014-04-22 08:43:45 -0500247 std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
248 m_dryTable.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600249 std::bind(&routingTableEntryCompare,
akmhoque157b0a42014-05-13 00:26:37 -0500250 _1, destRouter));
251 if (it == m_dryTable.end()) {
akmhoque53353462014-04-22 08:43:45 -0500252 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500253 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500254 m_dryTable.push_back(rte);
255 }
akmhoque157b0a42014-05-13 00:26:37 -0500256 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500257 (*it).getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500258 }
259}
260
261void
akmhoque53353462014-04-22 08:43:45 -0500262RoutingTable::clearRoutingTable()
263{
akmhoque157b0a42014-05-13 00:26:37 -0500264 if (m_rTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500265 m_rTable.clear();
266 }
267}
268
269void
270RoutingTable::clearDryRoutingTable()
271{
akmhoque157b0a42014-05-13 00:26:37 -0500272 if (m_dryTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500273 m_dryTable.clear();
274 }
275}
276
Nick Gordonfad8e252016-08-11 14:21:38 -0500277} // namespace nlsr