blob: d850ed24c7e9d75a3bee3a60fa23179361d24ab5 [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/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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
Davide Pesavento412c9822021-07-02 00:21:05 -040046const std::string CFG_RIB = "rib";
Wenkai Zheng6598fb02019-09-08 18:03:46 -070047const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
48const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
Teng Liang18c2b292019-10-18 14:31:04 -070049const std::string CFG_PA_VALIDATION = "prefix_announcement_validation";
Wenkai Zheng6598fb02019-09-08 18:03:46 -070050const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate";
51const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr";
52const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040053constexpr uint64_t PROPAGATE_DEFAULT_COST = 15;
54constexpr time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s;
Junxiao Shif4cfed12018-08-22 23:26:29 +000055
56static ConfigSection
57loadConfigSectionFromFile(const std::string& filename)
Alexander Afanasyev31367922015-02-09 20:51:10 -080058{
Junxiao Shif4cfed12018-08-22 23:26:29 +000059 ConfigSection config;
60 // Any format errors should have been caught already
61 boost::property_tree::read_info(filename, config);
62 return config;
63}
64
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040065// Look into NFD's config file and construct an appropriate transport to communicate with NFD.
Junxiao Shif4cfed12018-08-22 23:26:29 +000066static shared_ptr<ndn::Transport>
67makeLocalNfdTransport(const ConfigSection& config)
68{
69 if (config.get_child_optional("face_system.unix")) {
70 // default socket path should be the same as in UnixStreamFactory::processConfig
Eric Newberry22974902020-04-06 23:41:00 -070071#ifdef __linux__
72 auto path = config.get<std::string>("face_system.unix.path", "/run/nfd.sock");
73#else
Junxiao Shif4cfed12018-08-22 23:26:29 +000074 auto path = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
Eric Newberry22974902020-04-06 23:41:00 -070075#endif // __linux__
Junxiao Shif4cfed12018-08-22 23:26:29 +000076 return make_shared<ndn::UnixTransport>(path);
Teng Liang04d5ce62018-08-06 10:20:24 +080077 }
Junxiao Shif4cfed12018-08-22 23:26:29 +000078 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 Liang04d5ce62018-08-06 10:20:24 +080083 }
Junxiao Shif4cfed12018-08-22 23:26:29 +000084 else {
Davide Pesavento19779d82019-02-14 13:40:04 -050085 NDN_THROW(ConfigFile::Error("No transport is available to communicate with NFD"));
Junxiao Shif4cfed12018-08-22 23:26:29 +000086 }
87}
88
89Service::Service(const std::string& configFile, ndn::KeyChain& keyChain)
Ashlesh Gawandef84bac52018-08-27 07:39:31 +000090 : Service(keyChain, makeLocalNfdTransport(loadConfigSectionFromFile(configFile)),
91 [&configFile] (ConfigFile& config, bool isDryRun) {
92 config.parse(configFile, isDryRun);
93 })
Junxiao Shif4cfed12018-08-22 23:26:29 +000094{
Alexander Afanasyev31367922015-02-09 20:51:10 -080095}
96
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000097Service::Service(const ConfigSection& configSection, ndn::KeyChain& keyChain)
Ashlesh Gawandef84bac52018-08-27 07:39:31 +000098 : Service(keyChain, makeLocalNfdTransport(configSection),
99 [&configSection] (ConfigFile& config, bool isDryRun) {
100 config.parse(configSection, isDryRun, "internal://nfd.conf");
101 })
Junxiao Shif4cfed12018-08-22 23:26:29 +0000102{
Junxiao Shif4cfed12018-08-22 23:26:29 +0000103}
104
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000105template<typename ConfigParseFunc>
106Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport,
107 const ConfigParseFunc& configParse)
Junxiao Shif4cfed12018-08-22 23:26:29 +0000108 : 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 Pesavento0a71dd32019-03-17 20:36:18 -0400113 , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
Alexander Afanasyev31367922015-02-09 20:51:10 -0800114{
Teng Liang04d5ce62018-08-06 10:20:24 +0800115 if (s_instance != nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500116 NDN_THROW(std::logic_error("RIB service cannot be instantiated more than once"));
Teng Liang04d5ce62018-08-06 10:20:24 +0800117 }
118 if (&getGlobalIoService() != &getRibIoService()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500119 NDN_THROW(std::logic_error("RIB service must run on RIB thread"));
Teng Liang04d5ce62018-08-06 10:20:24 +0800120 }
121 s_instance = this;
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000122
123 ConfigFile config(ConfigFile::ignoreUnknownSection);
Davide Pesavento412c9822021-07-02 00:21:05 -0400124 config.addSectionHandler(CFG_RIB, [this] (auto&&... args) {
125 processConfig(std::forward<decltype(args)>(args)...);
126 });
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000127 configParse(config, true);
128 configParse(config, false);
129
130 m_ribManager.registerWithNfd();
131 m_ribManager.enableLocalFields();
Alexander Afanasyev31367922015-02-09 20:51:10 -0800132}
133
Teng Liang04d5ce62018-08-06 10:20:24 +0800134Service::~Service()
135{
136 s_instance = nullptr;
137}
138
139Service&
140Service::get()
141{
142 if (s_instance == nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500143 NDN_THROW(std::logic_error("RIB service is not instantiated"));
Teng Liang04d5ce62018-08-06 10:20:24 +0800144 }
145 if (&getGlobalIoService() != &getRibIoService()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500146 NDN_THROW(std::logic_error("Must get RIB service on RIB thread"));
Teng Liang04d5ce62018-08-06 10:20:24 +0800147 }
148 return *s_instance;
149}
Alexander Afanasyevc3ea5a72015-02-12 20:14:16 -0800150
Alexander Afanasyev31367922015-02-09 20:51:10 -0800151void
Junxiao Shif4cfed12018-08-22 23:26:29 +0000152Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
Alexander Afanasyev31367922015-02-09 20:51:10 -0800153{
Junxiao Shif4cfed12018-08-22 23:26:29 +0000154 if (isDryRun) {
155 checkConfig(section, filename);
156 }
157 else {
158 applyConfig(section, filename);
159 }
160}
Alexander Afanasyev31367922015-02-09 20:51:10 -0800161
Junxiao Shif4cfed12018-08-22 23:26:29 +0000162void
163Service::checkConfig(const ConfigSection& section, const std::string& filename)
164{
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700165 bool hasLocalhop = false;
166 bool hasPropagate = false;
167
Junxiao Shif4cfed12018-08-22 23:26:29 +0000168 for (const auto& item : section) {
169 const std::string& key = item.first;
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000170 const ConfigSection& value = item.second;
Jongseok Lee854866f2020-03-10 19:16:25 -0400171 if (key == CFG_LOCALHOST_SECURITY || key == CFG_PA_VALIDATION) {
172 ndn::ValidatorConfig testValidator(m_face);
173 testValidator.load(value, filename);
174 }
175 else if (key == CFG_LOCALHOP_SECURITY) {
176 hasLocalhop = true;
Alexander Afanasyev3c0c0352018-10-17 11:59:23 -0400177 ndn::ValidatorConfig testValidator(m_face);
178 testValidator.load(value, filename);
Junxiao Shif4cfed12018-08-22 23:26:29 +0000179 }
180 else if (key == CFG_PREFIX_PROPAGATE) {
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700181 hasPropagate = true;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000182 // AutoPrefixPropagator does not support config dry-run
183 }
184 else if (key == CFG_READVERTISE_NLSR) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400185 ConfigFile::parseYesNo(item, CFG_RIB + "." + CFG_READVERTISE_NLSR);
Junxiao Shif4cfed12018-08-22 23:26:29 +0000186 }
187 else {
Davide Pesavento412c9822021-07-02 00:21:05 -0400188 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_RIB + "." + key));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000189 }
190 }
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700191
192 if (hasLocalhop && hasPropagate) {
193 NDN_THROW(ConfigFile::Error(CFG_LOCALHOP_SECURITY + " and " + CFG_PREFIX_PROPAGATE +
194 " cannot be enabled at the same time"));
195 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000196}
197
198void
199Service::applyConfig(const ConfigSection& section, const std::string& filename)
200{
201 bool wantPrefixPropagate = false;
202 bool wantReadvertiseNlsr = false;
203
204 for (const auto& item : section) {
205 const std::string& key = item.first;
206 const ConfigSection& value = item.second;
207 if (key == CFG_LOCALHOST_SECURITY) {
208 m_ribManager.applyLocalhostConfig(value, filename);
209 }
210 else if (key == CFG_LOCALHOP_SECURITY) {
211 m_ribManager.enableLocalhop(value, filename);
212 }
Teng Liang18c2b292019-10-18 14:31:04 -0700213 else if (key == CFG_PA_VALIDATION) {
214 m_ribManager.applyPaConfig(value, filename);
215 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000216 else if (key == CFG_PREFIX_PROPAGATE) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000217 wantPrefixPropagate = true;
Yanbiao Lif48d0802018-06-01 03:00:02 -0700218
219 if (!m_readvertisePropagation) {
220 NFD_LOG_DEBUG("Enabling automatic prefix propagation");
221
Yanbiao Lif48d0802018-06-01 03:00:02 -0700222 auto cost = item.second.get_optional<uint64_t>("cost");
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700223 auto parameters = ndn::nfd::ControlParameters()
224 .setCost(cost.value_or(PROPAGATE_DEFAULT_COST))
225 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
Yanbiao Lif48d0802018-06-01 03:00:02 -0700226
Yanbiao Lif48d0802018-06-01 03:00:02 -0700227 auto timeout = item.second.get_optional<uint64_t>("timeout");
Wenkai Zheng6598fb02019-09-08 18:03:46 -0700228 auto options = ndn::nfd::CommandOptions()
229 .setPrefix(RibManager::LOCALHOP_TOP_PREFIX)
230 .setTimeout(timeout ? time::milliseconds(*timeout) : PROPAGATE_DEFAULT_TIMEOUT);
Yanbiao Lif48d0802018-06-01 03:00:02 -0700231
232 m_readvertisePropagation = make_unique<Readvertise>(
233 m_rib,
Yanbiao Lif48d0802018-06-01 03:00:02 -0700234 make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
235 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
236 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000237 }
238 else if (key == CFG_READVERTISE_NLSR) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400239 wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_RIB + "." + CFG_READVERTISE_NLSR);
Junxiao Shif4cfed12018-08-22 23:26:29 +0000240 }
241 else {
Davide Pesavento412c9822021-07-02 00:21:05 -0400242 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_RIB + "." + key));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000243 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800244 }
245
Yanbiao Lif48d0802018-06-01 03:00:02 -0700246 if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) {
247 NFD_LOG_DEBUG("Disabling automatic prefix propagation");
248 m_readvertisePropagation.reset();
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000249 }
250
Junxiao Shif4cfed12018-08-22 23:26:29 +0000251 if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
252 NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
Yanbiao Lif48d0802018-06-01 03:00:02 -0700253 auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000254 m_readvertiseNlsr = make_unique<Readvertise>(
255 m_rib,
256 make_unique<ClientToNlsrReadvertisePolicy>(),
Yanbiao Lif48d0802018-06-01 03:00:02 -0700257 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000258 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000259 else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
260 NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000261 m_readvertiseNlsr.reset();
262 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800263}
264
Alexander Afanasyev31367922015-02-09 20:51:10 -0800265} // namespace rib
266} // namespace nfd