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" |
| 38 | |
| 39 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 40 | #include <boost/foreach.hpp> |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 41 | #include <boost/concept/assert.hpp> |
| 42 | // #include <boost/graph/graph_concepts.hpp> |
| 43 | // #include <boost/graph/adjacency_list.hpp> |
| 44 | #include <boost/graph/dijkstra_shortest_paths.hpp> |
| 45 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 46 | #include "boost-graph-ndn-global-routing-helper.h" |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 48 | NS_LOG_COMPONENT_DEFINE ("ndn.GlobalRoutingHelper"); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 49 | |
| 50 | using namespace std; |
| 51 | using namespace boost; |
| 52 | |
| 53 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 54 | namespace ndn { |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 55 | |
| 56 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 57 | GlobalRoutingHelper::Install (Ptr<Node> node) |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 58 | { |
| 59 | NS_LOG_LOGIC ("Node: " << node->GetId ()); |
| 60 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 61 | Ptr<L3Protocol> ndn = node->GetObject<L3Protocol> (); |
| 62 | 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] | 63 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 64 | Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter> (); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 65 | if (gr != 0) |
| 66 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 67 | NS_LOG_DEBUG ("GlobalRouter is already installed: " << gr); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 68 | return; // already installed |
| 69 | } |
| 70 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 71 | gr = CreateObject<GlobalRouter> (); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 72 | node->AggregateObject (gr); |
| 73 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 74 | for (uint32_t faceId = 0; faceId < ndn->GetNFaces (); faceId++) |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 75 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 76 | Ptr<NetDeviceFace> face = DynamicCast<NetDeviceFace> (ndn->GetFace (faceId)); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 77 | if (face == 0) |
| 78 | { |
| 79 | NS_LOG_DEBUG ("Skipping non-netdevice face"); |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | Ptr<NetDevice> nd = face->GetNetDevice (); |
| 84 | if (nd == 0) |
| 85 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 86 | NS_LOG_DEBUG ("Not a NetDevice associated with NetDeviceFace"); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 87 | continue; |
| 88 | } |
| 89 | |
| 90 | Ptr<Channel> ch = nd->GetChannel (); |
| 91 | |
| 92 | if (ch == 0) |
| 93 | { |
| 94 | NS_LOG_DEBUG ("Channel is not associated with NetDevice"); |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | if (ch->GetNDevices () == 2) // e.g., point-to-point channel |
| 99 | { |
| 100 | for (uint32_t deviceId = 0; deviceId < ch->GetNDevices (); deviceId ++) |
| 101 | { |
| 102 | Ptr<NetDevice> otherSide = ch->GetDevice (deviceId); |
| 103 | if (nd == otherSide) continue; |
| 104 | |
| 105 | Ptr<Node> otherNode = otherSide->GetNode (); |
| 106 | NS_ASSERT (otherNode != 0); |
| 107 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 108 | Ptr<GlobalRouter> otherGr = otherNode->GetObject<GlobalRouter> (); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 109 | if (otherGr == 0) |
| 110 | { |
| 111 | Install (otherNode); |
| 112 | } |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 113 | otherGr = otherNode->GetObject<GlobalRouter> (); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 114 | NS_ASSERT (otherGr != 0); |
| 115 | gr->AddIncidency (face, otherGr); |
| 116 | } |
| 117 | } |
| 118 | else |
| 119 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 120 | Ptr<GlobalRouter> grChannel = ch->GetObject<GlobalRouter> (); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 121 | if (grChannel == 0) |
| 122 | { |
| 123 | Install (ch); |
| 124 | } |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 125 | grChannel = ch->GetObject<GlobalRouter> (); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 126 | |
| 127 | gr->AddIncidency (0, grChannel); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 133 | GlobalRoutingHelper::Install (Ptr<Channel> channel) |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 134 | { |
| 135 | NS_LOG_LOGIC ("Channel: " << channel->GetId ()); |
| 136 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 137 | Ptr<GlobalRouter> gr = channel->GetObject<GlobalRouter> (); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 138 | if (gr != 0) |
| 139 | return; |
| 140 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 141 | gr = CreateObject<GlobalRouter> (); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 142 | channel->AggregateObject (gr); |
| 143 | |
| 144 | for (uint32_t deviceId = 0; deviceId < channel->GetNDevices (); deviceId ++) |
| 145 | { |
| 146 | Ptr<NetDevice> dev = channel->GetDevice (deviceId); |
| 147 | |
| 148 | Ptr<Node> node = dev->GetNode (); |
| 149 | NS_ASSERT (node != 0); |
| 150 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 151 | Ptr<GlobalRouter> grOther = node->GetObject<GlobalRouter> (); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 152 | if (grOther == 0) |
| 153 | { |
| 154 | Install (node); |
| 155 | } |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 156 | grOther = node->GetObject<GlobalRouter> (); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 157 | NS_ASSERT (grOther != 0); |
| 158 | |
| 159 | gr->AddIncidency (0, grOther); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 164 | GlobalRoutingHelper::Install (const NodeContainer &nodes) |
Alexander Afanasyev | ce81014 | 2012-04-17 15:50:36 -0700 | [diff] [blame] | 165 | { |
| 166 | for (NodeContainer::Iterator node = nodes.Begin (); |
| 167 | node != nodes.End (); |
| 168 | node ++) |
| 169 | { |
| 170 | Install (*node); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 175 | GlobalRoutingHelper::InstallAll () |
Alexander Afanasyev | ce81014 | 2012-04-17 15:50:36 -0700 | [diff] [blame] | 176 | { |
| 177 | Install (NodeContainer::GetGlobal ()); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 182 | GlobalRoutingHelper::AddOrigin (const std::string &prefix, Ptr<Node> node) |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 183 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 184 | Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter> (); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 185 | NS_ASSERT_MSG (gr != 0, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 186 | "GlobalRouter is not installed on the node"); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 187 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 188 | Ptr<NameComponents> name = Create<NameComponents> (boost::lexical_cast<NameComponents> (prefix)); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 189 | gr->AddLocalPrefix (name); |
| 190 | } |
| 191 | |
| 192 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 193 | GlobalRoutingHelper::AddOrigins (const std::string &prefix, const NodeContainer &nodes) |
Alexander Afanasyev | 06d3a61 | 2012-04-17 22:25:40 -0700 | [diff] [blame] | 194 | { |
| 195 | for (NodeContainer::Iterator node = nodes.Begin (); |
| 196 | node != nodes.End (); |
| 197 | node++) |
| 198 | { |
| 199 | AddOrigin (prefix, *node); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 204 | GlobalRoutingHelper::AddOrigin (const std::string &prefix, const std::string &nodeName) |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 205 | { |
| 206 | Ptr<Node> node = Names::Find<Node> (nodeName); |
| 207 | NS_ASSERT_MSG (node != 0, nodeName << "is not a Node"); |
| 208 | |
| 209 | AddOrigin (prefix, node); |
| 210 | } |
| 211 | |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 212 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 213 | GlobalRoutingHelper::CalculateRoutes () |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 214 | { |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 215 | /** |
| 216 | * Implementation of route calculation is heavily based on Boost Graph Library |
| 217 | * See http://www.boost.org/doc/libs/1_49_0/libs/graph/doc/table_of_contents.html for more details |
| 218 | */ |
| 219 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 220 | BOOST_CONCEPT_ASSERT(( VertexListGraphConcept< NdnGlobalRouterGraph > )); |
| 221 | BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept< NdnGlobalRouterGraph > )); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 222 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 223 | NdnGlobalRouterGraph graph; |
| 224 | typedef graph_traits < NdnGlobalRouterGraph >::vertex_descriptor vertex_descriptor; |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 225 | |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 226 | // For now we doing Dijkstra for every node. Can be replaced with Bellman-Ford or Floyd-Warshall. |
| 227 | // Other algorithms should be faster, but they need additional EdgeListGraph concept provided by the graph, which |
| 228 | // is not obviously how implement in an efficient manner |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 229 | for (NodeList::Iterator node = NodeList::Begin (); node != NodeList::End (); node++) |
| 230 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 231 | Ptr<GlobalRouter> source = (*node)->GetObject<GlobalRouter> (); |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 232 | if (source == 0) |
| 233 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 234 | NS_LOG_DEBUG ("Node " << (*node)->GetId () << " does not export GlobalRouter interface"); |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 235 | continue; |
| 236 | } |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 237 | |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 238 | DistancesMap distances; |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 239 | |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 240 | dijkstra_shortest_paths (graph, source, |
| 241 | // predecessor_map (boost::ref(predecessors)) |
| 242 | // . |
| 243 | distance_map (boost::ref(distances)) |
| 244 | . |
| 245 | distance_inf (WeightInf) |
| 246 | . |
| 247 | distance_zero (WeightZero) |
| 248 | . |
| 249 | distance_compare (boost::WeightCompare ()) |
| 250 | . |
| 251 | distance_combine (boost::WeightCombine ()) |
| 252 | ); |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 253 | |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 254 | // NS_LOG_DEBUG (predecessors.size () << ", " << distances.size ()); |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 255 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 256 | Ptr<Fib> fib = source->GetObject<Fib> (); |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 257 | fib->InvalidateAll (); |
| 258 | NS_ASSERT (fib != 0); |
| 259 | |
| 260 | // cout << "Reachability from Node: " << source->GetObject<Node> ()->GetId () << endl; |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 261 | for (DistancesMap::iterator i = distances.begin (); |
| 262 | i != distances.end (); |
| 263 | i++) |
| 264 | { |
| 265 | if (i->first == source) |
| 266 | continue; |
| 267 | else |
| 268 | { |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 269 | // cout << " Node " << i->first->GetObject<Node> ()->GetId (); |
| 270 | if (i->second.get<0> () == 0) |
| 271 | { |
| 272 | // cout << " is unreachable" << endl; |
| 273 | } |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 274 | else |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 275 | { |
| 276 | // cout << " reachable via face " << *i->second.get<0> () |
| 277 | // << " with distance " << i->second.get<1> () << endl; |
| 278 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 279 | BOOST_FOREACH (const Ptr<const NameComponents> &prefix, i->first->GetLocalPrefixes ()) |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 280 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 281 | Ptr<fib::Entry> entry = fib->Add (prefix, i->second.get<0> (), i->second.get<1> ()); |
| 282 | if (i->second.get<0> ()->GetLimits ().IsEnabled ()) |
| 283 | { |
| 284 | entry->GetLimits ().SetMaxLimit (i->second.get<0> ()->GetLimits ().GetMaxLimit ()); |
| 285 | } |
Alexander Afanasyev | 8e2f112 | 2012-04-17 15:01:11 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 290 | } |
Alexander Afanasyev | ad3757f | 2012-04-17 10:27:59 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 294 | } // namespace ndn |
| 295 | } // namespace ns3 |