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