blob: 8c58034621b9273a9f52d34bfbf4d81b7f710642 [file] [log] [blame]
Alexander Afanasyev31c781e2015-02-09 17:39:59 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shiea47bde2017-01-26 17:49:16 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Alexander Afanasyev31c781e2015-02-09 17:39:59 -08004 * 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"
Junxiao Shiea47bde2017-01-26 17:49:16 +000033#include "face/face-system.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080034#include "face/null-face.hpp"
Yanbiao Li4ee73d42015-08-19 16:30:16 -070035#include "face/internal-face.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080036#include "mgmt/fib-manager.hpp"
37#include "mgmt/face-manager.hpp"
38#include "mgmt/strategy-choice-manager.hpp"
Yanbiao Li7cec7ea2015-08-19 16:30:16 -070039#include "mgmt/forwarder-status-manager.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
Junxiao Shi83be1da2017-06-30 13:37:37 +000049static inline ndn::net::NetworkMonitor*
Alexander Afanasyev441ba212015-09-10 23:41:07 -070050makeNetworkMonitor()
51{
52 try {
Junxiao Shi83be1da2017-06-30 13:37:37 +000053 return new ndn::net::NetworkMonitor(getGlobalIoService());
Alexander Afanasyev441ba212015-09-10 23:41:07 -070054 }
Junxiao Shi83be1da2017-06-30 13:37:37 +000055 catch (const ndn::net::NetworkMonitor::Error& e) {
Alexander Afanasyev441ba212015-09-10 23:41:07 -070056 NFD_LOG_WARN(e.what());
57 return nullptr;
58 }
59}
60
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080061Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain)
62 : m_configFile(configFile)
63 , m_keyChain(keyChain)
Alexander Afanasyev441ba212015-09-10 23:41:07 -070064 , m_networkMonitor(makeNetworkMonitor())
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080065{
66}
67
68Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain)
69 : m_configSection(config)
70 , m_keyChain(keyChain)
Alexander Afanasyev441ba212015-09-10 23:41:07 -070071 , m_networkMonitor(makeNetworkMonitor())
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080072{
73}
74
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000075// It is necessary to explicitly define the destructor, because some member variables (e.g.,
76// unique_ptr<Forwarder>) are forward-declared, but implicitly declared destructor requires
77// complete types for all members when instantiated.
78Nfd::~Nfd() = default;
Alexander Afanasyev2bda6f82015-02-10 14:17:19 -080079
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080080void
81Nfd::initialize()
82{
83 initializeLogging();
84
Alexander Afanasyev2bda6f82015-02-10 14:17:19 -080085 m_forwarder.reset(new Forwarder());
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080086
Junxiao Shia044be72015-10-26 11:00:56 -070087 FaceTable& faceTable = m_forwarder->getFaceTable();
Junxiao Shicde37ad2015-12-24 01:02:05 -070088 faceTable.addReserved(face::makeNullFace(), face::FACEID_NULL);
89 faceTable.addReserved(face::makeNullFace(FaceUri("contentstore://")), face::FACEID_CONTENT_STORE);
Junxiao Shiea47bde2017-01-26 17:49:16 +000090 m_faceSystem.reset(new face::FaceSystem(faceTable));
91
92 initializeManagement();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080093
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080094 PrivilegeHelper::drop();
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070095
Alexander Afanasyev441ba212015-09-10 23:41:07 -070096 if (m_networkMonitor) {
97 m_networkMonitor->onNetworkStateChanged.connect([this] {
98 // delay stages, so if multiple events are triggered in short sequence,
99 // only one auto-detection procedure is triggered
100 m_reloadConfigEvent = scheduler::schedule(time::seconds(5),
101 [this] {
102 NFD_LOG_INFO("Network change detected, reloading face section of the config file...");
103 this->reloadConfigFileFaceSection();
104 });
105 });
106 }
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800107}
108
109void
110Nfd::initializeLogging()
111{
112 ConfigFile config(&ConfigFile::ignoreUnknownSection);
113 LoggerFactory::getInstance().setConfigFile(config);
114
115 if (!m_configFile.empty()) {
116 config.parse(m_configFile, true);
117 config.parse(m_configFile, false);
118 }
119 else {
120 config.parse(m_configSection, true, INTERNAL_CONFIG);
121 config.parse(m_configSection, false, INTERNAL_CONFIG);
122 }
123}
124
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800125static inline void
126ignoreRibAndLogSections(const std::string& filename, const std::string& sectionName,
127 const ConfigSection& section, bool isDryRun)
128{
129 // Ignore "log" and "rib" sections, but raise an error if we're missing a
130 // handler for an NFD section.
131 if (sectionName == "rib" || sectionName == "log") {
132 // do nothing
133 }
134 else {
135 // missing NFD section
136 ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
137 }
138}
139
140void
141Nfd::initializeManagement()
142{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700143 std::tie(m_internalFace, m_internalClientFace) = face::makeInternalFace(m_keyChain);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700144 m_forwarder->getFaceTable().addReserved(m_internalFace, face::FACEID_INTERNAL_FACE);
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000145
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700146 m_dispatcher.reset(new ndn::mgmt::Dispatcher(*m_internalClientFace, m_keyChain));
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000147 m_authenticator = CommandAuthenticator::create();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800148
Yanbiao Li7cec7ea2015-08-19 16:30:16 -0700149 m_forwarderStatusManager.reset(new ForwarderStatusManager(*m_forwarder, *m_dispatcher));
Junxiao Shiea47bde2017-01-26 17:49:16 +0000150 m_faceManager.reset(new FaceManager(*m_faceSystem, *m_dispatcher, *m_authenticator));
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000151 m_fibManager.reset(new FibManager(m_forwarder->getFib(), m_forwarder->getFaceTable(),
152 *m_dispatcher, *m_authenticator));
153 m_strategyChoiceManager.reset(new StrategyChoiceManager(m_forwarder->getStrategyChoice(),
154 *m_dispatcher, *m_authenticator));
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800155
156 ConfigFile config(&ignoreRibAndLogSections);
157 general::setConfigFile(config);
158
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000159 TablesConfigSection tablesConfig(*m_forwarder);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800160 tablesConfig.setConfigFile(config);
161
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000162 m_authenticator->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800163 m_faceManager->setConfigFile(config);
164
165 // parse config file
166 if (!m_configFile.empty()) {
167 config.parse(m_configFile, true);
168 config.parse(m_configFile, false);
169 }
170 else {
171 config.parse(m_configSection, true, INTERNAL_CONFIG);
172 config.parse(m_configSection, false, INTERNAL_CONFIG);
173 }
174
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000175 tablesConfig.ensureConfigured();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800176
177 // add FIB entry for NFD Management Protocol
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700178 Name topPrefix("/localhost/nfd");
Junxiao Shia6de4292016-07-12 02:08:10 +0000179 m_forwarder->getFib().insert(topPrefix).first->addNextHop(*m_internalFace, 0);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700180 m_dispatcher->addTopPrefix(topPrefix, false);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800181}
182
183void
184Nfd::reloadConfigFile()
185{
186 // Logging
187 initializeLogging();
188 /// \todo Reopen log file
189
190 // Other stuff
191 ConfigFile config(&ignoreRibAndLogSections);
192
193 general::setConfigFile(config);
194
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000195 TablesConfigSection tablesConfig(*m_forwarder);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800196 tablesConfig.setConfigFile(config);
197
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000198 m_authenticator->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800199 m_faceManager->setConfigFile(config);
200
201 if (!m_configFile.empty()) {
202 config.parse(m_configFile, false);
203 }
204 else {
205 config.parse(m_configSection, false, INTERNAL_CONFIG);
206 }
207}
208
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -0700209void
210Nfd::reloadConfigFileFaceSection()
211{
212 // reload only face_system section of the config file to re-initialize multicast faces
213 ConfigFile config(&ConfigFile::ignoreUnknownSection);
214 m_faceManager->setConfigFile(config);
215
216 if (!m_configFile.empty()) {
217 config.parse(m_configFile, false);
218 }
219 else {
220 config.parse(m_configSection, false, INTERNAL_CONFIG);
221 }
222}
223
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800224} // namespace nfd