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 | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 27 | #include "ndn-global-routing-helper.h" |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 29 | #include "ns3/ndn-l3-protocol.h" |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 30 | #include "../model/ndn-net-device-face.h" |
| 31 | #include "../model/ndn-global-router.h" |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 32 | #include "ns3/ndn-name.h" |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 33 | #include "ns3/ndn-fib.h" |
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 | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 53 | #include "boost-graph-ndn-global-routing-helper.h" |
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 | |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 136 | gr->AddIncidency (0, grChannel); |
| 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 | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 237 | GlobalRoutingHelper::CalculateRoutes () |
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; |
| 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 | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 281 | fib->InvalidateAll (); |
| 282 | NS_ASSERT (fib != 0); |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 283 | |
Alexander Afanasyev | 5bcdc99 | 2012-11-19 22:25:55 -0800 | [diff] [blame] | 284 | NS_LOG_DEBUG ("Reachability from Node: " << source->GetObject<Node> ()->GetId ()); |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 285 | for (DistancesMap::iterator i = distances.begin (); |
| 286 | i != distances.end (); |
| 287 | i++) |
| 288 | { |
| 289 | if (i->first == source) |
| 290 | continue; |
| 291 | else |
| 292 | { |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 293 | // cout << " Node " << i->first->GetObject<Node> ()->GetId (); |
| 294 | if (i->second.get<0> () == 0) |
| 295 | { |
| 296 | // cout << " is unreachable" << endl; |
| 297 | } |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 298 | else |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 299 | { |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 300 | BOOST_FOREACH (const Ptr<const Name> &prefix, i->first->GetLocalPrefixes ()) |
Alexander Afanasyev | a8f5d88 | 2012-11-09 14:22:48 -0800 | [diff] [blame] | 301 | { |
Alexander Afanasyev | 5bcdc99 | 2012-11-19 22:25:55 -0800 | [diff] [blame] | 302 | NS_LOG_DEBUG (" prefix " << prefix << " reachable via face " << *i->second.get<0> () |
| 303 | << " with distance " << i->second.get<1> () |
| 304 | << " with delay " << i->second.get<2> ()); |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 305 | |
Alexander Afanasyev | a8f5d88 | 2012-11-09 14:22:48 -0800 | [diff] [blame] | 306 | 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] | 307 | entry->SetRealDelayToProducer (i->second.get<0> (), Seconds (i->second.get<2> ())); |
Alexander Afanasyev | 4916586 | 2013-01-31 00:38:20 -0800 | [diff] [blame] | 308 | |
Alexander Afanasyev | adcccf4 | 2012-11-26 23:55:34 -0800 | [diff] [blame] | 309 | Ptr<Limits> faceLimits = i->second.get<0> ()->GetObject<Limits> (); |
| 310 | |
| 311 | Ptr<Limits> fibLimits = entry->GetObject<Limits> (); |
| 312 | if (fibLimits != 0) |
Alexander Afanasyev | a8f5d88 | 2012-11-09 14:22:48 -0800 | [diff] [blame] | 313 | { |
Alexander Afanasyev | adcccf4 | 2012-11-26 23:55:34 -0800 | [diff] [blame] | 314 | // if it was created by the forwarding strategy via DidAddFibEntry event |
Alexander Afanasyev | 7353251 | 2012-11-27 00:42:35 -0800 | [diff] [blame] | 315 | fibLimits->SetLimits (faceLimits->GetMaxRate (), 2 * i->second.get<2> () /*exact RTT*/); |
| 316 | NS_LOG_DEBUG ("Set limit for prefix " << *prefix << " " << faceLimits->GetMaxRate () << " / " << |
| 317 | 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] | 318 | } |
| 319 | } |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 320 | } |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 323 | } |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 326 | void |
| 327 | GlobalRoutingHelper::CalculateAllPossibleRoutes () |
| 328 | { |
| 329 | /** |
| 330 | * Implementation of route calculation is heavily based on Boost Graph Library |
| 331 | * See http://www.boost.org/doc/libs/1_49_0/libs/graph/doc/table_of_contents.html for more details |
| 332 | */ |
| 333 | |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 334 | BOOST_CONCEPT_ASSERT(( VertexListGraphConcept< NdnGlobalRouterGraph > )); |
| 335 | BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept< NdnGlobalRouterGraph > )); |
| 336 | |
| 337 | NdnGlobalRouterGraph graph; |
| 338 | typedef graph_traits < NdnGlobalRouterGraph >::vertex_descriptor vertex_descriptor; |
| 339 | |
| 340 | // For now we doing Dijkstra for every node. Can be replaced with Bellman-Ford or Floyd-Warshall. |
| 341 | // Other algorithms should be faster, but they need additional EdgeListGraph concept provided by the graph, which |
| 342 | // is not obviously how implement in an efficient manner |
| 343 | for (NodeList::Iterator node = NodeList::Begin (); node != NodeList::End (); node++) |
| 344 | { |
| 345 | Ptr<GlobalRouter> source = (*node)->GetObject<GlobalRouter> (); |
| 346 | if (source == 0) |
| 347 | { |
| 348 | NS_LOG_DEBUG ("Node " << (*node)->GetId () << " does not export GlobalRouter interface"); |
| 349 | continue; |
| 350 | } |
| 351 | |
| 352 | Ptr<Fib> fib = source->GetObject<Fib> (); |
| 353 | fib->InvalidateAll (); |
| 354 | NS_ASSERT (fib != 0); |
| 355 | |
| 356 | NS_LOG_DEBUG ("==========="); |
| 357 | NS_LOG_DEBUG ("Reachability from Node: " << source->GetObject<Node> ()->GetId () << " (" << Names::FindName (source->GetObject<Node> ()) << ")"); |
| 358 | |
| 359 | Ptr<L3Protocol> l3 = source->GetObject<L3Protocol> (); |
| 360 | NS_ASSERT (l3 != 0); |
| 361 | |
| 362 | // remember interface statuses |
| 363 | std::vector<uint16_t> originalMetric (l3->GetNFaces ()); |
| 364 | for (uint32_t faceId = 0; faceId < l3->GetNFaces (); faceId++) |
| 365 | { |
| 366 | originalMetric[faceId] = l3->GetFace (faceId)->GetMetric (); |
Alexander Afanasyev | 52a09bd | 2013-07-29 18:49:01 -0700 | [diff] [blame] | 367 | 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] | 368 | } |
| 369 | |
| 370 | for (uint32_t enabledFaceId = 0; enabledFaceId < l3->GetNFaces (); enabledFaceId++) |
| 371 | { |
| 372 | if (DynamicCast<ndn::NetDeviceFace> (l3->GetFace (enabledFaceId)) == 0) |
| 373 | continue; |
| 374 | |
| 375 | // enabling only faceId |
| 376 | l3->GetFace (enabledFaceId)->SetMetric (originalMetric[enabledFaceId]); |
| 377 | |
| 378 | DistancesMap distances; |
| 379 | |
| 380 | NS_LOG_DEBUG ("-----------"); |
| 381 | |
| 382 | dijkstra_shortest_paths (graph, source, |
| 383 | // predecessor_map (boost::ref(predecessors)) |
| 384 | // . |
| 385 | distance_map (boost::ref(distances)) |
| 386 | . |
| 387 | distance_inf (WeightInf) |
| 388 | . |
| 389 | distance_zero (WeightZero) |
| 390 | . |
| 391 | distance_compare (boost::WeightCompare ()) |
| 392 | . |
| 393 | distance_combine (boost::WeightCombine ()) |
| 394 | ); |
| 395 | |
| 396 | // NS_LOG_DEBUG (predecessors.size () << ", " << distances.size ()); |
| 397 | |
| 398 | for (DistancesMap::iterator i = distances.begin (); |
| 399 | i != distances.end (); |
| 400 | i++) |
| 401 | { |
| 402 | if (i->first == source) |
| 403 | continue; |
| 404 | else |
| 405 | { |
| 406 | // cout << " Node " << i->first->GetObject<Node> ()->GetId (); |
| 407 | if (i->second.get<0> () == 0) |
| 408 | { |
| 409 | // cout << " is unreachable" << endl; |
| 410 | } |
| 411 | else |
| 412 | { |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 413 | BOOST_FOREACH (const Ptr<const Name> &prefix, i->first->GetLocalPrefixes ()) |
Alexander Afanasyev | f484fb9 | 2013-03-04 10:37:27 -0800 | [diff] [blame] | 414 | { |
| 415 | NS_LOG_DEBUG (" prefix " << *prefix << " reachable via face " << *i->second.get<0> () |
| 416 | << " with distance " << i->second.get<1> () |
| 417 | << " with delay " << i->second.get<2> ()); |
| 418 | |
| 419 | if (i->second.get<0> ()->GetMetric () == std::numeric_limits<uint16_t>::max ()-1) |
| 420 | continue; |
| 421 | |
| 422 | Ptr<fib::Entry> entry = fib->Add (prefix, i->second.get<0> (), i->second.get<1> ()); |
| 423 | entry->SetRealDelayToProducer (i->second.get<0> (), Seconds (i->second.get<2> ())); |
| 424 | |
| 425 | Ptr<Limits> faceLimits = i->second.get<0> ()->GetObject<Limits> (); |
| 426 | |
| 427 | Ptr<Limits> fibLimits = entry->GetObject<Limits> (); |
| 428 | if (fibLimits != 0) |
| 429 | { |
| 430 | // if it was created by the forwarding strategy via DidAddFibEntry event |
| 431 | fibLimits->SetLimits (faceLimits->GetMaxRate (), 2 * i->second.get<2> () /*exact RTT*/); |
| 432 | NS_LOG_DEBUG ("Set limit for prefix " << *prefix << " " << faceLimits->GetMaxRate () << " / " << |
| 433 | 2*i->second.get<2> () << "s (" << faceLimits->GetMaxRate () * 2 * i->second.get<2> () << ")"); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // disabling the face again |
| 441 | l3->GetFace (enabledFaceId)->SetMetric (std::numeric_limits<uint16_t>::max ()-1); |
| 442 | } |
| 443 | |
| 444 | // recover original interface statuses |
| 445 | for (uint32_t faceId = 0; faceId < l3->GetNFaces (); faceId++) |
| 446 | { |
| 447 | l3->GetFace (faceId)->SetMetric (originalMetric[faceId]); |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 452 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 453 | } // namespace ndn |
| 454 | } // namespace ns3 |