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 #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 
64 Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain)
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 
150  ConfigFile config(&ignoreRibAndLogSections);
151  general::setConfigFile(config);
152 
153  TablesConfigSection tablesConfig(*m_forwarder);
154  tablesConfig.setConfigFile(config);
155 
156  m_authenticator->setConfigFile(config);
157  m_faceSystem->setConfigFile(config);
158 
159  // parse config file
160  if (!m_configFile.empty()) {
161  config.parse(m_configFile, true);
162  config.parse(m_configFile, false);
163  }
164  else {
165  config.parse(m_configSection, true, INTERNAL_CONFIG);
166  config.parse(m_configSection, false, INTERNAL_CONFIG);
167  }
168 
169  tablesConfig.ensureConfigured();
170 
171  // add FIB entry for NFD Management Protocol
172  Name topPrefix("/localhost/nfd");
173  fib::Entry* entry = m_forwarder->getFib().insert(topPrefix).first;
174  m_forwarder->getFib().addOrUpdateNextHop(*entry, *m_internalFace, 0);
175  m_dispatcher->addTopPrefix(topPrefix, false);
176 }
177 
178 void
180 {
181  configureLogging();
182 
184  general::setConfigFile(config);
185 
186  TablesConfigSection tablesConfig(*m_forwarder);
187  tablesConfig.setConfigFile(config);
188 
189  m_authenticator->setConfigFile(config);
190  m_faceSystem->setConfigFile(config);
191 
192  if (!m_configFile.empty()) {
193  config.parse(m_configFile, false);
194  }
195  else {
196  config.parse(m_configSection, false, INTERNAL_CONFIG);
197  }
198 }
199 
200 void
201 Nfd::reloadConfigFileFaceSection()
202 {
203  // reload only face_system section of the config file to re-initialize multicast faces
205  m_faceSystem->setConfigFile(config);
206 
207  if (!m_configFile.empty()) {
208  config.parse(m_configFile, false);
209  }
210  else {
211  config.parse(m_configSection, false, INTERNAL_CONFIG);
212  }
213 }
214 
215 } // namespace nfd
nfd::log::setConfigFile
void setConfigFile(ConfigFile &config)
Definition: log-config-section.cpp:40
general-config-section.hpp
NFD_LOG_INFO
#define NFD_LOG_INFO
Definition: logger.hpp:39
global.hpp
strategy-choice-manager.hpp
nfd::Nfd::Nfd
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
nfd::face::FACEID_NULL
const FaceId FACEID_NULL
identifies the NullFace that drops every packet
Definition: face-common.hpp:53
ndn::FaceUri
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:45
nfd.hpp
nfd::ConfigFile::ignoreUnknownSection
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:51
ndn::Interest::setAutoCheckParametersDigest
static void setAutoCheckParametersDigest(bool b)
Definition: interest.hpp:342
nfd::CommandAuthenticator::create
static shared_ptr< CommandAuthenticator > create()
Definition: command-authenticator.cpp:100
nfd::TablesConfigSection
handles 'tables' config section
Definition: tables-config-section.hpp:71
nfd::Nfd
Nfd
Definition: nfd.cpp:46
nfd::general::setConfigFile
void setConfigFile(ConfigFile &config)
Definition: general-config-section.cpp:73
nfd::Nfd::reloadConfigFile
void reloadConfigFile()
Reload configuration file and apply updates (if any).
Definition: nfd.cpp:179
face-table.hpp
forwarder.hpp
nfd::getGlobalIoService
detail::SimulatorIo & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:49
nfd::face::FACEID_CONTENT_STORE
const FaceId FACEID_CONTENT_STORE
identifies a packet comes from the ContentStore
Definition: face-common.hpp:51
tables-config-section.hpp
ns3::ndn::Name
Name
Definition: ndn-common.cpp:25
face-system.hpp
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::ConfigFile
configuration file parsing utility
Definition: config-file.hpp:58
ndn::security::v2::KeyChain
The interface of signing key management.
Definition: key-chain.hpp:47
nfd::face::FACEID_INTERNAL_FACE
const FaceId FACEID_INTERNAL_FACE
identifies the InternalFace used in management
Definition: face-common.hpp:49
nfd::ConfigFile::parse
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:84
nfd::getScheduler
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:70
nfd::ignoreRibAndLogSections
static void ignoreRibAndLogSections(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: nfd.cpp:118
nfd::TablesConfigSection::setConfigFile
void setConfigFile(ConfigFile &configFile)
Definition: tables-config-section.cpp:40
nfd::face::makeInternalFace
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
Definition: internal-face.cpp:35
nfd::ConfigFile::throwErrorOnUnknownSection
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:41
nfd::PrivilegeHelper::drop
static void drop()
Definition: privilege-helper.cpp:122
log-config-section.hpp
nfd::ConfigSection
boost::property_tree::ptree ConfigSection
a config file section
Definition: ndn-l3-protocol.hpp:39
nfd::INTERNAL_CONFIG
const std::string INTERNAL_CONFIG("internal://nfd.conf")
nfd::Nfd::~Nfd
~Nfd()
Destructor.
fib-manager.hpp
nfd::face::makeNullFace
shared_ptr< Face > makeNullFace(const FaceUri &uri)
Definition: null-face.cpp:34
privilege-helper.hpp
nfd::Nfd::initialize
void initialize()
Perform initialization of NFD instance.
Definition: nfd.cpp:76
ndn::net
Definition: link-type-helper.cpp:30
face-manager.hpp
internal-face.hpp
nfd::Nfd
Class representing the NFD instance.
Definition: nfd.hpp:59
cs-manager.hpp
forwarder-status-manager.hpp
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
logger.hpp
null-face.hpp