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-2018, 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 "core/global-io.hpp"
35 #include "core/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 static const std::string CFG_SECTION = "rib";
49 static const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
50 static const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
51 static const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate";
52 static const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr";
53 static const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
54 static const uint64_t PROPAGATE_DEFAULT_COST = 15;
55 static const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s;
56 
57 static ConfigSection
58 loadConfigSectionFromFile(const std::string& filename)
59 {
60  ConfigSection config;
61  // Any format errors should have been caught already
62  boost::property_tree::read_info(filename, config);
63  return config;
64 }
65 
70 static shared_ptr<ndn::Transport>
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);
77  }
78  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);
83  }
84  else {
85  BOOST_THROW_EXCEPTION(ConfigFile::Error("No transport is available to communicate with NFD"));
86  }
87 }
88 
89 Service::Service(const std::string& configFile, ndn::KeyChain& keyChain)
91  [&configFile] (ConfigFile& config, bool isDryRun) {
92  config.parse(configFile, isDryRun);
93  })
94 {
95 }
96 
97 Service::Service(const ConfigSection& configSection, ndn::KeyChain& keyChain)
98  : Service(keyChain, makeLocalNfdTransport(configSection),
99  [&configSection] (ConfigFile& config, bool isDryRun) {
100  config.parse(configSection, isDryRun, "internal://nfd.conf");
101  })
102 {
103 }
104 
105 template<typename ConfigParseFunc>
106 Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport,
107  const ConfigParseFunc& configParse)
108  : m_keyChain(keyChain)
109  , m_face(std::move(localNfdTransport), getGlobalIoService(), m_keyChain)
110  , m_scheduler(m_face.getIoService())
111  , m_nfdController(m_face, m_keyChain)
112  , m_fibUpdater(m_rib, m_nfdController)
113  , m_dispatcher(m_face, m_keyChain)
114  , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher, m_scheduler)
115 {
116  if (s_instance != nullptr) {
117  BOOST_THROW_EXCEPTION(std::logic_error("RIB service cannot be instantiated more than once"));
118  }
119  if (&getGlobalIoService() != &getRibIoService()) {
120  BOOST_THROW_EXCEPTION(std::logic_error("RIB service must run on RIB thread"));
121  }
122  s_instance = this;
123 
125  config.addSectionHandler(CFG_SECTION, bind(&Service::processConfig, this, _1, _2, _3));
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  BOOST_THROW_EXCEPTION(std::logic_error("RIB service is not instantiated"));
143  }
144  if (&getGlobalIoService() != &getRibIoService()) {
145  BOOST_THROW_EXCEPTION(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  for (const auto& item : section) {
165  const std::string& key = item.first;
166  const ConfigSection& value = item.second;
167  if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY) {
168  ndn::ValidatorConfig testValidator(m_face);
169  testValidator.load(value, filename);
170  }
171  else if (key == CFG_PREFIX_PROPAGATE) {
172  // AutoPrefixPropagator does not support config dry-run
173  }
174  else if (key == CFG_READVERTISE_NLSR) {
175  ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
176  }
177  else {
178  BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
179  }
180  }
181 }
182 
183 void
184 Service::applyConfig(const ConfigSection& section, const std::string& filename)
185 {
186  bool wantPrefixPropagate = false;
187  bool wantReadvertiseNlsr = false;
188 
189  for (const auto& item : section) {
190  const std::string& key = item.first;
191  const ConfigSection& value = item.second;
192  if (key == CFG_LOCALHOST_SECURITY) {
193  m_ribManager.applyLocalhostConfig(value, filename);
194  }
195  else if (key == CFG_LOCALHOP_SECURITY) {
196  m_ribManager.enableLocalhop(value, filename);
197  }
198  else if (key == CFG_PREFIX_PROPAGATE) {
199  wantPrefixPropagate = true;
200 
201  if (!m_readvertisePropagation) {
202  NFD_LOG_DEBUG("Enabling automatic prefix propagation");
203 
204  auto parameters = ndn::nfd::ControlParameters()
205  .setCost(PROPAGATE_DEFAULT_COST)
206  .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
207  auto cost = item.second.get_optional<uint64_t>("cost");
208  if (cost) {
209  parameters.setCost(*cost);
210  }
211 
212  auto options = ndn::nfd::CommandOptions()
214  .setTimeout(PROPAGATE_DEFAULT_TIMEOUT);
215  auto timeout = item.second.get_optional<uint64_t>("timeout");
216  if (timeout) {
217  options.setTimeout(time::milliseconds(*timeout));
218  }
219 
220  m_readvertisePropagation = make_unique<Readvertise>(
221  m_rib,
222  m_scheduler,
223  make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
224  make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
225  }
226  }
227  else if (key == CFG_READVERTISE_NLSR) {
228  wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
229  }
230  else {
231  BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
232  }
233  }
234 
235  if (!wantPrefixPropagate && m_readvertisePropagation != nullptr) {
236  NFD_LOG_DEBUG("Disabling automatic prefix propagation");
237  m_readvertisePropagation.reset();
238  }
239 
240  if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
241  NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
242  auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
243  m_readvertiseNlsr = make_unique<Readvertise>(
244  m_rib,
245  m_scheduler,
246  make_unique<ClientToNlsrReadvertisePolicy>(),
247  make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
248  }
249  else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
250  NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
251  m_readvertiseNlsr.reset();
252  }
253 }
254 
255 } // namespace rib
256 } // namespace nfd
static const Name READVERTISE_NLSR_PREFIX
Definition: service.cpp:53
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:76
static Service & get()
Get a reference to the only instance of this class.
Definition: service.cpp:139
static const std::string CFG_SECTION
Definition: service.cpp:48
static const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT
Definition: service.cpp:55
configuration file parsing utility
Definition: config-file.hpp:57
static const Name LOCALHOP_TOP_PREFIX
static ConfigSection loadConfigSectionFromFile(const std::string &filename)
Definition: service.cpp:58
static const std::string CFG_LOCALHOST_SECURITY
Definition: service.cpp:49
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:59
static const std::string CFG_READVERTISE_NLSR
Definition: service.cpp:52
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:78
void registerWithNfd()
Start accepting commands and dataset requests.
Definition: rib-manager.cpp:91
void applyLocalhostConfig(const ConfigSection &section, const std::string &filename)
Apply localhost_security configuration.
Definition: rib-manager.cpp:72
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:71
void enableLocalFields()
Enable NDNLP IncomingFaceId field in order to support self-registration commands. ...
boost::asio::io_service & getRibIoService()
Definition: global-io.cpp:77
static const uint64_t PROPAGATE_DEFAULT_COST
Definition: service.cpp:54
Service(const std::string &configFile, ndn::KeyChain &keyChain)
create NFD-RIB service
Definition: service.cpp:89
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
static 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
static const std::string CFG_PREFIX_PROPAGATE
Definition: service.cpp:51
~Service()
Destructor.
Definition: service.cpp:133
initializes and executes NFD-RIB service thread
Definition: service.hpp:51
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:50
boost::asio::io_service & getGlobalIoService()
Definition: global-io.cpp:42