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