blob: 916ed93176f03feabbfcb45a66266b2c034505e5 [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))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070069 , m_validator(m_face)
Yingdi Yuf4db0b52014-04-17 13:17:39 -070070 , m_faceMonitor(m_face)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070071 , m_verbDispatch(COMMAND_VERBS,
72 COMMAND_VERBS + (sizeof(COMMAND_VERBS) / sizeof(VerbAndProcessor)))
73{
74}
75
76void
77RibManager::registerWithNfd()
78{
79 //check whether the components of localhop and localhost prefixes are same
80 BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size());
81
Alexander Afanasyev20d31442014-04-19 17:00:53 -070082 NFD_LOG_INFO("Setting interest filter on: " << COMMAND_PREFIX);
Yingdi Yuf4db0b52014-04-17 13:17:39 -070083 m_face.setController(m_nfdController);
Alexander Afanasyev20d31442014-04-19 17:00:53 -070084 m_face.setInterestFilter(COMMAND_PREFIX,
Yingdi Yuf4db0b52014-04-17 13:17:39 -070085 bind(&RibManager::onRibRequest, this, _2),
86 bind(&RibManager::setInterestFilterFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070087
Alexander Afanasyev20d31442014-04-19 17:00:53 -070088 NFD_LOG_INFO("Setting interest filter on: " << REMOTE_COMMAND_PREFIX);
89 m_face.setInterestFilter(REMOTE_COMMAND_PREFIX,
Yingdi Yuf4db0b52014-04-17 13:17:39 -070090 bind(&RibManager::onRibRequest, this, _2),
91 bind(&RibManager::setInterestFilterFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070092
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070093 NFD_LOG_INFO("Start monitoring face create/destroy events");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070094 m_faceMonitor.addSubscriber(boost::bind(&RibManager::onNotification, this, _1));
95 m_faceMonitor.startNotifications();
96}
97
98void
99RibManager::setConfigFile(ConfigFile& configFile)
100{
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600101 configFile.addSectionHandler("rib_security",
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700102 bind(&RibManager::onConfig, this, _1, _2, _3));
103}
104
105void
106RibManager::onConfig(const ConfigSection& configSection,
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700107 bool isDryRun,
108 const std::string& filename)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700109{
110 /// \todo remove check after validator-conf replaces settings on each load
111 if (!isDryRun)
112 m_validator.load(configSection, filename);
113}
114
115void
116RibManager::setInterestFilterFailed(const Name& name, const std::string& msg)
117{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700118 NFD_LOG_ERROR("Error in setting interest filter (" << name << "): " << msg);
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700119 m_face.shutdown();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700120}
121
122void
123RibManager::sendResponse(const Name& name,
124 const ControlResponse& response)
125{
126 const Block& encodedControl = response.wireEncode();
127
128 Data responseData(name);
129 responseData.setContent(encodedControl);
130
131 m_keyChain.sign(responseData);
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700132 m_face.put(responseData);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700133}
134
135void
136RibManager::sendResponse(const Name& name,
137 uint32_t code,
138 const std::string& text)
139{
140 ControlResponse response(code, text);
141 sendResponse(name, response);
142}
143
144void
145RibManager::onRibRequest(const Interest& request)
146{
147 m_validator.validate(request,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700148 bind(&RibManager::onCommandValidated, this, _1),
149 bind(&RibManager::onCommandValidationFailed, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700150}
151
152void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700153RibManager::onCommandValidated(const shared_ptr<const Interest>& request)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700154{
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700155 // REMOTE_COMMAND_PREFIX number of componenets are same as
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700156 // NRD_COMMAND_PREFIX's so no extra checks are required.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700157
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700158 const Name& command = request->getName();
159 const Name::Component& verb = command[COMMAND_PREFIX.size()];
160 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
161
162 VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700163 if (verbProcessor != m_verbDispatch.end())
164 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700165 ControlParameters parameters;
166 if (!extractParameters(parameterComponent, parameters))
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700167 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700168 NFD_LOG_DEBUG("command result: malformed verb: " << verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700169 sendResponse(command, 400, "Malformed command");
170 return;
171 }
172
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700173 if (!parameters.hasFaceId() || parameters.getFaceId() == 0)
174 {
175 parameters.setFaceId(request->getIncomingFaceId());
176 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700177
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700178 NFD_LOG_DEBUG("command result: processing verb: " << verb);
179 (verbProcessor->second)(this, request, parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700180 }
181 else
182 {
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700183 NFD_LOG_DEBUG("Unsupported command: " << verb);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700184 sendResponse(request->getName(), 501, "Unsupported command");
185 }
186}
187
188void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700189RibManager::registerEntry(const shared_ptr<const Interest>& request,
190 ControlParameters& parameters)
191{
192 ndn::nfd::RibRegisterCommand command;
193
194 if (!validateParameters(command, parameters))
195 {
196 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
197 sendResponse(request->getName(), 400, "Malformed command");
198 return;
199 }
200
201 RibEntry ribEntry;
202 ribEntry.name = parameters.getName();
203 ribEntry.faceId = parameters.getFaceId();
204 ribEntry.origin = parameters.getOrigin();
205 ribEntry.cost = parameters.getCost();
206 ribEntry.flags = parameters.getFlags();
207 ribEntry.expires = time::steady_clock::now() + parameters.getExpirationPeriod();
208
209 NFD_LOG_TRACE("register prefix: " << ribEntry);
210
211 // For right now, just pass the options to fib as it is,
212 // without processing flags. Later options will be first added to
213 // Rib tree, then nrd will generate fib updates based on flags and then
214 // will add next hops one by one..
215 m_managedRib.insert(ribEntry);
216 m_nfdController->start<ndn::nfd::FibAddNextHopCommand>(
217 ControlParameters()
218 .setName(ribEntry.name)
219 .setFaceId(ribEntry.faceId)
220 .setCost(ribEntry.cost),
221 bind(&RibManager::onRegSuccess, this, request, parameters, ribEntry),
222 bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry));
223}
224
225void
226RibManager::unregisterEntry(const shared_ptr<const Interest>& request,
227 ControlParameters& parameters)
228{
Alexander Afanasyevce7520e2014-04-28 09:40:06 -0700229 ndn::nfd::RibUnregisterCommand command;
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700230
231 if (!validateParameters(command, parameters))
232 {
233 NFD_LOG_DEBUG("register result: FAIL reason: malformed");
234 sendResponse(request->getName(), 400, "Malformed command");
235 return;
236 }
237
238 RibEntry ribEntry;
239 ribEntry.name = parameters.getName();
240 ribEntry.faceId = parameters.getFaceId();
241 ribEntry.origin = parameters.getOrigin();
242
243 NFD_LOG_TRACE("unregister prefix: " << ribEntry);
244
245 m_nfdController->start<ndn::nfd::FibRemoveNextHopCommand>(
246 ControlParameters()
247 .setName(ribEntry.name)
248 .setFaceId(ribEntry.faceId),
249 bind(&RibManager::onUnRegSuccess, this, request, parameters, ribEntry),
250 bind(&RibManager::onCommandError, this, _1, _2, request, ribEntry));
251}
252
253void
254RibManager::onCommandValidationFailed(const shared_ptr<const Interest>& request,
255 const std::string& failureInfo)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700256{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700257 NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700258 sendResponse(request->getName(), 403, failureInfo);
259}
260
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700261
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700262bool
263RibManager::extractParameters(const Name::Component& parameterComponent,
264 ControlParameters& extractedParameters)
265{
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700266 try
267 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700268 Block rawParameters = parameterComponent.blockFromValue();
269 extractedParameters.wireDecode(rawParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700270 }
271 catch (const ndn::Tlv::Error& e)
272 {
273 return false;
274 }
275
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700276 NFD_LOG_DEBUG("Parameters parsed OK");
277 return true;
278}
279
280bool
281RibManager::validateParameters(const ControlCommand& command,
282 ControlParameters& parameters)
283{
284 try
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700285 {
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700286 command.validateRequest(parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700287 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700288 catch (const ControlCommand::ArgumentError&)
289 {
290 return false;
291 }
292
293 command.applyDefaultsToRequest(parameters);
294
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700295 return true;
296}
297
298void
299RibManager::onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700300 const shared_ptr<const Interest>& request,
301 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700302{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700303 NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700304
305 ControlResponse response;
306
307 if (code == 404)
308 {
309 response.setCode(code);
310 response.setText(error);
311 }
312 else
313 {
314 response.setCode(533);
315 std::ostringstream os;
316 os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
317 response.setText(os.str());
318 }
319
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700320 sendResponse(request->getName(), response);
321 m_managedRib.erase(ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700322}
323
324void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700325RibManager::onRegSuccess(const shared_ptr<const Interest>& request,
326 const ControlParameters& parameters,
327 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700328{
329 ControlResponse response;
330
331 response.setCode(200);
332 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700333 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700334
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700335 NFD_LOG_TRACE("onRegSuccess: registered " << ribEntry);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700336
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700337 sendResponse(request->getName(), response);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700338}
339
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700340
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700341void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700342RibManager::onUnRegSuccess(const shared_ptr<const Interest>& request,
343 const ControlParameters& parameters,
344 const RibEntry& ribEntry)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700345{
346 ControlResponse response;
347
348 response.setCode(200);
349 response.setText("Success");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700350 response.setBody(parameters.wireEncode());
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700351
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700352 NFD_LOG_TRACE("onUnRegSuccess: unregistered " << ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700353
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700354 sendResponse(request->getName(), response);
355 m_managedRib.erase(ribEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700356}
357
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700358void
359RibManager::onControlHeaderSuccess()
360{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700361 NFD_LOG_DEBUG("Local control header enabled");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700362}
363
364void
365RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
366{
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700367 NFD_LOG_ERROR("Error: couldn't enable local control header "
368 << "(code: " << code << ", info: " << reason << ")");
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700369 m_face.shutdown();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700370}
371
372void
373RibManager::enableLocalControlHeader()
374{
375 m_nfdController->start<ndn::nfd::FaceEnableLocalControlCommand>(
376 ControlParameters()
377 .setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
378 bind(&RibManager::onControlHeaderSuccess, this),
379 bind(&RibManager::onControlHeaderError, this, _1, _2));
380}
381
382void
383RibManager::onNotification(const FaceEventNotification& notification)
384{
385 /// \todo A notification can be missed, in this case check Facelist
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -0700386 NFD_LOG_TRACE("onNotification: " << notification);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700387 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { //face destroyed
388 m_managedRib.erase(notification.getFaceId());
389 }
390}
391
392} // namespace rib
393} // namespace nfd