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 | |
| 15 | |
| 16 | #include <ndn-cpp-dev/management/fib-management-options.hpp> |
| 17 | #include <ndn-cpp-dev/encoding/tlv.hpp> |
| 18 | |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 19 | #include <ndn-cpp-dev/management/fib-management-options.hpp> |
| 20 | |
| 21 | namespace nfd { |
| 22 | |
| 23 | NFD_LOG_INIT("FibManager"); |
| 24 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 25 | const Name FibManager::FIB_MANAGER_COMMAND_PREFIX = "/localhost/nfd/fib"; |
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_UNSIGNED_NCOMPS = |
| 28 | FibManager::FIB_MANAGER_COMMAND_PREFIX.size() + |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 29 | 1 + // verb |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 30 | 1; // verb options |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 31 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 32 | const size_t FibManager::FIB_MANAGER_COMMAND_SIGNED_NCOMPS = |
| 33 | FibManager::FIB_MANAGER_COMMAND_UNSIGNED_NCOMPS + |
| 34 | 0; // No signed Interest support in mock, otherwise 3 (timestamp, signed info tlv, signature tlv) |
| 35 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 36 | const FibManager::VerbAndProcessor FibManager::FIB_MANAGER_COMMAND_VERBS[] = |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 37 | { |
| 38 | // Unsupported |
| 39 | |
| 40 | // VerbAndProcessor( |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 41 | // "insert", |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 42 | // &FibManager::fibInsert |
| 43 | // ), |
| 44 | |
| 45 | // VerbAndProcessor( |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 46 | // "delete", |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 47 | // &FibManager::fibDelete |
| 48 | // ), |
| 49 | |
| 50 | VerbAndProcessor( |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 51 | "add-nexthop", |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 52 | &FibManager::fibAddNextHop |
| 53 | ), |
| 54 | |
| 55 | // Unsupported |
| 56 | |
| 57 | // VerbAndProcessor( |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 58 | // "remove-nexthop", |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 59 | // &FibManager::fibRemoveNextHop |
| 60 | // ), |
| 61 | |
| 62 | // VerbAndProcessor( |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 63 | // "strategy", |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 64 | // &FibManager::fibStrategy |
| 65 | // ) |
| 66 | |
| 67 | }; |
| 68 | |
| 69 | FibManager::FibManager(Fib& fib, |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 70 | function<shared_ptr<Face>(FaceId)> getFace, |
| 71 | shared_ptr<AppFace> face) |
| 72 | : ManagerBase(face), |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 73 | m_managedFib(fib), |
| 74 | m_getFace(getFace), |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 75 | m_verbDispatch(FIB_MANAGER_COMMAND_VERBS, |
| 76 | FIB_MANAGER_COMMAND_VERBS + |
| 77 | (sizeof(FIB_MANAGER_COMMAND_VERBS) / sizeof(VerbAndProcessor))) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 78 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 79 | face->setInterestFilter("/localhost/nfd/fib", |
| 80 | bind(&FibManager::onFibRequest, this, _2)); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void |
| 84 | FibManager::onFibRequest(const Interest& request) |
| 85 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 86 | const Name& command = request.getName(); |
| 87 | const size_t commandNComps = command.size(); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 88 | |
| 89 | /// \todo Separate out response status codes 400 and 401 |
| 90 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 91 | if (FIB_MANAGER_COMMAND_UNSIGNED_NCOMPS <= commandNComps && |
| 92 | commandNComps < FIB_MANAGER_COMMAND_SIGNED_NCOMPS) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 93 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 94 | NFD_LOG_INFO("Unsigned command: " << command); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 95 | sendResponse(command, 401, "Signature required"); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 96 | |
| 97 | return; |
| 98 | } |
| 99 | else if (commandNComps < FIB_MANAGER_COMMAND_SIGNED_NCOMPS || |
| 100 | !FIB_MANAGER_COMMAND_PREFIX.isPrefixOf(command)) |
| 101 | { |
| 102 | NFD_LOG_INFO("Malformed command: " << command); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 103 | sendResponse(command, 400, "Malformed command"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 107 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 108 | const Name::Component& verb = command.get(FIB_MANAGER_COMMAND_PREFIX.size()); |
| 109 | |
| 110 | VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find (verb); |
| 111 | if (verbProcessor != m_verbDispatch.end()) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 112 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 113 | NFD_LOG_INFO("Processing command verb: " << verb); |
| 114 | (verbProcessor->second)(this, request); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 115 | } |
| 116 | else |
| 117 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 118 | NFD_LOG_INFO("Unsupported command verb: " << verb); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 119 | sendResponse(request.getName(), 501, "Unsupported command"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | FibManager::fibInsert(const Interest& request) |
| 126 | { |
| 127 | |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | FibManager::fibDelete(const Interest& request) |
| 132 | { |
| 133 | |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | FibManager::fibAddNextHop(const Interest& request) |
| 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 | ndn::FibManagementOptions options; |
| 141 | const size_t optionCompIndex = |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 142 | FIB_MANAGER_COMMAND_PREFIX.size() + 1; |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 143 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 144 | const ndn::Buffer& optionBuffer = |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 145 | request.getName()[optionCompIndex].getValue(); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 146 | shared_ptr<const ndn::Buffer> tmpOptionBuffer(make_shared<ndn::Buffer>(optionBuffer)); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 147 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 148 | try |
| 149 | { |
| 150 | Block rawOptions(tmpOptionBuffer); |
| 151 | options.wireDecode(rawOptions); |
| 152 | } |
| 153 | catch (const ndn::Tlv::Error& e) |
| 154 | { |
| 155 | NFD_LOG_INFO("Bad command option parse: " << command); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 156 | sendResponse(request.getName(), 400, "Malformed command"); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 157 | return; |
| 158 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 159 | |
| 160 | /// \todo authorize command |
| 161 | if (false) |
| 162 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 163 | NFD_LOG_INFO("Unauthorized command attempt: " << command); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 164 | sendResponse(request.getName(), 403, "Unauthorized command"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
| 168 | NFD_LOG_INFO("add-nexthop Name: " << options.getName() |
| 169 | << " FaceId: " << options.getFaceId() |
| 170 | << " Cost: " << options.getCost()); |
| 171 | |
| 172 | shared_ptr<Face> nextHopFace = m_getFace(options.getFaceId()); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 173 | if (static_cast<bool>(nextHopFace)) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 174 | { |
| 175 | std::pair<shared_ptr<fib::Entry>, bool> insertResult = m_managedFib.insert(options.getName()); |
| 176 | insertResult.first->addNextHop(nextHopFace, options.getCost()); |
| 177 | sendResponse(request.getName(), 200, "OK"); |
| 178 | } |
| 179 | else |
| 180 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 181 | NFD_LOG_INFO("Unknown FaceId: " << command); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 182 | sendResponse(request.getName(), 400, "Malformed command"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | FibManager::fibRemoveNextHop(const Interest& request) |
| 188 | { |
| 189 | |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | FibManager::fibStrategy(const Interest& request) |
| 194 | { |
| 195 | |
| 196 | } |
| 197 | |
| 198 | // void |
| 199 | // FibManager::onConfig(ConfigFile::Node section, bool isDryRun) |
| 200 | // { |
| 201 | |
| 202 | // } |
| 203 | |
| 204 | } // namespace nfd |