blob: 3db042dd60ffec24c72c6397c21ddc9b380c571a [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 Afanasyev89cf5e02014-04-17 12:08:57 -0700133 NFD_LOG_ERROR("Error in setting interest filter (" << name << "): " << msg);
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700134 m_face.shutdown();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700135}
136
137void
138RibManager::sendResponse(const Name& name,
139 const ControlResponse& response)
140{
141 const Block& encodedControl = response.wireEncode();
142
143 Data responseData(name);
144 responseData.setContent(encodedControl);
145
146 m_keyChain.sign(responseData);
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700147 m_face.put(responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700148}
149
150void
151RibManager::sendResponse(const Name& name,
152 uint32_t code,
153 const std::string& text)
154{
155 ControlResponse response(code, text);
156 sendResponse(name, response);
157}
158
159void
Yingdi Yue5224e92014-04-29 18:04:02 -0700160RibManager::onLocalhostRequest(const Interest& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700161{
Yingdi Yue5224e92014-04-29 18:04:02 -0700162 m_localhostValidator.validate(request,
163 bind(&RibManager::onCommandValidated, this, _1),
164 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
165}
166
167void
168RibManager::onLocalhopRequest(const Interest& request)
169{
170 m_localhopValidator.validate(request,
171 bind(&RibManager::onCommandValidated, this, _1),
172 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700173}
174
175void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700176RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700177{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700178 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700179 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700180
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700181 const Name& command = request->getName();
182 const Name::Component& verb = command[COMMAND_PREFIX.size()];
183 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
184
185 VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700186 if (verbProcessor != m_verbDispatch.end())
187 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700188 ControlParameters parameters;
189 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700190 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700191 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700192 sendResponse(command, 400, "Malformed command");
193 return;
194 }
195
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700196 if (!parameters.hasFaceId() || parameters.getFaceId() == 0)
197 {
198 parameters.setFaceId(request->getIncomingFaceId());
199 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700200
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700201 NFD_LOG_DEBUG("command result: processing verb: " << verb);
202 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700203 }
204 else
205 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700206 NFD_LOG_DEBUG("Unsupported command: " << verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700207 sendResponse(request->getName(), 501, "Unsupported command");
208 }
209}
210
211void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700212RibManager::registerEntry(const shared_ptr<const Interest>& request,
213 ControlParameters& parameters)
214{
215 ndn::nfd::RibRegisterCommand command;
216
217 if (!validateParameters(command, parameters))
218 {
219 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
220 sendResponse(request->getName(), 400, "Malformed command");
221 return;
222 }
223
224 RibEntry ribEntry;
225 ribEntry.name = parameters.getName();
226 ribEntry.faceId = parameters.getFaceId();
227 ribEntry.origin = parameters.getOrigin();
228 ribEntry.cost = parameters.getCost();
229 ribEntry.flags = parameters.getFlags();
230 ribEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
231
232 NFD_LOG_TRACE("register prefix: " << ribEntry);
233
234 // For right now, just pass the options to fib as it is,
235 // without processing flags. Later options will be first added to
236 // Rib tree, then nrd will generate fib updates based on flags and then
237 // will add next hops one by one..
238 m_managedRib.insert(ribEntry);
239 m_nfdController->start<ndn::nfd::FibAddNextHopCommand>(
240 ControlParameters()
241 .setName(ribEntry.name)
242 .setFaceId(ribEntry.faceId)
243 .setCost(ribEntry.cost),
244 bind(&RibManager::onRegSuccess, this, request, parameters, ribEntry),
245 bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry));
246}
247
248void
249RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
250 ControlParameters& parameters)
251{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700252 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700253
254 if (!validateParameters(command, parameters))
255 {
256 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
257 sendResponse(request->getName(), 400, "Malformed command");
258 return;
259 }
260
261 RibEntry ribEntry;
262 ribEntry.name = parameters.getName();
263 ribEntry.faceId = parameters.getFaceId();
264 ribEntry.origin = parameters.getOrigin();
265
266 NFD_LOG_TRACE("unregister prefix: " << ribEntry);
267
268 m_nfdController->start<ndn::nfd::FibRemoveNextHopCommand>(
269 ControlParameters()
270 .setName(ribEntry.name)
271 .setFaceId(ribEntry.faceId),
272 bind(&RibManager::onUnRegSuccess, this, request, parameters, ribEntry),
273 bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry));
274}
275
276void
277RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
278 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700279{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700280 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700281 sendResponse(request->getName(), 403, failureInfo);
282}
283
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700284
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700285bool
286RibManager::extractParameters(const Name::Component& parameterComponent,
287 ControlParameters& extractedParameters)
288{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700289 try
290 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700291 Block rawParameters = parameterComponent.blockFromValue();
292 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700293 }
294 catch (const ndn::Tlv::Error& e)
295 {
296 return false;
297 }
298
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700299 NFD_LOG_DEBUG("Parameters parsed OK");
300 return true;
301}
302
303bool
304RibManager::validateParameters(const ControlCommand& command,
305 ControlParameters& parameters)
306{
307 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700308 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700309 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700310 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700311 catch (const ControlCommand::ArgumentError&)
312 {
313 return false;
314 }
315
316 command.applyDefaultsToRequest(parameters);
317
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700318 return true;
319}
320
321void
322RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700323 const shared_ptr<const Interest>& request,
324 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700325{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700326 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700327
328 ControlResponse response;
329
330 if (code == 404)
331 {
332 response.setCode(code);
333 response.setText(error);
334 }
335 else
336 {
337 response.setCode(533);
338 std::ostringstream os;
339 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
340 response.setText(os.str());
341 }
342
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700343 sendResponse(request->getName(), response);
344 m_managedRib.erase(ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700345}
346
347void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700348RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
349 const ControlParameters& parameters,
350 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700351{
352 ControlResponse response;
353
354 response.setCode(200);
355 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700356 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700357
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700358 NFD_LOG_TRACE("onRegSuccess: registered " << ribEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700359
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700360 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700361}
362
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700363
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700364void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700365RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
366 const ControlParameters& parameters,
367 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700368{
369 ControlResponse response;
370
371 response.setCode(200);
372 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700373 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700374
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700375 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700376
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700377 sendResponse(request->getName(), response);
378 m_managedRib.erase(ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700379}
380
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700381void
382RibManager::onControlHeaderSuccess()
383{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700384 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700385}
386
387void
388RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
389{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700390 NFD_LOG_ERROR("Error: couldn't enable local control header "
391 << "(code: " << code << ", info: " << reason << ")");
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700392 m_face.shutdown();
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