blob: 4ff2ac5e275d9affc0302ef031720bb9e6d358d3 [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/*
Eric Newberry971d9622018-03-30 23:29:26 -07003 * Copyright (c) 2014-2018, 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"
29
30namespace nfd {
31namespace face {
32
33LpReliability::LpReliability(const LpReliability::Options& options, GenericLinkService* linkService)
34 : m_options(options)
35 , m_linkService(linkService)
36 , m_firstUnackedFrag(m_unackedFrags.begin())
Eric Newberry7b0071e2017-07-03 17:33:31 +000037 , m_lastTxSeqNo(-1) // set to "-1" to start TxSequence numbers at 0
Eric Newberry185ab292017-03-28 06:45:39 +000038 , m_isIdleAckTimerRunning(false)
39{
40 BOOST_ASSERT(m_linkService != nullptr);
41
42 BOOST_ASSERT(m_options.idleAckTimerPeriod > time::nanoseconds::zero());
43}
44
45void
46LpReliability::setOptions(const Options& options)
47{
48 BOOST_ASSERT(options.idleAckTimerPeriod > time::nanoseconds::zero());
49
50 if (m_options.isEnabled && !options.isEnabled) {
51 this->stopIdleAckTimer();
52 }
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) {
75 // Assign TxSequence number
76 lp::Sequence txSeq = assignTxSequence(frag);
77
Eric Newberry185ab292017-03-28 06:45:39 +000078 // Store LpPacket for future retransmissions
Eric Newberry7b0071e2017-07-03 17:33:31 +000079 unackedFragsIt = m_unackedFrags.emplace_hint(unackedFragsIt,
80 std::piecewise_construct,
81 std::forward_as_tuple(txSeq),
82 std::forward_as_tuple(frag));
83 unackedFragsIt->second.sendTime = sendTime;
Eric Newberry185ab292017-03-28 06:45:39 +000084 unackedFragsIt->second.rtoTimer =
Eric Newberry971d9622018-03-30 23:29:26 -070085 scheduler::schedule(m_rto.computeRto(), bind(&LpReliability::onLpPacketLost, this, txSeq));
Eric Newberry7b0071e2017-07-03 17:33:31 +000086 unackedFragsIt->second.netPkt = netPkt;
87
Eric Newberry185ab292017-03-28 06:45:39 +000088 if (m_unackedFrags.size() == 1) {
Eric Newberry7b0071e2017-07-03 17:33:31 +000089 m_firstUnackedFrag = m_unackedFrags.begin();
Eric Newberry185ab292017-03-28 06:45:39 +000090 }
Eric Newberry7b0071e2017-07-03 17:33:31 +000091
92 // Add to associated NetPkt
93 netPkt->unackedFrags.push_back(unackedFragsIt);
Eric Newberry185ab292017-03-28 06:45:39 +000094 }
95}
96
97void
98LpReliability::processIncomingPacket(const lp::Packet& pkt)
99{
100 BOOST_ASSERT(m_options.isEnabled);
101
102 auto now = time::steady_clock::now();
103
104 // Extract and parse Acks
105 for (lp::Sequence ackSeq : pkt.list<lp::AckField>()) {
Eric Newberry7b0071e2017-07-03 17:33:31 +0000106 auto fragIt = m_unackedFrags.find(ackSeq);
107 if (fragIt == m_unackedFrags.end()) {
108 // Ignore an Ack for an unknown TxSequence number
Eric Newberry185ab292017-03-28 06:45:39 +0000109 continue;
110 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000111 auto& frag = fragIt->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000112
113 // Cancel the RTO timer for the acknowledged fragment
Eric Newberry7b0071e2017-07-03 17:33:31 +0000114 frag.rtoTimer.cancel();
Eric Newberry185ab292017-03-28 06:45:39 +0000115
Eric Newberry7b0071e2017-07-03 17:33:31 +0000116 if (frag.retxCount == 0) {
Eric Newberry185ab292017-03-28 06:45:39 +0000117 // This sequence had no retransmissions, so use it to calculate the RTO
Eric Newberry7b0071e2017-07-03 17:33:31 +0000118 m_rto.addMeasurement(time::duration_cast<RttEstimator::Duration>(now - frag.sendTime));
Eric Newberry185ab292017-03-28 06:45:39 +0000119 }
120
Eric Newberry7b0071e2017-07-03 17:33:31 +0000121 // Look for frags with TxSequence numbers < ackSeq (allowing for wraparound) and consider them
122 // lost if a configurable number of Acks containing greater TxSequence numbers have been
123 // received.
124 auto lostLpPackets = findLostLpPackets(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000125
Eric Newberry7b0071e2017-07-03 17:33:31 +0000126 // Remove the fragment from the map of unacknowledged fragments and from its associated network
127 // packet. Potentially increment the start of the window.
128 onLpPacketAcknowledged(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000129
Eric Newberry971d9622018-03-30 23:29:26 -0700130 // This set contains TxSequences that have been removed by onLpPacketLost below because they
131 // were part of a network packet that was removed due to a fragment exceeding retx, as well as
132 // any other TxSequences removed by onLpPacketLost. This prevents onLpPacketLost from being
133 // called later for an invalid iterator.
134 std::set<lp::Sequence> removedLpPackets;
135
Eric Newberry7b0071e2017-07-03 17:33:31 +0000136 // Resend or fail fragments considered lost. Potentially increment the start of the window.
Eric Newberry971d9622018-03-30 23:29:26 -0700137 for (lp::Sequence txSeq : lostLpPackets) {
138 if (removedLpPackets.find(txSeq) == removedLpPackets.end()) {
139 auto removedThisTxSeq = this->onLpPacketLost(txSeq);
140 for (auto removedTxSeq : removedThisTxSeq) {
141 removedLpPackets.insert(removedTxSeq);
142 }
143 }
Eric Newberry185ab292017-03-28 06:45:39 +0000144 }
145 }
146
Eric Newberry7b0071e2017-07-03 17:33:31 +0000147 // If packet has Fragment and TxSequence fields, extract TxSequence and add to AckQueue
148 if (pkt.has<lp::FragmentField>() && pkt.has<lp::TxSequenceField>()) {
149 m_ackQueue.push(pkt.get<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000150 if (!m_isIdleAckTimerRunning) {
151 this->startIdleAckTimer();
152 }
153 }
154}
155
156void
157LpReliability::piggyback(lp::Packet& pkt, ssize_t mtu)
158{
159 BOOST_ASSERT(m_options.isEnabled);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000160 BOOST_ASSERT(pkt.wireEncode().type() == lp::tlv::LpPacket);
Eric Newberry185ab292017-03-28 06:45:39 +0000161
Eric Newberry7b0071e2017-07-03 17:33:31 +0000162 // up to 2 extra octets reserved for potential TLV-LENGTH size increases
163 ssize_t pktSize = pkt.wireEncode().size();
164 ssize_t reservedSpace = tlv::sizeOfVarNumber(ndn::MAX_NDN_PACKET_SIZE) -
165 tlv::sizeOfVarNumber(pktSize);
166 ssize_t remainingSpace = (mtu == MTU_UNLIMITED ? ndn::MAX_NDN_PACKET_SIZE : mtu) - reservedSpace;
167 remainingSpace -= pktSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000168
Eric Newberry7b0071e2017-07-03 17:33:31 +0000169 while (!m_ackQueue.empty()) {
170 lp::Sequence ackSeq = m_ackQueue.front();
171 // Ack Size = Ack Type (3 octets) + Ack Length (1 octet) + Value (1, 2, 4, or 8 octets)
172 ssize_t ackSize = tlv::sizeOfVarNumber(lp::tlv::Ack) +
173 tlv::sizeOfVarNumber(
174 tlv::sizeOfNonNegativeInteger(std::numeric_limits<lp::Sequence>::max())) +
175 tlv::sizeOfNonNegativeInteger(ackSeq);
176
177 if (ackSize > remainingSpace) {
178 break;
179 }
180
181 pkt.add<lp::AckField>(ackSeq);
Eric Newberry185ab292017-03-28 06:45:39 +0000182 m_ackQueue.pop();
Eric Newberry7b0071e2017-07-03 17:33:31 +0000183 remainingSpace -= ackSize;
Eric Newberry185ab292017-03-28 06:45:39 +0000184 }
185}
186
Eric Newberry7b0071e2017-07-03 17:33:31 +0000187lp::Sequence
188LpReliability::assignTxSequence(lp::Packet& frag)
189{
190 lp::Sequence txSeq = ++m_lastTxSeqNo;
191 frag.set<lp::TxSequenceField>(txSeq);
192 if (m_unackedFrags.size() > 0 && m_lastTxSeqNo == m_firstUnackedFrag->first) {
193 BOOST_THROW_EXCEPTION(std::length_error("TxSequence range exceeded"));
194 }
195 return m_lastTxSeqNo;
196}
197
Eric Newberry185ab292017-03-28 06:45:39 +0000198void
199LpReliability::startIdleAckTimer()
200{
201 BOOST_ASSERT(!m_isIdleAckTimerRunning);
202 m_isIdleAckTimerRunning = true;
203
204 m_idleAckTimer = scheduler::schedule(m_options.idleAckTimerPeriod, [this] {
205 while (!m_ackQueue.empty()) {
206 m_linkService->requestIdlePacket();
207 }
208
209 m_isIdleAckTimerRunning = false;
210 });
211}
212
213void
214LpReliability::stopIdleAckTimer()
215{
216 m_idleAckTimer.cancel();
217 m_isIdleAckTimerRunning = false;
218}
219
Eric Newberry971d9622018-03-30 23:29:26 -0700220std::vector<lp::Sequence>
Eric Newberry7b0071e2017-07-03 17:33:31 +0000221LpReliability::findLostLpPackets(LpReliability::UnackedFrags::iterator ackIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000222{
Eric Newberry971d9622018-03-30 23:29:26 -0700223 std::vector<lp::Sequence> lostLpPackets;
Eric Newberry185ab292017-03-28 06:45:39 +0000224
225 for (auto it = m_firstUnackedFrag; ; ++it) {
226 if (it == m_unackedFrags.end()) {
227 it = m_unackedFrags.begin();
228 }
229
Eric Newberry7b0071e2017-07-03 17:33:31 +0000230 if (it->first == ackIt->first) {
Eric Newberry185ab292017-03-28 06:45:39 +0000231 break;
232 }
233
234 auto& unackedFrag = it->second;
Eric Newberry185ab292017-03-28 06:45:39 +0000235 unackedFrag.nGreaterSeqAcks++;
236
Eric Newberry7b0071e2017-07-03 17:33:31 +0000237 if (unackedFrag.nGreaterSeqAcks >= m_options.seqNumLossThreshold) {
Eric Newberry971d9622018-03-30 23:29:26 -0700238 lostLpPackets.push_back(it->first);
Eric Newberry185ab292017-03-28 06:45:39 +0000239 }
240 }
241
242 return lostLpPackets;
243}
244
Eric Newberry971d9622018-03-30 23:29:26 -0700245std::vector<lp::Sequence>
246LpReliability::onLpPacketLost(lp::Sequence txSeq)
Eric Newberry185ab292017-03-28 06:45:39 +0000247{
Eric Newberry971d9622018-03-30 23:29:26 -0700248 BOOST_ASSERT(m_unackedFrags.count(txSeq) > 0);
249 auto txSeqIt = m_unackedFrags.find(txSeq);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000250
251 auto& txFrag = txSeqIt->second;
252 txFrag.rtoTimer.cancel();
253 auto netPkt = txFrag.netPkt;
Eric Newberry971d9622018-03-30 23:29:26 -0700254 std::vector<lp::Sequence> removedThisTxSeq;
Eric Newberry185ab292017-03-28 06:45:39 +0000255
256 // Check if maximum number of retransmissions exceeded
257 if (txFrag.retxCount >= m_options.maxRetx) {
Eric Newberry7b0071e2017-07-03 17:33:31 +0000258 // Delete all LpPackets of NetPkt from m_unackedFrags (except this one)
259 for (size_t i = 0; i < netPkt->unackedFrags.size(); i++) {
260 if (netPkt->unackedFrags[i] != txSeqIt) {
Eric Newberry971d9622018-03-30 23:29:26 -0700261 removedThisTxSeq.push_back(netPkt->unackedFrags[i]->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000262 deleteUnackedFrag(netPkt->unackedFrags[i]);
263 }
Eric Newberry185ab292017-03-28 06:45:39 +0000264 }
Eric Newberry185ab292017-03-28 06:45:39 +0000265
266 ++m_linkService->nRetxExhausted;
Eric Newberry41aba102017-11-01 16:42:13 -0700267
268 // Notify strategy of dropped Interest (if any)
269 if (netPkt->isInterest) {
270 BOOST_ASSERT(netPkt->pkt.has<lp::FragmentField>());
271 ndn::Buffer::const_iterator fragBegin, fragEnd;
272 std::tie(fragBegin, fragEnd) = netPkt->pkt.get<lp::FragmentField>();
273 Block frag(&*fragBegin, std::distance(fragBegin, fragEnd));
274 onDroppedInterest(Interest(frag));
275 }
276
Eric Newberry971d9622018-03-30 23:29:26 -0700277 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000278 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000279 }
280 else {
Eric Newberry7b0071e2017-07-03 17:33:31 +0000281 // Assign new TxSequence
282 lp::Sequence newTxSeq = assignTxSequence(txFrag.pkt);
Eric Newberry00d39fd2017-12-10 14:26:45 -0700283 netPkt->didRetx = true;
Eric Newberry185ab292017-03-28 06:45:39 +0000284
Eric Newberry7b0071e2017-07-03 17:33:31 +0000285 // Move fragment to new TxSequence mapping
286 auto newTxFragIt = m_unackedFrags.emplace_hint(
287 m_firstUnackedFrag != m_unackedFrags.end() && m_firstUnackedFrag->first > newTxSeq
288 ? m_firstUnackedFrag
289 : m_unackedFrags.end(),
290 std::piecewise_construct,
291 std::forward_as_tuple(newTxSeq),
292 std::forward_as_tuple(txFrag.pkt));
293 auto& newTxFrag = newTxFragIt->second;
294 newTxFrag.retxCount = txFrag.retxCount + 1;
295 newTxFrag.netPkt = netPkt;
296
297 // Update associated NetPkt
298 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), txSeqIt);
299 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
300 *fragInNetPkt = newTxFragIt;
301
Eric Newberry971d9622018-03-30 23:29:26 -0700302 removedThisTxSeq.push_back(txSeqIt->first);
Eric Newberry7b0071e2017-07-03 17:33:31 +0000303 deleteUnackedFrag(txSeqIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000304
305 // Retransmit fragment
Eric Newberry7b0071e2017-07-03 17:33:31 +0000306 m_linkService->sendLpPacket(lp::Packet(newTxFrag.pkt));
307
308 // Start RTO timer for this sequence
309 newTxFrag.rtoTimer = scheduler::schedule(m_rto.computeRto(),
Eric Newberry971d9622018-03-30 23:29:26 -0700310 bind(&LpReliability::onLpPacketLost, this, newTxSeq));
Eric Newberry185ab292017-03-28 06:45:39 +0000311 }
Eric Newberry971d9622018-03-30 23:29:26 -0700312
313 return removedThisTxSeq;
Eric Newberry185ab292017-03-28 06:45:39 +0000314}
315
316void
Eric Newberry7b0071e2017-07-03 17:33:31 +0000317LpReliability::onLpPacketAcknowledged(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000318{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000319 auto netPkt = fragIt->second.netPkt;
Eric Newberry185ab292017-03-28 06:45:39 +0000320
Eric Newberry7b0071e2017-07-03 17:33:31 +0000321 // Remove from NetPkt unacked fragment list
322 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), fragIt);
323 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
324 *fragInNetPkt = netPkt->unackedFrags.back();
325 netPkt->unackedFrags.pop_back();
Eric Newberry185ab292017-03-28 06:45:39 +0000326
Eric Newberry7b0071e2017-07-03 17:33:31 +0000327 // Check if network-layer packet completely received. If so, increment counters
328 if (netPkt->unackedFrags.empty()) {
329 if (netPkt->didRetx) {
Eric Newberry185ab292017-03-28 06:45:39 +0000330 ++m_linkService->nRetransmitted;
331 }
332 else {
333 ++m_linkService->nAcknowledged;
334 }
Eric Newberry185ab292017-03-28 06:45:39 +0000335 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000336
337 deleteUnackedFrag(fragIt);
Eric Newberry185ab292017-03-28 06:45:39 +0000338}
339
Eric Newberry7b0071e2017-07-03 17:33:31 +0000340void
341LpReliability::deleteUnackedFrag(UnackedFrags::iterator fragIt)
Eric Newberry185ab292017-03-28 06:45:39 +0000342{
Eric Newberry7b0071e2017-07-03 17:33:31 +0000343 lp::Sequence firstUnackedTxSeq = m_firstUnackedFrag->first;
344 lp::Sequence currentTxSeq = fragIt->first;
345 auto nextFragIt = m_unackedFrags.erase(fragIt);
346
347 if (!m_unackedFrags.empty() && firstUnackedTxSeq == currentTxSeq) {
348 // If "first" fragment in send window (allowing for wraparound), increment window begin
349 if (nextFragIt == m_unackedFrags.end()) {
350 m_firstUnackedFrag = m_unackedFrags.begin();
351 }
352 else {
353 m_firstUnackedFrag = nextFragIt;
354 }
Eric Newberry185ab292017-03-28 06:45:39 +0000355 }
Eric Newberry7b0071e2017-07-03 17:33:31 +0000356 else if (m_unackedFrags.empty()) {
357 m_firstUnackedFrag = m_unackedFrags.end();
358 }
Eric Newberry185ab292017-03-28 06:45:39 +0000359}
360
361LpReliability::UnackedFrag::UnackedFrag(lp::Packet pkt)
362 : pkt(std::move(pkt))
363 , sendTime(time::steady_clock::now())
364 , retxCount(0)
365 , nGreaterSeqAcks(0)
Eric Newberry185ab292017-03-28 06:45:39 +0000366{
367}
368
Eric Newberry41aba102017-11-01 16:42:13 -0700369LpReliability::NetPkt::NetPkt(lp::Packet&& pkt, bool isInterest)
370 : pkt(std::move(pkt))
371 , isInterest(isInterest)
372 , didRetx(false)
373{
374}
375
Eric Newberry185ab292017-03-28 06:45:39 +0000376} // namespace face
377} // namespace nfd