NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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 
36 #include <boost/property_tree/info_parser.hpp>
37 
38 #include "ns3/ndnSIM/NFD/daemon/fw/forwarder.hpp"
39 #include "ns3/ndnSIM/NFD/daemon/face/internal-face.hpp"
40 #include "ns3/ndnSIM/NFD/daemon/face/internal-transport.hpp"
41 #include "ns3/ndnSIM/NFD/daemon/mgmt/fib-manager.hpp"
42 #include "ns3/ndnSIM/NFD/daemon/mgmt/face-manager.hpp"
43 #include "ns3/ndnSIM/NFD/daemon/mgmt/strategy-choice-manager.hpp"
44 #include "ns3/ndnSIM/NFD/daemon/mgmt/cs-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/daemon/rib/service.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/daemon/common/global.hpp"
56 #include "ns3/ndnSIM/NFD/daemon/common/config-file.hpp"
57 
59 
60 NS_LOG_COMPONENT_DEFINE("ndn.L3Protocol");
61 
62 namespace ns3 {
63 namespace ndn {
64 
65 const uint16_t L3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
66 const uint16_t L3Protocol::IP_STACK_PORT = 9695;
67 
69 
70 TypeId
72 {
73  static TypeId tid =
74  TypeId("ns3::ndn::L3Protocol")
75  .SetGroupName("ndn")
76  .SetParent<Object>()
77  .AddConstructor<L3Protocol>()
78 
79  .AddTraceSource("OutInterests", "OutInterests",
80  MakeTraceSourceAccessor(&L3Protocol::m_outInterests),
81  "ns3::ndn::L3Protocol::InterestTraceCallback")
82  .AddTraceSource("InInterests", "InInterests",
83  MakeTraceSourceAccessor(&L3Protocol::m_inInterests),
84  "ns3::ndn::L3Protocol::InterestTraceCallback")
85 
87 
88  .AddTraceSource("OutData", "OutData", MakeTraceSourceAccessor(&L3Protocol::m_outData),
89  "ns3::ndn::L3Protocol::DataTraceCallback")
90  .AddTraceSource("InData", "InData", MakeTraceSourceAccessor(&L3Protocol::m_inData),
91  "ns3::ndn::L3Protocol::DataTraceCallback")
92 
94 
95  .AddTraceSource("OutNack", "OutNack", MakeTraceSourceAccessor(&L3Protocol::m_outNack),
96  "ns3::ndn::L3Protocol::NackTraceCallback")
97  .AddTraceSource("InNack", "InNack", MakeTraceSourceAccessor(&L3Protocol::m_inNack),
98  "ns3::ndn::L3Protocol::NackTraceCallback")
99 
101 
102  .AddTraceSource("SatisfiedInterests", "SatisfiedInterests",
103  MakeTraceSourceAccessor(&L3Protocol::m_satisfiedInterests),
104  "ns3::ndn::L3Protocol::SatisfiedInterestsCallback")
105  .AddTraceSource("TimedOutInterests", "TimedOutInterests",
106  MakeTraceSourceAccessor(&L3Protocol::m_timedOutInterests),
107  "ns3::ndn::L3Protocol::TimedOutInterestsCallback")
108  ;
109  return tid;
110 }
111 
113 private:
114  Impl()
115  {
116  // Do not modify initial config file. Use helpers to set specific NFD parameters
117  std::string initialConfig =
118  "general\n"
119  "{\n"
120  "}\n"
121  "\n"
122  "tables\n"
123  "{\n"
124  " cs_max_packets 100\n"
125  "\n"
126  " strategy_choice\n"
127  " {\n"
128  " / /localhost/nfd/strategy/best-route\n"
129  " /localhost /localhost/nfd/strategy/multicast\n"
130  " /localhost/nfd /localhost/nfd/strategy/best-route\n"
131  " /ndn/multicast /localhost/nfd/strategy/multicast\n"
132  " }\n"
133  "}\n"
134  "\n"
135  // "face_system\n"
136  // "{\n"
137  // "}\n"
138  "\n"
139  "authorizations\n"
140  "{\n"
141  " authorize\n"
142  " {\n"
143  " certfile any\n"
144  " privileges\n"
145  " {\n"
146  " faces\n"
147  " fib\n"
148  " strategy-choice\n"
149  " }\n"
150  " }\n"
151  "}\n"
152  "\n"
153  "rib\n"
154  "{\n"
155  " localhost_security\n"
156  " {\n"
157  " trust-anchor\n"
158  " {\n"
159  " type any\n"
160  " }\n"
161  " }\n"
162  "}\n"
163  "\n";
164 
165  std::istringstream input(initialConfig);
166  boost::property_tree::read_info(input, m_config);
167  }
168 
169  friend class L3Protocol;
170 
171  // note that shared_ptr needed for Python bindings
172 
173  std::shared_ptr<::nfd::Forwarder> m_forwarder;
174  std::unique_ptr<::nfd::FaceTable> m_faceTable;
175  std::unique_ptr<::nfd::face::FaceSystem> m_faceSystem;
176 
177  std::shared_ptr<::nfd::face::Face> m_internalFace;
178  std::shared_ptr<::ndn::Face> m_internalClientFace;
179  std::unique_ptr<::ndn::mgmt::Dispatcher> m_dispatcher;
180  std::shared_ptr<::nfd::CommandAuthenticator> m_authenticator;
181  std::unique_ptr<::nfd::ForwarderStatusManager> m_forwarderStatusManager;
182  std::unique_ptr<::nfd::FaceManager> m_faceManager;
183  std::shared_ptr<::nfd::FibManager> m_fibManager;
184  std::unique_ptr<::nfd::CsManager> m_csManager;
185  std::unique_ptr<::nfd::StrategyChoiceManager> m_strategyChoiceManager;
186 
187  std::shared_ptr<::nfd::face::Face> m_internalFaceForInjects;
188  std::shared_ptr<::ndn::Face> m_internalClientFaceForInjects;
189 
190  std::shared_ptr<::nfd::Face> m_internalRibFace;
191  std::shared_ptr<::ndn::Face> m_internalRibClientFace;
192  std::unique_ptr<::nfd::rib::Service> m_ribService;
193 
194  nfd::ConfigSection m_config;
195 
196  PolicyCreationCallback m_policy;
197 };
198 
200  : m_impl(new Impl())
201 {
202  NS_LOG_FUNCTION(this);
203 }
204 
206 {
207  NS_LOG_FUNCTION(this);
208 }
209 
210 void
211 L3Protocol::initialize()
212 {
213  m_impl->m_faceTable = make_unique<::nfd::FaceTable>();
214  m_impl->m_forwarder = make_shared<::nfd::Forwarder>(*m_impl->m_faceTable);
215  m_impl->m_faceSystem = make_unique<::nfd::face::FaceSystem>(*m_impl->m_faceTable, nullptr);
216 
217  initializeManagement();
218  initializeRibManager();
219 
220  m_impl->m_forwarder->beforeSatisfyInterest.connect(std::ref(m_satisfiedInterests));
221  m_impl->m_forwarder->beforeExpirePendingInterest.connect(std::ref(m_timedOutInterests));
222 }
223 
225 {
226 public:
227  IgnoreSections(const std::vector<std::string>& ignored)
228  : m_ignored(ignored)
229  {
230  }
231 
232  void
233  operator()(const std::string& filename, const std::string& sectionName,
234  const nfd::ConfigSection& section, bool isDryRun)
235 
236  {
237  if (std::find(m_ignored.begin(), m_ignored.end(), sectionName) == m_ignored.end()) {
238  nfd::ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
239  }
240  }
241 private:
242  std::vector<std::string> m_ignored;
243 };
244 
245 void
247 {
248  m_impl->m_internalClientFaceForInjects->expressInterest(interest, nullptr, nullptr, nullptr);
249 }
250 
251 void
253 {
254  m_impl->m_policy = policy;
255 }
256 
257 void
258 L3Protocol::initializeManagement()
259 {
260  auto& forwarder = m_impl->m_forwarder;
261  using namespace nfd;
262 
263  std::tie(m_impl->m_internalFace, m_impl->m_internalClientFace) = face::makeInternalFace(StackHelper::getKeyChain());
264  m_impl->m_faceTable->addReserved(m_impl->m_internalFace, face::FACEID_INTERNAL_FACE);
265 
266  std::tie(m_impl->m_internalFaceForInjects, m_impl->m_internalClientFaceForInjects) = face::makeInternalFace(StackHelper::getKeyChain());
267  m_impl->m_faceTable->addReserved(m_impl->m_internalFaceForInjects, face::FACEID_INTERNAL_FACE + 1);
268 
269  m_impl->m_dispatcher = make_unique<::ndn::mgmt::Dispatcher>(*m_impl->m_internalClientFace, StackHelper::getKeyChain());
270  m_impl->m_authenticator = ::nfd::CommandAuthenticator::create();
271 
272  if (!this->getConfig().get<bool>("ndnSIM.disable_forwarder_status_manager", false)) {
273  m_impl->m_forwarderStatusManager = make_unique<::nfd::ForwarderStatusManager>(*m_impl->m_forwarder, *m_impl->m_dispatcher);
274  }
275  m_impl->m_faceManager = make_unique<::nfd::FaceManager>(*m_impl->m_faceSystem, *m_impl->m_dispatcher, *m_impl->m_authenticator);
276  m_impl->m_fibManager = make_shared<::nfd::FibManager>(m_impl->m_forwarder->getFib(), *m_impl->m_faceTable,
277  *m_impl->m_dispatcher, *m_impl->m_authenticator);
278  m_impl->m_csManager = make_unique<::nfd::CsManager>(m_impl->m_forwarder->getCs(), m_impl->m_forwarder->getCounters(),
279  *m_impl->m_dispatcher, *m_impl->m_authenticator);
280  if (!this->getConfig().get<bool>("ndnSIM.disable_strategy_choice_manager", false)) {
281  m_impl->m_strategyChoiceManager = make_unique<::nfd::StrategyChoiceManager>(m_impl->m_forwarder->getStrategyChoice(),
282  *m_impl->m_dispatcher, *m_impl->m_authenticator);
283 
284  }
285  else {
286  this->getConfig().get_child("authorizations").get_child("authorize").get_child("privileges").erase("strategy-choice");
287  }
288 
289  ConfigFile config(&ConfigFile::ignoreUnknownSection);
290 
291  forwarder->getCs().setPolicy(m_impl->m_policy());
292 
293  TablesConfigSection tablesConfig(*forwarder);
294  tablesConfig.setConfigFile(config);
295 
296  m_impl->m_authenticator->setConfigFile(config);
297 
298  // if (!this->getConfig().get<bool>("ndnSIM.disable_face_manager", false)) {
299  m_impl->m_faceSystem->setConfigFile(config);
300  // }
301 
302  // apply config
303  config.parse(m_impl->m_config, false, "ndnSIM.conf");
304 
305  tablesConfig.ensureConfigured();
306 
307  // add FIB entry for NFD Management Protocol
308  Name topPrefix("/localhost/nfd");
309  auto entry = m_impl->m_forwarder->getFib().insert(topPrefix).first;
310  m_impl->m_forwarder->getFib().addOrUpdateNextHop(*entry, *m_impl->m_internalFace, 0);
311  m_impl->m_dispatcher->addTopPrefix(topPrefix, false);
312 }
313 
314 void
315 L3Protocol::initializeRibManager()
316 {
317  using namespace nfd;
318 
319  std::tie(m_impl->m_internalRibFace, m_impl->m_internalRibClientFace) = face::makeInternalFace(StackHelper::getKeyChain());
320  m_impl->m_faceTable->add(m_impl->m_internalRibFace);
321 
322  m_impl->m_ribService = make_unique<rib::Service>(m_impl->m_config,
323  std::ref(*m_impl->m_internalRibClientFace),
324  std::ref(StackHelper::getKeyChain()));
325 }
326 
327 shared_ptr<nfd::Forwarder>
329 {
330  return m_impl->m_forwarder;
331 }
332 
335 {
336  return *m_impl->m_faceTable;
337 }
338 
339 shared_ptr<nfd::FibManager>
341 {
342  return m_impl->m_fibManager;
343 }
344 
347 {
348  return *m_impl->m_strategyChoiceManager;
349 }
350 
353 {
354  return *m_impl->m_ribService;
355 }
356 
359 {
360  return m_impl->m_config;
361 }
362 
363 /*
364  * This method is called by AddAgregate and completes the aggregation
365  * by setting the node in the ndn stack
366  */
367 void
369 {
370  if (m_node == nullptr) {
371  m_node = GetObject<Node>();
372  if (m_node != nullptr) {
373  initialize();
374  }
375  }
376 
377  Object::NotifyNewAggregate();
378 }
379 
380 void
382 {
383  NS_LOG_FUNCTION(this);
384 
385  // MUST HAPPEN BEFORE Simulator IS DESTROYED
386  m_impl.reset();
387 
389 
390  m_node = 0;
391 
392  Object::DoDispose();
393 }
394 
396 L3Protocol::addFace(shared_ptr<Face> face)
397 {
398  NS_LOG_FUNCTION(this << face.get());
399 
400  m_impl->m_faceTable->add(face);
401 
402  std::weak_ptr<Face> weakFace = face;
403 
404  // // Connect Signals to TraceSource
405  face->afterReceiveInterest.connect([this, weakFace](const Interest& interest, const nfd::EndpointId&) {
406  shared_ptr<Face> face = weakFace.lock();
407  if (face != nullptr) {
408  this->m_inInterests(interest, *face);
409  }
410  });
411 
412  face->afterReceiveData.connect([this, weakFace](const Data& data, const nfd::EndpointId&) {
413  shared_ptr<Face> face = weakFace.lock();
414  if (face != nullptr) {
415  this->m_inData(data, *face);
416  }
417  });
418 
419  face->afterReceiveNack.connect([this, weakFace](const lp::Nack& nack, const nfd::EndpointId&) {
420  shared_ptr<Face> face = weakFace.lock();
421  if (face != nullptr) {
422  this->m_inNack(nack, *face);
423  }
424  });
425 
426  auto tracingLink = face->getLinkService();
427  NS_LOG_LOGIC("Adding trace sources for afterSendInterest and afterSendData");
428  tracingLink->afterSendInterest.connect([this, weakFace](const Interest& interest) {
429  shared_ptr<Face> face = weakFace.lock();
430  if (face != nullptr) {
431  this->m_outInterests(interest, *face);
432  }
433  });
434 
435  tracingLink->afterSendData.connect([this, weakFace](const Data& data) {
436  shared_ptr<Face> face = weakFace.lock();
437  if (face != nullptr) {
438  this->m_outData(data, *face);
439  }
440  });
441 
442  tracingLink->afterSendNack.connect([this, weakFace](const lp::Nack& nack) {
443  shared_ptr<Face> face = weakFace.lock();
444  if (face != nullptr) {
445  this->m_outNack(nack, *face);
446  }
447  });
448 
449  return face->getId();
450 }
451 
452 shared_ptr<Face>
454 {
455  return m_impl->m_faceTable->get(id)->shared_from_this();
456 }
457 
458 shared_ptr<Face>
459 L3Protocol::getFaceByNetDevice(Ptr<NetDevice> netDevice) const
460 {
461  for (auto& i : *m_impl->m_faceTable) {
462  auto transport = dynamic_cast<NetDeviceTransport*>(i.getTransport());
463  if (transport == nullptr)
464  continue;
465 
466  if (transport->GetNetDevice() == netDevice)
467  return i.shared_from_this();
468  }
469  return nullptr;
470 }
471 
472 Ptr<L3Protocol>
473 L3Protocol::getL3Protocol(Ptr<Object> node)
474 {
475  Ptr<L3Protocol> retval = node->GetObject<L3Protocol>();
476  NS_ASSERT_MSG(retval != nullptr, "L3Protocol is not aggregated on this object");
477  return retval;
478 }
479 
480 } // namespace ndn
481 } // 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.
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.
nfd::FaceTable & getFaceTable()
static const uint16_t ETHERNET_FRAME_TYPE
Ethernet Frame Type of Ndn.
initializes and executes NFD-RIB service thread
Definition: service.hpp:52
Implements the Strategy Choice Management of NFD Management Protocol.
nfd::ConfigSection & getConfig()
Get NFD config (boost::property_tree)
configuration file parsing utility
Definition: config-file.hpp:57
const FaceId FACEID_INTERNAL_FACE
identifies the InternalFace used in management
Definition: face-common.hpp:49
NS_OBJECT_ENSURE_REGISTERED(GlobalRouter)
void resetGlobalScheduler()
Definition: global.cpp:92
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
shared_ptr< nfd::FibManager > getFibManager()
Get smart pointer to nfd::FibManager, used by node&#39;s NFD.
uint64_t EndpointId
Identifies a remote endpoint on the link.
Definition: face-common.hpp:71
virtual void NotifyNewAggregate()
This function will notify other components connected to the node that a new stack member is now conne...
shared_ptr< Face > getFaceByNetDevice(Ptr< NetDevice > netDevice) const
Remove face from ndn stack (remove callbacks)
represents a Network Nack
Definition: nack.hpp:38
static TypeId GetTypeId()
Interface ID.
::nfd::rib::Service & getRibService()
container of all faces
Definition: face-table.hpp:38
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:41
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
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
a config file section
L3Protocol()
Default constructor.
ndn L3Protocol
Copyright (c) 2011-2015 Regents of the University of California.
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.
nfd::StrategyChoiceManager & getStrategyChoiceManager()
Get nfd::StrategyChoiceManager, used by node&#39;s NFD.
void injectInterest(const Interest &interest)
Inject interest through internal Face.
static shared_ptr< CommandAuthenticator > create()
IgnoreSections(const std::vector< std::string > &ignored)
static KeyChain & getKeyChain()
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:84
void setConfigFile(ConfigFile &configFile)
shared_ptr< Face > getFaceById(nfd::FaceId face) const
Get face by face ID.
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:44