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