blob: 3db26e7acb3616c2559ae01dffa0be26035f4e0d [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 DiBenedetto042bfe92014-01-30 15:05:08 -0700107
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700108 if (commandNComps <= COMMAND_PREFIX.size())
109 {
110 // command is too short to have a verb
111 NFD_LOG_DEBUG("command result: malformed");
112 sendResponse(command, 400, "Malformed command");
113 return;
114 }
115
116 const Name::Component& verb = command.at(COMMAND_PREFIX.size());
117
118 const auto unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600119 if (unsignedVerbProcessor != m_unsignedVerbDispatch.end())
120 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700121 NFD_LOG_DEBUG("command result: processing verb: " << verb);
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700122 (unsignedVerbProcessor->second)(this, request);
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600123 }
124 else if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600125 commandNComps < COMMAND_SIGNED_NCOMPS)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700126 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700127 NFD_LOG_DEBUG("command result: unsigned verb: " << command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700128 sendResponse(command, 401, "Signature required");
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700129 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700130 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600131 !COMMAND_PREFIX.isPrefixOf(command))
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700132 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700133 NFD_LOG_DEBUG("command result: malformed");
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700134 sendResponse(command, 400, "Malformed command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700135 }
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600136 else
137 {
138 validate(request,
139 bind(&FibManager::onValidatedFibRequest, this, _1),
140 bind(&ManagerBase::onCommandValidationFailed, this, _1, _2));
141 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700142}
143
144void
145FibManager::onValidatedFibRequest(const shared_ptr<const Interest>& request)
146{
147 const Name& command = request->getName();
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600148 const Name::Component& verb = command[COMMAND_PREFIX.size()];
149 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700150
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700151 SignedVerbDispatchTable::const_iterator verbProcessor = m_signedVerbDispatch.find(verb);
152 if (verbProcessor != m_signedVerbDispatch.end())
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700153 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600154 ControlParameters parameters;
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700155 if (!extractParameters(parameterComponent, parameters))
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700156 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700157 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700158 sendResponse(command, 400, "Malformed command");
159 return;
160 }
161
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700162 bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0);
163 if (isSelfRegistration)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600164 {
165 parameters.setFaceId(request->getIncomingFaceId());
166 }
167
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700168 NFD_LOG_DEBUG("command result: processing verb: " << verb);
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800169 ControlResponse response;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700170 (verbProcessor->second)(this, parameters, response);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700171 sendResponse(command, response);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700172 }
173 else
174 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700175 NFD_LOG_DEBUG("command result: unsupported verb: " << verb);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700176 sendResponse(command, 501, "Unsupported command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700177 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700178}
179
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700180void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600181FibManager::addNextHop(ControlParameters& parameters,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800182 ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700183{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600184 ndn::nfd::FibAddNextHopCommand command;
185
186 if (!validateParameters(command, parameters))
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600187 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700188 NFD_LOG_DEBUG("add-nexthop result: FAIL reason: malformed");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600189 setResponse(response, 400, "Malformed command");
190 return;
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600191 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700192
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600193 const Name& prefix = parameters.getName();
194 FaceId faceId = parameters.getFaceId();
195 uint64_t cost = parameters.getCost();
196
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700197 NFD_LOG_TRACE("add-nexthop prefix: " << prefix
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600198 << " faceid: " << faceId
199 << " cost: " << cost);
200
201 shared_ptr<Face> nextHopFace = m_getFace(faceId);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700202 if (static_cast<bool>(nextHopFace))
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700203 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600204 shared_ptr<fib::Entry> entry = m_managedFib.insert(prefix).first;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700205
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600206 entry->addNextHop(nextHopFace, cost);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600207
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700208 NFD_LOG_DEBUG("add-nexthop result: OK"
209 << " prefix:" << prefix
210 << " faceid: " << faceId
211 << " cost: " << cost);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600212
213 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700214 }
215 else
216 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700217 NFD_LOG_DEBUG("add-nexthop result: FAIL reason: unknown-faceid: " << faceId);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600218 setResponse(response, 410, "Face not found");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700219 }
220}
221
222void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600223FibManager::removeNextHop(ControlParameters& parameters,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700224 ControlResponse& response)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700225{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600226 ndn::nfd::FibRemoveNextHopCommand command;
227 if (!validateParameters(command, parameters))
228 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700229 NFD_LOG_DEBUG("remove-nexthop result: FAIL reason: malformed");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600230 setResponse(response, 400, "Malformed command");
231 return;
232 }
233
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700234 NFD_LOG_TRACE("remove-nexthop prefix: " << parameters.getName()
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600235 << " faceid: " << parameters.getFaceId());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700236
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600237 shared_ptr<Face> faceToRemove = m_getFace(parameters.getFaceId());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700238 if (static_cast<bool>(faceToRemove))
239 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600240 shared_ptr<fib::Entry> entry = m_managedFib.findExactMatch(parameters.getName());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700241 if (static_cast<bool>(entry))
242 {
243 entry->removeNextHop(faceToRemove);
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700244 NFD_LOG_DEBUG("remove-nexthop result: OK prefix: " << parameters.getName()
245 << " faceid: " << parameters.getFaceId());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700246
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600247 if (!entry->hasNextHops())
248 {
249 m_managedFib.erase(*entry);
250 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700251 }
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700252 else
253 {
254 NFD_LOG_DEBUG("remove-nexthop result: OK, but entry for face id "
255 << parameters.getFaceId() << " not found");
256 }
257 }
258 else
259 {
260 NFD_LOG_DEBUG("remove-nexthop result: OK, but face id "
261 << parameters.getFaceId() << " not found");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700262 }
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600263
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600264 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700265}
266
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600267void
268FibManager::listEntries(const Interest& request)
269{
270 const Name& command = request.getName();
271 const size_t commandNComps = command.size();
272
273 if (commandNComps < LIST_COMMAND_NCOMPS ||
274 !LIST_COMMAND_PREFIX.isPrefixOf(command))
275 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700276 NFD_LOG_DEBUG("command result: malformed");
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600277 sendResponse(command, 400, "Malformed command");
278 return;
279 }
280
281 m_fibEnumerationPublisher.publish();
282}
283
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700284} // namespace nfd