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