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" |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 27 | #include "fw/face-table.hpp" |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 28 | #include <ndn-cxx/lp/tags.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame^] | 29 | #include <ndn-cxx/mgmt/nfd/fib-entry.hpp> |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | |
| 33 | NFD_LOG_INIT("FibManager"); |
| 34 | |
| 35 | FibManager::FibManager(Fib& fib, |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 36 | const FaceTable& faceTable, |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 37 | Dispatcher& dispatcher, |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 38 | CommandAuthenticator& authenticator) |
| 39 | : NfdManagerBase(dispatcher, authenticator, "fib") |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 40 | , m_fib(fib) |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 41 | , m_faceTable(faceTable) |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 42 | { |
| 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 | |
| 51 | void |
| 52 | FibManager::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 Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 66 | Face* face = m_faceTable.get(faceId); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 67 | if (face != nullptr) { |
| 68 | fib::Entry* entry = m_fib.insert(prefix).first; |
| 69 | entry->addNextHop(*face, cost); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 70 | |
| 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 | |
| 84 | void |
| 85 | FibManager::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 Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 94 | Face* face = m_faceTable.get(parameters.getFaceId()); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 95 | if (face != nullptr) { |
| 96 | fib::Entry* entry = m_fib.findExactMatch(parameters.getName()); |
| 97 | if (entry != nullptr) { |
| 98 | entry->removeNextHop(*face); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 99 | 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 | |
| 117 | void |
| 118 | FibManager::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 Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 128 | nextHopRecord.setFaceId(next.getFace().getId()); |
Yanbiao Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 129 | 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 | |
| 141 | void |
| 142 | FibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters) |
| 143 | { |
| 144 | bool isSelfRegistration = (parameters.getFaceId() == 0); |
| 145 | if (isSelfRegistration) { |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 146 | 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 Li | 711c793 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 155 | } // namespace nfd |