blob: e3bcb75cbeaedee6fbb3262c7f47553de4855ba7 [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 Shif2bfb442018-01-05 12:34:57 +00003 * Copyright (c) 2014-2018, 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
Junxiao Shif2bfb442018-01-05 12:34:57 +000028#include "core/config-file.hpp"
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070029#include "core/global-io.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080030#include "core/logger-factory.hpp"
31#include "core/privilege-helper.hpp"
Junxiao Shiea47bde2017-01-26 17:49:16 +000032#include "face/face-system.hpp"
Yanbiao Li4ee73d42015-08-19 16:30:16 -070033#include "face/internal-face.hpp"
Junxiao Shif2bfb442018-01-05 12:34:57 +000034#include "face/null-face.hpp"
35#include "fw/forwarder.hpp"
36#include "mgmt/cs-manager.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080037#include "mgmt/face-manager.hpp"
Junxiao Shif2bfb442018-01-05 12:34:57 +000038#include "mgmt/fib-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"
Junxiao Shif2bfb442018-01-05 12:34:57 +000041#include "mgmt/strategy-choice-manager.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080042#include "mgmt/tables-config-section.hpp"
43
44namespace nfd {
45
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070046NFD_LOG_INIT("Nfd");
47
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080048static const std::string INTERNAL_CONFIG = "internal://nfd.conf";
49
Junxiao Shi2d491752017-07-14 21:32:05 +000050Nfd::Nfd(ndn::KeyChain& keyChain)
51 : m_keyChain(keyChain)
52 , m_netmon(make_shared<ndn::net::NetworkMonitor>(getGlobalIoService()))
Alexander Afanasyev441ba212015-09-10 23:41:07 -070053{
Alexander Afanasyev441ba212015-09-10 23:41:07 -070054}
55
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080056Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain)
Junxiao Shi2d491752017-07-14 21:32:05 +000057 : Nfd(keyChain)
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080058{
Junxiao Shi2d491752017-07-14 21:32:05 +000059 m_configFile = configFile;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080060}
61
62Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain)
Junxiao Shi2d491752017-07-14 21:32:05 +000063 : Nfd(keyChain)
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080064{
Junxiao Shi2d491752017-07-14 21:32:05 +000065 m_configSection = config;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080066}
67
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000068// 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.
71Nfd::~Nfd() = default;
Alexander Afanasyev2bda6f82015-02-10 14:17:19 -080072
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
Junxiao Shia044be72015-10-26 11:00:56 -070080 FaceTable& faceTable = m_forwarder->getFaceTable();
Junxiao Shicde37ad2015-12-24 01:02:05 -070081 faceTable.addReserved(face::makeNullFace(), face::FACEID_NULL);
82 faceTable.addReserved(face::makeNullFace(FaceUri("contentstore://")), face::FACEID_CONTENT_STORE);
Junxiao Shi2d491752017-07-14 21:32:05 +000083 m_faceSystem = make_unique<face::FaceSystem>(faceTable, m_netmon);
Junxiao Shiea47bde2017-01-26 17:49:16 +000084
85 initializeManagement();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080086
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080087 PrivilegeHelper::drop();
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070088
Junxiao Shi2d491752017-07-14 21:32:05 +000089 m_netmon->onNetworkStateChanged.connect([this] {
90 // delay stages, so if multiple events are triggered in short sequence,
91 // only one auto-detection procedure is triggered
92 m_reloadConfigEvent = scheduler::schedule(time::seconds(5),
93 [this] {
94 NFD_LOG_INFO("Network change detected, reloading face section of the config file...");
95 this->reloadConfigFileFaceSection();
96 });
97 });
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080098}
99
100void
101Nfd::initializeLogging()
102{
103 ConfigFile config(&ConfigFile::ignoreUnknownSection);
104 LoggerFactory::getInstance().setConfigFile(config);
105
106 if (!m_configFile.empty()) {
107 config.parse(m_configFile, true);
108 config.parse(m_configFile, false);
109 }
110 else {
111 config.parse(m_configSection, true, INTERNAL_CONFIG);
112 config.parse(m_configSection, false, INTERNAL_CONFIG);
113 }
114}
115
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800116static 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{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700134 std::tie(m_internalFace, m_internalClientFace) = face::makeInternalFace(m_keyChain);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700135 m_forwarder->getFaceTable().addReserved(m_internalFace, face::FACEID_INTERNAL_FACE);
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000136
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700137 m_dispatcher.reset(new ndn::mgmt::Dispatcher(*m_internalClientFace, m_keyChain));
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000138 m_authenticator = CommandAuthenticator::create();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800139
Yanbiao Li7cec7ea2015-08-19 16:30:16 -0700140 m_forwarderStatusManager.reset(new ForwarderStatusManager(*m_forwarder, *m_dispatcher));
Junxiao Shiea47bde2017-01-26 17:49:16 +0000141 m_faceManager.reset(new FaceManager(*m_faceSystem, *m_dispatcher, *m_authenticator));
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000142 m_fibManager.reset(new FibManager(m_forwarder->getFib(), m_forwarder->getFaceTable(),
143 *m_dispatcher, *m_authenticator));
Junxiao Shif2bfb442018-01-05 12:34:57 +0000144 m_csManager.reset(new CsManager(m_forwarder->getCs(), m_forwarder->getCounters(),
145 *m_dispatcher, *m_authenticator));
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000146 m_strategyChoiceManager.reset(new StrategyChoiceManager(m_forwarder->getStrategyChoice(),
147 *m_dispatcher, *m_authenticator));
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800148
149 ConfigFile config(&ignoreRibAndLogSections);
150 general::setConfigFile(config);
151
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000152 TablesConfigSection tablesConfig(*m_forwarder);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800153 tablesConfig.setConfigFile(config);
154
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000155 m_authenticator->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800156 m_faceManager->setConfigFile(config);
157
158 // parse config file
159 if (!m_configFile.empty()) {
160 config.parse(m_configFile, true);
161 config.parse(m_configFile, false);
162 }
163 else {
164 config.parse(m_configSection, true, INTERNAL_CONFIG);
165 config.parse(m_configSection, false, INTERNAL_CONFIG);
166 }
167
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000168 tablesConfig.ensureConfigured();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800169
170 // add FIB entry for NFD Management Protocol
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700171 Name topPrefix("/localhost/nfd");
Junxiao Shia6de4292016-07-12 02:08:10 +0000172 m_forwarder->getFib().insert(topPrefix).first->addNextHop(*m_internalFace, 0);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700173 m_dispatcher->addTopPrefix(topPrefix, false);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800174}
175
176void
177Nfd::reloadConfigFile()
178{
179 // Logging
180 initializeLogging();
181 /// \todo Reopen log file
182
183 // Other stuff
184 ConfigFile config(&ignoreRibAndLogSections);
185
186 general::setConfigFile(config);
187
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000188 TablesConfigSection tablesConfig(*m_forwarder);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800189 tablesConfig.setConfigFile(config);
190
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000191 m_authenticator->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800192 m_faceManager->setConfigFile(config);
193
194 if (!m_configFile.empty()) {
195 config.parse(m_configFile, false);
196 }
197 else {
198 config.parse(m_configSection, false, INTERNAL_CONFIG);
199 }
200}
201
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -0700202void
203Nfd::reloadConfigFileFaceSection()
204{
205 // reload only face_system section of the config file to re-initialize multicast faces
206 ConfigFile config(&ConfigFile::ignoreUnknownSection);
207 m_faceManager->setConfigFile(config);
208
209 if (!m_configFile.empty()) {
210 config.parse(m_configFile, false);
211 }
212 else {
213 config.parse(m_configSection, false, INTERNAL_CONFIG);
214 }
215}
216
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800217} // namespace nfd