blob: bda16810208483e111e93e98de8a2f969679997d [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehman5144f822014-07-23 15:12:56 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Vince Lehman5144f822014-07-23 15:12:56 -070024 */
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070025
26#include "fib-manager.hpp"
27
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028#include "core/logger.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070029#include "table/fib.hpp"
30#include "fw/forwarder.hpp"
31#include "mgmt/internal-face.hpp"
32#include "mgmt/app-face.hpp"
33
Alexander Afanasyev4a771362014-04-24 21:29:33 -070034#include <ndn-cxx/encoding/tlv.hpp>
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070035
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070036namespace nfd {
37
38NFD_LOG_INIT("FibManager");
39
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070040const Name FibManager::COMMAND_PREFIX = "/localhost/nfd/fib";
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070041
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070042const size_t FibManager::COMMAND_UNSIGNED_NCOMPS =
43 FibManager::COMMAND_PREFIX.size() +
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070044 1 + // verb
Steve DiBenedetto7564d972014-03-24 14:28:46 -060045 1; // verb parameters
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070046
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070047const size_t FibManager::COMMAND_SIGNED_NCOMPS =
48 FibManager::COMMAND_UNSIGNED_NCOMPS +
49 4; // (timestamp, nonce, signed info tlv, signature tlv)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070050
Steve DiBenedetto6214e562014-03-15 16:27:04 -060051const FibManager::SignedVerbAndProcessor FibManager::SIGNED_COMMAND_VERBS[] =
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070052 {
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070053
Steve DiBenedetto6214e562014-03-15 16:27:04 -060054 SignedVerbAndProcessor(
55 Name::Component("add-nexthop"),
56 &FibManager::addNextHop
57 ),
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070058
Steve DiBenedetto6214e562014-03-15 16:27:04 -060059 SignedVerbAndProcessor(
60 Name::Component("remove-nexthop"),
61 &FibManager::removeNextHop
62 ),
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070063
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070064 };
65
Steve DiBenedetto6214e562014-03-15 16:27:04 -060066const FibManager::UnsignedVerbAndProcessor FibManager::UNSIGNED_COMMAND_VERBS[] =
67 {
68 UnsignedVerbAndProcessor(
69 Name::Component("list"),
70 &FibManager::listEntries
71 ),
72 };
73
74const Name FibManager::LIST_COMMAND_PREFIX("/localhost/nfd/fib/list");
75const size_t FibManager::LIST_COMMAND_NCOMPS = LIST_COMMAND_PREFIX.size();
76
77
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070078FibManager::FibManager(Fib& fib,
Steve DiBenedetto3970c892014-01-31 23:31:13 -070079 function<shared_ptr<Face>(FaceId)> getFace,
Vince Lehman5144f822014-07-23 15:12:56 -070080 shared_ptr<InternalFace> face,
81 ndn::KeyChain& keyChain)
82 : ManagerBase(face, FIB_PRIVILEGE, keyChain)
Steve DiBenedetto6214e562014-03-15 16:27:04 -060083 , m_managedFib(fib)
84 , m_getFace(getFace)
Vince Lehman5144f822014-07-23 15:12:56 -070085 , m_fibEnumerationPublisher(fib, *face, LIST_COMMAND_PREFIX, keyChain)
Steve DiBenedetto6214e562014-03-15 16:27:04 -060086 , m_signedVerbDispatch(SIGNED_COMMAND_VERBS,
87 SIGNED_COMMAND_VERBS +
88 (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor)))
89 , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS,
90 UNSIGNED_COMMAND_VERBS +
91 (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor)))
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070092{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070093 face->setInterestFilter("/localhost/nfd/fib",
94 bind(&FibManager::onFibRequest, this, _2));
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070095}
96
Steve DiBenedettod030cfc2014-03-10 20:04:47 -060097FibManager::~FibManager()
98{
99
100}
101
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700102void
103FibManager::onFibRequest(const Interest& request)
104{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700105 const Name& command = request.getName();
106 const size_t commandNComps = command.size();
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600107 const Name::Component& verb = command.get(COMMAND_PREFIX.size());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700108
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600109 UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
110 if (unsignedVerbProcessor != m_unsignedVerbDispatch.end())
111 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700112 NFD_LOG_DEBUG("command result: processing verb: " << verb);
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700113 (unsignedVerbProcessor->second)(this, request);
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600114 }
115 else if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600116 commandNComps < COMMAND_SIGNED_NCOMPS)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700117 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700118 NFD_LOG_DEBUG("command result: unsigned verb: " << command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700119 sendResponse(command, 401, "Signature required");
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700120 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700121 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600122 !COMMAND_PREFIX.isPrefixOf(command))
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700123 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700124 NFD_LOG_DEBUG("command result: malformed");
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700125 sendResponse(command, 400, "Malformed command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700126 }
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600127 else
128 {
129 validate(request,
130 bind(&FibManager::onValidatedFibRequest, this, _1),
131 bind(&ManagerBase::onCommandValidationFailed, this, _1, _2));
132 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700133}
134
135void
136FibManager::onValidatedFibRequest(const shared_ptr<const Interest>& request)
137{
138 const Name& command = request->getName();
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600139 const Name::Component& verb = command[COMMAND_PREFIX.size()];
140 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700141
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700142 SignedVerbDispatchTable::const_iterator verbProcessor = m_signedVerbDispatch.find(verb);
143 if (verbProcessor != m_signedVerbDispatch.end())
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700144 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600145 ControlParameters parameters;
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600146 if (!extractParameters(parameterComponent, parameters) || !parameters.hasFaceId())
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700147 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700148 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700149 sendResponse(command, 400, "Malformed command");
150 return;
151 }
152
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600153 if (parameters.getFaceId() == 0)
154 {
155 parameters.setFaceId(request->getIncomingFaceId());
156 }
157
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700158 NFD_LOG_DEBUG("command result: processing verb: " << verb);
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800159 ControlResponse response;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700160 (verbProcessor->second)(this, parameters, response);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700161 sendResponse(command, response);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700162 }
163 else
164 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700165 NFD_LOG_DEBUG("command result: unsupported verb: " << verb);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700166 sendResponse(command, 501, "Unsupported command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700167 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700168}
169
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700170void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600171FibManager::addNextHop(ControlParameters& parameters,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800172 ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700173{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600174 ndn::nfd::FibAddNextHopCommand command;
175
176 if (!validateParameters(command, parameters))
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600177 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700178 NFD_LOG_DEBUG("add-nexthop result: FAIL reason: malformed");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600179 setResponse(response, 400, "Malformed command");
180 return;
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600181 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700182
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600183 const Name& prefix = parameters.getName();
184 FaceId faceId = parameters.getFaceId();
185 uint64_t cost = parameters.getCost();
186
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700187 NFD_LOG_TRACE("add-nexthop prefix: " << prefix
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600188 << " faceid: " << faceId
189 << " cost: " << cost);
190
191 shared_ptr<Face> nextHopFace = m_getFace(faceId);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700192 if (static_cast<bool>(nextHopFace))
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700193 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600194 shared_ptr<fib::Entry> entry = m_managedFib.insert(prefix).first;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700195
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600196 entry->addNextHop(nextHopFace, cost);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600197
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700198 NFD_LOG_DEBUG("add-nexthop result: OK"
199 << " prefix:" << prefix
200 << " faceid: " << faceId
201 << " cost: " << cost);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600202
203 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700204 }
205 else
206 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700207 NFD_LOG_DEBUG("add-nexthop result: FAIL reason: unknown-faceid: " << faceId);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600208 setResponse(response, 410, "Face not found");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700209 }
210}
211
212void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600213FibManager::removeNextHop(ControlParameters& parameters,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700214 ControlResponse& response)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700215{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600216 ndn::nfd::FibRemoveNextHopCommand command;
217 if (!validateParameters(command, parameters))
218 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700219 NFD_LOG_DEBUG("remove-nexthop result: FAIL reason: malformed");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600220 setResponse(response, 400, "Malformed command");
221 return;
222 }
223
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700224 NFD_LOG_TRACE("remove-nexthop prefix: " << parameters.getName()
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600225 << " faceid: " << parameters.getFaceId());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700226
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600227 shared_ptr<Face> faceToRemove = m_getFace(parameters.getFaceId());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700228 if (static_cast<bool>(faceToRemove))
229 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600230 shared_ptr<fib::Entry> entry = m_managedFib.findExactMatch(parameters.getName());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700231 if (static_cast<bool>(entry))
232 {
233 entry->removeNextHop(faceToRemove);
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700234 NFD_LOG_DEBUG("remove-nexthop result: OK prefix: " << parameters.getName()
235 << " faceid: " << parameters.getFaceId());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700236
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600237 if (!entry->hasNextHops())
238 {
239 m_managedFib.erase(*entry);
240 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700241 }
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700242 else
243 {
244 NFD_LOG_DEBUG("remove-nexthop result: OK, but entry for face id "
245 << parameters.getFaceId() << " not found");
246 }
247 }
248 else
249 {
250 NFD_LOG_DEBUG("remove-nexthop result: OK, but face id "
251 << parameters.getFaceId() << " not found");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700252 }
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600253
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600254 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700255}
256
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600257void
258FibManager::listEntries(const Interest& request)
259{
260 const Name& command = request.getName();
261 const size_t commandNComps = command.size();
262
263 if (commandNComps < LIST_COMMAND_NCOMPS ||
264 !LIST_COMMAND_PREFIX.isPrefixOf(command))
265 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700266 NFD_LOG_DEBUG("command result: malformed");
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600267 sendResponse(command, 400, "Malformed command");
268 return;
269 }
270
271 m_fibEnumerationPublisher.publish();
272}
273
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700274} // namespace nfd