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 | |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 61 | RibManager::RibManager(ndn::Face& face) |
| 62 | : m_face(face) |
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); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 189 | if (static_cast<bool>(request)) |
| 190 | sendResponse(command, 400, "Malformed command"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 194 | if (!parameters.hasFaceId() || parameters.getFaceId() == 0) |
| 195 | { |
| 196 | parameters.setFaceId(request->getIncomingFaceId()); |
| 197 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 198 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 199 | NFD_LOG_DEBUG("command result: processing verb: " << verb); |
| 200 | (verbProcessor->second)(this, request, parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 201 | } |
| 202 | else |
| 203 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 204 | NFD_LOG_DEBUG("Unsupported command: " << verb); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 205 | if (static_cast<bool>(request)) |
| 206 | sendResponse(request->getName(), 501, "Unsupported command"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 211 | RibManager::registerEntry(const shared_ptr<const Interest>& request, |
| 212 | ControlParameters& parameters) |
| 213 | { |
| 214 | ndn::nfd::RibRegisterCommand command; |
| 215 | |
| 216 | if (!validateParameters(command, parameters)) |
| 217 | { |
| 218 | NFD_LOG_DEBUG("register result: FAIL reason: malformed"); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 219 | if (static_cast<bool>(request)) |
| 220 | sendResponse(request->getName(), 400, "Malformed command"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 224 | FaceEntry faceEntry; |
| 225 | faceEntry.faceId = parameters.getFaceId(); |
| 226 | faceEntry.origin = parameters.getOrigin(); |
| 227 | faceEntry.cost = parameters.getCost(); |
| 228 | faceEntry.flags = parameters.getFlags(); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 229 | |
Alexander Afanasyev | f67cf08 | 2014-07-18 16:47:29 -0700 | [diff] [blame] | 230 | if (parameters.hasExpirationPeriod() && |
| 231 | parameters.getExpirationPeriod() != time::milliseconds::max()) |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 232 | { |
| 233 | faceEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod(); |
| 234 | |
| 235 | // Schedule a new event, the old one will be cancelled during rib insertion. |
| 236 | EventId eventId; |
| 237 | NFD_LOG_TRACE("scheduling unregistration at: " << faceEntry.expires); |
| 238 | eventId = scheduler::schedule(parameters.getExpirationPeriod(), |
| 239 | bind(&RibManager::unregisterEntry, |
| 240 | this, shared_ptr<Interest>(), parameters)); |
| 241 | |
| 242 | //set the NewEventId of this entry |
| 243 | faceEntry.setExpirationEvent(eventId); |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | faceEntry.expires = time::steady_clock::TimePoint::max(); |
| 248 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 249 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 250 | NFD_LOG_TRACE("register prefix: " << faceEntry); |
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 | m_managedRib.insert(parameters.getName(), faceEntry); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 253 | |
| 254 | sendUpdatesToFib(request, parameters); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void |
| 258 | RibManager::unregisterEntry(const shared_ptr<const Interest>& request, |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 259 | ControlParameters& params) |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 260 | { |
Alexander Afanasyev | ce7520e | 2014-04-28 09:40:06 -0700 | [diff] [blame] | 261 | ndn::nfd::RibUnregisterCommand command; |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 262 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 263 | //passing all parameters gives error in validation. |
| 264 | //so passing only the required arguments. |
| 265 | ControlParameters parameters; |
| 266 | parameters.setName(params.getName()); |
Alexander Afanasyev | 56356b2 | 2014-07-18 16:56:34 -0700 | [diff] [blame^] | 267 | if (params.hasFaceId()) |
| 268 | parameters.setFaceId(params.getFaceId()); |
| 269 | if (params.hasOrigin()) |
| 270 | parameters.setOrigin(params.getOrigin()); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 271 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 272 | if (!validateParameters(command, parameters)) |
| 273 | { |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 274 | NFD_LOG_DEBUG("unregister result: FAIL reason: malformed"); |
| 275 | if (static_cast<bool>(request)) |
| 276 | sendResponse(request->getName(), 400, "Malformed command"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 277 | return; |
| 278 | } |
| 279 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 280 | FaceEntry faceEntry; |
| 281 | faceEntry.faceId = parameters.getFaceId(); |
| 282 | faceEntry.origin = parameters.getOrigin(); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 283 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 284 | NFD_LOG_TRACE("unregister prefix: " << faceEntry); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 285 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 286 | m_managedRib.erase(parameters.getName(), faceEntry); |
| 287 | |
| 288 | sendUpdatesToFib(request, parameters); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | void |
| 292 | RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request, |
| 293 | const std::string& failureInfo) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 294 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 295 | NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 296 | if (static_cast<bool>(request)) |
| 297 | sendResponse(request->getName(), 403, failureInfo); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 300 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 301 | bool |
| 302 | RibManager::extractParameters(const Name::Component& parameterComponent, |
| 303 | ControlParameters& extractedParameters) |
| 304 | { |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 305 | try |
| 306 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 307 | Block rawParameters = parameterComponent.blockFromValue(); |
| 308 | extractedParameters.wireDecode(rawParameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 309 | } |
| 310 | catch (const ndn::Tlv::Error& e) |
| 311 | { |
| 312 | return false; |
| 313 | } |
| 314 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 315 | NFD_LOG_DEBUG("Parameters parsed OK"); |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | bool |
| 320 | RibManager::validateParameters(const ControlCommand& command, |
| 321 | ControlParameters& parameters) |
| 322 | { |
| 323 | try |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 324 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 325 | command.validateRequest(parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 326 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 327 | catch (const ControlCommand::ArgumentError&) |
| 328 | { |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | command.applyDefaultsToRequest(parameters); |
| 333 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 334 | return true; |
| 335 | } |
| 336 | |
| 337 | void |
| 338 | RibManager::onCommandError(uint32_t code, const std::string& error, |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 339 | const shared_ptr<const Interest>& request, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 340 | const FaceEntry& faceEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 341 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 342 | NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 343 | |
| 344 | ControlResponse response; |
| 345 | |
| 346 | if (code == 404) |
| 347 | { |
| 348 | response.setCode(code); |
| 349 | response.setText(error); |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | response.setCode(533); |
| 354 | std::ostringstream os; |
| 355 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 356 | response.setText(os.str()); |
| 357 | } |
| 358 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 359 | if (static_cast<bool>(request)) |
| 360 | sendResponse(request->getName(), response); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 364 | RibManager::onRegSuccess(const shared_ptr<const Interest>& request, |
| 365 | const ControlParameters& parameters, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 366 | const FaceEntry& faceEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 367 | { |
| 368 | ControlResponse response; |
| 369 | |
| 370 | response.setCode(200); |
| 371 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 372 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 373 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 374 | NFD_LOG_TRACE("onRegSuccess: registered " << faceEntry); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 375 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 376 | if (static_cast<bool>(request)) |
| 377 | sendResponse(request->getName(), response); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 380 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 381 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 382 | RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request, |
| 383 | const ControlParameters& parameters, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 384 | const FaceEntry& faceEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 385 | { |
| 386 | ControlResponse response; |
| 387 | |
| 388 | response.setCode(200); |
| 389 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 390 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 391 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 392 | NFD_LOG_TRACE("onUnRegSuccess: unregistered " << faceEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 393 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 394 | if (static_cast<bool>(request)) |
| 395 | sendResponse(request->getName(), response); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void |
| 399 | RibManager::sendSuccessResponse(const shared_ptr<const Interest>& request, |
| 400 | const ControlParameters& parameters) |
| 401 | { |
| 402 | if (!static_cast<bool>(request)) |
| 403 | { |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | ControlResponse response; |
| 408 | |
| 409 | response.setCode(200); |
| 410 | response.setText("Success"); |
| 411 | response.setBody(parameters.wireEncode()); |
| 412 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 413 | if (static_cast<bool>(request)) |
| 414 | sendResponse(request->getName(), response); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void |
| 418 | RibManager::sendErrorResponse(uint32_t code, const std::string& error, |
| 419 | const shared_ptr<const Interest>& request) |
| 420 | { |
| 421 | NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")"); |
| 422 | |
| 423 | if (!static_cast<bool>(request)) |
| 424 | { |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | ControlResponse response; |
| 429 | |
| 430 | if (code == 404) |
| 431 | { |
| 432 | response.setCode(code); |
| 433 | response.setText(error); |
| 434 | } |
| 435 | else |
| 436 | { |
| 437 | response.setCode(533); |
| 438 | std::ostringstream os; |
| 439 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 440 | response.setText(os.str()); |
| 441 | } |
| 442 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 443 | if (static_cast<bool>(request)) |
| 444 | sendResponse(request->getName(), response); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 447 | void |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 448 | RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix) |
| 449 | { |
| 450 | NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD"); |
| 451 | } |
| 452 | |
| 453 | void |
| 454 | RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg) |
| 455 | { |
| 456 | throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg); |
| 457 | } |
| 458 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 459 | bool |
| 460 | RibManager::isTransactionComplete(const TransactionId transactionId) |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 461 | { |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 462 | FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId); |
| 463 | |
| 464 | if (it != m_pendingFibTransactions.end()) |
| 465 | { |
| 466 | int& updatesLeft = it->second; |
| 467 | |
| 468 | updatesLeft--; |
| 469 | |
| 470 | // All of the updates have been applied successfully |
| 471 | if (updatesLeft == 0) |
| 472 | { |
| 473 | m_pendingFibTransactions.erase(it); |
| 474 | return true; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | return false; |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 482 | RibManager::invalidateTransaction(const TransactionId transactionId) |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 483 | { |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 484 | FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId); |
| 485 | |
| 486 | if (it != m_pendingFibTransactions.end()) |
| 487 | { |
| 488 | m_pendingFibTransactions.erase(it); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | void |
| 493 | RibManager::onAddNextHopSuccess(const shared_ptr<const Interest>& request, |
| 494 | const ControlParameters& parameters, |
| 495 | const TransactionId transactionId, |
| 496 | const bool shouldSendResponse) |
| 497 | { |
| 498 | if (isTransactionComplete(transactionId) && shouldSendResponse) |
| 499 | { |
| 500 | sendSuccessResponse(request, parameters); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | void |
| 505 | RibManager::onAddNextHopError(uint32_t code, const std::string& error, |
| 506 | const shared_ptr<const Interest>& request, |
| 507 | const TransactionId transactionId, const bool shouldSendResponse) |
| 508 | { |
| 509 | invalidateTransaction(transactionId); |
| 510 | |
| 511 | if (shouldSendResponse) |
| 512 | { |
| 513 | sendErrorResponse(code, error, request); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | void |
| 518 | RibManager::onRemoveNextHopSuccess(const shared_ptr<const Interest>& request, |
| 519 | const ControlParameters& parameters, |
| 520 | const TransactionId transactionId, |
| 521 | const bool shouldSendResponse) |
| 522 | { |
| 523 | if (isTransactionComplete(transactionId) && shouldSendResponse) |
| 524 | { |
| 525 | sendSuccessResponse(request, parameters); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | void |
| 530 | RibManager::onRemoveNextHopError(uint32_t code, const std::string& error, |
| 531 | const shared_ptr<const Interest>& request, |
| 532 | const TransactionId transactionId, const bool shouldSendResponse) |
| 533 | { |
| 534 | invalidateTransaction(transactionId); |
| 535 | |
| 536 | if (shouldSendResponse) |
| 537 | { |
| 538 | sendErrorResponse(code, error, request); |
| 539 | } |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 543 | RibManager::onControlHeaderSuccess() |
| 544 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 545 | NFD_LOG_DEBUG("Local control header enabled"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | void |
| 549 | RibManager::onControlHeaderError(uint32_t code, const std::string& reason) |
| 550 | { |
Alexander Afanasyev | b305165 | 2014-04-30 17:50:26 -0700 | [diff] [blame] | 551 | std::ostringstream os; |
| 552 | os << "Couldn't enable local control header " |
| 553 | << "(code: " << code << ", info: " << reason << ")"; |
| 554 | throw Error(os.str()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void |
| 558 | RibManager::enableLocalControlHeader() |
| 559 | { |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 560 | m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>( |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 561 | ControlParameters() |
| 562 | .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID), |
| 563 | bind(&RibManager::onControlHeaderSuccess, this), |
| 564 | bind(&RibManager::onControlHeaderError, this, _1, _2)); |
| 565 | } |
| 566 | |
| 567 | void |
| 568 | RibManager::onNotification(const FaceEventNotification& notification) |
| 569 | { |
| 570 | /// \todo A notification can be missed, in this case check Facelist |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 571 | NFD_LOG_TRACE("onNotification: " << notification); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 572 | if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) //face destroyed |
| 573 | { |
Alexander Afanasyev | 63108c4 | 2014-07-07 19:10:47 -0700 | [diff] [blame] | 574 | scheduler::schedule(time::seconds(0), |
| 575 | bind(&RibManager::processErasureAfterNotification, this, |
| 576 | notification.getFaceId())); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
| 580 | void |
Alexander Afanasyev | 63108c4 | 2014-07-07 19:10:47 -0700 | [diff] [blame] | 581 | RibManager::processErasureAfterNotification(uint64_t faceId) |
| 582 | { |
| 583 | m_managedRib.erase(faceId); |
| 584 | |
| 585 | sendUpdatesToFibAfterFaceDestroyEvent(); |
| 586 | } |
| 587 | |
| 588 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 589 | RibManager::sendUpdatesToFib(const shared_ptr<const Interest>& request, |
| 590 | const ControlParameters& parameters) |
| 591 | { |
| 592 | const Rib::FibUpdateList& updates = m_managedRib.getFibUpdates(); |
| 593 | |
| 594 | // If no updates were generated, consider the operation a success |
| 595 | if (updates.empty()) |
| 596 | { |
| 597 | sendSuccessResponse(request, parameters); |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | bool shouldWaitToRespond = false; |
| 602 | |
| 603 | // An application request should wait for all FIB updates to be applied |
| 604 | // successfully before sending a response |
| 605 | if (parameters.getOrigin() == ndn::nfd::ROUTE_ORIGIN_APP) |
| 606 | { |
| 607 | shouldWaitToRespond = true; |
| 608 | } |
| 609 | else // Respond immediately |
| 610 | { |
| 611 | sendSuccessResponse(request, parameters); |
| 612 | } |
| 613 | |
| 614 | NFD_LOG_DEBUG("Applying " << updates.size() << " updates to FIB"); |
| 615 | |
| 616 | // Assign an ID to this FIB transaction |
| 617 | TransactionId currentTransactionId = ++m_lastTransactionId; |
| 618 | |
| 619 | // Add this transaction to the transaction table |
| 620 | m_pendingFibTransactions[currentTransactionId] = updates.size(); |
| 621 | |
| 622 | for (Rib::FibUpdateList::const_iterator it = updates.begin(); it != updates.end(); ++it) |
| 623 | { |
| 624 | shared_ptr<const FibUpdate> update(*it); |
| 625 | |
| 626 | if (update->action == FibUpdate::ADD_NEXTHOP) |
| 627 | { |
| 628 | FaceEntry faceEntry; |
| 629 | faceEntry.faceId = update->faceId; |
| 630 | faceEntry.cost = update->cost; |
| 631 | |
| 632 | m_nfdController.start<ndn::nfd::FibAddNextHopCommand>( |
| 633 | ControlParameters() |
| 634 | .setName(update->name) |
| 635 | .setFaceId(faceEntry.faceId) |
| 636 | .setCost(faceEntry.cost), |
| 637 | bind(&RibManager::onAddNextHopSuccess, this, request, |
| 638 | parameters, |
| 639 | currentTransactionId, |
| 640 | shouldWaitToRespond), |
| 641 | bind(&RibManager::onAddNextHopError, this, _1, _2, request, currentTransactionId, |
| 642 | shouldWaitToRespond)); |
| 643 | } |
| 644 | else if (update->action == FibUpdate::REMOVE_NEXTHOP) |
| 645 | { |
| 646 | FaceEntry faceEntry; |
| 647 | faceEntry.faceId = update->faceId; |
| 648 | |
| 649 | m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>( |
| 650 | ControlParameters() |
| 651 | .setName(update->name) |
| 652 | .setFaceId(faceEntry.faceId), |
| 653 | bind(&RibManager::onRemoveNextHopSuccess, this, request, |
| 654 | parameters, |
| 655 | currentTransactionId, |
| 656 | shouldWaitToRespond), |
| 657 | bind(&RibManager::onRemoveNextHopError, this, _1, _2, request, currentTransactionId, |
| 658 | shouldWaitToRespond)); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | m_managedRib.clearFibUpdates(); |
| 663 | } |
| 664 | |
| 665 | void |
| 666 | RibManager::sendUpdatesToFibAfterFaceDestroyEvent() |
| 667 | { |
| 668 | ControlParameters parameters; |
| 669 | parameters.setOrigin(ndn::nfd::ROUTE_ORIGIN_STATIC); |
| 670 | |
| 671 | sendUpdatesToFib(shared_ptr<const Interest>(), parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | } // namespace rib |
| 675 | } // namespace nfd |