Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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" |
| 35 | #include "face/internal-client-face.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 36 | #include "mgmt/fib-manager.hpp" |
| 37 | #include "mgmt/face-manager.hpp" |
| 38 | #include "mgmt/strategy-choice-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" |
| 41 | #include "mgmt/tables-config-section.hpp" |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 42 | #include "mgmt/command-validator.hpp" |
| 43 | |
| 44 | #include <ndn-cxx/mgmt/dispatcher.hpp> |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 45 | |
| 46 | namespace nfd { |
| 47 | |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 48 | NFD_LOG_INIT("Nfd"); |
| 49 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 50 | static const std::string INTERNAL_CONFIG = "internal://nfd.conf"; |
| 51 | |
| 52 | Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain) |
| 53 | : m_configFile(configFile) |
| 54 | , m_keyChain(keyChain) |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 55 | , m_networkMonitor(getGlobalIoService()) |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | |
| 59 | Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain) |
| 60 | : m_configSection(config) |
| 61 | , m_keyChain(keyChain) |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 62 | , m_networkMonitor(getGlobalIoService()) |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 63 | { |
| 64 | } |
| 65 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 66 | Nfd::~Nfd() |
| 67 | { |
| 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 | } |
| 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 | |
| 80 | initializeManagement(); |
| 81 | |
| 82 | m_forwarder->getFaceTable().addReserved(make_shared<NullFace>(), FACEID_NULL); |
| 83 | m_forwarder->getFaceTable().addReserved(make_shared<NullFace>(FaceUri("contentstore://")), |
| 84 | FACEID_CONTENT_STORE); |
| 85 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 86 | PrivilegeHelper::drop(); |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 87 | |
| 88 | m_networkMonitor.onNetworkStateChanged.connect([this] { |
| 89 | // delay stages, so if multiple events are triggered in short sequence, |
| 90 | // only one auto-detection procedure is triggered |
| 91 | m_reloadConfigEvent = scheduler::schedule(time::seconds(5), |
| 92 | [this] { |
| 93 | NFD_LOG_INFO("Network change detected, reloading face section of the config file..."); |
| 94 | this->reloadConfigFileFaceSection(); |
| 95 | }); |
| 96 | }); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void |
| 100 | Nfd::initializeLogging() |
| 101 | { |
| 102 | ConfigFile config(&ConfigFile::ignoreUnknownSection); |
| 103 | LoggerFactory::getInstance().setConfigFile(config); |
| 104 | |
| 105 | if (!m_configFile.empty()) { |
| 106 | config.parse(m_configFile, true); |
| 107 | config.parse(m_configFile, false); |
| 108 | } |
| 109 | else { |
| 110 | config.parse(m_configSection, true, INTERNAL_CONFIG); |
| 111 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 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 | { |
| 134 | m_internalFace = make_shared<InternalFace>(); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 135 | m_forwarder->getFaceTable().addReserved(m_internalFace, FACEID_INTERNAL_FACE); |
| 136 | m_internalClientFace = makeInternalClientFace(m_internalFace, m_keyChain); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 137 | m_dispatcher.reset(new ndn::mgmt::Dispatcher(*m_internalClientFace, m_keyChain)); |
| 138 | |
| 139 | m_validator.reset(new CommandValidator()); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 140 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 141 | m_fibManager.reset(new FibManager(m_forwarder->getFib(), |
| 142 | bind(&Forwarder::getFace, m_forwarder.get(), _1), |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 143 | *m_dispatcher, |
| 144 | *m_validator)); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 145 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 146 | m_faceManager.reset(new FaceManager(m_forwarder->getFaceTable(), |
| 147 | *m_dispatcher, |
| 148 | *m_validator)); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 150 | m_strategyChoiceManager.reset(new StrategyChoiceManager(m_forwarder->getStrategyChoice(), |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 151 | *m_dispatcher, |
| 152 | *m_validator)); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 153 | |
Yanbiao Li | 7cec7ea | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 154 | m_forwarderStatusManager.reset(new ForwarderStatusManager(*m_forwarder, *m_dispatcher)); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 155 | |
| 156 | ConfigFile config(&ignoreRibAndLogSections); |
| 157 | general::setConfigFile(config); |
| 158 | |
| 159 | TablesConfigSection tablesConfig(m_forwarder->getCs(), |
| 160 | m_forwarder->getPit(), |
| 161 | m_forwarder->getFib(), |
| 162 | m_forwarder->getStrategyChoice(), |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 163 | m_forwarder->getMeasurements(), |
| 164 | m_forwarder->getNetworkRegionTable()); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 165 | tablesConfig.setConfigFile(config); |
| 166 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 167 | m_validator->setConfigFile(config); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 168 | |
| 169 | m_faceManager->setConfigFile(config); |
| 170 | |
| 171 | // parse config file |
| 172 | if (!m_configFile.empty()) { |
| 173 | config.parse(m_configFile, true); |
| 174 | config.parse(m_configFile, false); |
| 175 | } |
| 176 | else { |
| 177 | config.parse(m_configSection, true, INTERNAL_CONFIG); |
| 178 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 179 | } |
| 180 | |
| 181 | tablesConfig.ensureTablesAreConfigured(); |
| 182 | |
| 183 | // add FIB entry for NFD Management Protocol |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 184 | Name topPrefix("/localhost/nfd"); |
| 185 | auto entry = m_forwarder->getFib().insert(topPrefix).first; |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 186 | entry->addNextHop(m_internalFace, 0); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 187 | m_dispatcher->addTopPrefix(topPrefix, false); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void |
| 191 | Nfd::reloadConfigFile() |
| 192 | { |
| 193 | // Logging |
| 194 | initializeLogging(); |
| 195 | /// \todo Reopen log file |
| 196 | |
| 197 | // Other stuff |
| 198 | ConfigFile config(&ignoreRibAndLogSections); |
| 199 | |
| 200 | general::setConfigFile(config); |
| 201 | |
| 202 | TablesConfigSection tablesConfig(m_forwarder->getCs(), |
| 203 | m_forwarder->getPit(), |
| 204 | m_forwarder->getFib(), |
| 205 | m_forwarder->getStrategyChoice(), |
Vince Lehman | 63ab1bb | 2015-09-04 17:06:58 -0500 | [diff] [blame] | 206 | m_forwarder->getMeasurements(), |
| 207 | m_forwarder->getNetworkRegionTable()); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 208 | |
| 209 | tablesConfig.setConfigFile(config); |
| 210 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 211 | m_validator->setConfigFile(config); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 212 | m_faceManager->setConfigFile(config); |
| 213 | |
| 214 | if (!m_configFile.empty()) { |
| 215 | config.parse(m_configFile, false); |
| 216 | } |
| 217 | else { |
| 218 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 219 | } |
| 220 | } |
| 221 | |
Alexander Afanasyev | 3f41ade | 2015-06-29 18:31:22 -0700 | [diff] [blame] | 222 | void |
| 223 | Nfd::reloadConfigFileFaceSection() |
| 224 | { |
| 225 | // reload only face_system section of the config file to re-initialize multicast faces |
| 226 | ConfigFile config(&ConfigFile::ignoreUnknownSection); |
| 227 | m_faceManager->setConfigFile(config); |
| 228 | |
| 229 | if (!m_configFile.empty()) { |
| 230 | config.parse(m_configFile, false); |
| 231 | } |
| 232 | else { |
| 233 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 234 | } |
| 235 | } |
| 236 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 237 | } // namespace nfd |