blob: 34554e6bd847f0327e8e184dc7a2911f62851469 [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
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -06009#include "core/logger.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070010#include "table/fib.hpp"
11#include "fw/forwarder.hpp"
12#include "mgmt/internal-face.hpp"
13#include "mgmt/app-face.hpp"
14
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070015#include <ndn-cpp-dev/encoding/tlv.hpp>
16
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070017namespace nfd {
18
19NFD_LOG_INIT("FibManager");
20
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070021const Name FibManager::COMMAND_PREFIX = "/localhost/nfd/fib";
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070022
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070023const size_t FibManager::COMMAND_UNSIGNED_NCOMPS =
24 FibManager::COMMAND_PREFIX.size() +
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070025 1 + // verb
Steve DiBenedetto7564d972014-03-24 14:28:46 -060026 1; // verb parameters
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070027
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070028const size_t FibManager::COMMAND_SIGNED_NCOMPS =
29 FibManager::COMMAND_UNSIGNED_NCOMPS +
30 4; // (timestamp, nonce, signed info tlv, signature tlv)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070031
Steve DiBenedetto6214e562014-03-15 16:27:04 -060032const FibManager::SignedVerbAndProcessor FibManager::SIGNED_COMMAND_VERBS[] =
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070033 {
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070034
Steve DiBenedetto6214e562014-03-15 16:27:04 -060035 SignedVerbAndProcessor(
36 Name::Component("add-nexthop"),
37 &FibManager::addNextHop
38 ),
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070039
Steve DiBenedetto6214e562014-03-15 16:27:04 -060040 SignedVerbAndProcessor(
41 Name::Component("remove-nexthop"),
42 &FibManager::removeNextHop
43 ),
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070044
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070045 };
46
Steve DiBenedetto6214e562014-03-15 16:27:04 -060047const FibManager::UnsignedVerbAndProcessor FibManager::UNSIGNED_COMMAND_VERBS[] =
48 {
49 UnsignedVerbAndProcessor(
50 Name::Component("list"),
51 &FibManager::listEntries
52 ),
53 };
54
55const Name FibManager::LIST_COMMAND_PREFIX("/localhost/nfd/fib/list");
56const size_t FibManager::LIST_COMMAND_NCOMPS = LIST_COMMAND_PREFIX.size();
57
58
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070059FibManager::FibManager(Fib& fib,
Steve DiBenedetto3970c892014-01-31 23:31:13 -070060 function<shared_ptr<Face>(FaceId)> getFace,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070061 shared_ptr<InternalFace> face)
Steve DiBenedetto6214e562014-03-15 16:27:04 -060062 : 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 DiBenedetto042bfe92014-01-30 15:05:08 -070072{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070073 face->setInterestFilter("/localhost/nfd/fib",
74 bind(&FibManager::onFibRequest, this, _2));
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070075}
76
Steve DiBenedettod030cfc2014-03-10 20:04:47 -060077FibManager::~FibManager()
78{
79
80}
81
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070082void
83FibManager::onFibRequest(const Interest& request)
84{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070085 const Name& command = request.getName();
86 const size_t commandNComps = command.size();
Steve DiBenedetto6214e562014-03-15 16:27:04 -060087 const Name::Component& verb = command.get(COMMAND_PREFIX.size());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070088
Steve DiBenedetto6214e562014-03-15 16:27:04 -060089 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 DiBenedetto7564d972014-03-24 14:28:46 -060096 commandNComps < COMMAND_SIGNED_NCOMPS)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070097 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070098 NFD_LOG_INFO("command result: unsigned verb: " << command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -070099 sendResponse(command, 401, "Signature required");
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700100 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700101 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600102 !COMMAND_PREFIX.isPrefixOf(command))
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700103 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700104 NFD_LOG_INFO("command result: malformed");
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700105 sendResponse(command, 400, "Malformed command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700106 }
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600107 else
108 {
109 validate(request,
110 bind(&FibManager::onValidatedFibRequest, this, _1),
111 bind(&ManagerBase::onCommandValidationFailed, this, _1, _2));
112 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700113}
114
115void
116FibManager::onValidatedFibRequest(const shared_ptr<const Interest>& request)
117{
118 const Name& command = request->getName();
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600119 const Name::Component& verb = command[COMMAND_PREFIX.size()];
120 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700121
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600122 SignedVerbDispatchTable::const_iterator signedVerbProcessor = m_signedVerbDispatch.find (verb);
123 if (signedVerbProcessor != m_signedVerbDispatch.end())
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700124 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600125 ControlParameters parameters;
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600126 if (!extractParameters(parameterComponent, parameters) || !parameters.hasFaceId())
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700127 {
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700128 NFD_LOG_INFO("command result: malformed verb: " << verb);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700129 sendResponse(command, 400, "Malformed command");
130 return;
131 }
132
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600133 if (parameters.getFaceId() == 0)
134 {
135 parameters.setFaceId(request->getIncomingFaceId());
136 }
137
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700138 NFD_LOG_INFO("command result: processing verb: " << verb);
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800139 ControlResponse response;
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600140 (signedVerbProcessor->second)(this, parameters, response);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700141 sendResponse(command, response);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700142 }
143 else
144 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700145 NFD_LOG_INFO("command result: unsupported verb: " << verb);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700146 sendResponse(command, 501, "Unsupported command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700147 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700148}
149
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700150void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600151FibManager::addNextHop(ControlParameters& parameters,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800152 ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700153{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600154 ndn::nfd::FibAddNextHopCommand command;
155
156 if (!validateParameters(command, parameters))
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600157 {
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600158 NFD_LOG_INFO("add-nexthop result: FAIL reason: malformed");
159 setResponse(response, 400, "Malformed command");
160 return;
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600161 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700162
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600163 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 DiBenedettobdedce92014-02-02 22:49:39 -0700172 if (static_cast<bool>(nextHopFace))
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700173 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600174 shared_ptr<fib::Entry> entry = m_managedFib.insert(prefix).first;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700175
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600176 entry->addNextHop(nextHopFace, cost);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600177
178 NFD_LOG_INFO("add-nexthop result: OK"
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600179 << " prefix:" << prefix
180 << " faceid: " << faceId
181 << " cost: " << cost);
182
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600183 NFD_LOG_INFO("add-nexthop result: SUCCESS");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600184 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700185 }
186 else
187 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600188 NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-faceid: " << faceId);
189 setResponse(response, 410, "Face not found");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700190 }
191}
192
193void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600194FibManager::removeNextHop(ControlParameters& parameters,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700195 ControlResponse& response)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700196{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600197 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 DiBenedetto7564d972014-03-24 14:28:46 -0600205 NFD_LOG_DEBUG("remove-nexthop prefix: " << parameters.getName()
206 << " faceid: " << parameters.getFaceId());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700207
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600208 shared_ptr<Face> faceToRemove = m_getFace(parameters.getFaceId());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700209 if (static_cast<bool>(faceToRemove))
210 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600211 shared_ptr<fib::Entry> entry = m_managedFib.findExactMatch(parameters.getName());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700212 if (static_cast<bool>(entry))
213 {
214 entry->removeNextHop(faceToRemove);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600215 NFD_LOG_INFO("remove-nexthop result: OK prefix: " << parameters.getName()
216 << " faceid: " << parameters.getFaceId());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700217
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600218 if (!entry->hasNextHops())
219 {
220 m_managedFib.erase(*entry);
221 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700222 }
223 }
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600224
225 NFD_LOG_INFO("remove-nexthop result: SUCCESS");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600226 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700227}
228
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600229void
230FibManager::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 DiBenedetto042bfe92014-01-30 15:05:08 -0700246} // namespace nfd