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-2021, 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 #include "common/global.hpp"
28 #include "common/logger.hpp"
30 #include "face/face-system.hpp"
31 #include "face/internal-face.hpp"
32 #include "face/null-face.hpp"
33 #include "fw/face-table.hpp"
34 #include "fw/forwarder.hpp"
35 #include "mgmt/cs-manager.hpp"
36 #include "mgmt/face-manager.hpp"
37 #include "mgmt/fib-manager.hpp"
43 
44 namespace nfd {
45 
47 
48 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  // Disable automatic verification of parameters digest for decoded Interests.
56 }
57 
58 Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain)
59  : Nfd(keyChain)
60 {
61  m_configFile = configFile;
62 }
63 
65  : Nfd(keyChain)
66 {
67  m_configSection = config;
68 }
69 
70 // It is necessary to explicitly define the destructor, because some member variables (e.g.,
71 // unique_ptr<Forwarder>) are forward-declared, but implicitly declared destructor requires
72 // complete types for all members when instantiated.
73 Nfd::~Nfd() = default;
74 
75 void
77 {
78  configureLogging();
79 
80  m_faceTable = make_unique<FaceTable>();
81  m_faceTable->addReserved(face::makeNullFace(), face::FACEID_NULL);
82  m_faceTable->addReserved(face::makeNullFace(FaceUri("contentstore://")), face::FACEID_CONTENT_STORE);
83 
84  m_faceSystem = make_unique<face::FaceSystem>(*m_faceTable, m_netmon);
85  m_forwarder = make_unique<Forwarder>(*m_faceTable);
86 
87  initializeManagement();
88 
90 
91  m_netmon->onNetworkStateChanged.connect([this] {
92  // delay stages, so if multiple events are triggered in short sequence,
93  // only one auto-detection procedure is triggered
94  m_reloadConfigEvent = getScheduler().schedule(5_s, [this] {
95  NFD_LOG_INFO("Network change detected, reloading face section of the config file...");
96  reloadConfigFileFaceSection();
97  });
98  });
99 }
100 
101 void
102 Nfd::configureLogging()
103 {
105  log::setConfigFile(config);
106 
107  if (!m_configFile.empty()) {
108  config.parse(m_configFile, true);
109  config.parse(m_configFile, false);
110  }
111  else {
112  config.parse(m_configSection, true, INTERNAL_CONFIG);
113  config.parse(m_configSection, false, INTERNAL_CONFIG);
114  }
115 }
116 
117 static inline void
118 ignoreRibAndLogSections(const std::string& filename, const std::string& sectionName,
119  const ConfigSection& section, bool isDryRun)
120 {
121  // Ignore "log" and "rib" sections, but raise an error if we're missing a
122  // handler for an NFD section.
123  if (sectionName == "rib" || sectionName == "log") {
124  // do nothing
125  }
126  else {
127  // missing NFD section
128  ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
129  }
130 }
131 
132 void
133 Nfd::initializeManagement()
134 {
135  std::tie(m_internalFace, m_internalClientFace) = face::makeInternalFace(m_keyChain);
136  m_faceTable->addReserved(m_internalFace, face::FACEID_INTERNAL_FACE);
137 
138  m_dispatcher = make_unique<ndn::mgmt::Dispatcher>(*m_internalClientFace, m_keyChain);
139  m_authenticator = CommandAuthenticator::create();
140 
141  m_forwarderStatusManager = make_unique<ForwarderStatusManager>(*m_forwarder, *m_dispatcher);
142  m_faceManager = make_unique<FaceManager>(*m_faceSystem, *m_dispatcher, *m_authenticator);
143  m_fibManager = make_unique<FibManager>(m_forwarder->getFib(), *m_faceTable,
144  *m_dispatcher, *m_authenticator);
145  m_csManager = make_unique<CsManager>(m_forwarder->getCs(), m_forwarder->getCounters(),
146  *m_dispatcher, *m_authenticator);
147  m_strategyChoiceManager = make_unique<StrategyChoiceManager>(m_forwarder->getStrategyChoice(),
148  *m_dispatcher, *m_authenticator);
149 
151  general::setConfigFile(config);
152 
153  m_forwarder->setConfigFile(config);
154 
155  TablesConfigSection tablesConfig(*m_forwarder);
156  tablesConfig.setConfigFile(config);
157 
158  m_authenticator->setConfigFile(config);
159  m_faceSystem->setConfigFile(config);
160 
161  // parse config file
162  if (!m_configFile.empty()) {
163  config.parse(m_configFile, true);
164  config.parse(m_configFile, false);
165  }
166  else {
167  config.parse(m_configSection, true, INTERNAL_CONFIG);
168  config.parse(m_configSection, false, INTERNAL_CONFIG);
169  }
170 
171  tablesConfig.ensureConfigured();
172 
173  // add FIB entry for NFD Management Protocol
174  Name topPrefix("/localhost/nfd");
175  fib::Entry* entry = m_forwarder->getFib().insert(topPrefix).first;
176  m_forwarder->getFib().addOrUpdateNextHop(*entry, *m_internalFace, 0);
177  m_dispatcher->addTopPrefix(topPrefix, false);
178 }
179 
180 void
182 {
183  configureLogging();
184 
186  general::setConfigFile(config);
187 
188  m_forwarder->setConfigFile(config);
189 
190  TablesConfigSection tablesConfig(*m_forwarder);
191  tablesConfig.setConfigFile(config);
192 
193  m_authenticator->setConfigFile(config);
194  m_faceSystem->setConfigFile(config);
195 
196  if (!m_configFile.empty()) {
197  config.parse(m_configFile, false);
198  }
199  else {
200  config.parse(m_configSection, false, INTERNAL_CONFIG);
201  }
202 }
203 
204 void
205 Nfd::reloadConfigFileFaceSection()
206 {
207  // reload only face_system section of the config file to re-initialize multicast faces
209  m_faceSystem->setConfigFile(config);
210 
211  if (!m_configFile.empty()) {
212  config.parse(m_configFile, false);
213  }
214  else {
215  config.parse(m_configSection, false, INTERNAL_CONFIG);
216  }
217 }
218 
219 } // namespace nfd
Copyright (c) 2011-2015 Regents of the University of California.
shared_ptr< Face > makeNullFace(const FaceUri &uri)
Definition: null-face.cpp:34
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 an absolute or relative path to a configuration file.
Definition: nfd.cpp:58
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
void setConfigFile(ConfigFile &config)
ndn security KeyChain
Definition: key-chain.cpp:70
represents a FIB entry
Definition: fib-entry.hpp:53
configuration file parsing utility
Definition: config-file.hpp:57
const FaceId FACEID_INTERNAL_FACE
identifies the InternalFace used in management
Definition: face-common.hpp:49
detail::SimulatorIo & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:49
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:118
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:70
#define NFD_LOG_INFO
Definition: logger.hpp:39
const std::string INTERNAL_CONFIG("internal://nfd.conf")
void reloadConfigFile()
Reload configuration file and apply updates (if any).
Definition: nfd.cpp:181
Class representing the NFD instance.
Definition: nfd.hpp:58
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:41
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
boost::property_tree::ptree ConfigSection
a config file section
Represents an absolute name.
Definition: name.hpp:41
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
static void setAutoCheckParametersDigest(bool b)
Definition: interest.hpp:426
handles &#39;tables&#39; config section
void initialize()
Perform initialization of NFD instance.
Definition: nfd.cpp:76
~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:51
const FaceId FACEID_NULL
identifies the NullFace that drops every packet
Definition: face-common.hpp:53
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:84
void setConfigFile(ConfigFile &configFile)
const FaceId FACEID_CONTENT_STORE
identifies a packet comes from the ContentStore
Definition: face-common.hpp:51