NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 #include "ns3/random-variable.h"
32 
33 #include "ndn-face.hpp"
34 
35 #include "ndn-net-device-face.hpp"
36 #include "../helper/ndn-stack-helper.hpp"
37 #include "cs/ndn-content-store.hpp"
38 
39 #include <boost/property_tree/info_parser.hpp>
40 
41 #include "ns3/ndnSIM/NFD/daemon/fw/forwarder.hpp"
42 #include "ns3/ndnSIM/NFD/daemon/mgmt/internal-face.hpp"
43 #include "ns3/ndnSIM/NFD/daemon/mgmt/fib-manager.hpp"
44 #include "ns3/ndnSIM/NFD/daemon/mgmt/face-manager.hpp"
45 #include "ns3/ndnSIM/NFD/daemon/mgmt/strategy-choice-manager.hpp"
46 #include "ns3/ndnSIM/NFD/daemon/mgmt/status-server.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  .AddTraceSource("InInterests", "InInterests",
75  MakeTraceSourceAccessor(&L3Protocol::m_inInterests))
76 
78 
79  .AddTraceSource("OutData", "OutData", MakeTraceSourceAccessor(&L3Protocol::m_outData))
80  .AddTraceSource("InData", "InData", MakeTraceSourceAccessor(&L3Protocol::m_inData))
81 
83 
84  .AddTraceSource("SatisfiedInterests", "SatisfiedInterests",
85  MakeTraceSourceAccessor(&L3Protocol::m_satisfiedInterests))
86  .AddTraceSource("TimedOutInterests", "TimedOutInterests",
87  MakeTraceSourceAccessor(&L3Protocol::m_timedOutInterests))
88  ;
89  return tid;
90 }
91 
93 private:
94  Impl()
95  {
96  // Do not modify initial config file. Use helpers to set specific NFD parameters
97  std::string initialConfig =
98  "general\n"
99  "{\n"
100  "}\n"
101  "\n"
102  "tables\n"
103  "{\n"
104  " cs_max_packets 100\n"
105  "\n"
106  " strategy_choice\n"
107  " {\n"
108  " / /localhost/nfd/strategy/best-route\n"
109  " /localhost /localhost/nfd/strategy/broadcast\n"
110  " /localhost/nfd /localhost/nfd/strategy/best-route\n"
111  " /ndn/broadcast /localhost/nfd/strategy/broadcast\n"
112  " }\n"
113  "}\n"
114  "\n"
115  // "face_system\n"
116  // "{\n"
117  // "}\n"
118  "\n"
119  "authorizations\n"
120  "{\n"
121  " authorize\n"
122  " {\n"
123  " certfile any\n"
124  " privileges\n"
125  " {\n"
126  " faces\n"
127  " fib\n"
128  " strategy-choice\n"
129  " }\n"
130  " }\n"
131  "}\n"
132  "\n"
133  "rib\n"
134  "{\n"
135  " localhost_security\n"
136  " {\n"
137  " trust-anchor\n"
138  " {\n"
139  " type any\n"
140  " }\n"
141  " }\n"
142  "}\n"
143  "\n";
144 
145  std::istringstream input(initialConfig);
146  boost::property_tree::read_info(input, m_config);
147  }
148 
149  friend class L3Protocol;
150 
151  shared_ptr<nfd::Forwarder> m_forwarder;
152 
153  shared_ptr<nfd::InternalFace> m_internalFace;
154  shared_ptr<nfd::FibManager> m_fibManager;
155  shared_ptr<nfd::FaceManager> m_faceManager;
156  shared_ptr<nfd::StrategyChoiceManager> m_strategyChoiceManager;
157  shared_ptr<nfd::StatusServer> m_statusServer;
158 
159  nfd::ConfigSection m_config;
160 
161  Ptr<ContentStore> m_csFromNdnSim;
162 };
163 
165  : m_impl(new Impl())
166 {
167  NS_LOG_FUNCTION(this);
168 }
169 
171 {
172  NS_LOG_FUNCTION(this);
173 }
174 
175 void
177 {
178  m_impl->m_forwarder = make_shared<nfd::Forwarder>();
179 
180  initializeManagement();
181 
182  m_impl->m_forwarder->getFaceTable().addReserved(make_shared<nfd::NullFace>(), nfd::FACEID_NULL);
183 
184  m_impl->m_forwarder->beforeSatisfyInterest.connect(std::ref(m_satisfiedInterests));
185  m_impl->m_forwarder->beforeExpirePendingInterest.connect(std::ref(m_timedOutInterests));
186 }
187 
189 {
190 public:
191  IgnoreSections(const std::vector<std::string>& ignored)
192  : m_ignored(ignored)
193  {
194  }
195 
196  void
197  operator()(const std::string& filename, const std::string& sectionName,
198  const nfd::ConfigSection& section, bool isDryRun)
199 
200  {
201  if (std::find(m_ignored.begin(), m_ignored.end(), sectionName) == m_ignored.end()) {
202  nfd::ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
203  }
204  }
205 private:
206  std::vector<std::string> m_ignored;
207 };
208 
209 void
210 L3Protocol::initializeManagement()
211 {
212  auto keyChain = std::ref(StackHelper::getKeyChain());
213  auto& forwarder = m_impl->m_forwarder;
214  using namespace nfd;
215 
216  m_impl->m_internalFace = make_shared<InternalFace>();
217 
218  m_impl->m_fibManager = make_shared<FibManager>(std::ref(forwarder->getFib()),
219  bind(&Forwarder::getFace, forwarder.get(), _1),
220  m_impl->m_internalFace, keyChain);
221 
222  m_impl->m_faceManager = make_shared<FaceManager>(std::ref(forwarder->getFaceTable()),
223  m_impl->m_internalFace,
224  keyChain);
225 
226  m_impl->m_strategyChoiceManager =
227  make_shared<StrategyChoiceManager>(std::ref(forwarder->getStrategyChoice()),
228  m_impl->m_internalFace,
229  keyChain);
230 
231  m_impl->m_statusServer = make_shared<StatusServer>(m_impl->m_internalFace,
232  ref(*forwarder),
233  keyChain);
234 
235  ConfigFile config((IgnoreSections({"general", "log", "rib"})));
236 
237  TablesConfigSection tablesConfig(forwarder->getCs(),
238  forwarder->getPit(),
239  forwarder->getFib(),
240  forwarder->getStrategyChoice(),
241  forwarder->getMeasurements());
242  tablesConfig.setConfigFile(config);
243 
244  m_impl->m_internalFace->getValidator().setConfigFile(config);
245 
246  forwarder->getFaceTable().addReserved(m_impl->m_internalFace, FACEID_INTERNAL_FACE);
247 
248  m_impl->m_faceManager->setConfigFile(config);
249 
250  // apply config
251  config.parse(m_impl->m_config, false, "ndnSIM.conf");
252 
253  tablesConfig.ensureTablesAreConfigured();
254 
255  // add FIB entry for NFD Management Protocol
256  shared_ptr<fib::Entry> entry = forwarder->getFib().insert("/localhost/nfd").first;
257  entry->addNextHop(m_impl->m_internalFace, 0);
258 }
259 
260 shared_ptr<nfd::Forwarder>
262 {
263  return m_impl->m_forwarder;
264 }
265 
266 shared_ptr<nfd::FibManager>
268 {
269  return m_impl->m_fibManager;
270 }
271 
272 shared_ptr<nfd::StrategyChoiceManager>
274 {
275  return m_impl->m_strategyChoiceManager;
276 }
277 
280 {
281  return m_impl->m_config;
282 }
283 
284 /*
285  * This method is called by AddAgregate and completes the aggregation
286  * by setting the node in the ndn stack
287  */
288 void
290 {
291  if (m_node == nullptr) {
292  m_node = GetObject<Node>();
293  if (m_node != nullptr) {
294  NS_ASSERT(m_impl->m_forwarder != nullptr);
295  m_impl->m_csFromNdnSim = GetObject<ContentStore>();
296  if (m_impl->m_csFromNdnSim != nullptr) {
297  m_impl->m_forwarder->setCsFromNdnSim(m_impl->m_csFromNdnSim);
298  }
299  }
300  }
301 
302  Object::NotifyNewAggregate();
303 }
304 
305 void
307 {
308  NS_LOG_FUNCTION(this);
309 
310  m_node = 0;
311 
312  Object::DoDispose();
313 }
314 
316 L3Protocol::addFace(shared_ptr<Face> face)
317 {
318  NS_LOG_FUNCTION(this << face.get());
319 
320  m_impl->m_forwarder->addFace(face);
321 
322  // Connect Signals to TraceSource
323  face->onReceiveInterest +=
324  [this, face](const Interest& interest) { this->m_inInterests(interest, *face); };
325 
326  face->onSendInterest +=
327  [this, face](const Interest& interest) { this->m_outInterests(interest, *face); };
328 
329  face->onReceiveData += [this, face](const Data& data) { this->m_inData(data, *face); };
330 
331  face->onSendData += [this, face](const Data& data) { this->m_outData(data, *face); };
332 
333  return face->getId();
334 }
335 
336 // void
337 // L3Protocol::removeFace(shared_ptr<Face> face)
338 // {
339 // NS_LOG_FUNCTION(this << std::cref(*face));
340 // }
341 
342 shared_ptr<Face>
344 {
345  return m_impl->m_forwarder->getFaceTable().get(id);
346 }
347 
348 shared_ptr<Face>
349 L3Protocol::getFaceByNetDevice(Ptr<NetDevice> netDevice) const
350 {
351  for (const auto& i : m_impl->m_forwarder->getFaceTable()) {
352  shared_ptr<NetDeviceFace> netDeviceFace = std::dynamic_pointer_cast<NetDeviceFace>(i);
353  if (netDeviceFace == nullptr)
354  continue;
355 
356  if (netDeviceFace->GetNetDevice() == netDevice)
357  return i;
358  }
359  return nullptr;
360 }
361 
362 Ptr<L3Protocol>
363 L3Protocol::getL3Protocol(Ptr<Object> node)
364 {
365  Ptr<L3Protocol> retval = node->GetObject<L3Protocol>();
366  NS_ASSERT_MSG(retval != nullptr, "L3Protocol is not aggregated on this object");
367  return retval;
368 }
369 
370 } // namespace ndn
371 } // 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.
const FaceId FACEID_INTERNAL_FACE
identifies the InternalFace used in management
Definition: face.hpp:47
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'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:36
nfd::FaceId addFace(shared_ptr< Face > face)
Add face to NDN stack.
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:51
shared_ptr< nfd::StrategyChoiceManager > getStrategyChoiceManager()
Get smart pointer to nfd::StrategyChoiceManager, used by node's NFD.
Implementation network-layer of NDN stack.
static Ptr< L3Protocol > getL3Protocol(Ptr< Object > node)
void initialize()
Initialize NFD instance.
virtual void DoDispose(void)
Do cleanup.
IgnoreSections(const std::vector< std::string > &ignored)
static KeyChain & getKeyChain()
void setConfigFile(ConfigFile &configFile)
int FaceId
Definition: face.hpp:41