routing-table.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, The 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 
21 #include "routing-table.hpp"
22 #include "nlsr.hpp"
23 #include "map.hpp"
24 #include "conf-parameter.hpp"
26 #include "routing-table-entry.hpp"
27 #include "name-prefix-table.hpp"
28 #include "logger.hpp"
29 
30 #include <list>
31 #include <string>
32 
33 namespace nlsr {
34 
35 INIT_LOGGER(route.RoutingTable);
36 
37 RoutingTable::RoutingTable(ndn::Scheduler& scheduler, Fib& fib, Lsdb& lsdb,
38  NamePrefixTable& namePrefixTable, ConfParameter& confParam)
39  : afterRoutingChange{std::make_unique<AfterRoutingChange>()}
40  , m_scheduler(scheduler)
41  , m_fib(fib)
42  , m_lsdb(lsdb)
43  , m_namePrefixTable(namePrefixTable)
44  , m_routingCalcInterval{confParam.getRoutingCalcInterval()}
45  , m_isRoutingTableCalculating(false)
46  , m_isRouteCalculationScheduled(false)
47  , m_confParam(confParam)
48 {
49 }
50 
51 void
53 {
54  m_lsdb.writeCorLsdbLog();
55  m_lsdb.writeNameLsdbLog();
56  m_lsdb.writeAdjLsdbLog();
57  m_namePrefixTable.writeLog();
58  if (m_isRoutingTableCalculating == false) {
59  // setting routing table calculation
60  m_isRoutingTableCalculating = true;
61 
62  bool isHrEnabled = m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF;
63 
64  if ((!isHrEnabled &&
65  m_lsdb
66  .doesLsaExist(ndn::Name{m_confParam.getRouterPrefix()}
67  .append(std::to_string(Lsa::Type::ADJACENCY)), Lsa::Type::ADJACENCY))
68  ||
69  (isHrEnabled &&
70  m_lsdb
71  .doesLsaExist(ndn::Name{m_confParam.getRouterPrefix()}
72  .append(std::to_string(Lsa::Type::COORDINATE)), Lsa::Type::COORDINATE))) {
73  if (m_lsdb.getIsBuildAdjLsaSheduled() != 1) {
74  NLSR_LOG_TRACE("Clearing old routing table");
75  clearRoutingTable();
76  // for dry run options
77  clearDryRoutingTable();
78 
79  NLSR_LOG_DEBUG("Calculating routing table");
80 
81  // calculate Link State routing
82  if ((m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF)
83  || (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
84  calculateLsRoutingTable();
85  }
86  // calculate hyperbolic routing
87  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
88  calculateHypRoutingTable(false);
89  }
90  // calculate dry hyperbolic routing
91  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
92  calculateHypRoutingTable(true);
93  }
94  // Inform the NPT that updates have been made
95  NLSR_LOG_DEBUG("Calling Update NPT With new Route");
96  (*afterRoutingChange)(m_rTable);
97  writeLog();
98  m_namePrefixTable.writeLog();
99  m_fib.writeLog();
100  }
101  else {
102  NLSR_LOG_DEBUG("Adjacency building is scheduled, so"
103  " routing table can not be calculated :(");
104  }
105  }
106  else {
107  NLSR_LOG_DEBUG("No Adj LSA of router itself,"
108  " so Routing table can not be calculated :(");
109  clearRoutingTable();
110  clearDryRoutingTable(); // for dry run options
111  // need to update NPT here
112  NLSR_LOG_DEBUG("Calling Update NPT With new Route");
113  (*afterRoutingChange)(m_rTable);
114  writeLog();
115  m_namePrefixTable.writeLog();
116  m_fib.writeLog();
117  // debugging purpose end
118  }
119  m_isRouteCalculationScheduled = false; // clear scheduled flag
120  m_isRoutingTableCalculating = false; // unsetting routing table calculation
121  }
122  else {
124  }
125 }
126 
127 void
128 RoutingTable::calculateLsRoutingTable()
129 {
130  NLSR_LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called");
131 
132  Map map;
133  map.createFromAdjLsdb(m_lsdb.getAdjLsdb().begin(), m_lsdb.getAdjLsdb().end());
134  map.writeLog();
135 
136  size_t nRouters = map.getMapSize();
137 
138  LinkStateRoutingTableCalculator calculator(nRouters);
139 
140  calculator.calculatePath(map, *this, m_confParam, m_lsdb.getAdjLsdb());
141 }
142 
143 void
144 RoutingTable::calculateHypRoutingTable(bool isDryRun)
145 {
146  Map map;
147  map.createFromCoordinateLsdb(m_lsdb.getCoordinateLsdb().begin(),
148  m_lsdb.getCoordinateLsdb().end());
149  map.writeLog();
150 
151  size_t nRouters = map.getMapSize();
152 
153  HyperbolicRoutingCalculator calculator(nRouters, isDryRun, m_confParam.getRouterPrefix());
154 
155  calculator.calculatePath(map, *this, m_lsdb, m_confParam.getAdjacencyList());
156 }
157 
158 void
160 {
161  if (!m_isRouteCalculationScheduled) {
162  NLSR_LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
163  m_scheduler.schedule(m_routingCalcInterval, [this] { calculate(); });
164  m_isRouteCalculationScheduled = true;
165  }
166 }
167 
168 static bool
169 routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
170 {
171  return rte.getDestination() == destRouter;
172 }
173 
174 void
175 RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
176 {
177  NLSR_LOG_DEBUG("Adding " << nh << " for destination: " << destRouter);
178 
179  RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
180  if (rteChk == nullptr) {
181  RoutingTableEntry rte(destRouter);
182  rte.getNexthopList().addNextHop(nh);
183  m_rTable.push_back(rte);
184  }
185  else {
186  rteChk->getNexthopList().addNextHop(nh);
187  }
188 }
189 
191 RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
192 {
193  auto it = std::find_if(m_rTable.begin(), m_rTable.end(),
194  std::bind(&routingTableEntryCompare, _1, destRouter));
195  if (it != m_rTable.end()) {
196  return &(*it);
197  }
198  return nullptr;
199 }
200 
201 void
202 RoutingTable::writeLog()
203 {
204  NLSR_LOG_DEBUG("---------------Routing Table------------------");
205  for (const auto& rte : m_rTable) {
206  NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
207  NLSR_LOG_DEBUG("Nexthops: ");
208  rte.getNexthopList().writeLog();
209  }
210 
211  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
212  NLSR_LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
213  for (const auto& rte : m_dryTable) {
214  NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
215  NLSR_LOG_DEBUG("Nexthops: ");
216  rte.getNexthopList().writeLog();
217  }
218  }
219 }
220 
221 void
222 RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
223 {
224  NLSR_LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
225 
226  auto it = std::find_if(m_dryTable.begin(), m_dryTable.end(),
227  std::bind(&routingTableEntryCompare, _1, destRouter));
228  if (it == m_dryTable.end()) {
229  RoutingTableEntry rte(destRouter);
230  rte.getNexthopList().addNextHop(nh);
231  m_dryTable.push_back(rte);
232  }
233  else {
234  it->getNexthopList().addNextHop(nh);
235  }
236 }
237 
238 void
239 RoutingTable::clearRoutingTable()
240 {
241  if (m_rTable.size() > 0) {
242  m_rTable.clear();
243  }
244 }
245 
246 void
247 RoutingTable::clearDryRoutingTable()
248 {
249  if (m_dryTable.size() > 0) {
250  m_dryTable.clear();
251  }
252 }
253 
254 } // namespace nlsr
void writeLog()
Definition: fib.cpp:376
void calculate()
Calculates a list of next hops for each router in the network.
A class to house all the configuration parameters for NLSR.
void writeNameLsdbLog()
Definition: lsdb.cpp:335
void calculatePath(Map &map, RoutingTable &rt, Lsdb &lsdb, AdjacencyList &adjacencies)
size_t getMapSize() const
Definition: map.hpp:115
void createFromCoordinateLsdb(IteratorType begin, IteratorType end)
Definition: map.hpp:97
void writeAdjLsdbLog()
Definition: lsdb.cpp:1271
void scheduleRoutingTableCalculation()
Schedules a calculation event in the event scheduler only if one isn&#39;t already scheduled.
AdjacencyList & getAdjacencyList()
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
const ndn::Name & getRouterPrefix() const
RoutingTable(ndn::Scheduler &scheduler, Fib &fib, Lsdb &lsdb, NamePrefixTable &namePrefixTable, ConfParameter &confParam)
void writeCorLsdbLog()
Definition: lsdb.cpp:524
bool getIsBuildAdjLsaSheduled()
Definition: lsdb.hpp:205
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
Maps names to lists of next hops, and exports this information to NFD.
Definition: fib.hpp:52
#define INIT_LOGGER(name)
Definition: logger.hpp:35
const std::list< CoordinateLsa > & getCoordinateLsdb() const
Definition: lsdb.cpp:537
static bool routingTableEntryCompare(RoutingTableEntry &rte, ndn::Name &destRouter)
const ndn::Name & getDestination() const
void writeLog()
Definition: map.cpp:81
bool doesLsaExist(const ndn::Name &key, const Lsa::Type &lsType)
Definition: lsdb.cpp:1285
void createFromAdjLsdb(IteratorType begin, IteratorType end)
Definition: map.hpp:80
Copyright (c) 2014-2019, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
void calculatePath(Map &pMap, RoutingTable &rt, ConfParameter &confParam, const std::list< AdjLsa > &adjLsaList)
void addNextHop(const NextHop &nh)
Adds a next hop to the list.
void addNextHopToDryTable(const ndn::Name &destRouter, NextHop &nh)
Adds a next hop to a routing table entry in a dry run scenario.
RoutingTableEntry * findRoutingTableEntry(const ndn::Name &destRouter)
int32_t getHyperbolicState() const
void addNextHop(const ndn::Name &destRouter, NextHop &nh)
Adds a next hop to a routing table entry.
const std::list< AdjLsa > & getAdjLsdb() const
Definition: lsdb.cpp:774
#define NLSR_LOG_TRACE(x)
Definition: logger.hpp:37