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