blob: 74782c251e73d6c7f312ad3d1899af58581a7342 [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 Pesavento91c15c82024-01-15 17:14:23 -05003 * Copyright (c) 2014-2024, 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#include "lp-reliability.hpp"
Davide Pesavento9a63bf22023-11-11 17:12:51 -050027#include "common/global.hpp"
Eric Newberry185ab292017-03-28 06:45:39 +000028#include "generic-link-service.hpp"
29#include "transport.hpp"
Davide Pesavento9a63bf22023-11-11 17:12:51 -050030
31#include <ndn-cxx/lp/fields.hpp>
Eric Newberry185ab292017-03-28 06:45:39 +000032
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050033#include <set>
34
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::face {
Eric Newberry185ab292017-03-28 06:45:39 +000036
Eric Newberry5ab4cf82020-02-03 15:40:16 -080037NFD_LOG_INIT(LpReliability);
38
Eric Newberry185ab292017-03-28 06:45:39 +000039LpReliability::LpReliability(const LpReliability::Options& options, GenericLinkService* linkService)
40 : m_options(options)
41 , m_linkService(linkService)
42 , m_firstUnackedFrag(m_unackedFrags.begin())
Eric Newberry7b0071e2017-07-03 17:33:31 +000043 , m_lastTxSeqNo(-1) // set to "-1" to start TxSequence numbers at 0
Eric Newberry185ab292017-03-28 06:45:39 +000044{
45 BOOST_ASSERT(m_linkService != nullptr);
Davide Pesaventoe4b22382018-06-10 14:37:24 -040046 BOOST_ASSERT(m_options.idleAckTimerPeriod > 0_ns);
Eric Newberry185ab292017-03-28 06:45:39 +000047}
48
49void
50LpReliability::setOptions(const Options& options)
51{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040052 BOOST_ASSERT(options.idleAckTimerPeriod > 0_ns);
Eric Newberry185ab292017-03-28 06:45:39 +000053
54 if (m_options.isEnabled && !options.isEnabled) {
Davide Pesaventof190cfa2019-07-17 20:14:11 -040055 m_idleAckTimer.cancel();
Eric Newberry185ab292017-03-28 06:45:39 +000056 }
57
58 m_options = options;
59}
60
Eric Newberry185ab292017-03-28 06:45:39 +000061void
Eric Newberry41aba102017-11-01 16:42:13 -070062LpReliability::handleOutgoing(std::vector<lp::Packet>& frags, lp::Packet&& pkt, bool isInterest)
Eric Newberry185ab292017-03-28 06:45:39 +000063{
64 BOOST_ASSERT(m_options.isEnabled);
65
Eric Newberry185ab292017-03-28 06:45:39 +000066 auto unackedFragsIt = m_unackedFrags.begin();
Eric Newberry7b0071e2017-07-03 17:33:31 +000067 auto sendTime = time::steady_clock::now();
Eric Newberry185ab292017-03-28 06:45:39 +000068
Eric Newberry41aba102017-11-01 16:42:13 -070069 auto netPkt = make_shared<NetPkt>(std::move(pkt), isInterest);
Eric Newberry7b0071e2017-07-03 17:33:31 +000070 netPkt->unackedFrags.reserve(frags.size());
71
72 for (lp::Packet& frag : frags) {
Eric Newberry32f7eac2020-02-07 14:40:17 -080073 // Non-IDLE packets are required to have assigned Sequence numbers with LpReliability enabled
74 BOOST_ASSERT(frag.has<lp::SequenceField>());
75
Eric Newberry7b0071e2017-07-03 17:33:31 +000076 // Assign TxSequence number
77 lp::Sequence txSeq = assignTxSequence(frag);
78
Eric Newberry185ab292017-03-28 06:45:39 +000079 // Store LpPacket for future retransmissions
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040080 unackedFragsIt = m_unackedFrags.try_emplace(unackedFragsIt, txSeq, frag);
Eric Newberry7b0071e2017-07-03 17:33:31 +000081 unackedFragsIt->second.sendTime = sendTime;
Eric Newberry5ab4cf82020-02-03 15:40:16 -080082 auto rto = m_rttEst.getEstimatedRto();
Eric Newberry32f7eac2020-02-07 14:40:17 -080083 lp::Sequence seq = frag.get<lp::SequenceField>();
84 NFD_LOG_FACE_TRACE("transmitting seq=" << seq << ", txseq=" << txSeq << ", rto=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -080085 time::duration_cast<time::milliseconds>(rto).count() << "ms");
86 unackedFragsIt->second.rtoTimer = getScheduler().schedule(rto, [=] {
Eric Newberry32f7eac2020-02-07 14:40:17 -080087 onLpPacketLost(txSeq, true);
Eric Newberry5ab4cf82020-02-03 15:40:16 -080088 });
Eric Newberry7b0071e2017-07-03 17:33:31 +000089 unackedFragsIt->second.netPkt = netPkt;
90
Eric Newberry185ab292017-03-28 06:45:39 +000091 if (m_unackedFrags.size() == 1) {
Eric Newberry7b0071e2017-07-03 17:33:31 +000092 m_firstUnackedFrag = m_unackedFrags.begin();
Eric Newberry185ab292017-03-28 06:45:39 +000093 }
Eric Newberry7b0071e2017-07-03 17:33:31 +000094
95 // Add to associated NetPkt
96 netPkt->unackedFrags.push_back(unackedFragsIt);
Eric Newberry185ab292017-03-28 06:45:39 +000097 }
98}
99
Eric Newberry32f7eac2020-02-07 14:40:17 -0800100bool
Eric Newberry185ab292017-03-28 06:45:39 +0000101LpReliability::processIncomingPacket(const lp::Packet& pkt)
102{
103 BOOST_ASSERT(m_options.isEnabled);
104
Eric Newberry32f7eac2020-02-07 14:40:17 -0800105 bool isDuplicate = false;
Eric Newberry185ab292017-03-28 06:45:39 +0000106 auto now = time::steady_clock::now();
107
108 // Extract and parse Acks
Eric Newberry32f7eac2020-02-07 14:40:17 -0800109 for (lp::Sequence ackTxSeq : pkt.list<lp::AckField>()) {
110 auto fragIt = m_unackedFrags.find(ackTxSeq);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000111 if (fragIt == m_unackedFrags.end()) {
112 // Ignore an Ack for an unknown TxSequence number
Eric Newberry32f7eac2020-02-07 14:40:17 -0800113 NFD_LOG_FACE_DEBUG("received ack for unknown txseq=" << ackTxSeq);
Eric Newberry185ab292017-03-28 06:45:39 +0000114 continue;
115 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000116 auto& frag = fragIt->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000117
118 // Cancel the RTO timer for the acknowledged fragment
Eric Newberry7b0071e2017-07-03 17:33:31 +0000119 frag.rtoTimer.cancel();
Eric Newberry185ab292017-03-28 06:45:39 +0000120
Eric Newberry7b0071e2017-07-03 17:33:31 +0000121 if (frag.retxCount == 0) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800122 NFD_LOG_FACE_TRACE("received ack for seq=" << frag.pkt.get<lp::SequenceField>() << ", txseq=" <<
123 ackTxSeq << ", retx=0, rtt=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800124 time::duration_cast<time::milliseconds>(now - frag.sendTime).count() << "ms");
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400125 // This sequence had no retransmissions, so use it to estimate the RTO
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400126 m_rttEst.addMeasurement(now - frag.sendTime);
Eric Newberry185ab292017-03-28 06:45:39 +0000127 }
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800128 else {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800129 NFD_LOG_FACE_TRACE("received ack for seq=" << frag.pkt.get<lp::SequenceField>() << ", txseq=" <<
130 ackTxSeq << ", retx=" << frag.retxCount);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800131 }
Eric Newberry185ab292017-03-28 06:45:39 +0000132
Eric Newberry32f7eac2020-02-07 14:40:17 -0800133 // Look for frags with TxSequence numbers < ackTxSeq (allowing for wraparound) and consider
134 // them lost if a configurable number of Acks containing greater TxSequence numbers have been
Eric Newberry7b0071e2017-07-03 17:33:31 +0000135 // received.
136 auto lostLpPackets = findLostLpPackets(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000137
Eric Newberry7b0071e2017-07-03 17:33:31 +0000138 // Remove the fragment from the map of unacknowledged fragments and from its associated network
139 // packet. Potentially increment the start of the window.
140 onLpPacketAcknowledged(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000141
Eric Newberry971d9622018-03-30 23:29:26 -0700142 // This set contains TxSequences that have been removed by onLpPacketLost below because they
143 // were part of a network packet that was removed due to a fragment exceeding retx, as well as
144 // any other TxSequences removed by onLpPacketLost. This prevents onLpPacketLost from being
145 // called later for an invalid iterator.
146 std::set<lp::Sequence> removedLpPackets;
147
Eric Newberry7b0071e2017-07-03 17:33:31 +0000148 // Resend or fail fragments considered lost. Potentially increment the start of the window.
Eric Newberry971d9622018-03-30 23:29:26 -0700149 for (lp::Sequence txSeq : lostLpPackets) {
150 if (removedLpPackets.find(txSeq) == removedLpPackets.end()) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800151 auto removedTxSeqs = onLpPacketLost(txSeq, false);
152 for (auto removedTxSeq : removedTxSeqs) {
Eric Newberry971d9622018-03-30 23:29:26 -0700153 removedLpPackets.insert(removedTxSeq);
154 }
155 }
Eric Newberry185ab292017-03-28 06:45:39 +0000156 }
157 }
158
Eric Newberry7b0071e2017-07-03 17:33:31 +0000159 // If packet has Fragment and TxSequence fields, extract TxSequence and add to AckQueue
160 if (pkt.has<lp::FragmentField>() && pkt.has<lp::TxSequenceField>()) {
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800161 NFD_LOG_FACE_TRACE("queueing ack for remote txseq=" << pkt.get<lp::TxSequenceField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000162 m_ackQueue.push(pkt.get<lp::TxSequenceField>());
Eric Newberry32f7eac2020-02-07 14:40:17 -0800163
164 // Check for received frames with duplicate Sequences
165 if (pkt.has<lp::SequenceField>()) {
166 lp::Sequence pktSequence = pkt.get<lp::SequenceField>();
167 isDuplicate = m_recentRecvSeqs.count(pktSequence) > 0;
168 // Check for recent received Sequences to remove
169 auto now = time::steady_clock::now();
170 auto rto = m_rttEst.getEstimatedRto();
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500171 while (!m_recentRecvSeqsQueue.empty() &&
Eric Newberry32f7eac2020-02-07 14:40:17 -0800172 now > m_recentRecvSeqs[m_recentRecvSeqsQueue.front()] + rto) {
173 m_recentRecvSeqs.erase(m_recentRecvSeqsQueue.front());
174 m_recentRecvSeqsQueue.pop();
175 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400176 m_recentRecvSeqs.try_emplace(pktSequence, now);
Eric Newberry32f7eac2020-02-07 14:40:17 -0800177 m_recentRecvSeqsQueue.push(pktSequence);
178 }
179
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400180 startIdleAckTimer();
Eric Newberry185ab292017-03-28 06:45:39 +0000181 }
Eric Newberry32f7eac2020-02-07 14:40:17 -0800182
183 return !isDuplicate;
Eric Newberry185ab292017-03-28 06:45:39 +0000184}
185
186void
187LpReliability::piggyback(lp::Packet& pkt, ssize_t mtu)
188{
189 BOOST_ASSERT(m_options.isEnabled);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000190 BOOST_ASSERT(pkt.wireEncode().type() == lp::tlv::LpPacket);
Eric Newberry185ab292017-03-28 06:45:39 +0000191
Eric Newberry7b0071e2017-07-03 17:33:31 +0000192 // up to 2 extra octets reserved for potential TLV-LENGTH size increases
193 ssize_t pktSize = pkt.wireEncode().size();
194 ssize_t reservedSpace = tlv::sizeOfVarNumber(ndn::MAX_NDN_PACKET_SIZE) -
195 tlv::sizeOfVarNumber(pktSize);
196 ssize_t remainingSpace = (mtu == MTU_UNLIMITED ? ndn::MAX_NDN_PACKET_SIZE : mtu) - reservedSpace;
197 remainingSpace -= pktSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000198
Eric Newberry7b0071e2017-07-03 17:33:31 +0000199 while (!m_ackQueue.empty()) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800200 lp::Sequence ackTxSeq = m_ackQueue.front();
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400201 // Ack size = Ack TLV-TYPE (3 octets) + TLV-LENGTH (1 octet) + lp::Sequence (8 octets)
Junxiao Shi21e01932018-04-21 10:39:05 +0000202 const ssize_t ackSize = tlv::sizeOfVarNumber(lp::tlv::Ack) +
203 tlv::sizeOfVarNumber(sizeof(lp::Sequence)) +
204 sizeof(lp::Sequence);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000205
206 if (ackSize > remainingSpace) {
207 break;
208 }
209
Eric Newberry32f7eac2020-02-07 14:40:17 -0800210 NFD_LOG_FACE_TRACE("piggybacking ack for remote txseq=" << ackTxSeq);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800211
Eric Newberry32f7eac2020-02-07 14:40:17 -0800212 pkt.add<lp::AckField>(ackTxSeq);
Eric Newberry185ab292017-03-28 06:45:39 +0000213 m_ackQueue.pop();
Eric Newberry7b0071e2017-07-03 17:33:31 +0000214 remainingSpace -= ackSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000215 }
216}
217
Eric Newberry7b0071e2017-07-03 17:33:31 +0000218lp::Sequence
219LpReliability::assignTxSequence(lp::Packet& frag)
220{
221 lp::Sequence txSeq = ++m_lastTxSeqNo;
222 frag.set<lp::TxSequenceField>(txSeq);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500223 if (!m_unackedFrags.empty() && m_lastTxSeqNo == m_firstUnackedFrag->first) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500224 NDN_THROW(std::length_error("TxSequence range exceeded"));
Eric Newberry7b0071e2017-07-03 17:33:31 +0000225 }
226 return m_lastTxSeqNo;
227}
228
Eric Newberry185ab292017-03-28 06:45:39 +0000229void
230LpReliability::startIdleAckTimer()
231{
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400232 if (m_idleAckTimer) {
233 // timer is already running, do nothing
234 return;
235 }
Eric Newberry185ab292017-03-28 06:45:39 +0000236
Davide Pesavento3dade002019-03-19 11:29:56 -0600237 m_idleAckTimer = getScheduler().schedule(m_options.idleAckTimerPeriod, [this] {
Eric Newberry185ab292017-03-28 06:45:39 +0000238 while (!m_ackQueue.empty()) {
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700239 m_linkService->requestIdlePacket();
Eric Newberry185ab292017-03-28 06:45:39 +0000240 }
Eric Newberry185ab292017-03-28 06:45:39 +0000241 });
242}
243
Eric Newberry971d9622018-03-30 23:29:26 -0700244std::vector<lp::Sequence>
Eric Newberry7b0071e2017-07-03 17:33:31 +0000245LpReliability::findLostLpPackets(LpReliability::UnackedFrags::iterator ackIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000246{
Eric Newberry971d9622018-03-30 23:29:26 -0700247 std::vector<lp::Sequence> lostLpPackets;
Eric Newberry185ab292017-03-28 06:45:39 +0000248
249 for (auto it = m_firstUnackedFrag; ; ++it) {
250 if (it == m_unackedFrags.end()) {
251 it = m_unackedFrags.begin();
252 }
253
Eric Newberry7b0071e2017-07-03 17:33:31 +0000254 if (it->first == ackIt->first) {
Eric Newberry185ab292017-03-28 06:45:39 +0000255 break;
256 }
257
258 auto& unackedFrag = it->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000259 unackedFrag.nGreaterSeqAcks++;
Eric Newberry32f7eac2020-02-07 14:40:17 -0800260 NFD_LOG_FACE_TRACE("received ack=" << ackIt->first << " before=" << it->first <<
261 ", before count=" << unackedFrag.nGreaterSeqAcks);
Eric Newberry185ab292017-03-28 06:45:39 +0000262
Eric Newberry7b0071e2017-07-03 17:33:31 +0000263 if (unackedFrag.nGreaterSeqAcks >= m_options.seqNumLossThreshold) {
Eric Newberry971d9622018-03-30 23:29:26 -0700264 lostLpPackets.push_back(it->first);
Eric Newberry185ab292017-03-28 06:45:39 +0000265 }
266 }
267
268 return lostLpPackets;
269}
270
Eric Newberry971d9622018-03-30 23:29:26 -0700271std::vector<lp::Sequence>
Eric Newberry32f7eac2020-02-07 14:40:17 -0800272LpReliability::onLpPacketLost(lp::Sequence txSeq, bool isTimeout)
Eric Newberry185ab292017-03-28 06:45:39 +0000273{
Eric Newberry971d9622018-03-30 23:29:26 -0700274 BOOST_ASSERT(m_unackedFrags.count(txSeq) > 0);
275 auto txSeqIt = m_unackedFrags.find(txSeq);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000276
277 auto& txFrag = txSeqIt->second;
278 txFrag.rtoTimer.cancel();
279 auto netPkt = txFrag.netPkt;
Eric Newberry971d9622018-03-30 23:29:26 -0700280 std::vector<lp::Sequence> removedThisTxSeq;
Eric Newberry32f7eac2020-02-07 14:40:17 -0800281 lp::Sequence seq = txFrag.pkt.get<lp::SequenceField>();
282
283 if (isTimeout) {
284 NFD_LOG_FACE_TRACE("rto timer expired for seq=" << seq << ", txseq=" << txSeq);
285 }
286 else { // lost due to out-of-order TxSeqs
287 NFD_LOG_FACE_TRACE("seq=" << seq << ", txseq=" << txSeq <<
288 " considered lost from acks for more recent txseqs");
289 }
Eric Newberry185ab292017-03-28 06:45:39 +0000290
291 // Check if maximum number of retransmissions exceeded
292 if (txFrag.retxCount >= m_options.maxRetx) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800293 NFD_LOG_FACE_DEBUG("seq=" << seq << " exceeded allowed retransmissions: DROP");
Eric Newberry7b0071e2017-07-03 17:33:31 +0000294 // Delete all LpPackets of NetPkt from m_unackedFrags (except this one)
295 for (size_t i = 0; i < netPkt->unackedFrags.size(); i++) {
296 if (netPkt->unackedFrags[i] != txSeqIt) {
Eric Newberry971d9622018-03-30 23:29:26 -0700297 removedThisTxSeq.push_back(netPkt->unackedFrags[i]->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000298 deleteUnackedFrag(netPkt->unackedFrags[i]);
299 }
Eric Newberry185ab292017-03-28 06:45:39 +0000300 }
Eric Newberry185ab292017-03-28 06:45:39 +0000301
302 ++m_linkService->nRetxExhausted;
Eric Newberry41aba102017-11-01 16:42:13 -0700303
304 // Notify strategy of dropped Interest (if any)
305 if (netPkt->isInterest) {
306 BOOST_ASSERT(netPkt->pkt.has<lp::FragmentField>());
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500307 auto frag = netPkt->pkt.get<lp::FragmentField>();
308 onDroppedInterest(Interest(Block({frag.first, frag.second})));
Eric Newberry41aba102017-11-01 16:42:13 -0700309 }
310
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800311 // Delete this LpPacket from m_unackedFrags
Eric Newberry971d9622018-03-30 23:29:26 -0700312 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000313 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000314 }
315 else {
Eric Newberry7b0071e2017-07-03 17:33:31 +0000316 // Assign new TxSequence
317 lp::Sequence newTxSeq = assignTxSequence(txFrag.pkt);
Eric Newberry00d39fd2017-12-10 14:26:45 -0700318 netPkt->didRetx = true;
Eric Newberry185ab292017-03-28 06:45:39 +0000319
Eric Newberry7b0071e2017-07-03 17:33:31 +0000320 // Move fragment to new TxSequence mapping
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400321 auto hint = m_firstUnackedFrag != m_unackedFrags.end() && m_firstUnackedFrag->first > newTxSeq
322 ? m_firstUnackedFrag
323 : m_unackedFrags.end();
324 auto newTxFragIt = m_unackedFrags.try_emplace(hint, newTxSeq, txFrag.pkt);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000325 auto& newTxFrag = newTxFragIt->second;
326 newTxFrag.retxCount = txFrag.retxCount + 1;
327 newTxFrag.netPkt = netPkt;
328
329 // Update associated NetPkt
330 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), txSeqIt);
331 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
332 *fragInNetPkt = newTxFragIt;
333
Eric Newberry971d9622018-03-30 23:29:26 -0700334 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000335 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000336
337 // Retransmit fragment
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700338 m_linkService->sendLpPacket(lp::Packet(newTxFrag.pkt));
Eric Newberry7b0071e2017-07-03 17:33:31 +0000339
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800340 auto rto = m_rttEst.getEstimatedRto();
Eric Newberry32f7eac2020-02-07 14:40:17 -0800341 NFD_LOG_FACE_TRACE("retransmitting seq=" << seq << ", txseq=" << newTxSeq << ", retx=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800342 txFrag.retxCount << ", rto=" <<
343 time::duration_cast<time::milliseconds>(rto).count() << "ms");
344
Eric Newberry7b0071e2017-07-03 17:33:31 +0000345 // Start RTO timer for this sequence
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800346 newTxFrag.rtoTimer = getScheduler().schedule(rto, [=] {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800347 onLpPacketLost(newTxSeq, true);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800348 });
Eric Newberry185ab292017-03-28 06:45:39 +0000349 }
Eric Newberry971d9622018-03-30 23:29:26 -0700350
351 return removedThisTxSeq;
Eric Newberry185ab292017-03-28 06:45:39 +0000352}
353
354void
Eric Newberry7b0071e2017-07-03 17:33:31 +0000355LpReliability::onLpPacketAcknowledged(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000356{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000357 auto netPkt = fragIt->second.netPkt;
Eric Newberry185ab292017-03-28 06:45:39 +0000358
Eric Newberry7b0071e2017-07-03 17:33:31 +0000359 // Remove from NetPkt unacked fragment list
360 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), fragIt);
361 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
362 *fragInNetPkt = netPkt->unackedFrags.back();
363 netPkt->unackedFrags.pop_back();
Eric Newberry185ab292017-03-28 06:45:39 +0000364
Eric Newberry7b0071e2017-07-03 17:33:31 +0000365 // Check if network-layer packet completely received. If so, increment counters
366 if (netPkt->unackedFrags.empty()) {
367 if (netPkt->didRetx) {
Eric Newberry185ab292017-03-28 06:45:39 +0000368 ++m_linkService->nRetransmitted;
369 }
370 else {
371 ++m_linkService->nAcknowledged;
372 }
Eric Newberry185ab292017-03-28 06:45:39 +0000373 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000374
375 deleteUnackedFrag(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000376}
377
Eric Newberry7b0071e2017-07-03 17:33:31 +0000378void
379LpReliability::deleteUnackedFrag(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000380{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000381 lp::Sequence firstUnackedTxSeq = m_firstUnackedFrag->first;
382 lp::Sequence currentTxSeq = fragIt->first;
383 auto nextFragIt = m_unackedFrags.erase(fragIt);
384
385 if (!m_unackedFrags.empty() && firstUnackedTxSeq == currentTxSeq) {
386 // If "first" fragment in send window (allowing for wraparound), increment window begin
387 if (nextFragIt == m_unackedFrags.end()) {
388 m_firstUnackedFrag = m_unackedFrags.begin();
389 }
390 else {
391 m_firstUnackedFrag = nextFragIt;
392 }
Eric Newberry185ab292017-03-28 06:45:39 +0000393 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000394 else if (m_unackedFrags.empty()) {
395 m_firstUnackedFrag = m_unackedFrags.end();
396 }
Eric Newberry185ab292017-03-28 06:45:39 +0000397}
398
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800399std::ostream&
400operator<<(std::ostream& os, const FaceLogHelper<LpReliability>& flh)
401{
402 if (flh.obj.getLinkService() == nullptr) {
403 os << "[id=0,local=unknown,remote=unknown] ";
404 }
405 else {
406 os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
407 }
408 return os;
409}
410
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400411} // namespace nfd::face