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" |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 30 | #include "rib-manager.hpp" |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame^] | 31 | #include "readvertise/client-to-nlsr-readvertise-policy.hpp" |
| 32 | #include "readvertise/nfd-rib-readvertise-destination.hpp" |
| 33 | #include "readvertise/readvertise.hpp" |
| 34 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 35 | #include "core/global-io.hpp" |
| 36 | |
| 37 | #include <boost/property_tree/info_parser.hpp> |
| 38 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 39 | #include <ndn-cxx/transport/tcp-transport.hpp> |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame^] | 40 | #include <ndn-cxx/transport/unix-transport.hpp> |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 41 | |
| 42 | namespace nfd { |
| 43 | namespace rib { |
| 44 | |
| 45 | static const std::string INTERNAL_CONFIG = "internal://nfd.conf"; |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame^] | 46 | static const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr"; |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 47 | |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 48 | Service* Service::s_instance = nullptr; |
| 49 | |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 50 | Service::Service(const std::string& configFile, ndn::KeyChain& keyChain) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 51 | : m_configFile(configFile) |
| 52 | , m_keyChain(keyChain) |
| 53 | { |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 54 | if (s_instance != nullptr) { |
| 55 | BOOST_THROW_EXCEPTION(std::logic_error("RIB service cannot be instantiated more than once")); |
| 56 | } |
| 57 | if (&getGlobalIoService() != &getRibIoService()) { |
| 58 | BOOST_THROW_EXCEPTION(std::logic_error("RIB service must run on RIB thread")); |
| 59 | } |
| 60 | s_instance = this; |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame^] | 63 | Service::Service(const ConfigSection& configSection, ndn::KeyChain& keyChain) |
| 64 | : m_configSection(configSection) |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 65 | , m_keyChain(keyChain) |
| 66 | { |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 67 | if (s_instance != nullptr) { |
| 68 | BOOST_THROW_EXCEPTION(std::logic_error("RIB service cannot be instantiated more than once")); |
| 69 | } |
| 70 | if (&getGlobalIoService() != &getRibIoService()) { |
| 71 | BOOST_THROW_EXCEPTION(std::logic_error("RIB service must run on RIB thread")); |
| 72 | } |
| 73 | s_instance = this; |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Teng Liang | 04d5ce6 | 2018-08-06 10:20:24 +0800 | [diff] [blame] | 76 | Service::~Service() |
| 77 | { |
| 78 | s_instance = nullptr; |
| 79 | } |
| 80 | |
| 81 | Service& |
| 82 | Service::get() |
| 83 | { |
| 84 | if (s_instance == nullptr) { |
| 85 | BOOST_THROW_EXCEPTION(std::logic_error("RIB service is not instantiated")); |
| 86 | } |
| 87 | if (&getGlobalIoService() != &getRibIoService()) { |
| 88 | BOOST_THROW_EXCEPTION(std::logic_error("Must get RIB service on RIB thread")); |
| 89 | } |
| 90 | return *s_instance; |
| 91 | } |
Alexander Afanasyev | c3ea5a7 | 2015-02-12 20:14:16 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 93 | void |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 94 | Service::initialize() |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 95 | { |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 96 | m_face = make_unique<ndn::Face>(getLocalNfdTransport(), getGlobalIoService(), m_keyChain); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame^] | 97 | m_nfdController = make_unique<ndn::nfd::Controller>(*m_face, m_keyChain); |
| 98 | m_fibUpdater = make_unique<FibUpdater>(m_rib, *m_nfdController); |
| 99 | m_prefixPropagator = make_unique<AutoPrefixPropagator>(*m_nfdController, m_keyChain, m_rib); |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 100 | m_dispatcher = make_unique<ndn::mgmt::Dispatcher>(*m_face, m_keyChain); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame^] | 101 | m_ribManager = make_unique<RibManager>(m_rib, *m_dispatcher, *m_face, *m_nfdController, *m_prefixPropagator); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 102 | |
| 103 | ConfigFile config([] (const std::string& filename, const std::string& sectionName, |
| 104 | const ConfigSection& section, bool isDryRun) { |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 105 | // Ignore sections belonging to NFD, but raise an error |
| 106 | // if we're missing a handler for a "rib" section. |
| 107 | if (sectionName == "rib") { |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 108 | ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun); |
| 109 | } |
| 110 | }); |
| 111 | m_ribManager->setConfigFile(config); |
| 112 | |
| 113 | // parse config file |
| 114 | if (!m_configFile.empty()) { |
| 115 | config.parse(m_configFile, true); |
| 116 | config.parse(m_configFile, false); |
| 117 | } |
| 118 | else { |
| 119 | config.parse(m_configSection, true, INTERNAL_CONFIG); |
| 120 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 121 | } |
| 122 | |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame^] | 123 | if (m_ribManager->wantAutoPrefixPropagator) { |
| 124 | m_prefixPropagator->enable(); |
| 125 | } |
| 126 | else { |
| 127 | m_prefixPropagator->disable(); |
| 128 | } |
| 129 | |
| 130 | if (m_ribManager->wantReadvertiseToNlsr && m_readvertiseNlsr == nullptr) { |
| 131 | m_readvertiseNlsr = make_unique<Readvertise>( |
| 132 | m_rib, |
| 133 | make_unique<ClientToNlsrReadvertisePolicy>(), |
| 134 | make_unique<NfdRibReadvertiseDestination>(*m_nfdController, READVERTISE_NLSR_PREFIX, m_rib)); |
| 135 | } |
| 136 | else if (!m_ribManager->wantReadvertiseToNlsr && m_readvertiseNlsr != nullptr) { |
| 137 | m_readvertiseNlsr.reset(); |
| 138 | } |
| 139 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 140 | m_ribManager->registerWithNfd(); |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 141 | m_ribManager->enableLocalFields(); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 144 | shared_ptr<ndn::Transport> |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 145 | Service::getLocalNfdTransport() |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 146 | { |
| 147 | ConfigSection config; |
| 148 | |
| 149 | if (!m_configFile.empty()) { |
| 150 | // Any format errors should have been caught already |
| 151 | // If error is thrown at this point, it is development error |
| 152 | boost::property_tree::read_info(m_configFile, config); |
| 153 | } |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 154 | else { |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 155 | config = m_configSection; |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 156 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 157 | |
| 158 | if (config.get_child_optional("face_system.unix")) { |
| 159 | // unix socket enabled |
| 160 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 161 | auto socketPath = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock"); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame^] | 162 | // default socketPath should be the same as in UnixStreamFactory::processConfig |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 163 | |
| 164 | return make_shared<ndn::UnixTransport>(socketPath); |
| 165 | } |
| 166 | else if (config.get_child_optional("face_system.tcp") && |
| 167 | config.get<std::string>("face_system.tcp.listen", "yes") == "yes") { |
| 168 | // tcp is enabled |
| 169 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 170 | auto port = config.get<std::string>("face_system.tcp.port", "6363"); |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame^] | 171 | // default port should be the same as in TcpFactory::processConfig |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 172 | |
| 173 | return make_shared<ndn::TcpTransport>("localhost", port); |
| 174 | } |
| 175 | else { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 176 | BOOST_THROW_EXCEPTION(Error("No transport is available to communicate with NFD")); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | } // namespace rib |
| 181 | } // namespace nfd |