blob: 4019902ea966c0366f72f37107c0b9f280e002e5 [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 Pesavento9a63bf22023-11-11 17:12:51 -05003 * Copyright (c) 2014-2023, 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 Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::face {
Eric Newberry185ab292017-03-28 06:45:39 +000034
Eric Newberry5ab4cf82020-02-03 15:40:16 -080035NFD_LOG_INIT(LpReliability);
36
Eric Newberry185ab292017-03-28 06:45:39 +000037LpReliability::LpReliability(const LpReliability::Options& options, GenericLinkService* linkService)
38 : m_options(options)
39 , m_linkService(linkService)
40 , m_firstUnackedFrag(m_unackedFrags.begin())
Eric Newberry7b0071e2017-07-03 17:33:31 +000041 , m_lastTxSeqNo(-1) // set to "-1" to start TxSequence numbers at 0
Eric Newberry185ab292017-03-28 06:45:39 +000042{
43 BOOST_ASSERT(m_linkService != nullptr);
Davide Pesaventoe4b22382018-06-10 14:37:24 -040044 BOOST_ASSERT(m_options.idleAckTimerPeriod > 0_ns);
Eric Newberry185ab292017-03-28 06:45:39 +000045}
46
47void
48LpReliability::setOptions(const Options& options)
49{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040050 BOOST_ASSERT(options.idleAckTimerPeriod > 0_ns);
Eric Newberry185ab292017-03-28 06:45:39 +000051
52 if (m_options.isEnabled && !options.isEnabled) {
Davide Pesaventof190cfa2019-07-17 20:14:11 -040053 m_idleAckTimer.cancel();
Eric Newberry185ab292017-03-28 06:45:39 +000054 }
55
56 m_options = options;
57}
58
59const GenericLinkService*
60LpReliability::getLinkService() const
61{
62 return m_linkService;
63}
64
65void
Eric Newberry41aba102017-11-01 16:42:13 -070066LpReliability::handleOutgoing(std::vector<lp::Packet>& frags, lp::Packet&& pkt, bool isInterest)
Eric Newberry185ab292017-03-28 06:45:39 +000067{
68 BOOST_ASSERT(m_options.isEnabled);
69
Eric Newberry185ab292017-03-28 06:45:39 +000070 auto unackedFragsIt = m_unackedFrags.begin();
Eric Newberry7b0071e2017-07-03 17:33:31 +000071 auto sendTime = time::steady_clock::now();
Eric Newberry185ab292017-03-28 06:45:39 +000072
Eric Newberry41aba102017-11-01 16:42:13 -070073 auto netPkt = make_shared<NetPkt>(std::move(pkt), isInterest);
Eric Newberry7b0071e2017-07-03 17:33:31 +000074 netPkt->unackedFrags.reserve(frags.size());
75
76 for (lp::Packet& frag : frags) {
Eric Newberry32f7eac2020-02-07 14:40:17 -080077 // Non-IDLE packets are required to have assigned Sequence numbers with LpReliability enabled
78 BOOST_ASSERT(frag.has<lp::SequenceField>());
79
Eric Newberry7b0071e2017-07-03 17:33:31 +000080 // Assign TxSequence number
81 lp::Sequence txSeq = assignTxSequence(frag);
82
Eric Newberry185ab292017-03-28 06:45:39 +000083 // Store LpPacket for future retransmissions
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040084 unackedFragsIt = m_unackedFrags.try_emplace(unackedFragsIt, txSeq, frag);
Eric Newberry7b0071e2017-07-03 17:33:31 +000085 unackedFragsIt->second.sendTime = sendTime;
Eric Newberry5ab4cf82020-02-03 15:40:16 -080086 auto rto = m_rttEst.getEstimatedRto();
Eric Newberry32f7eac2020-02-07 14:40:17 -080087 lp::Sequence seq = frag.get<lp::SequenceField>();
88 NFD_LOG_FACE_TRACE("transmitting seq=" << seq << ", txseq=" << txSeq << ", rto=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -080089 time::duration_cast<time::milliseconds>(rto).count() << "ms");
90 unackedFragsIt->second.rtoTimer = getScheduler().schedule(rto, [=] {
Eric Newberry32f7eac2020-02-07 14:40:17 -080091 onLpPacketLost(txSeq, true);
Eric Newberry5ab4cf82020-02-03 15:40:16 -080092 });
Eric Newberry7b0071e2017-07-03 17:33:31 +000093 unackedFragsIt->second.netPkt = netPkt;
94
Eric Newberry185ab292017-03-28 06:45:39 +000095 if (m_unackedFrags.size() == 1) {
Eric Newberry7b0071e2017-07-03 17:33:31 +000096 m_firstUnackedFrag = m_unackedFrags.begin();
Eric Newberry185ab292017-03-28 06:45:39 +000097 }
Eric Newberry7b0071e2017-07-03 17:33:31 +000098
99 // Add to associated NetPkt
100 netPkt->unackedFrags.push_back(unackedFragsIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000101 }
102}
103
Eric Newberry32f7eac2020-02-07 14:40:17 -0800104bool
Eric Newberry185ab292017-03-28 06:45:39 +0000105LpReliability::processIncomingPacket(const lp::Packet& pkt)
106{
107 BOOST_ASSERT(m_options.isEnabled);
108
Eric Newberry32f7eac2020-02-07 14:40:17 -0800109 bool isDuplicate = false;
Eric Newberry185ab292017-03-28 06:45:39 +0000110 auto now = time::steady_clock::now();
111
112 // Extract and parse Acks
Eric Newberry32f7eac2020-02-07 14:40:17 -0800113 for (lp::Sequence ackTxSeq : pkt.list<lp::AckField>()) {
114 auto fragIt = m_unackedFrags.find(ackTxSeq);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000115 if (fragIt == m_unackedFrags.end()) {
116 // Ignore an Ack for an unknown TxSequence number
Eric Newberry32f7eac2020-02-07 14:40:17 -0800117 NFD_LOG_FACE_DEBUG("received ack for unknown txseq=" << ackTxSeq);
Eric Newberry185ab292017-03-28 06:45:39 +0000118 continue;
119 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000120 auto& frag = fragIt->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000121
122 // Cancel the RTO timer for the acknowledged fragment
Eric Newberry7b0071e2017-07-03 17:33:31 +0000123 frag.rtoTimer.cancel();
Eric Newberry185ab292017-03-28 06:45:39 +0000124
Eric Newberry7b0071e2017-07-03 17:33:31 +0000125 if (frag.retxCount == 0) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800126 NFD_LOG_FACE_TRACE("received ack for seq=" << frag.pkt.get<lp::SequenceField>() << ", txseq=" <<
127 ackTxSeq << ", retx=0, rtt=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800128 time::duration_cast<time::milliseconds>(now - frag.sendTime).count() << "ms");
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400129 // This sequence had no retransmissions, so use it to estimate the RTO
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400130 m_rttEst.addMeasurement(now - frag.sendTime);
Eric Newberry185ab292017-03-28 06:45:39 +0000131 }
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800132 else {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800133 NFD_LOG_FACE_TRACE("received ack for seq=" << frag.pkt.get<lp::SequenceField>() << ", txseq=" <<
134 ackTxSeq << ", retx=" << frag.retxCount);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800135 }
Eric Newberry185ab292017-03-28 06:45:39 +0000136
Eric Newberry32f7eac2020-02-07 14:40:17 -0800137 // Look for frags with TxSequence numbers < ackTxSeq (allowing for wraparound) and consider
138 // them lost if a configurable number of Acks containing greater TxSequence numbers have been
Eric Newberry7b0071e2017-07-03 17:33:31 +0000139 // received.
140 auto lostLpPackets = findLostLpPackets(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000141
Eric Newberry7b0071e2017-07-03 17:33:31 +0000142 // Remove the fragment from the map of unacknowledged fragments and from its associated network
143 // packet. Potentially increment the start of the window.
144 onLpPacketAcknowledged(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000145
Eric Newberry971d9622018-03-30 23:29:26 -0700146 // This set contains TxSequences that have been removed by onLpPacketLost below because they
147 // were part of a network packet that was removed due to a fragment exceeding retx, as well as
148 // any other TxSequences removed by onLpPacketLost. This prevents onLpPacketLost from being
149 // called later for an invalid iterator.
150 std::set<lp::Sequence> removedLpPackets;
151
Eric Newberry7b0071e2017-07-03 17:33:31 +0000152 // Resend or fail fragments considered lost. Potentially increment the start of the window.
Eric Newberry971d9622018-03-30 23:29:26 -0700153 for (lp::Sequence txSeq : lostLpPackets) {
154 if (removedLpPackets.find(txSeq) == removedLpPackets.end()) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800155 auto removedTxSeqs = onLpPacketLost(txSeq, false);
156 for (auto removedTxSeq : removedTxSeqs) {
Eric Newberry971d9622018-03-30 23:29:26 -0700157 removedLpPackets.insert(removedTxSeq);
158 }
159 }
Eric Newberry185ab292017-03-28 06:45:39 +0000160 }
161 }
162
Eric Newberry7b0071e2017-07-03 17:33:31 +0000163 // If packet has Fragment and TxSequence fields, extract TxSequence and add to AckQueue
164 if (pkt.has<lp::FragmentField>() && pkt.has<lp::TxSequenceField>()) {
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800165 NFD_LOG_FACE_TRACE("queueing ack for remote txseq=" << pkt.get<lp::TxSequenceField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000166 m_ackQueue.push(pkt.get<lp::TxSequenceField>());
Eric Newberry32f7eac2020-02-07 14:40:17 -0800167
168 // Check for received frames with duplicate Sequences
169 if (pkt.has<lp::SequenceField>()) {
170 lp::Sequence pktSequence = pkt.get<lp::SequenceField>();
171 isDuplicate = m_recentRecvSeqs.count(pktSequence) > 0;
172 // Check for recent received Sequences to remove
173 auto now = time::steady_clock::now();
174 auto rto = m_rttEst.getEstimatedRto();
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500175 while (!m_recentRecvSeqsQueue.empty() &&
Eric Newberry32f7eac2020-02-07 14:40:17 -0800176 now > m_recentRecvSeqs[m_recentRecvSeqsQueue.front()] + rto) {
177 m_recentRecvSeqs.erase(m_recentRecvSeqsQueue.front());
178 m_recentRecvSeqsQueue.pop();
179 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400180 m_recentRecvSeqs.try_emplace(pktSequence, now);
Eric Newberry32f7eac2020-02-07 14:40:17 -0800181 m_recentRecvSeqsQueue.push(pktSequence);
182 }
183
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400184 startIdleAckTimer();
Eric Newberry185ab292017-03-28 06:45:39 +0000185 }
Eric Newberry32f7eac2020-02-07 14:40:17 -0800186
187 return !isDuplicate;
Eric Newberry185ab292017-03-28 06:45:39 +0000188}
189
190void
191LpReliability::piggyback(lp::Packet& pkt, ssize_t mtu)
192{
193 BOOST_ASSERT(m_options.isEnabled);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000194 BOOST_ASSERT(pkt.wireEncode().type() == lp::tlv::LpPacket);
Eric Newberry185ab292017-03-28 06:45:39 +0000195
Eric Newberry7b0071e2017-07-03 17:33:31 +0000196 // up to 2 extra octets reserved for potential TLV-LENGTH size increases
197 ssize_t pktSize = pkt.wireEncode().size();
198 ssize_t reservedSpace = tlv::sizeOfVarNumber(ndn::MAX_NDN_PACKET_SIZE) -
199 tlv::sizeOfVarNumber(pktSize);
200 ssize_t remainingSpace = (mtu == MTU_UNLIMITED ? ndn::MAX_NDN_PACKET_SIZE : mtu) - reservedSpace;
201 remainingSpace -= pktSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000202
Eric Newberry7b0071e2017-07-03 17:33:31 +0000203 while (!m_ackQueue.empty()) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800204 lp::Sequence ackTxSeq = m_ackQueue.front();
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400205 // Ack size = Ack TLV-TYPE (3 octets) + TLV-LENGTH (1 octet) + lp::Sequence (8 octets)
Junxiao Shi21e01932018-04-21 10:39:05 +0000206 const ssize_t ackSize = tlv::sizeOfVarNumber(lp::tlv::Ack) +
207 tlv::sizeOfVarNumber(sizeof(lp::Sequence)) +
208 sizeof(lp::Sequence);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000209
210 if (ackSize > remainingSpace) {
211 break;
212 }
213
Eric Newberry32f7eac2020-02-07 14:40:17 -0800214 NFD_LOG_FACE_TRACE("piggybacking ack for remote txseq=" << ackTxSeq);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800215
Eric Newberry32f7eac2020-02-07 14:40:17 -0800216 pkt.add<lp::AckField>(ackTxSeq);
Eric Newberry185ab292017-03-28 06:45:39 +0000217 m_ackQueue.pop();
Eric Newberry7b0071e2017-07-03 17:33:31 +0000218 remainingSpace -= ackSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000219 }
220}
221
Eric Newberry7b0071e2017-07-03 17:33:31 +0000222lp::Sequence
223LpReliability::assignTxSequence(lp::Packet& frag)
224{
225 lp::Sequence txSeq = ++m_lastTxSeqNo;
226 frag.set<lp::TxSequenceField>(txSeq);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500227 if (!m_unackedFrags.empty() && m_lastTxSeqNo == m_firstUnackedFrag->first) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500228 NDN_THROW(std::length_error("TxSequence range exceeded"));
Eric Newberry7b0071e2017-07-03 17:33:31 +0000229 }
230 return m_lastTxSeqNo;
231}
232
Eric Newberry185ab292017-03-28 06:45:39 +0000233void
234LpReliability::startIdleAckTimer()
235{
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400236 if (m_idleAckTimer) {
237 // timer is already running, do nothing
238 return;
239 }
Eric Newberry185ab292017-03-28 06:45:39 +0000240
Davide Pesavento3dade002019-03-19 11:29:56 -0600241 m_idleAckTimer = getScheduler().schedule(m_options.idleAckTimerPeriod, [this] {
Eric Newberry185ab292017-03-28 06:45:39 +0000242 while (!m_ackQueue.empty()) {
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700243 m_linkService->requestIdlePacket();
Eric Newberry185ab292017-03-28 06:45:39 +0000244 }
Eric Newberry185ab292017-03-28 06:45:39 +0000245 });
246}
247
Eric Newberry971d9622018-03-30 23:29:26 -0700248std::vector<lp::Sequence>
Eric Newberry7b0071e2017-07-03 17:33:31 +0000249LpReliability::findLostLpPackets(LpReliability::UnackedFrags::iterator ackIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000250{
Eric Newberry971d9622018-03-30 23:29:26 -0700251 std::vector<lp::Sequence> lostLpPackets;
Eric Newberry185ab292017-03-28 06:45:39 +0000252
253 for (auto it = m_firstUnackedFrag; ; ++it) {
254 if (it == m_unackedFrags.end()) {
255 it = m_unackedFrags.begin();
256 }
257
Eric Newberry7b0071e2017-07-03 17:33:31 +0000258 if (it->first == ackIt->first) {
Eric Newberry185ab292017-03-28 06:45:39 +0000259 break;
260 }
261
262 auto& unackedFrag = it->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000263 unackedFrag.nGreaterSeqAcks++;
Eric Newberry32f7eac2020-02-07 14:40:17 -0800264 NFD_LOG_FACE_TRACE("received ack=" << ackIt->first << " before=" << it->first <<
265 ", before count=" << unackedFrag.nGreaterSeqAcks);
Eric Newberry185ab292017-03-28 06:45:39 +0000266
Eric Newberry7b0071e2017-07-03 17:33:31 +0000267 if (unackedFrag.nGreaterSeqAcks >= m_options.seqNumLossThreshold) {
Eric Newberry971d9622018-03-30 23:29:26 -0700268 lostLpPackets.push_back(it->first);
Eric Newberry185ab292017-03-28 06:45:39 +0000269 }
270 }
271
272 return lostLpPackets;
273}
274
Eric Newberry971d9622018-03-30 23:29:26 -0700275std::vector<lp::Sequence>
Eric Newberry32f7eac2020-02-07 14:40:17 -0800276LpReliability::onLpPacketLost(lp::Sequence txSeq, bool isTimeout)
Eric Newberry185ab292017-03-28 06:45:39 +0000277{
Eric Newberry971d9622018-03-30 23:29:26 -0700278 BOOST_ASSERT(m_unackedFrags.count(txSeq) > 0);
279 auto txSeqIt = m_unackedFrags.find(txSeq);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000280
281 auto& txFrag = txSeqIt->second;
282 txFrag.rtoTimer.cancel();
283 auto netPkt = txFrag.netPkt;
Eric Newberry971d9622018-03-30 23:29:26 -0700284 std::vector<lp::Sequence> removedThisTxSeq;
Eric Newberry32f7eac2020-02-07 14:40:17 -0800285 lp::Sequence seq = txFrag.pkt.get<lp::SequenceField>();
286
287 if (isTimeout) {
288 NFD_LOG_FACE_TRACE("rto timer expired for seq=" << seq << ", txseq=" << txSeq);
289 }
290 else { // lost due to out-of-order TxSeqs
291 NFD_LOG_FACE_TRACE("seq=" << seq << ", txseq=" << txSeq <<
292 " considered lost from acks for more recent txseqs");
293 }
Eric Newberry185ab292017-03-28 06:45:39 +0000294
295 // Check if maximum number of retransmissions exceeded
296 if (txFrag.retxCount >= m_options.maxRetx) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800297 NFD_LOG_FACE_DEBUG("seq=" << seq << " exceeded allowed retransmissions: DROP");
Eric Newberry7b0071e2017-07-03 17:33:31 +0000298 // Delete all LpPackets of NetPkt from m_unackedFrags (except this one)
299 for (size_t i = 0; i < netPkt->unackedFrags.size(); i++) {
300 if (netPkt->unackedFrags[i] != txSeqIt) {
Eric Newberry971d9622018-03-30 23:29:26 -0700301 removedThisTxSeq.push_back(netPkt->unackedFrags[i]->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000302 deleteUnackedFrag(netPkt->unackedFrags[i]);
303 }
Eric Newberry185ab292017-03-28 06:45:39 +0000304 }
Eric Newberry185ab292017-03-28 06:45:39 +0000305
306 ++m_linkService->nRetxExhausted;
Eric Newberry41aba102017-11-01 16:42:13 -0700307
308 // Notify strategy of dropped Interest (if any)
309 if (netPkt->isInterest) {
310 BOOST_ASSERT(netPkt->pkt.has<lp::FragmentField>());
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500311 auto frag = netPkt->pkt.get<lp::FragmentField>();
312 onDroppedInterest(Interest(Block({frag.first, frag.second})));
Eric Newberry41aba102017-11-01 16:42:13 -0700313 }
314
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800315 // Delete this LpPacket from m_unackedFrags
Eric Newberry971d9622018-03-30 23:29:26 -0700316 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000317 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000318 }
319 else {
Eric Newberry7b0071e2017-07-03 17:33:31 +0000320 // Assign new TxSequence
321 lp::Sequence newTxSeq = assignTxSequence(txFrag.pkt);
Eric Newberry00d39fd2017-12-10 14:26:45 -0700322 netPkt->didRetx = true;
Eric Newberry185ab292017-03-28 06:45:39 +0000323
Eric Newberry7b0071e2017-07-03 17:33:31 +0000324 // Move fragment to new TxSequence mapping
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400325 auto hint = m_firstUnackedFrag != m_unackedFrags.end() && m_firstUnackedFrag->first > newTxSeq
326 ? m_firstUnackedFrag
327 : m_unackedFrags.end();
328 auto newTxFragIt = m_unackedFrags.try_emplace(hint, newTxSeq, txFrag.pkt);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000329 auto& newTxFrag = newTxFragIt->second;
330 newTxFrag.retxCount = txFrag.retxCount + 1;
331 newTxFrag.netPkt = netPkt;
332
333 // Update associated NetPkt
334 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), txSeqIt);
335 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
336 *fragInNetPkt = newTxFragIt;
337
Eric Newberry971d9622018-03-30 23:29:26 -0700338 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000339 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000340
341 // Retransmit fragment
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700342 m_linkService->sendLpPacket(lp::Packet(newTxFrag.pkt));
Eric Newberry7b0071e2017-07-03 17:33:31 +0000343
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800344 auto rto = m_rttEst.getEstimatedRto();
Eric Newberry32f7eac2020-02-07 14:40:17 -0800345 NFD_LOG_FACE_TRACE("retransmitting seq=" << seq << ", txseq=" << newTxSeq << ", retx=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800346 txFrag.retxCount << ", rto=" <<
347 time::duration_cast<time::milliseconds>(rto).count() << "ms");
348
Eric Newberry7b0071e2017-07-03 17:33:31 +0000349 // Start RTO timer for this sequence
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800350 newTxFrag.rtoTimer = getScheduler().schedule(rto, [=] {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800351 onLpPacketLost(newTxSeq, true);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800352 });
Eric Newberry185ab292017-03-28 06:45:39 +0000353 }
Eric Newberry971d9622018-03-30 23:29:26 -0700354
355 return removedThisTxSeq;
Eric Newberry185ab292017-03-28 06:45:39 +0000356}
357
358void
Eric Newberry7b0071e2017-07-03 17:33:31 +0000359LpReliability::onLpPacketAcknowledged(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000360{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000361 auto netPkt = fragIt->second.netPkt;
Eric Newberry185ab292017-03-28 06:45:39 +0000362
Eric Newberry7b0071e2017-07-03 17:33:31 +0000363 // Remove from NetPkt unacked fragment list
364 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), fragIt);
365 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
366 *fragInNetPkt = netPkt->unackedFrags.back();
367 netPkt->unackedFrags.pop_back();
Eric Newberry185ab292017-03-28 06:45:39 +0000368
Eric Newberry7b0071e2017-07-03 17:33:31 +0000369 // Check if network-layer packet completely received. If so, increment counters
370 if (netPkt->unackedFrags.empty()) {
371 if (netPkt->didRetx) {
Eric Newberry185ab292017-03-28 06:45:39 +0000372 ++m_linkService->nRetransmitted;
373 }
374 else {
375 ++m_linkService->nAcknowledged;
376 }
Eric Newberry185ab292017-03-28 06:45:39 +0000377 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000378
379 deleteUnackedFrag(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000380}
381
Eric Newberry7b0071e2017-07-03 17:33:31 +0000382void
383LpReliability::deleteUnackedFrag(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000384{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000385 lp::Sequence firstUnackedTxSeq = m_firstUnackedFrag->first;
386 lp::Sequence currentTxSeq = fragIt->first;
387 auto nextFragIt = m_unackedFrags.erase(fragIt);
388
389 if (!m_unackedFrags.empty() && firstUnackedTxSeq == currentTxSeq) {
390 // If "first" fragment in send window (allowing for wraparound), increment window begin
391 if (nextFragIt == m_unackedFrags.end()) {
392 m_firstUnackedFrag = m_unackedFrags.begin();
393 }
394 else {
395 m_firstUnackedFrag = nextFragIt;
396 }
Eric Newberry185ab292017-03-28 06:45:39 +0000397 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000398 else if (m_unackedFrags.empty()) {
399 m_firstUnackedFrag = m_unackedFrags.end();
400 }
Eric Newberry185ab292017-03-28 06:45:39 +0000401}
402
403LpReliability::UnackedFrag::UnackedFrag(lp::Packet pkt)
404 : pkt(std::move(pkt))
405 , sendTime(time::steady_clock::now())
406 , retxCount(0)
407 , nGreaterSeqAcks(0)
Eric Newberry185ab292017-03-28 06:45:39 +0000408{
409}
410
Eric Newberry41aba102017-11-01 16:42:13 -0700411LpReliability::NetPkt::NetPkt(lp::Packet&& pkt, bool isInterest)
412 : pkt(std::move(pkt))
413 , isInterest(isInterest)
414 , didRetx(false)
415{
416}
417
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800418std::ostream&
419operator<<(std::ostream& os, const FaceLogHelper<LpReliability>& flh)
420{
421 if (flh.obj.getLinkService() == nullptr) {
422 os << "[id=0,local=unknown,remote=unknown] ";
423 }
424 else {
425 os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
426 }
427 return os;
428}
429
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400430} // namespace nfd::face