blob: 1c15ebfb30f3249266ad8fd65f1f6770424a2a03 [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"
Yanbiao Li711c7932015-08-19 16:30:16 -070028#include <ndn-cxx/management/nfd-fib-entry.hpp>
29
30namespace nfd {
31
32NFD_LOG_INIT("FibManager");
33
34FibManager::FibManager(Fib& fib,
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000035 const FaceTable& faceTable,
Yanbiao Li711c7932015-08-19 16:30:16 -070036 Dispatcher& dispatcher,
37 CommandValidator& validator)
Yanbiao Lidf846e52016-01-30 21:53:47 -080038 : NfdManagerBase(dispatcher, validator, "fib")
Yanbiao Li711c7932015-08-19 16:30:16 -070039 , m_fib(fib)
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000040 , m_faceTable(faceTable)
Yanbiao Li711c7932015-08-19 16:30:16 -070041{
42 registerCommandHandler<ndn::nfd::FibAddNextHopCommand>("add-nexthop",
43 bind(&FibManager::addNextHop, this, _2, _3, _4, _5));
44 registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop",
45 bind(&FibManager::removeNextHop, this, _2, _3, _4, _5));
46
47 registerStatusDatasetHandler("list", bind(&FibManager::listEntries, this, _1, _2, _3));
48}
49
50void
51FibManager::addNextHop(const Name& topPrefix, const Interest& interest,
52 ControlParameters parameters,
53 const ndn::mgmt::CommandContinuation& done)
54{
55 setFaceForSelfRegistration(interest, parameters);
56
57 const Name& prefix = parameters.getName();
58 FaceId faceId = parameters.getFaceId();
59 uint64_t cost = parameters.getCost();
60
61 NFD_LOG_TRACE("add-nexthop prefix: " << prefix
62 << " faceid: " << faceId
63 << " cost: " << cost);
64
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000065 Face* face = m_faceTable.get(faceId);
Junxiao Shia6de4292016-07-12 02:08:10 +000066 if (face != nullptr) {
67 fib::Entry* entry = m_fib.insert(prefix).first;
68 entry->addNextHop(*face, cost);
Yanbiao Li711c7932015-08-19 16:30:16 -070069
70 NFD_LOG_DEBUG("add-nexthop result: OK"
71 << " prefix:" << prefix
72 << " faceid: " << faceId
73 << " cost: " << cost);
74
75 return done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
76 }
77 else {
78 NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-faceid: " << faceId);
79 return done(ControlResponse(410, "Face not found"));
80 }
81}
82
83void
84FibManager::removeNextHop(const Name& topPrefix, const Interest& interest,
85 ControlParameters parameters,
86 const ndn::mgmt::CommandContinuation& done)
87{
88 setFaceForSelfRegistration(interest, parameters);
89
90 NFD_LOG_TRACE("remove-nexthop prefix: " << parameters.getName()
91 << " faceid: " << parameters.getFaceId());
92
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000093 Face* face = m_faceTable.get(parameters.getFaceId());
Junxiao Shia6de4292016-07-12 02:08:10 +000094 if (face != nullptr) {
95 fib::Entry* entry = m_fib.findExactMatch(parameters.getName());
96 if (entry != nullptr) {
97 entry->removeNextHop(*face);
Yanbiao Li711c7932015-08-19 16:30:16 -070098 NFD_LOG_DEBUG("remove-nexthop result: OK prefix: " << parameters.getName()
99 << " faceid: " << parameters.getFaceId());
100
101 if (!entry->hasNextHops()) {
102 m_fib.erase(*entry);
103 }
104 }
105 else {
106 NFD_LOG_DEBUG("remove-nexthop result: OK");
107 }
108 }
109 else {
110 NFD_LOG_DEBUG("remove-nexthop result: OK");
111 }
112
113 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
114}
115
116void
117FibManager::listEntries(const Name& topPrefix, const Interest& interest,
118 ndn::mgmt::StatusDatasetContext& context)
119{
120 for (auto&& entry : m_fib) {
121 auto prefix = entry.getPrefix();
122 ndn::nfd::FibEntry record;
123 const auto& nextHops = entry.getNextHops();
124
125 for (auto&& next : nextHops) {
126 ndn::nfd::NextHopRecord nextHopRecord;
Junxiao Shia6de4292016-07-12 02:08:10 +0000127 nextHopRecord.setFaceId(next.getFace().getId());
Yanbiao Li711c7932015-08-19 16:30:16 -0700128 nextHopRecord.setCost(next.getCost());
129
130 record.addNextHopRecord(nextHopRecord);
131 }
132
133 record.setPrefix(prefix);
134 context.append(record.wireEncode());
135 }
136
137 context.end();
138}
139
140void
141FibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
142{
143 bool isSelfRegistration = (parameters.getFaceId() == 0);
144 if (isSelfRegistration) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000145 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
146 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
147 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
148 // and is initialized synchronously with IncomingFaceId field enabled.
149 BOOST_ASSERT(incomingFaceIdTag != nullptr);
150 parameters.setFaceId(*incomingFaceIdTag);
Yanbiao Li711c7932015-08-19 16:30:16 -0700151 }
152}
153
Yanbiao Lidf846e52016-01-30 21:53:47 -0800154} // namespace nfd