blob: 043b9805f6492c0e7473e7ce66096ca0f65adc4f [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 Pesavento19779d82019-02-14 13:40:04 -05003 * Copyright (c) 2014-2019, 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
Junxiao Shif4cfed12018-08-22 23:26:29 +000034#include "core/logger.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060035#include "daemon/global.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
Junxiao Shif4cfed12018-08-22 23:26:29 +000048static const std::string CFG_SECTION = "rib";
49static const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
50static const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
51static const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate";
52static const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr";
53static const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
Yanbiao Lif48d0802018-06-01 03:00:02 -070054static const uint64_t PROPAGATE_DEFAULT_COST = 15;
55static const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s;
Junxiao Shif4cfed12018-08-22 23:26:29 +000056
57static ConfigSection
58loadConfigSectionFromFile(const std::string& filename)
Alexander Afanasyev31367922015-02-09 20:51:10 -080059{
Junxiao Shif4cfed12018-08-22 23:26:29 +000060 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 */
70static shared_ptr<ndn::Transport>
71makeLocalNfdTransport(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 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);
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 Afanasyev31367922015-02-09 20:51:10 -0800130}
131
Teng Liang04d5ce62018-08-06 10:20:24 +0800132Service::~Service()
133{
134 s_instance = nullptr;
135}
136
137Service&
138Service::get()
139{
140 if (s_instance == nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500141 NDN_THROW(std::logic_error("RIB service is not instantiated"));
Teng Liang04d5ce62018-08-06 10:20:24 +0800142 }
143 if (&getGlobalIoService() != &getRibIoService()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500144 NDN_THROW(std::logic_error("Must get RIB service on RIB thread"));
Teng Liang04d5ce62018-08-06 10:20:24 +0800145 }
146 return *s_instance;
147}
Alexander Afanasyevc3ea5a72015-02-12 20:14:16 -0800148
Alexander Afanasyev31367922015-02-09 20:51:10 -0800149void
Junxiao Shif4cfed12018-08-22 23:26:29 +0000150Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
Alexander Afanasyev31367922015-02-09 20:51:10 -0800151{
Junxiao Shif4cfed12018-08-22 23:26:29 +0000152 if (isDryRun) {
153 checkConfig(section, filename);
154 }
155 else {
156 applyConfig(section, filename);
157 }
158}
Alexander Afanasyev31367922015-02-09 20:51:10 -0800159
Junxiao Shif4cfed12018-08-22 23:26:29 +0000160void
161Service::checkConfig(const ConfigSection& section, const std::string& filename)
162{
163 for (const auto& item : section) {
164 const std::string& key = item.first;
Ashlesh Gawandef84bac52018-08-27 07:39:31 +0000165 const ConfigSection& value = item.second;
Junxiao Shif4cfed12018-08-22 23:26:29 +0000166 if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY) {
Alexander Afanasyev3c0c0352018-10-17 11:59:23 -0400167 ndn::ValidatorConfig testValidator(m_face);
168 testValidator.load(value, filename);
Junxiao Shif4cfed12018-08-22 23:26:29 +0000169 }
170 else if (key == CFG_PREFIX_PROPAGATE) {
171 // AutoPrefixPropagator does not support config dry-run
172 }
173 else if (key == CFG_READVERTISE_NLSR) {
174 ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
175 }
176 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500177 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000178 }
179 }
180}
181
182void
183Service::applyConfig(const ConfigSection& section, const std::string& filename)
184{
185 bool wantPrefixPropagate = false;
186 bool wantReadvertiseNlsr = false;
187
188 for (const auto& item : section) {
189 const std::string& key = item.first;
190 const ConfigSection& value = item.second;
191 if (key == CFG_LOCALHOST_SECURITY) {
192 m_ribManager.applyLocalhostConfig(value, filename);
193 }
194 else if (key == CFG_LOCALHOP_SECURITY) {
195 m_ribManager.enableLocalhop(value, filename);
196 }
197 else if (key == CFG_PREFIX_PROPAGATE) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000198 wantPrefixPropagate = true;
Yanbiao Lif48d0802018-06-01 03:00:02 -0700199
200 if (!m_readvertisePropagation) {
201 NFD_LOG_DEBUG("Enabling automatic prefix propagation");
202
203 auto parameters = ndn::nfd::ControlParameters()
204 .setCost(PROPAGATE_DEFAULT_COST)
205 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
206 auto cost = item.second.get_optional<uint64_t>("cost");
207 if (cost) {
208 parameters.setCost(*cost);
209 }
210
211 auto options = ndn::nfd::CommandOptions()
212 .setPrefix(RibManager::LOCALHOP_TOP_PREFIX)
213 .setTimeout(PROPAGATE_DEFAULT_TIMEOUT);
214 auto timeout = item.second.get_optional<uint64_t>("timeout");
215 if (timeout) {
216 options.setTimeout(time::milliseconds(*timeout));
217 }
218
219 m_readvertisePropagation = make_unique<Readvertise>(
220 m_rib,
Yanbiao Lif48d0802018-06-01 03:00:02 -0700221 make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
222 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
223 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000224 }
225 else if (key == CFG_READVERTISE_NLSR) {
226 wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
227 }
228 else {
Davide Pesavento19779d82019-02-14 13:40:04 -0500229 NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000230 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800231 }
232
Yanbiao Lif48d0802018-06-01 03:00:02 -0700233 if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) {
234 NFD_LOG_DEBUG("Disabling automatic prefix propagation");
235 m_readvertisePropagation.reset();
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000236 }
237
Junxiao Shif4cfed12018-08-22 23:26:29 +0000238 if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
239 NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
Yanbiao Lif48d0802018-06-01 03:00:02 -0700240 auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000241 m_readvertiseNlsr = make_unique<Readvertise>(
242 m_rib,
243 make_unique<ClientToNlsrReadvertisePolicy>(),
Yanbiao Lif48d0802018-06-01 03:00:02 -0700244 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000245 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000246 else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
247 NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000248 m_readvertiseNlsr.reset();
249 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800250}
251
Alexander Afanasyev31367922015-02-09 20:51:10 -0800252} // namespace rib
253} // namespace nfd