blob: 056d3599695d4e453508adecb73c587b049176f1 [file] [log] [blame]
Alexander Afanasyev2aa39622014-01-22 11:51:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa997d292017-08-24 20:16:59 -04002/*
Davide Pesavento47456e72018-02-25 16:21:53 -05003 * Copyright (c) 2014-2018, Regents of the University of California,
Alexander Afanasyev31c781e2015-02-09 17:39:59 -08004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060024 */
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080025
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080026#include "nfd.hpp"
Junxiao Shib2600172016-07-11 08:53:53 +000027#include "rib/service.hpp"
Junxiao Shi98e29f42014-03-31 10:27:26 -070028
Davide Pesavento59769b12017-11-12 23:52:06 -050029#include "core/extended-error-message.hpp"
Junxiao Shi98e29f42014-03-31 10:27:26 -070030#include "core/global-io.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080031#include "core/logger.hpp"
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060032#include "core/privilege-helper.hpp"
Davide Pesavento59769b12017-11-12 23:52:06 -050033#include "core/version.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080034
Davide Pesaventoa3148082018-04-12 18:21:54 -040035#include <string.h> // for strsignal()
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080036
Davide Pesavento47456e72018-02-25 16:21:53 -050037#include <boost/config.hpp>
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080038#include <boost/filesystem.hpp>
39#include <boost/program_options/options_description.hpp>
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080040#include <boost/program_options/parsers.hpp>
Davide Pesavento59769b12017-11-12 23:52:06 -050041#include <boost/program_options/variables_map.hpp>
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080042// boost::thread is used instead of std::thread to guarantee proper cleanup of thread local storage,
Davide Pesavento5f35f642018-05-10 19:36:03 -040043// see https://www.boost.org/doc/libs/1_58_0/doc/html/thread/thread_local_storage.html
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080044#include <boost/thread.hpp>
Davide Pesavento47456e72018-02-25 16:21:53 -050045#include <boost/version.hpp>
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080046
47#include <atomic>
48#include <condition_variable>
Davide Pesaventoa997d292017-08-24 20:16:59 -040049#include <iostream>
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080050
Davide Pesaventoa3148082018-04-12 18:21:54 -040051#include <ndn-cxx/util/logging.hpp>
Davide Pesavento47456e72018-02-25 16:21:53 -050052#include <ndn-cxx/version.hpp>
Davide Pesaventoa3148082018-04-12 18:21:54 -040053
Davide Pesavento47456e72018-02-25 16:21:53 -050054#ifdef HAVE_LIBPCAP
55#include <pcap/pcap.h>
56#endif
57#ifdef HAVE_WEBSOCKET
58#include <websocketpp/version.hpp>
59#endif
60
Davide Pesavento59769b12017-11-12 23:52:06 -050061namespace po = boost::program_options;
Junxiao Shi09bf7c42014-01-31 11:10:25 -070062
Davide Pesaventoa3148082018-04-12 18:21:54 -040063NFD_LOG_INIT(Main);
Junxiao Shi09bf7c42014-01-31 11:10:25 -070064
Davide Pesavento59769b12017-11-12 23:52:06 -050065namespace nfd {
66
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080067/** \brief Executes NFD with RIB manager
68 *
69 * NFD (main forwarding procedure) and RIB manager execute in two different threads.
Junxiao Shi71d12142016-07-23 20:10:18 +000070 * Each thread has its own instances of global io_service and global scheduler.
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080071 *
72 * When either of the daemons fails, execution of non-failed daemon will be terminated as
73 * well. In other words, when NFD fails, RIB manager will be terminated; when RIB manager
74 * fails, NFD will be terminated.
75 */
Alexander Afanasyev31367922015-02-09 20:51:10 -080076class NfdRunner : noncopyable
Junxiao Shi09bf7c42014-01-31 11:10:25 -070077{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070078public:
Alexander Afanasyev5959b012014-06-02 19:18:12 +030079 explicit
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080080 NfdRunner(const std::string& configFile)
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080081 : m_nfd(configFile, m_nfdKeyChain)
82 , m_configFile(configFile)
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080083 , m_terminationSignalSet(getGlobalIoService())
84 , m_reloadSignalSet(getGlobalIoService())
Alexander Afanasyev5959b012014-06-02 19:18:12 +030085 {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080086 m_terminationSignalSet.add(SIGINT);
87 m_terminationSignalSet.add(SIGTERM);
88 m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +030089
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080090 m_reloadSignalSet.add(SIGHUP);
91 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070092 }
93
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070094 void
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080095 initialize()
96 {
97 m_nfd.initialize();
98 }
99
100 int
101 run()
102 {
Davide Pesavento59769b12017-11-12 23:52:06 -0500103 // Return value: a non-zero value is assigned when either NFD or RIB manager (running in
104 // a separate thread) fails.
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800105 std::atomic_int retval(0);
106
107 boost::asio::io_service* const mainIo = &getGlobalIoService();
Junxiao Shib2600172016-07-11 08:53:53 +0000108 boost::asio::io_service* ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800109
110 // Mutex and conditional variable to implement synchronization between main and RIB manager
111 // threads:
Junxiao Shib2600172016-07-11 08:53:53 +0000112 // - to block main thread until RIB manager thread starts and initializes ribIo (to allow
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800113 // stopping it later)
114 std::mutex m;
115 std::condition_variable cv;
116
117 std::string configFile = this->m_configFile; // c++11 lambda cannot capture member variables
Junxiao Shib2600172016-07-11 08:53:53 +0000118 boost::thread ribThread([configFile, &retval, &ribIo, mainIo, &cv, &m] {
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800119 {
120 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000121 ribIo = &getGlobalIoService();
122 BOOST_ASSERT(ribIo != mainIo);
Teng Liang952d6fd2018-05-29 21:09:52 -0700123 setRibIoService(ribIo);
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800124 }
Junxiao Shib2600172016-07-11 08:53:53 +0000125 cv.notify_all(); // notify that ribIo has been assigned
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800126
127 try {
Junxiao Shib2600172016-07-11 08:53:53 +0000128 ndn::KeyChain ribKeyChain;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800129 // must be created inside a separate thread
Junxiao Shib2600172016-07-11 08:53:53 +0000130 rib::Service ribService(configFile, ribKeyChain);
131 ribService.initialize();
132 getGlobalIoService().run(); // ribIo is not thread-safe to use here
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800133 }
134 catch (const std::exception& e) {
135 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500136 retval = 1;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800137 mainIo->stop();
138 }
139
140 {
141 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000142 ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800143 }
144 });
145
146 {
Junxiao Shib2600172016-07-11 08:53:53 +0000147 // Wait to guarantee that ribIo is properly initialized, so it can be used to terminate
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800148 // RIB manager thread.
149 std::unique_lock<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000150 cv.wait(lock, [&ribIo] { return ribIo != nullptr; });
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800151 }
152
153 try {
154 mainIo->run();
155 }
156 catch (const std::exception& e) {
Junxiao Shi71d12142016-07-23 20:10:18 +0000157 NFD_LOG_FATAL(getExtendedErrorMessage(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500158 retval = 1;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800159 }
160 catch (const PrivilegeHelper::Error& e) {
161 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500162 retval = 4;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800163 }
164
165 {
Junxiao Shib2600172016-07-11 08:53:53 +0000166 // ribIo is guaranteed to be alive at this point
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800167 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000168 if (ribIo != nullptr) {
169 ribIo->stop();
170 ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800171 }
172 }
Junxiao Shib2600172016-07-11 08:53:53 +0000173 ribThread.join();
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800174
175 return retval;
176 }
177
178 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800179 terminate(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700180 {
181 if (error)
182 return;
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700183
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800184 NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting...");
185 getGlobalIoService().stop();
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700186 }
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600187
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300188 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800189 reload(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300190 {
191 if (error)
192 return;
193
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800194 NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', reloading...");
195 m_nfd.reloadConfigFile();
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300196
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800197 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300198 }
199
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700200private:
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800201 ndn::KeyChain m_nfdKeyChain;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800202 Nfd m_nfd;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800203 std::string m_configFile;
204
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800205 boost::asio::signal_set m_terminationSignalSet;
206 boost::asio::signal_set m_reloadSignalSet;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700207};
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700208
Davide Pesavento59769b12017-11-12 23:52:06 -0500209static void
Davide Pesaventoa3148082018-04-12 18:21:54 -0400210printUsage(std::ostream& os, const char* programName, const po::options_description& opts)
Davide Pesavento59769b12017-11-12 23:52:06 -0500211{
212 os << "Usage: " << programName << " [options]\n"
Davide Pesaventoa3148082018-04-12 18:21:54 -0400213 << "\n"
Davide Pesavento59769b12017-11-12 23:52:06 -0500214 << "Run the NDN Forwarding Daemon (NFD)\n"
215 << "\n"
216 << opts;
217}
218
219static void
220printLogModules(std::ostream& os)
221{
Davide Pesaventoa3148082018-04-12 18:21:54 -0400222 const auto& modules = ndn::util::Logging::getLoggerNames();
223 std::copy(modules.begin(), modules.end(), ndn::make_ostream_joiner(os, "\n"));
224 os << std::endl;
Davide Pesavento59769b12017-11-12 23:52:06 -0500225}
226
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700227} // namespace nfd
228
229int
230main(int argc, char** argv)
231{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700232 using namespace nfd;
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700233
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800234 std::string configFile = DEFAULT_CONFIG_FILE;
Davide Pesavento59769b12017-11-12 23:52:06 -0500235
236 po::options_description description("Options");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800237 description.add_options()
Davide Pesavento59769b12017-11-12 23:52:06 -0500238 ("help,h", "print this message and exit")
239 ("version,V", "show version information and exit")
240 ("config,c", po::value<std::string>(&configFile),
241 "path to configuration file (default: " DEFAULT_CONFIG_FILE ")")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800242 ("modules,m", "list available logging modules")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800243 ;
244
245 po::variables_map vm;
246 try {
Davide Pesavento59769b12017-11-12 23:52:06 -0500247 po::store(po::parse_command_line(argc, argv, description), vm);
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700248 po::notify(vm);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800249 }
250 catch (const std::exception& e) {
Davide Pesaventoa3148082018-04-12 18:21:54 -0400251 // Cannot use NFD_LOG_* macros here, because the logging subsystem is not initialized yet
252 // at this point. Moreover, we don't want to clutter error messages related to command-line
253 // parsing with timestamps and other useless text added by the macros.
Davide Pesavento59769b12017-11-12 23:52:06 -0500254 std::cerr << "ERROR: " << e.what() << "\n\n";
255 printUsage(std::cerr, argv[0], description);
256 return 2;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700257 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700258
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800259 if (vm.count("help") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500260 printUsage(std::cout, argv[0], description);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700261 return 0;
262 }
263
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800264 if (vm.count("version") > 0) {
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700265 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
266 return 0;
267 }
268
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800269 if (vm.count("modules") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500270 printLogModules(std::cout);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600271 return 0;
272 }
273
Davide Pesavento47456e72018-02-25 16:21:53 -0500274 const std::string boostBuildInfo =
275 "with Boost version " + to_string(BOOST_VERSION / 100000) +
276 "." + to_string(BOOST_VERSION / 100 % 1000) +
277 "." + to_string(BOOST_VERSION % 100);
278 const std::string pcapBuildInfo =
279#ifdef HAVE_LIBPCAP
280 "with " + std::string(pcap_lib_version());
281#else
282 "without libpcap";
283#endif
284 const std::string wsBuildInfo =
285#ifdef HAVE_WEBSOCKET
286 "with WebSocket++ version " + to_string(websocketpp::major_version) +
287 "." + to_string(websocketpp::minor_version) +
288 "." + to_string(websocketpp::patch_version);
289#else
290 "without WebSocket++";
291#endif
292
Davide Pesaventoa3148082018-04-12 18:21:54 -0400293 std::clog << "NFD version " NFD_VERSION_BUILD_STRING " starting\n"
294 << "Built with " BOOST_COMPILER ", with " BOOST_STDLIB
Davide Pesavento47456e72018-02-25 16:21:53 -0500295 ", " << boostBuildInfo <<
296 ", " << pcapBuildInfo <<
297 ", " << wsBuildInfo <<
Davide Pesaventoa3148082018-04-12 18:21:54 -0400298 ", with ndn-cxx version " NDN_CXX_VERSION_BUILD_STRING
299 << std::endl;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600300
Davide Pesavento59769b12017-11-12 23:52:06 -0500301 NfdRunner runner(configFile);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700302 try {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800303 runner.initialize();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700304 }
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800305 catch (const boost::filesystem::filesystem_error& e) {
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700306 if (e.code() == boost::system::errc::permission_denied) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500307 NFD_LOG_FATAL("Permission denied for " << e.path1() <<
308 ". This program should be run as superuser");
309 return 4;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700310 }
311 else {
Spyridon Mastorakisd374c402015-09-09 15:07:00 -0700312 NFD_LOG_FATAL(getExtendedErrorMessage(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500313 return 1;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700314 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600315 }
316 catch (const std::exception& e) {
Spyridon Mastorakisd374c402015-09-09 15:07:00 -0700317 NFD_LOG_FATAL(getExtendedErrorMessage(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500318 return 1;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700319 }
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600320 catch (const PrivilegeHelper::Error& e) {
321 // PrivilegeHelper::Errors do not inherit from std::exception
322 // and represent seteuid/gid failures
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600323 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500324 return 4;
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600325 }
326
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800327 return runner.run();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700328}