lp-reliability.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, 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 #ifndef NFD_DAEMON_FACE_LP_RELIABILITY_HPP
27 #define NFD_DAEMON_FACE_LP_RELIABILITY_HPP
28 
29 #include "face-common.hpp"
30 
31 #include <ndn-cxx/lp/packet.hpp>
32 #include <ndn-cxx/lp/sequence.hpp>
33 #include <ndn-cxx/util/rtt-estimator.hpp>
34 #include <ndn-cxx/util/scheduler.hpp>
35 
36 #include <map>
37 #include <queue>
38 
39 namespace nfd::face {
40 
41 class GenericLinkService;
42 
47 class LpReliability : noncopyable
48 {
49 public:
51  static constexpr size_t RESERVED_HEADER_SPACE = tlv::sizeOfVarNumber(lp::tlv::TxSequence) +
52  tlv::sizeOfVarNumber(sizeof(lp::Sequence)) +
53  sizeof(lp::Sequence);
54 
55  struct Options
56  {
59  bool isEnabled = false;
60 
63  size_t maxRetx = 3;
64 
67  time::nanoseconds idleAckTimerPeriod = 5_ms;
68 
72  size_t seqNumLossThreshold = 3;
73  };
74 
75  LpReliability(const Options& options, GenericLinkService* linkService);
76 
80  signal::Signal<LpReliability, Interest> onDroppedInterest;
81 
85  void
86  setOptions(const Options& options);
87 
93  const GenericLinkService*
94  getLinkService() const noexcept
95  {
96  return m_linkService;
97  }
98 
104  void
105  handleOutgoing(std::vector<lp::Packet>& frags, lp::Packet&& pkt, bool isInterest);
106 
111  bool
112  processIncomingPacket(const lp::Packet& pkt);
113 
118  void
119  piggyback(lp::Packet& pkt, ssize_t mtu);
120 
122  class UnackedFrag;
123  class NetPkt;
124  using UnackedFrags = std::map<lp::Sequence, UnackedFrag>;
125 
131  lp::Sequence
132  assignTxSequence(lp::Packet& frag);
133 
140  void
141  startIdleAckTimer();
142 
148  std::vector<lp::Sequence>
149  findLostLpPackets(UnackedFrags::iterator ackIt);
150 
154  std::vector<lp::Sequence>
155  onLpPacketLost(lp::Sequence txSeq, bool isTimeout);
156 
164  void
165  onLpPacketAcknowledged(UnackedFrags::iterator fragIt);
166 
174  void
175  deleteUnackedFrag(UnackedFrags::iterator fragIt);
176 
181  class UnackedFrag
182  {
183  public:
184  explicit
185  UnackedFrag(lp::Packet p)
186  : pkt(std::move(p))
187  {
188  }
189 
190  public:
191  lp::Packet pkt;
192  ndn::scheduler::ScopedEventId rtoTimer;
193  time::steady_clock::time_point sendTime = time::steady_clock::now();
194  size_t retxCount = 0;
195  size_t nGreaterSeqAcks = 0;
196  shared_ptr<NetPkt> netPkt;
197  };
198 
202  class NetPkt
203  {
204  public:
205  NetPkt(lp::Packet&& p, bool isInterest)
206  : pkt(std::move(p))
207  , isInterest(isInterest)
208  {
209  }
210 
211  public:
212  std::vector<UnackedFrags::iterator> unackedFrags;
213  lp::Packet pkt;
214  bool isInterest;
215  bool didRetx = false;
216  };
217 
218  Options m_options;
219  GenericLinkService* m_linkService = nullptr;
220  UnackedFrags m_unackedFrags;
221  // An iterator that points to the first unacknowledged fragment in the current window. The window
222  // can wrap around so that the beginning of the window is at a TxSequence greater than other
223  // fragments in the window. When the window is moved past the last item in the iterator, the
224  // first fragment in the map will become the start of the window.
225  UnackedFrags::iterator m_firstUnackedFrag;
226  std::queue<lp::Sequence> m_ackQueue;
227  std::map<lp::Sequence, time::steady_clock::time_point> m_recentRecvSeqs;
228  std::queue<lp::Sequence> m_recentRecvSeqsQueue;
229  lp::Sequence m_lastTxSeqNo;
230  ndn::scheduler::ScopedEventId m_idleAckTimer;
231  ndn::util::RttEstimator m_rttEst;
232 };
233 
234 std::ostream&
235 operator<<(std::ostream& os, const FaceLogHelper<LpReliability>& flh);
236 
237 } // namespace nfd::face
238 
239 #endif // NFD_DAEMON_FACE_LP_RELIABILITY_HPP
Provides for reliable sending and receiving of link-layer packets.
signal::Signal< LpReliability, Interest > onDroppedInterest
Called when an Interest is dropped for exceeding the allowed number of retransmissions.
LpReliability(const Options &options, GenericLinkService *linkService)
void piggyback(lp::Packet &pkt, ssize_t mtu)
Called by GenericLinkService to attach Acks onto an outgoing LpPacket.
static constexpr size_t RESERVED_HEADER_SPACE
TxSequence TLV-TYPE (3 octets) + TLV-LENGTH (1 octet) + lp::Sequence (8 octets)
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.
const GenericLinkService * getLinkService() const noexcept
Returns the GenericLinkService that owns this instance.
void setOptions(const Options &options)
Set options for reliability.
#define NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:41
std::ostream & operator<<(std::ostream &os, const FaceLogHelper< Face > &flh)
Definition: face.cpp:63
time::nanoseconds idleAckTimerPeriod
Period between sending pending Acks in an IDLE packet.
bool isEnabled
Enables link-layer reliability.
size_t maxRetx
Maximum number of retransmissions for an LpPacket.
size_t seqNumLossThreshold
A fragment is considered lost if this number of fragments with greater sequence numbers are acknowled...