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