blob: 4f26e5d9f30fba1d6c6c352b9684ef8965ecbcb4 [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
61const GenericLinkService*
62LpReliability::getLinkService() const
63{
64 return m_linkService;
65}
66
67void
Eric Newberry41aba102017-11-01 16:42:13 -070068LpReliability::handleOutgoing(std::vector<lp::Packet>& frags, lp::Packet&& pkt, bool isInterest)
Eric Newberry185ab292017-03-28 06:45:39 +000069{
70 BOOST_ASSERT(m_options.isEnabled);
71
Eric Newberry185ab292017-03-28 06:45:39 +000072 auto unackedFragsIt = m_unackedFrags.begin();
Eric Newberry7b0071e2017-07-03 17:33:31 +000073 auto sendTime = time::steady_clock::now();
Eric Newberry185ab292017-03-28 06:45:39 +000074
Eric Newberry41aba102017-11-01 16:42:13 -070075 auto netPkt = make_shared<NetPkt>(std::move(pkt), isInterest);
Eric Newberry7b0071e2017-07-03 17:33:31 +000076 netPkt->unackedFrags.reserve(frags.size());
77
78 for (lp::Packet& frag : frags) {
Eric Newberry32f7eac2020-02-07 14:40:17 -080079 // Non-IDLE packets are required to have assigned Sequence numbers with LpReliability enabled
80 BOOST_ASSERT(frag.has<lp::SequenceField>());
81
Eric Newberry7b0071e2017-07-03 17:33:31 +000082 // Assign TxSequence number
83 lp::Sequence txSeq = assignTxSequence(frag);
84
Eric Newberry185ab292017-03-28 06:45:39 +000085 // Store LpPacket for future retransmissions
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040086 unackedFragsIt = m_unackedFrags.try_emplace(unackedFragsIt, txSeq, frag);
Eric Newberry7b0071e2017-07-03 17:33:31 +000087 unackedFragsIt->second.sendTime = sendTime;
Eric Newberry5ab4cf82020-02-03 15:40:16 -080088 auto rto = m_rttEst.getEstimatedRto();
Eric Newberry32f7eac2020-02-07 14:40:17 -080089 lp::Sequence seq = frag.get<lp::SequenceField>();
90 NFD_LOG_FACE_TRACE("transmitting seq=" << seq << ", txseq=" << txSeq << ", rto=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -080091 time::duration_cast<time::milliseconds>(rto).count() << "ms");
92 unackedFragsIt->second.rtoTimer = getScheduler().schedule(rto, [=] {
Eric Newberry32f7eac2020-02-07 14:40:17 -080093 onLpPacketLost(txSeq, true);
Eric Newberry5ab4cf82020-02-03 15:40:16 -080094 });
Eric Newberry7b0071e2017-07-03 17:33:31 +000095 unackedFragsIt->second.netPkt = netPkt;
96
Eric Newberry185ab292017-03-28 06:45:39 +000097 if (m_unackedFrags.size() == 1) {
Eric Newberry7b0071e2017-07-03 17:33:31 +000098 m_firstUnackedFrag = m_unackedFrags.begin();
Eric Newberry185ab292017-03-28 06:45:39 +000099 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000100
101 // Add to associated NetPkt
102 netPkt->unackedFrags.push_back(unackedFragsIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000103 }
104}
105
Eric Newberry32f7eac2020-02-07 14:40:17 -0800106bool
Eric Newberry185ab292017-03-28 06:45:39 +0000107LpReliability::processIncomingPacket(const lp::Packet& pkt)
108{
109 BOOST_ASSERT(m_options.isEnabled);
110
Eric Newberry32f7eac2020-02-07 14:40:17 -0800111 bool isDuplicate = false;
Eric Newberry185ab292017-03-28 06:45:39 +0000112 auto now = time::steady_clock::now();
113
114 // Extract and parse Acks
Eric Newberry32f7eac2020-02-07 14:40:17 -0800115 for (lp::Sequence ackTxSeq : pkt.list<lp::AckField>()) {
116 auto fragIt = m_unackedFrags.find(ackTxSeq);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000117 if (fragIt == m_unackedFrags.end()) {
118 // Ignore an Ack for an unknown TxSequence number
Eric Newberry32f7eac2020-02-07 14:40:17 -0800119 NFD_LOG_FACE_DEBUG("received ack for unknown txseq=" << ackTxSeq);
Eric Newberry185ab292017-03-28 06:45:39 +0000120 continue;
121 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000122 auto& frag = fragIt->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000123
124 // Cancel the RTO timer for the acknowledged fragment
Eric Newberry7b0071e2017-07-03 17:33:31 +0000125 frag.rtoTimer.cancel();
Eric Newberry185ab292017-03-28 06:45:39 +0000126
Eric Newberry7b0071e2017-07-03 17:33:31 +0000127 if (frag.retxCount == 0) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800128 NFD_LOG_FACE_TRACE("received ack for seq=" << frag.pkt.get<lp::SequenceField>() << ", txseq=" <<
129 ackTxSeq << ", retx=0, rtt=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800130 time::duration_cast<time::milliseconds>(now - frag.sendTime).count() << "ms");
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400131 // This sequence had no retransmissions, so use it to estimate the RTO
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400132 m_rttEst.addMeasurement(now - frag.sendTime);
Eric Newberry185ab292017-03-28 06:45:39 +0000133 }
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800134 else {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800135 NFD_LOG_FACE_TRACE("received ack for seq=" << frag.pkt.get<lp::SequenceField>() << ", txseq=" <<
136 ackTxSeq << ", retx=" << frag.retxCount);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800137 }
Eric Newberry185ab292017-03-28 06:45:39 +0000138
Eric Newberry32f7eac2020-02-07 14:40:17 -0800139 // Look for frags with TxSequence numbers < ackTxSeq (allowing for wraparound) and consider
140 // them lost if a configurable number of Acks containing greater TxSequence numbers have been
Eric Newberry7b0071e2017-07-03 17:33:31 +0000141 // received.
142 auto lostLpPackets = findLostLpPackets(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000143
Eric Newberry7b0071e2017-07-03 17:33:31 +0000144 // Remove the fragment from the map of unacknowledged fragments and from its associated network
145 // packet. Potentially increment the start of the window.
146 onLpPacketAcknowledged(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000147
Eric Newberry971d9622018-03-30 23:29:26 -0700148 // This set contains TxSequences that have been removed by onLpPacketLost below because they
149 // were part of a network packet that was removed due to a fragment exceeding retx, as well as
150 // any other TxSequences removed by onLpPacketLost. This prevents onLpPacketLost from being
151 // called later for an invalid iterator.
152 std::set<lp::Sequence> removedLpPackets;
153
Eric Newberry7b0071e2017-07-03 17:33:31 +0000154 // Resend or fail fragments considered lost. Potentially increment the start of the window.
Eric Newberry971d9622018-03-30 23:29:26 -0700155 for (lp::Sequence txSeq : lostLpPackets) {
156 if (removedLpPackets.find(txSeq) == removedLpPackets.end()) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800157 auto removedTxSeqs = onLpPacketLost(txSeq, false);
158 for (auto removedTxSeq : removedTxSeqs) {
Eric Newberry971d9622018-03-30 23:29:26 -0700159 removedLpPackets.insert(removedTxSeq);
160 }
161 }
Eric Newberry185ab292017-03-28 06:45:39 +0000162 }
163 }
164
Eric Newberry7b0071e2017-07-03 17:33:31 +0000165 // If packet has Fragment and TxSequence fields, extract TxSequence and add to AckQueue
166 if (pkt.has<lp::FragmentField>() && pkt.has<lp::TxSequenceField>()) {
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800167 NFD_LOG_FACE_TRACE("queueing ack for remote txseq=" << pkt.get<lp::TxSequenceField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000168 m_ackQueue.push(pkt.get<lp::TxSequenceField>());
Eric Newberry32f7eac2020-02-07 14:40:17 -0800169
170 // Check for received frames with duplicate Sequences
171 if (pkt.has<lp::SequenceField>()) {
172 lp::Sequence pktSequence = pkt.get<lp::SequenceField>();
173 isDuplicate = m_recentRecvSeqs.count(pktSequence) > 0;
174 // Check for recent received Sequences to remove
175 auto now = time::steady_clock::now();
176 auto rto = m_rttEst.getEstimatedRto();
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500177 while (!m_recentRecvSeqsQueue.empty() &&
Eric Newberry32f7eac2020-02-07 14:40:17 -0800178 now > m_recentRecvSeqs[m_recentRecvSeqsQueue.front()] + rto) {
179 m_recentRecvSeqs.erase(m_recentRecvSeqsQueue.front());
180 m_recentRecvSeqsQueue.pop();
181 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400182 m_recentRecvSeqs.try_emplace(pktSequence, now);
Eric Newberry32f7eac2020-02-07 14:40:17 -0800183 m_recentRecvSeqsQueue.push(pktSequence);
184 }
185
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400186 startIdleAckTimer();
Eric Newberry185ab292017-03-28 06:45:39 +0000187 }
Eric Newberry32f7eac2020-02-07 14:40:17 -0800188
189 return !isDuplicate;
Eric Newberry185ab292017-03-28 06:45:39 +0000190}
191
192void
193LpReliability::piggyback(lp::Packet& pkt, ssize_t mtu)
194{
195 BOOST_ASSERT(m_options.isEnabled);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000196 BOOST_ASSERT(pkt.wireEncode().type() == lp::tlv::LpPacket);
Eric Newberry185ab292017-03-28 06:45:39 +0000197
Eric Newberry7b0071e2017-07-03 17:33:31 +0000198 // up to 2 extra octets reserved for potential TLV-LENGTH size increases
199 ssize_t pktSize = pkt.wireEncode().size();
200 ssize_t reservedSpace = tlv::sizeOfVarNumber(ndn::MAX_NDN_PACKET_SIZE) -
201 tlv::sizeOfVarNumber(pktSize);
202 ssize_t remainingSpace = (mtu == MTU_UNLIMITED ? ndn::MAX_NDN_PACKET_SIZE : mtu) - reservedSpace;
203 remainingSpace -= pktSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000204
Eric Newberry7b0071e2017-07-03 17:33:31 +0000205 while (!m_ackQueue.empty()) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800206 lp::Sequence ackTxSeq = m_ackQueue.front();
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400207 // Ack size = Ack TLV-TYPE (3 octets) + TLV-LENGTH (1 octet) + lp::Sequence (8 octets)
Junxiao Shi21e01932018-04-21 10:39:05 +0000208 const ssize_t ackSize = tlv::sizeOfVarNumber(lp::tlv::Ack) +
209 tlv::sizeOfVarNumber(sizeof(lp::Sequence)) +
210 sizeof(lp::Sequence);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000211
212 if (ackSize > remainingSpace) {
213 break;
214 }
215
Eric Newberry32f7eac2020-02-07 14:40:17 -0800216 NFD_LOG_FACE_TRACE("piggybacking ack for remote txseq=" << ackTxSeq);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800217
Eric Newberry32f7eac2020-02-07 14:40:17 -0800218 pkt.add<lp::AckField>(ackTxSeq);
Eric Newberry185ab292017-03-28 06:45:39 +0000219 m_ackQueue.pop();
Eric Newberry7b0071e2017-07-03 17:33:31 +0000220 remainingSpace -= ackSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000221 }
222}
223
Eric Newberry7b0071e2017-07-03 17:33:31 +0000224lp::Sequence
225LpReliability::assignTxSequence(lp::Packet& frag)
226{
227 lp::Sequence txSeq = ++m_lastTxSeqNo;
228 frag.set<lp::TxSequenceField>(txSeq);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500229 if (!m_unackedFrags.empty() && m_lastTxSeqNo == m_firstUnackedFrag->first) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500230 NDN_THROW(std::length_error("TxSequence range exceeded"));
Eric Newberry7b0071e2017-07-03 17:33:31 +0000231 }
232 return m_lastTxSeqNo;
233}
234
Eric Newberry185ab292017-03-28 06:45:39 +0000235void
236LpReliability::startIdleAckTimer()
237{
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400238 if (m_idleAckTimer) {
239 // timer is already running, do nothing
240 return;
241 }
Eric Newberry185ab292017-03-28 06:45:39 +0000242
Davide Pesavento3dade002019-03-19 11:29:56 -0600243 m_idleAckTimer = getScheduler().schedule(m_options.idleAckTimerPeriod, [this] {
Eric Newberry185ab292017-03-28 06:45:39 +0000244 while (!m_ackQueue.empty()) {
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700245 m_linkService->requestIdlePacket();
Eric Newberry185ab292017-03-28 06:45:39 +0000246 }
Eric Newberry185ab292017-03-28 06:45:39 +0000247 });
248}
249
Eric Newberry971d9622018-03-30 23:29:26 -0700250std::vector<lp::Sequence>
Eric Newberry7b0071e2017-07-03 17:33:31 +0000251LpReliability::findLostLpPackets(LpReliability::UnackedFrags::iterator ackIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000252{
Eric Newberry971d9622018-03-30 23:29:26 -0700253 std::vector<lp::Sequence> lostLpPackets;
Eric Newberry185ab292017-03-28 06:45:39 +0000254
255 for (auto it = m_firstUnackedFrag; ; ++it) {
256 if (it == m_unackedFrags.end()) {
257 it = m_unackedFrags.begin();
258 }
259
Eric Newberry7b0071e2017-07-03 17:33:31 +0000260 if (it->first == ackIt->first) {
Eric Newberry185ab292017-03-28 06:45:39 +0000261 break;
262 }
263
264 auto& unackedFrag = it->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000265 unackedFrag.nGreaterSeqAcks++;
Eric Newberry32f7eac2020-02-07 14:40:17 -0800266 NFD_LOG_FACE_TRACE("received ack=" << ackIt->first << " before=" << it->first <<
267 ", before count=" << unackedFrag.nGreaterSeqAcks);
Eric Newberry185ab292017-03-28 06:45:39 +0000268
Eric Newberry7b0071e2017-07-03 17:33:31 +0000269 if (unackedFrag.nGreaterSeqAcks >= m_options.seqNumLossThreshold) {
Eric Newberry971d9622018-03-30 23:29:26 -0700270 lostLpPackets.push_back(it->first);
Eric Newberry185ab292017-03-28 06:45:39 +0000271 }
272 }
273
274 return lostLpPackets;
275}
276
Eric Newberry971d9622018-03-30 23:29:26 -0700277std::vector<lp::Sequence>
Eric Newberry32f7eac2020-02-07 14:40:17 -0800278LpReliability::onLpPacketLost(lp::Sequence txSeq, bool isTimeout)
Eric Newberry185ab292017-03-28 06:45:39 +0000279{
Eric Newberry971d9622018-03-30 23:29:26 -0700280 BOOST_ASSERT(m_unackedFrags.count(txSeq) > 0);
281 auto txSeqIt = m_unackedFrags.find(txSeq);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000282
283 auto& txFrag = txSeqIt->second;
284 txFrag.rtoTimer.cancel();
285 auto netPkt = txFrag.netPkt;
Eric Newberry971d9622018-03-30 23:29:26 -0700286 std::vector<lp::Sequence> removedThisTxSeq;
Eric Newberry32f7eac2020-02-07 14:40:17 -0800287 lp::Sequence seq = txFrag.pkt.get<lp::SequenceField>();
288
289 if (isTimeout) {
290 NFD_LOG_FACE_TRACE("rto timer expired for seq=" << seq << ", txseq=" << txSeq);
291 }
292 else { // lost due to out-of-order TxSeqs
293 NFD_LOG_FACE_TRACE("seq=" << seq << ", txseq=" << txSeq <<
294 " considered lost from acks for more recent txseqs");
295 }
Eric Newberry185ab292017-03-28 06:45:39 +0000296
297 // Check if maximum number of retransmissions exceeded
298 if (txFrag.retxCount >= m_options.maxRetx) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800299 NFD_LOG_FACE_DEBUG("seq=" << seq << " exceeded allowed retransmissions: DROP");
Eric Newberry7b0071e2017-07-03 17:33:31 +0000300 // Delete all LpPackets of NetPkt from m_unackedFrags (except this one)
301 for (size_t i = 0; i < netPkt->unackedFrags.size(); i++) {
302 if (netPkt->unackedFrags[i] != txSeqIt) {
Eric Newberry971d9622018-03-30 23:29:26 -0700303 removedThisTxSeq.push_back(netPkt->unackedFrags[i]->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000304 deleteUnackedFrag(netPkt->unackedFrags[i]);
305 }
Eric Newberry185ab292017-03-28 06:45:39 +0000306 }
Eric Newberry185ab292017-03-28 06:45:39 +0000307
308 ++m_linkService->nRetxExhausted;
Eric Newberry41aba102017-11-01 16:42:13 -0700309
310 // Notify strategy of dropped Interest (if any)
311 if (netPkt->isInterest) {
312 BOOST_ASSERT(netPkt->pkt.has<lp::FragmentField>());
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500313 auto frag = netPkt->pkt.get<lp::FragmentField>();
314 onDroppedInterest(Interest(Block({frag.first, frag.second})));
Eric Newberry41aba102017-11-01 16:42:13 -0700315 }
316
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800317 // Delete this LpPacket from m_unackedFrags
Eric Newberry971d9622018-03-30 23:29:26 -0700318 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000319 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000320 }
321 else {
Eric Newberry7b0071e2017-07-03 17:33:31 +0000322 // Assign new TxSequence
323 lp::Sequence newTxSeq = assignTxSequence(txFrag.pkt);
Eric Newberry00d39fd2017-12-10 14:26:45 -0700324 netPkt->didRetx = true;
Eric Newberry185ab292017-03-28 06:45:39 +0000325
Eric Newberry7b0071e2017-07-03 17:33:31 +0000326 // Move fragment to new TxSequence mapping
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400327 auto hint = m_firstUnackedFrag != m_unackedFrags.end() && m_firstUnackedFrag->first > newTxSeq
328 ? m_firstUnackedFrag
329 : m_unackedFrags.end();
330 auto newTxFragIt = m_unackedFrags.try_emplace(hint, newTxSeq, txFrag.pkt);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000331 auto& newTxFrag = newTxFragIt->second;
332 newTxFrag.retxCount = txFrag.retxCount + 1;
333 newTxFrag.netPkt = netPkt;
334
335 // Update associated NetPkt
336 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), txSeqIt);
337 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
338 *fragInNetPkt = newTxFragIt;
339
Eric Newberry971d9622018-03-30 23:29:26 -0700340 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000341 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000342
343 // Retransmit fragment
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700344 m_linkService->sendLpPacket(lp::Packet(newTxFrag.pkt));
Eric Newberry7b0071e2017-07-03 17:33:31 +0000345
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800346 auto rto = m_rttEst.getEstimatedRto();
Eric Newberry32f7eac2020-02-07 14:40:17 -0800347 NFD_LOG_FACE_TRACE("retransmitting seq=" << seq << ", txseq=" << newTxSeq << ", retx=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800348 txFrag.retxCount << ", rto=" <<
349 time::duration_cast<time::milliseconds>(rto).count() << "ms");
350
Eric Newberry7b0071e2017-07-03 17:33:31 +0000351 // Start RTO timer for this sequence
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800352 newTxFrag.rtoTimer = getScheduler().schedule(rto, [=] {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800353 onLpPacketLost(newTxSeq, true);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800354 });
Eric Newberry185ab292017-03-28 06:45:39 +0000355 }
Eric Newberry971d9622018-03-30 23:29:26 -0700356
357 return removedThisTxSeq;
Eric Newberry185ab292017-03-28 06:45:39 +0000358}
359
360void
Eric Newberry7b0071e2017-07-03 17:33:31 +0000361LpReliability::onLpPacketAcknowledged(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000362{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000363 auto netPkt = fragIt->second.netPkt;
Eric Newberry185ab292017-03-28 06:45:39 +0000364
Eric Newberry7b0071e2017-07-03 17:33:31 +0000365 // Remove from NetPkt unacked fragment list
366 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), fragIt);
367 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
368 *fragInNetPkt = netPkt->unackedFrags.back();
369 netPkt->unackedFrags.pop_back();
Eric Newberry185ab292017-03-28 06:45:39 +0000370
Eric Newberry7b0071e2017-07-03 17:33:31 +0000371 // Check if network-layer packet completely received. If so, increment counters
372 if (netPkt->unackedFrags.empty()) {
373 if (netPkt->didRetx) {
Eric Newberry185ab292017-03-28 06:45:39 +0000374 ++m_linkService->nRetransmitted;
375 }
376 else {
377 ++m_linkService->nAcknowledged;
378 }
Eric Newberry185ab292017-03-28 06:45:39 +0000379 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000380
381 deleteUnackedFrag(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000382}
383
Eric Newberry7b0071e2017-07-03 17:33:31 +0000384void
385LpReliability::deleteUnackedFrag(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000386{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000387 lp::Sequence firstUnackedTxSeq = m_firstUnackedFrag->first;
388 lp::Sequence currentTxSeq = fragIt->first;
389 auto nextFragIt = m_unackedFrags.erase(fragIt);
390
391 if (!m_unackedFrags.empty() && firstUnackedTxSeq == currentTxSeq) {
392 // If "first" fragment in send window (allowing for wraparound), increment window begin
393 if (nextFragIt == m_unackedFrags.end()) {
394 m_firstUnackedFrag = m_unackedFrags.begin();
395 }
396 else {
397 m_firstUnackedFrag = nextFragIt;
398 }
Eric Newberry185ab292017-03-28 06:45:39 +0000399 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000400 else if (m_unackedFrags.empty()) {
401 m_firstUnackedFrag = m_unackedFrags.end();
402 }
Eric Newberry185ab292017-03-28 06:45:39 +0000403}
404
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800405std::ostream&
406operator<<(std::ostream& os, const FaceLogHelper<LpReliability>& flh)
407{
408 if (flh.obj.getLinkService() == nullptr) {
409 os << "[id=0,local=unknown,remote=unknown] ";
410 }
411 else {
412 os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
413 }
414 return os;
415}
416
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400417} // namespace nfd::face