blob: 66ab5142591d98f257ef1c4d4a09dfad1f67db58 [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"
42
43namespace nfd {
44
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070045NFD_LOG_INIT("Nfd");
46
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080047static const std::string INTERNAL_CONFIG = "internal://nfd.conf";
48
49Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain)
50 : m_configFile(configFile)
Yanbiao Li4ee73d42015-08-19 16:30:16 -070051 , m_keyChain(keyChain)
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070052 , m_networkMonitor(getGlobalIoService())
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080053{
54}
55
56Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain)
57 : m_configSection(config)
Yanbiao Li4ee73d42015-08-19 16:30:16 -070058 , m_keyChain(keyChain)
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070059 , m_networkMonitor(getGlobalIoService())
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080060{
61}
62
Alexander Afanasyev2bda6f82015-02-10 14:17:19 -080063Nfd::~Nfd()
64{
65 // It is necessary to explicitly define the destructor, because some member variables (e.g.,
66 // unique_ptr<Forwarder>) are forward-declared, but implicitly declared destructor requires
67 // complete types for all members when instantiated.
68}
69
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080070void
71Nfd::initialize()
72{
73 initializeLogging();
74
Alexander Afanasyev2bda6f82015-02-10 14:17:19 -080075 m_forwarder.reset(new Forwarder());
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080076
77 initializeManagement();
78
79 m_forwarder->getFaceTable().addReserved(make_shared<NullFace>(), FACEID_NULL);
80 m_forwarder->getFaceTable().addReserved(make_shared<NullFace>(FaceUri("contentstore://")),
81 FACEID_CONTENT_STORE);
82
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080083 PrivilegeHelper::drop();
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070084
85 m_networkMonitor.onNetworkStateChanged.connect([this] {
86 // delay stages, so if multiple events are triggered in short sequence,
87 // only one auto-detection procedure is triggered
88 m_reloadConfigEvent = scheduler::schedule(time::seconds(5),
89 [this] {
90 NFD_LOG_INFO("Network change detected, reloading face section of the config file...");
91 this->reloadConfigFileFaceSection();
92 });
93 });
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080094}
95
96void
97Nfd::initializeLogging()
98{
99 ConfigFile config(&ConfigFile::ignoreUnknownSection);
100 LoggerFactory::getInstance().setConfigFile(config);
101
102 if (!m_configFile.empty()) {
103 config.parse(m_configFile, true);
104 config.parse(m_configFile, false);
105 }
106 else {
107 config.parse(m_configSection, true, INTERNAL_CONFIG);
108 config.parse(m_configSection, false, INTERNAL_CONFIG);
109 }
110}
111
112
113static inline void
114ignoreRibAndLogSections(const std::string& filename, const std::string& sectionName,
115 const ConfigSection& section, bool isDryRun)
116{
117 // Ignore "log" and "rib" sections, but raise an error if we're missing a
118 // handler for an NFD section.
119 if (sectionName == "rib" || sectionName == "log") {
120 // do nothing
121 }
122 else {
123 // missing NFD section
124 ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
125 }
126}
127
128void
129Nfd::initializeManagement()
130{
131 m_internalFace = make_shared<InternalFace>();
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700132 m_forwarder->getFaceTable().addReserved(m_internalFace, FACEID_INTERNAL_FACE);
133 m_internalClientFace = makeInternalClientFace(m_internalFace, m_keyChain);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800134
Yanbiao Li36f35002015-08-19 16:30:16 -0700135 // m_fibManager.reset(new FibManager(m_forwarder->getFib(),
136 // bind(&Forwarder::getFace, m_forwarder.get(), _1),
137 // m_internalFace, m_keyChain));
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800138
Yanbiao Li36f35002015-08-19 16:30:16 -0700139 // m_faceManager.reset(new FaceManager(m_forwarder->getFaceTable(), m_internalFace, m_keyChain));
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800140
Yanbiao Li36f35002015-08-19 16:30:16 -0700141 // m_strategyChoiceManager.reset(new StrategyChoiceManager(m_forwarder->getStrategyChoice(),
142 // m_internalFace, m_keyChain));
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800143
Yanbiao Li36f35002015-08-19 16:30:16 -0700144 // m_statusServer.reset(new StatusServer(m_internalFace, *m_forwarder, m_keyChain));
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800145
146 ConfigFile config(&ignoreRibAndLogSections);
147 general::setConfigFile(config);
148
149 TablesConfigSection tablesConfig(m_forwarder->getCs(),
150 m_forwarder->getPit(),
151 m_forwarder->getFib(),
152 m_forwarder->getStrategyChoice(),
153 m_forwarder->getMeasurements());
154 tablesConfig.setConfigFile(config);
155
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700156 // m_internalFace->getValidator().setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800157
158 m_forwarder->getFaceTable().addReserved(m_internalFace, FACEID_INTERNAL_FACE);
159
Yanbiao Li36f35002015-08-19 16:30:16 -0700160 // m_faceManager->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800161
162 // parse config file
163 if (!m_configFile.empty()) {
164 config.parse(m_configFile, true);
165 config.parse(m_configFile, false);
166 }
167 else {
168 config.parse(m_configSection, true, INTERNAL_CONFIG);
169 config.parse(m_configSection, false, INTERNAL_CONFIG);
170 }
171
172 tablesConfig.ensureTablesAreConfigured();
173
174 // add FIB entry for NFD Management Protocol
175 shared_ptr<fib::Entry> entry = m_forwarder->getFib().insert("/localhost/nfd").first;
176 entry->addNextHop(m_internalFace, 0);
177}
178
179void
180Nfd::reloadConfigFile()
181{
182 // Logging
183 initializeLogging();
184 /// \todo Reopen log file
185
186 // Other stuff
187 ConfigFile config(&ignoreRibAndLogSections);
188
189 general::setConfigFile(config);
190
191 TablesConfigSection tablesConfig(m_forwarder->getCs(),
192 m_forwarder->getPit(),
193 m_forwarder->getFib(),
194 m_forwarder->getStrategyChoice(),
195 m_forwarder->getMeasurements());
196
197 tablesConfig.setConfigFile(config);
198
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700199 // m_internalFace->getValidator().setConfigFile(config);
Yanbiao Li36f35002015-08-19 16:30:16 -0700200 // m_faceManager->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800201
202 if (!m_configFile.empty()) {
203 config.parse(m_configFile, false);
204 }
205 else {
206 config.parse(m_configSection, false, INTERNAL_CONFIG);
207 }
208}
209
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -0700210void
211Nfd::reloadConfigFileFaceSection()
212{
213 // reload only face_system section of the config file to re-initialize multicast faces
214 ConfigFile config(&ConfigFile::ignoreUnknownSection);
Yanbiao Li36f35002015-08-19 16:30:16 -0700215 // m_faceManager->setConfigFile(config);
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -0700216
217 if (!m_configFile.empty()) {
218 config.parse(m_configFile, false);
219 }
220 else {
221 config.parse(m_configSection, false, INTERNAL_CONFIG);
222 }
223}
224
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800225} // namespace nfd