NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ndn-global-routing-helper.cc
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 
21 #if __clang__
22 #pragma clang diagnostic push
23 #pragma clang diagnostic ignored "-Wunused-variable"
24 #pragma clang diagnostic ignored "-Wunneeded-internal-declaration"
25 #endif
26 
27 #include "ndn-global-routing-helper.h"
28 
29 #include "ns3/ndn-l3-protocol.h"
30 #include "../model/ndn-net-device-face.h"
31 #include "../model/ndn-global-router.h"
32 #include "ns3/ndn-name.h"
33 #include "ns3/ndn-fib.h"
34 
35 #include "ns3/node.h"
36 #include "ns3/node-container.h"
37 #include "ns3/net-device.h"
38 #include "ns3/channel.h"
39 #include "ns3/log.h"
40 #include "ns3/assert.h"
41 #include "ns3/names.h"
42 #include "ns3/node-list.h"
43 #include "ns3/channel-list.h"
44 #include "ns3/object-factory.h"
45 
46 #include <boost/lexical_cast.hpp>
47 #include <boost/foreach.hpp>
48 #include <boost/concept/assert.hpp>
49 // #include <boost/graph/graph_concepts.hpp>
50 // #include <boost/graph/adjacency_list.hpp>
51 #include <boost/graph/dijkstra_shortest_paths.hpp>
52 
53 #include "boost-graph-ndn-global-routing-helper.h"
54 
55 #include <math.h>
56 
57 NS_LOG_COMPONENT_DEFINE ("ndn.GlobalRoutingHelper");
58 
59 using namespace std;
60 using namespace boost;
61 
62 namespace ns3 {
63 namespace ndn {
64 
65 void
66 GlobalRoutingHelper::Install (Ptr<Node> node)
67 {
68  NS_LOG_LOGIC ("Node: " << node->GetId ());
69 
70  Ptr<L3Protocol> ndn = node->GetObject<L3Protocol> ();
71  NS_ASSERT_MSG (ndn != 0, "Cannot install GlobalRoutingHelper before Ndn is installed on a node");
72 
73  Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter> ();
74  if (gr != 0)
75  {
76  NS_LOG_DEBUG ("GlobalRouter is already installed: " << gr);
77  return; // already installed
78  }
79 
80  gr = CreateObject<GlobalRouter> ();
81  node->AggregateObject (gr);
82 
83  for (uint32_t faceId = 0; faceId < ndn->GetNFaces (); faceId++)
84  {
85  Ptr<NetDeviceFace> face = DynamicCast<NetDeviceFace> (ndn->GetFace (faceId));
86  if (face == 0)
87  {
88  NS_LOG_DEBUG ("Skipping non-netdevice face");
89  continue;
90  }
91 
92  Ptr<NetDevice> nd = face->GetNetDevice ();
93  if (nd == 0)
94  {
95  NS_LOG_DEBUG ("Not a NetDevice associated with NetDeviceFace");
96  continue;
97  }
98 
99  Ptr<Channel> ch = nd->GetChannel ();
100 
101  if (ch == 0)
102  {
103  NS_LOG_DEBUG ("Channel is not associated with NetDevice");
104  continue;
105  }
106 
107  if (ch->GetNDevices () == 2) // e.g., point-to-point channel
108  {
109  for (uint32_t deviceId = 0; deviceId < ch->GetNDevices (); deviceId ++)
110  {
111  Ptr<NetDevice> otherSide = ch->GetDevice (deviceId);
112  if (nd == otherSide) continue;
113 
114  Ptr<Node> otherNode = otherSide->GetNode ();
115  NS_ASSERT (otherNode != 0);
116 
117  Ptr<GlobalRouter> otherGr = otherNode->GetObject<GlobalRouter> ();
118  if (otherGr == 0)
119  {
120  Install (otherNode);
121  }
122  otherGr = otherNode->GetObject<GlobalRouter> ();
123  NS_ASSERT (otherGr != 0);
124  gr->AddIncidency (face, otherGr);
125  }
126  }
127  else
128  {
129  Ptr<GlobalRouter> grChannel = ch->GetObject<GlobalRouter> ();
130  if (grChannel == 0)
131  {
132  Install (ch);
133  }
134  grChannel = ch->GetObject<GlobalRouter> ();
135 
136  gr->AddIncidency (face, grChannel);
137  }
138  }
139 }
140 
141 void
142 GlobalRoutingHelper::Install (Ptr<Channel> channel)
143 {
144  NS_LOG_LOGIC ("Channel: " << channel->GetId ());
145 
146  Ptr<GlobalRouter> gr = channel->GetObject<GlobalRouter> ();
147  if (gr != 0)
148  return;
149 
150  gr = CreateObject<GlobalRouter> ();
151  channel->AggregateObject (gr);
152 
153  for (uint32_t deviceId = 0; deviceId < channel->GetNDevices (); deviceId ++)
154  {
155  Ptr<NetDevice> dev = channel->GetDevice (deviceId);
156 
157  Ptr<Node> node = dev->GetNode ();
158  NS_ASSERT (node != 0);
159 
160  Ptr<GlobalRouter> grOther = node->GetObject<GlobalRouter> ();
161  if (grOther == 0)
162  {
163  Install (node);
164  }
165  grOther = node->GetObject<GlobalRouter> ();
166  NS_ASSERT (grOther != 0);
167 
168  gr->AddIncidency (0, grOther);
169  }
170 }
171 
172 void
173 GlobalRoutingHelper::Install (const NodeContainer &nodes)
174 {
175  for (NodeContainer::Iterator node = nodes.Begin ();
176  node != nodes.End ();
177  node ++)
178  {
179  Install (*node);
180  }
181 }
182 
183 void
184 GlobalRoutingHelper::InstallAll ()
185 {
186  Install (NodeContainer::GetGlobal ());
187 }
188 
189 
190 void
191 GlobalRoutingHelper::AddOrigin (const std::string &prefix, Ptr<Node> node)
192 {
193  Ptr<GlobalRouter> gr = node->GetObject<GlobalRouter> ();
194  NS_ASSERT_MSG (gr != 0,
195  "GlobalRouter is not installed on the node");
196 
197  Ptr<Name> name = Create<Name> (boost::lexical_cast<Name> (prefix));
198  gr->AddLocalPrefix (name);
199 }
200 
201 void
202 GlobalRoutingHelper::AddOrigins (const std::string &prefix, const NodeContainer &nodes)
203 {
204  for (NodeContainer::Iterator node = nodes.Begin ();
205  node != nodes.End ();
206  node++)
207  {
208  AddOrigin (prefix, *node);
209  }
210 }
211 
212 void
213 GlobalRoutingHelper::AddOrigin (const std::string &prefix, const std::string &nodeName)
214 {
215  Ptr<Node> node = Names::Find<Node> (nodeName);
216  NS_ASSERT_MSG (node != 0, nodeName << "is not a Node");
217 
218  AddOrigin (prefix, node);
219 }
220 
221 void
222 GlobalRoutingHelper::AddOriginsForAll ()
223 {
224  for (NodeList::Iterator node = NodeList::Begin (); node != NodeList::End (); node ++)
225  {
226  Ptr<GlobalRouter> gr = (*node)->GetObject<GlobalRouter> ();
227  string name = Names::FindName (*node);
228 
229  if (gr != 0 && !name.empty ())
230  {
231  AddOrigin ("/"+name, *node);
232  }
233  }
234 }
235 
236 void
237 GlobalRoutingHelper::CalculateRoutes (bool invalidatedRoutes/* = true*/)
238 {
244  BOOST_CONCEPT_ASSERT(( VertexListGraphConcept< NdnGlobalRouterGraph > ));
245  BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept< NdnGlobalRouterGraph > ));
246 
247  NdnGlobalRouterGraph graph;
248  // typedef graph_traits < NdnGlobalRouterGraph >::vertex_descriptor vertex_descriptor;
249 
250  // For now we doing Dijkstra for every node. Can be replaced with Bellman-Ford or Floyd-Warshall.
251  // Other algorithms should be faster, but they need additional EdgeListGraph concept provided by the graph, which
252  // is not obviously how implement in an efficient manner
253  for (NodeList::Iterator node = NodeList::Begin (); node != NodeList::End (); node++)
254  {
255  Ptr<GlobalRouter> source = (*node)->GetObject<GlobalRouter> ();
256  if (source == 0)
257  {
258  NS_LOG_DEBUG ("Node " << (*node)->GetId () << " does not export GlobalRouter interface");
259  continue;
260  }
261 
262  DistancesMap distances;
263 
264  dijkstra_shortest_paths (graph, source,
265  // predecessor_map (boost::ref(predecessors))
266  // .
267  distance_map (boost::ref(distances))
268  .
269  distance_inf (WeightInf)
270  .
271  distance_zero (WeightZero)
272  .
273  distance_compare (boost::WeightCompare ())
274  .
275  distance_combine (boost::WeightCombine ())
276  );
277 
278  // NS_LOG_DEBUG (predecessors.size () << ", " << distances.size ());
279 
280  Ptr<Fib> fib = source->GetObject<Fib> ();
281  if (invalidatedRoutes)
282  {
283  fib->InvalidateAll ();
284  }
285  NS_ASSERT (fib != 0);
286 
287  NS_LOG_DEBUG ("Reachability from Node: " << source->GetObject<Node> ()->GetId ());
288  for (DistancesMap::iterator i = distances.begin ();
289  i != distances.end ();
290  i++)
291  {
292  if (i->first == source)
293  continue;
294  else
295  {
296  // cout << " Node " << i->first->GetObject<Node> ()->GetId ();
297  if (i->second.get<0> () == 0)
298  {
299  // cout << " is unreachable" << endl;
300  }
301  else
302  {
303  BOOST_FOREACH (const Ptr<const Name> &prefix, i->first->GetLocalPrefixes ())
304  {
305  NS_LOG_DEBUG (" prefix " << prefix << " reachable via face " << *i->second.get<0> ()
306  << " with distance " << i->second.get<1> ()
307  << " with delay " << i->second.get<2> ());
308 
309  Ptr<fib::Entry> entry = fib->Add (prefix, i->second.get<0> (), i->second.get<1> ());
310  entry->SetRealDelayToProducer (i->second.get<0> (), Seconds (i->second.get<2> ()));
311 
312  Ptr<Limits> faceLimits = i->second.get<0> ()->GetObject<Limits> ();
313 
314  Ptr<Limits> fibLimits = entry->GetObject<Limits> ();
315  if (fibLimits != 0)
316  {
317  // if it was created by the forwarding strategy via DidAddFibEntry event
318  fibLimits->SetLimits (faceLimits->GetMaxRate (), 2 * i->second.get<2> () /*exact RTT*/);
319  NS_LOG_DEBUG ("Set limit for prefix " << *prefix << " " << faceLimits->GetMaxRate () << " / " <<
320  2*i->second.get<2> () << "s (" << faceLimits->GetMaxRate () * 2 * i->second.get<2> () << ")");
321  }
322  }
323  }
324  }
325  }
326  }
327 }
328 
329 void
330 GlobalRoutingHelper::CalculateAllPossibleRoutes (bool invalidatedRoutes/* = true*/)
331 {
337  BOOST_CONCEPT_ASSERT(( VertexListGraphConcept< NdnGlobalRouterGraph > ));
338  BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept< NdnGlobalRouterGraph > ));
339 
340  NdnGlobalRouterGraph graph;
341  // typedef graph_traits < NdnGlobalRouterGraph >::vertex_descriptor vertex_descriptor;
342 
343  // For now we doing Dijkstra for every node. Can be replaced with Bellman-Ford or Floyd-Warshall.
344  // Other algorithms should be faster, but they need additional EdgeListGraph concept provided by the graph, which
345  // is not obviously how implement in an efficient manner
346  for (NodeList::Iterator node = NodeList::Begin (); node != NodeList::End (); node++)
347  {
348  Ptr<GlobalRouter> source = (*node)->GetObject<GlobalRouter> ();
349  if (source == 0)
350  {
351  NS_LOG_DEBUG ("Node " << (*node)->GetId () << " does not export GlobalRouter interface");
352  continue;
353  }
354 
355  Ptr<Fib> fib = source->GetObject<Fib> ();
356  if (invalidatedRoutes)
357  {
358  fib->InvalidateAll ();
359  }
360  NS_ASSERT (fib != 0);
361 
362  NS_LOG_DEBUG ("===========");
363  NS_LOG_DEBUG ("Reachability from Node: " << source->GetObject<Node> ()->GetId () << " (" << Names::FindName (source->GetObject<Node> ()) << ")");
364 
365  Ptr<L3Protocol> l3 = source->GetObject<L3Protocol> ();
366  NS_ASSERT (l3 != 0);
367 
368  // remember interface statuses
369  std::vector<uint16_t> originalMetric (l3->GetNFaces ());
370  for (uint32_t faceId = 0; faceId < l3->GetNFaces (); faceId++)
371  {
372  originalMetric[faceId] = l3->GetFace (faceId)->GetMetric ();
373  l3->GetFace (faceId)->SetMetric (std::numeric_limits<uint16_t>::max ()-1); // value std::numeric_limits<uint16_t>::max () MUST NOT be used (reserved)
374  }
375 
376  for (uint32_t enabledFaceId = 0; enabledFaceId < l3->GetNFaces (); enabledFaceId++)
377  {
378  if (DynamicCast<ndn::NetDeviceFace> (l3->GetFace (enabledFaceId)) == 0)
379  continue;
380 
381  // enabling only faceId
382  l3->GetFace (enabledFaceId)->SetMetric (originalMetric[enabledFaceId]);
383 
384  DistancesMap distances;
385 
386  NS_LOG_DEBUG ("-----------");
387 
388  dijkstra_shortest_paths (graph, source,
389  // predecessor_map (boost::ref(predecessors))
390  // .
391  distance_map (boost::ref(distances))
392  .
393  distance_inf (WeightInf)
394  .
395  distance_zero (WeightZero)
396  .
397  distance_compare (boost::WeightCompare ())
398  .
399  distance_combine (boost::WeightCombine ())
400  );
401 
402  // NS_LOG_DEBUG (predecessors.size () << ", " << distances.size ());
403 
404  for (DistancesMap::iterator i = distances.begin ();
405  i != distances.end ();
406  i++)
407  {
408  if (i->first == source)
409  continue;
410  else
411  {
412  // cout << " Node " << i->first->GetObject<Node> ()->GetId ();
413  if (i->second.get<0> () == 0)
414  {
415  // cout << " is unreachable" << endl;
416  }
417  else
418  {
419  BOOST_FOREACH (const Ptr<const Name> &prefix, i->first->GetLocalPrefixes ())
420  {
421  NS_LOG_DEBUG (" prefix " << *prefix << " reachable via face " << *i->second.get<0> ()
422  << " with distance " << i->second.get<1> ()
423  << " with delay " << i->second.get<2> ());
424 
425  if (i->second.get<0> ()->GetMetric () == std::numeric_limits<uint16_t>::max ()-1)
426  continue;
427 
428  Ptr<fib::Entry> entry = fib->Add (prefix, i->second.get<0> (), i->second.get<1> ());
429  entry->SetRealDelayToProducer (i->second.get<0> (), Seconds (i->second.get<2> ()));
430 
431  Ptr<Limits> faceLimits = i->second.get<0> ()->GetObject<Limits> ();
432 
433  Ptr<Limits> fibLimits = entry->GetObject<Limits> ();
434  if (fibLimits != 0)
435  {
436  // if it was created by the forwarding strategy via DidAddFibEntry event
437  fibLimits->SetLimits (faceLimits->GetMaxRate (), 2 * i->second.get<2> () /*exact RTT*/);
438  NS_LOG_DEBUG ("Set limit for prefix " << *prefix << " " << faceLimits->GetMaxRate () << " / " <<
439  2*i->second.get<2> () << "s (" << faceLimits->GetMaxRate () * 2 * i->second.get<2> () << ")");
440  }
441  }
442  }
443  }
444  }
445 
446  // disabling the face again
447  l3->GetFace (enabledFaceId)->SetMetric (std::numeric_limits<uint16_t>::max ()-1);
448  }
449 
450  // recover original interface statuses
451  for (uint32_t faceId = 0; faceId < l3->GetNFaces (); faceId++)
452  {
453  l3->GetFace (faceId)->SetMetric (originalMetric[faceId]);
454  }
455  }
456 }
457 
458 
459 } // namespace ndn
460 } // namespace ns3
Abstract class to manage Interest limits.
Definition: ndn-limits.h:35
Class for NDN Name.
Definition: name.h:29
void AddIncidency(Ptr< Face > face, Ptr< GlobalRouter > ndn)
Add edge to the node.
Implementation network-layer of NDN stack.
virtual void InvalidateAll()=0
Invalidate all FIB entries.
Class representing global router interface for ndnSIM.
Class implementing FIB functionality.
Definition: ndn-fib.h:44
virtual void SetLimits(double rate, double delay)
Set limit for the number of outstanding interests.
Definition: ndn-limits.h:61