lp-reliability.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California,
4  * 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 #include "common/global.hpp"
30 
31 namespace nfd::face {
32 
33 NFD_LOG_INIT(LpReliability);
34 
36  : m_options(options)
37  , m_linkService(linkService)
38  , m_firstUnackedFrag(m_unackedFrags.begin())
39  , m_lastTxSeqNo(-1) // set to "-1" to start TxSequence numbers at 0
40 {
41  BOOST_ASSERT(m_linkService != nullptr);
42  BOOST_ASSERT(m_options.idleAckTimerPeriod > 0_ns);
43 }
44 
45 void
47 {
48  BOOST_ASSERT(options.idleAckTimerPeriod > 0_ns);
49 
50  if (m_options.isEnabled && !options.isEnabled) {
51  m_idleAckTimer.cancel();
52  }
53 
54  m_options = options;
55 }
56 
57 const GenericLinkService*
59 {
60  return m_linkService;
61 }
62 
63 void
64 LpReliability::handleOutgoing(std::vector<lp::Packet>& frags, lp::Packet&& pkt, bool isInterest)
65 {
66  BOOST_ASSERT(m_options.isEnabled);
67 
68  auto unackedFragsIt = m_unackedFrags.begin();
69  auto sendTime = time::steady_clock::now();
70 
71  auto netPkt = make_shared<NetPkt>(std::move(pkt), isInterest);
72  netPkt->unackedFrags.reserve(frags.size());
73 
74  for (lp::Packet& frag : frags) {
75  // Non-IDLE packets are required to have assigned Sequence numbers with LpReliability enabled
76  BOOST_ASSERT(frag.has<lp::SequenceField>());
77 
78  // Assign TxSequence number
79  lp::Sequence txSeq = assignTxSequence(frag);
80 
81  // Store LpPacket for future retransmissions
82  unackedFragsIt = m_unackedFrags.try_emplace(unackedFragsIt, txSeq, frag);
83  unackedFragsIt->second.sendTime = sendTime;
84  auto rto = m_rttEst.getEstimatedRto();
85  lp::Sequence seq = frag.get<lp::SequenceField>();
86  NFD_LOG_FACE_TRACE("transmitting seq=" << seq << ", txseq=" << txSeq << ", rto=" <<
87  time::duration_cast<time::milliseconds>(rto).count() << "ms");
88  unackedFragsIt->second.rtoTimer = getScheduler().schedule(rto, [=] {
89  onLpPacketLost(txSeq, true);
90  });
91  unackedFragsIt->second.netPkt = netPkt;
92 
93  if (m_unackedFrags.size() == 1) {
94  m_firstUnackedFrag = m_unackedFrags.begin();
95  }
96 
97  // Add to associated NetPkt
98  netPkt->unackedFrags.push_back(unackedFragsIt);
99  }
100 }
101 
102 bool
104 {
105  BOOST_ASSERT(m_options.isEnabled);
106 
107  bool isDuplicate = false;
108  auto now = time::steady_clock::now();
109 
110  // Extract and parse Acks
111  for (lp::Sequence ackTxSeq : pkt.list<lp::AckField>()) {
112  auto fragIt = m_unackedFrags.find(ackTxSeq);
113  if (fragIt == m_unackedFrags.end()) {
114  // Ignore an Ack for an unknown TxSequence number
115  NFD_LOG_FACE_DEBUG("received ack for unknown txseq=" << ackTxSeq);
116  continue;
117  }
118  auto& frag = fragIt->second;
119 
120  // Cancel the RTO timer for the acknowledged fragment
121  frag.rtoTimer.cancel();
122 
123  if (frag.retxCount == 0) {
124  NFD_LOG_FACE_TRACE("received ack for seq=" << frag.pkt.get<lp::SequenceField>() << ", txseq=" <<
125  ackTxSeq << ", retx=0, rtt=" <<
126  time::duration_cast<time::milliseconds>(now - frag.sendTime).count() << "ms");
127  // This sequence had no retransmissions, so use it to estimate the RTO
128  m_rttEst.addMeasurement(now - frag.sendTime);
129  }
130  else {
131  NFD_LOG_FACE_TRACE("received ack for seq=" << frag.pkt.get<lp::SequenceField>() << ", txseq=" <<
132  ackTxSeq << ", retx=" << frag.retxCount);
133  }
134 
135  // 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
137  // received.
138  auto lostLpPackets = findLostLpPackets(fragIt);
139 
140  // 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);
143 
144  // 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 
150  // Resend or fail fragments considered lost. Potentially increment the start of the window.
151  for (lp::Sequence txSeq : lostLpPackets) {
152  if (removedLpPackets.find(txSeq) == removedLpPackets.end()) {
153  auto removedTxSeqs = onLpPacketLost(txSeq, false);
154  for (auto removedTxSeq : removedTxSeqs) {
155  removedLpPackets.insert(removedTxSeq);
156  }
157  }
158  }
159  }
160 
161  // If packet has Fragment and TxSequence fields, extract TxSequence and add to AckQueue
162  if (pkt.has<lp::FragmentField>() && pkt.has<lp::TxSequenceField>()) {
163  NFD_LOG_FACE_TRACE("queueing ack for remote txseq=" << pkt.get<lp::TxSequenceField>());
164  m_ackQueue.push(pkt.get<lp::TxSequenceField>());
165 
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();
173  while (!m_recentRecvSeqsQueue.empty() &&
174  now > m_recentRecvSeqs[m_recentRecvSeqsQueue.front()] + rto) {
175  m_recentRecvSeqs.erase(m_recentRecvSeqsQueue.front());
176  m_recentRecvSeqsQueue.pop();
177  }
178  m_recentRecvSeqs.try_emplace(pktSequence, now);
179  m_recentRecvSeqsQueue.push(pktSequence);
180  }
181 
182  startIdleAckTimer();
183  }
184 
185  return !isDuplicate;
186 }
187 
188 void
189 LpReliability::piggyback(lp::Packet& pkt, ssize_t mtu)
190 {
191  BOOST_ASSERT(m_options.isEnabled);
192  BOOST_ASSERT(pkt.wireEncode().type() == lp::tlv::LpPacket);
193 
194  // 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;
200 
201  while (!m_ackQueue.empty()) {
202  lp::Sequence ackTxSeq = m_ackQueue.front();
203  // Ack size = Ack TLV-TYPE (3 octets) + TLV-LENGTH (1 octet) + lp::Sequence (8 octets)
204  const ssize_t ackSize = tlv::sizeOfVarNumber(lp::tlv::Ack) +
205  tlv::sizeOfVarNumber(sizeof(lp::Sequence)) +
206  sizeof(lp::Sequence);
207 
208  if (ackSize > remainingSpace) {
209  break;
210  }
211 
212  NFD_LOG_FACE_TRACE("piggybacking ack for remote txseq=" << ackTxSeq);
213 
214  pkt.add<lp::AckField>(ackTxSeq);
215  m_ackQueue.pop();
216  remainingSpace -= ackSize;
217  }
218 }
219 
220 lp::Sequence
221 LpReliability::assignTxSequence(lp::Packet& frag)
222 {
223  lp::Sequence txSeq = ++m_lastTxSeqNo;
224  frag.set<lp::TxSequenceField>(txSeq);
225  if (!m_unackedFrags.empty() && m_lastTxSeqNo == m_firstUnackedFrag->first) {
226  NDN_THROW(std::length_error("TxSequence range exceeded"));
227  }
228  return m_lastTxSeqNo;
229 }
230 
231 void
232 LpReliability::startIdleAckTimer()
233 {
234  if (m_idleAckTimer) {
235  // timer is already running, do nothing
236  return;
237  }
238 
239  m_idleAckTimer = getScheduler().schedule(m_options.idleAckTimerPeriod, [this] {
240  while (!m_ackQueue.empty()) {
241  m_linkService->requestIdlePacket();
242  }
243  });
244 }
245 
246 std::vector<lp::Sequence>
247 LpReliability::findLostLpPackets(LpReliability::UnackedFrags::iterator ackIt)
248 {
249  std::vector<lp::Sequence> lostLpPackets;
250 
251  for (auto it = m_firstUnackedFrag; ; ++it) {
252  if (it == m_unackedFrags.end()) {
253  it = m_unackedFrags.begin();
254  }
255 
256  if (it->first == ackIt->first) {
257  break;
258  }
259 
260  auto& unackedFrag = it->second;
261  unackedFrag.nGreaterSeqAcks++;
262  NFD_LOG_FACE_TRACE("received ack=" << ackIt->first << " before=" << it->first <<
263  ", before count=" << unackedFrag.nGreaterSeqAcks);
264 
265  if (unackedFrag.nGreaterSeqAcks >= m_options.seqNumLossThreshold) {
266  lostLpPackets.push_back(it->first);
267  }
268  }
269 
270  return lostLpPackets;
271 }
272 
273 std::vector<lp::Sequence>
274 LpReliability::onLpPacketLost(lp::Sequence txSeq, bool isTimeout)
275 {
276  BOOST_ASSERT(m_unackedFrags.count(txSeq) > 0);
277  auto txSeqIt = m_unackedFrags.find(txSeq);
278 
279  auto& txFrag = txSeqIt->second;
280  txFrag.rtoTimer.cancel();
281  auto netPkt = txFrag.netPkt;
282  std::vector<lp::Sequence> removedThisTxSeq;
283  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  }
292 
293  // Check if maximum number of retransmissions exceeded
294  if (txFrag.retxCount >= m_options.maxRetx) {
295  NFD_LOG_FACE_DEBUG("seq=" << seq << " exceeded allowed retransmissions: DROP");
296  // 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) {
299  removedThisTxSeq.push_back(netPkt->unackedFrags[i]->first);
300  deleteUnackedFrag(netPkt->unackedFrags[i]);
301  }
302  }
303 
304  ++m_linkService->nRetxExhausted;
305 
306  // Notify strategy of dropped Interest (if any)
307  if (netPkt->isInterest) {
308  BOOST_ASSERT(netPkt->pkt.has<lp::FragmentField>());
309  auto frag = netPkt->pkt.get<lp::FragmentField>();
310  onDroppedInterest(Interest(Block({frag.first, frag.second})));
311  }
312 
313  // Delete this LpPacket from m_unackedFrags
314  removedThisTxSeq.push_back(txSeqIt->first);
315  deleteUnackedFrag(txSeqIt);
316  }
317  else {
318  // Assign new TxSequence
319  lp::Sequence newTxSeq = assignTxSequence(txFrag.pkt);
320  netPkt->didRetx = true;
321 
322  // Move fragment to new TxSequence mapping
323  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);
327  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 
336  removedThisTxSeq.push_back(txSeqIt->first);
337  deleteUnackedFrag(txSeqIt);
338 
339  // Retransmit fragment
340  m_linkService->sendLpPacket(lp::Packet(newTxFrag.pkt));
341 
342  auto rto = m_rttEst.getEstimatedRto();
343  NFD_LOG_FACE_TRACE("retransmitting seq=" << seq << ", txseq=" << newTxSeq << ", retx=" <<
344  txFrag.retxCount << ", rto=" <<
345  time::duration_cast<time::milliseconds>(rto).count() << "ms");
346 
347  // Start RTO timer for this sequence
348  newTxFrag.rtoTimer = getScheduler().schedule(rto, [=] {
349  onLpPacketLost(newTxSeq, true);
350  });
351  }
352 
353  return removedThisTxSeq;
354 }
355 
356 void
357 LpReliability::onLpPacketAcknowledged(UnackedFrags::iterator fragIt)
358 {
359  auto netPkt = fragIt->second.netPkt;
360 
361  // 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();
366 
367  // Check if network-layer packet completely received. If so, increment counters
368  if (netPkt->unackedFrags.empty()) {
369  if (netPkt->didRetx) {
370  ++m_linkService->nRetransmitted;
371  }
372  else {
373  ++m_linkService->nAcknowledged;
374  }
375  }
376 
377  deleteUnackedFrag(fragIt);
378 }
379 
380 void
381 LpReliability::deleteUnackedFrag(UnackedFrags::iterator fragIt)
382 {
383  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  }
395  }
396  else if (m_unackedFrags.empty()) {
397  m_firstUnackedFrag = m_unackedFrags.end();
398  }
399 }
400 
401 LpReliability::UnackedFrag::UnackedFrag(lp::Packet pkt)
402  : pkt(std::move(pkt))
403  , sendTime(time::steady_clock::now())
404  , retxCount(0)
405  , nGreaterSeqAcks(0)
406 {
407 }
408 
409 LpReliability::NetPkt::NetPkt(lp::Packet&& pkt, bool isInterest)
410  : pkt(std::move(pkt))
411  , isInterest(isInterest)
412  , didRetx(false)
413 {
414 }
415 
416 std::ostream&
417 operator<<(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 
428 } // namespace nfd::face
For internal use by FaceLogging macros.
LpReliability(const Options &options, GenericLinkService *linkService)
void piggyback(lp::Packet &pkt, ssize_t mtu)
Called by GenericLinkService to attach Acks onto an outgoing LpPacket.
const GenericLinkService * getLinkService() const
void handleOutgoing(std::vector< lp::Packet > &frags, lp::Packet &&pkt, bool isInterest)
Observe outgoing fragment(s) of a network packet and store for potential retransmission.
bool processIncomingPacket(const lp::Packet &pkt)
Extract and parse all Acks and add Ack for contained Fragment (if any) to AckQueue.
void setOptions(const Options &options)
Set options for reliability.
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
std::ostream & operator<<(std::ostream &os, const FaceLogHelper< Face > &flh)
Definition: face.cpp:45
constexpr ssize_t MTU_UNLIMITED
Indicates that the transport has no limit on payload size.
Definition: transport.hpp:92
Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
Definition: global.cpp:45
time::nanoseconds idleAckTimerPeriod
Period between sending pending Acks in an IDLE packet.
bool isEnabled
Enables link-layer reliability.