blob: 50187308be2dc5d0fdee32292ba405a8509c41cf [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 Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, 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 Newberry185ab292017-03-28 06:45:39 +000029#include "core/rtt-estimator.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>
33
34#include <queue>
35
36namespace nfd {
37namespace face {
38
39class GenericLinkService;
40
41/** \brief provides for reliable sending and receiving of link-layer packets
42 * \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2
43 */
44class LpReliability : noncopyable
45{
46public:
47 struct Options
48 {
49 /** \brief enables link-layer reliability
50 */
51 bool isEnabled = false;
52
53 /** \brief maximum number of retransmissions for an LpPacket
54 */
55 size_t maxRetx = 3;
56
57 /** \brief period between sending pending Acks in an IDLE packet
58 */
Davide Pesaventoe4b22382018-06-10 14:37:24 -040059 time::nanoseconds idleAckTimerPeriod = 5_ms;
Eric Newberry185ab292017-03-28 06:45:39 +000060
61 /** \brief a fragment is considered lost if this number of fragments with greater sequence
62 * numbers are acknowledged
63 */
64 size_t seqNumLossThreshold = 3;
65 };
66
67 LpReliability(const Options& options, GenericLinkService* linkService);
68
Eric Newberry41aba102017-11-01 16:42:13 -070069 /** \brief signals on Interest dropped by reliability system for exceeding allowed number of retx
70 */
71 signal::Signal<LpReliability, Interest> onDroppedInterest;
72
Eric Newberry185ab292017-03-28 06:45:39 +000073 /** \brief set options for reliability
74 */
75 void
76 setOptions(const Options& options);
77
78 /** \return GenericLinkService that owns this instance
79 *
80 * This is only used for logging, and may be nullptr.
81 */
82 const GenericLinkService*
83 getLinkService() const;
84
Eric Newberry7b0071e2017-07-03 17:33:31 +000085 /** \brief observe outgoing fragment(s) of a network packet and store for potential retransmission
Eric Newberry185ab292017-03-28 06:45:39 +000086 * \param frags fragments of network packet
Eric Newberry41aba102017-11-01 16:42:13 -070087 * \param pkt encapsulated network packet
88 * \param isInterest whether the network packet is an Interest
Eric Newberry185ab292017-03-28 06:45:39 +000089 */
90 void
Eric Newberry41aba102017-11-01 16:42:13 -070091 handleOutgoing(std::vector<lp::Packet>& frags, lp::Packet&& pkt, bool isInterest);
Eric Newberry185ab292017-03-28 06:45:39 +000092
93 /** \brief extract and parse all Acks and add Ack for contained Fragment (if any) to AckQueue
94 * \param pkt incoming LpPacket
95 */
96 void
97 processIncomingPacket(const lp::Packet& pkt);
98
99 /** \brief called by GenericLinkService to attach Acks onto an outgoing LpPacket
100 * \param pkt outgoing LpPacket to attach Acks to
101 * \param mtu MTU of the Transport
102 */
103 void
104 piggyback(lp::Packet& pkt, ssize_t mtu);
105
106PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Eric Newberry7b0071e2017-07-03 17:33:31 +0000107 class UnackedFrag;
108 class NetPkt;
109 using UnackedFrags = std::map<lp::Sequence, UnackedFrag>;
110
111PUBLIC_WITH_TESTS_ELSE_PRIVATE:
112 /** \brief assign TxSequence number to a fragment
113 * \param frag fragment to assign TxSequence to
114 * \return assigned TxSequence number
115 * \throw std::length_error assigned TxSequence is equal to the start of the existing window
116 */
117 lp::Sequence
118 assignTxSequence(lp::Packet& frag);
119
Eric Newberry185ab292017-03-28 06:45:39 +0000120 /** \brief start the idle Ack timer
121 *
122 * This timer requests an IDLE packet to acknowledge pending fragments not already piggybacked.
123 * It is called regularly on a period configured in Options::idleAckTimerPeriod. This allows Acks
124 * to be returned to the sender, even if the link goes idle.
125 */
126 void
127 startIdleAckTimer();
128
129 /** \brief cancel the idle Ack timer
130 */
131 void
132 stopIdleAckTimer();
133
Eric Newberry7b0071e2017-07-03 17:33:31 +0000134 /** \brief find and mark as lost fragments where a configurable number of Acks
135 * (\p m_options.seqNumLossThreshold) have been received for greater TxSequence numbers
136 * \param ackIt iterator pointing to acknowledged fragment
Eric Newberry971d9622018-03-30 23:29:26 -0700137 * \return vector containing TxSequences of fragments marked lost by this mechanism
Eric Newberry185ab292017-03-28 06:45:39 +0000138 */
Eric Newberry971d9622018-03-30 23:29:26 -0700139 std::vector<lp::Sequence>
Eric Newberry7b0071e2017-07-03 17:33:31 +0000140 findLostLpPackets(UnackedFrags::iterator ackIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000141
Eric Newberry7b0071e2017-07-03 17:33:31 +0000142 /** \brief resend (or give up on) a lost fragment
Eric Newberry971d9622018-03-30 23:29:26 -0700143 * \return vector of the TxSequences of fragments removed due to a network packet being removed
Eric Newberry185ab292017-03-28 06:45:39 +0000144 */
Eric Newberry971d9622018-03-30 23:29:26 -0700145 std::vector<lp::Sequence>
146 onLpPacketLost(lp::Sequence txSeq);
Eric Newberry185ab292017-03-28 06:45:39 +0000147
148 /** \brief remove the fragment with the given sequence number from the map of unacknowledged
Eric Newberry7b0071e2017-07-03 17:33:31 +0000149 * fragments, as well as its associated network packet (if any)
150 * \param fragIt iterator to acknowledged fragment
Eric Newberry185ab292017-03-28 06:45:39 +0000151 *
Eric Newberry7b0071e2017-07-03 17:33:31 +0000152 * If the given TxSequence marks the beginning of the send window, the window will be incremented.
Eric Newberry185ab292017-03-28 06:45:39 +0000153 * If the associated network packet has been fully transmitted, it will be removed.
154 */
155 void
Eric Newberry7b0071e2017-07-03 17:33:31 +0000156 onLpPacketAcknowledged(UnackedFrags::iterator fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000157
Eric Newberry7b0071e2017-07-03 17:33:31 +0000158 /** \brief delete a fragment from UnackedFrags and advance acknowledge window if necessary
159 * \param fragIt iterator to an UnackedFrag, must be dereferencable
160 * \post fragIt is not in m_unackedFrags
161 * \post if was equal to m_firstUnackedFrag,
162 * m_firstUnackedFrag is set to the UnackedFrag after fragIt with consideration of
163 * TxSequence number wraparound, or set to m_unackedFrags.end() if m_unackedFrags is empty
164 */
165 void
166 deleteUnackedFrag(UnackedFrags::iterator fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000167
Eric Newberry7b0071e2017-07-03 17:33:31 +0000168PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Eric Newberry185ab292017-03-28 06:45:39 +0000169 /** \brief contains a sent fragment that has not been acknowledged and associated data
170 */
171 class UnackedFrag
172 {
173 public:
Eric Newberry7b0071e2017-07-03 17:33:31 +0000174 explicit
Eric Newberry185ab292017-03-28 06:45:39 +0000175 UnackedFrag(lp::Packet pkt);
176
177 public:
178 lp::Packet pkt;
179 scheduler::ScopedEventId rtoTimer;
180 time::steady_clock::TimePoint sendTime;
181 size_t retxCount;
182 size_t nGreaterSeqAcks; //!< number of Acks received for sequences greater than this fragment
Eric Newberry7b0071e2017-07-03 17:33:31 +0000183 shared_ptr<NetPkt> netPkt;
Eric Newberry185ab292017-03-28 06:45:39 +0000184 };
185
186 /** \brief contains a network-layer packet with unacknowledged fragments
187 */
188 class NetPkt
189 {
190 public:
Eric Newberry41aba102017-11-01 16:42:13 -0700191 NetPkt(lp::Packet&& pkt, bool isInterest);
192
193 public:
Eric Newberry7b0071e2017-07-03 17:33:31 +0000194 std::vector<UnackedFrags::iterator> unackedFrags;
Eric Newberry41aba102017-11-01 16:42:13 -0700195 lp::Packet pkt;
196 bool isInterest;
197 bool didRetx;
Eric Newberry185ab292017-03-28 06:45:39 +0000198 };
199
Eric Newberry7b0071e2017-07-03 17:33:31 +0000200public:
201 /// TxSequence TLV-TYPE (3 octets) + TxSequence TLV-LENGTH (1 octet) + sizeof(lp::Sequence)
202 static constexpr size_t RESERVED_HEADER_SPACE = 3 + 1 + sizeof(lp::Sequence);
203
Eric Newberry185ab292017-03-28 06:45:39 +0000204PUBLIC_WITH_TESTS_ELSE_PRIVATE:
205 Options m_options;
206 GenericLinkService* m_linkService;
Eric Newberry7b0071e2017-07-03 17:33:31 +0000207 UnackedFrags m_unackedFrags;
208 /** An iterator that points to the first unacknowledged fragment in the current window. The window
209 * can wrap around so that the beginning of the window is at a TxSequence greater than other
210 * fragments in the window. When the window is moved past the last item in the iterator, the
211 * first fragment in the map will become the start of the window.
212 */
213 UnackedFrags::iterator m_firstUnackedFrag;
Eric Newberry185ab292017-03-28 06:45:39 +0000214 std::queue<lp::Sequence> m_ackQueue;
Eric Newberry7b0071e2017-07-03 17:33:31 +0000215 lp::Sequence m_lastTxSeqNo;
Eric Newberry185ab292017-03-28 06:45:39 +0000216 scheduler::ScopedEventId m_idleAckTimer;
217 bool m_isIdleAckTimerRunning;
218 RttEstimator m_rto;
219};
220
221} // namespace face
222} // namespace nfd
223
224#endif // NFD_DAEMON_FACE_LP_RELIABILITY_HPP