Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, 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 | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 85 | , m_fibUpdater(m_managedRib, m_nfdController) |
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), |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 109 | bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(commandPrefix), _1), |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 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 | fbf7834 | 2015-01-23 14:46:41 -0700 | [diff] [blame] | 129 | m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1)); |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 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 | { |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 147 | bool isRemoteRegisterEnabled = false; |
| 148 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 149 | for (const auto& item : configSection) { |
| 150 | if (item.first == "localhost_security") { |
| 151 | m_localhostValidator.load(item.second, filename); |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 152 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 153 | else if (item.first == "localhop_security") { |
| 154 | m_localhopValidator.load(item.second, filename); |
| 155 | m_isLocalhopEnabled = true; |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 156 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 157 | else if (item.first == "remote_register") { |
| 158 | m_remoteRegistrator.loadConfig(item.second); |
| 159 | isRemoteRegisterEnabled = true; |
| 160 | |
| 161 | // Avoid other actions when isDryRun == true |
| 162 | if (isDryRun) { |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | m_remoteRegistrator.enable(); |
| 167 | } |
| 168 | else { |
| 169 | throw Error("Unrecognized rib property: " + item.first); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (!isRemoteRegisterEnabled) { |
| 174 | m_remoteRegistrator.disable(); |
| 175 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 179 | RibManager::sendResponse(const Name& name, |
| 180 | const ControlResponse& response) |
| 181 | { |
| 182 | const Block& encodedControl = response.wireEncode(); |
| 183 | |
Alexander Afanasyev | 97a9c2c | 2014-07-18 16:57:57 -0700 | [diff] [blame] | 184 | shared_ptr<Data> responseData = make_shared<Data>(name); |
| 185 | responseData->setContent(encodedControl); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 186 | |
Alexander Afanasyev | 97a9c2c | 2014-07-18 16:57:57 -0700 | [diff] [blame] | 187 | m_keyChain.sign(*responseData); |
| 188 | m_face.put(*responseData); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void |
| 192 | RibManager::sendResponse(const Name& name, |
| 193 | uint32_t code, |
| 194 | const std::string& text) |
| 195 | { |
| 196 | ControlResponse response(code, text); |
| 197 | sendResponse(name, response); |
| 198 | } |
| 199 | |
| 200 | void |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 201 | RibManager::onLocalhostRequest(const Interest& request) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 202 | { |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 203 | const Name& command = request.getName(); |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 204 | const Name::Component& verb = command.get(COMMAND_PREFIX.size()); |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 205 | |
| 206 | UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb); |
| 207 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 208 | if (unsignedVerbProcessor != m_unsignedVerbDispatch.end()) { |
| 209 | NFD_LOG_DEBUG("command result: processing unsigned verb: " << verb); |
| 210 | (unsignedVerbProcessor->second)(this, request); |
| 211 | } |
| 212 | else { |
| 213 | m_localhostValidator.validate(request, |
| 214 | bind(&RibManager::onCommandValidated, this, _1), |
| 215 | bind(&RibManager::onCommandValidationFailed, this, _1, _2)); |
| 216 | } |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void |
| 220 | RibManager::onLocalhopRequest(const Interest& request) |
| 221 | { |
| 222 | m_localhopValidator.validate(request, |
| 223 | bind(&RibManager::onCommandValidated, this, _1), |
| 224 | bind(&RibManager::onCommandValidationFailed, this, _1, _2)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 228 | RibManager::onCommandValidated(const shared_ptr<const Interest>& request) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 229 | { |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 230 | // REMOTE_COMMAND_PREFIX number of componenets are same as |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 231 | // NRD_COMMAND_PREFIX's so no extra checks are required. |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 232 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 233 | const Name& command = request->getName(); |
| 234 | const Name::Component& verb = command[COMMAND_PREFIX.size()]; |
| 235 | const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
| 236 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 237 | SignedVerbDispatchTable::const_iterator verbProcessor = m_signedVerbDispatch.find(verb); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 238 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 239 | if (verbProcessor != m_signedVerbDispatch.end()) { |
| 240 | |
| 241 | ControlParameters parameters; |
| 242 | if (!extractParameters(parameterComponent, parameters)) { |
| 243 | NFD_LOG_DEBUG("command result: malformed verb: " << verb); |
| 244 | |
| 245 | if (static_cast<bool>(request)) { |
| 246 | sendResponse(command, 400, "Malformed command"); |
| 247 | } |
| 248 | |
| 249 | return; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 250 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 251 | |
| 252 | NFD_LOG_DEBUG("command result: processing verb: " << verb); |
| 253 | (verbProcessor->second)(this, request, parameters); |
| 254 | } |
| 255 | else { |
| 256 | NFD_LOG_DEBUG("Unsupported command: " << verb); |
| 257 | |
| 258 | if (static_cast<bool>(request)) { |
| 259 | sendResponse(request->getName(), 501, "Unsupported command"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 260 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 261 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 265 | RibManager::registerEntry(const shared_ptr<const Interest>& request, |
| 266 | ControlParameters& parameters) |
| 267 | { |
| 268 | ndn::nfd::RibRegisterCommand command; |
| 269 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 270 | if (!validateParameters(command, parameters)) { |
| 271 | NFD_LOG_DEBUG("register result: FAIL reason: malformed"); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 272 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 273 | if (static_cast<bool>(request)) { |
| 274 | sendResponse(request->getName(), 400, "Malformed command"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 277 | return; |
| 278 | } |
| 279 | |
Alexander Afanasyev | 483efd1 | 2014-08-14 10:51:18 -0700 | [diff] [blame] | 280 | bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 281 | if (isSelfRegistration) { |
| 282 | parameters.setFaceId(request->getIncomingFaceId()); |
| 283 | } |
| 284 | |
| 285 | // Respond since command is valid and authorized |
| 286 | sendSuccessResponse(request, parameters); |
Alexander Afanasyev | fb1c808 | 2014-07-17 15:16:15 -0700 | [diff] [blame] | 287 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 288 | Route route; |
| 289 | route.faceId = parameters.getFaceId(); |
| 290 | route.origin = parameters.getOrigin(); |
| 291 | route.cost = parameters.getCost(); |
| 292 | route.flags = parameters.getFlags(); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 293 | |
Alexander Afanasyev | f67cf08 | 2014-07-18 16:47:29 -0700 | [diff] [blame] | 294 | if (parameters.hasExpirationPeriod() && |
| 295 | parameters.getExpirationPeriod() != time::milliseconds::max()) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 296 | { |
| 297 | route.expires = time::steady_clock::now() + parameters.getExpirationPeriod(); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 298 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 299 | // Schedule a new event, the old one will be cancelled during rib insertion. |
| 300 | scheduler::EventId eventId = scheduler::schedule(parameters.getExpirationPeriod(), |
| 301 | bind(&Rib::onRouteExpiration, &m_managedRib, parameters.getName(), route)); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 302 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 303 | NFD_LOG_TRACE("Scheduled unregistration at: " << route.expires << |
| 304 | " with EventId: " << eventId); |
| 305 | |
| 306 | // Set the NewEventId of this entry |
| 307 | route.setExpirationEvent(eventId); |
| 308 | } |
| 309 | else { |
| 310 | route.expires = time::steady_clock::TimePoint::max(); |
| 311 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 312 | |
Vince Lehman | ff8b397 | 2015-02-20 16:51:21 -0600 | [diff] [blame] | 313 | NFD_LOG_INFO("Adding route " << parameters.getName() << " nexthop=" << route.faceId |
| 314 | << " origin=" << route.origin |
| 315 | << " cost=" << route.cost); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 316 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 317 | RibUpdate update; |
| 318 | update.setAction(RibUpdate::REGISTER) |
| 319 | .setName(parameters.getName()) |
| 320 | .setRoute(route); |
| 321 | |
| 322 | m_managedRib.beginApplyUpdate(update, |
| 323 | bind(&RibManager::onRibUpdateSuccess, this, update), |
| 324 | bind(&RibManager::onRibUpdateFailure, this, update, _1, _2)); |
| 325 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 326 | m_registeredFaces.insert(route.faceId); |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 330 | RibManager::unregisterEntry(const shared_ptr<const Interest>& request, |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 331 | ControlParameters& params) |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 332 | { |
Alexander Afanasyev | ce7520e | 2014-04-28 09:40:06 -0700 | [diff] [blame] | 333 | ndn::nfd::RibUnregisterCommand command; |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 334 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 335 | // Passing all parameters gives error in validation, |
| 336 | // so passing only the required arguments. |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 337 | ControlParameters parameters; |
| 338 | parameters.setName(params.getName()); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 339 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 340 | if (params.hasFaceId()) { |
| 341 | parameters.setFaceId(params.getFaceId()); |
| 342 | } |
| 343 | |
| 344 | if (params.hasOrigin()) { |
| 345 | parameters.setOrigin(params.getOrigin()); |
| 346 | } |
| 347 | |
| 348 | if (!validateParameters(command, parameters)) { |
| 349 | NFD_LOG_DEBUG("unregister result: FAIL reason: malformed"); |
| 350 | |
| 351 | if (static_cast<bool>(request)) { |
| 352 | sendResponse(request->getName(), 400, "Malformed command"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 355 | return; |
| 356 | } |
| 357 | |
Alexander Afanasyev | 483efd1 | 2014-08-14 10:51:18 -0700 | [diff] [blame] | 358 | bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 359 | if (isSelfRegistration) { |
| 360 | parameters.setFaceId(request->getIncomingFaceId()); |
| 361 | } |
| 362 | |
| 363 | // Respond since command is valid and authorized |
| 364 | sendSuccessResponse(request, parameters); |
Alexander Afanasyev | fb1c808 | 2014-07-17 15:16:15 -0700 | [diff] [blame] | 365 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 366 | Route route; |
| 367 | route.faceId = parameters.getFaceId(); |
| 368 | route.origin = parameters.getOrigin(); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 369 | |
Vince Lehman | ff8b397 | 2015-02-20 16:51:21 -0600 | [diff] [blame] | 370 | NFD_LOG_INFO("Removing route " << parameters.getName() << " nexthop=" << route.faceId |
| 371 | << " origin=" << route.origin); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 372 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 373 | RibUpdate update; |
| 374 | update.setAction(RibUpdate::UNREGISTER) |
| 375 | .setName(parameters.getName()) |
| 376 | .setRoute(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 377 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 378 | m_managedRib.beginApplyUpdate(update, |
| 379 | bind(&RibManager::onRibUpdateSuccess, this, update), |
| 380 | bind(&RibManager::onRibUpdateFailure, this, update, _1, _2)); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void |
| 384 | RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request, |
| 385 | const std::string& failureInfo) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 386 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 387 | NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 388 | |
| 389 | if (static_cast<bool>(request)) { |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 390 | sendResponse(request->getName(), 403, failureInfo); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 391 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 394 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 395 | bool |
| 396 | RibManager::extractParameters(const Name::Component& parameterComponent, |
| 397 | ControlParameters& extractedParameters) |
| 398 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 399 | try { |
| 400 | Block rawParameters = parameterComponent.blockFromValue(); |
| 401 | extractedParameters.wireDecode(rawParameters); |
| 402 | } |
| 403 | catch (const tlv::Error&) { |
| 404 | return false; |
| 405 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 406 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 407 | NFD_LOG_DEBUG("Parameters parsed OK"); |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | bool |
| 412 | RibManager::validateParameters(const ControlCommand& command, |
| 413 | ControlParameters& parameters) |
| 414 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 415 | try { |
| 416 | command.validateRequest(parameters); |
| 417 | } |
| 418 | catch (const ControlCommand::ArgumentError&) { |
| 419 | return false; |
| 420 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 421 | |
| 422 | command.applyDefaultsToRequest(parameters); |
| 423 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 424 | return true; |
| 425 | } |
| 426 | |
| 427 | void |
| 428 | RibManager::onCommandError(uint32_t code, const std::string& error, |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 429 | const shared_ptr<const Interest>& request, |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 430 | const Route& route) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 431 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 432 | NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 433 | |
| 434 | ControlResponse response; |
| 435 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 436 | if (code == 404) { |
| 437 | response.setCode(code); |
| 438 | response.setText(error); |
| 439 | } |
| 440 | else { |
| 441 | response.setCode(533); |
| 442 | std::ostringstream os; |
| 443 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 444 | response.setText(os.str()); |
| 445 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 446 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 447 | if (static_cast<bool>(request)) { |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 448 | sendResponse(request->getName(), response); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 449 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 453 | RibManager::onRegSuccess(const shared_ptr<const Interest>& request, |
| 454 | const ControlParameters& parameters, |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 455 | const Route& route) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 456 | { |
| 457 | ControlResponse response; |
| 458 | |
| 459 | response.setCode(200); |
| 460 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 461 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 462 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 463 | NFD_LOG_TRACE("onRegSuccess: registered " << route); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 464 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 465 | if (static_cast<bool>(request)) { |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 466 | sendResponse(request->getName(), response); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 467 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 470 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 471 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 472 | RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request, |
| 473 | const ControlParameters& parameters, |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 474 | const Route& route) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 475 | { |
| 476 | ControlResponse response; |
| 477 | |
| 478 | response.setCode(200); |
| 479 | response.setText("Success"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 480 | response.setBody(parameters.wireEncode()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 481 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 482 | NFD_LOG_TRACE("onUnRegSuccess: unregistered " << route); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 483 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 484 | if (static_cast<bool>(request)) { |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 485 | sendResponse(request->getName(), response); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 486 | } |
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 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 493 | if (!static_cast<bool>(request)) { |
| 494 | return; |
| 495 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 496 | |
| 497 | ControlResponse response; |
| 498 | |
| 499 | response.setCode(200); |
| 500 | response.setText("Success"); |
| 501 | response.setBody(parameters.wireEncode()); |
| 502 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 503 | if (static_cast<bool>(request)) { |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 504 | sendResponse(request->getName(), response); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 505 | } |
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 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 514 | if (!static_cast<bool>(request)) { |
| 515 | return; |
| 516 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 517 | |
| 518 | ControlResponse response; |
| 519 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 520 | if (code == 404) { |
| 521 | response.setCode(code); |
| 522 | response.setText(error); |
| 523 | } |
| 524 | else { |
| 525 | response.setCode(533); |
| 526 | std::ostringstream os; |
| 527 | os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")"; |
| 528 | response.setText(os.str()); |
| 529 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 530 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 531 | if (static_cast<bool>(request)) { |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 532 | sendResponse(request->getName(), response); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | void |
| 537 | RibManager::onRibUpdateSuccess(const RibUpdate& update) |
| 538 | { |
| 539 | NFD_LOG_DEBUG("RIB update succeeded for " << update); |
| 540 | } |
| 541 | |
| 542 | void |
| 543 | RibManager::onRibUpdateFailure(const RibUpdate& update, uint32_t code, const std::string& error) |
| 544 | { |
| 545 | NFD_LOG_DEBUG("RIB update failed for " << update << " (code: " << code |
| 546 | << ", error: " << error << ")"); |
| 547 | |
| 548 | // Since the FIB rejected the update, clean up invalid routes |
| 549 | scheduleActiveFaceFetch(time::seconds(1)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 552 | void |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 553 | RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix, |
| 554 | const ndn::nfd::ControlParameters& result) |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 555 | { |
| 556 | NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD"); |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 557 | |
| 558 | // Routes must be inserted into the RIB so route flags can be applied |
| 559 | Route route; |
| 560 | route.faceId = result.getFaceId(); |
| 561 | route.origin = ndn::nfd::ROUTE_ORIGIN_APP; |
| 562 | route.expires = time::steady_clock::TimePoint::max(); |
| 563 | route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT; |
| 564 | |
| 565 | m_managedRib.insert(prefix, route); |
| 566 | |
| 567 | m_registeredFaces.insert(route.faceId); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | void |
| 571 | RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg) |
| 572 | { |
| 573 | throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg); |
| 574 | } |
| 575 | |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 576 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 577 | RibManager::onControlHeaderSuccess() |
| 578 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 579 | NFD_LOG_DEBUG("Local control header enabled"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | void |
| 583 | RibManager::onControlHeaderError(uint32_t code, const std::string& reason) |
| 584 | { |
Alexander Afanasyev | b305165 | 2014-04-30 17:50:26 -0700 | [diff] [blame] | 585 | std::ostringstream os; |
| 586 | os << "Couldn't enable local control header " |
| 587 | << "(code: " << code << ", info: " << reason << ")"; |
| 588 | throw Error(os.str()); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | void |
| 592 | RibManager::enableLocalControlHeader() |
| 593 | { |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 594 | m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>( |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 595 | ControlParameters() |
| 596 | .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID), |
| 597 | bind(&RibManager::onControlHeaderSuccess, this), |
| 598 | bind(&RibManager::onControlHeaderError, this, _1, _2)); |
| 599 | } |
| 600 | |
| 601 | void |
| 602 | RibManager::onNotification(const FaceEventNotification& notification) |
| 603 | { |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 604 | NFD_LOG_TRACE("onNotification: " << notification); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 605 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 606 | if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { |
| 607 | NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId()); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 608 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 609 | scheduler::schedule(time::seconds(0), |
| 610 | bind(&RibManager::onFaceDestroyedEvent, this, notification.getFaceId())); |
| 611 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 615 | RibManager::onFaceDestroyedEvent(uint64_t faceId) |
Alexander Afanasyev | 63108c4 | 2014-07-07 19:10:47 -0700 | [diff] [blame] | 616 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 617 | m_managedRib.beginRemoveFace(faceId); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 618 | m_registeredFaces.erase(faceId); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 621 | void |
| 622 | RibManager::listEntries(const Interest& request) |
| 623 | { |
| 624 | const Name& command = request.getName(); |
| 625 | const size_t commandNComps = command.size(); |
| 626 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 627 | if (commandNComps < LIST_COMMAND_NCOMPS || !LIST_COMMAND_PREFIX.isPrefixOf(command)) { |
| 628 | NFD_LOG_DEBUG("command result: malformed"); |
| 629 | |
| 630 | sendResponse(command, 400, "Malformed command"); |
| 631 | return; |
| 632 | } |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 633 | |
| 634 | m_ribStatusPublisher.publish(); |
| 635 | } |
| 636 | |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 637 | void |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 638 | RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait) |
| 639 | { |
| 640 | scheduler::cancel(m_activeFaceFetchEvent); |
| 641 | |
| 642 | m_activeFaceFetchEvent = scheduler::schedule(timeToWait, |
| 643 | bind(&RibManager::fetchActiveFaces, this)); |
| 644 | } |
| 645 | |
| 646 | void |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 647 | RibManager::fetchActiveFaces() |
| 648 | { |
| 649 | NFD_LOG_DEBUG("Fetching active faces"); |
| 650 | |
| 651 | Interest interest(FACES_LIST_DATASET_PREFIX); |
| 652 | interest.setChildSelector(1); |
| 653 | interest.setMustBeFresh(true); |
| 654 | |
| 655 | shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>(); |
| 656 | |
| 657 | m_face.expressInterest(interest, |
| 658 | bind(&RibManager::fetchSegments, this, _2, buffer), |
| 659 | bind(&RibManager::onFetchFaceStatusTimeout, this)); |
| 660 | } |
| 661 | |
| 662 | void |
| 663 | RibManager::fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer) |
| 664 | { |
| 665 | buffer->write(reinterpret_cast<const char*>(data.getContent().value()), |
| 666 | data.getContent().value_size()); |
| 667 | |
| 668 | uint64_t currentSegment = data.getName().get(-1).toSegment(); |
| 669 | |
| 670 | const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId(); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 671 | |
| 672 | if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment) { |
| 673 | m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1), |
| 674 | bind(&RibManager::fetchSegments, this, _2, buffer), |
| 675 | bind(&RibManager::onFetchFaceStatusTimeout, this)); |
| 676 | } |
| 677 | else { |
| 678 | removeInvalidFaces(buffer); |
| 679 | } |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | void |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 683 | RibManager::removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer) |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 684 | { |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 685 | NFD_LOG_DEBUG("Checking for invalid face registrations"); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 686 | |
| 687 | ndn::ConstBufferPtr buf = buffer->buf(); |
| 688 | |
| 689 | Block block; |
| 690 | size_t offset = 0; |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 691 | FaceIdSet activeFaces; |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 692 | |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 693 | while (offset < buf->size()) { |
| 694 | bool isOk = false; |
| 695 | std::tie(isOk, block) = Block::fromBuffer(buf, offset); |
| 696 | if (!isOk) { |
| 697 | std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl; |
| 698 | break; |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 699 | } |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 700 | |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 701 | offset += block.size(); |
| 702 | |
| 703 | ndn::nfd::FaceStatus status(block); |
| 704 | activeFaces.insert(status.getFaceId()); |
| 705 | } |
| 706 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 707 | // Look for face IDs that were registered but not active to find missed |
| 708 | // face destroyed events |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 709 | for (uint64_t faceId : m_registeredFaces) { |
| 710 | if (activeFaces.find(faceId) == activeFaces.end()) { |
| 711 | NFD_LOG_DEBUG("Removing invalid face ID: " << faceId); |
| 712 | |
| 713 | scheduler::schedule(time::seconds(0), |
| 714 | bind(&RibManager::onFaceDestroyedEvent, this, faceId)); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 715 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 716 | } |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 717 | |
| 718 | // Reschedule the check for future clean up |
| 719 | scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | void |
| 723 | RibManager::onFetchFaceStatusTimeout() |
| 724 | { |
| 725 | std::cerr << "Face Status Dataset request timed out" << std::endl; |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 726 | scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 727 | } |
| 728 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 729 | } // namespace rib |
| 730 | } // namespace nfd |