blob: bfaba7320c3bb8b67a60605e115951bc8a2fa4cb [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 Pesaventoa9e1ab22023-10-02 22:10:45 -04003 * Copyright (c) 2014-2023, 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 Pesavento2cae8ca2019-04-18 20:48:05 -040029#include "common/global.hpp"
30#include "common/logger.hpp"
31#include "common/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 Pesaventoa9b09b62022-06-04 14:07:25 -040036#include <boost/asio/signal_set.hpp>
Davide Pesavento47456e72018-02-25 16:21:53 -050037#include <boost/config.hpp>
Davide Pesavento97e33022019-02-14 16:00:50 -050038#include <boost/exception/diagnostic_information.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>
Davide Pesavento47456e72018-02-25 16:21:53 -050043#include <boost/version.hpp>
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080044
45#include <atomic>
46#include <condition_variable>
Davide Pesaventoa997d292017-08-24 20:16:59 -040047#include <iostream>
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050048#include <thread>
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080049
Davide Pesaventoa3148082018-04-12 18:21:54 -040050#include <ndn-cxx/util/logging.hpp>
Davide Pesavento759db612019-08-31 22:56:28 -040051#include <ndn-cxx/util/ostream-joiner.hpp>
Davide Pesavento47456e72018-02-25 16:21:53 -050052#include <ndn-cxx/version.hpp>
Davide Pesaventoa3148082018-04-12 18:21:54 -040053
Davide Pesavento264af772021-02-09 21:48:24 -050054#ifdef NFD_HAVE_LIBPCAP
Davide Pesavento47456e72018-02-25 16:21:53 -050055#include <pcap/pcap.h>
56#endif
Davide Pesavento264af772021-02-09 21:48:24 -050057#ifdef NFD_HAVE_SYSTEMD
Davide Pesavento774071c2018-11-15 21:33:23 -050058#include <systemd/sd-daemon.h>
59#endif
Davide Pesavento264af772021-02-09 21:48:24 -050060#ifdef NFD_HAVE_WEBSOCKET
Davide Pesavento47456e72018-02-25 16:21:53 -050061#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.
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -040073 * Each thread has its own instances of global io_context 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)
Davide Pesavento412c9822021-07-02 00:21:05 -040086 , m_terminateSignals(getGlobalIoService(), SIGINT, SIGTERM)
87 , m_reloadSignals(getGlobalIoService(), SIGHUP)
Alexander Afanasyev5959b012014-06-02 19:18:12 +030088 {
Davide Pesavento412c9822021-07-02 00:21:05 -040089 m_terminateSignals.async_wait([this] (auto&&... args) {
90 terminate(std::forward<decltype(args)>(args)...);
91 });
92 m_reloadSignals.async_wait([this] (auto&&... args) {
93 reload(std::forward<decltype(args)>(args)...);
94 });
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
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400110 boost::asio::io_context* const mainIo = &getGlobalIoService();
Teng Liangf59e58f2018-09-07 16:41:54 -0700111 setMainIoService(mainIo);
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400112 boost::asio::io_context* 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
Davide Pesavento2bdf60c2019-02-19 18:23:45 -0500121 std::thread ribThread([configFile = m_configFile, &retval, &ribIo, mainIo, &cv, &m] {
122 {
123 std::lock_guard<std::mutex> lock(m);
124 ribIo = &getGlobalIoService();
125 BOOST_ASSERT(ribIo != mainIo);
126 setRibIoService(ribIo);
127 }
128 cv.notify_all(); // notify that ribIo has been assigned
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800129
Davide Pesavento2bdf60c2019-02-19 18:23:45 -0500130 try {
131 ndn::KeyChain ribKeyChain;
132 // must be created inside a separate thread
133 rib::Service ribService(configFile, ribKeyChain);
134 getGlobalIoService().run(); // ribIo is not thread-safe to use here
135 }
136 catch (const std::exception& e) {
137 NFD_LOG_FATAL(boost::diagnostic_information(e));
138 retval = 1;
139 mainIo->stop();
140 }
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800141
Davide Pesavento2bdf60c2019-02-19 18:23:45 -0500142 {
143 std::lock_guard<std::mutex> lock(m);
144 ribIo = nullptr;
145 }
146 });
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800147
148 {
Junxiao Shib2600172016-07-11 08:53:53 +0000149 // Wait to guarantee that ribIo is properly initialized, so it can be used to terminate
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800150 // RIB manager thread.
151 std::unique_lock<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000152 cv.wait(lock, [&ribIo] { return ribIo != nullptr; });
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800153 }
154
155 try {
Davide Pesavento774071c2018-11-15 21:33:23 -0500156 systemdNotify("READY=1");
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800157 mainIo->run();
158 }
159 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500160 NFD_LOG_FATAL(boost::diagnostic_information(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500161 retval = 1;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800162 }
163 catch (const PrivilegeHelper::Error& e) {
164 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500165 retval = 4;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800166 }
167
168 {
Junxiao Shib2600172016-07-11 08:53:53 +0000169 // ribIo is guaranteed to be alive at this point
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800170 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000171 if (ribIo != nullptr) {
172 ribIo->stop();
173 ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800174 }
175 }
Junxiao Shib2600172016-07-11 08:53:53 +0000176 ribThread.join();
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800177
178 return retval;
179 }
180
Davide Pesavento774071c2018-11-15 21:33:23 -0500181 static void
182 systemdNotify(const char* state)
183 {
Davide Pesavento264af772021-02-09 21:48:24 -0500184#ifdef NFD_HAVE_SYSTEMD
Davide Pesavento774071c2018-11-15 21:33:23 -0500185 sd_notify(0, state);
186#endif
187 }
188
189private:
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800190 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800191 terminate(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700192 {
193 if (error)
194 return;
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700195
Davide Pesavento97e33022019-02-14 16:00:50 -0500196 NFD_LOG_INFO("Caught signal " << signalNo << " (" << ::strsignal(signalNo) << "), exiting...");
Davide Pesavento774071c2018-11-15 21:33:23 -0500197
198 systemdNotify("STOPPING=1");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800199 getGlobalIoService().stop();
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700200 }
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600201
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300202 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800203 reload(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300204 {
205 if (error)
206 return;
207
Davide Pesavento97e33022019-02-14 16:00:50 -0500208 NFD_LOG_INFO("Caught signal " << signalNo << " (" << ::strsignal(signalNo) << "), reloading...");
Davide Pesavento774071c2018-11-15 21:33:23 -0500209
210 systemdNotify("RELOADING=1");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800211 m_nfd.reloadConfigFile();
Davide Pesavento774071c2018-11-15 21:33:23 -0500212 systemdNotify("READY=1");
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300213
Davide Pesavento412c9822021-07-02 00:21:05 -0400214 m_reloadSignals.async_wait([this] (auto&&... args) {
215 reload(std::forward<decltype(args)>(args)...);
216 });
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300217 }
218
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700219private:
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800220 ndn::KeyChain m_nfdKeyChain;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800221 Nfd m_nfd;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800222 std::string m_configFile;
223
Davide Pesavento412c9822021-07-02 00:21:05 -0400224 boost::asio::signal_set m_terminateSignals;
225 boost::asio::signal_set m_reloadSignals;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700226};
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700227
Davide Pesavento59769b12017-11-12 23:52:06 -0500228static void
Davide Pesaventoa3148082018-04-12 18:21:54 -0400229printUsage(std::ostream& os, const char* programName, const po::options_description& opts)
Davide Pesavento59769b12017-11-12 23:52:06 -0500230{
231 os << "Usage: " << programName << " [options]\n"
Davide Pesaventoa3148082018-04-12 18:21:54 -0400232 << "\n"
Davide Pesavento59769b12017-11-12 23:52:06 -0500233 << "Run the NDN Forwarding Daemon (NFD)\n"
234 << "\n"
235 << opts;
236}
237
238static void
239printLogModules(std::ostream& os)
240{
Davide Pesaventoa3148082018-04-12 18:21:54 -0400241 const auto& modules = ndn::util::Logging::getLoggerNames();
242 std::copy(modules.begin(), modules.end(), ndn::make_ostream_joiner(os, "\n"));
243 os << std::endl;
Davide Pesavento59769b12017-11-12 23:52:06 -0500244}
245
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700246} // namespace nfd
247
248int
249main(int argc, char** argv)
250{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700251 using namespace nfd;
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700252
Davide Pesavento264af772021-02-09 21:48:24 -0500253 std::string configFile = NFD_DEFAULT_CONFIG_FILE;
Davide Pesavento59769b12017-11-12 23:52:06 -0500254
255 po::options_description description("Options");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800256 description.add_options()
Davide Pesavento59769b12017-11-12 23:52:06 -0500257 ("help,h", "print this message and exit")
258 ("version,V", "show version information and exit")
259 ("config,c", po::value<std::string>(&configFile),
Davide Pesavento264af772021-02-09 21:48:24 -0500260 "path to configuration file (default: " NFD_DEFAULT_CONFIG_FILE ")")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800261 ("modules,m", "list available logging modules")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800262 ;
263
264 po::variables_map vm;
265 try {
Davide Pesavento59769b12017-11-12 23:52:06 -0500266 po::store(po::parse_command_line(argc, argv, description), vm);
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700267 po::notify(vm);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800268 }
269 catch (const std::exception& e) {
Davide Pesaventoa3148082018-04-12 18:21:54 -0400270 // Cannot use NFD_LOG_* macros here, because the logging subsystem is not initialized yet
271 // at this point. Moreover, we don't want to clutter error messages related to command-line
272 // parsing with timestamps and other useless text added by the macros.
Davide Pesavento59769b12017-11-12 23:52:06 -0500273 std::cerr << "ERROR: " << e.what() << "\n\n";
274 printUsage(std::cerr, argv[0], description);
275 return 2;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700276 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700277
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800278 if (vm.count("help") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500279 printUsage(std::cout, argv[0], description);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700280 return 0;
281 }
282
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800283 if (vm.count("version") > 0) {
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700284 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
285 return 0;
286 }
287
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800288 if (vm.count("modules") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500289 printLogModules(std::cout);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600290 return 0;
291 }
292
Davide Pesavento47456e72018-02-25 16:21:53 -0500293 const std::string boostBuildInfo =
294 "with Boost version " + to_string(BOOST_VERSION / 100000) +
295 "." + to_string(BOOST_VERSION / 100 % 1000) +
296 "." + to_string(BOOST_VERSION % 100);
297 const std::string pcapBuildInfo =
Davide Pesavento264af772021-02-09 21:48:24 -0500298#ifdef NFD_HAVE_LIBPCAP
Davide Pesavento47456e72018-02-25 16:21:53 -0500299 "with " + std::string(pcap_lib_version());
300#else
301 "without libpcap";
302#endif
303 const std::string wsBuildInfo =
Davide Pesavento264af772021-02-09 21:48:24 -0500304#ifdef NFD_HAVE_WEBSOCKET
Davide Pesavento47456e72018-02-25 16:21:53 -0500305 "with WebSocket++ version " + to_string(websocketpp::major_version) +
306 "." + to_string(websocketpp::minor_version) +
307 "." + to_string(websocketpp::patch_version);
308#else
309 "without WebSocket++";
310#endif
311
Davide Pesavento03f45d22019-04-04 12:34:26 -0400312 std::clog << "NFD version " << NFD_VERSION_BUILD_STRING << " starting\n"
Davide Pesaventoa3148082018-04-12 18:21:54 -0400313 << "Built with " BOOST_COMPILER ", with " BOOST_STDLIB
Davide Pesavento47456e72018-02-25 16:21:53 -0500314 ", " << boostBuildInfo <<
315 ", " << pcapBuildInfo <<
316 ", " << wsBuildInfo <<
Davide Pesaventoa3148082018-04-12 18:21:54 -0400317 ", with ndn-cxx version " NDN_CXX_VERSION_BUILD_STRING
318 << std::endl;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600319
Davide Pesavento59769b12017-11-12 23:52:06 -0500320 NfdRunner runner(configFile);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700321 try {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800322 runner.initialize();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700323 }
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800324 catch (const boost::filesystem::filesystem_error& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500325 NFD_LOG_FATAL(boost::diagnostic_information(e));
326 return e.code() == boost::system::errc::permission_denied ? 4 : 1;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600327 }
328 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500329 NFD_LOG_FATAL(boost::diagnostic_information(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500330 return 1;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700331 }
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600332 catch (const PrivilegeHelper::Error& e) {
333 // PrivilegeHelper::Errors do not inherit from std::exception
334 // and represent seteuid/gid failures
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600335 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500336 return 4;
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600337 }
338
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800339 return runner.run();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700340}