blob: 0bf4f34feba1de54d946783e48da331f2acca080 [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "fib-manager.hpp"
8
9#include "table/fib.hpp"
10#include "fw/forwarder.hpp"
11#include "mgmt/internal-face.hpp"
12#include "mgmt/app-face.hpp"
13
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070014#include <ndn-cpp-dev/encoding/tlv.hpp>
15
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070016namespace nfd {
17
18NFD_LOG_INIT("FibManager");
19
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070020const Name FibManager::COMMAND_PREFIX = "/localhost/nfd/fib";
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070021
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070022const size_t FibManager::COMMAND_UNSIGNED_NCOMPS =
23 FibManager::COMMAND_PREFIX.size() +
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070024 1 + // verb
Steve DiBenedetto7564d972014-03-24 14:28:46 -060025 1; // verb parameters
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070026
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070027const size_t FibManager::COMMAND_SIGNED_NCOMPS =
28 FibManager::COMMAND_UNSIGNED_NCOMPS +
29 4; // (timestamp, nonce, signed info tlv, signature tlv)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070030
Steve DiBenedetto6214e562014-03-15 16:27:04 -060031const FibManager::SignedVerbAndProcessor FibManager::SIGNED_COMMAND_VERBS[] =
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070032 {
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070033
Steve DiBenedetto6214e562014-03-15 16:27:04 -060034 SignedVerbAndProcessor(
35 Name::Component("add-nexthop"),
36 &FibManager::addNextHop
37 ),
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070038
Steve DiBenedetto6214e562014-03-15 16:27:04 -060039 SignedVerbAndProcessor(
40 Name::Component("remove-nexthop"),
41 &FibManager::removeNextHop
42 ),
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070043
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070044 };
45
Steve DiBenedetto6214e562014-03-15 16:27:04 -060046const FibManager::UnsignedVerbAndProcessor FibManager::UNSIGNED_COMMAND_VERBS[] =
47 {
48 UnsignedVerbAndProcessor(
49 Name::Component("list"),
50 &FibManager::listEntries
51 ),
52 };
53
54const Name FibManager::LIST_COMMAND_PREFIX("/localhost/nfd/fib/list");
55const size_t FibManager::LIST_COMMAND_NCOMPS = LIST_COMMAND_PREFIX.size();
56
57
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070058FibManager::FibManager(Fib& fib,
Steve DiBenedetto3970c892014-01-31 23:31:13 -070059 function<shared_ptr<Face>(FaceId)> getFace,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070060 shared_ptr<InternalFace> face)
Steve DiBenedetto6214e562014-03-15 16:27:04 -060061 : ManagerBase(face, FIB_PRIVILEGE)
62 , m_managedFib(fib)
63 , m_getFace(getFace)
64 , m_fibEnumerationPublisher(fib, face, LIST_COMMAND_PREFIX)
65 , m_signedVerbDispatch(SIGNED_COMMAND_VERBS,
66 SIGNED_COMMAND_VERBS +
67 (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor)))
68 , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS,
69 UNSIGNED_COMMAND_VERBS +
70 (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor)))
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070071{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070072 face->setInterestFilter("/localhost/nfd/fib",
73 bind(&FibManager::onFibRequest, this, _2));
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070074}
75
Steve DiBenedettod030cfc2014-03-10 20:04:47 -060076FibManager::~FibManager()
77{
78
79}
80
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070081void
82FibManager::onFibRequest(const Interest& request)
83{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070084 const Name& command = request.getName();
85 const size_t commandNComps = command.size();
Steve DiBenedetto6214e562014-03-15 16:27:04 -060086 const Name::Component& verb = command.get(COMMAND_PREFIX.size());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070087
Steve DiBenedetto6214e562014-03-15 16:27:04 -060088 UnsignedVerbDispatchTable::const_iterator unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
89 if (unsignedVerbProcessor != m_unsignedVerbDispatch.end())
90 {
91 NFD_LOG_INFO("command result: processing verb: " << verb);
92 (unsignedVerbProcessor->second)(this, boost::cref(request));
93 }
94 else if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
Steve DiBenedetto7564d972014-03-24 14:28:46 -060095 commandNComps < COMMAND_SIGNED_NCOMPS)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070096 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070097 NFD_LOG_INFO("command result: unsigned verb: " << command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -070098 sendResponse(command, 401, "Signature required");
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070099 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700100 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600101 !COMMAND_PREFIX.isPrefixOf(command))
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700102 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700103 NFD_LOG_INFO("command result: malformed");
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700104 sendResponse(command, 400, "Malformed command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700105 }
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600106 else
107 {
108 validate(request,
109 bind(&FibManager::onValidatedFibRequest, this, _1),
110 bind(&ManagerBase::onCommandValidationFailed, this, _1, _2));
111 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700112}
113
114void
115FibManager::onValidatedFibRequest(const shared_ptr<const Interest>& request)
116{
117 const Name& command = request->getName();
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600118 const Name::Component& verb = command[COMMAND_PREFIX.size()];
119 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700120
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600121 SignedVerbDispatchTable::const_iterator signedVerbProcessor = m_signedVerbDispatch.find (verb);
122 if (signedVerbProcessor != m_signedVerbDispatch.end())
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700123 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600124 ControlParameters parameters;
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600125 if (!extractParameters(parameterComponent, parameters) || !parameters.hasFaceId())
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700126 {
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700127 NFD_LOG_INFO("command result: malformed verb: " << verb);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700128 sendResponse(command, 400, "Malformed command");
129 return;
130 }
131
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600132 if (parameters.getFaceId() == 0)
133 {
134 parameters.setFaceId(request->getIncomingFaceId());
135 }
136
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700137 NFD_LOG_INFO("command result: processing verb: " << verb);
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800138 ControlResponse response;
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600139 (signedVerbProcessor->second)(this, parameters, response);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700140 sendResponse(command, response);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700141 }
142 else
143 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700144 NFD_LOG_INFO("command result: unsupported verb: " << verb);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700145 sendResponse(command, 501, "Unsupported command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700146 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700147}
148
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700149void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600150FibManager::addNextHop(ControlParameters& parameters,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800151 ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700152{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600153 ndn::nfd::FibAddNextHopCommand command;
154
155 if (!validateParameters(command, parameters))
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600156 {
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600157 NFD_LOG_INFO("add-nexthop result: FAIL reason: malformed");
158 setResponse(response, 400, "Malformed command");
159 return;
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600160 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700161
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600162 const Name& prefix = parameters.getName();
163 FaceId faceId = parameters.getFaceId();
164 uint64_t cost = parameters.getCost();
165
166 NFD_LOG_DEBUG("add-nexthop prefix: " << prefix
167 << " faceid: " << faceId
168 << " cost: " << cost);
169
170 shared_ptr<Face> nextHopFace = m_getFace(faceId);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700171 if (static_cast<bool>(nextHopFace))
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700172 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600173 shared_ptr<fib::Entry> entry = m_managedFib.insert(prefix).first;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700174
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600175 entry->addNextHop(nextHopFace, cost);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600176
177 NFD_LOG_INFO("add-nexthop result: OK"
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600178 << " prefix:" << prefix
179 << " faceid: " << faceId
180 << " cost: " << cost);
181
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600182 NFD_LOG_INFO("add-nexthop result: SUCCESS");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600183 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700184 }
185 else
186 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600187 NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-faceid: " << faceId);
188 setResponse(response, 410, "Face not found");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700189 }
190}
191
192void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600193FibManager::removeNextHop(ControlParameters& parameters,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700194 ControlResponse& response)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700195{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600196 ndn::nfd::FibRemoveNextHopCommand command;
197 if (!validateParameters(command, parameters))
198 {
199 NFD_LOG_INFO("remove-nexthop result: FAIL reason: malformed");
200 setResponse(response, 400, "Malformed command");
201 return;
202 }
203
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600204 NFD_LOG_DEBUG("remove-nexthop prefix: " << parameters.getName()
205 << " faceid: " << parameters.getFaceId());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700206
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600207 shared_ptr<Face> faceToRemove = m_getFace(parameters.getFaceId());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700208 if (static_cast<bool>(faceToRemove))
209 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600210 shared_ptr<fib::Entry> entry = m_managedFib.findExactMatch(parameters.getName());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700211 if (static_cast<bool>(entry))
212 {
213 entry->removeNextHop(faceToRemove);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600214 NFD_LOG_INFO("remove-nexthop result: OK prefix: " << parameters.getName()
215 << " faceid: " << parameters.getFaceId());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700216
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600217 if (!entry->hasNextHops())
218 {
219 m_managedFib.erase(*entry);
220 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700221 }
222 }
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600223
224 NFD_LOG_INFO("remove-nexthop result: SUCCESS");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600225 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700226}
227
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600228void
229FibManager::listEntries(const Interest& request)
230{
231 const Name& command = request.getName();
232 const size_t commandNComps = command.size();
233
234 if (commandNComps < LIST_COMMAND_NCOMPS ||
235 !LIST_COMMAND_PREFIX.isPrefixOf(command))
236 {
237 NFD_LOG_INFO("command result: malformed");
238 sendResponse(command, 400, "Malformed command");
239 return;
240 }
241
242 m_fibEnumerationPublisher.publish();
243}
244
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700245} // namespace nfd