Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 28 | #include "core/global-io.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 29 | #include "core/logger-factory.hpp" |
| 30 | #include "core/privilege-helper.hpp" |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 31 | #include "core/config-file.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 32 | #include "fw/forwarder.hpp" |
| 33 | #include "face/null-face.hpp" |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 34 | #include "face/internal-face.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 35 | #include "mgmt/fib-manager.hpp" |
| 36 | #include "mgmt/face-manager.hpp" |
| 37 | #include "mgmt/strategy-choice-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" |
| 40 | #include "mgmt/tables-config-section.hpp" |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 41 | #include "mgmt/command-validator.hpp" |
| 42 | |
| 43 | #include <ndn-cxx/mgmt/dispatcher.hpp> |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 44 | |
| 45 | namespace nfd { |
| 46 | |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 47 | NFD_LOG_INIT("Nfd"); |
| 48 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 49 | static const std::string INTERNAL_CONFIG = "internal://nfd.conf"; |
| 50 | |
Alexander Afanasyev | 441ba21 | 2015-09-10 23:41:07 -0700 | [diff] [blame] | 51 | static inline ndn::util::NetworkMonitor* |
| 52 | makeNetworkMonitor() |
| 53 | { |
| 54 | try { |
| 55 | return new ndn::util::NetworkMonitor(getGlobalIoService()); |
| 56 | } |
| 57 | catch (const ndn::util::NetworkMonitor::Error& e) { |
| 58 | NFD_LOG_WARN(e.what()); |
| 59 | return nullptr; |
| 60 | } |
| 61 | } |
| 62 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 63 | Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain) |
| 64 | : m_configFile(configFile) |
| 65 | , m_keyChain(keyChain) |
Alexander Afanasyev | 441ba21 | 2015-09-10 23:41:07 -0700 | [diff] [blame] | 66 | , m_networkMonitor(makeNetworkMonitor()) |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 67 | { |
| 68 | } |
| 69 | |
| 70 | Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain) |
| 71 | : m_configSection(config) |
| 72 | , m_keyChain(keyChain) |
Alexander Afanasyev | 441ba21 | 2015-09-10 23:41:07 -0700 | [diff] [blame] | 73 | , m_networkMonitor(makeNetworkMonitor()) |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 74 | { |
| 75 | } |
| 76 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 77 | Nfd::~Nfd() |
| 78 | { |
| 79 | // It is necessary to explicitly define the destructor, because some member variables (e.g., |
| 80 | // unique_ptr<Forwarder>) are forward-declared, but implicitly declared destructor requires |
| 81 | // complete types for all members when instantiated. |
| 82 | } |
| 83 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 84 | void |
| 85 | Nfd::initialize() |
| 86 | { |
| 87 | initializeLogging(); |
| 88 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 89 | m_forwarder.reset(new Forwarder()); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 90 | |
| 91 | initializeManagement(); |
| 92 | |
Junxiao Shi | a044be7 | 2015-10-26 11:00:56 -0700 | [diff] [blame] | 93 | FaceTable& faceTable = m_forwarder->getFaceTable(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 94 | faceTable.addReserved(face::makeNullFace(), face::FACEID_NULL); |
| 95 | faceTable.addReserved(face::makeNullFace(FaceUri("contentstore://")), face::FACEID_CONTENT_STORE); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 97 | PrivilegeHelper::drop(); |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 98 | |
Alexander Afanasyev | 441ba21 | 2015-09-10 23:41:07 -0700 | [diff] [blame] | 99 | if (m_networkMonitor) { |
| 100 | m_networkMonitor->onNetworkStateChanged.connect([this] { |
| 101 | // delay stages, so if multiple events are triggered in short sequence, |
| 102 | // only one auto-detection procedure is triggered |
| 103 | m_reloadConfigEvent = scheduler::schedule(time::seconds(5), |
| 104 | [this] { |
| 105 | NFD_LOG_INFO("Network change detected, reloading face section of the config file..."); |
| 106 | this->reloadConfigFileFaceSection(); |
| 107 | }); |
| 108 | }); |
| 109 | } |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void |
| 113 | Nfd::initializeLogging() |
| 114 | { |
| 115 | ConfigFile config(&ConfigFile::ignoreUnknownSection); |
| 116 | LoggerFactory::getInstance().setConfigFile(config); |
| 117 | |
| 118 | if (!m_configFile.empty()) { |
| 119 | config.parse(m_configFile, true); |
| 120 | config.parse(m_configFile, false); |
| 121 | } |
| 122 | else { |
| 123 | config.parse(m_configSection, true, INTERNAL_CONFIG); |
| 124 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | static inline void |
| 130 | ignoreRibAndLogSections(const std::string& filename, const std::string& sectionName, |
| 131 | const ConfigSection& section, bool isDryRun) |
| 132 | { |
| 133 | // Ignore "log" and "rib" sections, but raise an error if we're missing a |
| 134 | // handler for an NFD section. |
| 135 | if (sectionName == "rib" || sectionName == "log") { |
| 136 | // do nothing |
| 137 | } |
| 138 | else { |
| 139 | // missing NFD section |
| 140 | ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | Nfd::initializeManagement() |
| 146 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 147 | std::tie(m_internalFace, m_internalClientFace) = face::makeInternalFace(m_keyChain); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 148 | m_forwarder->getFaceTable().addReserved(m_internalFace, face::FACEID_INTERNAL_FACE); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 149 | m_dispatcher.reset(new ndn::mgmt::Dispatcher(*m_internalClientFace, m_keyChain)); |
| 150 | |
| 151 | m_validator.reset(new CommandValidator()); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 152 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 153 | m_fibManager.reset(new FibManager(m_forwarder->getFib(), |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 154 | m_forwarder->getFaceTable(), |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 155 | *m_dispatcher, |
| 156 | *m_validator)); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 157 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 158 | m_faceManager.reset(new FaceManager(m_forwarder->getFaceTable(), |
| 159 | *m_dispatcher, |
| 160 | *m_validator)); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 161 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 162 | m_strategyChoiceManager.reset(new StrategyChoiceManager(m_forwarder->getStrategyChoice(), |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 163 | *m_dispatcher, |
| 164 | *m_validator)); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 165 | |
Yanbiao Li | 7cec7ea | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 166 | m_forwarderStatusManager.reset(new ForwarderStatusManager(*m_forwarder, *m_dispatcher)); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 167 | |
| 168 | ConfigFile config(&ignoreRibAndLogSections); |
| 169 | general::setConfigFile(config); |
| 170 | |
| 171 | TablesConfigSection tablesConfig(m_forwarder->getCs(), |
| 172 | m_forwarder->getPit(), |
| 173 | m_forwarder->getFib(), |
| 174 | m_forwarder->getStrategyChoice(), |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 175 | m_forwarder->getMeasurements(), |
| 176 | m_forwarder->getNetworkRegionTable()); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 177 | tablesConfig.setConfigFile(config); |
| 178 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 179 | m_validator->setConfigFile(config); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 180 | |
| 181 | m_faceManager->setConfigFile(config); |
| 182 | |
| 183 | // parse config file |
| 184 | if (!m_configFile.empty()) { |
| 185 | config.parse(m_configFile, true); |
| 186 | config.parse(m_configFile, false); |
| 187 | } |
| 188 | else { |
| 189 | config.parse(m_configSection, true, INTERNAL_CONFIG); |
| 190 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 191 | } |
| 192 | |
| 193 | tablesConfig.ensureTablesAreConfigured(); |
| 194 | |
| 195 | // add FIB entry for NFD Management Protocol |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 196 | Name topPrefix("/localhost/nfd"); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 197 | m_forwarder->getFib().insert(topPrefix).first->addNextHop(*m_internalFace, 0); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 198 | m_dispatcher->addTopPrefix(topPrefix, false); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void |
| 202 | Nfd::reloadConfigFile() |
| 203 | { |
| 204 | // Logging |
| 205 | initializeLogging(); |
| 206 | /// \todo Reopen log file |
| 207 | |
| 208 | // Other stuff |
| 209 | ConfigFile config(&ignoreRibAndLogSections); |
| 210 | |
| 211 | general::setConfigFile(config); |
| 212 | |
| 213 | TablesConfigSection tablesConfig(m_forwarder->getCs(), |
| 214 | m_forwarder->getPit(), |
| 215 | m_forwarder->getFib(), |
| 216 | m_forwarder->getStrategyChoice(), |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 217 | m_forwarder->getMeasurements(), |
| 218 | m_forwarder->getNetworkRegionTable()); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 219 | |
| 220 | tablesConfig.setConfigFile(config); |
| 221 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 222 | m_validator->setConfigFile(config); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 223 | m_faceManager->setConfigFile(config); |
| 224 | |
| 225 | if (!m_configFile.empty()) { |
| 226 | config.parse(m_configFile, false); |
| 227 | } |
| 228 | else { |
| 229 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 230 | } |
| 231 | } |
| 232 | |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 233 | void |
| 234 | Nfd::reloadConfigFileFaceSection() |
| 235 | { |
| 236 | // reload only face_system section of the config file to re-initialize multicast faces |
| 237 | ConfigFile config(&ConfigFile::ignoreUnknownSection); |
| 238 | m_faceManager->setConfigFile(config); |
| 239 | |
| 240 | if (!m_configFile.empty()) { |
| 241 | config.parse(m_configFile, false); |
| 242 | } |
| 243 | else { |
| 244 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 245 | } |
| 246 | } |
| 247 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 248 | } // namespace nfd |