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