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 | #ifndef NFD_RIB_RIB_MANAGER_HPP |
| 27 | #define NFD_RIB_RIB_MANAGER_HPP |
| 28 | |
| 29 | #include "rib.hpp" |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 30 | #include "core/config-file.hpp" |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 31 | #include "rib-status-publisher.hpp" |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 32 | #include "remote-registrator.hpp" |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 34 | #include <ndn-cxx/security/validator-config.hpp> |
Alexander Afanasyev | 585e5a6 | 2014-08-12 11:49:31 -0700 | [diff] [blame] | 35 | #include <ndn-cxx/management/nfd-face-monitor.hpp> |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 36 | #include <ndn-cxx/management/nfd-controller.hpp> |
| 37 | #include <ndn-cxx/management/nfd-control-command.hpp> |
| 38 | #include <ndn-cxx/management/nfd-control-response.hpp> |
| 39 | #include <ndn-cxx/management/nfd-control-parameters.hpp> |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 40 | |
| 41 | namespace nfd { |
| 42 | namespace rib { |
| 43 | |
| 44 | using ndn::nfd::ControlCommand; |
| 45 | using ndn::nfd::ControlResponse; |
| 46 | using ndn::nfd::ControlParameters; |
| 47 | |
Alexander Afanasyev | 585e5a6 | 2014-08-12 11:49:31 -0700 | [diff] [blame] | 48 | using ndn::nfd::FaceEventNotification; |
| 49 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 50 | class RibManager : noncopyable |
| 51 | { |
| 52 | public: |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 53 | class Error : public std::runtime_error |
| 54 | { |
| 55 | public: |
| 56 | explicit |
| 57 | Error(const std::string& what) |
| 58 | : std::runtime_error(what) |
| 59 | { |
| 60 | } |
| 61 | }; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 62 | |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 63 | explicit |
| 64 | RibManager(ndn::Face& face); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 65 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 66 | ~RibManager(); |
| 67 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 68 | void |
| 69 | registerWithNfd(); |
| 70 | |
| 71 | void |
| 72 | enableLocalControlHeader(); |
| 73 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 74 | void |
| 75 | setConfigFile(ConfigFile& configFile); |
| 76 | |
| 77 | private: |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 78 | typedef uint32_t TransactionId; |
| 79 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 80 | void |
| 81 | onConfig(const ConfigSection& configSection, |
| 82 | bool isDryRun, |
| 83 | const std::string& filename); |
| 84 | |
| 85 | void |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 86 | startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest); |
| 87 | |
| 88 | void |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 89 | onLocalhopRequest(const Interest& request); |
| 90 | |
| 91 | void |
| 92 | onLocalhostRequest(const Interest& request); |
| 93 | |
| 94 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 95 | sendResponse(const Name& name, |
| 96 | const ControlResponse& response); |
| 97 | |
| 98 | void |
| 99 | sendResponse(const Name& name, |
| 100 | uint32_t code, |
| 101 | const std::string& text); |
| 102 | |
| 103 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 104 | sendSuccessResponse(const shared_ptr<const Interest>& request, |
| 105 | const ControlParameters& parameters); |
| 106 | |
| 107 | void |
| 108 | sendErrorResponse(uint32_t code, const std::string& error, |
| 109 | const shared_ptr<const Interest>& request); |
| 110 | |
| 111 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 112 | registerEntry(const shared_ptr<const Interest>& request, |
| 113 | ControlParameters& parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 114 | |
| 115 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 116 | unregisterEntry(const shared_ptr<const Interest>& request, |
| 117 | ControlParameters& parameters); |
| 118 | |
| 119 | void |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 120 | expireEntry(const shared_ptr<const Interest>& request, ControlParameters& params); |
| 121 | |
| 122 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 123 | onCommandValidated(const shared_ptr<const Interest>& request); |
| 124 | |
| 125 | void |
| 126 | onCommandValidationFailed(const shared_ptr<const Interest>& request, |
| 127 | const std::string& failureInfo); |
| 128 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 129 | |
| 130 | void |
| 131 | onCommandError(uint32_t code, const std::string& error, |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 132 | const shared_ptr<const Interest>& request, |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 133 | const Route& route); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 134 | |
| 135 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 136 | onRegSuccess(const shared_ptr<const Interest>& request, |
| 137 | const ControlParameters& parameters, |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 138 | const Route& route); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 139 | |
| 140 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 141 | onUnRegSuccess(const shared_ptr<const Interest>& request, |
| 142 | const ControlParameters& parameters, |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 143 | const Route& route); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 144 | |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame^] | 145 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 146 | void |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame^] | 147 | onNrdCommandPrefixAddNextHopSuccess(const Name& prefix, |
| 148 | const ndn::nfd::ControlParameters& result); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 149 | |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame^] | 150 | private: |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 151 | void |
| 152 | onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg); |
| 153 | |
| 154 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 155 | onAddNextHopSuccess(const shared_ptr<const Interest>& request, |
| 156 | const ControlParameters& parameters, |
| 157 | const TransactionId transactionId, |
| 158 | const bool shouldSendResponse); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 159 | |
| 160 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 161 | onAddNextHopError(uint32_t code, const std::string& error, |
| 162 | const shared_ptr<const Interest>& request, |
| 163 | const TransactionId transactionId, const bool shouldSendResponse); |
| 164 | |
| 165 | void |
| 166 | onRemoveNextHopSuccess(const shared_ptr<const Interest>& request, |
| 167 | const ControlParameters& parameters, |
| 168 | const TransactionId transactionId, |
| 169 | const bool shouldSendResponse); |
| 170 | |
| 171 | void |
| 172 | onRemoveNextHopError(uint32_t code, const std::string& error, |
| 173 | const shared_ptr<const Interest>& request, |
| 174 | const TransactionId transactionId, const bool shouldSendResponse); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 175 | |
| 176 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 177 | onControlHeaderSuccess(); |
| 178 | |
| 179 | void |
| 180 | onControlHeaderError(uint32_t code, const std::string& reason); |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 181 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 182 | static bool |
| 183 | extractParameters(const Name::Component& parameterComponent, |
| 184 | ControlParameters& extractedParameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 185 | |
| 186 | bool |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 187 | validateParameters(const ControlCommand& command, |
| 188 | ControlParameters& parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 189 | |
| 190 | void |
| 191 | onNotification(const FaceEventNotification& notification); |
| 192 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 193 | void |
Alexander Afanasyev | 63108c4 | 2014-07-07 19:10:47 -0700 | [diff] [blame] | 194 | processErasureAfterNotification(uint64_t faceId); |
| 195 | |
| 196 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 197 | sendUpdatesToFib(const shared_ptr<const Interest>& request, |
| 198 | const ControlParameters& parameters); |
| 199 | |
| 200 | void |
| 201 | sendUpdatesToFibAfterFaceDestroyEvent(); |
| 202 | |
| 203 | /** \brief Checks if the transaction has received all of the expected responses |
| 204 | * from the FIB. |
| 205 | * \return{ True if the transaction with the passed transactionId has applied |
| 206 | * all of its FIB updates successfully; otherwise, false } |
| 207 | */ |
| 208 | bool |
| 209 | isTransactionComplete(const TransactionId transactionId); |
| 210 | |
| 211 | void |
| 212 | invalidateTransaction(const TransactionId transactionId); |
| 213 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 214 | void |
| 215 | listEntries(const Interest& request); |
| 216 | |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 217 | void |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 218 | scheduleActiveFaceFetch(const time::seconds& timeToWait); |
| 219 | |
| 220 | void |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 221 | fetchActiveFaces(); |
| 222 | |
| 223 | void |
| 224 | fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer); |
| 225 | |
| 226 | void |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 227 | onFetchFaceStatusTimeout(); |
| 228 | |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 229 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 230 | /** \param buffer Face dataset contents |
| 231 | */ |
| 232 | void |
| 233 | removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer); |
| 234 | |
| 235 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 236 | Rib m_managedRib; |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 237 | |
| 238 | private: |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 239 | ndn::Face& m_face; |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 240 | ndn::nfd::Controller m_nfdController; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 241 | ndn::KeyChain m_keyChain; |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 242 | ndn::ValidatorConfig m_localhostValidator; |
| 243 | ndn::ValidatorConfig m_localhopValidator; |
Alexander Afanasyev | 585e5a6 | 2014-08-12 11:49:31 -0700 | [diff] [blame] | 244 | ndn::nfd::FaceMonitor m_faceMonitor; |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 245 | bool m_isLocalhopEnabled; |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 246 | RemoteRegistrator m_remoteRegistrator; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 247 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 248 | RibStatusPublisher m_ribStatusPublisher; |
| 249 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 250 | /** \brief The last transaction ID for FIB update response messages. |
| 251 | * Each group of FIB updates applied to the FIB is assigned an incrementing |
| 252 | * ID that is used to track the number of successfully applied updates. |
| 253 | */ |
| 254 | TransactionId m_lastTransactionId; |
| 255 | |
| 256 | /// table of FIB update transactions => count of pending FIB updates |
| 257 | typedef std::map<TransactionId, int> FibTransactionTable; |
| 258 | |
| 259 | /** \brief Table used to track the number of FIB updates that have not yet received |
| 260 | * a response from the FIB. |
| 261 | * The table maps a transaction ID to the number of updates remaining for that |
| 262 | * specific transaction. |
| 263 | */ |
| 264 | FibTransactionTable m_pendingFibTransactions; |
| 265 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 266 | typedef function<void(RibManager*, |
| 267 | const shared_ptr<const Interest>& request, |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 268 | ControlParameters& parameters)> SignedVerbProcessor; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 269 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 270 | typedef std::map<name::Component, SignedVerbProcessor> SignedVerbDispatchTable; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 271 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 272 | typedef std::pair<name::Component, SignedVerbProcessor> SignedVerbAndProcessor; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 273 | |
| 274 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 275 | const SignedVerbDispatchTable m_signedVerbDispatch; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 276 | |
| 277 | static const Name COMMAND_PREFIX; // /localhost/nrd |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame^] | 278 | |
| 279 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 280 | static const Name REMOTE_COMMAND_PREFIX; // /localhop/nrd |
| 281 | |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame^] | 282 | private: |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 283 | // number of components in an invalid, but not malformed, unsigned command. |
| 284 | // (/localhost/nrd + verb + options) = 4 |
| 285 | static const size_t COMMAND_UNSIGNED_NCOMPS; |
| 286 | |
| 287 | // number of components in a valid signed Interest. |
| 288 | // 8 with signed Interest support. |
| 289 | static const size_t COMMAND_SIGNED_NCOMPS; |
| 290 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 291 | static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[]; |
| 292 | |
| 293 | typedef function<void(RibManager*, const Interest&)> UnsignedVerbProcessor; |
| 294 | typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable; |
| 295 | typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor; |
| 296 | |
| 297 | const UnsignedVerbDispatchTable m_unsignedVerbDispatch; |
| 298 | static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[]; |
| 299 | |
| 300 | static const Name LIST_COMMAND_PREFIX; |
| 301 | static const size_t LIST_COMMAND_NCOMPS; |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 302 | |
| 303 | static const Name FACES_LIST_DATASET_PREFIX; |
| 304 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 305 | static const time::seconds ACTIVE_FACE_FETCH_INTERVAL; |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [diff] [blame] | 306 | scheduler::EventId m_activeFaceFetchEvent; |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 307 | |
| 308 | typedef std::set<uint64_t> FaceIdSet; |
| 309 | /** \brief contains FaceIds with one or more Routes in the RIB |
| 310 | */ |
| 311 | FaceIdSet m_registeredFaces; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | } // namespace rib |
| 315 | } // namespace nfd |
| 316 | |
| 317 | #endif // NFD_RIB_RIB_MANAGER_HPP |