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