NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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-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 "nfd.hpp"
27 #include "rib/service.hpp"
28 
30 #include "core/global-io.hpp"
31 #include "core/logger.hpp"
32 #include "core/logger-factory.hpp"
34 #include "core/version.hpp"
35 
36 #include <string.h>
37 
38 #include <boost/config.hpp>
39 #include <boost/filesystem.hpp>
40 #include <boost/program_options/options_description.hpp>
41 #include <boost/program_options/parsers.hpp>
42 #include <boost/program_options/variables_map.hpp>
43 // boost::thread is used instead of std::thread to guarantee proper cleanup of thread local storage,
44 // see http://www.boost.org/doc/libs/1_54_0/doc/html/thread/thread_local_storage.html
45 #include <boost/thread.hpp>
46 #include <boost/version.hpp>
47 
48 #include <atomic>
49 #include <condition_variable>
50 #include <iostream>
51 
52 #include <ndn-cxx/version.hpp>
53 #ifdef HAVE_LIBPCAP
54 #include <pcap/pcap.h>
55 #endif
56 #ifdef HAVE_WEBSOCKET
57 #include <websocketpp/version.hpp>
58 #endif
59 
60 namespace po = boost::program_options;
61 
62 NFD_LOG_INIT("Main");
63 
64 namespace nfd {
65 
75 class NfdRunner : noncopyable
76 {
77 public:
78  explicit
79  NfdRunner(const std::string& configFile)
80  : m_nfd(configFile, m_nfdKeyChain)
81  , m_configFile(configFile)
82  , m_terminationSignalSet(getGlobalIoService())
83  , m_reloadSignalSet(getGlobalIoService())
84  {
85  m_terminationSignalSet.add(SIGINT);
86  m_terminationSignalSet.add(SIGTERM);
87  m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2));
88 
89  m_reloadSignalSet.add(SIGHUP);
90  m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
91  }
92 
93  void
95  {
96  m_nfd.initialize();
97  }
98 
99  int
100  run()
101  {
102  // Return value: a non-zero value is assigned when either NFD or RIB manager (running in
103  // a separate thread) fails.
104  std::atomic_int retval(0);
105 
106  boost::asio::io_service* const mainIo = &getGlobalIoService();
107  boost::asio::io_service* ribIo = nullptr;
108 
109  // Mutex and conditional variable to implement synchronization between main and RIB manager
110  // threads:
111  // - to block main thread until RIB manager thread starts and initializes ribIo (to allow
112  // stopping it later)
113  std::mutex m;
114  std::condition_variable cv;
115 
116  std::string configFile = this->m_configFile; // c++11 lambda cannot capture member variables
117  boost::thread ribThread([configFile, &retval, &ribIo, mainIo, &cv, &m] {
118  {
119  std::lock_guard<std::mutex> lock(m);
120  ribIo = &getGlobalIoService();
121  BOOST_ASSERT(ribIo != mainIo);
122  }
123  cv.notify_all(); // notify that ribIo has been assigned
124 
125  try {
126  ndn::KeyChain ribKeyChain;
127  // must be created inside a separate thread
128  rib::Service ribService(configFile, ribKeyChain);
129  ribService.initialize();
130  getGlobalIoService().run(); // ribIo is not thread-safe to use here
131  }
132  catch (const std::exception& e) {
133  NFD_LOG_FATAL(e.what());
134  retval = 1;
135  mainIo->stop();
136  }
137 
138  {
139  std::lock_guard<std::mutex> lock(m);
140  ribIo = nullptr;
141  }
142  });
143 
144  {
145  // Wait to guarantee that ribIo is properly initialized, so it can be used to terminate
146  // RIB manager thread.
147  std::unique_lock<std::mutex> lock(m);
148  cv.wait(lock, [&ribIo] { return ribIo != nullptr; });
149  }
150 
151  try {
152  mainIo->run();
153  }
154  catch (const std::exception& e) {
156  retval = 1;
157  }
158  catch (const PrivilegeHelper::Error& e) {
159  NFD_LOG_FATAL(e.what());
160  retval = 4;
161  }
162 
163  {
164  // ribIo is guaranteed to be alive at this point
165  std::lock_guard<std::mutex> lock(m);
166  if (ribIo != nullptr) {
167  ribIo->stop();
168  ribIo = nullptr;
169  }
170  }
171  ribThread.join();
172 
173  return retval;
174  }
175 
176  void
177  terminate(const boost::system::error_code& error, int signalNo)
178  {
179  if (error)
180  return;
181 
182  NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting...");
183  getGlobalIoService().stop();
184  }
185 
186  void
187  reload(const boost::system::error_code& error, int signalNo)
188  {
189  if (error)
190  return;
191 
192  NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', reloading...");
193  m_nfd.reloadConfigFile();
194 
195  m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
196  }
197 
198 private:
199  ndn::KeyChain m_nfdKeyChain;
200  Nfd m_nfd;
201  std::string m_configFile;
202 
203  boost::asio::signal_set m_terminationSignalSet;
204  boost::asio::signal_set m_reloadSignalSet;
205 };
206 
207 static void
208 printUsage(std::ostream& os, const char* programName,
209  const po::options_description& opts)
210 {
211  os << "Usage: " << programName << " [options]\n"
212  << "Run the NDN Forwarding Daemon (NFD)\n"
213  << "\n"
214  << opts;
215 }
216 
217 static void
218 printLogModules(std::ostream& os)
219 {
220  const auto& factory = LoggerFactory::getInstance();
221  for (const auto& module : factory.getModules()) {
222  os << module << "\n";
223  }
224 }
225 
226 } // namespace nfd
227 
228 int
229 main(int argc, char** argv)
230 {
231  using namespace nfd;
232 
233  std::string configFile = DEFAULT_CONFIG_FILE;
234 
235  po::options_description description("Options");
236  description.add_options()
237  ("help,h", "print this message and exit")
238  ("version,V", "show version information and exit")
239  ("config,c", po::value<std::string>(&configFile),
240  "path to configuration file (default: " DEFAULT_CONFIG_FILE ")")
241  ("modules,m", "list available logging modules")
242  ;
243 
244  po::variables_map vm;
245  try {
246  po::store(po::parse_command_line(argc, argv, description), vm);
247  po::notify(vm);
248  }
249  catch (const std::exception& e) {
250  // avoid NFD_LOG_FATAL to ensure that errors related to command-line parsing always appear on the
251  // terminal and are not littered with timestamps and other things added by the logging subsystem
252  std::cerr << "ERROR: " << e.what() << "\n\n";
253  printUsage(std::cerr, argv[0], description);
254  return 2;
255  }
256 
257  if (vm.count("help") > 0) {
258  printUsage(std::cout, argv[0], description);
259  return 0;
260  }
261 
262  if (vm.count("version") > 0) {
263  std::cout << NFD_VERSION_BUILD_STRING << std::endl;
264  return 0;
265  }
266 
267  if (vm.count("modules") > 0) {
268  printLogModules(std::cout);
269  return 0;
270  }
271 
272  const std::string boostBuildInfo =
273  "with Boost version " + to_string(BOOST_VERSION / 100000) +
274  "." + to_string(BOOST_VERSION / 100 % 1000) +
275  "." + to_string(BOOST_VERSION % 100);
276  const std::string pcapBuildInfo =
277 #ifdef HAVE_LIBPCAP
278  "with " + std::string(pcap_lib_version());
279 #else
280  "without libpcap";
281 #endif
282  const std::string wsBuildInfo =
283 #ifdef HAVE_WEBSOCKET
284  "with WebSocket++ version " + to_string(websocketpp::major_version) +
285  "." + to_string(websocketpp::minor_version) +
286  "." + to_string(websocketpp::patch_version);
287 #else
288  "without WebSocket++";
289 #endif
290 
291  NFD_LOG_INFO("NFD version " NFD_VERSION_BUILD_STRING " starting");
292  NFD_LOG_INFO("Built with " BOOST_COMPILER
293  ", with " BOOST_STDLIB
294  ", " << boostBuildInfo <<
295  ", " << pcapBuildInfo <<
296  ", " << wsBuildInfo <<
297  ", with ndn-cxx version " NDN_CXX_VERSION_BUILD_STRING);
298 
299  NfdRunner runner(configFile);
300  try {
301  runner.initialize();
302  }
303  catch (const boost::filesystem::filesystem_error& e) {
304  if (e.code() == boost::system::errc::permission_denied) {
305  NFD_LOG_FATAL("Permission denied for " << e.path1() <<
306  ". This program should be run as superuser");
307  return 4;
308  }
309  else {
311  return 1;
312  }
313  }
314  catch (const std::exception& e) {
316  return 1;
317  }
318  catch (const PrivilegeHelper::Error& e) {
319  // PrivilegeHelper::Errors do not inherit from std::exception
320  // and represent seteuid/gid failures
321  NFD_LOG_FATAL(e.what());
322  return 4;
323  }
324 
325  return runner.run();
326 }
void initialize()
Definition: main.cpp:94
std::string getExtendedErrorMessage(const E &exception)
static void printLogModules(std::ostream &os)
Definition: main.cpp:218
The interface of signing key management.
Definition: key-chain.hpp:46
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:229
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:79
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
void reload(const boost::system::error_code &error, int signalNo)
Definition: main.cpp:187
int run()
Definition: main.cpp:100
#define NFD_LOG_FATAL(expression)
Definition: logger.hpp:59
void reloadConfigFile()
Reload configuration file and apply update (if any)
Definition: nfd.cpp:177
Class representing NFD instance This class can be used to initialize all components of NFD...
Definition: nfd.hpp:63
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:74
Executes NFD with RIB manager.
Definition: main.cpp:75
static void printUsage(std::ostream &os, const char *programName, const po::options_description &opts)
Definition: main.cpp:208
std::string to_string(const V &v)
Definition: backports.hpp:107
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
void terminate(const boost::system::error_code &error, int signalNo)
Definition: main.cpp:177