NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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; -*- */
2 /*
3  * Copyright (c) 2014-2019, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include "nfd.hpp"
27 
28 #include "core/config-file.hpp"
29 #include "core/global-io.hpp"
32 #include "face/face-system.hpp"
33 #include "face/internal-face.hpp"
34 #include "face/null-face.hpp"
35 #include "fw/forwarder.hpp"
36 #include "mgmt/cs-manager.hpp"
37 #include "mgmt/face-manager.hpp"
38 #include "mgmt/fib-manager.hpp"
43 
44 namespace nfd {
45 
47 
48 static const std::string INTERNAL_CONFIG = "internal://nfd.conf";
49 
50 Nfd::Nfd(ndn::KeyChain& keyChain)
51  : m_keyChain(keyChain)
52  , m_netmon(make_shared<ndn::net::NetworkMonitor>(getGlobalIoService()))
53 {
54 }
55 
56 Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain)
57  : Nfd(keyChain)
58 {
59  m_configFile = configFile;
60 }
61 
62 Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain)
63  : Nfd(keyChain)
64 {
65  m_configSection = config;
66 }
67 
68 // It is necessary to explicitly define the destructor, because some member variables (e.g.,
69 // unique_ptr<Forwarder>) are forward-declared, but implicitly declared destructor requires
70 // complete types for all members when instantiated.
71 Nfd::~Nfd() = default;
72 
73 void
75 {
76  configureLogging();
77 
78  m_forwarder = make_unique<Forwarder>();
79 
80  FaceTable& faceTable = m_forwarder->getFaceTable();
82  faceTable.addReserved(face::makeNullFace(FaceUri("contentstore://")), face::FACEID_CONTENT_STORE);
83  m_faceSystem = make_unique<face::FaceSystem>(faceTable, m_netmon);
84 
85  initializeManagement();
86 
88 
89  m_netmon->onNetworkStateChanged.connect([this] {
90  // delay stages, so if multiple events are triggered in short sequence,
91  // only one auto-detection procedure is triggered
92  m_reloadConfigEvent = scheduler::schedule(5_s,
93  [this] {
94  NFD_LOG_INFO("Network change detected, reloading face section of the config file...");
95  this->reloadConfigFileFaceSection();
96  });
97  });
98 }
99 
100 void
101 Nfd::configureLogging()
102 {
104  log::setConfigFile(config);
105 
106  if (!m_configFile.empty()) {
107  config.parse(m_configFile, true);
108  config.parse(m_configFile, false);
109  }
110  else {
111  config.parse(m_configSection, true, INTERNAL_CONFIG);
112  config.parse(m_configSection, false, INTERNAL_CONFIG);
113  }
114 }
115 
116 static inline void
117 ignoreRibAndLogSections(const std::string& filename, const std::string& sectionName,
118  const ConfigSection& section, bool isDryRun)
119 {
120  // Ignore "log" and "rib" sections, but raise an error if we're missing a
121  // handler for an NFD section.
122  if (sectionName == "rib" || sectionName == "log") {
123  // do nothing
124  }
125  else {
126  // missing NFD section
127  ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
128  }
129 }
130 
131 void
132 Nfd::initializeManagement()
133 {
134  std::tie(m_internalFace, m_internalClientFace) = face::makeInternalFace(m_keyChain);
135  m_forwarder->getFaceTable().addReserved(m_internalFace, face::FACEID_INTERNAL_FACE);
136 
137  m_dispatcher = make_unique<ndn::mgmt::Dispatcher>(*m_internalClientFace, m_keyChain);
138  m_authenticator = CommandAuthenticator::create();
139 
140  m_forwarderStatusManager = make_unique<ForwarderStatusManager>(*m_forwarder, *m_dispatcher);
141  m_faceManager = make_unique<FaceManager>(*m_faceSystem, *m_dispatcher, *m_authenticator);
142  m_fibManager = make_unique<FibManager>(m_forwarder->getFib(), m_forwarder->getFaceTable(),
143  *m_dispatcher, *m_authenticator);
144  m_csManager = make_unique<CsManager>(m_forwarder->getCs(), m_forwarder->getCounters(),
145  *m_dispatcher, *m_authenticator);
146  m_strategyChoiceManager = make_unique<StrategyChoiceManager>(m_forwarder->getStrategyChoice(),
147  *m_dispatcher, *m_authenticator);
148 
149  ConfigFile config(&ignoreRibAndLogSections);
150  general::setConfigFile(config);
151 
152  TablesConfigSection tablesConfig(*m_forwarder);
153  tablesConfig.setConfigFile(config);
154 
155  m_authenticator->setConfigFile(config);
156  m_faceSystem->setConfigFile(config);
157 
158  // parse config file
159  if (!m_configFile.empty()) {
160  config.parse(m_configFile, true);
161  config.parse(m_configFile, false);
162  }
163  else {
164  config.parse(m_configSection, true, INTERNAL_CONFIG);
165  config.parse(m_configSection, false, INTERNAL_CONFIG);
166  }
167 
168  tablesConfig.ensureConfigured();
169 
170  // add FIB entry for NFD Management Protocol
171  Name topPrefix("/localhost/nfd");
172  m_forwarder->getFib().insert(topPrefix).first->addOrUpdateNextHop(*m_internalFace, 0, 0);
173  m_dispatcher->addTopPrefix(topPrefix, false);
174 }
175 
176 void
178 {
179  configureLogging();
180 
182  general::setConfigFile(config);
183 
184  TablesConfigSection tablesConfig(*m_forwarder);
185  tablesConfig.setConfigFile(config);
186 
187  m_authenticator->setConfigFile(config);
188  m_faceSystem->setConfigFile(config);
189 
190  if (!m_configFile.empty()) {
191  config.parse(m_configFile, false);
192  }
193  else {
194  config.parse(m_configSection, false, INTERNAL_CONFIG);
195  }
196 }
197 
198 void
199 Nfd::reloadConfigFileFaceSection()
200 {
201  // reload only face_system section of the config file to re-initialize multicast faces
203  m_faceSystem->setConfigFile(config);
204 
205  if (!m_configFile.empty()) {
206  config.parse(m_configFile, false);
207  }
208  else {
209  config.parse(m_configSection, false, INTERNAL_CONFIG);
210  }
211 }
212 
213 } // namespace nfd
Copyright (c) 2011-2015 Regents of the University of California.
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
void setConfigFile(ConfigFile &config)
Nfd(const std::string &configFile, ndn::KeyChain &keyChain)
Create NFD instance using absolute or relative path to configFile.
Definition: nfd.cpp:56
The interface of signing key management.
Definition: key-chain.hpp:46
void setConfigFile(ConfigFile &config)
static const std::string INTERNAL_CONFIG
Definition: nfd.cpp:48
configuration file parsing utility
Definition: config-file.hpp:57
const FaceId FACEID_INTERNAL_FACE
identifies the InternalFace used in management
Definition: face.hpp:44
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
#define NFD_LOG_INFO
Definition: logger.hpp:39
static void ignoreRibAndLogSections(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: nfd.cpp:117
void reloadConfigFile()
Reload configuration file and apply update (if any)
Definition: nfd.cpp:177
Class representing NFD instance This class can be used to initialize all components of NFD.
Definition: nfd.hpp:63
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
Nfd
Definition: nfd.cpp:46
boost::property_tree::ptree ConfigSection
a config file section
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
handles 'tables' config section
void initialize()
Perform initialization of NFD instance After initialization, NFD instance can be started by invoking ...
Definition: nfd.cpp:74
~Nfd()
Destructor.
static shared_ptr< CommandAuthenticator > create()
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:50
EventId schedule(time::nanoseconds after, const EventCallback &event)
Schedule an event.
Definition: scheduler.cpp:48
void addReserved(shared_ptr< Face > face, FaceId faceId)
add a special face with a reserved FaceId
Definition: face-table.cpp:71
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:83
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
void setConfigFile(ConfigFile &configFile)
const FaceId FACEID_CONTENT_STORE
identifies a packet comes from the ContentStore
Definition: face.hpp:46