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