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