NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
main.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2017, 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 "nfd.hpp"
27 #include "rib/service.hpp"
28 
29 #include "core/version.hpp"
30 #include "core/global-io.hpp"
31 #include "core/logger.hpp"
34 
35 #include <string.h>
36 
37 #include <boost/filesystem.hpp>
38 #include <boost/program_options/options_description.hpp>
39 #include <boost/program_options/variables_map.hpp>
40 #include <boost/program_options/parsers.hpp>
41 
42 // boost::thread is used instead of std::thread to guarantee proper cleanup of thread local storage,
43 // see http://www.boost.org/doc/libs/1_54_0/doc/html/thread/thread_local_storage.html
44 #include <boost/thread.hpp>
45 
46 #include <atomic>
47 #include <condition_variable>
48 #include <iostream>
49 
50 namespace nfd {
51 
52 NFD_LOG_INIT("NFD");
53 
63 class NfdRunner : noncopyable
64 {
65 public:
66  explicit
67  NfdRunner(const std::string& configFile)
68  : m_nfd(configFile, m_nfdKeyChain)
69  , m_configFile(configFile)
70  , m_terminationSignalSet(getGlobalIoService())
71  , m_reloadSignalSet(getGlobalIoService())
72  {
73  m_terminationSignalSet.add(SIGINT);
74  m_terminationSignalSet.add(SIGTERM);
75  m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2));
76 
77  m_reloadSignalSet.add(SIGHUP);
78  m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
79  }
80 
81  static void
82  printUsage(std::ostream& os, const std::string& programName)
83  {
84  os << "Usage: \n"
85  << " " << programName << " [options]\n"
86  << "\n"
87  << "Run NFD forwarding daemon\n"
88  << "\n"
89  << "Options:\n"
90  << " [--help] - print this help message\n"
91  << " [--version] - print version and exit\n"
92  << " [--modules] - list available logging modules\n"
93  << " [--config /path/to/nfd.conf] - path to configuration file "
94  << "(default: " << DEFAULT_CONFIG_FILE << ")\n"
95  ;
96  }
97 
98  static void
99  printModules(std::ostream& os)
100  {
101  os << "Available logging modules: \n";
102 
103  for (const auto& module : LoggerFactory::getInstance().getModules()) {
104  os << module << "\n";
105  }
106  }
107 
108  void
110  {
111  m_nfd.initialize();
112  }
113 
114  int
115  run()
116  {
121  std::atomic_int retval(0);
122 
123  boost::asio::io_service* const mainIo = &getGlobalIoService();
124  boost::asio::io_service* ribIo = nullptr;
125 
126  // Mutex and conditional variable to implement synchronization between main and RIB manager
127  // threads:
128  // - to block main thread until RIB manager thread starts and initializes ribIo (to allow
129  // stopping it later)
130  std::mutex m;
131  std::condition_variable cv;
132 
133  std::string configFile = this->m_configFile; // c++11 lambda cannot capture member variables
134  boost::thread ribThread([configFile, &retval, &ribIo, mainIo, &cv, &m] {
135  {
136  std::lock_guard<std::mutex> lock(m);
137  ribIo = &getGlobalIoService();
138  BOOST_ASSERT(ribIo != mainIo);
139  }
140  cv.notify_all(); // notify that ribIo has been assigned
141 
142  try {
143  ndn::KeyChain ribKeyChain;
144  // must be created inside a separate thread
145  rib::Service ribService(configFile, ribKeyChain);
146  ribService.initialize();
147  getGlobalIoService().run(); // ribIo is not thread-safe to use here
148  }
149  catch (const std::exception& e) {
150  NFD_LOG_FATAL(e.what());
151  retval = 6;
152  mainIo->stop();
153  }
154 
155  {
156  std::lock_guard<std::mutex> lock(m);
157  ribIo = nullptr;
158  }
159  });
160 
161  {
162  // Wait to guarantee that ribIo is properly initialized, so it can be used to terminate
163  // RIB manager thread.
164  std::unique_lock<std::mutex> lock(m);
165  cv.wait(lock, [&ribIo] { return ribIo != nullptr; });
166  }
167 
168  try {
169  mainIo->run();
170  }
171  catch (const std::exception& e) {
173  retval = 4;
174  }
175  catch (const PrivilegeHelper::Error& e) {
176  NFD_LOG_FATAL(e.what());
177  retval = 5;
178  }
179 
180  {
181  // ribIo is guaranteed to be alive at this point
182  std::lock_guard<std::mutex> lock(m);
183  if (ribIo != nullptr) {
184  ribIo->stop();
185  ribIo = nullptr;
186  }
187  }
188  ribThread.join();
189 
190  return retval;
191  }
192 
193  void
194  terminate(const boost::system::error_code& error, int signalNo)
195  {
196  if (error)
197  return;
198 
199  NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting...");
200  getGlobalIoService().stop();
201  }
202 
203  void
204  reload(const boost::system::error_code& error, int signalNo)
205  {
206  if (error)
207  return;
208 
209  NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', reloading...");
210  m_nfd.reloadConfigFile();
211 
212  m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
213  }
214 
215 private:
216  ndn::KeyChain m_nfdKeyChain;
217  Nfd m_nfd;
218  std::string m_configFile;
219 
220  boost::asio::signal_set m_terminationSignalSet;
221  boost::asio::signal_set m_reloadSignalSet;
222 };
223 
224 } // namespace nfd
225 
226 int
227 main(int argc, char** argv)
228 {
229  using namespace nfd;
230 
231  namespace po = boost::program_options;
232 
233  po::options_description description;
234 
235  std::string configFile = DEFAULT_CONFIG_FILE;
236  description.add_options()
237  ("help,h", "print this help message")
238  ("version,V", "print version and exit")
239  ("modules,m", "list available logging modules")
240  ("config,c", po::value<std::string>(&configFile), "path to configuration file")
241  ;
242 
243  po::variables_map vm;
244  try {
245  po::store(po::command_line_parser(argc, argv).options(description).run(), vm);
246  po::notify(vm);
247  }
248  catch (const std::exception& e) {
249  // avoid NFD_LOG_FATAL to ensure that errors related to command-line parsing always appear on the
250  // terminal and are not littered with timestamps and other things added by the logging subsystem
251  std::cerr << "ERROR: " << e.what() << std::endl;
252  NfdRunner::printUsage(std::cerr, argv[0]);
253  return 1;
254  }
255 
256  if (vm.count("help") > 0) {
257  NfdRunner::printUsage(std::cout, argv[0]);
258  return 0;
259  }
260 
261  if (vm.count("version") > 0) {
262  std::cout << NFD_VERSION_BUILD_STRING << std::endl;
263  return 0;
264  }
265 
266  if (vm.count("modules") > 0) {
267  NfdRunner::printModules(std::cout);
268  return 0;
269  }
270 
271  NfdRunner runner(configFile);
272 
273  try {
274  runner.initialize();
275  }
276  catch (const boost::filesystem::filesystem_error& e) {
277  if (e.code() == boost::system::errc::permission_denied) {
278  NFD_LOG_FATAL("Permissions denied for " << e.path1() << ". " <<
279  argv[0] << " should be run as superuser");
280  return 3;
281  }
282  else {
284  return 2;
285  }
286  }
287  catch (const std::exception& e) {
289  return 2;
290  }
291  catch (const PrivilegeHelper::Error& e) {
292  // PrivilegeHelper::Errors do not inherit from std::exception
293  // and represent seteuid/gid failures
294 
295  NFD_LOG_FATAL(e.what());
296  return 3;
297  }
298 
299  return runner.run();
300 }
static void printUsage(std::ostream &os, const std::string &programName)
Definition: main.cpp:82
void initialize()
Definition: main.cpp:109
std::string getExtendedErrorMessage(const E &exception)
initializes and executes NFD-RIB service thread
Definition: service.hpp:45
represents a serious seteuid/gid failure
int main(int argc, char **argv)
Definition: main.cpp:227
static void printModules(std::ostream &os)
Definition: main.cpp:99
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
void initialize()
Perform initialization of NFD-RIB instance.
Definition: service.cpp:63
NfdRunner(const std::string &configFile)
Definition: main.cpp:67
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
void reload(const boost::system::error_code &error, int signalNo)
Definition: main.cpp:204
int run()
Definition: main.cpp:115
#define NFD_LOG_FATAL(expression)
Definition: logger.hpp:59
void reloadConfigFile()
Reload configuration file and apply update (if any)
Definition: nfd.cpp:174
Class representing NFD instance This class can be used to initialize all components of NFD...
Definition: nfd.hpp:60
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
void initialize()
Perform initialization of NFD instance After initialization, NFD instance can be started by invoking ...
Definition: nfd.cpp:73
Executes NFD with RIB manager.
Definition: main.cpp:63
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
ndn security v2 KeyChain
Definition: key-chain.cpp:76
const char * what() const
void terminate(const boost::system::error_code &error, int signalNo)
Definition: main.cpp:194