blob: 7c27649a00441c106f0cf2ccf14f61aa2d96a185 [file] [log] [blame]
Yanbiao Li711c7932015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento1586aff2017-02-19 23:17:51 -05003 * Copyright (c) 2014-2017, Regents of the University of California,
Yanbiao Li711c7932015-08-19 16:30:16 -07004 * 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 "fib-manager.hpp"
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000027#include "fw/face-table.hpp"
Davide Pesavento1586aff2017-02-19 23:17:51 -050028
Junxiao Shicbc8e942016-09-06 03:17:45 +000029#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000030#include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
Yanbiao Li711c7932015-08-19 16:30:16 -070031
Davide Pesavento1586aff2017-02-19 23:17:51 -050032#include <boost/range/adaptor/transformed.hpp>
33
Yanbiao Li711c7932015-08-19 16:30:16 -070034namespace nfd {
35
36NFD_LOG_INIT("FibManager");
37
38FibManager::FibManager(Fib& fib,
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000039 const FaceTable& faceTable,
Yanbiao Li711c7932015-08-19 16:30:16 -070040 Dispatcher& dispatcher,
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000041 CommandAuthenticator& authenticator)
42 : NfdManagerBase(dispatcher, authenticator, "fib")
Yanbiao Li711c7932015-08-19 16:30:16 -070043 , m_fib(fib)
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000044 , m_faceTable(faceTable)
Yanbiao Li711c7932015-08-19 16:30:16 -070045{
46 registerCommandHandler<ndn::nfd::FibAddNextHopCommand>("add-nexthop",
47 bind(&FibManager::addNextHop, this, _2, _3, _4, _5));
48 registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop",
49 bind(&FibManager::removeNextHop, this, _2, _3, _4, _5));
50
51 registerStatusDatasetHandler("list", bind(&FibManager::listEntries, this, _1, _2, _3));
52}
53
54void
55FibManager::addNextHop(const Name& topPrefix, const Interest& interest,
56 ControlParameters parameters,
57 const ndn::mgmt::CommandContinuation& done)
58{
59 setFaceForSelfRegistration(interest, parameters);
60
61 const Name& prefix = parameters.getName();
62 FaceId faceId = parameters.getFaceId();
63 uint64_t cost = parameters.getCost();
64
65 NFD_LOG_TRACE("add-nexthop prefix: " << prefix
66 << " faceid: " << faceId
67 << " cost: " << cost);
68
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000069 Face* face = m_faceTable.get(faceId);
Junxiao Shia6de4292016-07-12 02:08:10 +000070 if (face != nullptr) {
71 fib::Entry* entry = m_fib.insert(prefix).first;
72 entry->addNextHop(*face, cost);
Yanbiao Li711c7932015-08-19 16:30:16 -070073
74 NFD_LOG_DEBUG("add-nexthop result: OK"
75 << " prefix:" << prefix
76 << " faceid: " << faceId
77 << " cost: " << cost);
78
79 return done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
80 }
81 else {
82 NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-faceid: " << faceId);
83 return done(ControlResponse(410, "Face not found"));
84 }
85}
86
87void
88FibManager::removeNextHop(const Name& topPrefix, const Interest& interest,
89 ControlParameters parameters,
90 const ndn::mgmt::CommandContinuation& done)
91{
92 setFaceForSelfRegistration(interest, parameters);
93
94 NFD_LOG_TRACE("remove-nexthop prefix: " << parameters.getName()
95 << " faceid: " << parameters.getFaceId());
96
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000097 Face* face = m_faceTable.get(parameters.getFaceId());
Junxiao Shia6de4292016-07-12 02:08:10 +000098 if (face != nullptr) {
99 fib::Entry* entry = m_fib.findExactMatch(parameters.getName());
100 if (entry != nullptr) {
101 entry->removeNextHop(*face);
Yanbiao Li711c7932015-08-19 16:30:16 -0700102 NFD_LOG_DEBUG("remove-nexthop result: OK prefix: " << parameters.getName()
103 << " faceid: " << parameters.getFaceId());
104
105 if (!entry->hasNextHops()) {
106 m_fib.erase(*entry);
107 }
108 }
109 else {
110 NFD_LOG_DEBUG("remove-nexthop result: OK");
111 }
112 }
113 else {
114 NFD_LOG_DEBUG("remove-nexthop result: OK");
115 }
116
117 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
118}
119
120void
121FibManager::listEntries(const Name& topPrefix, const Interest& interest,
122 ndn::mgmt::StatusDatasetContext& context)
123{
Davide Pesavento1586aff2017-02-19 23:17:51 -0500124 for (const auto& entry : m_fib) {
125 const auto& nexthops = entry.getNextHops() |
126 boost::adaptors::transformed([] (const fib::NextHop& nh) {
127 return ndn::nfd::NextHopRecord()
128 .setFaceId(nh.getFace().getId())
129 .setCost(nh.getCost());
130 });
131 context.append(ndn::nfd::FibEntry()
132 .setPrefix(entry.getPrefix())
133 .setNextHopRecords(std::begin(nexthops), std::end(nexthops))
134 .wireEncode());
Yanbiao Li711c7932015-08-19 16:30:16 -0700135 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700136 context.end();
137}
138
139void
140FibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
141{
142 bool isSelfRegistration = (parameters.getFaceId() == 0);
143 if (isSelfRegistration) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000144 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
145 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
146 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
147 // and is initialized synchronously with IncomingFaceId field enabled.
148 BOOST_ASSERT(incomingFaceIdTag != nullptr);
149 parameters.setFaceId(*incomingFaceIdTag);
Yanbiao Li711c7932015-08-19 16:30:16 -0700150 }
151}
152
Yanbiao Lidf846e52016-01-30 21:53:47 -0800153} // namespace nfd