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