blob: f4fc9c800a0eb1e04f527985d0e58e8a11e96da3 [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())
Yingdi Yuf4db0b52014-04-17 13:17:39 -070062 , m_nfdController(new ndn::nfd::Controller(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);
Yingdi Yuf4db0b52014-04-17 13:17:39 -070079 m_face.setController(m_nfdController);
Alexander Afanasyev20d31442014-04-19 17:00:53 -070080 m_face.setInterestFilter(COMMAND_PREFIX,
Yingdi Yue5224e92014-04-29 18:04:02 -070081 bind(&RibManager::onLocalhostRequest, this, _2),
Yingdi Yuf4db0b52014-04-17 13:17:39 -070082 bind(&RibManager::setInterestFilterFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070083
Yingdi Yue5224e92014-04-29 18:04:02 -070084 if (m_isLocalhopEnabled)
85 {
86 NFD_LOG_INFO("Setting interest filter on: " << REMOTE_COMMAND_PREFIX);
87 m_face.setInterestFilter(REMOTE_COMMAND_PREFIX,
88 bind(&RibManager::onLocalhopRequest, this, _2),
89 bind(&RibManager::setInterestFilterFailed, this, _1, _2));
90 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070091
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070092 NFD_LOG_INFO("Start monitoring face create/destroy events");
Alexander Afanasyevf6980282014-05-13 18:28:40 -070093 m_faceMonitor.addSubscriber(bind(&RibManager::onNotification, this, _1));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070094 m_faceMonitor.startNotifications();
95}
96
97void
98RibManager::setConfigFile(ConfigFile& configFile)
99{
Yingdi Yue5224e92014-04-29 18:04:02 -0700100 configFile.addSectionHandler("rib",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700101 bind(&RibManager::onConfig, this, _1, _2, _3));
102}
103
104void
105RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700106 bool isDryRun,
107 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700108{
Yingdi Yue5224e92014-04-29 18:04:02 -0700109 for (ConfigSection::const_iterator i = configSection.begin();
110 i != configSection.end(); ++i)
111 {
112 if (i->first == "localhost_security")
113 m_localhostValidator.load(i->second, filename);
114 else if (i->first == "localhop_security")
115 {
116 m_localhopValidator.load(i->second, filename);
117 m_isLocalhopEnabled = true;
118 }
119 else
120 throw Error("Unrecognized rib property: " + i->first);
121 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700122}
123
124void
125RibManager::setInterestFilterFailed(const Name& name, const std::string& msg)
126{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700127 throw Error("Error in setting interest filter (" + name.toUri() + "): " + msg);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700128}
129
130void
131RibManager::sendResponse(const Name& name,
132 const ControlResponse& response)
133{
134 const Block& encodedControl = response.wireEncode();
135
136 Data responseData(name);
137 responseData.setContent(encodedControl);
138
139 m_keyChain.sign(responseData);
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700140 m_face.put(responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700141}
142
143void
144RibManager::sendResponse(const Name& name,
145 uint32_t code,
146 const std::string& text)
147{
148 ControlResponse response(code, text);
149 sendResponse(name, response);
150}
151
152void
Yingdi Yue5224e92014-04-29 18:04:02 -0700153RibManager::onLocalhostRequest(const Interest& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700154{
Yingdi Yue5224e92014-04-29 18:04:02 -0700155 m_localhostValidator.validate(request,
156 bind(&RibManager::onCommandValidated, this, _1),
157 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
158}
159
160void
161RibManager::onLocalhopRequest(const Interest& request)
162{
163 m_localhopValidator.validate(request,
164 bind(&RibManager::onCommandValidated, this, _1),
165 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700166}
167
168void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700169RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700170{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700171 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700172 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700173
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700174 const Name& command = request->getName();
175 const Name::Component& verb = command[COMMAND_PREFIX.size()];
176 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
177
178 VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700179 if (verbProcessor != m_verbDispatch.end())
180 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700181 ControlParameters parameters;
182 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700183 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700184 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700185 sendResponse(command, 400, "Malformed command");
186 return;
187 }
188
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700189 if (!parameters.hasFaceId() || parameters.getFaceId() == 0)
190 {
191 parameters.setFaceId(request->getIncomingFaceId());
192 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700193
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700194 NFD_LOG_DEBUG("command result: processing verb: " << verb);
195 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700196 }
197 else
198 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700199 NFD_LOG_DEBUG("Unsupported command: " << verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700200 sendResponse(request->getName(), 501, "Unsupported command");
201 }
202}
203
204void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700205RibManager::registerEntry(const shared_ptr<const Interest>& request,
206 ControlParameters& parameters)
207{
208 ndn::nfd::RibRegisterCommand command;
209
210 if (!validateParameters(command, parameters))
211 {
212 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
213 sendResponse(request->getName(), 400, "Malformed command");
214 return;
215 }
216
217 RibEntry ribEntry;
218 ribEntry.name = parameters.getName();
219 ribEntry.faceId = parameters.getFaceId();
220 ribEntry.origin = parameters.getOrigin();
221 ribEntry.cost = parameters.getCost();
222 ribEntry.flags = parameters.getFlags();
223 ribEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
224
225 NFD_LOG_TRACE("register prefix: " << ribEntry);
226
227 // For right now, just pass the options to fib as it is,
228 // without processing flags. Later options will be first added to
229 // Rib tree, then nrd will generate fib updates based on flags and then
230 // will add next hops one by one..
231 m_managedRib.insert(ribEntry);
232 m_nfdController->start<ndn::nfd::FibAddNextHopCommand>(
233 ControlParameters()
234 .setName(ribEntry.name)
235 .setFaceId(ribEntry.faceId)
236 .setCost(ribEntry.cost),
237 bind(&RibManager::onRegSuccess, this, request, parameters, ribEntry),
238 bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry));
239}
240
241void
242RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
243 ControlParameters& parameters)
244{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700245 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700246
247 if (!validateParameters(command, parameters))
248 {
249 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
250 sendResponse(request->getName(), 400, "Malformed command");
251 return;
252 }
253
254 RibEntry ribEntry;
255 ribEntry.name = parameters.getName();
256 ribEntry.faceId = parameters.getFaceId();
257 ribEntry.origin = parameters.getOrigin();
258
259 NFD_LOG_TRACE("unregister prefix: " << ribEntry);
260
261 m_nfdController->start<ndn::nfd::FibRemoveNextHopCommand>(
262 ControlParameters()
263 .setName(ribEntry.name)
264 .setFaceId(ribEntry.faceId),
265 bind(&RibManager::onUnRegSuccess, this, request, parameters, ribEntry),
266 bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry));
267}
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,
317 const RibEntry& ribEntry)
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);
337 m_managedRib.erase(ribEntry);
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,
343 const RibEntry& ribEntry)
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
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700351 NFD_LOG_TRACE("onRegSuccess: registered " << ribEntry);
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,
360 const RibEntry& ribEntry)
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
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700368 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700369
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700370 sendResponse(request->getName(), response);
371 m_managedRib.erase(ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700372}
373
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700374void
375RibManager::onControlHeaderSuccess()
376{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700377 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700378}
379
380void
381RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
382{
Alexander Afanasyevb3051652014-04-30 17:50:26 -0700383 std::ostringstream os;
384 os << "Couldn't enable local control header "
385 << "(code: " << code << ", info: " << reason << ")";
386 throw Error(os.str());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700387}
388
389void
390RibManager::enableLocalControlHeader()
391{
392 m_nfdController->start<ndn::nfd::FaceEnableLocalControlCommand>(
393 ControlParameters()
394 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
395 bind(&RibManager::onControlHeaderSuccess, this),
396 bind(&RibManager::onControlHeaderError, this, _1, _2));
397}
398
399void
400RibManager::onNotification(const FaceEventNotification& notification)
401{
402 /// \todo A notification can be missed, in this case check Facelist
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700403 NFD_LOG_TRACE("onNotification: " << notification);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700404 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { //face destroyed
405 m_managedRib.erase(notification.getFaceId());
406 }
407}
408
409} // namespace rib
410} // namespace nfd