Eric Newberry | 185ab29 | 2017-03-28 06:45:39 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2017, 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 "core/common.hpp" |
| 30 | #include "core/rtt-estimator.hpp" |
| 31 | #include "core/scheduler.hpp" |
| 32 | |
| 33 | #include "face-log.hpp" |
| 34 | |
| 35 | #include <ndn-cxx/lp/packet.hpp> |
| 36 | #include <ndn-cxx/lp/sequence.hpp> |
| 37 | |
| 38 | #include <queue> |
| 39 | |
| 40 | namespace nfd { |
| 41 | namespace face { |
| 42 | |
| 43 | class GenericLinkService; |
| 44 | |
| 45 | /** \brief provides for reliable sending and receiving of link-layer packets |
| 46 | * \sa https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2 |
| 47 | */ |
| 48 | class LpReliability : noncopyable |
| 49 | { |
| 50 | public: |
| 51 | struct Options |
| 52 | { |
| 53 | /** \brief enables link-layer reliability |
| 54 | */ |
| 55 | bool isEnabled = false; |
| 56 | |
| 57 | /** \brief maximum number of retransmissions for an LpPacket |
| 58 | */ |
| 59 | size_t maxRetx = 3; |
| 60 | |
| 61 | /** \brief period between sending pending Acks in an IDLE packet |
| 62 | */ |
| 63 | time::nanoseconds idleAckTimerPeriod = time::milliseconds(5); |
| 64 | |
| 65 | /** \brief a fragment is considered lost if this number of fragments with greater sequence |
| 66 | * numbers are acknowledged |
| 67 | */ |
| 68 | size_t seqNumLossThreshold = 3; |
| 69 | }; |
| 70 | |
| 71 | LpReliability(const Options& options, GenericLinkService* linkService); |
| 72 | |
| 73 | /** \brief set options for reliability |
| 74 | */ |
| 75 | void |
| 76 | setOptions(const Options& options); |
| 77 | |
| 78 | /** \return GenericLinkService that owns this instance |
| 79 | * |
| 80 | * This is only used for logging, and may be nullptr. |
| 81 | */ |
| 82 | const GenericLinkService* |
| 83 | getLinkService() const; |
| 84 | |
| 85 | /** \brief observe outgoing fragment(s) of a network packet |
| 86 | * \param frags fragments of network packet |
| 87 | */ |
| 88 | void |
| 89 | observeOutgoing(const std::vector<lp::Packet>& frags); |
| 90 | |
| 91 | /** \brief extract and parse all Acks and add Ack for contained Fragment (if any) to AckQueue |
| 92 | * \param pkt incoming LpPacket |
| 93 | */ |
| 94 | void |
| 95 | processIncomingPacket(const lp::Packet& pkt); |
| 96 | |
| 97 | /** \brief called by GenericLinkService to attach Acks onto an outgoing LpPacket |
| 98 | * \param pkt outgoing LpPacket to attach Acks to |
| 99 | * \param mtu MTU of the Transport |
| 100 | */ |
| 101 | void |
| 102 | piggyback(lp::Packet& pkt, ssize_t mtu); |
| 103 | |
| 104 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 105 | /** \brief start the idle Ack timer |
| 106 | * |
| 107 | * This timer requests an IDLE packet to acknowledge pending fragments not already piggybacked. |
| 108 | * It is called regularly on a period configured in Options::idleAckTimerPeriod. This allows Acks |
| 109 | * to be returned to the sender, even if the link goes idle. |
| 110 | */ |
| 111 | void |
| 112 | startIdleAckTimer(); |
| 113 | |
| 114 | /** \brief cancel the idle Ack timer |
| 115 | */ |
| 116 | void |
| 117 | stopIdleAckTimer(); |
| 118 | |
| 119 | private: |
| 120 | /** \brief find and mark as lost fragments where a configurable number of Acks have been received |
| 121 | * for greater sequence numbers |
| 122 | * \param ackSeq sequence number of received Ack |
| 123 | * \return vector containing sequence numbers marked lost by this mechanism |
| 124 | */ |
| 125 | std::vector<lp::Sequence> |
| 126 | findLostLpPackets(lp::Sequence ackSeq); |
| 127 | |
| 128 | /** \brief resend (or declare as lost) a lost fragment |
| 129 | */ |
| 130 | void |
| 131 | onLpPacketLost(lp::Sequence seq); |
| 132 | |
| 133 | class UnackedFrag; |
| 134 | class NetPkt; |
| 135 | |
| 136 | /** \brief remove the fragment with the given sequence number from the map of unacknowledged |
| 137 | * fragments as well as its associated network packet |
| 138 | * \param fragIt iterator to fragment to be removed |
| 139 | * |
| 140 | * If the given sequence marks the beginning of the send window, the window will be incremented. |
| 141 | * If the associated network packet has been fully transmitted, it will be removed. |
| 142 | */ |
| 143 | void |
| 144 | onLpPacketAcknowledged(std::map<lp::Sequence, UnackedFrag>::iterator fragIt, |
| 145 | std::map<lp::Sequence, NetPkt>::iterator netPktIt); |
| 146 | |
| 147 | std::map<lp::Sequence, NetPkt>::iterator |
| 148 | getNetPktByFrag(lp::Sequence seq); |
| 149 | |
| 150 | private: |
| 151 | /** \brief contains a sent fragment that has not been acknowledged and associated data |
| 152 | */ |
| 153 | class UnackedFrag |
| 154 | { |
| 155 | public: |
| 156 | // Allows implicit conversion from an lp::Packet |
| 157 | UnackedFrag(lp::Packet pkt); |
| 158 | |
| 159 | public: |
| 160 | lp::Packet pkt; |
| 161 | scheduler::ScopedEventId rtoTimer; |
| 162 | time::steady_clock::TimePoint sendTime; |
| 163 | size_t retxCount; |
| 164 | size_t nGreaterSeqAcks; //!< number of Acks received for sequences greater than this fragment |
| 165 | bool wasTimedOutBySeq; //!< whether this fragment has been timed out by the sequence number mechanic |
| 166 | }; |
| 167 | |
| 168 | /** \brief contains a network-layer packet with unacknowledged fragments |
| 169 | */ |
| 170 | class NetPkt |
| 171 | { |
| 172 | public: |
| 173 | NetPkt(); |
| 174 | |
| 175 | public: |
| 176 | std::set<lp::Sequence> unackedFrags; |
| 177 | bool didRetx; |
| 178 | }; |
| 179 | |
| 180 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 181 | Options m_options; |
| 182 | GenericLinkService* m_linkService; |
| 183 | std::map<lp::Sequence, UnackedFrag> m_unackedFrags; |
| 184 | std::map<lp::Sequence, UnackedFrag>::iterator m_firstUnackedFrag; |
| 185 | std::map<lp::Sequence, NetPkt> m_netPkts; |
| 186 | std::queue<lp::Sequence> m_ackQueue; |
| 187 | scheduler::ScopedEventId m_idleAckTimer; |
| 188 | bool m_isIdleAckTimerRunning; |
| 189 | RttEstimator m_rto; |
| 190 | }; |
| 191 | |
| 192 | } // namespace face |
| 193 | } // namespace nfd |
| 194 | |
| 195 | #endif // NFD_DAEMON_FACE_LP_RELIABILITY_HPP |