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 | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 83 | |
| 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 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 106 | FibManagementOptions options; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 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); |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 122 | ControlResponse response; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 123 | (verbProcessor->second)(this, options, response); |
| 124 | |
| 125 | sendResponse(command, response); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 126 | } |
| 127 | else |
| 128 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 129 | NFD_LOG_INFO("command result: unsupported verb: " << verb); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 130 | sendResponse(request.getName(), 501, "Unsupported command"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | } |
| 134 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 135 | bool |
| 136 | FibManager::extractOptions(const Interest& request, |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 137 | FibManagementOptions& extractedOptions) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 138 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 139 | const Name& command = request.getName(); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 140 | const size_t optionCompIndex = |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 141 | FIB_MANAGER_COMMAND_PREFIX.size() + 1; |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 142 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 143 | try |
| 144 | { |
Alexander Afanasyev | b78ec7d | 2014-02-09 12:17:25 -0800 | [diff] [blame] | 145 | Block rawOptions = request.getName()[optionCompIndex].blockFromValue(); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 146 | extractedOptions.wireDecode(rawOptions); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 147 | } |
| 148 | catch (const ndn::Tlv::Error& e) |
| 149 | { |
| 150 | NFD_LOG_INFO("Bad command option parse: " << command); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 151 | return false; |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 152 | } |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 153 | |
| 154 | if (extractedOptions.getFaceId() == 0) |
| 155 | { |
| 156 | extractedOptions.setFaceId(request.getIncomingFaceId()); |
| 157 | } |
| 158 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 159 | NFD_LOG_DEBUG("Options parsed OK"); |
| 160 | return true; |
| 161 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 162 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 163 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 164 | FibManager::insertEntry(const FibManagementOptions& options, |
| 165 | ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 166 | { |
| 167 | NFD_LOG_DEBUG("insert prefix: " << options.getName()); |
| 168 | NFD_LOG_INFO("insert result: OK" |
| 169 | << " prefix: " << options.getName()); |
| 170 | std::pair<shared_ptr<fib::Entry>, bool> insertResult = m_managedFib.insert(options.getName()); |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 171 | setResponse(response, 200, "Success", options.wireEncode()); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 172 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 173 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 174 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 175 | FibManager::deleteEntry(const FibManagementOptions& options, |
| 176 | ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 177 | { |
| 178 | NFD_LOG_DEBUG("delete prefix: " << options.getName()); |
| 179 | NFD_LOG_INFO("delete result: OK" |
| 180 | << " prefix: " << options.getName()); |
| 181 | |
| 182 | m_managedFib.remove(options.getName()); |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 183 | setResponse(response, 200, "Success", options.wireEncode()); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static inline bool |
| 187 | nextHopEqPredicate(const fib::NextHop& target, const fib::NextHop& hop) |
| 188 | { |
| 189 | return target.getFace()->getId() == hop.getFace()->getId(); |
| 190 | } |
| 191 | |
| 192 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 193 | FibManager::addNextHop(const FibManagementOptions& options, |
| 194 | ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 195 | { |
| 196 | NFD_LOG_DEBUG("add-nexthop prefix: " << options.getName() |
| 197 | << " faceid: " << options.getFaceId() |
| 198 | << " cost: " << options.getCost()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 199 | |
| 200 | shared_ptr<Face> nextHopFace = m_getFace(options.getFaceId()); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 201 | if (static_cast<bool>(nextHopFace)) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 202 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 203 | shared_ptr<fib::Entry> entry = m_managedFib.findExactMatch(options.getName()); |
| 204 | if (static_cast<bool>(entry)) |
| 205 | { |
| 206 | entry->addNextHop(nextHopFace, options.getCost()); |
| 207 | |
| 208 | NFD_LOG_INFO("add-nexthop result: OK" |
| 209 | << " prefix:" << options.getName() |
| 210 | << " faceid: " << options.getFaceId() |
| 211 | << " cost: " << options.getCost()); |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 212 | setResponse(response, 200, "Success", options.wireEncode()); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 213 | } |
| 214 | else |
| 215 | { |
| 216 | NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-prefix: " << options.getName()); |
| 217 | setResponse(response, 404, "Prefix not found"); |
| 218 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 219 | } |
| 220 | else |
| 221 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 222 | NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-faceid: " << options.getFaceId()); |
| 223 | setResponse(response, 404, "Face not found"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 228 | FibManager::removeNextHop(const FibManagementOptions& options, |
| 229 | ControlResponse &response) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 230 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 231 | NFD_LOG_DEBUG("remove-nexthop prefix: " << options.getName() |
| 232 | << " faceid: " << options.getFaceId()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 233 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 234 | shared_ptr<Face> faceToRemove = m_getFace(options.getFaceId()); |
| 235 | if (static_cast<bool>(faceToRemove)) |
| 236 | { |
| 237 | shared_ptr<fib::Entry> entry = m_managedFib.findExactMatch(options.getName()); |
| 238 | if (static_cast<bool>(entry)) |
| 239 | { |
| 240 | entry->removeNextHop(faceToRemove); |
| 241 | NFD_LOG_INFO("remove-nexthop result: OK prefix: " << options.getName() |
| 242 | << " faceid: " << options.getFaceId()); |
| 243 | |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 244 | setResponse(response, 200, "Success", options.wireEncode()); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 245 | } |
| 246 | else |
| 247 | { |
| 248 | NFD_LOG_INFO("remove-nexthop result: FAIL reason: unknown-prefix: " |
| 249 | << options.getName()); |
| 250 | setResponse(response, 404, "Prefix not found"); |
| 251 | } |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | NFD_LOG_INFO("remove-nexthop result: FAIL reason: unknown-faceid: " |
| 256 | << options.getFaceId()); |
| 257 | setResponse(response, 404, "Face not found"); |
| 258 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 262 | FibManager::strategy(const FibManagementOptions& options, ControlResponse& response) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 263 | { |
| 264 | |
| 265 | } |
| 266 | |
| 267 | // void |
| 268 | // FibManager::onConfig(ConfigFile::Node section, bool isDryRun) |
| 269 | // { |
| 270 | |
| 271 | // } |
| 272 | |
| 273 | } // namespace nfd |