Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 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 | |
| 29 | namespace nfd { |
| 30 | |
| 31 | NFD_LOG_INIT("FibManager"); |
| 32 | |
| 33 | FibManager::FibManager(Fib& fib, |
| 34 | function<shared_ptr<Face>(FaceId)> getFace, |
| 35 | Dispatcher& dispatcher, |
| 36 | CommandValidator& validator) |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 37 | : NfdManagerBase(dispatcher, validator, "fib") |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 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 | |
| 49 | void |
| 50 | FibManager::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 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 64 | shared_ptr<Face> face = m_getFace(faceId); |
| 65 | if (face != nullptr) { |
| 66 | fib::Entry* entry = m_fib.insert(prefix).first; |
| 67 | entry->addNextHop(*face, cost); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 68 | |
| 69 | NFD_LOG_DEBUG("add-nexthop result: OK" |
| 70 | << " prefix:" << prefix |
| 71 | << " faceid: " << faceId |
| 72 | << " cost: " << cost); |
| 73 | |
| 74 | return done(ControlResponse(200, "Success").setBody(parameters.wireEncode())); |
| 75 | } |
| 76 | else { |
| 77 | NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-faceid: " << faceId); |
| 78 | return done(ControlResponse(410, "Face not found")); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | FibManager::removeNextHop(const Name& topPrefix, const Interest& interest, |
| 84 | ControlParameters parameters, |
| 85 | const ndn::mgmt::CommandContinuation& done) |
| 86 | { |
| 87 | setFaceForSelfRegistration(interest, parameters); |
| 88 | |
| 89 | NFD_LOG_TRACE("remove-nexthop prefix: " << parameters.getName() |
| 90 | << " faceid: " << parameters.getFaceId()); |
| 91 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 92 | shared_ptr<Face> face = m_getFace(parameters.getFaceId()); |
| 93 | if (face != nullptr) { |
| 94 | fib::Entry* entry = m_fib.findExactMatch(parameters.getName()); |
| 95 | if (entry != nullptr) { |
| 96 | entry->removeNextHop(*face); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 97 | NFD_LOG_DEBUG("remove-nexthop result: OK prefix: " << parameters.getName() |
| 98 | << " faceid: " << parameters.getFaceId()); |
| 99 | |
| 100 | if (!entry->hasNextHops()) { |
| 101 | m_fib.erase(*entry); |
| 102 | } |
| 103 | } |
| 104 | else { |
| 105 | NFD_LOG_DEBUG("remove-nexthop result: OK"); |
| 106 | } |
| 107 | } |
| 108 | else { |
| 109 | NFD_LOG_DEBUG("remove-nexthop result: OK"); |
| 110 | } |
| 111 | |
| 112 | done(ControlResponse(200, "Success").setBody(parameters.wireEncode())); |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | FibManager::listEntries(const Name& topPrefix, const Interest& interest, |
| 117 | ndn::mgmt::StatusDatasetContext& context) |
| 118 | { |
| 119 | for (auto&& entry : m_fib) { |
| 120 | auto prefix = entry.getPrefix(); |
| 121 | ndn::nfd::FibEntry record; |
| 122 | const auto& nextHops = entry.getNextHops(); |
| 123 | |
| 124 | for (auto&& next : nextHops) { |
| 125 | ndn::nfd::NextHopRecord nextHopRecord; |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 126 | nextHopRecord.setFaceId(next.getFace().getId()); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 127 | nextHopRecord.setCost(next.getCost()); |
| 128 | |
| 129 | record.addNextHopRecord(nextHopRecord); |
| 130 | } |
| 131 | |
| 132 | record.setPrefix(prefix); |
| 133 | context.append(record.wireEncode()); |
| 134 | } |
| 135 | |
| 136 | context.end(); |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | FibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters) |
| 141 | { |
| 142 | bool isSelfRegistration = (parameters.getFaceId() == 0); |
| 143 | if (isSelfRegistration) { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 144 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>(); |
| 145 | // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field", |
| 146 | // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD |
| 147 | // and is initialized synchronously with IncomingFaceId field enabled. |
| 148 | BOOST_ASSERT(incomingFaceIdTag != nullptr); |
| 149 | parameters.setFaceId(*incomingFaceIdTag); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 153 | } // namespace nfd |