akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 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 | * \author A K M Mahmudul Hoque <ahoque1@memphis.edu> |
| 21 | * |
| 22 | **/ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 23 | #include <iostream> |
| 24 | #include <string> |
| 25 | #include <list> |
| 26 | |
| 27 | #include "routing-table.hpp" |
| 28 | #include "nlsr.hpp" |
| 29 | #include "map.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 30 | #include "conf-parameter.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | #include "routing-table-calculator.hpp" |
| 32 | #include "routing-table-entry.hpp" |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 33 | #include "name-prefix-table.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 34 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | |
| 36 | namespace nlsr { |
| 37 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 38 | INIT_LOGGER("RoutingTable"); |
| 39 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 40 | using namespace std; |
| 41 | |
| 42 | void |
| 43 | RoutingTable::calculate(Nlsr& pnlsr) |
| 44 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 45 | pnlsr.getLsdb().writeCorLsdbLog(); |
| 46 | pnlsr.getLsdb().writeNameLsdbLog(); |
| 47 | pnlsr.getLsdb().writeAdjLsdbLog(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 48 | pnlsr.getNamePrefixTable().writeLog(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 49 | if (pnlsr.getIsRoutingTableCalculating() == false) { |
| 50 | //setting routing table calculation |
| 51 | pnlsr.setIsRoutingTableCalculating(true); |
Nick Gordon | e8e03ac | 2016-07-07 14:24:38 -0500 | [diff] [blame] | 52 | |
| 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")))) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 66 | if (pnlsr.getIsBuildAdjLsaSheduled() != 1) { |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 67 | _LOG_TRACE("Clearing old routing table"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 68 | clearRoutingTable(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 69 | // for dry run options |
| 70 | clearDryRoutingTable(); |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 71 | |
| 72 | _LOG_DEBUG("Calculating routing table"); |
| 73 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 74 | // calculate Link State routing |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 75 | if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF) |
| 76 | || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 77 | calculateLsRoutingTable(pnlsr); |
| 78 | } |
| 79 | //calculate hyperbolic routing |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 80 | if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 81 | calculateHypRoutingTable(pnlsr); |
| 82 | } |
| 83 | //calculate dry hyperbolic routing |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 84 | if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 85 | calculateHypDryRoutingTable(pnlsr); |
| 86 | } |
| 87 | //need to update NPT here |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 88 | _LOG_DEBUG("Calling Update NPT With new Route"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 89 | pnlsr.getNamePrefixTable().updateWithNewRoute(); |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 90 | writeLog(pnlsr.getConfParameter().getHyperbolicState()); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 91 | pnlsr.getNamePrefixTable().writeLog(); |
| 92 | pnlsr.getFib().writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 93 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 94 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 95 | _LOG_DEBUG("Adjacency building is scheduled, so" |
| 96 | " routing table can not be calculated :("); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 97 | } |
| 98 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 99 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 100 | _LOG_DEBUG("No Adj LSA of router itself," |
| 101 | " so Routing table can not be calculated :("); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 102 | clearRoutingTable(); |
| 103 | clearDryRoutingTable(); // for dry run options |
| 104 | // need to update NPT here |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 105 | _LOG_DEBUG("Calling Update NPT With new Route"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 106 | pnlsr.getNamePrefixTable().updateWithNewRoute(); |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 107 | writeLog(pnlsr.getConfParameter().getHyperbolicState()); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 108 | pnlsr.getNamePrefixTable().writeLog(); |
| 109 | pnlsr.getFib().writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 110 | //debugging purpose end |
| 111 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 112 | pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag |
| 113 | pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 114 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 115 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 116 | scheduleRoutingTableCalculation(pnlsr); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 122 | RoutingTable::calculateLsRoutingTable(Nlsr& nlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 123 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 124 | _LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called"); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 125 | |
| 126 | Map map; |
| 127 | map.createFromAdjLsdb(nlsr); |
| 128 | map.writeLog(); |
| 129 | |
| 130 | size_t nRouters = map.getMapSize(); |
| 131 | |
| 132 | LinkStateRoutingTableCalculator calculator(nRouters); |
| 133 | |
| 134 | calculator.calculatePath(map, ndn::ref(*this), nlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 138 | RoutingTable::calculateHypRoutingTable(Nlsr& nlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 139 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 140 | Map map; |
Nick Gordon | e8e03ac | 2016-07-07 14:24:38 -0500 | [diff] [blame] | 141 | map.createFromCoordinateLsdb(nlsr); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 142 | map.writeLog(); |
| 143 | |
| 144 | size_t nRouters = map.getMapSize(); |
| 145 | |
| 146 | HyperbolicRoutingCalculator calculator(nRouters, false, |
| 147 | nlsr.getConfParameter().getRouterPrefix()); |
| 148 | |
| 149 | calculator.calculatePaths(map, ndn::ref(*this), |
| 150 | nlsr.getLsdb(), nlsr.getAdjacencyList()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 154 | RoutingTable::calculateHypDryRoutingTable(Nlsr& nlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 155 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 156 | Map map; |
| 157 | map.createFromAdjLsdb(nlsr); |
| 158 | map.writeLog(); |
| 159 | |
| 160 | size_t nRouters = map.getMapSize(); |
| 161 | |
| 162 | HyperbolicRoutingCalculator calculator(nRouters, true, |
| 163 | nlsr.getConfParameter().getRouterPrefix()); |
| 164 | |
| 165 | calculator.calculatePaths(map, ndn::ref(*this), |
| 166 | nlsr.getLsdb(), nlsr.getAdjacencyList()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void |
| 170 | RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr) |
| 171 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 172 | if (pnlsr.getIsRouteCalculationScheduled() != true) { |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 173 | _LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval); |
| 174 | |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 175 | m_scheduler.scheduleEvent(m_routingCalcInterval, |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 176 | ndn::bind(&RoutingTable::calculate, this, ndn::ref(pnlsr))); |
| 177 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 178 | pnlsr.setIsRouteCalculationScheduled(true); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 183 | routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 184 | { |
| 185 | return rte.getDestination() == destRouter; |
| 186 | } |
| 187 | |
| 188 | // function related to manipulation of routing table |
| 189 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 190 | RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 191 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 192 | _LOG_DEBUG("Adding " << nh << " for destination: " << destRouter); |
| 193 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 194 | RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 195 | if (rteChk == 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 196 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 197 | rte.getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 198 | m_rTable.push_back(rte); |
| 199 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 200 | else { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 201 | rteChk->getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 205 | RoutingTableEntry* |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 206 | RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 207 | { |
| 208 | std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(), |
| 209 | m_rTable.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 210 | ndn::bind(&routingTableEntryCompare, |
| 211 | _1, destRouter)); |
| 212 | if (it != m_rTable.end()) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 213 | return &(*it); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 214 | } |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 215 | return 0; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 219 | RoutingTable::writeLog(int hyperbolicState) |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 220 | { |
| 221 | _LOG_DEBUG("---------------Routing Table------------------"); |
| 222 | for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ; |
| 223 | it != m_rTable.end(); ++it) { |
| 224 | _LOG_DEBUG("Destination: " << (*it).getDestination()); |
| 225 | _LOG_DEBUG("Nexthops: "); |
| 226 | (*it).getNexthopList().writeLog(); |
| 227 | } |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 228 | |
| 229 | if (hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) { |
| 230 | _LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------"); |
| 231 | for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ; |
| 232 | it != m_dryTable.end(); ++it) { |
| 233 | _LOG_DEBUG("Destination: " << (*it).getDestination()); |
| 234 | _LOG_DEBUG("Nexthops: "); |
| 235 | (*it).getNexthopList().writeLog(); |
| 236 | } |
| 237 | } |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 238 | } |
| 239 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 240 | //function related to manipulation of dry routing table |
| 241 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 242 | RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 243 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 244 | _LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter); |
| 245 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 246 | std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(), |
| 247 | m_dryTable.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 248 | ndn::bind(&routingTableEntryCompare, |
| 249 | _1, destRouter)); |
| 250 | if (it == m_dryTable.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 251 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 252 | rte.getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 253 | m_dryTable.push_back(rte); |
| 254 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 255 | else { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 256 | (*it).getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 261 | RoutingTable::clearRoutingTable() |
| 262 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 263 | if (m_rTable.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 264 | m_rTable.clear(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void |
| 269 | RoutingTable::clearDryRoutingTable() |
| 270 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 271 | if (m_dryTable.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 272 | m_dryTable.clear(); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | }//namespace nlsr |
| 277 | |