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