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