blob: 5c91547cfdb23fce8fa756fc59a42b4c28b35b63 [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;
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700146 if (!extractParameters(parameterComponent, parameters))
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
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700153 bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0);
154 if (isSelfRegistration)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600155 {
156 parameters.setFaceId(request->getIncomingFaceId());
157 }
158
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700159 NFD_LOG_DEBUG("command result: processing verb: " << verb);
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800160 ControlResponse response;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700161 (verbProcessor->second)(this, parameters, response);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700162 sendResponse(command, response);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700163 }
164 else
165 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700166 NFD_LOG_DEBUG("command result: unsupported verb: " << verb);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700167 sendResponse(command, 501, "Unsupported command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700168 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700169}
170
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700171void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600172FibManager::addNextHop(ControlParameters& parameters,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800173 ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700174{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600175 ndn::nfd::FibAddNextHopCommand command;
176
177 if (!validateParameters(command, parameters))
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600178 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700179 NFD_LOG_DEBUG("add-nexthop result: FAIL reason: malformed");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600180 setResponse(response, 400, "Malformed command");
181 return;
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600182 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700183
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600184 const Name& prefix = parameters.getName();
185 FaceId faceId = parameters.getFaceId();
186 uint64_t cost = parameters.getCost();
187
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700188 NFD_LOG_TRACE("add-nexthop prefix: " << prefix
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600189 << " faceid: " << faceId
190 << " cost: " << cost);
191
192 shared_ptr<Face> nextHopFace = m_getFace(faceId);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700193 if (static_cast<bool>(nextHopFace))
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700194 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600195 shared_ptr<fib::Entry> entry = m_managedFib.insert(prefix).first;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700196
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600197 entry->addNextHop(nextHopFace, cost);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600198
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700199 NFD_LOG_DEBUG("add-nexthop result: OK"
200 << " prefix:" << prefix
201 << " faceid: " << faceId
202 << " cost: " << cost);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600203
204 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700205 }
206 else
207 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700208 NFD_LOG_DEBUG("add-nexthop result: FAIL reason: unknown-faceid: " << faceId);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600209 setResponse(response, 410, "Face not found");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700210 }
211}
212
213void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600214FibManager::removeNextHop(ControlParameters& parameters,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700215 ControlResponse& response)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700216{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600217 ndn::nfd::FibRemoveNextHopCommand command;
218 if (!validateParameters(command, parameters))
219 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700220 NFD_LOG_DEBUG("remove-nexthop result: FAIL reason: malformed");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600221 setResponse(response, 400, "Malformed command");
222 return;
223 }
224
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700225 NFD_LOG_TRACE("remove-nexthop prefix: " << parameters.getName()
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600226 << " faceid: " << parameters.getFaceId());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700227
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600228 shared_ptr<Face> faceToRemove = m_getFace(parameters.getFaceId());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700229 if (static_cast<bool>(faceToRemove))
230 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600231 shared_ptr<fib::Entry> entry = m_managedFib.findExactMatch(parameters.getName());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700232 if (static_cast<bool>(entry))
233 {
234 entry->removeNextHop(faceToRemove);
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700235 NFD_LOG_DEBUG("remove-nexthop result: OK prefix: " << parameters.getName()
236 << " faceid: " << parameters.getFaceId());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700237
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600238 if (!entry->hasNextHops())
239 {
240 m_managedFib.erase(*entry);
241 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700242 }
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700243 else
244 {
245 NFD_LOG_DEBUG("remove-nexthop result: OK, but entry for face id "
246 << parameters.getFaceId() << " not found");
247 }
248 }
249 else
250 {
251 NFD_LOG_DEBUG("remove-nexthop result: OK, but face id "
252 << parameters.getFaceId() << " not found");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700253 }
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600254
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600255 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700256}
257
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600258void
259FibManager::listEntries(const Interest& request)
260{
261 const Name& command = request.getName();
262 const size_t commandNComps = command.size();
263
264 if (commandNComps < LIST_COMMAND_NCOMPS ||
265 !LIST_COMMAND_PREFIX.isPrefixOf(command))
266 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700267 NFD_LOG_DEBUG("command result: malformed");
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600268 sendResponse(command, 400, "Malformed command");
269 return;
270 }
271
272 m_fibEnumerationPublisher.publish();
273}
274
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700275} // namespace nfd