blob: b89b619593f5e45b621a421f785d5b1006414689 [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince12e49462014-06-09 13:29:32 -05003 * 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 Afanasyev3ecec502014-04-16 13:42:44 -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/>.
Vince12e49462014-06-09 13:29:32 -050024 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070025
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
Junxiao Shia3295742014-05-16 22:40:10 -070073RibManager::startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest)
74{
75 NFD_LOG_INFO("Listening on: " << commandPrefix);
76
77 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
78 ControlParameters()
79 .setName(commandPrefix)
80 .setFaceId(0),
81 bind(&RibManager::onNrdCommandPrefixAddNextHopSuccess, this, cref(commandPrefix)),
82 bind(&RibManager::onNrdCommandPrefixAddNextHopError, this, cref(commandPrefix), _2));
83
84 m_face.setInterestFilter(commandPrefix, onRequest);
85}
86
87void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070088RibManager::registerWithNfd()
89{
90 //check whether the components of localhop and localhost prefixes are same
91 BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size());
92
Junxiao Shia3295742014-05-16 22:40:10 -070093 this->startListening(COMMAND_PREFIX, bind(&RibManager::onLocalhostRequest, this, _2));
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070094
Junxiao Shia3295742014-05-16 22:40:10 -070095 if (m_isLocalhopEnabled) {
96 this->startListening(REMOTE_COMMAND_PREFIX,
97 bind(&RibManager::onLocalhopRequest, this, _2));
98 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070099
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700100 NFD_LOG_INFO("Start monitoring face create/destroy events");
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700101 m_faceMonitor.addSubscriber(bind(&RibManager::onNotification, this, _1));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700102 m_faceMonitor.startNotifications();
103}
104
105void
106RibManager::setConfigFile(ConfigFile& configFile)
107{
Yingdi Yue5224e92014-04-29 18:04:02 -0700108 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700109 bind(&RibManager::onConfig, this, _1, _2, _3));
110}
111
112void
113RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700114 bool isDryRun,
115 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700116{
Yingdi Yue5224e92014-04-29 18:04:02 -0700117 for (ConfigSection::const_iterator i = configSection.begin();
118 i != configSection.end(); ++i)
119 {
120 if (i->first == "localhost_security")
121 m_localhostValidator.load(i->second, filename);
122 else if (i->first == "localhop_security")
123 {
124 m_localhopValidator.load(i->second, filename);
125 m_isLocalhopEnabled = true;
126 }
127 else
128 throw Error("Unrecognized rib property: " + i->first);
129 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700130}
131
132void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700133RibManager::sendResponse(const Name& name,
134 const ControlResponse& response)
135{
136 const Block& encodedControl = response.wireEncode();
137
138 Data responseData(name);
139 responseData.setContent(encodedControl);
140
141 m_keyChain.sign(responseData);
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700142 m_face.put(responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700143}
144
145void
146RibManager::sendResponse(const Name& name,
147 uint32_t code,
148 const std::string& text)
149{
150 ControlResponse response(code, text);
151 sendResponse(name, response);
152}
153
154void
Yingdi Yue5224e92014-04-29 18:04:02 -0700155RibManager::onLocalhostRequest(const Interest& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700156{
Yingdi Yue5224e92014-04-29 18:04:02 -0700157 m_localhostValidator.validate(request,
158 bind(&RibManager::onCommandValidated, this, _1),
159 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
160}
161
162void
163RibManager::onLocalhopRequest(const Interest& request)
164{
165 m_localhopValidator.validate(request,
166 bind(&RibManager::onCommandValidated, this, _1),
167 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700168}
169
170void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700171RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700172{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700173 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700174 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700175
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700176 const Name& command = request->getName();
177 const Name::Component& verb = command[COMMAND_PREFIX.size()];
178 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
179
180 VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700181 if (verbProcessor != m_verbDispatch.end())
182 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700183 ControlParameters parameters;
184 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700185 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700186 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700187 sendResponse(command, 400, "Malformed command");
188 return;
189 }
190
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700191 if (!parameters.hasFaceId() || parameters.getFaceId() == 0)
192 {
193 parameters.setFaceId(request->getIncomingFaceId());
194 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700195
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700196 NFD_LOG_DEBUG("command result: processing verb: " << verb);
197 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700198 }
199 else
200 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700201 NFD_LOG_DEBUG("Unsupported command: " << verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700202 sendResponse(request->getName(), 501, "Unsupported command");
203 }
204}
205
206void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700207RibManager::registerEntry(const shared_ptr<const Interest>& request,
208 ControlParameters& parameters)
209{
210 ndn::nfd::RibRegisterCommand command;
211
212 if (!validateParameters(command, parameters))
213 {
214 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
215 sendResponse(request->getName(), 400, "Malformed command");
216 return;
217 }
218
Vince12e49462014-06-09 13:29:32 -0500219 FaceEntry faceEntry;
220 faceEntry.faceId = parameters.getFaceId();
221 faceEntry.origin = parameters.getOrigin();
222 faceEntry.cost = parameters.getCost();
223 faceEntry.flags = parameters.getFlags();
224 faceEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700225
Vince12e49462014-06-09 13:29:32 -0500226 NFD_LOG_TRACE("register prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700227
Vince12e49462014-06-09 13:29:32 -0500228 /// \todo For right now, just pass the options to fib as is
229 /// without processing flags. Later, options will first be added to
230 /// Rib tree, nrd will generate fib updates based on flags, and
231 /// will then add next hops one by one.
232 m_managedRib.insert(parameters.getName(), faceEntry);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700233 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700234 ControlParameters()
Vince12e49462014-06-09 13:29:32 -0500235 .setName(parameters.getName())
236 .setFaceId(faceEntry.faceId)
237 .setCost(faceEntry.cost),
238 bind(&RibManager::onRegSuccess, this, request, parameters, faceEntry),
239 bind(&RibManager::onCommandError, this, _1, _2, request, faceEntry));
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700240}
241
242void
243RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
244 ControlParameters& parameters)
245{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700246 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700247
248 if (!validateParameters(command, parameters))
249 {
250 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
251 sendResponse(request->getName(), 400, "Malformed command");
252 return;
253 }
254
Vince12e49462014-06-09 13:29:32 -0500255 FaceEntry faceEntry;
256 faceEntry.faceId = parameters.getFaceId();
257 faceEntry.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700258
Vince12e49462014-06-09 13:29:32 -0500259 NFD_LOG_TRACE("unregister prefix: " << faceEntry);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700260
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700261 m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>(
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700262 ControlParameters()
Vince12e49462014-06-09 13:29:32 -0500263 .setName(parameters.getName())
264 .setFaceId(faceEntry.faceId),
265 bind(&RibManager::onUnRegSuccess, this, request, parameters, faceEntry),
266 bind(&RibManager::onCommandError, this, _1, _2, request, faceEntry));
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700267}
268
269void
270RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
271 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700272{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700273 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700274 sendResponse(request->getName(), 403, failureInfo);
275}
276
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700277
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700278bool
279RibManager::extractParameters(const Name::Component& parameterComponent,
280 ControlParameters& extractedParameters)
281{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700282 try
283 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700284 Block rawParameters = parameterComponent.blockFromValue();
285 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700286 }
287 catch (const ndn::Tlv::Error& e)
288 {
289 return false;
290 }
291
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700292 NFD_LOG_DEBUG("Parameters parsed OK");
293 return true;
294}
295
296bool
297RibManager::validateParameters(const ControlCommand& command,
298 ControlParameters& parameters)
299{
300 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700301 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700302 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700303 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700304 catch (const ControlCommand::ArgumentError&)
305 {
306 return false;
307 }
308
309 command.applyDefaultsToRequest(parameters);
310
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700311 return true;
312}
313
314void
315RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700316 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500317 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700318{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700319 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700320
321 ControlResponse response;
322
323 if (code == 404)
324 {
325 response.setCode(code);
326 response.setText(error);
327 }
328 else
329 {
330 response.setCode(533);
331 std::ostringstream os;
332 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
333 response.setText(os.str());
334 }
335
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700336 sendResponse(request->getName(), response);
Vince12e49462014-06-09 13:29:32 -0500337 m_managedRib.erase(request->getName(), faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700338}
339
340void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700341RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
342 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500343 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700344{
345 ControlResponse response;
346
347 response.setCode(200);
348 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700349 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700350
Vince12e49462014-06-09 13:29:32 -0500351 NFD_LOG_TRACE("onRegSuccess: registered " << faceEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700352
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700353 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700354}
355
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700356
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700357void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700358RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
359 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500360 const FaceEntry& faceEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700361{
362 ControlResponse response;
363
364 response.setCode(200);
365 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700366 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700367
Vince12e49462014-06-09 13:29:32 -0500368 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700369
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700370 sendResponse(request->getName(), response);
Vince12e49462014-06-09 13:29:32 -0500371 m_managedRib.erase(request->getName(), faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700372}
373
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700374void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700375RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix)
376{
377 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
378}
379
380void
381RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg)
382{
383 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
384}
385
386void
387RibManager::onAddNextHopSuccess(const Name& prefix)
388{
389 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
390}
391
392void
393RibManager::onAddNextHopError(const Name& name, const std::string& msg)
394{
395 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
396}
397
398void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700399RibManager::onControlHeaderSuccess()
400{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700401 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700402}
403
404void
405RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
406{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700407 std::ostringstream os;
408 os << "Couldn't enable local control header "
409 << "(code: " << code << ", info: " << reason << ")";
410 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700411}
412
413void
414RibManager::enableLocalControlHeader()
415{
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700416 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700417 ControlParameters()
418 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
419 bind(&RibManager::onControlHeaderSuccess, this),
420 bind(&RibManager::onControlHeaderError, this, _1, _2));
421}
422
423void
424RibManager::onNotification(const FaceEventNotification& notification)
425{
426 /// \todo A notification can be missed, in this case check Facelist
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700427 NFD_LOG_TRACE("onNotification: " << notification);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700428 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { //face destroyed
429 m_managedRib.erase(notification.getFaceId());
430 }
431}
432
433} // namespace rib
434} // namespace nfd