NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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-2017, 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/global-io.hpp"
29 #include "core/logger-factory.hpp"
31 #include "core/config-file.hpp"
32 #include "fw/forwarder.hpp"
33 #include "face/face-system.hpp"
34 #include "face/null-face.hpp"
35 #include "face/internal-face.hpp"
36 #include "mgmt/fib-manager.hpp"
37 #include "mgmt/face-manager.hpp"
42 
43 namespace nfd {
44 
45 NFD_LOG_INIT("Nfd");
46 
47 static const std::string INTERNAL_CONFIG = "internal://nfd.conf";
48 
49 Nfd::Nfd(ndn::KeyChain& keyChain)
50  : m_keyChain(keyChain)
51  , m_netmon(make_shared<ndn::net::NetworkMonitor>(getGlobalIoService()))
52 {
53 }
54 
55 Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain)
56  : Nfd(keyChain)
57 {
58  m_configFile = configFile;
59 }
60 
62  : Nfd(keyChain)
63 {
64  m_configSection = config;
65 }
66 
67 // It is necessary to explicitly define the destructor, because some member variables (e.g.,
68 // unique_ptr<Forwarder>) are forward-declared, but implicitly declared destructor requires
69 // complete types for all members when instantiated.
70 Nfd::~Nfd() = default;
71 
72 void
74 {
75  initializeLogging();
76 
77  m_forwarder.reset(new Forwarder());
78 
79  FaceTable& faceTable = m_forwarder->getFaceTable();
81  faceTable.addReserved(face::makeNullFace(FaceUri("contentstore://")), face::FACEID_CONTENT_STORE);
82  m_faceSystem = make_unique<face::FaceSystem>(faceTable, m_netmon);
83 
84  initializeManagement();
85 
87 
88  m_netmon->onNetworkStateChanged.connect([this] {
89  // delay stages, so if multiple events are triggered in short sequence,
90  // only one auto-detection procedure is triggered
91  m_reloadConfigEvent = scheduler::schedule(time::seconds(5),
92  [this] {
93  NFD_LOG_INFO("Network change detected, reloading face section of the config file...");
94  this->reloadConfigFileFaceSection();
95  });
96  });
97 }
98 
99 void
100 Nfd::initializeLogging()
101 {
103  LoggerFactory::getInstance().setConfigFile(config);
104 
105  if (!m_configFile.empty()) {
106  config.parse(m_configFile, true);
107  config.parse(m_configFile, false);
108  }
109  else {
110  config.parse(m_configSection, true, INTERNAL_CONFIG);
111  config.parse(m_configSection, false, INTERNAL_CONFIG);
112  }
113 }
114 
115 static inline void
116 ignoreRibAndLogSections(const std::string& filename, const std::string& sectionName,
117  const ConfigSection& section, bool isDryRun)
118 {
119  // Ignore "log" and "rib" sections, but raise an error if we're missing a
120  // handler for an NFD section.
121  if (sectionName == "rib" || sectionName == "log") {
122  // do nothing
123  }
124  else {
125  // missing NFD section
126  ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
127  }
128 }
129 
130 void
131 Nfd::initializeManagement()
132 {
133  std::tie(m_internalFace, m_internalClientFace) = face::makeInternalFace(m_keyChain);
134  m_forwarder->getFaceTable().addReserved(m_internalFace, face::FACEID_INTERNAL_FACE);
135 
136  m_dispatcher.reset(new ndn::mgmt::Dispatcher(*m_internalClientFace, m_keyChain));
137  m_authenticator = CommandAuthenticator::create();
138 
139  m_forwarderStatusManager.reset(new ForwarderStatusManager(*m_forwarder, *m_dispatcher));
140  m_faceManager.reset(new FaceManager(*m_faceSystem, *m_dispatcher, *m_authenticator));
141  m_fibManager.reset(new FibManager(m_forwarder->getFib(), m_forwarder->getFaceTable(),
142  *m_dispatcher, *m_authenticator));
143  m_strategyChoiceManager.reset(new StrategyChoiceManager(m_forwarder->getStrategyChoice(),
144  *m_dispatcher, *m_authenticator));
145 
147  general::setConfigFile(config);
148 
149  TablesConfigSection tablesConfig(*m_forwarder);
150  tablesConfig.setConfigFile(config);
151 
152  m_authenticator->setConfigFile(config);
153  m_faceManager->setConfigFile(config);
154 
155  // parse config file
156  if (!m_configFile.empty()) {
157  config.parse(m_configFile, true);
158  config.parse(m_configFile, false);
159  }
160  else {
161  config.parse(m_configSection, true, INTERNAL_CONFIG);
162  config.parse(m_configSection, false, INTERNAL_CONFIG);
163  }
164 
165  tablesConfig.ensureConfigured();
166 
167  // add FIB entry for NFD Management Protocol
168  Name topPrefix("/localhost/nfd");
169  m_forwarder->getFib().insert(topPrefix).first->addNextHop(*m_internalFace, 0);
170  m_dispatcher->addTopPrefix(topPrefix, false);
171 }
172 
173 void
175 {
176  // Logging
177  initializeLogging();
179 
180  // Other stuff
182 
183  general::setConfigFile(config);
184 
185  TablesConfigSection tablesConfig(*m_forwarder);
186  tablesConfig.setConfigFile(config);
187 
188  m_authenticator->setConfigFile(config);
189  m_faceManager->setConfigFile(config);
190 
191  if (!m_configFile.empty()) {
192  config.parse(m_configFile, false);
193  }
194  else {
195  config.parse(m_configSection, false, INTERNAL_CONFIG);
196  }
197 }
198 
199 void
200 Nfd::reloadConfigFileFaceSection()
201 {
202  // reload only face_system section of the config file to re-initialize multicast faces
204  m_faceManager->setConfigFile(config);
205 
206  if (!m_configFile.empty()) {
207  config.parse(m_configFile, false);
208  }
209  else {
210  config.parse(m_configSection, false, INTERNAL_CONFIG);
211  }
212 }
213 
214 } // 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 ...
Nfd(const std::string &configFile, ndn::KeyChain &keyChain)
Create NFD instance using absolute or relative path to configFile.
Definition: nfd.cpp:55
represents a dispatcher on server side of NFD Management protocol
Definition: dispatcher.hpp:129
static const std::string INTERNAL_CONFIG
Definition: nfd.cpp:47
void setConfigFile(ConfigFile &configFile)
configuration file parsing utility
Definition: config-file.hpp:58
const FaceId FACEID_INTERNAL_FACE
identifies the InternalFace used in management
Definition: face.hpp:44
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
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:116
implement the Forwarder Status of NFD Management Protocol.
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
FibManager
Definition: fib-manager.cpp:36
void reloadConfigFile()
Reload configuration file and apply update (if any)
Definition: nfd.cpp:174
Class representing NFD instance This class can be used to initialize all components of NFD...
Definition: nfd.hpp:60
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
boost::property_tree::ptree ConfigSection
a config file section
Represents an absolute name.
Definition: name.hpp:42
Forwarder
Definition: forwarder.cpp:38
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:43
handles &#39;tables&#39; config section
void initialize()
Perform initialization of NFD instance After initialization, NFD instance can be started by invoking ...
Definition: nfd.cpp:73
~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:47
void addReserved(shared_ptr< Face > face, FaceId faceId)
add a special face with a reserved FaceId
Definition: face-table.cpp:70
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:34
void setConfigFile(ConfigFile &configFile)
const FaceId FACEID_CONTENT_STORE
identifies a packet comes from the ContentStore
Definition: face.hpp:46
ndn security v2 KeyChain
Definition: key-chain.cpp:76