Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "fib-manager.hpp" |
| 8 | |
| 9 | #include "table/fib.hpp" |
| 10 | #include "fw/forwarder.hpp" |
| 11 | #include "mgmt/internal-face.hpp" |
| 12 | #include "mgmt/app-face.hpp" |
| 13 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 14 | #include <ndn-cpp-dev/encoding/tlv.hpp> |
| 15 | |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 16 | namespace nfd { |
| 17 | |
| 18 | NFD_LOG_INIT("FibManager"); |
| 19 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 20 | const Name FibManager::FIB_MANAGER_COMMAND_PREFIX = "/localhost/nfd/fib"; |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 21 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 22 | const size_t FibManager::FIB_MANAGER_COMMAND_UNSIGNED_NCOMPS = |
| 23 | FibManager::FIB_MANAGER_COMMAND_PREFIX.size() + |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 24 | 1 + // verb |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 25 | 1; // verb options |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 26 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 27 | const size_t FibManager::FIB_MANAGER_COMMAND_SIGNED_NCOMPS = |
| 28 | FibManager::FIB_MANAGER_COMMAND_UNSIGNED_NCOMPS + |
| 29 | 0; // No signed Interest support in mock, otherwise 3 (timestamp, signed info tlv, signature tlv) |
| 30 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 31 | const FibManager::VerbAndProcessor FibManager::FIB_MANAGER_COMMAND_VERBS[] = |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 32 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 33 | VerbAndProcessor( |
Alexander Afanasyev | b78ec7d | 2014-02-09 12:17:25 -0800 | [diff] [blame] | 34 | Name::Component("insert"), |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 35 | &FibManager::insertEntry |
| 36 | ), |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 37 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 38 | VerbAndProcessor( |
Alexander Afanasyev | b78ec7d | 2014-02-09 12:17:25 -0800 | [diff] [blame] | 39 | Name::Component("delete"), |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 40 | &FibManager::deleteEntry |
| 41 | ), |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 42 | |
| 43 | VerbAndProcessor( |
Alexander Afanasyev | b78ec7d | 2014-02-09 12:17:25 -0800 | [diff] [blame] | 44 | Name::Component("add-nexthop"), |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 45 | &FibManager::addNextHop |
| 46 | ), |
| 47 | |
| 48 | |
| 49 | |
| 50 | VerbAndProcessor( |
Alexander Afanasyev | b78ec7d | 2014-02-09 12:17:25 -0800 | [diff] [blame] | 51 | Name::Component("remove-nexthop"), |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 52 | &FibManager::removeNextHop |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 53 | ), |
| 54 | |
| 55 | // Unsupported |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 56 | // VerbAndProcessor( |
Alexander Afanasyev | b78ec7d | 2014-02-09 12:17:25 -0800 | [diff] [blame] | 57 | // Name::Component("strategy"), |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 58 | // &FibManager::strategy |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 59 | // ) |
| 60 | |
| 61 | }; |
| 62 | |
| 63 | FibManager::FibManager(Fib& fib, |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 64 | function<shared_ptr<Face>(FaceId)> getFace, |
| 65 | shared_ptr<AppFace> face) |
| 66 | : ManagerBase(face), |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 67 | m_managedFib(fib), |
| 68 | m_getFace(getFace), |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 69 | m_verbDispatch(FIB_MANAGER_COMMAND_VERBS, |
| 70 | FIB_MANAGER_COMMAND_VERBS + |
| 71 | (sizeof(FIB_MANAGER_COMMAND_VERBS) / sizeof(VerbAndProcessor))) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 72 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 73 | face->setInterestFilter("/localhost/nfd/fib", |
| 74 | bind(&FibManager::onFibRequest, this, _2)); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
| 78 | FibManager::onFibRequest(const Interest& request) |
| 79 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 80 | const Name& command = request.getName(); |
| 81 | const size_t commandNComps = command.size(); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 82 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 83 | if (FIB_MANAGER_COMMAND_UNSIGNED_NCOMPS <= commandNComps && |
| 84 | commandNComps < FIB_MANAGER_COMMAND_SIGNED_NCOMPS) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 85 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 86 | NFD_LOG_INFO("command result: unsigned verb: " << command); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 87 | sendResponse(command, 401, "Signature required"); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 88 | |
| 89 | return; |
| 90 | } |
| 91 | else if (commandNComps < FIB_MANAGER_COMMAND_SIGNED_NCOMPS || |
| 92 | !FIB_MANAGER_COMMAND_PREFIX.isPrefixOf(command)) |
| 93 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 94 | NFD_LOG_INFO("command result: malformed"); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 95 | sendResponse(command, 400, "Malformed command"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 96 | return; |
| 97 | } |
| 98 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 99 | const Name::Component& verb = command.get(FIB_MANAGER_COMMAND_PREFIX.size()); |
| 100 | |
| 101 | VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find (verb); |
| 102 | if (verbProcessor != m_verbDispatch.end()) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 103 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame^] | 104 | FibManagementOptions options; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 105 | if (!extractOptions(request, options)) |
| 106 | { |
| 107 | sendResponse(command, 400, "Malformed command"); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | /// \todo authorize command |
| 112 | if (false) |
| 113 | { |
| 114 | NFD_LOG_INFO("command result: unauthorized verb: " << command); |
| 115 | sendResponse(request.getName(), 403, "Unauthorized command"); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | NFD_LOG_INFO("command result: processing verb: " << verb); |
| 120 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame^] | 121 | ControlResponse response; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 122 | (verbProcessor->second)(this, options, response); |
| 123 | |
| 124 | sendResponse(command, response); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 125 | } |
| 126 | else |
| 127 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 128 | NFD_LOG_INFO("command result: unsupported verb: " << verb); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 129 | sendResponse(request.getName(), 501, "Unsupported command"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } |
| 133 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 134 | bool |
| 135 | FibManager::extractOptions(const Interest& request, |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame^] | 136 | FibManagementOptions& extractedOptions) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 137 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 138 | const Name& command = request.getName(); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 139 | const size_t optionCompIndex = |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 140 | FIB_MANAGER_COMMAND_PREFIX.size() + 1; |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 141 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 142 | try |
| 143 | { |
Alexander Afanasyev | b78ec7d | 2014-02-09 12:17:25 -0800 | [diff] [blame] | 144 | Block rawOptions = request.getName()[optionCompIndex].blockFromValue(); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 145 | extractedOptions.wireDecode(rawOptions); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 146 | } |
| 147 | catch (const ndn::Tlv::Error& e) |
| 148 | { |
| 149 | NFD_LOG_INFO("Bad command option parse: " << command); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 150 | return false; |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 151 | } |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 152 | NFD_LOG_DEBUG("Options parsed OK"); |
| 153 | return true; |
| 154 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 155 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 156 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame^] | 157 | FibManager::insertEntry(const FibManagementOptions& options, |
| 158 | ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 159 | { |
| 160 | NFD_LOG_DEBUG("insert prefix: " << options.getName()); |
| 161 | NFD_LOG_INFO("insert result: OK" |
| 162 | << " prefix: " << options.getName()); |
| 163 | std::pair<shared_ptr<fib::Entry>, bool> insertResult = m_managedFib.insert(options.getName()); |
| 164 | setResponse(response, 200, "OK"); |
| 165 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 166 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 167 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame^] | 168 | FibManager::deleteEntry(const FibManagementOptions& options, |
| 169 | ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 170 | { |
| 171 | NFD_LOG_DEBUG("delete prefix: " << options.getName()); |
| 172 | NFD_LOG_INFO("delete result: OK" |
| 173 | << " prefix: " << options.getName()); |
| 174 | |
| 175 | m_managedFib.remove(options.getName()); |
| 176 | setResponse(response, 200, "OK"); |
| 177 | } |
| 178 | |
| 179 | static inline bool |
| 180 | nextHopEqPredicate(const fib::NextHop& target, const fib::NextHop& hop) |
| 181 | { |
| 182 | return target.getFace()->getId() == hop.getFace()->getId(); |
| 183 | } |
| 184 | |
| 185 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame^] | 186 | FibManager::addNextHop(const FibManagementOptions& options, |
| 187 | ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 188 | { |
| 189 | NFD_LOG_DEBUG("add-nexthop prefix: " << options.getName() |
| 190 | << " faceid: " << options.getFaceId() |
| 191 | << " cost: " << options.getCost()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 192 | |
| 193 | shared_ptr<Face> nextHopFace = m_getFace(options.getFaceId()); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 194 | if (static_cast<bool>(nextHopFace)) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 195 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 196 | shared_ptr<fib::Entry> entry = m_managedFib.findExactMatch(options.getName()); |
| 197 | if (static_cast<bool>(entry)) |
| 198 | { |
| 199 | entry->addNextHop(nextHopFace, options.getCost()); |
| 200 | |
| 201 | NFD_LOG_INFO("add-nexthop result: OK" |
| 202 | << " prefix:" << options.getName() |
| 203 | << " faceid: " << options.getFaceId() |
| 204 | << " cost: " << options.getCost()); |
| 205 | setResponse(response, 200, "OK"); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-prefix: " << options.getName()); |
| 210 | setResponse(response, 404, "Prefix not found"); |
| 211 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 212 | } |
| 213 | else |
| 214 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 215 | NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-faceid: " << options.getFaceId()); |
| 216 | setResponse(response, 404, "Face not found"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame^] | 221 | FibManager::removeNextHop(const FibManagementOptions& options, |
| 222 | ControlResponse &response) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 223 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 224 | NFD_LOG_DEBUG("remove-nexthop prefix: " << options.getName() |
| 225 | << " faceid: " << options.getFaceId()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 226 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 227 | shared_ptr<Face> faceToRemove = m_getFace(options.getFaceId()); |
| 228 | if (static_cast<bool>(faceToRemove)) |
| 229 | { |
| 230 | shared_ptr<fib::Entry> entry = m_managedFib.findExactMatch(options.getName()); |
| 231 | if (static_cast<bool>(entry)) |
| 232 | { |
| 233 | entry->removeNextHop(faceToRemove); |
| 234 | NFD_LOG_INFO("remove-nexthop result: OK prefix: " << options.getName() |
| 235 | << " faceid: " << options.getFaceId()); |
| 236 | |
| 237 | setResponse(response, 200, "OK"); |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | NFD_LOG_INFO("remove-nexthop result: FAIL reason: unknown-prefix: " |
| 242 | << options.getName()); |
| 243 | setResponse(response, 404, "Prefix not found"); |
| 244 | } |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | NFD_LOG_INFO("remove-nexthop result: FAIL reason: unknown-faceid: " |
| 249 | << options.getFaceId()); |
| 250 | setResponse(response, 404, "Face not found"); |
| 251 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame^] | 255 | FibManager::strategy(const FibManagementOptions& options, ControlResponse& response) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 256 | { |
| 257 | |
| 258 | } |
| 259 | |
| 260 | // void |
| 261 | // FibManager::onConfig(ConfigFile::Node section, bool isDryRun) |
| 262 | // { |
| 263 | |
| 264 | // } |
| 265 | |
| 266 | } // namespace nfd |