NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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"
41 
42 namespace nfd {
43 
44 NFD_LOG_INIT("Nfd");
45 
46 static const std::string INTERNAL_CONFIG = "internal://nfd.conf";
47 
48 static inline ndn::util::NetworkMonitor*
50 {
51  try {
53  }
54  catch (const ndn::util::NetworkMonitor::Error& e) {
55  NFD_LOG_WARN(e.what());
56  return nullptr;
57  }
58 }
59 
60 Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain)
61  : m_configFile(configFile)
62  , m_keyChain(keyChain)
63  , m_networkMonitor(makeNetworkMonitor())
64 {
65 }
66 
67 Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain)
68  : m_configSection(config)
69  , m_keyChain(keyChain)
70  , m_networkMonitor(makeNetworkMonitor())
71 {
72 }
73 
74 // It is necessary to explicitly define the destructor, because some member variables (e.g.,
75 // unique_ptr<Forwarder>) are forward-declared, but implicitly declared destructor requires
76 // complete types for all members when instantiated.
77 Nfd::~Nfd() = default;
78 
79 void
81 {
82  initializeLogging();
83 
84  m_forwarder.reset(new Forwarder());
85 
86  initializeManagement();
87 
88  FaceTable& faceTable = m_forwarder->getFaceTable();
90  faceTable.addReserved(face::makeNullFace(FaceUri("contentstore://")), face::FACEID_CONTENT_STORE);
91 
93 
94  if (m_networkMonitor) {
95  m_networkMonitor->onNetworkStateChanged.connect([this] {
96  // delay stages, so if multiple events are triggered in short sequence,
97  // only one auto-detection procedure is triggered
98  m_reloadConfigEvent = scheduler::schedule(time::seconds(5),
99  [this] {
100  NFD_LOG_INFO("Network change detected, reloading face section of the config file...");
101  this->reloadConfigFileFaceSection();
102  });
103  });
104  }
105 }
106 
107 void
108 Nfd::initializeLogging()
109 {
111  LoggerFactory::getInstance().setConfigFile(config);
112 
113  if (!m_configFile.empty()) {
114  config.parse(m_configFile, true);
115  config.parse(m_configFile, false);
116  }
117  else {
118  config.parse(m_configSection, true, INTERNAL_CONFIG);
119  config.parse(m_configSection, false, INTERNAL_CONFIG);
120  }
121 }
122 
123 static inline void
124 ignoreRibAndLogSections(const std::string& filename, const std::string& sectionName,
125  const ConfigSection& section, bool isDryRun)
126 {
127  // Ignore "log" and "rib" sections, but raise an error if we're missing a
128  // handler for an NFD section.
129  if (sectionName == "rib" || sectionName == "log") {
130  // do nothing
131  }
132  else {
133  // missing NFD section
134  ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
135  }
136 }
137 
138 void
139 Nfd::initializeManagement()
140 {
141  std::tie(m_internalFace, m_internalClientFace) = face::makeInternalFace(m_keyChain);
142  m_forwarder->getFaceTable().addReserved(m_internalFace, face::FACEID_INTERNAL_FACE);
143 
144  m_dispatcher.reset(new ndn::mgmt::Dispatcher(*m_internalClientFace, m_keyChain));
145  m_authenticator = CommandAuthenticator::create();
146 
147  m_forwarderStatusManager.reset(new ForwarderStatusManager(*m_forwarder, *m_dispatcher));
148  m_faceManager.reset(new FaceManager(m_forwarder->getFaceTable(),
149  *m_dispatcher, *m_authenticator));
150  m_fibManager.reset(new FibManager(m_forwarder->getFib(), m_forwarder->getFaceTable(),
151  *m_dispatcher, *m_authenticator));
152  m_strategyChoiceManager.reset(new StrategyChoiceManager(m_forwarder->getStrategyChoice(),
153  *m_dispatcher, *m_authenticator));
154 
156  general::setConfigFile(config);
157 
158  TablesConfigSection tablesConfig(*m_forwarder);
159  tablesConfig.setConfigFile(config);
160 
161  m_authenticator->setConfigFile(config);
162  m_faceManager->setConfigFile(config);
163 
164  // parse config file
165  if (!m_configFile.empty()) {
166  config.parse(m_configFile, true);
167  config.parse(m_configFile, false);
168  }
169  else {
170  config.parse(m_configSection, true, INTERNAL_CONFIG);
171  config.parse(m_configSection, false, INTERNAL_CONFIG);
172  }
173 
174  tablesConfig.ensureConfigured();
175 
176  // add FIB entry for NFD Management Protocol
177  Name topPrefix("/localhost/nfd");
178  m_forwarder->getFib().insert(topPrefix).first->addNextHop(*m_internalFace, 0);
179  m_dispatcher->addTopPrefix(topPrefix, false);
180 }
181 
182 void
184 {
185  // Logging
186  initializeLogging();
188 
189  // Other stuff
191 
192  general::setConfigFile(config);
193 
194  TablesConfigSection tablesConfig(*m_forwarder);
195  tablesConfig.setConfigFile(config);
196 
197  m_authenticator->setConfigFile(config);
198  m_faceManager->setConfigFile(config);
199 
200  if (!m_configFile.empty()) {
201  config.parse(m_configFile, false);
202  }
203  else {
204  config.parse(m_configSection, false, INTERNAL_CONFIG);
205  }
206 }
207 
208 void
209 Nfd::reloadConfigFileFaceSection()
210 {
211  // reload only face_system section of the config file to re-initialize multicast faces
213  m_faceManager->setConfigFile(config);
214 
215  if (!m_configFile.empty()) {
216  config.parse(m_configFile, false);
217  }
218  else {
219  config.parse(m_configSection, false, INTERNAL_CONFIG);
220  }
221 }
222 
223 } // 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:60
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:46
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
void setConfigFile(ConfigFile &configFile)
configuration file parsing utility
Definition: config-file.hpp:50
The packet signing interface.
Definition: key-chain.hpp:47
const FaceId FACEID_INTERNAL_FACE
identifies the InternalFace used in management
Definition: face.hpp:44
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
void ensureConfigured()
apply default configuration, if tables section was omitted in configuration file
static void ignoreRibAndLogSections(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: nfd.cpp:124
implement the Forwarder Status of NFD Management Protocol.
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
FibManager
Definition: fib-manager.cpp:33
void reloadConfigFile()
Reload configuration file and apply update (if any)
Definition: nfd.cpp:183
container of all faces
Definition: face-table.hpp:37
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:37
handles &#39;tables&#39; config section
#define NFD_LOG_WARN(expression)
Definition: logger.hpp:58
void initialize()
Perform initialization of NFD instance After initialization, NFD instance can be started by invoking ...
Definition: nfd.cpp:80
~Nfd()
Destructor.
static shared_ptr< CommandAuthenticator > create()
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:71
static ndn::util::NetworkMonitor * makeNetworkMonitor()
Definition: nfd.cpp:49
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
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
void setConfigFile(ConfigFile &configFile)
const FaceId FACEID_CONTENT_STORE
identifies a packet comes from the ContentStore
Definition: face.hpp:46