blob: 8a917a970d0cd1736f4208ddad9b549ab7a3dbeb [file] [log] [blame]
Alexander Afanasyev31c781e2015-02-09 17:39:59 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2d491752017-07-14 21:32:05 +00002/*
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 Shi2d491752017-07-14 21:32:05 +000049Nfd::Nfd(ndn::KeyChain& keyChain)
50 : m_keyChain(keyChain)
51 , m_netmon(make_shared<ndn::net::NetworkMonitor>(getGlobalIoService()))
Alexander Afanasyev441ba212015-09-10 23:41:07 -070052{
Alexander Afanasyev441ba212015-09-10 23:41:07 -070053}
54
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080055Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain)
Junxiao Shi2d491752017-07-14 21:32:05 +000056 : Nfd(keyChain)
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080057{
Junxiao Shi2d491752017-07-14 21:32:05 +000058 m_configFile = configFile;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080059}
60
61Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain)
Junxiao Shi2d491752017-07-14 21:32:05 +000062 : Nfd(keyChain)
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080063{
Junxiao Shi2d491752017-07-14 21:32:05 +000064 m_configSection = config;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080065}
66
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000067// 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.
70Nfd::~Nfd() = default;
Alexander Afanasyev2bda6f82015-02-10 14:17:19 -080071
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080072void
73Nfd::initialize()
74{
75 initializeLogging();
76
Alexander Afanasyev2bda6f82015-02-10 14:17:19 -080077 m_forwarder.reset(new Forwarder());
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080078
Junxiao Shia044be72015-10-26 11:00:56 -070079 FaceTable& faceTable = m_forwarder->getFaceTable();
Junxiao Shicde37ad2015-12-24 01:02:05 -070080 faceTable.addReserved(face::makeNullFace(), face::FACEID_NULL);
81 faceTable.addReserved(face::makeNullFace(FaceUri("contentstore://")), face::FACEID_CONTENT_STORE);
Junxiao Shi2d491752017-07-14 21:32:05 +000082 m_faceSystem = make_unique<face::FaceSystem>(faceTable, m_netmon);
Junxiao Shiea47bde2017-01-26 17:49:16 +000083
84 initializeManagement();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080085
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080086 PrivilegeHelper::drop();
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070087
Junxiao Shi2d491752017-07-14 21:32:05 +000088 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 });
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
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800115static inline void
116ignoreRibAndLogSections(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
130void
131Nfd::initializeManagement()
132{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700133 std::tie(m_internalFace, m_internalClientFace) = face::makeInternalFace(m_keyChain);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700134 m_forwarder->getFaceTable().addReserved(m_internalFace, face::FACEID_INTERNAL_FACE);
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000135
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700136 m_dispatcher.reset(new ndn::mgmt::Dispatcher(*m_internalClientFace, m_keyChain));
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000137 m_authenticator = CommandAuthenticator::create();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800138
Yanbiao Li7cec7ea2015-08-19 16:30:16 -0700139 m_forwarderStatusManager.reset(new ForwarderStatusManager(*m_forwarder, *m_dispatcher));
Junxiao Shiea47bde2017-01-26 17:49:16 +0000140 m_faceManager.reset(new FaceManager(*m_faceSystem, *m_dispatcher, *m_authenticator));
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000141 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));
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800145
146 ConfigFile config(&ignoreRibAndLogSections);
147 general::setConfigFile(config);
148
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000149 TablesConfigSection tablesConfig(*m_forwarder);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800150 tablesConfig.setConfigFile(config);
151
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000152 m_authenticator->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800153 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
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000165 tablesConfig.ensureConfigured();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800166
167 // add FIB entry for NFD Management Protocol
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700168 Name topPrefix("/localhost/nfd");
Junxiao Shia6de4292016-07-12 02:08:10 +0000169 m_forwarder->getFib().insert(topPrefix).first->addNextHop(*m_internalFace, 0);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700170 m_dispatcher->addTopPrefix(topPrefix, false);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800171}
172
173void
174Nfd::reloadConfigFile()
175{
176 // Logging
177 initializeLogging();
178 /// \todo Reopen log file
179
180 // Other stuff
181 ConfigFile config(&ignoreRibAndLogSections);
182
183 general::setConfigFile(config);
184
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000185 TablesConfigSection tablesConfig(*m_forwarder);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800186 tablesConfig.setConfigFile(config);
187
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000188 m_authenticator->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800189 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
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -0700199void
200Nfd::reloadConfigFileFaceSection()
201{
202 // reload only face_system section of the config file to re-initialize multicast faces
203 ConfigFile config(&ConfigFile::ignoreUnknownSection);
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
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800214} // namespace nfd