Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 944f38b | 2017-07-20 20:54:22 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Yanbiao Li | 73860e3 | 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 "face-manager.hpp" |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 27 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 28 | #include "common/logger.hpp" |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 29 | #include "face/generic-link-service.hpp" |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 30 | #include "face/protocol-factory.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 31 | #include "fw/face-table.hpp" |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 32 | |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/lp/tags.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 34 | #include <ndn-cxx/mgmt/nfd/channel-status.hpp> |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 35 | #include <ndn-cxx/mgmt/nfd/face-event-notification.hpp> |
| 36 | #include <ndn-cxx/mgmt/nfd/face-query-filter.hpp> |
| 37 | #include <ndn-cxx/mgmt/nfd/face-status.hpp> |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 38 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 39 | namespace nfd { |
| 40 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 41 | NFD_LOG_INIT(FaceManager); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 42 | |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 43 | FaceManager::FaceManager(FaceSystem& faceSystem, |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 44 | Dispatcher& dispatcher, CommandAuthenticator& authenticator) |
| 45 | : ManagerBase("faces", dispatcher, authenticator) |
Junxiao Shi | ea47bde | 2017-01-26 17:49:16 +0000 | [diff] [blame] | 46 | , m_faceSystem(faceSystem) |
| 47 | , m_faceTable(faceSystem.getFaceTable()) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 48 | { |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 49 | // register handlers for ControlCommand |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 50 | registerCommandHandler<ndn::nfd::FaceCreateCommand>("create", |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 51 | [this] (auto&&, auto&&, auto&&, auto&&... args) { createFace(std::forward<decltype(args)>(args)...); }); |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 52 | registerCommandHandler<ndn::nfd::FaceUpdateCommand>("update", |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 53 | [this] (auto&&, auto&&, auto&&... args) { updateFace(std::forward<decltype(args)>(args)...); }); |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 54 | registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy", |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 55 | [this] (auto&&, auto&&, auto&&, auto&&... args) { destroyFace(std::forward<decltype(args)>(args)...); }); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 56 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 57 | // register handlers for StatusDataset |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 58 | registerStatusDatasetHandler("list", |
| 59 | [this] (auto&&, auto&&, auto&&... args) { listFaces(std::forward<decltype(args)>(args)...); }); |
| 60 | registerStatusDatasetHandler("channels", |
| 61 | [this] (auto&&, auto&&, auto&&... args) { listChannels(std::forward<decltype(args)>(args)...); }); |
| 62 | registerStatusDatasetHandler("query", |
| 63 | [this] (auto&&, auto&&... args) { queryFaces(std::forward<decltype(args)>(args)...); }); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 64 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 65 | // register notification stream |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 66 | m_postNotification = registerNotificationStream("events"); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 67 | m_faceAddConn = m_faceTable.afterAdd.connect([this] (const Face& face) { |
| 68 | connectFaceStateChangeSignal(face); |
| 69 | notifyFaceEvent(face, ndn::nfd::FACE_EVENT_CREATED); |
| 70 | }); |
| 71 | m_faceRemoveConn = m_faceTable.beforeRemove.connect([this] (const Face& face) { |
| 72 | notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DESTROYED); |
| 73 | }); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 77 | FaceManager::createFace(const ControlParameters& parameters, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 78 | const ndn::mgmt::CommandContinuation& done) |
| 79 | { |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 80 | FaceUri remoteUri; |
| 81 | if (!remoteUri.parse(parameters.getUri())) { |
| 82 | NFD_LOG_TRACE("failed to parse remote URI: " << parameters.getUri()); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 83 | done(ControlResponse(400, "Malformed command")); |
| 84 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 87 | if (!remoteUri.isCanonical()) { |
| 88 | NFD_LOG_TRACE("received non-canonical remote URI: " << remoteUri.toString()); |
| 89 | done(ControlResponse(400, "Non-canonical remote URI")); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 90 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 93 | std::optional<FaceUri> localUri; |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 94 | if (parameters.hasLocalUri()) { |
| 95 | localUri = FaceUri{}; |
| 96 | |
| 97 | if (!localUri->parse(parameters.getLocalUri())) { |
| 98 | NFD_LOG_TRACE("failed to parse local URI: " << parameters.getLocalUri()); |
| 99 | done(ControlResponse(400, "Malformed command")); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (!localUri->isCanonical()) { |
| 104 | NFD_LOG_TRACE("received non-canonical local URI: " << localUri->toString()); |
| 105 | done(ControlResponse(400, "Non-canonical local URI")); |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | |
Davide Pesavento | e5eebad | 2017-04-06 20:23:26 -0400 | [diff] [blame] | 110 | face::ProtocolFactory* factory = m_faceSystem.getFactoryByScheme(remoteUri.getScheme()); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 111 | if (factory == nullptr) { |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 112 | NFD_LOG_TRACE("received create request for unsupported protocol: " << remoteUri.getScheme()); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 113 | done(ControlResponse(406, "Unsupported protocol")); |
| 114 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 117 | face::FaceParams faceParams; |
| 118 | faceParams.persistency = parameters.getFacePersistency(); |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 119 | if (parameters.hasBaseCongestionMarkingInterval()) { |
| 120 | faceParams.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval(); |
| 121 | } |
| 122 | if (parameters.hasDefaultCongestionThreshold()) { |
| 123 | faceParams.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold(); |
| 124 | } |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 125 | if (parameters.hasMtu()) { |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 126 | // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t |
Eric Newberry | cb6551e | 2020-03-02 14:12:16 -0800 | [diff] [blame] | 127 | faceParams.mtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu()); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 128 | } |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 129 | faceParams.wantLocalFields = parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 130 | parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED); |
| 131 | faceParams.wantLpReliability = parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED) && |
| 132 | parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED); |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 133 | if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) { |
| 134 | faceParams.wantCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED); |
| 135 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 136 | try { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 137 | factory->createFace({remoteUri, localUri, faceParams}, |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 138 | [this, parameters, done] (const auto& face) { |
| 139 | this->afterCreateFaceSuccess(face, parameters, done); |
| 140 | }, |
| 141 | [done] (uint32_t status, const std::string& reason) { |
| 142 | NFD_LOG_DEBUG("Face creation failed: " << reason); |
| 143 | done(ControlResponse(status, reason)); |
| 144 | }); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 145 | } |
| 146 | catch (const std::runtime_error& error) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 147 | NFD_LOG_ERROR("Face creation failed: " << error.what()); |
| 148 | done(ControlResponse(500, "Face creation failed due to internal error")); |
| 149 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 150 | } |
| 151 | catch (const std::logic_error& error) { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 152 | NFD_LOG_ERROR("Face creation failed: " << error.what()); |
| 153 | done(ControlResponse(500, "Face creation failed due to internal error")); |
| 154 | return; |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 158 | template<typename T> |
| 159 | static void |
| 160 | copyMtu(const Face& face, T& to) |
| 161 | { |
Eric Newberry | cb6551e | 2020-03-02 14:12:16 -0800 | [diff] [blame] | 162 | if (face.getMtu() >= 0) { |
| 163 | to.setMtu(std::min<size_t>(face.getMtu(), ndn::MAX_NDN_PACKET_SIZE)); |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 164 | } |
Eric Newberry | cb6551e | 2020-03-02 14:12:16 -0800 | [diff] [blame] | 165 | else if (face.getMtu() == face::MTU_UNLIMITED) { |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 166 | to.setMtu(ndn::MAX_NDN_PACKET_SIZE); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static ControlParameters |
| 171 | makeUpdateFaceResponse(const Face& face) |
| 172 | { |
| 173 | ControlParameters params; |
| 174 | params.setFaceId(face.getId()) |
| 175 | .setFacePersistency(face.getPersistency()); |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 176 | copyMtu(face, params); |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 177 | |
| 178 | auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService()); |
| 179 | if (linkService != nullptr) { |
| 180 | const auto& options = linkService->getOptions(); |
| 181 | params.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval) |
| 182 | .setDefaultCongestionThreshold(options.defaultCongestionThreshold) |
| 183 | .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields, false) |
| 184 | .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled, false) |
| 185 | .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking, false); |
| 186 | } |
| 187 | |
| 188 | return params; |
| 189 | } |
| 190 | |
| 191 | static ControlParameters |
| 192 | makeCreateFaceResponse(const Face& face) |
| 193 | { |
| 194 | ControlParameters params = makeUpdateFaceResponse(face); |
| 195 | params.setUri(face.getRemoteUri().toString()) |
| 196 | .setLocalUri(face.getLocalUri().toString()); |
| 197 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 198 | return params; |
| 199 | } |
| 200 | |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 201 | void |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 202 | FaceManager::afterCreateFaceSuccess(const shared_ptr<Face>& face, |
| 203 | const ControlParameters& parameters, |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 204 | const ndn::mgmt::CommandContinuation& done) |
| 205 | { |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 206 | if (face->getId() != face::INVALID_FACEID) { // Face already exists |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 207 | NFD_LOG_TRACE("Attempted to create duplicate face of " << face->getId()); |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 208 | ControlParameters response = makeCreateFaceResponse(*face); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 209 | done(ControlResponse(409, "Face with remote URI already exists").setBody(response.wireEncode())); |
| 210 | return; |
| 211 | } |
| 212 | |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 213 | // If scope non-local and flags set to enable local fields, request shouldn't |
| 214 | // have made it this far |
| 215 | BOOST_ASSERT(face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL || |
| 216 | !parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) || |
| 217 | (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 218 | !parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED))); |
| 219 | |
| 220 | m_faceTable.add(face); |
| 221 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 222 | ControlParameters response = makeCreateFaceResponse(*face); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 223 | done(ControlResponse(200, "OK").setBody(response.wireEncode())); |
| 224 | } |
| 225 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 226 | static void |
| 227 | updateLinkServiceOptions(Face& face, const ControlParameters& parameters) |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 228 | { |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 229 | auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService()); |
| 230 | if (linkService == nullptr) { |
| 231 | return; |
| 232 | } |
| 233 | auto options = linkService->getOptions(); |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 234 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 235 | if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 236 | face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) { |
| 237 | options.allowLocalFields = parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED); |
| 238 | } |
| 239 | if (parameters.hasFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) { |
| 240 | options.reliabilityOptions.isEnabled = parameters.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED); |
| 241 | } |
| 242 | if (parameters.hasFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) { |
| 243 | options.allowCongestionMarking = parameters.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED); |
| 244 | } |
| 245 | if (parameters.hasBaseCongestionMarkingInterval()) { |
| 246 | options.baseCongestionMarkingInterval = parameters.getBaseCongestionMarkingInterval(); |
| 247 | } |
| 248 | if (parameters.hasDefaultCongestionThreshold()) { |
| 249 | options.defaultCongestionThreshold = parameters.getDefaultCongestionThreshold(); |
| 250 | } |
| 251 | |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 252 | if (parameters.hasMtu()) { |
| 253 | // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t |
| 254 | options.overrideMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), parameters.getMtu()); |
| 255 | } |
| 256 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 257 | linkService->setOptions(options); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 261 | FaceManager::updateFace(const Interest& interest, |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 262 | const ControlParameters& parameters, |
| 263 | const ndn::mgmt::CommandContinuation& done) |
| 264 | { |
| 265 | FaceId faceId = parameters.getFaceId(); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 266 | if (faceId == face::INVALID_FACEID) { // Self-update |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 267 | auto incomingFaceIdTag = interest.getTag<lp::IncomingFaceIdTag>(); |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 268 | if (incomingFaceIdTag == nullptr) { |
| 269 | NFD_LOG_TRACE("unable to determine face for self-update"); |
| 270 | done(ControlResponse(404, "No FaceId specified and IncomingFaceId not available")); |
| 271 | return; |
| 272 | } |
| 273 | faceId = *incomingFaceIdTag; |
| 274 | } |
| 275 | |
| 276 | Face* face = m_faceTable.get(faceId); |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 277 | if (face == nullptr) { |
| 278 | NFD_LOG_TRACE("invalid face specified"); |
| 279 | done(ControlResponse(404, "Specified face does not exist")); |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | // Verify validity of requested changes |
| 284 | ControlParameters response; |
| 285 | bool areParamsValid = true; |
| 286 | |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 287 | if (parameters.hasFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 288 | parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED) && |
| 289 | face->getScope() != ndn::nfd::FACE_SCOPE_LOCAL) { |
| 290 | NFD_LOG_TRACE("received request to enable local fields on non-local face"); |
| 291 | areParamsValid = false; |
| 292 | response.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, |
| 293 | parameters.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)); |
| 294 | } |
| 295 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 296 | // check whether the requested FacePersistency change is valid if it's present |
| 297 | if (parameters.hasFacePersistency()) { |
| 298 | auto persistency = parameters.getFacePersistency(); |
| 299 | if (!face->getTransport()->canChangePersistencyTo(persistency)) { |
| 300 | NFD_LOG_TRACE("cannot change face persistency to " << persistency); |
| 301 | areParamsValid = false; |
| 302 | response.setFacePersistency(persistency); |
| 303 | } |
| 304 | } |
| 305 | |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 306 | // check whether the requested MTU override is valid (if it's present) |
| 307 | if (parameters.hasMtu()) { |
| 308 | auto mtu = parameters.getMtu(); |
| 309 | // The face system limits MTUs to ssize_t, but the management protocol uses uint64_t |
| 310 | auto actualMtu = std::min<uint64_t>(std::numeric_limits<ssize_t>::max(), mtu); |
| 311 | auto linkService = dynamic_cast<face::GenericLinkService*>(face->getLinkService()); |
| 312 | if (linkService == nullptr || !linkService->canOverrideMtuTo(actualMtu)) { |
| 313 | NFD_LOG_TRACE("cannot override face MTU to " << mtu); |
| 314 | areParamsValid = false; |
| 315 | response.setMtu(mtu); |
| 316 | } |
| 317 | } |
| 318 | |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 319 | if (!areParamsValid) { |
| 320 | done(ControlResponse(409, "Invalid properties specified").setBody(response.wireEncode())); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | // All specified properties are valid, so make changes |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 325 | if (parameters.hasFacePersistency()) { |
| 326 | face->setPersistency(parameters.getFacePersistency()); |
| 327 | } |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 328 | updateLinkServiceOptions(*face, parameters); |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 329 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 330 | // Prepare and send ControlResponse |
| 331 | response = makeUpdateFaceResponse(*face); |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 332 | done(ControlResponse(200, "OK").setBody(response.wireEncode())); |
| 333 | } |
| 334 | |
| 335 | void |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 336 | FaceManager::destroyFace(const ControlParameters& parameters, |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 337 | const ndn::mgmt::CommandContinuation& done) |
| 338 | { |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 339 | Face* face = m_faceTable.get(parameters.getFaceId()); |
| 340 | if (face != nullptr) { |
| 341 | face->close(); |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | done(ControlResponse(200, "OK").setBody(parameters.wireEncode())); |
| 345 | } |
| 346 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 347 | template<typename T> |
| 348 | static void |
| 349 | copyFaceProperties(const Face& face, T& to) |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 350 | { |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 351 | to.setFaceId(face.getId()) |
| 352 | .setRemoteUri(face.getRemoteUri().toString()) |
| 353 | .setLocalUri(face.getLocalUri().toString()) |
| 354 | .setFaceScope(face.getScope()) |
| 355 | .setFacePersistency(face.getPersistency()) |
| 356 | .setLinkType(face.getLinkType()); |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 357 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 358 | auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService()); |
| 359 | if (linkService != nullptr) { |
| 360 | const auto& options = linkService->getOptions(); |
| 361 | to.setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, options.allowLocalFields) |
| 362 | .setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, options.reliabilityOptions.isEnabled) |
| 363 | .setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, options.allowCongestionMarking); |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 364 | } |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 367 | static ndn::nfd::FaceStatus |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 368 | makeFaceStatus(const Face& face, const time::steady_clock::time_point& now) |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 369 | { |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 370 | ndn::nfd::FaceStatus status; |
| 371 | copyFaceProperties(face, status); |
| 372 | |
| 373 | auto expirationTime = face.getExpirationTime(); |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 374 | if (expirationTime != time::steady_clock::time_point::max()) { |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 375 | status.setExpirationPeriod(std::max(0_ms, |
| 376 | time::duration_cast<time::milliseconds>(expirationTime - now))); |
| 377 | } |
| 378 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 379 | auto linkService = dynamic_cast<face::GenericLinkService*>(face.getLinkService()); |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 380 | if (linkService != nullptr) { |
| 381 | const auto& options = linkService->getOptions(); |
| 382 | status.setBaseCongestionMarkingInterval(options.baseCongestionMarkingInterval) |
| 383 | .setDefaultCongestionThreshold(options.defaultCongestionThreshold); |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 386 | copyMtu(face, status); |
| 387 | |
| 388 | const auto& counters = face.getCounters(); |
| 389 | status.setNInInterests(counters.nInInterests) |
| 390 | .setNOutInterests(counters.nOutInterests) |
| 391 | .setNInData(counters.nInData) |
| 392 | .setNOutData(counters.nOutData) |
| 393 | .setNInNacks(counters.nInNacks) |
| 394 | .setNOutNacks(counters.nOutNacks) |
| 395 | .setNInBytes(counters.nInBytes) |
| 396 | .setNOutBytes(counters.nOutBytes); |
| 397 | |
| 398 | return status; |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Eric Newberry | b5aa7f5 | 2016-09-03 20:36:12 -0700 | [diff] [blame] | 401 | void |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 402 | FaceManager::listFaces(ndn::mgmt::StatusDatasetContext& context) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 403 | { |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 404 | auto now = time::steady_clock::now(); |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 405 | for (const auto& face : m_faceTable) { |
| 406 | ndn::nfd::FaceStatus status = makeFaceStatus(face, now); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 407 | context.append(status.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 408 | } |
| 409 | context.end(); |
| 410 | } |
| 411 | |
| 412 | void |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 413 | FaceManager::listChannels(ndn::mgmt::StatusDatasetContext& context) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 414 | { |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 415 | auto factories = m_faceSystem.listProtocolFactories(); |
Davide Pesavento | e5eebad | 2017-04-06 20:23:26 -0400 | [diff] [blame] | 416 | for (const auto* factory : factories) { |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 417 | for (const auto& channel : factory->getChannels()) { |
| 418 | ndn::nfd::ChannelStatus entry; |
| 419 | entry.setLocalUri(channel->getUri().toString()); |
| 420 | context.append(entry.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 423 | context.end(); |
| 424 | } |
| 425 | |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 426 | static bool |
| 427 | matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 428 | { |
| 429 | if (filter.hasFaceId() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 430 | filter.getFaceId() != static_cast<uint64_t>(face.getId())) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 431 | return false; |
| 432 | } |
| 433 | |
| 434 | if (filter.hasUriScheme() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 435 | filter.getUriScheme() != face.getRemoteUri().getScheme() && |
| 436 | filter.getUriScheme() != face.getLocalUri().getScheme()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 437 | return false; |
| 438 | } |
| 439 | |
| 440 | if (filter.hasRemoteUri() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 441 | filter.getRemoteUri() != face.getRemoteUri().toString()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 442 | return false; |
| 443 | } |
| 444 | |
| 445 | if (filter.hasLocalUri() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 446 | filter.getLocalUri() != face.getLocalUri().toString()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 447 | return false; |
| 448 | } |
| 449 | |
| 450 | if (filter.hasFaceScope() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 451 | filter.getFaceScope() != face.getScope()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 452 | return false; |
| 453 | } |
| 454 | |
| 455 | if (filter.hasFacePersistency() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 456 | filter.getFacePersistency() != face.getPersistency()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 457 | return false; |
| 458 | } |
| 459 | |
| 460 | if (filter.hasLinkType() && |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 461 | filter.getLinkType() != face.getLinkType()) { |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 462 | return false; |
| 463 | } |
| 464 | |
| 465 | return true; |
| 466 | } |
| 467 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 468 | void |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 469 | FaceManager::queryFaces(const Interest& interest, |
| 470 | ndn::mgmt::StatusDatasetContext& context) |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 471 | { |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 472 | ndn::nfd::FaceQueryFilter faceFilter; |
| 473 | try { |
| 474 | faceFilter.wireDecode(interest.getName()[-1].blockFromValue()); |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 475 | } |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 476 | catch (const tlv::Error& e) { |
| 477 | NFD_LOG_DEBUG("Malformed query filter: " << e.what()); |
| 478 | return context.reject(ControlResponse(400, "Malformed filter")); |
| 479 | } |
| 480 | |
| 481 | auto now = time::steady_clock::now(); |
| 482 | for (const auto& face : m_faceTable) { |
| 483 | if (matchFilter(faceFilter, face)) { |
| 484 | ndn::nfd::FaceStatus status = makeFaceStatus(face, now); |
| 485 | context.append(status.wireEncode()); |
| 486 | } |
| 487 | } |
| 488 | context.end(); |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | void |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 492 | FaceManager::notifyFaceEvent(const Face& face, ndn::nfd::FaceEventKind kind) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 493 | { |
| 494 | ndn::nfd::FaceEventNotification notification; |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 495 | notification.setKind(kind); |
Davide Pesavento | 2818132 | 2018-11-08 16:44:50 -0500 | [diff] [blame] | 496 | copyFaceProperties(face, notification); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 497 | |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 498 | m_postNotification(notification.wireEncode()); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | void |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 502 | FaceManager::connectFaceStateChangeSignal(const Face& face) |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 503 | { |
Davide Pesavento | 3cf75dc | 2018-03-17 00:38:03 -0400 | [diff] [blame] | 504 | using face::FaceState; |
| 505 | |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 506 | FaceId faceId = face.getId(); |
| 507 | m_faceStateChangeConn[faceId] = face.afterStateChange.connect( |
Davide Pesavento | 3cf75dc | 2018-03-17 00:38:03 -0400 | [diff] [blame] | 508 | [this, faceId, &face] (FaceState oldState, FaceState newState) { |
| 509 | if (newState == FaceState::UP) { |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 510 | notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP); |
| 511 | } |
Davide Pesavento | 3cf75dc | 2018-03-17 00:38:03 -0400 | [diff] [blame] | 512 | else if (newState == FaceState::DOWN) { |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 513 | notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN); |
| 514 | } |
Davide Pesavento | 3cf75dc | 2018-03-17 00:38:03 -0400 | [diff] [blame] | 515 | else if (newState == FaceState::CLOSED) { |
| 516 | // cannot use face.getId() because it may already be reset to INVALID_FACEID |
Eric Newberry | 1b4ba05 | 2016-10-07 23:04:07 -0700 | [diff] [blame] | 517 | m_faceStateChangeConn.erase(faceId); |
| 518 | } |
| 519 | }); |
Yanbiao Li | 73860e3 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 522 | } // namespace nfd |