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