NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
ndn-l3-protocol.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #include "ndn-l3-protocol.hpp"
21 
22 #include "ns3/packet.h"
23 #include "ns3/node.h"
24 #include "ns3/log.h"
25 #include "ns3/callback.h"
26 #include "ns3/uinteger.h"
27 #include "ns3/trace-source-accessor.h"
28 #include "ns3/object-vector.h"
29 #include "ns3/pointer.h"
30 #include "ns3/simulator.h"
31 
33 
34 #include "../helper/ndn-stack-helper.hpp"
35 #include "cs/ndn-content-store.hpp"
36 
37 #include <boost/property_tree/info_parser.hpp>
38 
39 #include "ns3/ndnSIM/NFD/daemon/fw/forwarder.hpp"
40 #include "ns3/ndnSIM/NFD/daemon/face/internal-face.hpp"
41 #include "ns3/ndnSIM/NFD/daemon/face/internal-transport.hpp"
42 #include "ns3/ndnSIM/NFD/daemon/mgmt/fib-manager.hpp"
43 #include "ns3/ndnSIM/NFD/daemon/mgmt/face-manager.hpp"
44 #include "ns3/ndnSIM/NFD/daemon/mgmt/strategy-choice-manager.hpp"
45 #include "ns3/ndnSIM/NFD/daemon/mgmt/forwarder-status-manager.hpp"
46 // #include "ns3/ndnSIM/NFD/daemon/mgmt/general-config-section.hpp"
47 #include "ns3/ndnSIM/NFD/daemon/mgmt/tables-config-section.hpp"
48 #include "ns3/ndnSIM/NFD/daemon/mgmt/command-authenticator.hpp"
49 
50 #include "ns3/ndnSIM/NFD/rib/rib-manager.hpp"
51 
52 #include "ns3/ndnSIM/NFD/daemon/face/null-face.hpp"
53 #include "ns3/ndnSIM/NFD/daemon/face/internal-face.hpp"
54 
55 #include "ns3/ndnSIM/NFD/core/config-file.hpp"
56 
57 #include <ndn-cxx/mgmt/dispatcher.hpp>
58 
59 NS_LOG_COMPONENT_DEFINE("ndn.L3Protocol");
60 
61 namespace ns3 {
62 namespace ndn {
63 
64 const uint16_t L3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
65 const uint16_t L3Protocol::IP_STACK_PORT = 9695;
66 
68 
69 TypeId
71 {
72  static TypeId tid =
73  TypeId("ns3::ndn::L3Protocol")
74  .SetGroupName("ndn")
75  .SetParent<Object>()
76  .AddConstructor<L3Protocol>()
77 
78  .AddTraceSource("OutInterests", "OutInterests",
79  MakeTraceSourceAccessor(&L3Protocol::m_outInterests),
80  "ns3::ndn::L3Protocol::InterestTraceCallback")
81  .AddTraceSource("InInterests", "InInterests",
82  MakeTraceSourceAccessor(&L3Protocol::m_inInterests),
83  "ns3::ndn::L3Protocol::InterestTraceCallback")
84 
86 
87  .AddTraceSource("OutData", "OutData", MakeTraceSourceAccessor(&L3Protocol::m_outData),
88  "ns3::ndn::L3Protocol::DataTraceCallback")
89  .AddTraceSource("InData", "InData", MakeTraceSourceAccessor(&L3Protocol::m_inData),
90  "ns3::ndn::L3Protocol::DataTraceCallback")
91 
93 
94  .AddTraceSource("OutNack", "OutNack", MakeTraceSourceAccessor(&L3Protocol::m_outNack),
95  "ns3::ndn::L3Protocol::NackTraceCallback")
96  .AddTraceSource("InNack", "InNack", MakeTraceSourceAccessor(&L3Protocol::m_inNack),
97  "ns3::ndn::L3Protocol::NackTraceCallback")
98 
100 
101  .AddTraceSource("SatisfiedInterests", "SatisfiedInterests",
102  MakeTraceSourceAccessor(&L3Protocol::m_satisfiedInterests),
103  "ns3::ndn::L3Protocol::SatisfiedInterestsCallback")
104  .AddTraceSource("TimedOutInterests", "TimedOutInterests",
105  MakeTraceSourceAccessor(&L3Protocol::m_timedOutInterests),
106  "ns3::ndn::L3Protocol::TimedOutInterestsCallback")
107  ;
108  return tid;
109 }
110 
112 private:
113  Impl()
114  {
115  // Do not modify initial config file. Use helpers to set specific NFD parameters
116  std::string initialConfig =
117  "general\n"
118  "{\n"
119  "}\n"
120  "\n"
121  "tables\n"
122  "{\n"
123  " cs_max_packets 100\n"
124  "\n"
125  " strategy_choice\n"
126  " {\n"
127  " / /localhost/nfd/strategy/best-route\n"
128  " /localhost /localhost/nfd/strategy/multicast\n"
129  " /localhost/nfd /localhost/nfd/strategy/best-route\n"
130  " /ndn/multicast /localhost/nfd/strategy/multicast\n"
131  " }\n"
132  "}\n"
133  "\n"
134  // "face_system\n"
135  // "{\n"
136  // "}\n"
137  "\n"
138  "authorizations\n"
139  "{\n"
140  " authorize\n"
141  " {\n"
142  " certfile any\n"
143  " privileges\n"
144  " {\n"
145  " faces\n"
146  " fib\n"
147  " strategy-choice\n"
148  " }\n"
149  " }\n"
150  "}\n"
151  "\n"
152  "rib\n"
153  "{\n"
154  " localhost_security\n"
155  " {\n"
156  " trust-anchor\n"
157  " {\n"
158  " type any\n"
159  " }\n"
160  " }\n"
161  "}\n"
162  "\n";
163 
164  std::istringstream input(initialConfig);
165  boost::property_tree::read_info(input, m_config);
166  }
167 
168  friend class L3Protocol;
169 
170  std::shared_ptr<nfd::Forwarder> m_forwarder;
171 
172  std::shared_ptr<nfd::Face> m_internalFace;
173  std::shared_ptr<::ndn::Face> m_internalClientFace;
174  std::shared_ptr<nfd::CommandAuthenticator> m_authenticator;
175 
176  std::shared_ptr<nfd::Face> m_internalRibFace;
177  std::shared_ptr<::ndn::Face> m_internalRibClientFace;
178 
179  std::unique_ptr<::ndn::mgmt::Dispatcher> m_dispatcher;
180  std::unique_ptr<::ndn::mgmt::Dispatcher> m_dispatcherRib;
181  std::shared_ptr<nfd::FibManager> m_fibManager;
182  std::shared_ptr<nfd::FaceManager> m_faceManager;
183  std::shared_ptr<nfd::StrategyChoiceManager> m_strategyChoiceManager;
184  std::shared_ptr<nfd::ForwarderStatusManager> m_forwarderStatusManager;
185  std::shared_ptr<nfd::rib::RibManager> m_ribManager;
186 
187  nfd::ConfigSection m_config;
188 
189  Ptr<ContentStore> m_csFromNdnSim;
190  PolicyCreationCallback m_policy;
191 };
192 
194  : m_impl(new Impl())
195 {
196  NS_LOG_FUNCTION(this);
197 }
198 
200 {
201  NS_LOG_FUNCTION(this);
202 }
203 
204 void
205 L3Protocol::initialize()
206 {
207  m_impl->m_forwarder = make_shared<nfd::Forwarder>();
208 
209  initializeManagement();
210 
211  nfd::FaceTable& faceTable = m_impl->m_forwarder->getFaceTable();
213  faceTable.addReserved(nfd::face::makeNullFace(FaceUri("contentstore://")), nfd::face::FACEID_CONTENT_STORE);
214 
215  if (!this->getConfig().get<bool>("ndnSIM.disable_rib_manager", false)) {
216  Simulator::ScheduleWithContext(m_node->GetId(), Seconds(0), &L3Protocol::initializeRibManager, this);
217  }
218 
219  m_impl->m_forwarder->beforeSatisfyInterest.connect(std::ref(m_satisfiedInterests));
220  m_impl->m_forwarder->beforeExpirePendingInterest.connect(std::ref(m_timedOutInterests));
221 }
222 
224 {
225 public:
226  IgnoreSections(const std::vector<std::string>& ignored)
227  : m_ignored(ignored)
228  {
229  }
230 
231  void
232  operator()(const std::string& filename, const std::string& sectionName,
233  const nfd::ConfigSection& section, bool isDryRun)
234 
235  {
236  if (std::find(m_ignored.begin(), m_ignored.end(), sectionName) == m_ignored.end()) {
237  nfd::ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
238  }
239  }
240 private:
241  std::vector<std::string> m_ignored;
242 };
243 
244 void
246 {
247  m_impl->m_internalFace->sendInterest(interest);
248 }
249 
250 void
252 {
253  m_impl->m_policy = policy;
254 }
255 
256 void
257 L3Protocol::initializeManagement()
258 {
259  auto& forwarder = m_impl->m_forwarder;
260  using namespace nfd;
261 
262  std::tie(m_impl->m_internalFace, m_impl->m_internalClientFace) = face::makeInternalFace(StackHelper::getKeyChain());
263  forwarder->getFaceTable().addReserved(m_impl->m_internalFace, face::FACEID_INTERNAL_FACE);
264  m_impl->m_dispatcher.reset(new ::ndn::mgmt::Dispatcher(*m_impl->m_internalClientFace, StackHelper::getKeyChain()));
265 
266  m_impl->m_authenticator = CommandAuthenticator::create();
267 
268  m_impl->m_fibManager.reset(new FibManager(forwarder->getFib(),
269  forwarder->getFaceTable(),
270  *m_impl->m_dispatcher,
271  *m_impl->m_authenticator));
272 
273  // Cannot be disabled for now
274  // if (!this->getConfig().get<bool>("ndnSIM.disable_face_manager", false)) {
275  m_impl->m_faceManager.reset(new FaceManager(forwarder->getFaceTable(),
276  *m_impl->m_dispatcher,
277  *m_impl->m_authenticator));
278  // }
279  // else {
280  // this->getConfig().get_child("authorizations").get_child("authorize").get_child("privileges").erase("faces");
281  // }
282 
283  if (!this->getConfig().get<bool>("ndnSIM.disable_strategy_choice_manager", false)) {
284  m_impl->m_strategyChoiceManager.reset(new StrategyChoiceManager(forwarder->getStrategyChoice(),
285  *m_impl->m_dispatcher,
286  *m_impl->m_authenticator));
287  }
288  else {
289  this->getConfig().get_child("authorizations").get_child("authorize").get_child("privileges").erase("strategy-choice");
290  }
291 
292  if (!this->getConfig().get<bool>("ndnSIM.disable_forwarder_status_manager", false)) {
293  m_impl->m_forwarderStatusManager.reset(new ForwarderStatusManager(*forwarder, *m_impl->m_dispatcher));
294  }
295 
296  ConfigFile config(&ConfigFile::ignoreUnknownSection);
297 
298  // if we use NFD's CS, we have to specify a replacement policy
299  m_impl->m_csFromNdnSim = GetObject<ContentStore>();
300  if (m_impl->m_csFromNdnSim == nullptr) {
301  forwarder->getCs().setPolicy(m_impl->m_policy());
302  }
303 
304  TablesConfigSection tablesConfig(*forwarder);
305  tablesConfig.setConfigFile(config);
306 
307  m_impl->m_authenticator->setConfigFile(config);
308 
309  // if (!this->getConfig().get<bool>("ndnSIM.disable_face_manager", false)) {
310  m_impl->m_faceManager->setConfigFile(config);
311  // }
312 
313  // apply config
314  config.parse(m_impl->m_config, false, "ndnSIM.conf");
315 
316  tablesConfig.ensureConfigured();
317 
318  // add FIB entry for NFD Management Protocol
319  Name topPrefix("/localhost/nfd");
320  auto entry = forwarder->getFib().insert(topPrefix).first;
321  entry->addNextHop(*(m_impl->m_internalFace), 0);
322  m_impl->m_dispatcher->addTopPrefix(topPrefix, false);
323 }
324 
325 void
326 L3Protocol::initializeRibManager()
327 {
328  using namespace nfd;
329 
330  std::tie(m_impl->m_internalRibFace, m_impl->m_internalRibClientFace) = face::makeInternalFace(StackHelper::getKeyChain());
331  m_impl->m_forwarder->getFaceTable().add(m_impl->m_internalRibFace);
332 
333  m_impl->m_dispatcherRib.reset(new ::ndn::mgmt::Dispatcher(*m_impl->m_internalRibClientFace, StackHelper::getKeyChain()));
334 
335  m_impl->m_ribManager = make_shared<rib::RibManager>(*(m_impl->m_dispatcherRib), *(m_impl->m_internalRibClientFace),
337 
338  ConfigFile config([] (const std::string& filename, const std::string& sectionName,
339  const ConfigSection& section, bool isDryRun) {
340  // Ignore "log" and sections belonging to NFD,
341  // but raise an error if we're missing a handler for a "rib" section.
342  if (sectionName != "rib" || sectionName == "log") {
343  // do nothing
344  }
345  else {
346  // missing RIB section
347  ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
348  }
349  });
350 
351  m_impl->m_ribManager->setConfigFile(config);
352 
353  // apply config
354  config.parse(m_impl->m_config, false, "ndnSIM.conf");
355 
356  m_impl->m_ribManager->registerWithNfd();
357 }
358 
359 shared_ptr<nfd::Forwarder>
361 {
362  return m_impl->m_forwarder;
363 }
364 
365 shared_ptr<nfd::FibManager>
367 {
368  return m_impl->m_fibManager;
369 }
370 
371 shared_ptr<nfd::StrategyChoiceManager>
373 {
374  return m_impl->m_strategyChoiceManager;
375 }
376 
379 {
380  return m_impl->m_config;
381 }
382 
383 /*
384  * This method is called by AddAgregate and completes the aggregation
385  * by setting the node in the ndn stack
386  */
387 void
389 {
390  if (m_node == nullptr) {
391  m_node = GetObject<Node>();
392  if (m_node != nullptr) {
393  initialize();
394 
395  NS_ASSERT(m_impl->m_forwarder != nullptr);
396  m_impl->m_csFromNdnSim = GetObject<ContentStore>();
397  if (m_impl->m_csFromNdnSim != nullptr) {
398  m_impl->m_forwarder->setCsFromNdnSim(m_impl->m_csFromNdnSim);
399  }
400  }
401  }
402 
403  Object::NotifyNewAggregate();
404 }
405 
406 void
408 {
409  NS_LOG_FUNCTION(this);
410 
411  // MUST HAPPEN BEFORE Simulator IS DESTROYED
412  m_impl.reset();
413 
414  nfd::scheduler::getGlobalScheduler().cancelAllEvents();
415 
416  m_node = 0;
417 
418  Object::DoDispose();
419 }
420 
422 L3Protocol::addFace(shared_ptr<Face> face)
423 {
424  NS_LOG_FUNCTION(this << face.get());
425 
426  m_impl->m_forwarder->addFace(face);
427 
428  std::weak_ptr<Face> weakFace = face;
429 
430  // // Connect Signals to TraceSource
431  face->afterReceiveInterest.connect([this, weakFace](const Interest& interest) {
432  shared_ptr<Face> face = weakFace.lock();
433  if (face != nullptr) {
434  this->m_inInterests(interest, *face);
435  }
436  });
437 
438  face->afterReceiveData.connect([this, weakFace](const Data& data) {
439  shared_ptr<Face> face = weakFace.lock();
440  if (face != nullptr) {
441  this->m_inData(data, *face);
442  }
443  });
444 
445  face->afterReceiveNack.connect([this, weakFace](const lp::Nack& nack) {
446  shared_ptr<Face> face = weakFace.lock();
447  if (face != nullptr) {
448  this->m_inNack(nack, *face);
449  }
450  });
451 
452  auto tracingLink = face->getLinkService();
453  NS_LOG_LOGIC("Adding trace sources for afterSendInterest and afterSendData");
454  tracingLink->afterSendInterest.connect([this, weakFace](const Interest& interest) {
455  shared_ptr<Face> face = weakFace.lock();
456  if (face != nullptr) {
457  this->m_outInterests(interest, *face);
458  }
459  });
460 
461  tracingLink->afterSendData.connect([this, weakFace](const Data& data) {
462  shared_ptr<Face> face = weakFace.lock();
463  if (face != nullptr) {
464  this->m_outData(data, *face);
465  }
466  });
467 
468  tracingLink->afterSendNack.connect([this, weakFace](const lp::Nack& nack) {
469  shared_ptr<Face> face = weakFace.lock();
470  if (face != nullptr) {
471  this->m_outNack(nack, *face);
472  }
473  });
474 
475  return face->getId();
476 }
477 
478 shared_ptr<Face>
480 {
481  return m_impl->m_forwarder->getFaceTable().get(id)->shared_from_this();
482 }
483 
484 shared_ptr<Face>
485 L3Protocol::getFaceByNetDevice(Ptr<NetDevice> netDevice) const
486 {
487  for (auto& i : m_impl->m_forwarder->getFaceTable()) {
488  auto transport = dynamic_cast<NetDeviceTransport*>(i.getTransport());
489  if (transport == nullptr)
490  continue;
491 
492  if (transport->GetNetDevice() == netDevice)
493  return i.shared_from_this();
494  }
495  return nullptr;
496 }
497 
498 Ptr<L3Protocol>
499 L3Protocol::getL3Protocol(Ptr<Object> node)
500 {
501  Ptr<L3Protocol> retval = node->GetObject<L3Protocol>();
502  NS_ASSERT_MSG(retval != nullptr, "L3Protocol is not aggregated on this object");
503  return retval;
504 }
505 
506 } // namespace ndn
507 } // namespace ns3
static const uint16_t IP_STACK_PORT
TCP/UDP port for NDN stack.
Copyright (c) 2011-2015 Regents of the University of California.
shared_ptr< Face > makeNullFace(const FaceUri &uri)
Definition: null-face.cpp:37
std::tuple< shared_ptr< Face >, shared_ptr< ndn::Face > > makeInternalFace(ndn::KeyChain &clientKeyChain)
make a pair of forwarder-side face and client-side face that are connected with each other ...
shared_ptr< nfd::Forwarder > getForwarder()
Get smart pointer to nfd::Forwarder installed on the node.
static const uint16_t ETHERNET_FRAME_TYPE
Ethernet Frame Type of Ndn.
NS_OBJECT_ENSURE_REGISTERED(ContentStore)
nfd::ConfigSection & getConfig()
Get NFD config (boost::property_tree)
configuration file parsing utility
Definition: config-file.hpp:50
const FaceId FACEID_INTERNAL_FACE
identifies the InternalFace used in management
Definition: face.hpp:44
void ensureConfigured()
apply default configuration, if tables section was omitted in configuration file
void operator()(const std::string &filename, const std::string &sectionName, const nfd::ConfigSection &section, bool isDryRun)
std::function< std::unique_ptr< nfd::cs::Policy >)> PolicyCreationCallback
implement the Forwarder Status of NFD Management Protocol.
shared_ptr< nfd::FibManager > getFibManager()
Get smart pointer to nfd::FibManager, used by node&#39;s NFD.
virtual void NotifyNewAggregate()
This function will notify other components connected to the node that a new stack member is now conne...
FibManager
Definition: fib-manager.cpp:33
shared_ptr< Face > getFaceByNetDevice(Ptr< NetDevice > netDevice) const
Remove face from ndn stack (remove callbacks)
ndn mgmt Dispatcher
Copyright (c) 2013-2016 Regents of the University of California.
Definition: dispatcher.cpp:28
represents a Network Nack
Definition: nack.hpp:40
static TypeId GetTypeId()
Interface ID.
container of all faces
Definition: face-table.hpp:37
void setCsReplacementPolicy(const PolicyCreationCallback &policy)
Set the replacement policy of NFD&#39;s CS.
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:40
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::FaceId addFace(shared_ptr< Face > face)
Add face to NDN stack.
Copyright (c) 2011-2015 Regents of the University of California.
boost::property_tree::ptree ConfigSection
L3Protocol()
Default constructor.
ndn L3Protocol
Copyright (c) 2011-2015 Regents of the University of California.
shared_ptr< nfd::StrategyChoiceManager > getStrategyChoiceManager()
Get smart pointer to nfd::StrategyChoiceManager, used by node&#39;s NFD.
Implementation network-layer of NDN stack.
static Ptr< L3Protocol > getL3Protocol(Ptr< Object > node)
handles &#39;tables&#39; config section
virtual void DoDispose(void)
Do cleanup.
void injectInterest(const Interest &interest)
Inject interest through internal Face.
void addReserved(shared_ptr< Face > face, FaceId faceId)
add a special face with a reserved FaceId
Definition: face-table.cpp:71
IgnoreSections(const std::vector< std::string > &ignored)
static KeyChain & getKeyChain()
const FaceId FACEID_NULL
identifies the NullFace that drops every packet
Definition: face.hpp:48
uint64_t FaceId
identifies a face
Definition: face.hpp:39
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:86
void setConfigFile(ConfigFile &configFile)
const FaceId FACEID_CONTENT_STORE
identifies a packet comes from the ContentStore
Definition: face.hpp:46
shared_ptr< Face > getFaceById(nfd::FaceId face) const
Get face by face ID.
Scheduler & getGlobalScheduler()
Definition: scheduler.cpp:37