Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 3 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 26 | #include "nfd.hpp" |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 27 | #include "rib/nrd.hpp" |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 29 | #include "version.hpp" |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 30 | #include "core/global-io.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 31 | #include "core/logger.hpp" |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 32 | #include "core/privilege-helper.hpp" |
Spyridon Mastorakis | d374c40 | 2015-09-09 15:07:00 -0700 | [diff] [blame^] | 33 | #include "core/extended-error-message.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 34 | |
| 35 | #include <string.h> |
| 36 | |
| 37 | #include <boost/filesystem.hpp> |
| 38 | #include <boost/program_options/options_description.hpp> |
| 39 | #include <boost/program_options/variables_map.hpp> |
| 40 | #include <boost/program_options/parsers.hpp> |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 42 | // boost::thread is used instead of std::thread to guarantee proper cleanup of thread local storage, |
| 43 | // see http://www.boost.org/doc/libs/1_48_0/doc/html/thread/thread_local_storage.html |
| 44 | #include <boost/thread.hpp> |
| 45 | |
| 46 | #include <atomic> |
| 47 | #include <condition_variable> |
| 48 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 49 | namespace nfd { |
| 50 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 51 | NFD_LOG_INIT("NFD"); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 52 | |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 53 | /** \brief Executes NFD with RIB manager |
| 54 | * |
| 55 | * NFD (main forwarding procedure) and RIB manager execute in two different threads. |
| 56 | * Each thread has its own instances global io_service and global scheduler. |
| 57 | * |
| 58 | * When either of the daemons fails, execution of non-failed daemon will be terminated as |
| 59 | * well. In other words, when NFD fails, RIB manager will be terminated; when RIB manager |
| 60 | * fails, NFD will be terminated. |
| 61 | */ |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 62 | class NfdRunner : noncopyable |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 63 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 64 | public: |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 65 | explicit |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 66 | NfdRunner(const std::string& configFile) |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 67 | : m_nfd(configFile, m_nfdKeyChain) |
| 68 | , m_configFile(configFile) |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 69 | , m_terminationSignalSet(getGlobalIoService()) |
| 70 | , m_reloadSignalSet(getGlobalIoService()) |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 71 | { |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 72 | m_terminationSignalSet.add(SIGINT); |
| 73 | m_terminationSignalSet.add(SIGTERM); |
| 74 | m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2)); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 75 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 76 | m_reloadSignalSet.add(SIGHUP); |
| 77 | m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2)); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 80 | static void |
| 81 | printUsage(std::ostream& os, const std::string& programName) |
| 82 | { |
| 83 | os << "Usage: \n" |
| 84 | << " " << programName << " [options]\n" |
| 85 | << "\n" |
| 86 | << "Run NFD forwarding daemon\n" |
| 87 | << "\n" |
| 88 | << "Options:\n" |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 89 | << " [--help] - print this help message\n" |
| 90 | << " [--version] - print version and exit\n" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 91 | << " [--modules] - list available logging modules\n" |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 92 | << " [--config /path/to/nfd.conf] - path to configuration file " |
| 93 | << "(default: " << DEFAULT_CONFIG_FILE << ")\n" |
| 94 | ; |
| 95 | } |
| 96 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 97 | static void |
| 98 | printModules(std::ostream& os) |
| 99 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 100 | os << "Available logging modules: \n"; |
| 101 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 102 | for (const auto& module : LoggerFactory::getInstance().getModules()) { |
| 103 | os << module << "\n"; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 106 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 107 | void |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 108 | initialize() |
| 109 | { |
| 110 | m_nfd.initialize(); |
| 111 | } |
| 112 | |
| 113 | int |
| 114 | run() |
| 115 | { |
| 116 | /** \brief return value |
| 117 | * A non-zero value is assigned when either NFD or RIB manager (running in a separate |
| 118 | * thread) fails. |
| 119 | */ |
| 120 | std::atomic_int retval(0); |
| 121 | |
| 122 | boost::asio::io_service* const mainIo = &getGlobalIoService(); |
| 123 | boost::asio::io_service* nrdIo = nullptr; |
| 124 | |
| 125 | // Mutex and conditional variable to implement synchronization between main and RIB manager |
| 126 | // threads: |
| 127 | // - to block main thread until RIB manager thread starts and initializes nrdIo (to allow |
| 128 | // stopping it later) |
| 129 | std::mutex m; |
| 130 | std::condition_variable cv; |
| 131 | |
| 132 | std::string configFile = this->m_configFile; // c++11 lambda cannot capture member variables |
| 133 | boost::thread nrdThread([configFile, &retval, &nrdIo, mainIo, &cv, &m] { |
| 134 | { |
| 135 | std::lock_guard<std::mutex> lock(m); |
| 136 | nrdIo = &getGlobalIoService(); |
| 137 | BOOST_ASSERT(nrdIo != mainIo); |
| 138 | } |
| 139 | cv.notify_all(); // notify that nrdIo has been assigned |
| 140 | |
| 141 | try { |
| 142 | ndn::KeyChain nrdKeyChain; |
| 143 | // must be created inside a separate thread |
| 144 | rib::Nrd nrd(configFile, nrdKeyChain); |
| 145 | nrd.initialize(); |
| 146 | getGlobalIoService().run(); // nrdIo is not thread-safe to use here |
| 147 | } |
| 148 | catch (const std::exception& e) { |
| 149 | NFD_LOG_FATAL(e.what()); |
| 150 | retval = 6; |
| 151 | mainIo->stop(); |
| 152 | } |
| 153 | |
| 154 | { |
| 155 | std::lock_guard<std::mutex> lock(m); |
| 156 | nrdIo = nullptr; |
| 157 | } |
| 158 | }); |
| 159 | |
| 160 | { |
| 161 | // Wait to guarantee that nrdIo is properly initialized, so it can be used to terminate |
| 162 | // RIB manager thread. |
| 163 | std::unique_lock<std::mutex> lock(m); |
| 164 | cv.wait(lock, [&nrdIo] { return nrdIo != nullptr; }); |
| 165 | } |
| 166 | |
| 167 | try { |
| 168 | mainIo->run(); |
| 169 | } |
| 170 | catch (const std::exception& e) { |
| 171 | NFD_LOG_FATAL(e.what()); |
| 172 | retval = 4; |
| 173 | } |
| 174 | catch (const PrivilegeHelper::Error& e) { |
| 175 | NFD_LOG_FATAL(e.what()); |
| 176 | retval = 5; |
| 177 | } |
| 178 | |
| 179 | { |
| 180 | // nrdIo is guaranteed to be alive at this point |
| 181 | std::lock_guard<std::mutex> lock(m); |
| 182 | if (nrdIo != nullptr) { |
| 183 | nrdIo->stop(); |
| 184 | nrdIo = nullptr; |
| 185 | } |
| 186 | } |
| 187 | nrdThread.join(); |
| 188 | |
| 189 | return retval; |
| 190 | } |
| 191 | |
| 192 | void |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 193 | terminate(const boost::system::error_code& error, int signalNo) |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 194 | { |
| 195 | if (error) |
| 196 | return; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 197 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 198 | NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting..."); |
| 199 | getGlobalIoService().stop(); |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 200 | } |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 201 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 202 | void |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 203 | reload(const boost::system::error_code& error, int signalNo) |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 204 | { |
| 205 | if (error) |
| 206 | return; |
| 207 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 208 | NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', reloading..."); |
| 209 | m_nfd.reloadConfigFile(); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 210 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 211 | m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2)); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 212 | } |
| 213 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 214 | private: |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 215 | ndn::KeyChain m_nfdKeyChain; |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 216 | Nfd m_nfd; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 217 | std::string m_configFile; |
| 218 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 219 | boost::asio::signal_set m_terminationSignalSet; |
| 220 | boost::asio::signal_set m_reloadSignalSet; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 221 | }; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 222 | |
| 223 | } // namespace nfd |
| 224 | |
| 225 | int |
| 226 | main(int argc, char** argv) |
| 227 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 228 | using namespace nfd; |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 229 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 230 | namespace po = boost::program_options; |
| 231 | |
| 232 | po::options_description description; |
| 233 | |
| 234 | std::string configFile = DEFAULT_CONFIG_FILE; |
| 235 | description.add_options() |
| 236 | ("help,h", "print this help message") |
| 237 | ("version,V", "print version and exit") |
| 238 | ("modules,m", "list available logging modules") |
| 239 | ("config,c", po::value<std::string>(&configFile), "path to configuration file") |
| 240 | ; |
| 241 | |
| 242 | po::variables_map vm; |
| 243 | try { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 244 | po::store(po::command_line_parser(argc, argv).options(description).run(), vm); |
| 245 | po::notify(vm); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 246 | } |
| 247 | catch (const std::exception& e) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 248 | // avoid NFD_LOG_FATAL to ensure that errors related to command-line parsing always appear on the |
| 249 | // terminal and are not littered with timestamps and other things added by the logging subsystem |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 250 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 251 | NfdRunner::printUsage(std::cerr, argv[0]); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 252 | return 1; |
| 253 | } |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 254 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 255 | if (vm.count("help") > 0) { |
| 256 | NfdRunner::printUsage(std::cout, argv[0]); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 260 | if (vm.count("version") > 0) { |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 261 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 262 | return 0; |
| 263 | } |
| 264 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 265 | if (vm.count("modules") > 0) { |
| 266 | NfdRunner::printModules(std::cout); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 270 | NfdRunner runner(configFile); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 271 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 272 | try { |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 273 | runner.initialize(); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 274 | } |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 275 | catch (const boost::filesystem::filesystem_error& e) { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 276 | if (e.code() == boost::system::errc::permission_denied) { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 277 | NFD_LOG_FATAL("Permissions denied for " << e.path1() << ". " << |
| 278 | argv[0] << " should be run as superuser"); |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 279 | return 3; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 280 | } |
| 281 | else { |
Spyridon Mastorakis | d374c40 | 2015-09-09 15:07:00 -0700 | [diff] [blame^] | 282 | NFD_LOG_FATAL(getExtendedErrorMessage(e)); |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 283 | return 2; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 284 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 285 | } |
| 286 | catch (const std::exception& e) { |
Spyridon Mastorakis | d374c40 | 2015-09-09 15:07:00 -0700 | [diff] [blame^] | 287 | NFD_LOG_FATAL(getExtendedErrorMessage(e)); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 288 | return 2; |
| 289 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 290 | catch (const PrivilegeHelper::Error& e) { |
| 291 | // PrivilegeHelper::Errors do not inherit from std::exception |
| 292 | // and represent seteuid/gid failures |
| 293 | |
| 294 | NFD_LOG_FATAL(e.what()); |
| 295 | return 3; |
| 296 | } |
| 297 | |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 298 | return runner.run(); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 299 | } |