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-2020, 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 {
42 namespace rib {
43 
44 NFD_LOG_INIT(RibService);
45 
46 Service* Service::s_instance = nullptr;
47 
48 const std::string CFG_SECTION = "rib";
49 const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
50 const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
51 const std::string CFG_PA_VALIDATION = "prefix_announcement_validation";
52 const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate";
53 const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr";
54 const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
55 const uint64_t PROPAGATE_DEFAULT_COST = 15;
56 const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s;
57 
58 static ConfigSection
59 loadConfigSectionFromFile(const std::string& filename)
60 {
61  ConfigSection config;
62  // Any format errors should have been caught already
63  boost::property_tree::read_info(filename, config);
64  return config;
65 }
66 
71 static shared_ptr<ndn::Transport>
73 {
74  if (config.get_child_optional("face_system.unix")) {
75  // default socket path should be the same as in UnixStreamFactory::processConfig
76 #ifdef __linux__
77  auto path = config.get<std::string>("face_system.unix.path", "/run/nfd.sock");
78 #else
79  auto path = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
80 #endif // __linux__
81  return make_shared<ndn::UnixTransport>(path);
82  }
83  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);
88  }
89  else {
90  NDN_THROW(ConfigFile::Error("No transport is available to communicate with NFD"));
91  }
92 }
93 
94 Service::Service(const std::string& configFile, ndn::KeyChain& keyChain)
96  [&configFile] (ConfigFile& config, bool isDryRun) {
97  config.parse(configFile, isDryRun);
98  })
99 {
100 }
101 
102 Service::Service(const ConfigSection& configSection, ndn::KeyChain& keyChain)
103  : Service(keyChain, makeLocalNfdTransport(configSection),
104  [&configSection] (ConfigFile& config, bool isDryRun) {
105  config.parse(configSection, isDryRun, "internal://nfd.conf");
106  })
107 {
108 }
109 
110 template<typename ConfigParseFunc>
111 Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport,
112  const ConfigParseFunc& configParse)
113  : 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)
118  , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
119 {
120  if (s_instance != nullptr) {
121  NDN_THROW(std::logic_error("RIB service cannot be instantiated more than once"));
122  }
123  if (&getGlobalIoService() != &getRibIoService()) {
124  NDN_THROW(std::logic_error("RIB service must run on RIB thread"));
125  }
126  s_instance = this;
127 
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();
135 }
136 
138 {
139  s_instance = nullptr;
140 }
141 
142 Service&
144 {
145  if (s_instance == nullptr) {
146  NDN_THROW(std::logic_error("RIB service is not instantiated"));
147  }
148  if (&getGlobalIoService() != &getRibIoService()) {
149  NDN_THROW(std::logic_error("Must get RIB service on RIB thread"));
150  }
151  return *s_instance;
152 }
153 
154 void
155 Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
156 {
157  if (isDryRun) {
158  checkConfig(section, filename);
159  }
160  else {
161  applyConfig(section, filename);
162  }
163 }
164 
165 void
166 Service::checkConfig(const ConfigSection& section, const std::string& filename)
167 {
168  bool hasLocalhop = false;
169  bool hasPropagate = false;
170 
171  for (const auto& item : section) {
172  const std::string& key = item.first;
173  const ConfigSection& value = item.second;
174  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;
180  ndn::ValidatorConfig testValidator(m_face);
181  testValidator.load(value, filename);
182  }
183  else if (key == CFG_PREFIX_PROPAGATE) {
184  hasPropagate = true;
185  // 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 {
191  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
192  }
193  }
194 
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  }
199 }
200 
201 void
202 Service::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  }
216  else if (key == CFG_PA_VALIDATION) {
217  m_ribManager.applyPaConfig(value, filename);
218  }
219  else if (key == CFG_PREFIX_PROPAGATE) {
220  wantPrefixPropagate = true;
221 
222  if (!m_readvertisePropagation) {
223  NFD_LOG_DEBUG("Enabling automatic prefix propagation");
224 
225  auto cost = item.second.get_optional<uint64_t>("cost");
226  auto parameters = ndn::nfd::ControlParameters()
227  .setCost(cost.value_or(PROPAGATE_DEFAULT_COST))
228  .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
229 
230  auto timeout = item.second.get_optional<uint64_t>("timeout");
231  auto options = ndn::nfd::CommandOptions()
233  .setTimeout(timeout ? time::milliseconds(*timeout) : PROPAGATE_DEFAULT_TIMEOUT);
234 
235  m_readvertisePropagation = make_unique<Readvertise>(
236  m_rib,
237  make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
238  make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
239  }
240  }
241  else if (key == CFG_READVERTISE_NLSR) {
242  wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
243  }
244  else {
245  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
246  }
247  }
248 
249  if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) {
250  NFD_LOG_DEBUG("Disabling automatic prefix propagation");
251  m_readvertisePropagation.reset();
252  }
253 
254  if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
255  NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
256  auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
257  m_readvertiseNlsr = make_unique<Readvertise>(
258  m_rib,
259  make_unique<ClientToNlsrReadvertisePolicy>(),
260  make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
261  }
262  else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
263  NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
264  m_readvertiseNlsr.reset();
265  }
266 }
267 
268 } // namespace rib
269 } // namespace nfd
const Name READVERTISE_NLSR_PREFIX
Definition: service.cpp:54
const std::string CFG_PA_VALIDATION
Definition: service.cpp:51
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:77
static Service & get()
Get a reference to the only instance of this class.
Definition: service.cpp:143
const std::string CFG_SECTION
Definition: service.cpp:48
const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT
Definition: service.cpp:56
configuration file parsing utility
Definition: config-file.hpp:57
static ConfigSection loadConfigSectionFromFile(const std::string &filename)
Definition: service.cpp:59
const std::string CFG_LOCALHOST_SECURITY
Definition: service.cpp:49
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:79
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
const std::string CFG_READVERTISE_NLSR
Definition: service.cpp:53
void applyLocalhostConfig(const ConfigSection &section, const std::string &filename)
Apply localhost_security configuration.
Definition: rib-manager.cpp:73
static shared_ptr< ndn::Transport > makeLocalNfdTransport(const ConfigSection &config)
Look into the config file and construct appropriate transport to communicate with NFD If NFD-RIB inst...
Definition: service.cpp:72
boost::asio::io_service & getRibIoService()
Definition: global.cpp:70
const uint64_t PROPAGATE_DEFAULT_COST
Definition: service.cpp:55
void registerWithNfd()
Start accepting commands and dataset requests.
Definition: rib-manager.cpp:98
Service(const std::string &configFile, ndn::KeyChain &keyChain)
create NFD-RIB service
Definition: service.cpp:94
boost::property_tree::ptree ConfigSection
a config file section
Definition: config-file.hpp:37
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
void applyPaConfig(const ConfigSection &section, const std::string &filename)
Apply prefix_announcement_validation configuration.
Definition: rib-manager.cpp:92
static const Name LOCALHOP_TOP_PREFIX
const std::string CFG_LOCALHOP_SECURITY
Definition: service.cpp:50
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
const std::string CFG_PREFIX_PROPAGATE
Definition: service.cpp:52
~Service()
Destructor.
Definition: service.cpp:137
void enableLocalFields()
Enable NDNLP IncomingFaceId field in order to support self-registration commands. ...
initializes and executes NFD-RIB service thread
Definition: service.hpp:52
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:51
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:36