blob: 633e4f07cde7c56bf66b30d408c0fb6c552a6143 [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/*
ashiqopu3ad49db2018-10-20 22:38:47 +00003 * Copyright (c) 2014-2019, 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"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/global.hpp"
28#include "common/logger.hpp"
29#include "common/privilege-helper.hpp"
Junxiao Shiea47bde2017-01-26 17:49:16 +000030#include "face/face-system.hpp"
Yanbiao Li4ee73d42015-08-19 16:30:16 -070031#include "face/internal-face.hpp"
Junxiao Shif2bfb442018-01-05 12:34:57 +000032#include "face/null-face.hpp"
33#include "fw/forwarder.hpp"
34#include "mgmt/cs-manager.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080035#include "mgmt/face-manager.hpp"
Junxiao Shif2bfb442018-01-05 12:34:57 +000036#include "mgmt/fib-manager.hpp"
Yanbiao Li7cec7ea2015-08-19 16:30:16 -070037#include "mgmt/forwarder-status-manager.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080038#include "mgmt/general-config-section.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040039#include "mgmt/log-config-section.hpp"
Junxiao Shif2bfb442018-01-05 12:34:57 +000040#include "mgmt/strategy-choice-manager.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080041#include "mgmt/tables-config-section.hpp"
42
43namespace nfd {
44
Davide Pesaventoa3148082018-04-12 18:21:54 -040045NFD_LOG_INIT(Nfd);
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070046
Davide Pesavento859a69e2019-07-13 17:10:46 -040047const std::string INTERNAL_CONFIG("internal://nfd.conf");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080048
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{
Davide Pesavento859a69e2019-07-13 17:10:46 -040053 // Disable automatic verification of parameters digest for decoded Interests.
54 Interest::setAutoCheckParametersDigest(false);
Alexander Afanasyev441ba212015-09-10 23:41:07 -070055}
56
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080057Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain)
Junxiao Shi2d491752017-07-14 21:32:05 +000058 : Nfd(keyChain)
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080059{
Junxiao Shi2d491752017-07-14 21:32:05 +000060 m_configFile = configFile;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080061}
62
63Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain)
Junxiao Shi2d491752017-07-14 21:32:05 +000064 : Nfd(keyChain)
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080065{
Junxiao Shi2d491752017-07-14 21:32:05 +000066 m_configSection = config;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080067}
68
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000069// It is necessary to explicitly define the destructor, because some member variables (e.g.,
70// unique_ptr<Forwarder>) are forward-declared, but implicitly declared destructor requires
71// complete types for all members when instantiated.
72Nfd::~Nfd() = default;
Alexander Afanasyev2bda6f82015-02-10 14:17:19 -080073
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080074void
75Nfd::initialize()
76{
Davide Pesaventoa3148082018-04-12 18:21:54 -040077 configureLogging();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080078
Davide Pesaventocfb1a312018-03-01 01:30:56 -050079 m_forwarder = make_unique<Forwarder>();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080080
Junxiao Shia044be72015-10-26 11:00:56 -070081 FaceTable& faceTable = m_forwarder->getFaceTable();
Junxiao Shicde37ad2015-12-24 01:02:05 -070082 faceTable.addReserved(face::makeNullFace(), face::FACEID_NULL);
83 faceTable.addReserved(face::makeNullFace(FaceUri("contentstore://")), face::FACEID_CONTENT_STORE);
Junxiao Shi2d491752017-07-14 21:32:05 +000084 m_faceSystem = make_unique<face::FaceSystem>(faceTable, m_netmon);
Junxiao Shiea47bde2017-01-26 17:49:16 +000085
86 initializeManagement();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080087
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080088 PrivilegeHelper::drop();
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070089
Junxiao Shi2d491752017-07-14 21:32:05 +000090 m_netmon->onNetworkStateChanged.connect([this] {
91 // delay stages, so if multiple events are triggered in short sequence,
92 // only one auto-detection procedure is triggered
Davide Pesavento3dade002019-03-19 11:29:56 -060093 m_reloadConfigEvent = getScheduler().schedule(5_s,
Junxiao Shi2d491752017-07-14 21:32:05 +000094 [this] {
95 NFD_LOG_INFO("Network change detected, reloading face section of the config file...");
96 this->reloadConfigFileFaceSection();
97 });
98 });
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080099}
100
101void
Davide Pesaventoa3148082018-04-12 18:21:54 -0400102Nfd::configureLogging()
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800103{
104 ConfigFile config(&ConfigFile::ignoreUnknownSection);
Davide Pesaventoa3148082018-04-12 18:21:54 -0400105 log::setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800106
107 if (!m_configFile.empty()) {
108 config.parse(m_configFile, true);
109 config.parse(m_configFile, false);
110 }
111 else {
112 config.parse(m_configSection, true, INTERNAL_CONFIG);
113 config.parse(m_configSection, false, INTERNAL_CONFIG);
114 }
115}
116
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800117static inline void
118ignoreRibAndLogSections(const std::string& filename, const std::string& sectionName,
119 const ConfigSection& section, bool isDryRun)
120{
121 // Ignore "log" and "rib" sections, but raise an error if we're missing a
122 // handler for an NFD section.
123 if (sectionName == "rib" || sectionName == "log") {
124 // do nothing
125 }
126 else {
127 // missing NFD section
128 ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
129 }
130}
131
132void
133Nfd::initializeManagement()
134{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700135 std::tie(m_internalFace, m_internalClientFace) = face::makeInternalFace(m_keyChain);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700136 m_forwarder->getFaceTable().addReserved(m_internalFace, face::FACEID_INTERNAL_FACE);
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000137
Davide Pesaventocfb1a312018-03-01 01:30:56 -0500138 m_dispatcher = make_unique<ndn::mgmt::Dispatcher>(*m_internalClientFace, m_keyChain);
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000139 m_authenticator = CommandAuthenticator::create();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800140
Davide Pesaventocfb1a312018-03-01 01:30:56 -0500141 m_forwarderStatusManager = make_unique<ForwarderStatusManager>(*m_forwarder, *m_dispatcher);
142 m_faceManager = make_unique<FaceManager>(*m_faceSystem, *m_dispatcher, *m_authenticator);
143 m_fibManager = make_unique<FibManager>(m_forwarder->getFib(), m_forwarder->getFaceTable(),
144 *m_dispatcher, *m_authenticator);
145 m_csManager = make_unique<CsManager>(m_forwarder->getCs(), m_forwarder->getCounters(),
146 *m_dispatcher, *m_authenticator);
147 m_strategyChoiceManager = make_unique<StrategyChoiceManager>(m_forwarder->getStrategyChoice(),
148 *m_dispatcher, *m_authenticator);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800149
150 ConfigFile config(&ignoreRibAndLogSections);
151 general::setConfigFile(config);
152
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000153 TablesConfigSection tablesConfig(*m_forwarder);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800154 tablesConfig.setConfigFile(config);
155
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000156 m_authenticator->setConfigFile(config);
Davide Pesaventocfb1a312018-03-01 01:30:56 -0500157 m_faceSystem->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800158
159 // parse config file
160 if (!m_configFile.empty()) {
161 config.parse(m_configFile, true);
162 config.parse(m_configFile, false);
163 }
164 else {
165 config.parse(m_configSection, true, INTERNAL_CONFIG);
166 config.parse(m_configSection, false, INTERNAL_CONFIG);
167 }
168
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000169 tablesConfig.ensureConfigured();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800170
171 // add FIB entry for NFD Management Protocol
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700172 Name topPrefix("/localhost/nfd");
ashiqopu3ad49db2018-10-20 22:38:47 +0000173 m_forwarder->getFib().insert(topPrefix).first->addOrUpdateNextHop(*m_internalFace, 0, 0);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700174 m_dispatcher->addTopPrefix(topPrefix, false);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800175}
176
177void
178Nfd::reloadConfigFile()
179{
Davide Pesaventoa3148082018-04-12 18:21:54 -0400180 configureLogging();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800181
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800182 ConfigFile config(&ignoreRibAndLogSections);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800183 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);
Davide Pesaventocfb1a312018-03-01 01:30:56 -0500189 m_faceSystem->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800190
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);
Davide Pesaventocfb1a312018-03-01 01:30:56 -0500204 m_faceSystem->setConfigFile(config);
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -0700205
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