blob: a3d2596161deba4f7c29296edd873978807db6f0 [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 Pesaventoa599d2a2022-02-16 18:52:43 -05003 * 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#include "lp-reliability.hpp"
27#include "generic-link-service.hpp"
28#include "transport.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040029#include "common/global.hpp"
Eric Newberry185ab292017-03-28 06:45:39 +000030
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::face {
Eric Newberry185ab292017-03-28 06:45:39 +000032
Eric Newberry5ab4cf82020-02-03 15:40:16 -080033NFD_LOG_INIT(LpReliability);
34
Eric Newberry185ab292017-03-28 06:45:39 +000035LpReliability::LpReliability(const LpReliability::Options& options, GenericLinkService* linkService)
36 : m_options(options)
37 , m_linkService(linkService)
38 , m_firstUnackedFrag(m_unackedFrags.begin())
Eric Newberry7b0071e2017-07-03 17:33:31 +000039 , m_lastTxSeqNo(-1) // set to "-1" to start TxSequence numbers at 0
Eric Newberry185ab292017-03-28 06:45:39 +000040{
41 BOOST_ASSERT(m_linkService != nullptr);
Davide Pesaventoe4b22382018-06-10 14:37:24 -040042 BOOST_ASSERT(m_options.idleAckTimerPeriod > 0_ns);
Eric Newberry185ab292017-03-28 06:45:39 +000043}
44
45void
46LpReliability::setOptions(const Options& options)
47{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040048 BOOST_ASSERT(options.idleAckTimerPeriod > 0_ns);
Eric Newberry185ab292017-03-28 06:45:39 +000049
50 if (m_options.isEnabled && !options.isEnabled) {
Davide Pesaventof190cfa2019-07-17 20:14:11 -040051 m_idleAckTimer.cancel();
Eric Newberry185ab292017-03-28 06:45:39 +000052 }
53
54 m_options = options;
55}
56
57const GenericLinkService*
58LpReliability::getLinkService() const
59{
60 return m_linkService;
61}
62
63void
Eric Newberry41aba102017-11-01 16:42:13 -070064LpReliability::handleOutgoing(std::vector<lp::Packet>& frags, lp::Packet&& pkt, bool isInterest)
Eric Newberry185ab292017-03-28 06:45:39 +000065{
66 BOOST_ASSERT(m_options.isEnabled);
67
Eric Newberry185ab292017-03-28 06:45:39 +000068 auto unackedFragsIt = m_unackedFrags.begin();
Eric Newberry7b0071e2017-07-03 17:33:31 +000069 auto sendTime = time::steady_clock::now();
Eric Newberry185ab292017-03-28 06:45:39 +000070
Eric Newberry41aba102017-11-01 16:42:13 -070071 auto netPkt = make_shared<NetPkt>(std::move(pkt), isInterest);
Eric Newberry7b0071e2017-07-03 17:33:31 +000072 netPkt->unackedFrags.reserve(frags.size());
73
74 for (lp::Packet& frag : frags) {
Eric Newberry32f7eac2020-02-07 14:40:17 -080075 // Non-IDLE packets are required to have assigned Sequence numbers with LpReliability enabled
76 BOOST_ASSERT(frag.has<lp::SequenceField>());
77
Eric Newberry7b0071e2017-07-03 17:33:31 +000078 // Assign TxSequence number
79 lp::Sequence txSeq = assignTxSequence(frag);
80
Eric Newberry185ab292017-03-28 06:45:39 +000081 // Store LpPacket for future retransmissions
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040082 unackedFragsIt = m_unackedFrags.try_emplace(unackedFragsIt, txSeq, frag);
Eric Newberry7b0071e2017-07-03 17:33:31 +000083 unackedFragsIt->second.sendTime = sendTime;
Eric Newberry5ab4cf82020-02-03 15:40:16 -080084 auto rto = m_rttEst.getEstimatedRto();
Eric Newberry32f7eac2020-02-07 14:40:17 -080085 lp::Sequence seq = frag.get<lp::SequenceField>();
86 NFD_LOG_FACE_TRACE("transmitting seq=" << seq << ", txseq=" << txSeq << ", rto=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -080087 time::duration_cast<time::milliseconds>(rto).count() << "ms");
88 unackedFragsIt->second.rtoTimer = getScheduler().schedule(rto, [=] {
Eric Newberry32f7eac2020-02-07 14:40:17 -080089 onLpPacketLost(txSeq, true);
Eric Newberry5ab4cf82020-02-03 15:40:16 -080090 });
Eric Newberry7b0071e2017-07-03 17:33:31 +000091 unackedFragsIt->second.netPkt = netPkt;
92
Eric Newberry185ab292017-03-28 06:45:39 +000093 if (m_unackedFrags.size() == 1) {
Eric Newberry7b0071e2017-07-03 17:33:31 +000094 m_firstUnackedFrag = m_unackedFrags.begin();
Eric Newberry185ab292017-03-28 06:45:39 +000095 }
Eric Newberry7b0071e2017-07-03 17:33:31 +000096
97 // Add to associated NetPkt
98 netPkt->unackedFrags.push_back(unackedFragsIt);
Eric Newberry185ab292017-03-28 06:45:39 +000099 }
100}
101
Eric Newberry32f7eac2020-02-07 14:40:17 -0800102bool
Eric Newberry185ab292017-03-28 06:45:39 +0000103LpReliability::processIncomingPacket(const lp::Packet& pkt)
104{
105 BOOST_ASSERT(m_options.isEnabled);
106
Eric Newberry32f7eac2020-02-07 14:40:17 -0800107 bool isDuplicate = false;
Eric Newberry185ab292017-03-28 06:45:39 +0000108 auto now = time::steady_clock::now();
109
110 // Extract and parse Acks
Eric Newberry32f7eac2020-02-07 14:40:17 -0800111 for (lp::Sequence ackTxSeq : pkt.list<lp::AckField>()) {
112 auto fragIt = m_unackedFrags.find(ackTxSeq);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000113 if (fragIt == m_unackedFrags.end()) {
114 // Ignore an Ack for an unknown TxSequence number
Eric Newberry32f7eac2020-02-07 14:40:17 -0800115 NFD_LOG_FACE_DEBUG("received ack for unknown txseq=" << ackTxSeq);
Eric Newberry185ab292017-03-28 06:45:39 +0000116 continue;
117 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000118 auto& frag = fragIt->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000119
120 // Cancel the RTO timer for the acknowledged fragment
Eric Newberry7b0071e2017-07-03 17:33:31 +0000121 frag.rtoTimer.cancel();
Eric Newberry185ab292017-03-28 06:45:39 +0000122
Eric Newberry7b0071e2017-07-03 17:33:31 +0000123 if (frag.retxCount == 0) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800124 NFD_LOG_FACE_TRACE("received ack for seq=" << frag.pkt.get<lp::SequenceField>() << ", txseq=" <<
125 ackTxSeq << ", retx=0, rtt=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800126 time::duration_cast<time::milliseconds>(now - frag.sendTime).count() << "ms");
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400127 // This sequence had no retransmissions, so use it to estimate the RTO
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400128 m_rttEst.addMeasurement(now - frag.sendTime);
Eric Newberry185ab292017-03-28 06:45:39 +0000129 }
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800130 else {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800131 NFD_LOG_FACE_TRACE("received ack for seq=" << frag.pkt.get<lp::SequenceField>() << ", txseq=" <<
132 ackTxSeq << ", retx=" << frag.retxCount);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800133 }
Eric Newberry185ab292017-03-28 06:45:39 +0000134
Eric Newberry32f7eac2020-02-07 14:40:17 -0800135 // Look for frags with TxSequence numbers < ackTxSeq (allowing for wraparound) and consider
136 // them lost if a configurable number of Acks containing greater TxSequence numbers have been
Eric Newberry7b0071e2017-07-03 17:33:31 +0000137 // received.
138 auto lostLpPackets = findLostLpPackets(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000139
Eric Newberry7b0071e2017-07-03 17:33:31 +0000140 // Remove the fragment from the map of unacknowledged fragments and from its associated network
141 // packet. Potentially increment the start of the window.
142 onLpPacketAcknowledged(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000143
Eric Newberry971d9622018-03-30 23:29:26 -0700144 // This set contains TxSequences that have been removed by onLpPacketLost below because they
145 // were part of a network packet that was removed due to a fragment exceeding retx, as well as
146 // any other TxSequences removed by onLpPacketLost. This prevents onLpPacketLost from being
147 // called later for an invalid iterator.
148 std::set<lp::Sequence> removedLpPackets;
149
Eric Newberry7b0071e2017-07-03 17:33:31 +0000150 // Resend or fail fragments considered lost. Potentially increment the start of the window.
Eric Newberry971d9622018-03-30 23:29:26 -0700151 for (lp::Sequence txSeq : lostLpPackets) {
152 if (removedLpPackets.find(txSeq) == removedLpPackets.end()) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800153 auto removedTxSeqs = onLpPacketLost(txSeq, false);
154 for (auto removedTxSeq : removedTxSeqs) {
Eric Newberry971d9622018-03-30 23:29:26 -0700155 removedLpPackets.insert(removedTxSeq);
156 }
157 }
Eric Newberry185ab292017-03-28 06:45:39 +0000158 }
159 }
160
Eric Newberry7b0071e2017-07-03 17:33:31 +0000161 // If packet has Fragment and TxSequence fields, extract TxSequence and add to AckQueue
162 if (pkt.has<lp::FragmentField>() && pkt.has<lp::TxSequenceField>()) {
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800163 NFD_LOG_FACE_TRACE("queueing ack for remote txseq=" << pkt.get<lp::TxSequenceField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000164 m_ackQueue.push(pkt.get<lp::TxSequenceField>());
Eric Newberry32f7eac2020-02-07 14:40:17 -0800165
166 // Check for received frames with duplicate Sequences
167 if (pkt.has<lp::SequenceField>()) {
168 lp::Sequence pktSequence = pkt.get<lp::SequenceField>();
169 isDuplicate = m_recentRecvSeqs.count(pktSequence) > 0;
170 // Check for recent received Sequences to remove
171 auto now = time::steady_clock::now();
172 auto rto = m_rttEst.getEstimatedRto();
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500173 while (!m_recentRecvSeqsQueue.empty() &&
Eric Newberry32f7eac2020-02-07 14:40:17 -0800174 now > m_recentRecvSeqs[m_recentRecvSeqsQueue.front()] + rto) {
175 m_recentRecvSeqs.erase(m_recentRecvSeqsQueue.front());
176 m_recentRecvSeqsQueue.pop();
177 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400178 m_recentRecvSeqs.try_emplace(pktSequence, now);
Eric Newberry32f7eac2020-02-07 14:40:17 -0800179 m_recentRecvSeqsQueue.push(pktSequence);
180 }
181
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400182 startIdleAckTimer();
Eric Newberry185ab292017-03-28 06:45:39 +0000183 }
Eric Newberry32f7eac2020-02-07 14:40:17 -0800184
185 return !isDuplicate;
Eric Newberry185ab292017-03-28 06:45:39 +0000186}
187
188void
189LpReliability::piggyback(lp::Packet& pkt, ssize_t mtu)
190{
191 BOOST_ASSERT(m_options.isEnabled);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000192 BOOST_ASSERT(pkt.wireEncode().type() == lp::tlv::LpPacket);
Eric Newberry185ab292017-03-28 06:45:39 +0000193
Eric Newberry7b0071e2017-07-03 17:33:31 +0000194 // up to 2 extra octets reserved for potential TLV-LENGTH size increases
195 ssize_t pktSize = pkt.wireEncode().size();
196 ssize_t reservedSpace = tlv::sizeOfVarNumber(ndn::MAX_NDN_PACKET_SIZE) -
197 tlv::sizeOfVarNumber(pktSize);
198 ssize_t remainingSpace = (mtu == MTU_UNLIMITED ? ndn::MAX_NDN_PACKET_SIZE : mtu) - reservedSpace;
199 remainingSpace -= pktSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000200
Eric Newberry7b0071e2017-07-03 17:33:31 +0000201 while (!m_ackQueue.empty()) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800202 lp::Sequence ackTxSeq = m_ackQueue.front();
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400203 // Ack size = Ack TLV-TYPE (3 octets) + TLV-LENGTH (1 octet) + lp::Sequence (8 octets)
Junxiao Shi21e01932018-04-21 10:39:05 +0000204 const ssize_t ackSize = tlv::sizeOfVarNumber(lp::tlv::Ack) +
205 tlv::sizeOfVarNumber(sizeof(lp::Sequence)) +
206 sizeof(lp::Sequence);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000207
208 if (ackSize > remainingSpace) {
209 break;
210 }
211
Eric Newberry32f7eac2020-02-07 14:40:17 -0800212 NFD_LOG_FACE_TRACE("piggybacking ack for remote txseq=" << ackTxSeq);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800213
Eric Newberry32f7eac2020-02-07 14:40:17 -0800214 pkt.add<lp::AckField>(ackTxSeq);
Eric Newberry185ab292017-03-28 06:45:39 +0000215 m_ackQueue.pop();
Eric Newberry7b0071e2017-07-03 17:33:31 +0000216 remainingSpace -= ackSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000217 }
218}
219
Eric Newberry7b0071e2017-07-03 17:33:31 +0000220lp::Sequence
221LpReliability::assignTxSequence(lp::Packet& frag)
222{
223 lp::Sequence txSeq = ++m_lastTxSeqNo;
224 frag.set<lp::TxSequenceField>(txSeq);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500225 if (!m_unackedFrags.empty() && m_lastTxSeqNo == m_firstUnackedFrag->first) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500226 NDN_THROW(std::length_error("TxSequence range exceeded"));
Eric Newberry7b0071e2017-07-03 17:33:31 +0000227 }
228 return m_lastTxSeqNo;
229}
230
Eric Newberry185ab292017-03-28 06:45:39 +0000231void
232LpReliability::startIdleAckTimer()
233{
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400234 if (m_idleAckTimer) {
235 // timer is already running, do nothing
236 return;
237 }
Eric Newberry185ab292017-03-28 06:45:39 +0000238
Davide Pesavento3dade002019-03-19 11:29:56 -0600239 m_idleAckTimer = getScheduler().schedule(m_options.idleAckTimerPeriod, [this] {
Eric Newberry185ab292017-03-28 06:45:39 +0000240 while (!m_ackQueue.empty()) {
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700241 m_linkService->requestIdlePacket();
Eric Newberry185ab292017-03-28 06:45:39 +0000242 }
Eric Newberry185ab292017-03-28 06:45:39 +0000243 });
244}
245
Eric Newberry971d9622018-03-30 23:29:26 -0700246std::vector<lp::Sequence>
Eric Newberry7b0071e2017-07-03 17:33:31 +0000247LpReliability::findLostLpPackets(LpReliability::UnackedFrags::iterator ackIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000248{
Eric Newberry971d9622018-03-30 23:29:26 -0700249 std::vector<lp::Sequence> lostLpPackets;
Eric Newberry185ab292017-03-28 06:45:39 +0000250
251 for (auto it = m_firstUnackedFrag; ; ++it) {
252 if (it == m_unackedFrags.end()) {
253 it = m_unackedFrags.begin();
254 }
255
Eric Newberry7b0071e2017-07-03 17:33:31 +0000256 if (it->first == ackIt->first) {
Eric Newberry185ab292017-03-28 06:45:39 +0000257 break;
258 }
259
260 auto& unackedFrag = it->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000261 unackedFrag.nGreaterSeqAcks++;
Eric Newberry32f7eac2020-02-07 14:40:17 -0800262 NFD_LOG_FACE_TRACE("received ack=" << ackIt->first << " before=" << it->first <<
263 ", before count=" << unackedFrag.nGreaterSeqAcks);
Eric Newberry185ab292017-03-28 06:45:39 +0000264
Eric Newberry7b0071e2017-07-03 17:33:31 +0000265 if (unackedFrag.nGreaterSeqAcks >= m_options.seqNumLossThreshold) {
Eric Newberry971d9622018-03-30 23:29:26 -0700266 lostLpPackets.push_back(it->first);
Eric Newberry185ab292017-03-28 06:45:39 +0000267 }
268 }
269
270 return lostLpPackets;
271}
272
Eric Newberry971d9622018-03-30 23:29:26 -0700273std::vector<lp::Sequence>
Eric Newberry32f7eac2020-02-07 14:40:17 -0800274LpReliability::onLpPacketLost(lp::Sequence txSeq, bool isTimeout)
Eric Newberry185ab292017-03-28 06:45:39 +0000275{
Eric Newberry971d9622018-03-30 23:29:26 -0700276 BOOST_ASSERT(m_unackedFrags.count(txSeq) > 0);
277 auto txSeqIt = m_unackedFrags.find(txSeq);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000278
279 auto& txFrag = txSeqIt->second;
280 txFrag.rtoTimer.cancel();
281 auto netPkt = txFrag.netPkt;
Eric Newberry971d9622018-03-30 23:29:26 -0700282 std::vector<lp::Sequence> removedThisTxSeq;
Eric Newberry32f7eac2020-02-07 14:40:17 -0800283 lp::Sequence seq = txFrag.pkt.get<lp::SequenceField>();
284
285 if (isTimeout) {
286 NFD_LOG_FACE_TRACE("rto timer expired for seq=" << seq << ", txseq=" << txSeq);
287 }
288 else { // lost due to out-of-order TxSeqs
289 NFD_LOG_FACE_TRACE("seq=" << seq << ", txseq=" << txSeq <<
290 " considered lost from acks for more recent txseqs");
291 }
Eric Newberry185ab292017-03-28 06:45:39 +0000292
293 // Check if maximum number of retransmissions exceeded
294 if (txFrag.retxCount >= m_options.maxRetx) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800295 NFD_LOG_FACE_DEBUG("seq=" << seq << " exceeded allowed retransmissions: DROP");
Eric Newberry7b0071e2017-07-03 17:33:31 +0000296 // Delete all LpPackets of NetPkt from m_unackedFrags (except this one)
297 for (size_t i = 0; i < netPkt->unackedFrags.size(); i++) {
298 if (netPkt->unackedFrags[i] != txSeqIt) {
Eric Newberry971d9622018-03-30 23:29:26 -0700299 removedThisTxSeq.push_back(netPkt->unackedFrags[i]->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000300 deleteUnackedFrag(netPkt->unackedFrags[i]);
301 }
Eric Newberry185ab292017-03-28 06:45:39 +0000302 }
Eric Newberry185ab292017-03-28 06:45:39 +0000303
304 ++m_linkService->nRetxExhausted;
Eric Newberry41aba102017-11-01 16:42:13 -0700305
306 // Notify strategy of dropped Interest (if any)
307 if (netPkt->isInterest) {
308 BOOST_ASSERT(netPkt->pkt.has<lp::FragmentField>());
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500309 auto frag = netPkt->pkt.get<lp::FragmentField>();
310 onDroppedInterest(Interest(Block({frag.first, frag.second})));
Eric Newberry41aba102017-11-01 16:42:13 -0700311 }
312
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800313 // Delete this LpPacket from m_unackedFrags
Eric Newberry971d9622018-03-30 23:29:26 -0700314 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000315 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000316 }
317 else {
Eric Newberry7b0071e2017-07-03 17:33:31 +0000318 // Assign new TxSequence
319 lp::Sequence newTxSeq = assignTxSequence(txFrag.pkt);
Eric Newberry00d39fd2017-12-10 14:26:45 -0700320 netPkt->didRetx = true;
Eric Newberry185ab292017-03-28 06:45:39 +0000321
Eric Newberry7b0071e2017-07-03 17:33:31 +0000322 // Move fragment to new TxSequence mapping
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400323 auto hint = m_firstUnackedFrag != m_unackedFrags.end() && m_firstUnackedFrag->first > newTxSeq
324 ? m_firstUnackedFrag
325 : m_unackedFrags.end();
326 auto newTxFragIt = m_unackedFrags.try_emplace(hint, newTxSeq, txFrag.pkt);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000327 auto& newTxFrag = newTxFragIt->second;
328 newTxFrag.retxCount = txFrag.retxCount + 1;
329 newTxFrag.netPkt = netPkt;
330
331 // Update associated NetPkt
332 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), txSeqIt);
333 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
334 *fragInNetPkt = newTxFragIt;
335
Eric Newberry971d9622018-03-30 23:29:26 -0700336 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000337 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000338
339 // Retransmit fragment
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700340 m_linkService->sendLpPacket(lp::Packet(newTxFrag.pkt));
Eric Newberry7b0071e2017-07-03 17:33:31 +0000341
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800342 auto rto = m_rttEst.getEstimatedRto();
Eric Newberry32f7eac2020-02-07 14:40:17 -0800343 NFD_LOG_FACE_TRACE("retransmitting seq=" << seq << ", txseq=" << newTxSeq << ", retx=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800344 txFrag.retxCount << ", rto=" <<
345 time::duration_cast<time::milliseconds>(rto).count() << "ms");
346
Eric Newberry7b0071e2017-07-03 17:33:31 +0000347 // Start RTO timer for this sequence
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800348 newTxFrag.rtoTimer = getScheduler().schedule(rto, [=] {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800349 onLpPacketLost(newTxSeq, true);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800350 });
Eric Newberry185ab292017-03-28 06:45:39 +0000351 }
Eric Newberry971d9622018-03-30 23:29:26 -0700352
353 return removedThisTxSeq;
Eric Newberry185ab292017-03-28 06:45:39 +0000354}
355
356void
Eric Newberry7b0071e2017-07-03 17:33:31 +0000357LpReliability::onLpPacketAcknowledged(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000358{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000359 auto netPkt = fragIt->second.netPkt;
Eric Newberry185ab292017-03-28 06:45:39 +0000360
Eric Newberry7b0071e2017-07-03 17:33:31 +0000361 // Remove from NetPkt unacked fragment list
362 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), fragIt);
363 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
364 *fragInNetPkt = netPkt->unackedFrags.back();
365 netPkt->unackedFrags.pop_back();
Eric Newberry185ab292017-03-28 06:45:39 +0000366
Eric Newberry7b0071e2017-07-03 17:33:31 +0000367 // Check if network-layer packet completely received. If so, increment counters
368 if (netPkt->unackedFrags.empty()) {
369 if (netPkt->didRetx) {
Eric Newberry185ab292017-03-28 06:45:39 +0000370 ++m_linkService->nRetransmitted;
371 }
372 else {
373 ++m_linkService->nAcknowledged;
374 }
Eric Newberry185ab292017-03-28 06:45:39 +0000375 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000376
377 deleteUnackedFrag(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000378}
379
Eric Newberry7b0071e2017-07-03 17:33:31 +0000380void
381LpReliability::deleteUnackedFrag(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000382{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000383 lp::Sequence firstUnackedTxSeq = m_firstUnackedFrag->first;
384 lp::Sequence currentTxSeq = fragIt->first;
385 auto nextFragIt = m_unackedFrags.erase(fragIt);
386
387 if (!m_unackedFrags.empty() && firstUnackedTxSeq == currentTxSeq) {
388 // If "first" fragment in send window (allowing for wraparound), increment window begin
389 if (nextFragIt == m_unackedFrags.end()) {
390 m_firstUnackedFrag = m_unackedFrags.begin();
391 }
392 else {
393 m_firstUnackedFrag = nextFragIt;
394 }
Eric Newberry185ab292017-03-28 06:45:39 +0000395 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000396 else if (m_unackedFrags.empty()) {
397 m_firstUnackedFrag = m_unackedFrags.end();
398 }
Eric Newberry185ab292017-03-28 06:45:39 +0000399}
400
401LpReliability::UnackedFrag::UnackedFrag(lp::Packet pkt)
402 : pkt(std::move(pkt))
403 , sendTime(time::steady_clock::now())
404 , retxCount(0)
405 , nGreaterSeqAcks(0)
Eric Newberry185ab292017-03-28 06:45:39 +0000406{
407}
408
Eric Newberry41aba102017-11-01 16:42:13 -0700409LpReliability::NetPkt::NetPkt(lp::Packet&& pkt, bool isInterest)
410 : pkt(std::move(pkt))
411 , isInterest(isInterest)
412 , didRetx(false)
413{
414}
415
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800416std::ostream&
417operator<<(std::ostream& os, const FaceLogHelper<LpReliability>& flh)
418{
419 if (flh.obj.getLinkService() == nullptr) {
420 os << "[id=0,local=unknown,remote=unknown] ";
421 }
422 else {
423 os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
424 }
425 return os;
426}
427
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400428} // namespace nfd::face