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 | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 24 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 25 | #include <getopt.h> |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 26 | #include <boost/filesystem.hpp> |
| 27 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 29 | #include "core/global-io.hpp" |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 30 | #include "fw/forwarder.hpp" |
| 31 | #include "mgmt/internal-face.hpp" |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 32 | #include "mgmt/fib-manager.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 33 | #include "mgmt/face-manager.hpp" |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 34 | #include "mgmt/strategy-choice-manager.hpp" |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 35 | #include "mgmt/status-server.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 36 | #include "core/config-file.hpp" |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 37 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 38 | namespace nfd { |
| 39 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 40 | NFD_LOG_INIT("NFD"); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 41 | |
| 42 | struct ProgramOptions |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 43 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 44 | bool showUsage; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 45 | bool showModules; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 46 | std::string config; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 49 | class Nfd : noncopyable |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 50 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 51 | public: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 52 | |
| 53 | void |
| 54 | initialize(const std::string& configFile) |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 55 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 56 | initializeLogging(configFile); |
| 57 | |
| 58 | m_forwarder = make_shared<Forwarder>(); |
| 59 | |
| 60 | initializeManagement(configFile); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | void |
| 65 | initializeLogging(const std::string& configFile) |
| 66 | { |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 67 | ConfigFile config(&ConfigFile::ignoreUnknownSection); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 68 | LoggerFactory::getInstance().setConfigFile(config); |
| 69 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 70 | config.parse(configFile, true); |
| 71 | config.parse(configFile, false); |
| 72 | } |
| 73 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 74 | class IgnoreRibAndLogSections |
| 75 | { |
| 76 | public: |
| 77 | void |
| 78 | operator()(const std::string& filename, |
| 79 | const std::string& sectionName, |
| 80 | const ConfigSection& section, |
| 81 | bool isDryRun) |
| 82 | |
| 83 | { |
| 84 | // Ignore "log" and sections beginning with "rib_" (intended for rib manager), |
| 85 | // but raise an error if we're missing a handler for an NFD section. |
| 86 | |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 87 | if (sectionName.find("rib") == 0 || sectionName == "log") |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 88 | { |
| 89 | // do nothing |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | // missing NFD section |
| 94 | ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun); |
| 95 | } |
| 96 | } |
| 97 | }; |
| 98 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 99 | void |
| 100 | initializeManagement(const std::string& configFile) |
| 101 | { |
| 102 | m_internalFace = make_shared<InternalFace>(); |
| 103 | |
| 104 | m_fibManager = make_shared<FibManager>(boost::ref(m_forwarder->getFib()), |
| 105 | bind(&Forwarder::getFace, m_forwarder.get(), _1), |
| 106 | m_internalFace); |
| 107 | |
| 108 | m_faceManager = make_shared<FaceManager>(boost::ref(m_forwarder->getFaceTable()), |
| 109 | m_internalFace); |
| 110 | |
| 111 | m_strategyChoiceManager = |
| 112 | make_shared<StrategyChoiceManager>(boost::ref(m_forwarder->getStrategyChoice()), |
| 113 | m_internalFace); |
| 114 | |
| 115 | m_statusServer = make_shared<StatusServer>(m_internalFace, |
| 116 | boost::ref(*m_forwarder)); |
| 117 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 118 | ConfigFile config((IgnoreRibAndLogSections())); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 119 | m_internalFace->getValidator().setConfigFile(config); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 120 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 121 | m_forwarder->addFace(m_internalFace); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 122 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 123 | m_faceManager->setConfigFile(config); |
| 124 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 125 | // parse config file |
| 126 | config.parse(configFile, true); |
| 127 | config.parse(configFile, false); |
| 128 | |
| 129 | // add FIB entry for NFD Management Protocol |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 130 | shared_ptr<fib::Entry> entry = m_forwarder->getFib().insert("/localhost/nfd").first; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 131 | entry->addNextHop(m_internalFace, 0); |
| 132 | } |
| 133 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 134 | static void |
| 135 | printUsage(std::ostream& os, const std::string& programName) |
| 136 | { |
| 137 | os << "Usage: \n" |
| 138 | << " " << programName << " [options]\n" |
| 139 | << "\n" |
| 140 | << "Run NFD forwarding daemon\n" |
| 141 | << "\n" |
| 142 | << "Options:\n" |
| 143 | << " [--help] - print this help message\n" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 144 | << " [--modules] - list available logging modules\n" |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 145 | << " [--config /path/to/nfd.conf] - path to configuration file " |
| 146 | << "(default: " << DEFAULT_CONFIG_FILE << ")\n" |
| 147 | ; |
| 148 | } |
| 149 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 150 | static void |
| 151 | printModules(std::ostream& os) |
| 152 | { |
| 153 | using namespace std; |
| 154 | |
| 155 | os << "Available logging modules: \n"; |
| 156 | |
| 157 | list<string> modules(LoggerFactory::getInstance().getModules()); |
| 158 | for (list<string>::const_iterator i = modules.begin(); i != modules.end(); ++i) |
| 159 | { |
| 160 | os << *i << "\n"; |
| 161 | } |
| 162 | } |
| 163 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 164 | static bool |
| 165 | parseCommandLine(int argc, char** argv, ProgramOptions& options) |
| 166 | { |
| 167 | options.showUsage = false; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 168 | options.showModules = false; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 169 | options.config = DEFAULT_CONFIG_FILE; |
| 170 | |
| 171 | while (true) { |
| 172 | int optionIndex = 0; |
| 173 | static ::option longOptions[] = { |
| 174 | { "help" , no_argument , 0, 0 }, |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 175 | { "modules", no_argument , 0, 0 }, |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 176 | { "config" , required_argument, 0, 0 }, |
| 177 | { 0 , 0 , 0, 0 } |
| 178 | }; |
| 179 | int c = getopt_long_only(argc, argv, "", longOptions, &optionIndex); |
| 180 | if (c == -1) |
| 181 | break; |
| 182 | |
| 183 | switch (c) { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 184 | case 0: |
| 185 | switch (optionIndex) { |
| 186 | case 0: // help |
| 187 | options.showUsage = true; |
| 188 | break; |
| 189 | case 1: // modules |
| 190 | options.showModules = true; |
| 191 | break; |
| 192 | case 2: // config |
| 193 | options.config = ::optarg; |
| 194 | break; |
| 195 | default: |
| 196 | return false; |
| 197 | } |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 198 | break; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 199 | } |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 200 | } |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 201 | return true; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 202 | } |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 203 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 204 | void |
| 205 | terminate(const boost::system::error_code& error, |
| 206 | int signalNo, |
| 207 | boost::asio::signal_set& signalSet) |
| 208 | { |
| 209 | if (error) |
| 210 | return; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 211 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 212 | if (signalNo == SIGINT || |
| 213 | signalNo == SIGTERM) |
| 214 | { |
| 215 | getGlobalIoService().stop(); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 216 | NFD_LOG_INFO("Caught signal '" << strsignal(signalNo) << "', exiting..."); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 217 | } |
| 218 | else |
| 219 | { |
| 220 | /// \todo May be try to reload config file (at least security section) |
| 221 | signalSet.async_wait(bind(&Nfd::terminate, this, _1, _2, |
| 222 | boost::ref(signalSet))); |
| 223 | } |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 224 | } |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 225 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 226 | private: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 227 | shared_ptr<Forwarder> m_forwarder; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 228 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 229 | shared_ptr<InternalFace> m_internalFace; |
| 230 | shared_ptr<FibManager> m_fibManager; |
| 231 | shared_ptr<FaceManager> m_faceManager; |
| 232 | shared_ptr<StrategyChoiceManager> m_strategyChoiceManager; |
| 233 | shared_ptr<StatusServer> m_statusServer; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 234 | }; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 235 | |
| 236 | } // namespace nfd |
| 237 | |
| 238 | int |
| 239 | main(int argc, char** argv) |
| 240 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 241 | using namespace nfd; |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 242 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 243 | ProgramOptions options; |
| 244 | bool isCommandLineValid = Nfd::parseCommandLine(argc, argv, options); |
| 245 | if (!isCommandLineValid) { |
| 246 | Nfd::printUsage(std::cerr, argv[0]); |
| 247 | return 1; |
| 248 | } |
| 249 | if (options.showUsage) { |
| 250 | Nfd::printUsage(std::cout, argv[0]); |
| 251 | return 0; |
| 252 | } |
| 253 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 254 | if (options.showModules) { |
| 255 | Nfd::printModules(std::cout); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | Nfd nfdInstance; |
| 260 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 261 | try { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 262 | nfdInstance.initialize(options.config); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 263 | } |
| 264 | catch (boost::filesystem::filesystem_error& e) { |
| 265 | if (e.code() == boost::system::errc::permission_denied) { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 266 | NFD_LOG_FATAL("Permissions denied for " << e.path1() << ". " << |
| 267 | argv[0] << " should be run as superuser"); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 268 | } |
| 269 | else { |
| 270 | NFD_LOG_FATAL(e.what()); |
| 271 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 272 | return 1; |
| 273 | } |
| 274 | catch (const std::exception& e) { |
| 275 | NFD_LOG_FATAL(e.what()); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 276 | return 2; |
| 277 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 278 | |
| 279 | boost::asio::signal_set signalSet(getGlobalIoService()); |
| 280 | signalSet.add(SIGINT); |
| 281 | signalSet.add(SIGTERM); |
| 282 | signalSet.add(SIGHUP); |
| 283 | signalSet.add(SIGUSR1); |
| 284 | signalSet.add(SIGUSR2); |
| 285 | signalSet.async_wait(bind(&Nfd::terminate, &nfdInstance, _1, _2, |
| 286 | boost::ref(signalSet))); |
| 287 | |
| 288 | try { |
| 289 | getGlobalIoService().run(); |
| 290 | } |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 291 | catch (std::exception& e) { |
| 292 | NFD_LOG_FATAL(e.what()); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 293 | return 3; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | } |