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" |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 30 | #include <ndn-cxx/management/nfd-face-status.hpp> |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | namespace rib { |
| 34 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 35 | NFD_LOG_INIT("RibManager"); |
| 36 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 37 | const Name RibManager::COMMAND_PREFIX = "/localhost/nfd/rib"; |
| 38 | const Name RibManager::REMOTE_COMMAND_PREFIX = "/localhop/nfd/rib"; |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 39 | const Name RibManager::FACES_LIST_DATASET_PREFIX = "/localhost/nfd/faces/list"; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 40 | |
| 41 | const size_t RibManager::COMMAND_UNSIGNED_NCOMPS = |
| 42 | RibManager::COMMAND_PREFIX.size() + |
| 43 | 1 + // verb |
| 44 | 1; // verb options |
| 45 | |
| 46 | const size_t RibManager::COMMAND_SIGNED_NCOMPS = |
| 47 | RibManager::COMMAND_UNSIGNED_NCOMPS + |
| 48 | 4; // (timestamp, nonce, signed info tlv, signature tlv) |
| 49 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 50 | const RibManager::SignedVerbAndProcessor RibManager::SIGNED_COMMAND_VERBS[] = |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 51 | { |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 52 | SignedVerbAndProcessor( |
| 53 | Name::Component("register"), |
| 54 | &RibManager::registerEntry |
| 55 | ), |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 56 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 57 | SignedVerbAndProcessor( |
| 58 | Name::Component("unregister"), |
| 59 | &RibManager::unregisterEntry |
| 60 | ), |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 63 | const RibManager::UnsignedVerbAndProcessor RibManager::UNSIGNED_COMMAND_VERBS[] = |
| 64 | { |
| 65 | UnsignedVerbAndProcessor( |
| 66 | Name::Component("list"), |
| 67 | &RibManager::listEntries |
| 68 | ), |
| 69 | }; |
| 70 | |
| 71 | const Name RibManager::LIST_COMMAND_PREFIX("/localhost/nfd/rib/list"); |
| 72 | const size_t RibManager::LIST_COMMAND_NCOMPS = LIST_COMMAND_PREFIX.size(); |
| 73 | |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 74 | RibManager::RibManager(ndn::Face& face) |
| 75 | : m_face(face) |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 76 | , m_nfdController(m_face) |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 77 | , m_localhostValidator(m_face) |
| 78 | , m_localhopValidator(m_face) |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 79 | , m_faceMonitor(m_face) |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 80 | , m_isLocalhopEnabled(false) |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 81 | , m_ribStatusPublisher(m_managedRib, face, LIST_COMMAND_PREFIX, m_keyChain) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 82 | , m_lastTransactionId(0) |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 83 | , m_signedVerbDispatch(SIGNED_COMMAND_VERBS, |
| 84 | SIGNED_COMMAND_VERBS + |
| 85 | (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor))) |
| 86 | , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS, |
| 87 | UNSIGNED_COMMAND_VERBS + |
| 88 | (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor))) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 89 | { |
| 90 | } |
| 91 | |
| 92 | void |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 93 | RibManager::startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest) |
| 94 | { |
| 95 | NFD_LOG_INFO("Listening on: " << commandPrefix); |
| 96 | |
| 97 | m_nfdController.start<ndn::nfd::FibAddNextHopCommand>( |
| 98 | ControlParameters() |
| 99 | .setName(commandPrefix) |
| 100 | .setFaceId(0), |
| 101 | bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(commandPrefix)), |
| 102 | bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(commandPrefix), _2)); |
| 103 | |
| 104 | m_face.setInterestFilter(commandPrefix, onRequest); |
| 105 | } |
| 106 | |
| 107 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 108 | RibManager::registerWithNfd() |
| 109 | { |
| 110 | //check whether the components of localhop and localhost prefixes are same |
| 111 | BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size()); |
| 112 | |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 113 | this->startListening(COMMAND_PREFIX, bind(&RibManager::onLocalhostRequest, this, _2)); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 114 | |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 115 | if (m_isLocalhopEnabled) { |
| 116 | this->startListening(REMOTE_COMMAND_PREFIX, |
| 117 | bind(&RibManager::onLocalhopRequest, this, _2)); |
| 118 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 120 | NFD_LOG_INFO("Start monitoring face create/destroy events"); |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 121 | m_faceMonitor.onNotification += bind(&RibManager::onNotification, this, _1); |
| 122 | m_faceMonitor.start(); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 123 | |
| 124 | fetchActiveFaces(); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
| 128 | RibManager::setConfigFile(ConfigFile& configFile) |
| 129 | { |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 130 | configFile.addSectionHandler("rib", |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 131 | bind(&RibManager::onConfig, this, _1, _2, _3)); |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | RibManager::onConfig(const ConfigSection& configSection, |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 136 | bool isDryRun, |
| 137 | const std::string& filename) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 138 | { |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 139 | for (ConfigSection::const_iterator i = configSection.begin(); |
| 140 | i != configSection.end(); ++i) |
| 141 | { |
| 142 | if (i->first == "localhost_security") |
| 143 | m_localhostValidator.load(i->second, filename); |
| 144 | else if (i->first == "localhop_security") |
| 145 | { |
| 146 | m_localhopValidator.load(i->second, filename); |
| 147 | m_isLocalhopEnabled = true; |
| 148 | } |
| 149 | else |
| 150 | throw Error("Unrecognized rib property: " + i->first); |
| 151 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 155 | RibManager::sendResponse(const Name& name, |
| 156 | const ControlResponse& response) |
| 157 | { |
| 158 | const Block& encodedControl = response.wireEncode(); |
| 159 | |
Alexander Afanasyev | 97a9c2c | 2014-07-18 16:57:57 -0700 | [diff] [blame] | 160 | shared_ptr<Data> responseData = make_shared<Data>(name); |
| 161 | responseData->setContent(encodedControl); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 162 | |
Alexander Afanasyev | 97a9c2c | 2014-07-18 16:57:57 -0700 | [diff] [blame] | 163 | m_keyChain.sign(*responseData); |
| 164 | m_face.put(*responseData); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void |
| 168 | RibManager::sendResponse(const Name& name, |
| 169 | uint32_t code, |
| 170 | const std::string& text) |
| 171 | { |
| 172 | ControlResponse response(code, text); |
| 173 | sendResponse(name, response); |
| 174 | } |
| 175 | |
| 176 | void |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 177 | RibManager::onLocalhostRequest(const Interest& request) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 178 | { |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 179 | const Name& command = request.getName(); |
| 180 | const Name::Component& verb = command.get(COMMAND_PREFIX.size()); |
| 181 | |
| 182 | UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb); |
| 183 | |
| 184 | if (unsignedVerbProcessor != m_unsignedVerbDispatch.end()) |
| 185 | { |
| 186 | NFD_LOG_DEBUG("command result: processing unsigned verb: " << verb); |
| 187 | (unsignedVerbProcessor->second)(this, request); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | m_localhostValidator.validate(request, |
| 192 | bind(&RibManager::onCommandValidated, this, _1), |
| 193 | bind(&RibManager::onCommandValidationFailed, this, _1, _2)); |
| 194 | } |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void |
| 198 | RibManager::onLocalhopRequest(const Interest& request) |
| 199 | { |
| 200 | m_localhopValidator.validate(request, |
| 201 | bind(&RibManager::onCommandValidated, this, _1), |
| 202 | bind(&RibManager::onCommandValidationFailed, this, _1, _2)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 206 | RibManager::onCommandValidated(const shared_ptr<const Interest>& request) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 207 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 208 | // REMOTE_COMMAND_PREFIX number of componenets are same as |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 209 | // NRD_COMMAND_PREFIX's so no extra checks are required. |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 210 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 211 | const Name& command = request->getName(); |
| 212 | const Name::Component& verb = command[COMMAND_PREFIX.size()]; |
| 213 | const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
| 214 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 215 | SignedVerbDispatchTable::const_iterator verbProcessor = m_signedVerbDispatch.find(verb); |
| 216 | if (verbProcessor != m_signedVerbDispatch.end()) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 217 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 218 | ControlParameters parameters; |
| 219 | if (!extractParameters(parameterComponent, parameters)) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 220 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 221 | NFD_LOG_DEBUG("command result: malformed verb: " << verb); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 222 | if (static_cast<bool>(request)) |
| 223 | sendResponse(command, 400, "Malformed command"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 227 | NFD_LOG_DEBUG("command result: processing verb: " << verb); |
| 228 | (verbProcessor->second)(this, request, parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 229 | } |
| 230 | else |
| 231 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 232 | NFD_LOG_DEBUG("Unsupported command: " << verb); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 233 | if (static_cast<bool>(request)) |
| 234 | sendResponse(request->getName(), 501, "Unsupported command"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 239 | RibManager::registerEntry(const shared_ptr<const Interest>& request, |
| 240 | ControlParameters& parameters) |
| 241 | { |
| 242 | ndn::nfd::RibRegisterCommand command; |
| 243 | |
| 244 | if (!validateParameters(command, parameters)) |
| 245 | { |
| 246 | NFD_LOG_DEBUG("register result: FAIL reason: malformed"); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 247 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 248 | if (static_cast<bool>(request)) |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 249 | { |
| 250 | sendResponse(request->getName(), 400, "Malformed command"); |
| 251 | } |
| 252 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | |
Alexander Afanasyev | 483efd1 | 2014-08-14 10:51:18 -0700 | [diff] [blame] | 256 | bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0); |
| 257 | if (isSelfRegistration) |
Alexander Afanasyev | fb1c808 | 2014-07-17 15:16:15 -0700 | [diff] [blame] | 258 | { |
| 259 | parameters.setFaceId(request->getIncomingFaceId()); |
| 260 | } |
| 261 | |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 262 | // Is the face valid? |
Alexander Afanasyev | 483efd1 | 2014-08-14 10:51:18 -0700 | [diff] [blame] | 263 | // Issue 1852: There is no need to check (and it can easily fail) for self-registrations |
| 264 | if (!isSelfRegistration && activeFaces.find(parameters.getFaceId()) == activeFaces.end()) |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 265 | { |
| 266 | NFD_LOG_DEBUG("register result: FAIL reason: unknown faceId"); |
| 267 | |
| 268 | if (static_cast<bool>(request)) |
| 269 | { |
| 270 | sendResponse(request->getName(), 410, "Face not found"); |
| 271 | } |
| 272 | |
| 273 | return; |
| 274 | } |
| 275 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 276 | FaceEntry faceEntry; |
| 277 | faceEntry.faceId = parameters.getFaceId(); |
| 278 | faceEntry.origin = parameters.getOrigin(); |
| 279 | faceEntry.cost = parameters.getCost(); |
| 280 | faceEntry.flags = parameters.getFlags(); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 281 | |
Alexander Afanasyev | f67cf08 | 2014-07-18 16:47:29 -0700 | [diff] [blame] | 282 | if (parameters.hasExpirationPeriod() && |
| 283 | parameters.getExpirationPeriod() != time::milliseconds::max()) |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 284 | { |
| 285 | faceEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod(); |
| 286 | |
| 287 | // Schedule a new event, the old one will be cancelled during rib insertion. |
| 288 | EventId eventId; |
| 289 | NFD_LOG_TRACE("scheduling unregistration at: " << faceEntry.expires); |
| 290 | eventId = scheduler::schedule(parameters.getExpirationPeriod(), |
| 291 | bind(&RibManager::unregisterEntry, |
| 292 | this, shared_ptr<Interest>(), parameters)); |
| 293 | |
| 294 | //set the NewEventId of this entry |
| 295 | faceEntry.setExpirationEvent(eventId); |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | faceEntry.expires = time::steady_clock::TimePoint::max(); |
| 300 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 301 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 302 | NFD_LOG_TRACE("register prefix: " << faceEntry); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 303 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 304 | m_managedRib.insert(parameters.getName(), faceEntry); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 305 | |
| 306 | sendUpdatesToFib(request, parameters); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void |
| 310 | RibManager::unregisterEntry(const shared_ptr<const Interest>& request, |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 311 | ControlParameters& params) |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 312 | { |
Alexander Afanasyev | ce7520e | 2014-04-28 09:40:06 -0700 | [diff] [blame] | 313 | ndn::nfd::RibUnregisterCommand command; |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 314 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 315 | //passing all parameters gives error in validation. |
| 316 | //so passing only the required arguments. |
| 317 | ControlParameters parameters; |
| 318 | parameters.setName(params.getName()); |
Alexander Afanasyev | b609f00 | 2014-07-18 16:56:34 -0700 | [diff] [blame] | 319 | if (params.hasFaceId()) |
| 320 | parameters.setFaceId(params.getFaceId()); |
| 321 | if (params.hasOrigin()) |
| 322 | parameters.setOrigin(params.getOrigin()); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 323 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 324 | if (!validateParameters(command, parameters)) |
| 325 | { |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 326 | NFD_LOG_DEBUG("unregister result: FAIL reason: malformed"); |
| 327 | if (static_cast<bool>(request)) |
| 328 | sendResponse(request->getName(), 400, "Malformed command"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
Alexander Afanasyev | 483efd1 | 2014-08-14 10:51:18 -0700 | [diff] [blame] | 332 | bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0); |
| 333 | if (isSelfRegistration) |
Alexander Afanasyev | fb1c808 | 2014-07-17 15:16:15 -0700 | [diff] [blame] | 334 | { |
| 335 | parameters.setFaceId(request->getIncomingFaceId()); |
| 336 | } |
| 337 | |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 338 | // Is the face valid? |
Alexander Afanasyev | 483efd1 | 2014-08-14 10:51:18 -0700 | [diff] [blame] | 339 | // Issue 1852: There is no need to check (and it can easily fail) for self-registrations |
| 340 | if (!isSelfRegistration && activeFaces.find(parameters.getFaceId()) == activeFaces.end()) |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 341 | { |
| 342 | NFD_LOG_DEBUG("register result: FAIL reason: unknown faceId"); |
| 343 | |
| 344 | if (static_cast<bool>(request)) |
| 345 | { |
| 346 | sendResponse(request->getName(), 410, "Face not found"); |
| 347 | } |
| 348 | |
| 349 | return; |
| 350 | } |
| 351 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 352 | FaceEntry faceEntry; |
| 353 | faceEntry.faceId = parameters.getFaceId(); |
| 354 | faceEntry.origin = parameters.getOrigin(); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 355 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 356 | NFD_LOG_TRACE("unregister prefix: " << faceEntry); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 357 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 358 | m_managedRib.erase(parameters.getName(), faceEntry); |
| 359 | |
| 360 | sendUpdatesToFib(request, parameters); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void |
| 364 | RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request, |
| 365 | const std::string& failureInfo) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 366 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 367 | NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 368 | if (static_cast<bool>(request)) |
| 369 | sendResponse(request->getName(), 403, failureInfo); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 372 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 373 | bool |
| 374 | RibManager::extractParameters(const Name::Component& parameterComponent, |
| 375 | ControlParameters& extractedParameters) |
| 376 | { |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 377 | try |
| 378 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 379 | Block rawParameters = parameterComponent.blockFromValue(); |
| 380 | extractedParameters.wireDecode(rawParameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 381 | } |
| 382 | catch (const ndn::Tlv::Error& e) |
| 383 | { |
| 384 | return false; |
| 385 | } |
| 386 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 387 | NFD_LOG_DEBUG("Parameters parsed OK"); |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | bool |
| 392 | RibManager::validateParameters(const ControlCommand& command, |
| 393 | ControlParameters& parameters) |
| 394 | { |
| 395 | try |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 396 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 397 | command.validateRequest(parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 398 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 399 | catch (const ControlCommand::ArgumentError&) |
| 400 | { |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | command.applyDefaultsToRequest(parameters); |
| 405 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 406 | return true; |
| 407 | } |
| 408 | |
| 409 | void |
| 410 | RibManager::onCommandError(uint32_t code, const std::string& error, |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 411 | const shared_ptr<const Interest>& request, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 412 | const FaceEntry& faceEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 413 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 414 | NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 415 | |
| 416 | ControlResponse response; |
| 417 | |
| 418 | if (code == 404) |
| 419 | { |
| 420 | response.setCode(code); |
| 421 | response.setText(error); |
| 422 | } |
| 423 | else |
| 424 | { |
| 425 | response.setCode(533); |
| 426 | std::ostringstream os; |
| 427 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 428 | response.setText(os.str()); |
| 429 | } |
| 430 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 431 | if (static_cast<bool>(request)) |
| 432 | sendResponse(request->getName(), response); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 436 | RibManager::onRegSuccess(const shared_ptr<const Interest>& request, |
| 437 | const ControlParameters& parameters, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 438 | const FaceEntry& faceEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 439 | { |
| 440 | ControlResponse response; |
| 441 | |
| 442 | response.setCode(200); |
| 443 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 444 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 445 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 446 | NFD_LOG_TRACE("onRegSuccess: registered " << faceEntry); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 447 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 448 | if (static_cast<bool>(request)) |
| 449 | sendResponse(request->getName(), response); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 452 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 453 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 454 | RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request, |
| 455 | const ControlParameters& parameters, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 456 | const FaceEntry& faceEntry) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 457 | { |
| 458 | ControlResponse response; |
| 459 | |
| 460 | response.setCode(200); |
| 461 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 462 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 463 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 464 | NFD_LOG_TRACE("onUnRegSuccess: unregistered " << faceEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 465 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 466 | if (static_cast<bool>(request)) |
| 467 | sendResponse(request->getName(), response); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | void |
| 471 | RibManager::sendSuccessResponse(const shared_ptr<const Interest>& request, |
| 472 | const ControlParameters& parameters) |
| 473 | { |
| 474 | if (!static_cast<bool>(request)) |
| 475 | { |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | ControlResponse response; |
| 480 | |
| 481 | response.setCode(200); |
| 482 | response.setText("Success"); |
| 483 | response.setBody(parameters.wireEncode()); |
| 484 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 485 | if (static_cast<bool>(request)) |
| 486 | sendResponse(request->getName(), response); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void |
| 490 | RibManager::sendErrorResponse(uint32_t code, const std::string& error, |
| 491 | const shared_ptr<const Interest>& request) |
| 492 | { |
| 493 | NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")"); |
| 494 | |
| 495 | if (!static_cast<bool>(request)) |
| 496 | { |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | ControlResponse response; |
| 501 | |
| 502 | if (code == 404) |
| 503 | { |
| 504 | response.setCode(code); |
| 505 | response.setText(error); |
| 506 | } |
| 507 | else |
| 508 | { |
| 509 | response.setCode(533); |
| 510 | std::ostringstream os; |
| 511 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 512 | response.setText(os.str()); |
| 513 | } |
| 514 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 515 | if (static_cast<bool>(request)) |
| 516 | sendResponse(request->getName(), response); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 519 | void |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 520 | RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix) |
| 521 | { |
| 522 | NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD"); |
| 523 | } |
| 524 | |
| 525 | void |
| 526 | RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg) |
| 527 | { |
| 528 | throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg); |
| 529 | } |
| 530 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 531 | bool |
| 532 | RibManager::isTransactionComplete(const TransactionId transactionId) |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 533 | { |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 534 | FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId); |
| 535 | |
| 536 | if (it != m_pendingFibTransactions.end()) |
| 537 | { |
| 538 | int& updatesLeft = it->second; |
| 539 | |
| 540 | updatesLeft--; |
| 541 | |
| 542 | // All of the updates have been applied successfully |
| 543 | if (updatesLeft == 0) |
| 544 | { |
| 545 | m_pendingFibTransactions.erase(it); |
| 546 | return true; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | return false; |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 554 | RibManager::invalidateTransaction(const TransactionId transactionId) |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 555 | { |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 556 | FibTransactionTable::iterator it = m_pendingFibTransactions.find(transactionId); |
| 557 | |
| 558 | if (it != m_pendingFibTransactions.end()) |
| 559 | { |
| 560 | m_pendingFibTransactions.erase(it); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | void |
| 565 | RibManager::onAddNextHopSuccess(const shared_ptr<const Interest>& request, |
| 566 | const ControlParameters& parameters, |
| 567 | const TransactionId transactionId, |
| 568 | const bool shouldSendResponse) |
| 569 | { |
| 570 | if (isTransactionComplete(transactionId) && shouldSendResponse) |
| 571 | { |
| 572 | sendSuccessResponse(request, parameters); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | void |
| 577 | RibManager::onAddNextHopError(uint32_t code, const std::string& error, |
| 578 | const shared_ptr<const Interest>& request, |
| 579 | const TransactionId transactionId, const bool shouldSendResponse) |
| 580 | { |
| 581 | invalidateTransaction(transactionId); |
| 582 | |
| 583 | if (shouldSendResponse) |
| 584 | { |
| 585 | sendErrorResponse(code, error, request); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | void |
| 590 | RibManager::onRemoveNextHopSuccess(const shared_ptr<const Interest>& request, |
| 591 | const ControlParameters& parameters, |
| 592 | const TransactionId transactionId, |
| 593 | const bool shouldSendResponse) |
| 594 | { |
| 595 | if (isTransactionComplete(transactionId) && shouldSendResponse) |
| 596 | { |
| 597 | sendSuccessResponse(request, parameters); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | void |
| 602 | RibManager::onRemoveNextHopError(uint32_t code, const std::string& error, |
| 603 | const shared_ptr<const Interest>& request, |
| 604 | const TransactionId transactionId, const bool shouldSendResponse) |
| 605 | { |
| 606 | invalidateTransaction(transactionId); |
| 607 | |
| 608 | if (shouldSendResponse) |
| 609 | { |
| 610 | sendErrorResponse(code, error, request); |
| 611 | } |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 615 | RibManager::onControlHeaderSuccess() |
| 616 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 617 | NFD_LOG_DEBUG("Local control header enabled"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | void |
| 621 | RibManager::onControlHeaderError(uint32_t code, const std::string& reason) |
| 622 | { |
Alexander Afanasyev | b305165 | 2014-04-30 17:50:26 -0700 | [diff] [blame] | 623 | std::ostringstream os; |
| 624 | os << "Couldn't enable local control header " |
| 625 | << "(code: " << code << ", info: " << reason << ")"; |
| 626 | throw Error(os.str()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | void |
| 630 | RibManager::enableLocalControlHeader() |
| 631 | { |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 632 | m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>( |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 633 | ControlParameters() |
| 634 | .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID), |
| 635 | bind(&RibManager::onControlHeaderSuccess, this), |
| 636 | bind(&RibManager::onControlHeaderError, this, _1, _2)); |
| 637 | } |
| 638 | |
| 639 | void |
| 640 | RibManager::onNotification(const FaceEventNotification& notification) |
| 641 | { |
| 642 | /// \todo A notification can be missed, in this case check Facelist |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 643 | NFD_LOG_TRACE("onNotification: " << notification); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 644 | |
| 645 | if (notification.getKind() == ndn::nfd::FACE_EVENT_CREATED) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 646 | { |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 647 | NFD_LOG_DEBUG("Received notification for created faceId: " << notification.getFaceId()); |
| 648 | activeFaces.insert(notification.getFaceId()); |
| 649 | } |
| 650 | else if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) |
| 651 | { |
| 652 | NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId()); |
| 653 | activeFaces.erase(notification.getFaceId()); |
| 654 | |
Alexander Afanasyev | 63108c4 | 2014-07-07 19:10:47 -0700 | [diff] [blame] | 655 | scheduler::schedule(time::seconds(0), |
| 656 | bind(&RibManager::processErasureAfterNotification, this, |
| 657 | notification.getFaceId())); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | void |
Alexander Afanasyev | 63108c4 | 2014-07-07 19:10:47 -0700 | [diff] [blame] | 662 | RibManager::processErasureAfterNotification(uint64_t faceId) |
| 663 | { |
| 664 | m_managedRib.erase(faceId); |
| 665 | |
| 666 | sendUpdatesToFibAfterFaceDestroyEvent(); |
| 667 | } |
| 668 | |
| 669 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 670 | RibManager::sendUpdatesToFib(const shared_ptr<const Interest>& request, |
| 671 | const ControlParameters& parameters) |
| 672 | { |
| 673 | const Rib::FibUpdateList& updates = m_managedRib.getFibUpdates(); |
| 674 | |
| 675 | // If no updates were generated, consider the operation a success |
| 676 | if (updates.empty()) |
| 677 | { |
| 678 | sendSuccessResponse(request, parameters); |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | bool shouldWaitToRespond = false; |
| 683 | |
| 684 | // An application request should wait for all FIB updates to be applied |
| 685 | // successfully before sending a response |
| 686 | if (parameters.getOrigin() == ndn::nfd::ROUTE_ORIGIN_APP) |
| 687 | { |
| 688 | shouldWaitToRespond = true; |
| 689 | } |
| 690 | else // Respond immediately |
| 691 | { |
| 692 | sendSuccessResponse(request, parameters); |
| 693 | } |
| 694 | |
| 695 | NFD_LOG_DEBUG("Applying " << updates.size() << " updates to FIB"); |
| 696 | |
| 697 | // Assign an ID to this FIB transaction |
| 698 | TransactionId currentTransactionId = ++m_lastTransactionId; |
| 699 | |
| 700 | // Add this transaction to the transaction table |
| 701 | m_pendingFibTransactions[currentTransactionId] = updates.size(); |
| 702 | |
| 703 | for (Rib::FibUpdateList::const_iterator it = updates.begin(); it != updates.end(); ++it) |
| 704 | { |
| 705 | shared_ptr<const FibUpdate> update(*it); |
| 706 | |
| 707 | if (update->action == FibUpdate::ADD_NEXTHOP) |
| 708 | { |
| 709 | FaceEntry faceEntry; |
| 710 | faceEntry.faceId = update->faceId; |
| 711 | faceEntry.cost = update->cost; |
| 712 | |
| 713 | m_nfdController.start<ndn::nfd::FibAddNextHopCommand>( |
| 714 | ControlParameters() |
| 715 | .setName(update->name) |
| 716 | .setFaceId(faceEntry.faceId) |
| 717 | .setCost(faceEntry.cost), |
| 718 | bind(&RibManager::onAddNextHopSuccess, this, request, |
| 719 | parameters, |
| 720 | currentTransactionId, |
| 721 | shouldWaitToRespond), |
| 722 | bind(&RibManager::onAddNextHopError, this, _1, _2, request, currentTransactionId, |
| 723 | shouldWaitToRespond)); |
| 724 | } |
| 725 | else if (update->action == FibUpdate::REMOVE_NEXTHOP) |
| 726 | { |
| 727 | FaceEntry faceEntry; |
| 728 | faceEntry.faceId = update->faceId; |
| 729 | |
| 730 | m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>( |
| 731 | ControlParameters() |
| 732 | .setName(update->name) |
| 733 | .setFaceId(faceEntry.faceId), |
| 734 | bind(&RibManager::onRemoveNextHopSuccess, this, request, |
| 735 | parameters, |
| 736 | currentTransactionId, |
| 737 | shouldWaitToRespond), |
| 738 | bind(&RibManager::onRemoveNextHopError, this, _1, _2, request, currentTransactionId, |
| 739 | shouldWaitToRespond)); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | m_managedRib.clearFibUpdates(); |
| 744 | } |
| 745 | |
| 746 | void |
| 747 | RibManager::sendUpdatesToFibAfterFaceDestroyEvent() |
| 748 | { |
| 749 | ControlParameters parameters; |
| 750 | parameters.setOrigin(ndn::nfd::ROUTE_ORIGIN_STATIC); |
| 751 | |
| 752 | sendUpdatesToFib(shared_ptr<const Interest>(), parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 755 | void |
| 756 | RibManager::listEntries(const Interest& request) |
| 757 | { |
| 758 | const Name& command = request.getName(); |
| 759 | const size_t commandNComps = command.size(); |
| 760 | |
| 761 | if (commandNComps < LIST_COMMAND_NCOMPS || |
| 762 | !LIST_COMMAND_PREFIX.isPrefixOf(command)) |
| 763 | { |
| 764 | NFD_LOG_DEBUG("command result: malformed"); |
| 765 | sendResponse(command, 400, "Malformed command"); |
| 766 | return; |
| 767 | } |
| 768 | |
| 769 | m_ribStatusPublisher.publish(); |
| 770 | } |
| 771 | |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 772 | void |
| 773 | RibManager::fetchActiveFaces() |
| 774 | { |
| 775 | NFD_LOG_DEBUG("Fetching active faces"); |
| 776 | |
| 777 | Interest interest(FACES_LIST_DATASET_PREFIX); |
| 778 | interest.setChildSelector(1); |
| 779 | interest.setMustBeFresh(true); |
| 780 | |
| 781 | shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>(); |
| 782 | |
| 783 | m_face.expressInterest(interest, |
| 784 | bind(&RibManager::fetchSegments, this, _2, buffer), |
| 785 | bind(&RibManager::onFetchFaceStatusTimeout, this)); |
| 786 | } |
| 787 | |
| 788 | void |
| 789 | RibManager::fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer) |
| 790 | { |
| 791 | buffer->write(reinterpret_cast<const char*>(data.getContent().value()), |
| 792 | data.getContent().value_size()); |
| 793 | |
| 794 | uint64_t currentSegment = data.getName().get(-1).toSegment(); |
| 795 | |
| 796 | const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId(); |
| 797 | if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment) |
| 798 | { |
| 799 | m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1), |
| 800 | bind(&RibManager::fetchSegments, this, _2, buffer), |
| 801 | bind(&RibManager::onFetchFaceStatusTimeout, this)); |
| 802 | } |
| 803 | else |
| 804 | { |
| 805 | updateActiveFaces(buffer); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | void |
| 810 | RibManager::updateActiveFaces(shared_ptr<ndn::OBufferStream> buffer) |
| 811 | { |
| 812 | NFD_LOG_DEBUG("Updating active faces"); |
| 813 | |
| 814 | ndn::ConstBufferPtr buf = buffer->buf(); |
| 815 | |
| 816 | Block block; |
| 817 | size_t offset = 0; |
| 818 | |
| 819 | while (offset < buf->size()) |
| 820 | { |
| 821 | if (!Block::fromBuffer(buf, offset, block)) |
| 822 | { |
| 823 | std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl; |
| 824 | break; |
| 825 | } |
| 826 | |
| 827 | offset += block.size(); |
| 828 | |
| 829 | ndn::nfd::FaceStatus status(block); |
| 830 | |
| 831 | NFD_LOG_DEBUG("Adding faceId: " << status.getFaceId() << " to activeFaces"); |
| 832 | activeFaces.insert(status.getFaceId()); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | void |
| 837 | RibManager::onFetchFaceStatusTimeout() |
| 838 | { |
| 839 | std::cerr << "Face Status Dataset request timed out" << std::endl; |
| 840 | } |
| 841 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 842 | } // namespace rib |
| 843 | } // namespace nfd |