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( |
| 36 | "insert", |
| 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( |
| 41 | "delete", |
| 42 | &FibManager::deleteEntry |
| 43 | ), |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 44 | |
| 45 | VerbAndProcessor( |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 46 | "add-nexthop", |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame^] | 47 | &FibManager::addNextHop |
| 48 | ), |
| 49 | |
| 50 | |
| 51 | |
| 52 | VerbAndProcessor( |
| 53 | "remove-nexthop", |
| 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( |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 59 | // "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 | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 144 | const ndn::Buffer& optionBuffer = |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 145 | request.getName()[optionCompIndex].getValue(); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 146 | shared_ptr<const ndn::Buffer> tmpOptionBuffer(make_shared<ndn::Buffer>(optionBuffer)); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 147 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 148 | try |
| 149 | { |
| 150 | Block rawOptions(tmpOptionBuffer); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame^] | 151 | extractedOptions.wireDecode(rawOptions); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 152 | } |
| 153 | catch (const ndn::Tlv::Error& e) |
| 154 | { |
| 155 | NFD_LOG_INFO("Bad command option parse: " << command); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame^] | 156 | return false; |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 157 | } |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame^] | 158 | NFD_LOG_DEBUG("Options parsed OK"); |
| 159 | return true; |
| 160 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 161 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame^] | 162 | void |
| 163 | FibManager::insertEntry(const ndn::FibManagementOptions& options, |
| 164 | ndn::ControlResponse& response) |
| 165 | { |
| 166 | NFD_LOG_DEBUG("insert prefix: " << options.getName()); |
| 167 | NFD_LOG_INFO("insert result: OK" |
| 168 | << " prefix: " << options.getName()); |
| 169 | std::pair<shared_ptr<fib::Entry>, bool> insertResult = m_managedFib.insert(options.getName()); |
| 170 | setResponse(response, 200, "OK"); |
| 171 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 172 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame^] | 173 | void |
| 174 | FibManager::deleteEntry(const ndn::FibManagementOptions& options, |
| 175 | ndn::ControlResponse& response) |
| 176 | { |
| 177 | NFD_LOG_DEBUG("delete prefix: " << options.getName()); |
| 178 | NFD_LOG_INFO("delete result: OK" |
| 179 | << " prefix: " << options.getName()); |
| 180 | |
| 181 | m_managedFib.remove(options.getName()); |
| 182 | setResponse(response, 200, "OK"); |
| 183 | } |
| 184 | |
| 185 | static inline bool |
| 186 | nextHopEqPredicate(const fib::NextHop& target, const fib::NextHop& hop) |
| 187 | { |
| 188 | return target.getFace()->getId() == hop.getFace()->getId(); |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | FibManager::addNextHop(const ndn::FibManagementOptions& options, |
| 193 | ndn::ControlResponse& response) |
| 194 | { |
| 195 | NFD_LOG_DEBUG("add-nexthop prefix: " << options.getName() |
| 196 | << " faceid: " << options.getFaceId() |
| 197 | << " cost: " << options.getCost()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 198 | |
| 199 | shared_ptr<Face> nextHopFace = m_getFace(options.getFaceId()); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 200 | if (static_cast<bool>(nextHopFace)) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 201 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame^] | 202 | shared_ptr<fib::Entry> entry = m_managedFib.findExactMatch(options.getName()); |
| 203 | if (static_cast<bool>(entry)) |
| 204 | { |
| 205 | entry->addNextHop(nextHopFace, options.getCost()); |
| 206 | |
| 207 | NFD_LOG_INFO("add-nexthop result: OK" |
| 208 | << " prefix:" << options.getName() |
| 209 | << " faceid: " << options.getFaceId() |
| 210 | << " cost: " << options.getCost()); |
| 211 | setResponse(response, 200, "OK"); |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-prefix: " << options.getName()); |
| 216 | setResponse(response, 404, "Prefix not found"); |
| 217 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 218 | } |
| 219 | else |
| 220 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame^] | 221 | NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-faceid: " << options.getFaceId()); |
| 222 | setResponse(response, 404, "Face not found"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | void |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame^] | 227 | FibManager::removeNextHop(const ndn::FibManagementOptions& options, |
| 228 | ndn::ControlResponse &response) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 229 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame^] | 230 | NFD_LOG_DEBUG("remove-nexthop prefix: " << options.getName() |
| 231 | << " faceid: " << options.getFaceId()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 232 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame^] | 233 | shared_ptr<Face> faceToRemove = m_getFace(options.getFaceId()); |
| 234 | if (static_cast<bool>(faceToRemove)) |
| 235 | { |
| 236 | shared_ptr<fib::Entry> entry = m_managedFib.findExactMatch(options.getName()); |
| 237 | if (static_cast<bool>(entry)) |
| 238 | { |
| 239 | entry->removeNextHop(faceToRemove); |
| 240 | NFD_LOG_INFO("remove-nexthop result: OK prefix: " << options.getName() |
| 241 | << " faceid: " << options.getFaceId()); |
| 242 | |
| 243 | setResponse(response, 200, "OK"); |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | NFD_LOG_INFO("remove-nexthop result: FAIL reason: unknown-prefix: " |
| 248 | << options.getName()); |
| 249 | setResponse(response, 404, "Prefix not found"); |
| 250 | } |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | NFD_LOG_INFO("remove-nexthop result: FAIL reason: unknown-faceid: " |
| 255 | << options.getFaceId()); |
| 256 | setResponse(response, 404, "Face not found"); |
| 257 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame^] | 261 | FibManager::strategy(const ndn::FibManagementOptions& options, ndn::ControlResponse& response) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 262 | { |
| 263 | |
| 264 | } |
| 265 | |
| 266 | // void |
| 267 | // FibManager::onConfig(ConfigFile::Node section, bool isDryRun) |
| 268 | // { |
| 269 | |
| 270 | // } |
| 271 | |
| 272 | } // namespace nfd |