blob: 5c049774a80847eb8028c78ff44b519a9736b64e [file] [log] [blame]
Yanbiao Li711c7932015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi02a0e0f2018-02-06 02:03:45 +00002/*
3 * Copyright (c) 2014-2018, 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"
Davide Pesaventoa3148082018-04-12 18:21:54 -040027
28#include "core/logger.hpp"
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000029#include "fw/face-table.hpp"
Davide Pesavento1586aff2017-02-19 23:17:51 -050030
Junxiao Shicbc8e942016-09-06 03:17:45 +000031#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000032#include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
Yanbiao Li711c7932015-08-19 16:30:16 -070033
Davide Pesavento1586aff2017-02-19 23:17:51 -050034#include <boost/range/adaptor/transformed.hpp>
35
Yanbiao Li711c7932015-08-19 16:30:16 -070036namespace nfd {
37
Davide Pesaventoa3148082018-04-12 18:21:54 -040038NFD_LOG_INIT(FibManager);
Yanbiao Li711c7932015-08-19 16:30:16 -070039
40FibManager::FibManager(Fib& fib,
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000041 const FaceTable& faceTable,
Yanbiao Li711c7932015-08-19 16:30:16 -070042 Dispatcher& dispatcher,
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000043 CommandAuthenticator& authenticator)
44 : NfdManagerBase(dispatcher, authenticator, "fib")
Yanbiao Li711c7932015-08-19 16:30:16 -070045 , m_fib(fib)
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000046 , m_faceTable(faceTable)
Yanbiao Li711c7932015-08-19 16:30:16 -070047{
48 registerCommandHandler<ndn::nfd::FibAddNextHopCommand>("add-nexthop",
49 bind(&FibManager::addNextHop, this, _2, _3, _4, _5));
50 registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop",
51 bind(&FibManager::removeNextHop, this, _2, _3, _4, _5));
52
53 registerStatusDatasetHandler("list", bind(&FibManager::listEntries, this, _1, _2, _3));
54}
55
56void
57FibManager::addNextHop(const Name& topPrefix, const Interest& interest,
58 ControlParameters parameters,
59 const ndn::mgmt::CommandContinuation& done)
60{
61 setFaceForSelfRegistration(interest, parameters);
Yanbiao Li711c7932015-08-19 16:30:16 -070062 const Name& prefix = parameters.getName();
63 FaceId faceId = parameters.getFaceId();
64 uint64_t cost = parameters.getCost();
65
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000066 if (prefix.size() > Fib::getMaxDepth()) {
67 NFD_LOG_DEBUG("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost <<
68 "): FAIL prefix-too-long");
69 return done(ControlResponse(414, "FIB entry prefix cannot exceed " +
70 ndn::to_string(Fib::getMaxDepth()) + " components"));
71 }
Yanbiao Li711c7932015-08-19 16:30:16 -070072
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000073 Face* face = m_faceTable.get(faceId);
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000074 if (face == nullptr) {
75 NFD_LOG_DEBUG("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost <<
76 "): FAIL unknown-faceid");
Yanbiao Li711c7932015-08-19 16:30:16 -070077 return done(ControlResponse(410, "Face not found"));
78 }
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000079
80 fib::Entry* entry = m_fib.insert(prefix).first;
81 entry->addNextHop(*face, cost);
82
83 NFD_LOG_TRACE("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost << "): OK");
84 return done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Yanbiao Li711c7932015-08-19 16:30:16 -070085}
86
87void
88FibManager::removeNextHop(const Name& topPrefix, const Interest& interest,
89 ControlParameters parameters,
90 const ndn::mgmt::CommandContinuation& done)
91{
92 setFaceForSelfRegistration(interest, parameters);
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000093 const Name& prefix = parameters.getName();
94 FaceId faceId = parameters.getFaceId();
Yanbiao Li711c7932015-08-19 16:30:16 -070095
96 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000097
98 Face* face = m_faceTable.get(faceId);
99 if (face == nullptr) {
100 NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-face");
101 return;
102 }
103
104 fib::Entry* entry = m_fib.findExactMatch(parameters.getName());
105 if (entry == nullptr) {
106 NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-entry");
107 return;
108 }
109
110 entry->removeNextHop(*face);
111 if (!entry->hasNextHops()) {
112 m_fib.erase(*entry);
113 NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK entry-erased");
114 }
115 else {
116 NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK nexthop-removed");
117 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700118}
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