blob: 06e660ac1f4b5e97f758cf56f4981f70058ea075 [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"
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);
Yanbiao Li711c7932015-08-19 16:30:16 -070060 const Name& prefix = parameters.getName();
61 FaceId faceId = parameters.getFaceId();
62 uint64_t cost = parameters.getCost();
63
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000064 if (prefix.size() > Fib::getMaxDepth()) {
65 NFD_LOG_DEBUG("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost <<
66 "): FAIL prefix-too-long");
67 return done(ControlResponse(414, "FIB entry prefix cannot exceed " +
68 ndn::to_string(Fib::getMaxDepth()) + " components"));
69 }
Yanbiao Li711c7932015-08-19 16:30:16 -070070
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000071 Face* face = m_faceTable.get(faceId);
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000072 if (face == nullptr) {
73 NFD_LOG_DEBUG("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost <<
74 "): FAIL unknown-faceid");
Yanbiao Li711c7932015-08-19 16:30:16 -070075 return done(ControlResponse(410, "Face not found"));
76 }
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000077
78 fib::Entry* entry = m_fib.insert(prefix).first;
79 entry->addNextHop(*face, cost);
80
81 NFD_LOG_TRACE("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost << "): OK");
82 return done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Yanbiao Li711c7932015-08-19 16:30:16 -070083}
84
85void
86FibManager::removeNextHop(const Name& topPrefix, const Interest& interest,
87 ControlParameters parameters,
88 const ndn::mgmt::CommandContinuation& done)
89{
90 setFaceForSelfRegistration(interest, parameters);
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000091 const Name& prefix = parameters.getName();
92 FaceId faceId = parameters.getFaceId();
Yanbiao Li711c7932015-08-19 16:30:16 -070093
94 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Junxiao Shi02a0e0f2018-02-06 02:03:45 +000095
96 Face* face = m_faceTable.get(faceId);
97 if (face == nullptr) {
98 NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-face");
99 return;
100 }
101
102 fib::Entry* entry = m_fib.findExactMatch(parameters.getName());
103 if (entry == nullptr) {
104 NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK no-entry");
105 return;
106 }
107
108 entry->removeNextHop(*face);
109 if (!entry->hasNextHops()) {
110 m_fib.erase(*entry);
111 NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK entry-erased");
112 }
113 else {
114 NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK nexthop-removed");
115 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700116}
117
118void
119FibManager::listEntries(const Name& topPrefix, const Interest& interest,
120 ndn::mgmt::StatusDatasetContext& context)
121{
Davide Pesavento1586aff2017-02-19 23:17:51 -0500122 for (const auto& entry : m_fib) {
123 const auto& nexthops = entry.getNextHops() |
124 boost::adaptors::transformed([] (const fib::NextHop& nh) {
125 return ndn::nfd::NextHopRecord()
126 .setFaceId(nh.getFace().getId())
127 .setCost(nh.getCost());
128 });
129 context.append(ndn::nfd::FibEntry()
130 .setPrefix(entry.getPrefix())
131 .setNextHopRecords(std::begin(nexthops), std::end(nexthops))
132 .wireEncode());
Yanbiao Li711c7932015-08-19 16:30:16 -0700133 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700134 context.end();
135}
136
137void
138FibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
139{
140 bool isSelfRegistration = (parameters.getFaceId() == 0);
141 if (isSelfRegistration) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000142 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
143 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
144 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
145 // and is initialized synchronously with IncomingFaceId field enabled.
146 BOOST_ASSERT(incomingFaceIdTag != nullptr);
147 parameters.setFaceId(*incomingFaceIdTag);
Yanbiao Li711c7932015-08-19 16:30:16 -0700148 }
149}
150
Yanbiao Lidf846e52016-01-30 21:53:47 -0800151} // namespace nfd