NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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 
32 #include "ndn-face.hpp"
33 
34 #include "ndn-net-device-face.hpp"
35 #include "../helper/ndn-stack-helper.hpp"
36 #include "cs/ndn-content-store.hpp"
37 
38 #include <boost/property_tree/info_parser.hpp>
39 
40 #include "ns3/ndnSIM/NFD/daemon/fw/forwarder.hpp"
41 #include "ns3/ndnSIM/NFD/daemon/mgmt/internal-face.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/status-server.hpp"
46 #include "ns3/ndnSIM/NFD/rib/rib-manager.hpp"
47 
48 #include "ns3/ndnSIM/NFD/daemon/face/null-face.hpp"
49 #include "ns3/ndnSIM/NFD/core/config-file.hpp"
50 #include "ns3/ndnSIM/NFD/daemon/mgmt/general-config-section.hpp"
51 #include "ns3/ndnSIM/NFD/daemon/mgmt/tables-config-section.hpp"
52 
53 NS_LOG_COMPONENT_DEFINE("ndn.L3Protocol");
54 
55 namespace ns3 {
56 namespace ndn {
57 
58 const uint16_t L3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
59 const uint16_t L3Protocol::IP_STACK_PORT = 9695;
60 
62 
63 TypeId
65 {
66  static TypeId tid =
67  TypeId("ns3::ndn::L3Protocol")
68  .SetGroupName("ndn")
69  .SetParent<Object>()
70  .AddConstructor<L3Protocol>()
71 
72  .AddTraceSource("OutInterests", "OutInterests",
73  MakeTraceSourceAccessor(&L3Protocol::m_outInterests),
74  "ns3::ndn::L3Protocol::InterestTraceCallback")
75  .AddTraceSource("InInterests", "InInterests",
76  MakeTraceSourceAccessor(&L3Protocol::m_inInterests),
77  "ns3::ndn::L3Protocol::InterestTraceCallback")
78 
80 
81  .AddTraceSource("OutData", "OutData", MakeTraceSourceAccessor(&L3Protocol::m_outData),
82  "ns3::ndn::L3Protocol::DataTraceCallback")
83  .AddTraceSource("InData", "InData", MakeTraceSourceAccessor(&L3Protocol::m_inData),
84  "ns3::ndn::L3Protocol::DataTraceCallback")
85 
87 
88  .AddTraceSource("SatisfiedInterests", "SatisfiedInterests",
89  MakeTraceSourceAccessor(&L3Protocol::m_satisfiedInterests),
90  "ns3::ndn::L3Protocol::SatisfiedInterestsCallback")
91  .AddTraceSource("TimedOutInterests", "TimedOutInterests",
92  MakeTraceSourceAccessor(&L3Protocol::m_timedOutInterests),
93  "ns3::ndn::L3Protocol::TimedOutInterestsCallback")
94  ;
95  return tid;
96 }
97 
99 private:
100  Impl()
101  {
102  // Do not modify initial config file. Use helpers to set specific NFD parameters
103  std::string initialConfig =
104  "general\n"
105  "{\n"
106  "}\n"
107  "\n"
108  "tables\n"
109  "{\n"
110  " cs_max_packets 100\n"
111  "\n"
112  " strategy_choice\n"
113  " {\n"
114  " / /localhost/nfd/strategy/best-route\n"
115  " /localhost /localhost/nfd/strategy/multicast\n"
116  " /localhost/nfd /localhost/nfd/strategy/best-route\n"
117  " /ndn/multicast /localhost/nfd/strategy/multicast\n"
118  " }\n"
119  "}\n"
120  "\n"
121  // "face_system\n"
122  // "{\n"
123  // "}\n"
124  "\n"
125  "authorizations\n"
126  "{\n"
127  " authorize\n"
128  " {\n"
129  " certfile any\n"
130  " privileges\n"
131  " {\n"
132  " faces\n"
133  " fib\n"
134  " strategy-choice\n"
135  " }\n"
136  " }\n"
137  "}\n"
138  "\n"
139  "rib\n"
140  "{\n"
141  " localhost_security\n"
142  " {\n"
143  " trust-anchor\n"
144  " {\n"
145  " type any\n"
146  " }\n"
147  " }\n"
148  "}\n"
149  "\n";
150 
151  std::istringstream input(initialConfig);
152  boost::property_tree::read_info(input, m_config);
153  }
154 
155  friend class L3Protocol;
156 
157  shared_ptr<nfd::Forwarder> m_forwarder;
158 
159  shared_ptr<nfd::InternalFace> m_internalFace;
160  shared_ptr<nfd::FibManager> m_fibManager;
161  shared_ptr<nfd::FaceManager> m_faceManager;
162  shared_ptr<nfd::StrategyChoiceManager> m_strategyChoiceManager;
163  shared_ptr<nfd::StatusServer> m_statusServer;
164  shared_ptr<nfd::rib::RibManager> m_ribManager;
165  shared_ptr< ::ndn::Face> m_face;
166 
167  nfd::ConfigSection m_config;
168 
169  Ptr<ContentStore> m_csFromNdnSim;
170 };
171 
173  : m_impl(new Impl())
174 {
175  NS_LOG_FUNCTION(this);
176 }
177 
179 {
180  NS_LOG_FUNCTION(this);
181 }
182 
183 void
184 L3Protocol::initialize()
185 {
186  m_impl->m_forwarder = make_shared<nfd::Forwarder>();
187 
188  initializeManagement();
189 
190  if (!this->getConfig().get<bool>("ndnSIM.disable_rib_manager", false)) {
191  Simulator::ScheduleWithContext(m_node->GetId(), Seconds(0), &L3Protocol::initializeRibManager, this);
192  }
193 
194  m_impl->m_forwarder->getFaceTable().addReserved(make_shared<nfd::NullFace>(), nfd::FACEID_NULL);
195 
196  m_impl->m_forwarder->beforeSatisfyInterest.connect(std::ref(m_satisfiedInterests));
197  m_impl->m_forwarder->beforeExpirePendingInterest.connect(std::ref(m_timedOutInterests));
198 }
199 
201 {
202 public:
203  IgnoreSections(const std::vector<std::string>& ignored)
204  : m_ignored(ignored)
205  {
206  }
207 
208  void
209  operator()(const std::string& filename, const std::string& sectionName,
210  const nfd::ConfigSection& section, bool isDryRun)
211 
212  {
213  if (std::find(m_ignored.begin(), m_ignored.end(), sectionName) == m_ignored.end()) {
214  nfd::ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
215  }
216  }
217 private:
218  std::vector<std::string> m_ignored;
219 };
220 
221 void
222 L3Protocol::initializeManagement()
223 {
224  auto& keyChain = StackHelper::getKeyChain();
225  auto& forwarder = m_impl->m_forwarder;
226  using namespace nfd;
227 
228  m_impl->m_internalFace = make_shared<InternalFace>();
229 
230  m_impl->m_fibManager = make_shared<FibManager>(std::ref(forwarder->getFib()),
231  bind(&Forwarder::getFace, forwarder.get(), _1),
232  m_impl->m_internalFace, keyChain);
233 
234  if (!this->getConfig().get<bool>("ndnSIM.disable_face_manager", false)) {
235  m_impl->m_faceManager = make_shared<FaceManager>(std::ref(forwarder->getFaceTable()),
236  m_impl->m_internalFace,
237  keyChain);
238  }
239  else {
240  this->getConfig().get_child("authorizations").get_child("authorize").get_child("privileges").erase("faces");
241  }
242 
243  if (!this->getConfig().get<bool>("ndnSIM.disable_strategy_choice_manager", false)) {
244  m_impl->m_strategyChoiceManager =
245  make_shared<StrategyChoiceManager>(std::ref(forwarder->getStrategyChoice()),
246  m_impl->m_internalFace,
247  keyChain);
248  }
249  else {
250  this->getConfig().get_child("authorizations").get_child("authorize").get_child("privileges").erase("strategy-choice");
251  }
252 
253  if (!this->getConfig().get<bool>("ndnSIM.disable_status_server", false)) {
254  m_impl->m_statusServer = make_shared<StatusServer>(m_impl->m_internalFace,
255  ref(*forwarder),
256  keyChain);
257  }
258 
259  ConfigFile config((IgnoreSections({"general", "log", "rib", "ndnSIM"})));
260 
261  TablesConfigSection tablesConfig(forwarder->getCs(),
262  forwarder->getPit(),
263  forwarder->getFib(),
264  forwarder->getStrategyChoice(),
265  forwarder->getMeasurements());
266  tablesConfig.setConfigFile(config);
267 
268  m_impl->m_internalFace->getValidator().setConfigFile(config);
269 
270  forwarder->getFaceTable().addReserved(m_impl->m_internalFace, FACEID_INTERNAL_FACE);
271 
272  m_impl->m_faceManager->setConfigFile(config);
273 
274  // apply config
275  config.parse(m_impl->m_config, false, "ndnSIM.conf");
276 
277  tablesConfig.ensureTablesAreConfigured();
278 
279  // add FIB entry for NFD Management Protocol
280  shared_ptr<fib::Entry> entry = forwarder->getFib().insert("/localhost/nfd").first;
281  entry->addNextHop(m_impl->m_internalFace, 0);
282 }
283 
284 void
285 L3Protocol::initializeRibManager()
286 {
287  using namespace nfd;
288 
289  m_impl->m_face = make_shared< ::ndn::Face>();
290  m_impl->m_ribManager = make_shared<rib::RibManager>(*(m_impl->m_face),
292 
293  ConfigFile config([] (const std::string& filename, const std::string& sectionName,
294  const ConfigSection& section, bool isDryRun) {
295  // Ignore "log" and sections belonging to NFD,
296  // but raise an error if we're missing a handler for a "rib" section.
297  if (sectionName != "rib" || sectionName == "log") {
298  // do nothing
299  }
300  else {
301  // missing NRD section
302  ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
303  }
304  });
305 
306  m_impl->m_ribManager->setConfigFile(config);
307 
308  // apply config
309  config.parse(m_impl->m_config, false, "ndnSIM.conf");
310 
311  m_impl->m_ribManager->registerWithNfd();
312 
313  m_impl->m_ribManager->enableLocalControlHeader();
314 }
315 
316 shared_ptr<nfd::Forwarder>
318 {
319  return m_impl->m_forwarder;
320 }
321 
322 shared_ptr<nfd::FibManager>
324 {
325  return m_impl->m_fibManager;
326 }
327 
328 shared_ptr<nfd::StrategyChoiceManager>
330 {
331  return m_impl->m_strategyChoiceManager;
332 }
333 
336 {
337  return m_impl->m_config;
338 }
339 
340 /*
341  * This method is called by AddAgregate and completes the aggregation
342  * by setting the node in the ndn stack
343  */
344 void
346 {
347  if (m_node == nullptr) {
348  m_node = GetObject<Node>();
349  if (m_node != nullptr) {
350  initialize();
351 
352  NS_ASSERT(m_impl->m_forwarder != nullptr);
353  m_impl->m_csFromNdnSim = GetObject<ContentStore>();
354  if (m_impl->m_csFromNdnSim != nullptr) {
355  m_impl->m_forwarder->setCsFromNdnSim(m_impl->m_csFromNdnSim);
356  }
357  }
358  }
359 
360  Object::NotifyNewAggregate();
361 }
362 
363 void
365 {
366  NS_LOG_FUNCTION(this);
367 
368  m_node = 0;
369 
370  Object::DoDispose();
371 }
372 
374 L3Protocol::addFace(shared_ptr<Face> face)
375 {
376  NS_LOG_FUNCTION(this << face.get());
377 
378  m_impl->m_forwarder->addFace(face);
379 
380  // Connect Signals to TraceSource
381  face->onReceiveInterest.connect
382  ([this, face](const Interest& interest) { this->m_inInterests(interest, *face); });
383 
384  face->onSendInterest.connect
385  ([this, face](const Interest& interest) { this->m_outInterests(interest, *face); });
386 
387  face->onReceiveData.connect([this, face](const Data& data) { this->m_inData(data, *face); });
388 
389  face->onSendData.connect([this, face](const Data& data) { this->m_outData(data, *face); });
390 
391  return face->getId();
392 }
393 
394 // void
395 // L3Protocol::removeFace(shared_ptr<Face> face)
396 // {
397 // NS_LOG_FUNCTION(this << std::cref(*face));
398 // }
399 
400 shared_ptr<Face>
402 {
403  return m_impl->m_forwarder->getFaceTable().get(id);
404 }
405 
406 shared_ptr<Face>
407 L3Protocol::getFaceByNetDevice(Ptr<NetDevice> netDevice) const
408 {
409  for (const auto& i : m_impl->m_forwarder->getFaceTable()) {
410  shared_ptr<NetDeviceFace> netDeviceFace = std::dynamic_pointer_cast<NetDeviceFace>(i);
411  if (netDeviceFace == nullptr)
412  continue;
413 
414  if (netDeviceFace->GetNetDevice() == netDevice)
415  return i;
416  }
417  return nullptr;
418 }
419 
420 Ptr<L3Protocol>
421 L3Protocol::getL3Protocol(Ptr<Object> node)
422 {
423  Ptr<L3Protocol> retval = node->GetObject<L3Protocol>();
424  NS_ASSERT_MSG(retval != nullptr, "L3Protocol is not aggregated on this object");
425  return retval;
426 }
427 
428 } // namespace ndn
429 } // namespace ns3
shared_ptr< Face > getFaceByNetDevice(Ptr< NetDevice > netDevice) const
Remove face from ndn stack (remove callbacks)
static const uint16_t IP_STACK_PORT
TCP/UDP port for NDN stack.
Copyright (c) 2011-2015 Regents of the University of California.
const FaceId FACEID_INTERNAL_FACE
identifies the InternalFace used in management
Definition: face.hpp:46
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)
Implementation of layer-2 (Ethernet) Ndn face.
shared_ptr< Face > getFaceById(nfd::FaceId face) const
Get face by face ID.
void operator()(const std::string &filename, const std::string &sectionName, const nfd::ConfigSection &section, bool isDryRun)
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...
static TypeId GetTypeId()
Interface ID.
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:34
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
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.
const FaceId FACEID_NULL
identifies the NullFace that drops every packet
Definition: face.hpp:50
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)
virtual void DoDispose(void)
Do cleanup.
IgnoreSections(const std::vector< std::string > &ignored)
static KeyChain & getKeyChain()
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:68
void setConfigFile(ConfigFile &configFile)
int FaceId
Definition: face.hpp:40