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 | /* |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, 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" |
| 27 | |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame^] | 28 | #include "core/config-file.hpp" |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 29 | #include "core/global-io.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 30 | #include "core/logger-factory.hpp" |
| 31 | #include "core/privilege-helper.hpp" |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 32 | #include "face/face-system.hpp" |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 33 | #include "face/internal-face.hpp" |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame^] | 34 | #include "face/null-face.hpp" |
| 35 | #include "fw/forwarder.hpp" |
| 36 | #include "mgmt/cs-manager.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 37 | #include "mgmt/face-manager.hpp" |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame^] | 38 | #include "mgmt/fib-manager.hpp" |
Yanbiao Li | 7cec7ea | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 39 | #include "mgmt/forwarder-status-manager.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 40 | #include "mgmt/general-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 | |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 46 | NFD_LOG_INIT("Nfd"); |
| 47 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 48 | static const std::string INTERNAL_CONFIG = "internal://nfd.conf"; |
| 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 | { |
Alexander Afanasyev | 441ba21 | 2015-09-10 23:41:07 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 56 | Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain) |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 57 | : Nfd(keyChain) |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 58 | { |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 59 | m_configFile = configFile; |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain) |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 63 | : Nfd(keyChain) |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 64 | { |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 65 | m_configSection = config; |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 68 | // 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. |
| 71 | Nfd::~Nfd() = default; |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 73 | void |
| 74 | Nfd::initialize() |
| 75 | { |
| 76 | initializeLogging(); |
| 77 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 78 | m_forwarder.reset(new Forwarder()); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 79 | |
Junxiao Shi | a044be7 | 2015-10-26 11:00:56 -0700 | [diff] [blame] | 80 | FaceTable& faceTable = m_forwarder->getFaceTable(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 81 | faceTable.addReserved(face::makeNullFace(), face::FACEID_NULL); |
| 82 | faceTable.addReserved(face::makeNullFace(FaceUri("contentstore://")), face::FACEID_CONTENT_STORE); |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 83 | m_faceSystem = make_unique<face::FaceSystem>(faceTable, m_netmon); |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 84 | |
| 85 | initializeManagement(); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 86 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 87 | PrivilegeHelper::drop(); |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 88 | |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 89 | 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 Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
| 101 | Nfd::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 Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 116 | static inline void |
| 117 | ignoreRibAndLogSections(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 | |
| 131 | void |
| 132 | Nfd::initializeManagement() |
| 133 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 134 | std::tie(m_internalFace, m_internalClientFace) = face::makeInternalFace(m_keyChain); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 135 | m_forwarder->getFaceTable().addReserved(m_internalFace, face::FACEID_INTERNAL_FACE); |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 136 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 137 | m_dispatcher.reset(new ndn::mgmt::Dispatcher(*m_internalClientFace, m_keyChain)); |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 138 | m_authenticator = CommandAuthenticator::create(); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 139 | |
Yanbiao Li | 7cec7ea | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 140 | m_forwarderStatusManager.reset(new ForwarderStatusManager(*m_forwarder, *m_dispatcher)); |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 141 | m_faceManager.reset(new FaceManager(*m_faceSystem, *m_dispatcher, *m_authenticator)); |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 142 | m_fibManager.reset(new FibManager(m_forwarder->getFib(), m_forwarder->getFaceTable(), |
| 143 | *m_dispatcher, *m_authenticator)); |
Junxiao Shi | f2bfb44 | 2018-01-05 12:34:57 +0000 | [diff] [blame^] | 144 | m_csManager.reset(new CsManager(m_forwarder->getCs(), m_forwarder->getCounters(), |
| 145 | *m_dispatcher, *m_authenticator)); |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 146 | m_strategyChoiceManager.reset(new StrategyChoiceManager(m_forwarder->getStrategyChoice(), |
| 147 | *m_dispatcher, *m_authenticator)); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 148 | |
| 149 | ConfigFile config(&ignoreRibAndLogSections); |
| 150 | general::setConfigFile(config); |
| 151 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 152 | TablesConfigSection tablesConfig(*m_forwarder); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 153 | tablesConfig.setConfigFile(config); |
| 154 | |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 155 | m_authenticator->setConfigFile(config); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 156 | 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 Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 168 | tablesConfig.ensureConfigured(); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 169 | |
| 170 | // add FIB entry for NFD Management Protocol |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 171 | Name topPrefix("/localhost/nfd"); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 172 | m_forwarder->getFib().insert(topPrefix).first->addNextHop(*m_internalFace, 0); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 173 | m_dispatcher->addTopPrefix(topPrefix, false); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void |
| 177 | Nfd::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 Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 188 | TablesConfigSection tablesConfig(*m_forwarder); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 189 | tablesConfig.setConfigFile(config); |
| 190 | |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 191 | m_authenticator->setConfigFile(config); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 192 | 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 Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 202 | void |
| 203 | Nfd::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 Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 217 | } // namespace nfd |