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