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