blob: f237a09be331c5df01c41254632f1cbfada6e3f7 [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 Pesavento97e33022019-02-14 16:00:50 -05003 * Copyright (c) 2014-2019, 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
Junxiao Shi98e29f42014-03-31 10:27:26 -070029#include "core/global-io.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080030#include "core/logger.hpp"
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060031#include "core/privilege-helper.hpp"
Davide Pesavento59769b12017-11-12 23:52:06 -050032#include "core/version.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080033
Davide Pesaventoa3148082018-04-12 18:21:54 -040034#include <string.h> // for strsignal()
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080035
Davide Pesavento47456e72018-02-25 16:21:53 -050036#include <boost/config.hpp>
Davide Pesavento97e33022019-02-14 16:00:50 -050037#include <boost/exception/diagnostic_information.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
Davide Pesavento774071c2018-11-15 21:33:23 -050057#ifdef HAVE_SYSTEMD
58#include <systemd/sd-daemon.h>
59#endif
Davide Pesavento47456e72018-02-25 16:21:53 -050060#ifdef HAVE_WEBSOCKET
61#include <websocketpp/version.hpp>
62#endif
63
Davide Pesavento59769b12017-11-12 23:52:06 -050064namespace po = boost::program_options;
Junxiao Shi09bf7c42014-01-31 11:10:25 -070065
Davide Pesaventoa3148082018-04-12 18:21:54 -040066NFD_LOG_INIT(Main);
Junxiao Shi09bf7c42014-01-31 11:10:25 -070067
Davide Pesavento59769b12017-11-12 23:52:06 -050068namespace nfd {
69
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080070/** \brief Executes NFD with RIB manager
71 *
72 * NFD (main forwarding procedure) and RIB manager execute in two different threads.
Junxiao Shi71d12142016-07-23 20:10:18 +000073 * Each thread has its own instances of global io_service and global scheduler.
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080074 *
75 * When either of the daemons fails, execution of non-failed daemon will be terminated as
76 * well. In other words, when NFD fails, RIB manager will be terminated; when RIB manager
77 * fails, NFD will be terminated.
78 */
Alexander Afanasyev31367922015-02-09 20:51:10 -080079class NfdRunner : noncopyable
Junxiao Shi09bf7c42014-01-31 11:10:25 -070080{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070081public:
Alexander Afanasyev5959b012014-06-02 19:18:12 +030082 explicit
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080083 NfdRunner(const std::string& configFile)
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080084 : m_nfd(configFile, m_nfdKeyChain)
85 , m_configFile(configFile)
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080086 , m_terminationSignalSet(getGlobalIoService())
87 , m_reloadSignalSet(getGlobalIoService())
Alexander Afanasyev5959b012014-06-02 19:18:12 +030088 {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080089 m_terminationSignalSet.add(SIGINT);
90 m_terminationSignalSet.add(SIGTERM);
91 m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +030092
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080093 m_reloadSignalSet.add(SIGHUP);
94 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070095 }
96
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070097 void
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080098 initialize()
99 {
100 m_nfd.initialize();
101 }
102
103 int
104 run()
105 {
Davide Pesavento59769b12017-11-12 23:52:06 -0500106 // Return value: a non-zero value is assigned when either NFD or RIB manager (running in
107 // a separate thread) fails.
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800108 std::atomic_int retval(0);
109
110 boost::asio::io_service* const mainIo = &getGlobalIoService();
Teng Liangf59e58f2018-09-07 16:41:54 -0700111 setMainIoService(mainIo);
Junxiao Shib2600172016-07-11 08:53:53 +0000112 boost::asio::io_service* ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800113
114 // Mutex and conditional variable to implement synchronization between main and RIB manager
115 // threads:
Junxiao Shib2600172016-07-11 08:53:53 +0000116 // - to block main thread until RIB manager thread starts and initializes ribIo (to allow
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800117 // stopping it later)
118 std::mutex m;
119 std::condition_variable cv;
120
121 std::string configFile = this->m_configFile; // c++11 lambda cannot capture member variables
Junxiao Shib2600172016-07-11 08:53:53 +0000122 boost::thread ribThread([configFile, &retval, &ribIo, mainIo, &cv, &m] {
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800123 {
124 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000125 ribIo = &getGlobalIoService();
126 BOOST_ASSERT(ribIo != mainIo);
Teng Liang952d6fd2018-05-29 21:09:52 -0700127 setRibIoService(ribIo);
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800128 }
Junxiao Shib2600172016-07-11 08:53:53 +0000129 cv.notify_all(); // notify that ribIo has been assigned
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800130
131 try {
Junxiao Shib2600172016-07-11 08:53:53 +0000132 ndn::KeyChain ribKeyChain;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800133 // must be created inside a separate thread
Junxiao Shib2600172016-07-11 08:53:53 +0000134 rib::Service ribService(configFile, ribKeyChain);
Junxiao Shib2600172016-07-11 08:53:53 +0000135 getGlobalIoService().run(); // ribIo is not thread-safe to use here
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800136 }
137 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500138 NFD_LOG_FATAL(boost::diagnostic_information(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500139 retval = 1;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800140 mainIo->stop();
141 }
142
143 {
144 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000145 ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800146 }
147 });
148
149 {
Junxiao Shib2600172016-07-11 08:53:53 +0000150 // Wait to guarantee that ribIo is properly initialized, so it can be used to terminate
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800151 // RIB manager thread.
152 std::unique_lock<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000153 cv.wait(lock, [&ribIo] { return ribIo != nullptr; });
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800154 }
155
156 try {
Davide Pesavento774071c2018-11-15 21:33:23 -0500157 systemdNotify("READY=1");
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800158 mainIo->run();
159 }
160 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500161 NFD_LOG_FATAL(boost::diagnostic_information(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500162 retval = 1;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800163 }
164 catch (const PrivilegeHelper::Error& e) {
165 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500166 retval = 4;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800167 }
168
169 {
Junxiao Shib2600172016-07-11 08:53:53 +0000170 // ribIo is guaranteed to be alive at this point
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800171 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000172 if (ribIo != nullptr) {
173 ribIo->stop();
174 ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800175 }
176 }
Junxiao Shib2600172016-07-11 08:53:53 +0000177 ribThread.join();
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800178
179 return retval;
180 }
181
Davide Pesavento774071c2018-11-15 21:33:23 -0500182 static void
183 systemdNotify(const char* state)
184 {
185#ifdef HAVE_SYSTEMD
186 sd_notify(0, state);
187#endif
188 }
189
190private:
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800191 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800192 terminate(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700193 {
194 if (error)
195 return;
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700196
Davide Pesavento97e33022019-02-14 16:00:50 -0500197 NFD_LOG_INFO("Caught signal " << signalNo << " (" << ::strsignal(signalNo) << "), exiting...");
Davide Pesavento774071c2018-11-15 21:33:23 -0500198
199 systemdNotify("STOPPING=1");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800200 getGlobalIoService().stop();
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700201 }
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600202
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300203 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800204 reload(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300205 {
206 if (error)
207 return;
208
Davide Pesavento97e33022019-02-14 16:00:50 -0500209 NFD_LOG_INFO("Caught signal " << signalNo << " (" << ::strsignal(signalNo) << "), reloading...");
Davide Pesavento774071c2018-11-15 21:33:23 -0500210
211 systemdNotify("RELOADING=1");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800212 m_nfd.reloadConfigFile();
Davide Pesavento774071c2018-11-15 21:33:23 -0500213 systemdNotify("READY=1");
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300214
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800215 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300216 }
217
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700218private:
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800219 ndn::KeyChain m_nfdKeyChain;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800220 Nfd m_nfd;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800221 std::string m_configFile;
222
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800223 boost::asio::signal_set m_terminationSignalSet;
224 boost::asio::signal_set m_reloadSignalSet;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700225};
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700226
Davide Pesavento59769b12017-11-12 23:52:06 -0500227static void
Davide Pesaventoa3148082018-04-12 18:21:54 -0400228printUsage(std::ostream& os, const char* programName, const po::options_description& opts)
Davide Pesavento59769b12017-11-12 23:52:06 -0500229{
230 os << "Usage: " << programName << " [options]\n"
Davide Pesaventoa3148082018-04-12 18:21:54 -0400231 << "\n"
Davide Pesavento59769b12017-11-12 23:52:06 -0500232 << "Run the NDN Forwarding Daemon (NFD)\n"
233 << "\n"
234 << opts;
235}
236
237static void
238printLogModules(std::ostream& os)
239{
Davide Pesaventoa3148082018-04-12 18:21:54 -0400240 const auto& modules = ndn::util::Logging::getLoggerNames();
241 std::copy(modules.begin(), modules.end(), ndn::make_ostream_joiner(os, "\n"));
242 os << std::endl;
Davide Pesavento59769b12017-11-12 23:52:06 -0500243}
244
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700245} // namespace nfd
246
247int
248main(int argc, char** argv)
249{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700250 using namespace nfd;
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700251
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800252 std::string configFile = DEFAULT_CONFIG_FILE;
Davide Pesavento59769b12017-11-12 23:52:06 -0500253
254 po::options_description description("Options");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800255 description.add_options()
Davide Pesavento59769b12017-11-12 23:52:06 -0500256 ("help,h", "print this message and exit")
257 ("version,V", "show version information and exit")
258 ("config,c", po::value<std::string>(&configFile),
259 "path to configuration file (default: " DEFAULT_CONFIG_FILE ")")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800260 ("modules,m", "list available logging modules")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800261 ;
262
263 po::variables_map vm;
264 try {
Davide Pesavento59769b12017-11-12 23:52:06 -0500265 po::store(po::parse_command_line(argc, argv, description), vm);
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700266 po::notify(vm);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800267 }
268 catch (const std::exception& e) {
Davide Pesaventoa3148082018-04-12 18:21:54 -0400269 // Cannot use NFD_LOG_* macros here, because the logging subsystem is not initialized yet
270 // at this point. Moreover, we don't want to clutter error messages related to command-line
271 // parsing with timestamps and other useless text added by the macros.
Davide Pesavento59769b12017-11-12 23:52:06 -0500272 std::cerr << "ERROR: " << e.what() << "\n\n";
273 printUsage(std::cerr, argv[0], description);
274 return 2;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700275 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700276
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800277 if (vm.count("help") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500278 printUsage(std::cout, argv[0], description);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700279 return 0;
280 }
281
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800282 if (vm.count("version") > 0) {
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700283 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
284 return 0;
285 }
286
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800287 if (vm.count("modules") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500288 printLogModules(std::cout);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600289 return 0;
290 }
291
Davide Pesavento47456e72018-02-25 16:21:53 -0500292 const std::string boostBuildInfo =
293 "with Boost version " + to_string(BOOST_VERSION / 100000) +
294 "." + to_string(BOOST_VERSION / 100 % 1000) +
295 "." + to_string(BOOST_VERSION % 100);
296 const std::string pcapBuildInfo =
297#ifdef HAVE_LIBPCAP
298 "with " + std::string(pcap_lib_version());
299#else
300 "without libpcap";
301#endif
302 const std::string wsBuildInfo =
303#ifdef HAVE_WEBSOCKET
304 "with WebSocket++ version " + to_string(websocketpp::major_version) +
305 "." + to_string(websocketpp::minor_version) +
306 "." + to_string(websocketpp::patch_version);
307#else
308 "without WebSocket++";
309#endif
310
Davide Pesaventoa3148082018-04-12 18:21:54 -0400311 std::clog << "NFD version " NFD_VERSION_BUILD_STRING " starting\n"
312 << "Built with " BOOST_COMPILER ", with " BOOST_STDLIB
Davide Pesavento47456e72018-02-25 16:21:53 -0500313 ", " << boostBuildInfo <<
314 ", " << pcapBuildInfo <<
315 ", " << wsBuildInfo <<
Davide Pesaventoa3148082018-04-12 18:21:54 -0400316 ", with ndn-cxx version " NDN_CXX_VERSION_BUILD_STRING
317 << std::endl;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600318
Davide Pesavento59769b12017-11-12 23:52:06 -0500319 NfdRunner runner(configFile);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700320 try {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800321 runner.initialize();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700322 }
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800323 catch (const boost::filesystem::filesystem_error& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500324 NFD_LOG_FATAL(boost::diagnostic_information(e));
325 return e.code() == boost::system::errc::permission_denied ? 4 : 1;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600326 }
327 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500328 NFD_LOG_FATAL(boost::diagnostic_information(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500329 return 1;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700330 }
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600331 catch (const PrivilegeHelper::Error& e) {
332 // PrivilegeHelper::Errors do not inherit from std::exception
333 // and represent seteuid/gid failures
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600334 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500335 return 4;
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600336 }
337
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800338 return runner.run();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700339}