Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 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 | * The University of Memphis |
| 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/>. |
| 24 | **/ |
| 25 | |
| 26 | #include "rib-manager.hpp" |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | namespace rib { |
| 31 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 32 | NFD_LOG_INIT("RibManager"); |
| 33 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 34 | const Name RibManager::COMMAND_PREFIX = "/localhost/nrd"; |
| 35 | const Name RibManager::REMOTE_COMMAND_PREFIX = "/localhop/nrd"; |
| 36 | |
| 37 | const size_t RibManager::COMMAND_UNSIGNED_NCOMPS = |
| 38 | RibManager::COMMAND_PREFIX.size() + |
| 39 | 1 + // verb |
| 40 | 1; // verb options |
| 41 | |
| 42 | const size_t RibManager::COMMAND_SIGNED_NCOMPS = |
| 43 | RibManager::COMMAND_UNSIGNED_NCOMPS + |
| 44 | 4; // (timestamp, nonce, signed info tlv, signature tlv) |
| 45 | |
| 46 | const RibManager::VerbAndProcessor RibManager::COMMAND_VERBS[] = |
| 47 | { |
| 48 | VerbAndProcessor( |
| 49 | Name::Component("register"), |
| 50 | &RibManager::insertEntry |
| 51 | ), |
| 52 | |
| 53 | VerbAndProcessor( |
| 54 | Name::Component("unregister"), |
| 55 | &RibManager::deleteEntry |
| 56 | ), |
| 57 | }; |
| 58 | |
| 59 | RibManager::RibManager() |
| 60 | : m_face(new ndn::Face()) |
| 61 | , m_nfdController(new ndn::nfd::Controller(*m_face)) |
| 62 | , m_validator(m_face) |
| 63 | , m_faceMonitor(*m_face) |
| 64 | , m_verbDispatch(COMMAND_VERBS, |
| 65 | COMMAND_VERBS + (sizeof(COMMAND_VERBS) / sizeof(VerbAndProcessor))) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | RibManager::registerWithNfd() |
| 71 | { |
| 72 | //check whether the components of localhop and localhost prefixes are same |
| 73 | BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size()); |
| 74 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 75 | NFD_LOG_INFO("Setting interest filter on: " << COMMAND_PREFIX.toUri()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 76 | m_face->setController(m_nfdController); |
| 77 | m_face->setInterestFilter(COMMAND_PREFIX.toUri(), |
| 78 | bind(&RibManager::onRibRequest, this, _2), |
| 79 | bind(&RibManager::setInterestFilterFailed, this, _1, _2)); |
| 80 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 81 | NFD_LOG_INFO("Setting interest filter on: " << REMOTE_COMMAND_PREFIX.toUri()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 82 | m_face->setInterestFilter(REMOTE_COMMAND_PREFIX.toUri(), |
| 83 | bind(&RibManager::onRibRequest, this, _2), |
| 84 | bind(&RibManager::setInterestFilterFailed, this, _1, _2)); |
| 85 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 86 | NFD_LOG_INFO("Start monitoring face create/destroy events"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 87 | m_faceMonitor.addSubscriber(boost::bind(&RibManager::onNotification, this, _1)); |
| 88 | m_faceMonitor.startNotifications(); |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | RibManager::setConfigFile(ConfigFile& configFile) |
| 93 | { |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame^] | 94 | configFile.addSectionHandler("rib_security", |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 95 | bind(&RibManager::onConfig, this, _1, _2, _3)); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | RibManager::onConfig(const ConfigSection& configSection, |
| 100 | bool isDryRun, |
| 101 | const std::string& filename) |
| 102 | { |
| 103 | /// \todo remove check after validator-conf replaces settings on each load |
| 104 | if (!isDryRun) |
| 105 | m_validator.load(configSection, filename); |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | RibManager::setInterestFilterFailed(const Name& name, const std::string& msg) |
| 110 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 111 | NFD_LOG_ERROR("Error in setting interest filter (" << name << "): " << msg); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 112 | m_face->shutdown(); |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | RibManager::sendResponse(const Name& name, |
| 117 | const ControlResponse& response) |
| 118 | { |
| 119 | const Block& encodedControl = response.wireEncode(); |
| 120 | |
| 121 | Data responseData(name); |
| 122 | responseData.setContent(encodedControl); |
| 123 | |
| 124 | m_keyChain.sign(responseData); |
| 125 | m_face->put(responseData); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | RibManager::sendResponse(const Name& name, |
| 130 | uint32_t code, |
| 131 | const std::string& text) |
| 132 | { |
| 133 | ControlResponse response(code, text); |
| 134 | sendResponse(name, response); |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | RibManager::onRibRequest(const Interest& request) |
| 139 | { |
| 140 | m_validator.validate(request, |
| 141 | bind(&RibManager::onRibRequestValidated, this, _1), |
| 142 | bind(&RibManager::onRibRequestValidationFailed, this, _1, _2)); |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | RibManager::onRibRequestValidated(const shared_ptr<const Interest>& request) |
| 147 | { |
| 148 | const Name& command = request->getName(); |
| 149 | |
| 150 | //REMOTE_COMMAND_PREFIX number of componenets are same as |
| 151 | // NRD_COMMAND_PREFIX's so no extra checks are required. |
| 152 | const Name::Component& verb = command.get(COMMAND_PREFIX.size()); |
| 153 | VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb); |
| 154 | |
| 155 | if (verbProcessor != m_verbDispatch.end()) |
| 156 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 157 | NFD_LOG_TRACE("Processing '" << verb << "' verb"); |
| 158 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 159 | PrefixRegOptions options; |
| 160 | if (!extractOptions(*request, options)) |
| 161 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 162 | NFD_LOG_DEBUG("Error while extracting options, returning malformed command"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 163 | sendResponse(command, 400, "Malformed command"); |
| 164 | return; |
| 165 | } |
| 166 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 167 | NFD_LOG_DEBUG("Received options (name, faceid, cost): " << options.getName() |
| 168 | << ", " << options.getFaceId() << ", " << options.getCost()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 169 | |
| 170 | ControlResponse response; |
| 171 | (verbProcessor->second)(this, *request, options); |
| 172 | } |
| 173 | else |
| 174 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 175 | NFD_LOG_DEBUG("Unsupported command: " << verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 176 | sendResponse(request->getName(), 501, "Unsupported command"); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | RibManager::onRibRequestValidationFailed(const shared_ptr<const Interest>& request, |
| 182 | const std::string& failureInfo) |
| 183 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 184 | NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 185 | sendResponse(request->getName(), 403, failureInfo); |
| 186 | } |
| 187 | |
| 188 | bool |
| 189 | RibManager::extractOptions(const Interest& request, |
| 190 | PrefixRegOptions& extractedOptions) |
| 191 | { |
| 192 | // const Name& command = request.getName(); |
| 193 | //REMOTE_COMMAND_PREFIX is same in size of NRD_COMMAND_PREFIX |
| 194 | //so no extra processing is required. |
| 195 | const size_t optionCompIndex = COMMAND_PREFIX.size() + 1; |
| 196 | |
| 197 | try |
| 198 | { |
| 199 | Block rawOptions = request.getName()[optionCompIndex].blockFromValue(); |
| 200 | extractedOptions.wireDecode(rawOptions); |
| 201 | } |
| 202 | catch (const ndn::Tlv::Error& e) |
| 203 | { |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | if (extractedOptions.getFaceId() == 0) |
| 208 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 209 | NFD_LOG_TRACE("IncomingFaceId: " << request.getIncomingFaceId()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 210 | extractedOptions.setFaceId(request.getIncomingFaceId()); |
| 211 | } |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | RibManager::onCommandError(uint32_t code, const std::string& error, |
| 217 | const Interest& request, |
| 218 | const PrefixRegOptions& options) |
| 219 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 220 | NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 221 | |
| 222 | ControlResponse response; |
| 223 | |
| 224 | if (code == 404) |
| 225 | { |
| 226 | response.setCode(code); |
| 227 | response.setText(error); |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | response.setCode(533); |
| 232 | std::ostringstream os; |
| 233 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 234 | response.setText(os.str()); |
| 235 | } |
| 236 | |
| 237 | sendResponse(request.getName(), response); |
| 238 | m_managedRib.erase(options); |
| 239 | } |
| 240 | |
| 241 | void |
| 242 | RibManager::onUnRegSuccess(const Interest& request, const PrefixRegOptions& options) |
| 243 | { |
| 244 | ControlResponse response; |
| 245 | |
| 246 | response.setCode(200); |
| 247 | response.setText("Success"); |
| 248 | response.setBody(options.wireEncode()); |
| 249 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 250 | NFD_LOG_DEBUG("onUnRegSuccess: Name unregistered (" << options.getName() |
| 251 | << ", " << options.getFaceId() << ")"); |
| 252 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 253 | sendResponse(request.getName(), response); |
| 254 | m_managedRib.erase(options); |
| 255 | } |
| 256 | |
| 257 | void |
| 258 | RibManager::onRegSuccess(const Interest& request, const PrefixRegOptions& options) |
| 259 | { |
| 260 | ControlResponse response; |
| 261 | |
| 262 | response.setCode(200); |
| 263 | response.setText("Success"); |
| 264 | response.setBody(options.wireEncode()); |
| 265 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 266 | NFD_LOG_DEBUG("onRegSuccess: Name registered (" << options.getName() << ", " |
| 267 | << options.getFaceId() << ")"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 268 | sendResponse(request.getName(), response); |
| 269 | } |
| 270 | |
| 271 | void |
| 272 | RibManager::insertEntry(const Interest& request, const PrefixRegOptions& options) |
| 273 | { |
| 274 | // For right now, just pass the options to fib as it is, |
| 275 | // without processing flags. Later options will be first added to |
| 276 | // Rib tree, then nrd will generate fib updates based on flags and then |
| 277 | // will add next hops one by one.. |
| 278 | m_managedRib.insert(options); |
| 279 | m_nfdController->start<ndn::nfd::FibAddNextHopCommand>( |
| 280 | ControlParameters() |
| 281 | .setName(options.getName()) |
| 282 | .setFaceId(options.getFaceId()) |
| 283 | .setCost(options.getCost()), |
| 284 | bind(&RibManager::onRegSuccess, this, request, options), |
| 285 | bind(&RibManager::onCommandError, this, _1, _2, request, options)); |
| 286 | } |
| 287 | |
| 288 | void |
| 289 | RibManager::deleteEntry(const Interest& request, const PrefixRegOptions& options) |
| 290 | { |
| 291 | m_nfdController->start<ndn::nfd::FibRemoveNextHopCommand>( |
| 292 | ControlParameters() |
| 293 | .setName(options.getName()) |
| 294 | .setFaceId(options.getFaceId()), |
| 295 | bind(&RibManager::onUnRegSuccess, this, request, options), |
| 296 | bind(&RibManager::onCommandError, this, _1, _2, request, options)); |
| 297 | } |
| 298 | |
| 299 | boost::asio::io_service& |
| 300 | RibManager::getIoService() |
| 301 | { |
| 302 | /// \todo Switch face to use global io service (needs library update) |
| 303 | return *m_face->ioService(); |
| 304 | } |
| 305 | |
| 306 | void |
| 307 | RibManager::onControlHeaderSuccess() |
| 308 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 309 | NFD_LOG_DEBUG("Local control header enabled"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void |
| 313 | RibManager::onControlHeaderError(uint32_t code, const std::string& reason) |
| 314 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 315 | NFD_LOG_ERROR("Error: couldn't enable local control header " |
| 316 | << "(code: " << code << ", info: " << reason << ")"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 317 | m_face->shutdown(); |
| 318 | } |
| 319 | |
| 320 | void |
| 321 | RibManager::enableLocalControlHeader() |
| 322 | { |
| 323 | m_nfdController->start<ndn::nfd::FaceEnableLocalControlCommand>( |
| 324 | ControlParameters() |
| 325 | .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID), |
| 326 | bind(&RibManager::onControlHeaderSuccess, this), |
| 327 | bind(&RibManager::onControlHeaderError, this, _1, _2)); |
| 328 | } |
| 329 | |
| 330 | void |
| 331 | RibManager::onNotification(const FaceEventNotification& notification) |
| 332 | { |
| 333 | /// \todo A notification can be missed, in this case check Facelist |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 334 | NFD_LOG_TRACE("onNotification: " << notification); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 335 | if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { //face destroyed |
| 336 | m_managedRib.erase(notification.getFaceId()); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | } // namespace rib |
| 341 | } // namespace nfd |