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