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