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 
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 
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
Copyright (c) 2014-2017, Regents of the University of California, Arizona Board of Regents...
Definition: dns-srv.cpp:41
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()
Destructor.
configuration file parsing utility
Definition: config-file.hpp:57
void setConfigFile(ConfigFile &config)
represents a FIB entry
Definition: fib-entry.hpp:53
const FaceId FACEID_INTERNAL_FACE
identifies the InternalFace used in management
Definition: face-common.hpp:49
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:45
void reloadConfigFile()
Reload configuration file and apply updates (if any).
Definition: nfd.cpp:179
const std::string INTERNAL_CONFIG("internal://nfd.conf")
boost::property_tree::ptree ConfigSection
a config file section
Definition: config-file.hpp:37
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
void ensureConfigured()
apply default configuration, if tables section was omitted in configuration file
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:84
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
void setConfigFile(ConfigFile &configFile)
static shared_ptr< CommandAuthenticator > create()
void initialize()
Perform initialization of NFD instance.
Definition: nfd.cpp:76
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:41
#define NFD_LOG_INFO
Definition: logger.hpp:39
handles &#39;tables&#39; config section
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
Class representing the NFD instance.
Definition: nfd.hpp:58
const FaceId FACEID_NULL
identifies the NullFace that drops every packet
Definition: face-common.hpp:53
const FaceId FACEID_CONTENT_STORE
identifies a packet comes from the ContentStore
Definition: face-common.hpp:51
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:51
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:36