blob: 3ba5bce4039fe54c838446aab75a2b32bb72a124 [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/*
3 * Copyright (c) 2014-2017, 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
38#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 Afanasyevc78b1412014-02-19 14:08:26 -080042
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>
46
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 Pesavento59769b12017-11-12 23:52:06 -050051namespace po = boost::program_options;
Junxiao Shi09bf7c42014-01-31 11:10:25 -070052
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070053NFD_LOG_INIT("NFD");
Junxiao Shi09bf7c42014-01-31 11:10:25 -070054
Davide Pesavento59769b12017-11-12 23:52:06 -050055namespace nfd {
56
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080057/** \brief Executes NFD with RIB manager
58 *
59 * NFD (main forwarding procedure) and RIB manager execute in two different threads.
Junxiao Shi71d12142016-07-23 20:10:18 +000060 * Each thread has its own instances of global io_service and global scheduler.
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080061 *
62 * When either of the daemons fails, execution of non-failed daemon will be terminated as
63 * well. In other words, when NFD fails, RIB manager will be terminated; when RIB manager
64 * fails, NFD will be terminated.
65 */
Alexander Afanasyev31367922015-02-09 20:51:10 -080066class NfdRunner : noncopyable
Junxiao Shi09bf7c42014-01-31 11:10:25 -070067{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070068public:
Alexander Afanasyev5959b012014-06-02 19:18:12 +030069 explicit
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080070 NfdRunner(const std::string& configFile)
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080071 : m_nfd(configFile, m_nfdKeyChain)
72 , m_configFile(configFile)
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080073 , m_terminationSignalSet(getGlobalIoService())
74 , m_reloadSignalSet(getGlobalIoService())
Alexander Afanasyev5959b012014-06-02 19:18:12 +030075 {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080076 m_terminationSignalSet.add(SIGINT);
77 m_terminationSignalSet.add(SIGTERM);
78 m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +030079
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080080 m_reloadSignalSet.add(SIGHUP);
81 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070082 }
83
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070084 void
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080085 initialize()
86 {
87 m_nfd.initialize();
88 }
89
90 int
91 run()
92 {
Davide Pesavento59769b12017-11-12 23:52:06 -050093 // Return value: a non-zero value is assigned when either NFD or RIB manager (running in
94 // a separate thread) fails.
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080095 std::atomic_int retval(0);
96
97 boost::asio::io_service* const mainIo = &getGlobalIoService();
Junxiao Shib2600172016-07-11 08:53:53 +000098 boost::asio::io_service* ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080099
100 // Mutex and conditional variable to implement synchronization between main and RIB manager
101 // threads:
Junxiao Shib2600172016-07-11 08:53:53 +0000102 // - to block main thread until RIB manager thread starts and initializes ribIo (to allow
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800103 // stopping it later)
104 std::mutex m;
105 std::condition_variable cv;
106
107 std::string configFile = this->m_configFile; // c++11 lambda cannot capture member variables
Junxiao Shib2600172016-07-11 08:53:53 +0000108 boost::thread ribThread([configFile, &retval, &ribIo, mainIo, &cv, &m] {
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800109 {
110 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000111 ribIo = &getGlobalIoService();
112 BOOST_ASSERT(ribIo != mainIo);
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800113 }
Junxiao Shib2600172016-07-11 08:53:53 +0000114 cv.notify_all(); // notify that ribIo has been assigned
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800115
116 try {
Junxiao Shib2600172016-07-11 08:53:53 +0000117 ndn::KeyChain ribKeyChain;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800118 // must be created inside a separate thread
Junxiao Shib2600172016-07-11 08:53:53 +0000119 rib::Service ribService(configFile, ribKeyChain);
120 ribService.initialize();
121 getGlobalIoService().run(); // ribIo is not thread-safe to use here
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800122 }
123 catch (const std::exception& e) {
124 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500125 retval = 1;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800126 mainIo->stop();
127 }
128
129 {
130 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000131 ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800132 }
133 });
134
135 {
Junxiao Shib2600172016-07-11 08:53:53 +0000136 // Wait to guarantee that ribIo is properly initialized, so it can be used to terminate
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800137 // RIB manager thread.
138 std::unique_lock<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000139 cv.wait(lock, [&ribIo] { return ribIo != nullptr; });
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800140 }
141
142 try {
143 mainIo->run();
144 }
145 catch (const std::exception& e) {
Junxiao Shi71d12142016-07-23 20:10:18 +0000146 NFD_LOG_FATAL(getExtendedErrorMessage(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500147 retval = 1;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800148 }
149 catch (const PrivilegeHelper::Error& e) {
150 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500151 retval = 4;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800152 }
153
154 {
Junxiao Shib2600172016-07-11 08:53:53 +0000155 // ribIo is guaranteed to be alive at this point
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800156 std::lock_guard<std::mutex> lock(m);
Junxiao Shib2600172016-07-11 08:53:53 +0000157 if (ribIo != nullptr) {
158 ribIo->stop();
159 ribIo = nullptr;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800160 }
161 }
Junxiao Shib2600172016-07-11 08:53:53 +0000162 ribThread.join();
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800163
164 return retval;
165 }
166
167 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800168 terminate(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700169 {
170 if (error)
171 return;
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700172
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800173 NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting...");
174 getGlobalIoService().stop();
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700175 }
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600176
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300177 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800178 reload(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300179 {
180 if (error)
181 return;
182
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800183 NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', reloading...");
184 m_nfd.reloadConfigFile();
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300185
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800186 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300187 }
188
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700189private:
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800190 ndn::KeyChain m_nfdKeyChain;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800191 Nfd m_nfd;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800192 std::string m_configFile;
193
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800194 boost::asio::signal_set m_terminationSignalSet;
195 boost::asio::signal_set m_reloadSignalSet;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700196};
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700197
Davide Pesavento59769b12017-11-12 23:52:06 -0500198static void
199printUsage(std::ostream& os, const char* programName,
200 const po::options_description& opts)
201{
202 os << "Usage: " << programName << " [options]\n"
203 << "Run the NDN Forwarding Daemon (NFD)\n"
204 << "\n"
205 << opts;
206}
207
208static void
209printLogModules(std::ostream& os)
210{
211 const auto& factory = LoggerFactory::getInstance();
212 for (const auto& module : factory.getModules()) {
213 os << module << "\n";
214 }
215}
216
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700217} // namespace nfd
218
219int
220main(int argc, char** argv)
221{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700222 using namespace nfd;
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700223
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800224 std::string configFile = DEFAULT_CONFIG_FILE;
Davide Pesavento59769b12017-11-12 23:52:06 -0500225
226 po::options_description description("Options");
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800227 description.add_options()
Davide Pesavento59769b12017-11-12 23:52:06 -0500228 ("help,h", "print this message and exit")
229 ("version,V", "show version information and exit")
230 ("config,c", po::value<std::string>(&configFile),
231 "path to configuration file (default: " DEFAULT_CONFIG_FILE ")")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800232 ("modules,m", "list available logging modules")
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800233 ;
234
235 po::variables_map vm;
236 try {
Davide Pesavento59769b12017-11-12 23:52:06 -0500237 po::store(po::parse_command_line(argc, argv, description), vm);
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700238 po::notify(vm);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800239 }
240 catch (const std::exception& e) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700241 // avoid NFD_LOG_FATAL to ensure that errors related to command-line parsing always appear on the
242 // terminal and are not littered with timestamps and other things added by the logging subsystem
Davide Pesavento59769b12017-11-12 23:52:06 -0500243 std::cerr << "ERROR: " << e.what() << "\n\n";
244 printUsage(std::cerr, argv[0], description);
245 return 2;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700246 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700247
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800248 if (vm.count("help") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500249 printUsage(std::cout, argv[0], description);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700250 return 0;
251 }
252
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800253 if (vm.count("version") > 0) {
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700254 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
255 return 0;
256 }
257
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800258 if (vm.count("modules") > 0) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500259 printLogModules(std::cout);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600260 return 0;
261 }
262
Davide Pesavento59769b12017-11-12 23:52:06 -0500263 NFD_LOG_INFO("Version " NFD_VERSION_BUILD_STRING " starting");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600264
Davide Pesavento59769b12017-11-12 23:52:06 -0500265 NfdRunner runner(configFile);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700266 try {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800267 runner.initialize();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700268 }
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800269 catch (const boost::filesystem::filesystem_error& e) {
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700270 if (e.code() == boost::system::errc::permission_denied) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500271 NFD_LOG_FATAL("Permission denied for " << e.path1() <<
272 ". This program should be run as superuser");
273 return 4;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700274 }
275 else {
Spyridon Mastorakisd374c402015-09-09 15:07:00 -0700276 NFD_LOG_FATAL(getExtendedErrorMessage(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500277 return 1;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700278 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600279 }
280 catch (const std::exception& e) {
Spyridon Mastorakisd374c402015-09-09 15:07:00 -0700281 NFD_LOG_FATAL(getExtendedErrorMessage(e));
Davide Pesavento59769b12017-11-12 23:52:06 -0500282 return 1;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700283 }
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600284 catch (const PrivilegeHelper::Error& e) {
285 // PrivilegeHelper::Errors do not inherit from std::exception
286 // and represent seteuid/gid failures
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600287 NFD_LOG_FATAL(e.what());
Davide Pesavento59769b12017-11-12 23:52:06 -0500288 return 4;
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600289 }
290
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800291 return runner.run();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700292}