Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [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 | * The University of Memphis |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -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/>. |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 25 | |
| 26 | #include "rib-manager.hpp" |
Alexander Afanasyev | 03ea3eb | 2014-04-17 18:19:06 -0700 | [diff] [blame] | 27 | #include "core/global-io.hpp" |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Alexander Afanasyev | 63108c4 | 2014-07-07 19:10:47 -0700 | [diff] [blame] | 29 | #include "core/scheduler.hpp" |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | namespace rib { |
| 33 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 34 | NFD_LOG_INIT("RibManager"); |
| 35 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 36 | const Name RibManager::COMMAND_PREFIX = "/localhost/nfd/rib"; |
| 37 | const Name RibManager::REMOTE_COMMAND_PREFIX = "/localhop/nfd/rib"; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 38 | |
| 39 | const size_t RibManager::COMMAND_UNSIGNED_NCOMPS = |
| 40 | RibManager::COMMAND_PREFIX.size() + |
| 41 | 1 + // verb |
| 42 | 1; // verb options |
| 43 | |
| 44 | const size_t RibManager::COMMAND_SIGNED_NCOMPS = |
| 45 | RibManager::COMMAND_UNSIGNED_NCOMPS + |
| 46 | 4; // (timestamp, nonce, signed info tlv, signature tlv) |
| 47 | |
| 48 | const RibManager::VerbAndProcessor RibManager::COMMAND_VERBS[] = |
| 49 | { |
| 50 | VerbAndProcessor( |
| 51 | Name::Component("register"), |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 52 | &RibManager::registerEntry |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 53 | ), |
| 54 | |
| 55 | VerbAndProcessor( |
| 56 | Name::Component("unregister"), |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 57 | &RibManager::unregisterEntry |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 58 | ), |
| 59 | }; |
| 60 | |
| 61 | RibManager::RibManager() |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 62 | : m_face(getGlobalIoService()) |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 63 | , m_nfdController(m_face) |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 64 | , m_localhostValidator(m_face) |
| 65 | , m_localhopValidator(m_face) |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 66 | , m_faceMonitor(m_face) |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 67 | , m_isLocalhopEnabled(false) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 68 | , m_lastTransactionId(0) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 69 | , m_verbDispatch(COMMAND_VERBS, |
| 70 | COMMAND_VERBS + (sizeof(COMMAND_VERBS) / sizeof(VerbAndProcessor))) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | void |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 75 | RibManager::startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest) |
| 76 | { |
| 77 | NFD_LOG_INFO("Listening on: " << commandPrefix); |
| 78 | |
| 79 | m_nfdController.start<ndn::nfd::FibAddNextHopCommand>( |
| 80 | ControlParameters() |
| 81 | .setName(commandPrefix) |
| 82 | .setFaceId(0), |
| 83 | bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(commandPrefix)), |
| 84 | bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(commandPrefix), _2)); |
| 85 | |
| 86 | m_face.setInterestFilter(commandPrefix, onRequest); |
| 87 | } |
| 88 | |
| 89 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 90 | RibManager::registerWithNfd() |
| 91 | { |
| 92 | //check whether the components of localhop and localhost prefixes are same |
| 93 | BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size()); |
| 94 | |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 95 | this->startListening(COMMAND_PREFIX, bind(&RibManager::onLocalhostRequest, this, _2)); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 96 | |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 97 | if (m_isLocalhopEnabled) { |
| 98 | this->startListening(REMOTE_COMMAND_PREFIX, |
| 99 | bind(&RibManager::onLocalhopRequest, this, _2)); |
| 100 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 101 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 102 | NFD_LOG_INFO("Start monitoring face create/destroy events"); |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 103 | m_faceMonitor.addSubscriber(bind(&RibManager::onNotification, this, _1)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 104 | m_faceMonitor.startNotifications(); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | RibManager::setConfigFile(ConfigFile& configFile) |
| 109 | { |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 110 | configFile.addSectionHandler("rib", |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 111 | bind(&RibManager::onConfig, this, _1, _2, _3)); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | RibManager::onConfig(const ConfigSection& configSection, |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 116 | bool isDryRun, |
| 117 | const std::string& filename) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 118 | { |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 119 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 120 | i != configSection.end(); ++i) |
| 121 | { |
| 122 | if (i->first == "localhost_security") |
| 123 | m_localhostValidator.load(i->second, filename); |
| 124 | else if (i->first == "localhop_security") |
| 125 | { |
| 126 | m_localhopValidator.load(i->second, filename); |
| 127 | m_isLocalhopEnabled = true; |
| 128 | } |
| 129 | else |
| 130 | throw Error("Unrecognized rib property: " + i->first); |
| 131 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 135 | RibManager::sendResponse(const Name& name, |
| 136 | const ControlResponse& response) |
| 137 | { |
| 138 | const Block& encodedControl = response.wireEncode(); |
| 139 | |
| 140 | Data responseData(name); |
| 141 | responseData.setContent(encodedControl); |
| 142 | |
| 143 | m_keyChain.sign(responseData); |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 144 | m_face.put(responseData); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void |
| 148 | RibManager::sendResponse(const Name& name, |
| 149 | uint32_t code, |
| 150 | const std::string& text) |
| 151 | { |
| 152 | ControlResponse response(code, text); |
| 153 | sendResponse(name, response); |
| 154 | } |
| 155 | |
| 156 | void |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 157 | RibManager::onLocalhostRequest(const Interest& request) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 158 | { |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 159 | m_localhostValidator.validate(request, |
| 160 | bind(&RibManager::onCommandValidated, this, _1), |
| 161 | bind(&RibManager::onCommandValidationFailed, this, _1, _2)); |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | RibManager::onLocalhopRequest(const Interest& request) |
| 166 | { |
| 167 | m_localhopValidator.validate(request, |
| 168 | bind(&RibManager::onCommandValidated, this, _1), |
| 169 | bind(&RibManager::onCommandValidationFailed, this, _1, _2)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 173 | RibManager::onCommandValidated(const shared_ptr<const Interest>& request) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 174 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 175 | // REMOTE_COMMAND_PREFIX number of componenets are same as |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 176 | // NRD_COMMAND_PREFIX's so no extra checks are required. |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 177 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 178 | const Name& command = request->getName(); |
| 179 | const Name::Component& verb = command[COMMAND_PREFIX.size()]; |
| 180 | const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
| 181 | |
| 182 | VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 183 | if (verbProcessor != m_verbDispatch.end()) |
| 184 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 185 | ControlParameters parameters; |
| 186 | if (!extractParameters(parameterComponent, parameters)) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 187 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 188 | NFD_LOG_DEBUG("command result: malformed verb: " << verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 189 | sendResponse(command, 400, "Malformed command"); |
| 190 | return; |
| 191 | } |
| 192 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 193 | if (!parameters.hasFaceId() || parameters.getFaceId() == 0) |
| 194 | { |
| 195 | parameters.setFaceId(request->getIncomingFaceId()); |
| 196 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 197 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 198 | NFD_LOG_DEBUG("command result: processing verb: " << verb); |
| 199 | (verbProcessor->second)(this, request, parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 200 | } |
| 201 | else |
| 202 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 203 | NFD_LOG_DEBUG("Unsupported command: " << verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 204 | sendResponse(request->getName(), 501, "Unsupported command"); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 209 | RibManager::registerEntry(const shared_ptr<const Interest>& request, |
| 210 | ControlParameters& parameters) |
| 211 | { |
| 212 | ndn::nfd::RibRegisterCommand command; |
| 213 | |
| 214 | if (!validateParameters(command, parameters)) |
| 215 | { |
| 216 | NFD_LOG_DEBUG("register result: FAIL reason: malformed"); |
| 217 | sendResponse(request->getName(), 400, "Malformed command"); |
| 218 | return; |
| 219 | } |
| 220 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 221 | FaceEntry faceEntry; |
| 222 | faceEntry.faceId = parameters.getFaceId(); |
| 223 | faceEntry.origin = parameters.getOrigin(); |
| 224 | faceEntry.cost = parameters.getCost(); |
| 225 | faceEntry.flags = parameters.getFlags(); |
| 226 | faceEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod(); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 227 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 228 | NFD_LOG_TRACE("register prefix: " << faceEntry); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 229 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 230 | m_managedRib.insert(parameters.getName(), faceEntry); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 231 | |
| 232 | sendUpdatesToFib(request, parameters); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void |
| 236 | RibManager::unregisterEntry(const shared_ptr<const Interest>& request, |
| 237 | ControlParameters& parameters) |
| 238 | { |
Alexander Afanasyev | ce7520e | 2014-04-28 09:40:06 -0700 | [diff] [blame] | 239 | ndn::nfd::RibUnregisterCommand command; |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 240 | |
| 241 | if (!validateParameters(command, parameters)) |
| 242 | { |
| 243 | NFD_LOG_DEBUG("register result: FAIL reason: malformed"); |
| 244 | sendResponse(request->getName(), 400, "Malformed command"); |
| 245 | return; |
| 246 | } |
| 247 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 248 | FaceEntry faceEntry; |
| 249 | faceEntry.faceId = parameters.getFaceId(); |
| 250 | faceEntry.origin = parameters.getOrigin(); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 251 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 252 | NFD_LOG_TRACE("unregister prefix: " << faceEntry); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 253 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 254 | m_managedRib.erase(parameters.getName(), faceEntry); |
| 255 | |
| 256 | sendUpdatesToFib(request, parameters); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void |
| 260 | RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request, |
| 261 | const std::string& failureInfo) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 262 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 263 | NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 264 | sendResponse(request->getName(), 403, failureInfo); |
| 265 | } |
| 266 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 267 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 268 | bool |
| 269 | RibManager::extractParameters(const Name::Component& parameterComponent, |
| 270 | ControlParameters& extractedParameters) |
| 271 | { |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 272 | try |
| 273 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 274 | Block rawParameters = parameterComponent.blockFromValue(); |
| 275 | extractedParameters.wireDecode(rawParameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 276 | } |
| 277 | catch (const ndn::Tlv::Error& e) |
| 278 | { |
| 279 | return false; |
| 280 | } |
| 281 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 282 | NFD_LOG_DEBUG("Parameters parsed OK"); |
| 283 | return true; |
| 284 | } |
| 285 | |
| 286 | bool |
| 287 | RibManager::validateParameters(const ControlCommand& command, |
| 288 | ControlParameters& parameters) |
| 289 | { |
| 290 | try |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 291 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 292 | command.validateRequest(parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 293 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 294 | catch (const ControlCommand::ArgumentError&) |
| 295 | { |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | command.applyDefaultsToRequest(parameters); |
| 300 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 301 | return true; |
| 302 | } |
| 303 | |
| 304 | void |
| 305 | RibManager::onCommandError(uint32_t code, const std::string& error, |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 306 | const shared_ptr<const Interest>& request, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 307 | const FaceEntry& faceEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 308 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 309 | NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 310 | |
| 311 | ControlResponse response; |
| 312 | |
| 313 | if (code == 404) |
| 314 | { |
| 315 | response.setCode(code); |
| 316 | response.setText(error); |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | response.setCode(533); |
| 321 | std::ostringstream os; |
| 322 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 323 | response.setText(os.str()); |
| 324 | } |
| 325 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 326 | sendResponse(request->getName(), response); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 330 | RibManager::onRegSuccess(const shared_ptr<const Interest>& request, |
| 331 | const ControlParameters& parameters, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 332 | const FaceEntry& faceEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 333 | { |
| 334 | ControlResponse response; |
| 335 | |
| 336 | response.setCode(200); |
| 337 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 338 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 339 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 340 | NFD_LOG_TRACE("onRegSuccess: registered " << faceEntry); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 341 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 342 | sendResponse(request->getName(), response); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 345 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 346 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 347 | RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request, |
| 348 | const ControlParameters& parameters, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 349 | const FaceEntry& faceEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 350 | { |
| 351 | ControlResponse response; |
| 352 | |
| 353 | response.setCode(200); |
| 354 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 355 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 356 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 357 | NFD_LOG_TRACE("onUnRegSuccess: unregistered " << faceEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 358 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 359 | sendResponse(request->getName(), response); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | void |
| 363 | RibManager::sendSuccessResponse(const shared_ptr<const Interest>& request, |
| 364 | const ControlParameters& parameters) |
| 365 | { |
| 366 | if (!static_cast<bool>(request)) |
| 367 | { |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | ControlResponse response; |
| 372 | |
| 373 | response.setCode(200); |
| 374 | response.setText("Success"); |
| 375 | response.setBody(parameters.wireEncode()); |
| 376 | |
| 377 | sendResponse(request->getName(), response); |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | RibManager::sendErrorResponse(uint32_t code, const std::string& error, |
| 382 | const shared_ptr<const Interest>& request) |
| 383 | { |
| 384 | NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")"); |
| 385 | |
| 386 | if (!static_cast<bool>(request)) |
| 387 | { |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | ControlResponse response; |
| 392 | |
| 393 | if (code == 404) |
| 394 | { |
| 395 | response.setCode(code); |
| 396 | response.setText(error); |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | response.setCode(533); |
| 401 | std::ostringstream os; |
| 402 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 403 | response.setText(os.str()); |
| 404 | } |
| 405 | |
| 406 | sendResponse(request->getName(), response); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 409 | void |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 410 | RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix) |
| 411 | { |
| 412 | NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD"); |
| 413 | } |
| 414 | |
| 415 | void |
| 416 | RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg) |
| 417 | { |
| 418 | throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg); |
| 419 | } |
| 420 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 421 | bool |
| 422 | RibManager::isTransactionComplete(const TransactionId transactionId) |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 423 | { |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 424 | FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId); |
| 425 | |
| 426 | if (it != m_pendingFibTransactions.end()) |
| 427 | { |
| 428 | int& updatesLeft = it->second; |
| 429 | |
| 430 | updatesLeft--; |
| 431 | |
| 432 | // All of the updates have been applied successfully |
| 433 | if (updatesLeft == 0) |
| 434 | { |
| 435 | m_pendingFibTransactions.erase(it); |
| 436 | return true; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | return false; |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 444 | RibManager::invalidateTransaction(const TransactionId transactionId) |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 445 | { |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 446 | FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId); |
| 447 | |
| 448 | if (it != m_pendingFibTransactions.end()) |
| 449 | { |
| 450 | m_pendingFibTransactions.erase(it); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | void |
| 455 | RibManager::onAddNextHopSuccess(const shared_ptr<const Interest>& request, |
| 456 | const ControlParameters& parameters, |
| 457 | const TransactionId transactionId, |
| 458 | const bool shouldSendResponse) |
| 459 | { |
| 460 | if (isTransactionComplete(transactionId) && shouldSendResponse) |
| 461 | { |
| 462 | sendSuccessResponse(request, parameters); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | void |
| 467 | RibManager::onAddNextHopError(uint32_t code, const std::string& error, |
| 468 | const shared_ptr<const Interest>& request, |
| 469 | const TransactionId transactionId, const bool shouldSendResponse) |
| 470 | { |
| 471 | invalidateTransaction(transactionId); |
| 472 | |
| 473 | if (shouldSendResponse) |
| 474 | { |
| 475 | sendErrorResponse(code, error, request); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | void |
| 480 | RibManager::onRemoveNextHopSuccess(const shared_ptr<const Interest>& request, |
| 481 | const ControlParameters& parameters, |
| 482 | const TransactionId transactionId, |
| 483 | const bool shouldSendResponse) |
| 484 | { |
| 485 | if (isTransactionComplete(transactionId) && shouldSendResponse) |
| 486 | { |
| 487 | sendSuccessResponse(request, parameters); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | void |
| 492 | RibManager::onRemoveNextHopError(uint32_t code, const std::string& error, |
| 493 | const shared_ptr<const Interest>& request, |
| 494 | const TransactionId transactionId, const bool shouldSendResponse) |
| 495 | { |
| 496 | invalidateTransaction(transactionId); |
| 497 | |
| 498 | if (shouldSendResponse) |
| 499 | { |
| 500 | sendErrorResponse(code, error, request); |
| 501 | } |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 505 | RibManager::onControlHeaderSuccess() |
| 506 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 507 | NFD_LOG_DEBUG("Local control header enabled"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | void |
| 511 | RibManager::onControlHeaderError(uint32_t code, const std::string& reason) |
| 512 | { |
Alexander Afanasyev | b305165 | 2014-04-30 17:50:26 -0700 | [diff] [blame] | 513 | std::ostringstream os; |
| 514 | os << "Couldn't enable local control header " |
| 515 | << "(code: " << code << ", info: " << reason << ")"; |
| 516 | throw Error(os.str()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | void |
| 520 | RibManager::enableLocalControlHeader() |
| 521 | { |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 522 | m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>( |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 523 | ControlParameters() |
| 524 | .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID), |
| 525 | bind(&RibManager::onControlHeaderSuccess, this), |
| 526 | bind(&RibManager::onControlHeaderError, this, _1, _2)); |
| 527 | } |
| 528 | |
| 529 | void |
| 530 | RibManager::onNotification(const FaceEventNotification& notification) |
| 531 | { |
| 532 | /// \todo A notification can be missed, in this case check Facelist |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 533 | NFD_LOG_TRACE("onNotification: " << notification); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 534 | if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) //face destroyed |
| 535 | { |
Alexander Afanasyev | 63108c4 | 2014-07-07 19:10:47 -0700 | [diff] [blame] | 536 | scheduler::schedule(time::seconds(0), |
| 537 | bind(&RibManager::processErasureAfterNotification, this, |
| 538 | notification.getFaceId())); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | void |
Alexander Afanasyev | 63108c4 | 2014-07-07 19:10:47 -0700 | [diff] [blame] | 543 | RibManager::processErasureAfterNotification(uint64_t faceId) |
| 544 | { |
| 545 | m_managedRib.erase(faceId); |
| 546 | |
| 547 | sendUpdatesToFibAfterFaceDestroyEvent(); |
| 548 | } |
| 549 | |
| 550 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 551 | RibManager::sendUpdatesToFib(const shared_ptr<const Interest>& request, |
| 552 | const ControlParameters& parameters) |
| 553 | { |
| 554 | const Rib::FibUpdateList& updates = m_managedRib.getFibUpdates(); |
| 555 | |
| 556 | // If no updates were generated, consider the operation a success |
| 557 | if (updates.empty()) |
| 558 | { |
| 559 | sendSuccessResponse(request, parameters); |
| 560 | return; |
| 561 | } |
| 562 | |
| 563 | bool shouldWaitToRespond = false; |
| 564 | |
| 565 | // An application request should wait for all FIB updates to be applied |
| 566 | // successfully before sending a response |
| 567 | if (parameters.getOrigin() == ndn::nfd::ROUTE_ORIGIN_APP) |
| 568 | { |
| 569 | shouldWaitToRespond = true; |
| 570 | } |
| 571 | else // Respond immediately |
| 572 | { |
| 573 | sendSuccessResponse(request, parameters); |
| 574 | } |
| 575 | |
| 576 | NFD_LOG_DEBUG("Applying " << updates.size() << " updates to FIB"); |
| 577 | |
| 578 | // Assign an ID to this FIB transaction |
| 579 | TransactionId currentTransactionId = ++m_lastTransactionId; |
| 580 | |
| 581 | // Add this transaction to the transaction table |
| 582 | m_pendingFibTransactions[currentTransactionId] = updates.size(); |
| 583 | |
| 584 | for (Rib::FibUpdateList::const_iterator it = updates.begin(); it != updates.end(); ++it) |
| 585 | { |
| 586 | shared_ptr<const FibUpdate> update(*it); |
| 587 | |
| 588 | if (update->action == FibUpdate::ADD_NEXTHOP) |
| 589 | { |
| 590 | FaceEntry faceEntry; |
| 591 | faceEntry.faceId = update->faceId; |
| 592 | faceEntry.cost = update->cost; |
| 593 | |
| 594 | m_nfdController.start<ndn::nfd::FibAddNextHopCommand>( |
| 595 | ControlParameters() |
| 596 | .setName(update->name) |
| 597 | .setFaceId(faceEntry.faceId) |
| 598 | .setCost(faceEntry.cost), |
| 599 | bind(&RibManager::onAddNextHopSuccess, this, request, |
| 600 | parameters, |
| 601 | currentTransactionId, |
| 602 | shouldWaitToRespond), |
| 603 | bind(&RibManager::onAddNextHopError, this, _1, _2, request, currentTransactionId, |
| 604 | shouldWaitToRespond)); |
| 605 | } |
| 606 | else if (update->action == FibUpdate::REMOVE_NEXTHOP) |
| 607 | { |
| 608 | FaceEntry faceEntry; |
| 609 | faceEntry.faceId = update->faceId; |
| 610 | |
| 611 | m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>( |
| 612 | ControlParameters() |
| 613 | .setName(update->name) |
| 614 | .setFaceId(faceEntry.faceId), |
| 615 | bind(&RibManager::onRemoveNextHopSuccess, this, request, |
| 616 | parameters, |
| 617 | currentTransactionId, |
| 618 | shouldWaitToRespond), |
| 619 | bind(&RibManager::onRemoveNextHopError, this, _1, _2, request, currentTransactionId, |
| 620 | shouldWaitToRespond)); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | m_managedRib.clearFibUpdates(); |
| 625 | } |
| 626 | |
| 627 | void |
| 628 | RibManager::sendUpdatesToFibAfterFaceDestroyEvent() |
| 629 | { |
| 630 | ControlParameters parameters; |
| 631 | parameters.setOrigin(ndn::nfd::ROUTE_ORIGIN_STATIC); |
| 632 | |
| 633 | sendUpdatesToFib(shared_ptr<const Interest>(), parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | } // namespace rib |
| 637 | } // namespace nfd |