blob: 73ef71d975f49da1375c5d375c7b3498ac8e950a [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * 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
10 *
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/>.
24 **/
25
26#include "rib-manager.hpp"
Alexander Afanasyev03ea3eb2014-04-17 18:19:06 -070027#include "core/global-io.hpp"
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070028#include "core/logger.hpp"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070029
30namespace nfd {
31namespace rib {
32
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070033NFD_LOG_INIT("RibManager");
34
Alexander Afanasyev20d31442014-04-19 17:00:53 -070035const Name RibManager::COMMAND_PREFIX = "/localhost/nfd/rib";
36const Name RibManager::REMOTE_COMMAND_PREFIX = "/localhop/nfd/rib";
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070037
38const size_t RibManager::COMMAND_UNSIGNED_NCOMPS =
39 RibManager::COMMAND_PREFIX.size() +
40 1 + // verb
41 1; // verb options
42
43const size_t RibManager::COMMAND_SIGNED_NCOMPS =
44 RibManager::COMMAND_UNSIGNED_NCOMPS +
45 4; // (timestamp, nonce, signed info tlv, signature tlv)
46
47const RibManager::VerbAndProcessor RibManager::COMMAND_VERBS[] =
48 {
49 VerbAndProcessor(
50 Name::Component("register"),
Alexander Afanasyev20d31442014-04-19 17:00:53 -070051 &RibManager::registerEntry
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070052 ),
53
54 VerbAndProcessor(
55 Name::Component("unregister"),
Alexander Afanasyev20d31442014-04-19 17:00:53 -070056 &RibManager::unregisterEntry
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070057 ),
58 };
59
Alexander Afanasyev03ea3eb2014-04-17 18:19:06 -070060inline static void
61NullDeleter(boost::asio::io_service* variable)
62{
63 // do nothing
64}
65
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070066RibManager::RibManager()
Yingdi Yuf4db0b52014-04-17 13:17:39 -070067 : m_face(shared_ptr<boost::asio::io_service>(&getGlobalIoService(), &NullDeleter))
68 , m_nfdController(new ndn::nfd::Controller(m_face))
Yingdi Yue5224e92014-04-29 18:04:02 -070069 , m_localhostValidator(m_face)
70 , m_localhopValidator(m_face)
Yingdi Yuf4db0b52014-04-17 13:17:39 -070071 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070072 , m_isLocalhopEnabled(false)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070073 , m_verbDispatch(COMMAND_VERBS,
74 COMMAND_VERBS + (sizeof(COMMAND_VERBS) / sizeof(VerbAndProcessor)))
75{
76}
77
78void
79RibManager::registerWithNfd()
80{
81 //check whether the components of localhop and localhost prefixes are same
82 BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size());
83
Alexander Afanasyev20d31442014-04-19 17:00:53 -070084 NFD_LOG_INFO("Setting interest filter on: " << COMMAND_PREFIX);
Yingdi Yuf4db0b52014-04-17 13:17:39 -070085 m_face.setController(m_nfdController);
Alexander Afanasyev20d31442014-04-19 17:00:53 -070086 m_face.setInterestFilter(COMMAND_PREFIX,
Yingdi Yue5224e92014-04-29 18:04:02 -070087 bind(&RibManager::onLocalhostRequest, this, _2),
Yingdi Yuf4db0b52014-04-17 13:17:39 -070088 bind(&RibManager::setInterestFilterFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070089
Yingdi Yue5224e92014-04-29 18:04:02 -070090 if (m_isLocalhopEnabled)
91 {
92 NFD_LOG_INFO("Setting interest filter on: " << REMOTE_COMMAND_PREFIX);
93 m_face.setInterestFilter(REMOTE_COMMAND_PREFIX,
94 bind(&RibManager::onLocalhopRequest, this, _2),
95 bind(&RibManager::setInterestFilterFailed, this, _1, _2));
96 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070097
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070098 NFD_LOG_INFO("Start monitoring face create/destroy events");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070099 m_faceMonitor.addSubscriber(boost::bind(&RibManager::onNotification, this, _1));
100 m_faceMonitor.startNotifications();
101}
102
103void
104RibManager::setConfigFile(ConfigFile& configFile)
105{
Yingdi Yue5224e92014-04-29 18:04:02 -0700106 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700107 bind(&RibManager::onConfig, this, _1, _2, _3));
108}
109
110void
111RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700112 bool isDryRun,
113 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700114{
Yingdi Yue5224e92014-04-29 18:04:02 -0700115 for (ConfigSection::const_iterator i = configSection.begin();
116 i != configSection.end(); ++i)
117 {
118 if (i->first == "localhost_security")
119 m_localhostValidator.load(i->second, filename);
120 else if (i->first == "localhop_security")
121 {
122 m_localhopValidator.load(i->second, filename);
123 m_isLocalhopEnabled = true;
124 }
125 else
126 throw Error("Unrecognized rib property: " + i->first);
127 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700128}
129
130void
131RibManager::setInterestFilterFailed(const Name& name, const std::string& msg)
132{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700133 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700134}
135
136void
137RibManager::sendResponse(const Name& name,
138 const ControlResponse& response)
139{
140 const Block& encodedControl = response.wireEncode();
141
142 Data responseData(name);
143 responseData.setContent(encodedControl);
144
145 m_keyChain.sign(responseData);
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700146 m_face.put(responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700147}
148
149void
150RibManager::sendResponse(const Name& name,
151 uint32_t code,
152 const std::string& text)
153{
154 ControlResponse response(code, text);
155 sendResponse(name, response);
156}
157
158void
Yingdi Yue5224e92014-04-29 18:04:02 -0700159RibManager::onLocalhostRequest(const Interest& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700160{
Yingdi Yue5224e92014-04-29 18:04:02 -0700161 m_localhostValidator.validate(request,
162 bind(&RibManager::onCommandValidated, this, _1),
163 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
164}
165
166void
167RibManager::onLocalhopRequest(const Interest& request)
168{
169 m_localhopValidator.validate(request,
170 bind(&RibManager::onCommandValidated, this, _1),
171 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700172}
173
174void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700175RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700176{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700177 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700178 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700179
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700180 const Name& command = request->getName();
181 const Name::Component& verb = command[COMMAND_PREFIX.size()];
182 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
183
184 VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700185 if (verbProcessor != m_verbDispatch.end())
186 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700187 ControlParameters parameters;
188 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700189 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700190 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700191 sendResponse(command, 400, "Malformed command");
192 return;
193 }
194
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700195 if (!parameters.hasFaceId() || parameters.getFaceId() == 0)
196 {
197 parameters.setFaceId(request->getIncomingFaceId());
198 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700199
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700200 NFD_LOG_DEBUG("command result: processing verb: " << verb);
201 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700202 }
203 else
204 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700205 NFD_LOG_DEBUG("Unsupported command: " << verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700206 sendResponse(request->getName(), 501, "Unsupported command");
207 }
208}
209
210void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700211RibManager::registerEntry(const shared_ptr<const Interest>& request,
212 ControlParameters& parameters)
213{
214 ndn::nfd::RibRegisterCommand command;
215
216 if (!validateParameters(command, parameters))
217 {
218 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
219 sendResponse(request->getName(), 400, "Malformed command");
220 return;
221 }
222
223 RibEntry ribEntry;
224 ribEntry.name = parameters.getName();
225 ribEntry.faceId = parameters.getFaceId();
226 ribEntry.origin = parameters.getOrigin();
227 ribEntry.cost = parameters.getCost();
228 ribEntry.flags = parameters.getFlags();
229 ribEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
230
231 NFD_LOG_TRACE("register prefix: " << ribEntry);
232
233 // For right now, just pass the options to fib as it is,
234 // without processing flags. Later options will be first added to
235 // Rib tree, then nrd will generate fib updates based on flags and then
236 // will add next hops one by one..
237 m_managedRib.insert(ribEntry);
238 m_nfdController->start<ndn::nfd::FibAddNextHopCommand>(
239 ControlParameters()
240 .setName(ribEntry.name)
241 .setFaceId(ribEntry.faceId)
242 .setCost(ribEntry.cost),
243 bind(&RibManager::onRegSuccess, this, request, parameters, ribEntry),
244 bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry));
245}
246
247void
248RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
249 ControlParameters& parameters)
250{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700251 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700252
253 if (!validateParameters(command, parameters))
254 {
255 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
256 sendResponse(request->getName(), 400, "Malformed command");
257 return;
258 }
259
260 RibEntry ribEntry;
261 ribEntry.name = parameters.getName();
262 ribEntry.faceId = parameters.getFaceId();
263 ribEntry.origin = parameters.getOrigin();
264
265 NFD_LOG_TRACE("unregister prefix: " << ribEntry);
266
267 m_nfdController->start<ndn::nfd::FibRemoveNextHopCommand>(
268 ControlParameters()
269 .setName(ribEntry.name)
270 .setFaceId(ribEntry.faceId),
271 bind(&RibManager::onUnRegSuccess, this, request, parameters, ribEntry),
272 bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry));
273}
274
275void
276RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
277 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700278{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700279 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700280 sendResponse(request->getName(), 403, failureInfo);
281}
282
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700283
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700284bool
285RibManager::extractParameters(const Name::Component& parameterComponent,
286 ControlParameters& extractedParameters)
287{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700288 try
289 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700290 Block rawParameters = parameterComponent.blockFromValue();
291 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700292 }
293 catch (const ndn::Tlv::Error& e)
294 {
295 return false;
296 }
297
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700298 NFD_LOG_DEBUG("Parameters parsed OK");
299 return true;
300}
301
302bool
303RibManager::validateParameters(const ControlCommand& command,
304 ControlParameters& parameters)
305{
306 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700307 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700308 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700309 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700310 catch (const ControlCommand::ArgumentError&)
311 {
312 return false;
313 }
314
315 command.applyDefaultsToRequest(parameters);
316
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700317 return true;
318}
319
320void
321RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700322 const shared_ptr<const Interest>& request,
323 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700324{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700325 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700326
327 ControlResponse response;
328
329 if (code == 404)
330 {
331 response.setCode(code);
332 response.setText(error);
333 }
334 else
335 {
336 response.setCode(533);
337 std::ostringstream os;
338 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
339 response.setText(os.str());
340 }
341
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700342 sendResponse(request->getName(), response);
343 m_managedRib.erase(ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700344}
345
346void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700347RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
348 const ControlParameters& parameters,
349 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700350{
351 ControlResponse response;
352
353 response.setCode(200);
354 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700355 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700356
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700357 NFD_LOG_TRACE("onRegSuccess: registered " << ribEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700358
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700359 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700360}
361
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700362
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700363void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700364RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
365 const ControlParameters& parameters,
366 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700367{
368 ControlResponse response;
369
370 response.setCode(200);
371 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700372 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700373
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700374 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700375
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700376 sendResponse(request->getName(), response);
377 m_managedRib.erase(ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700378}
379
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700380void
381RibManager::onControlHeaderSuccess()
382{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700383 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700384}
385
386void
387RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
388{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700389 std::ostringstream os;
390 os << "Couldn't enable local control header "
391 << "(code: " << code << ", info: " << reason << ")";
392 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700393}
394
395void
396RibManager::enableLocalControlHeader()
397{
398 m_nfdController->start<ndn::nfd::FaceEnableLocalControlCommand>(
399 ControlParameters()
400 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
401 bind(&RibManager::onControlHeaderSuccess, this),
402 bind(&RibManager::onControlHeaderError, this, _1, _2));
403}
404
405void
406RibManager::onNotification(const FaceEventNotification& notification)
407{
408 /// \todo A notification can be missed, in this case check Facelist
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700409 NFD_LOG_TRACE("onNotification: " << notification);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700410 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { //face destroyed
411 m_managedRib.erase(notification.getFaceId());
412 }
413}
414
415} // namespace rib
416} // namespace nfd