blob: f485c180fe9e368d4af77631402f0942c574cd4c [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
60RibManager::RibManager()
Alexander Afanasyevf6980282014-05-13 18:28:40 -070061 : m_face(getGlobalIoService())
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070062 , m_nfdController(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070063 , m_localhostValidator(m_face)
64 , m_localhopValidator(m_face)
Yingdi Yuf4db0b52014-04-17 13:17:39 -070065 , m_faceMonitor(m_face)
Yingdi Yue5224e92014-04-29 18:04:02 -070066 , m_isLocalhopEnabled(false)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070067 , m_verbDispatch(COMMAND_VERBS,
68 COMMAND_VERBS + (sizeof(COMMAND_VERBS) / sizeof(VerbAndProcessor)))
69{
70}
71
72void
73RibManager::registerWithNfd()
74{
75 //check whether the components of localhop and localhost prefixes are same
76 BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size());
77
Alexander Afanasyev20d31442014-04-19 17:00:53 -070078 NFD_LOG_INFO("Setting interest filter on: " << COMMAND_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070079
80 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
81 ControlParameters()
82 .setName(COMMAND_PREFIX),
83 bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(COMMAND_PREFIX)),
84 bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(COMMAND_PREFIX), _2));
85
86 m_face.setInterestFilter(COMMAND_PREFIX, bind(&RibManager::onLocalhostRequest, this, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070087
Yingdi Yue5224e92014-04-29 18:04:02 -070088 if (m_isLocalhopEnabled)
89 {
90 NFD_LOG_INFO("Setting interest filter on: " << REMOTE_COMMAND_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070091
92 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
93 ControlParameters()
94 .setName(COMMAND_PREFIX),
95 bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(REMOTE_COMMAND_PREFIX)),
96 bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(REMOTE_COMMAND_PREFIX), _2));
97
Yingdi Yue5224e92014-04-29 18:04:02 -070098 m_face.setInterestFilter(REMOTE_COMMAND_PREFIX,
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070099 bind(&RibManager::onLocalhopRequest, this, _2));
Yingdi Yue5224e92014-04-29 18:04:02 -0700100 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700101
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700102 NFD_LOG_INFO("Start monitoring face create/destroy events");
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700103 m_faceMonitor.addSubscriber(bind(&RibManager::onNotification, this, _1));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700104 m_faceMonitor.startNotifications();
105}
106
107void
108RibManager::setConfigFile(ConfigFile& configFile)
109{
Yingdi Yue5224e92014-04-29 18:04:02 -0700110 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700111 bind(&RibManager::onConfig, this, _1, _2, _3));
112}
113
114void
115RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700116 bool isDryRun,
117 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700118{
Yingdi Yue5224e92014-04-29 18:04:02 -0700119 for (ConfigSection::const_iterator i = configSection.begin();
120 i != configSection.end(); ++i)
121 {
122 if (i->first == "localhost_security")
123 m_localhostValidator.load(i->second, filename);
124 else if (i->first == "localhop_security")
125 {
126 m_localhopValidator.load(i->second, filename);
127 m_isLocalhopEnabled = true;
128 }
129 else
130 throw Error("Unrecognized rib property: " + i->first);
131 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700132}
133
134void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700135RibManager::sendResponse(const Name& name,
136 const ControlResponse& response)
137{
138 const Block& encodedControl = response.wireEncode();
139
140 Data responseData(name);
141 responseData.setContent(encodedControl);
142
143 m_keyChain.sign(responseData);
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700144 m_face.put(responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700145}
146
147void
148RibManager::sendResponse(const Name& name,
149 uint32_t code,
150 const std::string& text)
151{
152 ControlResponse response(code, text);
153 sendResponse(name, response);
154}
155
156void
Yingdi Yue5224e92014-04-29 18:04:02 -0700157RibManager::onLocalhostRequest(const Interest& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700158{
Yingdi Yue5224e92014-04-29 18:04:02 -0700159 m_localhostValidator.validate(request,
160 bind(&RibManager::onCommandValidated, this, _1),
161 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
162}
163
164void
165RibManager::onLocalhopRequest(const Interest& request)
166{
167 m_localhopValidator.validate(request,
168 bind(&RibManager::onCommandValidated, this, _1),
169 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700170}
171
172void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700173RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700174{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700175 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700176 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700177
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700178 const Name& command = request->getName();
179 const Name::Component& verb = command[COMMAND_PREFIX.size()];
180 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
181
182 VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700183 if (verbProcessor != m_verbDispatch.end())
184 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700185 ControlParameters parameters;
186 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700187 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700188 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700189 sendResponse(command, 400, "Malformed command");
190 return;
191 }
192
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700193 if (!parameters.hasFaceId() || parameters.getFaceId() == 0)
194 {
195 parameters.setFaceId(request->getIncomingFaceId());
196 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700197
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700198 NFD_LOG_DEBUG("command result: processing verb: " << verb);
199 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700200 }
201 else
202 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700203 NFD_LOG_DEBUG("Unsupported command: " << verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700204 sendResponse(request->getName(), 501, "Unsupported command");
205 }
206}
207
208void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700209RibManager::registerEntry(const shared_ptr<const Interest>& request,
210 ControlParameters& parameters)
211{
212 ndn::nfd::RibRegisterCommand command;
213
214 if (!validateParameters(command, parameters))
215 {
216 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
217 sendResponse(request->getName(), 400, "Malformed command");
218 return;
219 }
220
221 RibEntry ribEntry;
222 ribEntry.name = parameters.getName();
223 ribEntry.faceId = parameters.getFaceId();
224 ribEntry.origin = parameters.getOrigin();
225 ribEntry.cost = parameters.getCost();
226 ribEntry.flags = parameters.getFlags();
227 ribEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
228
229 NFD_LOG_TRACE("register prefix: " << ribEntry);
230
231 // For right now, just pass the options to fib as it is,
232 // without processing flags. Later options will be first added to
233 // Rib tree, then nrd will generate fib updates based on flags and then
234 // will add next hops one by one..
235 m_managedRib.insert(ribEntry);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700236 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700237 ControlParameters()
238 .setName(ribEntry.name)
239 .setFaceId(ribEntry.faceId)
240 .setCost(ribEntry.cost),
241 bind(&RibManager::onRegSuccess, this, request, parameters, ribEntry),
242 bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry));
243}
244
245void
246RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
247 ControlParameters& parameters)
248{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700249 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700250
251 if (!validateParameters(command, parameters))
252 {
253 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
254 sendResponse(request->getName(), 400, "Malformed command");
255 return;
256 }
257
258 RibEntry ribEntry;
259 ribEntry.name = parameters.getName();
260 ribEntry.faceId = parameters.getFaceId();
261 ribEntry.origin = parameters.getOrigin();
262
263 NFD_LOG_TRACE("unregister prefix: " << ribEntry);
264
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700265 m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>(
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700266 ControlParameters()
267 .setName(ribEntry.name)
268 .setFaceId(ribEntry.faceId),
269 bind(&RibManager::onUnRegSuccess, this, request, parameters, ribEntry),
270 bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry));
271}
272
273void
274RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
275 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700276{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700277 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700278 sendResponse(request->getName(), 403, failureInfo);
279}
280
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700281
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700282bool
283RibManager::extractParameters(const Name::Component& parameterComponent,
284 ControlParameters& extractedParameters)
285{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700286 try
287 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700288 Block rawParameters = parameterComponent.blockFromValue();
289 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700290 }
291 catch (const ndn::Tlv::Error& e)
292 {
293 return false;
294 }
295
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700296 NFD_LOG_DEBUG("Parameters parsed OK");
297 return true;
298}
299
300bool
301RibManager::validateParameters(const ControlCommand& command,
302 ControlParameters& parameters)
303{
304 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700305 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700306 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700307 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700308 catch (const ControlCommand::ArgumentError&)
309 {
310 return false;
311 }
312
313 command.applyDefaultsToRequest(parameters);
314
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700315 return true;
316}
317
318void
319RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700320 const shared_ptr<const Interest>& request,
321 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700322{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700323 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700324
325 ControlResponse response;
326
327 if (code == 404)
328 {
329 response.setCode(code);
330 response.setText(error);
331 }
332 else
333 {
334 response.setCode(533);
335 std::ostringstream os;
336 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
337 response.setText(os.str());
338 }
339
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700340 sendResponse(request->getName(), response);
341 m_managedRib.erase(ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700342}
343
344void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700345RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
346 const ControlParameters& parameters,
347 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700348{
349 ControlResponse response;
350
351 response.setCode(200);
352 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700353 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700354
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700355 NFD_LOG_TRACE("onRegSuccess: registered " << ribEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700356
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700357 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700358}
359
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700360
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700361void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700362RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
363 const ControlParameters& parameters,
364 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700365{
366 ControlResponse response;
367
368 response.setCode(200);
369 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700370 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700371
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700372 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700373
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700374 sendResponse(request->getName(), response);
375 m_managedRib.erase(ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700376}
377
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700378void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700379RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix)
380{
381 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
382}
383
384void
385RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg)
386{
387 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
388}
389
390void
391RibManager::onAddNextHopSuccess(const Name& prefix)
392{
393 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
394}
395
396void
397RibManager::onAddNextHopError(const Name& name, const std::string& msg)
398{
399 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
400}
401
402void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700403RibManager::onControlHeaderSuccess()
404{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700405 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700406}
407
408void
409RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
410{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700411 std::ostringstream os;
412 os << "Couldn't enable local control header "
413 << "(code: " << code << ", info: " << reason << ")";
414 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700415}
416
417void
418RibManager::enableLocalControlHeader()
419{
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700420 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700421 ControlParameters()
422 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
423 bind(&RibManager::onControlHeaderSuccess, this),
424 bind(&RibManager::onControlHeaderError, this, _1, _2));
425}
426
427void
428RibManager::onNotification(const FaceEventNotification& notification)
429{
430 /// \todo A notification can be missed, in this case check Facelist
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700431 NFD_LOG_TRACE("onNotification: " << notification);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700432 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { //face destroyed
433 m_managedRib.erase(notification.getFaceId());
434 }
435}
436
437} // namespace rib
438} // namespace nfd