blob: a84d7f634f2e6a9fce2b14c9fe3402099700749e [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
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
219 RibEntry ribEntry;
220 ribEntry.name = parameters.getName();
221 ribEntry.faceId = parameters.getFaceId();
222 ribEntry.origin = parameters.getOrigin();
223 ribEntry.cost = parameters.getCost();
224 ribEntry.flags = parameters.getFlags();
225 ribEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
226
227 NFD_LOG_TRACE("register prefix: " << ribEntry);
228
229 // For right now, just pass the options to fib as it is,
230 // without processing flags. Later options will be first added to
231 // Rib tree, then nrd will generate fib updates based on flags and then
232 // will add next hops one by one..
233 m_managedRib.insert(ribEntry);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700234 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700235 ControlParameters()
236 .setName(ribEntry.name)
237 .setFaceId(ribEntry.faceId)
238 .setCost(ribEntry.cost),
239 bind(&RibManager::onRegSuccess, this, request, parameters, ribEntry),
240 bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry));
241}
242
243void
244RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
245 ControlParameters& parameters)
246{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700247 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700248
249 if (!validateParameters(command, parameters))
250 {
251 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
252 sendResponse(request->getName(), 400, "Malformed command");
253 return;
254 }
255
256 RibEntry ribEntry;
257 ribEntry.name = parameters.getName();
258 ribEntry.faceId = parameters.getFaceId();
259 ribEntry.origin = parameters.getOrigin();
260
261 NFD_LOG_TRACE("unregister prefix: " << ribEntry);
262
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700263 m_nfdController.start<ndn::nfd::FibRemoveNextHopCommand>(
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700264 ControlParameters()
265 .setName(ribEntry.name)
266 .setFaceId(ribEntry.faceId),
267 bind(&RibManager::onUnRegSuccess, this, request, parameters, ribEntry),
268 bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry));
269}
270
271void
272RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
273 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700274{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700275 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700276 sendResponse(request->getName(), 403, failureInfo);
277}
278
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700279
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700280bool
281RibManager::extractParameters(const Name::Component& parameterComponent,
282 ControlParameters& extractedParameters)
283{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700284 try
285 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700286 Block rawParameters = parameterComponent.blockFromValue();
287 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700288 }
289 catch (const ndn::Tlv::Error& e)
290 {
291 return false;
292 }
293
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700294 NFD_LOG_DEBUG("Parameters parsed OK");
295 return true;
296}
297
298bool
299RibManager::validateParameters(const ControlCommand& command,
300 ControlParameters& parameters)
301{
302 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700303 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700304 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700305 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700306 catch (const ControlCommand::ArgumentError&)
307 {
308 return false;
309 }
310
311 command.applyDefaultsToRequest(parameters);
312
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700313 return true;
314}
315
316void
317RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700318 const shared_ptr<const Interest>& request,
319 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700320{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700321 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700322
323 ControlResponse response;
324
325 if (code == 404)
326 {
327 response.setCode(code);
328 response.setText(error);
329 }
330 else
331 {
332 response.setCode(533);
333 std::ostringstream os;
334 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
335 response.setText(os.str());
336 }
337
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700338 sendResponse(request->getName(), response);
339 m_managedRib.erase(ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700340}
341
342void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700343RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
344 const ControlParameters& parameters,
345 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700346{
347 ControlResponse response;
348
349 response.setCode(200);
350 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700351 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700352
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700353 NFD_LOG_TRACE("onRegSuccess: registered " << ribEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700354
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700355 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700356}
357
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700358
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700359void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700360RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
361 const ControlParameters& parameters,
362 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700363{
364 ControlResponse response;
365
366 response.setCode(200);
367 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700368 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700369
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700370 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700371
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700372 sendResponse(request->getName(), response);
373 m_managedRib.erase(ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700374}
375
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700376void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700377RibManager::onNrdCommandPrefixAddNextHopSuccess(const Name& prefix)
378{
379 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
380}
381
382void
383RibManager::onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg)
384{
385 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
386}
387
388void
389RibManager::onAddNextHopSuccess(const Name& prefix)
390{
391 NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD");
392}
393
394void
395RibManager::onAddNextHopError(const Name& name, const std::string& msg)
396{
397 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
398}
399
400void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700401RibManager::onControlHeaderSuccess()
402{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700403 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700404}
405
406void
407RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
408{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700409 std::ostringstream os;
410 os << "Couldn't enable local control header "
411 << "(code: " << code << ", info: " << reason << ")";
412 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700413}
414
415void
416RibManager::enableLocalControlHeader()
417{
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700418 m_nfdController.start<ndn::nfd::FaceEnableLocalControlCommand>(
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700419 ControlParameters()
420 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
421 bind(&RibManager::onControlHeaderSuccess, this),
422 bind(&RibManager::onControlHeaderError, this, _1, _2));
423}
424
425void
426RibManager::onNotification(const FaceEventNotification& notification)
427{
428 /// \todo A notification can be missed, in this case check Facelist
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700429 NFD_LOG_TRACE("onNotification: " << notification);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700430 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { //face destroyed
431 m_managedRib.erase(notification.getFaceId());
432 }
433}
434
435} // namespace rib
436} // namespace nfd