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