NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
nfd.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "nfd.hpp"
27 
28 #include "core/global-io.hpp"
29 #include "core/logger-factory.hpp"
31 #include "core/config-file.hpp"
32 #include "fw/forwarder.hpp"
33 #include "face/null-face.hpp"
34 #include "face/internal-face.hpp"
35 #include "mgmt/fib-manager.hpp"
36 #include "mgmt/face-manager.hpp"
42 
43 #include <ndn-cxx/mgmt/dispatcher.hpp>
44 
45 namespace nfd {
46 
47 NFD_LOG_INIT("Nfd");
48 
49 static const std::string INTERNAL_CONFIG = "internal://nfd.conf";
50 
51 static inline ndn::util::NetworkMonitor*
53 {
54  try {
56  }
57  catch (const ndn::util::NetworkMonitor::Error& e) {
58  NFD_LOG_WARN(e.what());
59  return nullptr;
60  }
61 }
62 
63 Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain)
64  : m_configFile(configFile)
65  , m_keyChain(keyChain)
66  , m_networkMonitor(makeNetworkMonitor())
67 {
68 }
69 
70 Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain)
71  : m_configSection(config)
72  , m_keyChain(keyChain)
73  , m_networkMonitor(makeNetworkMonitor())
74 {
75 }
76 
78 {
79  // It is necessary to explicitly define the destructor, because some member variables (e.g.,
80  // unique_ptr<Forwarder>) are forward-declared, but implicitly declared destructor requires
81  // complete types for all members when instantiated.
82 }
83 
84 void
86 {
87  initializeLogging();
88 
89  m_forwarder.reset(new Forwarder());
90 
91  initializeManagement();
92 
93  FaceTable& faceTable = m_forwarder->getFaceTable();
95  faceTable.addReserved(face::makeNullFace(FaceUri("contentstore://")), face::FACEID_CONTENT_STORE);
96 
98 
99  if (m_networkMonitor) {
100  m_networkMonitor->onNetworkStateChanged.connect([this] {
101  // delay stages, so if multiple events are triggered in short sequence,
102  // only one auto-detection procedure is triggered
103  m_reloadConfigEvent = scheduler::schedule(time::seconds(5),
104  [this] {
105  NFD_LOG_INFO("Network change detected, reloading face section of the config file...");
106  this->reloadConfigFileFaceSection();
107  });
108  });
109  }
110 }
111 
112 void
113 Nfd::initializeLogging()
114 {
116  LoggerFactory::getInstance().setConfigFile(config);
117 
118  if (!m_configFile.empty()) {
119  config.parse(m_configFile, true);
120  config.parse(m_configFile, false);
121  }
122  else {
123  config.parse(m_configSection, true, INTERNAL_CONFIG);
124  config.parse(m_configSection, false, INTERNAL_CONFIG);
125  }
126 }
127 
128 
129 static inline void
130 ignoreRibAndLogSections(const std::string& filename, const std::string& sectionName,
131  const ConfigSection& section, bool isDryRun)
132 {
133  // Ignore "log" and "rib" sections, but raise an error if we're missing a
134  // handler for an NFD section.
135  if (sectionName == "rib" || sectionName == "log") {
136  // do nothing
137  }
138  else {
139  // missing NFD section
140  ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
141  }
142 }
143 
144 void
145 Nfd::initializeManagement()
146 {
147  std::tie(m_internalFace, m_internalClientFace) = face::makeInternalFace(m_keyChain);
148  m_forwarder->getFaceTable().addReserved(m_internalFace, face::FACEID_INTERNAL_FACE);
149  m_dispatcher.reset(new ndn::mgmt::Dispatcher(*m_internalClientFace, m_keyChain));
150 
151  m_validator.reset(new CommandValidator());
152 
153  m_fibManager.reset(new FibManager(m_forwarder->getFib(),
154  bind(&Forwarder::getFace, m_forwarder.get(), _1),
155  *m_dispatcher,
156  *m_validator));
157 
158  m_faceManager.reset(new FaceManager(m_forwarder->getFaceTable(),
159  *m_dispatcher,
160  *m_validator));
161 
162  m_strategyChoiceManager.reset(new StrategyChoiceManager(m_forwarder->getStrategyChoice(),
163  *m_dispatcher,
164  *m_validator));
165 
166  m_forwarderStatusManager.reset(new ForwarderStatusManager(*m_forwarder, *m_dispatcher));
167 
169  general::setConfigFile(config);
170 
171  TablesConfigSection tablesConfig(m_forwarder->getCs(),
172  m_forwarder->getPit(),
173  m_forwarder->getFib(),
174  m_forwarder->getStrategyChoice(),
175  m_forwarder->getMeasurements(),
176  m_forwarder->getNetworkRegionTable());
177  tablesConfig.setConfigFile(config);
178 
179  m_validator->setConfigFile(config);
180 
181  m_faceManager->setConfigFile(config);
182 
183  // parse config file
184  if (!m_configFile.empty()) {
185  config.parse(m_configFile, true);
186  config.parse(m_configFile, false);
187  }
188  else {
189  config.parse(m_configSection, true, INTERNAL_CONFIG);
190  config.parse(m_configSection, false, INTERNAL_CONFIG);
191  }
192 
193  tablesConfig.ensureTablesAreConfigured();
194 
195  // add FIB entry for NFD Management Protocol
196  Name topPrefix("/localhost/nfd");
197  auto entry = m_forwarder->getFib().insert(topPrefix).first;
198  entry->addNextHop(m_internalFace, 0);
199  m_dispatcher->addTopPrefix(topPrefix, false);
200 }
201 
202 void
204 {
205  // Logging
206  initializeLogging();
208 
209  // Other stuff
211 
212  general::setConfigFile(config);
213 
214  TablesConfigSection tablesConfig(m_forwarder->getCs(),
215  m_forwarder->getPit(),
216  m_forwarder->getFib(),
217  m_forwarder->getStrategyChoice(),
218  m_forwarder->getMeasurements(),
219  m_forwarder->getNetworkRegionTable());
220 
221  tablesConfig.setConfigFile(config);
222 
223  m_validator->setConfigFile(config);
224  m_faceManager->setConfigFile(config);
225 
226  if (!m_configFile.empty()) {
227  config.parse(m_configFile, false);
228  }
229  else {
230  config.parse(m_configSection, false, INTERNAL_CONFIG);
231  }
232 }
233 
234 void
235 Nfd::reloadConfigFileFaceSection()
236 {
237  // reload only face_system section of the config file to re-initialize multicast faces
239  m_faceManager->setConfigFile(config);
240 
241  if (!m_configFile.empty()) {
242  config.parse(m_configFile, false);
243  }
244  else {
245  config.parse(m_configSection, false, INTERNAL_CONFIG);
246  }
247 }
248 
249 } // namespace nfd
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 ...
Nfd(const std::string &configFile, ndn::KeyChain &keyChain)
Create NFD instance using absolute or relative path to configFile.
Definition: nfd.cpp:63
Network state change monitor.
represents a dispatcher on server side of NFD Management protocol
Definition: dispatcher.hpp:129
static const std::string INTERNAL_CONFIG
Definition: nfd.cpp:49
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
void setConfigFile(ConfigFile &configFile)
The packet signing interface.
Definition: key-chain.hpp:48
const FaceId FACEID_INTERNAL_FACE
identifies the InternalFace used in management
Definition: face.hpp:44
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
static void ignoreRibAndLogSections(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: nfd.cpp:130
implement the Forwarder Status of NFD Management Protocol.
FibManager
Definition: fib-manager.cpp:31
#define NFD_LOG_WARN(expression)
Definition: logger.hpp:58
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
void reloadConfigFile()
Reload configuration file and apply update (if any)
Definition: nfd.cpp:203
container of all Faces
Definition: face-table.hpp:38
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
boost::property_tree::ptree ConfigSection
Name abstraction to represent an absolute name.
Definition: name.hpp:46
Forwarder
Definition: forwarder.cpp:38
shared_ptr< Face > getFace(FaceId id) const
get existing Face
Definition: forwarder.hpp:283
Provides parsing for tables configuration file section.
void initialize()
Perform initialization of NFD instance After initialization, NFD instance can be started by invoking ...
Definition: nfd.cpp:85
~Nfd()
Destructor.
Definition: nfd.cpp:77
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
EventId schedule(const time::nanoseconds &after, const Scheduler::Event &event)
schedule an event
Definition: scheduler.cpp:47
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:53
void addReserved(shared_ptr< Face > face, FaceId faceId)
add a special Face with a reserved FaceId
Definition: face-table.cpp:74
static ndn::util::NetworkMonitor * makeNetworkMonitor()
Definition: nfd.cpp:52
const FaceId FACEID_NULL
identifies the NullFace that drops every packet
Definition: face.hpp:48
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