blob: b1b869bbf8a303afc6d0295e92f255759042cacd [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 DiBenedetto7564d972014-03-24 14:28:46 -060025 1; // verb parameters
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 DiBenedetto7564d972014-03-24 14:28:46 -060095 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 ||
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600101 !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();
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600118 const Name::Component& verb = command[COMMAND_PREFIX.size()];
119 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700120
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600121 SignedVerbDispatchTable::const_iterator signedVerbProcessor = m_signedVerbDispatch.find (verb);
122 if (signedVerbProcessor != m_signedVerbDispatch.end())
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700123 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600124 ControlParameters parameters;
125 if (!extractParameters(parameterComponent, parameters) ||
126 !parameters.hasName() ||
127 !parameters.hasFaceId())
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700128 {
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700129 NFD_LOG_INFO("command result: malformed verb: " << verb);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700130 sendResponse(command, 400, "Malformed command");
131 return;
132 }
133
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600134 if (parameters.getFaceId() == 0)
135 {
136 parameters.setFaceId(request->getIncomingFaceId());
137 }
138
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700139 NFD_LOG_INFO("command result: processing verb: " << verb);
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800140 ControlResponse response;
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600141 (signedVerbProcessor->second)(this, parameters, response);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700142 sendResponse(command, response);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700143 }
144 else
145 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700146 NFD_LOG_INFO("command result: unsupported verb: " << verb);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700147 sendResponse(command, 501, "Unsupported command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700148 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700149}
150
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700151
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700152
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700153void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600154FibManager::addNextHop(ControlParameters& parameters,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800155 ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700156{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600157 if (!parameters.hasCost())
158 {
159 parameters.setCost(0);
160 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700161
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600162 const Name& prefix = parameters.getName();
163 FaceId faceId = parameters.getFaceId();
164 uint64_t cost = parameters.getCost();
165
166 NFD_LOG_DEBUG("add-nexthop prefix: " << prefix
167 << " faceid: " << faceId
168 << " cost: " << cost);
169
170 shared_ptr<Face> nextHopFace = m_getFace(faceId);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700171 if (static_cast<bool>(nextHopFace))
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700172 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600173 shared_ptr<fib::Entry> entry = m_managedFib.insert(prefix).first;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700174
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600175 entry->addNextHop(nextHopFace, cost);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600176
177 NFD_LOG_INFO("add-nexthop result: OK"
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600178 << " prefix:" << prefix
179 << " faceid: " << faceId
180 << " cost: " << cost);
181
182 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700183 }
184 else
185 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600186 NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-faceid: " << faceId);
187 setResponse(response, 410, "Face not found");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700188 }
189}
190
191void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600192FibManager::removeNextHop(ControlParameters& parameters,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700193 ControlResponse& response)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700194{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600195 NFD_LOG_DEBUG("remove-nexthop prefix: " << parameters.getName()
196 << " faceid: " << parameters.getFaceId());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700197
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600198 shared_ptr<Face> faceToRemove = m_getFace(parameters.getFaceId());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700199 if (static_cast<bool>(faceToRemove))
200 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600201 shared_ptr<fib::Entry> entry = m_managedFib.findExactMatch(parameters.getName());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700202 if (static_cast<bool>(entry))
203 {
204 entry->removeNextHop(faceToRemove);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600205 NFD_LOG_INFO("remove-nexthop result: OK prefix: " << parameters.getName()
206 << " faceid: " << parameters.getFaceId());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700207
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600208 if (!entry->hasNextHops())
209 {
210 m_managedFib.erase(*entry);
211 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700212 }
213 }
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600214 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700215}
216
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600217void
218FibManager::listEntries(const Interest& request)
219{
220 const Name& command = request.getName();
221 const size_t commandNComps = command.size();
222
223 if (commandNComps < LIST_COMMAND_NCOMPS ||
224 !LIST_COMMAND_PREFIX.isPrefixOf(command))
225 {
226 NFD_LOG_INFO("command result: malformed");
227 sendResponse(command, 400, "Malformed command");
228 return;
229 }
230
231 m_fibEnumerationPublisher.publish();
232}
233
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700234} // namespace nfd