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-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 #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 
35 #include <queue>
36 
37 namespace nfd::face {
38 
39 class GenericLinkService;
40 
45 class LpReliability : noncopyable
46 {
47 public:
49  static constexpr size_t RESERVED_HEADER_SPACE = tlv::sizeOfVarNumber(lp::tlv::TxSequence) +
50  tlv::sizeOfVarNumber(sizeof(lp::Sequence)) +
51  sizeof(lp::Sequence);
52 
53  struct Options
54  {
57  bool isEnabled = false;
58 
61  size_t maxRetx = 3;
62 
65  time::nanoseconds idleAckTimerPeriod = 5_ms;
66 
70  size_t seqNumLossThreshold = 3;
71  };
72 
73  LpReliability(const Options& options, GenericLinkService* linkService);
74 
77  signal::Signal<LpReliability, Interest> onDroppedInterest;
78 
81  void
82  setOptions(const Options& options);
83 
88  const GenericLinkService*
89  getLinkService() const;
90 
96  void
97  handleOutgoing(std::vector<lp::Packet>& frags, lp::Packet&& pkt, bool isInterest);
98 
103  bool
104  processIncomingPacket(const lp::Packet& pkt);
105 
110  void
111  piggyback(lp::Packet& pkt, ssize_t mtu);
112 
114  class UnackedFrag;
115  class NetPkt;
116  using UnackedFrags = std::map<lp::Sequence, UnackedFrag>;
117 
123  lp::Sequence
124  assignTxSequence(lp::Packet& frag);
125 
132  void
133  startIdleAckTimer();
134 
140  std::vector<lp::Sequence>
141  findLostLpPackets(UnackedFrags::iterator ackIt);
142 
146  std::vector<lp::Sequence>
147  onLpPacketLost(lp::Sequence txSeq, bool isTimeout);
148 
156  void
157  onLpPacketAcknowledged(UnackedFrags::iterator fragIt);
158 
166  void
167  deleteUnackedFrag(UnackedFrags::iterator fragIt);
168 
173  class UnackedFrag
174  {
175  public:
176  explicit
177  UnackedFrag(lp::Packet pkt);
178 
179  public:
180  lp::Packet pkt;
181  scheduler::ScopedEventId rtoTimer;
182  time::steady_clock::time_point sendTime;
183  size_t retxCount;
184  size_t nGreaterSeqAcks;
185  shared_ptr<NetPkt> netPkt;
186  };
187 
191  class NetPkt
192  {
193  public:
194  NetPkt(lp::Packet&& pkt, bool isInterest);
195 
196  public:
197  std::vector<UnackedFrags::iterator> unackedFrags;
198  lp::Packet pkt;
199  bool isInterest;
200  bool didRetx;
201  };
202 
203  Options m_options;
204  GenericLinkService* m_linkService;
205  UnackedFrags m_unackedFrags;
206  // An iterator that points to the first unacknowledged fragment in the current window. The window
207  // can wrap around so that the beginning of the window is at a TxSequence greater than other
208  // fragments in the window. When the window is moved past the last item in the iterator, the
209  // first fragment in the map will become the start of the window.
210  UnackedFrags::iterator m_firstUnackedFrag;
211  std::queue<lp::Sequence> m_ackQueue;
212  std::map<lp::Sequence, time::steady_clock::time_point> m_recentRecvSeqs;
213  std::queue<lp::Sequence> m_recentRecvSeqsQueue;
214  lp::Sequence m_lastTxSeqNo;
215  scheduler::ScopedEventId m_idleAckTimer;
216  ndn::util::RttEstimator m_rttEst;
217 };
218 
219 std::ostream&
220 operator<<(std::ostream& os, const FaceLogHelper<LpReliability>& flh);
221 
222 } // namespace nfd::face
223 
224 #endif // NFD_DAEMON_FACE_LP_RELIABILITY_HPP
Provides for reliable sending and receiving of link-layer packets.
signal::Signal< LpReliability, Interest > onDroppedInterest
Signals on Interest dropped by reliability system for exceeding allowed number of retx.
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)
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_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:41
std::ostream & operator<<(std::ostream &os, const FaceLogHelper< Face > &flh)
Definition: face.cpp:45
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...