blob: 135cd4c4649816f83ae8312c4dc83fb674e279d6 [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 ());
Alexander Afanasyev49165862013-01-31 00:38:20 -080063
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 }
Alexander Afanasyev49165862013-01-31 00:38:20 -080073
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070074 gr = CreateObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070075 node->AggregateObject (gr);
Alexander Afanasyev49165862013-01-31 00:38:20 -080076
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 }
Alexander Afanasyev49165862013-01-31 00:38:20 -080085
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070086 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 }
Alexander Afanasyev49165862013-01-31 00:38:20 -080092
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070093 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 Afanasyev49165862013-01-31 00:38:20 -0800110
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 Afanasyev49165862013-01-31 00:38:20 -0800129
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700130 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);
Alexander Afanasyev49165862013-01-31 00:38:20 -0800146
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700147 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 Afanasyev49165862013-01-31 00:38:20 -0800192 gr->AddLocalPrefix (name);
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700193}
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 Afanasyev49165862013-01-31 00:38:20 -0800216GlobalRoutingHelper::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
230void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700231GlobalRoutingHelper::CalculateRoutes ()
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700232{
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700233 /**
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 Afanasyev49165862013-01-31 00:38:20 -0800237
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700238 BOOST_CONCEPT_ASSERT(( VertexListGraphConcept< NdnGlobalRouterGraph > ));
239 BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept< NdnGlobalRouterGraph > ));
Alexander Afanasyev49165862013-01-31 00:38:20 -0800240
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700241 NdnGlobalRouterGraph graph;
242 typedef graph_traits < NdnGlobalRouterGraph >::vertex_descriptor vertex_descriptor;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700243
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700244 // 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 Afanasyeva5abcd92012-04-17 13:34:43 -0700247 for (NodeList::Iterator node = NodeList::Begin (); node != NodeList::End (); node++)
248 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700249 Ptr<GlobalRouter> source = (*node)->GetObject<GlobalRouter> ();
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700250 if (source == 0)
251 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700252 NS_LOG_DEBUG ("Node " << (*node)->GetId () << " does not export GlobalRouter interface");
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700253 continue;
254 }
Alexander Afanasyev49165862013-01-31 00:38:20 -0800255
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700256 DistancesMap distances;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700257
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700258 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 Afanasyevad3757f2012-04-17 10:27:59 -0700271
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700272 // NS_LOG_DEBUG (predecessors.size () << ", " << distances.size ());
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700273
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700274 Ptr<Fib> fib = source->GetObject<Fib> ();
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700275 fib->InvalidateAll ();
276 NS_ASSERT (fib != 0);
Alexander Afanasyev49165862013-01-31 00:38:20 -0800277
Alexander Afanasyev5bcdc992012-11-19 22:25:55 -0800278 NS_LOG_DEBUG ("Reachability from Node: " << source->GetObject<Node> ()->GetId ());
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700279 for (DistancesMap::iterator i = distances.begin ();
280 i != distances.end ();
281 i++)
282 {
283 if (i->first == source)
284 continue;
285 else
286 {
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700287 // cout << " Node " << i->first->GetObject<Node> ()->GetId ();
288 if (i->second.get<0> () == 0)
289 {
290 // cout << " is unreachable" << endl;
291 }
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700292 else
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700293 {
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -0800294 BOOST_FOREACH (const Ptr<const NameComponents> &prefix, i->first->GetLocalPrefixes ())
295 {
Alexander Afanasyev5bcdc992012-11-19 22:25:55 -0800296 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 Afanasyev49165862013-01-31 00:38:20 -0800299
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -0800300 Ptr<fib::Entry> entry = fib->Add (prefix, i->second.get<0> (), i->second.get<1> ());
Alexander Afanasyev6b0c88f2012-12-01 12:24:27 -0800301 entry->SetRealDelayToProducer (i->second.get<0> (), Seconds (i->second.get<2> ()));
Alexander Afanasyev49165862013-01-31 00:38:20 -0800302
Alexander Afanasyevadcccf42012-11-26 23:55:34 -0800303 Ptr<Limits> faceLimits = i->second.get<0> ()->GetObject<Limits> ();
304
305 Ptr<Limits> fibLimits = entry->GetObject<Limits> ();
306 if (fibLimits != 0)
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -0800307 {
Alexander Afanasyevadcccf42012-11-26 23:55:34 -0800308 // if it was created by the forwarding strategy via DidAddFibEntry event
Alexander Afanasyev73532512012-11-27 00:42:35 -0800309 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 Afanasyeva8f5d882012-11-09 14:22:48 -0800312 }
313 }
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700314 }
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700315 }
316 }
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700317 }
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700318}
319
320
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700321} // namespace ndn
322} // namespace ns3