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