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-2022, 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 
46 NFD_LOG_INIT(Nfd);
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.
55  Interest::setAutoCheckParametersDigest(false);
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  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
static shared_ptr< CommandAuthenticator > create()
Configuration file parsing utility.
Definition: config-file.hpp:66
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:41
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:84
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:51
Class representing the NFD instance.
Definition: nfd.hpp:60
void initialize()
Perform initialization of NFD instance.
Definition: nfd.cpp:76
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()
Destructor.
void reloadConfigFile()
Reload configuration file and apply updates (if any).
Definition: nfd.cpp:181
Handles the tables configuration file section.
void setConfigFile(ConfigFile &configFile)
#define NFD_LOG_INFO
Definition: logger.hpp:39
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
Definition: dns-srv.cpp:44
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 to each other.
constexpr FaceId FACEID_NULL
Identifies the NullFace that drops every packet.
Definition: face-common.hpp:57
constexpr FaceId FACEID_CONTENT_STORE
Identifies a packet comes from the ContentStore.
Definition: face-common.hpp:55
constexpr FaceId FACEID_INTERNAL_FACE
Identifies the InternalFace used in management.
Definition: face-common.hpp:53
shared_ptr< Face > makeNullFace(const FaceUri &uri)
Returns a Face that has no underlying transport and drops every packet.
Definition: null-face.cpp:33
void setConfigFile(ConfigFile &config)
void setConfigFile(ConfigFile &config)
-status-http-server
Definition: common.hpp:71
ndn::Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:45
boost::property_tree::ptree ConfigSection
A configuration file section.
Definition: config-file.hpp:41
const std::string INTERNAL_CONFIG
Definition: nfd.cpp:48
static void ignoreRibAndLogSections(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: nfd.cpp:118
boost::asio::io_context & getGlobalIoService()
Returns the global io_context instance for the calling thread.
Definition: global.cpp:36