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