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