blob: 12374f16ce6968d95fb056b9e854514bfd2a89d9 [file] [log] [blame]
Yanbiao Li711c7932015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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"
27#include <ndn-cxx/management/nfd-fib-entry.hpp>
28
29namespace nfd {
30
31NFD_LOG_INIT("FibManager");
32
33FibManager::FibManager(Fib& fib,
34 function<shared_ptr<Face>(FaceId)> getFace,
35 Dispatcher& dispatcher,
36 CommandValidator& validator)
37 : ManagerBase(dispatcher, validator, "fib")
38 , m_fib(fib)
39 , m_getFace(getFace)
40{
41 registerCommandHandler<ndn::nfd::FibAddNextHopCommand>("add-nexthop",
42 bind(&FibManager::addNextHop, this, _2, _3, _4, _5));
43 registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop",
44 bind(&FibManager::removeNextHop, this, _2, _3, _4, _5));
45
46 registerStatusDatasetHandler("list", bind(&FibManager::listEntries, this, _1, _2, _3));
47}
48
49void
50FibManager::addNextHop(const Name& topPrefix, const Interest& interest,
51 ControlParameters parameters,
52 const ndn::mgmt::CommandContinuation& done)
53{
54 setFaceForSelfRegistration(interest, parameters);
55
56 const Name& prefix = parameters.getName();
57 FaceId faceId = parameters.getFaceId();
58 uint64_t cost = parameters.getCost();
59
60 NFD_LOG_TRACE("add-nexthop prefix: " << prefix
61 << " faceid: " << faceId
62 << " cost: " << cost);
63
64 auto face = m_getFace(faceId);
65 if (static_cast<bool>(face)) {
66 auto entry = m_fib.insert(prefix).first;
67
68 entry->addNextHop(face, cost);
69
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
93 auto face = m_getFace(parameters.getFaceId());
94 if (static_cast<bool>(face)) {
95 auto entry = m_fib.findExactMatch(parameters.getName());
96 if (static_cast<bool>(entry)) {
97 entry->removeNextHop(face);
98 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;
127 nextHopRecord.setFaceId(next.getFace()->getId());
128 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) {
145 parameters.setFaceId(request.getIncomingFaceId());
146 }
147}
148
149} // namespace