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" |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 30 | #include "model/ndn-net-device-link-service.hpp" |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 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 | |
Christian Kreuzberger | 8a56e8d | 2015-02-18 08:45:01 -0800 | [diff] [blame] | 55 | #include <unordered_map> |
| 56 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 57 | #include "boost-graph-ndn-global-routing-helper.hpp" |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | 7353251 | 2012-11-27 00:42:35 -0800 | [diff] [blame] | 59 | #include <math.h> |
| 60 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 61 | NS_LOG_COMPONENT_DEFINE("ndn.GlobalRoutingHelper"); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 62 | |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 63 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 64 | namespace ndn { |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 65 | |
| 66 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 67 | GlobalRoutingHelper::Install(Ptr<Node> node) |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 68 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 69 | NS_LOG_LOGIC("Node: " << node->GetId()); |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 70 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 71 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol>(); |
| 72 | 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] | 73 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 74 | Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter>(); |
| 75 | if (gr != 0) { |
| 76 | NS_LOG_DEBUG("GlobalRouter is already installed: " << gr); |
| 77 | return; // already installed |
| 78 | } |
| 79 | |
| 80 | gr = CreateObject<GlobalRouter>(); |
| 81 | node->AggregateObject(gr); |
| 82 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 83 | for (auto& face : ndn->getForwarder()->getFaceTable()) { |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame^] | 84 | auto linkService = dynamic_cast<NetDeviceLinkService*>(face.getLinkService()); |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 85 | if (linkService == nullptr) { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 86 | NS_LOG_DEBUG("Skipping non-netdevice face"); |
| 87 | continue; |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 88 | } |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 90 | Ptr<NetDevice> nd = linkService->GetNetDevice(); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 91 | if (nd == 0) { |
| 92 | NS_LOG_DEBUG("Not a NetDevice associated with NetDeviceFace"); |
| 93 | continue; |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 94 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 95 | |
| 96 | Ptr<Channel> ch = nd->GetChannel(); |
| 97 | |
| 98 | if (ch == 0) { |
| 99 | NS_LOG_DEBUG("Channel is not associated with NetDevice"); |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | if (ch->GetNDevices() == 2) // e.g., point-to-point channel |
| 104 | { |
| 105 | for (uint32_t deviceId = 0; deviceId < ch->GetNDevices(); deviceId++) { |
| 106 | Ptr<NetDevice> otherSide = ch->GetDevice(deviceId); |
| 107 | if (nd == otherSide) |
| 108 | continue; |
| 109 | |
| 110 | Ptr<Node> otherNode = otherSide->GetNode(); |
| 111 | NS_ASSERT(otherNode != 0); |
| 112 | |
| 113 | Ptr<GlobalRouter> otherGr = otherNode->GetObject<GlobalRouter>(); |
| 114 | if (otherGr == 0) { |
| 115 | Install(otherNode); |
| 116 | } |
| 117 | otherGr = otherNode->GetObject<GlobalRouter>(); |
| 118 | NS_ASSERT(otherGr != 0); |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame^] | 119 | gr->AddIncidency(face.shared_from_this(), otherGr); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | else { |
| 123 | Ptr<GlobalRouter> grChannel = ch->GetObject<GlobalRouter>(); |
| 124 | if (grChannel == 0) { |
| 125 | Install(ch); |
| 126 | } |
| 127 | grChannel = ch->GetObject<GlobalRouter>(); |
| 128 | |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame^] | 129 | gr->AddIncidency(face.shared_from_this(), grChannel); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 135 | GlobalRoutingHelper::Install(Ptr<Channel> channel) |
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 | NS_LOG_LOGIC("Channel: " << channel->GetId()); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 138 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 139 | Ptr<GlobalRouter> gr = channel->GetObject<GlobalRouter>(); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 140 | if (gr != 0) |
| 141 | return; |
| 142 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 143 | gr = CreateObject<GlobalRouter>(); |
| 144 | channel->AggregateObject(gr); |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 146 | for (uint32_t deviceId = 0; deviceId < channel->GetNDevices(); deviceId++) { |
| 147 | Ptr<NetDevice> dev = channel->GetDevice(deviceId); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 148 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 149 | Ptr<Node> node = dev->GetNode(); |
| 150 | NS_ASSERT(node != 0); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 151 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 152 | Ptr<GlobalRouter> grOther = node->GetObject<GlobalRouter>(); |
| 153 | if (grOther == 0) { |
| 154 | Install(node); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 155 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 156 | grOther = node->GetObject<GlobalRouter>(); |
| 157 | NS_ASSERT(grOther != 0); |
| 158 | |
| 159 | gr->AddIncidency(0, grOther); |
| 160 | } |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 164 | GlobalRoutingHelper::Install(const NodeContainer& nodes) |
Alexander Afanasyev | ce81014 | 2012-04-17 15:50:36 -0700 | [diff] [blame] | 165 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 166 | for (NodeContainer::Iterator node = nodes.Begin(); node != nodes.End(); node++) { |
| 167 | Install(*node); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | GlobalRoutingHelper::InstallAll() |
| 173 | { |
| 174 | Install(NodeContainer::GetGlobal()); |
| 175 | } |
| 176 | |
| 177 | void |
| 178 | GlobalRoutingHelper::AddOrigin(const std::string& prefix, Ptr<Node> node) |
| 179 | { |
| 180 | Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter>(); |
| 181 | NS_ASSERT_MSG(gr != 0, "GlobalRouter is not installed on the node"); |
| 182 | |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 183 | auto name = make_shared<Name>(prefix); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 184 | gr->AddLocalPrefix(name); |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | GlobalRoutingHelper::AddOrigins(const std::string& prefix, const NodeContainer& nodes) |
| 189 | { |
| 190 | for (NodeContainer::Iterator node = nodes.Begin(); node != nodes.End(); node++) { |
| 191 | AddOrigin(prefix, *node); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | GlobalRoutingHelper::AddOrigin(const std::string& prefix, const std::string& nodeName) |
| 197 | { |
| 198 | Ptr<Node> node = Names::Find<Node>(nodeName); |
| 199 | NS_ASSERT_MSG(node != 0, nodeName << "is not a Node"); |
| 200 | |
| 201 | AddOrigin(prefix, node); |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | GlobalRoutingHelper::AddOriginsForAll() |
| 206 | { |
| 207 | for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) { |
| 208 | Ptr<GlobalRouter> gr = (*node)->GetObject<GlobalRouter>(); |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 209 | std::string name = Names::FindName(*node); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 210 | |
| 211 | if (gr != 0 && !name.empty()) { |
| 212 | AddOrigin("/" + name, *node); |
Alexander Afanasyev | ce81014 | 2012-04-17 15:50:36 -0700 | [diff] [blame] | 213 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 214 | } |
Alexander Afanasyev | ce81014 | 2012-04-17 15:50:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void |
Alexander Afanasyev | 8e60bcd | 2015-01-15 20:55:40 +0000 | [diff] [blame] | 218 | GlobalRoutingHelper::CalculateRoutes() |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 219 | { |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 220 | /** |
| 221 | * Implementation of route calculation is heavily based on Boost Graph Library |
| 222 | * See http://www.boost.org/doc/libs/1_49_0/libs/graph/doc/table_of_contents.html for more details |
| 223 | */ |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 224 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 225 | BOOST_CONCEPT_ASSERT((boost::VertexListGraphConcept<boost::NdnGlobalRouterGraph>)); |
| 226 | BOOST_CONCEPT_ASSERT((boost::IncidenceGraphConcept<boost::NdnGlobalRouterGraph>)); |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 227 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 228 | boost::NdnGlobalRouterGraph graph; |
Zongyi Zhao | c837263 | 2014-04-28 15:36:49 -0700 | [diff] [blame] | 229 | // typedef graph_traits < NdnGlobalRouterGraph >::vertex_descriptor vertex_descriptor; |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 230 | |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 231 | // 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] | 232 | // Other algorithms should be faster, but they need additional EdgeListGraph concept provided by |
| 233 | // the graph, which |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 234 | // is not obviously how implement in an efficient manner |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 235 | for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) { |
| 236 | Ptr<GlobalRouter> source = (*node)->GetObject<GlobalRouter>(); |
| 237 | if (source == 0) { |
| 238 | NS_LOG_DEBUG("Node " << (*node)->GetId() << " does not export GlobalRouter interface"); |
| 239 | continue; |
| 240 | } |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 241 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 242 | boost::DistancesMap distances; |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 243 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 244 | dijkstra_shortest_paths(graph, source, |
| 245 | // predecessor_map (boost::ref(predecessors)) |
| 246 | // . |
| 247 | distance_map(boost::ref(distances)) |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 248 | .distance_inf(boost::WeightInf) |
| 249 | .distance_zero(boost::WeightZero) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 250 | .distance_compare(boost::WeightCompare()) |
| 251 | .distance_combine(boost::WeightCombine())); |
| 252 | |
| 253 | // NS_LOG_DEBUG (predecessors.size () << ", " << distances.size ()); |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 254 | |
| 255 | Ptr<L3Protocol> L3protocol = (*node)->GetObject<L3Protocol>(); |
| 256 | shared_ptr<nfd::Forwarder> forwarder = L3protocol->getForwarder(); |
| 257 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 258 | NS_LOG_DEBUG("Reachability from Node: " << source->GetObject<Node>()->GetId()); |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 259 | for (const auto& dist : distances) { |
| 260 | if (dist.first == source) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 261 | continue; |
| 262 | else { |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 263 | // cout << " Node " << dist.first->GetObject<Node> ()->GetId (); |
| 264 | if (std::get<0>(dist.second) == 0) { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 265 | // cout << " is unreachable" << endl; |
| 266 | } |
| 267 | else { |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 268 | for (const auto& prefix : dist.first->GetLocalPrefixes()) { |
| 269 | NS_LOG_DEBUG(" prefix " << prefix << " reachable via face " << *std::get<0>(dist.second) |
| 270 | << " with distance " << std::get<1>(dist.second) << " with delay " |
| 271 | << std::get<2>(dist.second)); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 272 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 273 | FibHelper::AddRoute(*node, *prefix, std::get<0>(dist.second), |
| 274 | std::get<1>(dist.second)); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | void |
Alexander Afanasyev | 8e60bcd | 2015-01-15 20:55:40 +0000 | [diff] [blame] | 283 | GlobalRoutingHelper::CalculateAllPossibleRoutes() |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 284 | { |
| 285 | /** |
| 286 | * Implementation of route calculation is heavily based on Boost Graph Library |
| 287 | * See http://www.boost.org/doc/libs/1_49_0/libs/graph/doc/table_of_contents.html for more details |
| 288 | */ |
| 289 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 290 | BOOST_CONCEPT_ASSERT((boost::VertexListGraphConcept<boost::NdnGlobalRouterGraph>)); |
| 291 | BOOST_CONCEPT_ASSERT((boost::IncidenceGraphConcept<boost::NdnGlobalRouterGraph>)); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 292 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 293 | boost::NdnGlobalRouterGraph graph; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 294 | // typedef graph_traits < NdnGlobalRouterGraph >::vertex_descriptor vertex_descriptor; |
| 295 | |
| 296 | // For now we doing Dijkstra for every node. Can be replaced with Bellman-Ford or Floyd-Warshall. |
| 297 | // Other algorithms should be faster, but they need additional EdgeListGraph concept provided by |
| 298 | // the graph, which |
| 299 | // is not obviously how implement in an efficient manner |
| 300 | for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) { |
| 301 | Ptr<GlobalRouter> source = (*node)->GetObject<GlobalRouter>(); |
| 302 | if (source == 0) { |
| 303 | NS_LOG_DEBUG("Node " << (*node)->GetId() << " does not export GlobalRouter interface"); |
| 304 | continue; |
| 305 | } |
| 306 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 307 | Ptr<L3Protocol> L3protocol = (*node)->GetObject<L3Protocol>(); |
| 308 | shared_ptr<nfd::Forwarder> forwarder = L3protocol->getForwarder(); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 309 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 310 | NS_LOG_DEBUG("Reachability from Node: " << source->GetObject<Node>()->GetId() << " (" |
| 311 | << Names::FindName(source->GetObject<Node>()) << ")"); |
| 312 | |
| 313 | Ptr<L3Protocol> l3 = source->GetObject<L3Protocol>(); |
| 314 | NS_ASSERT(l3 != 0); |
| 315 | |
| 316 | // remember interface statuses |
Christian Kreuzberger | 8a56e8d | 2015-02-18 08:45:01 -0800 | [diff] [blame] | 317 | std::list<nfd::FaceId> faceIds; |
| 318 | std::unordered_map<nfd::FaceId, uint16_t> originalMetrics; |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame^] | 319 | for (auto& nfdFace : l3->getForwarder()->getFaceTable()) { |
| 320 | faceIds.push_back(nfdFace.getId()); |
| 321 | originalMetrics[nfdFace.getId()] = nfdFace.getMetric(); |
| 322 | nfdFace.setMetric(std::numeric_limits<uint16_t>::max() - 1); |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 323 | // value std::numeric_limits<uint16_t>::max () MUST NOT be used (reserved) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Christian Kreuzberger | 8a56e8d | 2015-02-18 08:45:01 -0800 | [diff] [blame] | 326 | for (auto& faceId : faceIds) { |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame^] | 327 | auto* face = l3->getForwarder()->getFaceTable().get(faceId); |
| 328 | NS_ASSERT(face != nullptr); |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 329 | auto linkService = dynamic_cast<NetDeviceLinkService*>(face->getLinkService()); |
| 330 | if (linkService == nullptr) { |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 331 | NS_LOG_DEBUG("Skipping non-netdevice face"); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 332 | continue; |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 333 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 334 | |
| 335 | // enabling only faceId |
Christian Kreuzberger | 8a56e8d | 2015-02-18 08:45:01 -0800 | [diff] [blame] | 336 | face->setMetric(originalMetrics[faceId]); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 337 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 338 | boost::DistancesMap distances; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 339 | |
| 340 | NS_LOG_DEBUG("-----------"); |
| 341 | |
| 342 | dijkstra_shortest_paths(graph, source, |
| 343 | // predecessor_map (boost::ref(predecessors)) |
| 344 | // . |
| 345 | distance_map(boost::ref(distances)) |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 346 | .distance_inf(boost::WeightInf) |
| 347 | .distance_zero(boost::WeightZero) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 348 | .distance_compare(boost::WeightCompare()) |
| 349 | .distance_combine(boost::WeightCombine())); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 350 | |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 351 | // NS_LOG_DEBUG (predecessors.size () << ", " << distances.size ()); |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 352 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 353 | for (const auto& dist : distances) { |
| 354 | if (dist.first == source) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 355 | continue; |
| 356 | else { |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 357 | // cout << " Node " << dist.first->GetObject<Node> ()->GetId (); |
| 358 | if (std::get<0>(dist.second) == 0) { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 359 | // cout << " is unreachable" << endl; |
| 360 | } |
| 361 | else { |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 362 | for (const auto& prefix : dist.first->GetLocalPrefixes()) { |
| 363 | NS_LOG_DEBUG(" prefix " << *prefix << " reachable via face " |
| 364 | << *std::get<0>(dist.second) |
| 365 | << " with distance " << std::get<1>(dist.second) |
| 366 | << " with delay " << std::get<2>(dist.second)); |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 367 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 368 | 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] | 369 | continue; |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 370 | |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 371 | FibHelper::AddRoute(*node, *prefix, std::get<0>(dist.second), |
| 372 | std::get<1>(dist.second)); |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 373 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 374 | } |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 375 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 376 | } |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 377 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 378 | // disabling the face again |
Spyridon Mastorakis | 60f4b99 | 2014-11-07 15:51:38 -0800 | [diff] [blame] | 379 | face->setMetric(std::numeric_limits<uint16_t>::max() - 1); |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 380 | } |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 381 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 382 | // recover original interface statuses |
Christian Kreuzberger | 8a56e8d | 2015-02-18 08:45:01 -0800 | [diff] [blame] | 383 | for (auto& i : originalMetrics) { |
| 384 | l3->getForwarder()->getFaceTable().get(i.first)->setMetric(i.second); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | } |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 388 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 389 | } // namespace ndn |
| 390 | } // namespace ns3 |