Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 25 | |
| 26 | #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" |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 33 | #include <ndn-cxx/security/validator-config.hpp> |
Alexander Afanasyev | 585e5a6 | 2014-08-12 11:49:31 -0700 | [diff] [blame^] | 34 | #include <ndn-cxx/management/nfd-face-monitor.hpp> |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 35 | #include <ndn-cxx/management/nfd-controller.hpp> |
| 36 | #include <ndn-cxx/management/nfd-control-command.hpp> |
| 37 | #include <ndn-cxx/management/nfd-control-response.hpp> |
| 38 | #include <ndn-cxx/management/nfd-control-parameters.hpp> |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 39 | |
| 40 | namespace nfd { |
| 41 | namespace rib { |
| 42 | |
| 43 | using ndn::nfd::ControlCommand; |
| 44 | using ndn::nfd::ControlResponse; |
| 45 | using ndn::nfd::ControlParameters; |
| 46 | |
Alexander Afanasyev | 585e5a6 | 2014-08-12 11:49:31 -0700 | [diff] [blame^] | 47 | using ndn::nfd::FaceEventNotification; |
| 48 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 49 | class RibManager : noncopyable |
| 50 | { |
| 51 | public: |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 52 | class Error : public std::runtime_error |
| 53 | { |
| 54 | public: |
| 55 | explicit |
| 56 | Error(const std::string& what) |
| 57 | : std::runtime_error(what) |
| 58 | { |
| 59 | } |
| 60 | }; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 61 | |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 62 | explicit |
| 63 | RibManager(ndn::Face& face); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 64 | |
| 65 | void |
| 66 | registerWithNfd(); |
| 67 | |
| 68 | void |
| 69 | enableLocalControlHeader(); |
| 70 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 71 | void |
| 72 | setConfigFile(ConfigFile& configFile); |
| 73 | |
| 74 | private: |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 75 | typedef uint32_t TransactionId; |
| 76 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 77 | void |
| 78 | onConfig(const ConfigSection& configSection, |
| 79 | bool isDryRun, |
| 80 | const std::string& filename); |
| 81 | |
| 82 | void |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 83 | startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest); |
| 84 | |
| 85 | void |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 86 | onLocalhopRequest(const Interest& request); |
| 87 | |
| 88 | void |
| 89 | onLocalhostRequest(const Interest& request); |
| 90 | |
| 91 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 92 | sendResponse(const Name& name, |
| 93 | const ControlResponse& response); |
| 94 | |
| 95 | void |
| 96 | sendResponse(const Name& name, |
| 97 | uint32_t code, |
| 98 | const std::string& text); |
| 99 | |
| 100 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 101 | sendSuccessResponse(const shared_ptr<const Interest>& request, |
| 102 | const ControlParameters& parameters); |
| 103 | |
| 104 | void |
| 105 | sendErrorResponse(uint32_t code, const std::string& error, |
| 106 | const shared_ptr<const Interest>& request); |
| 107 | |
| 108 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 109 | registerEntry(const shared_ptr<const Interest>& request, |
| 110 | ControlParameters& parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 111 | |
| 112 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 113 | unregisterEntry(const shared_ptr<const Interest>& request, |
| 114 | ControlParameters& parameters); |
| 115 | |
| 116 | void |
| 117 | onCommandValidated(const shared_ptr<const Interest>& request); |
| 118 | |
| 119 | void |
| 120 | onCommandValidationFailed(const shared_ptr<const Interest>& request, |
| 121 | const std::string& failureInfo); |
| 122 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 123 | |
| 124 | void |
| 125 | onCommandError(uint32_t code, const std::string& error, |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 126 | const shared_ptr<const Interest>& request, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 127 | const FaceEntry& faceEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 128 | |
| 129 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 130 | onRegSuccess(const shared_ptr<const Interest>& request, |
| 131 | const ControlParameters& parameters, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 132 | const FaceEntry& faceEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 133 | |
| 134 | void |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 135 | onUnRegSuccess(const shared_ptr<const Interest>& request, |
| 136 | const ControlParameters& parameters, |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 137 | const FaceEntry& faceEntry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 138 | |
| 139 | void |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 140 | onNrdCommandPrefixAddNextHopSuccess(const Name& prefix); |
| 141 | |
| 142 | void |
| 143 | onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg); |
| 144 | |
| 145 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 146 | onAddNextHopSuccess(const shared_ptr<const Interest>& request, |
| 147 | const ControlParameters& parameters, |
| 148 | const TransactionId transactionId, |
| 149 | const bool shouldSendResponse); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 150 | |
| 151 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 152 | onAddNextHopError(uint32_t code, const std::string& error, |
| 153 | const shared_ptr<const Interest>& request, |
| 154 | const TransactionId transactionId, const bool shouldSendResponse); |
| 155 | |
| 156 | void |
| 157 | onRemoveNextHopSuccess(const shared_ptr<const Interest>& request, |
| 158 | const ControlParameters& parameters, |
| 159 | const TransactionId transactionId, |
| 160 | const bool shouldSendResponse); |
| 161 | |
| 162 | void |
| 163 | onRemoveNextHopError(uint32_t code, const std::string& error, |
| 164 | const shared_ptr<const Interest>& request, |
| 165 | const TransactionId transactionId, const bool shouldSendResponse); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 166 | |
| 167 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 168 | onControlHeaderSuccess(); |
| 169 | |
| 170 | void |
| 171 | onControlHeaderError(uint32_t code, const std::string& reason); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 172 | static bool |
| 173 | extractParameters(const Name::Component& parameterComponent, |
| 174 | ControlParameters& extractedParameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 175 | |
| 176 | bool |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 177 | validateParameters(const ControlCommand& command, |
| 178 | ControlParameters& parameters); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 179 | |
| 180 | void |
| 181 | onNotification(const FaceEventNotification& notification); |
| 182 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 183 | void |
Alexander Afanasyev | 63108c4 | 2014-07-07 19:10:47 -0700 | [diff] [blame] | 184 | processErasureAfterNotification(uint64_t faceId); |
| 185 | |
| 186 | void |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 187 | sendUpdatesToFib(const shared_ptr<const Interest>& request, |
| 188 | const ControlParameters& parameters); |
| 189 | |
| 190 | void |
| 191 | sendUpdatesToFibAfterFaceDestroyEvent(); |
| 192 | |
| 193 | /** \brief Checks if the transaction has received all of the expected responses |
| 194 | * from the FIB. |
| 195 | * \return{ True if the transaction with the passed transactionId has applied |
| 196 | * all of its FIB updates successfully; otherwise, false } |
| 197 | */ |
| 198 | bool |
| 199 | isTransactionComplete(const TransactionId transactionId); |
| 200 | |
| 201 | void |
| 202 | invalidateTransaction(const TransactionId transactionId); |
| 203 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 204 | void |
| 205 | listEntries(const Interest& request); |
| 206 | |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 207 | void |
| 208 | fetchActiveFaces(); |
| 209 | |
| 210 | void |
| 211 | fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer); |
| 212 | |
| 213 | void |
| 214 | updateActiveFaces(shared_ptr<ndn::OBufferStream> buffer); |
| 215 | |
| 216 | void |
| 217 | onFetchFaceStatusTimeout(); |
| 218 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 219 | private: |
| 220 | Rib m_managedRib; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 221 | ndn::Face& m_face; |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 222 | ndn::nfd::Controller m_nfdController; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 223 | ndn::KeyChain m_keyChain; |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 224 | ndn::ValidatorConfig m_localhostValidator; |
| 225 | ndn::ValidatorConfig m_localhopValidator; |
Alexander Afanasyev | 585e5a6 | 2014-08-12 11:49:31 -0700 | [diff] [blame^] | 226 | ndn::nfd::FaceMonitor m_faceMonitor; |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 227 | bool m_isLocalhopEnabled; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 228 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 229 | RibStatusPublisher m_ribStatusPublisher; |
| 230 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 231 | /** \brief The last transaction ID for FIB update response messages. |
| 232 | * Each group of FIB updates applied to the FIB is assigned an incrementing |
| 233 | * ID that is used to track the number of successfully applied updates. |
| 234 | */ |
| 235 | TransactionId m_lastTransactionId; |
| 236 | |
| 237 | /// table of FIB update transactions => count of pending FIB updates |
| 238 | typedef std::map<TransactionId, int> FibTransactionTable; |
| 239 | |
| 240 | /** \brief Table used to track the number of FIB updates that have not yet received |
| 241 | * a response from the FIB. |
| 242 | * The table maps a transaction ID to the number of updates remaining for that |
| 243 | * specific transaction. |
| 244 | */ |
| 245 | FibTransactionTable m_pendingFibTransactions; |
| 246 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 247 | typedef function<void(RibManager*, |
| 248 | const shared_ptr<const Interest>& request, |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 249 | ControlParameters& parameters)> SignedVerbProcessor; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 250 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 251 | typedef std::map<name::Component, SignedVerbProcessor> SignedVerbDispatchTable; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 252 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 253 | typedef std::pair<name::Component, SignedVerbProcessor> SignedVerbAndProcessor; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 254 | |
| 255 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 256 | const SignedVerbDispatchTable m_signedVerbDispatch; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 257 | |
| 258 | static const Name COMMAND_PREFIX; // /localhost/nrd |
| 259 | static const Name REMOTE_COMMAND_PREFIX; // /localhop/nrd |
| 260 | |
| 261 | // number of components in an invalid, but not malformed, unsigned command. |
| 262 | // (/localhost/nrd + verb + options) = 4 |
| 263 | static const size_t COMMAND_UNSIGNED_NCOMPS; |
| 264 | |
| 265 | // number of components in a valid signed Interest. |
| 266 | // 8 with signed Interest support. |
| 267 | static const size_t COMMAND_SIGNED_NCOMPS; |
| 268 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 269 | static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[]; |
| 270 | |
| 271 | typedef function<void(RibManager*, const Interest&)> UnsignedVerbProcessor; |
| 272 | typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable; |
| 273 | typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor; |
| 274 | |
| 275 | const UnsignedVerbDispatchTable m_unsignedVerbDispatch; |
| 276 | static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[]; |
| 277 | |
| 278 | static const Name LIST_COMMAND_PREFIX; |
| 279 | static const size_t LIST_COMMAND_NCOMPS; |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 280 | |
| 281 | static const Name FACES_LIST_DATASET_PREFIX; |
| 282 | |
| 283 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 284 | std::set<int> activeFaces; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | } // namespace rib |
| 288 | } // namespace nfd |
| 289 | |
| 290 | #endif // NFD_RIB_RIB_MANAGER_HPP |