Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -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 | |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 26 | #include "service.hpp" |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 27 | |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 28 | #include "fib-updater.hpp" |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 29 | #include "readvertise/client-to-nlsr-readvertise-policy.hpp" |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 30 | #include "readvertise/host-to-gateway-readvertise-policy.hpp" |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 31 | #include "readvertise/nfd-rib-readvertise-destination.hpp" |
| 32 | #include "readvertise/readvertise.hpp" |
| 33 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 34 | #include "common/global.hpp" |
| 35 | #include "common/logger.hpp" |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 36 | |
| 37 | #include <boost/property_tree/info_parser.hpp> |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 38 | #include <ndn-cxx/transport/tcp-transport.hpp> |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 39 | #include <ndn-cxx/transport/unix-transport.hpp> |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 40 | |
| 41 | namespace nfd { |
| 42 | namespace rib { |
| 43 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 44 | NFD_LOG_INIT(RibService); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 45 | |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 46 | Service* Service::s_instance = nullptr; |
| 47 | |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 48 | const std::string CFG_SECTION = "rib"; |
| 49 | const std::string CFG_LOCALHOST_SECURITY = "localhost_security"; |
| 50 | const std::string CFG_LOCALHOP_SECURITY = "localhop_security"; |
| 51 | const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate"; |
| 52 | const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr"; |
| 53 | const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr"; |
| 54 | const uint64_t PROPAGATE_DEFAULT_COST = 15; |
| 55 | const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s; |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 56 | |
| 57 | static ConfigSection |
| 58 | loadConfigSectionFromFile(const std::string& filename) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 59 | { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 60 | ConfigSection config; |
| 61 | // Any format errors should have been caught already |
| 62 | boost::property_tree::read_info(filename, config); |
| 63 | return config; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * \brief Look into the config file and construct appropriate transport to communicate with NFD |
| 68 | * If NFD-RIB instance was initialized with config file, INFO format is assumed |
| 69 | */ |
| 70 | static shared_ptr<ndn::Transport> |
| 71 | makeLocalNfdTransport(const ConfigSection& config) |
| 72 | { |
| 73 | if (config.get_child_optional("face_system.unix")) { |
| 74 | // default socket path should be the same as in UnixStreamFactory::processConfig |
| 75 | auto path = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock"); |
| 76 | return make_shared<ndn::UnixTransport>(path); |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 77 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 78 | else if (config.get_child_optional("face_system.tcp") && |
| 79 | config.get<std::string>("face_system.tcp.listen", "yes") == "yes") { |
| 80 | // default port should be the same as in TcpFactory::processConfig |
| 81 | auto port = config.get<std::string>("face_system.tcp.port", "6363"); |
| 82 | return make_shared<ndn::TcpTransport>("localhost", port); |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 83 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 84 | else { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 85 | NDN_THROW(ConfigFile::Error("No transport is available to communicate with NFD")); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | Service::Service(const std::string& configFile, ndn::KeyChain& keyChain) |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 90 | : Service(keyChain, makeLocalNfdTransport(loadConfigSectionFromFile(configFile)), |
| 91 | [&configFile] (ConfigFile& config, bool isDryRun) { |
| 92 | config.parse(configFile, isDryRun); |
| 93 | }) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 94 | { |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 97 | Service::Service(const ConfigSection& configSection, ndn::KeyChain& keyChain) |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 98 | : Service(keyChain, makeLocalNfdTransport(configSection), |
| 99 | [&configSection] (ConfigFile& config, bool isDryRun) { |
| 100 | config.parse(configSection, isDryRun, "internal://nfd.conf"); |
| 101 | }) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 102 | { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 105 | template<typename ConfigParseFunc> |
| 106 | Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport, |
| 107 | const ConfigParseFunc& configParse) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 108 | : m_keyChain(keyChain) |
| 109 | , m_face(std::move(localNfdTransport), getGlobalIoService(), m_keyChain) |
| 110 | , m_nfdController(m_face, m_keyChain) |
| 111 | , m_fibUpdater(m_rib, m_nfdController) |
| 112 | , m_dispatcher(m_face, m_keyChain) |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 113 | , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 114 | { |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 115 | if (s_instance != nullptr) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 116 | NDN_THROW(std::logic_error("RIB service cannot be instantiated more than once")); |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 117 | } |
| 118 | if (&getGlobalIoService() != &getRibIoService()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 119 | NDN_THROW(std::logic_error("RIB service must run on RIB thread")); |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 120 | } |
| 121 | s_instance = this; |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 122 | |
| 123 | ConfigFile config(ConfigFile::ignoreUnknownSection); |
| 124 | config.addSectionHandler(CFG_SECTION, bind(&Service::processConfig, this, _1, _2, _3)); |
| 125 | configParse(config, true); |
| 126 | configParse(config, false); |
| 127 | |
| 128 | m_ribManager.registerWithNfd(); |
| 129 | m_ribManager.enableLocalFields(); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 132 | Service::~Service() |
| 133 | { |
| 134 | s_instance = nullptr; |
| 135 | } |
| 136 | |
| 137 | Service& |
| 138 | Service::get() |
| 139 | { |
| 140 | if (s_instance == nullptr) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 141 | NDN_THROW(std::logic_error("RIB service is not instantiated")); |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 142 | } |
| 143 | if (&getGlobalIoService() != &getRibIoService()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 144 | NDN_THROW(std::logic_error("Must get RIB service on RIB thread")); |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 145 | } |
| 146 | return *s_instance; |
| 147 | } |
Alexander Afanasyev | c3ea5a7 | 2015-02-12 20:14:16 -0800 | [diff] [blame] | 148 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 149 | void |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 150 | Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 151 | { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 152 | if (isDryRun) { |
| 153 | checkConfig(section, filename); |
| 154 | } |
| 155 | else { |
| 156 | applyConfig(section, filename); |
| 157 | } |
| 158 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 159 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 160 | void |
| 161 | Service::checkConfig(const ConfigSection& section, const std::string& filename) |
| 162 | { |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 163 | bool hasLocalhop = false; |
| 164 | bool hasPropagate = false; |
| 165 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 166 | for (const auto& item : section) { |
| 167 | const std::string& key = item.first; |
Ashlesh Gawande | f84bac5 | 2018-08-27 07:39:31 +0000 | [diff] [blame] | 168 | const ConfigSection& value = item.second; |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 169 | if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY) { |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 170 | hasLocalhop = key == CFG_LOCALHOP_SECURITY; |
Alexander Afanasyev | 3c0c035 | 2018-10-17 11:59:23 -0400 | [diff] [blame] | 171 | ndn::ValidatorConfig testValidator(m_face); |
| 172 | testValidator.load(value, filename); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 173 | } |
| 174 | else if (key == CFG_PREFIX_PROPAGATE) { |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 175 | hasPropagate = true; |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 176 | // AutoPrefixPropagator does not support config dry-run |
| 177 | } |
| 178 | else if (key == CFG_READVERTISE_NLSR) { |
| 179 | ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR); |
| 180 | } |
| 181 | else { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 182 | NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key)); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 185 | |
| 186 | if (hasLocalhop && hasPropagate) { |
| 187 | NDN_THROW(ConfigFile::Error(CFG_LOCALHOP_SECURITY + " and " + CFG_PREFIX_PROPAGATE + |
| 188 | " cannot be enabled at the same time")); |
| 189 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void |
| 193 | Service::applyConfig(const ConfigSection& section, const std::string& filename) |
| 194 | { |
| 195 | bool wantPrefixPropagate = false; |
| 196 | bool wantReadvertiseNlsr = false; |
| 197 | |
| 198 | for (const auto& item : section) { |
| 199 | const std::string& key = item.first; |
| 200 | const ConfigSection& value = item.second; |
| 201 | if (key == CFG_LOCALHOST_SECURITY) { |
| 202 | m_ribManager.applyLocalhostConfig(value, filename); |
| 203 | } |
| 204 | else if (key == CFG_LOCALHOP_SECURITY) { |
| 205 | m_ribManager.enableLocalhop(value, filename); |
| 206 | } |
| 207 | else if (key == CFG_PREFIX_PROPAGATE) { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 208 | wantPrefixPropagate = true; |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 209 | |
| 210 | if (!m_readvertisePropagation) { |
| 211 | NFD_LOG_DEBUG("Enabling automatic prefix propagation"); |
| 212 | |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 213 | auto cost = item.second.get_optional<uint64_t>("cost"); |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 214 | auto parameters = ndn::nfd::ControlParameters() |
| 215 | .setCost(cost.value_or(PROPAGATE_DEFAULT_COST)) |
| 216 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT); |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 217 | |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 218 | auto timeout = item.second.get_optional<uint64_t>("timeout"); |
Wenkai Zheng | 6598fb0 | 2019-09-08 18:03:46 -0700 | [diff] [blame] | 219 | auto options = ndn::nfd::CommandOptions() |
| 220 | .setPrefix(RibManager::LOCALHOP_TOP_PREFIX) |
| 221 | .setTimeout(timeout ? time::milliseconds(*timeout) : PROPAGATE_DEFAULT_TIMEOUT); |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 222 | |
| 223 | m_readvertisePropagation = make_unique<Readvertise>( |
| 224 | m_rib, |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 225 | make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second), |
| 226 | make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters)); |
| 227 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 228 | } |
| 229 | else if (key == CFG_READVERTISE_NLSR) { |
| 230 | wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR); |
| 231 | } |
| 232 | else { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 233 | NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key)); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 234 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 237 | if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) { |
| 238 | NFD_LOG_DEBUG("Disabling automatic prefix propagation"); |
| 239 | m_readvertisePropagation.reset(); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 242 | if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) { |
| 243 | NFD_LOG_DEBUG("Enabling readvertise-to-nlsr"); |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 244 | auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 245 | m_readvertiseNlsr = make_unique<Readvertise>( |
| 246 | m_rib, |
| 247 | make_unique<ClientToNlsrReadvertisePolicy>(), |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 248 | make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options)); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 249 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 250 | else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) { |
| 251 | NFD_LOG_DEBUG("Disabling readvertise-to-nlsr"); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 252 | m_readvertiseNlsr.reset(); |
| 253 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 256 | } // namespace rib |
| 257 | } // namespace nfd |