blob: 9ce7a6f53ccc498d81ec729704c1bdae0711b190 [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- 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 DiBenedetto80ddc212014-02-01 22:23:56 -070014#include <ndn-cpp-dev/encoding/tlv.hpp>
15
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070016namespace nfd {
17
18NFD_LOG_INIT("FibManager");
19
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070020const Name FibManager::COMMAND_PREFIX = "/localhost/nfd/fib";
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070021
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070022const size_t FibManager::COMMAND_UNSIGNED_NCOMPS =
23 FibManager::COMMAND_PREFIX.size() +
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070024 1 + // verb
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070025 1; // verb options
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070026
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070027const size_t FibManager::COMMAND_SIGNED_NCOMPS =
28 FibManager::COMMAND_UNSIGNED_NCOMPS +
29 4; // (timestamp, nonce, signed info tlv, signature tlv)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070030
Steve DiBenedetto6214e562014-03-15 16:27:04 -060031const FibManager::SignedVerbAndProcessor FibManager::SIGNED_COMMAND_VERBS[] =
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070032 {
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070033
Steve DiBenedetto6214e562014-03-15 16:27:04 -060034 SignedVerbAndProcessor(
35 Name::Component("add-nexthop"),
36 &FibManager::addNextHop
37 ),
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070038
Steve DiBenedetto6214e562014-03-15 16:27:04 -060039 SignedVerbAndProcessor(
40 Name::Component("remove-nexthop"),
41 &FibManager::removeNextHop
42 ),
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070043
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070044 };
45
Steve DiBenedetto6214e562014-03-15 16:27:04 -060046const FibManager::UnsignedVerbAndProcessor FibManager::UNSIGNED_COMMAND_VERBS[] =
47 {
48 UnsignedVerbAndProcessor(
49 Name::Component("list"),
50 &FibManager::listEntries
51 ),
52 };
53
54const Name FibManager::LIST_COMMAND_PREFIX("/localhost/nfd/fib/list");
55const size_t FibManager::LIST_COMMAND_NCOMPS = LIST_COMMAND_PREFIX.size();
56
57
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070058FibManager::FibManager(Fib& fib,
Steve DiBenedetto3970c892014-01-31 23:31:13 -070059 function<shared_ptr<Face>(FaceId)> getFace,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070060 shared_ptr<InternalFace> face)
Steve DiBenedetto6214e562014-03-15 16:27:04 -060061 : 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 DiBenedetto042bfe92014-01-30 15:05:08 -070071{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070072 face->setInterestFilter("/localhost/nfd/fib",
73 bind(&FibManager::onFibRequest, this, _2));
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070074}
75
Steve DiBenedettod030cfc2014-03-10 20:04:47 -060076FibManager::~FibManager()
77{
78
79}
80
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070081void
82FibManager::onFibRequest(const Interest& request)
83{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070084 const Name& command = request.getName();
85 const size_t commandNComps = command.size();
Steve DiBenedetto6214e562014-03-15 16:27:04 -060086 const Name::Component& verb = command.get(COMMAND_PREFIX.size());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070087
Steve DiBenedetto6214e562014-03-15 16:27:04 -060088 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 DiBenedetto2c2b8892014-02-27 11:46:48 -070095 commandNComps < COMMAND_SIGNED_NCOMPS)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070096 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070097 NFD_LOG_INFO("command result: unsigned verb: " << command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -070098 sendResponse(command, 401, "Signature required");
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070099 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700100 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
101 !COMMAND_PREFIX.isPrefixOf(command))
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700102 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700103 NFD_LOG_INFO("command result: malformed");
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700104 sendResponse(command, 400, "Malformed command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700105 }
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600106 else
107 {
108 validate(request,
109 bind(&FibManager::onValidatedFibRequest, this, _1),
110 bind(&ManagerBase::onCommandValidationFailed, this, _1, _2));
111 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700112}
113
114void
115FibManager::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 DiBenedetto80ddc212014-02-01 22:23:56 -0700119
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600120 SignedVerbDispatchTable::const_iterator signedVerbProcessor = m_signedVerbDispatch.find (verb);
121 if (signedVerbProcessor != m_signedVerbDispatch.end())
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700122 {
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800123 FibManagementOptions options;
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700124 if (!extractOptions(*request, options))
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700125 {
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700126 NFD_LOG_INFO("command result: malformed verb: " << verb);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700127 sendResponse(command, 400, "Malformed command");
128 return;
129 }
130
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700131 NFD_LOG_INFO("command result: processing verb: " << verb);
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800132 ControlResponse response;
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600133 (signedVerbProcessor->second)(this, options, response);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700134 sendResponse(command, response);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700135 }
136 else
137 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700138 NFD_LOG_INFO("command result: unsupported verb: " << verb);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700139 sendResponse(command, 501, "Unsupported command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700140 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700141}
142
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700143bool
144FibManager::extractOptions(const Interest& request,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800145 FibManagementOptions& extractedOptions)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700146{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700147 const Name& command = request.getName();
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700148 const size_t optionCompIndex = COMMAND_PREFIX.size() + 1;
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700149
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700150 try
151 {
Alexander Afanasyevb78ec7d2014-02-09 12:17:25 -0800152 Block rawOptions = request.getName()[optionCompIndex].blockFromValue();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700153 extractedOptions.wireDecode(rawOptions);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700154 }
155 catch (const ndn::Tlv::Error& e)
156 {
157 NFD_LOG_INFO("Bad command option parse: " << command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700158 return false;
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700159 }
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700160
161 if (extractedOptions.getFaceId() == 0)
162 {
163 extractedOptions.setFaceId(request.getIncomingFaceId());
164 }
165
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700166 NFD_LOG_DEBUG("Options parsed OK");
167 return true;
168}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700169
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700170void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800171FibManager::addNextHop(const FibManagementOptions& options,
172 ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700173{
174 NFD_LOG_DEBUG("add-nexthop prefix: " << options.getName()
175 << " faceid: " << options.getFaceId()
176 << " cost: " << options.getCost());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700177
178 shared_ptr<Face> nextHopFace = m_getFace(options.getFaceId());
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700179 if (static_cast<bool>(nextHopFace))
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700180 {
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600181 shared_ptr<fib::Entry> entry = m_managedFib.insert(options.getName()).first;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700182
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600183 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 DiBenedetto042bfe92014-01-30 15:05:08 -0700190 }
191 else
192 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700193 NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-faceid: " << options.getFaceId());
194 setResponse(response, 404, "Face not found");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700195 }
196}
197
198void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800199FibManager::removeNextHop(const FibManagementOptions& options,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700200 ControlResponse& response)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700201{
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700202 NFD_LOG_DEBUG("remove-nexthop prefix: " << options.getName()
203 << " faceid: " << options.getFaceId());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700204
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700205 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 DiBenedettod030cfc2014-03-10 20:04:47 -0600215 if (!entry->hasNextHops())
216 {
217 m_managedFib.erase(*entry);
218 }
219
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700220 setResponse(response, 200, "Success", options.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700221 }
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 DiBenedetto042bfe92014-01-30 15:05:08 -0700235}
236
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600237void
238FibManager::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 DiBenedetto042bfe92014-01-30 15:05:08 -0700254} // namespace nfd