blob: 067562030e9da13c3a5a653adcb170d0e46a61db [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
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080029#include "core/logger.hpp"
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060030#include "core/privilege-helper.hpp"
Davide Pesavento59769b12017-11-12 23:52:06 -050031#include "core/version.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060032#include "daemon/global.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>
Davide Pesavento47456e72018-02-25 16:21:53 -050042#include <boost/version.hpp>
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080043
44#include <atomic>
45#include <condition_variable>
Davide Pesaventoa997d292017-08-24 20:16:59 -040046#include <iostream>
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050047#include <thread>
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080048
Davide Pesaventoa3148082018-04-12 18:21:54 -040049#include <ndn-cxx/util/logging.hpp>
Davide Pesavento47456e72018-02-25 16:21:53 -050050#include <ndn-cxx/version.hpp>
Davide Pesaventoa3148082018-04-12 18:21:54 -040051
Davide Pesavento47456e72018-02-25 16:21:53 -050052#ifdef HAVE_LIBPCAP
53#include <pcap/pcap.h>
54#endif
Davide Pesavento774071c2018-11-15 21:33:23 -050055#ifdef HAVE_SYSTEMD
56#include <systemd/sd-daemon.h>
57#endif
Davide Pesavento47456e72018-02-25 16:21:53 -050058#ifdef HAVE_WEBSOCKET
59#include <websocketpp/version.hpp>
60#endif
61
Davide Pesavento59769b12017-11-12 23:52:06 -050062namespace po = boost::program_options;
Junxiao Shi09bf7c42014-01-31 11:10:25 -070063
Davide Pesaventoa3148082018-04-12 18:21:54 -040064NFD_LOG_INIT(Main);
Junxiao Shi09bf7c42014-01-31 11:10:25 -070065
Davide Pesavento59769b12017-11-12 23:52:06 -050066namespace nfd {
67
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080068/** \brief Executes NFD with RIB manager
69 *
70 * NFD (main forwarding procedure) and RIB manager execute in two different threads.
Junxiao Shi71d12142016-07-23 20:10:18 +000071 * Each thread has its own instances of global io_service and global scheduler.
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080072 *
73 * When either of the daemons fails, execution of non-failed daemon will be terminated as
74 * well. In other words, when NFD fails, RIB manager will be terminated; when RIB manager
75 * fails, NFD will be terminated.
76 */
Alexander Afanasyev31367922015-02-09 20:51:10 -080077class NfdRunner : noncopyable
Junxiao Shi09bf7c42014-01-31 11:10:25 -070078{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070079public:
Alexander Afanasyev5959b012014-06-02 19:18:12 +030080 explicit
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080081 NfdRunner(const std::string& configFile)
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080082 : m_nfd(configFile, m_nfdKeyChain)
83 , m_configFile(configFile)
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080084 , m_terminationSignalSet(getGlobalIoService())
85 , m_reloadSignalSet(getGlobalIoService())
Alexander Afanasyev5959b012014-06-02 19:18:12 +030086 {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080087 m_terminationSignalSet.add(SIGINT);
88 m_terminationSignalSet.add(SIGTERM);
89 m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +030090
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080091 m_reloadSignalSet.add(SIGHUP);
92 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070093 }
94
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070095 void
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080096 initialize()
97 {
98 m_nfd.initialize();
99 }
100
101 int
102 run()
103 {
Davide Pesavento59769b12017-11-12 23:52:06 -0500104 // Return value: a non-zero value is assigned when either NFD or RIB manager (running in
105 // a separate thread) fails.
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800106 std::atomic_int retval(0);
107
108 boost::asio::io_service* const mainIo = &getGlobalIoService();
Teng Liangf59e58f2018-09-07 16:41:54 -0700109 setMainIoService(mainIo);
Junxiao Shib2600172016-07-11 08:53:53 +0000110 boost::asio::io_service* ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800111
112 // Mutex and conditional variable to implement synchronization between main and RIB manager
113 // threads:
Junxiao Shib2600172016-07-11 08:53:53 +0000114 // - to block main thread until RIB manager thread starts and initializes ribIo (to allow
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800115 // stopping it later)
116 std::mutex m;
117 std::condition_variable cv;
118
Davide Pesavento2bdf60c2019-02-19 18:23:45 -0500119 std::thread ribThread([configFile = m_configFile, &retval, &ribIo, mainIo, &cv, &m] {
120 {
121 std::lock_guard<std::mutex> lock(m);
122 ribIo = &getGlobalIoService();
123 BOOST_ASSERT(ribIo != mainIo);
124 setRibIoService(ribIo);
125 }
126 cv.notify_all(); // notify that ribIo has been assigned
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800127
Davide Pesavento2bdf60c2019-02-19 18:23:45 -0500128 try {
129 ndn::KeyChain ribKeyChain;
130 // must be created inside a separate thread
131 rib::Service ribService(configFile, ribKeyChain);
132 getGlobalIoService().run(); // ribIo is not thread-safe to use here
133 }
134 catch (const std::exception& e) {
135 NFD_LOG_FATAL(boost::diagnostic_information(e));
136 retval = 1;
137 mainIo->stop();
138 }
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800139
Davide Pesavento2bdf60c2019-02-19 18:23:45 -0500140 {
141 std::lock_guard<std::mutex> lock(m);
142 ribIo = nullptr;
143 }
144 });
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800145
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 {
Davide Pesavento774071c2018-11-15 21:33:23 -0500154 systemdNotify("READY=1");
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800155 mainIo->run();
156 }
157 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500158 NFD_LOG_FATAL(boost::diagnostic_information(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500159 retval = 1;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800160 }
161 catch (const PrivilegeHelper::Error& e) {
162 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500163 retval = 4;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800164 }
165
166 {
Junxiao Shib2600172016-07-11 08:53:53 +0000167 // ribIo is guaranteed to be alive at this point
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800168 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000169 if (ribIo != nullptr) {
170 ribIo->stop();
171 ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800172 }
173 }
Junxiao Shib2600172016-07-11 08:53:53 +0000174 ribThread.join();
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800175
176 return retval;
177 }
178
Davide Pesavento774071c2018-11-15 21:33:23 -0500179 static void
180 systemdNotify(const char* state)
181 {
182#ifdef HAVE_SYSTEMD
183 sd_notify(0, state);
184#endif
185 }
186
187private:
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800188 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800189 terminate(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700190 {
191 if (error)
192 return;
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700193
Davide Pesavento97e33022019-02-14 16:00:50 -0500194 NFD_LOG_INFO("Caught signal " << signalNo << " (" << ::strsignal(signalNo) << "), exiting...");
Davide Pesavento774071c2018-11-15 21:33:23 -0500195
196 systemdNotify("STOPPING=1");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800197 getGlobalIoService().stop();
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700198 }
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600199
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300200 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800201 reload(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300202 {
203 if (error)
204 return;
205
Davide Pesavento97e33022019-02-14 16:00:50 -0500206 NFD_LOG_INFO("Caught signal " << signalNo << " (" << ::strsignal(signalNo) << "), reloading...");
Davide Pesavento774071c2018-11-15 21:33:23 -0500207
208 systemdNotify("RELOADING=1");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800209 m_nfd.reloadConfigFile();
Davide Pesavento774071c2018-11-15 21:33:23 -0500210 systemdNotify("READY=1");
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300211
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800212 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300213 }
214
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700215private:
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800216 ndn::KeyChain m_nfdKeyChain;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800217 Nfd m_nfd;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800218 std::string m_configFile;
219
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800220 boost::asio::signal_set m_terminationSignalSet;
221 boost::asio::signal_set m_reloadSignalSet;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700222};
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700223
Davide Pesavento59769b12017-11-12 23:52:06 -0500224static void
Davide Pesaventoa3148082018-04-12 18:21:54 -0400225printUsage(std::ostream& os, const char* programName, const po::options_description& opts)
Davide Pesavento59769b12017-11-12 23:52:06 -0500226{
227 os << "Usage: " << programName << " [options]\n"
Davide Pesaventoa3148082018-04-12 18:21:54 -0400228 << "\n"
Davide Pesavento59769b12017-11-12 23:52:06 -0500229 << "Run the NDN Forwarding Daemon (NFD)\n"
230 << "\n"
231 << opts;
232}
233
234static void
235printLogModules(std::ostream& os)
236{
Davide Pesaventoa3148082018-04-12 18:21:54 -0400237 const auto& modules = ndn::util::Logging::getLoggerNames();
238 std::copy(modules.begin(), modules.end(), ndn::make_ostream_joiner(os, "\n"));
239 os << std::endl;
Davide Pesavento59769b12017-11-12 23:52:06 -0500240}
241
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700242} // namespace nfd
243
244int
245main(int argc, char** argv)
246{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700247 using namespace nfd;
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700248
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800249 std::string configFile = DEFAULT_CONFIG_FILE;
Davide Pesavento59769b12017-11-12 23:52:06 -0500250
251 po::options_description description("Options");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800252 description.add_options()
Davide Pesavento59769b12017-11-12 23:52:06 -0500253 ("help,h", "print this message and exit")
254 ("version,V", "show version information and exit")
255 ("config,c", po::value<std::string>(&configFile),
256 "path to configuration file (default: " DEFAULT_CONFIG_FILE ")")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800257 ("modules,m", "list available logging modules")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800258 ;
259
260 po::variables_map vm;
261 try {
Davide Pesavento59769b12017-11-12 23:52:06 -0500262 po::store(po::parse_command_line(argc, argv, description), vm);
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700263 po::notify(vm);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800264 }
265 catch (const std::exception& e) {
Davide Pesaventoa3148082018-04-12 18:21:54 -0400266 // Cannot use NFD_LOG_* macros here, because the logging subsystem is not initialized yet
267 // at this point. Moreover, we don't want to clutter error messages related to command-line
268 // parsing with timestamps and other useless text added by the macros.
Davide Pesavento59769b12017-11-12 23:52:06 -0500269 std::cerr << "ERROR: " << e.what() << "\n\n";
270 printUsage(std::cerr, argv[0], description);
271 return 2;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700272 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700273
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800274 if (vm.count("help") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500275 printUsage(std::cout, argv[0], description);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700276 return 0;
277 }
278
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800279 if (vm.count("version") > 0) {
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700280 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
281 return 0;
282 }
283
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800284 if (vm.count("modules") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500285 printLogModules(std::cout);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600286 return 0;
287 }
288
Davide Pesavento47456e72018-02-25 16:21:53 -0500289 const std::string boostBuildInfo =
290 "with Boost version " + to_string(BOOST_VERSION / 100000) +
291 "." + to_string(BOOST_VERSION / 100 % 1000) +
292 "." + to_string(BOOST_VERSION % 100);
293 const std::string pcapBuildInfo =
294#ifdef HAVE_LIBPCAP
295 "with " + std::string(pcap_lib_version());
296#else
297 "without libpcap";
298#endif
299 const std::string wsBuildInfo =
300#ifdef HAVE_WEBSOCKET
301 "with WebSocket++ version " + to_string(websocketpp::major_version) +
302 "." + to_string(websocketpp::minor_version) +
303 "." + to_string(websocketpp::patch_version);
304#else
305 "without WebSocket++";
306#endif
307
Davide Pesavento03f45d22019-04-04 12:34:26 -0400308 std::clog << "NFD version " << NFD_VERSION_BUILD_STRING << " starting\n"
Davide Pesaventoa3148082018-04-12 18:21:54 -0400309 << "Built with " BOOST_COMPILER ", with " BOOST_STDLIB
Davide Pesavento47456e72018-02-25 16:21:53 -0500310 ", " << boostBuildInfo <<
311 ", " << pcapBuildInfo <<
312 ", " << wsBuildInfo <<
Davide Pesaventoa3148082018-04-12 18:21:54 -0400313 ", with ndn-cxx version " NDN_CXX_VERSION_BUILD_STRING
314 << std::endl;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600315
Davide Pesavento59769b12017-11-12 23:52:06 -0500316 NfdRunner runner(configFile);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700317 try {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800318 runner.initialize();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700319 }
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800320 catch (const boost::filesystem::filesystem_error& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500321 NFD_LOG_FATAL(boost::diagnostic_information(e));
322 return e.code() == boost::system::errc::permission_denied ? 4 : 1;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600323 }
324 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500325 NFD_LOG_FATAL(boost::diagnostic_information(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500326 return 1;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700327 }
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600328 catch (const PrivilegeHelper::Error& e) {
329 // PrivilegeHelper::Errors do not inherit from std::exception
330 // and represent seteuid/gid failures
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600331 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500332 return 4;
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600333 }
334
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800335 return runner.run();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700336}