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 | /* |
| 3 | * Copyright (c) 2014-2018, 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 | |
| 28 | #include "auto-prefix-propagator.hpp" |
| 29 | #include "fib-updater.hpp" |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 30 | #include "readvertise/client-to-nlsr-readvertise-policy.hpp" |
| 31 | #include "readvertise/nfd-rib-readvertise-destination.hpp" |
| 32 | #include "readvertise/readvertise.hpp" |
| 33 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 34 | #include "core/global-io.hpp" |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 35 | #include "core/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 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 48 | static const std::string CFG_SECTION = "rib"; |
| 49 | static const std::string CFG_LOCALHOST_SECURITY = "localhost_security"; |
| 50 | static const std::string CFG_LOCALHOP_SECURITY = "localhop_security"; |
| 51 | static const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate"; |
| 52 | static const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr"; |
| 53 | static const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr"; |
| 54 | |
| 55 | static ConfigSection |
| 56 | loadConfigSectionFromFile(const std::string& filename) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 57 | { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 58 | ConfigSection config; |
| 59 | // Any format errors should have been caught already |
| 60 | boost::property_tree::read_info(filename, config); |
| 61 | return config; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * \brief Look into the config file and construct appropriate transport to communicate with NFD |
| 66 | * If NFD-RIB instance was initialized with config file, INFO format is assumed |
| 67 | */ |
| 68 | static shared_ptr<ndn::Transport> |
| 69 | makeLocalNfdTransport(const ConfigSection& config) |
| 70 | { |
| 71 | if (config.get_child_optional("face_system.unix")) { |
| 72 | // default socket path should be the same as in UnixStreamFactory::processConfig |
| 73 | auto path = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock"); |
| 74 | return make_shared<ndn::UnixTransport>(path); |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 75 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 76 | else if (config.get_child_optional("face_system.tcp") && |
| 77 | config.get<std::string>("face_system.tcp.listen", "yes") == "yes") { |
| 78 | // default port should be the same as in TcpFactory::processConfig |
| 79 | auto port = config.get<std::string>("face_system.tcp.port", "6363"); |
| 80 | return make_shared<ndn::TcpTransport>("localhost", port); |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 81 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 82 | else { |
| 83 | BOOST_THROW_EXCEPTION(ConfigFile::Error("No transport is available to communicate with NFD")); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | Service::Service(const std::string& configFile, ndn::KeyChain& keyChain) |
| 88 | : Service(keyChain, makeLocalNfdTransport(loadConfigSectionFromFile(configFile))) |
| 89 | { |
| 90 | ConfigFile config(ConfigFile::ignoreUnknownSection); |
| 91 | config.addSectionHandler(CFG_SECTION, bind(&Service::processConfig, this, _1, _2, _3)); |
| 92 | config.parse(configFile, true); |
| 93 | config.parse(configFile, false); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 96 | Service::Service(const ConfigSection& configSection, ndn::KeyChain& keyChain) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 97 | : Service(keyChain, makeLocalNfdTransport(configSection)) |
| 98 | { |
| 99 | ConfigFile config(ConfigFile::ignoreUnknownSection); |
| 100 | config.addSectionHandler(CFG_SECTION, bind(&Service::processConfig, this, _1, _2, _3)); |
| 101 | const std::string INTERNAL_CONFIG = "internal://nfd.conf"; |
| 102 | config.parse(configSection, true, INTERNAL_CONFIG); |
| 103 | config.parse(configSection, false, INTERNAL_CONFIG); |
| 104 | } |
| 105 | |
| 106 | Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport) |
| 107 | : m_keyChain(keyChain) |
| 108 | , m_face(std::move(localNfdTransport), getGlobalIoService(), m_keyChain) |
| 109 | , m_nfdController(m_face, m_keyChain) |
| 110 | , m_fibUpdater(m_rib, m_nfdController) |
| 111 | , m_dispatcher(m_face, m_keyChain) |
| 112 | , m_ribManager(m_rib, m_face, m_nfdController, m_dispatcher) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 113 | { |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 114 | if (s_instance != nullptr) { |
| 115 | BOOST_THROW_EXCEPTION(std::logic_error("RIB service cannot be instantiated more than once")); |
| 116 | } |
| 117 | if (&getGlobalIoService() != &getRibIoService()) { |
| 118 | BOOST_THROW_EXCEPTION(std::logic_error("RIB service must run on RIB thread")); |
| 119 | } |
| 120 | s_instance = this; |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 123 | Service::~Service() |
| 124 | { |
| 125 | s_instance = nullptr; |
| 126 | } |
| 127 | |
| 128 | Service& |
| 129 | Service::get() |
| 130 | { |
| 131 | if (s_instance == nullptr) { |
| 132 | BOOST_THROW_EXCEPTION(std::logic_error("RIB service is not instantiated")); |
| 133 | } |
| 134 | if (&getGlobalIoService() != &getRibIoService()) { |
| 135 | BOOST_THROW_EXCEPTION(std::logic_error("Must get RIB service on RIB thread")); |
| 136 | } |
| 137 | return *s_instance; |
| 138 | } |
Alexander Afanasyev | c3ea5a7 | 2015-02-12 20:14:16 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 140 | void |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 141 | Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 142 | { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 143 | if (isDryRun) { |
| 144 | checkConfig(section, filename); |
| 145 | } |
| 146 | else { |
| 147 | applyConfig(section, filename); |
| 148 | } |
| 149 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 150 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 151 | void |
| 152 | Service::checkConfig(const ConfigSection& section, const std::string& filename) |
| 153 | { |
| 154 | for (const auto& item : section) { |
| 155 | const std::string& key = item.first; |
| 156 | if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY) { |
| 157 | ndn::security::v2::validator_config::ValidationPolicyConfig policy; |
| 158 | policy.load(section, filename); |
| 159 | } |
| 160 | else if (key == CFG_PREFIX_PROPAGATE) { |
| 161 | // AutoPrefixPropagator does not support config dry-run |
| 162 | } |
| 163 | else if (key == CFG_READVERTISE_NLSR) { |
| 164 | ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR); |
| 165 | } |
| 166 | else { |
| 167 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key)); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | Service::applyConfig(const ConfigSection& section, const std::string& filename) |
| 174 | { |
| 175 | bool wantPrefixPropagate = false; |
| 176 | bool wantReadvertiseNlsr = false; |
| 177 | |
| 178 | for (const auto& item : section) { |
| 179 | const std::string& key = item.first; |
| 180 | const ConfigSection& value = item.second; |
| 181 | if (key == CFG_LOCALHOST_SECURITY) { |
| 182 | m_ribManager.applyLocalhostConfig(value, filename); |
| 183 | } |
| 184 | else if (key == CFG_LOCALHOP_SECURITY) { |
| 185 | m_ribManager.enableLocalhop(value, filename); |
| 186 | } |
| 187 | else if (key == CFG_PREFIX_PROPAGATE) { |
| 188 | if (m_prefixPropagator == nullptr) { |
| 189 | m_prefixPropagator = make_unique<AutoPrefixPropagator>(m_nfdController, m_keyChain, m_rib); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 190 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 191 | m_prefixPropagator->loadConfig(item.second); |
| 192 | m_prefixPropagator->enable(); |
| 193 | wantPrefixPropagate = true; |
| 194 | } |
| 195 | else if (key == CFG_READVERTISE_NLSR) { |
| 196 | wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR); |
| 197 | } |
| 198 | else { |
| 199 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key)); |
| 200 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 203 | if (!wantPrefixPropagate && m_prefixPropagator != nullptr) { |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 204 | m_prefixPropagator->disable(); |
| 205 | } |
| 206 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 207 | if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) { |
| 208 | NFD_LOG_DEBUG("Enabling readvertise-to-nlsr"); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 209 | m_readvertiseNlsr = make_unique<Readvertise>( |
| 210 | m_rib, |
| 211 | make_unique<ClientToNlsrReadvertisePolicy>(), |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 212 | make_unique<NfdRibReadvertiseDestination>(m_nfdController, READVERTISE_NLSR_PREFIX, m_rib)); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 213 | } |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 214 | else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) { |
| 215 | NFD_LOG_DEBUG("Disabling readvertise-to-nlsr"); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 216 | m_readvertiseNlsr.reset(); |
| 217 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 220 | void |
| 221 | Service::initialize() |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 222 | { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame^] | 223 | m_ribManager.registerWithNfd(); |
| 224 | m_ribManager.enableLocalFields(); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | } // namespace rib |
| 228 | } // namespace nfd |