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