blob: c83b0bc0b3cfaa9a8576d91d67cef3e6f4f1c9b2 [file] [log] [blame]
Eric Newberry185ab292017-03-28 06:45:39 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry7b0071e2017-07-03 17:33:31 +00002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Eric Newberry185ab292017-03-28 06:45:39 +00004 * 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 Newberry5ab4cf82020-02-03 15:40:16 -080029#include "face-common.hpp"
Eric Newberry185ab292017-03-28 06:45:39 +000030
Eric Newberry185ab292017-03-28 06:45:39 +000031#include <ndn-cxx/lp/packet.hpp>
32#include <ndn-cxx/lp/sequence.hpp>
Davide Pesaventof190cfa2019-07-17 20:14:11 -040033#include <ndn-cxx/util/rtt-estimator.hpp>
Eric Newberry185ab292017-03-28 06:45:39 +000034
35#include <queue>
36
37namespace nfd {
38namespace face {
39
40class GenericLinkService;
41
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040042/**
43 * \brief Provides for reliable sending and receiving of link-layer packets
44 * \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2
Eric Newberry185ab292017-03-28 06:45:39 +000045 */
46class LpReliability : noncopyable
47{
48public:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040049 /// TxSequence TLV-TYPE (3 octets) + TLV-LENGTH (1 octet) + lp::Sequence (8 octets)
50 static constexpr size_t RESERVED_HEADER_SPACE = tlv::sizeOfVarNumber(lp::tlv::TxSequence) +
51 tlv::sizeOfVarNumber(sizeof(lp::Sequence)) +
52 sizeof(lp::Sequence);
53
Eric Newberry185ab292017-03-28 06:45:39 +000054 struct Options
55 {
56 /** \brief enables link-layer reliability
57 */
58 bool isEnabled = false;
59
60 /** \brief maximum number of retransmissions for an LpPacket
61 */
62 size_t maxRetx = 3;
63
64 /** \brief period between sending pending Acks in an IDLE packet
65 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -040066 time::nanoseconds idleAckTimerPeriod = 5_ms;
Eric Newberry185ab292017-03-28 06:45:39 +000067
68 /** \brief a fragment is considered lost if this number of fragments with greater sequence
69 * numbers are acknowledged
70 */
71 size_t seqNumLossThreshold = 3;
72 };
73
74 LpReliability(const Options& options, GenericLinkService* linkService);
75
Eric Newberry41aba102017-11-01 16:42:13 -070076 /** \brief signals on Interest dropped by reliability system for exceeding allowed number of retx
77 */
78 signal::Signal<LpReliability, Interest> onDroppedInterest;
79
Eric Newberry185ab292017-03-28 06:45:39 +000080 /** \brief set options for reliability
81 */
82 void
83 setOptions(const Options& options);
84
85 /** \return GenericLinkService that owns this instance
86 *
87 * This is only used for logging, and may be nullptr.
88 */
89 const GenericLinkService*
90 getLinkService() const;
91
Eric Newberry7b0071e2017-07-03 17:33:31 +000092 /** \brief observe outgoing fragment(s) of a network packet and store for potential retransmission
Eric Newberry185ab292017-03-28 06:45:39 +000093 * \param frags fragments of network packet
Eric Newberry41aba102017-11-01 16:42:13 -070094 * \param pkt encapsulated network packet
95 * \param isInterest whether the network packet is an Interest
Eric Newberry185ab292017-03-28 06:45:39 +000096 */
97 void
Eric Newberry41aba102017-11-01 16:42:13 -070098 handleOutgoing(std::vector<lp::Packet>& frags, lp::Packet&& pkt, bool isInterest);
Eric Newberry185ab292017-03-28 06:45:39 +000099
100 /** \brief extract and parse all Acks and add Ack for contained Fragment (if any) to AckQueue
101 * \param pkt incoming LpPacket
Eric Newberry32f7eac2020-02-07 14:40:17 -0800102 * \return whether incoming LpPacket is new and not a duplicate
Eric Newberry185ab292017-03-28 06:45:39 +0000103 */
Eric Newberry32f7eac2020-02-07 14:40:17 -0800104 bool
Eric Newberry185ab292017-03-28 06:45:39 +0000105 processIncomingPacket(const lp::Packet& pkt);
106
107 /** \brief called by GenericLinkService to attach Acks onto an outgoing LpPacket
108 * \param pkt outgoing LpPacket to attach Acks to
109 * \param mtu MTU of the Transport
110 */
111 void
112 piggyback(lp::Packet& pkt, ssize_t mtu);
113
Davide Pesavento264af772021-02-09 21:48:24 -0500114NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Eric Newberry7b0071e2017-07-03 17:33:31 +0000115 class UnackedFrag;
116 class NetPkt;
117 using UnackedFrags = std::map<lp::Sequence, UnackedFrag>;
118
Eric Newberry7b0071e2017-07-03 17:33:31 +0000119 /** \brief assign TxSequence number to a fragment
120 * \param frag fragment to assign TxSequence to
121 * \return assigned TxSequence number
122 * \throw std::length_error assigned TxSequence is equal to the start of the existing window
123 */
124 lp::Sequence
125 assignTxSequence(lp::Packet& frag);
126
Eric Newberry185ab292017-03-28 06:45:39 +0000127 /** \brief start the idle Ack timer
128 *
129 * This timer requests an IDLE packet to acknowledge pending fragments not already piggybacked.
130 * It is called regularly on a period configured in Options::idleAckTimerPeriod. This allows Acks
131 * to be returned to the sender, even if the link goes idle.
132 */
133 void
134 startIdleAckTimer();
135
Eric Newberry7b0071e2017-07-03 17:33:31 +0000136 /** \brief find and mark as lost fragments where a configurable number of Acks
137 * (\p m_options.seqNumLossThreshold) have been received for greater TxSequence numbers
138 * \param ackIt iterator pointing to acknowledged fragment
Eric Newberry971d9622018-03-30 23:29:26 -0700139 * \return vector containing TxSequences of fragments marked lost by this mechanism
Eric Newberry185ab292017-03-28 06:45:39 +0000140 */
Eric Newberry971d9622018-03-30 23:29:26 -0700141 std::vector<lp::Sequence>
Eric Newberry7b0071e2017-07-03 17:33:31 +0000142 findLostLpPackets(UnackedFrags::iterator ackIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000143
Eric Newberry7b0071e2017-07-03 17:33:31 +0000144 /** \brief resend (or give up on) a lost fragment
Eric Newberry971d9622018-03-30 23:29:26 -0700145 * \return vector of the TxSequences of fragments removed due to a network packet being removed
Eric Newberry185ab292017-03-28 06:45:39 +0000146 */
Eric Newberry971d9622018-03-30 23:29:26 -0700147 std::vector<lp::Sequence>
Eric Newberry32f7eac2020-02-07 14:40:17 -0800148 onLpPacketLost(lp::Sequence txSeq, bool isTimeout);
Eric Newberry185ab292017-03-28 06:45:39 +0000149
150 /** \brief remove the fragment with the given sequence number from the map of unacknowledged
Eric Newberry7b0071e2017-07-03 17:33:31 +0000151 * fragments, as well as its associated network packet (if any)
152 * \param fragIt iterator to acknowledged fragment
Eric Newberry185ab292017-03-28 06:45:39 +0000153 *
Eric Newberry7b0071e2017-07-03 17:33:31 +0000154 * If the given TxSequence marks the beginning of the send window, the window will be incremented.
Eric Newberry185ab292017-03-28 06:45:39 +0000155 * If the associated network packet has been fully transmitted, it will be removed.
156 */
157 void
Eric Newberry7b0071e2017-07-03 17:33:31 +0000158 onLpPacketAcknowledged(UnackedFrags::iterator fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000159
Eric Newberry7b0071e2017-07-03 17:33:31 +0000160 /** \brief delete a fragment from UnackedFrags and advance acknowledge window if necessary
161 * \param fragIt iterator to an UnackedFrag, must be dereferencable
162 * \post fragIt is not in m_unackedFrags
163 * \post if was equal to m_firstUnackedFrag,
164 * m_firstUnackedFrag is set to the UnackedFrag after fragIt with consideration of
165 * TxSequence number wraparound, or set to m_unackedFrags.end() if m_unackedFrags is empty
166 */
167 void
168 deleteUnackedFrag(UnackedFrags::iterator fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000169
Davide Pesavento264af772021-02-09 21:48:24 -0500170NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400171 /**
172 * \brief Contains a sent fragment that has not been acknowledged and associated data
Eric Newberry185ab292017-03-28 06:45:39 +0000173 */
174 class UnackedFrag
175 {
176 public:
Eric Newberry7b0071e2017-07-03 17:33:31 +0000177 explicit
Eric Newberry185ab292017-03-28 06:45:39 +0000178 UnackedFrag(lp::Packet pkt);
179
180 public:
181 lp::Packet pkt;
182 scheduler::ScopedEventId rtoTimer;
183 time::steady_clock::TimePoint sendTime;
184 size_t retxCount;
185 size_t nGreaterSeqAcks; //!< number of Acks received for sequences greater than this fragment
Eric Newberry7b0071e2017-07-03 17:33:31 +0000186 shared_ptr<NetPkt> netPkt;
Eric Newberry185ab292017-03-28 06:45:39 +0000187 };
188
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400189 /**
190 * \brief Contains a network-layer packet with unacknowledged fragments
Eric Newberry185ab292017-03-28 06:45:39 +0000191 */
192 class NetPkt
193 {
194 public:
Eric Newberry41aba102017-11-01 16:42:13 -0700195 NetPkt(lp::Packet&& pkt, bool isInterest);
196
197 public:
Eric Newberry7b0071e2017-07-03 17:33:31 +0000198 std::vector<UnackedFrags::iterator> unackedFrags;
Eric Newberry41aba102017-11-01 16:42:13 -0700199 lp::Packet pkt;
200 bool isInterest;
201 bool didRetx;
Eric Newberry185ab292017-03-28 06:45:39 +0000202 };
203
Eric Newberry185ab292017-03-28 06:45:39 +0000204 Options m_options;
205 GenericLinkService* m_linkService;
Eric Newberry7b0071e2017-07-03 17:33:31 +0000206 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 Newberry185ab292017-03-28 06:45:39 +0000213 std::queue<lp::Sequence> m_ackQueue;
Eric Newberry32f7eac2020-02-07 14:40:17 -0800214 std::map<lp::Sequence, time::steady_clock::TimePoint> m_recentRecvSeqs;
215 std::queue<lp::Sequence> m_recentRecvSeqsQueue;
Eric Newberry7b0071e2017-07-03 17:33:31 +0000216 lp::Sequence m_lastTxSeqNo;
Eric Newberry185ab292017-03-28 06:45:39 +0000217 scheduler::ScopedEventId m_idleAckTimer;
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400218 ndn::util::RttEstimator m_rttEst;
Eric Newberry185ab292017-03-28 06:45:39 +0000219};
220
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800221std::ostream&
222operator<<(std::ostream& os, const FaceLogHelper<LpReliability>& flh);
223
Eric Newberry185ab292017-03-28 06:45:39 +0000224} // namespace face
225} // namespace nfd
226
227#endif // NFD_DAEMON_FACE_LP_RELIABILITY_HPP