face: add best-effort link-layer reliability

refs #3931

Change-Id: I009fabe000f4dd4ceb62acab6b0c735c13112430
diff --git a/daemon/face/lp-reliability.hpp b/daemon/face/lp-reliability.hpp
new file mode 100644
index 0000000..47d5973
--- /dev/null
+++ b/daemon/face/lp-reliability.hpp
@@ -0,0 +1,195 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2017,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_DAEMON_FACE_LP_RELIABILITY_HPP
+#define NFD_DAEMON_FACE_LP_RELIABILITY_HPP
+
+#include "core/common.hpp"
+#include "core/rtt-estimator.hpp"
+#include "core/scheduler.hpp"
+
+#include "face-log.hpp"
+
+#include <ndn-cxx/lp/packet.hpp>
+#include <ndn-cxx/lp/sequence.hpp>
+
+#include <queue>
+
+namespace nfd {
+namespace face {
+
+class GenericLinkService;
+
+/** \brief provides for reliable sending and receiving of link-layer packets
+ *  \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2
+ */
+class LpReliability : noncopyable
+{
+public:
+  struct Options
+  {
+    /** \brief enables link-layer reliability
+     */
+    bool isEnabled = false;
+
+    /** \brief maximum number of retransmissions for an LpPacket
+     */
+    size_t maxRetx = 3;
+
+    /** \brief period between sending pending Acks in an IDLE packet
+     */
+    time::nanoseconds idleAckTimerPeriod = time::milliseconds(5);
+
+    /** \brief a fragment is considered lost if this number of fragments with greater sequence
+     *         numbers are acknowledged
+     */
+    size_t seqNumLossThreshold = 3;
+  };
+
+  LpReliability(const Options& options, GenericLinkService* linkService);
+
+  /** \brief set options for reliability
+   */
+  void
+  setOptions(const Options& options);
+
+  /** \return GenericLinkService that owns this instance
+   *
+   *  This is only used for logging, and may be nullptr.
+   */
+  const GenericLinkService*
+  getLinkService() const;
+
+  /** \brief observe outgoing fragment(s) of a network packet
+   *  \param frags fragments of network packet
+   */
+  void
+  observeOutgoing(const std::vector<lp::Packet>& frags);
+
+  /** \brief extract and parse all Acks and add Ack for contained Fragment (if any) to AckQueue
+   *  \param pkt incoming LpPacket
+   */
+  void
+  processIncomingPacket(const lp::Packet& pkt);
+
+  /** \brief called by GenericLinkService to attach Acks onto an outgoing LpPacket
+   *  \param pkt outgoing LpPacket to attach Acks to
+   *  \param mtu MTU of the Transport
+   */
+  void
+  piggyback(lp::Packet& pkt, ssize_t mtu);
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  /** \brief start the idle Ack timer
+   *
+   * This timer requests an IDLE packet to acknowledge pending fragments not already piggybacked.
+   * It is called regularly on a period configured in Options::idleAckTimerPeriod. This allows Acks
+   * to be returned to the sender, even if the link goes idle.
+   */
+  void
+  startIdleAckTimer();
+
+  /** \brief cancel the idle Ack timer
+   */
+  void
+  stopIdleAckTimer();
+
+private:
+  /** \brief find and mark as lost fragments where a configurable number of Acks have been received
+   *         for greater sequence numbers
+   *  \param ackSeq sequence number of received Ack
+   *  \return vector containing sequence numbers marked lost by this mechanism
+   */
+  std::vector<lp::Sequence>
+  findLostLpPackets(lp::Sequence ackSeq);
+
+  /** \brief resend (or declare as lost) a lost fragment
+   */
+  void
+  onLpPacketLost(lp::Sequence seq);
+
+  class UnackedFrag;
+  class NetPkt;
+
+  /** \brief remove the fragment with the given sequence number from the map of unacknowledged
+   *         fragments as well as its associated network packet
+   *  \param fragIt iterator to fragment to be removed
+   *
+   *  If the given sequence marks the beginning of the send window, the window will be incremented.
+   *  If the associated network packet has been fully transmitted, it will be removed.
+   */
+  void
+  onLpPacketAcknowledged(std::map<lp::Sequence, UnackedFrag>::iterator fragIt,
+                         std::map<lp::Sequence, NetPkt>::iterator netPktIt);
+
+  std::map<lp::Sequence, NetPkt>::iterator
+  getNetPktByFrag(lp::Sequence seq);
+
+private:
+  /** \brief contains a sent fragment that has not been acknowledged and associated data
+   */
+  class UnackedFrag
+  {
+  public:
+    // Allows implicit conversion from an lp::Packet
+    UnackedFrag(lp::Packet pkt);
+
+  public:
+    lp::Packet pkt;
+    scheduler::ScopedEventId rtoTimer;
+    time::steady_clock::TimePoint sendTime;
+    size_t retxCount;
+    size_t nGreaterSeqAcks; //!< number of Acks received for sequences greater than this fragment
+    bool wasTimedOutBySeq; //!< whether this fragment has been timed out by the sequence number mechanic
+  };
+
+  /** \brief contains a network-layer packet with unacknowledged fragments
+   */
+  class NetPkt
+  {
+  public:
+    NetPkt();
+
+  public:
+    std::set<lp::Sequence> unackedFrags;
+    bool didRetx;
+  };
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  Options m_options;
+  GenericLinkService* m_linkService;
+  std::map<lp::Sequence, UnackedFrag> m_unackedFrags;
+  std::map<lp::Sequence, UnackedFrag>::iterator m_firstUnackedFrag;
+  std::map<lp::Sequence, NetPkt> m_netPkts;
+  std::queue<lp::Sequence> m_ackQueue;
+  scheduler::ScopedEventId m_idleAckTimer;
+  bool m_isIdleAckTimerRunning;
+  RttEstimator m_rto;
+};
+
+} // namespace face
+} // namespace nfd
+
+#endif // NFD_DAEMON_FACE_LP_RELIABILITY_HPP