blob: 596fa27851b70a5fab412e393f5c9c4f2d70ebfe [file] [log] [blame]
Alexander Afanasyev31367922015-02-09 20:51:10 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3148082018-04-12 18:21:54 -04002/*
Jongseok Lee854866f2020-03-10 19:16:25 -04003 * Copyright (c) 2014-2020, Regents of the University of California,
Alexander Afanasyev31367922015-02-09 20:51:10 -08004 * 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 Shib2600172016-07-11 08:53:53 +000026#include "service.hpp"
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000027
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000028#include "fib-updater.hpp"
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000029#include "readvertise/client-to-nlsr-readvertise-policy.hpp"
Yanbiao Lif48d0802018-06-01 03:00:02 -070030#include "readvertise/host-to-gateway-readvertise-policy.hpp"
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000031#include "readvertise/nfd-rib-readvertise-destination.hpp"
32#include "readvertise/readvertise.hpp"
33
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040034#include "common/global.hpp"
35#include "common/logger.hpp"
Alexander Afanasyev31367922015-02-09 20:51:10 -080036
37#include <boost/property_tree/info_parser.hpp>
Alexander Afanasyev31367922015-02-09 20:51:10 -080038#include <ndn-cxx/transport/tcp-transport.hpp>
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000039#include <ndn-cxx/transport/unix-transport.hpp>
Alexander Afanasyev31367922015-02-09 20:51:10 -080040
41namespace nfd {
42namespace rib {
43
Junxiao Shif4cfed12018-08-22 23:26:29 +000044NFD_LOG_INIT(RibService);
Alexander Afanasyev31367922015-02-09 20:51:10 -080045
Teng Liang04d5ce62018-08-06 10:20:24 +080046Service* Service::s_instance = nullptr;
47
Wenkai Zheng6598fb02019-09-08 18:03:46 -070048const std::string CFG_SECTION = "rib";
49const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
50const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
Teng Liang18c2b292019-10-18 14:31:04 -070051const std::string CFG_PA_VALIDATION = "prefix_announcement_validation";
Wenkai Zheng6598fb02019-09-08 18:03:46 -070052const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate";
53const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr";
54const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
55const uint64_t PROPAGATE_DEFAULT_COST = 15;
56const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s;
Junxiao Shif4cfed12018-08-22 23:26:29 +000057
58static ConfigSection
59loadConfigSectionFromFile(const std::string& filename)
Alexander Afanasyev31367922015-02-09 20:51:10 -080060{
Junxiao Shif4cfed12018-08-22 23:26:29 +000061 ConfigSection config;
62 // Any format errors should have been caught already
63 boost::property_tree::read_info(filename, config);
64 return config;
65}
66
67/**
68 * \brief Look into the config file and construct appropriate transport to communicate with NFD
69 * If NFD-RIB instance was initialized with config file, INFO format is assumed
70 */
71static shared_ptr<ndn::Transport>
72makeLocalNfdTransport(const ConfigSection& config)
73{
74 if (config.get_child_optional("face_system.unix")) {
75 // default socket path should be the same as in UnixStreamFactory::processConfig
Eric Newberry22974902020-04-06 23:41:00 -070076#ifdef __linux__
77 auto path = config.get<std::string>("face_system.unix.path", "/run/nfd.sock");
78#else
Junxiao Shif4cfed12018-08-22 23:26:29 +000079 auto path = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
Eric Newberry22974902020-04-06 23:41:00 -070080#endif // __linux__
Junxiao Shif4cfed12018-08-22 23:26:29 +000081 return make_shared<ndn::UnixTransport>(path);
Teng Liang04d5ce62018-08-06 10:20:24 +080082 }
Junxiao Shif4cfed12018-08-22 23:26:29 +000083 else if (config.get_child_optional("face_system.tcp") &&
84 config.get<std::string>("face_system.tcp.listen", "yes") == "yes") {
85 // default port should be the same as in TcpFactory::processConfig
86 auto port = config.get<std::string>("face_system.tcp.port", "6363");
87 return make_shared<ndn::TcpTransport>("localhost", port);
Teng Liang04d5ce62018-08-06 10:20:24 +080088 }
Junxiao Shif4cfed12018-08-22 23:26:29 +000089 else {
Davide Pesavento19779d82019-02-14 13:40:04 -050090 NDN_THROW(ConfigFile::Error("No transport is available to communicate with NFD"));
Junxiao Shif4cfed12018-08-22 23:26:29 +000091 }
92}
93
94Service::Service(const std::string& configFile, ndn::KeyChain& keyChain)
Ashlesh Gawandef84bac52018-08-27 07:39:31 +000095 : Service(keyChain, makeLocalNfdTransport(loadConfigSectionFromFile(configFile)),
96 [&configFile] (ConfigFile& config, bool isDryRun) {
97 config.parse(configFile, isDryRun);
98 })
Junxiao Shif4cfed12018-08-22 23:26:29 +000099{
Alexander Afanasyev31367922015-02-09 20:51:10 -0800100}
101
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000102Service::Service(const ConfigSection& configSection, ndn::KeyChain& keyChain)
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000103 : Service(keyChain, makeLocalNfdTransport(configSection),
104 [&configSection] (ConfigFile& config, bool isDryRun) {
105 config.parse(configSection, isDryRun, "internal://nfd.conf");
106 })
Junxiao Shif4cfed12018-08-22 23:26:29 +0000107{
Junxiao Shif4cfed12018-08-22 23:26:29 +0000108}
109
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000110template<typename ConfigParseFunc>
111Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport,
112 const ConfigParseFunc& configParse)
Junxiao Shif4cfed12018-08-22 23:26:29 +0000113 : m_keyChain(keyChain)
114 , m_face(std::move(localNfdTransport), getGlobalIoService(), m_keyChain)
115 , m_nfdController(m_face, m_keyChain)
116 , m_fibUpdater(m_rib, m_nfdController)
117 , m_dispatcher(m_face, m_keyChain)
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400118 , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
Alexander Afanasyev31367922015-02-09 20:51:10 -0800119{
Teng Liang04d5ce62018-08-06 10:20:24 +0800120 if (s_instance != nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500121 NDN_THROW(std::logic_error("RIB service cannot be instantiated more than once"));
Teng Liang04d5ce62018-08-06 10:20:24 +0800122 }
123 if (&getGlobalIoService() != &getRibIoService()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500124 NDN_THROW(std::logic_error("RIB service must run on RIB thread"));
Teng Liang04d5ce62018-08-06 10:20:24 +0800125 }
126 s_instance = this;
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000127
128 ConfigFile config(ConfigFile::ignoreUnknownSection);
129 config.addSectionHandler(CFG_SECTION, bind(&Service::processConfig, this, _1, _2, _3));
130 configParse(config, true);
131 configParse(config, false);
132
133 m_ribManager.registerWithNfd();
134 m_ribManager.enableLocalFields();
Alexander Afanasyev31367922015-02-09 20:51:10 -0800135}
136
Teng Liang04d5ce62018-08-06 10:20:24 +0800137Service::~Service()
138{
139 s_instance = nullptr;
140}
141
142Service&
143Service::get()
144{
145 if (s_instance == nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500146 NDN_THROW(std::logic_error("RIB service is not instantiated"));
Teng Liang04d5ce62018-08-06 10:20:24 +0800147 }
148 if (&getGlobalIoService() != &getRibIoService()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500149 NDN_THROW(std::logic_error("Must get RIB service on RIB thread"));
Teng Liang04d5ce62018-08-06 10:20:24 +0800150 }
151 return *s_instance;
152}
Alexander Afanasyevc3ea5a72015-02-12 20:14:16 -0800153
Alexander Afanasyev31367922015-02-09 20:51:10 -0800154void
Junxiao Shif4cfed12018-08-22 23:26:29 +0000155Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
Alexander Afanasyev31367922015-02-09 20:51:10 -0800156{
Junxiao Shif4cfed12018-08-22 23:26:29 +0000157 if (isDryRun) {
158 checkConfig(section, filename);
159 }
160 else {
161 applyConfig(section, filename);
162 }
163}
Alexander Afanasyev31367922015-02-09 20:51:10 -0800164
Junxiao Shif4cfed12018-08-22 23:26:29 +0000165void
166Service::checkConfig(const ConfigSection& section, const std::string& filename)
167{
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700168 bool hasLocalhop = false;
169 bool hasPropagate = false;
170
Junxiao Shif4cfed12018-08-22 23:26:29 +0000171 for (const auto& item : section) {
172 const std::string& key = item.first;
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000173 const ConfigSection& value = item.second;
Jongseok Lee854866f2020-03-10 19:16:25 -0400174 if (key == CFG_LOCALHOST_SECURITY || key == CFG_PA_VALIDATION) {
175 ndn::ValidatorConfig testValidator(m_face);
176 testValidator.load(value, filename);
177 }
178 else if (key == CFG_LOCALHOP_SECURITY) {
179 hasLocalhop = true;
Alexander Afanasyev3c0c0352018-10-17 11:59:23 -0400180 ndn::ValidatorConfig testValidator(m_face);
181 testValidator.load(value, filename);
Junxiao Shif4cfed12018-08-22 23:26:29 +0000182 }
183 else if (key == CFG_PREFIX_PROPAGATE) {
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700184 hasPropagate = true;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000185 // AutoPrefixPropagator does not support config dry-run
186 }
187 else if (key == CFG_READVERTISE_NLSR) {
188 ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
189 }
190 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500191 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000192 }
193 }
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700194
195 if (hasLocalhop && hasPropagate) {
196 NDN_THROW(ConfigFile::Error(CFG_LOCALHOP_SECURITY + " and " + CFG_PREFIX_PROPAGATE +
197 " cannot be enabled at the same time"));
198 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000199}
200
201void
202Service::applyConfig(const ConfigSection& section, const std::string& filename)
203{
204 bool wantPrefixPropagate = false;
205 bool wantReadvertiseNlsr = false;
206
207 for (const auto& item : section) {
208 const std::string& key = item.first;
209 const ConfigSection& value = item.second;
210 if (key == CFG_LOCALHOST_SECURITY) {
211 m_ribManager.applyLocalhostConfig(value, filename);
212 }
213 else if (key == CFG_LOCALHOP_SECURITY) {
214 m_ribManager.enableLocalhop(value, filename);
215 }
Teng Liang18c2b292019-10-18 14:31:04 -0700216 else if (key == CFG_PA_VALIDATION) {
217 m_ribManager.applyPaConfig(value, filename);
218 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000219 else if (key == CFG_PREFIX_PROPAGATE) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000220 wantPrefixPropagate = true;
Yanbiao Lif48d0802018-06-01 03:00:02 -0700221
222 if (!m_readvertisePropagation) {
223 NFD_LOG_DEBUG("Enabling automatic prefix propagation");
224
Yanbiao Lif48d0802018-06-01 03:00:02 -0700225 auto cost = item.second.get_optional<uint64_t>("cost");
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700226 auto parameters = ndn::nfd::ControlParameters()
227 .setCost(cost.value_or(PROPAGATE_DEFAULT_COST))
228 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
Yanbiao Lif48d0802018-06-01 03:00:02 -0700229
Yanbiao Lif48d0802018-06-01 03:00:02 -0700230 auto timeout = item.second.get_optional<uint64_t>("timeout");
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700231 auto options = ndn::nfd::CommandOptions()
232 .setPrefix(RibManager::LOCALHOP_TOP_PREFIX)
233 .setTimeout(timeout ? time::milliseconds(*timeout) : PROPAGATE_DEFAULT_TIMEOUT);
Yanbiao Lif48d0802018-06-01 03:00:02 -0700234
235 m_readvertisePropagation = make_unique<Readvertise>(
236 m_rib,
Yanbiao Lif48d0802018-06-01 03:00:02 -0700237 make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
238 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
239 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000240 }
241 else if (key == CFG_READVERTISE_NLSR) {
242 wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
243 }
244 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500245 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000246 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800247 }
248
Yanbiao Lif48d0802018-06-01 03:00:02 -0700249 if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) {
250 NFD_LOG_DEBUG("Disabling automatic prefix propagation");
251 m_readvertisePropagation.reset();
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000252 }
253
Junxiao Shif4cfed12018-08-22 23:26:29 +0000254 if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
255 NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
Yanbiao Lif48d0802018-06-01 03:00:02 -0700256 auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000257 m_readvertiseNlsr = make_unique<Readvertise>(
258 m_rib,
259 make_unique<ClientToNlsrReadvertisePolicy>(),
Yanbiao Lif48d0802018-06-01 03:00:02 -0700260 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000261 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000262 else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
263 NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000264 m_readvertiseNlsr.reset();
265 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800266}
267
Alexander Afanasyev31367922015-02-09 20:51:10 -0800268} // namespace rib
269} // namespace nfd