blob: 35ed56d1b8f66467bd0cd6def1924071bd9a9c30 [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 Afanasyev2b4c9472012-08-09 15:00:38 -070049NS_LOG_COMPONENT_DEFINE ("ndn.GlobalRoutingHelper");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070050
51using namespace std;
52using namespace boost;
53
54namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070055namespace ndn {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070056
57void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058GlobalRoutingHelper::Install (Ptr<Node> node)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070059{
60 NS_LOG_LOGIC ("Node: " << node->GetId ());
61
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070062 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol> ();
63 NS_ASSERT_MSG (ndn != 0, "Cannot install GlobalRoutingHelper before Ndn is installed on a node");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070064
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070065 Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070066 if (gr != 0)
67 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070068 NS_LOG_DEBUG ("GlobalRouter is already installed: " << gr);
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070069 return; // already installed
70 }
71
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070072 gr = CreateObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070073 node->AggregateObject (gr);
74
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070075 for (uint32_t faceId = 0; faceId < ndn->GetNFaces (); faceId++)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070076 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070077 Ptr<NetDeviceFace> face = DynamicCast<NetDeviceFace> (ndn->GetFace (faceId));
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070078 if (face == 0)
79 {
80 NS_LOG_DEBUG ("Skipping non-netdevice face");
81 continue;
82 }
83
84 Ptr<NetDevice> nd = face->GetNetDevice ();
85 if (nd == 0)
86 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070087 NS_LOG_DEBUG ("Not a NetDevice associated with NetDeviceFace");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070088 continue;
89 }
90
91 Ptr<Channel> ch = nd->GetChannel ();
92
93 if (ch == 0)
94 {
95 NS_LOG_DEBUG ("Channel is not associated with NetDevice");
96 continue;
97 }
98
99 if (ch->GetNDevices () == 2) // e.g., point-to-point channel
100 {
101 for (uint32_t deviceId = 0; deviceId < ch->GetNDevices (); deviceId ++)
102 {
103 Ptr<NetDevice> otherSide = ch->GetDevice (deviceId);
104 if (nd == otherSide) continue;
105
106 Ptr<Node> otherNode = otherSide->GetNode ();
107 NS_ASSERT (otherNode != 0);
108
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700109 Ptr<GlobalRouter> otherGr = otherNode->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700110 if (otherGr == 0)
111 {
112 Install (otherNode);
113 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700114 otherGr = otherNode->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700115 NS_ASSERT (otherGr != 0);
116 gr->AddIncidency (face, otherGr);
117 }
118 }
119 else
120 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700121 Ptr<GlobalRouter> grChannel = ch->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700122 if (grChannel == 0)
123 {
124 Install (ch);
125 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700126 grChannel = ch->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700127
128 gr->AddIncidency (0, grChannel);
129 }
130 }
131}
132
133void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700134GlobalRoutingHelper::Install (Ptr<Channel> channel)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700135{
136 NS_LOG_LOGIC ("Channel: " << channel->GetId ());
137
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700138 Ptr<GlobalRouter> gr = channel->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700139 if (gr != 0)
140 return;
141
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700142 gr = CreateObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700143 channel->AggregateObject (gr);
144
145 for (uint32_t deviceId = 0; deviceId < channel->GetNDevices (); deviceId ++)
146 {
147 Ptr<NetDevice> dev = channel->GetDevice (deviceId);
148
149 Ptr<Node> node = dev->GetNode ();
150 NS_ASSERT (node != 0);
151
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700152 Ptr<GlobalRouter> grOther = node->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700153 if (grOther == 0)
154 {
155 Install (node);
156 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700157 grOther = node->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700158 NS_ASSERT (grOther != 0);
159
160 gr->AddIncidency (0, grOther);
161 }
162}
163
164void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700165GlobalRoutingHelper::Install (const NodeContainer &nodes)
Alexander Afanasyevce810142012-04-17 15:50:36 -0700166{
167 for (NodeContainer::Iterator node = nodes.Begin ();
168 node != nodes.End ();
169 node ++)
170 {
171 Install (*node);
172 }
173}
174
175void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700176GlobalRoutingHelper::InstallAll ()
Alexander Afanasyevce810142012-04-17 15:50:36 -0700177{
178 Install (NodeContainer::GetGlobal ());
179}
180
181
182void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700183GlobalRoutingHelper::AddOrigin (const std::string &prefix, Ptr<Node> node)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700184{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700185 Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700186 NS_ASSERT_MSG (gr != 0,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700187 "GlobalRouter is not installed on the node");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700188
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700189 Ptr<NameComponents> name = Create<NameComponents> (boost::lexical_cast<NameComponents> (prefix));
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700190 gr->AddLocalPrefix (name);
191}
192
193void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700194GlobalRoutingHelper::AddOrigins (const std::string &prefix, const NodeContainer &nodes)
Alexander Afanasyev06d3a612012-04-17 22:25:40 -0700195{
196 for (NodeContainer::Iterator node = nodes.Begin ();
197 node != nodes.End ();
198 node++)
199 {
200 AddOrigin (prefix, *node);
201 }
202}
203
204void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700205GlobalRoutingHelper::AddOrigin (const std::string &prefix, const std::string &nodeName)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700206{
207 Ptr<Node> node = Names::Find<Node> (nodeName);
208 NS_ASSERT_MSG (node != 0, nodeName << "is not a Node");
209
210 AddOrigin (prefix, node);
211}
212
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700213void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700214GlobalRoutingHelper::CalculateRoutes ()
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700215{
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700216 /**
217 * Implementation of route calculation is heavily based on Boost Graph Library
218 * See http://www.boost.org/doc/libs/1_49_0/libs/graph/doc/table_of_contents.html for more details
219 */
220
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700221 BOOST_CONCEPT_ASSERT(( VertexListGraphConcept< NdnGlobalRouterGraph > ));
222 BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept< NdnGlobalRouterGraph > ));
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700223
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700224 NdnGlobalRouterGraph graph;
225 typedef graph_traits < NdnGlobalRouterGraph >::vertex_descriptor vertex_descriptor;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700226
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700227 // For now we doing Dijkstra for every node. Can be replaced with Bellman-Ford or Floyd-Warshall.
228 // Other algorithms should be faster, but they need additional EdgeListGraph concept provided by the graph, which
229 // is not obviously how implement in an efficient manner
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700230 for (NodeList::Iterator node = NodeList::Begin (); node != NodeList::End (); node++)
231 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700232 Ptr<GlobalRouter> source = (*node)->GetObject<GlobalRouter> ();
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700233 if (source == 0)
234 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700235 NS_LOG_DEBUG ("Node " << (*node)->GetId () << " does not export GlobalRouter interface");
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700236 continue;
237 }
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700238
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700239 DistancesMap distances;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700240
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700241 dijkstra_shortest_paths (graph, source,
242 // predecessor_map (boost::ref(predecessors))
243 // .
244 distance_map (boost::ref(distances))
245 .
246 distance_inf (WeightInf)
247 .
248 distance_zero (WeightZero)
249 .
250 distance_compare (boost::WeightCompare ())
251 .
252 distance_combine (boost::WeightCombine ())
253 );
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700254
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700255 // NS_LOG_DEBUG (predecessors.size () << ", " << distances.size ());
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700256
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700257 Ptr<Fib> fib = source->GetObject<Fib> ();
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700258 fib->InvalidateAll ();
259 NS_ASSERT (fib != 0);
260
Alexander Afanasyev5bcdc992012-11-19 22:25:55 -0800261 NS_LOG_DEBUG ("Reachability from Node: " << source->GetObject<Node> ()->GetId ());
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700262 for (DistancesMap::iterator i = distances.begin ();
263 i != distances.end ();
264 i++)
265 {
266 if (i->first == source)
267 continue;
268 else
269 {
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700270 // cout << " Node " << i->first->GetObject<Node> ()->GetId ();
271 if (i->second.get<0> () == 0)
272 {
273 // cout << " is unreachable" << endl;
274 }
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700275 else
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700276 {
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -0800277 BOOST_FOREACH (const Ptr<const NameComponents> &prefix, i->first->GetLocalPrefixes ())
278 {
Alexander Afanasyev5bcdc992012-11-19 22:25:55 -0800279 NS_LOG_DEBUG (" prefix " << prefix << " reachable via face " << *i->second.get<0> ()
280 << " with distance " << i->second.get<1> ()
281 << " with delay " << i->second.get<2> ());
Alexander Afanasyevf5c07742012-10-31 13:13:05 -0700282
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -0800283 Ptr<fib::Entry> entry = fib->Add (prefix, i->second.get<0> (), i->second.get<1> ());
Alexander Afanasyevadcccf42012-11-26 23:55:34 -0800284 Ptr<Limits> faceLimits = i->second.get<0> ()->GetObject<Limits> ();
285
286 Ptr<Limits> fibLimits = entry->GetObject<Limits> ();
287 if (fibLimits != 0)
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -0800288 {
Alexander Afanasyevadcccf42012-11-26 23:55:34 -0800289 // if it was created by the forwarding strategy via DidAddFibEntry event
290 fibLimits->SetLimits (faceLimits->GetMaxRate (), 2*i->second.get<2> () /*exact RTT*/);
Alexander Afanasyeva8f5d882012-11-09 14:22:48 -0800291 }
292 }
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700293 }
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700294 }
295 }
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700296 }
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700297}
298
299
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700300} // namespace ndn
301} // namespace ns3