blob: d235185c622565014bfeec7f244dccabcddc8a78 [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"
38
39#include <boost/lexical_cast.hpp>
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -070040#include <boost/foreach.hpp>
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -070041#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 Afanasyev4aac5572012-08-09 10:49:55 -070046#include "boost-graph-ndn-global-routing-helper.h"
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070047
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048NS_LOG_COMPONENT_DEFINE ("ndn.GlobalRoutingHelper");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070049
50using namespace std;
51using namespace boost;
52
53namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070054namespace ndn {
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070055
56void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070057GlobalRoutingHelper::Install (Ptr<Node> node)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070058{
59 NS_LOG_LOGIC ("Node: " << node->GetId ());
60
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070061 Ptr<L3Protocol> ndn = node->GetObject<L3Protocol> ();
62 NS_ASSERT_MSG (ndn != 0, "Cannot install GlobalRoutingHelper before Ndn is installed on a node");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070063
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070064 Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070065 if (gr != 0)
66 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070067 NS_LOG_DEBUG ("GlobalRouter is already installed: " << gr);
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070068 return; // already installed
69 }
70
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070071 gr = CreateObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070072 node->AggregateObject (gr);
73
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070074 for (uint32_t faceId = 0; faceId < ndn->GetNFaces (); faceId++)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070075 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070076 Ptr<NetDeviceFace> face = DynamicCast<NetDeviceFace> (ndn->GetFace (faceId));
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070077 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 Afanasyev2b4c9472012-08-09 15:00:38 -070086 NS_LOG_DEBUG ("Not a NetDevice associated with NetDeviceFace");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -070087 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 Afanasyev2b4c9472012-08-09 15:00:38 -0700108 Ptr<GlobalRouter> otherGr = otherNode->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700109 if (otherGr == 0)
110 {
111 Install (otherNode);
112 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700113 otherGr = otherNode->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700114 NS_ASSERT (otherGr != 0);
115 gr->AddIncidency (face, otherGr);
116 }
117 }
118 else
119 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700120 Ptr<GlobalRouter> grChannel = ch->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700121 if (grChannel == 0)
122 {
123 Install (ch);
124 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700125 grChannel = ch->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700126
127 gr->AddIncidency (0, grChannel);
128 }
129 }
130}
131
132void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700133GlobalRoutingHelper::Install (Ptr<Channel> channel)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700134{
135 NS_LOG_LOGIC ("Channel: " << channel->GetId ());
136
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700137 Ptr<GlobalRouter> gr = channel->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700138 if (gr != 0)
139 return;
140
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700141 gr = CreateObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700142 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 Afanasyev2b4c9472012-08-09 15:00:38 -0700151 Ptr<GlobalRouter> grOther = node->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700152 if (grOther == 0)
153 {
154 Install (node);
155 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700156 grOther = node->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700157 NS_ASSERT (grOther != 0);
158
159 gr->AddIncidency (0, grOther);
160 }
161}
162
163void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700164GlobalRoutingHelper::Install (const NodeContainer &nodes)
Alexander Afanasyevce810142012-04-17 15:50:36 -0700165{
166 for (NodeContainer::Iterator node = nodes.Begin ();
167 node != nodes.End ();
168 node ++)
169 {
170 Install (*node);
171 }
172}
173
174void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700175GlobalRoutingHelper::InstallAll ()
Alexander Afanasyevce810142012-04-17 15:50:36 -0700176{
177 Install (NodeContainer::GetGlobal ());
178}
179
180
181void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700182GlobalRoutingHelper::AddOrigin (const std::string &prefix, Ptr<Node> node)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700183{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700184 Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter> ();
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700185 NS_ASSERT_MSG (gr != 0,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700186 "GlobalRouter is not installed on the node");
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700187
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700188 Ptr<NameComponents> name = Create<NameComponents> (boost::lexical_cast<NameComponents> (prefix));
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700189 gr->AddLocalPrefix (name);
190}
191
192void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700193GlobalRoutingHelper::AddOrigins (const std::string &prefix, const NodeContainer &nodes)
Alexander Afanasyev06d3a612012-04-17 22:25:40 -0700194{
195 for (NodeContainer::Iterator node = nodes.Begin ();
196 node != nodes.End ();
197 node++)
198 {
199 AddOrigin (prefix, *node);
200 }
201}
202
203void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700204GlobalRoutingHelper::AddOrigin (const std::string &prefix, const std::string &nodeName)
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700205{
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 Afanasyevad3757f2012-04-17 10:27:59 -0700212void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700213GlobalRoutingHelper::CalculateRoutes ()
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700214{
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700215 /**
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 Afanasyev4aac5572012-08-09 10:49:55 -0700220 BOOST_CONCEPT_ASSERT(( VertexListGraphConcept< NdnGlobalRouterGraph > ));
221 BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept< NdnGlobalRouterGraph > ));
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700222
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700223 NdnGlobalRouterGraph graph;
224 typedef graph_traits < NdnGlobalRouterGraph >::vertex_descriptor vertex_descriptor;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700225
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700226 // 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 Afanasyeva5abcd92012-04-17 13:34:43 -0700229 for (NodeList::Iterator node = NodeList::Begin (); node != NodeList::End (); node++)
230 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700231 Ptr<GlobalRouter> source = (*node)->GetObject<GlobalRouter> ();
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700232 if (source == 0)
233 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700234 NS_LOG_DEBUG ("Node " << (*node)->GetId () << " does not export GlobalRouter interface");
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700235 continue;
236 }
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700237
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700238 DistancesMap distances;
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700239
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700240 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 Afanasyevad3757f2012-04-17 10:27:59 -0700253
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700254 // NS_LOG_DEBUG (predecessors.size () << ", " << distances.size ());
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700255
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700256 Ptr<Fib> fib = source->GetObject<Fib> ();
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700257 fib->InvalidateAll ();
258 NS_ASSERT (fib != 0);
259
260 // cout << "Reachability from Node: " << source->GetObject<Node> ()->GetId () << endl;
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700261 for (DistancesMap::iterator i = distances.begin ();
262 i != distances.end ();
263 i++)
264 {
265 if (i->first == source)
266 continue;
267 else
268 {
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700269 // cout << " Node " << i->first->GetObject<Node> ()->GetId ();
270 if (i->second.get<0> () == 0)
271 {
272 // cout << " is unreachable" << endl;
273 }
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700274 else
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700275 {
276 // cout << " reachable via face " << *i->second.get<0> ()
277 // << " with distance " << i->second.get<1> () << endl;
278
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700279 BOOST_FOREACH (const Ptr<const NameComponents> &prefix, i->first->GetLocalPrefixes ())
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700280 {
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700281 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 Afanasyev8e2f1122012-04-17 15:01:11 -0700286 }
287 }
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700288 }
289 }
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -0700290 }
Alexander Afanasyevad3757f2012-04-17 10:27:59 -0700291}
292
293
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700294} // namespace ndn
295} // namespace ns3