blob: 310f78e8a7c91b593c658f09e772b871d3cd67d6 [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 Pesavento19779d82019-02-14 13:40:04 -05003 * Copyright (c) 2014-2019, 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
34LpReliability::LpReliability(const LpReliability::Options& options, GenericLinkService* linkService)
35 : m_options(options)
36 , m_linkService(linkService)
37 , m_firstUnackedFrag(m_unackedFrags.begin())
Eric Newberry7b0071e2017-07-03 17:33:31 +000038 , m_lastTxSeqNo(-1) // set to "-1" to start TxSequence numbers at 0
Eric Newberry185ab292017-03-28 06:45:39 +000039{
40 BOOST_ASSERT(m_linkService != nullptr);
Davide Pesaventoe4b22382018-06-10 14:37:24 -040041 BOOST_ASSERT(m_options.idleAckTimerPeriod > 0_ns);
Eric Newberry185ab292017-03-28 06:45:39 +000042}
43
44void
45LpReliability::setOptions(const Options& options)
46{
Davide Pesaventoe4b22382018-06-10 14:37:24 -040047 BOOST_ASSERT(options.idleAckTimerPeriod > 0_ns);
Eric Newberry185ab292017-03-28 06:45:39 +000048
49 if (m_options.isEnabled && !options.isEnabled) {
Davide Pesaventof190cfa2019-07-17 20:14:11 -040050 m_idleAckTimer.cancel();
Eric Newberry185ab292017-03-28 06:45:39 +000051 }
52
53 m_options = options;
54}
55
56const GenericLinkService*
57LpReliability::getLinkService() const
58{
59 return m_linkService;
60}
61
62void
Eric Newberry41aba102017-11-01 16:42:13 -070063LpReliability::handleOutgoing(std::vector<lp::Packet>& frags, lp::Packet&& pkt, bool isInterest)
Eric Newberry185ab292017-03-28 06:45:39 +000064{
65 BOOST_ASSERT(m_options.isEnabled);
66
Eric Newberry185ab292017-03-28 06:45:39 +000067 auto unackedFragsIt = m_unackedFrags.begin();
Eric Newberry7b0071e2017-07-03 17:33:31 +000068 auto sendTime = time::steady_clock::now();
Eric Newberry185ab292017-03-28 06:45:39 +000069
Eric Newberry41aba102017-11-01 16:42:13 -070070 auto netPkt = make_shared<NetPkt>(std::move(pkt), isInterest);
Eric Newberry7b0071e2017-07-03 17:33:31 +000071 netPkt->unackedFrags.reserve(frags.size());
72
73 for (lp::Packet& frag : frags) {
74 // Assign TxSequence number
75 lp::Sequence txSeq = assignTxSequence(frag);
76
Eric Newberry185ab292017-03-28 06:45:39 +000077 // Store LpPacket for future retransmissions
Eric Newberry7b0071e2017-07-03 17:33:31 +000078 unackedFragsIt = m_unackedFrags.emplace_hint(unackedFragsIt,
79 std::piecewise_construct,
80 std::forward_as_tuple(txSeq),
81 std::forward_as_tuple(frag));
82 unackedFragsIt->second.sendTime = sendTime;
Davide Pesaventof190cfa2019-07-17 20:14:11 -040083 unackedFragsIt->second.rtoTimer = getScheduler().schedule(m_rttEst.getEstimatedRto(),
Davide Pesavento3dade002019-03-19 11:29:56 -060084 [=] { onLpPacketLost(txSeq); });
Eric Newberry7b0071e2017-07-03 17:33:31 +000085 unackedFragsIt->second.netPkt = netPkt;
86
Eric Newberry185ab292017-03-28 06:45:39 +000087 if (m_unackedFrags.size() == 1) {
Eric Newberry7b0071e2017-07-03 17:33:31 +000088 m_firstUnackedFrag = m_unackedFrags.begin();
Eric Newberry185ab292017-03-28 06:45:39 +000089 }
Eric Newberry7b0071e2017-07-03 17:33:31 +000090
91 // Add to associated NetPkt
92 netPkt->unackedFrags.push_back(unackedFragsIt);
Eric Newberry185ab292017-03-28 06:45:39 +000093 }
94}
95
96void
97LpReliability::processIncomingPacket(const lp::Packet& pkt)
98{
99 BOOST_ASSERT(m_options.isEnabled);
100
101 auto now = time::steady_clock::now();
102
103 // Extract and parse Acks
104 for (lp::Sequence ackSeq : pkt.list<lp::AckField>()) {
Eric Newberry7b0071e2017-07-03 17:33:31 +0000105 auto fragIt = m_unackedFrags.find(ackSeq);
106 if (fragIt == m_unackedFrags.end()) {
107 // Ignore an Ack for an unknown TxSequence number
Eric Newberry185ab292017-03-28 06:45:39 +0000108 continue;
109 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000110 auto& frag = fragIt->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000111
112 // Cancel the RTO timer for the acknowledged fragment
Eric Newberry7b0071e2017-07-03 17:33:31 +0000113 frag.rtoTimer.cancel();
Eric Newberry185ab292017-03-28 06:45:39 +0000114
Eric Newberry7b0071e2017-07-03 17:33:31 +0000115 if (frag.retxCount == 0) {
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400116 // This sequence had no retransmissions, so use it to estimate the RTO
Davide Pesaventoeb7b7ab2019-08-14 19:00:15 -0400117 m_rttEst.addMeasurement(now - frag.sendTime);
Eric Newberry185ab292017-03-28 06:45:39 +0000118 }
119
Eric Newberry7b0071e2017-07-03 17:33:31 +0000120 // Look for frags with TxSequence numbers < ackSeq (allowing for wraparound) and consider them
121 // lost if a configurable number of Acks containing greater TxSequence numbers have been
122 // received.
123 auto lostLpPackets = findLostLpPackets(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000124
Eric Newberry7b0071e2017-07-03 17:33:31 +0000125 // Remove the fragment from the map of unacknowledged fragments and from its associated network
126 // packet. Potentially increment the start of the window.
127 onLpPacketAcknowledged(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000128
Eric Newberry971d9622018-03-30 23:29:26 -0700129 // This set contains TxSequences that have been removed by onLpPacketLost below because they
130 // were part of a network packet that was removed due to a fragment exceeding retx, as well as
131 // any other TxSequences removed by onLpPacketLost. This prevents onLpPacketLost from being
132 // called later for an invalid iterator.
133 std::set<lp::Sequence> removedLpPackets;
134
Eric Newberry7b0071e2017-07-03 17:33:31 +0000135 // Resend or fail fragments considered lost. Potentially increment the start of the window.
Eric Newberry971d9622018-03-30 23:29:26 -0700136 for (lp::Sequence txSeq : lostLpPackets) {
137 if (removedLpPackets.find(txSeq) == removedLpPackets.end()) {
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400138 auto removedThisTxSeq = onLpPacketLost(txSeq);
Eric Newberry971d9622018-03-30 23:29:26 -0700139 for (auto removedTxSeq : removedThisTxSeq) {
140 removedLpPackets.insert(removedTxSeq);
141 }
142 }
Eric Newberry185ab292017-03-28 06:45:39 +0000143 }
144 }
145
Eric Newberry7b0071e2017-07-03 17:33:31 +0000146 // If packet has Fragment and TxSequence fields, extract TxSequence and add to AckQueue
147 if (pkt.has<lp::FragmentField>() && pkt.has<lp::TxSequenceField>()) {
148 m_ackQueue.push(pkt.get<lp::TxSequenceField>());
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400149 startIdleAckTimer();
Eric Newberry185ab292017-03-28 06:45:39 +0000150 }
151}
152
153void
154LpReliability::piggyback(lp::Packet& pkt, ssize_t mtu)
155{
156 BOOST_ASSERT(m_options.isEnabled);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000157 BOOST_ASSERT(pkt.wireEncode().type() == lp::tlv::LpPacket);
Eric Newberry185ab292017-03-28 06:45:39 +0000158
Eric Newberry7b0071e2017-07-03 17:33:31 +0000159 // up to 2 extra octets reserved for potential TLV-LENGTH size increases
160 ssize_t pktSize = pkt.wireEncode().size();
161 ssize_t reservedSpace = tlv::sizeOfVarNumber(ndn::MAX_NDN_PACKET_SIZE) -
162 tlv::sizeOfVarNumber(pktSize);
163 ssize_t remainingSpace = (mtu == MTU_UNLIMITED ? ndn::MAX_NDN_PACKET_SIZE : mtu) - reservedSpace;
164 remainingSpace -= pktSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000165
Eric Newberry7b0071e2017-07-03 17:33:31 +0000166 while (!m_ackQueue.empty()) {
167 lp::Sequence ackSeq = m_ackQueue.front();
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400168 // Ack size = Ack TLV-TYPE (3 octets) + TLV-LENGTH (1 octet) + lp::Sequence (8 octets)
Junxiao Shi21e01932018-04-21 10:39:05 +0000169 const ssize_t ackSize = tlv::sizeOfVarNumber(lp::tlv::Ack) +
170 tlv::sizeOfVarNumber(sizeof(lp::Sequence)) +
171 sizeof(lp::Sequence);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000172
173 if (ackSize > remainingSpace) {
174 break;
175 }
176
177 pkt.add<lp::AckField>(ackSeq);
Eric Newberry185ab292017-03-28 06:45:39 +0000178 m_ackQueue.pop();
Eric Newberry7b0071e2017-07-03 17:33:31 +0000179 remainingSpace -= ackSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000180 }
181}
182
Eric Newberry7b0071e2017-07-03 17:33:31 +0000183lp::Sequence
184LpReliability::assignTxSequence(lp::Packet& frag)
185{
186 lp::Sequence txSeq = ++m_lastTxSeqNo;
187 frag.set<lp::TxSequenceField>(txSeq);
188 if (m_unackedFrags.size() > 0 && m_lastTxSeqNo == m_firstUnackedFrag->first) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500189 NDN_THROW(std::length_error("TxSequence range exceeded"));
Eric Newberry7b0071e2017-07-03 17:33:31 +0000190 }
191 return m_lastTxSeqNo;
192}
193
Eric Newberry185ab292017-03-28 06:45:39 +0000194void
195LpReliability::startIdleAckTimer()
196{
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400197 if (m_idleAckTimer) {
198 // timer is already running, do nothing
199 return;
200 }
Eric Newberry185ab292017-03-28 06:45:39 +0000201
Davide Pesavento3dade002019-03-19 11:29:56 -0600202 m_idleAckTimer = getScheduler().schedule(m_options.idleAckTimerPeriod, [this] {
Eric Newberry185ab292017-03-28 06:45:39 +0000203 while (!m_ackQueue.empty()) {
ashiqopu075bb7d2019-03-10 01:38:21 +0000204 m_linkService->requestIdlePacket(0);
Eric Newberry185ab292017-03-28 06:45:39 +0000205 }
Eric Newberry185ab292017-03-28 06:45:39 +0000206 });
207}
208
Eric Newberry971d9622018-03-30 23:29:26 -0700209std::vector<lp::Sequence>
Eric Newberry7b0071e2017-07-03 17:33:31 +0000210LpReliability::findLostLpPackets(LpReliability::UnackedFrags::iterator ackIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000211{
Eric Newberry971d9622018-03-30 23:29:26 -0700212 std::vector<lp::Sequence> lostLpPackets;
Eric Newberry185ab292017-03-28 06:45:39 +0000213
214 for (auto it = m_firstUnackedFrag; ; ++it) {
215 if (it == m_unackedFrags.end()) {
216 it = m_unackedFrags.begin();
217 }
218
Eric Newberry7b0071e2017-07-03 17:33:31 +0000219 if (it->first == ackIt->first) {
Eric Newberry185ab292017-03-28 06:45:39 +0000220 break;
221 }
222
223 auto& unackedFrag = it->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000224 unackedFrag.nGreaterSeqAcks++;
225
Eric Newberry7b0071e2017-07-03 17:33:31 +0000226 if (unackedFrag.nGreaterSeqAcks >= m_options.seqNumLossThreshold) {
Eric Newberry971d9622018-03-30 23:29:26 -0700227 lostLpPackets.push_back(it->first);
Eric Newberry185ab292017-03-28 06:45:39 +0000228 }
229 }
230
231 return lostLpPackets;
232}
233
Eric Newberry971d9622018-03-30 23:29:26 -0700234std::vector<lp::Sequence>
235LpReliability::onLpPacketLost(lp::Sequence txSeq)
Eric Newberry185ab292017-03-28 06:45:39 +0000236{
Eric Newberry971d9622018-03-30 23:29:26 -0700237 BOOST_ASSERT(m_unackedFrags.count(txSeq) > 0);
238 auto txSeqIt = m_unackedFrags.find(txSeq);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000239
240 auto& txFrag = txSeqIt->second;
241 txFrag.rtoTimer.cancel();
242 auto netPkt = txFrag.netPkt;
Eric Newberry971d9622018-03-30 23:29:26 -0700243 std::vector<lp::Sequence> removedThisTxSeq;
Eric Newberry185ab292017-03-28 06:45:39 +0000244
245 // Check if maximum number of retransmissions exceeded
246 if (txFrag.retxCount >= m_options.maxRetx) {
Eric Newberry7b0071e2017-07-03 17:33:31 +0000247 // Delete all LpPackets of NetPkt from m_unackedFrags (except this one)
248 for (size_t i = 0; i < netPkt->unackedFrags.size(); i++) {
249 if (netPkt->unackedFrags[i] != txSeqIt) {
Eric Newberry971d9622018-03-30 23:29:26 -0700250 removedThisTxSeq.push_back(netPkt->unackedFrags[i]->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000251 deleteUnackedFrag(netPkt->unackedFrags[i]);
252 }
Eric Newberry185ab292017-03-28 06:45:39 +0000253 }
Eric Newberry185ab292017-03-28 06:45:39 +0000254
255 ++m_linkService->nRetxExhausted;
Eric Newberry41aba102017-11-01 16:42:13 -0700256
257 // Notify strategy of dropped Interest (if any)
258 if (netPkt->isInterest) {
259 BOOST_ASSERT(netPkt->pkt.has<lp::FragmentField>());
260 ndn::Buffer::const_iterator fragBegin, fragEnd;
261 std::tie(fragBegin, fragEnd) = netPkt->pkt.get<lp::FragmentField>();
262 Block frag(&*fragBegin, std::distance(fragBegin, fragEnd));
263 onDroppedInterest(Interest(frag));
264 }
265
Eric Newberry971d9622018-03-30 23:29:26 -0700266 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000267 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000268 }
269 else {
Eric Newberry7b0071e2017-07-03 17:33:31 +0000270 // Assign new TxSequence
271 lp::Sequence newTxSeq = assignTxSequence(txFrag.pkt);
Eric Newberry00d39fd2017-12-10 14:26:45 -0700272 netPkt->didRetx = true;
Eric Newberry185ab292017-03-28 06:45:39 +0000273
Eric Newberry7b0071e2017-07-03 17:33:31 +0000274 // Move fragment to new TxSequence mapping
275 auto newTxFragIt = m_unackedFrags.emplace_hint(
276 m_firstUnackedFrag != m_unackedFrags.end() && m_firstUnackedFrag->first > newTxSeq
277 ? m_firstUnackedFrag
278 : m_unackedFrags.end(),
279 std::piecewise_construct,
280 std::forward_as_tuple(newTxSeq),
281 std::forward_as_tuple(txFrag.pkt));
282 auto& newTxFrag = newTxFragIt->second;
283 newTxFrag.retxCount = txFrag.retxCount + 1;
284 newTxFrag.netPkt = netPkt;
285
286 // Update associated NetPkt
287 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), txSeqIt);
288 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
289 *fragInNetPkt = newTxFragIt;
290
Eric Newberry971d9622018-03-30 23:29:26 -0700291 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000292 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000293
294 // Retransmit fragment
ashiqopu075bb7d2019-03-10 01:38:21 +0000295 m_linkService->sendLpPacket(lp::Packet(newTxFrag.pkt), 0);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000296
297 // Start RTO timer for this sequence
Davide Pesaventof190cfa2019-07-17 20:14:11 -0400298 newTxFrag.rtoTimer = getScheduler().schedule(m_rttEst.getEstimatedRto(),
299 [=] { onLpPacketLost(newTxSeq); });
Eric Newberry185ab292017-03-28 06:45:39 +0000300 }
Eric Newberry971d9622018-03-30 23:29:26 -0700301
302 return removedThisTxSeq;
Eric Newberry185ab292017-03-28 06:45:39 +0000303}
304
305void
Eric Newberry7b0071e2017-07-03 17:33:31 +0000306LpReliability::onLpPacketAcknowledged(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000307{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000308 auto netPkt = fragIt->second.netPkt;
Eric Newberry185ab292017-03-28 06:45:39 +0000309
Eric Newberry7b0071e2017-07-03 17:33:31 +0000310 // Remove from NetPkt unacked fragment list
311 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), fragIt);
312 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
313 *fragInNetPkt = netPkt->unackedFrags.back();
314 netPkt->unackedFrags.pop_back();
Eric Newberry185ab292017-03-28 06:45:39 +0000315
Eric Newberry7b0071e2017-07-03 17:33:31 +0000316 // Check if network-layer packet completely received. If so, increment counters
317 if (netPkt->unackedFrags.empty()) {
318 if (netPkt->didRetx) {
Eric Newberry185ab292017-03-28 06:45:39 +0000319 ++m_linkService->nRetransmitted;
320 }
321 else {
322 ++m_linkService->nAcknowledged;
323 }
Eric Newberry185ab292017-03-28 06:45:39 +0000324 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000325
326 deleteUnackedFrag(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000327}
328
Eric Newberry7b0071e2017-07-03 17:33:31 +0000329void
330LpReliability::deleteUnackedFrag(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000331{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000332 lp::Sequence firstUnackedTxSeq = m_firstUnackedFrag->first;
333 lp::Sequence currentTxSeq = fragIt->first;
334 auto nextFragIt = m_unackedFrags.erase(fragIt);
335
336 if (!m_unackedFrags.empty() && firstUnackedTxSeq == currentTxSeq) {
337 // If "first" fragment in send window (allowing for wraparound), increment window begin
338 if (nextFragIt == m_unackedFrags.end()) {
339 m_firstUnackedFrag = m_unackedFrags.begin();
340 }
341 else {
342 m_firstUnackedFrag = nextFragIt;
343 }
Eric Newberry185ab292017-03-28 06:45:39 +0000344 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000345 else if (m_unackedFrags.empty()) {
346 m_firstUnackedFrag = m_unackedFrags.end();
347 }
Eric Newberry185ab292017-03-28 06:45:39 +0000348}
349
350LpReliability::UnackedFrag::UnackedFrag(lp::Packet pkt)
351 : pkt(std::move(pkt))
352 , sendTime(time::steady_clock::now())
353 , retxCount(0)
354 , nGreaterSeqAcks(0)
Eric Newberry185ab292017-03-28 06:45:39 +0000355{
356}
357
Eric Newberry41aba102017-11-01 16:42:13 -0700358LpReliability::NetPkt::NetPkt(lp::Packet&& pkt, bool isInterest)
359 : pkt(std::move(pkt))
360 , isInterest(isInterest)
361 , didRetx(false)
362{
363}
364
Eric Newberry185ab292017-03-28 06:45:39 +0000365} // namespace face
366} // namespace nfd