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