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