blob: f4d136a4959168039ff1d7fe8d971b812fefa020 [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"
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"
Davide Pesaventoa3148082018-04-12 18:21:54 -040030#include "core/log-config-section.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080031#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
Davide Pesaventoa3148082018-04-12 18:21:54 -040046NFD_LOG_INIT(Nfd);
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -070047
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{
Davide Pesaventoa3148082018-04-12 18:21:54 -040076 configureLogging();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080077
Davide Pesaventocfb1a312018-03-01 01:30:56 -050078 m_forwarder = make_unique<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
Davide Pesaventoe4b22382018-06-10 14:37:24 -040092 m_reloadConfigEvent = scheduler::schedule(5_s,
Junxiao Shi2d491752017-07-14 21:32:05 +000093 [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
Davide Pesaventoa3148082018-04-12 18:21:54 -0400101Nfd::configureLogging()
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800102{
103 ConfigFile config(&ConfigFile::ignoreUnknownSection);
Davide Pesaventoa3148082018-04-12 18:21:54 -0400104 log::setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800105
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
Davide Pesaventocfb1a312018-03-01 01:30:56 -0500137 m_dispatcher = make_unique<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
Davide Pesaventocfb1a312018-03-01 01:30:56 -0500140 m_forwarderStatusManager = make_unique<ForwarderStatusManager>(*m_forwarder, *m_dispatcher);
141 m_faceManager = make_unique<FaceManager>(*m_faceSystem, *m_dispatcher, *m_authenticator);
142 m_fibManager = make_unique<FibManager>(m_forwarder->getFib(), m_forwarder->getFaceTable(),
143 *m_dispatcher, *m_authenticator);
144 m_csManager = make_unique<CsManager>(m_forwarder->getCs(), m_forwarder->getCounters(),
145 *m_dispatcher, *m_authenticator);
146 m_strategyChoiceManager = make_unique<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);
Davide Pesaventocfb1a312018-03-01 01:30:56 -0500156 m_faceSystem->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800157
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");
ashiqopu3ad49db2018-10-20 22:38:47 +0000172 m_forwarder->getFib().insert(topPrefix).first->addOrUpdateNextHop(*m_internalFace, 0, 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{
Davide Pesaventoa3148082018-04-12 18:21:54 -0400179 configureLogging();
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800180
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800181 ConfigFile config(&ignoreRibAndLogSections);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800182 general::setConfigFile(config);
183
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000184 TablesConfigSection tablesConfig(*m_forwarder);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800185 tablesConfig.setConfigFile(config);
186
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000187 m_authenticator->setConfigFile(config);
Davide Pesaventocfb1a312018-03-01 01:30:56 -0500188 m_faceSystem->setConfigFile(config);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800189
190 if (!m_configFile.empty()) {
191 config.parse(m_configFile, false);
192 }
193 else {
194 config.parse(m_configSection, false, INTERNAL_CONFIG);
195 }
196}
197
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -0700198void
199Nfd::reloadConfigFileFaceSection()
200{
201 // reload only face_system section of the config file to re-initialize multicast faces
202 ConfigFile config(&ConfigFile::ignoreUnknownSection);
Davide Pesaventocfb1a312018-03-01 01:30:56 -0500203 m_faceSystem->setConfigFile(config);
Alexander Afanasyev3f41ade2015-06-29 18:31:22 -0700204
205 if (!m_configFile.empty()) {
206 config.parse(m_configFile, false);
207 }
208 else {
209 config.parse(m_configSection, false, INTERNAL_CONFIG);
210 }
211}
212
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800213} // namespace nfd