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 | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
| 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 | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 29 | #include "core/rtt-estimator.hpp" |
| 30 | #include "core/scheduler.hpp" |
| 31 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/lp/packet.hpp> |
| 33 | #include <ndn-cxx/lp/sequence.hpp> |
| 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 | */ |
| 60 | time::nanoseconds idleAckTimerPeriod = time::milliseconds(5); |
| 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 | |
| 70 | /** \brief set options for reliability |
| 71 | */ |
| 72 | void |
| 73 | setOptions(const Options& options); |
| 74 | |
| 75 | /** \return GenericLinkService that owns this instance |
| 76 | * |
| 77 | * This is only used for logging, and may be nullptr. |
| 78 | */ |
| 79 | const GenericLinkService* |
| 80 | getLinkService() const; |
| 81 | |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 82 | /** \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] | 83 | * \param frags fragments of network packet |
| 84 | */ |
| 85 | void |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 86 | handleOutgoing(std::vector<lp::Packet>& frags); |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 87 | |
| 88 | /** \brief extract and parse all Acks and add Ack for contained Fragment (if any) to AckQueue |
| 89 | * \param pkt incoming LpPacket |
| 90 | */ |
| 91 | void |
| 92 | processIncomingPacket(const lp::Packet& pkt); |
| 93 | |
| 94 | /** \brief called by GenericLinkService to attach Acks onto an outgoing LpPacket |
| 95 | * \param pkt outgoing LpPacket to attach Acks to |
| 96 | * \param mtu MTU of the Transport |
| 97 | */ |
| 98 | void |
| 99 | piggyback(lp::Packet& pkt, ssize_t mtu); |
| 100 | |
| 101 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 102 | class UnackedFrag; |
| 103 | class NetPkt; |
| 104 | using UnackedFrags = std::map<lp::Sequence, UnackedFrag>; |
| 105 | |
| 106 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 107 | /** \brief assign TxSequence number to a fragment |
| 108 | * \param frag fragment to assign TxSequence to |
| 109 | * \return assigned TxSequence number |
| 110 | * \throw std::length_error assigned TxSequence is equal to the start of the existing window |
| 111 | */ |
| 112 | lp::Sequence |
| 113 | assignTxSequence(lp::Packet& frag); |
| 114 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 115 | /** \brief start the idle Ack timer |
| 116 | * |
| 117 | * This timer requests an IDLE packet to acknowledge pending fragments not already piggybacked. |
| 118 | * It is called regularly on a period configured in Options::idleAckTimerPeriod. This allows Acks |
| 119 | * to be returned to the sender, even if the link goes idle. |
| 120 | */ |
| 121 | void |
| 122 | startIdleAckTimer(); |
| 123 | |
| 124 | /** \brief cancel the idle Ack timer |
| 125 | */ |
| 126 | void |
| 127 | stopIdleAckTimer(); |
| 128 | |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 129 | /** \brief find and mark as lost fragments where a configurable number of Acks |
| 130 | * (\p m_options.seqNumLossThreshold) have been received for greater TxSequence numbers |
| 131 | * \param ackIt iterator pointing to acknowledged fragment |
| 132 | * \return vector containing iterators to fragments marked lost by this mechanism |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 133 | */ |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 134 | std::vector<UnackedFrags::iterator> |
| 135 | findLostLpPackets(UnackedFrags::iterator ackIt); |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 136 | |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 137 | /** \brief resend (or give up on) a lost fragment |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 138 | */ |
| 139 | void |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 140 | onLpPacketLost(UnackedFrags::iterator txSeqIt); |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 141 | |
| 142 | /** \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^] | 143 | * fragments, as well as its associated network packet (if any) |
| 144 | * \param fragIt iterator to acknowledged fragment |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 145 | * |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 146 | * 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] | 147 | * If the associated network packet has been fully transmitted, it will be removed. |
| 148 | */ |
| 149 | void |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 150 | onLpPacketAcknowledged(UnackedFrags::iterator fragIt); |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 151 | |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 152 | /** \brief delete a fragment from UnackedFrags and advance acknowledge window if necessary |
| 153 | * \param fragIt iterator to an UnackedFrag, must be dereferencable |
| 154 | * \post fragIt is not in m_unackedFrags |
| 155 | * \post if was equal to m_firstUnackedFrag, |
| 156 | * m_firstUnackedFrag is set to the UnackedFrag after fragIt with consideration of |
| 157 | * TxSequence number wraparound, or set to m_unackedFrags.end() if m_unackedFrags is empty |
| 158 | */ |
| 159 | void |
| 160 | deleteUnackedFrag(UnackedFrags::iterator fragIt); |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 161 | |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 162 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 163 | /** \brief contains a sent fragment that has not been acknowledged and associated data |
| 164 | */ |
| 165 | class UnackedFrag |
| 166 | { |
| 167 | public: |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 168 | explicit |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 169 | UnackedFrag(lp::Packet pkt); |
| 170 | |
| 171 | public: |
| 172 | lp::Packet pkt; |
| 173 | scheduler::ScopedEventId rtoTimer; |
| 174 | time::steady_clock::TimePoint sendTime; |
| 175 | size_t retxCount; |
| 176 | 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^] | 177 | shared_ptr<NetPkt> netPkt; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | /** \brief contains a network-layer packet with unacknowledged fragments |
| 181 | */ |
| 182 | class NetPkt |
| 183 | { |
| 184 | public: |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 185 | std::vector<UnackedFrags::iterator> unackedFrags; |
| 186 | bool didRetx = false; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 189 | public: |
| 190 | /// TxSequence TLV-TYPE (3 octets) + TxSequence TLV-LENGTH (1 octet) + sizeof(lp::Sequence) |
| 191 | static constexpr size_t RESERVED_HEADER_SPACE = 3 + 1 + sizeof(lp::Sequence); |
| 192 | |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 193 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 194 | Options m_options; |
| 195 | GenericLinkService* m_linkService; |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 196 | UnackedFrags m_unackedFrags; |
| 197 | /** An iterator that points to the first unacknowledged fragment in the current window. The window |
| 198 | * can wrap around so that the beginning of the window is at a TxSequence greater than other |
| 199 | * fragments in the window. When the window is moved past the last item in the iterator, the |
| 200 | * first fragment in the map will become the start of the window. |
| 201 | */ |
| 202 | UnackedFrags::iterator m_firstUnackedFrag; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 203 | std::queue<lp::Sequence> m_ackQueue; |
Eric Newberry | 7b0071e | 2017-07-03 17:33:31 +0000 | [diff] [blame^] | 204 | lp::Sequence m_lastTxSeqNo; |
Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 205 | scheduler::ScopedEventId m_idleAckTimer; |
| 206 | bool m_isIdleAckTimerRunning; |
| 207 | RttEstimator m_rto; |
| 208 | }; |
| 209 | |
| 210 | } // namespace face |
| 211 | } // namespace nfd |
| 212 | |
| 213 | #endif // NFD_DAEMON_FACE_LP_RELIABILITY_HPP |