Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 2 | /* |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -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 | #ifndef NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP |
| 27 | #define NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP |
| 28 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 29 | #include "link-service.hpp" |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 30 | #include "lp-fragmenter.hpp" |
| 31 | #include "lp-reassembler.hpp" |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 32 | #include "lp-reliability.hpp" |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 7b6433d | 2019-07-29 11:10:16 -0400 | [diff] [blame^] | 34 | #include <ndn-cxx/lp/tags.hpp> |
| 35 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 36 | namespace nfd { |
| 37 | namespace face { |
| 38 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 39 | /** \brief counters provided by GenericLinkService |
| 40 | * \note The type name 'GenericLinkServiceCounters' is implementation detail. |
| 41 | * Use 'GenericLinkService::Counters' in public API. |
| 42 | */ |
| 43 | class GenericLinkServiceCounters : public virtual LinkService::Counters |
| 44 | { |
| 45 | public: |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 46 | /** \brief count of failed fragmentations |
| 47 | */ |
| 48 | PacketCounter nFragmentationErrors; |
| 49 | |
| 50 | /** \brief count of outgoing LpPackets dropped due to exceeding MTU limit |
| 51 | * |
| 52 | * If this counter is non-zero, the operator should enable fragmentation. |
| 53 | */ |
| 54 | PacketCounter nOutOverMtu; |
| 55 | |
| 56 | /** \brief count of invalid LpPackets dropped before reassembly |
| 57 | */ |
| 58 | PacketCounter nInLpInvalid; |
| 59 | |
| 60 | /** \brief count of network-layer packets currently being reassembled |
| 61 | */ |
| 62 | SizeCounter<LpReassembler> nReassembling; |
| 63 | |
| 64 | /** \brief count of dropped partial network-layer packets due to reassembly timeout |
| 65 | */ |
| 66 | PacketCounter nReassemblyTimeouts; |
| 67 | |
| 68 | /** \brief count of invalid reassembled network-layer packets dropped |
| 69 | */ |
| 70 | PacketCounter nInNetInvalid; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 71 | |
| 72 | /** \brief count of network-layer packets that did not require retransmission of a fragment |
| 73 | */ |
| 74 | PacketCounter nAcknowledged; |
| 75 | |
| 76 | /** \brief count of network-layer packets that had at least one fragment retransmitted, but were |
| 77 | * eventually received in full |
| 78 | */ |
| 79 | PacketCounter nRetransmitted; |
| 80 | |
| 81 | /** \brief count of network-layer packets dropped because a fragment reached the maximum number |
| 82 | * of retransmissions |
| 83 | */ |
| 84 | PacketCounter nRetxExhausted; |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 85 | |
| 86 | /** \brief count of outgoing LpPackets that were marked with congestion marks |
| 87 | */ |
| 88 | PacketCounter nCongestionMarked; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 91 | /** \brief GenericLinkService is a LinkService that implements the NDNLPv2 protocol |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 92 | * \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2 |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 93 | */ |
Davide Pesavento | 16916ae | 2019-03-29 23:53:26 -0400 | [diff] [blame] | 94 | class GenericLinkService FINAL_UNLESS_WITH_TESTS : public LinkService |
| 95 | , protected virtual GenericLinkServiceCounters |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 96 | { |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 97 | public: |
| 98 | /** \brief Options that control the behavior of GenericLinkService |
| 99 | */ |
| 100 | class Options |
| 101 | { |
| 102 | public: |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 103 | Options() noexcept |
| 104 | { |
| 105 | } |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 106 | |
| 107 | public: |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 108 | /** \brief enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy |
| 109 | */ |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 110 | bool allowLocalFields = false; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 111 | |
| 112 | /** \brief enables fragmentation |
| 113 | */ |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 114 | bool allowFragmentation = false; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 115 | |
| 116 | /** \brief options for fragmentation |
| 117 | */ |
| 118 | LpFragmenter::Options fragmenterOptions; |
| 119 | |
| 120 | /** \brief enables reassembly |
| 121 | */ |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 122 | bool allowReassembly = false; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 123 | |
| 124 | /** \brief options for reassembly |
| 125 | */ |
| 126 | LpReassembler::Options reassemblerOptions; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 127 | |
| 128 | /** \brief options for reliability |
| 129 | */ |
| 130 | LpReliability::Options reliabilityOptions; |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 131 | |
| 132 | /** \brief enables send queue congestion detection and marking |
| 133 | */ |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 134 | bool allowCongestionMarking = false; |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 135 | |
| 136 | /** \brief starting value for congestion marking interval |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 137 | * |
| 138 | * The default value (100 ms) is taken from RFC 8289 (CoDel). |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 139 | */ |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 140 | time::nanoseconds baseCongestionMarkingInterval = 100_ms; |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 141 | |
| 142 | /** \brief default congestion threshold in bytes |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 143 | * |
| 144 | * The default value (64 KiB) works well for a queue capacity of 200 KiB. |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 145 | */ |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 146 | size_t defaultCongestionThreshold = 65536; |
Teng Liang | fdcbb4d | 2018-01-27 16:01:35 -0700 | [diff] [blame] | 147 | |
| 148 | /** \brief enables self-learning forwarding support |
| 149 | */ |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 150 | bool allowSelfLearning = true; |
Alexander Afanasyev | 7b6433d | 2019-07-29 11:10:16 -0400 | [diff] [blame^] | 151 | |
| 152 | /** \brief Enable encoding and decoding of GeoTags |
| 153 | * |
| 154 | * To enable, set value of enableGeoTags option to a function that generates `shared_ptr<GeoTag>` |
| 155 | */ |
| 156 | std::function<std::shared_ptr<ndn::lp::GeoTag>()> enableGeoTags; |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 159 | /** \brief counters provided by GenericLinkService |
| 160 | */ |
Junxiao Shi | fab9e0d | 2017-02-02 06:04:59 +0000 | [diff] [blame] | 161 | using Counters = GenericLinkServiceCounters; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 162 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 163 | explicit |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 164 | GenericLinkService(const Options& options = {}); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 165 | |
| 166 | /** \brief get Options used by GenericLinkService |
| 167 | */ |
| 168 | const Options& |
| 169 | getOptions() const; |
| 170 | |
| 171 | /** \brief sets Options used by GenericLinkService |
| 172 | */ |
| 173 | void |
| 174 | setOptions(const Options& options); |
| 175 | |
Junxiao Shi | fab9e0d | 2017-02-02 06:04:59 +0000 | [diff] [blame] | 176 | const Counters& |
Davide Pesavento | 16916ae | 2019-03-29 23:53:26 -0400 | [diff] [blame] | 177 | getCounters() const OVERRIDE_WITH_TESTS_ELSE_FINAL; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 178 | |
Junxiao Shi | fab9e0d | 2017-02-02 06:04:59 +0000 | [diff] [blame] | 179 | PROTECTED_WITH_TESTS_ELSE_PRIVATE: // send path |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 180 | /** \brief request an IDLE packet to transmit pending service fields |
| 181 | */ |
| 182 | void |
| 183 | requestIdlePacket(); |
| 184 | |
| 185 | /** \brief send an LpPacket fragment |
| 186 | * \param pkt LpPacket to send |
| 187 | */ |
| 188 | void |
| 189 | sendLpPacket(lp::Packet&& pkt); |
| 190 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 191 | /** \brief send Interest |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 192 | */ |
| 193 | void |
Davide Pesavento | 16916ae | 2019-03-29 23:53:26 -0400 | [diff] [blame] | 194 | doSendInterest(const Interest& interest) OVERRIDE_WITH_TESTS_ELSE_FINAL; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 195 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 196 | /** \brief send Data |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 197 | */ |
| 198 | void |
Davide Pesavento | 16916ae | 2019-03-29 23:53:26 -0400 | [diff] [blame] | 199 | doSendData(const Data& data) OVERRIDE_WITH_TESTS_ELSE_FINAL; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 200 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 201 | /** \brief send Nack |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 202 | */ |
| 203 | void |
Davide Pesavento | 16916ae | 2019-03-29 23:53:26 -0400 | [diff] [blame] | 204 | doSendNack(const ndn::lp::Nack& nack) OVERRIDE_WITH_TESTS_ELSE_FINAL; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 205 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 206 | private: // send path |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 207 | /** \brief encode link protocol fields from tags onto an outgoing LpPacket |
| 208 | * \param netPkt network-layer packet to extract tags from |
| 209 | * \param lpPacket LpPacket to add link protocol fields to |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 210 | */ |
Eric Newberry | ee400b5 | 2016-11-24 14:12:48 +0000 | [diff] [blame] | 211 | void |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 212 | encodeLpFields(const ndn::PacketBase& netPkt, lp::Packet& lpPacket); |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 213 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 214 | /** \brief send a complete network layer packet |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 215 | * \param pkt LpPacket containing a complete network layer packet |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 216 | * \param isInterest whether the network layer packet is an Interest |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 217 | */ |
| 218 | void |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 219 | sendNetPacket(lp::Packet&& pkt, bool isInterest); |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 220 | |
| 221 | /** \brief assign a sequence number to an LpPacket |
| 222 | */ |
| 223 | void |
| 224 | assignSequence(lp::Packet& pkt); |
| 225 | |
| 226 | /** \brief assign consecutive sequence numbers to LpPackets |
| 227 | */ |
| 228 | void |
| 229 | assignSequences(std::vector<lp::Packet>& pkts); |
| 230 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 231 | /** \brief if the send queue is found to be congested, add a congestion mark to the packet |
| 232 | * according to CoDel |
| 233 | * \sa https://tools.ietf.org/html/rfc8289 |
| 234 | */ |
| 235 | void |
| 236 | checkCongestionLevel(lp::Packet& pkt); |
| 237 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 238 | private: // receive path |
| 239 | /** \brief receive Packet from Transport |
| 240 | */ |
| 241 | void |
Davide Pesavento | 16916ae | 2019-03-29 23:53:26 -0400 | [diff] [blame] | 242 | doReceivePacket(Transport::Packet&& packet) OVERRIDE_WITH_TESTS_ELSE_FINAL; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 243 | |
| 244 | /** \brief decode incoming network-layer packet |
| 245 | * \param netPkt reassembled network-layer packet |
| 246 | * \param firstPkt LpPacket of first fragment |
| 247 | * |
| 248 | * If decoding is successful, a receive signal is emitted; |
| 249 | * otherwise, a warning is logged. |
| 250 | */ |
| 251 | void |
| 252 | decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt); |
| 253 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 254 | /** \brief decode incoming Interest |
| 255 | * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest |
| 256 | * \param firstPkt LpPacket of first fragment; must not have Nack field |
| 257 | * |
| 258 | * If decoding is successful, receiveInterest signal is emitted; |
| 259 | * otherwise, a warning is logged. |
| 260 | * |
| 261 | * \throw tlv::Error parse error in an LpHeader field |
| 262 | */ |
| 263 | void |
| 264 | decodeInterest(const Block& netPkt, const lp::Packet& firstPkt); |
| 265 | |
| 266 | /** \brief decode incoming Interest |
| 267 | * \param netPkt reassembled network-layer packet; TLV-TYPE must be Data |
| 268 | * \param firstPkt LpPacket of first fragment |
| 269 | * |
| 270 | * If decoding is successful, receiveData signal is emitted; |
| 271 | * otherwise, a warning is logged. |
| 272 | * |
| 273 | * \throw tlv::Error parse error in an LpHeader field |
| 274 | */ |
| 275 | void |
| 276 | decodeData(const Block& netPkt, const lp::Packet& firstPkt); |
| 277 | |
| 278 | /** \brief decode incoming Interest |
| 279 | * \param netPkt reassembled network-layer packet; TLV-TYPE must be Interest |
| 280 | * \param firstPkt LpPacket of first fragment; must have Nack field |
| 281 | * |
| 282 | * If decoding is successful, receiveNack signal is emitted; |
| 283 | * otherwise, a warning is logged. |
| 284 | * |
| 285 | * \throw tlv::Error parse error in an LpHeader field |
| 286 | */ |
| 287 | void |
| 288 | decodeNack(const Block& netPkt, const lp::Packet& firstPkt); |
| 289 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 290 | PROTECTED_WITH_TESTS_ELSE_PRIVATE: |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 291 | Options m_options; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 292 | LpFragmenter m_fragmenter; |
| 293 | LpReassembler m_reassembler; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 294 | LpReliability m_reliability; |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 295 | lp::Sequence m_lastSeqNo; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 296 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 297 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 298 | /// Time to mark next packet due to send queue congestion |
| 299 | time::steady_clock::TimePoint m_nextMarkTime; |
| 300 | /// Time last packet was marked |
| 301 | time::steady_clock::TimePoint m_lastMarkTime; |
| 302 | /// number of marked packets in the current incident of congestion |
| 303 | size_t m_nMarkedSinceInMarkingState; |
| 304 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 305 | friend class LpReliability; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 306 | }; |
| 307 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 308 | inline const GenericLinkService::Options& |
| 309 | GenericLinkService::getOptions() const |
| 310 | { |
| 311 | return m_options; |
| 312 | } |
| 313 | |
Eric Newberry | 4c3e6b8 | 2015-11-10 16:48:42 -0700 | [diff] [blame] | 314 | inline const GenericLinkService::Counters& |
| 315 | GenericLinkService::getCounters() const |
| 316 | { |
| 317 | return *this; |
| 318 | } |
| 319 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 320 | } // namespace face |
| 321 | } // namespace nfd |
| 322 | |
Eric Newberry | 86d3187 | 2015-09-23 16:24:59 -0700 | [diff] [blame] | 323 | #endif // NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP |