blob: c549ac9f098c3f08b415a91271cb05e0c7bccc53 [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"
Davide Pesavento59769b12017-11-12 23:52:06 -050032#include "core/logger-factory.hpp"
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060033#include "core/privilege-helper.hpp"
Davide Pesavento59769b12017-11-12 23:52:06 -050034#include "core/version.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080035
36#include <string.h>
37
Davide Pesavento47456e72018-02-25 16:21:53 -050038#include <boost/config.hpp>
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080039#include <boost/filesystem.hpp>
40#include <boost/program_options/options_description.hpp>
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080041#include <boost/program_options/parsers.hpp>
Davide Pesavento59769b12017-11-12 23:52:06 -050042#include <boost/program_options/variables_map.hpp>
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080043// boost::thread is used instead of std::thread to guarantee proper cleanup of thread local storage,
Davide Pesaventocafae242016-04-22 02:21:35 +020044// see http://www.boost.org/doc/libs/1_54_0/doc/html/thread/thread_local_storage.html
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080045#include <boost/thread.hpp>
Davide Pesavento47456e72018-02-25 16:21:53 -050046#include <boost/version.hpp>
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080047
48#include <atomic>
49#include <condition_variable>
Davide Pesaventoa997d292017-08-24 20:16:59 -040050#include <iostream>
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080051
Davide Pesavento47456e72018-02-25 16:21:53 -050052#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
Davide Pesavento59769b12017-11-12 23:52:06 -050060namespace po = boost::program_options;
Junxiao Shi09bf7c42014-01-31 11:10:25 -070061
Davide Pesavento47456e72018-02-25 16:21:53 -050062NFD_LOG_INIT("Main");
Junxiao Shi09bf7c42014-01-31 11:10:25 -070063
Davide Pesavento59769b12017-11-12 23:52:06 -050064namespace nfd {
65
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080066/** \brief Executes NFD with RIB manager
67 *
68 * NFD (main forwarding procedure) and RIB manager execute in two different threads.
Junxiao Shi71d12142016-07-23 20:10:18 +000069 * Each thread has its own instances of global io_service and global scheduler.
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080070 *
71 * When either of the daemons fails, execution of non-failed daemon will be terminated as
72 * well. In other words, when NFD fails, RIB manager will be terminated; when RIB manager
73 * fails, NFD will be terminated.
74 */
Alexander Afanasyev31367922015-02-09 20:51:10 -080075class NfdRunner : noncopyable
Junxiao Shi09bf7c42014-01-31 11:10:25 -070076{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070077public:
Alexander Afanasyev5959b012014-06-02 19:18:12 +030078 explicit
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080079 NfdRunner(const std::string& configFile)
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080080 : m_nfd(configFile, m_nfdKeyChain)
81 , m_configFile(configFile)
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080082 , m_terminationSignalSet(getGlobalIoService())
83 , m_reloadSignalSet(getGlobalIoService())
Alexander Afanasyev5959b012014-06-02 19:18:12 +030084 {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080085 m_terminationSignalSet.add(SIGINT);
86 m_terminationSignalSet.add(SIGTERM);
87 m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +030088
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080089 m_reloadSignalSet.add(SIGHUP);
90 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070091 }
92
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070093 void
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080094 initialize()
95 {
96 m_nfd.initialize();
97 }
98
99 int
100 run()
101 {
Davide Pesavento59769b12017-11-12 23:52:06 -0500102 // Return value: a non-zero value is assigned when either NFD or RIB manager (running in
103 // a separate thread) fails.
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800104 std::atomic_int retval(0);
105
106 boost::asio::io_service* const mainIo = &getGlobalIoService();
Junxiao Shib2600172016-07-11 08:53:53 +0000107 boost::asio::io_service* ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800108
109 // Mutex and conditional variable to implement synchronization between main and RIB manager
110 // threads:
Junxiao Shib2600172016-07-11 08:53:53 +0000111 // - to block main thread until RIB manager thread starts and initializes ribIo (to allow
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800112 // 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
Junxiao Shib2600172016-07-11 08:53:53 +0000117 boost::thread ribThread([configFile, &retval, &ribIo, mainIo, &cv, &m] {
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800118 {
119 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000120 ribIo = &getGlobalIoService();
121 BOOST_ASSERT(ribIo != mainIo);
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800122 }
Junxiao Shib2600172016-07-11 08:53:53 +0000123 cv.notify_all(); // notify that ribIo has been assigned
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800124
125 try {
Junxiao Shib2600172016-07-11 08:53:53 +0000126 ndn::KeyChain ribKeyChain;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800127 // must be created inside a separate thread
Junxiao Shib2600172016-07-11 08:53:53 +0000128 rib::Service ribService(configFile, ribKeyChain);
129 ribService.initialize();
130 getGlobalIoService().run(); // ribIo is not thread-safe to use here
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800131 }
132 catch (const std::exception& e) {
133 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500134 retval = 1;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800135 mainIo->stop();
136 }
137
138 {
139 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000140 ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800141 }
142 });
143
144 {
Junxiao Shib2600172016-07-11 08:53:53 +0000145 // Wait to guarantee that ribIo is properly initialized, so it can be used to terminate
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800146 // RIB manager thread.
147 std::unique_lock<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000148 cv.wait(lock, [&ribIo] { return ribIo != nullptr; });
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800149 }
150
151 try {
152 mainIo->run();
153 }
154 catch (const std::exception& e) {
Junxiao Shi71d12142016-07-23 20:10:18 +0000155 NFD_LOG_FATAL(getExtendedErrorMessage(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500156 retval = 1;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800157 }
158 catch (const PrivilegeHelper::Error& e) {
159 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500160 retval = 4;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800161 }
162
163 {
Junxiao Shib2600172016-07-11 08:53:53 +0000164 // ribIo is guaranteed to be alive at this point
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800165 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000166 if (ribIo != nullptr) {
167 ribIo->stop();
168 ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800169 }
170 }
Junxiao Shib2600172016-07-11 08:53:53 +0000171 ribThread.join();
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800172
173 return retval;
174 }
175
176 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800177 terminate(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700178 {
179 if (error)
180 return;
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700181
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800182 NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting...");
183 getGlobalIoService().stop();
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700184 }
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600185
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300186 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800187 reload(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300188 {
189 if (error)
190 return;
191
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800192 NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', reloading...");
193 m_nfd.reloadConfigFile();
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300194
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800195 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300196 }
197
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700198private:
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800199 ndn::KeyChain m_nfdKeyChain;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800200 Nfd m_nfd;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800201 std::string m_configFile;
202
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800203 boost::asio::signal_set m_terminationSignalSet;
204 boost::asio::signal_set m_reloadSignalSet;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700205};
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700206
Davide Pesavento59769b12017-11-12 23:52:06 -0500207static void
208printUsage(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
217static void
218printLogModules(std::ostream& os)
219{
220 const auto& factory = LoggerFactory::getInstance();
221 for (const auto& module : factory.getModules()) {
222 os << module << "\n";
223 }
224}
225
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700226} // namespace nfd
227
228int
229main(int argc, char** argv)
230{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700231 using namespace nfd;
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700232
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800233 std::string configFile = DEFAULT_CONFIG_FILE;
Davide Pesavento59769b12017-11-12 23:52:06 -0500234
235 po::options_description description("Options");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800236 description.add_options()
Davide Pesavento59769b12017-11-12 23:52:06 -0500237 ("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 ")")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800241 ("modules,m", "list available logging modules")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800242 ;
243
244 po::variables_map vm;
245 try {
Davide Pesavento59769b12017-11-12 23:52:06 -0500246 po::store(po::parse_command_line(argc, argv, description), vm);
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700247 po::notify(vm);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800248 }
249 catch (const std::exception& e) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700250 // 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
Davide Pesavento59769b12017-11-12 23:52:06 -0500252 std::cerr << "ERROR: " << e.what() << "\n\n";
253 printUsage(std::cerr, argv[0], description);
254 return 2;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700255 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700256
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800257 if (vm.count("help") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500258 printUsage(std::cout, argv[0], description);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700259 return 0;
260 }
261
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800262 if (vm.count("version") > 0) {
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700263 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
264 return 0;
265 }
266
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800267 if (vm.count("modules") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500268 printLogModules(std::cout);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600269 return 0;
270 }
271
Davide Pesavento47456e72018-02-25 16:21:53 -0500272 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);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600298
Davide Pesavento59769b12017-11-12 23:52:06 -0500299 NfdRunner runner(configFile);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700300 try {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800301 runner.initialize();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700302 }
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800303 catch (const boost::filesystem::filesystem_error& e) {
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700304 if (e.code() == boost::system::errc::permission_denied) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500305 NFD_LOG_FATAL("Permission denied for " << e.path1() <<
306 ". This program should be run as superuser");
307 return 4;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700308 }
309 else {
Spyridon Mastorakisd374c402015-09-09 15:07:00 -0700310 NFD_LOG_FATAL(getExtendedErrorMessage(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500311 return 1;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700312 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600313 }
314 catch (const std::exception& e) {
Spyridon Mastorakisd374c402015-09-09 15:07:00 -0700315 NFD_LOG_FATAL(getExtendedErrorMessage(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500316 return 1;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700317 }
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600318 catch (const PrivilegeHelper::Error& e) {
319 // PrivilegeHelper::Errors do not inherit from std::exception
320 // and represent seteuid/gid failures
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600321 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500322 return 4;
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600323 }
324
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800325 return runner.run();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700326}