blob: b287bc31472e0b38e06dab8f11fce71ee750a22e [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
31namespace nfd {
32namespace face {
33
Eric Newberry5ab4cf82020-02-03 15:40:16 -080034NFD_LOG_INIT(LpReliability);
35
Eric Newberry185ab292017-03-28 06:45:39 +000036LpReliability::LpReliability(const LpReliability::Options& options, GenericLinkService* linkService)
37 : m_options(options)
38 , m_linkService(linkService)
39 , m_firstUnackedFrag(m_unackedFrags.begin())
Eric Newberry7b0071e2017-07-03 17:33:31 +000040 , m_lastTxSeqNo(-1) // set to "-1" to start TxSequence numbers at 0
Eric Newberry185ab292017-03-28 06:45:39 +000041{
42 BOOST_ASSERT(m_linkService != nullptr);
Davide Pesaventoe4b22382018-06-10 14:37:24 -040043 BOOST_ASSERT(m_options.idleAckTimerPeriod > 0_ns);
Eric Newberry185ab292017-03-28 06:45:39 +000044}
45
46void
47LpReliability::setOptions(const Options& options)
48{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040049 BOOST_ASSERT(options.idleAckTimerPeriod > 0_ns);
Eric Newberry185ab292017-03-28 06:45:39 +000050
51 if (m_options.isEnabled && !options.isEnabled) {
Davide Pesaventof190cfa2019-07-17 20:14:11 -040052 m_idleAckTimer.cancel();
Eric Newberry185ab292017-03-28 06:45:39 +000053 }
54
55 m_options = options;
56}
57
58const GenericLinkService*
59LpReliability::getLinkService() const
60{
61 return m_linkService;
62}
63
64void
Eric Newberry41aba102017-11-01 16:42:13 -070065LpReliability::handleOutgoing(std::vector<lp::Packet>& frags, lp::Packet&& pkt, bool isInterest)
Eric Newberry185ab292017-03-28 06:45:39 +000066{
67 BOOST_ASSERT(m_options.isEnabled);
68
Eric Newberry185ab292017-03-28 06:45:39 +000069 auto unackedFragsIt = m_unackedFrags.begin();
Eric Newberry7b0071e2017-07-03 17:33:31 +000070 auto sendTime = time::steady_clock::now();
Eric Newberry185ab292017-03-28 06:45:39 +000071
Eric Newberry41aba102017-11-01 16:42:13 -070072 auto netPkt = make_shared<NetPkt>(std::move(pkt), isInterest);
Eric Newberry7b0071e2017-07-03 17:33:31 +000073 netPkt->unackedFrags.reserve(frags.size());
74
75 for (lp::Packet& frag : frags) {
Eric Newberry32f7eac2020-02-07 14:40:17 -080076 // Non-IDLE packets are required to have assigned Sequence numbers with LpReliability enabled
77 BOOST_ASSERT(frag.has<lp::SequenceField>());
78
Eric Newberry7b0071e2017-07-03 17:33:31 +000079 // Assign TxSequence number
80 lp::Sequence txSeq = assignTxSequence(frag);
81
Eric Newberry185ab292017-03-28 06:45:39 +000082 // Store LpPacket for future retransmissions
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040083 unackedFragsIt = m_unackedFrags.try_emplace(unackedFragsIt, txSeq, frag);
Eric Newberry7b0071e2017-07-03 17:33:31 +000084 unackedFragsIt->second.sendTime = sendTime;
Eric Newberry5ab4cf82020-02-03 15:40:16 -080085 auto rto = m_rttEst.getEstimatedRto();
Eric Newberry32f7eac2020-02-07 14:40:17 -080086 lp::Sequence seq = frag.get<lp::SequenceField>();
87 NFD_LOG_FACE_TRACE("transmitting seq=" << seq << ", txseq=" << txSeq << ", rto=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -080088 time::duration_cast<time::milliseconds>(rto).count() << "ms");
89 unackedFragsIt->second.rtoTimer = getScheduler().schedule(rto, [=] {
Eric Newberry32f7eac2020-02-07 14:40:17 -080090 onLpPacketLost(txSeq, true);
Eric Newberry5ab4cf82020-02-03 15:40:16 -080091 });
Eric Newberry7b0071e2017-07-03 17:33:31 +000092 unackedFragsIt->second.netPkt = netPkt;
93
Eric Newberry185ab292017-03-28 06:45:39 +000094 if (m_unackedFrags.size() == 1) {
Eric Newberry7b0071e2017-07-03 17:33:31 +000095 m_firstUnackedFrag = m_unackedFrags.begin();
Eric Newberry185ab292017-03-28 06:45:39 +000096 }
Eric Newberry7b0071e2017-07-03 17:33:31 +000097
98 // Add to associated NetPkt
99 netPkt->unackedFrags.push_back(unackedFragsIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000100 }
101}
102
Eric Newberry32f7eac2020-02-07 14:40:17 -0800103bool
Eric Newberry185ab292017-03-28 06:45:39 +0000104LpReliability::processIncomingPacket(const lp::Packet& pkt)
105{
106 BOOST_ASSERT(m_options.isEnabled);
107
Eric Newberry32f7eac2020-02-07 14:40:17 -0800108 bool isDuplicate = false;
Eric Newberry185ab292017-03-28 06:45:39 +0000109 auto now = time::steady_clock::now();
110
111 // Extract and parse Acks
Eric Newberry32f7eac2020-02-07 14:40:17 -0800112 for (lp::Sequence ackTxSeq : pkt.list<lp::AckField>()) {
113 auto fragIt = m_unackedFrags.find(ackTxSeq);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000114 if (fragIt == m_unackedFrags.end()) {
115 // Ignore an Ack for an unknown TxSequence number
Eric Newberry32f7eac2020-02-07 14:40:17 -0800116 NFD_LOG_FACE_DEBUG("received ack for unknown txseq=" << ackTxSeq);
Eric Newberry185ab292017-03-28 06:45:39 +0000117 continue;
118 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000119 auto& frag = fragIt->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000120
121 // Cancel the RTO timer for the acknowledged fragment
Eric Newberry7b0071e2017-07-03 17:33:31 +0000122 frag.rtoTimer.cancel();
Eric Newberry185ab292017-03-28 06:45:39 +0000123
Eric Newberry7b0071e2017-07-03 17:33:31 +0000124 if (frag.retxCount == 0) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800125 NFD_LOG_FACE_TRACE("received ack for seq=" << frag.pkt.get<lp::SequenceField>() << ", txseq=" <<
126 ackTxSeq << ", retx=0, rtt=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800127 time::duration_cast<time::milliseconds>(now - frag.sendTime).count() << "ms");
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400128 // This sequence had no retransmissions, so use it to estimate the RTO
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400129 m_rttEst.addMeasurement(now - frag.sendTime);
Eric Newberry185ab292017-03-28 06:45:39 +0000130 }
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800131 else {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800132 NFD_LOG_FACE_TRACE("received ack for seq=" << frag.pkt.get<lp::SequenceField>() << ", txseq=" <<
133 ackTxSeq << ", retx=" << frag.retxCount);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800134 }
Eric Newberry185ab292017-03-28 06:45:39 +0000135
Eric Newberry32f7eac2020-02-07 14:40:17 -0800136 // Look for frags with TxSequence numbers < ackTxSeq (allowing for wraparound) and consider
137 // them lost if a configurable number of Acks containing greater TxSequence numbers have been
Eric Newberry7b0071e2017-07-03 17:33:31 +0000138 // received.
139 auto lostLpPackets = findLostLpPackets(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000140
Eric Newberry7b0071e2017-07-03 17:33:31 +0000141 // Remove the fragment from the map of unacknowledged fragments and from its associated network
142 // packet. Potentially increment the start of the window.
143 onLpPacketAcknowledged(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000144
Eric Newberry971d9622018-03-30 23:29:26 -0700145 // This set contains TxSequences that have been removed by onLpPacketLost below because they
146 // were part of a network packet that was removed due to a fragment exceeding retx, as well as
147 // any other TxSequences removed by onLpPacketLost. This prevents onLpPacketLost from being
148 // called later for an invalid iterator.
149 std::set<lp::Sequence> removedLpPackets;
150
Eric Newberry7b0071e2017-07-03 17:33:31 +0000151 // Resend or fail fragments considered lost. Potentially increment the start of the window.
Eric Newberry971d9622018-03-30 23:29:26 -0700152 for (lp::Sequence txSeq : lostLpPackets) {
153 if (removedLpPackets.find(txSeq) == removedLpPackets.end()) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800154 auto removedTxSeqs = onLpPacketLost(txSeq, false);
155 for (auto removedTxSeq : removedTxSeqs) {
Eric Newberry971d9622018-03-30 23:29:26 -0700156 removedLpPackets.insert(removedTxSeq);
157 }
158 }
Eric Newberry185ab292017-03-28 06:45:39 +0000159 }
160 }
161
Eric Newberry7b0071e2017-07-03 17:33:31 +0000162 // If packet has Fragment and TxSequence fields, extract TxSequence and add to AckQueue
163 if (pkt.has<lp::FragmentField>() && pkt.has<lp::TxSequenceField>()) {
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800164 NFD_LOG_FACE_TRACE("queueing ack for remote txseq=" << pkt.get<lp::TxSequenceField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000165 m_ackQueue.push(pkt.get<lp::TxSequenceField>());
Eric Newberry32f7eac2020-02-07 14:40:17 -0800166
167 // Check for received frames with duplicate Sequences
168 if (pkt.has<lp::SequenceField>()) {
169 lp::Sequence pktSequence = pkt.get<lp::SequenceField>();
170 isDuplicate = m_recentRecvSeqs.count(pktSequence) > 0;
171 // Check for recent received Sequences to remove
172 auto now = time::steady_clock::now();
173 auto rto = m_rttEst.getEstimatedRto();
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500174 while (!m_recentRecvSeqsQueue.empty() &&
Eric Newberry32f7eac2020-02-07 14:40:17 -0800175 now > m_recentRecvSeqs[m_recentRecvSeqsQueue.front()] + rto) {
176 m_recentRecvSeqs.erase(m_recentRecvSeqsQueue.front());
177 m_recentRecvSeqsQueue.pop();
178 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400179 m_recentRecvSeqs.try_emplace(pktSequence, now);
Eric Newberry32f7eac2020-02-07 14:40:17 -0800180 m_recentRecvSeqsQueue.push(pktSequence);
181 }
182
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400183 startIdleAckTimer();
Eric Newberry185ab292017-03-28 06:45:39 +0000184 }
Eric Newberry32f7eac2020-02-07 14:40:17 -0800185
186 return !isDuplicate;
Eric Newberry185ab292017-03-28 06:45:39 +0000187}
188
189void
190LpReliability::piggyback(lp::Packet& pkt, ssize_t mtu)
191{
192 BOOST_ASSERT(m_options.isEnabled);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000193 BOOST_ASSERT(pkt.wireEncode().type() == lp::tlv::LpPacket);
Eric Newberry185ab292017-03-28 06:45:39 +0000194
Eric Newberry7b0071e2017-07-03 17:33:31 +0000195 // up to 2 extra octets reserved for potential TLV-LENGTH size increases
196 ssize_t pktSize = pkt.wireEncode().size();
197 ssize_t reservedSpace = tlv::sizeOfVarNumber(ndn::MAX_NDN_PACKET_SIZE) -
198 tlv::sizeOfVarNumber(pktSize);
199 ssize_t remainingSpace = (mtu == MTU_UNLIMITED ? ndn::MAX_NDN_PACKET_SIZE : mtu) - reservedSpace;
200 remainingSpace -= pktSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000201
Eric Newberry7b0071e2017-07-03 17:33:31 +0000202 while (!m_ackQueue.empty()) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800203 lp::Sequence ackTxSeq = m_ackQueue.front();
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400204 // Ack size = Ack TLV-TYPE (3 octets) + TLV-LENGTH (1 octet) + lp::Sequence (8 octets)
Junxiao Shi21e01932018-04-21 10:39:05 +0000205 const ssize_t ackSize = tlv::sizeOfVarNumber(lp::tlv::Ack) +
206 tlv::sizeOfVarNumber(sizeof(lp::Sequence)) +
207 sizeof(lp::Sequence);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000208
209 if (ackSize > remainingSpace) {
210 break;
211 }
212
Eric Newberry32f7eac2020-02-07 14:40:17 -0800213 NFD_LOG_FACE_TRACE("piggybacking ack for remote txseq=" << ackTxSeq);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800214
Eric Newberry32f7eac2020-02-07 14:40:17 -0800215 pkt.add<lp::AckField>(ackTxSeq);
Eric Newberry185ab292017-03-28 06:45:39 +0000216 m_ackQueue.pop();
Eric Newberry7b0071e2017-07-03 17:33:31 +0000217 remainingSpace -= ackSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000218 }
219}
220
Eric Newberry7b0071e2017-07-03 17:33:31 +0000221lp::Sequence
222LpReliability::assignTxSequence(lp::Packet& frag)
223{
224 lp::Sequence txSeq = ++m_lastTxSeqNo;
225 frag.set<lp::TxSequenceField>(txSeq);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500226 if (!m_unackedFrags.empty() && m_lastTxSeqNo == m_firstUnackedFrag->first) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500227 NDN_THROW(std::length_error("TxSequence range exceeded"));
Eric Newberry7b0071e2017-07-03 17:33:31 +0000228 }
229 return m_lastTxSeqNo;
230}
231
Eric Newberry185ab292017-03-28 06:45:39 +0000232void
233LpReliability::startIdleAckTimer()
234{
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400235 if (m_idleAckTimer) {
236 // timer is already running, do nothing
237 return;
238 }
Eric Newberry185ab292017-03-28 06:45:39 +0000239
Davide Pesavento3dade002019-03-19 11:29:56 -0600240 m_idleAckTimer = getScheduler().schedule(m_options.idleAckTimerPeriod, [this] {
Eric Newberry185ab292017-03-28 06:45:39 +0000241 while (!m_ackQueue.empty()) {
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700242 m_linkService->requestIdlePacket();
Eric Newberry185ab292017-03-28 06:45:39 +0000243 }
Eric Newberry185ab292017-03-28 06:45:39 +0000244 });
245}
246
Eric Newberry971d9622018-03-30 23:29:26 -0700247std::vector<lp::Sequence>
Eric Newberry7b0071e2017-07-03 17:33:31 +0000248LpReliability::findLostLpPackets(LpReliability::UnackedFrags::iterator ackIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000249{
Eric Newberry971d9622018-03-30 23:29:26 -0700250 std::vector<lp::Sequence> lostLpPackets;
Eric Newberry185ab292017-03-28 06:45:39 +0000251
252 for (auto it = m_firstUnackedFrag; ; ++it) {
253 if (it == m_unackedFrags.end()) {
254 it = m_unackedFrags.begin();
255 }
256
Eric Newberry7b0071e2017-07-03 17:33:31 +0000257 if (it->first == ackIt->first) {
Eric Newberry185ab292017-03-28 06:45:39 +0000258 break;
259 }
260
261 auto& unackedFrag = it->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000262 unackedFrag.nGreaterSeqAcks++;
Eric Newberry32f7eac2020-02-07 14:40:17 -0800263 NFD_LOG_FACE_TRACE("received ack=" << ackIt->first << " before=" << it->first <<
264 ", before count=" << unackedFrag.nGreaterSeqAcks);
Eric Newberry185ab292017-03-28 06:45:39 +0000265
Eric Newberry7b0071e2017-07-03 17:33:31 +0000266 if (unackedFrag.nGreaterSeqAcks >= m_options.seqNumLossThreshold) {
Eric Newberry971d9622018-03-30 23:29:26 -0700267 lostLpPackets.push_back(it->first);
Eric Newberry185ab292017-03-28 06:45:39 +0000268 }
269 }
270
271 return lostLpPackets;
272}
273
Eric Newberry971d9622018-03-30 23:29:26 -0700274std::vector<lp::Sequence>
Eric Newberry32f7eac2020-02-07 14:40:17 -0800275LpReliability::onLpPacketLost(lp::Sequence txSeq, bool isTimeout)
Eric Newberry185ab292017-03-28 06:45:39 +0000276{
Eric Newberry971d9622018-03-30 23:29:26 -0700277 BOOST_ASSERT(m_unackedFrags.count(txSeq) > 0);
278 auto txSeqIt = m_unackedFrags.find(txSeq);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000279
280 auto& txFrag = txSeqIt->second;
281 txFrag.rtoTimer.cancel();
282 auto netPkt = txFrag.netPkt;
Eric Newberry971d9622018-03-30 23:29:26 -0700283 std::vector<lp::Sequence> removedThisTxSeq;
Eric Newberry32f7eac2020-02-07 14:40:17 -0800284 lp::Sequence seq = txFrag.pkt.get<lp::SequenceField>();
285
286 if (isTimeout) {
287 NFD_LOG_FACE_TRACE("rto timer expired for seq=" << seq << ", txseq=" << txSeq);
288 }
289 else { // lost due to out-of-order TxSeqs
290 NFD_LOG_FACE_TRACE("seq=" << seq << ", txseq=" << txSeq <<
291 " considered lost from acks for more recent txseqs");
292 }
Eric Newberry185ab292017-03-28 06:45:39 +0000293
294 // Check if maximum number of retransmissions exceeded
295 if (txFrag.retxCount >= m_options.maxRetx) {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800296 NFD_LOG_FACE_DEBUG("seq=" << seq << " exceeded allowed retransmissions: DROP");
Eric Newberry7b0071e2017-07-03 17:33:31 +0000297 // Delete all LpPackets of NetPkt from m_unackedFrags (except this one)
298 for (size_t i = 0; i < netPkt->unackedFrags.size(); i++) {
299 if (netPkt->unackedFrags[i] != txSeqIt) {
Eric Newberry971d9622018-03-30 23:29:26 -0700300 removedThisTxSeq.push_back(netPkt->unackedFrags[i]->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000301 deleteUnackedFrag(netPkt->unackedFrags[i]);
302 }
Eric Newberry185ab292017-03-28 06:45:39 +0000303 }
Eric Newberry185ab292017-03-28 06:45:39 +0000304
305 ++m_linkService->nRetxExhausted;
Eric Newberry41aba102017-11-01 16:42:13 -0700306
307 // Notify strategy of dropped Interest (if any)
308 if (netPkt->isInterest) {
309 BOOST_ASSERT(netPkt->pkt.has<lp::FragmentField>());
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500310 auto frag = netPkt->pkt.get<lp::FragmentField>();
311 onDroppedInterest(Interest(Block({frag.first, frag.second})));
Eric Newberry41aba102017-11-01 16:42:13 -0700312 }
313
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800314 // Delete this LpPacket from m_unackedFrags
Eric Newberry971d9622018-03-30 23:29:26 -0700315 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000316 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000317 }
318 else {
Eric Newberry7b0071e2017-07-03 17:33:31 +0000319 // Assign new TxSequence
320 lp::Sequence newTxSeq = assignTxSequence(txFrag.pkt);
Eric Newberry00d39fd2017-12-10 14:26:45 -0700321 netPkt->didRetx = true;
Eric Newberry185ab292017-03-28 06:45:39 +0000322
Eric Newberry7b0071e2017-07-03 17:33:31 +0000323 // Move fragment to new TxSequence mapping
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400324 auto hint = m_firstUnackedFrag != m_unackedFrags.end() && m_firstUnackedFrag->first > newTxSeq
325 ? m_firstUnackedFrag
326 : m_unackedFrags.end();
327 auto newTxFragIt = m_unackedFrags.try_emplace(hint, newTxSeq, txFrag.pkt);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000328 auto& newTxFrag = newTxFragIt->second;
329 newTxFrag.retxCount = txFrag.retxCount + 1;
330 newTxFrag.netPkt = netPkt;
331
332 // Update associated NetPkt
333 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), txSeqIt);
334 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
335 *fragInNetPkt = newTxFragIt;
336
Eric Newberry971d9622018-03-30 23:29:26 -0700337 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000338 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000339
340 // Retransmit fragment
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700341 m_linkService->sendLpPacket(lp::Packet(newTxFrag.pkt));
Eric Newberry7b0071e2017-07-03 17:33:31 +0000342
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800343 auto rto = m_rttEst.getEstimatedRto();
Eric Newberry32f7eac2020-02-07 14:40:17 -0800344 NFD_LOG_FACE_TRACE("retransmitting seq=" << seq << ", txseq=" << newTxSeq << ", retx=" <<
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800345 txFrag.retxCount << ", rto=" <<
346 time::duration_cast<time::milliseconds>(rto).count() << "ms");
347
Eric Newberry7b0071e2017-07-03 17:33:31 +0000348 // Start RTO timer for this sequence
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800349 newTxFrag.rtoTimer = getScheduler().schedule(rto, [=] {
Eric Newberry32f7eac2020-02-07 14:40:17 -0800350 onLpPacketLost(newTxSeq, true);
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800351 });
Eric Newberry185ab292017-03-28 06:45:39 +0000352 }
Eric Newberry971d9622018-03-30 23:29:26 -0700353
354 return removedThisTxSeq;
Eric Newberry185ab292017-03-28 06:45:39 +0000355}
356
357void
Eric Newberry7b0071e2017-07-03 17:33:31 +0000358LpReliability::onLpPacketAcknowledged(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000359{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000360 auto netPkt = fragIt->second.netPkt;
Eric Newberry185ab292017-03-28 06:45:39 +0000361
Eric Newberry7b0071e2017-07-03 17:33:31 +0000362 // Remove from NetPkt unacked fragment list
363 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), fragIt);
364 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
365 *fragInNetPkt = netPkt->unackedFrags.back();
366 netPkt->unackedFrags.pop_back();
Eric Newberry185ab292017-03-28 06:45:39 +0000367
Eric Newberry7b0071e2017-07-03 17:33:31 +0000368 // Check if network-layer packet completely received. If so, increment counters
369 if (netPkt->unackedFrags.empty()) {
370 if (netPkt->didRetx) {
Eric Newberry185ab292017-03-28 06:45:39 +0000371 ++m_linkService->nRetransmitted;
372 }
373 else {
374 ++m_linkService->nAcknowledged;
375 }
Eric Newberry185ab292017-03-28 06:45:39 +0000376 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000377
378 deleteUnackedFrag(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000379}
380
Eric Newberry7b0071e2017-07-03 17:33:31 +0000381void
382LpReliability::deleteUnackedFrag(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000383{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000384 lp::Sequence firstUnackedTxSeq = m_firstUnackedFrag->first;
385 lp::Sequence currentTxSeq = fragIt->first;
386 auto nextFragIt = m_unackedFrags.erase(fragIt);
387
388 if (!m_unackedFrags.empty() && firstUnackedTxSeq == currentTxSeq) {
389 // If "first" fragment in send window (allowing for wraparound), increment window begin
390 if (nextFragIt == m_unackedFrags.end()) {
391 m_firstUnackedFrag = m_unackedFrags.begin();
392 }
393 else {
394 m_firstUnackedFrag = nextFragIt;
395 }
Eric Newberry185ab292017-03-28 06:45:39 +0000396 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000397 else if (m_unackedFrags.empty()) {
398 m_firstUnackedFrag = m_unackedFrags.end();
399 }
Eric Newberry185ab292017-03-28 06:45:39 +0000400}
401
402LpReliability::UnackedFrag::UnackedFrag(lp::Packet pkt)
403 : pkt(std::move(pkt))
404 , sendTime(time::steady_clock::now())
405 , retxCount(0)
406 , nGreaterSeqAcks(0)
Eric Newberry185ab292017-03-28 06:45:39 +0000407{
408}
409
Eric Newberry41aba102017-11-01 16:42:13 -0700410LpReliability::NetPkt::NetPkt(lp::Packet&& pkt, bool isInterest)
411 : pkt(std::move(pkt))
412 , isInterest(isInterest)
413 , didRetx(false)
414{
415}
416
Eric Newberry5ab4cf82020-02-03 15:40:16 -0800417std::ostream&
418operator<<(std::ostream& os, const FaceLogHelper<LpReliability>& flh)
419{
420 if (flh.obj.getLinkService() == nullptr) {
421 os << "[id=0,local=unknown,remote=unknown] ";
422 }
423 else {
424 os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
425 }
426 return os;
427}
428
Eric Newberry185ab292017-03-28 06:45:39 +0000429} // namespace face
430} // namespace nfd