blob: cda977aa74a4bc44e5e859ff1ba6bb36e39efbe6 [file] [log] [blame]
Alexander Afanasyevad3757f2012-04-17 10:27:59 -07001/* -*- 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 Afanasyev4aac5572012-08-09 10:49:55 -070021#include "ndn-global-routing-helper.h"
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070022
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070023#include "ns3/ndn-l3-protocol.h"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070024#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 Afanasyevad3757f2012-04-17 10:27:59 -070028
29#include "ns3/node.h"
Alexander Afanasyevce810142012-04-17 15:50:36 -070030#include "ns3/node-container.h"
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070031#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 Afanasyevf5c07742012-10-31 13:13:05 -070038#include "ns3/object-factory.h"
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070039
40#include <boost/lexical_cast.hpp>
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -070041#include <boost/foreach.hpp>
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -070042#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 Afanasyev4aac5572012-08-09 10:49:55 -070047#include "boost-graph-ndn-global-routing-helper.h"
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070048
Alexander Afanasyev73532512012-11-27 00:42:35 -080049#include <math.h>
50
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070051NS_LOG_COMPONENT_DEFINE ("ndn.GlobalRoutingHelper");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070052
53using namespace std;
54using namespace boost;
55
56namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070057namespace ndn {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070058
59void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070060GlobalRoutingHelper::Install (Ptr<Node> node)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070061{
62 NS_LOG_LOGIC ("Node: " << node->GetId ());
63
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070064 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol> ();
65 NS_ASSERT_MSG (ndn != 0, "Cannot install GlobalRoutingHelper before Ndn is installed on a node");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070066
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070067 Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070068 if (gr != 0)
69 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070070 NS_LOG_DEBUG ("GlobalRouter is already installed: " << gr);
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070071 return; // already installed
72 }
73
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070074 gr = CreateObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070075 node->AggregateObject (gr);
76
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070077 for (uint32_t faceId = 0; faceId < ndn->GetNFaces (); faceId++)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070078 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070079 Ptr<NetDeviceFace> face = DynamicCast<NetDeviceFace> (ndn->GetFace (faceId));
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070080 if (face == 0)
81 {
82 NS_LOG_DEBUG ("Skipping non-netdevice face");
83 continue;
84 }
85
86 Ptr<NetDevice> nd = face->GetNetDevice ();
87 if (nd == 0)
88 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070089 NS_LOG_DEBUG ("Not a NetDevice associated with NetDeviceFace");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070090 continue;
91 }
92
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);
110
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700111 Ptr<GlobalRouter> otherGr = otherNode->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700112 if (otherGr == 0)
113 {
114 Install (otherNode);
115 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700116 otherGr = otherNode->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700117 NS_ASSERT (otherGr != 0);
118 gr->AddIncidency (face, otherGr);
119 }
120 }
121 else
122 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700123 Ptr<GlobalRouter> grChannel = ch->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700124 if (grChannel == 0)
125 {
126 Install (ch);
127 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700128 grChannel = ch->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700129
130 gr->AddIncidency (0, grChannel);
131 }
132 }
133}
134
135void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700136GlobalRoutingHelper::Install (Ptr<Channel> channel)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700137{
138 NS_LOG_LOGIC ("Channel: " << channel->GetId ());
139
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700140 Ptr<GlobalRouter> gr = channel->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700141 if (gr != 0)
142 return;
143
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700144 gr = CreateObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700145 channel->AggregateObject (gr);
146
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 Afanasyev2b4c9472012-08-09 15:00:38 -0700154 Ptr<GlobalRouter> grOther = node->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700155 if (grOther == 0)
156 {
157 Install (node);
158 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700159 grOther = node->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700160 NS_ASSERT (grOther != 0);
161
162 gr->AddIncidency (0, grOther);
163 }
164}
165
166void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700167GlobalRoutingHelper::Install (const NodeContainer &nodes)
Alexander Afanasyevce810142012-04-17 15:50:36 -0700168{
169 for (NodeContainer::Iterator node = nodes.Begin ();
170 node != nodes.End ();
171 node ++)
172 {
173 Install (*node);
174 }
175}
176
177void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700178GlobalRoutingHelper::InstallAll ()
Alexander Afanasyevce810142012-04-17 15:50:36 -0700179{
180 Install (NodeContainer::GetGlobal ());
181}
182
183
184void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700185GlobalRoutingHelper::AddOrigin (const std::string &prefix, Ptr<Node> node)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700186{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700187 Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700188 NS_ASSERT_MSG (gr != 0,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700189 "GlobalRouter is not installed on the node");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700190
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700191 Ptr<NameComponents> name = Create<NameComponents> (boost::lexical_cast<NameComponents> (prefix));
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700192 gr->AddLocalPrefix (name);
193}
194
195void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700196GlobalRoutingHelper::AddOrigins (const std::string &prefix, const NodeContainer &nodes)
Alexander Afanasyev06d3a612012-04-17 22:25:40 -0700197{
198 for (NodeContainer::Iterator node = nodes.Begin ();
199 node != nodes.End ();
200 node++)
201 {
202 AddOrigin (prefix, *node);
203 }
204}
205
206void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700207GlobalRoutingHelper::AddOrigin (const std::string &prefix, const std::string &nodeName)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700208{
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 Afanasyevad3757f2012-04-17 10:27:59 -0700215void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700216GlobalRoutingHelper::CalculateRoutes ()
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700217{
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700218 /**
219 * Implementation of route calculation is heavily based on Boost Graph Library
220 * See http://www.boost.org/doc/libs/1_49_0/libs/graph/doc/table_of_contents.html for more details
221 */
222
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700223 BOOST_CONCEPT_ASSERT(( VertexListGraphConcept< NdnGlobalRouterGraph > ));
224 BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept< NdnGlobalRouterGraph > ));
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700225
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700226 NdnGlobalRouterGraph graph;
227 typedef graph_traits < NdnGlobalRouterGraph >::vertex_descriptor vertex_descriptor;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700228
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700229 // For now we doing Dijkstra for every node. Can be replaced with Bellman-Ford or Floyd-Warshall.
230 // Other algorithms should be faster, but they need additional EdgeListGraph concept provided by the graph, which
231 // is not obviously how implement in an efficient manner
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700232 for (NodeList::Iterator node = NodeList::Begin (); node != NodeList::End (); node++)
233 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700234 Ptr<GlobalRouter> source = (*node)->GetObject<GlobalRouter> ();
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700235 if (source == 0)
236 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700237 NS_LOG_DEBUG ("Node " << (*node)->GetId () << " does not export GlobalRouter interface");
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700238 continue;
239 }
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700240
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700241 DistancesMap distances;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700242
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700243 dijkstra_shortest_paths (graph, source,
244 // predecessor_map (boost::ref(predecessors))
245 // .
246 distance_map (boost::ref(distances))
247 .
248 distance_inf (WeightInf)
249 .
250 distance_zero (WeightZero)
251 .
252 distance_compare (boost::WeightCompare ())
253 .
254 distance_combine (boost::WeightCombine ())
255 );
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700256
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700257 // NS_LOG_DEBUG (predecessors.size () << ", " << distances.size ());
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700258
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700259 Ptr<Fib> fib = source->GetObject<Fib> ();
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700260 fib->InvalidateAll ();
261 NS_ASSERT (fib != 0);
262
Alexander Afanasyev5bcdc992012-11-19 22:25:55 -0800263 NS_LOG_DEBUG ("Reachability from Node: " << source->GetObject<Node> ()->GetId ());
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700264 for (DistancesMap::iterator i = distances.begin ();
265 i != distances.end ();
266 i++)
267 {
268 if (i->first == source)
269 continue;
270 else
271 {
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700272 // cout << " Node " << i->first->GetObject<Node> ()->GetId ();
273 if (i->second.get<0> () == 0)
274 {
275 // cout << " is unreachable" << endl;
276 }
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700277 else
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700278 {
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -0800279 BOOST_FOREACH (const Ptr<const NameComponents> &prefix, i->first->GetLocalPrefixes ())
280 {
Alexander Afanasyev5bcdc992012-11-19 22:25:55 -0800281 NS_LOG_DEBUG (" prefix " << prefix << " reachable via face " << *i->second.get<0> ()
282 << " with distance " << i->second.get<1> ()
283 << " with delay " << i->second.get<2> ());
Alexander Afanasyevf5c07742012-10-31 13:13:05 -0700284
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -0800285 Ptr<fib::Entry> entry = fib->Add (prefix, i->second.get<0> (), i->second.get<1> ());
Alexander Afanasyevadcccf42012-11-26 23:55:34 -0800286 Ptr<Limits> faceLimits = i->second.get<0> ()->GetObject<Limits> ();
287
288 Ptr<Limits> fibLimits = entry->GetObject<Limits> ();
289 if (fibLimits != 0)
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -0800290 {
Alexander Afanasyevadcccf42012-11-26 23:55:34 -0800291 // if it was created by the forwarding strategy via DidAddFibEntry event
Alexander Afanasyev73532512012-11-27 00:42:35 -0800292 fibLimits->SetLimits (faceLimits->GetMaxRate (), 2 * i->second.get<2> () /*exact RTT*/);
293 NS_LOG_DEBUG ("Set limit for prefix " << *prefix << " " << faceLimits->GetMaxRate () << " / " <<
294 2*i->second.get<2> () << "s (" << faceLimits->GetMaxRate () * 2 * i->second.get<2> () << ")");
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -0800295 }
296 }
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700297 }
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700298 }
299 }
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700300 }
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700301}
302
303
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700304} // namespace ndn
305} // namespace ns3