Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 6cc9541 | 2022-09-20 19:10:55 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 4 | * 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 Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/global.hpp" |
| 28 | #include "common/logger.hpp" |
| 29 | #include "common/privilege-helper.hpp" |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 30 | #include "face/face-system.hpp" |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 31 | #include "face/internal-face.hpp" |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 32 | #include "face/null-face.hpp" |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 33 | #include "fw/face-table.hpp" |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 34 | #include "fw/forwarder.hpp" |
| 35 | #include "mgmt/cs-manager.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 36 | #include "mgmt/face-manager.hpp" |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 37 | #include "mgmt/fib-manager.hpp" |
Yanbiao Li | 7cec7ea | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 38 | #include "mgmt/forwarder-status-manager.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 39 | #include "mgmt/general-config-section.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 40 | #include "mgmt/log-config-section.hpp" |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame] | 41 | #include "mgmt/strategy-choice-manager.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 42 | #include "mgmt/tables-config-section.hpp" |
| 43 | |
| 44 | namespace nfd { |
| 45 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 46 | NFD_LOG_INIT(Nfd); |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 47 | |
Davide Pesavento | 6cc9541 | 2022-09-20 19:10:55 -0400 | [diff] [blame] | 48 | const std::string INTERNAL_CONFIG{"internal://nfd.conf"}; |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 49 | |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 50 | Nfd::Nfd(ndn::KeyChain& keyChain) |
| 51 | : m_keyChain(keyChain) |
| 52 | , m_netmon(make_shared<ndn::net::NetworkMonitor>(getGlobalIoService())) |
Alexander Afanasyev | 441ba21 | 2015-09-10 23:41:07 -0700 | [diff] [blame] | 53 | { |
Davide Pesavento | 859a69e | 2019-07-13 17:10:46 -0400 | [diff] [blame] | 54 | // Disable automatic verification of parameters digest for decoded Interests. |
| 55 | Interest::setAutoCheckParametersDigest(false); |
Alexander Afanasyev | 441ba21 | 2015-09-10 23:41:07 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 58 | Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain) |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 59 | : Nfd(keyChain) |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 60 | { |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 61 | m_configFile = configFile; |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain) |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 65 | : Nfd(keyChain) |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 66 | { |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 67 | m_configSection = config; |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 70 | // It is necessary to explicitly define the destructor, because some member variables (e.g., |
| 71 | // unique_ptr<Forwarder>) are forward-declared, but implicitly declared destructor requires |
| 72 | // complete types for all members when instantiated. |
| 73 | Nfd::~Nfd() = default; |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 75 | void |
| 76 | Nfd::initialize() |
| 77 | { |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 78 | configureLogging(); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 79 | |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 80 | m_faceTable = make_unique<FaceTable>(); |
| 81 | m_faceTable->addReserved(face::makeNullFace(), face::FACEID_NULL); |
| 82 | m_faceTable->addReserved(face::makeNullFace(FaceUri("contentstore://")), face::FACEID_CONTENT_STORE); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 83 | |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 84 | m_faceSystem = make_unique<face::FaceSystem>(*m_faceTable, m_netmon); |
| 85 | m_forwarder = make_unique<Forwarder>(*m_faceTable); |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 86 | |
| 87 | initializeManagement(); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 89 | PrivilegeHelper::drop(); |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 90 | |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 91 | m_netmon->onNetworkStateChanged.connect([this] { |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 92 | // delay stages, so if multiple events are triggered in short sequence, |
| 93 | // only one auto-detection procedure is triggered |
| 94 | m_reloadConfigEvent = getScheduler().schedule(5_s, [this] { |
| 95 | NFD_LOG_INFO("Network change detected, reloading face section of the config file..."); |
| 96 | reloadConfigFileFaceSection(); |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 97 | }); |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 98 | }); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 102 | Nfd::configureLogging() |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 103 | { |
| 104 | ConfigFile config(&ConfigFile::ignoreUnknownSection); |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 105 | log::setConfigFile(config); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 106 | |
| 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 Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 117 | static inline void |
| 118 | ignoreRibAndLogSections(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 | |
| 132 | void |
| 133 | Nfd::initializeManagement() |
| 134 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 135 | std::tie(m_internalFace, m_internalClientFace) = face::makeInternalFace(m_keyChain); |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 136 | m_faceTable->addReserved(m_internalFace, face::FACEID_INTERNAL_FACE); |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 137 | |
Davide Pesavento | cfb1a31 | 2018-03-01 01:30:56 -0500 | [diff] [blame] | 138 | m_dispatcher = make_unique<ndn::mgmt::Dispatcher>(*m_internalClientFace, m_keyChain); |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 139 | m_authenticator = CommandAuthenticator::create(); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 140 | |
Davide Pesavento | cfb1a31 | 2018-03-01 01:30:56 -0500 | [diff] [blame] | 141 | m_forwarderStatusManager = make_unique<ForwarderStatusManager>(*m_forwarder, *m_dispatcher); |
| 142 | m_faceManager = make_unique<FaceManager>(*m_faceSystem, *m_dispatcher, *m_authenticator); |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 143 | m_fibManager = make_unique<FibManager>(m_forwarder->getFib(), *m_faceTable, |
Davide Pesavento | cfb1a31 | 2018-03-01 01:30:56 -0500 | [diff] [blame] | 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 Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 149 | |
| 150 | ConfigFile config(&ignoreRibAndLogSections); |
| 151 | general::setConfigFile(config); |
| 152 | |
Philipp Moll | a103334 | 2021-06-14 09:34:21 +0200 | [diff] [blame] | 153 | m_forwarder->setConfigFile(config); |
| 154 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 155 | TablesConfigSection tablesConfig(*m_forwarder); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 156 | tablesConfig.setConfigFile(config); |
| 157 | |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 158 | m_authenticator->setConfigFile(config); |
Davide Pesavento | cfb1a31 | 2018-03-01 01:30:56 -0500 | [diff] [blame] | 159 | m_faceSystem->setConfigFile(config); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 160 | |
| 161 | // parse config file |
| 162 | if (!m_configFile.empty()) { |
| 163 | config.parse(m_configFile, true); |
| 164 | config.parse(m_configFile, false); |
| 165 | } |
| 166 | else { |
| 167 | config.parse(m_configSection, true, INTERNAL_CONFIG); |
| 168 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 169 | } |
| 170 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 171 | tablesConfig.ensureConfigured(); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 172 | |
| 173 | // add FIB entry for NFD Management Protocol |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 174 | Name topPrefix("/localhost/nfd"); |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 175 | fib::Entry* entry = m_forwarder->getFib().insert(topPrefix).first; |
| 176 | m_forwarder->getFib().addOrUpdateNextHop(*entry, *m_internalFace, 0); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 177 | m_dispatcher->addTopPrefix(topPrefix, false); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void |
| 181 | Nfd::reloadConfigFile() |
| 182 | { |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 183 | configureLogging(); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 184 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 185 | ConfigFile config(&ignoreRibAndLogSections); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 186 | general::setConfigFile(config); |
| 187 | |
Philipp Moll | a103334 | 2021-06-14 09:34:21 +0200 | [diff] [blame] | 188 | m_forwarder->setConfigFile(config); |
| 189 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 190 | TablesConfigSection tablesConfig(*m_forwarder); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 191 | tablesConfig.setConfigFile(config); |
| 192 | |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 193 | m_authenticator->setConfigFile(config); |
Davide Pesavento | cfb1a31 | 2018-03-01 01:30:56 -0500 | [diff] [blame] | 194 | m_faceSystem->setConfigFile(config); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 195 | |
| 196 | if (!m_configFile.empty()) { |
| 197 | config.parse(m_configFile, false); |
| 198 | } |
| 199 | else { |
| 200 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 201 | } |
| 202 | } |
| 203 | |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 204 | void |
| 205 | Nfd::reloadConfigFileFaceSection() |
| 206 | { |
| 207 | // reload only face_system section of the config file to re-initialize multicast faces |
| 208 | ConfigFile config(&ConfigFile::ignoreUnknownSection); |
Davide Pesavento | cfb1a31 | 2018-03-01 01:30:56 -0500 | [diff] [blame] | 209 | m_faceSystem->setConfigFile(config); |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 210 | |
| 211 | if (!m_configFile.empty()) { |
| 212 | config.parse(m_configFile, false); |
| 213 | } |
| 214 | else { |
| 215 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 216 | } |
| 217 | } |
| 218 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 219 | } // namespace nfd |