blob: 60edb1ed38a16fe7ed62558ddeb0866a0f5ed27e [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 Pesavento4b1921f2024-01-12 20:25:45 -05003 * Copyright (c) 2014-2024, 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/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 Pesavento4b1921f2024-01-12 20:25:45 -050042#include <boost/system/system_error.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 Pesavento401d1a42024-12-19 21:10:22 -050048#include <system_error>
Davide Pesavento2bdf60c2019-02-19 18:23:45 -050049#include <thread>
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080050
Davide Pesaventoa3148082018-04-12 18:21:54 -040051#include <ndn-cxx/util/logging.hpp>
Davide Pesavento759db612019-08-31 22:56:28 -040052#include <ndn-cxx/util/ostream-joiner.hpp>
Davide Pesavento47456e72018-02-25 16:21:53 -050053#include <ndn-cxx/version.hpp>
Davide Pesaventoa3148082018-04-12 18:21:54 -040054
Davide Pesavento264af772021-02-09 21:48:24 -050055#ifdef NFD_HAVE_LIBPCAP
Davide Pesavento47456e72018-02-25 16:21:53 -050056#include <pcap/pcap.h>
57#endif
Davide Pesavento264af772021-02-09 21:48:24 -050058#ifdef NFD_HAVE_SYSTEMD
Davide Pesavento774071c2018-11-15 21:33:23 -050059#include <systemd/sd-daemon.h>
60#endif
Davide Pesavento264af772021-02-09 21:48:24 -050061#ifdef NFD_HAVE_WEBSOCKET
Davide Pesavento47456e72018-02-25 16:21:53 -050062#include <websocketpp/version.hpp>
63#endif
64
Davide Pesavento59769b12017-11-12 23:52:06 -050065namespace po = boost::program_options;
Junxiao Shi09bf7c42014-01-31 11:10:25 -070066
Davide Pesaventoa3148082018-04-12 18:21:54 -040067NFD_LOG_INIT(Main);
Junxiao Shi09bf7c42014-01-31 11:10:25 -070068
Davide Pesavento59769b12017-11-12 23:52:06 -050069namespace nfd {
70
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080071/** \brief Executes NFD with RIB manager
72 *
73 * NFD (main forwarding procedure) and RIB manager execute in two different threads.
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -040074 * Each thread has its own instances of global io_context and global scheduler.
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080075 *
76 * When either of the daemons fails, execution of non-failed daemon will be terminated as
77 * well. In other words, when NFD fails, RIB manager will be terminated; when RIB manager
78 * fails, NFD will be terminated.
79 */
Alexander Afanasyev31367922015-02-09 20:51:10 -080080class NfdRunner : noncopyable
Junxiao Shi09bf7c42014-01-31 11:10:25 -070081{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070082public:
Alexander Afanasyev5959b012014-06-02 19:18:12 +030083 explicit
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080084 NfdRunner(const std::string& configFile)
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080085 : m_nfd(configFile, m_nfdKeyChain)
86 , m_configFile(configFile)
Davide Pesavento412c9822021-07-02 00:21:05 -040087 , m_terminateSignals(getGlobalIoService(), SIGINT, SIGTERM)
88 , m_reloadSignals(getGlobalIoService(), SIGHUP)
Alexander Afanasyev5959b012014-06-02 19:18:12 +030089 {
Davide Pesavento412c9822021-07-02 00:21:05 -040090 m_terminateSignals.async_wait([this] (auto&&... args) {
91 terminate(std::forward<decltype(args)>(args)...);
92 });
93 m_reloadSignals.async_wait([this] (auto&&... args) {
94 reload(std::forward<decltype(args)>(args)...);
95 });
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070096 }
97
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070098 void
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080099 initialize()
100 {
101 m_nfd.initialize();
102 }
103
104 int
105 run()
106 {
Davide Pesavento59769b12017-11-12 23:52:06 -0500107 // Return value: a non-zero value is assigned when either NFD or RIB manager (running in
108 // a separate thread) fails.
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800109 std::atomic_int retval(0);
110
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400111 boost::asio::io_context* const mainIo = &getGlobalIoService();
Teng Liangf59e58f2018-09-07 16:41:54 -0700112 setMainIoService(mainIo);
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400113 boost::asio::io_context* ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800114
115 // Mutex and conditional variable to implement synchronization between main and RIB manager
116 // threads:
Junxiao Shib2600172016-07-11 08:53:53 +0000117 // - to block main thread until RIB manager thread starts and initializes ribIo (to allow
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800118 // stopping it later)
119 std::mutex m;
120 std::condition_variable cv;
121
Davide Pesavento2bdf60c2019-02-19 18:23:45 -0500122 std::thread ribThread([configFile = m_configFile, &retval, &ribIo, mainIo, &cv, &m] {
123 {
124 std::lock_guard<std::mutex> lock(m);
125 ribIo = &getGlobalIoService();
126 BOOST_ASSERT(ribIo != mainIo);
127 setRibIoService(ribIo);
128 }
129 cv.notify_all(); // notify that ribIo has been assigned
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800130
Davide Pesavento2bdf60c2019-02-19 18:23:45 -0500131 try {
132 ndn::KeyChain ribKeyChain;
133 // must be created inside a separate thread
134 rib::Service ribService(configFile, ribKeyChain);
135 getGlobalIoService().run(); // ribIo is not thread-safe to use here
136 }
137 catch (const std::exception& e) {
138 NFD_LOG_FATAL(boost::diagnostic_information(e));
139 retval = 1;
140 mainIo->stop();
141 }
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800142
Davide Pesavento2bdf60c2019-02-19 18:23:45 -0500143 {
144 std::lock_guard<std::mutex> lock(m);
145 ribIo = nullptr;
146 }
147 });
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800148
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 {
Davide Pesavento264af772021-02-09 21:48:24 -0500185#ifdef NFD_HAVE_SYSTEMD
Davide Pesavento774071c2018-11-15 21:33:23 -0500186 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
Davide Pesavento412c9822021-07-02 00:21:05 -0400215 m_reloadSignals.async_wait([this] (auto&&... args) {
216 reload(std::forward<decltype(args)>(args)...);
217 });
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300218 }
219
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700220private:
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800221 ndn::KeyChain m_nfdKeyChain;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800222 Nfd m_nfd;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800223 std::string m_configFile;
224
Davide Pesavento412c9822021-07-02 00:21:05 -0400225 boost::asio::signal_set m_terminateSignals;
226 boost::asio::signal_set m_reloadSignals;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700227};
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700228
Davide Pesavento59769b12017-11-12 23:52:06 -0500229static void
Davide Pesaventoa3148082018-04-12 18:21:54 -0400230printUsage(std::ostream& os, const char* programName, const po::options_description& opts)
Davide Pesavento59769b12017-11-12 23:52:06 -0500231{
232 os << "Usage: " << programName << " [options]\n"
Davide Pesaventoa3148082018-04-12 18:21:54 -0400233 << "\n"
Davide Pesavento59769b12017-11-12 23:52:06 -0500234 << "Run the NDN Forwarding Daemon (NFD)\n"
235 << "\n"
236 << opts;
237}
238
239static void
240printLogModules(std::ostream& os)
241{
Davide Pesaventoa3148082018-04-12 18:21:54 -0400242 const auto& modules = ndn::util::Logging::getLoggerNames();
243 std::copy(modules.begin(), modules.end(), ndn::make_ostream_joiner(os, "\n"));
244 os << std::endl;
Davide Pesavento59769b12017-11-12 23:52:06 -0500245}
246
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700247} // namespace nfd
248
249int
250main(int argc, char** argv)
251{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700252 using namespace nfd;
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700253
Davide Pesavento264af772021-02-09 21:48:24 -0500254 std::string configFile = NFD_DEFAULT_CONFIG_FILE;
Davide Pesavento59769b12017-11-12 23:52:06 -0500255
256 po::options_description description("Options");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800257 description.add_options()
Davide Pesavento59769b12017-11-12 23:52:06 -0500258 ("help,h", "print this message and exit")
259 ("version,V", "show version information and exit")
260 ("config,c", po::value<std::string>(&configFile),
Davide Pesavento264af772021-02-09 21:48:24 -0500261 "path to configuration file (default: " NFD_DEFAULT_CONFIG_FILE ")")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800262 ("modules,m", "list available logging modules")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800263 ;
264
265 po::variables_map vm;
266 try {
Davide Pesavento59769b12017-11-12 23:52:06 -0500267 po::store(po::parse_command_line(argc, argv, description), vm);
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700268 po::notify(vm);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800269 }
270 catch (const std::exception& e) {
Davide Pesaventoa3148082018-04-12 18:21:54 -0400271 // Cannot use NFD_LOG_* macros here, because the logging subsystem is not initialized yet
272 // at this point. Moreover, we don't want to clutter error messages related to command-line
273 // parsing with timestamps and other useless text added by the macros.
Davide Pesavento59769b12017-11-12 23:52:06 -0500274 std::cerr << "ERROR: " << e.what() << "\n\n";
275 printUsage(std::cerr, argv[0], description);
276 return 2;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700277 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700278
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800279 if (vm.count("help") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500280 printUsage(std::cout, argv[0], description);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700281 return 0;
282 }
283
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800284 if (vm.count("version") > 0) {
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700285 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
286 return 0;
287 }
288
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800289 if (vm.count("modules") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500290 printLogModules(std::cout);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600291 return 0;
292 }
293
Davide Pesavento47456e72018-02-25 16:21:53 -0500294 const std::string boostBuildInfo =
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500295 "with Boost version " + std::to_string(BOOST_VERSION / 100000) +
296 "." + std::to_string(BOOST_VERSION / 100 % 1000) +
297 "." + std::to_string(BOOST_VERSION % 100);
Davide Pesavento47456e72018-02-25 16:21:53 -0500298 const std::string pcapBuildInfo =
Davide Pesavento264af772021-02-09 21:48:24 -0500299#ifdef NFD_HAVE_LIBPCAP
Davide Pesavento47456e72018-02-25 16:21:53 -0500300 "with " + std::string(pcap_lib_version());
301#else
302 "without libpcap";
303#endif
304 const std::string wsBuildInfo =
Davide Pesavento264af772021-02-09 21:48:24 -0500305#ifdef NFD_HAVE_WEBSOCKET
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500306 "with WebSocket++ version " + std::to_string(websocketpp::major_version) +
307 "." + std::to_string(websocketpp::minor_version) +
308 "." + std::to_string(websocketpp::patch_version);
Davide Pesavento47456e72018-02-25 16:21:53 -0500309#else
310 "without WebSocket++";
311#endif
312
Davide Pesavento03f45d22019-04-04 12:34:26 -0400313 std::clog << "NFD version " << NFD_VERSION_BUILD_STRING << " starting\n"
Davide Pesaventoa3148082018-04-12 18:21:54 -0400314 << "Built with " BOOST_COMPILER ", with " BOOST_STDLIB
Davide Pesavento47456e72018-02-25 16:21:53 -0500315 ", " << boostBuildInfo <<
316 ", " << pcapBuildInfo <<
317 ", " << wsBuildInfo <<
Davide Pesaventoa3148082018-04-12 18:21:54 -0400318 ", with ndn-cxx version " NDN_CXX_VERSION_BUILD_STRING
319 << std::endl;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600320
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500321 auto flush = ndn::make_scope_exit([] { ndn::util::Logging::flush(); });
322
Davide Pesavento59769b12017-11-12 23:52:06 -0500323 NfdRunner runner(configFile);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700324 try {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800325 runner.initialize();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700326 }
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500327 catch (const boost::system::system_error& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500328 NFD_LOG_FATAL(boost::diagnostic_information(e));
Davide Pesavento4b1921f2024-01-12 20:25:45 -0500329 if (e.code() == boost::system::errc::operation_not_permitted ||
330 e.code() == boost::system::errc::permission_denied) {
331 return 4;
332 }
333 return 1;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600334 }
Davide Pesavento401d1a42024-12-19 21:10:22 -0500335 catch (const std::system_error& e) {
336 NFD_LOG_FATAL(boost::diagnostic_information(e));
337 if (e.code() == std::errc::operation_not_permitted ||
338 e.code() == std::errc::permission_denied) {
339 return 4;
340 }
341 return 1;
342 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600343 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500344 NFD_LOG_FATAL(boost::diagnostic_information(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500345 return 1;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700346 }
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600347 catch (const PrivilegeHelper::Error& e) {
348 // PrivilegeHelper::Errors do not inherit from std::exception
349 // and represent seteuid/gid failures
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600350 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500351 return 4;
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600352 }
353
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800354 return runner.run();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700355}