Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 19 | |
Alexander Afanasyev | 3898e1b | 2013-07-27 12:03:17 -0700 | [diff] [blame] | 20 | #if __clang__ |
| 21 | #pragma clang diagnostic push |
| 22 | #pragma clang diagnostic ignored "-Wunused-variable" |
| 23 | #pragma clang diagnostic ignored "-Wunneeded-internal-declaration" |
| 24 | #endif |
| 25 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 26 | #include "ndn-global-routing-helper.hpp" |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 27 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 28 | #include "model/ndn-l3-protocol.hpp" |
| 29 | #include "helper/ndn-fib-helper.hpp" |
| 30 | #include "model/ndn-net-device-face.hpp" |
| 31 | #include "model/ndn-global-router.hpp" |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 32 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 33 | #include "daemon/table/fib.hpp" |
| 34 | #include "daemon/fw/forwarder.hpp" |
| 35 | #include "daemon/table/fib-entry.hpp" |
| 36 | #include "daemon/table/fib-nexthop.hpp" |
| 37 | |
| 38 | #include "ns3/object.h" |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 39 | #include "ns3/node.h" |
Alexander Afanasyev | ce81014 | 2012-04-17 15:50:36 -0700 | [diff] [blame] | 40 | #include "ns3/node-container.h" |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 41 | #include "ns3/net-device.h" |
| 42 | #include "ns3/channel.h" |
| 43 | #include "ns3/log.h" |
| 44 | #include "ns3/assert.h" |
| 45 | #include "ns3/names.h" |
| 46 | #include "ns3/node-list.h" |
| 47 | #include "ns3/channel-list.h" |
Alexander Afanasyev | f5c0774 | 2012-10-31 13:13:05 -0700 | [diff] [blame] | 48 | #include "ns3/object-factory.h" |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 49 | |
| 50 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 51 | #include <boost/foreach.hpp> |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 52 | #include <boost/concept/assert.hpp> |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 53 | #include <boost/graph/dijkstra_shortest_paths.hpp> |
| 54 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 55 | #include "boost-graph-ndn-global-routing-helper.hpp" |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 56 | |
Alexander Afanasyev | 7353251 | 2012-11-27 00:42:35 -0800 | [diff] [blame] | 57 | #include <math.h> |
| 58 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 59 | NS_LOG_COMPONENT_DEFINE("ndn.GlobalRoutingHelper"); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 60 | |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 61 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 62 | namespace ndn { |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 63 | |
| 64 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 65 | GlobalRoutingHelper::Install(Ptr<Node> node) |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 66 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 67 | NS_LOG_LOGIC("Node: " << node->GetId()); |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 69 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 70 | NS_ASSERT_MSG(ndn != 0, "Cannot install GlobalRoutingHelper before Ndn is installed on a node"); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 71 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 72 | Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter>(); |
| 73 | if (gr != 0) { |
| 74 | NS_LOG_DEBUG("GlobalRouter is already installed: " << gr); |
| 75 | return; // already installed |
| 76 | } |
| 77 | |
| 78 | gr = CreateObject<GlobalRouter>(); |
| 79 | node->AggregateObject(gr); |
| 80 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 81 | for (auto& i : ndn->getForwarder()->getFaceTable()) { |
| 82 | shared_ptr<NetDeviceFace> face = std::dynamic_pointer_cast<NetDeviceFace>(i); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 83 | if (face == 0) { |
| 84 | NS_LOG_DEBUG("Skipping non-netdevice face"); |
| 85 | continue; |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 86 | } |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | Ptr<NetDevice> nd = face->GetNetDevice(); |
| 89 | if (nd == 0) { |
| 90 | NS_LOG_DEBUG("Not a NetDevice associated with NetDeviceFace"); |
| 91 | continue; |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 92 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 93 | |
| 94 | Ptr<Channel> ch = nd->GetChannel(); |
| 95 | |
| 96 | if (ch == 0) { |
| 97 | NS_LOG_DEBUG("Channel is not associated with NetDevice"); |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | if (ch->GetNDevices() == 2) // e.g., point-to-point channel |
| 102 | { |
| 103 | for (uint32_t deviceId = 0; deviceId < ch->GetNDevices(); deviceId++) { |
| 104 | Ptr<NetDevice> otherSide = ch->GetDevice(deviceId); |
| 105 | if (nd == otherSide) |
| 106 | continue; |
| 107 | |
| 108 | Ptr<Node> otherNode = otherSide->GetNode(); |
| 109 | NS_ASSERT(otherNode != 0); |
| 110 | |
| 111 | Ptr<GlobalRouter> otherGr = otherNode->GetObject<GlobalRouter>(); |
| 112 | if (otherGr == 0) { |
| 113 | Install(otherNode); |
| 114 | } |
| 115 | otherGr = otherNode->GetObject<GlobalRouter>(); |
| 116 | NS_ASSERT(otherGr != 0); |
| 117 | gr->AddIncidency(face, otherGr); |
| 118 | } |
| 119 | } |
| 120 | else { |
| 121 | Ptr<GlobalRouter> grChannel = ch->GetObject<GlobalRouter>(); |
| 122 | if (grChannel == 0) { |
| 123 | Install(ch); |
| 124 | } |
| 125 | grChannel = ch->GetObject<GlobalRouter>(); |
| 126 | |
| 127 | gr->AddIncidency(face, grChannel); |
| 128 | } |
| 129 | } |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 133 | GlobalRoutingHelper::Install(Ptr<Channel> channel) |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 134 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 135 | NS_LOG_LOGIC("Channel: " << channel->GetId()); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 136 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 137 | Ptr<GlobalRouter> gr = channel->GetObject<GlobalRouter>(); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 138 | if (gr != 0) |
| 139 | return; |
| 140 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 141 | gr = CreateObject<GlobalRouter>(); |
| 142 | channel->AggregateObject(gr); |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 143 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 144 | for (uint32_t deviceId = 0; deviceId < channel->GetNDevices(); deviceId++) { |
| 145 | Ptr<NetDevice> dev = channel->GetDevice(deviceId); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 146 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 147 | Ptr<Node> node = dev->GetNode(); |
| 148 | NS_ASSERT(node != 0); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 149 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 150 | Ptr<GlobalRouter> grOther = node->GetObject<GlobalRouter>(); |
| 151 | if (grOther == 0) { |
| 152 | Install(node); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 153 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 154 | grOther = node->GetObject<GlobalRouter>(); |
| 155 | NS_ASSERT(grOther != 0); |
| 156 | |
| 157 | gr->AddIncidency(0, grOther); |
| 158 | } |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 162 | GlobalRoutingHelper::Install(const NodeContainer& nodes) |
Alexander Afanasyev | ce81014 | 2012-04-17 15:50:36 -0700 | [diff] [blame] | 163 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 164 | for (NodeContainer::Iterator node = nodes.Begin(); node != nodes.End(); node++) { |
| 165 | Install(*node); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | GlobalRoutingHelper::InstallAll() |
| 171 | { |
| 172 | Install(NodeContainer::GetGlobal()); |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | GlobalRoutingHelper::AddOrigin(const std::string& prefix, Ptr<Node> node) |
| 177 | { |
| 178 | Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter>(); |
| 179 | NS_ASSERT_MSG(gr != 0, "GlobalRouter is not installed on the node"); |
| 180 | |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 181 | auto name = make_shared<Name>(prefix); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 182 | gr->AddLocalPrefix(name); |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | GlobalRoutingHelper::AddOrigins(const std::string& prefix, const NodeContainer& nodes) |
| 187 | { |
| 188 | for (NodeContainer::Iterator node = nodes.Begin(); node != nodes.End(); node++) { |
| 189 | AddOrigin(prefix, *node); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | GlobalRoutingHelper::AddOrigin(const std::string& prefix, const std::string& nodeName) |
| 195 | { |
| 196 | Ptr<Node> node = Names::Find<Node>(nodeName); |
| 197 | NS_ASSERT_MSG(node != 0, nodeName << "is not a Node"); |
| 198 | |
| 199 | AddOrigin(prefix, node); |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | GlobalRoutingHelper::AddOriginsForAll() |
| 204 | { |
| 205 | for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) { |
| 206 | Ptr<GlobalRouter> gr = (*node)->GetObject<GlobalRouter>(); |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 207 | std::string name = Names::FindName(*node); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 208 | |
| 209 | if (gr != 0 && !name.empty()) { |
| 210 | AddOrigin("/" + name, *node); |
Alexander Afanasyev | ce81014 | 2012-04-17 15:50:36 -0700 | [diff] [blame] | 211 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 212 | } |
Alexander Afanasyev | ce81014 | 2012-04-17 15:50:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 216 | GlobalRoutingHelper::CalculateRoutes(bool invalidatedRoutes /* = true*/) |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 217 | { |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 218 | /** |
| 219 | * Implementation of route calculation is heavily based on Boost Graph Library |
| 220 | * See http://www.boost.org/doc/libs/1_49_0/libs/graph/doc/table_of_contents.html for more details |
| 221 | */ |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 222 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 223 | BOOST_CONCEPT_ASSERT((boost::VertexListGraphConcept<boost::NdnGlobalRouterGraph>)); |
| 224 | BOOST_CONCEPT_ASSERT((boost::IncidenceGraphConcept<boost::NdnGlobalRouterGraph>)); |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 225 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 226 | boost::NdnGlobalRouterGraph graph; |
Zongyi Zhao | c837263 | 2014-04-28 15:36:49 -0700 | [diff] [blame] | 227 | // typedef graph_traits < NdnGlobalRouterGraph >::vertex_descriptor vertex_descriptor; |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 228 | |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 229 | // For now we doing Dijkstra for every node. Can be replaced with Bellman-Ford or Floyd-Warshall. |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 230 | // Other algorithms should be faster, but they need additional EdgeListGraph concept provided by |
| 231 | // the graph, which |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 232 | // is not obviously how implement in an efficient manner |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 233 | for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) { |
| 234 | Ptr<GlobalRouter> source = (*node)->GetObject<GlobalRouter>(); |
| 235 | if (source == 0) { |
| 236 | NS_LOG_DEBUG("Node " << (*node)->GetId() << " does not export GlobalRouter interface"); |
| 237 | continue; |
| 238 | } |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 239 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 240 | boost::DistancesMap distances; |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 241 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 242 | dijkstra_shortest_paths(graph, source, |
| 243 | // predecessor_map (boost::ref(predecessors)) |
| 244 | // . |
| 245 | distance_map(boost::ref(distances)) |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 246 | .distance_inf(boost::WeightInf) |
| 247 | .distance_zero(boost::WeightZero) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 248 | .distance_compare(boost::WeightCompare()) |
| 249 | .distance_combine(boost::WeightCombine())); |
| 250 | |
| 251 | // NS_LOG_DEBUG (predecessors.size () << ", " << distances.size ()); |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 252 | |
| 253 | Ptr<L3Protocol> L3protocol = (*node)->GetObject<L3Protocol>(); |
| 254 | shared_ptr<nfd::Forwarder> forwarder = L3protocol->getForwarder(); |
| 255 | |
| 256 | if (invalidatedRoutes) { |
| 257 | std::vector<::nfd::fib::NextHop> NextHopList; |
| 258 | for (nfd::Fib::const_iterator fibIt = forwarder->getFib().begin(); |
| 259 | fibIt != forwarder->getFib().end();) { |
| 260 | NextHopList.clear(); |
| 261 | NextHopList = fibIt->getNextHops(); |
| 262 | ++fibIt; |
| 263 | for (int i = 0; i < NextHopList.size(); i++) { |
| 264 | NextHopList[i].setCost(std::numeric_limits<uint64_t>::max()); |
| 265 | } |
Spyridon Mastorakis | e4f0d3c | 2014-10-29 13:20:03 -0700 | [diff] [blame] | 266 | } |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 269 | NS_LOG_DEBUG("Reachability from Node: " << source->GetObject<Node>()->GetId()); |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 270 | for (const auto& dist : distances) { |
| 271 | if (dist.first == source) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 272 | continue; |
| 273 | else { |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 274 | // cout << " Node " << dist.first->GetObject<Node> ()->GetId (); |
| 275 | if (std::get<0>(dist.second) == 0) { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 276 | // cout << " is unreachable" << endl; |
| 277 | } |
| 278 | else { |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 279 | for (const auto& prefix : dist.first->GetLocalPrefixes()) { |
| 280 | NS_LOG_DEBUG(" prefix " << prefix << " reachable via face " << *std::get<0>(dist.second) |
| 281 | << " with distance " << std::get<1>(dist.second) << " with delay " |
| 282 | << std::get<2>(dist.second)); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 283 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 284 | FibHelper::AddRoute(*node, *prefix, std::get<0>(dist.second), |
| 285 | std::get<1>(dist.second)); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | void |
| 294 | GlobalRoutingHelper::CalculateAllPossibleRoutes(bool invalidatedRoutes /* = true*/) |
| 295 | { |
| 296 | /** |
| 297 | * Implementation of route calculation is heavily based on Boost Graph Library |
| 298 | * See http://www.boost.org/doc/libs/1_49_0/libs/graph/doc/table_of_contents.html for more details |
| 299 | */ |
| 300 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 301 | BOOST_CONCEPT_ASSERT((boost::VertexListGraphConcept<boost::NdnGlobalRouterGraph>)); |
| 302 | BOOST_CONCEPT_ASSERT((boost::IncidenceGraphConcept<boost::NdnGlobalRouterGraph>)); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 303 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 304 | boost::NdnGlobalRouterGraph graph; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 305 | // typedef graph_traits < NdnGlobalRouterGraph >::vertex_descriptor vertex_descriptor; |
| 306 | |
| 307 | // For now we doing Dijkstra for every node. Can be replaced with Bellman-Ford or Floyd-Warshall. |
| 308 | // Other algorithms should be faster, but they need additional EdgeListGraph concept provided by |
| 309 | // the graph, which |
| 310 | // is not obviously how implement in an efficient manner |
| 311 | for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) { |
| 312 | Ptr<GlobalRouter> source = (*node)->GetObject<GlobalRouter>(); |
| 313 | if (source == 0) { |
| 314 | NS_LOG_DEBUG("Node " << (*node)->GetId() << " does not export GlobalRouter interface"); |
| 315 | continue; |
| 316 | } |
| 317 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 318 | Ptr<L3Protocol> L3protocol = (*node)->GetObject<L3Protocol>(); |
| 319 | shared_ptr<nfd::Forwarder> forwarder = L3protocol->getForwarder(); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 320 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 321 | if (invalidatedRoutes) { |
| 322 | std::vector<::nfd::fib::NextHop> NextHopList; |
| 323 | for (nfd::Fib::const_iterator fibIt = forwarder->getFib().begin(); |
| 324 | fibIt != forwarder->getFib().end();) { |
| 325 | NextHopList.clear(); |
| 326 | NextHopList = fibIt->getNextHops(); |
| 327 | ++fibIt; |
| 328 | for (int i = 0; i < NextHopList.size(); i++) { |
| 329 | NextHopList[i].setCost(std::numeric_limits<uint64_t>::max()); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 334 | NS_LOG_DEBUG("Reachability from Node: " << source->GetObject<Node>()->GetId() << " (" |
| 335 | << Names::FindName(source->GetObject<Node>()) << ")"); |
| 336 | |
| 337 | Ptr<L3Protocol> l3 = source->GetObject<L3Protocol>(); |
| 338 | NS_ASSERT(l3 != 0); |
| 339 | |
| 340 | // remember interface statuses |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 341 | int faceNumber = 0; |
| 342 | std::vector<uint16_t> originalMetric(uint32_t(l3->getForwarder()->getFaceTable().size())); |
| 343 | for (auto& i : l3->getForwarder()->getFaceTable()) { |
| 344 | faceNumber++; |
| 345 | shared_ptr<Face> nfdFace = std::dynamic_pointer_cast<Face>(i); |
| 346 | originalMetric[uint32_t(faceNumber)] = nfdFace->getMetric(); |
| 347 | nfdFace->setMetric(std::numeric_limits<uint16_t>::max() - 1); |
| 348 | // value std::numeric_limits<uint16_t>::max () MUST NOT be used (reserved) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 351 | faceNumber = 0; |
| 352 | for (auto& k : l3->getForwarder()->getFaceTable()) { |
| 353 | faceNumber++; |
| 354 | shared_ptr<NetDeviceFace> face = std::dynamic_pointer_cast<NetDeviceFace>(k); |
| 355 | if (face == 0) { |
| 356 | NS_LOG_DEBUG("Skipping non-netdevice face"); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 357 | continue; |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 358 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 359 | |
| 360 | // enabling only faceId |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 361 | face->setMetric(originalMetric[uint32_t(faceNumber)]); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 362 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 363 | boost::DistancesMap distances; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 364 | |
| 365 | NS_LOG_DEBUG("-----------"); |
| 366 | |
| 367 | dijkstra_shortest_paths(graph, source, |
| 368 | // predecessor_map (boost::ref(predecessors)) |
| 369 | // . |
| 370 | distance_map(boost::ref(distances)) |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 371 | .distance_inf(boost::WeightInf) |
| 372 | .distance_zero(boost::WeightZero) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 373 | .distance_compare(boost::WeightCompare()) |
| 374 | .distance_combine(boost::WeightCombine())); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 375 | |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 376 | // NS_LOG_DEBUG (predecessors.size () << ", " << distances.size ()); |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 377 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 378 | for (const auto& dist : distances) { |
| 379 | if (dist.first == source) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 380 | continue; |
| 381 | else { |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 382 | // cout << " Node " << dist.first->GetObject<Node> ()->GetId (); |
| 383 | if (std::get<0>(dist.second) == 0) { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 384 | // cout << " is unreachable" << endl; |
| 385 | } |
| 386 | else { |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 387 | for (const auto& prefix : dist.first->GetLocalPrefixes()) { |
| 388 | NS_LOG_DEBUG(" prefix " << *prefix << " reachable via face " |
| 389 | << *std::get<0>(dist.second) |
| 390 | << " with distance " << std::get<1>(dist.second) |
| 391 | << " with delay " << std::get<2>(dist.second)); |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 392 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 393 | if (std::get<0>(dist.second)->getMetric() == std::numeric_limits<uint16_t>::max() - 1) |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 394 | continue; |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 395 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 396 | FibHelper::AddRoute(*node, *prefix, std::get<0>(dist.second), |
| 397 | std::get<1>(dist.second)); |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 398 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 399 | } |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 400 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 401 | } |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 402 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 403 | // disabling the face again |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 404 | face->setMetric(std::numeric_limits<uint16_t>::max() - 1); |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 405 | } |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 406 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 407 | // recover original interface statuses |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 408 | faceNumber = 0; |
| 409 | for (auto& i : l3->getForwarder()->getFaceTable()) { |
| 410 | faceNumber++; |
| 411 | shared_ptr<Face> face = std::dynamic_pointer_cast<Face>(i); |
| 412 | face->setMetric(originalMetric[faceNumber]); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | } |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 416 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 417 | } // namespace ndn |
| 418 | } // namespace ns3 |