service.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California,
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 
26 #include "service.hpp"
27 
28 #include "fib-updater.hpp"
33 
34 #include "common/global.hpp"
35 #include "common/logger.hpp"
36 
37 #include <boost/property_tree/info_parser.hpp>
38 #include <ndn-cxx/transport/tcp-transport.hpp>
39 #include <ndn-cxx/transport/unix-transport.hpp>
40 
41 namespace nfd::rib {
42 
43 NFD_LOG_INIT(RibService);
44 
45 const std::string CFG_RIB = "rib";
46 const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
47 const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
48 const std::string CFG_PA_VALIDATION = "prefix_announcement_validation";
49 const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate";
50 const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr";
51 const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
52 constexpr uint64_t PROPAGATE_DEFAULT_COST = 15;
53 constexpr time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s;
54 
55 static ConfigSection
56 loadConfigSectionFromFile(const std::string& filename)
57 {
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 // Look into NFD's config file and construct an appropriate transport to communicate with NFD.
65 static shared_ptr<ndn::Transport>
67 {
68  if (config.get_child_optional("face_system.unix")) {
69  // default socket path should be the same as in UnixStreamFactory::processConfig
70 #ifdef __linux__
71  auto path = config.get<std::string>("face_system.unix.path", "/run/nfd.sock");
72 #else
73  auto path = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
74 #endif // __linux__
75  return make_shared<ndn::UnixTransport>(path);
76  }
77  else if (config.get_child_optional("face_system.tcp") &&
78  config.get<std::string>("face_system.tcp.listen", "yes") == "yes") {
79  // default port should be the same as in TcpFactory::processConfig
80  auto port = config.get<std::string>("face_system.tcp.port", "6363");
81  return make_shared<ndn::TcpTransport>("localhost", port);
82  }
83  else {
84  NDN_THROW(ConfigFile::Error("No transport is available to communicate with NFD"));
85  }
86 }
87 
88 Service::Service(const std::string& configFile, ndn::KeyChain& keyChain)
90  [&configFile] (ConfigFile& config, bool isDryRun) {
91  config.parse(configFile, isDryRun);
92  })
93 {
94 }
95 
96 Service::Service(const ConfigSection& configSection, ndn::KeyChain& keyChain)
97  : Service(keyChain, makeLocalNfdTransport(configSection),
98  [&configSection] (ConfigFile& config, bool isDryRun) {
99  config.parse(configSection, isDryRun, "internal://nfd.conf");
100  })
101 {
102 }
103 
104 template<typename ConfigParseFunc>
105 Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport,
106  const ConfigParseFunc& configParse)
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_keyChain, m_nfdController, m_dispatcher)
113 {
114  if (s_instance != nullptr) {
115  NDN_THROW(std::logic_error("RIB service cannot be instantiated more than once"));
116  }
117  if (&getGlobalIoService() != &getRibIoService()) {
118  NDN_THROW(std::logic_error("RIB service must run on RIB thread"));
119  }
120  s_instance = this;
121 
122  ConfigFile config(ConfigFile::ignoreUnknownSection);
123  config.addSectionHandler(CFG_RIB, [this] (auto&&... args) {
124  processConfig(std::forward<decltype(args)>(args)...);
125  });
126  configParse(config, true);
127  configParse(config, false);
128 
129  m_ribManager.registerWithNfd();
130  m_ribManager.enableLocalFields();
131 }
132 
134 {
135  s_instance = nullptr;
136 }
137 
138 Service&
140 {
141  if (s_instance == nullptr) {
142  NDN_THROW(std::logic_error("RIB service is not instantiated"));
143  }
144  if (&getGlobalIoService() != &getRibIoService()) {
145  NDN_THROW(std::logic_error("Must get RIB service on RIB thread"));
146  }
147  return *s_instance;
148 }
149 
150 void
151 Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
152 {
153  if (isDryRun) {
154  checkConfig(section, filename);
155  }
156  else {
157  applyConfig(section, filename);
158  }
159 }
160 
161 void
162 Service::checkConfig(const ConfigSection& section, const std::string& filename)
163 {
164  bool hasLocalhop = false;
165  bool hasPropagate = false;
166 
167  for (const auto& item : section) {
168  const std::string& key = item.first;
169  const ConfigSection& value = item.second;
170  if (key == CFG_LOCALHOST_SECURITY || key == CFG_PA_VALIDATION) {
171  ndn::ValidatorConfig testValidator(m_face);
172  testValidator.load(value, filename);
173  }
174  else if (key == CFG_LOCALHOP_SECURITY) {
175  hasLocalhop = true;
176  ndn::ValidatorConfig testValidator(m_face);
177  testValidator.load(value, filename);
178  }
179  else if (key == CFG_PREFIX_PROPAGATE) {
180  hasPropagate = true;
181  // AutoPrefixPropagator does not support config dry-run
182  }
183  else if (key == CFG_READVERTISE_NLSR) {
185  }
186  else {
187  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_RIB + "." + key));
188  }
189  }
190 
191  if (hasLocalhop && hasPropagate) {
192  NDN_THROW(ConfigFile::Error(CFG_LOCALHOP_SECURITY + " and " + CFG_PREFIX_PROPAGATE +
193  " cannot be enabled at the same time"));
194  }
195 }
196 
197 void
198 Service::applyConfig(const ConfigSection& section, const std::string& filename)
199 {
200  bool wantPrefixPropagate = false;
201  bool wantReadvertiseNlsr = false;
202 
203  for (const auto& item : section) {
204  const std::string& key = item.first;
205  const ConfigSection& value = item.second;
206  if (key == CFG_LOCALHOST_SECURITY) {
207  m_ribManager.applyLocalhostConfig(value, filename);
208  }
209  else if (key == CFG_LOCALHOP_SECURITY) {
210  m_ribManager.enableLocalhop(value, filename);
211  }
212  else if (key == CFG_PA_VALIDATION) {
213  m_ribManager.applyPaConfig(value, filename);
214  }
215  else if (key == CFG_PREFIX_PROPAGATE) {
216  wantPrefixPropagate = true;
217 
218  if (!m_readvertisePropagation) {
219  NFD_LOG_DEBUG("Enabling automatic prefix propagation");
220 
221  auto cost = item.second.get_optional<uint64_t>("cost");
222  auto parameters = ndn::nfd::ControlParameters()
223  .setCost(cost.value_or(PROPAGATE_DEFAULT_COST))
224  .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
225 
226  auto timeout = item.second.get_optional<uint64_t>("timeout");
227  auto options = ndn::nfd::CommandOptions()
229  .setTimeout(timeout ? time::milliseconds(*timeout) : PROPAGATE_DEFAULT_TIMEOUT);
230 
231  m_readvertisePropagation = make_unique<Readvertise>(
232  m_rib,
233  make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
234  make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
235  }
236  }
237  else if (key == CFG_READVERTISE_NLSR) {
238  wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_RIB + "." + CFG_READVERTISE_NLSR);
239  }
240  else {
241  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_RIB + "." + key));
242  }
243  }
244 
245  if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) {
246  NFD_LOG_DEBUG("Disabling automatic prefix propagation");
247  m_readvertisePropagation.reset();
248  }
249 
250  if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
251  NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
252  auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
253  m_readvertiseNlsr = make_unique<Readvertise>(
254  m_rib,
255  make_unique<ClientToNlsrReadvertisePolicy>(),
256  make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
257  }
258  else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
259  NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
260  m_readvertiseNlsr.reset();
261  }
262 }
263 
264 } // namespace nfd::rib
Configuration file parsing utility.
Definition: config-file.hpp:63
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:51
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string &sectionName)
Parse a config option that can be either "yes" or "no".
Definition: config-file.cpp:60
void registerWithNfd()
Start accepting commands and dataset requests.
Definition: rib-manager.cpp:96
static const Name LOCALHOP_TOP_PREFIX
void applyLocalhostConfig(const ConfigSection &section, const std::string &filename)
Apply localhost_security configuration.
Definition: rib-manager.cpp:71
void enableLocalhop(const ConfigSection &section, const std::string &filename)
Apply localhop_security configuration and allow accepting commands on /localhop/nfd/rib prefix.
Definition: rib-manager.cpp:77
void enableLocalFields()
Enable NDNLP IncomingFaceId field in order to support self-registration commands.
void applyPaConfig(const ConfigSection &section, const std::string &filename)
Apply prefix_announcement_validation configuration.
Definition: rib-manager.cpp:90
Initializes and executes the NFD-RIB service thread.
Definition: service.hpp:52
Service(const std::string &configFile, ndn::KeyChain &keyChain)
Create NFD-RIB service.
Definition: service.cpp:88
static Service & get()
Get a reference to the only instance of this class.
Definition: service.cpp:139
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
const std::string CFG_PA_VALIDATION
Definition: service.cpp:48
const std::string CFG_LOCALHOST_SECURITY
Definition: service.cpp:46
const Name READVERTISE_NLSR_PREFIX
Definition: service.cpp:51
static shared_ptr< ndn::Transport > makeLocalNfdTransport(const ConfigSection &config)
Definition: service.cpp:66
const std::string CFG_RIB
Definition: service.cpp:45
const std::string CFG_PREFIX_PROPAGATE
Definition: service.cpp:49
const std::string CFG_LOCALHOP_SECURITY
Definition: service.cpp:47
constexpr uint64_t PROPAGATE_DEFAULT_COST
Definition: service.cpp:52
const std::string CFG_READVERTISE_NLSR
Definition: service.cpp:50
constexpr time::milliseconds PROPAGATE_DEFAULT_TIMEOUT
Definition: service.cpp:53
static ConfigSection loadConfigSectionFromFile(const std::string &filename)
Definition: service.cpp:56
boost::asio::io_service & getRibIoService()
Definition: global.cpp:70
boost::property_tree::ptree ConfigSection
A configuration file section.
Definition: config-file.hpp:38
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:36