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