blob: 1f0d173f9343470cf1b253bd9ca179d3911af70c [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/*
ashiqopu3ad49db2018-10-20 22:38:47 +00003 * Copyright (c) 2014-2019, 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
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/logger.hpp"
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000029#include "fw/face-table.hpp"
Davide Pesavento78ddcab2019-02-28 22:00:03 -050030#include "table/fib.hpp"
Davide Pesavento1586aff2017-02-19 23:17:51 -050031
Junxiao Shicbc8e942016-09-06 03:17:45 +000032#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000033#include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
Yanbiao Li711c7932015-08-19 16:30:16 -070034
Davide Pesavento1586aff2017-02-19 23:17:51 -050035#include <boost/range/adaptor/transformed.hpp>
36
Yanbiao Li711c7932015-08-19 16:30:16 -070037namespace nfd {
38
Davide Pesaventoa3148082018-04-12 18:21:54 -040039NFD_LOG_INIT(FibManager);
Yanbiao Li711c7932015-08-19 16:30:16 -070040
Davide Pesavento78ddcab2019-02-28 22:00:03 -050041FibManager::FibManager(Fib& fib, const FaceTable& faceTable,
42 Dispatcher& dispatcher, CommandAuthenticator& authenticator)
43 : ManagerBase("fib", dispatcher, authenticator)
Yanbiao Li711c7932015-08-19 16:30:16 -070044 , m_fib(fib)
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000045 , m_faceTable(faceTable)
Yanbiao Li711c7932015-08-19 16:30:16 -070046{
47 registerCommandHandler<ndn::nfd::FibAddNextHopCommand>("add-nexthop",
48 bind(&FibManager::addNextHop, this, _2, _3, _4, _5));
49 registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop",
50 bind(&FibManager::removeNextHop, this, _2, _3, _4, _5));
51
52 registerStatusDatasetHandler("list", bind(&FibManager::listEntries, this, _1, _2, _3));
53}
54
55void
56FibManager::addNextHop(const Name& topPrefix, const Interest& interest,
57 ControlParameters parameters,
58 const ndn::mgmt::CommandContinuation& done)
59{
60 setFaceForSelfRegistration(interest, parameters);
Yanbiao Li711c7932015-08-19 16:30:16 -070061 const Name& prefix = parameters.getName();
62 FaceId faceId = parameters.getFaceId();
63 uint64_t cost = parameters.getCost();
64
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000065 if (prefix.size() > Fib::getMaxDepth()) {
66 NFD_LOG_DEBUG("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost <<
67 "): FAIL prefix-too-long");
68 return done(ControlResponse(414, "FIB entry prefix cannot exceed " +
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040069 to_string(Fib::getMaxDepth()) + " components"));
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000070 }
Yanbiao Li711c7932015-08-19 16:30:16 -070071
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000072 Face* face = m_faceTable.get(faceId);
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000073 if (face == nullptr) {
74 NFD_LOG_DEBUG("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost <<
75 "): FAIL unknown-faceid");
Yanbiao Li711c7932015-08-19 16:30:16 -070076 return done(ControlResponse(410, "Face not found"));
77 }
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000078
79 fib::Entry* entry = m_fib.insert(prefix).first;
ashiqopu3ad49db2018-10-20 22:38:47 +000080 entry->addOrUpdateNextHop(*face, 0, cost);
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000081
82 NFD_LOG_TRACE("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost << "): OK");
83 return done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Yanbiao Li711c7932015-08-19 16:30:16 -070084}
85
86void
87FibManager::removeNextHop(const Name& topPrefix, const Interest& interest,
88 ControlParameters parameters,
89 const ndn::mgmt::CommandContinuation& done)
90{
91 setFaceForSelfRegistration(interest, parameters);
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000092 const Name& prefix = parameters.getName();
93 FaceId faceId = parameters.getFaceId();
Yanbiao Li711c7932015-08-19 16:30:16 -070094
95 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000096
97 Face* face = m_faceTable.get(faceId);
98 if (face == nullptr) {
99 NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-face");
100 return;
101 }
102
103 fib::Entry* entry = m_fib.findExactMatch(parameters.getName());
104 if (entry == nullptr) {
105 NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-entry");
106 return;
107 }
108
ashiqopu3ad49db2018-10-20 22:38:47 +0000109 entry->removeNextHop(*face, 0);
Junxiao Shi02a0e0f2018-02-06 02:03:45 +0000110 if (!entry->hasNextHops()) {
111 m_fib.erase(*entry);
112 NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK entry-erased");
113 }
114 else {
115 NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK nexthop-removed");
116 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700117}
118
119void
120FibManager::listEntries(const Name& topPrefix, const Interest& interest,
121 ndn::mgmt::StatusDatasetContext& context)
122{
Davide Pesavento1586aff2017-02-19 23:17:51 -0500123 for (const auto& entry : m_fib) {
124 const auto& nexthops = entry.getNextHops() |
125 boost::adaptors::transformed([] (const fib::NextHop& nh) {
126 return ndn::nfd::NextHopRecord()
127 .setFaceId(nh.getFace().getId())
128 .setCost(nh.getCost());
129 });
130 context.append(ndn::nfd::FibEntry()
131 .setPrefix(entry.getPrefix())
132 .setNextHopRecords(std::begin(nexthops), std::end(nexthops))
133 .wireEncode());
Yanbiao Li711c7932015-08-19 16:30:16 -0700134 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700135 context.end();
136}
137
138void
139FibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
140{
141 bool isSelfRegistration = (parameters.getFaceId() == 0);
142 if (isSelfRegistration) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000143 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
144 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
145 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
146 // and is initialized synchronously with IncomingFaceId field enabled.
147 BOOST_ASSERT(incomingFaceIdTag != nullptr);
148 parameters.setFaceId(*incomingFaceIdTag);
Yanbiao Li711c7932015-08-19 16:30:16 -0700149 }
150}
151
Yanbiao Lidf846e52016-01-30 21:53:47 -0800152} // namespace nfd