blob: 741e495ff5c3960fdd47fac3f1909f2c3e41fad1 [file] [log] [blame]
Alexander Afanasyev31c781e2015-02-09 17:39:59 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, 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
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070028#include "core/global-io.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080029#include "core/logger-factory.hpp"
30#include "core/privilege-helper.hpp"
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070031#include "core/config-file.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080032#include "fw/forwarder.hpp"
33#include "face/null-face.hpp"
Yanbiao Li4ee73d42015-08-19 16:30:16 -070034#include "face/internal-face.hpp"
35#include "face/internal-client-face.hpp"
Yanbiao Li36f35002015-08-19 16:30:16 -070036// #include "mgmt/fib-manager.hpp"
37// #include "mgmt/face-manager.hpp"
38// #include "mgmt/strategy-choice-manager.hpp"
39// #include "mgmt/status-server.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080040#include "mgmt/general-config-section.hpp"
41#include "mgmt/tables-config-section.hpp"
Yanbiao Li698f4fe2015-08-19 16:30:16 -070042#include "mgmt/command-validator.hpp"
43
44#include <ndn-cxx/mgmt/dispatcher.hpp>
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080045
46namespace nfd {
47
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070048NFD_LOG_INIT("Nfd");
49
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080050static const std::string INTERNAL_CONFIG = "internal://nfd.conf";
51
52Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain)
53 : m_configFile(configFile)
Yanbiao Li4ee73d42015-08-19 16:30:16 -070054 , m_keyChain(keyChain)
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070055 , m_networkMonitor(getGlobalIoService())
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080056{
57}
58
59Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain)
60 : m_configSection(config)
Yanbiao Li4ee73d42015-08-19 16:30:16 -070061 , m_keyChain(keyChain)
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070062 , m_networkMonitor(getGlobalIoService())
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080063{
64}
65
Alexander Afanasyev2bda6f82015-02-10 14:17:19 -080066Nfd::~Nfd()
67{
68 // It is necessary to explicitly define the destructor, because some member variables (e.g.,
69 // unique_ptr<Forwarder>) are forward-declared, but implicitly declared destructor requires
70 // complete types for all members when instantiated.
71}
72
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080073void
74Nfd::initialize()
75{
76 initializeLogging();
77
Alexander Afanasyev2bda6f82015-02-10 14:17:19 -080078 m_forwarder.reset(new Forwarder());
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080079
80 initializeManagement();
81
82 m_forwarder->getFaceTable().addReserved(make_shared<NullFace>(), FACEID_NULL);
83 m_forwarder->getFaceTable().addReserved(make_shared<NullFace>(FaceUri("contentstore://")),
84 FACEID_CONTENT_STORE);
85
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080086 PrivilegeHelper::drop();
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070087
88 m_networkMonitor.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 });
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080097}
98
99void
100Nfd::initializeLogging()
101{
102 ConfigFile config(&ConfigFile::ignoreUnknownSection);
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
116static inline void
117ignoreRibAndLogSections(const std::string& filename, const std::string& sectionName,
118 const ConfigSection& section, bool isDryRun)
119{
120 // Ignore "log" and "rib" sections, but raise an error if we're missing a
121 // handler for an NFD section.
122 if (sectionName == "rib" || sectionName == "log") {
123 // do nothing
124 }
125 else {
126 // missing NFD section
127 ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
128 }
129}
130
131void
132Nfd::initializeManagement()
133{
134 m_internalFace = make_shared<InternalFace>();
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700135 m_forwarder->getFaceTable().addReserved(m_internalFace, FACEID_INTERNAL_FACE);
136 m_internalClientFace = makeInternalClientFace(m_internalFace, m_keyChain);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700137 m_dispatcher.reset(new ndn::mgmt::Dispatcher(*m_internalClientFace, m_keyChain));
138
139 m_validator.reset(new CommandValidator());
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800140
Yanbiao Li36f35002015-08-19 16:30:16 -0700141 // m_fibManager.reset(new FibManager(m_forwarder->getFib(),
142 // bind(&Forwarder::getFace, m_forwarder.get(), _1),
143 // m_internalFace, m_keyChain));
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800144
Yanbiao Li36f35002015-08-19 16:30:16 -0700145 // m_faceManager.reset(new FaceManager(m_forwarder->getFaceTable(), m_internalFace, m_keyChain));
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800146
Yanbiao Li36f35002015-08-19 16:30:16 -0700147 // m_strategyChoiceManager.reset(new StrategyChoiceManager(m_forwarder->getStrategyChoice(),
148 // m_internalFace, m_keyChain));
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800149
Yanbiao Li36f35002015-08-19 16:30:16 -0700150 // m_statusServer.reset(new StatusServer(m_internalFace, *m_forwarder, m_keyChain));
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800151
152 ConfigFile config(&ignoreRibAndLogSections);
153 general::setConfigFile(config);
154
155 TablesConfigSection tablesConfig(m_forwarder->getCs(),
156 m_forwarder->getPit(),
157 m_forwarder->getFib(),
158 m_forwarder->getStrategyChoice(),
159 m_forwarder->getMeasurements());
160 tablesConfig.setConfigFile(config);
161
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700162 m_validator->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800163
Yanbiao Li36f35002015-08-19 16:30:16 -0700164 // m_faceManager->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800165
166 // parse config file
167 if (!m_configFile.empty()) {
168 config.parse(m_configFile, true);
169 config.parse(m_configFile, false);
170 }
171 else {
172 config.parse(m_configSection, true, INTERNAL_CONFIG);
173 config.parse(m_configSection, false, INTERNAL_CONFIG);
174 }
175
176 tablesConfig.ensureTablesAreConfigured();
177
178 // add FIB entry for NFD Management Protocol
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700179 Name topPrefix("/localhost/nfd");
180 auto entry = m_forwarder->getFib().insert(topPrefix).first;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800181 entry->addNextHop(m_internalFace, 0);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700182 m_dispatcher->addTopPrefix(topPrefix, false);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800183}
184
185void
186Nfd::reloadConfigFile()
187{
188 // Logging
189 initializeLogging();
190 /// \todo Reopen log file
191
192 // Other stuff
193 ConfigFile config(&ignoreRibAndLogSections);
194
195 general::setConfigFile(config);
196
197 TablesConfigSection tablesConfig(m_forwarder->getCs(),
198 m_forwarder->getPit(),
199 m_forwarder->getFib(),
200 m_forwarder->getStrategyChoice(),
201 m_forwarder->getMeasurements());
202
203 tablesConfig.setConfigFile(config);
204
Yanbiao Li36f35002015-08-19 16:30:16 -0700205 // m_faceManager->setConfigFile(config);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700206 m_validator->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800207
208 if (!m_configFile.empty()) {
209 config.parse(m_configFile, false);
210 }
211 else {
212 config.parse(m_configSection, false, INTERNAL_CONFIG);
213 }
214}
215
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -0700216void
217Nfd::reloadConfigFileFaceSection()
218{
219 // reload only face_system section of the config file to re-initialize multicast faces
220 ConfigFile config(&ConfigFile::ignoreUnknownSection);
Yanbiao Li36f35002015-08-19 16:30:16 -0700221 // m_faceManager->setConfigFile(config);
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -0700222
223 if (!m_configFile.empty()) {
224 config.parse(m_configFile, false);
225 }
226 else {
227 config.parse(m_configSection, false, INTERNAL_CONFIG);
228 }
229}
230
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800231} // namespace nfd