blob: 66462409dd718489ea2b5db49a6d0a1e60c39c78 [file] [log] [blame]
Alexander Afanasyev2aa39622014-01-22 11:51:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev31c781e2015-02-09 17:39:59 -08003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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"
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080027#include "rib/nrd.hpp"
Junxiao Shi98e29f42014-03-31 10:27:26 -070028
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070029#include "version.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"
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060032#include "core/privilege-helper.hpp"
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080033
34#include <string.h>
35
36#include <boost/filesystem.hpp>
37#include <boost/program_options/options_description.hpp>
38#include <boost/program_options/variables_map.hpp>
39#include <boost/program_options/parsers.hpp>
Alexander Afanasyevc78b1412014-02-19 14:08:26 -080040
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080041// boost::thread is used instead of std::thread to guarantee proper cleanup of thread local storage,
42// see http://www.boost.org/doc/libs/1_48_0/doc/html/thread/thread_local_storage.html
43#include <boost/thread.hpp>
44
45#include <atomic>
46#include <condition_variable>
47
Junxiao Shi09bf7c42014-01-31 11:10:25 -070048namespace nfd {
49
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070050NFD_LOG_INIT("NFD");
Junxiao Shi09bf7c42014-01-31 11:10:25 -070051
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080052/** \brief Executes NFD with RIB manager
53 *
54 * NFD (main forwarding procedure) and RIB manager execute in two different threads.
55 * Each thread has its own instances global io_service and global scheduler.
56 *
57 * When either of the daemons fails, execution of non-failed daemon will be terminated as
58 * well. In other words, when NFD fails, RIB manager will be terminated; when RIB manager
59 * fails, NFD will be terminated.
60 */
Alexander Afanasyev31367922015-02-09 20:51:10 -080061class NfdRunner : noncopyable
Junxiao Shi09bf7c42014-01-31 11:10:25 -070062{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070063public:
Alexander Afanasyev5959b012014-06-02 19:18:12 +030064 explicit
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080065 NfdRunner(const std::string& configFile)
Alexander Afanasyevf08a7372015-02-09 21:28:19 -080066 : m_nfd(configFile, m_nfdKeyChain)
67 , m_configFile(configFile)
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080068 , m_terminationSignalSet(getGlobalIoService())
69 , m_reloadSignalSet(getGlobalIoService())
Alexander Afanasyev5959b012014-06-02 19:18:12 +030070 {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080071 m_terminationSignalSet.add(SIGINT);
72 m_terminationSignalSet.add(SIGTERM);
73 m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +030074
Alexander Afanasyev31c781e2015-02-09 17:39:59 -080075 m_reloadSignalSet.add(SIGHUP);
76 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070077 }
78
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070079 static void
80 printUsage(std::ostream& os, const std::string& programName)
81 {
82 os << "Usage: \n"
83 << " " << programName << " [options]\n"
84 << "\n"
85 << "Run NFD forwarding daemon\n"
86 << "\n"
87 << "Options:\n"
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070088 << " [--help] - print this help message\n"
89 << " [--version] - print version and exit\n"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060090 << " [--modules] - list available logging modules\n"
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -070091 << " [--config /path/to/nfd.conf] - path to configuration file "
92 << "(default: " << DEFAULT_CONFIG_FILE << ")\n"
93 ;
94 }
95
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060096 static void
97 printModules(std::ostream& os)
98 {
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060099 os << "Available logging modules: \n";
100
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800101 for (const auto& module : LoggerFactory::getInstance().getModules()) {
102 os << module << "\n";
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700103 }
104 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700105
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700106 void
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800107 initialize()
108 {
109 m_nfd.initialize();
110 }
111
112 int
113 run()
114 {
115 /** \brief return value
116 * A non-zero value is assigned when either NFD or RIB manager (running in a separate
117 * thread) fails.
118 */
119 std::atomic_int retval(0);
120
121 boost::asio::io_service* const mainIo = &getGlobalIoService();
122 boost::asio::io_service* nrdIo = nullptr;
123
124 // Mutex and conditional variable to implement synchronization between main and RIB manager
125 // threads:
126 // - to block main thread until RIB manager thread starts and initializes nrdIo (to allow
127 // stopping it later)
128 std::mutex m;
129 std::condition_variable cv;
130
131 std::string configFile = this->m_configFile; // c++11 lambda cannot capture member variables
132 boost::thread nrdThread([configFile, &retval, &nrdIo, mainIo, &cv, &m] {
133 {
134 std::lock_guard<std::mutex> lock(m);
135 nrdIo = &getGlobalIoService();
136 BOOST_ASSERT(nrdIo != mainIo);
137 }
138 cv.notify_all(); // notify that nrdIo has been assigned
139
140 try {
141 ndn::KeyChain nrdKeyChain;
142 // must be created inside a separate thread
143 rib::Nrd nrd(configFile, nrdKeyChain);
144 nrd.initialize();
145 getGlobalIoService().run(); // nrdIo is not thread-safe to use here
146 }
147 catch (const std::exception& e) {
148 NFD_LOG_FATAL(e.what());
149 retval = 6;
150 mainIo->stop();
151 }
152
153 {
154 std::lock_guard<std::mutex> lock(m);
155 nrdIo = nullptr;
156 }
157 });
158
159 {
160 // Wait to guarantee that nrdIo is properly initialized, so it can be used to terminate
161 // RIB manager thread.
162 std::unique_lock<std::mutex> lock(m);
163 cv.wait(lock, [&nrdIo] { return nrdIo != nullptr; });
164 }
165
166 try {
167 mainIo->run();
168 }
169 catch (const std::exception& e) {
170 NFD_LOG_FATAL(e.what());
171 retval = 4;
172 }
173 catch (const PrivilegeHelper::Error& e) {
174 NFD_LOG_FATAL(e.what());
175 retval = 5;
176 }
177
178 {
179 // nrdIo is guaranteed to be alive at this point
180 std::lock_guard<std::mutex> lock(m);
181 if (nrdIo != nullptr) {
182 nrdIo->stop();
183 nrdIo = nullptr;
184 }
185 }
186 nrdThread.join();
187
188 return retval;
189 }
190
191 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
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800197 NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting...");
198 getGlobalIoService().stop();
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700199 }
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600200
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300201 void
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800202 reload(const boost::system::error_code& error, int signalNo)
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300203 {
204 if (error)
205 return;
206
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800207 NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', reloading...");
208 m_nfd.reloadConfigFile();
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300209
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800210 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
Alexander Afanasyev5959b012014-06-02 19:18:12 +0300211 }
212
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700213private:
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800214 ndn::KeyChain m_nfdKeyChain;
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800215 Nfd m_nfd;
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800216 std::string m_configFile;
217
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800218 boost::asio::signal_set m_terminationSignalSet;
219 boost::asio::signal_set m_reloadSignalSet;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700220};
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700221
222} // namespace nfd
223
224int
225main(int argc, char** argv)
226{
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700227 using namespace nfd;
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700228
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800229 namespace po = boost::program_options;
230
231 po::options_description description;
232
233 std::string configFile = DEFAULT_CONFIG_FILE;
234 description.add_options()
235 ("help,h", "print this help message")
236 ("version,V", "print version and exit")
237 ("modules,m", "list available logging modules")
238 ("config,c", po::value<std::string>(&configFile), "path to configuration file")
239 ;
240
241 po::variables_map vm;
242 try {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700243 po::store(po::command_line_parser(argc, argv).options(description).run(), vm);
244 po::notify(vm);
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800245 }
246 catch (const std::exception& e) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700247 // avoid NFD_LOG_FATAL to ensure that errors related to command-line parsing always appear on the
248 // terminal and are not littered with timestamps and other things added by the logging subsystem
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800249 std::cerr << "ERROR: " << e.what() << std::endl;
250 NfdRunner::printUsage(std::cerr, argv[0]);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700251 return 1;
252 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700253
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800254 if (vm.count("help") > 0) {
255 NfdRunner::printUsage(std::cout, argv[0]);
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700256 return 0;
257 }
258
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800259 if (vm.count("version") > 0) {
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700260 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
261 return 0;
262 }
263
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800264 if (vm.count("modules") > 0) {
265 NfdRunner::printModules(std::cout);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600266 return 0;
267 }
268
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800269 NfdRunner runner(configFile);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600270
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700271 try {
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800272 runner.initialize();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700273 }
Alexander Afanasyev31c781e2015-02-09 17:39:59 -0800274 catch (const boost::filesystem::filesystem_error& e) {
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700275 if (e.code() == boost::system::errc::permission_denied) {
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600276 NFD_LOG_FATAL("Permissions denied for " << e.path1() << ". " <<
277 argv[0] << " should be run as superuser");
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700278 return 3;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700279 }
280 else {
281 NFD_LOG_FATAL(e.what());
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700282 return 2;
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700283 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600284 }
285 catch (const std::exception& e) {
286 NFD_LOG_FATAL(e.what());
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700287 return 2;
288 }
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600289 catch (const PrivilegeHelper::Error& e) {
290 // PrivilegeHelper::Errors do not inherit from std::exception
291 // and represent seteuid/gid failures
292
293 NFD_LOG_FATAL(e.what());
294 return 3;
295 }
296
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800297 return runner.run();
Alexander Afanasyev5a4388a2014-03-27 18:02:55 -0700298}