Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [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_LP_RELIABILITY_HPP |
| 27 | #define NFD_DAEMON_FACE_LP_RELIABILITY_HPP |
| 28 | |
Eric Newberry | 5ab4cf8 | 2020-02-03 15:40:16 -0800 | [diff] [blame] | 29 | #include "face-common.hpp" |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 30 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/lp/packet.hpp> |
| 32 | #include <ndn-cxx/lp/sequence.hpp> |
Davide Pesavento | f190cfa | 2019-07-17 20:14:11 -0400 | [diff] [blame] | 33 | #include <ndn-cxx/util/rtt-estimator.hpp> |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 34 | |
| 35 | #include <queue> |
| 36 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 37 | namespace nfd::face { |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 38 | |
| 39 | class GenericLinkService; |
| 40 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 41 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 42 | * \brief Provides for reliable sending and receiving of link-layer packets. |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 43 | * \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2 |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 44 | */ |
| 45 | class LpReliability : noncopyable |
| 46 | { |
| 47 | public: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 48 | /// TxSequence TLV-TYPE (3 octets) + TLV-LENGTH (1 octet) + lp::Sequence (8 octets) |
| 49 | static constexpr size_t RESERVED_HEADER_SPACE = tlv::sizeOfVarNumber(lp::tlv::TxSequence) + |
| 50 | tlv::sizeOfVarNumber(sizeof(lp::Sequence)) + |
| 51 | sizeof(lp::Sequence); |
| 52 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 53 | struct Options |
| 54 | { |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 55 | /** \brief Enables link-layer reliability. |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 56 | */ |
| 57 | bool isEnabled = false; |
| 58 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 59 | /** \brief Maximum number of retransmissions for an LpPacket. |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 60 | */ |
| 61 | size_t maxRetx = 3; |
| 62 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 63 | /** \brief Period between sending pending Acks in an IDLE packet. |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 64 | */ |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 65 | time::nanoseconds idleAckTimerPeriod = 5_ms; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 66 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 67 | /** \brief A fragment is considered lost if this number of fragments with greater sequence |
| 68 | * numbers are acknowledged. |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 69 | */ |
| 70 | size_t seqNumLossThreshold = 3; |
| 71 | }; |
| 72 | |
| 73 | LpReliability(const Options& options, GenericLinkService* linkService); |
| 74 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 75 | /** \brief Signals on Interest dropped by reliability system for exceeding allowed number of retx. |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 76 | */ |
| 77 | signal::Signal<LpReliability, Interest> onDroppedInterest; |
| 78 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 79 | /** \brief Set options for reliability. |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 80 | */ |
| 81 | void |
| 82 | setOptions(const Options& options); |
| 83 | |
| 84 | /** \return GenericLinkService that owns this instance |
| 85 | * |
| 86 | * This is only used for logging, and may be nullptr. |
| 87 | */ |
| 88 | const GenericLinkService* |
| 89 | getLinkService() const; |
| 90 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 91 | /** \brief Observe outgoing fragment(s) of a network packet and store for potential retransmission. |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 92 | * \param frags fragments of network packet |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 93 | * \param pkt encapsulated network packet |
| 94 | * \param isInterest whether the network packet is an Interest |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 95 | */ |
| 96 | void |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 97 | handleOutgoing(std::vector<lp::Packet>& frags, lp::Packet&& pkt, bool isInterest); |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 98 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 99 | /** \brief Extract and parse all Acks and add Ack for contained Fragment (if any) to AckQueue. |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 100 | * \param pkt incoming LpPacket |
Eric Newberry | 32f7eac | 2020-02-07 14:40:17 -0800 | [diff] [blame] | 101 | * \return whether incoming LpPacket is new and not a duplicate |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 102 | */ |
Eric Newberry | 32f7eac | 2020-02-07 14:40:17 -0800 | [diff] [blame] | 103 | bool |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 104 | processIncomingPacket(const lp::Packet& pkt); |
| 105 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 106 | /** \brief Called by GenericLinkService to attach Acks onto an outgoing LpPacket. |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 107 | * \param pkt outgoing LpPacket to attach Acks to |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 108 | * \param mtu MTU of the transport |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 109 | */ |
| 110 | void |
| 111 | piggyback(lp::Packet& pkt, ssize_t mtu); |
| 112 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 113 | NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 114 | class UnackedFrag; |
| 115 | class NetPkt; |
| 116 | using UnackedFrags = std::map<lp::Sequence, UnackedFrag>; |
| 117 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 118 | /** \brief Assign TxSequence number to a fragment. |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 119 | * \param frag fragment to assign TxSequence to |
| 120 | * \return assigned TxSequence number |
| 121 | * \throw std::length_error assigned TxSequence is equal to the start of the existing window |
| 122 | */ |
| 123 | lp::Sequence |
| 124 | assignTxSequence(lp::Packet& frag); |
| 125 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 126 | /** \brief Start the idle Ack timer. |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 127 | * |
| 128 | * This timer requests an IDLE packet to acknowledge pending fragments not already piggybacked. |
| 129 | * It is called regularly on a period configured in Options::idleAckTimerPeriod. This allows Acks |
| 130 | * to be returned to the sender, even if the link goes idle. |
| 131 | */ |
| 132 | void |
| 133 | startIdleAckTimer(); |
| 134 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 135 | /** \brief Find and mark as lost fragments where a configurable number of Acks |
| 136 | * (Options::seqNumLossThreshold) have been received for greater TxSequence numbers. |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 137 | * \param ackIt iterator pointing to acknowledged fragment |
Eric Newberry | 971d962 | 2018-03-30 23:29:26 -0700 | [diff] [blame] | 138 | * \return vector containing TxSequences of fragments marked lost by this mechanism |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 139 | */ |
Eric Newberry | 971d962 | 2018-03-30 23:29:26 -0700 | [diff] [blame] | 140 | std::vector<lp::Sequence> |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 141 | findLostLpPackets(UnackedFrags::iterator ackIt); |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 142 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 143 | /** \brief Resend (or give up on) a lost fragment. |
Eric Newberry | 971d962 | 2018-03-30 23:29:26 -0700 | [diff] [blame] | 144 | * \return vector of the TxSequences of fragments removed due to a network packet being removed |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 145 | */ |
Eric Newberry | 971d962 | 2018-03-30 23:29:26 -0700 | [diff] [blame] | 146 | std::vector<lp::Sequence> |
Eric Newberry | 32f7eac | 2020-02-07 14:40:17 -0800 | [diff] [blame] | 147 | onLpPacketLost(lp::Sequence txSeq, bool isTimeout); |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 148 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 149 | /** \brief Remove the fragment with the given sequence number from the map of unacknowledged |
| 150 | * fragments, as well as its associated network packet (if any). |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 151 | * \param fragIt iterator to acknowledged fragment |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 152 | * |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 153 | * If the given TxSequence marks the beginning of the send window, the window will be incremented. |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 154 | * If the associated network packet has been fully transmitted, it will be removed. |
| 155 | */ |
| 156 | void |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 157 | onLpPacketAcknowledged(UnackedFrags::iterator fragIt); |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 158 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 159 | /** \brief Delete a fragment from UnackedFrags and advance acknowledge window if necessary. |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 160 | * \param fragIt iterator to an UnackedFrag, must be dereferencable |
| 161 | * \post fragIt is not in m_unackedFrags |
| 162 | * \post if was equal to m_firstUnackedFrag, |
| 163 | * m_firstUnackedFrag is set to the UnackedFrag after fragIt with consideration of |
| 164 | * TxSequence number wraparound, or set to m_unackedFrags.end() if m_unackedFrags is empty |
| 165 | */ |
| 166 | void |
| 167 | deleteUnackedFrag(UnackedFrags::iterator fragIt); |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 168 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 169 | NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 170 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 171 | * \brief Contains a sent fragment that has not been acknowledged and associated data. |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 172 | */ |
| 173 | class UnackedFrag |
| 174 | { |
| 175 | public: |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 176 | explicit |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 177 | UnackedFrag(lp::Packet pkt); |
| 178 | |
| 179 | public: |
| 180 | lp::Packet pkt; |
| 181 | scheduler::ScopedEventId rtoTimer; |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 182 | time::steady_clock::time_point sendTime; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 183 | size_t retxCount; |
| 184 | size_t nGreaterSeqAcks; //!< number of Acks received for sequences greater than this fragment |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 185 | shared_ptr<NetPkt> netPkt; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 188 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 189 | * \brief Contains a network-layer packet with unacknowledged fragments. |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 190 | */ |
| 191 | class NetPkt |
| 192 | { |
| 193 | public: |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 194 | NetPkt(lp::Packet&& pkt, bool isInterest); |
| 195 | |
| 196 | public: |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 197 | std::vector<UnackedFrags::iterator> unackedFrags; |
Eric Newberry | 41aba10 | 2017-11-01 16:42:13 -0700 | [diff] [blame] | 198 | lp::Packet pkt; |
| 199 | bool isInterest; |
| 200 | bool didRetx; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 203 | Options m_options; |
| 204 | GenericLinkService* m_linkService; |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 205 | UnackedFrags m_unackedFrags; |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 206 | // An iterator that points to the first unacknowledged fragment in the current window. The window |
| 207 | // can wrap around so that the beginning of the window is at a TxSequence greater than other |
| 208 | // fragments in the window. When the window is moved past the last item in the iterator, the |
| 209 | // first fragment in the map will become the start of the window. |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 210 | UnackedFrags::iterator m_firstUnackedFrag; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 211 | std::queue<lp::Sequence> m_ackQueue; |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 212 | std::map<lp::Sequence, time::steady_clock::time_point> m_recentRecvSeqs; |
Eric Newberry | 32f7eac | 2020-02-07 14:40:17 -0800 | [diff] [blame] | 213 | std::queue<lp::Sequence> m_recentRecvSeqsQueue; |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame] | 214 | lp::Sequence m_lastTxSeqNo; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 215 | scheduler::ScopedEventId m_idleAckTimer; |
Davide Pesavento | f190cfa | 2019-07-17 20:14:11 -0400 | [diff] [blame] | 216 | ndn::util::RttEstimator m_rttEst; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Eric Newberry | 5ab4cf8 | 2020-02-03 15:40:16 -0800 | [diff] [blame] | 219 | std::ostream& |
| 220 | operator<<(std::ostream& os, const FaceLogHelper<LpReliability>& flh); |
| 221 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 222 | } // namespace nfd::face |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 223 | |
| 224 | #endif // NFD_DAEMON_FACE_LP_RELIABILITY_HPP |