blob: 14675b890e948add40f93fc86bd09baef0d40cc1 [file] [log] [blame]
Yanbiao Li711c7932015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Lidf846e52016-01-30 21:53:47 -08003 * Copyright (c) 2014-2016, 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"
Junxiao Shicbc8e942016-09-06 03:17:45 +000028#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000029#include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
Yanbiao Li711c7932015-08-19 16:30:16 -070030
31namespace nfd {
32
33NFD_LOG_INIT("FibManager");
34
35FibManager::FibManager(Fib& fib,
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000036 const FaceTable& faceTable,
Yanbiao Li711c7932015-08-19 16:30:16 -070037 Dispatcher& dispatcher,
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000038 CommandAuthenticator& authenticator)
39 : NfdManagerBase(dispatcher, authenticator, "fib")
Yanbiao Li711c7932015-08-19 16:30:16 -070040 , m_fib(fib)
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000041 , m_faceTable(faceTable)
Yanbiao Li711c7932015-08-19 16:30:16 -070042{
43 registerCommandHandler<ndn::nfd::FibAddNextHopCommand>("add-nexthop",
44 bind(&FibManager::addNextHop, this, _2, _3, _4, _5));
45 registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop",
46 bind(&FibManager::removeNextHop, this, _2, _3, _4, _5));
47
48 registerStatusDatasetHandler("list", bind(&FibManager::listEntries, this, _1, _2, _3));
49}
50
51void
52FibManager::addNextHop(const Name& topPrefix, const Interest& interest,
53 ControlParameters parameters,
54 const ndn::mgmt::CommandContinuation& done)
55{
56 setFaceForSelfRegistration(interest, parameters);
57
58 const Name& prefix = parameters.getName();
59 FaceId faceId = parameters.getFaceId();
60 uint64_t cost = parameters.getCost();
61
62 NFD_LOG_TRACE("add-nexthop prefix: " << prefix
63 << " faceid: " << faceId
64 << " cost: " << cost);
65
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000066 Face* face = m_faceTable.get(faceId);
Junxiao Shia6de4292016-07-12 02:08:10 +000067 if (face != nullptr) {
68 fib::Entry* entry = m_fib.insert(prefix).first;
69 entry->addNextHop(*face, cost);
Yanbiao Li711c7932015-08-19 16:30:16 -070070
71 NFD_LOG_DEBUG("add-nexthop result: OK"
72 << " prefix:" << prefix
73 << " faceid: " << faceId
74 << " cost: " << cost);
75
76 return done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
77 }
78 else {
79 NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-faceid: " << faceId);
80 return done(ControlResponse(410, "Face not found"));
81 }
82}
83
84void
85FibManager::removeNextHop(const Name& topPrefix, const Interest& interest,
86 ControlParameters parameters,
87 const ndn::mgmt::CommandContinuation& done)
88{
89 setFaceForSelfRegistration(interest, parameters);
90
91 NFD_LOG_TRACE("remove-nexthop prefix: " << parameters.getName()
92 << " faceid: " << parameters.getFaceId());
93
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000094 Face* face = m_faceTable.get(parameters.getFaceId());
Junxiao Shia6de4292016-07-12 02:08:10 +000095 if (face != nullptr) {
96 fib::Entry* entry = m_fib.findExactMatch(parameters.getName());
97 if (entry != nullptr) {
98 entry->removeNextHop(*face);
Yanbiao Li711c7932015-08-19 16:30:16 -070099 NFD_LOG_DEBUG("remove-nexthop result: OK prefix: " << parameters.getName()
100 << " faceid: " << parameters.getFaceId());
101
102 if (!entry->hasNextHops()) {
103 m_fib.erase(*entry);
104 }
105 }
106 else {
107 NFD_LOG_DEBUG("remove-nexthop result: OK");
108 }
109 }
110 else {
111 NFD_LOG_DEBUG("remove-nexthop result: OK");
112 }
113
114 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
115}
116
117void
118FibManager::listEntries(const Name& topPrefix, const Interest& interest,
119 ndn::mgmt::StatusDatasetContext& context)
120{
121 for (auto&& entry : m_fib) {
122 auto prefix = entry.getPrefix();
123 ndn::nfd::FibEntry record;
124 const auto& nextHops = entry.getNextHops();
125
126 for (auto&& next : nextHops) {
127 ndn::nfd::NextHopRecord nextHopRecord;
Junxiao Shia6de4292016-07-12 02:08:10 +0000128 nextHopRecord.setFaceId(next.getFace().getId());
Yanbiao Li711c7932015-08-19 16:30:16 -0700129 nextHopRecord.setCost(next.getCost());
130
131 record.addNextHopRecord(nextHopRecord);
132 }
133
134 record.setPrefix(prefix);
135 context.append(record.wireEncode());
136 }
137
138 context.end();
139}
140
141void
142FibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
143{
144 bool isSelfRegistration = (parameters.getFaceId() == 0);
145 if (isSelfRegistration) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000146 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
147 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
148 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
149 // and is initialized synchronously with IncomingFaceId field enabled.
150 BOOST_ASSERT(incomingFaceIdTag != nullptr);
151 parameters.setFaceId(*incomingFaceIdTag);
Yanbiao Li711c7932015-08-19 16:30:16 -0700152 }
153}
154
Yanbiao Lidf846e52016-01-30 21:53:47 -0800155} // namespace nfd